Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / staging / iio / meter / ade7753.c
1 /*
2  * ADE7753 Single-Phase Multifunction Metering IC with di/dt Sensor Interface
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 "ade7753.h"
25
26 static int ade7753_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 ade7753_state *st = iio_dev_get_devdata(indio_dev);
33
34         mutex_lock(&st->buf_lock);
35         st->tx[0] = ADE7753_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 ade7753_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_get_drvdata(dev);
50         struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
51
52         mutex_lock(&st->buf_lock);
53         st->tx[0] = ADE7753_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
62 static int ade7753_spi_read_reg_8(struct device *dev,
63                 u8 reg_address,
64                 u8 *val)
65 {
66         struct iio_dev *indio_dev = dev_get_drvdata(dev);
67         struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
68         ssize_t ret;
69
70         ret = spi_w8r8(st->us, ADE7753_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
81 static int ade7753_spi_read_reg_16(struct device *dev,
82                 u8 reg_address,
83                 u16 *val)
84 {
85         struct iio_dev *indio_dev = dev_get_drvdata(dev);
86         struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
87         ssize_t ret;
88
89         ret = spi_w8r16(st->us, ADE7753_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         *val = be16_to_cpup(val);
98
99         return 0;
100 }
101
102 static int ade7753_spi_read_reg_24(struct device *dev,
103                 u8 reg_address,
104                 u32 *val)
105 {
106         struct spi_message msg;
107         struct iio_dev *indio_dev = dev_get_drvdata(dev);
108         struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
109         int ret;
110         struct spi_transfer xfers[] = {
111                 {
112                         .tx_buf = st->tx,
113                         .bits_per_word = 8,
114                         .len = 1,
115                 }, {
116                         .rx_buf = st->tx,
117                         .bits_per_word = 8,
118                         .len = 3,
119                 }
120         };
121
122         mutex_lock(&st->buf_lock);
123         st->tx[0] = ADE7753_READ_REG(reg_address);
124
125         spi_message_init(&msg);
126         spi_message_add_tail(&xfers[0], &msg);
127         spi_message_add_tail(&xfers[1], &msg);
128         ret = spi_sync(st->us, &msg);
129         if (ret) {
130                 dev_err(&st->us->dev, "problem when reading 24 bit register 0x%02X",
131                                 reg_address);
132                 goto error_ret;
133         }
134         *val = (st->rx[0] << 16) | (st->rx[1] << 8) | st->rx[2];
135
136 error_ret:
137         mutex_unlock(&st->buf_lock);
138         return ret;
139 }
140
141 static ssize_t ade7753_read_8bit(struct device *dev,
142                 struct device_attribute *attr,
143                 char *buf)
144 {
145         int ret;
146         u8 val;
147         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
148
149         ret = ade7753_spi_read_reg_8(dev, this_attr->address, &val);
150         if (ret)
151                 return ret;
152
153         return sprintf(buf, "%u\n", val);
154 }
155
156 static ssize_t ade7753_read_16bit(struct device *dev,
157                 struct device_attribute *attr,
158                 char *buf)
159 {
160         int ret;
161         u16 val;
162         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
163
164         ret = ade7753_spi_read_reg_16(dev, this_attr->address, &val);
165         if (ret)
166                 return ret;
167
168         return sprintf(buf, "%u\n", val);
169 }
170
171 static ssize_t ade7753_read_24bit(struct device *dev,
172                 struct device_attribute *attr,
173                 char *buf)
174 {
175         int ret;
176         u32 val;
177         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
178
179         ret = ade7753_spi_read_reg_24(dev, this_attr->address, &val);
180         if (ret)
181                 return ret;
182
183         return sprintf(buf, "%u\n", val);
184 }
185
186 static ssize_t ade7753_write_8bit(struct device *dev,
187                 struct device_attribute *attr,
188                 const char *buf,
189                 size_t len)
190 {
191         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
192         int ret;
193         long val;
194
195         ret = strict_strtol(buf, 10, &val);
196         if (ret)
197                 goto error_ret;
198         ret = ade7753_spi_write_reg_8(dev, this_attr->address, val);
199
200 error_ret:
201         return ret ? ret : len;
202 }
203
204 static ssize_t ade7753_write_16bit(struct device *dev,
205                 struct device_attribute *attr,
206                 const char *buf,
207                 size_t len)
208 {
209         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
210         int ret;
211         long val;
212
213         ret = strict_strtol(buf, 10, &val);
214         if (ret)
215                 goto error_ret;
216         ret = ade7753_spi_write_reg_16(dev, this_attr->address, val);
217
218 error_ret:
219         return ret ? ret : len;
220 }
221
222 static int ade7753_reset(struct device *dev)
223 {
224         u16 val;
225
226         ade7753_spi_read_reg_16(dev, ADE7753_MODE, &val);
227         val |= 1 << 6; /* Software Chip Reset */
228
229         return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
230 }
231
232 static ssize_t ade7753_write_reset(struct device *dev,
233                 struct device_attribute *attr,
234                 const char *buf, size_t len)
235 {
236         if (len < 1)
237                 return -1;
238         switch (buf[0]) {
239         case '1':
240         case 'y':
241         case 'Y':
242                 return ade7753_reset(dev);
243         }
244         return -1;
245 }
246
247 static IIO_DEV_ATTR_AENERGY(ade7753_read_24bit, ADE7753_AENERGY);
248 static IIO_DEV_ATTR_LAENERGY(ade7753_read_24bit, ADE7753_LAENERGY);
249 static IIO_DEV_ATTR_VAENERGY(ade7753_read_24bit, ADE7753_VAENERGY);
250 static IIO_DEV_ATTR_LVAENERGY(ade7753_read_24bit, ADE7753_LVAENERGY);
251 static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
252                 ade7753_read_16bit,
253                 ade7753_write_16bit,
254                 ADE7753_CFDEN);
255 static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
256                 ade7753_read_8bit,
257                 ade7753_write_8bit,
258                 ADE7753_CFNUM);
259 static IIO_DEV_ATTR_CHKSUM(ade7753_read_8bit, ADE7753_CHKSUM);
260 static IIO_DEV_ATTR_PHCAL(S_IWUSR | S_IRUGO,
261                 ade7753_read_16bit,
262                 ade7753_write_16bit,
263                 ADE7753_PHCAL);
264 static IIO_DEV_ATTR_APOS(S_IWUSR | S_IRUGO,
265                 ade7753_read_16bit,
266                 ade7753_write_16bit,
267                 ADE7753_APOS);
268 static IIO_DEV_ATTR_SAGCYC(S_IWUSR | S_IRUGO,
269                 ade7753_read_8bit,
270                 ade7753_write_8bit,
271                 ADE7753_SAGCYC);
272 static IIO_DEV_ATTR_SAGLVL(S_IWUSR | S_IRUGO,
273                 ade7753_read_8bit,
274                 ade7753_write_8bit,
275                 ADE7753_SAGLVL);
276 static IIO_DEV_ATTR_LINECYC(S_IWUSR | S_IRUGO,
277                 ade7753_read_8bit,
278                 ade7753_write_8bit,
279                 ADE7753_LINECYC);
280 static IIO_DEV_ATTR_WDIV(S_IWUSR | S_IRUGO,
281                 ade7753_read_8bit,
282                 ade7753_write_8bit,
283                 ADE7753_WDIV);
284 static IIO_DEV_ATTR_IRMS(S_IWUSR | S_IRUGO,
285                 ade7753_read_24bit,
286                 NULL,
287                 ADE7753_IRMS);
288 static IIO_DEV_ATTR_VRMS(S_IRUGO,
289                 ade7753_read_24bit,
290                 NULL,
291                 ADE7753_VRMS);
292 static IIO_DEV_ATTR_IRMSOS(S_IWUSR | S_IRUGO,
293                 ade7753_read_16bit,
294                 ade7753_write_16bit,
295                 ADE7753_IRMSOS);
296 static IIO_DEV_ATTR_VRMSOS(S_IWUSR | S_IRUGO,
297                 ade7753_read_16bit,
298                 ade7753_write_16bit,
299                 ADE7753_VRMSOS);
300 static IIO_DEV_ATTR_WGAIN(S_IWUSR | S_IRUGO,
301                 ade7753_read_16bit,
302                 ade7753_write_16bit,
303                 ADE7753_WGAIN);
304 static IIO_DEV_ATTR_VAGAIN(S_IWUSR | S_IRUGO,
305                 ade7753_read_16bit,
306                 ade7753_write_16bit,
307                 ADE7753_VAGAIN);
308 static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR | S_IRUGO,
309                 ade7753_read_16bit,
310                 ade7753_write_16bit,
311                 ADE7753_GAIN);
312 static IIO_DEV_ATTR_IPKLVL(S_IWUSR | S_IRUGO,
313                 ade7753_read_8bit,
314                 ade7753_write_8bit,
315                 ADE7753_IPKLVL);
316 static IIO_DEV_ATTR_VPKLVL(S_IWUSR | S_IRUGO,
317                 ade7753_read_8bit,
318                 ade7753_write_8bit,
319                 ADE7753_VPKLVL);
320 static IIO_DEV_ATTR_IPEAK(S_IRUGO,
321                 ade7753_read_24bit,
322                 NULL,
323                 ADE7753_IPEAK);
324 static IIO_DEV_ATTR_VPEAK(S_IRUGO,
325                 ade7753_read_24bit,
326                 NULL,
327                 ADE7753_VPEAK);
328 static IIO_DEV_ATTR_VPERIOD(S_IRUGO,
329                 ade7753_read_16bit,
330                 NULL,
331                 ADE7753_PERIOD);
332 static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR | S_IRUGO,
333                 ade7753_read_8bit,
334                 ade7753_write_8bit,
335                 ADE7753_CH1OS);
336 static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR | S_IRUGO,
337                 ade7753_read_8bit,
338                 ade7753_write_8bit,
339                 ADE7753_CH2OS);
340
341 static int ade7753_set_irq(struct device *dev, bool enable)
342 {
343         int ret;
344         u8 irqen;
345         ret = ade7753_spi_read_reg_8(dev, ADE7753_IRQEN, &irqen);
346         if (ret)
347                 goto error_ret;
348
349         if (enable)
350                 irqen |= 1 << 3; /* Enables an interrupt when a data is
351                                     present in the waveform register */
352         else
353                 irqen &= ~(1 << 3);
354
355         ret = ade7753_spi_write_reg_8(dev, ADE7753_IRQEN, irqen);
356
357 error_ret:
358         return ret;
359 }
360
361 /* Power down the device */
362 static int ade7753_stop_device(struct device *dev)
363 {
364         u16 val;
365
366         ade7753_spi_read_reg_16(dev, ADE7753_MODE, &val);
367         val |= 1 << 4;  /* AD converters can be turned off */
368
369         return ade7753_spi_write_reg_16(dev, ADE7753_MODE, val);
370 }
371
372 static int ade7753_initial_setup(struct ade7753_state *st)
373 {
374         int ret;
375         struct device *dev = &st->indio_dev->dev;
376
377         /* use low spi speed for init */
378         st->us->mode = SPI_MODE_3;
379         spi_setup(st->us);
380
381         /* Disable IRQ */
382         ret = ade7753_set_irq(dev, false);
383         if (ret) {
384                 dev_err(dev, "disable irq failed");
385                 goto err_ret;
386         }
387
388         ade7753_reset(dev);
389         msleep(ADE7753_STARTUP_DELAY);
390
391 err_ret:
392         return ret;
393 }
394
395 static ssize_t ade7753_read_frequency(struct device *dev,
396                 struct device_attribute *attr,
397                 char *buf)
398 {
399         int ret, len = 0;
400         u8 t;
401         int sps;
402         ret = ade7753_spi_read_reg_8(dev, ADE7753_MODE, &t);
403         if (ret)
404                 return ret;
405
406         t = (t >> 11) & 0x3;
407         sps = 27900 / (1 + t);
408
409         len = sprintf(buf, "%d\n", sps);
410         return len;
411 }
412
413 static ssize_t ade7753_write_frequency(struct device *dev,
414                 struct device_attribute *attr,
415                 const char *buf,
416                 size_t len)
417 {
418         struct iio_dev *indio_dev = dev_get_drvdata(dev);
419         struct ade7753_state *st = iio_dev_get_devdata(indio_dev);
420         unsigned long val;
421         int ret;
422         u16 reg, t;
423
424         ret = strict_strtol(buf, 10, &val);
425         if (ret)
426                 return ret;
427
428         mutex_lock(&indio_dev->mlock);
429
430         t = (27900 / val);
431         if (t > 0)
432                 t--;
433
434         if (t > 1)
435                 st->us->max_speed_hz = ADE7753_SPI_SLOW;
436         else
437                 st->us->max_speed_hz = ADE7753_SPI_FAST;
438
439         ret = ade7753_spi_read_reg_16(dev, ADE7753_MODE, &reg);
440         if (ret)
441                 goto out;
442
443         reg &= ~(3 << 11);
444         reg |= t << 11;
445
446         ret = ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg);
447
448 out:
449         mutex_unlock(&indio_dev->mlock);
450
451         return ret ? ret : len;
452 }
453
454 static IIO_DEV_ATTR_TEMP_RAW(ade7753_read_8bit);
455 static IIO_CONST_ATTR(temp_offset, "-25 C");
456 static IIO_CONST_ATTR(temp_scale, "0.67 C");
457
458 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
459                 ade7753_read_frequency,
460                 ade7753_write_frequency);
461
462 static IIO_DEV_ATTR_RESET(ade7753_write_reset);
463
464 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
465
466 static struct attribute *ade7753_attributes[] = {
467         &iio_dev_attr_temp_raw.dev_attr.attr,
468         &iio_const_attr_temp_offset.dev_attr.attr,
469         &iio_const_attr_temp_scale.dev_attr.attr,
470         &iio_dev_attr_sampling_frequency.dev_attr.attr,
471         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
472         &iio_dev_attr_reset.dev_attr.attr,
473         &iio_dev_attr_phcal.dev_attr.attr,
474         &iio_dev_attr_cfden.dev_attr.attr,
475         &iio_dev_attr_aenergy.dev_attr.attr,
476         &iio_dev_attr_laenergy.dev_attr.attr,
477         &iio_dev_attr_vaenergy.dev_attr.attr,
478         &iio_dev_attr_lvaenergy.dev_attr.attr,
479         &iio_dev_attr_cfnum.dev_attr.attr,
480         &iio_dev_attr_apos.dev_attr.attr,
481         &iio_dev_attr_sagcyc.dev_attr.attr,
482         &iio_dev_attr_saglvl.dev_attr.attr,
483         &iio_dev_attr_linecyc.dev_attr.attr,
484         &iio_dev_attr_chksum.dev_attr.attr,
485         &iio_dev_attr_pga_gain.dev_attr.attr,
486         &iio_dev_attr_wgain.dev_attr.attr,
487         &iio_dev_attr_choff_1.dev_attr.attr,
488         &iio_dev_attr_choff_2.dev_attr.attr,
489         &iio_dev_attr_wdiv.dev_attr.attr,
490         &iio_dev_attr_irms.dev_attr.attr,
491         &iio_dev_attr_vrms.dev_attr.attr,
492         &iio_dev_attr_irmsos.dev_attr.attr,
493         &iio_dev_attr_vrmsos.dev_attr.attr,
494         &iio_dev_attr_vagain.dev_attr.attr,
495         &iio_dev_attr_ipklvl.dev_attr.attr,
496         &iio_dev_attr_vpklvl.dev_attr.attr,
497         &iio_dev_attr_ipeak.dev_attr.attr,
498         &iio_dev_attr_vpeak.dev_attr.attr,
499         &iio_dev_attr_vperiod.dev_attr.attr,
500         NULL,
501 };
502
503 static const struct attribute_group ade7753_attribute_group = {
504         .attrs = ade7753_attributes,
505 };
506
507 static const struct iio_info ade7753_info = {
508         .attrs = &ade7753_attribute_group,
509         .driver_module = THIS_MODULE,
510 };
511
512 static int __devinit ade7753_probe(struct spi_device *spi)
513 {
514         int ret, regdone = 0;
515         struct ade7753_state *st = kzalloc(sizeof *st, GFP_KERNEL);
516         if (!st) {
517                 ret =  -ENOMEM;
518                 goto error_ret;
519         }
520         /* this is only used for removal purposes */
521         spi_set_drvdata(spi, st);
522
523         /* Allocate the comms buffers */
524         st->rx = kzalloc(sizeof(*st->rx)*ADE7753_MAX_RX, GFP_KERNEL);
525         if (st->rx == NULL) {
526                 ret = -ENOMEM;
527                 goto error_free_st;
528         }
529         st->tx = kzalloc(sizeof(*st->tx)*ADE7753_MAX_TX, GFP_KERNEL);
530         if (st->tx == NULL) {
531                 ret = -ENOMEM;
532                 goto error_free_rx;
533         }
534         st->us = spi;
535         mutex_init(&st->buf_lock);
536         /* setup the industrialio driver allocated elements */
537         st->indio_dev = iio_allocate_device(0);
538         if (st->indio_dev == NULL) {
539                 ret = -ENOMEM;
540                 goto error_free_tx;
541         }
542
543         st->indio_dev->name = spi->dev.driver->name;
544         st->indio_dev->dev.parent = &spi->dev;
545         st->indio_dev->info = &ade7753_info;
546         st->indio_dev->dev_data = (void *)(st);
547         st->indio_dev->modes = INDIO_DIRECT_MODE;
548
549         ret = iio_device_register(st->indio_dev);
550         if (ret)
551                 goto error_free_dev;
552         regdone = 1;
553
554         /* Get the device into a sane initial state */
555         ret = ade7753_initial_setup(st);
556         if (ret)
557                 goto error_free_dev;
558         return 0;
559
560 error_free_dev:
561         if (regdone)
562                 iio_device_unregister(st->indio_dev);
563         else
564                 iio_free_device(st->indio_dev);
565 error_free_tx:
566         kfree(st->tx);
567 error_free_rx:
568         kfree(st->rx);
569 error_free_st:
570         kfree(st);
571 error_ret:
572         return ret;
573 }
574
575 /* fixme, confirm ordering in this function */
576 static int ade7753_remove(struct spi_device *spi)
577 {
578         int ret;
579         struct ade7753_state *st = spi_get_drvdata(spi);
580         struct iio_dev *indio_dev = st->indio_dev;
581
582         ret = ade7753_stop_device(&(indio_dev->dev));
583         if (ret)
584                 goto err_ret;
585
586         iio_device_unregister(indio_dev);
587         kfree(st->tx);
588         kfree(st->rx);
589         kfree(st);
590
591         return 0;
592
593 err_ret:
594         return ret;
595 }
596
597 static struct spi_driver ade7753_driver = {
598         .driver = {
599                 .name = "ade7753",
600                 .owner = THIS_MODULE,
601         },
602         .probe = ade7753_probe,
603         .remove = __devexit_p(ade7753_remove),
604 };
605
606 static __init int ade7753_init(void)
607 {
608         return spi_register_driver(&ade7753_driver);
609 }
610 module_init(ade7753_init);
611
612 static __exit void ade7753_exit(void)
613 {
614         spi_unregister_driver(&ade7753_driver);
615 }
616 module_exit(ade7753_exit);
617
618 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
619 MODULE_DESCRIPTION("Analog Devices ADE7753/6 Single-Phase Multifunction Meter");
620 MODULE_LICENSE("GPL v2");