2 * AD7879/AD7889 touchscreen (SPI bus)
4 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/input.h> /* BUS_SPI */
11 #include <linux/spi/spi.h>
15 #define AD7879_DEVID 0x7A /* AD7879/AD7889 */
17 #define MAX_SPI_FREQ_HZ 5000000
18 #define AD7879_CMD_MAGIC 0xE000
19 #define AD7879_CMD_READ (1 << 10)
20 #define AD7879_CMD(reg) (AD7879_CMD_MAGIC | ((reg) & 0xF))
21 #define AD7879_WRITECMD(reg) (AD7879_CMD(reg))
22 #define AD7879_READCMD(reg) (AD7879_CMD(reg) | AD7879_CMD_READ)
24 #ifdef CONFIG_PM_SLEEP
25 static int ad7879_spi_suspend(struct device *dev)
27 struct spi_device *spi = to_spi_device(dev);
28 struct ad7879 *ts = spi_get_drvdata(spi);
35 static int ad7879_spi_resume(struct device *dev)
37 struct spi_device *spi = to_spi_device(dev);
38 struct ad7879 *ts = spi_get_drvdata(spi);
46 static SIMPLE_DEV_PM_OPS(ad7879_spi_pm, ad7879_spi_suspend, ad7879_spi_resume);
49 * ad7879_read/write are only used for initial setup and for sysfs controls.
50 * The main traffic is done in ad7879_collect().
53 static int ad7879_spi_xfer(struct spi_device *spi,
54 u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
56 struct spi_message msg;
57 struct spi_transfer *xfers;
60 u16 *_rx_buf = _rx_buf; /* shut gcc up */
64 xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
68 spi_message_init(&msg);
73 /* ad7879_spi_{read,write} gave us buf on stack */
81 xfers[0].tx_buf = command;
83 spi_message_add_tail(&xfers[0], &msg);
86 for (idx = 0; idx < count; ++idx) {
88 xfers[idx].rx_buf = &rx_buf[idx];
90 xfers[idx].tx_buf = &tx_buf[idx];
92 spi_message_add_tail(&xfers[idx], &msg);
95 ret = spi_sync(spi, &msg);
98 _rx_buf[0] = command[2];
105 static int ad7879_spi_multi_read(struct device *dev,
106 u8 first_reg, u8 count, u16 *buf)
108 struct spi_device *spi = to_spi_device(dev);
110 return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
113 static int ad7879_spi_read(struct device *dev, u8 reg)
115 struct spi_device *spi = to_spi_device(dev);
118 return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
121 static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
123 struct spi_device *spi = to_spi_device(dev);
126 return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
129 static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
131 .read = ad7879_spi_read,
132 .multi_read = ad7879_spi_multi_read,
133 .write = ad7879_spi_write,
136 static int __devinit ad7879_spi_probe(struct spi_device *spi)
141 /* don't exceed max specified SPI CLK frequency */
142 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
143 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
147 spi->bits_per_word = 16;
148 err = spi_setup(spi);
150 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
154 ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
158 spi_set_drvdata(spi, ts);
163 static int __devexit ad7879_spi_remove(struct spi_device *spi)
165 struct ad7879 *ts = spi_get_drvdata(spi);
168 spi_set_drvdata(spi, NULL);
173 static struct spi_driver ad7879_spi_driver = {
176 .bus = &spi_bus_type,
177 .owner = THIS_MODULE,
178 .pm = &ad7879_spi_pm,
180 .probe = ad7879_spi_probe,
181 .remove = __devexit_p(ad7879_spi_remove),
184 static int __init ad7879_spi_init(void)
186 return spi_register_driver(&ad7879_spi_driver);
188 module_init(ad7879_spi_init);
190 static void __exit ad7879_spi_exit(void)
192 spi_unregister_driver(&ad7879_spi_driver);
194 module_exit(ad7879_spi_exit);
196 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
197 MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
198 MODULE_LICENSE("GPL");
199 MODULE_ALIAS("spi:ad7879");