1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * video-i2c.c - Support for I2C transport video devices 4 * 5 * Copyright (C) 2018 Matt Ranostay <matt.ranostay@konsulko.com> 6 * 7 * Supported: 8 * - Panasonic AMG88xx Grid-Eye Sensors 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/freezer.h> 13 #include <linux/hwmon.h> 14 #include <linux/kthread.h> 15 #include <linux/i2c.h> 16 #include <linux/list.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/of_device.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/regmap.h> 22 #include <linux/sched.h> 23 #include <linux/slab.h> 24 #include <linux/videodev2.h> 25 #include <media/v4l2-common.h> 26 #include <media/v4l2-device.h> 27 #include <media/v4l2-event.h> 28 #include <media/v4l2-fh.h> 29 #include <media/v4l2-ioctl.h> 30 #include <media/videobuf2-v4l2.h> 31 #include <media/videobuf2-vmalloc.h> 32 33 #define VIDEO_I2C_DRIVER "video-i2c" 34 35 struct video_i2c_chip; 36 37 struct video_i2c_buffer { 38 struct vb2_v4l2_buffer vb; 39 struct list_head list; 40 }; 41 42 struct video_i2c_data { 43 struct regmap *regmap; 44 const struct video_i2c_chip *chip; 45 struct mutex lock; 46 spinlock_t slock; 47 unsigned int sequence; 48 struct mutex queue_lock; 49 50 struct v4l2_device v4l2_dev; 51 struct video_device vdev; 52 struct vb2_queue vb_vidq; 53 54 struct task_struct *kthread_vid_cap; 55 struct list_head vid_cap_active; 56 57 struct v4l2_fract frame_interval; 58 }; 59 60 static const struct v4l2_fmtdesc amg88xx_format = { 61 .pixelformat = V4L2_PIX_FMT_Y12, 62 }; 63 64 static const struct v4l2_frmsize_discrete amg88xx_size = { 65 .width = 8, 66 .height = 8, 67 }; 68 69 static const struct regmap_config amg88xx_regmap_config = { 70 .reg_bits = 8, 71 .val_bits = 8, 72 .max_register = 0xff 73 }; 74 75 struct video_i2c_chip { 76 /* video dimensions */ 77 const struct v4l2_fmtdesc *format; 78 const struct v4l2_frmsize_discrete *size; 79 80 /* available frame intervals */ 81 const struct v4l2_fract *frame_intervals; 82 unsigned int num_frame_intervals; 83 84 /* pixel buffer size */ 85 unsigned int buffer_size; 86 87 /* pixel size in bits */ 88 unsigned int bpp; 89 90 const struct regmap_config *regmap_config; 91 92 /* setup function */ 93 int (*setup)(struct video_i2c_data *data); 94 95 /* xfer function */ 96 int (*xfer)(struct video_i2c_data *data, char *buf); 97 98 /* power control function */ 99 int (*set_power)(struct video_i2c_data *data, bool on); 100 101 /* hwmon init function */ 102 int (*hwmon_init)(struct video_i2c_data *data); 103 }; 104 105 /* Power control register */ 106 #define AMG88XX_REG_PCTL 0x00 107 #define AMG88XX_PCTL_NORMAL 0x00 108 #define AMG88XX_PCTL_SLEEP 0x10 109 110 /* Reset register */ 111 #define AMG88XX_REG_RST 0x01 112 #define AMG88XX_RST_FLAG 0x30 113 #define AMG88XX_RST_INIT 0x3f 114 115 /* Frame rate register */ 116 #define AMG88XX_REG_FPSC 0x02 117 #define AMG88XX_FPSC_1FPS BIT(0) 118 119 /* Thermistor register */ 120 #define AMG88XX_REG_TTHL 0x0e 121 122 /* Temperature register */ 123 #define AMG88XX_REG_T01L 0x80 124 125 static int amg88xx_xfer(struct video_i2c_data *data, char *buf) 126 { 127 return regmap_bulk_read(data->regmap, AMG88XX_REG_T01L, buf, 128 data->chip->buffer_size); 129 } 130 131 static int amg88xx_setup(struct video_i2c_data *data) 132 { 133 unsigned int mask = AMG88XX_FPSC_1FPS; 134 unsigned int val; 135 136 if (data->frame_interval.numerator == data->frame_interval.denominator) 137 val = mask; 138 else 139 val = 0; 140 141 return regmap_update_bits(data->regmap, AMG88XX_REG_FPSC, mask, val); 142 } 143 144 static int amg88xx_set_power_on(struct video_i2c_data *data) 145 { 146 int ret; 147 148 ret = regmap_write(data->regmap, AMG88XX_REG_PCTL, AMG88XX_PCTL_NORMAL); 149 if (ret) 150 return ret; 151 152 msleep(50); 153 154 ret = regmap_write(data->regmap, AMG88XX_REG_RST, AMG88XX_RST_INIT); 155 if (ret) 156 return ret; 157 158 usleep_range(2000, 3000); 159 160 ret = regmap_write(data->regmap, AMG88XX_REG_RST, AMG88XX_RST_FLAG); 161 if (ret) 162 return ret; 163 164 /* 165 * Wait two frames before reading thermistor and temperature registers 166 */ 167 msleep(200); 168 169 return 0; 170 } 171 172 static int amg88xx_set_power_off(struct video_i2c_data *data) 173 { 174 int ret; 175 176 ret = regmap_write(data->regmap, AMG88XX_REG_PCTL, AMG88XX_PCTL_SLEEP); 177 if (ret) 178 return ret; 179 /* 180 * Wait for a while to avoid resuming normal mode immediately after 181 * entering sleep mode, otherwise the device occasionally goes wrong 182 * (thermistor and temperature registers are not updated at all) 183 */ 184 msleep(100); 185 186 return 0; 187 } 188 189 static int amg88xx_set_power(struct video_i2c_data *data, bool on) 190 { 191 if (on) 192 return amg88xx_set_power_on(data); 193 194 return amg88xx_set_power_off(data); 195 } 196 197 #if IS_ENABLED(CONFIG_HWMON) 198 199 static const u32 amg88xx_temp_config[] = { 200 HWMON_T_INPUT, 201 0 202 }; 203 204 static const struct hwmon_channel_info amg88xx_temp = { 205 .type = hwmon_temp, 206 .config = amg88xx_temp_config, 207 }; 208 209 static const struct hwmon_channel_info *amg88xx_info[] = { 210 &amg88xx_temp, 211 NULL 212 }; 213 214 static umode_t amg88xx_is_visible(const void *drvdata, 215 enum hwmon_sensor_types type, 216 u32 attr, int channel) 217 { 218 return 0444; 219 } 220 221 static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type, 222 u32 attr, int channel, long *val) 223 { 224 struct video_i2c_data *data = dev_get_drvdata(dev); 225 __le16 buf; 226 int tmp; 227 228 tmp = pm_runtime_get_sync(regmap_get_device(data->regmap)); 229 if (tmp < 0) { 230 pm_runtime_put_noidle(regmap_get_device(data->regmap)); 231 return tmp; 232 } 233 234 tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2); 235 pm_runtime_mark_last_busy(regmap_get_device(data->regmap)); 236 pm_runtime_put_autosuspend(regmap_get_device(data->regmap)); 237 if (tmp) 238 return tmp; 239 240 tmp = le16_to_cpu(buf); 241 242 /* 243 * Check for sign bit, this isn't a two's complement value but an 244 * absolute temperature that needs to be inverted in the case of being 245 * negative. 246 */ 247 if (tmp & BIT(11)) 248 tmp = -(tmp & 0x7ff); 249 250 *val = (tmp * 625) / 10; 251 252 return 0; 253 } 254 255 static const struct hwmon_ops amg88xx_hwmon_ops = { 256 .is_visible = amg88xx_is_visible, 257 .read = amg88xx_read, 258 }; 259 260 static const struct hwmon_chip_info amg88xx_chip_info = { 261 .ops = &amg88xx_hwmon_ops, 262 .info = amg88xx_info, 263 }; 264 265 static int amg88xx_hwmon_init(struct video_i2c_data *data) 266 { 267 struct device *dev = regmap_get_device(data->regmap); 268 void *hwmon = devm_hwmon_device_register_with_info(dev, "amg88xx", data, 269 &amg88xx_chip_info, NULL); 270 271 return PTR_ERR_OR_ZERO(hwmon); 272 } 273 #else 274 #define amg88xx_hwmon_init NULL 275 #endif 276 277 #define AMG88XX 0 278 279 static const struct v4l2_fract amg88xx_frame_intervals[] = { 280 { 1, 10 }, 281 { 1, 1 }, 282 }; 283 284 static const struct video_i2c_chip video_i2c_chip[] = { 285 [AMG88XX] = { 286 .size = &amg88xx_size, 287 .format = &amg88xx_format, 288 .frame_intervals = amg88xx_frame_intervals, 289 .num_frame_intervals = ARRAY_SIZE(amg88xx_frame_intervals), 290 .buffer_size = 128, 291 .bpp = 16, 292 .regmap_config = &amg88xx_regmap_config, 293 .setup = &amg88xx_setup, 294 .xfer = &amg88xx_xfer, 295 .set_power = amg88xx_set_power, 296 .hwmon_init = amg88xx_hwmon_init, 297 }, 298 }; 299 300 static const struct v4l2_file_operations video_i2c_fops = { 301 .owner = THIS_MODULE, 302 .open = v4l2_fh_open, 303 .release = vb2_fop_release, 304 .poll = vb2_fop_poll, 305 .read = vb2_fop_read, 306 .mmap = vb2_fop_mmap, 307 .unlocked_ioctl = video_ioctl2, 308 }; 309 310 static int queue_setup(struct vb2_queue *vq, 311 unsigned int *nbuffers, unsigned int *nplanes, 312 unsigned int sizes[], struct device *alloc_devs[]) 313 { 314 struct video_i2c_data *data = vb2_get_drv_priv(vq); 315 unsigned int size = data->chip->buffer_size; 316 317 if (vq->num_buffers + *nbuffers < 2) 318 *nbuffers = 2; 319 320 if (*nplanes) 321 return sizes[0] < size ? -EINVAL : 0; 322 323 *nplanes = 1; 324 sizes[0] = size; 325 326 return 0; 327 } 328 329 static int buffer_prepare(struct vb2_buffer *vb) 330 { 331 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 332 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue); 333 unsigned int size = data->chip->buffer_size; 334 335 if (vb2_plane_size(vb, 0) < size) 336 return -EINVAL; 337 338 vbuf->field = V4L2_FIELD_NONE; 339 vb2_set_plane_payload(vb, 0, size); 340 341 return 0; 342 } 343 344 static void buffer_queue(struct vb2_buffer *vb) 345 { 346 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 347 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue); 348 struct video_i2c_buffer *buf = 349 container_of(vbuf, struct video_i2c_buffer, vb); 350 351 spin_lock(&data->slock); 352 list_add_tail(&buf->list, &data->vid_cap_active); 353 spin_unlock(&data->slock); 354 } 355 356 static int video_i2c_thread_vid_cap(void *priv) 357 { 358 struct video_i2c_data *data = priv; 359 unsigned int delay = mult_frac(HZ, data->frame_interval.numerator, 360 data->frame_interval.denominator); 361 362 set_freezable(); 363 364 do { 365 unsigned long start_jiffies = jiffies; 366 struct video_i2c_buffer *vid_cap_buf = NULL; 367 int schedule_delay; 368 369 try_to_freeze(); 370 371 spin_lock(&data->slock); 372 373 if (!list_empty(&data->vid_cap_active)) { 374 vid_cap_buf = list_last_entry(&data->vid_cap_active, 375 struct video_i2c_buffer, list); 376 list_del(&vid_cap_buf->list); 377 } 378 379 spin_unlock(&data->slock); 380 381 if (vid_cap_buf) { 382 struct vb2_buffer *vb2_buf = &vid_cap_buf->vb.vb2_buf; 383 void *vbuf = vb2_plane_vaddr(vb2_buf, 0); 384 int ret; 385 386 ret = data->chip->xfer(data, vbuf); 387 vb2_buf->timestamp = ktime_get_ns(); 388 vid_cap_buf->vb.sequence = data->sequence++; 389 vb2_buffer_done(vb2_buf, ret ? 390 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); 391 } 392 393 schedule_delay = delay - (jiffies - start_jiffies); 394 395 if (time_after(jiffies, start_jiffies + delay)) 396 schedule_delay = delay; 397 398 schedule_timeout_interruptible(schedule_delay); 399 } while (!kthread_should_stop()); 400 401 return 0; 402 } 403 404 static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state) 405 { 406 struct video_i2c_data *data = vb2_get_drv_priv(vq); 407 struct video_i2c_buffer *buf, *tmp; 408 409 spin_lock(&data->slock); 410 411 list_for_each_entry_safe(buf, tmp, &data->vid_cap_active, list) { 412 list_del(&buf->list); 413 vb2_buffer_done(&buf->vb.vb2_buf, state); 414 } 415 416 spin_unlock(&data->slock); 417 } 418 419 static int start_streaming(struct vb2_queue *vq, unsigned int count) 420 { 421 struct video_i2c_data *data = vb2_get_drv_priv(vq); 422 struct device *dev = regmap_get_device(data->regmap); 423 int ret; 424 425 if (data->kthread_vid_cap) 426 return 0; 427 428 ret = pm_runtime_get_sync(dev); 429 if (ret < 0) { 430 pm_runtime_put_noidle(dev); 431 goto error_del_list; 432 } 433 434 ret = data->chip->setup(data); 435 if (ret) 436 goto error_rpm_put; 437 438 data->sequence = 0; 439 data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data, 440 "%s-vid-cap", data->v4l2_dev.name); 441 ret = PTR_ERR_OR_ZERO(data->kthread_vid_cap); 442 if (!ret) 443 return 0; 444 445 error_rpm_put: 446 pm_runtime_mark_last_busy(dev); 447 pm_runtime_put_autosuspend(dev); 448 error_del_list: 449 video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED); 450 451 return ret; 452 } 453 454 static void stop_streaming(struct vb2_queue *vq) 455 { 456 struct video_i2c_data *data = vb2_get_drv_priv(vq); 457 458 if (data->kthread_vid_cap == NULL) 459 return; 460 461 kthread_stop(data->kthread_vid_cap); 462 data->kthread_vid_cap = NULL; 463 pm_runtime_mark_last_busy(regmap_get_device(data->regmap)); 464 pm_runtime_put_autosuspend(regmap_get_device(data->regmap)); 465 466 video_i2c_del_list(vq, VB2_BUF_STATE_ERROR); 467 } 468 469 static const struct vb2_ops video_i2c_video_qops = { 470 .queue_setup = queue_setup, 471 .buf_prepare = buffer_prepare, 472 .buf_queue = buffer_queue, 473 .start_streaming = start_streaming, 474 .stop_streaming = stop_streaming, 475 .wait_prepare = vb2_ops_wait_prepare, 476 .wait_finish = vb2_ops_wait_finish, 477 }; 478 479 static int video_i2c_querycap(struct file *file, void *priv, 480 struct v4l2_capability *vcap) 481 { 482 struct video_i2c_data *data = video_drvdata(file); 483 struct device *dev = regmap_get_device(data->regmap); 484 struct i2c_client *client = to_i2c_client(dev); 485 486 strscpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver)); 487 strscpy(vcap->card, data->vdev.name, sizeof(vcap->card)); 488 489 sprintf(vcap->bus_info, "I2C:%d-%d", client->adapter->nr, client->addr); 490 491 return 0; 492 } 493 494 static int video_i2c_g_input(struct file *file, void *fh, unsigned int *inp) 495 { 496 *inp = 0; 497 498 return 0; 499 } 500 501 static int video_i2c_s_input(struct file *file, void *fh, unsigned int inp) 502 { 503 return (inp > 0) ? -EINVAL : 0; 504 } 505 506 static int video_i2c_enum_input(struct file *file, void *fh, 507 struct v4l2_input *vin) 508 { 509 if (vin->index > 0) 510 return -EINVAL; 511 512 strscpy(vin->name, "Camera", sizeof(vin->name)); 513 514 vin->type = V4L2_INPUT_TYPE_CAMERA; 515 516 return 0; 517 } 518 519 static int video_i2c_enum_fmt_vid_cap(struct file *file, void *fh, 520 struct v4l2_fmtdesc *fmt) 521 { 522 struct video_i2c_data *data = video_drvdata(file); 523 enum v4l2_buf_type type = fmt->type; 524 525 if (fmt->index > 0) 526 return -EINVAL; 527 528 *fmt = *data->chip->format; 529 fmt->type = type; 530 531 return 0; 532 } 533 534 static int video_i2c_enum_framesizes(struct file *file, void *fh, 535 struct v4l2_frmsizeenum *fsize) 536 { 537 const struct video_i2c_data *data = video_drvdata(file); 538 const struct v4l2_frmsize_discrete *size = data->chip->size; 539 540 /* currently only one frame size is allowed */ 541 if (fsize->index > 0) 542 return -EINVAL; 543 544 if (fsize->pixel_format != data->chip->format->pixelformat) 545 return -EINVAL; 546 547 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 548 fsize->discrete.width = size->width; 549 fsize->discrete.height = size->height; 550 551 return 0; 552 } 553 554 static int video_i2c_enum_frameintervals(struct file *file, void *priv, 555 struct v4l2_frmivalenum *fe) 556 { 557 const struct video_i2c_data *data = video_drvdata(file); 558 const struct v4l2_frmsize_discrete *size = data->chip->size; 559 560 if (fe->index >= data->chip->num_frame_intervals) 561 return -EINVAL; 562 563 if (fe->width != size->width || fe->height != size->height) 564 return -EINVAL; 565 566 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE; 567 fe->discrete = data->chip->frame_intervals[fe->index]; 568 569 return 0; 570 } 571 572 static int video_i2c_try_fmt_vid_cap(struct file *file, void *fh, 573 struct v4l2_format *fmt) 574 { 575 const struct video_i2c_data *data = video_drvdata(file); 576 const struct v4l2_frmsize_discrete *size = data->chip->size; 577 struct v4l2_pix_format *pix = &fmt->fmt.pix; 578 unsigned int bpp = data->chip->bpp / 8; 579 580 pix->width = size->width; 581 pix->height = size->height; 582 pix->pixelformat = data->chip->format->pixelformat; 583 pix->field = V4L2_FIELD_NONE; 584 pix->bytesperline = pix->width * bpp; 585 pix->sizeimage = pix->bytesperline * pix->height; 586 pix->colorspace = V4L2_COLORSPACE_RAW; 587 588 return 0; 589 } 590 591 static int video_i2c_s_fmt_vid_cap(struct file *file, void *fh, 592 struct v4l2_format *fmt) 593 { 594 struct video_i2c_data *data = video_drvdata(file); 595 596 if (vb2_is_busy(&data->vb_vidq)) 597 return -EBUSY; 598 599 return video_i2c_try_fmt_vid_cap(file, fh, fmt); 600 } 601 602 static int video_i2c_g_parm(struct file *filp, void *priv, 603 struct v4l2_streamparm *parm) 604 { 605 struct video_i2c_data *data = video_drvdata(filp); 606 607 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 608 return -EINVAL; 609 610 parm->parm.capture.readbuffers = 1; 611 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 612 parm->parm.capture.timeperframe = data->frame_interval; 613 614 return 0; 615 } 616 617 static int video_i2c_s_parm(struct file *filp, void *priv, 618 struct v4l2_streamparm *parm) 619 { 620 struct video_i2c_data *data = video_drvdata(filp); 621 int i; 622 623 for (i = 0; i < data->chip->num_frame_intervals - 1; i++) { 624 if (V4L2_FRACT_COMPARE(parm->parm.capture.timeperframe, <=, 625 data->chip->frame_intervals[i])) 626 break; 627 } 628 data->frame_interval = data->chip->frame_intervals[i]; 629 630 return video_i2c_g_parm(filp, priv, parm); 631 } 632 633 static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = { 634 .vidioc_querycap = video_i2c_querycap, 635 .vidioc_g_input = video_i2c_g_input, 636 .vidioc_s_input = video_i2c_s_input, 637 .vidioc_enum_input = video_i2c_enum_input, 638 .vidioc_enum_fmt_vid_cap = video_i2c_enum_fmt_vid_cap, 639 .vidioc_enum_framesizes = video_i2c_enum_framesizes, 640 .vidioc_enum_frameintervals = video_i2c_enum_frameintervals, 641 .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap, 642 .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap, 643 .vidioc_g_parm = video_i2c_g_parm, 644 .vidioc_s_parm = video_i2c_s_parm, 645 .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap, 646 .vidioc_reqbufs = vb2_ioctl_reqbufs, 647 .vidioc_create_bufs = vb2_ioctl_create_bufs, 648 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 649 .vidioc_querybuf = vb2_ioctl_querybuf, 650 .vidioc_qbuf = vb2_ioctl_qbuf, 651 .vidioc_dqbuf = vb2_ioctl_dqbuf, 652 .vidioc_streamon = vb2_ioctl_streamon, 653 .vidioc_streamoff = vb2_ioctl_streamoff, 654 }; 655 656 static void video_i2c_release(struct video_device *vdev) 657 { 658 struct video_i2c_data *data = video_get_drvdata(vdev); 659 660 v4l2_device_unregister(&data->v4l2_dev); 661 mutex_destroy(&data->lock); 662 mutex_destroy(&data->queue_lock); 663 regmap_exit(data->regmap); 664 kfree(data); 665 } 666 667 static int video_i2c_probe(struct i2c_client *client, 668 const struct i2c_device_id *id) 669 { 670 struct video_i2c_data *data; 671 struct v4l2_device *v4l2_dev; 672 struct vb2_queue *queue; 673 int ret = -ENODEV; 674 675 data = kzalloc(sizeof(*data), GFP_KERNEL); 676 if (!data) 677 return -ENOMEM; 678 679 if (dev_fwnode(&client->dev)) 680 data->chip = device_get_match_data(&client->dev); 681 else if (id) 682 data->chip = &video_i2c_chip[id->driver_data]; 683 else 684 goto error_free_device; 685 686 data->regmap = regmap_init_i2c(client, data->chip->regmap_config); 687 if (IS_ERR(data->regmap)) { 688 ret = PTR_ERR(data->regmap); 689 goto error_free_device; 690 } 691 692 v4l2_dev = &data->v4l2_dev; 693 strscpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name)); 694 695 ret = v4l2_device_register(&client->dev, v4l2_dev); 696 if (ret < 0) 697 goto error_regmap_exit; 698 699 mutex_init(&data->lock); 700 mutex_init(&data->queue_lock); 701 702 queue = &data->vb_vidq; 703 queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 704 queue->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR | VB2_READ; 705 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 706 queue->drv_priv = data; 707 queue->buf_struct_size = sizeof(struct video_i2c_buffer); 708 queue->min_buffers_needed = 1; 709 queue->ops = &video_i2c_video_qops; 710 queue->mem_ops = &vb2_vmalloc_memops; 711 712 ret = vb2_queue_init(queue); 713 if (ret < 0) 714 goto error_unregister_device; 715 716 data->vdev.queue = queue; 717 data->vdev.queue->lock = &data->queue_lock; 718 719 snprintf(data->vdev.name, sizeof(data->vdev.name), 720 "I2C %d-%d Transport Video", 721 client->adapter->nr, client->addr); 722 723 data->vdev.v4l2_dev = v4l2_dev; 724 data->vdev.fops = &video_i2c_fops; 725 data->vdev.lock = &data->lock; 726 data->vdev.ioctl_ops = &video_i2c_ioctl_ops; 727 data->vdev.release = video_i2c_release; 728 data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | 729 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 730 731 spin_lock_init(&data->slock); 732 INIT_LIST_HEAD(&data->vid_cap_active); 733 734 data->frame_interval = data->chip->frame_intervals[0]; 735 736 video_set_drvdata(&data->vdev, data); 737 i2c_set_clientdata(client, data); 738 739 if (data->chip->set_power) { 740 ret = data->chip->set_power(data, true); 741 if (ret) 742 goto error_unregister_device; 743 } 744 745 pm_runtime_get_noresume(&client->dev); 746 pm_runtime_set_active(&client->dev); 747 pm_runtime_enable(&client->dev); 748 pm_runtime_set_autosuspend_delay(&client->dev, 2000); 749 pm_runtime_use_autosuspend(&client->dev); 750 751 if (data->chip->hwmon_init) { 752 ret = data->chip->hwmon_init(data); 753 if (ret < 0) { 754 dev_warn(&client->dev, 755 "failed to register hwmon device\n"); 756 } 757 } 758 759 ret = video_register_device(&data->vdev, VFL_TYPE_GRABBER, -1); 760 if (ret < 0) 761 goto error_pm_disable; 762 763 pm_runtime_mark_last_busy(&client->dev); 764 pm_runtime_put_autosuspend(&client->dev); 765 766 return 0; 767 768 error_pm_disable: 769 pm_runtime_disable(&client->dev); 770 pm_runtime_set_suspended(&client->dev); 771 pm_runtime_put_noidle(&client->dev); 772 773 if (data->chip->set_power) 774 data->chip->set_power(data, false); 775 776 error_unregister_device: 777 v4l2_device_unregister(v4l2_dev); 778 mutex_destroy(&data->lock); 779 mutex_destroy(&data->queue_lock); 780 781 error_regmap_exit: 782 regmap_exit(data->regmap); 783 784 error_free_device: 785 kfree(data); 786 787 return ret; 788 } 789 790 static int video_i2c_remove(struct i2c_client *client) 791 { 792 struct video_i2c_data *data = i2c_get_clientdata(client); 793 794 pm_runtime_get_sync(&client->dev); 795 pm_runtime_disable(&client->dev); 796 pm_runtime_set_suspended(&client->dev); 797 pm_runtime_put_noidle(&client->dev); 798 799 if (data->chip->set_power) 800 data->chip->set_power(data, false); 801 802 video_unregister_device(&data->vdev); 803 804 return 0; 805 } 806 807 #ifdef CONFIG_PM 808 809 static int video_i2c_pm_runtime_suspend(struct device *dev) 810 { 811 struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev)); 812 813 if (!data->chip->set_power) 814 return 0; 815 816 return data->chip->set_power(data, false); 817 } 818 819 static int video_i2c_pm_runtime_resume(struct device *dev) 820 { 821 struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev)); 822 823 if (!data->chip->set_power) 824 return 0; 825 826 return data->chip->set_power(data, true); 827 } 828 829 #endif 830 831 static const struct dev_pm_ops video_i2c_pm_ops = { 832 SET_RUNTIME_PM_OPS(video_i2c_pm_runtime_suspend, 833 video_i2c_pm_runtime_resume, NULL) 834 }; 835 836 static const struct i2c_device_id video_i2c_id_table[] = { 837 { "amg88xx", AMG88XX }, 838 {} 839 }; 840 MODULE_DEVICE_TABLE(i2c, video_i2c_id_table); 841 842 static const struct of_device_id video_i2c_of_match[] = { 843 { .compatible = "panasonic,amg88xx", .data = &video_i2c_chip[AMG88XX] }, 844 {} 845 }; 846 MODULE_DEVICE_TABLE(of, video_i2c_of_match); 847 848 static struct i2c_driver video_i2c_driver = { 849 .driver = { 850 .name = VIDEO_I2C_DRIVER, 851 .of_match_table = video_i2c_of_match, 852 .pm = &video_i2c_pm_ops, 853 }, 854 .probe = video_i2c_probe, 855 .remove = video_i2c_remove, 856 .id_table = video_i2c_id_table, 857 }; 858 859 module_i2c_driver(video_i2c_driver); 860 861 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 862 MODULE_DESCRIPTION("I2C transport video support"); 863 MODULE_LICENSE("GPL v2"); 864