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