1 From 3be790df5d506e21cc729cc0646a16dfebe01aa1 Mon Sep 17 00:00:00 2001
2 From: Lee Jackson <info@arducam.com>
3 Date: Thu, 14 Apr 2022 17:31:01 +0800
4 Subject: [PATCH] media: i2c: Add driver of Arducam Pivariety series
7 Add a driver for the Arducam Pivariety series CSI2 camera sensor.
9 Signed-off-by: Lee Jackson <info@arducam.com>
11 SQUASH: Fix VIDEO_ARDUCAM_PIVARIETY Kconfig entry
13 The cherry-pick from rpi-5.17.y put it in the wrong section, and failed
14 to update it for 5.18.
16 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
18 drivers/media/i2c/Kconfig | 11 +
19 drivers/media/i2c/Makefile | 1 +
20 drivers/media/i2c/arducam-pivariety.c | 1467 +++++++++++++++++++++++++
21 drivers/media/i2c/arducam-pivariety.h | 107 ++
22 4 files changed, 1586 insertions(+)
23 create mode 100644 drivers/media/i2c/arducam-pivariety.c
24 create mode 100644 drivers/media/i2c/arducam-pivariety.h
26 --- a/drivers/media/i2c/Kconfig
27 +++ b/drivers/media/i2c/Kconfig
28 @@ -54,6 +54,17 @@ config VIDEO_AR0521
29 To compile this driver as a module, choose M here: the
30 module will be called ar0521.
32 +config VIDEO_ARDUCAM_PIVARIETY
33 + tristate "Arducam Pivariety sensor support"
34 + depends on I2C && VIDEO_DEV
35 + select VIDEO_V4L2_SUBDEV_API
37 + This is a Video4Linux2 sensor driver for the Arducam
38 + Pivariety camera series.
40 + To compile this driver as a module, choose M here: the
41 + module will be called arducam-pivariety.
44 tristate "Hynix Hi-556 sensor support"
45 depends on I2C && VIDEO_DEV
46 --- a/drivers/media/i2c/Makefile
47 +++ b/drivers/media/i2c/Makefile
48 @@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_AK7375) += ak7375.o
49 obj-$(CONFIG_VIDEO_AK881X) += ak881x.o
50 obj-$(CONFIG_VIDEO_APTINA_PLL) += aptina-pll.o
51 obj-$(CONFIG_VIDEO_AR0521) += ar0521.o
52 +obj-$(CONFIG_VIDEO_ARDUCAM_PIVARIETY) += arducam-pivariety.o
53 obj-$(CONFIG_VIDEO_BT819) += bt819.o
54 obj-$(CONFIG_VIDEO_BT856) += bt856.o
55 obj-$(CONFIG_VIDEO_BT866) += bt866.o
57 +++ b/drivers/media/i2c/arducam-pivariety.c
59 +// SPDX-License-Identifier: GPL-2.0
61 + * A V4L2 driver for Arducam Pivariety Cameras
62 + * Copyright (C) 2022 Arducam Technology co., Ltd.
64 + * Based on Sony IMX219 camera driver
65 + * Copyright (C) 2019, Raspberry Pi (Trading) Ltd
67 + * I2C read and write method is taken from the OV9281 driver
68 + * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
71 +#include <linux/clk.h>
72 +#include <linux/delay.h>
73 +#include <linux/gpio/consumer.h>
74 +#include <linux/i2c.h>
75 +#include <linux/module.h>
76 +#include <linux/pm_runtime.h>
77 +#include <linux/regulator/consumer.h>
78 +#include <media/v4l2-ctrls.h>
79 +#include <media/v4l2-device.h>
80 +#include <media/v4l2-event.h>
81 +#include <media/v4l2-fwnode.h>
82 +#include "arducam-pivariety.h"
85 +module_param(debug, int, 0644);
87 +/* regulator supplies */
88 +static const char * const pivariety_supply_name[] = {
89 + /* Supplies can be enabled in any order */
90 + "VANA", /* Analog (2.8V) supply */
91 + "VDIG", /* Digital Core (1.8V) supply */
92 + "VDDL", /* IF (1.2V) supply */
95 +/* The supported raw formats. */
96 +static const u32 codes[] = {
97 + MEDIA_BUS_FMT_SBGGR8_1X8,
98 + MEDIA_BUS_FMT_SGBRG8_1X8,
99 + MEDIA_BUS_FMT_SGRBG8_1X8,
100 + MEDIA_BUS_FMT_SRGGB8_1X8,
101 + MEDIA_BUS_FMT_Y8_1X8,
103 + MEDIA_BUS_FMT_SBGGR10_1X10,
104 + MEDIA_BUS_FMT_SGBRG10_1X10,
105 + MEDIA_BUS_FMT_SGRBG10_1X10,
106 + MEDIA_BUS_FMT_SRGGB10_1X10,
107 + MEDIA_BUS_FMT_Y10_1X10,
109 + MEDIA_BUS_FMT_SBGGR12_1X12,
110 + MEDIA_BUS_FMT_SGBRG12_1X12,
111 + MEDIA_BUS_FMT_SGRBG12_1X12,
112 + MEDIA_BUS_FMT_SRGGB12_1X12,
113 + MEDIA_BUS_FMT_Y12_1X12,
116 +#define ARDUCAM_NUM_SUPPLIES ARRAY_SIZE(pivariety_supply_name)
118 +#define ARDUCAM_XCLR_MIN_DELAY_US 10000
119 +#define ARDUCAM_XCLR_DELAY_RANGE_US 1000
121 +#define MAX_CTRLS 32
124 + struct v4l2_subdev sd;
125 + struct media_pad pad;
127 + struct v4l2_fwnode_bus_mipi_csi2 bus;
131 + struct gpio_desc *reset_gpio;
132 + struct regulator_bulk_data supplies[ARDUCAM_NUM_SUPPLIES];
134 + struct arducam_format *supported_formats;
135 + int num_supported_formats;
136 + int current_format_idx;
137 + int current_resolution_idx;
139 + int bayer_order_volatile;
140 + bool wait_until_free;
142 + struct v4l2_ctrl_handler ctrl_handler;
143 + struct v4l2_ctrl *ctrls[MAX_CTRLS];
144 + /* V4L2 Controls */
145 + struct v4l2_ctrl *vflip;
146 + struct v4l2_ctrl *hflip;
148 + struct v4l2_rect crop;
150 + * Mutex for serialized access:
151 + * Protect sensor module set pad format and start/stop streaming safely.
153 + struct mutex mutex;
155 + /* Streaming on/off */
159 +static inline struct pivariety *to_pivariety(struct v4l2_subdev *_sd)
161 + return container_of(_sd, struct pivariety, sd);
164 +/* Write registers up to 4 at a time */
165 +static int pivariety_write_reg(struct i2c_client *client, u16 reg, u32 val)
167 + unsigned int len = sizeof(u32);
168 + u32 buf_i, val_i = 0;
174 + buf[1] = reg & 0xff;
176 + val_be = cpu_to_be32(val);
177 + val_p = (u8 *)&val_be;
181 + buf[buf_i++] = val_p[val_i++];
183 + if (i2c_master_send(client, buf, len + 2) != len + 2)
189 +/* Read registers up to 4 at a time */
190 +static int pivariety_read_reg(struct i2c_client *client, u16 reg, u32 *val)
192 + struct i2c_msg msgs[2];
193 + unsigned int len = sizeof(u32);
195 + __be32 data_be = 0;
196 + __be16 reg_addr_be = cpu_to_be16(reg);
199 + data_be_p = (u8 *)&data_be;
200 + /* Write register address */
201 + msgs[0].addr = client->addr;
204 + msgs[0].buf = (u8 *)®_addr_be;
206 + /* Read data from register */
207 + msgs[1].addr = client->addr;
208 + msgs[1].flags = I2C_M_RD;
210 + msgs[1].buf = data_be_p;
212 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
213 + if (ret != ARRAY_SIZE(msgs))
216 + *val = be32_to_cpu(data_be);
222 +pivariety_read(struct pivariety *pivariety, u16 addr, u32 *value)
224 + struct v4l2_subdev *sd = &pivariety->sd;
225 + struct i2c_client *client = v4l2_get_subdevdata(sd);
226 + int ret, count = 0;
228 + while (count++ < I2C_READ_RETRY_COUNT) {
229 + ret = pivariety_read_reg(client, addr, value);
231 + v4l2_dbg(2, debug, sd, "%s: 0x%02x 0x%04x\n",
232 + __func__, addr, *value);
237 + v4l2_err(sd, "%s: Reading register 0x%02x failed\n",
243 +static int pivariety_write(struct pivariety *pivariety, u16 addr, u32 value)
245 + struct v4l2_subdev *sd = &pivariety->sd;
246 + struct i2c_client *client = v4l2_get_subdevdata(sd);
247 + int ret, count = 0;
249 + while (count++ < I2C_WRITE_RETRY_COUNT) {
250 + ret = pivariety_write_reg(client, addr, value);
255 + v4l2_err(sd, "%s: Write 0x%04x to register 0x%02x failed\n",
256 + __func__, value, addr);
261 +static int wait_for_free(struct pivariety *pivariety, int interval)
266 + while (count++ < (1000 / interval)) {
267 + int ret = pivariety_read(pivariety, SYSTEM_IDLE_REG, &value);
269 + if (!ret && !value)
274 + v4l2_dbg(2, debug, &pivariety->sd, "%s: End wait, Count: %d.\n",
280 +static int is_raw(int pixformat)
282 + return pixformat >= 0x28 && pixformat <= 0x2D;
285 +static u32 bayer_to_mbus_code(int data_type, int bayer_order)
287 + const u32 depth8[] = {
288 + MEDIA_BUS_FMT_SBGGR8_1X8,
289 + MEDIA_BUS_FMT_SGBRG8_1X8,
290 + MEDIA_BUS_FMT_SGRBG8_1X8,
291 + MEDIA_BUS_FMT_SRGGB8_1X8,
292 + MEDIA_BUS_FMT_Y8_1X8,
295 + const u32 depth10[] = {
296 + MEDIA_BUS_FMT_SBGGR10_1X10,
297 + MEDIA_BUS_FMT_SGBRG10_1X10,
298 + MEDIA_BUS_FMT_SGRBG10_1X10,
299 + MEDIA_BUS_FMT_SRGGB10_1X10,
300 + MEDIA_BUS_FMT_Y10_1X10,
303 + const u32 depth12[] = {
304 + MEDIA_BUS_FMT_SBGGR12_1X12,
305 + MEDIA_BUS_FMT_SGBRG12_1X12,
306 + MEDIA_BUS_FMT_SGRBG12_1X12,
307 + MEDIA_BUS_FMT_SRGGB12_1X12,
308 + MEDIA_BUS_FMT_Y12_1X12,
311 + if (bayer_order < 0 || bayer_order > 4)
314 + switch (data_type) {
315 + case IMAGE_DT_RAW8:
316 + return depth8[bayer_order];
317 + case IMAGE_DT_RAW10:
318 + return depth10[bayer_order];
319 + case IMAGE_DT_RAW12:
320 + return depth12[bayer_order];
326 +static u32 yuv422_to_mbus_code(int data_type, int order)
328 + const u32 depth8[] = {
329 + MEDIA_BUS_FMT_YUYV8_1X16,
330 + MEDIA_BUS_FMT_YVYU8_1X16,
331 + MEDIA_BUS_FMT_UYVY8_1X16,
332 + MEDIA_BUS_FMT_VYUY8_1X16,
335 + const u32 depth10[] = {
336 + MEDIA_BUS_FMT_YUYV10_1X20,
337 + MEDIA_BUS_FMT_YVYU10_1X20,
338 + MEDIA_BUS_FMT_UYVY10_1X20,
339 + MEDIA_BUS_FMT_VYUY10_1X20,
342 + if (order < 0 || order > 3)
345 + switch (data_type) {
346 + case IMAGE_DT_YUV422_8:
347 + return depth8[order];
348 + case IMAGE_DT_YUV422_10:
349 + return depth10[order];
355 +static u32 data_type_to_mbus_code(int data_type, int bayer_order)
357 + if (is_raw(data_type))
358 + return bayer_to_mbus_code(data_type, bayer_order);
360 + switch (data_type) {
361 + case IMAGE_DT_YUV422_8:
362 + case IMAGE_DT_YUV422_10:
363 + return yuv422_to_mbus_code(data_type, bayer_order);
364 + case IMAGE_DT_RGB565:
365 + return MEDIA_BUS_FMT_RGB565_2X8_LE;
366 + case IMAGE_DT_RGB888:
367 + return MEDIA_BUS_FMT_RGB888_1X24;
373 +/* Get bayer order based on flip setting. */
374 +static u32 pivariety_get_format_code(struct pivariety *pivariety,
375 + struct arducam_format *format)
377 + unsigned int order, origin_order;
379 + lockdep_assert_held(&pivariety->mutex);
382 + * Only the bayer format needs to transform the format.
384 + if (!is_raw(format->data_type) ||
385 + !pivariety->bayer_order_volatile ||
386 + format->bayer_order == BAYER_ORDER_GRAY)
387 + return data_type_to_mbus_code(format->data_type,
388 + format->bayer_order);
390 + order = format->bayer_order;
392 + origin_order = order;
394 + order = (pivariety->hflip && pivariety->hflip->val ? order ^ 1 : order);
395 + order = (pivariety->vflip && pivariety->vflip->val ? order ^ 2 : order);
397 + v4l2_dbg(1, debug, &pivariety->sd, "%s: before: %d, after: %d.\n",
398 + __func__, origin_order, order);
400 + return data_type_to_mbus_code(format->data_type, order);
403 +/* Power/clock management functions */
404 +static int pivariety_power_on(struct device *dev)
406 + struct i2c_client *client = to_i2c_client(dev);
407 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
408 + struct pivariety *pivariety = to_pivariety(sd);
411 + ret = regulator_bulk_enable(ARDUCAM_NUM_SUPPLIES,
412 + pivariety->supplies);
414 + dev_err(dev, "%s: failed to enable regulators\n",
419 + ret = clk_prepare_enable(pivariety->xclk);
421 + dev_err(dev, "%s: failed to enable clock\n",
426 + gpiod_set_value_cansleep(pivariety->reset_gpio, 1);
427 + usleep_range(ARDUCAM_XCLR_MIN_DELAY_US,
428 + ARDUCAM_XCLR_MIN_DELAY_US + ARDUCAM_XCLR_DELAY_RANGE_US);
433 + regulator_bulk_disable(ARDUCAM_NUM_SUPPLIES, pivariety->supplies);
438 +static int pivariety_power_off(struct device *dev)
440 + struct i2c_client *client = to_i2c_client(dev);
441 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
442 + struct pivariety *pivariety = to_pivariety(sd);
444 + gpiod_set_value_cansleep(pivariety->reset_gpio, 0);
445 + regulator_bulk_disable(ARDUCAM_NUM_SUPPLIES, pivariety->supplies);
446 + clk_disable_unprepare(pivariety->xclk);
451 +static int pivariety_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
453 + struct pivariety *pivariety = to_pivariety(sd);
454 + struct v4l2_mbus_framefmt *try_fmt =
455 + v4l2_subdev_get_try_format(sd, fh->state, 0);
456 + struct arducam_format *def_fmt = &pivariety->supported_formats[0];
458 + /* Initialize try_fmt */
459 + try_fmt->width = def_fmt->resolution_set->width;
460 + try_fmt->height = def_fmt->resolution_set->height;
461 + try_fmt->code = def_fmt->mbus_code;
462 + try_fmt->field = V4L2_FIELD_NONE;
467 +static int pivariety_s_ctrl(struct v4l2_ctrl *ctrl)
470 + struct pivariety *pivariety =
471 + container_of(ctrl->handler, struct pivariety,
473 + struct arducam_format *supported_fmts = pivariety->supported_formats;
474 + int num_supported_formats = pivariety->num_supported_formats;
476 + v4l2_dbg(3, debug, &pivariety->sd, "%s: cid = (0x%X), value = (%d).\n",
477 + __func__, ctrl->id, ctrl->val);
479 + ret = pivariety_write(pivariety, CTRL_ID_REG, ctrl->id);
480 + ret += pivariety_write(pivariety, CTRL_VALUE_REG, ctrl->val);
484 + /* When flip is set, modify all bayer formats */
485 + if (ctrl->id == V4L2_CID_VFLIP || ctrl->id == V4L2_CID_HFLIP) {
486 + for (i = 0; i < num_supported_formats; i++) {
487 + supported_fmts[i].mbus_code =
488 + pivariety_get_format_code(pivariety,
489 + &supported_fmts[i]);
494 + * When starting streaming, controls are set in batches,
495 + * and the short interval will cause some controls to be unsuccessfully
498 + if (pivariety->wait_until_free)
499 + wait_for_free(pivariety, 1);
501 + usleep_range(200, 210);
506 +static const struct v4l2_ctrl_ops pivariety_ctrl_ops = {
507 + .s_ctrl = pivariety_s_ctrl,
510 +static int pivariety_enum_mbus_code(struct v4l2_subdev *sd,
511 + struct v4l2_subdev_state *sd_state,
512 + struct v4l2_subdev_mbus_code_enum *code)
514 + struct pivariety *pivariety = to_pivariety(sd);
515 + struct arducam_format *supported_formats = pivariety->supported_formats;
516 + int num_supported_formats = pivariety->num_supported_formats;
518 + v4l2_dbg(1, debug, sd, "%s: index = (%d)\n", __func__, code->index);
520 + if (code->index >= num_supported_formats)
523 + code->code = supported_formats[code->index].mbus_code;
528 +static int pivariety_enum_framesizes(struct v4l2_subdev *sd,
529 + struct v4l2_subdev_state *sd_state,
530 + struct v4l2_subdev_frame_size_enum *fse)
533 + struct pivariety *pivariety = to_pivariety(sd);
534 + struct arducam_format *supported_formats = pivariety->supported_formats;
535 + int num_supported_formats = pivariety->num_supported_formats;
536 + struct arducam_format *format;
537 + struct arducam_resolution *resolution;
539 + v4l2_dbg(1, debug, sd, "%s: code = (0x%X), index = (%d)\n",
540 + __func__, fse->code, fse->index);
542 + for (i = 0; i < num_supported_formats; i++) {
543 + format = &supported_formats[i];
544 + if (fse->code == format->mbus_code) {
545 + if (fse->index >= format->num_resolution_set)
548 + resolution = &format->resolution_set[fse->index];
549 + fse->min_width = resolution->width;
550 + fse->max_width = resolution->width;
551 + fse->min_height = resolution->height;
552 + fse->max_height = resolution->height;
561 +static int pivariety_get_fmt(struct v4l2_subdev *sd,
562 + struct v4l2_subdev_state *sd_state,
563 + struct v4l2_subdev_format *format)
565 + struct pivariety *pivariety = to_pivariety(sd);
566 + struct arducam_format *current_format;
567 + struct v4l2_mbus_framefmt *fmt = &format->format;
570 + if (format->pad != 0)
573 + mutex_lock(&pivariety->mutex);
576 + &pivariety->supported_formats[pivariety->current_format_idx];
577 + cur_res_idx = pivariety->current_resolution_idx;
578 + format->format.width =
579 + current_format->resolution_set[cur_res_idx].width;
580 + format->format.height =
581 + current_format->resolution_set[cur_res_idx].height;
582 + format->format.code = current_format->mbus_code;
583 + format->format.field = V4L2_FIELD_NONE;
584 + fmt->colorspace = V4L2_COLORSPACE_RAW;
585 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
586 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
589 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
591 + v4l2_dbg(1, debug, sd, "%s: width: (%d) height: (%d) code: (0x%X)\n",
592 + __func__, format->format.width, format->format.height,
593 + format->format.code);
595 + mutex_unlock(&pivariety->mutex);
599 +static int pivariety_get_fmt_idx_by_code(struct pivariety *pivariety,
604 + struct arducam_format *formats = pivariety->supported_formats;
606 + for (i = 0; i < pivariety->num_supported_formats; i++) {
607 + if (formats[i].mbus_code == mbus_code)
612 + * If the specified format is not found in the list of supported
613 + * formats, try to find a format of the same data type.
615 + for (i = 0; i < ARRAY_SIZE(codes); i++)
616 + if (codes[i] == mbus_code)
619 + if (i >= ARRAY_SIZE(codes))
622 + data_type = i / 5 + IMAGE_DT_RAW8;
624 + for (i = 0; i < pivariety->num_supported_formats; i++) {
625 + if (formats[i].data_type == data_type)
632 +static struct v4l2_ctrl *get_control(struct pivariety *pivariety,
637 + while (index < MAX_CTRLS && pivariety->ctrls[index]) {
638 + if (pivariety->ctrls[index]->id == id)
639 + return pivariety->ctrls[index];
646 +static int update_control(struct pivariety *pivariety, u32 id)
648 + struct v4l2_subdev *sd = &pivariety->sd;
649 + struct v4l2_ctrl *ctrl;
650 + u32 min, max, step, def, id2;
653 + pivariety_write(pivariety, CTRL_ID_REG, id);
654 + pivariety_read(pivariety, CTRL_ID_REG, &id2);
656 + v4l2_dbg(1, debug, sd, "%s: Write ID: 0x%08X Read ID: 0x%08X\n",
657 + __func__, id, id2);
659 + pivariety_write(pivariety, CTRL_VALUE_REG, 0);
660 + wait_for_free(pivariety, 1);
662 + ret += pivariety_read(pivariety, CTRL_MAX_REG, &max);
663 + ret += pivariety_read(pivariety, CTRL_MIN_REG, &min);
664 + ret += pivariety_read(pivariety, CTRL_DEF_REG, &def);
665 + ret += pivariety_read(pivariety, CTRL_STEP_REG, &step);
670 + if (id == NO_DATA_AVAILABLE || max == NO_DATA_AVAILABLE ||
671 + min == NO_DATA_AVAILABLE || def == NO_DATA_AVAILABLE ||
672 + step == NO_DATA_AVAILABLE)
675 + v4l2_dbg(1, debug, sd, "%s: min: %d, max: %d, step: %d, def: %d\n",
676 + __func__, min, max, step, def);
678 + ctrl = get_control(pivariety, id);
679 + return __v4l2_ctrl_modify_range(ctrl, min, max, step, def);
685 +static int update_controls(struct pivariety *pivariety)
690 + wait_for_free(pivariety, 5);
692 + while (index < MAX_CTRLS && pivariety->ctrls[index]) {
693 + ret += update_control(pivariety, pivariety->ctrls[index]->id);
700 +static int pivariety_set_fmt(struct v4l2_subdev *sd,
701 + struct v4l2_subdev_state *sd_state,
702 + struct v4l2_subdev_format *format)
705 + struct pivariety *pivariety = to_pivariety(sd);
706 + struct arducam_format *supported_formats = pivariety->supported_formats;
708 + if (format->pad != 0)
711 + mutex_lock(&pivariety->mutex);
713 + format->format.colorspace = V4L2_COLORSPACE_RAW;
714 + format->format.field = V4L2_FIELD_NONE;
716 + v4l2_dbg(1, debug, sd, "%s: code: 0x%X, width: %d, height: %d\n",
717 + __func__, format->format.code, format->format.width,
718 + format->format.height);
720 + i = pivariety_get_fmt_idx_by_code(pivariety, format->format.code);
724 + format->format.code = supported_formats[i].mbus_code;
726 + for (j = 0; j < supported_formats[i].num_resolution_set; j++) {
727 + if (supported_formats[i].resolution_set[j].width ==
728 + format->format.width &&
729 + supported_formats[i].resolution_set[j].height ==
730 + format->format.height) {
731 + v4l2_dbg(1, debug, sd,
732 + "%s: format match.\n", __func__);
733 + v4l2_dbg(1, debug, sd,
734 + "%s: set format to device: %d %d.\n",
735 + __func__, supported_formats[i].index, j);
737 + pivariety_write(pivariety, PIXFORMAT_INDEX_REG,
738 + supported_formats[i].index);
739 + pivariety_write(pivariety, RESOLUTION_INDEX_REG, j);
741 + pivariety->current_format_idx = i;
742 + pivariety->current_resolution_idx = j;
744 + update_controls(pivariety);
750 + format->format.width = supported_formats[i].resolution_set[0].width;
751 + format->format.height = supported_formats[i].resolution_set[0].height;
753 + pivariety_write(pivariety, PIXFORMAT_INDEX_REG,
754 + supported_formats[i].index);
755 + pivariety_write(pivariety, RESOLUTION_INDEX_REG, 0);
757 + pivariety->current_format_idx = i;
758 + pivariety->current_resolution_idx = 0;
759 + update_controls(pivariety);
763 + mutex_unlock(&pivariety->mutex);
768 +/* Start streaming */
769 +static int pivariety_start_streaming(struct pivariety *pivariety)
773 + /* set stream on register */
774 + ret = pivariety_write(pivariety, MODE_SELECT_REG,
775 + ARDUCAM_MODE_STREAMING);
780 + wait_for_free(pivariety, 2);
783 + * When starting streaming, controls are set in batches,
784 + * and the short interval will cause some controls to be unsuccessfully
787 + pivariety->wait_until_free = true;
788 + /* Apply customized values from user */
789 + ret = __v4l2_ctrl_handler_setup(pivariety->sd.ctrl_handler);
791 + pivariety->wait_until_free = false;
795 + wait_for_free(pivariety, 2);
800 +static int pivariety_read_sel(struct pivariety *pivariety,
801 + struct v4l2_rect *rect)
805 + ret += pivariety_read(pivariety, IPC_SEL_TOP_REG, &rect->top);
806 + ret += pivariety_read(pivariety, IPC_SEL_LEFT_REG, &rect->left);
807 + ret += pivariety_read(pivariety, IPC_SEL_WIDTH_REG, &rect->width);
808 + ret += pivariety_read(pivariety, IPC_SEL_HEIGHT_REG, &rect->height);
810 + if (ret || rect->top == NO_DATA_AVAILABLE ||
811 + rect->left == NO_DATA_AVAILABLE ||
812 + rect->width == NO_DATA_AVAILABLE ||
813 + rect->height == NO_DATA_AVAILABLE) {
814 + v4l2_err(&pivariety->sd, "%s: Failed to read selection.\n",
822 +static const struct v4l2_rect *
823 +__pivariety_get_pad_crop(struct pivariety *pivariety,
824 + struct v4l2_subdev_state *sd_state,
826 + enum v4l2_subdev_format_whence which)
831 + case V4L2_SUBDEV_FORMAT_TRY:
832 + return v4l2_subdev_get_try_crop(&pivariety->sd, sd_state, pad);
833 + case V4L2_SUBDEV_FORMAT_ACTIVE:
834 + ret = pivariety_read_sel(pivariety, &pivariety->crop);
837 + return &pivariety->crop;
843 +static int pivariety_get_selection(struct v4l2_subdev *sd,
844 + struct v4l2_subdev_state *sd_state,
845 + struct v4l2_subdev_selection *sel)
848 + struct v4l2_rect rect;
849 + struct pivariety *pivariety = to_pivariety(sd);
851 + ret = pivariety_write(pivariety, IPC_SEL_TARGET_REG, sel->target);
853 + v4l2_err(sd, "%s: Write register 0x%02x failed\n",
854 + __func__, IPC_SEL_TARGET_REG);
858 + wait_for_free(pivariety, 2);
860 + switch (sel->target) {
861 + case V4L2_SEL_TGT_CROP: {
862 + mutex_lock(&pivariety->mutex);
863 + sel->r = *__pivariety_get_pad_crop(pivariety, sd_state,
866 + mutex_unlock(&pivariety->mutex);
871 + case V4L2_SEL_TGT_NATIVE_SIZE:
872 + case V4L2_SEL_TGT_CROP_DEFAULT:
873 + case V4L2_SEL_TGT_CROP_BOUNDS:
874 + ret = pivariety_read_sel(pivariety, &rect);
885 +/* Stop streaming */
886 +static int pivariety_stop_streaming(struct pivariety *pivariety)
890 + /* set stream off register */
891 + ret = pivariety_write(pivariety, MODE_SELECT_REG, ARDUCAM_MODE_STANDBY);
893 + v4l2_err(&pivariety->sd, "%s failed to set stream\n", __func__);
896 + * Return success even if it was an error, as there is nothing the
897 + * caller can do about it.
902 +static int pivariety_set_stream(struct v4l2_subdev *sd, int enable)
904 + struct pivariety *pivariety = to_pivariety(sd);
905 + struct i2c_client *client = v4l2_get_subdevdata(sd);
908 + mutex_lock(&pivariety->mutex);
909 + if (pivariety->streaming == enable) {
910 + mutex_unlock(&pivariety->mutex);
915 + ret = pm_runtime_get_sync(&client->dev);
917 + pm_runtime_put_noidle(&client->dev);
922 + * Apply default & customized values
923 + * and then start streaming.
925 + ret = pivariety_start_streaming(pivariety);
929 + pivariety_stop_streaming(pivariety);
930 + pm_runtime_put(&client->dev);
933 + pivariety->streaming = enable;
936 + * vflip and hflip cannot change during streaming
937 + * Pivariety may not implement flip control.
939 + if (pivariety->vflip)
940 + __v4l2_ctrl_grab(pivariety->vflip, enable);
942 + if (pivariety->hflip)
943 + __v4l2_ctrl_grab(pivariety->hflip, enable);
945 + mutex_unlock(&pivariety->mutex);
950 + pm_runtime_put(&client->dev);
952 + mutex_unlock(&pivariety->mutex);
957 +static int __maybe_unused pivariety_suspend(struct device *dev)
959 + struct i2c_client *client = to_i2c_client(dev);
960 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
961 + struct pivariety *pivariety = to_pivariety(sd);
963 + if (pivariety->streaming)
964 + pivariety_stop_streaming(pivariety);
969 +static int __maybe_unused pivariety_resume(struct device *dev)
971 + struct i2c_client *client = to_i2c_client(dev);
972 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
973 + struct pivariety *pivariety = to_pivariety(sd);
976 + if (pivariety->streaming) {
977 + ret = pivariety_start_streaming(pivariety);
985 + pivariety_stop_streaming(pivariety);
986 + pivariety->streaming = 0;
990 +static int pivariety_get_regulators(struct pivariety *pivariety)
992 + struct i2c_client *client = v4l2_get_subdevdata(&pivariety->sd);
995 + for (i = 0; i < ARDUCAM_NUM_SUPPLIES; i++)
996 + pivariety->supplies[i].supply = pivariety_supply_name[i];
998 + return devm_regulator_bulk_get(&client->dev,
999 + ARDUCAM_NUM_SUPPLIES,
1000 + pivariety->supplies);
1003 +static int pivariety_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
1004 + struct v4l2_mbus_config *cfg)
1006 + struct pivariety *pivariety = to_pivariety(sd);
1007 + const u32 mask = V4L2_MBUS_CSI2_LANE_MASK;
1009 + if (pivariety->lanes > pivariety->bus.num_data_lanes)
1012 + cfg->type = V4L2_MBUS_CSI2_DPHY;
1013 + cfg->flags = (pivariety->lanes << __ffs(mask)) & mask;
1018 +static const struct v4l2_subdev_core_ops pivariety_core_ops = {
1019 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1020 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1023 +static const struct v4l2_subdev_video_ops pivariety_video_ops = {
1024 + .s_stream = pivariety_set_stream,
1027 +static const struct v4l2_subdev_pad_ops pivariety_pad_ops = {
1028 + .enum_mbus_code = pivariety_enum_mbus_code,
1029 + .get_fmt = pivariety_get_fmt,
1030 + .set_fmt = pivariety_set_fmt,
1031 + .enum_frame_size = pivariety_enum_framesizes,
1032 + .get_selection = pivariety_get_selection,
1033 + .get_mbus_config = pivariety_get_mbus_config,
1036 +static const struct v4l2_subdev_ops pivariety_subdev_ops = {
1037 + .core = &pivariety_core_ops,
1038 + .video = &pivariety_video_ops,
1039 + .pad = &pivariety_pad_ops,
1042 +static const struct v4l2_subdev_internal_ops pivariety_internal_ops = {
1043 + .open = pivariety_open,
1046 +static void pivariety_free_controls(struct pivariety *pivariety)
1048 + v4l2_ctrl_handler_free(pivariety->sd.ctrl_handler);
1049 + mutex_destroy(&pivariety->mutex);
1052 +static int pivariety_get_length_of_set(struct pivariety *pivariety,
1053 + u16 idx_reg, u16 val_reg)
1060 + ret = pivariety_write(pivariety, idx_reg, index);
1061 + ret += pivariety_read(pivariety, val_reg, &val);
1066 + if (val == NO_DATA_AVAILABLE)
1070 + pivariety_write(pivariety, idx_reg, 0);
1074 +static int pivariety_enum_resolution(struct pivariety *pivariety,
1075 + struct arducam_format *format)
1077 + struct i2c_client *client = v4l2_get_subdevdata(&pivariety->sd);
1079 + u32 width, height;
1080 + int num_resolution = 0;
1083 + num_resolution = pivariety_get_length_of_set(pivariety,
1084 + RESOLUTION_INDEX_REG,
1085 + FORMAT_WIDTH_REG);
1086 + if (num_resolution < 0)
1089 + format->resolution_set = devm_kzalloc(&client->dev,
1090 + sizeof(*format->resolution_set) *
1094 + ret = pivariety_write(pivariety, RESOLUTION_INDEX_REG, index);
1095 + ret += pivariety_read(pivariety, FORMAT_WIDTH_REG, &width);
1096 + ret += pivariety_read(pivariety, FORMAT_HEIGHT_REG, &height);
1101 + if (width == NO_DATA_AVAILABLE || height == NO_DATA_AVAILABLE)
1104 + format->resolution_set[index].width = width;
1105 + format->resolution_set[index].height = height;
1110 + format->num_resolution_set = index;
1111 + pivariety_write(pivariety, RESOLUTION_INDEX_REG, 0);
1117 +static int pivariety_enum_pixformat(struct pivariety *pivariety)
1120 + u32 mbus_code = 0;
1123 + int bayer_order_not_volatile;
1126 + int num_pixformat = 0;
1127 + struct arducam_format *arducam_fmt;
1128 + struct i2c_client *client = v4l2_get_subdevdata(&pivariety->sd);
1130 + num_pixformat = pivariety_get_length_of_set(pivariety,
1131 + PIXFORMAT_INDEX_REG,
1132 + PIXFORMAT_TYPE_REG);
1134 + if (num_pixformat < 0)
1137 + ret = pivariety_read(pivariety, FLIPS_DONT_CHANGE_ORDER_REG,
1138 + &bayer_order_not_volatile);
1139 + if (bayer_order_not_volatile == NO_DATA_AVAILABLE)
1140 + pivariety->bayer_order_volatile = 1;
1142 + pivariety->bayer_order_volatile = !bayer_order_not_volatile;
1147 + pivariety->supported_formats =
1148 + devm_kzalloc(&client->dev,
1149 + sizeof(*pivariety->supported_formats) *
1154 + ret = pivariety_write(pivariety, PIXFORMAT_INDEX_REG, index);
1155 + ret += pivariety_read(pivariety, PIXFORMAT_TYPE_REG,
1158 + if (pixfmt_type == NO_DATA_AVAILABLE)
1161 + ret += pivariety_read(pivariety, MIPI_LANES_REG, &lanes);
1162 + if (lanes == NO_DATA_AVAILABLE)
1165 + ret += pivariety_read(pivariety, PIXFORMAT_ORDER_REG,
1170 + mbus_code = data_type_to_mbus_code(pixfmt_type, bayer_order);
1171 + arducam_fmt = &pivariety->supported_formats[index];
1172 + arducam_fmt->index = index;
1173 + arducam_fmt->mbus_code = mbus_code;
1174 + arducam_fmt->bayer_order = bayer_order;
1175 + arducam_fmt->data_type = pixfmt_type;
1176 + if (pivariety_enum_resolution(pivariety, arducam_fmt))
1182 + pivariety_write(pivariety, PIXFORMAT_INDEX_REG, 0);
1183 + pivariety->num_supported_formats = index;
1184 + pivariety->current_format_idx = 0;
1185 + pivariety->current_resolution_idx = 0;
1186 + pivariety->lanes = lanes;
1194 +static const char *pivariety_ctrl_get_name(u32 id)
1197 + case V4L2_CID_ARDUCAM_EXT_TRI:
1198 + return "trigger_mode";
1199 + case V4L2_CID_ARDUCAM_IRCUT:
1206 +enum v4l2_ctrl_type pivariety_get_v4l2_ctrl_type(u32 id)
1209 + case V4L2_CID_ARDUCAM_EXT_TRI:
1210 + return V4L2_CTRL_TYPE_BOOLEAN;
1211 + case V4L2_CID_ARDUCAM_IRCUT:
1212 + return V4L2_CTRL_TYPE_BOOLEAN;
1214 + return V4L2_CTRL_TYPE_INTEGER;
1218 +static struct v4l2_ctrl *v4l2_ctrl_new_arducam(struct v4l2_ctrl_handler *hdl,
1219 + const struct v4l2_ctrl_ops *ops,
1220 + u32 id, s64 min, s64 max,
1221 + u64 step, s64 def)
1223 + struct v4l2_ctrl_config ctrl_cfg = {
1227 + .type = V4L2_CTRL_TYPE_INTEGER,
1235 + ctrl_cfg.name = pivariety_ctrl_get_name(id);
1236 + ctrl_cfg.type = pivariety_get_v4l2_ctrl_type(id);
1238 + return v4l2_ctrl_new_custom(hdl, &ctrl_cfg, NULL);
1241 +static int pivariety_enum_controls(struct pivariety *pivariety)
1243 + struct v4l2_subdev *sd = &pivariety->sd;
1244 + struct i2c_client *client = v4l2_get_subdevdata(sd);
1245 + struct v4l2_ctrl_handler *ctrl_hdlr = &pivariety->ctrl_handler;
1246 + struct v4l2_fwnode_device_properties props;
1247 + struct v4l2_ctrl **ctrl = pivariety->ctrls;
1248 + int ret, index, num_ctrls;
1249 + u32 id, min, max, def, step;
1251 + num_ctrls = pivariety_get_length_of_set(pivariety, CTRL_INDEX_REG,
1253 + if (num_ctrls < 0)
1256 + v4l2_dbg(1, debug, sd, "%s: num_ctrls = %d\n",
1257 + __func__, num_ctrls);
1259 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, num_ctrls);
1265 + ret = pivariety_write(pivariety, CTRL_INDEX_REG, index);
1266 + pivariety_write(pivariety, CTRL_VALUE_REG, 0);
1267 + wait_for_free(pivariety, 1);
1269 + ret += pivariety_read(pivariety, CTRL_ID_REG, &id);
1270 + ret += pivariety_read(pivariety, CTRL_MAX_REG, &max);
1271 + ret += pivariety_read(pivariety, CTRL_MIN_REG, &min);
1272 + ret += pivariety_read(pivariety, CTRL_DEF_REG, &def);
1273 + ret += pivariety_read(pivariety, CTRL_STEP_REG, &step);
1277 + if (id == NO_DATA_AVAILABLE || max == NO_DATA_AVAILABLE ||
1278 + min == NO_DATA_AVAILABLE || def == NO_DATA_AVAILABLE ||
1279 + step == NO_DATA_AVAILABLE)
1282 + v4l2_dbg(1, debug, sd,
1283 + "%s: index = %d, id = 0x%x, max = %d, min = %d, def = %d, step = %d\n",
1284 + __func__, index, id, max, min, def, step);
1286 + if (v4l2_ctrl_get_name(id)) {
1287 + *ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1288 + &pivariety_ctrl_ops,
1292 + v4l2_dbg(1, debug, sd, "%s: ctrl: 0x%p\n",
1294 + } else if (pivariety_ctrl_get_name(id)) {
1295 + *ctrl = v4l2_ctrl_new_arducam(ctrl_hdlr,
1296 + &pivariety_ctrl_ops,
1297 + id, min, max, step, def);
1299 + v4l2_dbg(1, debug, sd,
1300 + "%s: new custom ctrl, ctrl: 0x%p.\n",
1311 + case V4L2_CID_HFLIP:
1312 + pivariety->hflip = *ctrl;
1313 + if (pivariety->bayer_order_volatile)
1314 + pivariety->hflip->flags |=
1315 + V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1318 + case V4L2_CID_VFLIP:
1319 + pivariety->vflip = *ctrl;
1320 + if (pivariety->bayer_order_volatile)
1321 + pivariety->vflip->flags |=
1322 + V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1325 + case V4L2_CID_HBLANK:
1326 + (*ctrl)->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1334 + pivariety_write(pivariety, CTRL_INDEX_REG, 0);
1336 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
1340 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr,
1341 + &pivariety_ctrl_ops,
1346 + pivariety->sd.ctrl_handler = ctrl_hdlr;
1347 + v4l2_ctrl_handler_setup(ctrl_hdlr);
1353 +static int pivariety_parse_dt(struct pivariety *pivariety, struct device *dev)
1355 + struct fwnode_handle *endpoint;
1356 + struct v4l2_fwnode_endpoint ep_cfg = {
1357 + .bus_type = V4L2_MBUS_CSI2_DPHY
1359 + int ret = -EINVAL;
1361 + /* Get CSI2 bus config */
1362 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1364 + dev_err(dev, "endpoint node not found\n");
1368 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
1369 + dev_err(dev, "could not parse endpoint\n");
1373 + pivariety->bus = ep_cfg.bus.mipi_csi2;
1378 + v4l2_fwnode_endpoint_free(&ep_cfg);
1379 + fwnode_handle_put(endpoint);
1384 +static int pivariety_probe(struct i2c_client *client,
1385 + const struct i2c_device_id *id)
1387 + struct device *dev = &client->dev;
1388 + struct pivariety *pivariety;
1389 + u32 device_id, firmware_version;
1392 + pivariety = devm_kzalloc(&client->dev, sizeof(*pivariety), GFP_KERNEL);
1396 + /* Initialize subdev */
1397 + v4l2_i2c_subdev_init(&pivariety->sd, client,
1398 + &pivariety_subdev_ops);
1400 + if (pivariety_parse_dt(pivariety, dev))
1403 + /* Get system clock (xclk) */
1404 + pivariety->xclk = devm_clk_get(dev, "xclk");
1405 + if (IS_ERR(pivariety->xclk)) {
1406 + dev_err(dev, "failed to get xclk\n");
1407 + return PTR_ERR(pivariety->xclk);
1410 + pivariety->xclk_freq = clk_get_rate(pivariety->xclk);
1411 + if (pivariety->xclk_freq != 24000000) {
1412 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
1413 + pivariety->xclk_freq);
1417 + ret = pivariety_get_regulators(pivariety);
1421 + /* Request optional enable pin */
1422 + pivariety->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1425 + ret = pivariety_power_on(dev);
1429 + ret = pivariety_read(pivariety, DEVICE_ID_REG, &device_id);
1430 + if (ret || device_id != DEVICE_ID) {
1431 + dev_err(dev, "probe failed\n");
1433 + goto error_power_off;
1436 + ret = pivariety_read(pivariety, DEVICE_VERSION_REG, &firmware_version);
1438 + dev_err(dev, "read firmware version failed\n");
1440 + dev_info(dev, "firmware version: 0x%04X\n", firmware_version);
1442 + if (pivariety_enum_pixformat(pivariety)) {
1443 + dev_err(dev, "enum pixformat failed.\n");
1445 + goto error_power_off;
1448 + if (pivariety_enum_controls(pivariety)) {
1449 + dev_err(dev, "enum controls failed.\n");
1451 + goto error_power_off;
1454 + /* Initialize subdev */
1455 + pivariety->sd.internal_ops = &pivariety_internal_ops;
1456 + pivariety->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1457 + pivariety->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1458 + /* Initialize source pad */
1459 + pivariety->pad.flags = MEDIA_PAD_FL_SOURCE;
1461 + ret = media_entity_pads_init(&pivariety->sd.entity, 1, &pivariety->pad);
1463 + goto error_handler_free;
1465 + ret = v4l2_async_register_subdev_sensor(&pivariety->sd);
1467 + goto error_media_entity;
1469 + pm_runtime_set_active(dev);
1470 + pm_runtime_enable(dev);
1471 + pm_runtime_idle(dev);
1475 +error_media_entity:
1476 + media_entity_cleanup(&pivariety->sd.entity);
1478 +error_handler_free:
1479 + pivariety_free_controls(pivariety);
1482 + pivariety_power_off(dev);
1487 +static void pivariety_remove(struct i2c_client *client)
1489 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1490 + struct pivariety *pivariety = to_pivariety(sd);
1492 + v4l2_async_unregister_subdev(sd);
1493 + media_entity_cleanup(&sd->entity);
1494 + pivariety_free_controls(pivariety);
1496 + pm_runtime_disable(&client->dev);
1497 + pm_runtime_set_suspended(&client->dev);
1500 +static const struct dev_pm_ops pivariety_pm_ops = {
1501 + SET_SYSTEM_SLEEP_PM_OPS(pivariety_suspend, pivariety_resume)
1502 + SET_RUNTIME_PM_OPS(pivariety_power_off, pivariety_power_on, NULL)
1505 +static const struct of_device_id arducam_pivariety_dt_ids[] = {
1506 + { .compatible = "arducam,arducam-pivariety" },
1507 + { /* sentinel */ }
1509 +MODULE_DEVICE_TABLE(of, arducam_pivariety_dt_ids);
1511 +static struct i2c_driver arducam_pivariety_i2c_driver = {
1513 + .name = "arducam-pivariety",
1514 + .of_match_table = arducam_pivariety_dt_ids,
1515 + .pm = &pivariety_pm_ops,
1517 + .probe = pivariety_probe,
1518 + .remove = pivariety_remove,
1521 +module_i2c_driver(arducam_pivariety_i2c_driver);
1523 +MODULE_AUTHOR("Lee Jackson <info@arducam.com>");
1524 +MODULE_DESCRIPTION("Arducam Pivariety v4l2 driver");
1525 +MODULE_LICENSE("GPL v2");
1527 +++ b/drivers/media/i2c/arducam-pivariety.h
1529 +/* SPDX-License-Identifier: GPL-2.0 */
1530 +#ifndef _ARDUCAM_PIVARIETY_H_
1531 +#define _ARDUCAM_PIVARIETY_H_
1533 +#define DEVICE_REG_BASE 0x0100
1534 +#define PIXFORMAT_REG_BASE 0x0200
1535 +#define FORMAT_REG_BASE 0x0300
1536 +#define CTRL_REG_BASE 0x0400
1537 +#define IPC_REG_BASE 0x0600
1539 +#define ARDUCAM_MODE_STANDBY 0x00
1540 +#define ARDUCAM_MODE_STREAMING 0x01
1542 +#define MODE_SELECT_REG (DEVICE_REG_BASE | 0x0000)
1543 +#define DEVICE_VERSION_REG (DEVICE_REG_BASE | 0x0001)
1544 +#define SENSOR_ID_REG (DEVICE_REG_BASE | 0x0002)
1545 +#define DEVICE_ID_REG (DEVICE_REG_BASE | 0x0003)
1546 +#define SYSTEM_IDLE_REG (DEVICE_REG_BASE | 0x0007)
1548 +#define PIXFORMAT_INDEX_REG (PIXFORMAT_REG_BASE | 0x0000)
1549 +#define PIXFORMAT_TYPE_REG (PIXFORMAT_REG_BASE | 0x0001)
1550 +#define PIXFORMAT_ORDER_REG (PIXFORMAT_REG_BASE | 0x0002)
1551 +#define MIPI_LANES_REG (PIXFORMAT_REG_BASE | 0x0003)
1552 +#define FLIPS_DONT_CHANGE_ORDER_REG (PIXFORMAT_REG_BASE | 0x0004)
1554 +#define RESOLUTION_INDEX_REG (FORMAT_REG_BASE | 0x0000)
1555 +#define FORMAT_WIDTH_REG (FORMAT_REG_BASE | 0x0001)
1556 +#define FORMAT_HEIGHT_REG (FORMAT_REG_BASE | 0x0002)
1558 +#define CTRL_INDEX_REG (CTRL_REG_BASE | 0x0000)
1559 +#define CTRL_ID_REG (CTRL_REG_BASE | 0x0001)
1560 +#define CTRL_MIN_REG (CTRL_REG_BASE | 0x0002)
1561 +#define CTRL_MAX_REG (CTRL_REG_BASE | 0x0003)
1562 +#define CTRL_STEP_REG (CTRL_REG_BASE | 0x0004)
1563 +#define CTRL_DEF_REG (CTRL_REG_BASE | 0x0005)
1564 +#define CTRL_VALUE_REG (CTRL_REG_BASE | 0x0006)
1566 +#define IPC_SEL_TARGET_REG (IPC_REG_BASE | 0x0000)
1567 +#define IPC_SEL_TOP_REG (IPC_REG_BASE | 0x0001)
1568 +#define IPC_SEL_LEFT_REG (IPC_REG_BASE | 0x0002)
1569 +#define IPC_SEL_WIDTH_REG (IPC_REG_BASE | 0x0003)
1570 +#define IPC_SEL_HEIGHT_REG (IPC_REG_BASE | 0x0004)
1571 +#define IPC_DELAY_REG (IPC_REG_BASE | 0x0005)
1573 +#define NO_DATA_AVAILABLE 0xFFFFFFFE
1575 +#define DEVICE_ID 0x0030
1577 +#define I2C_READ_RETRY_COUNT 3
1578 +#define I2C_WRITE_RETRY_COUNT 2
1580 +#define V4L2_CID_ARDUCAM_BASE (V4L2_CID_USER_BASE + 0x1000)
1581 +#define V4L2_CID_ARDUCAM_EXT_TRI (V4L2_CID_ARDUCAM_BASE + 1)
1582 +#define V4L2_CID_ARDUCAM_IRCUT (V4L2_CID_ARDUCAM_BASE + 8)
1585 + IMAGE_DT_YUV420_8 = 0x18,
1586 + IMAGE_DT_YUV420_10,
1588 + IMAGE_DT_YUV420CSPS_8 = 0x1C,
1589 + IMAGE_DT_YUV420CSPS_10,
1590 + IMAGE_DT_YUV422_8,
1591 + IMAGE_DT_YUV422_10,
1598 + IMAGE_DT_RAW6 = 0x28,
1607 + BAYER_ORDER_BGGR = 0,
1608 + BAYER_ORDER_GBRG = 1,
1609 + BAYER_ORDER_GRBG = 2,
1610 + BAYER_ORDER_RGGB = 3,
1611 + BAYER_ORDER_GRAY = 4,
1615 + YUV_ORDER_YUYV = 0,
1616 + YUV_ORDER_YVYU = 1,
1617 + YUV_ORDER_UYVY = 2,
1618 + YUV_ORDER_VYUY = 3,
1621 +struct arducam_resolution {
1626 +struct arducam_format {
1631 + u32 num_resolution_set;
1632 + struct arducam_resolution *resolution_set;