75d85721154abfa89ce332e81105b1645affd03d
[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 <linux/mpu.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/gpio.h>
36 #include <linux/therm_est.h>
37 #include <linux/nct1008.h>
38 #include <mach/edp.h>
39 #include <mach/gpio-tegra.h>
40 #include <mach/pinmux-t11.h>
41 #include <mach/pinmux.h>
42 #include <media/imx091.h>
43 #include <media/ov9772.h>
44 #include <media/as364x.h>
45 #include <media/ad5816.h>
46 #include <generated/mach-types.h>
47
48 #include "gpio-names.h"
49 #include "board.h"
50 #include "board-dalmore.h"
51 #include "cpu-tegra.h"
52 #include "devices.h"
53 #include "tegra-board-id.h"
54
55 static struct board_info board_info;
56
57 static struct balanced_throttle tj_throttle = {
58         .throt_tab_size = 10,
59         .throt_tab = {
60                 {      0, 1000 },
61                 { 640000, 1000 },
62                 { 640000, 1000 },
63                 { 640000, 1000 },
64                 { 640000, 1000 },
65                 { 640000, 1000 },
66                 { 760000, 1000 },
67                 { 760000, 1050 },
68                 {1000000, 1050 },
69                 {1000000, 1100 },
70         },
71 };
72
73 static struct nct1008_platform_data dalmore_nct1008_pdata = {
74         .supported_hwrev = true,
75         .ext_range = true,
76         .conv_rate = 0x08,
77         .offset = 80, /* 4 * 20C. Bug 844025 - 1C for device accuracies */
78         .shutdown_ext_limit = 90, /* C */
79         .shutdown_local_limit = 120, /* C */
80
81         /* Thermal Throttling */
82         .passive = {
83                 .create_cdev = (struct thermal_cooling_device *(*)(void *))
84                                 balanced_throttle_register,
85                 .cdev_data = &tj_throttle,
86                 .trip_temp = 80000,
87                 .tc1 = 0,
88                 .tc2 = 1,
89                 .passive_delay = 2000,
90         }
91 };
92
93 static struct i2c_board_info dalmore_i2c4_nct1008_board_info[] = {
94         {
95                 I2C_BOARD_INFO("nct1008", 0x4C),
96                 .platform_data = &dalmore_nct1008_pdata,
97                 .irq = -1,
98         }
99 };
100
101 #define VI_PINMUX(_pingroup, _mux, _pupd, _tri, _io, _lock, _ioreset) \
102         {                                                       \
103                 .pingroup       = TEGRA_PINGROUP_##_pingroup,   \
104                 .func           = TEGRA_MUX_##_mux,             \
105                 .pupd           = TEGRA_PUPD_##_pupd,           \
106                 .tristate       = TEGRA_TRI_##_tri,             \
107                 .io             = TEGRA_PIN_##_io,              \
108                 .lock           = TEGRA_PIN_LOCK_##_lock,       \
109                 .od             = TEGRA_PIN_OD_DEFAULT,         \
110                 .ioreset        = TEGRA_PIN_IO_RESET_##_ioreset \
111         }
112
113 static struct tegra_pingroup_config mclk_disable =
114         VI_PINMUX(CAM_MCLK, VI, NORMAL, NORMAL, OUTPUT, DEFAULT, DEFAULT);
115
116 static struct tegra_pingroup_config mclk_enable =
117         VI_PINMUX(CAM_MCLK, VI_ALT3, NORMAL, NORMAL, OUTPUT, DEFAULT, DEFAULT);
118
119 static struct tegra_pingroup_config pbb0_disable =
120         VI_PINMUX(GPIO_PBB0, VI, NORMAL, NORMAL, OUTPUT, DEFAULT, DEFAULT);
121
122 static struct tegra_pingroup_config pbb0_enable =
123         VI_PINMUX(GPIO_PBB0, VI_ALT3, NORMAL, NORMAL, OUTPUT, DEFAULT, DEFAULT);
124
125 /*
126  * As a workaround, dalmore_vcmvdd need to be allocated to activate the
127  * sensor devices. This is due to the focuser device(AD5816) will hook up
128  * the i2c bus if it is not powered up.
129 */
130 static struct regulator *dalmore_vcmvdd;
131
132 static int dalmore_get_vcmvdd(void)
133 {
134         if (!dalmore_vcmvdd) {
135                 dalmore_vcmvdd = regulator_get(NULL, "vdd_af_cam1");
136                 if (unlikely(WARN_ON(IS_ERR(dalmore_vcmvdd)))) {
137                         pr_err("%s: can't get regulator vcmvdd: %ld\n",
138                                 __func__, PTR_ERR(dalmore_vcmvdd));
139                         dalmore_vcmvdd = NULL;
140                         return -ENODEV;
141                 }
142         }
143         return 0;
144 }
145
146 static int dalmore_imx091_power_on(struct imx091_power_rail *pw)
147 {
148         int err;
149
150         if (unlikely(!pw || !pw->avdd || !pw->iovdd))
151                 return -EFAULT;
152
153         if (dalmore_get_vcmvdd())
154                 goto imx091_poweron_fail;
155
156         gpio_set_value(CAM1_POWER_DWN_GPIO, 0);
157         usleep_range(10, 20);
158
159         err = regulator_enable(pw->avdd);
160         if (err)
161                 goto imx091_avdd_fail;
162
163         err = regulator_enable(pw->iovdd);
164         if (err)
165                 goto imx091_iovdd_fail;
166
167         usleep_range(1, 2);
168         gpio_set_value(CAM1_POWER_DWN_GPIO, 1);
169
170         err = regulator_enable(dalmore_vcmvdd);
171         if (unlikely(err))
172                 goto imx091_vcmvdd_fail;
173
174         tegra_pinmux_config_table(&mclk_enable, 1);
175         usleep_range(300, 310);
176
177         return 1;
178
179 imx091_vcmvdd_fail:
180         regulator_disable(pw->iovdd);
181
182 imx091_iovdd_fail:
183         regulator_disable(pw->avdd);
184
185 imx091_avdd_fail:
186         gpio_set_value(CAM1_POWER_DWN_GPIO, 0);
187
188 imx091_poweron_fail:
189         pr_err("%s FAILED\n", __func__);
190         return -ENODEV;
191 }
192
193 static int dalmore_imx091_power_off(struct imx091_power_rail *pw)
194 {
195         if (unlikely(!pw || !dalmore_vcmvdd || !pw->avdd || !pw->iovdd))
196                 return -EFAULT;
197
198         usleep_range(1, 2);
199         tegra_pinmux_config_table(&mclk_disable, 1);
200         gpio_set_value(CAM1_POWER_DWN_GPIO, 0);
201         usleep_range(1, 2);
202
203         regulator_disable(dalmore_vcmvdd);
204         regulator_disable(pw->iovdd);
205         regulator_disable(pw->avdd);
206
207         return 1;
208 }
209
210 struct imx091_platform_data dalmore_imx091_data = {
211         .power_on = dalmore_imx091_power_on,
212         .power_off = dalmore_imx091_power_off,
213 };
214
215 static int dalmore_ov9772_power_on(struct ov9772_power_rail *pw)
216 {
217         int err;
218
219         if (unlikely(!pw || !pw->avdd || !pw->dovdd))
220                 return -EFAULT;
221
222         if (dalmore_get_vcmvdd())
223                 goto ov9772_get_vcmvdd_fail;
224
225         gpio_set_value(CAM2_POWER_DWN_GPIO, 0);
226         gpio_set_value(CAM_RSTN, 0);
227
228         err = regulator_enable(pw->avdd);
229         if (unlikely(err))
230                 goto ov9772_avdd_fail;
231
232         err = regulator_enable(pw->dovdd);
233         if (unlikely(err))
234                 goto ov9772_dovdd_fail;
235
236         gpio_set_value(CAM_RSTN, 1);
237         gpio_set_value(CAM2_POWER_DWN_GPIO, 1);
238
239         err = regulator_enable(dalmore_vcmvdd);
240         if (unlikely(err))
241                 goto ov9772_vcmvdd_fail;
242
243         tegra_pinmux_config_table(&pbb0_enable, 1);
244         usleep_range(340, 380);
245
246         /* return 1 to skip the in-driver power_on sequence */
247         return 1;
248
249 ov9772_vcmvdd_fail:
250         regulator_disable(pw->dovdd);
251
252 ov9772_dovdd_fail:
253         regulator_disable(pw->avdd);
254
255 ov9772_avdd_fail:
256         gpio_set_value(CAM_RSTN, 0);
257         gpio_set_value(CAM2_POWER_DWN_GPIO, 0);
258
259 ov9772_get_vcmvdd_fail:
260         pr_err("%s FAILED\n", __func__);
261         return -ENODEV;
262 }
263
264 static int dalmore_ov9772_power_off(struct ov9772_power_rail *pw)
265 {
266         if (unlikely(!pw || !dalmore_vcmvdd || !pw->avdd || !pw->dovdd))
267                 return -EFAULT;
268
269         usleep_range(21, 25);
270         tegra_pinmux_config_table(&pbb0_disable, 1);
271
272         gpio_set_value(CAM2_POWER_DWN_GPIO, 0);
273         gpio_set_value(CAM_RSTN, 0);
274
275         regulator_disable(dalmore_vcmvdd);
276         regulator_disable(pw->dovdd);
277         regulator_disable(pw->avdd);
278
279         /* return 1 to skip the in-driver power_off sequence */
280         return 1;
281 }
282
283 static struct nvc_gpio_pdata ov9772_gpio_pdata[] = {
284         { OV9772_GPIO_TYPE_SHTDN, CAM2_POWER_DWN_GPIO, true, 0, },
285         { OV9772_GPIO_TYPE_PWRDN, CAM_RSTN, true, 0, },
286 };
287
288 static struct ov9772_platform_data dalmore_ov9772_pdata = {
289         .num            = 1,
290         .dev_name       = "camera",
291         .gpio_count     = ARRAY_SIZE(ov9772_gpio_pdata),
292         .gpio           = ov9772_gpio_pdata,
293         .power_on       = dalmore_ov9772_power_on,
294         .power_off      = dalmore_ov9772_power_off,
295 };
296
297 static int dalmore_as3648_power_on(struct as364x_power_rail *pw)
298 {
299         int err = dalmore_get_vcmvdd();
300
301         if (err)
302                 return err;
303
304         return regulator_enable(dalmore_vcmvdd);
305 }
306
307 static int dalmore_as3648_power_off(struct as364x_power_rail *pw)
308 {
309         if (!dalmore_vcmvdd)
310                 return -ENODEV;
311
312         return regulator_disable(dalmore_vcmvdd);
313 }
314
315 static struct as364x_platform_data dalmore_as3648_pdata = {
316         .config         = {
317                 .max_total_current_mA = 1000,
318                 .max_peak_current_mA = 600,
319                 .strobe_type = 1,
320                 },
321         .pinstate       = {
322                 .mask   = 1 << (CAM_FLASH_STROBE - TEGRA_GPIO_PBB0),
323                 .values = 1 << (CAM_FLASH_STROBE - TEGRA_GPIO_PBB0)
324                 },
325         .dev_name       = "torch",
326         .type           = AS3648,
327         .gpio_strobe    = CAM_FLASH_STROBE,
328         .led_mask       = 3,
329
330         .power_on_callback = dalmore_as3648_power_on,
331         .power_off_callback = dalmore_as3648_power_off,
332 };
333
334 static struct ad5816_platform_data pluto_ad5816_pdata = {
335         .cfg            = 0,
336         .num            = 0,
337         .sync           = 0,
338         .dev_name       = "focuser",
339 };
340
341 static struct i2c_board_info dalmore_i2c_board_info_e1625[] = {
342         {
343                 I2C_BOARD_INFO("imx091", 0x36),
344                 .platform_data = &dalmore_imx091_data,
345         },
346         {
347                 I2C_BOARD_INFO("ov9772", 0x10),
348                 .platform_data = &dalmore_ov9772_pdata,
349         },
350         {
351                 I2C_BOARD_INFO("as3648", 0x30),
352                 .platform_data = &dalmore_as3648_pdata,
353         },
354         {
355                 I2C_BOARD_INFO("ad5816", 0x0E),
356                 .platform_data = &pluto_ad5816_pdata,
357         },
358 };
359
360 static int dalmore_camera_init(void)
361 {
362         tegra_pinmux_config_table(&mclk_disable, 1);
363         tegra_pinmux_config_table(&pbb0_disable, 1);
364
365         i2c_register_board_info(2, dalmore_i2c_board_info_e1625,
366                 ARRAY_SIZE(dalmore_i2c_board_info_e1625));
367         return 0;
368 }
369
370 /* MPU board file definition    */
371 static struct mpu_platform_data mpu9150_gyro_data = {
372         .int_config     = 0x10,
373         .level_shifter  = 0,
374         /* Located in board_[platformname].h */
375         .orientation    = MPU_GYRO_ORIENTATION,
376         .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
377         .sec_slave_id   = COMPASS_ID_AK8975,
378         .secondary_i2c_addr     = MPU_COMPASS_ADDR,
379         .secondary_read_reg     = 0x06,
380         .secondary_orientation  = MPU_COMPASS_ORIENTATION,
381         .key            = {0x4E, 0xCC, 0x7E, 0xEB, 0xF6, 0x1E, 0x35, 0x22,
382                            0x00, 0x34, 0x0D, 0x65, 0x32, 0xE9, 0x94, 0x89},
383 };
384
385 #define TEGRA_CAMERA_GPIO(_gpio, _label, _value)                \
386         {                                                       \
387                 .gpio = _gpio,                                  \
388                 .label = _label,                                \
389                 .value = _value,                                \
390         }
391
392 static struct i2c_board_info dalmore_i2c_board_info_cm3218[] = {
393         {
394                 I2C_BOARD_INFO("cm3218", 0x48),
395         },
396 };
397
398 static struct i2c_board_info __initdata inv_mpu9150_i2c2_board_info[] = {
399         {
400                 I2C_BOARD_INFO(MPU_GYRO_NAME, MPU_GYRO_ADDR),
401                 .platform_data = &mpu9150_gyro_data,
402         },
403 };
404
405 static void mpuirq_init(void)
406 {
407         int ret = 0;
408         unsigned gyro_irq_gpio = MPU_GYRO_IRQ_GPIO;
409         unsigned gyro_bus_num = MPU_GYRO_BUS_NUM;
410         char *gyro_name = MPU_GYRO_NAME;
411
412         pr_info("*** MPU START *** mpuirq_init...\n");
413
414         ret = gpio_request(gyro_irq_gpio, gyro_name);
415
416         if (ret < 0) {
417                 pr_err("%s: gpio_request failed %d\n", __func__, ret);
418                 return;
419         }
420
421         ret = gpio_direction_input(gyro_irq_gpio);
422         if (ret < 0) {
423                 pr_err("%s: gpio_direction_input failed %d\n", __func__, ret);
424                 gpio_free(gyro_irq_gpio);
425                 return;
426         }
427         pr_info("*** MPU END *** mpuirq_init...\n");
428
429         inv_mpu9150_i2c2_board_info[0].irq = gpio_to_irq(MPU_GYRO_IRQ_GPIO);
430         i2c_register_board_info(gyro_bus_num, inv_mpu9150_i2c2_board_info,
431                 ARRAY_SIZE(inv_mpu9150_i2c2_board_info));
432 }
433
434 static int dalmore_nct1008_init(void)
435 {
436         int nct1008_port = -1;
437         int ret = 0;
438
439 #if defined(CONFIG_ARCH_TEGRA_11x_SOC)
440         if ((board_info.board_id == BOARD_E1611) ||
441             (board_info.board_id == BOARD_E1612) ||
442             (board_info.board_id == BOARD_E1641) ||
443             (board_info.board_id == BOARD_E1613) ||
444             (board_info.board_id == BOARD_P2454))
445         {
446                 /* per email from Matt 9/10/2012 */
447                 nct1008_port = TEGRA_GPIO_PX6;
448         } else {
449                 nct1008_port = TEGRA_GPIO_PX6;
450                 pr_err("Warning: nct alert_port assumed TEGRA_GPIO_PX6"
451                        " for unknown dalmore board id E%d\n",
452                        board_info.board_id);
453         }
454 #else
455         /* dalmore + AP30 interposer has SPI2_CS0 gpio */
456         nct1008_port = TEGRA_GPIO_PX3;
457 #endif
458
459         if (nct1008_port >= 0) {
460 #ifdef CONFIG_TEGRA_EDP_LIMITS
461                 const struct tegra_edp_limits *cpu_edp_limits;
462                 int cpu_edp_limits_size;
463                 int i;
464
465                 /* edp capping */
466                 tegra_get_cpu_edp_limits(&cpu_edp_limits, &cpu_edp_limits_size);
467
468                 if (cpu_edp_limits_size > MAX_THROT_TABLE_SIZE)
469                         BUG();
470
471                 for (i = 0; i < cpu_edp_limits_size-1; i++) {
472                         dalmore_nct1008_pdata.active[i].create_cdev =
473                                 (struct thermal_cooling_device *(*)(void *))
474                                         edp_cooling_device_create;
475                         dalmore_nct1008_pdata.active[i].cdev_data = (void *)i;
476                         dalmore_nct1008_pdata.active[i].trip_temp =
477                                 cpu_edp_limits[i].temperature * 1000;
478                         dalmore_nct1008_pdata.active[i].hysteresis = 1000;
479                 }
480                 dalmore_nct1008_pdata.active[i].create_cdev = NULL;
481 #endif
482
483                 dalmore_i2c4_nct1008_board_info[0].irq = gpio_to_irq(nct1008_port);
484                 pr_info("%s: dalmore nct1008 irq %d", __func__, dalmore_i2c4_nct1008_board_info[0].irq);
485
486                 ret = gpio_request(nct1008_port, "temp_alert");
487                 if (ret < 0)
488                         return ret;
489
490                 ret = gpio_direction_input(nct1008_port);
491                 if (ret < 0) {
492                         pr_info("%s: calling gpio_free(nct1008_port)", __func__);
493                         gpio_free(nct1008_port);
494                 }
495         }
496
497         /* dalmore has thermal sensor on GEN1-I2C i.e. instance 0 */
498         i2c_register_board_info(0, dalmore_i2c4_nct1008_board_info,
499                 ARRAY_SIZE(dalmore_i2c4_nct1008_board_info));
500
501         return ret;
502 }
503
504 static struct i2c_board_info __initdata bq20z45_pdata[] = {
505         {
506                 I2C_BOARD_INFO("sbs-battery", 0x0B),
507         },
508 };
509
510 #ifdef CONFIG_TEGRA_SKIN_THROTTLE
511 static int tegra_skin_match(struct thermal_zone_device *thz, void *data)
512 {
513         return strcmp((char *)data, thz->type) == 0;
514 }
515
516 static int tegra_skin_get_temp(void *data, long *temp)
517 {
518         struct thermal_zone_device *thz;
519
520         thz = thermal_zone_device_find(data, tegra_skin_match);
521
522         if (!thz || thz->ops->get_temp(thz, temp))
523                 *temp = 25000;
524
525         return 0;
526 }
527
528 static struct therm_est_data skin_data = {
529         .toffset = 9793,
530         .polling_period = 1100,
531         .ndevs = 2,
532         .devs = {
533                         {
534                                 .dev_data = "nct_ext",
535                                 .get_temp = tegra_skin_get_temp,
536                                 .coeffs = {
537                                         2, 1, 1, 1,
538                                         1, 1, 1, 1,
539                                         1, 1, 1, 0,
540                                         1, 1, 0, 0,
541                                         0, 0, -1, -7
542                                 },
543                         },
544                         {
545                                 .dev_data = "nct_int",
546                                 .get_temp = tegra_skin_get_temp,
547                                 .coeffs = {
548                                         -11, -7, -5, -3,
549                                         -3, -2, -1, 0,
550                                         0, 0, 1, 1,
551                                         1, 2, 2, 3,
552                                         4, 6, 11, 18
553                                 },
554                         },
555         },
556         .trip_temp = 43000,
557         .tc1 = 1,
558         .tc2 = 15,
559         .passive_delay = 15000,
560 };
561
562 static struct balanced_throttle skin_throttle = {
563         .throt_tab_size = 6,
564         .throt_tab = {
565                 { 640000, 1200 },
566                 { 640000, 1200 },
567                 { 760000, 1200 },
568                 { 760000, 1200 },
569                 {1000000, 1200 },
570                 {1000000, 1200 },
571         },
572 };
573
574 static int __init dalmore_skin_init(void)
575 {
576         struct thermal_cooling_device *skin_cdev;
577
578         skin_cdev = balanced_throttle_register(&skin_throttle);
579
580         skin_data.cdev = skin_cdev;
581         tegra_skin_therm_est_device.dev.platform_data = &skin_data;
582         platform_device_register(&tegra_skin_therm_est_device);
583
584         return 0;
585 }
586 late_initcall(dalmore_skin_init);
587 #endif
588
589 int __init dalmore_sensors_init(void)
590 {
591         int err;
592
593         tegra_get_board_info(&board_info);
594
595         err = dalmore_nct1008_init();
596         if (err)
597                 return err;
598
599         dalmore_camera_init();
600         mpuirq_init();
601
602         i2c_register_board_info(0, dalmore_i2c_board_info_cm3218,
603                 ARRAY_SIZE(dalmore_i2c_board_info_cm3218));
604
605         i2c_register_board_info(0, bq20z45_pdata,
606                 ARRAY_SIZE(bq20z45_pdata));
607
608         return 0;
609 }