0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/bits.h>
0013 #include <linux/delay.h>
0014 #include <linux/freezer.h>
0015 #include <linux/hwmon.h>
0016 #include <linux/kthread.h>
0017 #include <linux/i2c.h>
0018 #include <linux/list.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/of_device.h>
0022 #include <linux/pm_runtime.h>
0023 #include <linux/nvmem-provider.h>
0024 #include <linux/regmap.h>
0025 #include <linux/sched.h>
0026 #include <linux/slab.h>
0027 #include <linux/videodev2.h>
0028 #include <media/v4l2-common.h>
0029 #include <media/v4l2-device.h>
0030 #include <media/v4l2-event.h>
0031 #include <media/v4l2-fh.h>
0032 #include <media/v4l2-ioctl.h>
0033 #include <media/videobuf2-v4l2.h>
0034 #include <media/videobuf2-vmalloc.h>
0035
0036 #define VIDEO_I2C_DRIVER "video-i2c"
0037
0038
0039 #define AMG88XX_REG_PCTL 0x00
0040 #define AMG88XX_PCTL_NORMAL 0x00
0041 #define AMG88XX_PCTL_SLEEP 0x10
0042
0043
0044 #define AMG88XX_REG_RST 0x01
0045 #define AMG88XX_RST_FLAG 0x30
0046 #define AMG88XX_RST_INIT 0x3f
0047
0048
0049 #define AMG88XX_REG_FPSC 0x02
0050 #define AMG88XX_FPSC_1FPS BIT(0)
0051
0052
0053 #define AMG88XX_REG_TTHL 0x0e
0054
0055
0056 #define AMG88XX_REG_T01L 0x80
0057
0058
0059 #define MLX90640_RAM_START_ADDR 0x0400
0060
0061
0062 #define MLX90640_EEPROM_START_ADDR 0x2400
0063
0064
0065 #define MLX90640_REG_CTL1 0x800d
0066 #define MLX90640_REG_CTL1_MASK GENMASK(9, 7)
0067 #define MLX90640_REG_CTL1_MASK_SHIFT 7
0068
0069 struct video_i2c_chip;
0070
0071 struct video_i2c_buffer {
0072 struct vb2_v4l2_buffer vb;
0073 struct list_head list;
0074 };
0075
0076 struct video_i2c_data {
0077 struct regmap *regmap;
0078 const struct video_i2c_chip *chip;
0079 struct mutex lock;
0080 spinlock_t slock;
0081 unsigned int sequence;
0082 struct mutex queue_lock;
0083
0084 struct v4l2_device v4l2_dev;
0085 struct video_device vdev;
0086 struct vb2_queue vb_vidq;
0087
0088 struct task_struct *kthread_vid_cap;
0089 struct list_head vid_cap_active;
0090
0091 struct v4l2_fract frame_interval;
0092 };
0093
0094 static const struct v4l2_fmtdesc amg88xx_format = {
0095 .pixelformat = V4L2_PIX_FMT_Y12,
0096 };
0097
0098 static const struct v4l2_frmsize_discrete amg88xx_size = {
0099 .width = 8,
0100 .height = 8,
0101 };
0102
0103 static const struct v4l2_fmtdesc mlx90640_format = {
0104 .pixelformat = V4L2_PIX_FMT_Y16_BE,
0105 };
0106
0107 static const struct v4l2_frmsize_discrete mlx90640_size = {
0108 .width = 32,
0109 .height = 26,
0110 };
0111
0112 static const struct regmap_config amg88xx_regmap_config = {
0113 .reg_bits = 8,
0114 .val_bits = 8,
0115 .max_register = 0xff
0116 };
0117
0118 static const struct regmap_config mlx90640_regmap_config = {
0119 .reg_bits = 16,
0120 .val_bits = 16,
0121 };
0122
0123 struct video_i2c_chip {
0124
0125 const struct v4l2_fmtdesc *format;
0126 const struct v4l2_frmsize_discrete *size;
0127
0128
0129 const struct v4l2_fract *frame_intervals;
0130 unsigned int num_frame_intervals;
0131
0132
0133 unsigned int buffer_size;
0134
0135
0136 unsigned int bpp;
0137
0138 const struct regmap_config *regmap_config;
0139 struct nvmem_config *nvmem_config;
0140
0141
0142 int (*setup)(struct video_i2c_data *data);
0143
0144
0145 int (*xfer)(struct video_i2c_data *data, char *buf);
0146
0147
0148 int (*set_power)(struct video_i2c_data *data, bool on);
0149
0150
0151 int (*hwmon_init)(struct video_i2c_data *data);
0152 };
0153
0154 static int mlx90640_nvram_read(void *priv, unsigned int offset, void *val,
0155 size_t bytes)
0156 {
0157 struct video_i2c_data *data = priv;
0158
0159 return regmap_bulk_read(data->regmap, MLX90640_EEPROM_START_ADDR + offset, val, bytes);
0160 }
0161
0162 static struct nvmem_config mlx90640_nvram_config = {
0163 .name = "mlx90640_nvram",
0164 .word_size = 2,
0165 .stride = 1,
0166 .size = 1664,
0167 .reg_read = mlx90640_nvram_read,
0168 };
0169
0170 static int amg88xx_xfer(struct video_i2c_data *data, char *buf)
0171 {
0172 return regmap_bulk_read(data->regmap, AMG88XX_REG_T01L, buf,
0173 data->chip->buffer_size);
0174 }
0175
0176 static int mlx90640_xfer(struct video_i2c_data *data, char *buf)
0177 {
0178 return regmap_bulk_read(data->regmap, MLX90640_RAM_START_ADDR, buf,
0179 data->chip->buffer_size);
0180 }
0181
0182 static int amg88xx_setup(struct video_i2c_data *data)
0183 {
0184 unsigned int mask = AMG88XX_FPSC_1FPS;
0185 unsigned int val;
0186
0187 if (data->frame_interval.numerator == data->frame_interval.denominator)
0188 val = mask;
0189 else
0190 val = 0;
0191
0192 return regmap_update_bits(data->regmap, AMG88XX_REG_FPSC, mask, val);
0193 }
0194
0195 static int mlx90640_setup(struct video_i2c_data *data)
0196 {
0197 unsigned int n, idx;
0198
0199 for (n = 0; n < data->chip->num_frame_intervals - 1; n++) {
0200 if (V4L2_FRACT_COMPARE(data->frame_interval, ==,
0201 data->chip->frame_intervals[n]))
0202 break;
0203 }
0204
0205 idx = data->chip->num_frame_intervals - n - 1;
0206
0207 return regmap_update_bits(data->regmap, MLX90640_REG_CTL1,
0208 MLX90640_REG_CTL1_MASK,
0209 idx << MLX90640_REG_CTL1_MASK_SHIFT);
0210 }
0211
0212 static int amg88xx_set_power_on(struct video_i2c_data *data)
0213 {
0214 int ret;
0215
0216 ret = regmap_write(data->regmap, AMG88XX_REG_PCTL, AMG88XX_PCTL_NORMAL);
0217 if (ret)
0218 return ret;
0219
0220 msleep(50);
0221
0222 ret = regmap_write(data->regmap, AMG88XX_REG_RST, AMG88XX_RST_INIT);
0223 if (ret)
0224 return ret;
0225
0226 usleep_range(2000, 3000);
0227
0228 ret = regmap_write(data->regmap, AMG88XX_REG_RST, AMG88XX_RST_FLAG);
0229 if (ret)
0230 return ret;
0231
0232
0233
0234
0235 msleep(200);
0236
0237 return 0;
0238 }
0239
0240 static int amg88xx_set_power_off(struct video_i2c_data *data)
0241 {
0242 int ret;
0243
0244 ret = regmap_write(data->regmap, AMG88XX_REG_PCTL, AMG88XX_PCTL_SLEEP);
0245 if (ret)
0246 return ret;
0247
0248
0249
0250
0251
0252 msleep(100);
0253
0254 return 0;
0255 }
0256
0257 static int amg88xx_set_power(struct video_i2c_data *data, bool on)
0258 {
0259 if (on)
0260 return amg88xx_set_power_on(data);
0261
0262 return amg88xx_set_power_off(data);
0263 }
0264
0265 #if IS_REACHABLE(CONFIG_HWMON)
0266
0267 static const u32 amg88xx_temp_config[] = {
0268 HWMON_T_INPUT,
0269 0
0270 };
0271
0272 static const struct hwmon_channel_info amg88xx_temp = {
0273 .type = hwmon_temp,
0274 .config = amg88xx_temp_config,
0275 };
0276
0277 static const struct hwmon_channel_info *amg88xx_info[] = {
0278 &amg88xx_temp,
0279 NULL
0280 };
0281
0282 static umode_t amg88xx_is_visible(const void *drvdata,
0283 enum hwmon_sensor_types type,
0284 u32 attr, int channel)
0285 {
0286 return 0444;
0287 }
0288
0289 static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type,
0290 u32 attr, int channel, long *val)
0291 {
0292 struct video_i2c_data *data = dev_get_drvdata(dev);
0293 __le16 buf;
0294 int tmp;
0295
0296 tmp = pm_runtime_resume_and_get(regmap_get_device(data->regmap));
0297 if (tmp < 0)
0298 return tmp;
0299
0300 tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2);
0301 pm_runtime_mark_last_busy(regmap_get_device(data->regmap));
0302 pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
0303 if (tmp)
0304 return tmp;
0305
0306 tmp = le16_to_cpu(buf);
0307
0308
0309
0310
0311
0312
0313 if (tmp & BIT(11))
0314 tmp = -(tmp & 0x7ff);
0315
0316 *val = (tmp * 625) / 10;
0317
0318 return 0;
0319 }
0320
0321 static const struct hwmon_ops amg88xx_hwmon_ops = {
0322 .is_visible = amg88xx_is_visible,
0323 .read = amg88xx_read,
0324 };
0325
0326 static const struct hwmon_chip_info amg88xx_chip_info = {
0327 .ops = &amg88xx_hwmon_ops,
0328 .info = amg88xx_info,
0329 };
0330
0331 static int amg88xx_hwmon_init(struct video_i2c_data *data)
0332 {
0333 struct device *dev = regmap_get_device(data->regmap);
0334 void *hwmon = devm_hwmon_device_register_with_info(dev, "amg88xx", data,
0335 &amg88xx_chip_info, NULL);
0336
0337 return PTR_ERR_OR_ZERO(hwmon);
0338 }
0339 #else
0340 #define amg88xx_hwmon_init NULL
0341 #endif
0342
0343 enum {
0344 AMG88XX,
0345 MLX90640,
0346 };
0347
0348 static const struct v4l2_fract amg88xx_frame_intervals[] = {
0349 { 1, 10 },
0350 { 1, 1 },
0351 };
0352
0353 static const struct v4l2_fract mlx90640_frame_intervals[] = {
0354 { 1, 64 },
0355 { 1, 32 },
0356 { 1, 16 },
0357 { 1, 8 },
0358 { 1, 4 },
0359 { 1, 2 },
0360 { 1, 1 },
0361 { 2, 1 },
0362 };
0363
0364 static const struct video_i2c_chip video_i2c_chip[] = {
0365 [AMG88XX] = {
0366 .size = &amg88xx_size,
0367 .format = &amg88xx_format,
0368 .frame_intervals = amg88xx_frame_intervals,
0369 .num_frame_intervals = ARRAY_SIZE(amg88xx_frame_intervals),
0370 .buffer_size = 128,
0371 .bpp = 16,
0372 .regmap_config = &amg88xx_regmap_config,
0373 .setup = &amg88xx_setup,
0374 .xfer = &amg88xx_xfer,
0375 .set_power = amg88xx_set_power,
0376 .hwmon_init = amg88xx_hwmon_init,
0377 },
0378 [MLX90640] = {
0379 .size = &mlx90640_size,
0380 .format = &mlx90640_format,
0381 .frame_intervals = mlx90640_frame_intervals,
0382 .num_frame_intervals = ARRAY_SIZE(mlx90640_frame_intervals),
0383 .buffer_size = 1664,
0384 .bpp = 16,
0385 .regmap_config = &mlx90640_regmap_config,
0386 .nvmem_config = &mlx90640_nvram_config,
0387 .setup = mlx90640_setup,
0388 .xfer = mlx90640_xfer,
0389 },
0390 };
0391
0392 static const struct v4l2_file_operations video_i2c_fops = {
0393 .owner = THIS_MODULE,
0394 .open = v4l2_fh_open,
0395 .release = vb2_fop_release,
0396 .poll = vb2_fop_poll,
0397 .read = vb2_fop_read,
0398 .mmap = vb2_fop_mmap,
0399 .unlocked_ioctl = video_ioctl2,
0400 };
0401
0402 static int queue_setup(struct vb2_queue *vq,
0403 unsigned int *nbuffers, unsigned int *nplanes,
0404 unsigned int sizes[], struct device *alloc_devs[])
0405 {
0406 struct video_i2c_data *data = vb2_get_drv_priv(vq);
0407 unsigned int size = data->chip->buffer_size;
0408
0409 if (vq->num_buffers + *nbuffers < 2)
0410 *nbuffers = 2;
0411
0412 if (*nplanes)
0413 return sizes[0] < size ? -EINVAL : 0;
0414
0415 *nplanes = 1;
0416 sizes[0] = size;
0417
0418 return 0;
0419 }
0420
0421 static int buffer_prepare(struct vb2_buffer *vb)
0422 {
0423 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0424 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
0425 unsigned int size = data->chip->buffer_size;
0426
0427 if (vb2_plane_size(vb, 0) < size)
0428 return -EINVAL;
0429
0430 vbuf->field = V4L2_FIELD_NONE;
0431 vb2_set_plane_payload(vb, 0, size);
0432
0433 return 0;
0434 }
0435
0436 static void buffer_queue(struct vb2_buffer *vb)
0437 {
0438 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0439 struct video_i2c_data *data = vb2_get_drv_priv(vb->vb2_queue);
0440 struct video_i2c_buffer *buf =
0441 container_of(vbuf, struct video_i2c_buffer, vb);
0442
0443 spin_lock(&data->slock);
0444 list_add_tail(&buf->list, &data->vid_cap_active);
0445 spin_unlock(&data->slock);
0446 }
0447
0448 static int video_i2c_thread_vid_cap(void *priv)
0449 {
0450 struct video_i2c_data *data = priv;
0451 u32 delay = mult_frac(1000000UL, data->frame_interval.numerator,
0452 data->frame_interval.denominator);
0453 s64 end_us = ktime_to_us(ktime_get());
0454
0455 set_freezable();
0456
0457 do {
0458 struct video_i2c_buffer *vid_cap_buf = NULL;
0459 s64 current_us;
0460 int schedule_delay;
0461
0462 try_to_freeze();
0463
0464 spin_lock(&data->slock);
0465
0466 if (!list_empty(&data->vid_cap_active)) {
0467 vid_cap_buf = list_last_entry(&data->vid_cap_active,
0468 struct video_i2c_buffer, list);
0469 list_del(&vid_cap_buf->list);
0470 }
0471
0472 spin_unlock(&data->slock);
0473
0474 if (vid_cap_buf) {
0475 struct vb2_buffer *vb2_buf = &vid_cap_buf->vb.vb2_buf;
0476 void *vbuf = vb2_plane_vaddr(vb2_buf, 0);
0477 int ret;
0478
0479 ret = data->chip->xfer(data, vbuf);
0480 vb2_buf->timestamp = ktime_get_ns();
0481 vid_cap_buf->vb.sequence = data->sequence++;
0482 vb2_buffer_done(vb2_buf, ret ?
0483 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
0484 }
0485
0486 end_us += delay;
0487 current_us = ktime_to_us(ktime_get());
0488 if (current_us < end_us) {
0489 schedule_delay = end_us - current_us;
0490 usleep_range(schedule_delay * 3 / 4, schedule_delay);
0491 } else {
0492 end_us = current_us;
0493 }
0494 } while (!kthread_should_stop());
0495
0496 return 0;
0497 }
0498
0499 static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state)
0500 {
0501 struct video_i2c_data *data = vb2_get_drv_priv(vq);
0502 struct video_i2c_buffer *buf, *tmp;
0503
0504 spin_lock(&data->slock);
0505
0506 list_for_each_entry_safe(buf, tmp, &data->vid_cap_active, list) {
0507 list_del(&buf->list);
0508 vb2_buffer_done(&buf->vb.vb2_buf, state);
0509 }
0510
0511 spin_unlock(&data->slock);
0512 }
0513
0514 static int start_streaming(struct vb2_queue *vq, unsigned int count)
0515 {
0516 struct video_i2c_data *data = vb2_get_drv_priv(vq);
0517 struct device *dev = regmap_get_device(data->regmap);
0518 int ret;
0519
0520 if (data->kthread_vid_cap)
0521 return 0;
0522
0523 ret = pm_runtime_resume_and_get(dev);
0524 if (ret < 0)
0525 goto error_del_list;
0526
0527 ret = data->chip->setup(data);
0528 if (ret)
0529 goto error_rpm_put;
0530
0531 data->sequence = 0;
0532 data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data,
0533 "%s-vid-cap", data->v4l2_dev.name);
0534 ret = PTR_ERR_OR_ZERO(data->kthread_vid_cap);
0535 if (!ret)
0536 return 0;
0537
0538 error_rpm_put:
0539 pm_runtime_mark_last_busy(dev);
0540 pm_runtime_put_autosuspend(dev);
0541 error_del_list:
0542 video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED);
0543
0544 return ret;
0545 }
0546
0547 static void stop_streaming(struct vb2_queue *vq)
0548 {
0549 struct video_i2c_data *data = vb2_get_drv_priv(vq);
0550
0551 if (data->kthread_vid_cap == NULL)
0552 return;
0553
0554 kthread_stop(data->kthread_vid_cap);
0555 data->kthread_vid_cap = NULL;
0556 pm_runtime_mark_last_busy(regmap_get_device(data->regmap));
0557 pm_runtime_put_autosuspend(regmap_get_device(data->regmap));
0558
0559 video_i2c_del_list(vq, VB2_BUF_STATE_ERROR);
0560 }
0561
0562 static const struct vb2_ops video_i2c_video_qops = {
0563 .queue_setup = queue_setup,
0564 .buf_prepare = buffer_prepare,
0565 .buf_queue = buffer_queue,
0566 .start_streaming = start_streaming,
0567 .stop_streaming = stop_streaming,
0568 .wait_prepare = vb2_ops_wait_prepare,
0569 .wait_finish = vb2_ops_wait_finish,
0570 };
0571
0572 static int video_i2c_querycap(struct file *file, void *priv,
0573 struct v4l2_capability *vcap)
0574 {
0575 struct video_i2c_data *data = video_drvdata(file);
0576 struct device *dev = regmap_get_device(data->regmap);
0577 struct i2c_client *client = to_i2c_client(dev);
0578
0579 strscpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver));
0580 strscpy(vcap->card, data->vdev.name, sizeof(vcap->card));
0581
0582 sprintf(vcap->bus_info, "I2C:%d-%d", client->adapter->nr, client->addr);
0583
0584 return 0;
0585 }
0586
0587 static int video_i2c_g_input(struct file *file, void *fh, unsigned int *inp)
0588 {
0589 *inp = 0;
0590
0591 return 0;
0592 }
0593
0594 static int video_i2c_s_input(struct file *file, void *fh, unsigned int inp)
0595 {
0596 return (inp > 0) ? -EINVAL : 0;
0597 }
0598
0599 static int video_i2c_enum_input(struct file *file, void *fh,
0600 struct v4l2_input *vin)
0601 {
0602 if (vin->index > 0)
0603 return -EINVAL;
0604
0605 strscpy(vin->name, "Camera", sizeof(vin->name));
0606
0607 vin->type = V4L2_INPUT_TYPE_CAMERA;
0608
0609 return 0;
0610 }
0611
0612 static int video_i2c_enum_fmt_vid_cap(struct file *file, void *fh,
0613 struct v4l2_fmtdesc *fmt)
0614 {
0615 struct video_i2c_data *data = video_drvdata(file);
0616 enum v4l2_buf_type type = fmt->type;
0617
0618 if (fmt->index > 0)
0619 return -EINVAL;
0620
0621 *fmt = *data->chip->format;
0622 fmt->type = type;
0623
0624 return 0;
0625 }
0626
0627 static int video_i2c_enum_framesizes(struct file *file, void *fh,
0628 struct v4l2_frmsizeenum *fsize)
0629 {
0630 const struct video_i2c_data *data = video_drvdata(file);
0631 const struct v4l2_frmsize_discrete *size = data->chip->size;
0632
0633
0634 if (fsize->index > 0)
0635 return -EINVAL;
0636
0637 if (fsize->pixel_format != data->chip->format->pixelformat)
0638 return -EINVAL;
0639
0640 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
0641 fsize->discrete.width = size->width;
0642 fsize->discrete.height = size->height;
0643
0644 return 0;
0645 }
0646
0647 static int video_i2c_enum_frameintervals(struct file *file, void *priv,
0648 struct v4l2_frmivalenum *fe)
0649 {
0650 const struct video_i2c_data *data = video_drvdata(file);
0651 const struct v4l2_frmsize_discrete *size = data->chip->size;
0652
0653 if (fe->index >= data->chip->num_frame_intervals)
0654 return -EINVAL;
0655
0656 if (fe->width != size->width || fe->height != size->height)
0657 return -EINVAL;
0658
0659 fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
0660 fe->discrete = data->chip->frame_intervals[fe->index];
0661
0662 return 0;
0663 }
0664
0665 static int video_i2c_try_fmt_vid_cap(struct file *file, void *fh,
0666 struct v4l2_format *fmt)
0667 {
0668 const struct video_i2c_data *data = video_drvdata(file);
0669 const struct v4l2_frmsize_discrete *size = data->chip->size;
0670 struct v4l2_pix_format *pix = &fmt->fmt.pix;
0671 unsigned int bpp = data->chip->bpp / 8;
0672
0673 pix->width = size->width;
0674 pix->height = size->height;
0675 pix->pixelformat = data->chip->format->pixelformat;
0676 pix->field = V4L2_FIELD_NONE;
0677 pix->bytesperline = pix->width * bpp;
0678 pix->sizeimage = pix->bytesperline * pix->height;
0679 pix->colorspace = V4L2_COLORSPACE_RAW;
0680
0681 return 0;
0682 }
0683
0684 static int video_i2c_s_fmt_vid_cap(struct file *file, void *fh,
0685 struct v4l2_format *fmt)
0686 {
0687 struct video_i2c_data *data = video_drvdata(file);
0688
0689 if (vb2_is_busy(&data->vb_vidq))
0690 return -EBUSY;
0691
0692 return video_i2c_try_fmt_vid_cap(file, fh, fmt);
0693 }
0694
0695 static int video_i2c_g_parm(struct file *filp, void *priv,
0696 struct v4l2_streamparm *parm)
0697 {
0698 struct video_i2c_data *data = video_drvdata(filp);
0699
0700 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
0701 return -EINVAL;
0702
0703 parm->parm.capture.readbuffers = 1;
0704 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
0705 parm->parm.capture.timeperframe = data->frame_interval;
0706
0707 return 0;
0708 }
0709
0710 static int video_i2c_s_parm(struct file *filp, void *priv,
0711 struct v4l2_streamparm *parm)
0712 {
0713 struct video_i2c_data *data = video_drvdata(filp);
0714 int i;
0715
0716 for (i = 0; i < data->chip->num_frame_intervals - 1; i++) {
0717 if (V4L2_FRACT_COMPARE(parm->parm.capture.timeperframe, <=,
0718 data->chip->frame_intervals[i]))
0719 break;
0720 }
0721 data->frame_interval = data->chip->frame_intervals[i];
0722
0723 return video_i2c_g_parm(filp, priv, parm);
0724 }
0725
0726 static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = {
0727 .vidioc_querycap = video_i2c_querycap,
0728 .vidioc_g_input = video_i2c_g_input,
0729 .vidioc_s_input = video_i2c_s_input,
0730 .vidioc_enum_input = video_i2c_enum_input,
0731 .vidioc_enum_fmt_vid_cap = video_i2c_enum_fmt_vid_cap,
0732 .vidioc_enum_framesizes = video_i2c_enum_framesizes,
0733 .vidioc_enum_frameintervals = video_i2c_enum_frameintervals,
0734 .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
0735 .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap,
0736 .vidioc_g_parm = video_i2c_g_parm,
0737 .vidioc_s_parm = video_i2c_s_parm,
0738 .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap,
0739 .vidioc_reqbufs = vb2_ioctl_reqbufs,
0740 .vidioc_create_bufs = vb2_ioctl_create_bufs,
0741 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
0742 .vidioc_querybuf = vb2_ioctl_querybuf,
0743 .vidioc_qbuf = vb2_ioctl_qbuf,
0744 .vidioc_dqbuf = vb2_ioctl_dqbuf,
0745 .vidioc_streamon = vb2_ioctl_streamon,
0746 .vidioc_streamoff = vb2_ioctl_streamoff,
0747 };
0748
0749 static void video_i2c_release(struct video_device *vdev)
0750 {
0751 struct video_i2c_data *data = video_get_drvdata(vdev);
0752
0753 v4l2_device_unregister(&data->v4l2_dev);
0754 mutex_destroy(&data->lock);
0755 mutex_destroy(&data->queue_lock);
0756 regmap_exit(data->regmap);
0757 kfree(data);
0758 }
0759
0760 static int video_i2c_probe(struct i2c_client *client,
0761 const struct i2c_device_id *id)
0762 {
0763 struct video_i2c_data *data;
0764 struct v4l2_device *v4l2_dev;
0765 struct vb2_queue *queue;
0766 int ret = -ENODEV;
0767
0768 data = kzalloc(sizeof(*data), GFP_KERNEL);
0769 if (!data)
0770 return -ENOMEM;
0771
0772 if (dev_fwnode(&client->dev))
0773 data->chip = device_get_match_data(&client->dev);
0774 else if (id)
0775 data->chip = &video_i2c_chip[id->driver_data];
0776 else
0777 goto error_free_device;
0778
0779 data->regmap = regmap_init_i2c(client, data->chip->regmap_config);
0780 if (IS_ERR(data->regmap)) {
0781 ret = PTR_ERR(data->regmap);
0782 goto error_free_device;
0783 }
0784
0785 v4l2_dev = &data->v4l2_dev;
0786 strscpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name));
0787
0788 ret = v4l2_device_register(&client->dev, v4l2_dev);
0789 if (ret < 0)
0790 goto error_regmap_exit;
0791
0792 mutex_init(&data->lock);
0793 mutex_init(&data->queue_lock);
0794
0795 queue = &data->vb_vidq;
0796 queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
0797 queue->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR | VB2_READ;
0798 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
0799 queue->drv_priv = data;
0800 queue->buf_struct_size = sizeof(struct video_i2c_buffer);
0801 queue->min_buffers_needed = 1;
0802 queue->ops = &video_i2c_video_qops;
0803 queue->mem_ops = &vb2_vmalloc_memops;
0804
0805 ret = vb2_queue_init(queue);
0806 if (ret < 0)
0807 goto error_unregister_device;
0808
0809 data->vdev.queue = queue;
0810 data->vdev.queue->lock = &data->queue_lock;
0811
0812 snprintf(data->vdev.name, sizeof(data->vdev.name),
0813 "I2C %d-%d Transport Video",
0814 client->adapter->nr, client->addr);
0815
0816 data->vdev.v4l2_dev = v4l2_dev;
0817 data->vdev.fops = &video_i2c_fops;
0818 data->vdev.lock = &data->lock;
0819 data->vdev.ioctl_ops = &video_i2c_ioctl_ops;
0820 data->vdev.release = video_i2c_release;
0821 data->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
0822 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
0823
0824 spin_lock_init(&data->slock);
0825 INIT_LIST_HEAD(&data->vid_cap_active);
0826
0827 data->frame_interval = data->chip->frame_intervals[0];
0828
0829 video_set_drvdata(&data->vdev, data);
0830 i2c_set_clientdata(client, data);
0831
0832 if (data->chip->set_power) {
0833 ret = data->chip->set_power(data, true);
0834 if (ret)
0835 goto error_unregister_device;
0836 }
0837
0838 pm_runtime_get_noresume(&client->dev);
0839 pm_runtime_set_active(&client->dev);
0840 pm_runtime_enable(&client->dev);
0841 pm_runtime_set_autosuspend_delay(&client->dev, 2000);
0842 pm_runtime_use_autosuspend(&client->dev);
0843
0844 if (data->chip->hwmon_init) {
0845 ret = data->chip->hwmon_init(data);
0846 if (ret < 0) {
0847 dev_warn(&client->dev,
0848 "failed to register hwmon device\n");
0849 }
0850 }
0851
0852 if (data->chip->nvmem_config) {
0853 struct nvmem_config *config = data->chip->nvmem_config;
0854 struct nvmem_device *device;
0855
0856 config->priv = data;
0857 config->dev = &client->dev;
0858
0859 device = devm_nvmem_register(&client->dev, config);
0860
0861 if (IS_ERR(device)) {
0862 dev_warn(&client->dev,
0863 "failed to register nvmem device\n");
0864 }
0865 }
0866
0867 ret = video_register_device(&data->vdev, VFL_TYPE_VIDEO, -1);
0868 if (ret < 0)
0869 goto error_pm_disable;
0870
0871 pm_runtime_mark_last_busy(&client->dev);
0872 pm_runtime_put_autosuspend(&client->dev);
0873
0874 return 0;
0875
0876 error_pm_disable:
0877 pm_runtime_disable(&client->dev);
0878 pm_runtime_set_suspended(&client->dev);
0879 pm_runtime_put_noidle(&client->dev);
0880
0881 if (data->chip->set_power)
0882 data->chip->set_power(data, false);
0883
0884 error_unregister_device:
0885 v4l2_device_unregister(v4l2_dev);
0886 mutex_destroy(&data->lock);
0887 mutex_destroy(&data->queue_lock);
0888
0889 error_regmap_exit:
0890 regmap_exit(data->regmap);
0891
0892 error_free_device:
0893 kfree(data);
0894
0895 return ret;
0896 }
0897
0898 static int video_i2c_remove(struct i2c_client *client)
0899 {
0900 struct video_i2c_data *data = i2c_get_clientdata(client);
0901
0902 pm_runtime_get_sync(&client->dev);
0903 pm_runtime_disable(&client->dev);
0904 pm_runtime_set_suspended(&client->dev);
0905 pm_runtime_put_noidle(&client->dev);
0906
0907 if (data->chip->set_power)
0908 data->chip->set_power(data, false);
0909
0910 video_unregister_device(&data->vdev);
0911
0912 return 0;
0913 }
0914
0915 #ifdef CONFIG_PM
0916
0917 static int video_i2c_pm_runtime_suspend(struct device *dev)
0918 {
0919 struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev));
0920
0921 if (!data->chip->set_power)
0922 return 0;
0923
0924 return data->chip->set_power(data, false);
0925 }
0926
0927 static int video_i2c_pm_runtime_resume(struct device *dev)
0928 {
0929 struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev));
0930
0931 if (!data->chip->set_power)
0932 return 0;
0933
0934 return data->chip->set_power(data, true);
0935 }
0936
0937 #endif
0938
0939 static const struct dev_pm_ops video_i2c_pm_ops = {
0940 SET_RUNTIME_PM_OPS(video_i2c_pm_runtime_suspend,
0941 video_i2c_pm_runtime_resume, NULL)
0942 };
0943
0944 static const struct i2c_device_id video_i2c_id_table[] = {
0945 { "amg88xx", AMG88XX },
0946 { "mlx90640", MLX90640 },
0947 {}
0948 };
0949 MODULE_DEVICE_TABLE(i2c, video_i2c_id_table);
0950
0951 static const struct of_device_id video_i2c_of_match[] = {
0952 { .compatible = "panasonic,amg88xx", .data = &video_i2c_chip[AMG88XX] },
0953 { .compatible = "melexis,mlx90640", .data = &video_i2c_chip[MLX90640] },
0954 {}
0955 };
0956 MODULE_DEVICE_TABLE(of, video_i2c_of_match);
0957
0958 static struct i2c_driver video_i2c_driver = {
0959 .driver = {
0960 .name = VIDEO_I2C_DRIVER,
0961 .of_match_table = video_i2c_of_match,
0962 .pm = &video_i2c_pm_ops,
0963 },
0964 .probe = video_i2c_probe,
0965 .remove = video_i2c_remove,
0966 .id_table = video_i2c_id_table,
0967 };
0968
0969 module_i2c_driver(video_i2c_driver);
0970
0971 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
0972 MODULE_DESCRIPTION("I2C transport video support");
0973 MODULE_LICENSE("GPL v2");