2 * ADE7759 Active Energy Metering IC with di/dt Sensor Interface 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/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
26 static int ade7759_spi_write_reg_8(struct device
*dev
,
31 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
32 struct ade7759_state
*st
= iio_dev_get_devdata(indio_dev
);
34 mutex_lock(&st
->buf_lock
);
35 st
->tx
[0] = ADE7759_WRITE_REG(reg_address
);
38 ret
= spi_write(st
->us
, st
->tx
, 2);
39 mutex_unlock(&st
->buf_lock
);
44 static int ade7759_spi_write_reg_16(struct device
*dev
,
49 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
50 struct ade7759_state
*st
= iio_dev_get_devdata(indio_dev
);
52 mutex_lock(&st
->buf_lock
);
53 st
->tx
[0] = ADE7759_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 ade7759_spi_read_reg_8(struct device
*dev
,
66 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
67 struct ade7759_state
*st
= iio_dev_get_devdata(indio_dev
);
70 ret
= spi_w8r8(st
->us
, ADE7759_READ_REG(reg_address
));
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_get_drvdata(dev
);
86 struct ade7759_state
*st
= iio_dev_get_devdata(indio_dev
);
89 ret
= spi_w8r16(st
->us
, ADE7759_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 ade7759_spi_read_reg_40(struct device
*dev
,
106 struct spi_message msg
;
107 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
108 struct ade7759_state
*st
= iio_dev_get_devdata(indio_dev
);
110 struct spi_transfer xfers
[] = {
119 mutex_lock(&st
->buf_lock
);
120 st
->tx
[0] = ADE7759_READ_REG(reg_address
);
121 memset(&st
->tx
[1], 0 , 5);
123 spi_message_init(&msg
);
124 spi_message_add_tail(xfers
, &msg
);
125 ret
= spi_sync(st
->us
, &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];
135 mutex_unlock(&st
->buf_lock
);
139 static ssize_t
ade7759_read_8bit(struct device
*dev
,
140 struct device_attribute
*attr
,
145 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
147 ret
= ade7759_spi_read_reg_8(dev
, this_attr
->address
, &val
);
151 return sprintf(buf
, "%u\n", val
);
154 static ssize_t
ade7759_read_16bit(struct device
*dev
,
155 struct device_attribute
*attr
,
160 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
162 ret
= ade7759_spi_read_reg_16(dev
, this_attr
->address
, &val
);
166 return sprintf(buf
, "%u\n", val
);
169 static ssize_t
ade7759_read_40bit(struct device
*dev
,
170 struct device_attribute
*attr
,
175 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
177 ret
= ade7759_spi_read_reg_40(dev
, this_attr
->address
, &val
);
181 return sprintf(buf
, "%llu\n", val
);
184 static ssize_t
ade7759_write_8bit(struct device
*dev
,
185 struct device_attribute
*attr
,
189 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
193 ret
= strict_strtol(buf
, 10, &val
);
196 ret
= ade7759_spi_write_reg_8(dev
, this_attr
->address
, val
);
199 return ret
? ret
: len
;
202 static ssize_t
ade7759_write_16bit(struct device
*dev
,
203 struct device_attribute
*attr
,
207 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
211 ret
= strict_strtol(buf
, 10, &val
);
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
,
227 val
|= 1 << 6; /* Software Chip Reset */
228 ret
= ade7759_spi_write_reg_16(dev
,
235 static ssize_t
ade7759_write_reset(struct device
*dev
,
236 struct device_attribute
*attr
,
237 const char *buf
, size_t len
)
245 return ade7759_reset(dev
);
250 static IIO_DEV_ATTR_AENERGY(ade7759_read_40bit
, ADE7759_AENERGY
);
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(ade7759_read_8bit
, ADE7759_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_LENERGY(ade7759_read_40bit
, ADE7759_LENERGY
);
281 static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR
| S_IRUGO
,
285 static IIO_DEV_ATTR_ACTIVE_POWER_GAIN(S_IWUSR
| S_IRUGO
,
289 static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR
| S_IRUGO
,
293 static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR
| S_IRUGO
,
298 static int ade7759_set_irq(struct device
*dev
, bool enable
)
302 ret
= ade7759_spi_read_reg_8(dev
, ADE7759_IRQEN
, &irqen
);
307 irqen
|= 1 << 3; /* Enables an interrupt when a data is
308 present in the waveform register */
312 ret
= ade7759_spi_write_reg_8(dev
, ADE7759_IRQEN
, irqen
);
318 /* Power down the device */
319 static int ade7759_stop_device(struct device
*dev
)
323 ade7759_spi_read_reg_16(dev
,
326 val
|= 1 << 4; /* AD converters can be turned off */
328 return ade7759_spi_write_reg_16(dev
, ADE7759_MODE
, val
);
331 static int ade7759_initial_setup(struct ade7759_state
*st
)
334 struct device
*dev
= &st
->indio_dev
->dev
;
336 /* use low spi speed for init */
337 st
->us
->mode
= SPI_MODE_3
;
341 ret
= ade7759_set_irq(dev
, false);
343 dev_err(dev
, "disable irq failed");
348 msleep(ADE7759_STARTUP_DELAY
);
354 static ssize_t
ade7759_read_frequency(struct device
*dev
,
355 struct device_attribute
*attr
,
361 ret
= ade7759_spi_read_reg_16(dev
,
368 sps
= 27900 / (1 + t
);
370 return sprintf(buf
, "%d\n", sps
);
373 static ssize_t
ade7759_write_frequency(struct device
*dev
,
374 struct device_attribute
*attr
,
378 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
379 struct ade7759_state
*st
= iio_dev_get_devdata(indio_dev
);
384 ret
= strict_strtol(buf
, 10, &val
);
388 mutex_lock(&indio_dev
->mlock
);
395 st
->us
->max_speed_hz
= ADE7759_SPI_SLOW
;
397 st
->us
->max_speed_hz
= ADE7759_SPI_FAST
;
399 ret
= ade7759_spi_read_reg_16(dev
, ADE7759_MODE
, ®
);
406 ret
= ade7759_spi_write_reg_16(dev
, ADE7759_MODE
, reg
);
409 mutex_unlock(&indio_dev
->mlock
);
411 return ret
? ret
: len
;
413 static IIO_DEV_ATTR_TEMP_RAW(ade7759_read_8bit
);
414 static IIO_CONST_ATTR(temp_offset
, "70 C");
415 static IIO_CONST_ATTR(temp_scale
, "1 C");
417 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR
| S_IRUGO
,
418 ade7759_read_frequency
,
419 ade7759_write_frequency
);
421 static IIO_DEV_ATTR_RESET(ade7759_write_reset
);
423 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
425 static struct attribute
*ade7759_attributes
[] = {
426 &iio_dev_attr_temp_raw
.dev_attr
.attr
,
427 &iio_const_attr_temp_offset
.dev_attr
.attr
,
428 &iio_const_attr_temp_scale
.dev_attr
.attr
,
429 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
430 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
431 &iio_dev_attr_reset
.dev_attr
.attr
,
432 &iio_dev_attr_phcal
.dev_attr
.attr
,
433 &iio_dev_attr_cfden
.dev_attr
.attr
,
434 &iio_dev_attr_aenergy
.dev_attr
.attr
,
435 &iio_dev_attr_cfnum
.dev_attr
.attr
,
436 &iio_dev_attr_apos
.dev_attr
.attr
,
437 &iio_dev_attr_sagcyc
.dev_attr
.attr
,
438 &iio_dev_attr_saglvl
.dev_attr
.attr
,
439 &iio_dev_attr_linecyc
.dev_attr
.attr
,
440 &iio_dev_attr_lenergy
.dev_attr
.attr
,
441 &iio_dev_attr_chksum
.dev_attr
.attr
,
442 &iio_dev_attr_pga_gain
.dev_attr
.attr
,
443 &iio_dev_attr_active_power_gain
.dev_attr
.attr
,
444 &iio_dev_attr_choff_1
.dev_attr
.attr
,
445 &iio_dev_attr_choff_2
.dev_attr
.attr
,
449 static const struct attribute_group ade7759_attribute_group
= {
450 .attrs
= ade7759_attributes
,
453 static const struct iio_info ade7759_info
= {
454 .attrs
= &ade7759_attribute_group
,
455 .driver_module
= THIS_MODULE
,
458 static int __devinit
ade7759_probe(struct spi_device
*spi
)
461 struct ade7759_state
*st
= kzalloc(sizeof *st
, GFP_KERNEL
);
466 /* this is only used for removal purposes */
467 spi_set_drvdata(spi
, st
);
469 /* Allocate the comms buffers */
470 st
->rx
= kzalloc(sizeof(*st
->rx
)*ADE7759_MAX_RX
, GFP_KERNEL
);
471 if (st
->rx
== NULL
) {
475 st
->tx
= kzalloc(sizeof(*st
->tx
)*ADE7759_MAX_TX
, GFP_KERNEL
);
476 if (st
->tx
== NULL
) {
481 mutex_init(&st
->buf_lock
);
482 /* setup the industrialio driver allocated elements */
483 st
->indio_dev
= iio_allocate_device(0);
484 if (st
->indio_dev
== NULL
) {
489 st
->indio_dev
->name
= spi
->dev
.driver
->name
;
490 st
->indio_dev
->dev
.parent
= &spi
->dev
;
492 st
->indio_dev
->info
= &ade7759_info
;
493 st
->indio_dev
->dev_data
= (void *)(st
);
494 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
496 ret
= iio_device_register(st
->indio_dev
);
500 /* Get the device into a sane initial state */
501 ret
= ade7759_initial_setup(st
);
503 goto error_unreg_dev
;
508 iio_device_unregister(st
->indio_dev
);
510 iio_free_device(st
->indio_dev
);
521 /* fixme, confirm ordering in this function */
522 static int ade7759_remove(struct spi_device
*spi
)
525 struct ade7759_state
*st
= spi_get_drvdata(spi
);
526 struct iio_dev
*indio_dev
= st
->indio_dev
;
528 ret
= ade7759_stop_device(&(indio_dev
->dev
));
532 iio_device_unregister(indio_dev
);
543 static struct spi_driver ade7759_driver
= {
546 .owner
= THIS_MODULE
,
548 .probe
= ade7759_probe
,
549 .remove
= __devexit_p(ade7759_remove
),
552 static __init
int ade7759_init(void)
554 return spi_register_driver(&ade7759_driver
);
556 module_init(ade7759_init
);
558 static __exit
void ade7759_exit(void)
560 spi_unregister_driver(&ade7759_driver
);
562 module_exit(ade7759_exit
);
564 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
565 MODULE_DESCRIPTION("Analog Devices ADE7759 Active Energy Metering IC Driver");
566 MODULE_LICENSE("GPL v2");