ad7879.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * Copyright (C) 2008-2009 Michael Hennerich, Analog Devices Inc.
  3. *
  4. * Description: AD7879/AD7889 based touchscreen, and GPIO driver
  5. * (I2C/SPI Interface)
  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. * - ad7877.c
  37. * Copyright (C) 2006-2008 Analog Devices Inc.
  38. */
  39. #include <linux/device.h>
  40. #include <linux/init.h>
  41. #include <linux/delay.h>
  42. #include <linux/input.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/irq.h>
  45. #include <linux/slab.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/spi/spi.h>
  48. #include <linux/i2c.h>
  49. #include <linux/gpio.h>
  50. #include <linux/spi/ad7879.h>
  51. #define AD7879_REG_ZEROS 0
  52. #define AD7879_REG_CTRL1 1
  53. #define AD7879_REG_CTRL2 2
  54. #define AD7879_REG_CTRL3 3
  55. #define AD7879_REG_AUX1HIGH 4
  56. #define AD7879_REG_AUX1LOW 5
  57. #define AD7879_REG_TEMP1HIGH 6
  58. #define AD7879_REG_TEMP1LOW 7
  59. #define AD7879_REG_XPLUS 8
  60. #define AD7879_REG_YPLUS 9
  61. #define AD7879_REG_Z1 10
  62. #define AD7879_REG_Z2 11
  63. #define AD7879_REG_AUXVBAT 12
  64. #define AD7879_REG_TEMP 13
  65. #define AD7879_REG_REVID 14
  66. /* Control REG 1 */
  67. #define AD7879_TMR(x) ((x & 0xFF) << 0)
  68. #define AD7879_ACQ(x) ((x & 0x3) << 8)
  69. #define AD7879_MODE_NOC (0 << 10) /* Do not convert */
  70. #define AD7879_MODE_SCC (1 << 10) /* Single channel conversion */
  71. #define AD7879_MODE_SEQ0 (2 << 10) /* Sequence 0 in Slave Mode */
  72. #define AD7879_MODE_SEQ1 (3 << 10) /* Sequence 1 in Master Mode */
  73. #define AD7879_MODE_INT (1 << 15) /* PENIRQ disabled INT enabled */
  74. /* Control REG 2 */
  75. #define AD7879_FCD(x) ((x & 0x3) << 0)
  76. #define AD7879_RESET (1 << 4)
  77. #define AD7879_MFS(x) ((x & 0x3) << 5)
  78. #define AD7879_AVG(x) ((x & 0x3) << 7)
  79. #define AD7879_SER (1 << 9) /* non-differential */
  80. #define AD7879_DFR (0 << 9) /* differential */
  81. #define AD7879_GPIOPOL (1 << 10)
  82. #define AD7879_GPIODIR (1 << 11)
  83. #define AD7879_GPIO_DATA (1 << 12)
  84. #define AD7879_GPIO_EN (1 << 13)
  85. #define AD7879_PM(x) ((x & 0x3) << 14)
  86. #define AD7879_PM_SHUTDOWN (0)
  87. #define AD7879_PM_DYN (1)
  88. #define AD7879_PM_FULLON (2)
  89. /* Control REG 3 */
  90. #define AD7879_TEMPMASK_BIT (1<<15)
  91. #define AD7879_AUXVBATMASK_BIT (1<<14)
  92. #define AD7879_INTMODE_BIT (1<<13)
  93. #define AD7879_GPIOALERTMASK_BIT (1<<12)
  94. #define AD7879_AUXLOW_BIT (1<<11)
  95. #define AD7879_AUXHIGH_BIT (1<<10)
  96. #define AD7879_TEMPLOW_BIT (1<<9)
  97. #define AD7879_TEMPHIGH_BIT (1<<8)
  98. #define AD7879_YPLUS_BIT (1<<7)
  99. #define AD7879_XPLUS_BIT (1<<6)
  100. #define AD7879_Z1_BIT (1<<5)
  101. #define AD7879_Z2_BIT (1<<4)
  102. #define AD7879_AUX_BIT (1<<3)
  103. #define AD7879_VBAT_BIT (1<<2)
  104. #define AD7879_TEMP_BIT (1<<1)
  105. enum {
  106. AD7879_SEQ_XPOS = 0,
  107. AD7879_SEQ_YPOS = 1,
  108. AD7879_SEQ_Z1 = 2,
  109. AD7879_SEQ_Z2 = 3,
  110. AD7879_NR_SENSE = 4,
  111. };
  112. #define MAX_12BIT ((1<<12)-1)
  113. #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50)
  114. #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
  115. #define AD7879_DEVID 0x7A
  116. typedef struct spi_device bus_device;
  117. #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
  118. #define AD7879_DEVID 0x79
  119. typedef struct i2c_client bus_device;
  120. #endif
  121. struct ad7879 {
  122. bus_device *bus;
  123. struct input_dev *input;
  124. struct work_struct work;
  125. struct timer_list timer;
  126. #ifdef CONFIG_GPIOLIB
  127. struct gpio_chip gc;
  128. #endif
  129. struct mutex mutex;
  130. unsigned disabled:1; /* P: mutex */
  131. #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
  132. struct spi_message msg;
  133. struct spi_transfer xfer[AD7879_NR_SENSE + 1];
  134. u16 cmd;
  135. #endif
  136. u16 conversion_data[AD7879_NR_SENSE];
  137. char phys[32];
  138. u8 first_conversion_delay;
  139. u8 acquisition_time;
  140. u8 averaging;
  141. u8 pen_down_acc_interval;
  142. u8 median;
  143. u16 x_plate_ohms;
  144. u16 pressure_max;
  145. u16 cmd_crtl1;
  146. u16 cmd_crtl2;
  147. u16 cmd_crtl3;
  148. };
  149. static int ad7879_read(bus_device *, u8);
  150. static int ad7879_write(bus_device *, u8, u16);
  151. static void ad7879_collect(struct ad7879 *);
  152. static void ad7879_report(struct ad7879 *ts)
  153. {
  154. struct input_dev *input_dev = ts->input;
  155. unsigned Rt;
  156. u16 x, y, z1, z2;
  157. x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT;
  158. y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT;
  159. z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT;
  160. z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT;
  161. /*
  162. * The samples processed here are already preprocessed by the AD7879.
  163. * The preprocessing function consists of a median and an averaging filter.
  164. * The combination of these two techniques provides a robust solution,
  165. * discarding the spurious noise in the signal and keeping only the data of interest.
  166. * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h)
  167. * Other user-programmable conversion controls include variable acquisition time,
  168. * and first conversion delay. Up to 16 averages can be taken per conversion.
  169. */
  170. if (likely(x && z1)) {
  171. /* compute touch pressure resistance using equation #1 */
  172. Rt = (z2 - z1) * x * ts->x_plate_ohms;
  173. Rt /= z1;
  174. Rt = (Rt + 2047) >> 12;
  175. input_report_abs(input_dev, ABS_X, x);
  176. input_report_abs(input_dev, ABS_Y, y);
  177. input_report_abs(input_dev, ABS_PRESSURE, Rt);
  178. input_sync(input_dev);
  179. }
  180. }
  181. static void ad7879_work(struct work_struct *work)
  182. {
  183. struct ad7879 *ts = container_of(work, struct ad7879, work);
  184. /* use keventd context to read the result registers */
  185. ad7879_collect(ts);
  186. ad7879_report(ts);
  187. mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
  188. }
  189. static void ad7879_ts_event_release(struct ad7879 *ts)
  190. {
  191. struct input_dev *input_dev = ts->input;
  192. input_report_abs(input_dev, ABS_PRESSURE, 0);
  193. input_sync(input_dev);
  194. }
  195. static void ad7879_timer(unsigned long handle)
  196. {
  197. struct ad7879 *ts = (void *)handle;
  198. ad7879_ts_event_release(ts);
  199. }
  200. static irqreturn_t ad7879_irq(int irq, void *handle)
  201. {
  202. struct ad7879 *ts = handle;
  203. /* The repeated conversion sequencer controlled by TMR kicked off too fast.
  204. * We ignore the last and process the sample sequence currently in the queue.
  205. * It can't be older than 9.4ms
  206. */
  207. if (!work_pending(&ts->work))
  208. schedule_work(&ts->work);
  209. return IRQ_HANDLED;
  210. }
  211. static void ad7879_setup(struct ad7879 *ts)
  212. {
  213. ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
  214. ad7879_write(ts->bus, AD7879_REG_CTRL3, ts->cmd_crtl3);
  215. ad7879_write(ts->bus, AD7879_REG_CTRL1, ts->cmd_crtl1);
  216. }
  217. static void ad7879_disable(struct ad7879 *ts)
  218. {
  219. mutex_lock(&ts->mutex);
  220. if (!ts->disabled) {
  221. ts->disabled = 1;
  222. disable_irq(ts->bus->irq);
  223. cancel_work_sync(&ts->work);
  224. if (del_timer_sync(&ts->timer))
  225. ad7879_ts_event_release(ts);
  226. ad7879_write(ts->bus, AD7879_REG_CTRL2,
  227. AD7879_PM(AD7879_PM_SHUTDOWN));
  228. }
  229. mutex_unlock(&ts->mutex);
  230. }
  231. static void ad7879_enable(struct ad7879 *ts)
  232. {
  233. mutex_lock(&ts->mutex);
  234. if (ts->disabled) {
  235. ad7879_setup(ts);
  236. ts->disabled = 0;
  237. enable_irq(ts->bus->irq);
  238. }
  239. mutex_unlock(&ts->mutex);
  240. }
  241. static ssize_t ad7879_disable_show(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. struct ad7879 *ts = dev_get_drvdata(dev);
  245. return sprintf(buf, "%u\n", ts->disabled);
  246. }
  247. static ssize_t ad7879_disable_store(struct device *dev,
  248. struct device_attribute *attr,
  249. const char *buf, size_t count)
  250. {
  251. struct ad7879 *ts = dev_get_drvdata(dev);
  252. unsigned long val;
  253. int error;
  254. error = strict_strtoul(buf, 10, &val);
  255. if (error)
  256. return error;
  257. if (val)
  258. ad7879_disable(ts);
  259. else
  260. ad7879_enable(ts);
  261. return count;
  262. }
  263. static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
  264. static struct attribute *ad7879_attributes[] = {
  265. &dev_attr_disable.attr,
  266. NULL
  267. };
  268. static const struct attribute_group ad7879_attr_group = {
  269. .attrs = ad7879_attributes,
  270. };
  271. #ifdef CONFIG_GPIOLIB
  272. static int ad7879_gpio_direction_input(struct gpio_chip *chip,
  273. unsigned gpio)
  274. {
  275. struct ad7879 *ts = container_of(chip, struct ad7879, gc);
  276. int err;
  277. mutex_lock(&ts->mutex);
  278. ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
  279. err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
  280. mutex_unlock(&ts->mutex);
  281. return err;
  282. }
  283. static int ad7879_gpio_direction_output(struct gpio_chip *chip,
  284. unsigned gpio, int level)
  285. {
  286. struct ad7879 *ts = container_of(chip, struct ad7879, gc);
  287. int err;
  288. mutex_lock(&ts->mutex);
  289. ts->cmd_crtl2 &= ~AD7879_GPIODIR;
  290. ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL;
  291. if (level)
  292. ts->cmd_crtl2 |= AD7879_GPIO_DATA;
  293. else
  294. ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
  295. err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
  296. mutex_unlock(&ts->mutex);
  297. return err;
  298. }
  299. static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  300. {
  301. struct ad7879 *ts = container_of(chip, struct ad7879, gc);
  302. u16 val;
  303. mutex_lock(&ts->mutex);
  304. val = ad7879_read(ts->bus, AD7879_REG_CTRL2);
  305. mutex_unlock(&ts->mutex);
  306. return !!(val & AD7879_GPIO_DATA);
  307. }
  308. static void ad7879_gpio_set_value(struct gpio_chip *chip,
  309. unsigned gpio, int value)
  310. {
  311. struct ad7879 *ts = container_of(chip, struct ad7879, gc);
  312. mutex_lock(&ts->mutex);
  313. if (value)
  314. ts->cmd_crtl2 |= AD7879_GPIO_DATA;
  315. else
  316. ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
  317. ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
  318. mutex_unlock(&ts->mutex);
  319. }
  320. static int __devinit ad7879_gpio_add(struct device *dev)
  321. {
  322. struct ad7879 *ts = dev_get_drvdata(dev);
  323. struct ad7879_platform_data *pdata = dev->platform_data;
  324. int ret = 0;
  325. if (pdata->gpio_export) {
  326. ts->gc.direction_input = ad7879_gpio_direction_input;
  327. ts->gc.direction_output = ad7879_gpio_direction_output;
  328. ts->gc.get = ad7879_gpio_get_value;
  329. ts->gc.set = ad7879_gpio_set_value;
  330. ts->gc.can_sleep = 1;
  331. ts->gc.base = pdata->gpio_base;
  332. ts->gc.ngpio = 1;
  333. ts->gc.label = "AD7879-GPIO";
  334. ts->gc.owner = THIS_MODULE;
  335. ts->gc.dev = dev;
  336. ret = gpiochip_add(&ts->gc);
  337. if (ret)
  338. dev_err(dev, "failed to register gpio %d\n",
  339. ts->gc.base);
  340. }
  341. return ret;
  342. }
  343. /*
  344. * We mark ad7879_gpio_remove inline so there is a chance the code
  345. * gets discarded when not needed. We can't do __devinit/__devexit
  346. * markup since it is used in both probe and remove methods.
  347. */
  348. static inline void ad7879_gpio_remove(struct device *dev)
  349. {
  350. struct ad7879 *ts = dev_get_drvdata(dev);
  351. struct ad7879_platform_data *pdata = dev->platform_data;
  352. int ret;
  353. if (pdata->gpio_export) {
  354. ret = gpiochip_remove(&ts->gc);
  355. if (ret)
  356. dev_err(dev, "failed to remove gpio %d\n",
  357. ts->gc.base);
  358. }
  359. }
  360. #else
  361. static inline int ad7879_gpio_add(struct device *dev)
  362. {
  363. return 0;
  364. }
  365. static inline void ad7879_gpio_remove(struct device *dev)
  366. {
  367. }
  368. #endif
  369. static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
  370. {
  371. struct input_dev *input_dev;
  372. struct ad7879_platform_data *pdata = bus->dev.platform_data;
  373. int err;
  374. u16 revid;
  375. if (!bus->irq) {
  376. dev_err(&bus->dev, "no IRQ?\n");
  377. return -ENODEV;
  378. }
  379. if (!pdata) {
  380. dev_err(&bus->dev, "no platform data?\n");
  381. return -ENODEV;
  382. }
  383. input_dev = input_allocate_device();
  384. if (!input_dev)
  385. return -ENOMEM;
  386. ts->input = input_dev;
  387. setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts);
  388. INIT_WORK(&ts->work, ad7879_work);
  389. mutex_init(&ts->mutex);
  390. ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
  391. ts->pressure_max = pdata->pressure_max ? : ~0;
  392. ts->first_conversion_delay = pdata->first_conversion_delay;
  393. ts->acquisition_time = pdata->acquisition_time;
  394. ts->averaging = pdata->averaging;
  395. ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
  396. ts->median = pdata->median;
  397. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&bus->dev));
  398. input_dev->name = "AD7879 Touchscreen";
  399. input_dev->phys = ts->phys;
  400. input_dev->dev.parent = &bus->dev;
  401. __set_bit(EV_ABS, input_dev->evbit);
  402. __set_bit(ABS_X, input_dev->absbit);
  403. __set_bit(ABS_Y, input_dev->absbit);
  404. __set_bit(ABS_PRESSURE, input_dev->absbit);
  405. input_set_abs_params(input_dev, ABS_X,
  406. pdata->x_min ? : 0,
  407. pdata->x_max ? : MAX_12BIT,
  408. 0, 0);
  409. input_set_abs_params(input_dev, ABS_Y,
  410. pdata->y_min ? : 0,
  411. pdata->y_max ? : MAX_12BIT,
  412. 0, 0);
  413. input_set_abs_params(input_dev, ABS_PRESSURE,
  414. pdata->pressure_min, pdata->pressure_max, 0, 0);
  415. err = ad7879_write(bus, AD7879_REG_CTRL2, AD7879_RESET);
  416. if (err < 0) {
  417. dev_err(&bus->dev, "Failed to write %s\n", input_dev->name);
  418. goto err_free_mem;
  419. }
  420. revid = ad7879_read(bus, AD7879_REG_REVID);
  421. if ((revid & 0xFF) != AD7879_DEVID) {
  422. dev_err(&bus->dev, "Failed to probe %s\n", input_dev->name);
  423. err = -ENODEV;
  424. goto err_free_mem;
  425. }
  426. ts->cmd_crtl3 = AD7879_YPLUS_BIT |
  427. AD7879_XPLUS_BIT |
  428. AD7879_Z2_BIT |
  429. AD7879_Z1_BIT |
  430. AD7879_TEMPMASK_BIT |
  431. AD7879_AUXVBATMASK_BIT |
  432. AD7879_GPIOALERTMASK_BIT;
  433. ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR |
  434. AD7879_AVG(ts->averaging) |
  435. AD7879_MFS(ts->median) |
  436. AD7879_FCD(ts->first_conversion_delay);
  437. ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 |
  438. AD7879_ACQ(ts->acquisition_time) |
  439. AD7879_TMR(ts->pen_down_acc_interval);
  440. ad7879_setup(ts);
  441. err = request_irq(bus->irq, ad7879_irq,
  442. IRQF_TRIGGER_FALLING, bus->dev.driver->name, ts);
  443. if (err) {
  444. dev_err(&bus->dev, "irq %d busy?\n", bus->irq);
  445. goto err_free_mem;
  446. }
  447. err = sysfs_create_group(&bus->dev.kobj, &ad7879_attr_group);
  448. if (err)
  449. goto err_free_irq;
  450. err = ad7879_gpio_add(&bus->dev);
  451. if (err)
  452. goto err_remove_attr;
  453. err = input_register_device(input_dev);
  454. if (err)
  455. goto err_remove_gpio;
  456. dev_info(&bus->dev, "Rev.%d touchscreen, irq %d\n",
  457. revid >> 8, bus->irq);
  458. return 0;
  459. err_remove_gpio:
  460. ad7879_gpio_remove(&bus->dev);
  461. err_remove_attr:
  462. sysfs_remove_group(&bus->dev.kobj, &ad7879_attr_group);
  463. err_free_irq:
  464. free_irq(bus->irq, ts);
  465. err_free_mem:
  466. input_free_device(input_dev);
  467. return err;
  468. }
  469. static int __devexit ad7879_destroy(bus_device *bus, struct ad7879 *ts)
  470. {
  471. ad7879_gpio_remove(&bus->dev);
  472. ad7879_disable(ts);
  473. sysfs_remove_group(&ts->bus->dev.kobj, &ad7879_attr_group);
  474. free_irq(ts->bus->irq, ts);
  475. input_unregister_device(ts->input);
  476. dev_dbg(&bus->dev, "unregistered touchscreen\n");
  477. return 0;
  478. }
  479. #ifdef CONFIG_PM
  480. static int ad7879_suspend(bus_device *bus, pm_message_t message)
  481. {
  482. struct ad7879 *ts = dev_get_drvdata(&bus->dev);
  483. ad7879_disable(ts);
  484. return 0;
  485. }
  486. static int ad7879_resume(bus_device *bus)
  487. {
  488. struct ad7879 *ts = dev_get_drvdata(&bus->dev);
  489. ad7879_enable(ts);
  490. return 0;
  491. }
  492. #else
  493. #define ad7879_suspend NULL
  494. #define ad7879_resume NULL
  495. #endif
  496. #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
  497. #define MAX_SPI_FREQ_HZ 5000000
  498. #define AD7879_CMD_MAGIC 0xE000
  499. #define AD7879_CMD_READ (1 << 10)
  500. #define AD7879_WRITECMD(reg) (AD7879_CMD_MAGIC | (reg & 0xF))
  501. #define AD7879_READCMD(reg) (AD7879_CMD_MAGIC | AD7879_CMD_READ | (reg & 0xF))
  502. struct ser_req {
  503. u16 command;
  504. u16 data;
  505. struct spi_message msg;
  506. struct spi_transfer xfer[2];
  507. };
  508. /*
  509. * ad7879_read/write are only used for initial setup and for sysfs controls.
  510. * The main traffic is done in ad7879_collect().
  511. */
  512. static int ad7879_read(struct spi_device *spi, u8 reg)
  513. {
  514. struct ser_req *req;
  515. int status, ret;
  516. req = kzalloc(sizeof *req, GFP_KERNEL);
  517. if (!req)
  518. return -ENOMEM;
  519. spi_message_init(&req->msg);
  520. req->command = (u16) AD7879_READCMD(reg);
  521. req->xfer[0].tx_buf = &req->command;
  522. req->xfer[0].len = 2;
  523. req->xfer[1].rx_buf = &req->data;
  524. req->xfer[1].len = 2;
  525. spi_message_add_tail(&req->xfer[0], &req->msg);
  526. spi_message_add_tail(&req->xfer[1], &req->msg);
  527. status = spi_sync(spi, &req->msg);
  528. ret = status ? : req->data;
  529. kfree(req);
  530. return ret;
  531. }
  532. static int ad7879_write(struct spi_device *spi, u8 reg, u16 val)
  533. {
  534. struct ser_req *req;
  535. int status;
  536. req = kzalloc(sizeof *req, GFP_KERNEL);
  537. if (!req)
  538. return -ENOMEM;
  539. spi_message_init(&req->msg);
  540. req->command = (u16) AD7879_WRITECMD(reg);
  541. req->xfer[0].tx_buf = &req->command;
  542. req->xfer[0].len = 2;
  543. req->data = val;
  544. req->xfer[1].tx_buf = &req->data;
  545. req->xfer[1].len = 2;
  546. spi_message_add_tail(&req->xfer[0], &req->msg);
  547. spi_message_add_tail(&req->xfer[1], &req->msg);
  548. status = spi_sync(spi, &req->msg);
  549. kfree(req);
  550. return status;
  551. }
  552. static void ad7879_collect(struct ad7879 *ts)
  553. {
  554. int status = spi_sync(ts->bus, &ts->msg);
  555. if (status)
  556. dev_err(&ts->bus->dev, "spi_sync --> %d\n", status);
  557. }
  558. static void ad7879_setup_ts_def_msg(struct ad7879 *ts)
  559. {
  560. struct spi_message *m;
  561. int i;
  562. ts->cmd = (u16) AD7879_READCMD(AD7879_REG_XPLUS);
  563. m = &ts->msg;
  564. spi_message_init(m);
  565. ts->xfer[0].tx_buf = &ts->cmd;
  566. ts->xfer[0].len = 2;
  567. spi_message_add_tail(&ts->xfer[0], m);
  568. for (i = 0; i < AD7879_NR_SENSE; i++) {
  569. ts->xfer[i + 1].rx_buf = &ts->conversion_data[i];
  570. ts->xfer[i + 1].len = 2;
  571. spi_message_add_tail(&ts->xfer[i + 1], m);
  572. }
  573. }
  574. static int __devinit ad7879_probe(struct spi_device *spi)
  575. {
  576. struct ad7879 *ts;
  577. int error;
  578. /* don't exceed max specified SPI CLK frequency */
  579. if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
  580. dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
  581. return -EINVAL;
  582. }
  583. ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
  584. if (!ts)
  585. return -ENOMEM;
  586. dev_set_drvdata(&spi->dev, ts);
  587. ts->bus = spi;
  588. ad7879_setup_ts_def_msg(ts);
  589. error = ad7879_construct(spi, ts);
  590. if (error) {
  591. dev_set_drvdata(&spi->dev, NULL);
  592. kfree(ts);
  593. }
  594. return error;
  595. }
  596. static int __devexit ad7879_remove(struct spi_device *spi)
  597. {
  598. struct ad7879 *ts = dev_get_drvdata(&spi->dev);
  599. ad7879_destroy(spi, ts);
  600. dev_set_drvdata(&spi->dev, NULL);
  601. kfree(ts);
  602. return 0;
  603. }
  604. static struct spi_driver ad7879_driver = {
  605. .driver = {
  606. .name = "ad7879",
  607. .bus = &spi_bus_type,
  608. .owner = THIS_MODULE,
  609. },
  610. .probe = ad7879_probe,
  611. .remove = __devexit_p(ad7879_remove),
  612. .suspend = ad7879_suspend,
  613. .resume = ad7879_resume,
  614. };
  615. static int __init ad7879_init(void)
  616. {
  617. return spi_register_driver(&ad7879_driver);
  618. }
  619. module_init(ad7879_init);
  620. static void __exit ad7879_exit(void)
  621. {
  622. spi_unregister_driver(&ad7879_driver);
  623. }
  624. module_exit(ad7879_exit);
  625. #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
  626. /* All registers are word-sized.
  627. * AD7879 uses a high-byte first convention.
  628. */
  629. static int ad7879_read(struct i2c_client *client, u8 reg)
  630. {
  631. return swab16(i2c_smbus_read_word_data(client, reg));
  632. }
  633. static int ad7879_write(struct i2c_client *client, u8 reg, u16 val)
  634. {
  635. return i2c_smbus_write_word_data(client, reg, swab16(val));
  636. }
  637. static void ad7879_collect(struct ad7879 *ts)
  638. {
  639. int i;
  640. for (i = 0; i < AD7879_NR_SENSE; i++)
  641. ts->conversion_data[i] = ad7879_read(ts->bus,
  642. AD7879_REG_XPLUS + i);
  643. }
  644. static int __devinit ad7879_probe(struct i2c_client *client,
  645. const struct i2c_device_id *id)
  646. {
  647. struct ad7879 *ts;
  648. int error;
  649. if (!i2c_check_functionality(client->adapter,
  650. I2C_FUNC_SMBUS_WORD_DATA)) {
  651. dev_err(&client->dev, "SMBUS Word Data not Supported\n");
  652. return -EIO;
  653. }
  654. ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
  655. if (!ts)
  656. return -ENOMEM;
  657. i2c_set_clientdata(client, ts);
  658. ts->bus = client;
  659. error = ad7879_construct(client, ts);
  660. if (error) {
  661. i2c_set_clientdata(client, NULL);
  662. kfree(ts);
  663. }
  664. return error;
  665. }
  666. static int __devexit ad7879_remove(struct i2c_client *client)
  667. {
  668. struct ad7879 *ts = dev_get_drvdata(&client->dev);
  669. ad7879_destroy(client, ts);
  670. i2c_set_clientdata(client, NULL);
  671. kfree(ts);
  672. return 0;
  673. }
  674. static const struct i2c_device_id ad7879_id[] = {
  675. { "ad7879", 0 },
  676. { "ad7889", 0 },
  677. { }
  678. };
  679. MODULE_DEVICE_TABLE(i2c, ad7879_id);
  680. static struct i2c_driver ad7879_driver = {
  681. .driver = {
  682. .name = "ad7879",
  683. .owner = THIS_MODULE,
  684. },
  685. .probe = ad7879_probe,
  686. .remove = __devexit_p(ad7879_remove),
  687. .suspend = ad7879_suspend,
  688. .resume = ad7879_resume,
  689. .id_table = ad7879_id,
  690. };
  691. static int __init ad7879_init(void)
  692. {
  693. return i2c_add_driver(&ad7879_driver);
  694. }
  695. module_init(ad7879_init);
  696. static void __exit ad7879_exit(void)
  697. {
  698. i2c_del_driver(&ad7879_driver);
  699. }
  700. module_exit(ad7879_exit);
  701. #endif
  702. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  703. MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
  704. MODULE_LICENSE("GPL");
  705. MODULE_ALIAS("spi:ad7879");