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/align.h>
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.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>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/adc/ad_sigma_delta.h>
26 #include <asm/unaligned.h>
29 #define AD_SD_COMM_CHAN_MASK 0x3
31 #define AD_SD_REG_COMM 0x00
32 #define AD_SD_REG_DATA 0x03
35 * ad_sd_set_comm() - Set communications register
37 * @sigma_delta: The sigma delta device
38 * @comm: New value for the communications register
40 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
42 /* Some variants use the lower two bits of the communications register
43 * to select the channel */
44 sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
46 EXPORT_SYMBOL_NS_GPL(ad_sd_set_comm, IIO_AD_SIGMA_DELTA);
49 * ad_sd_write_reg() - Write a register
51 * @sigma_delta: The sigma delta device
52 * @reg: Address of the register
53 * @size: Size of the register (0-3)
54 * @val: Value to write to the register
56 * Returns 0 on success, an error code otherwise.
58 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
59 unsigned int size, unsigned int val)
61 uint8_t *data = sigma_delta->tx_buf;
62 struct spi_transfer t = {
65 .cs_change = sigma_delta->keep_cs_asserted,
70 data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
74 put_unaligned_be24(val, &data[1]);
77 put_unaligned_be16(val, &data[1]);
89 spi_message_add_tail(&t, &m);
91 if (sigma_delta->bus_locked)
92 ret = spi_sync_locked(sigma_delta->spi, &m);
94 ret = spi_sync(sigma_delta->spi, &m);
98 EXPORT_SYMBOL_NS_GPL(ad_sd_write_reg, IIO_AD_SIGMA_DELTA);
100 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
101 unsigned int reg, unsigned int size, uint8_t *val)
103 uint8_t *data = sigma_delta->tx_buf;
105 struct spi_transfer t[] = {
112 .cs_change = sigma_delta->bus_locked,
115 struct spi_message m;
117 spi_message_init(&m);
119 if (sigma_delta->info->has_registers) {
120 data[0] = reg << sigma_delta->info->addr_shift;
121 data[0] |= sigma_delta->info->read_mask;
122 data[0] |= sigma_delta->comm;
123 spi_message_add_tail(&t[0], &m);
125 spi_message_add_tail(&t[1], &m);
127 if (sigma_delta->bus_locked)
128 ret = spi_sync_locked(sigma_delta->spi, &m);
130 ret = spi_sync(sigma_delta->spi, &m);
136 * ad_sd_read_reg() - Read a register
138 * @sigma_delta: The sigma delta device
139 * @reg: Address of the register
140 * @size: Size of the register (1-4)
143 * Returns 0 on success, an error code otherwise.
145 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
146 unsigned int reg, unsigned int size, unsigned int *val)
150 ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf);
156 *val = get_unaligned_be32(sigma_delta->rx_buf);
159 *val = get_unaligned_be24(sigma_delta->rx_buf);
162 *val = get_unaligned_be16(sigma_delta->rx_buf);
165 *val = sigma_delta->rx_buf[0];
175 EXPORT_SYMBOL_NS_GPL(ad_sd_read_reg, IIO_AD_SIGMA_DELTA);
178 * ad_sd_reset() - Reset the serial interface
180 * @sigma_delta: The sigma delta device
181 * @reset_length: Number of SCLKs with DIN = 1
183 * Returns 0 on success, an error code otherwise.
185 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
186 unsigned int reset_length)
192 size = DIV_ROUND_UP(reset_length, 8);
193 buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
197 memset(buf, 0xff, size);
198 ret = spi_write(sigma_delta->spi, buf, size);
203 EXPORT_SYMBOL_NS_GPL(ad_sd_reset, IIO_AD_SIGMA_DELTA);
205 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
206 unsigned int mode, unsigned int channel)
209 unsigned long timeout;
211 ret = ad_sigma_delta_set_channel(sigma_delta, channel);
215 spi_bus_lock(sigma_delta->spi->master);
216 sigma_delta->bus_locked = true;
217 sigma_delta->keep_cs_asserted = true;
218 reinit_completion(&sigma_delta->completion);
220 ret = ad_sigma_delta_set_mode(sigma_delta, mode);
224 sigma_delta->irq_dis = false;
225 enable_irq(sigma_delta->spi->irq);
226 timeout = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ);
228 sigma_delta->irq_dis = true;
229 disable_irq_nosync(sigma_delta->spi->irq);
235 sigma_delta->keep_cs_asserted = false;
236 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
237 sigma_delta->bus_locked = false;
238 spi_bus_unlock(sigma_delta->spi->master);
242 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate, IIO_AD_SIGMA_DELTA);
245 * ad_sd_calibrate_all() - Performs channel calibration
246 * @sigma_delta: The sigma delta device
247 * @cb: Array of channels and calibration type to perform
248 * @n: Number of items in cb
250 * Returns 0 on success, an error code otherwise.
252 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
253 const struct ad_sd_calib_data *cb, unsigned int n)
258 for (i = 0; i < n; i++) {
259 ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
266 EXPORT_SYMBOL_NS_GPL(ad_sd_calibrate_all, IIO_AD_SIGMA_DELTA);
269 * ad_sigma_delta_single_conversion() - Performs a single data conversion
270 * @indio_dev: The IIO device
271 * @chan: The conversion is done for this channel
272 * @val: Pointer to the location where to store the read value
274 * Returns: 0 on success, an error value otherwise.
276 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
277 const struct iio_chan_spec *chan, int *val)
279 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
280 unsigned int sample, raw_sample;
281 unsigned int data_reg;
284 ret = iio_device_claim_direct_mode(indio_dev);
288 ad_sigma_delta_set_channel(sigma_delta, chan->address);
290 spi_bus_lock(sigma_delta->spi->master);
291 sigma_delta->bus_locked = true;
292 sigma_delta->keep_cs_asserted = true;
293 reinit_completion(&sigma_delta->completion);
295 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
297 sigma_delta->irq_dis = false;
298 enable_irq(sigma_delta->spi->irq);
299 ret = wait_for_completion_interruptible_timeout(
300 &sigma_delta->completion, HZ);
307 if (sigma_delta->info->data_reg != 0)
308 data_reg = sigma_delta->info->data_reg;
310 data_reg = AD_SD_REG_DATA;
312 ret = ad_sd_read_reg(sigma_delta, data_reg,
313 DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
317 if (!sigma_delta->irq_dis) {
318 disable_irq_nosync(sigma_delta->spi->irq);
319 sigma_delta->irq_dis = true;
322 sigma_delta->keep_cs_asserted = false;
323 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
324 sigma_delta->bus_locked = false;
325 spi_bus_unlock(sigma_delta->spi->master);
326 iio_device_release_direct_mode(indio_dev);
331 sample = raw_sample >> chan->scan_type.shift;
332 sample &= (1 << chan->scan_type.realbits) - 1;
335 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
341 EXPORT_SYMBOL_NS_GPL(ad_sigma_delta_single_conversion, IIO_AD_SIGMA_DELTA);
343 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
345 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
346 unsigned int i, slot, samples_buf_size;
347 unsigned int channel;
348 uint8_t *samples_buf;
351 if (sigma_delta->num_slots == 1) {
352 channel = find_first_bit(indio_dev->active_scan_mask,
353 indio_dev->masklength);
354 ret = ad_sigma_delta_set_channel(sigma_delta,
355 indio_dev->channels[channel].address);
361 * At this point update_scan_mode already enabled the required channels.
362 * For sigma-delta sequencer drivers with multiple slots, an update_scan_mode
363 * implementation is mandatory.
366 for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) {
367 sigma_delta->slots[slot] = indio_dev->channels[i].address;
372 sigma_delta->active_slots = slot;
373 sigma_delta->current_slot = 0;
375 if (sigma_delta->active_slots > 1) {
376 ret = ad_sigma_delta_append_status(sigma_delta, true);
381 samples_buf_size = ALIGN(slot * indio_dev->channels[0].scan_type.storagebits, 8);
382 samples_buf_size += sizeof(int64_t);
383 samples_buf = devm_krealloc(&sigma_delta->spi->dev, sigma_delta->samples_buf,
384 samples_buf_size, GFP_KERNEL);
388 sigma_delta->samples_buf = samples_buf;
390 spi_bus_lock(sigma_delta->spi->master);
391 sigma_delta->bus_locked = true;
392 sigma_delta->keep_cs_asserted = true;
394 ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
398 sigma_delta->irq_dis = false;
399 enable_irq(sigma_delta->spi->irq);
404 spi_bus_unlock(sigma_delta->spi->master);
409 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
411 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
413 reinit_completion(&sigma_delta->completion);
414 wait_for_completion_timeout(&sigma_delta->completion, HZ);
416 if (!sigma_delta->irq_dis) {
417 disable_irq_nosync(sigma_delta->spi->irq);
418 sigma_delta->irq_dis = true;
421 sigma_delta->keep_cs_asserted = false;
422 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
424 if (sigma_delta->status_appended)
425 ad_sigma_delta_append_status(sigma_delta, false);
427 ad_sigma_delta_disable_all(sigma_delta);
428 sigma_delta->bus_locked = false;
429 return spi_bus_unlock(sigma_delta->spi->master);
432 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
434 struct iio_poll_func *pf = p;
435 struct iio_dev *indio_dev = pf->indio_dev;
436 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
437 uint8_t *data = sigma_delta->rx_buf;
438 unsigned int transfer_size;
439 unsigned int sample_size;
440 unsigned int sample_pos;
441 unsigned int status_pos;
442 unsigned int reg_size;
443 unsigned int data_reg;
445 reg_size = indio_dev->channels[0].scan_type.realbits +
446 indio_dev->channels[0].scan_type.shift;
447 reg_size = DIV_ROUND_UP(reg_size, 8);
449 if (sigma_delta->info->data_reg != 0)
450 data_reg = sigma_delta->info->data_reg;
452 data_reg = AD_SD_REG_DATA;
454 /* Status word will be appended to the sample during transfer */
455 if (sigma_delta->status_appended)
456 transfer_size = reg_size + 1;
458 transfer_size = reg_size;
464 status_pos = reg_size;
465 ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[0]);
469 * Data array after transfer will look like (if status is appended):
470 * data[] = { [0][sample][sample][sample][status] }
471 * Keeping the first byte 0 shifts the status postion by 1 byte to the right.
473 status_pos = reg_size + 1;
475 /* We store 24 bit samples in a 32 bit word. Keep the upper
476 * byte set to zero. */
477 ad_sd_read_reg_raw(sigma_delta, data_reg, transfer_size, &data[1]);
482 * For devices sampling only one channel at
483 * once, there is no need for sample number tracking.
485 if (sigma_delta->active_slots == 1) {
486 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
490 if (sigma_delta->status_appended) {
491 u8 converted_channel;
493 converted_channel = data[status_pos] & sigma_delta->info->status_ch_mask;
494 if (converted_channel != sigma_delta->slots[sigma_delta->current_slot]) {
496 * Desync occurred during continuous sampling of multiple channels.
497 * Drop this incomplete sample and start from first channel again.
500 sigma_delta->current_slot = 0;
505 sample_size = indio_dev->channels[0].scan_type.storagebits / 8;
506 sample_pos = sample_size * sigma_delta->current_slot;
507 memcpy(&sigma_delta->samples_buf[sample_pos], data, sample_size);
508 sigma_delta->current_slot++;
510 if (sigma_delta->current_slot == sigma_delta->active_slots) {
511 sigma_delta->current_slot = 0;
512 iio_push_to_buffers_with_timestamp(indio_dev, sigma_delta->samples_buf,
517 iio_trigger_notify_done(indio_dev->trig);
518 sigma_delta->irq_dis = false;
519 enable_irq(sigma_delta->spi->irq);
524 static bool ad_sd_validate_scan_mask(struct iio_dev *indio_dev, const unsigned long *mask)
526 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
528 return bitmap_weight(mask, indio_dev->masklength) <= sigma_delta->num_slots;
531 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
532 .postenable = &ad_sd_buffer_postenable,
533 .postdisable = &ad_sd_buffer_postdisable,
534 .validate_scan_mask = &ad_sd_validate_scan_mask,
537 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
539 struct ad_sigma_delta *sigma_delta = private;
541 complete(&sigma_delta->completion);
542 disable_irq_nosync(irq);
543 sigma_delta->irq_dis = true;
544 iio_trigger_poll(sigma_delta->trig);
550 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
551 * @indio_dev: The IIO device
552 * @trig: The new trigger
554 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
555 * device, -EINVAL otherwise.
557 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
559 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
561 if (sigma_delta->trig != trig)
566 EXPORT_SYMBOL_NS_GPL(ad_sd_validate_trigger, IIO_AD_SIGMA_DELTA);
568 static int devm_ad_sd_probe_trigger(struct device *dev, struct iio_dev *indio_dev)
570 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
573 if (dev != &sigma_delta->spi->dev) {
574 dev_err(dev, "Trigger parent should be '%s', got '%s'\n",
575 dev_name(dev), dev_name(&sigma_delta->spi->dev));
579 sigma_delta->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
580 iio_device_id(indio_dev));
581 if (sigma_delta->trig == NULL)
584 init_completion(&sigma_delta->completion);
586 sigma_delta->irq_dis = true;
588 /* the IRQ core clears IRQ_DISABLE_UNLAZY flag when freeing an IRQ */
589 irq_set_status_flags(sigma_delta->spi->irq, IRQ_DISABLE_UNLAZY);
591 ret = devm_request_irq(dev, sigma_delta->spi->irq,
592 ad_sd_data_rdy_trig_poll,
593 sigma_delta->info->irq_flags | IRQF_NO_AUTOEN,
599 iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
601 ret = devm_iio_trigger_register(dev, sigma_delta->trig);
605 /* select default trigger */
606 indio_dev->trig = iio_trigger_get(sigma_delta->trig);
612 * devm_ad_sd_setup_buffer_and_trigger() - Device-managed buffer & trigger setup
613 * @dev: Device object to which to bind the life-time of the resources attached
614 * @indio_dev: The IIO device
616 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev)
618 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
621 sigma_delta->slots = devm_kcalloc(dev, sigma_delta->num_slots,
622 sizeof(*sigma_delta->slots), GFP_KERNEL);
623 if (!sigma_delta->slots)
626 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
627 &iio_pollfunc_store_time,
628 &ad_sd_trigger_handler,
629 &ad_sd_buffer_setup_ops);
633 return devm_ad_sd_probe_trigger(dev, indio_dev);
635 EXPORT_SYMBOL_NS_GPL(devm_ad_sd_setup_buffer_and_trigger, IIO_AD_SIGMA_DELTA);
638 * ad_sd_init() - Initializes a ad_sigma_delta struct
639 * @sigma_delta: The ad_sigma_delta device
640 * @indio_dev: The IIO device which the Sigma Delta device is used for
641 * @spi: The SPI device for the ad_sigma_delta device
642 * @info: Device specific callbacks and options
644 * This function needs to be called before any other operations are performed on
645 * the ad_sigma_delta struct.
647 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
648 struct spi_device *spi, const struct ad_sigma_delta_info *info)
650 sigma_delta->spi = spi;
651 sigma_delta->info = info;
653 /* If the field is unset in ad_sigma_delta_info, asume there can only be 1 slot. */
654 if (!info->num_slots)
655 sigma_delta->num_slots = 1;
657 sigma_delta->num_slots = info->num_slots;
659 if (sigma_delta->num_slots > 1) {
660 if (!indio_dev->info->update_scan_mode) {
661 dev_err(&spi->dev, "iio_dev lacks update_scan_mode().\n");
665 if (!info->disable_all) {
666 dev_err(&spi->dev, "ad_sigma_delta_info lacks disable_all().\n");
671 iio_device_set_drvdata(indio_dev, sigma_delta);
675 EXPORT_SYMBOL_NS_GPL(ad_sd_init, IIO_AD_SIGMA_DELTA);
677 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
678 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
679 MODULE_LICENSE("GPL v2");