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 ade7753_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 ade7753_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 ade7753_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 ade7753_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 ade7753_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[0], &
msg);
127 spi_message_add_tail(&xfers[1], &
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];
149 ret = ade7753_spi_read_reg_8(dev, this_attr->
address, &val);
153 return sprintf(buf,
"%u\n", val);
164 ret = ade7753_spi_read_reg_16(dev, this_attr->
address, &val);
168 return sprintf(buf,
"%u\n", val);
179 ret = ade7753_spi_read_reg_24(dev, this_attr->
address, &val);
183 return sprintf(buf,
"%u\n", val);
198 ret = ade7753_spi_write_reg_8(dev, this_attr->
address, val);
201 return ret ? ret : len;
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)
229 return ade7753_spi_write_reg_16(dev,
ADE7753_MODE, val);
234 const char *buf,
size_t len)
242 return ade7753_reset(dev);
341 static int ade7753_set_irq(
struct device *dev,
bool enable)
362 static int ade7753_stop_device(
struct device *dev)
369 return ade7753_spi_write_reg_16(dev,
ADE7753_MODE, val);
372 static int ade7753_initial_setup(
struct iio_dev *indio_dev)
383 ret = ade7753_set_irq(dev,
false);
385 dev_err(dev,
"disable irq failed");
408 sps = 27900 / (1 +
t);
410 len =
sprintf(buf,
"%d\n", sps);
419 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
454 return ret ? ret : len;
462 ade7753_read_frequency,
463 ade7753_write_frequency);
469 static struct attribute *ade7753_attributes[] = {
470 &iio_dev_attr_in_temp_raw.dev_attr.attr,
471 &iio_const_attr_in_temp_offset.dev_attr.attr,
472 &iio_const_attr_in_temp_scale.dev_attr.attr,
473 &iio_dev_attr_sampling_frequency.dev_attr.attr,
474 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
475 &iio_dev_attr_reset.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,
507 .attrs = ade7753_attributes,
510 static const struct iio_info ade7753_info = {
511 .attrs = &ade7753_attribute_group,
523 if (indio_dev ==
NULL) {
528 spi_set_drvdata(spi, indio_dev);
530 st = iio_priv(indio_dev);
534 indio_dev->
name = spi->
dev.driver->name;
535 indio_dev->
dev.parent = &spi->
dev;
536 indio_dev->
info = &ade7753_info;
540 ret = ade7753_initial_setup(indio_dev);
560 struct iio_dev *indio_dev = spi_get_drvdata(spi);
563 ade7753_stop_device(&indio_dev->
dev);
574 .probe = ade7753_probe,