2 * AD7887 SPI ADC driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
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/module.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
25 static int ad7887_scan_direct(struct ad7887_state
*st
, unsigned ch
)
27 int ret
= spi_sync(st
->spi
, &st
->msg
[ch
]);
31 return (st
->data
[(ch
* 2)] << 8) | st
->data
[(ch
* 2) + 1];
34 static int ad7887_read_raw(struct iio_dev
*indio_dev
,
35 struct iio_chan_spec
const *chan
,
41 struct ad7887_state
*st
= iio_priv(indio_dev
);
42 unsigned int scale_uv
;
45 case IIO_CHAN_INFO_RAW
:
46 mutex_lock(&indio_dev
->mlock
);
47 if (iio_buffer_enabled(indio_dev
))
50 ret
= ad7887_scan_direct(st
, chan
->address
);
51 mutex_unlock(&indio_dev
->mlock
);
55 *val
= (ret
>> st
->chip_info
->channel
[0].scan_type
.shift
) &
56 RES_MASK(st
->chip_info
->channel
[0].scan_type
.realbits
);
58 case IIO_CHAN_INFO_SCALE
:
59 scale_uv
= (st
->int_vref_mv
* 1000)
60 >> st
->chip_info
->channel
[0].scan_type
.realbits
;
62 *val2
= (scale_uv
%1000)*1000;
63 return IIO_VAL_INT_PLUS_MICRO
;
69 static const struct ad7887_chip_info ad7887_chip_info_tbl
[] = {
71 * More devices added in future
78 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
79 IIO_CHAN_INFO_SCALE_SHARED_BIT
,
82 .scan_type
= IIO_ST('u', 12, 16, 0),
88 .info_mask
= IIO_CHAN_INFO_RAW_SEPARATE_BIT
|
89 IIO_CHAN_INFO_SCALE_SHARED_BIT
,
92 .scan_type
= IIO_ST('u', 12, 16, 0),
94 .channel
[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
99 static const struct iio_info ad7887_info
= {
100 .read_raw
= &ad7887_read_raw
,
101 .driver_module
= THIS_MODULE
,
104 static int __devinit
ad7887_probe(struct spi_device
*spi
)
106 struct ad7887_platform_data
*pdata
= spi
->dev
.platform_data
;
107 struct ad7887_state
*st
;
108 int ret
, voltage_uv
= 0;
109 struct iio_dev
*indio_dev
= iio_device_alloc(sizeof(*st
));
111 if (indio_dev
== NULL
)
114 st
= iio_priv(indio_dev
);
116 st
->reg
= regulator_get(&spi
->dev
, "vcc");
117 if (!IS_ERR(st
->reg
)) {
118 ret
= regulator_enable(st
->reg
);
122 voltage_uv
= regulator_get_voltage(st
->reg
);
126 &ad7887_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
128 spi_set_drvdata(spi
, indio_dev
);
131 /* Estabilish that the iio_dev is a child of the spi device */
132 indio_dev
->dev
.parent
= &spi
->dev
;
133 indio_dev
->name
= spi_get_device_id(spi
)->name
;
134 indio_dev
->info
= &ad7887_info
;
135 indio_dev
->modes
= INDIO_DIRECT_MODE
;
137 /* Setup default message */
139 st
->tx_cmd_buf
[0] = AD7887_CH_AIN0
| AD7887_PM_MODE4
|
140 ((pdata
&& pdata
->use_onchip_ref
) ?
143 st
->xfer
[0].rx_buf
= &st
->data
[0];
144 st
->xfer
[0].tx_buf
= &st
->tx_cmd_buf
[0];
147 spi_message_init(&st
->msg
[AD7887_CH0
]);
148 spi_message_add_tail(&st
->xfer
[0], &st
->msg
[AD7887_CH0
]);
150 if (pdata
&& pdata
->en_dual
) {
151 st
->tx_cmd_buf
[0] |= AD7887_DUAL
| AD7887_REF_DIS
;
153 st
->tx_cmd_buf
[2] = AD7887_CH_AIN1
| AD7887_DUAL
|
154 AD7887_REF_DIS
| AD7887_PM_MODE4
;
155 st
->tx_cmd_buf
[4] = AD7887_CH_AIN0
| AD7887_DUAL
|
156 AD7887_REF_DIS
| AD7887_PM_MODE4
;
157 st
->tx_cmd_buf
[6] = AD7887_CH_AIN1
| AD7887_DUAL
|
158 AD7887_REF_DIS
| AD7887_PM_MODE4
;
160 st
->xfer
[1].rx_buf
= &st
->data
[0];
161 st
->xfer
[1].tx_buf
= &st
->tx_cmd_buf
[2];
164 st
->xfer
[2].rx_buf
= &st
->data
[2];
165 st
->xfer
[2].tx_buf
= &st
->tx_cmd_buf
[4];
168 spi_message_init(&st
->msg
[AD7887_CH0_CH1
]);
169 spi_message_add_tail(&st
->xfer
[1], &st
->msg
[AD7887_CH0_CH1
]);
170 spi_message_add_tail(&st
->xfer
[2], &st
->msg
[AD7887_CH0_CH1
]);
172 st
->xfer
[3].rx_buf
= &st
->data
[0];
173 st
->xfer
[3].tx_buf
= &st
->tx_cmd_buf
[6];
176 spi_message_init(&st
->msg
[AD7887_CH1
]);
177 spi_message_add_tail(&st
->xfer
[3], &st
->msg
[AD7887_CH1
]);
179 if (pdata
&& pdata
->vref_mv
)
180 st
->int_vref_mv
= pdata
->vref_mv
;
182 st
->int_vref_mv
= voltage_uv
/ 1000;
184 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
186 indio_dev
->channels
= st
->chip_info
->channel
;
187 indio_dev
->num_channels
= 3;
189 if (pdata
&& pdata
->vref_mv
)
190 st
->int_vref_mv
= pdata
->vref_mv
;
191 else if (pdata
&& pdata
->use_onchip_ref
)
192 st
->int_vref_mv
= st
->chip_info
->int_vref_mv
;
194 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
196 indio_dev
->channels
= &st
->chip_info
->channel
[1];
197 indio_dev
->num_channels
= 2;
200 ret
= ad7887_register_ring_funcs_and_init(indio_dev
);
202 goto error_disable_reg
;
204 ret
= iio_device_register(indio_dev
);
206 goto error_unregister_ring
;
209 error_unregister_ring
:
210 ad7887_ring_cleanup(indio_dev
);
212 if (!IS_ERR(st
->reg
))
213 regulator_disable(st
->reg
);
215 if (!IS_ERR(st
->reg
))
216 regulator_put(st
->reg
);
217 iio_device_free(indio_dev
);
222 static int ad7887_remove(struct spi_device
*spi
)
224 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
225 struct ad7887_state
*st
= iio_priv(indio_dev
);
227 iio_device_unregister(indio_dev
);
228 ad7887_ring_cleanup(indio_dev
);
229 if (!IS_ERR(st
->reg
)) {
230 regulator_disable(st
->reg
);
231 regulator_put(st
->reg
);
233 iio_device_free(indio_dev
);
238 static const struct spi_device_id ad7887_id
[] = {
239 {"ad7887", ID_AD7887
},
242 MODULE_DEVICE_TABLE(spi
, ad7887_id
);
244 static struct spi_driver ad7887_driver
= {
247 .owner
= THIS_MODULE
,
249 .probe
= ad7887_probe
,
250 .remove
= __devexit_p(ad7887_remove
),
251 .id_table
= ad7887_id
,
253 module_spi_driver(ad7887_driver
);
255 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
256 MODULE_DESCRIPTION("Analog Devices AD7887 ADC");
257 MODULE_LICENSE("GPL v2");