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