2 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/clk.h>
21 #include <linux/miscdevice.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/gpio.h>
26 #include <linux/module.h>
27 #include <linux/regmap.h>
28 #include <linux/gpio.h>
30 #include <linux/of_device.h>
31 #include <linux/of_gpio.h>
32 #include <linux/sysedp.h>
33 #include <media/ov5693.h>
34 #include <media/nvc.h>
35 #include "regmap_util.h"
37 #define OV5693_ID 0x5693
38 #define OV5693_SENSOR_TYPE NVC_IMAGER_TYPE_RAW
39 #define OV5693_RES_CHG_WAIT_TIME_MS 100
40 #define OV5693_SIZEOF_I2C_BUF 16
41 #define OV5693_TABLE_WAIT_MS 0
42 #define OV5693_TABLE_END 1
43 #define OV5693_TABLE_RESET_TIMEOUT 50
44 #define OV5693_LENS_MAX_APERTURE 0 /* _INT2FLOAT_DIVISOR */
45 #define OV5693_LENS_FNUMBER 0 /* _INT2FLOAT_DIVISOR */
46 #define OV5693_LENS_FOCAL_LENGTH 6120 /* _INT2FLOAT_DIVISOR */
47 #define OV5693_LENS_VIEW_ANGLE_H 60000 /* _INT2FLOAT_DIVISOR */
48 #define OV5693_LENS_VIEW_ANGLE_V 60000 /* _INT2FLOAT_DIVISOR */
49 #define OV5693_OTP_BUF_SIZE 16
50 #define OV5693_FUSE_ID_SIZE 8
52 static struct nvc_gpio_init ov5693_gpio[] = {
53 { OV5693_GPIO_TYPE_PWRDN, GPIOF_OUT_INIT_LOW, "pwrdn", false, true, },
58 struct i2c_client *i2c_client;
59 struct ov5693_platform_data *pdata;
61 struct miscdevice miscdev;
64 struct nvc_gpio gpio[ARRAY_SIZE(ov5693_gpio)];
65 struct ov5693_power_rail regulators;
70 unsigned test_pattern;
71 struct nvc_imager_static_nvc sdata;
73 struct nvc_fuseid fuseid;
74 struct regmap *regmap;
75 struct regulator *ext_vcm_vdd;
76 struct ov5693_cal_data cal;
77 struct sysedp_consumer *sysedpc;
79 struct ov5693_eeprom_data eeprom[OV5693_EEPROM_NUM_BLOCKS];
80 u8 eeprom_buf[OV5693_EEPROM_SIZE];
83 struct ov5693_mode_data {
84 struct nvc_imager_mode sensor_mode;
85 struct nvc_imager_dynamic_nvc sensor_dnvc;
86 struct reg_8 *p_mode_i2c;
89 static struct ov5693_platform_data ov5693_dflt_pdata = {
96 * NOTE: static vs dynamic
97 * If a member in the nvc_imager_static_nvc structure is not actually
98 * static data, then leave blank and add the parameter to the parameter
99 * read function that dynamically reads the data. The NVC user driver
100 * will call the parameter read for the data if the member data is 0.
101 * If the dynamic data becomes static during probe (a one time read
102 * such as device ID) then add the dynamic read to the _sdata_init
105 static struct nvc_imager_static_nvc ov5693_dflt_sdata = {
106 .api_version = NVC_IMAGER_API_STATIC_VER,
107 .sensor_type = OV5693_SENSOR_TYPE,
108 .bits_per_pixel = 10,
109 .sensor_id = OV5693_ID,
110 .sensor_id_minor = 0,
111 .focal_len = OV5693_LENS_FOCAL_LENGTH,
112 .max_aperture = OV5693_LENS_MAX_APERTURE,
113 .fnumber = OV5693_LENS_FNUMBER,
114 .view_angle_h = OV5693_LENS_VIEW_ANGLE_H,
115 .view_angle_v = OV5693_LENS_VIEW_ANGLE_V,
116 .res_chg_wait_time = OV5693_RES_CHG_WAIT_TIME_MS,
119 static const struct reg_8 ov5693_2592x1944_i2c[] = {
120 {0x0100, 0x00},/* Including sw reset */
340 {0x4800, 0x20}, /* dis-continuous */
381 {OV5693_TABLE_END, 0x0000}
384 static const struct reg_8 ov5693_1296x972_i2c[] = {
385 {0x0100, 0x00},/* Including sw reset */
587 {0x4800, 0x20}, /* dis-continuous */
628 {OV5693_TABLE_END, 0x0000}
631 static const struct reg_8 ov5693_1920x1080_i2c[] = {
632 {0x0100, 0x00},/*, 0xIncluding, 0xsw, 0xreset, 0x*/
834 {0x4800, 0x20}, /* dis-continuous */
875 {OV5693_TABLE_END, 0x0000}
879 static const struct reg_8 ov5693_1280x720_120fps_i2c[] = {
880 {0x0100, 0x00},/* Including sw reset */
1082 {0x4800, 0x20}, /* dis-continuous */
1123 {OV5693_TABLE_END, 0x0000}
1126 static const struct reg_8 ov5693_2592x1944_HDR_24fps_i2c[] = {
1127 {0x0100, 0x00},/* Including sw reset */
1330 {0x4800, 0x20}, /* dis-continuous */
1371 {OV5693_TABLE_END, 0x0000}
1374 static const struct reg_8 ov5693_1920x1080_HDR_30fps_i2c[] = {
1375 {0x0100, 0x00},/* Including sw reset */
1578 {0x4800, 0x20}, /* dis-continuous */
1619 {OV5693_TABLE_END, 0x0000}
1622 static const struct reg_8 ov5693_1296x972_HDR_30fps_i2c[] = {
1623 {0x0100, 0x00},/* Including sw reset */
1826 {0x4800, 0x20}, /* dis-continuous */
1867 {OV5693_TABLE_END, 0x0000}
1870 static const struct reg_8 ov5693_1280x720_HDR_60fps_i2c[] = {
1871 {0x0100, 0x00},/* Including sw reset */
2074 {0x4800, 0x20}, /* dis-continuous */
2115 {OV5693_TABLE_END, 0x0000}
2119 OV5693_MODE_2592x1944 = 0,
2120 OV5693_MODE_1920x1080,
2121 OV5693_MODE_1296x972,
2122 OV5693_MODE_1280x720_120FPS,
2123 OV5693_MODE_2592x1944_HDR,
2124 OV5693_MODE_1920x1080_HDR,
2125 OV5693_MODE_1296x972_HDR,
2126 OV5693_MODE_1280x720_HDR_60FPS,
2129 static const struct reg_8 *mode_table[] = {
2130 [OV5693_MODE_2592x1944] = ov5693_2592x1944_i2c,
2131 [OV5693_MODE_1920x1080] = ov5693_1920x1080_i2c,
2132 [OV5693_MODE_1296x972] = ov5693_1296x972_i2c,
2133 [OV5693_MODE_1280x720_120FPS] = ov5693_1280x720_120fps_i2c,
2134 [OV5693_MODE_2592x1944_HDR] = ov5693_2592x1944_HDR_24fps_i2c,
2135 [OV5693_MODE_1920x1080_HDR] = ov5693_1920x1080_HDR_30fps_i2c,
2136 [OV5693_MODE_1296x972_HDR] = ov5693_1296x972_HDR_30fps_i2c,
2137 [OV5693_MODE_1280x720_HDR_60FPS] = ov5693_1280x720_HDR_60fps_i2c,
2140 static inline int ov5693_frame_length_reg(struct reg_8 *regs,
2143 regs->addr = 0x380E;
2144 regs->val = (frame_length >> 8) & 0xff;
2145 (regs + 1)->addr = 0x380F;
2146 (regs + 1)->val = (frame_length) & 0xff;
2151 static inline int ov5693_coarse_time_reg(struct reg_8 *regs,
2152 u32 coarse_time, u32 coarse_time_short)
2156 regs->addr = 0x3500;
2157 regs->val = (coarse_time >> 12) & 0xff;
2158 (regs + 1)->addr = 0x3501;
2159 (regs + 1)->val = (coarse_time >> 4) & 0xff;
2160 (regs + 2)->addr = 0x3502;
2161 (regs + 2)->val = (coarse_time & 0xf) << 4;
2165 if (coarse_time_short != OV5693_INVALID_COARSE_TIME) {
2166 (regs + 3)->addr = 0x3506;
2167 (regs + 3)->val = (coarse_time_short >> 12) & 0xff;
2168 (regs + 4)->addr = 0x3507;
2169 (regs + 4)->val = (coarse_time_short >> 4) & 0xff;
2170 (regs + 5)->addr = 0x3508;
2171 (regs + 5)->val = (coarse_time_short & 0xf) << 4;
2179 #define OV5693_ENTER_GROUP_HOLD(group_hold) \
2182 reg_list[offset].addr = 0x3208; \
2183 reg_list[offset].val = 0x01;\
2188 #define OV5693_LEAVE_GROUP_HOLD(group_hold) \
2191 reg_list[offset].addr = 0x3208; \
2192 reg_list[offset].val = 0x11;\
2194 reg_list[offset].addr = 0x3208; \
2195 reg_list[offset].val = 0x61;\
2200 static int ov5693_set_frame_length(struct ov5693_info *info,
2201 u32 frame_length, bool group_hold)
2203 struct reg_8 reg_list[9];
2207 OV5693_ENTER_GROUP_HOLD(group_hold);
2208 offset += ov5693_frame_length_reg(reg_list + offset, frame_length);
2209 OV5693_LEAVE_GROUP_HOLD(group_hold);
2211 reg_list[offset].addr = OV5693_TABLE_END;
2214 err = regmap_util_write_table_8(info->regmap,
2216 OV5693_TABLE_WAIT_MS,
2222 static int ov5693_set_coarse_time(struct ov5693_info *info,
2223 u32 coarse_time, u32 coarse_time_short,
2226 struct reg_8 reg_list[16];
2230 OV5693_ENTER_GROUP_HOLD(group_hold);
2231 offset += ov5693_coarse_time_reg(reg_list + offset,
2234 OV5693_LEAVE_GROUP_HOLD(group_hold);
2236 reg_list[offset].addr = OV5693_TABLE_END;
2239 err = regmap_util_write_table_8(info->regmap,
2241 OV5693_TABLE_WAIT_MS,
2247 static inline int ov5693_gain_reg(struct reg_8 *regs, u32 gain)
2249 (regs)->addr = 0x350A;
2250 (regs)->val = gain >> 8;
2252 (regs + 1)->addr = 0x350B;
2253 (regs + 1)->val = gain & 0x00FF;
2258 static int ov5693_bin_wr(struct ov5693_info *info, u8 enable)
2262 if (enable == info->bin_en)
2266 info->bin_en = enable;
2267 dev_dbg(&info->i2c_client->dev, "%s bin_en=%x err=%d\n",
2268 __func__, info->bin_en, err);
2272 static int ov5693_exposure_wr(struct ov5693_info *info,
2273 struct ov5693_mode *mode)
2275 struct reg_8 reg_list[16];
2278 bool group_hold = true; /* To use GROUP_HOLD macros */
2280 OV5693_ENTER_GROUP_HOLD(group_hold);
2281 offset += ov5693_coarse_time_reg(reg_list + offset,
2283 mode->coarse_time_short);
2284 offset += ov5693_gain_reg(reg_list + offset, mode->gain);
2285 OV5693_LEAVE_GROUP_HOLD(group_hold);
2287 reg_list[offset].addr = OV5693_TABLE_END;
2288 err = regmap_util_write_table_8(info->regmap,
2290 OV5693_TABLE_WAIT_MS,
2297 static int ov5693_set_gain(struct ov5693_info *info, u32 gain, bool group_hold)
2299 struct reg_8 reg_list[9];
2303 OV5693_ENTER_GROUP_HOLD(group_hold);
2304 offset += ov5693_gain_reg(reg_list + offset, gain);
2305 OV5693_LEAVE_GROUP_HOLD(group_hold);
2307 reg_list[offset].addr = OV5693_TABLE_END;
2310 err = regmap_util_write_table_8(info->regmap,
2312 OV5693_TABLE_WAIT_MS,
2318 static int ov5693_awb_wr(struct ov5693_info *info)
2320 struct reg_8 reg_list[10];
2321 int rg, bg, rg_typical, bg_typical;
2322 int R_gain, G_gain, B_gain, G_gain_R, G_gain_B;
2326 if (info->cal.loaded == 0)
2329 /* update AWB calibration data to register lists */
2330 rg = info->cal.rg_ratio;
2331 bg = info->cal.bg_ratio;
2332 rg_typical = info->cal.rg_ratio_typical;
2333 bg_typical = info->cal.bg_ratio_typical;
2335 if ((rg == 0) || (bg == 0) || (rg_typical == 0) || (bg_typical == 0))
2338 if (bg < bg_typical) {
2339 if (rg < rg_typical) {
2341 B_gain = 0x400 * bg_typical / bg;
2342 R_gain = 0x400 * rg_typical / rg;
2345 G_gain = 0x400 * rg / rg_typical;
2346 B_gain = G_gain * bg_typical / bg;
2349 if (rg < rg_typical) {
2351 G_gain = 0x400 * bg / bg_typical;
2352 R_gain = G_gain * rg_typical / rg;
2354 G_gain_B = 0x400 * bg / bg_typical;
2355 G_gain_R = 0x400 * rg / rg_typical;
2356 if (G_gain_B > G_gain_R) {
2359 R_gain = G_gain * rg_typical / rg;
2363 B_gain = G_gain * bg_typical / bg;
2369 if (R_gain > 0x400) {
2370 reg_list[offset].addr = 0x3400;
2371 reg_list[offset].val = R_gain >> 8;
2373 reg_list[offset].addr = 0x3401;
2374 reg_list[offset].val = R_gain & 0x00ff;
2377 if (G_gain > 0x400) {
2378 reg_list[offset].addr = 0x3402;
2379 reg_list[offset].val = G_gain >> 8;
2381 reg_list[offset].addr = 0x3403;
2382 reg_list[offset].val = G_gain & 0x00ff;
2385 if (B_gain > 0x400) {
2386 reg_list[offset].addr = 0x3404;
2387 reg_list[offset].val = B_gain >> 8;
2389 reg_list[offset].addr = 0x3405;
2390 reg_list[offset].val = B_gain & 0x00ff;
2393 reg_list[offset].addr = OV5693_TABLE_END;
2396 err = regmap_util_write_table_8(info->regmap,
2398 OV5693_TABLE_WAIT_MS,
2404 static int ov5693_lsc_wr(struct ov5693_info *info)
2406 struct reg_8 reg_list[64];
2411 if (info->cal.loaded == 0)
2415 reg_list[offset].addr = 0x5000;
2416 reg_list[offset].val = 0x86;
2418 for (i = 0; i < 62; i++) {
2419 reg_list[offset].addr = 0x5800 + i;
2420 reg_list[offset].val = info->cal.lenc[i];
2423 reg_list[offset].addr = OV5693_TABLE_END;
2426 err = regmap_util_write_table_8(info->regmap,
2428 OV5693_TABLE_WAIT_MS,
2434 static int ov5693_set_group_hold(struct ov5693_info *info,
2435 struct ov5693_ae *ae)
2438 struct reg_8 reg_list[16];
2440 bool group_hold = true; /* To use GROUP_HOLD macros */
2442 OV5693_ENTER_GROUP_HOLD(group_hold);
2443 if (ae->gain_enable)
2444 offset += ov5693_gain_reg(reg_list + offset,
2446 if (ae->frame_length_enable)
2447 offset += ov5693_frame_length_reg(reg_list + offset,
2449 if (ae->coarse_time_enable)
2450 offset += ov5693_coarse_time_reg(reg_list + offset,
2451 ae->coarse_time, ae->coarse_time_short);
2452 OV5693_LEAVE_GROUP_HOLD(group_hold);
2454 reg_list[offset].addr = OV5693_TABLE_END;
2455 err = regmap_util_write_table_8(info->regmap,
2457 OV5693_TABLE_WAIT_MS,
2463 static int ov5693_gpio_rd(struct ov5693_info *info,
2464 enum ov5693_gpio_type type)
2468 if (info->gpio[type].gpio) {
2469 val = gpio_get_value_cansleep(info->gpio[type].gpio);
2470 dev_dbg(&info->i2c_client->dev, "%s %u %d\n", __func__,
2471 info->gpio[type].gpio, val);
2472 if (!info->gpio[type].active_high)
2476 return val; /* return read value or error */
2479 static int ov5693_gpio_wr(struct ov5693_info *info,
2480 enum ov5693_gpio_type type,
2481 int val) /* val: 0=deassert, 1=assert */
2485 if (info->gpio[type].gpio) {
2486 if (!info->gpio[type].active_high)
2490 gpio_set_value_cansleep(info->gpio[type].gpio, val);
2491 dev_dbg(&info->i2c_client->dev, "%s %u %d\n", __func__,
2492 info->gpio[type].gpio, val);
2494 return err; /* return value written or error */
2497 static void ov5693_gpio_pwrdn(struct ov5693_info *info, int val)
2501 prev_val = ov5693_gpio_rd(info, OV5693_GPIO_TYPE_PWRDN);
2502 if ((prev_val < 0) || (val == prev_val))
2505 ov5693_gpio_wr(info, OV5693_GPIO_TYPE_PWRDN, val);
2506 if (!val && prev_val)
2507 /* if transition from assert to deassert then delay for I2C */
2511 static void ov5693_gpio_exit(struct ov5693_info *info)
2515 for (i = 0; i < ARRAY_SIZE(ov5693_gpio); i++) {
2516 if (info->gpio[i].gpio && info->gpio[i].own)
2517 gpio_free(info->gpio[i].gpio);
2521 static void ov5693_gpio_init(struct ov5693_info *info)
2524 unsigned long flags;
2530 if (!info->pdata->gpio_count || !info->pdata->gpio)
2533 for (i = 0; i < ARRAY_SIZE(ov5693_gpio); i++) {
2534 type = ov5693_gpio[i].gpio_type;
2535 for (j = 0; j < info->pdata->gpio_count; j++) {
2536 if (type == info->pdata->gpio[j].gpio_type)
2539 if (j == info->pdata->gpio_count)
2542 info->gpio[type].gpio = info->pdata->gpio[j].gpio;
2543 if (ov5693_gpio[i].use_flags) {
2544 flags = ov5693_gpio[i].flags;
2545 info->gpio[type].active_high =
2546 ov5693_gpio[i].active_high;
2548 info->gpio[type].active_high =
2549 info->pdata->gpio[j].active_high;
2550 if (info->gpio[type].active_high)
2551 flags = GPIOF_OUT_INIT_LOW;
2553 flags = GPIOF_OUT_INIT_HIGH;
2555 if (!info->pdata->gpio[j].init_en)
2558 snprintf(label, sizeof(label), "ov5693_%u_%s",
2559 info->pdata->num, ov5693_gpio[i].label);
2560 err = gpio_request_one(info->gpio[type].gpio, flags, label);
2562 dev_err(&info->i2c_client->dev,
2563 "%s ERR %s %u\n", __func__, label,
2564 info->gpio[type].gpio);
2566 info->gpio[type].own = true;
2567 dev_dbg(&info->i2c_client->dev,
2568 "%s %s %u\n", __func__, label,
2569 info->gpio[type].gpio);
2574 static void ov5693_mclk_disable(struct ov5693_info *info)
2576 dev_dbg(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
2577 clk_disable_unprepare(info->mclk);
2580 static int ov5693_mclk_enable(struct ov5693_info *info)
2583 unsigned long mclk_init_rate = 24000000;
2585 dev_dbg(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
2586 __func__, mclk_init_rate);
2588 err = clk_set_rate(info->mclk, mclk_init_rate);
2590 err = clk_prepare_enable(info->mclk);
2595 static int ov5693_power_off(struct ov5693_info *info)
2597 struct ov5693_power_rail *pw = &info->regulators;
2600 if (false == info->power_on)
2603 if (info->pdata && info->pdata->power_off) {
2604 err = info->pdata->power_off(pw);
2607 info->power_on = false;
2608 ov5693_mclk_disable(info);
2609 sysedp_set_state(info->sysedpc, 0);
2611 dev_err(&info->i2c_client->dev,
2612 "%s ERR: has no power_off function\n", __func__);
2618 static int ov5693_power_on(struct ov5693_info *info, bool standby)
2620 struct ov5693_power_rail *pw = &info->regulators;
2623 if (true == info->power_on)
2626 err = ov5693_mclk_enable(info);
2630 if (info->pdata && info->pdata->power_on) {
2631 err = info->pdata->power_on(pw);
2633 info->power_on = true;
2634 info->pwr_dev = NVC_PWR_ON;
2637 dev_err(&info->i2c_client->dev,
2638 "%s ERR: has no power_on function\n", __func__);
2643 ov5693_mclk_disable(info);
2648 static int ov5693_pm_wr(struct ov5693_info *info, int pwr)
2652 if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
2653 (pwr == NVC_PWR_OFF ||
2654 pwr == NVC_PWR_STDBY_OFF))
2655 pwr = NVC_PWR_STDBY;
2656 if (pwr == info->pwr_dev)
2660 case NVC_PWR_OFF_FORCE:
2662 case NVC_PWR_STDBY_OFF:
2663 err = ov5693_power_off(info);
2664 info->mode_valid = false;
2669 err = ov5693_power_on(info, true);
2674 err = ov5693_power_on(info, false);
2683 dev_err(&info->i2c_client->dev, "%s err %d\n", __func__, err);
2686 info->pwr_dev = pwr;
2687 dev_dbg(&info->i2c_client->dev, "%s pwr_dev=%d\n",
2688 __func__, info->pwr_dev);
2695 static int ov5693_pm_dev_wr(struct ov5693_info *info, int pwr)
2697 if (info->mode_enable)
2699 if (pwr < info->pwr_api)
2700 pwr = info->pwr_api;
2701 return ov5693_pm_wr(info, pwr);
2704 static void ov5693_pm_exit(struct ov5693_info *info)
2706 ov5693_pm_wr(info, NVC_PWR_OFF_FORCE);
2708 ov5693_gpio_exit(info);
2711 static void ov5693_regulator_get(struct ov5693_info *info,
2712 struct regulator **vreg,
2713 const char vreg_name[])
2715 struct regulator *reg = NULL;
2718 reg = devm_regulator_get(&info->i2c_client->dev, vreg_name);
2720 dev_err(&info->i2c_client->dev, "%s %s ERR: %d\n",
2721 __func__, vreg_name, (int)reg);
2724 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
2725 __func__, vreg_name);
2731 static void ov5693_pm_init(struct ov5693_info *info)
2733 struct ov5693_power_rail *pw = &info->regulators;
2735 ov5693_gpio_init(info);
2737 ov5693_regulator_get(info, &pw->avdd, info->pdata->regulators.avdd);
2739 ov5693_regulator_get(info, &pw->dvdd, info->pdata->regulators.dvdd);
2741 ov5693_regulator_get(info, &pw->dovdd, info->pdata->regulators.dovdd);
2743 info->power_on = false;
2746 static int ov5693_mode_able(struct ov5693_info *info, bool mode_enable)
2755 err = regmap_write(info->regmap, 0x0100, val);
2757 info->mode_enable = mode_enable;
2758 dev_dbg(&info->i2c_client->dev, "%s streaming=%x\n",
2759 __func__, info->mode_enable);
2761 ov5693_pm_dev_wr(info, NVC_PWR_STDBY);
2766 static int ov5693_mode_wr_full(struct ov5693_info *info, u32 mode_index)
2770 ov5693_pm_dev_wr(info, NVC_PWR_ON);
2771 ov5693_bin_wr(info, 0);
2772 err = regmap_util_write_table_8(info->regmap,
2773 mode_table[mode_index],
2775 OV5693_TABLE_WAIT_MS,
2778 dev_dbg(&info->i2c_client->dev,
2779 "init done(mode=%d)!!!\n", mode_index);
2780 info->mode_index = mode_index;
2781 info->mode_valid = true;
2783 dev_dbg(&info->i2c_client->dev,
2784 "init error(mode=%d)!!!\n", mode_index);
2785 info->mode_valid = false;
2791 static int ov5693_set_mode(struct ov5693_info *info,
2792 struct ov5693_mode *mode)
2797 if (!mode->res_x && !mode->res_y) {
2798 if (mode->frame_length || mode->coarse_time || mode->gain) {
2799 /* write exposure only */
2800 err = ov5693_exposure_wr(info, mode);
2803 /* turn off streaming */
2804 err = ov5693_mode_able(info, false);
2809 if (mode->hdr_en == true) {
2810 if (mode->res_x == 2592 && mode->res_y == 1944)
2811 mode_index = OV5693_MODE_2592x1944_HDR;
2812 else if (mode->res_x == 1296 && mode->res_y == 972)
2813 mode_index = OV5693_MODE_1296x972_HDR;
2814 else if (mode->res_x == 1920 && mode->res_y == 1080)
2815 mode_index = OV5693_MODE_1920x1080_HDR;
2816 else if (mode->res_x == 1280 && mode->res_y == 720)
2817 mode_index = OV5693_MODE_1280x720_HDR_60FPS;
2819 if (mode->res_x == 2592 && mode->res_y == 1944)
2820 mode_index = OV5693_MODE_2592x1944;
2821 else if (mode->res_x == 1296 && mode->res_y == 972)
2822 mode_index = OV5693_MODE_1296x972;
2823 else if (mode->res_x == 1920 && mode->res_y == 1080)
2824 mode_index = OV5693_MODE_1920x1080;
2825 else if (mode->res_x == 1280 && mode->res_y == 720)
2826 mode_index = OV5693_MODE_1280x720_120FPS;
2829 /* request highest edp state */
2830 sysedp_set_state(info->sysedpc, 1);
2832 if (!info->mode_valid || (info->mode_index != mode_index))
2833 err = ov5693_mode_wr_full(info, mode_index);
2835 dev_dbg(&info->i2c_client->dev, "%s short mode\n", __func__);
2836 dev_dbg(&info->i2c_client->dev, "%s: mode #: %d\n",
2837 __func__, mode_index);
2838 dev_dbg(&info->i2c_client->dev, "%s: AE: %d, %d, %d\n",
2839 __func__, mode->frame_length,
2840 mode->coarse_time, mode->gain);
2841 err |= ov5693_exposure_wr(info, mode);
2843 info->mode_valid = false;
2844 dev_err(&info->i2c_client->dev,
2845 "%s set_mode error\n", __func__);
2846 goto ov5693_mode_wr_err;
2848 err = ov5693_awb_wr(info);
2850 info->mode_valid = false;
2851 dev_err(&info->i2c_client->dev,
2852 "%s:%d awb cal write error\n", __func__, __LINE__);
2853 goto ov5693_mode_wr_err;
2855 err = ov5693_lsc_wr(info);
2857 info->mode_valid = false;
2858 dev_err(&info->i2c_client->dev,
2859 "%s:%d lsc cal write error\n", __func__, __LINE__);
2860 goto ov5693_mode_wr_err;
2862 err = regmap_write(info->regmap, 0x0100, 0x01);
2864 info->mode_valid = false;
2865 dev_err(&info->i2c_client->dev,
2866 "%s:%d stream on failed\n", __func__, __LINE__);
2867 goto ov5693_mode_wr_err;
2873 if (!info->mode_enable)
2874 ov5693_pm_dev_wr(info, NVC_PWR_OFF);
2878 static int ov5693_get_fuse_id(struct ov5693_info *info)
2880 /* fuse stored at ov5693 bank 0 */
2882 err = regmap_write(info->regmap, 0x0100, 0x01);
2884 dev_err(&info->i2c_client->dev,
2885 "%s ERR %d: cannot write stream mode\n", __func__,
2889 usleep_range(10000, 11000);
2891 err = regmap_write(info->regmap, 0x3D84, 0xC0);
2893 dev_err(&info->i2c_client->dev,
2894 "%s ERR %d: cannot write bank index 0\n", __func__,
2898 usleep_range(10000, 11000);
2900 err = regmap_write(info->regmap, 0x3D81, 0x01);
2902 dev_err(&info->i2c_client->dev,
2903 "%s ERR %d: cannot load OTP data\n", __func__, err);
2906 usleep_range(10000, 11000);
2908 err = regmap_bulk_read(info->regmap, 0x3D00,
2909 info->fuseid.data, OV5693_FUSE_ID_SIZE);
2911 dev_err(&info->i2c_client->dev,
2912 "%s ERR %d: cannot read OTP buffer\n", __func__,
2917 info->fuseid.size = OV5693_FUSE_ID_SIZE;
2922 static int ov5693_read_otp_bank(struct ov5693_info *info,
2923 struct ov5693_otp_bank *bank)
2927 err = regmap_write(info->regmap, 0x0100, 0x01);
2929 dev_err(&info->i2c_client->dev,
2930 "%s ERR %d: cannot write stream mode\n", __func__,
2934 err = regmap_write(info->regmap, 0x3d84, 0xc0 | bank->id);
2936 dev_err(&info->i2c_client->dev,
2937 "%s ERR %d: cannot write bank index %d\n", __func__,
2941 err = regmap_write(info->regmap, 0x3d81, 0x01);
2943 dev_err(&info->i2c_client->dev,
2944 "%s ERR %d: cannot load OTP data\n", __func__, err);
2947 usleep_range(1000, 11000);
2948 err = regmap_bulk_read(info->regmap, 0x3d00, bank->buf, 16);
2950 dev_err(&info->i2c_client->dev,
2951 "%s ERR %d: cannot read OTP buffer\n", __func__,
2960 ov5693_eeprom_device_release(struct ov5693_info *info)
2964 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
2965 if (info->eeprom[i].i2c_client != NULL) {
2966 i2c_unregister_device(info->eeprom[i].i2c_client);
2967 info->eeprom[i].i2c_client = NULL;
2975 ov5693_eeprom_device_init(struct ov5693_info *info)
2977 char *dev_name = "eeprom_ov5693";
2978 static struct regmap_config eeprom_regmap_config = {
2985 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
2986 info->eeprom[i].adap = i2c_get_adapter(
2987 info->i2c_client->adapter->nr);
2988 memset(&info->eeprom[i].brd, 0, sizeof(info->eeprom[i].brd));
2989 strncpy(info->eeprom[i].brd.type, dev_name,
2990 sizeof(info->eeprom[i].brd.type));
2991 info->eeprom[i].brd.addr = OV5693_EEPROM_ADDRESS + i;
2992 info->eeprom[i].i2c_client = i2c_new_device(
2993 info->eeprom[i].adap, &info->eeprom[i].brd);
2995 info->eeprom[i].regmap = devm_regmap_init_i2c(
2996 info->eeprom[i].i2c_client, &eeprom_regmap_config);
2997 if (IS_ERR(info->eeprom[i].regmap)) {
2998 err = PTR_ERR(info->eeprom[i].regmap);
2999 ov5693_eeprom_device_release(info);
3008 ov5693_read_eeprom(struct ov5693_info *info, u8 reg, u16 length, u8 *buf)
3010 return regmap_raw_read(info->eeprom[0].regmap, reg, &buf[reg], length);
3014 ov5693_write_eeprom(struct ov5693_info *info, u16 addr, u8 val)
3016 return regmap_write(info->eeprom[addr >> 8].regmap, addr & 0xFF, val);
3019 static long ov5693_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3021 struct ov5693_info *info = file->private_data;
3024 switch (_IOC_NR(cmd)) {
3025 case _IOC_NR(OV5693_IOCTL_SET_POWER):
3027 u32 powerlevel = (u32) arg;
3031 if (powerlevel > NVC_PWR_ON) {
3032 dev_err(&info->i2c_client->dev,
3033 "%s:Invalid power level.\n",
3038 err = ov5693_pm_wr(info, powerlevel);
3041 case _IOC_NR(OV5693_IOCTL_GET_CAPS):
3042 if (copy_to_user((void __user *)arg,
3044 sizeof(struct nvc_imager_cap))) {
3045 dev_err(&info->i2c_client->dev,
3046 "%s copy_to_user err line %d\n",
3047 __func__, __LINE__);
3051 case _IOC_NR(OV5693_IOCTL_SET_MODE):
3053 struct ov5693_mode mode;
3054 if (copy_from_user(&mode,
3055 (const void __user *)arg,
3056 sizeof(struct ov5693_mode))) {
3057 dev_err(&info->i2c_client->dev,
3058 "%s:Failed to get mode from user.\n",
3062 return ov5693_set_mode(info, &mode);
3064 case _IOC_NR(OV5693_IOCTL_GET_STATUS): {
3066 if (copy_to_user((void __user *)arg, &status, sizeof(status))) {
3067 dev_err(&info->i2c_client->dev,
3068 "%s:Failed to copy status to user.\n",
3075 case _IOC_NR(OV5693_IOCTL_SET_GROUP_HOLD): {
3076 struct ov5693_ae ae;
3077 if (copy_from_user(&ae, (const void __user *)arg,
3078 sizeof(struct ov5693_ae))) {
3079 dev_dbg(&info->i2c_client->dev,
3080 "%s:fail group hold\n", __func__);
3084 return ov5693_set_group_hold(info, &ae);
3087 case _IOC_NR(OV5693_IOCTL_SET_FRAME_LENGTH):
3088 return ov5693_set_frame_length(info, (u32)arg, true);
3090 case _IOC_NR(OV5693_IOCTL_SET_COARSE_TIME):
3091 return ov5693_set_coarse_time(info, (u32)arg,
3092 OV5693_INVALID_COARSE_TIME, true);
3094 case _IOC_NR(OV5693_IOCTL_SET_HDR_COARSE_TIME):
3096 struct ov5693_hdr *hdrcoarse = (struct ov5693_hdr *)arg;
3097 int ret = ov5693_set_coarse_time(info,
3098 hdrcoarse->coarse_time_long,
3099 hdrcoarse->coarse_time_short,
3104 case _IOC_NR(OV5693_IOCTL_SET_GAIN):
3105 return ov5693_set_gain(info, (u32)arg, true);
3107 case _IOC_NR(OV5693_IOCTL_GET_FUSEID):
3109 err = ov5693_get_fuse_id(info);
3112 dev_err(&info->i2c_client->dev, "%s:Failed to get fuse id info.\n",
3116 if (copy_to_user((void __user *)arg,
3118 sizeof(struct nvc_fuseid))) {
3119 dev_dbg(&info->i2c_client->dev, "%s:Fail copy fuse id to user space\n",
3126 case _IOC_NR(OV5693_IOCTL_READ_OTP_BANK):
3128 struct ov5693_otp_bank bank;
3129 if (copy_from_user(&bank,
3130 (const void __user *)arg,
3132 dev_err(&info->i2c_client->dev,
3133 "%s %d copy_from_user err\n",
3134 __func__, __LINE__);
3138 err = ov5693_read_otp_bank(info, &bank);
3142 if (copy_to_user((void __user *)arg,
3145 dev_err(&info->i2c_client->dev,
3146 "%s %d copy_to_user err\n",
3147 __func__, __LINE__);
3153 case _IOC_NR(OV5693_IOCTL_SET_CAL_DATA):
3155 if (copy_from_user(&info->cal, (const void __user *)arg,
3156 sizeof(info->cal))) {
3157 dev_err(&info->i2c_client->dev,
3158 "%s %d copy_from_user err\n",
3159 __func__, __LINE__);
3165 case _IOC_NR(OV5693_IOCTL_GET_EEPROM_DATA):
3167 if (!info->pdata->has_eeprom)
3170 ov5693_read_eeprom(info,
3175 if (copy_to_user((void __user *)arg,
3176 info->eeprom_buf, OV5693_EEPROM_SIZE)) {
3177 dev_err(&info->i2c_client->dev,
3178 "%s:Failed to copy status to user\n",
3185 case _IOC_NR(OV5693_IOCTL_SET_EEPROM_DATA):
3189 if (!info->pdata->has_eeprom)
3192 if (copy_from_user(info->eeprom_buf,
3193 (const void __user *)arg, OV5693_EEPROM_SIZE)) {
3194 dev_err(&info->i2c_client->dev,
3195 "%s:Failed to read from user buffer\n",
3199 for (i = 0; i < OV5693_EEPROM_SIZE; i++) {
3200 ov5693_write_eeprom(info,
3202 info->eeprom_buf[i]);
3209 dev_err(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
3215 static void ov5693_sdata_init(struct ov5693_info *info)
3217 memcpy(&info->sdata, &ov5693_dflt_sdata, sizeof(info->sdata));
3218 if (info->pdata->lens_focal_length)
3219 info->sdata.focal_len = info->pdata->lens_focal_length;
3220 if (info->pdata->lens_max_aperture)
3221 info->sdata.max_aperture = info->pdata->lens_max_aperture;
3222 if (info->pdata->lens_fnumber)
3223 info->sdata.fnumber = info->pdata->lens_fnumber;
3224 if (info->pdata->lens_view_angle_h)
3225 info->sdata.view_angle_h = info->pdata->lens_view_angle_h;
3226 if (info->pdata->lens_view_angle_v)
3227 info->sdata.view_angle_v = info->pdata->lens_view_angle_v;
3230 static int ov5693_open(struct inode *inode, struct file *file)
3233 struct miscdevice *miscdev = file->private_data;
3234 struct ov5693_info *info = dev_get_drvdata(miscdev->parent);
3236 if (atomic_xchg(&info->in_use, 1))
3239 file->private_data = info;
3240 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3242 err = ov5693_power_on(info, false);
3246 static int ov5693_release(struct inode *inode, struct file *file)
3248 struct ov5693_info *info = file->private_data;
3250 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3251 ov5693_pm_wr(info, NVC_PWR_OFF);
3252 file->private_data = NULL;
3253 WARN_ON(!atomic_xchg(&info->in_use, 0));
3257 static const struct file_operations ov5693_fileops = {
3258 .owner = THIS_MODULE,
3259 .open = ov5693_open,
3260 .unlocked_ioctl = ov5693_ioctl,
3261 #ifdef CONFIG_COMPAT
3262 .compat_ioctl = ov5693_ioctl,
3264 .release = ov5693_release,
3267 static void ov5693_del(struct ov5693_info *info)
3269 ov5693_pm_exit(info);
3273 static int ov5693_remove(struct i2c_client *client)
3275 struct ov5693_info *info = i2c_get_clientdata(client);
3277 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3278 misc_deregister(&info->miscdev);
3279 sysedp_free_consumer(info->sysedpc);
3280 if (info->pdata->has_eeprom)
3281 ov5693_eeprom_device_release(info);
3286 static struct of_device_id ov5693_of_match[] = {
3287 { .compatible = "nvidia,ov5693", },
3291 MODULE_DEVICE_TABLE(of, ov5693_of_match);
3293 static int ov5693_platform_power_on(struct ov5693_power_rail *pw)
3296 struct ov5693_info *info = container_of(pw, struct ov5693_info,
3299 if (info->pdata->use_vcm_vdd) {
3300 err = regulator_enable(info->ext_vcm_vdd);
3302 goto ov5693_vcm_fail;
3305 ov5693_gpio_pwrdn(info, 0);
3306 usleep_range(10, 20);
3308 err = regulator_enable(pw->avdd);
3310 goto ov5693_avdd_fail;
3312 err = regulator_enable(pw->dovdd);
3314 goto ov5693_iovdd_fail;
3317 ov5693_gpio_pwrdn(info, 1);
3319 usleep_range(300, 310);
3324 regulator_disable(pw->avdd);
3327 if (info->pdata->use_vcm_vdd)
3328 regulator_disable(info->ext_vcm_vdd);
3331 pr_err("%s FAILED\n", __func__);
3335 static int ov5693_platform_power_off(struct ov5693_power_rail *pw)
3337 struct ov5693_info *info = container_of(pw, struct ov5693_info,
3340 usleep_range(21, 25);
3341 ov5693_gpio_pwrdn(info, 0);
3344 regulator_disable(pw->dovdd);
3345 regulator_disable(pw->avdd);
3346 if (info->pdata->use_vcm_vdd)
3347 regulator_disable(info->ext_vcm_vdd);
3352 static int ov5693_parse_dt_gpio(struct device_node *np, const char *name,
3353 enum ov5693_gpio_type type,
3354 struct nvc_gpio_pdata *pdata)
3356 enum of_gpio_flags gpio_flags;
3358 if (of_find_property(np, name, NULL)) {
3359 pdata->gpio = of_get_named_gpio_flags(np, name, 0, &gpio_flags);
3360 pdata->gpio_type = type;
3361 pdata->init_en = true;
3362 pdata->active_high = !(gpio_flags & OF_GPIO_ACTIVE_LOW);
3368 static struct ov5693_platform_data *ov5693_parse_dt(struct i2c_client *client)
3370 struct device_node *np = client->dev.of_node;
3371 struct ov5693_platform_data *pdata;
3372 struct nvc_gpio_pdata *gpio_pdata = NULL;
3374 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
3376 dev_err(&client->dev, "Failed to allocate pdata\n");
3377 return ERR_PTR(-ENOMEM);
3380 gpio_pdata = devm_kzalloc(&client->dev,
3381 sizeof(*gpio_pdata) * ARRAY_SIZE(ov5693_gpio), GFP_KERNEL);
3383 dev_err(&client->dev, "cannot allocate gpio data memory\n");
3384 return ERR_PTR(-ENOMEM);
3387 /* init with default platform data values */
3388 memcpy(pdata, &ov5693_dflt_pdata, sizeof(*pdata));
3390 /* extra regulators info */
3391 pdata->use_vcm_vdd = of_property_read_bool(np, "nvidia,use-vcm-vdd");
3394 of_property_read_u32(np, "nvidia,num", &pdata->num);
3395 of_property_read_string(np, "nvidia,dev-name", &pdata->dev_name);
3398 pdata->gpio_count = 0;
3399 pdata->gpio_count += ov5693_parse_dt_gpio(np,
3400 "reset-gpios", OV5693_GPIO_TYPE_PWRDN,
3401 &gpio_pdata[pdata->gpio_count]);
3402 pdata->gpio = gpio_pdata;
3404 /* MCLK clock info */
3405 of_property_read_string(np, "nvidia,mclk_name",
3408 /* ov5693 power functions */
3409 pdata->power_on = ov5693_platform_power_on;
3410 pdata->power_off = ov5693_platform_power_off;
3415 static int ov5693_probe(
3416 struct i2c_client *client,
3417 const struct i2c_device_id *id)
3419 struct ov5693_info *info;
3420 unsigned long clock_probe_rate;
3422 const char *mclk_name;
3423 static struct regmap_config ov5693_regmap_config = {
3429 dev_dbg(&client->dev, "%s\n", __func__);
3430 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
3432 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
3436 info->i2c_client = client;
3437 if (client->dev.of_node) {
3438 info->pdata = ov5693_parse_dt(client);
3439 if (IS_ERR(info->pdata)) {
3440 err = PTR_ERR(info->pdata);
3441 dev_err(&client->dev,
3442 "Failed to parse OF node: %d\n", err);
3445 } else if (client->dev.platform_data)
3446 info->pdata = client->dev.platform_data;
3448 info->pdata = &ov5693_dflt_pdata;
3449 dev_dbg(&client->dev,
3450 "%s No platform data. Using defaults.\n",
3453 if (info->pdata->use_vcm_vdd) {
3454 info->ext_vcm_vdd = devm_regulator_get(&info->i2c_client->dev,
3456 if (WARN_ON(IS_ERR(info->ext_vcm_vdd))) {
3457 err = PTR_ERR(info->ext_vcm_vdd);
3458 dev_err(&client->dev,
3459 "ext_vcm_vdd get failed %d\n", err);
3460 info->ext_vcm_vdd = NULL;
3465 info->regmap = devm_regmap_init_i2c(client, &ov5693_regmap_config);
3466 if (IS_ERR(info->regmap)) {
3467 err = PTR_ERR(info->regmap);
3468 dev_err(&client->dev,
3469 "Failed to allocate register map: %d\n", err);
3473 if (info->pdata->has_eeprom) {
3474 err = ov5693_eeprom_device_init(info);
3476 dev_err(&client->dev,
3477 "Failed to allocate eeprom register map: %d\n", err);
3482 mclk_name = info->pdata->mclk_name ?
3483 info->pdata->mclk_name : "default_mclk";
3484 info->mclk = devm_clk_get(&client->dev, mclk_name);
3485 if (IS_ERR(info->mclk)) {
3486 dev_err(&client->dev, "%s: unable to get clock %s\n",
3487 __func__, mclk_name);
3488 return PTR_ERR(info->mclk);
3491 i2c_set_clientdata(client, info);
3492 ov5693_pm_init(info);
3493 if (IS_ERR(info->regulators.avdd) || IS_ERR(info->regulators.dovdd))
3496 ov5693_sdata_init(info);
3497 if (info->pdata->cfg & (NVC_CFG_NODEV | NVC_CFG_BOOT_INIT)) {
3498 if (info->pdata->probe_clock) {
3499 clock_probe_rate = 6000; /* initial_clcok*/
3500 clock_probe_rate *= 1000;
3501 info->pdata->probe_clock(clock_probe_rate);
3503 ov5693_pm_dev_wr(info, NVC_PWR_COMM);
3504 ov5693_pm_dev_wr(info, NVC_PWR_OFF);
3505 if (info->pdata->probe_clock)
3506 info->pdata->probe_clock(0);
3508 if (info->pdata->dev_name != NULL)
3509 strncpy(info->devname, info->pdata->dev_name,
3510 sizeof(info->devname) - 1);
3512 strncpy(info->devname, "ov5693", sizeof(info->devname) - 1);
3513 if (info->pdata->num)
3514 snprintf(info->devname, sizeof(info->devname), "%s.%u",
3515 info->devname, info->pdata->num);
3517 info->miscdev.name = info->devname;
3518 info->miscdev.fops = &ov5693_fileops;
3519 info->miscdev.minor = MISC_DYNAMIC_MINOR;
3520 info->miscdev.parent = &client->dev;
3521 if (misc_register(&info->miscdev)) {
3522 dev_err(&client->dev, "%s unable to register misc device %s\n",
3523 __func__, info->devname);
3528 info->sysedpc = sysedp_create_consumer("ov5693", info->devname);
3530 dev_dbg(&client->dev, "ov5693 sensor driver loading done\n");
3534 static const struct i2c_device_id ov5693_id[] = {
3540 MODULE_DEVICE_TABLE(i2c, ov5693_id);
3542 static struct i2c_driver ov5693_i2c_driver = {
3545 .owner = THIS_MODULE,
3546 .of_match_table = ov5693_of_match,
3548 .id_table = ov5693_id,
3549 .probe = ov5693_probe,
3550 .remove = ov5693_remove,
3553 module_i2c_driver(ov5693_i2c_driver);
3554 MODULE_LICENSE("GPL v2");