2 * ADE7754 Polyphase Multifunction Energy Metering IC Driver
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 ade7754_spi_write_reg_8(struct device
*dev
,
31 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
32 struct ade7754_state
*st
= iio_priv(indio_dev
);
34 mutex_lock(&st
->buf_lock
);
35 st
->tx
[0] = ADE7754_WRITE_REG(reg_address
);
38 ret
= spi_write(st
->us
, st
->tx
, 2);
39 mutex_unlock(&st
->buf_lock
);
44 static int ade7754_spi_write_reg_16(struct device
*dev
,
49 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
50 struct ade7754_state
*st
= iio_priv(indio_dev
);
52 mutex_lock(&st
->buf_lock
);
53 st
->tx
[0] = ADE7754_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 ade7754_spi_read_reg_8(struct device
*dev
,
66 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
67 struct ade7754_state
*st
= iio_priv(indio_dev
);
70 ret
= spi_w8r8(st
->us
, ADE7754_READ_REG(reg_address
));
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_get_drvdata(dev
);
86 struct ade7754_state
*st
= iio_priv(indio_dev
);
89 ret
= spi_w8r16(st
->us
, ADE7754_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 ade7754_spi_read_reg_24(struct device
*dev
,
106 struct spi_message msg
;
107 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
108 struct ade7754_state
*st
= iio_priv(indio_dev
);
110 struct spi_transfer xfers
[] = {
119 mutex_lock(&st
->buf_lock
);
120 st
->tx
[0] = ADE7754_READ_REG(reg_address
);
125 spi_message_init(&msg
);
126 spi_message_add_tail(xfers
, &msg
);
127 ret
= spi_sync(st
->us
, &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];
136 mutex_unlock(&st
->buf_lock
);
140 static ssize_t
ade7754_read_8bit(struct device
*dev
,
141 struct device_attribute
*attr
,
146 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
148 ret
= ade7754_spi_read_reg_8(dev
, this_attr
->address
, &val
);
152 return sprintf(buf
, "%u\n", val
);
155 static ssize_t
ade7754_read_16bit(struct device
*dev
,
156 struct device_attribute
*attr
,
161 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
163 ret
= ade7754_spi_read_reg_16(dev
, this_attr
->address
, &val
);
167 return sprintf(buf
, "%u\n", val
);
170 static ssize_t
ade7754_read_24bit(struct device
*dev
,
171 struct device_attribute
*attr
,
176 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
178 ret
= ade7754_spi_read_reg_24(dev
, this_attr
->address
, &val
);
182 return sprintf(buf
, "%u\n", val
& 0xFFFFFF);
185 static ssize_t
ade7754_write_8bit(struct device
*dev
,
186 struct device_attribute
*attr
,
190 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
194 ret
= strict_strtol(buf
, 10, &val
);
197 ret
= ade7754_spi_write_reg_8(dev
, this_attr
->address
, val
);
200 return ret
? ret
: len
;
203 static ssize_t
ade7754_write_16bit(struct device
*dev
,
204 struct device_attribute
*attr
,
208 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
212 ret
= strict_strtol(buf
, 10, &val
);
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
)
225 ade7754_spi_read_reg_8(dev
, ADE7754_OPMODE
, &val
);
226 val
|= 1 << 6; /* Software Chip Reset */
227 return ade7754_spi_write_reg_8(dev
, ADE7754_OPMODE
, val
);
231 static ssize_t
ade7754_write_reset(struct device
*dev
,
232 struct device_attribute
*attr
,
233 const char *buf
, size_t len
)
241 return ade7754_reset(dev
);
246 static IIO_DEV_ATTR_AENERGY(ade7754_read_24bit
, ADE7754_AENERGY
);
247 static IIO_DEV_ATTR_LAENERGY(ade7754_read_24bit
, ADE7754_LAENERGY
);
248 static IIO_DEV_ATTR_VAENERGY(ade7754_read_24bit
, ADE7754_VAENERGY
);
249 static IIO_DEV_ATTR_LVAENERGY(ade7754_read_24bit
, ADE7754_LVAENERGY
);
250 static IIO_DEV_ATTR_VPEAK(S_IWUSR
| S_IRUGO
,
254 static IIO_DEV_ATTR_IPEAK(S_IWUSR
| S_IRUGO
,
258 static IIO_DEV_ATTR_APHCAL(S_IWUSR
| S_IRUGO
,
262 static IIO_DEV_ATTR_BPHCAL(S_IWUSR
| S_IRUGO
,
266 static IIO_DEV_ATTR_CPHCAL(S_IWUSR
| S_IRUGO
,
270 static IIO_DEV_ATTR_AAPOS(S_IWUSR
| S_IRUGO
,
274 static IIO_DEV_ATTR_BAPOS(S_IWUSR
| S_IRUGO
,
278 static IIO_DEV_ATTR_CAPOS(S_IWUSR
| S_IRUGO
,
282 static IIO_DEV_ATTR_WDIV(S_IWUSR
| S_IRUGO
,
286 static IIO_DEV_ATTR_VADIV(S_IWUSR
| S_IRUGO
,
290 static IIO_DEV_ATTR_CFNUM(S_IWUSR
| S_IRUGO
,
294 static IIO_DEV_ATTR_CFDEN(S_IWUSR
| S_IRUGO
,
298 static IIO_DEV_ATTR_ACTIVE_POWER_A_GAIN(S_IWUSR
| S_IRUGO
,
302 static IIO_DEV_ATTR_ACTIVE_POWER_B_GAIN(S_IWUSR
| S_IRUGO
,
306 static IIO_DEV_ATTR_ACTIVE_POWER_C_GAIN(S_IWUSR
| S_IRUGO
,
310 static IIO_DEV_ATTR_AIRMS(S_IRUGO
,
314 static IIO_DEV_ATTR_BIRMS(S_IRUGO
,
318 static IIO_DEV_ATTR_CIRMS(S_IRUGO
,
322 static IIO_DEV_ATTR_AVRMS(S_IRUGO
,
326 static IIO_DEV_ATTR_BVRMS(S_IRUGO
,
330 static IIO_DEV_ATTR_CVRMS(S_IRUGO
,
334 static IIO_DEV_ATTR_AIRMSOS(S_IRUGO
,
338 static IIO_DEV_ATTR_BIRMSOS(S_IRUGO
,
342 static IIO_DEV_ATTR_CIRMSOS(S_IRUGO
,
346 static IIO_DEV_ATTR_AVRMSOS(S_IRUGO
,
350 static IIO_DEV_ATTR_BVRMSOS(S_IRUGO
,
354 static IIO_DEV_ATTR_CVRMSOS(S_IRUGO
,
359 static int ade7754_set_irq(struct device
*dev
, bool enable
)
363 ret
= ade7754_spi_read_reg_16(dev
, ADE7754_IRQEN
, &irqen
);
368 irqen
|= 1 << 14; /* Enables an interrupt when a data is
369 present in the waveform register */
373 ret
= ade7754_spi_write_reg_16(dev
, ADE7754_IRQEN
, irqen
);
381 /* Power down the device */
382 static int ade7754_stop_device(struct device
*dev
)
386 ade7754_spi_read_reg_8(dev
, ADE7754_OPMODE
, &val
);
387 val
|= 7 << 3; /* ADE7754 powered down */
388 return ade7754_spi_write_reg_8(dev
, ADE7754_OPMODE
, val
);
391 static int ade7754_initial_setup(struct iio_dev
*indio_dev
)
394 struct ade7754_state
*st
= iio_priv(indio_dev
);
395 struct device
*dev
= &indio_dev
->dev
;
397 /* use low spi speed for init */
398 st
->us
->mode
= SPI_MODE_3
;
402 ret
= ade7754_set_irq(dev
, false);
404 dev_err(dev
, "disable irq failed");
409 msleep(ADE7754_STARTUP_DELAY
);
415 static ssize_t
ade7754_read_frequency(struct device
*dev
,
416 struct device_attribute
*attr
,
422 ret
= ade7754_spi_read_reg_8(dev
,
429 sps
= 26000 / (1 + t
);
431 return sprintf(buf
, "%d\n", sps
);
434 static ssize_t
ade7754_write_frequency(struct device
*dev
,
435 struct device_attribute
*attr
,
439 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
440 struct ade7754_state
*st
= iio_priv(indio_dev
);
445 ret
= strict_strtol(buf
, 10, &val
);
449 mutex_lock(&indio_dev
->mlock
);
456 st
->us
->max_speed_hz
= ADE7754_SPI_SLOW
;
458 st
->us
->max_speed_hz
= ADE7754_SPI_FAST
;
460 ret
= ade7754_spi_read_reg_8(dev
, ADE7754_WAVMODE
, ®
);
467 ret
= ade7754_spi_write_reg_8(dev
, ADE7754_WAVMODE
, reg
);
470 mutex_unlock(&indio_dev
->mlock
);
472 return ret
? ret
: len
;
474 static IIO_DEV_ATTR_TEMP_RAW(ade7754_read_8bit
);
475 static IIO_CONST_ATTR(in_temp_offset
, "129 C");
476 static IIO_CONST_ATTR(in_temp_scale
, "4 C");
478 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
479 ade7754_read_frequency
,
480 ade7754_write_frequency
);
482 static IIO_DEV_ATTR_RESET(ade7754_write_reset
);
484 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000");
486 static struct attribute
*ade7754_attributes
[] = {
487 &iio_dev_attr_in_temp_raw
.dev_attr
.attr
,
488 &iio_const_attr_in_temp_offset
.dev_attr
.attr
,
489 &iio_const_attr_in_temp_scale
.dev_attr
.attr
,
490 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
491 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
492 &iio_dev_attr_reset
.dev_attr
.attr
,
493 &iio_dev_attr_aenergy
.dev_attr
.attr
,
494 &iio_dev_attr_laenergy
.dev_attr
.attr
,
495 &iio_dev_attr_vaenergy
.dev_attr
.attr
,
496 &iio_dev_attr_lvaenergy
.dev_attr
.attr
,
497 &iio_dev_attr_vpeak
.dev_attr
.attr
,
498 &iio_dev_attr_ipeak
.dev_attr
.attr
,
499 &iio_dev_attr_aphcal
.dev_attr
.attr
,
500 &iio_dev_attr_bphcal
.dev_attr
.attr
,
501 &iio_dev_attr_cphcal
.dev_attr
.attr
,
502 &iio_dev_attr_aapos
.dev_attr
.attr
,
503 &iio_dev_attr_bapos
.dev_attr
.attr
,
504 &iio_dev_attr_capos
.dev_attr
.attr
,
505 &iio_dev_attr_wdiv
.dev_attr
.attr
,
506 &iio_dev_attr_vadiv
.dev_attr
.attr
,
507 &iio_dev_attr_cfnum
.dev_attr
.attr
,
508 &iio_dev_attr_cfden
.dev_attr
.attr
,
509 &iio_dev_attr_active_power_a_gain
.dev_attr
.attr
,
510 &iio_dev_attr_active_power_b_gain
.dev_attr
.attr
,
511 &iio_dev_attr_active_power_c_gain
.dev_attr
.attr
,
512 &iio_dev_attr_airms
.dev_attr
.attr
,
513 &iio_dev_attr_birms
.dev_attr
.attr
,
514 &iio_dev_attr_cirms
.dev_attr
.attr
,
515 &iio_dev_attr_avrms
.dev_attr
.attr
,
516 &iio_dev_attr_bvrms
.dev_attr
.attr
,
517 &iio_dev_attr_cvrms
.dev_attr
.attr
,
518 &iio_dev_attr_airmsos
.dev_attr
.attr
,
519 &iio_dev_attr_birmsos
.dev_attr
.attr
,
520 &iio_dev_attr_cirmsos
.dev_attr
.attr
,
521 &iio_dev_attr_avrmsos
.dev_attr
.attr
,
522 &iio_dev_attr_bvrmsos
.dev_attr
.attr
,
523 &iio_dev_attr_cvrmsos
.dev_attr
.attr
,
527 static const struct attribute_group ade7754_attribute_group
= {
528 .attrs
= ade7754_attributes
,
531 static const struct iio_info ade7754_info
= {
532 .attrs
= &ade7754_attribute_group
,
533 .driver_module
= THIS_MODULE
,
536 static int __devinit
ade7754_probe(struct spi_device
*spi
)
539 struct ade7754_state
*st
;
540 struct iio_dev
*indio_dev
;
542 /* setup the industrialio driver allocated elements */
543 indio_dev
= iio_allocate_device(sizeof(*st
));
544 if (indio_dev
== NULL
) {
548 /* this is only used for removal purposes */
549 spi_set_drvdata(spi
, indio_dev
);
551 st
= iio_priv(indio_dev
);
553 mutex_init(&st
->buf_lock
);
555 indio_dev
->name
= spi
->dev
.driver
->name
;
556 indio_dev
->dev
.parent
= &spi
->dev
;
557 indio_dev
->info
= &ade7754_info
;
558 indio_dev
->modes
= INDIO_DIRECT_MODE
;
560 /* Get the device into a sane initial state */
561 ret
= ade7754_initial_setup(indio_dev
);
564 ret
= iio_device_register(indio_dev
);
571 iio_free_device(indio_dev
);
577 /* fixme, confirm ordering in this function */
578 static int ade7754_remove(struct spi_device
*spi
)
581 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
583 iio_device_unregister(indio_dev
);
584 ret
= ade7754_stop_device(&(indio_dev
->dev
));
588 iio_free_device(indio_dev
);
595 static struct spi_driver ade7754_driver
= {
598 .owner
= THIS_MODULE
,
600 .probe
= ade7754_probe
,
601 .remove
= __devexit_p(ade7754_remove
),
603 module_spi_driver(ade7754_driver
);
605 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
606 MODULE_DESCRIPTION("Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver");
607 MODULE_LICENSE("GPL v2");
608 MODULE_ALIAS("spi:ad7754");