Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / meter / ade7854-i2c.c
blobdd723435340538a3f2b48abe6b7b5267dda539cc
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>
14 #include "../iio.h"
15 #include "ade7854.h"
17 static int ade7854_i2c_write_reg_8(struct device *dev,
18 u16 reg_address,
19 u8 value)
21 int ret;
22 struct iio_dev *indio_dev = dev_get_drvdata(dev);
23 struct ade7854_state *st = iio_priv(indio_dev);
25 mutex_lock(&st->buf_lock);
26 st->tx[0] = (reg_address >> 8) & 0xFF;
27 st->tx[1] = reg_address & 0xFF;
28 st->tx[2] = value;
30 ret = i2c_master_send(st->i2c, st->tx, 3);
31 mutex_unlock(&st->buf_lock);
33 return ret;
36 static int ade7854_i2c_write_reg_16(struct device *dev,
37 u16 reg_address,
38 u16 value)
40 int ret;
41 struct iio_dev *indio_dev = dev_get_drvdata(dev);
42 struct ade7854_state *st = iio_priv(indio_dev);
44 mutex_lock(&st->buf_lock);
45 st->tx[0] = (reg_address >> 8) & 0xFF;
46 st->tx[1] = reg_address & 0xFF;
47 st->tx[2] = (value >> 8) & 0xFF;
48 st->tx[3] = value & 0xFF;
50 ret = i2c_master_send(st->i2c, st->tx, 4);
51 mutex_unlock(&st->buf_lock);
53 return ret;
56 static int ade7854_i2c_write_reg_24(struct device *dev,
57 u16 reg_address,
58 u32 value)
60 int ret;
61 struct iio_dev *indio_dev = dev_get_drvdata(dev);
62 struct ade7854_state *st = iio_priv(indio_dev);
64 mutex_lock(&st->buf_lock);
65 st->tx[0] = (reg_address >> 8) & 0xFF;
66 st->tx[1] = reg_address & 0xFF;
67 st->tx[2] = (value >> 16) & 0xFF;
68 st->tx[3] = (value >> 8) & 0xFF;
69 st->tx[4] = value & 0xFF;
71 ret = i2c_master_send(st->i2c, st->tx, 5);
72 mutex_unlock(&st->buf_lock);
74 return ret;
77 static int ade7854_i2c_write_reg_32(struct device *dev,
78 u16 reg_address,
79 u32 value)
81 int ret;
82 struct iio_dev *indio_dev = dev_get_drvdata(dev);
83 struct ade7854_state *st = iio_priv(indio_dev);
85 mutex_lock(&st->buf_lock);
86 st->tx[0] = (reg_address >> 8) & 0xFF;
87 st->tx[1] = reg_address & 0xFF;
88 st->tx[2] = (value >> 24) & 0xFF;
89 st->tx[3] = (value >> 16) & 0xFF;
90 st->tx[4] = (value >> 8) & 0xFF;
91 st->tx[5] = value & 0xFF;
93 ret = i2c_master_send(st->i2c, st->tx, 6);
94 mutex_unlock(&st->buf_lock);
96 return ret;
99 static int ade7854_i2c_read_reg_8(struct device *dev,
100 u16 reg_address,
101 u8 *val)
103 struct iio_dev *indio_dev = dev_get_drvdata(dev);
104 struct ade7854_state *st = iio_priv(indio_dev);
105 int ret;
107 mutex_lock(&st->buf_lock);
108 st->tx[0] = (reg_address >> 8) & 0xFF;
109 st->tx[1] = reg_address & 0xFF;
111 ret = i2c_master_send(st->i2c, st->tx, 2);
112 if (ret)
113 goto out;
115 ret = i2c_master_recv(st->i2c, st->rx, 1);
116 if (ret)
117 goto out;
119 *val = st->rx[0];
120 out:
121 mutex_unlock(&st->buf_lock);
122 return ret;
125 static int ade7854_i2c_read_reg_16(struct device *dev,
126 u16 reg_address,
127 u16 *val)
129 struct iio_dev *indio_dev = dev_get_drvdata(dev);
130 struct ade7854_state *st = iio_priv(indio_dev);
131 int ret;
133 mutex_lock(&st->buf_lock);
134 st->tx[0] = (reg_address >> 8) & 0xFF;
135 st->tx[1] = reg_address & 0xFF;
137 ret = i2c_master_send(st->i2c, st->tx, 2);
138 if (ret)
139 goto out;
141 ret = i2c_master_recv(st->i2c, st->rx, 2);
142 if (ret)
143 goto out;
145 *val = (st->rx[0] << 8) | st->rx[1];
146 out:
147 mutex_unlock(&st->buf_lock);
148 return ret;
151 static int ade7854_i2c_read_reg_24(struct device *dev,
152 u16 reg_address,
153 u32 *val)
155 struct iio_dev *indio_dev = dev_get_drvdata(dev);
156 struct ade7854_state *st = iio_priv(indio_dev);
157 int ret;
159 mutex_lock(&st->buf_lock);
160 st->tx[0] = (reg_address >> 8) & 0xFF;
161 st->tx[1] = reg_address & 0xFF;
163 ret = i2c_master_send(st->i2c, st->tx, 2);
164 if (ret)
165 goto out;
167 ret = i2c_master_recv(st->i2c, st->rx, 3);
168 if (ret)
169 goto out;
171 *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
172 out:
173 mutex_unlock(&st->buf_lock);
174 return ret;
177 static int ade7854_i2c_read_reg_32(struct device *dev,
178 u16 reg_address,
179 u32 *val)
181 struct iio_dev *indio_dev = dev_get_drvdata(dev);
182 struct ade7854_state *st = iio_priv(indio_dev);
183 int ret;
185 mutex_lock(&st->buf_lock);
186 st->tx[0] = (reg_address >> 8) & 0xFF;
187 st->tx[1] = reg_address & 0xFF;
189 ret = i2c_master_send(st->i2c, st->tx, 2);
190 if (ret)
191 goto out;
193 ret = i2c_master_recv(st->i2c, st->rx, 3);
194 if (ret)
195 goto out;
197 *val = (st->rx[0] << 24) | (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
198 out:
199 mutex_unlock(&st->buf_lock);
200 return ret;
203 static int __devinit ade7854_i2c_probe(struct i2c_client *client,
204 const struct i2c_device_id *id)
206 int ret;
207 struct ade7854_state *st;
208 struct iio_dev *indio_dev;
210 indio_dev = iio_allocate_device(sizeof(*st));
211 if (indio_dev == NULL)
212 return -ENOMEM;
213 st = iio_priv(indio_dev);
214 i2c_set_clientdata(client, indio_dev);
215 st->read_reg_8 = ade7854_i2c_read_reg_8;
216 st->read_reg_16 = ade7854_i2c_read_reg_16;
217 st->read_reg_24 = ade7854_i2c_read_reg_24;
218 st->read_reg_32 = ade7854_i2c_read_reg_32;
219 st->write_reg_8 = ade7854_i2c_write_reg_8;
220 st->write_reg_16 = ade7854_i2c_write_reg_16;
221 st->write_reg_24 = ade7854_i2c_write_reg_24;
222 st->write_reg_32 = ade7854_i2c_write_reg_32;
223 st->i2c = client;
224 st->irq = client->irq;
226 ret = ade7854_probe(indio_dev, &client->dev);
227 if (ret)
228 iio_free_device(indio_dev);
230 return ret;
233 static int __devexit ade7854_i2c_remove(struct i2c_client *client)
235 return ade7854_remove(i2c_get_clientdata(client));
238 static const struct i2c_device_id ade7854_id[] = {
239 { "ade7854", 0 },
240 { "ade7858", 0 },
241 { "ade7868", 0 },
242 { "ade7878", 0 },
245 MODULE_DEVICE_TABLE(i2c, ade7854_id);
247 static struct i2c_driver ade7854_i2c_driver = {
248 .driver = {
249 .name = "ade7854",
251 .probe = ade7854_i2c_probe,
252 .remove = __devexit_p(ade7854_i2c_remove),
253 .id_table = ade7854_id,
256 static __init int ade7854_i2c_init(void)
258 return i2c_add_driver(&ade7854_i2c_driver);
260 module_init(ade7854_i2c_init);
262 static __exit void ade7854_i2c_exit(void)
264 i2c_del_driver(&ade7854_i2c_driver);
266 module_exit(ade7854_i2c_exit);
269 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
270 MODULE_DESCRIPTION("Analog Devices ADE7854/58/68/78 Polyphase Multifunction Energy Metering IC I2C Driver");
271 MODULE_LICENSE("GPL v2");