1 /*
2 * ADE7759 Active Energy Metering IC with di/dt Sensor Interface Driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
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>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include "meter.h"
24 #include "ade7759.h"
25
ade7759_spi_write_reg_8(struct device * dev,u8 reg_address,u8 val)26 static int ade7759_spi_write_reg_8(struct device *dev,
27 u8 reg_address,
28 u8 val)
29 {
30 int ret;
31 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
32 struct ade7759_state *st = iio_priv(indio_dev);
33
34 mutex_lock(&st->buf_lock);
35 st->tx[0] = ADE7759_WRITE_REG(reg_address);
36 st->tx[1] = val;
37
38 ret = spi_write(st->us, st->tx, 2);
39 mutex_unlock(&st->buf_lock);
40
41 return ret;
42 }
43
ade7759_spi_write_reg_16(struct device * dev,u8 reg_address,u16 value)44 static int ade7759_spi_write_reg_16(struct device *dev,
45 u8 reg_address,
46 u16 value)
47 {
48 int ret;
49 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
50 struct ade7759_state *st = iio_priv(indio_dev);
51
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);
58
59 return ret;
60 }
61
ade7759_spi_read_reg_8(struct device * dev,u8 reg_address,u8 * val)62 static int ade7759_spi_read_reg_8(struct device *dev,
63 u8 reg_address,
64 u8 *val)
65 {
66 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
67 struct ade7759_state *st = iio_priv(indio_dev);
68 int ret;
69
70 ret = spi_w8r8(st->us, ADE7759_READ_REG(reg_address));
71 if (ret < 0) {
72 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
73 reg_address);
74 return ret;
75 }
76 *val = ret;
77
78 return 0;
79 }
80
ade7759_spi_read_reg_16(struct device * dev,u8 reg_address,u16 * val)81 static int ade7759_spi_read_reg_16(struct device *dev,
82 u8 reg_address,
83 u16 *val)
84 {
85 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
86 struct ade7759_state *st = iio_priv(indio_dev);
87 int ret;
88
89 ret = spi_w8r16be(st->us, ADE7759_READ_REG(reg_address));
90 if (ret < 0) {
91 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
92 reg_address);
93 return ret;
94 }
95
96 *val = ret;
97
98 return 0;
99 }
100
ade7759_spi_read_reg_40(struct device * dev,u8 reg_address,u64 * val)101 static int ade7759_spi_read_reg_40(struct device *dev,
102 u8 reg_address,
103 u64 *val)
104 {
105 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
106 struct ade7759_state *st = iio_priv(indio_dev);
107 int ret;
108 struct spi_transfer xfers[] = {
109 {
110 .tx_buf = st->tx,
111 .rx_buf = st->rx,
112 .bits_per_word = 8,
113 .len = 6,
114 },
115 };
116
117 mutex_lock(&st->buf_lock);
118 st->tx[0] = ADE7759_READ_REG(reg_address);
119 memset(&st->tx[1], 0 , 5);
120
121 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
122 if (ret) {
123 dev_err(&st->us->dev, "problem when reading 40 bit register 0x%02X",
124 reg_address);
125 goto error_ret;
126 }
127 *val = ((u64)st->rx[1] << 32) | (st->rx[2] << 24) |
128 (st->rx[3] << 16) | (st->rx[4] << 8) | st->rx[5];
129
130 error_ret:
131 mutex_unlock(&st->buf_lock);
132 return ret;
133 }
134
ade7759_read_8bit(struct device * dev,struct device_attribute * attr,char * buf)135 static ssize_t ade7759_read_8bit(struct device *dev,
136 struct device_attribute *attr,
137 char *buf)
138 {
139 int ret;
140 u8 val = 0;
141 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
142
143 ret = ade7759_spi_read_reg_8(dev, this_attr->address, &val);
144 if (ret)
145 return ret;
146
147 return sprintf(buf, "%u\n", val);
148 }
149
ade7759_read_16bit(struct device * dev,struct device_attribute * attr,char * buf)150 static ssize_t ade7759_read_16bit(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
153 {
154 int ret;
155 u16 val = 0;
156 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
157
158 ret = ade7759_spi_read_reg_16(dev, this_attr->address, &val);
159 if (ret)
160 return ret;
161
162 return sprintf(buf, "%u\n", val);
163 }
164
ade7759_read_40bit(struct device * dev,struct device_attribute * attr,char * buf)165 static ssize_t ade7759_read_40bit(struct device *dev,
166 struct device_attribute *attr,
167 char *buf)
168 {
169 int ret;
170 u64 val = 0;
171 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
172
173 ret = ade7759_spi_read_reg_40(dev, this_attr->address, &val);
174 if (ret)
175 return ret;
176
177 return sprintf(buf, "%llu\n", val);
178 }
179
ade7759_write_8bit(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)180 static ssize_t ade7759_write_8bit(struct device *dev,
181 struct device_attribute *attr,
182 const char *buf,
183 size_t len)
184 {
185 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
186 int ret;
187 u8 val;
188
189 ret = kstrtou8(buf, 10, &val);
190 if (ret)
191 goto error_ret;
192 ret = ade7759_spi_write_reg_8(dev, this_attr->address, val);
193
194 error_ret:
195 return ret ? ret : len;
196 }
197
ade7759_write_16bit(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)198 static ssize_t ade7759_write_16bit(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf,
201 size_t len)
202 {
203 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
204 int ret;
205 u16 val;
206
207 ret = kstrtou16(buf, 10, &val);
208 if (ret)
209 goto error_ret;
210 ret = ade7759_spi_write_reg_16(dev, this_attr->address, val);
211
212 error_ret:
213 return ret ? ret : len;
214 }
215
ade7759_reset(struct device * dev)216 static int ade7759_reset(struct device *dev)
217 {
218 int ret;
219 u16 val;
220
221 ade7759_spi_read_reg_16(dev,
222 ADE7759_MODE,
223 &val);
224 val |= 1 << 6; /* Software Chip Reset */
225 ret = ade7759_spi_write_reg_16(dev,
226 ADE7759_MODE,
227 val);
228
229 return ret;
230 }
231
232 static IIO_DEV_ATTR_AENERGY(ade7759_read_40bit, ADE7759_AENERGY);
233 static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
234 ade7759_read_16bit,
235 ade7759_write_16bit,
236 ADE7759_CFDEN);
237 static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
238 ade7759_read_8bit,
239 ade7759_write_8bit,
240 ADE7759_CFNUM);
241 static IIO_DEV_ATTR_CHKSUM(ade7759_read_8bit, ADE7759_CHKSUM);
242 static IIO_DEV_ATTR_PHCAL(S_IWUSR | S_IRUGO,
243 ade7759_read_16bit,
244 ade7759_write_16bit,
245 ADE7759_PHCAL);
246 static IIO_DEV_ATTR_APOS(S_IWUSR | S_IRUGO,
247 ade7759_read_16bit,
248 ade7759_write_16bit,
249 ADE7759_APOS);
250 static IIO_DEV_ATTR_SAGCYC(S_IWUSR | S_IRUGO,
251 ade7759_read_8bit,
252 ade7759_write_8bit,
253 ADE7759_SAGCYC);
254 static IIO_DEV_ATTR_SAGLVL(S_IWUSR | S_IRUGO,
255 ade7759_read_8bit,
256 ade7759_write_8bit,
257 ADE7759_SAGLVL);
258 static IIO_DEV_ATTR_LINECYC(S_IWUSR | S_IRUGO,
259 ade7759_read_8bit,
260 ade7759_write_8bit,
261 ADE7759_LINECYC);
262 static IIO_DEV_ATTR_LENERGY(ade7759_read_40bit, ADE7759_LENERGY);
263 static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR | S_IRUGO,
264 ade7759_read_8bit,
265 ade7759_write_8bit,
266 ADE7759_GAIN);
267 static IIO_DEV_ATTR_ACTIVE_POWER_GAIN(S_IWUSR | S_IRUGO,
268 ade7759_read_16bit,
269 ade7759_write_16bit,
270 ADE7759_APGAIN);
271 static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR | S_IRUGO,
272 ade7759_read_8bit,
273 ade7759_write_8bit,
274 ADE7759_CH1OS);
275 static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR | S_IRUGO,
276 ade7759_read_8bit,
277 ade7759_write_8bit,
278 ADE7759_CH2OS);
279
ade7759_set_irq(struct device * dev,bool enable)280 static int ade7759_set_irq(struct device *dev, bool enable)
281 {
282 int ret;
283 u8 irqen;
284
285 ret = ade7759_spi_read_reg_8(dev, ADE7759_IRQEN, &irqen);
286 if (ret)
287 goto error_ret;
288
289 if (enable)
290 irqen |= 1 << 3; /* Enables an interrupt when a data is
291 present in the waveform register */
292 else
293 irqen &= ~(1 << 3);
294
295 ret = ade7759_spi_write_reg_8(dev, ADE7759_IRQEN, irqen);
296
297 error_ret:
298 return ret;
299 }
300
301 /* Power down the device */
ade7759_stop_device(struct device * dev)302 static int ade7759_stop_device(struct device *dev)
303 {
304 u16 val;
305
306 ade7759_spi_read_reg_16(dev,
307 ADE7759_MODE,
308 &val);
309 val |= 1 << 4; /* AD converters can be turned off */
310
311 return ade7759_spi_write_reg_16(dev, ADE7759_MODE, val);
312 }
313
ade7759_initial_setup(struct iio_dev * indio_dev)314 static int ade7759_initial_setup(struct iio_dev *indio_dev)
315 {
316 int ret;
317 struct ade7759_state *st = iio_priv(indio_dev);
318 struct device *dev = &indio_dev->dev;
319
320 /* use low spi speed for init */
321 st->us->mode = SPI_MODE_3;
322 spi_setup(st->us);
323
324 /* Disable IRQ */
325 ret = ade7759_set_irq(dev, false);
326 if (ret) {
327 dev_err(dev, "disable irq failed");
328 goto err_ret;
329 }
330
331 ade7759_reset(dev);
332 msleep(ADE7759_STARTUP_DELAY);
333
334 err_ret:
335 return ret;
336 }
337
ade7759_read_frequency(struct device * dev,struct device_attribute * attr,char * buf)338 static ssize_t ade7759_read_frequency(struct device *dev,
339 struct device_attribute *attr,
340 char *buf)
341 {
342 int ret;
343 u16 t;
344 int sps;
345
346 ret = ade7759_spi_read_reg_16(dev,
347 ADE7759_MODE,
348 &t);
349 if (ret)
350 return ret;
351
352 t = (t >> 3) & 0x3;
353 sps = 27900 / (1 + t);
354
355 return sprintf(buf, "%d\n", sps);
356 }
357
ade7759_write_frequency(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)358 static ssize_t ade7759_write_frequency(struct device *dev,
359 struct device_attribute *attr,
360 const char *buf,
361 size_t len)
362 {
363 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
364 struct ade7759_state *st = iio_priv(indio_dev);
365 u16 val;
366 int ret;
367 u16 reg, t;
368
369 ret = kstrtou16(buf, 10, &val);
370 if (ret)
371 return ret;
372 if (val == 0)
373 return -EINVAL;
374
375 mutex_lock(&indio_dev->mlock);
376
377 t = (27900 / val);
378 if (t > 0)
379 t--;
380
381 if (t > 1)
382 st->us->max_speed_hz = ADE7759_SPI_SLOW;
383 else
384 st->us->max_speed_hz = ADE7759_SPI_FAST;
385
386 ret = ade7759_spi_read_reg_16(dev, ADE7759_MODE, ®);
387 if (ret)
388 goto out;
389
390 reg &= ~(3 << 13);
391 reg |= t << 13;
392
393 ret = ade7759_spi_write_reg_16(dev, ADE7759_MODE, reg);
394
395 out:
396 mutex_unlock(&indio_dev->mlock);
397
398 return ret ? ret : len;
399 }
400 static IIO_DEV_ATTR_TEMP_RAW(ade7759_read_8bit);
401 static IIO_CONST_ATTR(in_temp_offset, "70 C");
402 static IIO_CONST_ATTR(in_temp_scale, "1 C");
403
404 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
405 ade7759_read_frequency,
406 ade7759_write_frequency);
407
408 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
409
410 static struct attribute *ade7759_attributes[] = {
411 &iio_dev_attr_in_temp_raw.dev_attr.attr,
412 &iio_const_attr_in_temp_offset.dev_attr.attr,
413 &iio_const_attr_in_temp_scale.dev_attr.attr,
414 &iio_dev_attr_sampling_frequency.dev_attr.attr,
415 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
416 &iio_dev_attr_phcal.dev_attr.attr,
417 &iio_dev_attr_cfden.dev_attr.attr,
418 &iio_dev_attr_aenergy.dev_attr.attr,
419 &iio_dev_attr_cfnum.dev_attr.attr,
420 &iio_dev_attr_apos.dev_attr.attr,
421 &iio_dev_attr_sagcyc.dev_attr.attr,
422 &iio_dev_attr_saglvl.dev_attr.attr,
423 &iio_dev_attr_linecyc.dev_attr.attr,
424 &iio_dev_attr_lenergy.dev_attr.attr,
425 &iio_dev_attr_chksum.dev_attr.attr,
426 &iio_dev_attr_pga_gain.dev_attr.attr,
427 &iio_dev_attr_active_power_gain.dev_attr.attr,
428 &iio_dev_attr_choff_1.dev_attr.attr,
429 &iio_dev_attr_choff_2.dev_attr.attr,
430 NULL,
431 };
432
433 static const struct attribute_group ade7759_attribute_group = {
434 .attrs = ade7759_attributes,
435 };
436
437 static const struct iio_info ade7759_info = {
438 .attrs = &ade7759_attribute_group,
439 .driver_module = THIS_MODULE,
440 };
441
ade7759_probe(struct spi_device * spi)442 static int ade7759_probe(struct spi_device *spi)
443 {
444 int ret;
445 struct ade7759_state *st;
446 struct iio_dev *indio_dev;
447
448 /* setup the industrialio driver allocated elements */
449 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
450 if (!indio_dev)
451 return -ENOMEM;
452 /* this is only used for removal purposes */
453 spi_set_drvdata(spi, indio_dev);
454
455 st = iio_priv(indio_dev);
456 st->us = spi;
457 mutex_init(&st->buf_lock);
458 indio_dev->name = spi->dev.driver->name;
459 indio_dev->dev.parent = &spi->dev;
460 indio_dev->info = &ade7759_info;
461 indio_dev->modes = INDIO_DIRECT_MODE;
462
463 /* Get the device into a sane initial state */
464 ret = ade7759_initial_setup(indio_dev);
465 if (ret)
466 return ret;
467
468 ret = iio_device_register(indio_dev);
469 if (ret)
470 return ret;
471
472 return 0;
473 }
474
475 /* fixme, confirm ordering in this function */
ade7759_remove(struct spi_device * spi)476 static int ade7759_remove(struct spi_device *spi)
477 {
478 struct iio_dev *indio_dev = spi_get_drvdata(spi);
479
480 iio_device_unregister(indio_dev);
481 ade7759_stop_device(&indio_dev->dev);
482
483 return 0;
484 }
485
486 static struct spi_driver ade7759_driver = {
487 .driver = {
488 .name = "ade7759",
489 .owner = THIS_MODULE,
490 },
491 .probe = ade7759_probe,
492 .remove = ade7759_remove,
493 };
494 module_spi_driver(ade7759_driver);
495
496 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
497 MODULE_DESCRIPTION("Analog Devices ADE7759 Active Energy Metering IC Driver");
498 MODULE_LICENSE("GPL v2");
499 MODULE_ALIAS("spi:ad7759");
500