1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Sony imx412 Camera Sensor Driver
4 *
5 * Copyright (C) 2021 Intel Corporation
6 */
7#include <asm/unaligned.h>
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/pm_runtime.h>
14#include <linux/regulator/consumer.h>
15
16#include <media/v4l2-ctrls.h>
17#include <media/v4l2-fwnode.h>
18#include <media/v4l2-subdev.h>
19
20/* Streaming Mode */
21#define IMX412_REG_MODE_SELECT 0x0100
22#define IMX412_MODE_STANDBY 0x00
23#define IMX412_MODE_STREAMING 0x01
24
25/* Lines per frame */
26#define IMX412_REG_LPFR 0x0340
27
28/* Chip ID */
29#define IMX412_REG_ID 0x0016
30#define IMX412_ID 0x577
31
32/* Exposure control */
33#define IMX412_REG_EXPOSURE_CIT 0x0202
34#define IMX412_EXPOSURE_MIN 8
35#define IMX412_EXPOSURE_OFFSET 22
36#define IMX412_EXPOSURE_STEP 1
37#define IMX412_EXPOSURE_DEFAULT 0x0648
38
39/* Analog gain control */
40#define IMX412_REG_AGAIN 0x0204
41#define IMX412_AGAIN_MIN 0
42#define IMX412_AGAIN_MAX 978
43#define IMX412_AGAIN_STEP 1
44#define IMX412_AGAIN_DEFAULT 0
45
46/* Group hold register */
47#define IMX412_REG_HOLD 0x0104
48
49/* Input clock rate */
50#define IMX412_INCLK_RATE 24000000
51
52/* CSI2 HW configuration */
53#define IMX412_LINK_FREQ 600000000
54#define IMX412_NUM_DATA_LANES 4
55
56#define IMX412_REG_MIN 0x00
57#define IMX412_REG_MAX 0xffff
58
59/**
60 * struct imx412_reg - imx412 sensor register
61 * @address: Register address
62 * @val: Register value
63 */
64struct imx412_reg {
65 u16 address;
66 u8 val;
67};
68
69/**
70 * struct imx412_reg_list - imx412 sensor register list
71 * @num_of_regs: Number of registers in the list
72 * @regs: Pointer to register list
73 */
74struct imx412_reg_list {
75 u32 num_of_regs;
76 const struct imx412_reg *regs;
77};
78
79/**
80 * struct imx412_mode - imx412 sensor mode structure
81 * @width: Frame width
82 * @height: Frame height
83 * @code: Format code
84 * @hblank: Horizontal blanking in lines
85 * @vblank: Vertical blanking in lines
86 * @vblank_min: Minimum vertical blanking in lines
87 * @vblank_max: Maximum vertical blanking in lines
88 * @pclk: Sensor pixel clock
89 * @link_freq_idx: Link frequency index
90 * @reg_list: Register list for sensor mode
91 */
92struct imx412_mode {
93 u32 width;
94 u32 height;
95 u32 code;
96 u32 hblank;
97 u32 vblank;
98 u32 vblank_min;
99 u32 vblank_max;
100 u64 pclk;
101 u32 link_freq_idx;
102 struct imx412_reg_list reg_list;
103};
104
105static const char * const imx412_supply_names[] = {
106 "dovdd", /* Digital I/O power */
107 "avdd", /* Analog power */
108 "dvdd", /* Digital core power */
109};
110
111/**
112 * struct imx412 - imx412 sensor device structure
113 * @dev: Pointer to generic device
114 * @client: Pointer to i2c client
115 * @sd: V4L2 sub-device
116 * @pad: Media pad. Only one pad supported
117 * @reset_gpio: Sensor reset gpio
118 * @inclk: Sensor input clock
119 * @supplies: Regulator supplies
120 * @ctrl_handler: V4L2 control handler
121 * @link_freq_ctrl: Pointer to link frequency control
122 * @pclk_ctrl: Pointer to pixel clock control
123 * @hblank_ctrl: Pointer to horizontal blanking control
124 * @vblank_ctrl: Pointer to vertical blanking control
125 * @exp_ctrl: Pointer to exposure control
126 * @again_ctrl: Pointer to analog gain control
127 * @vblank: Vertical blanking in lines
128 * @cur_mode: Pointer to current selected sensor mode
129 * @mutex: Mutex for serializing sensor controls
130 */
131struct imx412 {
132 struct device *dev;
133 struct i2c_client *client;
134 struct v4l2_subdev sd;
135 struct media_pad pad;
136 struct gpio_desc *reset_gpio;
137 struct clk *inclk;
138 struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
139 struct v4l2_ctrl_handler ctrl_handler;
140 struct v4l2_ctrl *link_freq_ctrl;
141 struct v4l2_ctrl *pclk_ctrl;
142 struct v4l2_ctrl *hblank_ctrl;
143 struct v4l2_ctrl *vblank_ctrl;
144 struct {
145 struct v4l2_ctrl *exp_ctrl;
146 struct v4l2_ctrl *again_ctrl;
147 };
148 u32 vblank;
149 const struct imx412_mode *cur_mode;
150 struct mutex mutex;
151};
152
153static const s64 link_freq[] = {
154 IMX412_LINK_FREQ,
155};
156
157/* Sensor mode registers */
158static const struct imx412_reg mode_4056x3040_regs[] = {
159 {0x0136, 0x18},
160 {0x0137, 0x00},
161 {0x3c7e, 0x08},
162 {0x3c7f, 0x02},
163 {0x38a8, 0x1f},
164 {0x38a9, 0xff},
165 {0x38aa, 0x1f},
166 {0x38ab, 0xff},
167 {0x55d4, 0x00},
168 {0x55d5, 0x00},
169 {0x55d6, 0x07},
170 {0x55d7, 0xff},
171 {0x55e8, 0x07},
172 {0x55e9, 0xff},
173 {0x55ea, 0x00},
174 {0x55eb, 0x00},
175 {0x575c, 0x07},
176 {0x575d, 0xff},
177 {0x575e, 0x00},
178 {0x575f, 0x00},
179 {0x5764, 0x00},
180 {0x5765, 0x00},
181 {0x5766, 0x07},
182 {0x5767, 0xff},
183 {0x5974, 0x04},
184 {0x5975, 0x01},
185 {0x5f10, 0x09},
186 {0x5f11, 0x92},
187 {0x5f12, 0x32},
188 {0x5f13, 0x72},
189 {0x5f14, 0x16},
190 {0x5f15, 0xba},
191 {0x5f17, 0x13},
192 {0x5f18, 0x24},
193 {0x5f19, 0x60},
194 {0x5f1a, 0xe3},
195 {0x5f1b, 0xad},
196 {0x5f1c, 0x74},
197 {0x5f2d, 0x25},
198 {0x5f5c, 0xd0},
199 {0x6a22, 0x00},
200 {0x6a23, 0x1d},
201 {0x7ba8, 0x00},
202 {0x7ba9, 0x00},
203 {0x886b, 0x00},
204 {0x9002, 0x0a},
205 {0x9004, 0x1a},
206 {0x9214, 0x93},
207 {0x9215, 0x69},
208 {0x9216, 0x93},
209 {0x9217, 0x6b},
210 {0x9218, 0x93},
211 {0x9219, 0x6d},
212 {0x921a, 0x57},
213 {0x921b, 0x58},
214 {0x921c, 0x57},
215 {0x921d, 0x59},
216 {0x921e, 0x57},
217 {0x921f, 0x5a},
218 {0x9220, 0x57},
219 {0x9221, 0x5b},
220 {0x9222, 0x93},
221 {0x9223, 0x02},
222 {0x9224, 0x93},
223 {0x9225, 0x03},
224 {0x9226, 0x93},
225 {0x9227, 0x04},
226 {0x9228, 0x93},
227 {0x9229, 0x05},
228 {0x922a, 0x98},
229 {0x922b, 0x21},
230 {0x922c, 0xb2},
231 {0x922d, 0xdb},
232 {0x922e, 0xb2},
233 {0x922f, 0xdc},
234 {0x9230, 0xb2},
235 {0x9231, 0xdd},
236 {0x9232, 0xe2},
237 {0x9233, 0xe1},
238 {0x9234, 0xb2},
239 {0x9235, 0xe2},
240 {0x9236, 0xb2},
241 {0x9237, 0xe3},
242 {0x9238, 0xb7},
243 {0x9239, 0xb9},
244 {0x923a, 0xb7},
245 {0x923b, 0xbb},
246 {0x923c, 0xb7},
247 {0x923d, 0xbc},
248 {0x923e, 0xb7},
249 {0x923f, 0xc5},
250 {0x9240, 0xb7},
251 {0x9241, 0xc7},
252 {0x9242, 0xb7},
253 {0x9243, 0xc9},
254 {0x9244, 0x98},
255 {0x9245, 0x56},
256 {0x9246, 0x98},
257 {0x9247, 0x55},
258 {0x9380, 0x00},
259 {0x9381, 0x62},
260 {0x9382, 0x00},
261 {0x9383, 0x56},
262 {0x9384, 0x00},
263 {0x9385, 0x52},
264 {0x9388, 0x00},
265 {0x9389, 0x55},
266 {0x938a, 0x00},
267 {0x938b, 0x55},
268 {0x938c, 0x00},
269 {0x938d, 0x41},
270 {0x5078, 0x01},
271 {0x0112, 0x0a},
272 {0x0113, 0x0a},
273 {0x0114, 0x03},
274 {0x0342, 0x11},
275 {0x0343, 0xa0},
276 {0x0340, 0x0d},
277 {0x0341, 0xda},
278 {0x3210, 0x00},
279 {0x0344, 0x00},
280 {0x0345, 0x00},
281 {0x0346, 0x00},
282 {0x0347, 0x00},
283 {0x0348, 0x0f},
284 {0x0349, 0xd7},
285 {0x034a, 0x0b},
286 {0x034b, 0xdf},
287 {0x00e3, 0x00},
288 {0x00e4, 0x00},
289 {0x00e5, 0x01},
290 {0x00fc, 0x0a},
291 {0x00fd, 0x0a},
292 {0x00fe, 0x0a},
293 {0x00ff, 0x0a},
294 {0xe013, 0x00},
295 {0x0220, 0x00},
296 {0x0221, 0x11},
297 {0x0381, 0x01},
298 {0x0383, 0x01},
299 {0x0385, 0x01},
300 {0x0387, 0x01},
301 {0x0900, 0x00},
302 {0x0901, 0x11},
303 {0x0902, 0x00},
304 {0x3140, 0x02},
305 {0x3241, 0x11},
306 {0x3250, 0x03},
307 {0x3e10, 0x00},
308 {0x3e11, 0x00},
309 {0x3f0d, 0x00},
310 {0x3f42, 0x00},
311 {0x3f43, 0x00},
312 {0x0401, 0x00},
313 {0x0404, 0x00},
314 {0x0405, 0x10},
315 {0x0408, 0x00},
316 {0x0409, 0x00},
317 {0x040a, 0x00},
318 {0x040b, 0x00},
319 {0x040c, 0x0f},
320 {0x040d, 0xd8},
321 {0x040e, 0x0b},
322 {0x040f, 0xe0},
323 {0x034c, 0x0f},
324 {0x034d, 0xd8},
325 {0x034e, 0x0b},
326 {0x034f, 0xe0},
327 {0x0301, 0x05},
328 {0x0303, 0x02},
329 {0x0305, 0x04},
330 {0x0306, 0x00},
331 {0x0307, 0xc8},
332 {0x0309, 0x0a},
333 {0x030b, 0x01},
334 {0x030d, 0x02},
335 {0x030e, 0x01},
336 {0x030f, 0x5e},
337 {0x0310, 0x00},
338 {0x0820, 0x12},
339 {0x0821, 0xc0},
340 {0x0822, 0x00},
341 {0x0823, 0x00},
342 {0x3e20, 0x01},
343 {0x3e37, 0x00},
344 {0x3f50, 0x00},
345 {0x3f56, 0x00},
346 {0x3f57, 0xe2},
347 {0x3c0a, 0x5a},
348 {0x3c0b, 0x55},
349 {0x3c0c, 0x28},
350 {0x3c0d, 0x07},
351 {0x3c0e, 0xff},
352 {0x3c0f, 0x00},
353 {0x3c10, 0x00},
354 {0x3c11, 0x02},
355 {0x3c12, 0x00},
356 {0x3c13, 0x03},
357 {0x3c14, 0x00},
358 {0x3c15, 0x00},
359 {0x3c16, 0x0c},
360 {0x3c17, 0x0c},
361 {0x3c18, 0x0c},
362 {0x3c19, 0x0a},
363 {0x3c1a, 0x0a},
364 {0x3c1b, 0x0a},
365 {0x3c1c, 0x00},
366 {0x3c1d, 0x00},
367 {0x3c1e, 0x00},
368 {0x3c1f, 0x00},
369 {0x3c20, 0x00},
370 {0x3c21, 0x00},
371 {0x3c22, 0x3f},
372 {0x3c23, 0x0a},
373 {0x3e35, 0x01},
374 {0x3f4a, 0x03},
375 {0x3f4b, 0xbf},
376 {0x3f26, 0x00},
377 {0x0202, 0x0d},
378 {0x0203, 0xc4},
379 {0x0204, 0x00},
380 {0x0205, 0x00},
381 {0x020e, 0x01},
382 {0x020f, 0x00},
383 {0x0210, 0x01},
384 {0x0211, 0x00},
385 {0x0212, 0x01},
386 {0x0213, 0x00},
387 {0x0214, 0x01},
388 {0x0215, 0x00},
389 {0xbcf1, 0x00},
390};
391
392/* Supported sensor mode configurations */
393static const struct imx412_mode supported_mode = {
394 .width = 4056,
395 .height = 3040,
396 .hblank = 456,
397 .vblank = 506,
398 .vblank_min = 506,
399 .vblank_max = 32420,
400 .pclk = 480000000,
401 .link_freq_idx = 0,
402 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
403 .reg_list = {
404 .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
405 .regs = mode_4056x3040_regs,
406 },
407};
408
409/**
410 * to_imx412() - imx412 V4L2 sub-device to imx412 device.
411 * @subdev: pointer to imx412 V4L2 sub-device
412 *
413 * Return: pointer to imx412 device
414 */
415static inline struct imx412 *to_imx412(struct v4l2_subdev *subdev)
416{
417 return container_of(subdev, struct imx412, sd);
418}
419
420/**
421 * imx412_read_reg() - Read registers.
422 * @imx412: pointer to imx412 device
423 * @reg: register address
424 * @len: length of bytes to read. Max supported bytes is 4
425 * @val: pointer to register value to be filled.
426 *
427 * Return: 0 if successful, error code otherwise.
428 */
429static int imx412_read_reg(struct imx412 *imx412, u16 reg, u32 len, u32 *val)
430{
431 struct i2c_client *client = v4l2_get_subdevdata(sd: &imx412->sd);
432 struct i2c_msg msgs[2] = {0};
433 u8 addr_buf[2] = {0};
434 u8 data_buf[4] = {0};
435 int ret;
436
437 if (WARN_ON(len > 4))
438 return -EINVAL;
439
440 put_unaligned_be16(val: reg, p: addr_buf);
441
442 /* Write register address */
443 msgs[0].addr = client->addr;
444 msgs[0].flags = 0;
445 msgs[0].len = ARRAY_SIZE(addr_buf);
446 msgs[0].buf = addr_buf;
447
448 /* Read data from register */
449 msgs[1].addr = client->addr;
450 msgs[1].flags = I2C_M_RD;
451 msgs[1].len = len;
452 msgs[1].buf = &data_buf[4 - len];
453
454 ret = i2c_transfer(adap: client->adapter, msgs, ARRAY_SIZE(msgs));
455 if (ret != ARRAY_SIZE(msgs))
456 return -EIO;
457
458 *val = get_unaligned_be32(p: data_buf);
459
460 return 0;
461}
462
463/**
464 * imx412_write_reg() - Write register
465 * @imx412: pointer to imx412 device
466 * @reg: register address
467 * @len: length of bytes. Max supported bytes is 4
468 * @val: register value
469 *
470 * Return: 0 if successful, error code otherwise.
471 */
472static int imx412_write_reg(struct imx412 *imx412, u16 reg, u32 len, u32 val)
473{
474 struct i2c_client *client = v4l2_get_subdevdata(sd: &imx412->sd);
475 u8 buf[6] = {0};
476
477 if (WARN_ON(len > 4))
478 return -EINVAL;
479
480 put_unaligned_be16(val: reg, p: buf);
481 put_unaligned_be32(val: val << (8 * (4 - len)), p: buf + 2);
482 if (i2c_master_send(client, buf, count: len + 2) != len + 2)
483 return -EIO;
484
485 return 0;
486}
487
488/**
489 * imx412_write_regs() - Write a list of registers
490 * @imx412: pointer to imx412 device
491 * @regs: list of registers to be written
492 * @len: length of registers array
493 *
494 * Return: 0 if successful, error code otherwise.
495 */
496static int imx412_write_regs(struct imx412 *imx412,
497 const struct imx412_reg *regs, u32 len)
498{
499 unsigned int i;
500 int ret;
501
502 for (i = 0; i < len; i++) {
503 ret = imx412_write_reg(imx412, reg: regs[i].address, len: 1, val: regs[i].val);
504 if (ret)
505 return ret;
506 }
507
508 return 0;
509}
510
511/**
512 * imx412_update_controls() - Update control ranges based on streaming mode
513 * @imx412: pointer to imx412 device
514 * @mode: pointer to imx412_mode sensor mode
515 *
516 * Return: 0 if successful, error code otherwise.
517 */
518static int imx412_update_controls(struct imx412 *imx412,
519 const struct imx412_mode *mode)
520{
521 int ret;
522
523 ret = __v4l2_ctrl_s_ctrl(ctrl: imx412->link_freq_ctrl, val: mode->link_freq_idx);
524 if (ret)
525 return ret;
526
527 ret = __v4l2_ctrl_s_ctrl(ctrl: imx412->hblank_ctrl, val: mode->hblank);
528 if (ret)
529 return ret;
530
531 return __v4l2_ctrl_modify_range(ctrl: imx412->vblank_ctrl, min: mode->vblank_min,
532 max: mode->vblank_max, step: 1, def: mode->vblank);
533}
534
535/**
536 * imx412_update_exp_gain() - Set updated exposure and gain
537 * @imx412: pointer to imx412 device
538 * @exposure: updated exposure value
539 * @gain: updated analog gain value
540 *
541 * Return: 0 if successful, error code otherwise.
542 */
543static int imx412_update_exp_gain(struct imx412 *imx412, u32 exposure, u32 gain)
544{
545 u32 lpfr, shutter;
546 int ret;
547
548 lpfr = imx412->vblank + imx412->cur_mode->height;
549 shutter = lpfr - exposure;
550
551 dev_dbg(imx412->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u",
552 exposure, gain, shutter, lpfr);
553
554 ret = imx412_write_reg(imx412, IMX412_REG_HOLD, len: 1, val: 1);
555 if (ret)
556 return ret;
557
558 ret = imx412_write_reg(imx412, IMX412_REG_LPFR, len: 2, val: lpfr);
559 if (ret)
560 goto error_release_group_hold;
561
562 ret = imx412_write_reg(imx412, IMX412_REG_EXPOSURE_CIT, len: 2, val: shutter);
563 if (ret)
564 goto error_release_group_hold;
565
566 ret = imx412_write_reg(imx412, IMX412_REG_AGAIN, len: 2, val: gain);
567
568error_release_group_hold:
569 imx412_write_reg(imx412, IMX412_REG_HOLD, len: 1, val: 0);
570
571 return ret;
572}
573
574/**
575 * imx412_set_ctrl() - Set subdevice control
576 * @ctrl: pointer to v4l2_ctrl structure
577 *
578 * Supported controls:
579 * - V4L2_CID_VBLANK
580 * - cluster controls:
581 * - V4L2_CID_ANALOGUE_GAIN
582 * - V4L2_CID_EXPOSURE
583 *
584 * Return: 0 if successful, error code otherwise.
585 */
586static int imx412_set_ctrl(struct v4l2_ctrl *ctrl)
587{
588 struct imx412 *imx412 =
589 container_of(ctrl->handler, struct imx412, ctrl_handler);
590 u32 analog_gain;
591 u32 exposure;
592 int ret;
593
594 switch (ctrl->id) {
595 case V4L2_CID_VBLANK:
596 imx412->vblank = imx412->vblank_ctrl->val;
597
598 dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u",
599 imx412->vblank,
600 imx412->vblank + imx412->cur_mode->height);
601
602 ret = __v4l2_ctrl_modify_range(ctrl: imx412->exp_ctrl,
603 IMX412_EXPOSURE_MIN,
604 max: imx412->vblank +
605 imx412->cur_mode->height -
606 IMX412_EXPOSURE_OFFSET,
607 step: 1, IMX412_EXPOSURE_DEFAULT);
608 break;
609 case V4L2_CID_EXPOSURE:
610 /* Set controls only if sensor is in power on state */
611 if (!pm_runtime_get_if_in_use(dev: imx412->dev))
612 return 0;
613
614 exposure = ctrl->val;
615 analog_gain = imx412->again_ctrl->val;
616
617 dev_dbg(imx412->dev, "Received exp %u, analog gain %u",
618 exposure, analog_gain);
619
620 ret = imx412_update_exp_gain(imx412, exposure, gain: analog_gain);
621
622 pm_runtime_put(dev: imx412->dev);
623
624 break;
625 default:
626 dev_err(imx412->dev, "Invalid control %d", ctrl->id);
627 ret = -EINVAL;
628 }
629
630 return ret;
631}
632
633/* V4l2 subdevice control ops*/
634static const struct v4l2_ctrl_ops imx412_ctrl_ops = {
635 .s_ctrl = imx412_set_ctrl,
636};
637
638/**
639 * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
640 * @sd: pointer to imx412 V4L2 sub-device structure
641 * @sd_state: V4L2 sub-device configuration
642 * @code: V4L2 sub-device code enumeration need to be filled
643 *
644 * Return: 0 if successful, error code otherwise.
645 */
646static int imx412_enum_mbus_code(struct v4l2_subdev *sd,
647 struct v4l2_subdev_state *sd_state,
648 struct v4l2_subdev_mbus_code_enum *code)
649{
650 if (code->index > 0)
651 return -EINVAL;
652
653 code->code = supported_mode.code;
654
655 return 0;
656}
657
658/**
659 * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
660 * @sd: pointer to imx412 V4L2 sub-device structure
661 * @sd_state: V4L2 sub-device configuration
662 * @fsize: V4L2 sub-device size enumeration need to be filled
663 *
664 * Return: 0 if successful, error code otherwise.
665 */
666static int imx412_enum_frame_size(struct v4l2_subdev *sd,
667 struct v4l2_subdev_state *sd_state,
668 struct v4l2_subdev_frame_size_enum *fsize)
669{
670 if (fsize->index > 0)
671 return -EINVAL;
672
673 if (fsize->code != supported_mode.code)
674 return -EINVAL;
675
676 fsize->min_width = supported_mode.width;
677 fsize->max_width = fsize->min_width;
678 fsize->min_height = supported_mode.height;
679 fsize->max_height = fsize->min_height;
680
681 return 0;
682}
683
684/**
685 * imx412_fill_pad_format() - Fill subdevice pad format
686 * from selected sensor mode
687 * @imx412: pointer to imx412 device
688 * @mode: pointer to imx412_mode sensor mode
689 * @fmt: V4L2 sub-device format need to be filled
690 */
691static void imx412_fill_pad_format(struct imx412 *imx412,
692 const struct imx412_mode *mode,
693 struct v4l2_subdev_format *fmt)
694{
695 fmt->format.width = mode->width;
696 fmt->format.height = mode->height;
697 fmt->format.code = mode->code;
698 fmt->format.field = V4L2_FIELD_NONE;
699 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
700 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
701 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
702 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
703}
704
705/**
706 * imx412_get_pad_format() - Get subdevice pad format
707 * @sd: pointer to imx412 V4L2 sub-device structure
708 * @sd_state: V4L2 sub-device configuration
709 * @fmt: V4L2 sub-device format need to be set
710 *
711 * Return: 0 if successful, error code otherwise.
712 */
713static int imx412_get_pad_format(struct v4l2_subdev *sd,
714 struct v4l2_subdev_state *sd_state,
715 struct v4l2_subdev_format *fmt)
716{
717 struct imx412 *imx412 = to_imx412(subdev: sd);
718
719 mutex_lock(&imx412->mutex);
720
721 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
722 struct v4l2_mbus_framefmt *framefmt;
723
724 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
725 fmt->format = *framefmt;
726 } else {
727 imx412_fill_pad_format(imx412, mode: imx412->cur_mode, fmt);
728 }
729
730 mutex_unlock(lock: &imx412->mutex);
731
732 return 0;
733}
734
735/**
736 * imx412_set_pad_format() - Set subdevice pad format
737 * @sd: pointer to imx412 V4L2 sub-device structure
738 * @sd_state: V4L2 sub-device configuration
739 * @fmt: V4L2 sub-device format need to be set
740 *
741 * Return: 0 if successful, error code otherwise.
742 */
743static int imx412_set_pad_format(struct v4l2_subdev *sd,
744 struct v4l2_subdev_state *sd_state,
745 struct v4l2_subdev_format *fmt)
746{
747 struct imx412 *imx412 = to_imx412(subdev: sd);
748 const struct imx412_mode *mode;
749 int ret = 0;
750
751 mutex_lock(&imx412->mutex);
752
753 mode = &supported_mode;
754 imx412_fill_pad_format(imx412, mode, fmt);
755
756 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
757 struct v4l2_mbus_framefmt *framefmt;
758
759 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
760 *framefmt = fmt->format;
761 } else {
762 ret = imx412_update_controls(imx412, mode);
763 if (!ret)
764 imx412->cur_mode = mode;
765 }
766
767 mutex_unlock(lock: &imx412->mutex);
768
769 return ret;
770}
771
772/**
773 * imx412_init_state() - Initialize sub-device state
774 * @sd: pointer to imx412 V4L2 sub-device structure
775 * @sd_state: V4L2 sub-device configuration
776 *
777 * Return: 0 if successful, error code otherwise.
778 */
779static int imx412_init_state(struct v4l2_subdev *sd,
780 struct v4l2_subdev_state *sd_state)
781{
782 struct imx412 *imx412 = to_imx412(subdev: sd);
783 struct v4l2_subdev_format fmt = { 0 };
784
785 fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
786 imx412_fill_pad_format(imx412, mode: &supported_mode, fmt: &fmt);
787
788 return imx412_set_pad_format(sd, sd_state, fmt: &fmt);
789}
790
791/**
792 * imx412_start_streaming() - Start sensor stream
793 * @imx412: pointer to imx412 device
794 *
795 * Return: 0 if successful, error code otherwise.
796 */
797static int imx412_start_streaming(struct imx412 *imx412)
798{
799 const struct imx412_reg_list *reg_list;
800 int ret;
801
802 /* Write sensor mode registers */
803 reg_list = &imx412->cur_mode->reg_list;
804 ret = imx412_write_regs(imx412, regs: reg_list->regs,
805 len: reg_list->num_of_regs);
806 if (ret) {
807 dev_err(imx412->dev, "fail to write initial registers");
808 return ret;
809 }
810
811 /* Setup handler will write actual exposure and gain */
812 ret = __v4l2_ctrl_handler_setup(hdl: imx412->sd.ctrl_handler);
813 if (ret) {
814 dev_err(imx412->dev, "fail to setup handler");
815 return ret;
816 }
817
818 /* Delay is required before streaming*/
819 usleep_range(min: 7400, max: 8000);
820
821 /* Start streaming */
822 ret = imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
823 len: 1, IMX412_MODE_STREAMING);
824 if (ret) {
825 dev_err(imx412->dev, "fail to start streaming");
826 return ret;
827 }
828
829 return 0;
830}
831
832/**
833 * imx412_stop_streaming() - Stop sensor stream
834 * @imx412: pointer to imx412 device
835 *
836 * Return: 0 if successful, error code otherwise.
837 */
838static int imx412_stop_streaming(struct imx412 *imx412)
839{
840 return imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
841 len: 1, IMX412_MODE_STANDBY);
842}
843
844/**
845 * imx412_set_stream() - Enable sensor streaming
846 * @sd: pointer to imx412 subdevice
847 * @enable: set to enable sensor streaming
848 *
849 * Return: 0 if successful, error code otherwise.
850 */
851static int imx412_set_stream(struct v4l2_subdev *sd, int enable)
852{
853 struct imx412 *imx412 = to_imx412(subdev: sd);
854 int ret;
855
856 mutex_lock(&imx412->mutex);
857
858 if (enable) {
859 ret = pm_runtime_resume_and_get(dev: imx412->dev);
860 if (ret)
861 goto error_unlock;
862
863 ret = imx412_start_streaming(imx412);
864 if (ret)
865 goto error_power_off;
866 } else {
867 imx412_stop_streaming(imx412);
868 pm_runtime_put(dev: imx412->dev);
869 }
870
871 mutex_unlock(lock: &imx412->mutex);
872
873 return 0;
874
875error_power_off:
876 pm_runtime_put(dev: imx412->dev);
877error_unlock:
878 mutex_unlock(lock: &imx412->mutex);
879
880 return ret;
881}
882
883/**
884 * imx412_detect() - Detect imx412 sensor
885 * @imx412: pointer to imx412 device
886 *
887 * Return: 0 if successful, -EIO if sensor id does not match
888 */
889static int imx412_detect(struct imx412 *imx412)
890{
891 int ret;
892 u32 val;
893
894 ret = imx412_read_reg(imx412, IMX412_REG_ID, len: 2, val: &val);
895 if (ret)
896 return ret;
897
898 if (val != IMX412_ID) {
899 dev_err(imx412->dev, "chip id mismatch: %x!=%x",
900 IMX412_ID, val);
901 return -ENXIO;
902 }
903
904 return 0;
905}
906
907/**
908 * imx412_parse_hw_config() - Parse HW configuration and check if supported
909 * @imx412: pointer to imx412 device
910 *
911 * Return: 0 if successful, error code otherwise.
912 */
913static int imx412_parse_hw_config(struct imx412 *imx412)
914{
915 struct fwnode_handle *fwnode = dev_fwnode(imx412->dev);
916 struct v4l2_fwnode_endpoint bus_cfg = {
917 .bus_type = V4L2_MBUS_CSI2_DPHY
918 };
919 struct fwnode_handle *ep;
920 unsigned long rate;
921 unsigned int i;
922 int ret;
923
924 if (!fwnode)
925 return -ENXIO;
926
927 /* Request optional reset pin */
928 imx412->reset_gpio = devm_gpiod_get_optional(dev: imx412->dev, con_id: "reset",
929 flags: GPIOD_OUT_LOW);
930 if (IS_ERR(ptr: imx412->reset_gpio)) {
931 dev_err(imx412->dev, "failed to get reset gpio %ld",
932 PTR_ERR(imx412->reset_gpio));
933 return PTR_ERR(ptr: imx412->reset_gpio);
934 }
935
936 /* Get sensor input clock */
937 imx412->inclk = devm_clk_get(dev: imx412->dev, NULL);
938 if (IS_ERR(ptr: imx412->inclk)) {
939 dev_err(imx412->dev, "could not get inclk");
940 return PTR_ERR(ptr: imx412->inclk);
941 }
942
943 rate = clk_get_rate(clk: imx412->inclk);
944 if (rate != IMX412_INCLK_RATE) {
945 dev_err(imx412->dev, "inclk frequency mismatch");
946 return -EINVAL;
947 }
948
949 /* Get optional DT defined regulators */
950 for (i = 0; i < ARRAY_SIZE(imx412_supply_names); i++)
951 imx412->supplies[i].supply = imx412_supply_names[i];
952
953 ret = devm_regulator_bulk_get(dev: imx412->dev,
954 ARRAY_SIZE(imx412_supply_names),
955 consumers: imx412->supplies);
956 if (ret)
957 return ret;
958
959 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
960 if (!ep)
961 return -ENXIO;
962
963 ret = v4l2_fwnode_endpoint_alloc_parse(fwnode: ep, vep: &bus_cfg);
964 fwnode_handle_put(fwnode: ep);
965 if (ret)
966 return ret;
967
968 if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX412_NUM_DATA_LANES) {
969 dev_err(imx412->dev,
970 "number of CSI2 data lanes %d is not supported",
971 bus_cfg.bus.mipi_csi2.num_data_lanes);
972 ret = -EINVAL;
973 goto done_endpoint_free;
974 }
975
976 if (!bus_cfg.nr_of_link_frequencies) {
977 dev_err(imx412->dev, "no link frequencies defined");
978 ret = -EINVAL;
979 goto done_endpoint_free;
980 }
981
982 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
983 if (bus_cfg.link_frequencies[i] == IMX412_LINK_FREQ)
984 goto done_endpoint_free;
985
986 ret = -EINVAL;
987
988done_endpoint_free:
989 v4l2_fwnode_endpoint_free(vep: &bus_cfg);
990
991 return ret;
992}
993
994/* V4l2 subdevice ops */
995static const struct v4l2_subdev_video_ops imx412_video_ops = {
996 .s_stream = imx412_set_stream,
997};
998
999static const struct v4l2_subdev_pad_ops imx412_pad_ops = {
1000 .enum_mbus_code = imx412_enum_mbus_code,
1001 .enum_frame_size = imx412_enum_frame_size,
1002 .get_fmt = imx412_get_pad_format,
1003 .set_fmt = imx412_set_pad_format,
1004};
1005
1006static const struct v4l2_subdev_ops imx412_subdev_ops = {
1007 .video = &imx412_video_ops,
1008 .pad = &imx412_pad_ops,
1009};
1010
1011static const struct v4l2_subdev_internal_ops imx412_internal_ops = {
1012 .init_state = imx412_init_state,
1013};
1014
1015/**
1016 * imx412_power_on() - Sensor power on sequence
1017 * @dev: pointer to i2c device
1018 *
1019 * Return: 0 if successful, error code otherwise.
1020 */
1021static int imx412_power_on(struct device *dev)
1022{
1023 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1024 struct imx412 *imx412 = to_imx412(subdev: sd);
1025 int ret;
1026
1027 ret = regulator_bulk_enable(ARRAY_SIZE(imx412_supply_names),
1028 consumers: imx412->supplies);
1029 if (ret < 0) {
1030 dev_err(dev, "failed to enable regulators\n");
1031 return ret;
1032 }
1033
1034 gpiod_set_value_cansleep(desc: imx412->reset_gpio, value: 0);
1035
1036 ret = clk_prepare_enable(clk: imx412->inclk);
1037 if (ret) {
1038 dev_err(imx412->dev, "fail to enable inclk");
1039 goto error_reset;
1040 }
1041
1042 usleep_range(min: 1000, max: 1200);
1043
1044 return 0;
1045
1046error_reset:
1047 gpiod_set_value_cansleep(desc: imx412->reset_gpio, value: 1);
1048 regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
1049 consumers: imx412->supplies);
1050
1051 return ret;
1052}
1053
1054/**
1055 * imx412_power_off() - Sensor power off sequence
1056 * @dev: pointer to i2c device
1057 *
1058 * Return: 0 if successful, error code otherwise.
1059 */
1060static int imx412_power_off(struct device *dev)
1061{
1062 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1063 struct imx412 *imx412 = to_imx412(subdev: sd);
1064
1065 clk_disable_unprepare(clk: imx412->inclk);
1066
1067 gpiod_set_value_cansleep(desc: imx412->reset_gpio, value: 1);
1068
1069 regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
1070 consumers: imx412->supplies);
1071
1072 return 0;
1073}
1074
1075/**
1076 * imx412_init_controls() - Initialize sensor subdevice controls
1077 * @imx412: pointer to imx412 device
1078 *
1079 * Return: 0 if successful, error code otherwise.
1080 */
1081static int imx412_init_controls(struct imx412 *imx412)
1082{
1083 struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler;
1084 const struct imx412_mode *mode = imx412->cur_mode;
1085 u32 lpfr;
1086 int ret;
1087
1088 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
1089 if (ret)
1090 return ret;
1091
1092 /* Serialize controls with sensor device */
1093 ctrl_hdlr->lock = &imx412->mutex;
1094
1095 /* Initialize exposure and gain */
1096 lpfr = mode->vblank + mode->height;
1097 imx412->exp_ctrl = v4l2_ctrl_new_std(hdl: ctrl_hdlr,
1098 ops: &imx412_ctrl_ops,
1099 V4L2_CID_EXPOSURE,
1100 IMX412_EXPOSURE_MIN,
1101 max: lpfr - IMX412_EXPOSURE_OFFSET,
1102 IMX412_EXPOSURE_STEP,
1103 IMX412_EXPOSURE_DEFAULT);
1104
1105 imx412->again_ctrl = v4l2_ctrl_new_std(hdl: ctrl_hdlr,
1106 ops: &imx412_ctrl_ops,
1107 V4L2_CID_ANALOGUE_GAIN,
1108 IMX412_AGAIN_MIN,
1109 IMX412_AGAIN_MAX,
1110 IMX412_AGAIN_STEP,
1111 IMX412_AGAIN_DEFAULT);
1112
1113 v4l2_ctrl_cluster(ncontrols: 2, controls: &imx412->exp_ctrl);
1114
1115 imx412->vblank_ctrl = v4l2_ctrl_new_std(hdl: ctrl_hdlr,
1116 ops: &imx412_ctrl_ops,
1117 V4L2_CID_VBLANK,
1118 min: mode->vblank_min,
1119 max: mode->vblank_max,
1120 step: 1, def: mode->vblank);
1121
1122 /* Read only controls */
1123 imx412->pclk_ctrl = v4l2_ctrl_new_std(hdl: ctrl_hdlr,
1124 ops: &imx412_ctrl_ops,
1125 V4L2_CID_PIXEL_RATE,
1126 min: mode->pclk, max: mode->pclk,
1127 step: 1, def: mode->pclk);
1128
1129 imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(hdl: ctrl_hdlr,
1130 ops: &imx412_ctrl_ops,
1131 V4L2_CID_LINK_FREQ,
1132 ARRAY_SIZE(link_freq) -
1133 1,
1134 def: mode->link_freq_idx,
1135 qmenu_int: link_freq);
1136 if (imx412->link_freq_ctrl)
1137 imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1138
1139 imx412->hblank_ctrl = v4l2_ctrl_new_std(hdl: ctrl_hdlr,
1140 ops: &imx412_ctrl_ops,
1141 V4L2_CID_HBLANK,
1142 IMX412_REG_MIN,
1143 IMX412_REG_MAX,
1144 step: 1, def: mode->hblank);
1145 if (imx412->hblank_ctrl)
1146 imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1147
1148 if (ctrl_hdlr->error) {
1149 dev_err(imx412->dev, "control init failed: %d",
1150 ctrl_hdlr->error);
1151 v4l2_ctrl_handler_free(hdl: ctrl_hdlr);
1152 return ctrl_hdlr->error;
1153 }
1154
1155 imx412->sd.ctrl_handler = ctrl_hdlr;
1156
1157 return 0;
1158}
1159
1160/**
1161 * imx412_probe() - I2C client device binding
1162 * @client: pointer to i2c client device
1163 *
1164 * Return: 0 if successful, error code otherwise.
1165 */
1166static int imx412_probe(struct i2c_client *client)
1167{
1168 struct imx412 *imx412;
1169 const char *name;
1170 int ret;
1171
1172 imx412 = devm_kzalloc(dev: &client->dev, size: sizeof(*imx412), GFP_KERNEL);
1173 if (!imx412)
1174 return -ENOMEM;
1175
1176 imx412->dev = &client->dev;
1177 name = device_get_match_data(dev: &client->dev);
1178 if (!name)
1179 return -ENODEV;
1180
1181 /* Initialize subdev */
1182 v4l2_i2c_subdev_init(sd: &imx412->sd, client, ops: &imx412_subdev_ops);
1183 imx412->sd.internal_ops = &imx412_internal_ops;
1184
1185 ret = imx412_parse_hw_config(imx412);
1186 if (ret) {
1187 dev_err(imx412->dev, "HW configuration is not supported");
1188 return ret;
1189 }
1190
1191 mutex_init(&imx412->mutex);
1192
1193 ret = imx412_power_on(dev: imx412->dev);
1194 if (ret) {
1195 dev_err(imx412->dev, "failed to power-on the sensor");
1196 goto error_mutex_destroy;
1197 }
1198
1199 /* Check module identity */
1200 ret = imx412_detect(imx412);
1201 if (ret) {
1202 dev_err(imx412->dev, "failed to find sensor: %d", ret);
1203 goto error_power_off;
1204 }
1205
1206 /* Set default mode to max resolution */
1207 imx412->cur_mode = &supported_mode;
1208 imx412->vblank = imx412->cur_mode->vblank;
1209
1210 ret = imx412_init_controls(imx412);
1211 if (ret) {
1212 dev_err(imx412->dev, "failed to init controls: %d", ret);
1213 goto error_power_off;
1214 }
1215
1216 /* Initialize subdev */
1217 imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1218 imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1219
1220 v4l2_i2c_subdev_set_name(sd: &imx412->sd, client, devname: name, NULL);
1221
1222 /* Initialize source pad */
1223 imx412->pad.flags = MEDIA_PAD_FL_SOURCE;
1224 ret = media_entity_pads_init(entity: &imx412->sd.entity, num_pads: 1, pads: &imx412->pad);
1225 if (ret) {
1226 dev_err(imx412->dev, "failed to init entity pads: %d", ret);
1227 goto error_handler_free;
1228 }
1229
1230 ret = v4l2_async_register_subdev_sensor(sd: &imx412->sd);
1231 if (ret < 0) {
1232 dev_err(imx412->dev,
1233 "failed to register async subdev: %d", ret);
1234 goto error_media_entity;
1235 }
1236
1237 pm_runtime_set_active(dev: imx412->dev);
1238 pm_runtime_enable(dev: imx412->dev);
1239 pm_runtime_idle(dev: imx412->dev);
1240
1241 return 0;
1242
1243error_media_entity:
1244 media_entity_cleanup(entity: &imx412->sd.entity);
1245error_handler_free:
1246 v4l2_ctrl_handler_free(hdl: imx412->sd.ctrl_handler);
1247error_power_off:
1248 imx412_power_off(dev: imx412->dev);
1249error_mutex_destroy:
1250 mutex_destroy(lock: &imx412->mutex);
1251
1252 return ret;
1253}
1254
1255/**
1256 * imx412_remove() - I2C client device unbinding
1257 * @client: pointer to I2C client device
1258 *
1259 * Return: 0 if successful, error code otherwise.
1260 */
1261static void imx412_remove(struct i2c_client *client)
1262{
1263 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1264 struct imx412 *imx412 = to_imx412(subdev: sd);
1265
1266 v4l2_async_unregister_subdev(sd);
1267 media_entity_cleanup(entity: &sd->entity);
1268 v4l2_ctrl_handler_free(hdl: sd->ctrl_handler);
1269
1270 pm_runtime_disable(dev: &client->dev);
1271 if (!pm_runtime_status_suspended(dev: &client->dev))
1272 imx412_power_off(dev: &client->dev);
1273 pm_runtime_set_suspended(dev: &client->dev);
1274
1275 mutex_destroy(lock: &imx412->mutex);
1276}
1277
1278static const struct dev_pm_ops imx412_pm_ops = {
1279 SET_RUNTIME_PM_OPS(imx412_power_off, imx412_power_on, NULL)
1280};
1281
1282static const struct of_device_id imx412_of_match[] = {
1283 { .compatible = "sony,imx412", .data = "imx412" },
1284 { .compatible = "sony,imx577", .data = "imx577" },
1285 { }
1286};
1287
1288MODULE_DEVICE_TABLE(of, imx412_of_match);
1289
1290static struct i2c_driver imx412_driver = {
1291 .probe = imx412_probe,
1292 .remove = imx412_remove,
1293 .driver = {
1294 .name = "imx412",
1295 .pm = &imx412_pm_ops,
1296 .of_match_table = imx412_of_match,
1297 },
1298};
1299
1300module_i2c_driver(imx412_driver);
1301
1302MODULE_DESCRIPTION("Sony imx412 sensor driver");
1303MODULE_LICENSE("GPL");
1304

source code of linux/drivers/media/i2c/imx412.c