2 * AD7887 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 ad7887_scan_direct(struct ad7887_state
*st
, unsigned ch
)
29 int ret
= spi_sync(st
->spi
, &st
->msg
[ch
]);
33 return (st
->data
[(ch
* 2)] << 8) | st
->data
[(ch
* 2) + 1];
36 static ssize_t
ad7887_scan(struct device
*dev
,
37 struct device_attribute
*attr
,
40 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
41 struct ad7887_state
*st
= dev_info
->dev_data
;
42 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
45 mutex_lock(&dev_info
->mlock
);
46 if (iio_ring_enabled(dev_info
))
47 ret
= ad7887_scan_from_ring(st
, 1 << this_attr
->address
);
49 ret
= ad7887_scan_direct(st
, this_attr
->address
);
50 mutex_unlock(&dev_info
->mlock
);
55 return sprintf(buf
, "%d\n", (ret
>> st
->chip_info
->left_shift
) &
56 RES_MASK(st
->chip_info
->bits
));
58 static IIO_DEV_ATTR_IN_RAW(0, ad7887_scan
, 0);
59 static IIO_DEV_ATTR_IN_RAW(1, ad7887_scan
, 1);
61 static ssize_t
ad7887_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 ad7887_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.%03d\n", scale_uv
/ 1000, scale_uv
% 1000);
73 static IIO_DEVICE_ATTR(in_scale
, S_IRUGO
, ad7887_show_scale
, NULL
, 0);
75 static ssize_t
ad7887_show_name(struct device
*dev
,
76 struct device_attribute
*attr
,
79 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
80 struct ad7887_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
, ad7887_show_name
, NULL
, 0);
86 static struct attribute
*ad7887_attributes
[] = {
87 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
88 &iio_dev_attr_in1_raw
.dev_attr
.attr
,
89 &iio_dev_attr_in_scale
.dev_attr
.attr
,
90 &iio_dev_attr_name
.dev_attr
.attr
,
94 static mode_t
ad7887_attr_is_visible(struct kobject
*kobj
,
95 struct attribute
*attr
, int n
)
97 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
98 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
99 struct ad7887_state
*st
= iio_dev_get_devdata(dev_info
);
101 mode_t mode
= attr
->mode
;
103 if ((attr
== &iio_dev_attr_in1_raw
.dev_attr
.attr
) && !st
->en_dual
)
109 static const struct attribute_group ad7887_attribute_group
= {
110 .attrs
= ad7887_attributes
,
111 .is_visible
= ad7887_attr_is_visible
,
114 static const struct ad7887_chip_info ad7887_chip_info_tbl
[] = {
116 * More devices added in future
122 .sign
= IIO_SCAN_EL_TYPE_UNSIGNED
,
127 static int __devinit
ad7887_probe(struct spi_device
*spi
)
129 struct ad7887_platform_data
*pdata
= spi
->dev
.platform_data
;
130 struct ad7887_state
*st
;
131 int ret
, voltage_uv
= 0;
133 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
139 st
->reg
= regulator_get(&spi
->dev
, "vcc");
140 if (!IS_ERR(st
->reg
)) {
141 ret
= regulator_enable(st
->reg
);
145 voltage_uv
= regulator_get_voltage(st
->reg
);
149 &ad7887_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
151 spi_set_drvdata(spi
, st
);
155 st
->indio_dev
= iio_allocate_device(0);
156 if (st
->indio_dev
== NULL
) {
158 goto error_disable_reg
;
161 /* Estabilish that the iio_dev is a child of the spi device */
162 st
->indio_dev
->dev
.parent
= &spi
->dev
;
163 st
->indio_dev
->attrs
= &ad7887_attribute_group
;
164 st
->indio_dev
->dev_data
= (void *)(st
);
165 st
->indio_dev
->driver_module
= THIS_MODULE
;
166 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
168 /* Setup default message */
170 st
->tx_cmd_buf
[0] = AD7887_CH_AIN0
| AD7887_PM_MODE4
|
171 ((pdata
&& pdata
->use_onchip_ref
) ?
174 st
->xfer
[0].rx_buf
= &st
->data
[0];
175 st
->xfer
[0].tx_buf
= &st
->tx_cmd_buf
[0];
178 spi_message_init(&st
->msg
[AD7887_CH0
]);
179 spi_message_add_tail(&st
->xfer
[0], &st
->msg
[AD7887_CH0
]);
181 if (pdata
&& pdata
->en_dual
) {
182 st
->tx_cmd_buf
[0] |= AD7887_DUAL
| AD7887_REF_DIS
;
184 st
->tx_cmd_buf
[2] = AD7887_CH_AIN1
| AD7887_DUAL
|
185 AD7887_REF_DIS
| AD7887_PM_MODE4
;
186 st
->tx_cmd_buf
[4] = AD7887_CH_AIN0
| AD7887_DUAL
|
187 AD7887_REF_DIS
| AD7887_PM_MODE4
;
188 st
->tx_cmd_buf
[6] = AD7887_CH_AIN1
| AD7887_DUAL
|
189 AD7887_REF_DIS
| AD7887_PM_MODE4
;
191 st
->xfer
[1].rx_buf
= &st
->data
[0];
192 st
->xfer
[1].tx_buf
= &st
->tx_cmd_buf
[2];
195 st
->xfer
[2].rx_buf
= &st
->data
[2];
196 st
->xfer
[2].tx_buf
= &st
->tx_cmd_buf
[4];
199 spi_message_init(&st
->msg
[AD7887_CH0_CH1
]);
200 spi_message_add_tail(&st
->xfer
[1], &st
->msg
[AD7887_CH0_CH1
]);
201 spi_message_add_tail(&st
->xfer
[2], &st
->msg
[AD7887_CH0_CH1
]);
203 st
->xfer
[3].rx_buf
= &st
->data
[0];
204 st
->xfer
[3].tx_buf
= &st
->tx_cmd_buf
[6];
207 spi_message_init(&st
->msg
[AD7887_CH1
]);
208 spi_message_add_tail(&st
->xfer
[3], &st
->msg
[AD7887_CH1
]);
212 if (pdata
&& pdata
->vref_mv
)
213 st
->int_vref_mv
= pdata
->vref_mv
;
215 st
->int_vref_mv
= voltage_uv
/ 1000;
217 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
220 if (pdata
&& pdata
->vref_mv
)
221 st
->int_vref_mv
= pdata
->vref_mv
;
222 else if (pdata
&& pdata
->use_onchip_ref
)
223 st
->int_vref_mv
= st
->chip_info
->int_vref_mv
;
225 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
229 ret
= ad7887_register_ring_funcs_and_init(st
->indio_dev
);
231 goto error_free_device
;
233 ret
= iio_device_register(st
->indio_dev
);
235 goto error_free_device
;
237 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
239 goto error_cleanup_ring
;
243 ad7887_ring_cleanup(st
->indio_dev
);
244 iio_device_unregister(st
->indio_dev
);
246 iio_free_device(st
->indio_dev
);
248 if (!IS_ERR(st
->reg
))
249 regulator_disable(st
->reg
);
251 if (!IS_ERR(st
->reg
))
252 regulator_put(st
->reg
);
258 static int ad7887_remove(struct spi_device
*spi
)
260 struct ad7887_state
*st
= spi_get_drvdata(spi
);
261 struct iio_dev
*indio_dev
= st
->indio_dev
;
262 iio_ring_buffer_unregister(indio_dev
->ring
);
263 ad7887_ring_cleanup(indio_dev
);
264 iio_device_unregister(indio_dev
);
265 if (!IS_ERR(st
->reg
)) {
266 regulator_disable(st
->reg
);
267 regulator_put(st
->reg
);
273 static const struct spi_device_id ad7887_id
[] = {
274 {"ad7887", ID_AD7887
},
278 static struct spi_driver ad7887_driver
= {
281 .bus
= &spi_bus_type
,
282 .owner
= THIS_MODULE
,
284 .probe
= ad7887_probe
,
285 .remove
= __devexit_p(ad7887_remove
),
286 .id_table
= ad7887_id
,
289 static int __init
ad7887_init(void)
291 return spi_register_driver(&ad7887_driver
);
293 module_init(ad7887_init
);
295 static void __exit
ad7887_exit(void)
297 spi_unregister_driver(&ad7887_driver
);
299 module_exit(ad7887_exit
);
301 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
302 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
303 MODULE_LICENSE("GPL v2");
304 MODULE_ALIAS("spi:ad7887");