1 /* 2 * A V4L2 driver for OmniVision OV5647 cameras. 3 * 4 * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver 5 * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com> 6 * 7 * Based on Omnivision OV7670 Camera Driver 8 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net> 9 * 10 * Copyright (C) 2016, Synopsys, Inc. 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License as 14 * published by the Free Software Foundation version 2. 15 * 16 * This program is distributed .as is. WITHOUT ANY WARRANTY of any 17 * kind, whether express or implied; without even the implied warranty 18 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 */ 21 22 #include <linux/clk.h> 23 #include <linux/delay.h> 24 #include <linux/i2c.h> 25 #include <linux/init.h> 26 #include <linux/io.h> 27 #include <linux/module.h> 28 #include <linux/slab.h> 29 #include <linux/videodev2.h> 30 #include <media/v4l2-device.h> 31 #include <media/v4l2-image-sizes.h> 32 #include <media/v4l2-mediabus.h> 33 #include <media/v4l2-of.h> 34 35 #define SENSOR_NAME "ov5647" 36 37 #define OV5647_SW_RESET 0x0103 38 #define OV5647_REG_CHIPID_H 0x300A 39 #define OV5647_REG_CHIPID_L 0x300B 40 41 #define REG_TERM 0xfffe 42 #define VAL_TERM 0xfe 43 #define REG_DLY 0xffff 44 45 #define OV5647_ROW_START 0x01 46 #define OV5647_ROW_START_MIN 0 47 #define OV5647_ROW_START_MAX 2004 48 #define OV5647_ROW_START_DEF 54 49 50 #define OV5647_COLUMN_START 0x02 51 #define OV5647_COLUMN_START_MIN 0 52 #define OV5647_COLUMN_START_MAX 2750 53 #define OV5647_COLUMN_START_DEF 16 54 55 #define OV5647_WINDOW_HEIGHT 0x03 56 #define OV5647_WINDOW_HEIGHT_MIN 2 57 #define OV5647_WINDOW_HEIGHT_MAX 2006 58 #define OV5647_WINDOW_HEIGHT_DEF 1944 59 60 #define OV5647_WINDOW_WIDTH 0x04 61 #define OV5647_WINDOW_WIDTH_MIN 2 62 #define OV5647_WINDOW_WIDTH_MAX 2752 63 #define OV5647_WINDOW_WIDTH_DEF 2592 64 65 struct regval_list { 66 u16 addr; 67 u8 data; 68 }; 69 70 struct ov5647 { 71 struct v4l2_subdev sd; 72 struct media_pad pad; 73 struct mutex lock; 74 struct v4l2_mbus_framefmt format; 75 unsigned int width; 76 unsigned int height; 77 int power_count; 78 struct clk *xclk; 79 }; 80 81 static inline struct ov5647 *to_state(struct v4l2_subdev *sd) 82 { 83 return container_of(sd, struct ov5647, sd); 84 } 85 86 static struct regval_list sensor_oe_disable_regs[] = { 87 {0x3000, 0x00}, 88 {0x3001, 0x00}, 89 {0x3002, 0x00}, 90 }; 91 92 static struct regval_list sensor_oe_enable_regs[] = { 93 {0x3000, 0x0f}, 94 {0x3001, 0xff}, 95 {0x3002, 0xe4}, 96 }; 97 98 static struct regval_list ov5647_640x480[] = { 99 {0x0100, 0x00}, 100 {0x0103, 0x01}, 101 {0x3034, 0x08}, 102 {0x3035, 0x21}, 103 {0x3036, 0x46}, 104 {0x303c, 0x11}, 105 {0x3106, 0xf5}, 106 {0x3821, 0x07}, 107 {0x3820, 0x41}, 108 {0x3827, 0xec}, 109 {0x370c, 0x0f}, 110 {0x3612, 0x59}, 111 {0x3618, 0x00}, 112 {0x5000, 0x06}, 113 {0x5001, 0x01}, 114 {0x5002, 0x41}, 115 {0x5003, 0x08}, 116 {0x5a00, 0x08}, 117 {0x3000, 0x00}, 118 {0x3001, 0x00}, 119 {0x3002, 0x00}, 120 {0x3016, 0x08}, 121 {0x3017, 0xe0}, 122 {0x3018, 0x44}, 123 {0x301c, 0xf8}, 124 {0x301d, 0xf0}, 125 {0x3a18, 0x00}, 126 {0x3a19, 0xf8}, 127 {0x3c01, 0x80}, 128 {0x3b07, 0x0c}, 129 {0x380c, 0x07}, 130 {0x380d, 0x68}, 131 {0x380e, 0x03}, 132 {0x380f, 0xd8}, 133 {0x3814, 0x31}, 134 {0x3815, 0x31}, 135 {0x3708, 0x64}, 136 {0x3709, 0x52}, 137 {0x3808, 0x02}, 138 {0x3809, 0x80}, 139 {0x380a, 0x01}, 140 {0x380b, 0xE0}, 141 {0x3801, 0x00}, 142 {0x3802, 0x00}, 143 {0x3803, 0x00}, 144 {0x3804, 0x0a}, 145 {0x3805, 0x3f}, 146 {0x3806, 0x07}, 147 {0x3807, 0xa1}, 148 {0x3811, 0x08}, 149 {0x3813, 0x02}, 150 {0x3630, 0x2e}, 151 {0x3632, 0xe2}, 152 {0x3633, 0x23}, 153 {0x3634, 0x44}, 154 {0x3636, 0x06}, 155 {0x3620, 0x64}, 156 {0x3621, 0xe0}, 157 {0x3600, 0x37}, 158 {0x3704, 0xa0}, 159 {0x3703, 0x5a}, 160 {0x3715, 0x78}, 161 {0x3717, 0x01}, 162 {0x3731, 0x02}, 163 {0x370b, 0x60}, 164 {0x3705, 0x1a}, 165 {0x3f05, 0x02}, 166 {0x3f06, 0x10}, 167 {0x3f01, 0x0a}, 168 {0x3a08, 0x01}, 169 {0x3a09, 0x27}, 170 {0x3a0a, 0x00}, 171 {0x3a0b, 0xf6}, 172 {0x3a0d, 0x04}, 173 {0x3a0e, 0x03}, 174 {0x3a0f, 0x58}, 175 {0x3a10, 0x50}, 176 {0x3a1b, 0x58}, 177 {0x3a1e, 0x50}, 178 {0x3a11, 0x60}, 179 {0x3a1f, 0x28}, 180 {0x4001, 0x02}, 181 {0x4004, 0x02}, 182 {0x4000, 0x09}, 183 {0x4837, 0x24}, 184 {0x4050, 0x6e}, 185 {0x4051, 0x8f}, 186 {0x0100, 0x01}, 187 }; 188 189 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val) 190 { 191 int ret; 192 unsigned char data[3] = { reg >> 8, reg & 0xff, val}; 193 struct i2c_client *client = v4l2_get_subdevdata(sd); 194 195 ret = i2c_master_send(client, data, 3); 196 if (ret < 0) 197 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n", 198 __func__, reg); 199 200 return ret; 201 } 202 203 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val) 204 { 205 int ret; 206 unsigned char data_w[2] = { reg >> 8, reg & 0xff }; 207 struct i2c_client *client = v4l2_get_subdevdata(sd); 208 209 ret = i2c_master_send(client, data_w, 2); 210 if (ret < 0) { 211 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n", 212 __func__, reg); 213 return ret; 214 } 215 216 ret = i2c_master_recv(client, val, 1); 217 if (ret < 0) 218 dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n", 219 __func__, reg); 220 221 return ret; 222 } 223 224 static int ov5647_write_array(struct v4l2_subdev *sd, 225 struct regval_list *regs, int array_size) 226 { 227 int i, ret; 228 229 for (i = 0; i < array_size; i++) { 230 ret = ov5647_write(sd, regs[i].addr, regs[i].data); 231 if (ret < 0) 232 return ret; 233 } 234 235 return 0; 236 } 237 238 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel) 239 { 240 u8 channel_id; 241 int ret; 242 243 ret = ov5647_read(sd, 0x4814, &channel_id); 244 if (ret < 0) 245 return ret; 246 247 channel_id &= ~(3 << 6); 248 return ov5647_write(sd, 0x4814, channel_id | (channel << 6)); 249 } 250 251 static int ov5647_stream_on(struct v4l2_subdev *sd) 252 { 253 int ret; 254 255 ret = ov5647_write(sd, 0x4202, 0x00); 256 if (ret < 0) 257 return ret; 258 259 return ov5647_write(sd, 0x300D, 0x00); 260 } 261 262 static int ov5647_stream_off(struct v4l2_subdev *sd) 263 { 264 int ret; 265 266 ret = ov5647_write(sd, 0x4202, 0x0f); 267 if (ret < 0) 268 return ret; 269 270 return ov5647_write(sd, 0x300D, 0x01); 271 } 272 273 static int set_sw_standby(struct v4l2_subdev *sd, bool standby) 274 { 275 int ret; 276 u8 rdval; 277 278 ret = ov5647_read(sd, 0x0100, &rdval); 279 if (ret < 0) 280 return ret; 281 282 if (standby) 283 rdval &= ~0x01; 284 else 285 rdval |= 0x01; 286 287 return ov5647_write(sd, 0x0100, rdval); 288 } 289 290 static int __sensor_init(struct v4l2_subdev *sd) 291 { 292 int ret; 293 u8 resetval, rdval; 294 struct i2c_client *client = v4l2_get_subdevdata(sd); 295 296 ret = ov5647_read(sd, 0x0100, &rdval); 297 if (ret < 0) 298 return ret; 299 300 ret = ov5647_write_array(sd, ov5647_640x480, 301 ARRAY_SIZE(ov5647_640x480)); 302 if (ret < 0) { 303 dev_err(&client->dev, "write sensor default regs error\n"); 304 return ret; 305 } 306 307 ret = ov5647_set_virtual_channel(sd, 0); 308 if (ret < 0) 309 return ret; 310 311 ret = ov5647_read(sd, 0x0100, &resetval); 312 if (ret < 0) 313 return ret; 314 315 if (!(resetval & 0x01)) { 316 dev_err(&client->dev, "Device was in SW standby"); 317 ret = ov5647_write(sd, 0x0100, 0x01); 318 if (ret < 0) 319 return ret; 320 } 321 322 return ov5647_write(sd, 0x4800, 0x04); 323 } 324 325 static int ov5647_sensor_power(struct v4l2_subdev *sd, int on) 326 { 327 int ret = 0; 328 struct ov5647 *ov5647 = to_state(sd); 329 struct i2c_client *client = v4l2_get_subdevdata(sd); 330 331 mutex_lock(&ov5647->lock); 332 333 if (on && !ov5647->power_count) { 334 dev_dbg(&client->dev, "OV5647 power on\n"); 335 336 ret = clk_prepare_enable(ov5647->xclk); 337 if (ret < 0) { 338 dev_err(&client->dev, "clk prepare enable failed\n"); 339 goto out; 340 } 341 342 ret = ov5647_write_array(sd, sensor_oe_enable_regs, 343 ARRAY_SIZE(sensor_oe_enable_regs)); 344 if (ret < 0) { 345 clk_disable_unprepare(ov5647->xclk); 346 dev_err(&client->dev, 347 "write sensor_oe_enable_regs error\n"); 348 goto out; 349 } 350 351 ret = __sensor_init(sd); 352 if (ret < 0) { 353 clk_disable_unprepare(ov5647->xclk); 354 dev_err(&client->dev, 355 "Camera not available, check Power\n"); 356 goto out; 357 } 358 } else if (!on && ov5647->power_count == 1) { 359 dev_dbg(&client->dev, "OV5647 power off\n"); 360 361 ret = ov5647_write_array(sd, sensor_oe_disable_regs, 362 ARRAY_SIZE(sensor_oe_disable_regs)); 363 364 if (ret < 0) 365 dev_dbg(&client->dev, "disable oe failed\n"); 366 367 ret = set_sw_standby(sd, true); 368 369 if (ret < 0) 370 dev_dbg(&client->dev, "soft stby failed\n"); 371 372 clk_disable_unprepare(ov5647->xclk); 373 } 374 375 /* Update the power count. */ 376 ov5647->power_count += on ? 1 : -1; 377 WARN_ON(ov5647->power_count < 0); 378 379 out: 380 mutex_unlock(&ov5647->lock); 381 382 return ret; 383 } 384 385 #ifdef CONFIG_VIDEO_ADV_DEBUG 386 static int ov5647_sensor_get_register(struct v4l2_subdev *sd, 387 struct v4l2_dbg_register *reg) 388 { 389 u8 val; 390 int ret; 391 392 ret = ov5647_read(sd, reg->reg & 0xff, &val); 393 if (ret < 0) 394 return ret; 395 396 reg->val = val; 397 reg->size = 1; 398 399 return 0; 400 } 401 402 static int ov5647_sensor_set_register(struct v4l2_subdev *sd, 403 const struct v4l2_dbg_register *reg) 404 { 405 return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff); 406 } 407 #endif 408 409 /** 410 * @short Subdev core operations registration 411 */ 412 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = { 413 .s_power = ov5647_sensor_power, 414 #ifdef CONFIG_VIDEO_ADV_DEBUG 415 .g_register = ov5647_sensor_get_register, 416 .s_register = ov5647_sensor_set_register, 417 #endif 418 }; 419 420 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable) 421 { 422 if (enable) 423 return ov5647_stream_on(sd); 424 else 425 return ov5647_stream_off(sd); 426 } 427 428 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = { 429 .s_stream = ov5647_s_stream, 430 }; 431 432 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd, 433 struct v4l2_subdev_pad_config *cfg, 434 struct v4l2_subdev_mbus_code_enum *code) 435 { 436 if (code->index > 0) 437 return -EINVAL; 438 439 code->code = MEDIA_BUS_FMT_SBGGR8_1X8; 440 441 return 0; 442 } 443 444 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = { 445 .enum_mbus_code = ov5647_enum_mbus_code, 446 }; 447 448 static const struct v4l2_subdev_ops ov5647_subdev_ops = { 449 .core = &ov5647_subdev_core_ops, 450 .video = &ov5647_subdev_video_ops, 451 .pad = &ov5647_subdev_pad_ops, 452 }; 453 454 static int ov5647_detect(struct v4l2_subdev *sd) 455 { 456 u8 read; 457 int ret; 458 struct i2c_client *client = v4l2_get_subdevdata(sd); 459 460 ret = ov5647_write(sd, OV5647_SW_RESET, 0x01); 461 if (ret < 0) 462 return ret; 463 464 ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read); 465 if (ret < 0) 466 return ret; 467 468 if (read != 0x56) { 469 dev_err(&client->dev, "ID High expected 0x56 got %x", read); 470 return -ENODEV; 471 } 472 473 ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read); 474 if (ret < 0) 475 return ret; 476 477 if (read != 0x47) { 478 dev_err(&client->dev, "ID Low expected 0x47 got %x", read); 479 return -ENODEV; 480 } 481 482 return ov5647_write(sd, OV5647_SW_RESET, 0x00); 483 } 484 485 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 486 { 487 struct v4l2_mbus_framefmt *format = 488 v4l2_subdev_get_try_format(sd, fh->pad, 0); 489 struct v4l2_rect *crop = 490 v4l2_subdev_get_try_crop(sd, fh->pad, 0); 491 492 crop->left = OV5647_COLUMN_START_DEF; 493 crop->top = OV5647_ROW_START_DEF; 494 crop->width = OV5647_WINDOW_WIDTH_DEF; 495 crop->height = OV5647_WINDOW_HEIGHT_DEF; 496 497 format->code = MEDIA_BUS_FMT_SBGGR8_1X8; 498 499 format->width = OV5647_WINDOW_WIDTH_DEF; 500 format->height = OV5647_WINDOW_HEIGHT_DEF; 501 format->field = V4L2_FIELD_NONE; 502 format->colorspace = V4L2_COLORSPACE_SRGB; 503 504 return 0; 505 } 506 507 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = { 508 .open = ov5647_open, 509 }; 510 511 static int ov5647_parse_dt(struct device_node *np) 512 { 513 struct v4l2_of_endpoint bus_cfg; 514 struct device_node *ep; 515 516 int ret; 517 518 ep = of_graph_get_next_endpoint(np, NULL); 519 if (!ep) 520 return -EINVAL; 521 522 ret = v4l2_of_parse_endpoint(ep, &bus_cfg); 523 524 of_node_put(ep); 525 return ret; 526 } 527 528 static int ov5647_probe(struct i2c_client *client, 529 const struct i2c_device_id *id) 530 { 531 struct device *dev = &client->dev; 532 struct ov5647 *sensor; 533 int ret; 534 struct v4l2_subdev *sd; 535 struct device_node *np = client->dev.of_node; 536 u32 xclk_freq; 537 538 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 539 if (!sensor) 540 return -ENOMEM; 541 542 if (IS_ENABLED(CONFIG_OF) && np) { 543 ret = ov5647_parse_dt(np); 544 if (ret) { 545 dev_err(dev, "DT parsing error: %d\n", ret); 546 return ret; 547 } 548 } 549 550 /* get system clock (xclk) */ 551 sensor->xclk = devm_clk_get(dev, NULL); 552 if (IS_ERR(sensor->xclk)) { 553 dev_err(dev, "could not get xclk"); 554 return PTR_ERR(sensor->xclk); 555 } 556 557 xclk_freq = clk_get_rate(sensor->xclk); 558 if (xclk_freq != 25000000) { 559 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq); 560 return -EINVAL; 561 } 562 563 mutex_init(&sensor->lock); 564 565 sd = &sensor->sd; 566 v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops); 567 sensor->sd.internal_ops = &ov5647_subdev_internal_ops; 568 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 569 570 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 571 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; 572 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad); 573 if (ret < 0) 574 goto mutex_remove; 575 576 ret = ov5647_detect(sd); 577 if (ret < 0) 578 goto error; 579 580 ret = v4l2_async_register_subdev(sd); 581 if (ret < 0) 582 goto error; 583 584 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n"); 585 return 0; 586 error: 587 media_entity_cleanup(&sd->entity); 588 mutex_remove: 589 mutex_destroy(&sensor->lock); 590 return ret; 591 } 592 593 static int ov5647_remove(struct i2c_client *client) 594 { 595 struct v4l2_subdev *sd = i2c_get_clientdata(client); 596 struct ov5647 *ov5647 = to_state(sd); 597 598 v4l2_async_unregister_subdev(&ov5647->sd); 599 media_entity_cleanup(&ov5647->sd.entity); 600 v4l2_device_unregister_subdev(sd); 601 mutex_destroy(&ov5647->lock); 602 603 return 0; 604 } 605 606 static const struct i2c_device_id ov5647_id[] = { 607 { "ov5647", 0 }, 608 { } 609 }; 610 MODULE_DEVICE_TABLE(i2c, ov5647_id); 611 612 #if IS_ENABLED(CONFIG_OF) 613 static const struct of_device_id ov5647_of_match[] = { 614 { .compatible = "ovti,ov5647" }, 615 { /* sentinel */ }, 616 }; 617 MODULE_DEVICE_TABLE(of, ov5647_of_match); 618 #endif 619 620 static struct i2c_driver ov5647_driver = { 621 .driver = { 622 .of_match_table = of_match_ptr(ov5647_of_match), 623 .name = SENSOR_NAME, 624 }, 625 .probe = ov5647_probe, 626 .remove = ov5647_remove, 627 .id_table = ov5647_id, 628 }; 629 630 module_i2c_driver(ov5647_driver); 631 632 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>"); 633 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors"); 634 MODULE_LICENSE("GPL v2"); 635