ad7877.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc.
  4. *
  5. * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver
  6. * Based on: ads7846.c
  7. *
  8. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  9. *
  10. * History:
  11. * Copyright (c) 2005 David Brownell
  12. * Copyright (c) 2006 Nokia Corporation
  13. * Various changes: Imre Deak <[email protected]>
  14. *
  15. * Using code from:
  16. * - corgi_ts.c
  17. * Copyright (C) 2004-2005 Richard Purdie
  18. * - omap_ts.[hc], ads7846.h, ts_osk.c
  19. * Copyright (C) 2002 MontaVista Software
  20. * Copyright (C) 2004 Texas Instruments
  21. * Copyright (C) 2005 Dirk Behme
  22. */
  23. #include <linux/device.h>
  24. #include <linux/delay.h>
  25. #include <linux/input.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/pm.h>
  28. #include <linux/slab.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/spi/ad7877.h>
  31. #include <linux/module.h>
  32. #include <asm/irq.h>
  33. #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
  34. #define MAX_SPI_FREQ_HZ 20000000
  35. #define MAX_12BIT ((1<<12)-1)
  36. #define AD7877_REG_ZEROS 0
  37. #define AD7877_REG_CTRL1 1
  38. #define AD7877_REG_CTRL2 2
  39. #define AD7877_REG_ALERT 3
  40. #define AD7877_REG_AUX1HIGH 4
  41. #define AD7877_REG_AUX1LOW 5
  42. #define AD7877_REG_BAT1HIGH 6
  43. #define AD7877_REG_BAT1LOW 7
  44. #define AD7877_REG_BAT2HIGH 8
  45. #define AD7877_REG_BAT2LOW 9
  46. #define AD7877_REG_TEMP1HIGH 10
  47. #define AD7877_REG_TEMP1LOW 11
  48. #define AD7877_REG_SEQ0 12
  49. #define AD7877_REG_SEQ1 13
  50. #define AD7877_REG_DAC 14
  51. #define AD7877_REG_NONE1 15
  52. #define AD7877_REG_EXTWRITE 15
  53. #define AD7877_REG_XPLUS 16
  54. #define AD7877_REG_YPLUS 17
  55. #define AD7877_REG_Z2 18
  56. #define AD7877_REG_aux1 19
  57. #define AD7877_REG_aux2 20
  58. #define AD7877_REG_aux3 21
  59. #define AD7877_REG_bat1 22
  60. #define AD7877_REG_bat2 23
  61. #define AD7877_REG_temp1 24
  62. #define AD7877_REG_temp2 25
  63. #define AD7877_REG_Z1 26
  64. #define AD7877_REG_GPIOCTRL1 27
  65. #define AD7877_REG_GPIOCTRL2 28
  66. #define AD7877_REG_GPIODATA 29
  67. #define AD7877_REG_NONE2 30
  68. #define AD7877_REG_NONE3 31
  69. #define AD7877_SEQ_YPLUS_BIT (1<<11)
  70. #define AD7877_SEQ_XPLUS_BIT (1<<10)
  71. #define AD7877_SEQ_Z2_BIT (1<<9)
  72. #define AD7877_SEQ_AUX1_BIT (1<<8)
  73. #define AD7877_SEQ_AUX2_BIT (1<<7)
  74. #define AD7877_SEQ_AUX3_BIT (1<<6)
  75. #define AD7877_SEQ_BAT1_BIT (1<<5)
  76. #define AD7877_SEQ_BAT2_BIT (1<<4)
  77. #define AD7877_SEQ_TEMP1_BIT (1<<3)
  78. #define AD7877_SEQ_TEMP2_BIT (1<<2)
  79. #define AD7877_SEQ_Z1_BIT (1<<1)
  80. enum {
  81. AD7877_SEQ_YPOS = 0,
  82. AD7877_SEQ_XPOS = 1,
  83. AD7877_SEQ_Z2 = 2,
  84. AD7877_SEQ_AUX1 = 3,
  85. AD7877_SEQ_AUX2 = 4,
  86. AD7877_SEQ_AUX3 = 5,
  87. AD7877_SEQ_BAT1 = 6,
  88. AD7877_SEQ_BAT2 = 7,
  89. AD7877_SEQ_TEMP1 = 8,
  90. AD7877_SEQ_TEMP2 = 9,
  91. AD7877_SEQ_Z1 = 10,
  92. AD7877_NR_SENSE = 11,
  93. };
  94. /* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */
  95. #define AD7877_DAC_CONF 0x1
  96. /* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */
  97. #define AD7877_EXTW_GPIO_3_CONF 0x1C4
  98. #define AD7877_EXTW_GPIO_DATA 0x200
  99. /* Control REG 2 */
  100. #define AD7877_TMR(x) ((x & 0x3) << 0)
  101. #define AD7877_REF(x) ((x & 0x1) << 2)
  102. #define AD7877_POL(x) ((x & 0x1) << 3)
  103. #define AD7877_FCD(x) ((x & 0x3) << 4)
  104. #define AD7877_PM(x) ((x & 0x3) << 6)
  105. #define AD7877_ACQ(x) ((x & 0x3) << 8)
  106. #define AD7877_AVG(x) ((x & 0x3) << 10)
  107. /* Control REG 1 */
  108. #define AD7877_SER (1 << 11) /* non-differential */
  109. #define AD7877_DFR (0 << 11) /* differential */
  110. #define AD7877_MODE_NOC (0) /* Do not convert */
  111. #define AD7877_MODE_SCC (1) /* Single channel conversion */
  112. #define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */
  113. #define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */
  114. #define AD7877_CHANADD(x) ((x&0xF)<<7)
  115. #define AD7877_READADD(x) ((x)<<2)
  116. #define AD7877_WRITEADD(x) ((x)<<12)
  117. #define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
  118. AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
  119. AD7877_READADD(AD7877_REG_ ## x))
  120. #define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
  121. AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
  122. /*
  123. * Non-touchscreen sensors only use single-ended conversions.
  124. */
  125. struct ser_req {
  126. u16 reset;
  127. u16 ref_on;
  128. u16 command;
  129. struct spi_message msg;
  130. struct spi_transfer xfer[6];
  131. /*
  132. * DMA (thus cache coherency maintenance) requires the
  133. * transfer buffers to live in their own cache lines.
  134. */
  135. u16 sample ____cacheline_aligned;
  136. };
  137. struct ad7877 {
  138. struct input_dev *input;
  139. char phys[32];
  140. struct spi_device *spi;
  141. u16 model;
  142. u16 vref_delay_usecs;
  143. u16 x_plate_ohms;
  144. u16 pressure_max;
  145. u16 cmd_crtl1;
  146. u16 cmd_crtl2;
  147. u16 cmd_dummy;
  148. u16 dac;
  149. u8 stopacq_polarity;
  150. u8 first_conversion_delay;
  151. u8 acquisition_time;
  152. u8 averaging;
  153. u8 pen_down_acc_interval;
  154. struct spi_transfer xfer[AD7877_NR_SENSE + 2];
  155. struct spi_message msg;
  156. struct mutex mutex;
  157. bool disabled; /* P: mutex */
  158. bool gpio3; /* P: mutex */
  159. bool gpio4; /* P: mutex */
  160. spinlock_t lock;
  161. struct timer_list timer; /* P: lock */
  162. /*
  163. * DMA (thus cache coherency maintenance) requires the
  164. * transfer buffers to live in their own cache lines.
  165. */
  166. u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
  167. };
  168. static bool gpio3;
  169. module_param(gpio3, bool, 0);
  170. MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
  171. static int ad7877_read(struct spi_device *spi, u16 reg)
  172. {
  173. struct ser_req *req;
  174. int status, ret;
  175. req = kzalloc(sizeof *req, GFP_KERNEL);
  176. if (!req)
  177. return -ENOMEM;
  178. spi_message_init(&req->msg);
  179. req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
  180. AD7877_READADD(reg));
  181. req->xfer[0].tx_buf = &req->command;
  182. req->xfer[0].len = 2;
  183. req->xfer[0].cs_change = 1;
  184. req->xfer[1].rx_buf = &req->sample;
  185. req->xfer[1].len = 2;
  186. spi_message_add_tail(&req->xfer[0], &req->msg);
  187. spi_message_add_tail(&req->xfer[1], &req->msg);
  188. status = spi_sync(spi, &req->msg);
  189. ret = status ? : req->sample;
  190. kfree(req);
  191. return ret;
  192. }
  193. static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
  194. {
  195. struct ser_req *req;
  196. int status;
  197. req = kzalloc(sizeof *req, GFP_KERNEL);
  198. if (!req)
  199. return -ENOMEM;
  200. spi_message_init(&req->msg);
  201. req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
  202. req->xfer[0].tx_buf = &req->command;
  203. req->xfer[0].len = 2;
  204. spi_message_add_tail(&req->xfer[0], &req->msg);
  205. status = spi_sync(spi, &req->msg);
  206. kfree(req);
  207. return status;
  208. }
  209. static int ad7877_read_adc(struct spi_device *spi, unsigned command)
  210. {
  211. struct ad7877 *ts = spi_get_drvdata(spi);
  212. struct ser_req *req;
  213. int status;
  214. int sample;
  215. int i;
  216. req = kzalloc(sizeof *req, GFP_KERNEL);
  217. if (!req)
  218. return -ENOMEM;
  219. spi_message_init(&req->msg);
  220. /* activate reference, so it has time to settle; */
  221. req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
  222. AD7877_POL(ts->stopacq_polarity) |
  223. AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
  224. AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
  225. req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
  226. req->command = (u16) command;
  227. req->xfer[0].tx_buf = &req->reset;
  228. req->xfer[0].len = 2;
  229. req->xfer[0].cs_change = 1;
  230. req->xfer[1].tx_buf = &req->ref_on;
  231. req->xfer[1].len = 2;
  232. req->xfer[1].delay.value = ts->vref_delay_usecs;
  233. req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
  234. req->xfer[1].cs_change = 1;
  235. req->xfer[2].tx_buf = &req->command;
  236. req->xfer[2].len = 2;
  237. req->xfer[2].delay.value = ts->vref_delay_usecs;
  238. req->xfer[2].delay.unit = SPI_DELAY_UNIT_USECS;
  239. req->xfer[2].cs_change = 1;
  240. req->xfer[3].rx_buf = &req->sample;
  241. req->xfer[3].len = 2;
  242. req->xfer[3].cs_change = 1;
  243. req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/
  244. req->xfer[4].len = 2;
  245. req->xfer[4].cs_change = 1;
  246. req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/
  247. req->xfer[5].len = 2;
  248. /* group all the transfers together, so we can't interfere with
  249. * reading touchscreen state; disable penirq while sampling
  250. */
  251. for (i = 0; i < 6; i++)
  252. spi_message_add_tail(&req->xfer[i], &req->msg);
  253. status = spi_sync(spi, &req->msg);
  254. sample = req->sample;
  255. kfree(req);
  256. return status ? : sample;
  257. }
  258. static int ad7877_process_data(struct ad7877 *ts)
  259. {
  260. struct input_dev *input_dev = ts->input;
  261. unsigned Rt;
  262. u16 x, y, z1, z2;
  263. x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
  264. y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
  265. z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
  266. z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
  267. /*
  268. * The samples processed here are already preprocessed by the AD7877.
  269. * The preprocessing function consists of an averaging filter.
  270. * The combination of 'first conversion delay' and averaging provides a robust solution,
  271. * discarding the spurious noise in the signal and keeping only the data of interest.
  272. * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
  273. * Other user-programmable conversion controls include variable acquisition time,
  274. * and first conversion delay. Up to 16 averages can be taken per conversion.
  275. */
  276. if (likely(x && z1)) {
  277. /* compute touch pressure resistance using equation #1 */
  278. Rt = (z2 - z1) * x * ts->x_plate_ohms;
  279. Rt /= z1;
  280. Rt = (Rt + 2047) >> 12;
  281. /*
  282. * Sample found inconsistent, pressure is beyond
  283. * the maximum. Don't report it to user space.
  284. */
  285. if (Rt > ts->pressure_max)
  286. return -EINVAL;
  287. if (!timer_pending(&ts->timer))
  288. input_report_key(input_dev, BTN_TOUCH, 1);
  289. input_report_abs(input_dev, ABS_X, x);
  290. input_report_abs(input_dev, ABS_Y, y);
  291. input_report_abs(input_dev, ABS_PRESSURE, Rt);
  292. input_sync(input_dev);
  293. return 0;
  294. }
  295. return -EINVAL;
  296. }
  297. static inline void ad7877_ts_event_release(struct ad7877 *ts)
  298. {
  299. struct input_dev *input_dev = ts->input;
  300. input_report_abs(input_dev, ABS_PRESSURE, 0);
  301. input_report_key(input_dev, BTN_TOUCH, 0);
  302. input_sync(input_dev);
  303. }
  304. static void ad7877_timer(struct timer_list *t)
  305. {
  306. struct ad7877 *ts = from_timer(ts, t, timer);
  307. unsigned long flags;
  308. spin_lock_irqsave(&ts->lock, flags);
  309. ad7877_ts_event_release(ts);
  310. spin_unlock_irqrestore(&ts->lock, flags);
  311. }
  312. static irqreturn_t ad7877_irq(int irq, void *handle)
  313. {
  314. struct ad7877 *ts = handle;
  315. unsigned long flags;
  316. int error;
  317. error = spi_sync(ts->spi, &ts->msg);
  318. if (error) {
  319. dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
  320. goto out;
  321. }
  322. spin_lock_irqsave(&ts->lock, flags);
  323. error = ad7877_process_data(ts);
  324. if (!error)
  325. mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
  326. spin_unlock_irqrestore(&ts->lock, flags);
  327. out:
  328. return IRQ_HANDLED;
  329. }
  330. static void ad7877_disable(void *data)
  331. {
  332. struct ad7877 *ts = data;
  333. mutex_lock(&ts->mutex);
  334. if (!ts->disabled) {
  335. ts->disabled = true;
  336. disable_irq(ts->spi->irq);
  337. if (del_timer_sync(&ts->timer))
  338. ad7877_ts_event_release(ts);
  339. }
  340. /*
  341. * We know the chip's in lowpower mode since we always
  342. * leave it that way after every request
  343. */
  344. mutex_unlock(&ts->mutex);
  345. }
  346. static void ad7877_enable(struct ad7877 *ts)
  347. {
  348. mutex_lock(&ts->mutex);
  349. if (ts->disabled) {
  350. ts->disabled = false;
  351. enable_irq(ts->spi->irq);
  352. }
  353. mutex_unlock(&ts->mutex);
  354. }
  355. #define SHOW(name) static ssize_t \
  356. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  357. { \
  358. struct ad7877 *ts = dev_get_drvdata(dev); \
  359. ssize_t v = ad7877_read_adc(ts->spi, \
  360. AD7877_READ_CHAN(name)); \
  361. if (v < 0) \
  362. return v; \
  363. return sprintf(buf, "%u\n", (unsigned) v); \
  364. } \
  365. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  366. SHOW(aux1)
  367. SHOW(aux2)
  368. SHOW(aux3)
  369. SHOW(bat1)
  370. SHOW(bat2)
  371. SHOW(temp1)
  372. SHOW(temp2)
  373. static ssize_t ad7877_disable_show(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. struct ad7877 *ts = dev_get_drvdata(dev);
  377. return sprintf(buf, "%u\n", ts->disabled);
  378. }
  379. static ssize_t ad7877_disable_store(struct device *dev,
  380. struct device_attribute *attr,
  381. const char *buf, size_t count)
  382. {
  383. struct ad7877 *ts = dev_get_drvdata(dev);
  384. unsigned int val;
  385. int error;
  386. error = kstrtouint(buf, 10, &val);
  387. if (error)
  388. return error;
  389. if (val)
  390. ad7877_disable(ts);
  391. else
  392. ad7877_enable(ts);
  393. return count;
  394. }
  395. static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
  396. static ssize_t ad7877_dac_show(struct device *dev,
  397. struct device_attribute *attr, char *buf)
  398. {
  399. struct ad7877 *ts = dev_get_drvdata(dev);
  400. return sprintf(buf, "%u\n", ts->dac);
  401. }
  402. static ssize_t ad7877_dac_store(struct device *dev,
  403. struct device_attribute *attr,
  404. const char *buf, size_t count)
  405. {
  406. struct ad7877 *ts = dev_get_drvdata(dev);
  407. unsigned int val;
  408. int error;
  409. error = kstrtouint(buf, 10, &val);
  410. if (error)
  411. return error;
  412. mutex_lock(&ts->mutex);
  413. ts->dac = val & 0xFF;
  414. ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
  415. mutex_unlock(&ts->mutex);
  416. return count;
  417. }
  418. static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
  419. static ssize_t ad7877_gpio3_show(struct device *dev,
  420. struct device_attribute *attr, char *buf)
  421. {
  422. struct ad7877 *ts = dev_get_drvdata(dev);
  423. return sprintf(buf, "%u\n", ts->gpio3);
  424. }
  425. static ssize_t ad7877_gpio3_store(struct device *dev,
  426. struct device_attribute *attr,
  427. const char *buf, size_t count)
  428. {
  429. struct ad7877 *ts = dev_get_drvdata(dev);
  430. unsigned int val;
  431. int error;
  432. error = kstrtouint(buf, 10, &val);
  433. if (error)
  434. return error;
  435. mutex_lock(&ts->mutex);
  436. ts->gpio3 = !!val;
  437. ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
  438. (ts->gpio4 << 4) | (ts->gpio3 << 5));
  439. mutex_unlock(&ts->mutex);
  440. return count;
  441. }
  442. static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
  443. static ssize_t ad7877_gpio4_show(struct device *dev,
  444. struct device_attribute *attr, char *buf)
  445. {
  446. struct ad7877 *ts = dev_get_drvdata(dev);
  447. return sprintf(buf, "%u\n", ts->gpio4);
  448. }
  449. static ssize_t ad7877_gpio4_store(struct device *dev,
  450. struct device_attribute *attr,
  451. const char *buf, size_t count)
  452. {
  453. struct ad7877 *ts = dev_get_drvdata(dev);
  454. unsigned int val;
  455. int error;
  456. error = kstrtouint(buf, 10, &val);
  457. if (error)
  458. return error;
  459. mutex_lock(&ts->mutex);
  460. ts->gpio4 = !!val;
  461. ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
  462. (ts->gpio4 << 4) | (ts->gpio3 << 5));
  463. mutex_unlock(&ts->mutex);
  464. return count;
  465. }
  466. static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
  467. static struct attribute *ad7877_attributes[] = {
  468. &dev_attr_temp1.attr,
  469. &dev_attr_temp2.attr,
  470. &dev_attr_aux1.attr,
  471. &dev_attr_aux2.attr,
  472. &dev_attr_aux3.attr,
  473. &dev_attr_bat1.attr,
  474. &dev_attr_bat2.attr,
  475. &dev_attr_disable.attr,
  476. &dev_attr_dac.attr,
  477. &dev_attr_gpio3.attr,
  478. &dev_attr_gpio4.attr,
  479. NULL
  480. };
  481. static umode_t ad7877_attr_is_visible(struct kobject *kobj,
  482. struct attribute *attr, int n)
  483. {
  484. umode_t mode = attr->mode;
  485. if (attr == &dev_attr_aux3.attr) {
  486. if (gpio3)
  487. mode = 0;
  488. } else if (attr == &dev_attr_gpio3.attr) {
  489. if (!gpio3)
  490. mode = 0;
  491. }
  492. return mode;
  493. }
  494. static const struct attribute_group ad7877_attr_group = {
  495. .is_visible = ad7877_attr_is_visible,
  496. .attrs = ad7877_attributes,
  497. };
  498. static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
  499. {
  500. struct spi_message *m;
  501. int i;
  502. ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
  503. AD7877_POL(ts->stopacq_polarity) |
  504. AD7877_AVG(ts->averaging) | AD7877_PM(1) |
  505. AD7877_TMR(ts->pen_down_acc_interval) |
  506. AD7877_ACQ(ts->acquisition_time) |
  507. AD7877_FCD(ts->first_conversion_delay);
  508. ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
  509. ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
  510. AD7877_READADD(AD7877_REG_XPLUS-1) |
  511. AD7877_MODE_SEQ1 | AD7877_DFR;
  512. ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
  513. ts->cmd_dummy = 0;
  514. m = &ts->msg;
  515. spi_message_init(m);
  516. m->context = ts;
  517. ts->xfer[0].tx_buf = &ts->cmd_crtl1;
  518. ts->xfer[0].len = 2;
  519. ts->xfer[0].cs_change = 1;
  520. spi_message_add_tail(&ts->xfer[0], m);
  521. ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
  522. ts->xfer[1].len = 2;
  523. ts->xfer[1].cs_change = 1;
  524. spi_message_add_tail(&ts->xfer[1], m);
  525. for (i = 0; i < AD7877_NR_SENSE; i++) {
  526. ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
  527. ts->xfer[i + 2].len = 2;
  528. if (i < (AD7877_NR_SENSE - 1))
  529. ts->xfer[i + 2].cs_change = 1;
  530. spi_message_add_tail(&ts->xfer[i + 2], m);
  531. }
  532. }
  533. static int ad7877_probe(struct spi_device *spi)
  534. {
  535. struct ad7877 *ts;
  536. struct input_dev *input_dev;
  537. struct ad7877_platform_data *pdata = dev_get_platdata(&spi->dev);
  538. int err;
  539. u16 verify;
  540. if (!spi->irq) {
  541. dev_dbg(&spi->dev, "no IRQ?\n");
  542. return -ENODEV;
  543. }
  544. if (!pdata) {
  545. dev_dbg(&spi->dev, "no platform data?\n");
  546. return -ENODEV;
  547. }
  548. /* don't exceed max specified SPI CLK frequency */
  549. if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
  550. dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
  551. return -EINVAL;
  552. }
  553. spi->bits_per_word = 16;
  554. err = spi_setup(spi);
  555. if (err) {
  556. dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
  557. return err;
  558. }
  559. ts = devm_kzalloc(&spi->dev, sizeof(struct ad7877), GFP_KERNEL);
  560. if (!ts)
  561. return -ENOMEM;
  562. input_dev = devm_input_allocate_device(&spi->dev);
  563. if (!input_dev)
  564. return -ENOMEM;
  565. err = devm_add_action_or_reset(&spi->dev, ad7877_disable, ts);
  566. if (err)
  567. return err;
  568. spi_set_drvdata(spi, ts);
  569. ts->spi = spi;
  570. ts->input = input_dev;
  571. timer_setup(&ts->timer, ad7877_timer, 0);
  572. mutex_init(&ts->mutex);
  573. spin_lock_init(&ts->lock);
  574. ts->model = pdata->model ? : 7877;
  575. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  576. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  577. ts->pressure_max = pdata->pressure_max ? : ~0;
  578. ts->stopacq_polarity = pdata->stopacq_polarity;
  579. ts->first_conversion_delay = pdata->first_conversion_delay;
  580. ts->acquisition_time = pdata->acquisition_time;
  581. ts->averaging = pdata->averaging;
  582. ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
  583. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
  584. input_dev->name = "AD7877 Touchscreen";
  585. input_dev->phys = ts->phys;
  586. input_dev->dev.parent = &spi->dev;
  587. __set_bit(EV_KEY, input_dev->evbit);
  588. __set_bit(BTN_TOUCH, input_dev->keybit);
  589. __set_bit(EV_ABS, input_dev->evbit);
  590. __set_bit(ABS_X, input_dev->absbit);
  591. __set_bit(ABS_Y, input_dev->absbit);
  592. __set_bit(ABS_PRESSURE, input_dev->absbit);
  593. input_set_abs_params(input_dev, ABS_X,
  594. pdata->x_min ? : 0,
  595. pdata->x_max ? : MAX_12BIT,
  596. 0, 0);
  597. input_set_abs_params(input_dev, ABS_Y,
  598. pdata->y_min ? : 0,
  599. pdata->y_max ? : MAX_12BIT,
  600. 0, 0);
  601. input_set_abs_params(input_dev, ABS_PRESSURE,
  602. pdata->pressure_min, pdata->pressure_max, 0, 0);
  603. ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
  604. verify = ad7877_read(spi, AD7877_REG_SEQ1);
  605. if (verify != AD7877_MM_SEQUENCE) {
  606. dev_err(&spi->dev, "%s: Failed to probe %s\n",
  607. dev_name(&spi->dev), input_dev->name);
  608. return -ENODEV;
  609. }
  610. if (gpio3)
  611. ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
  612. ad7877_setup_ts_def_msg(spi, ts);
  613. /* Request AD7877 /DAV GPIO interrupt */
  614. err = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, ad7877_irq,
  615. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  616. spi->dev.driver->name, ts);
  617. if (err) {
  618. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  619. return err;
  620. }
  621. err = devm_device_add_group(&spi->dev, &ad7877_attr_group);
  622. if (err)
  623. return err;
  624. err = input_register_device(input_dev);
  625. if (err)
  626. return err;
  627. return 0;
  628. }
  629. static int __maybe_unused ad7877_suspend(struct device *dev)
  630. {
  631. struct ad7877 *ts = dev_get_drvdata(dev);
  632. ad7877_disable(ts);
  633. return 0;
  634. }
  635. static int __maybe_unused ad7877_resume(struct device *dev)
  636. {
  637. struct ad7877 *ts = dev_get_drvdata(dev);
  638. ad7877_enable(ts);
  639. return 0;
  640. }
  641. static SIMPLE_DEV_PM_OPS(ad7877_pm, ad7877_suspend, ad7877_resume);
  642. static struct spi_driver ad7877_driver = {
  643. .driver = {
  644. .name = "ad7877",
  645. .pm = &ad7877_pm,
  646. },
  647. .probe = ad7877_probe,
  648. };
  649. module_spi_driver(ad7877_driver);
  650. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  651. MODULE_DESCRIPTION("AD7877 touchscreen Driver");
  652. MODULE_LICENSE("GPL");
  653. MODULE_ALIAS("spi:ad7877");