Linux 2.6.36-rc5
[linux-2.6/next.git] / drivers / input / touchscreen / ad7879-spi.c
blob59c6e68c432507a61392993b572541dc2c8693a4
1 /*
2 * AD7879/AD7889 touchscreen (SPI bus)
4 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/input.h> /* BUS_SPI */
10 #include <linux/spi/spi.h>
12 #include "ad7879.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)
23 #ifdef CONFIG_PM
24 static int ad7879_spi_suspend(struct spi_device *spi, pm_message_t message)
26 struct ad7879 *ts = spi_get_drvdata(spi);
28 ad7879_suspend(ts);
30 return 0;
33 static int ad7879_spi_resume(struct spi_device *spi)
35 struct ad7879 *ts = spi_get_drvdata(spi);
37 ad7879_resume(ts);
39 return 0;
41 #else
42 # define ad7879_spi_suspend NULL
43 # define ad7879_spi_resume NULL
44 #endif
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;
56 void *spi_data;
57 u16 *command;
58 u16 *_rx_buf = _rx_buf; /* shut gcc up */
59 u8 idx;
60 int ret;
62 xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
63 if (!spi_data)
64 return -ENOMEM;
66 spi_message_init(&msg);
68 command = spi_data;
69 command[0] = cmd;
70 if (count == 1) {
71 /* ad7879_spi_{read,write} gave us buf on stack */
72 command[1] = *tx_buf;
73 tx_buf = &command[1];
74 _rx_buf = rx_buf;
75 rx_buf = &command[2];
78 ++xfers;
79 xfers[0].tx_buf = command;
80 xfers[0].len = 2;
81 spi_message_add_tail(&xfers[0], &msg);
82 ++xfers;
84 for (idx = 0; idx < count; ++idx) {
85 if (rx_buf)
86 xfers[idx].rx_buf = &rx_buf[idx];
87 if (tx_buf)
88 xfers[idx].tx_buf = &tx_buf[idx];
89 xfers[idx].len = 2;
90 spi_message_add_tail(&xfers[idx], &msg);
93 ret = spi_sync(spi, &msg);
95 if (count == 1)
96 _rx_buf[0] = command[2];
98 kfree(spi_data);
100 return ret;
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);
114 u16 ret, dummy;
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);
122 u16 dummy;
124 return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
127 static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
128 .bustype = BUS_SPI,
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)
136 struct ad7879 *ts;
137 int err;
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);
142 return -EINVAL;
145 spi->bits_per_word = 16;
146 err = spi_setup(spi);
147 if (err) {
148 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
149 return err;
152 ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
153 if (IS_ERR(ts))
154 return PTR_ERR(ts);
156 spi_set_drvdata(spi, ts);
158 return 0;
161 static int __devexit ad7879_spi_remove(struct spi_device *spi)
163 struct ad7879 *ts = spi_get_drvdata(spi);
165 ad7879_remove(ts);
166 spi_set_drvdata(spi, NULL);
168 return 0;
171 static struct spi_driver ad7879_spi_driver = {
172 .driver = {
173 .name = "ad7879",
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);
195 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
196 MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
197 MODULE_LICENSE("GPL");
198 MODULE_ALIAS("spi:ad7879");