video-i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. #include <linux/delay.h>
  11. #include <linux/freezer.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/kthread.h>
  14. #include <linux/i2c.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of_device.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/videodev2.h>
  22. #include <media/v4l2-common.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-event.h>
  25. #include <media/v4l2-fh.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/videobuf2-v4l2.h>
  28. #include <media/videobuf2-vmalloc.h>
  29. #define VIDEO_I2C_DRIVER "video-i2c"
  30. struct video_i2c_chip;
  31. struct video_i2c_buffer {
  32. struct vb2_v4l2_buffer vb;
  33. struct list_head list;
  34. };
  35. struct video_i2c_data {
  36. struct i2c_client *client;
  37. const struct video_i2c_chip *chip;
  38. struct mutex lock;
  39. spinlock_t slock;
  40. unsigned int sequence;
  41. struct mutex queue_lock;
  42. struct v4l2_device v4l2_dev;
  43. struct video_device vdev;
  44. struct vb2_queue vb_vidq;
  45. struct task_struct *kthread_vid_cap;
  46. struct list_head vid_cap_active;
  47. };
  48. static const struct v4l2_fmtdesc amg88xx_format = {
  49. .pixelformat = V4L2_PIX_FMT_Y12,
  50. };
  51. static const struct v4l2_frmsize_discrete amg88xx_size = {
  52. .width = 8,
  53. .height = 8,
  54. };
  55. struct video_i2c_chip {
  56. /* video dimensions */
  57. const struct v4l2_fmtdesc *format;
  58. const struct v4l2_frmsize_discrete *size;
  59. /* max frames per second */
  60. unsigned int max_fps;
  61. /* pixel buffer size */
  62. unsigned int buffer_size;
  63. /* pixel size in bits */
  64. unsigned int bpp;
  65. /* xfer function */
  66. int (*xfer)(struct video_i2c_data *data, char *buf);
  67. /* hwmon init function */
  68. int (*hwmon_init)(struct video_i2c_data *data);
  69. };
  70. static int amg88xx_xfer(struct video_i2c_data *data, char *buf)
  71. {
  72. struct i2c_client *client = data->client;
  73. struct i2c_msg msg[2];
  74. u8 reg = 0x80;
  75. int ret;
  76. msg[0].addr = client->addr;
  77. msg[0].flags = 0;
  78. msg[0].len = 1;
  79. msg[0].buf = (char *)&reg;
  80. msg[1].addr = client->addr;
  81. msg[1].flags = I2C_M_RD;
  82. msg[1].len = data->chip->buffer_size;
  83. msg[1].buf = (char *)buf;
  84. ret = i2c_transfer(client->adapter, msg, 2);
  85. return (ret == 2) ? 0 : -EIO;
  86. }
  87. #if IS_REACHABLE(CONFIG_HWMON)
  88. static const u32 amg88xx_temp_config[] = {
  89. HWMON_T_INPUT,
  90. 0
  91. };
  92. static const struct hwmon_channel_info amg88xx_temp = {
  93. .type = hwmon_temp,
  94. .config = amg88xx_temp_config,
  95. };
  96. static const struct hwmon_channel_info *amg88xx_info[] = {
  97. &amg88xx_temp,
  98. NULL
  99. };
  100. static umode_t amg88xx_is_visible(const void *drvdata,
  101. enum hwmon_sensor_types type,
  102. u32 attr, int channel)
  103. {
  104. return 0444;
  105. }
  106. static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type,
  107. u32 attr, int channel, long *val)
  108. {
  109. struct video_i2c_data *data = dev_get_drvdata(dev);
  110. struct i2c_client *client = data->client;
  111. int tmp = i2c_smbus_read_word_data(client, 0x0e);
  112. if (tmp < 0)
  113. return tmp;
  114. /*
  115. * Check for sign bit, this isn't a two's complement value but an
  116. * absolute temperature that needs to be inverted in the case of being
  117. * negative.
  118. */
  119. if (tmp & BIT(11))
  120. tmp = -(tmp & 0x7ff);
  121. *val = (tmp * 625) / 10;
  122. return 0;
  123. }
  124. static const struct hwmon_ops amg88xx_hwmon_ops = {
  125. .is_visible = amg88xx_is_visible,
  126. .read = amg88xx_read,
  127. };
  128. static const struct hwmon_chip_info amg88xx_chip_info = {
  129. .ops = &amg88xx_hwmon_ops,
  130. .info = amg88xx_info,
  131. };
  132. static int amg88xx_hwmon_init(struct video_i2c_data *data)
  133. {
  134. void *hwmon = devm_hwmon_device_register_with_info(&data->client->dev,
  135. "amg88xx", data, &amg88xx_chip_info, NULL);
  136. return PTR_ERR_OR_ZERO(hwmon);
  137. }
  138. #else
  139. #define amg88xx_hwmon_init NULL
  140. #endif
  141. #define AMG88XX 0
  142. static const struct video_i2c_chip video_i2c_chip[] = {
  143. [AMG88XX] = {
  144. .size = &amg88xx_size,
  145. .format = &amg88xx_format,
  146. .max_fps = 10,
  147. .buffer_size = 128,
  148. .bpp = 16,
  149. .xfer = &amg88xx_xfer,
  150. .hwmon_init = amg88xx_hwmon_init,
  151. },
  152. };
  153. static const struct v4l2_file_operations video_i2c_fops = {
  154. .owner = THIS_MODULE,
  155. .open = v4l2_fh_open,
  156. .release = vb2_fop_release,
  157. .poll = vb2_fop_poll,
  158. .read = vb2_fop_read,
  159. .mmap = vb2_fop_mmap,
  160. .unlocked_ioctl = video_ioctl2,
  161. };
  162. static int queue_setup(struct vb2_queue *vq,
  163. unsigned int *nbuffers, unsigned int *nplanes,
  164. unsigned int sizes[], struct device *alloc_devs[])
  165. {
  166. struct video_i2c_data *data = vb2_get_drv_priv(vq);
  167. unsigned int size = data->chip->buffer_size;
  168. if (vq->num_buffers + *nbuffers < 2)
  169. *nbuffers = 2;
  170. if (*nplanes)
  171. return sizes[0] < size ? -EINVAL : 0;
  172. *nplanes = 1;
  173. sizes[0] = size;
  174. return 0;
  175. }
  176. static int buffer_prepare(struct vb2_buffer *vb)
  177. {
  178. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  179. struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
  180. unsigned int size = data->chip->buffer_size;
  181. if (vb2_plane_size(vb, 0) < size)
  182. return -EINVAL;
  183. vbuf->field = V4L2_FIELD_NONE;
  184. vb2_set_plane_payload(vb, 0, size);
  185. return 0;
  186. }
  187. static void buffer_queue(struct vb2_buffer *vb)
  188. {
  189. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  190. struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
  191. struct video_i2c_buffer *buf =
  192. container_of(vbuf, struct video_i2c_buffer, vb);
  193. spin_lock(&data->slock);
  194. list_add_tail(&buf->list, &data->vid_cap_active);
  195. spin_unlock(&data->slock);
  196. }
  197. static int video_i2c_thread_vid_cap(void *priv)
  198. {
  199. struct video_i2c_data *data = priv;
  200. unsigned int delay = msecs_to_jiffies(1000 / data->chip->max_fps);
  201. set_freezable();
  202. do {
  203. unsigned long start_jiffies = jiffies;
  204. struct video_i2c_buffer *vid_cap_buf = NULL;
  205. int schedule_delay;
  206. try_to_freeze();
  207. spin_lock(&data->slock);
  208. if (!list_empty(&data->vid_cap_active)) {
  209. vid_cap_buf = list_last_entry(&data->vid_cap_active,
  210. struct video_i2c_buffer, list);
  211. list_del(&vid_cap_buf->list);
  212. }
  213. spin_unlock(&data->slock);
  214. if (vid_cap_buf) {
  215. struct vb2_buffer *vb2_buf = &vid_cap_buf->vb.vb2_buf;
  216. void *vbuf = vb2_plane_vaddr(vb2_buf, 0);
  217. int ret;
  218. ret = data->chip->xfer(data, vbuf);
  219. vb2_buf->timestamp = ktime_get_ns();
  220. vid_cap_buf->vb.sequence = data->sequence++;
  221. vb2_buffer_done(vb2_buf, ret ?
  222. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  223. }
  224. schedule_delay = delay - (jiffies - start_jiffies);
  225. if (time_after(jiffies, start_jiffies + delay))
  226. schedule_delay = delay;
  227. schedule_timeout_interruptible(schedule_delay);
  228. } while (!kthread_should_stop());
  229. return 0;
  230. }
  231. static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state)
  232. {
  233. struct video_i2c_data *data = vb2_get_drv_priv(vq);
  234. struct video_i2c_buffer *buf, *tmp;
  235. spin_lock(&data->slock);
  236. list_for_each_entry_safe(buf, tmp, &data->vid_cap_active, list) {
  237. list_del(&buf->list);
  238. vb2_buffer_done(&buf->vb.vb2_buf, state);
  239. }
  240. spin_unlock(&data->slock);
  241. }
  242. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  243. {
  244. struct video_i2c_data *data = vb2_get_drv_priv(vq);
  245. if (data->kthread_vid_cap)
  246. return 0;
  247. data->sequence = 0;
  248. data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data,
  249. "%s-vid-cap", data->v4l2_dev.name);
  250. if (!IS_ERR(data->kthread_vid_cap))
  251. return 0;
  252. video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED);
  253. return PTR_ERR(data->kthread_vid_cap);
  254. }
  255. static void stop_streaming(struct vb2_queue *vq)
  256. {
  257. struct video_i2c_data *data = vb2_get_drv_priv(vq);
  258. if (data->kthread_vid_cap == NULL)
  259. return;
  260. kthread_stop(data->kthread_vid_cap);
  261. data->kthread_vid_cap = NULL;
  262. video_i2c_del_list(vq, VB2_BUF_STATE_ERROR);
  263. }
  264. static struct vb2_ops video_i2c_video_qops = {
  265. .queue_setup = queue_setup,
  266. .buf_prepare = buffer_prepare,
  267. .buf_queue = buffer_queue,
  268. .start_streaming = start_streaming,
  269. .stop_streaming = stop_streaming,
  270. .wait_prepare = vb2_ops_wait_prepare,
  271. .wait_finish = vb2_ops_wait_finish,
  272. };
  273. static int video_i2c_querycap(struct file *file, void *priv,
  274. struct v4l2_capability *vcap)
  275. {
  276. struct video_i2c_data *data = video_drvdata(file);
  277. struct i2c_client *client = data->client;
  278. strlcpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver));
  279. strlcpy(vcap->card, data->vdev.name, sizeof(vcap->card));
  280. sprintf(vcap->bus_info, "I2C:%d-%d", client->adapter->nr, client->addr);
  281. return 0;
  282. }
  283. static int video_i2c_g_input(struct file *file, void *fh, unsigned int *inp)
  284. {
  285. *inp = 0;
  286. return 0;
  287. }
  288. static int video_i2c_s_input(struct file *file, void *fh, unsigned int inp)
  289. {
  290. return (inp > 0) ? -EINVAL : 0;
  291. }
  292. static int video_i2c_enum_input(struct file *file, void *fh,
  293. struct v4l2_input *vin)
  294. {
  295. if (vin->index > 0)
  296. return -EINVAL;
  297. strlcpy(vin->name, "Camera", sizeof(vin->name));
  298. vin->type = V4L2_INPUT_TYPE_CAMERA;
  299. return 0;
  300. }
  301. static int video_i2c_enum_fmt_vid_cap(struct file *file, void *fh,
  302. struct v4l2_fmtdesc *fmt)
  303. {
  304. struct video_i2c_data *data = video_drvdata(file);
  305. enum v4l2_buf_type type = fmt->type;
  306. if (fmt->index > 0)
  307. return -EINVAL;
  308. *fmt = *data->chip->format;
  309. fmt->type = type;
  310. return 0;
  311. }
  312. static int video_i2c_enum_framesizes(struct file *file, void *fh,
  313. struct v4l2_frmsizeenum *fsize)
  314. {
  315. const struct video_i2c_data *data = video_drvdata(file);
  316. const struct v4l2_frmsize_discrete *size = data->chip->size;
  317. /* currently only one frame size is allowed */
  318. if (fsize->index > 0)
  319. return -EINVAL;
  320. if (fsize->pixel_format != data->chip->format->pixelformat)
  321. return -EINVAL;
  322. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  323. fsize->discrete.width = size->width;
  324. fsize->discrete.height = size->height;
  325. return 0;
  326. }
  327. static int video_i2c_enum_frameintervals(struct file *file, void *priv,
  328. struct v4l2_frmivalenum *fe)
  329. {
  330. const struct video_i2c_data *data = video_drvdata(file);
  331. const struct v4l2_frmsize_discrete *size = data->chip->size;
  332. if (fe->index > 0)
  333. return -EINVAL;
  334. if (fe->width != size->width || fe->height != size->height)
  335. return -EINVAL;
  336. fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  337. fe->discrete.numerator = 1;
  338. fe->discrete.denominator = data->chip->max_fps;
  339. return 0;
  340. }
  341. static int video_i2c_try_fmt_vid_cap(struct file *file, void *fh,
  342. struct v4l2_format *fmt)
  343. {
  344. const struct video_i2c_data *data = video_drvdata(file);
  345. const struct v4l2_frmsize_discrete *size = data->chip->size;
  346. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  347. unsigned int bpp = data->chip->bpp / 8;
  348. pix->width = size->width;
  349. pix->height = size->height;
  350. pix->pixelformat = data->chip->format->pixelformat;
  351. pix->field = V4L2_FIELD_NONE;
  352. pix->bytesperline = pix->width * bpp;
  353. pix->sizeimage = pix->bytesperline * pix->height;
  354. pix->colorspace = V4L2_COLORSPACE_RAW;
  355. return 0;
  356. }
  357. static int video_i2c_s_fmt_vid_cap(struct file *file, void *fh,
  358. struct v4l2_format *fmt)
  359. {
  360. struct video_i2c_data *data = video_drvdata(file);
  361. if (vb2_is_busy(&data->vb_vidq))
  362. return -EBUSY;
  363. return video_i2c_try_fmt_vid_cap(file, fh, fmt);
  364. }
  365. static int video_i2c_g_parm(struct file *filp, void *priv,
  366. struct v4l2_streamparm *parm)
  367. {
  368. struct video_i2c_data *data = video_drvdata(filp);
  369. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  370. return -EINVAL;
  371. parm->parm.capture.readbuffers = 1;
  372. parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  373. parm->parm.capture.timeperframe.numerator = 1;
  374. parm->parm.capture.timeperframe.denominator = data->chip->max_fps;
  375. return 0;
  376. }
  377. static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = {
  378. .vidioc_querycap = video_i2c_querycap,
  379. .vidioc_g_input = video_i2c_g_input,
  380. .vidioc_s_input = video_i2c_s_input,
  381. .vidioc_enum_input = video_i2c_enum_input,
  382. .vidioc_enum_fmt_vid_cap = video_i2c_enum_fmt_vid_cap,
  383. .vidioc_enum_framesizes = video_i2c_enum_framesizes,
  384. .vidioc_enum_frameintervals = video_i2c_enum_frameintervals,
  385. .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
  386. .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap,
  387. .vidioc_g_parm = video_i2c_g_parm,
  388. .vidioc_s_parm = video_i2c_g_parm,
  389. .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
  390. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  391. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  392. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  393. .vidioc_querybuf = vb2_ioctl_querybuf,
  394. .vidioc_qbuf = vb2_ioctl_qbuf,
  395. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  396. .vidioc_streamon = vb2_ioctl_streamon,
  397. .vidioc_streamoff = vb2_ioctl_streamoff,
  398. };
  399. static void video_i2c_release(struct video_device *vdev)
  400. {
  401. struct video_i2c_data *data = video_get_drvdata(vdev);
  402. v4l2_device_unregister(&data->v4l2_dev);
  403. mutex_destroy(&data->lock);
  404. mutex_destroy(&data->queue_lock);
  405. kfree(data);
  406. }
  407. static int video_i2c_probe(struct i2c_client *client,
  408. const struct i2c_device_id *id)
  409. {
  410. struct video_i2c_data *data;
  411. struct v4l2_device *v4l2_dev;
  412. struct vb2_queue *queue;
  413. int ret = -ENODEV;
  414. data = kzalloc(sizeof(*data), GFP_KERNEL);
  415. if (!data)
  416. return -ENOMEM;
  417. if (dev_fwnode(&client->dev))
  418. data->chip = device_get_match_data(&client->dev);
  419. else if (id)
  420. data->chip = &video_i2c_chip[id->driver_data];
  421. else
  422. goto error_free_device;
  423. data->client = client;
  424. v4l2_dev = &data->v4l2_dev;
  425. strlcpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name));
  426. ret = v4l2_device_register(&client->dev, v4l2_dev);
  427. if (ret < 0)
  428. goto error_free_device;
  429. mutex_init(&data->lock);
  430. mutex_init(&data->queue_lock);
  431. queue = &data->vb_vidq;
  432. queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  433. queue->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR | VB2_READ;
  434. queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  435. queue->drv_priv = data;
  436. queue->buf_struct_size = sizeof(struct video_i2c_buffer);
  437. queue->min_buffers_needed = 1;
  438. queue->ops = &video_i2c_video_qops;
  439. queue->mem_ops = &vb2_vmalloc_memops;
  440. ret = vb2_queue_init(queue);
  441. if (ret < 0)
  442. goto error_unregister_device;
  443. data->vdev.queue = queue;
  444. data->vdev.queue->lock = &data->queue_lock;
  445. snprintf(data->vdev.name, sizeof(data->vdev.name),
  446. "I2C %d-%d Transport Video",
  447. client->adapter->nr, client->addr);
  448. data->vdev.v4l2_dev = v4l2_dev;
  449. data->vdev.fops = &video_i2c_fops;
  450. data->vdev.lock = &data->lock;
  451. data->vdev.ioctl_ops = &video_i2c_ioctl_ops;
  452. data->vdev.release = video_i2c_release;
  453. data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
  454. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  455. spin_lock_init(&data->slock);
  456. INIT_LIST_HEAD(&data->vid_cap_active);
  457. video_set_drvdata(&data->vdev, data);
  458. i2c_set_clientdata(client, data);
  459. if (data->chip->hwmon_init) {
  460. ret = data->chip->hwmon_init(data);
  461. if (ret < 0) {
  462. dev_warn(&client->dev,
  463. "failed to register hwmon device\n");
  464. }
  465. }
  466. ret = video_register_device(&data->vdev, VFL_TYPE_GRABBER, -1);
  467. if (ret < 0)
  468. goto error_unregister_device;
  469. return 0;
  470. error_unregister_device:
  471. v4l2_device_unregister(v4l2_dev);
  472. mutex_destroy(&data->lock);
  473. mutex_destroy(&data->queue_lock);
  474. error_free_device:
  475. kfree(data);
  476. return ret;
  477. }
  478. static int video_i2c_remove(struct i2c_client *client)
  479. {
  480. struct video_i2c_data *data = i2c_get_clientdata(client);
  481. video_unregister_device(&data->vdev);
  482. return 0;
  483. }
  484. static const struct i2c_device_id video_i2c_id_table[] = {
  485. { "amg88xx", AMG88XX },
  486. {}
  487. };
  488. MODULE_DEVICE_TABLE(i2c, video_i2c_id_table);
  489. static const struct of_device_id video_i2c_of_match[] = {
  490. { .compatible = "panasonic,amg88xx", .data = &video_i2c_chip[AMG88XX] },
  491. {}
  492. };
  493. MODULE_DEVICE_TABLE(of, video_i2c_of_match);
  494. static struct i2c_driver video_i2c_driver = {
  495. .driver = {
  496. .name = VIDEO_I2C_DRIVER,
  497. .of_match_table = video_i2c_of_match,
  498. },
  499. .probe = video_i2c_probe,
  500. .remove = video_i2c_remove,
  501. .id_table = video_i2c_id_table,
  502. };
  503. module_i2c_driver(video_i2c_driver);
  504. MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
  505. MODULE_DESCRIPTION("I2C transport video support");
  506. MODULE_LICENSE("GPL v2");