1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2015 Synaptics Incorporated
4 * Copyright (C) 2016 Zodiac Inflight Innovations
7 #include <linux/kernel.h>
9 #include <linux/input.h>
10 #include <linux/slab.h>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ioctl.h>
15 #include <media/videobuf2-v4l2.h>
16 #include <media/videobuf2-vmalloc.h>
17 #include "rmi_driver.h"
19 #define F54_NAME "rmi4_f54"
21 /* F54 data offsets */
22 #define F54_REPORT_DATA_OFFSET 3
23 #define F54_FIFO_OFFSET 1
24 #define F54_NUM_TX_OFFSET 1
25 #define F54_NUM_RX_OFFSET 0
28 #define F54_GET_REPORT 1
29 #define F54_FORCE_CAL 2
31 /* F54 capabilities */
32 #define F54_CAP_BASELINE (1 << 2)
33 #define F54_CAP_IMAGE8 (1 << 3)
34 #define F54_CAP_IMAGE16 (1 << 6)
37 * enum rmi_f54_report_type - RMI4 F54 report types
39 * @F54_8BIT_IMAGE: Normalized 8-Bit Image Report. The capacitance variance
40 * from baseline for each pixel.
42 * @F54_16BIT_IMAGE: Normalized 16-Bit Image Report. The capacitance variance
43 * from baseline for each pixel.
45 * @F54_RAW_16BIT_IMAGE:
46 * Raw 16-Bit Image Report. The raw capacitance for each
49 * @F54_TRUE_BASELINE: True Baseline Report. The baseline capacitance for each
52 * @F54_FULL_RAW_CAP: Full Raw Capacitance Report. The raw capacitance with
53 * low reference set to its minimum value and high
54 * reference set to its maximum value.
56 * @F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
57 * Full Raw Capacitance with Receiver Offset Removed
58 * Report. Set Low reference to its minimum value and high
59 * references to its maximum value, then report the raw
60 * capacitance for each pixel.
62 enum rmi_f54_report_type
{
66 F54_RAW_16BIT_IMAGE
= 3,
67 F54_TRUE_BASELINE
= 9,
68 F54_FULL_RAW_CAP
= 19,
69 F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
= 20,
73 static const char * const rmi_f54_report_type_names
[] = {
74 [F54_REPORT_NONE
] = "Unknown",
75 [F54_8BIT_IMAGE
] = "Normalized 8-Bit Image",
76 [F54_16BIT_IMAGE
] = "Normalized 16-Bit Image",
77 [F54_RAW_16BIT_IMAGE
] = "Raw 16-Bit Image",
78 [F54_TRUE_BASELINE
] = "True Baseline",
79 [F54_FULL_RAW_CAP
] = "Full Raw Capacitance",
80 [F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
]
81 = "Full Raw Capacitance RX Offset Removed",
84 struct rmi_f54_reports
{
90 struct rmi_function
*fn
;
98 enum rmi_f54_report_type report_type
;
101 struct rmi_f54_reports standard_report
[2];
104 struct mutex status_mutex
;
105 struct mutex data_mutex
;
107 struct workqueue_struct
*workqueue
;
108 struct delayed_work work
;
109 unsigned long timeout
;
111 struct completion cmd_done
;
114 struct v4l2_device v4l2
;
115 struct v4l2_pix_format format
;
116 struct video_device vdev
;
117 struct vb2_queue queue
;
120 enum rmi_f54_report_type inputs
[F54_MAX_REPORT_TYPE
];
124 * Basic checks on report_type to ensure we write a valid type
127 static bool is_f54_report_type_valid(struct f54_data
*f54
,
128 enum rmi_f54_report_type reptype
)
132 return f54
->capabilities
& F54_CAP_IMAGE8
;
133 case F54_16BIT_IMAGE
:
134 case F54_RAW_16BIT_IMAGE
:
135 return f54
->capabilities
& F54_CAP_IMAGE16
;
136 case F54_TRUE_BASELINE
:
137 return f54
->capabilities
& F54_CAP_IMAGE16
;
138 case F54_FULL_RAW_CAP
:
139 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
:
146 static enum rmi_f54_report_type
rmi_f54_get_reptype(struct f54_data
*f54
,
149 if (i
>= F54_MAX_REPORT_TYPE
)
150 return F54_REPORT_NONE
;
152 return f54
->inputs
[i
];
155 static void rmi_f54_create_input_map(struct f54_data
*f54
)
158 enum rmi_f54_report_type reptype
;
160 for (reptype
= 1; reptype
< F54_MAX_REPORT_TYPE
; reptype
++) {
161 if (!is_f54_report_type_valid(f54
, reptype
))
164 f54
->inputs
[i
++] = reptype
;
167 /* Remaining values are zero via kzalloc */
170 static int rmi_f54_request_report(struct rmi_function
*fn
, u8 report_type
)
172 struct f54_data
*f54
= dev_get_drvdata(&fn
->dev
);
173 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
176 /* Write Report Type into F54_AD_Data0 */
177 if (f54
->report_type
!= report_type
) {
178 error
= rmi_write(rmi_dev
, f54
->fn
->fd
.data_base_addr
,
182 f54
->report_type
= report_type
;
186 * Small delay after disabling interrupts to avoid race condition
187 * in firmare. This value is a bit higher than absolutely necessary.
188 * Should be removed once issue is resolved in firmware.
190 usleep_range(2000, 3000);
192 mutex_lock(&f54
->data_mutex
);
194 error
= rmi_write(rmi_dev
, fn
->fd
.command_base_addr
, F54_GET_REPORT
);
198 init_completion(&f54
->cmd_done
);
201 f54
->timeout
= jiffies
+ msecs_to_jiffies(100);
203 queue_delayed_work(f54
->workqueue
, &f54
->work
, 0);
206 mutex_unlock(&f54
->data_mutex
);
211 static size_t rmi_f54_get_report_size(struct f54_data
*f54
)
213 struct rmi_device
*rmi_dev
= f54
->fn
->rmi_dev
;
214 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
215 u8 rx
= drv_data
->num_rx_electrodes
? : f54
->num_rx_electrodes
;
216 u8 tx
= drv_data
->num_tx_electrodes
? : f54
->num_tx_electrodes
;
219 switch (rmi_f54_get_reptype(f54
, f54
->input
)) {
223 case F54_16BIT_IMAGE
:
224 case F54_RAW_16BIT_IMAGE
:
225 case F54_TRUE_BASELINE
:
226 case F54_FULL_RAW_CAP
:
227 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
:
228 size
= sizeof(u16
) * rx
* tx
;
237 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype
, u32
*pixfmt
)
243 *pixfmt
= V4L2_TCH_FMT_DELTA_TD08
;
246 case F54_16BIT_IMAGE
:
247 *pixfmt
= V4L2_TCH_FMT_DELTA_TD16
;
250 case F54_RAW_16BIT_IMAGE
:
251 case F54_TRUE_BASELINE
:
252 case F54_FULL_RAW_CAP
:
253 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
:
254 *pixfmt
= V4L2_TCH_FMT_TU16
;
257 case F54_REPORT_NONE
:
258 case F54_MAX_REPORT_TYPE
:
266 static const struct v4l2_file_operations rmi_f54_video_fops
= {
267 .owner
= THIS_MODULE
,
268 .open
= v4l2_fh_open
,
269 .release
= vb2_fop_release
,
270 .unlocked_ioctl
= video_ioctl2
,
271 .read
= vb2_fop_read
,
272 .mmap
= vb2_fop_mmap
,
273 .poll
= vb2_fop_poll
,
276 static int rmi_f54_queue_setup(struct vb2_queue
*q
, unsigned int *nbuffers
,
277 unsigned int *nplanes
, unsigned int sizes
[],
278 struct device
*alloc_devs
[])
280 struct f54_data
*f54
= q
->drv_priv
;
283 return sizes
[0] < rmi_f54_get_report_size(f54
) ? -EINVAL
: 0;
286 sizes
[0] = rmi_f54_get_report_size(f54
);
291 static void rmi_f54_buffer_queue(struct vb2_buffer
*vb
)
293 struct f54_data
*f54
= vb2_get_drv_priv(vb
->vb2_queue
);
295 enum vb2_buffer_state state
;
296 enum rmi_f54_report_type reptype
;
299 mutex_lock(&f54
->status_mutex
);
301 reptype
= rmi_f54_get_reptype(f54
, f54
->input
);
302 if (reptype
== F54_REPORT_NONE
) {
303 state
= VB2_BUF_STATE_ERROR
;
308 state
= VB2_BUF_STATE_ERROR
;
312 ret
= rmi_f54_request_report(f54
->fn
, reptype
);
314 dev_err(&f54
->fn
->dev
, "Error requesting F54 report\n");
315 state
= VB2_BUF_STATE_ERROR
;
320 mutex_lock(&f54
->data_mutex
);
322 while (f54
->is_busy
) {
323 mutex_unlock(&f54
->data_mutex
);
324 if (!wait_for_completion_timeout(&f54
->cmd_done
,
325 msecs_to_jiffies(1000))) {
326 dev_err(&f54
->fn
->dev
, "Timed out\n");
327 state
= VB2_BUF_STATE_ERROR
;
330 mutex_lock(&f54
->data_mutex
);
333 ptr
= vb2_plane_vaddr(vb
, 0);
335 dev_err(&f54
->fn
->dev
, "Error acquiring frame ptr\n");
336 state
= VB2_BUF_STATE_ERROR
;
340 memcpy(ptr
, f54
->report_data
, f54
->report_size
);
341 vb2_set_plane_payload(vb
, 0, rmi_f54_get_report_size(f54
));
342 state
= VB2_BUF_STATE_DONE
;
345 mutex_unlock(&f54
->data_mutex
);
347 vb2_buffer_done(vb
, state
);
348 mutex_unlock(&f54
->status_mutex
);
351 /* V4L2 structures */
352 static const struct vb2_ops rmi_f54_queue_ops
= {
353 .queue_setup
= rmi_f54_queue_setup
,
354 .buf_queue
= rmi_f54_buffer_queue
,
355 .wait_prepare
= vb2_ops_wait_prepare
,
356 .wait_finish
= vb2_ops_wait_finish
,
359 static const struct vb2_queue rmi_f54_queue
= {
360 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
361 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
362 .buf_struct_size
= sizeof(struct vb2_buffer
),
363 .ops
= &rmi_f54_queue_ops
,
364 .mem_ops
= &vb2_vmalloc_memops
,
365 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
366 .min_buffers_needed
= 1,
369 static int rmi_f54_vidioc_querycap(struct file
*file
, void *priv
,
370 struct v4l2_capability
*cap
)
372 struct f54_data
*f54
= video_drvdata(file
);
374 strlcpy(cap
->driver
, F54_NAME
, sizeof(cap
->driver
));
375 strlcpy(cap
->card
, SYNAPTICS_INPUT_DEVICE_NAME
, sizeof(cap
->card
));
376 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
377 "rmi4:%s", dev_name(&f54
->fn
->dev
));
382 static int rmi_f54_vidioc_enum_input(struct file
*file
, void *priv
,
383 struct v4l2_input
*i
)
385 struct f54_data
*f54
= video_drvdata(file
);
386 enum rmi_f54_report_type reptype
;
388 reptype
= rmi_f54_get_reptype(f54
, i
->index
);
389 if (reptype
== F54_REPORT_NONE
)
392 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
394 strlcpy(i
->name
, rmi_f54_report_type_names
[reptype
], sizeof(i
->name
));
398 static int rmi_f54_set_input(struct f54_data
*f54
, unsigned int i
)
400 struct rmi_device
*rmi_dev
= f54
->fn
->rmi_dev
;
401 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
402 u8 rx
= drv_data
->num_rx_electrodes
? : f54
->num_rx_electrodes
;
403 u8 tx
= drv_data
->num_tx_electrodes
? : f54
->num_tx_electrodes
;
404 struct v4l2_pix_format
*f
= &f54
->format
;
405 enum rmi_f54_report_type reptype
;
408 reptype
= rmi_f54_get_reptype(f54
, i
);
409 if (reptype
== F54_REPORT_NONE
)
412 ret
= rmi_f54_get_pixel_fmt(reptype
, &f
->pixelformat
);
420 f
->field
= V4L2_FIELD_NONE
;
421 f
->colorspace
= V4L2_COLORSPACE_RAW
;
422 f
->bytesperline
= f
->width
* sizeof(u16
);
423 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
428 static int rmi_f54_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
430 return rmi_f54_set_input(video_drvdata(file
), i
);
433 static int rmi_f54_vidioc_g_input(struct file
*file
, void *priv
,
436 struct f54_data
*f54
= video_drvdata(file
);
443 static int rmi_f54_vidioc_fmt(struct file
*file
, void *priv
,
444 struct v4l2_format
*f
)
446 struct f54_data
*f54
= video_drvdata(file
);
448 f
->fmt
.pix
= f54
->format
;
453 static int rmi_f54_vidioc_enum_fmt(struct file
*file
, void *priv
,
454 struct v4l2_fmtdesc
*fmt
)
456 struct f54_data
*f54
= video_drvdata(file
);
458 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
464 fmt
->pixelformat
= f54
->format
.pixelformat
;
469 static int rmi_f54_vidioc_g_parm(struct file
*file
, void *fh
,
470 struct v4l2_streamparm
*a
)
472 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
475 a
->parm
.capture
.readbuffers
= 1;
476 a
->parm
.capture
.timeperframe
.numerator
= 1;
477 a
->parm
.capture
.timeperframe
.denominator
= 10;
481 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops
= {
482 .vidioc_querycap
= rmi_f54_vidioc_querycap
,
484 .vidioc_enum_fmt_vid_cap
= rmi_f54_vidioc_enum_fmt
,
485 .vidioc_s_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
486 .vidioc_g_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
487 .vidioc_try_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
488 .vidioc_g_parm
= rmi_f54_vidioc_g_parm
,
490 .vidioc_enum_input
= rmi_f54_vidioc_enum_input
,
491 .vidioc_g_input
= rmi_f54_vidioc_g_input
,
492 .vidioc_s_input
= rmi_f54_vidioc_s_input
,
494 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
495 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
496 .vidioc_querybuf
= vb2_ioctl_querybuf
,
497 .vidioc_qbuf
= vb2_ioctl_qbuf
,
498 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
499 .vidioc_expbuf
= vb2_ioctl_expbuf
,
501 .vidioc_streamon
= vb2_ioctl_streamon
,
502 .vidioc_streamoff
= vb2_ioctl_streamoff
,
505 static const struct video_device rmi_f54_video_device
= {
506 .name
= "Synaptics RMI4",
507 .fops
= &rmi_f54_video_fops
,
508 .ioctl_ops
= &rmi_f54_video_ioctl_ops
,
509 .release
= video_device_release_empty
,
510 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
511 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
514 static void rmi_f54_work(struct work_struct
*work
)
516 struct f54_data
*f54
= container_of(work
, struct f54_data
, work
.work
);
517 struct rmi_function
*fn
= f54
->fn
;
519 struct rmi_f54_reports
*report
;
525 data
= f54
->report_data
;
526 report_size
= rmi_f54_get_report_size(f54
);
527 if (report_size
== 0) {
528 dev_err(&fn
->dev
, "Bad report size, report type=%d\n",
531 goto error
; /* retry won't help */
533 f54
->standard_report
[0].size
= report_size
;
534 report
= f54
->standard_report
;
536 mutex_lock(&f54
->data_mutex
);
539 * Need to check if command has completed.
540 * If not try again later.
542 error
= rmi_read(fn
->rmi_dev
, f54
->fn
->fd
.command_base_addr
,
545 dev_err(&fn
->dev
, "Failed to read back command\n");
548 if (command
& F54_GET_REPORT
) {
549 if (time_after(jiffies
, f54
->timeout
)) {
550 dev_err(&fn
->dev
, "Get report command timed out\n");
557 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Get report command completed, reading data\n");
560 for (; report
->size
; report
++) {
561 fifo
[0] = report
->start
& 0xff;
562 fifo
[1] = (report
->start
>> 8) & 0xff;
563 error
= rmi_write_block(fn
->rmi_dev
,
564 fn
->fd
.data_base_addr
+ F54_FIFO_OFFSET
,
567 dev_err(&fn
->dev
, "Failed to set fifo start offset\n");
571 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.data_base_addr
+
572 F54_REPORT_DATA_OFFSET
, data
,
575 dev_err(&fn
->dev
, "%s: read [%d bytes] returned %d\n",
576 __func__
, report
->size
, error
);
579 data
+= report
->size
;
580 report_size
+= report
->size
;
584 f54
->report_size
= error
? 0 : report_size
;
589 if (report_size
== 0 && !error
) {
590 queue_delayed_work(f54
->workqueue
, &f54
->work
,
591 msecs_to_jiffies(1));
593 f54
->is_busy
= false;
594 complete(&f54
->cmd_done
);
597 mutex_unlock(&f54
->data_mutex
);
600 static int rmi_f54_config(struct rmi_function
*fn
)
602 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
604 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
609 static int rmi_f54_detect(struct rmi_function
*fn
)
612 struct f54_data
*f54
;
615 f54
= dev_get_drvdata(&fn
->dev
);
617 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
620 dev_err(&fn
->dev
, "%s: Failed to query F54 properties\n",
625 f54
->num_rx_electrodes
= buf
[0];
626 f54
->num_tx_electrodes
= buf
[1];
627 f54
->capabilities
= buf
[2];
628 f54
->clock_rate
= buf
[3] | (buf
[4] << 8);
629 f54
->family
= buf
[5];
631 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 num_rx_electrodes: %d\n",
632 f54
->num_rx_electrodes
);
633 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 num_tx_electrodes: %d\n",
634 f54
->num_tx_electrodes
);
635 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 capabilities: 0x%x\n",
637 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 clock rate: 0x%x\n",
639 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 family: 0x%x\n",
642 f54
->is_busy
= false;
647 static int rmi_f54_probe(struct rmi_function
*fn
)
649 struct f54_data
*f54
;
653 f54
= devm_kzalloc(&fn
->dev
, sizeof(struct f54_data
), GFP_KERNEL
);
658 dev_set_drvdata(&fn
->dev
, f54
);
660 ret
= rmi_f54_detect(fn
);
664 mutex_init(&f54
->data_mutex
);
665 mutex_init(&f54
->status_mutex
);
667 rx
= f54
->num_rx_electrodes
;
668 tx
= f54
->num_tx_electrodes
;
669 f54
->report_data
= devm_kzalloc(&fn
->dev
,
670 array3_size(tx
, rx
, sizeof(u16
)),
672 if (f54
->report_data
== NULL
)
675 INIT_DELAYED_WORK(&f54
->work
, rmi_f54_work
);
677 f54
->workqueue
= create_singlethread_workqueue("rmi4-poller");
681 rmi_f54_create_input_map(f54
);
682 rmi_f54_set_input(f54
, 0);
684 /* register video device */
685 strlcpy(f54
->v4l2
.name
, F54_NAME
, sizeof(f54
->v4l2
.name
));
686 ret
= v4l2_device_register(&fn
->dev
, &f54
->v4l2
);
688 dev_err(&fn
->dev
, "Unable to register video dev.\n");
692 /* initialize the queue */
693 mutex_init(&f54
->lock
);
694 f54
->queue
= rmi_f54_queue
;
695 f54
->queue
.drv_priv
= f54
;
696 f54
->queue
.lock
= &f54
->lock
;
697 f54
->queue
.dev
= &fn
->dev
;
699 ret
= vb2_queue_init(&f54
->queue
);
703 f54
->vdev
= rmi_f54_video_device
;
704 f54
->vdev
.v4l2_dev
= &f54
->v4l2
;
705 f54
->vdev
.lock
= &f54
->lock
;
706 f54
->vdev
.vfl_dir
= VFL_DIR_RX
;
707 f54
->vdev
.queue
= &f54
->queue
;
708 video_set_drvdata(&f54
->vdev
, f54
);
710 ret
= video_register_device(&f54
->vdev
, VFL_TYPE_TOUCH
, -1);
712 dev_err(&fn
->dev
, "Unable to register video subdevice.");
719 v4l2_device_unregister(&f54
->v4l2
);
721 cancel_delayed_work_sync(&f54
->work
);
722 flush_workqueue(f54
->workqueue
);
723 destroy_workqueue(f54
->workqueue
);
727 static void rmi_f54_remove(struct rmi_function
*fn
)
729 struct f54_data
*f54
= dev_get_drvdata(&fn
->dev
);
731 video_unregister_device(&f54
->vdev
);
732 v4l2_device_unregister(&f54
->v4l2
);
735 struct rmi_function_handler rmi_f54_handler
= {
740 .probe
= rmi_f54_probe
,
741 .config
= rmi_f54_config
,
742 .remove
= rmi_f54_remove
,