1 /* 2 * et8ek8_driver.c 3 * 4 * Copyright (C) 2008 Nokia Corporation 5 * 6 * Contact: Sakari Ailus <sakari.ailus@iki.fi> 7 * Tuukka Toivonen <tuukkat76@gmail.com> 8 * Pavel Machek <pavel@ucw.cz> 9 * 10 * Based on code from Toni Leinonen <toni.leinonen@offcode.fi>. 11 * 12 * This driver is based on the Micron MT9T012 camera imager driver 13 * (C) Texas Instruments. 14 * 15 * This program is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU General Public License 17 * version 2 as published by the Free Software Foundation. 18 * 19 * This program is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * General Public License for more details. 23 */ 24 25 #include <linux/clk.h> 26 #include <linux/delay.h> 27 #include <linux/gpio/consumer.h> 28 #include <linux/i2c.h> 29 #include <linux/kernel.h> 30 #include <linux/module.h> 31 #include <linux/mutex.h> 32 #include <linux/regulator/consumer.h> 33 #include <linux/slab.h> 34 #include <linux/sort.h> 35 #include <linux/v4l2-mediabus.h> 36 37 #include <media/media-entity.h> 38 #include <media/v4l2-ctrls.h> 39 #include <media/v4l2-device.h> 40 #include <media/v4l2-subdev.h> 41 42 #include "et8ek8_reg.h" 43 44 #define ET8EK8_NAME "et8ek8" 45 #define ET8EK8_PRIV_MEM_SIZE 128 46 #define ET8EK8_MAX_MSG 8 47 48 struct et8ek8_sensor { 49 struct v4l2_subdev subdev; 50 struct media_pad pad; 51 struct v4l2_mbus_framefmt format; 52 struct gpio_desc *reset; 53 struct regulator *vana; 54 struct clk *ext_clk; 55 u32 xclk_freq; 56 57 u16 version; 58 59 struct v4l2_ctrl_handler ctrl_handler; 60 struct v4l2_ctrl *exposure; 61 struct v4l2_ctrl *pixel_rate; 62 struct et8ek8_reglist *current_reglist; 63 64 u8 priv_mem[ET8EK8_PRIV_MEM_SIZE]; 65 66 struct mutex power_lock; 67 int power_count; 68 }; 69 70 #define to_et8ek8_sensor(sd) container_of(sd, struct et8ek8_sensor, subdev) 71 72 enum et8ek8_versions { 73 ET8EK8_REV_1 = 0x0001, 74 ET8EK8_REV_2, 75 }; 76 77 /* 78 * This table describes what should be written to the sensor register 79 * for each gain value. The gain(index in the table) is in terms of 80 * 0.1EV, i.e. 10 indexes in the table give 2 time more gain [0] in 81 * the *analog gain, [1] in the digital gain 82 * 83 * Analog gain [dB] = 20*log10(regvalue/32); 0x20..0x100 84 */ 85 static struct et8ek8_gain { 86 u16 analog; 87 u16 digital; 88 } const et8ek8_gain_table[] = { 89 { 32, 0}, /* x1 */ 90 { 34, 0}, 91 { 37, 0}, 92 { 39, 0}, 93 { 42, 0}, 94 { 45, 0}, 95 { 49, 0}, 96 { 52, 0}, 97 { 56, 0}, 98 { 60, 0}, 99 { 64, 0}, /* x2 */ 100 { 69, 0}, 101 { 74, 0}, 102 { 79, 0}, 103 { 84, 0}, 104 { 91, 0}, 105 { 97, 0}, 106 {104, 0}, 107 {111, 0}, 108 {119, 0}, 109 {128, 0}, /* x4 */ 110 {137, 0}, 111 {147, 0}, 112 {158, 0}, 113 {169, 0}, 114 {181, 0}, 115 {194, 0}, 116 {208, 0}, 117 {223, 0}, 118 {239, 0}, 119 {256, 0}, /* x8 */ 120 {256, 73}, 121 {256, 152}, 122 {256, 236}, 123 {256, 327}, 124 {256, 424}, 125 {256, 528}, 126 {256, 639}, 127 {256, 758}, 128 {256, 886}, 129 {256, 1023}, /* x16 */ 130 }; 131 132 /* Register definitions */ 133 #define REG_REVISION_NUMBER_L 0x1200 134 #define REG_REVISION_NUMBER_H 0x1201 135 136 #define PRIV_MEM_START_REG 0x0008 137 #define PRIV_MEM_WIN_SIZE 8 138 139 #define ET8EK8_I2C_DELAY 3 /* msec delay b/w accesses */ 140 141 #define USE_CRC 1 142 143 /* 144 * Register access helpers 145 * 146 * Read a 8/16/32-bit i2c register. The value is returned in 'val'. 147 * Returns zero if successful, or non-zero otherwise. 148 */ 149 static int et8ek8_i2c_read_reg(struct i2c_client *client, u16 data_length, 150 u16 reg, u32 *val) 151 { 152 int r; 153 struct i2c_msg msg; 154 unsigned char data[4]; 155 156 if (!client->adapter) 157 return -ENODEV; 158 if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT) 159 return -EINVAL; 160 161 msg.addr = client->addr; 162 msg.flags = 0; 163 msg.len = 2; 164 msg.buf = data; 165 166 /* high byte goes out first */ 167 data[0] = (u8) (reg >> 8); 168 data[1] = (u8) (reg & 0xff); 169 r = i2c_transfer(client->adapter, &msg, 1); 170 if (r < 0) 171 goto err; 172 173 msg.len = data_length; 174 msg.flags = I2C_M_RD; 175 r = i2c_transfer(client->adapter, &msg, 1); 176 if (r < 0) 177 goto err; 178 179 *val = 0; 180 /* high byte comes first */ 181 if (data_length == ET8EK8_REG_8BIT) 182 *val = data[0]; 183 else 184 *val = (data[1] << 8) + data[0]; 185 186 return 0; 187 188 err: 189 dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, r); 190 191 return r; 192 } 193 194 static void et8ek8_i2c_create_msg(struct i2c_client *client, u16 len, u16 reg, 195 u32 val, struct i2c_msg *msg, 196 unsigned char *buf) 197 { 198 msg->addr = client->addr; 199 msg->flags = 0; /* Write */ 200 msg->len = 2 + len; 201 msg->buf = buf; 202 203 /* high byte goes out first */ 204 buf[0] = (u8) (reg >> 8); 205 buf[1] = (u8) (reg & 0xff); 206 207 switch (len) { 208 case ET8EK8_REG_8BIT: 209 buf[2] = (u8) (val) & 0xff; 210 break; 211 case ET8EK8_REG_16BIT: 212 buf[2] = (u8) (val) & 0xff; 213 buf[3] = (u8) (val >> 8) & 0xff; 214 break; 215 default: 216 WARN_ONCE(1, ET8EK8_NAME ": %s: invalid message length.\n", 217 __func__); 218 } 219 } 220 221 /* 222 * A buffered write method that puts the wanted register write 223 * commands in smaller number of message lists and passes the lists to 224 * the i2c framework 225 */ 226 static int et8ek8_i2c_buffered_write_regs(struct i2c_client *client, 227 const struct et8ek8_reg *wnext, 228 int cnt) 229 { 230 struct i2c_msg msg[ET8EK8_MAX_MSG]; 231 unsigned char data[ET8EK8_MAX_MSG][6]; 232 int wcnt = 0; 233 u16 reg, data_length; 234 u32 val; 235 int rval; 236 237 /* Create new write messages for all writes */ 238 while (wcnt < cnt) { 239 data_length = wnext->type; 240 reg = wnext->reg; 241 val = wnext->val; 242 wnext++; 243 244 et8ek8_i2c_create_msg(client, data_length, reg, 245 val, &msg[wcnt], &data[wcnt][0]); 246 247 /* Update write count */ 248 wcnt++; 249 250 if (wcnt < ET8EK8_MAX_MSG) 251 continue; 252 253 rval = i2c_transfer(client->adapter, msg, wcnt); 254 if (rval < 0) 255 return rval; 256 257 cnt -= wcnt; 258 wcnt = 0; 259 } 260 261 rval = i2c_transfer(client->adapter, msg, wcnt); 262 263 return rval < 0 ? rval : 0; 264 } 265 266 /* 267 * Write a list of registers to i2c device. 268 * 269 * The list of registers is terminated by ET8EK8_REG_TERM. 270 * Returns zero if successful, or non-zero otherwise. 271 */ 272 static int et8ek8_i2c_write_regs(struct i2c_client *client, 273 const struct et8ek8_reg *regs) 274 { 275 int r, cnt = 0; 276 const struct et8ek8_reg *next; 277 278 if (!client->adapter) 279 return -ENODEV; 280 281 if (!regs) 282 return -EINVAL; 283 284 /* Initialize list pointers to the start of the list */ 285 next = regs; 286 287 do { 288 /* 289 * We have to go through the list to figure out how 290 * many regular writes we have in a row 291 */ 292 while (next->type != ET8EK8_REG_TERM && 293 next->type != ET8EK8_REG_DELAY) { 294 /* 295 * Here we check that the actual length fields 296 * are valid 297 */ 298 if (WARN(next->type != ET8EK8_REG_8BIT && 299 next->type != ET8EK8_REG_16BIT, 300 "Invalid type = %d", next->type)) { 301 return -EINVAL; 302 } 303 /* 304 * Increment count of successive writes and 305 * read pointer 306 */ 307 cnt++; 308 next++; 309 } 310 311 /* Now we start writing ... */ 312 r = et8ek8_i2c_buffered_write_regs(client, regs, cnt); 313 314 /* ... and then check that everything was OK */ 315 if (r < 0) { 316 dev_err(&client->dev, "i2c transfer error!\n"); 317 return r; 318 } 319 320 /* 321 * If we ran into a sleep statement when going through 322 * the list, this is where we snooze for the required time 323 */ 324 if (next->type == ET8EK8_REG_DELAY) { 325 msleep(next->val); 326 /* 327 * ZZZ ... 328 * Update list pointers and cnt and start over ... 329 */ 330 next++; 331 regs = next; 332 cnt = 0; 333 } 334 } while (next->type != ET8EK8_REG_TERM); 335 336 return 0; 337 } 338 339 /* 340 * Write to a 8/16-bit register. 341 * Returns zero if successful, or non-zero otherwise. 342 */ 343 static int et8ek8_i2c_write_reg(struct i2c_client *client, u16 data_length, 344 u16 reg, u32 val) 345 { 346 int r; 347 struct i2c_msg msg; 348 unsigned char data[6]; 349 350 if (!client->adapter) 351 return -ENODEV; 352 if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT) 353 return -EINVAL; 354 355 et8ek8_i2c_create_msg(client, data_length, reg, val, &msg, data); 356 357 r = i2c_transfer(client->adapter, &msg, 1); 358 if (r < 0) { 359 dev_err(&client->dev, 360 "wrote 0x%x to offset 0x%x error %d\n", val, reg, r); 361 return r; 362 } 363 364 return 0; 365 } 366 367 static struct et8ek8_reglist *et8ek8_reglist_find_type( 368 struct et8ek8_meta_reglist *meta, 369 u16 type) 370 { 371 struct et8ek8_reglist **next = &meta->reglist[0].ptr; 372 373 while (*next) { 374 if ((*next)->type == type) 375 return *next; 376 377 next++; 378 } 379 380 return NULL; 381 } 382 383 static int et8ek8_i2c_reglist_find_write(struct i2c_client *client, 384 struct et8ek8_meta_reglist *meta, 385 u16 type) 386 { 387 struct et8ek8_reglist *reglist; 388 389 reglist = et8ek8_reglist_find_type(meta, type); 390 if (!reglist) 391 return -EINVAL; 392 393 return et8ek8_i2c_write_regs(client, reglist->regs); 394 } 395 396 static struct et8ek8_reglist **et8ek8_reglist_first( 397 struct et8ek8_meta_reglist *meta) 398 { 399 return &meta->reglist[0].ptr; 400 } 401 402 static void et8ek8_reglist_to_mbus(const struct et8ek8_reglist *reglist, 403 struct v4l2_mbus_framefmt *fmt) 404 { 405 fmt->width = reglist->mode.window_width; 406 fmt->height = reglist->mode.window_height; 407 fmt->code = reglist->mode.bus_format; 408 } 409 410 static struct et8ek8_reglist *et8ek8_reglist_find_mode_fmt( 411 struct et8ek8_meta_reglist *meta, 412 struct v4l2_mbus_framefmt *fmt) 413 { 414 struct et8ek8_reglist **list = et8ek8_reglist_first(meta); 415 struct et8ek8_reglist *best_match = NULL; 416 struct et8ek8_reglist *best_other = NULL; 417 struct v4l2_mbus_framefmt format; 418 unsigned int max_dist_match = (unsigned int)-1; 419 unsigned int max_dist_other = (unsigned int)-1; 420 421 /* 422 * Find the mode with the closest image size. The distance between 423 * image sizes is the size in pixels of the non-overlapping regions 424 * between the requested size and the frame-specified size. 425 * 426 * Store both the closest mode that matches the requested format, and 427 * the closest mode for all other formats. The best match is returned 428 * if found, otherwise the best mode with a non-matching format is 429 * returned. 430 */ 431 for (; *list; list++) { 432 unsigned int dist; 433 434 if ((*list)->type != ET8EK8_REGLIST_MODE) 435 continue; 436 437 et8ek8_reglist_to_mbus(*list, &format); 438 439 dist = min(fmt->width, format.width) 440 * min(fmt->height, format.height); 441 dist = format.width * format.height 442 + fmt->width * fmt->height - 2 * dist; 443 444 445 if (fmt->code == format.code) { 446 if (dist < max_dist_match || !best_match) { 447 best_match = *list; 448 max_dist_match = dist; 449 } 450 } else { 451 if (dist < max_dist_other || !best_other) { 452 best_other = *list; 453 max_dist_other = dist; 454 } 455 } 456 } 457 458 return best_match ? best_match : best_other; 459 } 460 461 #define TIMEPERFRAME_AVG_FPS(t) \ 462 (((t).denominator + ((t).numerator >> 1)) / (t).numerator) 463 464 static struct et8ek8_reglist *et8ek8_reglist_find_mode_ival( 465 struct et8ek8_meta_reglist *meta, 466 struct et8ek8_reglist *current_reglist, 467 struct v4l2_fract *timeperframe) 468 { 469 int fps = TIMEPERFRAME_AVG_FPS(*timeperframe); 470 struct et8ek8_reglist **list = et8ek8_reglist_first(meta); 471 struct et8ek8_mode *current_mode = ¤t_reglist->mode; 472 473 for (; *list; list++) { 474 struct et8ek8_mode *mode = &(*list)->mode; 475 476 if ((*list)->type != ET8EK8_REGLIST_MODE) 477 continue; 478 479 if (mode->window_width != current_mode->window_width || 480 mode->window_height != current_mode->window_height) 481 continue; 482 483 if (TIMEPERFRAME_AVG_FPS(mode->timeperframe) == fps) 484 return *list; 485 } 486 487 return NULL; 488 } 489 490 static int et8ek8_reglist_cmp(const void *a, const void *b) 491 { 492 const struct et8ek8_reglist **list1 = (const struct et8ek8_reglist **)a, 493 **list2 = (const struct et8ek8_reglist **)b; 494 495 /* Put real modes in the beginning. */ 496 if ((*list1)->type == ET8EK8_REGLIST_MODE && 497 (*list2)->type != ET8EK8_REGLIST_MODE) 498 return -1; 499 if ((*list1)->type != ET8EK8_REGLIST_MODE && 500 (*list2)->type == ET8EK8_REGLIST_MODE) 501 return 1; 502 503 /* Descending width. */ 504 if ((*list1)->mode.window_width > (*list2)->mode.window_width) 505 return -1; 506 if ((*list1)->mode.window_width < (*list2)->mode.window_width) 507 return 1; 508 509 if ((*list1)->mode.window_height > (*list2)->mode.window_height) 510 return -1; 511 if ((*list1)->mode.window_height < (*list2)->mode.window_height) 512 return 1; 513 514 return 0; 515 } 516 517 static int et8ek8_reglist_import(struct i2c_client *client, 518 struct et8ek8_meta_reglist *meta) 519 { 520 int nlists = 0, i; 521 522 dev_info(&client->dev, "meta_reglist version %s\n", meta->version); 523 524 while (meta->reglist[nlists].ptr) 525 nlists++; 526 527 if (!nlists) 528 return -EINVAL; 529 530 sort(&meta->reglist[0].ptr, nlists, sizeof(meta->reglist[0].ptr), 531 et8ek8_reglist_cmp, NULL); 532 533 i = nlists; 534 nlists = 0; 535 536 while (i--) { 537 struct et8ek8_reglist *list; 538 539 list = meta->reglist[nlists].ptr; 540 541 dev_dbg(&client->dev, 542 "%s: type %d\tw %d\th %d\tfmt %x\tival %d/%d\tptr %p\n", 543 __func__, 544 list->type, 545 list->mode.window_width, list->mode.window_height, 546 list->mode.bus_format, 547 list->mode.timeperframe.numerator, 548 list->mode.timeperframe.denominator, 549 (void *)meta->reglist[nlists].ptr); 550 551 nlists++; 552 } 553 554 return 0; 555 } 556 557 /* Called to change the V4L2 gain control value. This function 558 * rounds and clamps the given value and updates the V4L2 control value. 559 * If power is on, also updates the sensor analog and digital gains. 560 * gain is in 0.1 EV (exposure value) units. 561 */ 562 static int et8ek8_set_gain(struct et8ek8_sensor *sensor, s32 gain) 563 { 564 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); 565 struct et8ek8_gain new; 566 int r; 567 568 new = et8ek8_gain_table[gain]; 569 570 /* FIXME: optimise I2C writes! */ 571 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 572 0x124a, new.analog >> 8); 573 if (r) 574 return r; 575 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 576 0x1249, new.analog & 0xff); 577 if (r) 578 return r; 579 580 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 581 0x124d, new.digital >> 8); 582 if (r) 583 return r; 584 r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 585 0x124c, new.digital & 0xff); 586 587 return r; 588 } 589 590 static int et8ek8_set_test_pattern(struct et8ek8_sensor *sensor, s32 mode) 591 { 592 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); 593 int cbh_mode, cbv_mode, tp_mode, din_sw, r1420, rval; 594 595 /* Values for normal mode */ 596 cbh_mode = 0; 597 cbv_mode = 0; 598 tp_mode = 0; 599 din_sw = 0x00; 600 r1420 = 0xF0; 601 602 if (mode) { 603 /* Test pattern mode */ 604 if (mode < 5) { 605 cbh_mode = 1; 606 cbv_mode = 1; 607 tp_mode = mode + 3; 608 } else { 609 cbh_mode = 0; 610 cbv_mode = 0; 611 tp_mode = mode - 4 + 3; 612 } 613 614 din_sw = 0x01; 615 r1420 = 0xE0; 616 } 617 618 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x111B, 619 tp_mode << 4); 620 if (rval) 621 return rval; 622 623 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1121, 624 cbh_mode << 7); 625 if (rval) 626 return rval; 627 628 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1124, 629 cbv_mode << 7); 630 if (rval) 631 return rval; 632 633 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x112C, din_sw); 634 if (rval) 635 return rval; 636 637 return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1420, r1420); 638 } 639 640 /* ----------------------------------------------------------------------------- 641 * V4L2 controls 642 */ 643 644 static int et8ek8_set_ctrl(struct v4l2_ctrl *ctrl) 645 { 646 struct et8ek8_sensor *sensor = 647 container_of(ctrl->handler, struct et8ek8_sensor, ctrl_handler); 648 649 switch (ctrl->id) { 650 case V4L2_CID_GAIN: 651 return et8ek8_set_gain(sensor, ctrl->val); 652 653 case V4L2_CID_EXPOSURE: 654 { 655 struct i2c_client *client = 656 v4l2_get_subdevdata(&sensor->subdev); 657 658 return et8ek8_i2c_write_reg(client, ET8EK8_REG_16BIT, 0x1243, 659 ctrl->val); 660 } 661 662 case V4L2_CID_TEST_PATTERN: 663 return et8ek8_set_test_pattern(sensor, ctrl->val); 664 665 case V4L2_CID_PIXEL_RATE: 666 return 0; 667 668 default: 669 return -EINVAL; 670 } 671 } 672 673 static const struct v4l2_ctrl_ops et8ek8_ctrl_ops = { 674 .s_ctrl = et8ek8_set_ctrl, 675 }; 676 677 static const char * const et8ek8_test_pattern_menu[] = { 678 "Normal", 679 "Vertical colorbar", 680 "Horizontal colorbar", 681 "Scale", 682 "Ramp", 683 "Small vertical colorbar", 684 "Small horizontal colorbar", 685 "Small scale", 686 "Small ramp", 687 }; 688 689 static int et8ek8_init_controls(struct et8ek8_sensor *sensor) 690 { 691 s32 max_rows; 692 693 v4l2_ctrl_handler_init(&sensor->ctrl_handler, 4); 694 695 /* V4L2_CID_GAIN */ 696 v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops, 697 V4L2_CID_GAIN, 0, ARRAY_SIZE(et8ek8_gain_table) - 1, 698 1, 0); 699 700 max_rows = sensor->current_reglist->mode.max_exp; 701 { 702 u32 min = 1, max = max_rows; 703 704 sensor->exposure = 705 v4l2_ctrl_new_std(&sensor->ctrl_handler, 706 &et8ek8_ctrl_ops, V4L2_CID_EXPOSURE, 707 min, max, min, max); 708 } 709 710 /* V4L2_CID_PIXEL_RATE */ 711 sensor->pixel_rate = 712 v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops, 713 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1); 714 715 /* V4L2_CID_TEST_PATTERN */ 716 v4l2_ctrl_new_std_menu_items(&sensor->ctrl_handler, 717 &et8ek8_ctrl_ops, V4L2_CID_TEST_PATTERN, 718 ARRAY_SIZE(et8ek8_test_pattern_menu) - 1, 719 0, 0, et8ek8_test_pattern_menu); 720 721 if (sensor->ctrl_handler.error) 722 return sensor->ctrl_handler.error; 723 724 sensor->subdev.ctrl_handler = &sensor->ctrl_handler; 725 726 return 0; 727 } 728 729 static void et8ek8_update_controls(struct et8ek8_sensor *sensor) 730 { 731 struct v4l2_ctrl *ctrl; 732 struct et8ek8_mode *mode = &sensor->current_reglist->mode; 733 734 u32 min, max, pixel_rate; 735 static const int S = 8; 736 737 ctrl = sensor->exposure; 738 739 min = 1; 740 max = mode->max_exp; 741 742 /* 743 * Calculate average pixel clock per line. Assume buffers can spread 744 * the data over horizontal blanking time. Rounding upwards. 745 * Formula taken from stock Nokia N900 kernel. 746 */ 747 pixel_rate = ((mode->pixel_clock + (1 << S) - 1) >> S) + mode->width; 748 pixel_rate = mode->window_width * (pixel_rate - 1) / mode->width; 749 750 __v4l2_ctrl_modify_range(ctrl, min, max, min, max); 751 __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate, pixel_rate << S); 752 } 753 754 static int et8ek8_configure(struct et8ek8_sensor *sensor) 755 { 756 struct v4l2_subdev *subdev = &sensor->subdev; 757 struct i2c_client *client = v4l2_get_subdevdata(subdev); 758 int rval; 759 760 rval = et8ek8_i2c_write_regs(client, sensor->current_reglist->regs); 761 if (rval) 762 goto fail; 763 764 /* Controls set while the power to the sensor is turned off are saved 765 * but not applied to the hardware. Now that we're about to start 766 * streaming apply all the current values to the hardware. 767 */ 768 rval = v4l2_ctrl_handler_setup(&sensor->ctrl_handler); 769 if (rval) 770 goto fail; 771 772 return 0; 773 774 fail: 775 dev_err(&client->dev, "sensor configuration failed\n"); 776 777 return rval; 778 } 779 780 static int et8ek8_stream_on(struct et8ek8_sensor *sensor) 781 { 782 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); 783 784 return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0xb0); 785 } 786 787 static int et8ek8_stream_off(struct et8ek8_sensor *sensor) 788 { 789 struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev); 790 791 return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0x30); 792 } 793 794 static int et8ek8_s_stream(struct v4l2_subdev *subdev, int streaming) 795 { 796 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 797 int ret; 798 799 if (!streaming) 800 return et8ek8_stream_off(sensor); 801 802 ret = et8ek8_configure(sensor); 803 if (ret < 0) 804 return ret; 805 806 return et8ek8_stream_on(sensor); 807 } 808 809 /* -------------------------------------------------------------------------- 810 * V4L2 subdev operations 811 */ 812 813 static int et8ek8_power_off(struct et8ek8_sensor *sensor) 814 { 815 gpiod_set_value(sensor->reset, 0); 816 udelay(1); 817 818 clk_disable_unprepare(sensor->ext_clk); 819 820 return regulator_disable(sensor->vana); 821 } 822 823 static int et8ek8_power_on(struct et8ek8_sensor *sensor) 824 { 825 struct v4l2_subdev *subdev = &sensor->subdev; 826 struct i2c_client *client = v4l2_get_subdevdata(subdev); 827 unsigned int xclk_freq; 828 int val, rval; 829 830 rval = regulator_enable(sensor->vana); 831 if (rval) { 832 dev_err(&client->dev, "failed to enable vana regulator\n"); 833 return rval; 834 } 835 836 if (sensor->current_reglist) 837 xclk_freq = sensor->current_reglist->mode.ext_clock; 838 else 839 xclk_freq = sensor->xclk_freq; 840 841 rval = clk_set_rate(sensor->ext_clk, xclk_freq); 842 if (rval < 0) { 843 dev_err(&client->dev, "unable to set extclk clock freq to %u\n", 844 xclk_freq); 845 goto out; 846 } 847 rval = clk_prepare_enable(sensor->ext_clk); 848 if (rval < 0) { 849 dev_err(&client->dev, "failed to enable extclk\n"); 850 goto out; 851 } 852 853 if (rval) 854 goto out; 855 856 udelay(10); /* I wish this is a good value */ 857 858 gpiod_set_value(sensor->reset, 1); 859 860 msleep(5000 * 1000 / xclk_freq + 1); /* Wait 5000 cycles */ 861 862 rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist, 863 ET8EK8_REGLIST_POWERON); 864 if (rval) 865 goto out; 866 867 #ifdef USE_CRC 868 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 0x1263, &val); 869 if (rval) 870 goto out; 871 #if USE_CRC /* TODO get crc setting from DT */ 872 val |= BIT(4); 873 #else 874 val &= ~BIT(4); 875 #endif 876 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1263, val); 877 if (rval) 878 goto out; 879 #endif 880 881 out: 882 if (rval) 883 et8ek8_power_off(sensor); 884 885 return rval; 886 } 887 888 /* -------------------------------------------------------------------------- 889 * V4L2 subdev video operations 890 */ 891 #define MAX_FMTS 4 892 static int et8ek8_enum_mbus_code(struct v4l2_subdev *subdev, 893 struct v4l2_subdev_pad_config *cfg, 894 struct v4l2_subdev_mbus_code_enum *code) 895 { 896 struct et8ek8_reglist **list = 897 et8ek8_reglist_first(&meta_reglist); 898 u32 pixelformat[MAX_FMTS]; 899 int npixelformat = 0; 900 901 if (code->index >= MAX_FMTS) 902 return -EINVAL; 903 904 for (; *list; list++) { 905 struct et8ek8_mode *mode = &(*list)->mode; 906 int i; 907 908 if ((*list)->type != ET8EK8_REGLIST_MODE) 909 continue; 910 911 for (i = 0; i < npixelformat; i++) { 912 if (pixelformat[i] == mode->bus_format) 913 break; 914 } 915 if (i != npixelformat) 916 continue; 917 918 if (code->index == npixelformat) { 919 code->code = mode->bus_format; 920 return 0; 921 } 922 923 pixelformat[npixelformat] = mode->bus_format; 924 npixelformat++; 925 } 926 927 return -EINVAL; 928 } 929 930 static int et8ek8_enum_frame_size(struct v4l2_subdev *subdev, 931 struct v4l2_subdev_pad_config *cfg, 932 struct v4l2_subdev_frame_size_enum *fse) 933 { 934 struct et8ek8_reglist **list = 935 et8ek8_reglist_first(&meta_reglist); 936 struct v4l2_mbus_framefmt format; 937 int cmp_width = INT_MAX; 938 int cmp_height = INT_MAX; 939 int index = fse->index; 940 941 for (; *list; list++) { 942 if ((*list)->type != ET8EK8_REGLIST_MODE) 943 continue; 944 945 et8ek8_reglist_to_mbus(*list, &format); 946 if (fse->code != format.code) 947 continue; 948 949 /* Assume that the modes are grouped by frame size. */ 950 if (format.width == cmp_width && format.height == cmp_height) 951 continue; 952 953 cmp_width = format.width; 954 cmp_height = format.height; 955 956 if (index-- == 0) { 957 fse->min_width = format.width; 958 fse->min_height = format.height; 959 fse->max_width = format.width; 960 fse->max_height = format.height; 961 return 0; 962 } 963 } 964 965 return -EINVAL; 966 } 967 968 static int et8ek8_enum_frame_ival(struct v4l2_subdev *subdev, 969 struct v4l2_subdev_pad_config *cfg, 970 struct v4l2_subdev_frame_interval_enum *fie) 971 { 972 struct et8ek8_reglist **list = 973 et8ek8_reglist_first(&meta_reglist); 974 struct v4l2_mbus_framefmt format; 975 int index = fie->index; 976 977 for (; *list; list++) { 978 struct et8ek8_mode *mode = &(*list)->mode; 979 980 if ((*list)->type != ET8EK8_REGLIST_MODE) 981 continue; 982 983 et8ek8_reglist_to_mbus(*list, &format); 984 if (fie->code != format.code) 985 continue; 986 987 if (fie->width != format.width || fie->height != format.height) 988 continue; 989 990 if (index-- == 0) { 991 fie->interval = mode->timeperframe; 992 return 0; 993 } 994 } 995 996 return -EINVAL; 997 } 998 999 static struct v4l2_mbus_framefmt * 1000 __et8ek8_get_pad_format(struct et8ek8_sensor *sensor, 1001 struct v4l2_subdev_pad_config *cfg, 1002 unsigned int pad, enum v4l2_subdev_format_whence which) 1003 { 1004 switch (which) { 1005 case V4L2_SUBDEV_FORMAT_TRY: 1006 return v4l2_subdev_get_try_format(&sensor->subdev, cfg, pad); 1007 case V4L2_SUBDEV_FORMAT_ACTIVE: 1008 return &sensor->format; 1009 default: 1010 return NULL; 1011 } 1012 } 1013 1014 static int et8ek8_get_pad_format(struct v4l2_subdev *subdev, 1015 struct v4l2_subdev_pad_config *cfg, 1016 struct v4l2_subdev_format *fmt) 1017 { 1018 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1019 struct v4l2_mbus_framefmt *format; 1020 1021 format = __et8ek8_get_pad_format(sensor, cfg, fmt->pad, fmt->which); 1022 if (!format) 1023 return -EINVAL; 1024 1025 fmt->format = *format; 1026 1027 return 0; 1028 } 1029 1030 static int et8ek8_set_pad_format(struct v4l2_subdev *subdev, 1031 struct v4l2_subdev_pad_config *cfg, 1032 struct v4l2_subdev_format *fmt) 1033 { 1034 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1035 struct v4l2_mbus_framefmt *format; 1036 struct et8ek8_reglist *reglist; 1037 1038 format = __et8ek8_get_pad_format(sensor, cfg, fmt->pad, fmt->which); 1039 if (!format) 1040 return -EINVAL; 1041 1042 reglist = et8ek8_reglist_find_mode_fmt(&meta_reglist, &fmt->format); 1043 et8ek8_reglist_to_mbus(reglist, &fmt->format); 1044 *format = fmt->format; 1045 1046 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 1047 sensor->current_reglist = reglist; 1048 et8ek8_update_controls(sensor); 1049 } 1050 1051 return 0; 1052 } 1053 1054 static int et8ek8_get_frame_interval(struct v4l2_subdev *subdev, 1055 struct v4l2_subdev_frame_interval *fi) 1056 { 1057 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1058 1059 memset(fi, 0, sizeof(*fi)); 1060 fi->interval = sensor->current_reglist->mode.timeperframe; 1061 1062 return 0; 1063 } 1064 1065 static int et8ek8_set_frame_interval(struct v4l2_subdev *subdev, 1066 struct v4l2_subdev_frame_interval *fi) 1067 { 1068 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1069 struct et8ek8_reglist *reglist; 1070 1071 reglist = et8ek8_reglist_find_mode_ival(&meta_reglist, 1072 sensor->current_reglist, 1073 &fi->interval); 1074 1075 if (!reglist) 1076 return -EINVAL; 1077 1078 if (sensor->current_reglist->mode.ext_clock != reglist->mode.ext_clock) 1079 return -EINVAL; 1080 1081 sensor->current_reglist = reglist; 1082 et8ek8_update_controls(sensor); 1083 1084 return 0; 1085 } 1086 1087 static int et8ek8_g_priv_mem(struct v4l2_subdev *subdev) 1088 { 1089 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1090 struct i2c_client *client = v4l2_get_subdevdata(subdev); 1091 unsigned int length = ET8EK8_PRIV_MEM_SIZE; 1092 unsigned int offset = 0; 1093 u8 *ptr = sensor->priv_mem; 1094 int rval = 0; 1095 1096 /* Read the EEPROM window-by-window, each window 8 bytes */ 1097 do { 1098 u8 buffer[PRIV_MEM_WIN_SIZE]; 1099 struct i2c_msg msg; 1100 int bytes, i; 1101 int ofs; 1102 1103 /* Set the current window */ 1104 rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x0001, 1105 0xe0 | (offset >> 3)); 1106 if (rval < 0) 1107 return rval; 1108 1109 /* Wait for status bit */ 1110 for (i = 0; i < 1000; ++i) { 1111 u32 status; 1112 1113 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 1114 0x0003, &status); 1115 if (rval < 0) 1116 return rval; 1117 if (!(status & 0x08)) 1118 break; 1119 usleep_range(1000, 2000); 1120 } 1121 1122 if (i == 1000) 1123 return -EIO; 1124 1125 /* Read window, 8 bytes at once, and copy to user space */ 1126 ofs = offset & 0x07; /* Offset within this window */ 1127 bytes = length + ofs > 8 ? 8-ofs : length; 1128 msg.addr = client->addr; 1129 msg.flags = 0; 1130 msg.len = 2; 1131 msg.buf = buffer; 1132 ofs += PRIV_MEM_START_REG; 1133 buffer[0] = (u8)(ofs >> 8); 1134 buffer[1] = (u8)(ofs & 0xFF); 1135 1136 rval = i2c_transfer(client->adapter, &msg, 1); 1137 if (rval < 0) 1138 return rval; 1139 1140 mdelay(ET8EK8_I2C_DELAY); 1141 msg.addr = client->addr; 1142 msg.len = bytes; 1143 msg.flags = I2C_M_RD; 1144 msg.buf = buffer; 1145 memset(buffer, 0, sizeof(buffer)); 1146 1147 rval = i2c_transfer(client->adapter, &msg, 1); 1148 if (rval < 0) 1149 return rval; 1150 1151 rval = 0; 1152 memcpy(ptr, buffer, bytes); 1153 1154 length -= bytes; 1155 offset += bytes; 1156 ptr += bytes; 1157 } while (length > 0); 1158 1159 return rval; 1160 } 1161 1162 static int et8ek8_dev_init(struct v4l2_subdev *subdev) 1163 { 1164 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1165 struct i2c_client *client = v4l2_get_subdevdata(subdev); 1166 int rval, rev_l, rev_h; 1167 1168 rval = et8ek8_power_on(sensor); 1169 if (rval) { 1170 dev_err(&client->dev, "could not power on\n"); 1171 return rval; 1172 } 1173 1174 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 1175 REG_REVISION_NUMBER_L, &rev_l); 1176 if (!rval) 1177 rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 1178 REG_REVISION_NUMBER_H, &rev_h); 1179 if (rval) { 1180 dev_err(&client->dev, "no et8ek8 sensor detected\n"); 1181 goto out_poweroff; 1182 } 1183 1184 sensor->version = (rev_h << 8) + rev_l; 1185 if (sensor->version != ET8EK8_REV_1 && sensor->version != ET8EK8_REV_2) 1186 dev_info(&client->dev, 1187 "unknown version 0x%x detected, continuing anyway\n", 1188 sensor->version); 1189 1190 rval = et8ek8_reglist_import(client, &meta_reglist); 1191 if (rval) { 1192 dev_err(&client->dev, 1193 "invalid register list %s, import failed\n", 1194 ET8EK8_NAME); 1195 goto out_poweroff; 1196 } 1197 1198 sensor->current_reglist = et8ek8_reglist_find_type(&meta_reglist, 1199 ET8EK8_REGLIST_MODE); 1200 if (!sensor->current_reglist) { 1201 dev_err(&client->dev, 1202 "invalid register list %s, no mode found\n", 1203 ET8EK8_NAME); 1204 rval = -ENODEV; 1205 goto out_poweroff; 1206 } 1207 1208 et8ek8_reglist_to_mbus(sensor->current_reglist, &sensor->format); 1209 1210 rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist, 1211 ET8EK8_REGLIST_POWERON); 1212 if (rval) { 1213 dev_err(&client->dev, 1214 "invalid register list %s, no POWERON mode found\n", 1215 ET8EK8_NAME); 1216 goto out_poweroff; 1217 } 1218 rval = et8ek8_stream_on(sensor); /* Needed to be able to read EEPROM */ 1219 if (rval) 1220 goto out_poweroff; 1221 rval = et8ek8_g_priv_mem(subdev); 1222 if (rval) 1223 dev_warn(&client->dev, 1224 "can not read OTP (EEPROM) memory from sensor\n"); 1225 rval = et8ek8_stream_off(sensor); 1226 if (rval) 1227 goto out_poweroff; 1228 1229 rval = et8ek8_power_off(sensor); 1230 if (rval) 1231 goto out_poweroff; 1232 1233 return 0; 1234 1235 out_poweroff: 1236 et8ek8_power_off(sensor); 1237 1238 return rval; 1239 } 1240 1241 /* -------------------------------------------------------------------------- 1242 * sysfs attributes 1243 */ 1244 static ssize_t 1245 et8ek8_priv_mem_read(struct device *dev, struct device_attribute *attr, 1246 char *buf) 1247 { 1248 struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev)); 1249 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1250 1251 #if PAGE_SIZE < ET8EK8_PRIV_MEM_SIZE 1252 #error PAGE_SIZE too small! 1253 #endif 1254 1255 memcpy(buf, sensor->priv_mem, ET8EK8_PRIV_MEM_SIZE); 1256 1257 return ET8EK8_PRIV_MEM_SIZE; 1258 } 1259 static DEVICE_ATTR(priv_mem, 0444, et8ek8_priv_mem_read, NULL); 1260 1261 /* -------------------------------------------------------------------------- 1262 * V4L2 subdev core operations 1263 */ 1264 1265 static int 1266 et8ek8_registered(struct v4l2_subdev *subdev) 1267 { 1268 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1269 struct i2c_client *client = v4l2_get_subdevdata(subdev); 1270 int rval; 1271 1272 dev_dbg(&client->dev, "registered!"); 1273 1274 rval = device_create_file(&client->dev, &dev_attr_priv_mem); 1275 if (rval) { 1276 dev_err(&client->dev, "could not register sysfs entry\n"); 1277 return rval; 1278 } 1279 1280 rval = et8ek8_dev_init(subdev); 1281 if (rval) 1282 goto err_file; 1283 1284 rval = et8ek8_init_controls(sensor); 1285 if (rval) { 1286 dev_err(&client->dev, "controls initialization failed\n"); 1287 goto err_file; 1288 } 1289 1290 __et8ek8_get_pad_format(sensor, NULL, 0, V4L2_SUBDEV_FORMAT_ACTIVE); 1291 1292 return 0; 1293 1294 err_file: 1295 device_remove_file(&client->dev, &dev_attr_priv_mem); 1296 1297 return rval; 1298 } 1299 1300 static int __et8ek8_set_power(struct et8ek8_sensor *sensor, bool on) 1301 { 1302 return on ? et8ek8_power_on(sensor) : et8ek8_power_off(sensor); 1303 } 1304 1305 static int et8ek8_set_power(struct v4l2_subdev *subdev, int on) 1306 { 1307 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1308 int ret = 0; 1309 1310 mutex_lock(&sensor->power_lock); 1311 1312 /* If the power count is modified from 0 to != 0 or from != 0 to 0, 1313 * update the power state. 1314 */ 1315 if (sensor->power_count == !on) { 1316 ret = __et8ek8_set_power(sensor, !!on); 1317 if (ret < 0) 1318 goto done; 1319 } 1320 1321 /* Update the power count. */ 1322 sensor->power_count += on ? 1 : -1; 1323 WARN_ON(sensor->power_count < 0); 1324 1325 done: 1326 mutex_unlock(&sensor->power_lock); 1327 1328 return ret; 1329 } 1330 1331 static int et8ek8_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 1332 { 1333 struct et8ek8_sensor *sensor = to_et8ek8_sensor(sd); 1334 struct v4l2_mbus_framefmt *format; 1335 struct et8ek8_reglist *reglist; 1336 1337 reglist = et8ek8_reglist_find_type(&meta_reglist, ET8EK8_REGLIST_MODE); 1338 format = __et8ek8_get_pad_format(sensor, fh->pad, 0, 1339 V4L2_SUBDEV_FORMAT_TRY); 1340 et8ek8_reglist_to_mbus(reglist, format); 1341 1342 return et8ek8_set_power(sd, true); 1343 } 1344 1345 static int et8ek8_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) 1346 { 1347 return et8ek8_set_power(sd, false); 1348 } 1349 1350 static const struct v4l2_subdev_video_ops et8ek8_video_ops = { 1351 .s_stream = et8ek8_s_stream, 1352 .g_frame_interval = et8ek8_get_frame_interval, 1353 .s_frame_interval = et8ek8_set_frame_interval, 1354 }; 1355 1356 static const struct v4l2_subdev_core_ops et8ek8_core_ops = { 1357 .s_power = et8ek8_set_power, 1358 }; 1359 1360 static const struct v4l2_subdev_pad_ops et8ek8_pad_ops = { 1361 .enum_mbus_code = et8ek8_enum_mbus_code, 1362 .enum_frame_size = et8ek8_enum_frame_size, 1363 .enum_frame_interval = et8ek8_enum_frame_ival, 1364 .get_fmt = et8ek8_get_pad_format, 1365 .set_fmt = et8ek8_set_pad_format, 1366 }; 1367 1368 static const struct v4l2_subdev_ops et8ek8_ops = { 1369 .core = &et8ek8_core_ops, 1370 .video = &et8ek8_video_ops, 1371 .pad = &et8ek8_pad_ops, 1372 }; 1373 1374 static const struct v4l2_subdev_internal_ops et8ek8_internal_ops = { 1375 .registered = et8ek8_registered, 1376 .open = et8ek8_open, 1377 .close = et8ek8_close, 1378 }; 1379 1380 /* -------------------------------------------------------------------------- 1381 * I2C driver 1382 */ 1383 static int __maybe_unused et8ek8_suspend(struct device *dev) 1384 { 1385 struct i2c_client *client = to_i2c_client(dev); 1386 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 1387 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1388 1389 if (!sensor->power_count) 1390 return 0; 1391 1392 return __et8ek8_set_power(sensor, false); 1393 } 1394 1395 static int __maybe_unused et8ek8_resume(struct device *dev) 1396 { 1397 struct i2c_client *client = to_i2c_client(dev); 1398 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 1399 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1400 1401 if (!sensor->power_count) 1402 return 0; 1403 1404 return __et8ek8_set_power(sensor, true); 1405 } 1406 1407 static int et8ek8_probe(struct i2c_client *client, 1408 const struct i2c_device_id *devid) 1409 { 1410 struct et8ek8_sensor *sensor; 1411 struct device *dev = &client->dev; 1412 int ret; 1413 1414 sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL); 1415 if (!sensor) 1416 return -ENOMEM; 1417 1418 sensor->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 1419 if (IS_ERR(sensor->reset)) { 1420 dev_dbg(&client->dev, "could not request reset gpio\n"); 1421 return PTR_ERR(sensor->reset); 1422 } 1423 1424 sensor->vana = devm_regulator_get(dev, "vana"); 1425 if (IS_ERR(sensor->vana)) { 1426 dev_err(&client->dev, "could not get regulator for vana\n"); 1427 return PTR_ERR(sensor->vana); 1428 } 1429 1430 sensor->ext_clk = devm_clk_get(dev, NULL); 1431 if (IS_ERR(sensor->ext_clk)) { 1432 dev_err(&client->dev, "could not get clock\n"); 1433 return PTR_ERR(sensor->ext_clk); 1434 } 1435 1436 ret = of_property_read_u32(dev->of_node, "clock-frequency", 1437 &sensor->xclk_freq); 1438 if (ret) { 1439 dev_warn(dev, "can't get clock-frequency\n"); 1440 return ret; 1441 } 1442 1443 mutex_init(&sensor->power_lock); 1444 1445 v4l2_i2c_subdev_init(&sensor->subdev, client, &et8ek8_ops); 1446 sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1447 sensor->subdev.internal_ops = &et8ek8_internal_ops; 1448 1449 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 1450 ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad); 1451 if (ret < 0) { 1452 dev_err(&client->dev, "media entity init failed!\n"); 1453 goto err_mutex; 1454 } 1455 1456 ret = v4l2_async_register_subdev_sensor_common(&sensor->subdev); 1457 if (ret < 0) 1458 goto err_entity; 1459 1460 dev_dbg(dev, "initialized!\n"); 1461 1462 return 0; 1463 1464 err_entity: 1465 media_entity_cleanup(&sensor->subdev.entity); 1466 err_mutex: 1467 mutex_destroy(&sensor->power_lock); 1468 return ret; 1469 } 1470 1471 static int __exit et8ek8_remove(struct i2c_client *client) 1472 { 1473 struct v4l2_subdev *subdev = i2c_get_clientdata(client); 1474 struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev); 1475 1476 if (sensor->power_count) { 1477 WARN_ON(1); 1478 et8ek8_power_off(sensor); 1479 sensor->power_count = 0; 1480 } 1481 1482 v4l2_device_unregister_subdev(&sensor->subdev); 1483 device_remove_file(&client->dev, &dev_attr_priv_mem); 1484 v4l2_ctrl_handler_free(&sensor->ctrl_handler); 1485 v4l2_async_unregister_subdev(&sensor->subdev); 1486 media_entity_cleanup(&sensor->subdev.entity); 1487 mutex_destroy(&sensor->power_lock); 1488 1489 return 0; 1490 } 1491 1492 static const struct of_device_id et8ek8_of_table[] = { 1493 { .compatible = "toshiba,et8ek8" }, 1494 { }, 1495 }; 1496 MODULE_DEVICE_TABLE(of, et8ek8_of_table); 1497 1498 static const struct i2c_device_id et8ek8_id_table[] = { 1499 { ET8EK8_NAME, 0 }, 1500 { } 1501 }; 1502 MODULE_DEVICE_TABLE(i2c, et8ek8_id_table); 1503 1504 static const struct dev_pm_ops et8ek8_pm_ops = { 1505 SET_SYSTEM_SLEEP_PM_OPS(et8ek8_suspend, et8ek8_resume) 1506 }; 1507 1508 static struct i2c_driver et8ek8_i2c_driver = { 1509 .driver = { 1510 .name = ET8EK8_NAME, 1511 .pm = &et8ek8_pm_ops, 1512 .of_match_table = et8ek8_of_table, 1513 }, 1514 .probe = et8ek8_probe, 1515 .remove = __exit_p(et8ek8_remove), 1516 .id_table = et8ek8_id_table, 1517 }; 1518 1519 module_i2c_driver(et8ek8_i2c_driver); 1520 1521 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>, Pavel Machek <pavel@ucw.cz"); 1522 MODULE_DESCRIPTION("Toshiba ET8EK8 camera sensor driver"); 1523 MODULE_LICENSE("GPL"); 1524