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>
32 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
39 ret = spi_write(st->
us, st->
tx, 2);
45 static int ade7758_spi_write_reg_16(
struct device *
dev,
51 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
63 st->
tx[1] = (value >> 8) & 0xFF;
64 st->
tx[2] = value & 0xFF;
66 spi_message_init(&
msg);
67 spi_message_add_tail(xfers, &
msg);
74 static int ade7758_spi_write_reg_24(
struct device *
dev,
80 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
92 st->
tx[1] = (value >> 16) & 0xFF;
93 st->
tx[2] = (value >> 8) & 0xFF;
94 st->
tx[3] = value & 0xFF;
96 spi_message_init(&
msg);
97 spi_message_add_tail(xfers, &
msg);
109 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
120 .tx_buf = &st->
tx[1],
131 spi_message_init(&msg);
132 spi_message_add_tail(&xfers[0], &msg);
133 spi_message_add_tail(&xfers[1], &msg);
136 dev_err(&st->
us->dev,
"problem when reading 8 bit register 0x%02X",
147 static int ade7758_spi_read_reg_16(
struct device *
dev,
152 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
163 .tx_buf = &st->
tx[1],
176 spi_message_init(&
msg);
177 spi_message_add_tail(&xfers[0], &
msg);
178 spi_message_add_tail(&xfers[1], &
msg);
181 dev_err(&st->
us->dev,
"problem when reading 16 bit register 0x%02X",
186 *val = (st->
rx[0] << 8) | st->
rx[1];
193 static int ade7758_spi_read_reg_24(
struct device *dev,
198 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
209 .tx_buf = &st->
tx[1],
222 spi_message_init(&
msg);
223 spi_message_add_tail(&xfers[0], &
msg);
224 spi_message_add_tail(&xfers[1], &
msg);
227 dev_err(&st->
us->dev,
"problem when reading 24 bit register 0x%02X",
231 *val = (st->
rx[0] << 16) | (st->
rx[1] << 8) | st->
rx[2];
250 return sprintf(buf,
"%u\n", val);
261 ret = ade7758_spi_read_reg_16(dev, this_attr->
address, &val);
265 return sprintf(buf,
"%u\n", val);
276 ret = ade7758_spi_read_reg_24(dev, this_attr->
address, &val);
280 return sprintf(buf,
"%u\n", val & 0xFFFFFF);
298 return ret ? ret : len;
313 ret = ade7758_spi_write_reg_16(dev, this_attr->
address, val);
316 return ret ? ret : len;
319 static int ade7758_reset(
struct device *dev)
336 const char *buf,
size_t len)
344 return ade7758_reset(dev);
454 ret = ade7758_spi_read_reg_24(dev,
ADE7758_MASK, &irqen);
464 ret = ade7758_spi_write_reg_24(dev,
ADE7758_MASK, irqen);
473 static int ade7758_stop_device(
struct device *dev)
488 static int ade7758_initial_setup(
struct iio_dev *indio_dev)
501 dev_err(dev,
"disable irq failed");
526 sps = 26040 / (1 <<
t);
528 len =
sprintf(buf,
"%d SPS\n", sps);
537 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
582 return ret ? ret : len;
609 ade7758_read_frequency,
610 ade7758_write_frequency);
616 static struct attribute *ade7758_attributes[] = {
617 &iio_dev_attr_in_temp_raw.dev_attr.attr,
618 &iio_const_attr_in_temp_offset.dev_attr.attr,
619 &iio_const_attr_in_temp_scale.dev_attr.attr,
620 &iio_dev_attr_sampling_frequency.dev_attr.attr,
621 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
622 &iio_dev_attr_reset.dev_attr.attr,
623 &iio_dev_attr_awatthr.dev_attr.attr,
624 &iio_dev_attr_bwatthr.dev_attr.attr,
625 &iio_dev_attr_cwatthr.dev_attr.attr,
626 &iio_dev_attr_avarhr.dev_attr.attr,
627 &iio_dev_attr_bvarhr.dev_attr.attr,
628 &iio_dev_attr_cvarhr.dev_attr.attr,
629 &iio_dev_attr_avahr.dev_attr.attr,
630 &iio_dev_attr_bvahr.dev_attr.attr,
631 &iio_dev_attr_cvahr.dev_attr.attr,
632 &iio_dev_attr_vpeak.dev_attr.attr,
633 &iio_dev_attr_ipeak.dev_attr.attr,
634 &iio_dev_attr_aphcal.dev_attr.attr,
635 &iio_dev_attr_bphcal.dev_attr.attr,
636 &iio_dev_attr_cphcal.dev_attr.attr,
637 &iio_dev_attr_wdiv.dev_attr.attr,
638 &iio_dev_attr_vadiv.dev_attr.attr,
639 &iio_dev_attr_airms.dev_attr.attr,
640 &iio_dev_attr_birms.dev_attr.attr,
641 &iio_dev_attr_cirms.dev_attr.attr,
642 &iio_dev_attr_avrms.dev_attr.attr,
643 &iio_dev_attr_bvrms.dev_attr.attr,
644 &iio_dev_attr_cvrms.dev_attr.attr,
645 &iio_dev_attr_aigain.dev_attr.attr,
646 &iio_dev_attr_bigain.dev_attr.attr,
647 &iio_dev_attr_cigain.dev_attr.attr,
648 &iio_dev_attr_avrmsgain.dev_attr.attr,
649 &iio_dev_attr_bvrmsgain.dev_attr.attr,
650 &iio_dev_attr_cvrmsgain.dev_attr.attr,
651 &iio_dev_attr_airmsos.dev_attr.attr,
652 &iio_dev_attr_birmsos.dev_attr.attr,
653 &iio_dev_attr_cirmsos.dev_attr.attr,
654 &iio_dev_attr_avrmsos.dev_attr.attr,
655 &iio_dev_attr_bvrmsos.dev_attr.attr,
656 &iio_dev_attr_cvrmsos.dev_attr.attr,
661 .attrs = ade7758_attributes,
669 .extend_name =
"raw",
683 .extend_name =
"raw",
697 .extend_name =
"apparent_raw",
711 .extend_name =
"active_raw",
725 .extend_name =
"reactive_raw",
739 .extend_name =
"raw",
753 .extend_name =
"raw",
767 .extend_name =
"apparent_raw",
781 .extend_name =
"active_raw",
795 .extend_name =
"reactive_raw",
809 .extend_name =
"raw",
823 .extend_name =
"raw",
837 .extend_name =
"apparent_raw",
851 .extend_name =
"active_raw",
865 .extend_name =
"reactive_raw",
879 static const struct iio_info ade7758_info = {
880 .attrs = &ade7758_attribute_group,
890 if (indio_dev ==
NULL) {
895 st = iio_priv(indio_dev);
897 spi_set_drvdata(spi, indio_dev);
914 indio_dev->
name = spi->
dev.driver->name;
915 indio_dev->
dev.parent = &spi->
dev;
916 indio_dev->
info = &ade7758_info;
924 &ade7758_channels[0],
927 dev_err(&spi->
dev,
"failed to initialize the ring\n");
928 goto error_unreg_ring_funcs;
932 ret = ade7758_initial_setup(indio_dev);
934 goto error_uninitialize_ring;
939 goto error_uninitialize_ring;
944 goto error_remove_trigger;
948 error_remove_trigger:
951 error_uninitialize_ring:
953 error_unreg_ring_funcs:
967 struct iio_dev *indio_dev = spi_get_drvdata(spi);
971 ade7758_stop_device(&indio_dev->
dev);
994 .probe = ade7758_probe,
996 .id_table = ade7758_id,
1001 MODULE_DESCRIPTION(
"Analog Devices ADE7758 Polyphase Multifunction Energy Metering IC Driver");