blob: b1643c8fa7c9a1bca311e90e2cfc39e7ba836e2f [file] [log] [blame]
Mike Frysinger4397c982010-06-30 01:40:52 -07001/*
2 * AD7879/AD7889 touchscreen (SPI bus)
3 *
4 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/input.h> /* BUS_SPI */
Mark Brownc7848c82011-01-20 22:48:44 -080010#include <linux/pm.h>
Mike Frysinger4397c982010-06-30 01:40:52 -070011#include <linux/spi/spi.h>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040012#include <linux/module.h>
Mike Frysinger4397c982010-06-30 01:40:52 -070013
14#include "ad7879.h"
15
16#define AD7879_DEVID 0x7A /* AD7879/AD7889 */
17
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)
24
Mark Brownc7848c82011-01-20 22:48:44 -080025#ifdef CONFIG_PM_SLEEP
26static int ad7879_spi_suspend(struct device *dev)
Mike Frysinger4397c982010-06-30 01:40:52 -070027{
Mark Brownc7848c82011-01-20 22:48:44 -080028 struct spi_device *spi = to_spi_device(dev);
Mike Frysinger4397c982010-06-30 01:40:52 -070029 struct ad7879 *ts = spi_get_drvdata(spi);
30
Dmitry Torokhov14fbbc32010-06-30 14:50:51 -070031 ad7879_suspend(ts);
Mike Frysinger4397c982010-06-30 01:40:52 -070032
33 return 0;
34}
35
Mark Brownc7848c82011-01-20 22:48:44 -080036static int ad7879_spi_resume(struct device *dev)
Mike Frysinger4397c982010-06-30 01:40:52 -070037{
Mark Brownc7848c82011-01-20 22:48:44 -080038 struct spi_device *spi = to_spi_device(dev);
Mike Frysinger4397c982010-06-30 01:40:52 -070039 struct ad7879 *ts = spi_get_drvdata(spi);
40
Dmitry Torokhov14fbbc32010-06-30 14:50:51 -070041 ad7879_resume(ts);
Mike Frysinger4397c982010-06-30 01:40:52 -070042
43 return 0;
44}
Mike Frysinger4397c982010-06-30 01:40:52 -070045#endif
46
Mark Brownc7848c82011-01-20 22:48:44 -080047static SIMPLE_DEV_PM_OPS(ad7879_spi_pm, ad7879_spi_suspend, ad7879_spi_resume);
48
Mike Frysinger4397c982010-06-30 01:40:52 -070049/*
50 * ad7879_read/write are only used for initial setup and for sysfs controls.
51 * The main traffic is done in ad7879_collect().
52 */
53
54static int ad7879_spi_xfer(struct spi_device *spi,
55 u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
56{
57 struct spi_message msg;
58 struct spi_transfer *xfers;
59 void *spi_data;
60 u16 *command;
61 u16 *_rx_buf = _rx_buf; /* shut gcc up */
62 u8 idx;
63 int ret;
64
65 xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
66 if (!spi_data)
67 return -ENOMEM;
68
69 spi_message_init(&msg);
70
71 command = spi_data;
72 command[0] = cmd;
73 if (count == 1) {
74 /* ad7879_spi_{read,write} gave us buf on stack */
75 command[1] = *tx_buf;
76 tx_buf = &command[1];
77 _rx_buf = rx_buf;
78 rx_buf = &command[2];
79 }
80
81 ++xfers;
82 xfers[0].tx_buf = command;
83 xfers[0].len = 2;
84 spi_message_add_tail(&xfers[0], &msg);
85 ++xfers;
86
87 for (idx = 0; idx < count; ++idx) {
88 if (rx_buf)
89 xfers[idx].rx_buf = &rx_buf[idx];
90 if (tx_buf)
91 xfers[idx].tx_buf = &tx_buf[idx];
92 xfers[idx].len = 2;
93 spi_message_add_tail(&xfers[idx], &msg);
94 }
95
96 ret = spi_sync(spi, &msg);
97
98 if (count == 1)
99 _rx_buf[0] = command[2];
100
101 kfree(spi_data);
102
103 return ret;
104}
105
106static int ad7879_spi_multi_read(struct device *dev,
107 u8 first_reg, u8 count, u16 *buf)
108{
109 struct spi_device *spi = to_spi_device(dev);
110
111 return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
112}
113
114static int ad7879_spi_read(struct device *dev, u8 reg)
115{
116 struct spi_device *spi = to_spi_device(dev);
117 u16 ret, dummy;
118
119 return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
120}
121
122static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
123{
124 struct spi_device *spi = to_spi_device(dev);
125 u16 dummy;
126
127 return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
128}
129
130static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
131 .bustype = BUS_SPI,
132 .read = ad7879_spi_read,
133 .multi_read = ad7879_spi_multi_read,
134 .write = ad7879_spi_write,
135};
136
137static int __devinit ad7879_spi_probe(struct spi_device *spi)
138{
139 struct ad7879 *ts;
Michael Hennerich447b90652010-06-30 14:51:09 -0700140 int err;
Mike Frysinger4397c982010-06-30 01:40:52 -0700141
142 /* don't exceed max specified SPI CLK frequency */
143 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
144 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
145 return -EINVAL;
146 }
147
Michael Hennerich447b90652010-06-30 14:51:09 -0700148 spi->bits_per_word = 16;
149 err = spi_setup(spi);
150 if (err) {
151 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
152 return err;
153 }
154
Mike Frysinger4397c982010-06-30 01:40:52 -0700155 ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
156 if (IS_ERR(ts))
157 return PTR_ERR(ts);
158
159 spi_set_drvdata(spi, ts);
160
161 return 0;
162}
163
164static int __devexit ad7879_spi_remove(struct spi_device *spi)
165{
166 struct ad7879 *ts = spi_get_drvdata(spi);
167
168 ad7879_remove(ts);
169 spi_set_drvdata(spi, NULL);
170
171 return 0;
172}
173
174static struct spi_driver ad7879_spi_driver = {
175 .driver = {
176 .name = "ad7879",
177 .bus = &spi_bus_type,
178 .owner = THIS_MODULE,
Mark Brownc7848c82011-01-20 22:48:44 -0800179 .pm = &ad7879_spi_pm,
Mike Frysinger4397c982010-06-30 01:40:52 -0700180 },
181 .probe = ad7879_spi_probe,
182 .remove = __devexit_p(ad7879_spi_remove),
Mike Frysinger4397c982010-06-30 01:40:52 -0700183};
184
185static int __init ad7879_spi_init(void)
186{
187 return spi_register_driver(&ad7879_spi_driver);
188}
189module_init(ad7879_spi_init);
190
191static void __exit ad7879_spi_exit(void)
192{
193 spi_unregister_driver(&ad7879_spi_driver);
194}
195module_exit(ad7879_spi_exit);
196
197MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
198MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
199MODULE_LICENSE("GPL");
200MODULE_ALIAS("spi:ad7879");