2 * Copyright (c) 2012-2015 Synaptics Incorporated
3 * Copyright (C) 2016 Zodiac Inflight Innovations
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/rmi.h>
12 #include <linux/input.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-vmalloc.h>
20 #include "rmi_driver.h"
22 #define F54_NAME "rmi4_f54"
24 /* F54 data offsets */
25 #define F54_REPORT_DATA_OFFSET 3
26 #define F54_FIFO_OFFSET 1
27 #define F54_NUM_TX_OFFSET 1
28 #define F54_NUM_RX_OFFSET 0
31 #define F54_GET_REPORT 1
32 #define F54_FORCE_CAL 2
34 /* F54 capabilities */
35 #define F54_CAP_BASELINE (1 << 2)
36 #define F54_CAP_IMAGE8 (1 << 3)
37 #define F54_CAP_IMAGE16 (1 << 6)
40 * enum rmi_f54_report_type - RMI4 F54 report types
42 * @F54_8BIT_IMAGE: Normalized 8-Bit Image Report. The capacitance variance
43 * from baseline for each pixel.
45 * @F54_16BIT_IMAGE: Normalized 16-Bit Image Report. The capacitance variance
46 * from baseline for each pixel.
48 * @F54_RAW_16BIT_IMAGE:
49 * Raw 16-Bit Image Report. The raw capacitance for each
52 * @F54_TRUE_BASELINE: True Baseline Report. The baseline capacitance for each
55 * @F54_FULL_RAW_CAP: Full Raw Capacitance Report. The raw capacitance with
56 * low reference set to its minimum value and high
57 * reference set to its maximum value.
59 * @F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
60 * Full Raw Capacitance with Receiver Offset Removed
61 * Report. Set Low reference to its minimum value and high
62 * references to its maximum value, then report the raw
63 * capacitance for each pixel.
65 enum rmi_f54_report_type {
69 F54_RAW_16BIT_IMAGE = 3,
70 F54_TRUE_BASELINE = 9,
71 F54_FULL_RAW_CAP = 19,
72 F54_FULL_RAW_CAP_RX_OFFSET_REMOVED = 20,
76 static const char * const rmi_f54_report_type_names[] = {
77 [F54_REPORT_NONE] = "Unknown",
78 [F54_8BIT_IMAGE] = "Normalized 8-Bit Image",
79 [F54_16BIT_IMAGE] = "Normalized 16-Bit Image",
80 [F54_RAW_16BIT_IMAGE] = "Raw 16-Bit Image",
81 [F54_TRUE_BASELINE] = "True Baseline",
82 [F54_FULL_RAW_CAP] = "Full Raw Capacitance",
83 [F54_FULL_RAW_CAP_RX_OFFSET_REMOVED]
84 = "Full Raw Capacitance RX Offset Removed",
87 struct rmi_f54_reports {
93 struct rmi_function *fn;
101 enum rmi_f54_report_type report_type;
104 struct rmi_f54_reports standard_report[2];
107 struct mutex status_mutex;
108 struct mutex data_mutex;
110 struct workqueue_struct *workqueue;
111 struct delayed_work work;
112 unsigned long timeout;
114 struct completion cmd_done;
117 struct v4l2_device v4l2;
118 struct v4l2_pix_format format;
119 struct video_device vdev;
120 struct vb2_queue queue;
123 enum rmi_f54_report_type inputs[F54_MAX_REPORT_TYPE];
127 * Basic checks on report_type to ensure we write a valid type
130 static bool is_f54_report_type_valid(struct f54_data *f54,
131 enum rmi_f54_report_type reptype)
135 return f54->capabilities & F54_CAP_IMAGE8;
136 case F54_16BIT_IMAGE:
137 case F54_RAW_16BIT_IMAGE:
138 return f54->capabilities & F54_CAP_IMAGE16;
139 case F54_TRUE_BASELINE:
140 return f54->capabilities & F54_CAP_IMAGE16;
141 case F54_FULL_RAW_CAP:
142 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
149 static enum rmi_f54_report_type rmi_f54_get_reptype(struct f54_data *f54,
152 if (i >= F54_MAX_REPORT_TYPE)
153 return F54_REPORT_NONE;
155 return f54->inputs[i];
158 static void rmi_f54_create_input_map(struct f54_data *f54)
161 enum rmi_f54_report_type reptype;
163 for (reptype = 1; reptype < F54_MAX_REPORT_TYPE; reptype++) {
164 if (!is_f54_report_type_valid(f54, reptype))
167 f54->inputs[i++] = reptype;
170 /* Remaining values are zero via kzalloc */
173 static int rmi_f54_request_report(struct rmi_function *fn, u8 report_type)
175 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
176 struct rmi_device *rmi_dev = fn->rmi_dev;
179 /* Write Report Type into F54_AD_Data0 */
180 if (f54->report_type != report_type) {
181 error = rmi_write(rmi_dev, f54->fn->fd.data_base_addr,
185 f54->report_type = report_type;
189 * Small delay after disabling interrupts to avoid race condition
190 * in firmare. This value is a bit higher than absolutely necessary.
191 * Should be removed once issue is resolved in firmware.
193 usleep_range(2000, 3000);
195 mutex_lock(&f54->data_mutex);
197 error = rmi_write(rmi_dev, fn->fd.command_base_addr, F54_GET_REPORT);
201 init_completion(&f54->cmd_done);
204 f54->timeout = jiffies + msecs_to_jiffies(100);
206 queue_delayed_work(f54->workqueue, &f54->work, 0);
209 mutex_unlock(&f54->data_mutex);
214 static size_t rmi_f54_get_report_size(struct f54_data *f54)
216 struct rmi_device *rmi_dev = f54->fn->rmi_dev;
217 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
218 u8 rx = drv_data->num_rx_electrodes ? : f54->num_rx_electrodes;
219 u8 tx = drv_data->num_tx_electrodes ? : f54->num_tx_electrodes;
222 switch (rmi_f54_get_reptype(f54, f54->input)) {
226 case F54_16BIT_IMAGE:
227 case F54_RAW_16BIT_IMAGE:
228 case F54_TRUE_BASELINE:
229 case F54_FULL_RAW_CAP:
230 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
231 size = sizeof(u16) * rx * tx;
240 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype, u32 *pixfmt)
246 *pixfmt = V4L2_TCH_FMT_DELTA_TD08;
249 case F54_16BIT_IMAGE:
250 *pixfmt = V4L2_TCH_FMT_DELTA_TD16;
253 case F54_RAW_16BIT_IMAGE:
254 case F54_TRUE_BASELINE:
255 case F54_FULL_RAW_CAP:
256 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
257 *pixfmt = V4L2_TCH_FMT_TU16;
260 case F54_REPORT_NONE:
261 case F54_MAX_REPORT_TYPE:
269 static const struct v4l2_file_operations rmi_f54_video_fops = {
270 .owner = THIS_MODULE,
271 .open = v4l2_fh_open,
272 .release = vb2_fop_release,
273 .unlocked_ioctl = video_ioctl2,
274 .read = vb2_fop_read,
275 .mmap = vb2_fop_mmap,
276 .poll = vb2_fop_poll,
279 static int rmi_f54_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
280 unsigned int *nplanes, unsigned int sizes[],
281 struct device *alloc_devs[])
283 struct f54_data *f54 = q->drv_priv;
286 return sizes[0] < rmi_f54_get_report_size(f54) ? -EINVAL : 0;
289 sizes[0] = rmi_f54_get_report_size(f54);
294 static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
296 struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
298 enum vb2_buffer_state state;
299 enum rmi_f54_report_type reptype;
302 mutex_lock(&f54->status_mutex);
304 reptype = rmi_f54_get_reptype(f54, f54->input);
305 if (reptype == F54_REPORT_NONE) {
306 state = VB2_BUF_STATE_ERROR;
311 state = VB2_BUF_STATE_ERROR;
315 ret = rmi_f54_request_report(f54->fn, reptype);
317 dev_err(&f54->fn->dev, "Error requesting F54 report\n");
318 state = VB2_BUF_STATE_ERROR;
323 mutex_lock(&f54->data_mutex);
325 while (f54->is_busy) {
326 mutex_unlock(&f54->data_mutex);
327 if (!wait_for_completion_timeout(&f54->cmd_done,
328 msecs_to_jiffies(1000))) {
329 dev_err(&f54->fn->dev, "Timed out\n");
330 state = VB2_BUF_STATE_ERROR;
333 mutex_lock(&f54->data_mutex);
336 ptr = vb2_plane_vaddr(vb, 0);
338 dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
339 state = VB2_BUF_STATE_ERROR;
343 memcpy(ptr, f54->report_data, f54->report_size);
344 vb2_set_plane_payload(vb, 0, rmi_f54_get_report_size(f54));
345 state = VB2_BUF_STATE_DONE;
348 mutex_unlock(&f54->data_mutex);
350 vb2_buffer_done(vb, state);
351 mutex_unlock(&f54->status_mutex);
354 /* V4L2 structures */
355 static const struct vb2_ops rmi_f54_queue_ops = {
356 .queue_setup = rmi_f54_queue_setup,
357 .buf_queue = rmi_f54_buffer_queue,
358 .wait_prepare = vb2_ops_wait_prepare,
359 .wait_finish = vb2_ops_wait_finish,
362 static const struct vb2_queue rmi_f54_queue = {
363 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
364 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
365 .buf_struct_size = sizeof(struct vb2_buffer),
366 .ops = &rmi_f54_queue_ops,
367 .mem_ops = &vb2_vmalloc_memops,
368 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
369 .min_buffers_needed = 1,
372 static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
373 struct v4l2_capability *cap)
375 struct f54_data *f54 = video_drvdata(file);
377 strlcpy(cap->driver, F54_NAME, sizeof(cap->driver));
378 strlcpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
379 snprintf(cap->bus_info, sizeof(cap->bus_info),
380 "rmi4:%s", dev_name(&f54->fn->dev));
385 static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
386 struct v4l2_input *i)
388 struct f54_data *f54 = video_drvdata(file);
389 enum rmi_f54_report_type reptype;
391 reptype = rmi_f54_get_reptype(f54, i->index);
392 if (reptype == F54_REPORT_NONE)
395 i->type = V4L2_INPUT_TYPE_TOUCH;
397 strlcpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
401 static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
403 struct rmi_device *rmi_dev = f54->fn->rmi_dev;
404 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
405 u8 rx = drv_data->num_rx_electrodes ? : f54->num_rx_electrodes;
406 u8 tx = drv_data->num_tx_electrodes ? : f54->num_tx_electrodes;
407 struct v4l2_pix_format *f = &f54->format;
408 enum rmi_f54_report_type reptype;
411 reptype = rmi_f54_get_reptype(f54, i);
412 if (reptype == F54_REPORT_NONE)
415 ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
423 f->field = V4L2_FIELD_NONE;
424 f->colorspace = V4L2_COLORSPACE_RAW;
425 f->bytesperline = f->width * sizeof(u16);
426 f->sizeimage = f->width * f->height * sizeof(u16);
431 static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
433 return rmi_f54_set_input(video_drvdata(file), i);
436 static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
439 struct f54_data *f54 = video_drvdata(file);
446 static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
447 struct v4l2_format *f)
449 struct f54_data *f54 = video_drvdata(file);
451 f->fmt.pix = f54->format;
456 static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
457 struct v4l2_fmtdesc *fmt)
459 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
462 switch (fmt->index) {
464 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
468 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD08;
472 fmt->pixelformat = V4L2_TCH_FMT_TU16;
482 static int rmi_f54_vidioc_g_parm(struct file *file, void *fh,
483 struct v4l2_streamparm *a)
485 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
488 a->parm.capture.readbuffers = 1;
489 a->parm.capture.timeperframe.numerator = 1;
490 a->parm.capture.timeperframe.denominator = 10;
494 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops = {
495 .vidioc_querycap = rmi_f54_vidioc_querycap,
497 .vidioc_enum_fmt_vid_cap = rmi_f54_vidioc_enum_fmt,
498 .vidioc_s_fmt_vid_cap = rmi_f54_vidioc_fmt,
499 .vidioc_g_fmt_vid_cap = rmi_f54_vidioc_fmt,
500 .vidioc_try_fmt_vid_cap = rmi_f54_vidioc_fmt,
501 .vidioc_g_parm = rmi_f54_vidioc_g_parm,
503 .vidioc_enum_input = rmi_f54_vidioc_enum_input,
504 .vidioc_g_input = rmi_f54_vidioc_g_input,
505 .vidioc_s_input = rmi_f54_vidioc_s_input,
507 .vidioc_reqbufs = vb2_ioctl_reqbufs,
508 .vidioc_create_bufs = vb2_ioctl_create_bufs,
509 .vidioc_querybuf = vb2_ioctl_querybuf,
510 .vidioc_qbuf = vb2_ioctl_qbuf,
511 .vidioc_dqbuf = vb2_ioctl_dqbuf,
512 .vidioc_expbuf = vb2_ioctl_expbuf,
514 .vidioc_streamon = vb2_ioctl_streamon,
515 .vidioc_streamoff = vb2_ioctl_streamoff,
518 static const struct video_device rmi_f54_video_device = {
519 .name = "Synaptics RMI4",
520 .fops = &rmi_f54_video_fops,
521 .ioctl_ops = &rmi_f54_video_ioctl_ops,
522 .release = video_device_release_empty,
523 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
524 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
527 static void rmi_f54_work(struct work_struct *work)
529 struct f54_data *f54 = container_of(work, struct f54_data, work.work);
530 struct rmi_function *fn = f54->fn;
532 struct rmi_f54_reports *report;
538 data = f54->report_data;
539 report_size = rmi_f54_get_report_size(f54);
540 if (report_size == 0) {
541 dev_err(&fn->dev, "Bad report size, report type=%d\n",
544 goto error; /* retry won't help */
546 f54->standard_report[0].size = report_size;
547 report = f54->standard_report;
549 mutex_lock(&f54->data_mutex);
552 * Need to check if command has completed.
553 * If not try again later.
555 error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
558 dev_err(&fn->dev, "Failed to read back command\n");
561 if (command & F54_GET_REPORT) {
562 if (time_after(jiffies, f54->timeout)) {
563 dev_err(&fn->dev, "Get report command timed out\n");
570 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
573 for (; report->size; report++) {
574 fifo[0] = report->start & 0xff;
575 fifo[1] = (report->start >> 8) & 0xff;
576 error = rmi_write_block(fn->rmi_dev,
577 fn->fd.data_base_addr + F54_FIFO_OFFSET,
580 dev_err(&fn->dev, "Failed to set fifo start offset\n");
584 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
585 F54_REPORT_DATA_OFFSET, data,
588 dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
589 __func__, report->size, error);
592 data += report->size;
593 report_size += report->size;
597 f54->report_size = error ? 0 : report_size;
602 if (report_size == 0 && !error) {
603 queue_delayed_work(f54->workqueue, &f54->work,
604 msecs_to_jiffies(1));
606 f54->is_busy = false;
607 complete(&f54->cmd_done);
610 mutex_unlock(&f54->data_mutex);
613 static int rmi_f54_config(struct rmi_function *fn)
615 struct rmi_driver *drv = fn->rmi_dev->driver;
617 drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
622 static int rmi_f54_detect(struct rmi_function *fn)
625 struct f54_data *f54;
628 f54 = dev_get_drvdata(&fn->dev);
630 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
633 dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
638 f54->num_rx_electrodes = buf[0];
639 f54->num_tx_electrodes = buf[1];
640 f54->capabilities = buf[2];
641 f54->clock_rate = buf[3] | (buf[4] << 8);
642 f54->family = buf[5];
644 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
645 f54->num_rx_electrodes);
646 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
647 f54->num_tx_electrodes);
648 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
650 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
652 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
655 f54->is_busy = false;
660 static int rmi_f54_probe(struct rmi_function *fn)
662 struct f54_data *f54;
666 f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
671 dev_set_drvdata(&fn->dev, f54);
673 ret = rmi_f54_detect(fn);
677 mutex_init(&f54->data_mutex);
678 mutex_init(&f54->status_mutex);
680 rx = f54->num_rx_electrodes;
681 tx = f54->num_tx_electrodes;
682 f54->report_data = devm_kzalloc(&fn->dev,
683 sizeof(u16) * tx * rx,
685 if (f54->report_data == NULL)
688 INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
690 f54->workqueue = create_singlethread_workqueue("rmi4-poller");
694 rmi_f54_create_input_map(f54);
696 /* register video device */
697 strlcpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
698 ret = v4l2_device_register(&fn->dev, &f54->v4l2);
700 dev_err(&fn->dev, "Unable to register video dev.\n");
704 /* initialize the queue */
705 mutex_init(&f54->lock);
706 f54->queue = rmi_f54_queue;
707 f54->queue.drv_priv = f54;
708 f54->queue.lock = &f54->lock;
709 f54->queue.dev = &fn->dev;
711 ret = vb2_queue_init(&f54->queue);
715 f54->vdev = rmi_f54_video_device;
716 f54->vdev.v4l2_dev = &f54->v4l2;
717 f54->vdev.lock = &f54->lock;
718 f54->vdev.vfl_dir = VFL_DIR_RX;
719 f54->vdev.queue = &f54->queue;
720 video_set_drvdata(&f54->vdev, f54);
722 ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
724 dev_err(&fn->dev, "Unable to register video subdevice.");
731 v4l2_device_unregister(&f54->v4l2);
733 cancel_delayed_work_sync(&f54->work);
734 flush_workqueue(f54->workqueue);
735 destroy_workqueue(f54->workqueue);
739 static void rmi_f54_remove(struct rmi_function *fn)
741 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
743 video_unregister_device(&f54->vdev);
744 v4l2_device_unregister(&f54->v4l2);
747 struct rmi_function_handler rmi_f54_handler = {
752 .probe = rmi_f54_probe,
753 .config = rmi_f54_config,
754 .remove = rmi_f54_remove,