Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: i2c: Add driver for THine THP7312

The THP7312 is an external camera ISP from THine. Add a V4L2 subdev
driver for it.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
Co-developed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
[Sakari Ailus: squash a patch to fix missing mutex_unlock by Laurent.]
Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>

authored by

Paul Elder and committed by
Hans Verkuil
7a52ab41 0d9e32a8

+2258
+1
MAINTAINERS
··· 21671 21671 T: git git://linuxtv.org/media_tree.git 21672 21672 F: Documentation/devicetree/bindings/media/i2c/thine,thp7312.yaml 21673 21673 F: Documentation/userspace-api/media/drivers/thp7312.rst 21674 + F: drivers/media/i2c/thp7312.c 21674 21675 F: include/uapi/linux/thp7312.h 21675 21676 21676 21677 THUNDERBOLT DMA TRAFFIC TEST DRIVER
+16
drivers/media/i2c/Kconfig
··· 638 638 639 639 endif 640 640 641 + menu "Camera ISPs" 642 + visible if MEDIA_CAMERA_SUPPORT 643 + 644 + config VIDEO_THP7312 645 + tristate "THine THP7312 support" 646 + depends on I2C 647 + select MEDIA_CONTROLLER 648 + select V4L2_CCI_I2C 649 + select V4L2_FWNODE 650 + select VIDEO_V4L2_SUBDEV_API 651 + help 652 + This is a Video4Linux2 sensor-level driver for the THine 653 + THP7312 ISP. 654 + 655 + endmenu 656 + 641 657 menu "Lens drivers" 642 658 visible if MEDIA_CAMERA_SUPPORT 643 659
+1
drivers/media/i2c/Makefile
··· 129 129 obj-$(CONFIG_VIDEO_TDA9840) += tda9840.o 130 130 obj-$(CONFIG_VIDEO_TEA6415C) += tea6415c.o 131 131 obj-$(CONFIG_VIDEO_TEA6420) += tea6420.o 132 + obj-$(CONFIG_VIDEO_THP7312) += thp7312.o 132 133 obj-$(CONFIG_VIDEO_THS7303) += ths7303.o 133 134 obj-$(CONFIG_VIDEO_THS8200) += ths8200.o 134 135 obj-$(CONFIG_VIDEO_TLV320AIC23B) += tlv320aic23b.o
+2240
drivers/media/i2c/thp7312.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright (C) 2021 THine Electronics, Inc. 4 + * Copyright (C) 2023 Ideas on Board Oy 5 + */ 6 + 7 + #include <asm/unaligned.h> 8 + 9 + #include <linux/clk.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> 25 + 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-event.h> 31 + #include <media/v4l2-fwnode.h> 32 + #include <media/v4l2-subdev.h> 33 + 34 + #include <uapi/linux/thp7312.h> 35 + 36 + /* ISP registers */ 37 + 38 + #define THP7312_REG_FIRMWARE_VERSION_1 CCI_REG8(0xf000) 39 + #define THP7312_REG_CAMERA_STATUS CCI_REG8(0xf001) 40 + #define THP7312_REG_FIRMWARE_VERSION_2 CCI_REG8(0xf005) 41 + #define THP7312_REG_SET_OUTPUT_ENABLE CCI_REG8(0xf008) 42 + #define THP7312_OUTPUT_ENABLE 0x01 43 + #define THP7312_OUTPUT_DISABLE 0x00 44 + #define THP7312_REG_SET_OUTPUT_COLOR_COMPRESSION CCI_REG8(0xf009) 45 + #define THP7312_REG_SET_OUTPUT_COLOR_UYVY 0x00 46 + #define THP7312_REG_SET_OUTPUT_COLOR_YUY2 0x04 47 + #define THP7312_REG_FLIP_MIRROR CCI_REG8(0xf00c) 48 + #define THP7312_REG_FLIP_MIRROR_FLIP BIT(0) 49 + #define THP7312_REG_FLIP_MIRROR_MIRROR BIT(1) 50 + #define THP7312_REG_VIDEO_IMAGE_SIZE CCI_REG8(0xf00d) 51 + #define THP7312_VIDEO_IMAGE_SIZE_640x360 0x52 52 + #define THP7312_VIDEO_IMAGE_SIZE_640x460 0x03 53 + #define THP7312_VIDEO_IMAGE_SIZE_1280x720 0x0a 54 + #define THP7312_VIDEO_IMAGE_SIZE_1920x1080 0x0b 55 + #define THP7312_VIDEO_IMAGE_SIZE_3840x2160 0x0d 56 + #define THP7312_VIDEO_IMAGE_SIZE_4160x3120 0x14 57 + #define THP7312_VIDEO_IMAGE_SIZE_2016x1512 0x20 58 + #define THP7312_VIDEO_IMAGE_SIZE_2048x1536 0x21 59 + #define THP7312_REG_VIDEO_FRAME_RATE_MODE CCI_REG8(0xf00f) 60 + #define THP7312_VIDEO_FRAME_RATE_MODE1 0x80 61 + #define THP7312_VIDEO_FRAME_RATE_MODE2 0x81 62 + #define THP7312_VIDEO_FRAME_RATE_MODE3 0x82 63 + #define THP7312_REG_SET_DRIVING_MODE CCI_REG8(0xf010) 64 + #define THP7312_REG_DRIVING_MODE_STATUS CCI_REG8(0xf011) 65 + #define THP7312_REG_JPEG_COMPRESSION_FACTOR CCI_REG8(0xf01b) 66 + #define THP7312_REG_AE_EXPOSURE_COMPENSATION CCI_REG8(0xf022) 67 + #define THP7312_REG_AE_FLICKER_MODE CCI_REG8(0xf023) 68 + #define THP7312_AE_FLICKER_MODE_50 0x00 69 + #define THP7312_AE_FLICKER_MODE_60 0x01 70 + #define THP7312_AE_FLICKER_MODE_DISABLE 0x80 71 + #define THP7312_REG_AE_FIX_FRAME_RATE CCI_REG8(0xf02e) 72 + #define THP7312_REG_MANUAL_WB_RED_GAIN CCI_REG8(0xf036) 73 + #define THP7312_REG_MANUAL_WB_BLUE_GAIN CCI_REG8(0xf037) 74 + #define THP7312_REG_WB_MODE CCI_REG8(0xf039) 75 + #define THP7312_WB_MODE_AUTO 0x00 76 + #define THP7312_WB_MODE_MANUAL 0x11 77 + #define THP7312_REG_MANUAL_FOCUS_POSITION CCI_REG16(0xf03c) 78 + #define THP7312_REG_AF_CONTROL CCI_REG8(0xf040) 79 + #define THP7312_REG_AF_CONTROL_AF 0x01 80 + #define THP7312_REG_AF_CONTROL_MANUAL 0x10 81 + #define THP7312_REG_AF_CONTROL_LOCK 0x80 82 + #define THP7312_REG_AF_SETTING CCI_REG8(0xf041) 83 + #define THP7312_REG_AF_SETTING_ONESHOT_CONTRAST 0x00 84 + #define THP7312_REG_AF_SETTING_ONESHOT_PDAF 0x40 85 + #define THP7312_REG_AF_SETTING_ONESHOT_HYBRID 0x80 86 + #define THP7312_REG_AF_SETTING_CONTINUOUS_CONTRAST 0x30 87 + #define THP7312_REG_AF_SETTING_CONTINUOUS_PDAF 0x70 88 + #define THP7312_REG_AF_SETTING_CONTINUOUS_HYBRID 0xf0 89 + #define THP7312_REG_AF_SUPPORT CCI_REG8(0xf043) 90 + #define THP7312_AF_SUPPORT_PDAF BIT(1) 91 + #define THP7312_AF_SUPPORT_CONTRAST BIT(0) 92 + #define THP7312_REG_SATURATION CCI_REG8(0xf052) 93 + #define THP7312_REG_SHARPNESS CCI_REG8(0xf053) 94 + #define THP7312_REG_BRIGHTNESS CCI_REG8(0xf056) 95 + #define THP7312_REG_CONTRAST CCI_REG8(0xf057) 96 + #define THP7312_REG_NOISE_REDUCTION CCI_REG8(0xf059) 97 + #define THP7312_REG_NOISE_REDUCTION_FIXED BIT(7) 98 + 99 + #define TH7312_REG_CUSTOM_MIPI_SET CCI_REG8(0xf0f6) 100 + #define TH7312_REG_CUSTOM_MIPI_STATUS CCI_REG8(0xf0f7) 101 + #define TH7312_REG_CUSTOM_MIPI_RD CCI_REG8(0xf0f8) 102 + #define TH7312_REG_CUSTOM_MIPI_TD CCI_REG8(0xf0f9) 103 + 104 + /* 105 + * Firmware update registers. Those use a different address space than the 106 + * normal operation ISP registers. 107 + */ 108 + 109 + #define THP7312_REG_FW_DRIVABILITY CCI_REG32(0xd65c) 110 + #define THP7312_REG_FW_DEST_BANK_ADDR CCI_REG32(0xff08) 111 + #define THP7312_REG_FW_VERIFY_RESULT CCI_REG8(0xff60) 112 + #define THP7312_REG_FW_RESET_FLASH CCI_REG8(0xff61) 113 + #define THP7312_REG_FW_MEMORY_IO_SETTING CCI_REG8(0xff62) 114 + #define THP7312_FW_MEMORY_IO_GPIO0 1 115 + #define THP7312_FW_MEMORY_IO_GPIO1 0 116 + #define THP7312_REG_FW_CRC_RESULT CCI_REG32(0xff64) 117 + #define THP7312_REG_FW_STATUS CCI_REG8(0xfffc) 118 + 119 + #define THP7312_FW_VERSION(major, minor) (((major) << 8) | (minor)) 120 + #define THP7312_FW_VERSION_MAJOR(v) ((v) >> 8) 121 + #define THP7312_FW_VERSION_MINOR(v) ((v) & 0xff) 122 + 123 + enum thp7312_focus_method { 124 + THP7312_FOCUS_METHOD_CONTRAST, 125 + THP7312_FOCUS_METHOD_PDAF, 126 + THP7312_FOCUS_METHOD_HYBRID, 127 + }; 128 + 129 + /* 130 + * enum thp7312_focus_state - State of the focus handler 131 + * 132 + * @THP7312_FOCUS_STATE_MANUAL: Manual focus, controlled through the 133 + * V4L2_CID_FOCUS_ABSOLUTE control 134 + * @THP7312_FOCUS_STATE_AUTO: Continuous auto-focus 135 + * @THP7312_FOCUS_STATE_LOCKED: Lock the focus to a fixed position. This state 136 + * is entered when switching from auto to manual mode. 137 + * @THP7312_FOCUS_STATE_ONESHOT: One-shot auto-focus 138 + * 139 + * Valid transitions are as follow: 140 + * 141 + * digraph fsm { 142 + * node [shape=circle]; 143 + * 144 + * manual [label="MANUAL"]; 145 + * auto [label="AUTO"]; 146 + * locked [label="LOCKED"]; 147 + * oneshot [label="ONESHOT"]; 148 + * 149 + * manual -> auto [label="FOCUS_AUTO <- true"] 150 + * locked -> auto [label="FOCUS_AUTO <- true"] 151 + * oneshot -> auto [label="FOCUS_AUTO <- true"] 152 + * auto -> locked [label="FOCUS_AUTO <- false"] 153 + * 154 + * locked -> manual [label="FOCUS_ABSOLUTE <- *"] 155 + * oneshot -> manual [label="FOCUS_ABSOLUTE <- *"] 156 + * 157 + * manual -> oneshot [label="FOCUS_START <- *"] 158 + * locked -> oneshot [label="FOCUS_START <- *"] 159 + * } 160 + */ 161 + enum thp7312_focus_state { 162 + THP7312_FOCUS_STATE_MANUAL, 163 + THP7312_FOCUS_STATE_AUTO, 164 + THP7312_FOCUS_STATE_LOCKED, 165 + THP7312_FOCUS_STATE_ONESHOT, 166 + }; 167 + 168 + enum thp7312_boot_mode { 169 + THP7312_BOOT_MODE_2WIRE_SLAVE = 0, 170 + THP7312_BOOT_MODE_SPI_MASTER = 1, 171 + }; 172 + 173 + struct thp7312_frame_rate { 174 + u32 fps; 175 + u32 link_freq; 176 + u8 reg_frame_rate_mode; 177 + }; 178 + 179 + struct thp7312_mode_info { 180 + u32 width; 181 + u32 height; 182 + u8 reg_image_size; 183 + const struct thp7312_frame_rate *rates; 184 + }; 185 + 186 + static const u32 thp7312_colour_fmts[] = { 187 + MEDIA_BUS_FMT_YUYV8_1X16, 188 + }; 189 + 190 + /* regulator supplies */ 191 + static const char * const thp7312_supply_name[] = { 192 + "vddcore", 193 + "vhtermrx", 194 + "vddtx", 195 + "vddhost", 196 + "vddcmos", 197 + "vddgpio-0", 198 + "vddgpio-1", 199 + }; 200 + 201 + static const struct thp7312_mode_info thp7312_mode_info_data[] = { 202 + { 203 + .width = 1920, 204 + .height = 1080, 205 + .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_1920x1080, 206 + .rates = (const struct thp7312_frame_rate[]) { 207 + { 30, 300000000, 0x81 }, 208 + { 60, 387500000, 0x82 }, 209 + { 0 } 210 + }, 211 + }, { 212 + .width = 2048, 213 + .height = 1536, 214 + .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_2048x1536, 215 + .rates = (const struct thp7312_frame_rate[]) { 216 + { 30, 300000000, 0x81 }, 217 + { 0 } 218 + } 219 + }, { 220 + .width = 3840, 221 + .height = 2160, 222 + .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_3840x2160, 223 + .rates = (const struct thp7312_frame_rate[]) { 224 + { 30, 600000000, 0x81 }, 225 + { 0 } 226 + }, 227 + }, { 228 + .width = 4160, 229 + .height = 3120, 230 + .reg_image_size = THP7312_VIDEO_IMAGE_SIZE_4160x3120, 231 + .rates = (const struct thp7312_frame_rate[]) { 232 + { 20, 600000000, 0x81 }, 233 + { 0 } 234 + }, 235 + }, 236 + }; 237 + 238 + struct thp7312_device; 239 + 240 + struct thp7312_sensor_info { 241 + const char *model; 242 + }; 243 + 244 + struct thp7312_sensor { 245 + const struct thp7312_sensor_info *info; 246 + u8 lane_remap; 247 + }; 248 + 249 + struct thp7312_device { 250 + struct device *dev; 251 + struct regmap *regmap; 252 + 253 + struct v4l2_subdev sd; 254 + struct media_pad pad; 255 + 256 + struct gpio_desc *reset_gpio; 257 + struct regulator_bulk_data supplies[ARRAY_SIZE(thp7312_supply_name)]; 258 + struct clk *iclk; 259 + 260 + u8 lane_remap; 261 + 262 + struct thp7312_sensor sensors[1]; 263 + 264 + enum thp7312_boot_mode boot_mode; 265 + 266 + struct v4l2_ctrl_handler ctrl_handler; 267 + bool ctrls_applied; 268 + 269 + /* These are protected by v4l2 active state */ 270 + const struct thp7312_mode_info *current_mode; 271 + const struct thp7312_frame_rate *current_rate; 272 + s64 link_freq; 273 + 274 + struct { 275 + struct v4l2_ctrl *hflip; 276 + struct v4l2_ctrl *vflip; 277 + }; 278 + 279 + struct { 280 + struct v4l2_ctrl *focus_auto; 281 + struct v4l2_ctrl *focus_absolute; 282 + struct v4l2_ctrl *focus_start; 283 + struct v4l2_ctrl *focus_method; 284 + }; 285 + 286 + enum thp7312_focus_state focus_state; 287 + 288 + struct { 289 + struct v4l2_ctrl *noise_reduction_auto; 290 + struct v4l2_ctrl *noise_reduction_absolute; 291 + }; 292 + 293 + /* Lock to protect fw_cancel */ 294 + struct mutex fw_lock; 295 + struct fw_upload *fwl; 296 + u8 *fw_write_buf; 297 + bool fw_cancel; 298 + 299 + u16 fw_version; 300 + }; 301 + 302 + static const struct thp7312_sensor_info thp7312_sensor_info[] = { 303 + { 304 + .model = "sony,imx258", 305 + }, 306 + }; 307 + 308 + static inline struct thp7312_device *to_thp7312_dev(struct v4l2_subdev *sd) 309 + { 310 + return container_of(sd, struct thp7312_device, sd); 311 + } 312 + 313 + /* ----------------------------------------------------------------------------- 314 + * Device Access & Configuration 315 + */ 316 + 317 + #define thp7312_read_poll_timeout(dev, addr, val, cond, sleep_us, timeout_us) \ 318 + ({ \ 319 + int __ret, __err; \ 320 + __ret = read_poll_timeout(cci_read, __err, __err || (cond), sleep_us, \ 321 + timeout_us, false, (dev)->regmap, addr, \ 322 + &(val), NULL); \ 323 + __ret ? : __err; \ 324 + }) 325 + 326 + static int thp7312_map_data_lanes(u8 *lane_remap, const u8 *lanes, u8 num_lanes) 327 + { 328 + u8 used_lanes = 0; 329 + u8 val = 0; 330 + unsigned int i; 331 + 332 + /* 333 + * The value that we write to the register is the index in the 334 + * data-lanes array, so we need to do a conversion. Do this in the same 335 + * pass as validating data-lanes. 336 + */ 337 + for (i = 0; i < num_lanes; i++) { 338 + if (lanes[i] < 1 || lanes[i] > 4) 339 + return -EINVAL; 340 + 341 + if (used_lanes & (BIT(lanes[i]))) 342 + return -EINVAL; 343 + 344 + used_lanes |= BIT(lanes[i]); 345 + 346 + /* 347 + * data-lanes is 1-indexed while the field position in the 348 + * register is 0-indexed. 349 + */ 350 + val |= i << ((lanes[i] - 1) * 2); 351 + } 352 + 353 + *lane_remap = val; 354 + 355 + return 0; 356 + } 357 + 358 + static int thp7312_set_mipi_lanes(struct thp7312_device *thp7312) 359 + { 360 + struct device *dev = thp7312->dev; 361 + int ret = 0; 362 + u64 val; 363 + 364 + cci_write(thp7312->regmap, TH7312_REG_CUSTOM_MIPI_RD, 365 + thp7312->sensors[0].lane_remap, &ret); 366 + cci_write(thp7312->regmap, TH7312_REG_CUSTOM_MIPI_TD, 367 + thp7312->lane_remap, &ret); 368 + cci_write(thp7312->regmap, TH7312_REG_CUSTOM_MIPI_SET, 1, &ret); 369 + 370 + if (ret) 371 + return ret; 372 + 373 + ret = thp7312_read_poll_timeout(thp7312, TH7312_REG_CUSTOM_MIPI_STATUS, 374 + val, val == 0x00, 100000, 2000000); 375 + if (ret) { 376 + dev_err(dev, "Failed to poll MIPI lane status: %d\n", ret); 377 + return ret; 378 + } 379 + 380 + return 0; 381 + } 382 + 383 + static int thp7312_change_mode(struct thp7312_device *thp7312, 384 + const struct thp7312_mode_info *mode, 385 + const struct thp7312_frame_rate *rate) 386 + { 387 + struct device *dev = thp7312->dev; 388 + u64 val = 0; 389 + int ret; 390 + 391 + ret = thp7312_read_poll_timeout(thp7312, THP7312_REG_CAMERA_STATUS, val, 392 + val == 0x80, 20000, 200000); 393 + if (ret < 0) { 394 + dev_err(dev, "%s(): failed to poll ISP: %d\n", __func__, ret); 395 + return ret; 396 + } 397 + 398 + cci_write(thp7312->regmap, THP7312_REG_VIDEO_IMAGE_SIZE, 399 + mode->reg_image_size, &ret); 400 + cci_write(thp7312->regmap, THP7312_REG_VIDEO_FRAME_RATE_MODE, 401 + rate->reg_frame_rate_mode, &ret); 402 + cci_write(thp7312->regmap, THP7312_REG_JPEG_COMPRESSION_FACTOR, 0x5e, 403 + &ret); 404 + cci_write(thp7312->regmap, THP7312_REG_SET_DRIVING_MODE, 0x01, &ret); 405 + 406 + if (ret) 407 + return ret; 408 + 409 + ret = thp7312_read_poll_timeout(thp7312, THP7312_REG_DRIVING_MODE_STATUS, 410 + val, val == 0x01, 20000, 100000); 411 + if (ret < 0) { 412 + dev_err(dev, "%s(): failed\n", __func__); 413 + return ret; 414 + } 415 + 416 + return 0; 417 + } 418 + 419 + static int thp7312_set_framefmt(struct thp7312_device *thp7312, 420 + struct v4l2_mbus_framefmt *format) 421 + { 422 + u8 val; 423 + 424 + switch (format->code) { 425 + case MEDIA_BUS_FMT_UYVY8_1X16: 426 + /* YUV422, UYVY */ 427 + val = THP7312_REG_SET_OUTPUT_COLOR_UYVY; 428 + break; 429 + case MEDIA_BUS_FMT_YUYV8_1X16: 430 + /* YUV422, YUYV */ 431 + val = THP7312_REG_SET_OUTPUT_COLOR_YUY2; 432 + break; 433 + default: 434 + /* Should never happen */ 435 + return -EINVAL; 436 + } 437 + 438 + return cci_write(thp7312->regmap, 439 + THP7312_REG_SET_OUTPUT_COLOR_COMPRESSION, val, NULL); 440 + } 441 + 442 + static int thp7312_init_mode(struct thp7312_device *thp7312, 443 + struct v4l2_subdev_state *sd_state) 444 + { 445 + struct v4l2_mbus_framefmt *fmt; 446 + int ret; 447 + 448 + fmt = v4l2_subdev_state_get_format(sd_state, 0); 449 + 450 + ret = thp7312_set_framefmt(thp7312, fmt); 451 + if (ret) 452 + return ret; 453 + 454 + return thp7312_change_mode(thp7312, thp7312->current_mode, 455 + thp7312->current_rate); 456 + } 457 + 458 + static int thp7312_stream_enable(struct thp7312_device *thp7312, bool enable) 459 + { 460 + return cci_write(thp7312->regmap, THP7312_REG_SET_OUTPUT_ENABLE, 461 + enable ? THP7312_OUTPUT_ENABLE : THP7312_OUTPUT_DISABLE, 462 + NULL); 463 + } 464 + 465 + static int thp7312_check_status_stream_mode(struct thp7312_device *thp7312) 466 + { 467 + struct device *dev = thp7312->dev; 468 + u64 status = 0; 469 + int ret; 470 + 471 + while (status != 0x80) { 472 + ret = cci_read(thp7312->regmap, THP7312_REG_CAMERA_STATUS, 473 + &status, NULL); 474 + if (ret) 475 + return ret; 476 + 477 + if (status == 0x80) { 478 + dev_dbg(dev, "Camera initialization done\n"); 479 + return 0; 480 + } 481 + 482 + if (status != 0x00) { 483 + dev_err(dev, "Invalid camera status %llx\n", status); 484 + return -EINVAL; 485 + } 486 + 487 + dev_dbg(dev, "Camera initializing...\n"); 488 + usleep_range(70000, 80000); 489 + } 490 + 491 + return 0; 492 + } 493 + 494 + static void thp7312_reset(struct thp7312_device *thp7312) 495 + { 496 + unsigned long rate; 497 + 498 + gpiod_set_value_cansleep(thp7312->reset_gpio, 1); 499 + 500 + /* 501 + * The minimum reset duration is 8 clock cycles, make it 10 to provide 502 + * a safety margin. 503 + */ 504 + rate = clk_get_rate(thp7312->iclk); 505 + fsleep(DIV_ROUND_UP(10 * USEC_PER_SEC, rate)); 506 + 507 + gpiod_set_value_cansleep(thp7312->reset_gpio, 0); 508 + 509 + /* 510 + * TODO: The documentation states that the device needs 2ms to 511 + * initialize after reset is deasserted. It then proceeds to load the 512 + * firmware from the flash memory, which takes an unspecified amount of 513 + * time. Check if this delay could be reduced. 514 + */ 515 + fsleep(300000); 516 + } 517 + 518 + /* ----------------------------------------------------------------------------- 519 + * Power Management 520 + */ 521 + 522 + static void __thp7312_power_off(struct thp7312_device *thp7312) 523 + { 524 + regulator_bulk_disable(ARRAY_SIZE(thp7312->supplies), thp7312->supplies); 525 + clk_disable_unprepare(thp7312->iclk); 526 + } 527 + 528 + static void thp7312_power_off(struct thp7312_device *thp7312) 529 + { 530 + __thp7312_power_off(thp7312); 531 + } 532 + 533 + static int __thp7312_power_on(struct thp7312_device *thp7312) 534 + { 535 + struct device *dev = thp7312->dev; 536 + int ret; 537 + 538 + ret = regulator_bulk_enable(ARRAY_SIZE(thp7312->supplies), 539 + thp7312->supplies); 540 + if (ret < 0) 541 + return ret; 542 + 543 + ret = clk_prepare_enable(thp7312->iclk); 544 + if (ret < 0) { 545 + dev_err(dev, "clk prepare enable failed\n"); 546 + regulator_bulk_disable(ARRAY_SIZE(thp7312->supplies), 547 + thp7312->supplies); 548 + return ret; 549 + } 550 + 551 + /* 552 + * We cannot assume that turning off and on again will reset, so do a 553 + * software reset on power up. 554 + */ 555 + thp7312_reset(thp7312); 556 + 557 + return 0; 558 + } 559 + 560 + static int thp7312_power_on(struct thp7312_device *thp7312) 561 + { 562 + int ret; 563 + 564 + ret = __thp7312_power_on(thp7312); 565 + if (ret < 0) 566 + return ret; 567 + 568 + ret = thp7312_check_status_stream_mode(thp7312); 569 + if (ret < 0) 570 + goto error; 571 + 572 + ret = thp7312_set_mipi_lanes(thp7312); 573 + if (ret) 574 + goto error; 575 + 576 + return 0; 577 + 578 + error: 579 + thp7312_power_off(thp7312); 580 + return ret; 581 + } 582 + 583 + static int __maybe_unused thp7312_pm_runtime_suspend(struct device *dev) 584 + { 585 + struct v4l2_subdev *sd = dev_get_drvdata(dev); 586 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 587 + 588 + thp7312_power_off(thp7312); 589 + 590 + thp7312->ctrls_applied = false; 591 + 592 + return 0; 593 + } 594 + 595 + static int __maybe_unused thp7312_pm_runtime_resume(struct device *dev) 596 + { 597 + struct v4l2_subdev *sd = dev_get_drvdata(dev); 598 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 599 + 600 + return thp7312_power_on(thp7312); 601 + } 602 + 603 + static const struct dev_pm_ops thp7312_pm_ops = { 604 + SET_RUNTIME_PM_OPS(thp7312_pm_runtime_suspend, 605 + thp7312_pm_runtime_resume, NULL) 606 + }; 607 + 608 + /* ----------------------------------------------------------------------------- 609 + * V4L2 Subdev Operations 610 + */ 611 + 612 + static bool thp7312_find_bus_code(u32 code) 613 + { 614 + unsigned int i; 615 + 616 + for (i = 0; i < ARRAY_SIZE(thp7312_colour_fmts); ++i) { 617 + if (thp7312_colour_fmts[i] == code) 618 + return true; 619 + } 620 + 621 + return false; 622 + } 623 + 624 + static const struct thp7312_mode_info * 625 + thp7312_find_mode(unsigned int width, unsigned int height, bool nearest) 626 + { 627 + const struct thp7312_mode_info *mode; 628 + 629 + mode = v4l2_find_nearest_size(thp7312_mode_info_data, 630 + ARRAY_SIZE(thp7312_mode_info_data), 631 + width, height, width, height); 632 + 633 + if (!nearest && (mode->width != width || mode->height != height)) 634 + return NULL; 635 + 636 + return mode; 637 + } 638 + 639 + static void thp7312_set_frame_rate(struct thp7312_device *thp7312, 640 + const struct thp7312_frame_rate *rate) 641 + { 642 + thp7312->link_freq = rate->link_freq; 643 + thp7312->current_rate = rate; 644 + } 645 + 646 + static int thp7312_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 >= ARRAY_SIZE(thp7312_colour_fmts)) 651 + return -EINVAL; 652 + 653 + code->code = thp7312_colour_fmts[code->index]; 654 + 655 + return 0; 656 + } 657 + 658 + static int thp7312_enum_frame_size(struct v4l2_subdev *sd, 659 + struct v4l2_subdev_state *sd_state, 660 + struct v4l2_subdev_frame_size_enum *fse) 661 + { 662 + if (!thp7312_find_bus_code(fse->code)) 663 + return -EINVAL; 664 + 665 + if (fse->index >= ARRAY_SIZE(thp7312_mode_info_data)) 666 + return -EINVAL; 667 + 668 + fse->min_width = thp7312_mode_info_data[fse->index].width; 669 + fse->max_width = fse->min_width; 670 + fse->min_height = thp7312_mode_info_data[fse->index].height; 671 + fse->max_height = fse->min_height; 672 + 673 + return 0; 674 + } 675 + 676 + static int thp7312_enum_frame_interval(struct v4l2_subdev *sd, 677 + struct v4l2_subdev_state *sd_state, 678 + struct v4l2_subdev_frame_interval_enum *fie) 679 + { 680 + const struct thp7312_frame_rate *rate; 681 + const struct thp7312_mode_info *mode; 682 + unsigned int index = fie->index; 683 + 684 + if (!thp7312_find_bus_code(fie->code)) 685 + return -EINVAL; 686 + 687 + mode = thp7312_find_mode(fie->width, fie->height, false); 688 + if (!mode) 689 + return -EINVAL; 690 + 691 + for (rate = mode->rates; rate->fps; ++rate, --index) { 692 + if (!index) { 693 + fie->interval.numerator = 1; 694 + fie->interval.denominator = rate->fps; 695 + 696 + return 0; 697 + } 698 + } 699 + 700 + return -EINVAL; 701 + } 702 + 703 + static int thp7312_set_fmt(struct v4l2_subdev *sd, 704 + struct v4l2_subdev_state *sd_state, 705 + struct v4l2_subdev_format *format) 706 + { 707 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 708 + struct v4l2_mbus_framefmt *mbus_fmt = &format->format; 709 + struct v4l2_mbus_framefmt *fmt; 710 + const struct thp7312_mode_info *mode; 711 + 712 + if (!thp7312_find_bus_code(mbus_fmt->code)) 713 + mbus_fmt->code = thp7312_colour_fmts[0]; 714 + 715 + mode = thp7312_find_mode(mbus_fmt->width, mbus_fmt->height, true); 716 + 717 + fmt = v4l2_subdev_state_get_format(sd_state, 0); 718 + 719 + fmt->code = mbus_fmt->code; 720 + fmt->width = mode->width; 721 + fmt->height = mode->height; 722 + fmt->colorspace = V4L2_COLORSPACE_SRGB; 723 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 724 + fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 725 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 726 + 727 + *mbus_fmt = *fmt; 728 + 729 + if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 730 + thp7312->current_mode = mode; 731 + thp7312_set_frame_rate(thp7312, &mode->rates[0]); 732 + } 733 + 734 + return 0; 735 + } 736 + 737 + static int thp7312_g_frame_interval(struct v4l2_subdev *sd, 738 + struct v4l2_subdev_frame_interval *fi) 739 + { 740 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 741 + struct v4l2_subdev_state *sd_state; 742 + 743 + sd_state = v4l2_subdev_lock_and_get_active_state(sd); 744 + fi->interval.numerator = 1; 745 + fi->interval.denominator = thp7312->current_rate->fps; 746 + v4l2_subdev_unlock_state(sd_state); 747 + 748 + return 0; 749 + } 750 + 751 + static int thp7312_s_frame_interval(struct v4l2_subdev *sd, 752 + struct v4l2_subdev_frame_interval *fi) 753 + { 754 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 755 + const struct thp7312_mode_info *mode; 756 + const struct thp7312_frame_rate *best_rate = NULL; 757 + const struct thp7312_frame_rate *rate; 758 + struct v4l2_subdev_state *sd_state; 759 + unsigned int best_delta = UINT_MAX; 760 + unsigned int fps; 761 + 762 + /* Avoid divisions by 0, pick the highest frame if the interval is 0. */ 763 + fps = fi->interval.numerator 764 + ? DIV_ROUND_CLOSEST(fi->interval.denominator, fi->interval.numerator) 765 + : UINT_MAX; 766 + 767 + sd_state = v4l2_subdev_lock_and_get_active_state(sd); 768 + 769 + mode = thp7312->current_mode; 770 + 771 + for (rate = mode->rates; rate->fps && best_delta; ++rate) { 772 + unsigned int delta = abs(rate->fps - fps); 773 + 774 + if (delta <= best_delta) { 775 + best_delta = delta; 776 + best_rate = rate; 777 + } 778 + } 779 + 780 + thp7312_set_frame_rate(thp7312, best_rate); 781 + 782 + v4l2_subdev_unlock_state(sd_state); 783 + 784 + fi->interval.numerator = 1; 785 + fi->interval.denominator = best_rate->fps; 786 + 787 + return 0; 788 + } 789 + 790 + static int thp7312_s_stream(struct v4l2_subdev *sd, int enable) 791 + { 792 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 793 + struct v4l2_subdev_state *sd_state; 794 + int ret; 795 + 796 + sd_state = v4l2_subdev_lock_and_get_active_state(sd); 797 + 798 + if (!enable) { 799 + thp7312_stream_enable(thp7312, false); 800 + 801 + pm_runtime_mark_last_busy(thp7312->dev); 802 + pm_runtime_put_autosuspend(thp7312->dev); 803 + 804 + v4l2_subdev_unlock_state(sd_state); 805 + 806 + return 0; 807 + } 808 + 809 + ret = pm_runtime_resume_and_get(thp7312->dev); 810 + if (ret) 811 + goto finish_unlock; 812 + 813 + ret = thp7312_init_mode(thp7312, sd_state); 814 + if (ret) 815 + goto finish_pm; 816 + 817 + if (!thp7312->ctrls_applied) { 818 + ret = __v4l2_ctrl_handler_setup(&thp7312->ctrl_handler); 819 + if (ret) 820 + goto finish_pm; 821 + 822 + thp7312->ctrls_applied = true; 823 + } 824 + 825 + ret = thp7312_stream_enable(thp7312, true); 826 + if (ret) 827 + goto finish_pm; 828 + 829 + goto finish_unlock; 830 + 831 + finish_pm: 832 + pm_runtime_mark_last_busy(thp7312->dev); 833 + pm_runtime_put_autosuspend(thp7312->dev); 834 + finish_unlock: 835 + v4l2_subdev_unlock_state(sd_state); 836 + 837 + return ret; 838 + } 839 + 840 + static int thp7312_init_cfg(struct v4l2_subdev *sd, 841 + struct v4l2_subdev_state *sd_state) 842 + { 843 + const struct thp7312_mode_info *default_mode = &thp7312_mode_info_data[0]; 844 + struct v4l2_mbus_framefmt *fmt; 845 + 846 + fmt = v4l2_subdev_state_get_format(sd_state, 0); 847 + 848 + /* 849 + * default init sequence initialize thp7312 to 850 + * YUV422 YUYV VGA@30fps 851 + */ 852 + fmt->code = MEDIA_BUS_FMT_YUYV8_1X16; 853 + fmt->colorspace = V4L2_COLORSPACE_SRGB; 854 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 855 + fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 856 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 857 + fmt->width = default_mode->width; 858 + fmt->height = default_mode->height; 859 + fmt->field = V4L2_FIELD_NONE; 860 + 861 + return 0; 862 + } 863 + 864 + static const struct v4l2_subdev_core_ops thp7312_core_ops = { 865 + .log_status = v4l2_ctrl_subdev_log_status, 866 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 867 + .unsubscribe_event = v4l2_event_subdev_unsubscribe, 868 + }; 869 + 870 + static const struct v4l2_subdev_video_ops thp7312_video_ops = { 871 + .g_frame_interval = thp7312_g_frame_interval, 872 + .s_frame_interval = thp7312_s_frame_interval, 873 + .s_stream = thp7312_s_stream, 874 + }; 875 + 876 + static const struct v4l2_subdev_pad_ops thp7312_pad_ops = { 877 + .enum_mbus_code = thp7312_enum_mbus_code, 878 + .init_cfg = thp7312_init_cfg, 879 + .get_fmt = v4l2_subdev_get_fmt, 880 + .set_fmt = thp7312_set_fmt, 881 + .enum_frame_size = thp7312_enum_frame_size, 882 + .enum_frame_interval = thp7312_enum_frame_interval, 883 + }; 884 + 885 + static const struct v4l2_subdev_ops thp7312_subdev_ops = { 886 + .core = &thp7312_core_ops, 887 + .video = &thp7312_video_ops, 888 + .pad = &thp7312_pad_ops, 889 + }; 890 + 891 + /* ----------------------------------------------------------------------------- 892 + * V4L2 Control Operations 893 + */ 894 + 895 + static inline struct thp7312_device *to_thp7312_from_ctrl(struct v4l2_ctrl *ctrl) 896 + { 897 + return container_of(ctrl->handler, struct thp7312_device, ctrl_handler); 898 + } 899 + 900 + /* 0: 3000cm, 18: 8cm */ 901 + static const u16 thp7312_focus_values[] = { 902 + 3000, 1000, 600, 450, 350, 903 + 290, 240, 200, 170, 150, 904 + 140, 130, 120, 110, 100, 905 + 93, 87, 83, 80, 906 + }; 907 + 908 + static int thp7312_set_focus(struct thp7312_device *thp7312) 909 + { 910 + enum thp7312_focus_state new_state = thp7312->focus_state; 911 + bool continuous; 912 + u8 af_control; 913 + u8 af_setting; 914 + int ret = 0; 915 + 916 + /* Start by programming the manual focus position if it has changed. */ 917 + if (thp7312->focus_absolute->is_new) { 918 + unsigned int value; 919 + 920 + value = thp7312_focus_values[thp7312->focus_absolute->val]; 921 + 922 + ret = cci_write(thp7312->regmap, 923 + THP7312_REG_MANUAL_FOCUS_POSITION, value, NULL); 924 + if (ret) 925 + return ret; 926 + } 927 + 928 + /* Calculate the new focus state. */ 929 + switch (thp7312->focus_state) { 930 + case THP7312_FOCUS_STATE_MANUAL: 931 + default: 932 + if (thp7312->focus_auto->val) 933 + new_state = THP7312_FOCUS_STATE_AUTO; 934 + else if (thp7312->focus_start->is_new) 935 + new_state = THP7312_FOCUS_STATE_ONESHOT; 936 + break; 937 + 938 + case THP7312_FOCUS_STATE_AUTO: 939 + if (!thp7312->focus_auto->val) 940 + new_state = THP7312_FOCUS_STATE_LOCKED; 941 + break; 942 + 943 + case THP7312_FOCUS_STATE_LOCKED: 944 + if (thp7312->focus_auto->val) 945 + new_state = THP7312_FOCUS_STATE_AUTO; 946 + else if (thp7312->focus_start->is_new) 947 + new_state = THP7312_FOCUS_STATE_ONESHOT; 948 + else if (thp7312->focus_absolute->is_new) 949 + new_state = THP7312_FOCUS_STATE_MANUAL; 950 + break; 951 + 952 + case THP7312_FOCUS_STATE_ONESHOT: 953 + if (thp7312->focus_auto->val) 954 + new_state = THP7312_FOCUS_STATE_AUTO; 955 + else if (thp7312->focus_start->is_new) 956 + new_state = THP7312_FOCUS_STATE_ONESHOT; 957 + else if (thp7312->focus_absolute->is_new) 958 + new_state = THP7312_FOCUS_STATE_MANUAL; 959 + break; 960 + } 961 + 962 + /* 963 + * If neither the state nor the focus method has changed, and no new 964 + * one-shot focus is requested, there's nothing new to program to the 965 + * hardware. 966 + */ 967 + if (thp7312->focus_state == new_state && 968 + !thp7312->focus_method->is_new && !thp7312->focus_start->is_new) 969 + return 0; 970 + 971 + continuous = new_state == THP7312_FOCUS_STATE_MANUAL || 972 + new_state == THP7312_FOCUS_STATE_ONESHOT; 973 + 974 + switch (thp7312->focus_method->val) { 975 + case THP7312_FOCUS_METHOD_CONTRAST: 976 + default: 977 + af_setting = continuous 978 + ? THP7312_REG_AF_SETTING_CONTINUOUS_CONTRAST 979 + : THP7312_REG_AF_SETTING_ONESHOT_CONTRAST; 980 + break; 981 + case THP7312_FOCUS_METHOD_PDAF: 982 + af_setting = continuous 983 + ? THP7312_REG_AF_SETTING_CONTINUOUS_PDAF 984 + : THP7312_REG_AF_SETTING_ONESHOT_PDAF; 985 + break; 986 + case THP7312_FOCUS_METHOD_HYBRID: 987 + af_setting = continuous 988 + ? THP7312_REG_AF_SETTING_CONTINUOUS_HYBRID 989 + : THP7312_REG_AF_SETTING_ONESHOT_HYBRID; 990 + break; 991 + } 992 + 993 + switch (new_state) { 994 + case THP7312_FOCUS_STATE_MANUAL: 995 + default: 996 + af_control = THP7312_REG_AF_CONTROL_MANUAL; 997 + break; 998 + case THP7312_FOCUS_STATE_AUTO: 999 + case THP7312_FOCUS_STATE_ONESHOT: 1000 + af_control = THP7312_REG_AF_CONTROL_AF; 1001 + break; 1002 + case THP7312_FOCUS_STATE_LOCKED: 1003 + af_control = THP7312_REG_AF_CONTROL_LOCK; 1004 + break; 1005 + } 1006 + 1007 + cci_write(thp7312->regmap, THP7312_REG_AF_SETTING, af_setting, &ret); 1008 + 1009 + if (new_state == THP7312_FOCUS_STATE_MANUAL && 1010 + (thp7312->focus_state == THP7312_FOCUS_STATE_AUTO || 1011 + thp7312->focus_state == THP7312_FOCUS_STATE_ONESHOT)) { 1012 + /* When switching to manual state, lock AF first. */ 1013 + cci_write(thp7312->regmap, THP7312_REG_AF_CONTROL, 1014 + THP7312_REG_AF_CONTROL_LOCK, &ret); 1015 + } 1016 + 1017 + cci_write(thp7312->regmap, THP7312_REG_AF_CONTROL, af_control, &ret); 1018 + 1019 + if (ret) 1020 + return ret; 1021 + 1022 + thp7312->focus_state = new_state; 1023 + 1024 + return 0; 1025 + } 1026 + 1027 + static int thp7312_s_ctrl(struct v4l2_ctrl *ctrl) 1028 + { 1029 + struct thp7312_device *thp7312 = to_thp7312_from_ctrl(ctrl); 1030 + int ret = 0; 1031 + u8 value; 1032 + 1033 + if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE) 1034 + return -EINVAL; 1035 + 1036 + if (!pm_runtime_get_if_active(thp7312->dev, true)) 1037 + return 0; 1038 + 1039 + switch (ctrl->id) { 1040 + case V4L2_CID_BRIGHTNESS: 1041 + cci_write(thp7312->regmap, THP7312_REG_BRIGHTNESS, 1042 + ctrl->val + 10, &ret); 1043 + break; 1044 + 1045 + case V4L2_CID_THP7312_LOW_LIGHT_COMPENSATION: 1046 + /* 0 = Auto adjust frame rate, 1 = Fix frame rate */ 1047 + cci_write(thp7312->regmap, THP7312_REG_AE_FIX_FRAME_RATE, 1048 + ctrl->val ? 0 : 1, &ret); 1049 + break; 1050 + 1051 + case V4L2_CID_FOCUS_AUTO: 1052 + case V4L2_CID_FOCUS_ABSOLUTE: 1053 + case V4L2_CID_AUTO_FOCUS_START: 1054 + case V4L2_CID_THP7312_AUTO_FOCUS_METHOD: 1055 + ret = thp7312_set_focus(thp7312); 1056 + break; 1057 + 1058 + case V4L2_CID_HFLIP: 1059 + case V4L2_CID_VFLIP: 1060 + value = (thp7312->hflip->val ? THP7312_REG_FLIP_MIRROR_MIRROR : 0) 1061 + | (thp7312->vflip->val ? THP7312_REG_FLIP_MIRROR_FLIP : 0); 1062 + 1063 + cci_write(thp7312->regmap, THP7312_REG_FLIP_MIRROR, value, &ret); 1064 + break; 1065 + 1066 + case V4L2_CID_THP7312_NOISE_REDUCTION_AUTO: 1067 + case V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE: 1068 + value = thp7312->noise_reduction_auto->val ? 0 1069 + : THP7312_REG_NOISE_REDUCTION_FIXED | 1070 + thp7312->noise_reduction_absolute->val; 1071 + 1072 + cci_write(thp7312->regmap, THP7312_REG_NOISE_REDUCTION, value, 1073 + &ret); 1074 + break; 1075 + 1076 + case V4L2_CID_AUTO_WHITE_BALANCE: 1077 + value = ctrl->val ? THP7312_WB_MODE_AUTO : THP7312_WB_MODE_MANUAL; 1078 + 1079 + cci_write(thp7312->regmap, THP7312_REG_WB_MODE, value, &ret); 1080 + break; 1081 + 1082 + case V4L2_CID_RED_BALANCE: 1083 + cci_write(thp7312->regmap, THP7312_REG_MANUAL_WB_RED_GAIN, 1084 + ctrl->val, &ret); 1085 + break; 1086 + 1087 + case V4L2_CID_BLUE_BALANCE: 1088 + cci_write(thp7312->regmap, THP7312_REG_MANUAL_WB_BLUE_GAIN, 1089 + ctrl->val, &ret); 1090 + break; 1091 + 1092 + case V4L2_CID_AUTO_EXPOSURE_BIAS: 1093 + cci_write(thp7312->regmap, THP7312_REG_AE_EXPOSURE_COMPENSATION, 1094 + ctrl->val, &ret); 1095 + break; 1096 + 1097 + case V4L2_CID_POWER_LINE_FREQUENCY: 1098 + if (ctrl->val == V4L2_CID_POWER_LINE_FREQUENCY_60HZ) { 1099 + value = THP7312_AE_FLICKER_MODE_60; 1100 + } else if (ctrl->val == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) { 1101 + value = THP7312_AE_FLICKER_MODE_50; 1102 + } else { 1103 + if (thp7312->fw_version == THP7312_FW_VERSION(40, 3)) { 1104 + /* THP7312_AE_FLICKER_MODE_DISABLE is not supported */ 1105 + value = THP7312_AE_FLICKER_MODE_50; 1106 + } else { 1107 + value = THP7312_AE_FLICKER_MODE_DISABLE; 1108 + } 1109 + } 1110 + 1111 + cci_write(thp7312->regmap, THP7312_REG_AE_FLICKER_MODE, 1112 + value, &ret); 1113 + break; 1114 + 1115 + case V4L2_CID_SATURATION: 1116 + cci_write(thp7312->regmap, THP7312_REG_SATURATION, 1117 + ctrl->val, &ret); 1118 + break; 1119 + 1120 + case V4L2_CID_CONTRAST: 1121 + cci_write(thp7312->regmap, THP7312_REG_CONTRAST, 1122 + ctrl->val, &ret); 1123 + break; 1124 + 1125 + case V4L2_CID_SHARPNESS: 1126 + cci_write(thp7312->regmap, THP7312_REG_SHARPNESS, 1127 + ctrl->val, &ret); 1128 + break; 1129 + 1130 + default: 1131 + break; 1132 + } 1133 + 1134 + pm_runtime_mark_last_busy(thp7312->dev); 1135 + pm_runtime_put_autosuspend(thp7312->dev); 1136 + 1137 + return ret; 1138 + } 1139 + 1140 + static const struct v4l2_ctrl_ops thp7312_ctrl_ops = { 1141 + .s_ctrl = thp7312_s_ctrl, 1142 + }; 1143 + 1144 + /* 1145 + * Refer to Documentation/userspace-api/media/drivers/thp7312.rst for details. 1146 + */ 1147 + static const struct v4l2_ctrl_config thp7312_ctrl_focus_method_cdaf = { 1148 + .ops = &thp7312_ctrl_ops, 1149 + .id = V4L2_CID_THP7312_AUTO_FOCUS_METHOD, 1150 + .name = "Auto-Focus Method", 1151 + .type = V4L2_CTRL_TYPE_INTEGER, 1152 + .min = THP7312_FOCUS_METHOD_CONTRAST, 1153 + .def = THP7312_FOCUS_METHOD_CONTRAST, 1154 + .max = THP7312_FOCUS_METHOD_CONTRAST, 1155 + .step = 1, 1156 + }; 1157 + 1158 + static const struct v4l2_ctrl_config thp7312_ctrl_focus_method_pdaf = { 1159 + .ops = &thp7312_ctrl_ops, 1160 + .id = V4L2_CID_THP7312_AUTO_FOCUS_METHOD, 1161 + .name = "Auto-Focus Method", 1162 + .type = V4L2_CTRL_TYPE_INTEGER, 1163 + .min = THP7312_FOCUS_METHOD_CONTRAST, 1164 + .def = THP7312_FOCUS_METHOD_HYBRID, 1165 + .max = THP7312_FOCUS_METHOD_HYBRID, 1166 + .step = 1, 1167 + }; 1168 + 1169 + static const struct v4l2_ctrl_config thp7312_v4l2_ctrls_custom[] = { 1170 + { 1171 + .ops = &thp7312_ctrl_ops, 1172 + .id = V4L2_CID_THP7312_LOW_LIGHT_COMPENSATION, 1173 + .name = "Low Light Compensation", 1174 + .type = V4L2_CTRL_TYPE_BOOLEAN, 1175 + .min = 0, 1176 + .def = 1, 1177 + .max = 1, 1178 + .step = 1, 1179 + }, { 1180 + .ops = &thp7312_ctrl_ops, 1181 + .id = V4L2_CID_THP7312_NOISE_REDUCTION_AUTO, 1182 + .name = "Noise Reduction Auto", 1183 + .type = V4L2_CTRL_TYPE_BOOLEAN, 1184 + .min = 0, 1185 + .def = 1, 1186 + .max = 1, 1187 + .step = 1, 1188 + }, { 1189 + .ops = &thp7312_ctrl_ops, 1190 + .id = V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE, 1191 + .name = "Noise Reduction Level", 1192 + .type = V4L2_CTRL_TYPE_INTEGER, 1193 + .min = 0, 1194 + .def = 0, 1195 + .max = 10, 1196 + .step = 1, 1197 + }, 1198 + }; 1199 + 1200 + static const s64 exp_bias_qmenu[] = { 1201 + -2000, -1667, -1333, -1000, -667, -333, 0, 333, 667, 1000, 1333, 1667, 2000 1202 + }; 1203 + 1204 + static int thp7312_init_controls(struct thp7312_device *thp7312) 1205 + { 1206 + struct v4l2_ctrl_handler *hdl = &thp7312->ctrl_handler; 1207 + struct device *dev = thp7312->dev; 1208 + struct v4l2_fwnode_device_properties props; 1209 + struct v4l2_ctrl *link_freq; 1210 + unsigned int num_controls; 1211 + unsigned int i; 1212 + u8 af_support; 1213 + int ret; 1214 + 1215 + /* 1216 + * Check what auto-focus methods the connected sensor supports, if any. 1217 + * Firmwares before v90.03 didn't expose the AF_SUPPORT register, 1218 + * consider both CDAF and PDAF as supported in that case. 1219 + */ 1220 + if (thp7312->fw_version >= THP7312_FW_VERSION(90, 3)) { 1221 + u64 val; 1222 + 1223 + ret = cci_read(thp7312->regmap, THP7312_REG_AF_SUPPORT, &val, 1224 + NULL); 1225 + if (ret) 1226 + return ret; 1227 + 1228 + af_support = val & (THP7312_AF_SUPPORT_PDAF | 1229 + THP7312_AF_SUPPORT_CONTRAST); 1230 + } else { 1231 + af_support = THP7312_AF_SUPPORT_PDAF 1232 + | THP7312_AF_SUPPORT_CONTRAST; 1233 + } 1234 + 1235 + num_controls = 14 + ARRAY_SIZE(thp7312_v4l2_ctrls_custom) 1236 + + (af_support ? 4 : 0); 1237 + 1238 + v4l2_ctrl_handler_init(hdl, num_controls); 1239 + 1240 + if (af_support) { 1241 + const struct v4l2_ctrl_config *af_method; 1242 + 1243 + af_method = af_support & THP7312_AF_SUPPORT_PDAF 1244 + ? &thp7312_ctrl_focus_method_pdaf 1245 + : &thp7312_ctrl_focus_method_cdaf; 1246 + 1247 + thp7312->focus_state = THP7312_FOCUS_STATE_MANUAL; 1248 + 1249 + thp7312->focus_auto = 1250 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, 1251 + V4L2_CID_FOCUS_AUTO, 1252 + 0, 1, 1, 1); 1253 + thp7312->focus_absolute = 1254 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, 1255 + V4L2_CID_FOCUS_ABSOLUTE, 1256 + 0, ARRAY_SIZE(thp7312_focus_values), 1257 + 1, 0); 1258 + thp7312->focus_method = 1259 + v4l2_ctrl_new_custom(hdl, af_method, NULL); 1260 + thp7312->focus_start = 1261 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, 1262 + V4L2_CID_AUTO_FOCUS_START, 1263 + 1, 1, 1, 1); 1264 + 1265 + v4l2_ctrl_cluster(4, &thp7312->focus_auto); 1266 + } 1267 + 1268 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE, 1269 + 0, 1, 1, 1); 1270 + /* 32: 1x, 255: 7.95x */ 1271 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_RED_BALANCE, 1272 + 32, 255, 1, 64); 1273 + /* 32: 1x, 255: 7.95x */ 1274 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_BLUE_BALANCE, 1275 + 32, 255, 1, 50); 1276 + 1277 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_BRIGHTNESS, 1278 + -10, 10, 1, 0); 1279 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_SATURATION, 1280 + 0, 31, 1, 10); 1281 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_CONTRAST, 1282 + 0, 20, 1, 10); 1283 + v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, V4L2_CID_SHARPNESS, 1284 + 0, 31, 1, 8); 1285 + 1286 + thp7312->hflip = v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, 1287 + V4L2_CID_HFLIP, 0, 1, 1, 0); 1288 + thp7312->vflip = v4l2_ctrl_new_std(hdl, &thp7312_ctrl_ops, 1289 + V4L2_CID_VFLIP, 0, 1, 1, 0); 1290 + 1291 + v4l2_ctrl_cluster(2, &thp7312->hflip); 1292 + 1293 + v4l2_ctrl_new_int_menu(hdl, &thp7312_ctrl_ops, 1294 + V4L2_CID_AUTO_EXPOSURE_BIAS, 1295 + ARRAY_SIZE(exp_bias_qmenu) - 1, 1296 + ARRAY_SIZE(exp_bias_qmenu) / 2, exp_bias_qmenu); 1297 + 1298 + v4l2_ctrl_new_std_menu(hdl, &thp7312_ctrl_ops, 1299 + V4L2_CID_POWER_LINE_FREQUENCY, 1300 + V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 1301 + V4L2_CID_POWER_LINE_FREQUENCY_50HZ); 1302 + 1303 + link_freq = v4l2_ctrl_new_int_menu(hdl, &thp7312_ctrl_ops, 1304 + V4L2_CID_LINK_FREQ, 0, 0, 1305 + &thp7312->link_freq); 1306 + 1307 + /* Set properties from fwnode (e.g. rotation, orientation). */ 1308 + ret = v4l2_fwnode_device_parse(dev, &props); 1309 + if (ret) { 1310 + dev_err(dev, "Failed to parse fwnode: %d\n", ret); 1311 + goto error; 1312 + } 1313 + 1314 + ret = v4l2_ctrl_new_fwnode_properties(hdl, &thp7312_ctrl_ops, &props); 1315 + if (ret) { 1316 + dev_err(dev, "Failed to create new v4l2 ctrl for fwnode properties: %d\n", ret); 1317 + goto error; 1318 + } 1319 + 1320 + for (i = 0; i < ARRAY_SIZE(thp7312_v4l2_ctrls_custom); i++) { 1321 + const struct v4l2_ctrl_config *ctrl_cfg = 1322 + &thp7312_v4l2_ctrls_custom[i]; 1323 + struct v4l2_ctrl *ctrl; 1324 + 1325 + ctrl = v4l2_ctrl_new_custom(hdl, ctrl_cfg, NULL); 1326 + 1327 + if (ctrl_cfg->id == V4L2_CID_THP7312_NOISE_REDUCTION_AUTO) 1328 + thp7312->noise_reduction_auto = ctrl; 1329 + else if (ctrl_cfg->id == V4L2_CID_THP7312_NOISE_REDUCTION_ABSOLUTE) 1330 + thp7312->noise_reduction_absolute = ctrl; 1331 + } 1332 + 1333 + v4l2_ctrl_cluster(2, &thp7312->noise_reduction_auto); 1334 + 1335 + if (hdl->error) { 1336 + dev_err(dev, "v4l2_ctrl_handler error\n"); 1337 + ret = hdl->error; 1338 + goto error; 1339 + } 1340 + 1341 + link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1342 + 1343 + return ret; 1344 + 1345 + error: 1346 + v4l2_ctrl_handler_free(hdl); 1347 + return ret; 1348 + } 1349 + 1350 + /* ----------------------------------------------------------------------------- 1351 + * Firmware Update 1352 + */ 1353 + 1354 + /* 1355 + * The firmware data is made of 128kB of RAM firmware, followed by a 1356 + * variable-size "header". Both are stored in flash memory. 1357 + */ 1358 + #define THP7312_FW_RAM_SIZE (128 * 1024) 1359 + #define THP7312_FW_MIN_SIZE (THP7312_FW_RAM_SIZE + 4) 1360 + #define THP7312_FW_MAX_SIZE (THP7312_FW_RAM_SIZE + 64 * 1024) 1361 + 1362 + /* 1363 + * Data is first uploaded to the THP7312 128kB SRAM, and then written to flash. 1364 + * The SRAM is exposed over I2C as 32kB banks, and up to 4kB of data can be 1365 + * transferred in a single I2C write. 1366 + */ 1367 + #define THP7312_RAM_BANK_SIZE (32 * 1024) 1368 + #define THP7312_FW_DOWNLOAD_UNIT (4 * 1024) 1369 + 1370 + #define THP7312_FLASH_MEMORY_ERASE_TIMEOUT 40 1371 + 1372 + #define THP7312_FLASH_MAX_REG_READ_SIZE 10 1373 + #define THP7312_FLASH_MAX_REG_DATA_SIZE 10 1374 + 1375 + static const u8 thp7312_cmd_config_flash_mem_if[] = { 1376 + 0xd5, 0x18, 0x00, 0x00, 0x00, 0x80 1377 + }; 1378 + 1379 + static const u8 thp7312_cmd_write_to_reg[] = { 1380 + 0xd5, 0x0c, 0x80, 0x00, 0x00, 0x00 1381 + }; 1382 + 1383 + static const u8 thp7312_cmd_read_reg[] = { 1384 + 0xd5, 0x04 1385 + }; 1386 + 1387 + /* 1388 + * THP7312 Write data from RAM to Flash Memory 1389 + * Command ID FF700F 1390 + * Format: FF700F AA AA AA BB BB BB 1391 + * AA AA AA: destination start address 1392 + * BB BB BB: (write size - 1) 1393 + * Source address always starts from 0 1394 + */ 1395 + static const u8 thp7312_cmd_write_ram_to_flash[] = { 0xff, 0x70, 0x0f }; 1396 + 1397 + /* 1398 + * THP7312 Calculate CRC command 1399 + * Command ID: FF70 09 1400 + * Format: FF70 09 AA AA AA BB BB BB 1401 + * AA AA AA: Start address of calculation 1402 + * BB BB BB: (calculate size - 1) 1403 + */ 1404 + static const u8 thp7312_cmd_calc_crc[] = { 0xff, 0x70, 0x09 }; 1405 + 1406 + static const u8 thp7312_jedec_rdid[] = { SPINOR_OP_RDID, 0x00, 0x00, 0x00 }; 1407 + static const u8 thp7312_jedec_rdsr[] = { SPINOR_OP_RDSR, 0x00, 0x00, 0x00 }; 1408 + static const u8 thp7312_jedec_wen[] = { SPINOR_OP_WREN }; 1409 + 1410 + static int thp7312_read_firmware_version(struct thp7312_device *thp7312) 1411 + { 1412 + u64 val = 0; 1413 + int ret = 0; 1414 + u8 major; 1415 + u8 minor; 1416 + 1417 + cci_read(thp7312->regmap, THP7312_REG_FIRMWARE_VERSION_1, &val, &ret); 1418 + major = val; 1419 + 1420 + cci_read(thp7312->regmap, THP7312_REG_FIRMWARE_VERSION_2, &val, &ret); 1421 + minor = val; 1422 + 1423 + thp7312->fw_version = THP7312_FW_VERSION(major, minor); 1424 + return ret; 1425 + } 1426 + 1427 + static int thp7312_write_buf(struct thp7312_device *thp7312, 1428 + const u8 *write_buf, u16 write_size) 1429 + { 1430 + struct i2c_client *client = to_i2c_client(thp7312->dev); 1431 + int ret; 1432 + 1433 + ret = i2c_master_send(client, write_buf, write_size); 1434 + return ret >= 0 ? 0 : ret; 1435 + } 1436 + 1437 + static int __thp7312_flash_reg_write(struct thp7312_device *thp7312, 1438 + const u8 *write_buf, u16 write_size) 1439 + { 1440 + struct device *dev = thp7312->dev; 1441 + u8 temp_write_buf[THP7312_FLASH_MAX_REG_DATA_SIZE + 2]; 1442 + int ret; 1443 + 1444 + if (write_size > THP7312_FLASH_MAX_REG_DATA_SIZE) { 1445 + dev_err(dev, "%s: Write size error size = %d\n", 1446 + __func__, write_size); 1447 + return -EINVAL; 1448 + } 1449 + 1450 + ret = thp7312_write_buf(thp7312, thp7312_cmd_config_flash_mem_if, 1451 + sizeof(thp7312_cmd_config_flash_mem_if)); 1452 + if (ret < 0) { 1453 + dev_err(dev, "%s: Failed to config flash memory IF: %d\n", 1454 + __func__, ret); 1455 + return ret; 1456 + } 1457 + 1458 + temp_write_buf[0] = 0xd5; 1459 + temp_write_buf[1] = 0x00; 1460 + memcpy((temp_write_buf + 2), write_buf, write_size); 1461 + ret = thp7312_write_buf(thp7312, temp_write_buf, write_size + 2); 1462 + if (ret < 0) 1463 + return ret; 1464 + 1465 + thp7312_write_buf(thp7312, thp7312_cmd_write_to_reg, 1466 + sizeof(thp7312_cmd_write_to_reg)); 1467 + 1468 + return 0; 1469 + } 1470 + 1471 + static int __thp7312_flash_reg_read(struct thp7312_device *thp7312, 1472 + const u8 *write_buf, u16 write_size, 1473 + u8 *read_buf, u16 read_size) 1474 + { 1475 + struct i2c_client *client = to_i2c_client(thp7312->dev); 1476 + struct i2c_msg msgs[2]; 1477 + int ret; 1478 + 1479 + ret = __thp7312_flash_reg_write(thp7312, write_buf, write_size); 1480 + if (ret) 1481 + return ret; 1482 + 1483 + msgs[0].addr = client->addr; 1484 + msgs[0].flags = 0; 1485 + msgs[0].len = sizeof(thp7312_cmd_read_reg), 1486 + msgs[0].buf = (u8 *)thp7312_cmd_read_reg; 1487 + 1488 + msgs[1].addr = client->addr; 1489 + msgs[1].flags = I2C_M_RD; 1490 + msgs[1].len = read_size; 1491 + msgs[1].buf = read_buf; 1492 + 1493 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 1494 + return ret >= 0 ? 0 : ret; 1495 + } 1496 + 1497 + #define thp7312_flash_reg_write(thp7312, wrbuf) \ 1498 + __thp7312_flash_reg_write(thp7312, wrbuf, sizeof(wrbuf)) 1499 + 1500 + #define thp7312_flash_reg_read(thp7312, wrbuf, rdbuf) \ 1501 + __thp7312_flash_reg_read(thp7312, wrbuf, sizeof(wrbuf), \ 1502 + rdbuf, sizeof(rdbuf)) 1503 + 1504 + static enum fw_upload_err thp7312_fw_prepare_config(struct thp7312_device *thp7312) 1505 + { 1506 + struct device *dev = thp7312->dev; 1507 + int ret; 1508 + 1509 + ret = cci_write(thp7312->regmap, THP7312_REG_FW_MEMORY_IO_SETTING, 1510 + THP7312_FW_MEMORY_IO_GPIO0, NULL); 1511 + if (ret) { 1512 + dev_err(dev, "Failed to set flash memory I/O\n"); 1513 + return FW_UPLOAD_ERR_HW_ERROR; 1514 + } 1515 + 1516 + /* Set max drivability. */ 1517 + ret = cci_write(thp7312->regmap, THP7312_REG_FW_DRIVABILITY, 0x00777777, 1518 + NULL); 1519 + if (ret) { 1520 + dev_err(dev, "Failed to set drivability: %d\n", ret); 1521 + return FW_UPLOAD_ERR_HW_ERROR; 1522 + } 1523 + 1524 + return FW_UPLOAD_ERR_NONE; 1525 + } 1526 + 1527 + static enum fw_upload_err thp7312_fw_prepare_check(struct thp7312_device *thp7312) 1528 + { 1529 + struct device *dev = thp7312->dev; 1530 + u8 read_buf[3] = { 0 }; 1531 + int ret; 1532 + 1533 + /* Get JEDEC ID */ 1534 + ret = thp7312_flash_reg_read(thp7312, thp7312_jedec_rdid, read_buf); 1535 + if (ret) { 1536 + dev_err(dev, "Failed to get JEDEC ID: %d\n", ret); 1537 + return FW_UPLOAD_ERR_HW_ERROR; 1538 + } 1539 + 1540 + dev_dbg(dev, "Flash Memory: JEDEC ID = 0x%x 0x%x 0x%x\n", 1541 + read_buf[0], read_buf[1], read_buf[2]); 1542 + 1543 + return FW_UPLOAD_ERR_NONE; 1544 + } 1545 + 1546 + static enum fw_upload_err thp7312_fw_prepare_reset(struct thp7312_device *thp7312) 1547 + { 1548 + struct device *dev = thp7312->dev; 1549 + int ret; 1550 + 1551 + ret = cci_write(thp7312->regmap, THP7312_REG_FW_RESET_FLASH, 0x81, NULL); 1552 + if (ret) { 1553 + dev_err(dev, "Failed to reset flash memory: %d\n", ret); 1554 + return FW_UPLOAD_ERR_HW_ERROR; 1555 + } 1556 + 1557 + return FW_UPLOAD_ERR_NONE; 1558 + } 1559 + 1560 + /* TODO: Erase only the amount of blocks necessary */ 1561 + static enum fw_upload_err thp7312_flash_erase(struct thp7312_device *thp7312) 1562 + { 1563 + struct device *dev = thp7312->dev; 1564 + u8 read_buf[1] = { 0 }; 1565 + unsigned int i; 1566 + u8 block; 1567 + int ret; 1568 + 1569 + for (block = 0; block < 3; block++) { 1570 + const u8 jedec_se[] = { SPINOR_OP_SE, block, 0x00, 0x00 }; 1571 + 1572 + ret = thp7312_flash_reg_write(thp7312, thp7312_jedec_wen); 1573 + if (ret < 0) { 1574 + dev_err(dev, "Failed to enable flash for writing\n"); 1575 + return FW_UPLOAD_ERR_RW_ERROR; 1576 + } 1577 + 1578 + ret = thp7312_flash_reg_write(thp7312, jedec_se); 1579 + if (ret < 0) { 1580 + dev_err(dev, "Failed to erase flash sector\n"); 1581 + return FW_UPLOAD_ERR_RW_ERROR; 1582 + } 1583 + 1584 + for (i = 0; i < THP7312_FLASH_MEMORY_ERASE_TIMEOUT; i++) { 1585 + usleep_range(100000, 101000); 1586 + thp7312_flash_reg_read(thp7312, thp7312_jedec_rdsr, 1587 + read_buf); 1588 + 1589 + /* Check Busy bit. Busy == 0x0 means erase complete. */ 1590 + if (!(read_buf[0] & SR_WIP)) 1591 + break; 1592 + } 1593 + 1594 + if (i == THP7312_FLASH_MEMORY_ERASE_TIMEOUT) 1595 + return FW_UPLOAD_ERR_TIMEOUT; 1596 + } 1597 + 1598 + thp7312_flash_reg_read(thp7312, thp7312_jedec_rdsr, read_buf); 1599 + 1600 + /* Check WEL bit. */ 1601 + if (read_buf[0] & SR_WEL) 1602 + return FW_UPLOAD_ERR_HW_ERROR; 1603 + 1604 + return FW_UPLOAD_ERR_NONE; 1605 + } 1606 + 1607 + static enum fw_upload_err 1608 + thp7312_write_download_data_by_unit(struct thp7312_device *thp7312, 1609 + unsigned int addr, const u8 *data, 1610 + unsigned int size) 1611 + { 1612 + struct device *dev = thp7312->dev; 1613 + u8 *write_buf = thp7312->fw_write_buf; 1614 + int ret; 1615 + 1616 + dev_dbg(dev, "%s: addr = 0x%04x, data = 0x%p, size = %u\n", 1617 + __func__, addr, data, size); 1618 + 1619 + write_buf[0] = (addr >> 8) & 0xff; 1620 + write_buf[1] = (addr >> 0) & 0xff; 1621 + memcpy(&write_buf[2], data, size); 1622 + 1623 + /* 1624 + * THP7312 Firmware download to RAM 1625 + * Command ID (address to download): 0x0000 - 0x7fff 1626 + * Format:: 0000 XX XX XX ........ XX 1627 + */ 1628 + ret = thp7312_write_buf(thp7312, write_buf, size + 2); 1629 + if (ret < 0) 1630 + dev_err(dev, "Unit transfer ERROR %s(): ret = %d\n", __func__, ret); 1631 + 1632 + return ret >= 0 ? FW_UPLOAD_ERR_NONE : FW_UPLOAD_ERR_RW_ERROR; 1633 + } 1634 + 1635 + static enum fw_upload_err thp7312_fw_load_to_ram(struct thp7312_device *thp7312, 1636 + const u8 *data, u32 size) 1637 + { 1638 + struct device *dev = thp7312->dev; 1639 + enum fw_upload_err ret; 1640 + unsigned int num_banks; 1641 + unsigned int i, j; 1642 + 1643 + num_banks = DIV_ROUND_UP(size, THP7312_RAM_BANK_SIZE); 1644 + 1645 + dev_dbg(dev, "%s: loading %u bytes in SRAM (%u banks)\n", __func__, 1646 + size, num_banks); 1647 + 1648 + for (i = 0; i < num_banks; i++) { 1649 + const u32 bank_addr = 0x10000000 | (i * THP7312_RAM_BANK_SIZE); 1650 + unsigned int bank_size; 1651 + unsigned int num_chunks; 1652 + 1653 + ret = cci_write(thp7312->regmap, THP7312_REG_FW_DEST_BANK_ADDR, 1654 + bank_addr, NULL); 1655 + if (ret) 1656 + return FW_UPLOAD_ERR_HW_ERROR; 1657 + 1658 + bank_size = min_t(u32, size, THP7312_RAM_BANK_SIZE); 1659 + num_chunks = DIV_ROUND_UP(bank_size, THP7312_FW_DOWNLOAD_UNIT); 1660 + 1661 + dev_dbg(dev, "%s: loading %u bytes in SRAM bank %u (%u chunks)\n", 1662 + __func__, bank_size, i, num_chunks); 1663 + 1664 + for (j = 0 ; j < num_chunks; j++) { 1665 + unsigned int chunk_addr; 1666 + unsigned int chunk_size; 1667 + 1668 + chunk_addr = j * THP7312_FW_DOWNLOAD_UNIT; 1669 + chunk_size = min_t(u32, size, THP7312_FW_DOWNLOAD_UNIT); 1670 + 1671 + ret = thp7312_write_download_data_by_unit(thp7312, chunk_addr, 1672 + data, chunk_size); 1673 + if (ret != FW_UPLOAD_ERR_NONE) { 1674 + dev_err(dev, "Unit transfer ERROR at bank transfer %s(): %d\n", 1675 + __func__, j); 1676 + return ret; 1677 + } 1678 + 1679 + data += chunk_size; 1680 + size -= chunk_size; 1681 + } 1682 + } 1683 + 1684 + return FW_UPLOAD_ERR_NONE; 1685 + } 1686 + 1687 + static enum fw_upload_err thp7312_fw_write_to_flash(struct thp7312_device *thp7312, 1688 + u32 dest, u32 write_size) 1689 + { 1690 + u8 command[sizeof(thp7312_cmd_write_ram_to_flash) + 6]; 1691 + static const u32 cmd_size = sizeof(thp7312_cmd_write_ram_to_flash); 1692 + u64 val; 1693 + int ret; 1694 + 1695 + memcpy(command, thp7312_cmd_write_ram_to_flash, cmd_size); 1696 + 1697 + command[cmd_size] = (dest & 0xff0000) >> 16; 1698 + command[cmd_size + 1] = (dest & 0x00ff00) >> 8; 1699 + command[cmd_size + 2] = (dest & 0x0000ff); 1700 + command[cmd_size + 3] = ((write_size - 1) & 0xff0000) >> 16; 1701 + command[cmd_size + 4] = ((write_size - 1) & 0x00ff00) >> 8; 1702 + command[cmd_size + 5] = ((write_size - 1) & 0x0000ff); 1703 + 1704 + ret = thp7312_write_buf(thp7312, command, sizeof(command)); 1705 + if (ret < 0) 1706 + return FW_UPLOAD_ERR_RW_ERROR; 1707 + 1708 + usleep_range(8000000, 8100000); 1709 + 1710 + ret = cci_read(thp7312->regmap, THP7312_REG_FW_VERIFY_RESULT, &val, 1711 + NULL); 1712 + if (ret < 0) 1713 + return FW_UPLOAD_ERR_RW_ERROR; 1714 + 1715 + return val ? FW_UPLOAD_ERR_HW_ERROR : FW_UPLOAD_ERR_NONE; 1716 + } 1717 + 1718 + static enum fw_upload_err thp7312_fw_check_crc(struct thp7312_device *thp7312, 1719 + const u8 *fw_data, u32 fw_size) 1720 + { 1721 + struct device *dev = thp7312->dev; 1722 + u16 header_size = fw_size - THP7312_FW_RAM_SIZE; 1723 + u8 command[sizeof(thp7312_cmd_calc_crc) + 6]; 1724 + static const u32 cmd_size = sizeof(thp7312_cmd_calc_crc); 1725 + u32 size = THP7312_FW_RAM_SIZE - 4; 1726 + u32 fw_crc; 1727 + u64 crc; 1728 + int ret; 1729 + 1730 + memcpy(command, thp7312_cmd_calc_crc, cmd_size); 1731 + 1732 + command[cmd_size] = 0; 1733 + command[cmd_size + 1] = (header_size >> 8) & 0xff; 1734 + command[cmd_size + 2] = header_size & 0xff; 1735 + 1736 + command[cmd_size + 3] = (size >> 16) & 0xff; 1737 + command[cmd_size + 4] = (size >> 8) & 0xff; 1738 + command[cmd_size + 5] = size & 0xff; 1739 + 1740 + ret = thp7312_write_buf(thp7312, command, sizeof(command)); 1741 + if (ret < 0) 1742 + return FW_UPLOAD_ERR_RW_ERROR; 1743 + 1744 + usleep_range(2000000, 2100000); 1745 + 1746 + fw_crc = get_unaligned_be32(&fw_data[fw_size - 4]); 1747 + 1748 + ret = cci_read(thp7312->regmap, THP7312_REG_FW_CRC_RESULT, &crc, NULL); 1749 + if (ret < 0) 1750 + return FW_UPLOAD_ERR_RW_ERROR; 1751 + 1752 + if (fw_crc != crc) { 1753 + dev_err(dev, "CRC mismatch: firmware 0x%08x, flash 0x%08llx\n", 1754 + fw_crc, crc); 1755 + return FW_UPLOAD_ERR_HW_ERROR; 1756 + } 1757 + 1758 + return FW_UPLOAD_ERR_NONE; 1759 + } 1760 + 1761 + static enum fw_upload_err thp7312_fw_prepare(struct fw_upload *fw_upload, 1762 + const u8 *data, u32 size) 1763 + { 1764 + struct thp7312_device *thp7312 = fw_upload->dd_handle; 1765 + struct device *dev = thp7312->dev; 1766 + enum fw_upload_err ret; 1767 + 1768 + mutex_lock(&thp7312->fw_lock); 1769 + thp7312->fw_cancel = false; 1770 + mutex_unlock(&thp7312->fw_lock); 1771 + 1772 + if (size < THP7312_FW_MIN_SIZE || size > THP7312_FW_MAX_SIZE) { 1773 + dev_err(dev, "%s: Invalid firmware size %d; must be between %d and %d\n", 1774 + __func__, size, THP7312_FW_MIN_SIZE, THP7312_FW_MAX_SIZE); 1775 + return FW_UPLOAD_ERR_INVALID_SIZE; 1776 + } 1777 + 1778 + ret = thp7312_fw_prepare_config(thp7312); 1779 + if (ret != FW_UPLOAD_ERR_NONE) 1780 + return ret; 1781 + 1782 + ret = thp7312_fw_prepare_check(thp7312); 1783 + if (ret != FW_UPLOAD_ERR_NONE) 1784 + return ret; 1785 + 1786 + ret = thp7312_fw_prepare_reset(thp7312); 1787 + if (ret != FW_UPLOAD_ERR_NONE) 1788 + return ret; 1789 + 1790 + mutex_lock(&thp7312->fw_lock); 1791 + ret = thp7312->fw_cancel ? FW_UPLOAD_ERR_CANCELED : FW_UPLOAD_ERR_NONE; 1792 + mutex_unlock(&thp7312->fw_lock); 1793 + 1794 + return ret; 1795 + } 1796 + 1797 + static enum fw_upload_err thp7312_fw_write(struct fw_upload *fw_upload, 1798 + const u8 *data, u32 offset, 1799 + u32 size, u32 *written) 1800 + { 1801 + struct thp7312_device *thp7312 = fw_upload->dd_handle; 1802 + struct device *dev = thp7312->dev; 1803 + u16 header_size = size - THP7312_FW_RAM_SIZE; 1804 + enum fw_upload_err ret; 1805 + bool cancel; 1806 + 1807 + mutex_lock(&thp7312->fw_lock); 1808 + cancel = thp7312->fw_cancel; 1809 + mutex_unlock(&thp7312->fw_lock); 1810 + 1811 + if (cancel) 1812 + return FW_UPLOAD_ERR_CANCELED; 1813 + 1814 + ret = thp7312_flash_erase(thp7312); 1815 + if (ret != FW_UPLOAD_ERR_NONE) 1816 + return ret; 1817 + 1818 + ret = thp7312_fw_load_to_ram(thp7312, data, THP7312_FW_RAM_SIZE); 1819 + if (ret != FW_UPLOAD_ERR_NONE) 1820 + return ret; 1821 + 1822 + ret = thp7312_fw_write_to_flash(thp7312, 0, 0x1ffff); 1823 + if (ret != FW_UPLOAD_ERR_NONE) 1824 + return ret; 1825 + 1826 + ret = thp7312_fw_load_to_ram(thp7312, data + THP7312_FW_RAM_SIZE, header_size); 1827 + if (ret != FW_UPLOAD_ERR_NONE) 1828 + return ret; 1829 + 1830 + ret = thp7312_fw_write_to_flash(thp7312, 0x20000, header_size - 1); 1831 + if (ret != FW_UPLOAD_ERR_NONE) 1832 + return ret; 1833 + 1834 + ret = thp7312_fw_check_crc(thp7312, data, size); 1835 + if (ret != FW_UPLOAD_ERR_NONE) 1836 + return ret; 1837 + 1838 + dev_info(dev, "Successfully wrote firmware\n"); 1839 + 1840 + *written = size; 1841 + return FW_UPLOAD_ERR_NONE; 1842 + } 1843 + 1844 + static enum fw_upload_err thp7312_fw_poll_complete(struct fw_upload *fw_upload) 1845 + { 1846 + return FW_UPLOAD_ERR_NONE; 1847 + } 1848 + 1849 + /* 1850 + * This may be called asynchronously with an on-going update. All other 1851 + * functions are called sequentially in a single thread. To avoid contention on 1852 + * register accesses, only update the cancel_request flag. Other functions will 1853 + * check this flag and handle the cancel request synchronously. 1854 + */ 1855 + static void thp7312_fw_cancel(struct fw_upload *fw_upload) 1856 + { 1857 + struct thp7312_device *thp7312 = fw_upload->dd_handle; 1858 + 1859 + mutex_lock(&thp7312->fw_lock); 1860 + thp7312->fw_cancel = true; 1861 + mutex_unlock(&thp7312->fw_lock); 1862 + } 1863 + 1864 + static const struct fw_upload_ops thp7312_fw_upload_ops = { 1865 + .prepare = thp7312_fw_prepare, 1866 + .write = thp7312_fw_write, 1867 + .poll_complete = thp7312_fw_poll_complete, 1868 + .cancel = thp7312_fw_cancel, 1869 + }; 1870 + 1871 + static int thp7312_register_flash_mode(struct thp7312_device *thp7312) 1872 + { 1873 + struct device *dev = thp7312->dev; 1874 + struct fw_upload *fwl; 1875 + u64 val; 1876 + int ret; 1877 + 1878 + dev_info(dev, "booted in flash mode\n"); 1879 + 1880 + mutex_init(&thp7312->fw_lock); 1881 + 1882 + thp7312->fw_write_buf = devm_kzalloc(dev, THP7312_FW_DOWNLOAD_UNIT + 2, 1883 + GFP_KERNEL); 1884 + if (!thp7312->fw_write_buf) 1885 + return -ENOMEM; 1886 + 1887 + ret = __thp7312_power_on(thp7312); 1888 + if (ret < 0) 1889 + return dev_err_probe(dev, ret, "Failed to power on\n"); 1890 + 1891 + ret = cci_read(thp7312->regmap, THP7312_REG_FW_STATUS, &val, NULL); 1892 + if (ret) { 1893 + dev_err_probe(dev, ret, "Camera status read failed\n"); 1894 + goto error; 1895 + } 1896 + 1897 + fwl = firmware_upload_register(THIS_MODULE, dev, "thp7312-firmware", 1898 + &thp7312_fw_upload_ops, thp7312); 1899 + if (IS_ERR(fwl)) { 1900 + ret = PTR_ERR(fwl); 1901 + dev_err_probe(dev, ret, "Failed to register firmware upload\n"); 1902 + goto error; 1903 + } 1904 + 1905 + thp7312->fwl = fwl; 1906 + return 0; 1907 + 1908 + error: 1909 + __thp7312_power_off(thp7312); 1910 + return ret; 1911 + } 1912 + 1913 + /* ----------------------------------------------------------------------------- 1914 + * Probe & Remove 1915 + */ 1916 + 1917 + static int thp7312_get_regulators(struct thp7312_device *thp7312) 1918 + { 1919 + unsigned int i; 1920 + 1921 + for (i = 0; i < ARRAY_SIZE(thp7312->supplies); i++) 1922 + thp7312->supplies[i].supply = thp7312_supply_name[i]; 1923 + 1924 + return devm_regulator_bulk_get(thp7312->dev, 1925 + ARRAY_SIZE(thp7312->supplies), 1926 + thp7312->supplies); 1927 + } 1928 + 1929 + static int thp7312_sensor_parse_dt(struct thp7312_device *thp7312, 1930 + struct fwnode_handle *node) 1931 + { 1932 + struct device *dev = thp7312->dev; 1933 + struct thp7312_sensor *sensor; 1934 + const char *model; 1935 + u8 data_lanes[4]; 1936 + u32 values[4]; 1937 + unsigned int i; 1938 + u32 reg; 1939 + int ret; 1940 + 1941 + /* Retrieve the sensor index from the reg property. */ 1942 + ret = fwnode_property_read_u32(node, "reg", &reg); 1943 + if (ret < 0) { 1944 + dev_err(dev, "'reg' property missing in sensor node\n"); 1945 + return -EINVAL; 1946 + } 1947 + 1948 + if (reg >= ARRAY_SIZE(thp7312->sensors)) { 1949 + dev_err(dev, "Out-of-bounds 'reg' value %u\n", reg); 1950 + return -EINVAL; 1951 + } 1952 + 1953 + sensor = &thp7312->sensors[reg]; 1954 + if (sensor->info) { 1955 + dev_err(dev, "Duplicate entry for sensor %u\n", reg); 1956 + return -EINVAL; 1957 + } 1958 + 1959 + ret = fwnode_property_read_string(node, "thine,model", &model); 1960 + if (ret < 0) { 1961 + dev_err(dev, "'thine,model' property missing in sensor node\n"); 1962 + return -EINVAL; 1963 + } 1964 + 1965 + for (i = 0; i < ARRAY_SIZE(thp7312_sensor_info); i++) { 1966 + const struct thp7312_sensor_info *info = 1967 + &thp7312_sensor_info[i]; 1968 + 1969 + if (!strcmp(info->model, model)) { 1970 + sensor->info = info; 1971 + break; 1972 + } 1973 + } 1974 + 1975 + if (!sensor->info) { 1976 + dev_err(dev, "Unsupported sensor model %s\n", model); 1977 + return -EINVAL; 1978 + } 1979 + 1980 + ret = fwnode_property_read_u32_array(node, "data-lanes", values, 1981 + ARRAY_SIZE(values)); 1982 + if (ret < 0) { 1983 + dev_err(dev, "Failed to read property data-lanes: %d\n", ret); 1984 + return ret; 1985 + } 1986 + 1987 + for (i = 0; i < ARRAY_SIZE(data_lanes); ++i) 1988 + data_lanes[i] = values[i]; 1989 + 1990 + ret = thp7312_map_data_lanes(&sensor->lane_remap, data_lanes, 1991 + ARRAY_SIZE(data_lanes)); 1992 + if (ret) { 1993 + dev_err(dev, "Invalid sensor@%u data-lanes value\n", reg); 1994 + return ret; 1995 + } 1996 + 1997 + return 0; 1998 + } 1999 + 2000 + static int thp7312_parse_dt(struct thp7312_device *thp7312) 2001 + { 2002 + struct v4l2_fwnode_endpoint ep = { 2003 + .bus_type = V4L2_MBUS_CSI2_DPHY, 2004 + }; 2005 + struct device *dev = thp7312->dev; 2006 + struct fwnode_handle *endpoint; 2007 + struct fwnode_handle *sensors; 2008 + unsigned int num_sensors = 0; 2009 + struct fwnode_handle *node; 2010 + int ret; 2011 + 2012 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); 2013 + if (!endpoint) 2014 + return dev_err_probe(dev, -EINVAL, "Endpoint node not found\n"); 2015 + 2016 + ret = v4l2_fwnode_endpoint_parse(endpoint, &ep); 2017 + fwnode_handle_put(endpoint); 2018 + if (ret) 2019 + return dev_err_probe(dev, ret, "Could not parse endpoint\n"); 2020 + 2021 + ret = thp7312_map_data_lanes(&thp7312->lane_remap, 2022 + ep.bus.mipi_csi2.data_lanes, 2023 + ep.bus.mipi_csi2.num_data_lanes); 2024 + if (ret) { 2025 + dev_err(dev, "Invalid data-lanes value\n"); 2026 + return ret; 2027 + } 2028 + 2029 + /* 2030 + * The thine,boot-mode property is optional and default to 2031 + * THP7312_BOOT_MODE_SPI_MASTER (1). 2032 + */ 2033 + thp7312->boot_mode = THP7312_BOOT_MODE_SPI_MASTER; 2034 + ret = device_property_read_u32(dev, "thine,boot-mode", 2035 + &thp7312->boot_mode); 2036 + if (ret && ret != -EINVAL) 2037 + return dev_err_probe(dev, ret, "Property '%s' is invalid\n", 2038 + "thine,boot-mode"); 2039 + 2040 + if (thp7312->boot_mode != THP7312_BOOT_MODE_2WIRE_SLAVE && 2041 + thp7312->boot_mode != THP7312_BOOT_MODE_SPI_MASTER) 2042 + return dev_err_probe(dev, -EINVAL, "Invalid '%s' value %u\n", 2043 + "thine,boot-mode", thp7312->boot_mode); 2044 + 2045 + /* Sensors */ 2046 + sensors = device_get_named_child_node(dev, "sensors"); 2047 + if (!sensors) { 2048 + dev_err(dev, "'sensors' child node not found\n"); 2049 + return -EINVAL; 2050 + } 2051 + 2052 + fwnode_for_each_available_child_node(sensors, node) { 2053 + if (fwnode_name_eq(node, "sensor")) { 2054 + if (!thp7312_sensor_parse_dt(thp7312, node)) 2055 + num_sensors++; 2056 + } 2057 + } 2058 + 2059 + fwnode_handle_put(sensors); 2060 + 2061 + if (!num_sensors) { 2062 + dev_err(dev, "No sensor found\n"); 2063 + return -EINVAL; 2064 + } 2065 + 2066 + return 0; 2067 + } 2068 + 2069 + static int thp7312_probe(struct i2c_client *client) 2070 + { 2071 + struct device *dev = &client->dev; 2072 + struct v4l2_subdev_state *sd_state; 2073 + struct thp7312_device *thp7312; 2074 + int ret; 2075 + 2076 + thp7312 = devm_kzalloc(dev, sizeof(*thp7312), GFP_KERNEL); 2077 + if (!thp7312) 2078 + return -ENOMEM; 2079 + 2080 + thp7312->dev = dev; 2081 + 2082 + thp7312->regmap = devm_cci_regmap_init_i2c(client, 16); 2083 + if (IS_ERR(thp7312->regmap)) 2084 + return dev_err_probe(dev, PTR_ERR(thp7312->regmap), 2085 + "Unable to initialize I2C\n"); 2086 + 2087 + ret = thp7312_parse_dt(thp7312); 2088 + if (ret < 0) 2089 + return ret; 2090 + 2091 + ret = thp7312_get_regulators(thp7312); 2092 + if (ret) 2093 + return dev_err_probe(dev, ret, "Failed to get regulators\n"); 2094 + 2095 + thp7312->iclk = devm_clk_get(dev, NULL); 2096 + if (IS_ERR(thp7312->iclk)) 2097 + return dev_err_probe(dev, PTR_ERR(thp7312->iclk), 2098 + "Failed to get iclk\n"); 2099 + 2100 + thp7312->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 2101 + if (IS_ERR(thp7312->reset_gpio)) 2102 + return dev_err_probe(dev, PTR_ERR(thp7312->reset_gpio), 2103 + "Failed to get reset gpio\n"); 2104 + 2105 + if (thp7312->boot_mode == THP7312_BOOT_MODE_2WIRE_SLAVE) 2106 + return thp7312_register_flash_mode(thp7312); 2107 + 2108 + v4l2_i2c_subdev_init(&thp7312->sd, client, &thp7312_subdev_ops); 2109 + thp7312->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 2110 + thp7312->pad.flags = MEDIA_PAD_FL_SOURCE; 2111 + thp7312->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 2112 + 2113 + ret = media_entity_pads_init(&thp7312->sd.entity, 1, &thp7312->pad); 2114 + if (ret) 2115 + return ret; 2116 + 2117 + /* 2118 + * Enable power management. The driver supports runtime PM, but needs to 2119 + * work when runtime PM is disabled in the kernel. To that end, power 2120 + * the device manually here. 2121 + */ 2122 + ret = thp7312_power_on(thp7312); 2123 + if (ret) 2124 + goto err_entity_cleanup; 2125 + 2126 + ret = thp7312_read_firmware_version(thp7312); 2127 + if (ret < 0) { 2128 + dev_err(dev, "Camera is not found\n"); 2129 + goto err_power_off; 2130 + } 2131 + 2132 + ret = thp7312_init_controls(thp7312); 2133 + if (ret) { 2134 + dev_err(dev, "Failed to initialize controls\n"); 2135 + goto err_power_off; 2136 + } 2137 + 2138 + thp7312->sd.ctrl_handler = &thp7312->ctrl_handler; 2139 + thp7312->sd.state_lock = thp7312->ctrl_handler.lock; 2140 + 2141 + ret = v4l2_subdev_init_finalize(&thp7312->sd); 2142 + if (ret < 0) { 2143 + dev_err(dev, "Subdev active state initialization failed\n"); 2144 + goto err_free_ctrls; 2145 + } 2146 + 2147 + sd_state = v4l2_subdev_lock_and_get_active_state(&thp7312->sd); 2148 + thp7312->current_mode = &thp7312_mode_info_data[0]; 2149 + thp7312_set_frame_rate(thp7312, &thp7312->current_mode->rates[0]); 2150 + v4l2_subdev_unlock_state(sd_state); 2151 + 2152 + /* 2153 + * Enable runtime PM with autosuspend. As the device has been powered 2154 + * manually, mark it as active, and increase the usage count without 2155 + * resuming the device. 2156 + */ 2157 + pm_runtime_set_active(dev); 2158 + pm_runtime_get_noresume(dev); 2159 + pm_runtime_enable(dev); 2160 + pm_runtime_set_autosuspend_delay(dev, 1000); 2161 + pm_runtime_use_autosuspend(dev); 2162 + 2163 + ret = v4l2_async_register_subdev(&thp7312->sd); 2164 + if (ret < 0) { 2165 + dev_err(dev, "Subdev registration failed\n"); 2166 + goto err_pm; 2167 + } 2168 + 2169 + /* 2170 + * Decrease the PM usage count. The device will get suspended after the 2171 + * autosuspend delay, turning the power off. 2172 + */ 2173 + pm_runtime_mark_last_busy(dev); 2174 + pm_runtime_put_autosuspend(dev); 2175 + 2176 + dev_info(dev, "THP7312 firmware version %02u.%02u\n", 2177 + THP7312_FW_VERSION_MAJOR(thp7312->fw_version), 2178 + THP7312_FW_VERSION_MINOR(thp7312->fw_version)); 2179 + 2180 + return 0; 2181 + 2182 + err_pm: 2183 + pm_runtime_disable(dev); 2184 + pm_runtime_put_noidle(dev); 2185 + v4l2_subdev_cleanup(&thp7312->sd); 2186 + err_free_ctrls: 2187 + v4l2_ctrl_handler_free(&thp7312->ctrl_handler); 2188 + err_power_off: 2189 + thp7312_power_off(thp7312); 2190 + err_entity_cleanup: 2191 + media_entity_cleanup(&thp7312->sd.entity); 2192 + return ret; 2193 + } 2194 + 2195 + static void thp7312_remove(struct i2c_client *client) 2196 + { 2197 + struct v4l2_subdev *sd = i2c_get_clientdata(client); 2198 + struct thp7312_device *thp7312 = to_thp7312_dev(sd); 2199 + 2200 + if (thp7312->boot_mode == THP7312_BOOT_MODE_2WIRE_SLAVE) { 2201 + firmware_upload_unregister(thp7312->fwl); 2202 + __thp7312_power_off(thp7312); 2203 + return; 2204 + } 2205 + 2206 + v4l2_async_unregister_subdev(&thp7312->sd); 2207 + v4l2_subdev_cleanup(&thp7312->sd); 2208 + media_entity_cleanup(&thp7312->sd.entity); 2209 + v4l2_ctrl_handler_free(&thp7312->ctrl_handler); 2210 + 2211 + /* 2212 + * Disable runtime PM. In case runtime PM is disabled in the kernel, 2213 + * make sure to turn power off manually. 2214 + */ 2215 + pm_runtime_disable(thp7312->dev); 2216 + if (!pm_runtime_status_suspended(thp7312->dev)) 2217 + thp7312_power_off(thp7312); 2218 + pm_runtime_set_suspended(thp7312->dev); 2219 + } 2220 + 2221 + static const struct of_device_id thp7312_dt_ids[] = { 2222 + { .compatible = "thine,thp7312" }, 2223 + { /* sentinel */ } 2224 + }; 2225 + MODULE_DEVICE_TABLE(of, thp7312_dt_ids); 2226 + 2227 + static struct i2c_driver thp7312_i2c_driver = { 2228 + .driver = { 2229 + .name = "thp7312", 2230 + .pm = &thp7312_pm_ops, 2231 + .of_match_table = thp7312_dt_ids, 2232 + }, 2233 + .probe = thp7312_probe, 2234 + .remove = thp7312_remove, 2235 + }; 2236 + 2237 + module_i2c_driver(thp7312_i2c_driver); 2238 + 2239 + MODULE_DESCRIPTION("THP7312 MIPI Camera Subdev Driver"); 2240 + MODULE_LICENSE("GPL");