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