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 int report_size;
108
109 bool is_busy;
110 struct mutex status_mutex;
111 struct mutex data_mutex;
112
113 struct workqueue_struct *workqueue;
114 struct delayed_work work;
115 unsigned long timeout;
116
117 struct completion cmd_done;
118
119 /* V4L2 support */
120 struct v4l2_device v4l2;
121 struct v4l2_pix_format format;
122 struct video_device vdev;
123 struct vb2_queue queue;
124 struct mutex lock;
125 u32 sequence;
126 int input;
127 enum rmi_f54_report_type inputs[F54_MAX_REPORT_TYPE];
128 };
129
130 /*
131 * Basic checks on report_type to ensure we write a valid type
132 * to the sensor.
133 */
is_f54_report_type_valid(struct f54_data * f54,enum rmi_f54_report_type reptype)134 static bool is_f54_report_type_valid(struct f54_data *f54,
135 enum rmi_f54_report_type reptype)
136 {
137 switch (reptype) {
138 case F54_8BIT_IMAGE:
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:
147 return true;
148 default:
149 return false;
150 }
151 }
152
rmi_f54_get_reptype(struct f54_data * f54,unsigned int i)153 static enum rmi_f54_report_type rmi_f54_get_reptype(struct f54_data *f54,
154 unsigned int i)
155 {
156 if (i >= F54_MAX_REPORT_TYPE)
157 return F54_REPORT_NONE;
158
159 return f54->inputs[i];
160 }
161
rmi_f54_create_input_map(struct f54_data * f54)162 static void rmi_f54_create_input_map(struct f54_data *f54)
163 {
164 int i = 0;
165 enum rmi_f54_report_type reptype;
166
167 for (reptype = 1; reptype < F54_MAX_REPORT_TYPE; reptype++) {
168 if (!is_f54_report_type_valid(f54, reptype))
169 continue;
170
171 f54->inputs[i++] = reptype;
172 }
173
174 /* Remaining values are zero via kzalloc */
175 }
176
rmi_f54_request_report(struct rmi_function * fn,u8 report_type)177 static int rmi_f54_request_report(struct rmi_function *fn, u8 report_type)
178 {
179 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
180 struct rmi_device *rmi_dev = fn->rmi_dev;
181 int error;
182
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,
186 report_type);
187 if (error)
188 return error;
189 f54->report_type = report_type;
190 }
191
192 /*
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.
196 */
197 usleep_range(2000, 3000);
198
199 mutex_lock(&f54->data_mutex);
200
201 error = rmi_write(rmi_dev, fn->fd.command_base_addr, F54_GET_REPORT);
202 if (error < 0)
203 goto unlock;
204
205 init_completion(&f54->cmd_done);
206
207 f54->is_busy = 1;
208 f54->timeout = jiffies + msecs_to_jiffies(100);
209
210 queue_delayed_work(f54->workqueue, &f54->work, 0);
211
212 unlock:
213 mutex_unlock(&f54->data_mutex);
214
215 return error;
216 }
217
rmi_f54_get_report_size(struct f54_data * f54)218 static size_t rmi_f54_get_report_size(struct f54_data *f54)
219 {
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;
224 size_t size;
225
226 switch (rmi_f54_get_reptype(f54, f54->input)) {
227 case F54_8BIT_IMAGE:
228 size = rx * tx;
229 break;
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;
236 break;
237 default:
238 size = 0;
239 }
240
241 return size;
242 }
243
rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype,u32 * pixfmt)244 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype, u32 *pixfmt)
245 {
246 int ret = 0;
247
248 switch (reptype) {
249 case F54_8BIT_IMAGE:
250 *pixfmt = V4L2_TCH_FMT_DELTA_TD08;
251 break;
252
253 case F54_16BIT_IMAGE:
254 *pixfmt = V4L2_TCH_FMT_DELTA_TD16;
255 break;
256
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;
262 break;
263
264 case F54_REPORT_NONE:
265 case F54_MAX_REPORT_TYPE:
266 ret = -EINVAL;
267 break;
268 }
269
270 return ret;
271 }
272
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,
281 };
282
rmi_f54_queue_setup(struct vb2_queue * q,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])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[])
286 {
287 struct f54_data *f54 = q->drv_priv;
288
289 if (*nplanes)
290 return sizes[0] < rmi_f54_get_report_size(f54) ? -EINVAL : 0;
291
292 *nplanes = 1;
293 sizes[0] = rmi_f54_get_report_size(f54);
294
295 return 0;
296 }
297
rmi_f54_buffer_queue(struct vb2_buffer * vb)298 static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
299 {
300 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
301 struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
302 u16 *ptr;
303 enum vb2_buffer_state state;
304 enum rmi_f54_report_type reptype;
305 int ret;
306
307 mutex_lock(&f54->status_mutex);
308
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;
313 goto done;
314 }
315
316 if (f54->is_busy) {
317 state = VB2_BUF_STATE_ERROR;
318 goto done;
319 }
320
321 ret = rmi_f54_request_report(f54->fn, reptype);
322 if (ret) {
323 dev_err(&f54->fn->dev, "Error requesting F54 report\n");
324 state = VB2_BUF_STATE_ERROR;
325 goto done;
326 }
327
328 /* get frame data */
329 mutex_lock(&f54->data_mutex);
330
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;
337 goto done;
338 }
339 mutex_lock(&f54->data_mutex);
340 }
341
342 ptr = vb2_plane_vaddr(vb, 0);
343 if (!ptr) {
344 dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
345 state = VB2_BUF_STATE_ERROR;
346 goto data_done;
347 }
348
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;
352
353 data_done:
354 mutex_unlock(&f54->data_mutex);
355 done:
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);
361 }
362
rmi_f54_stop_streaming(struct vb2_queue * q)363 static void rmi_f54_stop_streaming(struct vb2_queue *q)
364 {
365 struct f54_data *f54 = vb2_get_drv_priv(q);
366
367 f54->sequence = 0;
368 }
369
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 };
376
377 static const struct vb2_queue rmi_f54_queue = {
378 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
379 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
380 .buf_struct_size = sizeof(struct vb2_v4l2_buffer),
381 .ops = &rmi_f54_queue_ops,
382 .mem_ops = &vb2_vmalloc_memops,
383 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
384 };
385
rmi_f54_vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)386 static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
387 struct v4l2_capability *cap)
388 {
389 struct f54_data *f54 = video_drvdata(file);
390
391 strscpy(cap->driver, F54_NAME, sizeof(cap->driver));
392 strscpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
393 snprintf(cap->bus_info, sizeof(cap->bus_info),
394 "rmi4:%s", dev_name(&f54->fn->dev));
395
396 return 0;
397 }
398
rmi_f54_vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)399 static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
400 struct v4l2_input *i)
401 {
402 struct f54_data *f54 = video_drvdata(file);
403 enum rmi_f54_report_type reptype;
404
405 reptype = rmi_f54_get_reptype(f54, i->index);
406 if (reptype == F54_REPORT_NONE)
407 return -EINVAL;
408
409 i->type = V4L2_INPUT_TYPE_TOUCH;
410
411 strscpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
412 return 0;
413 }
414
rmi_f54_set_input(struct f54_data * f54,unsigned int i)415 static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
416 {
417 struct rmi_device *rmi_dev = f54->fn->rmi_dev;
418 struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
419 u8 rx = drv_data->num_rx_electrodes ? : f54->num_rx_electrodes;
420 u8 tx = drv_data->num_tx_electrodes ? : f54->num_tx_electrodes;
421 struct v4l2_pix_format *f = &f54->format;
422 enum rmi_f54_report_type reptype;
423 int ret;
424
425 reptype = rmi_f54_get_reptype(f54, i);
426 if (reptype == F54_REPORT_NONE)
427 return -EINVAL;
428
429 ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
430 if (ret)
431 return ret;
432
433 f54->input = i;
434
435 f->width = rx;
436 f->height = tx;
437 f->field = V4L2_FIELD_NONE;
438 f->colorspace = V4L2_COLORSPACE_RAW;
439 f->bytesperline = f->width * sizeof(u16);
440 f->sizeimage = f->width * f->height * sizeof(u16);
441
442 return 0;
443 }
444
rmi_f54_vidioc_s_input(struct file * file,void * priv,unsigned int i)445 static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
446 {
447 return rmi_f54_set_input(video_drvdata(file), i);
448 }
449
rmi_f54_vidioc_g_input(struct file * file,void * priv,unsigned int * i)450 static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
451 unsigned int *i)
452 {
453 struct f54_data *f54 = video_drvdata(file);
454
455 *i = f54->input;
456
457 return 0;
458 }
459
rmi_f54_vidioc_fmt(struct file * file,void * priv,struct v4l2_format * f)460 static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
461 struct v4l2_format *f)
462 {
463 struct f54_data *f54 = video_drvdata(file);
464
465 f->fmt.pix = f54->format;
466
467 return 0;
468 }
469
rmi_f54_vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * fmt)470 static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
471 struct v4l2_fmtdesc *fmt)
472 {
473 struct f54_data *f54 = video_drvdata(file);
474
475 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
476 return -EINVAL;
477
478 if (fmt->index)
479 return -EINVAL;
480
481 fmt->pixelformat = f54->format.pixelformat;
482
483 return 0;
484 }
485
rmi_f54_vidioc_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)486 static int rmi_f54_vidioc_g_parm(struct file *file, void *fh,
487 struct v4l2_streamparm *a)
488 {
489 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
490 return -EINVAL;
491
492 a->parm.capture.readbuffers = 1;
493 a->parm.capture.timeperframe.numerator = 1;
494 a->parm.capture.timeperframe.denominator = 10;
495 return 0;
496 }
497
498 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops = {
499 .vidioc_querycap = rmi_f54_vidioc_querycap,
500
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,
506
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,
510
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,
517
518 .vidioc_streamon = vb2_ioctl_streamon,
519 .vidioc_streamoff = vb2_ioctl_streamoff,
520 };
521
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,
529 };
530
rmi_f54_work(struct work_struct * work)531 static void rmi_f54_work(struct work_struct *work)
532 {
533 struct f54_data *f54 = container_of(work, struct f54_data, work.work);
534 struct rmi_function *fn = f54->fn;
535 u8 fifo[2];
536 int report_size;
537 u8 command;
538 int error;
539 int i;
540
541 report_size = rmi_f54_get_report_size(f54);
542 if (report_size == 0) {
543 dev_err(&fn->dev, "Bad report size, report type=%d\n",
544 f54->report_type);
545 error = -EINVAL;
546 goto error; /* retry won't help */
547 }
548
549 mutex_lock(&f54->data_mutex);
550
551 /*
552 * Need to check if command has completed.
553 * If not try again later.
554 */
555 error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
556 &command);
557 if (error) {
558 dev_err(&fn->dev, "Failed to read back command\n");
559 goto error;
560 }
561 if (command & F54_GET_REPORT) {
562 if (time_after(jiffies, f54->timeout)) {
563 dev_err(&fn->dev, "Get report command timed out\n");
564 error = -ETIMEDOUT;
565 }
566 report_size = 0;
567 goto error;
568 }
569
570 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
571
572 for (i = 0; i < report_size; i += F54_REPORT_DATA_SIZE) {
573 int size = min(F54_REPORT_DATA_SIZE, report_size - i);
574
575 fifo[0] = i & 0xff;
576 fifo[1] = i >> 8;
577 error = rmi_write_block(fn->rmi_dev,
578 fn->fd.data_base_addr + F54_FIFO_OFFSET,
579 fifo, sizeof(fifo));
580 if (error) {
581 dev_err(&fn->dev, "Failed to set fifo start offset\n");
582 goto abort;
583 }
584
585 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
586 F54_REPORT_DATA_OFFSET,
587 f54->report_data + i, size);
588 if (error) {
589 dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
590 __func__, size, error);
591 goto abort;
592 }
593 }
594
595 abort:
596 f54->report_size = error ? 0 : report_size;
597 error:
598 if (error)
599 report_size = 0;
600
601 if (report_size == 0 && !error) {
602 queue_delayed_work(f54->workqueue, &f54->work,
603 msecs_to_jiffies(1));
604 } else {
605 f54->is_busy = false;
606 complete(&f54->cmd_done);
607 }
608
609 mutex_unlock(&f54->data_mutex);
610 }
611
rmi_f54_config(struct rmi_function * fn)612 static int rmi_f54_config(struct rmi_function *fn)
613 {
614 struct rmi_driver *drv = fn->rmi_dev->driver;
615
616 drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
617
618 return 0;
619 }
620
rmi_f54_detect(struct rmi_function * fn)621 static int rmi_f54_detect(struct rmi_function *fn)
622 {
623 int error;
624 struct f54_data *f54;
625 u8 buf[6];
626
627 f54 = dev_get_drvdata(&fn->dev);
628
629 error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
630 buf, sizeof(buf));
631 if (error) {
632 dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
633 __func__);
634 return error;
635 }
636
637 f54->num_rx_electrodes = buf[0];
638 f54->num_tx_electrodes = buf[1];
639 f54->capabilities = buf[2];
640 f54->clock_rate = buf[3] | (buf[4] << 8);
641 f54->family = buf[5];
642
643 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
644 f54->num_rx_electrodes);
645 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
646 f54->num_tx_electrodes);
647 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
648 f54->capabilities);
649 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
650 f54->clock_rate);
651 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
652 f54->family);
653
654 f54->is_busy = false;
655
656 return 0;
657 }
658
rmi_f54_probe(struct rmi_function * fn)659 static int rmi_f54_probe(struct rmi_function *fn)
660 {
661 struct f54_data *f54;
662 int ret;
663 u8 rx, tx;
664
665 f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
666 if (!f54)
667 return -ENOMEM;
668
669 f54->fn = fn;
670 dev_set_drvdata(&fn->dev, f54);
671
672 ret = rmi_f54_detect(fn);
673 if (ret)
674 return ret;
675
676 mutex_init(&f54->data_mutex);
677 mutex_init(&f54->status_mutex);
678
679 rx = f54->num_rx_electrodes;
680 tx = f54->num_tx_electrodes;
681 f54->report_data = devm_kzalloc(&fn->dev,
682 array3_size(tx, rx, sizeof(u16)),
683 GFP_KERNEL);
684 if (f54->report_data == NULL)
685 return -ENOMEM;
686
687 INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
688
689 f54->workqueue = create_singlethread_workqueue("rmi4-poller");
690 if (!f54->workqueue)
691 return -ENOMEM;
692
693 rmi_f54_create_input_map(f54);
694 rmi_f54_set_input(f54, 0);
695
696 /* register video device */
697 strscpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
698 ret = v4l2_device_register(&fn->dev, &f54->v4l2);
699 if (ret) {
700 dev_err(&fn->dev, "Unable to register video dev.\n");
701 goto remove_wq;
702 }
703
704 /* initialize the queue */
705 mutex_init(&f54->lock);
706 f54->queue = rmi_f54_queue;
707 f54->queue.drv_priv = f54;
708 f54->queue.lock = &f54->lock;
709 f54->queue.dev = &fn->dev;
710
711 ret = vb2_queue_init(&f54->queue);
712 if (ret)
713 goto remove_v4l2;
714
715 f54->vdev = rmi_f54_video_device;
716 f54->vdev.v4l2_dev = &f54->v4l2;
717 f54->vdev.lock = &f54->lock;
718 f54->vdev.vfl_dir = VFL_DIR_RX;
719 f54->vdev.queue = &f54->queue;
720 video_set_drvdata(&f54->vdev, f54);
721
722 ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
723 if (ret) {
724 dev_err(&fn->dev, "Unable to register video subdevice.");
725 goto remove_v4l2;
726 }
727
728 return 0;
729
730 remove_v4l2:
731 v4l2_device_unregister(&f54->v4l2);
732 remove_wq:
733 cancel_delayed_work_sync(&f54->work);
734 destroy_workqueue(f54->workqueue);
735 return ret;
736 }
737
rmi_f54_remove(struct rmi_function * fn)738 static void rmi_f54_remove(struct rmi_function *fn)
739 {
740 struct f54_data *f54 = dev_get_drvdata(&fn->dev);
741
742 video_unregister_device(&f54->vdev);
743 v4l2_device_unregister(&f54->v4l2);
744 destroy_workqueue(f54->workqueue);
745 }
746
747 struct rmi_function_handler rmi_f54_handler = {
748 .driver = {
749 .name = F54_NAME,
750 },
751 .func = 0x54,
752 .probe = rmi_f54_probe,
753 .config = rmi_f54_config,
754 .remove = rmi_f54_remove,
755 };
756