Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AD7887 SPI ADC driver |
| 3 | * |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 4 | * Copyright 2010-2011 Analog Devices Inc. |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 5 | * |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 6 | * Licensed under the GPL-2. |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/sysfs.h> |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 13 | #include <linux/spi/spi.h> |
| 14 | #include <linux/regulator/consumer.h> |
| 15 | #include <linux/err.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 18 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/sysfs.h> |
| 21 | #include <linux/iio/buffer.h> |
Jonathan Cameron | cdf3870 | 2011-08-12 17:08:43 +0100 | [diff] [blame] | 22 | |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 23 | #include <linux/iio/trigger_consumer.h> |
| 24 | #include <linux/iio/triggered_buffer.h> |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 25 | |
Lars-Peter Clausen | 4eb3ccf | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 26 | #include <linux/platform_data/ad7887.h> |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 27 | |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 28 | #define AD7887_REF_DIS (1 << 5) /* on-chip reference disable */ |
| 29 | #define AD7887_DUAL (1 << 4) /* dual-channel mode */ |
| 30 | #define AD7887_CH_AIN1 (1 << 3) /* convert on channel 1, DUAL=1 */ |
| 31 | #define AD7887_CH_AIN0 (0 << 3) /* convert on channel 0, DUAL=0,1 */ |
| 32 | #define AD7887_PM_MODE1 (0) /* CS based shutdown */ |
| 33 | #define AD7887_PM_MODE2 (1) /* full on */ |
| 34 | #define AD7887_PM_MODE3 (2) /* auto shutdown after conversion */ |
| 35 | #define AD7887_PM_MODE4 (3) /* standby mode */ |
| 36 | |
| 37 | enum ad7887_channels { |
| 38 | AD7887_CH0, |
| 39 | AD7887_CH0_CH1, |
| 40 | AD7887_CH1, |
| 41 | }; |
| 42 | |
| 43 | #define RES_MASK(bits) ((1 << (bits)) - 1) |
| 44 | |
| 45 | /** |
| 46 | * struct ad7887_chip_info - chip specifc information |
| 47 | * @int_vref_mv: the internal reference voltage |
| 48 | * @channel: channel specification |
| 49 | */ |
| 50 | struct ad7887_chip_info { |
| 51 | u16 int_vref_mv; |
| 52 | struct iio_chan_spec channel[3]; |
| 53 | }; |
| 54 | |
| 55 | struct ad7887_state { |
| 56 | struct spi_device *spi; |
| 57 | const struct ad7887_chip_info *chip_info; |
| 58 | struct regulator *reg; |
| 59 | struct spi_transfer xfer[4]; |
| 60 | struct spi_message msg[3]; |
| 61 | struct spi_message *ring_msg; |
Lars-Peter Clausen | fce7c3e | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 62 | unsigned char tx_cmd_buf[4]; |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * DMA (thus cache coherency maintenance) requires the |
| 66 | * transfer buffers to live in their own cache lines. |
| 67 | * Buffer needs to be large enough to hold two 16 bit samples and a |
| 68 | * 64 bit aligned 64 bit timestamp. |
| 69 | */ |
| 70 | unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)] |
| 71 | ____cacheline_aligned; |
| 72 | }; |
| 73 | |
| 74 | enum ad7887_supported_device_ids { |
| 75 | ID_AD7887 |
| 76 | }; |
| 77 | |
| 78 | static int ad7887_ring_preenable(struct iio_dev *indio_dev) |
| 79 | { |
| 80 | struct ad7887_state *st = iio_priv(indio_dev); |
| 81 | int ret; |
| 82 | |
| 83 | ret = iio_sw_buffer_preenable(indio_dev); |
| 84 | if (ret < 0) |
| 85 | return ret; |
| 86 | |
| 87 | /* We know this is a single long so can 'cheat' */ |
| 88 | switch (*indio_dev->active_scan_mask) { |
| 89 | case (1 << 0): |
| 90 | st->ring_msg = &st->msg[AD7887_CH0]; |
| 91 | break; |
| 92 | case (1 << 1): |
| 93 | st->ring_msg = &st->msg[AD7887_CH1]; |
| 94 | /* Dummy read: push CH1 setting down to hardware */ |
| 95 | spi_sync(st->spi, st->ring_msg); |
| 96 | break; |
| 97 | case ((1 << 1) | (1 << 0)): |
| 98 | st->ring_msg = &st->msg[AD7887_CH0_CH1]; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int ad7887_ring_postdisable(struct iio_dev *indio_dev) |
| 106 | { |
| 107 | struct ad7887_state *st = iio_priv(indio_dev); |
| 108 | |
| 109 | /* dummy read: restore default CH0 settin */ |
| 110 | return spi_sync(st->spi, &st->msg[AD7887_CH0]); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * ad7887_trigger_handler() bh of trigger launched polling to ring buffer |
| 115 | * |
| 116 | * Currently there is no option in this driver to disable the saving of |
| 117 | * timestamps within the ring. |
| 118 | **/ |
| 119 | static irqreturn_t ad7887_trigger_handler(int irq, void *p) |
| 120 | { |
| 121 | struct iio_poll_func *pf = p; |
| 122 | struct iio_dev *indio_dev = pf->indio_dev; |
| 123 | struct ad7887_state *st = iio_priv(indio_dev); |
| 124 | s64 time_ns; |
| 125 | int b_sent; |
| 126 | |
| 127 | b_sent = spi_sync(st->spi, st->ring_msg); |
| 128 | if (b_sent) |
| 129 | goto done; |
| 130 | |
| 131 | time_ns = iio_get_time_ns(); |
| 132 | |
| 133 | if (indio_dev->scan_timestamp) |
| 134 | memcpy(st->data + indio_dev->scan_bytes - sizeof(s64), |
| 135 | &time_ns, sizeof(time_ns)); |
| 136 | |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 137 | iio_push_to_buffers(indio_dev, st->data); |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 138 | done: |
| 139 | iio_trigger_notify_done(indio_dev->trig); |
| 140 | |
| 141 | return IRQ_HANDLED; |
| 142 | } |
| 143 | |
| 144 | static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = { |
| 145 | .preenable = &ad7887_ring_preenable, |
| 146 | .postenable = &iio_triggered_buffer_postenable, |
| 147 | .predisable = &iio_triggered_buffer_predisable, |
| 148 | .postdisable = &ad7887_ring_postdisable, |
| 149 | }; |
| 150 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 151 | static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch) |
| 152 | { |
| 153 | int ret = spi_sync(st->spi, &st->msg[ch]); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1]; |
| 158 | } |
| 159 | |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 160 | static int ad7887_read_raw(struct iio_dev *indio_dev, |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 161 | struct iio_chan_spec const *chan, |
| 162 | int *val, |
| 163 | int *val2, |
| 164 | long m) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 165 | { |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 166 | int ret; |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 167 | struct ad7887_state *st = iio_priv(indio_dev); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 168 | |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 169 | switch (m) { |
Jonathan Cameron | b11f98f | 2012-04-15 17:41:18 +0100 | [diff] [blame] | 170 | case IIO_CHAN_INFO_RAW: |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 171 | mutex_lock(&indio_dev->mlock); |
| 172 | if (iio_buffer_enabled(indio_dev)) |
Jonathan Cameron | 790d875 | 2011-12-05 22:18:23 +0000 | [diff] [blame] | 173 | ret = -EBUSY; |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 174 | else |
| 175 | ret = ad7887_scan_direct(st, chan->address); |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 176 | mutex_unlock(&indio_dev->mlock); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 177 | |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 178 | if (ret < 0) |
| 179 | return ret; |
Lars-Peter Clausen | 98efb70 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 180 | *val = ret >> chan->scan_type.shift; |
| 181 | *val &= RES_MASK(chan->scan_type.realbits); |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 182 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 183 | case IIO_CHAN_INFO_SCALE: |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 184 | if (st->reg) { |
| 185 | *val = regulator_get_voltage(st->reg); |
| 186 | if (*val < 0) |
| 187 | return *val; |
| 188 | *val /= 1000; |
| 189 | } else { |
| 190 | *val = st->chip_info->int_vref_mv; |
| 191 | } |
| 192 | |
Lars-Peter Clausen | 98efb70 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 193 | *val2 = chan->scan_type.realbits; |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 194 | |
| 195 | return IIO_VAL_FRACTIONAL_LOG2; |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 196 | } |
| 197 | return -EINVAL; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 200 | |
| 201 | static const struct ad7887_chip_info ad7887_chip_info_tbl[] = { |
| 202 | /* |
| 203 | * More devices added in future |
| 204 | */ |
| 205 | [ID_AD7887] = { |
Jonathan Cameron | 31bf47d | 2011-09-30 10:05:44 +0100 | [diff] [blame] | 206 | .channel[0] = { |
| 207 | .type = IIO_VOLTAGE, |
| 208 | .indexed = 1, |
| 209 | .channel = 1, |
Jonathan Cameron | 16d186b | 2013-02-27 19:05:59 +0000 | [diff] [blame] | 210 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 211 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 31bf47d | 2011-09-30 10:05:44 +0100 | [diff] [blame] | 212 | .address = 1, |
| 213 | .scan_index = 1, |
| 214 | .scan_type = IIO_ST('u', 12, 16, 0), |
| 215 | }, |
| 216 | .channel[1] = { |
| 217 | .type = IIO_VOLTAGE, |
| 218 | .indexed = 1, |
| 219 | .channel = 0, |
Jonathan Cameron | 16d186b | 2013-02-27 19:05:59 +0000 | [diff] [blame] | 220 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 221 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Jonathan Cameron | 31bf47d | 2011-09-30 10:05:44 +0100 | [diff] [blame] | 222 | .address = 0, |
| 223 | .scan_index = 0, |
| 224 | .scan_type = IIO_ST('u', 12, 16, 0), |
| 225 | }, |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 226 | .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2), |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 227 | .int_vref_mv = 2500, |
| 228 | }, |
| 229 | }; |
| 230 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 231 | static const struct iio_info ad7887_info = { |
| 232 | .read_raw = &ad7887_read_raw, |
| 233 | .driver_module = THIS_MODULE, |
| 234 | }; |
| 235 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 236 | static int ad7887_probe(struct spi_device *spi) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 237 | { |
| 238 | struct ad7887_platform_data *pdata = spi->dev.platform_data; |
| 239 | struct ad7887_state *st; |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 240 | struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); |
Lars-Peter Clausen | fce7c3e | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 241 | uint8_t mode; |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 242 | int ret; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 243 | |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 244 | if (indio_dev == NULL) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | st = iio_priv(indio_dev); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 248 | |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 249 | if (!pdata || !pdata->use_onchip_ref) { |
| 250 | st->reg = regulator_get(&spi->dev, "vref"); |
| 251 | if (IS_ERR(st->reg)) { |
| 252 | ret = PTR_ERR(st->reg); |
| 253 | goto error_free; |
| 254 | } |
| 255 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 256 | ret = regulator_enable(st->reg); |
| 257 | if (ret) |
| 258 | goto error_put_reg; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | st->chip_info = |
| 262 | &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data]; |
| 263 | |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 264 | spi_set_drvdata(spi, indio_dev); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 265 | st->spi = spi; |
| 266 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 267 | /* Estabilish that the iio_dev is a child of the spi device */ |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 268 | indio_dev->dev.parent = &spi->dev; |
| 269 | indio_dev->name = spi_get_device_id(spi)->name; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 270 | indio_dev->info = &ad7887_info; |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 271 | indio_dev->modes = INDIO_DIRECT_MODE; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 272 | |
| 273 | /* Setup default message */ |
| 274 | |
Lars-Peter Clausen | fce7c3e | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 275 | mode = AD7887_PM_MODE4; |
| 276 | if (!pdata || !pdata->use_onchip_ref) |
| 277 | mode |= AD7887_REF_DIS; |
| 278 | if (pdata && pdata->en_dual) |
| 279 | mode |= AD7887_DUAL; |
| 280 | |
| 281 | st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 282 | |
| 283 | st->xfer[0].rx_buf = &st->data[0]; |
| 284 | st->xfer[0].tx_buf = &st->tx_cmd_buf[0]; |
| 285 | st->xfer[0].len = 2; |
| 286 | |
| 287 | spi_message_init(&st->msg[AD7887_CH0]); |
| 288 | spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]); |
| 289 | |
| 290 | if (pdata && pdata->en_dual) { |
Lars-Peter Clausen | fce7c3e | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 291 | st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 292 | |
| 293 | st->xfer[1].rx_buf = &st->data[0]; |
| 294 | st->xfer[1].tx_buf = &st->tx_cmd_buf[2]; |
| 295 | st->xfer[1].len = 2; |
| 296 | |
| 297 | st->xfer[2].rx_buf = &st->data[2]; |
Lars-Peter Clausen | fce7c3e | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 298 | st->xfer[2].tx_buf = &st->tx_cmd_buf[0]; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 299 | st->xfer[2].len = 2; |
| 300 | |
| 301 | spi_message_init(&st->msg[AD7887_CH0_CH1]); |
| 302 | spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]); |
| 303 | spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]); |
| 304 | |
Lars-Peter Clausen | fce7c3e | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 305 | st->xfer[3].rx_buf = &st->data[2]; |
| 306 | st->xfer[3].tx_buf = &st->tx_cmd_buf[2]; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 307 | st->xfer[3].len = 2; |
| 308 | |
| 309 | spi_message_init(&st->msg[AD7887_CH1]); |
| 310 | spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]); |
| 311 | |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 312 | indio_dev->channels = st->chip_info->channel; |
| 313 | indio_dev->num_channels = 3; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 314 | } else { |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 315 | indio_dev->channels = &st->chip_info->channel[1]; |
| 316 | indio_dev->num_channels = 2; |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 317 | } |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 318 | |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 319 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 320 | &ad7887_trigger_handler, &ad7887_ring_setup_ops); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 321 | if (ret) |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 322 | goto error_disable_reg; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 323 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 324 | ret = iio_device_register(indio_dev); |
| 325 | if (ret) |
| 326 | goto error_unregister_ring; |
| 327 | |
| 328 | return 0; |
| 329 | error_unregister_ring: |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 330 | iio_triggered_buffer_cleanup(indio_dev); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 331 | error_disable_reg: |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 332 | if (st->reg) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 333 | regulator_disable(st->reg); |
| 334 | error_put_reg: |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 335 | if (st->reg) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 336 | regulator_put(st->reg); |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 337 | error_free: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 338 | iio_device_free(indio_dev); |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 339 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 340 | return ret; |
| 341 | } |
| 342 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 343 | static int ad7887_remove(struct spi_device *spi) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 344 | { |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 345 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 346 | struct ad7887_state *st = iio_priv(indio_dev); |
| 347 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 348 | iio_device_unregister(indio_dev); |
Lars-Peter Clausen | 65dd3d3 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 349 | iio_triggered_buffer_cleanup(indio_dev); |
Lars-Peter Clausen | bf5d261 | 2012-11-05 09:56:00 +0000 | [diff] [blame] | 350 | if (st->reg) { |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 351 | regulator_disable(st->reg); |
| 352 | regulator_put(st->reg); |
| 353 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 354 | iio_device_free(indio_dev); |
Michael Hennerich | f39e086 | 2011-05-18 14:41:51 +0100 | [diff] [blame] | 355 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static const struct spi_device_id ad7887_id[] = { |
| 360 | {"ad7887", ID_AD7887}, |
| 361 | {} |
| 362 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 363 | MODULE_DEVICE_TABLE(spi, ad7887_id); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 364 | |
| 365 | static struct spi_driver ad7887_driver = { |
| 366 | .driver = { |
| 367 | .name = "ad7887", |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 368 | .owner = THIS_MODULE, |
| 369 | }, |
| 370 | .probe = ad7887_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 371 | .remove = ad7887_remove, |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 372 | .id_table = ad7887_id, |
| 373 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 374 | module_spi_driver(ad7887_driver); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 375 | |
| 376 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 377 | MODULE_DESCRIPTION("Analog Devices AD7887 ADC"); |
| 378 | MODULE_LICENSE("GPL v2"); |