1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support code for Analog Devices Sigma-Delta ADCs
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/adc/ad_sigma_delta.h>
25 #include <asm/unaligned.h>
28 #define AD_SD_COMM_CHAN_MASK 0x3
30 #define AD_SD_REG_COMM 0x00
31 #define AD_SD_REG_DATA 0x03
34 * ad_sd_set_comm() - Set communications register
36 * @sigma_delta: The sigma delta device
37 * @comm: New value for the communications register
39 void ad_sd_set_comm(struct ad_sigma_delta
*sigma_delta
, uint8_t comm
)
41 /* Some variants use the lower two bits of the communications register
42 * to select the channel */
43 sigma_delta
->comm
= comm
& AD_SD_COMM_CHAN_MASK
;
45 EXPORT_SYMBOL_GPL(ad_sd_set_comm
);
48 * ad_sd_write_reg() - Write a register
50 * @sigma_delta: The sigma delta device
51 * @reg: Address of the register
52 * @size: Size of the register (0-3)
53 * @val: Value to write to the register
55 * Returns 0 on success, an error code otherwise.
57 int ad_sd_write_reg(struct ad_sigma_delta
*sigma_delta
, unsigned int reg
,
58 unsigned int size
, unsigned int val
)
60 uint8_t *data
= sigma_delta
->tx_buf
;
61 struct spi_transfer t
= {
64 .cs_change
= sigma_delta
->keep_cs_asserted
,
69 data
[0] = (reg
<< sigma_delta
->info
->addr_shift
) | sigma_delta
->comm
;
73 put_unaligned_be24(val
, &data
[1]);
76 put_unaligned_be16(val
, &data
[1]);
88 spi_message_add_tail(&t
, &m
);
90 if (sigma_delta
->bus_locked
)
91 ret
= spi_sync_locked(sigma_delta
->spi
, &m
);
93 ret
= spi_sync(sigma_delta
->spi
, &m
);
97 EXPORT_SYMBOL_GPL(ad_sd_write_reg
);
99 static int ad_sd_read_reg_raw(struct ad_sigma_delta
*sigma_delta
,
100 unsigned int reg
, unsigned int size
, uint8_t *val
)
102 uint8_t *data
= sigma_delta
->tx_buf
;
104 struct spi_transfer t
[] = {
111 .cs_change
= sigma_delta
->bus_locked
,
114 struct spi_message m
;
116 spi_message_init(&m
);
118 if (sigma_delta
->info
->has_registers
) {
119 data
[0] = reg
<< sigma_delta
->info
->addr_shift
;
120 data
[0] |= sigma_delta
->info
->read_mask
;
121 data
[0] |= sigma_delta
->comm
;
122 spi_message_add_tail(&t
[0], &m
);
124 spi_message_add_tail(&t
[1], &m
);
126 if (sigma_delta
->bus_locked
)
127 ret
= spi_sync_locked(sigma_delta
->spi
, &m
);
129 ret
= spi_sync(sigma_delta
->spi
, &m
);
135 * ad_sd_read_reg() - Read a register
137 * @sigma_delta: The sigma delta device
138 * @reg: Address of the register
139 * @size: Size of the register (1-4)
142 * Returns 0 on success, an error code otherwise.
144 int ad_sd_read_reg(struct ad_sigma_delta
*sigma_delta
,
145 unsigned int reg
, unsigned int size
, unsigned int *val
)
149 ret
= ad_sd_read_reg_raw(sigma_delta
, reg
, size
, sigma_delta
->rx_buf
);
155 *val
= get_unaligned_be32(sigma_delta
->rx_buf
);
158 *val
= get_unaligned_be24(sigma_delta
->rx_buf
);
161 *val
= get_unaligned_be16(sigma_delta
->rx_buf
);
164 *val
= sigma_delta
->rx_buf
[0];
174 EXPORT_SYMBOL_GPL(ad_sd_read_reg
);
177 * ad_sd_reset() - Reset the serial interface
179 * @sigma_delta: The sigma delta device
180 * @reset_length: Number of SCLKs with DIN = 1
182 * Returns 0 on success, an error code otherwise.
184 int ad_sd_reset(struct ad_sigma_delta
*sigma_delta
,
185 unsigned int reset_length
)
191 size
= DIV_ROUND_UP(reset_length
, 8);
192 buf
= kcalloc(size
, sizeof(*buf
), GFP_KERNEL
);
196 memset(buf
, 0xff, size
);
197 ret
= spi_write(sigma_delta
->spi
, buf
, size
);
202 EXPORT_SYMBOL_GPL(ad_sd_reset
);
204 int ad_sd_calibrate(struct ad_sigma_delta
*sigma_delta
,
205 unsigned int mode
, unsigned int channel
)
208 unsigned long timeout
;
210 ret
= ad_sigma_delta_set_channel(sigma_delta
, channel
);
214 spi_bus_lock(sigma_delta
->spi
->master
);
215 sigma_delta
->bus_locked
= true;
216 sigma_delta
->keep_cs_asserted
= true;
217 reinit_completion(&sigma_delta
->completion
);
219 ret
= ad_sigma_delta_set_mode(sigma_delta
, mode
);
223 sigma_delta
->irq_dis
= false;
224 enable_irq(sigma_delta
->spi
->irq
);
225 timeout
= wait_for_completion_timeout(&sigma_delta
->completion
, 2 * HZ
);
227 sigma_delta
->irq_dis
= true;
228 disable_irq_nosync(sigma_delta
->spi
->irq
);
234 sigma_delta
->keep_cs_asserted
= false;
235 ad_sigma_delta_set_mode(sigma_delta
, AD_SD_MODE_IDLE
);
236 sigma_delta
->bus_locked
= false;
237 spi_bus_unlock(sigma_delta
->spi
->master
);
241 EXPORT_SYMBOL_GPL(ad_sd_calibrate
);
244 * ad_sd_calibrate_all() - Performs channel calibration
245 * @sigma_delta: The sigma delta device
246 * @cb: Array of channels and calibration type to perform
247 * @n: Number of items in cb
249 * Returns 0 on success, an error code otherwise.
251 int ad_sd_calibrate_all(struct ad_sigma_delta
*sigma_delta
,
252 const struct ad_sd_calib_data
*cb
, unsigned int n
)
257 for (i
= 0; i
< n
; i
++) {
258 ret
= ad_sd_calibrate(sigma_delta
, cb
[i
].mode
, cb
[i
].channel
);
265 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all
);
268 * ad_sigma_delta_single_conversion() - Performs a single data conversion
269 * @indio_dev: The IIO device
270 * @chan: The conversion is done for this channel
271 * @val: Pointer to the location where to store the read value
273 * Returns: 0 on success, an error value otherwise.
275 int ad_sigma_delta_single_conversion(struct iio_dev
*indio_dev
,
276 const struct iio_chan_spec
*chan
, int *val
)
278 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
279 unsigned int sample
, raw_sample
;
280 unsigned int data_reg
;
283 if (iio_buffer_enabled(indio_dev
))
286 mutex_lock(&indio_dev
->mlock
);
287 ad_sigma_delta_set_channel(sigma_delta
, chan
->address
);
289 spi_bus_lock(sigma_delta
->spi
->master
);
290 sigma_delta
->bus_locked
= true;
291 sigma_delta
->keep_cs_asserted
= true;
292 reinit_completion(&sigma_delta
->completion
);
294 ad_sigma_delta_set_mode(sigma_delta
, AD_SD_MODE_SINGLE
);
296 sigma_delta
->irq_dis
= false;
297 enable_irq(sigma_delta
->spi
->irq
);
298 ret
= wait_for_completion_interruptible_timeout(
299 &sigma_delta
->completion
, HZ
);
306 if (sigma_delta
->info
->data_reg
!= 0)
307 data_reg
= sigma_delta
->info
->data_reg
;
309 data_reg
= AD_SD_REG_DATA
;
311 ret
= ad_sd_read_reg(sigma_delta
, data_reg
,
312 DIV_ROUND_UP(chan
->scan_type
.realbits
+ chan
->scan_type
.shift
, 8),
316 if (!sigma_delta
->irq_dis
) {
317 disable_irq_nosync(sigma_delta
->spi
->irq
);
318 sigma_delta
->irq_dis
= true;
321 sigma_delta
->keep_cs_asserted
= false;
322 ad_sigma_delta_set_mode(sigma_delta
, AD_SD_MODE_IDLE
);
323 sigma_delta
->bus_locked
= false;
324 spi_bus_unlock(sigma_delta
->spi
->master
);
325 mutex_unlock(&indio_dev
->mlock
);
330 sample
= raw_sample
>> chan
->scan_type
.shift
;
331 sample
&= (1 << chan
->scan_type
.realbits
) - 1;
334 ret
= ad_sigma_delta_postprocess_sample(sigma_delta
, raw_sample
);
340 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion
);
342 static int ad_sd_buffer_postenable(struct iio_dev
*indio_dev
)
344 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
345 unsigned int channel
;
348 channel
= find_first_bit(indio_dev
->active_scan_mask
,
349 indio_dev
->masklength
);
350 ret
= ad_sigma_delta_set_channel(sigma_delta
,
351 indio_dev
->channels
[channel
].address
);
355 spi_bus_lock(sigma_delta
->spi
->master
);
356 sigma_delta
->bus_locked
= true;
357 sigma_delta
->keep_cs_asserted
= true;
359 ret
= ad_sigma_delta_set_mode(sigma_delta
, AD_SD_MODE_CONTINUOUS
);
363 sigma_delta
->irq_dis
= false;
364 enable_irq(sigma_delta
->spi
->irq
);
369 spi_bus_unlock(sigma_delta
->spi
->master
);
374 static int ad_sd_buffer_postdisable(struct iio_dev
*indio_dev
)
376 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
378 reinit_completion(&sigma_delta
->completion
);
379 wait_for_completion_timeout(&sigma_delta
->completion
, HZ
);
381 if (!sigma_delta
->irq_dis
) {
382 disable_irq_nosync(sigma_delta
->spi
->irq
);
383 sigma_delta
->irq_dis
= true;
386 sigma_delta
->keep_cs_asserted
= false;
387 ad_sigma_delta_set_mode(sigma_delta
, AD_SD_MODE_IDLE
);
389 sigma_delta
->bus_locked
= false;
390 return spi_bus_unlock(sigma_delta
->spi
->master
);
393 static irqreturn_t
ad_sd_trigger_handler(int irq
, void *p
)
395 struct iio_poll_func
*pf
= p
;
396 struct iio_dev
*indio_dev
= pf
->indio_dev
;
397 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
398 uint8_t *data
= sigma_delta
->rx_buf
;
399 unsigned int reg_size
;
400 unsigned int data_reg
;
402 reg_size
= indio_dev
->channels
[0].scan_type
.realbits
+
403 indio_dev
->channels
[0].scan_type
.shift
;
404 reg_size
= DIV_ROUND_UP(reg_size
, 8);
406 if (sigma_delta
->info
->data_reg
!= 0)
407 data_reg
= sigma_delta
->info
->data_reg
;
409 data_reg
= AD_SD_REG_DATA
;
415 ad_sd_read_reg_raw(sigma_delta
, data_reg
, reg_size
, &data
[0]);
418 /* We store 24 bit samples in a 32 bit word. Keep the upper
419 * byte set to zero. */
420 ad_sd_read_reg_raw(sigma_delta
, data_reg
, reg_size
, &data
[1]);
424 iio_push_to_buffers_with_timestamp(indio_dev
, data
, pf
->timestamp
);
426 iio_trigger_notify_done(indio_dev
->trig
);
427 sigma_delta
->irq_dis
= false;
428 enable_irq(sigma_delta
->spi
->irq
);
433 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops
= {
434 .postenable
= &ad_sd_buffer_postenable
,
435 .postdisable
= &ad_sd_buffer_postdisable
,
436 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
439 static irqreturn_t
ad_sd_data_rdy_trig_poll(int irq
, void *private)
441 struct ad_sigma_delta
*sigma_delta
= private;
443 complete(&sigma_delta
->completion
);
444 disable_irq_nosync(irq
);
445 sigma_delta
->irq_dis
= true;
446 iio_trigger_poll(sigma_delta
->trig
);
452 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
453 * @indio_dev: The IIO device
454 * @trig: The new trigger
456 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
457 * device, -EINVAL otherwise.
459 int ad_sd_validate_trigger(struct iio_dev
*indio_dev
, struct iio_trigger
*trig
)
461 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
463 if (sigma_delta
->trig
!= trig
)
468 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger
);
470 static const struct iio_trigger_ops ad_sd_trigger_ops
= {
473 static int ad_sd_probe_trigger(struct iio_dev
*indio_dev
)
475 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
478 sigma_delta
->trig
= iio_trigger_alloc("%s-dev%d", indio_dev
->name
,
480 if (sigma_delta
->trig
== NULL
) {
484 sigma_delta
->trig
->ops
= &ad_sd_trigger_ops
;
485 init_completion(&sigma_delta
->completion
);
487 ret
= request_irq(sigma_delta
->spi
->irq
,
488 ad_sd_data_rdy_trig_poll
,
489 sigma_delta
->info
->irq_flags
,
493 goto error_free_trig
;
495 if (!sigma_delta
->irq_dis
) {
496 sigma_delta
->irq_dis
= true;
497 disable_irq_nosync(sigma_delta
->spi
->irq
);
499 sigma_delta
->trig
->dev
.parent
= &sigma_delta
->spi
->dev
;
500 iio_trigger_set_drvdata(sigma_delta
->trig
, sigma_delta
);
502 ret
= iio_trigger_register(sigma_delta
->trig
);
506 /* select default trigger */
507 indio_dev
->trig
= iio_trigger_get(sigma_delta
->trig
);
512 free_irq(sigma_delta
->spi
->irq
, sigma_delta
);
514 iio_trigger_free(sigma_delta
->trig
);
519 static void ad_sd_remove_trigger(struct iio_dev
*indio_dev
)
521 struct ad_sigma_delta
*sigma_delta
= iio_device_get_drvdata(indio_dev
);
523 iio_trigger_unregister(sigma_delta
->trig
);
524 free_irq(sigma_delta
->spi
->irq
, sigma_delta
);
525 iio_trigger_free(sigma_delta
->trig
);
529 * ad_sd_setup_buffer_and_trigger() -
530 * @indio_dev: The IIO device
532 int ad_sd_setup_buffer_and_trigger(struct iio_dev
*indio_dev
)
536 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
537 &ad_sd_trigger_handler
, &ad_sd_buffer_setup_ops
);
541 ret
= ad_sd_probe_trigger(indio_dev
);
543 iio_triggered_buffer_cleanup(indio_dev
);
549 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger
);
552 * ad_sd_cleanup_buffer_and_trigger() -
553 * @indio_dev: The IIO device
555 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev
*indio_dev
)
557 ad_sd_remove_trigger(indio_dev
);
558 iio_triggered_buffer_cleanup(indio_dev
);
560 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger
);
563 * ad_sd_init() - Initializes a ad_sigma_delta struct
564 * @sigma_delta: The ad_sigma_delta device
565 * @indio_dev: The IIO device which the Sigma Delta device is used for
566 * @spi: The SPI device for the ad_sigma_delta device
567 * @info: Device specific callbacks and options
569 * This function needs to be called before any other operations are performed on
570 * the ad_sigma_delta struct.
572 int ad_sd_init(struct ad_sigma_delta
*sigma_delta
, struct iio_dev
*indio_dev
,
573 struct spi_device
*spi
, const struct ad_sigma_delta_info
*info
)
575 sigma_delta
->spi
= spi
;
576 sigma_delta
->info
= info
;
577 iio_device_set_drvdata(indio_dev
, sigma_delta
);
581 EXPORT_SYMBOL_GPL(ad_sd_init
);
583 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
584 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
585 MODULE_LICENSE("GPL v2");