1 // SPDX-License-Identifier: GPL-2.0
3 * Sony IMX290 CMOS Image Sensor Driver
5 * Copyright (C) 2019 FRAMOS GmbH.
7 * Copyright (C) 2019 Linaro Ltd.
8 * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
21 #include <asm/unaligned.h>
23 #include <media/media-entity.h>
24 #include <media/v4l2-cci.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-subdev.h>
31 #define IMX290_STANDBY CCI_REG8(0x3000)
32 #define IMX290_REGHOLD CCI_REG8(0x3001)
33 #define IMX290_XMSTA CCI_REG8(0x3002)
34 #define IMX290_ADBIT CCI_REG8(0x3005)
35 #define IMX290_ADBIT_10BIT (0 << 0)
36 #define IMX290_ADBIT_12BIT (1 << 0)
37 #define IMX290_CTRL_07 CCI_REG8(0x3007)
38 #define IMX290_VREVERSE BIT(0)
39 #define IMX290_HREVERSE BIT(1)
40 #define IMX290_WINMODE_1080P (0 << 4)
41 #define IMX290_WINMODE_720P (1 << 4)
42 #define IMX290_WINMODE_CROP (4 << 4)
43 #define IMX290_FR_FDG_SEL CCI_REG8(0x3009)
44 #define IMX290_BLKLEVEL CCI_REG16_LE(0x300a)
45 #define IMX290_GAIN CCI_REG8(0x3014)
46 #define IMX290_VMAX CCI_REG24_LE(0x3018)
47 #define IMX290_VMAX_MAX 0x3ffff
48 #define IMX290_HMAX CCI_REG16_LE(0x301c)
49 #define IMX290_HMAX_MAX 0xffff
50 #define IMX290_SHS1 CCI_REG24_LE(0x3020)
51 #define IMX290_WINWV_OB CCI_REG8(0x303a)
52 #define IMX290_WINPV CCI_REG16_LE(0x303c)
53 #define IMX290_WINWV CCI_REG16_LE(0x303e)
54 #define IMX290_WINPH CCI_REG16_LE(0x3040)
55 #define IMX290_WINWH CCI_REG16_LE(0x3042)
56 #define IMX290_OUT_CTRL CCI_REG8(0x3046)
57 #define IMX290_ODBIT_10BIT (0 << 0)
58 #define IMX290_ODBIT_12BIT (1 << 0)
59 #define IMX290_OPORTSEL_PARALLEL (0x0 << 4)
60 #define IMX290_OPORTSEL_LVDS_2CH (0xd << 4)
61 #define IMX290_OPORTSEL_LVDS_4CH (0xe << 4)
62 #define IMX290_OPORTSEL_LVDS_8CH (0xf << 4)
63 #define IMX290_XSOUTSEL CCI_REG8(0x304b)
64 #define IMX290_XSOUTSEL_XVSOUTSEL_HIGH (0 << 0)
65 #define IMX290_XSOUTSEL_XVSOUTSEL_VSYNC (2 << 0)
66 #define IMX290_XSOUTSEL_XHSOUTSEL_HIGH (0 << 2)
67 #define IMX290_XSOUTSEL_XHSOUTSEL_HSYNC (2 << 2)
68 #define IMX290_INCKSEL1 CCI_REG8(0x305c)
69 #define IMX290_INCKSEL2 CCI_REG8(0x305d)
70 #define IMX290_INCKSEL3 CCI_REG8(0x305e)
71 #define IMX290_INCKSEL4 CCI_REG8(0x305f)
72 #define IMX290_PGCTRL CCI_REG8(0x308c)
73 #define IMX290_ADBIT1 CCI_REG8(0x3129)
74 #define IMX290_ADBIT1_10BIT 0x1d
75 #define IMX290_ADBIT1_12BIT 0x00
76 #define IMX290_INCKSEL5 CCI_REG8(0x315e)
77 #define IMX290_INCKSEL6 CCI_REG8(0x3164)
78 #define IMX290_ADBIT2 CCI_REG8(0x317c)
79 #define IMX290_ADBIT2_10BIT 0x12
80 #define IMX290_ADBIT2_12BIT 0x00
81 #define IMX290_CHIP_ID CCI_REG16_LE(0x319a)
82 #define IMX290_ADBIT3 CCI_REG8(0x31ec)
83 #define IMX290_ADBIT3_10BIT 0x37
84 #define IMX290_ADBIT3_12BIT 0x0e
85 #define IMX290_REPETITION CCI_REG8(0x3405)
86 #define IMX290_PHY_LANE_NUM CCI_REG8(0x3407)
87 #define IMX290_OPB_SIZE_V CCI_REG8(0x3414)
88 #define IMX290_Y_OUT_SIZE CCI_REG16_LE(0x3418)
89 #define IMX290_CSI_DT_FMT CCI_REG16_LE(0x3441)
90 #define IMX290_CSI_DT_FMT_RAW10 0x0a0a
91 #define IMX290_CSI_DT_FMT_RAW12 0x0c0c
92 #define IMX290_CSI_LANE_MODE CCI_REG8(0x3443)
93 #define IMX290_EXTCK_FREQ CCI_REG16_LE(0x3444)
94 #define IMX290_TCLKPOST CCI_REG16_LE(0x3446)
95 #define IMX290_THSZERO CCI_REG16_LE(0x3448)
96 #define IMX290_THSPREPARE CCI_REG16_LE(0x344a)
97 #define IMX290_TCLKTRAIL CCI_REG16_LE(0x344c)
98 #define IMX290_THSTRAIL CCI_REG16_LE(0x344e)
99 #define IMX290_TCLKZERO CCI_REG16_LE(0x3450)
100 #define IMX290_TCLKPREPARE CCI_REG16_LE(0x3452)
101 #define IMX290_TLPX CCI_REG16_LE(0x3454)
102 #define IMX290_X_OUT_SIZE CCI_REG16_LE(0x3472)
103 #define IMX290_INCKSEL7 CCI_REG8(0x3480)
105 #define IMX290_PGCTRL_REGEN BIT(0)
106 #define IMX290_PGCTRL_THRU BIT(1)
107 #define IMX290_PGCTRL_MODE(n) ((n) << 4)
109 /* Number of lines by which exposure must be less than VMAX */
110 #define IMX290_EXPOSURE_OFFSET 2
112 #define IMX290_PIXEL_RATE 148500000
115 * The IMX290 pixel array is organized as follows:
117 * +------------------------------------+
118 * | Optical Black | } Vertical effective optical black (10)
119 * +---+------------------------------------+---+
120 * | | | | } Effective top margin (8)
121 * | | +----------------------------+ | | \
125 * | | | Recording Pixel Area | | | | Recommended height (1080)
129 * | | +----------------------------+ | | /
130 * | | | | } Effective bottom margin (9)
131 * +---+------------------------------------+---+
132 * <-> <-> <--------------------------> <-> <->
133 * \---- Ignored right margin (4)
134 * \-------- Effective right margin (9)
135 * \------------------------- Recommended width (1920)
136 * \----------------------------------------- Effective left margin (8)
137 * \--------------------------------------------- Ignored left margin (4)
139 * The optical black lines are output over CSI-2 with a separate data type.
141 * The pixel array is meant to have 1920x1080 usable pixels after image
142 * processing in an ISP. It has 8 (9) extra active pixels usable for color
143 * processing in the ISP on the top and left (bottom and right) sides of the
144 * image. In addition, 4 additional pixels are present on the left and right
145 * sides of the image, documented as "ignored area".
147 * As far as is understood, all pixels of the pixel array (ignored area, color
148 * processing margins and recording area) can be output by the sensor.
151 #define IMX290_PIXEL_ARRAY_WIDTH 1945
152 #define IMX290_PIXEL_ARRAY_HEIGHT 1097
153 #define IMX290_PIXEL_ARRAY_MARGIN_LEFT 12
154 #define IMX290_PIXEL_ARRAY_MARGIN_RIGHT 13
155 #define IMX290_PIXEL_ARRAY_MARGIN_TOP 8
156 #define IMX290_PIXEL_ARRAY_MARGIN_BOTTOM 9
157 #define IMX290_PIXEL_ARRAY_RECORDING_WIDTH 1920
158 #define IMX290_PIXEL_ARRAY_RECORDING_HEIGHT 1080
160 /* Equivalent value for 16bpp */
161 #define IMX290_BLACK_LEVEL_DEFAULT 3840
163 #define IMX290_NUM_SUPPLIES 3
165 enum imx290_colour_variant {
166 IMX290_VARIANT_COLOUR,
172 IMX290_MODEL_IMX290LQR,
173 IMX290_MODEL_IMX290LLR,
174 IMX290_MODEL_IMX327LQR,
177 struct imx290_model_info {
178 enum imx290_colour_variant colour_variant;
179 const struct cci_reg_sequence *init_regs;
180 size_t init_regs_num;
184 enum imx290_clk_freq {
191 * Clock configuration for registers INCKSEL1 to INCKSEL6.
193 struct imx290_clk_cfg {
210 const struct cci_reg_sequence *data;
213 const struct imx290_clk_cfg *clk_cfg;
216 struct imx290_csi_cfg {
231 struct regmap *regmap;
232 enum imx290_clk_freq xclk_idx;
234 const struct imx290_model_info *model;
236 struct v4l2_subdev sd;
237 struct media_pad pad;
239 const struct imx290_mode *current_mode;
241 struct regulator_bulk_data supplies[IMX290_NUM_SUPPLIES];
242 struct gpio_desc *rst_gpio;
244 struct v4l2_ctrl_handler ctrls;
245 struct v4l2_ctrl *link_freq;
246 struct v4l2_ctrl *hblank;
247 struct v4l2_ctrl *vblank;
248 struct v4l2_ctrl *exposure;
250 struct v4l2_ctrl *hflip;
251 struct v4l2_ctrl *vflip;
255 static inline struct imx290 *to_imx290(struct v4l2_subdev *_sd)
257 return container_of(_sd, struct imx290, sd);
260 /* -----------------------------------------------------------------------------
264 static const struct cci_reg_sequence imx290_global_init_settings[] = {
265 { IMX290_WINWV_OB, 12 },
268 { IMX290_WINWH, 1948 },
269 { IMX290_WINWV, 1097 },
270 { IMX290_XSOUTSEL, IMX290_XSOUTSEL_XVSOUTSEL_VSYNC |
271 IMX290_XSOUTSEL_XHSOUTSEL_HSYNC },
272 { CCI_REG8(0x3011), 0x02 },
273 { CCI_REG8(0x3012), 0x64 },
274 { CCI_REG8(0x3013), 0x00 },
277 static const struct cci_reg_sequence imx290_global_init_settings_290[] = {
278 { CCI_REG8(0x300f), 0x00 },
279 { CCI_REG8(0x3010), 0x21 },
280 { CCI_REG8(0x3016), 0x09 },
281 { CCI_REG8(0x3070), 0x02 },
282 { CCI_REG8(0x3071), 0x11 },
283 { CCI_REG8(0x309b), 0x10 },
284 { CCI_REG8(0x309c), 0x22 },
285 { CCI_REG8(0x30a2), 0x02 },
286 { CCI_REG8(0x30a6), 0x20 },
287 { CCI_REG8(0x30a8), 0x20 },
288 { CCI_REG8(0x30aa), 0x20 },
289 { CCI_REG8(0x30ac), 0x20 },
290 { CCI_REG8(0x30b0), 0x43 },
291 { CCI_REG8(0x3119), 0x9e },
292 { CCI_REG8(0x311c), 0x1e },
293 { CCI_REG8(0x311e), 0x08 },
294 { CCI_REG8(0x3128), 0x05 },
295 { CCI_REG8(0x313d), 0x83 },
296 { CCI_REG8(0x3150), 0x03 },
297 { CCI_REG8(0x317e), 0x00 },
298 { CCI_REG8(0x32b8), 0x50 },
299 { CCI_REG8(0x32b9), 0x10 },
300 { CCI_REG8(0x32ba), 0x00 },
301 { CCI_REG8(0x32bb), 0x04 },
302 { CCI_REG8(0x32c8), 0x50 },
303 { CCI_REG8(0x32c9), 0x10 },
304 { CCI_REG8(0x32ca), 0x00 },
305 { CCI_REG8(0x32cb), 0x04 },
306 { CCI_REG8(0x332c), 0xd3 },
307 { CCI_REG8(0x332d), 0x10 },
308 { CCI_REG8(0x332e), 0x0d },
309 { CCI_REG8(0x3358), 0x06 },
310 { CCI_REG8(0x3359), 0xe1 },
311 { CCI_REG8(0x335a), 0x11 },
312 { CCI_REG8(0x3360), 0x1e },
313 { CCI_REG8(0x3361), 0x61 },
314 { CCI_REG8(0x3362), 0x10 },
315 { CCI_REG8(0x33b0), 0x50 },
316 { CCI_REG8(0x33b2), 0x1a },
317 { CCI_REG8(0x33b3), 0x04 },
320 #define IMX290_NUM_CLK_REGS 2
321 static const struct cci_reg_sequence xclk_regs[][IMX290_NUM_CLK_REGS] = {
322 [IMX290_CLK_37_125] = {
323 { IMX290_EXTCK_FREQ, (37125 * 256) / 1000 },
324 { IMX290_INCKSEL7, 0x49 },
326 [IMX290_CLK_74_25] = {
327 { IMX290_EXTCK_FREQ, (74250 * 256) / 1000 },
328 { IMX290_INCKSEL7, 0x92 },
332 static const struct cci_reg_sequence imx290_global_init_settings_327[] = {
333 { CCI_REG8(0x309e), 0x4A },
334 { CCI_REG8(0x309f), 0x4A },
335 { CCI_REG8(0x313b), 0x61 },
338 static const struct cci_reg_sequence imx290_1080p_settings[] = {
340 { IMX290_WINWV_OB, 12 },
341 { IMX290_OPB_SIZE_V, 10 },
342 { IMX290_X_OUT_SIZE, 1920 },
343 { IMX290_Y_OUT_SIZE, 1080 },
346 static const struct cci_reg_sequence imx290_720p_settings[] = {
348 { IMX290_WINWV_OB, 6 },
349 { IMX290_OPB_SIZE_V, 4 },
350 { IMX290_X_OUT_SIZE, 1280 },
351 { IMX290_Y_OUT_SIZE, 720 },
354 static const struct cci_reg_sequence imx290_10bit_settings[] = {
355 { IMX290_ADBIT, IMX290_ADBIT_10BIT },
356 { IMX290_OUT_CTRL, IMX290_ODBIT_10BIT },
357 { IMX290_ADBIT1, IMX290_ADBIT1_10BIT },
358 { IMX290_ADBIT2, IMX290_ADBIT2_10BIT },
359 { IMX290_ADBIT3, IMX290_ADBIT3_10BIT },
360 { IMX290_CSI_DT_FMT, IMX290_CSI_DT_FMT_RAW10 },
363 static const struct cci_reg_sequence imx290_12bit_settings[] = {
364 { IMX290_ADBIT, IMX290_ADBIT_12BIT },
365 { IMX290_OUT_CTRL, IMX290_ODBIT_12BIT },
366 { IMX290_ADBIT1, IMX290_ADBIT1_12BIT },
367 { IMX290_ADBIT2, IMX290_ADBIT2_12BIT },
368 { IMX290_ADBIT3, IMX290_ADBIT3_12BIT },
369 { IMX290_CSI_DT_FMT, IMX290_CSI_DT_FMT_RAW12 },
372 static const struct imx290_csi_cfg imx290_csi_222_75mhz = {
373 /* 222.75MHz or 445.5Mbit/s per lane */
385 static const struct imx290_csi_cfg imx290_csi_445_5mhz = {
386 /* 445.5MHz or 891Mbit/s per lane */
398 static const struct imx290_csi_cfg imx290_csi_148_5mhz = {
399 /* 148.5MHz or 297Mbit/s per lane */
411 static const struct imx290_csi_cfg imx290_csi_297mhz = {
412 /* 297MHz or 594Mbit/s per lane */
424 /* supported link frequencies */
425 #define FREQ_INDEX_1080P 0
426 #define FREQ_INDEX_720P 1
427 static const s64 imx290_link_freq_2lanes[] = {
428 [FREQ_INDEX_1080P] = 445500000,
429 [FREQ_INDEX_720P] = 297000000,
432 static const s64 imx290_link_freq_4lanes[] = {
433 [FREQ_INDEX_1080P] = 222750000,
434 [FREQ_INDEX_720P] = 148500000,
438 * In this function and in the similar ones below We rely on imx290_probe()
439 * to ensure that nlanes is either 2 or 4.
441 static inline const s64 *imx290_link_freqs_ptr(const struct imx290 *imx290)
443 if (imx290->nlanes == 2)
444 return imx290_link_freq_2lanes;
446 return imx290_link_freq_4lanes;
449 static inline int imx290_link_freqs_num(const struct imx290 *imx290)
451 if (imx290->nlanes == 2)
452 return ARRAY_SIZE(imx290_link_freq_2lanes);
454 return ARRAY_SIZE(imx290_link_freq_4lanes);
457 static const struct imx290_clk_cfg imx290_1080p_clock_config[] = {
458 [IMX290_CLK_37_125] = {
459 /* 37.125MHz clock config */
467 [IMX290_CLK_74_25] = {
468 /* 74.25MHz clock config */
478 static const struct imx290_clk_cfg imx290_720p_clock_config[] = {
479 [IMX290_CLK_37_125] = {
480 /* 37.125MHz clock config */
488 [IMX290_CLK_74_25] = {
489 /* 74.25MHz clock config */
500 static const struct imx290_mode imx290_modes_2lanes[] = {
506 .link_freq_index = FREQ_INDEX_1080P,
507 .ctrl_07 = IMX290_WINMODE_1080P,
508 .data = imx290_1080p_settings,
509 .data_size = ARRAY_SIZE(imx290_1080p_settings),
510 .clk_cfg = imx290_1080p_clock_config,
517 .link_freq_index = FREQ_INDEX_720P,
518 .ctrl_07 = IMX290_WINMODE_720P,
519 .data = imx290_720p_settings,
520 .data_size = ARRAY_SIZE(imx290_720p_settings),
521 .clk_cfg = imx290_720p_clock_config,
525 static const struct imx290_mode imx290_modes_4lanes[] = {
531 .link_freq_index = FREQ_INDEX_1080P,
532 .ctrl_07 = IMX290_WINMODE_1080P,
533 .data = imx290_1080p_settings,
534 .data_size = ARRAY_SIZE(imx290_1080p_settings),
535 .clk_cfg = imx290_1080p_clock_config,
542 .link_freq_index = FREQ_INDEX_720P,
543 .ctrl_07 = IMX290_WINMODE_720P,
544 .data = imx290_720p_settings,
545 .data_size = ARRAY_SIZE(imx290_720p_settings),
546 .clk_cfg = imx290_720p_clock_config,
550 static inline const struct imx290_mode *imx290_modes_ptr(const struct imx290 *imx290)
552 if (imx290->nlanes == 2)
553 return imx290_modes_2lanes;
555 return imx290_modes_4lanes;
558 static inline int imx290_modes_num(const struct imx290 *imx290)
560 if (imx290->nlanes == 2)
561 return ARRAY_SIZE(imx290_modes_2lanes);
563 return ARRAY_SIZE(imx290_modes_4lanes);
566 struct imx290_format_info {
567 u32 code[IMX290_VARIANT_MAX];
569 const struct cci_reg_sequence *regs;
570 unsigned int num_regs;
573 static const struct imx290_format_info imx290_formats[] = {
576 [IMX290_VARIANT_COLOUR] = MEDIA_BUS_FMT_SRGGB10_1X10,
577 [IMX290_VARIANT_MONO] = MEDIA_BUS_FMT_Y10_1X10
580 .regs = imx290_10bit_settings,
581 .num_regs = ARRAY_SIZE(imx290_10bit_settings),
584 [IMX290_VARIANT_COLOUR] = MEDIA_BUS_FMT_SRGGB12_1X12,
585 [IMX290_VARIANT_MONO] = MEDIA_BUS_FMT_Y12_1X12
588 .regs = imx290_12bit_settings,
589 .num_regs = ARRAY_SIZE(imx290_12bit_settings),
593 static const struct imx290_format_info *
594 imx290_format_info(const struct imx290 *imx290, u32 code)
598 for (i = 0; i < ARRAY_SIZE(imx290_formats); ++i) {
599 const struct imx290_format_info *info = &imx290_formats[i];
601 if (info->code[imx290->model->colour_variant] == code)
608 static int imx290_set_register_array(struct imx290 *imx290,
609 const struct cci_reg_sequence *settings,
610 unsigned int num_settings)
614 ret = cci_multi_reg_write(imx290->regmap, settings, num_settings, NULL);
618 /* Provide 10ms settle time */
619 usleep_range(10000, 11000);
624 static int imx290_set_clock(struct imx290 *imx290)
626 const struct imx290_mode *mode = imx290->current_mode;
627 enum imx290_clk_freq clk_idx = imx290->xclk_idx;
628 const struct imx290_clk_cfg *clk_cfg = &mode->clk_cfg[clk_idx];
631 ret = imx290_set_register_array(imx290, xclk_regs[clk_idx],
632 IMX290_NUM_CLK_REGS);
634 cci_write(imx290->regmap, IMX290_INCKSEL1, clk_cfg->incksel1, &ret);
635 cci_write(imx290->regmap, IMX290_INCKSEL2, clk_cfg->incksel2, &ret);
636 cci_write(imx290->regmap, IMX290_INCKSEL3, clk_cfg->incksel3, &ret);
637 cci_write(imx290->regmap, IMX290_INCKSEL4, clk_cfg->incksel4, &ret);
638 cci_write(imx290->regmap, IMX290_INCKSEL5, clk_cfg->incksel5, &ret);
639 cci_write(imx290->regmap, IMX290_INCKSEL6, clk_cfg->incksel6, &ret);
644 static int imx290_set_data_lanes(struct imx290 *imx290)
648 cci_write(imx290->regmap, IMX290_PHY_LANE_NUM, imx290->nlanes - 1,
650 cci_write(imx290->regmap, IMX290_CSI_LANE_MODE, imx290->nlanes - 1,
652 cci_write(imx290->regmap, IMX290_FR_FDG_SEL, 0x01, &ret);
657 static int imx290_set_black_level(struct imx290 *imx290,
658 const struct v4l2_mbus_framefmt *format,
659 unsigned int black_level, int *err)
661 unsigned int bpp = imx290_format_info(imx290, format->code)->bpp;
663 return cci_write(imx290->regmap, IMX290_BLKLEVEL,
664 black_level >> (16 - bpp), err);
667 static int imx290_set_csi_config(struct imx290 *imx290)
669 const s64 *link_freqs = imx290_link_freqs_ptr(imx290);
670 const struct imx290_csi_cfg *csi_cfg;
673 switch (link_freqs[imx290->current_mode->link_freq_index]) {
675 csi_cfg = &imx290_csi_445_5mhz;
678 csi_cfg = &imx290_csi_297mhz;
681 csi_cfg = &imx290_csi_222_75mhz;
684 csi_cfg = &imx290_csi_148_5mhz;
690 cci_write(imx290->regmap, IMX290_REPETITION, csi_cfg->repetition, &ret);
691 cci_write(imx290->regmap, IMX290_TCLKPOST, csi_cfg->tclkpost, &ret);
692 cci_write(imx290->regmap, IMX290_THSZERO, csi_cfg->thszero, &ret);
693 cci_write(imx290->regmap, IMX290_THSPREPARE, csi_cfg->thsprepare, &ret);
694 cci_write(imx290->regmap, IMX290_TCLKTRAIL, csi_cfg->tclktrail, &ret);
695 cci_write(imx290->regmap, IMX290_THSTRAIL, csi_cfg->thstrail, &ret);
696 cci_write(imx290->regmap, IMX290_TCLKZERO, csi_cfg->tclkzero, &ret);
697 cci_write(imx290->regmap, IMX290_TCLKPREPARE, csi_cfg->tclkprepare,
699 cci_write(imx290->regmap, IMX290_TLPX, csi_cfg->tlpx, &ret);
704 static int imx290_setup_format(struct imx290 *imx290,
705 const struct v4l2_mbus_framefmt *format)
707 const struct imx290_format_info *info;
710 info = imx290_format_info(imx290, format->code);
712 ret = imx290_set_register_array(imx290, info->regs, info->num_regs);
714 dev_err(imx290->dev, "Could not set format registers\n");
718 return imx290_set_black_level(imx290, format,
719 IMX290_BLACK_LEVEL_DEFAULT, &ret);
722 /* ----------------------------------------------------------------------------
725 static void imx290_exposure_update(struct imx290 *imx290,
726 const struct imx290_mode *mode)
728 unsigned int exposure_max;
730 exposure_max = imx290->vblank->val + mode->height -
731 IMX290_EXPOSURE_OFFSET;
732 __v4l2_ctrl_modify_range(imx290->exposure, 1, exposure_max, 1,
736 static int imx290_set_ctrl(struct v4l2_ctrl *ctrl)
738 struct imx290 *imx290 = container_of(ctrl->handler,
739 struct imx290, ctrls);
740 const struct v4l2_mbus_framefmt *format;
741 struct v4l2_subdev_state *state;
745 * Return immediately for controls that don't need to be applied to the
748 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
751 if (ctrl->id == V4L2_CID_VBLANK) {
752 /* Changing vblank changes the allowed range for exposure. */
753 imx290_exposure_update(imx290, imx290->current_mode);
756 /* V4L2 controls values will be applied only when power is already up */
757 if (!pm_runtime_get_if_in_use(imx290->dev))
760 state = v4l2_subdev_get_locked_active_state(&imx290->sd);
761 format = v4l2_subdev_state_get_format(state, 0);
764 case V4L2_CID_ANALOGUE_GAIN:
765 ret = cci_write(imx290->regmap, IMX290_GAIN, ctrl->val, NULL);
768 case V4L2_CID_VBLANK:
769 ret = cci_write(imx290->regmap, IMX290_VMAX,
770 ctrl->val + imx290->current_mode->height, NULL);
772 * Due to the way that exposure is programmed in this sensor in
773 * relation to VMAX, we have to reprogramme it whenever VMAX is
775 * Update ctrl so that the V4L2_CID_EXPOSURE case can refer to
778 ctrl = imx290->exposure;
780 case V4L2_CID_EXPOSURE:
781 vmax = imx290->vblank->val + imx290->current_mode->height;
782 ret = cci_write(imx290->regmap, IMX290_SHS1,
783 vmax - ctrl->val - 1, NULL);
786 case V4L2_CID_TEST_PATTERN:
788 imx290_set_black_level(imx290, format, 0, &ret);
789 usleep_range(10000, 11000);
790 cci_write(imx290->regmap, IMX290_PGCTRL,
791 (u8)(IMX290_PGCTRL_REGEN |
793 IMX290_PGCTRL_MODE(ctrl->val)), &ret);
795 cci_write(imx290->regmap, IMX290_PGCTRL, 0x00, &ret);
796 usleep_range(10000, 11000);
797 imx290_set_black_level(imx290, format,
798 IMX290_BLACK_LEVEL_DEFAULT, &ret);
802 case V4L2_CID_HBLANK:
803 ret = cci_write(imx290->regmap, IMX290_HMAX,
804 ctrl->val + imx290->current_mode->width, NULL);
812 reg = imx290->current_mode->ctrl_07;
813 if (imx290->hflip->val)
814 reg |= IMX290_HREVERSE;
815 if (imx290->vflip->val)
816 reg |= IMX290_VREVERSE;
817 ret = cci_write(imx290->regmap, IMX290_CTRL_07, reg, NULL);
826 pm_runtime_mark_last_busy(imx290->dev);
827 pm_runtime_put_autosuspend(imx290->dev);
832 static const struct v4l2_ctrl_ops imx290_ctrl_ops = {
833 .s_ctrl = imx290_set_ctrl,
836 static const char * const imx290_test_pattern_menu[] = {
838 "Sequence Pattern 1",
839 "Horizontal Color-bar Chart",
840 "Vertical Color-bar Chart",
841 "Sequence Pattern 2",
842 "Gradation Pattern 1",
843 "Gradation Pattern 2",
844 "000/555h Toggle Pattern",
847 static void imx290_ctrl_update(struct imx290 *imx290,
848 const struct imx290_mode *mode)
850 unsigned int hblank_min = mode->hmax_min - mode->width;
851 unsigned int hblank_max = IMX290_HMAX_MAX - mode->width;
852 unsigned int vblank_min = mode->vmax_min - mode->height;
853 unsigned int vblank_max = IMX290_VMAX_MAX - mode->height;
855 __v4l2_ctrl_s_ctrl(imx290->link_freq, mode->link_freq_index);
857 __v4l2_ctrl_modify_range(imx290->hblank, hblank_min, hblank_max, 1,
859 __v4l2_ctrl_modify_range(imx290->vblank, vblank_min, vblank_max, 1,
863 static int imx290_ctrl_init(struct imx290 *imx290)
865 struct v4l2_fwnode_device_properties props;
868 ret = v4l2_fwnode_device_parse(imx290->dev, &props);
872 v4l2_ctrl_handler_init(&imx290->ctrls, 11);
875 * The sensor has an analog gain and a digital gain, both controlled
876 * through a single gain value, expressed in 0.3dB increments. Values
877 * from 0.0dB (0) to 30.0dB (100) apply analog gain only, higher values
878 * up to 72.0dB (240) add further digital gain. Limit the range to
879 * analog gain only, support for digital gain can be added separately
882 * The IMX327 and IMX462 are largely compatible with the IMX290, but
883 * have an analog gain range of 0.0dB to 29.4dB and 42dB of digital
884 * gain. When support for those sensors gets added to the driver, the
885 * gain control should be adjusted accordingly.
887 v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
888 V4L2_CID_ANALOGUE_GAIN, 0, 100, 1, 0);
891 * Correct range will be determined through imx290_ctrl_update setting
894 imx290->exposure = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
895 V4L2_CID_EXPOSURE, 1, 65535, 1,
899 * Set the link frequency, pixel rate, horizontal blanking and vertical
900 * blanking to hardcoded values, they will be updated by
901 * imx290_ctrl_update().
904 v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
906 imx290_link_freqs_num(imx290) - 1, 0,
907 imx290_link_freqs_ptr(imx290));
908 if (imx290->link_freq)
909 imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
911 v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops, V4L2_CID_PIXEL_RATE,
912 IMX290_PIXEL_RATE, IMX290_PIXEL_RATE, 1,
915 v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
916 V4L2_CID_TEST_PATTERN,
917 ARRAY_SIZE(imx290_test_pattern_menu) - 1,
918 0, 0, imx290_test_pattern_menu);
921 * Actual range will be set from imx290_ctrl_update later in the probe.
923 imx290->hblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
924 V4L2_CID_HBLANK, 1, 1, 1, 1);
926 imx290->vblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
927 V4L2_CID_VBLANK, 1, 1, 1, 1);
929 imx290->hflip = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
930 V4L2_CID_HFLIP, 0, 1, 1, 0);
931 imx290->vflip = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
932 V4L2_CID_VFLIP, 0, 1, 1, 0);
933 v4l2_ctrl_cluster(2, &imx290->hflip);
935 v4l2_ctrl_new_fwnode_properties(&imx290->ctrls, &imx290_ctrl_ops,
938 imx290->sd.ctrl_handler = &imx290->ctrls;
940 if (imx290->ctrls.error) {
941 ret = imx290->ctrls.error;
942 v4l2_ctrl_handler_free(&imx290->ctrls);
949 /* ----------------------------------------------------------------------------
953 /* Start streaming */
954 static int imx290_start_streaming(struct imx290 *imx290,
955 struct v4l2_subdev_state *state)
957 const struct v4l2_mbus_framefmt *format;
960 /* Set init register settings */
961 ret = imx290_set_register_array(imx290, imx290_global_init_settings,
962 ARRAY_SIZE(imx290_global_init_settings));
964 dev_err(imx290->dev, "Could not set init registers\n");
968 /* Set mdel specific init register settings */
969 ret = imx290_set_register_array(imx290, imx290->model->init_regs,
970 imx290->model->init_regs_num);
972 dev_err(imx290->dev, "Could not set model specific init registers\n");
976 /* Set clock parameters based on mode and xclk */
977 ret = imx290_set_clock(imx290);
979 dev_err(imx290->dev, "Could not set clocks - %d\n", ret);
983 /* Set data lane count */
984 ret = imx290_set_data_lanes(imx290);
986 dev_err(imx290->dev, "Could not set data lanes - %d\n", ret);
990 ret = imx290_set_csi_config(imx290);
992 dev_err(imx290->dev, "Could not set csi cfg - %d\n", ret);
996 /* Apply the register values related to current frame format */
997 format = v4l2_subdev_state_get_format(state, 0);
998 ret = imx290_setup_format(imx290, format);
1000 dev_err(imx290->dev, "Could not set frame format - %d\n", ret);
1004 /* Apply default values of current mode */
1005 ret = imx290_set_register_array(imx290, imx290->current_mode->data,
1006 imx290->current_mode->data_size);
1008 dev_err(imx290->dev, "Could not set current mode - %d\n", ret);
1012 /* Apply customized values from user */
1013 ret = __v4l2_ctrl_handler_setup(imx290->sd.ctrl_handler);
1015 dev_err(imx290->dev, "Could not sync v4l2 controls - %d\n", ret);
1019 cci_write(imx290->regmap, IMX290_STANDBY, 0x00, &ret);
1023 /* Start streaming */
1024 return cci_write(imx290->regmap, IMX290_XMSTA, 0x00, &ret);
1027 /* Stop streaming */
1028 static int imx290_stop_streaming(struct imx290 *imx290)
1032 cci_write(imx290->regmap, IMX290_STANDBY, 0x01, &ret);
1036 return cci_write(imx290->regmap, IMX290_XMSTA, 0x01, &ret);
1039 static int imx290_set_stream(struct v4l2_subdev *sd, int enable)
1041 struct imx290 *imx290 = to_imx290(sd);
1042 struct v4l2_subdev_state *state;
1045 state = v4l2_subdev_lock_and_get_active_state(sd);
1048 ret = pm_runtime_resume_and_get(imx290->dev);
1052 ret = imx290_start_streaming(imx290, state);
1054 dev_err(imx290->dev, "Start stream failed\n");
1055 pm_runtime_put_sync(imx290->dev);
1059 imx290_stop_streaming(imx290);
1060 pm_runtime_mark_last_busy(imx290->dev);
1061 pm_runtime_put_autosuspend(imx290->dev);
1065 * vflip and hflip should not be changed during streaming as the sensor
1066 * will produce an invalid frame.
1068 __v4l2_ctrl_grab(imx290->vflip, enable);
1069 __v4l2_ctrl_grab(imx290->hflip, enable);
1072 v4l2_subdev_unlock_state(state);
1076 static int imx290_enum_mbus_code(struct v4l2_subdev *sd,
1077 struct v4l2_subdev_state *sd_state,
1078 struct v4l2_subdev_mbus_code_enum *code)
1080 const struct imx290 *imx290 = to_imx290(sd);
1082 if (code->index >= ARRAY_SIZE(imx290_formats))
1085 code->code = imx290_formats[code->index].code[imx290->model->colour_variant];
1090 static int imx290_enum_frame_size(struct v4l2_subdev *sd,
1091 struct v4l2_subdev_state *sd_state,
1092 struct v4l2_subdev_frame_size_enum *fse)
1094 const struct imx290 *imx290 = to_imx290(sd);
1095 const struct imx290_mode *imx290_modes = imx290_modes_ptr(imx290);
1097 if (!imx290_format_info(imx290, fse->code))
1100 if (fse->index >= imx290_modes_num(imx290))
1103 fse->min_width = imx290_modes[fse->index].width;
1104 fse->max_width = imx290_modes[fse->index].width;
1105 fse->min_height = imx290_modes[fse->index].height;
1106 fse->max_height = imx290_modes[fse->index].height;
1111 static int imx290_set_fmt(struct v4l2_subdev *sd,
1112 struct v4l2_subdev_state *sd_state,
1113 struct v4l2_subdev_format *fmt)
1115 struct imx290 *imx290 = to_imx290(sd);
1116 const struct imx290_mode *mode;
1117 struct v4l2_mbus_framefmt *format;
1119 mode = v4l2_find_nearest_size(imx290_modes_ptr(imx290),
1120 imx290_modes_num(imx290), width, height,
1121 fmt->format.width, fmt->format.height);
1123 fmt->format.width = mode->width;
1124 fmt->format.height = mode->height;
1126 if (!imx290_format_info(imx290, fmt->format.code))
1127 fmt->format.code = imx290_formats[0].code[imx290->model->colour_variant];
1129 fmt->format.field = V4L2_FIELD_NONE;
1130 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
1131 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_601;
1132 fmt->format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1133 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
1135 format = v4l2_subdev_state_get_format(sd_state, 0);
1137 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1138 imx290->current_mode = mode;
1140 imx290_ctrl_update(imx290, mode);
1141 imx290_exposure_update(imx290, mode);
1144 *format = fmt->format;
1149 static int imx290_get_selection(struct v4l2_subdev *sd,
1150 struct v4l2_subdev_state *sd_state,
1151 struct v4l2_subdev_selection *sel)
1153 struct imx290 *imx290 = to_imx290(sd);
1154 struct v4l2_mbus_framefmt *format;
1156 switch (sel->target) {
1157 case V4L2_SEL_TGT_CROP: {
1158 format = v4l2_subdev_state_get_format(sd_state, 0);
1161 * The sensor moves the readout by 1 pixel based on flips to
1162 * keep the Bayer order the same.
1164 sel->r.top = IMX290_PIXEL_ARRAY_MARGIN_TOP
1165 + (IMX290_PIXEL_ARRAY_RECORDING_HEIGHT - format->height) / 2
1166 + imx290->vflip->val;
1167 sel->r.left = IMX290_PIXEL_ARRAY_MARGIN_LEFT
1168 + (IMX290_PIXEL_ARRAY_RECORDING_WIDTH - format->width) / 2
1169 + imx290->hflip->val;
1170 sel->r.width = format->width;
1171 sel->r.height = format->height;
1176 case V4L2_SEL_TGT_NATIVE_SIZE:
1177 case V4L2_SEL_TGT_CROP_BOUNDS:
1180 sel->r.width = IMX290_PIXEL_ARRAY_WIDTH;
1181 sel->r.height = IMX290_PIXEL_ARRAY_HEIGHT;
1185 case V4L2_SEL_TGT_CROP_DEFAULT:
1186 sel->r.top = IMX290_PIXEL_ARRAY_MARGIN_TOP;
1187 sel->r.left = IMX290_PIXEL_ARRAY_MARGIN_LEFT;
1188 sel->r.width = IMX290_PIXEL_ARRAY_RECORDING_WIDTH;
1189 sel->r.height = IMX290_PIXEL_ARRAY_RECORDING_HEIGHT;
1198 static int imx290_entity_init_state(struct v4l2_subdev *subdev,
1199 struct v4l2_subdev_state *sd_state)
1201 struct v4l2_subdev_format fmt = {
1202 .which = V4L2_SUBDEV_FORMAT_TRY,
1209 imx290_set_fmt(subdev, sd_state, &fmt);
1214 static const struct v4l2_subdev_core_ops imx290_core_ops = {
1215 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1216 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1219 static const struct v4l2_subdev_video_ops imx290_video_ops = {
1220 .s_stream = imx290_set_stream,
1223 static const struct v4l2_subdev_pad_ops imx290_pad_ops = {
1224 .enum_mbus_code = imx290_enum_mbus_code,
1225 .enum_frame_size = imx290_enum_frame_size,
1226 .get_fmt = v4l2_subdev_get_fmt,
1227 .set_fmt = imx290_set_fmt,
1228 .get_selection = imx290_get_selection,
1231 static const struct v4l2_subdev_ops imx290_subdev_ops = {
1232 .core = &imx290_core_ops,
1233 .video = &imx290_video_ops,
1234 .pad = &imx290_pad_ops,
1237 static const struct v4l2_subdev_internal_ops imx290_internal_ops = {
1238 .init_state = imx290_entity_init_state,
1241 static const struct media_entity_operations imx290_subdev_entity_ops = {
1242 .link_validate = v4l2_subdev_link_validate,
1245 static int imx290_subdev_init(struct imx290 *imx290)
1247 struct i2c_client *client = to_i2c_client(imx290->dev);
1248 struct v4l2_subdev_state *state;
1251 imx290->current_mode = &imx290_modes_ptr(imx290)[0];
1253 v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
1254 imx290->sd.internal_ops = &imx290_internal_ops;
1255 imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1256 V4L2_SUBDEV_FL_HAS_EVENTS;
1257 imx290->sd.dev = imx290->dev;
1258 imx290->sd.entity.ops = &imx290_subdev_entity_ops;
1259 imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1261 imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
1262 ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
1264 dev_err(imx290->dev, "Could not register media entity\n");
1268 ret = imx290_ctrl_init(imx290);
1270 dev_err(imx290->dev, "Control initialization error %d\n", ret);
1274 imx290->sd.state_lock = imx290->ctrls.lock;
1276 ret = v4l2_subdev_init_finalize(&imx290->sd);
1278 dev_err(imx290->dev, "subdev initialization error %d\n", ret);
1282 state = v4l2_subdev_lock_and_get_active_state(&imx290->sd);
1283 imx290_ctrl_update(imx290, imx290->current_mode);
1284 v4l2_subdev_unlock_state(state);
1289 v4l2_ctrl_handler_free(&imx290->ctrls);
1291 media_entity_cleanup(&imx290->sd.entity);
1295 static void imx290_subdev_cleanup(struct imx290 *imx290)
1297 v4l2_subdev_cleanup(&imx290->sd);
1298 media_entity_cleanup(&imx290->sd.entity);
1299 v4l2_ctrl_handler_free(&imx290->ctrls);
1302 /* ----------------------------------------------------------------------------
1306 static int imx290_power_on(struct imx290 *imx290)
1310 ret = clk_prepare_enable(imx290->xclk);
1312 dev_err(imx290->dev, "Failed to enable clock\n");
1316 ret = regulator_bulk_enable(ARRAY_SIZE(imx290->supplies),
1319 dev_err(imx290->dev, "Failed to enable regulators\n");
1320 clk_disable_unprepare(imx290->xclk);
1325 gpiod_set_value_cansleep(imx290->rst_gpio, 0);
1326 usleep_range(30000, 31000);
1331 static void imx290_power_off(struct imx290 *imx290)
1333 clk_disable_unprepare(imx290->xclk);
1334 gpiod_set_value_cansleep(imx290->rst_gpio, 1);
1335 regulator_bulk_disable(ARRAY_SIZE(imx290->supplies), imx290->supplies);
1338 static int imx290_runtime_resume(struct device *dev)
1340 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1341 struct imx290 *imx290 = to_imx290(sd);
1343 return imx290_power_on(imx290);
1346 static int imx290_runtime_suspend(struct device *dev)
1348 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1349 struct imx290 *imx290 = to_imx290(sd);
1351 imx290_power_off(imx290);
1356 static const struct dev_pm_ops imx290_pm_ops = {
1357 RUNTIME_PM_OPS(imx290_runtime_suspend, imx290_runtime_resume, NULL)
1360 /* ----------------------------------------------------------------------------
1364 static const char * const imx290_supply_name[IMX290_NUM_SUPPLIES] = {
1370 static int imx290_get_regulators(struct device *dev, struct imx290 *imx290)
1374 for (i = 0; i < ARRAY_SIZE(imx290->supplies); i++)
1375 imx290->supplies[i].supply = imx290_supply_name[i];
1377 return devm_regulator_bulk_get(dev, ARRAY_SIZE(imx290->supplies),
1381 static int imx290_init_clk(struct imx290 *imx290)
1386 ret = device_property_read_u32(imx290->dev, "clock-frequency",
1389 dev_err(imx290->dev, "Could not get xclk frequency\n");
1393 /* external clock must be 37.125 MHz or 74.25MHz */
1394 switch (xclk_freq) {
1396 imx290->xclk_idx = IMX290_CLK_37_125;
1399 imx290->xclk_idx = IMX290_CLK_74_25;
1402 dev_err(imx290->dev, "External clock frequency %u is not supported\n",
1407 ret = clk_set_rate(imx290->xclk, xclk_freq);
1409 dev_err(imx290->dev, "Could not set xclk frequency\n");
1417 * Returns 0 if all link frequencies used by the driver for the given number
1418 * of MIPI data lanes are mentioned in the device tree, or the value of the
1419 * first missing frequency otherwise.
1421 static s64 imx290_check_link_freqs(const struct imx290 *imx290,
1422 const struct v4l2_fwnode_endpoint *ep)
1425 const s64 *freqs = imx290_link_freqs_ptr(imx290);
1426 int freqs_count = imx290_link_freqs_num(imx290);
1428 for (i = 0; i < freqs_count; i++) {
1429 for (j = 0; j < ep->nr_of_link_frequencies; j++)
1430 if (freqs[i] == ep->link_frequencies[j])
1432 if (j == ep->nr_of_link_frequencies)
1438 static const struct imx290_model_info imx290_models[] = {
1439 [IMX290_MODEL_IMX290LQR] = {
1440 .colour_variant = IMX290_VARIANT_COLOUR,
1441 .init_regs = imx290_global_init_settings_290,
1442 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_290),
1445 [IMX290_MODEL_IMX290LLR] = {
1446 .colour_variant = IMX290_VARIANT_MONO,
1447 .init_regs = imx290_global_init_settings_290,
1448 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_290),
1451 [IMX290_MODEL_IMX327LQR] = {
1452 .colour_variant = IMX290_VARIANT_COLOUR,
1453 .init_regs = imx290_global_init_settings_327,
1454 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_327),
1459 static int imx290_parse_dt(struct imx290 *imx290)
1461 /* Only CSI2 is supported for now: */
1462 struct v4l2_fwnode_endpoint ep = {
1463 .bus_type = V4L2_MBUS_CSI2_DPHY
1465 struct fwnode_handle *endpoint;
1469 imx290->model = of_device_get_match_data(imx290->dev);
1471 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(imx290->dev), NULL);
1473 dev_err(imx290->dev, "Endpoint node not found\n");
1477 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
1478 fwnode_handle_put(endpoint);
1479 if (ret == -ENXIO) {
1480 dev_err(imx290->dev, "Unsupported bus type, should be CSI2\n");
1483 dev_err(imx290->dev, "Parsing endpoint node failed\n");
1487 /* Get number of data lanes */
1488 imx290->nlanes = ep.bus.mipi_csi2.num_data_lanes;
1489 if (imx290->nlanes != 2 && imx290->nlanes != 4) {
1490 dev_err(imx290->dev, "Invalid data lanes: %d\n", imx290->nlanes);
1495 dev_dbg(imx290->dev, "Using %u data lanes\n", imx290->nlanes);
1497 if (!ep.nr_of_link_frequencies) {
1498 dev_err(imx290->dev, "link-frequency property not found in DT\n");
1503 /* Check that link frequences for all the modes are in device tree */
1504 fq = imx290_check_link_freqs(imx290, &ep);
1506 dev_err(imx290->dev, "Link frequency of %lld is not supported\n",
1515 v4l2_fwnode_endpoint_free(&ep);
1519 static int imx290_probe(struct i2c_client *client)
1521 struct device *dev = &client->dev;
1522 struct imx290 *imx290;
1525 imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
1530 imx290->regmap = devm_cci_regmap_init_i2c(client, 16);
1531 if (IS_ERR(imx290->regmap)) {
1532 dev_err(dev, "Unable to initialize I2C\n");
1536 ret = imx290_parse_dt(imx290);
1540 /* Acquire resources. */
1541 imx290->xclk = devm_clk_get(dev, "xclk");
1542 if (IS_ERR(imx290->xclk))
1543 return dev_err_probe(dev, PTR_ERR(imx290->xclk),
1544 "Could not get xclk\n");
1546 ret = imx290_get_regulators(dev, imx290);
1548 return dev_err_probe(dev, ret, "Cannot get regulators\n");
1550 imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset",
1552 if (IS_ERR(imx290->rst_gpio))
1553 return dev_err_probe(dev, PTR_ERR(imx290->rst_gpio),
1554 "Cannot get reset gpio\n");
1556 /* Initialize external clock frequency. */
1557 ret = imx290_init_clk(imx290);
1562 * Enable power management. The driver supports runtime PM, but needs to
1563 * work when runtime PM is disabled in the kernel. To that end, power
1564 * the sensor on manually here.
1566 ret = imx290_power_on(imx290);
1568 dev_err(dev, "Could not power on the device\n");
1573 * Enable runtime PM with autosuspend. As the device has been powered
1574 * manually, mark it as active, and increase the usage count without
1575 * resuming the device.
1577 pm_runtime_set_active(dev);
1578 pm_runtime_get_noresume(dev);
1579 pm_runtime_enable(dev);
1580 pm_runtime_set_autosuspend_delay(dev, 1000);
1581 pm_runtime_use_autosuspend(dev);
1583 /* Initialize the V4L2 subdev. */
1584 ret = imx290_subdev_init(imx290);
1588 v4l2_i2c_subdev_set_name(&imx290->sd, client,
1589 imx290->model->name, NULL);
1592 * Finally, register the V4L2 subdev. This must be done after
1593 * initializing everything as the subdev can be used immediately after
1596 ret = v4l2_async_register_subdev(&imx290->sd);
1598 dev_err(dev, "Could not register v4l2 device\n");
1603 * Decrease the PM usage count. The device will get suspended after the
1604 * autosuspend delay, turning the power off.
1606 pm_runtime_mark_last_busy(dev);
1607 pm_runtime_put_autosuspend(dev);
1612 imx290_subdev_cleanup(imx290);
1614 pm_runtime_disable(dev);
1615 pm_runtime_put_noidle(dev);
1616 imx290_power_off(imx290);
1620 static void imx290_remove(struct i2c_client *client)
1622 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1623 struct imx290 *imx290 = to_imx290(sd);
1625 v4l2_async_unregister_subdev(sd);
1626 imx290_subdev_cleanup(imx290);
1629 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1630 * make sure to turn power off manually.
1632 pm_runtime_disable(imx290->dev);
1633 if (!pm_runtime_status_suspended(imx290->dev))
1634 imx290_power_off(imx290);
1635 pm_runtime_set_suspended(imx290->dev);
1638 static const struct of_device_id imx290_of_match[] = {
1640 /* Deprecated - synonym for "sony,imx290lqr" */
1641 .compatible = "sony,imx290",
1642 .data = &imx290_models[IMX290_MODEL_IMX290LQR],
1644 .compatible = "sony,imx290lqr",
1645 .data = &imx290_models[IMX290_MODEL_IMX290LQR],
1647 .compatible = "sony,imx290llr",
1648 .data = &imx290_models[IMX290_MODEL_IMX290LLR],
1650 .compatible = "sony,imx327lqr",
1651 .data = &imx290_models[IMX290_MODEL_IMX327LQR],
1655 MODULE_DEVICE_TABLE(of, imx290_of_match);
1657 static struct i2c_driver imx290_i2c_driver = {
1658 .probe = imx290_probe,
1659 .remove = imx290_remove,
1662 .pm = pm_ptr(&imx290_pm_ops),
1663 .of_match_table = imx290_of_match,
1667 module_i2c_driver(imx290_i2c_driver);
1669 MODULE_DESCRIPTION("Sony IMX290 CMOS Image Sensor Driver");
1670 MODULE_AUTHOR("FRAMOS GmbH");
1671 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
1672 MODULE_LICENSE("GPL v2");