2 * ov5693_v4l2.c - ov5693 sensor driver
4 * Copyright (c) 2013-2017, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/gpio.h>
22 #include <linux/module.h>
24 #include <linux/seq_file.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
29 #include <media/camera_common.h>
30 #include <media/ov5693.h>
32 #include "../platform/tegra/camera/camera_gpio.h"
34 #include "ov5693_mode_tbls.h"
36 #define OV5693_MAX_COARSE_DIFF 6
38 #define OV5693_GAIN_SHIFT 8
39 #define OV5693_REAL_GAIN_SHIFT 4
40 #define OV5693_MIN_GAIN (1 << OV5693_GAIN_SHIFT)
41 #define OV5693_MAX_GAIN (16 << OV5693_GAIN_SHIFT)
42 #define OV5693_MAX_UNREAL_GAIN (0x0F80)
43 #define OV5693_MIN_FRAME_LENGTH (0x0)
44 #define OV5693_MAX_FRAME_LENGTH (0x7fff)
45 #define OV5693_MIN_EXPOSURE_COARSE (0x0002)
46 #define OV5693_MAX_EXPOSURE_COARSE \
47 (OV5693_MAX_FRAME_LENGTH-OV5693_MAX_COARSE_DIFF)
48 #define OV5693_DEFAULT_LINE_LENGTH (0xA80)
49 #define OV5693_DEFAULT_PIXEL_CLOCK (160)
51 #define OV5693_DEFAULT_GAIN OV5693_MIN_GAIN
52 #define OV5693_DEFAULT_FRAME_LENGTH (0x07C0)
53 #define OV5693_DEFAULT_EXPOSURE_COARSE \
54 (OV5693_DEFAULT_FRAME_LENGTH-OV5693_MAX_COARSE_DIFF)
56 #define OV5693_DEFAULT_MODE OV5693_MODE_2592X1944
57 #define OV5693_DEFAULT_HDR_MODE OV5693_MODE_2592X1944_HDR
58 #define OV5693_DEFAULT_WIDTH 2592
59 #define OV5693_DEFAULT_HEIGHT 1944
60 #define OV5693_DEFAULT_DATAFMT MEDIA_BUS_FMT_SRGGB10_1X10
61 #define OV5693_DEFAULT_CLK_FREQ 24000000
64 struct camera_common_power_rail power;
66 struct v4l2_ctrl_handler ctrl_handler;
67 struct camera_common_eeprom_data eeprom[OV5693_EEPROM_NUM_BLOCKS];
68 u8 eeprom_buf[OV5693_EEPROM_SIZE];
69 struct i2c_client *i2c_client;
70 struct v4l2_subdev *subdev;
78 struct regmap *regmap;
79 struct camera_common_data *s_data;
80 struct camera_common_pdata *pdata;
81 struct v4l2_ctrl *ctrls[];
84 static struct regmap_config ov5693_regmap_config = {
89 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
90 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl);
91 static void ov5693_update_ctrl_range(struct ov5693 *priv, s32 frame_length);
93 static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
94 .g_volatile_ctrl = ov5693_g_volatile_ctrl,
95 .s_ctrl = ov5693_s_ctrl,
98 static struct v4l2_ctrl_config ctrl_config_list[] = {
99 /* Do not change the name field for the controls! */
101 .ops = &ov5693_ctrl_ops,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .flags = V4L2_CTRL_FLAG_SLIDER,
106 .min = OV5693_MIN_GAIN,
107 .max = OV5693_MAX_GAIN,
108 .def = OV5693_DEFAULT_GAIN,
112 .ops = &ov5693_ctrl_ops,
113 .id = V4L2_CID_FRAME_LENGTH,
114 .name = "Frame Length",
115 .type = V4L2_CTRL_TYPE_INTEGER,
116 .flags = V4L2_CTRL_FLAG_SLIDER,
117 .min = OV5693_MIN_FRAME_LENGTH,
118 .max = OV5693_MAX_FRAME_LENGTH,
119 .def = OV5693_DEFAULT_FRAME_LENGTH,
123 .ops = &ov5693_ctrl_ops,
124 .id = V4L2_CID_COARSE_TIME,
125 .name = "Coarse Time",
126 .type = V4L2_CTRL_TYPE_INTEGER,
127 .flags = V4L2_CTRL_FLAG_SLIDER,
128 .min = OV5693_MIN_EXPOSURE_COARSE,
129 .max = OV5693_MAX_EXPOSURE_COARSE,
130 .def = OV5693_DEFAULT_EXPOSURE_COARSE,
134 .ops = &ov5693_ctrl_ops,
135 .id = V4L2_CID_COARSE_TIME_SHORT,
136 .name = "Coarse Time Short",
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .flags = V4L2_CTRL_FLAG_SLIDER,
139 .min = OV5693_MIN_EXPOSURE_COARSE,
140 .max = OV5693_MAX_EXPOSURE_COARSE,
141 .def = OV5693_DEFAULT_EXPOSURE_COARSE,
145 .ops = &ov5693_ctrl_ops,
146 .id = V4L2_CID_GROUP_HOLD,
147 .name = "Group Hold",
148 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
150 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
153 .qmenu_int = switch_ctrl_qmenu,
156 .ops = &ov5693_ctrl_ops,
157 .id = V4L2_CID_HDR_EN,
158 .name = "HDR enable",
159 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
161 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
164 .qmenu_int = switch_ctrl_qmenu,
167 .ops = &ov5693_ctrl_ops,
168 .id = V4L2_CID_EEPROM_DATA,
169 .name = "EEPROM Data",
170 .type = V4L2_CTRL_TYPE_STRING,
171 .flags = V4L2_CTRL_FLAG_VOLATILE,
173 .max = OV5693_EEPROM_STR_SIZE,
177 .ops = &ov5693_ctrl_ops,
178 .id = V4L2_CID_OTP_DATA,
180 .type = V4L2_CTRL_TYPE_STRING,
181 .flags = V4L2_CTRL_FLAG_READ_ONLY,
183 .max = OV5693_OTP_STR_SIZE,
187 .ops = &ov5693_ctrl_ops,
188 .id = V4L2_CID_FUSE_ID,
190 .type = V4L2_CTRL_TYPE_STRING,
191 .flags = V4L2_CTRL_FLAG_READ_ONLY,
193 .max = OV5693_FUSE_ID_STR_SIZE,
198 static inline void ov5693_get_frame_length_regs(ov5693_reg *regs,
201 regs->addr = OV5693_FRAME_LENGTH_ADDR_MSB;
202 regs->val = (frame_length >> 8) & 0xff;
203 (regs + 1)->addr = OV5693_FRAME_LENGTH_ADDR_LSB;
204 (regs + 1)->val = (frame_length) & 0xff;
208 static inline void ov5693_get_coarse_time_regs(ov5693_reg *regs,
211 regs->addr = OV5693_COARSE_TIME_ADDR_1;
212 regs->val = (coarse_time >> 12) & 0xff;
213 (regs + 1)->addr = OV5693_COARSE_TIME_ADDR_2;
214 (regs + 1)->val = (coarse_time >> 4) & 0xff;
215 (regs + 2)->addr = OV5693_COARSE_TIME_ADDR_3;
216 (regs + 2)->val = (coarse_time & 0xf) << 4;
219 static inline void ov5693_get_coarse_time_short_regs(ov5693_reg *regs,
222 regs->addr = OV5693_COARSE_TIME_SHORT_ADDR_1;
223 regs->val = (coarse_time >> 12) & 0xff;
224 (regs + 1)->addr = OV5693_COARSE_TIME_SHORT_ADDR_2;
225 (regs + 1)->val = (coarse_time >> 4) & 0xff;
226 (regs + 2)->addr = OV5693_COARSE_TIME_SHORT_ADDR_3;
227 (regs + 2)->val = (coarse_time & 0xf) << 4;
230 static inline void ov5693_get_gain_regs(ov5693_reg *regs,
233 regs->addr = OV5693_GAIN_ADDR_MSB;
234 regs->val = (gain >> 8) & 0xff;
236 (regs + 1)->addr = OV5693_GAIN_ADDR_LSB;
237 (regs + 1)->val = (gain) & 0xff;
240 static int test_mode;
241 module_param(test_mode, int, 0644);
243 static inline int ov5693_read_reg(struct camera_common_data *s_data,
246 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
250 err = regmap_read(priv->regmap, addr, ®_val);
251 *val = reg_val & 0xFF;
256 static int ov5693_write_reg(struct camera_common_data *s_data, u16 addr, u8 val)
259 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
261 err = regmap_write(priv->regmap, addr, val);
263 pr_err("%s:i2c write failed, %x = %x\n",
264 __func__, addr, val);
269 static int ov5693_write_table(struct ov5693 *priv,
270 const ov5693_reg table[])
272 return regmap_util_write_table_8(priv->regmap,
275 OV5693_TABLE_WAIT_MS,
279 static void ov5693_gpio_set(struct ov5693 *priv,
280 unsigned int gpio, int val)
282 if (priv->pdata && priv->pdata->use_cam_gpio)
283 cam_gpio_ctrl(priv->i2c_client, gpio, val, 1);
285 if (gpio_cansleep(gpio))
286 gpio_set_value_cansleep(gpio, val);
288 gpio_set_value(gpio, val);
292 static int ov5693_power_on(struct camera_common_data *s_data)
295 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
296 struct camera_common_power_rail *pw = &priv->power;
298 dev_dbg(&priv->i2c_client->dev, "%s: power on\n", __func__);
300 if (priv->pdata && priv->pdata->power_on) {
301 err = priv->pdata->power_on(pw);
303 pr_err("%s failed.\n", __func__);
305 pw->state = SWITCH_ON;
309 /* sleeps calls in the sequence below are for internal device
310 * signal propagation as specified by sensor vendor */
313 err = regulator_enable(pw->avdd);
315 goto ov5693_avdd_fail;
318 err = regulator_enable(pw->iovdd);
320 goto ov5693_iovdd_fail;
324 ov5693_gpio_set(priv, pw->pwdn_gpio, 1);
327 * datasheet 2.9: reset requires ~2ms settling time
328 * a power on reset is generated after core power becomes stable
330 usleep_range(2000, 2010);
333 ov5693_gpio_set(priv, pw->reset_gpio, 1);
335 /* datasheet fig 2-9: t3 */
336 usleep_range(2000, 2010);
338 pw->state = SWITCH_ON;
339 ov5693_write_reg(s_data, 0x0100, 0x1);
340 ov5693_write_reg(s_data, 0x0100, 0x0);
344 regulator_disable(pw->avdd);
347 pr_err("%s failed.\n", __func__);
351 static int ov5693_power_off(struct camera_common_data *s_data)
354 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
355 struct camera_common_power_rail *pw = &priv->power;
357 dev_dbg(&priv->i2c_client->dev, "%s: power off\n", __func__);
359 if (priv->pdata && priv->pdata->power_off) {
360 err = priv->pdata->power_off(pw);
364 pr_err("%s failed.\n", __func__);
369 /* sleeps calls in the sequence below are for internal device
370 * signal propagation as specified by sensor vendor */
372 usleep_range(21, 25);
374 ov5693_gpio_set(priv, pw->pwdn_gpio, 0);
377 ov5693_gpio_set(priv, pw->reset_gpio, 0);
379 /* datasheet 2.9: reset requires ~2ms settling time*/
380 usleep_range(2000, 2010);
383 regulator_disable(pw->iovdd);
385 regulator_disable(pw->avdd);
388 pw->state = SWITCH_OFF;
392 static int ov5693_power_put(struct ov5693 *priv)
394 struct camera_common_power_rail *pw = &priv->power;
399 if (likely(pw->avdd))
400 regulator_put(pw->avdd);
402 if (likely(pw->iovdd))
403 regulator_put(pw->iovdd);
408 if (priv->pdata && priv->pdata->use_cam_gpio)
409 cam_gpio_deregister(priv->i2c_client, pw->pwdn_gpio);
411 gpio_free(pw->pwdn_gpio);
412 gpio_free(pw->reset_gpio);
418 static int ov5693_power_get(struct ov5693 *priv)
420 struct camera_common_power_rail *pw = &priv->power;
421 struct camera_common_pdata *pdata = priv->pdata;
422 const char *mclk_name;
423 const char *parentclk_name;
425 int err = 0, ret = 0;
428 dev_err(&priv->i2c_client->dev, "pdata missing\n");
432 mclk_name = pdata->mclk_name ?
433 pdata->mclk_name : "cam_mclk1";
434 pw->mclk = devm_clk_get(&priv->i2c_client->dev, mclk_name);
435 if (IS_ERR(pw->mclk)) {
436 dev_err(&priv->i2c_client->dev,
437 "unable to get clock %s\n", mclk_name);
438 return PTR_ERR(pw->mclk);
441 parentclk_name = pdata->parentclk_name;
442 if (parentclk_name) {
443 parent = devm_clk_get(&priv->i2c_client->dev, parentclk_name);
444 if (IS_ERR(parent)) {
445 dev_err(&priv->i2c_client->dev,
446 "unable to get parent clcok %s",
449 clk_set_parent(pw->mclk, parent);
454 err |= camera_common_regulator_get(priv->i2c_client,
455 &pw->avdd, pdata->regulators.avdd);
457 err |= camera_common_regulator_get(priv->i2c_client,
458 &pw->iovdd, pdata->regulators.iovdd);
461 pw->reset_gpio = pdata->reset_gpio;
462 pw->pwdn_gpio = pdata->pwdn_gpio;
465 if (pdata->use_cam_gpio) {
466 err = cam_gpio_register(priv->i2c_client, pw->pwdn_gpio);
468 dev_err(&priv->i2c_client->dev,
469 "%s ERR can't register cam gpio %u!\n",
470 __func__, pw->pwdn_gpio);
472 ret = gpio_request(pw->pwdn_gpio, "cam_pwdn_gpio");
474 dev_dbg(&priv->i2c_client->dev,
475 "%s can't request pwdn_gpio %d\n",
477 ret = gpio_request(pw->reset_gpio, "cam_reset_gpio");
479 dev_dbg(&priv->i2c_client->dev,
480 "%s can't request reset_gpio %d\n",
485 pw->state = SWITCH_OFF;
489 static int ov5693_set_gain(struct ov5693 *priv, s32 val);
490 static int ov5693_set_frame_length(struct ov5693 *priv, s32 val);
491 static int ov5693_set_coarse_time(struct ov5693 *priv, s32 val);
492 static int ov5693_set_coarse_time_short(struct ov5693 *priv, s32 val);
494 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
496 struct i2c_client *client = v4l2_get_subdevdata(sd);
497 struct camera_common_data *s_data = to_camera_common_data(client);
498 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
499 struct v4l2_control control;
504 dev_dbg(&client->dev, "%s++\n", __func__);
507 ov5693_update_ctrl_range(priv, OV5693_MAX_FRAME_LENGTH);
509 err = ov5693_write_table(priv,
510 mode_table[OV5693_MODE_STOP_STREAM]);
515 * Wait for one frame to make sure sensor is set to
516 * software standby in V-blank
518 * frame_time = frame length rows * Tline
519 * Tline = line length / pixel clock (in MHz)
521 frame_time = priv->frame_length *
522 OV5693_DEFAULT_LINE_LENGTH / OV5693_DEFAULT_PIXEL_CLOCK;
524 usleep_range(frame_time, frame_time + 1000);
528 err = ov5693_write_table(priv, mode_table[s_data->mode]);
533 if (s_data->override_enable) {
535 * write list of override regs for the asking frame length,
536 * coarse integration time, and gain. Failures to write
537 * overrides are non-fatal
539 control.id = V4L2_CID_GAIN;
540 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
541 err |= ov5693_set_gain(priv, control.value);
543 dev_dbg(&client->dev, "%s: warning gain override failed\n",
546 control.id = V4L2_CID_FRAME_LENGTH;
547 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
548 err |= ov5693_set_frame_length(priv, control.value);
550 dev_dbg(&client->dev,
551 "%s: warning frame length override failed\n",
554 control.id = V4L2_CID_COARSE_TIME;
555 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
556 err |= ov5693_set_coarse_time(priv, control.value);
558 dev_dbg(&client->dev,
559 "%s: warning coarse time override failed\n",
562 control.id = V4L2_CID_COARSE_TIME_SHORT;
563 err = v4l2_g_ctrl(&priv->ctrl_handler, &control);
564 err |= ov5693_set_coarse_time_short(priv, control.value);
566 dev_dbg(&client->dev,
567 "%s: warning coarse time short override failed\n",
571 err = ov5693_write_table(priv, mode_table[OV5693_MODE_START_STREAM]);
574 if (priv->pdata->v_flip) {
575 ov5693_read_reg(priv->s_data, OV5693_TIMING_REG20, &val);
576 ov5693_write_reg(priv->s_data, OV5693_TIMING_REG20,
577 val | VERTICAL_FLIP);
579 if (priv->pdata->h_mirror) {
580 ov5693_read_reg(priv->s_data, OV5693_TIMING_REG21, &val);
581 ov5693_write_reg(priv->s_data, OV5693_TIMING_REG21,
582 val | HORIZONTAL_MIRROR_MASK);
584 ov5693_read_reg(priv->s_data, OV5693_TIMING_REG21, &val);
585 ov5693_write_reg(priv->s_data, OV5693_TIMING_REG21,
586 val & (~HORIZONTAL_MIRROR_MASK));
589 err = ov5693_write_table(priv,
590 mode_table[OV5693_MODE_TEST_PATTERN]);
592 dev_dbg(&client->dev, "%s--\n", __func__);
595 dev_dbg(&client->dev, "%s: error setting stream\n", __func__);
599 static int ov5693_g_input_status(struct v4l2_subdev *sd, u32 *status)
601 struct i2c_client *client = v4l2_get_subdevdata(sd);
602 struct camera_common_data *s_data = to_camera_common_data(client);
603 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
604 struct camera_common_power_rail *pw = &priv->power;
606 *status = pw->state == SWITCH_ON;
610 static struct v4l2_subdev_video_ops ov5693_subdev_video_ops = {
611 .s_stream = ov5693_s_stream,
612 .g_mbus_config = camera_common_g_mbus_config,
613 .g_input_status = ov5693_g_input_status,
616 static struct v4l2_subdev_core_ops ov5693_subdev_core_ops = {
617 .s_power = camera_common_s_power,
620 static int ov5693_get_fmt(struct v4l2_subdev *sd,
621 struct v4l2_subdev_pad_config *cfg,
622 struct v4l2_subdev_format *format)
624 return camera_common_g_fmt(sd, &format->format);
627 static int ov5693_set_fmt(struct v4l2_subdev *sd,
628 struct v4l2_subdev_pad_config *cfg,
629 struct v4l2_subdev_format *format)
633 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
634 ret = camera_common_try_fmt(sd, &format->format);
636 ret = camera_common_s_fmt(sd, &format->format);
641 static struct v4l2_subdev_pad_ops ov5693_subdev_pad_ops = {
642 .set_fmt = ov5693_set_fmt,
643 .get_fmt = ov5693_get_fmt,
644 .enum_mbus_code = camera_common_enum_mbus_code,
645 .enum_frame_size = camera_common_enum_framesizes,
646 .enum_frame_interval = camera_common_enum_frameintervals,
649 static struct v4l2_subdev_ops ov5693_subdev_ops = {
650 .core = &ov5693_subdev_core_ops,
651 .video = &ov5693_subdev_video_ops,
652 .pad = &ov5693_subdev_pad_ops,
655 static struct of_device_id ov5693_of_match[] = {
656 { .compatible = "nvidia,ov5693", },
660 static struct camera_common_sensor_ops ov5693_common_ops = {
661 .power_on = ov5693_power_on,
662 .power_off = ov5693_power_off,
663 .write_reg = ov5693_write_reg,
664 .read_reg = ov5693_read_reg,
667 static int ov5693_set_group_hold(struct ov5693 *priv)
670 int gh_prev = switch_ctrl_qmenu[priv->group_hold_prev];
672 if (priv->group_hold_en == true && gh_prev == SWITCH_OFF) {
673 /* enter group hold */
674 err = ov5693_write_reg(priv->s_data,
675 OV5693_GROUP_HOLD_ADDR, 0x01);
679 priv->group_hold_prev = 1;
681 dev_dbg(&priv->i2c_client->dev,
682 "%s: enter group hold\n", __func__);
683 } else if (priv->group_hold_en == false && gh_prev == SWITCH_ON) {
684 /* leave group hold */
685 err = ov5693_write_reg(priv->s_data,
686 OV5693_GROUP_HOLD_ADDR, 0x11);
690 err = ov5693_write_reg(priv->s_data,
691 OV5693_GROUP_HOLD_ADDR, 0x61);
695 priv->group_hold_prev = 0;
697 dev_dbg(&priv->i2c_client->dev,
698 "%s: leave group hold\n", __func__);
704 dev_dbg(&priv->i2c_client->dev,
705 "%s: Group hold control error\n", __func__);
709 static u16 ov5693_to_real_gain(u32 rep, int shift)
714 int min_int = (1 << shift);
716 if (rep < OV5693_MIN_GAIN)
717 rep = OV5693_MIN_GAIN;
718 else if (rep > OV5693_MAX_GAIN)
719 rep = OV5693_MAX_GAIN;
721 gain_int = (int)(rep >> shift);
722 gain_dec = (int)(rep & ~(0xffff << shift));
724 /* derived from formulat gain = (x * 16 + 0.5) */
725 gain = ((gain_int * min_int + gain_dec) * 32 + min_int) / (2 * min_int);
730 static int ov5693_set_gain(struct ov5693 *priv, s32 val)
732 ov5693_reg reg_list[2];
737 if (!priv->group_hold_prev)
738 ov5693_set_group_hold(priv);
740 /* translate value */
741 gain = ov5693_to_real_gain((u32)val, OV5693_GAIN_SHIFT);
743 ov5693_get_gain_regs(reg_list, gain);
744 dev_dbg(&priv->i2c_client->dev,
745 "%s: gain %04x val: %04x\n", __func__, val, gain);
747 for (i = 0; i < 2; i++) {
748 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
757 dev_dbg(&priv->i2c_client->dev,
758 "%s: GAIN control error\n", __func__);
762 static void ov5693_update_ctrl_range(struct ov5693 *priv, s32 frame_length)
764 struct v4l2_ctrl *ctrl = NULL;
765 int ctrl_ids[2] = {V4L2_CID_COARSE_TIME,
766 V4L2_CID_COARSE_TIME_SHORT};
770 for (i = 0; i < ARRAY_SIZE(ctrl_ids); i++) {
771 for (j = 0; j < priv->numctrls; j++) {
772 if (priv->ctrls[j]->id == ctrl_ids[i]) {
773 ctrl = priv->ctrls[j];
778 if (j == priv->numctrls) {
779 dev_err(&priv->i2c_client->dev,
780 "could not find ctrl %x\n",
785 max = frame_length - OV5693_MAX_COARSE_DIFF;
786 /* clamp the value in case above is negative */
787 max = clamp_val(max, OV5693_MIN_EXPOSURE_COARSE,
788 OV5693_MAX_EXPOSURE_COARSE);
789 min = OV5693_MIN_EXPOSURE_COARSE;
790 def = clamp_val(OV5693_DEFAULT_EXPOSURE_COARSE, min, max);
791 if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def))
792 dev_err(&priv->i2c_client->dev,
793 "ctrl %x: range update failed\n",
799 static int ov5693_set_frame_length(struct ov5693 *priv, s32 val)
801 ov5693_reg reg_list[2];
806 if (!priv->group_hold_prev)
807 ov5693_set_group_hold(priv);
809 frame_length = (u32)val;
811 ov5693_get_frame_length_regs(reg_list, frame_length);
812 dev_dbg(&priv->i2c_client->dev,
813 "%s: val: %d\n", __func__, frame_length);
815 for (i = 0; i < 2; i++) {
816 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
822 priv->frame_length = frame_length;
824 ov5693_update_ctrl_range(priv, val);
828 dev_dbg(&priv->i2c_client->dev,
829 "%s: FRAME_LENGTH control error\n", __func__);
833 static int ov5693_set_coarse_time(struct ov5693 *priv, s32 val)
835 ov5693_reg reg_list[3];
840 if (!priv->group_hold_prev)
841 ov5693_set_group_hold(priv);
843 coarse_time = (u32)val;
845 ov5693_get_coarse_time_regs(reg_list, coarse_time);
846 dev_dbg(&priv->i2c_client->dev,
847 "%s: val: %d\n", __func__, coarse_time);
849 for (i = 0; i < 3; i++) {
850 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
859 dev_dbg(&priv->i2c_client->dev,
860 "%s: COARSE_TIME control error\n", __func__);
864 static int ov5693_set_coarse_time_short(struct ov5693 *priv, s32 val)
866 ov5693_reg reg_list[3];
868 struct v4l2_control hdr_control;
870 u32 coarse_time_short;
873 if (!priv->group_hold_prev)
874 ov5693_set_group_hold(priv);
876 /* check hdr enable ctrl */
877 hdr_control.id = V4L2_CID_HDR_EN;
879 err = camera_common_g_ctrl(priv->s_data, &hdr_control);
881 dev_err(&priv->i2c_client->dev,
882 "could not find device ctrl.\n");
886 hdr_en = switch_ctrl_qmenu[hdr_control.value];
887 if (hdr_en == SWITCH_OFF)
890 coarse_time_short = (u32)val;
892 ov5693_get_coarse_time_short_regs(reg_list, coarse_time_short);
893 dev_dbg(&priv->i2c_client->dev,
894 "%s: val: %d\n", __func__, coarse_time_short);
896 for (i = 0; i < 3; i++) {
897 err = ov5693_write_reg(priv->s_data, reg_list[i].addr,
906 dev_dbg(&priv->i2c_client->dev,
907 "%s: COARSE_TIME_SHORT control error\n", __func__);
911 static int ov5693_eeprom_device_release(struct ov5693 *priv)
915 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
916 if (priv->eeprom[i].i2c_client != NULL) {
917 i2c_unregister_device(priv->eeprom[i].i2c_client);
918 priv->eeprom[i].i2c_client = NULL;
925 static int ov5693_eeprom_device_init(struct ov5693 *priv)
927 char *dev_name = "eeprom_ov5693";
928 static struct regmap_config eeprom_regmap_config = {
935 if (!priv->pdata->has_eeprom)
938 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
939 priv->eeprom[i].adap = i2c_get_adapter(
940 priv->i2c_client->adapter->nr);
941 memset(&priv->eeprom[i].brd, 0, sizeof(priv->eeprom[i].brd));
942 strncpy(priv->eeprom[i].brd.type, dev_name,
943 sizeof(priv->eeprom[i].brd.type));
944 priv->eeprom[i].brd.addr = OV5693_EEPROM_ADDRESS + i;
945 priv->eeprom[i].i2c_client = i2c_new_device(
946 priv->eeprom[i].adap, &priv->eeprom[i].brd);
948 priv->eeprom[i].regmap = devm_regmap_init_i2c(
949 priv->eeprom[i].i2c_client, &eeprom_regmap_config);
950 if (IS_ERR(priv->eeprom[i].regmap)) {
951 err = PTR_ERR(priv->eeprom[i].regmap);
952 ov5693_eeprom_device_release(priv);
960 static int ov5693_read_eeprom(struct ov5693 *priv,
961 struct v4l2_ctrl *ctrl)
965 for (i = 0; i < OV5693_EEPROM_NUM_BLOCKS; i++) {
966 err = regmap_bulk_read(priv->eeprom[i].regmap, 0,
967 &priv->eeprom_buf[i * OV5693_EEPROM_BLOCK_SIZE],
968 OV5693_EEPROM_BLOCK_SIZE);
973 for (i = 0; i < OV5693_EEPROM_SIZE; i++)
974 sprintf(&ctrl->p_new.p_char[i*2], "%02x",
975 priv->eeprom_buf[i]);
979 static int ov5693_write_eeprom(struct ov5693 *priv,
987 for (i = 0; i < OV5693_EEPROM_SIZE; i++) {
988 curr[0] = string[i*2];
989 curr[1] = string[i*2+1];
992 err = kstrtol(curr, 16, &data);
994 dev_err(&priv->i2c_client->dev,
995 "invalid eeprom string\n");
999 priv->eeprom_buf[i] = (u8)data;
1000 err = regmap_write(priv->eeprom[i >> 8].regmap,
1001 i & 0xFF, (u8)data);
1009 static int ov5693_read_otp_bank(struct ov5693 *priv,
1010 u8 *buf, int bank, u16 addr, int size)
1014 /* sleeps calls in the sequence below are for internal device
1015 * signal propagation as specified by sensor vendor */
1017 usleep_range(10000, 11000);
1018 err = ov5693_write_table(priv, mode_table[OV5693_MODE_START_STREAM]);
1022 err = ov5693_write_reg(priv->s_data, OV5693_OTP_BANK_SELECT_ADDR,
1027 err = ov5693_write_reg(priv->s_data, OV5693_OTP_LOAD_CTRL_ADDR, 0x01);
1031 usleep_range(10000, 11000);
1032 err = regmap_bulk_read(priv->regmap, addr, buf, size);
1036 err = ov5693_write_table(priv,
1037 mode_table[OV5693_MODE_STOP_STREAM]);
1044 static int ov5693_otp_setup(struct ov5693 *priv)
1048 struct v4l2_ctrl *ctrl;
1049 u8 otp_buf[OV5693_OTP_SIZE];
1051 err = camera_common_s_power(priv->subdev, true);
1055 for (i = 0; i < OV5693_OTP_NUM_BANKS; i++) {
1056 err = ov5693_read_otp_bank(priv,
1057 &otp_buf[i * OV5693_OTP_BANK_SIZE],
1059 OV5693_OTP_BANK_START_ADDR,
1060 OV5693_OTP_BANK_SIZE);
1062 dev_err(&priv->i2c_client->dev,
1063 "could not read otp bank\n");
1068 ctrl = v4l2_ctrl_find(&priv->ctrl_handler, V4L2_CID_OTP_DATA);
1070 dev_err(&priv->i2c_client->dev,
1071 "could not find device ctrl.\n");
1076 for (i = 0; i < OV5693_OTP_SIZE; i++)
1077 sprintf(&ctrl->p_new.p_char[i*2], "%02x",
1079 ctrl->p_cur.p_char = ctrl->p_new.p_char;
1082 camera_common_s_power(priv->subdev, false);
1087 static int ov5693_fuse_id_setup(struct ov5693 *priv)
1091 struct v4l2_ctrl *ctrl;
1092 u8 fuse_id[OV5693_FUSE_ID_SIZE];
1094 err = camera_common_s_power(priv->subdev, true);
1098 err = ov5693_read_otp_bank(priv,
1100 OV5693_FUSE_ID_OTP_BANK,
1101 OV5693_FUSE_ID_OTP_START_ADDR,
1102 OV5693_FUSE_ID_SIZE);
1104 dev_err(&priv->i2c_client->dev,
1105 "could not read otp bank\n");
1109 ctrl = v4l2_ctrl_find(&priv->ctrl_handler, V4L2_CID_FUSE_ID);
1111 dev_err(&priv->i2c_client->dev,
1112 "could not find device ctrl.\n");
1117 for (i = 0; i < OV5693_FUSE_ID_SIZE; i++)
1118 sprintf(&ctrl->p_new.p_char[i*2], "%02x",
1120 ctrl->p_cur.p_char = ctrl->p_new.p_char;
1123 camera_common_s_power(priv->subdev, false);
1128 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1130 struct ov5693 *priv =
1131 container_of(ctrl->handler, struct ov5693, ctrl_handler);
1134 if (priv->power.state == SWITCH_OFF)
1138 case V4L2_CID_EEPROM_DATA:
1139 err = ov5693_read_eeprom(priv, ctrl);
1144 pr_err("%s: unknown ctrl id.\n", __func__);
1151 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
1153 struct ov5693 *priv =
1154 container_of(ctrl->handler, struct ov5693, ctrl_handler);
1157 if (priv->power.state == SWITCH_OFF)
1162 err = ov5693_set_gain(priv, ctrl->val);
1164 case V4L2_CID_FRAME_LENGTH:
1165 err = ov5693_set_frame_length(priv, ctrl->val);
1167 case V4L2_CID_COARSE_TIME:
1168 err = ov5693_set_coarse_time(priv, ctrl->val);
1170 case V4L2_CID_COARSE_TIME_SHORT:
1171 err = ov5693_set_coarse_time_short(priv, ctrl->val);
1173 case V4L2_CID_GROUP_HOLD:
1174 if (switch_ctrl_qmenu[ctrl->val] == SWITCH_ON) {
1175 priv->group_hold_en = true;
1177 priv->group_hold_en = false;
1178 err = ov5693_set_group_hold(priv);
1181 case V4L2_CID_EEPROM_DATA:
1182 if (!ctrl->p_new.p_char[0])
1184 err = ov5693_write_eeprom(priv, ctrl->p_new.p_char);
1188 case V4L2_CID_HDR_EN:
1191 pr_err("%s: unknown ctrl id.\n", __func__);
1198 static int ov5693_ctrls_init(struct ov5693 *priv, bool eeprom_ctrl)
1200 struct i2c_client *client = priv->i2c_client;
1201 struct camera_common_data *common_data = priv->s_data;
1202 struct v4l2_ctrl *ctrl;
1207 dev_dbg(&client->dev, "%s++\n", __func__);
1209 numctrls = ARRAY_SIZE(ctrl_config_list);
1210 v4l2_ctrl_handler_init(&priv->ctrl_handler, numctrls);
1212 for (i = 0; i < numctrls; i++) {
1213 /* Skip control 'V4L2_CID_EEPROM_DATA' if eeprom inint err */
1214 if (ctrl_config_list[i].id == V4L2_CID_EEPROM_DATA) {
1216 common_data->numctrls -= 1;
1221 ctrl = v4l2_ctrl_new_custom(&priv->ctrl_handler,
1222 &ctrl_config_list[i], NULL);
1224 dev_err(&client->dev, "Failed to init %s ctrl\n",
1225 ctrl_config_list[i].name);
1229 if (ctrl_config_list[i].type == V4L2_CTRL_TYPE_STRING &&
1230 ctrl_config_list[i].flags & V4L2_CTRL_FLAG_READ_ONLY) {
1231 ctrl->p_new.p_char = devm_kzalloc(&client->dev,
1232 ctrl_config_list[i].max + 1, GFP_KERNEL);
1233 if (!ctrl->p_new.p_char)
1236 priv->ctrls[i] = ctrl;
1239 priv->numctrls = numctrls;
1240 priv->subdev->ctrl_handler = &priv->ctrl_handler;
1241 if (priv->ctrl_handler.error) {
1242 dev_err(&client->dev, "Error %d adding controls\n",
1243 priv->ctrl_handler.error);
1244 err = priv->ctrl_handler.error;
1248 err = v4l2_ctrl_handler_setup(&priv->ctrl_handler);
1250 dev_err(&client->dev,
1251 "Error %d setting default controls\n", err);
1255 err = ov5693_otp_setup(priv);
1257 dev_err(&client->dev,
1258 "Error %d reading otp data\n", err);
1262 err = ov5693_fuse_id_setup(priv);
1264 dev_err(&client->dev,
1265 "Error %d reading fuse id data\n", err);
1272 v4l2_ctrl_handler_free(&priv->ctrl_handler);
1276 MODULE_DEVICE_TABLE(of, ov5693_of_match);
1278 static struct camera_common_pdata *ov5693_parse_dt(struct i2c_client *client)
1280 struct device_node *node = client->dev.of_node;
1281 struct camera_common_pdata *board_priv_pdata;
1282 const struct of_device_id *match;
1285 struct camera_common_pdata *ret = NULL;
1290 match = of_match_device(ov5693_of_match, &client->dev);
1292 dev_err(&client->dev, "Failed to find matching dt id\n");
1296 board_priv_pdata = devm_kzalloc(&client->dev,
1297 sizeof(*board_priv_pdata), GFP_KERNEL);
1298 if (!board_priv_pdata)
1301 err = camera_common_parse_clocks(client, board_priv_pdata);
1303 dev_err(&client->dev, "Failed to find clocks\n");
1307 gpio = of_get_named_gpio(node, "pwdn-gpios", 0);
1309 if (gpio == -EPROBE_DEFER) {
1310 ret = ERR_PTR(-EPROBE_DEFER);
1313 dev_err(&client->dev, "pwdn gpios not in DT\n");
1316 board_priv_pdata->pwdn_gpio = (unsigned int)gpio;
1318 gpio = of_get_named_gpio(node, "reset-gpios", 0);
1320 /* reset-gpio is not absolutely needed */
1321 if (gpio == -EPROBE_DEFER) {
1322 ret = ERR_PTR(-EPROBE_DEFER);
1325 dev_dbg(&client->dev, "reset gpios not in DT\n");
1328 board_priv_pdata->reset_gpio = (unsigned int)gpio;
1330 board_priv_pdata->use_cam_gpio =
1331 of_property_read_bool(node, "cam,use-cam-gpio");
1333 err = of_property_read_string(node, "avdd-reg",
1334 &board_priv_pdata->regulators.avdd);
1336 dev_err(&client->dev, "avdd-reg not in DT\n");
1339 err = of_property_read_string(node, "iovdd-reg",
1340 &board_priv_pdata->regulators.iovdd);
1342 dev_err(&client->dev, "iovdd-reg not in DT\n");
1346 board_priv_pdata->has_eeprom =
1347 of_property_read_bool(node, "has-eeprom");
1348 board_priv_pdata->v_flip= of_property_read_bool(node, "vertical-flip");
1349 board_priv_pdata->h_mirror = of_property_read_bool(node,
1350 "horizontal-mirror");
1351 return board_priv_pdata;
1354 devm_kfree(&client->dev, board_priv_pdata);
1358 static int ov5693_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1360 struct i2c_client *client = v4l2_get_subdevdata(sd);
1362 dev_dbg(&client->dev, "%s:\n", __func__);
1366 static const struct v4l2_subdev_internal_ops ov5693_subdev_internal_ops = {
1367 .open = ov5693_open,
1370 static const struct media_entity_operations ov5693_media_ops = {
1371 .link_validate = v4l2_subdev_link_validate,
1374 static int ov5693_probe(struct i2c_client *client,
1375 const struct i2c_device_id *id)
1377 struct camera_common_data *common_data;
1378 struct device_node *node = client->dev.of_node;
1379 struct ov5693 *priv;
1380 char debugfs_name[10];
1383 pr_info("[OV5693]: probing v4l2 sensor.\n");
1385 if (!IS_ENABLED(CONFIG_OF) || !node)
1388 common_data = devm_kzalloc(&client->dev,
1389 sizeof(struct camera_common_data), GFP_KERNEL);
1393 priv = devm_kzalloc(&client->dev,
1394 sizeof(struct ov5693) + sizeof(struct v4l2_ctrl *) *
1395 ARRAY_SIZE(ctrl_config_list),
1400 priv->regmap = devm_regmap_init_i2c(client, &ov5693_regmap_config);
1401 if (IS_ERR(priv->regmap)) {
1402 dev_err(&client->dev,
1403 "regmap init failed: %ld\n", PTR_ERR(priv->regmap));
1407 priv->pdata = ov5693_parse_dt(client);
1408 if (PTR_ERR(priv->pdata) == -EPROBE_DEFER)
1409 return -EPROBE_DEFER;
1411 dev_err(&client->dev, "unable to get platform data\n");
1415 common_data->ops = &ov5693_common_ops;
1416 common_data->ctrl_handler = &priv->ctrl_handler;
1417 common_data->i2c_client = client;
1418 common_data->frmfmt = ov5693_frmfmt;
1419 common_data->colorfmt = camera_common_find_datafmt(
1420 OV5693_DEFAULT_DATAFMT);
1421 common_data->power = &priv->power;
1422 common_data->ctrls = priv->ctrls;
1423 common_data->priv = (void *)priv;
1424 common_data->numctrls = ARRAY_SIZE(ctrl_config_list);
1425 common_data->numfmts = ARRAY_SIZE(ov5693_frmfmt);
1426 common_data->def_mode = OV5693_DEFAULT_MODE;
1427 common_data->def_width = OV5693_DEFAULT_WIDTH;
1428 common_data->def_height = OV5693_DEFAULT_HEIGHT;
1429 common_data->fmt_width = common_data->def_width;
1430 common_data->fmt_height = common_data->def_height;
1431 common_data->def_clk_freq = OV5693_DEFAULT_CLK_FREQ;
1433 priv->i2c_client = client;
1434 priv->s_data = common_data;
1435 priv->subdev = &common_data->subdev;
1436 priv->subdev->dev = &client->dev;
1437 priv->s_data->dev = &client->dev;
1439 err = ov5693_power_get(priv);
1443 err = camera_common_parse_ports(client, common_data);
1445 dev_err(&client->dev, "Failed to find port info\n");
1448 sprintf(debugfs_name, "ov5693_%c", common_data->csi_port + 'a');
1449 dev_dbg(&client->dev, "%s: name %s\n", __func__, debugfs_name);
1450 camera_common_create_debugfs(common_data, debugfs_name);
1452 v4l2_i2c_subdev_init(priv->subdev, client, &ov5693_subdev_ops);
1454 /* eeprom interface */
1455 err = ov5693_eeprom_device_init(priv);
1456 if (err && priv->pdata->has_eeprom)
1457 dev_err(&client->dev,
1458 "Failed to allocate eeprom reg map: %d\n", err);
1460 err = ov5693_ctrls_init(priv, !err);
1464 priv->subdev->internal_ops = &ov5693_subdev_internal_ops;
1465 priv->subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1466 V4L2_SUBDEV_FL_HAS_EVENTS;
1468 #if defined(CONFIG_MEDIA_CONTROLLER)
1469 priv->pad.flags = MEDIA_PAD_FL_SOURCE;
1470 priv->subdev->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
1471 priv->subdev->entity.ops = &ov5693_media_ops;
1472 err = media_entity_init(&priv->subdev->entity, 1, &priv->pad, 0);
1474 dev_err(&client->dev, "unable to init media entity\n");
1479 err = v4l2_async_register_subdev(priv->subdev);
1483 dev_dbg(&client->dev, "Detected OV5693 sensor\n");
1490 ov5693_remove(struct i2c_client *client)
1492 struct camera_common_data *s_data = to_camera_common_data(client);
1493 struct ov5693 *priv = (struct ov5693 *)s_data->priv;
1495 v4l2_async_unregister_subdev(priv->subdev);
1496 #if defined(CONFIG_MEDIA_CONTROLLER)
1497 media_entity_cleanup(&priv->subdev->entity);
1500 v4l2_ctrl_handler_free(&priv->ctrl_handler);
1501 ov5693_power_put(priv);
1502 camera_common_remove_debugfs(s_data);
1507 static const struct i2c_device_id ov5693_id[] = {
1512 MODULE_DEVICE_TABLE(i2c, ov5693_id);
1514 static struct i2c_driver ov5693_i2c_driver = {
1517 .owner = THIS_MODULE,
1518 .of_match_table = of_match_ptr(ov5693_of_match),
1520 .probe = ov5693_probe,
1521 .remove = ov5693_remove,
1522 .id_table = ov5693_id,
1525 module_i2c_driver(ov5693_i2c_driver);
1527 MODULE_DESCRIPTION("SoC Camera driver for Sony OV5693");
1528 MODULE_AUTHOR("David Wang <davidw@nvidia.com>");
1529 MODULE_LICENSE("GPL v2");