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> |
| 17 | |
| 18 | #include "../iio.h" |
| 19 | #include "../sysfs.h" |
| 20 | #include "../ring_generic.h" |
| 21 | #include "adc.h" |
| 22 | |
| 23 | #include "ad7298.h" |
| 24 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 25 | static struct iio_chan_spec ad7298_channels[] = { |
| 26 | IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0, |
| 27 | (1 << IIO_CHAN_INFO_SCALE_SEPARATE), |
| 28 | 9, AD7298_CH_TEMP, IIO_ST('s', 32, 32, 0), 0), |
| 29 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0, |
| 30 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 31 | 0, 0, IIO_ST('u', 12, 16, 0), 0), |
| 32 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0, |
| 33 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 34 | 1, 1, IIO_ST('u', 12, 16, 0), 0), |
| 35 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0, |
| 36 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 37 | 2, 2, IIO_ST('u', 12, 16, 0), 0), |
| 38 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0, |
| 39 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 40 | 3, 3, IIO_ST('u', 12, 16, 0), 0), |
| 41 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0, |
| 42 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 43 | 4, 4, IIO_ST('u', 12, 16, 0), 0), |
| 44 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0, |
| 45 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 46 | 5, 5, IIO_ST('u', 12, 16, 0), 0), |
| 47 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0, |
| 48 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 49 | 6, 6, IIO_ST('u', 12, 16, 0), 0), |
| 50 | IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0, |
| 51 | (1 << IIO_CHAN_INFO_SCALE_SHARED), |
| 52 | 7, 7, IIO_ST('u', 12, 16, 0), 0), |
| 53 | IIO_CHAN_SOFT_TIMESTAMP(8), |
| 54 | }; |
| 55 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 56 | static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch) |
| 57 | { |
| 58 | int ret; |
| 59 | st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref | |
| 60 | (AD7298_CH(0) >> ch)); |
| 61 | |
| 62 | ret = spi_sync(st->spi, &st->scan_single_msg); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
| 66 | return be16_to_cpu(st->rx_buf[0]); |
| 67 | } |
| 68 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 69 | static int ad7298_scan_temp(struct ad7298_state *st, int *val) |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 70 | { |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 71 | int tmp, ret; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 72 | |
| 73 | tmp = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE | |
| 74 | AD7298_TAVG | st->ext_ref); |
| 75 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 76 | ret = spi_write(st->spi, (u8 *)&tmp, 2); |
| 77 | if (ret) |
| 78 | return ret; |
| 79 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 80 | tmp = 0; |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 81 | |
| 82 | ret = spi_write(st->spi, (u8 *)&tmp, 2); |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 86 | usleep_range(101, 1000); /* sleep > 100us */ |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 87 | |
| 88 | ret = spi_read(st->spi, (u8 *)&tmp, 2); |
| 89 | if (ret) |
| 90 | return ret; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 91 | |
| 92 | tmp = be16_to_cpu(tmp) & RES_MASK(AD7298_BITS); |
| 93 | |
| 94 | /* |
| 95 | * One LSB of the ADC corresponds to 0.25 deg C. |
| 96 | * The temperature reading is in 12-bit twos complement format |
| 97 | */ |
| 98 | |
| 99 | if (tmp & (1 << (AD7298_BITS - 1))) { |
| 100 | tmp = (4096 - tmp) * 250; |
| 101 | tmp -= (2 * tmp); |
| 102 | |
| 103 | } else { |
| 104 | tmp *= 250; /* temperature in milli degrees Celsius */ |
| 105 | } |
| 106 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 107 | *val = tmp; |
| 108 | |
| 109 | return 0; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 112 | static int ad7298_read_raw(struct iio_dev *dev_info, |
| 113 | struct iio_chan_spec const *chan, |
| 114 | int *val, |
| 115 | int *val2, |
| 116 | long m) |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 117 | { |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 118 | int ret; |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 119 | struct ad7298_state *st = iio_priv(dev_info); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 120 | unsigned int scale_uv; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 121 | |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 122 | switch (m) { |
| 123 | case 0: |
| 124 | mutex_lock(&dev_info->mlock); |
| 125 | if (iio_ring_enabled(dev_info)) { |
| 126 | if (chan->address == AD7298_CH_TEMP) |
| 127 | ret = -ENODEV; |
| 128 | else |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 129 | ret = ad7298_scan_from_ring(dev_info, |
| 130 | chan->address); |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 131 | } else { |
| 132 | if (chan->address == AD7298_CH_TEMP) |
| 133 | ret = ad7298_scan_temp(st, val); |
| 134 | else |
| 135 | ret = ad7298_scan_direct(st, chan->address); |
| 136 | } |
| 137 | mutex_unlock(&dev_info->mlock); |
| 138 | |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | if (chan->address != AD7298_CH_TEMP) |
| 143 | *val = ret & RES_MASK(AD7298_BITS); |
| 144 | |
| 145 | return IIO_VAL_INT; |
| 146 | case (1 << IIO_CHAN_INFO_SCALE_SHARED): |
| 147 | scale_uv = (st->int_vref_mv * 1000) >> AD7298_BITS; |
| 148 | *val = scale_uv / 1000; |
| 149 | *val2 = (scale_uv % 1000) * 1000; |
| 150 | return IIO_VAL_INT_PLUS_MICRO; |
| 151 | case (1 << IIO_CHAN_INFO_SCALE_SEPARATE): |
| 152 | *val = 1; |
| 153 | *val2 = 0; |
| 154 | return IIO_VAL_INT_PLUS_MICRO; |
| 155 | } |
| 156 | return -EINVAL; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 157 | } |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 158 | |
| 159 | static int __devinit ad7298_probe(struct spi_device *spi) |
| 160 | { |
| 161 | struct ad7298_platform_data *pdata = spi->dev.platform_data; |
| 162 | struct ad7298_state *st; |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 163 | int ret, regdone = 0; |
| 164 | struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st)); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 165 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 166 | if (indio_dev == NULL) |
| 167 | return -ENOMEM; |
| 168 | |
| 169 | st = iio_priv(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 170 | |
| 171 | st->reg = regulator_get(&spi->dev, "vcc"); |
| 172 | if (!IS_ERR(st->reg)) { |
| 173 | ret = regulator_enable(st->reg); |
| 174 | if (ret) |
| 175 | goto error_put_reg; |
| 176 | } |
| 177 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 178 | spi_set_drvdata(spi, indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 179 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 180 | st->spi = spi; |
| 181 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 182 | indio_dev->name = spi_get_device_id(spi)->name; |
| 183 | indio_dev->dev.parent = &spi->dev; |
| 184 | indio_dev->driver_module = THIS_MODULE; |
| 185 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 186 | indio_dev->channels = ad7298_channels; |
| 187 | indio_dev->num_channels = ARRAY_SIZE(ad7298_channels); |
| 188 | indio_dev->read_raw = &ad7298_read_raw; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 189 | |
| 190 | /* Setup default message */ |
| 191 | |
| 192 | st->scan_single_xfer[0].tx_buf = &st->tx_buf[0]; |
| 193 | st->scan_single_xfer[0].len = 2; |
| 194 | st->scan_single_xfer[0].cs_change = 1; |
| 195 | st->scan_single_xfer[1].tx_buf = &st->tx_buf[1]; |
| 196 | st->scan_single_xfer[1].len = 2; |
| 197 | st->scan_single_xfer[1].cs_change = 1; |
| 198 | st->scan_single_xfer[2].rx_buf = &st->rx_buf[0]; |
| 199 | st->scan_single_xfer[2].len = 2; |
| 200 | |
| 201 | spi_message_init(&st->scan_single_msg); |
| 202 | spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg); |
| 203 | spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg); |
| 204 | spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg); |
| 205 | |
| 206 | if (pdata && pdata->vref_mv) { |
| 207 | st->int_vref_mv = pdata->vref_mv; |
| 208 | st->ext_ref = AD7298_EXTREF; |
| 209 | } else { |
| 210 | st->int_vref_mv = AD7298_INTREF_mV; |
| 211 | } |
| 212 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 213 | ret = ad7298_register_ring_funcs_and_init(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 214 | if (ret) |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 215 | goto error_disable_reg; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 216 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 217 | ret = iio_device_register(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 218 | if (ret) |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 219 | goto error_disable_reg; |
| 220 | regdone = 1; |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 221 | |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 222 | ret = iio_ring_buffer_register_ex(indio_dev->ring, 0, |
Michael Hennerich | 01a99e1 | 2011-05-18 14:41:55 +0100 | [diff] [blame] | 223 | &ad7298_channels[1], /* skip temp0 */ |
| 224 | ARRAY_SIZE(ad7298_channels) - 1); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 225 | if (ret) |
| 226 | goto error_cleanup_ring; |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 227 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 228 | return 0; |
| 229 | |
| 230 | error_cleanup_ring: |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 231 | ad7298_ring_cleanup(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 232 | error_disable_reg: |
| 233 | if (!IS_ERR(st->reg)) |
| 234 | regulator_disable(st->reg); |
| 235 | error_put_reg: |
| 236 | if (!IS_ERR(st->reg)) |
| 237 | regulator_put(st->reg); |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 238 | |
| 239 | if (regdone) |
| 240 | iio_device_unregister(indio_dev); |
| 241 | else |
| 242 | iio_free_device(indio_dev); |
| 243 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static int __devexit ad7298_remove(struct spi_device *spi) |
| 248 | { |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 249 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 250 | struct ad7298_state *st = iio_priv(indio_dev); |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 251 | |
| 252 | iio_ring_buffer_unregister(indio_dev->ring); |
| 253 | ad7298_ring_cleanup(indio_dev); |
| 254 | iio_device_unregister(indio_dev); |
| 255 | if (!IS_ERR(st->reg)) { |
| 256 | regulator_disable(st->reg); |
| 257 | regulator_put(st->reg); |
| 258 | } |
Michael Hennerich | cc4a48e | 2011-05-18 14:42:25 +0100 | [diff] [blame^] | 259 | iio_device_unregister(indio_dev); |
| 260 | |
Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static const struct spi_device_id ad7298_id[] = { |
| 265 | {"ad7298", 0}, |
| 266 | {} |
| 267 | }; |
| 268 | |
| 269 | static struct spi_driver ad7298_driver = { |
| 270 | .driver = { |
| 271 | .name = "ad7298", |
| 272 | .bus = &spi_bus_type, |
| 273 | .owner = THIS_MODULE, |
| 274 | }, |
| 275 | .probe = ad7298_probe, |
| 276 | .remove = __devexit_p(ad7298_remove), |
| 277 | .id_table = ad7298_id, |
| 278 | }; |
| 279 | |
| 280 | static int __init ad7298_init(void) |
| 281 | { |
| 282 | return spi_register_driver(&ad7298_driver); |
| 283 | } |
| 284 | module_init(ad7298_init); |
| 285 | |
| 286 | static void __exit ad7298_exit(void) |
| 287 | { |
| 288 | spi_unregister_driver(&ad7298_driver); |
| 289 | } |
| 290 | module_exit(ad7298_exit); |
| 291 | |
| 292 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 293 | MODULE_DESCRIPTION("Analog Devices AD7298 ADC"); |
| 294 | MODULE_LICENSE("GPL v2"); |
| 295 | MODULE_ALIAS("spi:ad7298"); |