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/workqueue.h>
48 #include <linux/spi/spi.h>
49 #include <linux/i2c.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 work_struct work
;
134 struct timer_list timer
;
137 unsigned disabled
:1; /* P: mutex */
139 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
140 struct spi_message msg
;
141 struct spi_transfer xfer
[AD7879_NR_SENSE
+ 1];
144 u16 conversion_data
[AD7879_NR_SENSE
];
146 u8 first_conversion_delay
;
149 u8 pen_down_acc_interval
;
160 static int ad7879_read(bus_device
*, u8
);
161 static int ad7879_write(bus_device
*, u8
, u16
);
162 static void ad7879_collect(struct ad7879
*);
164 static void ad7879_report(struct ad7879
*ts
)
166 struct input_dev
*input_dev
= ts
->input
;
170 x
= ts
->conversion_data
[AD7879_SEQ_XPOS
] & MAX_12BIT
;
171 y
= ts
->conversion_data
[AD7879_SEQ_YPOS
] & MAX_12BIT
;
172 z1
= ts
->conversion_data
[AD7879_SEQ_Z1
] & MAX_12BIT
;
173 z2
= ts
->conversion_data
[AD7879_SEQ_Z2
] & MAX_12BIT
;
176 * The samples processed here are already preprocessed by the AD7879.
177 * The preprocessing function consists of a median and an averaging filter.
178 * The combination of these two techniques provides a robust solution,
179 * discarding the spurious noise in the signal and keeping only the data of interest.
180 * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h)
181 * Other user-programmable conversion controls include variable acquisition time,
182 * and first conversion delay. Up to 16 averages can be taken per conversion.
185 if (likely(x
&& z1
)) {
186 /* compute touch pressure resistance using equation #1 */
187 Rt
= (z2
- z1
) * x
* ts
->x_plate_ohms
;
189 Rt
= (Rt
+ 2047) >> 12;
191 input_report_abs(input_dev
, ABS_X
, x
);
192 input_report_abs(input_dev
, ABS_Y
, y
);
193 input_report_abs(input_dev
, ABS_PRESSURE
, Rt
);
194 input_sync(input_dev
);
198 static void ad7879_work(struct work_struct
*work
)
200 struct ad7879
*ts
= container_of(work
, struct ad7879
, work
);
202 /* use keventd context to read the result registers */
205 mod_timer(&ts
->timer
, jiffies
+ TS_PEN_UP_TIMEOUT
);
208 static void ad7879_ts_event_release(struct ad7879
*ts
)
210 struct input_dev
*input_dev
= ts
->input
;
212 input_report_abs(input_dev
, ABS_PRESSURE
, 0);
213 input_sync(input_dev
);
216 static void ad7879_timer(unsigned long handle
)
218 struct ad7879
*ts
= (void *)handle
;
220 ad7879_ts_event_release(ts
);
223 static irqreturn_t
ad7879_irq(int irq
, void *handle
)
225 struct ad7879
*ts
= handle
;
227 /* The repeated conversion sequencer controlled by TMR kicked off too fast.
228 * We ignore the last and process the sample sequence currently in the queue.
229 * It can't be older than 9.4ms
232 if (!work_pending(&ts
->work
))
233 schedule_work(&ts
->work
);
238 static void ad7879_setup(struct ad7879
*ts
)
240 ts
->cmd_crtl3
= AD7879_YPLUS_BIT
|
244 AD7879_TEMPMASK_BIT
|
245 AD7879_AUXVBATMASK_BIT
|
246 AD7879_GPIOALERTMASK_BIT
;
248 ts
->cmd_crtl2
= AD7879_PM(AD7879_PM_DYN
) | AD7879_DFR
|
249 AD7879_AVG(ts
->averaging
) |
250 AD7879_MFS(ts
->median
) |
251 AD7879_FCD(ts
->first_conversion_delay
) |
254 ts
->cmd_crtl1
= AD7879_MODE_INT
| AD7879_MODE_SEQ1
|
255 AD7879_ACQ(ts
->acquisition_time
) |
256 AD7879_TMR(ts
->pen_down_acc_interval
);
258 ad7879_write(ts
->bus
, AD7879_REG_CTRL2
, ts
->cmd_crtl2
);
259 ad7879_write(ts
->bus
, AD7879_REG_CTRL3
, ts
->cmd_crtl3
);
260 ad7879_write(ts
->bus
, AD7879_REG_CTRL1
, ts
->cmd_crtl1
);
263 static void ad7879_disable(struct ad7879
*ts
)
265 mutex_lock(&ts
->mutex
);
270 disable_irq(ts
->bus
->irq
);
272 cancel_work_sync(&ts
->work
);
274 if (del_timer_sync(&ts
->timer
))
275 ad7879_ts_event_release(ts
);
277 ad7879_write(ts
->bus
, AD7879_REG_CTRL2
,
278 AD7879_PM(AD7879_PM_SHUTDOWN
));
281 mutex_unlock(&ts
->mutex
);
284 static void ad7879_enable(struct ad7879
*ts
)
286 mutex_lock(&ts
->mutex
);
291 enable_irq(ts
->bus
->irq
);
294 mutex_unlock(&ts
->mutex
);
297 static ssize_t
ad7879_disable_show(struct device
*dev
,
298 struct device_attribute
*attr
, char *buf
)
300 struct ad7879
*ts
= dev_get_drvdata(dev
);
302 return sprintf(buf
, "%u\n", ts
->disabled
);
305 static ssize_t
ad7879_disable_store(struct device
*dev
,
306 struct device_attribute
*attr
,
307 const char *buf
, size_t count
)
309 struct ad7879
*ts
= dev_get_drvdata(dev
);
313 error
= strict_strtoul(buf
, 10, &val
);
325 static DEVICE_ATTR(disable
, 0664, ad7879_disable_show
, ad7879_disable_store
);
327 static ssize_t
ad7879_gpio_show(struct device
*dev
,
328 struct device_attribute
*attr
, char *buf
)
330 struct ad7879
*ts
= dev_get_drvdata(dev
);
332 return sprintf(buf
, "%u\n", ts
->gpio
);
335 static ssize_t
ad7879_gpio_store(struct device
*dev
,
336 struct device_attribute
*attr
,
337 const char *buf
, size_t count
)
339 struct ad7879
*ts
= dev_get_drvdata(dev
);
343 error
= strict_strtoul(buf
, 10, &val
);
347 mutex_lock(&ts
->mutex
);
349 error
= ad7879_write(ts
->bus
, AD7879_REG_CTRL2
,
351 ts
->cmd_crtl2
& ~AD7879_GPIO_DATA
:
352 ts
->cmd_crtl2
| AD7879_GPIO_DATA
);
353 mutex_unlock(&ts
->mutex
);
355 return error
? : count
;
358 static DEVICE_ATTR(gpio
, 0664, ad7879_gpio_show
, ad7879_gpio_store
);
360 static struct attribute
*ad7879_attributes
[] = {
361 &dev_attr_disable
.attr
,
366 static const struct attribute_group ad7879_attr_group
= {
367 .attrs
= ad7879_attributes
,
370 static int __devinit
ad7879_construct(bus_device
*bus
, struct ad7879
*ts
)
372 struct input_dev
*input_dev
;
373 struct ad7879_platform_data
*pdata
= bus
->dev
.platform_data
;
378 dev_err(&bus
->dev
, "no IRQ?\n");
383 dev_err(&bus
->dev
, "no platform data?\n");
387 input_dev
= input_allocate_device();
391 ts
->input
= input_dev
;
393 setup_timer(&ts
->timer
, ad7879_timer
, (unsigned long) ts
);
394 INIT_WORK(&ts
->work
, ad7879_work
);
395 mutex_init(&ts
->mutex
);
397 ts
->x_plate_ohms
= pdata
->x_plate_ohms
? : 400;
398 ts
->pressure_max
= pdata
->pressure_max
? : ~0;
400 ts
->first_conversion_delay
= pdata
->first_conversion_delay
;
401 ts
->acquisition_time
= pdata
->acquisition_time
;
402 ts
->averaging
= pdata
->averaging
;
403 ts
->pen_down_acc_interval
= pdata
->pen_down_acc_interval
;
404 ts
->median
= pdata
->median
;
406 if (pdata
->gpio_output
)
407 ts
->gpio_init
= AD7879_GPIO_EN
|
408 (pdata
->gpio_default
? 0 : AD7879_GPIO_DATA
);
410 ts
->gpio_init
= AD7879_GPIO_EN
| AD7879_GPIODIR
;
412 snprintf(ts
->phys
, sizeof(ts
->phys
), "%s/input0", dev_name(&bus
->dev
));
414 input_dev
->name
= "AD7879 Touchscreen";
415 input_dev
->phys
= ts
->phys
;
416 input_dev
->dev
.parent
= &bus
->dev
;
418 __set_bit(EV_ABS
, input_dev
->evbit
);
419 __set_bit(ABS_X
, input_dev
->absbit
);
420 __set_bit(ABS_Y
, input_dev
->absbit
);
421 __set_bit(ABS_PRESSURE
, input_dev
->absbit
);
423 input_set_abs_params(input_dev
, ABS_X
,
425 pdata
->x_max
? : MAX_12BIT
,
427 input_set_abs_params(input_dev
, ABS_Y
,
429 pdata
->y_max
? : MAX_12BIT
,
431 input_set_abs_params(input_dev
, ABS_PRESSURE
,
432 pdata
->pressure_min
, pdata
->pressure_max
, 0, 0);
434 err
= ad7879_write(bus
, AD7879_REG_CTRL2
, AD7879_RESET
);
437 dev_err(&bus
->dev
, "Failed to write %s\n", input_dev
->name
);
441 revid
= ad7879_read(bus
, AD7879_REG_REVID
);
443 if ((revid
& 0xFF) != AD7879_DEVID
) {
444 dev_err(&bus
->dev
, "Failed to probe %s\n", input_dev
->name
);
451 err
= request_irq(bus
->irq
, ad7879_irq
,
452 IRQF_TRIGGER_FALLING
, bus
->dev
.driver
->name
, ts
);
455 dev_err(&bus
->dev
, "irq %d busy?\n", bus
->irq
);
459 err
= sysfs_create_group(&bus
->dev
.kobj
, &ad7879_attr_group
);
463 err
= input_register_device(input_dev
);
465 goto err_remove_attr
;
467 dev_info(&bus
->dev
, "Rev.%d touchscreen, irq %d\n",
468 revid
>> 8, bus
->irq
);
473 sysfs_remove_group(&bus
->dev
.kobj
, &ad7879_attr_group
);
475 free_irq(bus
->irq
, ts
);
477 input_free_device(input_dev
);
482 static int __devexit
ad7879_destroy(bus_device
*bus
, struct ad7879
*ts
)
485 sysfs_remove_group(&ts
->bus
->dev
.kobj
, &ad7879_attr_group
);
486 free_irq(ts
->bus
->irq
, ts
);
487 input_unregister_device(ts
->input
);
488 dev_dbg(&bus
->dev
, "unregistered touchscreen\n");
494 static int ad7879_suspend(bus_device
*bus
, pm_message_t message
)
496 struct ad7879
*ts
= dev_get_drvdata(&bus
->dev
);
503 static int ad7879_resume(bus_device
*bus
)
505 struct ad7879
*ts
= dev_get_drvdata(&bus
->dev
);
512 #define ad7879_suspend NULL
513 #define ad7879_resume NULL
516 #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
517 #define MAX_SPI_FREQ_HZ 5000000
518 #define AD7879_CMD_MAGIC 0xE000
519 #define AD7879_CMD_READ (1 << 10)
520 #define AD7879_WRITECMD(reg) (AD7879_CMD_MAGIC | (reg & 0xF))
521 #define AD7879_READCMD(reg) (AD7879_CMD_MAGIC | AD7879_CMD_READ | (reg & 0xF))
526 struct spi_message msg
;
527 struct spi_transfer xfer
[2];
531 * ad7879_read/write are only used for initial setup and for sysfs controls.
532 * The main traffic is done in ad7879_collect().
535 static int ad7879_read(struct spi_device
*spi
, u8 reg
)
540 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
544 spi_message_init(&req
->msg
);
546 req
->command
= (u16
) AD7879_READCMD(reg
);
547 req
->xfer
[0].tx_buf
= &req
->command
;
548 req
->xfer
[0].len
= 2;
550 req
->xfer
[1].rx_buf
= &req
->data
;
551 req
->xfer
[1].len
= 2;
553 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
554 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
556 status
= spi_sync(spi
, &req
->msg
);
557 ret
= status
? : req
->data
;
564 static int ad7879_write(struct spi_device
*spi
, u8 reg
, u16 val
)
569 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
573 spi_message_init(&req
->msg
);
575 req
->command
= (u16
) AD7879_WRITECMD(reg
);
576 req
->xfer
[0].tx_buf
= &req
->command
;
577 req
->xfer
[0].len
= 2;
580 req
->xfer
[1].tx_buf
= &req
->data
;
581 req
->xfer
[1].len
= 2;
583 spi_message_add_tail(&req
->xfer
[0], &req
->msg
);
584 spi_message_add_tail(&req
->xfer
[1], &req
->msg
);
586 status
= spi_sync(spi
, &req
->msg
);
593 static void ad7879_collect(struct ad7879
*ts
)
595 int status
= spi_sync(ts
->bus
, &ts
->msg
);
598 dev_err(&ts
->bus
->dev
, "spi_sync --> %d\n", status
);
601 static void ad7879_setup_ts_def_msg(struct ad7879
*ts
)
603 struct spi_message
*m
;
606 ts
->cmd
= (u16
) AD7879_READCMD(AD7879_REG_XPLUS
);
610 ts
->xfer
[0].tx_buf
= &ts
->cmd
;
613 spi_message_add_tail(&ts
->xfer
[0], m
);
615 for (i
= 0; i
< AD7879_NR_SENSE
; i
++) {
616 ts
->xfer
[i
+ 1].rx_buf
= &ts
->conversion_data
[i
];
617 ts
->xfer
[i
+ 1].len
= 2;
618 spi_message_add_tail(&ts
->xfer
[i
+ 1], m
);
622 static int __devinit
ad7879_probe(struct spi_device
*spi
)
627 /* don't exceed max specified SPI CLK frequency */
628 if (spi
->max_speed_hz
> MAX_SPI_FREQ_HZ
) {
629 dev_err(&spi
->dev
, "SPI CLK %d Hz?\n", spi
->max_speed_hz
);
633 ts
= kzalloc(sizeof(struct ad7879
), GFP_KERNEL
);
637 dev_set_drvdata(&spi
->dev
, ts
);
640 ad7879_setup_ts_def_msg(ts
);
642 error
= ad7879_construct(spi
, ts
);
644 dev_set_drvdata(&spi
->dev
, NULL
);
651 static int __devexit
ad7879_remove(struct spi_device
*spi
)
653 struct ad7879
*ts
= dev_get_drvdata(&spi
->dev
);
655 ad7879_destroy(spi
, ts
);
656 dev_set_drvdata(&spi
->dev
, NULL
);
662 static struct spi_driver ad7879_driver
= {
665 .bus
= &spi_bus_type
,
666 .owner
= THIS_MODULE
,
668 .probe
= ad7879_probe
,
669 .remove
= __devexit_p(ad7879_remove
),
670 .suspend
= ad7879_suspend
,
671 .resume
= ad7879_resume
,
674 static int __init
ad7879_init(void)
676 return spi_register_driver(&ad7879_driver
);
678 module_init(ad7879_init
);
680 static void __exit
ad7879_exit(void)
682 spi_unregister_driver(&ad7879_driver
);
684 module_exit(ad7879_exit
);
686 #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
688 /* All registers are word-sized.
689 * AD7879 uses a high-byte first convention.
691 static int ad7879_read(struct i2c_client
*client
, u8 reg
)
693 return swab16(i2c_smbus_read_word_data(client
, reg
));
696 static int ad7879_write(struct i2c_client
*client
, u8 reg
, u16 val
)
698 return i2c_smbus_write_word_data(client
, reg
, swab16(val
));
701 static void ad7879_collect(struct ad7879
*ts
)
705 for (i
= 0; i
< AD7879_NR_SENSE
; i
++)
706 ts
->conversion_data
[i
] = ad7879_read(ts
->bus
,
707 AD7879_REG_XPLUS
+ i
);
710 static int __devinit
ad7879_probe(struct i2c_client
*client
,
711 const struct i2c_device_id
*id
)
716 if (!i2c_check_functionality(client
->adapter
,
717 I2C_FUNC_SMBUS_WORD_DATA
)) {
718 dev_err(&client
->dev
, "SMBUS Word Data not Supported\n");
722 ts
= kzalloc(sizeof(struct ad7879
), GFP_KERNEL
);
726 i2c_set_clientdata(client
, ts
);
729 error
= ad7879_construct(client
, ts
);
731 i2c_set_clientdata(client
, NULL
);
738 static int __devexit
ad7879_remove(struct i2c_client
*client
)
740 struct ad7879
*ts
= dev_get_drvdata(&client
->dev
);
742 ad7879_destroy(client
, ts
);
743 i2c_set_clientdata(client
, NULL
);
749 static const struct i2c_device_id ad7879_id
[] = {
754 MODULE_DEVICE_TABLE(i2c
, ad7879_id
);
756 static struct i2c_driver ad7879_driver
= {
759 .owner
= THIS_MODULE
,
761 .probe
= ad7879_probe
,
762 .remove
= __devexit_p(ad7879_remove
),
763 .suspend
= ad7879_suspend
,
764 .resume
= ad7879_resume
,
765 .id_table
= ad7879_id
,
768 static int __init
ad7879_init(void)
770 return i2c_add_driver(&ad7879_driver
);
772 module_init(ad7879_init
);
774 static void __exit
ad7879_exit(void)
776 i2c_del_driver(&ad7879_driver
);
778 module_exit(ad7879_exit
);
781 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
782 MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
783 MODULE_LICENSE("GPL");
784 MODULE_ALIAS("spi:ad7879");