Christophe Leroy | 0eac259 | 2013-02-13 06:47:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * AD7923 SPI ADC driver |
| 3 | * |
| 4 | * Copyright 2011 Analog Devices Inc (from AD7923 Driver) |
| 5 | * Copyright 2012 CS Systemes d'Information |
| 6 | * |
| 7 | * Licensed under the GPL-2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/sysfs.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | |
| 20 | #include <linux/iio/iio.h> |
| 21 | #include <linux/iio/sysfs.h> |
| 22 | #include <linux/iio/buffer.h> |
| 23 | #include <linux/iio/trigger_consumer.h> |
| 24 | #include <linux/iio/triggered_buffer.h> |
| 25 | |
| 26 | #define AD7923_WRITE_CR (1 << 11) /* write control register */ |
| 27 | #define AD7923_RANGE (1 << 1) /* range to REFin */ |
| 28 | #define AD7923_CODING (1 << 0) /* coding is straight binary */ |
| 29 | #define AD7923_PM_MODE_AS (1) /* auto shutdown */ |
| 30 | #define AD7923_PM_MODE_FS (2) /* full shutdown */ |
| 31 | #define AD7923_PM_MODE_OPS (3) /* normal operation */ |
| 32 | #define AD7923_CHANNEL_0 (0) /* analog input 0 */ |
| 33 | #define AD7923_CHANNEL_1 (1) /* analog input 1 */ |
| 34 | #define AD7923_CHANNEL_2 (2) /* analog input 2 */ |
| 35 | #define AD7923_CHANNEL_3 (3) /* analog input 3 */ |
| 36 | #define AD7923_SEQUENCE_OFF (0) /* no sequence fonction */ |
| 37 | #define AD7923_SEQUENCE_PROTECT (2) /* no interrupt write cycle */ |
| 38 | #define AD7923_SEQUENCE_ON (3) /* continuous sequence */ |
| 39 | |
| 40 | #define AD7923_MAX_CHAN 4 |
| 41 | |
| 42 | #define AD7923_PM_MODE_WRITE(mode) (mode << 4) /* write mode */ |
| 43 | #define AD7923_CHANNEL_WRITE(channel) (channel << 6) /* write channel */ |
| 44 | #define AD7923_SEQUENCE_WRITE(sequence) (((sequence & 1) << 3) \ |
| 45 | + ((sequence & 2) << 9)) |
| 46 | /* write sequence fonction */ |
| 47 | /* left shift for CR : bit 11 transmit in first */ |
| 48 | #define AD7923_SHIFT_REGISTER 4 |
| 49 | |
| 50 | /* val = value, dec = left shift, bits = number of bits of the mask */ |
| 51 | #define EXTRACT(val, dec, bits) ((val >> dec) & ((1 << bits) - 1)) |
| 52 | |
| 53 | struct ad7923_state { |
| 54 | struct spi_device *spi; |
| 55 | struct spi_transfer ring_xfer[5]; |
| 56 | struct spi_transfer scan_single_xfer[2]; |
| 57 | struct spi_message ring_msg; |
| 58 | struct spi_message scan_single_msg; |
| 59 | /* |
| 60 | * DMA (thus cache coherency maintenance) requires the |
| 61 | * transfer buffers to live in their own cache lines. |
| 62 | */ |
| 63 | __be16 rx_buf[4] ____cacheline_aligned; |
| 64 | __be16 tx_buf[4]; |
| 65 | }; |
| 66 | |
| 67 | #define AD7923_V_CHAN(index) \ |
| 68 | { \ |
| 69 | .type = IIO_VOLTAGE, \ |
| 70 | .indexed = 1, \ |
| 71 | .channel = index, \ |
Jonathan Cameron | 7657719 | 2013-03-03 12:26:47 +0000 | [diff] [blame] | 72 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 73 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Christophe Leroy | 0eac259 | 2013-02-13 06:47:00 +0000 | [diff] [blame] | 74 | .address = index, \ |
| 75 | .scan_index = index, \ |
| 76 | .scan_type = { \ |
| 77 | .sign = 'u', \ |
| 78 | .realbits = 12, \ |
| 79 | .storagebits = 16, \ |
| 80 | .endianness = IIO_BE, \ |
| 81 | }, \ |
| 82 | } |
| 83 | |
| 84 | static const struct iio_chan_spec ad7923_channels[] = { |
| 85 | AD7923_V_CHAN(0), |
| 86 | AD7923_V_CHAN(1), |
| 87 | AD7923_V_CHAN(2), |
| 88 | AD7923_V_CHAN(3), |
| 89 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * ad7923_update_scan_mode() setup the spi transfer buffer for the new scan mask |
| 94 | **/ |
| 95 | static int ad7923_update_scan_mode(struct iio_dev *indio_dev, |
| 96 | const unsigned long *active_scan_mask) |
| 97 | { |
| 98 | struct ad7923_state *st = iio_priv(indio_dev); |
| 99 | int i, cmd, len; |
| 100 | |
| 101 | len = 0; |
| 102 | for_each_set_bit(i, active_scan_mask, AD7923_MAX_CHAN) { |
| 103 | cmd = AD7923_WRITE_CR | AD7923_CODING | AD7923_RANGE | |
| 104 | AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS) | |
| 105 | AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) | |
| 106 | AD7923_CHANNEL_WRITE(i); |
| 107 | cmd <<= AD7923_SHIFT_REGISTER; |
| 108 | st->tx_buf[len++] = cpu_to_be16(cmd); |
| 109 | } |
| 110 | /* build spi ring message */ |
| 111 | st->ring_xfer[0].tx_buf = &st->tx_buf[0]; |
| 112 | st->ring_xfer[0].len = len; |
| 113 | st->ring_xfer[0].cs_change = 1; |
| 114 | |
| 115 | spi_message_init(&st->ring_msg); |
| 116 | spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg); |
| 117 | |
| 118 | for (i = 0; i < len; i++) { |
| 119 | st->ring_xfer[i + 1].rx_buf = &st->rx_buf[i]; |
| 120 | st->ring_xfer[i + 1].len = 2; |
| 121 | st->ring_xfer[i + 1].cs_change = 1; |
| 122 | spi_message_add_tail(&st->ring_xfer[i + 1], &st->ring_msg); |
| 123 | } |
| 124 | /* make sure last transfer cs_change is not set */ |
| 125 | st->ring_xfer[i + 1].cs_change = 0; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * ad7923_trigger_handler() bh of trigger launched polling to ring buffer |
| 132 | * |
| 133 | * Currently there is no option in this driver to disable the saving of |
| 134 | * timestamps within the ring. |
| 135 | **/ |
| 136 | static irqreturn_t ad7923_trigger_handler(int irq, void *p) |
| 137 | { |
| 138 | struct iio_poll_func *pf = p; |
| 139 | struct iio_dev *indio_dev = pf->indio_dev; |
| 140 | struct ad7923_state *st = iio_priv(indio_dev); |
| 141 | s64 time_ns = 0; |
| 142 | int b_sent; |
| 143 | |
| 144 | b_sent = spi_sync(st->spi, &st->ring_msg); |
| 145 | if (b_sent) |
| 146 | goto done; |
| 147 | |
| 148 | if (indio_dev->scan_timestamp) { |
| 149 | time_ns = iio_get_time_ns(); |
| 150 | memcpy((u8 *)st->rx_buf + indio_dev->scan_bytes - sizeof(s64), |
| 151 | &time_ns, sizeof(time_ns)); |
| 152 | } |
| 153 | |
| 154 | iio_push_to_buffers(indio_dev, (u8 *)st->rx_buf); |
| 155 | |
| 156 | done: |
| 157 | iio_trigger_notify_done(indio_dev->trig); |
| 158 | |
| 159 | return IRQ_HANDLED; |
| 160 | } |
| 161 | |
| 162 | static int ad7923_scan_direct(struct ad7923_state *st, unsigned ch) |
| 163 | { |
| 164 | int ret, cmd; |
| 165 | |
| 166 | cmd = AD7923_WRITE_CR | AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS) | |
| 167 | AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) | AD7923_CODING | |
| 168 | AD7923_CHANNEL_WRITE(ch) | AD7923_RANGE; |
| 169 | cmd <<= AD7923_SHIFT_REGISTER; |
| 170 | st->tx_buf[0] = cpu_to_be16(cmd); |
| 171 | |
| 172 | ret = spi_sync(st->spi, &st->scan_single_msg); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | return be16_to_cpu(st->rx_buf[0]); |
| 177 | } |
| 178 | |
| 179 | static int ad7923_read_raw(struct iio_dev *indio_dev, |
| 180 | struct iio_chan_spec const *chan, |
| 181 | int *val, |
| 182 | int *val2, |
| 183 | long m) |
| 184 | { |
| 185 | int ret; |
| 186 | struct ad7923_state *st = iio_priv(indio_dev); |
| 187 | |
| 188 | switch (m) { |
| 189 | case IIO_CHAN_INFO_RAW: |
| 190 | mutex_lock(&indio_dev->mlock); |
| 191 | if (iio_buffer_enabled(indio_dev)) |
| 192 | ret = -EBUSY; |
| 193 | else |
| 194 | ret = ad7923_scan_direct(st, chan->address); |
| 195 | mutex_unlock(&indio_dev->mlock); |
| 196 | |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | |
| 200 | if (chan->address == EXTRACT(ret, 12, 4)) |
| 201 | *val = EXTRACT(ret, 0, 12); |
Lars-Peter Clausen | 135f064 | 2013-03-04 19:30:00 +0000 | [diff] [blame^] | 202 | else |
| 203 | return -EIO; |
Christophe Leroy | 0eac259 | 2013-02-13 06:47:00 +0000 | [diff] [blame] | 204 | |
| 205 | return IIO_VAL_INT; |
| 206 | } |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | static const struct iio_info ad7923_info = { |
| 211 | .read_raw = &ad7923_read_raw, |
| 212 | .update_scan_mode = ad7923_update_scan_mode, |
| 213 | .driver_module = THIS_MODULE, |
| 214 | }; |
| 215 | |
| 216 | static int ad7923_probe(struct spi_device *spi) |
| 217 | { |
| 218 | struct ad7923_state *st; |
| 219 | struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); |
| 220 | int ret; |
| 221 | |
| 222 | if (indio_dev == NULL) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | st = iio_priv(indio_dev); |
| 226 | |
| 227 | spi_set_drvdata(spi, indio_dev); |
| 228 | |
| 229 | st->spi = spi; |
| 230 | |
| 231 | indio_dev->name = spi_get_device_id(spi)->name; |
| 232 | indio_dev->dev.parent = &spi->dev; |
| 233 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 234 | indio_dev->channels = ad7923_channels; |
| 235 | indio_dev->num_channels = ARRAY_SIZE(ad7923_channels); |
| 236 | indio_dev->info = &ad7923_info; |
| 237 | |
| 238 | /* Setup default message */ |
| 239 | |
| 240 | st->scan_single_xfer[0].tx_buf = &st->tx_buf[0]; |
| 241 | st->scan_single_xfer[0].len = 2; |
| 242 | st->scan_single_xfer[0].cs_change = 1; |
| 243 | st->scan_single_xfer[1].rx_buf = &st->rx_buf[0]; |
| 244 | st->scan_single_xfer[1].len = 2; |
| 245 | |
| 246 | spi_message_init(&st->scan_single_msg); |
| 247 | spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg); |
| 248 | spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg); |
| 249 | |
| 250 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 251 | &ad7923_trigger_handler, NULL); |
| 252 | if (ret) |
| 253 | goto error_free; |
| 254 | |
| 255 | ret = iio_device_register(indio_dev); |
| 256 | if (ret) |
| 257 | goto error_cleanup_ring; |
| 258 | |
| 259 | return 0; |
| 260 | |
| 261 | error_cleanup_ring: |
| 262 | iio_triggered_buffer_cleanup(indio_dev); |
| 263 | error_free: |
| 264 | iio_device_free(indio_dev); |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int ad7923_remove(struct spi_device *spi) |
| 270 | { |
| 271 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 272 | |
| 273 | iio_device_unregister(indio_dev); |
| 274 | iio_triggered_buffer_cleanup(indio_dev); |
| 275 | iio_device_free(indio_dev); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static const struct spi_device_id ad7923_id[] = { |
| 281 | {"ad7923", 0}, |
| 282 | {} |
| 283 | }; |
| 284 | MODULE_DEVICE_TABLE(spi, ad7923_id); |
| 285 | |
| 286 | static struct spi_driver ad7923_driver = { |
| 287 | .driver = { |
| 288 | .name = "ad7923", |
| 289 | .owner = THIS_MODULE, |
| 290 | }, |
| 291 | .probe = ad7923_probe, |
| 292 | .remove = ad7923_remove, |
| 293 | .id_table = ad7923_id, |
| 294 | }; |
| 295 | module_spi_driver(ad7923_driver); |
| 296 | |
| 297 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 298 | MODULE_AUTHOR("Patrick Vasseur <patrick.vasseur@c-s.fr>"); |
| 299 | MODULE_DESCRIPTION("Analog Devices AD7923 ADC"); |
| 300 | MODULE_LICENSE("GPL v2"); |