]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/iio/meter/ade7758_core.c
Merge branch 'x86/urgent' into x86-mm
[mv-sheeva.git] / drivers / staging / iio / meter / ade7758_core.c
1 /*
2  * ADE7758 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>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "meter.h"
24 #include "ade7758.h"
25
26 int ade7758_spi_write_reg_8(struct device *dev,
27                 u8 reg_address,
28                 u8 val)
29 {
30         int ret;
31         struct iio_dev *indio_dev = dev_get_drvdata(dev);
32         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
33
34         mutex_lock(&st->buf_lock);
35         st->tx[0] = ADE7758_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
44 static int ade7758_spi_write_reg_16(struct device *dev,
45                 u8 reg_address,
46                 u16 value)
47 {
48         int ret;
49         struct spi_message msg;
50         struct iio_dev *indio_dev = dev_get_drvdata(dev);
51         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
52         struct spi_transfer xfers[] = {
53                 {
54                         .tx_buf = st->tx,
55                         .bits_per_word = 8,
56                         .len = 3,
57                 }
58         };
59
60         mutex_lock(&st->buf_lock);
61         st->tx[0] = ADE7758_WRITE_REG(reg_address);
62         st->tx[1] = (value >> 8) & 0xFF;
63         st->tx[2] = value & 0xFF;
64
65         spi_message_init(&msg);
66         spi_message_add_tail(xfers, &msg);
67         ret = spi_sync(st->us, &msg);
68         mutex_unlock(&st->buf_lock);
69
70         return ret;
71 }
72
73 static int ade7758_spi_write_reg_24(struct device *dev,
74                 u8 reg_address,
75                 u32 value)
76 {
77         int ret;
78         struct spi_message msg;
79         struct iio_dev *indio_dev = dev_get_drvdata(dev);
80         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
81         struct spi_transfer xfers[] = {
82                 {
83                         .tx_buf = st->tx,
84                         .bits_per_word = 8,
85                         .len = 4,
86                 }
87         };
88
89         mutex_lock(&st->buf_lock);
90         st->tx[0] = ADE7758_WRITE_REG(reg_address);
91         st->tx[1] = (value >> 16) & 0xFF;
92         st->tx[2] = (value >> 8) & 0xFF;
93         st->tx[3] = value & 0xFF;
94
95         spi_message_init(&msg);
96         spi_message_add_tail(xfers, &msg);
97         ret = spi_sync(st->us, &msg);
98         mutex_unlock(&st->buf_lock);
99
100         return ret;
101 }
102
103 static int ade7758_spi_read_reg_8(struct device *dev,
104                 u8 reg_address,
105                 u8 *val)
106 {
107         struct spi_message msg;
108         struct iio_dev *indio_dev = dev_get_drvdata(dev);
109         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
110         int ret;
111         struct spi_transfer xfers[] = {
112                 {
113                         .tx_buf = st->tx,
114                         .rx_buf = st->rx,
115                         .bits_per_word = 8,
116                         .len = 2,
117                 },
118         };
119
120         mutex_lock(&st->buf_lock);
121         st->tx[0] = ADE7758_READ_REG(reg_address);
122         st->tx[1] = 0;
123
124         spi_message_init(&msg);
125         spi_message_add_tail(xfers, &msg);
126         ret = spi_sync(st->us, &msg);
127         if (ret) {
128                 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
129                                 reg_address);
130                 goto error_ret;
131         }
132         *val = st->rx[1];
133
134 error_ret:
135         mutex_unlock(&st->buf_lock);
136         return ret;
137 }
138
139 static int ade7758_spi_read_reg_16(struct device *dev,
140                 u8 reg_address,
141                 u16 *val)
142 {
143         struct spi_message msg;
144         struct iio_dev *indio_dev = dev_get_drvdata(dev);
145         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
146         int ret;
147         struct spi_transfer xfers[] = {
148                 {
149                         .tx_buf = st->tx,
150                         .rx_buf = st->rx,
151                         .bits_per_word = 8,
152                         .len = 3,
153                 },
154         };
155
156         mutex_lock(&st->buf_lock);
157         st->tx[0] = ADE7758_READ_REG(reg_address);
158         st->tx[1] = 0;
159         st->tx[2] = 0;
160
161         spi_message_init(&msg);
162         spi_message_add_tail(xfers, &msg);
163         ret = spi_sync(st->us, &msg);
164         if (ret) {
165                 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
166                                 reg_address);
167                 goto error_ret;
168         }
169         *val = (st->rx[1] << 8) | st->rx[2];
170
171 error_ret:
172         mutex_unlock(&st->buf_lock);
173         return ret;
174 }
175
176 static int ade7758_spi_read_reg_24(struct device *dev,
177                 u8 reg_address,
178                 u32 *val)
179 {
180         struct spi_message msg;
181         struct iio_dev *indio_dev = dev_get_drvdata(dev);
182         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
183         int ret;
184         struct spi_transfer xfers[] = {
185                 {
186                         .tx_buf = st->tx,
187                         .rx_buf = st->rx,
188                         .bits_per_word = 8,
189                         .len = 4,
190                 },
191         };
192
193         mutex_lock(&st->buf_lock);
194         st->tx[0] = ADE7758_READ_REG(reg_address);
195         st->tx[1] = 0;
196         st->tx[2] = 0;
197         st->tx[3] = 0;
198
199         spi_message_init(&msg);
200         spi_message_add_tail(xfers, &msg);
201         ret = spi_sync(st->us, &msg);
202         if (ret) {
203                 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
204                                 reg_address);
205                 goto error_ret;
206         }
207         *val = (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
208
209 error_ret:
210         mutex_unlock(&st->buf_lock);
211         return ret;
212 }
213
214 static ssize_t ade7758_read_8bit(struct device *dev,
215                 struct device_attribute *attr,
216                 char *buf)
217 {
218         int ret;
219         u8 val = 0;
220         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
221
222         ret = ade7758_spi_read_reg_8(dev, this_attr->address, &val);
223         if (ret)
224                 return ret;
225
226         return sprintf(buf, "%u\n", val);
227 }
228
229 static ssize_t ade7758_read_16bit(struct device *dev,
230                 struct device_attribute *attr,
231                 char *buf)
232 {
233         int ret;
234         u16 val = 0;
235         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
236
237         ret = ade7758_spi_read_reg_16(dev, this_attr->address, &val);
238         if (ret)
239                 return ret;
240
241         return sprintf(buf, "%u\n", val);
242 }
243
244 static ssize_t ade7758_read_24bit(struct device *dev,
245                 struct device_attribute *attr,
246                 char *buf)
247 {
248         int ret;
249         u32 val = 0;
250         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
251
252         ret = ade7758_spi_read_reg_24(dev, this_attr->address, &val);
253         if (ret)
254                 return ret;
255
256         return sprintf(buf, "%u\n", val & 0xFFFFFF);
257 }
258
259 static ssize_t ade7758_write_8bit(struct device *dev,
260                 struct device_attribute *attr,
261                 const char *buf,
262                 size_t len)
263 {
264         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
265         int ret;
266         long val;
267
268         ret = strict_strtol(buf, 10, &val);
269         if (ret)
270                 goto error_ret;
271         ret = ade7758_spi_write_reg_8(dev, this_attr->address, val);
272
273 error_ret:
274         return ret ? ret : len;
275 }
276
277 static ssize_t ade7758_write_16bit(struct device *dev,
278                 struct device_attribute *attr,
279                 const char *buf,
280                 size_t len)
281 {
282         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
283         int ret;
284         long val;
285
286         ret = strict_strtol(buf, 10, &val);
287         if (ret)
288                 goto error_ret;
289         ret = ade7758_spi_write_reg_16(dev, this_attr->address, val);
290
291 error_ret:
292         return ret ? ret : len;
293 }
294
295 int ade7758_reset(struct device *dev)
296 {
297         int ret;
298         u8 val;
299         ade7758_spi_read_reg_8(dev,
300                         ADE7758_OPMODE,
301                         &val);
302         val |= 1 << 6; /* Software Chip Reset */
303         ret = ade7758_spi_write_reg_8(dev,
304                         ADE7758_OPMODE,
305                         val);
306
307         return ret;
308 }
309
310 static ssize_t ade7758_write_reset(struct device *dev,
311                 struct device_attribute *attr,
312                 const char *buf, size_t len)
313 {
314         if (len < 1)
315                 return -1;
316         switch (buf[0]) {
317         case '1':
318         case 'y':
319         case 'Y':
320                 return ade7758_reset(dev);
321         }
322         return -1;
323 }
324
325 static IIO_DEV_ATTR_VPEAK(S_IWUSR | S_IRUGO,
326                 ade7758_read_8bit,
327                 ade7758_write_8bit,
328                 ADE7758_VPEAK);
329 static IIO_DEV_ATTR_IPEAK(S_IWUSR | S_IRUGO,
330                 ade7758_read_8bit,
331                 ade7758_write_8bit,
332                 ADE7758_VPEAK);
333 static IIO_DEV_ATTR_APHCAL(S_IWUSR | S_IRUGO,
334                 ade7758_read_8bit,
335                 ade7758_write_8bit,
336                 ADE7758_APHCAL);
337 static IIO_DEV_ATTR_BPHCAL(S_IWUSR | S_IRUGO,
338                 ade7758_read_8bit,
339                 ade7758_write_8bit,
340                 ADE7758_BPHCAL);
341 static IIO_DEV_ATTR_CPHCAL(S_IWUSR | S_IRUGO,
342                 ade7758_read_8bit,
343                 ade7758_write_8bit,
344                 ADE7758_CPHCAL);
345 static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
346                 ade7758_read_8bit,
347                 ade7758_write_8bit,
348                 ADE7758_WDIV);
349 static IIO_DEV_ATTR_VADIV(S_IWUSR | S_IRUGO,
350                 ade7758_read_8bit,
351                 ade7758_write_8bit,
352                 ADE7758_VADIV);
353 static IIO_DEV_ATTR_AIRMS(S_IRUGO,
354                 ade7758_read_24bit,
355                 NULL,
356                 ADE7758_AIRMS);
357 static IIO_DEV_ATTR_BIRMS(S_IRUGO,
358                 ade7758_read_24bit,
359                 NULL,
360                 ADE7758_BIRMS);
361 static IIO_DEV_ATTR_CIRMS(S_IRUGO,
362                 ade7758_read_24bit,
363                 NULL,
364                 ADE7758_CIRMS);
365 static IIO_DEV_ATTR_AVRMS(S_IRUGO,
366                 ade7758_read_24bit,
367                 NULL,
368                 ADE7758_AVRMS);
369 static IIO_DEV_ATTR_BVRMS(S_IRUGO,
370                 ade7758_read_24bit,
371                 NULL,
372                 ADE7758_BVRMS);
373 static IIO_DEV_ATTR_CVRMS(S_IRUGO,
374                 ade7758_read_24bit,
375                 NULL,
376                 ADE7758_CVRMS);
377 static IIO_DEV_ATTR_AIRMSOS(S_IWUSR | S_IRUGO,
378                 ade7758_read_16bit,
379                 ade7758_write_16bit,
380                 ADE7758_AIRMSOS);
381 static IIO_DEV_ATTR_BIRMSOS(S_IWUSR | S_IRUGO,
382                 ade7758_read_16bit,
383                 ade7758_write_16bit,
384                 ADE7758_BIRMSOS);
385 static IIO_DEV_ATTR_CIRMSOS(S_IWUSR | S_IRUGO,
386                 ade7758_read_16bit,
387                 ade7758_write_16bit,
388                 ADE7758_CIRMSOS);
389 static IIO_DEV_ATTR_AVRMSOS(S_IWUSR | S_IRUGO,
390                 ade7758_read_16bit,
391                 ade7758_write_16bit,
392                 ADE7758_AVRMSOS);
393 static IIO_DEV_ATTR_BVRMSOS(S_IWUSR | S_IRUGO,
394                 ade7758_read_16bit,
395                 ade7758_write_16bit,
396                 ADE7758_BVRMSOS);
397 static IIO_DEV_ATTR_CVRMSOS(S_IWUSR | S_IRUGO,
398                 ade7758_read_16bit,
399                 ade7758_write_16bit,
400                 ADE7758_CVRMSOS);
401 static IIO_DEV_ATTR_AIGAIN(S_IWUSR | S_IRUGO,
402                 ade7758_read_16bit,
403                 ade7758_write_16bit,
404                 ADE7758_AIGAIN);
405 static IIO_DEV_ATTR_BIGAIN(S_IWUSR | S_IRUGO,
406                 ade7758_read_16bit,
407                 ade7758_write_16bit,
408                 ADE7758_BIGAIN);
409 static IIO_DEV_ATTR_CIGAIN(S_IWUSR | S_IRUGO,
410                 ade7758_read_16bit,
411                 ade7758_write_16bit,
412                 ADE7758_CIGAIN);
413 static IIO_DEV_ATTR_AVRMSGAIN(S_IWUSR | S_IRUGO,
414                 ade7758_read_16bit,
415                 ade7758_write_16bit,
416                 ADE7758_AVRMSGAIN);
417 static IIO_DEV_ATTR_BVRMSGAIN(S_IWUSR | S_IRUGO,
418                 ade7758_read_16bit,
419                 ade7758_write_16bit,
420                 ADE7758_BVRMSGAIN);
421 static IIO_DEV_ATTR_CVRMSGAIN(S_IWUSR | S_IRUGO,
422                 ade7758_read_16bit,
423                 ade7758_write_16bit,
424                 ADE7758_CVRMSGAIN);
425
426 int ade7758_set_irq(struct device *dev, bool enable)
427 {
428         int ret;
429         u32 irqen;
430         ret = ade7758_spi_read_reg_24(dev, ADE7758_MASK, &irqen);
431         if (ret)
432                 goto error_ret;
433
434         if (enable)
435                 irqen |= 1 << 16; /* Enables an interrupt when a data is
436                                      present in the waveform register */
437         else
438                 irqen &= ~(1 << 16);
439
440         ret = ade7758_spi_write_reg_24(dev, ADE7758_MASK, irqen);
441         if (ret)
442                 goto error_ret;
443
444 error_ret:
445         return ret;
446 }
447
448 /* Power down the device */
449 static int ade7758_stop_device(struct device *dev)
450 {
451         int ret;
452         u8 val;
453         ade7758_spi_read_reg_8(dev,
454                         ADE7758_OPMODE,
455                         &val);
456         val |= 7 << 3;  /* ADE7758 powered down */
457         ret = ade7758_spi_write_reg_8(dev,
458                         ADE7758_OPMODE,
459                         val);
460
461         return ret;
462 }
463
464 static int ade7758_initial_setup(struct ade7758_state *st)
465 {
466         int ret;
467         struct device *dev = &st->indio_dev->dev;
468
469         /* use low spi speed for init */
470         st->us->mode = SPI_MODE_3;
471         spi_setup(st->us);
472
473         /* Disable IRQ */
474         ret = ade7758_set_irq(dev, false);
475         if (ret) {
476                 dev_err(dev, "disable irq failed");
477                 goto err_ret;
478         }
479
480         ade7758_reset(dev);
481         msleep(ADE7758_STARTUP_DELAY);
482
483 err_ret:
484         return ret;
485 }
486
487 static ssize_t ade7758_read_frequency(struct device *dev,
488                 struct device_attribute *attr,
489                 char *buf)
490 {
491         int ret, len = 0;
492         u8 t;
493         int sps;
494         ret = ade7758_spi_read_reg_8(dev,
495                         ADE7758_WAVMODE,
496                         &t);
497         if (ret)
498                 return ret;
499
500         t = (t >> 5) & 0x3;
501         sps = 26040 / (1 << t);
502
503         len = sprintf(buf, "%d SPS\n", sps);
504         return len;
505 }
506
507 static ssize_t ade7758_write_frequency(struct device *dev,
508                 struct device_attribute *attr,
509                 const char *buf,
510                 size_t len)
511 {
512         struct iio_dev *indio_dev = dev_get_drvdata(dev);
513         struct ade7758_state *st = iio_dev_get_devdata(indio_dev);
514         unsigned long val;
515         int ret;
516         u8 reg, t;
517
518         ret = strict_strtol(buf, 10, &val);
519         if (ret)
520                 return ret;
521
522         mutex_lock(&indio_dev->mlock);
523
524         t = (26040 / val);
525         if (t > 0)
526                 t >>= 1;
527
528         if (t > 1)
529                 st->us->max_speed_hz = ADE7758_SPI_SLOW;
530         else
531                 st->us->max_speed_hz = ADE7758_SPI_FAST;
532
533         ret = ade7758_spi_read_reg_8(dev,
534                         ADE7758_WAVMODE,
535                         &reg);
536         if (ret)
537                 goto out;
538
539         reg &= ~(5 << 3);
540         reg |= t << 5;
541
542         ret = ade7758_spi_write_reg_8(dev,
543                         ADE7758_WAVMODE,
544                         reg);
545
546 out:
547         mutex_unlock(&indio_dev->mlock);
548
549         return ret ? ret : len;
550 }
551
552 static ssize_t ade7758_read_waveform_type(struct device *dev,
553                 struct device_attribute *attr,
554                 char *buf)
555 {
556         int ret, len = 0;
557         u8 t;
558         ret = ade7758_spi_read_reg_8(dev,
559                         ADE7758_WAVMODE,
560                         &t);
561         if (ret)
562                 return ret;
563
564         t = (t >> 2) & 0x7;
565
566         len = sprintf(buf, "%d\n", t);
567
568         return len;
569 }
570
571 static ssize_t ade7758_write_waveform_type(struct device *dev,
572                 struct device_attribute *attr,
573                 const char *buf,
574                 size_t len)
575 {
576         struct iio_dev *indio_dev = dev_get_drvdata(dev);
577         unsigned long val;
578         int ret;
579         u8 reg;
580
581         ret = strict_strtol(buf, 10, &val);
582         if (ret)
583                 return ret;
584
585         if (val > 4)
586                 return -EINVAL;
587
588         mutex_lock(&indio_dev->mlock);
589
590         ret = ade7758_spi_read_reg_8(dev,
591                         ADE7758_WAVMODE,
592                         &reg);
593         if (ret)
594                 goto out;
595
596         reg &= ~(7 << 2);
597         reg |= val << 2;
598
599         ret = ade7758_spi_write_reg_8(dev,
600                         ADE7758_WAVMODE,
601                         reg);
602
603 out:
604         mutex_unlock(&indio_dev->mlock);
605
606         return ret ? ret : len;
607 }
608
609 static IIO_DEV_ATTR_TEMP_RAW(ade7758_read_8bit);
610 static IIO_CONST_ATTR(temp_offset, "129 C");
611 static IIO_CONST_ATTR(temp_scale, "4 C");
612
613 static IIO_DEV_ATTR_AWATTHR(ade7758_read_16bit,
614                 ADE7758_AWATTHR);
615 static IIO_DEV_ATTR_BWATTHR(ade7758_read_16bit,
616                 ADE7758_BWATTHR);
617 static IIO_DEV_ATTR_CWATTHR(ade7758_read_16bit,
618                 ADE7758_CWATTHR);
619 static IIO_DEV_ATTR_AVARHR(ade7758_read_16bit,
620                 ADE7758_AVARHR);
621 static IIO_DEV_ATTR_BVARHR(ade7758_read_16bit,
622                 ADE7758_BVARHR);
623 static IIO_DEV_ATTR_CVARHR(ade7758_read_16bit,
624                 ADE7758_CVARHR);
625 static IIO_DEV_ATTR_AVAHR(ade7758_read_16bit,
626                 ADE7758_AVAHR);
627 static IIO_DEV_ATTR_BVAHR(ade7758_read_16bit,
628                 ADE7758_BVAHR);
629 static IIO_DEV_ATTR_CVAHR(ade7758_read_16bit,
630                 ADE7758_CVAHR);
631
632 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
633                 ade7758_read_frequency,
634                 ade7758_write_frequency);
635
636 /**
637  * IIO_DEV_ATTR_WAVEFORM_TYPE - set the type of waveform.
638  * @_mode: sysfs file mode/permissions
639  * @_show: output method for the attribute
640  * @_store: input method for the attribute
641  **/
642 #define IIO_DEV_ATTR_WAVEFORM_TYPE(_mode, _show, _store)                        \
643         IIO_DEVICE_ATTR(waveform_type, _mode, _show, _store, 0)
644
645 static IIO_DEV_ATTR_WAVEFORM_TYPE(S_IWUSR | S_IRUGO,
646                 ade7758_read_waveform_type,
647                 ade7758_write_waveform_type);
648
649 static IIO_DEV_ATTR_RESET(ade7758_write_reset);
650
651 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000");
652
653 static IIO_CONST_ATTR(name, "ade7758");
654
655 static struct attribute *ade7758_event_attributes[] = {
656         NULL
657 };
658
659 static struct attribute_group ade7758_event_attribute_group = {
660         .attrs = ade7758_event_attributes,
661 };
662
663 static struct attribute *ade7758_attributes[] = {
664         &iio_dev_attr_temp_raw.dev_attr.attr,
665         &iio_const_attr_temp_offset.dev_attr.attr,
666         &iio_const_attr_temp_scale.dev_attr.attr,
667         &iio_dev_attr_sampling_frequency.dev_attr.attr,
668         &iio_dev_attr_waveform_type.dev_attr.attr,
669         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
670         &iio_dev_attr_reset.dev_attr.attr,
671         &iio_const_attr_name.dev_attr.attr,
672         &iio_dev_attr_awatthr.dev_attr.attr,
673         &iio_dev_attr_bwatthr.dev_attr.attr,
674         &iio_dev_attr_cwatthr.dev_attr.attr,
675         &iio_dev_attr_avarhr.dev_attr.attr,
676         &iio_dev_attr_bvarhr.dev_attr.attr,
677         &iio_dev_attr_cvarhr.dev_attr.attr,
678         &iio_dev_attr_avahr.dev_attr.attr,
679         &iio_dev_attr_bvahr.dev_attr.attr,
680         &iio_dev_attr_cvahr.dev_attr.attr,
681         &iio_dev_attr_vpeak.dev_attr.attr,
682         &iio_dev_attr_ipeak.dev_attr.attr,
683         &iio_dev_attr_aphcal.dev_attr.attr,
684         &iio_dev_attr_bphcal.dev_attr.attr,
685         &iio_dev_attr_cphcal.dev_attr.attr,
686         &iio_dev_attr_wdiv.dev_attr.attr,
687         &iio_dev_attr_vadiv.dev_attr.attr,
688         &iio_dev_attr_airms.dev_attr.attr,
689         &iio_dev_attr_birms.dev_attr.attr,
690         &iio_dev_attr_cirms.dev_attr.attr,
691         &iio_dev_attr_avrms.dev_attr.attr,
692         &iio_dev_attr_bvrms.dev_attr.attr,
693         &iio_dev_attr_cvrms.dev_attr.attr,
694         &iio_dev_attr_aigain.dev_attr.attr,
695         &iio_dev_attr_bigain.dev_attr.attr,
696         &iio_dev_attr_cigain.dev_attr.attr,
697         &iio_dev_attr_avrmsgain.dev_attr.attr,
698         &iio_dev_attr_bvrmsgain.dev_attr.attr,
699         &iio_dev_attr_cvrmsgain.dev_attr.attr,
700         &iio_dev_attr_airmsos.dev_attr.attr,
701         &iio_dev_attr_birmsos.dev_attr.attr,
702         &iio_dev_attr_cirmsos.dev_attr.attr,
703         &iio_dev_attr_avrmsos.dev_attr.attr,
704         &iio_dev_attr_bvrmsos.dev_attr.attr,
705         &iio_dev_attr_cvrmsos.dev_attr.attr,
706         NULL,
707 };
708
709 static const struct attribute_group ade7758_attribute_group = {
710         .attrs = ade7758_attributes,
711 };
712
713
714
715 static int __devinit ade7758_probe(struct spi_device *spi)
716 {
717         int ret, regdone = 0;
718         struct ade7758_state *st = kzalloc(sizeof *st, GFP_KERNEL);
719         if (!st) {
720                 ret =  -ENOMEM;
721                 goto error_ret;
722         }
723         /* this is only used for removal purposes */
724         spi_set_drvdata(spi, st);
725
726         /* Allocate the comms buffers */
727         st->rx = kzalloc(sizeof(*st->rx)*ADE7758_MAX_RX, GFP_KERNEL);
728         if (st->rx == NULL) {
729                 ret = -ENOMEM;
730                 goto error_free_st;
731         }
732         st->tx = kzalloc(sizeof(*st->tx)*ADE7758_MAX_TX, GFP_KERNEL);
733         if (st->tx == NULL) {
734                 ret = -ENOMEM;
735                 goto error_free_rx;
736         }
737         st->us = spi;
738         mutex_init(&st->buf_lock);
739         /* setup the industrialio driver allocated elements */
740         st->indio_dev = iio_allocate_device();
741         if (st->indio_dev == NULL) {
742                 ret = -ENOMEM;
743                 goto error_free_tx;
744         }
745
746         st->indio_dev->dev.parent = &spi->dev;
747         st->indio_dev->num_interrupt_lines = 1;
748         st->indio_dev->event_attrs = &ade7758_event_attribute_group;
749         st->indio_dev->attrs = &ade7758_attribute_group;
750         st->indio_dev->dev_data = (void *)(st);
751         st->indio_dev->driver_module = THIS_MODULE;
752         st->indio_dev->modes = INDIO_DIRECT_MODE;
753
754         ret = ade7758_configure_ring(st->indio_dev);
755         if (ret)
756                 goto error_free_dev;
757
758         ret = iio_device_register(st->indio_dev);
759         if (ret)
760                 goto error_unreg_ring_funcs;
761         regdone = 1;
762
763         ret = ade7758_initialize_ring(st->indio_dev->ring);
764         if (ret) {
765                 printk(KERN_ERR "failed to initialize the ring\n");
766                 goto error_unreg_ring_funcs;
767         }
768
769         if (spi->irq) {
770                 ret = iio_register_interrupt_line(spi->irq,
771                                 st->indio_dev,
772                                 0,
773                                 IRQF_TRIGGER_FALLING,
774                                 "ade7758");
775                 if (ret)
776                         goto error_uninitialize_ring;
777
778                 ret = ade7758_probe_trigger(st->indio_dev);
779                 if (ret)
780                         goto error_unregister_line;
781         }
782
783         /* Get the device into a sane initial state */
784         ret = ade7758_initial_setup(st);
785         if (ret)
786                 goto error_remove_trigger;
787         return 0;
788
789 error_remove_trigger:
790         if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
791                 ade7758_remove_trigger(st->indio_dev);
792 error_unregister_line:
793         if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
794                 iio_unregister_interrupt_line(st->indio_dev, 0);
795 error_uninitialize_ring:
796         ade7758_uninitialize_ring(st->indio_dev->ring);
797 error_unreg_ring_funcs:
798         ade7758_unconfigure_ring(st->indio_dev);
799 error_free_dev:
800         if (regdone)
801                 iio_device_unregister(st->indio_dev);
802         else
803                 iio_free_device(st->indio_dev);
804 error_free_tx:
805         kfree(st->tx);
806 error_free_rx:
807         kfree(st->rx);
808 error_free_st:
809         kfree(st);
810 error_ret:
811         return ret;
812 }
813
814 static int ade7758_remove(struct spi_device *spi)
815 {
816         int ret;
817         struct ade7758_state *st = spi_get_drvdata(spi);
818         struct iio_dev *indio_dev = st->indio_dev;
819
820         ret = ade7758_stop_device(&(indio_dev->dev));
821         if (ret)
822                 goto err_ret;
823
824         flush_scheduled_work();
825
826         ade7758_remove_trigger(indio_dev);
827         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
828                 iio_unregister_interrupt_line(indio_dev, 0);
829
830         ade7758_uninitialize_ring(indio_dev->ring);
831         iio_device_unregister(indio_dev);
832         ade7758_unconfigure_ring(indio_dev);
833         kfree(st->tx);
834         kfree(st->rx);
835         kfree(st);
836
837         return 0;
838
839 err_ret:
840         return ret;
841 }
842
843 static struct spi_driver ade7758_driver = {
844         .driver = {
845                 .name = "ade7758",
846                 .owner = THIS_MODULE,
847         },
848         .probe = ade7758_probe,
849         .remove = __devexit_p(ade7758_remove),
850 };
851
852 static __init int ade7758_init(void)
853 {
854         return spi_register_driver(&ade7758_driver);
855 }
856 module_init(ade7758_init);
857
858 static __exit void ade7758_exit(void)
859 {
860         spi_unregister_driver(&ade7758_driver);
861 }
862 module_exit(ade7758_exit);
863
864 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
865 MODULE_DESCRIPTION("Analog Devices ADE7758 Polyphase Multifunction Energy Metering IC Driver");
866 MODULE_LICENSE("GPL v2");