blob: 69287108f793bcb81bcf91451b64f1f4f86c2257 [file] [log] [blame]
Barry Song09434ef2010-10-27 21:44:14 -04001/*
Jonathan Cameron72db6eb2011-02-11 14:07:40 +00002 * ADE7753 Single-Phase Multifunction Metering IC with di/dt Sensor Interface
Barry Song09434ef2010-10-27 21:44:14 -04003 *
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 Song09434ef2010-10-27 21:44:14 -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 Song09434ef2010-10-27 21:44:14 -040020
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
Barry Song09434ef2010-10-27 21:44:14 -040023#include "meter.h"
24#include "ade7753.h"
25
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000026static int ade7753_spi_write_reg_8(struct device *dev,
27 u8 reg_address,
28 u8 val)
Barry Song09434ef2010-10-27 21:44:14 -040029{
30 int ret;
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020031 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +010032 struct ade7753_state *st = iio_priv(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -040033
34 mutex_lock(&st->buf_lock);
35 st->tx[0] = ADE7753_WRITE_REG(reg_address);
36 st->tx[1] = val;
37
38 ret = spi_write(st->us, st->tx, 2);
39 mutex_unlock(&st->buf_lock);
40
41 return ret;
42}
43
44static int ade7753_spi_write_reg_16(struct device *dev,
45 u8 reg_address,
46 u16 value)
47{
48 int ret;
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020049 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +010050 struct ade7753_state *st = iio_priv(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -040051
52 mutex_lock(&st->buf_lock);
53 st->tx[0] = ADE7753_WRITE_REG(reg_address);
54 st->tx[1] = (value >> 8) & 0xFF;
55 st->tx[2] = value & 0xFF;
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000056 ret = spi_write(st->us, st->tx, 3);
Barry Song09434ef2010-10-27 21:44:14 -040057 mutex_unlock(&st->buf_lock);
58
59 return ret;
60}
61
62static int ade7753_spi_read_reg_8(struct device *dev,
63 u8 reg_address,
64 u8 *val)
65{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020066 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +010067 struct ade7753_state *st = iio_priv(indio_dev);
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000068 ssize_t ret;
Barry Song09434ef2010-10-27 21:44:14 -040069
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000070 ret = spi_w8r8(st->us, ADE7753_READ_REG(reg_address));
71 if (ret < 0) {
Barry Song09434ef2010-10-27 21:44:14 -040072 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
73 reg_address);
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000074 return ret;
Barry Song09434ef2010-10-27 21:44:14 -040075 }
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000076 *val = ret;
Barry Song09434ef2010-10-27 21:44:14 -040077
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000078 return 0;
Barry Song09434ef2010-10-27 21:44:14 -040079}
80
81static int ade7753_spi_read_reg_16(struct device *dev,
82 u8 reg_address,
83 u16 *val)
84{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020085 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +010086 struct ade7753_state *st = iio_priv(indio_dev);
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000087 ssize_t ret;
Barry Song09434ef2010-10-27 21:44:14 -040088
Lars-Peter Clausen23590742013-09-27 16:34:29 +020089 ret = spi_w8r16be(st->us, ADE7753_READ_REG(reg_address));
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000090 if (ret < 0) {
Barry Song09434ef2010-10-27 21:44:14 -040091 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000092 reg_address);
93 return ret;
Barry Song09434ef2010-10-27 21:44:14 -040094 }
Barry Song09434ef2010-10-27 21:44:14 -040095
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000096 *val = ret;
Jonathan Cameron72db6eb2011-02-11 14:07:40 +000097
98 return 0;
Barry Song09434ef2010-10-27 21:44:14 -040099}
100
101static int ade7753_spi_read_reg_24(struct device *dev,
102 u8 reg_address,
103 u32 *val)
104{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +0200105 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100106 struct ade7753_state *st = iio_priv(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -0400107 int ret;
108 struct spi_transfer xfers[] = {
109 {
110 .tx_buf = st->tx,
Barry Song09434ef2010-10-27 21:44:14 -0400111 .bits_per_word = 8,
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000112 .len = 1,
113 }, {
114 .rx_buf = st->tx,
115 .bits_per_word = 8,
116 .len = 3,
117 }
Barry Song09434ef2010-10-27 21:44:14 -0400118 };
119
120 mutex_lock(&st->buf_lock);
121 st->tx[0] = ADE7753_READ_REG(reg_address);
Barry Song09434ef2010-10-27 21:44:14 -0400122
Lars-Peter Clausenad6c46b2013-01-09 17:31:00 +0000123 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
Barry Song09434ef2010-10-27 21:44:14 -0400124 if (ret) {
125 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
126 reg_address);
127 goto error_ret;
128 }
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000129 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
Barry Song09434ef2010-10-27 21:44:14 -0400130
131error_ret:
132 mutex_unlock(&st->buf_lock);
133 return ret;
134}
135
136static ssize_t ade7753_read_8bit(struct device *dev,
137 struct device_attribute *attr,
138 char *buf)
139{
140 int ret;
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000141 u8 val;
Barry Song09434ef2010-10-27 21:44:14 -0400142 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
143
144 ret = ade7753_spi_read_reg_8(dev, this_attr->address, &val);
145 if (ret)
146 return ret;
147
148 return sprintf(buf, "%u\n", val);
149}
150
151static ssize_t ade7753_read_16bit(struct device *dev,
152 struct device_attribute *attr,
153 char *buf)
154{
155 int ret;
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000156 u16 val;
Barry Song09434ef2010-10-27 21:44:14 -0400157 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
158
159 ret = ade7753_spi_read_reg_16(dev, this_attr->address, &val);
160 if (ret)
161 return ret;
162
163 return sprintf(buf, "%u\n", val);
164}
165
166static ssize_t ade7753_read_24bit(struct device *dev,
167 struct device_attribute *attr,
168 char *buf)
169{
170 int ret;
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000171 u32 val;
Barry Song09434ef2010-10-27 21:44:14 -0400172 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
173
174 ret = ade7753_spi_read_reg_24(dev, this_attr->address, &val);
175 if (ret)
176 return ret;
177
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000178 return sprintf(buf, "%u\n", val);
Barry Song09434ef2010-10-27 21:44:14 -0400179}
180
181static ssize_t ade7753_write_8bit(struct device *dev,
182 struct device_attribute *attr,
183 const char *buf,
184 size_t len)
185{
186 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
187 int ret;
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100188 u8 val;
Barry Song09434ef2010-10-27 21:44:14 -0400189
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100190 ret = kstrtou8(buf, 10, &val);
Barry Song09434ef2010-10-27 21:44:14 -0400191 if (ret)
192 goto error_ret;
193 ret = ade7753_spi_write_reg_8(dev, this_attr->address, val);
194
195error_ret:
196 return ret ? ret : len;
197}
198
199static ssize_t ade7753_write_16bit(struct device *dev,
200 struct device_attribute *attr,
201 const char *buf,
202 size_t len)
203{
204 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
205 int ret;
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100206 u16 val;
Barry Song09434ef2010-10-27 21:44:14 -0400207
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100208 ret = kstrtou16(buf, 10, &val);
Barry Song09434ef2010-10-27 21:44:14 -0400209 if (ret)
210 goto error_ret;
211 ret = ade7753_spi_write_reg_16(dev, this_attr->address, val);
212
213error_ret:
214 return ret ? ret : len;
215}
216
217static int ade7753_reset(struct device *dev)
218{
Barry Song09434ef2010-10-27 21:44:14 -0400219 u16 val;
Arnd Bergmann7e1da862016-01-25 16:52:40 +0100220 int ret;
Barry Song09434ef2010-10-27 21:44:14 -0400221
Arnd Bergmann7e1da862016-01-25 16:52:40 +0100222 ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &val);
223 if (ret)
224 return ret;
225
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530226 val |= BIT(6); /* Software Chip Reset */
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000227
228 return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
Barry Song09434ef2010-10-27 21:44:14 -0400229}
230
Barry Song09434ef2010-10-27 21:44:14 -0400231static IIO_DEV_ATTR_AENERGY(ade7753_read_24bit, ADE7753_AENERGY);
232static IIO_DEV_ATTR_LAENERGY(ade7753_read_24bit, ADE7753_LAENERGY);
233static IIO_DEV_ATTR_VAENERGY(ade7753_read_24bit, ADE7753_VAENERGY);
234static IIO_DEV_ATTR_LVAENERGY(ade7753_read_24bit, ADE7753_LVAENERGY);
235static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
236 ade7753_read_16bit,
237 ade7753_write_16bit,
238 ADE7753_CFDEN);
239static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
240 ade7753_read_8bit,
241 ade7753_write_8bit,
242 ADE7753_CFNUM);
243static IIO_DEV_ATTR_CHKSUM(ade7753_read_8bit, ADE7753_CHKSUM);
244static IIO_DEV_ATTR_PHCAL(S_IWUSR | S_IRUGO,
245 ade7753_read_16bit,
246 ade7753_write_16bit,
247 ADE7753_PHCAL);
248static IIO_DEV_ATTR_APOS(S_IWUSR | S_IRUGO,
249 ade7753_read_16bit,
250 ade7753_write_16bit,
251 ADE7753_APOS);
252static IIO_DEV_ATTR_SAGCYC(S_IWUSR | S_IRUGO,
253 ade7753_read_8bit,
254 ade7753_write_8bit,
255 ADE7753_SAGCYC);
256static IIO_DEV_ATTR_SAGLVL(S_IWUSR | S_IRUGO,
257 ade7753_read_8bit,
258 ade7753_write_8bit,
259 ADE7753_SAGLVL);
260static IIO_DEV_ATTR_LINECYC(S_IWUSR | S_IRUGO,
261 ade7753_read_8bit,
262 ade7753_write_8bit,
263 ADE7753_LINECYC);
264static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
265 ade7753_read_8bit,
266 ade7753_write_8bit,
267 ADE7753_WDIV);
268static IIO_DEV_ATTR_IRMS(S_IWUSR | S_IRUGO,
269 ade7753_read_24bit,
270 NULL,
271 ADE7753_IRMS);
272static IIO_DEV_ATTR_VRMS(S_IRUGO,
273 ade7753_read_24bit,
274 NULL,
275 ADE7753_VRMS);
276static IIO_DEV_ATTR_IRMSOS(S_IWUSR | S_IRUGO,
277 ade7753_read_16bit,
278 ade7753_write_16bit,
279 ADE7753_IRMSOS);
280static IIO_DEV_ATTR_VRMSOS(S_IWUSR | S_IRUGO,
281 ade7753_read_16bit,
282 ade7753_write_16bit,
283 ADE7753_VRMSOS);
284static IIO_DEV_ATTR_WGAIN(S_IWUSR | S_IRUGO,
285 ade7753_read_16bit,
286 ade7753_write_16bit,
287 ADE7753_WGAIN);
288static IIO_DEV_ATTR_VAGAIN(S_IWUSR | S_IRUGO,
289 ade7753_read_16bit,
290 ade7753_write_16bit,
291 ADE7753_VAGAIN);
292static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR | S_IRUGO,
293 ade7753_read_16bit,
294 ade7753_write_16bit,
295 ADE7753_GAIN);
296static IIO_DEV_ATTR_IPKLVL(S_IWUSR | S_IRUGO,
297 ade7753_read_8bit,
298 ade7753_write_8bit,
299 ADE7753_IPKLVL);
300static IIO_DEV_ATTR_VPKLVL(S_IWUSR | S_IRUGO,
301 ade7753_read_8bit,
302 ade7753_write_8bit,
303 ADE7753_VPKLVL);
304static IIO_DEV_ATTR_IPEAK(S_IRUGO,
305 ade7753_read_24bit,
306 NULL,
307 ADE7753_IPEAK);
308static IIO_DEV_ATTR_VPEAK(S_IRUGO,
309 ade7753_read_24bit,
310 NULL,
311 ADE7753_VPEAK);
312static IIO_DEV_ATTR_VPERIOD(S_IRUGO,
313 ade7753_read_16bit,
314 NULL,
315 ADE7753_PERIOD);
316static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR | S_IRUGO,
317 ade7753_read_8bit,
318 ade7753_write_8bit,
319 ADE7753_CH1OS);
320static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR | S_IRUGO,
321 ade7753_read_8bit,
322 ade7753_write_8bit,
323 ADE7753_CH2OS);
324
325static int ade7753_set_irq(struct device *dev, bool enable)
326{
327 int ret;
328 u8 irqen;
Tina Johnsonb581c3d2014-09-13 15:46:15 +0530329
Barry Song09434ef2010-10-27 21:44:14 -0400330 ret = ade7753_spi_read_reg_8(dev, ADE7753_IRQEN, &irqen);
331 if (ret)
332 goto error_ret;
333
334 if (enable)
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530335 irqen |= BIT(3); /* Enables an interrupt when a data is
Barry Song09434ef2010-10-27 21:44:14 -0400336 present in the waveform register */
337 else
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530338 irqen &= ~BIT(3);
Barry Song09434ef2010-10-27 21:44:14 -0400339
340 ret = ade7753_spi_write_reg_8(dev, ADE7753_IRQEN, irqen);
Barry Song09434ef2010-10-27 21:44:14 -0400341
342error_ret:
343 return ret;
344}
345
346/* Power down the device */
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000347static int ade7753_stop_device(struct device *dev)
Barry Song09434ef2010-10-27 21:44:14 -0400348{
Barry Song09434ef2010-10-27 21:44:14 -0400349 u16 val;
Arnd Bergmann7e1da862016-01-25 16:52:40 +0100350 int ret;
Barry Song09434ef2010-10-27 21:44:14 -0400351
Arnd Bergmann7e1da862016-01-25 16:52:40 +0100352 ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &val);
353 if (ret)
354 return ret;
355
Shraddha Barkeb4d8ace2015-09-10 22:00:30 +0530356 val |= BIT(4); /* AD converters can be turned off */
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000357
358 return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
Barry Song09434ef2010-10-27 21:44:14 -0400359}
360
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100361static int ade7753_initial_setup(struct iio_dev *indio_dev)
Barry Song09434ef2010-10-27 21:44:14 -0400362{
363 int ret;
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100364 struct device *dev = &indio_dev->dev;
365 struct ade7753_state *st = iio_priv(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -0400366
367 /* use low spi speed for init */
368 st->us->mode = SPI_MODE_3;
369 spi_setup(st->us);
370
371 /* Disable IRQ */
372 ret = ade7753_set_irq(dev, false);
373 if (ret) {
374 dev_err(dev, "disable irq failed");
375 goto err_ret;
376 }
377
378 ade7753_reset(dev);
379 msleep(ADE7753_STARTUP_DELAY);
380
381err_ret:
382 return ret;
383}
384
385static ssize_t ade7753_read_frequency(struct device *dev,
386 struct device_attribute *attr,
387 char *buf)
388{
Tina Johnson90347202014-09-14 16:30:05 +0530389 int ret;
Jonathan Camerone1703b322011-07-14 10:24:02 +0100390 u16 t;
Barry Song09434ef2010-10-27 21:44:14 -0400391 int sps;
Tina Johnsonb581c3d2014-09-13 15:46:15 +0530392
Jonathan Camerone1703b322011-07-14 10:24:02 +0100393 ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &t);
Barry Song09434ef2010-10-27 21:44:14 -0400394 if (ret)
395 return ret;
396
397 t = (t >> 11) & 0x3;
398 sps = 27900 / (1 + t);
399
Tina Johnson90347202014-09-14 16:30:05 +0530400 return sprintf(buf, "%d\n", sps);
Barry Song09434ef2010-10-27 21:44:14 -0400401}
402
403static ssize_t ade7753_write_frequency(struct device *dev,
404 struct device_attribute *attr,
405 const char *buf,
406 size_t len)
407{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +0200408 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100409 struct ade7753_state *st = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100410 u16 val;
Barry Song09434ef2010-10-27 21:44:14 -0400411 int ret;
412 u16 reg, t;
413
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100414 ret = kstrtou16(buf, 10, &val);
Barry Song09434ef2010-10-27 21:44:14 -0400415 if (ret)
416 return ret;
Cristina Moraru062f7822015-10-20 22:55:41 +0300417 if (!val)
Dan Carpenter50d4b302012-08-18 09:48:00 +0100418 return -EINVAL;
Barry Song09434ef2010-10-27 21:44:14 -0400419
420 mutex_lock(&indio_dev->mlock);
421
Haneen Mohammed5db48512015-03-13 20:47:22 +0300422 t = 27900 / val;
Barry Song09434ef2010-10-27 21:44:14 -0400423 if (t > 0)
424 t--;
425
426 if (t > 1)
427 st->us->max_speed_hz = ADE7753_SPI_SLOW;
428 else
429 st->us->max_speed_hz = ADE7753_SPI_FAST;
430
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000431 ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &reg);
Barry Song09434ef2010-10-27 21:44:14 -0400432 if (ret)
433 goto out;
434
435 reg &= ~(3 << 11);
436 reg |= t << 11;
437
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000438 ret = ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg);
Barry Song09434ef2010-10-27 21:44:14 -0400439
440out:
441 mutex_unlock(&indio_dev->mlock);
442
443 return ret ? ret : len;
444}
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000445
Barry Song09434ef2010-10-27 21:44:14 -0400446static IIO_DEV_ATTR_TEMP_RAW(ade7753_read_8bit);
Jonathan Cameron322c9562011-09-14 13:01:23 +0100447static IIO_CONST_ATTR(in_temp_offset, "-25 C");
448static IIO_CONST_ATTR(in_temp_scale, "0.67 C");
Barry Song09434ef2010-10-27 21:44:14 -0400449
450static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
451 ade7753_read_frequency,
452 ade7753_write_frequency);
453
Barry Song09434ef2010-10-27 21:44:14 -0400454static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
455
Barry Song09434ef2010-10-27 21:44:14 -0400456static struct attribute *ade7753_attributes[] = {
Jonathan Cameron322c9562011-09-14 13:01:23 +0100457 &iio_dev_attr_in_temp_raw.dev_attr.attr,
458 &iio_const_attr_in_temp_offset.dev_attr.attr,
459 &iio_const_attr_in_temp_scale.dev_attr.attr,
Barry Song09434ef2010-10-27 21:44:14 -0400460 &iio_dev_attr_sampling_frequency.dev_attr.attr,
461 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Barry Song09434ef2010-10-27 21:44:14 -0400462 &iio_dev_attr_phcal.dev_attr.attr,
463 &iio_dev_attr_cfden.dev_attr.attr,
464 &iio_dev_attr_aenergy.dev_attr.attr,
465 &iio_dev_attr_laenergy.dev_attr.attr,
466 &iio_dev_attr_vaenergy.dev_attr.attr,
467 &iio_dev_attr_lvaenergy.dev_attr.attr,
468 &iio_dev_attr_cfnum.dev_attr.attr,
469 &iio_dev_attr_apos.dev_attr.attr,
470 &iio_dev_attr_sagcyc.dev_attr.attr,
471 &iio_dev_attr_saglvl.dev_attr.attr,
472 &iio_dev_attr_linecyc.dev_attr.attr,
473 &iio_dev_attr_chksum.dev_attr.attr,
474 &iio_dev_attr_pga_gain.dev_attr.attr,
475 &iio_dev_attr_wgain.dev_attr.attr,
476 &iio_dev_attr_choff_1.dev_attr.attr,
477 &iio_dev_attr_choff_2.dev_attr.attr,
478 &iio_dev_attr_wdiv.dev_attr.attr,
479 &iio_dev_attr_irms.dev_attr.attr,
480 &iio_dev_attr_vrms.dev_attr.attr,
481 &iio_dev_attr_irmsos.dev_attr.attr,
482 &iio_dev_attr_vrmsos.dev_attr.attr,
483 &iio_dev_attr_vagain.dev_attr.attr,
484 &iio_dev_attr_ipklvl.dev_attr.attr,
485 &iio_dev_attr_vpklvl.dev_attr.attr,
486 &iio_dev_attr_ipeak.dev_attr.attr,
487 &iio_dev_attr_vpeak.dev_attr.attr,
488 &iio_dev_attr_vperiod.dev_attr.attr,
489 NULL,
490};
491
492static const struct attribute_group ade7753_attribute_group = {
493 .attrs = ade7753_attributes,
494};
495
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100496static const struct iio_info ade7753_info = {
497 .attrs = &ade7753_attribute_group,
498 .driver_module = THIS_MODULE,
499};
500
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500501static int ade7753_probe(struct spi_device *spi)
Barry Song09434ef2010-10-27 21:44:14 -0400502{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100503 int ret;
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100504 struct ade7753_state *st;
505 struct iio_dev *indio_dev;
506
507 /* setup the industrialio driver allocated elements */
Sachin Kamate7d4eb02013-09-11 10:55:00 +0100508 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
509 if (!indio_dev)
510 return -ENOMEM;
Barry Song09434ef2010-10-27 21:44:14 -0400511 /* this is only used for removal purposes */
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100512 spi_set_drvdata(spi, indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -0400513
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100514 st = iio_priv(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -0400515 st->us = spi;
516 mutex_init(&st->buf_lock);
Barry Song09434ef2010-10-27 21:44:14 -0400517
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100518 indio_dev->name = spi->dev.driver->name;
519 indio_dev->dev.parent = &spi->dev;
520 indio_dev->info = &ade7753_info;
521 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song09434ef2010-10-27 21:44:14 -0400522
Barry Song09434ef2010-10-27 21:44:14 -0400523 /* Get the device into a sane initial state */
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100524 ret = ade7753_initial_setup(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -0400525 if (ret)
Sachin Kamate7d4eb02013-09-11 10:55:00 +0100526 return ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100527
Cristina Opriceanada294612015-03-12 19:10:31 +0200528 return iio_device_register(indio_dev);
Barry Song09434ef2010-10-27 21:44:14 -0400529}
530
531/* fixme, confirm ordering in this function */
Bill Pemberton447d4f22012-11-19 13:26:37 -0500532static int ade7753_remove(struct spi_device *spi)
Barry Song09434ef2010-10-27 21:44:14 -0400533{
Jonathan Cameronbfccd4f2011-06-27 13:07:48 +0100534 struct iio_dev *indio_dev = spi_get_drvdata(spi);
Barry Song09434ef2010-10-27 21:44:14 -0400535
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100536 iio_device_unregister(indio_dev);
Lars-Peter Clausend576c752012-09-22 09:56:00 +0100537 ade7753_stop_device(&indio_dev->dev);
Lars-Peter Clausend576c752012-09-22 09:56:00 +0100538
539 return 0;
Barry Song09434ef2010-10-27 21:44:14 -0400540}
541
542static struct spi_driver ade7753_driver = {
543 .driver = {
544 .name = "ade7753",
Barry Song09434ef2010-10-27 21:44:14 -0400545 },
546 .probe = ade7753_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500547 .remove = ade7753_remove,
Barry Song09434ef2010-10-27 21:44:14 -0400548};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100549module_spi_driver(ade7753_driver);
Barry Song09434ef2010-10-27 21:44:14 -0400550
551MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
Jonathan Cameron72db6eb2011-02-11 14:07:40 +0000552MODULE_DESCRIPTION("Analog Devices ADE7753/6 Single-Phase Multifunction Meter");
Barry Song09434ef2010-10-27 21:44:14 -0400553MODULE_LICENSE("GPL v2");
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100554MODULE_ALIAS("spi:ade7753");