2 * Support code for Analog Devices Sigma-Delta ADCs
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2.
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_GPL(ad_sd_set_comm);
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->data;
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;
79 put_unaligned_be16(val, &data[1]);
91 spi_message_add_tail(&t, &m);
93 if (sigma_delta->bus_locked)
94 ret = spi_sync_locked(sigma_delta->spi, &m);
96 ret = spi_sync(sigma_delta->spi, &m);
100 EXPORT_SYMBOL_GPL(ad_sd_write_reg);
102 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
103 unsigned int reg, unsigned int size, uint8_t *val)
105 uint8_t *data = sigma_delta->data;
107 struct spi_transfer t[] = {
114 .cs_change = sigma_delta->bus_locked,
117 struct spi_message m;
119 spi_message_init(&m);
121 if (sigma_delta->info->has_registers) {
122 data[0] = reg << sigma_delta->info->addr_shift;
123 data[0] |= sigma_delta->info->read_mask;
124 data[0] |= sigma_delta->comm;
125 spi_message_add_tail(&t[0], &m);
127 spi_message_add_tail(&t[1], &m);
129 if (sigma_delta->bus_locked)
130 ret = spi_sync_locked(sigma_delta->spi, &m);
132 ret = spi_sync(sigma_delta->spi, &m);
138 * ad_sd_read_reg() - Read a register
140 * @sigma_delta: The sigma delta device
141 * @reg: Address of the register
142 * @size: Size of the register (1-4)
145 * Returns 0 on success, an error code otherwise.
147 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
148 unsigned int reg, unsigned int size, unsigned int *val)
152 ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
158 *val = get_unaligned_be32(sigma_delta->data);
161 *val = (sigma_delta->data[0] << 16) |
162 (sigma_delta->data[1] << 8) |
163 sigma_delta->data[2];
166 *val = get_unaligned_be16(sigma_delta->data);
169 *val = sigma_delta->data[0];
179 EXPORT_SYMBOL_GPL(ad_sd_read_reg);
182 * ad_sd_reset() - Reset the serial interface
184 * @sigma_delta: The sigma delta device
185 * @reset_length: Number of SCLKs with DIN = 1
187 * Returns 0 on success, an error code otherwise.
189 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
190 unsigned int reset_length)
196 size = DIV_ROUND_UP(reset_length, 8);
197 buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
201 memset(buf, 0xff, size);
202 ret = spi_write(sigma_delta->spi, buf, size);
207 EXPORT_SYMBOL_GPL(ad_sd_reset);
209 static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
210 unsigned int mode, unsigned int channel)
214 ret = ad_sigma_delta_set_channel(sigma_delta, channel);
218 spi_bus_lock(sigma_delta->spi->master);
219 sigma_delta->bus_locked = true;
220 sigma_delta->keep_cs_asserted = true;
221 reinit_completion(&sigma_delta->completion);
223 ret = ad_sigma_delta_set_mode(sigma_delta, mode);
227 sigma_delta->irq_dis = false;
228 enable_irq(sigma_delta->spi->irq);
229 ret = wait_for_completion_timeout(&sigma_delta->completion, 2*HZ);
231 sigma_delta->irq_dis = true;
232 disable_irq_nosync(sigma_delta->spi->irq);
238 sigma_delta->keep_cs_asserted = false;
239 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
240 sigma_delta->bus_locked = false;
241 spi_bus_unlock(sigma_delta->spi->master);
247 * ad_sd_calibrate_all() - Performs channel calibration
248 * @sigma_delta: The sigma delta device
249 * @cb: Array of channels and calibration type to perform
250 * @n: Number of items in cb
252 * Returns 0 on success, an error code otherwise.
254 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
255 const struct ad_sd_calib_data *cb, unsigned int n)
260 for (i = 0; i < n; i++) {
261 ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
268 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all);
271 * ad_sigma_delta_single_conversion() - Performs a single data conversion
272 * @indio_dev: The IIO device
273 * @chan: The conversion is done for this channel
274 * @val: Pointer to the location where to store the read value
276 * Returns: 0 on success, an error value otherwise.
278 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
279 const struct iio_chan_spec *chan, int *val)
281 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
282 unsigned int sample, raw_sample;
285 if (iio_buffer_enabled(indio_dev))
288 mutex_lock(&indio_dev->mlock);
289 ad_sigma_delta_set_channel(sigma_delta, chan->address);
291 spi_bus_lock(sigma_delta->spi->master);
292 sigma_delta->bus_locked = true;
293 sigma_delta->keep_cs_asserted = true;
294 reinit_completion(&sigma_delta->completion);
296 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
298 sigma_delta->irq_dis = false;
299 enable_irq(sigma_delta->spi->irq);
300 ret = wait_for_completion_interruptible_timeout(
301 &sigma_delta->completion, HZ);
308 ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_DATA,
309 DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
313 if (!sigma_delta->irq_dis) {
314 disable_irq_nosync(sigma_delta->spi->irq);
315 sigma_delta->irq_dis = true;
318 sigma_delta->keep_cs_asserted = false;
319 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
320 sigma_delta->bus_locked = false;
321 spi_bus_unlock(sigma_delta->spi->master);
322 mutex_unlock(&indio_dev->mlock);
327 sample = raw_sample >> chan->scan_type.shift;
328 sample &= (1 << chan->scan_type.realbits) - 1;
331 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
337 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion);
339 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
341 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
342 unsigned int channel;
345 ret = iio_triggered_buffer_postenable(indio_dev);
349 channel = find_first_bit(indio_dev->active_scan_mask,
350 indio_dev->masklength);
351 ret = ad_sigma_delta_set_channel(sigma_delta,
352 indio_dev->channels[channel].address);
356 spi_bus_lock(sigma_delta->spi->master);
357 sigma_delta->bus_locked = true;
358 sigma_delta->keep_cs_asserted = true;
360 ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
364 sigma_delta->irq_dis = false;
365 enable_irq(sigma_delta->spi->irq);
370 spi_bus_unlock(sigma_delta->spi->master);
376 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
378 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
380 reinit_completion(&sigma_delta->completion);
381 wait_for_completion_timeout(&sigma_delta->completion, HZ);
383 if (!sigma_delta->irq_dis) {
384 disable_irq_nosync(sigma_delta->spi->irq);
385 sigma_delta->irq_dis = true;
388 sigma_delta->keep_cs_asserted = false;
389 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
391 sigma_delta->bus_locked = false;
392 return spi_bus_unlock(sigma_delta->spi->master);
395 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
397 struct iio_poll_func *pf = p;
398 struct iio_dev *indio_dev = pf->indio_dev;
399 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
400 unsigned int reg_size;
404 memset(data, 0x00, 16);
406 reg_size = indio_dev->channels[0].scan_type.realbits +
407 indio_dev->channels[0].scan_type.shift;
408 reg_size = DIV_ROUND_UP(reg_size, 8);
414 ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
418 /* We store 24 bit samples in a 32 bit word. Keep the upper
419 * byte set to zero. */
420 ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
425 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
427 iio_trigger_notify_done(indio_dev->trig);
428 sigma_delta->irq_dis = false;
429 enable_irq(sigma_delta->spi->irq);
434 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
435 .postenable = &ad_sd_buffer_postenable,
436 .predisable = &iio_triggered_buffer_predisable,
437 .postdisable = &ad_sd_buffer_postdisable,
438 .validate_scan_mask = &iio_validate_scan_mask_onehot,
441 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
443 struct ad_sigma_delta *sigma_delta = private;
445 complete(&sigma_delta->completion);
446 disable_irq_nosync(irq);
447 sigma_delta->irq_dis = true;
448 iio_trigger_poll(sigma_delta->trig);
454 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
455 * @indio_dev: The IIO device
456 * @trig: The new trigger
458 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
459 * device, -EINVAL otherwise.
461 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
463 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
465 if (sigma_delta->trig != trig)
470 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger);
472 static const struct iio_trigger_ops ad_sd_trigger_ops = {
473 .owner = THIS_MODULE,
476 static int ad_sd_probe_trigger(struct iio_dev *indio_dev)
478 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
481 sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
483 if (sigma_delta->trig == NULL) {
487 sigma_delta->trig->ops = &ad_sd_trigger_ops;
488 init_completion(&sigma_delta->completion);
490 ret = request_irq(sigma_delta->spi->irq,
491 ad_sd_data_rdy_trig_poll,
496 goto error_free_trig;
498 if (!sigma_delta->irq_dis) {
499 sigma_delta->irq_dis = true;
500 disable_irq_nosync(sigma_delta->spi->irq);
502 sigma_delta->trig->dev.parent = &sigma_delta->spi->dev;
503 iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
505 ret = iio_trigger_register(sigma_delta->trig);
509 /* select default trigger */
510 indio_dev->trig = iio_trigger_get(sigma_delta->trig);
515 free_irq(sigma_delta->spi->irq, sigma_delta);
517 iio_trigger_free(sigma_delta->trig);
522 static void ad_sd_remove_trigger(struct iio_dev *indio_dev)
524 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
526 iio_trigger_unregister(sigma_delta->trig);
527 free_irq(sigma_delta->spi->irq, sigma_delta);
528 iio_trigger_free(sigma_delta->trig);
532 * ad_sd_setup_buffer_and_trigger() -
533 * @indio_dev: The IIO device
535 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev)
539 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
540 &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
544 ret = ad_sd_probe_trigger(indio_dev);
546 iio_triggered_buffer_cleanup(indio_dev);
552 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger);
555 * ad_sd_cleanup_buffer_and_trigger() -
556 * @indio_dev: The IIO device
558 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev)
560 ad_sd_remove_trigger(indio_dev);
561 iio_triggered_buffer_cleanup(indio_dev);
563 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger);
566 * ad_sd_init() - Initializes a ad_sigma_delta struct
567 * @sigma_delta: The ad_sigma_delta device
568 * @indio_dev: The IIO device which the Sigma Delta device is used for
569 * @spi: The SPI device for the ad_sigma_delta device
570 * @info: Device specific callbacks and options
572 * This function needs to be called before any other operations are performed on
573 * the ad_sigma_delta struct.
575 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
576 struct spi_device *spi, const struct ad_sigma_delta_info *info)
578 sigma_delta->spi = spi;
579 sigma_delta->info = info;
580 iio_device_set_drvdata(indio_dev, sigma_delta);
584 EXPORT_SYMBOL_GPL(ad_sd_init);
586 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
587 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
588 MODULE_LICENSE("GPL v2");