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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/media/i2c/video-i2c.c