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 <linux/iio/kfifo_buf.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 too 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 ade7758_state
*st
= iio_priv(indio_dev
);
66 u32
*dat32
= (u32
*)dat64
;
68 if (!bitmap_empty(indio_dev
->active_scan_mask
, indio_dev
->masklength
))
69 if (ade7758_spi_read_burst(indio_dev
) >= 0)
70 *dat32
= get_unaligned_be32(&st
->rx_buf
[5]) & 0xFFFFFF;
72 iio_push_to_buffers_with_timestamp(indio_dev
, dat64
, pf
->timestamp
);
74 iio_trigger_notify_done(indio_dev
->trig
);
80 * ade7758_ring_preenable() setup the parameters of the ring before enabling
82 * The complex nature of the setting of the number of bytes per datum is due
83 * to this driver currently ensuring that the timestamp is stored at an 8
86 static int ade7758_ring_preenable(struct iio_dev
*indio_dev
)
88 struct ade7758_state
*st
= iio_priv(indio_dev
);
92 if (!bitmap_empty(indio_dev
->active_scan_mask
, indio_dev
->masklength
))
95 ret
= iio_sw_buffer_preenable(indio_dev
);
99 channel
= find_first_bit(indio_dev
->active_scan_mask
,
100 indio_dev
->masklength
);
102 ade7758_write_waveform_type(&indio_dev
->dev
,
103 st
->ade7758_ring_channels
[channel
].address
);
108 static const struct iio_buffer_setup_ops ade7758_ring_setup_ops
= {
109 .preenable
= &ade7758_ring_preenable
,
110 .postenable
= &iio_triggered_buffer_postenable
,
111 .predisable
= &iio_triggered_buffer_predisable
,
112 .validate_scan_mask
= &iio_validate_scan_mask_onehot
,
115 void ade7758_unconfigure_ring(struct iio_dev
*indio_dev
)
117 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
118 iio_kfifo_free(indio_dev
->buffer
);
121 int ade7758_configure_ring(struct iio_dev
*indio_dev
)
123 struct ade7758_state
*st
= iio_priv(indio_dev
);
126 indio_dev
->buffer
= iio_kfifo_allocate(indio_dev
);
127 if (!indio_dev
->buffer
) {
132 indio_dev
->setup_ops
= &ade7758_ring_setup_ops
;
134 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
135 &ade7758_trigger_handler
,
138 "ade7759_consumer%d",
140 if (indio_dev
->pollfunc
== NULL
) {
142 goto error_iio_kfifo_free
;
145 indio_dev
->modes
|= INDIO_BUFFER_TRIGGERED
;
147 st
->tx_buf
[0] = ADE7758_READ_REG(ADE7758_RSTATUS
);
151 st
->tx_buf
[4] = ADE7758_READ_REG(ADE7758_WFORM
);
156 /* build spi ring message */
157 st
->ring_xfer
[0].tx_buf
= &st
->tx_buf
[0];
158 st
->ring_xfer
[0].len
= 1;
159 st
->ring_xfer
[0].bits_per_word
= 8;
160 st
->ring_xfer
[0].delay_usecs
= 4;
161 st
->ring_xfer
[1].rx_buf
= &st
->rx_buf
[1];
162 st
->ring_xfer
[1].len
= 3;
163 st
->ring_xfer
[1].bits_per_word
= 8;
164 st
->ring_xfer
[1].cs_change
= 1;
166 st
->ring_xfer
[2].tx_buf
= &st
->tx_buf
[4];
167 st
->ring_xfer
[2].len
= 1;
168 st
->ring_xfer
[2].bits_per_word
= 8;
169 st
->ring_xfer
[2].delay_usecs
= 1;
170 st
->ring_xfer
[3].rx_buf
= &st
->rx_buf
[5];
171 st
->ring_xfer
[3].len
= 3;
172 st
->ring_xfer
[3].bits_per_word
= 8;
174 spi_message_init(&st
->ring_msg
);
175 spi_message_add_tail(&st
->ring_xfer
[0], &st
->ring_msg
);
176 spi_message_add_tail(&st
->ring_xfer
[1], &st
->ring_msg
);
177 spi_message_add_tail(&st
->ring_xfer
[2], &st
->ring_msg
);
178 spi_message_add_tail(&st
->ring_xfer
[3], &st
->ring_msg
);
182 error_iio_kfifo_free
:
183 iio_kfifo_free(indio_dev
->buffer
);
187 void ade7758_uninitialize_ring(struct iio_dev
*indio_dev
)
189 iio_buffer_unregister(indio_dev
);