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 * The smbus protocol can read only 32 bytes max at a time.
29 * But this should be fine for i2c/spi as well.
31 #define F54_REPORT_DATA_SIZE 32
34 #define F54_GET_REPORT 1
35 #define F54_FORCE_CAL 2
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_REPORT_NONE: No Image Report.
47 * @F54_8BIT_IMAGE: Normalized 8-Bit Image Report. The capacitance variance
48 * from baseline for each pixel.
50 * @F54_16BIT_IMAGE: Normalized 16-Bit Image Report. The capacitance variance
51 * from baseline for each pixel.
53 * @F54_RAW_16BIT_IMAGE:
54 * Raw 16-Bit Image Report. The raw capacitance for each
57 * @F54_TRUE_BASELINE: True Baseline Report. The baseline capacitance for each
60 * @F54_FULL_RAW_CAP: Full Raw Capacitance Report. The raw capacitance with
61 * low reference set to its minimum value and high
62 * reference set to its maximum value.
64 * @F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
65 * Full Raw Capacitance with Receiver Offset Removed
66 * Report. Set Low reference to its minimum value and high
67 * references to its maximum value, then report the raw
68 * capacitance for each pixel.
70 * @F54_MAX_REPORT_TYPE:
71 * Maximum number of Report Types. Used for sanity
74 enum rmi_f54_report_type
{
78 F54_RAW_16BIT_IMAGE
= 3,
79 F54_TRUE_BASELINE
= 9,
80 F54_FULL_RAW_CAP
= 19,
81 F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
= 20,
85 static const char * const rmi_f54_report_type_names
[] = {
86 [F54_REPORT_NONE
] = "Unknown",
87 [F54_8BIT_IMAGE
] = "Normalized 8-Bit Image",
88 [F54_16BIT_IMAGE
] = "Normalized 16-Bit Image",
89 [F54_RAW_16BIT_IMAGE
] = "Raw 16-Bit Image",
90 [F54_TRUE_BASELINE
] = "True Baseline",
91 [F54_FULL_RAW_CAP
] = "Full Raw Capacitance",
92 [F54_FULL_RAW_CAP_RX_OFFSET_REMOVED
]
93 = "Full Raw Capacitance RX Offset Removed",
97 struct rmi_function
*fn
;
100 u8 num_tx_electrodes
;
105 enum rmi_f54_report_type report_type
;
110 struct mutex status_mutex
;
111 struct mutex data_mutex
;
113 struct workqueue_struct
*workqueue
;
114 struct delayed_work work
;
115 unsigned long timeout
;
117 struct completion cmd_done
;
120 struct v4l2_device v4l2
;
121 struct v4l2_pix_format format
;
122 struct video_device vdev
;
123 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 vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
301 struct f54_data
*f54
= vb2_get_drv_priv(vb
->vb2_queue
);
303 enum vb2_buffer_state state
;
304 enum rmi_f54_report_type reptype
;
307 mutex_lock(&f54
->status_mutex
);
309 vb2_set_plane_payload(vb
, 0, 0);
310 reptype
= rmi_f54_get_reptype(f54
, f54
->input
);
311 if (reptype
== F54_REPORT_NONE
) {
312 state
= VB2_BUF_STATE_ERROR
;
317 state
= VB2_BUF_STATE_ERROR
;
321 ret
= rmi_f54_request_report(f54
->fn
, reptype
);
323 dev_err(&f54
->fn
->dev
, "Error requesting F54 report\n");
324 state
= VB2_BUF_STATE_ERROR
;
329 mutex_lock(&f54
->data_mutex
);
331 while (f54
->is_busy
) {
332 mutex_unlock(&f54
->data_mutex
);
333 if (!wait_for_completion_timeout(&f54
->cmd_done
,
334 msecs_to_jiffies(1000))) {
335 dev_err(&f54
->fn
->dev
, "Timed out\n");
336 state
= VB2_BUF_STATE_ERROR
;
339 mutex_lock(&f54
->data_mutex
);
342 ptr
= vb2_plane_vaddr(vb
, 0);
344 dev_err(&f54
->fn
->dev
, "Error acquiring frame ptr\n");
345 state
= VB2_BUF_STATE_ERROR
;
349 memcpy(ptr
, f54
->report_data
, f54
->report_size
);
350 vb2_set_plane_payload(vb
, 0, rmi_f54_get_report_size(f54
));
351 state
= VB2_BUF_STATE_DONE
;
354 mutex_unlock(&f54
->data_mutex
);
356 vb
->timestamp
= ktime_get_ns();
357 vbuf
->field
= V4L2_FIELD_NONE
;
358 vbuf
->sequence
= f54
->sequence
++;
359 vb2_buffer_done(vb
, state
);
360 mutex_unlock(&f54
->status_mutex
);
363 static void rmi_f54_stop_streaming(struct vb2_queue
*q
)
365 struct f54_data
*f54
= vb2_get_drv_priv(q
);
370 /* V4L2 structures */
371 static const struct vb2_ops rmi_f54_queue_ops
= {
372 .queue_setup
= rmi_f54_queue_setup
,
373 .buf_queue
= rmi_f54_buffer_queue
,
374 .stop_streaming
= rmi_f54_stop_streaming
,
375 .wait_prepare
= vb2_ops_wait_prepare
,
376 .wait_finish
= vb2_ops_wait_finish
,
379 static const struct vb2_queue rmi_f54_queue
= {
380 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
381 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
382 .buf_struct_size
= sizeof(struct vb2_v4l2_buffer
),
383 .ops
= &rmi_f54_queue_ops
,
384 .mem_ops
= &vb2_vmalloc_memops
,
385 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
388 static int rmi_f54_vidioc_querycap(struct file
*file
, void *priv
,
389 struct v4l2_capability
*cap
)
391 struct f54_data
*f54
= video_drvdata(file
);
393 strlcpy(cap
->driver
, F54_NAME
, sizeof(cap
->driver
));
394 strlcpy(cap
->card
, SYNAPTICS_INPUT_DEVICE_NAME
, sizeof(cap
->card
));
395 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
396 "rmi4:%s", dev_name(&f54
->fn
->dev
));
401 static int rmi_f54_vidioc_enum_input(struct file
*file
, void *priv
,
402 struct v4l2_input
*i
)
404 struct f54_data
*f54
= video_drvdata(file
);
405 enum rmi_f54_report_type reptype
;
407 reptype
= rmi_f54_get_reptype(f54
, i
->index
);
408 if (reptype
== F54_REPORT_NONE
)
411 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
413 strlcpy(i
->name
, rmi_f54_report_type_names
[reptype
], sizeof(i
->name
));
417 static int rmi_f54_set_input(struct f54_data
*f54
, unsigned int i
)
419 struct rmi_device
*rmi_dev
= f54
->fn
->rmi_dev
;
420 struct rmi_driver_data
*drv_data
= dev_get_drvdata(&rmi_dev
->dev
);
421 u8 rx
= drv_data
->num_rx_electrodes
? : f54
->num_rx_electrodes
;
422 u8 tx
= drv_data
->num_tx_electrodes
? : f54
->num_tx_electrodes
;
423 struct v4l2_pix_format
*f
= &f54
->format
;
424 enum rmi_f54_report_type reptype
;
427 reptype
= rmi_f54_get_reptype(f54
, i
);
428 if (reptype
== F54_REPORT_NONE
)
431 ret
= rmi_f54_get_pixel_fmt(reptype
, &f
->pixelformat
);
439 f
->field
= V4L2_FIELD_NONE
;
440 f
->colorspace
= V4L2_COLORSPACE_RAW
;
441 f
->bytesperline
= f
->width
* sizeof(u16
);
442 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
447 static int rmi_f54_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
449 return rmi_f54_set_input(video_drvdata(file
), i
);
452 static int rmi_f54_vidioc_g_input(struct file
*file
, void *priv
,
455 struct f54_data
*f54
= video_drvdata(file
);
462 static int rmi_f54_vidioc_fmt(struct file
*file
, void *priv
,
463 struct v4l2_format
*f
)
465 struct f54_data
*f54
= video_drvdata(file
);
467 f
->fmt
.pix
= f54
->format
;
472 static int rmi_f54_vidioc_enum_fmt(struct file
*file
, void *priv
,
473 struct v4l2_fmtdesc
*fmt
)
475 struct f54_data
*f54
= video_drvdata(file
);
477 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
483 fmt
->pixelformat
= f54
->format
.pixelformat
;
488 static int rmi_f54_vidioc_g_parm(struct file
*file
, void *fh
,
489 struct v4l2_streamparm
*a
)
491 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
494 a
->parm
.capture
.readbuffers
= 1;
495 a
->parm
.capture
.timeperframe
.numerator
= 1;
496 a
->parm
.capture
.timeperframe
.denominator
= 10;
500 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops
= {
501 .vidioc_querycap
= rmi_f54_vidioc_querycap
,
503 .vidioc_enum_fmt_vid_cap
= rmi_f54_vidioc_enum_fmt
,
504 .vidioc_s_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
505 .vidioc_g_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
506 .vidioc_try_fmt_vid_cap
= rmi_f54_vidioc_fmt
,
507 .vidioc_g_parm
= rmi_f54_vidioc_g_parm
,
509 .vidioc_enum_input
= rmi_f54_vidioc_enum_input
,
510 .vidioc_g_input
= rmi_f54_vidioc_g_input
,
511 .vidioc_s_input
= rmi_f54_vidioc_s_input
,
513 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
514 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
515 .vidioc_querybuf
= vb2_ioctl_querybuf
,
516 .vidioc_qbuf
= vb2_ioctl_qbuf
,
517 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
518 .vidioc_expbuf
= vb2_ioctl_expbuf
,
520 .vidioc_streamon
= vb2_ioctl_streamon
,
521 .vidioc_streamoff
= vb2_ioctl_streamoff
,
524 static const struct video_device rmi_f54_video_device
= {
525 .name
= "Synaptics RMI4",
526 .fops
= &rmi_f54_video_fops
,
527 .ioctl_ops
= &rmi_f54_video_ioctl_ops
,
528 .release
= video_device_release_empty
,
529 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
530 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
533 static void rmi_f54_work(struct work_struct
*work
)
535 struct f54_data
*f54
= container_of(work
, struct f54_data
, work
.work
);
536 struct rmi_function
*fn
= f54
->fn
;
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 */
551 mutex_lock(&f54
->data_mutex
);
554 * Need to check if command has completed.
555 * If not try again later.
557 error
= rmi_read(fn
->rmi_dev
, f54
->fn
->fd
.command_base_addr
,
560 dev_err(&fn
->dev
, "Failed to read back command\n");
563 if (command
& F54_GET_REPORT
) {
564 if (time_after(jiffies
, f54
->timeout
)) {
565 dev_err(&fn
->dev
, "Get report command timed out\n");
572 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "Get report command completed, reading data\n");
574 for (i
= 0; i
< report_size
; i
+= F54_REPORT_DATA_SIZE
) {
575 int size
= min(F54_REPORT_DATA_SIZE
, report_size
- i
);
579 error
= rmi_write_block(fn
->rmi_dev
,
580 fn
->fd
.data_base_addr
+ F54_FIFO_OFFSET
,
583 dev_err(&fn
->dev
, "Failed to set fifo start offset\n");
587 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.data_base_addr
+
588 F54_REPORT_DATA_OFFSET
,
589 f54
->report_data
+ i
, size
);
591 dev_err(&fn
->dev
, "%s: read [%d bytes] returned %d\n",
592 __func__
, size
, error
);
598 f54
->report_size
= error
? 0 : report_size
;
603 if (report_size
== 0 && !error
) {
604 queue_delayed_work(f54
->workqueue
, &f54
->work
,
605 msecs_to_jiffies(1));
607 f54
->is_busy
= false;
608 complete(&f54
->cmd_done
);
611 mutex_unlock(&f54
->data_mutex
);
614 static int rmi_f54_config(struct rmi_function
*fn
)
616 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
618 drv
->clear_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
623 static int rmi_f54_detect(struct rmi_function
*fn
)
626 struct f54_data
*f54
;
629 f54
= dev_get_drvdata(&fn
->dev
);
631 error
= rmi_read_block(fn
->rmi_dev
, fn
->fd
.query_base_addr
,
634 dev_err(&fn
->dev
, "%s: Failed to query F54 properties\n",
639 f54
->num_rx_electrodes
= buf
[0];
640 f54
->num_tx_electrodes
= buf
[1];
641 f54
->capabilities
= buf
[2];
642 f54
->clock_rate
= buf
[3] | (buf
[4] << 8);
643 f54
->family
= buf
[5];
645 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 num_rx_electrodes: %d\n",
646 f54
->num_rx_electrodes
);
647 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 num_tx_electrodes: %d\n",
648 f54
->num_tx_electrodes
);
649 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 capabilities: 0x%x\n",
651 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 clock rate: 0x%x\n",
653 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "F54 family: 0x%x\n",
656 f54
->is_busy
= false;
661 static int rmi_f54_probe(struct rmi_function
*fn
)
663 struct f54_data
*f54
;
667 f54
= devm_kzalloc(&fn
->dev
, sizeof(struct f54_data
), GFP_KERNEL
);
672 dev_set_drvdata(&fn
->dev
, f54
);
674 ret
= rmi_f54_detect(fn
);
678 mutex_init(&f54
->data_mutex
);
679 mutex_init(&f54
->status_mutex
);
681 rx
= f54
->num_rx_electrodes
;
682 tx
= f54
->num_tx_electrodes
;
683 f54
->report_data
= devm_kzalloc(&fn
->dev
,
684 array3_size(tx
, rx
, sizeof(u16
)),
686 if (f54
->report_data
== NULL
)
689 INIT_DELAYED_WORK(&f54
->work
, rmi_f54_work
);
691 f54
->workqueue
= create_singlethread_workqueue("rmi4-poller");
695 rmi_f54_create_input_map(f54
);
696 rmi_f54_set_input(f54
, 0);
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 .remove
= rmi_f54_remove
,