xref: /openbmc/linux/drivers/input/rmi4/rmi_f54.c (revision a36954f5)
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 	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 
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 
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 
298 static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
299 {
300 	struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
301 	u16 *ptr;
302 	enum vb2_buffer_state state;
303 	enum rmi_f54_report_type reptype;
304 	int ret;
305 
306 	mutex_lock(&f54->status_mutex);
307 
308 	reptype = rmi_f54_get_reptype(f54, f54->input);
309 	if (reptype == F54_REPORT_NONE) {
310 		state = VB2_BUF_STATE_ERROR;
311 		goto done;
312 	}
313 
314 	if (f54->is_busy) {
315 		state = VB2_BUF_STATE_ERROR;
316 		goto done;
317 	}
318 
319 	ret = rmi_f54_request_report(f54->fn, reptype);
320 	if (ret) {
321 		dev_err(&f54->fn->dev, "Error requesting F54 report\n");
322 		state = VB2_BUF_STATE_ERROR;
323 		goto done;
324 	}
325 
326 	/* get frame data */
327 	mutex_lock(&f54->data_mutex);
328 
329 	while (f54->is_busy) {
330 		mutex_unlock(&f54->data_mutex);
331 		if (!wait_for_completion_timeout(&f54->cmd_done,
332 						 msecs_to_jiffies(1000))) {
333 			dev_err(&f54->fn->dev, "Timed out\n");
334 			state = VB2_BUF_STATE_ERROR;
335 			goto done;
336 		}
337 		mutex_lock(&f54->data_mutex);
338 	}
339 
340 	ptr = vb2_plane_vaddr(vb, 0);
341 	if (!ptr) {
342 		dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
343 		state = VB2_BUF_STATE_ERROR;
344 		goto data_done;
345 	}
346 
347 	memcpy(ptr, f54->report_data, f54->report_size);
348 	vb2_set_plane_payload(vb, 0, rmi_f54_get_report_size(f54));
349 	state = VB2_BUF_STATE_DONE;
350 
351 data_done:
352 	mutex_unlock(&f54->data_mutex);
353 done:
354 	vb2_buffer_done(vb, state);
355 	mutex_unlock(&f54->status_mutex);
356 }
357 
358 /* V4L2 structures */
359 static const struct vb2_ops rmi_f54_queue_ops = {
360 	.queue_setup            = rmi_f54_queue_setup,
361 	.buf_queue              = rmi_f54_buffer_queue,
362 	.wait_prepare           = vb2_ops_wait_prepare,
363 	.wait_finish            = vb2_ops_wait_finish,
364 };
365 
366 static const struct vb2_queue rmi_f54_queue = {
367 	.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
368 	.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
369 	.buf_struct_size = sizeof(struct vb2_buffer),
370 	.ops = &rmi_f54_queue_ops,
371 	.mem_ops = &vb2_vmalloc_memops,
372 	.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
373 	.min_buffers_needed = 1,
374 };
375 
376 static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
377 				   struct v4l2_capability *cap)
378 {
379 	struct f54_data *f54 = video_drvdata(file);
380 
381 	strlcpy(cap->driver, F54_NAME, sizeof(cap->driver));
382 	strlcpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
383 	snprintf(cap->bus_info, sizeof(cap->bus_info),
384 		"rmi4:%s", dev_name(&f54->fn->dev));
385 
386 	return 0;
387 }
388 
389 static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
390 				     struct v4l2_input *i)
391 {
392 	struct f54_data *f54 = video_drvdata(file);
393 	enum rmi_f54_report_type reptype;
394 
395 	reptype = rmi_f54_get_reptype(f54, i->index);
396 	if (reptype == F54_REPORT_NONE)
397 		return -EINVAL;
398 
399 	i->type = V4L2_INPUT_TYPE_TOUCH;
400 
401 	strlcpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
402 	return 0;
403 }
404 
405 static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
406 {
407 	struct rmi_device *rmi_dev = f54->fn->rmi_dev;
408 	struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
409 	u8 rx = drv_data->num_rx_electrodes ? : f54->num_rx_electrodes;
410 	u8 tx = drv_data->num_tx_electrodes ? : f54->num_tx_electrodes;
411 	struct v4l2_pix_format *f = &f54->format;
412 	enum rmi_f54_report_type reptype;
413 	int ret;
414 
415 	reptype = rmi_f54_get_reptype(f54, i);
416 	if (reptype == F54_REPORT_NONE)
417 		return -EINVAL;
418 
419 	ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
420 	if (ret)
421 		return ret;
422 
423 	f54->input = i;
424 
425 	f->width = rx;
426 	f->height = tx;
427 	f->field = V4L2_FIELD_NONE;
428 	f->colorspace = V4L2_COLORSPACE_RAW;
429 	f->bytesperline = f->width * sizeof(u16);
430 	f->sizeimage = f->width * f->height * sizeof(u16);
431 
432 	return 0;
433 }
434 
435 static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
436 {
437 	return rmi_f54_set_input(video_drvdata(file), i);
438 }
439 
440 static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
441 				  unsigned int *i)
442 {
443 	struct f54_data *f54 = video_drvdata(file);
444 
445 	*i = f54->input;
446 
447 	return 0;
448 }
449 
450 static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
451 			      struct v4l2_format *f)
452 {
453 	struct f54_data *f54 = video_drvdata(file);
454 
455 	f->fmt.pix = f54->format;
456 
457 	return 0;
458 }
459 
460 static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
461 				   struct v4l2_fmtdesc *fmt)
462 {
463 	if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
464 		return -EINVAL;
465 
466 	switch (fmt->index) {
467 	case 0:
468 		fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
469 		break;
470 
471 	case 1:
472 		fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD08;
473 		break;
474 
475 	case 2:
476 		fmt->pixelformat = V4L2_TCH_FMT_TU16;
477 		break;
478 
479 	default:
480 		return -EINVAL;
481 	}
482 
483 	return 0;
484 }
485 
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 
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 	struct rmi_f54_reports *report;
537 	int report_size;
538 	u8 command;
539 	u8 *data;
540 	int error;
541 
542 	data = f54->report_data;
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",
546 				f54->report_type);
547 		error = -EINVAL;
548 		goto error;     /* retry won't help */
549 	}
550 	f54->standard_report[0].size = report_size;
551 	report = f54->standard_report;
552 
553 	mutex_lock(&f54->data_mutex);
554 
555 	/*
556 	 * Need to check if command has completed.
557 	 * If not try again later.
558 	 */
559 	error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
560 			 &command);
561 	if (error) {
562 		dev_err(&fn->dev, "Failed to read back command\n");
563 		goto error;
564 	}
565 	if (command & F54_GET_REPORT) {
566 		if (time_after(jiffies, f54->timeout)) {
567 			dev_err(&fn->dev, "Get report command timed out\n");
568 			error = -ETIMEDOUT;
569 		}
570 		report_size = 0;
571 		goto error;
572 	}
573 
574 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
575 
576 	report_size = 0;
577 	for (; report->size; report++) {
578 		fifo[0] = report->start & 0xff;
579 		fifo[1] = (report->start >> 8) & 0xff;
580 		error = rmi_write_block(fn->rmi_dev,
581 					fn->fd.data_base_addr + F54_FIFO_OFFSET,
582 					fifo, sizeof(fifo));
583 		if (error) {
584 			dev_err(&fn->dev, "Failed to set fifo start offset\n");
585 			goto abort;
586 		}
587 
588 		error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
589 				       F54_REPORT_DATA_OFFSET, data,
590 				       report->size);
591 		if (error) {
592 			dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
593 				__func__, report->size, error);
594 			goto abort;
595 		}
596 		data += report->size;
597 		report_size += report->size;
598 	}
599 
600 abort:
601 	f54->report_size = error ? 0 : report_size;
602 error:
603 	if (error)
604 		report_size = 0;
605 
606 	if (report_size == 0 && !error) {
607 		queue_delayed_work(f54->workqueue, &f54->work,
608 				   msecs_to_jiffies(1));
609 	} else {
610 		f54->is_busy = false;
611 		complete(&f54->cmd_done);
612 	}
613 
614 	mutex_unlock(&f54->data_mutex);
615 }
616 
617 static int rmi_f54_attention(struct rmi_function *fn, unsigned long *irqbits)
618 {
619 	return 0;
620 }
621 
622 static int rmi_f54_config(struct rmi_function *fn)
623 {
624 	struct rmi_driver *drv = fn->rmi_dev->driver;
625 
626 	drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
627 
628 	return 0;
629 }
630 
631 static int rmi_f54_detect(struct rmi_function *fn)
632 {
633 	int error;
634 	struct f54_data *f54;
635 
636 	f54 = dev_get_drvdata(&fn->dev);
637 
638 	error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
639 			       &f54->qry, sizeof(f54->qry));
640 	if (error) {
641 		dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
642 			__func__);
643 		return error;
644 	}
645 
646 	f54->num_rx_electrodes = f54->qry[0];
647 	f54->num_tx_electrodes = f54->qry[1];
648 	f54->capabilities = f54->qry[2];
649 	f54->clock_rate = f54->qry[3] | (f54->qry[4] << 8);
650 	f54->family = f54->qry[5];
651 
652 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
653 		f54->num_rx_electrodes);
654 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
655 		f54->num_tx_electrodes);
656 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
657 		f54->capabilities);
658 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
659 		f54->clock_rate);
660 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
661 		f54->family);
662 
663 	f54->is_busy = false;
664 
665 	return 0;
666 }
667 
668 static int rmi_f54_probe(struct rmi_function *fn)
669 {
670 	struct f54_data *f54;
671 	int ret;
672 	u8 rx, tx;
673 
674 	f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
675 	if (!f54)
676 		return -ENOMEM;
677 
678 	f54->fn = fn;
679 	dev_set_drvdata(&fn->dev, f54);
680 
681 	ret = rmi_f54_detect(fn);
682 	if (ret)
683 		return ret;
684 
685 	mutex_init(&f54->data_mutex);
686 	mutex_init(&f54->status_mutex);
687 
688 	rx = f54->num_rx_electrodes;
689 	tx = f54->num_tx_electrodes;
690 	f54->report_data = devm_kzalloc(&fn->dev,
691 					sizeof(u16) * tx * rx,
692 					GFP_KERNEL);
693 	if (f54->report_data == NULL)
694 		return -ENOMEM;
695 
696 	INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
697 
698 	f54->workqueue = create_singlethread_workqueue("rmi4-poller");
699 	if (!f54->workqueue)
700 		return -ENOMEM;
701 
702 	rmi_f54_create_input_map(f54);
703 
704 	/* register video device */
705 	strlcpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
706 	ret = v4l2_device_register(&fn->dev, &f54->v4l2);
707 	if (ret) {
708 		dev_err(&fn->dev, "Unable to register video dev.\n");
709 		goto remove_wq;
710 	}
711 
712 	/* initialize the queue */
713 	mutex_init(&f54->lock);
714 	f54->queue = rmi_f54_queue;
715 	f54->queue.drv_priv = f54;
716 	f54->queue.lock = &f54->lock;
717 	f54->queue.dev = &fn->dev;
718 
719 	ret = vb2_queue_init(&f54->queue);
720 	if (ret)
721 		goto remove_v4l2;
722 
723 	f54->vdev = rmi_f54_video_device;
724 	f54->vdev.v4l2_dev = &f54->v4l2;
725 	f54->vdev.lock = &f54->lock;
726 	f54->vdev.vfl_dir = VFL_DIR_RX;
727 	f54->vdev.queue = &f54->queue;
728 	video_set_drvdata(&f54->vdev, f54);
729 
730 	ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
731 	if (ret) {
732 		dev_err(&fn->dev, "Unable to register video subdevice.");
733 		goto remove_v4l2;
734 	}
735 
736 	return 0;
737 
738 remove_v4l2:
739 	v4l2_device_unregister(&f54->v4l2);
740 remove_wq:
741 	cancel_delayed_work_sync(&f54->work);
742 	flush_workqueue(f54->workqueue);
743 	destroy_workqueue(f54->workqueue);
744 	return ret;
745 }
746 
747 static void rmi_f54_remove(struct rmi_function *fn)
748 {
749 	struct f54_data *f54 = dev_get_drvdata(&fn->dev);
750 
751 	video_unregister_device(&f54->vdev);
752 	v4l2_device_unregister(&f54->v4l2);
753 }
754 
755 struct rmi_function_handler rmi_f54_handler = {
756 	.driver = {
757 		.name = F54_NAME,
758 	},
759 	.func = 0x54,
760 	.probe = rmi_f54_probe,
761 	.config = rmi_f54_config,
762 	.attention = rmi_f54_attention,
763 	.remove = rmi_f54_remove,
764 };
765