mxt.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /****************************************************************************
  2. * drivers/input/mxt.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. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. /* Suppress verbose debug output so that we don't swamp the system */
  25. #ifdef CONFIG_MXT_DISABLE_CONFIG_DEBUG_INFO
  26. # undef CONFIG_DEBUG_INFO
  27. #endif
  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/kmalloc.h>
  40. #include <nuttx/arch.h>
  41. #include <nuttx/fs/fs.h>
  42. #include <nuttx/i2c/i2c_master.h>
  43. #include <nuttx/wqueue.h>
  44. #include <nuttx/random.h>
  45. #include <nuttx/signal.h>
  46. #include <nuttx/semaphore.h>
  47. #include <nuttx/input/touchscreen.h>
  48. #include <nuttx/input/mxt.h>
  49. #include "mxt.h"
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. /* Driver support ***********************************************************/
  54. /* This format is used to construct the /dev/input[n] device driver path. It
  55. * defined here so that it will be used consistently in all places.
  56. */
  57. #define DEV_FORMAT "/dev/input%d"
  58. #define DEV_NAMELEN 16
  59. /* This is a value for the threshold that guarantees a big difference on the
  60. * first pendown (but can't overflow).
  61. */
  62. #define INVALID_POSITION 0x1000
  63. /* Maximum number of retries */
  64. #define MAX_RETRIES 3
  65. /* Get a 16-bit value in little endian order (not necessarily aligned). The
  66. * source data is in little endian order. The host byte order does not
  67. * matter in this case.
  68. */
  69. #define MXT_GETUINT16(p) \
  70. (((uint16_t)(((FAR uint8_t *)(p))[1]) << 8) | \
  71. (uint16_t)(((FAR uint8_t *)(p))[0]))
  72. /****************************************************************************
  73. * Private Types
  74. ****************************************************************************/
  75. /* This enumeration describes the state of one contact.
  76. *
  77. * |
  78. * v
  79. * CONTACT_NONE (1) Touch
  80. * / (1) ^ (3) (2) Release
  81. * v \ (3) Event reported
  82. * CONTACT_NEW CONTACT_LOST
  83. * \ (3) ^ (2)
  84. * v /
  85. * CONTACT_REPORT
  86. * \ (1) ^ (3)
  87. * v /
  88. * CONTACT_MOVE
  89. *
  90. * NOTE: This state transition diagram is simplified. There are a few other
  91. * sneaky transitions to handle unexpected conditions.
  92. */
  93. enum mxt_contact_e
  94. {
  95. CONTACT_NONE = 0, /* No contact */
  96. CONTACT_NEW, /* New contact */
  97. CONTACT_MOVE, /* Same contact, possibly different position */
  98. CONTACT_REPORT, /* Contact reported */
  99. CONTACT_LOST, /* Contact lost */
  100. };
  101. /* This structure describes the results of one MXT sample */
  102. struct mxt_sample_s
  103. {
  104. uint8_t id; /* Sampled touch point ID */
  105. uint8_t contact; /* Contact state (see enum mxt_contact_e) */
  106. bool valid; /* True: x,y,pressure contain valid, sampled data */
  107. uint16_t x; /* Measured X position */
  108. uint16_t y; /* Measured Y position */
  109. uint16_t lastx; /* Last reported X position */
  110. uint16_t lasty; /* Last reported Y position */
  111. uint8_t area; /* Contact area */
  112. uint8_t pressure; /* Contact pressure */
  113. };
  114. /* This 7-bit 'info' data read from the MXT and that describes the
  115. * characteristics of the particular maXTouch chip
  116. */
  117. struct mxt_info_s
  118. {
  119. uint8_t family; /* MXT family ID */
  120. uint8_t variant; /* MXT variant ID */
  121. uint8_t version; /* MXT version number */
  122. uint8_t build; /* MXT build number */
  123. uint8_t xsize; /* Matrix X size */
  124. uint8_t ysize; /* Matrix Y size */
  125. uint8_t nobjects; /* Number of objects */
  126. };
  127. #define MXT_INFO_SIZE 7
  128. /* Describes the state of the MXT driver */
  129. struct mxt_dev_s
  130. {
  131. /* These are the retained references to the I2C device and to the
  132. * lower half configuration data.
  133. */
  134. FAR struct i2c_master_s *i2c;
  135. FAR const struct mxt_lower_s *lower;
  136. /* This is the allocated array of object information */
  137. FAR struct mxt_object_s *objtab;
  138. /* This is an allocated array of sample data, one for each possible touch */
  139. FAR struct mxt_sample_s *sample; /* Last sampled touch point data */
  140. uint8_t nwaiters; /* Number of threads waiting for MXT data */
  141. uint8_t id; /* Current touch point ID */
  142. uint8_t nslots; /* Number of slots */
  143. uint8_t crefs; /* Reference count */
  144. /* Cached parameters from object table */
  145. #ifdef MXT_SUPPORT_T6
  146. uint8_t t6id; /* T6 report ID */
  147. #endif
  148. uint8_t t9idmin; /* T9 touch event report IDs */
  149. uint8_t t9idmax;
  150. #ifdef CONFIG_MXT_BUTTONS
  151. uint8_t t19id; /* T19 button report ID */
  152. #endif
  153. volatile bool event; /* True: An unreported event is buffered */
  154. sem_t devsem; /* Manages exclusive access to this structure */
  155. sem_t waitsem; /* Used to wait for the availability of data */
  156. uint32_t frequency; /* Current I2C frequency */
  157. char phys[64]; /* Device physical location */
  158. struct mxt_info_s info; /* Configuration info read from device */
  159. struct work_s work; /* Supports the interrupt handling "bottom half" */
  160. /* The following is a list if poll structures of threads waiting for
  161. * driver events. The 'struct pollfd' reference for each open is also
  162. * retained in the f_priv field of the 'struct file'.
  163. */
  164. struct pollfd *fds[CONFIG_MXT_NPOLLWAITERS];
  165. };
  166. /****************************************************************************
  167. * Private Function Prototypes
  168. ****************************************************************************/
  169. /* MXT register access */
  170. static int mxt_getreg(FAR struct mxt_dev_s *priv, uint16_t regaddr,
  171. FAR uint8_t *buffer, size_t buflen);
  172. static int mxt_putreg(FAR struct mxt_dev_s *priv, uint16_t regaddr,
  173. FAR const uint8_t *buffer, size_t buflen);
  174. /* MXT object/message access */
  175. static FAR struct mxt_object_s *mxt_object(FAR struct mxt_dev_s *priv,
  176. uint8_t type);
  177. static int mxt_getmessage(FAR struct mxt_dev_s *priv,
  178. FAR struct mxt_msg_s *msg);
  179. static int mxt_putobject(FAR struct mxt_dev_s *priv, uint8_t type,
  180. uint8_t offset, uint8_t value);
  181. #if 0 /* Not used */
  182. static int mxt_getobject(FAR struct mxt_dev_s *priv, uint8_t type,
  183. uint8_t offset, FAR uint8_t *value);
  184. #endif
  185. static int mxt_flushmsgs(FAR struct mxt_dev_s *priv);
  186. /* Poll support */
  187. static void mxt_notify(FAR struct mxt_dev_s *priv);
  188. /* Touch event waiting */
  189. static inline int mxt_checksample(FAR struct mxt_dev_s *priv);
  190. static inline int mxt_waitsample(FAR struct mxt_dev_s *priv);
  191. /* Interrupt handling/position sampling */
  192. #ifdef CONFIG_MXT_BUTTONS
  193. static void mxt_button_event(FAR struct mxt_dev_s *priv,
  194. FAR struct mxt_msg_s *msg);
  195. #endif
  196. static void mxt_touch_event(FAR struct mxt_dev_s *priv,
  197. FAR struct mxt_msg_s *msg, int ndx);
  198. static void mxt_worker(FAR void *arg);
  199. static int mxt_interrupt(FAR const struct mxt_lower_s *lower,
  200. FAR void *context);
  201. /* Character driver methods */
  202. static int mxt_open(FAR struct file *filep);
  203. static int mxt_close(FAR struct file *filep);
  204. static ssize_t mxt_read(FAR struct file *filep, FAR char *buffer,
  205. size_t len);
  206. static int mxt_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  207. static int mxt_poll(FAR struct file *filep, struct pollfd *fds, bool setup);
  208. /* Initialization */
  209. static int mxt_getinfo(struct mxt_dev_s *priv);
  210. static int mxt_getobjtab(FAR struct mxt_dev_s *priv);
  211. static int mxt_hwinitialize(FAR struct mxt_dev_s *priv);
  212. /****************************************************************************
  213. * Private Data
  214. ****************************************************************************/
  215. /* This the vtable that supports the character driver interface */
  216. static const struct file_operations mxt_fops =
  217. {
  218. mxt_open, /* open */
  219. mxt_close, /* close */
  220. mxt_read, /* read */
  221. 0, /* write */
  222. 0, /* seek */
  223. mxt_ioctl, /* ioctl */
  224. mxt_poll /* poll */
  225. };
  226. /****************************************************************************
  227. * Private Functions
  228. ****************************************************************************/
  229. /****************************************************************************
  230. * Name: mxt_getreg
  231. ****************************************************************************/
  232. static int mxt_getreg(FAR struct mxt_dev_s *priv, uint16_t regaddr,
  233. FAR uint8_t *buffer, size_t buflen)
  234. {
  235. struct i2c_msg_s msg[2];
  236. uint8_t addrbuf[2];
  237. int retries;
  238. int ret;
  239. /* Try up to three times to read the register */
  240. for (retries = 1; retries <= MAX_RETRIES; retries++)
  241. {
  242. iinfo("retries=%d regaddr=%04x buflen=%d\n", retries, regaddr, buflen);
  243. /* Set up to write the address */
  244. addrbuf[0] = regaddr & 0xff;
  245. addrbuf[1] = (regaddr >> 8) & 0xff;
  246. msg[0].frequency = priv->frequency;
  247. msg[0].addr = priv->lower->address;
  248. msg[0].flags = 0;
  249. msg[0].buffer = addrbuf;
  250. msg[0].length = 2;
  251. /* Followed by the read data */
  252. msg[1].frequency = priv->frequency;
  253. msg[1].addr = priv->lower->address;
  254. msg[1].flags = I2C_M_READ;
  255. msg[1].buffer = buffer;
  256. msg[1].length = buflen;
  257. /* Read the register data. The returned value is the number messages
  258. * completed.
  259. */
  260. ret = I2C_TRANSFER(priv->i2c, msg, 2);
  261. if (ret < 0)
  262. {
  263. #ifdef CONFIG_I2C_RESET
  264. /* Perhaps the I2C bus is locked up? Try to shake the bus free.
  265. * Don't bother with the reset if this was the last attempt.
  266. */
  267. if (retries < MAX_RETRIES)
  268. {
  269. iwarn("WARNING: I2C_TRANSFER failed: %d ... Resetting\n", ret);
  270. ret = I2C_RESET(priv->i2c);
  271. if (ret < 0)
  272. {
  273. ierr("ERROR: I2C_RESET failed: %d\n", ret);
  274. break;
  275. }
  276. }
  277. #else
  278. ierr("ERROR: I2C_TRANSFER failed: %d\n", ret);
  279. #endif
  280. }
  281. else
  282. {
  283. /* The I2C transfer was successful... break out of the loop and
  284. * return the success indication.
  285. */
  286. break;
  287. }
  288. }
  289. /* Return the last status returned by I2C_TRANSFER */
  290. return ret;
  291. }
  292. /****************************************************************************
  293. * Name: mxt_putreg
  294. ****************************************************************************/
  295. static int mxt_putreg(FAR struct mxt_dev_s *priv, uint16_t regaddr,
  296. FAR const uint8_t *buffer, size_t buflen)
  297. {
  298. struct i2c_msg_s msg[2];
  299. uint8_t addrbuf[2];
  300. int retries;
  301. int ret;
  302. /* Try up to three times to read the register */
  303. for (retries = 1; retries <= MAX_RETRIES; retries++)
  304. {
  305. iinfo("retries=%d regaddr=%04x buflen=%d\n", retries, regaddr, buflen);
  306. /* Set up to write the address */
  307. addrbuf[0] = regaddr & 0xff;
  308. addrbuf[1] = (regaddr >> 8) & 0xff;
  309. msg[0].frequency = priv->frequency;
  310. msg[0].addr = priv->lower->address;
  311. msg[0].flags = 0;
  312. msg[0].buffer = addrbuf;
  313. msg[0].length = 2;
  314. /* Followed by the write data (with no repeated start) */
  315. msg[1].frequency = priv->frequency;
  316. msg[1].addr = priv->lower->address;
  317. msg[1].flags = I2C_M_NOSTART;
  318. msg[1].buffer = (FAR uint8_t *)buffer;
  319. msg[1].length = buflen;
  320. /* Write the register data. The returned value is the number messages
  321. * completed.
  322. */
  323. ret = I2C_TRANSFER(priv->i2c, msg, 2);
  324. if (ret < 0)
  325. {
  326. #ifdef CONFIG_I2C_RESET
  327. /* Perhaps the I2C bus is locked up? Try to shake the bus free.
  328. * Don't bother with the reset if this was the last attempt.
  329. */
  330. if (retries < MAX_RETRIES)
  331. {
  332. iwarn("WARNING: I2C_TRANSFER failed: %d ... Resetting\n", ret);
  333. ret = I2C_RESET(priv->i2c);
  334. if (ret < 0)
  335. {
  336. ierr("ERROR: I2C_RESET failed: %d\n", ret);
  337. }
  338. }
  339. #else
  340. ierr("ERROR: I2C_TRANSFER failed: %d\n", ret);
  341. #endif
  342. }
  343. else
  344. {
  345. /* The I2C transfer was successful... break out of the loop and
  346. * return the success indication.
  347. */
  348. break;
  349. }
  350. }
  351. /* Return the last status returned by I2C_TRANSFER */
  352. return ret;
  353. }
  354. /****************************************************************************
  355. * Name: mxt_object
  356. ****************************************************************************/
  357. static FAR struct mxt_object_s *mxt_object(FAR struct mxt_dev_s *priv,
  358. uint8_t type)
  359. {
  360. struct mxt_object_s *object;
  361. int i;
  362. /* Search the object table for the entry matching the type */
  363. for (i = 0; i < priv->info.nobjects; i++)
  364. {
  365. object = &priv->objtab[i];
  366. if (object->type == type)
  367. {
  368. /* Found it.. return the pointer to the object structure */
  369. return object;
  370. }
  371. }
  372. ierr("ERROR: Invalid object type: %d\n", type);
  373. return NULL;
  374. }
  375. /****************************************************************************
  376. * Name: mxt_getmessage
  377. ****************************************************************************/
  378. static int mxt_getmessage(FAR struct mxt_dev_s *priv,
  379. FAR struct mxt_msg_s *msg)
  380. {
  381. struct mxt_object_s *object;
  382. uint16_t regaddr;
  383. object = mxt_object(priv, MXT_GEN_MESSAGE_T5);
  384. if (object == NULL)
  385. {
  386. ierr("ERROR: mxt_object failed\n");
  387. return -EINVAL;
  388. }
  389. regaddr = MXT_GETUINT16(object->addr);
  390. return mxt_getreg(priv, regaddr, (FAR uint8_t *)msg,
  391. sizeof(struct mxt_msg_s));
  392. }
  393. /****************************************************************************
  394. * Name: mxt_putobject
  395. ****************************************************************************/
  396. static int mxt_putobject(FAR struct mxt_dev_s *priv, uint8_t type,
  397. uint8_t offset, uint8_t value)
  398. {
  399. FAR struct mxt_object_s *object;
  400. uint16_t regaddr;
  401. object = mxt_object(priv, type);
  402. if (object == NULL || offset >= object->size + 1)
  403. {
  404. return -EINVAL;
  405. }
  406. regaddr = MXT_GETUINT16(object->addr);
  407. return mxt_putreg(priv, regaddr + offset, &value, 1);
  408. }
  409. /****************************************************************************
  410. * Name: mxt_getobject
  411. ****************************************************************************/
  412. #if 0 /* Not used */
  413. static int mxt_getobject(FAR struct mxt_dev_s *priv, uint8_t type,
  414. uint8_t offset, FAR uint8_t *value)
  415. {
  416. FAR struct mxt_object_s *object;
  417. uint16_t regaddr;
  418. object = mxt_object(priv, type);
  419. if (object == NULL || offset >= object->size + 1)
  420. {
  421. return -EINVAL;
  422. }
  423. regaddr = MXT_GETUINT16(object->addr);
  424. return mxt_getreg(priv, regaddr + offset, value, 1);
  425. }
  426. #endif
  427. /****************************************************************************
  428. * Name: mxt_flushmsgs
  429. *
  430. * Clear any pending messages be reading messages until there are no
  431. * pending messages. This will force the CHG pin to the high state and
  432. * prevent spurious initial interrupts.
  433. *
  434. ****************************************************************************/
  435. static int mxt_flushmsgs(FAR struct mxt_dev_s *priv)
  436. {
  437. struct mxt_msg_s msg;
  438. int retries = 16;
  439. int ret;
  440. /* Read dummy message until there are no more to read (or until we have
  441. * tried 10 times).
  442. */
  443. do
  444. {
  445. ret = mxt_getmessage(priv, &msg);
  446. if (ret < 0)
  447. {
  448. ierr("ERROR: mxt_getmessage failed: %d\n", ret);
  449. return ret;
  450. }
  451. }
  452. while (msg.id != 0xff && --retries > 0);
  453. /* Complain if we exceed the retry limit */
  454. if (retries <= 0)
  455. {
  456. ierr("ERROR: Failed to clear messages: ID=%02x\n", msg.id);
  457. return -EBUSY;
  458. }
  459. return OK;
  460. }
  461. /****************************************************************************
  462. * Name: mxt_notify
  463. ****************************************************************************/
  464. static void mxt_notify(FAR struct mxt_dev_s *priv)
  465. {
  466. int i;
  467. /* If there are threads waiting on poll() for maXTouch data to become
  468. * available, then wake them up now. NOTE: we wake up all waiting threads
  469. * because we do not know that they are going to do. If they all try to
  470. * read the data, then some make end up blocking after all.
  471. */
  472. for (i = 0; i < CONFIG_MXT_NPOLLWAITERS; i++)
  473. {
  474. struct pollfd *fds = priv->fds[i];
  475. if (fds)
  476. {
  477. fds->revents |= POLLIN;
  478. iinfo("Report events: %02x\n", fds->revents);
  479. nxsem_post(fds->sem);
  480. }
  481. }
  482. /* If there are threads waiting for read data, then signal one of them
  483. * that the read data is available.
  484. */
  485. if (priv->nwaiters > 0)
  486. {
  487. /* After posting this semaphore, we need to exit because the maXTouch
  488. * is no longer available.
  489. */
  490. nxsem_post(&priv->waitsem);
  491. }
  492. }
  493. /****************************************************************************
  494. * Name: mxt_checksample
  495. *
  496. * Description:
  497. * This function implements a test and clear of the priv->event flag.
  498. * Called only from mxt_waitsample.
  499. *
  500. * Assumptions:
  501. * - Scheduler must be locked to prevent the worker thread from running
  502. * while this thread runs. The sample data is, of course, updated from
  503. * the worker thread.
  504. * - Interrupts must be disabled when this is called to (1) prevent posting
  505. * of semaphores from interrupt handlers, and (2) to prevent sampled data
  506. * from changing until it has been reported.
  507. *
  508. ****************************************************************************/
  509. static inline int mxt_checksample(FAR struct mxt_dev_s *priv)
  510. {
  511. /* Is there new maXTouch sample data available? */
  512. if (priv->event)
  513. {
  514. /* Yes.. clear the flag and return success */
  515. priv->event = false;
  516. return OK;
  517. }
  518. /* No.. return failure */
  519. return -EAGAIN;
  520. }
  521. /****************************************************************************
  522. * Name: mxt_waitsample
  523. *
  524. * Wait until sample data is available. Called only from mxt_read.
  525. *
  526. * Assumptions:
  527. * - Scheduler must be locked to prevent the worker thread from running
  528. * while this thread runs. The sample data is, of course, updated from
  529. * the worker thread.
  530. *
  531. ****************************************************************************/
  532. static inline int mxt_waitsample(FAR struct mxt_dev_s *priv)
  533. {
  534. irqstate_t flags;
  535. int ret;
  536. /* Interrupts me be disabled when this is called to (1) prevent posting
  537. * of semaphores from interrupt handlers, and (2) to prevent sampled data
  538. * from changing until it has been reported.
  539. */
  540. flags = enter_critical_section();
  541. /* Now release the semaphore that manages mutually exclusive access to
  542. * the device structure. This may cause other tasks to become ready to
  543. * run, but they cannot run yet because pre-emption is disabled.
  544. */
  545. nxsem_post(&priv->devsem);
  546. /* Try to get the a sample... if we cannot, then wait on the semaphore
  547. * that is posted when new sample data is available.
  548. */
  549. while (mxt_checksample(priv) < 0)
  550. {
  551. /* Wait for a change in the maXTouch state */
  552. priv->nwaiters++;
  553. ret = nxsem_wait(&priv->waitsem);
  554. priv->nwaiters--;
  555. if (ret < 0)
  556. {
  557. goto errout;
  558. }
  559. }
  560. /* Re-acquire the semaphore that manages mutually exclusive access to
  561. * the device structure. We may have to wait here. But we have our
  562. * sample. Interrupts and pre-emption will be re-enabled while we wait.
  563. */
  564. ret = nxsem_wait(&priv->devsem);
  565. errout:
  566. /* Then re-enable interrupts. We might get interrupt here and there
  567. * could be a new sample. But no new threads will run because we still
  568. * have pre-emption disabled.
  569. */
  570. leave_critical_section(flags);
  571. return ret;
  572. }
  573. /****************************************************************************
  574. * Name: mxt_button_event
  575. ****************************************************************************/
  576. #ifdef CONFIG_MXT_BUTTONS
  577. static void mxt_button_event(FAR struct mxt_dev_s *priv,
  578. FAR struct mxt_msg_s *msg)
  579. {
  580. bool button;
  581. int i;
  582. /* REVISIT: Button inputs are currently ignored */
  583. /* Buttons are active low and determined by the GPIO bit
  584. * settings in byte 0 of the message data: A button is
  585. * pressed if the corresponding bit is zero.
  586. */
  587. for (i = 0; i < priv->lower->nbuttons; i++)
  588. {
  589. uint8_t bit = (MXT_GPIO0_MASK << i);
  590. /* Does this implementation support the button? */
  591. if ((priv->lower->bmask & bit) != 0)
  592. {
  593. /* Yes.. get the button state */
  594. button = (msg->body[0] & mask) == 0;
  595. /* Now what? */
  596. UNUSED(button);
  597. }
  598. }
  599. }
  600. #endif
  601. /****************************************************************************
  602. * Name: mxt_touch_event
  603. ****************************************************************************/
  604. static void mxt_touch_event(FAR struct mxt_dev_s *priv,
  605. FAR struct mxt_msg_s *msg, int ndx)
  606. {
  607. FAR struct mxt_sample_s *sample;
  608. uint16_t x;
  609. uint16_t y;
  610. uint8_t area;
  611. uint8_t pressure;
  612. uint8_t status;
  613. /* Extract the 12-bit X and Y positions */
  614. x = ((uint16_t)msg->body[1] << 4) |
  615. (((uint16_t)msg->body[3] >> 4) & 0x0f);
  616. y = ((uint16_t)msg->body[2] << 4) |
  617. (((uint16_t)msg->body[3] & 0x0f));
  618. /* Swap X/Y as necessary */
  619. if (priv->lower->swapxy)
  620. {
  621. uint16_t tmp = x;
  622. y = x;
  623. x = tmp;
  624. }
  625. /* Extract area pressure and status */
  626. area = msg->body[4];
  627. pressure = msg->body[5];
  628. status = msg->body[0];
  629. iinfo("ndx=%u status=%02x pos(%u,%u) area=%u pressure=%u\n",
  630. ndx, status, x, y, area, pressure);
  631. /* The normal sequence that we would see for a touch would be something
  632. * like:
  633. *
  634. * 1. MXT_DETECT + MXT_PRESS
  635. * 2. MXT_DETECT + MXT_AMP
  636. * 3. MXT_DETECT + MXT_MOVE + MXT_AMP
  637. * 4. MXT_RELEASE
  638. *
  639. * So we really only need to check MXT_DETECT to drive this state machine.
  640. */
  641. /* Is this a loss of contact? */
  642. sample = &priv->sample[ndx];
  643. if ((status & MXT_DETECT) == 0)
  644. {
  645. /* Ignore the event if there was no contact to be lost:
  646. *
  647. * CONTACT_NONE = No touch and loss-of-contact already reported
  648. * CONTACT_LOST = No touch and unreported loss-of-contact.
  649. */
  650. if (sample->contact == CONTACT_NONE)
  651. {
  652. /* Return without posting any event */
  653. return;
  654. }
  655. /* State is one of CONTACT_NEW, CONTACT_MOVE, CONTACT_REPORT or
  656. * CONTACT_LOST.
  657. *
  658. * NOTE: Here we do not check for these other states because there is
  659. * not much that can be done anyway. The transition to CONTACK_LOST
  660. * really only makes sense if the preceding state was CONTACT_REPORT.
  661. * If we were in (unreported) CONTACT_NEW or CONTACT_MOVE states, then
  662. * this will overwrite that event and it will not be reported. This
  663. * opens the possibility for contact lost reports when no contact was
  664. * ever reported.
  665. *
  666. * We could improve this be leaving the unreported states in place,
  667. * remembering that the contact was lost, and then reporting the loss-
  668. * of-contact after touch state is reported.
  669. */
  670. sample->contact = CONTACT_LOST;
  671. /* Reset the last position so that we guarantee that the next position
  672. * will pass the thresholding test.
  673. */
  674. sample->lastx = INVALID_POSITION;
  675. sample->lasty = INVALID_POSITION;
  676. }
  677. else
  678. {
  679. /* It is a touch event. If the last loss-of-contact event has not
  680. * been processed yet, then have to bump up the touch identifier and
  681. * hope that the client is smart enough to infer the loss-of-contact
  682. * event for the preceding touch.
  683. */
  684. if (sample->contact == CONTACT_LOST)
  685. {
  686. priv->id++;
  687. }
  688. /* Save the measurements */
  689. sample->x = x;
  690. sample->y = y;
  691. sample->area = area;
  692. sample->pressure = pressure;
  693. sample->valid = true;
  694. add_ui_randomness((x << 16) ^ y ^ (area << 9) ^ (pressure << 1));
  695. /* If this is not the first touch report, then report it as a move:
  696. * Same contact, same ID, but with a new, updated position.
  697. * The CONTACT_REPORT state means that a contacted has been detected,
  698. * but all contact events have been successfully reported.
  699. */
  700. if (sample->contact == CONTACT_REPORT)
  701. {
  702. uint16_t xdiff;
  703. uint16_t ydiff;
  704. /* Not a new contact. Check if the new measurements represent a
  705. * non-trivial change in position. A trivial change is detected
  706. * by comparing the change in position since the last report
  707. * against configurable threshold values.
  708. *
  709. * REVISIT: Should a large change in pressure also generate a
  710. * event?
  711. */
  712. xdiff = x > sample->lastx ? (x - sample->lastx) :
  713. (sample->lastx - x);
  714. ydiff = y > sample->lasty ? (y - sample->lasty) :
  715. (sample->lasty - y);
  716. /* Check the thresholds */
  717. if (xdiff >= CONFIG_MXT_THRESHX || ydiff >= CONFIG_MXT_THRESHY)
  718. {
  719. /* Report a contact move event. This state will be set back
  720. * to CONTACT_REPORT after it been reported.
  721. */
  722. sample->contact = CONTACT_MOVE;
  723. /* Update the last position for next threshold calculations */
  724. sample->lastx = x;
  725. sample->lasty = y;
  726. }
  727. else
  728. {
  729. /* Bail without reporting anything for this event */
  730. return;
  731. }
  732. }
  733. /* If we have seen this contact before but it has not yet been
  734. * reported, then do nothing other than overwrite the positional
  735. * data.
  736. *
  737. * This the state must be one of CONTACT_NONE or CONTACT_LOST (see
  738. * above) and we have a new contact with a new ID.
  739. */
  740. else if (sample->contact != CONTACT_NEW &&
  741. sample->contact != CONTACT_MOVE)
  742. {
  743. /* First contact. Save the contact event and assign a new
  744. * ID to the contact.
  745. */
  746. sample->contact = CONTACT_NEW;
  747. sample->id = priv->id++;
  748. /* Update the last position for next threshold calculations */
  749. sample->lastx = x;
  750. sample->lasty = y;
  751. /* This state will be set to CONTACT_REPORT after it
  752. * been reported.
  753. */
  754. }
  755. }
  756. /* Indicate the availability of new sample data for this ID and notify
  757. * any waiters that new maXTouch data is available
  758. */
  759. priv->event = true;
  760. mxt_notify(priv);
  761. }
  762. /****************************************************************************
  763. * Name: mxt_worker
  764. ****************************************************************************/
  765. static void mxt_worker(FAR void *arg)
  766. {
  767. FAR struct mxt_dev_s *priv = (FAR struct mxt_dev_s *)arg;
  768. FAR const struct mxt_lower_s *lower;
  769. struct mxt_msg_s msg;
  770. uint8_t id;
  771. int retries;
  772. int ret;
  773. DEBUGASSERT(priv != NULL);
  774. /* Get a pointer the callbacks for convenience (and so the code is not so
  775. * ugly).
  776. */
  777. lower = priv->lower;
  778. DEBUGASSERT(lower != NULL);
  779. /* Get exclusive access to the MXT driver data structure */
  780. do
  781. {
  782. ret = nxsem_wait_uninterruptible(&priv->devsem);
  783. /* This would only fail if something canceled the worker thread?
  784. * That is not expected.
  785. */
  786. DEBUGASSERT(ret == OK || ret == -ECANCELED);
  787. }
  788. while (ret < 0);
  789. /* Loop, processing each message from the maXTouch */
  790. retries = 0;
  791. do
  792. {
  793. /* Retrieve the next message from the maXTouch */
  794. ret = mxt_getmessage(priv, &msg);
  795. if (ret < 0)
  796. {
  797. ierr("ERROR: mxt_getmessage failed: %d\n", ret);
  798. goto errout_with_semaphore;
  799. }
  800. id = msg.id;
  801. #ifdef MXT_SUPPORT_T6
  802. /* Check for T6 */
  803. if (id == priv->t6id)
  804. {
  805. uint32_t chksum;
  806. int status;
  807. status = msg.body[0];
  808. chksum = (uint32_t)msg.body[1] |
  809. ((uint32_t)msg.body[2] << 8) |
  810. ((uint32_t)msg.body[3] << 16);
  811. iinfo("T6: status: %02x checksum: %06lx\n",
  812. status, (unsigned long)chksum);
  813. retries = 0;
  814. }
  815. else
  816. #endif
  817. /* Check for T9 */
  818. if (id >= priv->t9idmin && id <= priv->t9idmax)
  819. {
  820. mxt_touch_event(priv, &msg, id - priv->t9idmin);
  821. retries = 0;
  822. }
  823. #ifdef CONFIG_MXT_BUTTONS
  824. /* Check for T19 */
  825. else if (msg.id == priv->t19id)
  826. {
  827. mxt_button_event(priv, &msg);
  828. retries = 0;
  829. }
  830. #endif
  831. /* 0xff marks the end of the messages; any other message IDs are
  832. * ignored (after complaining a little).
  833. */
  834. else if (msg.id != 0xff)
  835. {
  836. iinfo("Ignored: id=%u message="
  837. "{%02x %02x %02x %02x %02x %02x %02x}\n",
  838. msg.id,
  839. msg.body[0], msg.body[1], msg.body[2],
  840. msg.body[3], msg.body[4], msg.body[5],
  841. msg.body[6]);
  842. retries++;
  843. }
  844. }
  845. while (id != 0xff && retries < 16);
  846. errout_with_semaphore:
  847. /* Release our lock on the MXT device */
  848. nxsem_post(&priv->devsem);
  849. /* Acknowledge and re-enable maXTouch interrupts */
  850. MXT_CLEAR(lower);
  851. MXT_ENABLE(lower);
  852. }
  853. /****************************************************************************
  854. * Name: mxt_interrupt
  855. ****************************************************************************/
  856. static int mxt_interrupt(FAR const struct mxt_lower_s *lower, FAR void *arg)
  857. {
  858. FAR struct mxt_dev_s *priv = (FAR struct mxt_dev_s *)arg;
  859. int ret;
  860. /* Get a pointer the callbacks for convenience (and so the code is not so
  861. * ugly).
  862. */
  863. DEBUGASSERT(lower != NULL && priv != NULL);
  864. /* Disable further interrupts */
  865. MXT_DISABLE(lower);
  866. /* Transfer processing to the worker thread. Since maXTouch interrupts are
  867. * disabled while the work is pending, no special action should be required
  868. * to protected the work queue.
  869. */
  870. DEBUGASSERT(priv->work.worker == NULL);
  871. ret = work_queue(HPWORK, &priv->work, mxt_worker, priv, 0);
  872. if (ret != 0)
  873. {
  874. ierr("ERROR: Failed to queue work: %d\n", ret);
  875. }
  876. /* Clear any pending interrupts and return success */
  877. lower->clear(lower);
  878. return OK;
  879. }
  880. /****************************************************************************
  881. * Name: mxt_open
  882. ****************************************************************************/
  883. static int mxt_open(FAR struct file *filep)
  884. {
  885. FAR struct inode *inode;
  886. FAR struct mxt_dev_s *priv;
  887. uint8_t tmp;
  888. int ret;
  889. DEBUGASSERT(filep);
  890. inode = filep->f_inode;
  891. DEBUGASSERT(inode && inode->i_private);
  892. priv = (FAR struct mxt_dev_s *)inode->i_private;
  893. /* Get exclusive access to the driver data structure */
  894. ret = nxsem_wait(&priv->devsem);
  895. if (ret < 0)
  896. {
  897. return ret;
  898. }
  899. /* Increment the reference count */
  900. tmp = priv->crefs + 1;
  901. if (tmp == 0)
  902. {
  903. /* More than 255 opens; uint8_t overflows to zero */
  904. ierr("ERROR: Too many opens: %d\n", priv->crefs);
  905. ret = -EMFILE;
  906. goto errout_with_sem;
  907. }
  908. /* When the reference increments to 1, this is the first open event
  909. * on the driver.. and an opportunity to do any one-time initialization.
  910. */
  911. if (tmp == 1)
  912. {
  913. /* Touch enable */
  914. ret = mxt_putobject(priv, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
  915. if (ret < 0)
  916. {
  917. ierr("ERROR: Failed to enable touch: %d\n", ret);
  918. goto errout_with_sem;
  919. }
  920. /* Clear any pending messages by reading all messages. This will
  921. * force the CHG interrupt pin to the high state and prevent spurious
  922. * interrupts when they are enabled.
  923. */
  924. ret = mxt_flushmsgs(priv);
  925. if (ret < 0)
  926. {
  927. ierr("ERROR: mxt_flushmsgs failed: %d\n", ret);
  928. mxt_putobject(priv, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
  929. goto errout_with_sem;
  930. }
  931. /* Enable touch interrupts */
  932. MXT_ENABLE(priv->lower);
  933. }
  934. /* Save the new open count on success */
  935. priv->crefs = tmp;
  936. errout_with_sem:
  937. nxsem_post(&priv->devsem);
  938. return ret;
  939. }
  940. /****************************************************************************
  941. * Name: mxt_close
  942. ****************************************************************************/
  943. static int mxt_close(FAR struct file *filep)
  944. {
  945. FAR struct inode *inode;
  946. FAR struct mxt_dev_s *priv;
  947. int ret;
  948. DEBUGASSERT(filep);
  949. inode = filep->f_inode;
  950. DEBUGASSERT(inode && inode->i_private);
  951. priv = (FAR struct mxt_dev_s *)inode->i_private;
  952. /* Get exclusive access to the driver data structure */
  953. ret = nxsem_wait(&priv->devsem);
  954. if (ret < 0)
  955. {
  956. return ret;
  957. }
  958. /* Decrement the reference count unless it would decrement a negative
  959. * value. When the count decrements to zero, there are no further
  960. * open references to the driver.
  961. */
  962. if (priv->crefs >= 1)
  963. {
  964. if (--priv->crefs < 1)
  965. {
  966. /* Disable touch interrupts */
  967. MXT_ENABLE(priv->lower);
  968. /* Touch disable */
  969. ret = mxt_putobject(priv, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
  970. if (ret < 0)
  971. {
  972. ierr("ERROR: Failed to disable touch: %d\n", ret);
  973. }
  974. }
  975. }
  976. nxsem_post(&priv->devsem);
  977. return OK;
  978. }
  979. /****************************************************************************
  980. * Name: mxt_read
  981. ****************************************************************************/
  982. static ssize_t mxt_read(FAR struct file *filep, FAR char *buffer, size_t len)
  983. {
  984. FAR struct inode *inode;
  985. FAR struct mxt_dev_s *priv;
  986. ssize_t samplesize;
  987. int ncontacts;
  988. int ret;
  989. int i;
  990. int j;
  991. DEBUGASSERT(filep);
  992. inode = filep->f_inode;
  993. DEBUGASSERT(inode && inode->i_private);
  994. priv = (FAR struct mxt_dev_s *)inode->i_private;
  995. /* Verify that the caller has provided a buffer large enough to receive
  996. * the touch data.
  997. */
  998. if (len < SIZEOF_TOUCH_SAMPLE_S(1))
  999. {
  1000. /* We could provide logic to break up a touch report into segments and
  1001. * handle smaller reads... but why?
  1002. */
  1003. return -ENOSYS;
  1004. }
  1005. /* Get exclusive access to the driver data structure */
  1006. ret = nxsem_wait(&priv->devsem);
  1007. if (ret < 0)
  1008. {
  1009. return ret;
  1010. }
  1011. /* Locking the scheduler will prevent the worker thread from running
  1012. * until we finish here.
  1013. */
  1014. sched_lock();
  1015. /* Try to read sample data. */
  1016. ret = mxt_checksample(priv);
  1017. if (ret < 0)
  1018. {
  1019. /* Sample data is not available now. We would ave to wait to get
  1020. * receive sample data. If the user has specified the O_NONBLOCK
  1021. * option, then just return an error.
  1022. */
  1023. if (filep->f_oflags & O_NONBLOCK)
  1024. {
  1025. ret = -EAGAIN;
  1026. goto errout;
  1027. }
  1028. /* Wait for sample data */
  1029. ret = mxt_waitsample(priv);
  1030. if (ret < 0)
  1031. {
  1032. /* We might have been awakened by a signal */
  1033. goto errout;
  1034. }
  1035. }
  1036. /* In any event, we now have sampled maXTouch data that we can report
  1037. * to the caller. First, count the number of valid contacts.
  1038. */
  1039. samplesize = 0;
  1040. ncontacts = 0;
  1041. for (i = 0; i < priv->nslots; i++)
  1042. {
  1043. FAR struct mxt_sample_s *sample = &priv->sample[i];
  1044. /* Do we need to report this? We need to report the event if
  1045. * it is CONTACT_LOST or CONTACT_REPORT or CONTACT_MOVE (with new,
  1046. * valid positional data).
  1047. */
  1048. if (sample->contact == CONTACT_LOST ||
  1049. sample->contact == CONTACT_NEW ||
  1050. sample->contact == CONTACT_MOVE)
  1051. {
  1052. int newcount = ncontacts + 1;
  1053. ssize_t newsize = SIZEOF_TOUCH_SAMPLE_S(newcount);
  1054. /* Would this sample exceed the buffer size provided by the
  1055. * caller?
  1056. */
  1057. if (newsize > len)
  1058. {
  1059. /* Yes.. break out of the loop using the previous size and
  1060. * count.
  1061. */
  1062. break;
  1063. }
  1064. /* Save the new size and count */
  1065. ncontacts = newcount;
  1066. samplesize = newsize;
  1067. }
  1068. }
  1069. /* Did we find any valid samples? */
  1070. if (ncontacts > 0)
  1071. {
  1072. FAR struct touch_sample_s *report =
  1073. (FAR struct touch_sample_s *)buffer;
  1074. /* Yes, copy the sample data into the user buffer */
  1075. memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(ncontacts));
  1076. report->npoints = ncontacts;
  1077. for (i = 0, j = 0; i < priv->nslots && j < ncontacts; i++)
  1078. {
  1079. FAR struct mxt_sample_s *sample = &priv->sample[i];
  1080. /* Do we need to report this? We need to report the event if
  1081. * it is CONTACT_LOST or CONTACT_REPORT or CONTACT_MOVE (with new,
  1082. * valid positional data).
  1083. */
  1084. if (sample->contact == CONTACT_LOST ||
  1085. sample->contact == CONTACT_NEW ||
  1086. sample->contact == CONTACT_MOVE)
  1087. {
  1088. /* Yes.. transfer the sample data */
  1089. FAR struct touch_point_s *point = &report->point[j];
  1090. j++;
  1091. /* REVISIT: height and width are not set, area is
  1092. * not used.
  1093. */
  1094. point->id = sample->id;
  1095. point->x = sample->x;
  1096. point->y = sample->y;
  1097. point->pressure = sample->pressure;
  1098. /* Report the appropriate flags */
  1099. if (sample->contact == CONTACT_LOST)
  1100. {
  1101. /* The contact was lost. Is the positional data
  1102. * valid? This is important to know because the release
  1103. * will be sent to the window based on its last positional
  1104. * data.
  1105. */
  1106. if (sample->valid)
  1107. {
  1108. point->flags = TOUCH_UP | TOUCH_ID_VALID |
  1109. TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
  1110. }
  1111. else
  1112. {
  1113. point->flags = TOUCH_UP | TOUCH_ID_VALID;
  1114. }
  1115. /* Change to CONTACT_NONE to indicate that the sample
  1116. * has been reported. From here it can change only
  1117. * to CONTACT_REPORT (with a new ID).
  1118. */
  1119. sample->contact = CONTACT_NONE;
  1120. }
  1121. else
  1122. {
  1123. /* We have contact. Is it the first contact? */
  1124. if (sample->contact == CONTACT_NEW)
  1125. {
  1126. /* Yes.. first contact. */
  1127. point->flags = TOUCH_DOWN | TOUCH_ID_VALID |
  1128. TOUCH_POS_VALID;
  1129. }
  1130. else /* if (sample->contact == CONTACT_MOVE) */
  1131. {
  1132. /* No.. then it must be movement of the same contact */
  1133. point->flags = TOUCH_MOVE | TOUCH_ID_VALID |
  1134. TOUCH_POS_VALID;
  1135. }
  1136. /* Change to CONTACT_REPORT to indicate that the sample
  1137. * has been reported. From here is can change to
  1138. * CONTACT_LOST (same ID), CONTACT_MOVE (same ID) or back
  1139. * to CONTACT_NEW (new ID).
  1140. */
  1141. sample->contact = CONTACT_REPORT;
  1142. /* A pressure measurement of zero means that pressure is
  1143. * not available.
  1144. */
  1145. if (point->pressure != 0)
  1146. {
  1147. point->flags |= TOUCH_PRESSURE_VALID;
  1148. }
  1149. }
  1150. /* In any case, the sample data has been reported and is no
  1151. * longer valid.
  1152. */
  1153. sample->valid = false;
  1154. }
  1155. }
  1156. }
  1157. ret = samplesize;
  1158. errout:
  1159. sched_unlock();
  1160. nxsem_post(&priv->devsem);
  1161. return ret;
  1162. }
  1163. /****************************************************************************
  1164. * Name: mxt_ioctl
  1165. ****************************************************************************/
  1166. static int mxt_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  1167. {
  1168. FAR struct inode *inode;
  1169. FAR struct mxt_dev_s *priv;
  1170. int ret;
  1171. iinfo("cmd: %d arg: %ld\n", cmd, arg);
  1172. DEBUGASSERT(filep);
  1173. inode = filep->f_inode;
  1174. DEBUGASSERT(inode && inode->i_private);
  1175. priv = (FAR struct mxt_dev_s *)inode->i_private;
  1176. /* Get exclusive access to the driver data structure */
  1177. ret = nxsem_wait(&priv->devsem);
  1178. if (ret < 0)
  1179. {
  1180. return ret;
  1181. }
  1182. /* Process the IOCTL by command */
  1183. switch (cmd)
  1184. {
  1185. case TSIOC_SETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
  1186. {
  1187. FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
  1188. DEBUGASSERT(priv->lower != NULL && ptr != NULL);
  1189. priv->frequency = *ptr;
  1190. }
  1191. break;
  1192. case TSIOC_GETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
  1193. {
  1194. FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
  1195. DEBUGASSERT(priv->lower != NULL && ptr != NULL);
  1196. *ptr = priv->frequency;
  1197. }
  1198. break;
  1199. default:
  1200. ret = -ENOTTY;
  1201. break;
  1202. }
  1203. nxsem_post(&priv->devsem);
  1204. return ret;
  1205. }
  1206. /****************************************************************************
  1207. * Name: mxt_poll
  1208. ****************************************************************************/
  1209. static int mxt_poll(FAR struct file *filep, FAR struct pollfd *fds,
  1210. bool setup)
  1211. {
  1212. FAR struct inode *inode;
  1213. FAR struct mxt_dev_s *priv;
  1214. int ret;
  1215. int i;
  1216. iinfo("setup: %d\n", (int)setup);
  1217. DEBUGASSERT(filep && fds);
  1218. inode = filep->f_inode;
  1219. DEBUGASSERT(inode && inode->i_private);
  1220. priv = (FAR struct mxt_dev_s *)inode->i_private;
  1221. /* Are we setting up the poll? Or tearing it down? */
  1222. ret = nxsem_wait(&priv->devsem);
  1223. if (ret < 0)
  1224. {
  1225. return ret;
  1226. }
  1227. if (setup)
  1228. {
  1229. /* Ignore waits that do not include POLLIN */
  1230. if ((fds->events & POLLIN) == 0)
  1231. {
  1232. ierr("ERROR: Missing POLLIN: revents: %08x\n", fds->revents);
  1233. ret = -EDEADLK;
  1234. goto errout;
  1235. }
  1236. /* This is a request to set up the poll. Find an available
  1237. * slot for the poll structure reference
  1238. */
  1239. for (i = 0; i < CONFIG_MXT_NPOLLWAITERS; i++)
  1240. {
  1241. /* Find an available slot */
  1242. if (!priv->fds[i])
  1243. {
  1244. /* Bind the poll structure and this slot */
  1245. priv->fds[i] = fds;
  1246. fds->priv = &priv->fds[i];
  1247. break;
  1248. }
  1249. }
  1250. if (i >= CONFIG_MXT_NPOLLWAITERS)
  1251. {
  1252. ierr("ERROR: No available slot found: %d\n", i);
  1253. fds->priv = NULL;
  1254. ret = -EBUSY;
  1255. goto errout;
  1256. }
  1257. /* Should we immediately notify on any of the requested events? */
  1258. if (priv->event)
  1259. {
  1260. mxt_notify(priv);
  1261. }
  1262. }
  1263. else if (fds->priv)
  1264. {
  1265. /* This is a request to tear down the poll. */
  1266. struct pollfd **slot = (struct pollfd **)fds->priv;
  1267. DEBUGASSERT(slot != NULL);
  1268. /* Remove all memory of the poll setup */
  1269. *slot = NULL;
  1270. fds->priv = NULL;
  1271. }
  1272. errout:
  1273. nxsem_post(&priv->devsem);
  1274. return ret;
  1275. }
  1276. /****************************************************************************
  1277. * Name: mxt_getinfo
  1278. ****************************************************************************/
  1279. static int mxt_getinfo(struct mxt_dev_s *priv)
  1280. {
  1281. int ret;
  1282. /* Read 7-byte information block starting at address MXT_INFO */
  1283. ret = mxt_getreg(priv, MXT_INFO, (FAR uint8_t *)&priv->info,
  1284. sizeof(struct mxt_info_s));
  1285. if (ret < 0)
  1286. {
  1287. ierr("ERROR: mxt_getreg failed: %d\n", ret);
  1288. return ret;
  1289. }
  1290. return OK;
  1291. }
  1292. /****************************************************************************
  1293. * Name: mxt_getobjtab
  1294. ****************************************************************************/
  1295. static int mxt_getobjtab(FAR struct mxt_dev_s *priv)
  1296. {
  1297. FAR struct mxt_object_s *object;
  1298. size_t tabsize;
  1299. uint8_t idmin;
  1300. uint8_t idmax;
  1301. uint8_t id;
  1302. int ret;
  1303. int i;
  1304. /* Read the size of the object table */
  1305. tabsize = priv->info.nobjects * sizeof(struct mxt_object_s);
  1306. ret = mxt_getreg(priv, MXT_OBJECT_START, (FAR uint8_t *)priv->objtab,
  1307. tabsize);
  1308. if (ret < 0)
  1309. {
  1310. ierr("ERROR: Failed to object table size: %d\n", ret);
  1311. return ret;
  1312. }
  1313. /* Search through the object table. Find the values associated with
  1314. * certain object types and save those ID.Valid report IDs start at ID=1.
  1315. */
  1316. for (i = 0, id = 1; i < priv->info.nobjects; i++)
  1317. {
  1318. object = &priv->objtab[i];
  1319. if (object->nids > 0)
  1320. {
  1321. idmin = id;
  1322. id += object->nids * (object->ninstances + 1);
  1323. idmax = id - 1;
  1324. }
  1325. else
  1326. {
  1327. idmin = 0;
  1328. idmax = 0;
  1329. }
  1330. iinfo("%2d. type %2d addr %04x size: %d instances: %d IDs: %u-%u\n",
  1331. i, object->type, MXT_GETUINT16(object->addr), object->size + 1,
  1332. object->ninstances + 1, idmin, idmax);
  1333. switch (object->type)
  1334. {
  1335. #ifdef MXT_SUPPORT_T6
  1336. case MXT_GEN_COMMAND_T6:
  1337. priv->t6id = idmin;
  1338. break;
  1339. #endif
  1340. case MXT_TOUCH_MULTI_T9:
  1341. priv->t9idmin = idmin;
  1342. priv->t9idmax = idmax;
  1343. break;
  1344. #ifdef CONFIG_MXT_BUTTONS
  1345. case MXT_SPT_GPIOPWM_T19:
  1346. priv->t19id = idmin;
  1347. break;
  1348. #endif
  1349. default:
  1350. break;
  1351. }
  1352. }
  1353. return OK;
  1354. }
  1355. /****************************************************************************
  1356. * Name: mxt_hwinitialize
  1357. ****************************************************************************/
  1358. static int mxt_hwinitialize(FAR struct mxt_dev_s *priv)
  1359. {
  1360. struct mxt_info_s *info = &priv->info;
  1361. unsigned int nslots;
  1362. uint8_t regval;
  1363. int ret;
  1364. /* Set the selected I2C frequency */
  1365. priv->frequency = priv->lower->frequency;
  1366. /* Read the info registers from the device */
  1367. ret = mxt_getinfo(priv);
  1368. if (ret < 0)
  1369. {
  1370. ierr("ERROR: Failed to read info registers: %d\n", ret);
  1371. return ret;
  1372. }
  1373. /* Allocate memory for the object table */
  1374. priv->objtab = kmm_zalloc(info->nobjects * sizeof(struct mxt_object_s));
  1375. if (priv->objtab == NULL)
  1376. {
  1377. ierr("ERROR: Failed to allocate object table\n");
  1378. return -ENOMEM;
  1379. }
  1380. /* Get object table information */
  1381. ret = mxt_getobjtab(priv);
  1382. if (ret < 0)
  1383. {
  1384. goto errout_with_objtab;
  1385. }
  1386. /* Perform a soft reset */
  1387. ret = mxt_putobject(priv, MXT_GEN_COMMAND_T6, MXT_COMMAND_RESET, 1);
  1388. if (ret < 0)
  1389. {
  1390. ierr("ERROR: Soft reset failed: %d\n", ret);
  1391. goto errout_with_objtab;
  1392. }
  1393. nxsig_usleep(MXT_RESET_TIME);
  1394. /* Update matrix size in the info structure */
  1395. ret = mxt_getreg(priv, MXT_MATRIX_X_SIZE, (FAR uint8_t *)&regval, 1);
  1396. if (ret < 0)
  1397. {
  1398. ierr("ERROR: Failed to get X size: %d\n", ret);
  1399. goto errout_with_objtab;
  1400. }
  1401. info->xsize = regval;
  1402. ret = mxt_getreg(priv, MXT_MATRIX_Y_SIZE, (FAR uint8_t *)&regval, 1);
  1403. if (ret < 0)
  1404. {
  1405. ierr("ERROR: Failed to get Y size: %d\n", ret);
  1406. goto errout_with_objtab;
  1407. }
  1408. info->ysize = regval;
  1409. iinfo("Family: %u variant: %u version: %u.%u.%02x\n",
  1410. info->family, info->variant, info->version >> 4,
  1411. info->version & 0x0f, info->build);
  1412. iinfo("Matrix size: (%u,%u) objects: %u\n",
  1413. info->xsize, info->ysize, info->nobjects);
  1414. /* How many multi touch "slots" */
  1415. nslots = priv->t9idmax - priv->t9idmin + 1;
  1416. DEBUGASSERT(nslots > 0 && nslots < 256);
  1417. priv->nslots = nslots;
  1418. /* Allocate a place to hold sample data for each slot */
  1419. priv->sample = (FAR struct mxt_sample_s *)
  1420. kmm_zalloc(nslots * sizeof(struct mxt_sample_s));
  1421. if (priv->sample == NULL)
  1422. {
  1423. ierr("ERROR: Failed to allocate object table\n");
  1424. ret = -ENOMEM;
  1425. goto errout_with_objtab;
  1426. }
  1427. return OK;
  1428. /* Error exits */
  1429. errout_with_objtab:
  1430. kmm_free(priv->objtab);
  1431. priv->objtab = NULL;
  1432. return ret;
  1433. }
  1434. /****************************************************************************
  1435. * Public Functions
  1436. ****************************************************************************/
  1437. /****************************************************************************
  1438. * Name: mxt_register
  1439. *
  1440. * Description:
  1441. * Configure the maXTouch to use the provided I2C device instance. This
  1442. * will register the driver as /dev/inputN where N is the minor device
  1443. * number
  1444. *
  1445. * Input Parameters:
  1446. * i2c - An I2C driver instance
  1447. * lower - Persistent board configuration data
  1448. * minor - The input device minor number
  1449. *
  1450. * Returned Value:
  1451. * Zero is returned on success. Otherwise, a negated errno value is
  1452. * returned to indicate the nature of the failure.
  1453. *
  1454. ****************************************************************************/
  1455. int mxt_register(FAR struct i2c_master_s *i2c,
  1456. FAR const struct mxt_lower_s * const lower, int minor)
  1457. {
  1458. FAR struct mxt_dev_s *priv;
  1459. char devname[DEV_NAMELEN];
  1460. int ret;
  1461. iinfo("i2c: %p minor: %d\n", i2c, minor);
  1462. /* Debug-only sanity checks */
  1463. DEBUGASSERT(i2c != NULL && lower != NULL && minor >= 0 && minor < 100);
  1464. /* Create and initialize a maXTouch device driver instance */
  1465. priv = (FAR struct mxt_dev_s *)kmm_zalloc(sizeof(struct mxt_dev_s));
  1466. if (priv == NULL)
  1467. {
  1468. ierr("ERROR: Failed allocate device structure\n");
  1469. return -ENOMEM;
  1470. }
  1471. /* Initialize the ADS7843E device driver instance */
  1472. memset(priv, 0, sizeof(struct mxt_dev_s));
  1473. priv->i2c = i2c; /* Save the SPI device handle */
  1474. priv->lower = lower; /* Save the board configuration */
  1475. /* Initialize semaphores */
  1476. nxsem_init(&priv->devsem, 0, 1); /* Initialize device semaphore */
  1477. nxsem_init(&priv->waitsem, 0, 0); /* Initialize event wait semaphore */
  1478. /* The event wait semaphore is used for signaling and, hence, should not
  1479. * have priority inheritance enabled.
  1480. */
  1481. nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE);
  1482. /* Make sure that interrupts are disabled */
  1483. MXT_CLEAR(lower);
  1484. MXT_DISABLE(lower);
  1485. /* Attach the interrupt handler */
  1486. ret = MXT_ATTACH(lower, mxt_interrupt, priv);
  1487. if (ret < 0)
  1488. {
  1489. ierr("ERROR: Failed to attach interrupt\n");
  1490. goto errout_with_priv;
  1491. }
  1492. /* Configure the MXT hardware */
  1493. ret = mxt_hwinitialize(priv);
  1494. if (ret < 0)
  1495. {
  1496. ierr("ERROR: mxt_hwinitialize failed: %d\n", ret);
  1497. goto errout_with_irq;
  1498. }
  1499. /* Register the device as an input device */
  1500. snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor);
  1501. iinfo("Registering %s\n", devname);
  1502. ret = register_driver(devname, &mxt_fops, 0666, priv);
  1503. if (ret < 0)
  1504. {
  1505. ierr("ERROR: register_driver() failed: %d\n", ret);
  1506. goto errout_with_hwinit;
  1507. }
  1508. /* And return success. MXT interrupts will not be enable until the
  1509. * MXT device has been opened (see mxt_open).
  1510. */
  1511. return OK;
  1512. /* Error clean-up exits */
  1513. errout_with_hwinit:
  1514. kmm_free(priv->objtab);
  1515. kmm_free(priv->sample);
  1516. errout_with_irq:
  1517. MXT_DETACH(lower);
  1518. errout_with_priv:
  1519. nxsem_destroy(&priv->devsem);
  1520. nxsem_destroy(&priv->waitsem);
  1521. kmm_free(priv);
  1522. return ret;
  1523. }