2 * ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC Driver (I2C Bus)
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/i2c.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
15 #include <linux/iio/iio.h>
18 static int ade7854_i2c_write_reg(struct device *dev,
25 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
26 struct ade7854_state *st = iio_priv(indio_dev);
28 mutex_lock(&st->buf_lock);
29 st->tx[0] = (reg_address >> 8) & 0xFF;
30 st->tx[1] = reg_address & 0xFF;
34 st->tx[2] = val & 0xFF;
38 st->tx[2] = (val >> 8) & 0xFF;
39 st->tx[3] = val & 0xFF;
43 st->tx[2] = (val >> 16) & 0xFF;
44 st->tx[3] = (val >> 8) & 0xFF;
45 st->tx[4] = val & 0xFF;
49 st->tx[2] = (val >> 24) & 0xFF;
50 st->tx[3] = (val >> 16) & 0xFF;
51 st->tx[4] = (val >> 8) & 0xFF;
52 st->tx[5] = val & 0xFF;
60 ret = i2c_master_send(st->i2c, st->tx, count);
63 mutex_unlock(&st->buf_lock);
65 return ret < 0 ? ret : 0;
68 static int ade7854_i2c_read_reg(struct device *dev,
73 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
74 struct ade7854_state *st = iio_priv(indio_dev);
77 mutex_lock(&st->buf_lock);
78 st->tx[0] = (reg_address >> 8) & 0xFF;
79 st->tx[1] = reg_address & 0xFF;
81 ret = i2c_master_send(st->i2c, st->tx, 2);
85 ret = i2c_master_recv(st->i2c, st->rx, bits);
94 *val = (st->rx[0] << 8) | st->rx[1];
97 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
100 *val = (st->rx[0] << 24) | (st->rx[1] << 16) |
101 (st->rx[2] << 8) | st->rx[3];
109 mutex_unlock(&st->buf_lock);
113 static int ade7854_i2c_probe(struct i2c_client *client,
114 const struct i2c_device_id *id)
116 struct ade7854_state *st;
117 struct iio_dev *indio_dev;
119 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
122 st = iio_priv(indio_dev);
123 i2c_set_clientdata(client, indio_dev);
124 st->read_reg = ade7854_i2c_read_reg;
125 st->write_reg = ade7854_i2c_write_reg;
127 st->irq = client->irq;
129 return ade7854_probe(indio_dev, &client->dev);
132 static const struct i2c_device_id ade7854_id[] = {
139 MODULE_DEVICE_TABLE(i2c, ade7854_id);
141 static struct i2c_driver ade7854_i2c_driver = {
145 .probe = ade7854_i2c_probe,
146 .id_table = ade7854_id,
148 module_i2c_driver(ade7854_i2c_driver);
150 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
151 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC I2C Driver");
152 MODULE_LICENSE("GPL v2");