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