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 /* Fixed sizes of reports */
35 #define F54_QUERY_LEN 27
37 /* F54 capabilities */
38 #define F54_CAP_BASELINE (1 << 2)
39 #define F54_CAP_IMAGE8 (1 << 3)
40 #define F54_CAP_IMAGE16 (1 << 6)
43 * enum rmi_f54_report_type - RMI4 F54 report types
45 * @F54_8BIT_IMAGE: Normalized 8-Bit Image Report. The capacitance variance
46 * from baseline for each pixel.
48 * @F54_16BIT_IMAGE: Normalized 16-Bit Image Report. The capacitance variance
49 * from baseline for each pixel.
51 * @F54_RAW_16BIT_IMAGE:
52 * Raw 16-Bit Image Report. The raw capacitance for each
55 * @F54_TRUE_BASELINE: True Baseline Report. The baseline capacitance for each
58 * @F54_FULL_RAW_CAP: Full Raw Capacitance Report. The raw capacitance with
59 * low reference set to its minimum value and high
60 * reference set to its maximum value.
62 * @F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
63 * Full Raw Capacitance with Receiver Offset Removed
64 * Report. Set Low reference to its minimum value and high
65 * references to its maximum value, then report the raw
66 * capacitance for each pixel.
68 enum rmi_f54_report_type {
72 F54_RAW_16BIT_IMAGE = 3,
73 F54_TRUE_BASELINE = 9,
74 F54_FULL_RAW_CAP = 19,
75 F54_FULL_RAW_CAP_RX_OFFSET_REMOVED = 20,
79 const char *rmi_f54_report_type_names[] = {
80 [F54_REPORT_NONE] = "Unknown",
81 [F54_8BIT_IMAGE] = "Normalized 8-Bit Image",
82 [F54_16BIT_IMAGE] = "Normalized 16-Bit Image",
83 [F54_RAW_16BIT_IMAGE] = "Raw 16-Bit Image",
84 [F54_TRUE_BASELINE] = "True Baseline",
85 [F54_FULL_RAW_CAP] = "Full Raw Capacitance",
86 [F54_FULL_RAW_CAP_RX_OFFSET_REMOVED]
87 = "Full Raw Capacitance RX Offset Removed",
90 struct rmi_f54_reports {
96 struct rmi_function *fn;
98 u8 qry[F54_QUERY_LEN];
100 u8 num_tx_electrodes;
105 enum rmi_f54_report_type report_type;
108 struct rmi_f54_reports standard_report[2];
111 struct mutex status_mutex;
112 struct mutex data_mutex;
114 struct workqueue_struct *workqueue;
115 struct delayed_work work;
116 unsigned long timeout;
118 struct completion cmd_done;
121 struct v4l2_device v4l2;
122 struct v4l2_pix_format format;
123 struct video_device vdev;
124 struct vb2_queue queue;
127 enum rmi_f54_report_type inputs[F54_MAX_REPORT_TYPE];
131 * Basic checks on report_type to ensure we write a valid type
134 static bool is_f54_report_type_valid(struct f54_data *f54,
135 enum rmi_f54_report_type reptype)
139 return f54->capabilities & F54_CAP_IMAGE8;
140 case F54_16BIT_IMAGE:
141 case F54_RAW_16BIT_IMAGE:
142 return f54->capabilities & F54_CAP_IMAGE16;
143 case F54_TRUE_BASELINE:
144 return f54->capabilities & F54_CAP_IMAGE16;
145 case F54_FULL_RAW_CAP:
146 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
153 static enum rmi_f54_report_type rmi_f54_get_reptype(struct f54_data *f54,
156 if (i >= F54_MAX_REPORT_TYPE)
157 return F54_REPORT_NONE;
159 return f54->inputs[i];
162 static void rmi_f54_create_input_map(struct f54_data *f54)
165 enum rmi_f54_report_type reptype;
167 for (reptype = 1; reptype < F54_MAX_REPORT_TYPE; reptype++) {
168 if (!is_f54_report_type_valid(f54, reptype))
171 f54->inputs[i++] = reptype;
174 /* Remaining values are zero via kzalloc */
177 static int rmi_f54_request_report(struct rmi_function *fn, u8 report_type)
179 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
180 struct rmi_device *rmi_dev = fn->rmi_dev;
183 /* Write Report Type into F54_AD_Data0 */
184 if (f54->report_type != report_type) {
185 error = rmi_write(rmi_dev, f54->fn->fd.data_base_addr,
189 f54->report_type = report_type;
193 * Small delay after disabling interrupts to avoid race condition
194 * in firmare. This value is a bit higher than absolutely necessary.
195 * Should be removed once issue is resolved in firmware.
197 usleep_range(2000, 3000);
199 mutex_lock(&f54->data_mutex);
201 error = rmi_write(rmi_dev, fn->fd.command_base_addr, F54_GET_REPORT);
205 init_completion(&f54->cmd_done);
208 f54->timeout = jiffies + msecs_to_jiffies(100);
210 queue_delayed_work(f54->workqueue, &f54->work, 0);
213 mutex_unlock(&f54->data_mutex);
218 static size_t rmi_f54_get_report_size(struct f54_data *f54)
220 u8 rx = f54->num_rx_electrodes ? : f54->num_rx_electrodes;
221 u8 tx = f54->num_tx_electrodes ? : f54->num_tx_electrodes;
224 switch (rmi_f54_get_reptype(f54, f54->input)) {
228 case F54_16BIT_IMAGE:
229 case F54_RAW_16BIT_IMAGE:
230 case F54_TRUE_BASELINE:
231 case F54_FULL_RAW_CAP:
232 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
233 size = sizeof(u16) * rx * tx;
242 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype, u32 *pixfmt)
248 *pixfmt = V4L2_TCH_FMT_DELTA_TD08;
251 case F54_16BIT_IMAGE:
252 *pixfmt = V4L2_TCH_FMT_DELTA_TD16;
255 case F54_RAW_16BIT_IMAGE:
256 case F54_TRUE_BASELINE:
257 case F54_FULL_RAW_CAP:
258 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
259 *pixfmt = V4L2_TCH_FMT_TU16;
262 case F54_REPORT_NONE:
263 case F54_MAX_REPORT_TYPE:
271 static const struct v4l2_file_operations rmi_f54_video_fops = {
272 .owner = THIS_MODULE,
273 .open = v4l2_fh_open,
274 .release = vb2_fop_release,
275 .unlocked_ioctl = video_ioctl2,
276 .read = vb2_fop_read,
277 .mmap = vb2_fop_mmap,
278 .poll = vb2_fop_poll,
281 static int rmi_f54_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
282 unsigned int *nplanes, unsigned int sizes[],
283 struct device *alloc_devs[])
285 struct f54_data *f54 = q->drv_priv;
288 return sizes[0] < rmi_f54_get_report_size(f54) ? -EINVAL : 0;
291 sizes[0] = rmi_f54_get_report_size(f54);
296 static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
298 struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
300 enum vb2_buffer_state state;
301 enum rmi_f54_report_type reptype;
304 mutex_lock(&f54->status_mutex);
306 reptype = rmi_f54_get_reptype(f54, f54->input);
307 if (reptype == F54_REPORT_NONE) {
308 state = VB2_BUF_STATE_ERROR;
313 state = VB2_BUF_STATE_ERROR;
317 ret = rmi_f54_request_report(f54->fn, reptype);
319 dev_err(&f54->fn->dev, "Error requesting F54 report\n");
320 state = VB2_BUF_STATE_ERROR;
325 mutex_lock(&f54->data_mutex);
327 while (f54->is_busy) {
328 mutex_unlock(&f54->data_mutex);
329 if (!wait_for_completion_timeout(&f54->cmd_done,
330 msecs_to_jiffies(1000))) {
331 dev_err(&f54->fn->dev, "Timed out\n");
332 state = VB2_BUF_STATE_ERROR;
335 mutex_lock(&f54->data_mutex);
338 ptr = vb2_plane_vaddr(vb, 0);
340 dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
341 state = VB2_BUF_STATE_ERROR;
345 memcpy(ptr, f54->report_data, f54->report_size);
346 vb2_set_plane_payload(vb, 0, rmi_f54_get_report_size(f54));
347 state = VB2_BUF_STATE_DONE;
350 mutex_unlock(&f54->data_mutex);
352 vb2_buffer_done(vb, state);
353 mutex_unlock(&f54->status_mutex);
356 /* V4L2 structures */
357 static const struct vb2_ops rmi_f54_queue_ops = {
358 .queue_setup = rmi_f54_queue_setup,
359 .buf_queue = rmi_f54_buffer_queue,
360 .wait_prepare = vb2_ops_wait_prepare,
361 .wait_finish = vb2_ops_wait_finish,
364 static const struct vb2_queue rmi_f54_queue = {
365 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
366 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
367 .buf_struct_size = sizeof(struct vb2_v4l2_buffer),
368 .ops = &rmi_f54_queue_ops,
369 .mem_ops = &vb2_vmalloc_memops,
370 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
371 .min_buffers_needed = 1,
374 static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
375 struct v4l2_capability *cap)
377 struct f54_data *f54 = video_drvdata(file);
379 strlcpy(cap->driver, F54_NAME, sizeof(cap->driver));
380 strlcpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
381 snprintf(cap->bus_info, sizeof(cap->bus_info),
382 "rmi4:%s", dev_name(&f54->fn->dev));
387 static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
388 struct v4l2_input *i)
390 struct f54_data *f54 = video_drvdata(file);
391 enum rmi_f54_report_type reptype;
393 reptype = rmi_f54_get_reptype(f54, i->index);
394 if (reptype == F54_REPORT_NONE)
397 i->type = V4L2_INPUT_TYPE_TOUCH;
399 strlcpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
403 static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
405 struct v4l2_pix_format *f = &f54->format;
406 enum rmi_f54_report_type reptype;
409 reptype = rmi_f54_get_reptype(f54, i);
410 if (reptype == F54_REPORT_NONE)
413 ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
419 f->width = f54->num_rx_electrodes;
420 f->height = f54->num_tx_electrodes;
421 f->field = V4L2_FIELD_NONE;
422 f->colorspace = V4L2_COLORSPACE_RAW;
423 f->bytesperline = f->width * sizeof(u16);
424 f->sizeimage = f->width * f->height * sizeof(u16);
429 static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
431 return rmi_f54_set_input(video_drvdata(file), i);
434 static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
437 struct f54_data *f54 = video_drvdata(file);
444 static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
445 struct v4l2_format *f)
447 struct f54_data *f54 = video_drvdata(file);
449 f->fmt.pix = f54->format;
454 static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
455 struct v4l2_fmtdesc *fmt)
457 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
460 switch (fmt->index) {
462 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
466 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD08;
470 fmt->pixelformat = V4L2_TCH_FMT_TU16;
480 static int rmi_f54_vidioc_g_parm(struct file *file, void *fh,
481 struct v4l2_streamparm *a)
483 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
486 a->parm.capture.readbuffers = 1;
487 a->parm.capture.timeperframe.numerator = 1;
488 a->parm.capture.timeperframe.denominator = 10;
492 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops = {
493 .vidioc_querycap = rmi_f54_vidioc_querycap,
495 .vidioc_enum_fmt_vid_cap = rmi_f54_vidioc_enum_fmt,
496 .vidioc_s_fmt_vid_cap = rmi_f54_vidioc_fmt,
497 .vidioc_g_fmt_vid_cap = rmi_f54_vidioc_fmt,
498 .vidioc_try_fmt_vid_cap = rmi_f54_vidioc_fmt,
499 .vidioc_g_parm = rmi_f54_vidioc_g_parm,
501 .vidioc_enum_input = rmi_f54_vidioc_enum_input,
502 .vidioc_g_input = rmi_f54_vidioc_g_input,
503 .vidioc_s_input = rmi_f54_vidioc_s_input,
505 .vidioc_reqbufs = vb2_ioctl_reqbufs,
506 .vidioc_create_bufs = vb2_ioctl_create_bufs,
507 .vidioc_querybuf = vb2_ioctl_querybuf,
508 .vidioc_qbuf = vb2_ioctl_qbuf,
509 .vidioc_dqbuf = vb2_ioctl_dqbuf,
510 .vidioc_expbuf = vb2_ioctl_expbuf,
512 .vidioc_streamon = vb2_ioctl_streamon,
513 .vidioc_streamoff = vb2_ioctl_streamoff,
516 static const struct video_device rmi_f54_video_device = {
517 .name = "Synaptics RMI4",
518 .fops = &rmi_f54_video_fops,
519 .ioctl_ops = &rmi_f54_video_ioctl_ops,
520 .release = video_device_release_empty,
521 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
522 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
525 static void rmi_f54_work(struct work_struct *work)
527 struct f54_data *f54 = container_of(work, struct f54_data, work.work);
528 struct rmi_function *fn = f54->fn;
530 struct rmi_f54_reports *report;
536 data = f54->report_data;
537 report_size = rmi_f54_get_report_size(f54);
538 if (report_size == 0) {
539 dev_err(&fn->dev, "Bad report size, report type=%d\n",
542 goto error; /* retry won't help */
544 f54->standard_report[0].size = report_size;
545 report = f54->standard_report;
547 mutex_lock(&f54->data_mutex);
550 * Need to check if command has completed.
551 * If not try again later.
553 error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
556 dev_err(&fn->dev, "Failed to read back command\n");
559 if (command & F54_GET_REPORT) {
560 if (time_after(jiffies, f54->timeout)) {
561 dev_err(&fn->dev, "Get report command timed out\n");
568 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
571 for (; report->size; report++) {
572 fifo[0] = report->start & 0xff;
573 fifo[1] = (report->start >> 8) & 0xff;
574 error = rmi_write_block(fn->rmi_dev,
575 fn->fd.data_base_addr + F54_FIFO_OFFSET,
578 dev_err(&fn->dev, "Failed to set fifo start offset\n");
582 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
583 F54_REPORT_DATA_OFFSET, data,
586 dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
587 __func__, report->size, error);
590 data += report->size;
591 report_size += report->size;
595 f54->report_size = error ? 0 : report_size;
600 if (report_size == 0 && !error) {
601 queue_delayed_work(f54->workqueue, &f54->work,
602 msecs_to_jiffies(1));
604 f54->is_busy = false;
605 complete(&f54->cmd_done);
608 mutex_unlock(&f54->data_mutex);
611 static int rmi_f54_attention(struct rmi_function *fn, unsigned long *irqbits)
616 static int rmi_f54_config(struct rmi_function *fn)
618 struct rmi_driver *drv = fn->rmi_dev->driver;
620 drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
625 static int rmi_f54_detect(struct rmi_function *fn)
628 struct f54_data *f54;
630 f54 = dev_get_drvdata(&fn->dev);
632 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
633 &f54->qry, sizeof(f54->qry));
635 dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
640 f54->num_rx_electrodes = f54->qry[0];
641 f54->num_tx_electrodes = f54->qry[1];
642 f54->capabilities = f54->qry[2];
643 f54->clock_rate = f54->qry[3] | (f54->qry[4] << 8);
644 f54->family = f54->qry[5];
646 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
647 f54->num_rx_electrodes);
648 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
649 f54->num_tx_electrodes);
650 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
652 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
654 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
657 f54->is_busy = false;
662 static int rmi_f54_probe(struct rmi_function *fn)
664 struct f54_data *f54;
668 f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
673 dev_set_drvdata(&fn->dev, f54);
675 ret = rmi_f54_detect(fn);
679 mutex_init(&f54->data_mutex);
680 mutex_init(&f54->status_mutex);
682 rx = f54->num_rx_electrodes;
683 tx = f54->num_tx_electrodes;
684 f54->report_data = devm_kzalloc(&fn->dev,
685 sizeof(u16) * tx * rx,
687 if (f54->report_data == NULL)
690 INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
692 f54->workqueue = create_singlethread_workqueue("rmi4-poller");
696 rmi_f54_create_input_map(f54);
698 /* register video device */
699 strlcpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
700 ret = v4l2_device_register(&fn->dev, &f54->v4l2);
702 dev_err(&fn->dev, "Unable to register video dev.\n");
706 /* initialize the queue */
707 mutex_init(&f54->lock);
708 f54->queue = rmi_f54_queue;
709 f54->queue.drv_priv = f54;
710 f54->queue.lock = &f54->lock;
711 f54->queue.dev = &fn->dev;
713 ret = vb2_queue_init(&f54->queue);
717 f54->vdev = rmi_f54_video_device;
718 f54->vdev.v4l2_dev = &f54->v4l2;
719 f54->vdev.lock = &f54->lock;
720 f54->vdev.vfl_dir = VFL_DIR_RX;
721 f54->vdev.queue = &f54->queue;
722 video_set_drvdata(&f54->vdev, f54);
724 ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
726 dev_err(&fn->dev, "Unable to register video subdevice.");
733 v4l2_device_unregister(&f54->v4l2);
735 cancel_delayed_work_sync(&f54->work);
736 flush_workqueue(f54->workqueue);
737 destroy_workqueue(f54->workqueue);
741 static void rmi_f54_remove(struct rmi_function *fn)
743 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
745 video_unregister_device(&f54->vdev);
746 v4l2_device_unregister(&f54->v4l2);
747 destroy_workqueue(f54->workqueue);
750 struct rmi_function_handler rmi_f54_handler = {
755 .probe = rmi_f54_probe,
756 .config = rmi_f54_config,
757 .attention = rmi_f54_attention,
758 .remove = rmi_f54_remove,