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