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/delay.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.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_get_drvdata(dev
);
32 struct ade7753_state
*st
= iio_priv(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_priv(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_priv(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_priv(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_priv(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 iio_dev
*indio_dev
)
375 struct device
*dev
= &indio_dev
->dev
;
376 struct ade7753_state
*st
= iio_priv(indio_dev
);
378 /* use low spi speed for init */
379 st
->us
->mode
= SPI_MODE_3
;
383 ret
= ade7753_set_irq(dev
, false);
385 dev_err(dev
, "disable irq failed");
390 msleep(ADE7753_STARTUP_DELAY
);
396 static ssize_t
ade7753_read_frequency(struct device
*dev
,
397 struct device_attribute
*attr
,
403 ret
= ade7753_spi_read_reg_16(dev
, ADE7753_MODE
, &t
);
408 sps
= 27900 / (1 + t
);
410 len
= sprintf(buf
, "%d\n", sps
);
414 static ssize_t
ade7753_write_frequency(struct device
*dev
,
415 struct device_attribute
*attr
,
419 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
420 struct ade7753_state
*st
= iio_priv(indio_dev
);
425 ret
= strict_strtol(buf
, 10, &val
);
429 mutex_lock(&indio_dev
->mlock
);
436 st
->us
->max_speed_hz
= ADE7753_SPI_SLOW
;
438 st
->us
->max_speed_hz
= ADE7753_SPI_FAST
;
440 ret
= ade7753_spi_read_reg_16(dev
, ADE7753_MODE
, ®
);
447 ret
= ade7753_spi_write_reg_16(dev
, ADE7753_MODE
, reg
);
450 mutex_unlock(&indio_dev
->mlock
);
452 return ret
? ret
: len
;
455 static IIO_DEV_ATTR_TEMP_RAW(ade7753_read_8bit
);
456 static IIO_CONST_ATTR(in_temp_offset
, "-25 C");
457 static IIO_CONST_ATTR(in_temp_scale
, "0.67 C");
459 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
460 ade7753_read_frequency
,
461 ade7753_write_frequency
);
463 static IIO_DEV_ATTR_RESET(ade7753_write_reset
);
465 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
467 static struct attribute
*ade7753_attributes
[] = {
468 &iio_dev_attr_in_temp_raw
.dev_attr
.attr
,
469 &iio_const_attr_in_temp_offset
.dev_attr
.attr
,
470 &iio_const_attr_in_temp_scale
.dev_attr
.attr
,
471 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
472 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
473 &iio_dev_attr_reset
.dev_attr
.attr
,
474 &iio_dev_attr_phcal
.dev_attr
.attr
,
475 &iio_dev_attr_cfden
.dev_attr
.attr
,
476 &iio_dev_attr_aenergy
.dev_attr
.attr
,
477 &iio_dev_attr_laenergy
.dev_attr
.attr
,
478 &iio_dev_attr_vaenergy
.dev_attr
.attr
,
479 &iio_dev_attr_lvaenergy
.dev_attr
.attr
,
480 &iio_dev_attr_cfnum
.dev_attr
.attr
,
481 &iio_dev_attr_apos
.dev_attr
.attr
,
482 &iio_dev_attr_sagcyc
.dev_attr
.attr
,
483 &iio_dev_attr_saglvl
.dev_attr
.attr
,
484 &iio_dev_attr_linecyc
.dev_attr
.attr
,
485 &iio_dev_attr_chksum
.dev_attr
.attr
,
486 &iio_dev_attr_pga_gain
.dev_attr
.attr
,
487 &iio_dev_attr_wgain
.dev_attr
.attr
,
488 &iio_dev_attr_choff_1
.dev_attr
.attr
,
489 &iio_dev_attr_choff_2
.dev_attr
.attr
,
490 &iio_dev_attr_wdiv
.dev_attr
.attr
,
491 &iio_dev_attr_irms
.dev_attr
.attr
,
492 &iio_dev_attr_vrms
.dev_attr
.attr
,
493 &iio_dev_attr_irmsos
.dev_attr
.attr
,
494 &iio_dev_attr_vrmsos
.dev_attr
.attr
,
495 &iio_dev_attr_vagain
.dev_attr
.attr
,
496 &iio_dev_attr_ipklvl
.dev_attr
.attr
,
497 &iio_dev_attr_vpklvl
.dev_attr
.attr
,
498 &iio_dev_attr_ipeak
.dev_attr
.attr
,
499 &iio_dev_attr_vpeak
.dev_attr
.attr
,
500 &iio_dev_attr_vperiod
.dev_attr
.attr
,
504 static const struct attribute_group ade7753_attribute_group
= {
505 .attrs
= ade7753_attributes
,
508 static const struct iio_info ade7753_info
= {
509 .attrs
= &ade7753_attribute_group
,
510 .driver_module
= THIS_MODULE
,
513 static int __devinit
ade7753_probe(struct spi_device
*spi
)
516 struct ade7753_state
*st
;
517 struct iio_dev
*indio_dev
;
519 /* setup the industrialio driver allocated elements */
520 indio_dev
= iio_allocate_device(sizeof(*st
));
521 if (indio_dev
== NULL
) {
525 /* this is only used for removal purposes */
526 spi_set_drvdata(spi
, indio_dev
);
528 st
= iio_priv(indio_dev
);
530 mutex_init(&st
->buf_lock
);
532 indio_dev
->name
= spi
->dev
.driver
->name
;
533 indio_dev
->dev
.parent
= &spi
->dev
;
534 indio_dev
->info
= &ade7753_info
;
535 indio_dev
->modes
= INDIO_DIRECT_MODE
;
537 /* Get the device into a sane initial state */
538 ret
= ade7753_initial_setup(indio_dev
);
542 ret
= iio_device_register(indio_dev
);
549 iio_free_device(indio_dev
);
555 /* fixme, confirm ordering in this function */
556 static int ade7753_remove(struct spi_device
*spi
)
559 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
561 iio_device_unregister(indio_dev
);
563 ret
= ade7753_stop_device(&(indio_dev
->dev
));
567 iio_free_device(indio_dev
);
572 static struct spi_driver ade7753_driver
= {
575 .owner
= THIS_MODULE
,
577 .probe
= ade7753_probe
,
578 .remove
= __devexit_p(ade7753_remove
),
580 module_spi_driver(ade7753_driver
);
582 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
583 MODULE_DESCRIPTION("Analog Devices ADE7753/6 Single-Phase Multifunction Meter");
584 MODULE_LICENSE("GPL v2");
585 MODULE_ALIAS("spi:ade7753");