1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012-2015 Synaptics Incorporated
4 * Copyright (C) 2016 Zodiac Inflight Innovations
5 */
6
7 #include <linux/kernel.h>
8 #include <linux/rmi.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"
18
19 #define F54_NAME "rmi4_f54"
20
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
26
27 /*
28 * The smbus protocol can read only 32 bytes max at a time.
29 * But this should be fine for i2c/spi as well.
30 */
31 #define F54_REPORT_DATA_SIZE 32
32
33 /* F54 commands */
34 #define F54_GET_REPORT 1
35 #define F54_FORCE_CAL 2
36
37 /* F54 capabilities */
38 #define F54_CAP_BASELINE (1 << 2)
39 #define F54_CAP_IMAGE8 (1 << 3)
40 #define F54_CAP_IMAGE16 (1 << 6)
41
42 /**
43 * enum rmi_f54_report_type - RMI4 F54 report types
44 *
45 * @F54_REPORT_NONE: No Image Report.
46 *
47 * @F54_8BIT_IMAGE: Normalized 8-Bit Image Report. The capacitance variance
48 * from baseline for each pixel.
49 *
50 * @F54_16BIT_IMAGE: Normalized 16-Bit Image Report. The capacitance variance
51 * from baseline for each pixel.
52 *
53 * @F54_RAW_16BIT_IMAGE:
54 * Raw 16-Bit Image Report. The raw capacitance for each
55 * pixel.
56 *
57 * @F54_TRUE_BASELINE: True Baseline Report. The baseline capacitance for each
58 * pixel.
59 *
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.
63 *
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.
69 *
70 * @F54_MAX_REPORT_TYPE:
71 * Maximum number of Report Types. Used for sanity
72 * checking.
73 */
74 enum rmi_f54_report_type {
75 F54_REPORT_NONE = 0,
76 F54_8BIT_IMAGE = 1,
77 F54_16BIT_IMAGE = 2,
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,
82 F54_MAX_REPORT_TYPE,
83 };
84
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",
94 };
95
96 struct f54_data {
97 struct rmi_function *fn;
98
99 u8 num_rx_electrodes;
100 u8 num_tx_electrodes;
101 u8 capabilities;
102 u16 clock_rate;
103 u8 family;
104
105 enum rmi_f54_report_type report_type;
106 u8 *report_data;
107 size_t max_report_size;
108 int report_size;
109 int report_error;
110
111 bool is_busy;
112 struct mutex status_mutex;
113 struct mutex data_mutex;
114
115 struct workqueue_struct *workqueue;
116 struct delayed_work work;
117 unsigned long timeout;
118
119 struct completion cmd_done;
120
121 /* V4L2 support */
122 struct v4l2_device v4l2;
123 struct v4l2_pix_format format;
124 struct video_device vdev;
125 struct vb2_queue queue;
126 struct mutex lock;
127 u32 sequence;
128 int input;
129 enum rmi_f54_report_type inputs[F54_MAX_REPORT_TYPE];
130 };
131
132 /*
133 * Basic checks on report_type to ensure we write a valid type
134 * to the sensor.
135 */
is_f54_report_type_valid(struct f54_data * f54,enum rmi_f54_report_type reptype)136 static bool is_f54_report_type_valid(struct f54_data *f54,
137 enum rmi_f54_report_type reptype)
138 {
139 switch (reptype) {
140 case F54_8BIT_IMAGE:
141 return f54->capabilities & F54_CAP_IMAGE8;
142 case F54_16BIT_IMAGE:
143 case F54_RAW_16BIT_IMAGE:
144 return f54->capabilities & F54_CAP_IMAGE16;
145 case F54_TRUE_BASELINE:
146 return f54->capabilities & F54_CAP_IMAGE16;
147 case F54_FULL_RAW_CAP:
148 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
149 return true;
150 default:
151 return false;
152 }
153 }
154
rmi_f54_get_reptype(struct f54_data * f54,unsigned int i)155 static enum rmi_f54_report_type rmi_f54_get_reptype(struct f54_data *f54,
156 unsigned int i)
157 {
158 if (i >= F54_MAX_REPORT_TYPE)
159 return F54_REPORT_NONE;
160
161 return f54->inputs[i];
162 }
163
rmi_f54_create_input_map(struct f54_data * f54)164 static void rmi_f54_create_input_map(struct f54_data *f54)
165 {
166 int i = 0;
167 enum rmi_f54_report_type reptype;
168
169 for (reptype = 1; reptype < F54_MAX_REPORT_TYPE; reptype++) {
170 if (!is_f54_report_type_valid(f54, reptype))
171 continue;
172
173 f54->inputs[i++] = reptype;
174 }
175
176 /* Remaining values are zero via kzalloc */
177 }
178
rmi_f54_request_report(struct rmi_function * fn,u8 report_type)179 static int rmi_f54_request_report(struct rmi_function *fn, u8 report_type)
180 {
181 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
182 struct rmi_device *rmi_dev = fn->rmi_dev;
183 int error;
184
185 /* Write Report Type into F54_AD_Data0 */
186 if (f54->report_type != report_type) {
187 error = rmi_write(rmi_dev, f54->fn->fd.data_base_addr,
188 report_type);
189 if (error)
190 return error;
191 f54->report_type = report_type;
192 }
193
194 /*
195 * Small delay after disabling interrupts to avoid race condition
196 * in firmare. This value is a bit higher than absolutely necessary.
197 * Should be removed once issue is resolved in firmware.
198 */
199 usleep_range(2000, 3000);
200
201 mutex_lock(&f54->data_mutex);
202
203 error = rmi_write(rmi_dev, fn->fd.command_base_addr, F54_GET_REPORT);
204 if (error < 0)
205 goto unlock;
206
207 init_completion(&f54->cmd_done);
208
209 f54->is_busy = 1;
210 f54->timeout = jiffies + msecs_to_jiffies(100);
211
212 queue_delayed_work(f54->workqueue, &f54->work, 0);
213
214 unlock:
215 mutex_unlock(&f54->data_mutex);
216
217 return error;
218 }
219
rmi_f54_get_report_size(struct f54_data * f54)220 static size_t rmi_f54_get_report_size(struct f54_data *f54)
221 {
222 struct rmi_device *rmi_dev = f54->fn->rmi_dev;
223 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
224 u8 rx = drv_data->num_rx_electrodes ? : f54->num_rx_electrodes;
225 u8 tx = drv_data->num_tx_electrodes ? : f54->num_tx_electrodes;
226 size_t size;
227
228 switch (rmi_f54_get_reptype(f54, f54->input)) {
229 case F54_8BIT_IMAGE:
230 size = rx * tx;
231 break;
232 case F54_16BIT_IMAGE:
233 case F54_RAW_16BIT_IMAGE:
234 case F54_TRUE_BASELINE:
235 case F54_FULL_RAW_CAP:
236 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
237 size = sizeof(u16) * rx * tx;
238 break;
239 default:
240 size = 0;
241 }
242
243 return size;
244 }
245
rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype,u32 * pixfmt)246 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype, u32 *pixfmt)
247 {
248 int ret = 0;
249
250 switch (reptype) {
251 case F54_8BIT_IMAGE:
252 *pixfmt = V4L2_TCH_FMT_DELTA_TD08;
253 break;
254
255 case F54_16BIT_IMAGE:
256 *pixfmt = V4L2_TCH_FMT_DELTA_TD16;
257 break;
258
259 case F54_RAW_16BIT_IMAGE:
260 case F54_TRUE_BASELINE:
261 case F54_FULL_RAW_CAP:
262 case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
263 *pixfmt = V4L2_TCH_FMT_TU16;
264 break;
265
266 case F54_REPORT_NONE:
267 case F54_MAX_REPORT_TYPE:
268 ret = -EINVAL;
269 break;
270 }
271
272 return ret;
273 }
274
275 static const struct v4l2_file_operations rmi_f54_video_fops = {
276 .owner = THIS_MODULE,
277 .open = v4l2_fh_open,
278 .release = vb2_fop_release,
279 .unlocked_ioctl = video_ioctl2,
280 .read = vb2_fop_read,
281 .mmap = vb2_fop_mmap,
282 .poll = vb2_fop_poll,
283 };
284
rmi_f54_queue_setup(struct vb2_queue * q,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])285 static int rmi_f54_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
286 unsigned int *nplanes, unsigned int sizes[],
287 struct device *alloc_devs[])
288 {
289 struct f54_data *f54 = q->drv_priv;
290
291 if (*nplanes)
292 return sizes[0] < rmi_f54_get_report_size(f54) ? -EINVAL : 0;
293
294 *nplanes = 1;
295 sizes[0] = rmi_f54_get_report_size(f54);
296
297 return 0;
298 }
299
rmi_f54_buffer_queue(struct vb2_buffer * vb)300 static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
301 {
302 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
303 struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
304 u16 *ptr;
305 enum vb2_buffer_state state;
306 enum rmi_f54_report_type reptype;
307 int ret;
308
309 mutex_lock(&f54->status_mutex);
310
311 vb2_set_plane_payload(vb, 0, 0);
312 reptype = rmi_f54_get_reptype(f54, f54->input);
313 if (reptype == F54_REPORT_NONE) {
314 state = VB2_BUF_STATE_ERROR;
315 goto done;
316 }
317
318 if (f54->is_busy) {
319 state = VB2_BUF_STATE_ERROR;
320 goto done;
321 }
322
323 ret = rmi_f54_request_report(f54->fn, reptype);
324 if (ret) {
325 dev_err(&f54->fn->dev, "Error requesting F54 report\n");
326 state = VB2_BUF_STATE_ERROR;
327 goto done;
328 }
329
330 /* get frame data */
331 mutex_lock(&f54->data_mutex);
332
333 while (f54->is_busy) {
334 mutex_unlock(&f54->data_mutex);
335 if (!wait_for_completion_timeout(&f54->cmd_done,
336 msecs_to_jiffies(1000))) {
337 dev_err(&f54->fn->dev, "Timed out\n");
338 state = VB2_BUF_STATE_ERROR;
339 goto done;
340 }
341 mutex_lock(&f54->data_mutex);
342 }
343
344 if (f54->report_error) {
345 dev_err(&f54->fn->dev, "Error acquiring report: %d\n", f54->report_error);
346 state = VB2_BUF_STATE_ERROR;
347 goto data_done;
348 }
349
350 ptr = vb2_plane_vaddr(vb, 0);
351 if (!ptr) {
352 dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
353 state = VB2_BUF_STATE_ERROR;
354 goto data_done;
355 }
356
357 memcpy(ptr, f54->report_data, f54->report_size);
358 vb2_set_plane_payload(vb, 0, rmi_f54_get_report_size(f54));
359 state = VB2_BUF_STATE_DONE;
360
361 data_done:
362 mutex_unlock(&f54->data_mutex);
363 done:
364 vb->timestamp = ktime_get_ns();
365 vbuf->field = V4L2_FIELD_NONE;
366 vbuf->sequence = f54->sequence++;
367 vb2_buffer_done(vb, state);
368 mutex_unlock(&f54->status_mutex);
369 }
370
rmi_f54_stop_streaming(struct vb2_queue * q)371 static void rmi_f54_stop_streaming(struct vb2_queue *q)
372 {
373 struct f54_data *f54 = vb2_get_drv_priv(q);
374
375 f54->sequence = 0;
376 }
377
378 /* V4L2 structures */
379 static const struct vb2_ops rmi_f54_queue_ops = {
380 .queue_setup = rmi_f54_queue_setup,
381 .buf_queue = rmi_f54_buffer_queue,
382 .stop_streaming = rmi_f54_stop_streaming,
383 };
384
385 static const struct vb2_queue rmi_f54_queue = {
386 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
387 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
388 .buf_struct_size = sizeof(struct vb2_v4l2_buffer),
389 .ops = &rmi_f54_queue_ops,
390 .mem_ops = &vb2_vmalloc_memops,
391 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
392 };
393
rmi_f54_vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)394 static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
395 struct v4l2_capability *cap)
396 {
397 struct f54_data *f54 = video_drvdata(file);
398
399 strscpy(cap->driver, F54_NAME, sizeof(cap->driver));
400 strscpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
401 snprintf(cap->bus_info, sizeof(cap->bus_info),
402 "rmi4:%s", dev_name(&f54->fn->dev));
403
404 return 0;
405 }
406
rmi_f54_vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)407 static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
408 struct v4l2_input *i)
409 {
410 struct f54_data *f54 = video_drvdata(file);
411 enum rmi_f54_report_type reptype;
412
413 reptype = rmi_f54_get_reptype(f54, i->index);
414 if (reptype == F54_REPORT_NONE)
415 return -EINVAL;
416
417 i->type = V4L2_INPUT_TYPE_TOUCH;
418
419 strscpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
420 return 0;
421 }
422
rmi_f54_set_input(struct f54_data * f54,unsigned int i)423 static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
424 {
425 struct rmi_device *rmi_dev = f54->fn->rmi_dev;
426 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
427 u8 rx = drv_data->num_rx_electrodes ? : f54->num_rx_electrodes;
428 u8 tx = drv_data->num_tx_electrodes ? : f54->num_tx_electrodes;
429 struct v4l2_pix_format *f = &f54->format;
430 enum rmi_f54_report_type reptype;
431 int ret;
432
433 reptype = rmi_f54_get_reptype(f54, i);
434 if (reptype == F54_REPORT_NONE)
435 return -EINVAL;
436
437 ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
438 if (ret)
439 return ret;
440
441 f54->input = i;
442
443 f->width = rx;
444 f->height = tx;
445 f->field = V4L2_FIELD_NONE;
446 f->colorspace = V4L2_COLORSPACE_RAW;
447 f->bytesperline = f->width * sizeof(u16);
448 f->sizeimage = f->width * f->height * sizeof(u16);
449
450 return 0;
451 }
452
rmi_f54_vidioc_s_input(struct file * file,void * priv,unsigned int i)453 static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
454 {
455 struct f54_data *f54 = video_drvdata(file);
456
457 if (vb2_is_busy(&f54->queue))
458 return -EBUSY;
459
460 return rmi_f54_set_input(f54, i);
461 }
462
rmi_f54_vidioc_g_input(struct file * file,void * priv,unsigned int * i)463 static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
464 unsigned int *i)
465 {
466 struct f54_data *f54 = video_drvdata(file);
467
468 *i = f54->input;
469
470 return 0;
471 }
472
rmi_f54_vidioc_fmt(struct file * file,void * priv,struct v4l2_format * f)473 static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
474 struct v4l2_format *f)
475 {
476 struct f54_data *f54 = video_drvdata(file);
477
478 f->fmt.pix = f54->format;
479
480 return 0;
481 }
482
rmi_f54_vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * fmt)483 static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
484 struct v4l2_fmtdesc *fmt)
485 {
486 struct f54_data *f54 = video_drvdata(file);
487
488 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
489 return -EINVAL;
490
491 if (fmt->index)
492 return -EINVAL;
493
494 fmt->pixelformat = f54->format.pixelformat;
495
496 return 0;
497 }
498
rmi_f54_vidioc_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)499 static int rmi_f54_vidioc_g_parm(struct file *file, void *fh,
500 struct v4l2_streamparm *a)
501 {
502 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
503 return -EINVAL;
504
505 a->parm.capture.readbuffers = 1;
506 a->parm.capture.timeperframe.numerator = 1;
507 a->parm.capture.timeperframe.denominator = 10;
508 return 0;
509 }
510
511 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops = {
512 .vidioc_querycap = rmi_f54_vidioc_querycap,
513
514 .vidioc_enum_fmt_vid_cap = rmi_f54_vidioc_enum_fmt,
515 .vidioc_s_fmt_vid_cap = rmi_f54_vidioc_fmt,
516 .vidioc_g_fmt_vid_cap = rmi_f54_vidioc_fmt,
517 .vidioc_try_fmt_vid_cap = rmi_f54_vidioc_fmt,
518 .vidioc_g_parm = rmi_f54_vidioc_g_parm,
519
520 .vidioc_enum_input = rmi_f54_vidioc_enum_input,
521 .vidioc_g_input = rmi_f54_vidioc_g_input,
522 .vidioc_s_input = rmi_f54_vidioc_s_input,
523
524 .vidioc_reqbufs = vb2_ioctl_reqbufs,
525 .vidioc_create_bufs = vb2_ioctl_create_bufs,
526 .vidioc_querybuf = vb2_ioctl_querybuf,
527 .vidioc_qbuf = vb2_ioctl_qbuf,
528 .vidioc_dqbuf = vb2_ioctl_dqbuf,
529 .vidioc_expbuf = vb2_ioctl_expbuf,
530
531 .vidioc_streamon = vb2_ioctl_streamon,
532 .vidioc_streamoff = vb2_ioctl_streamoff,
533 };
534
535 static const struct video_device rmi_f54_video_device = {
536 .name = "Synaptics RMI4",
537 .fops = &rmi_f54_video_fops,
538 .ioctl_ops = &rmi_f54_video_ioctl_ops,
539 .release = video_device_release_empty,
540 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
541 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
542 };
543
rmi_f54_work(struct work_struct * work)544 static void rmi_f54_work(struct work_struct *work)
545 {
546 struct f54_data *f54 = container_of(work, struct f54_data, work.work);
547 struct rmi_function *fn = f54->fn;
548 u8 fifo[2];
549 int report_size;
550 u8 command;
551 int error;
552 int i;
553
554 mutex_lock(&f54->data_mutex);
555
556 report_size = rmi_f54_get_report_size(f54);
557 if (report_size == 0) {
558 dev_err(&fn->dev, "Bad report size, report type=%d\n",
559 f54->report_type);
560 error = -EINVAL;
561 goto out; /* retry won't help */
562 }
563
564 if (report_size > f54->max_report_size) {
565 dev_err(&fn->dev, "Report size %d exceeds buffer size %zu\n",
566 report_size, f54->max_report_size);
567 error = -EINVAL;
568 goto out;
569 }
570
571 /*
572 * Need to check if command has completed.
573 * If not try again later.
574 */
575 error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
576 &command);
577 if (error) {
578 dev_err(&fn->dev, "Failed to read back command\n");
579 goto out;
580 }
581 if (command & F54_GET_REPORT) {
582 if (time_after(jiffies, f54->timeout)) {
583 dev_err(&fn->dev, "Get report command timed out\n");
584 error = -ETIMEDOUT;
585 }
586 report_size = 0;
587 goto out;
588 }
589
590 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
591
592 for (i = 0; i < report_size; i += F54_REPORT_DATA_SIZE) {
593 int size = min(F54_REPORT_DATA_SIZE, report_size - i);
594
595 fifo[0] = i & 0xff;
596 fifo[1] = i >> 8;
597 error = rmi_write_block(fn->rmi_dev,
598 fn->fd.data_base_addr + F54_FIFO_OFFSET,
599 fifo, sizeof(fifo));
600 if (error) {
601 dev_err(&fn->dev, "Failed to set fifo start offset\n");
602 goto out;
603 }
604
605 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
606 F54_REPORT_DATA_OFFSET,
607 f54->report_data + i, size);
608 if (error) {
609 dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
610 __func__, size, error);
611 goto out;
612 }
613 }
614
615 out:
616 if (error)
617 report_size = 0;
618
619 f54->report_size = report_size;
620 f54->report_error = error;
621
622 if (report_size == 0 && !error) {
623 queue_delayed_work(f54->workqueue, &f54->work,
624 msecs_to_jiffies(1));
625 } else {
626 f54->is_busy = false;
627 complete(&f54->cmd_done);
628 }
629
630 mutex_unlock(&f54->data_mutex);
631 }
632
rmi_f54_config(struct rmi_function * fn)633 static int rmi_f54_config(struct rmi_function *fn)
634 {
635 struct rmi_driver *drv = fn->rmi_dev->driver;
636
637 drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
638
639 return 0;
640 }
641
rmi_f54_detect(struct rmi_function * fn)642 static int rmi_f54_detect(struct rmi_function *fn)
643 {
644 int error;
645 struct f54_data *f54;
646 u8 buf[6];
647
648 f54 = dev_get_drvdata(&fn->dev);
649
650 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
651 buf, sizeof(buf));
652 if (error) {
653 dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
654 __func__);
655 return error;
656 }
657
658 f54->num_rx_electrodes = buf[0];
659 f54->num_tx_electrodes = buf[1];
660 f54->capabilities = buf[2];
661 f54->clock_rate = buf[3] | (buf[4] << 8);
662 f54->family = buf[5];
663
664 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
665 f54->num_rx_electrodes);
666 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
667 f54->num_tx_electrodes);
668 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
669 f54->capabilities);
670 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
671 f54->clock_rate);
672 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
673 f54->family);
674
675 f54->is_busy = false;
676
677 return 0;
678 }
679
rmi_f54_probe(struct rmi_function * fn)680 static int rmi_f54_probe(struct rmi_function *fn)
681 {
682 struct f54_data *f54;
683 int ret;
684 u8 rx, tx;
685
686 f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
687 if (!f54)
688 return -ENOMEM;
689
690 f54->fn = fn;
691 dev_set_drvdata(&fn->dev, f54);
692
693 ret = rmi_f54_detect(fn);
694 if (ret)
695 return ret;
696
697 mutex_init(&f54->data_mutex);
698 mutex_init(&f54->status_mutex);
699
700 rx = f54->num_rx_electrodes;
701 tx = f54->num_tx_electrodes;
702 f54->max_report_size = array3_size(tx, rx, sizeof(u16));
703 f54->report_data = devm_kzalloc(&fn->dev, f54->max_report_size,
704 GFP_KERNEL);
705 if (f54->report_data == NULL)
706 return -ENOMEM;
707
708 INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
709
710 f54->workqueue = create_singlethread_workqueue("rmi4-poller");
711 if (!f54->workqueue)
712 return -ENOMEM;
713
714 rmi_f54_create_input_map(f54);
715 rmi_f54_set_input(f54, 0);
716
717 /* register video device */
718 strscpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
719 ret = v4l2_device_register(&fn->dev, &f54->v4l2);
720 if (ret) {
721 dev_err(&fn->dev, "Unable to register video dev.\n");
722 goto remove_wq;
723 }
724
725 /* initialize the queue */
726 mutex_init(&f54->lock);
727 f54->queue = rmi_f54_queue;
728 f54->queue.drv_priv = f54;
729 f54->queue.lock = &f54->lock;
730 f54->queue.dev = &fn->dev;
731
732 ret = vb2_queue_init(&f54->queue);
733 if (ret)
734 goto remove_v4l2;
735
736 f54->vdev = rmi_f54_video_device;
737 f54->vdev.v4l2_dev = &f54->v4l2;
738 f54->vdev.lock = &f54->lock;
739 f54->vdev.vfl_dir = VFL_DIR_RX;
740 f54->vdev.queue = &f54->queue;
741 video_set_drvdata(&f54->vdev, f54);
742
743 ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
744 if (ret) {
745 dev_err(&fn->dev, "Unable to register video subdevice.");
746 goto remove_v4l2;
747 }
748
749 return 0;
750
751 remove_v4l2:
752 v4l2_device_unregister(&f54->v4l2);
753 remove_wq:
754 cancel_delayed_work_sync(&f54->work);
755 destroy_workqueue(f54->workqueue);
756 return ret;
757 }
758
rmi_f54_remove(struct rmi_function * fn)759 static void rmi_f54_remove(struct rmi_function *fn)
760 {
761 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
762
763 video_unregister_device(&f54->vdev);
764 v4l2_device_unregister(&f54->v4l2);
765 destroy_workqueue(f54->workqueue);
766 }
767
768 struct rmi_function_handler rmi_f54_handler = {
769 .driver = {
770 .name = F54_NAME,
771 },
772 .func = 0x54,
773 .probe = rmi_f54_probe,
774 .config = rmi_f54_config,
775 .remove = rmi_f54_remove,
776 };
777