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 */
10 #include <linux/spi/spi.h>
14 #define AD7879_DEVID 0x7A /* AD7879/AD7889 */
16 #define MAX_SPI_FREQ_HZ 5000000
17 #define AD7879_CMD_MAGIC 0xE000
18 #define AD7879_CMD_READ (1 << 10)
19 #define AD7879_CMD(reg) (AD7879_CMD_MAGIC | ((reg) & 0xF))
20 #define AD7879_WRITECMD(reg) (AD7879_CMD(reg))
21 #define AD7879_READCMD(reg) (AD7879_CMD(reg) | AD7879_CMD_READ)
24 static int ad7879_spi_suspend(struct spi_device *spi, pm_message_t message)
26 struct ad7879 *ts = spi_get_drvdata(spi);
33 static int ad7879_spi_resume(struct spi_device *spi)
35 struct ad7879 *ts = spi_get_drvdata(spi);
42 # define ad7879_spi_suspend NULL
43 # define ad7879_spi_resume NULL
47 * ad7879_read/write are only used for initial setup and for sysfs controls.
48 * The main traffic is done in ad7879_collect().
51 static int ad7879_spi_xfer(struct spi_device *spi,
52 u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
54 struct spi_message msg;
55 struct spi_transfer *xfers;
58 u16 *_rx_buf = _rx_buf; /* shut gcc up */
62 xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
66 spi_message_init(&msg);
71 /* ad7879_spi_{read,write} gave us buf on stack */
79 xfers[0].tx_buf = command;
81 spi_message_add_tail(&xfers[0], &msg);
84 for (idx = 0; idx < count; ++idx) {
86 xfers[idx].rx_buf = &rx_buf[idx];
88 xfers[idx].tx_buf = &tx_buf[idx];
90 spi_message_add_tail(&xfers[idx], &msg);
93 ret = spi_sync(spi, &msg);
96 _rx_buf[0] = command[2];
103 static int ad7879_spi_multi_read(struct device *dev,
104 u8 first_reg, u8 count, u16 *buf)
106 struct spi_device *spi = to_spi_device(dev);
108 return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
111 static int ad7879_spi_read(struct device *dev, u8 reg)
113 struct spi_device *spi = to_spi_device(dev);
116 return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
119 static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
121 struct spi_device *spi = to_spi_device(dev);
124 return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
127 static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
129 .read = ad7879_spi_read,
130 .multi_read = ad7879_spi_multi_read,
131 .write = ad7879_spi_write,
134 static int __devinit ad7879_spi_probe(struct spi_device *spi)
139 /* don't exceed max specified SPI CLK frequency */
140 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
141 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
145 spi->bits_per_word = 16;
146 err = spi_setup(spi);
148 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
152 ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
156 spi_set_drvdata(spi, ts);
161 static int __devexit ad7879_spi_remove(struct spi_device *spi)
163 struct ad7879 *ts = spi_get_drvdata(spi);
166 spi_set_drvdata(spi, NULL);
171 static struct spi_driver ad7879_spi_driver = {
174 .bus = &spi_bus_type,
175 .owner = THIS_MODULE,
177 .probe = ad7879_spi_probe,
178 .remove = __devexit_p(ad7879_spi_remove),
179 .suspend = ad7879_spi_suspend,
180 .resume = ad7879_spi_resume,
183 static int __init ad7879_spi_init(void)
185 return spi_register_driver(&ad7879_spi_driver);
187 module_init(ad7879_spi_init);
189 static void __exit ad7879_spi_exit(void)
191 spi_unregister_driver(&ad7879_spi_driver);
193 module_exit(ad7879_spi_exit);
196 MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
197 MODULE_LICENSE("GPL");
198 MODULE_ALIAS("spi:ad7879");