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