2 * ADE7753 Single-Phase Multifunction Metering IC with di/dt Sensor Interface
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
26 static int ade7753_spi_write_reg_8(struct device *dev,
31 struct iio_dev *indio_dev = dev_get_drvdata(dev);
32 struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
34 mutex_lock(&st->buf_lock);
35 st->tx[0] = ADE7753_WRITE_REG(reg_address);
38 ret = spi_write(st->us, st->tx, 2);
39 mutex_unlock(&st->buf_lock);
44 static int ade7753_spi_write_reg_16(struct device *dev,
49 struct iio_dev *indio_dev = dev_get_drvdata(dev);
50 struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
52 mutex_lock(&st->buf_lock);
53 st->tx[0] = ADE7753_WRITE_REG(reg_address);
54 st->tx[1] = (value >> 8) & 0xFF;
55 st->tx[2] = value & 0xFF;
56 ret = spi_write(st->us, st->tx, 3);
57 mutex_unlock(&st->buf_lock);
62 static int ade7753_spi_read_reg_8(struct device *dev,
66 struct iio_dev *indio_dev = dev_get_drvdata(dev);
67 struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
70 ret = spi_w8r8(st->us, ADE7753_READ_REG(reg_address));
72 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
81 static int ade7753_spi_read_reg_16(struct device *dev,
85 struct iio_dev *indio_dev = dev_get_drvdata(dev);
86 struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
89 ret = spi_w8r16(st->us, ADE7753_READ_REG(reg_address));
91 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
97 *val = be16_to_cpup(val);
102 static int ade7753_spi_read_reg_24(struct device *dev,
106 struct spi_message msg;
107 struct iio_dev *indio_dev = dev_get_drvdata(dev);
108 struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
110 struct spi_transfer xfers[] = {
122 mutex_lock(&st->buf_lock);
123 st->tx[0] = ADE7753_READ_REG(reg_address);
125 spi_message_init(&msg);
126 spi_message_add_tail(&xfers[0], &msg);
127 spi_message_add_tail(&xfers[1], &msg);
128 ret = spi_sync(st->us, &msg);
130 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
134 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
137 mutex_unlock(&st->buf_lock);
141 static ssize_t ade7753_read_8bit(struct device *dev,
142 struct device_attribute *attr,
147 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
149 ret = ade7753_spi_read_reg_8(dev, this_attr->address, &val);
153 return sprintf(buf, "%u\n", val);
156 static ssize_t ade7753_read_16bit(struct device *dev,
157 struct device_attribute *attr,
162 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
164 ret = ade7753_spi_read_reg_16(dev, this_attr->address, &val);
168 return sprintf(buf, "%u\n", val);
171 static ssize_t ade7753_read_24bit(struct device *dev,
172 struct device_attribute *attr,
177 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
179 ret = ade7753_spi_read_reg_24(dev, this_attr->address, &val);
183 return sprintf(buf, "%u\n", val);
186 static ssize_t ade7753_write_8bit(struct device *dev,
187 struct device_attribute *attr,
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
195 ret = strict_strtol(buf, 10, &val);
198 ret = ade7753_spi_write_reg_8(dev, this_attr->address, val);
201 return ret ? ret : len;
204 static ssize_t ade7753_write_16bit(struct device *dev,
205 struct device_attribute *attr,
209 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
213 ret = strict_strtol(buf, 10, &val);
216 ret = ade7753_spi_write_reg_16(dev, this_attr->address, val);
219 return ret ? ret : len;
222 static int ade7753_reset(struct device *dev)
226 ade7753_spi_read_reg_16(dev, ADE7753_MODE, &val);
227 val |= 1 << 6; /* Software Chip Reset */
229 return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
232 static ssize_t ade7753_write_reset(struct device *dev,
233 struct device_attribute *attr,
234 const char *buf, size_t len)
242 return ade7753_reset(dev);
247 static IIO_DEV_ATTR_AENERGY(ade7753_read_24bit, ADE7753_AENERGY);
248 static IIO_DEV_ATTR_LAENERGY(ade7753_read_24bit, ADE7753_LAENERGY);
249 static IIO_DEV_ATTR_VAENERGY(ade7753_read_24bit, ADE7753_VAENERGY);
250 static IIO_DEV_ATTR_LVAENERGY(ade7753_read_24bit, ADE7753_LVAENERGY);
251 static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
255 static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
259 static IIO_DEV_ATTR_CHKSUM(ade7753_read_8bit, ADE7753_CHKSUM);
260 static IIO_DEV_ATTR_PHCAL(S_IWUSR | S_IRUGO,
264 static IIO_DEV_ATTR_APOS(S_IWUSR | S_IRUGO,
268 static IIO_DEV_ATTR_SAGCYC(S_IWUSR | S_IRUGO,
272 static IIO_DEV_ATTR_SAGLVL(S_IWUSR | S_IRUGO,
276 static IIO_DEV_ATTR_LINECYC(S_IWUSR | S_IRUGO,
280 static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
284 static IIO_DEV_ATTR_IRMS(S_IWUSR | S_IRUGO,
288 static IIO_DEV_ATTR_VRMS(S_IRUGO,
292 static IIO_DEV_ATTR_IRMSOS(S_IWUSR | S_IRUGO,
296 static IIO_DEV_ATTR_VRMSOS(S_IWUSR | S_IRUGO,
300 static IIO_DEV_ATTR_WGAIN(S_IWUSR | S_IRUGO,
304 static IIO_DEV_ATTR_VAGAIN(S_IWUSR | S_IRUGO,
308 static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR | S_IRUGO,
312 static IIO_DEV_ATTR_IPKLVL(S_IWUSR | S_IRUGO,
316 static IIO_DEV_ATTR_VPKLVL(S_IWUSR | S_IRUGO,
320 static IIO_DEV_ATTR_IPEAK(S_IRUGO,
324 static IIO_DEV_ATTR_VPEAK(S_IRUGO,
328 static IIO_DEV_ATTR_VPERIOD(S_IRUGO,
332 static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR | S_IRUGO,
336 static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR | S_IRUGO,
341 static int ade7753_set_irq(struct device *dev, bool enable)
345 ret = ade7753_spi_read_reg_8(dev, ADE7753_IRQEN, &irqen);
350 irqen |= 1 << 3; /* Enables an interrupt when a data is
351 present in the waveform register */
355 ret = ade7753_spi_write_reg_8(dev, ADE7753_IRQEN, irqen);
361 /* Power down the device */
362 static int ade7753_stop_device(struct device *dev)
366 ade7753_spi_read_reg_16(dev, ADE7753_MODE, &val);
367 val |= 1 << 4; /* AD converters can be turned off */
369 return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
372 static int ade7753_initial_setup(struct ade7753_state *st)
375 struct device *dev = &st->indio_dev->dev;
377 /* use low spi speed for init */
378 st->us->mode = SPI_MODE_3;
382 ret = ade7753_set_irq(dev, false);
384 dev_err(dev, "disable irq failed");
389 msleep(ADE7753_STARTUP_DELAY);
395 static ssize_t ade7753_read_frequency(struct device *dev,
396 struct device_attribute *attr,
402 ret = ade7753_spi_read_reg_8(dev, ADE7753_MODE, &t);
407 sps = 27900 / (1 + t);
409 len = sprintf(buf, "%d\n", sps);
413 static ssize_t ade7753_write_frequency(struct device *dev,
414 struct device_attribute *attr,
418 struct iio_dev *indio_dev = dev_get_drvdata(dev);
419 struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
424 ret = strict_strtol(buf, 10, &val);
428 mutex_lock(&indio_dev->mlock);
435 st->us->max_speed_hz = ADE7753_SPI_SLOW;
437 st->us->max_speed_hz = ADE7753_SPI_FAST;
439 ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, ®);
446 ret = ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg);
449 mutex_unlock(&indio_dev->mlock);
451 return ret ? ret : len;
454 static IIO_DEV_ATTR_TEMP_RAW(ade7753_read_8bit);
455 static IIO_CONST_ATTR(temp_offset, "-25 C");
456 static IIO_CONST_ATTR(temp_scale, "0.67 C");
458 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
459 ade7753_read_frequency,
460 ade7753_write_frequency);
462 static IIO_DEV_ATTR_RESET(ade7753_write_reset);
464 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
466 static IIO_CONST_ATTR(name, "ade7753");
468 static struct attribute *ade7753_attributes[] = {
469 &iio_dev_attr_temp_raw.dev_attr.attr,
470 &iio_const_attr_temp_offset.dev_attr.attr,
471 &iio_const_attr_temp_scale.dev_attr.attr,
472 &iio_dev_attr_sampling_frequency.dev_attr.attr,
473 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
474 &iio_dev_attr_reset.dev_attr.attr,
475 &iio_const_attr_name.dev_attr.attr,
476 &iio_dev_attr_phcal.dev_attr.attr,
477 &iio_dev_attr_cfden.dev_attr.attr,
478 &iio_dev_attr_aenergy.dev_attr.attr,
479 &iio_dev_attr_laenergy.dev_attr.attr,
480 &iio_dev_attr_vaenergy.dev_attr.attr,
481 &iio_dev_attr_lvaenergy.dev_attr.attr,
482 &iio_dev_attr_cfnum.dev_attr.attr,
483 &iio_dev_attr_apos.dev_attr.attr,
484 &iio_dev_attr_sagcyc.dev_attr.attr,
485 &iio_dev_attr_saglvl.dev_attr.attr,
486 &iio_dev_attr_linecyc.dev_attr.attr,
487 &iio_dev_attr_chksum.dev_attr.attr,
488 &iio_dev_attr_pga_gain.dev_attr.attr,
489 &iio_dev_attr_wgain.dev_attr.attr,
490 &iio_dev_attr_choff_1.dev_attr.attr,
491 &iio_dev_attr_choff_2.dev_attr.attr,
492 &iio_dev_attr_wdiv.dev_attr.attr,
493 &iio_dev_attr_irms.dev_attr.attr,
494 &iio_dev_attr_vrms.dev_attr.attr,
495 &iio_dev_attr_irmsos.dev_attr.attr,
496 &iio_dev_attr_vrmsos.dev_attr.attr,
497 &iio_dev_attr_vagain.dev_attr.attr,
498 &iio_dev_attr_ipklvl.dev_attr.attr,
499 &iio_dev_attr_vpklvl.dev_attr.attr,
500 &iio_dev_attr_ipeak.dev_attr.attr,
501 &iio_dev_attr_vpeak.dev_attr.attr,
502 &iio_dev_attr_vperiod.dev_attr.attr,
506 static const struct attribute_group ade7753_attribute_group = {
507 .attrs = ade7753_attributes,
510 static int __devinit ade7753_probe(struct spi_device *spi)
512 int ret, regdone = 0;
513 struct ade7753_state *st = kzalloc(sizeof *st, GFP_KERNEL);
518 /* this is only used for removal purposes */
519 spi_set_drvdata(spi, st);
521 /* Allocate the comms buffers */
522 st->rx = kzalloc(sizeof(*st->rx)*ADE7753_MAX_RX, GFP_KERNEL);
523 if (st->rx == NULL) {
527 st->tx = kzalloc(sizeof(*st->tx)*ADE7753_MAX_TX, GFP_KERNEL);
528 if (st->tx == NULL) {
533 mutex_init(&st->buf_lock);
534 /* setup the industrialio driver allocated elements */
535 st->indio_dev = iio_allocate_device();
536 if (st->indio_dev == NULL) {
541 st->indio_dev->dev.parent = &spi->dev;
542 st->indio_dev->attrs = &ade7753_attribute_group;
543 st->indio_dev->dev_data = (void *)(st);
544 st->indio_dev->driver_module = THIS_MODULE;
545 st->indio_dev->modes = INDIO_DIRECT_MODE;
547 ret = iio_device_register(st->indio_dev);
552 /* Get the device into a sane initial state */
553 ret = ade7753_initial_setup(st);
560 iio_device_unregister(st->indio_dev);
562 iio_free_device(st->indio_dev);
573 /* fixme, confirm ordering in this function */
574 static int ade7753_remove(struct spi_device *spi)
577 struct ade7753_state *st = spi_get_drvdata(spi);
578 struct iio_dev *indio_dev = st->indio_dev;
580 ret = ade7753_stop_device(&(indio_dev->dev));
584 iio_device_unregister(indio_dev);
595 static struct spi_driver ade7753_driver = {
598 .owner = THIS_MODULE,
600 .probe = ade7753_probe,
601 .remove = __devexit_p(ade7753_remove),
604 static __init int ade7753_init(void)
606 return spi_register_driver(&ade7753_driver);
608 module_init(ade7753_init);
610 static __exit void ade7753_exit(void)
612 spi_unregister_driver(&ade7753_driver);
614 module_exit(ade7753_exit);
616 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
617 MODULE_DESCRIPTION("Analog Devices ADE7753/6 Single-Phase Multifunction Meter");
618 MODULE_LICENSE("GPL v2");