staging:iio: Streamline API function naming
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / dac / ad5421.c
1 /*
2  * AD5421 Digital to analog converters  driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
9 #include <linux/device.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
22 #include "dac.h"
23 #include "ad5421.h"
24
25
26 #define AD5421_REG_DAC_DATA             0x1
27 #define AD5421_REG_CTRL                 0x2
28 #define AD5421_REG_OFFSET               0x3
29 #define AD5421_REG_GAIN                 0x4
30 /* load dac and fault shared the same register number. Writing to it will cause
31  * a dac load command, reading from it will return the fault status register */
32 #define AD5421_REG_LOAD_DAC             0x5
33 #define AD5421_REG_FAULT                0x5
34 #define AD5421_REG_FORCE_ALARM_CURRENT  0x6
35 #define AD5421_REG_RESET                0x7
36 #define AD5421_REG_START_CONVERSION     0x8
37 #define AD5421_REG_NOOP                 0x9
38
39 #define AD5421_CTRL_WATCHDOG_DISABLE    BIT(12)
40 #define AD5421_CTRL_AUTO_FAULT_READBACK BIT(11)
41 #define AD5421_CTRL_MIN_CURRENT         BIT(9)
42 #define AD5421_CTRL_ADC_SOURCE_TEMP     BIT(8)
43 #define AD5421_CTRL_ADC_ENABLE          BIT(7)
44 #define AD5421_CTRL_PWR_DOWN_INT_VREF   BIT(6)
45
46 #define AD5421_FAULT_SPI                        BIT(15)
47 #define AD5421_FAULT_PEC                        BIT(14)
48 #define AD5421_FAULT_OVER_CURRENT               BIT(13)
49 #define AD5421_FAULT_UNDER_CURRENT              BIT(12)
50 #define AD5421_FAULT_TEMP_OVER_140              BIT(11)
51 #define AD5421_FAULT_TEMP_OVER_100              BIT(10)
52 #define AD5421_FAULT_UNDER_VOLTAGE_6V           BIT(9)
53 #define AD5421_FAULT_UNDER_VOLTAGE_12V          BIT(8)
54
55 /* These bits will cause the fault pin to go high */
56 #define AD5421_FAULT_TRIGGER_IRQ \
57         (AD5421_FAULT_SPI | AD5421_FAULT_PEC | AD5421_FAULT_OVER_CURRENT | \
58         AD5421_FAULT_UNDER_CURRENT | AD5421_FAULT_TEMP_OVER_140)
59
60 /**
61  * struct ad5421_state - driver instance specific data
62  * @spi:                spi_device
63  * @ctrl:               control register cache
64  * @current_range:      current range which the device is configured for
65  * @data:               spi transfer buffers
66  * @fault_mask:         software masking of events
67  */
68 struct ad5421_state {
69         struct spi_device               *spi;
70         unsigned int                    ctrl;
71         enum ad5421_current_range       current_range;
72         unsigned int                    fault_mask;
73
74         /*
75          * DMA (thus cache coherency maintenance) requires the
76          * transfer buffers to live in their own cache lines.
77          */
78         union {
79                 u32 d32;
80                 u8 d8[4];
81         } data[2] ____cacheline_aligned;
82 };
83
84 static const struct iio_chan_spec ad5421_channels[] = {
85         {
86                 .type = IIO_CURRENT,
87                 .indexed = 1,
88                 .output = 1,
89                 .channel = 0,
90                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
91                         IIO_CHAN_INFO_SCALE_SHARED_BIT |
92                         IIO_CHAN_INFO_OFFSET_SHARED_BIT |
93                         IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
94                         IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
95                 .scan_type = IIO_ST('u', 16, 16, 0),
96                 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) |
97                         IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
98         },
99         {
100                 .type = IIO_TEMP,
101                 .channel = -1,
102                 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
103         },
104 };
105
106 static int ad5421_write_unlocked(struct iio_dev *indio_dev,
107         unsigned int reg, unsigned int val)
108 {
109         struct ad5421_state *st = iio_priv(indio_dev);
110
111         st->data[0].d32 = cpu_to_be32((reg << 16) | val);
112
113         return spi_write(st->spi, &st->data[0].d8[1], 3);
114 }
115
116 static int ad5421_write(struct iio_dev *indio_dev, unsigned int reg,
117         unsigned int val)
118 {
119         int ret;
120
121         mutex_lock(&indio_dev->mlock);
122         ret = ad5421_write_unlocked(indio_dev, reg, val);
123         mutex_unlock(&indio_dev->mlock);
124
125         return ret;
126 }
127
128 static int ad5421_read(struct iio_dev *indio_dev, unsigned int reg)
129 {
130         struct ad5421_state *st = iio_priv(indio_dev);
131         struct spi_message m;
132         int ret;
133         struct spi_transfer t[] = {
134                 {
135                         .tx_buf = &st->data[0].d8[1],
136                         .len = 3,
137                         .cs_change = 1,
138                 }, {
139                         .rx_buf = &st->data[1].d8[1],
140                         .len = 3,
141                 },
142         };
143
144         spi_message_init(&m);
145         spi_message_add_tail(&t[0], &m);
146         spi_message_add_tail(&t[1], &m);
147
148         mutex_lock(&indio_dev->mlock);
149
150         st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
151
152         ret = spi_sync(st->spi, &m);
153         if (ret >= 0)
154                 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
155
156         mutex_unlock(&indio_dev->mlock);
157
158         return ret;
159 }
160
161 static int ad5421_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
162         unsigned int clr)
163 {
164         struct ad5421_state *st = iio_priv(indio_dev);
165         unsigned int ret;
166
167         mutex_lock(&indio_dev->mlock);
168
169         st->ctrl &= ~clr;
170         st->ctrl |= set;
171
172         ret = ad5421_write_unlocked(indio_dev, AD5421_REG_CTRL, st->ctrl);
173
174         mutex_unlock(&indio_dev->mlock);
175
176         return ret;
177 }
178
179 static irqreturn_t ad5421_fault_handler(int irq, void *data)
180 {
181         struct iio_dev *indio_dev = data;
182         struct ad5421_state *st = iio_priv(indio_dev);
183         unsigned int fault;
184         unsigned int old_fault = 0;
185         unsigned int events;
186
187         fault = ad5421_read(indio_dev, AD5421_REG_FAULT);
188         if (!fault)
189                 return IRQ_NONE;
190
191         /* If we had a fault, this might mean that the DAC has lost its state
192          * and has been reset. Make sure that the control register actually
193          * contains what we expect it to contain. Otherwise the watchdog might
194          * be enabled and we get watchdog timeout faults, which will render the
195          * DAC unusable. */
196         ad5421_update_ctrl(indio_dev, 0, 0);
197
198
199         /* The fault pin stays high as long as a fault condition is present and
200          * it is not possible to mask fault conditions. For certain fault
201          * conditions for example like over-temperature it takes some time
202          * until the fault condition disappears. If we would exit the interrupt
203          * handler immediately after handling the event it would be entered
204          * again instantly. Thus we fall back to polling in case we detect that
205          * a interrupt condition is still present.
206          */
207         do {
208                 /* 0xffff is a invalid value for the register and will only be
209                  * read if there has been a communication error */
210                 if (fault == 0xffff)
211                         fault = 0;
212
213                 /* we are only interested in new events */
214                 events = (old_fault ^ fault) & fault;
215                 events &= st->fault_mask;
216
217                 if (events & AD5421_FAULT_OVER_CURRENT) {
218                         iio_push_event(indio_dev,
219                                 IIO_UNMOD_EVENT_CODE(IIO_CURRENT,
220                                         0,
221                                         IIO_EV_TYPE_THRESH,
222                                         IIO_EV_DIR_RISING),
223                         iio_get_time_ns());
224                 }
225
226                 if (events & AD5421_FAULT_UNDER_CURRENT) {
227                         iio_push_event(indio_dev,
228                                 IIO_UNMOD_EVENT_CODE(IIO_CURRENT,
229                                         0,
230                                         IIO_EV_TYPE_THRESH,
231                                         IIO_EV_DIR_FALLING),
232                                 iio_get_time_ns());
233                 }
234
235                 if (events & AD5421_FAULT_TEMP_OVER_140) {
236                         iio_push_event(indio_dev,
237                                 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
238                                         0,
239                                         IIO_EV_TYPE_MAG,
240                                         IIO_EV_DIR_RISING),
241                                 iio_get_time_ns());
242                 }
243
244                 old_fault = fault;
245                 fault = ad5421_read(indio_dev, AD5421_REG_FAULT);
246
247                 /* still active? go to sleep for some time */
248                 if (fault & AD5421_FAULT_TRIGGER_IRQ)
249                         msleep(1000);
250
251         } while (fault & AD5421_FAULT_TRIGGER_IRQ);
252
253
254         return IRQ_HANDLED;
255 }
256
257 static void ad5421_get_current_min_max(struct ad5421_state *st,
258         unsigned int *min, unsigned int *max)
259 {
260         /* The current range is configured using external pins, which are
261          * usually hard-wired and not run-time switchable. */
262         switch (st->current_range) {
263         case AD5421_CURRENT_RANGE_4mA_20mA:
264                 *min = 4000;
265                 *max = 20000;
266                 break;
267         case AD5421_CURRENT_RANGE_3mA8_21mA:
268                 *min = 3800;
269                 *max = 21000;
270                 break;
271         case AD5421_CURRENT_RANGE_3mA2_24mA:
272                 *min = 3200;
273                 *max = 24000;
274                 break;
275         default:
276                 *min = 0;
277                 *max = 1;
278                 break;
279         }
280 }
281
282 static inline unsigned int ad5421_get_offset(struct ad5421_state *st)
283 {
284         unsigned int min, max;
285
286         ad5421_get_current_min_max(st, &min, &max);
287         return (min * (1 << 16)) / (max - min);
288 }
289
290 static inline unsigned int ad5421_get_scale(struct ad5421_state *st)
291 {
292         unsigned int min, max;
293
294         ad5421_get_current_min_max(st, &min, &max);
295         return ((max - min) * 1000) / (1 << 16);
296 }
297
298 static int ad5421_read_raw(struct iio_dev *indio_dev,
299         struct iio_chan_spec const *chan, int *val, int *val2, long m)
300 {
301         struct ad5421_state *st = iio_priv(indio_dev);
302         int ret;
303
304         if (chan->type != IIO_CURRENT)
305                 return -EINVAL;
306
307         switch (m) {
308         case IIO_CHAN_INFO_RAW:
309                 ret = ad5421_read(indio_dev, AD5421_REG_DAC_DATA);
310                 if (ret < 0)
311                         return ret;
312                 *val = ret;
313                 return IIO_VAL_INT;
314         case IIO_CHAN_INFO_SCALE:
315                 *val = 0;
316                 *val2 = ad5421_get_scale(st);
317                 return IIO_VAL_INT_PLUS_MICRO;
318         case IIO_CHAN_INFO_OFFSET:
319                 *val = ad5421_get_offset(st);
320                 return IIO_VAL_INT;
321         case IIO_CHAN_INFO_CALIBBIAS:
322                 ret = ad5421_read(indio_dev, AD5421_REG_OFFSET);
323                 if (ret < 0)
324                         return ret;
325                 *val = ret - 32768;
326                 return IIO_VAL_INT;
327         case IIO_CHAN_INFO_CALIBSCALE:
328                 ret = ad5421_read(indio_dev, AD5421_REG_GAIN);
329                 if (ret < 0)
330                         return ret;
331                 *val = ret;
332                 return IIO_VAL_INT;
333         }
334
335         return -EINVAL;
336 }
337
338 static int ad5421_write_raw(struct iio_dev *indio_dev,
339         struct iio_chan_spec const *chan, int val, int val2, long mask)
340 {
341         const unsigned int max_val = 1 << 16;
342
343         switch (mask) {
344         case IIO_CHAN_INFO_RAW:
345                 if (val >= max_val || val < 0)
346                         return -EINVAL;
347
348                 return ad5421_write(indio_dev, AD5421_REG_DAC_DATA, val);
349         case IIO_CHAN_INFO_CALIBBIAS:
350                 val += 32768;
351                 if (val >= max_val || val < 0)
352                         return -EINVAL;
353
354                 return ad5421_write(indio_dev, AD5421_REG_OFFSET, val);
355         case IIO_CHAN_INFO_CALIBSCALE:
356                 if (val >= max_val || val < 0)
357                         return -EINVAL;
358
359                 return ad5421_write(indio_dev, AD5421_REG_GAIN, val);
360         default:
361                 break;
362         }
363
364         return -EINVAL;
365 }
366
367 static int ad5421_write_event_config(struct iio_dev *indio_dev,
368         u64 event_code, int state)
369 {
370         struct ad5421_state *st = iio_priv(indio_dev);
371         unsigned int mask;
372
373         switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
374         case IIO_CURRENT:
375                 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
376                         IIO_EV_DIR_RISING)
377                         mask = AD5421_FAULT_OVER_CURRENT;
378                 else
379                         mask = AD5421_FAULT_UNDER_CURRENT;
380                 break;
381         case IIO_TEMP:
382                 mask = AD5421_FAULT_TEMP_OVER_140;
383                 break;
384         default:
385                 return -EINVAL;
386         }
387
388         mutex_lock(&indio_dev->mlock);
389         if (state)
390                 st->fault_mask |= mask;
391         else
392                 st->fault_mask &= ~mask;
393         mutex_unlock(&indio_dev->mlock);
394
395         return 0;
396 }
397
398 static int ad5421_read_event_config(struct iio_dev *indio_dev,
399         u64 event_code)
400 {
401         struct ad5421_state *st = iio_priv(indio_dev);
402         unsigned int mask;
403
404         switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
405         case IIO_CURRENT:
406                 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
407                         IIO_EV_DIR_RISING)
408                         mask = AD5421_FAULT_OVER_CURRENT;
409                 else
410                         mask = AD5421_FAULT_UNDER_CURRENT;
411                 break;
412         case IIO_TEMP:
413                 mask = AD5421_FAULT_TEMP_OVER_140;
414                 break;
415         default:
416                 return -EINVAL;
417         }
418
419         return (bool)(st->fault_mask & mask);
420 }
421
422 static int ad5421_read_event_value(struct iio_dev *indio_dev, u64 event_code,
423         int *val)
424 {
425         int ret;
426
427         switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
428         case IIO_CURRENT:
429                 ret = ad5421_read(indio_dev, AD5421_REG_DAC_DATA);
430                 if (ret < 0)
431                         return ret;
432                 *val = ret;
433                 break;
434         case IIO_TEMP:
435                 *val = 140000;
436                 break;
437         default:
438                 return -EINVAL;
439         }
440
441         return 0;
442 }
443
444 static const struct iio_info ad5421_info = {
445         .read_raw =             ad5421_read_raw,
446         .write_raw =            ad5421_write_raw,
447         .read_event_config =    ad5421_read_event_config,
448         .write_event_config =   ad5421_write_event_config,
449         .read_event_value =     ad5421_read_event_value,
450         .driver_module =        THIS_MODULE,
451 };
452
453 static int __devinit ad5421_probe(struct spi_device *spi)
454 {
455         struct ad5421_platform_data *pdata = dev_get_platdata(&spi->dev);
456         struct iio_dev *indio_dev;
457         struct ad5421_state *st;
458         int ret;
459
460         indio_dev = iio_device_alloc(sizeof(*st));
461         if (indio_dev == NULL) {
462                 dev_err(&spi->dev, "Failed to allocate iio device\n");
463                 return  -ENOMEM;
464         }
465
466         st = iio_priv(indio_dev);
467         spi_set_drvdata(spi, indio_dev);
468
469         st->spi = spi;
470
471         indio_dev->dev.parent = &spi->dev;
472         indio_dev->name = "ad5421";
473         indio_dev->info = &ad5421_info;
474         indio_dev->modes = INDIO_DIRECT_MODE;
475         indio_dev->channels = ad5421_channels;
476         indio_dev->num_channels = ARRAY_SIZE(ad5421_channels);
477
478         st->ctrl = AD5421_CTRL_WATCHDOG_DISABLE |
479                         AD5421_CTRL_AUTO_FAULT_READBACK;
480
481         if (pdata) {
482                 st->current_range = pdata->current_range;
483                 if (pdata->external_vref)
484                         st->ctrl |= AD5421_CTRL_PWR_DOWN_INT_VREF;
485         } else {
486                 st->current_range = AD5421_CURRENT_RANGE_4mA_20mA;
487         }
488
489         /* write initial ctrl register value */
490         ad5421_update_ctrl(indio_dev, 0, 0);
491
492         if (spi->irq) {
493                 ret = request_threaded_irq(spi->irq,
494                                            NULL,
495                                            ad5421_fault_handler,
496                                            IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
497                                            "ad5421 fault",
498                                            indio_dev);
499                 if (ret)
500                         goto error_free;
501         }
502
503         ret = iio_device_register(indio_dev);
504         if (ret) {
505                 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
506                 goto error_free_irq;
507         }
508
509         return 0;
510
511 error_free_irq:
512         if (spi->irq)
513                 free_irq(spi->irq, indio_dev);
514 error_free:
515         iio_device_free(indio_dev);
516
517         return ret;
518 }
519
520 static int __devexit ad5421_remove(struct spi_device *spi)
521 {
522         struct iio_dev *indio_dev = spi_get_drvdata(spi);
523
524         iio_device_unregister(indio_dev);
525         if (spi->irq)
526                 free_irq(spi->irq, indio_dev);
527         iio_device_free(indio_dev);
528
529         return 0;
530 }
531
532 static struct spi_driver ad5421_driver = {
533         .driver = {
534                    .name = "ad5421",
535                    .owner = THIS_MODULE,
536         },
537         .probe = ad5421_probe,
538         .remove = __devexit_p(ad5421_remove),
539 };
540 module_spi_driver(ad5421_driver);
541
542 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
543 MODULE_DESCRIPTION("Analog Devices AD5421 DAC");
544 MODULE_LICENSE("GPL v2");
545 MODULE_ALIAS("spi:ad5421");