arm: tegra: Implement sensor board file for cardhu.
[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 <generated/mach-types.h>
31 #include "gpio-names.h"
32 #include "board.h"
33 #include <linux/mpu.h>
34
35 #include <mach/gpio.h>
36
37 #include "gpio-names.h"
38 #include "board-cardhu.h"
39
40 static struct regulator *cardhu_1v8_cam1 = NULL;
41 static struct regulator *cardhu_avdd_dsi_csi = NULL;
42 static struct regulator *cardhu_vdd_2v8_cam1 = NULL;
43
44 #ifdef CONFIG_I2C_MUX_PCA954x
45 static struct pca954x_platform_mode cardhu_pca954x_modes[] = {
46         { .adap_id = PCA954x_I2C_BUS0, },
47         { .adap_id = PCA954x_I2C_BUS1, },
48         { .adap_id = PCA954x_I2C_BUS2, },
49         { .adap_id = PCA954x_I2C_BUS3, },
50 };
51
52 static struct pca954x_platform_data cardhu_pca954x_data = {
53         .modes    = cardhu_pca954x_modes,
54         .num_modes      = ARRAY_SIZE(cardhu_pca954x_modes),
55 };
56 #endif
57
58 static int cardhu_camera_init(void)
59 {
60         int ret;
61         struct board_info board_info;
62
63         tegra_get_board_info(&board_info);
64         /* Boards E1198 and E1291 are of Cardhu personality
65          * and donot have TCA6416 exp for camera */
66         if ((board_info.board_id == BOARD_E1198) ||
67                 (board_info.board_id == BOARD_E1291)) {
68                 tegra_gpio_enable(CAM1_POWER_DWN_GPIO);
69                 ret = gpio_request(CAM1_POWER_DWN_GPIO, "camera_power_en");
70                 if (ret < 0)
71                         pr_err("%s: gpio_request failed for gpio %s\n",
72                                 __func__, "CAM1_POWER_DWN_GPIO");
73                 tegra_gpio_enable(OV5650_RESETN_GPIO);
74                 ret = gpio_request(OV5650_RESETN_GPIO, "camera_reset");
75                 if (ret < 0)
76                         pr_err("%s: gpio_request failed for gpio %s\n",
77                                 __func__, "OV5650_RESETN_GPIO");
78                 gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
79                 mdelay(10);
80
81                 gpio_direction_output(OV5650_RESETN_GPIO, 1);
82                 mdelay(5);
83                 gpio_direction_output(OV5650_RESETN_GPIO, 0);
84                 mdelay(5);
85                 gpio_direction_output(OV5650_RESETN_GPIO, 1);
86                 mdelay(5);
87
88         }
89
90         /* To select the CSIB MUX either for cam2 or cam3 */
91         tegra_gpio_enable(CAMERA_CSI_MUX_SEL_GPIO);
92         ret = gpio_request(CAMERA_CSI_MUX_SEL_GPIO, "camera_csi_sel");
93         if (ret < 0)
94                 pr_err("%s: gpio_request failed for gpio %s\n",
95                         __func__, "CAMERA_CSI_MUX_SEL_GPIO");
96         gpio_direction_output(CAMERA_CSI_MUX_SEL_GPIO, 0);
97         gpio_export(CAMERA_CSI_MUX_SEL_GPIO, false);
98
99         return 0;
100 }
101
102 static int cardhu_ov5650_power_on(void)
103 {
104         int ret;
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 const struct i2c_board_info cardhu_i2c3_board_info[] = {
194 #ifdef CONFIG_I2C_MUX_PCA954x
195         {
196                 I2C_BOARD_INFO("pca9546", 0x70),
197                 .platform_data = &cardhu_pca954x_data,
198         },
199 #endif
200 };
201 static struct i2c_board_info cardhu_i2c6_board_info[] = {
202         {
203                 I2C_BOARD_INFO("ov5650", 0x36),
204                 .platform_data = &cardhu_ov5650_data,
205         },
206 };
207
208 static struct nct1008_platform_data cardhu_nct1008_pdata = {
209         .supported_hwrev = true,
210         .ext_range = false,
211         .conv_rate = 0x08,
212         .offset = 0,
213         .hysteresis = 5,
214         .shutdown_ext_limit = 75,
215         .shutdown_local_limit = 75,
216         .throttling_ext_limit = 60,
217         .alarm_fn = NULL,
218 };
219
220 static struct i2c_board_info cardhu_i2c4_board_info[] = {
221         {
222                 I2C_BOARD_INFO("nct1008", 0x4C),
223                 .platform_data = &cardhu_nct1008_pdata,
224                 .irq = -1,
225         }
226 };
227
228 static int cardhu_nct1008_init(void)
229 {
230         int nct1008_port = -1;
231         struct board_info BoardInfo;
232         int ret;
233
234         tegra_get_board_info(&BoardInfo);
235         if ((BoardInfo.board_id == BOARD_E1198) ||
236                 (BoardInfo.board_id == BOARD_E1291)) {
237                 nct1008_port = TEGRA_GPIO_PCC2;
238         } else if ((BoardInfo.board_id == BOARD_E1186) ||
239                 (BoardInfo.board_id == BOARD_E1187)) {
240                 /* FIXME: seems to be conflicting with usb3 vbus on E1186 */
241                 /* nct1008_port = TEGRA_GPIO_PH7; */
242         }
243
244         if (nct1008_port >= 0) {
245                 /* FIXME: enable irq when throttling is supported */
246                 /* cardhu_i2c4_board_info[0].irq = */
247                 /* TEGRA_GPIO_TO_IRQ(nct1008_port); */
248
249                 ret = gpio_request(nct1008_port, "temp_alert");
250                 if (ret < 0)
251                         return ret;
252
253                 ret = gpio_direction_input(nct1008_port);
254                 if (ret < 0)
255                         gpio_free(nct1008_port);
256                 else
257                         tegra_gpio_enable(nct1008_port);
258
259         }
260         return ret;
261 }
262
263 #if defined(CONFIG_GPIO_PCA953X)
264 static struct pca953x_platform_data cardhu_pmu_tca6416_data = {
265         .gpio_base      = PMU_TCA6416_GPIO_BASE,
266 };
267
268 static const struct i2c_board_info cardhu_i2c4_board_info_tca6416[] = {
269         {
270                 I2C_BOARD_INFO("tca6416", 0x20),
271                 .platform_data = &cardhu_pmu_tca6416_data,
272         },
273 };
274
275 static struct pca953x_platform_data cardhu_cam_tca6416_data = {
276         .gpio_base      = CAM_TCA6416_GPIO_BASE,
277 };
278
279 static const struct i2c_board_info cardhu_i2c2_board_info_tca6416[] = {
280         {
281                 I2C_BOARD_INFO("tca6416", 0x20),
282                 .platform_data = &cardhu_cam_tca6416_data,
283         },
284 };
285
286 static int __init pmu_tca6416_init(void)
287 {
288         struct board_info board_info;
289         tegra_get_board_info(&board_info);
290         if ((board_info.board_id == BOARD_E1198) ||
291                         (board_info.board_id == BOARD_E1291))
292                 return 0;
293
294         pr_info("Registering pmu pca6416\n");
295         i2c_register_board_info(4, cardhu_i2c4_board_info_tca6416,
296                 ARRAY_SIZE(cardhu_i2c4_board_info_tca6416));
297         return 0;
298 }
299
300 static int __init cam_tca6416_init(void)
301 {
302         struct board_info board_info;
303         tegra_get_board_info(&board_info);
304         /* Boards E1198 and E1291 are of Cardhu personality
305          * and donot have TCA6416 exp for camera */
306         if ((board_info.board_id == BOARD_E1198) ||
307                 (board_info.board_id == BOARD_E1291))
308                 return 0;
309
310         pr_info("Registering cam pca6416\n");
311         i2c_register_board_info(2, cardhu_i2c2_board_info_tca6416,
312                 ARRAY_SIZE(cardhu_i2c2_board_info_tca6416));
313         return 0;
314 }
315 #else
316 static int __init pmu_tca6416_init(void)
317 {
318         return 0;
319 }
320
321 static int __init cam_tca6416_init(void)
322 {
323         return 0;
324 }
325 #endif
326
327 #ifdef CONFIG_SENSORS_MPU3050
328 #define SENSOR_MPU_NAME "mpu3050"
329 static struct mpu3050_platform_data mpu3050_data = {
330         .int_config  = 0x10,
331         .orientation = { 0, -1, 0, -1, 0, 0, 0, 0, -1 },  /* Orientation matrix for MPU on cardhu */
332         .level_shifter = 0,
333
334         .accel = {
335         .get_slave_descr = get_accel_slave_descr,
336         .adapt_num   = 2,
337         .bus         = EXT_SLAVE_BUS_SECONDARY,
338         .address     = 0x0F,
339         .orientation = { 0, -1, 0, -1, 0, 0, 0, 0, -1 },  /* Orientation matrix for Kionix on cardhu */
340         },
341
342         .compass = {
343         .get_slave_descr = get_compass_slave_descr,
344         .adapt_num   = 2,
345         .bus         = EXT_SLAVE_BUS_PRIMARY,
346         .address     = 0x0C,
347         .orientation = { 1, 0, 0, 0, -1, 0, 0, 0, -1 },  /* Orientation matrix for AKM on cardhu */
348         },
349 };
350
351 static struct i2c_board_info __initdata mpu3050_i2c0_boardinfo[] = {
352         {
353                 I2C_BOARD_INFO(SENSOR_MPU_NAME, 0x68),
354                 .irq = TEGRA_GPIO_TO_IRQ(TEGRA_GPIO_PX1),
355                 .platform_data = &mpu3050_data,
356         },
357 };
358
359 static void cardhu_mpuirq_init(void)
360 {
361         pr_info("*** MPU START *** cardhu_mpuirq_init...\n");
362         tegra_gpio_enable(TEGRA_GPIO_PX1);
363         gpio_request(TEGRA_GPIO_PX1, SENSOR_MPU_NAME);
364         gpio_direction_input(TEGRA_GPIO_PX1);
365         pr_info("*** MPU END *** cardhu_mpuirq_init...\n");
366 }
367 #endif
368
369
370 int __init cardhu_sensors_init(void)
371 {
372         int err;
373
374         cardhu_camera_init();
375         cam_tca6416_init();
376
377         i2c_register_board_info(2, cardhu_i2c3_board_info,
378                 ARRAY_SIZE(cardhu_i2c3_board_info));
379
380         /* CAM-A is on BUS0 to set CAM-B need to change
381          * PCA954x_I2C_BUS0 to PCA954x_I2C_BUS1 */
382         i2c_register_board_info(PCA954x_I2C_BUS0, cardhu_i2c6_board_info,
383                 ARRAY_SIZE(cardhu_i2c6_board_info));
384
385         pmu_tca6416_init();
386
387         i2c_register_board_info(4, cardhu_i2c4_board_info,
388                 ARRAY_SIZE(cardhu_i2c4_board_info));
389
390         err = cardhu_nct1008_init();
391         if (err)
392                 return err;
393
394 #ifdef CONFIG_SENSORS_MPU3050
395         cardhu_mpuirq_init();
396 #endif
397
398         if (ARRAY_SIZE(cardhu_i2c3_board_info))
399                 i2c_register_board_info(3, cardhu_i2c3_board_info,
400                         ARRAY_SIZE(cardhu_i2c3_board_info));
401
402 #ifdef CONFIG_SENSORS_MPU3050
403         i2c_register_board_info(2, mpu3050_i2c0_boardinfo,
404                 ARRAY_SIZE(mpu3050_i2c0_boardinfo));
405 #endif
406
407         return 0;
408 }
409
410 #if defined(CONFIG_GPIO_PCA953X)
411 struct ov5650_gpios {
412         const char *name;
413         int gpio;
414         int enabled;
415 };
416
417 #define OV5650_GPIO(_name, _gpio, _enabled)             \
418         {                                               \
419                 .name = _name,                          \
420                 .gpio = _gpio,                          \
421                 .enabled = _enabled,                    \
422         }
423
424 static struct ov5650_gpios ov5650_gpio_keys[] = {
425         [0] = OV5650_GPIO("cam1_pwdn", CAM1_PWR_DN_GPIO, 0),
426         [1] = OV5650_GPIO("cam1_rst_lo", CAM1_RST_L_GPIO, 1),
427         [2] = OV5650_GPIO("cam1_af_pwdn_lo", CAM1_AF_PWR_DN_L_GPIO, 1),
428         [3] = OV5650_GPIO("cam1_ldo_shdn_lo", CAM1_LDO_SHUTDN_L_GPIO, 1),
429         [4] = OV5650_GPIO("cam2_pwdn", CAM2_PWR_DN_GPIO, 0),
430         [5] = OV5650_GPIO("cam2_rst_lo", CAM2_RST_L_GPIO, 1),
431         [6] = OV5650_GPIO("cam2_af_pwdn_lo", CAM2_AF_PWR_DN_L_GPIO, 1),
432         [7] = OV5650_GPIO("cam2_ldo_shdn_lo", CAM2_LDO_SHUTDN_L_GPIO, 1),
433         [8] = OV5650_GPIO("cam3_pwdn", CAM_FRONT_PWR_DN_GPIO, 0),
434         [9] = OV5650_GPIO("cam3_rst_lo", CAM_FRONT_RST_L_GPIO, 1),
435         [10] = OV5650_GPIO("cam3_af_pwdn_lo", CAM_FRONT_AF_PWR_DN_L_GPIO, 1),
436         [11] = OV5650_GPIO("cam3_ldo_shdn_lo", CAM_FRONT_LDO_SHUTDN_L_GPIO, 1),
437         [12] = OV5650_GPIO("cam_led_exp", CAM_FRONT_LED_EXP, 1),
438         [13] = OV5650_GPIO("cam_led_rear_exp", CAM_SNN_LED_REAR_EXP, 1),
439         [14] = OV5650_GPIO("cam_i2c_mux_rst", CAM_I2C_MUX_RST_EXP, 1),
440 };
441
442 int __init cardhu_ov5650_late_init(void)
443 {
444         int ret;
445         int i;
446
447         for (i = 0; i < ARRAY_SIZE(ov5650_gpio_keys); i++) {
448                 ret = gpio_request(ov5650_gpio_keys[i].gpio,
449                         ov5650_gpio_keys[i].name);
450                 if (ret < 0) {
451                         printk("%s: gpio_request failed for gpio #%d\n",
452                                 __func__, i);
453                         goto fail;
454                 }
455                 gpio_direction_output(ov5650_gpio_keys[i].gpio,
456                         ov5650_gpio_keys[i].enabled);
457                 gpio_export(ov5650_gpio_keys[i].gpio, false);
458         }
459
460         return 0;
461
462 fail:
463         while (i--)
464                 gpio_free(ov5650_gpio_keys[i].gpio);
465         return ret;
466 }
467
468 late_initcall(cardhu_ov5650_late_init);
469 #endif