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/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/workqueue.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <asm/unaligned.h>
23 #include "../ring_sw.h"
24 #include "../accel/accel.h"
25 #include "../trigger.h"
29 * ade7758_spi_read_burst() - read data registers
30 * @dev: device associated with child of actual device (iio_dev or iio_trig)
32 static int ade7758_spi_read_burst(struct device
*dev
)
34 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
35 struct ade7758_state
*st
= iio_priv(indio_dev
);
38 ret
= spi_sync(st
->us
, &st
->ring_msg
);
40 dev_err(&st
->us
->dev
, "problem when reading WFORM value\n");
45 static int ade7758_write_waveform_type(struct device
*dev
, unsigned type
)
50 ret
= ade7758_spi_read_reg_8(dev
,
59 ret
= ade7758_spi_write_reg_8(dev
,
66 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
67 * specific to be rolled into the core.
69 static irqreturn_t
ade7758_trigger_handler(int irq
, void *p
)
71 struct iio_poll_func
*pf
= p
;
72 struct iio_dev
*indio_dev
= pf
->private_data
;
73 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
74 struct ade7758_state
*st
= iio_priv(indio_dev
);
76 u32
*dat32
= (u32
*)dat64
;
79 if (ade7758_spi_read_burst(&indio_dev
->dev
) >= 0)
80 *dat32
= get_unaligned_be32(&st
->rx_buf
[5]) & 0xFFFFFF;
82 /* Guaranteed to be aligned with 8 byte boundary */
83 if (ring
->scan_timestamp
)
84 dat64
[1] = pf
->timestamp
;
86 ring
->access
->store_to(ring
, (u8
*)dat64
, pf
->timestamp
);
88 iio_trigger_notify_done(indio_dev
->trig
);
94 * ade7758_ring_preenable() setup the parameters of the ring before enabling
96 * The complex nature of the setting of the nuber of bytes per datum is due
97 * to this driver currently ensuring that the timestamp is stored at an 8
100 static int ade7758_ring_preenable(struct iio_dev
*indio_dev
)
102 struct ade7758_state
*st
= iio_priv(indio_dev
);
103 struct iio_ring_buffer
*ring
= indio_dev
->ring
;
107 if (!ring
->scan_count
)
110 channel
= __ffs(ring
->scan_mask
);
112 d_size
= st
->ade7758_ring_channels
[channel
].scan_type
.storagebits
/ 8;
114 if (ring
->scan_timestamp
) {
115 d_size
+= sizeof(s64
);
117 if (d_size
% sizeof(s64
))
118 d_size
+= sizeof(s64
) - (d_size
% sizeof(s64
));
121 if (indio_dev
->ring
->access
->set_bytes_per_datum
)
122 indio_dev
->ring
->access
->set_bytes_per_datum(indio_dev
->ring
,
125 ade7758_write_waveform_type(&indio_dev
->dev
,
126 st
->ade7758_ring_channels
[channel
].address
);
131 static const struct iio_ring_setup_ops ade7758_ring_setup_ops
= {
132 .preenable
= &ade7758_ring_preenable
,
133 .postenable
= &iio_triggered_ring_postenable
,
134 .predisable
= &iio_triggered_ring_predisable
,
137 void ade7758_unconfigure_ring(struct iio_dev
*indio_dev
)
139 /* ensure that the trigger has been detached */
140 if (indio_dev
->trig
) {
141 iio_put_trigger(indio_dev
->trig
);
142 iio_trigger_dettach_poll_func(indio_dev
->trig
,
143 indio_dev
->pollfunc
);
145 iio_dealloc_pollfunc(indio_dev
->pollfunc
);
146 iio_sw_rb_free(indio_dev
->ring
);
149 int ade7758_configure_ring(struct iio_dev
*indio_dev
)
151 struct ade7758_state
*st
= iio_priv(indio_dev
);
154 indio_dev
->ring
= iio_sw_rb_allocate(indio_dev
);
155 if (!indio_dev
->ring
) {
160 /* Effectively select the ring buffer implementation */
161 indio_dev
->ring
->access
= &ring_sw_access_funcs
;
162 indio_dev
->ring
->setup_ops
= &ade7758_ring_setup_ops
;
163 indio_dev
->ring
->owner
= THIS_MODULE
;
165 indio_dev
->pollfunc
= iio_alloc_pollfunc(&iio_pollfunc_store_time
,
166 &ade7758_trigger_handler
,
169 "ade7759_consumer%d",
171 if (indio_dev
->pollfunc
== NULL
) {
173 goto error_iio_sw_rb_free
;
176 indio_dev
->modes
|= INDIO_RING_TRIGGERED
;
178 st
->tx_buf
[0] = ADE7758_READ_REG(ADE7758_RSTATUS
);
182 st
->tx_buf
[4] = ADE7758_READ_REG(ADE7758_WFORM
);
187 /* build spi ring message */
188 st
->ring_xfer
[0].tx_buf
= &st
->tx_buf
[0];
189 st
->ring_xfer
[0].len
= 1;
190 st
->ring_xfer
[0].bits_per_word
= 8;
191 st
->ring_xfer
[0].delay_usecs
= 4;
192 st
->ring_xfer
[1].rx_buf
= &st
->rx_buf
[1];
193 st
->ring_xfer
[1].len
= 3;
194 st
->ring_xfer
[1].bits_per_word
= 8;
195 st
->ring_xfer
[1].cs_change
= 1;
197 st
->ring_xfer
[2].tx_buf
= &st
->tx_buf
[4];
198 st
->ring_xfer
[2].len
= 1;
199 st
->ring_xfer
[2].bits_per_word
= 8;
200 st
->ring_xfer
[2].delay_usecs
= 1;
201 st
->ring_xfer
[3].rx_buf
= &st
->rx_buf
[5];
202 st
->ring_xfer
[3].len
= 3;
203 st
->ring_xfer
[3].bits_per_word
= 8;
205 spi_message_init(&st
->ring_msg
);
206 spi_message_add_tail(&st
->ring_xfer
[0], &st
->ring_msg
);
207 spi_message_add_tail(&st
->ring_xfer
[1], &st
->ring_msg
);
208 spi_message_add_tail(&st
->ring_xfer
[2], &st
->ring_msg
);
209 spi_message_add_tail(&st
->ring_xfer
[3], &st
->ring_msg
);
213 error_iio_sw_rb_free
:
214 iio_sw_rb_free(indio_dev
->ring
);
218 void ade7758_uninitialize_ring(struct iio_ring_buffer
*ring
)
220 iio_ring_buffer_unregister(ring
);