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