]> Pileus Git - ~andy/linux/blob - drivers/input/touchscreen/ad7879-spi.c
Input: ad7879 - consolidate PM methods
[~andy/linux] / drivers / input / touchscreen / ad7879-spi.c
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/pm.h>
11 #include <linux/spi/spi.h>
12
13 #include "ad7879.h"
14
15 #define AD7879_DEVID            0x7A    /* AD7879/AD7889 */
16
17 #define MAX_SPI_FREQ_HZ      5000000
18 #define AD7879_CMD_MAGIC     0xE000
19 #define AD7879_CMD_READ      (1 << 10)
20 #define AD7879_CMD(reg)      (AD7879_CMD_MAGIC | ((reg) & 0xF))
21 #define AD7879_WRITECMD(reg) (AD7879_CMD(reg))
22 #define AD7879_READCMD(reg)  (AD7879_CMD(reg) | AD7879_CMD_READ)
23
24 /*
25  * ad7879_read/write are only used for initial setup and for sysfs controls.
26  * The main traffic is done in ad7879_collect().
27  */
28
29 static int ad7879_spi_xfer(struct spi_device *spi,
30                            u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
31 {
32         struct spi_message msg;
33         struct spi_transfer *xfers;
34         void *spi_data;
35         u16 *command;
36         u16 *_rx_buf = _rx_buf; /* shut gcc up */
37         u8 idx;
38         int ret;
39
40         xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
41         if (!spi_data)
42                 return -ENOMEM;
43
44         spi_message_init(&msg);
45
46         command = spi_data;
47         command[0] = cmd;
48         if (count == 1) {
49                 /* ad7879_spi_{read,write} gave us buf on stack */
50                 command[1] = *tx_buf;
51                 tx_buf = &command[1];
52                 _rx_buf = rx_buf;
53                 rx_buf = &command[2];
54         }
55
56         ++xfers;
57         xfers[0].tx_buf = command;
58         xfers[0].len = 2;
59         spi_message_add_tail(&xfers[0], &msg);
60         ++xfers;
61
62         for (idx = 0; idx < count; ++idx) {
63                 if (rx_buf)
64                         xfers[idx].rx_buf = &rx_buf[idx];
65                 if (tx_buf)
66                         xfers[idx].tx_buf = &tx_buf[idx];
67                 xfers[idx].len = 2;
68                 spi_message_add_tail(&xfers[idx], &msg);
69         }
70
71         ret = spi_sync(spi, &msg);
72
73         if (count == 1)
74                 _rx_buf[0] = command[2];
75
76         kfree(spi_data);
77
78         return ret;
79 }
80
81 static int ad7879_spi_multi_read(struct device *dev,
82                                  u8 first_reg, u8 count, u16 *buf)
83 {
84         struct spi_device *spi = to_spi_device(dev);
85
86         return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
87 }
88
89 static int ad7879_spi_read(struct device *dev, u8 reg)
90 {
91         struct spi_device *spi = to_spi_device(dev);
92         u16 ret, dummy;
93
94         return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
95 }
96
97 static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
98 {
99         struct spi_device *spi = to_spi_device(dev);
100         u16 dummy;
101
102         return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
103 }
104
105 static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
106         .bustype        = BUS_SPI,
107         .read           = ad7879_spi_read,
108         .multi_read     = ad7879_spi_multi_read,
109         .write          = ad7879_spi_write,
110 };
111
112 static int __devinit ad7879_spi_probe(struct spi_device *spi)
113 {
114         struct ad7879 *ts;
115         int err;
116
117         /* don't exceed max specified SPI CLK frequency */
118         if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
119                 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
120                 return -EINVAL;
121         }
122
123         spi->bits_per_word = 16;
124         err = spi_setup(spi);
125         if (err) {
126                 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
127                 return err;
128         }
129
130         ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
131         if (IS_ERR(ts))
132                 return PTR_ERR(ts);
133
134         spi_set_drvdata(spi, ts);
135
136         return 0;
137 }
138
139 static int __devexit ad7879_spi_remove(struct spi_device *spi)
140 {
141         struct ad7879 *ts = spi_get_drvdata(spi);
142
143         ad7879_remove(ts);
144         spi_set_drvdata(spi, NULL);
145
146         return 0;
147 }
148
149 static struct spi_driver ad7879_spi_driver = {
150         .driver = {
151                 .name   = "ad7879",
152                 .bus    = &spi_bus_type,
153                 .owner  = THIS_MODULE,
154                 .pm     = &ad7879_pm_ops,
155         },
156         .probe          = ad7879_spi_probe,
157         .remove         = __devexit_p(ad7879_spi_remove),
158 };
159
160 static int __init ad7879_spi_init(void)
161 {
162         return spi_register_driver(&ad7879_spi_driver);
163 }
164 module_init(ad7879_spi_init);
165
166 static void __exit ad7879_spi_exit(void)
167 {
168         spi_unregister_driver(&ad7879_spi_driver);
169 }
170 module_exit(ad7879_spi_exit);
171
172 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
173 MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
174 MODULE_LICENSE("GPL");
175 MODULE_ALIAS("spi:ad7879");