• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "../iio.h"
16 #include "../ring_sw.h"
17 #include "../trigger_consumer.h"
18 #include "ade7758.h"
19 
20 /**
21  * ade7758_spi_read_burst() - read data registers
22  * @dev: device associated with child of actual device (iio_dev or iio_trig)
23  **/
ade7758_spi_read_burst(struct device * dev)24 static int ade7758_spi_read_burst(struct device *dev)
25 {
26 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
27 	struct ade7758_state *st = iio_priv(indio_dev);
28 	int ret;
29 
30 	ret = spi_sync(st->us, &st->ring_msg);
31 	if (ret)
32 		dev_err(&st->us->dev, "problem when reading WFORM value\n");
33 
34 	return ret;
35 }
36 
ade7758_write_waveform_type(struct device * dev,unsigned type)37 static int ade7758_write_waveform_type(struct device *dev, unsigned type)
38 {
39 	int ret;
40 	u8 reg;
41 
42 	ret = ade7758_spi_read_reg_8(dev,
43 			ADE7758_WAVMODE,
44 			&reg);
45 	if (ret)
46 		goto out;
47 
48 	reg &= ~0x1F;
49 	reg |= type & 0x1F;
50 
51 	ret = ade7758_spi_write_reg_8(dev,
52 			ADE7758_WAVMODE,
53 			reg);
54 out:
55 	return ret;
56 }
57 
58 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
59  * specific to be rolled into the core.
60  */
ade7758_trigger_handler(int irq,void * p)61 static irqreturn_t ade7758_trigger_handler(int irq, void *p)
62 {
63 	struct iio_poll_func *pf = p;
64 	struct iio_dev *indio_dev = pf->indio_dev;
65 	struct iio_buffer *ring = indio_dev->buffer;
66 	struct ade7758_state *st = iio_priv(indio_dev);
67 	s64 dat64[2];
68 	u32 *dat32 = (u32 *)dat64;
69 
70 	if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
71 		if (ade7758_spi_read_burst(&indio_dev->dev) >= 0)
72 			*dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
73 
74 	/* Guaranteed to be aligned with 8 byte boundary */
75 	if (ring->scan_timestamp)
76 		dat64[1] = pf->timestamp;
77 
78 	ring->access->store_to(ring, (u8 *)dat64, pf->timestamp);
79 
80 	iio_trigger_notify_done(indio_dev->trig);
81 
82 	return IRQ_HANDLED;
83 }
84 
85 /**
86  * ade7758_ring_preenable() setup the parameters of the ring before enabling
87  *
88  * The complex nature of the setting of the number of bytes per datum is due
89  * to this driver currently ensuring that the timestamp is stored at an 8
90  * byte boundary.
91  **/
ade7758_ring_preenable(struct iio_dev * indio_dev)92 static int ade7758_ring_preenable(struct iio_dev *indio_dev)
93 {
94 	struct ade7758_state *st = iio_priv(indio_dev);
95 	struct iio_buffer *ring = indio_dev->buffer;
96 	size_t d_size;
97 	unsigned channel;
98 
99 	if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
100 		return -EINVAL;
101 
102 	channel = find_first_bit(indio_dev->active_scan_mask,
103 				 indio_dev->masklength);
104 
105 	d_size = st->ade7758_ring_channels[channel].scan_type.storagebits / 8;
106 
107 	if (ring->scan_timestamp) {
108 		d_size += sizeof(s64);
109 
110 		if (d_size % sizeof(s64))
111 			d_size += sizeof(s64) - (d_size % sizeof(s64));
112 	}
113 
114 	if (indio_dev->buffer->access->set_bytes_per_datum)
115 		indio_dev->buffer->access->
116 			set_bytes_per_datum(indio_dev->buffer, d_size);
117 
118 	ade7758_write_waveform_type(&indio_dev->dev,
119 		st->ade7758_ring_channels[channel].address);
120 
121 	return 0;
122 }
123 
124 static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = {
125 	.preenable = &ade7758_ring_preenable,
126 	.postenable = &iio_triggered_buffer_postenable,
127 	.predisable = &iio_triggered_buffer_predisable,
128 };
129 
ade7758_unconfigure_ring(struct iio_dev * indio_dev)130 void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
131 {
132 	iio_dealloc_pollfunc(indio_dev->pollfunc);
133 	iio_sw_rb_free(indio_dev->buffer);
134 }
135 
ade7758_configure_ring(struct iio_dev * indio_dev)136 int ade7758_configure_ring(struct iio_dev *indio_dev)
137 {
138 	struct ade7758_state *st = iio_priv(indio_dev);
139 	int ret = 0;
140 
141 	indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
142 	if (!indio_dev->buffer) {
143 		ret = -ENOMEM;
144 		return ret;
145 	}
146 
147 	indio_dev->setup_ops = &ade7758_ring_setup_ops;
148 
149 	indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
150 						 &ade7758_trigger_handler,
151 						 0,
152 						 indio_dev,
153 						 "ade7759_consumer%d",
154 						 indio_dev->id);
155 	if (indio_dev->pollfunc == NULL) {
156 		ret = -ENOMEM;
157 		goto error_iio_sw_rb_free;
158 	}
159 
160 	indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
161 
162 	st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
163 	st->tx_buf[1] = 0;
164 	st->tx_buf[2] = 0;
165 	st->tx_buf[3] = 0;
166 	st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
167 	st->tx_buf[5] = 0;
168 	st->tx_buf[6] = 0;
169 	st->tx_buf[7] = 0;
170 
171 	/* build spi ring message */
172 	st->ring_xfer[0].tx_buf = &st->tx_buf[0];
173 	st->ring_xfer[0].len = 1;
174 	st->ring_xfer[0].bits_per_word = 8;
175 	st->ring_xfer[0].delay_usecs = 4;
176 	st->ring_xfer[1].rx_buf = &st->rx_buf[1];
177 	st->ring_xfer[1].len = 3;
178 	st->ring_xfer[1].bits_per_word = 8;
179 	st->ring_xfer[1].cs_change = 1;
180 
181 	st->ring_xfer[2].tx_buf = &st->tx_buf[4];
182 	st->ring_xfer[2].len = 1;
183 	st->ring_xfer[2].bits_per_word = 8;
184 	st->ring_xfer[2].delay_usecs = 1;
185 	st->ring_xfer[3].rx_buf = &st->rx_buf[5];
186 	st->ring_xfer[3].len = 3;
187 	st->ring_xfer[3].bits_per_word = 8;
188 
189 	spi_message_init(&st->ring_msg);
190 	spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
191 	spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
192 	spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
193 	spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
194 
195 	return 0;
196 
197 error_iio_sw_rb_free:
198 	iio_sw_rb_free(indio_dev->buffer);
199 	return ret;
200 }
201 
ade7758_uninitialize_ring(struct iio_dev * indio_dev)202 void ade7758_uninitialize_ring(struct iio_dev *indio_dev)
203 {
204 	iio_buffer_unregister(indio_dev);
205 }
206