blob: f12b2e50329b6a209af4e95cebaa10a8722ad74b [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
26static int ade7754_spi_write_reg_8(struct device *dev,
27 u8 reg_address,
28 u8 val)
29{
30 int ret;
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +020031 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +010032 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040033
34 mutex_lock(&st->buf_lock);
35 st->tx[0] = ADE7754_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 ade7754_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 Cameron57157d12011-06-27 13:07:49 +010050 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040051
52 mutex_lock(&st->buf_lock);
53 st->tx[0] = ADE7754_WRITE_REG(reg_address);
54 st->tx[1] = (value >> 8) & 0xFF;
55 st->tx[2] = value & 0xFF;
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000056 ret = spi_write(st->us, st->tx, 3);
Barry Song8d97a582010-10-27 21:44:15 -040057 mutex_unlock(&st->buf_lock);
58
59 return ret;
60}
61
62static int ade7754_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 Cameron57157d12011-06-27 13:07:49 +010067 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040068 int ret;
Barry Song8d97a582010-10-27 21:44:15 -040069
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000070 ret = spi_w8r8(st->us, ADE7754_READ_REG(reg_address));
71 if (ret < 0) {
Barry Song8d97a582010-10-27 21:44:15 -040072 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
73 reg_address);
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000074 return ret;
Barry Song8d97a582010-10-27 21:44:15 -040075 }
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000076 *val = ret;
Barry Song8d97a582010-10-27 21:44:15 -040077
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000078 return 0;
Barry Song8d97a582010-10-27 21:44:15 -040079}
80
81static int ade7754_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 Cameron57157d12011-06-27 13:07:49 +010086 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -040087 int ret;
Barry Song8d97a582010-10-27 21:44:15 -040088
Lars-Peter Clausen23590742013-09-27 16:34:29 +020089 ret = spi_w8r16be(st->us, ADE7754_READ_REG(reg_address));
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000090 if (ret < 0) {
Barry Song8d97a582010-10-27 21:44:15 -040091 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000092 reg_address);
93 return ret;
Barry Song8d97a582010-10-27 21:44:15 -040094 }
Barry Song8d97a582010-10-27 21:44:15 -040095
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000096 *val = ret;
Jonathan Cameron5a0326d2011-02-11 14:07:41 +000097
98 return 0;
Barry Song8d97a582010-10-27 21:44:15 -040099}
100
101static int ade7754_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 Cameron57157d12011-06-27 13:07:49 +0100106 struct ade7754_state *st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400107 int ret;
108 struct spi_transfer xfers[] = {
109 {
110 .tx_buf = st->tx,
111 .rx_buf = st->rx,
112 .bits_per_word = 8,
113 .len = 4,
114 },
115 };
116
117 mutex_lock(&st->buf_lock);
118 st->tx[0] = ADE7754_READ_REG(reg_address);
119 st->tx[1] = 0;
120 st->tx[2] = 0;
121 st->tx[3] = 0;
122
Lars-Peter Clausenad6c46b2013-01-09 17:31:00 +0000123 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
Barry Song8d97a582010-10-27 21:44:15 -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 }
129 *val = (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
130
131error_ret:
132 mutex_unlock(&st->buf_lock);
133 return ret;
134}
135
136static ssize_t ade7754_read_8bit(struct device *dev,
137 struct device_attribute *attr,
138 char *buf)
139{
140 int ret;
141 u8 val = 0;
142 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
143
144 ret = ade7754_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 ade7754_read_16bit(struct device *dev,
152 struct device_attribute *attr,
153 char *buf)
154{
155 int ret;
156 u16 val = 0;
157 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
158
159 ret = ade7754_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 ade7754_read_24bit(struct device *dev,
167 struct device_attribute *attr,
168 char *buf)
169{
170 int ret;
171 u32 val = 0;
172 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
173
174 ret = ade7754_spi_read_reg_24(dev, this_attr->address, &val);
175 if (ret)
176 return ret;
177
178 return sprintf(buf, "%u\n", val & 0xFFFFFF);
179}
180
181static ssize_t ade7754_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 Song8d97a582010-10-27 21:44:15 -0400189
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100190 ret = kstrtou8(buf, 10, &val);
Barry Song8d97a582010-10-27 21:44:15 -0400191 if (ret)
192 goto error_ret;
193 ret = ade7754_spi_write_reg_8(dev, this_attr->address, val);
194
195error_ret:
196 return ret ? ret : len;
197}
198
199static ssize_t ade7754_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 Song8d97a582010-10-27 21:44:15 -0400207
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100208 ret = kstrtou16(buf, 10, &val);
Barry Song8d97a582010-10-27 21:44:15 -0400209 if (ret)
210 goto error_ret;
211 ret = ade7754_spi_write_reg_16(dev, this_attr->address, val);
212
213error_ret:
214 return ret ? ret : len;
215}
216
217static int ade7754_reset(struct device *dev)
218{
Devendra Nagaabe4e262015-01-02 04:02:54 -0500219 int ret;
Barry Song8d97a582010-10-27 21:44:15 -0400220 u8 val;
Barry Song8d97a582010-10-27 21:44:15 -0400221
Devendra Nagaabe4e262015-01-02 04:02:54 -0500222 ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
223 if (ret < 0)
224 return ret;
225
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000226 val |= 1 << 6; /* Software Chip Reset */
227 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
Barry Song8d97a582010-10-27 21:44:15 -0400228}
229
Barry Song8d97a582010-10-27 21:44:15 -0400230static IIO_DEV_ATTR_AENERGY(ade7754_read_24bit, ADE7754_AENERGY);
231static IIO_DEV_ATTR_LAENERGY(ade7754_read_24bit, ADE7754_LAENERGY);
232static IIO_DEV_ATTR_VAENERGY(ade7754_read_24bit, ADE7754_VAENERGY);
233static IIO_DEV_ATTR_LVAENERGY(ade7754_read_24bit, ADE7754_LVAENERGY);
234static IIO_DEV_ATTR_VPEAK(S_IWUSR | S_IRUGO,
235 ade7754_read_8bit,
236 ade7754_write_8bit,
237 ADE7754_VPEAK);
238static IIO_DEV_ATTR_IPEAK(S_IWUSR | S_IRUGO,
239 ade7754_read_8bit,
240 ade7754_write_8bit,
241 ADE7754_VPEAK);
242static IIO_DEV_ATTR_APHCAL(S_IWUSR | S_IRUGO,
243 ade7754_read_8bit,
244 ade7754_write_8bit,
245 ADE7754_APHCAL);
246static IIO_DEV_ATTR_BPHCAL(S_IWUSR | S_IRUGO,
247 ade7754_read_8bit,
248 ade7754_write_8bit,
249 ADE7754_BPHCAL);
250static IIO_DEV_ATTR_CPHCAL(S_IWUSR | S_IRUGO,
251 ade7754_read_8bit,
252 ade7754_write_8bit,
253 ADE7754_CPHCAL);
254static IIO_DEV_ATTR_AAPOS(S_IWUSR | S_IRUGO,
255 ade7754_read_16bit,
256 ade7754_write_16bit,
257 ADE7754_AAPOS);
258static IIO_DEV_ATTR_BAPOS(S_IWUSR | S_IRUGO,
259 ade7754_read_16bit,
260 ade7754_write_16bit,
261 ADE7754_BAPOS);
262static IIO_DEV_ATTR_CAPOS(S_IWUSR | S_IRUGO,
263 ade7754_read_16bit,
264 ade7754_write_16bit,
265 ADE7754_CAPOS);
266static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
267 ade7754_read_8bit,
268 ade7754_write_8bit,
269 ADE7754_WDIV);
270static IIO_DEV_ATTR_VADIV(S_IWUSR | S_IRUGO,
271 ade7754_read_8bit,
272 ade7754_write_8bit,
273 ADE7754_VADIV);
274static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
275 ade7754_read_16bit,
276 ade7754_write_16bit,
277 ADE7754_CFNUM);
278static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
279 ade7754_read_16bit,
280 ade7754_write_16bit,
281 ADE7754_CFDEN);
282static IIO_DEV_ATTR_ACTIVE_POWER_A_GAIN(S_IWUSR | S_IRUGO,
283 ade7754_read_16bit,
284 ade7754_write_16bit,
285 ADE7754_AAPGAIN);
286static IIO_DEV_ATTR_ACTIVE_POWER_B_GAIN(S_IWUSR | S_IRUGO,
287 ade7754_read_16bit,
288 ade7754_write_16bit,
289 ADE7754_BAPGAIN);
290static IIO_DEV_ATTR_ACTIVE_POWER_C_GAIN(S_IWUSR | S_IRUGO,
291 ade7754_read_16bit,
292 ade7754_write_16bit,
293 ADE7754_CAPGAIN);
294static IIO_DEV_ATTR_AIRMS(S_IRUGO,
295 ade7754_read_24bit,
296 NULL,
297 ADE7754_AIRMS);
298static IIO_DEV_ATTR_BIRMS(S_IRUGO,
299 ade7754_read_24bit,
300 NULL,
301 ADE7754_BIRMS);
302static IIO_DEV_ATTR_CIRMS(S_IRUGO,
303 ade7754_read_24bit,
304 NULL,
305 ADE7754_CIRMS);
306static IIO_DEV_ATTR_AVRMS(S_IRUGO,
307 ade7754_read_24bit,
308 NULL,
309 ADE7754_AVRMS);
310static IIO_DEV_ATTR_BVRMS(S_IRUGO,
311 ade7754_read_24bit,
312 NULL,
313 ADE7754_BVRMS);
314static IIO_DEV_ATTR_CVRMS(S_IRUGO,
315 ade7754_read_24bit,
316 NULL,
317 ADE7754_CVRMS);
318static IIO_DEV_ATTR_AIRMSOS(S_IRUGO,
319 ade7754_read_16bit,
320 ade7754_write_16bit,
321 ADE7754_AIRMSOS);
322static IIO_DEV_ATTR_BIRMSOS(S_IRUGO,
323 ade7754_read_16bit,
324 ade7754_write_16bit,
325 ADE7754_BIRMSOS);
326static IIO_DEV_ATTR_CIRMSOS(S_IRUGO,
327 ade7754_read_16bit,
328 ade7754_write_16bit,
329 ADE7754_CIRMSOS);
330static IIO_DEV_ATTR_AVRMSOS(S_IRUGO,
331 ade7754_read_16bit,
332 ade7754_write_16bit,
333 ADE7754_AVRMSOS);
334static IIO_DEV_ATTR_BVRMSOS(S_IRUGO,
335 ade7754_read_16bit,
336 ade7754_write_16bit,
337 ADE7754_BVRMSOS);
338static IIO_DEV_ATTR_CVRMSOS(S_IRUGO,
339 ade7754_read_16bit,
340 ade7754_write_16bit,
341 ADE7754_CVRMSOS);
342
343static int ade7754_set_irq(struct device *dev, bool enable)
344{
345 int ret;
346 u16 irqen;
Darshana Padmadas1592bfd2014-09-30 20:22:56 +0530347
Barry Song8d97a582010-10-27 21:44:15 -0400348 ret = ade7754_spi_read_reg_16(dev, ADE7754_IRQEN, &irqen);
349 if (ret)
350 goto error_ret;
351
352 if (enable)
353 irqen |= 1 << 14; /* Enables an interrupt when a data is
354 present in the waveform register */
355 else
356 irqen &= ~(1 << 14);
357
358 ret = ade7754_spi_write_reg_16(dev, ADE7754_IRQEN, irqen);
359 if (ret)
360 goto error_ret;
361
362error_ret:
363 return ret;
364}
365
366/* Power down the device */
367static int ade7754_stop_device(struct device *dev)
368{
Devendra Nagaabe4e262015-01-02 04:02:54 -0500369 int ret;
Barry Song8d97a582010-10-27 21:44:15 -0400370 u8 val;
Barry Song8d97a582010-10-27 21:44:15 -0400371
Devendra Nagaabe4e262015-01-02 04:02:54 -0500372 ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
373 if (ret < 0) {
374 dev_err(dev, "unable to power down the device, error: %d",
375 ret);
376 return ret;
377 }
378
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000379 val |= 7 << 3; /* ADE7754 powered down */
380 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
Barry Song8d97a582010-10-27 21:44:15 -0400381}
382
Jonathan Cameron57157d12011-06-27 13:07:49 +0100383static int ade7754_initial_setup(struct iio_dev *indio_dev)
Barry Song8d97a582010-10-27 21:44:15 -0400384{
385 int ret;
Jonathan Cameron57157d12011-06-27 13:07:49 +0100386 struct ade7754_state *st = iio_priv(indio_dev);
387 struct device *dev = &indio_dev->dev;
Barry Song8d97a582010-10-27 21:44:15 -0400388
389 /* use low spi speed for init */
390 st->us->mode = SPI_MODE_3;
391 spi_setup(st->us);
392
393 /* Disable IRQ */
394 ret = ade7754_set_irq(dev, false);
395 if (ret) {
396 dev_err(dev, "disable irq failed");
397 goto err_ret;
398 }
399
400 ade7754_reset(dev);
401 msleep(ADE7754_STARTUP_DELAY);
402
403err_ret:
404 return ret;
405}
406
407static ssize_t ade7754_read_frequency(struct device *dev,
408 struct device_attribute *attr,
409 char *buf)
410{
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000411 int ret;
Barry Song8d97a582010-10-27 21:44:15 -0400412 u8 t;
413 int sps;
Darshana Padmadas1592bfd2014-09-30 20:22:56 +0530414
Barry Song8d97a582010-10-27 21:44:15 -0400415 ret = ade7754_spi_read_reg_8(dev,
416 ADE7754_WAVMODE,
417 &t);
418 if (ret)
419 return ret;
420
421 t = (t >> 3) & 0x3;
422 sps = 26000 / (1 + t);
423
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000424 return sprintf(buf, "%d\n", sps);
Barry Song8d97a582010-10-27 21:44:15 -0400425}
426
427static ssize_t ade7754_write_frequency(struct device *dev,
428 struct device_attribute *attr,
429 const char *buf,
430 size_t len)
431{
Lars-Peter Clausen196b59c2012-05-12 15:39:54 +0200432 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron57157d12011-06-27 13:07:49 +0100433 struct ade7754_state *st = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100434 u16 val;
Barry Song8d97a582010-10-27 21:44:15 -0400435 int ret;
436 u8 reg, t;
437
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100438 ret = kstrtou16(buf, 10, &val);
Barry Song8d97a582010-10-27 21:44:15 -0400439 if (ret)
440 return ret;
Dan Carpenter50d4b302012-08-18 09:48:00 +0100441 if (val == 0)
442 return -EINVAL;
Barry Song8d97a582010-10-27 21:44:15 -0400443
444 mutex_lock(&indio_dev->mlock);
445
Haneen Mohammed5db48512015-03-13 20:47:22 +0300446 t = 26000 / val;
Barry Song8d97a582010-10-27 21:44:15 -0400447 if (t > 0)
448 t--;
449
450 if (t > 1)
451 st->us->max_speed_hz = ADE7754_SPI_SLOW;
452 else
453 st->us->max_speed_hz = ADE7754_SPI_FAST;
454
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000455 ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, &reg);
Barry Song8d97a582010-10-27 21:44:15 -0400456 if (ret)
457 goto out;
458
459 reg &= ~(3 << 3);
460 reg |= t << 3;
461
Jonathan Cameron5a0326d2011-02-11 14:07:41 +0000462 ret = ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg);
Barry Song8d97a582010-10-27 21:44:15 -0400463
464out:
465 mutex_unlock(&indio_dev->mlock);
466
467 return ret ? ret : len;
468}
469static IIO_DEV_ATTR_TEMP_RAW(ade7754_read_8bit);
Jonathan Cameron322c9562011-09-14 13:01:23 +0100470static IIO_CONST_ATTR(in_temp_offset, "129 C");
471static IIO_CONST_ATTR(in_temp_scale, "4 C");
Barry Song8d97a582010-10-27 21:44:15 -0400472
473static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
474 ade7754_read_frequency,
475 ade7754_write_frequency);
476
Barry Song8d97a582010-10-27 21:44:15 -0400477static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000");
478
Barry Song8d97a582010-10-27 21:44:15 -0400479static struct attribute *ade7754_attributes[] = {
Jonathan Cameron322c9562011-09-14 13:01:23 +0100480 &iio_dev_attr_in_temp_raw.dev_attr.attr,
481 &iio_const_attr_in_temp_offset.dev_attr.attr,
482 &iio_const_attr_in_temp_scale.dev_attr.attr,
Barry Song8d97a582010-10-27 21:44:15 -0400483 &iio_dev_attr_sampling_frequency.dev_attr.attr,
484 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Barry Song8d97a582010-10-27 21:44:15 -0400485 &iio_dev_attr_aenergy.dev_attr.attr,
486 &iio_dev_attr_laenergy.dev_attr.attr,
487 &iio_dev_attr_vaenergy.dev_attr.attr,
488 &iio_dev_attr_lvaenergy.dev_attr.attr,
489 &iio_dev_attr_vpeak.dev_attr.attr,
490 &iio_dev_attr_ipeak.dev_attr.attr,
491 &iio_dev_attr_aphcal.dev_attr.attr,
492 &iio_dev_attr_bphcal.dev_attr.attr,
493 &iio_dev_attr_cphcal.dev_attr.attr,
494 &iio_dev_attr_aapos.dev_attr.attr,
495 &iio_dev_attr_bapos.dev_attr.attr,
496 &iio_dev_attr_capos.dev_attr.attr,
497 &iio_dev_attr_wdiv.dev_attr.attr,
498 &iio_dev_attr_vadiv.dev_attr.attr,
499 &iio_dev_attr_cfnum.dev_attr.attr,
500 &iio_dev_attr_cfden.dev_attr.attr,
501 &iio_dev_attr_active_power_a_gain.dev_attr.attr,
502 &iio_dev_attr_active_power_b_gain.dev_attr.attr,
503 &iio_dev_attr_active_power_c_gain.dev_attr.attr,
504 &iio_dev_attr_airms.dev_attr.attr,
505 &iio_dev_attr_birms.dev_attr.attr,
506 &iio_dev_attr_cirms.dev_attr.attr,
507 &iio_dev_attr_avrms.dev_attr.attr,
508 &iio_dev_attr_bvrms.dev_attr.attr,
509 &iio_dev_attr_cvrms.dev_attr.attr,
510 &iio_dev_attr_airmsos.dev_attr.attr,
511 &iio_dev_attr_birmsos.dev_attr.attr,
512 &iio_dev_attr_cirmsos.dev_attr.attr,
513 &iio_dev_attr_avrmsos.dev_attr.attr,
514 &iio_dev_attr_bvrmsos.dev_attr.attr,
515 &iio_dev_attr_cvrmsos.dev_attr.attr,
516 NULL,
517};
518
519static const struct attribute_group ade7754_attribute_group = {
520 .attrs = ade7754_attributes,
521};
522
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100523static const struct iio_info ade7754_info = {
524 .attrs = &ade7754_attribute_group,
525 .driver_module = THIS_MODULE,
526};
Barry Song8d97a582010-10-27 21:44:15 -0400527
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500528static int ade7754_probe(struct spi_device *spi)
Barry Song8d97a582010-10-27 21:44:15 -0400529{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100530 int ret;
Jonathan Cameron57157d12011-06-27 13:07:49 +0100531 struct ade7754_state *st;
532 struct iio_dev *indio_dev;
533
534 /* setup the industrialio driver allocated elements */
Sachin Kamatfde93552013-09-11 10:55:00 +0100535 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
536 if (!indio_dev)
537 return -ENOMEM;
Barry Song8d97a582010-10-27 21:44:15 -0400538 /* this is only used for removal purposes */
Jonathan Cameron57157d12011-06-27 13:07:49 +0100539 spi_set_drvdata(spi, indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400540
Jonathan Cameron57157d12011-06-27 13:07:49 +0100541 st = iio_priv(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400542 st->us = spi;
543 mutex_init(&st->buf_lock);
Barry Song8d97a582010-10-27 21:44:15 -0400544
Jonathan Cameron57157d12011-06-27 13:07:49 +0100545 indio_dev->name = spi->dev.driver->name;
546 indio_dev->dev.parent = &spi->dev;
547 indio_dev->info = &ade7754_info;
548 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song8d97a582010-10-27 21:44:15 -0400549
Barry Song8d97a582010-10-27 21:44:15 -0400550 /* Get the device into a sane initial state */
Jonathan Cameron57157d12011-06-27 13:07:49 +0100551 ret = ade7754_initial_setup(indio_dev);
Barry Song8d97a582010-10-27 21:44:15 -0400552 if (ret)
Cristina Opriceanab72eb702015-03-29 16:14:39 +0300553 goto powerdown_on_error;
554 ret = iio_device_register(indio_dev);
555 if (ret)
556 goto powerdown_on_error;
557 return ret;
558
559powerdown_on_error:
560 ade7754_stop_device(&indio_dev->dev);
561 return ret;
Barry Song8d97a582010-10-27 21:44:15 -0400562}
563
564/* fixme, confirm ordering in this function */
Bill Pemberton447d4f22012-11-19 13:26:37 -0500565static int ade7754_remove(struct spi_device *spi)
Barry Song8d97a582010-10-27 21:44:15 -0400566{
Jonathan Cameron57157d12011-06-27 13:07:49 +0100567 struct iio_dev *indio_dev = spi_get_drvdata(spi);
Barry Song8d97a582010-10-27 21:44:15 -0400568
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100569 iio_device_unregister(indio_dev);
Lars-Peter Clausendb314a12012-09-22 09:56:00 +0100570 ade7754_stop_device(&indio_dev->dev);
Barry Song8d97a582010-10-27 21:44:15 -0400571
Lars-Peter Clausendb314a12012-09-22 09:56:00 +0100572 return 0;
Barry Song8d97a582010-10-27 21:44:15 -0400573}
574
575static struct spi_driver ade7754_driver = {
576 .driver = {
577 .name = "ade7754",
578 .owner = THIS_MODULE,
579 },
580 .probe = ade7754_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500581 .remove = ade7754_remove,
Barry Song8d97a582010-10-27 21:44:15 -0400582};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100583module_spi_driver(ade7754_driver);
Barry Song8d97a582010-10-27 21:44:15 -0400584
585MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
586MODULE_DESCRIPTION("Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver");
587MODULE_LICENSE("GPL v2");
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100588MODULE_ALIAS("spi:ad7754");