Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ADE7758 Poly Phase Multifunction Energy Metering IC driver |
| 3 | * |
| 4 | * Copyright 2010-2011 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2. |
| 7 | */ |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 9 | #include <linux/interrupt.h> |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/spi/spi.h> |
| 12 | #include <linux/slab.h> |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 14 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 15 | #include <linux/iio/iio.h> |
Jonathan Cameron | 94f3c7c | 2012-11-30 14:22:00 +0000 | [diff] [blame] | 16 | #include <linux/iio/kfifo_buf.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 17 | #include <linux/iio/trigger_consumer.h> |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 18 | #include "ade7758.h" |
| 19 | |
| 20 | /** |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 21 | * ade7758_spi_read_burst() - read data registers |
Lars-Peter Clausen | 2a0b871 | 2012-05-12 15:39:40 +0200 | [diff] [blame] | 22 | * @indio_dev: the IIO device |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 23 | **/ |
Lars-Peter Clausen | 2a0b871 | 2012-05-12 15:39:40 +0200 | [diff] [blame] | 24 | static int ade7758_spi_read_burst(struct iio_dev *indio_dev) |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 25 | { |
Michael Hennerich | a3f0237 | 2011-05-18 14:42:36 +0100 | [diff] [blame] | 26 | struct ade7758_state *st = iio_priv(indio_dev); |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 27 | int ret; |
| 28 | |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 29 | ret = spi_sync(st->us, &st->ring_msg); |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 30 | if (ret) |
| 31 | dev_err(&st->us->dev, "problem when reading WFORM value\n"); |
| 32 | |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 33 | return ret; |
| 34 | } |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 35 | |
Alison Schofield | 51fadb9 | 2016-03-26 12:50:24 -0700 | [diff] [blame] | 36 | static int ade7758_write_waveform_type(struct device *dev, unsigned int type) |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 37 | { |
| 38 | int ret; |
| 39 | u8 reg; |
| 40 | |
Georgiana Rodica Chelu | 89237e0 | 2016-09-19 18:41:56 +0300 | [diff] [blame] | 41 | ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 42 | if (ret) |
| 43 | goto out; |
| 44 | |
| 45 | reg &= ~0x1F; |
| 46 | reg |= type & 0x1F; |
| 47 | |
Georgiana Rodica Chelu | 89237e0 | 2016-09-19 18:41:56 +0300 | [diff] [blame] | 48 | ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg); |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 49 | out: |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 50 | return ret; |
| 51 | } |
| 52 | |
Peter Meerwald | 7d241b2 | 2013-07-07 21:24:00 +0100 | [diff] [blame] | 53 | /* Whilst this makes a lot of calls to iio_sw_ring functions - it is too device |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 54 | * specific to be rolled into the core. |
| 55 | */ |
Jonathan Cameron | 3f00ca4 | 2011-05-18 14:41:26 +0100 | [diff] [blame] | 56 | static irqreturn_t ade7758_trigger_handler(int irq, void *p) |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 57 | { |
Jonathan Cameron | 3f00ca4 | 2011-05-18 14:41:26 +0100 | [diff] [blame] | 58 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 59 | struct iio_dev *indio_dev = pf->indio_dev; |
Michael Hennerich | a3f0237 | 2011-05-18 14:42:36 +0100 | [diff] [blame] | 60 | struct ade7758_state *st = iio_priv(indio_dev); |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 61 | s64 dat64[2]; |
| 62 | u32 *dat32 = (u32 *)dat64; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 63 | |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 64 | if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) |
Lars-Peter Clausen | 2a0b871 | 2012-05-12 15:39:40 +0200 | [diff] [blame] | 65 | if (ade7758_spi_read_burst(indio_dev) >= 0) |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 66 | *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 67 | |
Lars-Peter Clausen | 9d6c398 | 2013-09-19 14:00:00 +0100 | [diff] [blame] | 68 | iio_push_to_buffers_with_timestamp(indio_dev, dat64, pf->timestamp); |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 69 | |
Michael Hennerich | a3f0237 | 2011-05-18 14:42:36 +0100 | [diff] [blame] | 70 | iio_trigger_notify_done(indio_dev->trig); |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 71 | |
Jonathan Cameron | 3f00ca4 | 2011-05-18 14:41:26 +0100 | [diff] [blame] | 72 | return IRQ_HANDLED; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 75 | /** |
| 76 | * ade7758_ring_preenable() setup the parameters of the ring before enabling |
| 77 | * |
Justin P. Mattock | 4abf6f8 | 2012-02-29 22:00:38 -0800 | [diff] [blame] | 78 | * The complex nature of the setting of the number of bytes per datum is due |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 79 | * to this driver currently ensuring that the timestamp is stored at an 8 |
| 80 | * byte boundary. |
| 81 | **/ |
| 82 | static int ade7758_ring_preenable(struct iio_dev *indio_dev) |
| 83 | { |
Alison Schofield | 51fadb9 | 2016-03-26 12:50:24 -0700 | [diff] [blame] | 84 | unsigned int channel; |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 85 | |
Lars-Peter Clausen | 79fa64e | 2014-11-04 18:03:15 +0100 | [diff] [blame] | 86 | if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 87 | return -EINVAL; |
| 88 | |
Jonathan Cameron | 550268ca | 2011-12-05 22:18:15 +0000 | [diff] [blame] | 89 | channel = find_first_bit(indio_dev->active_scan_mask, |
| 90 | indio_dev->masklength); |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 91 | |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 92 | ade7758_write_waveform_type(&indio_dev->dev, |
Georgiana Rodica Chelu | 89237e0 | 2016-09-19 18:41:56 +0300 | [diff] [blame] | 93 | indio_dev->channels[channel].address); |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 98 | static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = { |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 99 | .preenable = &ade7758_ring_preenable, |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 100 | .postenable = &iio_triggered_buffer_postenable, |
| 101 | .predisable = &iio_triggered_buffer_predisable, |
Lars-Peter Clausen | ee0312a | 2012-07-09 10:00:00 +0100 | [diff] [blame] | 102 | .validate_scan_mask = &iio_validate_scan_mask_onehot, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 105 | void ade7758_unconfigure_ring(struct iio_dev *indio_dev) |
| 106 | { |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 107 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
Jonathan Cameron | 94f3c7c | 2012-11-30 14:22:00 +0000 | [diff] [blame] | 108 | iio_kfifo_free(indio_dev->buffer); |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int ade7758_configure_ring(struct iio_dev *indio_dev) |
| 112 | { |
Michael Hennerich | a3f0237 | 2011-05-18 14:42:36 +0100 | [diff] [blame] | 113 | struct ade7758_state *st = iio_priv(indio_dev); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 114 | struct iio_buffer *buffer; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 115 | int ret = 0; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 116 | |
Karol Wrona | 7ab374a | 2014-12-19 18:39:24 +0100 | [diff] [blame] | 117 | buffer = iio_kfifo_allocate(); |
Taiane Coelho Ramos | 6fe9092 | 2015-03-13 16:22:44 -0300 | [diff] [blame] | 118 | if (!buffer) |
Tina Johnson | 9d9f5a2 | 2015-03-09 16:11:18 +0530 | [diff] [blame] | 119 | return -ENOMEM; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 120 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 121 | iio_device_attach_buffer(indio_dev, buffer); |
| 122 | |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 123 | indio_dev->setup_ops = &ade7758_ring_setup_ops; |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 124 | |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 125 | indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time, |
| 126 | &ade7758_trigger_handler, |
| 127 | 0, |
| 128 | indio_dev, |
| 129 | "ade7759_consumer%d", |
| 130 | indio_dev->id); |
Cristina Opriceana | 4529757 | 2015-03-31 13:01:18 +0300 | [diff] [blame] | 131 | if (!indio_dev->pollfunc) { |
Jonathan Cameron | 3f00ca4 | 2011-05-18 14:41:26 +0100 | [diff] [blame] | 132 | ret = -ENOMEM; |
Jonathan Cameron | 94f3c7c | 2012-11-30 14:22:00 +0000 | [diff] [blame] | 133 | goto error_iio_kfifo_free; |
Jonathan Cameron | 3f00ca4 | 2011-05-18 14:41:26 +0100 | [diff] [blame] | 134 | } |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 135 | |
Jonathan Cameron | ec3afa4 | 2011-09-21 11:15:54 +0100 | [diff] [blame] | 136 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
Jonathan Cameron | 3f00ca4 | 2011-05-18 14:41:26 +0100 | [diff] [blame] | 137 | |
Michael Hennerich | 8a27a02 | 2011-05-18 14:42:07 +0100 | [diff] [blame] | 138 | st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS); |
| 139 | st->tx_buf[1] = 0; |
| 140 | st->tx_buf[2] = 0; |
| 141 | st->tx_buf[3] = 0; |
| 142 | st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM); |
| 143 | st->tx_buf[5] = 0; |
| 144 | st->tx_buf[6] = 0; |
| 145 | st->tx_buf[7] = 0; |
| 146 | |
| 147 | /* build spi ring message */ |
| 148 | st->ring_xfer[0].tx_buf = &st->tx_buf[0]; |
| 149 | st->ring_xfer[0].len = 1; |
| 150 | st->ring_xfer[0].bits_per_word = 8; |
| 151 | st->ring_xfer[0].delay_usecs = 4; |
| 152 | st->ring_xfer[1].rx_buf = &st->rx_buf[1]; |
| 153 | st->ring_xfer[1].len = 3; |
| 154 | st->ring_xfer[1].bits_per_word = 8; |
| 155 | st->ring_xfer[1].cs_change = 1; |
| 156 | |
| 157 | st->ring_xfer[2].tx_buf = &st->tx_buf[4]; |
| 158 | st->ring_xfer[2].len = 1; |
| 159 | st->ring_xfer[2].bits_per_word = 8; |
| 160 | st->ring_xfer[2].delay_usecs = 1; |
| 161 | st->ring_xfer[3].rx_buf = &st->rx_buf[5]; |
| 162 | st->ring_xfer[3].len = 3; |
| 163 | st->ring_xfer[3].bits_per_word = 8; |
| 164 | |
| 165 | spi_message_init(&st->ring_msg); |
| 166 | spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg); |
| 167 | spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg); |
| 168 | spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg); |
| 169 | spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg); |
| 170 | |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 171 | return 0; |
| 172 | |
Jonathan Cameron | 94f3c7c | 2012-11-30 14:22:00 +0000 | [diff] [blame] | 173 | error_iio_kfifo_free: |
| 174 | iio_kfifo_free(indio_dev->buffer); |
Barry Song | 8210cfe | 2010-10-27 21:44:16 -0400 | [diff] [blame] | 175 | return ret; |
| 176 | } |