Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 1 | /* |
| 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 Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 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> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 20 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 23 | #include "meter.h" |
| 24 | #include "ade7754.h" |
| 25 | |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 26 | static int ade7754_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 27 | { |
| 28 | int ret; |
Lars-Peter Clausen | 196b59c | 2012-05-12 15:39:54 +0200 | [diff] [blame] | 29 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 30 | struct ade7754_state *st = iio_priv(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 31 | |
| 32 | mutex_lock(&st->buf_lock); |
| 33 | st->tx[0] = ADE7754_WRITE_REG(reg_address); |
| 34 | st->tx[1] = val; |
| 35 | |
| 36 | ret = spi_write(st->us, st->tx, 2); |
| 37 | mutex_unlock(&st->buf_lock); |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static int ade7754_spi_write_reg_16(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 43 | u8 reg_address, u16 value) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 44 | { |
| 45 | int ret; |
Lars-Peter Clausen | 196b59c | 2012-05-12 15:39:54 +0200 | [diff] [blame] | 46 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 47 | struct ade7754_state *st = iio_priv(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 48 | |
| 49 | mutex_lock(&st->buf_lock); |
| 50 | st->tx[0] = ADE7754_WRITE_REG(reg_address); |
| 51 | st->tx[1] = (value >> 8) & 0xFF; |
| 52 | st->tx[2] = value & 0xFF; |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 53 | ret = spi_write(st->us, st->tx, 3); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 54 | mutex_unlock(&st->buf_lock); |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 59 | static int ade7754_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 60 | { |
Lars-Peter Clausen | 196b59c | 2012-05-12 15:39:54 +0200 | [diff] [blame] | 61 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 62 | struct ade7754_state *st = iio_priv(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 63 | int ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 64 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 65 | ret = spi_w8r8(st->us, ADE7754_READ_REG(reg_address)); |
| 66 | if (ret < 0) { |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 67 | dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X", |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 68 | reg_address); |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 69 | return ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 70 | } |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 71 | *val = ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 72 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 73 | return 0; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int ade7754_spi_read_reg_16(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 77 | u8 reg_address, u16 *val) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 78 | { |
Lars-Peter Clausen | 196b59c | 2012-05-12 15:39:54 +0200 | [diff] [blame] | 79 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 80 | struct ade7754_state *st = iio_priv(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 81 | int ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 82 | |
Lars-Peter Clausen | 2359074 | 2013-09-27 16:34:29 +0200 | [diff] [blame] | 83 | ret = spi_w8r16be(st->us, ADE7754_READ_REG(reg_address)); |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 84 | if (ret < 0) { |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 85 | dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X", |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 86 | reg_address); |
| 87 | return ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 88 | } |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 89 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 90 | *val = ret; |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 91 | |
| 92 | return 0; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int ade7754_spi_read_reg_24(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 96 | u8 reg_address, u32 *val) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 97 | { |
Lars-Peter Clausen | 196b59c | 2012-05-12 15:39:54 +0200 | [diff] [blame] | 98 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 99 | struct ade7754_state *st = iio_priv(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 100 | int ret; |
| 101 | struct spi_transfer xfers[] = { |
| 102 | { |
| 103 | .tx_buf = st->tx, |
| 104 | .rx_buf = st->rx, |
| 105 | .bits_per_word = 8, |
| 106 | .len = 4, |
| 107 | }, |
| 108 | }; |
| 109 | |
| 110 | mutex_lock(&st->buf_lock); |
| 111 | st->tx[0] = ADE7754_READ_REG(reg_address); |
| 112 | st->tx[1] = 0; |
| 113 | st->tx[2] = 0; |
| 114 | st->tx[3] = 0; |
| 115 | |
Lars-Peter Clausen | ad6c46b | 2013-01-09 17:31:00 +0000 | [diff] [blame] | 116 | ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers)); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 117 | if (ret) { |
| 118 | dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X", |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 119 | reg_address); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 120 | goto error_ret; |
| 121 | } |
| 122 | *val = (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3]; |
| 123 | |
| 124 | error_ret: |
| 125 | mutex_unlock(&st->buf_lock); |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | static ssize_t ade7754_read_8bit(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 130 | struct device_attribute *attr, |
| 131 | char *buf) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 132 | { |
| 133 | int ret; |
| 134 | u8 val = 0; |
| 135 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 136 | |
| 137 | ret = ade7754_spi_read_reg_8(dev, this_attr->address, &val); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | |
| 141 | return sprintf(buf, "%u\n", val); |
| 142 | } |
| 143 | |
| 144 | static ssize_t ade7754_read_16bit(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 145 | struct device_attribute *attr, |
| 146 | char *buf) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 147 | { |
| 148 | int ret; |
| 149 | u16 val = 0; |
| 150 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 151 | |
| 152 | ret = ade7754_spi_read_reg_16(dev, this_attr->address, &val); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | return sprintf(buf, "%u\n", val); |
| 157 | } |
| 158 | |
| 159 | static ssize_t ade7754_read_24bit(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 160 | struct device_attribute *attr, |
| 161 | char *buf) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 162 | { |
| 163 | int ret; |
| 164 | u32 val = 0; |
| 165 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 166 | |
| 167 | ret = ade7754_spi_read_reg_24(dev, this_attr->address, &val); |
| 168 | if (ret) |
| 169 | return ret; |
| 170 | |
| 171 | return sprintf(buf, "%u\n", val & 0xFFFFFF); |
| 172 | } |
| 173 | |
| 174 | static ssize_t ade7754_write_8bit(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 175 | struct device_attribute *attr, |
| 176 | const char *buf, |
| 177 | size_t len) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 178 | { |
| 179 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 180 | int ret; |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 181 | u8 val; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 182 | |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 183 | ret = kstrtou8(buf, 10, &val); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 184 | if (ret) |
| 185 | goto error_ret; |
| 186 | ret = ade7754_spi_write_reg_8(dev, this_attr->address, val); |
| 187 | |
| 188 | error_ret: |
| 189 | return ret ? ret : len; |
| 190 | } |
| 191 | |
| 192 | static ssize_t ade7754_write_16bit(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 193 | struct device_attribute *attr, |
| 194 | const char *buf, |
| 195 | size_t len) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 196 | { |
| 197 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 198 | int ret; |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 199 | u16 val; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 200 | |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 201 | ret = kstrtou16(buf, 10, &val); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 202 | if (ret) |
| 203 | goto error_ret; |
| 204 | ret = ade7754_spi_write_reg_16(dev, this_attr->address, val); |
| 205 | |
| 206 | error_ret: |
| 207 | return ret ? ret : len; |
| 208 | } |
| 209 | |
| 210 | static int ade7754_reset(struct device *dev) |
| 211 | { |
Devendra Naga | abe4e26 | 2015-01-02 04:02:54 -0500 | [diff] [blame] | 212 | int ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 213 | u8 val; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 214 | |
Devendra Naga | abe4e26 | 2015-01-02 04:02:54 -0500 | [diff] [blame] | 215 | ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val); |
| 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | |
Shraddha Barke | b4d8ace | 2015-09-10 22:00:30 +0530 | [diff] [blame] | 219 | val |= BIT(6); /* Software Chip Reset */ |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 220 | return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 223 | static IIO_DEV_ATTR_AENERGY(ade7754_read_24bit, ADE7754_AENERGY); |
| 224 | static IIO_DEV_ATTR_LAENERGY(ade7754_read_24bit, ADE7754_LAENERGY); |
| 225 | static IIO_DEV_ATTR_VAENERGY(ade7754_read_24bit, ADE7754_VAENERGY); |
| 226 | static IIO_DEV_ATTR_LVAENERGY(ade7754_read_24bit, ADE7754_LVAENERGY); |
| 227 | static IIO_DEV_ATTR_VPEAK(S_IWUSR | S_IRUGO, |
| 228 | ade7754_read_8bit, |
| 229 | ade7754_write_8bit, |
| 230 | ADE7754_VPEAK); |
| 231 | static IIO_DEV_ATTR_IPEAK(S_IWUSR | S_IRUGO, |
| 232 | ade7754_read_8bit, |
| 233 | ade7754_write_8bit, |
| 234 | ADE7754_VPEAK); |
| 235 | static IIO_DEV_ATTR_APHCAL(S_IWUSR | S_IRUGO, |
| 236 | ade7754_read_8bit, |
| 237 | ade7754_write_8bit, |
| 238 | ADE7754_APHCAL); |
| 239 | static IIO_DEV_ATTR_BPHCAL(S_IWUSR | S_IRUGO, |
| 240 | ade7754_read_8bit, |
| 241 | ade7754_write_8bit, |
| 242 | ADE7754_BPHCAL); |
| 243 | static IIO_DEV_ATTR_CPHCAL(S_IWUSR | S_IRUGO, |
| 244 | ade7754_read_8bit, |
| 245 | ade7754_write_8bit, |
| 246 | ADE7754_CPHCAL); |
| 247 | static IIO_DEV_ATTR_AAPOS(S_IWUSR | S_IRUGO, |
| 248 | ade7754_read_16bit, |
| 249 | ade7754_write_16bit, |
| 250 | ADE7754_AAPOS); |
| 251 | static IIO_DEV_ATTR_BAPOS(S_IWUSR | S_IRUGO, |
| 252 | ade7754_read_16bit, |
| 253 | ade7754_write_16bit, |
| 254 | ADE7754_BAPOS); |
| 255 | static IIO_DEV_ATTR_CAPOS(S_IWUSR | S_IRUGO, |
| 256 | ade7754_read_16bit, |
| 257 | ade7754_write_16bit, |
| 258 | ADE7754_CAPOS); |
| 259 | static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO, |
| 260 | ade7754_read_8bit, |
| 261 | ade7754_write_8bit, |
| 262 | ADE7754_WDIV); |
| 263 | static IIO_DEV_ATTR_VADIV(S_IWUSR | S_IRUGO, |
| 264 | ade7754_read_8bit, |
| 265 | ade7754_write_8bit, |
| 266 | ADE7754_VADIV); |
| 267 | static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO, |
| 268 | ade7754_read_16bit, |
| 269 | ade7754_write_16bit, |
| 270 | ADE7754_CFNUM); |
| 271 | static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO, |
| 272 | ade7754_read_16bit, |
| 273 | ade7754_write_16bit, |
| 274 | ADE7754_CFDEN); |
| 275 | static IIO_DEV_ATTR_ACTIVE_POWER_A_GAIN(S_IWUSR | S_IRUGO, |
| 276 | ade7754_read_16bit, |
| 277 | ade7754_write_16bit, |
| 278 | ADE7754_AAPGAIN); |
| 279 | static IIO_DEV_ATTR_ACTIVE_POWER_B_GAIN(S_IWUSR | S_IRUGO, |
| 280 | ade7754_read_16bit, |
| 281 | ade7754_write_16bit, |
| 282 | ADE7754_BAPGAIN); |
| 283 | static IIO_DEV_ATTR_ACTIVE_POWER_C_GAIN(S_IWUSR | S_IRUGO, |
| 284 | ade7754_read_16bit, |
| 285 | ade7754_write_16bit, |
| 286 | ADE7754_CAPGAIN); |
| 287 | static IIO_DEV_ATTR_AIRMS(S_IRUGO, |
| 288 | ade7754_read_24bit, |
| 289 | NULL, |
| 290 | ADE7754_AIRMS); |
| 291 | static IIO_DEV_ATTR_BIRMS(S_IRUGO, |
| 292 | ade7754_read_24bit, |
| 293 | NULL, |
| 294 | ADE7754_BIRMS); |
| 295 | static IIO_DEV_ATTR_CIRMS(S_IRUGO, |
| 296 | ade7754_read_24bit, |
| 297 | NULL, |
| 298 | ADE7754_CIRMS); |
| 299 | static IIO_DEV_ATTR_AVRMS(S_IRUGO, |
| 300 | ade7754_read_24bit, |
| 301 | NULL, |
| 302 | ADE7754_AVRMS); |
| 303 | static IIO_DEV_ATTR_BVRMS(S_IRUGO, |
| 304 | ade7754_read_24bit, |
| 305 | NULL, |
| 306 | ADE7754_BVRMS); |
| 307 | static IIO_DEV_ATTR_CVRMS(S_IRUGO, |
| 308 | ade7754_read_24bit, |
| 309 | NULL, |
| 310 | ADE7754_CVRMS); |
| 311 | static IIO_DEV_ATTR_AIRMSOS(S_IRUGO, |
| 312 | ade7754_read_16bit, |
| 313 | ade7754_write_16bit, |
| 314 | ADE7754_AIRMSOS); |
| 315 | static IIO_DEV_ATTR_BIRMSOS(S_IRUGO, |
| 316 | ade7754_read_16bit, |
| 317 | ade7754_write_16bit, |
| 318 | ADE7754_BIRMSOS); |
| 319 | static IIO_DEV_ATTR_CIRMSOS(S_IRUGO, |
| 320 | ade7754_read_16bit, |
| 321 | ade7754_write_16bit, |
| 322 | ADE7754_CIRMSOS); |
| 323 | static IIO_DEV_ATTR_AVRMSOS(S_IRUGO, |
| 324 | ade7754_read_16bit, |
| 325 | ade7754_write_16bit, |
| 326 | ADE7754_AVRMSOS); |
| 327 | static IIO_DEV_ATTR_BVRMSOS(S_IRUGO, |
| 328 | ade7754_read_16bit, |
| 329 | ade7754_write_16bit, |
| 330 | ADE7754_BVRMSOS); |
| 331 | static IIO_DEV_ATTR_CVRMSOS(S_IRUGO, |
| 332 | ade7754_read_16bit, |
| 333 | ade7754_write_16bit, |
| 334 | ADE7754_CVRMSOS); |
| 335 | |
| 336 | static int ade7754_set_irq(struct device *dev, bool enable) |
| 337 | { |
| 338 | int ret; |
| 339 | u16 irqen; |
Darshana Padmadas | 1592bfd | 2014-09-30 20:22:56 +0530 | [diff] [blame] | 340 | |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 341 | ret = ade7754_spi_read_reg_16(dev, ADE7754_IRQEN, &irqen); |
| 342 | if (ret) |
Sandhya Bankar | 78731a0 | 2016-03-06 18:44:59 +0530 | [diff] [blame] | 343 | return ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 344 | |
| 345 | if (enable) |
Shraddha Barke | b4d8ace | 2015-09-10 22:00:30 +0530 | [diff] [blame] | 346 | irqen |= BIT(14); /* Enables an interrupt when a data is |
Alison Schofield | a6634f8 | 2016-03-21 15:39:53 -0700 | [diff] [blame] | 347 | * present in the waveform register |
| 348 | */ |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 349 | else |
Shraddha Barke | b4d8ace | 2015-09-10 22:00:30 +0530 | [diff] [blame] | 350 | irqen &= ~BIT(14); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 351 | |
| 352 | ret = ade7754_spi_write_reg_16(dev, ADE7754_IRQEN, irqen); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 353 | |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | /* Power down the device */ |
| 358 | static int ade7754_stop_device(struct device *dev) |
| 359 | { |
Devendra Naga | abe4e26 | 2015-01-02 04:02:54 -0500 | [diff] [blame] | 360 | int ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 361 | u8 val; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 362 | |
Devendra Naga | abe4e26 | 2015-01-02 04:02:54 -0500 | [diff] [blame] | 363 | ret = ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val); |
| 364 | if (ret < 0) { |
| 365 | dev_err(dev, "unable to power down the device, error: %d", |
| 366 | ret); |
| 367 | return ret; |
| 368 | } |
| 369 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 370 | val |= 7 << 3; /* ADE7754 powered down */ |
| 371 | return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 374 | static int ade7754_initial_setup(struct iio_dev *indio_dev) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 375 | { |
| 376 | int ret; |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 377 | struct ade7754_state *st = iio_priv(indio_dev); |
| 378 | struct device *dev = &indio_dev->dev; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 379 | |
| 380 | /* use low spi speed for init */ |
| 381 | st->us->mode = SPI_MODE_3; |
| 382 | spi_setup(st->us); |
| 383 | |
| 384 | /* Disable IRQ */ |
| 385 | ret = ade7754_set_irq(dev, false); |
| 386 | if (ret) { |
| 387 | dev_err(dev, "disable irq failed"); |
| 388 | goto err_ret; |
| 389 | } |
| 390 | |
| 391 | ade7754_reset(dev); |
| 392 | msleep(ADE7754_STARTUP_DELAY); |
| 393 | |
| 394 | err_ret: |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | static ssize_t ade7754_read_frequency(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 399 | struct device_attribute *attr, |
| 400 | char *buf) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 401 | { |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 402 | int ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 403 | u8 t; |
| 404 | int sps; |
Darshana Padmadas | 1592bfd | 2014-09-30 20:22:56 +0530 | [diff] [blame] | 405 | |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 406 | ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, &t); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 407 | if (ret) |
| 408 | return ret; |
| 409 | |
| 410 | t = (t >> 3) & 0x3; |
| 411 | sps = 26000 / (1 + t); |
| 412 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 413 | return sprintf(buf, "%d\n", sps); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static ssize_t ade7754_write_frequency(struct device *dev, |
Georgiana Rodica Chelu | 748df3b | 2016-09-19 19:35:09 +0300 | [diff] [blame] | 417 | struct device_attribute *attr, |
| 418 | const char *buf, |
| 419 | size_t len) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 420 | { |
Lars-Peter Clausen | 196b59c | 2012-05-12 15:39:54 +0200 | [diff] [blame] | 421 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 422 | struct ade7754_state *st = iio_priv(indio_dev); |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 423 | u16 val; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 424 | int ret; |
| 425 | u8 reg, t; |
| 426 | |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 427 | ret = kstrtou16(buf, 10, &val); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 428 | if (ret) |
| 429 | return ret; |
Cristina Moraru | 062f782 | 2015-10-20 22:55:41 +0300 | [diff] [blame] | 430 | if (!val) |
Dan Carpenter | 50d4b30 | 2012-08-18 09:48:00 +0100 | [diff] [blame] | 431 | return -EINVAL; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 432 | |
| 433 | mutex_lock(&indio_dev->mlock); |
| 434 | |
Haneen Mohammed | 5db4851 | 2015-03-13 20:47:22 +0300 | [diff] [blame] | 435 | t = 26000 / val; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 436 | if (t > 0) |
| 437 | t--; |
| 438 | |
| 439 | if (t > 1) |
| 440 | st->us->max_speed_hz = ADE7754_SPI_SLOW; |
| 441 | else |
| 442 | st->us->max_speed_hz = ADE7754_SPI_FAST; |
| 443 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 444 | ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, ®); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 445 | if (ret) |
| 446 | goto out; |
| 447 | |
| 448 | reg &= ~(3 << 3); |
| 449 | reg |= t << 3; |
| 450 | |
Jonathan Cameron | 5a0326d | 2011-02-11 14:07:41 +0000 | [diff] [blame] | 451 | ret = ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 452 | |
| 453 | out: |
| 454 | mutex_unlock(&indio_dev->mlock); |
| 455 | |
| 456 | return ret ? ret : len; |
| 457 | } |
| 458 | static IIO_DEV_ATTR_TEMP_RAW(ade7754_read_8bit); |
Jonathan Cameron | 322c956 | 2011-09-14 13:01:23 +0100 | [diff] [blame] | 459 | static IIO_CONST_ATTR(in_temp_offset, "129 C"); |
| 460 | static IIO_CONST_ATTR(in_temp_scale, "4 C"); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 461 | |
| 462 | static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, |
| 463 | ade7754_read_frequency, |
| 464 | ade7754_write_frequency); |
| 465 | |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 466 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000"); |
| 467 | |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 468 | static struct attribute *ade7754_attributes[] = { |
Jonathan Cameron | 322c956 | 2011-09-14 13:01:23 +0100 | [diff] [blame] | 469 | &iio_dev_attr_in_temp_raw.dev_attr.attr, |
| 470 | &iio_const_attr_in_temp_offset.dev_attr.attr, |
| 471 | &iio_const_attr_in_temp_scale.dev_attr.attr, |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 472 | &iio_dev_attr_sampling_frequency.dev_attr.attr, |
| 473 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 474 | &iio_dev_attr_aenergy.dev_attr.attr, |
| 475 | &iio_dev_attr_laenergy.dev_attr.attr, |
| 476 | &iio_dev_attr_vaenergy.dev_attr.attr, |
| 477 | &iio_dev_attr_lvaenergy.dev_attr.attr, |
| 478 | &iio_dev_attr_vpeak.dev_attr.attr, |
| 479 | &iio_dev_attr_ipeak.dev_attr.attr, |
| 480 | &iio_dev_attr_aphcal.dev_attr.attr, |
| 481 | &iio_dev_attr_bphcal.dev_attr.attr, |
| 482 | &iio_dev_attr_cphcal.dev_attr.attr, |
| 483 | &iio_dev_attr_aapos.dev_attr.attr, |
| 484 | &iio_dev_attr_bapos.dev_attr.attr, |
| 485 | &iio_dev_attr_capos.dev_attr.attr, |
| 486 | &iio_dev_attr_wdiv.dev_attr.attr, |
| 487 | &iio_dev_attr_vadiv.dev_attr.attr, |
| 488 | &iio_dev_attr_cfnum.dev_attr.attr, |
| 489 | &iio_dev_attr_cfden.dev_attr.attr, |
| 490 | &iio_dev_attr_active_power_a_gain.dev_attr.attr, |
| 491 | &iio_dev_attr_active_power_b_gain.dev_attr.attr, |
| 492 | &iio_dev_attr_active_power_c_gain.dev_attr.attr, |
| 493 | &iio_dev_attr_airms.dev_attr.attr, |
| 494 | &iio_dev_attr_birms.dev_attr.attr, |
| 495 | &iio_dev_attr_cirms.dev_attr.attr, |
| 496 | &iio_dev_attr_avrms.dev_attr.attr, |
| 497 | &iio_dev_attr_bvrms.dev_attr.attr, |
| 498 | &iio_dev_attr_cvrms.dev_attr.attr, |
| 499 | &iio_dev_attr_airmsos.dev_attr.attr, |
| 500 | &iio_dev_attr_birmsos.dev_attr.attr, |
| 501 | &iio_dev_attr_cirmsos.dev_attr.attr, |
| 502 | &iio_dev_attr_avrmsos.dev_attr.attr, |
| 503 | &iio_dev_attr_bvrmsos.dev_attr.attr, |
| 504 | &iio_dev_attr_cvrmsos.dev_attr.attr, |
| 505 | NULL, |
| 506 | }; |
| 507 | |
| 508 | static const struct attribute_group ade7754_attribute_group = { |
| 509 | .attrs = ade7754_attributes, |
| 510 | }; |
| 511 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 512 | static const struct iio_info ade7754_info = { |
| 513 | .attrs = &ade7754_attribute_group, |
| 514 | .driver_module = THIS_MODULE, |
| 515 | }; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 516 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 517 | static int ade7754_probe(struct spi_device *spi) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 518 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 519 | int ret; |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 520 | struct ade7754_state *st; |
| 521 | struct iio_dev *indio_dev; |
| 522 | |
| 523 | /* setup the industrialio driver allocated elements */ |
Sachin Kamat | fde9355 | 2013-09-11 10:55:00 +0100 | [diff] [blame] | 524 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 525 | if (!indio_dev) |
| 526 | return -ENOMEM; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 527 | /* this is only used for removal purposes */ |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 528 | spi_set_drvdata(spi, indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 529 | |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 530 | st = iio_priv(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 531 | st->us = spi; |
| 532 | mutex_init(&st->buf_lock); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 533 | |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 534 | indio_dev->name = spi->dev.driver->name; |
| 535 | indio_dev->dev.parent = &spi->dev; |
| 536 | indio_dev->info = &ade7754_info; |
| 537 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 538 | |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 539 | /* Get the device into a sane initial state */ |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 540 | ret = ade7754_initial_setup(indio_dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 541 | if (ret) |
Cristina Opriceana | b72eb70 | 2015-03-29 16:14:39 +0300 | [diff] [blame] | 542 | goto powerdown_on_error; |
| 543 | ret = iio_device_register(indio_dev); |
| 544 | if (ret) |
| 545 | goto powerdown_on_error; |
| 546 | return ret; |
| 547 | |
| 548 | powerdown_on_error: |
| 549 | ade7754_stop_device(&indio_dev->dev); |
| 550 | return ret; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 551 | } |
| 552 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 553 | static int ade7754_remove(struct spi_device *spi) |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 554 | { |
Jonathan Cameron | 57157d1 | 2011-06-27 13:07:49 +0100 | [diff] [blame] | 555 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 556 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 557 | iio_device_unregister(indio_dev); |
Lars-Peter Clausen | db314a1 | 2012-09-22 09:56:00 +0100 | [diff] [blame] | 558 | ade7754_stop_device(&indio_dev->dev); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 559 | |
Lars-Peter Clausen | db314a1 | 2012-09-22 09:56:00 +0100 | [diff] [blame] | 560 | return 0; |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static struct spi_driver ade7754_driver = { |
| 564 | .driver = { |
| 565 | .name = "ade7754", |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 566 | }, |
| 567 | .probe = ade7754_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 568 | .remove = ade7754_remove, |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 569 | }; |
Lars-Peter Clausen | ae6ae6f | 2011-11-16 10:13:39 +0100 | [diff] [blame] | 570 | module_spi_driver(ade7754_driver); |
Barry Song | 8d97a58 | 2010-10-27 21:44:15 -0400 | [diff] [blame] | 571 | |
| 572 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 573 | MODULE_DESCRIPTION("Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver"); |
| 574 | MODULE_LICENSE("GPL v2"); |
Lars-Peter Clausen | 55e4390 | 2011-11-16 08:53:31 +0100 | [diff] [blame] | 575 | MODULE_ALIAS("spi:ad7754"); |