blob: 9a24e0226f8baba43153801fae7df9292b266a1d [file] [log] [blame]
Michael Hennerich8a27a022011-05-18 14:42:07 +01001/*
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 */
Paul Gortmaker8e336a72011-07-10 13:09:12 -04008#include <linux/export.h>
Barry Song8210cfe2010-10-27 21:44:16 -04009#include <linux/interrupt.h>
Barry Song8210cfe2010-10-27 21:44:16 -040010#include <linux/kernel.h>
11#include <linux/spi/spi.h>
12#include <linux/slab.h>
Michael Hennerich8a27a022011-05-18 14:42:07 +010013#include <asm/unaligned.h>
Barry Song8210cfe2010-10-27 21:44:16 -040014
Jonathan Cameron06458e22012-04-25 15:54:58 +010015#include <linux/iio/iio.h>
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +000016#include <linux/iio/kfifo_buf.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010017#include <linux/iio/trigger_consumer.h>
Barry Song8210cfe2010-10-27 21:44:16 -040018#include "ade7758.h"
19
20/**
Michael Hennerich8a27a022011-05-18 14:42:07 +010021 * ade7758_spi_read_burst() - read data registers
Lars-Peter Clausen2a0b8712012-05-12 15:39:40 +020022 * @indio_dev: the IIO device
Barry Song8210cfe2010-10-27 21:44:16 -040023 **/
Lars-Peter Clausen2a0b8712012-05-12 15:39:40 +020024static int ade7758_spi_read_burst(struct iio_dev *indio_dev)
Barry Song8210cfe2010-10-27 21:44:16 -040025{
Michael Hennericha3f02372011-05-18 14:42:36 +010026 struct ade7758_state *st = iio_priv(indio_dev);
Barry Song8210cfe2010-10-27 21:44:16 -040027 int ret;
28
Michael Hennerich8a27a022011-05-18 14:42:07 +010029 ret = spi_sync(st->us, &st->ring_msg);
Barry Song8210cfe2010-10-27 21:44:16 -040030 if (ret)
31 dev_err(&st->us->dev, "problem when reading WFORM value\n");
32
Michael Hennerich8a27a022011-05-18 14:42:07 +010033 return ret;
34}
Barry Song8210cfe2010-10-27 21:44:16 -040035
Michael Hennerich8a27a022011-05-18 14:42:07 +010036static int ade7758_write_waveform_type(struct device *dev, unsigned type)
37{
38 int ret;
39 u8 reg;
40
41 ret = ade7758_spi_read_reg_8(dev,
42 ADE7758_WAVMODE,
43 &reg);
44 if (ret)
45 goto out;
46
47 reg &= ~0x1F;
48 reg |= type & 0x1F;
49
50 ret = ade7758_spi_write_reg_8(dev,
51 ADE7758_WAVMODE,
52 reg);
53out:
Barry Song8210cfe2010-10-27 21:44:16 -040054 return ret;
55}
56
Peter Meerwald7d241b22013-07-07 21:24:00 +010057/* Whilst this makes a lot of calls to iio_sw_ring functions - it is too device
Barry Song8210cfe2010-10-27 21:44:16 -040058 * specific to be rolled into the core.
59 */
Jonathan Cameron3f00ca42011-05-18 14:41:26 +010060static irqreturn_t ade7758_trigger_handler(int irq, void *p)
Barry Song8210cfe2010-10-27 21:44:16 -040061{
Jonathan Cameron3f00ca42011-05-18 14:41:26 +010062 struct iio_poll_func *pf = p;
Jonathan Camerone65bc6a2011-08-24 17:28:36 +010063 struct iio_dev *indio_dev = pf->indio_dev;
Michael Hennericha3f02372011-05-18 14:42:36 +010064 struct ade7758_state *st = iio_priv(indio_dev);
Michael Hennerich8a27a022011-05-18 14:42:07 +010065 s64 dat64[2];
66 u32 *dat32 = (u32 *)dat64;
Barry Song8210cfe2010-10-27 21:44:16 -040067
Jonathan Cameron550268ca2011-12-05 22:18:15 +000068 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
Lars-Peter Clausen2a0b8712012-05-12 15:39:40 +020069 if (ade7758_spi_read_burst(indio_dev) >= 0)
Michael Hennerich8a27a022011-05-18 14:42:07 +010070 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
Barry Song8210cfe2010-10-27 21:44:16 -040071
Lars-Peter Clausen9d6c3982013-09-19 14:00:00 +010072 iio_push_to_buffers_with_timestamp(indio_dev, dat64, pf->timestamp);
Barry Song8210cfe2010-10-27 21:44:16 -040073
Michael Hennericha3f02372011-05-18 14:42:36 +010074 iio_trigger_notify_done(indio_dev->trig);
Barry Song8210cfe2010-10-27 21:44:16 -040075
Jonathan Cameron3f00ca42011-05-18 14:41:26 +010076 return IRQ_HANDLED;
Barry Song8210cfe2010-10-27 21:44:16 -040077}
78
Michael Hennerich8a27a022011-05-18 14:42:07 +010079/**
80 * ade7758_ring_preenable() setup the parameters of the ring before enabling
81 *
Justin P. Mattock4abf6f82012-02-29 22:00:38 -080082 * The complex nature of the setting of the number of bytes per datum is due
Michael Hennerich8a27a022011-05-18 14:42:07 +010083 * to this driver currently ensuring that the timestamp is stored at an 8
84 * byte boundary.
85 **/
86static int ade7758_ring_preenable(struct iio_dev *indio_dev)
87{
Michael Hennerich8a27a022011-05-18 14:42:07 +010088 unsigned channel;
89
Lars-Peter Clausen79fa64e2014-11-04 18:03:15 +010090 if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
Michael Hennerich8a27a022011-05-18 14:42:07 +010091 return -EINVAL;
92
Jonathan Cameron550268ca2011-12-05 22:18:15 +000093 channel = find_first_bit(indio_dev->active_scan_mask,
94 indio_dev->masklength);
Michael Hennerich8a27a022011-05-18 14:42:07 +010095
Michael Hennerich8a27a022011-05-18 14:42:07 +010096 ade7758_write_waveform_type(&indio_dev->dev,
Lars-Peter Clausene1055472014-11-04 18:03:14 +010097 indio_dev->channels[channel].address);
Michael Hennerich8a27a022011-05-18 14:42:07 +010098
99 return 0;
100}
101
Jonathan Cameron14555b12011-09-21 11:15:57 +0100102static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100103 .preenable = &ade7758_ring_preenable,
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100104 .postenable = &iio_triggered_buffer_postenable,
105 .predisable = &iio_triggered_buffer_predisable,
Lars-Peter Clausenee0312a2012-07-09 10:00:00 +0100106 .validate_scan_mask = &iio_validate_scan_mask_onehot,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100107};
108
Barry Song8210cfe2010-10-27 21:44:16 -0400109void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
110{
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100111 iio_dealloc_pollfunc(indio_dev->pollfunc);
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +0000112 iio_kfifo_free(indio_dev->buffer);
Barry Song8210cfe2010-10-27 21:44:16 -0400113}
114
115int ade7758_configure_ring(struct iio_dev *indio_dev)
116{
Michael Hennericha3f02372011-05-18 14:42:36 +0100117 struct ade7758_state *st = iio_priv(indio_dev);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100118 struct iio_buffer *buffer;
Barry Song8210cfe2010-10-27 21:44:16 -0400119 int ret = 0;
Barry Song8210cfe2010-10-27 21:44:16 -0400120
Karol Wrona7ab374a2014-12-19 18:39:24 +0100121 buffer = iio_kfifo_allocate();
Taiane Coelho Ramos6fe90922015-03-13 16:22:44 -0300122 if (!buffer)
Tina Johnson9d9f5a22015-03-09 16:11:18 +0530123 return -ENOMEM;
Barry Song8210cfe2010-10-27 21:44:16 -0400124
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100125 iio_device_attach_buffer(indio_dev, buffer);
126
Jonathan Cameron16122442011-12-05 22:18:14 +0000127 indio_dev->setup_ops = &ade7758_ring_setup_ops;
Barry Song8210cfe2010-10-27 21:44:16 -0400128
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100129 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
130 &ade7758_trigger_handler,
131 0,
132 indio_dev,
133 "ade7759_consumer%d",
134 indio_dev->id);
Cristina Opriceana45297572015-03-31 13:01:18 +0300135 if (!indio_dev->pollfunc) {
Jonathan Cameron3f00ca42011-05-18 14:41:26 +0100136 ret = -ENOMEM;
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +0000137 goto error_iio_kfifo_free;
Jonathan Cameron3f00ca42011-05-18 14:41:26 +0100138 }
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100139
Jonathan Cameronec3afa42011-09-21 11:15:54 +0100140 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
Jonathan Cameron3f00ca42011-05-18 14:41:26 +0100141
Michael Hennerich8a27a022011-05-18 14:42:07 +0100142 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
143 st->tx_buf[1] = 0;
144 st->tx_buf[2] = 0;
145 st->tx_buf[3] = 0;
146 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
147 st->tx_buf[5] = 0;
148 st->tx_buf[6] = 0;
149 st->tx_buf[7] = 0;
150
151 /* build spi ring message */
152 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
153 st->ring_xfer[0].len = 1;
154 st->ring_xfer[0].bits_per_word = 8;
155 st->ring_xfer[0].delay_usecs = 4;
156 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
157 st->ring_xfer[1].len = 3;
158 st->ring_xfer[1].bits_per_word = 8;
159 st->ring_xfer[1].cs_change = 1;
160
161 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
162 st->ring_xfer[2].len = 1;
163 st->ring_xfer[2].bits_per_word = 8;
164 st->ring_xfer[2].delay_usecs = 1;
165 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
166 st->ring_xfer[3].len = 3;
167 st->ring_xfer[3].bits_per_word = 8;
168
169 spi_message_init(&st->ring_msg);
170 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
171 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
172 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
173 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
174
Barry Song8210cfe2010-10-27 21:44:16 -0400175 return 0;
176
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +0000177error_iio_kfifo_free:
178 iio_kfifo_free(indio_dev->buffer);
Barry Song8210cfe2010-10-27 21:44:16 -0400179 return ret;
180}