rmi_f54.c 18 KB

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