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