Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / staging / iio / meter / ade7759.c
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/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>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "meter.h"
24 #include "ade7759.h"
25
26 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_get_drvdata(dev);
32         struct ade7759_state *st = iio_dev_get_devdata(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
44 static int ade7759_spi_write_reg_16(struct device *dev,
45                 u8 reg_address,
46                 u16 value)
47 {
48         int ret;
49         struct spi_message msg;
50         struct iio_dev *indio_dev = dev_get_drvdata(dev);
51         struct ade7759_state *st = iio_dev_get_devdata(indio_dev);
52         struct spi_transfer xfers[] = {
53                 {
54                         .tx_buf = st->tx,
55                         .bits_per_word = 8,
56                         .len = 3,
57                 }
58         };
59
60         mutex_lock(&st->buf_lock);
61         st->tx[0] = ADE7759_WRITE_REG(reg_address);
62         st->tx[1] = (value >> 8) & 0xFF;
63         st->tx[2] = value & 0xFF;
64
65         spi_message_init(&msg);
66         spi_message_add_tail(xfers, &msg);
67         ret = spi_sync(st->us, &msg);
68         mutex_unlock(&st->buf_lock);
69
70         return ret;
71 }
72
73 static int ade7759_spi_read_reg_8(struct device *dev,
74                 u8 reg_address,
75                 u8 *val)
76 {
77         struct spi_message msg;
78         struct iio_dev *indio_dev = dev_get_drvdata(dev);
79         struct ade7759_state *st = iio_dev_get_devdata(indio_dev);
80         int ret;
81         struct spi_transfer xfers[] = {
82                 {
83                         .tx_buf = st->tx,
84                         .rx_buf = st->rx,
85                         .bits_per_word = 8,
86                         .len = 2,
87                 },
88         };
89
90         mutex_lock(&st->buf_lock);
91         st->tx[0] = ADE7759_READ_REG(reg_address);
92         st->tx[1] = 0;
93
94         spi_message_init(&msg);
95         spi_message_add_tail(xfers, &msg);
96         ret = spi_sync(st->us, &msg);
97         if (ret) {
98                 dev_err(&st->us->dev, "problem when reading 8 bit register 0x%02X",
99                                 reg_address);
100                 goto error_ret;
101         }
102         *val = st->rx[1];
103
104 error_ret:
105         mutex_unlock(&st->buf_lock);
106         return ret;
107 }
108
109 static int ade7759_spi_read_reg_16(struct device *dev,
110                 u8 reg_address,
111                 u16 *val)
112 {
113         struct spi_message msg;
114         struct iio_dev *indio_dev = dev_get_drvdata(dev);
115         struct ade7759_state *st = iio_dev_get_devdata(indio_dev);
116         int ret;
117         struct spi_transfer xfers[] = {
118                 {
119                         .tx_buf = st->tx,
120                         .rx_buf = st->rx,
121                         .bits_per_word = 8,
122                         .len = 3,
123                 },
124         };
125
126         mutex_lock(&st->buf_lock);
127         st->tx[0] = ADE7759_READ_REG(reg_address);
128         st->tx[1] = 0;
129         st->tx[2] = 0;
130
131         spi_message_init(&msg);
132         spi_message_add_tail(xfers, &msg);
133         ret = spi_sync(st->us, &msg);
134         if (ret) {
135                 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
136                                 reg_address);
137                 goto error_ret;
138         }
139         *val = (st->rx[1] << 8) | st->rx[2];
140
141 error_ret:
142         mutex_unlock(&st->buf_lock);
143         return ret;
144 }
145
146 static int ade7759_spi_read_reg_40(struct device *dev,
147                 u8 reg_address,
148                 u64 *val)
149 {
150         struct spi_message msg;
151         struct iio_dev *indio_dev = dev_get_drvdata(dev);
152         struct ade7759_state *st = iio_dev_get_devdata(indio_dev);
153         int ret;
154         struct spi_transfer xfers[] = {
155                 {
156                         .tx_buf = st->tx,
157                         .rx_buf = st->rx,
158                         .bits_per_word = 8,
159                         .len = 6,
160                 },
161         };
162
163         mutex_lock(&st->buf_lock);
164         st->tx[0] = ADE7759_READ_REG(reg_address);
165         memset(&st->tx[1], 0 , 5);
166
167         spi_message_init(&msg);
168         spi_message_add_tail(xfers, &msg);
169         ret = spi_sync(st->us, &msg);
170         if (ret) {
171                 dev_err(&st->us->dev, "problem when reading 40 bit register 0x%02X",
172                                 reg_address);
173                 goto error_ret;
174         }
175         *val = ((u64)st->rx[1] << 32) | (st->rx[2] << 24) |
176                 (st->rx[3] << 16) | (st->rx[4] << 8) | st->rx[5];
177
178 error_ret:
179         mutex_unlock(&st->buf_lock);
180         return ret;
181 }
182
183 static ssize_t ade7759_read_8bit(struct device *dev,
184                 struct device_attribute *attr,
185                 char *buf)
186 {
187         int ret;
188         u8 val = 0;
189         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
190
191         ret = ade7759_spi_read_reg_8(dev, this_attr->address, &val);
192         if (ret)
193                 return ret;
194
195         return sprintf(buf, "%u\n", val);
196 }
197
198 static ssize_t ade7759_read_16bit(struct device *dev,
199                 struct device_attribute *attr,
200                 char *buf)
201 {
202         int ret;
203         u16 val = 0;
204         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
205
206         ret = ade7759_spi_read_reg_16(dev, this_attr->address, &val);
207         if (ret)
208                 return ret;
209
210         return sprintf(buf, "%u\n", val);
211 }
212
213 static ssize_t ade7759_read_40bit(struct device *dev,
214                 struct device_attribute *attr,
215                 char *buf)
216 {
217         int ret;
218         u64 val = 0;
219         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
220
221         ret = ade7759_spi_read_reg_40(dev, this_attr->address, &val);
222         if (ret)
223                 return ret;
224
225         return sprintf(buf, "%llu\n", val);
226 }
227
228 static ssize_t ade7759_write_8bit(struct device *dev,
229                 struct device_attribute *attr,
230                 const char *buf,
231                 size_t len)
232 {
233         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
234         int ret;
235         long val;
236
237         ret = strict_strtol(buf, 10, &val);
238         if (ret)
239                 goto error_ret;
240         ret = ade7759_spi_write_reg_8(dev, this_attr->address, val);
241
242 error_ret:
243         return ret ? ret : len;
244 }
245
246 static ssize_t ade7759_write_16bit(struct device *dev,
247                 struct device_attribute *attr,
248                 const char *buf,
249                 size_t len)
250 {
251         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
252         int ret;
253         long val;
254
255         ret = strict_strtol(buf, 10, &val);
256         if (ret)
257                 goto error_ret;
258         ret = ade7759_spi_write_reg_16(dev, this_attr->address, val);
259
260 error_ret:
261         return ret ? ret : len;
262 }
263
264 static int ade7759_reset(struct device *dev)
265 {
266         int ret;
267         u16 val;
268         ade7759_spi_read_reg_16(dev,
269                         ADE7759_MODE,
270                         &val);
271         val |= 1 << 6; /* Software Chip Reset */
272         ret = ade7759_spi_write_reg_16(dev,
273                         ADE7759_MODE,
274                         val);
275
276         return ret;
277 }
278
279 static ssize_t ade7759_write_reset(struct device *dev,
280                 struct device_attribute *attr,
281                 const char *buf, size_t len)
282 {
283         if (len < 1)
284                 return -1;
285         switch (buf[0]) {
286         case '1':
287         case 'y':
288         case 'Y':
289                 return ade7759_reset(dev);
290         }
291         return -1;
292 }
293
294 static IIO_DEV_ATTR_AENERGY(ade7759_read_40bit, ADE7759_AENERGY);
295 static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
296                 ade7759_read_16bit,
297                 ade7759_write_16bit,
298                 ADE7759_CFDEN);
299 static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
300                 ade7759_read_8bit,
301                 ade7759_write_8bit,
302                 ADE7759_CFNUM);
303 static IIO_DEV_ATTR_CHKSUM(ade7759_read_8bit, ADE7759_CHKSUM);
304 static IIO_DEV_ATTR_PHCAL(S_IWUSR | S_IRUGO,
305                 ade7759_read_16bit,
306                 ade7759_write_16bit,
307                 ADE7759_PHCAL);
308 static IIO_DEV_ATTR_APOS(S_IWUSR | S_IRUGO,
309                 ade7759_read_16bit,
310                 ade7759_write_16bit,
311                 ADE7759_APOS);
312 static IIO_DEV_ATTR_SAGCYC(S_IWUSR | S_IRUGO,
313                 ade7759_read_8bit,
314                 ade7759_write_8bit,
315                 ADE7759_SAGCYC);
316 static IIO_DEV_ATTR_SAGLVL(S_IWUSR | S_IRUGO,
317                 ade7759_read_8bit,
318                 ade7759_write_8bit,
319                 ADE7759_SAGLVL);
320 static IIO_DEV_ATTR_LINECYC(S_IWUSR | S_IRUGO,
321                 ade7759_read_8bit,
322                 ade7759_write_8bit,
323                 ADE7759_LINECYC);
324 static IIO_DEV_ATTR_LENERGY(ade7759_read_40bit, ADE7759_LENERGY);
325 static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR | S_IRUGO,
326                 ade7759_read_8bit,
327                 ade7759_write_8bit,
328                 ADE7759_GAIN);
329 static IIO_DEV_ATTR_ACTIVE_POWER_GAIN(S_IWUSR | S_IRUGO,
330                 ade7759_read_16bit,
331                 ade7759_write_16bit,
332                 ADE7759_APGAIN);
333 static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR | S_IRUGO,
334                 ade7759_read_8bit,
335                 ade7759_write_8bit,
336                 ADE7759_CH1OS);
337 static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR | S_IRUGO,
338                 ade7759_read_8bit,
339                 ade7759_write_8bit,
340                 ADE7759_CH2OS);
341
342 static int ade7759_set_irq(struct device *dev, bool enable)
343 {
344         int ret;
345         u8 irqen;
346         ret = ade7759_spi_read_reg_8(dev, ADE7759_IRQEN, &irqen);
347         if (ret)
348                 goto error_ret;
349
350         if (enable)
351                 irqen |= 1 << 3; /* Enables an interrupt when a data is
352                                     present in the waveform register */
353         else
354                 irqen &= ~(1 << 3);
355
356         ret = ade7759_spi_write_reg_8(dev, ADE7759_IRQEN, irqen);
357         if (ret)
358                 goto error_ret;
359
360 error_ret:
361         return ret;
362 }
363
364 /* Power down the device */
365 int ade7759_stop_device(struct device *dev)
366 {
367         int ret;
368         u16 val;
369         ade7759_spi_read_reg_16(dev,
370                         ADE7759_MODE,
371                         &val);
372         val |= 1 << 4;  /* AD converters can be turned off */
373         ret = ade7759_spi_write_reg_16(dev,
374                         ADE7759_MODE,
375                         val);
376
377         return ret;
378 }
379
380 static int ade7759_initial_setup(struct ade7759_state *st)
381 {
382         int ret;
383         struct device *dev = &st->indio_dev->dev;
384
385         /* use low spi speed for init */
386         st->us->mode = SPI_MODE_3;
387         spi_setup(st->us);
388
389         /* Disable IRQ */
390         ret = ade7759_set_irq(dev, false);
391         if (ret) {
392                 dev_err(dev, "disable irq failed");
393                 goto err_ret;
394         }
395
396         ade7759_reset(dev);
397         msleep(ADE7759_STARTUP_DELAY);
398
399 err_ret:
400         return ret;
401 }
402
403 static ssize_t ade7759_read_frequency(struct device *dev,
404                 struct device_attribute *attr,
405                 char *buf)
406 {
407         int ret, len = 0;
408         u16 t;
409         int sps;
410         ret = ade7759_spi_read_reg_16(dev,
411                         ADE7759_MODE,
412                         &t);
413         if (ret)
414                 return ret;
415
416         t = (t >> 3) & 0x3;
417         sps = 27900 / (1 + t);
418
419         len = sprintf(buf, "%d SPS\n", sps);
420         return len;
421 }
422
423 static ssize_t ade7759_write_frequency(struct device *dev,
424                 struct device_attribute *attr,
425                 const char *buf,
426                 size_t len)
427 {
428         struct iio_dev *indio_dev = dev_get_drvdata(dev);
429         struct ade7759_state *st = iio_dev_get_devdata(indio_dev);
430         unsigned long val;
431         int ret;
432         u16 reg, t;
433
434         ret = strict_strtol(buf, 10, &val);
435         if (ret)
436                 return ret;
437
438         mutex_lock(&indio_dev->mlock);
439
440         t = (27900 / val);
441         if (t > 0)
442                 t--;
443
444         if (t > 1)
445                 st->us->max_speed_hz = ADE7759_SPI_SLOW;
446         else
447                 st->us->max_speed_hz = ADE7759_SPI_FAST;
448
449         ret = ade7759_spi_read_reg_16(dev,
450                         ADE7759_MODE,
451                         &reg);
452         if (ret)
453                 goto out;
454
455         reg &= ~(3 << 13);
456         reg |= t << 13;
457
458         ret = ade7759_spi_write_reg_16(dev,
459                         ADE7759_MODE,
460                         reg);
461
462 out:
463         mutex_unlock(&indio_dev->mlock);
464
465         return ret ? ret : len;
466 }
467 static IIO_DEV_ATTR_TEMP_RAW(ade7759_read_8bit);
468 static IIO_CONST_ATTR(temp_offset, "70 C");
469 static IIO_CONST_ATTR(temp_scale, "1 C");
470
471 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
472                 ade7759_read_frequency,
473                 ade7759_write_frequency);
474
475 static IIO_DEV_ATTR_RESET(ade7759_write_reset);
476
477 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
478
479 static IIO_CONST_ATTR(name, "ade7759");
480
481 static struct attribute *ade7759_event_attributes[] = {
482         NULL
483 };
484
485 static struct attribute_group ade7759_event_attribute_group = {
486         .attrs = ade7759_event_attributes,
487 };
488
489 static struct attribute *ade7759_attributes[] = {
490         &iio_dev_attr_temp_raw.dev_attr.attr,
491         &iio_const_attr_temp_offset.dev_attr.attr,
492         &iio_const_attr_temp_scale.dev_attr.attr,
493         &iio_dev_attr_sampling_frequency.dev_attr.attr,
494         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
495         &iio_dev_attr_reset.dev_attr.attr,
496         &iio_const_attr_name.dev_attr.attr,
497         &iio_dev_attr_phcal.dev_attr.attr,
498         &iio_dev_attr_cfden.dev_attr.attr,
499         &iio_dev_attr_aenergy.dev_attr.attr,
500         &iio_dev_attr_cfnum.dev_attr.attr,
501         &iio_dev_attr_apos.dev_attr.attr,
502         &iio_dev_attr_sagcyc.dev_attr.attr,
503         &iio_dev_attr_saglvl.dev_attr.attr,
504         &iio_dev_attr_linecyc.dev_attr.attr,
505         &iio_dev_attr_lenergy.dev_attr.attr,
506         &iio_dev_attr_chksum.dev_attr.attr,
507         &iio_dev_attr_pga_gain.dev_attr.attr,
508         &iio_dev_attr_active_power_gain.dev_attr.attr,
509         &iio_dev_attr_choff_1.dev_attr.attr,
510         &iio_dev_attr_choff_2.dev_attr.attr,
511         NULL,
512 };
513
514 static const struct attribute_group ade7759_attribute_group = {
515         .attrs = ade7759_attributes,
516 };
517
518 static int __devinit ade7759_probe(struct spi_device *spi)
519 {
520         int ret, regdone = 0;
521         struct ade7759_state *st = kzalloc(sizeof *st, GFP_KERNEL);
522         if (!st) {
523                 ret =  -ENOMEM;
524                 goto error_ret;
525         }
526         /* this is only used for removal purposes */
527         spi_set_drvdata(spi, st);
528
529         /* Allocate the comms buffers */
530         st->rx = kzalloc(sizeof(*st->rx)*ADE7759_MAX_RX, GFP_KERNEL);
531         if (st->rx == NULL) {
532                 ret = -ENOMEM;
533                 goto error_free_st;
534         }
535         st->tx = kzalloc(sizeof(*st->tx)*ADE7759_MAX_TX, GFP_KERNEL);
536         if (st->tx == NULL) {
537                 ret = -ENOMEM;
538                 goto error_free_rx;
539         }
540         st->us = spi;
541         mutex_init(&st->buf_lock);
542         /* setup the industrialio driver allocated elements */
543         st->indio_dev = iio_allocate_device();
544         if (st->indio_dev == NULL) {
545                 ret = -ENOMEM;
546                 goto error_free_tx;
547         }
548
549         st->indio_dev->dev.parent = &spi->dev;
550         st->indio_dev->num_interrupt_lines = 1;
551         st->indio_dev->event_attrs = &ade7759_event_attribute_group;
552         st->indio_dev->attrs = &ade7759_attribute_group;
553         st->indio_dev->dev_data = (void *)(st);
554         st->indio_dev->driver_module = THIS_MODULE;
555         st->indio_dev->modes = INDIO_DIRECT_MODE;
556
557         ret = ade7759_configure_ring(st->indio_dev);
558         if (ret)
559                 goto error_free_dev;
560
561         ret = iio_device_register(st->indio_dev);
562         if (ret)
563                 goto error_unreg_ring_funcs;
564         regdone = 1;
565
566         ret = ade7759_initialize_ring(st->indio_dev->ring);
567         if (ret) {
568                 printk(KERN_ERR "failed to initialize the ring\n");
569                 goto error_unreg_ring_funcs;
570         }
571
572         if (spi->irq) {
573                 ret = iio_register_interrupt_line(spi->irq,
574                                 st->indio_dev,
575                                 0,
576                                 IRQF_TRIGGER_FALLING,
577                                 "ade7759");
578                 if (ret)
579                         goto error_uninitialize_ring;
580
581                 ret = ade7759_probe_trigger(st->indio_dev);
582                 if (ret)
583                         goto error_unregister_line;
584         }
585
586         /* Get the device into a sane initial state */
587         ret = ade7759_initial_setup(st);
588         if (ret)
589                 goto error_remove_trigger;
590         return 0;
591
592 error_remove_trigger:
593         if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
594                 ade7759_remove_trigger(st->indio_dev);
595 error_unregister_line:
596         if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
597                 iio_unregister_interrupt_line(st->indio_dev, 0);
598 error_uninitialize_ring:
599         ade7759_uninitialize_ring(st->indio_dev->ring);
600 error_unreg_ring_funcs:
601         ade7759_unconfigure_ring(st->indio_dev);
602 error_free_dev:
603         if (regdone)
604                 iio_device_unregister(st->indio_dev);
605         else
606                 iio_free_device(st->indio_dev);
607 error_free_tx:
608         kfree(st->tx);
609 error_free_rx:
610         kfree(st->rx);
611 error_free_st:
612         kfree(st);
613 error_ret:
614         return ret;
615 }
616
617 /* fixme, confirm ordering in this function */
618 static int ade7759_remove(struct spi_device *spi)
619 {
620         int ret;
621         struct ade7759_state *st = spi_get_drvdata(spi);
622         struct iio_dev *indio_dev = st->indio_dev;
623
624         ret = ade7759_stop_device(&(indio_dev->dev));
625         if (ret)
626                 goto err_ret;
627
628         flush_scheduled_work();
629
630         ade7759_remove_trigger(indio_dev);
631         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
632                 iio_unregister_interrupt_line(indio_dev, 0);
633
634         ade7759_uninitialize_ring(indio_dev->ring);
635         ade7759_unconfigure_ring(indio_dev);
636         iio_device_unregister(indio_dev);
637         kfree(st->tx);
638         kfree(st->rx);
639         kfree(st);
640
641         return 0;
642
643 err_ret:
644         return ret;
645 }
646
647 static struct spi_driver ade7759_driver = {
648         .driver = {
649                 .name = "ade7759",
650                 .owner = THIS_MODULE,
651         },
652         .probe = ade7759_probe,
653         .remove = __devexit_p(ade7759_remove),
654 };
655
656 static __init int ade7759_init(void)
657 {
658         return spi_register_driver(&ade7759_driver);
659 }
660 module_init(ade7759_init);
661
662 static __exit void ade7759_exit(void)
663 {
664         spi_unregister_driver(&ade7759_driver);
665 }
666 module_exit(ade7759_exit);
667
668 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
669 MODULE_DESCRIPTION("Analog Devices ADE7759 Active Energy Metering IC Driver");
670 MODULE_LICENSE("GPL v2");