Mike Frysinger | 4397c98 | 2010-06-30 01:40:52 -0700 | [diff] [blame] | 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 |
| 24 | static int ad7879_spi_suspend(struct spi_device *spi, pm_message_t message) |
| 25 | { |
| 26 | struct ad7879 *ts = spi_get_drvdata(spi); |
| 27 | |
Dmitry Torokhov | 14fbbc3 | 2010-06-30 14:50:51 -0700 | [diff] [blame] | 28 | ad7879_suspend(ts); |
Mike Frysinger | 4397c98 | 2010-06-30 01:40:52 -0700 | [diff] [blame] | 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static int ad7879_spi_resume(struct spi_device *spi) |
| 34 | { |
| 35 | struct ad7879 *ts = spi_get_drvdata(spi); |
| 36 | |
Dmitry Torokhov | 14fbbc3 | 2010-06-30 14:50:51 -0700 | [diff] [blame] | 37 | ad7879_resume(ts); |
Mike Frysinger | 4397c98 | 2010-06-30 01:40:52 -0700 | [diff] [blame] | 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 | |
| 51 | static 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 | |
| 103 | static 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 | |
| 111 | static 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 | |
| 119 | static 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 | |
| 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, |
| 132 | }; |
| 133 | |
| 134 | static int __devinit ad7879_spi_probe(struct spi_device *spi) |
| 135 | { |
| 136 | struct ad7879 *ts; |
Michael Hennerich | 447b906 | 2010-06-30 14:51:09 -0700 | [diff] [blame] | 137 | int err; |
Mike Frysinger | 4397c98 | 2010-06-30 01:40:52 -0700 | [diff] [blame] | 138 | |
| 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; |
| 143 | } |
| 144 | |
Michael Hennerich | 447b906 | 2010-06-30 14:51:09 -0700 | [diff] [blame] | 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; |
| 150 | } |
| 151 | |
Mike Frysinger | 4397c98 | 2010-06-30 01:40:52 -0700 | [diff] [blame] | 152 | ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops); |
| 153 | if (IS_ERR(ts)) |
| 154 | return PTR_ERR(ts); |
| 155 | |
| 156 | spi_set_drvdata(spi, ts); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int __devexit ad7879_spi_remove(struct spi_device *spi) |
| 162 | { |
| 163 | struct ad7879 *ts = spi_get_drvdata(spi); |
| 164 | |
| 165 | ad7879_remove(ts); |
| 166 | spi_set_drvdata(spi, NULL); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static struct spi_driver ad7879_spi_driver = { |
| 172 | .driver = { |
| 173 | .name = "ad7879", |
| 174 | .bus = &spi_bus_type, |
| 175 | .owner = THIS_MODULE, |
| 176 | }, |
| 177 | .probe = ad7879_spi_probe, |
| 178 | .remove = __devexit_p(ad7879_spi_remove), |
| 179 | .suspend = ad7879_spi_suspend, |
| 180 | .resume = ad7879_spi_resume, |
| 181 | }; |
| 182 | |
| 183 | static int __init ad7879_spi_init(void) |
| 184 | { |
| 185 | return spi_register_driver(&ad7879_spi_driver); |
| 186 | } |
| 187 | module_init(ad7879_spi_init); |
| 188 | |
| 189 | static void __exit ad7879_spi_exit(void) |
| 190 | { |
| 191 | spi_unregister_driver(&ad7879_spi_driver); |
| 192 | } |
| 193 | module_exit(ad7879_spi_exit); |
| 194 | |
| 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"); |