1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD7298 SPI ADC driver
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/sysfs.h>
12 #include <linux/spi/spi.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/bitops.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #include <linux/platform_data/ad7298.h>
28 #define AD7298_WRITE BIT(15) /* write to the control register */
29 #define AD7298_REPEAT BIT(14) /* repeated conversion enable */
30 #define AD7298_CH(x) BIT(13 - (x)) /* channel select */
31 #define AD7298_TSENSE BIT(5) /* temperature conversion enable */
32 #define AD7298_EXTREF BIT(2) /* external reference enable */
33 #define AD7298_TAVG BIT(1) /* temperature sensor averaging enable */
34 #define AD7298_PDD BIT(0) /* partial power down enable */
36 #define AD7298_MAX_CHAN 8
37 #define AD7298_INTREF_mV 2500
39 #define AD7298_CH_TEMP 9
42 struct spi_device
*spi
;
43 struct regulator
*reg
;
45 struct spi_transfer ring_xfer
[10];
46 struct spi_transfer scan_single_xfer
[3];
47 struct spi_message ring_msg
;
48 struct spi_message scan_single_msg
;
50 * DMA (thus cache coherency maintenance) requires the
51 * transfer buffers to live in their own cache lines.
53 __be16 rx_buf
[12] ____cacheline_aligned
;
57 #define AD7298_V_CHAN(index) \
59 .type = IIO_VOLTAGE, \
62 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
63 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
65 .scan_index = index, \
70 .endianness = IIO_BE, \
74 static const struct iio_chan_spec ad7298_channels
[] = {
79 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
80 BIT(IIO_CHAN_INFO_SCALE
) |
81 BIT(IIO_CHAN_INFO_OFFSET
),
82 .address
= AD7298_CH_TEMP
,
98 IIO_CHAN_SOFT_TIMESTAMP(8),
102 * ad7298_update_scan_mode() setup the spi transfer buffer for the new scan mask
104 static int ad7298_update_scan_mode(struct iio_dev
*indio_dev
,
105 const unsigned long *active_scan_mask
)
107 struct ad7298_state
*st
= iio_priv(indio_dev
);
109 unsigned short command
;
112 /* Now compute overall size */
113 scan_count
= bitmap_weight(active_scan_mask
, indio_dev
->masklength
);
115 command
= AD7298_WRITE
| st
->ext_ref
;
117 for (i
= 0, m
= AD7298_CH(0); i
< AD7298_MAX_CHAN
; i
++, m
>>= 1)
118 if (test_bit(i
, active_scan_mask
))
121 st
->tx_buf
[0] = cpu_to_be16(command
);
123 /* build spi ring message */
124 st
->ring_xfer
[0].tx_buf
= &st
->tx_buf
[0];
125 st
->ring_xfer
[0].len
= 2;
126 st
->ring_xfer
[0].cs_change
= 1;
127 st
->ring_xfer
[1].tx_buf
= &st
->tx_buf
[1];
128 st
->ring_xfer
[1].len
= 2;
129 st
->ring_xfer
[1].cs_change
= 1;
131 spi_message_init(&st
->ring_msg
);
132 spi_message_add_tail(&st
->ring_xfer
[0], &st
->ring_msg
);
133 spi_message_add_tail(&st
->ring_xfer
[1], &st
->ring_msg
);
135 for (i
= 0; i
< scan_count
; i
++) {
136 st
->ring_xfer
[i
+ 2].rx_buf
= &st
->rx_buf
[i
];
137 st
->ring_xfer
[i
+ 2].len
= 2;
138 st
->ring_xfer
[i
+ 2].cs_change
= 1;
139 spi_message_add_tail(&st
->ring_xfer
[i
+ 2], &st
->ring_msg
);
141 /* make sure last transfer cs_change is not set */
142 st
->ring_xfer
[i
+ 1].cs_change
= 0;
148 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
150 * Currently there is no option in this driver to disable the saving of
151 * timestamps within the ring.
153 static irqreturn_t
ad7298_trigger_handler(int irq
, void *p
)
155 struct iio_poll_func
*pf
= p
;
156 struct iio_dev
*indio_dev
= pf
->indio_dev
;
157 struct ad7298_state
*st
= iio_priv(indio_dev
);
160 b_sent
= spi_sync(st
->spi
, &st
->ring_msg
);
164 iio_push_to_buffers_with_timestamp(indio_dev
, st
->rx_buf
,
165 iio_get_time_ns(indio_dev
));
168 iio_trigger_notify_done(indio_dev
->trig
);
173 static int ad7298_scan_direct(struct ad7298_state
*st
, unsigned ch
)
176 st
->tx_buf
[0] = cpu_to_be16(AD7298_WRITE
| st
->ext_ref
|
177 (AD7298_CH(0) >> ch
));
179 ret
= spi_sync(st
->spi
, &st
->scan_single_msg
);
183 return be16_to_cpu(st
->rx_buf
[0]);
186 static int ad7298_scan_temp(struct ad7298_state
*st
, int *val
)
191 buf
= cpu_to_be16(AD7298_WRITE
| AD7298_TSENSE
|
192 AD7298_TAVG
| st
->ext_ref
);
194 ret
= spi_write(st
->spi
, (u8
*)&buf
, 2);
198 buf
= cpu_to_be16(0);
200 ret
= spi_write(st
->spi
, (u8
*)&buf
, 2);
204 usleep_range(101, 1000); /* sleep > 100us */
206 ret
= spi_read(st
->spi
, (u8
*)&buf
, 2);
210 *val
= sign_extend32(be16_to_cpu(buf
), 11);
215 static int ad7298_get_ref_voltage(struct ad7298_state
*st
)
220 vref
= regulator_get_voltage(st
->reg
);
226 return AD7298_INTREF_mV
;
230 static int ad7298_read_raw(struct iio_dev
*indio_dev
,
231 struct iio_chan_spec
const *chan
,
237 struct ad7298_state
*st
= iio_priv(indio_dev
);
240 case IIO_CHAN_INFO_RAW
:
241 ret
= iio_device_claim_direct_mode(indio_dev
);
245 if (chan
->address
== AD7298_CH_TEMP
)
246 ret
= ad7298_scan_temp(st
, val
);
248 ret
= ad7298_scan_direct(st
, chan
->address
);
250 iio_device_release_direct_mode(indio_dev
);
255 if (chan
->address
!= AD7298_CH_TEMP
)
256 *val
= ret
& GENMASK(chan
->scan_type
.realbits
- 1, 0);
259 case IIO_CHAN_INFO_SCALE
:
260 switch (chan
->type
) {
262 *val
= ad7298_get_ref_voltage(st
);
263 *val2
= chan
->scan_type
.realbits
;
264 return IIO_VAL_FRACTIONAL_LOG2
;
266 *val
= ad7298_get_ref_voltage(st
);
268 return IIO_VAL_FRACTIONAL
;
272 case IIO_CHAN_INFO_OFFSET
:
273 *val
= 1093 - 2732500 / ad7298_get_ref_voltage(st
);
279 static const struct iio_info ad7298_info
= {
280 .read_raw
= &ad7298_read_raw
,
281 .update_scan_mode
= ad7298_update_scan_mode
,
284 static int ad7298_probe(struct spi_device
*spi
)
286 struct ad7298_platform_data
*pdata
= spi
->dev
.platform_data
;
287 struct ad7298_state
*st
;
288 struct iio_dev
*indio_dev
;
291 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
292 if (indio_dev
== NULL
)
295 st
= iio_priv(indio_dev
);
297 if (pdata
&& pdata
->ext_ref
)
298 st
->ext_ref
= AD7298_EXTREF
;
301 st
->reg
= devm_regulator_get(&spi
->dev
, "vref");
303 return PTR_ERR(st
->reg
);
305 ret
= regulator_enable(st
->reg
);
310 spi_set_drvdata(spi
, indio_dev
);
314 indio_dev
->name
= spi_get_device_id(spi
)->name
;
315 indio_dev
->dev
.parent
= &spi
->dev
;
316 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
317 indio_dev
->modes
= INDIO_DIRECT_MODE
;
318 indio_dev
->channels
= ad7298_channels
;
319 indio_dev
->num_channels
= ARRAY_SIZE(ad7298_channels
);
320 indio_dev
->info
= &ad7298_info
;
322 /* Setup default message */
324 st
->scan_single_xfer
[0].tx_buf
= &st
->tx_buf
[0];
325 st
->scan_single_xfer
[0].len
= 2;
326 st
->scan_single_xfer
[0].cs_change
= 1;
327 st
->scan_single_xfer
[1].tx_buf
= &st
->tx_buf
[1];
328 st
->scan_single_xfer
[1].len
= 2;
329 st
->scan_single_xfer
[1].cs_change
= 1;
330 st
->scan_single_xfer
[2].rx_buf
= &st
->rx_buf
[0];
331 st
->scan_single_xfer
[2].len
= 2;
333 spi_message_init(&st
->scan_single_msg
);
334 spi_message_add_tail(&st
->scan_single_xfer
[0], &st
->scan_single_msg
);
335 spi_message_add_tail(&st
->scan_single_xfer
[1], &st
->scan_single_msg
);
336 spi_message_add_tail(&st
->scan_single_xfer
[2], &st
->scan_single_msg
);
338 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
339 &ad7298_trigger_handler
, NULL
);
341 goto error_disable_reg
;
343 ret
= iio_device_register(indio_dev
);
345 goto error_cleanup_ring
;
350 iio_triggered_buffer_cleanup(indio_dev
);
353 regulator_disable(st
->reg
);
358 static int ad7298_remove(struct spi_device
*spi
)
360 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
361 struct ad7298_state
*st
= iio_priv(indio_dev
);
363 iio_device_unregister(indio_dev
);
364 iio_triggered_buffer_cleanup(indio_dev
);
366 regulator_disable(st
->reg
);
371 static const struct spi_device_id ad7298_id
[] = {
375 MODULE_DEVICE_TABLE(spi
, ad7298_id
);
377 static struct spi_driver ad7298_driver
= {
381 .probe
= ad7298_probe
,
382 .remove
= ad7298_remove
,
383 .id_table
= ad7298_id
,
385 module_spi_driver(ad7298_driver
);
387 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
388 MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
389 MODULE_LICENSE("GPL v2");