Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 1 | #include <linux/interrupt.h> |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 2 | #include <linux/mutex.h> |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 3 | #include <linux/kernel.h> |
| 4 | #include <linux/spi/spi.h> |
Mike Frysinger | 1cb6c1f | 2010-05-23 03:10:35 -0400 | [diff] [blame] | 5 | #include <linux/slab.h> |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 6 | #include <linux/bitops.h> |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame] | 7 | #include <linux/export.h> |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 8 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 9 | #include <linux/iio/iio.h> |
Lars-Peter Clausen | 1ffe2e7 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 10 | #include <linux/iio/buffer.h> |
| 11 | #include <linux/iio/triggered_buffer.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 12 | #include <linux/iio/trigger_consumer.h> |
Lars-Peter Clausen | cd888a17 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 13 | |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 14 | #include "adis16400.h" |
| 15 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 16 | int adis16400_update_scan_mode(struct iio_dev *indio_dev, |
| 17 | const unsigned long *scan_mask) |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 18 | { |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 19 | struct adis16400_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 20 | struct adis *adis = &st->adis; |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 21 | unsigned int burst_length; |
| 22 | u8 *tx; |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 23 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 24 | if (st->variant->flags & ADIS16400_NO_BURST) |
| 25 | return adis_update_scan_mode(indio_dev, scan_mask); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 26 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 27 | kfree(adis->xfer); |
| 28 | kfree(adis->buffer); |
| 29 | |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 30 | /* All but the timestamp channel */ |
| 31 | burst_length = (indio_dev->num_channels - 1) * sizeof(u16); |
Lars-Peter Clausen | d046ba2 | 2015-05-15 17:18:38 +0200 | [diff] [blame] | 32 | if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) |
| 33 | burst_length += sizeof(u16); |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 34 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 35 | adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL); |
| 36 | if (!adis->xfer) |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 37 | return -ENOMEM; |
| 38 | |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 39 | adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL); |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 40 | if (!adis->buffer) |
| 41 | return -ENOMEM; |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 42 | |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 43 | tx = adis->buffer + burst_length; |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 44 | tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD); |
| 45 | tx[1] = 0; |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 46 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 47 | adis->xfer[0].tx_buf = tx; |
| 48 | adis->xfer[0].bits_per_word = 8; |
| 49 | adis->xfer[0].len = 2; |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 50 | adis->xfer[1].rx_buf = adis->buffer; |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 51 | adis->xfer[1].bits_per_word = 8; |
Paul Cercueil | 9df5603 | 2015-05-15 17:18:37 +0200 | [diff] [blame] | 52 | adis->xfer[1].len = burst_length; |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 53 | |
| 54 | spi_message_init(&adis->msg); |
| 55 | spi_message_add_tail(&adis->xfer[0], &adis->msg); |
| 56 | spi_message_add_tail(&adis->xfer[1], &adis->msg); |
| 57 | |
| 58 | return 0; |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 61 | irqreturn_t adis16400_trigger_handler(int irq, void *p) |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 62 | { |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 63 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 64 | struct iio_dev *indio_dev = pf->indio_dev; |
Jonathan Cameron | 38d15f0 | 2011-05-18 14:42:23 +0100 | [diff] [blame] | 65 | struct adis16400_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 66 | struct adis *adis = &st->adis; |
| 67 | u32 old_speed_hz = st->adis.spi->max_speed_hz; |
Lars-Peter Clausen | d046ba2 | 2015-05-15 17:18:38 +0200 | [diff] [blame] | 68 | void *buffer; |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 69 | int ret; |
Jonathan Cameron | 420fe2e | 2012-04-21 10:09:35 +0100 | [diff] [blame] | 70 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 71 | if (!adis->buffer) |
| 72 | return -ENOMEM; |
| 73 | |
| 74 | if (!(st->variant->flags & ADIS16400_NO_BURST) && |
| 75 | st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) { |
| 76 | st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST; |
| 77 | spi_setup(st->adis.spi); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 80 | ret = spi_sync(adis->spi, &adis->msg); |
| 81 | if (ret) |
| 82 | dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret); |
| 83 | |
| 84 | if (!(st->variant->flags & ADIS16400_NO_BURST)) { |
| 85 | st->adis.spi->max_speed_hz = old_speed_hz; |
| 86 | spi_setup(st->adis.spi); |
Jonathan Cameron | 2a29a90 | 2011-05-18 14:41:28 +0100 | [diff] [blame] | 87 | } |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 88 | |
Lars-Peter Clausen | d046ba2 | 2015-05-15 17:18:38 +0200 | [diff] [blame] | 89 | if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) |
| 90 | buffer = adis->buffer + sizeof(u16); |
| 91 | else |
| 92 | buffer = adis->buffer; |
| 93 | |
| 94 | iio_push_to_buffers_with_timestamp(indio_dev, buffer, |
Lars-Peter Clausen | 8c7b032 | 2013-09-19 13:59:00 +0100 | [diff] [blame] | 95 | pf->timestamp); |
Lars-Peter Clausen | 5eda355 | 2013-01-16 12:48:00 +0000 | [diff] [blame] | 96 | |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 97 | iio_trigger_notify_done(indio_dev->trig); |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 98 | |
Jonathan Cameron | e785484 | 2011-05-18 14:41:27 +0100 | [diff] [blame] | 99 | return IRQ_HANDLED; |
Barry Song | a9d26f0 | 2010-05-04 14:43:15 +0100 | [diff] [blame] | 100 | } |