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