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 ade7759_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 ade7759_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 ade7759_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 ade7759_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 ade7759_spi_read_reg_40(
struct device *dev,
107 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
121 memset(&st->tx[1], 0 , 5);
123 spi_message_init(&
msg);
124 spi_message_add_tail(xfers, &
msg);
127 dev_err(&st->
us->dev,
"problem when reading 40 bit register 0x%02X",
131 *val = ((
u64)st->
rx[1] << 32) | (st->
rx[2] << 24) |
132 (st->
rx[3] << 16) | (st->
rx[4] << 8) | st->
rx[5];
147 ret = ade7759_spi_read_reg_8(dev, this_attr->
address, &val);
151 return sprintf(buf,
"%u\n", val);
162 ret = ade7759_spi_read_reg_16(dev, this_attr->
address, &val);
166 return sprintf(buf,
"%u\n", val);
177 ret = ade7759_spi_read_reg_40(dev, this_attr->
address, &val);
181 return sprintf(buf,
"%llu\n", val);
196 ret = ade7759_spi_write_reg_8(dev, this_attr->
address, val);
199 return ret ? ret : len;
214 ret = ade7759_spi_write_reg_16(dev, this_attr->
address, val);
217 return ret ? ret : len;
220 static int ade7759_reset(
struct device *dev)
224 ade7759_spi_read_reg_16(dev,
228 ret = ade7759_spi_write_reg_16(dev,
237 const char *buf,
size_t len)
245 return ade7759_reset(dev);
298 static int ade7759_set_irq(
struct device *dev,
bool enable)
319 static int ade7759_stop_device(
struct device *dev)
323 ade7759_spi_read_reg_16(dev,
328 return ade7759_spi_write_reg_16(dev,
ADE7759_MODE, val);
331 static int ade7759_initial_setup(
struct iio_dev *indio_dev)
342 ret = ade7759_set_irq(dev,
false);
344 dev_err(dev,
"disable irq failed");
362 ret = ade7759_spi_read_reg_16(dev,
369 sps = 27900 / (1 +
t);
371 return sprintf(buf,
"%d\n", sps);
379 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
414 return ret ? ret : len;
421 ade7759_read_frequency,
422 ade7759_write_frequency);
428 static struct attribute *ade7759_attributes[] = {
429 &iio_dev_attr_in_temp_raw.dev_attr.attr,
430 &iio_const_attr_in_temp_offset.dev_attr.attr,
431 &iio_const_attr_in_temp_scale.dev_attr.attr,
432 &iio_dev_attr_sampling_frequency.dev_attr.attr,
433 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
434 &iio_dev_attr_reset.dev_attr.attr,
435 &iio_dev_attr_phcal.dev_attr.attr,
436 &iio_dev_attr_cfden.dev_attr.attr,
437 &iio_dev_attr_aenergy.dev_attr.attr,
438 &iio_dev_attr_cfnum.dev_attr.attr,
439 &iio_dev_attr_apos.dev_attr.attr,
440 &iio_dev_attr_sagcyc.dev_attr.attr,
441 &iio_dev_attr_saglvl.dev_attr.attr,
442 &iio_dev_attr_linecyc.dev_attr.attr,
443 &iio_dev_attr_lenergy.dev_attr.attr,
444 &iio_dev_attr_chksum.dev_attr.attr,
445 &iio_dev_attr_pga_gain.dev_attr.attr,
446 &iio_dev_attr_active_power_gain.dev_attr.attr,
447 &iio_dev_attr_choff_1.dev_attr.attr,
448 &iio_dev_attr_choff_2.dev_attr.attr,
453 .attrs = ade7759_attributes,
456 static const struct iio_info ade7759_info = {
457 .attrs = &ade7759_attribute_group,
469 if (indio_dev ==
NULL) {
474 spi_set_drvdata(spi, indio_dev);
476 st = iio_priv(indio_dev);
479 indio_dev->
name = spi->
dev.driver->name;
480 indio_dev->
dev.parent = &spi->
dev;
481 indio_dev->
info = &ade7759_info;
485 ret = ade7759_initial_setup(indio_dev);
504 struct iio_dev *indio_dev = spi_get_drvdata(spi);
507 ade7759_stop_device(&indio_dev->
dev);
518 .probe = ade7759_probe,