ad7879.c 19 KB

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