blob: de12f38f347ce248034f8c0ba22c63289106a704 [file] [log] [blame]
Matt Ranostay5cebaac2018-04-06 18:52:31 -04001// 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
Matt Ranostay8866cfb2018-12-11 13:17:01 -02009 * - Melexis MLX90640 Thermal Cameras
Matt Ranostay5cebaac2018-04-06 18:52:31 -040010 */
11
12#include <linux/delay.h>
13#include <linux/freezer.h>
Matt Ranostayacbea672018-06-28 14:11:04 -040014#include <linux/hwmon.h>
Matt Ranostay5cebaac2018-04-06 18:52:31 -040015#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>
Akinobu Mita69d2a7342018-11-22 11:13:56 -050021#include <linux/pm_runtime.h>
Matt Ranostay8866cfb2018-12-11 13:17:01 -020022#include <linux/nvmem-provider.h>
Akinobu Mitaae9e1962018-10-20 10:26:24 -040023#include <linux/regmap.h>
Matt Ranostay5cebaac2018-04-06 18:52:31 -040024#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
37struct video_i2c_chip;
38
39struct video_i2c_buffer {
40 struct vb2_v4l2_buffer vb;
41 struct list_head list;
42};
43
44struct video_i2c_data {
Akinobu Mitaae9e1962018-10-20 10:26:24 -040045 struct regmap *regmap;
Matt Ranostay5cebaac2018-04-06 18:52:31 -040046 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;
Akinobu Mita56281022018-10-20 10:26:27 -040058
59 struct v4l2_fract frame_interval;
Matt Ranostay5cebaac2018-04-06 18:52:31 -040060};
61
Mauro Carvalho Chehab1b3d5f22018-05-04 10:18:05 -040062static const struct v4l2_fmtdesc amg88xx_format = {
Matt Ranostay5cebaac2018-04-06 18:52:31 -040063 .pixelformat = V4L2_PIX_FMT_Y12,
64};
65
Mauro Carvalho Chehab1b3d5f22018-05-04 10:18:05 -040066static const struct v4l2_frmsize_discrete amg88xx_size = {
Matt Ranostay5cebaac2018-04-06 18:52:31 -040067 .width = 8,
68 .height = 8,
69};
70
Matt Ranostay8866cfb2018-12-11 13:17:01 -020071static const struct v4l2_fmtdesc mlx90640_format = {
72 .pixelformat = V4L2_PIX_FMT_Y16_BE,
73};
74
75static 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
Akinobu Mitaae9e1962018-10-20 10:26:24 -040080static const struct regmap_config amg88xx_regmap_config = {
81 .reg_bits = 8,
82 .val_bits = 8,
83 .max_register = 0xff
84};
85
Matt Ranostay8866cfb2018-12-11 13:17:01 -020086static const struct regmap_config mlx90640_regmap_config = {
87 .reg_bits = 16,
88 .val_bits = 16,
89};
90
Matt Ranostay5cebaac2018-04-06 18:52:31 -040091struct video_i2c_chip {
92 /* video dimensions */
93 const struct v4l2_fmtdesc *format;
94 const struct v4l2_frmsize_discrete *size;
95
Akinobu Mita56281022018-10-20 10:26:27 -040096 /* available frame intervals */
97 const struct v4l2_fract *frame_intervals;
98 unsigned int num_frame_intervals;
Matt Ranostay5cebaac2018-04-06 18:52:31 -040099
100 /* pixel buffer size */
101 unsigned int buffer_size;
102
103 /* pixel size in bits */
104 unsigned int bpp;
105
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400106 const struct regmap_config *regmap_config;
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200107 struct nvmem_config *nvmem_config;
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400108
Akinobu Mita56281022018-10-20 10:26:27 -0400109 /* setup function */
110 int (*setup)(struct video_i2c_data *data);
111
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400112 /* xfer function */
113 int (*xfer)(struct video_i2c_data *data, char *buf);
Matt Ranostayacbea672018-06-28 14:11:04 -0400114
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500115 /* power control function */
116 int (*set_power)(struct video_i2c_data *data, bool on);
117
Matt Ranostayacbea672018-06-28 14:11:04 -0400118 /* hwmon init function */
119 int (*hwmon_init)(struct video_i2c_data *data);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400120};
121
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200122static 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
130static 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
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500138/* 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
Akinobu Mita56281022018-10-20 10:26:27 -0400148/* Frame rate register */
149#define AMG88XX_REG_FPSC 0x02
150#define AMG88XX_FPSC_1FPS BIT(0)
151
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400152/* Thermistor register */
153#define AMG88XX_REG_TTHL 0x0e
154
155/* Temperature register */
156#define AMG88XX_REG_T01L 0x80
157
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200158/* Control register */
159#define MLX90640_REG_CTL1 0x800d
160#define MLX90640_REG_CTL1_MASK 0x0380
161#define MLX90640_REG_CTL1_MASK_SHIFT 7
162
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400163static int amg88xx_xfer(struct video_i2c_data *data, char *buf)
164{
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400165 return regmap_bulk_read(data->regmap, AMG88XX_REG_T01L, buf,
166 data->chip->buffer_size);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400167}
168
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200169static 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
Akinobu Mita56281022018-10-20 10:26:27 -0400175static 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
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200188static 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++) {
Akinobu Mita94954bb2019-06-15 11:00:57 -0400193 if (V4L2_FRACT_COMPARE(data->frame_interval, ==,
194 data->chip->frame_intervals[n]))
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200195 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
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500205static 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
233static 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
250static 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
Matt Ranostay64d4fc92020-03-24 02:07:41 +0100258#if IS_REACHABLE(CONFIG_HWMON)
Matt Ranostayacbea672018-06-28 14:11:04 -0400259
260static const u32 amg88xx_temp_config[] = {
261 HWMON_T_INPUT,
262 0
263};
264
265static const struct hwmon_channel_info amg88xx_temp = {
266 .type = hwmon_temp,
267 .config = amg88xx_temp_config,
268};
269
270static const struct hwmon_channel_info *amg88xx_info[] = {
271 &amg88xx_temp,
272 NULL
273};
274
275static 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
282static 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);
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400286 __le16 buf;
287 int tmp;
Matt Ranostayacbea672018-06-28 14:11:04 -0400288
Mauro Carvalho Chehaba959a7b2021-04-23 17:19:17 +0200289 tmp = pm_runtime_resume_and_get(regmap_get_device(data->regmap));
290 if (tmp < 0)
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500291 return tmp;
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500292
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400293 tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2);
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500294 pm_runtime_mark_last_busy(regmap_get_device(data->regmap));
295 pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400296 if (tmp)
Matt Ranostayacbea672018-06-28 14:11:04 -0400297 return tmp;
298
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400299 tmp = le16_to_cpu(buf);
300
Matt Ranostayacbea672018-06-28 14:11:04 -0400301 /*
302 * Check for sign bit, this isn't a two's complement value but an
303 * absolute temperature that needs to be inverted in the case of being
304 * negative.
305 */
306 if (tmp & BIT(11))
307 tmp = -(tmp & 0x7ff);
308
309 *val = (tmp * 625) / 10;
310
311 return 0;
312}
313
314static const struct hwmon_ops amg88xx_hwmon_ops = {
315 .is_visible = amg88xx_is_visible,
316 .read = amg88xx_read,
317};
318
319static const struct hwmon_chip_info amg88xx_chip_info = {
320 .ops = &amg88xx_hwmon_ops,
321 .info = amg88xx_info,
322};
323
324static int amg88xx_hwmon_init(struct video_i2c_data *data)
325{
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400326 struct device *dev = regmap_get_device(data->regmap);
327 void *hwmon = devm_hwmon_device_register_with_info(dev, "amg88xx", data,
328 &amg88xx_chip_info, NULL);
Matt Ranostayacbea672018-06-28 14:11:04 -0400329
Matt Ranostay17f330c2018-07-26 21:47:24 -0400330 return PTR_ERR_OR_ZERO(hwmon);
Matt Ranostayacbea672018-06-28 14:11:04 -0400331}
332#else
333#define amg88xx_hwmon_init NULL
334#endif
335
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200336enum {
337 AMG88XX,
338 MLX90640,
339};
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400340
Akinobu Mita56281022018-10-20 10:26:27 -0400341static const struct v4l2_fract amg88xx_frame_intervals[] = {
342 { 1, 10 },
343 { 1, 1 },
344};
345
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200346static const struct v4l2_fract mlx90640_frame_intervals[] = {
347 { 1, 64 },
348 { 1, 32 },
349 { 1, 16 },
350 { 1, 8 },
351 { 1, 4 },
352 { 1, 2 },
353 { 1, 1 },
354 { 2, 1 },
355};
356
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400357static const struct video_i2c_chip video_i2c_chip[] = {
358 [AMG88XX] = {
359 .size = &amg88xx_size,
360 .format = &amg88xx_format,
Akinobu Mita56281022018-10-20 10:26:27 -0400361 .frame_intervals = amg88xx_frame_intervals,
362 .num_frame_intervals = ARRAY_SIZE(amg88xx_frame_intervals),
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400363 .buffer_size = 128,
364 .bpp = 16,
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400365 .regmap_config = &amg88xx_regmap_config,
Akinobu Mita56281022018-10-20 10:26:27 -0400366 .setup = &amg88xx_setup,
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400367 .xfer = &amg88xx_xfer,
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500368 .set_power = amg88xx_set_power,
Matt Ranostayacbea672018-06-28 14:11:04 -0400369 .hwmon_init = amg88xx_hwmon_init,
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400370 },
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200371 [MLX90640] = {
372 .size = &mlx90640_size,
373 .format = &mlx90640_format,
374 .frame_intervals = mlx90640_frame_intervals,
375 .num_frame_intervals = ARRAY_SIZE(mlx90640_frame_intervals),
376 .buffer_size = 1664,
377 .bpp = 16,
378 .regmap_config = &mlx90640_regmap_config,
379 .nvmem_config = &mlx90640_nvram_config,
380 .setup = mlx90640_setup,
381 .xfer = mlx90640_xfer,
382 },
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400383};
384
385static const struct v4l2_file_operations video_i2c_fops = {
386 .owner = THIS_MODULE,
387 .open = v4l2_fh_open,
388 .release = vb2_fop_release,
389 .poll = vb2_fop_poll,
390 .read = vb2_fop_read,
391 .mmap = vb2_fop_mmap,
392 .unlocked_ioctl = video_ioctl2,
393};
394
395static int queue_setup(struct vb2_queue *vq,
396 unsigned int *nbuffers, unsigned int *nplanes,
397 unsigned int sizes[], struct device *alloc_devs[])
398{
399 struct video_i2c_data *data = vb2_get_drv_priv(vq);
400 unsigned int size = data->chip->buffer_size;
401
402 if (vq->num_buffers + *nbuffers < 2)
403 *nbuffers = 2;
404
405 if (*nplanes)
406 return sizes[0] < size ? -EINVAL : 0;
407
408 *nplanes = 1;
409 sizes[0] = size;
410
411 return 0;
412}
413
414static int buffer_prepare(struct vb2_buffer *vb)
415{
416 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
417 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
418 unsigned int size = data->chip->buffer_size;
419
420 if (vb2_plane_size(vb, 0) < size)
421 return -EINVAL;
422
423 vbuf->field = V4L2_FIELD_NONE;
424 vb2_set_plane_payload(vb, 0, size);
425
426 return 0;
427}
428
429static void buffer_queue(struct vb2_buffer *vb)
430{
431 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
432 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
433 struct video_i2c_buffer *buf =
434 container_of(vbuf, struct video_i2c_buffer, vb);
435
436 spin_lock(&data->slock);
437 list_add_tail(&buf->list, &data->vid_cap_active);
438 spin_unlock(&data->slock);
439}
440
441static int video_i2c_thread_vid_cap(void *priv)
442{
443 struct video_i2c_data *data = priv;
Akinobu Mita56281022018-10-20 10:26:27 -0400444 unsigned int delay = mult_frac(HZ, data->frame_interval.numerator,
445 data->frame_interval.denominator);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400446
447 set_freezable();
448
449 do {
450 unsigned long start_jiffies = jiffies;
451 struct video_i2c_buffer *vid_cap_buf = NULL;
452 int schedule_delay;
453
454 try_to_freeze();
455
456 spin_lock(&data->slock);
457
458 if (!list_empty(&data->vid_cap_active)) {
459 vid_cap_buf = list_last_entry(&data->vid_cap_active,
460 struct video_i2c_buffer, list);
461 list_del(&vid_cap_buf->list);
462 }
463
464 spin_unlock(&data->slock);
465
466 if (vid_cap_buf) {
467 struct vb2_buffer *vb2_buf = &vid_cap_buf->vb.vb2_buf;
468 void *vbuf = vb2_plane_vaddr(vb2_buf, 0);
469 int ret;
470
471 ret = data->chip->xfer(data, vbuf);
472 vb2_buf->timestamp = ktime_get_ns();
473 vid_cap_buf->vb.sequence = data->sequence++;
474 vb2_buffer_done(vb2_buf, ret ?
475 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
476 }
477
478 schedule_delay = delay - (jiffies - start_jiffies);
479
480 if (time_after(jiffies, start_jiffies + delay))
481 schedule_delay = delay;
482
483 schedule_timeout_interruptible(schedule_delay);
484 } while (!kthread_should_stop());
485
486 return 0;
487}
488
489static 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
504static int start_streaming(struct vb2_queue *vq, unsigned int count)
505{
506 struct video_i2c_data *data = vb2_get_drv_priv(vq);
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500507 struct device *dev = regmap_get_device(data->regmap);
Akinobu Mita56281022018-10-20 10:26:27 -0400508 int ret;
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400509
510 if (data->kthread_vid_cap)
511 return 0;
512
Mauro Carvalho Chehaba959a7b2021-04-23 17:19:17 +0200513 ret = pm_runtime_resume_and_get(dev);
514 if (ret < 0)
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500515 goto error_del_list;
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500516
Akinobu Mita56281022018-10-20 10:26:27 -0400517 ret = data->chip->setup(data);
518 if (ret)
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500519 goto error_rpm_put;
Akinobu Mita56281022018-10-20 10:26:27 -0400520
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400521 data->sequence = 0;
522 data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data,
523 "%s-vid-cap", data->v4l2_dev.name);
Akinobu Mita56281022018-10-20 10:26:27 -0400524 ret = PTR_ERR_OR_ZERO(data->kthread_vid_cap);
525 if (!ret)
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400526 return 0;
527
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500528error_rpm_put:
529 pm_runtime_mark_last_busy(dev);
530 pm_runtime_put_autosuspend(dev);
Akinobu Mita56281022018-10-20 10:26:27 -0400531error_del_list:
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400532 video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED);
533
Akinobu Mita56281022018-10-20 10:26:27 -0400534 return ret;
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400535}
536
537static void stop_streaming(struct vb2_queue *vq)
538{
539 struct video_i2c_data *data = vb2_get_drv_priv(vq);
540
541 if (data->kthread_vid_cap == NULL)
542 return;
543
544 kthread_stop(data->kthread_vid_cap);
545 data->kthread_vid_cap = NULL;
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500546 pm_runtime_mark_last_busy(regmap_get_device(data->regmap));
547 pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400548
549 video_i2c_del_list(vq, VB2_BUF_STATE_ERROR);
550}
551
Julia Lawallc7f7da22018-10-30 11:31:22 -0400552static const struct vb2_ops video_i2c_video_qops = {
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400553 .queue_setup = queue_setup,
554 .buf_prepare = buffer_prepare,
555 .buf_queue = buffer_queue,
556 .start_streaming = start_streaming,
557 .stop_streaming = stop_streaming,
558 .wait_prepare = vb2_ops_wait_prepare,
559 .wait_finish = vb2_ops_wait_finish,
560};
561
562static int video_i2c_querycap(struct file *file, void *priv,
563 struct v4l2_capability *vcap)
564{
565 struct video_i2c_data *data = video_drvdata(file);
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400566 struct device *dev = regmap_get_device(data->regmap);
567 struct i2c_client *client = to_i2c_client(dev);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400568
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400569 strscpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver));
570 strscpy(vcap->card, data->vdev.name, sizeof(vcap->card));
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400571
572 sprintf(vcap->bus_info, "I2C:%d-%d", client->adapter->nr, client->addr);
573
574 return 0;
575}
576
577static int video_i2c_g_input(struct file *file, void *fh, unsigned int *inp)
578{
579 *inp = 0;
580
581 return 0;
582}
583
584static int video_i2c_s_input(struct file *file, void *fh, unsigned int inp)
585{
586 return (inp > 0) ? -EINVAL : 0;
587}
588
589static int video_i2c_enum_input(struct file *file, void *fh,
590 struct v4l2_input *vin)
591{
592 if (vin->index > 0)
593 return -EINVAL;
594
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400595 strscpy(vin->name, "Camera", sizeof(vin->name));
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400596
597 vin->type = V4L2_INPUT_TYPE_CAMERA;
598
599 return 0;
600}
601
602static int video_i2c_enum_fmt_vid_cap(struct file *file, void *fh,
603 struct v4l2_fmtdesc *fmt)
604{
605 struct video_i2c_data *data = video_drvdata(file);
606 enum v4l2_buf_type type = fmt->type;
607
608 if (fmt->index > 0)
609 return -EINVAL;
610
611 *fmt = *data->chip->format;
612 fmt->type = type;
613
614 return 0;
615}
616
617static int video_i2c_enum_framesizes(struct file *file, void *fh,
618 struct v4l2_frmsizeenum *fsize)
619{
620 const struct video_i2c_data *data = video_drvdata(file);
621 const struct v4l2_frmsize_discrete *size = data->chip->size;
622
623 /* currently only one frame size is allowed */
624 if (fsize->index > 0)
625 return -EINVAL;
626
627 if (fsize->pixel_format != data->chip->format->pixelformat)
628 return -EINVAL;
629
630 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
631 fsize->discrete.width = size->width;
632 fsize->discrete.height = size->height;
633
634 return 0;
635}
636
637static int video_i2c_enum_frameintervals(struct file *file, void *priv,
638 struct v4l2_frmivalenum *fe)
639{
640 const struct video_i2c_data *data = video_drvdata(file);
641 const struct v4l2_frmsize_discrete *size = data->chip->size;
642
Akinobu Mita56281022018-10-20 10:26:27 -0400643 if (fe->index >= data->chip->num_frame_intervals)
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400644 return -EINVAL;
645
646 if (fe->width != size->width || fe->height != size->height)
647 return -EINVAL;
648
649 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
Akinobu Mita56281022018-10-20 10:26:27 -0400650 fe->discrete = data->chip->frame_intervals[fe->index];
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400651
652 return 0;
653}
654
655static int video_i2c_try_fmt_vid_cap(struct file *file, void *fh,
656 struct v4l2_format *fmt)
657{
658 const struct video_i2c_data *data = video_drvdata(file);
659 const struct v4l2_frmsize_discrete *size = data->chip->size;
660 struct v4l2_pix_format *pix = &fmt->fmt.pix;
661 unsigned int bpp = data->chip->bpp / 8;
662
663 pix->width = size->width;
664 pix->height = size->height;
665 pix->pixelformat = data->chip->format->pixelformat;
666 pix->field = V4L2_FIELD_NONE;
667 pix->bytesperline = pix->width * bpp;
668 pix->sizeimage = pix->bytesperline * pix->height;
669 pix->colorspace = V4L2_COLORSPACE_RAW;
670
671 return 0;
672}
673
674static int video_i2c_s_fmt_vid_cap(struct file *file, void *fh,
675 struct v4l2_format *fmt)
676{
677 struct video_i2c_data *data = video_drvdata(file);
678
679 if (vb2_is_busy(&data->vb_vidq))
680 return -EBUSY;
681
682 return video_i2c_try_fmt_vid_cap(file, fh, fmt);
683}
684
685static int video_i2c_g_parm(struct file *filp, void *priv,
686 struct v4l2_streamparm *parm)
687{
688 struct video_i2c_data *data = video_drvdata(filp);
689
690 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
691 return -EINVAL;
692
693 parm->parm.capture.readbuffers = 1;
694 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
Akinobu Mita56281022018-10-20 10:26:27 -0400695 parm->parm.capture.timeperframe = data->frame_interval;
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400696
697 return 0;
698}
699
Akinobu Mita56281022018-10-20 10:26:27 -0400700static int video_i2c_s_parm(struct file *filp, void *priv,
701 struct v4l2_streamparm *parm)
702{
703 struct video_i2c_data *data = video_drvdata(filp);
704 int i;
705
706 for (i = 0; i < data->chip->num_frame_intervals - 1; i++) {
707 if (V4L2_FRACT_COMPARE(parm->parm.capture.timeperframe, <=,
708 data->chip->frame_intervals[i]))
709 break;
710 }
711 data->frame_interval = data->chip->frame_intervals[i];
712
713 return video_i2c_g_parm(filp, priv, parm);
714}
715
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400716static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = {
717 .vidioc_querycap = video_i2c_querycap,
718 .vidioc_g_input = video_i2c_g_input,
719 .vidioc_s_input = video_i2c_s_input,
720 .vidioc_enum_input = video_i2c_enum_input,
721 .vidioc_enum_fmt_vid_cap = video_i2c_enum_fmt_vid_cap,
722 .vidioc_enum_framesizes = video_i2c_enum_framesizes,
723 .vidioc_enum_frameintervals = video_i2c_enum_frameintervals,
724 .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
725 .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap,
726 .vidioc_g_parm = video_i2c_g_parm,
Akinobu Mita56281022018-10-20 10:26:27 -0400727 .vidioc_s_parm = video_i2c_s_parm,
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400728 .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
729 .vidioc_reqbufs = vb2_ioctl_reqbufs,
730 .vidioc_create_bufs = vb2_ioctl_create_bufs,
731 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
732 .vidioc_querybuf = vb2_ioctl_querybuf,
733 .vidioc_qbuf = vb2_ioctl_qbuf,
734 .vidioc_dqbuf = vb2_ioctl_dqbuf,
735 .vidioc_streamon = vb2_ioctl_streamon,
736 .vidioc_streamoff = vb2_ioctl_streamoff,
737};
738
739static void video_i2c_release(struct video_device *vdev)
740{
Akinobu Mitac764da92018-10-20 10:26:23 -0400741 struct video_i2c_data *data = video_get_drvdata(vdev);
742
743 v4l2_device_unregister(&data->v4l2_dev);
744 mutex_destroy(&data->lock);
745 mutex_destroy(&data->queue_lock);
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400746 regmap_exit(data->regmap);
Akinobu Mitac764da92018-10-20 10:26:23 -0400747 kfree(data);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400748}
749
750static int video_i2c_probe(struct i2c_client *client,
751 const struct i2c_device_id *id)
752{
753 struct video_i2c_data *data;
754 struct v4l2_device *v4l2_dev;
755 struct vb2_queue *queue;
756 int ret = -ENODEV;
757
758 data = kzalloc(sizeof(*data), GFP_KERNEL);
759 if (!data)
760 return -ENOMEM;
761
762 if (dev_fwnode(&client->dev))
763 data->chip = device_get_match_data(&client->dev);
764 else if (id)
765 data->chip = &video_i2c_chip[id->driver_data];
766 else
767 goto error_free_device;
768
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400769 data->regmap = regmap_init_i2c(client, data->chip->regmap_config);
770 if (IS_ERR(data->regmap)) {
771 ret = PTR_ERR(data->regmap);
772 goto error_free_device;
773 }
774
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400775 v4l2_dev = &data->v4l2_dev;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400776 strscpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name));
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400777
778 ret = v4l2_device_register(&client->dev, v4l2_dev);
779 if (ret < 0)
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400780 goto error_regmap_exit;
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400781
782 mutex_init(&data->lock);
783 mutex_init(&data->queue_lock);
784
785 queue = &data->vb_vidq;
786 queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
787 queue->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR | VB2_READ;
788 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
789 queue->drv_priv = data;
790 queue->buf_struct_size = sizeof(struct video_i2c_buffer);
791 queue->min_buffers_needed = 1;
792 queue->ops = &video_i2c_video_qops;
793 queue->mem_ops = &vb2_vmalloc_memops;
794
795 ret = vb2_queue_init(queue);
796 if (ret < 0)
797 goto error_unregister_device;
798
799 data->vdev.queue = queue;
800 data->vdev.queue->lock = &data->queue_lock;
801
802 snprintf(data->vdev.name, sizeof(data->vdev.name),
803 "I2C %d-%d Transport Video",
804 client->adapter->nr, client->addr);
805
806 data->vdev.v4l2_dev = v4l2_dev;
807 data->vdev.fops = &video_i2c_fops;
808 data->vdev.lock = &data->lock;
809 data->vdev.ioctl_ops = &video_i2c_ioctl_ops;
810 data->vdev.release = video_i2c_release;
811 data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
812 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
813
814 spin_lock_init(&data->slock);
815 INIT_LIST_HEAD(&data->vid_cap_active);
816
Akinobu Mita56281022018-10-20 10:26:27 -0400817 data->frame_interval = data->chip->frame_intervals[0];
818
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400819 video_set_drvdata(&data->vdev, data);
820 i2c_set_clientdata(client, data);
821
Matt Ranostayac11da42018-11-24 17:03:23 -0500822 if (data->chip->set_power) {
823 ret = data->chip->set_power(data, true);
824 if (ret)
825 goto error_unregister_device;
826 }
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500827
828 pm_runtime_get_noresume(&client->dev);
829 pm_runtime_set_active(&client->dev);
830 pm_runtime_enable(&client->dev);
831 pm_runtime_set_autosuspend_delay(&client->dev, 2000);
832 pm_runtime_use_autosuspend(&client->dev);
833
Matt Ranostayacbea672018-06-28 14:11:04 -0400834 if (data->chip->hwmon_init) {
835 ret = data->chip->hwmon_init(data);
836 if (ret < 0) {
837 dev_warn(&client->dev,
838 "failed to register hwmon device\n");
839 }
840 }
841
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200842 if (data->chip->nvmem_config) {
843 struct nvmem_config *config = data->chip->nvmem_config;
844 struct nvmem_device *device;
845
846 config->priv = data;
847 config->dev = &client->dev;
848
849 device = devm_nvmem_register(&client->dev, config);
850
851 if (IS_ERR(device)) {
852 dev_warn(&client->dev,
853 "failed to register nvmem device\n");
854 }
855 }
856
Hans Verkuil63479532020-02-03 12:41:16 +0100857 ret = video_register_device(&data->vdev, VFL_TYPE_VIDEO, -1);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400858 if (ret < 0)
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500859 goto error_pm_disable;
860
861 pm_runtime_mark_last_busy(&client->dev);
862 pm_runtime_put_autosuspend(&client->dev);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400863
864 return 0;
865
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500866error_pm_disable:
867 pm_runtime_disable(&client->dev);
868 pm_runtime_set_suspended(&client->dev);
869 pm_runtime_put_noidle(&client->dev);
Matt Ranostayac11da42018-11-24 17:03:23 -0500870
871 if (data->chip->set_power)
872 data->chip->set_power(data, false);
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500873
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400874error_unregister_device:
875 v4l2_device_unregister(v4l2_dev);
876 mutex_destroy(&data->lock);
877 mutex_destroy(&data->queue_lock);
878
Akinobu Mitaae9e1962018-10-20 10:26:24 -0400879error_regmap_exit:
880 regmap_exit(data->regmap);
881
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400882error_free_device:
883 kfree(data);
884
885 return ret;
886}
887
888static int video_i2c_remove(struct i2c_client *client)
889{
890 struct video_i2c_data *data = i2c_get_clientdata(client);
891
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500892 pm_runtime_get_sync(&client->dev);
893 pm_runtime_disable(&client->dev);
894 pm_runtime_set_suspended(&client->dev);
895 pm_runtime_put_noidle(&client->dev);
Matt Ranostayac11da42018-11-24 17:03:23 -0500896
897 if (data->chip->set_power)
898 data->chip->set_power(data, false);
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500899
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400900 video_unregister_device(&data->vdev);
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400901
902 return 0;
903}
904
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500905#ifdef CONFIG_PM
906
907static int video_i2c_pm_runtime_suspend(struct device *dev)
908{
909 struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev));
910
Matt Ranostayac11da42018-11-24 17:03:23 -0500911 if (!data->chip->set_power)
912 return 0;
913
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500914 return data->chip->set_power(data, false);
915}
916
917static int video_i2c_pm_runtime_resume(struct device *dev)
918{
919 struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev));
920
Matt Ranostayac11da42018-11-24 17:03:23 -0500921 if (!data->chip->set_power)
922 return 0;
923
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500924 return data->chip->set_power(data, true);
925}
926
927#endif
928
929static const struct dev_pm_ops video_i2c_pm_ops = {
930 SET_RUNTIME_PM_OPS(video_i2c_pm_runtime_suspend,
931 video_i2c_pm_runtime_resume, NULL)
932};
933
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400934static const struct i2c_device_id video_i2c_id_table[] = {
935 { "amg88xx", AMG88XX },
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200936 { "mlx90640", MLX90640 },
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400937 {}
938};
939MODULE_DEVICE_TABLE(i2c, video_i2c_id_table);
940
941static const struct of_device_id video_i2c_of_match[] = {
942 { .compatible = "panasonic,amg88xx", .data = &video_i2c_chip[AMG88XX] },
Matt Ranostay8866cfb2018-12-11 13:17:01 -0200943 { .compatible = "melexis,mlx90640", .data = &video_i2c_chip[MLX90640] },
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400944 {}
945};
946MODULE_DEVICE_TABLE(of, video_i2c_of_match);
947
948static struct i2c_driver video_i2c_driver = {
949 .driver = {
950 .name = VIDEO_I2C_DRIVER,
951 .of_match_table = video_i2c_of_match,
Akinobu Mita69d2a7342018-11-22 11:13:56 -0500952 .pm = &video_i2c_pm_ops,
Matt Ranostay5cebaac2018-04-06 18:52:31 -0400953 },
954 .probe = video_i2c_probe,
955 .remove = video_i2c_remove,
956 .id_table = video_i2c_id_table,
957};
958
959module_i2c_driver(video_i2c_driver);
960
961MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
962MODULE_DESCRIPTION("I2C transport video support");
963MODULE_LICENSE("GPL v2");