video-i2c.c 23 KB

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