ad7877.c 22 KB

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