2 * ADE7754 Polyphase Multifunction Energy Metering IC Driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/spi/spi.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
24 #define ADE7754_AENERGY 0x01
25 #define ADE7754_RAENERGY 0x02
26 #define ADE7754_LAENERGY 0x03
27 #define ADE7754_VAENERGY 0x04
28 #define ADE7754_RVAENERGY 0x05
29 #define ADE7754_LVAENERGY 0x06
30 #define ADE7754_PERIOD 0x07
31 #define ADE7754_TEMP 0x08
32 #define ADE7754_WFORM 0x09
33 #define ADE7754_OPMODE 0x0A
34 #define ADE7754_MMODE 0x0B
35 #define ADE7754_WAVMODE 0x0C
36 #define ADE7754_WATMODE 0x0D
37 #define ADE7754_VAMODE 0x0E
38 #define ADE7754_IRQEN 0x0F
39 #define ADE7754_STATUS 0x10
40 #define ADE7754_RSTATUS 0x11
41 #define ADE7754_ZXTOUT 0x12
42 #define ADE7754_LINCYC 0x13
43 #define ADE7754_SAGCYC 0x14
44 #define ADE7754_SAGLVL 0x15
45 #define ADE7754_VPEAK 0x16
46 #define ADE7754_IPEAK 0x17
47 #define ADE7754_GAIN 0x18
48 #define ADE7754_AWG 0x19
49 #define ADE7754_BWG 0x1A
50 #define ADE7754_CWG 0x1B
51 #define ADE7754_AVAG 0x1C
52 #define ADE7754_BVAG 0x1D
53 #define ADE7754_CVAG 0x1E
54 #define ADE7754_APHCAL 0x1F
55 #define ADE7754_BPHCAL 0x20
56 #define ADE7754_CPHCAL 0x21
57 #define ADE7754_AAPOS 0x22
58 #define ADE7754_BAPOS 0x23
59 #define ADE7754_CAPOS 0x24
60 #define ADE7754_CFNUM 0x25
61 #define ADE7754_CFDEN 0x26
62 #define ADE7754_WDIV 0x27
63 #define ADE7754_VADIV 0x28
64 #define ADE7754_AIRMS 0x29
65 #define ADE7754_BIRMS 0x2A
66 #define ADE7754_CIRMS 0x2B
67 #define ADE7754_AVRMS 0x2C
68 #define ADE7754_BVRMS 0x2D
69 #define ADE7754_CVRMS 0x2E
70 #define ADE7754_AIRMSOS 0x2F
71 #define ADE7754_BIRMSOS 0x30
72 #define ADE7754_CIRMSOS 0x31
73 #define ADE7754_AVRMSOS 0x32
74 #define ADE7754_BVRMSOS 0x33
75 #define ADE7754_CVRMSOS 0x34
76 #define ADE7754_AAPGAIN 0x35
77 #define ADE7754_BAPGAIN 0x36
78 #define ADE7754_CAPGAIN 0x37
79 #define ADE7754_AVGAIN 0x38
80 #define ADE7754_BVGAIN 0x39
81 #define ADE7754_CVGAIN 0x3A
82 #define ADE7754_CHKSUM 0x3E
83 #define ADE7754_VERSION 0x3F
85 #define ADE7754_READ_REG(a) a
86 #define ADE7754_WRITE_REG(a) ((a) | 0x80)
88 #define ADE7754_MAX_TX 4
89 #define ADE7754_MAX_RX 4
90 #define ADE7754_STARTUP_DELAY 1000
92 #define ADE7754_SPI_SLOW (u32)(300 * 1000)
93 #define ADE7754_SPI_BURST (u32)(1000 * 1000)
94 #define ADE7754_SPI_FAST (u32)(2000 * 1000)
97 * struct ade7754_state - device instance specific data
98 * @us: actual spi_device
99 * @buf_lock: mutex to protect tx, rx and write frequency
100 * @tx: transmit buffer
101 * @rx: receive buffer
103 struct ade7754_state {
104 struct spi_device *us;
105 struct mutex buf_lock;
106 u8 tx[ADE7754_MAX_TX] ____cacheline_aligned;
107 u8 rx[ADE7754_MAX_RX];
110 /* Unlocked version of ade7754_spi_write_reg_8 function */
111 static int __ade7754_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val)
113 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
114 struct ade7754_state *st = iio_priv(indio_dev);
116 st->tx[0] = ADE7754_WRITE_REG(reg_address);
118 return spi_write(st->us, st->tx, 2);
121 static int ade7754_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val)
124 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
125 struct ade7754_state *st = iio_priv(indio_dev);
127 mutex_lock(&st->buf_lock);
128 ret = __ade7754_spi_write_reg_8(dev, reg_address, val);
129 mutex_unlock(&st->buf_lock);
134 static int ade7754_spi_write_reg_16(struct device *dev,
135 u8 reg_address, u16 value)
138 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
139 struct ade7754_state *st = iio_priv(indio_dev);
141 mutex_lock(&st->buf_lock);
142 st->tx[0] = ADE7754_WRITE_REG(reg_address);
143 st->tx[1] = (value >> 8) & 0xFF;
144 st->tx[2] = value & 0xFF;
145 ret = spi_write(st->us, st->tx, 3);
146 mutex_unlock(&st->buf_lock);
151 static int ade7754_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val)
153 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
154 struct ade7754_state *st = iio_priv(indio_dev);
157 ret = spi_w8r8(st->us, ADE7754_READ_REG(reg_address));
159 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
168 static int ade7754_spi_read_reg_16(struct device *dev,
169 u8 reg_address, u16 *val)
171 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
172 struct ade7754_state *st = iio_priv(indio_dev);
175 ret = spi_w8r16be(st->us, ADE7754_READ_REG(reg_address));
177 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
187 static int ade7754_spi_read_reg_24(struct device *dev,
188 u8 reg_address, u32 *val)
190 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
191 struct ade7754_state *st = iio_priv(indio_dev);
193 struct spi_transfer xfers[] = {
202 mutex_lock(&st->buf_lock);
203 st->tx[0] = ADE7754_READ_REG(reg_address);
208 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
210 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
214 *val = (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
217 mutex_unlock(&st->buf_lock);
221 static ssize_t ade7754_read_8bit(struct device *dev,
222 struct device_attribute *attr,
227 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
229 ret = ade7754_spi_read_reg_8(dev, this_attr->address, &val);
233 return sprintf(buf, "%u\n", val);
236 static ssize_t ade7754_read_16bit(struct device *dev,
237 struct device_attribute *attr,
242 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
244 ret = ade7754_spi_read_reg_16(dev, this_attr->address, &val);
248 return sprintf(buf, "%u\n", val);
251 static ssize_t ade7754_read_24bit(struct device *dev,
252 struct device_attribute *attr,
257 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
259 ret = ade7754_spi_read_reg_24(dev, this_attr->address, &val);
263 return sprintf(buf, "%u\n", val & 0xFFFFFF);
266 static ssize_t ade7754_write_8bit(struct device *dev,
267 struct device_attribute *attr,
271 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
275 ret = kstrtou8(buf, 10, &val);
278 ret = ade7754_spi_write_reg_8(dev, this_attr->address, val);
281 return ret ? ret : len;
284 static ssize_t ade7754_write_16bit(struct device *dev,
285 struct device_attribute *attr,
289 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
293 ret = kstrtou16(buf, 10, &val);
296 ret = ade7754_spi_write_reg_16(dev, this_attr->address, val);
299 return ret ? ret : len;
302 static int ade7754_reset(struct device *dev)
307 ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
311 val |= BIT(6); /* Software Chip Reset */
312 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
315 static IIO_DEV_ATTR_AENERGY(ade7754_read_24bit, ADE7754_AENERGY);
316 static IIO_DEV_ATTR_LAENERGY(ade7754_read_24bit, ADE7754_LAENERGY);
317 static IIO_DEV_ATTR_VAENERGY(ade7754_read_24bit, ADE7754_VAENERGY);
318 static IIO_DEV_ATTR_LVAENERGY(ade7754_read_24bit, ADE7754_LVAENERGY);
319 static IIO_DEV_ATTR_VPEAK(0644,
323 static IIO_DEV_ATTR_IPEAK(0644,
327 static IIO_DEV_ATTR_APHCAL(0644,
331 static IIO_DEV_ATTR_BPHCAL(0644,
335 static IIO_DEV_ATTR_CPHCAL(0644,
339 static IIO_DEV_ATTR_AAPOS(0644,
343 static IIO_DEV_ATTR_BAPOS(0644,
347 static IIO_DEV_ATTR_CAPOS(0644,
351 static IIO_DEV_ATTR_WDIV(0644,
355 static IIO_DEV_ATTR_VADIV(0644,
359 static IIO_DEV_ATTR_CFNUM(0644,
363 static IIO_DEV_ATTR_CFDEN(0644,
367 static IIO_DEV_ATTR_ACTIVE_POWER_A_GAIN(0644,
371 static IIO_DEV_ATTR_ACTIVE_POWER_B_GAIN(0644,
375 static IIO_DEV_ATTR_ACTIVE_POWER_C_GAIN(0644,
379 static IIO_DEV_ATTR_AIRMS(0444,
383 static IIO_DEV_ATTR_BIRMS(0444,
387 static IIO_DEV_ATTR_CIRMS(0444,
391 static IIO_DEV_ATTR_AVRMS(0444,
395 static IIO_DEV_ATTR_BVRMS(0444,
399 static IIO_DEV_ATTR_CVRMS(0444,
403 static IIO_DEV_ATTR_AIRMSOS(0444,
407 static IIO_DEV_ATTR_BIRMSOS(0444,
411 static IIO_DEV_ATTR_CIRMSOS(0444,
415 static IIO_DEV_ATTR_AVRMSOS(0444,
419 static IIO_DEV_ATTR_BVRMSOS(0444,
423 static IIO_DEV_ATTR_CVRMSOS(0444,
428 static int ade7754_set_irq(struct device *dev, bool enable)
433 ret = ade7754_spi_read_reg_16(dev, ADE7754_IRQEN, &irqen);
438 irqen |= BIT(14); /* Enables an interrupt when a data is
439 * present in the waveform register
444 return ade7754_spi_write_reg_16(dev, ADE7754_IRQEN, irqen);
447 /* Power down the device */
448 static int ade7754_stop_device(struct device *dev)
453 ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
455 dev_err(dev, "unable to power down the device, error: %d",
460 val |= 7 << 3; /* ADE7754 powered down */
461 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
464 static int ade7754_initial_setup(struct iio_dev *indio_dev)
467 struct ade7754_state *st = iio_priv(indio_dev);
468 struct device *dev = &indio_dev->dev;
470 /* use low spi speed for init */
471 st->us->mode = SPI_MODE_3;
475 ret = ade7754_set_irq(dev, false);
477 dev_err(dev, "disable irq failed");
482 usleep_range(ADE7754_STARTUP_DELAY, ADE7754_STARTUP_DELAY + 100);
488 static ssize_t ade7754_read_frequency(struct device *dev,
489 struct device_attribute *attr,
496 ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, &t);
501 sps = 26000 / (1 + t);
503 return sprintf(buf, "%d\n", sps);
506 static ssize_t ade7754_write_frequency(struct device *dev,
507 struct device_attribute *attr,
511 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
512 struct ade7754_state *st = iio_priv(indio_dev);
517 ret = kstrtou16(buf, 10, &val);
523 mutex_lock(&st->buf_lock);
530 st->us->max_speed_hz = ADE7754_SPI_SLOW;
532 st->us->max_speed_hz = ADE7754_SPI_FAST;
534 ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, ®);
541 ret = __ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg);
544 mutex_unlock(&st->buf_lock);
546 return ret ? ret : len;
548 static IIO_DEV_ATTR_TEMP_RAW(ade7754_read_8bit);
549 static IIO_CONST_ATTR(in_temp_offset, "129 C");
550 static IIO_CONST_ATTR(in_temp_scale, "4 C");
552 static IIO_DEV_ATTR_SAMP_FREQ(0644,
553 ade7754_read_frequency,
554 ade7754_write_frequency);
556 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000");
558 static struct attribute *ade7754_attributes[] = {
559 &iio_dev_attr_in_temp_raw.dev_attr.attr,
560 &iio_const_attr_in_temp_offset.dev_attr.attr,
561 &iio_const_attr_in_temp_scale.dev_attr.attr,
562 &iio_dev_attr_sampling_frequency.dev_attr.attr,
563 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
564 &iio_dev_attr_aenergy.dev_attr.attr,
565 &iio_dev_attr_laenergy.dev_attr.attr,
566 &iio_dev_attr_vaenergy.dev_attr.attr,
567 &iio_dev_attr_lvaenergy.dev_attr.attr,
568 &iio_dev_attr_vpeak.dev_attr.attr,
569 &iio_dev_attr_ipeak.dev_attr.attr,
570 &iio_dev_attr_aphcal.dev_attr.attr,
571 &iio_dev_attr_bphcal.dev_attr.attr,
572 &iio_dev_attr_cphcal.dev_attr.attr,
573 &iio_dev_attr_aapos.dev_attr.attr,
574 &iio_dev_attr_bapos.dev_attr.attr,
575 &iio_dev_attr_capos.dev_attr.attr,
576 &iio_dev_attr_wdiv.dev_attr.attr,
577 &iio_dev_attr_vadiv.dev_attr.attr,
578 &iio_dev_attr_cfnum.dev_attr.attr,
579 &iio_dev_attr_cfden.dev_attr.attr,
580 &iio_dev_attr_active_power_a_gain.dev_attr.attr,
581 &iio_dev_attr_active_power_b_gain.dev_attr.attr,
582 &iio_dev_attr_active_power_c_gain.dev_attr.attr,
583 &iio_dev_attr_airms.dev_attr.attr,
584 &iio_dev_attr_birms.dev_attr.attr,
585 &iio_dev_attr_cirms.dev_attr.attr,
586 &iio_dev_attr_avrms.dev_attr.attr,
587 &iio_dev_attr_bvrms.dev_attr.attr,
588 &iio_dev_attr_cvrms.dev_attr.attr,
589 &iio_dev_attr_airmsos.dev_attr.attr,
590 &iio_dev_attr_birmsos.dev_attr.attr,
591 &iio_dev_attr_cirmsos.dev_attr.attr,
592 &iio_dev_attr_avrmsos.dev_attr.attr,
593 &iio_dev_attr_bvrmsos.dev_attr.attr,
594 &iio_dev_attr_cvrmsos.dev_attr.attr,
598 static const struct attribute_group ade7754_attribute_group = {
599 .attrs = ade7754_attributes,
602 static const struct iio_info ade7754_info = {
603 .attrs = &ade7754_attribute_group,
604 .driver_module = THIS_MODULE,
607 static int ade7754_probe(struct spi_device *spi)
610 struct ade7754_state *st;
611 struct iio_dev *indio_dev;
613 /* setup the industrialio driver allocated elements */
614 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
617 /* this is only used for removal purposes */
618 spi_set_drvdata(spi, indio_dev);
620 st = iio_priv(indio_dev);
622 mutex_init(&st->buf_lock);
624 indio_dev->name = spi->dev.driver->name;
625 indio_dev->dev.parent = &spi->dev;
626 indio_dev->info = &ade7754_info;
627 indio_dev->modes = INDIO_DIRECT_MODE;
629 /* Get the device into a sane initial state */
630 ret = ade7754_initial_setup(indio_dev);
632 goto powerdown_on_error;
633 ret = iio_device_register(indio_dev);
635 goto powerdown_on_error;
639 ade7754_stop_device(&indio_dev->dev);
643 static int ade7754_remove(struct spi_device *spi)
645 struct iio_dev *indio_dev = spi_get_drvdata(spi);
647 iio_device_unregister(indio_dev);
648 ade7754_stop_device(&indio_dev->dev);
653 static struct spi_driver ade7754_driver = {
657 .probe = ade7754_probe,
658 .remove = ade7754_remove,
660 module_spi_driver(ade7754_driver);
662 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
663 MODULE_DESCRIPTION("Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver");
664 MODULE_LICENSE("GPL v2");
665 MODULE_ALIAS("spi:ad7754");