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 struct rmi_device
*rmi_dev
= f54
->fn
->rmi_dev
;
221 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
222 u8 rx
= drv_data
->num_rx_electrodes
? : f54
->num_rx_electrodes
;
223 u8 tx
= drv_data
->num_tx_electrodes
? : f54
->num_tx_electrodes
;
226 switch (rmi_f54_get_reptype(f54
, f54
->input
)) {
230 case F54_16BIT_IMAGE
:
231 case F54_RAW_16BIT_IMAGE
:
232 case F54_TRUE_BASELINE
:
233 case F54_FULL_RAW_CAP
:
234 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
:
235 size
= sizeof(u16
) * rx
* tx
;
244 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype
, u32
*pixfmt
)
250 *pixfmt
= V4L2_TCH_FMT_DELTA_TD08
;
253 case F54_16BIT_IMAGE
:
254 *pixfmt
= V4L2_TCH_FMT_DELTA_TD16
;
257 case F54_RAW_16BIT_IMAGE
:
258 case F54_TRUE_BASELINE
:
259 case F54_FULL_RAW_CAP
:
260 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
:
261 *pixfmt
= V4L2_TCH_FMT_TU16
;
264 case F54_REPORT_NONE
:
265 case F54_MAX_REPORT_TYPE
:
273 static const struct v4l2_file_operations rmi_f54_video_fops
= {
274 .owner
= THIS_MODULE
,
275 .open
= v4l2_fh_open
,
276 .release
= vb2_fop_release
,
277 .unlocked_ioctl
= video_ioctl2
,
278 .read
= vb2_fop_read
,
279 .mmap
= vb2_fop_mmap
,
280 .poll
= vb2_fop_poll
,
283 static int rmi_f54_queue_setup(struct vb2_queue
*q
, unsigned int *nbuffers
,
284 unsigned int *nplanes
, unsigned int sizes
[],
285 struct device
*alloc_devs
[])
287 struct f54_data
*f54
= q
->drv_priv
;
290 return sizes
[0] < rmi_f54_get_report_size(f54
) ? -EINVAL
: 0;
293 sizes
[0] = rmi_f54_get_report_size(f54
);
298 static void rmi_f54_buffer_queue(struct vb2_buffer
*vb
)
300 struct f54_data
*f54
= vb2_get_drv_priv(vb
->vb2_queue
);
302 enum vb2_buffer_state state
;
303 enum rmi_f54_report_type reptype
;
306 mutex_lock(&f54
->status_mutex
);
308 reptype
= rmi_f54_get_reptype(f54
, f54
->input
);
309 if (reptype
== F54_REPORT_NONE
) {
310 state
= VB2_BUF_STATE_ERROR
;
315 state
= VB2_BUF_STATE_ERROR
;
319 ret
= rmi_f54_request_report(f54
->fn
, reptype
);
321 dev_err(&f54
->fn
->dev
, "Error requesting F54 report\n");
322 state
= VB2_BUF_STATE_ERROR
;
327 mutex_lock(&f54
->data_mutex
);
329 while (f54
->is_busy
) {
330 mutex_unlock(&f54
->data_mutex
);
331 if (!wait_for_completion_timeout(&f54
->cmd_done
,
332 msecs_to_jiffies(1000))) {
333 dev_err(&f54
->fn
->dev
, "Timed out\n");
334 state
= VB2_BUF_STATE_ERROR
;
337 mutex_lock(&f54
->data_mutex
);
340 ptr
= vb2_plane_vaddr(vb
, 0);
342 dev_err(&f54
->fn
->dev
, "Error acquiring frame ptr\n");
343 state
= VB2_BUF_STATE_ERROR
;
347 memcpy(ptr
, f54
->report_data
, f54
->report_size
);
348 vb2_set_plane_payload(vb
, 0, rmi_f54_get_report_size(f54
));
349 state
= VB2_BUF_STATE_DONE
;
352 mutex_unlock(&f54
->data_mutex
);
354 vb2_buffer_done(vb
, state
);
355 mutex_unlock(&f54
->status_mutex
);
358 /* V4L2 structures */
359 static const struct vb2_ops rmi_f54_queue_ops
= {
360 .queue_setup
= rmi_f54_queue_setup
,
361 .buf_queue
= rmi_f54_buffer_queue
,
362 .wait_prepare
= vb2_ops_wait_prepare
,
363 .wait_finish
= vb2_ops_wait_finish
,
366 static const struct vb2_queue rmi_f54_queue
= {
367 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
368 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
369 .buf_struct_size
= sizeof(struct vb2_buffer
),
370 .ops
= &rmi_f54_queue_ops
,
371 .mem_ops
= &vb2_vmalloc_memops
,
372 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
373 .min_buffers_needed
= 1,
376 static int rmi_f54_vidioc_querycap(struct file
*file
, void *priv
,
377 struct v4l2_capability
*cap
)
379 struct f54_data
*f54
= video_drvdata(file
);
381 strlcpy(cap
->driver
, F54_NAME
, sizeof(cap
->driver
));
382 strlcpy(cap
->card
, SYNAPTICS_INPUT_DEVICE_NAME
, sizeof(cap
->card
));
383 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
384 "rmi4:%s", dev_name(&f54
->fn
->dev
));
389 static int rmi_f54_vidioc_enum_input(struct file
*file
, void *priv
,
390 struct v4l2_input
*i
)
392 struct f54_data
*f54
= video_drvdata(file
);
393 enum rmi_f54_report_type reptype
;
395 reptype
= rmi_f54_get_reptype(f54
, i
->index
);
396 if (reptype
== F54_REPORT_NONE
)
399 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
401 strlcpy(i
->name
, rmi_f54_report_type_names
[reptype
], sizeof(i
->name
));
405 static int rmi_f54_set_input(struct f54_data
*f54
, unsigned int i
)
407 struct rmi_device
*rmi_dev
= f54
->fn
->rmi_dev
;
408 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
409 u8 rx
= drv_data
->num_rx_electrodes
? : f54
->num_rx_electrodes
;
410 u8 tx
= drv_data
->num_tx_electrodes
? : f54
->num_tx_electrodes
;
411 struct v4l2_pix_format
*f
= &f54
->format
;
412 enum rmi_f54_report_type reptype
;
415 reptype
= rmi_f54_get_reptype(f54
, i
);
416 if (reptype
== F54_REPORT_NONE
)
419 ret
= rmi_f54_get_pixel_fmt(reptype
, &f
->pixelformat
);
427 f
->field
= V4L2_FIELD_NONE
;
428 f
->colorspace
= V4L2_COLORSPACE_RAW
;
429 f
->bytesperline
= f
->width
* sizeof(u16
);
430 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
435 static int rmi_f54_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
437 return rmi_f54_set_input(video_drvdata(file
), i
);
440 static int rmi_f54_vidioc_g_input(struct file
*file
, void *priv
,
443 struct f54_data
*f54
= video_drvdata(file
);
450 static int rmi_f54_vidioc_fmt(struct file
*file
, void *priv
,
451 struct v4l2_format
*f
)
453 struct f54_data
*f54
= video_drvdata(file
);
455 f
->fmt
.pix
= f54
->format
;
460 static int rmi_f54_vidioc_enum_fmt(struct file
*file
, void *priv
,
461 struct v4l2_fmtdesc
*fmt
)
463 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
466 switch (fmt
->index
) {
468 fmt
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
472 fmt
->pixelformat
= V4L2_TCH_FMT_DELTA_TD08
;
476 fmt
->pixelformat
= V4L2_TCH_FMT_TU16
;
486 static int rmi_f54_vidioc_g_parm(struct file
*file
, void *fh
,
487 struct v4l2_streamparm
*a
)
489 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
492 a
->parm
.capture
.readbuffers
= 1;
493 a
->parm
.capture
.timeperframe
.numerator
= 1;
494 a
->parm
.capture
.timeperframe
.denominator
= 10;
498 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops
= {
499 .vidioc_querycap
= rmi_f54_vidioc_querycap
,
501 .vidioc_enum_fmt_vid_cap
= rmi_f54_vidioc_enum_fmt
,
502 .vidioc_s_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
503 .vidioc_g_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
504 .vidioc_try_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
505 .vidioc_g_parm
= rmi_f54_vidioc_g_parm
,
507 .vidioc_enum_input
= rmi_f54_vidioc_enum_input
,
508 .vidioc_g_input
= rmi_f54_vidioc_g_input
,
509 .vidioc_s_input
= rmi_f54_vidioc_s_input
,
511 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
512 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
513 .vidioc_querybuf
= vb2_ioctl_querybuf
,
514 .vidioc_qbuf
= vb2_ioctl_qbuf
,
515 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
516 .vidioc_expbuf
= vb2_ioctl_expbuf
,
518 .vidioc_streamon
= vb2_ioctl_streamon
,
519 .vidioc_streamoff
= vb2_ioctl_streamoff
,
522 static const struct video_device rmi_f54_video_device
= {
523 .name
= "Synaptics RMI4",
524 .fops
= &rmi_f54_video_fops
,
525 .ioctl_ops
= &rmi_f54_video_ioctl_ops
,
526 .release
= video_device_release_empty
,
527 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
528 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
531 static void rmi_f54_work(struct work_struct
*work
)
533 struct f54_data
*f54
= container_of(work
, struct f54_data
, work
.work
);
534 struct rmi_function
*fn
= f54
->fn
;
536 struct rmi_f54_reports
*report
;
542 data
= f54
->report_data
;
543 report_size
= rmi_f54_get_report_size(f54
);
544 if (report_size
== 0) {
545 dev_err(&fn
->dev
, "Bad report size, report type=%d\n",
548 goto error
; /* retry won't help */
550 f54
->standard_report
[0].size
= report_size
;
551 report
= f54
->standard_report
;
553 mutex_lock(&f54
->data_mutex
);
556 * Need to check if command has completed.
557 * If not try again later.
559 error
= rmi_read(fn
->rmi_dev
, f54
->fn
->fd
.command_base_addr
,
562 dev_err(&fn
->dev
, "Failed to read back command\n");
565 if (command
& F54_GET_REPORT
) {
566 if (time_after(jiffies
, f54
->timeout
)) {
567 dev_err(&fn
->dev
, "Get report command timed out\n");
574 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Get report command completed, reading data\n");
577 for (; report
->size
; report
++) {
578 fifo
[0] = report
->start
& 0xff;
579 fifo
[1] = (report
->start
>> 8) & 0xff;
580 error
= rmi_write_block(fn
->rmi_dev
,
581 fn
->fd
.data_base_addr
+ F54_FIFO_OFFSET
,
584 dev_err(&fn
->dev
, "Failed to set fifo start offset\n");
588 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.data_base_addr
+
589 F54_REPORT_DATA_OFFSET
, data
,
592 dev_err(&fn
->dev
, "%s: read [%d bytes] returned %d\n",
593 __func__
, report
->size
, error
);
596 data
+= report
->size
;
597 report_size
+= report
->size
;
601 f54
->report_size
= error
? 0 : report_size
;
606 if (report_size
== 0 && !error
) {
607 queue_delayed_work(f54
->workqueue
, &f54
->work
,
608 msecs_to_jiffies(1));
610 f54
->is_busy
= false;
611 complete(&f54
->cmd_done
);
614 mutex_unlock(&f54
->data_mutex
);
617 static int rmi_f54_attention(struct rmi_function
*fn
, unsigned long *irqbits
)
622 static int rmi_f54_config(struct rmi_function
*fn
)
624 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
626 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
631 static int rmi_f54_detect(struct rmi_function
*fn
)
634 struct f54_data
*f54
;
636 f54
= dev_get_drvdata(&fn
->dev
);
638 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
639 &f54
->qry
, sizeof(f54
->qry
));
641 dev_err(&fn
->dev
, "%s: Failed to query F54 properties\n",
646 f54
->num_rx_electrodes
= f54
->qry
[0];
647 f54
->num_tx_electrodes
= f54
->qry
[1];
648 f54
->capabilities
= f54
->qry
[2];
649 f54
->clock_rate
= f54
->qry
[3] | (f54
->qry
[4] << 8);
650 f54
->family
= f54
->qry
[5];
652 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 num_rx_electrodes: %d\n",
653 f54
->num_rx_electrodes
);
654 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 num_tx_electrodes: %d\n",
655 f54
->num_tx_electrodes
);
656 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 capabilities: 0x%x\n",
658 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 clock rate: 0x%x\n",
660 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 family: 0x%x\n",
663 f54
->is_busy
= false;
668 static int rmi_f54_probe(struct rmi_function
*fn
)
670 struct f54_data
*f54
;
674 f54
= devm_kzalloc(&fn
->dev
, sizeof(struct f54_data
), GFP_KERNEL
);
679 dev_set_drvdata(&fn
->dev
, f54
);
681 ret
= rmi_f54_detect(fn
);
685 mutex_init(&f54
->data_mutex
);
686 mutex_init(&f54
->status_mutex
);
688 rx
= f54
->num_rx_electrodes
;
689 tx
= f54
->num_tx_electrodes
;
690 f54
->report_data
= devm_kzalloc(&fn
->dev
,
691 sizeof(u16
) * tx
* rx
,
693 if (f54
->report_data
== NULL
)
696 INIT_DELAYED_WORK(&f54
->work
, rmi_f54_work
);
698 f54
->workqueue
= create_singlethread_workqueue("rmi4-poller");
702 rmi_f54_create_input_map(f54
);
704 /* register video device */
705 strlcpy(f54
->v4l2
.name
, F54_NAME
, sizeof(f54
->v4l2
.name
));
706 ret
= v4l2_device_register(&fn
->dev
, &f54
->v4l2
);
708 dev_err(&fn
->dev
, "Unable to register video dev.\n");
712 /* initialize the queue */
713 mutex_init(&f54
->lock
);
714 f54
->queue
= rmi_f54_queue
;
715 f54
->queue
.drv_priv
= f54
;
716 f54
->queue
.lock
= &f54
->lock
;
717 f54
->queue
.dev
= &fn
->dev
;
719 ret
= vb2_queue_init(&f54
->queue
);
723 f54
->vdev
= rmi_f54_video_device
;
724 f54
->vdev
.v4l2_dev
= &f54
->v4l2
;
725 f54
->vdev
.lock
= &f54
->lock
;
726 f54
->vdev
.vfl_dir
= VFL_DIR_RX
;
727 f54
->vdev
.queue
= &f54
->queue
;
728 video_set_drvdata(&f54
->vdev
, f54
);
730 ret
= video_register_device(&f54
->vdev
, VFL_TYPE_TOUCH
, -1);
732 dev_err(&fn
->dev
, "Unable to register video subdevice.");
739 v4l2_device_unregister(&f54
->v4l2
);
741 cancel_delayed_work_sync(&f54
->work
);
742 flush_workqueue(f54
->workqueue
);
743 destroy_workqueue(f54
->workqueue
);
747 static void rmi_f54_remove(struct rmi_function
*fn
)
749 struct f54_data
*f54
= dev_get_drvdata(&fn
->dev
);
751 video_unregister_device(&f54
->vdev
);
752 v4l2_device_unregister(&f54
->v4l2
);
755 struct rmi_function_handler rmi_f54_handler
= {
760 .probe
= rmi_f54_probe
,
761 .config
= rmi_f54_config
,
762 .attention
= rmi_f54_attention
,
763 .remove
= rmi_f54_remove
,