1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Sony IMX415 CMOS Image Sensor.
5 * Copyright (C) 2023 WolfVision GmbH.
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-subdev.h>
23 #define IMX415_PIXEL_ARRAY_TOP 0
24 #define IMX415_PIXEL_ARRAY_LEFT 0
25 #define IMX415_PIXEL_ARRAY_WIDTH 3864
26 #define IMX415_PIXEL_ARRAY_HEIGHT 2192
27 #define IMX415_PIXEL_ARRAY_VBLANK 58
29 #define IMX415_NUM_CLK_PARAM_REGS 11
31 #define IMX415_REG_8BIT(n) ((1 << 16) | (n))
32 #define IMX415_REG_16BIT(n) ((2 << 16) | (n))
33 #define IMX415_REG_24BIT(n) ((3 << 16) | (n))
34 #define IMX415_REG_SIZE_SHIFT 16
35 #define IMX415_REG_ADDR_MASK 0xffff
37 #define IMX415_MODE IMX415_REG_8BIT(0x3000)
38 #define IMX415_MODE_OPERATING (0)
39 #define IMX415_MODE_STANDBY BIT(0)
40 #define IMX415_REGHOLD IMX415_REG_8BIT(0x3001)
41 #define IMX415_REGHOLD_INVALID (0)
42 #define IMX415_REGHOLD_VALID BIT(0)
43 #define IMX415_XMSTA IMX415_REG_8BIT(0x3002)
44 #define IMX415_XMSTA_START (0)
45 #define IMX415_XMSTA_STOP BIT(0)
46 #define IMX415_BCWAIT_TIME IMX415_REG_16BIT(0x3008)
47 #define IMX415_CPWAIT_TIME IMX415_REG_16BIT(0x300A)
48 #define IMX415_WINMODE IMX415_REG_8BIT(0x301C)
49 #define IMX415_ADDMODE IMX415_REG_8BIT(0x3022)
50 #define IMX415_REVERSE IMX415_REG_8BIT(0x3030)
51 #define IMX415_HREVERSE_SHIFT (0)
52 #define IMX415_VREVERSE_SHIFT BIT(0)
53 #define IMX415_ADBIT IMX415_REG_8BIT(0x3031)
54 #define IMX415_MDBIT IMX415_REG_8BIT(0x3032)
55 #define IMX415_SYS_MODE IMX415_REG_8BIT(0x3033)
56 #define IMX415_OUTSEL IMX415_REG_8BIT(0x30C0)
57 #define IMX415_DRV IMX415_REG_8BIT(0x30C1)
58 #define IMX415_VMAX IMX415_REG_24BIT(0x3024)
59 #define IMX415_HMAX IMX415_REG_16BIT(0x3028)
60 #define IMX415_SHR0 IMX415_REG_24BIT(0x3050)
61 #define IMX415_GAIN_PCG_0 IMX415_REG_16BIT(0x3090)
62 #define IMX415_AGAIN_MIN 0
63 #define IMX415_AGAIN_MAX 100
64 #define IMX415_AGAIN_STEP 1
65 #define IMX415_BLKLEVEL IMX415_REG_16BIT(0x30E2)
66 #define IMX415_BLKLEVEL_DEFAULT 50
67 #define IMX415_TPG_EN_DUOUT IMX415_REG_8BIT(0x30E4)
68 #define IMX415_TPG_PATSEL_DUOUT IMX415_REG_8BIT(0x30E6)
69 #define IMX415_TPG_COLORWIDTH IMX415_REG_8BIT(0x30E8)
70 #define IMX415_TESTCLKEN_MIPI IMX415_REG_8BIT(0x3110)
71 #define IMX415_INCKSEL1 IMX415_REG_8BIT(0x3115)
72 #define IMX415_INCKSEL2 IMX415_REG_8BIT(0x3116)
73 #define IMX415_INCKSEL3 IMX415_REG_16BIT(0x3118)
74 #define IMX415_INCKSEL4 IMX415_REG_16BIT(0x311A)
75 #define IMX415_INCKSEL5 IMX415_REG_8BIT(0x311E)
76 #define IMX415_DIG_CLP_MODE IMX415_REG_8BIT(0x32C8)
77 #define IMX415_WRJ_OPEN IMX415_REG_8BIT(0x3390)
78 #define IMX415_SENSOR_INFO IMX415_REG_16BIT(0x3F12)
79 #define IMX415_SENSOR_INFO_MASK 0xFFF
80 #define IMX415_CHIP_ID 0x514
81 #define IMX415_LANEMODE IMX415_REG_16BIT(0x4001)
82 #define IMX415_LANEMODE_2 1
83 #define IMX415_LANEMODE_4 3
84 #define IMX415_TXCLKESC_FREQ IMX415_REG_16BIT(0x4004)
85 #define IMX415_INCKSEL6 IMX415_REG_8BIT(0x400C)
86 #define IMX415_TCLKPOST IMX415_REG_16BIT(0x4018)
87 #define IMX415_TCLKPREPARE IMX415_REG_16BIT(0x401A)
88 #define IMX415_TCLKTRAIL IMX415_REG_16BIT(0x401C)
89 #define IMX415_TCLKZERO IMX415_REG_16BIT(0x401E)
90 #define IMX415_THSPREPARE IMX415_REG_16BIT(0x4020)
91 #define IMX415_THSZERO IMX415_REG_16BIT(0x4022)
92 #define IMX415_THSTRAIL IMX415_REG_16BIT(0x4024)
93 #define IMX415_THSEXIT IMX415_REG_16BIT(0x4026)
94 #define IMX415_TLPX IMX415_REG_16BIT(0x4028)
95 #define IMX415_INCKSEL7 IMX415_REG_8BIT(0x4074)
102 static const char *const imx415_supply_names[] = {
109 * The IMX415 data sheet uses lane rates but v4l2 uses link frequency to
110 * describe MIPI CSI-2 speed. This driver uses lane rates wherever possible
111 * and converts them to link frequencies by a factor of two when needed.
113 static const s64 link_freq_menu_items[] = {
114 594000000 / 2, 720000000 / 2, 891000000 / 2,
115 1440000000 / 2, 1485000000 / 2,
118 struct imx415_clk_params {
121 struct imx415_reg regs[IMX415_NUM_CLK_PARAM_REGS];
124 /* INCK Settings - includes all lane rate and INCK dependent registers */
125 static const struct imx415_clk_params imx415_clk_params[] = {
127 .lane_rate = 594000000,
129 .regs[0] = { IMX415_BCWAIT_TIME, 0x05D },
130 .regs[1] = { IMX415_CPWAIT_TIME, 0x042 },
131 .regs[2] = { IMX415_SYS_MODE, 0x7 },
132 .regs[3] = { IMX415_INCKSEL1, 0x00 },
133 .regs[4] = { IMX415_INCKSEL2, 0x23 },
134 .regs[5] = { IMX415_INCKSEL3, 0x084 },
135 .regs[6] = { IMX415_INCKSEL4, 0x0E7 },
136 .regs[7] = { IMX415_INCKSEL5, 0x23 },
137 .regs[8] = { IMX415_INCKSEL6, 0x0 },
138 .regs[9] = { IMX415_INCKSEL7, 0x1 },
139 .regs[10] = { IMX415_TXCLKESC_FREQ, 0x06C0 },
142 .lane_rate = 720000000,
144 .regs[0] = { IMX415_BCWAIT_TIME, 0x054 },
145 .regs[1] = { IMX415_CPWAIT_TIME, 0x03B },
146 .regs[2] = { IMX415_SYS_MODE, 0x9 },
147 .regs[3] = { IMX415_INCKSEL1, 0x00 },
148 .regs[4] = { IMX415_INCKSEL2, 0x23 },
149 .regs[5] = { IMX415_INCKSEL3, 0x0B4 },
150 .regs[6] = { IMX415_INCKSEL4, 0x0FC },
151 .regs[7] = { IMX415_INCKSEL5, 0x23 },
152 .regs[8] = { IMX415_INCKSEL6, 0x0 },
153 .regs[9] = { IMX415_INCKSEL7, 0x1 },
154 .regs[10] = { IMX415_TXCLKESC_FREQ, 0x0600 },
157 .lane_rate = 891000000,
159 .regs[0] = { IMX415_BCWAIT_TIME, 0x05D },
160 .regs[1] = { IMX415_CPWAIT_TIME, 0x042 },
161 .regs[2] = { IMX415_SYS_MODE, 0x5 },
162 .regs[3] = { IMX415_INCKSEL1, 0x00 },
163 .regs[4] = { IMX415_INCKSEL2, 0x23 },
164 .regs[5] = { IMX415_INCKSEL3, 0x0C6 },
165 .regs[6] = { IMX415_INCKSEL4, 0x0E7 },
166 .regs[7] = { IMX415_INCKSEL5, 0x23 },
167 .regs[8] = { IMX415_INCKSEL6, 0x0 },
168 .regs[9] = { IMX415_INCKSEL7, 0x1 },
169 .regs[10] = { IMX415_TXCLKESC_FREQ, 0x06C0 },
172 .lane_rate = 1440000000,
174 .regs[0] = { IMX415_BCWAIT_TIME, 0x054 },
175 .regs[1] = { IMX415_CPWAIT_TIME, 0x03B },
176 .regs[2] = { IMX415_SYS_MODE, 0x8 },
177 .regs[3] = { IMX415_INCKSEL1, 0x00 },
178 .regs[4] = { IMX415_INCKSEL2, 0x23 },
179 .regs[5] = { IMX415_INCKSEL3, 0x0B4 },
180 .regs[6] = { IMX415_INCKSEL4, 0x0FC },
181 .regs[7] = { IMX415_INCKSEL5, 0x23 },
182 .regs[8] = { IMX415_INCKSEL6, 0x1 },
183 .regs[9] = { IMX415_INCKSEL7, 0x0 },
184 .regs[10] = { IMX415_TXCLKESC_FREQ, 0x0600 },
187 .lane_rate = 1485000000,
189 .regs[0] = { IMX415_BCWAIT_TIME, 0x05D },
190 .regs[1] = { IMX415_CPWAIT_TIME, 0x042 },
191 .regs[2] = { IMX415_SYS_MODE, 0x8 },
192 .regs[3] = { IMX415_INCKSEL1, 0x00 },
193 .regs[4] = { IMX415_INCKSEL2, 0x23 },
194 .regs[5] = { IMX415_INCKSEL3, 0x0A5 },
195 .regs[6] = { IMX415_INCKSEL4, 0x0E7 },
196 .regs[7] = { IMX415_INCKSEL5, 0x23 },
197 .regs[8] = { IMX415_INCKSEL6, 0x1 },
198 .regs[9] = { IMX415_INCKSEL7, 0x0 },
199 .regs[10] = { IMX415_TXCLKESC_FREQ, 0x06C0 },
203 /* all-pixel 2-lane 720 Mbps 15.74 Hz mode */
204 static const struct imx415_reg imx415_mode_2_720[] = {
205 { IMX415_VMAX, 0x08CA },
206 { IMX415_HMAX, 0x07F0 },
207 { IMX415_LANEMODE, IMX415_LANEMODE_2 },
208 { IMX415_TCLKPOST, 0x006F },
209 { IMX415_TCLKPREPARE, 0x002F },
210 { IMX415_TCLKTRAIL, 0x002F },
211 { IMX415_TCLKZERO, 0x00BF },
212 { IMX415_THSPREPARE, 0x002F },
213 { IMX415_THSZERO, 0x0057 },
214 { IMX415_THSTRAIL, 0x002F },
215 { IMX415_THSEXIT, 0x004F },
216 { IMX415_TLPX, 0x0027 },
219 /* all-pixel 2-lane 1440 Mbps 30.01 Hz mode */
220 static const struct imx415_reg imx415_mode_2_1440[] = {
221 { IMX415_VMAX, 0x08CA },
222 { IMX415_HMAX, 0x042A },
223 { IMX415_LANEMODE, IMX415_LANEMODE_2 },
224 { IMX415_TCLKPOST, 0x009F },
225 { IMX415_TCLKPREPARE, 0x0057 },
226 { IMX415_TCLKTRAIL, 0x0057 },
227 { IMX415_TCLKZERO, 0x0187 },
228 { IMX415_THSPREPARE, 0x005F },
229 { IMX415_THSZERO, 0x00A7 },
230 { IMX415_THSTRAIL, 0x005F },
231 { IMX415_THSEXIT, 0x0097 },
232 { IMX415_TLPX, 0x004F },
235 /* all-pixel 4-lane 891 Mbps 30 Hz mode */
236 static const struct imx415_reg imx415_mode_4_891[] = {
237 { IMX415_VMAX, 0x08CA },
238 { IMX415_HMAX, 0x044C },
239 { IMX415_LANEMODE, IMX415_LANEMODE_4 },
240 { IMX415_TCLKPOST, 0x007F },
241 { IMX415_TCLKPREPARE, 0x0037 },
242 { IMX415_TCLKTRAIL, 0x0037 },
243 { IMX415_TCLKZERO, 0x00F7 },
244 { IMX415_THSPREPARE, 0x003F },
245 { IMX415_THSZERO, 0x006F },
246 { IMX415_THSTRAIL, 0x003F },
247 { IMX415_THSEXIT, 0x005F },
248 { IMX415_TLPX, 0x002F },
251 struct imx415_mode_reg_list {
253 const struct imx415_reg *regs;
257 * Mode : number of lanes, lane rate and frame rate dependent settings
259 * pixel_rate and hmax_pix are needed to calculate hblank for the v4l2 ctrl
260 * interface. These values can not be found in the data sheet and should be
261 * treated as virtual values. Use following table when adding new modes.
263 * lane_rate lanes fps hmax_pix pixel_rate
265 * 594 2 10.000 4400 99000000
266 * 891 2 15.000 4400 148500000
267 * 720 2 15.748 4064 144000000
268 * 1782 2 30.000 4400 297000000
269 * 2079 2 30.000 4400 297000000
270 * 1440 2 30.019 4510 304615385
272 * 594 4 20.000 5500 247500000
273 * 594 4 25.000 4400 247500000
274 * 720 4 25.000 4400 247500000
275 * 720 4 30.019 4510 304615385
276 * 891 4 30.000 4400 297000000
277 * 1440 4 30.019 4510 304615385
278 * 1440 4 60.038 4510 609230769
279 * 1485 4 60.000 4400 594000000
280 * 1782 4 60.000 4400 594000000
281 * 2079 4 60.000 4400 594000000
282 * 2376 4 90.164 4392 891000000
289 struct imx415_mode_reg_list reg_list;
293 static const struct imx415_mode supported_modes[] = {
295 .lane_rate = 720000000,
298 .pixel_rate = 144000000,
300 .num_of_regs = ARRAY_SIZE(imx415_mode_2_720),
301 .regs = imx415_mode_2_720,
305 .lane_rate = 1440000000,
308 .pixel_rate = 304615385,
310 .num_of_regs = ARRAY_SIZE(imx415_mode_2_1440),
311 .regs = imx415_mode_2_1440,
315 .lane_rate = 891000000,
318 .pixel_rate = 297000000,
320 .num_of_regs = ARRAY_SIZE(imx415_mode_4_891),
321 .regs = imx415_mode_4_891,
326 static const struct regmap_config imx415_regmap_config = {
331 static const char *const imx415_test_pattern_menu[] = {
337 "stripes light/dark grey",
338 "stripes dark/light grey",
339 "stripes black/dark grey",
340 "stripes dark grey/black",
341 "stripes black/white",
342 "stripes white/black",
343 "horizontal color bar",
344 "vertical color bar",
350 struct regulator_bulk_data supplies[ARRAY_SIZE(imx415_supply_names)];
351 struct gpio_desc *reset;
352 struct regmap *regmap;
354 const struct imx415_clk_params *clk_params;
356 struct v4l2_subdev subdev;
357 struct media_pad pad;
359 struct v4l2_ctrl_handler ctrls;
360 struct v4l2_ctrl *vblank;
361 struct v4l2_ctrl *hflip;
362 struct v4l2_ctrl *vflip;
364 unsigned int cur_mode;
365 unsigned int num_data_lanes;
369 * This table includes fixed register settings and a bunch of undocumented
370 * registers that have to be set to another value than default.
372 static const struct imx415_reg imx415_init_table[] = {
373 /* use all-pixel readout mode, no flip */
374 { IMX415_WINMODE, 0x00 },
375 { IMX415_ADDMODE, 0x00 },
376 { IMX415_REVERSE, 0x00 },
377 /* use RAW 10-bit mode */
378 { IMX415_ADBIT, 0x00 },
379 { IMX415_MDBIT, 0x00 },
380 /* output VSYNC on XVS and low on XHS */
381 { IMX415_OUTSEL, 0x22 },
382 { IMX415_DRV, 0x00 },
384 /* SONY magic registers */
385 { IMX415_REG_8BIT(0x32D4), 0x21 },
386 { IMX415_REG_8BIT(0x32EC), 0xA1 },
387 { IMX415_REG_8BIT(0x3452), 0x7F },
388 { IMX415_REG_8BIT(0x3453), 0x03 },
389 { IMX415_REG_8BIT(0x358A), 0x04 },
390 { IMX415_REG_8BIT(0x35A1), 0x02 },
391 { IMX415_REG_8BIT(0x36BC), 0x0C },
392 { IMX415_REG_8BIT(0x36CC), 0x53 },
393 { IMX415_REG_8BIT(0x36CD), 0x00 },
394 { IMX415_REG_8BIT(0x36CE), 0x3C },
395 { IMX415_REG_8BIT(0x36D0), 0x8C },
396 { IMX415_REG_8BIT(0x36D1), 0x00 },
397 { IMX415_REG_8BIT(0x36D2), 0x71 },
398 { IMX415_REG_8BIT(0x36D4), 0x3C },
399 { IMX415_REG_8BIT(0x36D6), 0x53 },
400 { IMX415_REG_8BIT(0x36D7), 0x00 },
401 { IMX415_REG_8BIT(0x36D8), 0x71 },
402 { IMX415_REG_8BIT(0x36DA), 0x8C },
403 { IMX415_REG_8BIT(0x36DB), 0x00 },
404 { IMX415_REG_8BIT(0x3724), 0x02 },
405 { IMX415_REG_8BIT(0x3726), 0x02 },
406 { IMX415_REG_8BIT(0x3732), 0x02 },
407 { IMX415_REG_8BIT(0x3734), 0x03 },
408 { IMX415_REG_8BIT(0x3736), 0x03 },
409 { IMX415_REG_8BIT(0x3742), 0x03 },
410 { IMX415_REG_8BIT(0x3862), 0xE0 },
411 { IMX415_REG_8BIT(0x38CC), 0x30 },
412 { IMX415_REG_8BIT(0x38CD), 0x2F },
413 { IMX415_REG_8BIT(0x395C), 0x0C },
414 { IMX415_REG_8BIT(0x3A42), 0xD1 },
415 { IMX415_REG_8BIT(0x3A4C), 0x77 },
416 { IMX415_REG_8BIT(0x3AE0), 0x02 },
417 { IMX415_REG_8BIT(0x3AEC), 0x0C },
418 { IMX415_REG_8BIT(0x3B00), 0x2E },
419 { IMX415_REG_8BIT(0x3B06), 0x29 },
420 { IMX415_REG_8BIT(0x3B98), 0x25 },
421 { IMX415_REG_8BIT(0x3B99), 0x21 },
422 { IMX415_REG_8BIT(0x3B9B), 0x13 },
423 { IMX415_REG_8BIT(0x3B9C), 0x13 },
424 { IMX415_REG_8BIT(0x3B9D), 0x13 },
425 { IMX415_REG_8BIT(0x3B9E), 0x13 },
426 { IMX415_REG_8BIT(0x3BA1), 0x00 },
427 { IMX415_REG_8BIT(0x3BA2), 0x06 },
428 { IMX415_REG_8BIT(0x3BA3), 0x0B },
429 { IMX415_REG_8BIT(0x3BA4), 0x10 },
430 { IMX415_REG_8BIT(0x3BA5), 0x14 },
431 { IMX415_REG_8BIT(0x3BA6), 0x18 },
432 { IMX415_REG_8BIT(0x3BA7), 0x1A },
433 { IMX415_REG_8BIT(0x3BA8), 0x1A },
434 { IMX415_REG_8BIT(0x3BA9), 0x1A },
435 { IMX415_REG_8BIT(0x3BAC), 0xED },
436 { IMX415_REG_8BIT(0x3BAD), 0x01 },
437 { IMX415_REG_8BIT(0x3BAE), 0xF6 },
438 { IMX415_REG_8BIT(0x3BAF), 0x02 },
439 { IMX415_REG_8BIT(0x3BB0), 0xA2 },
440 { IMX415_REG_8BIT(0x3BB1), 0x03 },
441 { IMX415_REG_8BIT(0x3BB2), 0xE0 },
442 { IMX415_REG_8BIT(0x3BB3), 0x03 },
443 { IMX415_REG_8BIT(0x3BB4), 0xE0 },
444 { IMX415_REG_8BIT(0x3BB5), 0x03 },
445 { IMX415_REG_8BIT(0x3BB6), 0xE0 },
446 { IMX415_REG_8BIT(0x3BB7), 0x03 },
447 { IMX415_REG_8BIT(0x3BB8), 0xE0 },
448 { IMX415_REG_8BIT(0x3BBA), 0xE0 },
449 { IMX415_REG_8BIT(0x3BBC), 0xDA },
450 { IMX415_REG_8BIT(0x3BBE), 0x88 },
451 { IMX415_REG_8BIT(0x3BC0), 0x44 },
452 { IMX415_REG_8BIT(0x3BC2), 0x7B },
453 { IMX415_REG_8BIT(0x3BC4), 0xA2 },
454 { IMX415_REG_8BIT(0x3BC8), 0xBD },
455 { IMX415_REG_8BIT(0x3BCA), 0xBD },
458 static inline struct imx415 *to_imx415(struct v4l2_subdev *sd)
460 return container_of(sd, struct imx415, subdev);
463 static int imx415_read(struct imx415 *sensor, u32 addr)
468 ret = regmap_raw_read(sensor->regmap, addr & IMX415_REG_ADDR_MASK, data,
469 (addr >> IMX415_REG_SIZE_SHIFT) & 3);
473 return (data[2] << 16) | (data[1] << 8) | data[0];
476 static int imx415_write(struct imx415 *sensor, u32 addr, u32 value)
478 u8 data[3] = { value & 0xff, (value >> 8) & 0xff, value >> 16 };
481 ret = regmap_raw_write(sensor->regmap, addr & IMX415_REG_ADDR_MASK,
482 data, (addr >> IMX415_REG_SIZE_SHIFT) & 3);
484 dev_err_ratelimited(sensor->dev,
485 "%u-bit write to 0x%04x failed: %d\n",
486 ((addr >> IMX415_REG_SIZE_SHIFT) & 3) * 8,
487 addr & IMX415_REG_ADDR_MASK, ret);
492 static int imx415_set_testpattern(struct imx415 *sensor, int val)
497 ret = imx415_write(sensor, IMX415_BLKLEVEL, 0x00);
500 ret = imx415_write(sensor, IMX415_TPG_EN_DUOUT, 0x01);
503 ret = imx415_write(sensor, IMX415_TPG_PATSEL_DUOUT, val - 1);
506 ret = imx415_write(sensor, IMX415_TPG_COLORWIDTH, 0x01);
509 ret = imx415_write(sensor, IMX415_TESTCLKEN_MIPI, 0x20);
512 ret = imx415_write(sensor, IMX415_DIG_CLP_MODE, 0x00);
515 ret = imx415_write(sensor, IMX415_WRJ_OPEN, 0x00);
517 ret = imx415_write(sensor, IMX415_BLKLEVEL,
518 IMX415_BLKLEVEL_DEFAULT);
521 ret = imx415_write(sensor, IMX415_TPG_EN_DUOUT, 0x00);
524 ret = imx415_write(sensor, IMX415_TESTCLKEN_MIPI, 0x00);
527 ret = imx415_write(sensor, IMX415_DIG_CLP_MODE, 0x01);
530 ret = imx415_write(sensor, IMX415_WRJ_OPEN, 0x01);
535 static int imx415_s_ctrl(struct v4l2_ctrl *ctrl)
537 struct imx415 *sensor = container_of(ctrl->handler, struct imx415,
539 const struct v4l2_mbus_framefmt *format;
540 struct v4l2_subdev_state *state;
545 if (!pm_runtime_get_if_in_use(sensor->dev))
548 state = v4l2_subdev_get_locked_active_state(&sensor->subdev);
549 format = v4l2_subdev_get_pad_format(&sensor->subdev, state, 0);
552 case V4L2_CID_EXPOSURE:
553 /* clamp the exposure value to VMAX. */
554 vmax = format->height + sensor->vblank->cur.val;
555 ctrl->val = min_t(int, ctrl->val, vmax);
556 ret = imx415_write(sensor, IMX415_SHR0, vmax - ctrl->val);
559 case V4L2_CID_ANALOGUE_GAIN:
560 /* analogue gain in 0.3 dB step size */
561 ret = imx415_write(sensor, IMX415_GAIN_PCG_0, ctrl->val);
566 flip = (sensor->hflip->val << IMX415_HREVERSE_SHIFT) |
567 (sensor->vflip->val << IMX415_VREVERSE_SHIFT);
568 ret = imx415_write(sensor, IMX415_REVERSE, flip);
571 case V4L2_CID_TEST_PATTERN:
572 ret = imx415_set_testpattern(sensor, ctrl->val);
580 pm_runtime_put(sensor->dev);
585 static const struct v4l2_ctrl_ops imx415_ctrl_ops = {
586 .s_ctrl = imx415_s_ctrl,
589 static int imx415_ctrls_init(struct imx415 *sensor)
591 struct v4l2_fwnode_device_properties props;
592 struct v4l2_ctrl *ctrl;
593 u64 pixel_rate = supported_modes[sensor->cur_mode].pixel_rate;
594 u64 lane_rate = supported_modes[sensor->cur_mode].lane_rate;
595 u32 exposure_max = IMX415_PIXEL_ARRAY_HEIGHT +
596 IMX415_PIXEL_ARRAY_VBLANK - 8;
601 ret = v4l2_fwnode_device_parse(sensor->dev, &props);
605 v4l2_ctrl_handler_init(&sensor->ctrls, 10);
607 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); ++i) {
608 if (lane_rate == link_freq_menu_items[i] * 2)
611 if (i == ARRAY_SIZE(link_freq_menu_items)) {
612 return dev_err_probe(sensor->dev, -EINVAL,
613 "lane rate %llu not supported\n",
617 ctrl = v4l2_ctrl_new_int_menu(&sensor->ctrls, &imx415_ctrl_ops,
619 ARRAY_SIZE(link_freq_menu_items) - 1, i,
620 link_freq_menu_items);
623 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
625 v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, V4L2_CID_EXPOSURE,
626 4, exposure_max, 1, exposure_max);
628 v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops,
629 V4L2_CID_ANALOGUE_GAIN, IMX415_AGAIN_MIN,
630 IMX415_AGAIN_MAX, IMX415_AGAIN_STEP,
633 hblank = supported_modes[sensor->cur_mode].hmax_pix -
634 IMX415_PIXEL_ARRAY_WIDTH;
635 ctrl = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops,
636 V4L2_CID_HBLANK, hblank, hblank, 1, hblank);
638 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
640 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops,
642 IMX415_PIXEL_ARRAY_VBLANK,
643 IMX415_PIXEL_ARRAY_VBLANK, 1,
644 IMX415_PIXEL_ARRAY_VBLANK);
646 sensor->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
649 * The pixel rate used here is a virtual value and can be used for
650 * calculating the frame rate together with hblank. It may not
651 * necessarily be the physically correct pixel clock.
653 v4l2_ctrl_new_std(&sensor->ctrls, NULL, V4L2_CID_PIXEL_RATE, pixel_rate,
654 pixel_rate, 1, pixel_rate);
656 sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops,
657 V4L2_CID_HFLIP, 0, 1, 1, 0);
658 sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops,
659 V4L2_CID_VFLIP, 0, 1, 1, 0);
661 v4l2_ctrl_new_std_menu_items(&sensor->ctrls, &imx415_ctrl_ops,
662 V4L2_CID_TEST_PATTERN,
663 ARRAY_SIZE(imx415_test_pattern_menu) - 1,
664 0, 0, imx415_test_pattern_menu);
666 v4l2_ctrl_new_fwnode_properties(&sensor->ctrls, &imx415_ctrl_ops,
669 if (sensor->ctrls.error) {
670 dev_err_probe(sensor->dev, sensor->ctrls.error,
671 "failed to add controls\n");
672 v4l2_ctrl_handler_free(&sensor->ctrls);
673 return sensor->ctrls.error;
675 sensor->subdev.ctrl_handler = &sensor->ctrls;
680 static int imx415_set_mode(struct imx415 *sensor, int mode)
682 const struct imx415_reg *reg;
686 if (mode >= ARRAY_SIZE(supported_modes)) {
687 dev_err(sensor->dev, "Mode %d not supported\n", mode);
691 for (i = 0; i < supported_modes[mode].reg_list.num_of_regs; ++i) {
692 reg = &supported_modes[mode].reg_list.regs[i];
693 ret = imx415_write(sensor, reg->address, reg->val);
698 for (i = 0; i < IMX415_NUM_CLK_PARAM_REGS; ++i) {
699 reg = &sensor->clk_params->regs[i];
700 ret = imx415_write(sensor, reg->address, reg->val);
708 static int imx415_setup(struct imx415 *sensor, struct v4l2_subdev_state *state)
713 for (i = 0; i < ARRAY_SIZE(imx415_init_table); ++i) {
714 ret = imx415_write(sensor, imx415_init_table[i].address,
715 imx415_init_table[i].val);
720 return imx415_set_mode(sensor, sensor->cur_mode);
723 static int imx415_wakeup(struct imx415 *sensor)
727 ret = imx415_write(sensor, IMX415_MODE, IMX415_MODE_OPERATING);
732 * According to the datasheet we have to wait at least 63 us after
733 * leaving standby mode. But this doesn't work even after 30 ms.
734 * So probably this should be 63 ms and therefore we wait for 80 ms.
741 static int imx415_stream_on(struct imx415 *sensor)
745 ret = imx415_wakeup(sensor);
749 return imx415_write(sensor, IMX415_XMSTA, IMX415_XMSTA_START);
752 static int imx415_stream_off(struct imx415 *sensor)
756 ret = imx415_write(sensor, IMX415_XMSTA, IMX415_XMSTA_STOP);
760 return imx415_write(sensor, IMX415_MODE, IMX415_MODE_STANDBY);
763 static int imx415_s_stream(struct v4l2_subdev *sd, int enable)
765 struct imx415 *sensor = to_imx415(sd);
766 struct v4l2_subdev_state *state;
769 state = v4l2_subdev_lock_and_get_active_state(sd);
772 ret = imx415_stream_off(sensor);
774 pm_runtime_mark_last_busy(sensor->dev);
775 pm_runtime_put_autosuspend(sensor->dev);
780 ret = pm_runtime_resume_and_get(sensor->dev);
784 ret = imx415_setup(sensor, state);
788 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls);
792 ret = imx415_stream_on(sensor);
799 v4l2_subdev_unlock_state(state);
805 * In case of error, turn the power off synchronously as the device
806 * likely has no other chance to recover.
808 pm_runtime_put_sync(sensor->dev);
813 static int imx415_enum_mbus_code(struct v4l2_subdev *sd,
814 struct v4l2_subdev_state *state,
815 struct v4l2_subdev_mbus_code_enum *code)
817 if (code->index != 0)
820 code->code = MEDIA_BUS_FMT_SGBRG10_1X10;
825 static int imx415_enum_frame_size(struct v4l2_subdev *sd,
826 struct v4l2_subdev_state *state,
827 struct v4l2_subdev_frame_size_enum *fse)
829 const struct v4l2_mbus_framefmt *format;
831 format = v4l2_subdev_get_pad_format(sd, state, fse->pad);
833 if (fse->index > 0 || fse->code != format->code)
836 fse->min_width = IMX415_PIXEL_ARRAY_WIDTH;
837 fse->max_width = fse->min_width;
838 fse->min_height = IMX415_PIXEL_ARRAY_HEIGHT;
839 fse->max_height = fse->min_height;
843 static int imx415_set_format(struct v4l2_subdev *sd,
844 struct v4l2_subdev_state *state,
845 struct v4l2_subdev_format *fmt)
847 struct v4l2_mbus_framefmt *format;
849 format = v4l2_subdev_get_pad_format(sd, state, fmt->pad);
851 format->width = fmt->format.width;
852 format->height = fmt->format.height;
853 format->code = MEDIA_BUS_FMT_SGBRG10_1X10;
854 format->field = V4L2_FIELD_NONE;
855 format->colorspace = V4L2_COLORSPACE_RAW;
856 format->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
857 format->quantization = V4L2_QUANTIZATION_DEFAULT;
858 format->xfer_func = V4L2_XFER_FUNC_NONE;
860 fmt->format = *format;
864 static int imx415_get_selection(struct v4l2_subdev *sd,
865 struct v4l2_subdev_state *sd_state,
866 struct v4l2_subdev_selection *sel)
868 switch (sel->target) {
869 case V4L2_SEL_TGT_CROP:
870 case V4L2_SEL_TGT_CROP_DEFAULT:
871 case V4L2_SEL_TGT_CROP_BOUNDS:
872 sel->r.top = IMX415_PIXEL_ARRAY_TOP;
873 sel->r.left = IMX415_PIXEL_ARRAY_LEFT;
874 sel->r.width = IMX415_PIXEL_ARRAY_WIDTH;
875 sel->r.height = IMX415_PIXEL_ARRAY_HEIGHT;
883 static int imx415_init_cfg(struct v4l2_subdev *sd,
884 struct v4l2_subdev_state *state)
886 struct v4l2_subdev_format format = {
888 .width = IMX415_PIXEL_ARRAY_WIDTH,
889 .height = IMX415_PIXEL_ARRAY_HEIGHT,
893 imx415_set_format(sd, state, &format);
898 static const struct v4l2_subdev_video_ops imx415_subdev_video_ops = {
899 .s_stream = imx415_s_stream,
902 static const struct v4l2_subdev_pad_ops imx415_subdev_pad_ops = {
903 .enum_mbus_code = imx415_enum_mbus_code,
904 .enum_frame_size = imx415_enum_frame_size,
905 .get_fmt = v4l2_subdev_get_fmt,
906 .set_fmt = imx415_set_format,
907 .get_selection = imx415_get_selection,
908 .init_cfg = imx415_init_cfg,
911 static const struct v4l2_subdev_ops imx415_subdev_ops = {
912 .video = &imx415_subdev_video_ops,
913 .pad = &imx415_subdev_pad_ops,
916 static int imx415_subdev_init(struct imx415 *sensor)
918 struct i2c_client *client = to_i2c_client(sensor->dev);
921 v4l2_i2c_subdev_init(&sensor->subdev, client, &imx415_subdev_ops);
923 ret = imx415_ctrls_init(sensor);
927 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
928 V4L2_SUBDEV_FL_HAS_EVENTS;
929 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
930 sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
931 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
933 v4l2_ctrl_handler_free(&sensor->ctrls);
937 sensor->subdev.state_lock = sensor->subdev.ctrl_handler->lock;
938 v4l2_subdev_init_finalize(&sensor->subdev);
943 static void imx415_subdev_cleanup(struct imx415 *sensor)
945 media_entity_cleanup(&sensor->subdev.entity);
946 v4l2_ctrl_handler_free(&sensor->ctrls);
949 static int imx415_power_on(struct imx415 *sensor)
953 ret = regulator_bulk_enable(ARRAY_SIZE(sensor->supplies),
958 gpiod_set_value_cansleep(sensor->reset, 0);
962 ret = clk_prepare_enable(sensor->clk);
967 * Data sheet states that 20 us are required before communication start,
968 * but this doesn't work in all cases. Use 100 us to be on the safe
971 usleep_range(100, 200);
976 gpiod_set_value_cansleep(sensor->reset, 1);
977 regulator_bulk_disable(ARRAY_SIZE(sensor->supplies), sensor->supplies);
981 static void imx415_power_off(struct imx415 *sensor)
983 clk_disable_unprepare(sensor->clk);
984 gpiod_set_value_cansleep(sensor->reset, 1);
985 regulator_bulk_disable(ARRAY_SIZE(sensor->supplies), sensor->supplies);
988 static int imx415_identify_model(struct imx415 *sensor)
993 * While most registers can be read when the sensor is in standby, this
994 * is not the case of the sensor info register :-(
996 ret = imx415_wakeup(sensor);
998 return dev_err_probe(sensor->dev, ret,
999 "failed to get sensor out of standby\n");
1001 ret = imx415_read(sensor, IMX415_SENSOR_INFO);
1003 dev_err_probe(sensor->dev, ret,
1004 "failed to read sensor information\n");
1008 model = ret & IMX415_SENSOR_INFO_MASK;
1011 case IMX415_CHIP_ID:
1012 dev_info(sensor->dev, "Detected IMX415 image sensor\n");
1015 ret = dev_err_probe(sensor->dev, -ENODEV,
1016 "invalid device model 0x%04x\n", model);
1023 imx415_write(sensor, IMX415_MODE, IMX415_MODE_STANDBY);
1027 static int imx415_check_inck(unsigned long inck, u64 link_frequency)
1031 for (i = 0; i < ARRAY_SIZE(imx415_clk_params); ++i) {
1032 if ((imx415_clk_params[i].lane_rate == link_frequency * 2) &&
1033 imx415_clk_params[i].inck == inck)
1037 if (i == ARRAY_SIZE(imx415_clk_params))
1043 static int imx415_parse_hw_config(struct imx415 *sensor)
1045 struct v4l2_fwnode_endpoint bus_cfg = {
1046 .bus_type = V4L2_MBUS_CSI2_DPHY,
1048 struct fwnode_handle *ep;
1054 for (i = 0; i < ARRAY_SIZE(sensor->supplies); ++i)
1055 sensor->supplies[i].supply = imx415_supply_names[i];
1057 ret = devm_regulator_bulk_get(sensor->dev, ARRAY_SIZE(sensor->supplies),
1060 return dev_err_probe(sensor->dev, ret,
1061 "failed to get supplies\n");
1063 sensor->reset = devm_gpiod_get_optional(sensor->dev, "reset",
1065 if (IS_ERR(sensor->reset))
1066 return dev_err_probe(sensor->dev, PTR_ERR(sensor->reset),
1067 "failed to get reset GPIO\n");
1069 sensor->clk = devm_clk_get(sensor->dev, "inck");
1070 if (IS_ERR(sensor->clk))
1071 return dev_err_probe(sensor->dev, PTR_ERR(sensor->clk),
1072 "failed to get clock\n");
1074 ep = fwnode_graph_get_next_endpoint(dev_fwnode(sensor->dev), NULL);
1078 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1079 fwnode_handle_put(ep);
1083 switch (bus_cfg.bus.mipi_csi2.num_data_lanes) {
1086 sensor->num_data_lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
1089 ret = dev_err_probe(sensor->dev, -EINVAL,
1090 "invalid number of CSI2 data lanes %d\n",
1091 bus_cfg.bus.mipi_csi2.num_data_lanes);
1092 goto done_endpoint_free;
1095 if (!bus_cfg.nr_of_link_frequencies) {
1096 ret = dev_err_probe(sensor->dev, -EINVAL,
1097 "no link frequencies defined");
1098 goto done_endpoint_free;
1102 * Check if there exists a sensor mode defined for current INCK,
1103 * number of lanes and given lane rates.
1105 inck = clk_get_rate(sensor->clk);
1106 for (i = 0; i < bus_cfg.nr_of_link_frequencies; ++i) {
1107 if (imx415_check_inck(inck, bus_cfg.link_frequencies[i])) {
1108 dev_dbg(sensor->dev,
1109 "INCK %lu Hz not supported for this link freq",
1114 for (j = 0; j < ARRAY_SIZE(supported_modes); ++j) {
1115 if (sensor->num_data_lanes != supported_modes[j].lanes)
1117 if (bus_cfg.link_frequencies[i] * 2 !=
1118 supported_modes[j].lane_rate)
1120 sensor->cur_mode = j;
1123 if (j < ARRAY_SIZE(supported_modes))
1126 if (i == bus_cfg.nr_of_link_frequencies) {
1127 ret = dev_err_probe(sensor->dev, -EINVAL,
1128 "no valid sensor mode defined\n");
1129 goto done_endpoint_free;
1132 lane_rate = supported_modes[sensor->cur_mode].lane_rate;
1133 for (i = 0; i < ARRAY_SIZE(imx415_clk_params); ++i) {
1134 if (lane_rate == imx415_clk_params[i].lane_rate &&
1135 inck == imx415_clk_params[i].inck) {
1136 sensor->clk_params = &imx415_clk_params[i];
1140 if (i == ARRAY_SIZE(imx415_clk_params)) {
1141 ret = dev_err_probe(sensor->dev, -EINVAL,
1142 "Mode %d not supported\n",
1144 goto done_endpoint_free;
1148 dev_dbg(sensor->dev, "clock: %lu Hz, lane_rate: %llu bps, lanes: %d\n",
1149 inck, lane_rate, sensor->num_data_lanes);
1152 v4l2_fwnode_endpoint_free(&bus_cfg);
1157 static int imx415_probe(struct i2c_client *client)
1159 struct imx415 *sensor;
1162 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
1166 sensor->dev = &client->dev;
1168 ret = imx415_parse_hw_config(sensor);
1172 sensor->regmap = devm_regmap_init_i2c(client, &imx415_regmap_config);
1173 if (IS_ERR(sensor->regmap))
1174 return PTR_ERR(sensor->regmap);
1177 * Enable power management. The driver supports runtime PM, but needs to
1178 * work when runtime PM is disabled in the kernel. To that end, power
1179 * the sensor on manually here, identify it, and fully initialize it.
1181 ret = imx415_power_on(sensor);
1185 ret = imx415_identify_model(sensor);
1189 ret = imx415_subdev_init(sensor);
1194 * Enable runtime PM. As the device has been powered manually, mark it
1195 * as active, and increase the usage count without resuming the device.
1197 pm_runtime_set_active(sensor->dev);
1198 pm_runtime_get_noresume(sensor->dev);
1199 pm_runtime_enable(sensor->dev);
1201 ret = v4l2_async_register_subdev_sensor(&sensor->subdev);
1206 * Finally, enable autosuspend and decrease the usage count. The device
1207 * will get suspended after the autosuspend delay, turning the power
1210 pm_runtime_set_autosuspend_delay(sensor->dev, 1000);
1211 pm_runtime_use_autosuspend(sensor->dev);
1212 pm_runtime_put_autosuspend(sensor->dev);
1217 pm_runtime_disable(sensor->dev);
1218 pm_runtime_put_noidle(sensor->dev);
1219 imx415_subdev_cleanup(sensor);
1221 imx415_power_off(sensor);
1225 static void imx415_remove(struct i2c_client *client)
1227 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1228 struct imx415 *sensor = to_imx415(subdev);
1230 v4l2_async_unregister_subdev(subdev);
1232 imx415_subdev_cleanup(sensor);
1235 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1236 * make sure to turn power off manually.
1238 pm_runtime_disable(sensor->dev);
1239 if (!pm_runtime_status_suspended(sensor->dev))
1240 imx415_power_off(sensor);
1241 pm_runtime_set_suspended(sensor->dev);
1244 static int imx415_runtime_resume(struct device *dev)
1246 struct i2c_client *client = to_i2c_client(dev);
1247 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1248 struct imx415 *sensor = to_imx415(subdev);
1250 return imx415_power_on(sensor);
1253 static int imx415_runtime_suspend(struct device *dev)
1255 struct i2c_client *client = to_i2c_client(dev);
1256 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1257 struct imx415 *sensor = to_imx415(subdev);
1259 imx415_power_off(sensor);
1264 static DEFINE_RUNTIME_DEV_PM_OPS(imx415_pm_ops, imx415_runtime_suspend,
1265 imx415_runtime_resume, NULL);
1267 static const struct of_device_id imx415_of_match[] = {
1268 { .compatible = "sony,imx415" },
1272 MODULE_DEVICE_TABLE(of, imx415_of_match);
1274 static struct i2c_driver imx415_driver = {
1275 .probe = imx415_probe,
1276 .remove = imx415_remove,
1279 .of_match_table = imx415_of_match,
1280 .pm = pm_ptr(&imx415_pm_ops),
1284 module_i2c_driver(imx415_driver);
1286 MODULE_DESCRIPTION("Sony IMX415 image sensor driver");
1287 MODULE_AUTHOR("Gerald Loacker <gerald.loacker@wolfvision.net>");
1288 MODULE_AUTHOR("Michael Riesch <michael.riesch@wolfvision.net>");
1289 MODULE_LICENSE("GPL");