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.
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 <linux/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-fwnode.h>
28 #include <media/v4l2-subdev.h>
30 #define IMX290_STANDBY CCI_REG8(0x3000)
31 #define IMX290_REGHOLD CCI_REG8(0x3001)
32 #define IMX290_XMSTA CCI_REG8(0x3002)
33 #define IMX290_ADBIT CCI_REG8(0x3005)
34 #define IMX290_ADBIT_10BIT (0 << 0)
35 #define IMX290_ADBIT_12BIT (1 << 0)
36 #define IMX290_CTRL_07 CCI_REG8(0x3007)
37 #define IMX290_VREVERSE BIT(0)
38 #define IMX290_HREVERSE BIT(1)
39 #define IMX290_WINMODE_1080P (0 << 4)
40 #define IMX290_WINMODE_720P (1 << 4)
41 #define IMX290_WINMODE_CROP (4 << 4)
42 #define IMX290_FR_FDG_SEL CCI_REG8(0x3009)
43 #define IMX290_BLKLEVEL CCI_REG16_LE(0x300a)
44 #define IMX290_GAIN CCI_REG8(0x3014)
45 #define IMX290_VMAX CCI_REG24_LE(0x3018)
46 #define IMX290_VMAX_MAX 0x3ffff
47 #define IMX290_HMAX CCI_REG16_LE(0x301c)
48 #define IMX290_HMAX_MAX 0xffff
49 #define IMX290_SHS1 CCI_REG24_LE(0x3020)
50 #define IMX290_WINWV_OB CCI_REG8(0x303a)
51 #define IMX290_WINPV CCI_REG16_LE(0x303c)
52 #define IMX290_WINWV CCI_REG16_LE(0x303e)
53 #define IMX290_WINPH CCI_REG16_LE(0x3040)
54 #define IMX290_WINWH CCI_REG16_LE(0x3042)
55 #define IMX290_OUT_CTRL CCI_REG8(0x3046)
56 #define IMX290_ODBIT_10BIT (0 << 0)
57 #define IMX290_ODBIT_12BIT (1 << 0)
58 #define IMX290_OPORTSEL_PARALLEL (0x0 << 4)
59 #define IMX290_OPORTSEL_LVDS_2CH (0xd << 4)
60 #define IMX290_OPORTSEL_LVDS_4CH (0xe << 4)
61 #define IMX290_OPORTSEL_LVDS_8CH (0xf << 4)
62 #define IMX290_XSOUTSEL CCI_REG8(0x304b)
63 #define IMX290_XSOUTSEL_XVSOUTSEL_HIGH (0 << 0)
64 #define IMX290_XSOUTSEL_XVSOUTSEL_VSYNC (2 << 0)
65 #define IMX290_XSOUTSEL_XHSOUTSEL_HIGH (0 << 2)
66 #define IMX290_XSOUTSEL_XHSOUTSEL_HSYNC (2 << 2)
67 #define IMX290_INCKSEL1 CCI_REG8(0x305c)
68 #define IMX290_INCKSEL2 CCI_REG8(0x305d)
69 #define IMX290_INCKSEL3 CCI_REG8(0x305e)
70 #define IMX290_INCKSEL4 CCI_REG8(0x305f)
71 #define IMX290_PGCTRL CCI_REG8(0x308c)
72 #define IMX290_ADBIT1 CCI_REG8(0x3129)
73 #define IMX290_ADBIT1_10BIT 0x1d
74 #define IMX290_ADBIT1_12BIT 0x00
75 #define IMX290_INCKSEL5 CCI_REG8(0x315e)
76 #define IMX290_INCKSEL6 CCI_REG8(0x3164)
77 #define IMX290_ADBIT2 CCI_REG8(0x317c)
78 #define IMX290_ADBIT2_10BIT 0x12
79 #define IMX290_ADBIT2_12BIT 0x00
80 #define IMX290_ADBIT3 CCI_REG8(0x31ec)
81 #define IMX290_ADBIT3_10BIT 0x37
82 #define IMX290_ADBIT3_12BIT 0x0e
83 #define IMX290_REPETITION CCI_REG8(0x3405)
84 #define IMX290_PHY_LANE_NUM CCI_REG8(0x3407)
85 #define IMX290_OPB_SIZE_V CCI_REG8(0x3414)
86 #define IMX290_Y_OUT_SIZE CCI_REG16_LE(0x3418)
87 #define IMX290_CSI_DT_FMT CCI_REG16_LE(0x3441)
88 #define IMX290_CSI_DT_FMT_RAW10 0x0a0a
89 #define IMX290_CSI_DT_FMT_RAW12 0x0c0c
90 #define IMX290_CSI_LANE_MODE CCI_REG8(0x3443)
91 #define IMX290_EXTCK_FREQ CCI_REG16_LE(0x3444)
92 #define IMX290_TCLKPOST CCI_REG16_LE(0x3446)
93 #define IMX290_THSZERO CCI_REG16_LE(0x3448)
94 #define IMX290_THSPREPARE CCI_REG16_LE(0x344a)
95 #define IMX290_TCLKTRAIL CCI_REG16_LE(0x344c)
96 #define IMX290_THSTRAIL CCI_REG16_LE(0x344e)
97 #define IMX290_TCLKZERO CCI_REG16_LE(0x3450)
98 #define IMX290_TCLKPREPARE CCI_REG16_LE(0x3452)
99 #define IMX290_TLPX CCI_REG16_LE(0x3454)
100 #define IMX290_X_OUT_SIZE CCI_REG16_LE(0x3472)
101 #define IMX290_INCKSEL7 CCI_REG8(0x3480)
103 #define IMX290_PGCTRL_REGEN BIT(0)
104 #define IMX290_PGCTRL_THRU BIT(1)
105 #define IMX290_PGCTRL_MODE(n) ((n) << 4)
107 /* Number of lines by which exposure must be less than VMAX */
108 #define IMX290_EXPOSURE_OFFSET 2
110 #define IMX290_PIXEL_RATE 148500000
113 * The IMX290 pixel array is organized as follows:
115 * +------------------------------------+
116 * | Optical Black | } Vertical effective optical black (10)
117 * +---+------------------------------------+---+
118 * | | | | } Effective top margin (8)
119 * | | +----------------------------+ | | \
123 * | | | Recording Pixel Area | | | | Recommended height (1080)
127 * | | +----------------------------+ | | /
128 * | | | | } Effective bottom margin (9)
129 * +---+------------------------------------+---+
130 * <-> <-> <--------------------------> <-> <->
131 * \---- Ignored right margin (4)
132 * \-------- Effective right margin (9)
133 * \------------------------- Recommended width (1920)
134 * \----------------------------------------- Effective left margin (8)
135 * \--------------------------------------------- Ignored left margin (4)
137 * The optical black lines are output over CSI-2 with a separate data type.
139 * The pixel array is meant to have 1920x1080 usable pixels after image
140 * processing in an ISP. It has 8 (9) extra active pixels usable for color
141 * processing in the ISP on the top and left (bottom and right) sides of the
142 * image. In addition, 4 additional pixels are present on the left and right
143 * sides of the image, documented as "ignored area".
145 * As far as is understood, all pixels of the pixel array (ignored area, color
146 * processing margins and recording area) can be output by the sensor.
149 #define IMX290_PIXEL_ARRAY_WIDTH 1945
150 #define IMX290_PIXEL_ARRAY_HEIGHT 1097
151 #define IMX290_PIXEL_ARRAY_MARGIN_LEFT 12
152 #define IMX290_PIXEL_ARRAY_MARGIN_RIGHT 13
153 #define IMX290_PIXEL_ARRAY_MARGIN_TOP 8
154 #define IMX290_PIXEL_ARRAY_MARGIN_BOTTOM 9
155 #define IMX290_PIXEL_ARRAY_RECORDING_WIDTH 1920
156 #define IMX290_PIXEL_ARRAY_RECORDING_HEIGHT 1080
158 /* Equivalent value for 16bpp */
159 #define IMX290_BLACK_LEVEL_DEFAULT 3840
161 #define IMX290_NUM_SUPPLIES 3
163 enum imx290_colour_variant {
164 IMX290_VARIANT_COLOUR,
170 IMX290_MODEL_IMX290LQR,
171 IMX290_MODEL_IMX290LLR,
172 IMX290_MODEL_IMX327LQR,
173 IMX290_MODEL_IMX462LQR,
174 IMX290_MODEL_IMX462LLR,
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;
181 unsigned int max_analog_gain;
185 enum imx290_clk_freq {
192 * Clock configuration for registers INCKSEL1 to INCKSEL6.
194 struct imx290_clk_cfg {
211 const struct cci_reg_sequence *data;
214 const struct imx290_clk_cfg *clk_cfg;
217 struct imx290_csi_cfg {
232 struct regmap *regmap;
233 enum imx290_clk_freq xclk_idx;
235 const struct imx290_model_info *model;
237 struct v4l2_subdev sd;
238 struct media_pad pad;
240 const struct imx290_mode *current_mode;
242 struct regulator_bulk_data supplies[IMX290_NUM_SUPPLIES];
243 struct gpio_desc *rst_gpio;
245 struct v4l2_ctrl_handler ctrls;
246 struct v4l2_ctrl *link_freq;
247 struct v4l2_ctrl *hblank;
248 struct v4l2_ctrl *vblank;
249 struct v4l2_ctrl *exposure;
251 struct v4l2_ctrl *hflip;
252 struct v4l2_ctrl *vflip;
256 static inline struct imx290 *to_imx290(struct v4l2_subdev *_sd)
258 return container_of(_sd, struct imx290, sd);
261 /* -----------------------------------------------------------------------------
265 static const struct cci_reg_sequence imx290_global_init_settings[] = {
266 { IMX290_WINWV_OB, 12 },
269 { IMX290_WINWH, 1948 },
270 { IMX290_WINWV, 1097 },
271 { IMX290_XSOUTSEL, IMX290_XSOUTSEL_XVSOUTSEL_VSYNC |
272 IMX290_XSOUTSEL_XHSOUTSEL_HSYNC },
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(0x3011), 0x00 },
281 { CCI_REG8(0x3016), 0x09 },
282 { CCI_REG8(0x3070), 0x02 },
283 { CCI_REG8(0x3071), 0x11 },
284 { CCI_REG8(0x309b), 0x10 },
285 { CCI_REG8(0x309c), 0x22 },
286 { CCI_REG8(0x30a2), 0x02 },
287 { CCI_REG8(0x30a6), 0x20 },
288 { CCI_REG8(0x30a8), 0x20 },
289 { CCI_REG8(0x30aa), 0x20 },
290 { CCI_REG8(0x30ac), 0x20 },
291 { CCI_REG8(0x30b0), 0x43 },
292 { CCI_REG8(0x3119), 0x9e },
293 { CCI_REG8(0x311c), 0x1e },
294 { CCI_REG8(0x311e), 0x08 },
295 { CCI_REG8(0x3128), 0x05 },
296 { CCI_REG8(0x313d), 0x83 },
297 { CCI_REG8(0x3150), 0x03 },
298 { CCI_REG8(0x317e), 0x00 },
299 { CCI_REG8(0x32b8), 0x50 },
300 { CCI_REG8(0x32b9), 0x10 },
301 { CCI_REG8(0x32ba), 0x00 },
302 { CCI_REG8(0x32bb), 0x04 },
303 { CCI_REG8(0x32c8), 0x50 },
304 { CCI_REG8(0x32c9), 0x10 },
305 { CCI_REG8(0x32ca), 0x00 },
306 { CCI_REG8(0x32cb), 0x04 },
307 { CCI_REG8(0x332c), 0xd3 },
308 { CCI_REG8(0x332d), 0x10 },
309 { CCI_REG8(0x332e), 0x0d },
310 { CCI_REG8(0x3358), 0x06 },
311 { CCI_REG8(0x3359), 0xe1 },
312 { CCI_REG8(0x335a), 0x11 },
313 { CCI_REG8(0x3360), 0x1e },
314 { CCI_REG8(0x3361), 0x61 },
315 { CCI_REG8(0x3362), 0x10 },
316 { CCI_REG8(0x33b0), 0x50 },
317 { CCI_REG8(0x33b2), 0x1a },
318 { CCI_REG8(0x33b3), 0x04 },
321 static const struct cci_reg_sequence imx290_global_init_settings_462[] = {
322 { CCI_REG8(0x300f), 0x00 },
323 { CCI_REG8(0x3010), 0x21 },
324 { CCI_REG8(0x3011), 0x02 },
325 { CCI_REG8(0x3016), 0x09 },
326 { CCI_REG8(0x3070), 0x02 },
327 { CCI_REG8(0x3071), 0x11 },
328 { CCI_REG8(0x309b), 0x10 },
329 { CCI_REG8(0x309c), 0x22 },
330 { CCI_REG8(0x30a2), 0x02 },
331 { CCI_REG8(0x30a6), 0x20 },
332 { CCI_REG8(0x30a8), 0x20 },
333 { CCI_REG8(0x30aa), 0x20 },
334 { CCI_REG8(0x30ac), 0x20 },
335 { CCI_REG8(0x30b0), 0x43 },
336 { CCI_REG8(0x3119), 0x9e },
337 { CCI_REG8(0x311c), 0x1e },
338 { CCI_REG8(0x311e), 0x08 },
339 { CCI_REG8(0x3128), 0x05 },
340 { CCI_REG8(0x313d), 0x83 },
341 { CCI_REG8(0x3150), 0x03 },
342 { CCI_REG8(0x317e), 0x00 },
343 { CCI_REG8(0x32b8), 0x50 },
344 { CCI_REG8(0x32b9), 0x10 },
345 { CCI_REG8(0x32ba), 0x00 },
346 { CCI_REG8(0x32bb), 0x04 },
347 { CCI_REG8(0x32c8), 0x50 },
348 { CCI_REG8(0x32c9), 0x10 },
349 { CCI_REG8(0x32ca), 0x00 },
350 { CCI_REG8(0x32cb), 0x04 },
351 { CCI_REG8(0x332c), 0xd3 },
352 { CCI_REG8(0x332d), 0x10 },
353 { CCI_REG8(0x332e), 0x0d },
354 { CCI_REG8(0x3358), 0x06 },
355 { CCI_REG8(0x3359), 0xe1 },
356 { CCI_REG8(0x335a), 0x11 },
357 { CCI_REG8(0x3360), 0x1e },
358 { CCI_REG8(0x3361), 0x61 },
359 { CCI_REG8(0x3362), 0x10 },
360 { CCI_REG8(0x33b0), 0x50 },
361 { CCI_REG8(0x33b2), 0x1a },
362 { CCI_REG8(0x33b3), 0x04 },
365 #define IMX290_NUM_CLK_REGS 2
366 static const struct cci_reg_sequence xclk_regs[][IMX290_NUM_CLK_REGS] = {
367 [IMX290_CLK_37_125] = {
368 { IMX290_EXTCK_FREQ, (37125 * 256) / 1000 },
369 { IMX290_INCKSEL7, 0x49 },
371 [IMX290_CLK_74_25] = {
372 { IMX290_EXTCK_FREQ, (74250 * 256) / 1000 },
373 { IMX290_INCKSEL7, 0x92 },
377 static const struct cci_reg_sequence imx290_global_init_settings_327[] = {
378 { CCI_REG8(0x3011), 0x02 },
379 { CCI_REG8(0x309e), 0x4A },
380 { CCI_REG8(0x309f), 0x4A },
381 { CCI_REG8(0x313b), 0x61 },
384 static const struct cci_reg_sequence imx290_1080p_settings[] = {
386 { IMX290_WINWV_OB, 12 },
387 { IMX290_OPB_SIZE_V, 10 },
388 { IMX290_X_OUT_SIZE, 1920 },
389 { IMX290_Y_OUT_SIZE, 1080 },
392 static const struct cci_reg_sequence imx290_720p_settings[] = {
394 { IMX290_WINWV_OB, 6 },
395 { IMX290_OPB_SIZE_V, 4 },
396 { IMX290_X_OUT_SIZE, 1280 },
397 { IMX290_Y_OUT_SIZE, 720 },
400 static const struct cci_reg_sequence imx290_10bit_settings[] = {
401 { IMX290_ADBIT, IMX290_ADBIT_10BIT },
402 { IMX290_OUT_CTRL, IMX290_ODBIT_10BIT },
403 { IMX290_ADBIT1, IMX290_ADBIT1_10BIT },
404 { IMX290_ADBIT2, IMX290_ADBIT2_10BIT },
405 { IMX290_ADBIT3, IMX290_ADBIT3_10BIT },
406 { IMX290_CSI_DT_FMT, IMX290_CSI_DT_FMT_RAW10 },
409 static const struct cci_reg_sequence imx290_12bit_settings[] = {
410 { IMX290_ADBIT, IMX290_ADBIT_12BIT },
411 { IMX290_OUT_CTRL, IMX290_ODBIT_12BIT },
412 { IMX290_ADBIT1, IMX290_ADBIT1_12BIT },
413 { IMX290_ADBIT2, IMX290_ADBIT2_12BIT },
414 { IMX290_ADBIT3, IMX290_ADBIT3_12BIT },
415 { IMX290_CSI_DT_FMT, IMX290_CSI_DT_FMT_RAW12 },
418 static const struct imx290_csi_cfg imx290_csi_222_75mhz = {
419 /* 222.75MHz or 445.5Mbit/s per lane */
431 static const struct imx290_csi_cfg imx290_csi_445_5mhz = {
432 /* 445.5MHz or 891Mbit/s per lane */
444 static const struct imx290_csi_cfg imx290_csi_148_5mhz = {
445 /* 148.5MHz or 297Mbit/s per lane */
457 static const struct imx290_csi_cfg imx290_csi_297mhz = {
458 /* 297MHz or 594Mbit/s per lane */
470 /* supported link frequencies */
471 #define FREQ_INDEX_1080P 0
472 #define FREQ_INDEX_720P 1
473 static const s64 imx290_link_freq_2lanes[] = {
474 [FREQ_INDEX_1080P] = 445500000,
475 [FREQ_INDEX_720P] = 297000000,
478 static const s64 imx290_link_freq_4lanes[] = {
479 [FREQ_INDEX_1080P] = 222750000,
480 [FREQ_INDEX_720P] = 148500000,
484 * In this function and in the similar ones below We rely on imx290_probe()
485 * to ensure that nlanes is either 2 or 4.
487 static inline const s64 *imx290_link_freqs_ptr(const struct imx290 *imx290)
489 if (imx290->nlanes == 2)
490 return imx290_link_freq_2lanes;
492 return imx290_link_freq_4lanes;
495 static inline int imx290_link_freqs_num(const struct imx290 *imx290)
497 if (imx290->nlanes == 2)
498 return ARRAY_SIZE(imx290_link_freq_2lanes);
500 return ARRAY_SIZE(imx290_link_freq_4lanes);
503 static const struct imx290_clk_cfg imx290_1080p_clock_config[] = {
504 [IMX290_CLK_37_125] = {
505 /* 37.125MHz clock config */
513 [IMX290_CLK_74_25] = {
514 /* 74.25MHz clock config */
524 static const struct imx290_clk_cfg imx290_720p_clock_config[] = {
525 [IMX290_CLK_37_125] = {
526 /* 37.125MHz clock config */
534 [IMX290_CLK_74_25] = {
535 /* 74.25MHz clock config */
546 static const struct imx290_mode imx290_modes_2lanes[] = {
552 .link_freq_index = FREQ_INDEX_1080P,
553 .ctrl_07 = IMX290_WINMODE_1080P,
554 .data = imx290_1080p_settings,
555 .data_size = ARRAY_SIZE(imx290_1080p_settings),
556 .clk_cfg = imx290_1080p_clock_config,
563 .link_freq_index = FREQ_INDEX_720P,
564 .ctrl_07 = IMX290_WINMODE_720P,
565 .data = imx290_720p_settings,
566 .data_size = ARRAY_SIZE(imx290_720p_settings),
567 .clk_cfg = imx290_720p_clock_config,
571 static const struct imx290_mode imx290_modes_4lanes[] = {
577 .link_freq_index = FREQ_INDEX_1080P,
578 .ctrl_07 = IMX290_WINMODE_1080P,
579 .data = imx290_1080p_settings,
580 .data_size = ARRAY_SIZE(imx290_1080p_settings),
581 .clk_cfg = imx290_1080p_clock_config,
588 .link_freq_index = FREQ_INDEX_720P,
589 .ctrl_07 = IMX290_WINMODE_720P,
590 .data = imx290_720p_settings,
591 .data_size = ARRAY_SIZE(imx290_720p_settings),
592 .clk_cfg = imx290_720p_clock_config,
596 static inline const struct imx290_mode *imx290_modes_ptr(const struct imx290 *imx290)
598 if (imx290->nlanes == 2)
599 return imx290_modes_2lanes;
601 return imx290_modes_4lanes;
604 static inline int imx290_modes_num(const struct imx290 *imx290)
606 if (imx290->nlanes == 2)
607 return ARRAY_SIZE(imx290_modes_2lanes);
609 return ARRAY_SIZE(imx290_modes_4lanes);
612 struct imx290_format_info {
613 u32 code[IMX290_VARIANT_MAX];
615 const struct cci_reg_sequence *regs;
616 unsigned int num_regs;
619 static const struct imx290_format_info imx290_formats[] = {
622 [IMX290_VARIANT_COLOUR] = MEDIA_BUS_FMT_SRGGB10_1X10,
623 [IMX290_VARIANT_MONO] = MEDIA_BUS_FMT_Y10_1X10
626 .regs = imx290_10bit_settings,
627 .num_regs = ARRAY_SIZE(imx290_10bit_settings),
630 [IMX290_VARIANT_COLOUR] = MEDIA_BUS_FMT_SRGGB12_1X12,
631 [IMX290_VARIANT_MONO] = MEDIA_BUS_FMT_Y12_1X12
634 .regs = imx290_12bit_settings,
635 .num_regs = ARRAY_SIZE(imx290_12bit_settings),
639 static const struct imx290_format_info *
640 imx290_format_info(const struct imx290 *imx290, u32 code)
644 for (i = 0; i < ARRAY_SIZE(imx290_formats); ++i) {
645 const struct imx290_format_info *info = &imx290_formats[i];
647 if (info->code[imx290->model->colour_variant] == code)
654 static int imx290_set_register_array(struct imx290 *imx290,
655 const struct cci_reg_sequence *settings,
656 unsigned int num_settings)
660 ret = cci_multi_reg_write(imx290->regmap, settings, num_settings, NULL);
664 /* Provide 10ms settle time */
665 usleep_range(10000, 11000);
670 static int imx290_set_clock(struct imx290 *imx290)
672 const struct imx290_mode *mode = imx290->current_mode;
673 enum imx290_clk_freq clk_idx = imx290->xclk_idx;
674 const struct imx290_clk_cfg *clk_cfg = &mode->clk_cfg[clk_idx];
677 ret = imx290_set_register_array(imx290, xclk_regs[clk_idx],
678 IMX290_NUM_CLK_REGS);
680 cci_write(imx290->regmap, IMX290_INCKSEL1, clk_cfg->incksel1, &ret);
681 cci_write(imx290->regmap, IMX290_INCKSEL2, clk_cfg->incksel2, &ret);
682 cci_write(imx290->regmap, IMX290_INCKSEL3, clk_cfg->incksel3, &ret);
683 cci_write(imx290->regmap, IMX290_INCKSEL4, clk_cfg->incksel4, &ret);
684 cci_write(imx290->regmap, IMX290_INCKSEL5, clk_cfg->incksel5, &ret);
685 cci_write(imx290->regmap, IMX290_INCKSEL6, clk_cfg->incksel6, &ret);
690 static int imx290_set_data_lanes(struct imx290 *imx290)
694 cci_write(imx290->regmap, IMX290_PHY_LANE_NUM, imx290->nlanes - 1,
696 cci_write(imx290->regmap, IMX290_CSI_LANE_MODE, imx290->nlanes - 1,
698 cci_write(imx290->regmap, IMX290_FR_FDG_SEL, 0x01, &ret);
703 static int imx290_set_black_level(struct imx290 *imx290,
704 const struct v4l2_mbus_framefmt *format,
705 unsigned int black_level, int *err)
707 unsigned int bpp = imx290_format_info(imx290, format->code)->bpp;
709 return cci_write(imx290->regmap, IMX290_BLKLEVEL,
710 black_level >> (16 - bpp), err);
713 static int imx290_set_csi_config(struct imx290 *imx290)
715 const s64 *link_freqs = imx290_link_freqs_ptr(imx290);
716 const struct imx290_csi_cfg *csi_cfg;
719 switch (link_freqs[imx290->current_mode->link_freq_index]) {
721 csi_cfg = &imx290_csi_445_5mhz;
724 csi_cfg = &imx290_csi_297mhz;
727 csi_cfg = &imx290_csi_222_75mhz;
730 csi_cfg = &imx290_csi_148_5mhz;
736 cci_write(imx290->regmap, IMX290_REPETITION, csi_cfg->repetition, &ret);
737 cci_write(imx290->regmap, IMX290_TCLKPOST, csi_cfg->tclkpost, &ret);
738 cci_write(imx290->regmap, IMX290_THSZERO, csi_cfg->thszero, &ret);
739 cci_write(imx290->regmap, IMX290_THSPREPARE, csi_cfg->thsprepare, &ret);
740 cci_write(imx290->regmap, IMX290_TCLKTRAIL, csi_cfg->tclktrail, &ret);
741 cci_write(imx290->regmap, IMX290_THSTRAIL, csi_cfg->thstrail, &ret);
742 cci_write(imx290->regmap, IMX290_TCLKZERO, csi_cfg->tclkzero, &ret);
743 cci_write(imx290->regmap, IMX290_TCLKPREPARE, csi_cfg->tclkprepare,
745 cci_write(imx290->regmap, IMX290_TLPX, csi_cfg->tlpx, &ret);
750 static int imx290_setup_format(struct imx290 *imx290,
751 const struct v4l2_mbus_framefmt *format)
753 const struct imx290_format_info *info;
756 info = imx290_format_info(imx290, format->code);
758 ret = imx290_set_register_array(imx290, info->regs, info->num_regs);
760 dev_err(imx290->dev, "Could not set format registers\n");
764 return imx290_set_black_level(imx290, format,
765 IMX290_BLACK_LEVEL_DEFAULT, &ret);
768 /* ----------------------------------------------------------------------------
771 static void imx290_exposure_update(struct imx290 *imx290,
772 const struct imx290_mode *mode)
774 unsigned int exposure_max;
776 exposure_max = imx290->vblank->val + mode->height -
777 IMX290_EXPOSURE_OFFSET;
778 __v4l2_ctrl_modify_range(imx290->exposure, 1, exposure_max, 1,
782 static int imx290_set_ctrl(struct v4l2_ctrl *ctrl)
784 struct imx290 *imx290 = container_of(ctrl->handler,
785 struct imx290, ctrls);
786 const struct v4l2_mbus_framefmt *format;
787 struct v4l2_subdev_state *state;
791 * Return immediately for controls that don't need to be applied to the
794 if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
797 if (ctrl->id == V4L2_CID_VBLANK) {
798 /* Changing vblank changes the allowed range for exposure. */
799 imx290_exposure_update(imx290, imx290->current_mode);
802 /* V4L2 controls values will be applied only when power is already up */
803 if (!pm_runtime_get_if_in_use(imx290->dev))
806 state = v4l2_subdev_get_locked_active_state(&imx290->sd);
807 format = v4l2_subdev_state_get_format(state, 0);
810 case V4L2_CID_ANALOGUE_GAIN:
811 ret = cci_write(imx290->regmap, IMX290_GAIN, ctrl->val, NULL);
814 case V4L2_CID_VBLANK:
815 ret = cci_write(imx290->regmap, IMX290_VMAX,
816 ctrl->val + imx290->current_mode->height, NULL);
818 * Due to the way that exposure is programmed in this sensor in
819 * relation to VMAX, we have to reprogramme it whenever VMAX is
821 * Update ctrl so that the V4L2_CID_EXPOSURE case can refer to
824 ctrl = imx290->exposure;
826 case V4L2_CID_EXPOSURE:
827 vmax = imx290->vblank->val + imx290->current_mode->height;
828 ret = cci_write(imx290->regmap, IMX290_SHS1,
829 vmax - ctrl->val - 1, NULL);
832 case V4L2_CID_TEST_PATTERN:
834 imx290_set_black_level(imx290, format, 0, &ret);
835 usleep_range(10000, 11000);
836 cci_write(imx290->regmap, IMX290_PGCTRL,
837 (u8)(IMX290_PGCTRL_REGEN |
839 IMX290_PGCTRL_MODE(ctrl->val)), &ret);
841 cci_write(imx290->regmap, IMX290_PGCTRL, 0x00, &ret);
842 usleep_range(10000, 11000);
843 imx290_set_black_level(imx290, format,
844 IMX290_BLACK_LEVEL_DEFAULT, &ret);
848 case V4L2_CID_HBLANK:
849 ret = cci_write(imx290->regmap, IMX290_HMAX,
850 ctrl->val + imx290->current_mode->width, NULL);
858 reg = imx290->current_mode->ctrl_07;
859 if (imx290->hflip->val)
860 reg |= IMX290_HREVERSE;
861 if (imx290->vflip->val)
862 reg |= IMX290_VREVERSE;
863 ret = cci_write(imx290->regmap, IMX290_CTRL_07, reg, NULL);
872 pm_runtime_mark_last_busy(imx290->dev);
873 pm_runtime_put_autosuspend(imx290->dev);
878 static const struct v4l2_ctrl_ops imx290_ctrl_ops = {
879 .s_ctrl = imx290_set_ctrl,
882 static const char * const imx290_test_pattern_menu[] = {
884 "Sequence Pattern 1",
885 "Horizontal Color-bar Chart",
886 "Vertical Color-bar Chart",
887 "Sequence Pattern 2",
888 "Gradation Pattern 1",
889 "Gradation Pattern 2",
890 "000/555h Toggle Pattern",
893 static void imx290_ctrl_update(struct imx290 *imx290,
894 const struct imx290_mode *mode)
896 unsigned int hblank_min = mode->hmax_min - mode->width;
897 unsigned int hblank_max = IMX290_HMAX_MAX - mode->width;
898 unsigned int vblank_min = mode->vmax_min - mode->height;
899 unsigned int vblank_max = IMX290_VMAX_MAX - mode->height;
901 __v4l2_ctrl_s_ctrl(imx290->link_freq, mode->link_freq_index);
903 __v4l2_ctrl_modify_range(imx290->hblank, hblank_min, hblank_max, 1,
905 __v4l2_ctrl_modify_range(imx290->vblank, vblank_min, vblank_max, 1,
909 static int imx290_ctrl_init(struct imx290 *imx290)
911 struct v4l2_fwnode_device_properties props;
914 ret = v4l2_fwnode_device_parse(imx290->dev, &props);
918 v4l2_ctrl_handler_init(&imx290->ctrls, 11);
921 * The sensor has an analog gain and a digital gain, both controlled
922 * through a single gain value, expressed in 0.3dB increments. Values
923 * from 0.0dB (0) to 30.0dB (100) apply analog gain only, higher values
924 * up to 72.0dB (240) add further digital gain. Limit the range to
925 * analog gain only, support for digital gain can be added separately
928 v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
929 V4L2_CID_ANALOGUE_GAIN, 0,
930 imx290->model->max_analog_gain, 1, 0);
933 * Correct range will be determined through imx290_ctrl_update setting
936 imx290->exposure = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
937 V4L2_CID_EXPOSURE, 1, 65535, 1,
941 * Set the link frequency, pixel rate, horizontal blanking and vertical
942 * blanking to hardcoded values, they will be updated by
943 * imx290_ctrl_update().
946 v4l2_ctrl_new_int_menu(&imx290->ctrls, &imx290_ctrl_ops,
948 imx290_link_freqs_num(imx290) - 1, 0,
949 imx290_link_freqs_ptr(imx290));
950 if (imx290->link_freq)
951 imx290->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
953 v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops, V4L2_CID_PIXEL_RATE,
954 IMX290_PIXEL_RATE, IMX290_PIXEL_RATE, 1,
957 v4l2_ctrl_new_std_menu_items(&imx290->ctrls, &imx290_ctrl_ops,
958 V4L2_CID_TEST_PATTERN,
959 ARRAY_SIZE(imx290_test_pattern_menu) - 1,
960 0, 0, imx290_test_pattern_menu);
963 * Actual range will be set from imx290_ctrl_update later in the probe.
965 imx290->hblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
966 V4L2_CID_HBLANK, 1, 1, 1, 1);
968 imx290->vblank = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
969 V4L2_CID_VBLANK, 1, 1, 1, 1);
971 imx290->hflip = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
972 V4L2_CID_HFLIP, 0, 1, 1, 0);
973 imx290->vflip = v4l2_ctrl_new_std(&imx290->ctrls, &imx290_ctrl_ops,
974 V4L2_CID_VFLIP, 0, 1, 1, 0);
975 v4l2_ctrl_cluster(2, &imx290->hflip);
977 v4l2_ctrl_new_fwnode_properties(&imx290->ctrls, &imx290_ctrl_ops,
980 imx290->sd.ctrl_handler = &imx290->ctrls;
982 if (imx290->ctrls.error) {
983 ret = imx290->ctrls.error;
984 v4l2_ctrl_handler_free(&imx290->ctrls);
991 /* ----------------------------------------------------------------------------
995 /* Start streaming */
996 static int imx290_start_streaming(struct imx290 *imx290,
997 struct v4l2_subdev_state *state)
999 const struct v4l2_mbus_framefmt *format;
1002 /* Set init register settings */
1003 ret = imx290_set_register_array(imx290, imx290_global_init_settings,
1004 ARRAY_SIZE(imx290_global_init_settings));
1006 dev_err(imx290->dev, "Could not set init registers\n");
1010 /* Set mdel specific init register settings */
1011 ret = imx290_set_register_array(imx290, imx290->model->init_regs,
1012 imx290->model->init_regs_num);
1014 dev_err(imx290->dev, "Could not set model specific init registers\n");
1018 /* Set clock parameters based on mode and xclk */
1019 ret = imx290_set_clock(imx290);
1021 dev_err(imx290->dev, "Could not set clocks - %d\n", ret);
1025 /* Set data lane count */
1026 ret = imx290_set_data_lanes(imx290);
1028 dev_err(imx290->dev, "Could not set data lanes - %d\n", ret);
1032 ret = imx290_set_csi_config(imx290);
1034 dev_err(imx290->dev, "Could not set csi cfg - %d\n", ret);
1038 /* Apply the register values related to current frame format */
1039 format = v4l2_subdev_state_get_format(state, 0);
1040 ret = imx290_setup_format(imx290, format);
1042 dev_err(imx290->dev, "Could not set frame format - %d\n", ret);
1046 /* Apply default values of current mode */
1047 ret = imx290_set_register_array(imx290, imx290->current_mode->data,
1048 imx290->current_mode->data_size);
1050 dev_err(imx290->dev, "Could not set current mode - %d\n", ret);
1054 /* Apply customized values from user */
1055 ret = __v4l2_ctrl_handler_setup(imx290->sd.ctrl_handler);
1057 dev_err(imx290->dev, "Could not sync v4l2 controls - %d\n", ret);
1061 cci_write(imx290->regmap, IMX290_STANDBY, 0x00, &ret);
1065 /* Start streaming */
1066 return cci_write(imx290->regmap, IMX290_XMSTA, 0x00, &ret);
1069 /* Stop streaming */
1070 static int imx290_stop_streaming(struct imx290 *imx290)
1074 cci_write(imx290->regmap, IMX290_STANDBY, 0x01, &ret);
1078 return cci_write(imx290->regmap, IMX290_XMSTA, 0x01, &ret);
1081 static int imx290_set_stream(struct v4l2_subdev *sd, int enable)
1083 struct imx290 *imx290 = to_imx290(sd);
1084 struct v4l2_subdev_state *state;
1087 state = v4l2_subdev_lock_and_get_active_state(sd);
1090 ret = pm_runtime_resume_and_get(imx290->dev);
1094 ret = imx290_start_streaming(imx290, state);
1096 dev_err(imx290->dev, "Start stream failed\n");
1097 pm_runtime_put_sync(imx290->dev);
1101 imx290_stop_streaming(imx290);
1102 pm_runtime_mark_last_busy(imx290->dev);
1103 pm_runtime_put_autosuspend(imx290->dev);
1107 * vflip and hflip should not be changed during streaming as the sensor
1108 * will produce an invalid frame.
1110 __v4l2_ctrl_grab(imx290->vflip, enable);
1111 __v4l2_ctrl_grab(imx290->hflip, enable);
1114 v4l2_subdev_unlock_state(state);
1118 static int imx290_enum_mbus_code(struct v4l2_subdev *sd,
1119 struct v4l2_subdev_state *sd_state,
1120 struct v4l2_subdev_mbus_code_enum *code)
1122 const struct imx290 *imx290 = to_imx290(sd);
1124 if (code->index >= ARRAY_SIZE(imx290_formats))
1127 code->code = imx290_formats[code->index].code[imx290->model->colour_variant];
1132 static int imx290_enum_frame_size(struct v4l2_subdev *sd,
1133 struct v4l2_subdev_state *sd_state,
1134 struct v4l2_subdev_frame_size_enum *fse)
1136 const struct imx290 *imx290 = to_imx290(sd);
1137 const struct imx290_mode *imx290_modes = imx290_modes_ptr(imx290);
1139 if (!imx290_format_info(imx290, fse->code))
1142 if (fse->index >= imx290_modes_num(imx290))
1145 fse->min_width = imx290_modes[fse->index].width;
1146 fse->max_width = imx290_modes[fse->index].width;
1147 fse->min_height = imx290_modes[fse->index].height;
1148 fse->max_height = imx290_modes[fse->index].height;
1153 static int imx290_set_fmt(struct v4l2_subdev *sd,
1154 struct v4l2_subdev_state *sd_state,
1155 struct v4l2_subdev_format *fmt)
1157 struct imx290 *imx290 = to_imx290(sd);
1158 const struct imx290_mode *mode;
1159 struct v4l2_mbus_framefmt *format;
1161 mode = v4l2_find_nearest_size(imx290_modes_ptr(imx290),
1162 imx290_modes_num(imx290), width, height,
1163 fmt->format.width, fmt->format.height);
1165 fmt->format.width = mode->width;
1166 fmt->format.height = mode->height;
1168 if (!imx290_format_info(imx290, fmt->format.code))
1169 fmt->format.code = imx290_formats[0].code[imx290->model->colour_variant];
1171 fmt->format.field = V4L2_FIELD_NONE;
1172 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
1173 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_601;
1174 fmt->format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1175 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
1177 format = v4l2_subdev_state_get_format(sd_state, 0);
1179 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1180 imx290->current_mode = mode;
1182 imx290_ctrl_update(imx290, mode);
1183 imx290_exposure_update(imx290, mode);
1186 *format = fmt->format;
1191 static int imx290_get_selection(struct v4l2_subdev *sd,
1192 struct v4l2_subdev_state *sd_state,
1193 struct v4l2_subdev_selection *sel)
1195 struct imx290 *imx290 = to_imx290(sd);
1196 struct v4l2_mbus_framefmt *format;
1198 switch (sel->target) {
1199 case V4L2_SEL_TGT_CROP: {
1200 format = v4l2_subdev_state_get_format(sd_state, 0);
1203 * The sensor moves the readout by 1 pixel based on flips to
1204 * keep the Bayer order the same.
1206 sel->r.top = IMX290_PIXEL_ARRAY_MARGIN_TOP
1207 + (IMX290_PIXEL_ARRAY_RECORDING_HEIGHT - format->height) / 2
1208 + imx290->vflip->val;
1209 sel->r.left = IMX290_PIXEL_ARRAY_MARGIN_LEFT
1210 + (IMX290_PIXEL_ARRAY_RECORDING_WIDTH - format->width) / 2
1211 + imx290->hflip->val;
1212 sel->r.width = format->width;
1213 sel->r.height = format->height;
1218 case V4L2_SEL_TGT_NATIVE_SIZE:
1219 case V4L2_SEL_TGT_CROP_BOUNDS:
1222 sel->r.width = IMX290_PIXEL_ARRAY_WIDTH;
1223 sel->r.height = IMX290_PIXEL_ARRAY_HEIGHT;
1227 case V4L2_SEL_TGT_CROP_DEFAULT:
1228 sel->r.top = IMX290_PIXEL_ARRAY_MARGIN_TOP;
1229 sel->r.left = IMX290_PIXEL_ARRAY_MARGIN_LEFT;
1230 sel->r.width = IMX290_PIXEL_ARRAY_RECORDING_WIDTH;
1231 sel->r.height = IMX290_PIXEL_ARRAY_RECORDING_HEIGHT;
1240 static int imx290_entity_init_state(struct v4l2_subdev *subdev,
1241 struct v4l2_subdev_state *sd_state)
1243 struct v4l2_subdev_format fmt = {
1244 .which = V4L2_SUBDEV_FORMAT_TRY,
1251 imx290_set_fmt(subdev, sd_state, &fmt);
1256 static const struct v4l2_subdev_video_ops imx290_video_ops = {
1257 .s_stream = imx290_set_stream,
1260 static const struct v4l2_subdev_pad_ops imx290_pad_ops = {
1261 .enum_mbus_code = imx290_enum_mbus_code,
1262 .enum_frame_size = imx290_enum_frame_size,
1263 .get_fmt = v4l2_subdev_get_fmt,
1264 .set_fmt = imx290_set_fmt,
1265 .get_selection = imx290_get_selection,
1268 static const struct v4l2_subdev_ops imx290_subdev_ops = {
1269 .video = &imx290_video_ops,
1270 .pad = &imx290_pad_ops,
1273 static const struct v4l2_subdev_internal_ops imx290_internal_ops = {
1274 .init_state = imx290_entity_init_state,
1277 static const struct media_entity_operations imx290_subdev_entity_ops = {
1278 .link_validate = v4l2_subdev_link_validate,
1281 static int imx290_subdev_init(struct imx290 *imx290)
1283 struct i2c_client *client = to_i2c_client(imx290->dev);
1284 struct v4l2_subdev_state *state;
1287 imx290->current_mode = &imx290_modes_ptr(imx290)[0];
1290 * After linking the subdev with the imx290 instance, we are allowed to
1291 * use the pm_runtime functions. Decrease the PM usage count. The device
1292 * will get suspended after the autosuspend delay, turning the power
1293 * off. However, the communication happening in imx290_ctrl_update()
1294 * will already be prevented even before the delay.
1296 v4l2_i2c_subdev_init(&imx290->sd, client, &imx290_subdev_ops);
1297 imx290->sd.dev = imx290->dev;
1298 pm_runtime_mark_last_busy(imx290->dev);
1299 pm_runtime_put_autosuspend(imx290->dev);
1301 imx290->sd.internal_ops = &imx290_internal_ops;
1302 imx290->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1303 imx290->sd.entity.ops = &imx290_subdev_entity_ops;
1304 imx290->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1306 imx290->pad.flags = MEDIA_PAD_FL_SOURCE;
1307 ret = media_entity_pads_init(&imx290->sd.entity, 1, &imx290->pad);
1309 dev_err(imx290->dev, "Could not register media entity\n");
1313 ret = imx290_ctrl_init(imx290);
1315 dev_err(imx290->dev, "Control initialization error %d\n", ret);
1319 imx290->sd.state_lock = imx290->ctrls.lock;
1321 ret = v4l2_subdev_init_finalize(&imx290->sd);
1323 dev_err(imx290->dev, "subdev initialization error %d\n", ret);
1327 state = v4l2_subdev_lock_and_get_active_state(&imx290->sd);
1328 imx290_ctrl_update(imx290, imx290->current_mode);
1329 v4l2_subdev_unlock_state(state);
1334 v4l2_ctrl_handler_free(&imx290->ctrls);
1336 media_entity_cleanup(&imx290->sd.entity);
1340 static void imx290_subdev_cleanup(struct imx290 *imx290)
1342 v4l2_subdev_cleanup(&imx290->sd);
1343 media_entity_cleanup(&imx290->sd.entity);
1344 v4l2_ctrl_handler_free(&imx290->ctrls);
1347 /* ----------------------------------------------------------------------------
1351 static int imx290_power_on(struct imx290 *imx290)
1355 ret = clk_prepare_enable(imx290->xclk);
1357 dev_err(imx290->dev, "Failed to enable clock\n");
1361 ret = regulator_bulk_enable(ARRAY_SIZE(imx290->supplies),
1364 dev_err(imx290->dev, "Failed to enable regulators\n");
1365 clk_disable_unprepare(imx290->xclk);
1370 gpiod_set_value_cansleep(imx290->rst_gpio, 0);
1371 usleep_range(30000, 31000);
1376 static void imx290_power_off(struct imx290 *imx290)
1378 clk_disable_unprepare(imx290->xclk);
1379 gpiod_set_value_cansleep(imx290->rst_gpio, 1);
1380 regulator_bulk_disable(ARRAY_SIZE(imx290->supplies), imx290->supplies);
1383 static int imx290_runtime_resume(struct device *dev)
1385 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1386 struct imx290 *imx290 = to_imx290(sd);
1388 return imx290_power_on(imx290);
1391 static int imx290_runtime_suspend(struct device *dev)
1393 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1394 struct imx290 *imx290 = to_imx290(sd);
1396 imx290_power_off(imx290);
1401 static const struct dev_pm_ops imx290_pm_ops = {
1402 RUNTIME_PM_OPS(imx290_runtime_suspend, imx290_runtime_resume, NULL)
1405 /* ----------------------------------------------------------------------------
1409 static const char * const imx290_supply_name[IMX290_NUM_SUPPLIES] = {
1415 static int imx290_get_regulators(struct device *dev, struct imx290 *imx290)
1419 for (i = 0; i < ARRAY_SIZE(imx290->supplies); i++)
1420 imx290->supplies[i].supply = imx290_supply_name[i];
1422 return devm_regulator_bulk_get(dev, ARRAY_SIZE(imx290->supplies),
1426 static int imx290_init_clk(struct imx290 *imx290)
1431 ret = device_property_read_u32(imx290->dev, "clock-frequency",
1434 dev_err(imx290->dev, "Could not get xclk frequency\n");
1438 /* external clock must be 37.125 MHz or 74.25MHz */
1439 switch (xclk_freq) {
1441 imx290->xclk_idx = IMX290_CLK_37_125;
1444 imx290->xclk_idx = IMX290_CLK_74_25;
1447 dev_err(imx290->dev, "External clock frequency %u is not supported\n",
1452 ret = clk_set_rate(imx290->xclk, xclk_freq);
1454 dev_err(imx290->dev, "Could not set xclk frequency\n");
1462 * Returns 0 if all link frequencies used by the driver for the given number
1463 * of MIPI data lanes are mentioned in the device tree, or the value of the
1464 * first missing frequency otherwise.
1466 static s64 imx290_check_link_freqs(const struct imx290 *imx290,
1467 const struct v4l2_fwnode_endpoint *ep)
1470 const s64 *freqs = imx290_link_freqs_ptr(imx290);
1471 int freqs_count = imx290_link_freqs_num(imx290);
1473 for (i = 0; i < freqs_count; i++) {
1474 for (j = 0; j < ep->nr_of_link_frequencies; j++)
1475 if (freqs[i] == ep->link_frequencies[j])
1477 if (j == ep->nr_of_link_frequencies)
1483 static const struct imx290_model_info imx290_models[] = {
1484 [IMX290_MODEL_IMX290LQR] = {
1485 .colour_variant = IMX290_VARIANT_COLOUR,
1486 .init_regs = imx290_global_init_settings_290,
1487 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_290),
1488 .max_analog_gain = 100,
1491 [IMX290_MODEL_IMX290LLR] = {
1492 .colour_variant = IMX290_VARIANT_MONO,
1493 .init_regs = imx290_global_init_settings_290,
1494 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_290),
1495 .max_analog_gain = 100,
1498 [IMX290_MODEL_IMX327LQR] = {
1499 .colour_variant = IMX290_VARIANT_COLOUR,
1500 .init_regs = imx290_global_init_settings_327,
1501 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_327),
1502 .max_analog_gain = 98,
1505 [IMX290_MODEL_IMX462LQR] = {
1506 .colour_variant = IMX290_VARIANT_COLOUR,
1507 .init_regs = imx290_global_init_settings_462,
1508 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_462),
1509 .max_analog_gain = 98,
1512 [IMX290_MODEL_IMX462LLR] = {
1513 .colour_variant = IMX290_VARIANT_MONO,
1514 .init_regs = imx290_global_init_settings_462,
1515 .init_regs_num = ARRAY_SIZE(imx290_global_init_settings_462),
1516 .max_analog_gain = 98,
1521 static int imx290_parse_dt(struct imx290 *imx290)
1523 /* Only CSI2 is supported for now: */
1524 struct v4l2_fwnode_endpoint ep = {
1525 .bus_type = V4L2_MBUS_CSI2_DPHY
1527 struct fwnode_handle *endpoint;
1531 imx290->model = of_device_get_match_data(imx290->dev);
1533 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(imx290->dev), NULL);
1535 dev_err(imx290->dev, "Endpoint node not found\n");
1539 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
1540 fwnode_handle_put(endpoint);
1541 if (ret == -ENXIO) {
1542 dev_err(imx290->dev, "Unsupported bus type, should be CSI2\n");
1545 dev_err(imx290->dev, "Parsing endpoint node failed\n");
1549 /* Get number of data lanes */
1550 imx290->nlanes = ep.bus.mipi_csi2.num_data_lanes;
1551 if (imx290->nlanes != 2 && imx290->nlanes != 4) {
1552 dev_err(imx290->dev, "Invalid data lanes: %d\n", imx290->nlanes);
1557 dev_dbg(imx290->dev, "Using %u data lanes\n", imx290->nlanes);
1559 if (!ep.nr_of_link_frequencies) {
1560 dev_err(imx290->dev, "link-frequency property not found in DT\n");
1565 /* Check that link frequences for all the modes are in device tree */
1566 fq = imx290_check_link_freqs(imx290, &ep);
1568 dev_err(imx290->dev, "Link frequency of %lld is not supported\n",
1577 v4l2_fwnode_endpoint_free(&ep);
1581 static int imx290_probe(struct i2c_client *client)
1583 struct device *dev = &client->dev;
1584 struct imx290 *imx290;
1587 imx290 = devm_kzalloc(dev, sizeof(*imx290), GFP_KERNEL);
1592 imx290->regmap = devm_cci_regmap_init_i2c(client, 16);
1593 if (IS_ERR(imx290->regmap)) {
1594 dev_err(dev, "Unable to initialize I2C\n");
1598 ret = imx290_parse_dt(imx290);
1602 /* Acquire resources. */
1603 imx290->xclk = devm_clk_get(dev, "xclk");
1604 if (IS_ERR(imx290->xclk))
1605 return dev_err_probe(dev, PTR_ERR(imx290->xclk),
1606 "Could not get xclk\n");
1608 ret = imx290_get_regulators(dev, imx290);
1610 return dev_err_probe(dev, ret, "Cannot get regulators\n");
1612 imx290->rst_gpio = devm_gpiod_get_optional(dev, "reset",
1614 if (IS_ERR(imx290->rst_gpio))
1615 return dev_err_probe(dev, PTR_ERR(imx290->rst_gpio),
1616 "Cannot get reset gpio\n");
1618 /* Initialize external clock frequency. */
1619 ret = imx290_init_clk(imx290);
1624 * Enable power management. The driver supports runtime PM, but needs to
1625 * work when runtime PM is disabled in the kernel. To that end, power
1626 * the sensor on manually here.
1628 ret = imx290_power_on(imx290);
1630 dev_err(dev, "Could not power on the device\n");
1635 * Enable runtime PM with autosuspend. As the device has been powered
1636 * manually, mark it as active, and increase the usage count without
1637 * resuming the device.
1639 pm_runtime_set_active(dev);
1640 pm_runtime_get_noresume(dev);
1641 pm_runtime_enable(dev);
1642 pm_runtime_set_autosuspend_delay(dev, 1000);
1643 pm_runtime_use_autosuspend(dev);
1646 * Make sure the sensor is available, in STANDBY and not streaming
1647 * before the V4L2 subdev is initialized.
1649 ret = imx290_stop_streaming(imx290);
1651 ret = dev_err_probe(dev, ret, "Could not initialize device\n");
1655 /* Initialize the V4L2 subdev. */
1656 ret = imx290_subdev_init(imx290);
1660 v4l2_i2c_subdev_set_name(&imx290->sd, client,
1661 imx290->model->name, NULL);
1664 * Finally, register the V4L2 subdev. This must be done after
1665 * initializing everything as the subdev can be used immediately after
1668 ret = v4l2_async_register_subdev(&imx290->sd);
1670 dev_err(dev, "Could not register v4l2 device\n");
1677 imx290_subdev_cleanup(imx290);
1679 pm_runtime_disable(dev);
1680 pm_runtime_put_noidle(dev);
1681 imx290_power_off(imx290);
1685 static void imx290_remove(struct i2c_client *client)
1687 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1688 struct imx290 *imx290 = to_imx290(sd);
1690 v4l2_async_unregister_subdev(sd);
1691 imx290_subdev_cleanup(imx290);
1694 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1695 * make sure to turn power off manually.
1697 pm_runtime_disable(imx290->dev);
1698 if (!pm_runtime_status_suspended(imx290->dev))
1699 imx290_power_off(imx290);
1700 pm_runtime_set_suspended(imx290->dev);
1703 static const struct of_device_id imx290_of_match[] = {
1705 /* Deprecated - synonym for "sony,imx290lqr" */
1706 .compatible = "sony,imx290",
1707 .data = &imx290_models[IMX290_MODEL_IMX290LQR],
1709 .compatible = "sony,imx290lqr",
1710 .data = &imx290_models[IMX290_MODEL_IMX290LQR],
1712 .compatible = "sony,imx290llr",
1713 .data = &imx290_models[IMX290_MODEL_IMX290LLR],
1715 .compatible = "sony,imx327lqr",
1716 .data = &imx290_models[IMX290_MODEL_IMX327LQR],
1718 .compatible = "sony,imx462lqr",
1719 .data = &imx290_models[IMX290_MODEL_IMX462LQR],
1721 .compatible = "sony,imx462llr",
1722 .data = &imx290_models[IMX290_MODEL_IMX462LLR],
1726 MODULE_DEVICE_TABLE(of, imx290_of_match);
1728 static struct i2c_driver imx290_i2c_driver = {
1729 .probe = imx290_probe,
1730 .remove = imx290_remove,
1733 .pm = pm_ptr(&imx290_pm_ops),
1734 .of_match_table = imx290_of_match,
1738 module_i2c_driver(imx290_i2c_driver);
1740 MODULE_DESCRIPTION("Sony IMX290 CMOS Image Sensor Driver");
1741 MODULE_AUTHOR("FRAMOS GmbH");
1743 MODULE_LICENSE("GPL v2");