USB: EHCI: remove PCI assumption
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / touchscreen / ad7879.c
blob794d070c6900607752f2af1b7641f0cc98ef267f
1 /*
2 * Copyright (C) 2008-2009 Michael Hennerich, Analog Devices Inc.
4 * Description: AD7879/AD7889 based touchscreen, and GPIO driver
5 * (I2C/SPI Interface)
7 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
24 * History:
25 * Copyright (c) 2005 David Brownell
26 * Copyright (c) 2006 Nokia Corporation
27 * Various changes: Imre Deak <imre.deak@nokia.com>
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.
40 #include <linux/device.h>
41 #include <linux/init.h>
42 #include <linux/delay.h>
43 #include <linux/input.h>
44 #include <linux/interrupt.h>
45 #include <linux/irq.h>
46 #include <linux/slab.h>
47 #include <linux/workqueue.h>
48 #include <linux/spi/spi.h>
49 #include <linux/i2c.h>
50 #include <linux/gpio.h>
52 #include <linux/spi/ad7879.h>
54 #define AD7879_REG_ZEROS 0
55 #define AD7879_REG_CTRL1 1
56 #define AD7879_REG_CTRL2 2
57 #define AD7879_REG_CTRL3 3
58 #define AD7879_REG_AUX1HIGH 4
59 #define AD7879_REG_AUX1LOW 5
60 #define AD7879_REG_TEMP1HIGH 6
61 #define AD7879_REG_TEMP1LOW 7
62 #define AD7879_REG_XPLUS 8
63 #define AD7879_REG_YPLUS 9
64 #define AD7879_REG_Z1 10
65 #define AD7879_REG_Z2 11
66 #define AD7879_REG_AUXVBAT 12
67 #define AD7879_REG_TEMP 13
68 #define AD7879_REG_REVID 14
70 /* Control REG 1 */
71 #define AD7879_TMR(x) ((x & 0xFF) << 0)
72 #define AD7879_ACQ(x) ((x & 0x3) << 8)
73 #define AD7879_MODE_NOC (0 << 10) /* Do not convert */
74 #define AD7879_MODE_SCC (1 << 10) /* Single channel conversion */
75 #define AD7879_MODE_SEQ0 (2 << 10) /* Sequence 0 in Slave Mode */
76 #define AD7879_MODE_SEQ1 (3 << 10) /* Sequence 1 in Master Mode */
77 #define AD7879_MODE_INT (1 << 15) /* PENIRQ disabled INT enabled */
79 /* Control REG 2 */
80 #define AD7879_FCD(x) ((x & 0x3) << 0)
81 #define AD7879_RESET (1 << 4)
82 #define AD7879_MFS(x) ((x & 0x3) << 5)
83 #define AD7879_AVG(x) ((x & 0x3) << 7)
84 #define AD7879_SER (1 << 9) /* non-differential */
85 #define AD7879_DFR (0 << 9) /* differential */
86 #define AD7879_GPIOPOL (1 << 10)
87 #define AD7879_GPIODIR (1 << 11)
88 #define AD7879_GPIO_DATA (1 << 12)
89 #define AD7879_GPIO_EN (1 << 13)
90 #define AD7879_PM(x) ((x & 0x3) << 14)
91 #define AD7879_PM_SHUTDOWN (0)
92 #define AD7879_PM_DYN (1)
93 #define AD7879_PM_FULLON (2)
95 /* Control REG 3 */
96 #define AD7879_TEMPMASK_BIT (1<<15)
97 #define AD7879_AUXVBATMASK_BIT (1<<14)
98 #define AD7879_INTMODE_BIT (1<<13)
99 #define AD7879_GPIOALERTMASK_BIT (1<<12)
100 #define AD7879_AUXLOW_BIT (1<<11)
101 #define AD7879_AUXHIGH_BIT (1<<10)
102 #define AD7879_TEMPLOW_BIT (1<<9)
103 #define AD7879_TEMPHIGH_BIT (1<<8)
104 #define AD7879_YPLUS_BIT (1<<7)
105 #define AD7879_XPLUS_BIT (1<<6)
106 #define AD7879_Z1_BIT (1<<5)
107 #define AD7879_Z2_BIT (1<<4)
108 #define AD7879_AUX_BIT (1<<3)
109 #define AD7879_VBAT_BIT (1<<2)
110 #define AD7879_TEMP_BIT (1<<1)
112 enum {
113 AD7879_SEQ_XPOS = 0,
114 AD7879_SEQ_YPOS = 1,
115 AD7879_SEQ_Z1 = 2,
116 AD7879_SEQ_Z2 = 3,
117 AD7879_NR_SENSE = 4,
120 #define MAX_12BIT ((1<<12)-1)
121 #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50)
123 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
124 #define AD7879_DEVID 0x7A
125 typedef struct spi_device bus_device;
126 #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
127 #define AD7879_DEVID 0x79
128 typedef struct i2c_client bus_device;
129 #endif
131 struct ad7879 {
132 bus_device *bus;
133 struct input_dev *input;
134 struct work_struct work;
135 struct timer_list timer;
136 #ifdef CONFIG_GPIOLIB
137 struct gpio_chip gc;
138 #endif
139 struct mutex mutex;
140 unsigned disabled:1; /* P: mutex */
142 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
143 struct spi_message msg;
144 struct spi_transfer xfer[AD7879_NR_SENSE + 1];
145 u16 cmd;
146 #endif
147 u16 conversion_data[AD7879_NR_SENSE];
148 char phys[32];
149 u8 first_conversion_delay;
150 u8 acquisition_time;
151 u8 averaging;
152 u8 pen_down_acc_interval;
153 u8 median;
154 u16 x_plate_ohms;
155 u16 pressure_max;
156 u16 cmd_crtl1;
157 u16 cmd_crtl2;
158 u16 cmd_crtl3;
161 static int ad7879_read(bus_device *, u8);
162 static int ad7879_write(bus_device *, u8, u16);
163 static void ad7879_collect(struct ad7879 *);
165 static void ad7879_report(struct ad7879 *ts)
167 struct input_dev *input_dev = ts->input;
168 unsigned Rt;
169 u16 x, y, z1, z2;
171 x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT;
172 y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT;
173 z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT;
174 z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT;
177 * The samples processed here are already preprocessed by the AD7879.
178 * The preprocessing function consists of a median and an averaging filter.
179 * The combination of these two techniques provides a robust solution,
180 * discarding the spurious noise in the signal and keeping only the data of interest.
181 * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h)
182 * Other user-programmable conversion controls include variable acquisition time,
183 * and first conversion delay. Up to 16 averages can be taken per conversion.
186 if (likely(x && z1)) {
187 /* compute touch pressure resistance using equation #1 */
188 Rt = (z2 - z1) * x * ts->x_plate_ohms;
189 Rt /= z1;
190 Rt = (Rt + 2047) >> 12;
192 input_report_abs(input_dev, ABS_X, x);
193 input_report_abs(input_dev, ABS_Y, y);
194 input_report_abs(input_dev, ABS_PRESSURE, Rt);
195 input_sync(input_dev);
199 static void ad7879_work(struct work_struct *work)
201 struct ad7879 *ts = container_of(work, struct ad7879, work);
203 /* use keventd context to read the result registers */
204 ad7879_collect(ts);
205 ad7879_report(ts);
206 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
209 static void ad7879_ts_event_release(struct ad7879 *ts)
211 struct input_dev *input_dev = ts->input;
213 input_report_abs(input_dev, ABS_PRESSURE, 0);
214 input_sync(input_dev);
217 static void ad7879_timer(unsigned long handle)
219 struct ad7879 *ts = (void *)handle;
221 ad7879_ts_event_release(ts);
224 static irqreturn_t ad7879_irq(int irq, void *handle)
226 struct ad7879 *ts = handle;
228 /* The repeated conversion sequencer controlled by TMR kicked off too fast.
229 * We ignore the last and process the sample sequence currently in the queue.
230 * It can't be older than 9.4ms
233 if (!work_pending(&ts->work))
234 schedule_work(&ts->work);
236 return IRQ_HANDLED;
239 static void ad7879_setup(struct ad7879 *ts)
241 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
242 ad7879_write(ts->bus, AD7879_REG_CTRL3, ts->cmd_crtl3);
243 ad7879_write(ts->bus, AD7879_REG_CTRL1, ts->cmd_crtl1);
246 static void ad7879_disable(struct ad7879 *ts)
248 mutex_lock(&ts->mutex);
250 if (!ts->disabled) {
252 ts->disabled = 1;
253 disable_irq(ts->bus->irq);
255 cancel_work_sync(&ts->work);
257 if (del_timer_sync(&ts->timer))
258 ad7879_ts_event_release(ts);
260 ad7879_write(ts->bus, AD7879_REG_CTRL2,
261 AD7879_PM(AD7879_PM_SHUTDOWN));
264 mutex_unlock(&ts->mutex);
267 static void ad7879_enable(struct ad7879 *ts)
269 mutex_lock(&ts->mutex);
271 if (ts->disabled) {
272 ad7879_setup(ts);
273 ts->disabled = 0;
274 enable_irq(ts->bus->irq);
277 mutex_unlock(&ts->mutex);
280 static ssize_t ad7879_disable_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct ad7879 *ts = dev_get_drvdata(dev);
285 return sprintf(buf, "%u\n", ts->disabled);
288 static ssize_t ad7879_disable_store(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf, size_t count)
292 struct ad7879 *ts = dev_get_drvdata(dev);
293 unsigned long val;
294 int error;
296 error = strict_strtoul(buf, 10, &val);
297 if (error)
298 return error;
300 if (val)
301 ad7879_disable(ts);
302 else
303 ad7879_enable(ts);
305 return count;
308 static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
310 static struct attribute *ad7879_attributes[] = {
311 &dev_attr_disable.attr,
312 NULL
315 static const struct attribute_group ad7879_attr_group = {
316 .attrs = ad7879_attributes,
319 #ifdef CONFIG_GPIOLIB
320 static int ad7879_gpio_direction_input(struct gpio_chip *chip,
321 unsigned gpio)
323 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
324 int err;
326 mutex_lock(&ts->mutex);
327 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
328 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
329 mutex_unlock(&ts->mutex);
331 return err;
334 static int ad7879_gpio_direction_output(struct gpio_chip *chip,
335 unsigned gpio, int level)
337 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
338 int err;
340 mutex_lock(&ts->mutex);
341 ts->cmd_crtl2 &= ~AD7879_GPIODIR;
342 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL;
343 if (level)
344 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
345 else
346 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
348 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
349 mutex_unlock(&ts->mutex);
351 return err;
354 static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
356 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
357 u16 val;
359 mutex_lock(&ts->mutex);
360 val = ad7879_read(ts->bus, AD7879_REG_CTRL2);
361 mutex_unlock(&ts->mutex);
363 return !!(val & AD7879_GPIO_DATA);
366 static void ad7879_gpio_set_value(struct gpio_chip *chip,
367 unsigned gpio, int value)
369 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
371 mutex_lock(&ts->mutex);
372 if (value)
373 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
374 else
375 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
377 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
378 mutex_unlock(&ts->mutex);
381 static int __devinit ad7879_gpio_add(struct device *dev)
383 struct ad7879 *ts = dev_get_drvdata(dev);
384 struct ad7879_platform_data *pdata = dev->platform_data;
385 int ret = 0;
387 if (pdata->gpio_export) {
388 ts->gc.direction_input = ad7879_gpio_direction_input;
389 ts->gc.direction_output = ad7879_gpio_direction_output;
390 ts->gc.get = ad7879_gpio_get_value;
391 ts->gc.set = ad7879_gpio_set_value;
392 ts->gc.can_sleep = 1;
393 ts->gc.base = pdata->gpio_base;
394 ts->gc.ngpio = 1;
395 ts->gc.label = "AD7879-GPIO";
396 ts->gc.owner = THIS_MODULE;
397 ts->gc.dev = dev;
399 ret = gpiochip_add(&ts->gc);
400 if (ret)
401 dev_err(dev, "failed to register gpio %d\n",
402 ts->gc.base);
405 return ret;
409 * We mark ad7879_gpio_remove inline so there is a chance the code
410 * gets discarded when not needed. We can't do __devinit/__devexit
411 * markup since it is used in both probe and remove methods.
413 static inline void ad7879_gpio_remove(struct device *dev)
415 struct ad7879 *ts = dev_get_drvdata(dev);
416 struct ad7879_platform_data *pdata = dev->platform_data;
417 int ret;
419 if (pdata->gpio_export) {
420 ret = gpiochip_remove(&ts->gc);
421 if (ret)
422 dev_err(dev, "failed to remove gpio %d\n",
423 ts->gc.base);
426 #else
427 static inline int ad7879_gpio_add(struct device *dev)
429 return 0;
432 static inline void ad7879_gpio_remove(struct device *dev)
435 #endif
437 static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
439 struct input_dev *input_dev;
440 struct ad7879_platform_data *pdata = bus->dev.platform_data;
441 int err;
442 u16 revid;
444 if (!bus->irq) {
445 dev_err(&bus->dev, "no IRQ?\n");
446 return -ENODEV;
449 if (!pdata) {
450 dev_err(&bus->dev, "no platform data?\n");
451 return -ENODEV;
454 input_dev = input_allocate_device();
455 if (!input_dev)
456 return -ENOMEM;
458 ts->input = input_dev;
460 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts);
461 INIT_WORK(&ts->work, ad7879_work);
462 mutex_init(&ts->mutex);
464 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
465 ts->pressure_max = pdata->pressure_max ? : ~0;
467 ts->first_conversion_delay = pdata->first_conversion_delay;
468 ts->acquisition_time = pdata->acquisition_time;
469 ts->averaging = pdata->averaging;
470 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
471 ts->median = pdata->median;
473 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&bus->dev));
475 input_dev->name = "AD7879 Touchscreen";
476 input_dev->phys = ts->phys;
477 input_dev->dev.parent = &bus->dev;
479 __set_bit(EV_ABS, input_dev->evbit);
480 __set_bit(ABS_X, input_dev->absbit);
481 __set_bit(ABS_Y, input_dev->absbit);
482 __set_bit(ABS_PRESSURE, input_dev->absbit);
484 input_set_abs_params(input_dev, ABS_X,
485 pdata->x_min ? : 0,
486 pdata->x_max ? : MAX_12BIT,
487 0, 0);
488 input_set_abs_params(input_dev, ABS_Y,
489 pdata->y_min ? : 0,
490 pdata->y_max ? : MAX_12BIT,
491 0, 0);
492 input_set_abs_params(input_dev, ABS_PRESSURE,
493 pdata->pressure_min, pdata->pressure_max, 0, 0);
495 err = ad7879_write(bus, AD7879_REG_CTRL2, AD7879_RESET);
497 if (err < 0) {
498 dev_err(&bus->dev, "Failed to write %s\n", input_dev->name);
499 goto err_free_mem;
502 revid = ad7879_read(bus, AD7879_REG_REVID);
504 if ((revid & 0xFF) != AD7879_DEVID) {
505 dev_err(&bus->dev, "Failed to probe %s\n", input_dev->name);
506 err = -ENODEV;
507 goto err_free_mem;
510 ts->cmd_crtl3 = AD7879_YPLUS_BIT |
511 AD7879_XPLUS_BIT |
512 AD7879_Z2_BIT |
513 AD7879_Z1_BIT |
514 AD7879_TEMPMASK_BIT |
515 AD7879_AUXVBATMASK_BIT |
516 AD7879_GPIOALERTMASK_BIT;
518 ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR |
519 AD7879_AVG(ts->averaging) |
520 AD7879_MFS(ts->median) |
521 AD7879_FCD(ts->first_conversion_delay);
523 ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 |
524 AD7879_ACQ(ts->acquisition_time) |
525 AD7879_TMR(ts->pen_down_acc_interval);
527 ad7879_setup(ts);
529 err = request_irq(bus->irq, ad7879_irq,
530 IRQF_TRIGGER_FALLING, bus->dev.driver->name, ts);
532 if (err) {
533 dev_err(&bus->dev, "irq %d busy?\n", bus->irq);
534 goto err_free_mem;
537 err = sysfs_create_group(&bus->dev.kobj, &ad7879_attr_group);
538 if (err)
539 goto err_free_irq;
541 err = ad7879_gpio_add(&bus->dev);
542 if (err)
543 goto err_remove_attr;
545 err = input_register_device(input_dev);
546 if (err)
547 goto err_remove_gpio;
549 dev_info(&bus->dev, "Rev.%d touchscreen, irq %d\n",
550 revid >> 8, bus->irq);
552 return 0;
554 err_remove_gpio:
555 ad7879_gpio_remove(&bus->dev);
556 err_remove_attr:
557 sysfs_remove_group(&bus->dev.kobj, &ad7879_attr_group);
558 err_free_irq:
559 free_irq(bus->irq, ts);
560 err_free_mem:
561 input_free_device(input_dev);
563 return err;
566 static int __devexit ad7879_destroy(bus_device *bus, struct ad7879 *ts)
568 ad7879_gpio_remove(&bus->dev);
569 ad7879_disable(ts);
570 sysfs_remove_group(&ts->bus->dev.kobj, &ad7879_attr_group);
571 free_irq(ts->bus->irq, ts);
572 input_unregister_device(ts->input);
573 dev_dbg(&bus->dev, "unregistered touchscreen\n");
575 return 0;
578 #ifdef CONFIG_PM
579 static int ad7879_suspend(bus_device *bus, pm_message_t message)
581 struct ad7879 *ts = dev_get_drvdata(&bus->dev);
583 ad7879_disable(ts);
585 return 0;
588 static int ad7879_resume(bus_device *bus)
590 struct ad7879 *ts = dev_get_drvdata(&bus->dev);
592 ad7879_enable(ts);
594 return 0;
596 #else
597 #define ad7879_suspend NULL
598 #define ad7879_resume NULL
599 #endif
601 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
602 #define MAX_SPI_FREQ_HZ 5000000
603 #define AD7879_CMD_MAGIC 0xE000
604 #define AD7879_CMD_READ (1 << 10)
605 #define AD7879_WRITECMD(reg) (AD7879_CMD_MAGIC | (reg & 0xF))
606 #define AD7879_READCMD(reg) (AD7879_CMD_MAGIC | AD7879_CMD_READ | (reg & 0xF))
608 struct ser_req {
609 u16 command;
610 u16 data;
611 struct spi_message msg;
612 struct spi_transfer xfer[2];
616 * ad7879_read/write are only used for initial setup and for sysfs controls.
617 * The main traffic is done in ad7879_collect().
620 static int ad7879_read(struct spi_device *spi, u8 reg)
622 struct ser_req *req;
623 int status, ret;
625 req = kzalloc(sizeof *req, GFP_KERNEL);
626 if (!req)
627 return -ENOMEM;
629 spi_message_init(&req->msg);
631 req->command = (u16) AD7879_READCMD(reg);
632 req->xfer[0].tx_buf = &req->command;
633 req->xfer[0].len = 2;
635 req->xfer[1].rx_buf = &req->data;
636 req->xfer[1].len = 2;
638 spi_message_add_tail(&req->xfer[0], &req->msg);
639 spi_message_add_tail(&req->xfer[1], &req->msg);
641 status = spi_sync(spi, &req->msg);
642 ret = status ? : req->data;
644 kfree(req);
646 return ret;
649 static int ad7879_write(struct spi_device *spi, u8 reg, u16 val)
651 struct ser_req *req;
652 int status;
654 req = kzalloc(sizeof *req, GFP_KERNEL);
655 if (!req)
656 return -ENOMEM;
658 spi_message_init(&req->msg);
660 req->command = (u16) AD7879_WRITECMD(reg);
661 req->xfer[0].tx_buf = &req->command;
662 req->xfer[0].len = 2;
664 req->data = val;
665 req->xfer[1].tx_buf = &req->data;
666 req->xfer[1].len = 2;
668 spi_message_add_tail(&req->xfer[0], &req->msg);
669 spi_message_add_tail(&req->xfer[1], &req->msg);
671 status = spi_sync(spi, &req->msg);
673 kfree(req);
675 return status;
678 static void ad7879_collect(struct ad7879 *ts)
680 int status = spi_sync(ts->bus, &ts->msg);
682 if (status)
683 dev_err(&ts->bus->dev, "spi_sync --> %d\n", status);
686 static void ad7879_setup_ts_def_msg(struct ad7879 *ts)
688 struct spi_message *m;
689 int i;
691 ts->cmd = (u16) AD7879_READCMD(AD7879_REG_XPLUS);
693 m = &ts->msg;
694 spi_message_init(m);
695 ts->xfer[0].tx_buf = &ts->cmd;
696 ts->xfer[0].len = 2;
698 spi_message_add_tail(&ts->xfer[0], m);
700 for (i = 0; i < AD7879_NR_SENSE; i++) {
701 ts->xfer[i + 1].rx_buf = &ts->conversion_data[i];
702 ts->xfer[i + 1].len = 2;
703 spi_message_add_tail(&ts->xfer[i + 1], m);
707 static int __devinit ad7879_probe(struct spi_device *spi)
709 struct ad7879 *ts;
710 int error;
712 /* don't exceed max specified SPI CLK frequency */
713 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
714 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
715 return -EINVAL;
718 ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
719 if (!ts)
720 return -ENOMEM;
722 dev_set_drvdata(&spi->dev, ts);
723 ts->bus = spi;
725 ad7879_setup_ts_def_msg(ts);
727 error = ad7879_construct(spi, ts);
728 if (error) {
729 dev_set_drvdata(&spi->dev, NULL);
730 kfree(ts);
733 return error;
736 static int __devexit ad7879_remove(struct spi_device *spi)
738 struct ad7879 *ts = dev_get_drvdata(&spi->dev);
740 ad7879_destroy(spi, ts);
741 dev_set_drvdata(&spi->dev, NULL);
742 kfree(ts);
744 return 0;
747 static struct spi_driver ad7879_driver = {
748 .driver = {
749 .name = "ad7879",
750 .bus = &spi_bus_type,
751 .owner = THIS_MODULE,
753 .probe = ad7879_probe,
754 .remove = __devexit_p(ad7879_remove),
755 .suspend = ad7879_suspend,
756 .resume = ad7879_resume,
759 static int __init ad7879_init(void)
761 return spi_register_driver(&ad7879_driver);
763 module_init(ad7879_init);
765 static void __exit ad7879_exit(void)
767 spi_unregister_driver(&ad7879_driver);
769 module_exit(ad7879_exit);
771 #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
773 /* All registers are word-sized.
774 * AD7879 uses a high-byte first convention.
776 static int ad7879_read(struct i2c_client *client, u8 reg)
778 return swab16(i2c_smbus_read_word_data(client, reg));
781 static int ad7879_write(struct i2c_client *client, u8 reg, u16 val)
783 return i2c_smbus_write_word_data(client, reg, swab16(val));
786 static void ad7879_collect(struct ad7879 *ts)
788 int i;
790 for (i = 0; i < AD7879_NR_SENSE; i++)
791 ts->conversion_data[i] = ad7879_read(ts->bus,
792 AD7879_REG_XPLUS + i);
795 static int __devinit ad7879_probe(struct i2c_client *client,
796 const struct i2c_device_id *id)
798 struct ad7879 *ts;
799 int error;
801 if (!i2c_check_functionality(client->adapter,
802 I2C_FUNC_SMBUS_WORD_DATA)) {
803 dev_err(&client->dev, "SMBUS Word Data not Supported\n");
804 return -EIO;
807 ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
808 if (!ts)
809 return -ENOMEM;
811 i2c_set_clientdata(client, ts);
812 ts->bus = client;
814 error = ad7879_construct(client, ts);
815 if (error) {
816 i2c_set_clientdata(client, NULL);
817 kfree(ts);
820 return error;
823 static int __devexit ad7879_remove(struct i2c_client *client)
825 struct ad7879 *ts = dev_get_drvdata(&client->dev);
827 ad7879_destroy(client, ts);
828 i2c_set_clientdata(client, NULL);
829 kfree(ts);
831 return 0;
834 static const struct i2c_device_id ad7879_id[] = {
835 { "ad7879", 0 },
836 { "ad7889", 0 },
839 MODULE_DEVICE_TABLE(i2c, ad7879_id);
841 static struct i2c_driver ad7879_driver = {
842 .driver = {
843 .name = "ad7879",
844 .owner = THIS_MODULE,
846 .probe = ad7879_probe,
847 .remove = __devexit_p(ad7879_remove),
848 .suspend = ad7879_suspend,
849 .resume = ad7879_resume,
850 .id_table = ad7879_id,
853 static int __init ad7879_init(void)
855 return i2c_add_driver(&ad7879_driver);
857 module_init(ad7879_init);
859 static void __exit ad7879_exit(void)
861 i2c_del_driver(&ad7879_driver);
863 module_exit(ad7879_exit);
864 #endif
866 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
867 MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
868 MODULE_LICENSE("GPL");
869 MODULE_ALIAS("spi:ad7879");