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