GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / iio / adc / xilinx-xadc-core.c
1 /*
2  * Xilinx XADC driver
3  *
4  * Copyright 2013-2014 Analog Devices Inc.
5  *  Author: Lars-Peter Clauen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2.
8  *
9  * Documentation for the parts can be found at:
10  *  - XADC hardmacro: Xilinx UG480
11  *  - ZYNQ XADC interface: Xilinx UG585
12  *  - AXI XADC interface: Xilinx PG019
13  */
14
15 #include <linux/clk.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
26
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/events.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/trigger.h>
32 #include <linux/iio/trigger_consumer.h>
33 #include <linux/iio/triggered_buffer.h>
34
35 #include "xilinx-xadc.h"
36
37 static const unsigned int XADC_ZYNQ_UNMASK_TIMEOUT = 500;
38
39 /* ZYNQ register definitions */
40 #define XADC_ZYNQ_REG_CFG       0x00
41 #define XADC_ZYNQ_REG_INTSTS    0x04
42 #define XADC_ZYNQ_REG_INTMSK    0x08
43 #define XADC_ZYNQ_REG_STATUS    0x0c
44 #define XADC_ZYNQ_REG_CFIFO     0x10
45 #define XADC_ZYNQ_REG_DFIFO     0x14
46 #define XADC_ZYNQ_REG_CTL               0x18
47
48 #define XADC_ZYNQ_CFG_ENABLE            BIT(31)
49 #define XADC_ZYNQ_CFG_CFIFOTH_MASK      (0xf << 20)
50 #define XADC_ZYNQ_CFG_CFIFOTH_OFFSET    20
51 #define XADC_ZYNQ_CFG_DFIFOTH_MASK      (0xf << 16)
52 #define XADC_ZYNQ_CFG_DFIFOTH_OFFSET    16
53 #define XADC_ZYNQ_CFG_WEDGE             BIT(13)
54 #define XADC_ZYNQ_CFG_REDGE             BIT(12)
55 #define XADC_ZYNQ_CFG_TCKRATE_MASK      (0x3 << 8)
56 #define XADC_ZYNQ_CFG_TCKRATE_DIV2      (0x0 << 8)
57 #define XADC_ZYNQ_CFG_TCKRATE_DIV4      (0x1 << 8)
58 #define XADC_ZYNQ_CFG_TCKRATE_DIV8      (0x2 << 8)
59 #define XADC_ZYNQ_CFG_TCKRATE_DIV16     (0x3 << 8)
60 #define XADC_ZYNQ_CFG_IGAP_MASK         0x1f
61 #define XADC_ZYNQ_CFG_IGAP(x)           (x)
62
63 #define XADC_ZYNQ_INT_CFIFO_LTH         BIT(9)
64 #define XADC_ZYNQ_INT_DFIFO_GTH         BIT(8)
65 #define XADC_ZYNQ_INT_ALARM_MASK        0xff
66 #define XADC_ZYNQ_INT_ALARM_OFFSET      0
67
68 #define XADC_ZYNQ_STATUS_CFIFO_LVL_MASK (0xf << 16)
69 #define XADC_ZYNQ_STATUS_CFIFO_LVL_OFFSET       16
70 #define XADC_ZYNQ_STATUS_DFIFO_LVL_MASK (0xf << 12)
71 #define XADC_ZYNQ_STATUS_DFIFO_LVL_OFFSET       12
72 #define XADC_ZYNQ_STATUS_CFIFOF         BIT(11)
73 #define XADC_ZYNQ_STATUS_CFIFOE         BIT(10)
74 #define XADC_ZYNQ_STATUS_DFIFOF         BIT(9)
75 #define XADC_ZYNQ_STATUS_DFIFOE         BIT(8)
76 #define XADC_ZYNQ_STATUS_OT             BIT(7)
77 #define XADC_ZYNQ_STATUS_ALM(x)         BIT(x)
78
79 #define XADC_ZYNQ_CTL_RESET             BIT(4)
80
81 #define XADC_ZYNQ_CMD_NOP               0x00
82 #define XADC_ZYNQ_CMD_READ              0x01
83 #define XADC_ZYNQ_CMD_WRITE             0x02
84
85 #define XADC_ZYNQ_CMD(cmd, addr, data) (((cmd) << 26) | ((addr) << 16) | (data))
86
87 /* AXI register definitions */
88 #define XADC_AXI_REG_RESET              0x00
89 #define XADC_AXI_REG_STATUS             0x04
90 #define XADC_AXI_REG_ALARM_STATUS       0x08
91 #define XADC_AXI_REG_CONVST             0x0c
92 #define XADC_AXI_REG_XADC_RESET         0x10
93 #define XADC_AXI_REG_GIER               0x5c
94 #define XADC_AXI_REG_IPISR              0x60
95 #define XADC_AXI_REG_IPIER              0x68
96 #define XADC_AXI_ADC_REG_OFFSET         0x200
97
98 #define XADC_AXI_RESET_MAGIC            0xa
99 #define XADC_AXI_GIER_ENABLE            BIT(31)
100
101 #define XADC_AXI_INT_EOS                BIT(4)
102 #define XADC_AXI_INT_ALARM_MASK         0x3c0f
103
104 #define XADC_FLAGS_BUFFERED BIT(0)
105
106 /*
107  * The XADC hardware supports a samplerate of up to 1MSPS. Unfortunately it does
108  * not have a hardware FIFO. Which means an interrupt is generated for each
109  * conversion sequence. At 1MSPS sample rate the CPU in ZYNQ7000 is completely
110  * overloaded by the interrupts that it soft-lockups. For this reason the driver
111  * limits the maximum samplerate 150kSPS. At this rate the CPU is fairly busy,
112  * but still responsive.
113  */
114 #define XADC_MAX_SAMPLERATE 150000
115
116 static void xadc_write_reg(struct xadc *xadc, unsigned int reg,
117         uint32_t val)
118 {
119         writel(val, xadc->base + reg);
120 }
121
122 static void xadc_read_reg(struct xadc *xadc, unsigned int reg,
123         uint32_t *val)
124 {
125         *val = readl(xadc->base + reg);
126 }
127
128 /*
129  * The ZYNQ interface uses two asynchronous FIFOs for communication with the
130  * XADC. Reads and writes to the XADC register are performed by submitting a
131  * request to the command FIFO (CFIFO), once the request has been completed the
132  * result can be read from the data FIFO (DFIFO). The method currently used in
133  * this driver is to submit the request for a read/write operation, then go to
134  * sleep and wait for an interrupt that signals that a response is available in
135  * the data FIFO.
136  */
137
138 static void xadc_zynq_write_fifo(struct xadc *xadc, uint32_t *cmd,
139         unsigned int n)
140 {
141         unsigned int i;
142
143         for (i = 0; i < n; i++)
144                 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFIFO, cmd[i]);
145 }
146
147 static void xadc_zynq_drain_fifo(struct xadc *xadc)
148 {
149         uint32_t status, tmp;
150
151         xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
152
153         while (!(status & XADC_ZYNQ_STATUS_DFIFOE)) {
154                 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
155                 xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
156         }
157 }
158
159 static void xadc_zynq_update_intmsk(struct xadc *xadc, unsigned int mask,
160         unsigned int val)
161 {
162         xadc->zynq_intmask &= ~mask;
163         xadc->zynq_intmask |= val;
164
165         xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK,
166                 xadc->zynq_intmask | xadc->zynq_masked_alarm);
167 }
168
169 static int xadc_zynq_write_adc_reg(struct xadc *xadc, unsigned int reg,
170         uint16_t val)
171 {
172         uint32_t cmd[1];
173         uint32_t tmp;
174         int ret;
175
176         spin_lock_irq(&xadc->lock);
177         xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
178                         XADC_ZYNQ_INT_DFIFO_GTH);
179
180         reinit_completion(&xadc->completion);
181
182         cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_WRITE, reg, val);
183         xadc_zynq_write_fifo(xadc, cmd, ARRAY_SIZE(cmd));
184         xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &tmp);
185         tmp &= ~XADC_ZYNQ_CFG_DFIFOTH_MASK;
186         tmp |= 0 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET;
187         xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, tmp);
188
189         xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
190         spin_unlock_irq(&xadc->lock);
191
192         ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
193         if (ret == 0)
194                 ret = -EIO;
195         else
196                 ret = 0;
197
198         xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
199
200         return ret;
201 }
202
203 static int xadc_zynq_read_adc_reg(struct xadc *xadc, unsigned int reg,
204         uint16_t *val)
205 {
206         uint32_t cmd[2];
207         uint32_t resp, tmp;
208         int ret;
209
210         cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_READ, reg, 0);
211         cmd[1] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_NOP, 0, 0);
212
213         spin_lock_irq(&xadc->lock);
214         xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
215                         XADC_ZYNQ_INT_DFIFO_GTH);
216         xadc_zynq_drain_fifo(xadc);
217         reinit_completion(&xadc->completion);
218
219         xadc_zynq_write_fifo(xadc, cmd, ARRAY_SIZE(cmd));
220         xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &tmp);
221         tmp &= ~XADC_ZYNQ_CFG_DFIFOTH_MASK;
222         tmp |= 1 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET;
223         xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, tmp);
224
225         xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
226         spin_unlock_irq(&xadc->lock);
227         ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
228         if (ret == 0)
229                 ret = -EIO;
230         if (ret < 0)
231                 return ret;
232
233         xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
234         xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
235
236         *val = resp & 0xffff;
237
238         return 0;
239 }
240
241 static unsigned int xadc_zynq_transform_alarm(unsigned int alarm)
242 {
243         return ((alarm & 0x80) >> 4) |
244                 ((alarm & 0x78) << 1) |
245                 (alarm & 0x07);
246 }
247
248 /*
249  * The ZYNQ threshold interrupts are level sensitive. Since we can't make the
250  * threshold condition go way from within the interrupt handler, this means as
251  * soon as a threshold condition is present we would enter the interrupt handler
252  * again and again. To work around this we mask all active thresholds interrupts
253  * in the interrupt handler and start a timer. In this timer we poll the
254  * interrupt status and only if the interrupt is inactive we unmask it again.
255  */
256 static void xadc_zynq_unmask_worker(struct work_struct *work)
257 {
258         struct xadc *xadc = container_of(work, struct xadc, zynq_unmask_work.work);
259         unsigned int misc_sts, unmask;
260
261         xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &misc_sts);
262
263         misc_sts &= XADC_ZYNQ_INT_ALARM_MASK;
264
265         spin_lock_irq(&xadc->lock);
266
267         /* Clear those bits which are not active anymore */
268         unmask = (xadc->zynq_masked_alarm ^ misc_sts) & xadc->zynq_masked_alarm;
269         xadc->zynq_masked_alarm &= misc_sts;
270
271         /* Also clear those which are masked out anyway */
272         xadc->zynq_masked_alarm &= ~xadc->zynq_intmask;
273
274         /* Clear the interrupts before we unmask them */
275         xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, unmask);
276
277         xadc_zynq_update_intmsk(xadc, 0, 0);
278
279         spin_unlock_irq(&xadc->lock);
280
281         /* if still pending some alarm re-trigger the timer */
282         if (xadc->zynq_masked_alarm) {
283                 schedule_delayed_work(&xadc->zynq_unmask_work,
284                                 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
285         }
286
287 }
288
289 static irqreturn_t xadc_zynq_interrupt_handler(int irq, void *devid)
290 {
291         struct iio_dev *indio_dev = devid;
292         struct xadc *xadc = iio_priv(indio_dev);
293         uint32_t status;
294
295         xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
296
297         status &= ~(xadc->zynq_intmask | xadc->zynq_masked_alarm);
298
299         if (!status)
300                 return IRQ_NONE;
301
302         spin_lock(&xadc->lock);
303
304         xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status);
305
306         if (status & XADC_ZYNQ_INT_DFIFO_GTH) {
307                 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
308                         XADC_ZYNQ_INT_DFIFO_GTH);
309                 complete(&xadc->completion);
310         }
311
312         status &= XADC_ZYNQ_INT_ALARM_MASK;
313         if (status) {
314                 xadc->zynq_masked_alarm |= status;
315                 /*
316                  * mask the current event interrupt,
317                  * unmask it when the interrupt is no more active.
318                  */
319                 xadc_zynq_update_intmsk(xadc, 0, 0);
320
321                 xadc_handle_events(indio_dev,
322                                 xadc_zynq_transform_alarm(status));
323
324                 /* unmask the required interrupts in timer. */
325                 schedule_delayed_work(&xadc->zynq_unmask_work,
326                                 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
327         }
328         spin_unlock(&xadc->lock);
329
330         return IRQ_HANDLED;
331 }
332
333 #define XADC_ZYNQ_TCK_RATE_MAX 50000000
334 #define XADC_ZYNQ_IGAP_DEFAULT 20
335 #define XADC_ZYNQ_PCAP_RATE_MAX 200000000
336
337 static int xadc_zynq_setup(struct platform_device *pdev,
338         struct iio_dev *indio_dev, int irq)
339 {
340         struct xadc *xadc = iio_priv(indio_dev);
341         unsigned long pcap_rate;
342         unsigned int tck_div;
343         unsigned int div;
344         unsigned int igap;
345         unsigned int tck_rate;
346         int ret;
347
348         /* TODO: Figure out how to make igap and tck_rate configurable */
349         igap = XADC_ZYNQ_IGAP_DEFAULT;
350         tck_rate = XADC_ZYNQ_TCK_RATE_MAX;
351
352         xadc->zynq_intmask = ~0;
353
354         pcap_rate = clk_get_rate(xadc->clk);
355         if (!pcap_rate)
356                 return -EINVAL;
357
358         if (pcap_rate > XADC_ZYNQ_PCAP_RATE_MAX) {
359                 ret = clk_set_rate(xadc->clk,
360                                    (unsigned long)XADC_ZYNQ_PCAP_RATE_MAX);
361                 if (ret)
362                         return ret;
363         }
364
365         if (tck_rate > pcap_rate / 2) {
366                 div = 2;
367         } else {
368                 div = pcap_rate / tck_rate;
369                 if (pcap_rate / div > XADC_ZYNQ_TCK_RATE_MAX)
370                         div++;
371         }
372
373         if (div <= 3)
374                 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV2;
375         else if (div <= 7)
376                 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV4;
377         else if (div <= 15)
378                 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV8;
379         else
380                 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV16;
381
382         xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, XADC_ZYNQ_CTL_RESET);
383         xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, 0);
384         xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, ~0);
385         xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK, xadc->zynq_intmask);
386         xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, XADC_ZYNQ_CFG_ENABLE |
387                         XADC_ZYNQ_CFG_REDGE | XADC_ZYNQ_CFG_WEDGE |
388                         tck_div | XADC_ZYNQ_CFG_IGAP(igap));
389
390         if (pcap_rate > XADC_ZYNQ_PCAP_RATE_MAX) {
391                 ret = clk_set_rate(xadc->clk, pcap_rate);
392                 if (ret)
393                         return ret;
394         }
395
396         return 0;
397 }
398
399 static unsigned long xadc_zynq_get_dclk_rate(struct xadc *xadc)
400 {
401         unsigned int div;
402         uint32_t val;
403
404         xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &val);
405
406         switch (val & XADC_ZYNQ_CFG_TCKRATE_MASK) {
407         case XADC_ZYNQ_CFG_TCKRATE_DIV4:
408                 div = 4;
409                 break;
410         case XADC_ZYNQ_CFG_TCKRATE_DIV8:
411                 div = 8;
412                 break;
413         case XADC_ZYNQ_CFG_TCKRATE_DIV16:
414                 div = 16;
415                 break;
416         default:
417                 div = 2;
418                 break;
419         }
420
421         return clk_get_rate(xadc->clk) / div;
422 }
423
424 static void xadc_zynq_update_alarm(struct xadc *xadc, unsigned int alarm)
425 {
426         unsigned long flags;
427         uint32_t status;
428
429         /* Move OT to bit 7 */
430         alarm = ((alarm & 0x08) << 4) | ((alarm & 0xf0) >> 1) | (alarm & 0x07);
431
432         spin_lock_irqsave(&xadc->lock, flags);
433
434         /* Clear previous interrupts if any. */
435         xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
436         xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status & alarm);
437
438         xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_ALARM_MASK,
439                 ~alarm & XADC_ZYNQ_INT_ALARM_MASK);
440
441         spin_unlock_irqrestore(&xadc->lock, flags);
442 }
443
444 static const struct xadc_ops xadc_zynq_ops = {
445         .read = xadc_zynq_read_adc_reg,
446         .write = xadc_zynq_write_adc_reg,
447         .setup = xadc_zynq_setup,
448         .get_dclk_rate = xadc_zynq_get_dclk_rate,
449         .interrupt_handler = xadc_zynq_interrupt_handler,
450         .update_alarm = xadc_zynq_update_alarm,
451 };
452
453 static int xadc_axi_read_adc_reg(struct xadc *xadc, unsigned int reg,
454         uint16_t *val)
455 {
456         uint32_t val32;
457
458         xadc_read_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, &val32);
459         *val = val32 & 0xffff;
460
461         return 0;
462 }
463
464 static int xadc_axi_write_adc_reg(struct xadc *xadc, unsigned int reg,
465         uint16_t val)
466 {
467         xadc_write_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, val);
468
469         return 0;
470 }
471
472 static int xadc_axi_setup(struct platform_device *pdev,
473         struct iio_dev *indio_dev, int irq)
474 {
475         struct xadc *xadc = iio_priv(indio_dev);
476
477         xadc_write_reg(xadc, XADC_AXI_REG_RESET, XADC_AXI_RESET_MAGIC);
478         xadc_write_reg(xadc, XADC_AXI_REG_GIER, XADC_AXI_GIER_ENABLE);
479
480         return 0;
481 }
482
483 static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid)
484 {
485         struct iio_dev *indio_dev = devid;
486         struct xadc *xadc = iio_priv(indio_dev);
487         uint32_t status, mask;
488         unsigned int events;
489
490         xadc_read_reg(xadc, XADC_AXI_REG_IPISR, &status);
491         xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &mask);
492         status &= mask;
493
494         if (!status)
495                 return IRQ_NONE;
496
497         if ((status & XADC_AXI_INT_EOS) && xadc->trigger)
498                 iio_trigger_poll(xadc->trigger);
499
500         if (status & XADC_AXI_INT_ALARM_MASK) {
501                 /*
502                  * The order of the bits in the AXI-XADC status register does
503                  * not match the order of the bits in the XADC alarm enable
504                  * register. xadc_handle_events() expects the events to be in
505                  * the same order as the XADC alarm enable register.
506                  */
507                 events = (status & 0x000e) >> 1;
508                 events |= (status & 0x0001) << 3;
509                 events |= (status & 0x3c00) >> 6;
510                 xadc_handle_events(indio_dev, events);
511         }
512
513         xadc_write_reg(xadc, XADC_AXI_REG_IPISR, status);
514
515         return IRQ_HANDLED;
516 }
517
518 static void xadc_axi_update_alarm(struct xadc *xadc, unsigned int alarm)
519 {
520         uint32_t val;
521         unsigned long flags;
522
523         /*
524          * The order of the bits in the AXI-XADC status register does not match
525          * the order of the bits in the XADC alarm enable register. We get
526          * passed the alarm mask in the same order as in the XADC alarm enable
527          * register.
528          */
529         alarm = ((alarm & 0x07) << 1) | ((alarm & 0x08) >> 3) |
530                         ((alarm & 0xf0) << 6);
531
532         spin_lock_irqsave(&xadc->lock, flags);
533         xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
534         val &= ~XADC_AXI_INT_ALARM_MASK;
535         val |= alarm;
536         xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
537         spin_unlock_irqrestore(&xadc->lock, flags);
538 }
539
540 static unsigned long xadc_axi_get_dclk(struct xadc *xadc)
541 {
542         return clk_get_rate(xadc->clk);
543 }
544
545 static const struct xadc_ops xadc_axi_ops = {
546         .read = xadc_axi_read_adc_reg,
547         .write = xadc_axi_write_adc_reg,
548         .setup = xadc_axi_setup,
549         .get_dclk_rate = xadc_axi_get_dclk,
550         .update_alarm = xadc_axi_update_alarm,
551         .interrupt_handler = xadc_axi_interrupt_handler,
552         .flags = XADC_FLAGS_BUFFERED,
553 };
554
555 static int _xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
556         uint16_t mask, uint16_t val)
557 {
558         uint16_t tmp;
559         int ret;
560
561         ret = _xadc_read_adc_reg(xadc, reg, &tmp);
562         if (ret)
563                 return ret;
564
565         return _xadc_write_adc_reg(xadc, reg, (tmp & ~mask) | val);
566 }
567
568 static int xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
569         uint16_t mask, uint16_t val)
570 {
571         int ret;
572
573         mutex_lock(&xadc->mutex);
574         ret = _xadc_update_adc_reg(xadc, reg, mask, val);
575         mutex_unlock(&xadc->mutex);
576
577         return ret;
578 }
579
580 static unsigned long xadc_get_dclk_rate(struct xadc *xadc)
581 {
582         return xadc->ops->get_dclk_rate(xadc);
583 }
584
585 static int xadc_update_scan_mode(struct iio_dev *indio_dev,
586         const unsigned long *mask)
587 {
588         struct xadc *xadc = iio_priv(indio_dev);
589         unsigned int n;
590
591         n = bitmap_weight(mask, indio_dev->masklength);
592
593         kfree(xadc->data);
594         xadc->data = kcalloc(n, sizeof(*xadc->data), GFP_KERNEL);
595         if (!xadc->data)
596                 return -ENOMEM;
597
598         return 0;
599 }
600
601 static unsigned int xadc_scan_index_to_channel(unsigned int scan_index)
602 {
603         switch (scan_index) {
604         case 5:
605                 return XADC_REG_VCCPINT;
606         case 6:
607                 return XADC_REG_VCCPAUX;
608         case 7:
609                 return XADC_REG_VCCO_DDR;
610         case 8:
611                 return XADC_REG_TEMP;
612         case 9:
613                 return XADC_REG_VCCINT;
614         case 10:
615                 return XADC_REG_VCCAUX;
616         case 11:
617                 return XADC_REG_VPVN;
618         case 12:
619                 return XADC_REG_VREFP;
620         case 13:
621                 return XADC_REG_VREFN;
622         case 14:
623                 return XADC_REG_VCCBRAM;
624         default:
625                 return XADC_REG_VAUX(scan_index - 16);
626         }
627 }
628
629 static irqreturn_t xadc_trigger_handler(int irq, void *p)
630 {
631         struct iio_poll_func *pf = p;
632         struct iio_dev *indio_dev = pf->indio_dev;
633         struct xadc *xadc = iio_priv(indio_dev);
634         unsigned int chan;
635         int i, j;
636
637         if (!xadc->data)
638                 goto out;
639
640         j = 0;
641         for_each_set_bit(i, indio_dev->active_scan_mask,
642                 indio_dev->masklength) {
643                 chan = xadc_scan_index_to_channel(i);
644                 xadc_read_adc_reg(xadc, chan, &xadc->data[j]);
645                 j++;
646         }
647
648         iio_push_to_buffers(indio_dev, xadc->data);
649
650 out:
651         iio_trigger_notify_done(indio_dev->trig);
652
653         return IRQ_HANDLED;
654 }
655
656 static int xadc_trigger_set_state(struct iio_trigger *trigger, bool state)
657 {
658         struct xadc *xadc = iio_trigger_get_drvdata(trigger);
659         unsigned long flags;
660         unsigned int convst;
661         unsigned int val;
662         int ret = 0;
663
664         mutex_lock(&xadc->mutex);
665
666         if (state) {
667                 /* Only one of the two triggers can be active at the a time. */
668                 if (xadc->trigger != NULL) {
669                         ret = -EBUSY;
670                         goto err_out;
671                 } else {
672                         xadc->trigger = trigger;
673                         if (trigger == xadc->convst_trigger)
674                                 convst = XADC_CONF0_EC;
675                         else
676                                 convst = 0;
677                 }
678                 ret = _xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF0_EC,
679                                         convst);
680                 if (ret)
681                         goto err_out;
682         } else {
683                 xadc->trigger = NULL;
684         }
685
686         spin_lock_irqsave(&xadc->lock, flags);
687         xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
688         xadc_write_reg(xadc, XADC_AXI_REG_IPISR, XADC_AXI_INT_EOS);
689         if (state)
690                 val |= XADC_AXI_INT_EOS;
691         else
692                 val &= ~XADC_AXI_INT_EOS;
693         xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
694         spin_unlock_irqrestore(&xadc->lock, flags);
695
696 err_out:
697         mutex_unlock(&xadc->mutex);
698
699         return ret;
700 }
701
702 static const struct iio_trigger_ops xadc_trigger_ops = {
703         .set_trigger_state = &xadc_trigger_set_state,
704 };
705
706 static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev,
707         const char *name)
708 {
709         struct iio_trigger *trig;
710         int ret;
711
712         trig = iio_trigger_alloc("%s%d-%s", indio_dev->name,
713                                 indio_dev->id, name);
714         if (trig == NULL)
715                 return ERR_PTR(-ENOMEM);
716
717         trig->dev.parent = indio_dev->dev.parent;
718         trig->ops = &xadc_trigger_ops;
719         iio_trigger_set_drvdata(trig, iio_priv(indio_dev));
720
721         ret = iio_trigger_register(trig);
722         if (ret)
723                 goto error_free_trig;
724
725         return trig;
726
727 error_free_trig:
728         iio_trigger_free(trig);
729         return ERR_PTR(ret);
730 }
731
732 static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode)
733 {
734         uint16_t val;
735
736         /* Powerdown the ADC-B when it is not needed. */
737         switch (seq_mode) {
738         case XADC_CONF1_SEQ_SIMULTANEOUS:
739         case XADC_CONF1_SEQ_INDEPENDENT:
740                 val = 0;
741                 break;
742         default:
743                 val = XADC_CONF2_PD_ADC_B;
744                 break;
745         }
746
747         return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_PD_MASK,
748                 val);
749 }
750
751 static int xadc_get_seq_mode(struct xadc *xadc, unsigned long scan_mode)
752 {
753         unsigned int aux_scan_mode = scan_mode >> 16;
754
755         if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_DUAL)
756                 return XADC_CONF1_SEQ_SIMULTANEOUS;
757
758         if ((aux_scan_mode & 0xff00) == 0 ||
759                 (aux_scan_mode & 0x00ff) == 0)
760                 return XADC_CONF1_SEQ_CONTINUOUS;
761
762         return XADC_CONF1_SEQ_SIMULTANEOUS;
763 }
764
765 static int xadc_postdisable(struct iio_dev *indio_dev)
766 {
767         struct xadc *xadc = iio_priv(indio_dev);
768         unsigned long scan_mask;
769         int ret;
770         int i;
771
772         scan_mask = 1; /* Run calibration as part of the sequence */
773         for (i = 0; i < indio_dev->num_channels; i++)
774                 scan_mask |= BIT(indio_dev->channels[i].scan_index);
775
776         /* Enable all channels and calibration */
777         ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
778         if (ret)
779                 return ret;
780
781         ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
782         if (ret)
783                 return ret;
784
785         ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
786                 XADC_CONF1_SEQ_CONTINUOUS);
787         if (ret)
788                 return ret;
789
790         return xadc_power_adc_b(xadc, XADC_CONF1_SEQ_CONTINUOUS);
791 }
792
793 static int xadc_preenable(struct iio_dev *indio_dev)
794 {
795         struct xadc *xadc = iio_priv(indio_dev);
796         unsigned long scan_mask;
797         int seq_mode;
798         int ret;
799
800         ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
801                 XADC_CONF1_SEQ_DEFAULT);
802         if (ret)
803                 goto err;
804
805         scan_mask = *indio_dev->active_scan_mask;
806         seq_mode = xadc_get_seq_mode(xadc, scan_mask);
807
808         ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
809         if (ret)
810                 goto err;
811
812         /*
813          * In simultaneous mode the upper and lower aux channels are samples at
814          * the same time. In this mode the upper 8 bits in the sequencer
815          * register are don't care and the lower 8 bits control two channels
816          * each. As such we must set the bit if either the channel in the lower
817          * group or the upper group is enabled.
818          */
819         if (seq_mode == XADC_CONF1_SEQ_SIMULTANEOUS)
820                 scan_mask = ((scan_mask >> 8) | scan_mask) & 0xff0000;
821
822         ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
823         if (ret)
824                 goto err;
825
826         ret = xadc_power_adc_b(xadc, seq_mode);
827         if (ret)
828                 goto err;
829
830         ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
831                 seq_mode);
832         if (ret)
833                 goto err;
834
835         return 0;
836 err:
837         xadc_postdisable(indio_dev);
838         return ret;
839 }
840
841 static const struct iio_buffer_setup_ops xadc_buffer_ops = {
842         .preenable = &xadc_preenable,
843         .postenable = &iio_triggered_buffer_postenable,
844         .predisable = &iio_triggered_buffer_predisable,
845         .postdisable = &xadc_postdisable,
846 };
847
848 static int xadc_read_samplerate(struct xadc *xadc)
849 {
850         unsigned int div;
851         uint16_t val16;
852         int ret;
853
854         ret = xadc_read_adc_reg(xadc, XADC_REG_CONF2, &val16);
855         if (ret)
856                 return ret;
857
858         div = (val16 & XADC_CONF2_DIV_MASK) >> XADC_CONF2_DIV_OFFSET;
859         if (div < 2)
860                 div = 2;
861
862         return xadc_get_dclk_rate(xadc) / div / 26;
863 }
864
865 static int xadc_read_raw(struct iio_dev *indio_dev,
866         struct iio_chan_spec const *chan, int *val, int *val2, long info)
867 {
868         struct xadc *xadc = iio_priv(indio_dev);
869         uint16_t val16;
870         int ret;
871
872         switch (info) {
873         case IIO_CHAN_INFO_RAW:
874                 if (iio_buffer_enabled(indio_dev))
875                         return -EBUSY;
876                 ret = xadc_read_adc_reg(xadc, chan->address, &val16);
877                 if (ret < 0)
878                         return ret;
879
880                 val16 >>= 4;
881                 if (chan->scan_type.sign == 'u')
882                         *val = val16;
883                 else
884                         *val = sign_extend32(val16, 11);
885
886                 return IIO_VAL_INT;
887         case IIO_CHAN_INFO_SCALE:
888                 switch (chan->type) {
889                 case IIO_VOLTAGE:
890                         /* V = (val * 3.0) / 4096 */
891                         switch (chan->address) {
892                         case XADC_REG_VCCINT:
893                         case XADC_REG_VCCAUX:
894                         case XADC_REG_VREFP:
895                         case XADC_REG_VREFN:
896                         case XADC_REG_VCCBRAM:
897                         case XADC_REG_VCCPINT:
898                         case XADC_REG_VCCPAUX:
899                         case XADC_REG_VCCO_DDR:
900                                 *val = 3000;
901                                 break;
902                         default:
903                                 *val = 1000;
904                                 break;
905                         }
906                         *val2 = 12;
907                         return IIO_VAL_FRACTIONAL_LOG2;
908                 case IIO_TEMP:
909                         /* Temp in C = (val * 503.975) / 4096 - 273.15 */
910                         *val = 503975;
911                         *val2 = 12;
912                         return IIO_VAL_FRACTIONAL_LOG2;
913                 default:
914                         return -EINVAL;
915                 }
916         case IIO_CHAN_INFO_OFFSET:
917                 /* Only the temperature channel has an offset */
918                 *val = -((273150 << 12) / 503975);
919                 return IIO_VAL_INT;
920         case IIO_CHAN_INFO_SAMP_FREQ:
921                 ret = xadc_read_samplerate(xadc);
922                 if (ret < 0)
923                         return ret;
924
925                 *val = ret;
926                 return IIO_VAL_INT;
927         default:
928                 return -EINVAL;
929         }
930 }
931
932 static int xadc_write_samplerate(struct xadc *xadc, int val)
933 {
934         unsigned long clk_rate = xadc_get_dclk_rate(xadc);
935         unsigned int div;
936
937         if (!clk_rate)
938                 return -EINVAL;
939
940         if (val <= 0)
941                 return -EINVAL;
942
943         /* Max. 150 kSPS */
944         if (val > XADC_MAX_SAMPLERATE)
945                 val = XADC_MAX_SAMPLERATE;
946
947         val *= 26;
948
949         /* Min 1MHz */
950         if (val < 1000000)
951                 val = 1000000;
952
953         /*
954          * We want to round down, but only if we do not exceed the 150 kSPS
955          * limit.
956          */
957         div = clk_rate / val;
958         if (clk_rate / div / 26 > XADC_MAX_SAMPLERATE)
959                 div++;
960         if (div < 2)
961                 div = 2;
962         else if (div > 0xff)
963                 div = 0xff;
964
965         return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_DIV_MASK,
966                 div << XADC_CONF2_DIV_OFFSET);
967 }
968
969 static int xadc_write_raw(struct iio_dev *indio_dev,
970         struct iio_chan_spec const *chan, int val, int val2, long info)
971 {
972         struct xadc *xadc = iio_priv(indio_dev);
973
974         if (info != IIO_CHAN_INFO_SAMP_FREQ)
975                 return -EINVAL;
976
977         return xadc_write_samplerate(xadc, val);
978 }
979
980 static const struct iio_event_spec xadc_temp_events[] = {
981         {
982                 .type = IIO_EV_TYPE_THRESH,
983                 .dir = IIO_EV_DIR_RISING,
984                 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
985                                 BIT(IIO_EV_INFO_VALUE) |
986                                 BIT(IIO_EV_INFO_HYSTERESIS),
987         },
988 };
989
990 /* Separate values for upper and lower thresholds, but only a shared enabled */
991 static const struct iio_event_spec xadc_voltage_events[] = {
992         {
993                 .type = IIO_EV_TYPE_THRESH,
994                 .dir = IIO_EV_DIR_RISING,
995                 .mask_separate = BIT(IIO_EV_INFO_VALUE),
996         }, {
997                 .type = IIO_EV_TYPE_THRESH,
998                 .dir = IIO_EV_DIR_FALLING,
999                 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1000         }, {
1001                 .type = IIO_EV_TYPE_THRESH,
1002                 .dir = IIO_EV_DIR_EITHER,
1003                 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1004         },
1005 };
1006
1007 #define XADC_CHAN_TEMP(_chan, _scan_index, _addr) { \
1008         .type = IIO_TEMP, \
1009         .indexed = 1, \
1010         .channel = (_chan), \
1011         .address = (_addr), \
1012         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1013                 BIT(IIO_CHAN_INFO_SCALE) | \
1014                 BIT(IIO_CHAN_INFO_OFFSET), \
1015         .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1016         .event_spec = xadc_temp_events, \
1017         .num_event_specs = ARRAY_SIZE(xadc_temp_events), \
1018         .scan_index = (_scan_index), \
1019         .scan_type = { \
1020                 .sign = 'u', \
1021                 .realbits = 12, \
1022                 .storagebits = 16, \
1023                 .shift = 4, \
1024                 .endianness = IIO_CPU, \
1025         }, \
1026 }
1027
1028 #define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) { \
1029         .type = IIO_VOLTAGE, \
1030         .indexed = 1, \
1031         .channel = (_chan), \
1032         .address = (_addr), \
1033         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1034                 BIT(IIO_CHAN_INFO_SCALE), \
1035         .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1036         .event_spec = (_alarm) ? xadc_voltage_events : NULL, \
1037         .num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \
1038         .scan_index = (_scan_index), \
1039         .scan_type = { \
1040                 .sign = ((_addr) == XADC_REG_VREFN) ? 's' : 'u', \
1041                 .realbits = 12, \
1042                 .storagebits = 16, \
1043                 .shift = 4, \
1044                 .endianness = IIO_CPU, \
1045         }, \
1046         .extend_name = _ext, \
1047 }
1048
1049 static const struct iio_chan_spec xadc_channels[] = {
1050         XADC_CHAN_TEMP(0, 8, XADC_REG_TEMP),
1051         XADC_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true),
1052         XADC_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX, "vccaux", true),
1053         XADC_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true),
1054         XADC_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpint", true),
1055         XADC_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpaux", true),
1056         XADC_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccoddr", true),
1057         XADC_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false),
1058         XADC_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false),
1059         XADC_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false),
1060         XADC_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false),
1061         XADC_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false),
1062         XADC_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false),
1063         XADC_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false),
1064         XADC_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false),
1065         XADC_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false),
1066         XADC_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false),
1067         XADC_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false),
1068         XADC_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false),
1069         XADC_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false),
1070         XADC_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false),
1071         XADC_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false),
1072         XADC_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false),
1073         XADC_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false),
1074         XADC_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false),
1075         XADC_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false),
1076 };
1077
1078 static const struct iio_info xadc_info = {
1079         .read_raw = &xadc_read_raw,
1080         .write_raw = &xadc_write_raw,
1081         .read_event_config = &xadc_read_event_config,
1082         .write_event_config = &xadc_write_event_config,
1083         .read_event_value = &xadc_read_event_value,
1084         .write_event_value = &xadc_write_event_value,
1085         .update_scan_mode = &xadc_update_scan_mode,
1086 };
1087
1088 static const struct of_device_id xadc_of_match_table[] = {
1089         { .compatible = "xlnx,zynq-xadc-1.00.a", (void *)&xadc_zynq_ops },
1090         { .compatible = "xlnx,axi-xadc-1.00.a", (void *)&xadc_axi_ops },
1091         { },
1092 };
1093 MODULE_DEVICE_TABLE(of, xadc_of_match_table);
1094
1095 static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np,
1096         unsigned int *conf)
1097 {
1098         struct xadc *xadc = iio_priv(indio_dev);
1099         struct iio_chan_spec *channels, *chan;
1100         struct device_node *chan_node, *child;
1101         unsigned int num_channels;
1102         const char *external_mux;
1103         u32 ext_mux_chan;
1104         u32 reg;
1105         int ret;
1106
1107         *conf = 0;
1108
1109         ret = of_property_read_string(np, "xlnx,external-mux", &external_mux);
1110         if (ret < 0 || strcasecmp(external_mux, "none") == 0)
1111                 xadc->external_mux_mode = XADC_EXTERNAL_MUX_NONE;
1112         else if (strcasecmp(external_mux, "single") == 0)
1113                 xadc->external_mux_mode = XADC_EXTERNAL_MUX_SINGLE;
1114         else if (strcasecmp(external_mux, "dual") == 0)
1115                 xadc->external_mux_mode = XADC_EXTERNAL_MUX_DUAL;
1116         else
1117                 return -EINVAL;
1118
1119         if (xadc->external_mux_mode != XADC_EXTERNAL_MUX_NONE) {
1120                 ret = of_property_read_u32(np, "xlnx,external-mux-channel",
1121                                         &ext_mux_chan);
1122                 if (ret < 0)
1123                         return ret;
1124
1125                 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_SINGLE) {
1126                         if (ext_mux_chan == 0)
1127                                 ext_mux_chan = XADC_REG_VPVN;
1128                         else if (ext_mux_chan <= 16)
1129                                 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1130                         else
1131                                 return -EINVAL;
1132                 } else {
1133                         if (ext_mux_chan > 0 && ext_mux_chan <= 8)
1134                                 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1135                         else
1136                                 return -EINVAL;
1137                 }
1138
1139                 *conf |= XADC_CONF0_MUX | XADC_CONF0_CHAN(ext_mux_chan);
1140         }
1141
1142         channels = kmemdup(xadc_channels, sizeof(xadc_channels), GFP_KERNEL);
1143         if (!channels)
1144                 return -ENOMEM;
1145
1146         num_channels = 9;
1147         chan = &channels[9];
1148
1149         chan_node = of_get_child_by_name(np, "xlnx,channels");
1150         if (chan_node) {
1151                 for_each_child_of_node(chan_node, child) {
1152                         if (num_channels >= ARRAY_SIZE(xadc_channels)) {
1153                                 of_node_put(child);
1154                                 break;
1155                         }
1156
1157                         ret = of_property_read_u32(child, "reg", &reg);
1158                         if (ret || reg > 16)
1159                                 continue;
1160
1161                         if (of_property_read_bool(child, "xlnx,bipolar"))
1162                                 chan->scan_type.sign = 's';
1163
1164                         if (reg == 0) {
1165                                 chan->scan_index = 11;
1166                                 chan->address = XADC_REG_VPVN;
1167                         } else {
1168                                 chan->scan_index = 15 + reg;
1169                                 chan->address = XADC_REG_VAUX(reg - 1);
1170                         }
1171                         num_channels++;
1172                         chan++;
1173                 }
1174         }
1175         of_node_put(chan_node);
1176
1177         indio_dev->num_channels = num_channels;
1178         indio_dev->channels = krealloc(channels, sizeof(*channels) *
1179                                         num_channels, GFP_KERNEL);
1180         /* If we can't resize the channels array, just use the original */
1181         if (!indio_dev->channels)
1182                 indio_dev->channels = channels;
1183
1184         return 0;
1185 }
1186
1187 static int xadc_probe(struct platform_device *pdev)
1188 {
1189         const struct of_device_id *id;
1190         struct iio_dev *indio_dev;
1191         unsigned int bipolar_mask;
1192         struct resource *mem;
1193         unsigned int conf0;
1194         struct xadc *xadc;
1195         int ret;
1196         int irq;
1197         int i;
1198
1199         if (!pdev->dev.of_node)
1200                 return -ENODEV;
1201
1202         id = of_match_node(xadc_of_match_table, pdev->dev.of_node);
1203         if (!id)
1204                 return -EINVAL;
1205
1206         irq = platform_get_irq(pdev, 0);
1207         if (irq <= 0)
1208                 return -ENXIO;
1209
1210         indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*xadc));
1211         if (!indio_dev)
1212                 return -ENOMEM;
1213
1214         xadc = iio_priv(indio_dev);
1215         xadc->ops = id->data;
1216         xadc->irq = irq;
1217         init_completion(&xadc->completion);
1218         mutex_init(&xadc->mutex);
1219         spin_lock_init(&xadc->lock);
1220         INIT_DELAYED_WORK(&xadc->zynq_unmask_work, xadc_zynq_unmask_worker);
1221
1222         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1223         xadc->base = devm_ioremap_resource(&pdev->dev, mem);
1224         if (IS_ERR(xadc->base))
1225                 return PTR_ERR(xadc->base);
1226
1227         indio_dev->dev.parent = &pdev->dev;
1228         indio_dev->dev.of_node = pdev->dev.of_node;
1229         indio_dev->name = "xadc";
1230         indio_dev->modes = INDIO_DIRECT_MODE;
1231         indio_dev->info = &xadc_info;
1232
1233         ret = xadc_parse_dt(indio_dev, pdev->dev.of_node, &conf0);
1234         if (ret)
1235                 goto err_device_free;
1236
1237         if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1238                 ret = iio_triggered_buffer_setup(indio_dev,
1239                         &iio_pollfunc_store_time, &xadc_trigger_handler,
1240                         &xadc_buffer_ops);
1241                 if (ret)
1242                         goto err_device_free;
1243
1244                 xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst");
1245                 if (IS_ERR(xadc->convst_trigger)) {
1246                         ret = PTR_ERR(xadc->convst_trigger);
1247                         goto err_triggered_buffer_cleanup;
1248                 }
1249                 xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev,
1250                         "samplerate");
1251                 if (IS_ERR(xadc->samplerate_trigger)) {
1252                         ret = PTR_ERR(xadc->samplerate_trigger);
1253                         goto err_free_convst_trigger;
1254                 }
1255         }
1256
1257         xadc->clk = devm_clk_get(&pdev->dev, NULL);
1258         if (IS_ERR(xadc->clk)) {
1259                 ret = PTR_ERR(xadc->clk);
1260                 goto err_free_samplerate_trigger;
1261         }
1262
1263         ret = clk_prepare_enable(xadc->clk);
1264         if (ret)
1265                 goto err_free_samplerate_trigger;
1266
1267         /*
1268          * Make sure not to exceed the maximum samplerate since otherwise the
1269          * resulting interrupt storm will soft-lock the system.
1270          */
1271         if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1272                 ret = xadc_read_samplerate(xadc);
1273                 if (ret < 0)
1274                         goto err_free_samplerate_trigger;
1275                 if (ret > XADC_MAX_SAMPLERATE) {
1276                         ret = xadc_write_samplerate(xadc, XADC_MAX_SAMPLERATE);
1277                         if (ret < 0)
1278                                 goto err_free_samplerate_trigger;
1279                 }
1280         }
1281
1282         ret = request_irq(xadc->irq, xadc->ops->interrupt_handler, 0,
1283                         dev_name(&pdev->dev), indio_dev);
1284         if (ret)
1285                 goto err_clk_disable_unprepare;
1286
1287         ret = xadc->ops->setup(pdev, indio_dev, xadc->irq);
1288         if (ret)
1289                 goto err_free_irq;
1290
1291         for (i = 0; i < 16; i++)
1292                 xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1293                         &xadc->threshold[i]);
1294
1295         ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0);
1296         if (ret)
1297                 goto err_free_irq;
1298
1299         bipolar_mask = 0;
1300         for (i = 0; i < indio_dev->num_channels; i++) {
1301                 if (indio_dev->channels[i].scan_type.sign == 's')
1302                         bipolar_mask |= BIT(indio_dev->channels[i].scan_index);
1303         }
1304
1305         ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask);
1306         if (ret)
1307                 goto err_free_irq;
1308         ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1),
1309                 bipolar_mask >> 16);
1310         if (ret)
1311                 goto err_free_irq;
1312
1313         /* Disable all alarms */
1314         ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_ALARM_MASK,
1315                                   XADC_CONF1_ALARM_MASK);
1316         if (ret)
1317                 goto err_free_irq;
1318
1319         /* Set thresholds to min/max */
1320         for (i = 0; i < 16; i++) {
1321                 /*
1322                  * Set max voltage threshold and both temperature thresholds to
1323                  * 0xffff, min voltage threshold to 0.
1324                  */
1325                 if (i % 8 < 4 || i == 7)
1326                         xadc->threshold[i] = 0xffff;
1327                 else
1328                         xadc->threshold[i] = 0;
1329                 xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1330                         xadc->threshold[i]);
1331         }
1332
1333         /* Go to non-buffered mode */
1334         xadc_postdisable(indio_dev);
1335
1336         ret = iio_device_register(indio_dev);
1337         if (ret)
1338                 goto err_free_irq;
1339
1340         platform_set_drvdata(pdev, indio_dev);
1341
1342         return 0;
1343
1344 err_free_irq:
1345         free_irq(xadc->irq, indio_dev);
1346         cancel_delayed_work_sync(&xadc->zynq_unmask_work);
1347 err_clk_disable_unprepare:
1348         clk_disable_unprepare(xadc->clk);
1349 err_free_samplerate_trigger:
1350         if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1351                 iio_trigger_free(xadc->samplerate_trigger);
1352 err_free_convst_trigger:
1353         if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1354                 iio_trigger_free(xadc->convst_trigger);
1355 err_triggered_buffer_cleanup:
1356         if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1357                 iio_triggered_buffer_cleanup(indio_dev);
1358 err_device_free:
1359         kfree(indio_dev->channels);
1360
1361         return ret;
1362 }
1363
1364 static int xadc_remove(struct platform_device *pdev)
1365 {
1366         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1367         struct xadc *xadc = iio_priv(indio_dev);
1368
1369         iio_device_unregister(indio_dev);
1370         if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1371                 iio_trigger_free(xadc->samplerate_trigger);
1372                 iio_trigger_free(xadc->convst_trigger);
1373                 iio_triggered_buffer_cleanup(indio_dev);
1374         }
1375         free_irq(xadc->irq, indio_dev);
1376         cancel_delayed_work_sync(&xadc->zynq_unmask_work);
1377         clk_disable_unprepare(xadc->clk);
1378         kfree(xadc->data);
1379         kfree(indio_dev->channels);
1380
1381         return 0;
1382 }
1383
1384 static struct platform_driver xadc_driver = {
1385         .probe = xadc_probe,
1386         .remove = xadc_remove,
1387         .driver = {
1388                 .name = "xadc",
1389                 .of_match_table = xadc_of_match_table,
1390         },
1391 };
1392 module_platform_driver(xadc_driver);
1393
1394 MODULE_LICENSE("GPL v2");
1395 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1396 MODULE_DESCRIPTION("Xilinx XADC IIO driver");