2 * Copyright (C) 2008-2009 Michael Hennerich, Analog Devices Inc.
4 * Description: AD7879/AD7889 based touchscreen, and GPIO driver
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
25 * Copyright (c) 2005 David Brownell
26 * Copyright (c) 2006 Nokia Corporation
27 * Various changes: Imre Deak <imre.deak@nokia.com>
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
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/spi/spi.h>
48 #include <linux/i2c.h>
49 #include <linux/gpio.h>
51 #include <linux/spi/ad7879.h>
53 #define AD7879_REG_ZEROS 0
54 #define AD7879_REG_CTRL1 1
55 #define AD7879_REG_CTRL2 2
56 #define AD7879_REG_CTRL3 3
57 #define AD7879_REG_AUX1HIGH 4
58 #define AD7879_REG_AUX1LOW 5
59 #define AD7879_REG_TEMP1HIGH 6
60 #define AD7879_REG_TEMP1LOW 7
61 #define AD7879_REG_XPLUS 8
62 #define AD7879_REG_YPLUS 9
63 #define AD7879_REG_Z1 10
64 #define AD7879_REG_Z2 11
65 #define AD7879_REG_AUXVBAT 12
66 #define AD7879_REG_TEMP 13
67 #define AD7879_REG_REVID 14
70 #define AD7879_TMR(x) ((x & 0xFF) << 0)
71 #define AD7879_ACQ(x) ((x & 0x3) << 8)
72 #define AD7879_MODE_NOC (0 << 10) /* Do not convert */
73 #define AD7879_MODE_SCC (1 << 10) /* Single channel conversion */
74 #define AD7879_MODE_SEQ0 (2 << 10) /* Sequence 0 in Slave Mode */
75 #define AD7879_MODE_SEQ1 (3 << 10) /* Sequence 1 in Master Mode */
76 #define AD7879_MODE_INT (1 << 15) /* PENIRQ disabled INT enabled */
79 #define AD7879_FCD(x) ((x & 0x3) << 0)
80 #define AD7879_RESET (1 << 4)
81 #define AD7879_MFS(x) ((x & 0x3) << 5)
82 #define AD7879_AVG(x) ((x & 0x3) << 7)
83 #define AD7879_SER (1 << 9) /* non-differential */
84 #define AD7879_DFR (0 << 9) /* differential */
85 #define AD7879_GPIOPOL (1 << 10)
86 #define AD7879_GPIODIR (1 << 11)
87 #define AD7879_GPIO_DATA (1 << 12)
88 #define AD7879_GPIO_EN (1 << 13)
89 #define AD7879_PM(x) ((x & 0x3) << 14)
90 #define AD7879_PM_SHUTDOWN (0)
91 #define AD7879_PM_DYN (1)
92 #define AD7879_PM_FULLON (2)
95 #define AD7879_TEMPMASK_BIT (1<<15)
96 #define AD7879_AUXVBATMASK_BIT (1<<14)
97 #define AD7879_INTMODE_BIT (1<<13)
98 #define AD7879_GPIOALERTMASK_BIT (1<<12)
99 #define AD7879_AUXLOW_BIT (1<<11)
100 #define AD7879_AUXHIGH_BIT (1<<10)
101 #define AD7879_TEMPLOW_BIT (1<<9)
102 #define AD7879_TEMPHIGH_BIT (1<<8)
103 #define AD7879_YPLUS_BIT (1<<7)
104 #define AD7879_XPLUS_BIT (1<<6)
105 #define AD7879_Z1_BIT (1<<5)
106 #define AD7879_Z2_BIT (1<<4)
107 #define AD7879_AUX_BIT (1<<3)
108 #define AD7879_VBAT_BIT (1<<2)
109 #define AD7879_TEMP_BIT (1<<1)
119 #define MAX_12BIT ((1<<12)-1)
120 #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50)
122 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
123 #define AD7879_DEVID 0x7A
124 typedef struct spi_device bus_device;
125 #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
126 #define AD7879_DEVID 0x79
127 typedef struct i2c_client bus_device;
132 struct input_dev *input;
133 struct timer_list timer;
134 #ifdef CONFIG_GPIOLIB
138 bool disabled; /* P: mutex */
140 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
141 struct spi_message msg;
142 struct spi_transfer xfer[AD7879_NR_SENSE + 1];
145 u16 conversion_data[AD7879_NR_SENSE];
147 u8 first_conversion_delay;
150 u8 pen_down_acc_interval;
159 static int ad7879_read(bus_device *, u8);
160 static int ad7879_write(bus_device *, u8, u16);
161 static void ad7879_collect(struct ad7879 *);
163 static void ad7879_report(struct ad7879 *ts)
165 struct input_dev *input_dev = ts->input;
169 x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT;
170 y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT;
171 z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT;
172 z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT;
175 * The samples processed here are already preprocessed by the AD7879.
176 * The preprocessing function consists of a median and an averaging filter.
177 * The combination of these two techniques provides a robust solution,
178 * discarding the spurious noise in the signal and keeping only the data of interest.
179 * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h)
180 * Other user-programmable conversion controls include variable acquisition time,
181 * and first conversion delay. Up to 16 averages can be taken per conversion.
184 if (likely(x && z1)) {
185 /* compute touch pressure resistance using equation #1 */
186 Rt = (z2 - z1) * x * ts->x_plate_ohms;
188 Rt = (Rt + 2047) >> 12;
190 input_report_abs(input_dev, ABS_X, x);
191 input_report_abs(input_dev, ABS_Y, y);
192 input_report_abs(input_dev, ABS_PRESSURE, Rt);
193 input_sync(input_dev);
197 static void ad7879_ts_event_release(struct ad7879 *ts)
199 struct input_dev *input_dev = ts->input;
201 input_report_abs(input_dev, ABS_PRESSURE, 0);
202 input_sync(input_dev);
205 static void ad7879_timer(unsigned long handle)
207 struct ad7879 *ts = (void *)handle;
209 ad7879_ts_event_release(ts);
212 static irqreturn_t ad7879_irq(int irq, void *handle)
214 struct ad7879 *ts = handle;
219 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
224 static void ad7879_setup(struct ad7879 *ts)
226 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
227 ad7879_write(ts->bus, AD7879_REG_CTRL3, ts->cmd_crtl3);
228 ad7879_write(ts->bus, AD7879_REG_CTRL1, ts->cmd_crtl1);
231 static void ad7879_disable(struct ad7879 *ts)
233 mutex_lock(&ts->mutex);
238 disable_irq(ts->bus->irq);
240 if (del_timer_sync(&ts->timer))
241 ad7879_ts_event_release(ts);
243 ad7879_write(ts->bus, AD7879_REG_CTRL2,
244 AD7879_PM(AD7879_PM_SHUTDOWN));
247 mutex_unlock(&ts->mutex);
250 static void ad7879_enable(struct ad7879 *ts)
252 mutex_lock(&ts->mutex);
256 ts->disabled = false;
257 enable_irq(ts->bus->irq);
260 mutex_unlock(&ts->mutex);
263 static ssize_t ad7879_disable_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
266 struct ad7879 *ts = dev_get_drvdata(dev);
268 return sprintf(buf, "%u\n", ts->disabled);
271 static ssize_t ad7879_disable_store(struct device *dev,
272 struct device_attribute *attr,
273 const char *buf, size_t count)
275 struct ad7879 *ts = dev_get_drvdata(dev);
279 error = strict_strtoul(buf, 10, &val);
291 static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
293 static struct attribute *ad7879_attributes[] = {
294 &dev_attr_disable.attr,
298 static const struct attribute_group ad7879_attr_group = {
299 .attrs = ad7879_attributes,
302 #ifdef CONFIG_GPIOLIB
303 static int ad7879_gpio_direction_input(struct gpio_chip *chip,
306 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
309 mutex_lock(&ts->mutex);
310 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
311 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
312 mutex_unlock(&ts->mutex);
317 static int ad7879_gpio_direction_output(struct gpio_chip *chip,
318 unsigned gpio, int level)
320 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
323 mutex_lock(&ts->mutex);
324 ts->cmd_crtl2 &= ~AD7879_GPIODIR;
325 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL;
327 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
329 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
331 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
332 mutex_unlock(&ts->mutex);
337 static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
339 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
342 mutex_lock(&ts->mutex);
343 val = ad7879_read(ts->bus, AD7879_REG_CTRL2);
344 mutex_unlock(&ts->mutex);
346 return !!(val & AD7879_GPIO_DATA);
349 static void ad7879_gpio_set_value(struct gpio_chip *chip,
350 unsigned gpio, int value)
352 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
354 mutex_lock(&ts->mutex);
356 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
358 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
360 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
361 mutex_unlock(&ts->mutex);
364 static int __devinit ad7879_gpio_add(struct device *dev)
366 struct ad7879 *ts = dev_get_drvdata(dev);
367 struct ad7879_platform_data *pdata = dev->platform_data;
370 if (pdata->gpio_export) {
371 ts->gc.direction_input = ad7879_gpio_direction_input;
372 ts->gc.direction_output = ad7879_gpio_direction_output;
373 ts->gc.get = ad7879_gpio_get_value;
374 ts->gc.set = ad7879_gpio_set_value;
375 ts->gc.can_sleep = 1;
376 ts->gc.base = pdata->gpio_base;
378 ts->gc.label = "AD7879-GPIO";
379 ts->gc.owner = THIS_MODULE;
382 ret = gpiochip_add(&ts->gc);
384 dev_err(dev, "failed to register gpio %d\n",
392 * We mark ad7879_gpio_remove inline so there is a chance the code
393 * gets discarded when not needed. We can't do __devinit/__devexit
394 * markup since it is used in both probe and remove methods.
396 static inline void ad7879_gpio_remove(struct device *dev)
398 struct ad7879 *ts = dev_get_drvdata(dev);
399 struct ad7879_platform_data *pdata = dev->platform_data;
402 if (pdata->gpio_export) {
403 ret = gpiochip_remove(&ts->gc);
405 dev_err(dev, "failed to remove gpio %d\n",
410 static inline int ad7879_gpio_add(struct device *dev)
415 static inline void ad7879_gpio_remove(struct device *dev)
420 static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
422 struct input_dev *input_dev;
423 struct ad7879_platform_data *pdata = bus->dev.platform_data;
428 dev_err(&bus->dev, "no IRQ?\n");
433 dev_err(&bus->dev, "no platform data?\n");
437 input_dev = input_allocate_device();
441 ts->input = input_dev;
443 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts);
444 mutex_init(&ts->mutex);
446 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
447 ts->pressure_max = pdata->pressure_max ? : ~0;
449 ts->first_conversion_delay = pdata->first_conversion_delay;
450 ts->acquisition_time = pdata->acquisition_time;
451 ts->averaging = pdata->averaging;
452 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
453 ts->median = pdata->median;
455 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&bus->dev));
457 input_dev->name = "AD7879 Touchscreen";
458 input_dev->phys = ts->phys;
459 input_dev->dev.parent = &bus->dev;
461 __set_bit(EV_ABS, input_dev->evbit);
462 __set_bit(ABS_X, input_dev->absbit);
463 __set_bit(ABS_Y, input_dev->absbit);
464 __set_bit(ABS_PRESSURE, input_dev->absbit);
466 input_set_abs_params(input_dev, ABS_X,
468 pdata->x_max ? : MAX_12BIT,
470 input_set_abs_params(input_dev, ABS_Y,
472 pdata->y_max ? : MAX_12BIT,
474 input_set_abs_params(input_dev, ABS_PRESSURE,
475 pdata->pressure_min, pdata->pressure_max, 0, 0);
477 err = ad7879_write(bus, AD7879_REG_CTRL2, AD7879_RESET);
480 dev_err(&bus->dev, "Failed to write %s\n", input_dev->name);
484 revid = ad7879_read(bus, AD7879_REG_REVID);
486 if ((revid & 0xFF) != AD7879_DEVID) {
487 dev_err(&bus->dev, "Failed to probe %s\n", input_dev->name);
492 ts->cmd_crtl3 = AD7879_YPLUS_BIT |
496 AD7879_TEMPMASK_BIT |
497 AD7879_AUXVBATMASK_BIT |
498 AD7879_GPIOALERTMASK_BIT;
500 ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR |
501 AD7879_AVG(ts->averaging) |
502 AD7879_MFS(ts->median) |
503 AD7879_FCD(ts->first_conversion_delay);
505 ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 |
506 AD7879_ACQ(ts->acquisition_time) |
507 AD7879_TMR(ts->pen_down_acc_interval);
511 err = request_threaded_irq(bus->irq, NULL, ad7879_irq,
512 IRQF_TRIGGER_FALLING,
513 bus->dev.driver->name, ts);
515 dev_err(&bus->dev, "irq %d busy?\n", bus->irq);
519 err = sysfs_create_group(&bus->dev.kobj, &ad7879_attr_group);
523 err = ad7879_gpio_add(&bus->dev);
525 goto err_remove_attr;
527 err = input_register_device(input_dev);
529 goto err_remove_gpio;
531 dev_info(&bus->dev, "Rev.%d touchscreen, irq %d\n",
532 revid >> 8, bus->irq);
537 ad7879_gpio_remove(&bus->dev);
539 sysfs_remove_group(&bus->dev.kobj, &ad7879_attr_group);
541 free_irq(bus->irq, ts);
543 input_free_device(input_dev);
548 static int __devexit ad7879_destroy(bus_device *bus, struct ad7879 *ts)
550 ad7879_gpio_remove(&bus->dev);
552 sysfs_remove_group(&ts->bus->dev.kobj, &ad7879_attr_group);
553 free_irq(ts->bus->irq, ts);
554 input_unregister_device(ts->input);
555 dev_dbg(&bus->dev, "unregistered touchscreen\n");
561 static int ad7879_suspend(bus_device *bus, pm_message_t message)
563 struct ad7879 *ts = dev_get_drvdata(&bus->dev);
570 static int ad7879_resume(bus_device *bus)
572 struct ad7879 *ts = dev_get_drvdata(&bus->dev);
579 #define ad7879_suspend NULL
580 #define ad7879_resume NULL
583 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
584 #define MAX_SPI_FREQ_HZ 5000000
585 #define AD7879_CMD_MAGIC 0xE000
586 #define AD7879_CMD_READ (1 << 10)
587 #define AD7879_WRITECMD(reg) (AD7879_CMD_MAGIC | (reg & 0xF))
588 #define AD7879_READCMD(reg) (AD7879_CMD_MAGIC | AD7879_CMD_READ | (reg & 0xF))
593 struct spi_message msg;
594 struct spi_transfer xfer[2];
598 * ad7879_read/write are only used for initial setup and for sysfs controls.
599 * The main traffic is done in ad7879_collect().
602 static int ad7879_read(struct spi_device *spi, u8 reg)
607 req = kzalloc(sizeof *req, GFP_KERNEL);
611 spi_message_init(&req->msg);
613 req->command = (u16) AD7879_READCMD(reg);
614 req->xfer[0].tx_buf = &req->command;
615 req->xfer[0].len = 2;
617 req->xfer[1].rx_buf = &req->data;
618 req->xfer[1].len = 2;
620 spi_message_add_tail(&req->xfer[0], &req->msg);
621 spi_message_add_tail(&req->xfer[1], &req->msg);
623 status = spi_sync(spi, &req->msg);
624 ret = status ? : req->data;
631 static int ad7879_write(struct spi_device *spi, u8 reg, u16 val)
636 req = kzalloc(sizeof *req, GFP_KERNEL);
640 spi_message_init(&req->msg);
642 req->command = (u16) AD7879_WRITECMD(reg);
643 req->xfer[0].tx_buf = &req->command;
644 req->xfer[0].len = 2;
647 req->xfer[1].tx_buf = &req->data;
648 req->xfer[1].len = 2;
650 spi_message_add_tail(&req->xfer[0], &req->msg);
651 spi_message_add_tail(&req->xfer[1], &req->msg);
653 status = spi_sync(spi, &req->msg);
660 static void ad7879_collect(struct ad7879 *ts)
662 int status = spi_sync(ts->bus, &ts->msg);
665 dev_err(&ts->bus->dev, "spi_sync --> %d\n", status);
668 static void ad7879_setup_ts_def_msg(struct ad7879 *ts)
670 struct spi_message *m;
673 ts->cmd = (u16) AD7879_READCMD(AD7879_REG_XPLUS);
677 ts->xfer[0].tx_buf = &ts->cmd;
680 spi_message_add_tail(&ts->xfer[0], m);
682 for (i = 0; i < AD7879_NR_SENSE; i++) {
683 ts->xfer[i + 1].rx_buf = &ts->conversion_data[i];
684 ts->xfer[i + 1].len = 2;
685 spi_message_add_tail(&ts->xfer[i + 1], m);
689 static int __devinit ad7879_probe(struct spi_device *spi)
694 /* don't exceed max specified SPI CLK frequency */
695 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
696 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
700 ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
704 dev_set_drvdata(&spi->dev, ts);
707 ad7879_setup_ts_def_msg(ts);
709 error = ad7879_construct(spi, ts);
711 dev_set_drvdata(&spi->dev, NULL);
718 static int __devexit ad7879_remove(struct spi_device *spi)
720 struct ad7879 *ts = dev_get_drvdata(&spi->dev);
722 ad7879_destroy(spi, ts);
723 dev_set_drvdata(&spi->dev, NULL);
729 static struct spi_driver ad7879_driver = {
732 .bus = &spi_bus_type,
733 .owner = THIS_MODULE,
735 .probe = ad7879_probe,
736 .remove = __devexit_p(ad7879_remove),
737 .suspend = ad7879_suspend,
738 .resume = ad7879_resume,
741 static int __init ad7879_init(void)
743 return spi_register_driver(&ad7879_driver);
745 module_init(ad7879_init);
747 static void __exit ad7879_exit(void)
749 spi_unregister_driver(&ad7879_driver);
751 module_exit(ad7879_exit);
753 #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
755 /* All registers are word-sized.
756 * AD7879 uses a high-byte first convention.
758 static int ad7879_read(struct i2c_client *client, u8 reg)
760 return swab16(i2c_smbus_read_word_data(client, reg));
763 static int ad7879_write(struct i2c_client *client, u8 reg, u16 val)
765 return i2c_smbus_write_word_data(client, reg, swab16(val));
768 static void ad7879_collect(struct ad7879 *ts)
772 for (i = 0; i < AD7879_NR_SENSE; i++)
773 ts->conversion_data[i] = ad7879_read(ts->bus,
774 AD7879_REG_XPLUS + i);
777 static int __devinit ad7879_probe(struct i2c_client *client,
778 const struct i2c_device_id *id)
783 if (!i2c_check_functionality(client->adapter,
784 I2C_FUNC_SMBUS_WORD_DATA)) {
785 dev_err(&client->dev, "SMBUS Word Data not Supported\n");
789 ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
793 i2c_set_clientdata(client, ts);
796 error = ad7879_construct(client, ts);
803 static int __devexit ad7879_remove(struct i2c_client *client)
805 struct ad7879 *ts = dev_get_drvdata(&client->dev);
807 ad7879_destroy(client, ts);
813 static const struct i2c_device_id ad7879_id[] = {
818 MODULE_DEVICE_TABLE(i2c, ad7879_id);
820 static struct i2c_driver ad7879_driver = {
823 .owner = THIS_MODULE,
825 .probe = ad7879_probe,
826 .remove = __devexit_p(ad7879_remove),
827 .suspend = ad7879_suspend,
828 .resume = ad7879_resume,
829 .id_table = ad7879_id,
832 static int __init ad7879_init(void)
834 return i2c_add_driver(&ad7879_driver);
836 module_init(ad7879_init);
838 static void __exit ad7879_exit(void)
840 i2c_del_driver(&ad7879_driver);
842 module_exit(ad7879_exit);
845 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
846 MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
847 MODULE_LICENSE("GPL");
848 MODULE_ALIAS("spi:ad7879");