ad7877.c 21 KB

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