13 #include <linux/device.h>
14 #include <linux/kernel.h>
16 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
26 static int ade7754_spi_write_reg_8(
struct device *
dev,
31 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
38 ret = spi_write(st->
us, st->tx, 2);
44 static int ade7754_spi_write_reg_16(
struct device *
dev,
49 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
54 st->tx[1] = (value >> 8) & 0xFF;
55 st->tx[2] = value & 0xFF;
56 ret = spi_write(st->
us, st->tx, 3);
62 static int ade7754_spi_read_reg_8(
struct device *
dev,
66 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
72 dev_err(&st->
us->dev,
"problem when reading 8 bit register 0x%02X",
81 static int ade7754_spi_read_reg_16(
struct device *dev,
85 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
91 dev_err(&st->
us->dev,
"problem when reading 16 bit register 0x%02X",
102 static int ade7754_spi_read_reg_24(
struct device *dev,
107 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
125 spi_message_init(&
msg);
126 spi_message_add_tail(xfers, &
msg);
129 dev_err(&st->
us->dev,
"problem when reading 24 bit register 0x%02X",
133 *val = (st->
rx[1] << 16) | (st->
rx[2] << 8) | st->
rx[3];
148 ret = ade7754_spi_read_reg_8(dev, this_attr->
address, &val);
152 return sprintf(buf,
"%u\n", val);
163 ret = ade7754_spi_read_reg_16(dev, this_attr->
address, &val);
167 return sprintf(buf,
"%u\n", val);
178 ret = ade7754_spi_read_reg_24(dev, this_attr->
address, &val);
182 return sprintf(buf,
"%u\n", val & 0xFFFFFF);
197 ret = ade7754_spi_write_reg_8(dev, this_attr->
address, val);
200 return ret ? ret : len;
215 ret = ade7754_spi_write_reg_16(dev, this_attr->
address, val);
218 return ret ? ret : len;
221 static int ade7754_reset(
struct device *dev)
233 const char *buf,
size_t len)
241 return ade7754_reset(dev);
359 static int ade7754_set_irq(
struct device *dev,
bool enable)
382 static int ade7754_stop_device(
struct device *dev)
391 static int ade7754_initial_setup(
struct iio_dev *indio_dev)
402 ret = ade7754_set_irq(dev,
false);
404 dev_err(dev,
"disable irq failed");
422 ret = ade7754_spi_read_reg_8(dev,
429 sps = 26000 / (1 +
t);
431 return sprintf(buf,
"%d\n", sps);
439 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
474 return ret ? ret : len;
481 ade7754_read_frequency,
482 ade7754_write_frequency);
488 static struct attribute *ade7754_attributes[] = {
489 &iio_dev_attr_in_temp_raw.dev_attr.attr,
490 &iio_const_attr_in_temp_offset.dev_attr.attr,
491 &iio_const_attr_in_temp_scale.dev_attr.attr,
492 &iio_dev_attr_sampling_frequency.dev_attr.attr,
493 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
494 &iio_dev_attr_reset.dev_attr.attr,
495 &iio_dev_attr_aenergy.dev_attr.attr,
496 &iio_dev_attr_laenergy.dev_attr.attr,
497 &iio_dev_attr_vaenergy.dev_attr.attr,
498 &iio_dev_attr_lvaenergy.dev_attr.attr,
499 &iio_dev_attr_vpeak.dev_attr.attr,
500 &iio_dev_attr_ipeak.dev_attr.attr,
501 &iio_dev_attr_aphcal.dev_attr.attr,
502 &iio_dev_attr_bphcal.dev_attr.attr,
503 &iio_dev_attr_cphcal.dev_attr.attr,
504 &iio_dev_attr_aapos.dev_attr.attr,
505 &iio_dev_attr_bapos.dev_attr.attr,
506 &iio_dev_attr_capos.dev_attr.attr,
507 &iio_dev_attr_wdiv.dev_attr.attr,
508 &iio_dev_attr_vadiv.dev_attr.attr,
509 &iio_dev_attr_cfnum.dev_attr.attr,
510 &iio_dev_attr_cfden.dev_attr.attr,
511 &iio_dev_attr_active_power_a_gain.dev_attr.attr,
512 &iio_dev_attr_active_power_b_gain.dev_attr.attr,
513 &iio_dev_attr_active_power_c_gain.dev_attr.attr,
514 &iio_dev_attr_airms.dev_attr.attr,
515 &iio_dev_attr_birms.dev_attr.attr,
516 &iio_dev_attr_cirms.dev_attr.attr,
517 &iio_dev_attr_avrms.dev_attr.attr,
518 &iio_dev_attr_bvrms.dev_attr.attr,
519 &iio_dev_attr_cvrms.dev_attr.attr,
520 &iio_dev_attr_airmsos.dev_attr.attr,
521 &iio_dev_attr_birmsos.dev_attr.attr,
522 &iio_dev_attr_cirmsos.dev_attr.attr,
523 &iio_dev_attr_avrmsos.dev_attr.attr,
524 &iio_dev_attr_bvrmsos.dev_attr.attr,
525 &iio_dev_attr_cvrmsos.dev_attr.attr,
530 .attrs = ade7754_attributes,
533 static const struct iio_info ade7754_info = {
534 .attrs = &ade7754_attribute_group,
546 if (indio_dev ==
NULL) {
551 spi_set_drvdata(spi, indio_dev);
553 st = iio_priv(indio_dev);
557 indio_dev->
name = spi->
dev.driver->name;
558 indio_dev->
dev.parent = &spi->
dev;
559 indio_dev->
info = &ade7754_info;
563 ret = ade7754_initial_setup(indio_dev);
582 struct iio_dev *indio_dev = spi_get_drvdata(spi);
585 ade7754_stop_device(&indio_dev->
dev);
596 .probe = ade7754_probe,
602 MODULE_DESCRIPTION(
"Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver");