]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/meter/ade7759.c
Merge tag 'blackfin-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/realm...
[karo-tx-linux.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/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
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
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
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
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_w8r16(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         *val = be16_to_cpup(val);
98
99         return 0;
100 }
101
102 static int ade7759_spi_read_reg_40(struct device *dev,
103                 u8 reg_address,
104                 u64 *val)
105 {
106         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
107         struct ade7759_state *st = iio_priv(indio_dev);
108         int ret;
109         struct spi_transfer xfers[] = {
110                 {
111                         .tx_buf = st->tx,
112                         .rx_buf = st->rx,
113                         .bits_per_word = 8,
114                         .len = 6,
115                 },
116         };
117
118         mutex_lock(&st->buf_lock);
119         st->tx[0] = ADE7759_READ_REG(reg_address);
120         memset(&st->tx[1], 0 , 5);
121
122         ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
123         if (ret) {
124                 dev_err(&st->us->dev, "problem when reading 40 bit register 0x%02X",
125                                 reg_address);
126                 goto error_ret;
127         }
128         *val = ((u64)st->rx[1] << 32) | (st->rx[2] << 24) |
129                 (st->rx[3] << 16) | (st->rx[4] << 8) | st->rx[5];
130
131 error_ret:
132         mutex_unlock(&st->buf_lock);
133         return ret;
134 }
135
136 static ssize_t ade7759_read_8bit(struct device *dev,
137                 struct device_attribute *attr,
138                 char *buf)
139 {
140         int ret;
141         u8 val = 0;
142         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
143
144         ret = ade7759_spi_read_reg_8(dev, this_attr->address, &val);
145         if (ret)
146                 return ret;
147
148         return sprintf(buf, "%u\n", val);
149 }
150
151 static ssize_t ade7759_read_16bit(struct device *dev,
152                 struct device_attribute *attr,
153                 char *buf)
154 {
155         int ret;
156         u16 val = 0;
157         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
158
159         ret = ade7759_spi_read_reg_16(dev, this_attr->address, &val);
160         if (ret)
161                 return ret;
162
163         return sprintf(buf, "%u\n", val);
164 }
165
166 static ssize_t ade7759_read_40bit(struct device *dev,
167                 struct device_attribute *attr,
168                 char *buf)
169 {
170         int ret;
171         u64 val = 0;
172         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
173
174         ret = ade7759_spi_read_reg_40(dev, this_attr->address, &val);
175         if (ret)
176                 return ret;
177
178         return sprintf(buf, "%llu\n", val);
179 }
180
181 static ssize_t ade7759_write_8bit(struct device *dev,
182                 struct device_attribute *attr,
183                 const char *buf,
184                 size_t len)
185 {
186         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
187         int ret;
188         long val;
189
190         ret = strict_strtol(buf, 10, &val);
191         if (ret)
192                 goto error_ret;
193         ret = ade7759_spi_write_reg_8(dev, this_attr->address, val);
194
195 error_ret:
196         return ret ? ret : len;
197 }
198
199 static ssize_t ade7759_write_16bit(struct device *dev,
200                 struct device_attribute *attr,
201                 const char *buf,
202                 size_t len)
203 {
204         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
205         int ret;
206         long val;
207
208         ret = strict_strtol(buf, 10, &val);
209         if (ret)
210                 goto error_ret;
211         ret = ade7759_spi_write_reg_16(dev, this_attr->address, val);
212
213 error_ret:
214         return ret ? ret : len;
215 }
216
217 static int ade7759_reset(struct device *dev)
218 {
219         int ret;
220         u16 val;
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
280 static int ade7759_set_irq(struct device *dev, bool enable)
281 {
282         int ret;
283         u8 irqen;
284         ret = ade7759_spi_read_reg_8(dev, ADE7759_IRQEN, &irqen);
285         if (ret)
286                 goto error_ret;
287
288         if (enable)
289                 irqen |= 1 << 3; /* Enables an interrupt when a data is
290                                     present in the waveform register */
291         else
292                 irqen &= ~(1 << 3);
293
294         ret = ade7759_spi_write_reg_8(dev, ADE7759_IRQEN, irqen);
295
296 error_ret:
297         return ret;
298 }
299
300 /* Power down the device */
301 static int ade7759_stop_device(struct device *dev)
302 {
303         u16 val;
304
305         ade7759_spi_read_reg_16(dev,
306                         ADE7759_MODE,
307                         &val);
308         val |= 1 << 4;  /* AD converters can be turned off */
309
310         return ade7759_spi_write_reg_16(dev, ADE7759_MODE, val);
311 }
312
313 static int ade7759_initial_setup(struct iio_dev *indio_dev)
314 {
315         int ret;
316         struct ade7759_state *st = iio_priv(indio_dev);
317         struct device *dev = &indio_dev->dev;
318
319         /* use low spi speed for init */
320         st->us->mode = SPI_MODE_3;
321         spi_setup(st->us);
322
323         /* Disable IRQ */
324         ret = ade7759_set_irq(dev, false);
325         if (ret) {
326                 dev_err(dev, "disable irq failed");
327                 goto err_ret;
328         }
329
330         ade7759_reset(dev);
331         msleep(ADE7759_STARTUP_DELAY);
332
333 err_ret:
334         return ret;
335 }
336
337 static ssize_t ade7759_read_frequency(struct device *dev,
338                 struct device_attribute *attr,
339                 char *buf)
340 {
341         int ret;
342         u16 t;
343         int sps;
344         ret = ade7759_spi_read_reg_16(dev,
345                         ADE7759_MODE,
346                         &t);
347         if (ret)
348                 return ret;
349
350         t = (t >> 3) & 0x3;
351         sps = 27900 / (1 + t);
352
353         return sprintf(buf, "%d\n", sps);
354 }
355
356 static ssize_t ade7759_write_frequency(struct device *dev,
357                 struct device_attribute *attr,
358                 const char *buf,
359                 size_t len)
360 {
361         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
362         struct ade7759_state *st = iio_priv(indio_dev);
363         unsigned long val;
364         int ret;
365         u16 reg, t;
366
367         ret = strict_strtol(buf, 10, &val);
368         if (ret)
369                 return ret;
370         if (val == 0)
371                 return -EINVAL;
372
373         mutex_lock(&indio_dev->mlock);
374
375         t = (27900 / val);
376         if (t > 0)
377                 t--;
378
379         if (t > 1)
380                 st->us->max_speed_hz = ADE7759_SPI_SLOW;
381         else
382                 st->us->max_speed_hz = ADE7759_SPI_FAST;
383
384         ret = ade7759_spi_read_reg_16(dev, ADE7759_MODE, &reg);
385         if (ret)
386                 goto out;
387
388         reg &= ~(3 << 13);
389         reg |= t << 13;
390
391         ret = ade7759_spi_write_reg_16(dev, ADE7759_MODE, reg);
392
393 out:
394         mutex_unlock(&indio_dev->mlock);
395
396         return ret ? ret : len;
397 }
398 static IIO_DEV_ATTR_TEMP_RAW(ade7759_read_8bit);
399 static IIO_CONST_ATTR(in_temp_offset, "70 C");
400 static IIO_CONST_ATTR(in_temp_scale, "1 C");
401
402 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
403                 ade7759_read_frequency,
404                 ade7759_write_frequency);
405
406 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
407
408 static struct attribute *ade7759_attributes[] = {
409         &iio_dev_attr_in_temp_raw.dev_attr.attr,
410         &iio_const_attr_in_temp_offset.dev_attr.attr,
411         &iio_const_attr_in_temp_scale.dev_attr.attr,
412         &iio_dev_attr_sampling_frequency.dev_attr.attr,
413         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
414         &iio_dev_attr_phcal.dev_attr.attr,
415         &iio_dev_attr_cfden.dev_attr.attr,
416         &iio_dev_attr_aenergy.dev_attr.attr,
417         &iio_dev_attr_cfnum.dev_attr.attr,
418         &iio_dev_attr_apos.dev_attr.attr,
419         &iio_dev_attr_sagcyc.dev_attr.attr,
420         &iio_dev_attr_saglvl.dev_attr.attr,
421         &iio_dev_attr_linecyc.dev_attr.attr,
422         &iio_dev_attr_lenergy.dev_attr.attr,
423         &iio_dev_attr_chksum.dev_attr.attr,
424         &iio_dev_attr_pga_gain.dev_attr.attr,
425         &iio_dev_attr_active_power_gain.dev_attr.attr,
426         &iio_dev_attr_choff_1.dev_attr.attr,
427         &iio_dev_attr_choff_2.dev_attr.attr,
428         NULL,
429 };
430
431 static const struct attribute_group ade7759_attribute_group = {
432         .attrs = ade7759_attributes,
433 };
434
435 static const struct iio_info ade7759_info = {
436         .attrs = &ade7759_attribute_group,
437         .driver_module = THIS_MODULE,
438 };
439
440 static int ade7759_probe(struct spi_device *spi)
441 {
442         int ret;
443         struct ade7759_state *st;
444         struct iio_dev *indio_dev;
445
446         /* setup the industrialio driver allocated elements */
447         indio_dev = iio_device_alloc(sizeof(*st));
448         if (indio_dev == NULL) {
449                 ret = -ENOMEM;
450                 goto error_ret;
451         }
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                 goto error_free_dev;
467
468         ret = iio_device_register(indio_dev);
469         if (ret)
470                 goto error_free_dev;
471
472         return 0;
473
474 error_free_dev:
475         iio_device_free(indio_dev);
476 error_ret:
477         return ret;
478 }
479
480 /* fixme, confirm ordering in this function */
481 static int ade7759_remove(struct spi_device *spi)
482 {
483         struct iio_dev *indio_dev = spi_get_drvdata(spi);
484
485         iio_device_unregister(indio_dev);
486         ade7759_stop_device(&indio_dev->dev);
487         iio_device_free(indio_dev);
488
489         return 0;
490 }
491
492 static struct spi_driver ade7759_driver = {
493         .driver = {
494                 .name = "ade7759",
495                 .owner = THIS_MODULE,
496         },
497         .probe = ade7759_probe,
498         .remove = ade7759_remove,
499 };
500 module_spi_driver(ade7759_driver);
501
502 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
503 MODULE_DESCRIPTION("Analog Devices ADE7759 Active Energy Metering IC Driver");
504 MODULE_LICENSE("GPL v2");
505 MODULE_ALIAS("spi:ad7759");