Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / staging / iio / meter / ade7854-i2c.c
blob1e1faa0479d3a734c32ffca43f86417442ce7e3d
1 /*
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.
7 */
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 "../iio.h"
16 #include "ade7854.h"
18 static int ade7854_i2c_write_reg_8(struct device *dev,
19 u16 reg_address,
20 u8 value)
22 int ret;
23 struct iio_dev *indio_dev = dev_get_drvdata(dev);
24 struct ade7854_state *st = iio_priv(indio_dev);
26 mutex_lock(&st->buf_lock);
27 st->tx[0] = (reg_address >> 8) & 0xFF;
28 st->tx[1] = reg_address & 0xFF;
29 st->tx[2] = value;
31 ret = i2c_master_send(st->i2c, st->tx, 3);
32 mutex_unlock(&st->buf_lock);
34 return ret;
37 static int ade7854_i2c_write_reg_16(struct device *dev,
38 u16 reg_address,
39 u16 value)
41 int ret;
42 struct iio_dev *indio_dev = dev_get_drvdata(dev);
43 struct ade7854_state *st = iio_priv(indio_dev);
45 mutex_lock(&st->buf_lock);
46 st->tx[0] = (reg_address >> 8) & 0xFF;
47 st->tx[1] = reg_address & 0xFF;
48 st->tx[2] = (value >> 8) & 0xFF;
49 st->tx[3] = value & 0xFF;
51 ret = i2c_master_send(st->i2c, st->tx, 4);
52 mutex_unlock(&st->buf_lock);
54 return ret;
57 static int ade7854_i2c_write_reg_24(struct device *dev,
58 u16 reg_address,
59 u32 value)
61 int ret;
62 struct iio_dev *indio_dev = dev_get_drvdata(dev);
63 struct ade7854_state *st = iio_priv(indio_dev);
65 mutex_lock(&st->buf_lock);
66 st->tx[0] = (reg_address >> 8) & 0xFF;
67 st->tx[1] = reg_address & 0xFF;
68 st->tx[2] = (value >> 16) & 0xFF;
69 st->tx[3] = (value >> 8) & 0xFF;
70 st->tx[4] = value & 0xFF;
72 ret = i2c_master_send(st->i2c, st->tx, 5);
73 mutex_unlock(&st->buf_lock);
75 return ret;
78 static int ade7854_i2c_write_reg_32(struct device *dev,
79 u16 reg_address,
80 u32 value)
82 int ret;
83 struct iio_dev *indio_dev = dev_get_drvdata(dev);
84 struct ade7854_state *st = iio_priv(indio_dev);
86 mutex_lock(&st->buf_lock);
87 st->tx[0] = (reg_address >> 8) & 0xFF;
88 st->tx[1] = reg_address & 0xFF;
89 st->tx[2] = (value >> 24) & 0xFF;
90 st->tx[3] = (value >> 16) & 0xFF;
91 st->tx[4] = (value >> 8) & 0xFF;
92 st->tx[5] = value & 0xFF;
94 ret = i2c_master_send(st->i2c, st->tx, 6);
95 mutex_unlock(&st->buf_lock);
97 return ret;
100 static int ade7854_i2c_read_reg_8(struct device *dev,
101 u16 reg_address,
102 u8 *val)
104 struct iio_dev *indio_dev = dev_get_drvdata(dev);
105 struct ade7854_state *st = iio_priv(indio_dev);
106 int ret;
108 mutex_lock(&st->buf_lock);
109 st->tx[0] = (reg_address >> 8) & 0xFF;
110 st->tx[1] = reg_address & 0xFF;
112 ret = i2c_master_send(st->i2c, st->tx, 2);
113 if (ret)
114 goto out;
116 ret = i2c_master_recv(st->i2c, st->rx, 1);
117 if (ret)
118 goto out;
120 *val = st->rx[0];
121 out:
122 mutex_unlock(&st->buf_lock);
123 return ret;
126 static int ade7854_i2c_read_reg_16(struct device *dev,
127 u16 reg_address,
128 u16 *val)
130 struct iio_dev *indio_dev = dev_get_drvdata(dev);
131 struct ade7854_state *st = iio_priv(indio_dev);
132 int ret;
134 mutex_lock(&st->buf_lock);
135 st->tx[0] = (reg_address >> 8) & 0xFF;
136 st->tx[1] = reg_address & 0xFF;
138 ret = i2c_master_send(st->i2c, st->tx, 2);
139 if (ret)
140 goto out;
142 ret = i2c_master_recv(st->i2c, st->rx, 2);
143 if (ret)
144 goto out;
146 *val = (st->rx[0] << 8) | st->rx[1];
147 out:
148 mutex_unlock(&st->buf_lock);
149 return ret;
152 static int ade7854_i2c_read_reg_24(struct device *dev,
153 u16 reg_address,
154 u32 *val)
156 struct iio_dev *indio_dev = dev_get_drvdata(dev);
157 struct ade7854_state *st = iio_priv(indio_dev);
158 int ret;
160 mutex_lock(&st->buf_lock);
161 st->tx[0] = (reg_address >> 8) & 0xFF;
162 st->tx[1] = reg_address & 0xFF;
164 ret = i2c_master_send(st->i2c, st->tx, 2);
165 if (ret)
166 goto out;
168 ret = i2c_master_recv(st->i2c, st->rx, 3);
169 if (ret)
170 goto out;
172 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
173 out:
174 mutex_unlock(&st->buf_lock);
175 return ret;
178 static int ade7854_i2c_read_reg_32(struct device *dev,
179 u16 reg_address,
180 u32 *val)
182 struct iio_dev *indio_dev = dev_get_drvdata(dev);
183 struct ade7854_state *st = iio_priv(indio_dev);
184 int ret;
186 mutex_lock(&st->buf_lock);
187 st->tx[0] = (reg_address >> 8) & 0xFF;
188 st->tx[1] = reg_address & 0xFF;
190 ret = i2c_master_send(st->i2c, st->tx, 2);
191 if (ret)
192 goto out;
194 ret = i2c_master_recv(st->i2c, st->rx, 3);
195 if (ret)
196 goto out;
198 *val = (st->rx[0] << 24) | (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
199 out:
200 mutex_unlock(&st->buf_lock);
201 return ret;
204 static int __devinit ade7854_i2c_probe(struct i2c_client *client,
205 const struct i2c_device_id *id)
207 int ret;
208 struct ade7854_state *st;
209 struct iio_dev *indio_dev;
211 indio_dev = iio_allocate_device(sizeof(*st));
212 if (indio_dev == NULL)
213 return -ENOMEM;
214 st = iio_priv(indio_dev);
215 i2c_set_clientdata(client, indio_dev);
216 st->read_reg_8 = ade7854_i2c_read_reg_8;
217 st->read_reg_16 = ade7854_i2c_read_reg_16;
218 st->read_reg_24 = ade7854_i2c_read_reg_24;
219 st->read_reg_32 = ade7854_i2c_read_reg_32;
220 st->write_reg_8 = ade7854_i2c_write_reg_8;
221 st->write_reg_16 = ade7854_i2c_write_reg_16;
222 st->write_reg_24 = ade7854_i2c_write_reg_24;
223 st->write_reg_32 = ade7854_i2c_write_reg_32;
224 st->i2c = client;
225 st->irq = client->irq;
227 ret = ade7854_probe(indio_dev, &client->dev);
228 if (ret)
229 iio_free_device(indio_dev);
231 return ret;
234 static int __devexit ade7854_i2c_remove(struct i2c_client *client)
236 return ade7854_remove(i2c_get_clientdata(client));
239 static const struct i2c_device_id ade7854_id[] = {
240 { "ade7854", 0 },
241 { "ade7858", 0 },
242 { "ade7868", 0 },
243 { "ade7878", 0 },
246 MODULE_DEVICE_TABLE(i2c, ade7854_id);
248 static struct i2c_driver ade7854_i2c_driver = {
249 .driver = {
250 .name = "ade7854",
252 .probe = ade7854_i2c_probe,
253 .remove = __devexit_p(ade7854_i2c_remove),
254 .id_table = ade7854_id,
256 module_i2c_driver(ade7854_i2c_driver);
258 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
259 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC I2C Driver");
260 MODULE_LICENSE("GPL v2");