1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 THine Electronics, Inc.
4 * Copyright (C) 2023 Ideas on Board Oy
7 #include <linux/unaligned.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/firmware.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mtd/spi-nor.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/property.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #include <media/v4l2-async.h>
27 #include <media/v4l2-cci.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fwnode.h>
31 #include <media/v4l2-subdev.h>
33 #include <uapi/linux/thp7312.h>
37 #define THP7312_REG_FIRMWARE_VERSION_1 CCI_REG8(0xf000)
38 #define THP7312_REG_CAMERA_STATUS CCI_REG8(0xf001)
39 #define THP7312_REG_FIRMWARE_VERSION_2 CCI_REG8(0xf005)
40 #define THP7312_REG_SET_OUTPUT_ENABLE CCI_REG8(0xf008)
41 #define THP7312_OUTPUT_ENABLE 0x01
42 #define THP7312_OUTPUT_DISABLE 0x00
43 #define THP7312_REG_SET_OUTPUT_COLOR_COMPRESSION CCI_REG8(0xf009)
44 #define THP7312_REG_SET_OUTPUT_COLOR_UYVY 0x00
45 #define THP7312_REG_SET_OUTPUT_COLOR_YUY2 0x04
46 #define THP7312_REG_FLIP_MIRROR CCI_REG8(0xf00c)
47 #define THP7312_REG_FLIP_MIRROR_FLIP BIT(0)
48 #define THP7312_REG_FLIP_MIRROR_MIRROR BIT(1)
49 #define THP7312_REG_VIDEO_IMAGE_SIZE CCI_REG8(0xf00d)
50 #define THP7312_VIDEO_IMAGE_SIZE_640x360 0x52
51 #define THP7312_VIDEO_IMAGE_SIZE_640x460 0x03
52 #define THP7312_VIDEO_IMAGE_SIZE_1280x720 0x0a
53 #define THP7312_VIDEO_IMAGE_SIZE_1920x1080 0x0b
54 #define THP7312_VIDEO_IMAGE_SIZE_3840x2160 0x0d
55 #define THP7312_VIDEO_IMAGE_SIZE_4160x3120 0x14
56 #define THP7312_VIDEO_IMAGE_SIZE_2016x1512 0x20
57 #define THP7312_VIDEO_IMAGE_SIZE_2048x1536 0x21
58 #define THP7312_REG_VIDEO_FRAME_RATE_MODE CCI_REG8(0xf00f)
59 #define THP7312_VIDEO_FRAME_RATE_MODE1 0x80
60 #define THP7312_VIDEO_FRAME_RATE_MODE2 0x81
61 #define THP7312_VIDEO_FRAME_RATE_MODE3 0x82
62 #define THP7312_REG_SET_DRIVING_MODE CCI_REG8(0xf010)
63 #define THP7312_REG_DRIVING_MODE_STATUS CCI_REG8(0xf011)
64 #define THP7312_REG_JPEG_COMPRESSION_FACTOR CCI_REG8(0xf01b)
65 #define THP7312_REG_AE_EXPOSURE_COMPENSATION CCI_REG8(0xf022)
66 #define THP7312_REG_AE_FLICKER_MODE CCI_REG8(0xf023)
67 #define THP7312_AE_FLICKER_MODE_50 0x00
68 #define THP7312_AE_FLICKER_MODE_60 0x01
69 #define THP7312_AE_FLICKER_MODE_DISABLE 0x80
70 #define THP7312_REG_AE_FIX_FRAME_RATE CCI_REG8(0xf02e)
71 #define THP7312_REG_MANUAL_WB_RED_GAIN CCI_REG8(0xf036)
72 #define THP7312_REG_MANUAL_WB_BLUE_GAIN CCI_REG8(0xf037)
73 #define THP7312_REG_WB_MODE CCI_REG8(0xf039)
74 #define THP7312_WB_MODE_AUTO 0x00
75 #define THP7312_WB_MODE_MANUAL 0x11
76 #define THP7312_REG_MANUAL_FOCUS_POSITION CCI_REG16(0xf03c)
77 #define THP7312_REG_AF_CONTROL CCI_REG8(0xf040)
78 #define THP7312_REG_AF_CONTROL_AF 0x01
79 #define THP7312_REG_AF_CONTROL_MANUAL 0x10
80 #define THP7312_REG_AF_CONTROL_LOCK 0x80
81 #define THP7312_REG_AF_SETTING CCI_REG8(0xf041)
82 #define THP7312_REG_AF_SETTING_ONESHOT_CONTRAST 0x00
83 #define THP7312_REG_AF_SETTING_ONESHOT_PDAF 0x40
84 #define THP7312_REG_AF_SETTING_ONESHOT_HYBRID 0x80
85 #define THP7312_REG_AF_SETTING_CONTINUOUS_CONTRAST 0x30
86 #define THP7312_REG_AF_SETTING_CONTINUOUS_PDAF 0x70
87 #define THP7312_REG_AF_SETTING_CONTINUOUS_HYBRID 0xf0
88 #define THP7312_REG_AF_SUPPORT CCI_REG8(0xf043)
89 #define THP7312_AF_SUPPORT_PDAF BIT(1)
90 #define THP7312_AF_SUPPORT_CONTRAST BIT(0)
91 #define THP7312_REG_SATURATION CCI_REG8(0xf052)
92 #define THP7312_REG_SHARPNESS CCI_REG8(0xf053)
93 #define THP7312_REG_BRIGHTNESS CCI_REG8(0xf056)
94 #define THP7312_REG_CONTRAST CCI_REG8(0xf057)
95 #define THP7312_REG_NOISE_REDUCTION CCI_REG8(0xf059)
96 #define THP7312_REG_NOISE_REDUCTION_FIXED BIT(7)
98 #define TH7312_REG_CUSTOM_MIPI_SET CCI_REG8(0xf0f6)
99 #define TH7312_REG_CUSTOM_MIPI_STATUS CCI_REG8(0xf0f7)
100 #define TH7312_REG_CUSTOM_MIPI_RD CCI_REG8(0xf0f8)
101 #define TH7312_REG_CUSTOM_MIPI_TD CCI_REG8(0xf0f9)
104 * Firmware update registers. Those use a different address space than the
105 * normal operation ISP registers.
108 #define THP7312_REG_FW_DRIVABILITY CCI_REG32(0xd65c)
109 #define THP7312_REG_FW_DEST_BANK_ADDR CCI_REG32(0xff08)
110 #define THP7312_REG_FW_VERIFY_RESULT CCI_REG8(0xff60)
111 #define THP7312_REG_FW_RESET_FLASH CCI_REG8(0xff61)
112 #define THP7312_REG_FW_MEMORY_IO_SETTING CCI_REG8(0xff62)
113 #define THP7312_FW_MEMORY_IO_GPIO0 1
114 #define THP7312_FW_MEMORY_IO_GPIO1 0
115 #define THP7312_REG_FW_CRC_RESULT CCI_REG32(0xff64)
116 #define THP7312_REG_FW_STATUS CCI_REG8(0xfffc)
118 #define THP7312_FW_VERSION(major, minor) (((major) << 8) | (minor))
119 #define THP7312_FW_VERSION_MAJOR(v) ((v) >> 8)
120 #define THP7312_FW_VERSION_MINOR(v) ((v) & 0xff)
122 enum thp7312_focus_method
{
123 THP7312_FOCUS_METHOD_CONTRAST
,
124 THP7312_FOCUS_METHOD_PDAF
,
125 THP7312_FOCUS_METHOD_HYBRID
,
129 * enum thp7312_focus_state - State of the focus handler
131 * @THP7312_FOCUS_STATE_MANUAL: Manual focus, controlled through the
132 * V4L2_CID_FOCUS_ABSOLUTE control
133 * @THP7312_FOCUS_STATE_AUTO: Continuous auto-focus
134 * @THP7312_FOCUS_STATE_LOCKED: Lock the focus to a fixed position. This state
135 * is entered when switching from auto to manual mode.
136 * @THP7312_FOCUS_STATE_ONESHOT: One-shot auto-focus
138 * Valid transitions are as follow:
141 * node [shape=circle];
143 * manual [label="MANUAL"];
144 * auto [label="AUTO"];
145 * locked [label="LOCKED"];
146 * oneshot [label="ONESHOT"];
148 * manual -> auto [label="FOCUS_AUTO <- true"]
149 * locked -> auto [label="FOCUS_AUTO <- true"]
150 * oneshot -> auto [label="FOCUS_AUTO <- true"]
151 * auto -> locked [label="FOCUS_AUTO <- false"]
153 * locked -> manual [label="FOCUS_ABSOLUTE <- *"]
154 * oneshot -> manual [label="FOCUS_ABSOLUTE <- *"]
156 * manual -> oneshot [label="FOCUS_START <- *"]
157 * locked -> oneshot [label="FOCUS_START <- *"]
160 enum thp7312_focus_state
{
161 THP7312_FOCUS_STATE_MANUAL
,
162 THP7312_FOCUS_STATE_AUTO
,
163 THP7312_FOCUS_STATE_LOCKED
,
164 THP7312_FOCUS_STATE_ONESHOT
,
167 enum thp7312_boot_mode
{
168 THP7312_BOOT_MODE_2WIRE_SLAVE
= 0,
169 THP7312_BOOT_MODE_SPI_MASTER
= 1,
172 struct thp7312_frame_rate
{
175 u8 reg_frame_rate_mode
;
178 struct thp7312_mode_info
{
182 const struct thp7312_frame_rate
*rates
;
185 static const u32 thp7312_colour_fmts
[] = {
186 MEDIA_BUS_FMT_YUYV8_1X16
,
189 /* regulator supplies */
190 static const char * const thp7312_supply_name
[] = {
200 static const struct thp7312_mode_info thp7312_mode_info_data
[] = {
204 .reg_image_size
= THP7312_VIDEO_IMAGE_SIZE_1920x1080
,
205 .rates
= (const struct thp7312_frame_rate
[]) {
206 { 30, 300000000, 0x81 },
207 { 60, 387500000, 0x82 },
213 .reg_image_size
= THP7312_VIDEO_IMAGE_SIZE_2048x1536
,
214 .rates
= (const struct thp7312_frame_rate
[]) {
215 { 30, 300000000, 0x81 },
221 .reg_image_size
= THP7312_VIDEO_IMAGE_SIZE_3840x2160
,
222 .rates
= (const struct thp7312_frame_rate
[]) {
223 { 30, 600000000, 0x81 },
229 .reg_image_size
= THP7312_VIDEO_IMAGE_SIZE_4160x3120
,
230 .rates
= (const struct thp7312_frame_rate
[]) {
231 { 20, 600000000, 0x81 },
237 struct thp7312_device
;
239 struct thp7312_sensor_info
{
243 struct thp7312_sensor
{
244 const struct thp7312_sensor_info
*info
;
248 struct thp7312_device
{
250 struct regmap
*regmap
;
252 struct v4l2_subdev sd
;
253 struct media_pad pad
;
255 struct gpio_desc
*reset_gpio
;
256 struct regulator_bulk_data supplies
[ARRAY_SIZE(thp7312_supply_name
)];
261 struct thp7312_sensor sensors
[1];
263 enum thp7312_boot_mode boot_mode
;
265 struct v4l2_ctrl_handler ctrl_handler
;
271 struct v4l2_ctrl
*hflip
;
272 struct v4l2_ctrl
*vflip
;
276 struct v4l2_ctrl
*focus_auto
;
277 struct v4l2_ctrl
*focus_absolute
;
278 struct v4l2_ctrl
*focus_start
;
279 struct v4l2_ctrl
*focus_method
;
282 enum thp7312_focus_state focus_state
;
285 struct v4l2_ctrl
*noise_reduction_auto
;
286 struct v4l2_ctrl
*noise_reduction_absolute
;
289 /* Lock to protect fw_cancel */
290 struct mutex fw_lock
;
291 struct fw_upload
*fwl
;
298 static const struct thp7312_sensor_info thp7312_sensor_info
[] = {
300 .model
= "sony,imx258",
304 static inline struct thp7312_device
*to_thp7312_dev(struct v4l2_subdev
*sd
)
306 return container_of(sd
, struct thp7312_device
, sd
);
309 static const struct thp7312_mode_info
*
310 thp7312_find_mode(unsigned int width
, unsigned int height
, bool nearest
)
312 const struct thp7312_mode_info
*mode
;
314 mode
= v4l2_find_nearest_size(thp7312_mode_info_data
,
315 ARRAY_SIZE(thp7312_mode_info_data
),
316 width
, height
, width
, height
);
318 if (!nearest
&& (mode
->width
!= width
|| mode
->height
!= height
))
324 static const struct thp7312_frame_rate
*
325 thp7312_find_rate(const struct thp7312_mode_info
*mode
, unsigned int fps
,
328 const struct thp7312_frame_rate
*best_rate
= NULL
;
329 const struct thp7312_frame_rate
*rate
;
330 unsigned int best_delta
= UINT_MAX
;
335 for (rate
= mode
->rates
; rate
->fps
&& best_delta
; ++rate
) {
336 unsigned int delta
= abs(rate
->fps
- fps
);
338 if (delta
<= best_delta
) {
344 if (!nearest
&& best_delta
)
350 /* -----------------------------------------------------------------------------
351 * Device Access & Configuration
354 #define thp7312_read_poll_timeout(dev, addr, val, cond, sleep_us, timeout_us) \
357 __ret = read_poll_timeout(cci_read, __err, __err || (cond), sleep_us, \
358 timeout_us, false, (dev)->regmap, addr, \
363 static int thp7312_map_data_lanes(u8
*lane_remap
, const u8
*lanes
, u8 num_lanes
)
370 * The value that we write to the register is the index in the
371 * data-lanes array, so we need to do a conversion. Do this in the same
372 * pass as validating data-lanes.
374 for (i
= 0; i
< num_lanes
; i
++) {
375 if (lanes
[i
] < 1 || lanes
[i
] > 4)
378 if (used_lanes
& (BIT(lanes
[i
])))
381 used_lanes
|= BIT(lanes
[i
]);
384 * data-lanes is 1-indexed while the field position in the
385 * register is 0-indexed.
387 val
|= i
<< ((lanes
[i
] - 1) * 2);
395 static int thp7312_set_mipi_lanes(struct thp7312_device
*thp7312
)
397 struct device
*dev
= thp7312
->dev
;
401 cci_write(thp7312
->regmap
, TH7312_REG_CUSTOM_MIPI_RD
,
402 thp7312
->sensors
[0].lane_remap
, &ret
);
403 cci_write(thp7312
->regmap
, TH7312_REG_CUSTOM_MIPI_TD
,
404 thp7312
->lane_remap
, &ret
);
405 cci_write(thp7312
->regmap
, TH7312_REG_CUSTOM_MIPI_SET
, 1, &ret
);
410 ret
= thp7312_read_poll_timeout(thp7312
, TH7312_REG_CUSTOM_MIPI_STATUS
,
411 val
, val
== 0x00, 100000, 2000000);
413 dev_err(dev
, "Failed to poll MIPI lane status: %d\n", ret
);
420 static int thp7312_change_mode(struct thp7312_device
*thp7312
,
421 const struct thp7312_mode_info
*mode
,
422 const struct thp7312_frame_rate
*rate
)
424 struct device
*dev
= thp7312
->dev
;
428 ret
= thp7312_read_poll_timeout(thp7312
, THP7312_REG_CAMERA_STATUS
, val
,
429 val
== 0x80, 20000, 200000);
431 dev_err(dev
, "%s(): failed to poll ISP: %d\n", __func__
, ret
);
435 cci_write(thp7312
->regmap
, THP7312_REG_VIDEO_IMAGE_SIZE
,
436 mode
->reg_image_size
, &ret
);
437 cci_write(thp7312
->regmap
, THP7312_REG_VIDEO_FRAME_RATE_MODE
,
438 rate
->reg_frame_rate_mode
, &ret
);
439 cci_write(thp7312
->regmap
, THP7312_REG_JPEG_COMPRESSION_FACTOR
, 0x5e,
441 cci_write(thp7312
->regmap
, THP7312_REG_SET_DRIVING_MODE
, 0x01, &ret
);
446 ret
= thp7312_read_poll_timeout(thp7312
, THP7312_REG_DRIVING_MODE_STATUS
,
447 val
, val
== 0x01, 20000, 100000);
449 dev_err(dev
, "%s(): failed\n", __func__
);
456 static int thp7312_set_framefmt(struct thp7312_device
*thp7312
,
457 struct v4l2_mbus_framefmt
*format
)
461 switch (format
->code
) {
462 case MEDIA_BUS_FMT_UYVY8_1X16
:
464 val
= THP7312_REG_SET_OUTPUT_COLOR_UYVY
;
466 case MEDIA_BUS_FMT_YUYV8_1X16
:
468 val
= THP7312_REG_SET_OUTPUT_COLOR_YUY2
;
471 /* Should never happen */
475 return cci_write(thp7312
->regmap
,
476 THP7312_REG_SET_OUTPUT_COLOR_COMPRESSION
, val
, NULL
);
479 static int thp7312_init_mode(struct thp7312_device
*thp7312
,
480 struct v4l2_subdev_state
*sd_state
)
482 const struct thp7312_mode_info
*mode
;
483 const struct thp7312_frame_rate
*rate
;
484 struct v4l2_mbus_framefmt
*fmt
;
485 struct v4l2_fract
*interval
;
489 * TODO: The mode and rate should be cached in the subdev state, once
490 * support for extending states will be available.
492 fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
493 interval
= v4l2_subdev_state_get_interval(sd_state
, 0);
495 mode
= thp7312_find_mode(fmt
->width
, fmt
->height
, false);
496 rate
= thp7312_find_rate(mode
, interval
->denominator
, false);
498 if (WARN_ON(!mode
|| !rate
))
501 ret
= thp7312_set_framefmt(thp7312
, fmt
);
505 return thp7312_change_mode(thp7312
, mode
, rate
);
508 static int thp7312_stream_enable(struct thp7312_device
*thp7312
, bool enable
)
510 return cci_write(thp7312
->regmap
, THP7312_REG_SET_OUTPUT_ENABLE
,
511 enable
? THP7312_OUTPUT_ENABLE
: THP7312_OUTPUT_DISABLE
,
515 static int thp7312_check_status_stream_mode(struct thp7312_device
*thp7312
)
517 struct device
*dev
= thp7312
->dev
;
521 while (status
!= 0x80) {
522 ret
= cci_read(thp7312
->regmap
, THP7312_REG_CAMERA_STATUS
,
527 if (status
== 0x80) {
528 dev_dbg(dev
, "Camera initialization done\n");
532 if (status
!= 0x00) {
533 dev_err(dev
, "Invalid camera status %llx\n", status
);
537 dev_dbg(dev
, "Camera initializing...\n");
538 usleep_range(70000, 80000);
544 static void thp7312_reset(struct thp7312_device
*thp7312
)
548 gpiod_set_value_cansleep(thp7312
->reset_gpio
, 1);
551 * The minimum reset duration is 8 clock cycles, make it 10 to provide
554 rate
= clk_get_rate(thp7312
->iclk
);
555 fsleep(DIV_ROUND_UP(10 * USEC_PER_SEC
, rate
));
557 gpiod_set_value_cansleep(thp7312
->reset_gpio
, 0);
560 * TODO: The documentation states that the device needs 2ms to
561 * initialize after reset is deasserted. It then proceeds to load the
562 * firmware from the flash memory, which takes an unspecified amount of
563 * time. Check if this delay could be reduced.
568 /* -----------------------------------------------------------------------------
572 static void __thp7312_power_off(struct thp7312_device
*thp7312
)
574 regulator_bulk_disable(ARRAY_SIZE(thp7312
->supplies
), thp7312
->supplies
);
575 clk_disable_unprepare(thp7312
->iclk
);
578 static void thp7312_power_off(struct thp7312_device
*thp7312
)
580 __thp7312_power_off(thp7312
);
583 static int __thp7312_power_on(struct thp7312_device
*thp7312
)
585 struct device
*dev
= thp7312
->dev
;
588 ret
= regulator_bulk_enable(ARRAY_SIZE(thp7312
->supplies
),
593 ret
= clk_prepare_enable(thp7312
->iclk
);
595 dev_err(dev
, "clk prepare enable failed\n");
596 regulator_bulk_disable(ARRAY_SIZE(thp7312
->supplies
),
602 * We cannot assume that turning off and on again will reset, so do a
603 * software reset on power up.
605 thp7312_reset(thp7312
);
610 static int thp7312_power_on(struct thp7312_device
*thp7312
)
614 ret
= __thp7312_power_on(thp7312
);
618 ret
= thp7312_check_status_stream_mode(thp7312
);
622 ret
= thp7312_set_mipi_lanes(thp7312
);
629 thp7312_power_off(thp7312
);
633 static int __maybe_unused
thp7312_pm_runtime_suspend(struct device
*dev
)
635 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
636 struct thp7312_device
*thp7312
= to_thp7312_dev(sd
);
638 thp7312_power_off(thp7312
);
640 thp7312
->ctrls_applied
= false;
645 static int __maybe_unused
thp7312_pm_runtime_resume(struct device
*dev
)
647 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
648 struct thp7312_device
*thp7312
= to_thp7312_dev(sd
);
650 return thp7312_power_on(thp7312
);
653 static const struct dev_pm_ops thp7312_pm_ops
= {
654 SET_RUNTIME_PM_OPS(thp7312_pm_runtime_suspend
,
655 thp7312_pm_runtime_resume
, NULL
)
658 /* -----------------------------------------------------------------------------
659 * V4L2 Subdev Operations
662 static bool thp7312_find_bus_code(u32 code
)
666 for (i
= 0; i
< ARRAY_SIZE(thp7312_colour_fmts
); ++i
) {
667 if (thp7312_colour_fmts
[i
] == code
)
674 static int thp7312_enum_mbus_code(struct v4l2_subdev
*sd
,
675 struct v4l2_subdev_state
*sd_state
,
676 struct v4l2_subdev_mbus_code_enum
*code
)
678 if (code
->index
>= ARRAY_SIZE(thp7312_colour_fmts
))
681 code
->code
= thp7312_colour_fmts
[code
->index
];
686 static int thp7312_enum_frame_size(struct v4l2_subdev
*sd
,
687 struct v4l2_subdev_state
*sd_state
,
688 struct v4l2_subdev_frame_size_enum
*fse
)
690 if (!thp7312_find_bus_code(fse
->code
))
693 if (fse
->index
>= ARRAY_SIZE(thp7312_mode_info_data
))
696 fse
->min_width
= thp7312_mode_info_data
[fse
->index
].width
;
697 fse
->max_width
= fse
->min_width
;
698 fse
->min_height
= thp7312_mode_info_data
[fse
->index
].height
;
699 fse
->max_height
= fse
->min_height
;
704 static int thp7312_enum_frame_interval(struct v4l2_subdev
*sd
,
705 struct v4l2_subdev_state
*sd_state
,
706 struct v4l2_subdev_frame_interval_enum
*fie
)
708 const struct thp7312_frame_rate
*rate
;
709 const struct thp7312_mode_info
*mode
;
710 unsigned int index
= fie
->index
;
712 if (!thp7312_find_bus_code(fie
->code
))
715 mode
= thp7312_find_mode(fie
->width
, fie
->height
, false);
719 for (rate
= mode
->rates
; rate
->fps
; ++rate
, --index
) {
721 fie
->interval
.numerator
= 1;
722 fie
->interval
.denominator
= rate
->fps
;
731 static int thp7312_set_fmt(struct v4l2_subdev
*sd
,
732 struct v4l2_subdev_state
*sd_state
,
733 struct v4l2_subdev_format
*format
)
735 struct thp7312_device
*thp7312
= to_thp7312_dev(sd
);
736 struct v4l2_mbus_framefmt
*mbus_fmt
= &format
->format
;
737 struct v4l2_mbus_framefmt
*fmt
;
738 struct v4l2_fract
*interval
;
739 const struct thp7312_mode_info
*mode
;
741 if (!thp7312_find_bus_code(mbus_fmt
->code
))
742 mbus_fmt
->code
= thp7312_colour_fmts
[0];
744 mode
= thp7312_find_mode(mbus_fmt
->width
, mbus_fmt
->height
, true);
746 fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
748 fmt
->code
= mbus_fmt
->code
;
749 fmt
->width
= mode
->width
;
750 fmt
->height
= mode
->height
;
751 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
752 fmt
->ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(fmt
->colorspace
);
753 fmt
->quantization
= V4L2_QUANTIZATION_FULL_RANGE
;
754 fmt
->xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(fmt
->colorspace
);
758 interval
= v4l2_subdev_state_get_interval(sd_state
, 0);
759 interval
->numerator
= 1;
760 interval
->denominator
= mode
->rates
[0].fps
;
762 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
763 thp7312
->link_freq
= mode
->rates
[0].link_freq
;
768 static int thp7312_set_frame_interval(struct v4l2_subdev
*sd
,
769 struct v4l2_subdev_state
*sd_state
,
770 struct v4l2_subdev_frame_interval
*fi
)
772 struct thp7312_device
*thp7312
= to_thp7312_dev(sd
);
773 const struct thp7312_mode_info
*mode
;
774 const struct thp7312_frame_rate
*rate
;
775 const struct v4l2_mbus_framefmt
*fmt
;
776 struct v4l2_fract
*interval
;
779 /* Avoid divisions by 0, pick the highest frame if the interval is 0. */
780 fps
= fi
->interval
.numerator
781 ? DIV_ROUND_CLOSEST(fi
->interval
.denominator
, fi
->interval
.numerator
)
784 fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
785 mode
= thp7312_find_mode(fmt
->width
, fmt
->height
, false);
786 rate
= thp7312_find_rate(mode
, fps
, true);
788 interval
= v4l2_subdev_state_get_interval(sd_state
, 0);
789 interval
->numerator
= 1;
790 interval
->denominator
= rate
->fps
;
792 if (fi
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
793 thp7312
->link_freq
= rate
->link_freq
;
795 fi
->interval
= *interval
;
800 static int thp7312_s_stream(struct v4l2_subdev
*sd
, int enable
)
802 struct thp7312_device
*thp7312
= to_thp7312_dev(sd
);
803 struct v4l2_subdev_state
*sd_state
;
806 sd_state
= v4l2_subdev_lock_and_get_active_state(sd
);
809 thp7312_stream_enable(thp7312
, false);
811 pm_runtime_mark_last_busy(thp7312
->dev
);
812 pm_runtime_put_autosuspend(thp7312
->dev
);
814 v4l2_subdev_unlock_state(sd_state
);
819 ret
= pm_runtime_resume_and_get(thp7312
->dev
);
823 ret
= thp7312_init_mode(thp7312
, sd_state
);
827 if (!thp7312
->ctrls_applied
) {
828 ret
= __v4l2_ctrl_handler_setup(&thp7312
->ctrl_handler
);
832 thp7312
->ctrls_applied
= true;
835 ret
= thp7312_stream_enable(thp7312
, true);
842 pm_runtime_mark_last_busy(thp7312
->dev
);
843 pm_runtime_put_autosuspend(thp7312
->dev
);
845 v4l2_subdev_unlock_state(sd_state
);
850 static int thp7312_init_state(struct v4l2_subdev
*sd
,
851 struct v4l2_subdev_state
*sd_state
)
853 const struct thp7312_mode_info
*default_mode
= &thp7312_mode_info_data
[0];
854 struct v4l2_mbus_framefmt
*fmt
;
855 struct v4l2_fract
*interval
;
857 fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
858 interval
= v4l2_subdev_state_get_interval(sd_state
, 0);
861 * default init sequence initialize thp7312 to
862 * YUV422 YUYV VGA@30fps
864 fmt
->code
= MEDIA_BUS_FMT_YUYV8_1X16
;
865 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
866 fmt
->ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(fmt
->colorspace
);
867 fmt
->quantization
= V4L2_QUANTIZATION_FULL_RANGE
;
868 fmt
->xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(fmt
->colorspace
);
869 fmt
->width
= default_mode
->width
;
870 fmt
->height
= default_mode
->height
;
871 fmt
->field
= V4L2_FIELD_NONE
;
873 interval
->numerator
= 1;
874 interval
->denominator
= default_mode
->rates
[0].fps
;
879 static const struct v4l2_subdev_core_ops thp7312_core_ops
= {
880 .log_status
= v4l2_ctrl_subdev_log_status
,
883 static const struct v4l2_subdev_video_ops thp7312_video_ops
= {
884 .s_stream
= thp7312_s_stream
,
887 static const struct v4l2_subdev_pad_ops thp7312_pad_ops
= {
888 .enum_mbus_code
= thp7312_enum_mbus_code
,
889 .get_fmt
= v4l2_subdev_get_fmt
,
890 .set_fmt
= thp7312_set_fmt
,
891 .get_frame_interval
= v4l2_subdev_get_frame_interval
,
892 .set_frame_interval
= thp7312_set_frame_interval
,
893 .enum_frame_size
= thp7312_enum_frame_size
,
894 .enum_frame_interval
= thp7312_enum_frame_interval
,
897 static const struct v4l2_subdev_ops thp7312_subdev_ops
= {
898 .core
= &thp7312_core_ops
,
899 .video
= &thp7312_video_ops
,
900 .pad
= &thp7312_pad_ops
,
903 static const struct v4l2_subdev_internal_ops thp7312_internal_ops
= {
904 .init_state
= thp7312_init_state
,
907 /* -----------------------------------------------------------------------------
908 * V4L2 Control Operations
911 static inline struct thp7312_device
*to_thp7312_from_ctrl(struct v4l2_ctrl
*ctrl
)
913 return container_of(ctrl
->handler
, struct thp7312_device
, ctrl_handler
);
916 /* 0: 3000cm, 18: 8cm */
917 static const u16 thp7312_focus_values
[] = {
918 3000, 1000, 600, 450, 350,
919 290, 240, 200, 170, 150,
920 140, 130, 120, 110, 100,
924 static int thp7312_set_focus(struct thp7312_device
*thp7312
)
926 enum thp7312_focus_state new_state
= thp7312
->focus_state
;
932 /* Start by programming the manual focus position if it has changed. */
933 if (thp7312
->focus_absolute
->is_new
) {
936 value
= thp7312_focus_values
[thp7312
->focus_absolute
->val
];
938 ret
= cci_write(thp7312
->regmap
,
939 THP7312_REG_MANUAL_FOCUS_POSITION
, value
, NULL
);
944 /* Calculate the new focus state. */
945 switch (thp7312
->focus_state
) {
946 case THP7312_FOCUS_STATE_MANUAL
:
948 if (thp7312
->focus_auto
->val
)
949 new_state
= THP7312_FOCUS_STATE_AUTO
;
950 else if (thp7312
->focus_start
->is_new
)
951 new_state
= THP7312_FOCUS_STATE_ONESHOT
;
954 case THP7312_FOCUS_STATE_AUTO
:
955 if (!thp7312
->focus_auto
->val
)
956 new_state
= THP7312_FOCUS_STATE_LOCKED
;
959 case THP7312_FOCUS_STATE_LOCKED
:
960 if (thp7312
->focus_auto
->val
)
961 new_state
= THP7312_FOCUS_STATE_AUTO
;
962 else if (thp7312
->focus_start
->is_new
)
963 new_state
= THP7312_FOCUS_STATE_ONESHOT
;
964 else if (thp7312
->focus_absolute
->is_new
)
965 new_state
= THP7312_FOCUS_STATE_MANUAL
;
968 case THP7312_FOCUS_STATE_ONESHOT
:
969 if (thp7312
->focus_auto
->val
)
970 new_state
= THP7312_FOCUS_STATE_AUTO
;
971 else if (thp7312
->focus_start
->is_new
)
972 new_state
= THP7312_FOCUS_STATE_ONESHOT
;
973 else if (thp7312
->focus_absolute
->is_new
)
974 new_state
= THP7312_FOCUS_STATE_MANUAL
;
979 * If neither the state nor the focus method has changed, and no new
980 * one-shot focus is requested, there's nothing new to program to the
983 if (thp7312
->focus_state
== new_state
&&
984 !thp7312
->focus_method
->is_new
&& !thp7312
->focus_start
->is_new
)
987 continuous
= new_state
== THP7312_FOCUS_STATE_MANUAL
||
988 new_state
== THP7312_FOCUS_STATE_ONESHOT
;
990 switch (thp7312
->focus_method
->val
) {
991 case THP7312_FOCUS_METHOD_CONTRAST
:
993 af_setting
= continuous
994 ? THP7312_REG_AF_SETTING_CONTINUOUS_CONTRAST
995 : THP7312_REG_AF_SETTING_ONESHOT_CONTRAST
;
997 case THP7312_FOCUS_METHOD_PDAF
:
998 af_setting
= continuous
999 ? THP7312_REG_AF_SETTING_CONTINUOUS_PDAF
1000 : THP7312_REG_AF_SETTING_ONESHOT_PDAF
;
1002 case THP7312_FOCUS_METHOD_HYBRID
:
1003 af_setting
= continuous
1004 ? THP7312_REG_AF_SETTING_CONTINUOUS_HYBRID
1005 : THP7312_REG_AF_SETTING_ONESHOT_HYBRID
;
1009 switch (new_state
) {
1010 case THP7312_FOCUS_STATE_MANUAL
:
1012 af_control
= THP7312_REG_AF_CONTROL_MANUAL
;
1014 case THP7312_FOCUS_STATE_AUTO
:
1015 case THP7312_FOCUS_STATE_ONESHOT
:
1016 af_control
= THP7312_REG_AF_CONTROL_AF
;
1018 case THP7312_FOCUS_STATE_LOCKED
:
1019 af_control
= THP7312_REG_AF_CONTROL_LOCK
;
1023 cci_write(thp7312
->regmap
, THP7312_REG_AF_SETTING
, af_setting
, &ret
);
1025 if (new_state
== THP7312_FOCUS_STATE_MANUAL
&&
1026 (thp7312
->focus_state
== THP7312_FOCUS_STATE_AUTO
||
1027 thp7312
->focus_state
== THP7312_FOCUS_STATE_ONESHOT
)) {
1028 /* When switching to manual state, lock AF first. */
1029 cci_write(thp7312
->regmap
, THP7312_REG_AF_CONTROL
,
1030 THP7312_REG_AF_CONTROL_LOCK
, &ret
);
1033 cci_write(thp7312
->regmap
, THP7312_REG_AF_CONTROL
, af_control
, &ret
);
1038 thp7312
->focus_state
= new_state
;
1043 static int thp7312_s_ctrl(struct v4l2_ctrl
*ctrl
)
1045 struct thp7312_device
*thp7312
= to_thp7312_from_ctrl(ctrl
);
1049 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
1052 if (!pm_runtime_get_if_active(thp7312
->dev
))
1056 case V4L2_CID_BRIGHTNESS
:
1057 cci_write(thp7312
->regmap
, THP7312_REG_BRIGHTNESS
,
1058 ctrl
->val
+ 10, &ret
);
1061 case V4L2_CID_THP7312_LOW_LIGHT_COMPENSATION
:
1062 /* 0 = Auto adjust frame rate, 1 = Fix frame rate */
1063 cci_write(thp7312
->regmap
, THP7312_REG_AE_FIX_FRAME_RATE
,
1064 ctrl
->val
? 0 : 1, &ret
);
1067 case V4L2_CID_FOCUS_AUTO
:
1068 case V4L2_CID_FOCUS_ABSOLUTE
:
1069 case V4L2_CID_AUTO_FOCUS_START
:
1070 case V4L2_CID_THP7312_AUTO_FOCUS_METHOD
:
1071 ret
= thp7312_set_focus(thp7312
);
1074 case V4L2_CID_HFLIP
:
1075 case V4L2_CID_VFLIP
:
1076 value
= (thp7312
->hflip
->val
? THP7312_REG_FLIP_MIRROR_MIRROR
: 0)
1077 | (thp7312
->vflip
->val
? THP7312_REG_FLIP_MIRROR_FLIP
: 0);
1079 cci_write(thp7312
->regmap
, THP7312_REG_FLIP_MIRROR
, value
, &ret
);
1082 case V4L2_CID_THP7312_NOISE_REDUCTION_AUTO
:
1083 case V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE
:
1084 value
= thp7312
->noise_reduction_auto
->val
? 0
1085 : THP7312_REG_NOISE_REDUCTION_FIXED
|
1086 thp7312
->noise_reduction_absolute
->val
;
1088 cci_write(thp7312
->regmap
, THP7312_REG_NOISE_REDUCTION
, value
,
1092 case V4L2_CID_AUTO_WHITE_BALANCE
:
1093 value
= ctrl
->val
? THP7312_WB_MODE_AUTO
: THP7312_WB_MODE_MANUAL
;
1095 cci_write(thp7312
->regmap
, THP7312_REG_WB_MODE
, value
, &ret
);
1098 case V4L2_CID_RED_BALANCE
:
1099 cci_write(thp7312
->regmap
, THP7312_REG_MANUAL_WB_RED_GAIN
,
1103 case V4L2_CID_BLUE_BALANCE
:
1104 cci_write(thp7312
->regmap
, THP7312_REG_MANUAL_WB_BLUE_GAIN
,
1108 case V4L2_CID_AUTO_EXPOSURE_BIAS
:
1109 cci_write(thp7312
->regmap
, THP7312_REG_AE_EXPOSURE_COMPENSATION
,
1113 case V4L2_CID_POWER_LINE_FREQUENCY
:
1114 if (ctrl
->val
== V4L2_CID_POWER_LINE_FREQUENCY_60HZ
) {
1115 value
= THP7312_AE_FLICKER_MODE_60
;
1116 } else if (ctrl
->val
== V4L2_CID_POWER_LINE_FREQUENCY_50HZ
) {
1117 value
= THP7312_AE_FLICKER_MODE_50
;
1119 if (thp7312
->fw_version
== THP7312_FW_VERSION(40, 3)) {
1120 /* THP7312_AE_FLICKER_MODE_DISABLE is not supported */
1121 value
= THP7312_AE_FLICKER_MODE_50
;
1123 value
= THP7312_AE_FLICKER_MODE_DISABLE
;
1127 cci_write(thp7312
->regmap
, THP7312_REG_AE_FLICKER_MODE
,
1131 case V4L2_CID_SATURATION
:
1132 cci_write(thp7312
->regmap
, THP7312_REG_SATURATION
,
1136 case V4L2_CID_CONTRAST
:
1137 cci_write(thp7312
->regmap
, THP7312_REG_CONTRAST
,
1141 case V4L2_CID_SHARPNESS
:
1142 cci_write(thp7312
->regmap
, THP7312_REG_SHARPNESS
,
1150 pm_runtime_mark_last_busy(thp7312
->dev
);
1151 pm_runtime_put_autosuspend(thp7312
->dev
);
1156 static const struct v4l2_ctrl_ops thp7312_ctrl_ops
= {
1157 .s_ctrl
= thp7312_s_ctrl
,
1161 * Refer to Documentation/userspace-api/media/drivers/thp7312.rst for details.
1163 static const struct v4l2_ctrl_config thp7312_ctrl_focus_method_cdaf
= {
1164 .ops
= &thp7312_ctrl_ops
,
1165 .id
= V4L2_CID_THP7312_AUTO_FOCUS_METHOD
,
1166 .name
= "Auto-Focus Method",
1167 .type
= V4L2_CTRL_TYPE_INTEGER
,
1168 .min
= THP7312_FOCUS_METHOD_CONTRAST
,
1169 .def
= THP7312_FOCUS_METHOD_CONTRAST
,
1170 .max
= THP7312_FOCUS_METHOD_CONTRAST
,
1174 static const struct v4l2_ctrl_config thp7312_ctrl_focus_method_pdaf
= {
1175 .ops
= &thp7312_ctrl_ops
,
1176 .id
= V4L2_CID_THP7312_AUTO_FOCUS_METHOD
,
1177 .name
= "Auto-Focus Method",
1178 .type
= V4L2_CTRL_TYPE_INTEGER
,
1179 .min
= THP7312_FOCUS_METHOD_CONTRAST
,
1180 .def
= THP7312_FOCUS_METHOD_HYBRID
,
1181 .max
= THP7312_FOCUS_METHOD_HYBRID
,
1185 static const struct v4l2_ctrl_config thp7312_v4l2_ctrls_custom
[] = {
1187 .ops
= &thp7312_ctrl_ops
,
1188 .id
= V4L2_CID_THP7312_LOW_LIGHT_COMPENSATION
,
1189 .name
= "Low Light Compensation",
1190 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1196 .ops
= &thp7312_ctrl_ops
,
1197 .id
= V4L2_CID_THP7312_NOISE_REDUCTION_AUTO
,
1198 .name
= "Noise Reduction Auto",
1199 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1205 .ops
= &thp7312_ctrl_ops
,
1206 .id
= V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE
,
1207 .name
= "Noise Reduction Level",
1208 .type
= V4L2_CTRL_TYPE_INTEGER
,
1216 static const s64 exp_bias_qmenu
[] = {
1217 -2000, -1667, -1333, -1000, -667, -333, 0, 333, 667, 1000, 1333, 1667, 2000
1220 static int thp7312_init_controls(struct thp7312_device
*thp7312
)
1222 struct v4l2_ctrl_handler
*hdl
= &thp7312
->ctrl_handler
;
1223 struct device
*dev
= thp7312
->dev
;
1224 struct v4l2_fwnode_device_properties props
;
1225 struct v4l2_ctrl
*link_freq
;
1226 unsigned int num_controls
;
1232 * Check what auto-focus methods the connected sensor supports, if any.
1233 * Firmwares before v90.03 didn't expose the AF_SUPPORT register,
1234 * consider both CDAF and PDAF as supported in that case.
1236 if (thp7312
->fw_version
>= THP7312_FW_VERSION(90, 3)) {
1239 ret
= cci_read(thp7312
->regmap
, THP7312_REG_AF_SUPPORT
, &val
,
1244 af_support
= val
& (THP7312_AF_SUPPORT_PDAF
|
1245 THP7312_AF_SUPPORT_CONTRAST
);
1247 af_support
= THP7312_AF_SUPPORT_PDAF
1248 | THP7312_AF_SUPPORT_CONTRAST
;
1251 num_controls
= 14 + ARRAY_SIZE(thp7312_v4l2_ctrls_custom
)
1252 + (af_support
? 4 : 0);
1254 v4l2_ctrl_handler_init(hdl
, num_controls
);
1257 const struct v4l2_ctrl_config
*af_method
;
1259 af_method
= af_support
& THP7312_AF_SUPPORT_PDAF
1260 ? &thp7312_ctrl_focus_method_pdaf
1261 : &thp7312_ctrl_focus_method_cdaf
;
1263 thp7312
->focus_state
= THP7312_FOCUS_STATE_MANUAL
;
1265 thp7312
->focus_auto
=
1266 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
,
1267 V4L2_CID_FOCUS_AUTO
,
1269 thp7312
->focus_absolute
=
1270 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
,
1271 V4L2_CID_FOCUS_ABSOLUTE
,
1272 0, ARRAY_SIZE(thp7312_focus_values
),
1274 thp7312
->focus_method
=
1275 v4l2_ctrl_new_custom(hdl
, af_method
, NULL
);
1276 thp7312
->focus_start
=
1277 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
,
1278 V4L2_CID_AUTO_FOCUS_START
,
1281 v4l2_ctrl_cluster(4, &thp7312
->focus_auto
);
1284 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_AUTO_WHITE_BALANCE
,
1286 /* 32: 1x, 255: 7.95x */
1287 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_RED_BALANCE
,
1289 /* 32: 1x, 255: 7.95x */
1290 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_BLUE_BALANCE
,
1293 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_BRIGHTNESS
,
1295 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_SATURATION
,
1297 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_CONTRAST
,
1299 v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
, V4L2_CID_SHARPNESS
,
1302 thp7312
->hflip
= v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
,
1303 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1304 thp7312
->vflip
= v4l2_ctrl_new_std(hdl
, &thp7312_ctrl_ops
,
1305 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1307 v4l2_ctrl_cluster(2, &thp7312
->hflip
);
1309 v4l2_ctrl_new_int_menu(hdl
, &thp7312_ctrl_ops
,
1310 V4L2_CID_AUTO_EXPOSURE_BIAS
,
1311 ARRAY_SIZE(exp_bias_qmenu
) - 1,
1312 ARRAY_SIZE(exp_bias_qmenu
) / 2, exp_bias_qmenu
);
1314 v4l2_ctrl_new_std_menu(hdl
, &thp7312_ctrl_ops
,
1315 V4L2_CID_POWER_LINE_FREQUENCY
,
1316 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, 0,
1317 V4L2_CID_POWER_LINE_FREQUENCY_50HZ
);
1319 thp7312
->link_freq
= thp7312_mode_info_data
[0].rates
[0].link_freq
;
1321 link_freq
= v4l2_ctrl_new_int_menu(hdl
, &thp7312_ctrl_ops
,
1322 V4L2_CID_LINK_FREQ
, 0, 0,
1323 &thp7312
->link_freq
);
1325 /* Set properties from fwnode (e.g. rotation, orientation). */
1326 ret
= v4l2_fwnode_device_parse(dev
, &props
);
1328 dev_err(dev
, "Failed to parse fwnode: %d\n", ret
);
1332 ret
= v4l2_ctrl_new_fwnode_properties(hdl
, &thp7312_ctrl_ops
, &props
);
1334 dev_err(dev
, "Failed to create new v4l2 ctrl for fwnode properties: %d\n", ret
);
1338 for (i
= 0; i
< ARRAY_SIZE(thp7312_v4l2_ctrls_custom
); i
++) {
1339 const struct v4l2_ctrl_config
*ctrl_cfg
=
1340 &thp7312_v4l2_ctrls_custom
[i
];
1341 struct v4l2_ctrl
*ctrl
;
1343 ctrl
= v4l2_ctrl_new_custom(hdl
, ctrl_cfg
, NULL
);
1345 if (ctrl_cfg
->id
== V4L2_CID_THP7312_NOISE_REDUCTION_AUTO
)
1346 thp7312
->noise_reduction_auto
= ctrl
;
1347 else if (ctrl_cfg
->id
== V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE
)
1348 thp7312
->noise_reduction_absolute
= ctrl
;
1351 v4l2_ctrl_cluster(2, &thp7312
->noise_reduction_auto
);
1354 dev_err(dev
, "v4l2_ctrl_handler error\n");
1359 link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1364 v4l2_ctrl_handler_free(hdl
);
1368 /* -----------------------------------------------------------------------------
1373 * The firmware data is made of 128kB of RAM firmware, followed by a
1374 * variable-size "header". Both are stored in flash memory.
1376 #define THP7312_FW_RAM_SIZE (128 * 1024)
1377 #define THP7312_FW_MIN_SIZE (THP7312_FW_RAM_SIZE + 4)
1378 #define THP7312_FW_MAX_SIZE (THP7312_FW_RAM_SIZE + 64 * 1024)
1381 * Data is first uploaded to the THP7312 128kB SRAM, and then written to flash.
1382 * The SRAM is exposed over I2C as 32kB banks, and up to 4kB of data can be
1383 * transferred in a single I2C write.
1385 #define THP7312_RAM_BANK_SIZE (32 * 1024)
1386 #define THP7312_FW_DOWNLOAD_UNIT (4 * 1024)
1388 #define THP7312_FLASH_MEMORY_ERASE_TIMEOUT 40
1390 #define THP7312_FLASH_MAX_REG_READ_SIZE 10
1391 #define THP7312_FLASH_MAX_REG_DATA_SIZE 10
1393 static const u8 thp7312_cmd_config_flash_mem_if
[] = {
1394 0xd5, 0x18, 0x00, 0x00, 0x00, 0x80
1397 static const u8 thp7312_cmd_write_to_reg
[] = {
1398 0xd5, 0x0c, 0x80, 0x00, 0x00, 0x00
1401 static const u8 thp7312_cmd_read_reg
[] = {
1406 * THP7312 Write data from RAM to Flash Memory
1408 * Format: FF700F AA AA AA BB BB BB
1409 * AA AA AA: destination start address
1410 * BB BB BB: (write size - 1)
1411 * Source address always starts from 0
1413 static const u8 thp7312_cmd_write_ram_to_flash
[] = { 0xff, 0x70, 0x0f };
1416 * THP7312 Calculate CRC command
1417 * Command ID: FF70 09
1418 * Format: FF70 09 AA AA AA BB BB BB
1419 * AA AA AA: Start address of calculation
1420 * BB BB BB: (calculate size - 1)
1422 static const u8 thp7312_cmd_calc_crc
[] = { 0xff, 0x70, 0x09 };
1424 static const u8 thp7312_jedec_rdid
[] = { SPINOR_OP_RDID
, 0x00, 0x00, 0x00 };
1425 static const u8 thp7312_jedec_rdsr
[] = { SPINOR_OP_RDSR
, 0x00, 0x00, 0x00 };
1426 static const u8 thp7312_jedec_wen
[] = { SPINOR_OP_WREN
};
1428 static int thp7312_read_firmware_version(struct thp7312_device
*thp7312
)
1435 cci_read(thp7312
->regmap
, THP7312_REG_FIRMWARE_VERSION_1
, &val
, &ret
);
1438 cci_read(thp7312
->regmap
, THP7312_REG_FIRMWARE_VERSION_2
, &val
, &ret
);
1441 thp7312
->fw_version
= THP7312_FW_VERSION(major
, minor
);
1445 static int thp7312_write_buf(struct thp7312_device
*thp7312
,
1446 const u8
*write_buf
, u16 write_size
)
1448 struct i2c_client
*client
= to_i2c_client(thp7312
->dev
);
1451 ret
= i2c_master_send(client
, write_buf
, write_size
);
1452 return ret
>= 0 ? 0 : ret
;
1455 static int __thp7312_flash_reg_write(struct thp7312_device
*thp7312
,
1456 const u8
*write_buf
, u16 write_size
)
1458 struct device
*dev
= thp7312
->dev
;
1459 u8 temp_write_buf
[THP7312_FLASH_MAX_REG_DATA_SIZE
+ 2];
1462 if (write_size
> THP7312_FLASH_MAX_REG_DATA_SIZE
) {
1463 dev_err(dev
, "%s: Write size error size = %d\n",
1464 __func__
, write_size
);
1468 ret
= thp7312_write_buf(thp7312
, thp7312_cmd_config_flash_mem_if
,
1469 sizeof(thp7312_cmd_config_flash_mem_if
));
1471 dev_err(dev
, "%s: Failed to config flash memory IF: %d\n",
1476 temp_write_buf
[0] = 0xd5;
1477 temp_write_buf
[1] = 0x00;
1478 memcpy((temp_write_buf
+ 2), write_buf
, write_size
);
1479 ret
= thp7312_write_buf(thp7312
, temp_write_buf
, write_size
+ 2);
1483 thp7312_write_buf(thp7312
, thp7312_cmd_write_to_reg
,
1484 sizeof(thp7312_cmd_write_to_reg
));
1489 static int __thp7312_flash_reg_read(struct thp7312_device
*thp7312
,
1490 const u8
*write_buf
, u16 write_size
,
1491 u8
*read_buf
, u16 read_size
)
1493 struct i2c_client
*client
= to_i2c_client(thp7312
->dev
);
1494 struct i2c_msg msgs
[2];
1497 ret
= __thp7312_flash_reg_write(thp7312
, write_buf
, write_size
);
1501 msgs
[0].addr
= client
->addr
;
1503 msgs
[0].len
= sizeof(thp7312_cmd_read_reg
);
1504 msgs
[0].buf
= (u8
*)thp7312_cmd_read_reg
;
1506 msgs
[1].addr
= client
->addr
;
1507 msgs
[1].flags
= I2C_M_RD
;
1508 msgs
[1].len
= read_size
;
1509 msgs
[1].buf
= read_buf
;
1511 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1512 return ret
>= 0 ? 0 : ret
;
1515 #define thp7312_flash_reg_write(thp7312, wrbuf) \
1516 __thp7312_flash_reg_write(thp7312, wrbuf, sizeof(wrbuf))
1518 #define thp7312_flash_reg_read(thp7312, wrbuf, rdbuf) \
1519 __thp7312_flash_reg_read(thp7312, wrbuf, sizeof(wrbuf), \
1520 rdbuf, sizeof(rdbuf))
1522 static enum fw_upload_err
thp7312_fw_prepare_config(struct thp7312_device
*thp7312
)
1524 struct device
*dev
= thp7312
->dev
;
1527 ret
= cci_write(thp7312
->regmap
, THP7312_REG_FW_MEMORY_IO_SETTING
,
1528 THP7312_FW_MEMORY_IO_GPIO0
, NULL
);
1530 dev_err(dev
, "Failed to set flash memory I/O\n");
1531 return FW_UPLOAD_ERR_HW_ERROR
;
1534 /* Set max drivability. */
1535 ret
= cci_write(thp7312
->regmap
, THP7312_REG_FW_DRIVABILITY
, 0x00777777,
1538 dev_err(dev
, "Failed to set drivability: %d\n", ret
);
1539 return FW_UPLOAD_ERR_HW_ERROR
;
1542 return FW_UPLOAD_ERR_NONE
;
1545 static enum fw_upload_err
thp7312_fw_prepare_check(struct thp7312_device
*thp7312
)
1547 struct device
*dev
= thp7312
->dev
;
1548 u8 read_buf
[3] = { 0 };
1552 ret
= thp7312_flash_reg_read(thp7312
, thp7312_jedec_rdid
, read_buf
);
1554 dev_err(dev
, "Failed to get JEDEC ID: %d\n", ret
);
1555 return FW_UPLOAD_ERR_HW_ERROR
;
1558 dev_dbg(dev
, "Flash Memory: JEDEC ID = 0x%x 0x%x 0x%x\n",
1559 read_buf
[0], read_buf
[1], read_buf
[2]);
1561 return FW_UPLOAD_ERR_NONE
;
1564 static enum fw_upload_err
thp7312_fw_prepare_reset(struct thp7312_device
*thp7312
)
1566 struct device
*dev
= thp7312
->dev
;
1569 ret
= cci_write(thp7312
->regmap
, THP7312_REG_FW_RESET_FLASH
, 0x81, NULL
);
1571 dev_err(dev
, "Failed to reset flash memory: %d\n", ret
);
1572 return FW_UPLOAD_ERR_HW_ERROR
;
1575 return FW_UPLOAD_ERR_NONE
;
1578 /* TODO: Erase only the amount of blocks necessary */
1579 static enum fw_upload_err
thp7312_flash_erase(struct thp7312_device
*thp7312
)
1581 struct device
*dev
= thp7312
->dev
;
1582 u8 read_buf
[1] = { 0 };
1587 for (block
= 0; block
< 3; block
++) {
1588 const u8 jedec_se
[] = { SPINOR_OP_SE
, block
, 0x00, 0x00 };
1590 ret
= thp7312_flash_reg_write(thp7312
, thp7312_jedec_wen
);
1592 dev_err(dev
, "Failed to enable flash for writing\n");
1593 return FW_UPLOAD_ERR_RW_ERROR
;
1596 ret
= thp7312_flash_reg_write(thp7312
, jedec_se
);
1598 dev_err(dev
, "Failed to erase flash sector\n");
1599 return FW_UPLOAD_ERR_RW_ERROR
;
1602 for (i
= 0; i
< THP7312_FLASH_MEMORY_ERASE_TIMEOUT
; i
++) {
1603 usleep_range(100000, 101000);
1604 thp7312_flash_reg_read(thp7312
, thp7312_jedec_rdsr
,
1607 /* Check Busy bit. Busy == 0x0 means erase complete. */
1608 if (!(read_buf
[0] & SR_WIP
))
1612 if (i
== THP7312_FLASH_MEMORY_ERASE_TIMEOUT
)
1613 return FW_UPLOAD_ERR_TIMEOUT
;
1616 thp7312_flash_reg_read(thp7312
, thp7312_jedec_rdsr
, read_buf
);
1618 /* Check WEL bit. */
1619 if (read_buf
[0] & SR_WEL
)
1620 return FW_UPLOAD_ERR_HW_ERROR
;
1622 return FW_UPLOAD_ERR_NONE
;
1625 static enum fw_upload_err
1626 thp7312_write_download_data_by_unit(struct thp7312_device
*thp7312
,
1627 unsigned int addr
, const u8
*data
,
1630 struct device
*dev
= thp7312
->dev
;
1631 u8
*write_buf
= thp7312
->fw_write_buf
;
1634 dev_dbg(dev
, "%s: addr = 0x%04x, data = 0x%p, size = %u\n",
1635 __func__
, addr
, data
, size
);
1637 write_buf
[0] = (addr
>> 8) & 0xff;
1638 write_buf
[1] = (addr
>> 0) & 0xff;
1639 memcpy(&write_buf
[2], data
, size
);
1642 * THP7312 Firmware download to RAM
1643 * Command ID (address to download): 0x0000 - 0x7fff
1644 * Format:: 0000 XX XX XX ........ XX
1646 ret
= thp7312_write_buf(thp7312
, write_buf
, size
+ 2);
1648 dev_err(dev
, "Unit transfer ERROR %s(): ret = %d\n", __func__
, ret
);
1650 return ret
>= 0 ? FW_UPLOAD_ERR_NONE
: FW_UPLOAD_ERR_RW_ERROR
;
1653 static enum fw_upload_err
thp7312_fw_load_to_ram(struct thp7312_device
*thp7312
,
1654 const u8
*data
, u32 size
)
1656 struct device
*dev
= thp7312
->dev
;
1657 enum fw_upload_err ret
;
1658 unsigned int num_banks
;
1661 num_banks
= DIV_ROUND_UP(size
, THP7312_RAM_BANK_SIZE
);
1663 dev_dbg(dev
, "%s: loading %u bytes in SRAM (%u banks)\n", __func__
,
1666 for (i
= 0; i
< num_banks
; i
++) {
1667 const u32 bank_addr
= 0x10000000 | (i
* THP7312_RAM_BANK_SIZE
);
1668 unsigned int bank_size
;
1669 unsigned int num_chunks
;
1671 ret
= cci_write(thp7312
->regmap
, THP7312_REG_FW_DEST_BANK_ADDR
,
1674 return FW_UPLOAD_ERR_HW_ERROR
;
1676 bank_size
= min_t(u32
, size
, THP7312_RAM_BANK_SIZE
);
1677 num_chunks
= DIV_ROUND_UP(bank_size
, THP7312_FW_DOWNLOAD_UNIT
);
1679 dev_dbg(dev
, "%s: loading %u bytes in SRAM bank %u (%u chunks)\n",
1680 __func__
, bank_size
, i
, num_chunks
);
1682 for (j
= 0 ; j
< num_chunks
; j
++) {
1683 unsigned int chunk_addr
;
1684 unsigned int chunk_size
;
1686 chunk_addr
= j
* THP7312_FW_DOWNLOAD_UNIT
;
1687 chunk_size
= min_t(u32
, size
, THP7312_FW_DOWNLOAD_UNIT
);
1689 ret
= thp7312_write_download_data_by_unit(thp7312
, chunk_addr
,
1691 if (ret
!= FW_UPLOAD_ERR_NONE
) {
1692 dev_err(dev
, "Unit transfer ERROR at bank transfer %s(): %d\n",
1702 return FW_UPLOAD_ERR_NONE
;
1705 static enum fw_upload_err
thp7312_fw_write_to_flash(struct thp7312_device
*thp7312
,
1706 u32 dest
, u32 write_size
)
1708 u8 command
[sizeof(thp7312_cmd_write_ram_to_flash
) + 6];
1709 static const u32 cmd_size
= sizeof(thp7312_cmd_write_ram_to_flash
);
1713 memcpy(command
, thp7312_cmd_write_ram_to_flash
, cmd_size
);
1715 command
[cmd_size
] = (dest
& 0xff0000) >> 16;
1716 command
[cmd_size
+ 1] = (dest
& 0x00ff00) >> 8;
1717 command
[cmd_size
+ 2] = (dest
& 0x0000ff);
1718 command
[cmd_size
+ 3] = ((write_size
- 1) & 0xff0000) >> 16;
1719 command
[cmd_size
+ 4] = ((write_size
- 1) & 0x00ff00) >> 8;
1720 command
[cmd_size
+ 5] = ((write_size
- 1) & 0x0000ff);
1722 ret
= thp7312_write_buf(thp7312
, command
, sizeof(command
));
1724 return FW_UPLOAD_ERR_RW_ERROR
;
1726 usleep_range(8000000, 8100000);
1728 ret
= cci_read(thp7312
->regmap
, THP7312_REG_FW_VERIFY_RESULT
, &val
,
1731 return FW_UPLOAD_ERR_RW_ERROR
;
1733 return val
? FW_UPLOAD_ERR_HW_ERROR
: FW_UPLOAD_ERR_NONE
;
1736 static enum fw_upload_err
thp7312_fw_check_crc(struct thp7312_device
*thp7312
,
1737 const u8
*fw_data
, u32 fw_size
)
1739 struct device
*dev
= thp7312
->dev
;
1740 u16 header_size
= fw_size
- THP7312_FW_RAM_SIZE
;
1741 u8 command
[sizeof(thp7312_cmd_calc_crc
) + 6];
1742 static const u32 cmd_size
= sizeof(thp7312_cmd_calc_crc
);
1743 u32 size
= THP7312_FW_RAM_SIZE
- 4;
1748 memcpy(command
, thp7312_cmd_calc_crc
, cmd_size
);
1750 command
[cmd_size
] = 0;
1751 command
[cmd_size
+ 1] = (header_size
>> 8) & 0xff;
1752 command
[cmd_size
+ 2] = header_size
& 0xff;
1754 command
[cmd_size
+ 3] = (size
>> 16) & 0xff;
1755 command
[cmd_size
+ 4] = (size
>> 8) & 0xff;
1756 command
[cmd_size
+ 5] = size
& 0xff;
1758 ret
= thp7312_write_buf(thp7312
, command
, sizeof(command
));
1760 return FW_UPLOAD_ERR_RW_ERROR
;
1762 usleep_range(2000000, 2100000);
1764 fw_crc
= get_unaligned_be32(&fw_data
[fw_size
- 4]);
1766 ret
= cci_read(thp7312
->regmap
, THP7312_REG_FW_CRC_RESULT
, &crc
, NULL
);
1768 return FW_UPLOAD_ERR_RW_ERROR
;
1770 if (fw_crc
!= crc
) {
1771 dev_err(dev
, "CRC mismatch: firmware 0x%08x, flash 0x%08llx\n",
1773 return FW_UPLOAD_ERR_HW_ERROR
;
1776 return FW_UPLOAD_ERR_NONE
;
1779 static enum fw_upload_err
thp7312_fw_prepare(struct fw_upload
*fw_upload
,
1780 const u8
*data
, u32 size
)
1782 struct thp7312_device
*thp7312
= fw_upload
->dd_handle
;
1783 struct device
*dev
= thp7312
->dev
;
1784 enum fw_upload_err ret
;
1786 mutex_lock(&thp7312
->fw_lock
);
1787 thp7312
->fw_cancel
= false;
1788 mutex_unlock(&thp7312
->fw_lock
);
1790 if (size
< THP7312_FW_MIN_SIZE
|| size
> THP7312_FW_MAX_SIZE
) {
1791 dev_err(dev
, "%s: Invalid firmware size %d; must be between %d and %d\n",
1792 __func__
, size
, THP7312_FW_MIN_SIZE
, THP7312_FW_MAX_SIZE
);
1793 return FW_UPLOAD_ERR_INVALID_SIZE
;
1796 ret
= thp7312_fw_prepare_config(thp7312
);
1797 if (ret
!= FW_UPLOAD_ERR_NONE
)
1800 ret
= thp7312_fw_prepare_check(thp7312
);
1801 if (ret
!= FW_UPLOAD_ERR_NONE
)
1804 ret
= thp7312_fw_prepare_reset(thp7312
);
1805 if (ret
!= FW_UPLOAD_ERR_NONE
)
1808 mutex_lock(&thp7312
->fw_lock
);
1809 ret
= thp7312
->fw_cancel
? FW_UPLOAD_ERR_CANCELED
: FW_UPLOAD_ERR_NONE
;
1810 mutex_unlock(&thp7312
->fw_lock
);
1815 static enum fw_upload_err
thp7312_fw_write(struct fw_upload
*fw_upload
,
1816 const u8
*data
, u32 offset
,
1817 u32 size
, u32
*written
)
1819 struct thp7312_device
*thp7312
= fw_upload
->dd_handle
;
1820 struct device
*dev
= thp7312
->dev
;
1821 u16 header_size
= size
- THP7312_FW_RAM_SIZE
;
1822 enum fw_upload_err ret
;
1825 mutex_lock(&thp7312
->fw_lock
);
1826 cancel
= thp7312
->fw_cancel
;
1827 mutex_unlock(&thp7312
->fw_lock
);
1830 return FW_UPLOAD_ERR_CANCELED
;
1832 ret
= thp7312_flash_erase(thp7312
);
1833 if (ret
!= FW_UPLOAD_ERR_NONE
)
1836 ret
= thp7312_fw_load_to_ram(thp7312
, data
, THP7312_FW_RAM_SIZE
);
1837 if (ret
!= FW_UPLOAD_ERR_NONE
)
1840 ret
= thp7312_fw_write_to_flash(thp7312
, 0, 0x1ffff);
1841 if (ret
!= FW_UPLOAD_ERR_NONE
)
1844 ret
= thp7312_fw_load_to_ram(thp7312
, data
+ THP7312_FW_RAM_SIZE
, header_size
);
1845 if (ret
!= FW_UPLOAD_ERR_NONE
)
1848 ret
= thp7312_fw_write_to_flash(thp7312
, 0x20000, header_size
- 1);
1849 if (ret
!= FW_UPLOAD_ERR_NONE
)
1852 ret
= thp7312_fw_check_crc(thp7312
, data
, size
);
1853 if (ret
!= FW_UPLOAD_ERR_NONE
)
1856 dev_info(dev
, "Successfully wrote firmware\n");
1859 return FW_UPLOAD_ERR_NONE
;
1862 static enum fw_upload_err
thp7312_fw_poll_complete(struct fw_upload
*fw_upload
)
1864 return FW_UPLOAD_ERR_NONE
;
1868 * This may be called asynchronously with an on-going update. All other
1869 * functions are called sequentially in a single thread. To avoid contention on
1870 * register accesses, only update the cancel_request flag. Other functions will
1871 * check this flag and handle the cancel request synchronously.
1873 static void thp7312_fw_cancel(struct fw_upload
*fw_upload
)
1875 struct thp7312_device
*thp7312
= fw_upload
->dd_handle
;
1877 mutex_lock(&thp7312
->fw_lock
);
1878 thp7312
->fw_cancel
= true;
1879 mutex_unlock(&thp7312
->fw_lock
);
1882 static const struct fw_upload_ops thp7312_fw_upload_ops
= {
1883 .prepare
= thp7312_fw_prepare
,
1884 .write
= thp7312_fw_write
,
1885 .poll_complete
= thp7312_fw_poll_complete
,
1886 .cancel
= thp7312_fw_cancel
,
1889 static int thp7312_register_flash_mode(struct thp7312_device
*thp7312
)
1891 struct device
*dev
= thp7312
->dev
;
1892 struct fw_upload
*fwl
;
1896 dev_info(dev
, "booted in flash mode\n");
1898 mutex_init(&thp7312
->fw_lock
);
1900 thp7312
->fw_write_buf
= devm_kzalloc(dev
, THP7312_FW_DOWNLOAD_UNIT
+ 2,
1902 if (!thp7312
->fw_write_buf
)
1905 ret
= __thp7312_power_on(thp7312
);
1907 return dev_err_probe(dev
, ret
, "Failed to power on\n");
1909 ret
= cci_read(thp7312
->regmap
, THP7312_REG_FW_STATUS
, &val
, NULL
);
1911 dev_err_probe(dev
, ret
, "Camera status read failed\n");
1915 fwl
= firmware_upload_register(THIS_MODULE
, dev
, "thp7312-firmware",
1916 &thp7312_fw_upload_ops
, thp7312
);
1919 dev_err_probe(dev
, ret
, "Failed to register firmware upload\n");
1927 __thp7312_power_off(thp7312
);
1931 /* -----------------------------------------------------------------------------
1935 static int thp7312_get_regulators(struct thp7312_device
*thp7312
)
1939 for (i
= 0; i
< ARRAY_SIZE(thp7312
->supplies
); i
++)
1940 thp7312
->supplies
[i
].supply
= thp7312_supply_name
[i
];
1942 return devm_regulator_bulk_get(thp7312
->dev
,
1943 ARRAY_SIZE(thp7312
->supplies
),
1947 static int thp7312_sensor_parse_dt(struct thp7312_device
*thp7312
,
1948 struct fwnode_handle
*node
)
1950 struct device
*dev
= thp7312
->dev
;
1951 struct thp7312_sensor
*sensor
;
1959 /* Retrieve the sensor index from the reg property. */
1960 ret
= fwnode_property_read_u32(node
, "reg", ®
);
1962 dev_err(dev
, "'reg' property missing in sensor node\n");
1966 if (reg
>= ARRAY_SIZE(thp7312
->sensors
)) {
1967 dev_err(dev
, "Out-of-bounds 'reg' value %u\n", reg
);
1971 sensor
= &thp7312
->sensors
[reg
];
1973 dev_err(dev
, "Duplicate entry for sensor %u\n", reg
);
1977 ret
= fwnode_property_read_string(node
, "thine,model", &model
);
1979 dev_err(dev
, "'thine,model' property missing in sensor node\n");
1983 for (i
= 0; i
< ARRAY_SIZE(thp7312_sensor_info
); i
++) {
1984 const struct thp7312_sensor_info
*info
=
1985 &thp7312_sensor_info
[i
];
1987 if (!strcmp(info
->model
, model
)) {
1988 sensor
->info
= info
;
1993 if (!sensor
->info
) {
1994 dev_err(dev
, "Unsupported sensor model %s\n", model
);
1998 ret
= fwnode_property_read_u32_array(node
, "data-lanes", values
,
1999 ARRAY_SIZE(values
));
2001 dev_err(dev
, "Failed to read property data-lanes: %d\n", ret
);
2005 for (i
= 0; i
< ARRAY_SIZE(data_lanes
); ++i
)
2006 data_lanes
[i
] = values
[i
];
2008 ret
= thp7312_map_data_lanes(&sensor
->lane_remap
, data_lanes
,
2009 ARRAY_SIZE(data_lanes
));
2011 dev_err(dev
, "Invalid sensor@%u data-lanes value\n", reg
);
2018 static int thp7312_parse_dt(struct thp7312_device
*thp7312
)
2020 struct v4l2_fwnode_endpoint ep
= {
2021 .bus_type
= V4L2_MBUS_CSI2_DPHY
,
2023 struct device
*dev
= thp7312
->dev
;
2024 struct fwnode_handle
*endpoint
;
2025 struct fwnode_handle
*sensors
;
2026 unsigned int num_sensors
= 0;
2027 struct fwnode_handle
*node
;
2030 endpoint
= fwnode_graph_get_next_endpoint(dev_fwnode(dev
), NULL
);
2032 return dev_err_probe(dev
, -EINVAL
, "Endpoint node not found\n");
2034 ret
= v4l2_fwnode_endpoint_parse(endpoint
, &ep
);
2035 fwnode_handle_put(endpoint
);
2037 return dev_err_probe(dev
, ret
, "Could not parse endpoint\n");
2039 ret
= thp7312_map_data_lanes(&thp7312
->lane_remap
,
2040 ep
.bus
.mipi_csi2
.data_lanes
,
2041 ep
.bus
.mipi_csi2
.num_data_lanes
);
2043 dev_err(dev
, "Invalid data-lanes value\n");
2048 * The thine,boot-mode property is optional and default to
2049 * THP7312_BOOT_MODE_SPI_MASTER (1).
2051 thp7312
->boot_mode
= THP7312_BOOT_MODE_SPI_MASTER
;
2052 ret
= device_property_read_u32(dev
, "thine,boot-mode",
2053 &thp7312
->boot_mode
);
2054 if (ret
&& ret
!= -EINVAL
)
2055 return dev_err_probe(dev
, ret
, "Property '%s' is invalid\n",
2058 if (thp7312
->boot_mode
!= THP7312_BOOT_MODE_2WIRE_SLAVE
&&
2059 thp7312
->boot_mode
!= THP7312_BOOT_MODE_SPI_MASTER
)
2060 return dev_err_probe(dev
, -EINVAL
, "Invalid '%s' value %u\n",
2061 "thine,boot-mode", thp7312
->boot_mode
);
2064 sensors
= device_get_named_child_node(dev
, "sensors");
2066 dev_err(dev
, "'sensors' child node not found\n");
2070 fwnode_for_each_available_child_node(sensors
, node
) {
2071 if (fwnode_name_eq(node
, "sensor")) {
2072 if (!thp7312_sensor_parse_dt(thp7312
, node
))
2077 fwnode_handle_put(sensors
);
2080 dev_err(dev
, "No sensor found\n");
2087 static int thp7312_probe(struct i2c_client
*client
)
2089 struct device
*dev
= &client
->dev
;
2090 struct thp7312_device
*thp7312
;
2093 thp7312
= devm_kzalloc(dev
, sizeof(*thp7312
), GFP_KERNEL
);
2099 thp7312
->regmap
= devm_cci_regmap_init_i2c(client
, 16);
2100 if (IS_ERR(thp7312
->regmap
))
2101 return dev_err_probe(dev
, PTR_ERR(thp7312
->regmap
),
2102 "Unable to initialize I2C\n");
2104 ret
= thp7312_parse_dt(thp7312
);
2108 ret
= thp7312_get_regulators(thp7312
);
2110 return dev_err_probe(dev
, ret
, "Failed to get regulators\n");
2112 thp7312
->iclk
= devm_clk_get(dev
, NULL
);
2113 if (IS_ERR(thp7312
->iclk
))
2114 return dev_err_probe(dev
, PTR_ERR(thp7312
->iclk
),
2115 "Failed to get iclk\n");
2117 thp7312
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
2118 if (IS_ERR(thp7312
->reset_gpio
))
2119 return dev_err_probe(dev
, PTR_ERR(thp7312
->reset_gpio
),
2120 "Failed to get reset gpio\n");
2122 if (thp7312
->boot_mode
== THP7312_BOOT_MODE_2WIRE_SLAVE
)
2123 return thp7312_register_flash_mode(thp7312
);
2125 v4l2_i2c_subdev_init(&thp7312
->sd
, client
, &thp7312_subdev_ops
);
2126 thp7312
->sd
.internal_ops
= &thp7312_internal_ops
;
2127 thp7312
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
2128 thp7312
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2129 thp7312
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2131 ret
= media_entity_pads_init(&thp7312
->sd
.entity
, 1, &thp7312
->pad
);
2136 * Enable power management. The driver supports runtime PM, but needs to
2137 * work when runtime PM is disabled in the kernel. To that end, power
2138 * the device manually here.
2140 ret
= thp7312_power_on(thp7312
);
2142 goto err_entity_cleanup
;
2144 ret
= thp7312_read_firmware_version(thp7312
);
2146 dev_err(dev
, "Camera is not found\n");
2150 ret
= thp7312_init_controls(thp7312
);
2152 dev_err(dev
, "Failed to initialize controls\n");
2156 thp7312
->sd
.ctrl_handler
= &thp7312
->ctrl_handler
;
2157 thp7312
->sd
.state_lock
= thp7312
->ctrl_handler
.lock
;
2159 ret
= v4l2_subdev_init_finalize(&thp7312
->sd
);
2161 dev_err(dev
, "Subdev active state initialization failed\n");
2162 goto err_free_ctrls
;
2166 * Enable runtime PM with autosuspend. As the device has been powered
2167 * manually, mark it as active, and increase the usage count without
2168 * resuming the device.
2170 pm_runtime_set_active(dev
);
2171 pm_runtime_get_noresume(dev
);
2172 pm_runtime_enable(dev
);
2173 pm_runtime_set_autosuspend_delay(dev
, 1000);
2174 pm_runtime_use_autosuspend(dev
);
2176 ret
= v4l2_async_register_subdev(&thp7312
->sd
);
2178 dev_err(dev
, "Subdev registration failed\n");
2183 * Decrease the PM usage count. The device will get suspended after the
2184 * autosuspend delay, turning the power off.
2186 pm_runtime_mark_last_busy(dev
);
2187 pm_runtime_put_autosuspend(dev
);
2189 dev_info(dev
, "THP7312 firmware version %02u.%02u\n",
2190 THP7312_FW_VERSION_MAJOR(thp7312
->fw_version
),
2191 THP7312_FW_VERSION_MINOR(thp7312
->fw_version
));
2196 pm_runtime_disable(dev
);
2197 pm_runtime_put_noidle(dev
);
2198 v4l2_subdev_cleanup(&thp7312
->sd
);
2200 v4l2_ctrl_handler_free(&thp7312
->ctrl_handler
);
2202 thp7312_power_off(thp7312
);
2204 media_entity_cleanup(&thp7312
->sd
.entity
);
2208 static void thp7312_remove(struct i2c_client
*client
)
2210 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2211 struct thp7312_device
*thp7312
= to_thp7312_dev(sd
);
2213 if (thp7312
->boot_mode
== THP7312_BOOT_MODE_2WIRE_SLAVE
) {
2214 firmware_upload_unregister(thp7312
->fwl
);
2215 __thp7312_power_off(thp7312
);
2219 v4l2_async_unregister_subdev(&thp7312
->sd
);
2220 v4l2_subdev_cleanup(&thp7312
->sd
);
2221 media_entity_cleanup(&thp7312
->sd
.entity
);
2222 v4l2_ctrl_handler_free(&thp7312
->ctrl_handler
);
2225 * Disable runtime PM. In case runtime PM is disabled in the kernel,
2226 * make sure to turn power off manually.
2228 pm_runtime_disable(thp7312
->dev
);
2229 if (!pm_runtime_status_suspended(thp7312
->dev
))
2230 thp7312_power_off(thp7312
);
2231 pm_runtime_set_suspended(thp7312
->dev
);
2234 static const struct of_device_id thp7312_dt_ids
[] = {
2235 { .compatible
= "thine,thp7312" },
2238 MODULE_DEVICE_TABLE(of
, thp7312_dt_ids
);
2240 static struct i2c_driver thp7312_i2c_driver
= {
2243 .pm
= &thp7312_pm_ops
,
2244 .of_match_table
= thp7312_dt_ids
,
2246 .probe
= thp7312_probe
,
2247 .remove
= thp7312_remove
,
2250 module_i2c_driver(thp7312_i2c_driver
);
2252 MODULE_DESCRIPTION("THP7312 MIPI Camera Subdev Driver");
2253 MODULE_LICENSE("GPL");