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 */
8 #include <linux/export.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/slab.h>
13 #include <asm/unaligned.h>
14
15 #include <linux/iio/iio.h>
16 #include <linux/iio/kfifo_buf.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include "ade7758.h"
19
20 /**
21 * ade7758_spi_read_burst() - read data registers
22 * @indio_dev: the IIO device
23 **/
ade7758_spi_read_burst(struct iio_dev * indio_dev)24 static int ade7758_spi_read_burst(struct iio_dev *indio_dev)
25 {
26 struct ade7758_state *st = iio_priv(indio_dev);
27 int ret;
28
29 ret = spi_sync(st->us, &st->ring_msg);
30 if (ret)
31 dev_err(&st->us->dev, "problem when reading WFORM value\n");
32
33 return ret;
34 }
35
ade7758_write_waveform_type(struct device * dev,unsigned int type)36 static int ade7758_write_waveform_type(struct device *dev, unsigned int type)
37 {
38 int ret;
39 u8 reg;
40
41 ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®);
42 if (ret)
43 goto out;
44
45 reg &= ~0x1F;
46 reg |= type & 0x1F;
47
48 ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg);
49 out:
50 return ret;
51 }
52
53 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is too device
54 * specific to be rolled into the core.
55 */
ade7758_trigger_handler(int irq,void * p)56 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
57 {
58 struct iio_poll_func *pf = p;
59 struct iio_dev *indio_dev = pf->indio_dev;
60 struct ade7758_state *st = iio_priv(indio_dev);
61 s64 dat64[2];
62 u32 *dat32 = (u32 *)dat64;
63
64 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
65 if (ade7758_spi_read_burst(indio_dev) >= 0)
66 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
67
68 iio_push_to_buffers_with_timestamp(indio_dev, dat64, pf->timestamp);
69
70 iio_trigger_notify_done(indio_dev->trig);
71
72 return IRQ_HANDLED;
73 }
74
75 /**
76 * ade7758_ring_preenable() setup the parameters of the ring before enabling
77 *
78 * The complex nature of the setting of the number of bytes per datum is due
79 * to this driver currently ensuring that the timestamp is stored at an 8
80 * byte boundary.
81 **/
ade7758_ring_preenable(struct iio_dev * indio_dev)82 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
83 {
84 unsigned int channel;
85
86 if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
87 return -EINVAL;
88
89 channel = find_first_bit(indio_dev->active_scan_mask,
90 indio_dev->masklength);
91
92 ade7758_write_waveform_type(&indio_dev->dev,
93 indio_dev->channels[channel].address);
94
95 return 0;
96 }
97
98 static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = {
99 .preenable = &ade7758_ring_preenable,
100 .postenable = &iio_triggered_buffer_postenable,
101 .predisable = &iio_triggered_buffer_predisable,
102 .validate_scan_mask = &iio_validate_scan_mask_onehot,
103 };
104
ade7758_unconfigure_ring(struct iio_dev * indio_dev)105 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
106 {
107 iio_dealloc_pollfunc(indio_dev->pollfunc);
108 iio_kfifo_free(indio_dev->buffer);
109 }
110
ade7758_configure_ring(struct iio_dev * indio_dev)111 int ade7758_configure_ring(struct iio_dev *indio_dev)
112 {
113 struct ade7758_state *st = iio_priv(indio_dev);
114 struct iio_buffer *buffer;
115 int ret = 0;
116
117 buffer = iio_kfifo_allocate();
118 if (!buffer)
119 return -ENOMEM;
120
121 iio_device_attach_buffer(indio_dev, buffer);
122
123 indio_dev->setup_ops = &ade7758_ring_setup_ops;
124
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);
131 if (!indio_dev->pollfunc) {
132 ret = -ENOMEM;
133 goto error_iio_kfifo_free;
134 }
135
136 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
137
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
171 return 0;
172
173 error_iio_kfifo_free:
174 iio_kfifo_free(indio_dev->buffer);
175 return ret;
176 }
177