ad7877.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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(50)
  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. u16 sample;
  143. struct spi_message msg;
  144. struct spi_transfer xfer[6];
  145. };
  146. struct ad7877 {
  147. struct input_dev *input;
  148. char phys[32];
  149. struct spi_device *spi;
  150. u16 model;
  151. u16 vref_delay_usecs;
  152. u16 x_plate_ohms;
  153. u16 pressure_max;
  154. u16 cmd_crtl1;
  155. u16 cmd_crtl2;
  156. u16 cmd_dummy;
  157. u16 dac;
  158. u8 stopacq_polarity;
  159. u8 first_conversion_delay;
  160. u8 acquisition_time;
  161. u8 averaging;
  162. u8 pen_down_acc_interval;
  163. u16 conversion_data[AD7877_NR_SENSE];
  164. struct spi_transfer xfer[AD7877_NR_SENSE + 2];
  165. struct spi_message msg;
  166. struct mutex mutex;
  167. unsigned disabled:1; /* P: mutex */
  168. unsigned gpio3:1; /* P: mutex */
  169. unsigned gpio4:1; /* P: mutex */
  170. spinlock_t lock;
  171. struct timer_list timer; /* P: lock */
  172. unsigned pending:1; /* P: lock */
  173. };
  174. static int gpio3;
  175. module_param(gpio3, int, 0);
  176. MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
  177. /*
  178. * ad7877_read/write are only used for initial setup and for sysfs controls.
  179. * The main traffic is done using spi_async() in the interrupt handler.
  180. */
  181. static int ad7877_read(struct spi_device *spi, u16 reg)
  182. {
  183. struct ser_req *req;
  184. int status, ret;
  185. req = kzalloc(sizeof *req, GFP_KERNEL);
  186. if (!req)
  187. return -ENOMEM;
  188. spi_message_init(&req->msg);
  189. req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
  190. AD7877_READADD(reg));
  191. req->xfer[0].tx_buf = &req->command;
  192. req->xfer[0].len = 2;
  193. req->xfer[1].rx_buf = &req->sample;
  194. req->xfer[1].len = 2;
  195. spi_message_add_tail(&req->xfer[0], &req->msg);
  196. spi_message_add_tail(&req->xfer[1], &req->msg);
  197. status = spi_sync(spi, &req->msg);
  198. ret = status ? : req->sample;
  199. kfree(req);
  200. return ret;
  201. }
  202. static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
  203. {
  204. struct ser_req *req;
  205. int status;
  206. req = kzalloc(sizeof *req, GFP_KERNEL);
  207. if (!req)
  208. return -ENOMEM;
  209. spi_message_init(&req->msg);
  210. req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
  211. req->xfer[0].tx_buf = &req->command;
  212. req->xfer[0].len = 2;
  213. spi_message_add_tail(&req->xfer[0], &req->msg);
  214. status = spi_sync(spi, &req->msg);
  215. kfree(req);
  216. return status;
  217. }
  218. static int ad7877_read_adc(struct spi_device *spi, unsigned command)
  219. {
  220. struct ad7877 *ts = dev_get_drvdata(&spi->dev);
  221. struct ser_req *req;
  222. int status;
  223. int sample;
  224. int i;
  225. req = kzalloc(sizeof *req, GFP_KERNEL);
  226. if (!req)
  227. return -ENOMEM;
  228. spi_message_init(&req->msg);
  229. /* activate reference, so it has time to settle; */
  230. req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
  231. AD7877_POL(ts->stopacq_polarity) |
  232. AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
  233. AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
  234. req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
  235. req->command = (u16) command;
  236. req->xfer[0].tx_buf = &req->reset;
  237. req->xfer[0].len = 2;
  238. req->xfer[1].tx_buf = &req->ref_on;
  239. req->xfer[1].len = 2;
  240. req->xfer[1].delay_usecs = ts->vref_delay_usecs;
  241. req->xfer[2].tx_buf = &req->command;
  242. req->xfer[2].len = 2;
  243. req->xfer[2].delay_usecs = ts->vref_delay_usecs;
  244. req->xfer[3].rx_buf = &req->sample;
  245. req->xfer[3].len = 2;
  246. req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/
  247. req->xfer[4].len = 2;
  248. req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/
  249. req->xfer[5].len = 2;
  250. /* group all the transfers together, so we can't interfere with
  251. * reading touchscreen state; disable penirq while sampling
  252. */
  253. for (i = 0; i < 6; i++)
  254. spi_message_add_tail(&req->xfer[i], &req->msg);
  255. status = spi_sync(spi, &req->msg);
  256. sample = req->sample;
  257. kfree(req);
  258. return status ? : sample;
  259. }
  260. static void ad7877_rx(struct ad7877 *ts)
  261. {
  262. struct input_dev *input_dev = ts->input;
  263. unsigned Rt;
  264. u16 x, y, z1, z2;
  265. x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
  266. y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
  267. z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
  268. z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
  269. /*
  270. * The samples processed here are already preprocessed by the AD7877.
  271. * The preprocessing function consists of an averaging filter.
  272. * The combination of 'first conversion delay' and averaging provides a robust solution,
  273. * discarding the spurious noise in the signal and keeping only the data of interest.
  274. * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
  275. * Other user-programmable conversion controls include variable acquisition time,
  276. * and first conversion delay. Up to 16 averages can be taken per conversion.
  277. */
  278. if (likely(x && z1)) {
  279. /* compute touch pressure resistance using equation #1 */
  280. Rt = (z2 - z1) * x * ts->x_plate_ohms;
  281. Rt /= z1;
  282. Rt = (Rt + 2047) >> 12;
  283. input_report_abs(input_dev, ABS_X, x);
  284. input_report_abs(input_dev, ABS_Y, y);
  285. input_report_abs(input_dev, ABS_PRESSURE, Rt);
  286. input_sync(input_dev);
  287. }
  288. }
  289. static inline void ad7877_ts_event_release(struct ad7877 *ts)
  290. {
  291. struct input_dev *input_dev = ts->input;
  292. input_report_abs(input_dev, ABS_PRESSURE, 0);
  293. input_sync(input_dev);
  294. }
  295. static void ad7877_timer(unsigned long handle)
  296. {
  297. struct ad7877 *ts = (void *)handle;
  298. ad7877_ts_event_release(ts);
  299. }
  300. static irqreturn_t ad7877_irq(int irq, void *handle)
  301. {
  302. struct ad7877 *ts = handle;
  303. unsigned long flags;
  304. int status;
  305. /*
  306. * The repeated conversion sequencer controlled by TMR kicked off
  307. * too fast. We ignore the last and process the sample sequence
  308. * currently in the queue. It can't be older than 9.4ms, and we
  309. * need to avoid that ts->msg doesn't get issued twice while in work.
  310. */
  311. spin_lock_irqsave(&ts->lock, flags);
  312. if (!ts->pending) {
  313. ts->pending = 1;
  314. status = spi_async(ts->spi, &ts->msg);
  315. if (status)
  316. dev_err(&ts->spi->dev, "spi_sync --> %d\n", status);
  317. }
  318. spin_unlock_irqrestore(&ts->lock, flags);
  319. return IRQ_HANDLED;
  320. }
  321. static void ad7877_callback(void *_ts)
  322. {
  323. struct ad7877 *ts = _ts;
  324. spin_lock_irq(&ts->lock);
  325. ad7877_rx(ts);
  326. ts->pending = 0;
  327. mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
  328. spin_unlock_irq(&ts->lock);
  329. }
  330. static void ad7877_disable(struct ad7877 *ts)
  331. {
  332. mutex_lock(&ts->mutex);
  333. if (!ts->disabled) {
  334. ts->disabled = 1;
  335. disable_irq(ts->spi->irq);
  336. /* Wait for spi_async callback */
  337. while (ts->pending)
  338. msleep(1);
  339. if (del_timer_sync(&ts->timer))
  340. ad7877_ts_event_release(ts);
  341. }
  342. /* we know the chip's in lowpower mode since we always
  343. * leave it that way after every request
  344. */
  345. mutex_unlock(&ts->mutex);
  346. }
  347. static void ad7877_enable(struct ad7877 *ts)
  348. {
  349. mutex_lock(&ts->mutex);
  350. if (ts->disabled) {
  351. ts->disabled = 0;
  352. enable_irq(ts->spi->irq);
  353. }
  354. mutex_unlock(&ts->mutex);
  355. }
  356. #define SHOW(name) static ssize_t \
  357. name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
  358. { \
  359. struct ad7877 *ts = dev_get_drvdata(dev); \
  360. ssize_t v = ad7877_read_adc(ts->spi, \
  361. AD7877_READ_CHAN(name)); \
  362. if (v < 0) \
  363. return v; \
  364. return sprintf(buf, "%u\n", (unsigned) v); \
  365. } \
  366. static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
  367. SHOW(aux1)
  368. SHOW(aux2)
  369. SHOW(aux3)
  370. SHOW(bat1)
  371. SHOW(bat2)
  372. SHOW(temp1)
  373. SHOW(temp2)
  374. static ssize_t ad7877_disable_show(struct device *dev,
  375. struct device_attribute *attr, char *buf)
  376. {
  377. struct ad7877 *ts = dev_get_drvdata(dev);
  378. return sprintf(buf, "%u\n", ts->disabled);
  379. }
  380. static ssize_t ad7877_disable_store(struct device *dev,
  381. struct device_attribute *attr,
  382. const char *buf, size_t count)
  383. {
  384. struct ad7877 *ts = dev_get_drvdata(dev);
  385. unsigned long val;
  386. int error;
  387. error = strict_strtoul(buf, 10, &val);
  388. if (error)
  389. return error;
  390. if (val)
  391. ad7877_disable(ts);
  392. else
  393. ad7877_enable(ts);
  394. return count;
  395. }
  396. static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
  397. static ssize_t ad7877_dac_show(struct device *dev,
  398. struct device_attribute *attr, char *buf)
  399. {
  400. struct ad7877 *ts = dev_get_drvdata(dev);
  401. return sprintf(buf, "%u\n", ts->dac);
  402. }
  403. static ssize_t ad7877_dac_store(struct device *dev,
  404. struct device_attribute *attr,
  405. const char *buf, size_t count)
  406. {
  407. struct ad7877 *ts = dev_get_drvdata(dev);
  408. unsigned long val;
  409. int error;
  410. error = strict_strtoul(buf, 10, &val);
  411. if (error)
  412. return error;
  413. mutex_lock(&ts->mutex);
  414. ts->dac = val & 0xFF;
  415. ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
  416. mutex_unlock(&ts->mutex);
  417. return count;
  418. }
  419. static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
  420. static ssize_t ad7877_gpio3_show(struct device *dev,
  421. struct device_attribute *attr, char *buf)
  422. {
  423. struct ad7877 *ts = dev_get_drvdata(dev);
  424. return sprintf(buf, "%u\n", ts->gpio3);
  425. }
  426. static ssize_t ad7877_gpio3_store(struct device *dev,
  427. struct device_attribute *attr,
  428. const char *buf, size_t count)
  429. {
  430. struct ad7877 *ts = dev_get_drvdata(dev);
  431. unsigned long val;
  432. int error;
  433. error = strict_strtoul(buf, 10, &val);
  434. if (error)
  435. return error;
  436. mutex_lock(&ts->mutex);
  437. ts->gpio3 = !!val;
  438. ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
  439. (ts->gpio4 << 4) | (ts->gpio3 << 5));
  440. mutex_unlock(&ts->mutex);
  441. return count;
  442. }
  443. static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
  444. static ssize_t ad7877_gpio4_show(struct device *dev,
  445. struct device_attribute *attr, char *buf)
  446. {
  447. struct ad7877 *ts = dev_get_drvdata(dev);
  448. return sprintf(buf, "%u\n", ts->gpio4);
  449. }
  450. static ssize_t ad7877_gpio4_store(struct device *dev,
  451. struct device_attribute *attr,
  452. const char *buf, size_t count)
  453. {
  454. struct ad7877 *ts = dev_get_drvdata(dev);
  455. unsigned long val;
  456. int error;
  457. error = strict_strtoul(buf, 10, &val);
  458. if (error)
  459. return error;
  460. mutex_lock(&ts->mutex);
  461. ts->gpio4 = !!val;
  462. ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
  463. (ts->gpio4 << 4) | (ts->gpio3 << 5));
  464. mutex_unlock(&ts->mutex);
  465. return count;
  466. }
  467. static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
  468. static struct attribute *ad7877_attributes[] = {
  469. &dev_attr_temp1.attr,
  470. &dev_attr_temp2.attr,
  471. &dev_attr_aux1.attr,
  472. &dev_attr_aux2.attr,
  473. &dev_attr_bat1.attr,
  474. &dev_attr_bat2.attr,
  475. &dev_attr_disable.attr,
  476. &dev_attr_dac.attr,
  477. &dev_attr_gpio4.attr,
  478. NULL
  479. };
  480. static const struct attribute_group ad7877_attr_group = {
  481. .attrs = ad7877_attributes,
  482. };
  483. static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
  484. {
  485. struct spi_message *m;
  486. int i;
  487. ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
  488. AD7877_POL(ts->stopacq_polarity) |
  489. AD7877_AVG(ts->averaging) | AD7877_PM(1) |
  490. AD7877_TMR(ts->pen_down_acc_interval) |
  491. AD7877_ACQ(ts->acquisition_time) |
  492. AD7877_FCD(ts->first_conversion_delay);
  493. ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
  494. ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
  495. AD7877_READADD(AD7877_REG_XPLUS-1) |
  496. AD7877_MODE_SEQ1 | AD7877_DFR;
  497. ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
  498. ts->cmd_dummy = 0;
  499. m = &ts->msg;
  500. spi_message_init(m);
  501. m->complete = ad7877_callback;
  502. m->context = ts;
  503. ts->xfer[0].tx_buf = &ts->cmd_crtl1;
  504. ts->xfer[0].len = 2;
  505. spi_message_add_tail(&ts->xfer[0], m);
  506. ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
  507. ts->xfer[1].len = 2;
  508. spi_message_add_tail(&ts->xfer[1], m);
  509. for (i = 0; i < 11; i++) {
  510. ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
  511. ts->xfer[i + 2].len = 2;
  512. spi_message_add_tail(&ts->xfer[i + 2], m);
  513. }
  514. }
  515. static int __devinit ad7877_probe(struct spi_device *spi)
  516. {
  517. struct ad7877 *ts;
  518. struct input_dev *input_dev;
  519. struct ad7877_platform_data *pdata = spi->dev.platform_data;
  520. int err;
  521. u16 verify;
  522. if (!spi->irq) {
  523. dev_dbg(&spi->dev, "no IRQ?\n");
  524. return -ENODEV;
  525. }
  526. if (!pdata) {
  527. dev_dbg(&spi->dev, "no platform data?\n");
  528. return -ENODEV;
  529. }
  530. /* don't exceed max specified SPI CLK frequency */
  531. if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
  532. dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
  533. return -EINVAL;
  534. }
  535. ts = kzalloc(sizeof(struct ad7877), GFP_KERNEL);
  536. input_dev = input_allocate_device();
  537. if (!ts || !input_dev) {
  538. err = -ENOMEM;
  539. goto err_free_mem;
  540. }
  541. dev_set_drvdata(&spi->dev, ts);
  542. ts->spi = spi;
  543. ts->input = input_dev;
  544. setup_timer(&ts->timer, ad7877_timer, (unsigned long) ts);
  545. mutex_init(&ts->mutex);
  546. spin_lock_init(&ts->lock);
  547. ts->model = pdata->model ? : 7877;
  548. ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
  549. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  550. ts->pressure_max = pdata->pressure_max ? : ~0;
  551. ts->stopacq_polarity = pdata->stopacq_polarity;
  552. ts->first_conversion_delay = pdata->first_conversion_delay;
  553. ts->acquisition_time = pdata->acquisition_time;
  554. ts->averaging = pdata->averaging;
  555. ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
  556. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
  557. input_dev->name = "AD7877 Touchscreen";
  558. input_dev->phys = ts->phys;
  559. input_dev->dev.parent = &spi->dev;
  560. __set_bit(EV_ABS, input_dev->evbit);
  561. __set_bit(ABS_X, input_dev->absbit);
  562. __set_bit(ABS_Y, input_dev->absbit);
  563. __set_bit(ABS_PRESSURE, input_dev->absbit);
  564. input_set_abs_params(input_dev, ABS_X,
  565. pdata->x_min ? : 0,
  566. pdata->x_max ? : MAX_12BIT,
  567. 0, 0);
  568. input_set_abs_params(input_dev, ABS_Y,
  569. pdata->y_min ? : 0,
  570. pdata->y_max ? : MAX_12BIT,
  571. 0, 0);
  572. input_set_abs_params(input_dev, ABS_PRESSURE,
  573. pdata->pressure_min, pdata->pressure_max, 0, 0);
  574. ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
  575. verify = ad7877_read(spi, AD7877_REG_SEQ1);
  576. if (verify != AD7877_MM_SEQUENCE){
  577. dev_err(&spi->dev, "%s: Failed to probe %s\n",
  578. dev_name(&spi->dev), input_dev->name);
  579. err = -ENODEV;
  580. goto err_free_mem;
  581. }
  582. if (gpio3)
  583. ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
  584. ad7877_setup_ts_def_msg(spi, ts);
  585. /* Request AD7877 /DAV GPIO interrupt */
  586. err = request_irq(spi->irq, ad7877_irq, IRQF_TRIGGER_FALLING |
  587. IRQF_SAMPLE_RANDOM, spi->dev.driver->name, ts);
  588. if (err) {
  589. dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
  590. goto err_free_mem;
  591. }
  592. err = sysfs_create_group(&spi->dev.kobj, &ad7877_attr_group);
  593. if (err)
  594. goto err_free_irq;
  595. err = device_create_file(&spi->dev,
  596. gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3);
  597. if (err)
  598. goto err_remove_attr_group;
  599. err = input_register_device(input_dev);
  600. if (err)
  601. goto err_remove_attr;
  602. return 0;
  603. err_remove_attr:
  604. device_remove_file(&spi->dev,
  605. gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3);
  606. err_remove_attr_group:
  607. sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group);
  608. err_free_irq:
  609. free_irq(spi->irq, ts);
  610. err_free_mem:
  611. input_free_device(input_dev);
  612. kfree(ts);
  613. dev_set_drvdata(&spi->dev, NULL);
  614. return err;
  615. }
  616. static int __devexit ad7877_remove(struct spi_device *spi)
  617. {
  618. struct ad7877 *ts = dev_get_drvdata(&spi->dev);
  619. sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group);
  620. device_remove_file(&spi->dev,
  621. gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3);
  622. ad7877_disable(ts);
  623. free_irq(ts->spi->irq, ts);
  624. input_unregister_device(ts->input);
  625. kfree(ts);
  626. dev_dbg(&spi->dev, "unregistered touchscreen\n");
  627. dev_set_drvdata(&spi->dev, NULL);
  628. return 0;
  629. }
  630. #ifdef CONFIG_PM
  631. static int ad7877_suspend(struct spi_device *spi, pm_message_t message)
  632. {
  633. struct ad7877 *ts = dev_get_drvdata(&spi->dev);
  634. ad7877_disable(ts);
  635. return 0;
  636. }
  637. static int ad7877_resume(struct spi_device *spi)
  638. {
  639. struct ad7877 *ts = dev_get_drvdata(&spi->dev);
  640. ad7877_enable(ts);
  641. return 0;
  642. }
  643. #else
  644. #define ad7877_suspend NULL
  645. #define ad7877_resume NULL
  646. #endif
  647. static struct spi_driver ad7877_driver = {
  648. .driver = {
  649. .name = "ad7877",
  650. .bus = &spi_bus_type,
  651. .owner = THIS_MODULE,
  652. },
  653. .probe = ad7877_probe,
  654. .remove = __devexit_p(ad7877_remove),
  655. .suspend = ad7877_suspend,
  656. .resume = ad7877_resume,
  657. };
  658. static int __init ad7877_init(void)
  659. {
  660. return spi_register_driver(&ad7877_driver);
  661. }
  662. module_init(ad7877_init);
  663. static void __exit ad7877_exit(void)
  664. {
  665. spi_unregister_driver(&ad7877_driver);
  666. }
  667. module_exit(ad7877_exit);
  668. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  669. MODULE_DESCRIPTION("AD7877 touchscreen Driver");
  670. MODULE_LICENSE("GPL");