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