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