staging:iio: use the new central name attribute creation code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / meter / ade7754.c
blob4179325c8d787241999fe0ffd2e92c942f2844fb
1 /*
2 * ADE7754 Polyphase Multifunction Energy Metering IC Driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "meter.h"
24 #include "ade7754.h"
26 static int ade7754_spi_write_reg_8(struct device *dev,
27 u8 reg_address,
28 u8 val)
30 int ret;
31 struct iio_dev *indio_dev = dev_get_drvdata(dev);
32 struct ade7754_state *st = iio_dev_get_devdata(indio_dev);
34 mutex_lock(&st->buf_lock);
35 st->tx[0] = ADE7754_WRITE_REG(reg_address);
36 st->tx[1] = val;
38 ret = spi_write(st->us, st->tx, 2);
39 mutex_unlock(&st->buf_lock);
41 return ret;
44 static int ade7754_spi_write_reg_16(struct device *dev,
45 u8 reg_address,
46 u16 value)
48 int ret;
49 struct iio_dev *indio_dev = dev_get_drvdata(dev);
50 struct ade7754_state *st = iio_dev_get_devdata(indio_dev);
52 mutex_lock(&st->buf_lock);
53 st->tx[0] = ADE7754_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);
59 return ret;
62 static int ade7754_spi_read_reg_8(struct device *dev,
63 u8 reg_address,
64 u8 *val)
66 struct iio_dev *indio_dev = dev_get_drvdata(dev);
67 struct ade7754_state *st = iio_dev_get_devdata(indio_dev);
68 int ret;
70 ret = spi_w8r8(st->us, ADE7754_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;
76 *val = ret;
78 return 0;
81 static int ade7754_spi_read_reg_16(struct device *dev,
82 u8 reg_address,
83 u16 *val)
85 struct iio_dev *indio_dev = dev_get_drvdata(dev);
86 struct ade7754_state *st = iio_dev_get_devdata(indio_dev);
87 int ret;
89 ret = spi_w8r16(st->us, ADE7754_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;
96 *val = ret;
97 *val = be16_to_cpup(val);
99 return 0;
102 static int ade7754_spi_read_reg_24(struct device *dev,
103 u8 reg_address,
104 u32 *val)
106 struct spi_message msg;
107 struct iio_dev *indio_dev = dev_get_drvdata(dev);
108 struct ade7754_state *st = iio_dev_get_devdata(indio_dev);
109 int ret;
110 struct spi_transfer xfers[] = {
112 .tx_buf = st->tx,
113 .rx_buf = st->rx,
114 .bits_per_word = 8,
115 .len = 4,
119 mutex_lock(&st->buf_lock);
120 st->tx[0] = ADE7754_READ_REG(reg_address);
121 st->tx[1] = 0;
122 st->tx[2] = 0;
123 st->tx[3] = 0;
125 spi_message_init(&msg);
126 spi_message_add_tail(xfers, &msg);
127 ret = spi_sync(st->us, &msg);
128 if (ret) {
129 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
130 reg_address);
131 goto error_ret;
133 *val = (st->rx[1] << 16) | (st->rx[2] << 8) | st->rx[3];
135 error_ret:
136 mutex_unlock(&st->buf_lock);
137 return ret;
140 static ssize_t ade7754_read_8bit(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
144 int ret;
145 u8 val = 0;
146 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
148 ret = ade7754_spi_read_reg_8(dev, this_attr->address, &val);
149 if (ret)
150 return ret;
152 return sprintf(buf, "%u\n", val);
155 static ssize_t ade7754_read_16bit(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
159 int ret;
160 u16 val = 0;
161 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
163 ret = ade7754_spi_read_reg_16(dev, this_attr->address, &val);
164 if (ret)
165 return ret;
167 return sprintf(buf, "%u\n", val);
170 static ssize_t ade7754_read_24bit(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
174 int ret;
175 u32 val = 0;
176 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
178 ret = ade7754_spi_read_reg_24(dev, this_attr->address, &val);
179 if (ret)
180 return ret;
182 return sprintf(buf, "%u\n", val & 0xFFFFFF);
185 static ssize_t ade7754_write_8bit(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf,
188 size_t len)
190 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
191 int ret;
192 long val;
194 ret = strict_strtol(buf, 10, &val);
195 if (ret)
196 goto error_ret;
197 ret = ade7754_spi_write_reg_8(dev, this_attr->address, val);
199 error_ret:
200 return ret ? ret : len;
203 static ssize_t ade7754_write_16bit(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf,
206 size_t len)
208 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
209 int ret;
210 long val;
212 ret = strict_strtol(buf, 10, &val);
213 if (ret)
214 goto error_ret;
215 ret = ade7754_spi_write_reg_16(dev, this_attr->address, val);
217 error_ret:
218 return ret ? ret : len;
221 static int ade7754_reset(struct device *dev)
223 u8 val;
225 ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
226 val |= 1 << 6; /* Software Chip Reset */
227 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
231 static ssize_t ade7754_write_reset(struct device *dev,
232 struct device_attribute *attr,
233 const char *buf, size_t len)
235 if (len < 1)
236 return -1;
237 switch (buf[0]) {
238 case '1':
239 case 'y':
240 case 'Y':
241 return ade7754_reset(dev);
243 return -1;
246 static IIO_DEV_ATTR_AENERGY(ade7754_read_24bit, ADE7754_AENERGY);
247 static IIO_DEV_ATTR_LAENERGY(ade7754_read_24bit, ADE7754_LAENERGY);
248 static IIO_DEV_ATTR_VAENERGY(ade7754_read_24bit, ADE7754_VAENERGY);
249 static IIO_DEV_ATTR_LVAENERGY(ade7754_read_24bit, ADE7754_LVAENERGY);
250 static IIO_DEV_ATTR_VPEAK(S_IWUSR | S_IRUGO,
251 ade7754_read_8bit,
252 ade7754_write_8bit,
253 ADE7754_VPEAK);
254 static IIO_DEV_ATTR_IPEAK(S_IWUSR | S_IRUGO,
255 ade7754_read_8bit,
256 ade7754_write_8bit,
257 ADE7754_VPEAK);
258 static IIO_DEV_ATTR_APHCAL(S_IWUSR | S_IRUGO,
259 ade7754_read_8bit,
260 ade7754_write_8bit,
261 ADE7754_APHCAL);
262 static IIO_DEV_ATTR_BPHCAL(S_IWUSR | S_IRUGO,
263 ade7754_read_8bit,
264 ade7754_write_8bit,
265 ADE7754_BPHCAL);
266 static IIO_DEV_ATTR_CPHCAL(S_IWUSR | S_IRUGO,
267 ade7754_read_8bit,
268 ade7754_write_8bit,
269 ADE7754_CPHCAL);
270 static IIO_DEV_ATTR_AAPOS(S_IWUSR | S_IRUGO,
271 ade7754_read_16bit,
272 ade7754_write_16bit,
273 ADE7754_AAPOS);
274 static IIO_DEV_ATTR_BAPOS(S_IWUSR | S_IRUGO,
275 ade7754_read_16bit,
276 ade7754_write_16bit,
277 ADE7754_BAPOS);
278 static IIO_DEV_ATTR_CAPOS(S_IWUSR | S_IRUGO,
279 ade7754_read_16bit,
280 ade7754_write_16bit,
281 ADE7754_CAPOS);
282 static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
283 ade7754_read_8bit,
284 ade7754_write_8bit,
285 ADE7754_WDIV);
286 static IIO_DEV_ATTR_VADIV(S_IWUSR | S_IRUGO,
287 ade7754_read_8bit,
288 ade7754_write_8bit,
289 ADE7754_VADIV);
290 static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
291 ade7754_read_16bit,
292 ade7754_write_16bit,
293 ADE7754_CFNUM);
294 static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
295 ade7754_read_16bit,
296 ade7754_write_16bit,
297 ADE7754_CFDEN);
298 static IIO_DEV_ATTR_ACTIVE_POWER_A_GAIN(S_IWUSR | S_IRUGO,
299 ade7754_read_16bit,
300 ade7754_write_16bit,
301 ADE7754_AAPGAIN);
302 static IIO_DEV_ATTR_ACTIVE_POWER_B_GAIN(S_IWUSR | S_IRUGO,
303 ade7754_read_16bit,
304 ade7754_write_16bit,
305 ADE7754_BAPGAIN);
306 static IIO_DEV_ATTR_ACTIVE_POWER_C_GAIN(S_IWUSR | S_IRUGO,
307 ade7754_read_16bit,
308 ade7754_write_16bit,
309 ADE7754_CAPGAIN);
310 static IIO_DEV_ATTR_AIRMS(S_IRUGO,
311 ade7754_read_24bit,
312 NULL,
313 ADE7754_AIRMS);
314 static IIO_DEV_ATTR_BIRMS(S_IRUGO,
315 ade7754_read_24bit,
316 NULL,
317 ADE7754_BIRMS);
318 static IIO_DEV_ATTR_CIRMS(S_IRUGO,
319 ade7754_read_24bit,
320 NULL,
321 ADE7754_CIRMS);
322 static IIO_DEV_ATTR_AVRMS(S_IRUGO,
323 ade7754_read_24bit,
324 NULL,
325 ADE7754_AVRMS);
326 static IIO_DEV_ATTR_BVRMS(S_IRUGO,
327 ade7754_read_24bit,
328 NULL,
329 ADE7754_BVRMS);
330 static IIO_DEV_ATTR_CVRMS(S_IRUGO,
331 ade7754_read_24bit,
332 NULL,
333 ADE7754_CVRMS);
334 static IIO_DEV_ATTR_AIRMSOS(S_IRUGO,
335 ade7754_read_16bit,
336 ade7754_write_16bit,
337 ADE7754_AIRMSOS);
338 static IIO_DEV_ATTR_BIRMSOS(S_IRUGO,
339 ade7754_read_16bit,
340 ade7754_write_16bit,
341 ADE7754_BIRMSOS);
342 static IIO_DEV_ATTR_CIRMSOS(S_IRUGO,
343 ade7754_read_16bit,
344 ade7754_write_16bit,
345 ADE7754_CIRMSOS);
346 static IIO_DEV_ATTR_AVRMSOS(S_IRUGO,
347 ade7754_read_16bit,
348 ade7754_write_16bit,
349 ADE7754_AVRMSOS);
350 static IIO_DEV_ATTR_BVRMSOS(S_IRUGO,
351 ade7754_read_16bit,
352 ade7754_write_16bit,
353 ADE7754_BVRMSOS);
354 static IIO_DEV_ATTR_CVRMSOS(S_IRUGO,
355 ade7754_read_16bit,
356 ade7754_write_16bit,
357 ADE7754_CVRMSOS);
359 static int ade7754_set_irq(struct device *dev, bool enable)
361 int ret;
362 u16 irqen;
363 ret = ade7754_spi_read_reg_16(dev, ADE7754_IRQEN, &irqen);
364 if (ret)
365 goto error_ret;
367 if (enable)
368 irqen |= 1 << 14; /* Enables an interrupt when a data is
369 present in the waveform register */
370 else
371 irqen &= ~(1 << 14);
373 ret = ade7754_spi_write_reg_16(dev, ADE7754_IRQEN, irqen);
374 if (ret)
375 goto error_ret;
377 error_ret:
378 return ret;
381 /* Power down the device */
382 static int ade7754_stop_device(struct device *dev)
384 u8 val;
386 ade7754_spi_read_reg_8(dev, ADE7754_OPMODE, &val);
387 val |= 7 << 3; /* ADE7754 powered down */
388 return ade7754_spi_write_reg_8(dev, ADE7754_OPMODE, val);
391 static int ade7754_initial_setup(struct ade7754_state *st)
393 int ret;
394 struct device *dev = &st->indio_dev->dev;
396 /* use low spi speed for init */
397 st->us->mode = SPI_MODE_3;
398 spi_setup(st->us);
400 /* Disable IRQ */
401 ret = ade7754_set_irq(dev, false);
402 if (ret) {
403 dev_err(dev, "disable irq failed");
404 goto err_ret;
407 ade7754_reset(dev);
408 msleep(ADE7754_STARTUP_DELAY);
410 err_ret:
411 return ret;
414 static ssize_t ade7754_read_frequency(struct device *dev,
415 struct device_attribute *attr,
416 char *buf)
418 int ret;
419 u8 t;
420 int sps;
421 ret = ade7754_spi_read_reg_8(dev,
422 ADE7754_WAVMODE,
423 &t);
424 if (ret)
425 return ret;
427 t = (t >> 3) & 0x3;
428 sps = 26000 / (1 + t);
430 return sprintf(buf, "%d\n", sps);
433 static ssize_t ade7754_write_frequency(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf,
436 size_t len)
438 struct iio_dev *indio_dev = dev_get_drvdata(dev);
439 struct ade7754_state *st = iio_dev_get_devdata(indio_dev);
440 unsigned long val;
441 int ret;
442 u8 reg, t;
444 ret = strict_strtol(buf, 10, &val);
445 if (ret)
446 return ret;
448 mutex_lock(&indio_dev->mlock);
450 t = (26000 / val);
451 if (t > 0)
452 t--;
454 if (t > 1)
455 st->us->max_speed_hz = ADE7754_SPI_SLOW;
456 else
457 st->us->max_speed_hz = ADE7754_SPI_FAST;
459 ret = ade7754_spi_read_reg_8(dev, ADE7754_WAVMODE, &reg);
460 if (ret)
461 goto out;
463 reg &= ~(3 << 3);
464 reg |= t << 3;
466 ret = ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg);
468 out:
469 mutex_unlock(&indio_dev->mlock);
471 return ret ? ret : len;
473 static IIO_DEV_ATTR_TEMP_RAW(ade7754_read_8bit);
474 static IIO_CONST_ATTR(temp_offset, "129 C");
475 static IIO_CONST_ATTR(temp_scale, "4 C");
477 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
478 ade7754_read_frequency,
479 ade7754_write_frequency);
481 static IIO_DEV_ATTR_RESET(ade7754_write_reset);
483 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("26000 13000 65000 33000");
485 static struct attribute *ade7754_attributes[] = {
486 &iio_dev_attr_temp_raw.dev_attr.attr,
487 &iio_const_attr_temp_offset.dev_attr.attr,
488 &iio_const_attr_temp_scale.dev_attr.attr,
489 &iio_dev_attr_sampling_frequency.dev_attr.attr,
490 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
491 &iio_dev_attr_reset.dev_attr.attr,
492 &iio_dev_attr_aenergy.dev_attr.attr,
493 &iio_dev_attr_laenergy.dev_attr.attr,
494 &iio_dev_attr_vaenergy.dev_attr.attr,
495 &iio_dev_attr_lvaenergy.dev_attr.attr,
496 &iio_dev_attr_vpeak.dev_attr.attr,
497 &iio_dev_attr_ipeak.dev_attr.attr,
498 &iio_dev_attr_aphcal.dev_attr.attr,
499 &iio_dev_attr_bphcal.dev_attr.attr,
500 &iio_dev_attr_cphcal.dev_attr.attr,
501 &iio_dev_attr_aapos.dev_attr.attr,
502 &iio_dev_attr_bapos.dev_attr.attr,
503 &iio_dev_attr_capos.dev_attr.attr,
504 &iio_dev_attr_wdiv.dev_attr.attr,
505 &iio_dev_attr_vadiv.dev_attr.attr,
506 &iio_dev_attr_cfnum.dev_attr.attr,
507 &iio_dev_attr_cfden.dev_attr.attr,
508 &iio_dev_attr_active_power_a_gain.dev_attr.attr,
509 &iio_dev_attr_active_power_b_gain.dev_attr.attr,
510 &iio_dev_attr_active_power_c_gain.dev_attr.attr,
511 &iio_dev_attr_airms.dev_attr.attr,
512 &iio_dev_attr_birms.dev_attr.attr,
513 &iio_dev_attr_cirms.dev_attr.attr,
514 &iio_dev_attr_avrms.dev_attr.attr,
515 &iio_dev_attr_bvrms.dev_attr.attr,
516 &iio_dev_attr_cvrms.dev_attr.attr,
517 &iio_dev_attr_airmsos.dev_attr.attr,
518 &iio_dev_attr_birmsos.dev_attr.attr,
519 &iio_dev_attr_cirmsos.dev_attr.attr,
520 &iio_dev_attr_avrmsos.dev_attr.attr,
521 &iio_dev_attr_bvrmsos.dev_attr.attr,
522 &iio_dev_attr_cvrmsos.dev_attr.attr,
523 NULL,
526 static const struct attribute_group ade7754_attribute_group = {
527 .attrs = ade7754_attributes,
532 static int __devinit ade7754_probe(struct spi_device *spi)
534 int ret, regdone = 0;
535 struct ade7754_state *st = kzalloc(sizeof *st, GFP_KERNEL);
536 if (!st) {
537 ret = -ENOMEM;
538 goto error_ret;
540 /* this is only used for removal purposes */
541 spi_set_drvdata(spi, st);
543 /* Allocate the comms buffers */
544 st->rx = kzalloc(sizeof(*st->rx)*ADE7754_MAX_RX, GFP_KERNEL);
545 if (st->rx == NULL) {
546 ret = -ENOMEM;
547 goto error_free_st;
549 st->tx = kzalloc(sizeof(*st->tx)*ADE7754_MAX_TX, GFP_KERNEL);
550 if (st->tx == NULL) {
551 ret = -ENOMEM;
552 goto error_free_rx;
554 st->us = spi;
555 mutex_init(&st->buf_lock);
556 /* setup the industrialio driver allocated elements */
557 st->indio_dev = iio_allocate_device(0);
558 if (st->indio_dev == NULL) {
559 ret = -ENOMEM;
560 goto error_free_tx;
563 st->indio_dev->name = spi->dev.driver->name;
564 st->indio_dev->dev.parent = &spi->dev;
565 st->indio_dev->attrs = &ade7754_attribute_group;
566 st->indio_dev->dev_data = (void *)(st);
567 st->indio_dev->driver_module = THIS_MODULE;
568 st->indio_dev->modes = INDIO_DIRECT_MODE;
570 ret = iio_device_register(st->indio_dev);
571 if (ret)
572 goto error_free_dev;
573 regdone = 1;
575 /* Get the device into a sane initial state */
576 ret = ade7754_initial_setup(st);
577 if (ret)
578 goto error_free_dev;
579 return 0;
581 error_free_dev:
582 if (regdone)
583 iio_device_unregister(st->indio_dev);
584 else
585 iio_free_device(st->indio_dev);
586 error_free_tx:
587 kfree(st->tx);
588 error_free_rx:
589 kfree(st->rx);
590 error_free_st:
591 kfree(st);
592 error_ret:
593 return ret;
596 /* fixme, confirm ordering in this function */
597 static int ade7754_remove(struct spi_device *spi)
599 int ret;
600 struct ade7754_state *st = spi_get_drvdata(spi);
601 struct iio_dev *indio_dev = st->indio_dev;
603 ret = ade7754_stop_device(&(indio_dev->dev));
604 if (ret)
605 goto err_ret;
607 iio_device_unregister(indio_dev);
608 kfree(st->tx);
609 kfree(st->rx);
610 kfree(st);
612 return 0;
614 err_ret:
615 return ret;
618 static struct spi_driver ade7754_driver = {
619 .driver = {
620 .name = "ade7754",
621 .owner = THIS_MODULE,
623 .probe = ade7754_probe,
624 .remove = __devexit_p(ade7754_remove),
627 static __init int ade7754_init(void)
629 return spi_register_driver(&ade7754_driver);
631 module_init(ade7754_init);
633 static __exit void ade7754_exit(void)
635 spi_unregister_driver(&ade7754_driver);
637 module_exit(ade7754_exit);
639 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
640 MODULE_DESCRIPTION("Analog Devices ADE7754 Polyphase Multifunction Energy Metering IC Driver");
641 MODULE_LICENSE("GPL v2");