arm: tegra: cardhu: bringup of OV5650 on T30
[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 "board-cardhu.h"
34
35 static struct regulator *cardhu_1v8_cam1 = NULL;
36
37 #ifdef CONFIG_I2C_MUX_PCA954x
38 static struct pca954x_platform_mode cardhu_pca954x_modes[] = {
39         { .adap_id = PCA954x_I2C_BUS0, },
40         { .adap_id = PCA954x_I2C_BUS1, },
41         { .adap_id = PCA954x_I2C_BUS2, },
42         { .adap_id = PCA954x_I2C_BUS3, },
43 };
44
45 static struct pca954x_platform_data cardhu_pca954x_data = {
46         .modes    = cardhu_pca954x_modes,
47         .num_modes      = ARRAY_SIZE(cardhu_pca954x_modes),
48 };
49 #endif
50
51 static int cardhu_camera_init(void)
52 {
53         int ret;
54         struct board_info board_info;
55
56         tegra_get_board_info(&board_info);
57         /* Boards E1198 and E1291 are of Cardhu personality
58          * and donot have TCA6416 exp for camera */
59         if ((board_info.board_id == BOARD_E1198) ||
60                 (board_info.board_id == BOARD_E1291)) {
61                 tegra_gpio_enable(CAMERA_CSI_CAM_SEL_GPIO);
62                 ret = gpio_request(CAMERA_CSI_CAM_SEL_GPIO, "camera_cam_mclk");
63                 if (ret < 0)
64                         pr_err("%s: gpio_request failed for gpio %s\n",
65                                 __func__, "CAMERA_CSI_CAM_SEL_GPIO");
66                 gpio_direction_output(CAMERA_CSI_CAM_SEL_GPIO, 1);
67                 gpio_export(CAMERA_CSI_CAM_SEL_GPIO, false);
68         }
69
70         /* To select the CSIB MUX either for cam2 or cam3 */
71         tegra_gpio_enable(CAMERA_CSI_MUX_SEL_GPIO);
72         ret = gpio_request(CAMERA_CSI_MUX_SEL_GPIO, "camera_csi_sel");
73         if (ret < 0)
74                 pr_err("%s: gpio_request failed for gpio %s\n",
75                         __func__, "CAMERA_CSI_MUX_SEL_GPIO");
76         gpio_direction_output(CAMERA_CSI_MUX_SEL_GPIO, 0);
77         gpio_export(CAMERA_CSI_MUX_SEL_GPIO, false);
78
79         return 0;
80 }
81
82 static int cardhu_ov5650_power_on(void)
83 {
84         int ret;
85         struct board_info board_info;
86         tegra_get_board_info(&board_info);
87         /* Boards E1198 and E1291 are of Cardhu personality
88          * and donot have TCA6416 exp for camera */
89         if ((board_info.board_id == BOARD_E1198) ||
90                 (board_info.board_id == BOARD_E1291)) {
91                 tegra_gpio_enable(CAM1_POWER_DWN_GPIO);
92                 ret = gpio_request(CAM1_POWER_DWN_GPIO, "camera_power_en");
93                 if (ret < 0)
94                         pr_err("%s: gpio_request failed for gpio %s\n",
95                                 __func__, "CAM1_POWER_DWN_GPIO");
96                 gpio_direction_output(CAM1_POWER_DWN_GPIO, 0);
97                 gpio_export(CAM1_POWER_DWN_GPIO, false);
98
99                 mdelay(10);
100                 tegra_gpio_enable(CAM1_LDO_EN_GPIO);
101                 ret = gpio_request(CAM1_LDO_EN_GPIO, "camera_ldo");
102                 if (ret < 0)
103                         pr_err("%s: gpio_request failed for gpio %s\n",
104                                 __func__, "CAM1_LDO_EN_GPIO");
105                 gpio_direction_output(CAM1_LDO_EN_GPIO, 1);
106                 gpio_export(CAM1_LDO_EN_GPIO, false);
107
108                 tegra_gpio_enable(OV5650_RESETN_GPIO);
109                 ret = gpio_request(OV5650_RESETN_GPIO, "camera_reset");
110                 if (ret < 0)
111                         pr_err("%s: gpio_request failed for gpio %s\n",
112                                 __func__, "OV5650_RESETN_GPIO");
113                 gpio_direction_output(OV5650_RESETN_GPIO, 1);
114                 mdelay(5);
115                 gpio_direction_output(OV5650_RESETN_GPIO, 0);
116                 mdelay(5);
117                 gpio_direction_output(OV5650_RESETN_GPIO, 1);
118                 mdelay(5);
119         }
120         else {
121                 if (cardhu_1v8_cam1 == NULL) {
122                         cardhu_1v8_cam1 = regulator_get(NULL, "vdd_1v8_cam1");
123                         if (WARN_ON(IS_ERR(cardhu_1v8_cam1)))
124                                 pr_err("%s: couldn't get regulator vdd_1v8_cam1: %ld\n",
125                                         __func__, PTR_ERR(cardhu_1v8_cam1));
126                         else
127                                 regulator_enable(cardhu_1v8_cam1);
128                 }
129         }
130
131         return 0;
132 }
133
134 static int cardhu_ov5650_power_off(void)
135 {
136         struct board_info board_info;
137         tegra_get_board_info(&board_info);
138         /* Boards E1198 and E1291 are of Cardhu personality
139          * and donot have TCA6416 exp for camera */
140         if ((board_info.board_id == BOARD_E1198) ||
141                 (board_info.board_id == BOARD_E1291)) {
142                 gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
143                 gpio_direction_output(OV5650_RESETN_GPIO, 0);
144                 gpio_direction_output(CAM1_LDO_EN_GPIO, 0);
145         }
146         else {
147                 if (cardhu_1v8_cam1)
148                         regulator_disable(cardhu_1v8_cam1);
149         }
150
151         return 0;
152 }
153
154
155 struct ov5650_platform_data cardhu_ov5650_data = {
156         .power_on = cardhu_ov5650_power_on,
157         .power_off = cardhu_ov5650_power_off,
158 };
159
160 static const struct i2c_board_info cardhu_i2c3_board_info[] = {
161 #ifdef CONFIG_I2C_MUX_PCA954x
162         {
163                 I2C_BOARD_INFO("pca9546", 0x70),
164                 .platform_data = &cardhu_pca954x_data,
165         },
166 #endif
167 };
168 static struct i2c_board_info cardhu_i2c6_board_info[] = {
169         {
170                 I2C_BOARD_INFO("ov5650", 0x36),
171                 .platform_data = &cardhu_ov5650_data,
172         },
173 };
174
175 static struct nct1008_platform_data cardhu_nct1008_pdata = {
176         .supported_hwrev = true,
177         .ext_range = false,
178         .conv_rate = 0x08,
179         .offset = 0,
180         .hysteresis = 5,
181         .shutdown_ext_limit = 75,
182         .shutdown_local_limit = 75,
183         .throttling_ext_limit = 60,
184         .alarm_fn = NULL,
185 };
186
187 static struct i2c_board_info cardhu_i2c4_board_info[] = {
188         {
189                 I2C_BOARD_INFO("nct1008", 0x4C),
190                 .platform_data = &cardhu_nct1008_pdata,
191                 .irq = -1,
192         }
193 };
194
195 static int cardhu_nct1008_init(void)
196 {
197         int nct1008_port = -1;
198         struct board_info BoardInfo;
199         int ret;
200
201         tegra_get_board_info(&BoardInfo);
202         if ((BoardInfo.board_id == BOARD_E1198) ||
203                 (BoardInfo.board_id == BOARD_E1291)) {
204                 nct1008_port = TEGRA_GPIO_PCC2;
205         } else if ((BoardInfo.board_id == BOARD_E1186) ||
206                 (BoardInfo.board_id == BOARD_E1187)) {
207                 /* FIXME: seems to be conflicting with usb3 vbus on E1186 */
208                 /* nct1008_port = TEGRA_GPIO_PH7; */
209         }
210
211         if (nct1008_port >= 0) {
212                 /* FIXME: enable irq when throttling is supported */
213                 /* cardhu_i2c4_board_info[0].irq = */
214                 /* TEGRA_GPIO_TO_IRQ(nct1008_port); */
215
216                 ret = gpio_request(nct1008_port, "temp_alert");
217                 if (ret < 0)
218                         return ret;
219
220                 ret = gpio_direction_input(nct1008_port);
221                 if (ret < 0)
222                         gpio_free(nct1008_port);
223                 else
224                         tegra_gpio_enable(nct1008_port);
225
226         }
227         return ret;
228 }
229
230 #if defined(CONFIG_GPIO_PCA953X)
231 static struct pca953x_platform_data cardhu_pmu_tca6416_data = {
232         .gpio_base      = PMU_TCA6416_GPIO_BASE,
233 };
234
235 static const struct i2c_board_info cardhu_i2c4_board_info_tca6416[] = {
236         {
237                 I2C_BOARD_INFO("tca6416", 0x20),
238                 .platform_data = &cardhu_pmu_tca6416_data,
239         },
240 };
241
242 static struct pca953x_platform_data cardhu_cam_tca6416_data = {
243         .gpio_base      = CAM_TCA6416_GPIO_BASE,
244 };
245
246 static const struct i2c_board_info cardhu_i2c2_board_info_tca6416[] = {
247         {
248                 I2C_BOARD_INFO("tca6416", 0x20),
249                 .platform_data = &cardhu_cam_tca6416_data,
250         },
251 };
252
253 static int __init pmu_tca6416_init(void)
254 {
255         struct board_info board_info;
256         tegra_get_board_info(&board_info);
257         if ((board_info.board_id == BOARD_E1198) ||
258                         (board_info.board_id == BOARD_E1291))
259                 return 0;
260
261         pr_info("Registering pmu pca6416\n");
262         i2c_register_board_info(4, cardhu_i2c4_board_info_tca6416,
263                 ARRAY_SIZE(cardhu_i2c4_board_info_tca6416));
264         return 0;
265 }
266
267 static int __init cam_tca6416_init(void)
268 {
269         struct board_info board_info;
270         tegra_get_board_info(&board_info);
271         /* Boards E1198 and E1291 are of Cardhu personality
272          * and donot have TCA6416 exp for camera */
273         if ((board_info.board_id == BOARD_E1198) ||
274                 (board_info.board_id == BOARD_E1291))
275                 return 0;
276
277         pr_info("Registering cam pca6416\n");
278         i2c_register_board_info(2, cardhu_i2c2_board_info_tca6416,
279                 ARRAY_SIZE(cardhu_i2c2_board_info_tca6416));
280         return 0;
281 }
282 #else
283 static int __init pmu_tca6416_init(void)
284 {
285         return 0;
286 }
287
288 static int __init cam_tca6416_init(void)
289 {
290         return 0;
291 }
292 #endif
293
294 int __init cardhu_sensors_init(void)
295 {
296         int err;
297
298         cardhu_camera_init();
299         cam_tca6416_init();
300
301         i2c_register_board_info(2, cardhu_i2c3_board_info,
302                 ARRAY_SIZE(cardhu_i2c3_board_info));
303
304         /* CAM-A is on BUS0 to set CAM-B need to change
305          * PCA954x_I2C_BUS0 to PCA954x_I2C_BUS1 */
306         i2c_register_board_info(PCA954x_I2C_BUS0, cardhu_i2c6_board_info,
307                 ARRAY_SIZE(cardhu_i2c6_board_info));
308
309         pmu_tca6416_init();
310
311         i2c_register_board_info(4, cardhu_i2c4_board_info,
312                 ARRAY_SIZE(cardhu_i2c4_board_info));
313
314         err = cardhu_nct1008_init();
315         if (err)
316                 return err;
317
318         return 0;
319 }
320
321 #if defined(CONFIG_GPIO_PCA953X)
322 struct ov5650_gpios {
323         const char *name;
324         int gpio;
325         int enabled;
326 };
327
328 #define OV5650_GPIO(_name, _gpio, _enabled)             \
329         {                                               \
330                 .name = _name,                          \
331                 .gpio = _gpio,                          \
332                 .enabled = _enabled,                    \
333         }
334
335 static struct ov5650_gpios ov5650_gpio_keys[] = {
336         [0] = OV5650_GPIO("cam1_pwdn", CAM1_PWR_DN_GPIO, 0),
337         [1] = OV5650_GPIO("cam1_rst_lo", CAM1_RST_L_GPIO, 1),
338         [2] = OV5650_GPIO("cam1_af_pwdn_lo", CAM1_AF_PWR_DN_L_GPIO, 1),
339         [3] = OV5650_GPIO("cam1_ldo_shdn_lo", CAM1_LDO_SHUTDN_L_GPIO, 1),
340         [4] = OV5650_GPIO("cam2_pwdn", CAM2_PWR_DN_GPIO, 0),
341         [5] = OV5650_GPIO("cam2_rst_lo", CAM2_RST_L_GPIO, 1),
342         [6] = OV5650_GPIO("cam2_af_pwdn_lo", CAM2_AF_PWR_DN_L_GPIO, 1),
343         [7] = OV5650_GPIO("cam2_ldo_shdn_lo", CAM2_LDO_SHUTDN_L_GPIO, 1),
344         [8] = OV5650_GPIO("cam3_pwdn", CAM_FRONT_PWR_DN_GPIO, 0),
345         [9] = OV5650_GPIO("cam3_rst_lo", CAM_FRONT_RST_L_GPIO, 1),
346         [10] = OV5650_GPIO("cam3_af_pwdn_lo", CAM_FRONT_AF_PWR_DN_L_GPIO, 1),
347         [11] = OV5650_GPIO("cam3_ldo_shdn_lo", CAM_FRONT_LDO_SHUTDN_L_GPIO, 1),
348         [12] = OV5650_GPIO("cam_led_exp", CAM_FRONT_LED_EXP, 1),
349         [13] = OV5650_GPIO("cam_led_rear_exp", CAM_SNN_LED_REAR_EXP, 1),
350         [14] = OV5650_GPIO("cam_i2c_mux_rst", CAM_I2C_MUX_RST_EXP, 1),
351 };
352
353 int __init cardhu_ov5650_late_init(void)
354 {
355         int ret;
356         int i;
357
358         for (i = 0; i < ARRAY_SIZE(ov5650_gpio_keys); i++) {
359                 ret = gpio_request(ov5650_gpio_keys[i].gpio,
360                         ov5650_gpio_keys[i].name);
361                 if (ret < 0) {
362                         printk("%s: gpio_request failed for gpio #%d\n",
363                                 __func__, i);
364                         goto fail;
365                 }
366                 gpio_direction_output(ov5650_gpio_keys[i].gpio,
367                         ov5650_gpio_keys[i].enabled);
368                 gpio_export(ov5650_gpio_keys[i].gpio, false);
369         }
370
371         return 0;
372
373 fail:
374         while (i--)
375                 gpio_free(ov5650_gpio_keys[i].gpio);
376         return ret;
377 }
378
379 late_initcall(cardhu_ov5650_late_init);
380 #endif