blob: 57c213dfadcc702830723b3f2fd4f812a566fea0 [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
Alison Schofield51fadb92016-03-26 12:50:24 -070036static int ade7758_write_waveform_type(struct device *dev, unsigned int type)
Michael Hennerich8a27a022011-05-18 14:42:07 +010037{
38 int ret;
39 u8 reg;
40
Georgiana Rodica Chelu89237e02016-09-19 18:41:56 +030041 ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &reg);
Michael Hennerich8a27a022011-05-18 14:42:07 +010042 if (ret)
43 goto out;
44
45 reg &= ~0x1F;
46 reg |= type & 0x1F;
47
Georgiana Rodica Chelu89237e02016-09-19 18:41:56 +030048 ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg);
Michael Hennerich8a27a022011-05-18 14:42:07 +010049out:
Barry Song8210cfe2010-10-27 21:44:16 -040050 return ret;
51}
52
Peter Meerwald7d241b22013-07-07 21:24:00 +010053/* Whilst this makes a lot of calls to iio_sw_ring functions - it is too device
Barry Song8210cfe2010-10-27 21:44:16 -040054 * specific to be rolled into the core.
55 */
Jonathan Cameron3f00ca42011-05-18 14:41:26 +010056static irqreturn_t ade7758_trigger_handler(int irq, void *p)
Barry Song8210cfe2010-10-27 21:44:16 -040057{
Jonathan Cameron3f00ca42011-05-18 14:41:26 +010058 struct iio_poll_func *pf = p;
Jonathan Camerone65bc6a2011-08-24 17:28:36 +010059 struct iio_dev *indio_dev = pf->indio_dev;
Michael Hennericha3f02372011-05-18 14:42:36 +010060 struct ade7758_state *st = iio_priv(indio_dev);
Michael Hennerich8a27a022011-05-18 14:42:07 +010061 s64 dat64[2];
62 u32 *dat32 = (u32 *)dat64;
Barry Song8210cfe2010-10-27 21:44:16 -040063
Jonathan Cameron550268ca2011-12-05 22:18:15 +000064 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
Lars-Peter Clausen2a0b8712012-05-12 15:39:40 +020065 if (ade7758_spi_read_burst(indio_dev) >= 0)
Michael Hennerich8a27a022011-05-18 14:42:07 +010066 *dat32 = get_unaligned_be32(&st->rx_buf[5]) & 0xFFFFFF;
Barry Song8210cfe2010-10-27 21:44:16 -040067
Lars-Peter Clausen9d6c3982013-09-19 14:00:00 +010068 iio_push_to_buffers_with_timestamp(indio_dev, dat64, pf->timestamp);
Barry Song8210cfe2010-10-27 21:44:16 -040069
Michael Hennericha3f02372011-05-18 14:42:36 +010070 iio_trigger_notify_done(indio_dev->trig);
Barry Song8210cfe2010-10-27 21:44:16 -040071
Jonathan Cameron3f00ca42011-05-18 14:41:26 +010072 return IRQ_HANDLED;
Barry Song8210cfe2010-10-27 21:44:16 -040073}
74
Michael Hennerich8a27a022011-05-18 14:42:07 +010075/**
76 * ade7758_ring_preenable() setup the parameters of the ring before enabling
77 *
Justin P. Mattock4abf6f82012-02-29 22:00:38 -080078 * The complex nature of the setting of the number of bytes per datum is due
Michael Hennerich8a27a022011-05-18 14:42:07 +010079 * to this driver currently ensuring that the timestamp is stored at an 8
80 * byte boundary.
81 **/
82static int ade7758_ring_preenable(struct iio_dev *indio_dev)
83{
Alison Schofield51fadb92016-03-26 12:50:24 -070084 unsigned int channel;
Michael Hennerich8a27a022011-05-18 14:42:07 +010085
Lars-Peter Clausen79fa64e2014-11-04 18:03:15 +010086 if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
Michael Hennerich8a27a022011-05-18 14:42:07 +010087 return -EINVAL;
88
Jonathan Cameron550268ca2011-12-05 22:18:15 +000089 channel = find_first_bit(indio_dev->active_scan_mask,
90 indio_dev->masklength);
Michael Hennerich8a27a022011-05-18 14:42:07 +010091
Michael Hennerich8a27a022011-05-18 14:42:07 +010092 ade7758_write_waveform_type(&indio_dev->dev,
Georgiana Rodica Chelu89237e02016-09-19 18:41:56 +030093 indio_dev->channels[channel].address);
Michael Hennerich8a27a022011-05-18 14:42:07 +010094
95 return 0;
96}
97
Jonathan Cameron14555b12011-09-21 11:15:57 +010098static const struct iio_buffer_setup_ops ade7758_ring_setup_ops = {
Jonathan Cameron5565a452011-05-18 14:42:24 +010099 .preenable = &ade7758_ring_preenable,
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100100 .postenable = &iio_triggered_buffer_postenable,
101 .predisable = &iio_triggered_buffer_predisable,
Lars-Peter Clausenee0312a2012-07-09 10:00:00 +0100102 .validate_scan_mask = &iio_validate_scan_mask_onehot,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100103};
104
Barry Song8210cfe2010-10-27 21:44:16 -0400105void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
106{
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100107 iio_dealloc_pollfunc(indio_dev->pollfunc);
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +0000108 iio_kfifo_free(indio_dev->buffer);
Barry Song8210cfe2010-10-27 21:44:16 -0400109}
110
111int ade7758_configure_ring(struct iio_dev *indio_dev)
112{
Michael Hennericha3f02372011-05-18 14:42:36 +0100113 struct ade7758_state *st = iio_priv(indio_dev);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100114 struct iio_buffer *buffer;
Barry Song8210cfe2010-10-27 21:44:16 -0400115 int ret = 0;
Barry Song8210cfe2010-10-27 21:44:16 -0400116
Karol Wrona7ab374a2014-12-19 18:39:24 +0100117 buffer = iio_kfifo_allocate();
Taiane Coelho Ramos6fe90922015-03-13 16:22:44 -0300118 if (!buffer)
Tina Johnson9d9f5a22015-03-09 16:11:18 +0530119 return -ENOMEM;
Barry Song8210cfe2010-10-27 21:44:16 -0400120
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100121 iio_device_attach_buffer(indio_dev, buffer);
122
Jonathan Cameron16122442011-12-05 22:18:14 +0000123 indio_dev->setup_ops = &ade7758_ring_setup_ops;
Barry Song8210cfe2010-10-27 21:44:16 -0400124
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100125 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
126 &ade7758_trigger_handler,
127 0,
128 indio_dev,
129 "ade7759_consumer%d",
130 indio_dev->id);
Cristina Opriceana45297572015-03-31 13:01:18 +0300131 if (!indio_dev->pollfunc) {
Jonathan Cameron3f00ca42011-05-18 14:41:26 +0100132 ret = -ENOMEM;
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +0000133 goto error_iio_kfifo_free;
Jonathan Cameron3f00ca42011-05-18 14:41:26 +0100134 }
Jonathan Cameron0ed731d2011-05-18 14:42:39 +0100135
Jonathan Cameronec3afa42011-09-21 11:15:54 +0100136 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
Jonathan Cameron3f00ca42011-05-18 14:41:26 +0100137
Michael Hennerich8a27a022011-05-18 14:42:07 +0100138 st->tx_buf[0] = ADE7758_READ_REG(ADE7758_RSTATUS);
139 st->tx_buf[1] = 0;
140 st->tx_buf[2] = 0;
141 st->tx_buf[3] = 0;
142 st->tx_buf[4] = ADE7758_READ_REG(ADE7758_WFORM);
143 st->tx_buf[5] = 0;
144 st->tx_buf[6] = 0;
145 st->tx_buf[7] = 0;
146
147 /* build spi ring message */
148 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
149 st->ring_xfer[0].len = 1;
150 st->ring_xfer[0].bits_per_word = 8;
151 st->ring_xfer[0].delay_usecs = 4;
152 st->ring_xfer[1].rx_buf = &st->rx_buf[1];
153 st->ring_xfer[1].len = 3;
154 st->ring_xfer[1].bits_per_word = 8;
155 st->ring_xfer[1].cs_change = 1;
156
157 st->ring_xfer[2].tx_buf = &st->tx_buf[4];
158 st->ring_xfer[2].len = 1;
159 st->ring_xfer[2].bits_per_word = 8;
160 st->ring_xfer[2].delay_usecs = 1;
161 st->ring_xfer[3].rx_buf = &st->rx_buf[5];
162 st->ring_xfer[3].len = 3;
163 st->ring_xfer[3].bits_per_word = 8;
164
165 spi_message_init(&st->ring_msg);
166 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
167 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
168 spi_message_add_tail(&st->ring_xfer[2], &st->ring_msg);
169 spi_message_add_tail(&st->ring_xfer[3], &st->ring_msg);
170
Barry Song8210cfe2010-10-27 21:44:16 -0400171 return 0;
172
Jonathan Cameron94f3c7c2012-11-30 14:22:00 +0000173error_iio_kfifo_free:
174 iio_kfifo_free(indio_dev->buffer);
Barry Song8210cfe2010-10-27 21:44:16 -0400175 return ret;
176}