9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
16 #include <linux/module.h>
52 return spi_read(st->
spi, st->data, 2);
58 return spi_read(st->
spi, st->data, 1);
61 static int ad7266_preenable(
struct iio_dev *indio_dev)
66 ret = ad7266_wakeup(st);
77 static int ad7266_postdisable(
struct iio_dev *indio_dev)
80 return ad7266_powerdown(st);
84 .preenable = &ad7266_preenable,
87 .postdisable = &ad7266_postdisable,
90 static irqreturn_t ad7266_trigger_handler(
int irq,
void *
p)
98 ret = spi_read(st->
spi, st->data, 4);
110 static void ad7266_select_input(
struct ad7266_state *st,
unsigned int nr)
129 for (i = 0; i < 3; ++
i)
133 static int ad7266_update_scan_mode(
struct iio_dev *indio_dev,
134 const unsigned long *scan_mask)
139 ad7266_select_input(st, nr);
149 ad7266_select_input(st, address);
157 static int ad7266_read_raw(
struct iio_dev *indio_dev,
161 unsigned long scale_uv;
166 if (iio_buffer_enabled(indio_dev))
169 ret = ad7266_read_single(st, val, chan->
address);
173 *val = (*val >> 2) & 0xfff;
175 *val = sign_extend32(*val, 11);
179 scale_uv = (st->
vref_uv * 100);
186 *val = scale_uv / 100000;
187 *val2 = (scale_uv % 100000) * 10;
200 #define AD7266_CHAN(_chan, _sign) { \
201 .type = IIO_VOLTAGE, \
203 .channel = (_chan), \
204 .address = (_chan), \
205 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT \
206 | IIO_CHAN_INFO_SCALE_SHARED_BIT \
207 | IIO_CHAN_INFO_OFFSET_SHARED_BIT, \
208 .scan_index = (_chan), \
214 .endianness = IIO_BE, \
218 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS(_name, _sign) \
219 const struct iio_chan_spec ad7266_channels_##_name[] = { \
220 AD7266_CHAN(0, (_sign)), \
221 AD7266_CHAN(1, (_sign)), \
222 AD7266_CHAN(2, (_sign)), \
223 AD7266_CHAN(3, (_sign)), \
224 AD7266_CHAN(4, (_sign)), \
225 AD7266_CHAN(5, (_sign)), \
226 AD7266_CHAN(6, (_sign)), \
227 AD7266_CHAN(7, (_sign)), \
228 AD7266_CHAN(8, (_sign)), \
229 AD7266_CHAN(9, (_sign)), \
230 AD7266_CHAN(10, (_sign)), \
231 AD7266_CHAN(11, (_sign)), \
232 IIO_CHAN_SOFT_TIMESTAMP(13), \
235 #define AD7266_DECLARE_SINGLE_ENDED_CHANNELS_FIXED(_name, _sign) \
236 const struct iio_chan_spec ad7266_channels_##_name##_fixed[] = { \
237 AD7266_CHAN(0, (_sign)), \
238 AD7266_CHAN(1, (_sign)), \
239 IIO_CHAN_SOFT_TIMESTAMP(2), \
247 #define AD7266_CHAN_DIFF(_chan, _sign) { \
248 .type = IIO_VOLTAGE, \
250 .channel = (_chan) * 2, \
251 .channel2 = (_chan) * 2 + 1, \
252 .address = (_chan), \
253 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT \
254 | IIO_CHAN_INFO_SCALE_SHARED_BIT \
255 | IIO_CHAN_INFO_OFFSET_SHARED_BIT, \
256 .scan_index = (_chan), \
262 .endianness = IIO_BE, \
267 #define AD7266_DECLARE_DIFF_CHANNELS(_name, _sign) \
268 const struct iio_chan_spec ad7266_channels_diff_##_name[] = { \
269 AD7266_CHAN_DIFF(0, (_sign)), \
270 AD7266_CHAN_DIFF(1, (_sign)), \
271 AD7266_CHAN_DIFF(2, (_sign)), \
272 AD7266_CHAN_DIFF(3, (_sign)), \
273 AD7266_CHAN_DIFF(4, (_sign)), \
274 AD7266_CHAN_DIFF(5, (_sign)), \
275 IIO_CHAN_SOFT_TIMESTAMP(6), \
281 #define AD7266_DECLARE_DIFF_CHANNELS_FIXED(_name, _sign) \
282 const struct iio_chan_spec ad7266_channels_diff_fixed_##_name[] = { \
283 AD7266_CHAN_DIFF(0, (_sign)), \
284 AD7266_CHAN_DIFF(1, (_sign)), \
285 IIO_CHAN_SOFT_TIMESTAMP(2), \
291 static const struct iio_info ad7266_info = {
292 .read_raw = &ad7266_read_raw,
293 .update_scan_mode = &ad7266_update_scan_mode,
297 static unsigned long ad7266_available_scan_masks[] = {
307 static unsigned long ad7266_available_scan_masks_diff[] = {
314 static unsigned long ad7266_available_scan_masks_fixed[] = {
325 #define AD7266_CHAN_INFO_INDEX(_differential, _signed, _fixed) \
326 (((_differential) << 2) | ((_signed) << 1) | ((_fixed) << 0))
330 .channels = ad7266_channels_u,
331 .num_channels =
ARRAY_SIZE(ad7266_channels_u),
332 .scan_masks = ad7266_available_scan_masks,
335 .channels = ad7266_channels_u_fixed,
336 .num_channels =
ARRAY_SIZE(ad7266_channels_u_fixed),
337 .scan_masks = ad7266_available_scan_masks_fixed,
340 .channels = ad7266_channels_s,
341 .num_channels =
ARRAY_SIZE(ad7266_channels_s),
342 .scan_masks = ad7266_available_scan_masks,
345 .channels = ad7266_channels_s_fixed,
346 .num_channels =
ARRAY_SIZE(ad7266_channels_s_fixed),
347 .scan_masks = ad7266_available_scan_masks_fixed,
350 .channels = ad7266_channels_diff_u,
351 .num_channels =
ARRAY_SIZE(ad7266_channels_diff_u),
352 .scan_masks = ad7266_available_scan_masks_diff,
355 .channels = ad7266_channels_diff_fixed_u,
356 .num_channels =
ARRAY_SIZE(ad7266_channels_diff_fixed_u),
357 .scan_masks = ad7266_available_scan_masks_fixed,
360 .channels = ad7266_channels_diff_s,
361 .num_channels =
ARRAY_SIZE(ad7266_channels_diff_s),
362 .scan_masks = ad7266_available_scan_masks_diff,
365 .channels = ad7266_channels_diff_fixed_s,
366 .num_channels =
ARRAY_SIZE(ad7266_channels_diff_fixed_s),
367 .scan_masks = ad7266_available_scan_masks_fixed,
383 chan_info = &ad7266_chan_infos[
i];
391 static const char *
const ad7266_gpio_labels[] = {
404 if (indio_dev ==
NULL)
407 st = iio_priv(indio_dev);
410 if (!IS_ERR_OR_NULL(st->
reg)) {
430 st->
gpios[
i].label = ad7266_gpio_labels[
i];
435 goto error_disable_reg;
443 spi_set_drvdata(spi, indio_dev);
446 indio_dev->
dev.parent = &spi->
dev;
449 indio_dev->
info = &ad7266_info;
451 ad7266_init_channels(indio_dev);
471 &ad7266_trigger_handler, &iio_triggered_buffer_setup_ops);
473 goto error_free_gpios;
477 goto error_buffer_cleanup;
481 error_buffer_cleanup:
487 if (!IS_ERR_OR_NULL(st->
reg))
490 if (!IS_ERR_OR_NULL(st->
reg))
500 struct iio_dev *indio_dev = spi_get_drvdata(spi);
507 if (!IS_ERR_OR_NULL(st->
reg)) {
528 .probe = ad7266_probe,
530 .id_table = ad7266_id,