linux/drivers/staging/iio/meter/ade7759.c
<<
>>
Prefs
   1/*
   2 * ADE7759 Active Energy Metering IC with di/dt Sensor Interface 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/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 "meter.h"
  24
  25#define ADE7759_WAVEFORM  0x01
  26#define ADE7759_AENERGY   0x02
  27#define ADE7759_RSTENERGY 0x03
  28#define ADE7759_STATUS    0x04
  29#define ADE7759_RSTSTATUS 0x05
  30#define ADE7759_MODE      0x06
  31#define ADE7759_CFDEN     0x07
  32#define ADE7759_CH1OS     0x08
  33#define ADE7759_CH2OS     0x09
  34#define ADE7759_GAIN      0x0A
  35#define ADE7759_APGAIN    0x0B
  36#define ADE7759_PHCAL     0x0C
  37#define ADE7759_APOS      0x0D
  38#define ADE7759_ZXTOUT    0x0E
  39#define ADE7759_SAGCYC    0x0F
  40#define ADE7759_IRQEN     0x10
  41#define ADE7759_SAGLVL    0x11
  42#define ADE7759_TEMP      0x12
  43#define ADE7759_LINECYC   0x13
  44#define ADE7759_LENERGY   0x14
  45#define ADE7759_CFNUM     0x15
  46#define ADE7759_CHKSUM    0x1E
  47#define ADE7759_DIEREV    0x1F
  48
  49#define ADE7759_READ_REG(a)    a
  50#define ADE7759_WRITE_REG(a) ((a) | 0x80)
  51
  52#define ADE7759_MAX_TX    6
  53#define ADE7759_MAX_RX    6
  54#define ADE7759_STARTUP_DELAY 1000
  55
  56#define ADE7759_SPI_SLOW        (u32)(300 * 1000)
  57#define ADE7759_SPI_BURST       (u32)(1000 * 1000)
  58#define ADE7759_SPI_FAST        (u32)(2000 * 1000)
  59
  60/**
  61 * struct ade7759_state - device instance specific data
  62 * @us:                 actual spi_device
  63 * @buf_lock:           mutex to protect tx and rx
  64 * @tx:                 transmit buffer
  65 * @rx:                 receive buffer
  66 **/
  67struct ade7759_state {
  68        struct spi_device       *us;
  69        struct mutex            buf_lock;
  70        u8                      tx[ADE7759_MAX_TX] ____cacheline_aligned;
  71        u8                      rx[ADE7759_MAX_RX];
  72};
  73
  74static int ade7759_spi_write_reg_8(struct device *dev,
  75                u8 reg_address,
  76                u8 val)
  77{
  78        int ret;
  79        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  80        struct ade7759_state *st = iio_priv(indio_dev);
  81
  82        mutex_lock(&st->buf_lock);
  83        st->tx[0] = ADE7759_WRITE_REG(reg_address);
  84        st->tx[1] = val;
  85
  86        ret = spi_write(st->us, st->tx, 2);
  87        mutex_unlock(&st->buf_lock);
  88
  89        return ret;
  90}
  91
  92static int ade7759_spi_write_reg_16(struct device *dev,
  93                u8 reg_address,
  94                u16 value)
  95{
  96        int ret;
  97        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  98        struct ade7759_state *st = iio_priv(indio_dev);
  99
 100        mutex_lock(&st->buf_lock);
 101        st->tx[0] = ADE7759_WRITE_REG(reg_address);
 102        st->tx[1] = (value >> 8) & 0xFF;
 103        st->tx[2] = value & 0xFF;
 104        ret = spi_write(st->us, st->tx, 3);
 105        mutex_unlock(&st->buf_lock);
 106
 107        return ret;
 108}
 109
 110static int ade7759_spi_read_reg_8(struct device *dev,
 111                u8 reg_address,
 112                u8 *val)
 113{
 114        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 115        struct ade7759_state *st = iio_priv(indio_dev);
 116        int ret;
 117
 118        ret = spi_w8r8(st->us, ADE7759_READ_REG(reg_address));
 119        if (ret < 0) {
 120                dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
 121                                reg_address);
 122                return ret;
 123        }
 124        *val = ret;
 125
 126        return 0;
 127}
 128
 129static int ade7759_spi_read_reg_16(struct device *dev,
 130                u8 reg_address,
 131                u16 *val)
 132{
 133        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 134        struct ade7759_state *st = iio_priv(indio_dev);
 135        int ret;
 136
 137        ret = spi_w8r16be(st->us, ADE7759_READ_REG(reg_address));
 138        if (ret < 0) {
 139                dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
 140                        reg_address);
 141                return ret;
 142        }
 143
 144        *val = ret;
 145
 146        return 0;
 147}
 148
 149static int ade7759_spi_read_reg_40(struct device *dev,
 150                u8 reg_address,
 151                u64 *val)
 152{
 153        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 154        struct ade7759_state *st = iio_priv(indio_dev);
 155        int ret;
 156        struct spi_transfer xfers[] = {
 157                {
 158                        .tx_buf = st->tx,
 159                        .rx_buf = st->rx,
 160                        .bits_per_word = 8,
 161                        .len = 6,
 162                },
 163        };
 164
 165        mutex_lock(&st->buf_lock);
 166        st->tx[0] = ADE7759_READ_REG(reg_address);
 167        memset(&st->tx[1], 0, 5);
 168
 169        ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
 170        if (ret) {
 171                dev_err(&st->us->dev, "problem when reading 40 bit register 0x%02X",
 172                                reg_address);
 173                goto error_ret;
 174        }
 175        *val = ((u64)st->rx[1] << 32) | ((u64)st->rx[2] << 24) |
 176                (st->rx[3] << 16) | (st->rx[4] << 8) | st->rx[5];
 177
 178error_ret:
 179        mutex_unlock(&st->buf_lock);
 180        return ret;
 181}
 182
 183static ssize_t ade7759_read_8bit(struct device *dev,
 184                struct device_attribute *attr,
 185                char *buf)
 186{
 187        int ret;
 188        u8 val = 0;
 189        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 190
 191        ret = ade7759_spi_read_reg_8(dev, this_attr->address, &val);
 192        if (ret)
 193                return ret;
 194
 195        return sprintf(buf, "%u\n", val);
 196}
 197
 198static ssize_t ade7759_read_16bit(struct device *dev,
 199                struct device_attribute *attr,
 200                char *buf)
 201{
 202        int ret;
 203        u16 val = 0;
 204        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 205
 206        ret = ade7759_spi_read_reg_16(dev, this_attr->address, &val);
 207        if (ret)
 208                return ret;
 209
 210        return sprintf(buf, "%u\n", val);
 211}
 212
 213static ssize_t ade7759_read_40bit(struct device *dev,
 214                struct device_attribute *attr,
 215                char *buf)
 216{
 217        int ret;
 218        u64 val = 0;
 219        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 220
 221        ret = ade7759_spi_read_reg_40(dev, this_attr->address, &val);
 222        if (ret)
 223                return ret;
 224
 225        return sprintf(buf, "%llu\n", val);
 226}
 227
 228static ssize_t ade7759_write_8bit(struct device *dev,
 229                struct device_attribute *attr,
 230                const char *buf,
 231                size_t len)
 232{
 233        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 234        int ret;
 235        u8 val;
 236
 237        ret = kstrtou8(buf, 10, &val);
 238        if (ret)
 239                goto error_ret;
 240        ret = ade7759_spi_write_reg_8(dev, this_attr->address, val);
 241
 242error_ret:
 243        return ret ? ret : len;
 244}
 245
 246static ssize_t ade7759_write_16bit(struct device *dev,
 247                struct device_attribute *attr,
 248                const char *buf,
 249                size_t len)
 250{
 251        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 252        int ret;
 253        u16 val;
 254
 255        ret = kstrtou16(buf, 10, &val);
 256        if (ret)
 257                goto error_ret;
 258        ret = ade7759_spi_write_reg_16(dev, this_attr->address, val);
 259
 260error_ret:
 261        return ret ? ret : len;
 262}
 263
 264static int ade7759_reset(struct device *dev)
 265{
 266        int ret;
 267        u16 val;
 268
 269        ret = ade7759_spi_read_reg_16(dev,
 270                        ADE7759_MODE,
 271                        &val);
 272        if (ret < 0)
 273                return ret;
 274
 275        val |= BIT(6); /* Software Chip Reset */
 276        return ade7759_spi_write_reg_16(dev,
 277                        ADE7759_MODE,
 278                        val);
 279}
 280
 281static IIO_DEV_ATTR_AENERGY(ade7759_read_40bit, ADE7759_AENERGY);
 282static IIO_DEV_ATTR_CFDEN(0644,
 283                ade7759_read_16bit,
 284                ade7759_write_16bit,
 285                ADE7759_CFDEN);
 286static IIO_DEV_ATTR_CFNUM(0644,
 287                ade7759_read_8bit,
 288                ade7759_write_8bit,
 289                ADE7759_CFNUM);
 290static IIO_DEV_ATTR_CHKSUM(ade7759_read_8bit, ADE7759_CHKSUM);
 291static IIO_DEV_ATTR_PHCAL(0644,
 292                ade7759_read_16bit,
 293                ade7759_write_16bit,
 294                ADE7759_PHCAL);
 295static IIO_DEV_ATTR_APOS(0644,
 296                ade7759_read_16bit,
 297                ade7759_write_16bit,
 298                ADE7759_APOS);
 299static IIO_DEV_ATTR_SAGCYC(0644,
 300                ade7759_read_8bit,
 301                ade7759_write_8bit,
 302                ADE7759_SAGCYC);
 303static IIO_DEV_ATTR_SAGLVL(0644,
 304                ade7759_read_8bit,
 305                ade7759_write_8bit,
 306                ADE7759_SAGLVL);
 307static IIO_DEV_ATTR_LINECYC(0644,
 308                ade7759_read_8bit,
 309                ade7759_write_8bit,
 310                ADE7759_LINECYC);
 311static IIO_DEV_ATTR_LENERGY(ade7759_read_40bit, ADE7759_LENERGY);
 312static IIO_DEV_ATTR_PGA_GAIN(0644,
 313                ade7759_read_8bit,
 314                ade7759_write_8bit,
 315                ADE7759_GAIN);
 316static IIO_DEV_ATTR_ACTIVE_POWER_GAIN(0644,
 317                ade7759_read_16bit,
 318                ade7759_write_16bit,
 319                ADE7759_APGAIN);
 320static IIO_DEV_ATTR_CH_OFF(1, 0644,
 321                ade7759_read_8bit,
 322                ade7759_write_8bit,
 323                ADE7759_CH1OS);
 324static IIO_DEV_ATTR_CH_OFF(2, 0644,
 325                ade7759_read_8bit,
 326                ade7759_write_8bit,
 327                ADE7759_CH2OS);
 328
 329static int ade7759_set_irq(struct device *dev, bool enable)
 330{
 331        int ret;
 332        u8 irqen;
 333
 334        ret = ade7759_spi_read_reg_8(dev, ADE7759_IRQEN, &irqen);
 335        if (ret)
 336                goto error_ret;
 337
 338        if (enable)
 339                irqen |= BIT(3); /* Enables an interrupt when a data is
 340                                  * present in the waveform register
 341                                  */
 342        else
 343                irqen &= ~BIT(3);
 344
 345        ret = ade7759_spi_write_reg_8(dev, ADE7759_IRQEN, irqen);
 346
 347error_ret:
 348        return ret;
 349}
 350
 351/* Power down the device */
 352static int ade7759_stop_device(struct device *dev)
 353{
 354        int ret;
 355        u16 val;
 356
 357        ret = ade7759_spi_read_reg_16(dev,
 358                        ADE7759_MODE,
 359                        &val);
 360        if (ret < 0) {
 361                dev_err(dev, "unable to power down the device, error: %d\n",
 362                        ret);
 363                return ret;
 364        }
 365
 366        val |= BIT(4);  /* AD converters can be turned off */
 367
 368        return ade7759_spi_write_reg_16(dev, ADE7759_MODE, val);
 369}
 370
 371static int ade7759_initial_setup(struct iio_dev *indio_dev)
 372{
 373        int ret;
 374        struct ade7759_state *st = iio_priv(indio_dev);
 375        struct device *dev = &indio_dev->dev;
 376
 377        /* use low spi speed for init */
 378        st->us->mode = SPI_MODE_3;
 379        spi_setup(st->us);
 380
 381        /* Disable IRQ */
 382        ret = ade7759_set_irq(dev, false);
 383        if (ret) {
 384                dev_err(dev, "disable irq failed");
 385                goto err_ret;
 386        }
 387
 388        ade7759_reset(dev);
 389        usleep_range(ADE7759_STARTUP_DELAY, ADE7759_STARTUP_DELAY + 100);
 390
 391err_ret:
 392        return ret;
 393}
 394
 395static ssize_t ade7759_read_frequency(struct device *dev,
 396                struct device_attribute *attr,
 397                char *buf)
 398{
 399        int ret;
 400        u16 t;
 401        int sps;
 402
 403        ret = ade7759_spi_read_reg_16(dev,
 404                        ADE7759_MODE,
 405                        &t);
 406        if (ret)
 407                return ret;
 408
 409        t = (t >> 3) & 0x3;
 410        sps = 27900 / (1 + t);
 411
 412        return sprintf(buf, "%d\n", sps);
 413}
 414
 415static ssize_t ade7759_write_frequency(struct device *dev,
 416                struct device_attribute *attr,
 417                const char *buf,
 418                size_t len)
 419{
 420        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 421        struct ade7759_state *st = iio_priv(indio_dev);
 422        u16 val;
 423        int ret;
 424        u16 reg, t;
 425
 426        ret = kstrtou16(buf, 10, &val);
 427        if (ret)
 428                return ret;
 429        if (!val)
 430                return -EINVAL;
 431
 432        mutex_lock(&indio_dev->mlock);
 433
 434        t = 27900 / val;
 435        if (t > 0)
 436                t--;
 437
 438        if (t > 1)
 439                st->us->max_speed_hz = ADE7759_SPI_SLOW;
 440        else
 441                st->us->max_speed_hz = ADE7759_SPI_FAST;
 442
 443        ret = ade7759_spi_read_reg_16(dev, ADE7759_MODE, &reg);
 444        if (ret)
 445                goto out;
 446
 447        reg &= ~(3 << 13);
 448        reg |= t << 13;
 449
 450        ret = ade7759_spi_write_reg_16(dev, ADE7759_MODE, reg);
 451
 452out:
 453        mutex_unlock(&indio_dev->mlock);
 454
 455        return ret ? ret : len;
 456}
 457static IIO_DEV_ATTR_TEMP_RAW(ade7759_read_8bit);
 458static IIO_CONST_ATTR(in_temp_offset, "70 C");
 459static IIO_CONST_ATTR(in_temp_scale, "1 C");
 460
 461static IIO_DEV_ATTR_SAMP_FREQ(0644,
 462                ade7759_read_frequency,
 463                ade7759_write_frequency);
 464
 465static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
 466
 467static struct attribute *ade7759_attributes[] = {
 468        &iio_dev_attr_in_temp_raw.dev_attr.attr,
 469        &iio_const_attr_in_temp_offset.dev_attr.attr,
 470        &iio_const_attr_in_temp_scale.dev_attr.attr,
 471        &iio_dev_attr_sampling_frequency.dev_attr.attr,
 472        &iio_const_attr_sampling_frequency_available.dev_attr.attr,
 473        &iio_dev_attr_phcal.dev_attr.attr,
 474        &iio_dev_attr_cfden.dev_attr.attr,
 475        &iio_dev_attr_aenergy.dev_attr.attr,
 476        &iio_dev_attr_cfnum.dev_attr.attr,
 477        &iio_dev_attr_apos.dev_attr.attr,
 478        &iio_dev_attr_sagcyc.dev_attr.attr,
 479        &iio_dev_attr_saglvl.dev_attr.attr,
 480        &iio_dev_attr_linecyc.dev_attr.attr,
 481        &iio_dev_attr_lenergy.dev_attr.attr,
 482        &iio_dev_attr_chksum.dev_attr.attr,
 483        &iio_dev_attr_pga_gain.dev_attr.attr,
 484        &iio_dev_attr_active_power_gain.dev_attr.attr,
 485        &iio_dev_attr_choff_1.dev_attr.attr,
 486        &iio_dev_attr_choff_2.dev_attr.attr,
 487        NULL,
 488};
 489
 490static const struct attribute_group ade7759_attribute_group = {
 491        .attrs = ade7759_attributes,
 492};
 493
 494static const struct iio_info ade7759_info = {
 495        .attrs = &ade7759_attribute_group,
 496        .driver_module = THIS_MODULE,
 497};
 498
 499static int ade7759_probe(struct spi_device *spi)
 500{
 501        int ret;
 502        struct ade7759_state *st;
 503        struct iio_dev *indio_dev;
 504
 505        /* setup the industrialio driver allocated elements */
 506        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 507        if (!indio_dev)
 508                return -ENOMEM;
 509        /* this is only used for removal purposes */
 510        spi_set_drvdata(spi, indio_dev);
 511
 512        st = iio_priv(indio_dev);
 513        st->us = spi;
 514        mutex_init(&st->buf_lock);
 515        indio_dev->name = spi->dev.driver->name;
 516        indio_dev->dev.parent = &spi->dev;
 517        indio_dev->info = &ade7759_info;
 518        indio_dev->modes = INDIO_DIRECT_MODE;
 519
 520        /* Get the device into a sane initial state */
 521        ret = ade7759_initial_setup(indio_dev);
 522        if (ret)
 523                return ret;
 524
 525        return iio_device_register(indio_dev);
 526}
 527
 528static int ade7759_remove(struct spi_device *spi)
 529{
 530        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 531
 532        iio_device_unregister(indio_dev);
 533        ade7759_stop_device(&indio_dev->dev);
 534
 535        return 0;
 536}
 537
 538static struct spi_driver ade7759_driver = {
 539        .driver = {
 540                .name = "ade7759",
 541        },
 542        .probe = ade7759_probe,
 543        .remove = ade7759_remove,
 544};
 545module_spi_driver(ade7759_driver);
 546
 547MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 548MODULE_DESCRIPTION("Analog Devices ADE7759 Active Energy Metering IC Driver");
 549MODULE_LICENSE("GPL v2");
 550MODULE_ALIAS("spi:ad7759");
 551