arm: tegra: dalmore: add ALS sensor support
[linux-2.6.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(struct device *dev)
73 {
74         int i;
75         for (i = 0; i < ARRAY_SIZE(dalmore_cam_reg_name); i++) {
76                 if (!dalmore_cam_reg[i]) {
77                         dalmore_cam_reg[i] = regulator_get(dev,
78                                         dalmore_cam_reg_name[i]);
79                         if (WARN_ON(IS_ERR(dalmore_cam_reg[i]))) {
80                                 pr_err("%s: didn't get regulator #%d: %ld\n",
81                                 __func__, i, PTR_ERR(dalmore_cam_reg[i]));
82                                 goto reg_alloc_fail;
83                         }
84                 }
85                 regulator_enable(dalmore_cam_reg[i]);
86         }
87
88         gpio_direction_output(CAM_RSTN, 0);
89         mdelay(10);
90         gpio_direction_output(CAM_AF_PWDN, 1);
91         gpio_direction_output(CAM2_POWER_DWN_GPIO, 1);
92         gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
93         gpio_direction_output(CAM_RSTN, 1);
94
95         return 0;
96
97 reg_alloc_fail:
98
99         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
100                 if (dalmore_cam_reg[i]) {
101                         regulator_put(dalmore_cam_reg[i]);
102                         dalmore_cam_reg[i] = NULL;
103                 }
104         }
105
106         return -ENODEV;
107 }
108
109 static int dalmore_imx091_power_off(struct device *dev)
110 {
111         int i;
112         gpio_direction_output(CAM1_POWER_DWN_GPIO, 0);
113
114         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
115                 if (dalmore_cam_reg[i]) {
116                         regulator_disable(dalmore_cam_reg[i]);
117                         regulator_put(dalmore_cam_reg[i]);
118                         dalmore_cam_reg[i] = NULL;
119                 }
120         }
121
122         return 0;
123 }
124
125 static int dalmore_ov9772_power_on(struct device *dev)
126 {
127         int i;
128         for (i = 0; i < ARRAY_SIZE(dalmore_cam_reg_name); i++) {
129                 if (!dalmore_cam_reg[i]) {
130                         dalmore_cam_reg[i] = regulator_get(dev,
131                                         dalmore_cam_reg_name[i]);
132                         if (WARN_ON(IS_ERR(dalmore_cam_reg[i]))) {
133                                 pr_err("%s: didn't get regulator #%d: %ld\n",
134                                 __func__, i, PTR_ERR(dalmore_cam_reg[i]));
135                                 goto reg_alloc_fail;
136                         }
137                 }
138                 regulator_enable(dalmore_cam_reg[i]);
139         }
140
141         gpio_direction_output(CAM_RSTN, 0);
142         mdelay(10);
143         gpio_direction_output(CAM_AF_PWDN, 1);
144         gpio_direction_output(CAM2_POWER_DWN_GPIO, 1);
145         gpio_direction_output(CAM1_POWER_DWN_GPIO, 1);
146         gpio_direction_output(CAM_RSTN, 1);
147
148         return 0;
149
150 reg_alloc_fail:
151
152         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
153                 if (dalmore_cam_reg[i]) {
154                         regulator_put(dalmore_cam_reg[i]);
155                         dalmore_cam_reg[i] = NULL;
156                 }
157         }
158
159         return -ENODEV;
160 }
161
162 static int dalmore_ov9772_power_off(struct device *dev)
163 {
164         int i;
165         gpio_direction_output(CAM1_POWER_DWN_GPIO, 0);
166
167         for (i = ARRAY_SIZE(dalmore_cam_reg_name) - 1; i >= 0; i--) {
168                 if (dalmore_cam_reg[i]) {
169                         regulator_disable(dalmore_cam_reg[i]);
170                         regulator_put(dalmore_cam_reg[i]);
171                         dalmore_cam_reg[i] = NULL;
172                 }
173         }
174
175         return 0;
176 }
177
178 struct imx091_platform_data dalmore_imx091_data = {
179         .power_on = dalmore_imx091_power_on,
180         .power_off = dalmore_imx091_power_off,
181 };
182
183 struct ov9772_platform_data dalmore_ov9772_data = {
184         .power_on = dalmore_ov9772_power_on,
185         .power_off = dalmore_ov9772_power_off,
186 };
187
188 static struct i2c_board_info dalmore_i2c_board_info_e1625[] = {
189         {
190                 I2C_BOARD_INFO("imx091", 0x36),
191                 .platform_data = &dalmore_imx091_data,
192         },
193         {
194                 I2C_BOARD_INFO("ov9772", 0x10),
195                 .platform_data = &dalmore_ov9772_data,
196         },
197 };
198
199 struct dalmore_cam_gpio {
200         int gpio;
201         const char *label;
202         int value;
203 };
204
205 #define TEGRA_CAMERA_GPIO(_gpio, _label, _value)                \
206         {                                                       \
207                 .gpio = _gpio,                                  \
208                 .label = _label,                                \
209                 .value = _value,                                \
210         }
211
212 static struct dalmore_cam_gpio dalmore_cam_gpio_data[] = {
213         [0] = TEGRA_CAMERA_GPIO(CAM1_POWER_DWN_GPIO, "camera_power_en", 0),
214         [1] = TEGRA_CAMERA_GPIO(CAM2_POWER_DWN_GPIO, "camera2_power_en", 0),
215         [2] = TEGRA_CAMERA_GPIO(CAM_GPIO1, "camera_gpio1", 0),
216         [3] = TEGRA_CAMERA_GPIO(CAM_GPIO2, "camera_gpio2", 0),
217         [4] = TEGRA_CAMERA_GPIO(CAM_RSTN, "camera_rstn", 1),
218         [5] = TEGRA_CAMERA_GPIO(CAM_AF_PWDN, "camera_af_pwdn", 1),
219 };
220
221 static int dalmore_camera_init(void)
222 {
223
224         int ret;
225         int i;
226
227         pr_debug("%s: ++\n", __func__);
228
229         for (i = 0; i < ARRAY_SIZE(dalmore_cam_gpio_data); i++) {
230                 ret = gpio_request(dalmore_cam_gpio_data[i].gpio,
231                                 dalmore_cam_gpio_data[i].label);
232                 if (ret < 0) {
233                         pr_err("%s: gpio_request failed for gpio #%d\n",
234                                 __func__, i);
235                         goto fail_free_gpio;
236                 }
237                 gpio_direction_output(dalmore_cam_gpio_data[i].gpio,
238                                         dalmore_cam_gpio_data[i].value);
239                 gpio_export(dalmore_cam_gpio_data[i].gpio, false);
240         }
241
242         i2c_register_board_info(2, dalmore_i2c_board_info_e1625,
243                 ARRAY_SIZE(dalmore_i2c_board_info_e1625));
244
245 fail_free_gpio:
246
247         while (i--)
248                 gpio_free(dalmore_cam_gpio_data[i].gpio);
249         return ret;
250
251 }
252
253
254 /* MPU board file definition    */
255 static struct mpu_platform_data mpu9150_gyro_data = {
256         .int_config     = 0x10,
257         .level_shifter  = 0,
258         .orientation    = MPU_GYRO_ORIENTATION, /* Located in board_[platformname].h    */
259         .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
260         .sec_slave_id   = COMPASS_ID_AK8975,
261         .secondary_i2c_addr     = MPU_COMPASS_ADDR,
262         .secondary_read_reg     = 0x06,
263         .secondary_orientation  = MPU_COMPASS_ORIENTATION,
264         .key            = {0x4E, 0xCC, 0x7E, 0xEB, 0xF6, 0x1E, 0x35, 0x22,
265                            0x00, 0x34, 0x0D, 0x65, 0x32, 0xE9, 0x94, 0x89},
266 };
267
268 #define TEGRA_CAMERA_GPIO(_gpio, _label, _value)                \
269         {                                                       \
270                 .gpio = _gpio,                                  \
271                 .label = _label,                                \
272                 .value = _value,                                \
273         }
274
275 static struct i2c_board_info dalmore_i2c_board_info_cm3218[] = {
276         {
277                 I2C_BOARD_INFO("cm3218", 0x48),
278         },
279 };
280
281 static struct i2c_board_info __initdata inv_mpu9150_i2c2_board_info[] = {
282         {
283                 I2C_BOARD_INFO(MPU_GYRO_NAME, MPU_GYRO_ADDR),
284                 .platform_data = &mpu9150_gyro_data,
285         },
286 };
287
288 static void mpuirq_init(void)
289 {
290         int ret = 0;
291         unsigned gyro_irq_gpio = MPU_GYRO_IRQ_GPIO;
292         unsigned gyro_bus_num = MPU_GYRO_BUS_NUM;
293         char *gyro_name = MPU_GYRO_NAME;
294
295         pr_info("*** MPU START *** mpuirq_init...\n");
296
297         ret = gpio_request(gyro_irq_gpio, gyro_name);
298
299         if (ret < 0) {
300                 pr_err("%s: gpio_request failed %d\n", __func__, ret);
301                 return;
302         }
303
304         ret = gpio_direction_input(gyro_irq_gpio);
305         if (ret < 0) {
306                 pr_err("%s: gpio_direction_input failed %d\n", __func__, ret);
307                 gpio_free(gyro_irq_gpio);
308                 return;
309         }
310         pr_info("*** MPU END *** mpuirq_init...\n");
311
312         inv_mpu9150_i2c2_board_info[0].irq = gpio_to_irq(MPU_GYRO_IRQ_GPIO);
313         i2c_register_board_info(gyro_bus_num, inv_mpu9150_i2c2_board_info,
314                 ARRAY_SIZE(inv_mpu9150_i2c2_board_info));
315 }
316
317 int __init dalmore_sensors_init(void)
318 {
319         tegra_get_board_info(&board_info);
320
321         dalmore_camera_init();
322         mpuirq_init();
323
324         i2c_register_board_info(0, dalmore_i2c_board_info_cm3218,
325                 ARRAY_SIZE(dalmore_i2c_board_info_cm3218));
326
327         return 0;
328 }