2 * ADE7758 Poly Phase Multifunction Energy Metering IC driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
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>
15 #include <linux/iio/iio.h>
16 #include "../ring_sw.h"
17 #include <linux/iio/trigger_consumer.h>
21 * ade7758_spi_read_burst() - read data registers
22 * @indio_dev: the IIO device
24 static int ade7758_spi_read_burst(struct iio_dev
*indio_dev
)
26 struct ade7758_state
*st
= iio_priv(indio_dev
);
29 ret
= spi_sync(st
->us
, &st
->ring_msg
);
31 dev_err(&st
->us
->dev
, "problem when reading WFORM value\n");
36 static int ade7758_write_waveform_type(struct device
*dev
, unsigned type
)
41 ret
= ade7758_spi_read_reg_8(dev
,
50 ret
= ade7758_spi_write_reg_8(dev
,
57 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
58 * specific to be rolled into the core.
60 static irqreturn_t
ade7758_trigger_handler(int irq
, void *p
)
62 struct iio_poll_func
*pf
= p
;
63 struct iio_dev
*indio_dev
= pf
->indio_dev
;
64 struct iio_buffer
*ring
= indio_dev
->buffer
;
65 struct ade7758_state
*st
= iio_priv(indio_dev
);
67 u32
*dat32
= (u32
*)dat64
;
69 if (!bitmap_empty(indio_dev
->active_scan_mask
, indio_dev
->masklength
))
70 if (ade7758_spi_read_burst(indio_dev
) >= 0)
71 *dat32
= get_unaligned_be32(&st
->rx_buf
[5]) & 0xFFFFFF;
73 /* Guaranteed to be aligned with 8 byte boundary */
74 if (indio_dev
->scan_timestamp
)
75 dat64
[1] = pf
->timestamp
;
77 ring
->access
->store_to(ring
, (u8
*)dat64
, pf
->timestamp
);
79 iio_trigger_notify_done(indio_dev
->trig
);
85 * ade7758_ring_preenable() setup the parameters of the ring before enabling
87 * The complex nature of the setting of the number of bytes per datum is due
88 * to this driver currently ensuring that the timestamp is stored at an 8
91 static int ade7758_ring_preenable(struct iio_dev
*indio_dev
)
93 struct ade7758_state
*st
= iio_priv(indio_dev
);
97 if (!bitmap_empty(indio_dev
->active_scan_mask
, indio_dev
->masklength
))
100 ret
= iio_sw_buffer_preenable(indio_dev
);
104 channel
= find_first_bit(indio_dev
->active_scan_mask
,
105 indio_dev
->masklength
);
107 ade7758_write_waveform_type(&indio_dev
->dev
,
108 st
->ade7758_ring_channels
[channel
].address
);
113 static const struct iio_buffer_setup_ops ade7758_ring_setup_ops
= {
114 .preenable
= &ade7758_ring_preenable
,
115 .postenable
= &iio_triggered_buffer_postenable
,
116 .predisable
= &iio_triggered_buffer_predisable
,
117 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
120 void ade7758_unconfigure_ring(struct iio_dev
*indio_dev
)
122 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
123 iio_sw_rb_free(indio_dev
->buffer
);
126 int ade7758_configure_ring(struct iio_dev
*indio_dev
)
128 struct ade7758_state
*st
= iio_priv(indio_dev
);
131 indio_dev
->buffer
= iio_sw_rb_allocate(indio_dev
);
132 if (!indio_dev
->buffer
) {
137 indio_dev
->setup_ops
= &ade7758_ring_setup_ops
;
139 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
140 &ade7758_trigger_handler
,
143 "ade7759_consumer%d",
145 if (indio_dev
->pollfunc
== NULL
) {
147 goto error_iio_sw_rb_free
;
150 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
152 st
->tx_buf
[0] = ADE7758_READ_REG(ADE7758_RSTATUS
);
156 st
->tx_buf
[4] = ADE7758_READ_REG(ADE7758_WFORM
);
161 /* build spi ring message */
162 st
->ring_xfer
[0].tx_buf
= &st
->tx_buf
[0];
163 st
->ring_xfer
[0].len
= 1;
164 st
->ring_xfer
[0].bits_per_word
= 8;
165 st
->ring_xfer
[0].delay_usecs
= 4;
166 st
->ring_xfer
[1].rx_buf
= &st
->rx_buf
[1];
167 st
->ring_xfer
[1].len
= 3;
168 st
->ring_xfer
[1].bits_per_word
= 8;
169 st
->ring_xfer
[1].cs_change
= 1;
171 st
->ring_xfer
[2].tx_buf
= &st
->tx_buf
[4];
172 st
->ring_xfer
[2].len
= 1;
173 st
->ring_xfer
[2].bits_per_word
= 8;
174 st
->ring_xfer
[2].delay_usecs
= 1;
175 st
->ring_xfer
[3].rx_buf
= &st
->rx_buf
[5];
176 st
->ring_xfer
[3].len
= 3;
177 st
->ring_xfer
[3].bits_per_word
= 8;
179 spi_message_init(&st
->ring_msg
);
180 spi_message_add_tail(&st
->ring_xfer
[0], &st
->ring_msg
);
181 spi_message_add_tail(&st
->ring_xfer
[1], &st
->ring_msg
);
182 spi_message_add_tail(&st
->ring_xfer
[2], &st
->ring_msg
);
183 spi_message_add_tail(&st
->ring_xfer
[3], &st
->ring_msg
);
187 error_iio_sw_rb_free
:
188 iio_sw_rb_free(indio_dev
->buffer
);
192 void ade7758_uninitialize_ring(struct iio_dev
*indio_dev
)
194 iio_buffer_unregister(indio_dev
);