2 * AD7466/7/8 AD7476/5/7/8 (A) SPI ADC driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
22 #include "../ring_generic.h"
27 static int ad7476_scan_direct(struct ad7476_state
*st
)
31 ret
= spi_sync(st
->spi
, &st
->msg
);
35 return (st
->data
[0] << 8) | st
->data
[1];
38 static ssize_t
ad7476_scan(struct device
*dev
,
39 struct device_attribute
*attr
,
42 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
43 struct ad7476_state
*st
= dev_info
->dev_data
;
46 mutex_lock(&dev_info
->mlock
);
47 if (iio_ring_enabled(dev_info
))
48 ret
= ad7476_scan_from_ring(st
);
50 ret
= ad7476_scan_direct(st
);
51 mutex_unlock(&dev_info
->mlock
);
56 return sprintf(buf
, "%d\n", (ret
>> st
->chip_info
->res_shift
) &
57 RES_MASK(st
->chip_info
->bits
));
59 static IIO_DEV_ATTR_IN_RAW(0, ad7476_scan
, 0);
61 static ssize_t
ad7476_show_scale(struct device
*dev
,
62 struct device_attribute
*attr
,
65 /* Driver currently only support internal vref */
66 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
67 struct ad7476_state
*st
= iio_dev_get_devdata(dev_info
);
68 /* Corresponds to Vref / 2^(bits) */
69 unsigned int scale_uv
= (st
->int_vref_mv
* 1000) >> st
->chip_info
->bits
;
71 return sprintf(buf
, "%d.%d\n", scale_uv
/ 1000, scale_uv
% 1000);
73 static IIO_DEVICE_ATTR(in_scale
, S_IRUGO
, ad7476_show_scale
, NULL
, 0);
75 static ssize_t
ad7476_show_name(struct device
*dev
,
76 struct device_attribute
*attr
,
79 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
80 struct ad7476_state
*st
= iio_dev_get_devdata(dev_info
);
82 return sprintf(buf
, "%s\n", spi_get_device_id(st
->spi
)->name
);
84 static IIO_DEVICE_ATTR(name
, S_IRUGO
, ad7476_show_name
, NULL
, 0);
86 static struct attribute
*ad7476_attributes
[] = {
87 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
88 &iio_dev_attr_in_scale
.dev_attr
.attr
,
89 &iio_dev_attr_name
.dev_attr
.attr
,
93 static const struct attribute_group ad7476_attribute_group
= {
94 .attrs
= ad7476_attributes
,
97 static const struct ad7476_chip_info ad7476_chip_info_tbl
[] = {
102 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
108 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
114 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
120 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
126 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
132 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
138 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
145 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
149 static int __devinit
ad7476_probe(struct spi_device
*spi
)
151 struct ad7476_platform_data
*pdata
= spi
->dev
.platform_data
;
152 struct ad7476_state
*st
;
153 int ret
, voltage_uv
= 0;
155 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
161 st
->reg
= regulator_get(&spi
->dev
, "vcc");
162 if (!IS_ERR(st
->reg
)) {
163 ret
= regulator_enable(st
->reg
);
167 voltage_uv
= regulator_get_voltage(st
->reg
);
171 &ad7476_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
173 if (st
->chip_info
->int_vref_mv
)
174 st
->int_vref_mv
= st
->chip_info
->int_vref_mv
;
175 else if (pdata
&& pdata
->vref_mv
)
176 st
->int_vref_mv
= pdata
->vref_mv
;
178 st
->int_vref_mv
= voltage_uv
/ 1000;
180 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
182 spi_set_drvdata(spi
, st
);
184 atomic_set(&st
->protect_ring
, 0);
187 st
->indio_dev
= iio_allocate_device();
188 if (st
->indio_dev
== NULL
) {
190 goto error_disable_reg
;
193 /* Estabilish that the iio_dev is a child of the i2c device */
194 st
->indio_dev
->dev
.parent
= &spi
->dev
;
195 st
->indio_dev
->attrs
= &ad7476_attribute_group
;
196 st
->indio_dev
->dev_data
= (void *)(st
);
197 st
->indio_dev
->driver_module
= THIS_MODULE
;
198 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
200 /* Setup default message */
202 st
->xfer
.rx_buf
= &st
->data
;
203 st
->xfer
.len
= st
->chip_info
->storagebits
/ 8;
205 spi_message_init(&st
->msg
);
206 spi_message_add_tail(&st
->xfer
, &st
->msg
);
208 ret
= ad7476_register_ring_funcs_and_init(st
->indio_dev
);
210 goto error_free_device
;
212 ret
= iio_device_register(st
->indio_dev
);
214 goto error_free_device
;
216 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
218 goto error_cleanup_ring
;
222 ad7476_ring_cleanup(st
->indio_dev
);
223 iio_device_unregister(st
->indio_dev
);
225 iio_free_device(st
->indio_dev
);
227 if (!IS_ERR(st
->reg
))
228 regulator_disable(st
->reg
);
230 if (!IS_ERR(st
->reg
))
231 regulator_put(st
->reg
);
237 static int ad7476_remove(struct spi_device
*spi
)
239 struct ad7476_state
*st
= spi_get_drvdata(spi
);
240 struct iio_dev
*indio_dev
= st
->indio_dev
;
241 iio_ring_buffer_unregister(indio_dev
->ring
);
242 ad7476_ring_cleanup(indio_dev
);
243 iio_device_unregister(indio_dev
);
244 if (!IS_ERR(st
->reg
)) {
245 regulator_disable(st
->reg
);
246 regulator_put(st
->reg
);
252 static const struct spi_device_id ad7476_id
[] = {
253 {"ad7466", ID_AD7466
},
254 {"ad7467", ID_AD7467
},
255 {"ad7468", ID_AD7468
},
256 {"ad7475", ID_AD7475
},
257 {"ad7476", ID_AD7476
},
258 {"ad7476a", ID_AD7476
},
259 {"ad7477", ID_AD7477
},
260 {"ad7477a", ID_AD7477
},
261 {"ad7478", ID_AD7478
},
262 {"ad7478a", ID_AD7478
},
263 {"ad7495", ID_AD7495
},
267 static struct spi_driver ad7476_driver
= {
270 .bus
= &spi_bus_type
,
271 .owner
= THIS_MODULE
,
273 .probe
= ad7476_probe
,
274 .remove
= __devexit_p(ad7476_remove
),
275 .id_table
= ad7476_id
,
278 static int __init
ad7476_init(void)
280 return spi_register_driver(&ad7476_driver
);
282 module_init(ad7476_init
);
284 static void __exit
ad7476_exit(void)
286 spi_unregister_driver(&ad7476_driver
);
288 module_exit(ad7476_exit
);
290 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
291 MODULE_DESCRIPTION("Analog Devices AD7475/6/7/8(A) AD7466/7/8 ADC");
292 MODULE_LICENSE("GPL v2");
293 MODULE_ALIAS("spi:ad7476");