1 // SPDX-License-Identifier: GPL-2.0
3 * video-i2c.c - Support for I2C transport video devices
5 * Copyright (C) 2018 Matt Ranostay <matt.ranostay@konsulko.com>
8 * - Panasonic AMG88xx Grid-Eye Sensors
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/regmap.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-fh.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/videobuf2-v4l2.h>
30 #include <media/videobuf2-vmalloc.h>
32 #define VIDEO_I2C_DRIVER "video-i2c"
34 struct video_i2c_chip;
36 struct video_i2c_buffer {
37 struct vb2_v4l2_buffer vb;
38 struct list_head list;
41 struct video_i2c_data {
42 struct regmap *regmap;
43 const struct video_i2c_chip *chip;
46 unsigned int sequence;
47 struct mutex queue_lock;
49 struct v4l2_device v4l2_dev;
50 struct video_device vdev;
51 struct vb2_queue vb_vidq;
53 struct task_struct *kthread_vid_cap;
54 struct list_head vid_cap_active;
56 struct v4l2_fract frame_interval;
59 static const struct v4l2_fmtdesc amg88xx_format = {
60 .pixelformat = V4L2_PIX_FMT_Y12,
63 static const struct v4l2_frmsize_discrete amg88xx_size = {
68 static const struct regmap_config amg88xx_regmap_config = {
74 struct video_i2c_chip {
75 /* video dimensions */
76 const struct v4l2_fmtdesc *format;
77 const struct v4l2_frmsize_discrete *size;
79 /* available frame intervals */
80 const struct v4l2_fract *frame_intervals;
81 unsigned int num_frame_intervals;
83 /* pixel buffer size */
84 unsigned int buffer_size;
86 /* pixel size in bits */
89 const struct regmap_config *regmap_config;
92 int (*setup)(struct video_i2c_data *data);
95 int (*xfer)(struct video_i2c_data *data, char *buf);
97 /* hwmon init function */
98 int (*hwmon_init)(struct video_i2c_data *data);
101 /* Frame rate register */
102 #define AMG88XX_REG_FPSC 0x02
103 #define AMG88XX_FPSC_1FPS BIT(0)
105 /* Thermistor register */
106 #define AMG88XX_REG_TTHL 0x0e
108 /* Temperature register */
109 #define AMG88XX_REG_T01L 0x80
111 static int amg88xx_xfer(struct video_i2c_data *data, char *buf)
113 return regmap_bulk_read(data->regmap, AMG88XX_REG_T01L, buf,
114 data->chip->buffer_size);
117 static int amg88xx_setup(struct video_i2c_data *data)
119 unsigned int mask = AMG88XX_FPSC_1FPS;
122 if (data->frame_interval.numerator == data->frame_interval.denominator)
127 return regmap_update_bits(data->regmap, AMG88XX_REG_FPSC, mask, val);
130 #if IS_ENABLED(CONFIG_HWMON)
132 static const u32 amg88xx_temp_config[] = {
137 static const struct hwmon_channel_info amg88xx_temp = {
139 .config = amg88xx_temp_config,
142 static const struct hwmon_channel_info *amg88xx_info[] = {
147 static umode_t amg88xx_is_visible(const void *drvdata,
148 enum hwmon_sensor_types type,
149 u32 attr, int channel)
154 static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type,
155 u32 attr, int channel, long *val)
157 struct video_i2c_data *data = dev_get_drvdata(dev);
161 tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2);
165 tmp = le16_to_cpu(buf);
168 * Check for sign bit, this isn't a two's complement value but an
169 * absolute temperature that needs to be inverted in the case of being
173 tmp = -(tmp & 0x7ff);
175 *val = (tmp * 625) / 10;
180 static const struct hwmon_ops amg88xx_hwmon_ops = {
181 .is_visible = amg88xx_is_visible,
182 .read = amg88xx_read,
185 static const struct hwmon_chip_info amg88xx_chip_info = {
186 .ops = &amg88xx_hwmon_ops,
187 .info = amg88xx_info,
190 static int amg88xx_hwmon_init(struct video_i2c_data *data)
192 struct device *dev = regmap_get_device(data->regmap);
193 void *hwmon = devm_hwmon_device_register_with_info(dev, "amg88xx", data,
194 &amg88xx_chip_info, NULL);
196 return PTR_ERR_OR_ZERO(hwmon);
199 #define amg88xx_hwmon_init NULL
204 static const struct v4l2_fract amg88xx_frame_intervals[] = {
209 static const struct video_i2c_chip video_i2c_chip[] = {
211 .size = &amg88xx_size,
212 .format = &amg88xx_format,
213 .frame_intervals = amg88xx_frame_intervals,
214 .num_frame_intervals = ARRAY_SIZE(amg88xx_frame_intervals),
217 .regmap_config = &amg88xx_regmap_config,
218 .setup = &amg88xx_setup,
219 .xfer = &amg88xx_xfer,
220 .hwmon_init = amg88xx_hwmon_init,
224 static const struct v4l2_file_operations video_i2c_fops = {
225 .owner = THIS_MODULE,
226 .open = v4l2_fh_open,
227 .release = vb2_fop_release,
228 .poll = vb2_fop_poll,
229 .read = vb2_fop_read,
230 .mmap = vb2_fop_mmap,
231 .unlocked_ioctl = video_ioctl2,
234 static int queue_setup(struct vb2_queue *vq,
235 unsigned int *nbuffers, unsigned int *nplanes,
236 unsigned int sizes[], struct device *alloc_devs[])
238 struct video_i2c_data *data = vb2_get_drv_priv(vq);
239 unsigned int size = data->chip->buffer_size;
241 if (vq->num_buffers + *nbuffers < 2)
245 return sizes[0] < size ? -EINVAL : 0;
253 static int buffer_prepare(struct vb2_buffer *vb)
255 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
256 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
257 unsigned int size = data->chip->buffer_size;
259 if (vb2_plane_size(vb, 0) < size)
262 vbuf->field = V4L2_FIELD_NONE;
263 vb2_set_plane_payload(vb, 0, size);
268 static void buffer_queue(struct vb2_buffer *vb)
270 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
271 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
272 struct video_i2c_buffer *buf =
273 container_of(vbuf, struct video_i2c_buffer, vb);
275 spin_lock(&data->slock);
276 list_add_tail(&buf->list, &data->vid_cap_active);
277 spin_unlock(&data->slock);
280 static int video_i2c_thread_vid_cap(void *priv)
282 struct video_i2c_data *data = priv;
283 unsigned int delay = mult_frac(HZ, data->frame_interval.numerator,
284 data->frame_interval.denominator);
289 unsigned long start_jiffies = jiffies;
290 struct video_i2c_buffer *vid_cap_buf = NULL;
295 spin_lock(&data->slock);
297 if (!list_empty(&data->vid_cap_active)) {
298 vid_cap_buf = list_last_entry(&data->vid_cap_active,
299 struct video_i2c_buffer, list);
300 list_del(&vid_cap_buf->list);
303 spin_unlock(&data->slock);
306 struct vb2_buffer *vb2_buf = &vid_cap_buf->vb.vb2_buf;
307 void *vbuf = vb2_plane_vaddr(vb2_buf, 0);
310 ret = data->chip->xfer(data, vbuf);
311 vb2_buf->timestamp = ktime_get_ns();
312 vid_cap_buf->vb.sequence = data->sequence++;
313 vb2_buffer_done(vb2_buf, ret ?
314 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
317 schedule_delay = delay - (jiffies - start_jiffies);
319 if (time_after(jiffies, start_jiffies + delay))
320 schedule_delay = delay;
322 schedule_timeout_interruptible(schedule_delay);
323 } while (!kthread_should_stop());
328 static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state)
330 struct video_i2c_data *data = vb2_get_drv_priv(vq);
331 struct video_i2c_buffer *buf, *tmp;
333 spin_lock(&data->slock);
335 list_for_each_entry_safe(buf, tmp, &data->vid_cap_active, list) {
336 list_del(&buf->list);
337 vb2_buffer_done(&buf->vb.vb2_buf, state);
340 spin_unlock(&data->slock);
343 static int start_streaming(struct vb2_queue *vq, unsigned int count)
345 struct video_i2c_data *data = vb2_get_drv_priv(vq);
348 if (data->kthread_vid_cap)
351 ret = data->chip->setup(data);
356 data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data,
357 "%s-vid-cap", data->v4l2_dev.name);
358 ret = PTR_ERR_OR_ZERO(data->kthread_vid_cap);
363 video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED);
368 static void stop_streaming(struct vb2_queue *vq)
370 struct video_i2c_data *data = vb2_get_drv_priv(vq);
372 if (data->kthread_vid_cap == NULL)
375 kthread_stop(data->kthread_vid_cap);
376 data->kthread_vid_cap = NULL;
378 video_i2c_del_list(vq, VB2_BUF_STATE_ERROR);
381 static const struct vb2_ops video_i2c_video_qops = {
382 .queue_setup = queue_setup,
383 .buf_prepare = buffer_prepare,
384 .buf_queue = buffer_queue,
385 .start_streaming = start_streaming,
386 .stop_streaming = stop_streaming,
387 .wait_prepare = vb2_ops_wait_prepare,
388 .wait_finish = vb2_ops_wait_finish,
391 static int video_i2c_querycap(struct file *file, void *priv,
392 struct v4l2_capability *vcap)
394 struct video_i2c_data *data = video_drvdata(file);
395 struct device *dev = regmap_get_device(data->regmap);
396 struct i2c_client *client = to_i2c_client(dev);
398 strscpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver));
399 strscpy(vcap->card, data->vdev.name, sizeof(vcap->card));
401 sprintf(vcap->bus_info, "I2C:%d-%d", client->adapter->nr, client->addr);
406 static int video_i2c_g_input(struct file *file, void *fh, unsigned int *inp)
413 static int video_i2c_s_input(struct file *file, void *fh, unsigned int inp)
415 return (inp > 0) ? -EINVAL : 0;
418 static int video_i2c_enum_input(struct file *file, void *fh,
419 struct v4l2_input *vin)
424 strscpy(vin->name, "Camera", sizeof(vin->name));
426 vin->type = V4L2_INPUT_TYPE_CAMERA;
431 static int video_i2c_enum_fmt_vid_cap(struct file *file, void *fh,
432 struct v4l2_fmtdesc *fmt)
434 struct video_i2c_data *data = video_drvdata(file);
435 enum v4l2_buf_type type = fmt->type;
440 *fmt = *data->chip->format;
446 static int video_i2c_enum_framesizes(struct file *file, void *fh,
447 struct v4l2_frmsizeenum *fsize)
449 const struct video_i2c_data *data = video_drvdata(file);
450 const struct v4l2_frmsize_discrete *size = data->chip->size;
452 /* currently only one frame size is allowed */
453 if (fsize->index > 0)
456 if (fsize->pixel_format != data->chip->format->pixelformat)
459 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
460 fsize->discrete.width = size->width;
461 fsize->discrete.height = size->height;
466 static int video_i2c_enum_frameintervals(struct file *file, void *priv,
467 struct v4l2_frmivalenum *fe)
469 const struct video_i2c_data *data = video_drvdata(file);
470 const struct v4l2_frmsize_discrete *size = data->chip->size;
472 if (fe->index >= data->chip->num_frame_intervals)
475 if (fe->width != size->width || fe->height != size->height)
478 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
479 fe->discrete = data->chip->frame_intervals[fe->index];
484 static int video_i2c_try_fmt_vid_cap(struct file *file, void *fh,
485 struct v4l2_format *fmt)
487 const struct video_i2c_data *data = video_drvdata(file);
488 const struct v4l2_frmsize_discrete *size = data->chip->size;
489 struct v4l2_pix_format *pix = &fmt->fmt.pix;
490 unsigned int bpp = data->chip->bpp / 8;
492 pix->width = size->width;
493 pix->height = size->height;
494 pix->pixelformat = data->chip->format->pixelformat;
495 pix->field = V4L2_FIELD_NONE;
496 pix->bytesperline = pix->width * bpp;
497 pix->sizeimage = pix->bytesperline * pix->height;
498 pix->colorspace = V4L2_COLORSPACE_RAW;
503 static int video_i2c_s_fmt_vid_cap(struct file *file, void *fh,
504 struct v4l2_format *fmt)
506 struct video_i2c_data *data = video_drvdata(file);
508 if (vb2_is_busy(&data->vb_vidq))
511 return video_i2c_try_fmt_vid_cap(file, fh, fmt);
514 static int video_i2c_g_parm(struct file *filp, void *priv,
515 struct v4l2_streamparm *parm)
517 struct video_i2c_data *data = video_drvdata(filp);
519 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
522 parm->parm.capture.readbuffers = 1;
523 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
524 parm->parm.capture.timeperframe = data->frame_interval;
529 static int video_i2c_s_parm(struct file *filp, void *priv,
530 struct v4l2_streamparm *parm)
532 struct video_i2c_data *data = video_drvdata(filp);
535 for (i = 0; i < data->chip->num_frame_intervals - 1; i++) {
536 if (V4L2_FRACT_COMPARE(parm->parm.capture.timeperframe, <=,
537 data->chip->frame_intervals[i]))
540 data->frame_interval = data->chip->frame_intervals[i];
542 return video_i2c_g_parm(filp, priv, parm);
545 static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = {
546 .vidioc_querycap = video_i2c_querycap,
547 .vidioc_g_input = video_i2c_g_input,
548 .vidioc_s_input = video_i2c_s_input,
549 .vidioc_enum_input = video_i2c_enum_input,
550 .vidioc_enum_fmt_vid_cap = video_i2c_enum_fmt_vid_cap,
551 .vidioc_enum_framesizes = video_i2c_enum_framesizes,
552 .vidioc_enum_frameintervals = video_i2c_enum_frameintervals,
553 .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
554 .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap,
555 .vidioc_g_parm = video_i2c_g_parm,
556 .vidioc_s_parm = video_i2c_s_parm,
557 .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
558 .vidioc_reqbufs = vb2_ioctl_reqbufs,
559 .vidioc_create_bufs = vb2_ioctl_create_bufs,
560 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
561 .vidioc_querybuf = vb2_ioctl_querybuf,
562 .vidioc_qbuf = vb2_ioctl_qbuf,
563 .vidioc_dqbuf = vb2_ioctl_dqbuf,
564 .vidioc_streamon = vb2_ioctl_streamon,
565 .vidioc_streamoff = vb2_ioctl_streamoff,
568 static void video_i2c_release(struct video_device *vdev)
570 struct video_i2c_data *data = video_get_drvdata(vdev);
572 v4l2_device_unregister(&data->v4l2_dev);
573 mutex_destroy(&data->lock);
574 mutex_destroy(&data->queue_lock);
575 regmap_exit(data->regmap);
579 static int video_i2c_probe(struct i2c_client *client,
580 const struct i2c_device_id *id)
582 struct video_i2c_data *data;
583 struct v4l2_device *v4l2_dev;
584 struct vb2_queue *queue;
587 data = kzalloc(sizeof(*data), GFP_KERNEL);
591 if (dev_fwnode(&client->dev))
592 data->chip = device_get_match_data(&client->dev);
594 data->chip = &video_i2c_chip[id->driver_data];
596 goto error_free_device;
598 data->regmap = regmap_init_i2c(client, data->chip->regmap_config);
599 if (IS_ERR(data->regmap)) {
600 ret = PTR_ERR(data->regmap);
601 goto error_free_device;
604 v4l2_dev = &data->v4l2_dev;
605 strscpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name));
607 ret = v4l2_device_register(&client->dev, v4l2_dev);
609 goto error_regmap_exit;
611 mutex_init(&data->lock);
612 mutex_init(&data->queue_lock);
614 queue = &data->vb_vidq;
615 queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
616 queue->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR | VB2_READ;
617 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
618 queue->drv_priv = data;
619 queue->buf_struct_size = sizeof(struct video_i2c_buffer);
620 queue->min_buffers_needed = 1;
621 queue->ops = &video_i2c_video_qops;
622 queue->mem_ops = &vb2_vmalloc_memops;
624 ret = vb2_queue_init(queue);
626 goto error_unregister_device;
628 data->vdev.queue = queue;
629 data->vdev.queue->lock = &data->queue_lock;
631 snprintf(data->vdev.name, sizeof(data->vdev.name),
632 "I2C %d-%d Transport Video",
633 client->adapter->nr, client->addr);
635 data->vdev.v4l2_dev = v4l2_dev;
636 data->vdev.fops = &video_i2c_fops;
637 data->vdev.lock = &data->lock;
638 data->vdev.ioctl_ops = &video_i2c_ioctl_ops;
639 data->vdev.release = video_i2c_release;
640 data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
641 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
643 spin_lock_init(&data->slock);
644 INIT_LIST_HEAD(&data->vid_cap_active);
646 data->frame_interval = data->chip->frame_intervals[0];
648 video_set_drvdata(&data->vdev, data);
649 i2c_set_clientdata(client, data);
651 if (data->chip->hwmon_init) {
652 ret = data->chip->hwmon_init(data);
654 dev_warn(&client->dev,
655 "failed to register hwmon device\n");
659 ret = video_register_device(&data->vdev, VFL_TYPE_GRABBER, -1);
661 goto error_unregister_device;
665 error_unregister_device:
666 v4l2_device_unregister(v4l2_dev);
667 mutex_destroy(&data->lock);
668 mutex_destroy(&data->queue_lock);
671 regmap_exit(data->regmap);
679 static int video_i2c_remove(struct i2c_client *client)
681 struct video_i2c_data *data = i2c_get_clientdata(client);
683 video_unregister_device(&data->vdev);
688 static const struct i2c_device_id video_i2c_id_table[] = {
689 { "amg88xx", AMG88XX },
692 MODULE_DEVICE_TABLE(i2c, video_i2c_id_table);
694 static const struct of_device_id video_i2c_of_match[] = {
695 { .compatible = "panasonic,amg88xx", .data = &video_i2c_chip[AMG88XX] },
698 MODULE_DEVICE_TABLE(of, video_i2c_of_match);
700 static struct i2c_driver video_i2c_driver = {
702 .name = VIDEO_I2C_DRIVER,
703 .of_match_table = video_i2c_of_match,
705 .probe = video_i2c_probe,
706 .remove = video_i2c_remove,
707 .id_table = video_i2c_id_table,
710 module_i2c_driver(video_i2c_driver);
712 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
713 MODULE_DESCRIPTION("I2C transport video support");
714 MODULE_LICENSE("GPL v2");