rmi_f54.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. #include <linux/kernel.h>
  10. #include <linux/rmi.h>
  11. #include <linux/input.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/v4l2-ioctl.h>
  17. #include <media/videobuf2-v4l2.h>
  18. #include <media/videobuf2-vmalloc.h>
  19. #include "rmi_driver.h"
  20. #define F54_NAME "rmi4_f54"
  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. /* F54 commands */
  27. #define F54_GET_REPORT 1
  28. #define F54_FORCE_CAL 2
  29. /* Fixed sizes of reports */
  30. #define F54_QUERY_LEN 27
  31. /* F54 capabilities */
  32. #define F54_CAP_BASELINE (1 << 2)
  33. #define F54_CAP_IMAGE8 (1 << 3)
  34. #define F54_CAP_IMAGE16 (1 << 6)
  35. /**
  36. * enum rmi_f54_report_type - RMI4 F54 report types
  37. *
  38. * @F54_8BIT_IMAGE: Normalized 8-Bit Image Report. The capacitance variance
  39. * from baseline for each pixel.
  40. *
  41. * @F54_16BIT_IMAGE: Normalized 16-Bit Image Report. The capacitance variance
  42. * from baseline for each pixel.
  43. *
  44. * @F54_RAW_16BIT_IMAGE:
  45. * Raw 16-Bit Image Report. The raw capacitance for each
  46. * pixel.
  47. *
  48. * @F54_TRUE_BASELINE: True Baseline Report. The baseline capacitance for each
  49. * pixel.
  50. *
  51. * @F54_FULL_RAW_CAP: Full Raw Capacitance Report. The raw capacitance with
  52. * low reference set to its minimum value and high
  53. * reference set to its maximum value.
  54. *
  55. * @F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
  56. * Full Raw Capacitance with Receiver Offset Removed
  57. * Report. Set Low reference to its minimum value and high
  58. * references to its maximum value, then report the raw
  59. * capacitance for each pixel.
  60. */
  61. enum rmi_f54_report_type {
  62. F54_REPORT_NONE = 0,
  63. F54_8BIT_IMAGE = 1,
  64. F54_16BIT_IMAGE = 2,
  65. F54_RAW_16BIT_IMAGE = 3,
  66. F54_TRUE_BASELINE = 9,
  67. F54_FULL_RAW_CAP = 19,
  68. F54_FULL_RAW_CAP_RX_OFFSET_REMOVED = 20,
  69. F54_MAX_REPORT_TYPE,
  70. };
  71. const char *rmi_f54_report_type_names[] = {
  72. [F54_REPORT_NONE] = "Unknown",
  73. [F54_8BIT_IMAGE] = "Normalized 8-Bit Image",
  74. [F54_16BIT_IMAGE] = "Normalized 16-Bit Image",
  75. [F54_RAW_16BIT_IMAGE] = "Raw 16-Bit Image",
  76. [F54_TRUE_BASELINE] = "True Baseline",
  77. [F54_FULL_RAW_CAP] = "Full Raw Capacitance",
  78. [F54_FULL_RAW_CAP_RX_OFFSET_REMOVED]
  79. = "Full Raw Capacitance RX Offset Removed",
  80. };
  81. struct rmi_f54_reports {
  82. int start;
  83. int size;
  84. };
  85. struct f54_data {
  86. struct rmi_function *fn;
  87. u8 qry[F54_QUERY_LEN];
  88. u8 num_rx_electrodes;
  89. u8 num_tx_electrodes;
  90. u8 capabilities;
  91. u16 clock_rate;
  92. u8 family;
  93. enum rmi_f54_report_type report_type;
  94. u8 *report_data;
  95. int report_size;
  96. struct rmi_f54_reports standard_report[2];
  97. bool is_busy;
  98. struct mutex status_mutex;
  99. struct mutex data_mutex;
  100. struct workqueue_struct *workqueue;
  101. struct delayed_work work;
  102. unsigned long timeout;
  103. struct completion cmd_done;
  104. /* V4L2 support */
  105. struct v4l2_device v4l2;
  106. struct v4l2_pix_format format;
  107. struct video_device vdev;
  108. struct vb2_queue queue;
  109. struct mutex lock;
  110. int input;
  111. enum rmi_f54_report_type inputs[F54_MAX_REPORT_TYPE];
  112. };
  113. /*
  114. * Basic checks on report_type to ensure we write a valid type
  115. * to the sensor.
  116. */
  117. static bool is_f54_report_type_valid(struct f54_data *f54,
  118. enum rmi_f54_report_type reptype)
  119. {
  120. switch (reptype) {
  121. case F54_8BIT_IMAGE:
  122. return f54->capabilities & F54_CAP_IMAGE8;
  123. case F54_16BIT_IMAGE:
  124. case F54_RAW_16BIT_IMAGE:
  125. return f54->capabilities & F54_CAP_IMAGE16;
  126. case F54_TRUE_BASELINE:
  127. return f54->capabilities & F54_CAP_IMAGE16;
  128. case F54_FULL_RAW_CAP:
  129. case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
  130. return true;
  131. default:
  132. return false;
  133. }
  134. }
  135. static enum rmi_f54_report_type rmi_f54_get_reptype(struct f54_data *f54,
  136. unsigned int i)
  137. {
  138. if (i >= F54_MAX_REPORT_TYPE)
  139. return F54_REPORT_NONE;
  140. return f54->inputs[i];
  141. }
  142. static void rmi_f54_create_input_map(struct f54_data *f54)
  143. {
  144. int i = 0;
  145. enum rmi_f54_report_type reptype;
  146. for (reptype = 1; reptype < F54_MAX_REPORT_TYPE; reptype++) {
  147. if (!is_f54_report_type_valid(f54, reptype))
  148. continue;
  149. f54->inputs[i++] = reptype;
  150. }
  151. /* Remaining values are zero via kzalloc */
  152. }
  153. static int rmi_f54_request_report(struct rmi_function *fn, u8 report_type)
  154. {
  155. struct f54_data *f54 = dev_get_drvdata(&fn->dev);
  156. struct rmi_device *rmi_dev = fn->rmi_dev;
  157. int error;
  158. /* Write Report Type into F54_AD_Data0 */
  159. if (f54->report_type != report_type) {
  160. error = rmi_write(rmi_dev, f54->fn->fd.data_base_addr,
  161. report_type);
  162. if (error)
  163. return error;
  164. f54->report_type = report_type;
  165. }
  166. /*
  167. * Small delay after disabling interrupts to avoid race condition
  168. * in firmare. This value is a bit higher than absolutely necessary.
  169. * Should be removed once issue is resolved in firmware.
  170. */
  171. usleep_range(2000, 3000);
  172. mutex_lock(&f54->data_mutex);
  173. error = rmi_write(rmi_dev, fn->fd.command_base_addr, F54_GET_REPORT);
  174. if (error < 0)
  175. goto unlock;
  176. init_completion(&f54->cmd_done);
  177. f54->is_busy = 1;
  178. f54->timeout = jiffies + msecs_to_jiffies(100);
  179. queue_delayed_work(f54->workqueue, &f54->work, 0);
  180. unlock:
  181. mutex_unlock(&f54->data_mutex);
  182. return error;
  183. }
  184. static size_t rmi_f54_get_report_size(struct f54_data *f54)
  185. {
  186. u8 rx = f54->num_rx_electrodes ? : f54->num_rx_electrodes;
  187. u8 tx = f54->num_tx_electrodes ? : f54->num_tx_electrodes;
  188. size_t size;
  189. switch (rmi_f54_get_reptype(f54, f54->input)) {
  190. case F54_8BIT_IMAGE:
  191. size = rx * tx;
  192. break;
  193. case F54_16BIT_IMAGE:
  194. case F54_RAW_16BIT_IMAGE:
  195. case F54_TRUE_BASELINE:
  196. case F54_FULL_RAW_CAP:
  197. case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
  198. size = sizeof(u16) * rx * tx;
  199. break;
  200. default:
  201. size = 0;
  202. }
  203. return size;
  204. }
  205. static int rmi_f54_get_pixel_fmt(enum rmi_f54_report_type reptype, u32 *pixfmt)
  206. {
  207. int ret = 0;
  208. switch (reptype) {
  209. case F54_8BIT_IMAGE:
  210. *pixfmt = V4L2_TCH_FMT_DELTA_TD08;
  211. break;
  212. case F54_16BIT_IMAGE:
  213. *pixfmt = V4L2_TCH_FMT_DELTA_TD16;
  214. break;
  215. case F54_RAW_16BIT_IMAGE:
  216. case F54_TRUE_BASELINE:
  217. case F54_FULL_RAW_CAP:
  218. case F54_FULL_RAW_CAP_RX_OFFSET_REMOVED:
  219. *pixfmt = V4L2_TCH_FMT_TU16;
  220. break;
  221. case F54_REPORT_NONE:
  222. case F54_MAX_REPORT_TYPE:
  223. ret = -EINVAL;
  224. break;
  225. }
  226. return ret;
  227. }
  228. static const struct v4l2_file_operations rmi_f54_video_fops = {
  229. .owner = THIS_MODULE,
  230. .open = v4l2_fh_open,
  231. .release = vb2_fop_release,
  232. .unlocked_ioctl = video_ioctl2,
  233. .read = vb2_fop_read,
  234. .mmap = vb2_fop_mmap,
  235. .poll = vb2_fop_poll,
  236. };
  237. static int rmi_f54_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
  238. unsigned int *nplanes, unsigned int sizes[],
  239. struct device *alloc_devs[])
  240. {
  241. struct f54_data *f54 = q->drv_priv;
  242. if (*nplanes)
  243. return sizes[0] < rmi_f54_get_report_size(f54) ? -EINVAL : 0;
  244. *nplanes = 1;
  245. sizes[0] = rmi_f54_get_report_size(f54);
  246. return 0;
  247. }
  248. static void rmi_f54_buffer_queue(struct vb2_buffer *vb)
  249. {
  250. struct f54_data *f54 = vb2_get_drv_priv(vb->vb2_queue);
  251. u16 *ptr;
  252. enum vb2_buffer_state state;
  253. enum rmi_f54_report_type reptype;
  254. int ret;
  255. mutex_lock(&f54->status_mutex);
  256. reptype = rmi_f54_get_reptype(f54, f54->input);
  257. if (reptype == F54_REPORT_NONE) {
  258. state = VB2_BUF_STATE_ERROR;
  259. goto done;
  260. }
  261. if (f54->is_busy) {
  262. state = VB2_BUF_STATE_ERROR;
  263. goto done;
  264. }
  265. ret = rmi_f54_request_report(f54->fn, reptype);
  266. if (ret) {
  267. dev_err(&f54->fn->dev, "Error requesting F54 report\n");
  268. state = VB2_BUF_STATE_ERROR;
  269. goto done;
  270. }
  271. /* get frame data */
  272. mutex_lock(&f54->data_mutex);
  273. while (f54->is_busy) {
  274. mutex_unlock(&f54->data_mutex);
  275. if (!wait_for_completion_timeout(&f54->cmd_done,
  276. msecs_to_jiffies(1000))) {
  277. dev_err(&f54->fn->dev, "Timed out\n");
  278. state = VB2_BUF_STATE_ERROR;
  279. goto done;
  280. }
  281. mutex_lock(&f54->data_mutex);
  282. }
  283. ptr = vb2_plane_vaddr(vb, 0);
  284. if (!ptr) {
  285. dev_err(&f54->fn->dev, "Error acquiring frame ptr\n");
  286. state = VB2_BUF_STATE_ERROR;
  287. goto data_done;
  288. }
  289. memcpy(ptr, f54->report_data, f54->report_size);
  290. vb2_set_plane_payload(vb, 0, rmi_f54_get_report_size(f54));
  291. state = VB2_BUF_STATE_DONE;
  292. data_done:
  293. mutex_unlock(&f54->data_mutex);
  294. done:
  295. vb2_buffer_done(vb, state);
  296. mutex_unlock(&f54->status_mutex);
  297. }
  298. /* V4L2 structures */
  299. static const struct vb2_ops rmi_f54_queue_ops = {
  300. .queue_setup = rmi_f54_queue_setup,
  301. .buf_queue = rmi_f54_buffer_queue,
  302. .wait_prepare = vb2_ops_wait_prepare,
  303. .wait_finish = vb2_ops_wait_finish,
  304. };
  305. static const struct vb2_queue rmi_f54_queue = {
  306. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  307. .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
  308. .buf_struct_size = sizeof(struct vb2_buffer),
  309. .ops = &rmi_f54_queue_ops,
  310. .mem_ops = &vb2_vmalloc_memops,
  311. .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
  312. .min_buffers_needed = 1,
  313. };
  314. static int rmi_f54_vidioc_querycap(struct file *file, void *priv,
  315. struct v4l2_capability *cap)
  316. {
  317. struct f54_data *f54 = video_drvdata(file);
  318. strlcpy(cap->driver, F54_NAME, sizeof(cap->driver));
  319. strlcpy(cap->card, SYNAPTICS_INPUT_DEVICE_NAME, sizeof(cap->card));
  320. snprintf(cap->bus_info, sizeof(cap->bus_info),
  321. "rmi4:%s", dev_name(&f54->fn->dev));
  322. return 0;
  323. }
  324. static int rmi_f54_vidioc_enum_input(struct file *file, void *priv,
  325. struct v4l2_input *i)
  326. {
  327. struct f54_data *f54 = video_drvdata(file);
  328. enum rmi_f54_report_type reptype;
  329. reptype = rmi_f54_get_reptype(f54, i->index);
  330. if (reptype == F54_REPORT_NONE)
  331. return -EINVAL;
  332. i->type = V4L2_INPUT_TYPE_TOUCH;
  333. strlcpy(i->name, rmi_f54_report_type_names[reptype], sizeof(i->name));
  334. return 0;
  335. }
  336. static int rmi_f54_set_input(struct f54_data *f54, unsigned int i)
  337. {
  338. struct v4l2_pix_format *f = &f54->format;
  339. enum rmi_f54_report_type reptype;
  340. int ret;
  341. reptype = rmi_f54_get_reptype(f54, i);
  342. if (reptype == F54_REPORT_NONE)
  343. return -EINVAL;
  344. ret = rmi_f54_get_pixel_fmt(reptype, &f->pixelformat);
  345. if (ret)
  346. return ret;
  347. f54->input = i;
  348. f->width = f54->num_rx_electrodes;
  349. f->height = f54->num_tx_electrodes;
  350. f->field = V4L2_FIELD_NONE;
  351. f->colorspace = V4L2_COLORSPACE_RAW;
  352. f->bytesperline = f->width * sizeof(u16);
  353. f->sizeimage = f->width * f->height * sizeof(u16);
  354. return 0;
  355. }
  356. static int rmi_f54_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  357. {
  358. return rmi_f54_set_input(video_drvdata(file), i);
  359. }
  360. static int rmi_f54_vidioc_g_input(struct file *file, void *priv,
  361. unsigned int *i)
  362. {
  363. struct f54_data *f54 = video_drvdata(file);
  364. *i = f54->input;
  365. return 0;
  366. }
  367. static int rmi_f54_vidioc_fmt(struct file *file, void *priv,
  368. struct v4l2_format *f)
  369. {
  370. struct f54_data *f54 = video_drvdata(file);
  371. f->fmt.pix = f54->format;
  372. return 0;
  373. }
  374. static int rmi_f54_vidioc_enum_fmt(struct file *file, void *priv,
  375. struct v4l2_fmtdesc *fmt)
  376. {
  377. if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  378. return -EINVAL;
  379. switch (fmt->index) {
  380. case 0:
  381. fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
  382. break;
  383. case 1:
  384. fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD08;
  385. break;
  386. case 2:
  387. fmt->pixelformat = V4L2_TCH_FMT_TU16;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. static int rmi_f54_vidioc_g_parm(struct file *file, void *fh,
  395. struct v4l2_streamparm *a)
  396. {
  397. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  398. return -EINVAL;
  399. a->parm.capture.readbuffers = 1;
  400. a->parm.capture.timeperframe.numerator = 1;
  401. a->parm.capture.timeperframe.denominator = 10;
  402. return 0;
  403. }
  404. static const struct v4l2_ioctl_ops rmi_f54_video_ioctl_ops = {
  405. .vidioc_querycap = rmi_f54_vidioc_querycap,
  406. .vidioc_enum_fmt_vid_cap = rmi_f54_vidioc_enum_fmt,
  407. .vidioc_s_fmt_vid_cap = rmi_f54_vidioc_fmt,
  408. .vidioc_g_fmt_vid_cap = rmi_f54_vidioc_fmt,
  409. .vidioc_try_fmt_vid_cap = rmi_f54_vidioc_fmt,
  410. .vidioc_g_parm = rmi_f54_vidioc_g_parm,
  411. .vidioc_enum_input = rmi_f54_vidioc_enum_input,
  412. .vidioc_g_input = rmi_f54_vidioc_g_input,
  413. .vidioc_s_input = rmi_f54_vidioc_s_input,
  414. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  415. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  416. .vidioc_querybuf = vb2_ioctl_querybuf,
  417. .vidioc_qbuf = vb2_ioctl_qbuf,
  418. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  419. .vidioc_expbuf = vb2_ioctl_expbuf,
  420. .vidioc_streamon = vb2_ioctl_streamon,
  421. .vidioc_streamoff = vb2_ioctl_streamoff,
  422. };
  423. static const struct video_device rmi_f54_video_device = {
  424. .name = "Synaptics RMI4",
  425. .fops = &rmi_f54_video_fops,
  426. .ioctl_ops = &rmi_f54_video_ioctl_ops,
  427. .release = video_device_release_empty,
  428. .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
  429. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
  430. };
  431. static void rmi_f54_work(struct work_struct *work)
  432. {
  433. struct f54_data *f54 = container_of(work, struct f54_data, work.work);
  434. struct rmi_function *fn = f54->fn;
  435. u8 fifo[2];
  436. struct rmi_f54_reports *report;
  437. int report_size;
  438. u8 command;
  439. u8 *data;
  440. int error;
  441. data = f54->report_data;
  442. report_size = rmi_f54_get_report_size(f54);
  443. if (report_size == 0) {
  444. dev_err(&fn->dev, "Bad report size, report type=%d\n",
  445. f54->report_type);
  446. error = -EINVAL;
  447. goto error; /* retry won't help */
  448. }
  449. f54->standard_report[0].size = report_size;
  450. report = f54->standard_report;
  451. mutex_lock(&f54->data_mutex);
  452. /*
  453. * Need to check if command has completed.
  454. * If not try again later.
  455. */
  456. error = rmi_read(fn->rmi_dev, f54->fn->fd.command_base_addr,
  457. &command);
  458. if (error) {
  459. dev_err(&fn->dev, "Failed to read back command\n");
  460. goto error;
  461. }
  462. if (command & F54_GET_REPORT) {
  463. if (time_after(jiffies, f54->timeout)) {
  464. dev_err(&fn->dev, "Get report command timed out\n");
  465. error = -ETIMEDOUT;
  466. }
  467. report_size = 0;
  468. goto error;
  469. }
  470. rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Get report command completed, reading data\n");
  471. report_size = 0;
  472. for (; report->size; report++) {
  473. fifo[0] = report->start & 0xff;
  474. fifo[1] = (report->start >> 8) & 0xff;
  475. error = rmi_write_block(fn->rmi_dev,
  476. fn->fd.data_base_addr + F54_FIFO_OFFSET,
  477. fifo, sizeof(fifo));
  478. if (error) {
  479. dev_err(&fn->dev, "Failed to set fifo start offset\n");
  480. goto abort;
  481. }
  482. error = rmi_read_block(fn->rmi_dev, fn->fd.data_base_addr +
  483. F54_REPORT_DATA_OFFSET, data,
  484. report->size);
  485. if (error) {
  486. dev_err(&fn->dev, "%s: read [%d bytes] returned %d\n",
  487. __func__, report->size, error);
  488. goto abort;
  489. }
  490. data += report->size;
  491. report_size += report->size;
  492. }
  493. abort:
  494. f54->report_size = error ? 0 : report_size;
  495. error:
  496. if (error)
  497. report_size = 0;
  498. if (report_size == 0 && !error) {
  499. queue_delayed_work(f54->workqueue, &f54->work,
  500. msecs_to_jiffies(1));
  501. } else {
  502. f54->is_busy = false;
  503. complete(&f54->cmd_done);
  504. }
  505. mutex_unlock(&f54->data_mutex);
  506. }
  507. static int rmi_f54_attention(struct rmi_function *fn, unsigned long *irqbits)
  508. {
  509. return 0;
  510. }
  511. static int rmi_f54_config(struct rmi_function *fn)
  512. {
  513. struct rmi_driver *drv = fn->rmi_dev->driver;
  514. drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
  515. return 0;
  516. }
  517. static int rmi_f54_detect(struct rmi_function *fn)
  518. {
  519. int error;
  520. struct f54_data *f54;
  521. f54 = dev_get_drvdata(&fn->dev);
  522. error = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
  523. &f54->qry, sizeof(f54->qry));
  524. if (error) {
  525. dev_err(&fn->dev, "%s: Failed to query F54 properties\n",
  526. __func__);
  527. return error;
  528. }
  529. f54->num_rx_electrodes = f54->qry[0];
  530. f54->num_tx_electrodes = f54->qry[1];
  531. f54->capabilities = f54->qry[2];
  532. f54->clock_rate = f54->qry[3] | (f54->qry[4] << 8);
  533. f54->family = f54->qry[5];
  534. rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_rx_electrodes: %d\n",
  535. f54->num_rx_electrodes);
  536. rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 num_tx_electrodes: %d\n",
  537. f54->num_tx_electrodes);
  538. rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 capabilities: 0x%x\n",
  539. f54->capabilities);
  540. rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 clock rate: 0x%x\n",
  541. f54->clock_rate);
  542. rmi_dbg(RMI_DEBUG_FN, &fn->dev, "F54 family: 0x%x\n",
  543. f54->family);
  544. f54->is_busy = false;
  545. return 0;
  546. }
  547. static int rmi_f54_probe(struct rmi_function *fn)
  548. {
  549. struct f54_data *f54;
  550. int ret;
  551. u8 rx, tx;
  552. f54 = devm_kzalloc(&fn->dev, sizeof(struct f54_data), GFP_KERNEL);
  553. if (!f54)
  554. return -ENOMEM;
  555. f54->fn = fn;
  556. dev_set_drvdata(&fn->dev, f54);
  557. ret = rmi_f54_detect(fn);
  558. if (ret)
  559. return ret;
  560. mutex_init(&f54->data_mutex);
  561. mutex_init(&f54->status_mutex);
  562. rx = f54->num_rx_electrodes;
  563. tx = f54->num_tx_electrodes;
  564. f54->report_data = devm_kzalloc(&fn->dev,
  565. sizeof(u16) * tx * rx,
  566. GFP_KERNEL);
  567. if (f54->report_data == NULL)
  568. return -ENOMEM;
  569. INIT_DELAYED_WORK(&f54->work, rmi_f54_work);
  570. f54->workqueue = create_singlethread_workqueue("rmi4-poller");
  571. if (!f54->workqueue)
  572. return -ENOMEM;
  573. rmi_f54_create_input_map(f54);
  574. /* register video device */
  575. strlcpy(f54->v4l2.name, F54_NAME, sizeof(f54->v4l2.name));
  576. ret = v4l2_device_register(&fn->dev, &f54->v4l2);
  577. if (ret) {
  578. dev_err(&fn->dev, "Unable to register video dev.\n");
  579. goto remove_wq;
  580. }
  581. /* initialize the queue */
  582. mutex_init(&f54->lock);
  583. f54->queue = rmi_f54_queue;
  584. f54->queue.drv_priv = f54;
  585. f54->queue.lock = &f54->lock;
  586. f54->queue.dev = &fn->dev;
  587. ret = vb2_queue_init(&f54->queue);
  588. if (ret)
  589. goto remove_v4l2;
  590. f54->vdev = rmi_f54_video_device;
  591. f54->vdev.v4l2_dev = &f54->v4l2;
  592. f54->vdev.lock = &f54->lock;
  593. f54->vdev.vfl_dir = VFL_DIR_RX;
  594. f54->vdev.queue = &f54->queue;
  595. video_set_drvdata(&f54->vdev, f54);
  596. ret = video_register_device(&f54->vdev, VFL_TYPE_TOUCH, -1);
  597. if (ret) {
  598. dev_err(&fn->dev, "Unable to register video subdevice.");
  599. goto remove_v4l2;
  600. }
  601. return 0;
  602. remove_v4l2:
  603. v4l2_device_unregister(&f54->v4l2);
  604. remove_wq:
  605. cancel_delayed_work_sync(&f54->work);
  606. flush_workqueue(f54->workqueue);
  607. destroy_workqueue(f54->workqueue);
  608. return ret;
  609. }
  610. static void rmi_f54_remove(struct rmi_function *fn)
  611. {
  612. struct f54_data *f54 = dev_get_drvdata(&fn->dev);
  613. video_unregister_device(&f54->vdev);
  614. v4l2_device_unregister(&f54->v4l2);
  615. }
  616. struct rmi_function_handler rmi_f54_handler = {
  617. .driver = {
  618. .name = F54_NAME,
  619. },
  620. .func = 0x54,
  621. .probe = rmi_f54_probe,
  622. .config = rmi_f54_config,
  623. .attention = rmi_f54_attention,
  624. .remove = rmi_f54_remove,
  625. };