max11802.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /****************************************************************************
  2. * drivers/input/max11802.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /* References:
  21. * "Low-Power, Ultra-Small Resistive Touch-Screen Controllers
  22. * with I2C/SPI Interface" Maxim IC, Rev 3, 10/2010
  23. */
  24. /****************************************************************************
  25. * Included Files
  26. ****************************************************************************/
  27. #include <nuttx/config.h>
  28. #include <sys/types.h>
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <fcntl.h>
  34. #include <poll.h>
  35. #include <errno.h>
  36. #include <assert.h>
  37. #include <debug.h>
  38. #include <nuttx/irq.h>
  39. #include <nuttx/arch.h>
  40. #include <nuttx/wdog.h>
  41. #include <nuttx/kmalloc.h>
  42. #include <nuttx/wqueue.h>
  43. #include <nuttx/fs/fs.h>
  44. #include <nuttx/spi/spi.h>
  45. #include <nuttx/random.h>
  46. #include <nuttx/input/touchscreen.h>
  47. #include <nuttx/input/max11802.h>
  48. #include "max11802.h"
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. /* This is a value for the threshold that guarantees a big difference on the
  53. * first pendown (but can't overflow).
  54. */
  55. #define INVALID_THRESHOLD 0x1000
  56. /****************************************************************************
  57. * Private Types
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Private Function Prototypes
  61. ****************************************************************************/
  62. /* Low-level SPI helpers */
  63. static void max11802_lock(FAR struct spi_dev_s *spi);
  64. static void max11802_unlock(FAR struct spi_dev_s *spi);
  65. static uint16_t max11802_sendcmd(FAR struct max11802_dev_s *priv,
  66. uint8_t cmd, int *tags);
  67. /* Interrupts and data sampling */
  68. static void max11802_notify(FAR struct max11802_dev_s *priv);
  69. static int max11802_sample(FAR struct max11802_dev_s *priv,
  70. FAR struct max11802_sample_s *sample);
  71. static int max11802_waitsample(FAR struct max11802_dev_s *priv,
  72. FAR struct max11802_sample_s *sample);
  73. static void max11802_worker(FAR void *arg);
  74. static int max11802_interrupt(int irq, FAR void *context, FAR void *arg);
  75. /* Character driver methods */
  76. static int max11802_open(FAR struct file *filep);
  77. static int max11802_close(FAR struct file *filep);
  78. static ssize_t max11802_read(FAR struct file *filep, FAR char *buffer,
  79. size_t len);
  80. static int max11802_ioctl(FAR struct file *filep, int cmd,
  81. unsigned long arg);
  82. static int max11802_poll(FAR struct file *filep, struct pollfd *fds,
  83. bool setup);
  84. /****************************************************************************
  85. * Private Data
  86. ****************************************************************************/
  87. /* This the vtable that supports the character driver interface */
  88. static const struct file_operations max11802_fops =
  89. {
  90. max11802_open, /* open */
  91. max11802_close, /* close */
  92. max11802_read, /* read */
  93. 0, /* write */
  94. 0, /* seek */
  95. max11802_ioctl, /* ioctl */
  96. max11802_poll /* poll */
  97. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  98. , 0 /* unlink */
  99. #endif
  100. };
  101. /* If only a single MAX11802 device is supported, then the driver state
  102. * structure may as well be pre-allocated.
  103. */
  104. #ifndef CONFIG_MAX11802_MULTIPLE
  105. static struct max11802_dev_s g_max11802;
  106. /* Otherwise, we will need to maintain allocated driver instances in a list */
  107. #else
  108. static struct max11802_dev_s *g_max11802list;
  109. #endif
  110. /****************************************************************************
  111. * Private Functions
  112. ****************************************************************************/
  113. /****************************************************************************
  114. * Name: max11802_lock
  115. *
  116. * Description:
  117. * Lock the SPI bus and re-configure as necessary. This function must be
  118. * to assure: (1) exclusive access to the SPI bus, and (2) to assure that
  119. * the shared bus is properly configured for the touchscreen controller.
  120. *
  121. * Input Parameters:
  122. * spi - Reference to the SPI driver structure
  123. *
  124. * Returned Value:
  125. * None
  126. *
  127. * Assumptions:
  128. *
  129. ****************************************************************************/
  130. static void max11802_lock(FAR struct spi_dev_s *spi)
  131. {
  132. /* Lock the SPI bus because there are multiple devices competing for the
  133. * SPI bus
  134. */
  135. SPI_LOCK(spi, true);
  136. /* We have the lock. Now make sure that the SPI bus is configured for the
  137. * MAX11802 (it might have gotten configured for a different device while
  138. * unlocked)
  139. */
  140. SPI_SETMODE(spi, CONFIG_MAX11802_SPIMODE);
  141. SPI_SETBITS(spi, 8);
  142. SPI_HWFEATURES(spi, 0);
  143. SPI_SETFREQUENCY(spi, CONFIG_MAX11802_FREQUENCY);
  144. }
  145. /****************************************************************************
  146. * Name: max11802_unlock
  147. *
  148. * Description:
  149. * Un-lock the SPI bus after each transfer, possibly losing the current
  150. * configuration if we are sharing the SPI bus with other devices
  151. *
  152. * Input Parameters:
  153. * spi - Reference to the SPI driver structure
  154. *
  155. * Returned Value:
  156. * None
  157. *
  158. * Assumptions:
  159. *
  160. ****************************************************************************/
  161. static void max11802_unlock(FAR struct spi_dev_s *spi)
  162. {
  163. /* Relinquish the SPI bus. */
  164. SPI_LOCK(spi, false);
  165. }
  166. /****************************************************************************
  167. * Name: max11802_sendcmd
  168. ****************************************************************************/
  169. static uint16_t max11802_sendcmd(FAR struct max11802_dev_s *priv,
  170. uint8_t cmd, int *tags)
  171. {
  172. uint8_t buffer[2];
  173. uint16_t result;
  174. /* Select the MAX11802 */
  175. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  176. /* Send the command */
  177. SPI_SEND(priv->spi, cmd);
  178. /* Read the data */
  179. SPI_RECVBLOCK(priv->spi, buffer, 2);
  180. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  181. result = ((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1];
  182. *tags = result & 0xf;
  183. result >>= 4; /* Get rid of tags */
  184. iinfo("cmd:%02x response:%04x\n", cmd, result);
  185. return result;
  186. }
  187. /****************************************************************************
  188. * Name: max11802_notify
  189. ****************************************************************************/
  190. static void max11802_notify(FAR struct max11802_dev_s *priv)
  191. {
  192. int i;
  193. /* If there are threads waiting on poll() for MAX11802 data to become
  194. * available, then wake them up now. NOTE: we wake up all waiting
  195. * threads because we do not know that they are going to do. If they
  196. * all try to read the data, then some make end up blocking after all.
  197. */
  198. for (i = 0; i < CONFIG_MAX11802_NPOLLWAITERS; i++)
  199. {
  200. struct pollfd *fds = priv->fds[i];
  201. if (fds)
  202. {
  203. fds->revents |= POLLIN;
  204. iinfo("Report events: %02x\n", fds->revents);
  205. nxsem_post(fds->sem);
  206. }
  207. }
  208. /* If there are threads waiting for read data, then signal one of them
  209. * that the read data is available.
  210. */
  211. if (priv->nwaiters > 0)
  212. {
  213. /* After posting this semaphore, we need to exit because the sample
  214. * is no longer available.
  215. */
  216. nxsem_post(&priv->waitsem);
  217. }
  218. }
  219. /****************************************************************************
  220. * Name: max11802_sample
  221. ****************************************************************************/
  222. static int max11802_sample(FAR struct max11802_dev_s *priv,
  223. FAR struct max11802_sample_s *sample)
  224. {
  225. irqstate_t flags;
  226. int ret = -EAGAIN;
  227. /* Interrupts must be disabled when this is called to (1) prevent posting
  228. * of semaphores from interrupt handlers, and (2) to prevent sampled data
  229. * from changing until it has been reported.
  230. */
  231. flags = enter_critical_section();
  232. /* Is there new MAX11802 sample data available? */
  233. if (priv->penchange)
  234. {
  235. /* Yes.. the state has changed in some way. Return a copy of the
  236. * sampled data.
  237. */
  238. memcpy(sample, &priv->sample, sizeof(struct max11802_sample_s));
  239. /* Now manage state transitions */
  240. if (sample->contact == CONTACT_UP)
  241. {
  242. /* Next.. no contact. Increment the ID so that next contact ID
  243. * will be unique. X/Y positions are no longer valid.
  244. */
  245. priv->sample.contact = CONTACT_NONE;
  246. priv->sample.valid = false;
  247. priv->id++;
  248. }
  249. else if (sample->contact == CONTACT_DOWN)
  250. {
  251. /* First report -- next report will be a movement */
  252. priv->sample.contact = CONTACT_MOVE;
  253. }
  254. priv->penchange = false;
  255. ret = OK;
  256. }
  257. leave_critical_section(flags);
  258. return ret;
  259. }
  260. /****************************************************************************
  261. * Name: max11802_waitsample
  262. ****************************************************************************/
  263. static int max11802_waitsample(FAR struct max11802_dev_s *priv,
  264. FAR struct max11802_sample_s *sample)
  265. {
  266. irqstate_t flags;
  267. int ret;
  268. /* Interrupts must be disabled when this is called to (1) prevent posting
  269. * of semaphores from interrupt handlers, and (2) to prevent sampled data
  270. * from changing until it has been reported.
  271. *
  272. * In addition, we will also disable pre-emption to prevent other threads
  273. * from getting control while we muck with the semaphores.
  274. */
  275. sched_lock();
  276. flags = enter_critical_section();
  277. /* Now release the semaphore that manages mutually exclusive access to
  278. * the device structure. This may cause other tasks to become ready to
  279. * run, but they cannot run yet because pre-emption is disabled.
  280. */
  281. nxsem_post(&priv->devsem);
  282. /* Try to get the a sample... if we cannot, then wait on the semaphore
  283. * that is posted when new sample data is available.
  284. */
  285. while (max11802_sample(priv, sample) < 0)
  286. {
  287. /* Wait for a change in the MAX11802 state */
  288. iinfo("Waiting..\n");
  289. priv->nwaiters++;
  290. ret = nxsem_wait(&priv->waitsem);
  291. priv->nwaiters--;
  292. if (ret < 0)
  293. {
  294. ierr("ERROR: nxsem_wait: %d\n", ret);
  295. goto errout;
  296. }
  297. }
  298. iinfo("Sampled\n");
  299. /* Re-acquire the semaphore that manages mutually exclusive access to
  300. * the device structure. We may have to wait here. But we have our
  301. * sample. Interrupts and pre-emption will be re-enabled while we wait.
  302. */
  303. ret = nxsem_wait(&priv->devsem);
  304. errout:
  305. /* Then re-enable interrupts. We might get interrupt here and there
  306. * could be a new sample. But no new threads will run because we still
  307. * have pre-emption disabled.
  308. */
  309. leave_critical_section(flags);
  310. /* Restore pre-emption. We might get suspended here but that is okay
  311. * because we already have our sample. Note: this means that if there
  312. * were two threads reading from the MAX11802 for some reason, the data
  313. * might be read out of order.
  314. */
  315. sched_unlock();
  316. return ret;
  317. }
  318. /****************************************************************************
  319. * Name: max11802_schedule
  320. ****************************************************************************/
  321. static int max11802_schedule(FAR struct max11802_dev_s *priv)
  322. {
  323. FAR struct max11802_config_s *config;
  324. int ret;
  325. /* Get a pointer the callbacks for convenience (and so the code is not so
  326. * ugly).
  327. */
  328. config = priv->config;
  329. DEBUGASSERT(config != NULL);
  330. /* Disable further interrupts. MAX11802 interrupts will be re-enabled
  331. * after the worker thread executes.
  332. */
  333. config->enable(config, false);
  334. /* Disable the watchdog timer. It will be re-enabled in the worker thread
  335. * while the pen remains down.
  336. */
  337. wd_cancel(&priv->wdog);
  338. /* Transfer processing to the worker thread. Since MAX11802 interrupts are
  339. * disabled while the work is pending, no special action should be required
  340. * to protected the work queue.
  341. */
  342. DEBUGASSERT(priv->work.worker == NULL);
  343. ret = work_queue(HPWORK, &priv->work, max11802_worker, priv, 0);
  344. if (ret != 0)
  345. {
  346. ierr("ERROR: Failed to queue work: %d\n", ret);
  347. }
  348. return OK;
  349. }
  350. /****************************************************************************
  351. * Name: max11802_wdog
  352. ****************************************************************************/
  353. static void max11802_wdog(wdparm_t arg)
  354. {
  355. FAR struct max11802_dev_s *priv =
  356. (FAR struct max11802_dev_s *)arg;
  357. max11802_schedule(priv);
  358. }
  359. /****************************************************************************
  360. * Name: max11802_worker
  361. ****************************************************************************/
  362. static void max11802_worker(FAR void *arg)
  363. {
  364. FAR struct max11802_dev_s *priv = (FAR struct max11802_dev_s *)arg;
  365. FAR struct max11802_config_s *config;
  366. uint16_t x;
  367. uint16_t y;
  368. uint16_t xdiff;
  369. uint16_t ydiff;
  370. bool pendown;
  371. int tags;
  372. int tags2;
  373. DEBUGASSERT(priv != NULL);
  374. /* Get a pointer the callbacks for convenience (and so the code is not so
  375. * ugly).
  376. */
  377. config = priv->config;
  378. DEBUGASSERT(config != NULL);
  379. /* Disable the watchdog timer. This is safe because it is started only
  380. * by this function and this function is serialized on the worker thread.
  381. */
  382. wd_cancel(&priv->wdog);
  383. /* Lock the SPI bus so that we have exclusive access */
  384. max11802_lock(priv->spi);
  385. /* Start coordinate measurement */
  386. max11802_sendcmd(priv, MAX11802_CMD_MEASUREXY, &tags);
  387. /* Get exclusive access to the driver data structure */
  388. do
  389. {
  390. ret = nxsem_wait_uninterruptible(&priv->devsem);
  391. /* This would only fail if something canceled the worker thread?
  392. * That is not expected.
  393. */
  394. DEBUGASSERT(ret == OK || ret == -ECANCELED);
  395. }
  396. while (ret < 0);
  397. /* Check for pen up or down by reading the PENIRQ GPIO. */
  398. pendown = config->pendown(config);
  399. /* Handle the change from pen down to pen up */
  400. if (pendown)
  401. {
  402. iinfo("\nPD\n");
  403. }
  404. else
  405. {
  406. iinfo("\nPU\n");
  407. }
  408. if (!pendown)
  409. {
  410. /* The pen is up.. reset thresholding variables. */
  411. priv->threshx = INVALID_THRESHOLD;
  412. priv->threshy = INVALID_THRESHOLD;
  413. /* Ignore the interrupt if the pen was already up (CONTACT_NONE ==
  414. * pen up and already reported; CONTACT_UP == pen up, but not
  415. * reported).
  416. */
  417. iinfo("\nPC%d\n", priv->sample.contact);
  418. if (priv->sample.contact == CONTACT_NONE ||
  419. priv->sample.contact == CONTACT_UP)
  420. {
  421. goto ignored;
  422. }
  423. /* The pen is up. NOTE: We know from a previous test, that this is a
  424. * loss of contact condition. This will be changed to CONTACT_NONE
  425. * after the loss of contact is sampled.
  426. */
  427. priv->sample.contact = CONTACT_UP;
  428. }
  429. /* It is a pen down event. If the last loss-of-contact event has not been
  430. * processed yet, then we have to ignore the pen down event (or else it
  431. * will look like a drag event)
  432. */
  433. else if (priv->sample.contact == CONTACT_UP)
  434. {
  435. /* If we have not yet processed the last pen up event, then we
  436. * cannot handle this pen down event. We will have to discard it.
  437. * That should be okay because we will set the timer to sample
  438. * again later.
  439. */
  440. iinfo("Previous pen up event still in buffer\n");
  441. max11802_notify(priv);
  442. wd_start(&priv->wdog, MAX11802_WDOG_DELAY,
  443. max11802_wdog, (wdparm_t)priv);
  444. goto ignored;
  445. }
  446. else
  447. {
  448. /* Wait for data ready
  449. *
  450. * Note: MAX11802 signals the readiness of the results using
  451. * the lowest 4 bits of the result. However these are the
  452. * last bits to be read out of the device. It appears that
  453. * the hardware value can change in the middle of the readout,
  454. * causing the upper bits to be still invalid even though lower
  455. * bits indicate valid result.
  456. *
  457. * We work around this by reading the registers once more after
  458. * the tags indicate they are ready.
  459. */
  460. int readycount = 0;
  461. do
  462. {
  463. #ifdef CONFIG_MAX11802_SWAPXY
  464. x = max11802_sendcmd(priv, MAX11802_CMD_YPOSITION, &tags);
  465. y = max11802_sendcmd(priv, MAX11802_CMD_XPOSITION, &tags2);
  466. #else
  467. x = max11802_sendcmd(priv, MAX11802_CMD_XPOSITION, &tags);
  468. y = max11802_sendcmd(priv, MAX11802_CMD_YPOSITION, &tags2);
  469. #endif
  470. if (tags != 0xf && tags2 != 0xf)
  471. {
  472. readycount++;
  473. }
  474. }
  475. while (readycount < 2);
  476. add_ui_randomness((x << 16) | y);
  477. /* Continue to sample the position while the pen is down */
  478. wd_start(&priv->wdog, MAX11802_WDOG_DELAY,
  479. max11802_wdog, (wdparm_t)priv);
  480. /* Check if data is valid */
  481. if ((tags & 0x03) != 0)
  482. {
  483. iinfo("Touch ended before measurement\n");
  484. goto ignored;
  485. }
  486. /* Perform a thresholding operation so that the results will be more
  487. * stable. If the difference from the last sample is small, then
  488. * ignore the event.
  489. *
  490. * REVISIT: Should a large change in pressure also generate a event?
  491. */
  492. xdiff = x > priv->threshx ? (x - priv->threshx) : (priv->threshx - x);
  493. ydiff = y > priv->threshy ? (y - priv->threshy) : (priv->threshy - y);
  494. /* Check the thresholds. Bail if there is no significant
  495. * difference.
  496. */
  497. if (xdiff < CONFIG_MAX11802_THRESHX && ydiff < CONFIG_MAX11802_THRESHY)
  498. {
  499. /* Little or no change in either direction ... don't report
  500. * anything.
  501. */
  502. goto ignored;
  503. }
  504. /* When we see a big difference, snap to the new x/y thresholds */
  505. priv->threshx = x;
  506. priv->threshy = y;
  507. /* Update the x/y position in the sample data */
  508. priv->sample.x = priv->threshx;
  509. priv->sample.y = priv->threshy;
  510. /* The X/Y positional data is now valid */
  511. priv->sample.valid = true;
  512. /* If this is the first (acknowledged) pen down report, then report
  513. * this as the first contact. If contact == CONTACT_DOWN, it will be
  514. * set to set to CONTACT_MOVE after the contact is first sampled.
  515. */
  516. if (priv->sample.contact != CONTACT_MOVE)
  517. {
  518. /* First contact */
  519. priv->sample.contact = CONTACT_DOWN;
  520. }
  521. }
  522. /* Indicate the availability of new sample data for this ID */
  523. priv->sample.id = priv->id;
  524. priv->penchange = true;
  525. /* Notify any waiters that new MAX11802 data is available */
  526. max11802_notify(priv);
  527. ignored:
  528. config->enable(config, true);
  529. /* Release our lock on the state structure and unlock the SPI bus */
  530. nxsem_post(&priv->devsem);
  531. max11802_unlock(priv->spi);
  532. }
  533. /****************************************************************************
  534. * Name: max11802_interrupt
  535. ****************************************************************************/
  536. static int max11802_interrupt(int irq, FAR void *context, FAR void *arg)
  537. {
  538. FAR struct max11802_dev_s *priv;
  539. FAR struct max11802_config_s *config;
  540. int ret;
  541. /* Which MAX11802 device caused the interrupt? */
  542. #ifndef CONFIG_MAX11802_MULTIPLE
  543. priv = &g_max11802;
  544. #else
  545. for (priv = g_max11802list;
  546. priv && priv->configs->irq != irq;
  547. priv = priv->flink);
  548. DEBUGASSERT(priv != NULL);
  549. #endif
  550. /* Get a pointer the callbacks for convenience (and so the code is not so
  551. * ugly).
  552. */
  553. config = priv->config;
  554. DEBUGASSERT(config != NULL);
  555. /* Schedule sampling to occur on the worker thread */
  556. ret = max11802_schedule(priv);
  557. /* Clear any pending interrupts and return success */
  558. config->clear(config);
  559. return ret;
  560. }
  561. /****************************************************************************
  562. * Name: max11802_open
  563. ****************************************************************************/
  564. static int max11802_open(FAR struct file *filep)
  565. {
  566. #ifdef CONFIG_MAX11802_REFCNT
  567. FAR struct inode *inode;
  568. FAR struct max11802_dev_s *priv;
  569. uint8_t tmp;
  570. int ret;
  571. iinfo("Opening\n");
  572. DEBUGASSERT(filep);
  573. inode = filep->f_inode;
  574. DEBUGASSERT(inode && inode->i_private);
  575. priv = (FAR struct max11802_dev_s *)inode->i_private;
  576. /* Get exclusive access to the driver data structure */
  577. ret = nxsem_wait(&priv->devsem);
  578. if (ret < 0)
  579. {
  580. return ret;
  581. }
  582. /* Increment the reference count */
  583. tmp = priv->crefs + 1;
  584. if (tmp == 0)
  585. {
  586. /* More than 255 opens; uint8_t overflows to zero */
  587. ret = -EMFILE;
  588. goto errout_with_sem;
  589. }
  590. /* When the reference increments to 1, this is the first open event
  591. * on the driver.. and an opportunity to do any one-time initialization.
  592. */
  593. /* Save the new open count on success */
  594. priv->crefs = tmp;
  595. errout_with_sem:
  596. nxsem_post(&priv->devsem);
  597. return ret;
  598. #else
  599. iinfo("Opening\n");
  600. return OK;
  601. #endif
  602. }
  603. /****************************************************************************
  604. * Name: max11802_close
  605. ****************************************************************************/
  606. static int max11802_close(FAR struct file *filep)
  607. {
  608. #ifdef CONFIG_MAX11802_REFCNT
  609. FAR struct inode *inode;
  610. FAR struct max11802_dev_s *priv;
  611. int ret;
  612. iinfo("Closing\n");
  613. DEBUGASSERT(filep);
  614. inode = filep->f_inode;
  615. DEBUGASSERT(inode && inode->i_private);
  616. priv = (FAR struct max11802_dev_s *)inode->i_private;
  617. /* Get exclusive access to the driver data structure */
  618. ret = nxsem_wait(&priv->devsem);
  619. if (ret < 0)
  620. {
  621. return ret;
  622. }
  623. /* Decrement the reference count unless it would decrement a negative
  624. * value. When the count decrements to zero, there are no further
  625. * open references to the driver.
  626. */
  627. if (priv->crefs >= 1)
  628. {
  629. priv->crefs--;
  630. }
  631. nxsem_post(&priv->devsem);
  632. #endif
  633. iinfo("Closing\n");
  634. return OK;
  635. }
  636. /****************************************************************************
  637. * Name: max11802_read
  638. ****************************************************************************/
  639. static ssize_t max11802_read(FAR struct file *filep, FAR char *buffer,
  640. size_t len)
  641. {
  642. FAR struct inode *inode;
  643. FAR struct max11802_dev_s *priv;
  644. FAR struct touch_sample_s *report;
  645. struct max11802_sample_s sample;
  646. int ret;
  647. iinfo("buffer:%p len:%d\n", buffer, len);
  648. DEBUGASSERT(filep);
  649. inode = filep->f_inode;
  650. DEBUGASSERT(inode && inode->i_private);
  651. priv = (FAR struct max11802_dev_s *)inode->i_private;
  652. /* Verify that the caller has provided a buffer large enough to receive
  653. * the touch data.
  654. */
  655. if (len < SIZEOF_TOUCH_SAMPLE_S(1))
  656. {
  657. /* We could provide logic to break up a touch report into segments and
  658. * handle smaller reads... but why?
  659. */
  660. ierr("ERROR: Unsupported read size: %d\n", len);
  661. return -ENOSYS;
  662. }
  663. /* Get exclusive access to the driver data structure */
  664. ret = nxsem_wait(&priv->devsem);
  665. if (ret < 0)
  666. {
  667. ierr("ERROR: nxsem_wait: %d\n", ret);
  668. return ret;
  669. }
  670. /* Try to read sample data. */
  671. ret = max11802_sample(priv, &sample);
  672. if (ret < 0)
  673. {
  674. /* Sample data is not available now. We would ave to wait to get
  675. * receive sample data. If the user has specified the O_NONBLOCK
  676. * option, then just return an error.
  677. */
  678. iinfo("Sample data is not available\n");
  679. if (filep->f_oflags & O_NONBLOCK)
  680. {
  681. ret = -EAGAIN;
  682. goto errout;
  683. }
  684. /* Wait for sample data */
  685. ret = max11802_waitsample(priv, &sample);
  686. if (ret < 0)
  687. {
  688. /* We might have been awakened by a signal */
  689. ierr("ERROR: max11802_waitsample: %d\n", ret);
  690. goto errout;
  691. }
  692. }
  693. /* In any event, we now have sampled MAX11802 data that we can report
  694. * to the caller.
  695. */
  696. report = (FAR struct touch_sample_s *)buffer;
  697. memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1));
  698. report->npoints = 1;
  699. report->point[0].id = sample.id;
  700. report->point[0].x = sample.x;
  701. report->point[0].y = sample.y;
  702. /* Report the appropriate flags */
  703. if (sample.contact == CONTACT_UP)
  704. {
  705. /* Pen is now up. Is the positional data valid? This is important to
  706. * know because the release will be sent to the window based on its
  707. * last positional data.
  708. */
  709. if (sample.valid)
  710. {
  711. report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID |
  712. TOUCH_POS_VALID;
  713. }
  714. else
  715. {
  716. report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
  717. }
  718. }
  719. else if (sample.contact == CONTACT_DOWN)
  720. {
  721. /* First contact */
  722. report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
  723. TOUCH_POS_VALID;
  724. }
  725. else /* if (sample->contact == CONTACT_MOVE) */
  726. {
  727. /* Movement of the same contact */
  728. report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID |
  729. TOUCH_POS_VALID;
  730. }
  731. iinfo(" id: %d\n", report->point[0].id);
  732. iinfo(" flags: %02x\n", report->point[0].flags);
  733. iinfo(" x: %d\n", report->point[0].x);
  734. iinfo(" y: %d\n", report->point[0].y);
  735. ret = SIZEOF_TOUCH_SAMPLE_S(1);
  736. errout:
  737. nxsem_post(&priv->devsem);
  738. iinfo("Returning: %d\n", ret);
  739. return ret;
  740. }
  741. /****************************************************************************
  742. * Name: max11802_ioctl
  743. ****************************************************************************/
  744. static int max11802_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  745. {
  746. FAR struct inode *inode;
  747. FAR struct max11802_dev_s *priv;
  748. int ret;
  749. iinfo("cmd: %d arg: %ld\n", cmd, arg);
  750. DEBUGASSERT(filep);
  751. inode = filep->f_inode;
  752. DEBUGASSERT(inode && inode->i_private);
  753. priv = (FAR struct max11802_dev_s *)inode->i_private;
  754. /* Get exclusive access to the driver data structure */
  755. ret = nxsem_wait(&priv->devsem);
  756. if (ret < 0)
  757. {
  758. return ret;
  759. }
  760. /* Process the IOCTL by command */
  761. switch (cmd)
  762. {
  763. case TSIOC_SETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
  764. {
  765. FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
  766. DEBUGASSERT(priv->config != NULL && ptr != NULL);
  767. priv->config->frequency = SPI_SETFREQUENCY(priv->spi, *ptr);
  768. }
  769. break;
  770. case TSIOC_GETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
  771. {
  772. FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
  773. DEBUGASSERT(priv->config != NULL && ptr != NULL);
  774. *ptr = priv->config->frequency;
  775. }
  776. break;
  777. default:
  778. ret = -ENOTTY;
  779. break;
  780. }
  781. nxsem_post(&priv->devsem);
  782. return ret;
  783. }
  784. /****************************************************************************
  785. * Name: max11802_poll
  786. ****************************************************************************/
  787. static int max11802_poll(FAR struct file *filep, FAR struct pollfd *fds,
  788. bool setup)
  789. {
  790. FAR struct inode *inode;
  791. FAR struct max11802_dev_s *priv;
  792. int ret;
  793. int i;
  794. iinfo("setup: %d\n", (int)setup);
  795. DEBUGASSERT(filep && fds);
  796. inode = filep->f_inode;
  797. DEBUGASSERT(inode && inode->i_private);
  798. priv = (FAR struct max11802_dev_s *)inode->i_private;
  799. /* Are we setting up the poll? Or tearing it down? */
  800. ret = nxsem_wait(&priv->devsem);
  801. if (ret < 0)
  802. {
  803. return ret;
  804. }
  805. if (setup)
  806. {
  807. /* Ignore waits that do not include POLLIN */
  808. if ((fds->events & POLLIN) == 0)
  809. {
  810. ret = -EDEADLK;
  811. goto errout;
  812. }
  813. /* This is a request to set up the poll. Find an available
  814. * slot for the poll structure reference
  815. */
  816. for (i = 0; i < CONFIG_MAX11802_NPOLLWAITERS; i++)
  817. {
  818. /* Find an available slot */
  819. if (!priv->fds[i])
  820. {
  821. /* Bind the poll structure and this slot */
  822. priv->fds[i] = fds;
  823. fds->priv = &priv->fds[i];
  824. break;
  825. }
  826. }
  827. if (i >= CONFIG_MAX11802_NPOLLWAITERS)
  828. {
  829. fds->priv = NULL;
  830. ret = -EBUSY;
  831. goto errout;
  832. }
  833. /* Should we immediately notify on any of the requested events? */
  834. if (priv->penchange)
  835. {
  836. max11802_notify(priv);
  837. }
  838. }
  839. else if (fds->priv)
  840. {
  841. /* This is a request to tear down the poll. */
  842. struct pollfd **slot = (struct pollfd **)fds->priv;
  843. DEBUGASSERT(slot != NULL);
  844. /* Remove all memory of the poll setup */
  845. *slot = NULL;
  846. fds->priv = NULL;
  847. }
  848. errout:
  849. nxsem_post(&priv->devsem);
  850. return ret;
  851. }
  852. /****************************************************************************
  853. * Public Functions
  854. ****************************************************************************/
  855. /****************************************************************************
  856. * Name: max11802_register
  857. *
  858. * Description:
  859. * Configure the MAX11802 to use the provided SPI device instance. This
  860. * will register the driver as /dev/inputN where N is the minor device
  861. * number
  862. *
  863. * Input Parameters:
  864. * dev - An SPI driver instance
  865. * config - Persistent board configuration data
  866. * minor - The input device minor number
  867. *
  868. * Returned Value:
  869. * Zero is returned on success. Otherwise, a negated errno value is
  870. * returned to indicate the nature of the failure.
  871. *
  872. ****************************************************************************/
  873. int max11802_register(FAR struct spi_dev_s *spi,
  874. FAR struct max11802_config_s *config, int minor)
  875. {
  876. FAR struct max11802_dev_s *priv;
  877. char devname[DEV_NAMELEN];
  878. #ifdef CONFIG_MAX11802_MULTIPLE
  879. irqstate_t flags;
  880. #endif
  881. int ret;
  882. iinfo("spi: %p minor: %d\n", spi, minor);
  883. /* Debug-only sanity checks */
  884. DEBUGASSERT(spi != NULL && config != NULL && minor >= 0 && minor < 100);
  885. /* Create and initialize a MAX11802 device driver instance */
  886. #ifndef CONFIG_MAX11802_MULTIPLE
  887. priv = &g_max11802;
  888. #else
  889. priv = (FAR struct max11802_dev_s *)
  890. kmm_malloc(sizeof(struct max11802_dev_s));
  891. if (!priv)
  892. {
  893. ierr("ERROR: kmm_malloc(%d) failed\n", sizeof(struct max11802_dev_s));
  894. return -ENOMEM;
  895. }
  896. #endif
  897. /* Initialize the MAX11802 device driver instance */
  898. memset(priv, 0, sizeof(struct max11802_dev_s));
  899. priv->spi = spi; /* Save the SPI device handle */
  900. priv->config = config; /* Save the board configuration */
  901. priv->threshx = INVALID_THRESHOLD; /* Initialize thresholding logic */
  902. priv->threshy = INVALID_THRESHOLD; /* Initialize thresholding logic */
  903. /* Initialize semaphores */
  904. nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */
  905. nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */
  906. /* The pen event semaphore is used for signaling and, hence, should not
  907. * have priority inheritance enabled.
  908. */
  909. nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE);
  910. /* Make sure that interrupts are disabled */
  911. config->clear(config);
  912. config->enable(config, false);
  913. /* Attach the interrupt handler */
  914. ret = config->attach(config, max11802_interrupt);
  915. if (ret < 0)
  916. {
  917. ierr("ERROR: Failed to attach interrupt\n");
  918. goto errout_with_priv;
  919. }
  920. iinfo("Mode: %d Bits: 8 Frequency: %d\n",
  921. CONFIG_MAX11802_SPIMODE, CONFIG_MAX11802_FREQUENCY);
  922. /* Lock the SPI bus so that we have exclusive access */
  923. max11802_lock(spi);
  924. /* Configure MAX11802 registers */
  925. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  926. SPI_SEND(priv->spi, MAX11802_CMD_MODE_WR);
  927. SPI_SEND(priv->spi, MAX11802_MODE);
  928. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  929. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  930. SPI_SEND(priv->spi, MAX11802_CMD_AVG_WR);
  931. SPI_SEND(priv->spi, MAX11802_AVG);
  932. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  933. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  934. SPI_SEND(priv->spi, MAX11802_CMD_SAMPLE_WR);
  935. SPI_SEND(priv->spi, MAX11802_SAMPLE);
  936. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  937. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  938. SPI_SEND(priv->spi, MAX11802_CMD_TIMING_WR);
  939. SPI_SEND(priv->spi, MAX11802_TIMING);
  940. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  941. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  942. SPI_SEND(priv->spi, MAX11802_CMD_DELAY_WR);
  943. SPI_SEND(priv->spi, MAX11802_DELAY);
  944. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  945. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  946. SPI_SEND(priv->spi, MAX11802_CMD_PULL_WR);
  947. SPI_SEND(priv->spi, MAX11802_PULL);
  948. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  949. /* Test that the device access was successful. */
  950. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
  951. SPI_SEND(priv->spi, MAX11802_CMD_MODE_RD);
  952. ret = SPI_SEND(priv->spi, 0);
  953. SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
  954. /* Unlock the bus */
  955. max11802_unlock(spi);
  956. if (ret != MAX11802_MODE)
  957. {
  958. ierr("ERROR: max11802 mode readback failed: %02x\n", ret);
  959. goto errout_with_priv;
  960. }
  961. /* Register the device as an input device */
  962. snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor);
  963. iinfo("Registering %s\n", devname);
  964. ret = register_driver(devname, &max11802_fops, 0666, priv);
  965. if (ret < 0)
  966. {
  967. ierr("ERROR: register_driver() failed: %d\n", ret);
  968. goto errout_with_priv;
  969. }
  970. /* If multiple MAX11802 devices are supported, then we will need to add
  971. * this new instance to a list of device instances so that it can be
  972. * found by the interrupt handler based on the received IRQ number.
  973. */
  974. #ifdef CONFIG_MAX11802_MULTIPLE
  975. flags = enter_critical_section();
  976. priv->flink = g_max11802list;
  977. g_max11802list = priv;
  978. leave_critical_section(flags);
  979. #endif
  980. /* Schedule work to perform the initial sampling and to set the data
  981. * availability conditions.
  982. */
  983. ret = work_queue(HPWORK, &priv->work, max11802_worker, priv, 0);
  984. if (ret != 0)
  985. {
  986. ierr("ERROR: Failed to queue work: %d\n", ret);
  987. goto errout_with_priv;
  988. }
  989. /* And return success (?) */
  990. return OK;
  991. errout_with_priv:
  992. nxsem_destroy(&priv->devsem);
  993. #ifdef CONFIG_MAX11802_MULTIPLE
  994. kmm_free(priv);
  995. #endif
  996. return ret;
  997. }