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