1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * A V4L2 driver for OmniVision OV5647 cameras. 4 * 5 * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver 6 * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com> 7 * 8 * Based on Omnivision OV7670 Camera Driver 9 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net> 10 * 11 * Copyright (C) 2016, Synopsys, Inc. 12 */ 13 14 #include <linux/clk.h> 15 #include <linux/delay.h> 16 #include <linux/gpio/consumer.h> 17 #include <linux/i2c.h> 18 #include <linux/init.h> 19 #include <linux/io.h> 20 #include <linux/module.h> 21 #include <linux/of_graph.h> 22 #include <linux/pm_runtime.h> 23 #include <linux/regmap.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/slab.h> 26 #include <linux/videodev2.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-image-sizes.h> 33 #include <media/v4l2-mediabus.h> 34 35 /* 36 * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes 37 * high if reset is inserted after PWDN goes high, host can access sensor's 38 * SCCB to initialize sensor." 39 */ 40 #define PWDN_ACTIVE_DELAY_MS 20 41 42 #define MIPI_CTRL00_CLOCK_LANE_GATE BIT(5) 43 #define MIPI_CTRL00_LINE_SYNC_ENABLE BIT(4) 44 #define MIPI_CTRL00_BUS_IDLE BIT(2) 45 #define MIPI_CTRL00_CLOCK_LANE_DISABLE BIT(0) 46 47 #define OV5647_SW_STANDBY CCI_REG8(0x0100) 48 #define OV5647_SW_RESET CCI_REG8(0x0103) 49 #define OV5647_REG_CHIPID CCI_REG16(0x300a) 50 #define OV5640_REG_PAD_OUT CCI_REG8(0x300d) 51 #define OV5647_REG_EXPOSURE CCI_REG24(0x3500) 52 #define OV5647_REG_AEC_AGC CCI_REG8(0x3503) 53 #define OV5647_REG_GAIN CCI_REG16(0x350a) 54 #define OV5647_REG_HTS CCI_REG16(0x380c) 55 #define OV5647_REG_VTS CCI_REG16(0x380e) 56 #define OV5647_REG_TIMING_TC_V CCI_REG8(0x3820) 57 #define OV5647_REG_TIMING_TC_H CCI_REG8(0x3821) 58 #define OV5647_REG_FRAME_OFF_NUMBER CCI_REG8(0x4202) 59 #define OV5647_REG_MIPI_CTRL00 CCI_REG8(0x4800) 60 #define OV5647_REG_MIPI_CTRL14 CCI_REG8(0x4814) 61 #define OV5647_REG_MIPI_CTRL14_CHANNEL_MASK GENMASK(7, 6) 62 #define OV5647_REG_MIPI_CTRL14_CHANNEL_SHIFT 6 63 #define OV5647_REG_AWB CCI_REG8(0x5001) 64 #define OV5647_REG_ISPCTRL3D CCI_REG8(0x503d) 65 66 #define OV5647_CHIP_ID 0x5647 67 #define REG_TERM 0xfffe 68 #define VAL_TERM 0xfe 69 #define REG_DLY 0xffff 70 71 /* OV5647 native and active pixel array size */ 72 #define OV5647_NATIVE_WIDTH 2624U 73 #define OV5647_NATIVE_HEIGHT 1956U 74 75 #define OV5647_PIXEL_ARRAY_LEFT 16U 76 #define OV5647_PIXEL_ARRAY_TOP 6U 77 #define OV5647_PIXEL_ARRAY_WIDTH 2592U 78 #define OV5647_PIXEL_ARRAY_HEIGHT 1944U 79 80 #define OV5647_VBLANK_MIN 24 81 #define OV5647_VTS_MAX 32767 82 83 #define OV5647_HTS_MAX 0x1fff 84 85 #define OV5647_EXPOSURE_MIN 4 86 #define OV5647_EXPOSURE_STEP 1 87 #define OV5647_EXPOSURE_DEFAULT 1000 88 #define OV5647_EXPOSURE_MAX 65535 89 90 /* regulator supplies */ 91 static const char * const ov5647_supply_names[] = { 92 "avdd", /* Analog power */ 93 "dovdd", /* Digital I/O power */ 94 "dvdd", /* Digital core power */ 95 }; 96 97 #define OV5647_NUM_SUPPLIES ARRAY_SIZE(ov5647_supply_names) 98 99 #define FREQ_INDEX_FULL 0 100 #define FREQ_INDEX_VGA 1 101 static const s64 ov5647_link_freqs[] = { 102 [FREQ_INDEX_FULL] = 218750000, 103 [FREQ_INDEX_VGA] = 145833300, 104 }; 105 106 struct ov5647_mode { 107 struct v4l2_mbus_framefmt format; 108 struct v4l2_rect crop; 109 u64 pixel_rate; 110 unsigned int link_freq_index; 111 int hts; 112 int vts; 113 const struct reg_sequence *reg_list; 114 unsigned int num_regs; 115 }; 116 117 struct ov5647 { 118 struct v4l2_subdev sd; 119 struct regmap *regmap; 120 struct media_pad pad; 121 struct clk *xclk; 122 struct gpio_desc *pwdn; 123 struct regulator_bulk_data supplies[OV5647_NUM_SUPPLIES]; 124 bool clock_ncont; 125 struct v4l2_ctrl_handler ctrls; 126 const struct ov5647_mode *mode; 127 struct v4l2_ctrl *pixel_rate; 128 struct v4l2_ctrl *hblank; 129 struct v4l2_ctrl *vblank; 130 struct v4l2_ctrl *exposure; 131 struct v4l2_ctrl *hflip; 132 struct v4l2_ctrl *vflip; 133 struct v4l2_ctrl *link_freq; 134 }; 135 136 static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd) 137 { 138 return container_of(sd, struct ov5647, sd); 139 } 140 141 static const char * const ov5647_test_pattern_menu[] = { 142 "Disabled", 143 "Color Bars", 144 "Color Squares", 145 "Random Data", 146 }; 147 148 static const u8 ov5647_test_pattern_val[] = { 149 0x00, /* Disabled */ 150 0x80, /* Color Bars */ 151 0x82, /* Color Squares */ 152 0x81, /* Random Data */ 153 }; 154 155 static const struct reg_sequence sensor_oe_disable_regs[] = { 156 {0x3000, 0x00}, 157 {0x3001, 0x00}, 158 {0x3002, 0x00}, 159 }; 160 161 static const struct reg_sequence sensor_oe_enable_regs[] = { 162 {0x3000, 0x0f}, 163 {0x3001, 0xff}, 164 {0x3002, 0xe4}, 165 }; 166 167 static const struct reg_sequence ov5647_common_regs[] = { 168 {0x0100, 0x00}, 169 {0x0103, 0x01}, 170 {0x3034, 0x1a}, 171 {0x3035, 0x21}, 172 {0x303c, 0x11}, 173 {0x3106, 0xf5}, 174 {0x3827, 0xec}, 175 {0x370c, 0x03}, 176 {0x5000, 0x06}, 177 {0x5003, 0x08}, 178 {0x5a00, 0x08}, 179 {0x3000, 0x00}, 180 {0x3001, 0x00}, 181 {0x3002, 0x00}, 182 {0x3016, 0x08}, 183 {0x3017, 0xe0}, 184 {0x3018, 0x44}, 185 {0x301c, 0xf8}, 186 {0x301d, 0xf0}, 187 {0x3a18, 0x00}, 188 {0x3a19, 0xf8}, 189 {0x3c01, 0x80}, 190 {0x3b07, 0x0c}, 191 {0x3630, 0x2e}, 192 {0x3632, 0xe2}, 193 {0x3633, 0x23}, 194 {0x3634, 0x44}, 195 {0x3636, 0x06}, 196 {0x3620, 0x64}, 197 {0x3621, 0xe0}, 198 {0x3600, 0x37}, 199 {0x3704, 0xa0}, 200 {0x3703, 0x5a}, 201 {0x3715, 0x78}, 202 {0x3717, 0x01}, 203 {0x3731, 0x02}, 204 {0x370b, 0x60}, 205 {0x3705, 0x1a}, 206 {0x3f05, 0x02}, 207 {0x3f06, 0x10}, 208 {0x3f01, 0x0a}, 209 {0x3a08, 0x01}, 210 {0x3a0f, 0x58}, 211 {0x3a10, 0x50}, 212 {0x3a1b, 0x58}, 213 {0x3a1e, 0x50}, 214 {0x3a11, 0x60}, 215 {0x3a1f, 0x28}, 216 {0x4001, 0x02}, 217 {0x4000, 0x09}, 218 {0x3503, 0x03}, 219 }; 220 221 static const struct reg_sequence ov5647_2592x1944_10bpp[] = { 222 {0x3036, 0x69}, 223 {0x3821, 0x02}, 224 {0x3820, 0x00}, 225 {0x3612, 0x5b}, 226 {0x3618, 0x04}, 227 {0x5002, 0x41}, 228 {0x3814, 0x11}, 229 {0x3815, 0x11}, 230 {0x3708, 0x64}, 231 {0x3709, 0x12}, 232 {0x3800, 0x00}, 233 {0x3801, 0x00}, 234 {0x3802, 0x00}, 235 {0x3803, 0x00}, 236 {0x3804, 0x0a}, 237 {0x3805, 0x3f}, 238 {0x3806, 0x07}, 239 {0x3807, 0xa3}, 240 {0x3808, 0x0a}, 241 {0x3809, 0x20}, 242 {0x380a, 0x07}, 243 {0x380b, 0x98}, 244 {0x3811, 0x10}, 245 {0x3813, 0x06}, 246 {0x3a09, 0x28}, 247 {0x3a0a, 0x00}, 248 {0x3a0b, 0xf6}, 249 {0x3a0d, 0x08}, 250 {0x3a0e, 0x06}, 251 {0x4004, 0x04}, 252 {0x4837, 0x19}, 253 {0x4800, 0x24}, 254 {0x0100, 0x01}, 255 }; 256 257 static const struct reg_sequence ov5647_1080p30_10bpp[] = { 258 {0x3036, 0x69}, 259 {0x3821, 0x02}, 260 {0x3820, 0x00}, 261 {0x3612, 0x5b}, 262 {0x3618, 0x04}, 263 {0x5002, 0x41}, 264 {0x3814, 0x11}, 265 {0x3815, 0x11}, 266 {0x3708, 0x64}, 267 {0x3709, 0x12}, 268 {0x3800, 0x01}, 269 {0x3801, 0x5c}, 270 {0x3802, 0x01}, 271 {0x3803, 0xb2}, 272 {0x3804, 0x08}, 273 {0x3805, 0xe3}, 274 {0x3806, 0x05}, 275 {0x3807, 0xf1}, 276 {0x3808, 0x07}, 277 {0x3809, 0x80}, 278 {0x380a, 0x04}, 279 {0x380b, 0x38}, 280 {0x3811, 0x04}, 281 {0x3813, 0x02}, 282 {0x3a09, 0x4b}, 283 {0x3a0a, 0x01}, 284 {0x3a0b, 0x13}, 285 {0x3a0d, 0x04}, 286 {0x3a0e, 0x03}, 287 {0x4004, 0x04}, 288 {0x4837, 0x19}, 289 {0x4800, 0x34}, 290 {0x0100, 0x01}, 291 }; 292 293 static const struct reg_sequence ov5647_2x2binned_10bpp[] = { 294 {0x3036, 0x69}, 295 {0x3821, 0x03}, 296 {0x3820, 0x41}, 297 {0x3612, 0x59}, 298 {0x3618, 0x00}, 299 {0x5002, 0x41}, 300 {0x3800, 0x00}, 301 {0x3801, 0x00}, 302 {0x3802, 0x00}, 303 {0x3803, 0x00}, 304 {0x3804, 0x0a}, 305 {0x3805, 0x3f}, 306 {0x3806, 0x07}, 307 {0x3807, 0xa3}, 308 {0x3808, 0x05}, 309 {0x3809, 0x10}, 310 {0x380a, 0x03}, 311 {0x380b, 0xcc}, 312 {0x3811, 0x0c}, 313 {0x3813, 0x06}, 314 {0x3814, 0x31}, 315 {0x3815, 0x31}, 316 {0x3a09, 0x28}, 317 {0x3a0a, 0x00}, 318 {0x3a0b, 0xf6}, 319 {0x3a0d, 0x08}, 320 {0x3a0e, 0x06}, 321 {0x4004, 0x04}, 322 {0x4837, 0x16}, 323 {0x4800, 0x24}, 324 {0x350a, 0x00}, 325 {0x350b, 0x10}, 326 {0x3500, 0x00}, 327 {0x3501, 0x1a}, 328 {0x3502, 0xf0}, 329 {0x3212, 0xa0}, 330 {0x0100, 0x01}, 331 }; 332 333 static const struct reg_sequence ov5647_640x480_10bpp[] = { 334 {0x3036, 0x46}, 335 {0x3821, 0x03}, 336 {0x3820, 0x41}, 337 {0x3612, 0x59}, 338 {0x3618, 0x00}, 339 {0x3814, 0x35}, 340 {0x3815, 0x35}, 341 {0x3708, 0x64}, 342 {0x3709, 0x52}, 343 {0x3800, 0x00}, 344 {0x3801, 0x10}, 345 {0x3802, 0x00}, 346 {0x3803, 0x00}, 347 {0x3804, 0x0a}, 348 {0x3805, 0x2f}, 349 {0x3806, 0x07}, 350 {0x3807, 0x9f}, 351 {0x3808, 0x02}, 352 {0x3809, 0x80}, 353 {0x380a, 0x01}, 354 {0x380b, 0xe0}, 355 {0x3a09, 0x2e}, 356 {0x3a0a, 0x00}, 357 {0x3a0b, 0xfb}, 358 {0x3a0d, 0x02}, 359 {0x3a0e, 0x01}, 360 {0x4004, 0x02}, 361 {0x4800, 0x34}, 362 {0x0100, 0x01}, 363 }; 364 365 static const struct ov5647_mode ov5647_modes[] = { 366 /* 2592x1944 full resolution full FOV 10-bit mode. */ 367 { 368 .format = { 369 .code = MEDIA_BUS_FMT_SBGGR10_1X10, 370 .colorspace = V4L2_COLORSPACE_RAW, 371 .field = V4L2_FIELD_NONE, 372 .width = 2592, 373 .height = 1944 374 }, 375 .crop = { 376 .left = OV5647_PIXEL_ARRAY_LEFT, 377 .top = OV5647_PIXEL_ARRAY_TOP, 378 .width = 2592, 379 .height = 1944 380 }, 381 .pixel_rate = 87500000, 382 .link_freq_index = FREQ_INDEX_FULL, 383 .hts = 2844, 384 .vts = 0x7b0, 385 .reg_list = ov5647_2592x1944_10bpp, 386 .num_regs = ARRAY_SIZE(ov5647_2592x1944_10bpp) 387 }, 388 /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */ 389 { 390 .format = { 391 .code = MEDIA_BUS_FMT_SBGGR10_1X10, 392 .colorspace = V4L2_COLORSPACE_RAW, 393 .field = V4L2_FIELD_NONE, 394 .width = 1920, 395 .height = 1080 396 }, 397 .crop = { 398 .left = 348 + OV5647_PIXEL_ARRAY_LEFT, 399 .top = 434 + OV5647_PIXEL_ARRAY_TOP, 400 .width = 1928, 401 .height = 1080, 402 }, 403 .pixel_rate = 87500000, 404 .link_freq_index = FREQ_INDEX_FULL, 405 .hts = 2416, 406 .vts = 0x450, 407 .reg_list = ov5647_1080p30_10bpp, 408 .num_regs = ARRAY_SIZE(ov5647_1080p30_10bpp) 409 }, 410 /* 2x2 binned full FOV 10-bit mode. */ 411 { 412 .format = { 413 .code = MEDIA_BUS_FMT_SBGGR10_1X10, 414 .colorspace = V4L2_COLORSPACE_RAW, 415 .field = V4L2_FIELD_NONE, 416 .width = 1296, 417 .height = 972 418 }, 419 .crop = { 420 .left = OV5647_PIXEL_ARRAY_LEFT, 421 .top = OV5647_PIXEL_ARRAY_TOP, 422 .width = 2592, 423 .height = 1944, 424 }, 425 .pixel_rate = 87500000, 426 .link_freq_index = FREQ_INDEX_FULL, 427 .hts = 1896, 428 .vts = 0x59b, 429 .reg_list = ov5647_2x2binned_10bpp, 430 .num_regs = ARRAY_SIZE(ov5647_2x2binned_10bpp) 431 }, 432 /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */ 433 { 434 .format = { 435 .code = MEDIA_BUS_FMT_SBGGR10_1X10, 436 .colorspace = V4L2_COLORSPACE_RAW, 437 .field = V4L2_FIELD_NONE, 438 .width = 640, 439 .height = 480 440 }, 441 .crop = { 442 .left = 16 + OV5647_PIXEL_ARRAY_LEFT, 443 .top = OV5647_PIXEL_ARRAY_TOP, 444 .width = 2560, 445 .height = 1920, 446 }, 447 .pixel_rate = 58333000, 448 .link_freq_index = FREQ_INDEX_VGA, 449 .hts = 1852, 450 .vts = 0x1f8, 451 .reg_list = ov5647_640x480_10bpp, 452 .num_regs = ARRAY_SIZE(ov5647_640x480_10bpp) 453 }, 454 }; 455 456 /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */ 457 #define OV5647_DEFAULT_MODE (&ov5647_modes[3]) 458 #define OV5647_DEFAULT_FORMAT (ov5647_modes[3].format) 459 460 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel) 461 { 462 struct ov5647 *sensor = to_sensor(sd); 463 464 return cci_update_bits(sensor->regmap, OV5647_REG_MIPI_CTRL14, 465 OV5647_REG_MIPI_CTRL14_CHANNEL_MASK, 466 channel << OV5647_REG_MIPI_CTRL14_CHANNEL_SHIFT, 467 NULL); 468 } 469 470 static int ov5647_set_mode(struct v4l2_subdev *sd) 471 { 472 struct i2c_client *client = v4l2_get_subdevdata(sd); 473 struct ov5647 *sensor = to_sensor(sd); 474 u64 resetval, rdval; 475 int ret; 476 477 ret = cci_read(sensor->regmap, OV5647_SW_STANDBY, &rdval, NULL); 478 if (ret < 0) 479 return ret; 480 481 ret = regmap_multi_reg_write(sensor->regmap, ov5647_common_regs, 482 ARRAY_SIZE(ov5647_common_regs)); 483 if (ret < 0) { 484 dev_err(&client->dev, "write sensor common regs error\n"); 485 return ret; 486 } 487 488 ret = regmap_multi_reg_write(sensor->regmap, sensor->mode->reg_list, 489 sensor->mode->num_regs); 490 if (ret < 0) { 491 dev_err(&client->dev, "write sensor default regs error\n"); 492 return ret; 493 } 494 495 ret = ov5647_set_virtual_channel(sd, 0); 496 if (ret < 0) 497 return ret; 498 499 ret = cci_read(sensor->regmap, OV5647_SW_STANDBY, &resetval, NULL); 500 if (ret < 0) 501 return ret; 502 503 if (!(resetval & 0x01)) { 504 dev_err(&client->dev, "Device was in SW standby"); 505 ret = cci_write(sensor->regmap, OV5647_SW_STANDBY, 0x01, NULL); 506 if (ret < 0) 507 return ret; 508 } 509 510 return 0; 511 } 512 513 static int ov5647_stream_stop(struct ov5647 *sensor) 514 { 515 int ret = 0; 516 517 cci_write(sensor->regmap, OV5647_REG_MIPI_CTRL00, 518 MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE | 519 MIPI_CTRL00_CLOCK_LANE_DISABLE, &ret); 520 cci_write(sensor->regmap, OV5647_REG_FRAME_OFF_NUMBER, 0x0f, &ret); 521 cci_write(sensor->regmap, OV5640_REG_PAD_OUT, 0x01, &ret); 522 523 return ret; 524 } 525 526 static int ov5647_enable_streams(struct v4l2_subdev *sd, 527 struct v4l2_subdev_state *state, u32 pad, 528 u64 streams_mask) 529 { 530 struct i2c_client *client = v4l2_get_subdevdata(sd); 531 struct ov5647 *sensor = to_sensor(sd); 532 u8 val = MIPI_CTRL00_BUS_IDLE; 533 int ret; 534 535 ret = pm_runtime_resume_and_get(&client->dev); 536 if (ret < 0) 537 return ret; 538 539 ret = ov5647_set_mode(sd); 540 if (ret) { 541 dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret); 542 goto done; 543 } 544 545 /* Apply customized values from user when stream starts. */ 546 ret = __v4l2_ctrl_handler_setup(sd->ctrl_handler); 547 if (ret) 548 goto done; 549 550 if (sensor->clock_ncont) 551 val |= MIPI_CTRL00_CLOCK_LANE_GATE | 552 MIPI_CTRL00_LINE_SYNC_ENABLE; 553 554 cci_write(sensor->regmap, OV5647_REG_MIPI_CTRL00, val, &ret); 555 cci_write(sensor->regmap, OV5647_REG_FRAME_OFF_NUMBER, 0x00, &ret); 556 cci_write(sensor->regmap, OV5640_REG_PAD_OUT, 0x00, &ret); 557 558 done: 559 if (ret) 560 pm_runtime_put(&client->dev); 561 562 return ret; 563 } 564 565 static int ov5647_disable_streams(struct v4l2_subdev *sd, 566 struct v4l2_subdev_state *state, u32 pad, 567 u64 streams_mask) 568 { 569 struct i2c_client *client = v4l2_get_subdevdata(sd); 570 struct ov5647 *sensor = to_sensor(sd); 571 int ret; 572 573 ret = ov5647_stream_stop(sensor); 574 575 pm_runtime_put(&client->dev); 576 577 return ret; 578 } 579 580 static int ov5647_power_on(struct device *dev) 581 { 582 struct ov5647 *sensor = dev_get_drvdata(dev); 583 int ret; 584 585 dev_dbg(dev, "OV5647 power on\n"); 586 587 ret = regulator_bulk_enable(OV5647_NUM_SUPPLIES, sensor->supplies); 588 if (ret < 0) { 589 dev_err(dev, "Failed to enable regulators: %d\n", ret); 590 return ret; 591 } 592 593 ret = gpiod_set_value_cansleep(sensor->pwdn, 0); 594 if (ret < 0) { 595 dev_err(dev, "pwdn gpio set value failed: %d\n", ret); 596 goto error_reg_disable; 597 } 598 599 msleep(PWDN_ACTIVE_DELAY_MS); 600 601 ret = clk_prepare_enable(sensor->xclk); 602 if (ret < 0) { 603 dev_err(dev, "clk prepare enable failed\n"); 604 goto error_pwdn; 605 } 606 607 ret = regmap_multi_reg_write(sensor->regmap, sensor_oe_enable_regs, 608 ARRAY_SIZE(sensor_oe_enable_regs)); 609 if (ret < 0) { 610 dev_err(dev, "write sensor_oe_enable_regs error\n"); 611 goto error_clk_disable; 612 } 613 614 /* Stream off to coax lanes into LP-11 state. */ 615 ret = ov5647_stream_stop(sensor); 616 if (ret < 0) { 617 dev_err(dev, "camera not available, check power\n"); 618 goto error_clk_disable; 619 } 620 621 return 0; 622 623 error_clk_disable: 624 clk_disable_unprepare(sensor->xclk); 625 error_pwdn: 626 gpiod_set_value_cansleep(sensor->pwdn, 1); 627 error_reg_disable: 628 regulator_bulk_disable(OV5647_NUM_SUPPLIES, sensor->supplies); 629 630 return ret; 631 } 632 633 static int ov5647_power_off(struct device *dev) 634 { 635 struct ov5647 *sensor = dev_get_drvdata(dev); 636 int ret; 637 638 dev_dbg(dev, "OV5647 power off\n"); 639 640 ret = regmap_multi_reg_write(sensor->regmap, sensor_oe_disable_regs, 641 ARRAY_SIZE(sensor_oe_disable_regs)); 642 if (ret < 0) 643 dev_dbg(dev, "disable oe failed\n"); 644 645 /* Enter software standby */ 646 ret = cci_update_bits(sensor->regmap, OV5647_SW_STANDBY, 0x01, 0x00, NULL); 647 if (ret < 0) 648 dev_dbg(dev, "software standby failed\n"); 649 650 clk_disable_unprepare(sensor->xclk); 651 gpiod_set_value_cansleep(sensor->pwdn, 1); 652 regulator_bulk_disable(OV5647_NUM_SUPPLIES, sensor->supplies); 653 654 return 0; 655 } 656 657 #ifdef CONFIG_VIDEO_ADV_DEBUG 658 static int ov5647_sensor_get_register(struct v4l2_subdev *sd, 659 struct v4l2_dbg_register *reg) 660 { 661 struct ov5647 *sensor = to_sensor(sd); 662 int ret; 663 u64 val; 664 665 ret = cci_read(sensor->regmap, reg->reg & 0xff, &val, NULL); 666 if (ret < 0) 667 return ret; 668 669 reg->val = val; 670 reg->size = 1; 671 672 return 0; 673 } 674 675 static int ov5647_sensor_set_register(struct v4l2_subdev *sd, 676 const struct v4l2_dbg_register *reg) 677 { 678 struct ov5647 *sensor = to_sensor(sd); 679 680 return cci_write(sensor->regmap, reg->reg & 0xff, reg->val & 0xff, NULL); 681 } 682 #endif 683 684 /* Subdev core operations registration */ 685 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = { 686 .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 687 .unsubscribe_event = v4l2_event_subdev_unsubscribe, 688 #ifdef CONFIG_VIDEO_ADV_DEBUG 689 .g_register = ov5647_sensor_get_register, 690 .s_register = ov5647_sensor_set_register, 691 #endif 692 }; 693 694 static const struct v4l2_rect * 695 __ov5647_get_pad_crop(struct ov5647 *ov5647, 696 struct v4l2_subdev_state *sd_state, 697 unsigned int pad, enum v4l2_subdev_format_whence which) 698 { 699 switch (which) { 700 case V4L2_SUBDEV_FORMAT_TRY: 701 return v4l2_subdev_state_get_crop(sd_state, pad); 702 case V4L2_SUBDEV_FORMAT_ACTIVE: 703 return &ov5647->mode->crop; 704 } 705 706 return NULL; 707 } 708 709 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = { 710 .s_stream = v4l2_subdev_s_stream_helper, 711 }; 712 713 /* 714 * This function returns the mbus code for the current settings of the HFLIP 715 * and VFLIP controls. 716 */ 717 static u32 ov5647_get_mbus_code(struct v4l2_subdev *sd) 718 { 719 struct ov5647 *sensor = to_sensor(sd); 720 /* The control values are only 0 or 1. */ 721 int index = sensor->hflip->val | (sensor->vflip->val << 1); 722 723 static const u32 codes[4] = { 724 MEDIA_BUS_FMT_SGBRG10_1X10, 725 MEDIA_BUS_FMT_SBGGR10_1X10, 726 MEDIA_BUS_FMT_SRGGB10_1X10, 727 MEDIA_BUS_FMT_SGRBG10_1X10 728 }; 729 730 return codes[index]; 731 } 732 733 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd, 734 struct v4l2_subdev_state *sd_state, 735 struct v4l2_subdev_mbus_code_enum *code) 736 { 737 if (code->index > 0) 738 return -EINVAL; 739 740 code->code = ov5647_get_mbus_code(sd); 741 742 return 0; 743 } 744 745 static int ov5647_enum_frame_size(struct v4l2_subdev *sd, 746 struct v4l2_subdev_state *sd_state, 747 struct v4l2_subdev_frame_size_enum *fse) 748 { 749 const struct v4l2_mbus_framefmt *fmt; 750 751 if (fse->code != ov5647_get_mbus_code(sd) || 752 fse->index >= ARRAY_SIZE(ov5647_modes)) 753 return -EINVAL; 754 755 fmt = &ov5647_modes[fse->index].format; 756 fse->min_width = fmt->width; 757 fse->max_width = fmt->width; 758 fse->min_height = fmt->height; 759 fse->max_height = fmt->height; 760 761 return 0; 762 } 763 764 static int ov5647_get_pad_fmt(struct v4l2_subdev *sd, 765 struct v4l2_subdev_state *sd_state, 766 struct v4l2_subdev_format *format) 767 { 768 struct v4l2_mbus_framefmt *fmt = &format->format; 769 const struct v4l2_mbus_framefmt *sensor_format; 770 struct ov5647 *sensor = to_sensor(sd); 771 772 switch (format->which) { 773 case V4L2_SUBDEV_FORMAT_TRY: 774 sensor_format = v4l2_subdev_state_get_format(sd_state, 775 format->pad); 776 break; 777 default: 778 sensor_format = &sensor->mode->format; 779 break; 780 } 781 782 *fmt = *sensor_format; 783 /* The code we pass back must reflect the current h/vflips. */ 784 fmt->code = ov5647_get_mbus_code(sd); 785 786 return 0; 787 } 788 789 static int ov5647_set_pad_fmt(struct v4l2_subdev *sd, 790 struct v4l2_subdev_state *sd_state, 791 struct v4l2_subdev_format *format) 792 { 793 struct v4l2_mbus_framefmt *fmt = &format->format; 794 struct ov5647 *sensor = to_sensor(sd); 795 const struct ov5647_mode *mode; 796 797 mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes), 798 format.width, format.height, 799 fmt->width, fmt->height); 800 801 /* Update the sensor mode and apply at it at streamon time. */ 802 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 803 *v4l2_subdev_state_get_format(sd_state, format->pad) = mode->format; 804 } else { 805 int exposure_max, exposure_def; 806 int hblank, vblank; 807 808 sensor->mode = mode; 809 __v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate, 810 mode->pixel_rate, 1, mode->pixel_rate); 811 812 hblank = mode->hts - mode->format.width; 813 __v4l2_ctrl_modify_range(sensor->hblank, hblank, 814 OV5647_HTS_MAX - mode->format.width, 1, 815 hblank); 816 817 vblank = mode->vts - mode->format.height; 818 __v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN, 819 OV5647_VTS_MAX - mode->format.height, 820 1, vblank); 821 __v4l2_ctrl_s_ctrl(sensor->vblank, vblank); 822 823 exposure_max = mode->vts - 4; 824 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT); 825 __v4l2_ctrl_modify_range(sensor->exposure, 826 sensor->exposure->minimum, 827 exposure_max, sensor->exposure->step, 828 exposure_def); 829 830 __v4l2_ctrl_s_ctrl(sensor->link_freq, mode->link_freq_index); 831 } 832 *fmt = mode->format; 833 /* The code we pass back must reflect the current h/vflips. */ 834 fmt->code = ov5647_get_mbus_code(sd); 835 836 return 0; 837 } 838 839 static int ov5647_get_selection(struct v4l2_subdev *sd, 840 struct v4l2_subdev_state *sd_state, 841 struct v4l2_subdev_selection *sel) 842 { 843 switch (sel->target) { 844 case V4L2_SEL_TGT_CROP: { 845 struct ov5647 *sensor = to_sensor(sd); 846 847 sel->r = *__ov5647_get_pad_crop(sensor, sd_state, sel->pad, 848 sel->which); 849 850 return 0; 851 } 852 853 case V4L2_SEL_TGT_NATIVE_SIZE: 854 sel->r.top = 0; 855 sel->r.left = 0; 856 sel->r.width = OV5647_NATIVE_WIDTH; 857 sel->r.height = OV5647_NATIVE_HEIGHT; 858 859 return 0; 860 861 case V4L2_SEL_TGT_CROP_DEFAULT: 862 case V4L2_SEL_TGT_CROP_BOUNDS: 863 sel->r.top = OV5647_PIXEL_ARRAY_TOP; 864 sel->r.left = OV5647_PIXEL_ARRAY_LEFT; 865 sel->r.width = OV5647_PIXEL_ARRAY_WIDTH; 866 sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT; 867 868 return 0; 869 } 870 871 return -EINVAL; 872 } 873 874 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = { 875 .enum_mbus_code = ov5647_enum_mbus_code, 876 .enum_frame_size = ov5647_enum_frame_size, 877 .set_fmt = ov5647_set_pad_fmt, 878 .get_fmt = ov5647_get_pad_fmt, 879 .get_selection = ov5647_get_selection, 880 .enable_streams = ov5647_enable_streams, 881 .disable_streams = ov5647_disable_streams, 882 }; 883 884 static const struct v4l2_subdev_ops ov5647_subdev_ops = { 885 .core = &ov5647_subdev_core_ops, 886 .video = &ov5647_subdev_video_ops, 887 .pad = &ov5647_subdev_pad_ops, 888 }; 889 890 static int ov5647_detect(struct v4l2_subdev *sd) 891 { 892 struct ov5647 *sensor = to_sensor(sd); 893 struct i2c_client *client = v4l2_get_subdevdata(sd); 894 u64 read; 895 int ret; 896 897 ret = cci_write(sensor->regmap, OV5647_SW_RESET, 0x01, NULL); 898 if (ret < 0) 899 return ret; 900 901 ret = cci_read(sensor->regmap, OV5647_REG_CHIPID, &read, NULL); 902 if (ret < 0) 903 return dev_err_probe(&client->dev, ret, 904 "failed to read chip id %x\n", 905 OV5647_REG_CHIPID); 906 907 if (read != OV5647_CHIP_ID) { 908 dev_err(&client->dev, "Chip ID expected 0x5647 got 0x%llx", read); 909 return -ENODEV; 910 } 911 912 return cci_write(sensor->regmap, OV5647_SW_RESET, 0x00, NULL); 913 } 914 915 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 916 { 917 struct v4l2_mbus_framefmt *format = 918 v4l2_subdev_state_get_format(fh->state, 0); 919 struct v4l2_rect *crop = v4l2_subdev_state_get_crop(fh->state, 0); 920 921 crop->left = OV5647_PIXEL_ARRAY_LEFT; 922 crop->top = OV5647_PIXEL_ARRAY_TOP; 923 crop->width = OV5647_PIXEL_ARRAY_WIDTH; 924 crop->height = OV5647_PIXEL_ARRAY_HEIGHT; 925 926 *format = OV5647_DEFAULT_FORMAT; 927 928 return 0; 929 } 930 931 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = { 932 .open = ov5647_open, 933 }; 934 935 static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl) 936 { 937 struct ov5647 *sensor = container_of(ctrl->handler, 938 struct ov5647, ctrls); 939 struct v4l2_subdev *sd = &sensor->sd; 940 struct i2c_client *client = v4l2_get_subdevdata(sd); 941 int ret = 0; 942 943 if (ctrl->id == V4L2_CID_VBLANK) { 944 int exposure_max, exposure_def; 945 946 /* Update max exposure while meeting expected vblanking */ 947 exposure_max = sensor->mode->format.height + ctrl->val - 4; 948 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT); 949 __v4l2_ctrl_modify_range(sensor->exposure, 950 sensor->exposure->minimum, 951 exposure_max, sensor->exposure->step, 952 exposure_def); 953 } 954 955 /* 956 * If the device is not powered up do not apply any controls 957 * to H/W at this time. Instead the controls will be restored 958 * at s_stream(1) time. 959 */ 960 if (pm_runtime_get_if_in_use(&client->dev) == 0) 961 return 0; 962 963 switch (ctrl->id) { 964 case V4L2_CID_AUTO_WHITE_BALANCE: 965 ret = cci_write(sensor->regmap, OV5647_REG_AWB, 966 ctrl->val ? 1 : 0, NULL); 967 break; 968 case V4L2_CID_AUTOGAIN: 969 /* Non-zero turns on AGC by clearing bit 1.*/ 970 return cci_update_bits(sensor->regmap, OV5647_REG_AEC_AGC, BIT(1), 971 ctrl->val ? 0 : BIT(1), NULL); 972 break; 973 case V4L2_CID_EXPOSURE_AUTO: 974 /* 975 * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by 976 * clearing bit 0. 977 */ 978 return cci_update_bits(sensor->regmap, OV5647_REG_AEC_AGC, BIT(0), 979 ctrl->val == V4L2_EXPOSURE_MANUAL ? BIT(0) : 0, NULL); 980 break; 981 case V4L2_CID_ANALOGUE_GAIN: 982 /* 10 bits of gain, 2 in the high register. */ 983 return cci_write(sensor->regmap, OV5647_REG_GAIN, 984 ctrl->val & 0x3ff, NULL); 985 break; 986 case V4L2_CID_EXPOSURE: 987 /* 988 * Sensor has 20 bits, but the bottom 4 bits are fractions of a line 989 * which we leave as zero (and don't receive in "val"). 990 */ 991 ret = cci_write(sensor->regmap, OV5647_REG_EXPOSURE, 992 ctrl->val << 4, NULL); 993 break; 994 case V4L2_CID_VBLANK: 995 ret = cci_write(sensor->regmap, OV5647_REG_VTS, 996 sensor->mode->format.height + ctrl->val, NULL); 997 break; 998 case V4L2_CID_HBLANK: 999 ret = cci_write(sensor->regmap, OV5647_REG_HTS, 1000 sensor->mode->format.width + ctrl->val, &ret); 1001 break; 1002 case V4L2_CID_TEST_PATTERN: 1003 ret = cci_write(sensor->regmap, OV5647_REG_ISPCTRL3D, 1004 ov5647_test_pattern_val[ctrl->val], NULL); 1005 break; 1006 case V4L2_CID_HFLIP: 1007 /* There's an in-built hflip in the sensor, so account for that here. */ 1008 ret = cci_update_bits(sensor->regmap, OV5647_REG_TIMING_TC_H, BIT(1), 1009 ctrl->val ? 0 : BIT(1), NULL); 1010 break; 1011 case V4L2_CID_VFLIP: 1012 ret = cci_update_bits(sensor->regmap, OV5647_REG_TIMING_TC_V, BIT(1), 1013 ctrl->val ? BIT(1) : 0, NULL); 1014 break; 1015 1016 default: 1017 dev_info(&client->dev, 1018 "Control (id:0x%x, val:0x%x) not supported\n", 1019 ctrl->id, ctrl->val); 1020 return -EINVAL; 1021 } 1022 1023 pm_runtime_put(&client->dev); 1024 1025 return ret; 1026 } 1027 1028 static const struct v4l2_ctrl_ops ov5647_ctrl_ops = { 1029 .s_ctrl = ov5647_s_ctrl, 1030 }; 1031 1032 static int ov5647_configure_regulators(struct device *dev, 1033 struct ov5647 *sensor) 1034 { 1035 for (unsigned int i = 0; i < OV5647_NUM_SUPPLIES; i++) 1036 sensor->supplies[i].supply = ov5647_supply_names[i]; 1037 1038 return devm_regulator_bulk_get(dev, OV5647_NUM_SUPPLIES, 1039 sensor->supplies); 1040 } 1041 1042 static int ov5647_init_controls(struct ov5647 *sensor) 1043 { 1044 struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd); 1045 struct v4l2_fwnode_device_properties props; 1046 int hblank, exposure_max, exposure_def; 1047 struct device *dev = &client->dev; 1048 1049 v4l2_ctrl_handler_init(&sensor->ctrls, 14); 1050 1051 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1052 V4L2_CID_AUTOGAIN, 0, 1, 1, 0); 1053 1054 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1055 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0); 1056 1057 v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops, 1058 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 1059 0, V4L2_EXPOSURE_MANUAL); 1060 1061 exposure_max = sensor->mode->vts - 4; 1062 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT); 1063 sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1064 V4L2_CID_EXPOSURE, 1065 OV5647_EXPOSURE_MIN, 1066 exposure_max, OV5647_EXPOSURE_STEP, 1067 exposure_def); 1068 1069 /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */ 1070 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1071 V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32); 1072 1073 /* By default, PIXEL_RATE is read only, but it does change per mode */ 1074 sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, NULL, 1075 V4L2_CID_PIXEL_RATE, 1076 sensor->mode->pixel_rate, 1077 sensor->mode->pixel_rate, 1, 1078 sensor->mode->pixel_rate); 1079 1080 hblank = sensor->mode->hts - sensor->mode->format.width; 1081 sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1082 V4L2_CID_HBLANK, hblank, 1083 OV5647_HTS_MAX - 1084 sensor->mode->format.width, 1, 1085 hblank); 1086 1087 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1088 V4L2_CID_VBLANK, OV5647_VBLANK_MIN, 1089 OV5647_VTS_MAX - 1090 sensor->mode->format.height, 1, 1091 sensor->mode->vts - 1092 sensor->mode->format.height); 1093 1094 v4l2_ctrl_new_std_menu_items(&sensor->ctrls, &ov5647_ctrl_ops, 1095 V4L2_CID_TEST_PATTERN, 1096 ARRAY_SIZE(ov5647_test_pattern_menu) - 1, 1097 0, 0, ov5647_test_pattern_menu); 1098 1099 sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1100 V4L2_CID_HFLIP, 0, 1, 1, 0); 1101 1102 sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops, 1103 V4L2_CID_VFLIP, 0, 1, 1, 0); 1104 1105 sensor->link_freq = 1106 v4l2_ctrl_new_int_menu(&sensor->ctrls, NULL, V4L2_CID_LINK_FREQ, 1107 ARRAY_SIZE(ov5647_link_freqs) - 1, 1108 sensor->mode->link_freq_index, 1109 ov5647_link_freqs); 1110 if (sensor->link_freq) 1111 sensor->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1112 1113 v4l2_fwnode_device_parse(dev, &props); 1114 1115 v4l2_ctrl_new_fwnode_properties(&sensor->ctrls, &ov5647_ctrl_ops, 1116 &props); 1117 1118 if (sensor->ctrls.error) 1119 goto handler_free; 1120 1121 sensor->sd.ctrl_handler = &sensor->ctrls; 1122 1123 return 0; 1124 1125 handler_free: 1126 dev_err(&client->dev, "%s Controls initialization failed (%d)\n", 1127 __func__, sensor->ctrls.error); 1128 v4l2_ctrl_handler_free(&sensor->ctrls); 1129 1130 return sensor->ctrls.error; 1131 } 1132 1133 static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np) 1134 { 1135 struct v4l2_fwnode_endpoint bus_cfg = { 1136 .bus_type = V4L2_MBUS_CSI2_DPHY, 1137 }; 1138 struct device_node *ep __free(device_node) = 1139 of_graph_get_endpoint_by_regs(np, 0, -1); 1140 int ret; 1141 1142 if (!ep) 1143 return -EINVAL; 1144 1145 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg); 1146 if (ret) 1147 return ret; 1148 1149 sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags & 1150 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; 1151 1152 return 0; 1153 } 1154 1155 static int ov5647_probe(struct i2c_client *client) 1156 { 1157 struct device_node *np = client->dev.of_node; 1158 struct device *dev = &client->dev; 1159 struct ov5647 *sensor; 1160 struct v4l2_subdev *sd; 1161 u32 xclk_freq; 1162 int ret; 1163 1164 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 1165 if (!sensor) 1166 return -ENOMEM; 1167 1168 if (IS_ENABLED(CONFIG_OF) && np) { 1169 ret = ov5647_parse_dt(sensor, np); 1170 if (ret) { 1171 dev_err(dev, "DT parsing error: %d\n", ret); 1172 return ret; 1173 } 1174 } 1175 1176 sensor->xclk = devm_v4l2_sensor_clk_get(dev, NULL); 1177 if (IS_ERR(sensor->xclk)) 1178 return dev_err_probe(dev, PTR_ERR(sensor->xclk), 1179 "could not get xclk\n"); 1180 1181 xclk_freq = clk_get_rate(sensor->xclk); 1182 if (xclk_freq != 25000000) { 1183 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq); 1184 return -EINVAL; 1185 } 1186 1187 /* Request the power down GPIO asserted. */ 1188 sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH); 1189 if (IS_ERR(sensor->pwdn)) { 1190 dev_err(dev, "Failed to get 'pwdn' gpio\n"); 1191 return -EINVAL; 1192 } 1193 1194 ret = ov5647_configure_regulators(dev, sensor); 1195 if (ret) 1196 dev_err_probe(dev, ret, "Failed to get power regulators\n"); 1197 1198 sensor->mode = OV5647_DEFAULT_MODE; 1199 1200 sd = &sensor->sd; 1201 v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops); 1202 sd->internal_ops = &ov5647_subdev_internal_ops; 1203 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 1204 1205 ret = ov5647_init_controls(sensor); 1206 if (ret) 1207 return ret; 1208 1209 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 1210 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; 1211 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad); 1212 if (ret < 0) 1213 goto ctrl_handler_free; 1214 1215 sensor->regmap = devm_cci_regmap_init_i2c(client, 16); 1216 if (IS_ERR(sensor->regmap)) { 1217 ret = dev_err_probe(dev, PTR_ERR(sensor->regmap), 1218 "Failed to init CCI\n"); 1219 goto entity_cleanup; 1220 } 1221 1222 ret = ov5647_power_on(dev); 1223 if (ret) 1224 goto entity_cleanup; 1225 1226 ret = ov5647_detect(sd); 1227 if (ret < 0) 1228 goto power_off; 1229 1230 sd->state_lock = sensor->ctrls.lock; 1231 ret = v4l2_subdev_init_finalize(sd); 1232 if (ret < 0) { 1233 ret = dev_err_probe(dev, ret, "failed to init subdev\n"); 1234 goto power_off; 1235 } 1236 1237 /* Enable runtime PM and turn off the device */ 1238 pm_runtime_set_active(dev); 1239 pm_runtime_enable(dev); 1240 1241 ret = v4l2_async_register_subdev_sensor(sd); 1242 if (ret < 0) 1243 goto v4l2_subdev_cleanup; 1244 1245 pm_runtime_idle(dev); 1246 1247 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n"); 1248 1249 return 0; 1250 1251 v4l2_subdev_cleanup: 1252 v4l2_subdev_cleanup(sd); 1253 power_off: 1254 ov5647_power_off(dev); 1255 entity_cleanup: 1256 media_entity_cleanup(&sd->entity); 1257 ctrl_handler_free: 1258 v4l2_ctrl_handler_free(&sensor->ctrls); 1259 1260 return ret; 1261 } 1262 1263 static void ov5647_remove(struct i2c_client *client) 1264 { 1265 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1266 struct ov5647 *sensor = to_sensor(sd); 1267 1268 v4l2_async_unregister_subdev(&sensor->sd); 1269 v4l2_subdev_cleanup(sd); 1270 media_entity_cleanup(&sensor->sd.entity); 1271 v4l2_ctrl_handler_free(&sensor->ctrls); 1272 v4l2_device_unregister_subdev(sd); 1273 pm_runtime_disable(&client->dev); 1274 } 1275 1276 static const struct dev_pm_ops ov5647_pm_ops = { 1277 SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL) 1278 }; 1279 1280 static const struct i2c_device_id ov5647_id[] = { 1281 { "ov5647" }, 1282 { /* sentinel */ } 1283 }; 1284 MODULE_DEVICE_TABLE(i2c, ov5647_id); 1285 1286 #if IS_ENABLED(CONFIG_OF) 1287 static const struct of_device_id ov5647_of_match[] = { 1288 { .compatible = "ovti,ov5647" }, 1289 { /* sentinel */ }, 1290 }; 1291 MODULE_DEVICE_TABLE(of, ov5647_of_match); 1292 #endif 1293 1294 static struct i2c_driver ov5647_driver = { 1295 .driver = { 1296 .of_match_table = of_match_ptr(ov5647_of_match), 1297 .name = "ov5647", 1298 .pm = &ov5647_pm_ops, 1299 }, 1300 .probe = ov5647_probe, 1301 .remove = ov5647_remove, 1302 .id_table = ov5647_id, 1303 }; 1304 1305 module_i2c_driver(ov5647_driver); 1306 1307 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>"); 1308 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors"); 1309 MODULE_LICENSE("GPL v2"); 1310