1 // SPDX-License-Identifier: GPL-2.0
3 * V4L2 Support for the IMX283
5 * Diagonal 15.86 mm (Type 1) CMOS Image Sensor with Square Pixel for Color
8 * Copyright (C) 2024 Ideas on Board Oy.
10 * Based on Sony IMX283 driver prepared by Will Whang
12 * Based on Sony imx477 camera driver
13 * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
16 #include <linux/array_size.h>
17 #include <linux/bitops.h>
18 #include <linux/container_of.h>
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/i2c.h>
24 #include <linux/minmax.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/property.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/types.h>
31 #include <linux/units.h>
32 #include <media/v4l2-cci.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-fwnode.h>
37 #include <media/v4l2-mediabus.h>
40 #define IMX283_REG_CHIP_ID CCI_REG8(0x3000)
41 #define IMX283_CHIP_ID 0x0b // Default power on state
43 #define IMX283_REG_STANDBY CCI_REG8(0x3000)
44 #define IMX283_ACTIVE 0
45 #define IMX283_STANDBY BIT(0)
46 #define IMX283_STBLOGIC BIT(1)
47 #define IMX283_STBMIPI BIT(2)
48 #define IMX283_STBDV BIT(3)
49 #define IMX283_SLEEP BIT(4)
51 #define IMX283_REG_CLAMP CCI_REG8(0x3001)
52 #define IMX283_CLPSQRST BIT(4)
54 #define IMX283_REG_PLSTMG08 CCI_REG8(0x3003)
55 #define IMX283_PLSTMG08_VAL 0x77
57 #define IMX283_REG_MDSEL1 CCI_REG8(0x3004)
58 #define IMX283_REG_MDSEL2 CCI_REG8(0x3005)
59 #define IMX283_REG_MDSEL3 CCI_REG8(0x3006)
60 #define IMX283_MDSEL3_VCROP_EN BIT(5)
61 #define IMX283_REG_MDSEL4 CCI_REG8(0x3007)
62 #define IMX283_MDSEL4_VCROP_EN (BIT(4) | BIT(6))
64 #define IMX283_REG_SVR CCI_REG16_LE(0x3009)
66 #define IMX283_REG_HTRIMMING CCI_REG8(0x300b)
67 #define IMX283_MDVREV BIT(0) /* VFLIP */
68 #define IMX283_HTRIMMING_EN BIT(4)
70 #define IMX283_REG_VWINPOS CCI_REG16_LE(0x300f)
71 #define IMX283_REG_VWIDCUT CCI_REG16_LE(0x3011)
73 #define IMX283_REG_MDSEL7 CCI_REG16_LE(0x3013)
75 /* CSI Clock Configuration */
76 #define IMX283_REG_TCLKPOST CCI_REG8(0x3018)
77 #define IMX283_REG_THSPREPARE CCI_REG8(0x301a)
78 #define IMX283_REG_THSZERO CCI_REG8(0x301c)
79 #define IMX283_REG_THSTRAIL CCI_REG8(0x301e)
80 #define IMX283_REG_TCLKTRAIL CCI_REG8(0x3020)
81 #define IMX283_REG_TCLKPREPARE CCI_REG8(0x3022)
82 #define IMX283_REG_TCLKZERO CCI_REG16_LE(0x3024)
83 #define IMX283_REG_TLPX CCI_REG8(0x3026)
84 #define IMX283_REG_THSEXIT CCI_REG8(0x3028)
85 #define IMX283_REG_TCLKPRE CCI_REG8(0x302a)
86 #define IMX283_REG_SYSMODE CCI_REG8(0x3104)
88 #define IMX283_REG_Y_OUT_SIZE CCI_REG16_LE(0x302f)
89 #define IMX283_REG_WRITE_VSIZE CCI_REG16_LE(0x3031)
90 #define IMX283_REG_OB_SIZE_V CCI_REG8(0x3033)
92 /* HMAX internal HBLANK */
93 #define IMX283_REG_HMAX CCI_REG16_LE(0x3036)
94 #define IMX283_HMAX_MAX (BIT(16) - 1)
96 /* VMAX internal VBLANK */
97 #define IMX283_REG_VMAX CCI_REG24_LE(0x3038)
98 #define IMX283_VMAX_MAX (BIT(16) - 1)
101 #define IMX283_REG_SHR CCI_REG16_LE(0x303b)
102 #define IMX283_SHR_MIN 11
105 * Analog gain control
106 * Gain [dB] = -20log{(2048 - value [10:0]) /2048}
107 * Range: 0dB to approximately +27dB
109 #define IMX283_REG_ANALOG_GAIN CCI_REG16_LE(0x3042)
110 #define IMX283_ANA_GAIN_MIN 0
111 #define IMX283_ANA_GAIN_MAX 1957
112 #define IMX283_ANA_GAIN_STEP 1
113 #define IMX283_ANA_GAIN_DEFAULT 0x0
116 * Digital gain control
117 * Gain [dB] = value * 6
118 * Range: 0dB to +18db
120 #define IMX283_REG_DIGITAL_GAIN CCI_REG8(0x3044)
121 #define IMX283_DGTL_GAIN_MIN 0
122 #define IMX283_DGTL_GAIN_MAX 3
123 #define IMX283_DGTL_GAIN_DEFAULT 0
124 #define IMX283_DGTL_GAIN_STEP 1
126 #define IMX283_REG_HTRIMMING_START CCI_REG16_LE(0x3058)
127 #define IMX283_REG_HTRIMMING_END CCI_REG16_LE(0x305a)
129 #define IMX283_REG_MDSEL18 CCI_REG16_LE(0x30f6)
131 /* Master Mode Operation Control */
132 #define IMX283_REG_XMSTA CCI_REG8(0x3105)
133 #define IMX283_XMSTA BIT(0)
135 #define IMX283_REG_SYNCDRV CCI_REG8(0x3107)
136 #define IMX283_SYNCDRV_XHS_XVS (0xa0 | 0x02)
137 #define IMX283_SYNCDRV_HIZ (0xa0 | 0x03)
140 #define IMX283_REG_STBPL CCI_REG8(0x320b)
141 #define IMX283_STBPL_NORMAL 0x00
142 #define IMX283_STBPL_STANDBY 0x03
144 /* Input Frequency Setting */
145 #define IMX283_REG_PLRD1 CCI_REG8(0x36c1)
146 #define IMX283_REG_PLRD2 CCI_REG16_LE(0x36c2)
147 #define IMX283_REG_PLRD3 CCI_REG8(0x36f7)
148 #define IMX283_REG_PLRD4 CCI_REG8(0x36f8)
150 #define IMX283_REG_PLSTMG02 CCI_REG8(0x36aa)
151 #define IMX283_PLSTMG02_VAL 0x00
153 #define IMX283_REG_EBD_X_OUT_SIZE CCI_REG16_LE(0x3a54)
155 /* Test pattern generator */
156 #define IMX283_REG_TPG_CTRL CCI_REG8(0x3156)
157 #define IMX283_TPG_CTRL_CLKEN BIT(0)
158 #define IMX283_TPG_CTRL_PATEN BIT(4)
160 #define IMX283_REG_TPG_PAT CCI_REG8(0x3157)
161 #define IMX283_TPG_PAT_ALL_000 0x00
162 #define IMX283_TPG_PAT_ALL_FFF 0x01
163 #define IMX283_TPG_PAT_ALL_555 0x02
164 #define IMX283_TPG_PAT_ALL_AAA 0x03
165 #define IMX283_TPG_PAT_H_COLOR_BARS 0x0a
166 #define IMX283_TPG_PAT_V_COLOR_BARS 0x0b
168 /* Exposure control */
169 #define IMX283_EXPOSURE_MIN 52
170 #define IMX283_EXPOSURE_STEP 1
171 #define IMX283_EXPOSURE_DEFAULT 1000
172 #define IMX283_EXPOSURE_MAX 49865
176 #define IMX283_XCLR_MIN_DELAY_US (1 * USEC_PER_MSEC)
177 #define IMX283_XCLR_DELAY_RANGE_US (1 * USEC_PER_MSEC)
179 /* IMX283 native and active pixel array size. */
180 static const struct v4l2_rect imx283_native_area = {
187 static const struct v4l2_rect imx283_active_area = {
194 struct imx283_reg_list {
195 unsigned int num_of_regs;
196 const struct cci_reg_sequence *regs;
199 /* Mode : resolution and related config values */
213 * Minimum horizontal timing in pixel-units
215 * Note that HMAX is written in 72MHz units, and the datasheet assumes a
216 * 720MHz link frequency. Convert datasheet values with the following:
218 * For 12 bpp modes (480Mbps) convert with:
219 * hmax = [hmax in 72MHz units] * 480 / 72
221 * For 10 bpp modes (576Mbps) convert with:
222 * hmax = [hmax in 72MHz units] * 576 / 72
226 /* minimum V-timing in lines */
229 /* default H-timing */
232 /* default V-timing */
239 * Per-mode vertical crop constants used to calculate values
240 * of IMX283REG_WIDCUT and IMX283_REG_VWINPOS.
246 /* Horizontal and vertical binning ratio */
250 /* Optical Blanking */
254 /* Analog crop rectangle. */
255 struct v4l2_rect crop;
258 struct imx283_input_frequency {
260 unsigned int reg_count;
261 struct cci_reg_sequence regs[4];
264 static const struct imx283_input_frequency imx283_frequencies[] = {
266 .mhz = 6 * HZ_PER_MHZ,
269 { IMX283_REG_PLRD1, 0x00 },
270 { IMX283_REG_PLRD2, 0x00f0 },
271 { IMX283_REG_PLRD3, 0x00 },
272 { IMX283_REG_PLRD4, 0xc0 },
276 .mhz = 12 * HZ_PER_MHZ,
279 { IMX283_REG_PLRD1, 0x01 },
280 { IMX283_REG_PLRD2, 0x00f0 },
281 { IMX283_REG_PLRD3, 0x01 },
282 { IMX283_REG_PLRD4, 0xc0 },
286 .mhz = 18 * HZ_PER_MHZ,
289 { IMX283_REG_PLRD1, 0x01 },
290 { IMX283_REG_PLRD2, 0x00a0 },
291 { IMX283_REG_PLRD3, 0x01 },
292 { IMX283_REG_PLRD4, 0x80 },
296 .mhz = 24 * HZ_PER_MHZ,
299 { IMX283_REG_PLRD1, 0x02 },
300 { IMX283_REG_PLRD2, 0x00f0 },
301 { IMX283_REG_PLRD3, 0x02 },
302 { IMX283_REG_PLRD4, 0xc0 },
320 struct imx283_readout_mode {
327 static const struct imx283_readout_mode imx283_readout_modes[] = {
328 /* All pixel scan modes */
329 [IMX283_MODE_0] = { 0x04, 0x03, 0x10, 0x00 }, /* 12 bit */
330 [IMX283_MODE_1] = { 0x04, 0x01, 0x00, 0x00 }, /* 10 bit */
331 [IMX283_MODE_1A] = { 0x04, 0x01, 0x20, 0x50 }, /* 10 bit */
332 [IMX283_MODE_1S] = { 0x04, 0x41, 0x20, 0x50 }, /* 10 bit */
334 /* Horizontal / Vertical 2/2-line binning */
335 [IMX283_MODE_2] = { 0x0d, 0x11, 0x50, 0x00 }, /* 12 bit */
336 [IMX283_MODE_2A] = { 0x0d, 0x11, 0x70, 0x50 }, /* 12 bit */
338 /* Horizontal / Vertical 3/3-line binning */
339 [IMX283_MODE_3] = { 0x1e, 0x18, 0x10, 0x00 }, /* 12 bit */
341 /* Vertical 2/9 subsampling, horizontal 3 binning cropping */
342 [IMX283_MODE_4] = { 0x29, 0x18, 0x30, 0x50 }, /* 12 bit */
344 /* Vertical 2/19 subsampling binning, horizontal 3 binning */
345 [IMX283_MODE_5] = { 0x2d, 0x18, 0x10, 0x00 }, /* 12 bit */
347 /* Vertical 2 binning horizontal 2/4, subsampling 16:9 cropping */
348 [IMX283_MODE_6] = { 0x18, 0x21, 0x00, 0x09 }, /* 10 bit */
351 * New modes should make sure the offset period is complied.
352 * See imx283_exposure() for reference.
356 static const struct cci_reg_sequence mipi_data_rate_1440Mbps[] = {
357 /* The default register settings provide the 1440Mbps rate */
358 { CCI_REG8(0x36c5), 0x00 }, /* Undocumented */
359 { CCI_REG8(0x3ac4), 0x00 }, /* Undocumented */
361 { IMX283_REG_STBPL, 0x00 },
362 { IMX283_REG_TCLKPOST, 0xa7 },
363 { IMX283_REG_THSPREPARE, 0x6f },
364 { IMX283_REG_THSZERO, 0x9f },
365 { IMX283_REG_THSTRAIL, 0x5f },
366 { IMX283_REG_TCLKTRAIL, 0x5f },
367 { IMX283_REG_TCLKPREPARE, 0x6f },
368 { IMX283_REG_TCLKZERO, 0x017f },
369 { IMX283_REG_TLPX, 0x4f },
370 { IMX283_REG_THSEXIT, 0x47 },
371 { IMX283_REG_TCLKPRE, 0x07 },
372 { IMX283_REG_SYSMODE, 0x02 },
375 static const struct cci_reg_sequence mipi_data_rate_720Mbps[] = {
376 /* Undocumented Additions "For 720MBps" Setting */
377 { CCI_REG8(0x36c5), 0x01 }, /* Undocumented */
378 { CCI_REG8(0x3ac4), 0x01 }, /* Undocumented */
380 { IMX283_REG_STBPL, 0x00 },
381 { IMX283_REG_TCLKPOST, 0x77 },
382 { IMX283_REG_THSPREPARE, 0x37 },
383 { IMX283_REG_THSZERO, 0x67 },
384 { IMX283_REG_THSTRAIL, 0x37 },
385 { IMX283_REG_TCLKTRAIL, 0x37 },
386 { IMX283_REG_TCLKPREPARE, 0x37 },
387 { IMX283_REG_TCLKZERO, 0xdf },
388 { IMX283_REG_TLPX, 0x2f },
389 { IMX283_REG_THSEXIT, 0x47 },
390 { IMX283_REG_TCLKPRE, 0x0f },
391 { IMX283_REG_SYSMODE, 0x02 },
394 static const s64 link_frequencies[] = {
395 720 * HZ_PER_MHZ, /* 1440 Mbps lane data rate */
396 360 * HZ_PER_MHZ, /* 720 Mbps data lane rate */
399 static const struct imx283_reg_list link_freq_reglist[] = {
401 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1440Mbps),
402 .regs = mipi_data_rate_1440Mbps,
405 .num_of_regs = ARRAY_SIZE(mipi_data_rate_720Mbps),
406 .regs = mipi_data_rate_720Mbps,
411 static const struct imx283_mode supported_modes_12bit[] = {
413 /* 20MPix 21.40 fps readout mode 0 */
414 .mode = IMX283_MODE_0,
418 .min_hmax = 5914, /* 887 @ 480MHz/72MHz */
419 .min_vmax = 3793, /* Lines */
429 .default_hmax = 6000, /* 900 @ 480MHz/72MHz */
430 .default_vmax = 4000,
444 * Readout mode 2 : 2/2 binned mode (2736x1824)
446 .mode = IMX283_MODE_2,
450 .min_hmax = 2414, /* Pixels (362 * 480MHz/72MHz + padding) */
451 .min_vmax = 3840, /* Lines */
454 .default_hmax = 2500, /* 375 @ 480MHz/72Mhz */
455 .default_vmax = 3840,
477 static const struct imx283_mode supported_modes_10bit[] = {
479 /* 20MPix 25.48 fps readout mode 1 */
480 .mode = IMX283_MODE_1,
484 .min_hmax = 5960, /* 745 @ 576MHz / 72MHz */
488 .default_hmax = 6000, /* 750 @ 576MHz / 72MHz */
489 .default_vmax = 3840,
503 static const u32 imx283_mbus_codes[] = {
504 MEDIA_BUS_FMT_SRGGB12_1X12,
505 MEDIA_BUS_FMT_SRGGB10_1X10,
508 /* regulator supplies */
509 static const char *const imx283_supply_name[] = {
510 "vadd", /* Analog (2.9V) supply */
511 "vdd1", /* Supply Voltage 2 (1.8V) supply */
512 "vdd2", /* Supply Voltage 3 (1.2V) supply */
519 const struct imx283_input_frequency *freq;
521 struct v4l2_subdev sd;
522 struct media_pad pad;
526 struct gpio_desc *reset_gpio;
527 struct regulator_bulk_data supplies[ARRAY_SIZE(imx283_supply_name)];
530 struct v4l2_ctrl_handler ctrl_handler;
531 struct v4l2_ctrl *exposure;
532 struct v4l2_ctrl *vblank;
533 struct v4l2_ctrl *hblank;
534 struct v4l2_ctrl *vflip;
536 unsigned long link_freq_bitmap;
542 static inline struct imx283 *to_imx283(struct v4l2_subdev *sd)
544 return container_of_const(sd, struct imx283, sd);
547 static inline void get_mode_table(unsigned int code,
548 const struct imx283_mode **mode_list,
549 unsigned int *num_modes)
552 case MEDIA_BUS_FMT_SRGGB12_1X12:
553 case MEDIA_BUS_FMT_SGRBG12_1X12:
554 case MEDIA_BUS_FMT_SGBRG12_1X12:
555 case MEDIA_BUS_FMT_SBGGR12_1X12:
556 *mode_list = supported_modes_12bit;
557 *num_modes = ARRAY_SIZE(supported_modes_12bit);
560 case MEDIA_BUS_FMT_SRGGB10_1X10:
561 case MEDIA_BUS_FMT_SGRBG10_1X10:
562 case MEDIA_BUS_FMT_SGBRG10_1X10:
563 case MEDIA_BUS_FMT_SBGGR10_1X10:
564 *mode_list = supported_modes_10bit;
565 *num_modes = ARRAY_SIZE(supported_modes_10bit);
574 /* Calculate the Pixel Rate based on the current mode */
575 static u64 imx283_pixel_rate(struct imx283 *imx283,
576 const struct imx283_mode *mode)
578 u64 link_frequency = link_frequencies[__ffs(imx283->link_freq_bitmap)];
579 unsigned int bpp = mode->bpp;
580 const unsigned int ddr = 2; /* Double Data Rate */
581 const unsigned int lanes = 4; /* Only 4 lane support */
582 u64 numerator = link_frequency * ddr * lanes;
584 do_div(numerator, bpp);
589 /* Convert from a variable pixel_rate to 72 MHz clock cycles */
590 static u64 imx283_internal_clock(unsigned int pixel_rate, unsigned int pixels)
593 * Determine the following operation without overflow:
594 * pixels = 72 Mhz / pixel_rate
596 * The internal clock at 72MHz and Pixel Rate (between 240 and 576MHz)
597 * can easily overflow this calculation, so pre-divide to simplify.
599 const u32 iclk_pre = 72;
600 const u32 pclk_pre = pixel_rate / HZ_PER_MHZ;
601 u64 numerator = pixels * iclk_pre;
603 do_div(numerator, pclk_pre);
608 /* Internal clock (72MHz) to Pixel Rate clock (Variable) */
609 static u64 imx283_iclk_to_pix(unsigned int pixel_rate, unsigned int cycles)
612 * Determine the following operation without overflow:
613 * cycles * pixel_rate / 72 MHz
615 * The internal clock at 72MHz and Pixel Rate (between 240 and 576MHz)
616 * can easily overflow this calculation, so pre-divide to simplify.
618 const u32 iclk_pre = 72;
619 const u32 pclk_pre = pixel_rate / HZ_PER_MHZ;
620 u64 numerator = cycles * pclk_pre;
622 do_div(numerator, iclk_pre);
627 /* Determine the exposure based on current hmax, vmax and a given SHR */
628 static u32 imx283_exposure(struct imx283 *imx283,
629 const struct imx283_mode *mode, u64 shr)
631 u32 svr = 0; /* SVR feature is not currently supported */
635 /* Number of clocks per internal offset period */
636 offset = mode->mode == IMX283_MODE_0 ? 209 : 157;
637 numerator = (imx283->vmax * (svr + 1) - shr) * imx283->hmax + offset;
639 do_div(numerator, imx283->hmax);
641 return clamp(numerator, 0, U32_MAX);
644 static void imx283_exposure_limits(struct imx283 *imx283,
645 const struct imx283_mode *mode,
646 s64 *min_exposure, s64 *max_exposure)
648 u32 svr = 0; /* SVR feature is not currently supported */
649 u64 min_shr = mode->min_shr;
650 /* Global Shutter is not supported */
651 u64 max_shr = (svr + 1) * imx283->vmax - 4;
653 max_shr = min(max_shr, BIT(16) - 1);
655 *min_exposure = imx283_exposure(imx283, mode, max_shr);
656 *max_exposure = imx283_exposure(imx283, mode, min_shr);
660 * Integration Time [s] = [ {VMAX x (SVR + 1) – (SHR)} x HMAX + offset ]
663 static u32 imx283_shr(struct imx283 *imx283, const struct imx283_mode *mode,
666 u32 svr = 0; /* SVR feature is not currently supported */
670 /* Number of clocks per internal offset period */
671 offset = mode->mode == IMX283_MODE_0 ? 209 : 157;
672 temp = ((u64)exposure * imx283->hmax - offset);
673 do_div(temp, imx283->hmax);
675 return (imx283->vmax * (svr + 1) - temp);
678 static const char * const imx283_tpg_menu[] = {
684 "Horizontal color bars",
685 "Vertical color bars",
688 static const int imx283_tpg_val[] = {
689 IMX283_TPG_PAT_ALL_000,
690 IMX283_TPG_PAT_ALL_000,
691 IMX283_TPG_PAT_ALL_FFF,
692 IMX283_TPG_PAT_ALL_555,
693 IMX283_TPG_PAT_ALL_AAA,
694 IMX283_TPG_PAT_H_COLOR_BARS,
695 IMX283_TPG_PAT_V_COLOR_BARS,
698 static int imx283_update_test_pattern(struct imx283 *imx283, u32 pattern_index)
702 if (pattern_index >= ARRAY_SIZE(imx283_tpg_val))
706 return cci_write(imx283->cci, IMX283_REG_TPG_CTRL, 0x00, NULL);
708 ret = cci_write(imx283->cci, IMX283_REG_TPG_PAT,
709 imx283_tpg_val[pattern_index], NULL);
713 return cci_write(imx283->cci, IMX283_REG_TPG_CTRL,
714 IMX283_TPG_CTRL_CLKEN | IMX283_TPG_CTRL_PATEN, NULL);
717 static int imx283_set_ctrl(struct v4l2_ctrl *ctrl)
719 struct imx283 *imx283 = container_of(ctrl->handler, struct imx283,
721 const struct imx283_mode *mode;
722 struct v4l2_mbus_framefmt *fmt;
723 const struct imx283_mode *mode_list;
724 struct v4l2_subdev_state *state;
725 unsigned int num_modes;
729 state = v4l2_subdev_get_locked_active_state(&imx283->sd);
730 fmt = v4l2_subdev_state_get_format(state, 0);
732 get_mode_table(fmt->code, &mode_list, &num_modes);
733 mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
734 fmt->width, fmt->height);
737 * The VBLANK control may change the limits of usable exposure, so check
738 * and adjust if necessary.
740 if (ctrl->id == V4L2_CID_VBLANK) {
741 /* Honour the VBLANK limits when setting exposure. */
742 s64 current_exposure, max_exposure, min_exposure;
744 imx283->vmax = mode->height + ctrl->val;
746 imx283_exposure_limits(imx283, mode,
747 &min_exposure, &max_exposure);
749 current_exposure = imx283->exposure->val;
750 current_exposure = clamp(current_exposure, min_exposure,
753 __v4l2_ctrl_modify_range(imx283->exposure, min_exposure,
754 max_exposure, 1, current_exposure);
758 * Applying V4L2 control value only happens
759 * when power is up for streaming
761 if (!pm_runtime_get_if_active(imx283->dev))
765 case V4L2_CID_EXPOSURE:
766 shr = imx283_shr(imx283, mode, ctrl->val);
767 dev_dbg(imx283->dev, "V4L2_CID_EXPOSURE : %d - SHR: %lld\n",
769 ret = cci_write(imx283->cci, IMX283_REG_SHR, shr, NULL);
772 case V4L2_CID_HBLANK:
773 pixel_rate = imx283_pixel_rate(imx283, mode);
774 imx283->hmax = imx283_internal_clock(pixel_rate, mode->width + ctrl->val);
775 dev_dbg(imx283->dev, "V4L2_CID_HBLANK : %d HMAX : %u\n",
776 ctrl->val, imx283->hmax);
777 ret = cci_write(imx283->cci, IMX283_REG_HMAX, imx283->hmax, NULL);
780 case V4L2_CID_VBLANK:
781 imx283->vmax = mode->height + ctrl->val;
782 dev_dbg(imx283->dev, "V4L2_CID_VBLANK : %d VMAX : %u\n",
783 ctrl->val, imx283->vmax);
784 ret = cci_write(imx283->cci, IMX283_REG_VMAX, imx283->vmax, NULL);
787 case V4L2_CID_ANALOGUE_GAIN:
788 ret = cci_write(imx283->cci, IMX283_REG_ANALOG_GAIN, ctrl->val, NULL);
791 case V4L2_CID_DIGITAL_GAIN:
792 ret = cci_write(imx283->cci, IMX283_REG_DIGITAL_GAIN, ctrl->val, NULL);
797 * VFLIP is managed by BIT(0) of IMX283_REG_HTRIMMING address, hence
798 * both need to be set simultaneously.
801 cci_write(imx283->cci, IMX283_REG_HTRIMMING,
802 IMX283_HTRIMMING_EN | IMX283_MDVREV, &ret);
804 cci_write(imx283->cci, IMX283_REG_HTRIMMING,
805 IMX283_HTRIMMING_EN, &ret);
809 case V4L2_CID_TEST_PATTERN:
810 ret = imx283_update_test_pattern(imx283, ctrl->val);
814 dev_err(imx283->dev, "ctrl(id:0x%x, val:0x%x) is not handled\n",
815 ctrl->id, ctrl->val);
819 pm_runtime_put(imx283->dev);
824 static const struct v4l2_ctrl_ops imx283_ctrl_ops = {
825 .s_ctrl = imx283_set_ctrl,
828 static int imx283_enum_mbus_code(struct v4l2_subdev *sd,
829 struct v4l2_subdev_state *sd_state,
830 struct v4l2_subdev_mbus_code_enum *code)
832 if (code->index >= ARRAY_SIZE(imx283_mbus_codes))
835 code->code = imx283_mbus_codes[code->index];
840 static int imx283_enum_frame_size(struct v4l2_subdev *sd,
841 struct v4l2_subdev_state *sd_state,
842 struct v4l2_subdev_frame_size_enum *fse)
844 const struct imx283_mode *mode_list;
845 unsigned int num_modes;
847 get_mode_table(fse->code, &mode_list, &num_modes);
849 if (fse->index >= num_modes)
852 fse->min_width = mode_list[fse->index].width;
853 fse->max_width = fse->min_width;
854 fse->min_height = mode_list[fse->index].height;
855 fse->max_height = fse->min_height;
860 static void imx283_update_image_pad_format(struct imx283 *imx283,
861 const struct imx283_mode *mode,
862 struct v4l2_mbus_framefmt *format)
864 format->width = mode->width;
865 format->height = mode->height;
866 format->field = V4L2_FIELD_NONE;
867 format->colorspace = V4L2_COLORSPACE_RAW;
868 format->ycbcr_enc = V4L2_YCBCR_ENC_601;
869 format->quantization = V4L2_QUANTIZATION_FULL_RANGE;
870 format->xfer_func = V4L2_XFER_FUNC_NONE;
873 static int imx283_init_state(struct v4l2_subdev *sd,
874 struct v4l2_subdev_state *state)
876 struct imx283 *imx283 = to_imx283(sd);
877 struct v4l2_mbus_framefmt *format;
878 const struct imx283_mode *mode;
879 struct v4l2_rect *crop;
881 /* Initialize try_fmt */
882 format = v4l2_subdev_state_get_format(state, IMAGE_PAD);
884 mode = &supported_modes_12bit[0];
885 format->code = MEDIA_BUS_FMT_SRGGB12_1X12;
886 imx283_update_image_pad_format(imx283, mode, format);
888 /* Initialize crop rectangle to mode default */
889 crop = v4l2_subdev_state_get_crop(state, IMAGE_PAD);
895 static void imx283_set_framing_limits(struct imx283 *imx283,
896 const struct imx283_mode *mode)
898 u64 pixel_rate = imx283_pixel_rate(imx283, mode);
899 u64 min_hblank, max_hblank, def_hblank;
901 /* Initialise hmax and vmax for exposure calculations */
902 imx283->hmax = imx283_internal_clock(pixel_rate, mode->default_hmax);
903 imx283->vmax = mode->default_vmax;
906 * Horizontal Blanking
907 * Convert the HMAX_MAX (72MHz) to Pixel rate values for HBLANK_MAX
909 min_hblank = mode->min_hmax - mode->width;
910 max_hblank = imx283_iclk_to_pix(pixel_rate, IMX283_HMAX_MAX) - mode->width;
911 def_hblank = mode->default_hmax - mode->width;
912 __v4l2_ctrl_modify_range(imx283->hblank, min_hblank, max_hblank, 1,
914 __v4l2_ctrl_s_ctrl(imx283->hblank, def_hblank);
916 /* Vertical Blanking */
917 __v4l2_ctrl_modify_range(imx283->vblank, mode->min_vmax - mode->height,
918 IMX283_VMAX_MAX - mode->height, 1,
919 mode->default_vmax - mode->height);
920 __v4l2_ctrl_s_ctrl(imx283->vblank, mode->default_vmax - mode->height);
923 static int imx283_set_pad_format(struct v4l2_subdev *sd,
924 struct v4l2_subdev_state *sd_state,
925 struct v4l2_subdev_format *fmt)
927 struct v4l2_mbus_framefmt *format;
928 const struct imx283_mode *mode;
929 struct imx283 *imx283 = to_imx283(sd);
930 const struct imx283_mode *mode_list;
931 unsigned int num_modes;
933 get_mode_table(fmt->format.code, &mode_list, &num_modes);
935 mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
936 fmt->format.width, fmt->format.height);
938 fmt->format.width = mode->width;
939 fmt->format.height = mode->height;
940 fmt->format.field = V4L2_FIELD_NONE;
941 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
942 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_601;
943 fmt->format.quantization = V4L2_QUANTIZATION_FULL_RANGE;
944 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
946 format = v4l2_subdev_state_get_format(sd_state, 0);
948 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
949 imx283_set_framing_limits(imx283, mode);
951 *format = fmt->format;
956 static int imx283_standby_cancel(struct imx283 *imx283)
958 unsigned int link_freq_idx;
961 cci_write(imx283->cci, IMX283_REG_STANDBY,
962 IMX283_STBLOGIC | IMX283_STBDV, &ret);
964 /* Configure PLL clocks based on the xclk */
965 cci_multi_reg_write(imx283->cci, imx283->freq->regs,
966 imx283->freq->reg_count, &ret);
968 dev_dbg(imx283->dev, "Using clk freq %ld MHz",
969 imx283->freq->mhz / HZ_PER_MHZ);
971 /* Initialise communication */
972 cci_write(imx283->cci, IMX283_REG_PLSTMG08, IMX283_PLSTMG08_VAL, &ret);
973 cci_write(imx283->cci, IMX283_REG_PLSTMG02, IMX283_PLSTMG02_VAL, &ret);
976 cci_write(imx283->cci, IMX283_REG_STBPL, IMX283_STBPL_NORMAL, &ret);
978 /* Configure the MIPI link speed */
979 link_freq_idx = __ffs(imx283->link_freq_bitmap);
980 cci_multi_reg_write(imx283->cci, link_freq_reglist[link_freq_idx].regs,
981 link_freq_reglist[link_freq_idx].num_of_regs,
984 /* 1st Stabilisation period of 1 ms or more */
985 usleep_range(1000, 2000);
988 cci_write(imx283->cci, IMX283_REG_STANDBY, IMX283_ACTIVE, &ret);
990 /* 2nd Stabilisation period of 19ms or more */
991 usleep_range(19000, 20000);
993 cci_write(imx283->cci, IMX283_REG_CLAMP, IMX283_CLPSQRST, &ret);
994 cci_write(imx283->cci, IMX283_REG_XMSTA, 0, &ret);
995 cci_write(imx283->cci, IMX283_REG_SYNCDRV, IMX283_SYNCDRV_XHS_XVS, &ret);
1000 /* Start streaming */
1001 static int imx283_start_streaming(struct imx283 *imx283,
1002 struct v4l2_subdev_state *state)
1004 const struct imx283_readout_mode *readout;
1005 const struct imx283_mode *mode;
1006 const struct v4l2_mbus_framefmt *fmt;
1007 const struct imx283_mode *mode_list;
1008 unsigned int num_modes;
1015 fmt = v4l2_subdev_state_get_format(state, 0);
1016 get_mode_table(fmt->code, &mode_list, &num_modes);
1017 mode = v4l2_find_nearest_size(mode_list, num_modes, width, height,
1018 fmt->width, fmt->height);
1020 ret = imx283_standby_cancel(imx283);
1022 dev_err(imx283->dev, "failed to cancel standby\n");
1027 * Set the readout mode registers.
1028 * MDSEL3 and MDSEL4 are updated to enable Arbitrary Vertical Cropping.
1030 readout = &imx283_readout_modes[mode->mode];
1031 cci_write(imx283->cci, IMX283_REG_MDSEL1, readout->mdsel1, &ret);
1032 cci_write(imx283->cci, IMX283_REG_MDSEL2, readout->mdsel2, &ret);
1033 cci_write(imx283->cci, IMX283_REG_MDSEL3,
1034 readout->mdsel3 | IMX283_MDSEL3_VCROP_EN, &ret);
1035 cci_write(imx283->cci, IMX283_REG_MDSEL4,
1036 readout->mdsel4 | IMX283_MDSEL4_VCROP_EN, &ret);
1038 /* Mode 1S specific entries from the Readout Drive Mode Tables */
1039 if (mode->mode == IMX283_MODE_1S) {
1040 cci_write(imx283->cci, IMX283_REG_MDSEL7, 0x01, &ret);
1041 cci_write(imx283->cci, IMX283_REG_MDSEL18, 0x1098, &ret);
1045 dev_err(imx283->dev, "failed to set readout\n");
1049 /* Initialise SVR. Unsupported for now - Always 0 */
1050 cci_write(imx283->cci, IMX283_REG_SVR, 0x00, &ret);
1052 dev_dbg(imx283->dev, "Mode: Size %d x %d\n", mode->width, mode->height);
1053 dev_dbg(imx283->dev, "Analogue Crop (in the mode) %d,%d %dx%d\n",
1059 y_out_size = mode->crop.height / mode->vbin_ratio;
1060 write_v_size = y_out_size + mode->vertical_ob;
1062 * cropping start position = (VWINPOS – Vst) × 2
1063 * cropping width = Veff – (VWIDCUT – Vct) × 2
1065 v_pos = imx283->vflip->val ?
1066 ((-mode->crop.top / mode->vbin_ratio) / 2) + mode->vst :
1067 ((mode->crop.top / mode->vbin_ratio) / 2) + mode->vst;
1068 v_widcut = ((mode->veff - y_out_size) / 2) + mode->vct;
1070 cci_write(imx283->cci, IMX283_REG_Y_OUT_SIZE, y_out_size, &ret);
1071 cci_write(imx283->cci, IMX283_REG_WRITE_VSIZE, write_v_size, &ret);
1072 cci_write(imx283->cci, IMX283_REG_VWIDCUT, v_widcut, &ret);
1073 cci_write(imx283->cci, IMX283_REG_VWINPOS, v_pos, &ret);
1075 cci_write(imx283->cci, IMX283_REG_OB_SIZE_V, mode->vertical_ob, &ret);
1077 /* TODO: Validate mode->crop is fully contained within imx283_native_area */
1078 cci_write(imx283->cci, IMX283_REG_HTRIMMING_START, mode->crop.left, &ret);
1079 cci_write(imx283->cci, IMX283_REG_HTRIMMING_END,
1080 mode->crop.left + mode->crop.width, &ret);
1082 /* Disable embedded data */
1083 cci_write(imx283->cci, IMX283_REG_EBD_X_OUT_SIZE, 0, &ret);
1085 /* Apply customized values from controls (HMAX/VMAX/SHR) */
1086 ret = __v4l2_ctrl_handler_setup(imx283->sd.ctrl_handler);
1091 static int imx283_enable_streams(struct v4l2_subdev *sd,
1092 struct v4l2_subdev_state *state, u32 pad,
1095 struct imx283 *imx283 = to_imx283(sd);
1098 if (pad != IMAGE_PAD)
1101 ret = pm_runtime_get_sync(imx283->dev);
1103 pm_runtime_put_noidle(imx283->dev);
1107 ret = imx283_start_streaming(imx283, state);
1114 pm_runtime_mark_last_busy(imx283->dev);
1115 pm_runtime_put_autosuspend(imx283->dev);
1120 static int imx283_disable_streams(struct v4l2_subdev *sd,
1121 struct v4l2_subdev_state *state, u32 pad,
1124 struct imx283 *imx283 = to_imx283(sd);
1127 if (pad != IMAGE_PAD)
1130 ret = cci_write(imx283->cci, IMX283_REG_STANDBY, IMX283_STBLOGIC, NULL);
1132 dev_err(imx283->dev, "Failed to stop stream\n");
1134 pm_runtime_mark_last_busy(imx283->dev);
1135 pm_runtime_put_autosuspend(imx283->dev);
1140 /* Power/clock management functions */
1141 static int imx283_power_on(struct imx283 *imx283)
1145 ret = regulator_bulk_enable(ARRAY_SIZE(imx283_supply_name),
1148 dev_err(imx283->dev, "failed to enable regulators\n");
1152 ret = clk_prepare_enable(imx283->xclk);
1154 dev_err(imx283->dev, "failed to enable clock\n");
1158 gpiod_set_value_cansleep(imx283->reset_gpio, 0);
1160 usleep_range(IMX283_XCLR_MIN_DELAY_US,
1161 IMX283_XCLR_MIN_DELAY_US + IMX283_XCLR_DELAY_RANGE_US);
1166 regulator_bulk_disable(ARRAY_SIZE(imx283_supply_name), imx283->supplies);
1170 static int imx283_power_off(struct imx283 *imx283)
1172 gpiod_set_value_cansleep(imx283->reset_gpio, 1);
1173 regulator_bulk_disable(ARRAY_SIZE(imx283_supply_name), imx283->supplies);
1174 clk_disable_unprepare(imx283->xclk);
1179 static int imx283_runtime_resume(struct device *dev)
1181 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1182 struct imx283 *imx283 = to_imx283(sd);
1184 return imx283_power_on(imx283);
1187 static int imx283_runtime_suspend(struct device *dev)
1189 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1190 struct imx283 *imx283 = to_imx283(sd);
1192 imx283_power_off(imx283);
1197 static int imx283_get_regulators(struct imx283 *imx283)
1201 for (i = 0; i < ARRAY_SIZE(imx283_supply_name); i++)
1202 imx283->supplies[i].supply = imx283_supply_name[i];
1204 return devm_regulator_bulk_get(imx283->dev,
1205 ARRAY_SIZE(imx283_supply_name),
1209 /* Verify chip ID */
1210 static int imx283_identify_module(struct imx283 *imx283)
1215 ret = cci_read(imx283->cci, IMX283_REG_CHIP_ID, &val, NULL);
1217 dev_err(imx283->dev, "failed to read chip id %x, with error %d\n",
1218 IMX283_CHIP_ID, ret);
1222 if (val != IMX283_CHIP_ID) {
1223 dev_err(imx283->dev, "chip id mismatch: %x!=%llx\n",
1224 IMX283_CHIP_ID, val);
1231 static int imx283_get_selection(struct v4l2_subdev *sd,
1232 struct v4l2_subdev_state *sd_state,
1233 struct v4l2_subdev_selection *sel)
1235 switch (sel->target) {
1236 case V4L2_SEL_TGT_CROP: {
1237 sel->r = *v4l2_subdev_state_get_crop(sd_state, 0);
1241 case V4L2_SEL_TGT_NATIVE_SIZE:
1242 sel->r = imx283_native_area;
1245 case V4L2_SEL_TGT_CROP_DEFAULT:
1246 case V4L2_SEL_TGT_CROP_BOUNDS:
1247 sel->r = imx283_active_area;
1254 static const struct v4l2_subdev_core_ops imx283_core_ops = {
1255 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1256 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1259 static const struct v4l2_subdev_video_ops imx283_video_ops = {
1260 .s_stream = v4l2_subdev_s_stream_helper,
1263 static const struct v4l2_subdev_pad_ops imx283_pad_ops = {
1264 .enum_mbus_code = imx283_enum_mbus_code,
1265 .get_fmt = v4l2_subdev_get_fmt,
1266 .set_fmt = imx283_set_pad_format,
1267 .get_selection = imx283_get_selection,
1268 .enum_frame_size = imx283_enum_frame_size,
1269 .enable_streams = imx283_enable_streams,
1270 .disable_streams = imx283_disable_streams,
1273 static const struct v4l2_subdev_internal_ops imx283_internal_ops = {
1274 .init_state = imx283_init_state,
1277 static const struct v4l2_subdev_ops imx283_subdev_ops = {
1278 .core = &imx283_core_ops,
1279 .video = &imx283_video_ops,
1280 .pad = &imx283_pad_ops,
1283 /* Initialize control handlers */
1284 static int imx283_init_controls(struct imx283 *imx283)
1286 struct v4l2_ctrl_handler *ctrl_hdlr;
1287 struct v4l2_fwnode_device_properties props;
1288 struct v4l2_ctrl *link_freq;
1289 const struct imx283_mode *mode = &supported_modes_12bit[0];
1290 u64 min_hblank, max_hblank, def_hblank;
1294 ctrl_hdlr = &imx283->ctrl_handler;
1295 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
1300 * Create the controls here, but mode specific limits are setup
1301 * in the imx283_set_framing_limits() call below.
1304 /* By default, PIXEL_RATE is read only */
1305 pixel_rate = imx283_pixel_rate(imx283, mode);
1306 v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
1307 V4L2_CID_PIXEL_RATE, pixel_rate,
1308 pixel_rate, 1, pixel_rate);
1310 link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx283_ctrl_ops,
1312 __fls(imx283->link_freq_bitmap),
1313 __ffs(imx283->link_freq_bitmap),
1316 link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1318 /* Initialise vblank/hblank/exposure based on the current mode. */
1319 imx283->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
1321 mode->min_vmax - mode->height,
1323 mode->default_vmax - mode->height);
1325 min_hblank = mode->min_hmax - mode->width;
1326 max_hblank = imx283_iclk_to_pix(pixel_rate, IMX283_HMAX_MAX) - mode->width;
1327 def_hblank = mode->default_hmax - mode->width;
1328 imx283->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
1329 V4L2_CID_HBLANK, min_hblank, max_hblank,
1332 imx283->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops,
1334 IMX283_EXPOSURE_MIN,
1335 IMX283_EXPOSURE_MAX,
1336 IMX283_EXPOSURE_STEP,
1337 IMX283_EXPOSURE_DEFAULT);
1339 v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1340 IMX283_ANA_GAIN_MIN, IMX283_ANA_GAIN_MAX,
1341 IMX283_ANA_GAIN_STEP, IMX283_ANA_GAIN_DEFAULT);
1343 v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1344 IMX283_DGTL_GAIN_MIN, IMX283_DGTL_GAIN_MAX,
1345 IMX283_DGTL_GAIN_STEP, IMX283_DGTL_GAIN_DEFAULT);
1347 imx283->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx283_ctrl_ops, V4L2_CID_VFLIP,
1350 imx283->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1352 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx283_ctrl_ops,
1353 V4L2_CID_TEST_PATTERN,
1354 ARRAY_SIZE(imx283_tpg_menu) - 1,
1355 0, 0, imx283_tpg_menu);
1357 if (ctrl_hdlr->error) {
1358 ret = ctrl_hdlr->error;
1359 dev_err(imx283->dev, "control init failed (%d)\n", ret);
1363 ret = v4l2_fwnode_device_parse(imx283->dev, &props);
1367 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx283_ctrl_ops,
1372 imx283->sd.ctrl_handler = ctrl_hdlr;
1374 mutex_lock(imx283->ctrl_handler.lock);
1376 /* Setup exposure and frame/line length limits. */
1377 imx283_set_framing_limits(imx283, mode);
1379 mutex_unlock(imx283->ctrl_handler.lock);
1384 v4l2_ctrl_handler_free(ctrl_hdlr);
1389 static int imx283_parse_endpoint(struct imx283 *imx283)
1391 struct fwnode_handle *fwnode;
1392 struct v4l2_fwnode_endpoint bus_cfg = {
1393 .bus_type = V4L2_MBUS_CSI2_DPHY
1395 struct fwnode_handle *ep;
1398 fwnode = dev_fwnode(imx283->dev);
1399 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1401 dev_err(imx283->dev, "Failed to get next endpoint\n");
1405 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1406 fwnode_handle_put(ep);
1410 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 4) {
1411 dev_err(imx283->dev,
1412 "number of CSI2 data lanes %d is not supported\n",
1413 bus_cfg.bus.mipi_csi2.num_data_lanes);
1415 goto done_endpoint_free;
1418 ret = v4l2_link_freq_to_bitmap(imx283->dev, bus_cfg.link_frequencies,
1419 bus_cfg.nr_of_link_frequencies,
1420 link_frequencies, ARRAY_SIZE(link_frequencies),
1421 &imx283->link_freq_bitmap);
1424 v4l2_fwnode_endpoint_free(&bus_cfg);
1429 static int imx283_probe(struct i2c_client *client)
1431 struct imx283 *imx283;
1433 unsigned int xclk_freq;
1436 imx283 = devm_kzalloc(&client->dev, sizeof(*imx283), GFP_KERNEL);
1440 imx283->dev = &client->dev;
1442 v4l2_i2c_subdev_init(&imx283->sd, client, &imx283_subdev_ops);
1444 imx283->cci = devm_cci_regmap_init_i2c(client, 16);
1445 if (IS_ERR(imx283->cci)) {
1446 ret = PTR_ERR(imx283->cci);
1447 dev_err(imx283->dev, "failed to initialize CCI: %d\n", ret);
1451 /* Get system clock (xclk) */
1452 imx283->xclk = devm_clk_get(imx283->dev, NULL);
1453 if (IS_ERR(imx283->xclk)) {
1454 return dev_err_probe(imx283->dev, PTR_ERR(imx283->xclk),
1455 "failed to get xclk\n");
1458 xclk_freq = clk_get_rate(imx283->xclk);
1459 for (i = 0; i < ARRAY_SIZE(imx283_frequencies); i++) {
1460 if (xclk_freq == imx283_frequencies[i].mhz) {
1461 imx283->freq = &imx283_frequencies[i];
1465 if (!imx283->freq) {
1466 dev_err(imx283->dev, "xclk frequency unsupported: %d Hz\n", xclk_freq);
1470 ret = imx283_get_regulators(imx283);
1472 return dev_err_probe(imx283->dev, ret,
1473 "failed to get regulators\n");
1476 ret = imx283_parse_endpoint(imx283);
1478 dev_err(imx283->dev, "failed to parse endpoint configuration\n");
1482 /* Request optional enable pin */
1483 imx283->reset_gpio = devm_gpiod_get_optional(imx283->dev, "reset",
1485 if (IS_ERR(imx283->reset_gpio))
1486 return dev_err_probe(imx283->dev, PTR_ERR(imx283->reset_gpio),
1487 "failed to get reset GPIO\n");
1490 * The sensor must be powered for imx283_identify_module()
1491 * to be able to read the CHIP_ID register
1493 ret = imx283_power_on(imx283);
1497 ret = imx283_identify_module(imx283);
1499 goto error_power_off;
1502 * Enable runtime PM with autosuspend. As the device has been powered
1503 * manually, mark it as active, and increase the usage count without
1504 * resuming the device.
1506 pm_runtime_set_active(imx283->dev);
1507 pm_runtime_get_noresume(imx283->dev);
1508 pm_runtime_enable(imx283->dev);
1509 pm_runtime_set_autosuspend_delay(imx283->dev, 1000);
1510 pm_runtime_use_autosuspend(imx283->dev);
1512 /* This needs the pm runtime to be registered. */
1513 ret = imx283_init_controls(imx283);
1517 /* Initialize subdev */
1518 imx283->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1519 V4L2_SUBDEV_FL_HAS_EVENTS;
1520 imx283->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1521 imx283->sd.internal_ops = &imx283_internal_ops;
1523 /* Initialize source pads */
1524 imx283->pad.flags = MEDIA_PAD_FL_SOURCE;
1526 ret = media_entity_pads_init(&imx283->sd.entity, 1, &imx283->pad);
1528 dev_err(imx283->dev, "failed to init entity pads: %d\n", ret);
1529 goto error_handler_free;
1532 imx283->sd.state_lock = imx283->ctrl_handler.lock;
1533 ret = v4l2_subdev_init_finalize(&imx283->sd);
1535 dev_err(imx283->dev, "subdev init error: %d\n", ret);
1536 goto error_media_entity;
1539 ret = v4l2_async_register_subdev_sensor(&imx283->sd);
1541 dev_err(imx283->dev, "failed to register sensor sub-device: %d\n", ret);
1542 goto error_subdev_cleanup;
1546 * Decrease the PM usage count. The device will get suspended after the
1547 * autosuspend delay, turning the power off.
1549 pm_runtime_mark_last_busy(imx283->dev);
1550 pm_runtime_put_autosuspend(imx283->dev);
1554 error_subdev_cleanup:
1555 v4l2_subdev_cleanup(&imx283->sd);
1558 media_entity_cleanup(&imx283->sd.entity);
1561 v4l2_ctrl_handler_free(imx283->sd.ctrl_handler);
1564 pm_runtime_disable(imx283->dev);
1565 pm_runtime_set_suspended(imx283->dev);
1567 imx283_power_off(imx283);
1572 static void imx283_remove(struct i2c_client *client)
1574 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1575 struct imx283 *imx283 = to_imx283(sd);
1577 v4l2_async_unregister_subdev(sd);
1578 v4l2_subdev_cleanup(&imx283->sd);
1579 media_entity_cleanup(&sd->entity);
1580 v4l2_ctrl_handler_free(imx283->sd.ctrl_handler);
1582 pm_runtime_disable(imx283->dev);
1583 if (!pm_runtime_status_suspended(imx283->dev))
1584 imx283_power_off(imx283);
1585 pm_runtime_set_suspended(imx283->dev);
1588 static DEFINE_RUNTIME_DEV_PM_OPS(imx283_pm_ops, imx283_runtime_suspend,
1589 imx283_runtime_resume, NULL);
1591 static const struct of_device_id imx283_dt_ids[] = {
1592 { .compatible = "sony,imx283" },
1595 MODULE_DEVICE_TABLE(of, imx283_dt_ids);
1597 static struct i2c_driver imx283_i2c_driver = {
1600 .pm = pm_ptr(&imx283_pm_ops),
1601 .of_match_table = imx283_dt_ids,
1603 .probe = imx283_probe,
1604 .remove = imx283_remove,
1606 module_i2c_driver(imx283_i2c_driver);
1611 MODULE_DESCRIPTION("Sony IMX283 Sensor Driver");
1612 MODULE_LICENSE("GPL");