rmi_f54.c 18 KB

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