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 | ec04cb0 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 21 | #include <linux/iio/imu/adis.h> |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 22 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 23 | int adis_update_scan_mode(struct iio_dev *indio_dev, |
| 24 | const unsigned long *scan_mask) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 25 | { |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 26 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
| 27 | const struct iio_chan_spec *chan; |
| 28 | unsigned int scan_count; |
| 29 | unsigned int i, j; |
| 30 | __be16 *tx, *rx; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 31 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 32 | kfree(adis->xfer); |
| 33 | kfree(adis->buffer); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 34 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 35 | scan_count = indio_dev->scan_bytes / 2; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 36 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 37 | adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL); |
| 38 | if (!adis->xfer) |
| 39 | return -ENOMEM; |
| 40 | |
| 41 | adis->buffer = kzalloc(indio_dev->scan_bytes * 2, GFP_KERNEL); |
| 42 | if (!adis->buffer) |
| 43 | return -ENOMEM; |
| 44 | |
| 45 | rx = adis->buffer; |
Lars-Peter Clausen | d590faf | 2015-11-27 14:55:56 +0100 | [diff] [blame^] | 46 | tx = rx + scan_count; |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 47 | |
| 48 | spi_message_init(&adis->msg); |
| 49 | |
| 50 | for (j = 0; j <= scan_count; j++) { |
| 51 | adis->xfer[j].bits_per_word = 8; |
| 52 | if (j != scan_count) |
| 53 | adis->xfer[j].cs_change = 1; |
| 54 | adis->xfer[j].len = 2; |
| 55 | adis->xfer[j].delay_usecs = adis->data->read_delay; |
| 56 | if (j < scan_count) |
| 57 | adis->xfer[j].tx_buf = &tx[j]; |
| 58 | if (j >= 1) |
| 59 | adis->xfer[j].rx_buf = &rx[j - 1]; |
| 60 | spi_message_add_tail(&adis->xfer[j], &adis->msg); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 63 | chan = indio_dev->channels; |
| 64 | for (i = 0; i < indio_dev->num_channels; i++, chan++) { |
| 65 | if (!test_bit(chan->scan_index, scan_mask)) |
| 66 | continue; |
Lars-Peter Clausen | 57a1228 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 67 | if (chan->scan_type.storagebits == 32) |
| 68 | *tx++ = cpu_to_be16((chan->address + 2) << 8); |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 69 | *tx++ = cpu_to_be16(chan->address << 8); |
| 70 | } |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 71 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 72 | return 0; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 73 | } |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(adis_update_scan_mode); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 75 | |
| 76 | static irqreturn_t adis_trigger_handler(int irq, void *p) |
| 77 | { |
| 78 | struct iio_poll_func *pf = p; |
| 79 | struct iio_dev *indio_dev = pf->indio_dev; |
| 80 | struct adis *adis = iio_device_get_drvdata(indio_dev); |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 81 | int ret; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 82 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 83 | if (!adis->buffer) |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 84 | return -ENOMEM; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 85 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 86 | if (adis->data->has_paging) { |
| 87 | mutex_lock(&adis->txrx_lock); |
| 88 | if (adis->current_page != 0) { |
| 89 | adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID); |
| 90 | adis->tx[1] = 0; |
| 91 | spi_write(adis->spi, adis->tx, 2); |
| 92 | } |
| 93 | } |
| 94 | |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 95 | ret = spi_sync(adis->spi, &adis->msg); |
| 96 | if (ret) |
| 97 | dev_err(&adis->spi->dev, "Failed to read data: %d", ret); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 98 | |
Lars-Peter Clausen | 484a0bf | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 99 | |
| 100 | if (adis->data->has_paging) { |
| 101 | adis->current_page = 0; |
| 102 | mutex_unlock(&adis->txrx_lock); |
| 103 | } |
| 104 | |
Lars-Peter Clausen | 419d8ce | 2013-09-19 13:59:00 +0100 | [diff] [blame] | 105 | iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer, |
| 106 | pf->timestamp); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 107 | |
| 108 | iio_trigger_notify_done(indio_dev->trig); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 109 | |
| 110 | return IRQ_HANDLED; |
| 111 | } |
| 112 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 113 | /** |
| 114 | * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device |
| 115 | * @adis: The adis device. |
| 116 | * @indio_dev: The IIO device. |
| 117 | * @trigger_handler: Optional trigger handler, may be NULL. |
| 118 | * |
| 119 | * Returns 0 on success, a negative error code otherwise. |
| 120 | * |
| 121 | * This function sets up the buffer and trigger for a adis devices. If |
| 122 | * 'trigger_handler' is NULL the default trigger handler will be used. The |
| 123 | * default trigger handler will simply read the registers assigned to the |
| 124 | * currently active channels. |
| 125 | * |
| 126 | * adis_cleanup_buffer_and_trigger() should be called to free the resources |
| 127 | * allocated by this function. |
| 128 | */ |
| 129 | int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev, |
| 130 | irqreturn_t (*trigger_handler)(int, void *)) |
| 131 | { |
| 132 | int ret; |
| 133 | |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 134 | if (!trigger_handler) |
| 135 | trigger_handler = adis_trigger_handler; |
| 136 | |
| 137 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 138 | trigger_handler, NULL); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 139 | if (ret) |
| 140 | return ret; |
| 141 | |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 142 | if (adis->spi->irq) { |
| 143 | ret = adis_probe_trigger(adis, indio_dev); |
| 144 | if (ret) |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 145 | goto error_buffer_cleanup; |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 146 | } |
| 147 | return 0; |
| 148 | |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 149 | error_buffer_cleanup: |
| 150 | iio_triggered_buffer_cleanup(indio_dev); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 151 | return ret; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger); |
| 154 | |
| 155 | /** |
| 156 | * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources |
| 157 | * @adis: The adis device. |
| 158 | * @indio_dev: The IIO device. |
| 159 | * |
| 160 | * Frees resources allocated by adis_setup_buffer_and_trigger() |
| 161 | */ |
| 162 | void adis_cleanup_buffer_and_trigger(struct adis *adis, |
| 163 | struct iio_dev *indio_dev) |
| 164 | { |
| 165 | if (adis->spi->irq) |
| 166 | adis_remove_trigger(adis); |
Lars-Peter Clausen | aacff89 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 167 | kfree(adis->buffer); |
| 168 | kfree(adis->xfer); |
Lars-Peter Clausen | a458c55 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 169 | iio_triggered_buffer_cleanup(indio_dev); |
Lars-Peter Clausen | ccd2b52 | 2012-11-13 13:28:00 +0000 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger); |