1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2013-2014 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
8 * Documentation for the parts can be found at:
9 * - XADC hardmacro: Xilinx UG480
10 * - ZYNQ XADC interface: Xilinx UG585
11 * - AXI XADC interface: Xilinx PG019
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
22 #include <linux/overflow.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/slab.h>
26 #include <linux/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/trigger.h>
33 #include <linux/iio/trigger_consumer.h>
34 #include <linux/iio/triggered_buffer.h>
36 #include "xilinx-xadc.h"
38 static const unsigned int XADC_ZYNQ_UNMASK_TIMEOUT
= 500;
40 /* ZYNQ register definitions */
41 #define XADC_ZYNQ_REG_CFG 0x00
42 #define XADC_ZYNQ_REG_INTSTS 0x04
43 #define XADC_ZYNQ_REG_INTMSK 0x08
44 #define XADC_ZYNQ_REG_STATUS 0x0c
45 #define XADC_ZYNQ_REG_CFIFO 0x10
46 #define XADC_ZYNQ_REG_DFIFO 0x14
47 #define XADC_ZYNQ_REG_CTL 0x18
49 #define XADC_ZYNQ_CFG_ENABLE BIT(31)
50 #define XADC_ZYNQ_CFG_CFIFOTH_MASK (0xf << 20)
51 #define XADC_ZYNQ_CFG_CFIFOTH_OFFSET 20
52 #define XADC_ZYNQ_CFG_DFIFOTH_MASK (0xf << 16)
53 #define XADC_ZYNQ_CFG_DFIFOTH_OFFSET 16
54 #define XADC_ZYNQ_CFG_WEDGE BIT(13)
55 #define XADC_ZYNQ_CFG_REDGE BIT(12)
56 #define XADC_ZYNQ_CFG_TCKRATE_MASK (0x3 << 8)
57 #define XADC_ZYNQ_CFG_TCKRATE_DIV2 (0x0 << 8)
58 #define XADC_ZYNQ_CFG_TCKRATE_DIV4 (0x1 << 8)
59 #define XADC_ZYNQ_CFG_TCKRATE_DIV8 (0x2 << 8)
60 #define XADC_ZYNQ_CFG_TCKRATE_DIV16 (0x3 << 8)
61 #define XADC_ZYNQ_CFG_IGAP_MASK 0x1f
62 #define XADC_ZYNQ_CFG_IGAP(x) (x)
64 #define XADC_ZYNQ_INT_CFIFO_LTH BIT(9)
65 #define XADC_ZYNQ_INT_DFIFO_GTH BIT(8)
66 #define XADC_ZYNQ_INT_ALARM_MASK 0xff
67 #define XADC_ZYNQ_INT_ALARM_OFFSET 0
69 #define XADC_ZYNQ_STATUS_CFIFO_LVL_MASK (0xf << 16)
70 #define XADC_ZYNQ_STATUS_CFIFO_LVL_OFFSET 16
71 #define XADC_ZYNQ_STATUS_DFIFO_LVL_MASK (0xf << 12)
72 #define XADC_ZYNQ_STATUS_DFIFO_LVL_OFFSET 12
73 #define XADC_ZYNQ_STATUS_CFIFOF BIT(11)
74 #define XADC_ZYNQ_STATUS_CFIFOE BIT(10)
75 #define XADC_ZYNQ_STATUS_DFIFOF BIT(9)
76 #define XADC_ZYNQ_STATUS_DFIFOE BIT(8)
77 #define XADC_ZYNQ_STATUS_OT BIT(7)
78 #define XADC_ZYNQ_STATUS_ALM(x) BIT(x)
80 #define XADC_ZYNQ_CTL_RESET BIT(4)
82 #define XADC_ZYNQ_CMD_NOP 0x00
83 #define XADC_ZYNQ_CMD_READ 0x01
84 #define XADC_ZYNQ_CMD_WRITE 0x02
86 #define XADC_ZYNQ_CMD(cmd, addr, data) (((cmd) << 26) | ((addr) << 16) | (data))
88 /* AXI register definitions */
89 #define XADC_AXI_REG_RESET 0x00
90 #define XADC_AXI_REG_STATUS 0x04
91 #define XADC_AXI_REG_ALARM_STATUS 0x08
92 #define XADC_AXI_REG_CONVST 0x0c
93 #define XADC_AXI_REG_XADC_RESET 0x10
94 #define XADC_AXI_REG_GIER 0x5c
95 #define XADC_AXI_REG_IPISR 0x60
96 #define XADC_AXI_REG_IPIER 0x68
99 #define XADC_7S_AXI_ADC_REG_OFFSET 0x200
102 #define XADC_US_AXI_ADC_REG_OFFSET 0x400
104 #define XADC_AXI_RESET_MAGIC 0xa
105 #define XADC_AXI_GIER_ENABLE BIT(31)
107 #define XADC_AXI_INT_EOS BIT(4)
108 #define XADC_AXI_INT_ALARM_MASK 0x3c0f
110 #define XADC_FLAGS_BUFFERED BIT(0)
111 #define XADC_FLAGS_IRQ_OPTIONAL BIT(1)
114 * The XADC hardware supports a samplerate of up to 1MSPS. Unfortunately it does
115 * not have a hardware FIFO. Which means an interrupt is generated for each
116 * conversion sequence. At 1MSPS sample rate the CPU in ZYNQ7000 is completely
117 * overloaded by the interrupts that it soft-lockups. For this reason the driver
118 * limits the maximum samplerate 150kSPS. At this rate the CPU is fairly busy,
119 * but still responsive.
121 #define XADC_MAX_SAMPLERATE 150000
123 static void xadc_write_reg(struct xadc
*xadc
, unsigned int reg
,
126 writel(val
, xadc
->base
+ reg
);
129 static void xadc_read_reg(struct xadc
*xadc
, unsigned int reg
,
132 *val
= readl(xadc
->base
+ reg
);
136 * The ZYNQ interface uses two asynchronous FIFOs for communication with the
137 * XADC. Reads and writes to the XADC register are performed by submitting a
138 * request to the command FIFO (CFIFO), once the request has been completed the
139 * result can be read from the data FIFO (DFIFO). The method currently used in
140 * this driver is to submit the request for a read/write operation, then go to
141 * sleep and wait for an interrupt that signals that a response is available in
145 static void xadc_zynq_write_fifo(struct xadc
*xadc
, uint32_t *cmd
,
150 for (i
= 0; i
< n
; i
++)
151 xadc_write_reg(xadc
, XADC_ZYNQ_REG_CFIFO
, cmd
[i
]);
154 static void xadc_zynq_drain_fifo(struct xadc
*xadc
)
156 uint32_t status
, tmp
;
158 xadc_read_reg(xadc
, XADC_ZYNQ_REG_STATUS
, &status
);
160 while (!(status
& XADC_ZYNQ_STATUS_DFIFOE
)) {
161 xadc_read_reg(xadc
, XADC_ZYNQ_REG_DFIFO
, &tmp
);
162 xadc_read_reg(xadc
, XADC_ZYNQ_REG_STATUS
, &status
);
166 static void xadc_zynq_update_intmsk(struct xadc
*xadc
, unsigned int mask
,
169 xadc
->zynq_intmask
&= ~mask
;
170 xadc
->zynq_intmask
|= val
;
172 xadc_write_reg(xadc
, XADC_ZYNQ_REG_INTMSK
,
173 xadc
->zynq_intmask
| xadc
->zynq_masked_alarm
);
176 static int xadc_zynq_write_adc_reg(struct xadc
*xadc
, unsigned int reg
,
183 spin_lock_irq(&xadc
->lock
);
184 xadc_zynq_update_intmsk(xadc
, XADC_ZYNQ_INT_DFIFO_GTH
,
185 XADC_ZYNQ_INT_DFIFO_GTH
);
187 reinit_completion(&xadc
->completion
);
189 cmd
[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_WRITE
, reg
, val
);
190 xadc_zynq_write_fifo(xadc
, cmd
, ARRAY_SIZE(cmd
));
191 xadc_read_reg(xadc
, XADC_ZYNQ_REG_CFG
, &tmp
);
192 tmp
&= ~XADC_ZYNQ_CFG_DFIFOTH_MASK
;
193 tmp
|= 0 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET
;
194 xadc_write_reg(xadc
, XADC_ZYNQ_REG_CFG
, tmp
);
196 xadc_zynq_update_intmsk(xadc
, XADC_ZYNQ_INT_DFIFO_GTH
, 0);
197 spin_unlock_irq(&xadc
->lock
);
199 ret
= wait_for_completion_interruptible_timeout(&xadc
->completion
, HZ
);
205 xadc_read_reg(xadc
, XADC_ZYNQ_REG_DFIFO
, &tmp
);
210 static int xadc_zynq_read_adc_reg(struct xadc
*xadc
, unsigned int reg
,
217 cmd
[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_READ
, reg
, 0);
218 cmd
[1] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_NOP
, 0, 0);
220 spin_lock_irq(&xadc
->lock
);
221 xadc_zynq_update_intmsk(xadc
, XADC_ZYNQ_INT_DFIFO_GTH
,
222 XADC_ZYNQ_INT_DFIFO_GTH
);
223 xadc_zynq_drain_fifo(xadc
);
224 reinit_completion(&xadc
->completion
);
226 xadc_zynq_write_fifo(xadc
, cmd
, ARRAY_SIZE(cmd
));
227 xadc_read_reg(xadc
, XADC_ZYNQ_REG_CFG
, &tmp
);
228 tmp
&= ~XADC_ZYNQ_CFG_DFIFOTH_MASK
;
229 tmp
|= 1 << XADC_ZYNQ_CFG_DFIFOTH_OFFSET
;
230 xadc_write_reg(xadc
, XADC_ZYNQ_REG_CFG
, tmp
);
232 xadc_zynq_update_intmsk(xadc
, XADC_ZYNQ_INT_DFIFO_GTH
, 0);
233 spin_unlock_irq(&xadc
->lock
);
234 ret
= wait_for_completion_interruptible_timeout(&xadc
->completion
, HZ
);
240 xadc_read_reg(xadc
, XADC_ZYNQ_REG_DFIFO
, &resp
);
241 xadc_read_reg(xadc
, XADC_ZYNQ_REG_DFIFO
, &resp
);
243 *val
= resp
& 0xffff;
248 static unsigned int xadc_zynq_transform_alarm(unsigned int alarm
)
250 return ((alarm
& 0x80) >> 4) |
251 ((alarm
& 0x78) << 1) |
256 * The ZYNQ threshold interrupts are level sensitive. Since we can't make the
257 * threshold condition go way from within the interrupt handler, this means as
258 * soon as a threshold condition is present we would enter the interrupt handler
259 * again and again. To work around this we mask all active thresholds interrupts
260 * in the interrupt handler and start a timer. In this timer we poll the
261 * interrupt status and only if the interrupt is inactive we unmask it again.
263 static void xadc_zynq_unmask_worker(struct work_struct
*work
)
265 struct xadc
*xadc
= container_of(work
, struct xadc
, zynq_unmask_work
.work
);
266 unsigned int misc_sts
, unmask
;
268 xadc_read_reg(xadc
, XADC_ZYNQ_REG_STATUS
, &misc_sts
);
270 misc_sts
&= XADC_ZYNQ_INT_ALARM_MASK
;
272 spin_lock_irq(&xadc
->lock
);
274 /* Clear those bits which are not active anymore */
275 unmask
= (xadc
->zynq_masked_alarm
^ misc_sts
) & xadc
->zynq_masked_alarm
;
276 xadc
->zynq_masked_alarm
&= misc_sts
;
278 /* Also clear those which are masked out anyway */
279 xadc
->zynq_masked_alarm
&= ~xadc
->zynq_intmask
;
281 /* Clear the interrupts before we unmask them */
282 xadc_write_reg(xadc
, XADC_ZYNQ_REG_INTSTS
, unmask
);
284 xadc_zynq_update_intmsk(xadc
, 0, 0);
286 spin_unlock_irq(&xadc
->lock
);
288 /* if still pending some alarm re-trigger the timer */
289 if (xadc
->zynq_masked_alarm
) {
290 schedule_delayed_work(&xadc
->zynq_unmask_work
,
291 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT
));
296 static irqreturn_t
xadc_zynq_interrupt_handler(int irq
, void *devid
)
298 struct iio_dev
*indio_dev
= devid
;
299 struct xadc
*xadc
= iio_priv(indio_dev
);
302 xadc_read_reg(xadc
, XADC_ZYNQ_REG_INTSTS
, &status
);
304 status
&= ~(xadc
->zynq_intmask
| xadc
->zynq_masked_alarm
);
309 spin_lock(&xadc
->lock
);
311 xadc_write_reg(xadc
, XADC_ZYNQ_REG_INTSTS
, status
);
313 if (status
& XADC_ZYNQ_INT_DFIFO_GTH
) {
314 xadc_zynq_update_intmsk(xadc
, XADC_ZYNQ_INT_DFIFO_GTH
,
315 XADC_ZYNQ_INT_DFIFO_GTH
);
316 complete(&xadc
->completion
);
319 status
&= XADC_ZYNQ_INT_ALARM_MASK
;
321 xadc
->zynq_masked_alarm
|= status
;
323 * mask the current event interrupt,
324 * unmask it when the interrupt is no more active.
326 xadc_zynq_update_intmsk(xadc
, 0, 0);
328 xadc_handle_events(indio_dev
,
329 xadc_zynq_transform_alarm(status
));
331 /* unmask the required interrupts in timer. */
332 schedule_delayed_work(&xadc
->zynq_unmask_work
,
333 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT
));
335 spin_unlock(&xadc
->lock
);
340 #define XADC_ZYNQ_TCK_RATE_MAX 50000000
341 #define XADC_ZYNQ_IGAP_DEFAULT 20
342 #define XADC_ZYNQ_PCAP_RATE_MAX 200000000
344 static int xadc_zynq_setup(struct platform_device
*pdev
,
345 struct iio_dev
*indio_dev
, int irq
)
347 struct xadc
*xadc
= iio_priv(indio_dev
);
348 unsigned long pcap_rate
;
349 unsigned int tck_div
;
352 unsigned int tck_rate
;
355 /* TODO: Figure out how to make igap and tck_rate configurable */
356 igap
= XADC_ZYNQ_IGAP_DEFAULT
;
357 tck_rate
= XADC_ZYNQ_TCK_RATE_MAX
;
359 xadc
->zynq_intmask
= ~0;
361 pcap_rate
= clk_get_rate(xadc
->clk
);
365 if (pcap_rate
> XADC_ZYNQ_PCAP_RATE_MAX
) {
366 ret
= clk_set_rate(xadc
->clk
,
367 (unsigned long)XADC_ZYNQ_PCAP_RATE_MAX
);
372 if (tck_rate
> pcap_rate
/ 2) {
375 div
= pcap_rate
/ tck_rate
;
376 if (pcap_rate
/ div
> XADC_ZYNQ_TCK_RATE_MAX
)
381 tck_div
= XADC_ZYNQ_CFG_TCKRATE_DIV2
;
383 tck_div
= XADC_ZYNQ_CFG_TCKRATE_DIV4
;
385 tck_div
= XADC_ZYNQ_CFG_TCKRATE_DIV8
;
387 tck_div
= XADC_ZYNQ_CFG_TCKRATE_DIV16
;
389 xadc_write_reg(xadc
, XADC_ZYNQ_REG_CTL
, XADC_ZYNQ_CTL_RESET
);
390 xadc_write_reg(xadc
, XADC_ZYNQ_REG_CTL
, 0);
391 xadc_write_reg(xadc
, XADC_ZYNQ_REG_INTSTS
, ~0);
392 xadc_write_reg(xadc
, XADC_ZYNQ_REG_INTMSK
, xadc
->zynq_intmask
);
393 xadc_write_reg(xadc
, XADC_ZYNQ_REG_CFG
, XADC_ZYNQ_CFG_ENABLE
|
394 XADC_ZYNQ_CFG_REDGE
| XADC_ZYNQ_CFG_WEDGE
|
395 tck_div
| XADC_ZYNQ_CFG_IGAP(igap
));
397 if (pcap_rate
> XADC_ZYNQ_PCAP_RATE_MAX
) {
398 ret
= clk_set_rate(xadc
->clk
, pcap_rate
);
406 static unsigned long xadc_zynq_get_dclk_rate(struct xadc
*xadc
)
411 xadc_read_reg(xadc
, XADC_ZYNQ_REG_CFG
, &val
);
413 switch (val
& XADC_ZYNQ_CFG_TCKRATE_MASK
) {
414 case XADC_ZYNQ_CFG_TCKRATE_DIV4
:
417 case XADC_ZYNQ_CFG_TCKRATE_DIV8
:
420 case XADC_ZYNQ_CFG_TCKRATE_DIV16
:
428 return clk_get_rate(xadc
->clk
) / div
;
431 static void xadc_zynq_update_alarm(struct xadc
*xadc
, unsigned int alarm
)
436 /* Move OT to bit 7 */
437 alarm
= ((alarm
& 0x08) << 4) | ((alarm
& 0xf0) >> 1) | (alarm
& 0x07);
439 spin_lock_irqsave(&xadc
->lock
, flags
);
441 /* Clear previous interrupts if any. */
442 xadc_read_reg(xadc
, XADC_ZYNQ_REG_INTSTS
, &status
);
443 xadc_write_reg(xadc
, XADC_ZYNQ_REG_INTSTS
, status
& alarm
);
445 xadc_zynq_update_intmsk(xadc
, XADC_ZYNQ_INT_ALARM_MASK
,
446 ~alarm
& XADC_ZYNQ_INT_ALARM_MASK
);
448 spin_unlock_irqrestore(&xadc
->lock
, flags
);
451 static const struct xadc_ops xadc_zynq_ops
= {
452 .read
= xadc_zynq_read_adc_reg
,
453 .write
= xadc_zynq_write_adc_reg
,
454 .setup
= xadc_zynq_setup
,
455 .get_dclk_rate
= xadc_zynq_get_dclk_rate
,
456 .interrupt_handler
= xadc_zynq_interrupt_handler
,
457 .update_alarm
= xadc_zynq_update_alarm
,
458 .type
= XADC_TYPE_S7
,
459 /* Temp in C = (val * 503.975) / 2**bits - 273.15 */
460 .temp_scale
= 503975,
461 .temp_offset
= 273150,
464 static const unsigned int xadc_axi_reg_offsets
[] = {
465 [XADC_TYPE_S7
] = XADC_7S_AXI_ADC_REG_OFFSET
,
466 [XADC_TYPE_US
] = XADC_US_AXI_ADC_REG_OFFSET
,
469 static int xadc_axi_read_adc_reg(struct xadc
*xadc
, unsigned int reg
,
474 xadc_read_reg(xadc
, xadc_axi_reg_offsets
[xadc
->ops
->type
] + reg
* 4,
476 *val
= val32
& 0xffff;
481 static int xadc_axi_write_adc_reg(struct xadc
*xadc
, unsigned int reg
,
484 xadc_write_reg(xadc
, xadc_axi_reg_offsets
[xadc
->ops
->type
] + reg
* 4,
490 static int xadc_axi_setup(struct platform_device
*pdev
,
491 struct iio_dev
*indio_dev
, int irq
)
493 struct xadc
*xadc
= iio_priv(indio_dev
);
495 xadc_write_reg(xadc
, XADC_AXI_REG_RESET
, XADC_AXI_RESET_MAGIC
);
496 xadc_write_reg(xadc
, XADC_AXI_REG_GIER
, XADC_AXI_GIER_ENABLE
);
501 static irqreturn_t
xadc_axi_interrupt_handler(int irq
, void *devid
)
503 struct iio_dev
*indio_dev
= devid
;
504 struct xadc
*xadc
= iio_priv(indio_dev
);
505 uint32_t status
, mask
;
508 xadc_read_reg(xadc
, XADC_AXI_REG_IPISR
, &status
);
509 xadc_read_reg(xadc
, XADC_AXI_REG_IPIER
, &mask
);
515 if ((status
& XADC_AXI_INT_EOS
) && xadc
->trigger
)
516 iio_trigger_poll(xadc
->trigger
);
518 if (status
& XADC_AXI_INT_ALARM_MASK
) {
520 * The order of the bits in the AXI-XADC status register does
521 * not match the order of the bits in the XADC alarm enable
522 * register. xadc_handle_events() expects the events to be in
523 * the same order as the XADC alarm enable register.
525 events
= (status
& 0x000e) >> 1;
526 events
|= (status
& 0x0001) << 3;
527 events
|= (status
& 0x3c00) >> 6;
528 xadc_handle_events(indio_dev
, events
);
531 xadc_write_reg(xadc
, XADC_AXI_REG_IPISR
, status
);
536 static void xadc_axi_update_alarm(struct xadc
*xadc
, unsigned int alarm
)
542 * The order of the bits in the AXI-XADC status register does not match
543 * the order of the bits in the XADC alarm enable register. We get
544 * passed the alarm mask in the same order as in the XADC alarm enable
547 alarm
= ((alarm
& 0x07) << 1) | ((alarm
& 0x08) >> 3) |
548 ((alarm
& 0xf0) << 6);
550 spin_lock_irqsave(&xadc
->lock
, flags
);
551 xadc_read_reg(xadc
, XADC_AXI_REG_IPIER
, &val
);
552 val
&= ~XADC_AXI_INT_ALARM_MASK
;
554 xadc_write_reg(xadc
, XADC_AXI_REG_IPIER
, val
);
555 spin_unlock_irqrestore(&xadc
->lock
, flags
);
558 static unsigned long xadc_axi_get_dclk(struct xadc
*xadc
)
560 return clk_get_rate(xadc
->clk
);
563 static const struct xadc_ops xadc_7s_axi_ops
= {
564 .read
= xadc_axi_read_adc_reg
,
565 .write
= xadc_axi_write_adc_reg
,
566 .setup
= xadc_axi_setup
,
567 .get_dclk_rate
= xadc_axi_get_dclk
,
568 .update_alarm
= xadc_axi_update_alarm
,
569 .interrupt_handler
= xadc_axi_interrupt_handler
,
570 .flags
= XADC_FLAGS_BUFFERED
| XADC_FLAGS_IRQ_OPTIONAL
,
571 .type
= XADC_TYPE_S7
,
572 /* Temp in C = (val * 503.975) / 2**bits - 273.15 */
573 .temp_scale
= 503975,
574 .temp_offset
= 273150,
577 static const struct xadc_ops xadc_us_axi_ops
= {
578 .read
= xadc_axi_read_adc_reg
,
579 .write
= xadc_axi_write_adc_reg
,
580 .setup
= xadc_axi_setup
,
581 .get_dclk_rate
= xadc_axi_get_dclk
,
582 .update_alarm
= xadc_axi_update_alarm
,
583 .interrupt_handler
= xadc_axi_interrupt_handler
,
584 .flags
= XADC_FLAGS_BUFFERED
| XADC_FLAGS_IRQ_OPTIONAL
,
585 .type
= XADC_TYPE_US
,
587 * Values below are for UltraScale+ (SYSMONE4) using internal reference.
588 * See https://docs.xilinx.com/v/u/en-US/ug580-ultrascale-sysmon
590 .temp_scale
= 509314,
591 .temp_offset
= 280231,
594 static int _xadc_update_adc_reg(struct xadc
*xadc
, unsigned int reg
,
595 uint16_t mask
, uint16_t val
)
600 ret
= _xadc_read_adc_reg(xadc
, reg
, &tmp
);
604 return _xadc_write_adc_reg(xadc
, reg
, (tmp
& ~mask
) | val
);
607 static int xadc_update_adc_reg(struct xadc
*xadc
, unsigned int reg
,
608 uint16_t mask
, uint16_t val
)
612 mutex_lock(&xadc
->mutex
);
613 ret
= _xadc_update_adc_reg(xadc
, reg
, mask
, val
);
614 mutex_unlock(&xadc
->mutex
);
619 static unsigned long xadc_get_dclk_rate(struct xadc
*xadc
)
621 return xadc
->ops
->get_dclk_rate(xadc
);
624 static int xadc_update_scan_mode(struct iio_dev
*indio_dev
,
625 const unsigned long *mask
)
627 struct xadc
*xadc
= iio_priv(indio_dev
);
631 n
= bitmap_weight(mask
, iio_get_masklength(indio_dev
));
633 data
= devm_krealloc_array(indio_dev
->dev
.parent
, xadc
->data
,
634 n
, sizeof(*xadc
->data
), GFP_KERNEL
);
638 memset(data
, 0, n
* sizeof(*xadc
->data
));
644 static unsigned int xadc_scan_index_to_channel(unsigned int scan_index
)
646 switch (scan_index
) {
648 return XADC_REG_VCCPINT
;
650 return XADC_REG_VCCPAUX
;
652 return XADC_REG_VCCO_DDR
;
654 return XADC_REG_TEMP
;
656 return XADC_REG_VCCINT
;
658 return XADC_REG_VCCAUX
;
660 return XADC_REG_VPVN
;
662 return XADC_REG_VREFP
;
664 return XADC_REG_VREFN
;
666 return XADC_REG_VCCBRAM
;
668 return XADC_REG_VAUX(scan_index
- 16);
672 static irqreturn_t
xadc_trigger_handler(int irq
, void *p
)
674 struct iio_poll_func
*pf
= p
;
675 struct iio_dev
*indio_dev
= pf
->indio_dev
;
676 struct xadc
*xadc
= iio_priv(indio_dev
);
684 iio_for_each_active_channel(indio_dev
, i
) {
685 chan
= xadc_scan_index_to_channel(i
);
686 xadc_read_adc_reg(xadc
, chan
, &xadc
->data
[j
]);
690 iio_push_to_buffers(indio_dev
, xadc
->data
);
693 iio_trigger_notify_done(indio_dev
->trig
);
698 static int xadc_trigger_set_state(struct iio_trigger
*trigger
, bool state
)
700 struct xadc
*xadc
= iio_trigger_get_drvdata(trigger
);
706 mutex_lock(&xadc
->mutex
);
709 /* Only one of the two triggers can be active at a time. */
710 if (xadc
->trigger
!= NULL
) {
714 xadc
->trigger
= trigger
;
715 if (trigger
== xadc
->convst_trigger
)
716 convst
= XADC_CONF0_EC
;
720 ret
= _xadc_update_adc_reg(xadc
, XADC_REG_CONF1
, XADC_CONF0_EC
,
725 xadc
->trigger
= NULL
;
728 spin_lock_irqsave(&xadc
->lock
, flags
);
729 xadc_read_reg(xadc
, XADC_AXI_REG_IPIER
, &val
);
730 xadc_write_reg(xadc
, XADC_AXI_REG_IPISR
, XADC_AXI_INT_EOS
);
732 val
|= XADC_AXI_INT_EOS
;
734 val
&= ~XADC_AXI_INT_EOS
;
735 xadc_write_reg(xadc
, XADC_AXI_REG_IPIER
, val
);
736 spin_unlock_irqrestore(&xadc
->lock
, flags
);
739 mutex_unlock(&xadc
->mutex
);
744 static const struct iio_trigger_ops xadc_trigger_ops
= {
745 .set_trigger_state
= &xadc_trigger_set_state
,
748 static struct iio_trigger
*xadc_alloc_trigger(struct iio_dev
*indio_dev
,
751 struct device
*dev
= indio_dev
->dev
.parent
;
752 struct iio_trigger
*trig
;
755 trig
= devm_iio_trigger_alloc(dev
, "%s%d-%s", indio_dev
->name
,
756 iio_device_id(indio_dev
), name
);
758 return ERR_PTR(-ENOMEM
);
760 trig
->ops
= &xadc_trigger_ops
;
761 iio_trigger_set_drvdata(trig
, iio_priv(indio_dev
));
763 ret
= devm_iio_trigger_register(dev
, trig
);
770 static int xadc_power_adc_b(struct xadc
*xadc
, unsigned int seq_mode
)
775 * As per datasheet the power-down bits are don't care in the
776 * UltraScale, but as per reality setting the power-down bit for the
777 * non-existing ADC-B powers down the main ADC, so just return and don't
780 if (xadc
->ops
->type
== XADC_TYPE_US
)
783 /* Powerdown the ADC-B when it is not needed. */
785 case XADC_CONF1_SEQ_SIMULTANEOUS
:
786 case XADC_CONF1_SEQ_INDEPENDENT
:
790 val
= XADC_CONF2_PD_ADC_B
;
794 return xadc_update_adc_reg(xadc
, XADC_REG_CONF2
, XADC_CONF2_PD_MASK
,
798 static int xadc_get_seq_mode(struct xadc
*xadc
, unsigned long scan_mode
)
800 unsigned int aux_scan_mode
= scan_mode
>> 16;
802 /* UltraScale has only one ADC and supports only continuous mode */
803 if (xadc
->ops
->type
== XADC_TYPE_US
)
804 return XADC_CONF1_SEQ_CONTINUOUS
;
806 if (xadc
->external_mux_mode
== XADC_EXTERNAL_MUX_DUAL
)
807 return XADC_CONF1_SEQ_SIMULTANEOUS
;
809 if ((aux_scan_mode
& 0xff00) == 0 ||
810 (aux_scan_mode
& 0x00ff) == 0)
811 return XADC_CONF1_SEQ_CONTINUOUS
;
813 return XADC_CONF1_SEQ_SIMULTANEOUS
;
816 static int xadc_postdisable(struct iio_dev
*indio_dev
)
818 struct xadc
*xadc
= iio_priv(indio_dev
);
819 unsigned long scan_mask
;
823 scan_mask
= 1; /* Run calibration as part of the sequence */
824 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
825 scan_mask
|= BIT(indio_dev
->channels
[i
].scan_index
);
827 /* Enable all channels and calibration */
828 ret
= xadc_write_adc_reg(xadc
, XADC_REG_SEQ(0), scan_mask
& 0xffff);
832 ret
= xadc_write_adc_reg(xadc
, XADC_REG_SEQ(1), scan_mask
>> 16);
836 ret
= xadc_update_adc_reg(xadc
, XADC_REG_CONF1
, XADC_CONF1_SEQ_MASK
,
837 XADC_CONF1_SEQ_CONTINUOUS
);
841 return xadc_power_adc_b(xadc
, XADC_CONF1_SEQ_CONTINUOUS
);
844 static int xadc_preenable(struct iio_dev
*indio_dev
)
846 struct xadc
*xadc
= iio_priv(indio_dev
);
847 unsigned long scan_mask
;
851 ret
= xadc_update_adc_reg(xadc
, XADC_REG_CONF1
, XADC_CONF1_SEQ_MASK
,
852 XADC_CONF1_SEQ_DEFAULT
);
856 scan_mask
= *indio_dev
->active_scan_mask
;
857 seq_mode
= xadc_get_seq_mode(xadc
, scan_mask
);
859 ret
= xadc_write_adc_reg(xadc
, XADC_REG_SEQ(0), scan_mask
& 0xffff);
864 * In simultaneous mode the upper and lower aux channels are samples at
865 * the same time. In this mode the upper 8 bits in the sequencer
866 * register are don't care and the lower 8 bits control two channels
867 * each. As such we must set the bit if either the channel in the lower
868 * group or the upper group is enabled.
870 if (seq_mode
== XADC_CONF1_SEQ_SIMULTANEOUS
)
871 scan_mask
= ((scan_mask
>> 8) | scan_mask
) & 0xff0000;
873 ret
= xadc_write_adc_reg(xadc
, XADC_REG_SEQ(1), scan_mask
>> 16);
877 ret
= xadc_power_adc_b(xadc
, seq_mode
);
881 ret
= xadc_update_adc_reg(xadc
, XADC_REG_CONF1
, XADC_CONF1_SEQ_MASK
,
888 xadc_postdisable(indio_dev
);
892 static const struct iio_buffer_setup_ops xadc_buffer_ops
= {
893 .preenable
= &xadc_preenable
,
894 .postdisable
= &xadc_postdisable
,
897 static int xadc_read_samplerate(struct xadc
*xadc
)
903 ret
= xadc_read_adc_reg(xadc
, XADC_REG_CONF2
, &val16
);
907 div
= (val16
& XADC_CONF2_DIV_MASK
) >> XADC_CONF2_DIV_OFFSET
;
911 return xadc_get_dclk_rate(xadc
) / div
/ 26;
914 static int xadc_read_raw(struct iio_dev
*indio_dev
,
915 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
917 struct xadc
*xadc
= iio_priv(indio_dev
);
918 unsigned int bits
= chan
->scan_type
.realbits
;
923 case IIO_CHAN_INFO_RAW
:
924 if (iio_buffer_enabled(indio_dev
))
926 ret
= xadc_read_adc_reg(xadc
, chan
->address
, &val16
);
930 val16
>>= chan
->scan_type
.shift
;
931 if (chan
->scan_type
.sign
== 'u')
934 *val
= sign_extend32(val16
, bits
- 1);
937 case IIO_CHAN_INFO_SCALE
:
938 switch (chan
->type
) {
940 /* V = (val * 3.0) / 2**bits */
941 switch (chan
->address
) {
942 case XADC_REG_VCCINT
:
943 case XADC_REG_VCCAUX
:
946 case XADC_REG_VCCBRAM
:
947 case XADC_REG_VCCPINT
:
948 case XADC_REG_VCCPAUX
:
949 case XADC_REG_VCCO_DDR
:
957 return IIO_VAL_FRACTIONAL_LOG2
;
959 *val
= xadc
->ops
->temp_scale
;
961 return IIO_VAL_FRACTIONAL_LOG2
;
965 case IIO_CHAN_INFO_OFFSET
:
966 /* Only the temperature channel has an offset */
967 *val
= -((xadc
->ops
->temp_offset
<< bits
) / xadc
->ops
->temp_scale
);
969 case IIO_CHAN_INFO_SAMP_FREQ
:
970 ret
= xadc_read_samplerate(xadc
);
981 static int xadc_write_samplerate(struct xadc
*xadc
, int val
)
983 unsigned long clk_rate
= xadc_get_dclk_rate(xadc
);
993 if (val
> XADC_MAX_SAMPLERATE
)
994 val
= XADC_MAX_SAMPLERATE
;
1003 * We want to round down, but only if we do not exceed the 150 kSPS
1006 div
= clk_rate
/ val
;
1007 if (clk_rate
/ div
/ 26 > XADC_MAX_SAMPLERATE
)
1011 else if (div
> 0xff)
1014 return xadc_update_adc_reg(xadc
, XADC_REG_CONF2
, XADC_CONF2_DIV_MASK
,
1015 div
<< XADC_CONF2_DIV_OFFSET
);
1018 static int xadc_write_raw(struct iio_dev
*indio_dev
,
1019 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
1021 struct xadc
*xadc
= iio_priv(indio_dev
);
1023 if (info
!= IIO_CHAN_INFO_SAMP_FREQ
)
1026 return xadc_write_samplerate(xadc
, val
);
1029 static const struct iio_event_spec xadc_temp_events
[] = {
1031 .type
= IIO_EV_TYPE_THRESH
,
1032 .dir
= IIO_EV_DIR_RISING
,
1033 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
1034 BIT(IIO_EV_INFO_VALUE
) |
1035 BIT(IIO_EV_INFO_HYSTERESIS
),
1039 /* Separate values for upper and lower thresholds, but only a shared enabled */
1040 static const struct iio_event_spec xadc_voltage_events
[] = {
1042 .type
= IIO_EV_TYPE_THRESH
,
1043 .dir
= IIO_EV_DIR_RISING
,
1044 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1046 .type
= IIO_EV_TYPE_THRESH
,
1047 .dir
= IIO_EV_DIR_FALLING
,
1048 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1050 .type
= IIO_EV_TYPE_THRESH
,
1051 .dir
= IIO_EV_DIR_EITHER
,
1052 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
1056 #define XADC_CHAN_TEMP(_chan, _scan_index, _addr, _bits) { \
1059 .channel = (_chan), \
1060 .address = (_addr), \
1061 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1062 BIT(IIO_CHAN_INFO_SCALE) | \
1063 BIT(IIO_CHAN_INFO_OFFSET), \
1064 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1065 .event_spec = xadc_temp_events, \
1066 .num_event_specs = ARRAY_SIZE(xadc_temp_events), \
1067 .scan_index = (_scan_index), \
1070 .realbits = (_bits), \
1071 .storagebits = 16, \
1072 .shift = 16 - (_bits), \
1073 .endianness = IIO_CPU, \
1077 #define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _bits, _ext, _alarm) { \
1078 .type = IIO_VOLTAGE, \
1080 .channel = (_chan), \
1081 .address = (_addr), \
1082 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1083 BIT(IIO_CHAN_INFO_SCALE), \
1084 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1085 .event_spec = (_alarm) ? xadc_voltage_events : NULL, \
1086 .num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \
1087 .scan_index = (_scan_index), \
1089 .sign = ((_addr) == XADC_REG_VREFN) ? 's' : 'u', \
1090 .realbits = (_bits), \
1091 .storagebits = 16, \
1092 .shift = 16 - (_bits), \
1093 .endianness = IIO_CPU, \
1095 .extend_name = _ext, \
1099 #define XADC_7S_CHAN_TEMP(_chan, _scan_index, _addr) \
1100 XADC_CHAN_TEMP(_chan, _scan_index, _addr, 12)
1101 #define XADC_7S_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) \
1102 XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, 12, _ext, _alarm)
1104 static const struct iio_chan_spec xadc_7s_channels
[] = {
1105 XADC_7S_CHAN_TEMP(0, 8, XADC_REG_TEMP
),
1106 XADC_7S_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT
, "vccint", true),
1107 XADC_7S_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX
, "vccaux", true),
1108 XADC_7S_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM
, "vccbram", true),
1109 XADC_7S_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT
, "vccpint", true),
1110 XADC_7S_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX
, "vccpaux", true),
1111 XADC_7S_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR
, "vccoddr", true),
1112 XADC_7S_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP
, "vrefp", false),
1113 XADC_7S_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN
, "vrefn", false),
1114 XADC_7S_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN
, NULL
, false),
1115 XADC_7S_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL
, false),
1116 XADC_7S_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL
, false),
1117 XADC_7S_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL
, false),
1118 XADC_7S_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL
, false),
1119 XADC_7S_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL
, false),
1120 XADC_7S_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL
, false),
1121 XADC_7S_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL
, false),
1122 XADC_7S_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL
, false),
1123 XADC_7S_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL
, false),
1124 XADC_7S_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL
, false),
1125 XADC_7S_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL
, false),
1126 XADC_7S_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL
, false),
1127 XADC_7S_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL
, false),
1128 XADC_7S_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL
, false),
1129 XADC_7S_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL
, false),
1130 XADC_7S_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL
, false),
1134 #define XADC_US_CHAN_TEMP(_chan, _scan_index, _addr) \
1135 XADC_CHAN_TEMP(_chan, _scan_index, _addr, 10)
1136 #define XADC_US_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) \
1137 XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, 10, _ext, _alarm)
1139 static const struct iio_chan_spec xadc_us_channels
[] = {
1140 XADC_US_CHAN_TEMP(0, 8, XADC_REG_TEMP
),
1141 XADC_US_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT
, "vccint", true),
1142 XADC_US_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX
, "vccaux", true),
1143 XADC_US_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM
, "vccbram", true),
1144 XADC_US_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT
, "vccpsintlp", true),
1145 XADC_US_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX
, "vccpsintfp", true),
1146 XADC_US_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR
, "vccpsaux", true),
1147 XADC_US_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP
, "vrefp", false),
1148 XADC_US_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN
, "vrefn", false),
1149 XADC_US_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN
, NULL
, false),
1150 XADC_US_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL
, false),
1151 XADC_US_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL
, false),
1152 XADC_US_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL
, false),
1153 XADC_US_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL
, false),
1154 XADC_US_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL
, false),
1155 XADC_US_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL
, false),
1156 XADC_US_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL
, false),
1157 XADC_US_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL
, false),
1158 XADC_US_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL
, false),
1159 XADC_US_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL
, false),
1160 XADC_US_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL
, false),
1161 XADC_US_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL
, false),
1162 XADC_US_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL
, false),
1163 XADC_US_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL
, false),
1164 XADC_US_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL
, false),
1165 XADC_US_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL
, false),
1168 static const struct iio_info xadc_info
= {
1169 .read_raw
= &xadc_read_raw
,
1170 .write_raw
= &xadc_write_raw
,
1171 .read_event_config
= &xadc_read_event_config
,
1172 .write_event_config
= &xadc_write_event_config
,
1173 .read_event_value
= &xadc_read_event_value
,
1174 .write_event_value
= &xadc_write_event_value
,
1175 .update_scan_mode
= &xadc_update_scan_mode
,
1178 static const struct of_device_id xadc_of_match_table
[] = {
1180 .compatible
= "xlnx,zynq-xadc-1.00.a",
1181 .data
= &xadc_zynq_ops
1183 .compatible
= "xlnx,axi-xadc-1.00.a",
1184 .data
= &xadc_7s_axi_ops
1186 .compatible
= "xlnx,system-management-wiz-1.3",
1187 .data
= &xadc_us_axi_ops
1191 MODULE_DEVICE_TABLE(of
, xadc_of_match_table
);
1193 static int xadc_parse_dt(struct iio_dev
*indio_dev
, unsigned int *conf
, int irq
)
1195 struct device
*dev
= indio_dev
->dev
.parent
;
1196 struct xadc
*xadc
= iio_priv(indio_dev
);
1197 const struct iio_chan_spec
*channel_templates
;
1198 struct iio_chan_spec
*channels
, *chan
;
1199 struct fwnode_handle
*chan_node
, *child
;
1200 unsigned int max_channels
;
1201 unsigned int num_channels
;
1202 const char *external_mux
;
1210 ret
= device_property_read_string(dev
, "xlnx,external-mux", &external_mux
);
1211 if (ret
< 0 || strcasecmp(external_mux
, "none") == 0)
1212 xadc
->external_mux_mode
= XADC_EXTERNAL_MUX_NONE
;
1213 else if (strcasecmp(external_mux
, "single") == 0)
1214 xadc
->external_mux_mode
= XADC_EXTERNAL_MUX_SINGLE
;
1215 else if (strcasecmp(external_mux
, "dual") == 0)
1216 xadc
->external_mux_mode
= XADC_EXTERNAL_MUX_DUAL
;
1220 if (xadc
->external_mux_mode
!= XADC_EXTERNAL_MUX_NONE
) {
1221 ret
= device_property_read_u32(dev
, "xlnx,external-mux-channel", &ext_mux_chan
);
1225 if (xadc
->external_mux_mode
== XADC_EXTERNAL_MUX_SINGLE
) {
1226 if (ext_mux_chan
== 0)
1227 ext_mux_chan
= XADC_REG_VPVN
;
1228 else if (ext_mux_chan
<= 16)
1229 ext_mux_chan
= XADC_REG_VAUX(ext_mux_chan
- 1);
1233 if (ext_mux_chan
> 0 && ext_mux_chan
<= 8)
1234 ext_mux_chan
= XADC_REG_VAUX(ext_mux_chan
- 1);
1239 *conf
|= XADC_CONF0_MUX
| XADC_CONF0_CHAN(ext_mux_chan
);
1241 if (xadc
->ops
->type
== XADC_TYPE_S7
) {
1242 channel_templates
= xadc_7s_channels
;
1243 max_channels
= ARRAY_SIZE(xadc_7s_channels
);
1245 channel_templates
= xadc_us_channels
;
1246 max_channels
= ARRAY_SIZE(xadc_us_channels
);
1248 channels
= devm_kmemdup(dev
, channel_templates
,
1249 sizeof(channels
[0]) * max_channels
, GFP_KERNEL
);
1254 chan
= &channels
[9];
1256 chan_node
= device_get_named_child_node(dev
, "xlnx,channels");
1257 fwnode_for_each_child_node(chan_node
, child
) {
1258 if (num_channels
>= max_channels
) {
1259 fwnode_handle_put(child
);
1263 ret
= fwnode_property_read_u32(child
, "reg", ®
);
1264 if (ret
|| reg
> 16)
1267 if (fwnode_property_read_bool(child
, "xlnx,bipolar"))
1268 chan
->scan_type
.sign
= 's';
1271 chan
->scan_index
= 11;
1272 chan
->address
= XADC_REG_VPVN
;
1274 chan
->scan_index
= 15 + reg
;
1275 chan
->address
= XADC_REG_VAUX(reg
- 1);
1280 fwnode_handle_put(chan_node
);
1282 /* No IRQ => no events */
1284 for (i
= 0; i
< num_channels
; i
++) {
1285 channels
[i
].event_spec
= NULL
;
1286 channels
[i
].num_event_specs
= 0;
1290 indio_dev
->num_channels
= num_channels
;
1291 indio_dev
->channels
= devm_krealloc_array(dev
, channels
,
1292 num_channels
, sizeof(*channels
),
1294 /* If we can't resize the channels array, just use the original */
1295 if (!indio_dev
->channels
)
1296 indio_dev
->channels
= channels
;
1301 static const char * const xadc_type_names
[] = {
1302 [XADC_TYPE_S7
] = "xadc",
1303 [XADC_TYPE_US
] = "xilinx-system-monitor",
1306 static void xadc_cancel_delayed_work(void *data
)
1308 struct delayed_work
*work
= data
;
1310 cancel_delayed_work_sync(work
);
1313 static int xadc_probe(struct platform_device
*pdev
)
1315 struct device
*dev
= &pdev
->dev
;
1316 const struct xadc_ops
*ops
;
1317 struct iio_dev
*indio_dev
;
1318 unsigned int bipolar_mask
;
1325 ops
= device_get_match_data(dev
);
1329 irq
= platform_get_irq_optional(pdev
, 0);
1331 (irq
!= -ENXIO
|| !(ops
->flags
& XADC_FLAGS_IRQ_OPTIONAL
)))
1334 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*xadc
));
1338 xadc
= iio_priv(indio_dev
);
1340 init_completion(&xadc
->completion
);
1341 mutex_init(&xadc
->mutex
);
1342 spin_lock_init(&xadc
->lock
);
1343 INIT_DELAYED_WORK(&xadc
->zynq_unmask_work
, xadc_zynq_unmask_worker
);
1345 xadc
->base
= devm_platform_ioremap_resource(pdev
, 0);
1346 if (IS_ERR(xadc
->base
))
1347 return PTR_ERR(xadc
->base
);
1349 indio_dev
->name
= xadc_type_names
[xadc
->ops
->type
];
1350 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1351 indio_dev
->info
= &xadc_info
;
1353 ret
= xadc_parse_dt(indio_dev
, &conf0
, irq
);
1357 if (xadc
->ops
->flags
& XADC_FLAGS_BUFFERED
) {
1358 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
1359 &iio_pollfunc_store_time
,
1360 &xadc_trigger_handler
,
1366 xadc
->convst_trigger
= xadc_alloc_trigger(indio_dev
, "convst");
1367 if (IS_ERR(xadc
->convst_trigger
))
1368 return PTR_ERR(xadc
->convst_trigger
);
1370 xadc
->samplerate_trigger
= xadc_alloc_trigger(indio_dev
,
1372 if (IS_ERR(xadc
->samplerate_trigger
))
1373 return PTR_ERR(xadc
->samplerate_trigger
);
1377 xadc
->clk
= devm_clk_get_enabled(dev
, NULL
);
1378 if (IS_ERR(xadc
->clk
))
1379 return PTR_ERR(xadc
->clk
);
1382 * Make sure not to exceed the maximum samplerate since otherwise the
1383 * resulting interrupt storm will soft-lock the system.
1385 if (xadc
->ops
->flags
& XADC_FLAGS_BUFFERED
) {
1386 ret
= xadc_read_samplerate(xadc
);
1390 if (ret
> XADC_MAX_SAMPLERATE
) {
1391 ret
= xadc_write_samplerate(xadc
, XADC_MAX_SAMPLERATE
);
1398 ret
= devm_request_irq(dev
, irq
, xadc
->ops
->interrupt_handler
,
1399 0, dev_name(dev
), indio_dev
);
1403 ret
= devm_add_action_or_reset(dev
, xadc_cancel_delayed_work
,
1404 &xadc
->zynq_unmask_work
);
1409 ret
= xadc
->ops
->setup(pdev
, indio_dev
, irq
);
1413 for (i
= 0; i
< 16; i
++)
1414 xadc_read_adc_reg(xadc
, XADC_REG_THRESHOLD(i
),
1415 &xadc
->threshold
[i
]);
1417 ret
= xadc_write_adc_reg(xadc
, XADC_REG_CONF0
, conf0
);
1422 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1423 if (indio_dev
->channels
[i
].scan_type
.sign
== 's')
1424 bipolar_mask
|= BIT(indio_dev
->channels
[i
].scan_index
);
1427 ret
= xadc_write_adc_reg(xadc
, XADC_REG_INPUT_MODE(0), bipolar_mask
);
1431 ret
= xadc_write_adc_reg(xadc
, XADC_REG_INPUT_MODE(1),
1432 bipolar_mask
>> 16);
1436 /* Go to non-buffered mode */
1437 xadc_postdisable(indio_dev
);
1439 return devm_iio_device_register(dev
, indio_dev
);
1442 static struct platform_driver xadc_driver
= {
1443 .probe
= xadc_probe
,
1446 .of_match_table
= xadc_of_match_table
,
1449 module_platform_driver(xadc_driver
);
1451 MODULE_LICENSE("GPL v2");
1452 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1453 MODULE_DESCRIPTION("Xilinx XADC IIO driver");