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