Merge 3.17-rc6 into staging-next.
[linux/fpc-iii.git] / drivers / iio / adc / xilinx-xadc-core.c
bloba221f7329b7914449492f7a287f6524d4770a4c3
1 /*
2 * Xilinx XADC driver
4 * Copyright 2013-2014 Analog Devices Inc.
5 * Author: Lars-Peter Clauen <lars@metafoo.de>
7 * Licensed under the GPL-2.
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
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>
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>
35 #include "xilinx-xadc.h"
37 static const unsigned int XADC_ZYNQ_UNMASK_TIMEOUT = 500;
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
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)
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
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)
79 #define XADC_ZYNQ_CTL_RESET BIT(4)
81 #define XADC_ZYNQ_CMD_NOP 0x00
82 #define XADC_ZYNQ_CMD_READ 0x01
83 #define XADC_ZYNQ_CMD_WRITE 0x02
85 #define XADC_ZYNQ_CMD(cmd, addr, data) (((cmd) << 26) | ((addr) << 16) | (data))
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
98 #define XADC_AXI_RESET_MAGIC 0xa
99 #define XADC_AXI_GIER_ENABLE BIT(31)
101 #define XADC_AXI_INT_EOS BIT(4)
102 #define XADC_AXI_INT_ALARM_MASK 0x3c0f
104 #define XADC_FLAGS_BUFFERED BIT(0)
106 static void xadc_write_reg(struct xadc *xadc, unsigned int reg,
107 uint32_t val)
109 writel(val, xadc->base + reg);
112 static void xadc_read_reg(struct xadc *xadc, unsigned int reg,
113 uint32_t *val)
115 *val = readl(xadc->base + reg);
119 * The ZYNQ interface uses two asynchronous FIFOs for communication with the
120 * XADC. Reads and writes to the XADC register are performed by submitting a
121 * request to the command FIFO (CFIFO), once the request has been completed the
122 * result can be read from the data FIFO (DFIFO). The method currently used in
123 * this driver is to submit the request for a read/write operation, then go to
124 * sleep and wait for an interrupt that signals that a response is available in
125 * the data FIFO.
128 static void xadc_zynq_write_fifo(struct xadc *xadc, uint32_t *cmd,
129 unsigned int n)
131 unsigned int i;
133 for (i = 0; i < n; i++)
134 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFIFO, cmd[i]);
137 static void xadc_zynq_drain_fifo(struct xadc *xadc)
139 uint32_t status, tmp;
141 xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
143 while (!(status & XADC_ZYNQ_STATUS_DFIFOE)) {
144 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
145 xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
149 static void xadc_zynq_update_intmsk(struct xadc *xadc, unsigned int mask,
150 unsigned int val)
152 xadc->zynq_intmask &= ~mask;
153 xadc->zynq_intmask |= val;
155 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK,
156 xadc->zynq_intmask | xadc->zynq_masked_alarm);
159 static int xadc_zynq_write_adc_reg(struct xadc *xadc, unsigned int reg,
160 uint16_t val)
162 uint32_t cmd[1];
163 uint32_t tmp;
164 int ret;
166 spin_lock_irq(&xadc->lock);
167 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
168 XADC_ZYNQ_INT_DFIFO_GTH);
170 reinit_completion(&xadc->completion);
172 cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_WRITE, reg, val);
173 xadc_zynq_write_fifo(xadc, cmd, ARRAY_SIZE(cmd));
174 xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &tmp);
175 tmp &= ~XADC_ZYNQ_CFG_DFIFOTH_MASK;
176 tmp |= 0 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET;
177 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, tmp);
179 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
180 spin_unlock_irq(&xadc->lock);
182 ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
183 if (ret == 0)
184 ret = -EIO;
185 else
186 ret = 0;
188 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
190 return ret;
193 static int xadc_zynq_read_adc_reg(struct xadc *xadc, unsigned int reg,
194 uint16_t *val)
196 uint32_t cmd[2];
197 uint32_t resp, tmp;
198 int ret;
200 cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_READ, reg, 0);
201 cmd[1] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_NOP, 0, 0);
203 spin_lock_irq(&xadc->lock);
204 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
205 XADC_ZYNQ_INT_DFIFO_GTH);
206 xadc_zynq_drain_fifo(xadc);
207 reinit_completion(&xadc->completion);
209 xadc_zynq_write_fifo(xadc, cmd, ARRAY_SIZE(cmd));
210 xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &tmp);
211 tmp &= ~XADC_ZYNQ_CFG_DFIFOTH_MASK;
212 tmp |= 1 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET;
213 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, tmp);
215 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
216 spin_unlock_irq(&xadc->lock);
217 ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
218 if (ret == 0)
219 ret = -EIO;
220 if (ret < 0)
221 return ret;
223 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
224 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
226 *val = resp & 0xffff;
228 return 0;
231 static unsigned int xadc_zynq_transform_alarm(unsigned int alarm)
233 return ((alarm & 0x80) >> 4) |
234 ((alarm & 0x78) << 1) |
235 (alarm & 0x07);
239 * The ZYNQ threshold interrupts are level sensitive. Since we can't make the
240 * threshold condition go way from within the interrupt handler, this means as
241 * soon as a threshold condition is present we would enter the interrupt handler
242 * again and again. To work around this we mask all active thresholds interrupts
243 * in the interrupt handler and start a timer. In this timer we poll the
244 * interrupt status and only if the interrupt is inactive we unmask it again.
246 static void xadc_zynq_unmask_worker(struct work_struct *work)
248 struct xadc *xadc = container_of(work, struct xadc, zynq_unmask_work.work);
249 unsigned int misc_sts, unmask;
251 xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &misc_sts);
253 misc_sts &= XADC_ZYNQ_INT_ALARM_MASK;
255 spin_lock_irq(&xadc->lock);
257 /* Clear those bits which are not active anymore */
258 unmask = (xadc->zynq_masked_alarm ^ misc_sts) & xadc->zynq_masked_alarm;
259 xadc->zynq_masked_alarm &= misc_sts;
261 /* Also clear those which are masked out anyway */
262 xadc->zynq_masked_alarm &= ~xadc->zynq_intmask;
264 /* Clear the interrupts before we unmask them */
265 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, unmask);
267 xadc_zynq_update_intmsk(xadc, 0, 0);
269 spin_unlock_irq(&xadc->lock);
271 /* if still pending some alarm re-trigger the timer */
272 if (xadc->zynq_masked_alarm) {
273 schedule_delayed_work(&xadc->zynq_unmask_work,
274 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
278 static irqreturn_t xadc_zynq_threaded_interrupt_handler(int irq, void *devid)
280 struct iio_dev *indio_dev = devid;
281 struct xadc *xadc = iio_priv(indio_dev);
282 unsigned int alarm;
284 spin_lock_irq(&xadc->lock);
285 alarm = xadc->zynq_alarm;
286 xadc->zynq_alarm = 0;
287 spin_unlock_irq(&xadc->lock);
289 xadc_handle_events(indio_dev, xadc_zynq_transform_alarm(alarm));
291 /* unmask the required interrupts in timer. */
292 schedule_delayed_work(&xadc->zynq_unmask_work,
293 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
295 return IRQ_HANDLED;
298 static irqreturn_t xadc_zynq_interrupt_handler(int irq, void *devid)
300 struct iio_dev *indio_dev = devid;
301 struct xadc *xadc = iio_priv(indio_dev);
302 irqreturn_t ret = IRQ_HANDLED;
303 uint32_t status;
305 xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
307 status &= ~(xadc->zynq_intmask | xadc->zynq_masked_alarm);
309 if (!status)
310 return IRQ_NONE;
312 spin_lock(&xadc->lock);
314 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status);
316 if (status & XADC_ZYNQ_INT_DFIFO_GTH) {
317 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
318 XADC_ZYNQ_INT_DFIFO_GTH);
319 complete(&xadc->completion);
322 status &= XADC_ZYNQ_INT_ALARM_MASK;
323 if (status) {
324 xadc->zynq_alarm |= status;
325 xadc->zynq_masked_alarm |= status;
327 * mask the current event interrupt,
328 * unmask it when the interrupt is no more active.
330 xadc_zynq_update_intmsk(xadc, 0, 0);
331 ret = IRQ_WAKE_THREAD;
333 spin_unlock(&xadc->lock);
335 return ret;
338 #define XADC_ZYNQ_TCK_RATE_MAX 50000000
339 #define XADC_ZYNQ_IGAP_DEFAULT 20
341 static int xadc_zynq_setup(struct platform_device *pdev,
342 struct iio_dev *indio_dev, int irq)
344 struct xadc *xadc = iio_priv(indio_dev);
345 unsigned long pcap_rate;
346 unsigned int tck_div;
347 unsigned int div;
348 unsigned int igap;
349 unsigned int tck_rate;
351 /* TODO: Figure out how to make igap and tck_rate configurable */
352 igap = XADC_ZYNQ_IGAP_DEFAULT;
353 tck_rate = XADC_ZYNQ_TCK_RATE_MAX;
355 xadc->zynq_intmask = ~0;
357 pcap_rate = clk_get_rate(xadc->clk);
359 if (tck_rate > XADC_ZYNQ_TCK_RATE_MAX)
360 tck_rate = XADC_ZYNQ_TCK_RATE_MAX;
361 if (tck_rate > pcap_rate / 2) {
362 div = 2;
363 } else {
364 div = pcap_rate / tck_rate;
365 if (pcap_rate / div > XADC_ZYNQ_TCK_RATE_MAX)
366 div++;
369 if (div <= 3)
370 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV2;
371 else if (div <= 7)
372 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV4;
373 else if (div <= 15)
374 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV8;
375 else
376 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV16;
378 xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, XADC_ZYNQ_CTL_RESET);
379 xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, 0);
380 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, ~0);
381 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK, xadc->zynq_intmask);
382 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, XADC_ZYNQ_CFG_ENABLE |
383 XADC_ZYNQ_CFG_REDGE | XADC_ZYNQ_CFG_WEDGE |
384 tck_div | XADC_ZYNQ_CFG_IGAP(igap));
386 return 0;
389 static unsigned long xadc_zynq_get_dclk_rate(struct xadc *xadc)
391 unsigned int div;
392 uint32_t val;
394 xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &val);
396 switch (val & XADC_ZYNQ_CFG_TCKRATE_MASK) {
397 case XADC_ZYNQ_CFG_TCKRATE_DIV4:
398 div = 4;
399 break;
400 case XADC_ZYNQ_CFG_TCKRATE_DIV8:
401 div = 8;
402 break;
403 case XADC_ZYNQ_CFG_TCKRATE_DIV16:
404 div = 16;
405 break;
406 default:
407 div = 2;
408 break;
411 return clk_get_rate(xadc->clk) / div;
414 static void xadc_zynq_update_alarm(struct xadc *xadc, unsigned int alarm)
416 unsigned long flags;
417 uint32_t status;
419 /* Move OT to bit 7 */
420 alarm = ((alarm & 0x08) << 4) | ((alarm & 0xf0) >> 1) | (alarm & 0x07);
422 spin_lock_irqsave(&xadc->lock, flags);
424 /* Clear previous interrupts if any. */
425 xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
426 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status & alarm);
428 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_ALARM_MASK,
429 ~alarm & XADC_ZYNQ_INT_ALARM_MASK);
431 spin_unlock_irqrestore(&xadc->lock, flags);
434 static const struct xadc_ops xadc_zynq_ops = {
435 .read = xadc_zynq_read_adc_reg,
436 .write = xadc_zynq_write_adc_reg,
437 .setup = xadc_zynq_setup,
438 .get_dclk_rate = xadc_zynq_get_dclk_rate,
439 .interrupt_handler = xadc_zynq_interrupt_handler,
440 .threaded_interrupt_handler = xadc_zynq_threaded_interrupt_handler,
441 .update_alarm = xadc_zynq_update_alarm,
444 static int xadc_axi_read_adc_reg(struct xadc *xadc, unsigned int reg,
445 uint16_t *val)
447 uint32_t val32;
449 xadc_read_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, &val32);
450 *val = val32 & 0xffff;
452 return 0;
455 static int xadc_axi_write_adc_reg(struct xadc *xadc, unsigned int reg,
456 uint16_t val)
458 xadc_write_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, val);
460 return 0;
463 static int xadc_axi_setup(struct platform_device *pdev,
464 struct iio_dev *indio_dev, int irq)
466 struct xadc *xadc = iio_priv(indio_dev);
468 xadc_write_reg(xadc, XADC_AXI_REG_RESET, XADC_AXI_RESET_MAGIC);
469 xadc_write_reg(xadc, XADC_AXI_REG_GIER, XADC_AXI_GIER_ENABLE);
471 return 0;
474 static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid)
476 struct iio_dev *indio_dev = devid;
477 struct xadc *xadc = iio_priv(indio_dev);
478 uint32_t status, mask;
479 unsigned int events;
481 xadc_read_reg(xadc, XADC_AXI_REG_IPISR, &status);
482 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &mask);
483 status &= mask;
485 if (!status)
486 return IRQ_NONE;
488 if ((status & XADC_AXI_INT_EOS) && xadc->trigger)
489 iio_trigger_poll(xadc->trigger);
491 if (status & XADC_AXI_INT_ALARM_MASK) {
493 * The order of the bits in the AXI-XADC status register does
494 * not match the order of the bits in the XADC alarm enable
495 * register. xadc_handle_events() expects the events to be in
496 * the same order as the XADC alarm enable register.
498 events = (status & 0x000e) >> 1;
499 events |= (status & 0x0001) << 3;
500 events |= (status & 0x3c00) >> 6;
501 xadc_handle_events(indio_dev, events);
504 xadc_write_reg(xadc, XADC_AXI_REG_IPISR, status);
506 return IRQ_HANDLED;
509 static void xadc_axi_update_alarm(struct xadc *xadc, unsigned int alarm)
511 uint32_t val;
512 unsigned long flags;
515 * The order of the bits in the AXI-XADC status register does not match
516 * the order of the bits in the XADC alarm enable register. We get
517 * passed the alarm mask in the same order as in the XADC alarm enable
518 * register.
520 alarm = ((alarm & 0x07) << 1) | ((alarm & 0x08) >> 3) |
521 ((alarm & 0xf0) << 6);
523 spin_lock_irqsave(&xadc->lock, flags);
524 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
525 val &= ~XADC_AXI_INT_ALARM_MASK;
526 val |= alarm;
527 xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
528 spin_unlock_irqrestore(&xadc->lock, flags);
531 static unsigned long xadc_axi_get_dclk(struct xadc *xadc)
533 return clk_get_rate(xadc->clk);
536 static const struct xadc_ops xadc_axi_ops = {
537 .read = xadc_axi_read_adc_reg,
538 .write = xadc_axi_write_adc_reg,
539 .setup = xadc_axi_setup,
540 .get_dclk_rate = xadc_axi_get_dclk,
541 .update_alarm = xadc_axi_update_alarm,
542 .interrupt_handler = xadc_axi_interrupt_handler,
543 .flags = XADC_FLAGS_BUFFERED,
546 static int _xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
547 uint16_t mask, uint16_t val)
549 uint16_t tmp;
550 int ret;
552 ret = _xadc_read_adc_reg(xadc, reg, &tmp);
553 if (ret)
554 return ret;
556 return _xadc_write_adc_reg(xadc, reg, (tmp & ~mask) | val);
559 static int xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
560 uint16_t mask, uint16_t val)
562 int ret;
564 mutex_lock(&xadc->mutex);
565 ret = _xadc_update_adc_reg(xadc, reg, mask, val);
566 mutex_unlock(&xadc->mutex);
568 return ret;
571 static unsigned long xadc_get_dclk_rate(struct xadc *xadc)
573 return xadc->ops->get_dclk_rate(xadc);
576 static int xadc_update_scan_mode(struct iio_dev *indio_dev,
577 const unsigned long *mask)
579 struct xadc *xadc = iio_priv(indio_dev);
580 unsigned int n;
582 n = bitmap_weight(mask, indio_dev->masklength);
584 kfree(xadc->data);
585 xadc->data = kcalloc(n, sizeof(*xadc->data), GFP_KERNEL);
586 if (!xadc->data)
587 return -ENOMEM;
589 return 0;
592 static unsigned int xadc_scan_index_to_channel(unsigned int scan_index)
594 switch (scan_index) {
595 case 5:
596 return XADC_REG_VCCPINT;
597 case 6:
598 return XADC_REG_VCCPAUX;
599 case 7:
600 return XADC_REG_VCCO_DDR;
601 case 8:
602 return XADC_REG_TEMP;
603 case 9:
604 return XADC_REG_VCCINT;
605 case 10:
606 return XADC_REG_VCCAUX;
607 case 11:
608 return XADC_REG_VPVN;
609 case 12:
610 return XADC_REG_VREFP;
611 case 13:
612 return XADC_REG_VREFN;
613 case 14:
614 return XADC_REG_VCCBRAM;
615 default:
616 return XADC_REG_VAUX(scan_index - 16);
620 static irqreturn_t xadc_trigger_handler(int irq, void *p)
622 struct iio_poll_func *pf = p;
623 struct iio_dev *indio_dev = pf->indio_dev;
624 struct xadc *xadc = iio_priv(indio_dev);
625 unsigned int chan;
626 int i, j;
628 if (!xadc->data)
629 goto out;
631 j = 0;
632 for_each_set_bit(i, indio_dev->active_scan_mask,
633 indio_dev->masklength) {
634 chan = xadc_scan_index_to_channel(i);
635 xadc_read_adc_reg(xadc, chan, &xadc->data[j]);
636 j++;
639 iio_push_to_buffers(indio_dev, xadc->data);
641 out:
642 iio_trigger_notify_done(indio_dev->trig);
644 return IRQ_HANDLED;
647 static int xadc_trigger_set_state(struct iio_trigger *trigger, bool state)
649 struct xadc *xadc = iio_trigger_get_drvdata(trigger);
650 unsigned long flags;
651 unsigned int convst;
652 unsigned int val;
653 int ret = 0;
655 mutex_lock(&xadc->mutex);
657 if (state) {
658 /* Only one of the two triggers can be active at the a time. */
659 if (xadc->trigger != NULL) {
660 ret = -EBUSY;
661 goto err_out;
662 } else {
663 xadc->trigger = trigger;
664 if (trigger == xadc->convst_trigger)
665 convst = XADC_CONF0_EC;
666 else
667 convst = 0;
669 ret = _xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF0_EC,
670 convst);
671 if (ret)
672 goto err_out;
673 } else {
674 xadc->trigger = NULL;
677 spin_lock_irqsave(&xadc->lock, flags);
678 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
679 xadc_write_reg(xadc, XADC_AXI_REG_IPISR, val & XADC_AXI_INT_EOS);
680 if (state)
681 val |= XADC_AXI_INT_EOS;
682 else
683 val &= ~XADC_AXI_INT_EOS;
684 xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
685 spin_unlock_irqrestore(&xadc->lock, flags);
687 err_out:
688 mutex_unlock(&xadc->mutex);
690 return ret;
693 static const struct iio_trigger_ops xadc_trigger_ops = {
694 .owner = THIS_MODULE,
695 .set_trigger_state = &xadc_trigger_set_state,
698 static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev,
699 const char *name)
701 struct iio_trigger *trig;
702 int ret;
704 trig = iio_trigger_alloc("%s%d-%s", indio_dev->name,
705 indio_dev->id, name);
706 if (trig == NULL)
707 return ERR_PTR(-ENOMEM);
709 trig->dev.parent = indio_dev->dev.parent;
710 trig->ops = &xadc_trigger_ops;
711 iio_trigger_set_drvdata(trig, iio_priv(indio_dev));
713 ret = iio_trigger_register(trig);
714 if (ret)
715 goto error_free_trig;
717 return trig;
719 error_free_trig:
720 iio_trigger_free(trig);
721 return ERR_PTR(ret);
724 static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode)
726 uint16_t val;
728 switch (seq_mode) {
729 case XADC_CONF1_SEQ_SIMULTANEOUS:
730 case XADC_CONF1_SEQ_INDEPENDENT:
731 val = XADC_CONF2_PD_ADC_B;
732 break;
733 default:
734 val = 0;
735 break;
738 return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_PD_MASK,
739 val);
742 static int xadc_get_seq_mode(struct xadc *xadc, unsigned long scan_mode)
744 unsigned int aux_scan_mode = scan_mode >> 16;
746 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_DUAL)
747 return XADC_CONF1_SEQ_SIMULTANEOUS;
749 if ((aux_scan_mode & 0xff00) == 0 ||
750 (aux_scan_mode & 0x00ff) == 0)
751 return XADC_CONF1_SEQ_CONTINUOUS;
753 return XADC_CONF1_SEQ_SIMULTANEOUS;
756 static int xadc_postdisable(struct iio_dev *indio_dev)
758 struct xadc *xadc = iio_priv(indio_dev);
759 unsigned long scan_mask;
760 int ret;
761 int i;
763 scan_mask = 1; /* Run calibration as part of the sequence */
764 for (i = 0; i < indio_dev->num_channels; i++)
765 scan_mask |= BIT(indio_dev->channels[i].scan_index);
767 /* Enable all channels and calibration */
768 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
769 if (ret)
770 return ret;
772 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
773 if (ret)
774 return ret;
776 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
777 XADC_CONF1_SEQ_CONTINUOUS);
778 if (ret)
779 return ret;
781 return xadc_power_adc_b(xadc, XADC_CONF1_SEQ_CONTINUOUS);
784 static int xadc_preenable(struct iio_dev *indio_dev)
786 struct xadc *xadc = iio_priv(indio_dev);
787 unsigned long scan_mask;
788 int seq_mode;
789 int ret;
791 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
792 XADC_CONF1_SEQ_DEFAULT);
793 if (ret)
794 goto err;
796 scan_mask = *indio_dev->active_scan_mask;
797 seq_mode = xadc_get_seq_mode(xadc, scan_mask);
799 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
800 if (ret)
801 goto err;
803 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
804 if (ret)
805 goto err;
807 ret = xadc_power_adc_b(xadc, seq_mode);
808 if (ret)
809 goto err;
811 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
812 seq_mode);
813 if (ret)
814 goto err;
816 return 0;
817 err:
818 xadc_postdisable(indio_dev);
819 return ret;
822 static struct iio_buffer_setup_ops xadc_buffer_ops = {
823 .preenable = &xadc_preenable,
824 .postenable = &iio_triggered_buffer_postenable,
825 .predisable = &iio_triggered_buffer_predisable,
826 .postdisable = &xadc_postdisable,
829 static int xadc_read_raw(struct iio_dev *indio_dev,
830 struct iio_chan_spec const *chan, int *val, int *val2, long info)
832 struct xadc *xadc = iio_priv(indio_dev);
833 unsigned int div;
834 uint16_t val16;
835 int ret;
837 switch (info) {
838 case IIO_CHAN_INFO_RAW:
839 if (iio_buffer_enabled(indio_dev))
840 return -EBUSY;
841 ret = xadc_read_adc_reg(xadc, chan->address, &val16);
842 if (ret < 0)
843 return ret;
845 val16 >>= 4;
846 if (chan->scan_type.sign == 'u')
847 *val = val16;
848 else
849 *val = sign_extend32(val16, 11);
851 return IIO_VAL_INT;
852 case IIO_CHAN_INFO_SCALE:
853 switch (chan->type) {
854 case IIO_VOLTAGE:
855 /* V = (val * 3.0) / 4096 */
856 switch (chan->address) {
857 case XADC_REG_VCCINT:
858 case XADC_REG_VCCAUX:
859 case XADC_REG_VCCBRAM:
860 case XADC_REG_VCCPINT:
861 case XADC_REG_VCCPAUX:
862 case XADC_REG_VCCO_DDR:
863 *val = 3000;
864 break;
865 default:
866 *val = 1000;
867 break;
869 *val2 = 12;
870 return IIO_VAL_FRACTIONAL_LOG2;
871 case IIO_TEMP:
872 /* Temp in C = (val * 503.975) / 4096 - 273.15 */
873 *val = 503975;
874 *val2 = 12;
875 return IIO_VAL_FRACTIONAL_LOG2;
876 default:
877 return -EINVAL;
879 case IIO_CHAN_INFO_OFFSET:
880 /* Only the temperature channel has an offset */
881 *val = -((273150 << 12) / 503975);
882 return IIO_VAL_INT;
883 case IIO_CHAN_INFO_SAMP_FREQ:
884 ret = xadc_read_adc_reg(xadc, XADC_REG_CONF2, &val16);
885 if (ret)
886 return ret;
888 div = (val16 & XADC_CONF2_DIV_MASK) >> XADC_CONF2_DIV_OFFSET;
889 if (div < 2)
890 div = 2;
892 *val = xadc_get_dclk_rate(xadc) / div / 26;
894 return IIO_VAL_INT;
895 default:
896 return -EINVAL;
900 static int xadc_write_raw(struct iio_dev *indio_dev,
901 struct iio_chan_spec const *chan, int val, int val2, long info)
903 struct xadc *xadc = iio_priv(indio_dev);
904 unsigned long clk_rate = xadc_get_dclk_rate(xadc);
905 unsigned int div;
907 if (info != IIO_CHAN_INFO_SAMP_FREQ)
908 return -EINVAL;
910 if (val <= 0)
911 return -EINVAL;
913 /* Max. 150 kSPS */
914 if (val > 150000)
915 val = 150000;
917 val *= 26;
919 /* Min 1MHz */
920 if (val < 1000000)
921 val = 1000000;
924 * We want to round down, but only if we do not exceed the 150 kSPS
925 * limit.
927 div = clk_rate / val;
928 if (clk_rate / div / 26 > 150000)
929 div++;
930 if (div < 2)
931 div = 2;
932 else if (div > 0xff)
933 div = 0xff;
935 return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_DIV_MASK,
936 div << XADC_CONF2_DIV_OFFSET);
939 static const struct iio_event_spec xadc_temp_events[] = {
941 .type = IIO_EV_TYPE_THRESH,
942 .dir = IIO_EV_DIR_RISING,
943 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
944 BIT(IIO_EV_INFO_VALUE) |
945 BIT(IIO_EV_INFO_HYSTERESIS),
949 /* Separate values for upper and lower thresholds, but only a shared enabled */
950 static const struct iio_event_spec xadc_voltage_events[] = {
952 .type = IIO_EV_TYPE_THRESH,
953 .dir = IIO_EV_DIR_RISING,
954 .mask_separate = BIT(IIO_EV_INFO_VALUE),
955 }, {
956 .type = IIO_EV_TYPE_THRESH,
957 .dir = IIO_EV_DIR_FALLING,
958 .mask_separate = BIT(IIO_EV_INFO_VALUE),
959 }, {
960 .type = IIO_EV_TYPE_THRESH,
961 .dir = IIO_EV_DIR_EITHER,
962 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
966 #define XADC_CHAN_TEMP(_chan, _scan_index, _addr) { \
967 .type = IIO_TEMP, \
968 .indexed = 1, \
969 .channel = (_chan), \
970 .address = (_addr), \
971 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
972 BIT(IIO_CHAN_INFO_SCALE) | \
973 BIT(IIO_CHAN_INFO_OFFSET), \
974 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
975 .event_spec = xadc_temp_events, \
976 .num_event_specs = ARRAY_SIZE(xadc_temp_events), \
977 .scan_index = (_scan_index), \
978 .scan_type = { \
979 .sign = 'u', \
980 .realbits = 12, \
981 .storagebits = 16, \
982 .shift = 4, \
983 .endianness = IIO_CPU, \
984 }, \
987 #define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) { \
988 .type = IIO_VOLTAGE, \
989 .indexed = 1, \
990 .channel = (_chan), \
991 .address = (_addr), \
992 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
993 BIT(IIO_CHAN_INFO_SCALE), \
994 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
995 .event_spec = (_alarm) ? xadc_voltage_events : NULL, \
996 .num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \
997 .scan_index = (_scan_index), \
998 .scan_type = { \
999 .sign = 'u', \
1000 .realbits = 12, \
1001 .storagebits = 16, \
1002 .shift = 4, \
1003 .endianness = IIO_CPU, \
1004 }, \
1005 .extend_name = _ext, \
1008 static const struct iio_chan_spec xadc_channels[] = {
1009 XADC_CHAN_TEMP(0, 8, XADC_REG_TEMP),
1010 XADC_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true),
1011 XADC_CHAN_VOLTAGE(1, 10, XADC_REG_VCCINT, "vccaux", true),
1012 XADC_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true),
1013 XADC_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpint", true),
1014 XADC_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpaux", true),
1015 XADC_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccoddr", true),
1016 XADC_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false),
1017 XADC_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false),
1018 XADC_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false),
1019 XADC_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false),
1020 XADC_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false),
1021 XADC_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false),
1022 XADC_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false),
1023 XADC_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false),
1024 XADC_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false),
1025 XADC_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false),
1026 XADC_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false),
1027 XADC_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false),
1028 XADC_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false),
1029 XADC_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false),
1030 XADC_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false),
1031 XADC_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false),
1032 XADC_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false),
1033 XADC_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false),
1034 XADC_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false),
1037 static const struct iio_info xadc_info = {
1038 .read_raw = &xadc_read_raw,
1039 .write_raw = &xadc_write_raw,
1040 .read_event_config = &xadc_read_event_config,
1041 .write_event_config = &xadc_write_event_config,
1042 .read_event_value = &xadc_read_event_value,
1043 .write_event_value = &xadc_write_event_value,
1044 .update_scan_mode = &xadc_update_scan_mode,
1045 .driver_module = THIS_MODULE,
1048 static const struct of_device_id xadc_of_match_table[] = {
1049 { .compatible = "xlnx,zynq-xadc-1.00.a", (void *)&xadc_zynq_ops },
1050 { .compatible = "xlnx,axi-xadc-1.00.a", (void *)&xadc_axi_ops },
1051 { },
1053 MODULE_DEVICE_TABLE(of, xadc_of_match_table);
1055 static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np,
1056 unsigned int *conf)
1058 struct xadc *xadc = iio_priv(indio_dev);
1059 struct iio_chan_spec *channels, *chan;
1060 struct device_node *chan_node, *child;
1061 unsigned int num_channels;
1062 const char *external_mux;
1063 u32 ext_mux_chan;
1064 int reg;
1065 int ret;
1067 *conf = 0;
1069 ret = of_property_read_string(np, "xlnx,external-mux", &external_mux);
1070 if (ret < 0 || strcasecmp(external_mux, "none") == 0)
1071 xadc->external_mux_mode = XADC_EXTERNAL_MUX_NONE;
1072 else if (strcasecmp(external_mux, "single") == 0)
1073 xadc->external_mux_mode = XADC_EXTERNAL_MUX_SINGLE;
1074 else if (strcasecmp(external_mux, "dual") == 0)
1075 xadc->external_mux_mode = XADC_EXTERNAL_MUX_DUAL;
1076 else
1077 return -EINVAL;
1079 if (xadc->external_mux_mode != XADC_EXTERNAL_MUX_NONE) {
1080 ret = of_property_read_u32(np, "xlnx,external-mux-channel",
1081 &ext_mux_chan);
1082 if (ret < 0)
1083 return ret;
1085 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_SINGLE) {
1086 if (ext_mux_chan == 0)
1087 ext_mux_chan = XADC_REG_VPVN;
1088 else if (ext_mux_chan <= 16)
1089 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1090 else
1091 return -EINVAL;
1092 } else {
1093 if (ext_mux_chan > 0 && ext_mux_chan <= 8)
1094 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1095 else
1096 return -EINVAL;
1099 *conf |= XADC_CONF0_MUX | XADC_CONF0_CHAN(ext_mux_chan);
1102 channels = kmemdup(xadc_channels, sizeof(xadc_channels), GFP_KERNEL);
1103 if (!channels)
1104 return -ENOMEM;
1106 num_channels = 9;
1107 chan = &channels[9];
1109 chan_node = of_get_child_by_name(np, "xlnx,channels");
1110 if (chan_node) {
1111 for_each_child_of_node(chan_node, child) {
1112 if (num_channels >= ARRAY_SIZE(xadc_channels)) {
1113 of_node_put(child);
1114 break;
1117 ret = of_property_read_u32(child, "reg", &reg);
1118 if (ret || reg > 16)
1119 continue;
1121 if (of_property_read_bool(child, "xlnx,bipolar"))
1122 chan->scan_type.sign = 's';
1124 if (reg == 0) {
1125 chan->scan_index = 11;
1126 chan->address = XADC_REG_VPVN;
1127 } else {
1128 chan->scan_index = 15 + reg;
1129 chan->address = XADC_REG_VAUX(reg - 1);
1131 num_channels++;
1132 chan++;
1135 of_node_put(chan_node);
1137 indio_dev->num_channels = num_channels;
1138 indio_dev->channels = krealloc(channels, sizeof(*channels) *
1139 num_channels, GFP_KERNEL);
1140 /* If we can't resize the channels array, just use the original */
1141 if (!indio_dev->channels)
1142 indio_dev->channels = channels;
1144 return 0;
1147 static int xadc_probe(struct platform_device *pdev)
1149 const struct of_device_id *id;
1150 struct iio_dev *indio_dev;
1151 unsigned int bipolar_mask;
1152 struct resource *mem;
1153 unsigned int conf0;
1154 struct xadc *xadc;
1155 int ret;
1156 int irq;
1157 int i;
1159 if (!pdev->dev.of_node)
1160 return -ENODEV;
1162 id = of_match_node(xadc_of_match_table, pdev->dev.of_node);
1163 if (!id)
1164 return -EINVAL;
1166 irq = platform_get_irq(pdev, 0);
1167 if (irq <= 0)
1168 return -ENXIO;
1170 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*xadc));
1171 if (!indio_dev)
1172 return -ENOMEM;
1174 xadc = iio_priv(indio_dev);
1175 xadc->ops = id->data;
1176 init_completion(&xadc->completion);
1177 mutex_init(&xadc->mutex);
1178 spin_lock_init(&xadc->lock);
1179 INIT_DELAYED_WORK(&xadc->zynq_unmask_work, xadc_zynq_unmask_worker);
1181 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1182 xadc->base = devm_ioremap_resource(&pdev->dev, mem);
1183 if (IS_ERR(xadc->base))
1184 return PTR_ERR(xadc->base);
1186 indio_dev->dev.parent = &pdev->dev;
1187 indio_dev->dev.of_node = pdev->dev.of_node;
1188 indio_dev->name = "xadc";
1189 indio_dev->modes = INDIO_DIRECT_MODE;
1190 indio_dev->info = &xadc_info;
1192 ret = xadc_parse_dt(indio_dev, pdev->dev.of_node, &conf0);
1193 if (ret)
1194 goto err_device_free;
1196 if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1197 ret = iio_triggered_buffer_setup(indio_dev,
1198 &iio_pollfunc_store_time, &xadc_trigger_handler,
1199 &xadc_buffer_ops);
1200 if (ret)
1201 goto err_device_free;
1203 xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst");
1204 if (IS_ERR(xadc->convst_trigger)) {
1205 ret = PTR_ERR(xadc->convst_trigger);
1206 goto err_triggered_buffer_cleanup;
1208 xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev,
1209 "samplerate");
1210 if (IS_ERR(xadc->samplerate_trigger)) {
1211 ret = PTR_ERR(xadc->samplerate_trigger);
1212 goto err_free_convst_trigger;
1216 xadc->clk = devm_clk_get(&pdev->dev, NULL);
1217 if (IS_ERR(xadc->clk)) {
1218 ret = PTR_ERR(xadc->clk);
1219 goto err_free_samplerate_trigger;
1221 clk_prepare_enable(xadc->clk);
1223 ret = xadc->ops->setup(pdev, indio_dev, irq);
1224 if (ret)
1225 goto err_free_samplerate_trigger;
1227 ret = request_threaded_irq(irq, xadc->ops->interrupt_handler,
1228 xadc->ops->threaded_interrupt_handler,
1229 0, dev_name(&pdev->dev), indio_dev);
1230 if (ret)
1231 goto err_clk_disable_unprepare;
1233 for (i = 0; i < 16; i++)
1234 xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1235 &xadc->threshold[i]);
1237 ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0);
1238 if (ret)
1239 goto err_free_irq;
1241 bipolar_mask = 0;
1242 for (i = 0; i < indio_dev->num_channels; i++) {
1243 if (indio_dev->channels[i].scan_type.sign == 's')
1244 bipolar_mask |= BIT(indio_dev->channels[i].scan_index);
1247 ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask);
1248 if (ret)
1249 goto err_free_irq;
1250 ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1),
1251 bipolar_mask >> 16);
1252 if (ret)
1253 goto err_free_irq;
1255 /* Disable all alarms */
1256 xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_ALARM_MASK,
1257 XADC_CONF1_ALARM_MASK);
1259 /* Set thresholds to min/max */
1260 for (i = 0; i < 16; i++) {
1262 * Set max voltage threshold and both temperature thresholds to
1263 * 0xffff, min voltage threshold to 0.
1265 if (i % 8 < 4 || i == 7)
1266 xadc->threshold[i] = 0xffff;
1267 else
1268 xadc->threshold[i] = 0;
1269 xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1270 xadc->threshold[i]);
1273 /* Go to non-buffered mode */
1274 xadc_postdisable(indio_dev);
1276 ret = iio_device_register(indio_dev);
1277 if (ret)
1278 goto err_free_irq;
1280 platform_set_drvdata(pdev, indio_dev);
1282 return 0;
1284 err_free_irq:
1285 free_irq(irq, indio_dev);
1286 err_free_samplerate_trigger:
1287 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1288 iio_trigger_free(xadc->samplerate_trigger);
1289 err_free_convst_trigger:
1290 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1291 iio_trigger_free(xadc->convst_trigger);
1292 err_triggered_buffer_cleanup:
1293 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1294 iio_triggered_buffer_cleanup(indio_dev);
1295 err_clk_disable_unprepare:
1296 clk_disable_unprepare(xadc->clk);
1297 err_device_free:
1298 kfree(indio_dev->channels);
1300 return ret;
1303 static int xadc_remove(struct platform_device *pdev)
1305 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1306 struct xadc *xadc = iio_priv(indio_dev);
1307 int irq = platform_get_irq(pdev, 0);
1309 iio_device_unregister(indio_dev);
1310 if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1311 iio_trigger_free(xadc->samplerate_trigger);
1312 iio_trigger_free(xadc->convst_trigger);
1313 iio_triggered_buffer_cleanup(indio_dev);
1315 free_irq(irq, indio_dev);
1316 clk_disable_unprepare(xadc->clk);
1317 cancel_delayed_work(&xadc->zynq_unmask_work);
1318 kfree(xadc->data);
1319 kfree(indio_dev->channels);
1321 return 0;
1324 static struct platform_driver xadc_driver = {
1325 .probe = xadc_probe,
1326 .remove = xadc_remove,
1327 .driver = {
1328 .name = "xadc",
1329 .of_match_table = xadc_of_match_table,
1332 module_platform_driver(xadc_driver);
1334 MODULE_LICENSE("GPL v2");
1335 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1336 MODULE_DESCRIPTION("Xilinx XADC IIO driver");