GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / input / rmi4 / rmi_f54.c
1 /*
2  * Copyright (c) 2012-2015 Synaptics Incorporated
3  * Copyright (C) 2016 Zodiac Inflight Innovations
4  *
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.
8  */
9
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"
21
22 #define F54_NAME                "rmi4_f54"
23
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
29
30 /* F54 commands */
31 #define F54_GET_REPORT          1
32 #define F54_FORCE_CAL           2
33
34 /* Fixed sizes of reports */
35 #define F54_QUERY_LEN                   27
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_8BIT_IMAGE:     Normalized 8-Bit Image Report. The capacitance variance
46  *                      from baseline for each pixel.
47  *
48  * @F54_16BIT_IMAGE:    Normalized 16-Bit Image Report. The capacitance variance
49  *                      from baseline for each pixel.
50  *
51  * @F54_RAW_16BIT_IMAGE:
52  *                      Raw 16-Bit Image Report. The raw capacitance for each
53  *                      pixel.
54  *
55  * @F54_TRUE_BASELINE:  True Baseline Report. The baseline capacitance for each
56  *                      pixel.
57  *
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.
61  *
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.
67  */
68 enum rmi_f54_report_type {
69         F54_REPORT_NONE = 0,
70         F54_8BIT_IMAGE = 1,
71         F54_16BIT_IMAGE = 2,
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,
76         F54_MAX_REPORT_TYPE,
77 };
78
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",
88 };
89
90 struct rmi_f54_reports {
91         int start;
92         int size;
93 };
94
95 struct f54_data {
96         struct rmi_function *fn;
97
98         u8 qry[F54_QUERY_LEN];
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         struct rmi_f54_reports standard_report[2];
109
110         bool is_busy;
111         struct mutex status_mutex;
112         struct mutex data_mutex;
113
114         struct workqueue_struct *workqueue;
115         struct delayed_work work;
116         unsigned long timeout;
117
118         struct completion cmd_done;
119
120         /* V4L2 support */
121         struct v4l2_device v4l2;
122         struct v4l2_pix_format format;
123         struct video_device vdev;
124         struct vb2_queue queue;
125         struct mutex lock;
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  */
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
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
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
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
218 static size_t rmi_f54_get_report_size(struct f54_data *f54)
219 {
220         u8 rx = f54->num_rx_electrodes ? : f54->num_rx_electrodes;
221         u8 tx = f54->num_tx_electrodes ? : f54->num_tx_electrodes;
222         size_t size;
223
224         switch (rmi_f54_get_reptype(f54, f54->input)) {
225         case F54_8BIT_IMAGE:
226                 size = rx * tx;
227                 break;
228         case F54_16BIT_IMAGE:
229         case F54_RAW_16BIT_IMAGE:
230         case F54_TRUE_BASELINE:
231         case F54_FULL_RAW_CAP:
232         case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
233                 size = sizeof(u16) * rx * tx;
234                 break;
235         default:
236                 size = 0;
237         }
238
239         return size;
240 }
241
242 static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype, u32 *pixfmt)
243 {
244         int ret = 0;
245
246         switch (reptype) {
247         case F54_8BIT_IMAGE:
248                 *pixfmt = V4L2_TCH_FMT_DELTA_TD08;
249                 break;
250
251         case F54_16BIT_IMAGE:
252                 *pixfmt = V4L2_TCH_FMT_DELTA_TD16;
253                 break;
254
255         case F54_RAW_16BIT_IMAGE:
256         case F54_TRUE_BASELINE:
257         case F54_FULL_RAW_CAP:
258         case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
259                 *pixfmt = V4L2_TCH_FMT_TU16;
260                 break;
261
262         case F54_REPORT_NONE:
263         case F54_MAX_REPORT_TYPE:
264                 ret = -EINVAL;
265                 break;
266         }
267
268         return ret;
269 }
270
271 static const struct v4l2_file_operations rmi_f54_video_fops = {
272         .owner = THIS_MODULE,
273         .open = v4l2_fh_open,
274         .release = vb2_fop_release,
275         .unlocked_ioctl = video_ioctl2,
276         .read = vb2_fop_read,
277         .mmap = vb2_fop_mmap,
278         .poll = vb2_fop_poll,
279 };
280
281 static int rmi_f54_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
282                                unsigned int *nplanes, unsigned int sizes[],
283                                struct device *alloc_devs[])
284 {
285         struct f54_data *f54 = q->drv_priv;
286
287         if (*nplanes)
288                 return sizes[0] < rmi_f54_get_report_size(f54) ? -EINVAL : 0;
289
290         *nplanes = 1;
291         sizes[0] = rmi_f54_get_report_size(f54);
292
293         return 0;
294 }
295
296 static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
297 {
298         struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
299         u16 *ptr;
300         enum vb2_buffer_state state;
301         enum rmi_f54_report_type reptype;
302         int ret;
303
304         mutex_lock(&f54->status_mutex);
305
306         reptype = rmi_f54_get_reptype(f54, f54->input);
307         if (reptype == F54_REPORT_NONE) {
308                 state = VB2_BUF_STATE_ERROR;
309                 goto done;
310         }
311
312         if (f54->is_busy) {
313                 state = VB2_BUF_STATE_ERROR;
314                 goto done;
315         }
316
317         ret = rmi_f54_request_report(f54->fn, reptype);
318         if (ret) {
319                 dev_err(&f54->fn->dev, "Error requesting F54 report\n");
320                 state = VB2_BUF_STATE_ERROR;
321                 goto done;
322         }
323
324         /* get frame data */
325         mutex_lock(&f54->data_mutex);
326
327         while (f54->is_busy) {
328                 mutex_unlock(&f54->data_mutex);
329                 if (!wait_for_completion_timeout(&f54->cmd_done,
330                                                  msecs_to_jiffies(1000))) {
331                         dev_err(&f54->fn->dev, "Timed out\n");
332                         state = VB2_BUF_STATE_ERROR;
333                         goto done;
334                 }
335                 mutex_lock(&f54->data_mutex);
336         }
337
338         ptr = vb2_plane_vaddr(vb, 0);
339         if (!ptr) {
340                 dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
341                 state = VB2_BUF_STATE_ERROR;
342                 goto data_done;
343         }
344
345         memcpy(ptr, f54->report_data, f54->report_size);
346         vb2_set_plane_payload(vb, 0, rmi_f54_get_report_size(f54));
347         state = VB2_BUF_STATE_DONE;
348
349 data_done:
350         mutex_unlock(&f54->data_mutex);
351 done:
352         vb2_buffer_done(vb, state);
353         mutex_unlock(&f54->status_mutex);
354 }
355
356 /* V4L2 structures */
357 static const struct vb2_ops rmi_f54_queue_ops = {
358         .queue_setup            = rmi_f54_queue_setup,
359         .buf_queue              = rmi_f54_buffer_queue,
360         .wait_prepare           = vb2_ops_wait_prepare,
361         .wait_finish            = vb2_ops_wait_finish,
362 };
363
364 static const struct vb2_queue rmi_f54_queue = {
365         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
366         .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
367         .buf_struct_size = sizeof(struct vb2_v4l2_buffer),
368         .ops = &rmi_f54_queue_ops,
369         .mem_ops = &vb2_vmalloc_memops,
370         .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
371         .min_buffers_needed = 1,
372 };
373
374 static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
375                                    struct v4l2_capability *cap)
376 {
377         struct f54_data *f54 = video_drvdata(file);
378
379         strlcpy(cap->driver, F54_NAME, sizeof(cap->driver));
380         strlcpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
381         snprintf(cap->bus_info, sizeof(cap->bus_info),
382                 "rmi4:%s", dev_name(&f54->fn->dev));
383
384         return 0;
385 }
386
387 static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
388                                      struct v4l2_input *i)
389 {
390         struct f54_data *f54 = video_drvdata(file);
391         enum rmi_f54_report_type reptype;
392
393         reptype = rmi_f54_get_reptype(f54, i->index);
394         if (reptype == F54_REPORT_NONE)
395                 return -EINVAL;
396
397         i->type = V4L2_INPUT_TYPE_TOUCH;
398
399         strlcpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
400         return 0;
401 }
402
403 static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
404 {
405         struct v4l2_pix_format *f = &f54->format;
406         enum rmi_f54_report_type reptype;
407         int ret;
408
409         reptype = rmi_f54_get_reptype(f54, i);
410         if (reptype == F54_REPORT_NONE)
411                 return -EINVAL;
412
413         ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
414         if (ret)
415                 return ret;
416
417         f54->input = i;
418
419         f->width = f54->num_rx_electrodes;
420         f->height = f54->num_tx_electrodes;
421         f->field = V4L2_FIELD_NONE;
422         f->colorspace = V4L2_COLORSPACE_RAW;
423         f->bytesperline = f->width * sizeof(u16);
424         f->sizeimage = f->width * f->height * sizeof(u16);
425
426         return 0;
427 }
428
429 static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
430 {
431         return rmi_f54_set_input(video_drvdata(file), i);
432 }
433
434 static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
435                                   unsigned int *i)
436 {
437         struct f54_data *f54 = video_drvdata(file);
438
439         *i = f54->input;
440
441         return 0;
442 }
443
444 static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
445                               struct v4l2_format *f)
446 {
447         struct f54_data *f54 = video_drvdata(file);
448
449         f->fmt.pix = f54->format;
450
451         return 0;
452 }
453
454 static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
455                                    struct v4l2_fmtdesc *fmt)
456 {
457         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
458                 return -EINVAL;
459
460         switch (fmt->index) {
461         case 0:
462                 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
463                 break;
464
465         case 1:
466                 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD08;
467                 break;
468
469         case 2:
470                 fmt->pixelformat = V4L2_TCH_FMT_TU16;
471                 break;
472
473         default:
474                 return -EINVAL;
475         }
476
477         return 0;
478 }
479
480 static int rmi_f54_vidioc_g_parm(struct file *file, void *fh,
481                                  struct v4l2_streamparm *a)
482 {
483         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
484                 return -EINVAL;
485
486         a->parm.capture.readbuffers = 1;
487         a->parm.capture.timeperframe.numerator = 1;
488         a->parm.capture.timeperframe.denominator = 10;
489         return 0;
490 }
491
492 static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops = {
493         .vidioc_querycap        = rmi_f54_vidioc_querycap,
494
495         .vidioc_enum_fmt_vid_cap = rmi_f54_vidioc_enum_fmt,
496         .vidioc_s_fmt_vid_cap   = rmi_f54_vidioc_fmt,
497         .vidioc_g_fmt_vid_cap   = rmi_f54_vidioc_fmt,
498         .vidioc_try_fmt_vid_cap = rmi_f54_vidioc_fmt,
499         .vidioc_g_parm          = rmi_f54_vidioc_g_parm,
500
501         .vidioc_enum_input      = rmi_f54_vidioc_enum_input,
502         .vidioc_g_input         = rmi_f54_vidioc_g_input,
503         .vidioc_s_input         = rmi_f54_vidioc_s_input,
504
505         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
506         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
507         .vidioc_querybuf        = vb2_ioctl_querybuf,
508         .vidioc_qbuf            = vb2_ioctl_qbuf,
509         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
510         .vidioc_expbuf          = vb2_ioctl_expbuf,
511
512         .vidioc_streamon        = vb2_ioctl_streamon,
513         .vidioc_streamoff       = vb2_ioctl_streamoff,
514 };
515
516 static const struct video_device rmi_f54_video_device = {
517         .name = "Synaptics RMI4",
518         .fops = &rmi_f54_video_fops,
519         .ioctl_ops = &rmi_f54_video_ioctl_ops,
520         .release = video_device_release_empty,
521         .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
522                        V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
523 };
524
525 static void rmi_f54_work(struct work_struct *work)
526 {
527         struct f54_data *f54 = container_of(work, struct f54_data, work.work);
528         struct rmi_function *fn = f54->fn;
529         u8 fifo[2];
530         struct rmi_f54_reports *report;
531         int report_size;
532         u8 command;
533         u8 *data;
534         int error;
535
536         data = f54->report_data;
537         report_size = rmi_f54_get_report_size(f54);
538         if (report_size == 0) {
539                 dev_err(&fn->dev, "Bad report size, report type=%d\n",
540                                 f54->report_type);
541                 error = -EINVAL;
542                 goto error;     /* retry won't help */
543         }
544         f54->standard_report[0].size = report_size;
545         report = f54->standard_report;
546
547         mutex_lock(&f54->data_mutex);
548
549         /*
550          * Need to check if command has completed.
551          * If not try again later.
552          */
553         error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
554                          &command);
555         if (error) {
556                 dev_err(&fn->dev, "Failed to read back command\n");
557                 goto error;
558         }
559         if (command & F54_GET_REPORT) {
560                 if (time_after(jiffies, f54->timeout)) {
561                         dev_err(&fn->dev, "Get report command timed out\n");
562                         error = -ETIMEDOUT;
563                 }
564                 report_size = 0;
565                 goto error;
566         }
567
568         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
569
570         report_size = 0;
571         for (; report->size; report++) {
572                 fifo[0] = report->start & 0xff;
573                 fifo[1] = (report->start >> 8) & 0xff;
574                 error = rmi_write_block(fn->rmi_dev,
575                                         fn->fd.data_base_addr + F54_FIFO_OFFSET,
576                                         fifo, sizeof(fifo));
577                 if (error) {
578                         dev_err(&fn->dev, "Failed to set fifo start offset\n");
579                         goto abort;
580                 }
581
582                 error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
583                                        F54_REPORT_DATA_OFFSET, data,
584                                        report->size);
585                 if (error) {
586                         dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
587                                 __func__, report->size, error);
588                         goto abort;
589                 }
590                 data += report->size;
591                 report_size += report->size;
592         }
593
594 abort:
595         f54->report_size = error ? 0 : report_size;
596 error:
597         if (error)
598                 report_size = 0;
599
600         if (report_size == 0 && !error) {
601                 queue_delayed_work(f54->workqueue, &f54->work,
602                                    msecs_to_jiffies(1));
603         } else {
604                 f54->is_busy = false;
605                 complete(&f54->cmd_done);
606         }
607
608         mutex_unlock(&f54->data_mutex);
609 }
610
611 static int rmi_f54_attention(struct rmi_function *fn, unsigned long *irqbits)
612 {
613         return 0;
614 }
615
616 static int rmi_f54_config(struct rmi_function *fn)
617 {
618         struct rmi_driver *drv = fn->rmi_dev->driver;
619
620         drv->clear_irq_bits(fn->rmi_dev, fn->irq_mask);
621
622         return 0;
623 }
624
625 static int rmi_f54_detect(struct rmi_function *fn)
626 {
627         int error;
628         struct f54_data *f54;
629
630         f54 = dev_get_drvdata(&fn->dev);
631
632         error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
633                                &f54->qry, sizeof(f54->qry));
634         if (error) {
635                 dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
636                         __func__);
637                 return error;
638         }
639
640         f54->num_rx_electrodes = f54->qry[0];
641         f54->num_tx_electrodes = f54->qry[1];
642         f54->capabilities = f54->qry[2];
643         f54->clock_rate = f54->qry[3] | (f54->qry[4] << 8);
644         f54->family = f54->qry[5];
645
646         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
647                 f54->num_rx_electrodes);
648         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
649                 f54->num_tx_electrodes);
650         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
651                 f54->capabilities);
652         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
653                 f54->clock_rate);
654         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
655                 f54->family);
656
657         f54->is_busy = false;
658
659         return 0;
660 }
661
662 static int rmi_f54_probe(struct rmi_function *fn)
663 {
664         struct f54_data *f54;
665         int ret;
666         u8 rx, tx;
667
668         f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
669         if (!f54)
670                 return -ENOMEM;
671
672         f54->fn = fn;
673         dev_set_drvdata(&fn->dev, f54);
674
675         ret = rmi_f54_detect(fn);
676         if (ret)
677                 return ret;
678
679         mutex_init(&f54->data_mutex);
680         mutex_init(&f54->status_mutex);
681
682         rx = f54->num_rx_electrodes;
683         tx = f54->num_tx_electrodes;
684         f54->report_data = devm_kzalloc(&fn->dev,
685                                         sizeof(u16) * tx * rx,
686                                         GFP_KERNEL);
687         if (f54->report_data == NULL)
688                 return -ENOMEM;
689
690         INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
691
692         f54->workqueue = create_singlethread_workqueue("rmi4-poller");
693         if (!f54->workqueue)
694                 return -ENOMEM;
695
696         rmi_f54_create_input_map(f54);
697
698         /* register video device */
699         strlcpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
700         ret = v4l2_device_register(&fn->dev, &f54->v4l2);
701         if (ret) {
702                 dev_err(&fn->dev, "Unable to register video dev.\n");
703                 goto remove_wq;
704         }
705
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;
712
713         ret = vb2_queue_init(&f54->queue);
714         if (ret)
715                 goto remove_v4l2;
716
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);
723
724         ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
725         if (ret) {
726                 dev_err(&fn->dev, "Unable to register video subdevice.");
727                 goto remove_v4l2;
728         }
729
730         return 0;
731
732 remove_v4l2:
733         v4l2_device_unregister(&f54->v4l2);
734 remove_wq:
735         cancel_delayed_work_sync(&f54->work);
736         flush_workqueue(f54->workqueue);
737         destroy_workqueue(f54->workqueue);
738         return ret;
739 }
740
741 static void rmi_f54_remove(struct rmi_function *fn)
742 {
743         struct f54_data *f54 = dev_get_drvdata(&fn->dev);
744
745         video_unregister_device(&f54->vdev);
746         v4l2_device_unregister(&f54->v4l2);
747         destroy_workqueue(f54->workqueue);
748 }
749
750 struct rmi_function_handler rmi_f54_handler = {
751         .driver = {
752                 .name = F54_NAME,
753         },
754         .func = 0x54,
755         .probe = rmi_f54_probe,
756         .config = rmi_f54_config,
757         .attention = rmi_f54_attention,
758         .remove = rmi_f54_remove,
759 };