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