]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/touchscreen/ad7879-spi.c
Input: ad7879 - add open and close methods
[net-next-2.6.git] / drivers / input / touchscreen / ad7879-spi.c
CommitLineData
4397c98a
MF
1/*
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 */
10#include <linux/spi/spi.h>
11
12#include "ad7879.h"
13
14#define AD7879_DEVID 0x7A /* AD7879/AD7889 */
15
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)
22
23#ifdef CONFIG_PM
24static int ad7879_spi_suspend(struct spi_device *spi, pm_message_t message)
25{
26 struct ad7879 *ts = spi_get_drvdata(spi);
27
14fbbc36 28 ad7879_suspend(ts);
4397c98a
MF
29
30 return 0;
31}
32
33static int ad7879_spi_resume(struct spi_device *spi)
34{
35 struct ad7879 *ts = spi_get_drvdata(spi);
36
14fbbc36 37 ad7879_resume(ts);
4397c98a
MF
38
39 return 0;
40}
41#else
42# define ad7879_spi_suspend NULL
43# define ad7879_spi_resume NULL
44#endif
45
46/*
47 * ad7879_read/write are only used for initial setup and for sysfs controls.
48 * The main traffic is done in ad7879_collect().
49 */
50
51static int ad7879_spi_xfer(struct spi_device *spi,
52 u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
53{
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;
61
62 xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
63 if (!spi_data)
64 return -ENOMEM;
65
66 spi_message_init(&msg);
67
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];
76 }
77
78 ++xfers;
79 xfers[0].tx_buf = command;
80 xfers[0].len = 2;
81 spi_message_add_tail(&xfers[0], &msg);
82 ++xfers;
83
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);
91 }
92
93 ret = spi_sync(spi, &msg);
94
95 if (count == 1)
96 _rx_buf[0] = command[2];
97
98 kfree(spi_data);
99
100 return ret;
101}
102
103static int ad7879_spi_multi_read(struct device *dev,
104 u8 first_reg, u8 count, u16 *buf)
105{
106 struct spi_device *spi = to_spi_device(dev);
107
108 return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
109}
110
111static int ad7879_spi_read(struct device *dev, u8 reg)
112{
113 struct spi_device *spi = to_spi_device(dev);
114 u16 ret, dummy;
115
116 return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
117}
118
119static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
120{
121 struct spi_device *spi = to_spi_device(dev);
122 u16 dummy;
123
124 return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
125}
126
127static 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,
132};
133
134static int __devinit ad7879_spi_probe(struct spi_device *spi)
135{
136 struct ad7879 *ts;
137
138 /* don't exceed max specified SPI CLK frequency */
139 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
140 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
141 return -EINVAL;
142 }
143
144 ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
145 if (IS_ERR(ts))
146 return PTR_ERR(ts);
147
148 spi_set_drvdata(spi, ts);
149
150 return 0;
151}
152
153static int __devexit ad7879_spi_remove(struct spi_device *spi)
154{
155 struct ad7879 *ts = spi_get_drvdata(spi);
156
157 ad7879_remove(ts);
158 spi_set_drvdata(spi, NULL);
159
160 return 0;
161}
162
163static struct spi_driver ad7879_spi_driver = {
164 .driver = {
165 .name = "ad7879",
166 .bus = &spi_bus_type,
167 .owner = THIS_MODULE,
168 },
169 .probe = ad7879_spi_probe,
170 .remove = __devexit_p(ad7879_spi_remove),
171 .suspend = ad7879_spi_suspend,
172 .resume = ad7879_spi_resume,
173};
174
175static int __init ad7879_spi_init(void)
176{
177 return spi_register_driver(&ad7879_spi_driver);
178}
179module_init(ad7879_spi_init);
180
181static void __exit ad7879_spi_exit(void)
182{
183 spi_unregister_driver(&ad7879_spi_driver);
184}
185module_exit(ad7879_spi_exit);
186
187MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
188MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
189MODULE_LICENSE("GPL");
190MODULE_ALIAS("spi:ad7879");