iio: pressure: add support for MS5611 pressure and temperature sensor
[cascardo/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_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
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
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
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
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
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
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
216 static int ade7759_reset(struct device *dev)
217 {
218         int ret;
219         u16 val;
220
221         ret = ade7759_spi_read_reg_16(dev,
222                         ADE7759_MODE,
223                         &val);
224         if (ret < 0)
225                 return ret;
226
227         val |= 1 << 6; /* Software Chip Reset */
228         ret = ade7759_spi_write_reg_16(dev,
229                         ADE7759_MODE,
230                         val);
231
232         return ret;
233 }
234
235 static IIO_DEV_ATTR_AENERGY(ade7759_read_40bit, ADE7759_AENERGY);
236 static IIO_DEV_ATTR_CFDEN(S_IWUSR | S_IRUGO,
237                 ade7759_read_16bit,
238                 ade7759_write_16bit,
239                 ADE7759_CFDEN);
240 static IIO_DEV_ATTR_CFNUM(S_IWUSR | S_IRUGO,
241                 ade7759_read_8bit,
242                 ade7759_write_8bit,
243                 ADE7759_CFNUM);
244 static IIO_DEV_ATTR_CHKSUM(ade7759_read_8bit, ADE7759_CHKSUM);
245 static IIO_DEV_ATTR_PHCAL(S_IWUSR | S_IRUGO,
246                 ade7759_read_16bit,
247                 ade7759_write_16bit,
248                 ADE7759_PHCAL);
249 static IIO_DEV_ATTR_APOS(S_IWUSR | S_IRUGO,
250                 ade7759_read_16bit,
251                 ade7759_write_16bit,
252                 ADE7759_APOS);
253 static IIO_DEV_ATTR_SAGCYC(S_IWUSR | S_IRUGO,
254                 ade7759_read_8bit,
255                 ade7759_write_8bit,
256                 ADE7759_SAGCYC);
257 static IIO_DEV_ATTR_SAGLVL(S_IWUSR | S_IRUGO,
258                 ade7759_read_8bit,
259                 ade7759_write_8bit,
260                 ADE7759_SAGLVL);
261 static IIO_DEV_ATTR_LINECYC(S_IWUSR | S_IRUGO,
262                 ade7759_read_8bit,
263                 ade7759_write_8bit,
264                 ADE7759_LINECYC);
265 static IIO_DEV_ATTR_LENERGY(ade7759_read_40bit, ADE7759_LENERGY);
266 static IIO_DEV_ATTR_PGA_GAIN(S_IWUSR | S_IRUGO,
267                 ade7759_read_8bit,
268                 ade7759_write_8bit,
269                 ADE7759_GAIN);
270 static IIO_DEV_ATTR_ACTIVE_POWER_GAIN(S_IWUSR | S_IRUGO,
271                 ade7759_read_16bit,
272                 ade7759_write_16bit,
273                 ADE7759_APGAIN);
274 static IIO_DEV_ATTR_CH_OFF(1, S_IWUSR | S_IRUGO,
275                 ade7759_read_8bit,
276                 ade7759_write_8bit,
277                 ADE7759_CH1OS);
278 static IIO_DEV_ATTR_CH_OFF(2, S_IWUSR | S_IRUGO,
279                 ade7759_read_8bit,
280                 ade7759_write_8bit,
281                 ADE7759_CH2OS);
282
283 static int ade7759_set_irq(struct device *dev, bool enable)
284 {
285         int ret;
286         u8 irqen;
287
288         ret = ade7759_spi_read_reg_8(dev, ADE7759_IRQEN, &irqen);
289         if (ret)
290                 goto error_ret;
291
292         if (enable)
293                 irqen |= 1 << 3; /* Enables an interrupt when a data is
294                                     present in the waveform register */
295         else
296                 irqen &= ~(1 << 3);
297
298         ret = ade7759_spi_write_reg_8(dev, ADE7759_IRQEN, irqen);
299
300 error_ret:
301         return ret;
302 }
303
304 /* Power down the device */
305 static int ade7759_stop_device(struct device *dev)
306 {
307         int ret;
308         u16 val;
309
310         ret = ade7759_spi_read_reg_16(dev,
311                         ADE7759_MODE,
312                         &val);
313         if (ret < 0) {
314                 dev_err(dev, "unable to power down the device, error: %d\n",
315                         ret);
316                 return ret;
317         }
318
319         val |= 1 << 4;  /* AD converters can be turned off */
320
321         return ade7759_spi_write_reg_16(dev, ADE7759_MODE, val);
322 }
323
324 static int ade7759_initial_setup(struct iio_dev *indio_dev)
325 {
326         int ret;
327         struct ade7759_state *st = iio_priv(indio_dev);
328         struct device *dev = &indio_dev->dev;
329
330         /* use low spi speed for init */
331         st->us->mode = SPI_MODE_3;
332         spi_setup(st->us);
333
334         /* Disable IRQ */
335         ret = ade7759_set_irq(dev, false);
336         if (ret) {
337                 dev_err(dev, "disable irq failed");
338                 goto err_ret;
339         }
340
341         ade7759_reset(dev);
342         msleep(ADE7759_STARTUP_DELAY);
343
344 err_ret:
345         return ret;
346 }
347
348 static ssize_t ade7759_read_frequency(struct device *dev,
349                 struct device_attribute *attr,
350                 char *buf)
351 {
352         int ret;
353         u16 t;
354         int sps;
355
356         ret = ade7759_spi_read_reg_16(dev,
357                         ADE7759_MODE,
358                         &t);
359         if (ret)
360                 return ret;
361
362         t = (t >> 3) & 0x3;
363         sps = 27900 / (1 + t);
364
365         return sprintf(buf, "%d\n", sps);
366 }
367
368 static ssize_t ade7759_write_frequency(struct device *dev,
369                 struct device_attribute *attr,
370                 const char *buf,
371                 size_t len)
372 {
373         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
374         struct ade7759_state *st = iio_priv(indio_dev);
375         u16 val;
376         int ret;
377         u16 reg, t;
378
379         ret = kstrtou16(buf, 10, &val);
380         if (ret)
381                 return ret;
382         if (val == 0)
383                 return -EINVAL;
384
385         mutex_lock(&indio_dev->mlock);
386
387         t = (27900 / val);
388         if (t > 0)
389                 t--;
390
391         if (t > 1)
392                 st->us->max_speed_hz = ADE7759_SPI_SLOW;
393         else
394                 st->us->max_speed_hz = ADE7759_SPI_FAST;
395
396         ret = ade7759_spi_read_reg_16(dev, ADE7759_MODE, &reg);
397         if (ret)
398                 goto out;
399
400         reg &= ~(3 << 13);
401         reg |= t << 13;
402
403         ret = ade7759_spi_write_reg_16(dev, ADE7759_MODE, reg);
404
405 out:
406         mutex_unlock(&indio_dev->mlock);
407
408         return ret ? ret : len;
409 }
410 static IIO_DEV_ATTR_TEMP_RAW(ade7759_read_8bit);
411 static IIO_CONST_ATTR(in_temp_offset, "70 C");
412 static IIO_CONST_ATTR(in_temp_scale, "1 C");
413
414 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
415                 ade7759_read_frequency,
416                 ade7759_write_frequency);
417
418 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("27900 14000 7000 3500");
419
420 static struct attribute *ade7759_attributes[] = {
421         &iio_dev_attr_in_temp_raw.dev_attr.attr,
422         &iio_const_attr_in_temp_offset.dev_attr.attr,
423         &iio_const_attr_in_temp_scale.dev_attr.attr,
424         &iio_dev_attr_sampling_frequency.dev_attr.attr,
425         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
426         &iio_dev_attr_phcal.dev_attr.attr,
427         &iio_dev_attr_cfden.dev_attr.attr,
428         &iio_dev_attr_aenergy.dev_attr.attr,
429         &iio_dev_attr_cfnum.dev_attr.attr,
430         &iio_dev_attr_apos.dev_attr.attr,
431         &iio_dev_attr_sagcyc.dev_attr.attr,
432         &iio_dev_attr_saglvl.dev_attr.attr,
433         &iio_dev_attr_linecyc.dev_attr.attr,
434         &iio_dev_attr_lenergy.dev_attr.attr,
435         &iio_dev_attr_chksum.dev_attr.attr,
436         &iio_dev_attr_pga_gain.dev_attr.attr,
437         &iio_dev_attr_active_power_gain.dev_attr.attr,
438         &iio_dev_attr_choff_1.dev_attr.attr,
439         &iio_dev_attr_choff_2.dev_attr.attr,
440         NULL,
441 };
442
443 static const struct attribute_group ade7759_attribute_group = {
444         .attrs = ade7759_attributes,
445 };
446
447 static const struct iio_info ade7759_info = {
448         .attrs = &ade7759_attribute_group,
449         .driver_module = THIS_MODULE,
450 };
451
452 static int ade7759_probe(struct spi_device *spi)
453 {
454         int ret;
455         struct ade7759_state *st;
456         struct iio_dev *indio_dev;
457
458         /* setup the industrialio driver allocated elements */
459         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
460         if (!indio_dev)
461                 return -ENOMEM;
462         /* this is only used for removal purposes */
463         spi_set_drvdata(spi, indio_dev);
464
465         st = iio_priv(indio_dev);
466         st->us = spi;
467         mutex_init(&st->buf_lock);
468         indio_dev->name = spi->dev.driver->name;
469         indio_dev->dev.parent = &spi->dev;
470         indio_dev->info = &ade7759_info;
471         indio_dev->modes = INDIO_DIRECT_MODE;
472
473         /* Get the device into a sane initial state */
474         ret = ade7759_initial_setup(indio_dev);
475         if (ret)
476                 return ret;
477
478         ret = iio_device_register(indio_dev);
479         if (ret)
480                 return ret;
481
482         return 0;
483 }
484
485 /* fixme, confirm ordering in this function */
486 static int ade7759_remove(struct spi_device *spi)
487 {
488         struct iio_dev *indio_dev = spi_get_drvdata(spi);
489
490         iio_device_unregister(indio_dev);
491         ade7759_stop_device(&indio_dev->dev);
492
493         return 0;
494 }
495
496 static struct spi_driver ade7759_driver = {
497         .driver = {
498                 .name = "ade7759",
499                 .owner = THIS_MODULE,
500         },
501         .probe = ade7759_probe,
502         .remove = ade7759_remove,
503 };
504 module_spi_driver(ade7759_driver);
505
506 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
507 MODULE_DESCRIPTION("Analog Devices ADE7759 Active Energy Metering IC Driver");
508 MODULE_LICENSE("GPL v2");
509 MODULE_ALIAS("spi:ad7759");