Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / input / touchscreen / ad7879-spi.c
blob1a7b1143536e1d4a68dd6fb9e1c85cf992afee04
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/pm.h>
11 #include <linux/spi/spi.h>
12 #include <linux/module.h>
14 #include "ad7879.h"
16 #define AD7879_DEVID 0x7A /* AD7879/AD7889 */
18 #define MAX_SPI_FREQ_HZ 5000000
19 #define AD7879_CMD_MAGIC 0xE000
20 #define AD7879_CMD_READ (1 << 10)
21 #define AD7879_CMD(reg) (AD7879_CMD_MAGIC | ((reg) & 0xF))
22 #define AD7879_WRITECMD(reg) (AD7879_CMD(reg))
23 #define AD7879_READCMD(reg) (AD7879_CMD(reg) | AD7879_CMD_READ)
26 * ad7879_read/write are only used for initial setup and for sysfs controls.
27 * The main traffic is done in ad7879_collect().
30 static int ad7879_spi_xfer(struct spi_device *spi,
31 u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
33 struct spi_message msg;
34 struct spi_transfer *xfers;
35 void *spi_data;
36 u16 *command;
37 u16 *_rx_buf = _rx_buf; /* shut gcc up */
38 u8 idx;
39 int ret;
41 xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
42 if (!spi_data)
43 return -ENOMEM;
45 spi_message_init(&msg);
47 command = spi_data;
48 command[0] = cmd;
49 if (count == 1) {
50 /* ad7879_spi_{read,write} gave us buf on stack */
51 command[1] = *tx_buf;
52 tx_buf = &command[1];
53 _rx_buf = rx_buf;
54 rx_buf = &command[2];
57 ++xfers;
58 xfers[0].tx_buf = command;
59 xfers[0].len = 2;
60 spi_message_add_tail(&xfers[0], &msg);
61 ++xfers;
63 for (idx = 0; idx < count; ++idx) {
64 if (rx_buf)
65 xfers[idx].rx_buf = &rx_buf[idx];
66 if (tx_buf)
67 xfers[idx].tx_buf = &tx_buf[idx];
68 xfers[idx].len = 2;
69 spi_message_add_tail(&xfers[idx], &msg);
72 ret = spi_sync(spi, &msg);
74 if (count == 1)
75 _rx_buf[0] = command[2];
77 kfree(spi_data);
79 return ret;
82 static int ad7879_spi_multi_read(struct device *dev,
83 u8 first_reg, u8 count, u16 *buf)
85 struct spi_device *spi = to_spi_device(dev);
87 return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
90 static int ad7879_spi_read(struct device *dev, u8 reg)
92 struct spi_device *spi = to_spi_device(dev);
93 u16 ret, dummy;
95 return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
98 static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
100 struct spi_device *spi = to_spi_device(dev);
101 u16 dummy;
103 return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
106 static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
107 .bustype = BUS_SPI,
108 .read = ad7879_spi_read,
109 .multi_read = ad7879_spi_multi_read,
110 .write = ad7879_spi_write,
113 static int ad7879_spi_probe(struct spi_device *spi)
115 struct ad7879 *ts;
116 int err;
118 /* don't exceed max specified SPI CLK frequency */
119 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
120 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
121 return -EINVAL;
124 spi->bits_per_word = 16;
125 err = spi_setup(spi);
126 if (err) {
127 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
128 return err;
131 ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
132 if (IS_ERR(ts))
133 return PTR_ERR(ts);
135 spi_set_drvdata(spi, ts);
137 return 0;
140 static int ad7879_spi_remove(struct spi_device *spi)
142 struct ad7879 *ts = spi_get_drvdata(spi);
144 ad7879_remove(ts);
146 return 0;
149 static struct spi_driver ad7879_spi_driver = {
150 .driver = {
151 .name = "ad7879",
152 .owner = THIS_MODULE,
153 .pm = &ad7879_pm_ops,
155 .probe = ad7879_spi_probe,
156 .remove = ad7879_spi_remove,
159 module_spi_driver(ad7879_spi_driver);
161 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
162 MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
163 MODULE_LICENSE("GPL");
164 MODULE_ALIAS("spi:ad7879");