blob: 17309591ca5718a015745f58278eb6133ff93d8b [file] [log] [blame]
Barry Song8d97a582010-10-27 21:44:15 -04001/*
2 * ADE7754 Polyphase Multifunction Energy Metering IC Driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/irq.h>
Barry Song8d97a582010-10-27 21:44:15 -040011#include <linux/delay.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>
Paul Gortmaker99c97852011-07-03 15:49:50 -040019#include <linux/module.h>
Barry Song8d97a582010-10-27 21:44:15 -040020
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
Barry Song8d97a582010-10-27 21:44:15 -040023#include "meter.h"
24#include "ade7754.h"
25
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +030026static int ade7754_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val)
Barry Song8d97a582010-10-27 21:44:15 -040027{
28 int ret;
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020029 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +010030 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040031
32 mutex_lock(&st->buf_lock);
33 st->tx[0] = ADE7754_WRITE_REG(reg_address);
34 st->tx[1] = val;
35
36 ret = spi_write(st->us, st->tx, 2);
37 mutex_unlock(&st->buf_lock);
38
39 return ret;
40}
41
42static int ade7754_spi_write_reg_16(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +030043 u8 reg_address, u16 value)
Barry Song8d97a582010-10-27 21:44:15 -040044{
45 int ret;
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020046 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +010047 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040048
49 mutex_lock(&st->buf_lock);
50 st->tx[0] = ADE7754_WRITE_REG(reg_address);
51 st->tx[1] = (value >> 8) & 0xFF;
52 st->tx[2] = value & 0xFF;
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000053 ret = spi_write(st->us, st->tx, 3);
Barry Song8d97a582010-10-27 21:44:15 -040054 mutex_unlock(&st->buf_lock);
55
56 return ret;
57}
58
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +030059static int ade7754_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val)
Barry Song8d97a582010-10-27 21:44:15 -040060{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020061 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +010062 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040063 int ret;
Barry Song8d97a582010-10-27 21:44:15 -040064
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000065 ret = spi_w8r8(st->us, ADE7754_READ_REG(reg_address));
66 if (ret < 0) {
Barry Song8d97a582010-10-27 21:44:15 -040067 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +030068 reg_address);
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000069 return ret;
Barry Song8d97a582010-10-27 21:44:15 -040070 }
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000071 *val = ret;
Barry Song8d97a582010-10-27 21:44:15 -040072
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000073 return 0;
Barry Song8d97a582010-10-27 21:44:15 -040074}
75
76static int ade7754_spi_read_reg_16(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +030077 u8 reg_address, u16 *val)
Barry Song8d97a582010-10-27 21:44:15 -040078{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020079 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +010080 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040081 int ret;
Barry Song8d97a582010-10-27 21:44:15 -040082
Lars-Peter Clausen23590742013-09-27 16:34:29 +020083 ret = spi_w8r16be(st->us, ADE7754_READ_REG(reg_address));
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000084 if (ret < 0) {
Barry Song8d97a582010-10-27 21:44:15 -040085 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000086 reg_address);
87 return ret;
Barry Song8d97a582010-10-27 21:44:15 -040088 }
Barry Song8d97a582010-10-27 21:44:15 -040089
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000090 *val = ret;
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000091
92 return 0;
Barry Song8d97a582010-10-27 21:44:15 -040093}
94
95static int ade7754_spi_read_reg_24(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +030096 u8 reg_address, u32 *val)
Barry Song8d97a582010-10-27 21:44:15 -040097{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020098 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +010099 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400100 int ret;
101 struct spi_transfer xfers[] = {
102 {
103 .tx_buf = st->tx,
104 .rx_buf = st->rx,
105 .bits_per_word = 8,
106 .len = 4,
107 },
108 };
109
110 mutex_lock(&st->buf_lock);
111 st->tx[0] = ADE7754_READ_REG(reg_address);
112 st->tx[1] = 0;
113 st->tx[2] = 0;
114 st->tx[3] = 0;
115
Lars-Peter Clausenad6c46b2013-01-09 17:31:00 +0000116 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
Barry Song8d97a582010-10-27 21:44:15 -0400117 if (ret) {
118 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300119 reg_address);
Barry Song8d97a582010-10-27 21:44:15 -0400120 goto error_ret;
121 }
122 *val = (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
123
124error_ret:
125 mutex_unlock(&st->buf_lock);
126 return ret;
127}
128
129static ssize_t ade7754_read_8bit(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300130 struct device_attribute *attr,
131 char *buf)
Barry Song8d97a582010-10-27 21:44:15 -0400132{
133 int ret;
134 u8 val = 0;
135 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
136
137 ret = ade7754_spi_read_reg_8(dev, this_attr->address, &val);
138 if (ret)
139 return ret;
140
141 return sprintf(buf, "%u\n", val);
142}
143
144static ssize_t ade7754_read_16bit(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300145 struct device_attribute *attr,
146 char *buf)
Barry Song8d97a582010-10-27 21:44:15 -0400147{
148 int ret;
149 u16 val = 0;
150 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
151
152 ret = ade7754_spi_read_reg_16(dev, this_attr->address, &val);
153 if (ret)
154 return ret;
155
156 return sprintf(buf, "%u\n", val);
157}
158
159static ssize_t ade7754_read_24bit(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300160 struct device_attribute *attr,
161 char *buf)
Barry Song8d97a582010-10-27 21:44:15 -0400162{
163 int ret;
164 u32 val = 0;
165 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
166
167 ret = ade7754_spi_read_reg_24(dev, this_attr->address, &val);
168 if (ret)
169 return ret;
170
171 return sprintf(buf, "%u\n", val & 0xFFFFFF);
172}
173
174static ssize_t ade7754_write_8bit(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300175 struct device_attribute *attr,
176 const char *buf,
177 size_t len)
Barry Song8d97a582010-10-27 21:44:15 -0400178{
179 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
180 int ret;
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100181 u8 val;
Barry Song8d97a582010-10-27 21:44:15 -0400182
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100183 ret = kstrtou8(buf, 10, &val);
Barry Song8d97a582010-10-27 21:44:15 -0400184 if (ret)
185 goto error_ret;
186 ret = ade7754_spi_write_reg_8(dev, this_attr->address, val);
187
188error_ret:
189 return ret ? ret : len;
190}
191
192static ssize_t ade7754_write_16bit(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300193 struct device_attribute *attr,
194 const char *buf,
195 size_t len)
Barry Song8d97a582010-10-27 21:44:15 -0400196{
197 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
198 int ret;
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100199 u16 val;
Barry Song8d97a582010-10-27 21:44:15 -0400200
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100201 ret = kstrtou16(buf, 10, &val);
Barry Song8d97a582010-10-27 21:44:15 -0400202 if (ret)
203 goto error_ret;
204 ret = ade7754_spi_write_reg_16(dev, this_attr->address, val);
205
206error_ret:
207 return ret ? ret : len;
208}
209
210static int ade7754_reset(struct device *dev)
211{
Devendra Nagaabe4e262015-01-02 04:02:54 -0500212 int ret;
Barry Song8d97a582010-10-27 21:44:15 -0400213 u8 val;
Barry Song8d97a582010-10-27 21:44:15 -0400214
Devendra Nagaabe4e262015-01-02 04:02:54 -0500215 ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
216 if (ret < 0)
217 return ret;
218
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530219 val |= BIT(6); /* Software Chip Reset */
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000220 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
Barry Song8d97a582010-10-27 21:44:15 -0400221}
222
Barry Song8d97a582010-10-27 21:44:15 -0400223static IIO_DEV_ATTR_AENERGY(ade7754_read_24bit, ADE7754_AENERGY);
224static IIO_DEV_ATTR_LAENERGY(ade7754_read_24bit, ADE7754_LAENERGY);
225static IIO_DEV_ATTR_VAENERGY(ade7754_read_24bit, ADE7754_VAENERGY);
226static IIO_DEV_ATTR_LVAENERGY(ade7754_read_24bit, ADE7754_LVAENERGY);
227static IIO_DEV_ATTR_VPEAK(S_IWUSR | S_IRUGO,
228 ade7754_read_8bit,
229 ade7754_write_8bit,
230 ADE7754_VPEAK);
231static IIO_DEV_ATTR_IPEAK(S_IWUSR | S_IRUGO,
232 ade7754_read_8bit,
233 ade7754_write_8bit,
234 ADE7754_VPEAK);
235static IIO_DEV_ATTR_APHCAL(S_IWUSR | S_IRUGO,
236 ade7754_read_8bit,
237 ade7754_write_8bit,
238 ADE7754_APHCAL);
239static IIO_DEV_ATTR_BPHCAL(S_IWUSR | S_IRUGO,
240 ade7754_read_8bit,
241 ade7754_write_8bit,
242 ADE7754_BPHCAL);
243static IIO_DEV_ATTR_CPHCAL(S_IWUSR | S_IRUGO,
244 ade7754_read_8bit,
245 ade7754_write_8bit,
246 ADE7754_CPHCAL);
247static IIO_DEV_ATTR_AAPOS(S_IWUSR | S_IRUGO,
248 ade7754_read_16bit,
249 ade7754_write_16bit,
250 ADE7754_AAPOS);
251static IIO_DEV_ATTR_BAPOS(S_IWUSR | S_IRUGO,
252 ade7754_read_16bit,
253 ade7754_write_16bit,
254 ADE7754_BAPOS);
255static IIO_DEV_ATTR_CAPOS(S_IWUSR | S_IRUGO,
256 ade7754_read_16bit,
257 ade7754_write_16bit,
258 ADE7754_CAPOS);
259static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
260 ade7754_read_8bit,
261 ade7754_write_8bit,
262 ADE7754_WDIV);
263static IIO_DEV_ATTR_VADIV(S_IWUSR | S_IRUGO,
264 ade7754_read_8bit,
265 ade7754_write_8bit,
266 ADE7754_VADIV);
267static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
268 ade7754_read_16bit,
269 ade7754_write_16bit,
270 ADE7754_CFNUM);
271static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
272 ade7754_read_16bit,
273 ade7754_write_16bit,
274 ADE7754_CFDEN);
275static IIO_DEV_ATTR_ACTIVE_POWER_A_GAIN(S_IWUSR | S_IRUGO,
276 ade7754_read_16bit,
277 ade7754_write_16bit,
278 ADE7754_AAPGAIN);
279static IIO_DEV_ATTR_ACTIVE_POWER_B_GAIN(S_IWUSR | S_IRUGO,
280 ade7754_read_16bit,
281 ade7754_write_16bit,
282 ADE7754_BAPGAIN);
283static IIO_DEV_ATTR_ACTIVE_POWER_C_GAIN(S_IWUSR | S_IRUGO,
284 ade7754_read_16bit,
285 ade7754_write_16bit,
286 ADE7754_CAPGAIN);
287static IIO_DEV_ATTR_AIRMS(S_IRUGO,
288 ade7754_read_24bit,
289 NULL,
290 ADE7754_AIRMS);
291static IIO_DEV_ATTR_BIRMS(S_IRUGO,
292 ade7754_read_24bit,
293 NULL,
294 ADE7754_BIRMS);
295static IIO_DEV_ATTR_CIRMS(S_IRUGO,
296 ade7754_read_24bit,
297 NULL,
298 ADE7754_CIRMS);
299static IIO_DEV_ATTR_AVRMS(S_IRUGO,
300 ade7754_read_24bit,
301 NULL,
302 ADE7754_AVRMS);
303static IIO_DEV_ATTR_BVRMS(S_IRUGO,
304 ade7754_read_24bit,
305 NULL,
306 ADE7754_BVRMS);
307static IIO_DEV_ATTR_CVRMS(S_IRUGO,
308 ade7754_read_24bit,
309 NULL,
310 ADE7754_CVRMS);
311static IIO_DEV_ATTR_AIRMSOS(S_IRUGO,
312 ade7754_read_16bit,
313 ade7754_write_16bit,
314 ADE7754_AIRMSOS);
315static IIO_DEV_ATTR_BIRMSOS(S_IRUGO,
316 ade7754_read_16bit,
317 ade7754_write_16bit,
318 ADE7754_BIRMSOS);
319static IIO_DEV_ATTR_CIRMSOS(S_IRUGO,
320 ade7754_read_16bit,
321 ade7754_write_16bit,
322 ADE7754_CIRMSOS);
323static IIO_DEV_ATTR_AVRMSOS(S_IRUGO,
324 ade7754_read_16bit,
325 ade7754_write_16bit,
326 ADE7754_AVRMSOS);
327static IIO_DEV_ATTR_BVRMSOS(S_IRUGO,
328 ade7754_read_16bit,
329 ade7754_write_16bit,
330 ADE7754_BVRMSOS);
331static IIO_DEV_ATTR_CVRMSOS(S_IRUGO,
332 ade7754_read_16bit,
333 ade7754_write_16bit,
334 ADE7754_CVRMSOS);
335
336static int ade7754_set_irq(struct device *dev, bool enable)
337{
338 int ret;
339 u16 irqen;
Darshana Padmadas1592bfd2014-09-30 20:22:56 +0530340
Barry Song8d97a582010-10-27 21:44:15 -0400341 ret = ade7754_spi_read_reg_16(dev, ADE7754_IRQEN, &irqen);
342 if (ret)
Sandhya Bankar78731a02016-03-06 18:44:59 +0530343 return ret;
Barry Song8d97a582010-10-27 21:44:15 -0400344
345 if (enable)
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530346 irqen |= BIT(14); /* Enables an interrupt when a data is
Alison Schofielda6634f82016-03-21 15:39:53 -0700347 * present in the waveform register
348 */
Barry Song8d97a582010-10-27 21:44:15 -0400349 else
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530350 irqen &= ~BIT(14);
Barry Song8d97a582010-10-27 21:44:15 -0400351
352 ret = ade7754_spi_write_reg_16(dev, ADE7754_IRQEN, irqen);
Barry Song8d97a582010-10-27 21:44:15 -0400353
Barry Song8d97a582010-10-27 21:44:15 -0400354 return ret;
355}
356
357/* Power down the device */
358static int ade7754_stop_device(struct device *dev)
359{
Devendra Nagaabe4e262015-01-02 04:02:54 -0500360 int ret;
Barry Song8d97a582010-10-27 21:44:15 -0400361 u8 val;
Barry Song8d97a582010-10-27 21:44:15 -0400362
Devendra Nagaabe4e262015-01-02 04:02:54 -0500363 ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
364 if (ret < 0) {
365 dev_err(dev, "unable to power down the device, error: %d",
366 ret);
367 return ret;
368 }
369
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000370 val |= 7 << 3; /* ADE7754 powered down */
371 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
Barry Song8d97a582010-10-27 21:44:15 -0400372}
373
Jonathan Cameron57157d12011-06-27 13:07:49 +0100374static int ade7754_initial_setup(struct iio_dev *indio_dev)
Barry Song8d97a582010-10-27 21:44:15 -0400375{
376 int ret;
Jonathan Cameron57157d12011-06-27 13:07:49 +0100377 struct ade7754_state *st = iio_priv(indio_dev);
378 struct device *dev = &indio_dev->dev;
Barry Song8d97a582010-10-27 21:44:15 -0400379
380 /* use low spi speed for init */
381 st->us->mode = SPI_MODE_3;
382 spi_setup(st->us);
383
384 /* Disable IRQ */
385 ret = ade7754_set_irq(dev, false);
386 if (ret) {
387 dev_err(dev, "disable irq failed");
388 goto err_ret;
389 }
390
391 ade7754_reset(dev);
392 msleep(ADE7754_STARTUP_DELAY);
393
394err_ret:
395 return ret;
396}
397
398static ssize_t ade7754_read_frequency(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300399 struct device_attribute *attr,
400 char *buf)
Barry Song8d97a582010-10-27 21:44:15 -0400401{
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000402 int ret;
Barry Song8d97a582010-10-27 21:44:15 -0400403 u8 t;
404 int sps;
Darshana Padmadas1592bfd2014-09-30 20:22:56 +0530405
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300406 ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, &t);
Barry Song8d97a582010-10-27 21:44:15 -0400407 if (ret)
408 return ret;
409
410 t = (t >> 3) & 0x3;
411 sps = 26000 / (1 + t);
412
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000413 return sprintf(buf, "%d\n", sps);
Barry Song8d97a582010-10-27 21:44:15 -0400414}
415
416static ssize_t ade7754_write_frequency(struct device *dev,
Georgiana Rodica Chelu748df3b2016-09-19 19:35:09 +0300417 struct device_attribute *attr,
418 const char *buf,
419 size_t len)
Barry Song8d97a582010-10-27 21:44:15 -0400420{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +0200421 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +0100422 struct ade7754_state *st = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100423 u16 val;
Barry Song8d97a582010-10-27 21:44:15 -0400424 int ret;
425 u8 reg, t;
426
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100427 ret = kstrtou16(buf, 10, &val);
Barry Song8d97a582010-10-27 21:44:15 -0400428 if (ret)
429 return ret;
Cristina Moraru062f7822015-10-20 22:55:41 +0300430 if (!val)
Dan Carpenter50d4b302012-08-18 09:48:00 +0100431 return -EINVAL;
Barry Song8d97a582010-10-27 21:44:15 -0400432
433 mutex_lock(&indio_dev->mlock);
434
Haneen Mohammed5db48512015-03-13 20:47:22 +0300435 t = 26000 / val;
Barry Song8d97a582010-10-27 21:44:15 -0400436 if (t > 0)
437 t--;
438
439 if (t > 1)
440 st->us->max_speed_hz = ADE7754_SPI_SLOW;
441 else
442 st->us->max_speed_hz = ADE7754_SPI_FAST;
443
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000444 ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, &reg);
Barry Song8d97a582010-10-27 21:44:15 -0400445 if (ret)
446 goto out;
447
448 reg &= ~(3 << 3);
449 reg |= t << 3;
450
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000451 ret = ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg);
Barry Song8d97a582010-10-27 21:44:15 -0400452
453out:
454 mutex_unlock(&indio_dev->mlock);
455
456 return ret ? ret : len;
457}
458static IIO_DEV_ATTR_TEMP_RAW(ade7754_read_8bit);
Jonathan Cameron322c9562011-09-14 13:01:23 +0100459static IIO_CONST_ATTR(in_temp_offset, "129 C");
460static IIO_CONST_ATTR(in_temp_scale, "4 C");
Barry Song8d97a582010-10-27 21:44:15 -0400461
462static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
463 ade7754_read_frequency,
464 ade7754_write_frequency);
465
Barry Song8d97a582010-10-27 21:44:15 -0400466static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000");
467
Barry Song8d97a582010-10-27 21:44:15 -0400468static struct attribute *ade7754_attributes[] = {
Jonathan Cameron322c9562011-09-14 13:01:23 +0100469 &iio_dev_attr_in_temp_raw.dev_attr.attr,
470 &iio_const_attr_in_temp_offset.dev_attr.attr,
471 &iio_const_attr_in_temp_scale.dev_attr.attr,
Barry Song8d97a582010-10-27 21:44:15 -0400472 &iio_dev_attr_sampling_frequency.dev_attr.attr,
473 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Barry Song8d97a582010-10-27 21:44:15 -0400474 &iio_dev_attr_aenergy.dev_attr.attr,
475 &iio_dev_attr_laenergy.dev_attr.attr,
476 &iio_dev_attr_vaenergy.dev_attr.attr,
477 &iio_dev_attr_lvaenergy.dev_attr.attr,
478 &iio_dev_attr_vpeak.dev_attr.attr,
479 &iio_dev_attr_ipeak.dev_attr.attr,
480 &iio_dev_attr_aphcal.dev_attr.attr,
481 &iio_dev_attr_bphcal.dev_attr.attr,
482 &iio_dev_attr_cphcal.dev_attr.attr,
483 &iio_dev_attr_aapos.dev_attr.attr,
484 &iio_dev_attr_bapos.dev_attr.attr,
485 &iio_dev_attr_capos.dev_attr.attr,
486 &iio_dev_attr_wdiv.dev_attr.attr,
487 &iio_dev_attr_vadiv.dev_attr.attr,
488 &iio_dev_attr_cfnum.dev_attr.attr,
489 &iio_dev_attr_cfden.dev_attr.attr,
490 &iio_dev_attr_active_power_a_gain.dev_attr.attr,
491 &iio_dev_attr_active_power_b_gain.dev_attr.attr,
492 &iio_dev_attr_active_power_c_gain.dev_attr.attr,
493 &iio_dev_attr_airms.dev_attr.attr,
494 &iio_dev_attr_birms.dev_attr.attr,
495 &iio_dev_attr_cirms.dev_attr.attr,
496 &iio_dev_attr_avrms.dev_attr.attr,
497 &iio_dev_attr_bvrms.dev_attr.attr,
498 &iio_dev_attr_cvrms.dev_attr.attr,
499 &iio_dev_attr_airmsos.dev_attr.attr,
500 &iio_dev_attr_birmsos.dev_attr.attr,
501 &iio_dev_attr_cirmsos.dev_attr.attr,
502 &iio_dev_attr_avrmsos.dev_attr.attr,
503 &iio_dev_attr_bvrmsos.dev_attr.attr,
504 &iio_dev_attr_cvrmsos.dev_attr.attr,
505 NULL,
506};
507
508static const struct attribute_group ade7754_attribute_group = {
509 .attrs = ade7754_attributes,
510};
511
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100512static const struct iio_info ade7754_info = {
513 .attrs = &ade7754_attribute_group,
514 .driver_module = THIS_MODULE,
515};
Barry Song8d97a582010-10-27 21:44:15 -0400516
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500517static int ade7754_probe(struct spi_device *spi)
Barry Song8d97a582010-10-27 21:44:15 -0400518{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100519 int ret;
Jonathan Cameron57157d12011-06-27 13:07:49 +0100520 struct ade7754_state *st;
521 struct iio_dev *indio_dev;
522
523 /* setup the industrialio driver allocated elements */
Sachin Kamatfde93552013-09-11 10:55:00 +0100524 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
525 if (!indio_dev)
526 return -ENOMEM;
Barry Song8d97a582010-10-27 21:44:15 -0400527 /* this is only used for removal purposes */
Jonathan Cameron57157d12011-06-27 13:07:49 +0100528 spi_set_drvdata(spi, indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400529
Jonathan Cameron57157d12011-06-27 13:07:49 +0100530 st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400531 st->us = spi;
532 mutex_init(&st->buf_lock);
Barry Song8d97a582010-10-27 21:44:15 -0400533
Jonathan Cameron57157d12011-06-27 13:07:49 +0100534 indio_dev->name = spi->dev.driver->name;
535 indio_dev->dev.parent = &spi->dev;
536 indio_dev->info = &ade7754_info;
537 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song8d97a582010-10-27 21:44:15 -0400538
Barry Song8d97a582010-10-27 21:44:15 -0400539 /* Get the device into a sane initial state */
Jonathan Cameron57157d12011-06-27 13:07:49 +0100540 ret = ade7754_initial_setup(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400541 if (ret)
Cristina Opriceanab72eb702015-03-29 16:14:39 +0300542 goto powerdown_on_error;
543 ret = iio_device_register(indio_dev);
544 if (ret)
545 goto powerdown_on_error;
546 return ret;
547
548powerdown_on_error:
549 ade7754_stop_device(&indio_dev->dev);
550 return ret;
Barry Song8d97a582010-10-27 21:44:15 -0400551}
552
Bill Pemberton447d4f22012-11-19 13:26:37 -0500553static int ade7754_remove(struct spi_device *spi)
Barry Song8d97a582010-10-27 21:44:15 -0400554{
Jonathan Cameron57157d12011-06-27 13:07:49 +0100555 struct iio_dev *indio_dev = spi_get_drvdata(spi);
Barry Song8d97a582010-10-27 21:44:15 -0400556
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100557 iio_device_unregister(indio_dev);
Lars-Peter Clausendb314a12012-09-22 09:56:00 +0100558 ade7754_stop_device(&indio_dev->dev);
Barry Song8d97a582010-10-27 21:44:15 -0400559
Lars-Peter Clausendb314a12012-09-22 09:56:00 +0100560 return 0;
Barry Song8d97a582010-10-27 21:44:15 -0400561}
562
563static struct spi_driver ade7754_driver = {
564 .driver = {
565 .name = "ade7754",
Barry Song8d97a582010-10-27 21:44:15 -0400566 },
567 .probe = ade7754_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500568 .remove = ade7754_remove,
Barry Song8d97a582010-10-27 21:44:15 -0400569};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100570module_spi_driver(ade7754_driver);
Barry Song8d97a582010-10-27 21:44:15 -0400571
572MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
573MODULE_DESCRIPTION("Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver");
574MODULE_LICENSE("GPL v2");
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100575MODULE_ALIAS("spi:ad7754");