Commit | Line | Data |
---|---|---|
5cebaac6 MR |
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 | */ | |
10 | ||
11 | #include <linux/delay.h> | |
12 | #include <linux/freezer.h> | |
acbea679 | 13 | #include <linux/hwmon.h> |
5cebaac6 MR |
14 | #include <linux/kthread.h> |
15 | #include <linux/i2c.h> | |
16 | #include <linux/list.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/mutex.h> | |
19 | #include <linux/of_device.h> | |
ae9e196d | 20 | #include <linux/regmap.h> |
5cebaac6 MR |
21 | #include <linux/sched.h> |
22 | #include <linux/slab.h> | |
23 | #include <linux/videodev2.h> | |
24 | #include <media/v4l2-common.h> | |
25 | #include <media/v4l2-device.h> | |
26 | #include <media/v4l2-event.h> | |
27 | #include <media/v4l2-fh.h> | |
28 | #include <media/v4l2-ioctl.h> | |
29 | #include <media/videobuf2-v4l2.h> | |
30 | #include <media/videobuf2-vmalloc.h> | |
31 | ||
32 | #define VIDEO_I2C_DRIVER "video-i2c" | |
33 | ||
34 | struct video_i2c_chip; | |
35 | ||
36 | struct video_i2c_buffer { | |
37 | struct vb2_v4l2_buffer vb; | |
38 | struct list_head list; | |
39 | }; | |
40 | ||
41 | struct video_i2c_data { | |
ae9e196d | 42 | struct regmap *regmap; |
5cebaac6 MR |
43 | const struct video_i2c_chip *chip; |
44 | struct mutex lock; | |
45 | spinlock_t slock; | |
46 | unsigned int sequence; | |
47 | struct mutex queue_lock; | |
48 | ||
49 | struct v4l2_device v4l2_dev; | |
50 | struct video_device vdev; | |
51 | struct vb2_queue vb_vidq; | |
52 | ||
53 | struct task_struct *kthread_vid_cap; | |
54 | struct list_head vid_cap_active; | |
56281021 AM |
55 | |
56 | struct v4l2_fract frame_interval; | |
5cebaac6 MR |
57 | }; |
58 | ||
1b3d5f2a | 59 | static const struct v4l2_fmtdesc amg88xx_format = { |
5cebaac6 MR |
60 | .pixelformat = V4L2_PIX_FMT_Y12, |
61 | }; | |
62 | ||
1b3d5f2a | 63 | static const struct v4l2_frmsize_discrete amg88xx_size = { |
5cebaac6 MR |
64 | .width = 8, |
65 | .height = 8, | |
66 | }; | |
67 | ||
ae9e196d AM |
68 | static const struct regmap_config amg88xx_regmap_config = { |
69 | .reg_bits = 8, | |
70 | .val_bits = 8, | |
71 | .max_register = 0xff | |
72 | }; | |
73 | ||
5cebaac6 MR |
74 | struct video_i2c_chip { |
75 | /* video dimensions */ | |
76 | const struct v4l2_fmtdesc *format; | |
77 | const struct v4l2_frmsize_discrete *size; | |
78 | ||
56281021 AM |
79 | /* available frame intervals */ |
80 | const struct v4l2_fract *frame_intervals; | |
81 | unsigned int num_frame_intervals; | |
5cebaac6 MR |
82 | |
83 | /* pixel buffer size */ | |
84 | unsigned int buffer_size; | |
85 | ||
86 | /* pixel size in bits */ | |
87 | unsigned int bpp; | |
88 | ||
ae9e196d AM |
89 | const struct regmap_config *regmap_config; |
90 | ||
56281021 AM |
91 | /* setup function */ |
92 | int (*setup)(struct video_i2c_data *data); | |
93 | ||
5cebaac6 MR |
94 | /* xfer function */ |
95 | int (*xfer)(struct video_i2c_data *data, char *buf); | |
acbea679 MR |
96 | |
97 | /* hwmon init function */ | |
98 | int (*hwmon_init)(struct video_i2c_data *data); | |
5cebaac6 MR |
99 | }; |
100 | ||
56281021 AM |
101 | /* Frame rate register */ |
102 | #define AMG88XX_REG_FPSC 0x02 | |
103 | #define AMG88XX_FPSC_1FPS BIT(0) | |
104 | ||
ae9e196d AM |
105 | /* Thermistor register */ |
106 | #define AMG88XX_REG_TTHL 0x0e | |
5cebaac6 | 107 | |
ae9e196d AM |
108 | /* Temperature register */ |
109 | #define AMG88XX_REG_T01L 0x80 | |
5cebaac6 | 110 | |
ae9e196d AM |
111 | static int amg88xx_xfer(struct video_i2c_data *data, char *buf) |
112 | { | |
113 | return regmap_bulk_read(data->regmap, AMG88XX_REG_T01L, buf, | |
114 | data->chip->buffer_size); | |
5cebaac6 MR |
115 | } |
116 | ||
56281021 AM |
117 | static int amg88xx_setup(struct video_i2c_data *data) |
118 | { | |
119 | unsigned int mask = AMG88XX_FPSC_1FPS; | |
120 | unsigned int val; | |
121 | ||
122 | if (data->frame_interval.numerator == data->frame_interval.denominator) | |
123 | val = mask; | |
124 | else | |
125 | val = 0; | |
126 | ||
127 | return regmap_update_bits(data->regmap, AMG88XX_REG_FPSC, mask, val); | |
128 | } | |
129 | ||
acbea679 MR |
130 | #if IS_ENABLED(CONFIG_HWMON) |
131 | ||
132 | static const u32 amg88xx_temp_config[] = { | |
133 | HWMON_T_INPUT, | |
134 | 0 | |
135 | }; | |
136 | ||
137 | static const struct hwmon_channel_info amg88xx_temp = { | |
138 | .type = hwmon_temp, | |
139 | .config = amg88xx_temp_config, | |
140 | }; | |
141 | ||
142 | static const struct hwmon_channel_info *amg88xx_info[] = { | |
143 | &amg88xx_temp, | |
144 | NULL | |
145 | }; | |
146 | ||
147 | static umode_t amg88xx_is_visible(const void *drvdata, | |
148 | enum hwmon_sensor_types type, | |
149 | u32 attr, int channel) | |
150 | { | |
151 | return 0444; | |
152 | } | |
153 | ||
154 | static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type, | |
155 | u32 attr, int channel, long *val) | |
156 | { | |
157 | struct video_i2c_data *data = dev_get_drvdata(dev); | |
ae9e196d AM |
158 | __le16 buf; |
159 | int tmp; | |
acbea679 | 160 | |
ae9e196d AM |
161 | tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2); |
162 | if (tmp) | |
acbea679 MR |
163 | return tmp; |
164 | ||
ae9e196d AM |
165 | tmp = le16_to_cpu(buf); |
166 | ||
acbea679 MR |
167 | /* |
168 | * Check for sign bit, this isn't a two's complement value but an | |
169 | * absolute temperature that needs to be inverted in the case of being | |
170 | * negative. | |
171 | */ | |
172 | if (tmp & BIT(11)) | |
173 | tmp = -(tmp & 0x7ff); | |
174 | ||
175 | *val = (tmp * 625) / 10; | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | static const struct hwmon_ops amg88xx_hwmon_ops = { | |
181 | .is_visible = amg88xx_is_visible, | |
182 | .read = amg88xx_read, | |
183 | }; | |
184 | ||
185 | static const struct hwmon_chip_info amg88xx_chip_info = { | |
186 | .ops = &amg88xx_hwmon_ops, | |
187 | .info = amg88xx_info, | |
188 | }; | |
189 | ||
190 | static int amg88xx_hwmon_init(struct video_i2c_data *data) | |
191 | { | |
ae9e196d AM |
192 | struct device *dev = regmap_get_device(data->regmap); |
193 | void *hwmon = devm_hwmon_device_register_with_info(dev, "amg88xx", data, | |
194 | &amg88xx_chip_info, NULL); | |
acbea679 | 195 | |
17f330ce | 196 | return PTR_ERR_OR_ZERO(hwmon); |
acbea679 MR |
197 | } |
198 | #else | |
199 | #define amg88xx_hwmon_init NULL | |
200 | #endif | |
201 | ||
5cebaac6 MR |
202 | #define AMG88XX 0 |
203 | ||
56281021 AM |
204 | static const struct v4l2_fract amg88xx_frame_intervals[] = { |
205 | { 1, 10 }, | |
206 | { 1, 1 }, | |
207 | }; | |
208 | ||
5cebaac6 MR |
209 | static const struct video_i2c_chip video_i2c_chip[] = { |
210 | [AMG88XX] = { | |
211 | .size = &amg88xx_size, | |
212 | .format = &amg88xx_format, | |
56281021 AM |
213 | .frame_intervals = amg88xx_frame_intervals, |
214 | .num_frame_intervals = ARRAY_SIZE(amg88xx_frame_intervals), | |
5cebaac6 MR |
215 | .buffer_size = 128, |
216 | .bpp = 16, | |
ae9e196d | 217 | .regmap_config = &amg88xx_regmap_config, |
56281021 | 218 | .setup = &amg88xx_setup, |
5cebaac6 | 219 | .xfer = &amg88xx_xfer, |
acbea679 | 220 | .hwmon_init = amg88xx_hwmon_init, |
5cebaac6 MR |
221 | }, |
222 | }; | |
223 | ||
224 | static const struct v4l2_file_operations video_i2c_fops = { | |
225 | .owner = THIS_MODULE, | |
226 | .open = v4l2_fh_open, | |
227 | .release = vb2_fop_release, | |
228 | .poll = vb2_fop_poll, | |
229 | .read = vb2_fop_read, | |
230 | .mmap = vb2_fop_mmap, | |
231 | .unlocked_ioctl = video_ioctl2, | |
232 | }; | |
233 | ||
234 | static int queue_setup(struct vb2_queue *vq, | |
235 | unsigned int *nbuffers, unsigned int *nplanes, | |
236 | unsigned int sizes[], struct device *alloc_devs[]) | |
237 | { | |
238 | struct video_i2c_data *data = vb2_get_drv_priv(vq); | |
239 | unsigned int size = data->chip->buffer_size; | |
240 | ||
241 | if (vq->num_buffers + *nbuffers < 2) | |
242 | *nbuffers = 2; | |
243 | ||
244 | if (*nplanes) | |
245 | return sizes[0] < size ? -EINVAL : 0; | |
246 | ||
247 | *nplanes = 1; | |
248 | sizes[0] = size; | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | static int buffer_prepare(struct vb2_buffer *vb) | |
254 | { | |
255 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); | |
256 | struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue); | |
257 | unsigned int size = data->chip->buffer_size; | |
258 | ||
259 | if (vb2_plane_size(vb, 0) < size) | |
260 | return -EINVAL; | |
261 | ||
262 | vbuf->field = V4L2_FIELD_NONE; | |
263 | vb2_set_plane_payload(vb, 0, size); | |
264 | ||
265 | return 0; | |
266 | } | |
267 | ||
268 | static void buffer_queue(struct vb2_buffer *vb) | |
269 | { | |
270 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); | |
271 | struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue); | |
272 | struct video_i2c_buffer *buf = | |
273 | container_of(vbuf, struct video_i2c_buffer, vb); | |
274 | ||
275 | spin_lock(&data->slock); | |
276 | list_add_tail(&buf->list, &data->vid_cap_active); | |
277 | spin_unlock(&data->slock); | |
278 | } | |
279 | ||
280 | static int video_i2c_thread_vid_cap(void *priv) | |
281 | { | |
282 | struct video_i2c_data *data = priv; | |
56281021 AM |
283 | unsigned int delay = mult_frac(HZ, data->frame_interval.numerator, |
284 | data->frame_interval.denominator); | |
5cebaac6 MR |
285 | |
286 | set_freezable(); | |
287 | ||
288 | do { | |
289 | unsigned long start_jiffies = jiffies; | |
290 | struct video_i2c_buffer *vid_cap_buf = NULL; | |
291 | int schedule_delay; | |
292 | ||
293 | try_to_freeze(); | |
294 | ||
295 | spin_lock(&data->slock); | |
296 | ||
297 | if (!list_empty(&data->vid_cap_active)) { | |
298 | vid_cap_buf = list_last_entry(&data->vid_cap_active, | |
299 | struct video_i2c_buffer, list); | |
300 | list_del(&vid_cap_buf->list); | |
301 | } | |
302 | ||
303 | spin_unlock(&data->slock); | |
304 | ||
305 | if (vid_cap_buf) { | |
306 | struct vb2_buffer *vb2_buf = &vid_cap_buf->vb.vb2_buf; | |
307 | void *vbuf = vb2_plane_vaddr(vb2_buf, 0); | |
308 | int ret; | |
309 | ||
310 | ret = data->chip->xfer(data, vbuf); | |
311 | vb2_buf->timestamp = ktime_get_ns(); | |
312 | vid_cap_buf->vb.sequence = data->sequence++; | |
313 | vb2_buffer_done(vb2_buf, ret ? | |
314 | VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); | |
315 | } | |
316 | ||
317 | schedule_delay = delay - (jiffies - start_jiffies); | |
318 | ||
319 | if (time_after(jiffies, start_jiffies + delay)) | |
320 | schedule_delay = delay; | |
321 | ||
322 | schedule_timeout_interruptible(schedule_delay); | |
323 | } while (!kthread_should_stop()); | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
328 | static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state) | |
329 | { | |
330 | struct video_i2c_data *data = vb2_get_drv_priv(vq); | |
331 | struct video_i2c_buffer *buf, *tmp; | |
332 | ||
333 | spin_lock(&data->slock); | |
334 | ||
335 | list_for_each_entry_safe(buf, tmp, &data->vid_cap_active, list) { | |
336 | list_del(&buf->list); | |
337 | vb2_buffer_done(&buf->vb.vb2_buf, state); | |
338 | } | |
339 | ||
340 | spin_unlock(&data->slock); | |
341 | } | |
342 | ||
343 | static int start_streaming(struct vb2_queue *vq, unsigned int count) | |
344 | { | |
345 | struct video_i2c_data *data = vb2_get_drv_priv(vq); | |
56281021 | 346 | int ret; |
5cebaac6 MR |
347 | |
348 | if (data->kthread_vid_cap) | |
349 | return 0; | |
350 | ||
56281021 AM |
351 | ret = data->chip->setup(data); |
352 | if (ret) | |
353 | goto error_del_list; | |
354 | ||
5cebaac6 MR |
355 | data->sequence = 0; |
356 | data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data, | |
357 | "%s-vid-cap", data->v4l2_dev.name); | |
56281021 AM |
358 | ret = PTR_ERR_OR_ZERO(data->kthread_vid_cap); |
359 | if (!ret) | |
5cebaac6 MR |
360 | return 0; |
361 | ||
56281021 | 362 | error_del_list: |
5cebaac6 MR |
363 | video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED); |
364 | ||
56281021 | 365 | return ret; |
5cebaac6 MR |
366 | } |
367 | ||
368 | static void stop_streaming(struct vb2_queue *vq) | |
369 | { | |
370 | struct video_i2c_data *data = vb2_get_drv_priv(vq); | |
371 | ||
372 | if (data->kthread_vid_cap == NULL) | |
373 | return; | |
374 | ||
375 | kthread_stop(data->kthread_vid_cap); | |
376 | data->kthread_vid_cap = NULL; | |
377 | ||
378 | video_i2c_del_list(vq, VB2_BUF_STATE_ERROR); | |
379 | } | |
380 | ||
c7f7da2a | 381 | static const struct vb2_ops video_i2c_video_qops = { |
5cebaac6 MR |
382 | .queue_setup = queue_setup, |
383 | .buf_prepare = buffer_prepare, | |
384 | .buf_queue = buffer_queue, | |
385 | .start_streaming = start_streaming, | |
386 | .stop_streaming = stop_streaming, | |
387 | .wait_prepare = vb2_ops_wait_prepare, | |
388 | .wait_finish = vb2_ops_wait_finish, | |
389 | }; | |
390 | ||
391 | static int video_i2c_querycap(struct file *file, void *priv, | |
392 | struct v4l2_capability *vcap) | |
393 | { | |
394 | struct video_i2c_data *data = video_drvdata(file); | |
ae9e196d AM |
395 | struct device *dev = regmap_get_device(data->regmap); |
396 | struct i2c_client *client = to_i2c_client(dev); | |
5cebaac6 | 397 | |
c0decac1 MCC |
398 | strscpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver)); |
399 | strscpy(vcap->card, data->vdev.name, sizeof(vcap->card)); | |
5cebaac6 MR |
400 | |
401 | sprintf(vcap->bus_info, "I2C:%d-%d", client->adapter->nr, client->addr); | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | static int video_i2c_g_input(struct file *file, void *fh, unsigned int *inp) | |
407 | { | |
408 | *inp = 0; | |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
413 | static int video_i2c_s_input(struct file *file, void *fh, unsigned int inp) | |
414 | { | |
415 | return (inp > 0) ? -EINVAL : 0; | |
416 | } | |
417 | ||
418 | static int video_i2c_enum_input(struct file *file, void *fh, | |
419 | struct v4l2_input *vin) | |
420 | { | |
421 | if (vin->index > 0) | |
422 | return -EINVAL; | |
423 | ||
c0decac1 | 424 | strscpy(vin->name, "Camera", sizeof(vin->name)); |
5cebaac6 MR |
425 | |
426 | vin->type = V4L2_INPUT_TYPE_CAMERA; | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | static int video_i2c_enum_fmt_vid_cap(struct file *file, void *fh, | |
432 | struct v4l2_fmtdesc *fmt) | |
433 | { | |
434 | struct video_i2c_data *data = video_drvdata(file); | |
435 | enum v4l2_buf_type type = fmt->type; | |
436 | ||
437 | if (fmt->index > 0) | |
438 | return -EINVAL; | |
439 | ||
440 | *fmt = *data->chip->format; | |
441 | fmt->type = type; | |
442 | ||
443 | return 0; | |
444 | } | |
445 | ||
446 | static int video_i2c_enum_framesizes(struct file *file, void *fh, | |
447 | struct v4l2_frmsizeenum *fsize) | |
448 | { | |
449 | const struct video_i2c_data *data = video_drvdata(file); | |
450 | const struct v4l2_frmsize_discrete *size = data->chip->size; | |
451 | ||
452 | /* currently only one frame size is allowed */ | |
453 | if (fsize->index > 0) | |
454 | return -EINVAL; | |
455 | ||
456 | if (fsize->pixel_format != data->chip->format->pixelformat) | |
457 | return -EINVAL; | |
458 | ||
459 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; | |
460 | fsize->discrete.width = size->width; | |
461 | fsize->discrete.height = size->height; | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
466 | static int video_i2c_enum_frameintervals(struct file *file, void *priv, | |
467 | struct v4l2_frmivalenum *fe) | |
468 | { | |
469 | const struct video_i2c_data *data = video_drvdata(file); | |
470 | const struct v4l2_frmsize_discrete *size = data->chip->size; | |
471 | ||
56281021 | 472 | if (fe->index >= data->chip->num_frame_intervals) |
5cebaac6 MR |
473 | return -EINVAL; |
474 | ||
475 | if (fe->width != size->width || fe->height != size->height) | |
476 | return -EINVAL; | |
477 | ||
478 | fe->type = V4L2_FRMIVAL_TYPE_DISCRETE; | |
56281021 | 479 | fe->discrete = data->chip->frame_intervals[fe->index]; |
5cebaac6 MR |
480 | |
481 | return 0; | |
482 | } | |
483 | ||
484 | static int video_i2c_try_fmt_vid_cap(struct file *file, void *fh, | |
485 | struct v4l2_format *fmt) | |
486 | { | |
487 | const struct video_i2c_data *data = video_drvdata(file); | |
488 | const struct v4l2_frmsize_discrete *size = data->chip->size; | |
489 | struct v4l2_pix_format *pix = &fmt->fmt.pix; | |
490 | unsigned int bpp = data->chip->bpp / 8; | |
491 | ||
492 | pix->width = size->width; | |
493 | pix->height = size->height; | |
494 | pix->pixelformat = data->chip->format->pixelformat; | |
495 | pix->field = V4L2_FIELD_NONE; | |
496 | pix->bytesperline = pix->width * bpp; | |
497 | pix->sizeimage = pix->bytesperline * pix->height; | |
498 | pix->colorspace = V4L2_COLORSPACE_RAW; | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
503 | static int video_i2c_s_fmt_vid_cap(struct file *file, void *fh, | |
504 | struct v4l2_format *fmt) | |
505 | { | |
506 | struct video_i2c_data *data = video_drvdata(file); | |
507 | ||
508 | if (vb2_is_busy(&data->vb_vidq)) | |
509 | return -EBUSY; | |
510 | ||
511 | return video_i2c_try_fmt_vid_cap(file, fh, fmt); | |
512 | } | |
513 | ||
514 | static int video_i2c_g_parm(struct file *filp, void *priv, | |
515 | struct v4l2_streamparm *parm) | |
516 | { | |
517 | struct video_i2c_data *data = video_drvdata(filp); | |
518 | ||
519 | if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
520 | return -EINVAL; | |
521 | ||
522 | parm->parm.capture.readbuffers = 1; | |
523 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; | |
56281021 | 524 | parm->parm.capture.timeperframe = data->frame_interval; |
5cebaac6 MR |
525 | |
526 | return 0; | |
527 | } | |
528 | ||
56281021 AM |
529 | static int video_i2c_s_parm(struct file *filp, void *priv, |
530 | struct v4l2_streamparm *parm) | |
531 | { | |
532 | struct video_i2c_data *data = video_drvdata(filp); | |
533 | int i; | |
534 | ||
535 | for (i = 0; i < data->chip->num_frame_intervals - 1; i++) { | |
536 | if (V4L2_FRACT_COMPARE(parm->parm.capture.timeperframe, <=, | |
537 | data->chip->frame_intervals[i])) | |
538 | break; | |
539 | } | |
540 | data->frame_interval = data->chip->frame_intervals[i]; | |
541 | ||
542 | return video_i2c_g_parm(filp, priv, parm); | |
543 | } | |
544 | ||
5cebaac6 MR |
545 | static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = { |
546 | .vidioc_querycap = video_i2c_querycap, | |
547 | .vidioc_g_input = video_i2c_g_input, | |
548 | .vidioc_s_input = video_i2c_s_input, | |
549 | .vidioc_enum_input = video_i2c_enum_input, | |
550 | .vidioc_enum_fmt_vid_cap = video_i2c_enum_fmt_vid_cap, | |
551 | .vidioc_enum_framesizes = video_i2c_enum_framesizes, | |
552 | .vidioc_enum_frameintervals = video_i2c_enum_frameintervals, | |
553 | .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap, | |
554 | .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap, | |
555 | .vidioc_g_parm = video_i2c_g_parm, | |
56281021 | 556 | .vidioc_s_parm = video_i2c_s_parm, |
5cebaac6 MR |
557 | .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap, |
558 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
559 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
560 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
561 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
562 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
563 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
564 | .vidioc_streamon = vb2_ioctl_streamon, | |
565 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
566 | }; | |
567 | ||
568 | static void video_i2c_release(struct video_device *vdev) | |
569 | { | |
c764da98 AM |
570 | struct video_i2c_data *data = video_get_drvdata(vdev); |
571 | ||
572 | v4l2_device_unregister(&data->v4l2_dev); | |
573 | mutex_destroy(&data->lock); | |
574 | mutex_destroy(&data->queue_lock); | |
ae9e196d | 575 | regmap_exit(data->regmap); |
c764da98 | 576 | kfree(data); |
5cebaac6 MR |
577 | } |
578 | ||
579 | static int video_i2c_probe(struct i2c_client *client, | |
580 | const struct i2c_device_id *id) | |
581 | { | |
582 | struct video_i2c_data *data; | |
583 | struct v4l2_device *v4l2_dev; | |
584 | struct vb2_queue *queue; | |
585 | int ret = -ENODEV; | |
586 | ||
587 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
588 | if (!data) | |
589 | return -ENOMEM; | |
590 | ||
591 | if (dev_fwnode(&client->dev)) | |
592 | data->chip = device_get_match_data(&client->dev); | |
593 | else if (id) | |
594 | data->chip = &video_i2c_chip[id->driver_data]; | |
595 | else | |
596 | goto error_free_device; | |
597 | ||
ae9e196d AM |
598 | data->regmap = regmap_init_i2c(client, data->chip->regmap_config); |
599 | if (IS_ERR(data->regmap)) { | |
600 | ret = PTR_ERR(data->regmap); | |
601 | goto error_free_device; | |
602 | } | |
603 | ||
5cebaac6 | 604 | v4l2_dev = &data->v4l2_dev; |
c0decac1 | 605 | strscpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name)); |
5cebaac6 MR |
606 | |
607 | ret = v4l2_device_register(&client->dev, v4l2_dev); | |
608 | if (ret < 0) | |
ae9e196d | 609 | goto error_regmap_exit; |
5cebaac6 MR |
610 | |
611 | mutex_init(&data->lock); | |
612 | mutex_init(&data->queue_lock); | |
613 | ||
614 | queue = &data->vb_vidq; | |
615 | queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
616 | queue->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR | VB2_READ; | |
617 | queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
618 | queue->drv_priv = data; | |
619 | queue->buf_struct_size = sizeof(struct video_i2c_buffer); | |
620 | queue->min_buffers_needed = 1; | |
621 | queue->ops = &video_i2c_video_qops; | |
622 | queue->mem_ops = &vb2_vmalloc_memops; | |
623 | ||
624 | ret = vb2_queue_init(queue); | |
625 | if (ret < 0) | |
626 | goto error_unregister_device; | |
627 | ||
628 | data->vdev.queue = queue; | |
629 | data->vdev.queue->lock = &data->queue_lock; | |
630 | ||
631 | snprintf(data->vdev.name, sizeof(data->vdev.name), | |
632 | "I2C %d-%d Transport Video", | |
633 | client->adapter->nr, client->addr); | |
634 | ||
635 | data->vdev.v4l2_dev = v4l2_dev; | |
636 | data->vdev.fops = &video_i2c_fops; | |
637 | data->vdev.lock = &data->lock; | |
638 | data->vdev.ioctl_ops = &video_i2c_ioctl_ops; | |
639 | data->vdev.release = video_i2c_release; | |
640 | data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | | |
641 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
642 | ||
643 | spin_lock_init(&data->slock); | |
644 | INIT_LIST_HEAD(&data->vid_cap_active); | |
645 | ||
56281021 AM |
646 | data->frame_interval = data->chip->frame_intervals[0]; |
647 | ||
5cebaac6 MR |
648 | video_set_drvdata(&data->vdev, data); |
649 | i2c_set_clientdata(client, data); | |
650 | ||
acbea679 MR |
651 | if (data->chip->hwmon_init) { |
652 | ret = data->chip->hwmon_init(data); | |
653 | if (ret < 0) { | |
654 | dev_warn(&client->dev, | |
655 | "failed to register hwmon device\n"); | |
656 | } | |
657 | } | |
658 | ||
5cebaac6 MR |
659 | ret = video_register_device(&data->vdev, VFL_TYPE_GRABBER, -1); |
660 | if (ret < 0) | |
661 | goto error_unregister_device; | |
662 | ||
663 | return 0; | |
664 | ||
665 | error_unregister_device: | |
666 | v4l2_device_unregister(v4l2_dev); | |
667 | mutex_destroy(&data->lock); | |
668 | mutex_destroy(&data->queue_lock); | |
669 | ||
ae9e196d AM |
670 | error_regmap_exit: |
671 | regmap_exit(data->regmap); | |
672 | ||
5cebaac6 MR |
673 | error_free_device: |
674 | kfree(data); | |
675 | ||
676 | return ret; | |
677 | } | |
678 | ||
679 | static int video_i2c_remove(struct i2c_client *client) | |
680 | { | |
681 | struct video_i2c_data *data = i2c_get_clientdata(client); | |
682 | ||
683 | video_unregister_device(&data->vdev); | |
5cebaac6 MR |
684 | |
685 | return 0; | |
686 | } | |
687 | ||
688 | static const struct i2c_device_id video_i2c_id_table[] = { | |
689 | { "amg88xx", AMG88XX }, | |
690 | {} | |
691 | }; | |
692 | MODULE_DEVICE_TABLE(i2c, video_i2c_id_table); | |
693 | ||
694 | static const struct of_device_id video_i2c_of_match[] = { | |
695 | { .compatible = "panasonic,amg88xx", .data = &video_i2c_chip[AMG88XX] }, | |
696 | {} | |
697 | }; | |
698 | MODULE_DEVICE_TABLE(of, video_i2c_of_match); | |
699 | ||
700 | static struct i2c_driver video_i2c_driver = { | |
701 | .driver = { | |
702 | .name = VIDEO_I2C_DRIVER, | |
703 | .of_match_table = video_i2c_of_match, | |
704 | }, | |
705 | .probe = video_i2c_probe, | |
706 | .remove = video_i2c_remove, | |
707 | .id_table = video_i2c_id_table, | |
708 | }; | |
709 | ||
710 | module_i2c_driver(video_i2c_driver); | |
711 | ||
712 | MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); | |
713 | MODULE_DESCRIPTION("I2C transport video support"); | |
714 | MODULE_LICENSE("GPL v2"); |