arm: arch: dalmore: Update camera clock and regulator
[linux-3.10.git] / arch / arm / mach-tegra / board-dalmore-sensors.c
1 /*
2  * arch/arm/mach-tegra/board-dalmore-sensors.c
3  *
4  * Copyright (c) 2012 NVIDIA CORPORATION, All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of NVIDIA CORPORATION nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is licensed under the terms of the GNU General Public
22  * License version 2, as published by the Free Software Foundation, and
23  * may be copied, distributed, and modified under those terms.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  */
30
31 #include <linux/i2c.h>
32 #include <linux/delay.h>
33 #include <mach/gpio.h>
34 #include "gpio-names.h"
35 #include "board.h"
36 #include <mach/gpio.h>
37 #include <media/imx091.h>
38 #include <media/ov9772.h>
39 #include "board-dalmore.h"
40 #include "cpu-tegra.h"
41
42 #include <linux/i2c.h>
43 #include <linux/delay.h>
44 #include <linux/regulator/consumer.h>
45 #include <linux/i2c/pca954x.h>
46 #include <linux/i2c/pca953x.h>
47 #include <linux/nct1008.h>
48 #include <linux/gpio.h>
49
50 #include <mach/gpio-tegra.h>
51 #include <mach/fb.h>
52
53 #include <media/imx091.h>
54 #include <generated/mach-types.h>
55
56 #include <linux/mpu.h>
57 #include "board-dalmore.h"
58
59 static struct board_info board_info;
60
61 static char *dalmore_cam_reg_name[] = {
62         "vdd_sensor_2v85",      /* 2.85V */
63         "avddio_usb",           /* VDDIO USB CAM */
64         "dvdd_cam",             /* DVDD CAM */
65         "vddio_cam",            /* Tegra CAM_I2C, CAM_MCLK, VDD 1.8V */
66         "avdd_cam1",            /* Analog VDD 2.7V */
67         "avdd_cam2",            /* Analog VDD 2.7V */
68 };
69
70 static struct regulator *dalmore_cam_reg[ARRAY_SIZE(dalmore_cam_reg_name)];
71
72 static int dalmore_imx091_power_on(void)
73 {
74
75         int i;
76
77         for (i = 0; i < ARRAY_SIZE(dalmore_cam_reg_name); i++) {
78                 if (!dalmore_cam_reg[i]) {
79                         dalmore_cam_reg[i] = regulator_get(NULL,
80                                         dalmore_cam_reg_name[i]);
81                         if (WARN_ON(IS_ERR(dalmore_cam_reg[i]))) {
82                                 pr_err("%s: didn't get regulator #%d: %ld\n",
83                                 __func__, i, PTR_ERR(dalmore_cam_reg[i]));
84                                 goto reg_alloc_fail;
85                         }
86                 }
87                 regulator_enable(dalmore_cam_reg[i]);
88         }
89
90         gpio_direction_output(CAM_RSTN, 0);
91         mdelay(10);
92         gpio_direction_output(CAM_AF_PWDN, 1);
93         gpio_direction_output(CAM2_POWER_DWN_GPIO, 1);
94         gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
95         gpio_direction_output(CAM_RSTN, 1);
96
97         return 0;
98
99 reg_alloc_fail:
100
101         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
102                 if (dalmore_cam_reg[i]) {
103                         regulator_put(dalmore_cam_reg[i]);
104                         dalmore_cam_reg[i] = NULL;
105                 }
106         }
107
108         return -ENODEV;
109 }
110
111 static int dalmore_imx091_power_off(void)
112 {
113         int i;
114         gpio_direction_output(CAM1_POWER_DWN_GPIO, 0);
115
116         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
117                 if (dalmore_cam_reg[i]) {
118                         regulator_disable(dalmore_cam_reg[i]);
119                         regulator_put(dalmore_cam_reg[i]);
120                         dalmore_cam_reg[i] = NULL;
121                 }
122         }
123
124         return 0;
125 }
126
127 static int dalmore_ov9772_power_on(void)
128 {
129
130         int i;
131
132         for (i = 0; i < ARRAY_SIZE(dalmore_cam_reg_name); i++) {
133                 if (!dalmore_cam_reg[i]) {
134                         dalmore_cam_reg[i] = regulator_get(NULL,
135                                         dalmore_cam_reg_name[i]);
136                         if (WARN_ON(IS_ERR(dalmore_cam_reg[i]))) {
137                                 pr_err("%s: didn't get regulator #%d: %ld\n",
138                                 __func__, i, PTR_ERR(dalmore_cam_reg[i]));
139                                 goto reg_alloc_fail;
140                         }
141                 }
142                 regulator_enable(dalmore_cam_reg[i]);
143         }
144
145         gpio_direction_output(CAM_RSTN, 0);
146         mdelay(10);
147         gpio_direction_output(CAM_AF_PWDN, 1);
148         gpio_direction_output(CAM2_POWER_DWN_GPIO, 1);
149         gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
150         gpio_direction_output(CAM_RSTN, 1);
151
152         return 0;
153
154 reg_alloc_fail:
155
156         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
157                 if (dalmore_cam_reg[i]) {
158                         regulator_put(dalmore_cam_reg[i]);
159                         dalmore_cam_reg[i] = NULL;
160                 }
161         }
162
163         return -ENODEV;
164 }
165
166 static int dalmore_ov9772_power_off(void)
167 {
168         int i;
169         gpio_direction_output(CAM1_POWER_DWN_GPIO, 0);
170
171         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
172                 if (dalmore_cam_reg[i]) {
173                         regulator_disable(dalmore_cam_reg[i]);
174                         regulator_put(dalmore_cam_reg[i]);
175                         dalmore_cam_reg[i] = NULL;
176                 }
177         }
178
179         return 0;
180 }
181
182 struct imx091_platform_data dalmore_imx091_data = {
183         .power_on = dalmore_imx091_power_on,
184         .power_off = dalmore_imx091_power_off,
185 };
186
187 struct ov9772_platform_data dalmore_ov9772_data = {
188         .power_on = dalmore_ov9772_power_on,
189         .power_off = dalmore_ov9772_power_off,
190 };
191
192 static struct i2c_board_info dalmore_i2c_board_info_e1625[] = {
193         {
194                 I2C_BOARD_INFO("imx091", 0x36),
195                 .platform_data = &dalmore_imx091_data,
196         },
197         {
198                 I2C_BOARD_INFO("ov9772", 0x10),
199                 .platform_data = &dalmore_ov9772_data,
200         },
201 };
202
203 struct dalmore_cam_gpio {
204         int gpio;
205         const char *label;
206         int value;
207 };
208
209 #define TEGRA_CAMERA_GPIO(_gpio, _label, _value)                \
210         {                                                       \
211                 .gpio = _gpio,                                  \
212                 .label = _label,                                \
213                 .value = _value,                                \
214         }
215
216 static struct dalmore_cam_gpio dalmore_cam_gpio_data[] = {
217         [0] = TEGRA_CAMERA_GPIO(CAM1_POWER_DWN_GPIO, "camera_power_en", 0),
218         [1] = TEGRA_CAMERA_GPIO(CAM2_POWER_DWN_GPIO, "camera2_power_en", 0),
219         [2] = TEGRA_CAMERA_GPIO(CAM_GPIO1, "camera_gpio1", 0),
220         [3] = TEGRA_CAMERA_GPIO(CAM_GPIO2, "camera_gpio2", 0),
221         [4] = TEGRA_CAMERA_GPIO(CAM_RSTN, "camera_rstn", 1),
222         [5] = TEGRA_CAMERA_GPIO(CAM_AF_PWDN, "camera_af_pwdn", 1),
223 };
224
225 static int dalmore_camera_init(void)
226 {
227
228         int ret;
229         int i;
230
231         pr_debug("%s: ++\n", __func__);
232
233         for (i = 0; i < ARRAY_SIZE(dalmore_cam_gpio_data); i++) {
234                 ret = gpio_request(dalmore_cam_gpio_data[i].gpio,
235                                 dalmore_cam_gpio_data[i].label);
236                 if (ret < 0) {
237                         pr_err("%s: gpio_request failed for gpio #%d\n",
238                                 __func__, i);
239                         goto fail_free_gpio;
240                 }
241                 gpio_direction_output(dalmore_cam_gpio_data[i].gpio,
242                                         dalmore_cam_gpio_data[i].value);
243                 gpio_export(dalmore_cam_gpio_data[i].gpio, false);
244         }
245
246         i2c_register_board_info(2, dalmore_i2c_board_info_e1625,
247                 ARRAY_SIZE(dalmore_i2c_board_info_e1625));
248
249 fail_free_gpio:
250
251         while (i--)
252                 gpio_free(dalmore_cam_gpio_data[i].gpio);
253         return ret;
254
255 }
256
257
258 /* MPU board file definition    */
259 static struct mpu_platform_data mpu9150_gyro_data = {
260         .int_config     = 0x10,
261         .level_shifter  = 0,
262         .orientation    = MPU_GYRO_ORIENTATION, /* Located in board_[platformname].h    */
263         .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
264         .sec_slave_id   = COMPASS_ID_AK8975,
265         .secondary_i2c_addr     = MPU_COMPASS_ADDR,
266         .secondary_read_reg     = 0x06,
267         .secondary_orientation  = MPU_COMPASS_ORIENTATION,
268         .key            = {0x4E, 0xCC, 0x7E, 0xEB, 0xF6, 0x1E, 0x35, 0x22,
269                            0x00, 0x34, 0x0D, 0x65, 0x32, 0xE9, 0x94, 0x89},
270 };
271
272 #define TEGRA_CAMERA_GPIO(_gpio, _label, _value)                \
273         {                                                       \
274                 .gpio = _gpio,                                  \
275                 .label = _label,                                \
276                 .value = _value,                                \
277         }
278
279 static struct i2c_board_info __initdata inv_mpu9150_i2c2_board_info[] = {
280         {
281                 I2C_BOARD_INFO(MPU_GYRO_NAME, MPU_GYRO_ADDR),
282                 .platform_data = &mpu9150_gyro_data,
283         },
284 };
285
286 static void mpuirq_init(void)
287 {
288         int ret = 0;
289         unsigned gyro_irq_gpio = MPU_GYRO_IRQ_GPIO;
290         unsigned gyro_bus_num = MPU_GYRO_BUS_NUM;
291         char *gyro_name = MPU_GYRO_NAME;
292
293         pr_info("*** MPU START *** mpuirq_init...\n");
294
295         ret = gpio_request(gyro_irq_gpio, gyro_name);
296
297         if (ret < 0) {
298                 pr_err("%s: gpio_request failed %d\n", __func__, ret);
299                 return;
300         }
301
302         ret = gpio_direction_input(gyro_irq_gpio);
303         if (ret < 0) {
304                 pr_err("%s: gpio_direction_input failed %d\n", __func__, ret);
305                 gpio_free(gyro_irq_gpio);
306                 return;
307         }
308         pr_info("*** MPU END *** mpuirq_init...\n");
309
310         inv_mpu9150_i2c2_board_info[0].irq = gpio_to_irq(MPU_GYRO_IRQ_GPIO);
311         i2c_register_board_info(gyro_bus_num, inv_mpu9150_i2c2_board_info,
312                 ARRAY_SIZE(inv_mpu9150_i2c2_board_info));
313 }
314
315 int __init dalmore_sensors_init(void)
316 {
317         tegra_get_board_info(&board_info);
318
319         dalmore_camera_init();
320         mpuirq_init();
321
322         return 0;
323 }