1 From d26583e9c31d10919c8d4b965b938724fde1ba3c 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 media: i2c: ov9281: fix mclk issue when probe multiple camera.
11 Takes the ov9281 part only from the Rockchip's patch.
13 Change-Id: I30e833baf2c1bb07d6d87ddb3b00759ab45a90e4
14 Signed-off-by: Zefa Chen <zefa.chen@rock-chips.com>
16 media: i2c: ov9281: add enum_frame_interval function for iq tool 2.2 and hal3
18 Adds the ov9281 parts of the Rockchip patch adding enum_frame_interval to
19 a large number of drivers.
21 Change-Id: I03344cd6cf278dd7c18fce8e97479089ef185a5c
22 Signed-off-by: Zefa Chen <zefa.chen@rock-chips.com>
24 media: i2c: ov9281: Fixup for recent kernel releases, and remove custom code
26 The Rockchip driver was based on a 4.4 kernel, and had several custom
29 Update to 5.4 kernel APIs, with the relevant controls required by
30 libcamera, and remove custom Rockchip parts.
32 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
34 media: i2c: ov9281: Read chip ID via 2 reads
36 Vision Components have made an OV9281 module which blocks reading
37 back the majority of registers to comply with NDAs, and in doing
38 so doesn't allow auto-increment register reading as used when
41 Use two reads and manually combine the results.
43 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
45 media: i2c: ov9281: Add support for 8 bit readout
47 The sensor supports 8 bit mode as well as 10bit, so add the
48 relevant code to allow selection of this.
50 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
52 media: ov9281: Add 1280x720 and 640x480 modes
54 Breaks out common register set and adds the different registers
55 for 1280x720 (cropped) and 640x480 (skipped) modes
57 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
59 Fixed picture line bug in all ov9281 modes
61 Signed-off-by: Mathias Anhalt <mathiasanhalt@web.de>
63 Added hflip and vflip controls to ov9281
65 Signed-off-by: Mathias Anhalt <mathiasanhalt@web.de>
67 media: i2c: ov9281: Remove override of subdev name
69 From the original Rockchip driver, the subdev was renamed
70 from the default to being "mov9281 <dev_name>" whereas the
71 default would have been "ov9281 <dev_name>".
73 Remove the override to drop back to the default rather than
74 a vendor custom string.
76 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
78 media: v4l2-subdev: add subdev-wide state struct
80 Signed-off-by: Dom Cobley <popcornmix@gmail.com>
82 media: i2c: ov9281: Add fwnode properties controls
84 Add call to v4l2_ctrl_new_fwnode_properties to read and
85 create the fwnode based controls.
87 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
89 media: i2c: ov9281: Sensor should report RAW color space
91 Tested on Raspberry Pi running libcamera.
93 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
95 drivers/media/i2c/Kconfig | 11 +
96 drivers/media/i2c/Makefile | 1 +
97 drivers/media/i2c/imx290.c | 9 +-
98 drivers/media/i2c/imx477.c | 30 +-
99 drivers/media/i2c/irs1125.c | 12 +-
100 drivers/media/i2c/ov9281.c | 1290 +++++++++++++++++
101 .../media/platform/bcm2835/bcm2835-unicam.c | 20 +-
102 .../bcm2835-isp/bcm2835-v4l2-isp.c | 4 +-
103 8 files changed, 1341 insertions(+), 36 deletions(-)
104 create mode 100644 drivers/media/i2c/ov9281.c
106 --- a/drivers/media/i2c/Kconfig
107 +++ b/drivers/media/i2c/Kconfig
108 @@ -1343,6 +1343,17 @@ config VIDEO_TW2804
109 To compile this driver as a module, choose M here: the
110 module will be called tw2804.
113 + tristate "OmniVision OV9281 sensor support"
114 + depends on I2C && VIDEO_DEV
115 + depends on MEDIA_CAMERA_SUPPORT
117 + This is a Video4Linux2 sensor-level driver for the OmniVision
120 + To compile this driver as a module, choose M here: the
121 + module will be called ov9281.
124 tristate "Techwell TW9903 video decoder"
125 depends on VIDEO_DEV && I2C
126 --- a/drivers/media/i2c/Makefile
127 +++ b/drivers/media/i2c/Makefile
128 @@ -98,6 +98,7 @@ obj-$(CONFIG_VIDEO_OV772X) += ov772x.o
129 obj-$(CONFIG_VIDEO_OV7740) += ov7740.o
130 obj-$(CONFIG_VIDEO_OV8856) += ov8856.o
131 obj-$(CONFIG_VIDEO_OV8865) += ov8865.o
132 +obj-$(CONFIG_VIDEO_OV9281) += ov9281.o
133 obj-$(CONFIG_VIDEO_OV9282) += ov9282.o
134 obj-$(CONFIG_VIDEO_OV9640) += ov9640.o
135 obj-$(CONFIG_VIDEO_OV9650) += ov9650.o
136 --- a/drivers/media/i2c/imx290.c
137 +++ b/drivers/media/i2c/imx290.c
138 @@ -982,12 +982,13 @@ static int imx290_write_current_format(s
141 static const struct v4l2_rect *
142 -__imx290_get_pad_crop(struct imx290 *imx290, struct v4l2_subdev_pad_config *cfg,
143 +__imx290_get_pad_crop(struct imx290 *imx290,
144 + struct v4l2_subdev_state *sd_state,
145 unsigned int pad, enum v4l2_subdev_format_whence which)
148 case V4L2_SUBDEV_FORMAT_TRY:
149 - return v4l2_subdev_get_try_crop(&imx290->sd, cfg, pad);
150 + return v4l2_subdev_get_try_crop(&imx290->sd, sd_state, pad);
151 case V4L2_SUBDEV_FORMAT_ACTIVE:
152 return &imx290->current_mode->crop;
154 @@ -996,7 +997,7 @@ __imx290_get_pad_crop(struct imx290 *imx
157 static int imx290_get_selection(struct v4l2_subdev *sd,
158 - struct v4l2_subdev_pad_config *cfg,
159 + struct v4l2_subdev_state *sd_state,
160 struct v4l2_subdev_selection *sel)
162 switch (sel->target) {
163 @@ -1004,7 +1005,7 @@ static int imx290_get_selection(struct v
164 struct imx290 *imx290 = to_imx290(sd);
166 mutex_lock(&imx290->lock);
167 - sel->r = *__imx290_get_pad_crop(imx290, cfg, sel->pad,
168 + sel->r = *__imx290_get_pad_crop(imx290, sd_state, sel->pad,
170 mutex_unlock(&imx290->lock);
172 --- a/drivers/media/i2c/imx477.c
173 +++ b/drivers/media/i2c/imx477.c
174 @@ -1272,9 +1272,9 @@ static int imx477_open(struct v4l2_subde
176 struct imx477 *imx477 = to_imx477(sd);
177 struct v4l2_mbus_framefmt *try_fmt_img =
178 - v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
179 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
180 struct v4l2_mbus_framefmt *try_fmt_meta =
181 - v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
182 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
183 struct v4l2_rect *try_crop;
185 mutex_lock(&imx477->mutex);
186 @@ -1293,7 +1293,7 @@ static int imx477_open(struct v4l2_subde
187 try_fmt_meta->field = V4L2_FIELD_NONE;
189 /* Initialize try_crop */
190 - try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, IMAGE_PAD);
191 + try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
192 try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
193 try_crop->top = IMX477_PIXEL_ARRAY_TOP;
194 try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
195 @@ -1425,7 +1425,7 @@ static const struct v4l2_ctrl_ops imx477
198 static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
199 - struct v4l2_subdev_pad_config *cfg,
200 + struct v4l2_subdev_state *sd_state,
201 struct v4l2_subdev_mbus_code_enum *code)
203 struct imx477 *imx477 = to_imx477(sd);
204 @@ -1450,7 +1450,7 @@ static int imx477_enum_mbus_code(struct
207 static int imx477_enum_frame_size(struct v4l2_subdev *sd,
208 - struct v4l2_subdev_pad_config *cfg,
209 + struct v4l2_subdev_state *sd_state,
210 struct v4l2_subdev_frame_size_enum *fse)
212 struct imx477 *imx477 = to_imx477(sd);
213 @@ -1516,7 +1516,7 @@ static void imx477_update_metadata_pad_f
216 static int imx477_get_pad_format(struct v4l2_subdev *sd,
217 - struct v4l2_subdev_pad_config *cfg,
218 + struct v4l2_subdev_state *sd_state,
219 struct v4l2_subdev_format *fmt)
221 struct imx477 *imx477 = to_imx477(sd);
222 @@ -1528,7 +1528,8 @@ static int imx477_get_pad_format(struct
224 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
225 struct v4l2_mbus_framefmt *try_fmt =
226 - v4l2_subdev_get_try_format(&imx477->sd, cfg, fmt->pad);
227 + v4l2_subdev_get_try_format(&imx477->sd, sd_state,
229 /* update the code which could change due to vflip or hflip: */
230 try_fmt->code = fmt->pad == IMAGE_PAD ?
231 imx477_get_format_code(imx477, try_fmt->code) :
232 @@ -1593,7 +1594,7 @@ static void imx477_set_framing_limits(st
235 static int imx477_set_pad_format(struct v4l2_subdev *sd,
236 - struct v4l2_subdev_pad_config *cfg,
237 + struct v4l2_subdev_state *sd_state,
238 struct v4l2_subdev_format *fmt)
240 struct v4l2_mbus_framefmt *framefmt;
241 @@ -1622,7 +1623,7 @@ static int imx477_set_pad_format(struct
243 imx477_update_image_pad_format(imx477, mode, fmt);
244 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
245 - framefmt = v4l2_subdev_get_try_format(sd, cfg,
246 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
248 *framefmt = fmt->format;
249 } else if (imx477->mode != mode) {
250 @@ -1632,7 +1633,7 @@ static int imx477_set_pad_format(struct
253 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
254 - framefmt = v4l2_subdev_get_try_format(sd, cfg,
255 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
257 *framefmt = fmt->format;
259 @@ -1647,12 +1648,13 @@ static int imx477_set_pad_format(struct
262 static const struct v4l2_rect *
263 -__imx477_get_pad_crop(struct imx477 *imx477, struct v4l2_subdev_pad_config *cfg,
264 +__imx477_get_pad_crop(struct imx477 *imx477,
265 + struct v4l2_subdev_state *sd_state,
266 unsigned int pad, enum v4l2_subdev_format_whence which)
269 case V4L2_SUBDEV_FORMAT_TRY:
270 - return v4l2_subdev_get_try_crop(&imx477->sd, cfg, pad);
271 + return v4l2_subdev_get_try_crop(&imx477->sd, sd_state, pad);
272 case V4L2_SUBDEV_FORMAT_ACTIVE:
273 return &imx477->mode->crop;
275 @@ -1661,7 +1663,7 @@ __imx477_get_pad_crop(struct imx477 *imx
278 static int imx477_get_selection(struct v4l2_subdev *sd,
279 - struct v4l2_subdev_pad_config *cfg,
280 + struct v4l2_subdev_state *sd_state,
281 struct v4l2_subdev_selection *sel)
283 switch (sel->target) {
284 @@ -1669,7 +1671,7 @@ static int imx477_get_selection(struct v
285 struct imx477 *imx477 = to_imx477(sd);
287 mutex_lock(&imx477->mutex);
288 - sel->r = *__imx477_get_pad_crop(imx477, cfg, sel->pad,
289 + sel->r = *__imx477_get_pad_crop(imx477, sd_state, sel->pad,
291 mutex_unlock(&imx477->mutex);
293 --- a/drivers/media/i2c/irs1125.c
294 +++ b/drivers/media/i2c/irs1125.c
295 @@ -562,8 +562,8 @@ static const struct v4l2_subdev_video_op
298 static int irs1125_enum_mbus_code(struct v4l2_subdev *sd,
299 - struct v4l2_subdev_pad_config *cfg,
300 - struct v4l2_subdev_mbus_code_enum *code)
301 + struct v4l2_subdev_state *sd_state,
302 + struct v4l2_subdev_mbus_code_enum *code)
306 @@ -574,7 +574,7 @@ static int irs1125_enum_mbus_code(struct
309 static int irs1125_set_get_fmt(struct v4l2_subdev *sd,
310 - struct v4l2_subdev_pad_config *cfg,
311 + struct v4l2_subdev_state *sd_state,
312 struct v4l2_subdev_format *format)
314 struct v4l2_mbus_framefmt *fmt = &format->format;
315 @@ -930,7 +930,7 @@ static int irs1125_detect(struct v4l2_su
316 static int irs1125_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
318 struct v4l2_mbus_framefmt *format =
319 - v4l2_subdev_get_try_format(sd, fh->pad, 0);
320 + v4l2_subdev_get_try_format(sd, fh->state, 0);
322 format->code = MEDIA_BUS_FMT_Y12_1X12;
323 format->width = IRS1125_WINDOW_WIDTH_DEF;
324 @@ -1161,7 +1161,7 @@ mutex_remove:
328 -static int irs1125_remove(struct i2c_client *client)
329 +static void irs1125_remove(struct i2c_client *client)
331 struct v4l2_subdev *sd = i2c_get_clientdata(client);
332 struct irs1125 *irs1125 = to_state(sd);
333 @@ -1171,8 +1171,6 @@ static int irs1125_remove(struct i2c_cli
334 v4l2_device_unregister_subdev(sd);
335 mutex_destroy(&irs1125->lock);
336 v4l2_ctrl_handler_free(&irs1125->ctrl_handler);
341 #if IS_ENABLED(CONFIG_OF)
343 +++ b/drivers/media/i2c/ov9281.c
345 +// SPDX-License-Identifier: GPL-2.0
347 + * Omnivision OV9281 1280x800 global shutter image sensor driver
349 + * This driver has been taken from
350 + * https://github.com/rockchip-linux/kernel/blob/develop-4.4/drivers/media/i2c/ov9281.c
351 + * cleaned up, made to compile against mainline kernels instead of the Rockchip
352 + * vendor kernel, and the relevant controls added to work with libcamera.
354 + * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
355 + * V0.0X01.0X02 fix mclk issue when probe multiple camera.
356 + * V0.0X01.0X03 add enum_frame_interval function.
359 +#include <linux/clk.h>
360 +#include <linux/device.h>
361 +#include <linux/delay.h>
362 +#include <linux/gpio/consumer.h>
363 +#include <linux/i2c.h>
364 +#include <linux/module.h>
365 +#include <linux/pm_runtime.h>
366 +#include <linux/regulator/consumer.h>
367 +#include <linux/sysfs.h>
368 +#include <linux/slab.h>
369 +#include <media/media-entity.h>
370 +#include <media/v4l2-async.h>
371 +#include <media/v4l2-ctrls.h>
372 +#include <media/v4l2-fwnode.h>
373 +#include <media/v4l2-subdev.h>
375 +#define OV9281_LINK_FREQ_400MHZ 400000000
376 +#define OV9281_LANES 2
378 +/* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
379 +#define OV9281_PIXEL_RATE_10BIT (OV9281_LINK_FREQ_400MHZ * 2 * \
381 +#define OV9281_PIXEL_RATE_8BIT (OV9281_LINK_FREQ_400MHZ * 2 * \
383 +#define OV9281_XVCLK_FREQ 24000000
385 +#define CHIP_ID 0x9281
386 +#define OV9281_REG_CHIP_ID 0x300a
388 +#define OV9281_REG_TIMING_FORMAT_1 0x3820
389 +#define OV9281_REG_TIMING_FORMAT_2 0x3821
390 +#define OV9281_FLIP_BIT BIT(2)
392 +#define OV9281_REG_CTRL_MODE 0x0100
393 +#define OV9281_MODE_SW_STANDBY 0x0
394 +#define OV9281_MODE_STREAMING BIT(0)
396 +#define OV9281_REG_EXPOSURE 0x3500
397 +#define OV9281_EXPOSURE_MIN 4
398 +#define OV9281_EXPOSURE_STEP 1
399 +#define OV9281_VTS_MAX 0x7fff
401 +#define OV9281_REG_GAIN_H 0x3508
402 +#define OV9281_REG_GAIN_L 0x3509
403 +#define OV9281_GAIN_H_MASK 0x07
404 +#define OV9281_GAIN_H_SHIFT 8
405 +#define OV9281_GAIN_L_MASK 0xff
406 +#define OV9281_GAIN_MIN 0x10
407 +#define OV9281_GAIN_MAX 0xf8
408 +#define OV9281_GAIN_STEP 1
409 +#define OV9281_GAIN_DEFAULT 0x10
411 +#define OV9281_REG_TEST_PATTERN 0x5e00
412 +#define OV9281_TEST_PATTERN_ENABLE 0x80
413 +#define OV9281_TEST_PATTERN_DISABLE 0x0
415 +#define OV9281_REG_VTS 0x380e
418 + * OV9281 native and active pixel array size.
419 + * Datasheet not available to confirm these values, so assume there are no
422 +#define OV9281_NATIVE_WIDTH 1280U
423 +#define OV9281_NATIVE_HEIGHT 800U
424 +#define OV9281_PIXEL_ARRAY_LEFT 0U
425 +#define OV9281_PIXEL_ARRAY_TOP 0U
426 +#define OV9281_PIXEL_ARRAY_WIDTH 1280U
427 +#define OV9281_PIXEL_ARRAY_HEIGHT 800U
429 +#define REG_NULL 0xFFFF
431 +#define OV9281_REG_VALUE_08BIT 1
432 +#define OV9281_REG_VALUE_16BIT 2
433 +#define OV9281_REG_VALUE_24BIT 3
435 +#define OV9281_NAME "ov9281"
437 +static const char * const ov9281_supply_names[] = {
438 + "avdd", /* Analog power */
439 + "dovdd", /* Digital I/O power */
440 + "dvdd", /* Digital core power */
443 +#define OV9281_NUM_SUPPLIES ARRAY_SIZE(ov9281_supply_names)
450 +struct ov9281_mode {
456 + struct v4l2_rect crop;
457 + const struct regval *reg_list;
461 + struct i2c_client *client;
463 + struct gpio_desc *reset_gpio;
464 + struct gpio_desc *pwdn_gpio;
465 + struct regulator_bulk_data supplies[OV9281_NUM_SUPPLIES];
467 + struct v4l2_subdev subdev;
468 + struct media_pad pad;
469 + struct v4l2_ctrl_handler ctrl_handler;
470 + struct v4l2_ctrl *exposure;
471 + struct v4l2_ctrl *anal_gain;
472 + struct v4l2_ctrl *digi_gain;
473 + struct v4l2_ctrl *hblank;
474 + struct v4l2_ctrl *vblank;
475 + struct v4l2_ctrl *hflip;
476 + struct v4l2_ctrl *vflip;
477 + struct v4l2_ctrl *pixel_rate;
478 + struct v4l2_ctrl *test_pattern;
479 + struct mutex mutex;
482 + const struct ov9281_mode *cur_mode;
486 +#define to_ov9281(sd) container_of(sd, struct ov9281, subdev)
490 + * max_framerate 120fps for 10 bit, 144fps for 8 bit.
491 + * mipi_datarate per lane 800Mbps
493 +static const struct regval ov9281_common_regs[] = {
558 +static const struct regval ov9281_1280x800_regs[] = {
594 +static const struct regval ov9281_1280x720_regs[] = {
630 +static const struct regval ov9281_640x400_regs[] = {
665 +static const struct regval op_10bit[] = {
671 +static const struct regval op_8bit[] = {
677 +static const struct ov9281_mode supported_modes[] = {
682 + .hts_def = 0x05b0, /* 0x2d8*2 */
690 + .reg_list = ov9281_1280x800_regs,
704 + .reg_list = ov9281_1280x720_regs,
718 + .reg_list = ov9281_640x400_regs,
722 +static const s64 link_freq_menu_items[] = {
723 + OV9281_LINK_FREQ_400MHZ
726 +static const char * const ov9281_test_pattern_menu[] = {
728 + "Vertical Color Bar Type 1",
729 + "Vertical Color Bar Type 2",
730 + "Vertical Color Bar Type 3",
731 + "Vertical Color Bar Type 4"
734 +/* Write registers up to 4 at a time */
735 +static int ov9281_write_reg(struct i2c_client *client, u16 reg,
747 + buf[1] = reg & 0xff;
749 + val_be = cpu_to_be32(val);
750 + val_p = (u8 *)&val_be;
755 + buf[buf_i++] = val_p[val_i++];
757 + if (i2c_master_send(client, buf, len + 2) != len + 2)
763 +static int ov9281_write_array(struct i2c_client *client,
764 + const struct regval *regs)
769 + for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
770 + ret = ov9281_write_reg(client, regs[i].addr,
771 + OV9281_REG_VALUE_08BIT, regs[i].val);
776 +/* Read registers up to 4 at a time */
777 +static int ov9281_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
780 + struct i2c_msg msgs[2];
782 + __be32 data_be = 0;
783 + __be16 reg_addr_be = cpu_to_be16(reg);
786 + if (len > 4 || !len)
789 + data_be_p = (u8 *)&data_be;
790 + /* Write register address */
791 + msgs[0].addr = client->addr;
794 + msgs[0].buf = (u8 *)®_addr_be;
796 + /* Read data from register */
797 + msgs[1].addr = client->addr;
798 + msgs[1].flags = I2C_M_RD;
800 + msgs[1].buf = &data_be_p[4 - len];
802 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
803 + if (ret != ARRAY_SIZE(msgs))
806 + *val = be32_to_cpu(data_be);
811 +static int ov9281_get_reso_dist(const struct ov9281_mode *mode,
812 + struct v4l2_mbus_framefmt *framefmt)
814 + return abs(mode->width - framefmt->width) +
815 + abs(mode->height - framefmt->height);
818 +static const struct ov9281_mode *
819 +ov9281_find_best_fit(struct v4l2_subdev_format *fmt)
821 + struct v4l2_mbus_framefmt *framefmt = &fmt->format;
823 + int cur_best_fit = 0;
824 + int cur_best_fit_dist = -1;
827 + for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
828 + dist = ov9281_get_reso_dist(&supported_modes[i], framefmt);
829 + if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
830 + cur_best_fit_dist = dist;
835 + return &supported_modes[cur_best_fit];
838 +static int ov9281_set_fmt(struct v4l2_subdev *sd,
839 + struct v4l2_subdev_state *sd_state,
840 + struct v4l2_subdev_format *fmt)
842 + struct ov9281 *ov9281 = to_ov9281(sd);
843 + const struct ov9281_mode *mode;
844 + s64 h_blank, vblank_def, pixel_rate;
846 + mutex_lock(&ov9281->mutex);
848 + mode = ov9281_find_best_fit(fmt);
849 + if (fmt->format.code != MEDIA_BUS_FMT_Y8_1X8)
850 + fmt->format.code = MEDIA_BUS_FMT_Y10_1X10;
851 + fmt->format.width = mode->width;
852 + fmt->format.height = mode->height;
853 + fmt->format.field = V4L2_FIELD_NONE;
854 + fmt->format.colorspace = V4L2_COLORSPACE_RAW;
855 + fmt->format.ycbcr_enc =
856 + V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
857 + fmt->format.quantization =
858 + V4L2_MAP_QUANTIZATION_DEFAULT(true, fmt->format.colorspace,
859 + fmt->format.ycbcr_enc);
860 + fmt->format.xfer_func =
861 + V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
863 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
864 + *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
866 + ov9281->cur_mode = mode;
867 + ov9281->code = fmt->format.code;
868 + h_blank = mode->hts_def - mode->width;
869 + __v4l2_ctrl_modify_range(ov9281->hblank, h_blank,
870 + h_blank, 1, h_blank);
871 + __v4l2_ctrl_s_ctrl(ov9281->hblank, h_blank);
872 + vblank_def = mode->vts_def - mode->height;
873 + __v4l2_ctrl_modify_range(ov9281->vblank, vblank_def,
874 + OV9281_VTS_MAX - mode->height,
876 + __v4l2_ctrl_s_ctrl(ov9281->vblank, vblank_def);
878 + pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
879 + OV9281_PIXEL_RATE_10BIT : OV9281_PIXEL_RATE_8BIT;
880 + __v4l2_ctrl_modify_range(ov9281->pixel_rate, pixel_rate,
881 + pixel_rate, 1, pixel_rate);
884 + mutex_unlock(&ov9281->mutex);
889 +static int ov9281_get_fmt(struct v4l2_subdev *sd,
890 + struct v4l2_subdev_state *sd_state,
891 + struct v4l2_subdev_format *fmt)
893 + struct ov9281 *ov9281 = to_ov9281(sd);
894 + const struct ov9281_mode *mode = ov9281->cur_mode;
896 + mutex_lock(&ov9281->mutex);
897 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
898 + fmt->format = *v4l2_subdev_get_try_format(sd, sd_state,
901 + fmt->format.width = mode->width;
902 + fmt->format.height = mode->height;
903 + fmt->format.code = ov9281->code;
904 + fmt->format.field = V4L2_FIELD_NONE;
905 + fmt->format.colorspace = V4L2_COLORSPACE_RAW;
906 + fmt->format.ycbcr_enc =
907 + V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
908 + fmt->format.quantization =
909 + V4L2_MAP_QUANTIZATION_DEFAULT(true,
910 + fmt->format.colorspace,
911 + fmt->format.ycbcr_enc);
912 + fmt->format.xfer_func =
913 + V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
915 + mutex_unlock(&ov9281->mutex);
920 +static int ov9281_enum_mbus_code(struct v4l2_subdev *sd,
921 + struct v4l2_subdev_state *sd_state,
922 + struct v4l2_subdev_mbus_code_enum *code)
924 + switch (code->index) {
928 + code->code = MEDIA_BUS_FMT_Y10_1X10;
931 + code->code = MEDIA_BUS_FMT_Y8_1X8;
938 +static int ov9281_enum_frame_sizes(struct v4l2_subdev *sd,
939 + struct v4l2_subdev_state *sd_state,
940 + struct v4l2_subdev_frame_size_enum *fse)
942 + if (fse->index >= ARRAY_SIZE(supported_modes))
945 + if (fse->code != MEDIA_BUS_FMT_Y10_1X10 &&
946 + fse->code != MEDIA_BUS_FMT_Y8_1X8)
949 + fse->min_width = supported_modes[fse->index].width;
950 + fse->max_width = supported_modes[fse->index].width;
951 + fse->max_height = supported_modes[fse->index].height;
952 + fse->min_height = supported_modes[fse->index].height;
957 +static int ov9281_enable_test_pattern(struct ov9281 *ov9281, u32 pattern)
962 + val = (pattern - 1) | OV9281_TEST_PATTERN_ENABLE;
964 + val = OV9281_TEST_PATTERN_DISABLE;
966 + return ov9281_write_reg(ov9281->client, OV9281_REG_TEST_PATTERN,
967 + OV9281_REG_VALUE_08BIT, val);
970 +static int ov9281_set_ctrl_hflip(struct ov9281 *ov9281, int value)
973 + int ret = ov9281_read_reg(ov9281->client, OV9281_REG_TIMING_FORMAT_2,
974 + OV9281_REG_VALUE_08BIT, ¤t_val);
977 + current_val |= OV9281_FLIP_BIT;
979 + current_val &= ~OV9281_FLIP_BIT;
980 + return ov9281_write_reg(ov9281->client,
981 + OV9281_REG_TIMING_FORMAT_2,
982 + OV9281_REG_VALUE_08BIT,
988 +static int ov9281_set_ctrl_vflip(struct ov9281 *ov9281, int value)
991 + int ret = ov9281_read_reg(ov9281->client, OV9281_REG_TIMING_FORMAT_1,
992 + OV9281_REG_VALUE_08BIT, ¤t_val);
995 + current_val |= OV9281_FLIP_BIT;
997 + current_val &= ~OV9281_FLIP_BIT;
998 + return ov9281_write_reg(ov9281->client,
999 + OV9281_REG_TIMING_FORMAT_1,
1000 + OV9281_REG_VALUE_08BIT,
1006 +static const struct v4l2_rect *
1007 +__ov9281_get_pad_crop(struct ov9281 *ov9281,
1008 + struct v4l2_subdev_state *sd_state,
1009 + unsigned int pad, enum v4l2_subdev_format_whence which)
1012 + case V4L2_SUBDEV_FORMAT_TRY:
1013 + return v4l2_subdev_get_try_crop(&ov9281->subdev, sd_state,
1015 + case V4L2_SUBDEV_FORMAT_ACTIVE:
1016 + return &ov9281->cur_mode->crop;
1022 +static int ov9281_get_selection(struct v4l2_subdev *sd,
1023 + struct v4l2_subdev_state *sd_state,
1024 + struct v4l2_subdev_selection *sel)
1026 + switch (sel->target) {
1027 + case V4L2_SEL_TGT_CROP: {
1028 + struct ov9281 *ov9281 = to_ov9281(sd);
1030 + mutex_lock(&ov9281->mutex);
1031 + sel->r = *__ov9281_get_pad_crop(ov9281, sd_state, sel->pad,
1033 + mutex_unlock(&ov9281->mutex);
1038 + case V4L2_SEL_TGT_NATIVE_SIZE:
1041 + sel->r.width = OV9281_NATIVE_WIDTH;
1042 + sel->r.height = OV9281_NATIVE_HEIGHT;
1046 + case V4L2_SEL_TGT_CROP_DEFAULT:
1047 + case V4L2_SEL_TGT_CROP_BOUNDS:
1048 + sel->r.top = OV9281_PIXEL_ARRAY_TOP;
1049 + sel->r.left = OV9281_PIXEL_ARRAY_LEFT;
1050 + sel->r.width = OV9281_PIXEL_ARRAY_WIDTH;
1051 + sel->r.height = OV9281_PIXEL_ARRAY_HEIGHT;
1059 +static int __ov9281_start_stream(struct ov9281 *ov9281)
1063 + ret = ov9281_write_array(ov9281->client, ov9281_common_regs);
1067 + ret = ov9281_write_array(ov9281->client, ov9281->cur_mode->reg_list);
1071 + if (ov9281->code == MEDIA_BUS_FMT_Y10_1X10)
1072 + ret = ov9281_write_array(ov9281->client, op_10bit);
1074 + ret = ov9281_write_array(ov9281->client, op_8bit);
1078 + /* In case these controls are set before streaming */
1079 + mutex_unlock(&ov9281->mutex);
1080 + ret = v4l2_ctrl_handler_setup(&ov9281->ctrl_handler);
1081 + mutex_lock(&ov9281->mutex);
1085 + return ov9281_write_reg(ov9281->client, OV9281_REG_CTRL_MODE,
1086 + OV9281_REG_VALUE_08BIT, OV9281_MODE_STREAMING);
1089 +static int __ov9281_stop_stream(struct ov9281 *ov9281)
1091 + return ov9281_write_reg(ov9281->client, OV9281_REG_CTRL_MODE,
1092 + OV9281_REG_VALUE_08BIT, OV9281_MODE_SW_STANDBY);
1095 +static int ov9281_s_stream(struct v4l2_subdev *sd, int on)
1097 + struct ov9281 *ov9281 = to_ov9281(sd);
1098 + struct i2c_client *client = ov9281->client;
1101 + mutex_lock(&ov9281->mutex);
1103 + if (on == ov9281->streaming)
1104 + goto unlock_and_return;
1107 + ret = pm_runtime_get_sync(&client->dev);
1109 + pm_runtime_put_noidle(&client->dev);
1110 + goto unlock_and_return;
1113 + ret = __ov9281_start_stream(ov9281);
1115 + v4l2_err(sd, "start stream failed while write regs\n");
1116 + pm_runtime_put(&client->dev);
1117 + goto unlock_and_return;
1120 + __ov9281_stop_stream(ov9281);
1121 + pm_runtime_put(&client->dev);
1124 + ov9281->streaming = on;
1127 + mutex_unlock(&ov9281->mutex);
1132 +static int ov9281_s_power(struct v4l2_subdev *sd, int on)
1134 + struct ov9281 *ov9281 = to_ov9281(sd);
1135 + struct i2c_client *client = ov9281->client;
1138 + mutex_lock(&ov9281->mutex);
1140 + /* If the power state is not modified - no work to do. */
1141 + if (ov9281->power_on == !!on)
1142 + goto unlock_and_return;
1145 + ret = pm_runtime_get_sync(&client->dev);
1147 + pm_runtime_put_noidle(&client->dev);
1148 + goto unlock_and_return;
1150 + ov9281->power_on = true;
1152 + pm_runtime_put(&client->dev);
1153 + ov9281->power_on = false;
1157 + mutex_unlock(&ov9281->mutex);
1162 +/* Calculate the delay in us by clock rate and clock cycles */
1163 +static inline u32 ov9281_cal_delay(u32 cycles)
1165 + return DIV_ROUND_UP(cycles, OV9281_XVCLK_FREQ / 1000 / 1000);
1168 +static int __ov9281_power_on(struct ov9281 *ov9281)
1172 + struct device *dev = &ov9281->client->dev;
1174 + ret = clk_set_rate(ov9281->xvclk, OV9281_XVCLK_FREQ);
1176 + dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
1177 + if (clk_get_rate(ov9281->xvclk) != OV9281_XVCLK_FREQ)
1178 + dev_warn(dev, "xvclk mismatched, modes are based on 24MHz - rate is %lu\n",
1179 + clk_get_rate(ov9281->xvclk));
1181 + ret = clk_prepare_enable(ov9281->xvclk);
1183 + dev_err(dev, "Failed to enable xvclk\n");
1187 + if (!IS_ERR(ov9281->reset_gpio))
1188 + gpiod_set_value_cansleep(ov9281->reset_gpio, 0);
1190 + ret = regulator_bulk_enable(OV9281_NUM_SUPPLIES, ov9281->supplies);
1192 + dev_err(dev, "Failed to enable regulators\n");
1196 + if (!IS_ERR(ov9281->reset_gpio))
1197 + gpiod_set_value_cansleep(ov9281->reset_gpio, 1);
1199 + usleep_range(500, 1000);
1200 + if (!IS_ERR(ov9281->pwdn_gpio))
1201 + gpiod_set_value_cansleep(ov9281->pwdn_gpio, 1);
1203 + /* 8192 cycles prior to first SCCB transaction */
1204 + delay_us = ov9281_cal_delay(8192);
1205 + usleep_range(delay_us, delay_us * 2);
1210 + clk_disable_unprepare(ov9281->xvclk);
1215 +static void __ov9281_power_off(struct ov9281 *ov9281)
1217 + if (!IS_ERR(ov9281->pwdn_gpio))
1218 + gpiod_set_value_cansleep(ov9281->pwdn_gpio, 0);
1219 + clk_disable_unprepare(ov9281->xvclk);
1220 + if (!IS_ERR(ov9281->reset_gpio))
1221 + gpiod_set_value_cansleep(ov9281->reset_gpio, 0);
1222 + regulator_bulk_disable(OV9281_NUM_SUPPLIES, ov9281->supplies);
1225 +static int ov9281_runtime_resume(struct device *dev)
1227 + struct i2c_client *client = to_i2c_client(dev);
1228 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1229 + struct ov9281 *ov9281 = to_ov9281(sd);
1231 + return __ov9281_power_on(ov9281);
1234 +static int ov9281_runtime_suspend(struct device *dev)
1236 + struct i2c_client *client = to_i2c_client(dev);
1237 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1238 + struct ov9281 *ov9281 = to_ov9281(sd);
1240 + __ov9281_power_off(ov9281);
1245 +static int ov9281_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1247 + struct ov9281 *ov9281 = to_ov9281(sd);
1248 + struct v4l2_mbus_framefmt *try_fmt =
1249 + v4l2_subdev_get_try_format(sd, fh->state, 0);
1250 + const struct ov9281_mode *def_mode = &supported_modes[0];
1252 + mutex_lock(&ov9281->mutex);
1253 + /* Initialize try_fmt */
1254 + try_fmt->width = def_mode->width;
1255 + try_fmt->height = def_mode->height;
1256 + try_fmt->code = MEDIA_BUS_FMT_Y10_1X10;
1257 + try_fmt->field = V4L2_FIELD_NONE;
1258 + try_fmt->colorspace = V4L2_COLORSPACE_RAW;
1259 + try_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(try_fmt->colorspace);
1260 + try_fmt->quantization =
1261 + V4L2_MAP_QUANTIZATION_DEFAULT(true, try_fmt->colorspace,
1262 + try_fmt->ycbcr_enc);
1263 + try_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(try_fmt->colorspace);
1265 + mutex_unlock(&ov9281->mutex);
1266 + /* No crop or compose */
1271 +static const struct dev_pm_ops ov9281_pm_ops = {
1272 + SET_RUNTIME_PM_OPS(ov9281_runtime_suspend,
1273 + ov9281_runtime_resume, NULL)
1276 +static const struct v4l2_subdev_internal_ops ov9281_internal_ops = {
1277 + .open = ov9281_open,
1280 +static const struct v4l2_subdev_core_ops ov9281_core_ops = {
1281 + .s_power = ov9281_s_power,
1284 +static const struct v4l2_subdev_video_ops ov9281_video_ops = {
1285 + .s_stream = ov9281_s_stream,
1288 +static const struct v4l2_subdev_pad_ops ov9281_pad_ops = {
1289 + .enum_mbus_code = ov9281_enum_mbus_code,
1290 + .enum_frame_size = ov9281_enum_frame_sizes,
1291 + .get_fmt = ov9281_get_fmt,
1292 + .set_fmt = ov9281_set_fmt,
1293 + .get_selection = ov9281_get_selection,
1296 +static const struct v4l2_subdev_ops ov9281_subdev_ops = {
1297 + .core = &ov9281_core_ops,
1298 + .video = &ov9281_video_ops,
1299 + .pad = &ov9281_pad_ops,
1302 +static int ov9281_set_ctrl(struct v4l2_ctrl *ctrl)
1304 + struct ov9281 *ov9281 = container_of(ctrl->handler,
1305 + struct ov9281, ctrl_handler);
1306 + struct i2c_client *client = ov9281->client;
1310 + /* Propagate change of current control to all related controls */
1311 + switch (ctrl->id) {
1312 + case V4L2_CID_VBLANK:
1313 + /* Update max exposure while meeting expected vblanking */
1314 + max = ov9281->cur_mode->height + ctrl->val - 4;
1315 + __v4l2_ctrl_modify_range(ov9281->exposure,
1316 + ov9281->exposure->minimum, max,
1317 + ov9281->exposure->step,
1318 + ov9281->exposure->default_value);
1322 + if (pm_runtime_get(&client->dev) <= 0)
1325 + switch (ctrl->id) {
1326 + case V4L2_CID_HFLIP:
1327 + ret = ov9281_set_ctrl_hflip(ov9281, ctrl->val);
1329 + case V4L2_CID_VFLIP:
1330 + ret = ov9281_set_ctrl_vflip(ov9281, ctrl->val);
1332 + case V4L2_CID_EXPOSURE:
1333 + /* 4 least significant bits of expsoure are fractional part */
1334 + ret = ov9281_write_reg(ov9281->client, OV9281_REG_EXPOSURE,
1335 + OV9281_REG_VALUE_24BIT, ctrl->val << 4);
1337 + case V4L2_CID_ANALOGUE_GAIN:
1338 + ret = ov9281_write_reg(ov9281->client, OV9281_REG_GAIN_H,
1339 + OV9281_REG_VALUE_08BIT,
1340 + (ctrl->val >> OV9281_GAIN_H_SHIFT) &
1341 + OV9281_GAIN_H_MASK);
1342 + ret |= ov9281_write_reg(ov9281->client, OV9281_REG_GAIN_L,
1343 + OV9281_REG_VALUE_08BIT,
1344 + ctrl->val & OV9281_GAIN_L_MASK);
1346 + case V4L2_CID_VBLANK:
1347 + ret = ov9281_write_reg(ov9281->client, OV9281_REG_VTS,
1348 + OV9281_REG_VALUE_16BIT,
1349 + ctrl->val + ov9281->cur_mode->height);
1351 + case V4L2_CID_TEST_PATTERN:
1352 + ret = ov9281_enable_test_pattern(ov9281, ctrl->val);
1355 + dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
1356 + __func__, ctrl->id, ctrl->val);
1360 + pm_runtime_put(&client->dev);
1365 +static const struct v4l2_ctrl_ops ov9281_ctrl_ops = {
1366 + .s_ctrl = ov9281_set_ctrl,
1369 +static int ov9281_initialize_controls(struct ov9281 *ov9281)
1371 + struct v4l2_fwnode_device_properties props;
1372 + const struct ov9281_mode *mode;
1373 + struct v4l2_ctrl_handler *handler;
1374 + struct v4l2_ctrl *ctrl;
1375 + s64 exposure_max, vblank_def;
1379 + handler = &ov9281->ctrl_handler;
1380 + mode = ov9281->cur_mode;
1381 + ret = v4l2_ctrl_handler_init(handler, 11);
1384 + handler->lock = &ov9281->mutex;
1386 + ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
1387 + 0, 0, link_freq_menu_items);
1389 + ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1391 + ov9281->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
1392 + V4L2_CID_PIXEL_RATE,
1393 + OV9281_PIXEL_RATE_10BIT,
1394 + OV9281_PIXEL_RATE_10BIT, 1,
1395 + OV9281_PIXEL_RATE_10BIT);
1397 + h_blank = mode->hts_def - mode->width;
1398 + ov9281->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
1399 + h_blank, h_blank, 1, h_blank);
1400 + if (ov9281->hblank)
1401 + ov9281->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1403 + vblank_def = mode->vts_def - mode->height;
1404 + ov9281->vblank = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
1405 + V4L2_CID_VBLANK, vblank_def,
1406 + OV9281_VTS_MAX - mode->height, 1,
1409 + exposure_max = mode->vts_def - 4;
1410 + ov9281->exposure = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
1411 + V4L2_CID_EXPOSURE,
1412 + OV9281_EXPOSURE_MIN, exposure_max,
1413 + OV9281_EXPOSURE_STEP,
1416 + ov9281->anal_gain = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
1417 + V4L2_CID_ANALOGUE_GAIN,
1418 + OV9281_GAIN_MIN, OV9281_GAIN_MAX,
1420 + OV9281_GAIN_DEFAULT);
1422 + ov9281->vflip = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
1426 + ov9281->hflip = v4l2_ctrl_new_std(handler, &ov9281_ctrl_ops,
1430 + ov9281->test_pattern =
1431 + v4l2_ctrl_new_std_menu_items(handler, &ov9281_ctrl_ops,
1432 + V4L2_CID_TEST_PATTERN,
1433 + ARRAY_SIZE(ov9281_test_pattern_menu) - 1,
1434 + 0, 0, ov9281_test_pattern_menu);
1436 + if (handler->error) {
1437 + ret = handler->error;
1438 + dev_err(&ov9281->client->dev,
1439 + "Failed to init controls(%d)\n", ret);
1440 + goto err_free_handler;
1443 + ret = v4l2_fwnode_device_parse(&ov9281->client->dev, &props);
1445 + goto err_free_handler;
1447 + ret = v4l2_ctrl_new_fwnode_properties(handler, &ov9281_ctrl_ops,
1450 + goto err_free_handler;
1452 + ov9281->subdev.ctrl_handler = handler;
1457 + v4l2_ctrl_handler_free(handler);
1462 +static int ov9281_check_sensor_id(struct ov9281 *ov9281,
1463 + struct i2c_client *client)
1465 + struct device *dev = &ov9281->client->dev;
1466 + u32 id = 0, id_msb;
1469 + ret = ov9281_read_reg(client, OV9281_REG_CHIP_ID + 1,
1470 + OV9281_REG_VALUE_08BIT, &id);
1472 + ret = ov9281_read_reg(client, OV9281_REG_CHIP_ID,
1473 + OV9281_REG_VALUE_08BIT, &id_msb);
1474 + id |= (id_msb << 8);
1475 + if (ret || id != CHIP_ID) {
1476 + dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
1480 + dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
1485 +static int ov9281_configure_regulators(struct ov9281 *ov9281)
1489 + for (i = 0; i < OV9281_NUM_SUPPLIES; i++)
1490 + ov9281->supplies[i].supply = ov9281_supply_names[i];
1492 + return devm_regulator_bulk_get(&ov9281->client->dev,
1493 + OV9281_NUM_SUPPLIES,
1494 + ov9281->supplies);
1497 +static int ov9281_probe(struct i2c_client *client,
1498 + const struct i2c_device_id *id)
1500 + struct device *dev = &client->dev;
1501 + struct ov9281 *ov9281;
1502 + struct v4l2_subdev *sd;
1505 + ov9281 = devm_kzalloc(dev, sizeof(*ov9281), GFP_KERNEL);
1509 + ov9281->client = client;
1510 + ov9281->cur_mode = &supported_modes[0];
1512 + ov9281->xvclk = devm_clk_get(dev, "xvclk");
1513 + if (IS_ERR(ov9281->xvclk)) {
1514 + dev_err(dev, "Failed to get xvclk\n");
1518 + ov9281->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1520 + if (IS_ERR(ov9281->reset_gpio))
1521 + dev_warn(dev, "Failed to get reset-gpios\n");
1523 + ov9281->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_LOW);
1524 + if (IS_ERR(ov9281->pwdn_gpio))
1525 + dev_warn(dev, "Failed to get pwdn-gpios\n");
1527 + ret = ov9281_configure_regulators(ov9281);
1529 + dev_err(dev, "Failed to get power regulators\n");
1533 + mutex_init(&ov9281->mutex);
1535 + sd = &ov9281->subdev;
1536 + v4l2_i2c_subdev_init(sd, client, &ov9281_subdev_ops);
1537 + ret = ov9281_initialize_controls(ov9281);
1539 + goto err_destroy_mutex;
1541 + ret = __ov9281_power_on(ov9281);
1543 + goto err_free_handler;
1545 + ret = ov9281_check_sensor_id(ov9281, client);
1547 + goto err_power_off;
1549 + sd->internal_ops = &ov9281_internal_ops;
1550 + sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1552 + ov9281->pad.flags = MEDIA_PAD_FL_SOURCE;
1553 + sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1554 + ret = media_entity_pads_init(&sd->entity, 1, &ov9281->pad);
1556 + goto err_power_off;
1558 + ret = v4l2_async_register_subdev_sensor(sd);
1560 + dev_err(dev, "v4l2 async register subdev failed\n");
1561 + goto err_clean_entity;
1564 + pm_runtime_set_active(dev);
1565 + pm_runtime_enable(dev);
1566 + pm_runtime_idle(dev);
1571 + media_entity_cleanup(&sd->entity);
1573 + __ov9281_power_off(ov9281);
1575 + v4l2_ctrl_handler_free(&ov9281->ctrl_handler);
1577 + mutex_destroy(&ov9281->mutex);
1582 +static void ov9281_remove(struct i2c_client *client)
1584 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1585 + struct ov9281 *ov9281 = to_ov9281(sd);
1587 + v4l2_async_unregister_subdev(sd);
1588 + media_entity_cleanup(&sd->entity);
1589 + v4l2_ctrl_handler_free(&ov9281->ctrl_handler);
1590 + mutex_destroy(&ov9281->mutex);
1592 + pm_runtime_disable(&client->dev);
1593 + if (!pm_runtime_status_suspended(&client->dev))
1594 + __ov9281_power_off(ov9281);
1595 + pm_runtime_set_suspended(&client->dev);
1598 +static const struct of_device_id ov9281_of_match[] = {
1599 + { .compatible = "ovti,ov9281" },
1602 +MODULE_DEVICE_TABLE(of, ov9281_of_match);
1604 +static const struct i2c_device_id ov9281_match_id[] = {
1605 + { "ovti,ov9281", 0 },
1609 +static struct i2c_driver ov9281_i2c_driver = {
1611 + .name = OV9281_NAME,
1612 + .pm = &ov9281_pm_ops,
1613 + .of_match_table = of_match_ptr(ov9281_of_match),
1615 + .probe = &ov9281_probe,
1616 + .remove = &ov9281_remove,
1617 + .id_table = ov9281_match_id,
1620 +static int __init sensor_mod_init(void)
1622 + return i2c_add_driver(&ov9281_i2c_driver);
1625 +static void __exit sensor_mod_exit(void)
1627 + i2c_del_driver(&ov9281_i2c_driver);
1630 +device_initcall_sync(sensor_mod_init);
1631 +module_exit(sensor_mod_exit);
1633 +MODULE_DESCRIPTION("OmniVision ov9281 sensor driver");
1634 +MODULE_LICENSE("GPL v2");
1635 --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
1636 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
1637 @@ -443,7 +443,7 @@ struct unicam_device {
1638 /* ptr to sub device */
1639 struct v4l2_subdev *sensor;
1640 /* Pad config for the sensor */
1641 - struct v4l2_subdev_pad_config *sensor_config;
1642 + struct v4l2_subdev_state *sensor_state;
1644 enum v4l2_mbus_type bus_type;
1646 @@ -594,7 +594,7 @@ static int __subdev_get_format(struct un
1650 - ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
1651 + ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_state,
1655 @@ -618,7 +618,7 @@ static int __subdev_set_format(struct un
1657 sd_fmt.format = *fmt;
1659 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
1660 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_state,
1664 @@ -1094,7 +1094,7 @@ static int unicam_try_fmt_vid_cap(struct
1666 mbus_fmt->field = V4L2_FIELD_NONE;
1668 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
1669 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_state,
1671 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1673 @@ -1116,7 +1116,7 @@ static int unicam_try_fmt_vid_cap(struct
1674 mbus_fmt->code = fmt->code;
1676 ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
1677 - dev->sensor_config, &sd_fmt);
1678 + dev->sensor_state, &sd_fmt);
1679 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1682 @@ -2320,8 +2320,8 @@ static void unicam_release(struct kref *
1683 v4l2_ctrl_handler_free(&unicam->ctrl_handler);
1684 media_device_cleanup(&unicam->mdev);
1686 - if (unicam->sensor_config)
1687 - v4l2_subdev_free_pad_config(unicam->sensor_config);
1688 + if (unicam->sensor_state)
1689 + __v4l2_subdev_state_free(unicam->sensor_state);
1693 @@ -2579,12 +2579,14 @@ static void unregister_nodes(struct unic
1695 static int unicam_probe_complete(struct unicam_device *unicam)
1697 + static struct lock_class_key key;
1700 unicam->v4l2_dev.notify = unicam_notify;
1702 - unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
1703 - if (!unicam->sensor_config)
1704 + unicam->sensor_state = __v4l2_subdev_state_alloc(unicam->sensor,
1705 + "unicam:async->lock", &key);
1706 + if (!unicam->sensor_state)
1709 unicam->sensor_embedded_data = (unicam->sensor->entity.num_pads >= 2);
1710 --- a/drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
1711 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
1712 @@ -1465,7 +1465,7 @@ queue_cleanup:
1715 /* Unregister one of the /dev/video<N> nodes associated with the ISP. */
1716 -static void unregister_node(struct bcm2835_isp_node *node)
1717 +static void bcm2835_unregister_node(struct bcm2835_isp_node *node)
1719 struct bcm2835_isp_dev *dev = node_get_dev(node);
1721 @@ -1668,7 +1668,7 @@ static int bcm2835_isp_remove(struct pla
1722 media_controller_unregister(dev);
1724 for (i = 0; i < BCM2835_ISP_NUM_NODES; i++)
1725 - unregister_node(&dev->node[i]);
1726 + bcm2835_unregister_node(&dev->node[i]);
1728 v4l2_device_unregister(&dev->v4l2_dev);