86a8d44fadc9b2d5a04c1c0d6fa6c797f0bb8436
[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                 I2C_BOARD_INFO("sh532u", 0x72),
268         },
269 };
270
271 static struct i2c_board_info cardhu_i2c8_board_info[] = {
272         {
273                 I2C_BOARD_INFO("ov2710", 0x36),
274                 .platform_data = &cardhu_ov2710_data,
275         },
276 };
277
278 static struct nct1008_platform_data cardhu_nct1008_pdata = {
279         .supported_hwrev = true,
280         .ext_range = false,
281         .conv_rate = 0x08,
282         .offset = 0,
283         .hysteresis = 5,
284         .shutdown_ext_limit = 75,
285         .shutdown_local_limit = 75,
286         .throttling_ext_limit = 60,
287         .alarm_fn = NULL,
288 };
289
290 static struct i2c_board_info cardhu_i2c4_board_info[] = {
291         {
292                 I2C_BOARD_INFO("bq27510", 0x55),
293         },
294         {
295                 I2C_BOARD_INFO("nct1008", 0x4C),
296                 .platform_data = &cardhu_nct1008_pdata,
297                 .irq = -1,
298         }
299 };
300
301 static int cardhu_nct1008_init(void)
302 {
303         int nct1008_port = -1;
304         struct board_info BoardInfo;
305         int ret;
306
307         tegra_get_board_info(&BoardInfo);
308         if ((BoardInfo.board_id == BOARD_E1198) ||
309                 (BoardInfo.board_id == BOARD_E1291)) {
310                 nct1008_port = TEGRA_GPIO_PCC2;
311         } else if ((BoardInfo.board_id == BOARD_E1186) ||
312                 (BoardInfo.board_id == BOARD_E1187)) {
313                 /* FIXME: seems to be conflicting with usb3 vbus on E1186 */
314                 /* nct1008_port = TEGRA_GPIO_PH7; */
315         }
316
317         if (nct1008_port >= 0) {
318                 /* FIXME: enable irq when throttling is supported */
319                 /* cardhu_i2c4_board_info[0].irq = */
320                 /* TEGRA_GPIO_TO_IRQ(nct1008_port); */
321
322                 ret = gpio_request(nct1008_port, "temp_alert");
323                 if (ret < 0)
324                         return ret;
325
326                 ret = gpio_direction_input(nct1008_port);
327                 if (ret < 0)
328                         gpio_free(nct1008_port);
329                 else
330                         tegra_gpio_enable(nct1008_port);
331
332         }
333         return ret;
334 }
335
336 #if defined(CONFIG_GPIO_PCA953X)
337 static struct pca953x_platform_data cardhu_pmu_tca6416_data = {
338         .gpio_base      = PMU_TCA6416_GPIO_BASE,
339 };
340
341 static const struct i2c_board_info cardhu_i2c4_board_info_tca6416[] = {
342         {
343                 I2C_BOARD_INFO("tca6416", 0x20),
344                 .platform_data = &cardhu_pmu_tca6416_data,
345         },
346 };
347
348 static struct pca953x_platform_data cardhu_cam_tca6416_data = {
349         .gpio_base      = CAM_TCA6416_GPIO_BASE,
350 };
351
352 static const struct i2c_board_info cardhu_i2c2_board_info_tca6416[] = {
353         {
354                 I2C_BOARD_INFO("tca6416", 0x20),
355                 .platform_data = &cardhu_cam_tca6416_data,
356         },
357 };
358
359 static int __init pmu_tca6416_init(void)
360 {
361         struct board_info board_info;
362         tegra_get_board_info(&board_info);
363         if ((board_info.board_id == BOARD_E1198) ||
364                         (board_info.board_id == BOARD_E1291))
365                 return 0;
366
367         pr_info("Registering pmu pca6416\n");
368         i2c_register_board_info(4, cardhu_i2c4_board_info_tca6416,
369                 ARRAY_SIZE(cardhu_i2c4_board_info_tca6416));
370         return 0;
371 }
372
373 static int __init cam_tca6416_init(void)
374 {
375         struct board_info board_info;
376         tegra_get_board_info(&board_info);
377         /* Boards E1198 and E1291 are of Cardhu personality
378          * and donot have TCA6416 exp for camera */
379         if ((board_info.board_id == BOARD_E1198) ||
380                 (board_info.board_id == BOARD_E1291))
381                 return 0;
382
383         pr_info("Registering cam pca6416\n");
384         i2c_register_board_info(2, cardhu_i2c2_board_info_tca6416,
385                 ARRAY_SIZE(cardhu_i2c2_board_info_tca6416));
386         return 0;
387 }
388 #else
389 static int __init pmu_tca6416_init(void)
390 {
391         return 0;
392 }
393
394 static int __init cam_tca6416_init(void)
395 {
396         return 0;
397 }
398 #endif
399
400 #ifdef CONFIG_SENSORS_MPU3050
401 #define SENSOR_MPU_NAME "mpu3050"
402 static struct mpu3050_platform_data mpu3050_data = {
403         .int_config  = 0x10,
404         .orientation = { 0, -1, 0, -1, 0, 0, 0, 0, -1 },  /* Orientation matrix for MPU on cardhu */
405         .level_shifter = 0,
406
407         .accel = {
408         .get_slave_descr = get_accel_slave_descr,
409         .adapt_num   = 2,
410         .bus         = EXT_SLAVE_BUS_SECONDARY,
411         .address     = 0x0F,
412         .orientation = { 0, -1, 0, -1, 0, 0, 0, 0, -1 },  /* Orientation matrix for Kionix on cardhu */
413         },
414
415         .compass = {
416         .get_slave_descr = get_compass_slave_descr,
417         .adapt_num   = 2,
418         .bus         = EXT_SLAVE_BUS_PRIMARY,
419         .address     = 0x0C,
420         .orientation = { 1, 0, 0, 0, -1, 0, 0, 0, -1 },  /* Orientation matrix for AKM on cardhu */
421         },
422 };
423
424 static struct i2c_board_info __initdata mpu3050_i2c0_boardinfo[] = {
425         {
426                 I2C_BOARD_INFO(SENSOR_MPU_NAME, 0x68),
427                 .irq = TEGRA_GPIO_TO_IRQ(TEGRA_GPIO_PX1),
428                 .platform_data = &mpu3050_data,
429         },
430 };
431
432 static void cardhu_mpuirq_init(void)
433 {
434         pr_info("*** MPU START *** cardhu_mpuirq_init...\n");
435         tegra_gpio_enable(TEGRA_GPIO_PX1);
436         gpio_request(TEGRA_GPIO_PX1, SENSOR_MPU_NAME);
437         gpio_direction_input(TEGRA_GPIO_PX1);
438         pr_info("*** MPU END *** cardhu_mpuirq_init...\n");
439 }
440 #endif
441
442
443 int __init cardhu_sensors_init(void)
444 {
445         int err;
446
447         cardhu_camera_init();
448         cam_tca6416_init();
449
450         i2c_register_board_info(2, cardhu_i2c3_board_info,
451                 ARRAY_SIZE(cardhu_i2c3_board_info));
452
453         /* CAM-A is on BUS0 to set CAM-B need to change
454          * PCA954x_I2C_BUS0 to PCA954x_I2C_BUS1 */
455         i2c_register_board_info(PCA954x_I2C_BUS0, cardhu_i2c6_board_info,
456                 ARRAY_SIZE(cardhu_i2c6_board_info));
457
458         i2c_register_board_info(PCA954x_I2C_BUS2, cardhu_i2c8_board_info,
459                 ARRAY_SIZE(cardhu_i2c8_board_info));
460
461         pmu_tca6416_init();
462
463         i2c_register_board_info(4, cardhu_i2c4_board_info,
464                 ARRAY_SIZE(cardhu_i2c4_board_info));
465
466         err = cardhu_nct1008_init();
467         if (err)
468                 return err;
469
470 #ifdef CONFIG_SENSORS_MPU3050
471         cardhu_mpuirq_init();
472 #endif
473
474         if (ARRAY_SIZE(cardhu_i2c3_board_info))
475                 i2c_register_board_info(3, cardhu_i2c3_board_info,
476                         ARRAY_SIZE(cardhu_i2c3_board_info));
477
478 #ifdef CONFIG_SENSORS_MPU3050
479         i2c_register_board_info(2, mpu3050_i2c0_boardinfo,
480                 ARRAY_SIZE(mpu3050_i2c0_boardinfo));
481 #endif
482
483         return 0;
484 }
485
486 #if defined(CONFIG_GPIO_PCA953X)
487 struct ov5650_gpios {
488         const char *name;
489         int gpio;
490         int enabled;
491 };
492
493 #define OV5650_GPIO(_name, _gpio, _enabled)             \
494         {                                               \
495                 .name = _name,                          \
496                 .gpio = _gpio,                          \
497                 .enabled = _enabled,                    \
498         }
499
500 static struct ov5650_gpios ov5650_gpio_keys[] = {
501         [0] = OV5650_GPIO("cam1_pwdn", CAM1_PWR_DN_GPIO, 0),
502         [1] = OV5650_GPIO("cam1_rst_lo", CAM1_RST_L_GPIO, 1),
503         [2] = OV5650_GPIO("cam1_af_pwdn_lo", CAM1_AF_PWR_DN_L_GPIO, 1),
504         [3] = OV5650_GPIO("cam1_ldo_shdn_lo", CAM1_LDO_SHUTDN_L_GPIO, 1),
505         [4] = OV5650_GPIO("cam2_pwdn", CAM2_PWR_DN_GPIO, 0),
506         [5] = OV5650_GPIO("cam2_rst_lo", CAM2_RST_L_GPIO, 1),
507         [6] = OV5650_GPIO("cam2_af_pwdn_lo", CAM2_AF_PWR_DN_L_GPIO, 1),
508         [7] = OV5650_GPIO("cam2_ldo_shdn_lo", CAM2_LDO_SHUTDN_L_GPIO, 1),
509         [8] = OV5650_GPIO("cam3_pwdn", CAM_FRONT_PWR_DN_GPIO, 0),
510         [9] = OV5650_GPIO("cam3_rst_lo", CAM_FRONT_RST_L_GPIO, 1),
511         [10] = OV5650_GPIO("cam3_af_pwdn_lo", CAM_FRONT_AF_PWR_DN_L_GPIO, 1),
512         [11] = OV5650_GPIO("cam3_ldo_shdn_lo", CAM_FRONT_LDO_SHUTDN_L_GPIO, 1),
513         [12] = OV5650_GPIO("cam_led_exp", CAM_FRONT_LED_EXP, 1),
514         [13] = OV5650_GPIO("cam_led_rear_exp", CAM_SNN_LED_REAR_EXP, 1),
515         [14] = OV5650_GPIO("cam_i2c_mux_rst", CAM_I2C_MUX_RST_EXP, 1),
516 };
517
518 int __init cardhu_ov5650_late_init(void)
519 {
520         int ret;
521         int i;
522
523         for (i = 0; i < ARRAY_SIZE(ov5650_gpio_keys); i++) {
524                 ret = gpio_request(ov5650_gpio_keys[i].gpio,
525                         ov5650_gpio_keys[i].name);
526                 if (ret < 0) {
527                         printk("%s: gpio_request failed for gpio #%d\n",
528                                 __func__, i);
529                         goto fail;
530                 }
531                 gpio_direction_output(ov5650_gpio_keys[i].gpio,
532                         ov5650_gpio_keys[i].enabled);
533                 gpio_export(ov5650_gpio_keys[i].gpio, false);
534         }
535
536         return 0;
537
538 fail:
539         while (i--)
540                 gpio_free(ov5650_gpio_keys[i].gpio);
541         return ret;
542 }
543
544 late_initcall(cardhu_ov5650_late_init);
545 #endif