2 * Copyright (c) 2013-2015, 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 <media/camera_common.h>
36 #include "cam_dev/camera_gpio.h"
37 #include "nvc_utilities.h"
39 #define OV5693_ID 0x5693
40 #define OV5693_SENSOR_TYPE NVC_IMAGER_TYPE_RAW
41 #define OV5693_RES_CHG_WAIT_TIME_MS 100
42 #define OV5693_SIZEOF_I2C_BUF 16
43 #define OV5693_TABLE_WAIT_MS 0
44 #define OV5693_TABLE_END 1
45 #define OV5693_TABLE_RESET_TIMEOUT 50
46 #define OV5693_LENS_MAX_APERTURE 0 /* _INT2FLOAT_DIVISOR */
47 #define OV5693_LENS_FNUMBER 0 /* _INT2FLOAT_DIVISOR */
48 #define OV5693_LENS_FOCAL_LENGTH 6120 /* _INT2FLOAT_DIVISOR */
49 #define OV5693_LENS_VIEW_ANGLE_H 60000 /* _INT2FLOAT_DIVISOR */
50 #define OV5693_LENS_VIEW_ANGLE_V 60000 /* _INT2FLOAT_DIVISOR */
51 #define OV5693_OTP_BUF_SIZE 16
52 #define OV5693_FUSE_ID_SIZE 8
54 static struct nvc_gpio_init ov5693_gpio[] = {
55 { OV5693_GPIO_TYPE_PWRDN, GPIOF_OUT_INIT_LOW, "pwrdn", true, true, },
56 { OV5693_GPIO_TYPE_RESET, GPIOF_OUT_INIT_LOW, "reset", true, true, },
61 struct i2c_client *i2c_client;
62 struct ov5693_platform_data *pdata;
64 struct miscdevice miscdev;
67 struct nvc_gpio gpio[ARRAY_SIZE(ov5693_gpio)];
68 struct ov5693_power_rail regulators;
73 unsigned test_pattern;
74 struct nvc_imager_static_nvc sdata;
76 struct nvc_fuseid fuseid;
77 struct regmap *regmap;
78 struct regulator *ext_vcm_vdd;
79 struct ov5693_cal_data cal;
80 struct sysedp_consumer *sysedpc;
82 struct ov5693_eeprom_data eeprom[OV5693_EEPROM_NUM_BLOCKS];
83 u8 eeprom_buf[OV5693_EEPROM_SIZE];
86 struct ov5693_mode_data {
87 struct nvc_imager_mode sensor_mode;
88 struct nvc_imager_dynamic_nvc sensor_dnvc;
89 struct reg_8 *p_mode_i2c;
92 static struct ov5693_platform_data ov5693_dflt_pdata = {
104 * NOTE: static vs dynamic
105 * If a member in the nvc_imager_static_nvc structure is not actually
106 * static data, then leave blank and add the parameter to the parameter
107 * read function that dynamically reads the data. The NVC user driver
108 * will call the parameter read for the data if the member data is 0.
109 * If the dynamic data becomes static during probe (a one time read
110 * such as device ID) then add the dynamic read to the _sdata_init
113 static struct nvc_imager_static_nvc ov5693_dflt_sdata = {
114 .api_version = NVC_IMAGER_API_STATIC_VER,
115 .sensor_type = OV5693_SENSOR_TYPE,
116 .bits_per_pixel = 10,
117 .sensor_id = OV5693_ID,
118 .sensor_id_minor = 0,
119 .focal_len = OV5693_LENS_FOCAL_LENGTH,
120 .max_aperture = OV5693_LENS_MAX_APERTURE,
121 .fnumber = OV5693_LENS_FNUMBER,
122 .view_angle_h = OV5693_LENS_VIEW_ANGLE_H,
123 .view_angle_v = OV5693_LENS_VIEW_ANGLE_V,
124 .res_chg_wait_time = OV5693_RES_CHG_WAIT_TIME_MS,
127 static const struct reg_8 ov5693_2592x1944_i2c[] = {
128 {0x0100, 0x00},/* Including sw reset */
349 {0x4800, 0x20}, /* dis-continuous */
390 {OV5693_TABLE_END, 0x0000}
393 static const struct reg_8 ov5693_1296x972_i2c[] = {
394 {0x0100, 0x00},/* Including sw reset */
596 {0x4800, 0x20}, /* dis-continuous */
637 {OV5693_TABLE_END, 0x0000}
640 static const struct reg_8 ov5693_1920x1080_i2c[] = {
641 {0x0100, 0x00},/*, 0xIncluding, 0xsw, 0xreset, 0x*/
843 {0x4800, 0x20}, /* dis-continuous */
884 {OV5693_TABLE_END, 0x0000}
887 static const struct reg_8 ov5693_2592x1458_i2c[] = {
888 {0x0100, 0x00},/*, 0xIncluding, 0xsw, 0xreset, 0x*/
1097 {0x4800, 0x20}, /* dis-continuous */
1142 {OV5693_TABLE_END, 0x0000}
1145 static const struct reg_8 ov5693_1280x720_120fps_i2c[] = {
1146 {0x0100, 0x00},/* Including sw reset */
1348 {0x4800, 0x20}, /* dis-continuous */
1389 {OV5693_TABLE_END, 0x0000}
1392 static const struct reg_8 ov5693_2592x1944_HDR_24fps_i2c[] = {
1393 {0x0100, 0x00},/* Including sw reset */
1596 {0x4800, 0x20}, /* dis-continuous */
1637 {OV5693_TABLE_END, 0x0000}
1640 static const struct reg_8 ov5693_1920x1080_HDR_30fps_i2c[] = {
1641 {0x0100, 0x00},/* Including sw reset */
1844 {0x4800, 0x20}, /* dis-continuous */
1885 {OV5693_TABLE_END, 0x0000}
1888 static const struct reg_8 ov5693_1296x972_HDR_30fps_i2c[] = {
1889 {0x0100, 0x00},/* Including sw reset */
2092 {0x4800, 0x20}, /* dis-continuous */
2133 {OV5693_TABLE_END, 0x0000}
2136 static const struct reg_8 ov5693_1280x720_HDR_60fps_i2c[] = {
2137 {0x0100, 0x00},/* Including sw reset */
2340 {0x4800, 0x20}, /* dis-continuous */
2381 {OV5693_TABLE_END, 0x0000}
2385 OV5693_MODE_2592x1944 = 0,
2386 OV5693_MODE_1920x1080,
2387 OV5693_MODE_2592x1458,
2388 OV5693_MODE_1296x972,
2389 OV5693_MODE_1280x720_120FPS,
2390 OV5693_MODE_2592x1944_HDR,
2391 OV5693_MODE_1920x1080_HDR,
2392 OV5693_MODE_1296x972_HDR,
2393 OV5693_MODE_1280x720_HDR_60FPS,
2396 static const struct reg_8 *mode_table[] = {
2397 [OV5693_MODE_2592x1944] = ov5693_2592x1944_i2c,
2398 [OV5693_MODE_1920x1080] = ov5693_1920x1080_i2c,
2399 [OV5693_MODE_2592x1458] = ov5693_2592x1458_i2c,
2400 [OV5693_MODE_1296x972] = ov5693_1296x972_i2c,
2401 [OV5693_MODE_1280x720_120FPS] = ov5693_1280x720_120fps_i2c,
2402 [OV5693_MODE_2592x1944_HDR] = ov5693_2592x1944_HDR_24fps_i2c,
2403 [OV5693_MODE_1920x1080_HDR] = ov5693_1920x1080_HDR_30fps_i2c,
2404 [OV5693_MODE_1296x972_HDR] = ov5693_1296x972_HDR_30fps_i2c,
2405 [OV5693_MODE_1280x720_HDR_60FPS] = ov5693_1280x720_HDR_60fps_i2c,
2408 static inline int ov5693_frame_length_reg(struct reg_8 *regs,
2411 regs->addr = 0x380E;
2412 regs->val = (frame_length >> 8) & 0xff;
2413 (regs + 1)->addr = 0x380F;
2414 (regs + 1)->val = (frame_length) & 0xff;
2419 static inline int ov5693_coarse_time_reg(struct reg_8 *regs,
2420 u32 coarse_time, u32 coarse_time_short)
2424 regs->addr = 0x3500;
2425 regs->val = (coarse_time >> 12) & 0xff;
2426 (regs + 1)->addr = 0x3501;
2427 (regs + 1)->val = (coarse_time >> 4) & 0xff;
2428 (regs + 2)->addr = 0x3502;
2429 (regs + 2)->val = (coarse_time & 0xf) << 4;
2433 if (coarse_time_short != OV5693_INVALID_COARSE_TIME) {
2434 (regs + 3)->addr = 0x3506;
2435 (regs + 3)->val = (coarse_time_short >> 12) & 0xff;
2436 (regs + 4)->addr = 0x3507;
2437 (regs + 4)->val = (coarse_time_short >> 4) & 0xff;
2438 (regs + 5)->addr = 0x3508;
2439 (regs + 5)->val = (coarse_time_short & 0xf) << 4;
2447 #define OV5693_ENTER_GROUP_HOLD(group_hold) \
2450 reg_list[offset].addr = 0x3208; \
2451 reg_list[offset].val = 0x01;\
2456 #define OV5693_LEAVE_GROUP_HOLD(group_hold) \
2459 reg_list[offset].addr = 0x3208; \
2460 reg_list[offset].val = 0x11;\
2462 reg_list[offset].addr = 0x3208; \
2463 reg_list[offset].val = 0x61;\
2468 static int ov5693_set_frame_length(struct ov5693_info *info,
2469 u32 frame_length, bool group_hold)
2471 struct reg_8 reg_list[9];
2475 OV5693_ENTER_GROUP_HOLD(group_hold);
2476 offset += ov5693_frame_length_reg(reg_list + offset, frame_length);
2477 OV5693_LEAVE_GROUP_HOLD(group_hold);
2479 reg_list[offset].addr = OV5693_TABLE_END;
2482 err = regmap_util_write_table_8(info->regmap,
2484 OV5693_TABLE_WAIT_MS,
2490 static int ov5693_set_coarse_time(struct ov5693_info *info,
2491 u32 coarse_time, u32 coarse_time_short,
2494 struct reg_8 reg_list[16];
2498 OV5693_ENTER_GROUP_HOLD(group_hold);
2499 offset += ov5693_coarse_time_reg(reg_list + offset,
2502 OV5693_LEAVE_GROUP_HOLD(group_hold);
2504 reg_list[offset].addr = OV5693_TABLE_END;
2507 err = regmap_util_write_table_8(info->regmap,
2509 OV5693_TABLE_WAIT_MS,
2515 static inline int ov5693_gain_reg(struct reg_8 *regs, u32 gain)
2517 (regs)->addr = 0x350A;
2518 (regs)->val = gain >> 8;
2520 (regs + 1)->addr = 0x350B;
2521 (regs + 1)->val = gain & 0x00FF;
2526 static int ov5693_bin_wr(struct ov5693_info *info, u8 enable)
2530 if (enable == info->bin_en)
2534 info->bin_en = enable;
2535 dev_dbg(&info->i2c_client->dev, "%s bin_en=%x err=%d\n",
2536 __func__, info->bin_en, err);
2540 static int ov5693_exposure_wr(struct ov5693_info *info,
2541 struct ov5693_mode *mode)
2543 struct reg_8 reg_list[16];
2547 offset += ov5693_coarse_time_reg(reg_list + offset,
2549 mode->coarse_time_short);
2550 offset += ov5693_gain_reg(reg_list + offset, mode->gain);
2552 reg_list[offset].addr = OV5693_TABLE_END;
2553 err = regmap_util_write_table_8(info->regmap,
2555 OV5693_TABLE_WAIT_MS,
2562 static int ov5693_set_gain(struct ov5693_info *info, u32 gain, bool group_hold)
2564 struct reg_8 reg_list[9];
2568 OV5693_ENTER_GROUP_HOLD(group_hold);
2569 offset += ov5693_gain_reg(reg_list + offset, gain);
2570 OV5693_LEAVE_GROUP_HOLD(group_hold);
2572 reg_list[offset].addr = OV5693_TABLE_END;
2575 err = regmap_util_write_table_8(info->regmap,
2577 OV5693_TABLE_WAIT_MS,
2583 static int ov5693_set_group_hold(struct ov5693_info *info,
2584 struct ov5693_ae *ae)
2587 struct reg_8 reg_list[16];
2589 bool group_hold = true; /* To use GROUP_HOLD macros */
2591 OV5693_ENTER_GROUP_HOLD(group_hold);
2592 if (ae->gain_enable)
2593 offset += ov5693_gain_reg(reg_list + offset,
2595 if (ae->frame_length_enable)
2596 offset += ov5693_frame_length_reg(reg_list + offset,
2598 if (ae->coarse_time_enable)
2599 offset += ov5693_coarse_time_reg(reg_list + offset,
2600 ae->coarse_time, ae->coarse_time_short);
2601 OV5693_LEAVE_GROUP_HOLD(group_hold);
2603 reg_list[offset].addr = OV5693_TABLE_END;
2604 err = regmap_util_write_table_8(info->regmap,
2606 OV5693_TABLE_WAIT_MS,
2612 static int ov5693_gpio_rd(struct ov5693_info *info,
2613 enum ov5693_gpio_type type)
2617 if (info->gpio[type].gpio) {
2618 val = gpio_get_value_cansleep(info->gpio[type].gpio);
2619 dev_dbg(&info->i2c_client->dev, "%s %u %d\n", __func__,
2620 info->gpio[type].gpio, val);
2621 if (!info->gpio[type].active_high)
2625 return val; /* return read value or error */
2628 static int ov5693_gpio_wr(struct ov5693_info *info,
2629 enum ov5693_gpio_type type,
2630 int val) /* val: 0=deassert, 1=assert */
2634 if (!info->gpio[type].gpio)
2637 if (info->pdata->use_cam_gpio) {
2638 err = cam_gpio_ctrl(info->i2c_client,
2639 info->gpio[type].gpio, val,
2640 info->gpio[type].active_high);
2642 val = info->gpio[type].active_high ? val : !val;
2645 gpio_set_value_cansleep(info->gpio[type].gpio, val);
2646 dev_dbg(&info->i2c_client->dev, "%s %u %d\n", __func__,
2647 info->gpio[type].gpio, val);
2650 return err; /* return value written or error */
2653 static void ov5693_gpio_pwrdn(struct ov5693_info *info, int val)
2657 prev_val = ov5693_gpio_rd(info, OV5693_GPIO_TYPE_PWRDN);
2661 ov5693_gpio_wr(info, OV5693_GPIO_TYPE_PWRDN, val);
2662 if (!val && prev_val)
2663 /* if transition from assert to deassert then delay for I2C */
2667 static void ov5693_gpio_reset(struct ov5693_info *info, int val)
2671 prev_val = ov5693_gpio_rd(info, OV5693_GPIO_TYPE_RESET);
2675 ov5693_gpio_wr(info, OV5693_GPIO_TYPE_RESET, val);
2676 if (!val && prev_val)
2677 /* if transition from assert to deassert then delay for I2C */
2681 static void ov5693_gpio_exit(struct ov5693_info *info)
2685 for (i = 0; i < ARRAY_SIZE(ov5693_gpio); i++) {
2686 if (info->gpio[i].gpio && info->gpio[i].own)
2687 if (info->pdata->use_cam_gpio)
2688 cam_gpio_deregister(info->i2c_client,
2689 info->gpio[i].gpio);
2690 gpio_free(info->gpio[i].gpio);
2694 static void ov5693_gpio_init(struct ov5693_info *info)
2697 unsigned long flags;
2703 if (!info->pdata->gpio_count || !info->pdata->gpio)
2706 for (i = 0; i < ARRAY_SIZE(ov5693_gpio); i++) {
2707 type = ov5693_gpio[i].gpio_type;
2708 for (j = 0; j < info->pdata->gpio_count; j++) {
2709 if (type == info->pdata->gpio[j].gpio_type)
2712 if (j == info->pdata->gpio_count)
2715 info->gpio[type].gpio = info->pdata->gpio[j].gpio;
2716 if (ov5693_gpio[i].use_flags) {
2717 flags = ov5693_gpio[i].flags;
2718 info->gpio[type].active_high =
2719 ov5693_gpio[i].active_high;
2721 info->gpio[type].active_high =
2722 info->pdata->gpio[j].active_high;
2723 if (info->gpio[type].active_high)
2724 flags = GPIOF_OUT_INIT_LOW;
2726 flags = GPIOF_OUT_INIT_HIGH;
2729 if (info->pdata->use_cam_gpio) {
2730 err = cam_gpio_register(
2731 info->i2c_client, info->gpio[type].gpio);
2733 dev_err(&info->i2c_client->dev,
2734 "%s ERR can't register cam gpio %u!\n",
2735 __func__, info->gpio[type].gpio);
2738 if (!info->pdata->gpio[j].init_en)
2741 snprintf(label, sizeof(label), "ov5693_%u_%s",
2742 info->pdata->num, ov5693_gpio[i].label);
2743 err = gpio_request_one(info->gpio[type].gpio, flags, label);
2745 if (type == OV5693_GPIO_TYPE_PWRDN)
2746 dev_err(&info->i2c_client->dev,
2747 "%s ERR %s %u\n", __func__, label,
2748 info->gpio[type].gpio);
2750 dev_dbg(&info->i2c_client->dev,
2751 "%s WARNING %s %u\n", __func__, label,
2752 info->gpio[type].gpio);
2754 info->gpio[type].own = true;
2755 dev_dbg(&info->i2c_client->dev,
2756 "%s %s %u\n", __func__, label,
2757 info->gpio[type].gpio);
2762 static void ov5693_mclk_disable(struct ov5693_info *info)
2764 dev_dbg(&info->i2c_client->dev, "%s: disable MCLK\n", __func__);
2765 clk_disable_unprepare(info->mclk);
2768 static int ov5693_mclk_enable(struct ov5693_info *info)
2771 unsigned long mclk_init_rate = 24000000;
2773 dev_dbg(&info->i2c_client->dev, "%s: enable MCLK with %lu Hz\n",
2774 __func__, mclk_init_rate);
2776 err = clk_set_rate(info->mclk, mclk_init_rate);
2778 err = clk_prepare_enable(info->mclk);
2783 static int ov5693_platform_power_on(struct ov5693_power_rail *pw)
2786 struct ov5693_info *info = container_of(pw, struct ov5693_info,
2789 if (info->pdata->power_on)
2790 return info->pdata->power_on(pw);
2792 if (info->pdata->use_vcm_vdd) {
2793 err = regulator_enable(info->ext_vcm_vdd);
2795 goto ov5693_vcm_fail;
2799 err = regulator_enable(pw->avdd);
2801 goto ov5693_avdd_fail;
2804 err = regulator_enable(pw->dovdd);
2806 goto ov5693_iovdd_fail;
2809 ov5693_gpio_pwrdn(info, 1);
2810 ov5693_gpio_reset(info, 1);
2812 usleep_range(1000, 1110);
2817 regulator_disable(pw->avdd);
2820 if (info->pdata->use_vcm_vdd)
2821 regulator_disable(info->ext_vcm_vdd);
2824 pr_err("%s FAILED\n", __func__);
2828 static int ov5693_platform_power_off(struct ov5693_power_rail *pw)
2830 struct ov5693_info *info = container_of(pw, struct ov5693_info,
2833 if (info->pdata->power_off)
2834 return info->pdata->power_off(pw);
2836 usleep_range(21, 25);
2837 ov5693_gpio_pwrdn(info, 0);
2838 ov5693_gpio_reset(info, 0);
2842 regulator_disable(pw->dovdd);
2844 regulator_disable(pw->avdd);
2845 if (info->pdata->use_vcm_vdd)
2846 regulator_disable(info->ext_vcm_vdd);
2851 static int ov5693_power_off(struct ov5693_info *info)
2853 struct ov5693_power_rail *pw = &info->regulators;
2856 if (false == info->power_on)
2859 err = ov5693_platform_power_off(pw);
2862 info->power_on = false;
2863 ov5693_mclk_disable(info);
2864 sysedp_set_state(info->sysedpc, 0);
2869 static int ov5693_power_on(struct ov5693_info *info, bool standby)
2871 struct ov5693_power_rail *pw = &info->regulators;
2874 if (true == info->power_on)
2877 err = ov5693_mclk_enable(info);
2881 err = ov5693_platform_power_on(pw);
2883 info->power_on = true;
2884 info->pwr_dev = NVC_PWR_ON;
2886 ov5693_mclk_disable(info);
2891 static int ov5693_pm_wr(struct ov5693_info *info, int pwr)
2895 if ((info->pdata->cfg & (NVC_CFG_OFF2STDBY | NVC_CFG_BOOT_INIT)) &&
2896 (pwr == NVC_PWR_OFF ||
2897 pwr == NVC_PWR_STDBY_OFF))
2898 pwr = NVC_PWR_STDBY;
2899 if (pwr == info->pwr_dev)
2903 case NVC_PWR_OFF_FORCE:
2905 case NVC_PWR_STDBY_OFF:
2906 err = ov5693_power_off(info);
2907 info->mode_valid = false;
2912 err = ov5693_power_on(info, true);
2917 err = ov5693_power_on(info, false);
2926 dev_err(&info->i2c_client->dev, "%s err %d\n", __func__, err);
2929 info->pwr_dev = pwr;
2930 dev_dbg(&info->i2c_client->dev, "%s pwr_dev=%d\n",
2931 __func__, info->pwr_dev);
2938 static int ov5693_pm_dev_wr(struct ov5693_info *info, int pwr)
2940 if (info->mode_enable)
2942 if (pwr < info->pwr_api)
2943 pwr = info->pwr_api;
2944 return ov5693_pm_wr(info, pwr);
2947 static void ov5693_pm_exit(struct ov5693_info *info)
2949 ov5693_pm_wr(info, NVC_PWR_OFF_FORCE);
2951 ov5693_gpio_exit(info);
2954 static void ov5693_regulator_get(struct ov5693_info *info,
2955 struct regulator **vreg,
2956 const char vreg_name[])
2958 struct regulator *reg = NULL;
2961 if (vreg_name == NULL)
2964 reg = devm_regulator_get(&info->i2c_client->dev, vreg_name);
2966 dev_err(&info->i2c_client->dev, "%s %s ERR: %p\n",
2967 __func__, vreg_name, reg);
2970 dev_dbg(&info->i2c_client->dev, "%s: %s\n",
2971 __func__, vreg_name);
2977 static void ov5693_pm_init(struct ov5693_info *info)
2979 struct ov5693_power_rail *pw = &info->regulators;
2981 ov5693_gpio_init(info);
2983 ov5693_regulator_get(info, &pw->avdd, info->pdata->regulators.avdd);
2985 ov5693_regulator_get(info, &pw->dvdd, info->pdata->regulators.dvdd);
2987 ov5693_regulator_get(info, &pw->dovdd, info->pdata->regulators.dovdd);
2989 info->power_on = false;
2992 static int ov5693_mode_able(struct ov5693_info *info, bool mode_enable)
3001 err = regmap_write(info->regmap, 0x0100, val);
3003 info->mode_enable = mode_enable;
3004 dev_dbg(&info->i2c_client->dev, "%s streaming=%x\n",
3005 __func__, info->mode_enable);
3007 ov5693_pm_dev_wr(info, NVC_PWR_STDBY);
3012 static int ov5693_mode_wr_full(struct ov5693_info *info, u32 mode_index)
3016 ov5693_pm_dev_wr(info, NVC_PWR_ON);
3017 ov5693_bin_wr(info, 0);
3018 err = regmap_util_write_table_8(info->regmap,
3019 mode_table[mode_index],
3021 OV5693_TABLE_WAIT_MS,
3024 dev_dbg(&info->i2c_client->dev,
3025 "init done(mode=%d)!!!\n", mode_index);
3026 info->mode_index = mode_index;
3027 info->mode_valid = true;
3029 dev_dbg(&info->i2c_client->dev,
3030 "init error(mode=%d)!!!\n", mode_index);
3031 info->mode_valid = false;
3037 static int ov5693_set_mode(struct ov5693_info *info,
3038 struct ov5693_mode *mode)
3043 dev_info(&info->i2c_client->dev,
3044 "[OV5693] sensor %s:%d ++\n", __func__, __LINE__);
3045 dev_info(&info->i2c_client->dev,
3046 "%s: xres %u yres %u framelength %u coarsetime %u gain %u, hdr %d\n",
3047 __func__, mode->res_x, mode->res_y, mode->frame_length,
3048 mode->coarse_time, mode->gain, mode->hdr_en);
3050 if (!mode->res_x && !mode->res_y) {
3051 if (mode->frame_length || mode->coarse_time || mode->gain) {
3052 /* write exposure only */
3053 err = ov5693_exposure_wr(info, mode);
3056 /* turn off streaming */
3057 err = ov5693_mode_able(info, false);
3062 if (mode->hdr_en == true) {
3063 if (mode->res_x == 2592 && mode->res_y == 1944)
3064 mode_index = OV5693_MODE_2592x1944_HDR;
3065 else if (mode->res_x == 1296 && mode->res_y == 972)
3066 mode_index = OV5693_MODE_1296x972_HDR;
3067 else if (mode->res_x == 1920 && mode->res_y == 1080)
3068 mode_index = OV5693_MODE_1920x1080_HDR;
3069 else if (mode->res_x == 1280 && mode->res_y == 720)
3070 mode_index = OV5693_MODE_1280x720_HDR_60FPS;
3072 if (mode->res_x == 2592 && mode->res_y == 1944)
3073 mode_index = OV5693_MODE_2592x1944;
3074 else if (mode->res_x == 1296 && mode->res_y == 972)
3075 mode_index = OV5693_MODE_1296x972;
3076 else if (mode->res_x == 1920 && mode->res_y == 1080)
3077 mode_index = OV5693_MODE_1920x1080;
3078 else if (mode->res_x == 2592 && mode->res_y == 1458)
3079 mode_index = OV5693_MODE_2592x1458;
3080 else if (mode->res_x == 1280 && mode->res_y == 720)
3081 mode_index = OV5693_MODE_1280x720_120FPS;
3084 /* request highest edp state */
3085 sysedp_set_state(info->sysedpc, 1);
3087 if (!info->mode_valid || (info->mode_index != mode_index))
3088 err = ov5693_mode_wr_full(info, mode_index);
3090 dev_dbg(&info->i2c_client->dev, "%s short mode\n", __func__);
3091 dev_dbg(&info->i2c_client->dev, "%s: mode #: %d\n",
3092 __func__, mode_index);
3093 dev_dbg(&info->i2c_client->dev, "%s: AE: %d, %d, %d\n",
3094 __func__, mode->frame_length,
3095 mode->coarse_time, mode->gain);
3096 err |= ov5693_exposure_wr(info, mode);
3098 info->mode_valid = false;
3099 dev_err(&info->i2c_client->dev,
3100 "%s set_mode error\n", __func__);
3101 goto ov5693_mode_wr_err;
3103 err = regmap_write(info->regmap, 0x0100, 0x01);
3105 info->mode_valid = false;
3106 dev_err(&info->i2c_client->dev,
3107 "%s:%d stream on failed\n", __func__, __LINE__);
3108 goto ov5693_mode_wr_err;
3114 if (!info->mode_enable)
3115 ov5693_pm_dev_wr(info, NVC_PWR_OFF);
3119 static int ov5693_get_fuse_id(struct ov5693_info *info)
3121 /* fuse stored at ov5693 bank 0 */
3124 /* if fuse id already cached skip reading again */
3125 if (info->fuseid.size > 0)
3128 err = regmap_write(info->regmap, 0x0100, 0x01);
3130 dev_err(&info->i2c_client->dev,
3131 "%s ERR %d: cannot write stream mode\n", __func__,
3135 usleep_range(10000, 11000);
3137 err = regmap_write(info->regmap, 0x3D84, 0xC0);
3139 dev_err(&info->i2c_client->dev,
3140 "%s ERR %d: cannot write bank index 0\n", __func__,
3144 usleep_range(10000, 11000);
3146 err = regmap_write(info->regmap, 0x3D81, 0x01);
3148 dev_err(&info->i2c_client->dev,
3149 "%s ERR %d: cannot load OTP data\n", __func__, err);
3152 usleep_range(10000, 11000);
3154 err = regmap_bulk_read(info->regmap, 0x3D00,
3155 info->fuseid.data, OV5693_FUSE_ID_SIZE);
3157 dev_err(&info->i2c_client->dev,
3158 "%s ERR %d: cannot read OTP buffer\n", __func__,
3163 info->fuseid.size = OV5693_FUSE_ID_SIZE;
3168 static int ov5693_read_otp_bank(struct ov5693_info *info,
3169 struct ov5693_otp_bank *bank)
3173 err = regmap_write(info->regmap, 0x0100, 0x01);
3175 dev_err(&info->i2c_client->dev,
3176 "%s ERR %d: cannot write stream mode\n", __func__,
3180 err = regmap_write(info->regmap, 0x3d84, 0xc0 | bank->id);
3182 dev_err(&info->i2c_client->dev,
3183 "%s ERR %d: cannot write bank index %d\n", __func__,
3187 err = regmap_write(info->regmap, 0x3d81, 0x01);
3189 dev_err(&info->i2c_client->dev,
3190 "%s ERR %d: cannot load OTP data\n", __func__, err);
3193 usleep_range(1000, 11000);
3194 err = regmap_bulk_read(info->regmap, 0x3d00, bank->buf, 16);
3196 dev_err(&info->i2c_client->dev,
3197 "%s ERR %d: cannot read OTP buffer\n", __func__,
3206 ov5693_eeprom_device_release(struct ov5693_info *info)
3210 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
3211 if (info->eeprom[i].i2c_client != NULL) {
3212 i2c_unregister_device(info->eeprom[i].i2c_client);
3213 info->eeprom[i].i2c_client = NULL;
3221 ov5693_eeprom_device_init(struct ov5693_info *info)
3223 char *dev_name = "eeprom_ov5693";
3224 static struct regmap_config eeprom_regmap_config = {
3231 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
3232 info->eeprom[i].adap = i2c_get_adapter(
3233 info->i2c_client->adapter->nr);
3234 memset(&info->eeprom[i].brd, 0, sizeof(info->eeprom[i].brd));
3235 strncpy(info->eeprom[i].brd.type, dev_name,
3236 sizeof(info->eeprom[i].brd.type));
3237 info->eeprom[i].brd.addr = OV5693_EEPROM_ADDRESS + i;
3238 info->eeprom[i].i2c_client = i2c_new_device(
3239 info->eeprom[i].adap, &info->eeprom[i].brd);
3241 info->eeprom[i].regmap = devm_regmap_init_i2c(
3242 info->eeprom[i].i2c_client, &eeprom_regmap_config);
3243 if (IS_ERR(info->eeprom[i].regmap)) {
3244 err = PTR_ERR(info->eeprom[i].regmap);
3245 ov5693_eeprom_device_release(info);
3254 ov5693_read_eeprom(struct ov5693_info *info, u8 reg, u16 length, u8 *buf)
3256 return regmap_raw_read(info->eeprom[0].regmap, reg, &buf[reg], length);
3260 ov5693_write_eeprom(struct ov5693_info *info, u16 addr, u8 val)
3262 return regmap_write(info->eeprom[addr >> 8].regmap, addr & 0xFF, val);
3265 static long ov5693_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3267 struct ov5693_info *info = file->private_data;
3270 switch (_IOC_NR(cmd)) {
3271 case _IOC_NR(OV5693_IOCTL_SET_POWER):
3273 u32 powerlevel = (u32) arg;
3275 if (powerlevel > NVC_PWR_ON) {
3276 dev_err(&info->i2c_client->dev,
3277 "%s:Invalid power level.\n",
3282 err = ov5693_pm_wr(info, powerlevel);
3285 case _IOC_NR(OV5693_IOCTL_GET_CAPS):
3286 if (copy_to_user((void __user *)arg,
3288 sizeof(struct nvc_imager_cap))) {
3289 dev_err(&info->i2c_client->dev,
3290 "%s copy_to_user err line %d\n",
3291 __func__, __LINE__);
3295 case _IOC_NR(OV5693_IOCTL_SET_MODE):
3297 struct ov5693_mode mode;
3298 if (copy_from_user(&mode,
3299 (const void __user *)arg,
3300 sizeof(struct ov5693_mode))) {
3301 dev_err(&info->i2c_client->dev,
3302 "%s:Failed to get mode from user.\n",
3306 return ov5693_set_mode(info, &mode);
3308 case _IOC_NR(OV5693_IOCTL_GET_STATUS): {
3310 if (copy_to_user((void __user *)arg, &status, sizeof(status))) {
3311 dev_err(&info->i2c_client->dev,
3312 "%s:Failed to copy status to user.\n",
3319 case _IOC_NR(OV5693_IOCTL_SET_GROUP_HOLD): {
3320 struct ov5693_ae ae;
3321 if (copy_from_user(&ae, (const void __user *)arg,
3322 sizeof(struct ov5693_ae))) {
3323 dev_dbg(&info->i2c_client->dev,
3324 "%s:fail group hold\n", __func__);
3328 return ov5693_set_group_hold(info, &ae);
3331 case _IOC_NR(OV5693_IOCTL_SET_FRAME_LENGTH):
3332 return ov5693_set_frame_length(info, (u32)arg, true);
3334 case _IOC_NR(OV5693_IOCTL_SET_COARSE_TIME):
3335 return ov5693_set_coarse_time(info, (u32)arg,
3336 OV5693_INVALID_COARSE_TIME, true);
3338 case _IOC_NR(OV5693_IOCTL_SET_HDR_COARSE_TIME):
3340 struct ov5693_hdr *hdrcoarse = (struct ov5693_hdr *)arg;
3341 int ret = ov5693_set_coarse_time(info,
3342 hdrcoarse->coarse_time_long,
3343 hdrcoarse->coarse_time_short,
3348 case _IOC_NR(OV5693_IOCTL_SET_GAIN):
3349 return ov5693_set_gain(info, (u32)arg, true);
3351 case _IOC_NR(OV5693_IOCTL_GET_FUSEID):
3353 err = ov5693_get_fuse_id(info);
3356 dev_err(&info->i2c_client->dev, "%s:Failed to get fuse id info.\n",
3360 if (copy_to_user((void __user *)arg,
3362 sizeof(struct nvc_fuseid))) {
3363 dev_dbg(&info->i2c_client->dev, "%s:Fail copy fuse id to user space\n",
3370 case _IOC_NR(OV5693_IOCTL_READ_OTP_BANK):
3372 struct ov5693_otp_bank bank;
3373 if (copy_from_user(&bank,
3374 (const void __user *)arg,
3376 dev_err(&info->i2c_client->dev,
3377 "%s %d copy_from_user err\n",
3378 __func__, __LINE__);
3382 err = ov5693_read_otp_bank(info, &bank);
3386 if (copy_to_user((void __user *)arg,
3389 dev_err(&info->i2c_client->dev,
3390 "%s %d copy_to_user err\n",
3391 __func__, __LINE__);
3397 case _IOC_NR(OV5693_IOCTL_SET_CAL_DATA):
3399 if (copy_from_user(&info->cal, (const void __user *)arg,
3400 sizeof(info->cal))) {
3401 dev_err(&info->i2c_client->dev,
3402 "%s %d copy_from_user err\n",
3403 __func__, __LINE__);
3409 case _IOC_NR(OV5693_IOCTL_GET_EEPROM_DATA):
3411 if (!info->pdata->has_eeprom)
3414 ov5693_read_eeprom(info,
3419 if (copy_to_user((void __user *)arg,
3420 info->eeprom_buf, OV5693_EEPROM_SIZE)) {
3421 dev_err(&info->i2c_client->dev,
3422 "%s:Failed to copy status to user\n",
3429 case _IOC_NR(OV5693_IOCTL_SET_EEPROM_DATA):
3433 if (!info->pdata->has_eeprom)
3436 if (copy_from_user(info->eeprom_buf,
3437 (const void __user *)arg, OV5693_EEPROM_SIZE)) {
3438 dev_err(&info->i2c_client->dev,
3439 "%s:Failed to read from user buffer\n",
3443 for (i = 0; i < OV5693_EEPROM_SIZE; i++) {
3444 ov5693_write_eeprom(info,
3446 info->eeprom_buf[i]);
3453 dev_err(&info->i2c_client->dev, "%s unsupported ioctl: %x\n",
3459 static void ov5693_sdata_init(struct ov5693_info *info)
3461 struct nvc_imager_static_nvc *static_info;
3463 memcpy(&info->sdata, &ov5693_dflt_sdata, sizeof(info->sdata));
3464 if (!info->pdata->static_info)
3467 static_info = info->pdata->static_info;
3468 if (static_info->focal_len)
3469 info->sdata.focal_len = static_info->focal_len;
3470 if (static_info->max_aperture)
3471 info->sdata.max_aperture = static_info->max_aperture;
3472 if (static_info->fnumber)
3473 info->sdata.fnumber = static_info->fnumber;
3474 if (static_info->view_angle_h)
3475 info->sdata.view_angle_h = static_info->view_angle_h;
3476 if (static_info->view_angle_v)
3477 info->sdata.view_angle_v = static_info->view_angle_v;
3480 static int ov5693_open(struct inode *inode, struct file *file)
3483 struct miscdevice *miscdev = file->private_data;
3484 struct ov5693_info *info = dev_get_drvdata(miscdev->parent);
3486 if (atomic_xchg(&info->in_use, 1))
3489 file->private_data = info;
3490 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3492 err = ov5693_power_on(info, false);
3496 static int ov5693_release(struct inode *inode, struct file *file)
3498 struct ov5693_info *info = file->private_data;
3500 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3501 ov5693_pm_wr(info, NVC_PWR_OFF);
3502 file->private_data = NULL;
3503 WARN_ON(!atomic_xchg(&info->in_use, 0));
3507 static const struct file_operations ov5693_fileops = {
3508 .owner = THIS_MODULE,
3509 .open = ov5693_open,
3510 .unlocked_ioctl = ov5693_ioctl,
3511 #ifdef CONFIG_COMPAT
3512 .compat_ioctl = ov5693_ioctl,
3514 .release = ov5693_release,
3517 static void ov5693_del(struct ov5693_info *info)
3519 ov5693_pm_exit(info);
3523 static int ov5693_remove(struct i2c_client *client)
3525 struct ov5693_info *info = i2c_get_clientdata(client);
3527 dev_dbg(&info->i2c_client->dev, "%s\n", __func__);
3528 misc_deregister(&info->miscdev);
3529 sysedp_free_consumer(info->sysedpc);
3530 if (info->pdata->has_eeprom)
3531 ov5693_eeprom_device_release(info);
3536 static struct of_device_id ov5693_of_match[] = {
3537 { .compatible = "nvidia,ov5693", },
3541 MODULE_DEVICE_TABLE(of, ov5693_of_match);
3543 static int ov5693_parse_dt_gpio(struct device_node *np, const char *name,
3544 enum ov5693_gpio_type type,
3545 struct nvc_gpio_pdata *pdata)
3547 enum of_gpio_flags gpio_flags;
3549 if (of_find_property(np, name, NULL)) {
3550 pdata->gpio = of_get_named_gpio_flags(np, name, 0, &gpio_flags);
3551 pdata->gpio_type = type;
3552 pdata->init_en = true;
3553 pdata->active_high = !(gpio_flags & OF_GPIO_ACTIVE_LOW);
3559 static struct ov5693_platform_data *ov5693_parse_dt(struct i2c_client *client)
3561 struct device_node *np = client->dev.of_node;
3562 struct ov5693_platform_data *pdata;
3563 struct nvc_gpio_pdata *gpio_pdata = NULL;
3568 dev_dbg(&client->dev, "%s: %s\n", __func__, np->full_name);
3569 num = sizeof(*pdata) + sizeof(*pdata->cap) + sizeof(*pdata->static_info)
3570 + sizeof(*gpio_pdata) * ARRAY_SIZE(ov5693_gpio);
3571 pdata = devm_kzalloc(&client->dev, num, GFP_KERNEL);
3573 dev_err(&client->dev, "Failed to allocate pdata\n");
3574 return ERR_PTR(-ENOMEM);
3577 /* init with default platform data values in board file or driver */
3578 if (client->dev.platform_data)
3579 memcpy(pdata, client->dev.platform_data, sizeof(*pdata));
3581 memcpy(pdata, &ov5693_dflt_pdata, sizeof(*pdata));
3584 memcpy((void *)(pdata + 1), pdata->cap, sizeof(*pdata->cap));
3586 pdata->cap = (void *)(pdata + 1);
3587 pdata->static_info = (void *)(pdata->cap + 1);
3588 gpio_pdata = (void *)(pdata->static_info + 1);
3592 ret = of_property_read_string_index(
3593 np, "regulators", num, &sname);
3598 pdata->regulators.avdd = sname;
3599 pdata->regulators.dvdd = NULL;
3600 pdata->regulators.dovdd = NULL;
3603 pdata->regulators.dovdd = sname;
3606 pdata->regulators.dvdd = sname;
3614 /* extra regulators info */
3615 pdata->use_vcm_vdd = of_property_read_bool(np, "use-vcm-vdd");
3618 of_property_read_string(np, "dev_name", &pdata->dev_name);
3619 of_property_read_u32(np, "num", &pdata->num);
3620 if (of_property_read_bool(np, "has-eeprom"))
3621 pdata->has_eeprom = true;
3622 if (of_property_read_bool(np, "off-to-standby"))
3623 pdata->cfg |= NVC_CFG_OFF2STDBY;
3624 if (of_property_read_bool(np, "boot-init"))
3625 pdata->cfg |= NVC_CFG_BOOT_INIT;
3626 if (of_property_read_bool(np, "nodev-check"))
3627 pdata->cfg |= NVC_CFG_NODEV;
3630 pdata->gpio_count = 0;
3631 pdata->gpio_count += ov5693_parse_dt_gpio(np,
3632 "cam2-gpios", OV5693_GPIO_TYPE_PWRDN,
3633 &gpio_pdata[pdata->gpio_count]);
3634 pdata->gpio_count += ov5693_parse_dt_gpio(np,
3635 "reset-gpios", OV5693_GPIO_TYPE_RESET,
3636 &gpio_pdata[pdata->gpio_count]);
3637 pdata->gpio = gpio_pdata;
3639 pdata->use_cam_gpio = of_property_read_bool(np, "cam,use-cam-gpio");
3641 /* MCLK clock info */
3642 of_property_read_string(np, "clocks", &pdata->mclk_name);
3645 nvc_imager_parse_caps(np, pdata->cap, pdata->static_info);
3650 static int ov5693_probe(
3651 struct i2c_client *client,
3652 const struct i2c_device_id *id)
3654 struct ov5693_info *info;
3655 unsigned long clock_probe_rate;
3657 const char *mclk_name;
3658 static struct regmap_config ov5693_regmap_config = {
3664 dev_dbg(&client->dev, "%s\n", __func__);
3665 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
3667 dev_err(&client->dev, "%s: kzalloc error\n", __func__);
3671 info->i2c_client = client;
3672 if (client->dev.of_node) {
3673 info->pdata = ov5693_parse_dt(client);
3674 if (IS_ERR(info->pdata)) {
3675 err = PTR_ERR(info->pdata);
3676 dev_err(&client->dev,
3677 "Failed to parse OF node: %d\n", err);
3680 } else if (client->dev.platform_data)
3681 info->pdata = client->dev.platform_data;
3683 info->pdata = &ov5693_dflt_pdata;
3684 dev_dbg(&client->dev,
3685 "%s No platform data. Using defaults.\n",
3688 if (info->pdata->use_vcm_vdd) {
3689 info->ext_vcm_vdd = devm_regulator_get(&info->i2c_client->dev,
3691 if (WARN_ON(IS_ERR(info->ext_vcm_vdd))) {
3692 err = PTR_ERR(info->ext_vcm_vdd);
3693 dev_err(&client->dev,
3694 "ext_vcm_vdd get failed %d\n", err);
3695 info->ext_vcm_vdd = NULL;
3700 info->regmap = devm_regmap_init_i2c(client, &ov5693_regmap_config);
3701 if (IS_ERR(info->regmap)) {
3702 err = PTR_ERR(info->regmap);
3703 dev_err(&client->dev,
3704 "Failed to allocate register map: %d\n", err);
3708 if (info->pdata->has_eeprom) {
3709 err = ov5693_eeprom_device_init(info);
3711 dev_err(&client->dev,
3712 "Failed to allocate eeprom register map: %d\n", err);
3717 mclk_name = info->pdata->mclk_name ?
3718 info->pdata->mclk_name : "default_mclk";
3719 info->mclk = devm_clk_get(&client->dev, mclk_name);
3720 if (IS_ERR(info->mclk)) {
3721 dev_err(&client->dev, "%s: unable to get clock %s\n",
3722 __func__, mclk_name);
3723 return PTR_ERR(info->mclk);
3726 i2c_set_clientdata(client, info);
3727 ov5693_pm_init(info);
3728 ov5693_sdata_init(info);
3730 if (info->pdata->cfg & (NVC_CFG_NODEV | NVC_CFG_BOOT_INIT)) {
3731 if (info->pdata->probe_clock) {
3732 clock_probe_rate = 6000; /* initial_clcok*/
3733 clock_probe_rate *= 1000;
3734 info->pdata->probe_clock(clock_probe_rate);
3736 ov5693_pm_dev_wr(info, NVC_PWR_COMM);
3737 ov5693_pm_dev_wr(info, NVC_PWR_OFF);
3738 if (info->pdata->probe_clock)
3739 info->pdata->probe_clock(0);
3741 if (info->pdata->dev_name != NULL)
3742 strncpy(info->devname, info->pdata->dev_name,
3743 sizeof(info->devname) - 1);
3745 strncpy(info->devname, "ov5693", sizeof(info->devname) - 1);
3746 if (info->pdata->num)
3747 snprintf(info->devname, sizeof(info->devname), "%s.%u",
3748 info->devname, info->pdata->num);
3750 info->miscdev.name = info->devname;
3751 info->miscdev.fops = &ov5693_fileops;
3752 info->miscdev.minor = MISC_DYNAMIC_MINOR;
3753 info->miscdev.parent = &client->dev;
3754 if (misc_register(&info->miscdev)) {
3755 dev_err(&client->dev, "%s unable to register misc device %s\n",
3756 __func__, info->devname);
3761 info->sysedpc = sysedp_create_consumer("ov5693", info->devname);
3763 dev_dbg(&client->dev, "ov5693 sensor driver loading done\n");
3767 static const struct i2c_device_id ov5693_id[] = {
3773 MODULE_DEVICE_TABLE(i2c, ov5693_id);
3775 static struct i2c_driver ov5693_i2c_driver = {
3778 .owner = THIS_MODULE,
3779 .of_match_table = ov5693_of_match,
3781 .id_table = ov5693_id,
3782 .probe = ov5693_probe,
3783 .remove = ov5693_remove,
3786 module_i2c_driver(ov5693_i2c_driver);
3787 MODULE_LICENSE("GPL v2");