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