arm: tegra: cardhu: Add OV2710 sensor driver for cardhu
[linux-2.6.git] / arch / arm / mach-tegra / board-cardhu-sensors.c
1 /*
2  * arch/arm/mach-tegra/board-cardhu-sensors.c
3  *
4  * Copyright (c) 2011, NVIDIA, All Rights Reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/i2c/pca954x.h>
25 #include <linux/i2c/pca953x.h>
26 #include <linux/nct1008.h>
27 #include <mach/fb.h>
28 #include <mach/gpio.h>
29 #include <media/ov5650.h>
30 #include <media/ov2710.h>
31 #include <generated/mach-types.h>
32 #include "gpio-names.h"
33 #include "board.h"
34 #include <linux/mpu.h>
35
36 #include <mach/gpio.h>
37
38 #include "gpio-names.h"
39 #include "board-cardhu.h"
40
41 static struct regulator *cardhu_1v8_cam1 = NULL;
42 static struct regulator *cardhu_avdd_dsi_csi = NULL;
43 static struct regulator *cardhu_vdd_2v8_cam1 = NULL;
44
45 #ifdef CONFIG_I2C_MUX_PCA954x
46 static struct pca954x_platform_mode cardhu_pca954x_modes[] = {
47         { .adap_id = PCA954x_I2C_BUS0, },
48         { .adap_id = PCA954x_I2C_BUS1, },
49         { .adap_id = PCA954x_I2C_BUS2, },
50         { .adap_id = PCA954x_I2C_BUS3, },
51 };
52
53 static struct pca954x_platform_data cardhu_pca954x_data = {
54         .modes    = cardhu_pca954x_modes,
55         .num_modes      = ARRAY_SIZE(cardhu_pca954x_modes),
56 };
57 #endif
58
59 static int cardhu_camera_init(void)
60 {
61         int ret;
62         struct board_info board_info;
63
64         tegra_get_board_info(&board_info);
65         /* Boards E1198 and E1291 are of Cardhu personality
66          * and donot have TCA6416 exp for camera */
67         if ((board_info.board_id == BOARD_E1198) ||
68                 (board_info.board_id == BOARD_E1291)) {
69                 tegra_gpio_enable(CAM1_POWER_DWN_GPIO);
70                 ret = gpio_request(CAM1_POWER_DWN_GPIO, "camera_power_en");
71                 if (ret < 0)
72                         pr_err("%s: gpio_request failed for gpio %s\n",
73                                 __func__, "CAM1_POWER_DWN_GPIO");
74                 tegra_gpio_enable(OV5650_RESETN_GPIO);
75                 ret = gpio_request(OV5650_RESETN_GPIO, "camera_reset");
76                 if (ret < 0)
77                         pr_err("%s: gpio_request failed for gpio %s\n",
78                                 __func__, "OV5650_RESETN_GPIO");
79                 gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
80                 mdelay(10);
81
82                 gpio_direction_output(OV5650_RESETN_GPIO, 1);
83                 mdelay(5);
84                 gpio_direction_output(OV5650_RESETN_GPIO, 0);
85                 mdelay(5);
86                 gpio_direction_output(OV5650_RESETN_GPIO, 1);
87                 mdelay(5);
88
89         }
90
91         /* To select the CSIB MUX either for cam2 or cam3 */
92         tegra_gpio_enable(CAMERA_CSI_MUX_SEL_GPIO);
93         ret = gpio_request(CAMERA_CSI_MUX_SEL_GPIO, "camera_csi_sel");
94         if (ret < 0)
95                 pr_err("%s: gpio_request failed for gpio %s\n",
96                         __func__, "CAMERA_CSI_MUX_SEL_GPIO");
97         gpio_direction_output(CAMERA_CSI_MUX_SEL_GPIO, 0);
98         gpio_export(CAMERA_CSI_MUX_SEL_GPIO, false);
99
100         return 0;
101 }
102
103 static int cardhu_ov5650_power_on(void)
104 {
105         int ret;
106         struct board_info board_info;
107         tegra_get_board_info(&board_info);
108         /* Boards E1198 and E1291 are of Cardhu personality
109          * and donot have TCA6416 exp for camera */
110         if ((board_info.board_id == BOARD_E1198) ||
111                 (board_info.board_id == BOARD_E1291)) {
112
113                 gpio_direction_output(CAM1_POWER_DWN_GPIO, 0);
114                 mdelay(10);
115
116                 if (cardhu_vdd_2v8_cam1 == NULL) {
117                         cardhu_vdd_2v8_cam1 = regulator_get(NULL, "vdd_2v8_cam1");
118                         if (WARN_ON(IS_ERR(cardhu_vdd_2v8_cam1))) {
119                                 pr_err("%s: couldn't get regulator vdd_2v8_cam1: %ld\n",
120                                         __func__, PTR_ERR(cardhu_vdd_2v8_cam1));
121                                 goto reg_alloc_fail;
122                         }
123                 }
124
125                 regulator_enable(cardhu_vdd_2v8_cam1);
126                 mdelay(5);
127         }
128         /* Enable VDD_1V8_Cam1 */
129         if (cardhu_1v8_cam1 == NULL) {
130                 cardhu_1v8_cam1 = regulator_get(NULL, "vdd_1v8_cam1");
131                 if (WARN_ON(IS_ERR(cardhu_1v8_cam1))) {
132                         pr_err("%s: couldn't get regulator vdd_1v8_cam1: %ld\n",
133                                 __func__, PTR_ERR(cardhu_1v8_cam1));
134                         goto reg_alloc_fail;
135                 }
136         }
137         regulator_enable(cardhu_1v8_cam1);
138
139         /* Enable AVDD_CSI_DSI */
140         if (cardhu_avdd_dsi_csi == NULL) {
141                 cardhu_avdd_dsi_csi = regulator_get(NULL, "avdd_dsi_csi");
142                 if (WARN_ON(IS_ERR(cardhu_avdd_dsi_csi))) {
143                         pr_err("%s: couldn't get regulator avdd_dsi_csi: %ld\n",
144                                 __func__, PTR_ERR(cardhu_avdd_dsi_csi));
145                         goto reg_alloc_fail;
146                 }
147         }
148         regulator_enable(cardhu_avdd_dsi_csi);
149         mdelay(5);
150         return 0;
151
152 reg_alloc_fail:
153         if (cardhu_1v8_cam1) {
154                 regulator_put(cardhu_1v8_cam1);
155                 cardhu_1v8_cam1 = NULL;
156         }
157         if (cardhu_avdd_dsi_csi) {
158                 regulator_put(cardhu_avdd_dsi_csi);
159                 cardhu_avdd_dsi_csi = NULL;
160         }
161         if (cardhu_vdd_2v8_cam1) {
162                 regulator_put(cardhu_vdd_2v8_cam1);
163                 cardhu_vdd_2v8_cam1 = NULL;
164         }
165         return -ENODEV;
166
167 }
168
169 static int cardhu_ov5650_power_off(void)
170 {
171         struct board_info board_info;
172         tegra_get_board_info(&board_info);
173         /* Boards E1198 and E1291 are of Cardhu personality
174          * and donot have TCA6416 exp for camera */
175         if ((board_info.board_id == BOARD_E1198) ||
176                 (board_info.board_id == BOARD_E1291)) {
177                 gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
178         }
179         if (cardhu_1v8_cam1)
180                 regulator_disable(cardhu_1v8_cam1);
181         if (cardhu_avdd_dsi_csi)
182                 regulator_disable(cardhu_avdd_dsi_csi);
183         if (cardhu_vdd_2v8_cam1)
184                 regulator_disable(cardhu_vdd_2v8_cam1);
185
186         return 0;
187 }
188
189 struct ov5650_platform_data cardhu_ov5650_data = {
190         .power_on = cardhu_ov5650_power_on,
191         .power_off = cardhu_ov5650_power_off,
192 };
193
194 static int cardhu_ov2710_power_on(void)
195 {
196         cardhu_ov5650_power_on();
197         gpio_direction_output(CAMERA_CSI_MUX_SEL_GPIO, 1);
198         return 0;
199 }
200
201 static int cardhu_ov2710_power_off(void)
202 {
203         cardhu_ov5650_power_off();
204         return 0;
205 }
206
207 struct ov2710_platform_data cardhu_ov2710_data = {
208         .power_on = cardhu_ov2710_power_on,
209         .power_off = cardhu_ov2710_power_off,
210 };
211
212 static const struct i2c_board_info cardhu_i2c3_board_info[] = {
213 #ifdef CONFIG_I2C_MUX_PCA954x
214         {
215                 I2C_BOARD_INFO("pca9546", 0x70),
216                 .platform_data = &cardhu_pca954x_data,
217         },
218 #endif
219 };
220 static struct i2c_board_info cardhu_i2c6_board_info[] = {
221         {
222                 I2C_BOARD_INFO("ov5650", 0x36),
223                 .platform_data = &cardhu_ov5650_data,
224         },
225 };
226
227 static struct i2c_board_info cardhu_i2c8_board_info[] = {
228         {
229                 I2C_BOARD_INFO("ov2710", 0x36),
230                 .platform_data = &cardhu_ov2710_data,
231         },
232 };
233
234 static struct nct1008_platform_data cardhu_nct1008_pdata = {
235         .supported_hwrev = true,
236         .ext_range = false,
237         .conv_rate = 0x08,
238         .offset = 0,
239         .hysteresis = 5,
240         .shutdown_ext_limit = 75,
241         .shutdown_local_limit = 75,
242         .throttling_ext_limit = 60,
243         .alarm_fn = NULL,
244 };
245
246 static struct i2c_board_info cardhu_i2c4_board_info[] = {
247         {
248                 I2C_BOARD_INFO("nct1008", 0x4C),
249                 .platform_data = &cardhu_nct1008_pdata,
250                 .irq = -1,
251         }
252 };
253
254 static int cardhu_nct1008_init(void)
255 {
256         int nct1008_port = -1;
257         struct board_info BoardInfo;
258         int ret;
259
260         tegra_get_board_info(&BoardInfo);
261         if ((BoardInfo.board_id == BOARD_E1198) ||
262                 (BoardInfo.board_id == BOARD_E1291)) {
263                 nct1008_port = TEGRA_GPIO_PCC2;
264         } else if ((BoardInfo.board_id == BOARD_E1186) ||
265                 (BoardInfo.board_id == BOARD_E1187)) {
266                 /* FIXME: seems to be conflicting with usb3 vbus on E1186 */
267                 /* nct1008_port = TEGRA_GPIO_PH7; */
268         }
269
270         if (nct1008_port >= 0) {
271                 /* FIXME: enable irq when throttling is supported */
272                 /* cardhu_i2c4_board_info[0].irq = */
273                 /* TEGRA_GPIO_TO_IRQ(nct1008_port); */
274
275                 ret = gpio_request(nct1008_port, "temp_alert");
276                 if (ret < 0)
277                         return ret;
278
279                 ret = gpio_direction_input(nct1008_port);
280                 if (ret < 0)
281                         gpio_free(nct1008_port);
282                 else
283                         tegra_gpio_enable(nct1008_port);
284
285         }
286         return ret;
287 }
288
289 #if defined(CONFIG_GPIO_PCA953X)
290 static struct pca953x_platform_data cardhu_pmu_tca6416_data = {
291         .gpio_base      = PMU_TCA6416_GPIO_BASE,
292 };
293
294 static const struct i2c_board_info cardhu_i2c4_board_info_tca6416[] = {
295         {
296                 I2C_BOARD_INFO("tca6416", 0x20),
297                 .platform_data = &cardhu_pmu_tca6416_data,
298         },
299 };
300
301 static struct pca953x_platform_data cardhu_cam_tca6416_data = {
302         .gpio_base      = CAM_TCA6416_GPIO_BASE,
303 };
304
305 static const struct i2c_board_info cardhu_i2c2_board_info_tca6416[] = {
306         {
307                 I2C_BOARD_INFO("tca6416", 0x20),
308                 .platform_data = &cardhu_cam_tca6416_data,
309         },
310 };
311
312 static int __init pmu_tca6416_init(void)
313 {
314         struct board_info board_info;
315         tegra_get_board_info(&board_info);
316         if ((board_info.board_id == BOARD_E1198) ||
317                         (board_info.board_id == BOARD_E1291))
318                 return 0;
319
320         pr_info("Registering pmu pca6416\n");
321         i2c_register_board_info(4, cardhu_i2c4_board_info_tca6416,
322                 ARRAY_SIZE(cardhu_i2c4_board_info_tca6416));
323         return 0;
324 }
325
326 static int __init cam_tca6416_init(void)
327 {
328         struct board_info board_info;
329         tegra_get_board_info(&board_info);
330         /* Boards E1198 and E1291 are of Cardhu personality
331          * and donot have TCA6416 exp for camera */
332         if ((board_info.board_id == BOARD_E1198) ||
333                 (board_info.board_id == BOARD_E1291))
334                 return 0;
335
336         pr_info("Registering cam pca6416\n");
337         i2c_register_board_info(2, cardhu_i2c2_board_info_tca6416,
338                 ARRAY_SIZE(cardhu_i2c2_board_info_tca6416));
339         return 0;
340 }
341 #else
342 static int __init pmu_tca6416_init(void)
343 {
344         return 0;
345 }
346
347 static int __init cam_tca6416_init(void)
348 {
349         return 0;
350 }
351 #endif
352
353 #ifdef CONFIG_SENSORS_MPU3050
354 #define SENSOR_MPU_NAME "mpu3050"
355 static struct mpu3050_platform_data mpu3050_data = {
356         .int_config  = 0x10,
357         .orientation = { 0, -1, 0, -1, 0, 0, 0, 0, -1 },  /* Orientation matrix for MPU on cardhu */
358         .level_shifter = 0,
359
360         .accel = {
361         .get_slave_descr = get_accel_slave_descr,
362         .adapt_num   = 2,
363         .bus         = EXT_SLAVE_BUS_SECONDARY,
364         .address     = 0x0F,
365         .orientation = { 0, -1, 0, -1, 0, 0, 0, 0, -1 },  /* Orientation matrix for Kionix on cardhu */
366         },
367
368         .compass = {
369         .get_slave_descr = get_compass_slave_descr,
370         .adapt_num   = 2,
371         .bus         = EXT_SLAVE_BUS_PRIMARY,
372         .address     = 0x0C,
373         .orientation = { 1, 0, 0, 0, -1, 0, 0, 0, -1 },  /* Orientation matrix for AKM on cardhu */
374         },
375 };
376
377 static struct i2c_board_info __initdata mpu3050_i2c0_boardinfo[] = {
378         {
379                 I2C_BOARD_INFO(SENSOR_MPU_NAME, 0x68),
380                 .irq = TEGRA_GPIO_TO_IRQ(TEGRA_GPIO_PX1),
381                 .platform_data = &mpu3050_data,
382         },
383 };
384
385 static void cardhu_mpuirq_init(void)
386 {
387         pr_info("*** MPU START *** cardhu_mpuirq_init...\n");
388         tegra_gpio_enable(TEGRA_GPIO_PX1);
389         gpio_request(TEGRA_GPIO_PX1, SENSOR_MPU_NAME);
390         gpio_direction_input(TEGRA_GPIO_PX1);
391         pr_info("*** MPU END *** cardhu_mpuirq_init...\n");
392 }
393 #endif
394
395
396 int __init cardhu_sensors_init(void)
397 {
398         int err;
399
400         cardhu_camera_init();
401         cam_tca6416_init();
402
403         i2c_register_board_info(2, cardhu_i2c3_board_info,
404                 ARRAY_SIZE(cardhu_i2c3_board_info));
405
406         /* CAM-A is on BUS0 to set CAM-B need to change
407          * PCA954x_I2C_BUS0 to PCA954x_I2C_BUS1 */
408         i2c_register_board_info(PCA954x_I2C_BUS0, cardhu_i2c6_board_info,
409                 ARRAY_SIZE(cardhu_i2c6_board_info));
410
411         i2c_register_board_info(PCA954x_I2C_BUS2, cardhu_i2c8_board_info,
412                 ARRAY_SIZE(cardhu_i2c8_board_info));
413
414         pmu_tca6416_init();
415
416         i2c_register_board_info(4, cardhu_i2c4_board_info,
417                 ARRAY_SIZE(cardhu_i2c4_board_info));
418
419         err = cardhu_nct1008_init();
420         if (err)
421                 return err;
422
423 #ifdef CONFIG_SENSORS_MPU3050
424         cardhu_mpuirq_init();
425 #endif
426
427         if (ARRAY_SIZE(cardhu_i2c3_board_info))
428                 i2c_register_board_info(3, cardhu_i2c3_board_info,
429                         ARRAY_SIZE(cardhu_i2c3_board_info));
430
431 #ifdef CONFIG_SENSORS_MPU3050
432         i2c_register_board_info(2, mpu3050_i2c0_boardinfo,
433                 ARRAY_SIZE(mpu3050_i2c0_boardinfo));
434 #endif
435
436         return 0;
437 }
438
439 #if defined(CONFIG_GPIO_PCA953X)
440 struct ov5650_gpios {
441         const char *name;
442         int gpio;
443         int enabled;
444 };
445
446 #define OV5650_GPIO(_name, _gpio, _enabled)             \
447         {                                               \
448                 .name = _name,                          \
449                 .gpio = _gpio,                          \
450                 .enabled = _enabled,                    \
451         }
452
453 static struct ov5650_gpios ov5650_gpio_keys[] = {
454         [0] = OV5650_GPIO("cam1_pwdn", CAM1_PWR_DN_GPIO, 0),
455         [1] = OV5650_GPIO("cam1_rst_lo", CAM1_RST_L_GPIO, 1),
456         [2] = OV5650_GPIO("cam1_af_pwdn_lo", CAM1_AF_PWR_DN_L_GPIO, 1),
457         [3] = OV5650_GPIO("cam1_ldo_shdn_lo", CAM1_LDO_SHUTDN_L_GPIO, 1),
458         [4] = OV5650_GPIO("cam2_pwdn", CAM2_PWR_DN_GPIO, 0),
459         [5] = OV5650_GPIO("cam2_rst_lo", CAM2_RST_L_GPIO, 1),
460         [6] = OV5650_GPIO("cam2_af_pwdn_lo", CAM2_AF_PWR_DN_L_GPIO, 1),
461         [7] = OV5650_GPIO("cam2_ldo_shdn_lo", CAM2_LDO_SHUTDN_L_GPIO, 1),
462         [8] = OV5650_GPIO("cam3_pwdn", CAM_FRONT_PWR_DN_GPIO, 0),
463         [9] = OV5650_GPIO("cam3_rst_lo", CAM_FRONT_RST_L_GPIO, 1),
464         [10] = OV5650_GPIO("cam3_af_pwdn_lo", CAM_FRONT_AF_PWR_DN_L_GPIO, 1),
465         [11] = OV5650_GPIO("cam3_ldo_shdn_lo", CAM_FRONT_LDO_SHUTDN_L_GPIO, 1),
466         [12] = OV5650_GPIO("cam_led_exp", CAM_FRONT_LED_EXP, 1),
467         [13] = OV5650_GPIO("cam_led_rear_exp", CAM_SNN_LED_REAR_EXP, 1),
468         [14] = OV5650_GPIO("cam_i2c_mux_rst", CAM_I2C_MUX_RST_EXP, 1),
469 };
470
471 int __init cardhu_ov5650_late_init(void)
472 {
473         int ret;
474         int i;
475
476         for (i = 0; i < ARRAY_SIZE(ov5650_gpio_keys); i++) {
477                 ret = gpio_request(ov5650_gpio_keys[i].gpio,
478                         ov5650_gpio_keys[i].name);
479                 if (ret < 0) {
480                         printk("%s: gpio_request failed for gpio #%d\n",
481                                 __func__, i);
482                         goto fail;
483                 }
484                 gpio_direction_output(ov5650_gpio_keys[i].gpio,
485                         ov5650_gpio_keys[i].enabled);
486                 gpio_export(ov5650_gpio_keys[i].gpio, false);
487         }
488
489         return 0;
490
491 fail:
492         while (i--)
493                 gpio_free(ov5650_gpio_keys[i].gpio);
494         return ret;
495 }
496
497 late_initcall(cardhu_ov5650_late_init);
498 #endif