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/sched.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/videobuf2-vmalloc.h>
31 #define VIDEO_I2C_DRIVER "video-i2c"
33 struct video_i2c_chip
;
35 struct video_i2c_buffer
{
36 struct vb2_v4l2_buffer vb
;
37 struct list_head list
;
40 struct video_i2c_data
{
41 struct i2c_client
*client
;
42 const struct video_i2c_chip
*chip
;
45 unsigned int sequence
;
46 struct mutex queue_lock
;
48 struct v4l2_device v4l2_dev
;
49 struct video_device vdev
;
50 struct vb2_queue vb_vidq
;
52 struct task_struct
*kthread_vid_cap
;
53 struct list_head vid_cap_active
;
56 static const struct v4l2_fmtdesc amg88xx_format
= {
57 .pixelformat
= V4L2_PIX_FMT_Y12
,
60 static const struct v4l2_frmsize_discrete amg88xx_size
= {
65 struct video_i2c_chip
{
66 /* video dimensions */
67 const struct v4l2_fmtdesc
*format
;
68 const struct v4l2_frmsize_discrete
*size
;
70 /* max frames per second */
73 /* pixel buffer size */
74 unsigned int buffer_size
;
76 /* pixel size in bits */
80 int (*xfer
)(struct video_i2c_data
*data
, char *buf
);
82 /* hwmon init function */
83 int (*hwmon_init
)(struct video_i2c_data
*data
);
86 static int amg88xx_xfer(struct video_i2c_data
*data
, char *buf
)
88 struct i2c_client
*client
= data
->client
;
89 struct i2c_msg msg
[2];
93 msg
[0].addr
= client
->addr
;
96 msg
[0].buf
= (char *)®
;
98 msg
[1].addr
= client
->addr
;
99 msg
[1].flags
= I2C_M_RD
;
100 msg
[1].len
= data
->chip
->buffer_size
;
101 msg
[1].buf
= (char *)buf
;
103 ret
= i2c_transfer(client
->adapter
, msg
, 2);
105 return (ret
== 2) ? 0 : -EIO
;
108 #if IS_REACHABLE(CONFIG_HWMON)
110 static const u32 amg88xx_temp_config
[] = {
115 static const struct hwmon_channel_info amg88xx_temp
= {
117 .config
= amg88xx_temp_config
,
120 static const struct hwmon_channel_info
*amg88xx_info
[] = {
125 static umode_t
amg88xx_is_visible(const void *drvdata
,
126 enum hwmon_sensor_types type
,
127 u32 attr
, int channel
)
132 static int amg88xx_read(struct device
*dev
, enum hwmon_sensor_types type
,
133 u32 attr
, int channel
, long *val
)
135 struct video_i2c_data
*data
= dev_get_drvdata(dev
);
136 struct i2c_client
*client
= data
->client
;
137 int tmp
= i2c_smbus_read_word_data(client
, 0x0e);
143 * Check for sign bit, this isn't a two's complement value but an
144 * absolute temperature that needs to be inverted in the case of being
148 tmp
= -(tmp
& 0x7ff);
150 *val
= (tmp
* 625) / 10;
155 static const struct hwmon_ops amg88xx_hwmon_ops
= {
156 .is_visible
= amg88xx_is_visible
,
157 .read
= amg88xx_read
,
160 static const struct hwmon_chip_info amg88xx_chip_info
= {
161 .ops
= &amg88xx_hwmon_ops
,
162 .info
= amg88xx_info
,
165 static int amg88xx_hwmon_init(struct video_i2c_data
*data
)
167 void *hwmon
= devm_hwmon_device_register_with_info(&data
->client
->dev
,
168 "amg88xx", data
, &amg88xx_chip_info
, NULL
);
170 return PTR_ERR_OR_ZERO(hwmon
);
173 #define amg88xx_hwmon_init NULL
178 static const struct video_i2c_chip video_i2c_chip
[] = {
180 .size
= &amg88xx_size
,
181 .format
= &amg88xx_format
,
185 .xfer
= &amg88xx_xfer
,
186 .hwmon_init
= amg88xx_hwmon_init
,
190 static const struct v4l2_file_operations video_i2c_fops
= {
191 .owner
= THIS_MODULE
,
192 .open
= v4l2_fh_open
,
193 .release
= vb2_fop_release
,
194 .poll
= vb2_fop_poll
,
195 .read
= vb2_fop_read
,
196 .mmap
= vb2_fop_mmap
,
197 .unlocked_ioctl
= video_ioctl2
,
200 static int queue_setup(struct vb2_queue
*vq
,
201 unsigned int *nbuffers
, unsigned int *nplanes
,
202 unsigned int sizes
[], struct device
*alloc_devs
[])
204 struct video_i2c_data
*data
= vb2_get_drv_priv(vq
);
205 unsigned int size
= data
->chip
->buffer_size
;
207 if (vq
->num_buffers
+ *nbuffers
< 2)
211 return sizes
[0] < size
? -EINVAL
: 0;
219 static int buffer_prepare(struct vb2_buffer
*vb
)
221 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
222 struct video_i2c_data
*data
= vb2_get_drv_priv(vb
->vb2_queue
);
223 unsigned int size
= data
->chip
->buffer_size
;
225 if (vb2_plane_size(vb
, 0) < size
)
228 vbuf
->field
= V4L2_FIELD_NONE
;
229 vb2_set_plane_payload(vb
, 0, size
);
234 static void buffer_queue(struct vb2_buffer
*vb
)
236 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
237 struct video_i2c_data
*data
= vb2_get_drv_priv(vb
->vb2_queue
);
238 struct video_i2c_buffer
*buf
=
239 container_of(vbuf
, struct video_i2c_buffer
, vb
);
241 spin_lock(&data
->slock
);
242 list_add_tail(&buf
->list
, &data
->vid_cap_active
);
243 spin_unlock(&data
->slock
);
246 static int video_i2c_thread_vid_cap(void *priv
)
248 struct video_i2c_data
*data
= priv
;
249 unsigned int delay
= msecs_to_jiffies(1000 / data
->chip
->max_fps
);
254 unsigned long start_jiffies
= jiffies
;
255 struct video_i2c_buffer
*vid_cap_buf
= NULL
;
260 spin_lock(&data
->slock
);
262 if (!list_empty(&data
->vid_cap_active
)) {
263 vid_cap_buf
= list_last_entry(&data
->vid_cap_active
,
264 struct video_i2c_buffer
, list
);
265 list_del(&vid_cap_buf
->list
);
268 spin_unlock(&data
->slock
);
271 struct vb2_buffer
*vb2_buf
= &vid_cap_buf
->vb
.vb2_buf
;
272 void *vbuf
= vb2_plane_vaddr(vb2_buf
, 0);
275 ret
= data
->chip
->xfer(data
, vbuf
);
276 vb2_buf
->timestamp
= ktime_get_ns();
277 vid_cap_buf
->vb
.sequence
= data
->sequence
++;
278 vb2_buffer_done(vb2_buf
, ret
?
279 VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
282 schedule_delay
= delay
- (jiffies
- start_jiffies
);
284 if (time_after(jiffies
, start_jiffies
+ delay
))
285 schedule_delay
= delay
;
287 schedule_timeout_interruptible(schedule_delay
);
288 } while (!kthread_should_stop());
293 static void video_i2c_del_list(struct vb2_queue
*vq
, enum vb2_buffer_state state
)
295 struct video_i2c_data
*data
= vb2_get_drv_priv(vq
);
296 struct video_i2c_buffer
*buf
, *tmp
;
298 spin_lock(&data
->slock
);
300 list_for_each_entry_safe(buf
, tmp
, &data
->vid_cap_active
, list
) {
301 list_del(&buf
->list
);
302 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
305 spin_unlock(&data
->slock
);
308 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
310 struct video_i2c_data
*data
= vb2_get_drv_priv(vq
);
312 if (data
->kthread_vid_cap
)
316 data
->kthread_vid_cap
= kthread_run(video_i2c_thread_vid_cap
, data
,
317 "%s-vid-cap", data
->v4l2_dev
.name
);
318 if (!IS_ERR(data
->kthread_vid_cap
))
321 video_i2c_del_list(vq
, VB2_BUF_STATE_QUEUED
);
323 return PTR_ERR(data
->kthread_vid_cap
);
326 static void stop_streaming(struct vb2_queue
*vq
)
328 struct video_i2c_data
*data
= vb2_get_drv_priv(vq
);
330 if (data
->kthread_vid_cap
== NULL
)
333 kthread_stop(data
->kthread_vid_cap
);
334 data
->kthread_vid_cap
= NULL
;
336 video_i2c_del_list(vq
, VB2_BUF_STATE_ERROR
);
339 static struct vb2_ops video_i2c_video_qops
= {
340 .queue_setup
= queue_setup
,
341 .buf_prepare
= buffer_prepare
,
342 .buf_queue
= buffer_queue
,
343 .start_streaming
= start_streaming
,
344 .stop_streaming
= stop_streaming
,
345 .wait_prepare
= vb2_ops_wait_prepare
,
346 .wait_finish
= vb2_ops_wait_finish
,
349 static int video_i2c_querycap(struct file
*file
, void *priv
,
350 struct v4l2_capability
*vcap
)
352 struct video_i2c_data
*data
= video_drvdata(file
);
353 struct i2c_client
*client
= data
->client
;
355 strlcpy(vcap
->driver
, data
->v4l2_dev
.name
, sizeof(vcap
->driver
));
356 strlcpy(vcap
->card
, data
->vdev
.name
, sizeof(vcap
->card
));
358 sprintf(vcap
->bus_info
, "I2C:%d-%d", client
->adapter
->nr
, client
->addr
);
363 static int video_i2c_g_input(struct file
*file
, void *fh
, unsigned int *inp
)
370 static int video_i2c_s_input(struct file
*file
, void *fh
, unsigned int inp
)
372 return (inp
> 0) ? -EINVAL
: 0;
375 static int video_i2c_enum_input(struct file
*file
, void *fh
,
376 struct v4l2_input
*vin
)
381 strlcpy(vin
->name
, "Camera", sizeof(vin
->name
));
383 vin
->type
= V4L2_INPUT_TYPE_CAMERA
;
388 static int video_i2c_enum_fmt_vid_cap(struct file
*file
, void *fh
,
389 struct v4l2_fmtdesc
*fmt
)
391 struct video_i2c_data
*data
= video_drvdata(file
);
392 enum v4l2_buf_type type
= fmt
->type
;
397 *fmt
= *data
->chip
->format
;
403 static int video_i2c_enum_framesizes(struct file
*file
, void *fh
,
404 struct v4l2_frmsizeenum
*fsize
)
406 const struct video_i2c_data
*data
= video_drvdata(file
);
407 const struct v4l2_frmsize_discrete
*size
= data
->chip
->size
;
409 /* currently only one frame size is allowed */
410 if (fsize
->index
> 0)
413 if (fsize
->pixel_format
!= data
->chip
->format
->pixelformat
)
416 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
417 fsize
->discrete
.width
= size
->width
;
418 fsize
->discrete
.height
= size
->height
;
423 static int video_i2c_enum_frameintervals(struct file
*file
, void *priv
,
424 struct v4l2_frmivalenum
*fe
)
426 const struct video_i2c_data
*data
= video_drvdata(file
);
427 const struct v4l2_frmsize_discrete
*size
= data
->chip
->size
;
432 if (fe
->width
!= size
->width
|| fe
->height
!= size
->height
)
435 fe
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
436 fe
->discrete
.numerator
= 1;
437 fe
->discrete
.denominator
= data
->chip
->max_fps
;
442 static int video_i2c_try_fmt_vid_cap(struct file
*file
, void *fh
,
443 struct v4l2_format
*fmt
)
445 const struct video_i2c_data
*data
= video_drvdata(file
);
446 const struct v4l2_frmsize_discrete
*size
= data
->chip
->size
;
447 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
448 unsigned int bpp
= data
->chip
->bpp
/ 8;
450 pix
->width
= size
->width
;
451 pix
->height
= size
->height
;
452 pix
->pixelformat
= data
->chip
->format
->pixelformat
;
453 pix
->field
= V4L2_FIELD_NONE
;
454 pix
->bytesperline
= pix
->width
* bpp
;
455 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
456 pix
->colorspace
= V4L2_COLORSPACE_RAW
;
461 static int video_i2c_s_fmt_vid_cap(struct file
*file
, void *fh
,
462 struct v4l2_format
*fmt
)
464 struct video_i2c_data
*data
= video_drvdata(file
);
466 if (vb2_is_busy(&data
->vb_vidq
))
469 return video_i2c_try_fmt_vid_cap(file
, fh
, fmt
);
472 static int video_i2c_g_parm(struct file
*filp
, void *priv
,
473 struct v4l2_streamparm
*parm
)
475 struct video_i2c_data
*data
= video_drvdata(filp
);
477 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
480 parm
->parm
.capture
.readbuffers
= 1;
481 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
482 parm
->parm
.capture
.timeperframe
.numerator
= 1;
483 parm
->parm
.capture
.timeperframe
.denominator
= data
->chip
->max_fps
;
488 static const struct v4l2_ioctl_ops video_i2c_ioctl_ops
= {
489 .vidioc_querycap
= video_i2c_querycap
,
490 .vidioc_g_input
= video_i2c_g_input
,
491 .vidioc_s_input
= video_i2c_s_input
,
492 .vidioc_enum_input
= video_i2c_enum_input
,
493 .vidioc_enum_fmt_vid_cap
= video_i2c_enum_fmt_vid_cap
,
494 .vidioc_enum_framesizes
= video_i2c_enum_framesizes
,
495 .vidioc_enum_frameintervals
= video_i2c_enum_frameintervals
,
496 .vidioc_g_fmt_vid_cap
= video_i2c_try_fmt_vid_cap
,
497 .vidioc_s_fmt_vid_cap
= video_i2c_s_fmt_vid_cap
,
498 .vidioc_g_parm
= video_i2c_g_parm
,
499 .vidioc_s_parm
= video_i2c_g_parm
,
500 .vidioc_try_fmt_vid_cap
= video_i2c_try_fmt_vid_cap
,
501 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
502 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
503 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
504 .vidioc_querybuf
= vb2_ioctl_querybuf
,
505 .vidioc_qbuf
= vb2_ioctl_qbuf
,
506 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
507 .vidioc_streamon
= vb2_ioctl_streamon
,
508 .vidioc_streamoff
= vb2_ioctl_streamoff
,
511 static void video_i2c_release(struct video_device
*vdev
)
513 struct video_i2c_data
*data
= video_get_drvdata(vdev
);
515 v4l2_device_unregister(&data
->v4l2_dev
);
516 mutex_destroy(&data
->lock
);
517 mutex_destroy(&data
->queue_lock
);
521 static int video_i2c_probe(struct i2c_client
*client
,
522 const struct i2c_device_id
*id
)
524 struct video_i2c_data
*data
;
525 struct v4l2_device
*v4l2_dev
;
526 struct vb2_queue
*queue
;
529 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
533 if (dev_fwnode(&client
->dev
))
534 data
->chip
= device_get_match_data(&client
->dev
);
536 data
->chip
= &video_i2c_chip
[id
->driver_data
];
538 goto error_free_device
;
540 data
->client
= client
;
541 v4l2_dev
= &data
->v4l2_dev
;
542 strlcpy(v4l2_dev
->name
, VIDEO_I2C_DRIVER
, sizeof(v4l2_dev
->name
));
544 ret
= v4l2_device_register(&client
->dev
, v4l2_dev
);
546 goto error_free_device
;
548 mutex_init(&data
->lock
);
549 mutex_init(&data
->queue_lock
);
551 queue
= &data
->vb_vidq
;
552 queue
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
553 queue
->io_modes
= VB2_DMABUF
| VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
554 queue
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
555 queue
->drv_priv
= data
;
556 queue
->buf_struct_size
= sizeof(struct video_i2c_buffer
);
557 queue
->min_buffers_needed
= 1;
558 queue
->ops
= &video_i2c_video_qops
;
559 queue
->mem_ops
= &vb2_vmalloc_memops
;
561 ret
= vb2_queue_init(queue
);
563 goto error_unregister_device
;
565 data
->vdev
.queue
= queue
;
566 data
->vdev
.queue
->lock
= &data
->queue_lock
;
568 snprintf(data
->vdev
.name
, sizeof(data
->vdev
.name
),
569 "I2C %d-%d Transport Video",
570 client
->adapter
->nr
, client
->addr
);
572 data
->vdev
.v4l2_dev
= v4l2_dev
;
573 data
->vdev
.fops
= &video_i2c_fops
;
574 data
->vdev
.lock
= &data
->lock
;
575 data
->vdev
.ioctl_ops
= &video_i2c_ioctl_ops
;
576 data
->vdev
.release
= video_i2c_release
;
577 data
->vdev
.device_caps
= V4L2_CAP_VIDEO_CAPTURE
|
578 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
580 spin_lock_init(&data
->slock
);
581 INIT_LIST_HEAD(&data
->vid_cap_active
);
583 video_set_drvdata(&data
->vdev
, data
);
584 i2c_set_clientdata(client
, data
);
586 if (data
->chip
->hwmon_init
) {
587 ret
= data
->chip
->hwmon_init(data
);
589 dev_warn(&client
->dev
,
590 "failed to register hwmon device\n");
594 ret
= video_register_device(&data
->vdev
, VFL_TYPE_GRABBER
, -1);
596 goto error_unregister_device
;
600 error_unregister_device
:
601 v4l2_device_unregister(v4l2_dev
);
602 mutex_destroy(&data
->lock
);
603 mutex_destroy(&data
->queue_lock
);
611 static int video_i2c_remove(struct i2c_client
*client
)
613 struct video_i2c_data
*data
= i2c_get_clientdata(client
);
615 video_unregister_device(&data
->vdev
);
620 static const struct i2c_device_id video_i2c_id_table
[] = {
621 { "amg88xx", AMG88XX
},
624 MODULE_DEVICE_TABLE(i2c
, video_i2c_id_table
);
626 static const struct of_device_id video_i2c_of_match
[] = {
627 { .compatible
= "panasonic,amg88xx", .data
= &video_i2c_chip
[AMG88XX
] },
630 MODULE_DEVICE_TABLE(of
, video_i2c_of_match
);
632 static struct i2c_driver video_i2c_driver
= {
634 .name
= VIDEO_I2C_DRIVER
,
635 .of_match_table
= video_i2c_of_match
,
637 .probe
= video_i2c_probe
,
638 .remove
= video_i2c_remove
,
639 .id_table
= video_i2c_id_table
,
642 module_i2c_driver(video_i2c_driver
);
644 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
645 MODULE_DESCRIPTION("I2C transport video support");
646 MODULE_LICENSE("GPL v2");