| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * A V4L2 driver for Sony IMX708 cameras. |
| 4 | * Copyright (C) 2022, Raspberry Pi Ltd |
| 5 | * |
| 6 | * Based on Sony imx477 camera driver |
| 7 | * Copyright (C) 2020 Raspberry Pi Ltd |
| 8 | */ |
| 9 | #include <linux/unaligned.h> |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/gpio/consumer.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/pm_runtime.h> |
| 16 | #include <linux/regulator/consumer.h> |
| 17 | #include <media/v4l2-ctrls.h> |
| 18 | #include <media/v4l2-device.h> |
| 19 | #include <media/v4l2-event.h> |
| 20 | #include <media/v4l2-fwnode.h> |
| 21 | #include <media/v4l2-mediabus.h> |
| 22 | |
| 23 | /* |
| 24 | * Parameter to adjust Quad Bayer re-mosaic broken line correction |
| 25 | * strength, used in full-resolution mode only. Set zero to disable. |
| 26 | */ |
| 27 | static int qbc_adjust = 2; |
| 28 | module_param(qbc_adjust, int, 0644); |
| 29 | MODULE_PARM_DESC(qbc_adjust, "Quad Bayer broken line correction strength [0,2-5]"); |
| 30 | |
| 31 | #define IMX708_REG_VALUE_08BIT 1 |
| 32 | #define IMX708_REG_VALUE_16BIT 2 |
| 33 | |
| 34 | /* Chip ID */ |
| 35 | #define IMX708_REG_CHIP_ID 0x0016 |
| 36 | #define IMX708_CHIP_ID 0x0708 |
| 37 | |
| 38 | #define IMX708_REG_MODE_SELECT 0x0100 |
| 39 | #define IMX708_MODE_STANDBY 0x00 |
| 40 | #define IMX708_MODE_STREAMING 0x01 |
| 41 | |
| 42 | #define IMX708_REG_ORIENTATION 0x101 |
| 43 | |
| 44 | #define IMX708_INCLK_FREQ 24000000 |
| 45 | |
| 46 | /* Default initial pixel rate, will get updated for each mode. */ |
| 47 | #define IMX708_INITIAL_PIXEL_RATE 590000000 |
| 48 | |
| 49 | /* V_TIMING internal */ |
| 50 | #define IMX708_REG_FRAME_LENGTH 0x0340 |
| 51 | #define IMX708_FRAME_LENGTH_MAX 0xffff |
| 52 | |
| 53 | /* H_TIMING internal */ |
| 54 | #define IMX708_REG_LINE_LENGTH 0x0342 |
| 55 | |
| 56 | /* Long exposure multiplier */ |
| 57 | #define IMX708_LONG_EXP_SHIFT_MAX 7 |
| 58 | #define IMX708_LONG_EXP_SHIFT_REG 0x3100 |
| 59 | |
| 60 | /* Exposure control */ |
| 61 | #define IMX708_REG_EXPOSURE 0x0202 |
| 62 | #define IMX708_EXPOSURE_OFFSET 48 |
| 63 | #define IMX708_EXPOSURE_DEFAULT 0x640 |
| 64 | #define IMX708_EXPOSURE_STEP 1 |
| 65 | #define IMX708_EXPOSURE_MIN 1 |
| 66 | #define IMX708_EXPOSURE_MAX (IMX708_FRAME_LENGTH_MAX - \ |
| 67 | IMX708_EXPOSURE_OFFSET) |
| 68 | |
| 69 | /* Analog gain control */ |
| 70 | #define IMX708_REG_ANALOG_GAIN 0x0204 |
| 71 | #define IMX708_ANA_GAIN_MIN 112 |
| 72 | #define IMX708_ANA_GAIN_MAX 960 |
| 73 | #define IMX708_ANA_GAIN_STEP 1 |
| 74 | #define IMX708_ANA_GAIN_DEFAULT IMX708_ANA_GAIN_MIN |
| 75 | |
| 76 | /* Digital gain control */ |
| 77 | #define IMX708_REG_DIGITAL_GAIN 0x020e |
| 78 | #define IMX708_DGTL_GAIN_MIN 0x0100 |
| 79 | #define IMX708_DGTL_GAIN_MAX 0xffff |
| 80 | #define IMX708_DGTL_GAIN_DEFAULT 0x0100 |
| 81 | #define IMX708_DGTL_GAIN_STEP 1 |
| 82 | |
| 83 | /* Colour balance controls */ |
| 84 | #define IMX708_REG_COLOUR_BALANCE_RED 0x0b90 |
| 85 | #define IMX708_REG_COLOUR_BALANCE_BLUE 0x0b92 |
| 86 | #define IMX708_COLOUR_BALANCE_MIN 0x01 |
| 87 | #define IMX708_COLOUR_BALANCE_MAX 0xffff |
| 88 | #define IMX708_COLOUR_BALANCE_STEP 0x01 |
| 89 | #define IMX708_COLOUR_BALANCE_DEFAULT 0x100 |
| 90 | |
| 91 | /* Test Pattern Control */ |
| 92 | #define IMX708_REG_TEST_PATTERN 0x0600 |
| 93 | #define IMX708_TEST_PATTERN_DISABLE 0 |
| 94 | #define IMX708_TEST_PATTERN_SOLID_COLOR 1 |
| 95 | #define IMX708_TEST_PATTERN_COLOR_BARS 2 |
| 96 | #define IMX708_TEST_PATTERN_GREY_COLOR 3 |
| 97 | #define IMX708_TEST_PATTERN_PN9 4 |
| 98 | |
| 99 | /* Test pattern colour components */ |
| 100 | #define IMX708_REG_TEST_PATTERN_R 0x0602 |
| 101 | #define IMX708_REG_TEST_PATTERN_GR 0x0604 |
| 102 | #define IMX708_REG_TEST_PATTERN_B 0x0606 |
| 103 | #define IMX708_REG_TEST_PATTERN_GB 0x0608 |
| 104 | #define IMX708_TEST_PATTERN_COLOUR_MIN 0 |
| 105 | #define IMX708_TEST_PATTERN_COLOUR_MAX 0x0fff |
| 106 | #define IMX708_TEST_PATTERN_COLOUR_STEP 1 |
| 107 | |
| 108 | #define IMX708_REG_BASE_SPC_GAINS_L 0x7b10 |
| 109 | #define IMX708_REG_BASE_SPC_GAINS_R 0x7c00 |
| 110 | |
| 111 | /* HDR exposure ratio (long:med == med:short) */ |
| 112 | #define IMX708_HDR_EXPOSURE_RATIO 4 |
| 113 | #define IMX708_REG_MID_EXPOSURE 0x3116 |
| 114 | #define IMX708_REG_SHT_EXPOSURE 0x0224 |
| 115 | #define IMX708_REG_MID_ANALOG_GAIN 0x3118 |
| 116 | #define IMX708_REG_SHT_ANALOG_GAIN 0x0216 |
| 117 | |
| 118 | /* QBC Re-mosaic broken line correction registers */ |
| 119 | #define IMX708_LPF_INTENSITY_EN 0xC428 |
| 120 | #define IMX708_LPF_INTENSITY_ENABLED 0x00 |
| 121 | #define IMX708_LPF_INTENSITY_DISABLED 0x01 |
| 122 | #define IMX708_LPF_INTENSITY 0xC429 |
| 123 | |
| 124 | /* |
| 125 | * Metadata buffer holds a variety of data, all sent with the same VC/DT (0x12). |
| 126 | * It comprises two scanlines (of up to 5760 bytes each, for 4608 pixels) |
| 127 | * of embedded data, one line of PDAF data, and two lines of AE-HIST data |
| 128 | * (AE histograms are valid for HDR mode and empty in non-HDR modes). |
| 129 | */ |
| 130 | #define IMX708_EMBEDDED_LINE_WIDTH (5 * 5760) |
| 131 | #define IMX708_NUM_EMBEDDED_LINES 1 |
| 132 | |
| 133 | enum pad_types { |
| 134 | IMAGE_PAD, |
| 135 | METADATA_PAD, |
| 136 | NUM_PADS |
| 137 | }; |
| 138 | |
| 139 | /* IMX708 native and active pixel array size. */ |
| 140 | #define IMX708_NATIVE_WIDTH 4640U |
| 141 | #define IMX708_NATIVE_HEIGHT 2658U |
| 142 | #define IMX708_PIXEL_ARRAY_LEFT 16U |
| 143 | #define IMX708_PIXEL_ARRAY_TOP 24U |
| 144 | #define IMX708_PIXEL_ARRAY_WIDTH 4608U |
| 145 | #define IMX708_PIXEL_ARRAY_HEIGHT 2592U |
| 146 | |
| 147 | struct imx708_reg { |
| 148 | u16 address; |
| 149 | u8 val; |
| 150 | }; |
| 151 | |
| 152 | struct imx708_reg_list { |
| 153 | unsigned int num_of_regs; |
| 154 | const struct imx708_reg *regs; |
| 155 | }; |
| 156 | |
| 157 | /* Mode : resolution and related config&values */ |
| 158 | struct imx708_mode { |
| 159 | /* Frame width */ |
| 160 | unsigned int width; |
| 161 | |
| 162 | /* Frame height */ |
| 163 | unsigned int height; |
| 164 | |
| 165 | /* H-timing in pixels */ |
| 166 | unsigned int line_length_pix; |
| 167 | |
| 168 | /* Analog crop rectangle. */ |
| 169 | struct v4l2_rect crop; |
| 170 | |
| 171 | /* Highest possible framerate. */ |
| 172 | unsigned int vblank_min; |
| 173 | |
| 174 | /* Default framerate. */ |
| 175 | unsigned int vblank_default; |
| 176 | |
| 177 | /* Default register values */ |
| 178 | struct imx708_reg_list reg_list; |
| 179 | |
| 180 | /* Not all modes have the same pixel rate. */ |
| 181 | u64 pixel_rate; |
| 182 | |
| 183 | /* Not all modes have the same minimum exposure. */ |
| 184 | u32 exposure_lines_min; |
| 185 | |
| 186 | /* Not all modes have the same exposure lines step. */ |
| 187 | u32 exposure_lines_step; |
| 188 | |
| 189 | /* HDR flag, used for checking if the current mode is HDR */ |
| 190 | bool hdr; |
| 191 | |
| 192 | /* Quad Bayer Re-mosaic flag */ |
| 193 | bool remosaic; |
| 194 | }; |
| 195 | |
| 196 | /* Default PDAF pixel correction gains */ |
| 197 | static const u8 pdaf_gains[2][9] = { |
| 198 | { 0x4c, 0x4c, 0x4c, 0x46, 0x3e, 0x38, 0x35, 0x35, 0x35 }, |
| 199 | { 0x35, 0x35, 0x35, 0x38, 0x3e, 0x46, 0x4c, 0x4c, 0x4c } |
| 200 | }; |
| 201 | |
| 202 | /* Link frequency setup */ |
| 203 | enum { |
| 204 | IMX708_LINK_FREQ_450MHZ, |
| 205 | IMX708_LINK_FREQ_447MHZ, |
| 206 | IMX708_LINK_FREQ_453MHZ, |
| 207 | }; |
| 208 | |
| 209 | static const s64 link_freqs[] = { |
| 210 | [IMX708_LINK_FREQ_450MHZ] = 450000000, |
| 211 | [IMX708_LINK_FREQ_447MHZ] = 447000000, |
| 212 | [IMX708_LINK_FREQ_453MHZ] = 453000000, |
| 213 | }; |
| 214 | |
| 215 | /* 450MHz is the nominal "default" link frequency */ |
| 216 | static const struct imx708_reg link_450Mhz_regs[] = { |
| 217 | {0x030E, 0x01}, |
| 218 | {0x030F, 0x2c}, |
| 219 | }; |
| 220 | |
| 221 | static const struct imx708_reg link_447Mhz_regs[] = { |
| 222 | {0x030E, 0x01}, |
| 223 | {0x030F, 0x2a}, |
| 224 | }; |
| 225 | |
| 226 | static const struct imx708_reg link_453Mhz_regs[] = { |
| 227 | {0x030E, 0x01}, |
| 228 | {0x030F, 0x2e}, |
| 229 | }; |
| 230 | |
| 231 | static const struct imx708_reg_list link_freq_regs[] = { |
| 232 | [IMX708_LINK_FREQ_450MHZ] = { |
| 233 | .regs = link_450Mhz_regs, |
| 234 | .num_of_regs = ARRAY_SIZE(link_450Mhz_regs) |
| 235 | }, |
| 236 | [IMX708_LINK_FREQ_447MHZ] = { |
| 237 | .regs = link_447Mhz_regs, |
| 238 | .num_of_regs = ARRAY_SIZE(link_447Mhz_regs) |
| 239 | }, |
| 240 | [IMX708_LINK_FREQ_453MHZ] = { |
| 241 | .regs = link_453Mhz_regs, |
| 242 | .num_of_regs = ARRAY_SIZE(link_453Mhz_regs) |
| 243 | }, |
| 244 | }; |
| 245 | |
| 246 | static const struct imx708_reg mode_common_regs[] = { |
| 247 | {0x0100, 0x00}, |
| 248 | {0x0136, 0x18}, |
| 249 | {0x0137, 0x00}, |
| 250 | {0x33F0, 0x02}, |
| 251 | {0x33F1, 0x05}, |
| 252 | {0x3062, 0x00}, |
| 253 | {0x3063, 0x12}, |
| 254 | {0x3068, 0x00}, |
| 255 | {0x3069, 0x12}, |
| 256 | {0x306A, 0x00}, |
| 257 | {0x306B, 0x30}, |
| 258 | {0x3076, 0x00}, |
| 259 | {0x3077, 0x30}, |
| 260 | {0x3078, 0x00}, |
| 261 | {0x3079, 0x30}, |
| 262 | {0x5E54, 0x0C}, |
| 263 | {0x6E44, 0x00}, |
| 264 | {0xB0B6, 0x01}, |
| 265 | {0xE829, 0x00}, |
| 266 | {0xF001, 0x08}, |
| 267 | {0xF003, 0x08}, |
| 268 | {0xF00D, 0x10}, |
| 269 | {0xF00F, 0x10}, |
| 270 | {0xF031, 0x08}, |
| 271 | {0xF033, 0x08}, |
| 272 | {0xF03D, 0x10}, |
| 273 | {0xF03F, 0x10}, |
| 274 | {0x0112, 0x0A}, |
| 275 | {0x0113, 0x0A}, |
| 276 | {0x0114, 0x03}, |
| 277 | {0x0B8E, 0x01}, |
| 278 | {0x0B8F, 0x00}, |
| 279 | {0x0B94, 0x01}, |
| 280 | {0x0B95, 0x00}, |
| 281 | {0x3400, 0x01}, |
| 282 | {0x3478, 0x01}, |
| 283 | {0x3479, 0x1c}, |
| 284 | {0x3091, 0x01}, |
| 285 | {0x3092, 0x00}, |
| 286 | {0x3419, 0x00}, |
| 287 | {0xBCF1, 0x02}, |
| 288 | {0x3094, 0x01}, |
| 289 | {0x3095, 0x01}, |
| 290 | {0x3362, 0x00}, |
| 291 | {0x3363, 0x00}, |
| 292 | {0x3364, 0x00}, |
| 293 | {0x3365, 0x00}, |
| 294 | {0x0138, 0x01}, |
| 295 | }; |
| 296 | |
| 297 | /* 10-bit. */ |
| 298 | static const struct imx708_reg mode_4608x2592_regs[] = { |
| 299 | {0x0340, 0x0A}, |
| 300 | {0x0341, 0x59}, |
| 301 | {0x0344, 0x00}, |
| 302 | {0x0345, 0x00}, |
| 303 | {0x0346, 0x00}, |
| 304 | {0x0347, 0x00}, |
| 305 | {0x0348, 0x11}, |
| 306 | {0x0349, 0xFF}, |
| 307 | {0x034A, 0X0A}, |
| 308 | {0x034B, 0x1F}, |
| 309 | {0x0220, 0x62}, |
| 310 | {0x0222, 0x01}, |
| 311 | {0x0900, 0x00}, |
| 312 | {0x0901, 0x11}, |
| 313 | {0x0902, 0x0A}, |
| 314 | {0x3200, 0x01}, |
| 315 | {0x3201, 0x01}, |
| 316 | {0x32D5, 0x01}, |
| 317 | {0x32D6, 0x00}, |
| 318 | {0x32DB, 0x01}, |
| 319 | {0x32DF, 0x00}, |
| 320 | {0x350C, 0x00}, |
| 321 | {0x350D, 0x00}, |
| 322 | {0x0408, 0x00}, |
| 323 | {0x0409, 0x00}, |
| 324 | {0x040A, 0x00}, |
| 325 | {0x040B, 0x00}, |
| 326 | {0x040C, 0x12}, |
| 327 | {0x040D, 0x00}, |
| 328 | {0x040E, 0x0A}, |
| 329 | {0x040F, 0x20}, |
| 330 | {0x034C, 0x12}, |
| 331 | {0x034D, 0x00}, |
| 332 | {0x034E, 0x0A}, |
| 333 | {0x034F, 0x20}, |
| 334 | {0x0301, 0x05}, |
| 335 | {0x0303, 0x02}, |
| 336 | {0x0305, 0x02}, |
| 337 | {0x0306, 0x00}, |
| 338 | {0x0307, 0x7C}, |
| 339 | {0x030B, 0x02}, |
| 340 | {0x030D, 0x04}, |
| 341 | {0x0310, 0x01}, |
| 342 | {0x3CA0, 0x00}, |
| 343 | {0x3CA1, 0x64}, |
| 344 | {0x3CA4, 0x00}, |
| 345 | {0x3CA5, 0x00}, |
| 346 | {0x3CA6, 0x00}, |
| 347 | {0x3CA7, 0x00}, |
| 348 | {0x3CAA, 0x00}, |
| 349 | {0x3CAB, 0x00}, |
| 350 | {0x3CB8, 0x00}, |
| 351 | {0x3CB9, 0x08}, |
| 352 | {0x3CBA, 0x00}, |
| 353 | {0x3CBB, 0x00}, |
| 354 | {0x3CBC, 0x00}, |
| 355 | {0x3CBD, 0x3C}, |
| 356 | {0x3CBE, 0x00}, |
| 357 | {0x3CBF, 0x00}, |
| 358 | {0x0202, 0x0A}, |
| 359 | {0x0203, 0x29}, |
| 360 | {0x0224, 0x01}, |
| 361 | {0x0225, 0xF4}, |
| 362 | {0x3116, 0x01}, |
| 363 | {0x3117, 0xF4}, |
| 364 | {0x0204, 0x00}, |
| 365 | {0x0205, 0x00}, |
| 366 | {0x0216, 0x00}, |
| 367 | {0x0217, 0x00}, |
| 368 | {0x0218, 0x01}, |
| 369 | {0x0219, 0x00}, |
| 370 | {0x020E, 0x01}, |
| 371 | {0x020F, 0x00}, |
| 372 | {0x3118, 0x00}, |
| 373 | {0x3119, 0x00}, |
| 374 | {0x311A, 0x01}, |
| 375 | {0x311B, 0x00}, |
| 376 | {0x341a, 0x00}, |
| 377 | {0x341b, 0x00}, |
| 378 | {0x341c, 0x00}, |
| 379 | {0x341d, 0x00}, |
| 380 | {0x341e, 0x01}, |
| 381 | {0x341f, 0x20}, |
| 382 | {0x3420, 0x00}, |
| 383 | {0x3421, 0xd8}, |
| 384 | {0x3366, 0x00}, |
| 385 | {0x3367, 0x00}, |
| 386 | {0x3368, 0x00}, |
| 387 | {0x3369, 0x00}, |
| 388 | }; |
| 389 | |
| 390 | static const struct imx708_reg mode_2x2binned_regs[] = { |
| 391 | {0x0340, 0x05}, |
| 392 | {0x0341, 0x38}, |
| 393 | {0x0344, 0x00}, |
| 394 | {0x0345, 0x00}, |
| 395 | {0x0346, 0x00}, |
| 396 | {0x0347, 0x00}, |
| 397 | {0x0348, 0x11}, |
| 398 | {0x0349, 0xFF}, |
| 399 | {0x034A, 0X0A}, |
| 400 | {0x034B, 0x1F}, |
| 401 | {0x0220, 0x62}, |
| 402 | {0x0222, 0x01}, |
| 403 | {0x0900, 0x01}, |
| 404 | {0x0901, 0x22}, |
| 405 | {0x0902, 0x08}, |
| 406 | {0x3200, 0x41}, |
| 407 | {0x3201, 0x41}, |
| 408 | {0x32D5, 0x00}, |
| 409 | {0x32D6, 0x00}, |
| 410 | {0x32DB, 0x01}, |
| 411 | {0x32DF, 0x00}, |
| 412 | {0x350C, 0x00}, |
| 413 | {0x350D, 0x00}, |
| 414 | {0x0408, 0x00}, |
| 415 | {0x0409, 0x00}, |
| 416 | {0x040A, 0x00}, |
| 417 | {0x040B, 0x00}, |
| 418 | {0x040C, 0x09}, |
| 419 | {0x040D, 0x00}, |
| 420 | {0x040E, 0x05}, |
| 421 | {0x040F, 0x10}, |
| 422 | {0x034C, 0x09}, |
| 423 | {0x034D, 0x00}, |
| 424 | {0x034E, 0x05}, |
| 425 | {0x034F, 0x10}, |
| 426 | {0x0301, 0x05}, |
| 427 | {0x0303, 0x02}, |
| 428 | {0x0305, 0x02}, |
| 429 | {0x0306, 0x00}, |
| 430 | {0x0307, 0x7A}, |
| 431 | {0x030B, 0x02}, |
| 432 | {0x030D, 0x04}, |
| 433 | {0x0310, 0x01}, |
| 434 | {0x3CA0, 0x00}, |
| 435 | {0x3CA1, 0x3C}, |
| 436 | {0x3CA4, 0x00}, |
| 437 | {0x3CA5, 0x3C}, |
| 438 | {0x3CA6, 0x00}, |
| 439 | {0x3CA7, 0x00}, |
| 440 | {0x3CAA, 0x00}, |
| 441 | {0x3CAB, 0x00}, |
| 442 | {0x3CB8, 0x00}, |
| 443 | {0x3CB9, 0x1C}, |
| 444 | {0x3CBA, 0x00}, |
| 445 | {0x3CBB, 0x08}, |
| 446 | {0x3CBC, 0x00}, |
| 447 | {0x3CBD, 0x1E}, |
| 448 | {0x3CBE, 0x00}, |
| 449 | {0x3CBF, 0x0A}, |
| 450 | {0x0202, 0x05}, |
| 451 | {0x0203, 0x08}, |
| 452 | {0x0224, 0x01}, |
| 453 | {0x0225, 0xF4}, |
| 454 | {0x3116, 0x01}, |
| 455 | {0x3117, 0xF4}, |
| 456 | {0x0204, 0x00}, |
| 457 | {0x0205, 0x70}, |
| 458 | {0x0216, 0x00}, |
| 459 | {0x0217, 0x70}, |
| 460 | {0x0218, 0x01}, |
| 461 | {0x0219, 0x00}, |
| 462 | {0x020E, 0x01}, |
| 463 | {0x020F, 0x00}, |
| 464 | {0x3118, 0x00}, |
| 465 | {0x3119, 0x70}, |
| 466 | {0x311A, 0x01}, |
| 467 | {0x311B, 0x00}, |
| 468 | {0x341a, 0x00}, |
| 469 | {0x341b, 0x00}, |
| 470 | {0x341c, 0x00}, |
| 471 | {0x341d, 0x00}, |
| 472 | {0x341e, 0x00}, |
| 473 | {0x341f, 0x90}, |
| 474 | {0x3420, 0x00}, |
| 475 | {0x3421, 0x6c}, |
| 476 | {0x3366, 0x00}, |
| 477 | {0x3367, 0x00}, |
| 478 | {0x3368, 0x00}, |
| 479 | {0x3369, 0x00}, |
| 480 | }; |
| 481 | |
| 482 | static const struct imx708_reg mode_2x2binned_720p_regs[] = { |
| 483 | {0x0340, 0x04}, |
| 484 | {0x0341, 0xB6}, |
| 485 | {0x0344, 0x03}, |
| 486 | {0x0345, 0x00}, |
| 487 | {0x0346, 0x01}, |
| 488 | {0x0347, 0xB0}, |
| 489 | {0x0348, 0x0E}, |
| 490 | {0x0349, 0xFF}, |
| 491 | {0x034A, 0x08}, |
| 492 | {0x034B, 0x6F}, |
| 493 | {0x0220, 0x62}, |
| 494 | {0x0222, 0x01}, |
| 495 | {0x0900, 0x01}, |
| 496 | {0x0901, 0x22}, |
| 497 | {0x0902, 0x08}, |
| 498 | {0x3200, 0x41}, |
| 499 | {0x3201, 0x41}, |
| 500 | {0x32D5, 0x00}, |
| 501 | {0x32D6, 0x00}, |
| 502 | {0x32DB, 0x01}, |
| 503 | {0x32DF, 0x01}, |
| 504 | {0x350C, 0x00}, |
| 505 | {0x350D, 0x00}, |
| 506 | {0x0408, 0x00}, |
| 507 | {0x0409, 0x00}, |
| 508 | {0x040A, 0x00}, |
| 509 | {0x040B, 0x00}, |
| 510 | {0x040C, 0x06}, |
| 511 | {0x040D, 0x00}, |
| 512 | {0x040E, 0x03}, |
| 513 | {0x040F, 0x60}, |
| 514 | {0x034C, 0x06}, |
| 515 | {0x034D, 0x00}, |
| 516 | {0x034E, 0x03}, |
| 517 | {0x034F, 0x60}, |
| 518 | {0x0301, 0x05}, |
| 519 | {0x0303, 0x02}, |
| 520 | {0x0305, 0x02}, |
| 521 | {0x0306, 0x00}, |
| 522 | {0x0307, 0x76}, |
| 523 | {0x030B, 0x02}, |
| 524 | {0x030D, 0x04}, |
| 525 | {0x0310, 0x01}, |
| 526 | {0x3CA0, 0x00}, |
| 527 | {0x3CA1, 0x3C}, |
| 528 | {0x3CA4, 0x01}, |
| 529 | {0x3CA5, 0x5E}, |
| 530 | {0x3CA6, 0x00}, |
| 531 | {0x3CA7, 0x00}, |
| 532 | {0x3CAA, 0x00}, |
| 533 | {0x3CAB, 0x00}, |
| 534 | {0x3CB8, 0x00}, |
| 535 | {0x3CB9, 0x0C}, |
| 536 | {0x3CBA, 0x00}, |
| 537 | {0x3CBB, 0x04}, |
| 538 | {0x3CBC, 0x00}, |
| 539 | {0x3CBD, 0x1E}, |
| 540 | {0x3CBE, 0x00}, |
| 541 | {0x3CBF, 0x05}, |
| 542 | {0x0202, 0x04}, |
| 543 | {0x0203, 0x86}, |
| 544 | {0x0224, 0x01}, |
| 545 | {0x0225, 0xF4}, |
| 546 | {0x3116, 0x01}, |
| 547 | {0x3117, 0xF4}, |
| 548 | {0x0204, 0x00}, |
| 549 | {0x0205, 0x70}, |
| 550 | {0x0216, 0x00}, |
| 551 | {0x0217, 0x70}, |
| 552 | {0x0218, 0x01}, |
| 553 | {0x0219, 0x00}, |
| 554 | {0x020E, 0x01}, |
| 555 | {0x020F, 0x00}, |
| 556 | {0x3118, 0x00}, |
| 557 | {0x3119, 0x70}, |
| 558 | {0x311A, 0x01}, |
| 559 | {0x311B, 0x00}, |
| 560 | {0x341a, 0x00}, |
| 561 | {0x341b, 0x00}, |
| 562 | {0x341c, 0x00}, |
| 563 | {0x341d, 0x00}, |
| 564 | {0x341e, 0x00}, |
| 565 | {0x341f, 0x60}, |
| 566 | {0x3420, 0x00}, |
| 567 | {0x3421, 0x48}, |
| 568 | {0x3366, 0x00}, |
| 569 | {0x3367, 0x00}, |
| 570 | {0x3368, 0x00}, |
| 571 | {0x3369, 0x00}, |
| 572 | }; |
| 573 | |
| 574 | static const struct imx708_reg mode_hdr_regs[] = { |
| 575 | {0x0340, 0x0A}, |
| 576 | {0x0341, 0x5B}, |
| 577 | {0x0344, 0x00}, |
| 578 | {0x0345, 0x00}, |
| 579 | {0x0346, 0x00}, |
| 580 | {0x0347, 0x00}, |
| 581 | {0x0348, 0x11}, |
| 582 | {0x0349, 0xFF}, |
| 583 | {0x034A, 0X0A}, |
| 584 | {0x034B, 0x1F}, |
| 585 | {0x0220, 0x01}, |
| 586 | {0x0222, IMX708_HDR_EXPOSURE_RATIO}, |
| 587 | {0x0900, 0x00}, |
| 588 | {0x0901, 0x11}, |
| 589 | {0x0902, 0x0A}, |
| 590 | {0x3200, 0x01}, |
| 591 | {0x3201, 0x01}, |
| 592 | {0x32D5, 0x00}, |
| 593 | {0x32D6, 0x00}, |
| 594 | {0x32DB, 0x01}, |
| 595 | {0x32DF, 0x00}, |
| 596 | {0x350C, 0x00}, |
| 597 | {0x350D, 0x00}, |
| 598 | {0x0408, 0x00}, |
| 599 | {0x0409, 0x00}, |
| 600 | {0x040A, 0x00}, |
| 601 | {0x040B, 0x00}, |
| 602 | {0x040C, 0x09}, |
| 603 | {0x040D, 0x00}, |
| 604 | {0x040E, 0x05}, |
| 605 | {0x040F, 0x10}, |
| 606 | {0x034C, 0x09}, |
| 607 | {0x034D, 0x00}, |
| 608 | {0x034E, 0x05}, |
| 609 | {0x034F, 0x10}, |
| 610 | {0x0301, 0x05}, |
| 611 | {0x0303, 0x02}, |
| 612 | {0x0305, 0x02}, |
| 613 | {0x0306, 0x00}, |
| 614 | {0x0307, 0xA2}, |
| 615 | {0x030B, 0x02}, |
| 616 | {0x030D, 0x04}, |
| 617 | {0x0310, 0x01}, |
| 618 | {0x3CA0, 0x00}, |
| 619 | {0x3CA1, 0x00}, |
| 620 | {0x3CA4, 0x00}, |
| 621 | {0x3CA5, 0x00}, |
| 622 | {0x3CA6, 0x00}, |
| 623 | {0x3CA7, 0x28}, |
| 624 | {0x3CAA, 0x00}, |
| 625 | {0x3CAB, 0x00}, |
| 626 | {0x3CB8, 0x00}, |
| 627 | {0x3CB9, 0x30}, |
| 628 | {0x3CBA, 0x00}, |
| 629 | {0x3CBB, 0x00}, |
| 630 | {0x3CBC, 0x00}, |
| 631 | {0x3CBD, 0x32}, |
| 632 | {0x3CBE, 0x00}, |
| 633 | {0x3CBF, 0x00}, |
| 634 | {0x0202, 0x0A}, |
| 635 | {0x0203, 0x2B}, |
| 636 | {0x0224, 0x0A}, |
| 637 | {0x0225, 0x2B}, |
| 638 | {0x3116, 0x0A}, |
| 639 | {0x3117, 0x2B}, |
| 640 | {0x0204, 0x00}, |
| 641 | {0x0205, 0x00}, |
| 642 | {0x0216, 0x00}, |
| 643 | {0x0217, 0x00}, |
| 644 | {0x0218, 0x01}, |
| 645 | {0x0219, 0x00}, |
| 646 | {0x020E, 0x01}, |
| 647 | {0x020F, 0x00}, |
| 648 | {0x3118, 0x00}, |
| 649 | {0x3119, 0x00}, |
| 650 | {0x311A, 0x01}, |
| 651 | {0x311B, 0x00}, |
| 652 | {0x341a, 0x00}, |
| 653 | {0x341b, 0x00}, |
| 654 | {0x341c, 0x00}, |
| 655 | {0x341d, 0x00}, |
| 656 | {0x341e, 0x00}, |
| 657 | {0x341f, 0x90}, |
| 658 | {0x3420, 0x00}, |
| 659 | {0x3421, 0x6c}, |
| 660 | {0x3360, 0x01}, |
| 661 | {0x3361, 0x01}, |
| 662 | {0x3366, 0x09}, |
| 663 | {0x3367, 0x00}, |
| 664 | {0x3368, 0x05}, |
| 665 | {0x3369, 0x10}, |
| 666 | }; |
| 667 | |
| 668 | /* Mode configs. Keep separate lists for when HDR is enabled or not. */ |
| 669 | static const struct imx708_mode supported_modes_10bit_no_hdr[] = { |
| 670 | { |
| 671 | /* Full resolution. */ |
| 672 | .width = 4608, |
| 673 | .height = 2592, |
| 674 | .line_length_pix = 10432, |
| 675 | .crop = { |
| 676 | .left = IMX708_PIXEL_ARRAY_LEFT, |
| 677 | .top = IMX708_PIXEL_ARRAY_TOP, |
| 678 | .width = 4608, |
| 679 | .height = 2592, |
| 680 | }, |
| 681 | .vblank_min = 58, |
| 682 | .vblank_default = 58, |
| 683 | .reg_list = { |
| 684 | .num_of_regs = ARRAY_SIZE(mode_4608x2592_regs), |
| 685 | .regs = mode_4608x2592_regs, |
| 686 | }, |
| 687 | .pixel_rate = 595200000, |
| 688 | .exposure_lines_min = 8, |
| 689 | .exposure_lines_step = 1, |
| 690 | .hdr = false, |
| 691 | .remosaic = true |
| 692 | }, |
| 693 | { |
| 694 | /* regular 2x2 binned. */ |
| 695 | .width = 2304, |
| 696 | .height = 1296, |
| 697 | .line_length_pix = 5216, |
| 698 | .crop = { |
| 699 | .left = IMX708_PIXEL_ARRAY_LEFT, |
| 700 | .top = IMX708_PIXEL_ARRAY_TOP, |
| 701 | .width = 4608, |
| 702 | .height = 2592, |
| 703 | }, |
| 704 | .vblank_min = 40, |
| 705 | .vblank_default = 1198, |
| 706 | .reg_list = { |
| 707 | .num_of_regs = ARRAY_SIZE(mode_2x2binned_regs), |
| 708 | .regs = mode_2x2binned_regs, |
| 709 | }, |
| 710 | .pixel_rate = 585600000, |
| 711 | .exposure_lines_min = 4, |
| 712 | .exposure_lines_step = 2, |
| 713 | .hdr = false, |
| 714 | .remosaic = false |
| 715 | }, |
| 716 | { |
| 717 | /* 2x2 binned and cropped for 720p. */ |
| 718 | .width = 1536, |
| 719 | .height = 864, |
| 720 | .line_length_pix = 5216, |
| 721 | .crop = { |
| 722 | .left = IMX708_PIXEL_ARRAY_LEFT + 768, |
| 723 | .top = IMX708_PIXEL_ARRAY_TOP + 432, |
| 724 | .width = 3072, |
| 725 | .height = 1728, |
| 726 | }, |
| 727 | .vblank_min = 40, |
| 728 | .vblank_default = 2755, |
| 729 | .reg_list = { |
| 730 | .num_of_regs = ARRAY_SIZE(mode_2x2binned_720p_regs), |
| 731 | .regs = mode_2x2binned_720p_regs, |
| 732 | }, |
| 733 | .pixel_rate = 566400000, |
| 734 | .exposure_lines_min = 4, |
| 735 | .exposure_lines_step = 2, |
| 736 | .hdr = false, |
| 737 | .remosaic = false |
| 738 | }, |
| 739 | }; |
| 740 | |
| 741 | static const struct imx708_mode supported_modes_10bit_hdr[] = { |
| 742 | { |
| 743 | /* There's only one HDR mode, which is 2x2 downscaled */ |
| 744 | .width = 2304, |
| 745 | .height = 1296, |
| 746 | .line_length_pix = 5216, |
| 747 | .crop = { |
| 748 | .left = IMX708_PIXEL_ARRAY_LEFT, |
| 749 | .top = IMX708_PIXEL_ARRAY_TOP, |
| 750 | .width = 4608, |
| 751 | .height = 2592, |
| 752 | }, |
| 753 | .vblank_min = 3673, |
| 754 | .vblank_default = 3673, |
| 755 | .reg_list = { |
| 756 | .num_of_regs = ARRAY_SIZE(mode_hdr_regs), |
| 757 | .regs = mode_hdr_regs, |
| 758 | }, |
| 759 | .pixel_rate = 777600000, |
| 760 | .exposure_lines_min = 8 * IMX708_HDR_EXPOSURE_RATIO * IMX708_HDR_EXPOSURE_RATIO, |
| 761 | .exposure_lines_step = 2 * IMX708_HDR_EXPOSURE_RATIO * IMX708_HDR_EXPOSURE_RATIO, |
| 762 | .hdr = true, |
| 763 | .remosaic = false |
| 764 | } |
| 765 | }; |
| 766 | |
| 767 | /* |
| 768 | * The supported formats. |
| 769 | * This table MUST contain 4 entries per format, to cover the various flip |
| 770 | * combinations in the order |
| 771 | * - no flip |
| 772 | * - h flip |
| 773 | * - v flip |
| 774 | * - h&v flips |
| 775 | */ |
| 776 | static const u32 codes[] = { |
| 777 | /* 10-bit modes. */ |
| 778 | MEDIA_BUS_FMT_SRGGB10_1X10, |
| 779 | MEDIA_BUS_FMT_SGRBG10_1X10, |
| 780 | MEDIA_BUS_FMT_SGBRG10_1X10, |
| 781 | MEDIA_BUS_FMT_SBGGR10_1X10, |
| 782 | }; |
| 783 | |
| 784 | static const char * const imx708_test_pattern_menu[] = { |
| 785 | "Disabled", |
| 786 | "Color Bars", |
| 787 | "Solid Color", |
| 788 | "Grey Color Bars", |
| 789 | "PN9" |
| 790 | }; |
| 791 | |
| 792 | static const int imx708_test_pattern_val[] = { |
| 793 | IMX708_TEST_PATTERN_DISABLE, |
| 794 | IMX708_TEST_PATTERN_COLOR_BARS, |
| 795 | IMX708_TEST_PATTERN_SOLID_COLOR, |
| 796 | IMX708_TEST_PATTERN_GREY_COLOR, |
| 797 | IMX708_TEST_PATTERN_PN9, |
| 798 | }; |
| 799 | |
| 800 | /* regulator supplies */ |
| 801 | static const char * const imx708_supply_name[] = { |
| 802 | /* Supplies can be enabled in any order */ |
| 803 | "vana1", /* Analog1 (2.8V) supply */ |
| 804 | "vana2", /* Analog2 (1.8V) supply */ |
| 805 | "vdig", /* Digital Core (1.1V) supply */ |
| 806 | "vddl", /* IF (1.8V) supply */ |
| 807 | }; |
| 808 | |
| 809 | /* |
| 810 | * Initialisation delay between XCLR low->high and the moment when the sensor |
| 811 | * can start capture (i.e. can leave software standby), given by T7 in the |
| 812 | * datasheet is 8ms. This does include I2C setup time as well. |
| 813 | * |
| 814 | * Note, that delay between XCLR low->high and reading the CCI ID register (T6 |
| 815 | * in the datasheet) is much smaller - 600us. |
| 816 | */ |
| 817 | #define IMX708_XCLR_MIN_DELAY_US 8000 |
| 818 | #define IMX708_XCLR_DELAY_RANGE_US 1000 |
| 819 | |
| 820 | struct imx708 { |
| 821 | struct v4l2_subdev sd; |
| 822 | struct media_pad pad[NUM_PADS]; |
| 823 | |
| 824 | struct v4l2_mbus_framefmt fmt; |
| 825 | |
| 826 | struct clk *inclk; |
| 827 | u32 inclk_freq; |
| 828 | |
| 829 | struct gpio_desc *reset_gpio; |
| 830 | struct regulator_bulk_data supplies[ARRAY_SIZE(imx708_supply_name)]; |
| 831 | |
| 832 | struct v4l2_ctrl_handler ctrl_handler; |
| 833 | /* V4L2 Controls */ |
| 834 | struct v4l2_ctrl *pixel_rate; |
| 835 | struct v4l2_ctrl *exposure; |
| 836 | struct v4l2_ctrl *vblank; |
| 837 | struct v4l2_ctrl *hblank; |
| 838 | struct v4l2_ctrl *hdr_mode; |
| 839 | struct v4l2_ctrl *link_freq; |
| 840 | struct { |
| 841 | struct v4l2_ctrl *hflip; |
| 842 | struct v4l2_ctrl *vflip; |
| 843 | }; |
| 844 | |
| 845 | /* Current mode */ |
| 846 | const struct imx708_mode *mode; |
| 847 | |
| 848 | /* |
| 849 | * Mutex for serialized access: |
| 850 | * Protect sensor module set pad format and start/stop streaming safely. |
| 851 | */ |
| 852 | struct mutex mutex; |
| 853 | |
| 854 | /* Streaming on/off */ |
| 855 | bool streaming; |
| 856 | |
| 857 | /* Rewrite common registers on stream on? */ |
| 858 | bool common_regs_written; |
| 859 | |
| 860 | /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */ |
| 861 | unsigned int long_exp_shift; |
| 862 | |
| 863 | unsigned int link_freq_idx; |
| 864 | }; |
| 865 | |
| 866 | static inline struct imx708 *to_imx708(struct v4l2_subdev *_sd) |
| 867 | { |
| 868 | return container_of(_sd, struct imx708, sd); |
| 869 | } |
| 870 | |
| 871 | static inline void get_mode_table(unsigned int code, |
| 872 | const struct imx708_mode **mode_list, |
| 873 | unsigned int *num_modes, |
| 874 | bool hdr_enable) |
| 875 | { |
| 876 | switch (code) { |
| 877 | /* 10-bit */ |
| 878 | case MEDIA_BUS_FMT_SRGGB10_1X10: |
| 879 | case MEDIA_BUS_FMT_SGRBG10_1X10: |
| 880 | case MEDIA_BUS_FMT_SGBRG10_1X10: |
| 881 | case MEDIA_BUS_FMT_SBGGR10_1X10: |
| 882 | if (hdr_enable) { |
| 883 | *mode_list = supported_modes_10bit_hdr; |
| 884 | *num_modes = ARRAY_SIZE(supported_modes_10bit_hdr); |
| 885 | } else { |
| 886 | *mode_list = supported_modes_10bit_no_hdr; |
| 887 | *num_modes = ARRAY_SIZE(supported_modes_10bit_no_hdr); |
| 888 | } |
| 889 | break; |
| 890 | default: |
| 891 | *mode_list = NULL; |
| 892 | *num_modes = 0; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /* Read registers up to 2 at a time */ |
| 897 | static int imx708_read_reg(struct imx708 *imx708, u16 reg, u32 len, u32 *val) |
| 898 | { |
| 899 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 900 | struct i2c_msg msgs[2]; |
| 901 | u8 addr_buf[2] = { reg >> 8, reg & 0xff }; |
| 902 | u8 data_buf[4] = { 0, }; |
| 903 | int ret; |
| 904 | |
| 905 | if (len > 4) |
| 906 | return -EINVAL; |
| 907 | |
| 908 | /* Write register address */ |
| 909 | msgs[0].addr = client->addr; |
| 910 | msgs[0].flags = 0; |
| 911 | msgs[0].len = ARRAY_SIZE(addr_buf); |
| 912 | msgs[0].buf = addr_buf; |
| 913 | |
| 914 | /* Read data from register */ |
| 915 | msgs[1].addr = client->addr; |
| 916 | msgs[1].flags = I2C_M_RD; |
| 917 | msgs[1].len = len; |
| 918 | msgs[1].buf = &data_buf[4 - len]; |
| 919 | |
| 920 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 921 | if (ret != ARRAY_SIZE(msgs)) |
| 922 | return -EIO; |
| 923 | |
| 924 | *val = get_unaligned_be32(data_buf); |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* Write registers up to 2 at a time */ |
| 930 | static int imx708_write_reg(struct imx708 *imx708, u16 reg, u32 len, u32 val) |
| 931 | { |
| 932 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 933 | u8 buf[6]; |
| 934 | |
| 935 | if (len > 4) |
| 936 | return -EINVAL; |
| 937 | |
| 938 | put_unaligned_be16(reg, buf); |
| 939 | put_unaligned_be32(val << (8 * (4 - len)), buf + 2); |
| 940 | if (i2c_master_send(client, buf, len + 2) != len + 2) |
| 941 | return -EIO; |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | /* Write a list of registers */ |
| 947 | static int imx708_write_regs(struct imx708 *imx708, |
| 948 | const struct imx708_reg *regs, u32 len) |
| 949 | { |
| 950 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 951 | unsigned int i; |
| 952 | |
| 953 | for (i = 0; i < len; i++) { |
| 954 | int ret; |
| 955 | |
| 956 | ret = imx708_write_reg(imx708, regs[i].address, 1, regs[i].val); |
| 957 | if (ret) { |
| 958 | dev_err_ratelimited(&client->dev, |
| 959 | "Failed to write reg 0x%4.4x. error = %d\n", |
| 960 | regs[i].address, ret); |
| 961 | |
| 962 | return ret; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | /* Get bayer order based on flip setting. */ |
| 970 | static u32 imx708_get_format_code(struct imx708 *imx708) |
| 971 | { |
| 972 | unsigned int i; |
| 973 | |
| 974 | lockdep_assert_held(&imx708->mutex); |
| 975 | |
| 976 | i = (imx708->vflip->val ? 2 : 0) | |
| 977 | (imx708->hflip->val ? 1 : 0); |
| 978 | |
| 979 | return codes[i]; |
| 980 | } |
| 981 | |
| 982 | static void imx708_set_default_format(struct imx708 *imx708) |
| 983 | { |
| 984 | struct v4l2_mbus_framefmt *fmt = &imx708->fmt; |
| 985 | |
| 986 | /* Set default mode to max resolution */ |
| 987 | imx708->mode = &supported_modes_10bit_no_hdr[0]; |
| 988 | |
| 989 | /* fmt->code not set as it will always be computed based on flips */ |
| 990 | fmt->colorspace = V4L2_COLORSPACE_RAW; |
| 991 | fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); |
| 992 | fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, |
| 993 | fmt->colorspace, |
| 994 | fmt->ycbcr_enc); |
| 995 | fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); |
| 996 | fmt->width = imx708->mode->width; |
| 997 | fmt->height = imx708->mode->height; |
| 998 | fmt->field = V4L2_FIELD_NONE; |
| 999 | } |
| 1000 | |
| 1001 | static int imx708_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) |
| 1002 | { |
| 1003 | struct imx708 *imx708 = to_imx708(sd); |
| 1004 | struct v4l2_mbus_framefmt *try_fmt_img = |
| 1005 | v4l2_subdev_state_get_format(fh->state, IMAGE_PAD); |
| 1006 | struct v4l2_mbus_framefmt *try_fmt_meta = |
| 1007 | v4l2_subdev_state_get_format(fh->state, METADATA_PAD); |
| 1008 | struct v4l2_rect *try_crop; |
| 1009 | |
| 1010 | mutex_lock(&imx708->mutex); |
| 1011 | |
| 1012 | /* Initialize try_fmt for the image pad */ |
| 1013 | if (imx708->hdr_mode->val) { |
| 1014 | try_fmt_img->width = supported_modes_10bit_hdr[0].width; |
| 1015 | try_fmt_img->height = supported_modes_10bit_hdr[0].height; |
| 1016 | } else { |
| 1017 | try_fmt_img->width = supported_modes_10bit_no_hdr[0].width; |
| 1018 | try_fmt_img->height = supported_modes_10bit_no_hdr[0].height; |
| 1019 | } |
| 1020 | try_fmt_img->code = imx708_get_format_code(imx708); |
| 1021 | try_fmt_img->field = V4L2_FIELD_NONE; |
| 1022 | |
| 1023 | /* Initialize try_fmt for the embedded metadata pad */ |
| 1024 | try_fmt_meta->width = IMX708_EMBEDDED_LINE_WIDTH; |
| 1025 | try_fmt_meta->height = IMX708_NUM_EMBEDDED_LINES; |
| 1026 | try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA; |
| 1027 | try_fmt_meta->field = V4L2_FIELD_NONE; |
| 1028 | |
| 1029 | /* Initialize try_crop */ |
| 1030 | try_crop = v4l2_subdev_state_get_crop(fh->state, IMAGE_PAD); |
| 1031 | try_crop->left = IMX708_PIXEL_ARRAY_LEFT; |
| 1032 | try_crop->top = IMX708_PIXEL_ARRAY_TOP; |
| 1033 | try_crop->width = IMX708_PIXEL_ARRAY_WIDTH; |
| 1034 | try_crop->height = IMX708_PIXEL_ARRAY_HEIGHT; |
| 1035 | |
| 1036 | mutex_unlock(&imx708->mutex); |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | static int imx708_set_exposure(struct imx708 *imx708, unsigned int val) |
| 1042 | { |
| 1043 | val = max(val, imx708->mode->exposure_lines_min); |
| 1044 | val -= val % imx708->mode->exposure_lines_step; |
| 1045 | |
| 1046 | /* |
| 1047 | * In HDR mode this will set the longest exposure. The sensor |
| 1048 | * will automatically divide the medium and short ones by 4,16. |
| 1049 | */ |
| 1050 | return imx708_write_reg(imx708, IMX708_REG_EXPOSURE, |
| 1051 | IMX708_REG_VALUE_16BIT, |
| 1052 | val >> imx708->long_exp_shift); |
| 1053 | } |
| 1054 | |
| 1055 | static void imx708_adjust_exposure_range(struct imx708 *imx708, |
| 1056 | struct v4l2_ctrl *ctrl) |
| 1057 | { |
| 1058 | int exposure_max, exposure_def; |
| 1059 | |
| 1060 | /* Honour the VBLANK limits when setting exposure. */ |
| 1061 | exposure_max = imx708->mode->height + imx708->vblank->val - |
| 1062 | IMX708_EXPOSURE_OFFSET; |
| 1063 | exposure_def = min(exposure_max, imx708->exposure->val); |
| 1064 | __v4l2_ctrl_modify_range(imx708->exposure, imx708->exposure->minimum, |
| 1065 | exposure_max, imx708->exposure->step, |
| 1066 | exposure_def); |
| 1067 | } |
| 1068 | |
| 1069 | static int imx708_set_analogue_gain(struct imx708 *imx708, unsigned int val) |
| 1070 | { |
| 1071 | int ret; |
| 1072 | |
| 1073 | /* |
| 1074 | * In HDR mode this will set the gain for the longest exposure, |
| 1075 | * and by default the sensor uses the same gain for all of them. |
| 1076 | */ |
| 1077 | ret = imx708_write_reg(imx708, IMX708_REG_ANALOG_GAIN, |
| 1078 | IMX708_REG_VALUE_16BIT, val); |
| 1079 | |
| 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | static int imx708_set_frame_length(struct imx708 *imx708, unsigned int val) |
| 1084 | { |
| 1085 | int ret; |
| 1086 | |
| 1087 | imx708->long_exp_shift = 0; |
| 1088 | |
| 1089 | while (val > IMX708_FRAME_LENGTH_MAX) { |
| 1090 | imx708->long_exp_shift++; |
| 1091 | val >>= 1; |
| 1092 | } |
| 1093 | |
| 1094 | ret = imx708_write_reg(imx708, IMX708_REG_FRAME_LENGTH, |
| 1095 | IMX708_REG_VALUE_16BIT, val); |
| 1096 | if (ret) |
| 1097 | return ret; |
| 1098 | |
| 1099 | return imx708_write_reg(imx708, IMX708_LONG_EXP_SHIFT_REG, |
| 1100 | IMX708_REG_VALUE_08BIT, imx708->long_exp_shift); |
| 1101 | } |
| 1102 | |
| 1103 | static void imx708_set_framing_limits(struct imx708 *imx708) |
| 1104 | { |
| 1105 | const struct imx708_mode *mode = imx708->mode; |
| 1106 | unsigned int hblank; |
| 1107 | |
| 1108 | __v4l2_ctrl_modify_range(imx708->pixel_rate, |
| 1109 | mode->pixel_rate, mode->pixel_rate, |
| 1110 | 1, mode->pixel_rate); |
| 1111 | |
| 1112 | /* Update limits and set FPS to default */ |
| 1113 | __v4l2_ctrl_modify_range(imx708->vblank, mode->vblank_min, |
| 1114 | ((1 << IMX708_LONG_EXP_SHIFT_MAX) * |
| 1115 | IMX708_FRAME_LENGTH_MAX) - mode->height, |
| 1116 | 1, mode->vblank_default); |
| 1117 | |
| 1118 | hblank = mode->line_length_pix - mode->width; |
| 1119 | __v4l2_ctrl_modify_range(imx708->hblank, hblank, hblank, 1, hblank); |
| 1120 | } |
| 1121 | |
| 1122 | static int imx708_set_ctrl(struct v4l2_ctrl *ctrl) |
| 1123 | { |
| 1124 | struct imx708 *imx708 = |
| 1125 | container_of(ctrl->handler, struct imx708, ctrl_handler); |
| 1126 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 1127 | const struct imx708_mode *mode_list; |
| 1128 | unsigned int code, num_modes; |
| 1129 | int ret = 0; |
| 1130 | |
| 1131 | switch (ctrl->id) { |
| 1132 | case V4L2_CID_VBLANK: |
| 1133 | /* |
| 1134 | * The VBLANK control may change the limits of usable exposure, |
| 1135 | * so check and adjust if necessary. |
| 1136 | */ |
| 1137 | imx708_adjust_exposure_range(imx708, ctrl); |
| 1138 | break; |
| 1139 | |
| 1140 | case V4L2_CID_WIDE_DYNAMIC_RANGE: |
| 1141 | /* |
| 1142 | * The WIDE_DYNAMIC_RANGE control can also be applied immediately |
| 1143 | * as it doesn't set any registers. Don't do anything if the mode |
| 1144 | * already matches. |
| 1145 | */ |
| 1146 | if (imx708->mode && imx708->mode->hdr != ctrl->val) { |
| 1147 | code = imx708_get_format_code(imx708); |
| 1148 | get_mode_table(code, &mode_list, &num_modes, ctrl->val); |
| 1149 | imx708->mode = v4l2_find_nearest_size(mode_list, |
| 1150 | num_modes, |
| 1151 | width, height, |
| 1152 | imx708->mode->width, |
| 1153 | imx708->mode->height); |
| 1154 | imx708_set_framing_limits(imx708); |
| 1155 | } |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Applying V4L2 control value only happens |
| 1161 | * when power is up for streaming |
| 1162 | */ |
| 1163 | if (pm_runtime_get_if_in_use(&client->dev) == 0) |
| 1164 | return 0; |
| 1165 | |
| 1166 | switch (ctrl->id) { |
| 1167 | case V4L2_CID_ANALOGUE_GAIN: |
| 1168 | imx708_set_analogue_gain(imx708, ctrl->val); |
| 1169 | break; |
| 1170 | case V4L2_CID_EXPOSURE: |
| 1171 | ret = imx708_set_exposure(imx708, ctrl->val); |
| 1172 | break; |
| 1173 | case V4L2_CID_DIGITAL_GAIN: |
| 1174 | ret = imx708_write_reg(imx708, IMX708_REG_DIGITAL_GAIN, |
| 1175 | IMX708_REG_VALUE_16BIT, ctrl->val); |
| 1176 | break; |
| 1177 | case V4L2_CID_TEST_PATTERN: |
| 1178 | ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN, |
| 1179 | IMX708_REG_VALUE_16BIT, |
| 1180 | imx708_test_pattern_val[ctrl->val]); |
| 1181 | break; |
| 1182 | case V4L2_CID_TEST_PATTERN_RED: |
| 1183 | ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_R, |
| 1184 | IMX708_REG_VALUE_16BIT, ctrl->val); |
| 1185 | break; |
| 1186 | case V4L2_CID_TEST_PATTERN_GREENR: |
| 1187 | ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_GR, |
| 1188 | IMX708_REG_VALUE_16BIT, ctrl->val); |
| 1189 | break; |
| 1190 | case V4L2_CID_TEST_PATTERN_BLUE: |
| 1191 | ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_B, |
| 1192 | IMX708_REG_VALUE_16BIT, ctrl->val); |
| 1193 | break; |
| 1194 | case V4L2_CID_TEST_PATTERN_GREENB: |
| 1195 | ret = imx708_write_reg(imx708, IMX708_REG_TEST_PATTERN_GB, |
| 1196 | IMX708_REG_VALUE_16BIT, ctrl->val); |
| 1197 | break; |
| 1198 | case V4L2_CID_HFLIP: |
| 1199 | case V4L2_CID_VFLIP: |
| 1200 | ret = imx708_write_reg(imx708, IMX708_REG_ORIENTATION, 1, |
| 1201 | imx708->hflip->val | |
| 1202 | imx708->vflip->val << 1); |
| 1203 | break; |
| 1204 | case V4L2_CID_VBLANK: |
| 1205 | ret = imx708_set_frame_length(imx708, |
| 1206 | imx708->mode->height + ctrl->val); |
| 1207 | break; |
| 1208 | case V4L2_CID_HBLANK: |
| 1209 | ret = imx708_write_reg(imx708, IMX708_REG_LINE_LENGTH, 2, |
| 1210 | imx708->mode->width + ctrl->val); |
| 1211 | break; |
| 1212 | case V4L2_CID_NOTIFY_GAINS: |
| 1213 | ret = imx708_write_reg(imx708, IMX708_REG_COLOUR_BALANCE_BLUE, |
| 1214 | IMX708_REG_VALUE_16BIT, |
| 1215 | ctrl->p_new.p_u32[0]); |
| 1216 | if (ret) |
| 1217 | break; |
| 1218 | ret = imx708_write_reg(imx708, IMX708_REG_COLOUR_BALANCE_RED, |
| 1219 | IMX708_REG_VALUE_16BIT, |
| 1220 | ctrl->p_new.p_u32[3]); |
| 1221 | break; |
| 1222 | case V4L2_CID_WIDE_DYNAMIC_RANGE: |
| 1223 | /* Already handled above. */ |
| 1224 | break; |
| 1225 | default: |
| 1226 | dev_info(&client->dev, |
| 1227 | "ctrl(id:0x%x,val:0x%x) is not handled\n", |
| 1228 | ctrl->id, ctrl->val); |
| 1229 | ret = -EINVAL; |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | pm_runtime_put(&client->dev); |
| 1234 | |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | static const struct v4l2_ctrl_ops imx708_ctrl_ops = { |
| 1239 | .s_ctrl = imx708_set_ctrl, |
| 1240 | }; |
| 1241 | |
| 1242 | static int imx708_enum_mbus_code(struct v4l2_subdev *sd, |
| 1243 | struct v4l2_subdev_state *sd_state, |
| 1244 | struct v4l2_subdev_mbus_code_enum *code) |
| 1245 | { |
| 1246 | struct imx708 *imx708 = to_imx708(sd); |
| 1247 | |
| 1248 | if (code->pad >= NUM_PADS) |
| 1249 | return -EINVAL; |
| 1250 | |
| 1251 | if (code->pad == IMAGE_PAD) { |
| 1252 | if (code->index >= (ARRAY_SIZE(codes) / 4)) |
| 1253 | return -EINVAL; |
| 1254 | |
| 1255 | code->code = imx708_get_format_code(imx708); |
| 1256 | } else { |
| 1257 | if (code->index > 0) |
| 1258 | return -EINVAL; |
| 1259 | |
| 1260 | code->code = MEDIA_BUS_FMT_SENSOR_DATA; |
| 1261 | } |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | static int imx708_enum_frame_size(struct v4l2_subdev *sd, |
| 1267 | struct v4l2_subdev_state *sd_state, |
| 1268 | struct v4l2_subdev_frame_size_enum *fse) |
| 1269 | { |
| 1270 | struct imx708 *imx708 = to_imx708(sd); |
| 1271 | |
| 1272 | if (fse->pad >= NUM_PADS) |
| 1273 | return -EINVAL; |
| 1274 | |
| 1275 | if (fse->pad == IMAGE_PAD) { |
| 1276 | const struct imx708_mode *mode_list; |
| 1277 | unsigned int num_modes; |
| 1278 | |
| 1279 | get_mode_table(fse->code, &mode_list, &num_modes, |
| 1280 | imx708->hdr_mode->val); |
| 1281 | |
| 1282 | if (fse->index >= num_modes) |
| 1283 | return -EINVAL; |
| 1284 | |
| 1285 | if (fse->code != imx708_get_format_code(imx708)) |
| 1286 | return -EINVAL; |
| 1287 | |
| 1288 | fse->min_width = mode_list[fse->index].width; |
| 1289 | fse->max_width = fse->min_width; |
| 1290 | fse->min_height = mode_list[fse->index].height; |
| 1291 | fse->max_height = fse->min_height; |
| 1292 | } else { |
| 1293 | if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0) |
| 1294 | return -EINVAL; |
| 1295 | |
| 1296 | fse->min_width = IMX708_EMBEDDED_LINE_WIDTH; |
| 1297 | fse->max_width = fse->min_width; |
| 1298 | fse->min_height = IMX708_NUM_EMBEDDED_LINES; |
| 1299 | fse->max_height = fse->min_height; |
| 1300 | } |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | static void imx708_reset_colorspace(struct v4l2_mbus_framefmt *fmt) |
| 1306 | { |
| 1307 | fmt->colorspace = V4L2_COLORSPACE_RAW; |
| 1308 | fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); |
| 1309 | fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, |
| 1310 | fmt->colorspace, |
| 1311 | fmt->ycbcr_enc); |
| 1312 | fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); |
| 1313 | } |
| 1314 | |
| 1315 | static void imx708_update_image_pad_format(struct imx708 *imx708, |
| 1316 | const struct imx708_mode *mode, |
| 1317 | struct v4l2_subdev_format *fmt) |
| 1318 | { |
| 1319 | fmt->format.width = mode->width; |
| 1320 | fmt->format.height = mode->height; |
| 1321 | fmt->format.field = V4L2_FIELD_NONE; |
| 1322 | imx708_reset_colorspace(&fmt->format); |
| 1323 | } |
| 1324 | |
| 1325 | static void imx708_update_metadata_pad_format(struct v4l2_subdev_format *fmt) |
| 1326 | { |
| 1327 | fmt->format.width = IMX708_EMBEDDED_LINE_WIDTH; |
| 1328 | fmt->format.height = IMX708_NUM_EMBEDDED_LINES; |
| 1329 | fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA; |
| 1330 | fmt->format.field = V4L2_FIELD_NONE; |
| 1331 | } |
| 1332 | |
| 1333 | static int imx708_get_pad_format(struct v4l2_subdev *sd, |
| 1334 | struct v4l2_subdev_state *sd_state, |
| 1335 | struct v4l2_subdev_format *fmt) |
| 1336 | { |
| 1337 | struct imx708 *imx708 = to_imx708(sd); |
| 1338 | |
| 1339 | if (fmt->pad >= NUM_PADS) |
| 1340 | return -EINVAL; |
| 1341 | |
| 1342 | mutex_lock(&imx708->mutex); |
| 1343 | |
| 1344 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1345 | struct v4l2_mbus_framefmt *try_fmt = |
| 1346 | v4l2_subdev_state_get_format(sd_state, |
| 1347 | fmt->pad); |
| 1348 | /* update the code which could change due to vflip or hflip */ |
| 1349 | try_fmt->code = fmt->pad == IMAGE_PAD ? |
| 1350 | imx708_get_format_code(imx708) : |
| 1351 | MEDIA_BUS_FMT_SENSOR_DATA; |
| 1352 | fmt->format = *try_fmt; |
| 1353 | } else { |
| 1354 | if (fmt->pad == IMAGE_PAD) { |
| 1355 | imx708_update_image_pad_format(imx708, imx708->mode, |
| 1356 | fmt); |
| 1357 | fmt->format.code = imx708_get_format_code(imx708); |
| 1358 | } else { |
| 1359 | imx708_update_metadata_pad_format(fmt); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | mutex_unlock(&imx708->mutex); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | static int imx708_set_pad_format(struct v4l2_subdev *sd, |
| 1368 | struct v4l2_subdev_state *sd_state, |
| 1369 | struct v4l2_subdev_format *fmt) |
| 1370 | { |
| 1371 | struct v4l2_mbus_framefmt *framefmt; |
| 1372 | const struct imx708_mode *mode; |
| 1373 | struct imx708 *imx708 = to_imx708(sd); |
| 1374 | |
| 1375 | if (fmt->pad >= NUM_PADS) |
| 1376 | return -EINVAL; |
| 1377 | |
| 1378 | mutex_lock(&imx708->mutex); |
| 1379 | |
| 1380 | if (fmt->pad == IMAGE_PAD) { |
| 1381 | const struct imx708_mode *mode_list; |
| 1382 | unsigned int num_modes; |
| 1383 | |
| 1384 | /* Bayer order varies with flips */ |
| 1385 | fmt->format.code = imx708_get_format_code(imx708); |
| 1386 | |
| 1387 | get_mode_table(fmt->format.code, &mode_list, &num_modes, |
| 1388 | imx708->hdr_mode->val); |
| 1389 | |
| 1390 | mode = v4l2_find_nearest_size(mode_list, |
| 1391 | num_modes, |
| 1392 | width, height, |
| 1393 | fmt->format.width, |
| 1394 | fmt->format.height); |
| 1395 | imx708_update_image_pad_format(imx708, mode, fmt); |
| 1396 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1397 | framefmt = v4l2_subdev_state_get_format(sd_state, |
| 1398 | fmt->pad); |
| 1399 | *framefmt = fmt->format; |
| 1400 | } else { |
| 1401 | imx708->mode = mode; |
| 1402 | imx708_set_framing_limits(imx708); |
| 1403 | } |
| 1404 | } else { |
| 1405 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1406 | framefmt = v4l2_subdev_state_get_format(sd_state, |
| 1407 | fmt->pad); |
| 1408 | *framefmt = fmt->format; |
| 1409 | } else { |
| 1410 | /* Only one embedded data mode is supported */ |
| 1411 | imx708_update_metadata_pad_format(fmt); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | mutex_unlock(&imx708->mutex); |
| 1416 | |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | static const struct v4l2_rect * |
| 1421 | __imx708_get_pad_crop(struct imx708 *imx708, struct v4l2_subdev_state *sd_state, |
| 1422 | unsigned int pad, enum v4l2_subdev_format_whence which) |
| 1423 | { |
| 1424 | switch (which) { |
| 1425 | case V4L2_SUBDEV_FORMAT_TRY: |
| 1426 | return v4l2_subdev_state_get_crop(sd_state, pad); |
| 1427 | case V4L2_SUBDEV_FORMAT_ACTIVE: |
| 1428 | return &imx708->mode->crop; |
| 1429 | } |
| 1430 | |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
| 1434 | static int imx708_get_selection(struct v4l2_subdev *sd, |
| 1435 | struct v4l2_subdev_state *sd_state, |
| 1436 | struct v4l2_subdev_selection *sel) |
| 1437 | { |
| 1438 | switch (sel->target) { |
| 1439 | case V4L2_SEL_TGT_CROP: { |
| 1440 | struct imx708 *imx708 = to_imx708(sd); |
| 1441 | |
| 1442 | mutex_lock(&imx708->mutex); |
| 1443 | sel->r = *__imx708_get_pad_crop(imx708, sd_state, sel->pad, |
| 1444 | sel->which); |
| 1445 | mutex_unlock(&imx708->mutex); |
| 1446 | |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | case V4L2_SEL_TGT_NATIVE_SIZE: |
| 1451 | sel->r.left = 0; |
| 1452 | sel->r.top = 0; |
| 1453 | sel->r.width = IMX708_NATIVE_WIDTH; |
| 1454 | sel->r.height = IMX708_NATIVE_HEIGHT; |
| 1455 | |
| 1456 | return 0; |
| 1457 | |
| 1458 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 1459 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 1460 | sel->r.left = IMX708_PIXEL_ARRAY_LEFT; |
| 1461 | sel->r.top = IMX708_PIXEL_ARRAY_TOP; |
| 1462 | sel->r.width = IMX708_PIXEL_ARRAY_WIDTH; |
| 1463 | sel->r.height = IMX708_PIXEL_ARRAY_HEIGHT; |
| 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | return -EINVAL; |
| 1469 | } |
| 1470 | |
| 1471 | /* Start streaming */ |
| 1472 | static int imx708_start_streaming(struct imx708 *imx708) |
| 1473 | { |
| 1474 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 1475 | const struct imx708_reg_list *reg_list, *freq_regs; |
| 1476 | int i, ret; |
| 1477 | u32 val; |
| 1478 | |
| 1479 | if (!imx708->common_regs_written) { |
| 1480 | ret = imx708_write_regs(imx708, mode_common_regs, |
| 1481 | ARRAY_SIZE(mode_common_regs)); |
| 1482 | if (ret) { |
| 1483 | dev_err(&client->dev, "%s failed to set common settings\n", |
| 1484 | __func__); |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | ret = imx708_read_reg(imx708, IMX708_REG_BASE_SPC_GAINS_L, |
| 1489 | IMX708_REG_VALUE_08BIT, &val); |
| 1490 | if (ret == 0 && val == 0x40) { |
| 1491 | for (i = 0; i < 54 && ret == 0; i++) { |
| 1492 | ret = imx708_write_reg(imx708, |
| 1493 | IMX708_REG_BASE_SPC_GAINS_L + i, |
| 1494 | IMX708_REG_VALUE_08BIT, |
| 1495 | pdaf_gains[0][i % 9]); |
| 1496 | } |
| 1497 | for (i = 0; i < 54 && ret == 0; i++) { |
| 1498 | ret = imx708_write_reg(imx708, |
| 1499 | IMX708_REG_BASE_SPC_GAINS_R + i, |
| 1500 | IMX708_REG_VALUE_08BIT, |
| 1501 | pdaf_gains[1][i % 9]); |
| 1502 | } |
| 1503 | } |
| 1504 | if (ret) { |
| 1505 | dev_err(&client->dev, "%s failed to set PDAF gains\n", |
| 1506 | __func__); |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
| 1510 | imx708->common_regs_written = true; |
| 1511 | } |
| 1512 | |
| 1513 | /* Apply default values of current mode */ |
| 1514 | reg_list = &imx708->mode->reg_list; |
| 1515 | ret = imx708_write_regs(imx708, reg_list->regs, reg_list->num_of_regs); |
| 1516 | if (ret) { |
| 1517 | dev_err(&client->dev, "%s failed to set mode\n", __func__); |
| 1518 | return ret; |
| 1519 | } |
| 1520 | |
| 1521 | /* Update the link frequency registers */ |
| 1522 | freq_regs = &link_freq_regs[imx708->link_freq_idx]; |
| 1523 | ret = imx708_write_regs(imx708, freq_regs->regs, |
| 1524 | freq_regs->num_of_regs); |
| 1525 | if (ret) { |
| 1526 | dev_err(&client->dev, "%s failed to set link frequency registers\n", |
| 1527 | __func__); |
| 1528 | return ret; |
| 1529 | } |
| 1530 | |
| 1531 | /* Quad Bayer re-mosaic adjustments (for full-resolution mode only) */ |
| 1532 | if (imx708->mode->remosaic && qbc_adjust > 0) { |
| 1533 | imx708_write_reg(imx708, IMX708_LPF_INTENSITY, |
| 1534 | IMX708_REG_VALUE_08BIT, qbc_adjust); |
| 1535 | imx708_write_reg(imx708, |
| 1536 | IMX708_LPF_INTENSITY_EN, |
| 1537 | IMX708_REG_VALUE_08BIT, |
| 1538 | IMX708_LPF_INTENSITY_ENABLED); |
| 1539 | } else { |
| 1540 | imx708_write_reg(imx708, |
| 1541 | IMX708_LPF_INTENSITY_EN, |
| 1542 | IMX708_REG_VALUE_08BIT, |
| 1543 | IMX708_LPF_INTENSITY_DISABLED); |
| 1544 | } |
| 1545 | |
| 1546 | /* Apply customized values from user */ |
| 1547 | ret = __v4l2_ctrl_handler_setup(imx708->sd.ctrl_handler); |
| 1548 | if (ret) |
| 1549 | return ret; |
| 1550 | |
| 1551 | /* set stream on register */ |
| 1552 | return imx708_write_reg(imx708, IMX708_REG_MODE_SELECT, |
| 1553 | IMX708_REG_VALUE_08BIT, IMX708_MODE_STREAMING); |
| 1554 | } |
| 1555 | |
| 1556 | /* Stop streaming */ |
| 1557 | static void imx708_stop_streaming(struct imx708 *imx708) |
| 1558 | { |
| 1559 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 1560 | int ret; |
| 1561 | |
| 1562 | /* set stream off register */ |
| 1563 | ret = imx708_write_reg(imx708, IMX708_REG_MODE_SELECT, |
| 1564 | IMX708_REG_VALUE_08BIT, IMX708_MODE_STANDBY); |
| 1565 | if (ret) |
| 1566 | dev_err(&client->dev, "%s failed to set stream\n", __func__); |
| 1567 | } |
| 1568 | |
| 1569 | static int imx708_set_stream(struct v4l2_subdev *sd, int enable) |
| 1570 | { |
| 1571 | struct imx708 *imx708 = to_imx708(sd); |
| 1572 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
| 1573 | int ret = 0; |
| 1574 | |
| 1575 | mutex_lock(&imx708->mutex); |
| 1576 | if (imx708->streaming == enable) { |
| 1577 | mutex_unlock(&imx708->mutex); |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | if (enable) { |
| 1582 | ret = pm_runtime_get_sync(&client->dev); |
| 1583 | if (ret < 0) { |
| 1584 | pm_runtime_put_noidle(&client->dev); |
| 1585 | goto err_unlock; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * Apply default & customized values |
| 1590 | * and then start streaming. |
| 1591 | */ |
| 1592 | ret = imx708_start_streaming(imx708); |
| 1593 | if (ret) |
| 1594 | goto err_rpm_put; |
| 1595 | } else { |
| 1596 | imx708_stop_streaming(imx708); |
| 1597 | pm_runtime_put(&client->dev); |
| 1598 | } |
| 1599 | |
| 1600 | imx708->streaming = enable; |
| 1601 | |
| 1602 | /* vflip/hflip and hdr mode cannot change during streaming */ |
| 1603 | __v4l2_ctrl_grab(imx708->vflip, enable); |
| 1604 | __v4l2_ctrl_grab(imx708->hflip, enable); |
| 1605 | __v4l2_ctrl_grab(imx708->hdr_mode, enable); |
| 1606 | |
| 1607 | mutex_unlock(&imx708->mutex); |
| 1608 | |
| 1609 | return ret; |
| 1610 | |
| 1611 | err_rpm_put: |
| 1612 | pm_runtime_put(&client->dev); |
| 1613 | err_unlock: |
| 1614 | mutex_unlock(&imx708->mutex); |
| 1615 | |
| 1616 | return ret; |
| 1617 | } |
| 1618 | |
| 1619 | /* Power/clock management functions */ |
| 1620 | static int imx708_power_on(struct device *dev) |
| 1621 | { |
| 1622 | struct i2c_client *client = to_i2c_client(dev); |
| 1623 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1624 | struct imx708 *imx708 = to_imx708(sd); |
| 1625 | int ret; |
| 1626 | |
| 1627 | ret = regulator_bulk_enable(ARRAY_SIZE(imx708_supply_name), |
| 1628 | imx708->supplies); |
| 1629 | if (ret) { |
| 1630 | dev_err(&client->dev, "%s: failed to enable regulators\n", |
| 1631 | __func__); |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
| 1635 | ret = clk_prepare_enable(imx708->inclk); |
| 1636 | if (ret) { |
| 1637 | dev_err(&client->dev, "%s: failed to enable clock\n", |
| 1638 | __func__); |
| 1639 | goto reg_off; |
| 1640 | } |
| 1641 | |
| 1642 | gpiod_set_value_cansleep(imx708->reset_gpio, 1); |
| 1643 | usleep_range(IMX708_XCLR_MIN_DELAY_US, |
| 1644 | IMX708_XCLR_MIN_DELAY_US + IMX708_XCLR_DELAY_RANGE_US); |
| 1645 | |
| 1646 | return 0; |
| 1647 | |
| 1648 | reg_off: |
| 1649 | regulator_bulk_disable(ARRAY_SIZE(imx708_supply_name), |
| 1650 | imx708->supplies); |
| 1651 | return ret; |
| 1652 | } |
| 1653 | |
| 1654 | static int imx708_power_off(struct device *dev) |
| 1655 | { |
| 1656 | struct i2c_client *client = to_i2c_client(dev); |
| 1657 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1658 | struct imx708 *imx708 = to_imx708(sd); |
| 1659 | |
| 1660 | gpiod_set_value_cansleep(imx708->reset_gpio, 0); |
| 1661 | regulator_bulk_disable(ARRAY_SIZE(imx708_supply_name), |
| 1662 | imx708->supplies); |
| 1663 | clk_disable_unprepare(imx708->inclk); |
| 1664 | |
| 1665 | /* Force reprogramming of the common registers when powered up again. */ |
| 1666 | imx708->common_regs_written = false; |
| 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | static int __maybe_unused imx708_suspend(struct device *dev) |
| 1672 | { |
| 1673 | struct i2c_client *client = to_i2c_client(dev); |
| 1674 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1675 | struct imx708 *imx708 = to_imx708(sd); |
| 1676 | |
| 1677 | if (imx708->streaming) |
| 1678 | imx708_stop_streaming(imx708); |
| 1679 | |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | static int __maybe_unused imx708_resume(struct device *dev) |
| 1684 | { |
| 1685 | struct i2c_client *client = to_i2c_client(dev); |
| 1686 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1687 | struct imx708 *imx708 = to_imx708(sd); |
| 1688 | int ret; |
| 1689 | |
| 1690 | if (imx708->streaming) { |
| 1691 | ret = imx708_start_streaming(imx708); |
| 1692 | if (ret) |
| 1693 | goto error; |
| 1694 | } |
| 1695 | |
| 1696 | return 0; |
| 1697 | |
| 1698 | error: |
| 1699 | imx708_stop_streaming(imx708); |
| 1700 | imx708->streaming = 0; |
| 1701 | return ret; |
| 1702 | } |
| 1703 | |
| 1704 | static int imx708_get_regulators(struct imx708 *imx708) |
| 1705 | { |
| 1706 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 1707 | unsigned int i; |
| 1708 | |
| 1709 | for (i = 0; i < ARRAY_SIZE(imx708_supply_name); i++) |
| 1710 | imx708->supplies[i].supply = imx708_supply_name[i]; |
| 1711 | |
| 1712 | return devm_regulator_bulk_get(&client->dev, |
| 1713 | ARRAY_SIZE(imx708_supply_name), |
| 1714 | imx708->supplies); |
| 1715 | } |
| 1716 | |
| 1717 | /* Verify chip ID */ |
| 1718 | static int imx708_identify_module(struct imx708 *imx708) |
| 1719 | { |
| 1720 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 1721 | int ret; |
| 1722 | u32 val; |
| 1723 | |
| 1724 | ret = imx708_read_reg(imx708, IMX708_REG_CHIP_ID, |
| 1725 | IMX708_REG_VALUE_16BIT, &val); |
| 1726 | if (ret) { |
| 1727 | dev_err(&client->dev, "failed to read chip id %x, with error %d\n", |
| 1728 | IMX708_CHIP_ID, ret); |
| 1729 | return ret; |
| 1730 | } |
| 1731 | |
| 1732 | if (val != IMX708_CHIP_ID) { |
| 1733 | dev_err(&client->dev, "chip id mismatch: %x!=%x\n", |
| 1734 | IMX708_CHIP_ID, val); |
| 1735 | return -EIO; |
| 1736 | } |
| 1737 | |
| 1738 | ret = imx708_read_reg(imx708, 0x0000, IMX708_REG_VALUE_16BIT, &val); |
| 1739 | if (!ret) { |
| 1740 | dev_info(&client->dev, "camera module ID 0x%04x\n", val); |
| 1741 | snprintf(imx708->sd.name, sizeof(imx708->sd.name), "imx708%s%s", |
| 1742 | val & 0x02 ? "_wide" : "", |
| 1743 | val & 0x80 ? "_noir" : ""); |
| 1744 | } |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | static const struct v4l2_subdev_core_ops imx708_core_ops = { |
| 1750 | .subscribe_event = v4l2_ctrl_subdev_subscribe_event, |
| 1751 | .unsubscribe_event = v4l2_event_subdev_unsubscribe, |
| 1752 | }; |
| 1753 | |
| 1754 | static const struct v4l2_subdev_video_ops imx708_video_ops = { |
| 1755 | .s_stream = imx708_set_stream, |
| 1756 | }; |
| 1757 | |
| 1758 | static const struct v4l2_subdev_pad_ops imx708_pad_ops = { |
| 1759 | .enum_mbus_code = imx708_enum_mbus_code, |
| 1760 | .get_fmt = imx708_get_pad_format, |
| 1761 | .set_fmt = imx708_set_pad_format, |
| 1762 | .get_selection = imx708_get_selection, |
| 1763 | .enum_frame_size = imx708_enum_frame_size, |
| 1764 | }; |
| 1765 | |
| 1766 | static const struct v4l2_subdev_ops imx708_subdev_ops = { |
| 1767 | .core = &imx708_core_ops, |
| 1768 | .video = &imx708_video_ops, |
| 1769 | .pad = &imx708_pad_ops, |
| 1770 | }; |
| 1771 | |
| 1772 | static const struct v4l2_subdev_internal_ops imx708_internal_ops = { |
| 1773 | .open = imx708_open, |
| 1774 | }; |
| 1775 | |
| 1776 | static const struct v4l2_ctrl_config imx708_notify_gains_ctrl = { |
| 1777 | .ops = &imx708_ctrl_ops, |
| 1778 | .id = V4L2_CID_NOTIFY_GAINS, |
| 1779 | .type = V4L2_CTRL_TYPE_U32, |
| 1780 | .min = IMX708_COLOUR_BALANCE_MIN, |
| 1781 | .max = IMX708_COLOUR_BALANCE_MAX, |
| 1782 | .step = IMX708_COLOUR_BALANCE_STEP, |
| 1783 | .def = IMX708_COLOUR_BALANCE_DEFAULT, |
| 1784 | .dims = { 4 }, |
| 1785 | .elem_size = sizeof(u32), |
| 1786 | }; |
| 1787 | |
| 1788 | /* Initialize control handlers */ |
| 1789 | static int imx708_init_controls(struct imx708 *imx708) |
| 1790 | { |
| 1791 | struct v4l2_ctrl_handler *ctrl_hdlr; |
| 1792 | struct i2c_client *client = v4l2_get_subdevdata(&imx708->sd); |
| 1793 | struct v4l2_fwnode_device_properties props; |
| 1794 | struct v4l2_ctrl *ctrl; |
| 1795 | unsigned int i; |
| 1796 | int ret; |
| 1797 | |
| 1798 | ctrl_hdlr = &imx708->ctrl_handler; |
| 1799 | ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16); |
| 1800 | if (ret) |
| 1801 | return ret; |
| 1802 | |
| 1803 | mutex_init(&imx708->mutex); |
| 1804 | ctrl_hdlr->lock = &imx708->mutex; |
| 1805 | |
| 1806 | /* By default, PIXEL_RATE is read only */ |
| 1807 | imx708->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1808 | V4L2_CID_PIXEL_RATE, |
| 1809 | IMX708_INITIAL_PIXEL_RATE, |
| 1810 | IMX708_INITIAL_PIXEL_RATE, 1, |
| 1811 | IMX708_INITIAL_PIXEL_RATE); |
| 1812 | |
| 1813 | ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx708_ctrl_ops, |
| 1814 | V4L2_CID_LINK_FREQ, 0, 0, |
| 1815 | &link_freqs[imx708->link_freq_idx]); |
| 1816 | if (ctrl) |
| 1817 | ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; |
| 1818 | |
| 1819 | /* |
| 1820 | * Create the controls here, but mode specific limits are setup |
| 1821 | * in the imx708_set_framing_limits() call below. |
| 1822 | */ |
| 1823 | imx708->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1824 | V4L2_CID_VBLANK, 0, 0xffff, 1, 0); |
| 1825 | imx708->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1826 | V4L2_CID_HBLANK, 0, 0xffff, 1, 0); |
| 1827 | |
| 1828 | imx708->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1829 | V4L2_CID_EXPOSURE, |
| 1830 | IMX708_EXPOSURE_MIN, |
| 1831 | IMX708_EXPOSURE_MAX, |
| 1832 | IMX708_EXPOSURE_STEP, |
| 1833 | IMX708_EXPOSURE_DEFAULT); |
| 1834 | |
| 1835 | v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, |
| 1836 | IMX708_ANA_GAIN_MIN, IMX708_ANA_GAIN_MAX, |
| 1837 | IMX708_ANA_GAIN_STEP, IMX708_ANA_GAIN_DEFAULT); |
| 1838 | |
| 1839 | v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, V4L2_CID_DIGITAL_GAIN, |
| 1840 | IMX708_DGTL_GAIN_MIN, IMX708_DGTL_GAIN_MAX, |
| 1841 | IMX708_DGTL_GAIN_STEP, IMX708_DGTL_GAIN_DEFAULT); |
| 1842 | |
| 1843 | imx708->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1844 | V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 1845 | |
| 1846 | imx708->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1847 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 1848 | v4l2_ctrl_cluster(2, &imx708->hflip); |
| 1849 | |
| 1850 | v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx708_ctrl_ops, |
| 1851 | V4L2_CID_TEST_PATTERN, |
| 1852 | ARRAY_SIZE(imx708_test_pattern_menu) - 1, |
| 1853 | 0, 0, imx708_test_pattern_menu); |
| 1854 | for (i = 0; i < 4; i++) { |
| 1855 | /* |
| 1856 | * The assumption is that |
| 1857 | * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1 |
| 1858 | * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2 |
| 1859 | * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3 |
| 1860 | */ |
| 1861 | v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1862 | V4L2_CID_TEST_PATTERN_RED + i, |
| 1863 | IMX708_TEST_PATTERN_COLOUR_MIN, |
| 1864 | IMX708_TEST_PATTERN_COLOUR_MAX, |
| 1865 | IMX708_TEST_PATTERN_COLOUR_STEP, |
| 1866 | IMX708_TEST_PATTERN_COLOUR_MAX); |
| 1867 | /* The "Solid color" pattern is white by default */ |
| 1868 | } |
| 1869 | |
| 1870 | v4l2_ctrl_new_custom(ctrl_hdlr, &imx708_notify_gains_ctrl, NULL); |
| 1871 | |
| 1872 | imx708->hdr_mode = v4l2_ctrl_new_std(ctrl_hdlr, &imx708_ctrl_ops, |
| 1873 | V4L2_CID_WIDE_DYNAMIC_RANGE, |
| 1874 | 0, 1, 1, 0); |
| 1875 | |
| 1876 | ret = v4l2_fwnode_device_parse(&client->dev, &props); |
| 1877 | if (ret) |
| 1878 | goto error; |
| 1879 | |
| 1880 | v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx708_ctrl_ops, &props); |
| 1881 | |
| 1882 | if (ctrl_hdlr->error) { |
| 1883 | ret = ctrl_hdlr->error; |
| 1884 | dev_err(&client->dev, "%s control init failed (%d)\n", |
| 1885 | __func__, ret); |
| 1886 | goto error; |
| 1887 | } |
| 1888 | |
| 1889 | imx708->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; |
| 1890 | imx708->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; |
| 1891 | imx708->hdr_mode->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; |
| 1892 | |
| 1893 | imx708->sd.ctrl_handler = ctrl_hdlr; |
| 1894 | |
| 1895 | /* Setup exposure and frame/line length limits. */ |
| 1896 | imx708_set_framing_limits(imx708); |
| 1897 | |
| 1898 | return 0; |
| 1899 | |
| 1900 | error: |
| 1901 | v4l2_ctrl_handler_free(ctrl_hdlr); |
| 1902 | mutex_destroy(&imx708->mutex); |
| 1903 | |
| 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | static void imx708_free_controls(struct imx708 *imx708) |
| 1908 | { |
| 1909 | v4l2_ctrl_handler_free(imx708->sd.ctrl_handler); |
| 1910 | mutex_destroy(&imx708->mutex); |
| 1911 | } |
| 1912 | |
| 1913 | static int imx708_check_hwcfg(struct device *dev, struct imx708 *imx708) |
| 1914 | { |
| 1915 | struct fwnode_handle *endpoint; |
| 1916 | struct v4l2_fwnode_endpoint ep_cfg = { |
| 1917 | .bus_type = V4L2_MBUS_CSI2_DPHY |
| 1918 | }; |
| 1919 | int ret = -EINVAL; |
| 1920 | int i; |
| 1921 | |
| 1922 | endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); |
| 1923 | if (!endpoint) { |
| 1924 | dev_err(dev, "endpoint node not found\n"); |
| 1925 | return -EINVAL; |
| 1926 | } |
| 1927 | |
| 1928 | if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) { |
| 1929 | dev_err(dev, "could not parse endpoint\n"); |
| 1930 | goto error_out; |
| 1931 | } |
| 1932 | |
| 1933 | /* Check the number of MIPI CSI2 data lanes */ |
| 1934 | if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2 && |
| 1935 | ep_cfg.bus.mipi_csi2.num_data_lanes != 4) { |
| 1936 | dev_err(dev, "only 2 or 4 data lanes are supported\n"); |
| 1937 | goto error_out; |
| 1938 | } |
| 1939 | |
| 1940 | /* Check the link frequency set in device tree */ |
| 1941 | if (!ep_cfg.nr_of_link_frequencies) { |
| 1942 | dev_err(dev, "link-frequency property not found in DT\n"); |
| 1943 | goto error_out; |
| 1944 | } |
| 1945 | |
| 1946 | for (i = 0; i < ARRAY_SIZE(link_freqs); i++) { |
| 1947 | if (link_freqs[i] == ep_cfg.link_frequencies[0]) { |
| 1948 | imx708->link_freq_idx = i; |
| 1949 | break; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | if (i == ARRAY_SIZE(link_freqs)) { |
| 1954 | dev_err(dev, "Link frequency not supported: %lld\n", |
| 1955 | ep_cfg.link_frequencies[0]); |
| 1956 | ret = -EINVAL; |
| 1957 | goto error_out; |
| 1958 | } |
| 1959 | |
| 1960 | ret = 0; |
| 1961 | |
| 1962 | error_out: |
| 1963 | v4l2_fwnode_endpoint_free(&ep_cfg); |
| 1964 | fwnode_handle_put(endpoint); |
| 1965 | |
| 1966 | return ret; |
| 1967 | } |
| 1968 | |
| 1969 | static int imx708_probe(struct i2c_client *client) |
| 1970 | { |
| 1971 | struct device *dev = &client->dev; |
| 1972 | struct imx708 *imx708; |
| 1973 | int ret; |
| 1974 | |
| 1975 | imx708 = devm_kzalloc(&client->dev, sizeof(*imx708), GFP_KERNEL); |
| 1976 | if (!imx708) |
| 1977 | return -ENOMEM; |
| 1978 | |
| 1979 | v4l2_i2c_subdev_init(&imx708->sd, client, &imx708_subdev_ops); |
| 1980 | |
| 1981 | /* Check the hardware configuration in device tree */ |
| 1982 | if (imx708_check_hwcfg(dev, imx708)) |
| 1983 | return -EINVAL; |
| 1984 | |
| 1985 | /* Get system clock (inclk) */ |
| 1986 | imx708->inclk = devm_clk_get(dev, "inclk"); |
| 1987 | if (IS_ERR(imx708->inclk)) |
| 1988 | return dev_err_probe(dev, PTR_ERR(imx708->inclk), |
| 1989 | "failed to get inclk\n"); |
| 1990 | |
| 1991 | imx708->inclk_freq = clk_get_rate(imx708->inclk); |
| 1992 | if (imx708->inclk_freq != IMX708_INCLK_FREQ) |
| 1993 | return dev_err_probe(dev, -EINVAL, |
| 1994 | "inclk frequency not supported: %d Hz\n", |
| 1995 | imx708->inclk_freq); |
| 1996 | |
| 1997 | ret = imx708_get_regulators(imx708); |
| 1998 | if (ret) |
| 1999 | return dev_err_probe(dev, ret, "failed to get regulators\n"); |
| 2000 | |
| 2001 | /* Request optional enable pin */ |
| 2002 | imx708->reset_gpio = devm_gpiod_get_optional(dev, "reset", |
| 2003 | GPIOD_OUT_HIGH); |
| 2004 | |
| 2005 | /* |
| 2006 | * The sensor must be powered for imx708_identify_module() |
| 2007 | * to be able to read the CHIP_ID register |
| 2008 | */ |
| 2009 | ret = imx708_power_on(dev); |
| 2010 | if (ret) |
| 2011 | return ret; |
| 2012 | |
| 2013 | ret = imx708_identify_module(imx708); |
| 2014 | if (ret) |
| 2015 | goto error_power_off; |
| 2016 | |
| 2017 | /* Initialize default format */ |
| 2018 | imx708_set_default_format(imx708); |
| 2019 | |
| 2020 | /* Enable runtime PM and turn off the device */ |
| 2021 | pm_runtime_set_active(dev); |
| 2022 | pm_runtime_enable(dev); |
| 2023 | pm_runtime_idle(dev); |
| 2024 | |
| 2025 | /* This needs the pm runtime to be registered. */ |
| 2026 | ret = imx708_init_controls(imx708); |
| 2027 | if (ret) |
| 2028 | goto error_pm_runtime; |
| 2029 | |
| 2030 | /* Initialize subdev */ |
| 2031 | imx708->sd.internal_ops = &imx708_internal_ops; |
| 2032 | imx708->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | |
| 2033 | V4L2_SUBDEV_FL_HAS_EVENTS; |
| 2034 | imx708->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; |
| 2035 | |
| 2036 | /* Initialize source pads */ |
| 2037 | imx708->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE; |
| 2038 | imx708->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE; |
| 2039 | |
| 2040 | ret = media_entity_pads_init(&imx708->sd.entity, NUM_PADS, imx708->pad); |
| 2041 | if (ret) { |
| 2042 | dev_err(dev, "failed to init entity pads: %d\n", ret); |
| 2043 | goto error_handler_free; |
| 2044 | } |
| 2045 | |
| 2046 | ret = v4l2_async_register_subdev_sensor(&imx708->sd); |
| 2047 | if (ret < 0) { |
| 2048 | dev_err(dev, "failed to register sensor sub-device: %d\n", ret); |
| 2049 | goto error_media_entity; |
| 2050 | } |
| 2051 | |
| 2052 | return 0; |
| 2053 | |
| 2054 | error_media_entity: |
| 2055 | media_entity_cleanup(&imx708->sd.entity); |
| 2056 | |
| 2057 | error_handler_free: |
| 2058 | imx708_free_controls(imx708); |
| 2059 | |
| 2060 | error_pm_runtime: |
| 2061 | pm_runtime_disable(&client->dev); |
| 2062 | pm_runtime_set_suspended(&client->dev); |
| 2063 | |
| 2064 | error_power_off: |
| 2065 | imx708_power_off(&client->dev); |
| 2066 | |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
| 2070 | static void imx708_remove(struct i2c_client *client) |
| 2071 | { |
| 2072 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 2073 | struct imx708 *imx708 = to_imx708(sd); |
| 2074 | |
| 2075 | v4l2_async_unregister_subdev(sd); |
| 2076 | media_entity_cleanup(&sd->entity); |
| 2077 | imx708_free_controls(imx708); |
| 2078 | |
| 2079 | pm_runtime_disable(&client->dev); |
| 2080 | if (!pm_runtime_status_suspended(&client->dev)) |
| 2081 | imx708_power_off(&client->dev); |
| 2082 | pm_runtime_set_suspended(&client->dev); |
| 2083 | } |
| 2084 | |
| 2085 | static const struct of_device_id imx708_dt_ids[] = { |
| 2086 | { .compatible = "sony,imx708" }, |
| 2087 | { /* sentinel */ } |
| 2088 | }; |
| 2089 | MODULE_DEVICE_TABLE(of, imx708_dt_ids); |
| 2090 | |
| 2091 | static const struct dev_pm_ops imx708_pm_ops = { |
| 2092 | SET_SYSTEM_SLEEP_PM_OPS(imx708_suspend, imx708_resume) |
| 2093 | SET_RUNTIME_PM_OPS(imx708_power_off, imx708_power_on, NULL) |
| 2094 | }; |
| 2095 | |
| 2096 | static struct i2c_driver imx708_i2c_driver = { |
| 2097 | .driver = { |
| 2098 | .name = "imx708", |
| 2099 | .of_match_table = imx708_dt_ids, |
| 2100 | .pm = &imx708_pm_ops, |
| 2101 | }, |
| 2102 | .probe = imx708_probe, |
| 2103 | .remove = imx708_remove, |
| 2104 | }; |
| 2105 | |
| 2106 | module_i2c_driver(imx708_i2c_driver); |
| 2107 | |
| 2108 | MODULE_AUTHOR("David Plowman <david.plowman@raspberrypi.com>"); |
| 2109 | MODULE_DESCRIPTION("Sony IMX708 sensor driver"); |
| 2110 | MODULE_LICENSE("GPL v2"); |