Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AD7298 SPI ADC driver |
| 3 | * |
| 4 | * Copyright 2011 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2. |
| 7 | */ |
| 8 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/sysfs.h> |
| 13 | #include <linux/spi/spi.h> |
| 14 | #include <linux/regulator/consumer.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/delay.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +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> |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 22 | |
| 23 | #include "ad7298.h" |
| 24 | |
Jonathan Cameron | 95e48f7 | 2012-04-13 10:42:54 +0100 | [diff] [blame] | 25 | #define AD7298_V_CHAN(index) \ |
| 26 | { \ |
| 27 | .type = IIO_VOLTAGE, \ |
| 28 | .indexed = 1, \ |
| 29 | .channel = index, \ |
Jonathan Cameron | b11f98f | 2012-04-15 17:41:18 +0100 | [diff] [blame] | 30 | .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ |
| 31 | IIO_CHAN_INFO_SCALE_SHARED_BIT, \ |
Jonathan Cameron | 95e48f7 | 2012-04-13 10:42:54 +0100 | [diff] [blame] | 32 | .address = index, \ |
| 33 | .scan_index = index, \ |
| 34 | .scan_type = { \ |
| 35 | .sign = 'u', \ |
| 36 | .realbits = 12, \ |
| 37 | .storagebits = 16, \ |
| 38 | }, \ |
| 39 | } |
| 40 | |
Lars-Peter Clausen | f4e4b95 | 2012-08-09 08:51:00 +0100 | [diff] [blame^] | 41 | static const struct iio_chan_spec ad7298_channels[] = { |
Jonathan Cameron | 95e48f7 | 2012-04-13 10:42:54 +0100 | [diff] [blame] | 42 | { |
| 43 | .type = IIO_TEMP, |
| 44 | .indexed = 1, |
| 45 | .channel = 0, |
Jonathan Cameron | b11f98f | 2012-04-15 17:41:18 +0100 | [diff] [blame] | 46 | .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | |
| 47 | IIO_CHAN_INFO_SCALE_SEPARATE_BIT, |
Lars-Peter Clausen | d045b9d | 2012-06-18 18:33:51 +0200 | [diff] [blame] | 48 | .address = AD7298_CH_TEMP, |
| 49 | .scan_index = -1, |
Jonathan Cameron | 95e48f7 | 2012-04-13 10:42:54 +0100 | [diff] [blame] | 50 | .scan_type = { |
| 51 | .sign = 's', |
| 52 | .realbits = 32, |
| 53 | .storagebits = 32, |
| 54 | }, |
| 55 | }, |
| 56 | AD7298_V_CHAN(0), |
| 57 | AD7298_V_CHAN(1), |
| 58 | AD7298_V_CHAN(2), |
| 59 | AD7298_V_CHAN(3), |
| 60 | AD7298_V_CHAN(4), |
| 61 | AD7298_V_CHAN(5), |
| 62 | AD7298_V_CHAN(6), |
| 63 | AD7298_V_CHAN(7), |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 64 | IIO_CHAN_SOFT_TIMESTAMP(8), |
| 65 | }; |
| 66 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 67 | static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch) |
| 68 | { |
| 69 | int ret; |
| 70 | st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref | |
| 71 | (AD7298_CH(0) >> ch)); |
| 72 | |
| 73 | ret = spi_sync(st->spi, &st->scan_single_msg); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
| 77 | return be16_to_cpu(st->rx_buf[0]); |
| 78 | } |
| 79 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 80 | static int ad7298_scan_temp(struct ad7298_state *st, int *val) |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 81 | { |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 82 | int tmp, ret; |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 83 | __be16 buf; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 84 | |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 85 | buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 86 | AD7298_TAVG | st->ext_ref); |
| 87 | |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 88 | ret = spi_write(st->spi, (u8 *)&buf, 2); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 89 | if (ret) |
| 90 | return ret; |
| 91 | |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 92 | buf = cpu_to_be16(0); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 93 | |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 94 | ret = spi_write(st->spi, (u8 *)&buf, 2); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 95 | if (ret) |
| 96 | return ret; |
| 97 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 98 | usleep_range(101, 1000); /* sleep > 100us */ |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 99 | |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 100 | ret = spi_read(st->spi, (u8 *)&buf, 2); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 101 | if (ret) |
| 102 | return ret; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 103 | |
Dan Carpenter | c1a7528 | 2011-11-22 10:39:15 +0300 | [diff] [blame] | 104 | tmp = be16_to_cpu(buf) & RES_MASK(AD7298_BITS); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * One LSB of the ADC corresponds to 0.25 deg C. |
| 108 | * The temperature reading is in 12-bit twos complement format |
| 109 | */ |
| 110 | |
| 111 | if (tmp & (1 << (AD7298_BITS - 1))) { |
| 112 | tmp = (4096 - tmp) * 250; |
| 113 | tmp -= (2 * tmp); |
| 114 | |
| 115 | } else { |
| 116 | tmp *= 250; /* temperature in milli degrees Celsius */ |
| 117 | } |
| 118 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 119 | *val = tmp; |
| 120 | |
| 121 | return 0; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 124 | static int ad7298_read_raw(struct iio_dev *indio_dev, |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 125 | struct iio_chan_spec const *chan, |
| 126 | int *val, |
| 127 | int *val2, |
| 128 | long m) |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 129 | { |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 130 | int ret; |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 131 | struct ad7298_state *st = iio_priv(indio_dev); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 132 | unsigned int scale_uv; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 133 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 134 | switch (m) { |
Jonathan Cameron | b11f98f | 2012-04-15 17:41:18 +0100 | [diff] [blame] | 135 | case IIO_CHAN_INFO_RAW: |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 136 | mutex_lock(&indio_dev->mlock); |
Jonathan Cameron | 389ac48 | 2011-12-05 22:18:19 +0000 | [diff] [blame] | 137 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { |
| 138 | ret = -EBUSY; |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 139 | } else { |
| 140 | if (chan->address == AD7298_CH_TEMP) |
| 141 | ret = ad7298_scan_temp(st, val); |
| 142 | else |
| 143 | ret = ad7298_scan_direct(st, chan->address); |
| 144 | } |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 145 | mutex_unlock(&indio_dev->mlock); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 146 | |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
| 150 | if (chan->address != AD7298_CH_TEMP) |
| 151 | *val = ret & RES_MASK(AD7298_BITS); |
| 152 | |
| 153 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 154 | case IIO_CHAN_INFO_SCALE: |
| 155 | switch (chan->type) { |
| 156 | case IIO_VOLTAGE: |
| 157 | scale_uv = (st->int_vref_mv * 1000) >> AD7298_BITS; |
| 158 | *val = scale_uv / 1000; |
| 159 | *val2 = (scale_uv % 1000) * 1000; |
| 160 | return IIO_VAL_INT_PLUS_MICRO; |
| 161 | case IIO_TEMP: |
| 162 | *val = 1; |
| 163 | *val2 = 0; |
| 164 | return IIO_VAL_INT_PLUS_MICRO; |
| 165 | default: |
| 166 | return -EINVAL; |
| 167 | } |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 168 | } |
| 169 | return -EINVAL; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 170 | } |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 171 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 172 | static const struct iio_info ad7298_info = { |
| 173 | .read_raw = &ad7298_read_raw, |
Lars-Peter Clausen | d045b9d | 2012-06-18 18:33:51 +0200 | [diff] [blame] | 174 | .update_scan_mode = ad7298_update_scan_mode, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 175 | .driver_module = THIS_MODULE, |
| 176 | }; |
| 177 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 178 | static int __devinit ad7298_probe(struct spi_device *spi) |
| 179 | { |
| 180 | struct ad7298_platform_data *pdata = spi->dev.platform_data; |
| 181 | struct ad7298_state *st; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 182 | int ret; |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 183 | struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 184 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 185 | if (indio_dev == NULL) |
| 186 | return -ENOMEM; |
| 187 | |
| 188 | st = iio_priv(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 189 | |
| 190 | st->reg = regulator_get(&spi->dev, "vcc"); |
| 191 | if (!IS_ERR(st->reg)) { |
| 192 | ret = regulator_enable(st->reg); |
| 193 | if (ret) |
| 194 | goto error_put_reg; |
| 195 | } |
| 196 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 197 | spi_set_drvdata(spi, indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 198 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 199 | st->spi = spi; |
| 200 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 201 | indio_dev->name = spi_get_device_id(spi)->name; |
| 202 | indio_dev->dev.parent = &spi->dev; |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 203 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 204 | indio_dev->channels = ad7298_channels; |
| 205 | indio_dev->num_channels = ARRAY_SIZE(ad7298_channels); |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 206 | indio_dev->info = &ad7298_info; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 207 | |
| 208 | /* Setup default message */ |
| 209 | |
| 210 | st->scan_single_xfer[0].tx_buf = &st->tx_buf[0]; |
| 211 | st->scan_single_xfer[0].len = 2; |
| 212 | st->scan_single_xfer[0].cs_change = 1; |
| 213 | st->scan_single_xfer[1].tx_buf = &st->tx_buf[1]; |
| 214 | st->scan_single_xfer[1].len = 2; |
| 215 | st->scan_single_xfer[1].cs_change = 1; |
| 216 | st->scan_single_xfer[2].rx_buf = &st->rx_buf[0]; |
| 217 | st->scan_single_xfer[2].len = 2; |
| 218 | |
| 219 | spi_message_init(&st->scan_single_msg); |
| 220 | spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg); |
| 221 | spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg); |
| 222 | spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg); |
| 223 | |
| 224 | if (pdata && pdata->vref_mv) { |
| 225 | st->int_vref_mv = pdata->vref_mv; |
| 226 | st->ext_ref = AD7298_EXTREF; |
| 227 | } else { |
| 228 | st->int_vref_mv = AD7298_INTREF_mV; |
| 229 | } |
| 230 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 231 | ret = ad7298_register_ring_funcs_and_init(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 232 | if (ret) |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 233 | goto error_disable_reg; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 234 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 235 | ret = iio_device_register(indio_dev); |
| 236 | if (ret) |
Lars-Peter Clausen | d045b9d | 2012-06-18 18:33:51 +0200 | [diff] [blame] | 237 | goto error_cleanup_ring; |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 238 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 239 | return 0; |
| 240 | |
| 241 | error_cleanup_ring: |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 242 | ad7298_ring_cleanup(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 243 | error_disable_reg: |
| 244 | if (!IS_ERR(st->reg)) |
| 245 | regulator_disable(st->reg); |
| 246 | error_put_reg: |
| 247 | if (!IS_ERR(st->reg)) |
| 248 | regulator_put(st->reg); |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 249 | iio_device_free(indio_dev); |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 250 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static int __devexit ad7298_remove(struct spi_device *spi) |
| 255 | { |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 256 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 257 | struct ad7298_state *st = iio_priv(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 258 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 259 | iio_device_unregister(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 260 | ad7298_ring_cleanup(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 261 | if (!IS_ERR(st->reg)) { |
| 262 | regulator_disable(st->reg); |
| 263 | regulator_put(st->reg); |
| 264 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 265 | iio_device_free(indio_dev); |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame] | 266 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static const struct spi_device_id ad7298_id[] = { |
| 271 | {"ad7298", 0}, |
| 272 | {} |
| 273 | }; |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 274 | MODULE_DEVICE_TABLE(spi, ad7298_id); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 275 | |
| 276 | static struct spi_driver ad7298_driver = { |
| 277 | .driver = { |
| 278 | .name = "ad7298", |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 279 | .owner = THIS_MODULE, |
| 280 | }, |
| 281 | .probe = ad7298_probe, |
| 282 | .remove = __devexit_p(ad7298_remove), |
| 283 | .id_table = ad7298_id, |
| 284 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 285 | module_spi_driver(ad7298_driver); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 286 | |
| 287 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 288 | MODULE_DESCRIPTION("Analog Devices AD7298 ADC"); |
| 289 | MODULE_LICENSE("GPL v2"); |