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.%d\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
);
153 atomic_set(&st
->protect_ring
, 0);
156 st
->indio_dev
= iio_allocate_device();
157 if (st
->indio_dev
== NULL
) {
159 goto error_disable_reg
;
162 /* Estabilish that the iio_dev is a child of the spi device */
163 st
->indio_dev
->dev
.parent
= &spi
->dev
;
164 st
->indio_dev
->attrs
= &ad7887_attribute_group
;
165 st
->indio_dev
->dev_data
= (void *)(st
);
166 st
->indio_dev
->driver_module
= THIS_MODULE
;
167 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
169 /* Setup default message */
171 st
->tx_cmd_buf
[0] = AD7887_CH_AIN0
| AD7887_PM_MODE4
|
172 ((pdata
&& pdata
->use_onchip_ref
) ?
175 st
->xfer
[0].rx_buf
= &st
->data
[0];
176 st
->xfer
[0].tx_buf
= &st
->tx_cmd_buf
[0];
179 spi_message_init(&st
->msg
[AD7887_CH0
]);
180 spi_message_add_tail(&st
->xfer
[0], &st
->msg
[AD7887_CH0
]);
182 if (pdata
&& pdata
->en_dual
) {
183 st
->tx_cmd_buf
[0] |= AD7887_DUAL
| AD7887_REF_DIS
;
185 st
->tx_cmd_buf
[2] = AD7887_CH_AIN1
| AD7887_DUAL
|
186 AD7887_REF_DIS
| AD7887_PM_MODE4
;
187 st
->tx_cmd_buf
[4] = AD7887_CH_AIN0
| AD7887_DUAL
|
188 AD7887_REF_DIS
| AD7887_PM_MODE4
;
189 st
->tx_cmd_buf
[6] = AD7887_CH_AIN1
| AD7887_DUAL
|
190 AD7887_REF_DIS
| AD7887_PM_MODE4
;
192 st
->xfer
[1].rx_buf
= &st
->data
[0];
193 st
->xfer
[1].tx_buf
= &st
->tx_cmd_buf
[2];
196 st
->xfer
[2].rx_buf
= &st
->data
[2];
197 st
->xfer
[2].tx_buf
= &st
->tx_cmd_buf
[4];
200 spi_message_init(&st
->msg
[AD7887_CH0_CH1
]);
201 spi_message_add_tail(&st
->xfer
[1], &st
->msg
[AD7887_CH0_CH1
]);
202 spi_message_add_tail(&st
->xfer
[2], &st
->msg
[AD7887_CH0_CH1
]);
204 st
->xfer
[3].rx_buf
= &st
->data
[0];
205 st
->xfer
[3].tx_buf
= &st
->tx_cmd_buf
[6];
208 spi_message_init(&st
->msg
[AD7887_CH1
]);
209 spi_message_add_tail(&st
->xfer
[3], &st
->msg
[AD7887_CH1
]);
213 if (pdata
&& pdata
->vref_mv
)
214 st
->int_vref_mv
= pdata
->vref_mv
;
216 st
->int_vref_mv
= voltage_uv
/ 1000;
218 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
221 if (pdata
&& pdata
->vref_mv
)
222 st
->int_vref_mv
= pdata
->vref_mv
;
223 else if (pdata
&& pdata
->use_onchip_ref
)
224 st
->int_vref_mv
= st
->chip_info
->int_vref_mv
;
226 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
230 ret
= ad7887_register_ring_funcs_and_init(st
->indio_dev
);
232 goto error_free_device
;
234 ret
= iio_device_register(st
->indio_dev
);
236 goto error_free_device
;
238 ret
= iio_ring_buffer_register(st
->indio_dev
->ring
, 0);
240 goto error_cleanup_ring
;
244 ad7887_ring_cleanup(st
->indio_dev
);
245 iio_device_unregister(st
->indio_dev
);
247 iio_free_device(st
->indio_dev
);
249 if (!IS_ERR(st
->reg
))
250 regulator_disable(st
->reg
);
252 if (!IS_ERR(st
->reg
))
253 regulator_put(st
->reg
);
259 static int ad7887_remove(struct spi_device
*spi
)
261 struct ad7887_state
*st
= spi_get_drvdata(spi
);
262 struct iio_dev
*indio_dev
= st
->indio_dev
;
263 iio_ring_buffer_unregister(indio_dev
->ring
);
264 ad7887_ring_cleanup(indio_dev
);
265 iio_device_unregister(indio_dev
);
266 if (!IS_ERR(st
->reg
)) {
267 regulator_disable(st
->reg
);
268 regulator_put(st
->reg
);
274 static const struct spi_device_id ad7887_id
[] = {
275 {"ad7887", ID_AD7887
},
279 static struct spi_driver ad7887_driver
= {
282 .bus
= &spi_bus_type
,
283 .owner
= THIS_MODULE
,
285 .probe
= ad7887_probe
,
286 .remove
= __devexit_p(ad7887_remove
),
287 .id_table
= ad7887_id
,
290 static int __init
ad7887_init(void)
292 return spi_register_driver(&ad7887_driver
);
294 module_init(ad7887_init
);
296 static void __exit
ad7887_exit(void)
298 spi_unregister_driver(&ad7887_driver
);
300 module_exit(ad7887_exit
);
302 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
303 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
304 MODULE_LICENSE("GPL v2");
305 MODULE_ALIAS("spi:ad7887");