1 From 2caa809318d62cff46258e8109a904c7b43b9ff7 Mon Sep 17 00:00:00 2001
2 From: Zefa Chen <zefa.chen@rock-chips.com>
3 Date: Fri, 17 May 2019 18:23:03 +0800
4 Subject: [PATCH] media: i2c: add ov9281 driver.
6 Change-Id: I7b77250bbc56d2f861450cf77271ad15f9b88ab1
7 Signed-off-by: Zefa Chen <zefa.chen@rock-chips.com>
9 drivers/media/i2c/Kconfig | 11 +
10 drivers/media/i2c/Makefile | 1 +
11 drivers/media/i2c/ov9281.c | 1171 ++++++++++++++++++++++++++++++++++++
12 3 files changed, 1183 insertions(+)
13 create mode 100644 drivers/media/i2c/ov9281.c
15 --- a/drivers/media/i2c/Kconfig
16 +++ b/drivers/media/i2c/Kconfig
17 @@ -1164,6 +1164,17 @@ config VIDEO_OV9640
18 This is a Video4Linux2 sensor driver for the OmniVision
22 + tristate "OmniVision OV9281 sensor support"
23 + depends on I2C && VIDEO_V4L2
24 + depends on MEDIA_CAMERA_SUPPORT
26 + This is a Video4Linux2 sensor-level driver for the OmniVision
29 + To compile this driver as a module, choose M here: the
30 + module will be called ov9281.
33 tristate "OmniVision OV9650/OV9652 sensor support"
34 depends on I2C && VIDEO_V4L2
35 --- a/drivers/media/i2c/Makefile
36 +++ b/drivers/media/i2c/Makefile
37 @@ -84,6 +84,7 @@ obj-$(CONFIG_VIDEO_OV772X) += ov772x.o
38 obj-$(CONFIG_VIDEO_OV7740) += ov7740.o
39 obj-$(CONFIG_VIDEO_OV8856) += ov8856.o
40 obj-$(CONFIG_VIDEO_OV8865) += ov8865.o
41 +obj-$(CONFIG_VIDEO_OV9281) += ov9281.o
42 obj-$(CONFIG_VIDEO_OV9282) += ov9282.o
43 obj-$(CONFIG_VIDEO_OV9640) += ov9640.o
44 obj-$(CONFIG_VIDEO_OV9650) += ov9650.o
46 +++ b/drivers/media/i2c/ov9281.c
48 +// SPDX-License-Identifier: GPL-2.0
52 + * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
55 +#include <linux/clk.h>
56 +#include <linux/device.h>
57 +#include <linux/delay.h>
58 +#include <linux/gpio/consumer.h>
59 +#include <linux/i2c.h>
60 +#include <linux/module.h>
61 +#include <linux/pm_runtime.h>
62 +#include <linux/regulator/consumer.h>
63 +#include <linux/sysfs.h>
64 +#include <linux/slab.h>
65 +#include <linux/rk-camera-module.h>
66 +#include <media/media-entity.h>
67 +#include <media/v4l2-async.h>
68 +#include <media/v4l2-ctrls.h>
69 +#include <media/v4l2-subdev.h>
70 +#include <linux/pinctrl/consumer.h>
72 +#define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x0)
74 +#ifndef V4L2_CID_DIGITAL_GAIN
75 +#define V4L2_CID_DIGITAL_GAIN V4L2_CID_GAIN
78 +#define OV9281_LINK_FREQ_400MHZ 400000000
79 +/* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
80 +#define OV9281_PIXEL_RATE (OV9281_LINK_FREQ_400MHZ * 2 * 2 / 10)
81 +#define OV9281_XVCLK_FREQ 24000000
83 +#define CHIP_ID 0x9281
84 +#define OV9281_REG_CHIP_ID 0x300a
86 +#define OV9281_REG_CTRL_MODE 0x0100
87 +#define OV9281_MODE_SW_STANDBY 0x0
88 +#define OV9281_MODE_STREAMING BIT(0)
90 +#define OV9281_REG_EXPOSURE 0x3500
91 +#define OV9281_EXPOSURE_MIN 4
92 +#define OV9281_EXPOSURE_STEP 1
93 +#define OV9281_VTS_MAX 0x7fff
95 +#define OV9281_REG_GAIN_H 0x3508
96 +#define OV9281_REG_GAIN_L 0x3509
97 +#define OV9281_GAIN_H_MASK 0x07
98 +#define OV9281_GAIN_H_SHIFT 8
99 +#define OV9281_GAIN_L_MASK 0xff
100 +#define OV9281_GAIN_MIN 0x10
101 +#define OV9281_GAIN_MAX 0xf8
102 +#define OV9281_GAIN_STEP 1
103 +#define OV9281_GAIN_DEFAULT 0x10
105 +#define OV9281_REG_TEST_PATTERN 0x5e00
106 +#define OV9281_TEST_PATTERN_ENABLE 0x80
107 +#define OV9281_TEST_PATTERN_DISABLE 0x0
109 +#define OV9281_REG_VTS 0x380e
111 +#define REG_NULL 0xFFFF
113 +#define OV9281_REG_VALUE_08BIT 1
114 +#define OV9281_REG_VALUE_16BIT 2
115 +#define OV9281_REG_VALUE_24BIT 3
117 +#define OV9281_LANES 2
118 +#define OV9281_BITS_PER_SAMPLE 10
120 +#define OF_CAMERA_PINCTRL_STATE_DEFAULT "rockchip,camera_default"
121 +#define OF_CAMERA_PINCTRL_STATE_SLEEP "rockchip,camera_sleep"
123 +#define OV9281_NAME "ov9281"
125 +static const char * const ov9281_supply_names[] = {
126 + "avdd", /* Analog power */
127 + "dovdd", /* Digital I/O power */
128 + "dvdd", /* Digital core power */
131 +#define OV9281_NUM_SUPPLIES ARRAY_SIZE(ov9281_supply_names)
138 +struct ov9281_mode {
145 + const struct regval *reg_list;
149 + struct i2c_client *client;
151 + struct gpio_desc *reset_gpio;
152 + struct gpio_desc *pwdn_gpio;
153 + struct regulator_bulk_data supplies[OV9281_NUM_SUPPLIES];
155 + struct pinctrl *pinctrl;
156 + struct pinctrl_state *pins_default;
157 + struct pinctrl_state *pins_sleep;
159 + struct v4l2_subdev subdev;
160 + struct media_pad pad;
161 + struct v4l2_ctrl_handler ctrl_handler;
162 + struct v4l2_ctrl *exposure;
163 + struct v4l2_ctrl *anal_gain;
164 + struct v4l2_ctrl *digi_gain;
165 + struct v4l2_ctrl *hblank;
166 + struct v4l2_ctrl *vblank;
167 + struct v4l2_ctrl *test_pattern;
168 + struct mutex mutex;
171 + const struct ov9281_mode *cur_mode;
173 + const char *module_facing;
174 + const char *module_name;
175 + const char *len_name;
178 +#define to_ov9281(sd) container_of(sd, struct ov9281, subdev)
183 +static const struct regval ov9281_global_regs[] = {
189 + * max_framerate 120fps
190 + * mipi_datarate per lane 800Mbps
192 +static const struct regval ov9281_1280x800_regs[] = {
291 +static const struct ov9281_mode supported_modes[] = {
297 + .hts_def = 0x0b60,//0x2d8*4
299 + .reg_list = ov9281_1280x800_regs,
303 +static const s64 link_freq_menu_items[] = {
304 + OV9281_LINK_FREQ_400MHZ
307 +static const char * const ov9281_test_pattern_menu[] = {
309 + "Vertical Color Bar Type 1",
310 + "Vertical Color Bar Type 2",
311 + "Vertical Color Bar Type 3",
312 + "Vertical Color Bar Type 4"
315 +/* Write registers up to 4 at a time */
316 +static int ov9281_write_reg(struct i2c_client *client, u16 reg,
328 + buf[1] = reg & 0xff;
330 + val_be = cpu_to_be32(val);
331 + val_p = (u8 *)&val_be;
336 + buf[buf_i++] = val_p[val_i++];
338 + if (i2c_master_send(client, buf, len + 2) != len + 2)
344 +static int ov9281_write_array(struct i2c_client *client,
345 + const struct regval *regs)
350 + for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
351 + ret = ov9281_write_reg(client, regs[i].addr,
352 + OV9281_REG_VALUE_08BIT, regs[i].val);
357 +/* Read registers up to 4 at a time */
358 +static int ov9281_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
361 + struct i2c_msg msgs[2];
363 + __be32 data_be = 0;
364 + __be16 reg_addr_be = cpu_to_be16(reg);
367 + if (len > 4 || !len)
370 + data_be_p = (u8 *)&data_be;
371 + /* Write register address */
372 + msgs[0].addr = client->addr;
375 + msgs[0].buf = (u8 *)®_addr_be;
377 + /* Read data from register */
378 + msgs[1].addr = client->addr;
379 + msgs[1].flags = I2C_M_RD;
381 + msgs[1].buf = &data_be_p[4 - len];
383 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
384 + if (ret != ARRAY_SIZE(msgs))
387 + *val = be32_to_cpu(data_be);
392 +static int ov9281_get_reso_dist(const struct ov9281_mode *mode,
393 + struct v4l2_mbus_framefmt *framefmt)
395 + return abs(mode->width - framefmt->width) +
396 + abs(mode->height - framefmt->height);
399 +static const struct ov9281_mode *
400 +ov9281_find_best_fit(struct v4l2_subdev_format *fmt)
402 + struct v4l2_mbus_framefmt *framefmt = &fmt->format;
404 + int cur_best_fit = 0;
405 + int cur_best_fit_dist = -1;
408 + for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
409 + dist = ov9281_get_reso_dist(&supported_modes[i], framefmt);
410 + if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
411 + cur_best_fit_dist = dist;
416 + return &supported_modes[cur_best_fit];
419 +static int ov9281_set_fmt(struct v4l2_subdev *sd,
420 + struct v4l2_subdev_pad_config *cfg,
421 + struct v4l2_subdev_format *fmt)
423 + struct ov9281 *ov9281 = to_ov9281(sd);
424 + const struct ov9281_mode *mode;
425 + s64 h_blank, vblank_def;
427 + mutex_lock(&ov9281->mutex);
429 + mode = ov9281_find_best_fit(fmt);
430 + fmt->format.code = MEDIA_BUS_FMT_Y10_1X10;
431 + fmt->format.width = mode->width;
432 + fmt->format.height = mode->height;
433 + fmt->format.field = V4L2_FIELD_NONE;
434 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
435 +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
436 + *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
438 + mutex_unlock(&ov9281->mutex);
442 + ov9281->cur_mode = mode;
443 + h_blank = mode->hts_def - mode->width;
444 + __v4l2_ctrl_modify_range(ov9281->hblank, h_blank,
445 + h_blank, 1, h_blank);
446 + vblank_def = mode->vts_def - mode->height;
447 + __v4l2_ctrl_modify_range(ov9281->vblank, vblank_def,
448 + OV9281_VTS_MAX - mode->height,
452 + mutex_unlock(&ov9281->mutex);
457 +static int ov9281_get_fmt(struct v4l2_subdev *sd,
458 + struct v4l2_subdev_pad_config *cfg,
459 + struct v4l2_subdev_format *fmt)
461 + struct ov9281 *ov9281 = to_ov9281(sd);
462 + const struct ov9281_mode *mode = ov9281->cur_mode;
464 + mutex_lock(&ov9281->mutex);
465 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
466 +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
467 + fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
469 + mutex_unlock(&ov9281->mutex);
473 + fmt->format.width = mode->width;
474 + fmt->format.height = mode->height;
475 + fmt->format.code = MEDIA_BUS_FMT_Y10_1X10;
476 + fmt->format.field = V4L2_FIELD_NONE;
478 + mutex_unlock(&ov9281->mutex);
483 +static int ov9281_enum_mbus_code(struct v4l2_subdev *sd,
484 + struct v4l2_subdev_pad_config *cfg,
485 + struct v4l2_subdev_mbus_code_enum *code)
487 + if (code->index != 0)
489 + code->code = MEDIA_BUS_FMT_Y10_1X10;
494 +static int ov9281_enum_frame_sizes(struct v4l2_subdev *sd,
495 + struct v4l2_subdev_pad_config *cfg,
496 + struct v4l2_subdev_frame_size_enum *fse)
498 + if (fse->index >= ARRAY_SIZE(supported_modes))
501 + if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
504 + fse->min_width = supported_modes[fse->index].width;
505 + fse->max_width = supported_modes[fse->index].width;
506 + fse->max_height = supported_modes[fse->index].height;
507 + fse->min_height = supported_modes[fse->index].height;
512 +static int ov9281_enable_test_pattern(struct ov9281 *ov9281, u32 pattern)
517 + val = (pattern - 1) | OV9281_TEST_PATTERN_ENABLE;
519 + val = OV9281_TEST_PATTERN_DISABLE;
521 + return ov9281_write_reg(ov9281->client, OV9281_REG_TEST_PATTERN,
522 + OV9281_REG_VALUE_08BIT, val);
525 +static int OV9281_g_frame_interval(struct v4l2_subdev *sd,
526 + struct v4l2_subdev_frame_interval *fi)
528 + struct ov9281 *ov9281 = to_ov9281(sd);
529 + const struct ov9281_mode *mode = ov9281->cur_mode;
531 + mutex_lock(&ov9281->mutex);
532 + fi->interval.numerator = 10000;
533 + fi->interval.denominator = mode->max_fps * 10000;
534 + mutex_unlock(&ov9281->mutex);
539 +static void ov9281_get_module_inf(struct ov9281 *ov9281,
540 + struct rkmodule_inf *inf)
542 + memset(inf, 0, sizeof(*inf));
543 + strlcpy(inf->base.sensor, OV9281_NAME, sizeof(inf->base.sensor));
544 + strlcpy(inf->base.module, ov9281->module_name,
545 + sizeof(inf->base.module));
546 + strlcpy(inf->base.lens, ov9281->len_name, sizeof(inf->base.lens));
549 +static long ov9281_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
551 + struct ov9281 *ov9281 = to_ov9281(sd);
555 + case RKMODULE_GET_MODULE_INFO:
556 + ov9281_get_module_inf(ov9281, (struct rkmodule_inf *)arg);
559 + ret = -ENOIOCTLCMD;
566 +#ifdef CONFIG_COMPAT
567 +static long ov9281_compat_ioctl32(struct v4l2_subdev *sd,
568 + unsigned int cmd, unsigned long arg)
570 + void __user *up = compat_ptr(arg);
571 + struct rkmodule_inf *inf;
572 + struct rkmodule_awb_cfg *cfg;
576 + case RKMODULE_GET_MODULE_INFO:
577 + inf = kzalloc(sizeof(*inf), GFP_KERNEL);
583 + ret = ov9281_ioctl(sd, cmd, inf);
585 + ret = copy_to_user(up, inf, sizeof(*inf));
588 + case RKMODULE_AWB_CFG:
589 + cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
595 + ret = copy_from_user(cfg, up, sizeof(*cfg));
597 + ret = ov9281_ioctl(sd, cmd, cfg);
601 + ret = -ENOIOCTLCMD;
609 +static int __ov9281_start_stream(struct ov9281 *ov9281)
613 + ret = ov9281_write_array(ov9281->client, ov9281->cur_mode->reg_list);
617 + /* In case these controls are set before streaming */
618 + mutex_unlock(&ov9281->mutex);
619 + ret = v4l2_ctrl_handler_setup(&ov9281->ctrl_handler);
620 + mutex_lock(&ov9281->mutex);
624 + return ov9281_write_reg(ov9281->client, OV9281_REG_CTRL_MODE,
625 + OV9281_REG_VALUE_08BIT, OV9281_MODE_STREAMING);
628 +static int __ov9281_stop_stream(struct ov9281 *ov9281)
630 + return ov9281_write_reg(ov9281->client, OV9281_REG_CTRL_MODE,
631 + OV9281_REG_VALUE_08BIT, OV9281_MODE_SW_STANDBY);
634 +static int ov9281_s_stream(struct v4l2_subdev *sd, int on)
636 + struct ov9281 *ov9281 = to_ov9281(sd);
637 + struct i2c_client *client = ov9281->client;
640 + mutex_lock(&ov9281->mutex);
642 + if (on == ov9281->streaming)
643 + goto unlock_and_return;
646 + ret = pm_runtime_get_sync(&client->dev);
648 + pm_runtime_put_noidle(&client->dev);
649 + goto unlock_and_return;
652 + ret = __ov9281_start_stream(ov9281);
654 + v4l2_err(sd, "start stream failed while write regs\n");
655 + pm_runtime_put(&client->dev);
656 + goto unlock_and_return;
659 + __ov9281_stop_stream(ov9281);
660 + pm_runtime_put(&client->dev);
663 + ov9281->streaming = on;
666 + mutex_unlock(&ov9281->mutex);
671 +static int ov9281_s_power(struct v4l2_subdev *sd, int on)
673 + struct ov9281 *ov9281 = to_ov9281(sd);
674 + struct i2c_client *client = ov9281->client;
677 + mutex_lock(&ov9281->mutex);
679 + /* If the power state is not modified - no work to do. */
680 + if (ov9281->power_on == !!on)
681 + goto unlock_and_return;
684 + ret = pm_runtime_get_sync(&client->dev);
686 + pm_runtime_put_noidle(&client->dev);
687 + goto unlock_and_return;
689 + ret = ov9281_write_array(ov9281->client, ov9281_global_regs);
691 + v4l2_err(sd, "could not set init registers\n");
692 + pm_runtime_put_noidle(&client->dev);
693 + goto unlock_and_return;
695 + ov9281->power_on = true;
697 + pm_runtime_put(&client->dev);
698 + ov9281->power_on = false;
702 + mutex_unlock(&ov9281->mutex);
707 +/* Calculate the delay in us by clock rate and clock cycles */
708 +static inline u32 ov9281_cal_delay(u32 cycles)
710 + return DIV_ROUND_UP(cycles, OV9281_XVCLK_FREQ / 1000 / 1000);
713 +static int __ov9281_power_on(struct ov9281 *ov9281)
717 + struct device *dev = &ov9281->client->dev;
719 + if (!IS_ERR_OR_NULL(ov9281->pins_default)) {
720 + ret = pinctrl_select_state(ov9281->pinctrl,
721 + ov9281->pins_default);
723 + dev_err(dev, "could not set pins\n");
726 + ret = clk_prepare_enable(ov9281->xvclk);
728 + dev_err(dev, "Failed to enable xvclk\n");
732 + if (!IS_ERR(ov9281->reset_gpio))
733 + gpiod_set_value_cansleep(ov9281->reset_gpio, 0);
735 + ret = regulator_bulk_enable(OV9281_NUM_SUPPLIES, ov9281->supplies);
737 + dev_err(dev, "Failed to enable regulators\n");
741 + if (!IS_ERR(ov9281->reset_gpio))
742 + gpiod_set_value_cansleep(ov9281->reset_gpio, 1);
744 + usleep_range(500, 1000);
745 + if (!IS_ERR(ov9281->pwdn_gpio))
746 + gpiod_set_value_cansleep(ov9281->pwdn_gpio, 1);
748 + /* 8192 cycles prior to first SCCB transaction */
749 + delay_us = ov9281_cal_delay(8192);
750 + usleep_range(delay_us, delay_us * 2);
755 + clk_disable_unprepare(ov9281->xvclk);
760 +static void __ov9281_power_off(struct ov9281 *ov9281)
763 + struct device *dev = &ov9281->client->dev;
765 + if (!IS_ERR(ov9281->pwdn_gpio))
766 + gpiod_set_value_cansleep(ov9281->pwdn_gpio, 0);
767 + clk_disable_unprepare(ov9281->xvclk);
768 + if (!IS_ERR(ov9281->reset_gpio))
769 + gpiod_set_value_cansleep(ov9281->reset_gpio, 0);
770 + if (!IS_ERR_OR_NULL(ov9281->pins_sleep)) {
771 + ret = pinctrl_select_state(ov9281->pinctrl,
772 + ov9281->pins_sleep);
774 + dev_dbg(dev, "could not set pins\n");
776 + regulator_bulk_disable(OV9281_NUM_SUPPLIES, ov9281->supplies);
779 +static int ov9281_runtime_resume(struct device *dev)
781 + struct i2c_client *client = to_i2c_client(dev);
782 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
783 + struct ov9281 *ov9281 = to_ov9281(sd);
785 + return __ov9281_power_on(ov9281);
788 +static int ov9281_runtime_suspend(struct device *dev)
790 + struct i2c_client *client = to_i2c_client(dev);
791 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
792 + struct ov9281 *ov9281 = to_ov9281(sd);
794 + __ov9281_power_off(ov9281);
799 +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
800 +static int ov9281_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
802 + struct ov9281 *ov9281 = to_ov9281(sd);
803 + struct v4l2_mbus_framefmt *try_fmt =
804 + v4l2_subdev_get_try_format(sd, fh->pad, 0);
805 + const struct ov9281_mode *def_mode = &supported_modes[0];
807 + mutex_lock(&ov9281->mutex);
808 + /* Initialize try_fmt */
809 + try_fmt->width = def_mode->width;
810 + try_fmt->height = def_mode->height;
811 + try_fmt->code = MEDIA_BUS_FMT_Y10_1X10;
812 + try_fmt->field = V4L2_FIELD_NONE;
814 + mutex_unlock(&ov9281->mutex);
815 + /* No crop or compose */
821 +static const struct dev_pm_ops ov9281_pm_ops = {
822 + SET_RUNTIME_PM_OPS(ov9281_runtime_suspend,
823 + ov9281_runtime_resume, NULL)
826 +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
827 +static const struct v4l2_subdev_internal_ops ov9281_internal_ops = {
828 + .open = ov9281_open,
832 +static const struct v4l2_subdev_core_ops ov9281_core_ops = {
833 + .s_power = ov9281_s_power,
834 + .ioctl = ov9281_ioctl,
835 +#ifdef CONFIG_COMPAT
836 + .compat_ioctl32 = ov9281_compat_ioctl32,
840 +static const struct v4l2_subdev_video_ops ov9281_video_ops = {
841 + .s_stream = ov9281_s_stream,
842 + .g_frame_interval = OV9281_g_frame_interval,
845 +static const struct v4l2_subdev_pad_ops ov9281_pad_ops = {
846 + .enum_mbus_code = ov9281_enum_mbus_code,
847 + .enum_frame_size = ov9281_enum_frame_sizes,
848 + .get_fmt = ov9281_get_fmt,
849 + .set_fmt = ov9281_set_fmt,
852 +static const struct v4l2_subdev_ops ov9281_subdev_ops = {
853 + .core = &ov9281_core_ops,
854 + .video = &ov9281_video_ops,
855 + .pad = &ov9281_pad_ops,
858 +static int ov9281_set_ctrl(struct v4l2_ctrl *ctrl)
860 + struct ov9281 *ov9281 = container_of(ctrl->handler,
861 + struct ov9281, ctrl_handler);
862 + struct i2c_client *client = ov9281->client;
866 + /* Propagate change of current control to all related controls */
867 + switch (ctrl->id) {
868 + case V4L2_CID_VBLANK:
869 + /* Update max exposure while meeting expected vblanking */
870 + max = ov9281->cur_mode->height + ctrl->val - 4;
871 + __v4l2_ctrl_modify_range(ov9281->exposure,
872 + ov9281->exposure->minimum, max,
873 + ov9281->exposure->step,
874 + ov9281->exposure->default_value);
878 + if (pm_runtime_get(&client->dev) <= 0)
881 + switch (ctrl->id) {
882 + case V4L2_CID_EXPOSURE:
883 + /* 4 least significant bits of expsoure are fractional part */
884 + ret = ov9281_write_reg(ov9281->client, OV9281_REG_EXPOSURE,
885 + OV9281_REG_VALUE_24BIT, ctrl->val << 4);
887 + case V4L2_CID_ANALOGUE_GAIN:
888 + ret = ov9281_write_reg(ov9281->client, OV9281_REG_GAIN_H,
889 + OV9281_REG_VALUE_08BIT,
890 + (ctrl->val >> OV9281_GAIN_H_SHIFT) & OV9281_GAIN_H_MASK);
891 + ret |= ov9281_write_reg(ov9281->client, OV9281_REG_GAIN_L,
892 + OV9281_REG_VALUE_08BIT,
893 + ctrl->val & OV9281_GAIN_L_MASK);
895 + case V4L2_CID_VBLANK:
896 + ret = ov9281_write_reg(ov9281->client, OV9281_REG_VTS,
897 + OV9281_REG_VALUE_16BIT,
898 + ctrl->val + ov9281->cur_mode->height);
900 + case V4L2_CID_TEST_PATTERN:
901 + ret = ov9281_enable_test_pattern(ov9281, ctrl->val);
904 + dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
905 + __func__, ctrl->id, ctrl->val);
909 + pm_runtime_put(&client->dev);
914 +static const struct v4l2_ctrl_ops ov9281_ctrl_ops = {
915 + .s_ctrl = ov9281_set_ctrl,
918 +static int ov9281_initialize_controls(struct ov9281 *ov9281)
920 + const struct ov9281_mode *mode;
921 + struct v4l2_ctrl_handler *handler;
922 + struct v4l2_ctrl *ctrl;
923 + s64 exposure_max, vblank_def;
927 + handler = &ov9281->ctrl_handler;
928 + mode = ov9281->cur_mode;
929 + ret = v4l2_ctrl_handler_init(handler, 8);
932 + handler->lock = &ov9281->mutex;
934 + ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
935 + 0, 0, link_freq_menu_items);
937 + ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
939 + v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
940 + 0, OV9281_PIXEL_RATE, 1, OV9281_PIXEL_RATE);
942 + h_blank = mode->hts_def - mode->width;
943 + ov9281->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
944 + h_blank, h_blank, 1, h_blank);
945 + if (ov9281->hblank)
946 + ov9281->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
948 + vblank_def = mode->vts_def - mode->height;
949 + ov9281->vblank = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
950 + V4L2_CID_VBLANK, vblank_def,
951 + OV9281_VTS_MAX - mode->height,
954 + exposure_max = mode->vts_def - 4;
955 + ov9281->exposure = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
956 + V4L2_CID_EXPOSURE, OV9281_EXPOSURE_MIN,
957 + exposure_max, OV9281_EXPOSURE_STEP,
960 + ov9281->anal_gain = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
961 + V4L2_CID_ANALOGUE_GAIN, OV9281_GAIN_MIN,
962 + OV9281_GAIN_MAX, OV9281_GAIN_STEP,
963 + OV9281_GAIN_DEFAULT);
965 + ov9281->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
966 + &ov9281_ctrl_ops, V4L2_CID_TEST_PATTERN,
967 + ARRAY_SIZE(ov9281_test_pattern_menu) - 1,
968 + 0, 0, ov9281_test_pattern_menu);
970 + if (handler->error) {
971 + ret = handler->error;
972 + dev_err(&ov9281->client->dev,
973 + "Failed to init controls(%d)\n", ret);
974 + goto err_free_handler;
977 + ov9281->subdev.ctrl_handler = handler;
982 + v4l2_ctrl_handler_free(handler);
987 +static int ov9281_check_sensor_id(struct ov9281 *ov9281,
988 + struct i2c_client *client)
990 + struct device *dev = &ov9281->client->dev;
994 + ret = ov9281_read_reg(client, OV9281_REG_CHIP_ID,
995 + OV9281_REG_VALUE_16BIT, &id);
996 + if (id != CHIP_ID) {
997 + dev_err(dev, "Unexpected sensor id(%06x), ret(%d)\n", id, ret);
1001 + dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
1006 +static int ov9281_configure_regulators(struct ov9281 *ov9281)
1010 + for (i = 0; i < OV9281_NUM_SUPPLIES; i++)
1011 + ov9281->supplies[i].supply = ov9281_supply_names[i];
1013 + return devm_regulator_bulk_get(&ov9281->client->dev,
1014 + OV9281_NUM_SUPPLIES,
1015 + ov9281->supplies);
1018 +static int ov9281_probe(struct i2c_client *client,
1019 + const struct i2c_device_id *id)
1021 + struct device *dev = &client->dev;
1022 + struct device_node *node = dev->of_node;
1023 + struct ov9281 *ov9281;
1024 + struct v4l2_subdev *sd;
1028 + dev_info(dev, "driver version: %02x.%02x.%02x",
1029 + DRIVER_VERSION >> 16,
1030 + (DRIVER_VERSION & 0xff00) >> 8,
1031 + DRIVER_VERSION & 0x00ff);
1033 + ov9281 = devm_kzalloc(dev, sizeof(*ov9281), GFP_KERNEL);
1037 + ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1038 + &ov9281->module_index);
1039 + ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1040 + &ov9281->module_facing);
1041 + ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1042 + &ov9281->module_name);
1043 + ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1044 + &ov9281->len_name);
1046 + dev_err(dev, "could not get module information!\n");
1050 + ov9281->client = client;
1051 + ov9281->cur_mode = &supported_modes[0];
1053 + ov9281->xvclk = devm_clk_get(dev, "xvclk");
1054 + if (IS_ERR(ov9281->xvclk)) {
1055 + dev_err(dev, "Failed to get xvclk\n");
1058 + ret = clk_set_rate(ov9281->xvclk, OV9281_XVCLK_FREQ);
1060 + dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
1063 + if (clk_get_rate(ov9281->xvclk) != OV9281_XVCLK_FREQ)
1064 + dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
1066 + ov9281->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1067 + if (IS_ERR(ov9281->reset_gpio))
1068 + dev_warn(dev, "Failed to get reset-gpios\n");
1070 + ov9281->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1071 + if (IS_ERR(ov9281->pwdn_gpio))
1072 + dev_warn(dev, "Failed to get pwdn-gpios\n");
1074 + ov9281->pinctrl = devm_pinctrl_get(dev);
1075 + if (!IS_ERR(ov9281->pinctrl)) {
1076 + ov9281->pins_default =
1077 + pinctrl_lookup_state(ov9281->pinctrl,
1078 + OF_CAMERA_PINCTRL_STATE_DEFAULT);
1079 + if (IS_ERR(ov9281->pins_default))
1080 + dev_err(dev, "could not get default pinstate\n");
1082 + ov9281->pins_sleep =
1083 + pinctrl_lookup_state(ov9281->pinctrl,
1084 + OF_CAMERA_PINCTRL_STATE_SLEEP);
1085 + if (IS_ERR(ov9281->pins_sleep))
1086 + dev_err(dev, "could not get sleep pinstate\n");
1088 + dev_err(dev, "no pinctrl\n");
1091 + ret = ov9281_configure_regulators(ov9281);
1093 + dev_err(dev, "Failed to get power regulators\n");
1097 + mutex_init(&ov9281->mutex);
1099 + sd = &ov9281->subdev;
1100 + v4l2_i2c_subdev_init(sd, client, &ov9281_subdev_ops);
1101 + ret = ov9281_initialize_controls(ov9281);
1103 + goto err_destroy_mutex;
1105 + ret = __ov9281_power_on(ov9281);
1107 + goto err_free_handler;
1109 + ret = ov9281_check_sensor_id(ov9281, client);
1111 + goto err_power_off;
1113 +#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1114 + sd->internal_ops = &ov9281_internal_ops;
1115 + sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1117 +#if defined(CONFIG_MEDIA_CONTROLLER)
1118 + ov9281->pad.flags = MEDIA_PAD_FL_SOURCE;
1119 + sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
1120 + ret = media_entity_init(&sd->entity, 1, &ov9281->pad, 0);
1122 + goto err_power_off;
1125 + memset(facing, 0, sizeof(facing));
1126 + if (strcmp(ov9281->module_facing, "back") == 0)
1131 + snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1132 + ov9281->module_index, facing,
1133 + OV9281_NAME, dev_name(sd->dev));
1134 + ret = v4l2_async_register_subdev_sensor(sd);
1136 + dev_err(dev, "v4l2 async register subdev failed\n");
1137 + goto err_clean_entity;
1140 + pm_runtime_set_active(dev);
1141 + pm_runtime_enable(dev);
1142 + pm_runtime_idle(dev);
1147 +#if defined(CONFIG_MEDIA_CONTROLLER)
1148 + media_entity_cleanup(&sd->entity);
1151 + __ov9281_power_off(ov9281);
1153 + v4l2_ctrl_handler_free(&ov9281->ctrl_handler);
1155 + mutex_destroy(&ov9281->mutex);
1160 +static int ov9281_remove(struct i2c_client *client)
1162 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1163 + struct ov9281 *ov9281 = to_ov9281(sd);
1165 + v4l2_async_unregister_subdev(sd);
1166 +#if defined(CONFIG_MEDIA_CONTROLLER)
1167 + media_entity_cleanup(&sd->entity);
1169 + v4l2_ctrl_handler_free(&ov9281->ctrl_handler);
1170 + mutex_destroy(&ov9281->mutex);
1172 + pm_runtime_disable(&client->dev);
1173 + if (!pm_runtime_status_suspended(&client->dev))
1174 + __ov9281_power_off(ov9281);
1175 + pm_runtime_set_suspended(&client->dev);
1180 +#if IS_ENABLED(CONFIG_OF)
1181 +static const struct of_device_id ov9281_of_match[] = {
1182 + { .compatible = "ovti,ov9281" },
1185 +MODULE_DEVICE_TABLE(of, ov9281_of_match);
1188 +static const struct i2c_device_id ov9281_match_id[] = {
1189 + { "ovti,ov9281", 0 },
1193 +static struct i2c_driver ov9281_i2c_driver = {
1195 + .name = OV9281_NAME,
1196 + .pm = &ov9281_pm_ops,
1197 + .of_match_table = of_match_ptr(ov9281_of_match),
1199 + .probe = &ov9281_probe,
1200 + .remove = &ov9281_remove,
1201 + .id_table = ov9281_match_id,
1204 +static int __init sensor_mod_init(void)
1206 + return i2c_add_driver(&ov9281_i2c_driver);
1209 +static void __exit sensor_mod_exit(void)
1211 + i2c_del_driver(&ov9281_i2c_driver);
1214 +device_initcall_sync(sensor_mod_init);
1215 +module_exit(sensor_mod_exit);
1217 +MODULE_DESCRIPTION("OmniVision ov9281 sensor driver");
1218 +MODULE_LICENSE("GPL v2");