Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Common library for ADIS16XXX devices |
| 3 | * |
| 4 | * Copyright 2012 Analog Devices Inc. |
| 5 | * Author: Lars-Peter Clausen <lars@metafoo.de> |
| 6 | * |
| 7 | * Licensed under the GPL-2 or later. |
| 8 | */ |
| 9 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 10 | #include <linux/export.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | #include <linux/slab.h> |
| 16 | |
| 17 | #include <linux/iio/iio.h> |
| 18 | #include <linux/iio/buffer.h> |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 19 | #include <linux/iio/trigger_consumer.h> |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 20 | #include <linux/iio/triggered_buffer.h> |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 21 | |
| 22 | #include "adis.h" |
| 23 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 24 | int adis_update_scan_mode(struct iio_dev *indio_dev, |
| 25 | const unsigned long *scan_mask) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 26 | { |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 27 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
| 28 | const struct iio_chan_spec *chan; |
| 29 | unsigned int scan_count; |
| 30 | unsigned int i, j; |
| 31 | __be16 *tx, *rx; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 32 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 33 | kfree(adis->xfer); |
| 34 | kfree(adis->buffer); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 35 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 36 | scan_count = indio_dev->scan_bytes / 2; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 37 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 38 | adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL); |
| 39 | if (!adis->xfer) |
| 40 | return -ENOMEM; |
| 41 | |
| 42 | adis->buffer = kzalloc(indio_dev->scan_bytes * 2, GFP_KERNEL); |
| 43 | if (!adis->buffer) |
| 44 | return -ENOMEM; |
| 45 | |
| 46 | rx = adis->buffer; |
| 47 | tx = rx + indio_dev->scan_bytes; |
| 48 | |
| 49 | spi_message_init(&adis->msg); |
| 50 | |
| 51 | for (j = 0; j <= scan_count; j++) { |
| 52 | adis->xfer[j].bits_per_word = 8; |
| 53 | if (j != scan_count) |
| 54 | adis->xfer[j].cs_change = 1; |
| 55 | adis->xfer[j].len = 2; |
| 56 | adis->xfer[j].delay_usecs = adis->data->read_delay; |
| 57 | if (j < scan_count) |
| 58 | adis->xfer[j].tx_buf = &tx[j]; |
| 59 | if (j >= 1) |
| 60 | adis->xfer[j].rx_buf = &rx[j - 1]; |
| 61 | spi_message_add_tail(&adis->xfer[j], &adis->msg); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 64 | chan = indio_dev->channels; |
| 65 | for (i = 0; i < indio_dev->num_channels; i++, chan++) { |
| 66 | if (!test_bit(chan->scan_index, scan_mask)) |
| 67 | continue; |
| 68 | *tx++ = cpu_to_be16(chan->address << 8); |
| 69 | } |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 70 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 71 | return 0; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 72 | } |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 73 | EXPORT_SYMBOL_GPL(adis_update_scan_mode); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 74 | |
| 75 | static irqreturn_t adis_trigger_handler(int irq, void *p) |
| 76 | { |
| 77 | struct iio_poll_func *pf = p; |
| 78 | struct iio_dev *indio_dev = pf->indio_dev; |
| 79 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 80 | int ret; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 81 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 82 | if (!adis->buffer) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 83 | return -ENOMEM; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 84 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 85 | ret = spi_sync(adis->spi, &adis->msg); |
| 86 | if (ret) |
| 87 | dev_err(&adis->spi->dev, "Failed to read data: %d", ret); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 88 | |
| 89 | /* Guaranteed to be aligned with 8 byte boundary */ |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 90 | if (indio_dev->scan_timestamp) { |
| 91 | void *b = adis->buffer + indio_dev->scan_bytes - sizeof(s64); |
| 92 | *(s64 *)b = pf->timestamp; |
| 93 | } |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 94 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 95 | iio_push_to_buffers(indio_dev, adis->buffer); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 96 | |
| 97 | iio_trigger_notify_done(indio_dev->trig); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 98 | |
| 99 | return IRQ_HANDLED; |
| 100 | } |
| 101 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 102 | /** |
| 103 | * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device |
| 104 | * @adis: The adis device. |
| 105 | * @indio_dev: The IIO device. |
| 106 | * @trigger_handler: Optional trigger handler, may be NULL. |
| 107 | * |
| 108 | * Returns 0 on success, a negative error code otherwise. |
| 109 | * |
| 110 | * This function sets up the buffer and trigger for a adis devices. If |
| 111 | * 'trigger_handler' is NULL the default trigger handler will be used. The |
| 112 | * default trigger handler will simply read the registers assigned to the |
| 113 | * currently active channels. |
| 114 | * |
| 115 | * adis_cleanup_buffer_and_trigger() should be called to free the resources |
| 116 | * allocated by this function. |
| 117 | */ |
| 118 | int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev, |
| 119 | irqreturn_t (*trigger_handler)(int, void *)) |
| 120 | { |
| 121 | int ret; |
| 122 | |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 123 | if (!trigger_handler) |
| 124 | trigger_handler = adis_trigger_handler; |
| 125 | |
| 126 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 127 | trigger_handler, NULL); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 128 | if (ret) |
| 129 | return ret; |
| 130 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 131 | if (adis->spi->irq) { |
| 132 | ret = adis_probe_trigger(adis, indio_dev); |
| 133 | if (ret) |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 134 | goto error_buffer_cleanup; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 135 | } |
| 136 | return 0; |
| 137 | |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 138 | error_buffer_cleanup: |
| 139 | iio_triggered_buffer_cleanup(indio_dev); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger); |
| 143 | |
| 144 | /** |
| 145 | * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources |
| 146 | * @adis: The adis device. |
| 147 | * @indio_dev: The IIO device. |
| 148 | * |
| 149 | * Frees resources allocated by adis_setup_buffer_and_trigger() |
| 150 | */ |
| 151 | void adis_cleanup_buffer_and_trigger(struct adis *adis, |
| 152 | struct iio_dev *indio_dev) |
| 153 | { |
| 154 | if (adis->spi->irq) |
| 155 | adis_remove_trigger(adis); |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame^] | 156 | kfree(adis->buffer); |
| 157 | kfree(adis->xfer); |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 158 | iio_triggered_buffer_cleanup(indio_dev); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 159 | } |
| 160 | EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger); |