blob: 23395fc5b267ced8ba4b180c978a32c82980b32d [file] [log] [blame]
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001/*
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
37static 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
106static void xadc_write_reg(struct xadc *xadc, unsigned int reg,
107 uint32_t val)
108{
109 writel(val, xadc->base + reg);
110}
111
112static void xadc_read_reg(struct xadc *xadc, unsigned int reg,
113 uint32_t *val)
114{
115 *val = readl(xadc->base + reg);
116}
117
118/*
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.
126 */
127
128static void xadc_zynq_write_fifo(struct xadc *xadc, uint32_t *cmd,
129 unsigned int n)
130{
131 unsigned int i;
132
133 for (i = 0; i < n; i++)
134 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFIFO, cmd[i]);
135}
136
137static void xadc_zynq_drain_fifo(struct xadc *xadc)
138{
139 uint32_t status, tmp;
140
141 xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &status);
142
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);
146 }
147}
148
149static void xadc_zynq_update_intmsk(struct xadc *xadc, unsigned int mask,
150 unsigned int val)
151{
152 xadc->zynq_intmask &= ~mask;
153 xadc->zynq_intmask |= val;
154
155 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK,
156 xadc->zynq_intmask | xadc->zynq_masked_alarm);
157}
158
159static int xadc_zynq_write_adc_reg(struct xadc *xadc, unsigned int reg,
160 uint16_t val)
161{
162 uint32_t cmd[1];
163 uint32_t tmp;
164 int ret;
165
166 spin_lock_irq(&xadc->lock);
167 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
168 XADC_ZYNQ_INT_DFIFO_GTH);
169
170 reinit_completion(&xadc->completion);
171
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);
178
179 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH, 0);
180 spin_unlock_irq(&xadc->lock);
181
182 ret = wait_for_completion_interruptible_timeout(&xadc->completion, HZ);
183 if (ret == 0)
184 ret = -EIO;
185 else
186 ret = 0;
187
188 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &tmp);
189
190 return ret;
191}
192
193static int xadc_zynq_read_adc_reg(struct xadc *xadc, unsigned int reg,
194 uint16_t *val)
195{
196 uint32_t cmd[2];
197 uint32_t resp, tmp;
198 int ret;
199
200 cmd[0] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_READ, reg, 0);
201 cmd[1] = XADC_ZYNQ_CMD(XADC_ZYNQ_CMD_NOP, 0, 0);
202
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);
208
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);
214
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;
222
223 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
224 xadc_read_reg(xadc, XADC_ZYNQ_REG_DFIFO, &resp);
225
226 *val = resp & 0xffff;
227
228 return 0;
229}
230
231static unsigned int xadc_zynq_transform_alarm(unsigned int alarm)
232{
233 return ((alarm & 0x80) >> 4) |
234 ((alarm & 0x78) << 1) |
235 (alarm & 0x07);
236}
237
238/*
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.
245 */
246static void xadc_zynq_unmask_worker(struct work_struct *work)
247{
248 struct xadc *xadc = container_of(work, struct xadc, zynq_unmask_work.work);
249 unsigned int misc_sts, unmask;
250
251 xadc_read_reg(xadc, XADC_ZYNQ_REG_STATUS, &misc_sts);
252
253 misc_sts &= XADC_ZYNQ_INT_ALARM_MASK;
254
255 spin_lock_irq(&xadc->lock);
256
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;
260
261 /* Also clear those which are masked out anyway */
262 xadc->zynq_masked_alarm &= ~xadc->zynq_intmask;
263
264 /* Clear the interrupts before we unmask them */
265 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, unmask);
266
267 xadc_zynq_update_intmsk(xadc, 0, 0);
268
269 spin_unlock_irq(&xadc->lock);
270
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));
275 }
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000276
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000277}
278
279static irqreturn_t xadc_zynq_interrupt_handler(int irq, void *devid)
280{
281 struct iio_dev *indio_dev = devid;
282 struct xadc *xadc = iio_priv(indio_dev);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000283 uint32_t status;
284
285 xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
286
287 status &= ~(xadc->zynq_intmask | xadc->zynq_masked_alarm);
288
289 if (!status)
290 return IRQ_NONE;
291
292 spin_lock(&xadc->lock);
293
294 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status);
295
296 if (status & XADC_ZYNQ_INT_DFIFO_GTH) {
297 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_DFIFO_GTH,
298 XADC_ZYNQ_INT_DFIFO_GTH);
299 complete(&xadc->completion);
300 }
301
302 status &= XADC_ZYNQ_INT_ALARM_MASK;
303 if (status) {
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000304 xadc->zynq_masked_alarm |= status;
305 /*
306 * mask the current event interrupt,
307 * unmask it when the interrupt is no more active.
308 */
309 xadc_zynq_update_intmsk(xadc, 0, 0);
Xander Huff70581e02015-08-11 18:00:49 -0500310
311 xadc_handle_events(indio_dev,
312 xadc_zynq_transform_alarm(status));
313
314 /* unmask the required interrupts in timer. */
315 schedule_delayed_work(&xadc->zynq_unmask_work,
316 msecs_to_jiffies(XADC_ZYNQ_UNMASK_TIMEOUT));
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000317 }
318 spin_unlock(&xadc->lock);
319
Xander Huff70581e02015-08-11 18:00:49 -0500320 return IRQ_HANDLED;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000321}
322
323#define XADC_ZYNQ_TCK_RATE_MAX 50000000
324#define XADC_ZYNQ_IGAP_DEFAULT 20
325
326static int xadc_zynq_setup(struct platform_device *pdev,
327 struct iio_dev *indio_dev, int irq)
328{
329 struct xadc *xadc = iio_priv(indio_dev);
330 unsigned long pcap_rate;
331 unsigned int tck_div;
332 unsigned int div;
333 unsigned int igap;
334 unsigned int tck_rate;
335
336 /* TODO: Figure out how to make igap and tck_rate configurable */
337 igap = XADC_ZYNQ_IGAP_DEFAULT;
338 tck_rate = XADC_ZYNQ_TCK_RATE_MAX;
339
340 xadc->zynq_intmask = ~0;
341
342 pcap_rate = clk_get_rate(xadc->clk);
Manish Narani0a846092018-07-23 20:32:00 +0530343 if (!pcap_rate)
344 return -EINVAL;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000345
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000346 if (tck_rate > pcap_rate / 2) {
347 div = 2;
348 } else {
349 div = pcap_rate / tck_rate;
350 if (pcap_rate / div > XADC_ZYNQ_TCK_RATE_MAX)
351 div++;
352 }
353
354 if (div <= 3)
355 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV2;
356 else if (div <= 7)
357 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV4;
358 else if (div <= 15)
359 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV8;
360 else
361 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV16;
362
363 xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, XADC_ZYNQ_CTL_RESET);
364 xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, 0);
365 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, ~0);
366 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK, xadc->zynq_intmask);
367 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, XADC_ZYNQ_CFG_ENABLE |
368 XADC_ZYNQ_CFG_REDGE | XADC_ZYNQ_CFG_WEDGE |
369 tck_div | XADC_ZYNQ_CFG_IGAP(igap));
370
371 return 0;
372}
373
374static unsigned long xadc_zynq_get_dclk_rate(struct xadc *xadc)
375{
376 unsigned int div;
377 uint32_t val;
378
379 xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &val);
380
381 switch (val & XADC_ZYNQ_CFG_TCKRATE_MASK) {
382 case XADC_ZYNQ_CFG_TCKRATE_DIV4:
383 div = 4;
384 break;
385 case XADC_ZYNQ_CFG_TCKRATE_DIV8:
386 div = 8;
387 break;
388 case XADC_ZYNQ_CFG_TCKRATE_DIV16:
389 div = 16;
390 break;
391 default:
392 div = 2;
393 break;
394 }
395
396 return clk_get_rate(xadc->clk) / div;
397}
398
399static void xadc_zynq_update_alarm(struct xadc *xadc, unsigned int alarm)
400{
401 unsigned long flags;
402 uint32_t status;
403
404 /* Move OT to bit 7 */
405 alarm = ((alarm & 0x08) << 4) | ((alarm & 0xf0) >> 1) | (alarm & 0x07);
406
407 spin_lock_irqsave(&xadc->lock, flags);
408
409 /* Clear previous interrupts if any. */
410 xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
411 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status & alarm);
412
413 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_ALARM_MASK,
414 ~alarm & XADC_ZYNQ_INT_ALARM_MASK);
415
416 spin_unlock_irqrestore(&xadc->lock, flags);
417}
418
419static const struct xadc_ops xadc_zynq_ops = {
420 .read = xadc_zynq_read_adc_reg,
421 .write = xadc_zynq_write_adc_reg,
422 .setup = xadc_zynq_setup,
423 .get_dclk_rate = xadc_zynq_get_dclk_rate,
424 .interrupt_handler = xadc_zynq_interrupt_handler,
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000425 .update_alarm = xadc_zynq_update_alarm,
426};
427
428static int xadc_axi_read_adc_reg(struct xadc *xadc, unsigned int reg,
429 uint16_t *val)
430{
431 uint32_t val32;
432
433 xadc_read_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, &val32);
434 *val = val32 & 0xffff;
435
436 return 0;
437}
438
439static int xadc_axi_write_adc_reg(struct xadc *xadc, unsigned int reg,
440 uint16_t val)
441{
442 xadc_write_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, val);
443
444 return 0;
445}
446
447static int xadc_axi_setup(struct platform_device *pdev,
448 struct iio_dev *indio_dev, int irq)
449{
450 struct xadc *xadc = iio_priv(indio_dev);
451
452 xadc_write_reg(xadc, XADC_AXI_REG_RESET, XADC_AXI_RESET_MAGIC);
453 xadc_write_reg(xadc, XADC_AXI_REG_GIER, XADC_AXI_GIER_ENABLE);
454
455 return 0;
456}
457
458static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid)
459{
460 struct iio_dev *indio_dev = devid;
461 struct xadc *xadc = iio_priv(indio_dev);
462 uint32_t status, mask;
463 unsigned int events;
464
465 xadc_read_reg(xadc, XADC_AXI_REG_IPISR, &status);
466 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &mask);
467 status &= mask;
468
469 if (!status)
470 return IRQ_NONE;
471
472 if ((status & XADC_AXI_INT_EOS) && xadc->trigger)
Peter Meerwald398fd222014-12-06 06:46:00 +0000473 iio_trigger_poll(xadc->trigger);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000474
475 if (status & XADC_AXI_INT_ALARM_MASK) {
476 /*
477 * The order of the bits in the AXI-XADC status register does
478 * not match the order of the bits in the XADC alarm enable
479 * register. xadc_handle_events() expects the events to be in
480 * the same order as the XADC alarm enable register.
481 */
482 events = (status & 0x000e) >> 1;
483 events |= (status & 0x0001) << 3;
484 events |= (status & 0x3c00) >> 6;
485 xadc_handle_events(indio_dev, events);
486 }
487
488 xadc_write_reg(xadc, XADC_AXI_REG_IPISR, status);
489
490 return IRQ_HANDLED;
491}
492
493static void xadc_axi_update_alarm(struct xadc *xadc, unsigned int alarm)
494{
495 uint32_t val;
496 unsigned long flags;
497
498 /*
499 * The order of the bits in the AXI-XADC status register does not match
500 * the order of the bits in the XADC alarm enable register. We get
501 * passed the alarm mask in the same order as in the XADC alarm enable
502 * register.
503 */
504 alarm = ((alarm & 0x07) << 1) | ((alarm & 0x08) >> 3) |
505 ((alarm & 0xf0) << 6);
506
507 spin_lock_irqsave(&xadc->lock, flags);
508 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
509 val &= ~XADC_AXI_INT_ALARM_MASK;
510 val |= alarm;
511 xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
512 spin_unlock_irqrestore(&xadc->lock, flags);
513}
514
515static unsigned long xadc_axi_get_dclk(struct xadc *xadc)
516{
517 return clk_get_rate(xadc->clk);
518}
519
520static const struct xadc_ops xadc_axi_ops = {
521 .read = xadc_axi_read_adc_reg,
522 .write = xadc_axi_write_adc_reg,
523 .setup = xadc_axi_setup,
524 .get_dclk_rate = xadc_axi_get_dclk,
525 .update_alarm = xadc_axi_update_alarm,
526 .interrupt_handler = xadc_axi_interrupt_handler,
527 .flags = XADC_FLAGS_BUFFERED,
528};
529
530static int _xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
531 uint16_t mask, uint16_t val)
532{
533 uint16_t tmp;
534 int ret;
535
536 ret = _xadc_read_adc_reg(xadc, reg, &tmp);
537 if (ret)
538 return ret;
539
540 return _xadc_write_adc_reg(xadc, reg, (tmp & ~mask) | val);
541}
542
543static int xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
544 uint16_t mask, uint16_t val)
545{
546 int ret;
547
548 mutex_lock(&xadc->mutex);
549 ret = _xadc_update_adc_reg(xadc, reg, mask, val);
550 mutex_unlock(&xadc->mutex);
551
552 return ret;
553}
554
555static unsigned long xadc_get_dclk_rate(struct xadc *xadc)
556{
557 return xadc->ops->get_dclk_rate(xadc);
558}
559
560static int xadc_update_scan_mode(struct iio_dev *indio_dev,
561 const unsigned long *mask)
562{
563 struct xadc *xadc = iio_priv(indio_dev);
564 unsigned int n;
565
566 n = bitmap_weight(mask, indio_dev->masklength);
567
568 kfree(xadc->data);
569 xadc->data = kcalloc(n, sizeof(*xadc->data), GFP_KERNEL);
570 if (!xadc->data)
571 return -ENOMEM;
572
573 return 0;
574}
575
576static unsigned int xadc_scan_index_to_channel(unsigned int scan_index)
577{
578 switch (scan_index) {
579 case 5:
580 return XADC_REG_VCCPINT;
581 case 6:
582 return XADC_REG_VCCPAUX;
583 case 7:
584 return XADC_REG_VCCO_DDR;
585 case 8:
586 return XADC_REG_TEMP;
587 case 9:
588 return XADC_REG_VCCINT;
589 case 10:
590 return XADC_REG_VCCAUX;
591 case 11:
592 return XADC_REG_VPVN;
593 case 12:
594 return XADC_REG_VREFP;
595 case 13:
596 return XADC_REG_VREFN;
597 case 14:
598 return XADC_REG_VCCBRAM;
599 default:
600 return XADC_REG_VAUX(scan_index - 16);
601 }
602}
603
604static irqreturn_t xadc_trigger_handler(int irq, void *p)
605{
606 struct iio_poll_func *pf = p;
607 struct iio_dev *indio_dev = pf->indio_dev;
608 struct xadc *xadc = iio_priv(indio_dev);
609 unsigned int chan;
610 int i, j;
611
612 if (!xadc->data)
613 goto out;
614
615 j = 0;
616 for_each_set_bit(i, indio_dev->active_scan_mask,
617 indio_dev->masklength) {
618 chan = xadc_scan_index_to_channel(i);
619 xadc_read_adc_reg(xadc, chan, &xadc->data[j]);
620 j++;
621 }
622
623 iio_push_to_buffers(indio_dev, xadc->data);
624
625out:
626 iio_trigger_notify_done(indio_dev->trig);
627
628 return IRQ_HANDLED;
629}
630
631static int xadc_trigger_set_state(struct iio_trigger *trigger, bool state)
632{
633 struct xadc *xadc = iio_trigger_get_drvdata(trigger);
634 unsigned long flags;
635 unsigned int convst;
636 unsigned int val;
637 int ret = 0;
638
639 mutex_lock(&xadc->mutex);
640
641 if (state) {
642 /* Only one of the two triggers can be active at the a time. */
643 if (xadc->trigger != NULL) {
644 ret = -EBUSY;
645 goto err_out;
646 } else {
647 xadc->trigger = trigger;
648 if (trigger == xadc->convst_trigger)
649 convst = XADC_CONF0_EC;
650 else
651 convst = 0;
652 }
653 ret = _xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF0_EC,
654 convst);
655 if (ret)
656 goto err_out;
657 } else {
658 xadc->trigger = NULL;
659 }
660
661 spin_lock_irqsave(&xadc->lock, flags);
662 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
663 xadc_write_reg(xadc, XADC_AXI_REG_IPISR, val & XADC_AXI_INT_EOS);
664 if (state)
665 val |= XADC_AXI_INT_EOS;
666 else
667 val &= ~XADC_AXI_INT_EOS;
668 xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
669 spin_unlock_irqrestore(&xadc->lock, flags);
670
671err_out:
672 mutex_unlock(&xadc->mutex);
673
674 return ret;
675}
676
677static const struct iio_trigger_ops xadc_trigger_ops = {
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000678 .set_trigger_state = &xadc_trigger_set_state,
679};
680
681static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev,
682 const char *name)
683{
684 struct iio_trigger *trig;
685 int ret;
686
687 trig = iio_trigger_alloc("%s%d-%s", indio_dev->name,
688 indio_dev->id, name);
689 if (trig == NULL)
690 return ERR_PTR(-ENOMEM);
691
692 trig->dev.parent = indio_dev->dev.parent;
693 trig->ops = &xadc_trigger_ops;
694 iio_trigger_set_drvdata(trig, iio_priv(indio_dev));
695
696 ret = iio_trigger_register(trig);
697 if (ret)
698 goto error_free_trig;
699
700 return trig;
701
702error_free_trig:
703 iio_trigger_free(trig);
704 return ERR_PTR(ret);
705}
706
707static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode)
708{
709 uint16_t val;
710
711 switch (seq_mode) {
712 case XADC_CONF1_SEQ_SIMULTANEOUS:
713 case XADC_CONF1_SEQ_INDEPENDENT:
714 val = XADC_CONF2_PD_ADC_B;
715 break;
716 default:
717 val = 0;
718 break;
719 }
720
721 return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_PD_MASK,
722 val);
723}
724
725static int xadc_get_seq_mode(struct xadc *xadc, unsigned long scan_mode)
726{
727 unsigned int aux_scan_mode = scan_mode >> 16;
728
729 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_DUAL)
730 return XADC_CONF1_SEQ_SIMULTANEOUS;
731
732 if ((aux_scan_mode & 0xff00) == 0 ||
733 (aux_scan_mode & 0x00ff) == 0)
734 return XADC_CONF1_SEQ_CONTINUOUS;
735
736 return XADC_CONF1_SEQ_SIMULTANEOUS;
737}
738
739static int xadc_postdisable(struct iio_dev *indio_dev)
740{
741 struct xadc *xadc = iio_priv(indio_dev);
742 unsigned long scan_mask;
743 int ret;
744 int i;
745
746 scan_mask = 1; /* Run calibration as part of the sequence */
747 for (i = 0; i < indio_dev->num_channels; i++)
748 scan_mask |= BIT(indio_dev->channels[i].scan_index);
749
750 /* Enable all channels and calibration */
751 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
752 if (ret)
753 return ret;
754
755 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
756 if (ret)
757 return ret;
758
759 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
760 XADC_CONF1_SEQ_CONTINUOUS);
761 if (ret)
762 return ret;
763
764 return xadc_power_adc_b(xadc, XADC_CONF1_SEQ_CONTINUOUS);
765}
766
767static int xadc_preenable(struct iio_dev *indio_dev)
768{
769 struct xadc *xadc = iio_priv(indio_dev);
770 unsigned long scan_mask;
771 int seq_mode;
772 int ret;
773
774 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
775 XADC_CONF1_SEQ_DEFAULT);
776 if (ret)
777 goto err;
778
779 scan_mask = *indio_dev->active_scan_mask;
780 seq_mode = xadc_get_seq_mode(xadc, scan_mask);
781
782 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
783 if (ret)
784 goto err;
785
786 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
787 if (ret)
788 goto err;
789
790 ret = xadc_power_adc_b(xadc, seq_mode);
791 if (ret)
792 goto err;
793
794 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
795 seq_mode);
796 if (ret)
797 goto err;
798
799 return 0;
800err:
801 xadc_postdisable(indio_dev);
802 return ret;
803}
804
Julia Lawallcef7e122015-11-15 21:00:02 +0100805static const struct iio_buffer_setup_ops xadc_buffer_ops = {
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000806 .preenable = &xadc_preenable,
807 .postenable = &iio_triggered_buffer_postenable,
808 .predisable = &iio_triggered_buffer_predisable,
809 .postdisable = &xadc_postdisable,
810};
811
812static int xadc_read_raw(struct iio_dev *indio_dev,
813 struct iio_chan_spec const *chan, int *val, int *val2, long info)
814{
815 struct xadc *xadc = iio_priv(indio_dev);
816 unsigned int div;
817 uint16_t val16;
818 int ret;
819
820 switch (info) {
821 case IIO_CHAN_INFO_RAW:
822 if (iio_buffer_enabled(indio_dev))
823 return -EBUSY;
824 ret = xadc_read_adc_reg(xadc, chan->address, &val16);
825 if (ret < 0)
826 return ret;
827
828 val16 >>= 4;
829 if (chan->scan_type.sign == 'u')
830 *val = val16;
831 else
832 *val = sign_extend32(val16, 11);
833
834 return IIO_VAL_INT;
835 case IIO_CHAN_INFO_SCALE:
836 switch (chan->type) {
837 case IIO_VOLTAGE:
838 /* V = (val * 3.0) / 4096 */
839 switch (chan->address) {
840 case XADC_REG_VCCINT:
841 case XADC_REG_VCCAUX:
Thomas Betker00db4e52015-04-15 21:11:49 +0200842 case XADC_REG_VREFP:
Thomas Betkera57f8da2015-11-11 21:24:38 +0100843 case XADC_REG_VREFN:
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000844 case XADC_REG_VCCBRAM:
845 case XADC_REG_VCCPINT:
846 case XADC_REG_VCCPAUX:
847 case XADC_REG_VCCO_DDR:
848 *val = 3000;
849 break;
850 default:
851 *val = 1000;
852 break;
853 }
854 *val2 = 12;
855 return IIO_VAL_FRACTIONAL_LOG2;
856 case IIO_TEMP:
857 /* Temp in C = (val * 503.975) / 4096 - 273.15 */
858 *val = 503975;
859 *val2 = 12;
860 return IIO_VAL_FRACTIONAL_LOG2;
861 default:
862 return -EINVAL;
863 }
864 case IIO_CHAN_INFO_OFFSET:
865 /* Only the temperature channel has an offset */
866 *val = -((273150 << 12) / 503975);
867 return IIO_VAL_INT;
868 case IIO_CHAN_INFO_SAMP_FREQ:
869 ret = xadc_read_adc_reg(xadc, XADC_REG_CONF2, &val16);
870 if (ret)
871 return ret;
872
873 div = (val16 & XADC_CONF2_DIV_MASK) >> XADC_CONF2_DIV_OFFSET;
874 if (div < 2)
875 div = 2;
876
877 *val = xadc_get_dclk_rate(xadc) / div / 26;
878
879 return IIO_VAL_INT;
880 default:
881 return -EINVAL;
882 }
883}
884
885static int xadc_write_raw(struct iio_dev *indio_dev,
886 struct iio_chan_spec const *chan, int val, int val2, long info)
887{
888 struct xadc *xadc = iio_priv(indio_dev);
889 unsigned long clk_rate = xadc_get_dclk_rate(xadc);
890 unsigned int div;
891
Manish Narani0a846092018-07-23 20:32:00 +0530892 if (!clk_rate)
893 return -EINVAL;
894
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000895 if (info != IIO_CHAN_INFO_SAMP_FREQ)
896 return -EINVAL;
897
898 if (val <= 0)
899 return -EINVAL;
900
901 /* Max. 150 kSPS */
902 if (val > 150000)
903 val = 150000;
904
905 val *= 26;
906
907 /* Min 1MHz */
908 if (val < 1000000)
909 val = 1000000;
910
911 /*
912 * We want to round down, but only if we do not exceed the 150 kSPS
913 * limit.
914 */
915 div = clk_rate / val;
916 if (clk_rate / div / 26 > 150000)
917 div++;
918 if (div < 2)
919 div = 2;
920 else if (div > 0xff)
921 div = 0xff;
922
923 return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_DIV_MASK,
924 div << XADC_CONF2_DIV_OFFSET);
925}
926
927static const struct iio_event_spec xadc_temp_events[] = {
928 {
929 .type = IIO_EV_TYPE_THRESH,
930 .dir = IIO_EV_DIR_RISING,
931 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
932 BIT(IIO_EV_INFO_VALUE) |
933 BIT(IIO_EV_INFO_HYSTERESIS),
934 },
935};
936
937/* Separate values for upper and lower thresholds, but only a shared enabled */
938static const struct iio_event_spec xadc_voltage_events[] = {
939 {
940 .type = IIO_EV_TYPE_THRESH,
941 .dir = IIO_EV_DIR_RISING,
942 .mask_separate = BIT(IIO_EV_INFO_VALUE),
943 }, {
944 .type = IIO_EV_TYPE_THRESH,
945 .dir = IIO_EV_DIR_FALLING,
946 .mask_separate = BIT(IIO_EV_INFO_VALUE),
947 }, {
948 .type = IIO_EV_TYPE_THRESH,
949 .dir = IIO_EV_DIR_EITHER,
950 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
951 },
952};
953
954#define XADC_CHAN_TEMP(_chan, _scan_index, _addr) { \
955 .type = IIO_TEMP, \
956 .indexed = 1, \
957 .channel = (_chan), \
958 .address = (_addr), \
959 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
960 BIT(IIO_CHAN_INFO_SCALE) | \
961 BIT(IIO_CHAN_INFO_OFFSET), \
962 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
963 .event_spec = xadc_temp_events, \
964 .num_event_specs = ARRAY_SIZE(xadc_temp_events), \
965 .scan_index = (_scan_index), \
966 .scan_type = { \
967 .sign = 'u', \
968 .realbits = 12, \
969 .storagebits = 16, \
970 .shift = 4, \
971 .endianness = IIO_CPU, \
972 }, \
973}
974
975#define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) { \
976 .type = IIO_VOLTAGE, \
977 .indexed = 1, \
978 .channel = (_chan), \
979 .address = (_addr), \
980 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
981 BIT(IIO_CHAN_INFO_SCALE), \
982 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
983 .event_spec = (_alarm) ? xadc_voltage_events : NULL, \
984 .num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \
985 .scan_index = (_scan_index), \
986 .scan_type = { \
Thomas Betker97ffae12015-04-15 21:11:50 +0200987 .sign = ((_addr) == XADC_REG_VREFN) ? 's' : 'u', \
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000988 .realbits = 12, \
989 .storagebits = 16, \
990 .shift = 4, \
991 .endianness = IIO_CPU, \
992 }, \
993 .extend_name = _ext, \
994}
995
996static const struct iio_chan_spec xadc_channels[] = {
997 XADC_CHAN_TEMP(0, 8, XADC_REG_TEMP),
998 XADC_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true),
Thomas Betkerd6c96c42015-04-15 21:11:48 +0200999 XADC_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX, "vccaux", true),
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001000 XADC_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true),
1001 XADC_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpint", true),
1002 XADC_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpaux", true),
1003 XADC_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccoddr", true),
1004 XADC_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false),
1005 XADC_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false),
1006 XADC_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false),
1007 XADC_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false),
1008 XADC_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false),
1009 XADC_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false),
1010 XADC_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false),
1011 XADC_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false),
1012 XADC_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false),
1013 XADC_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false),
1014 XADC_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false),
1015 XADC_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false),
1016 XADC_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false),
1017 XADC_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false),
1018 XADC_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false),
1019 XADC_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false),
1020 XADC_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false),
1021 XADC_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false),
1022 XADC_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false),
1023};
1024
1025static const struct iio_info xadc_info = {
1026 .read_raw = &xadc_read_raw,
1027 .write_raw = &xadc_write_raw,
1028 .read_event_config = &xadc_read_event_config,
1029 .write_event_config = &xadc_write_event_config,
1030 .read_event_value = &xadc_read_event_value,
1031 .write_event_value = &xadc_write_event_value,
1032 .update_scan_mode = &xadc_update_scan_mode,
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001033};
1034
1035static const struct of_device_id xadc_of_match_table[] = {
1036 { .compatible = "xlnx,zynq-xadc-1.00.a", (void *)&xadc_zynq_ops },
1037 { .compatible = "xlnx,axi-xadc-1.00.a", (void *)&xadc_axi_ops },
1038 { },
1039};
1040MODULE_DEVICE_TABLE(of, xadc_of_match_table);
1041
1042static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np,
1043 unsigned int *conf)
1044{
1045 struct xadc *xadc = iio_priv(indio_dev);
1046 struct iio_chan_spec *channels, *chan;
1047 struct device_node *chan_node, *child;
1048 unsigned int num_channels;
1049 const char *external_mux;
1050 u32 ext_mux_chan;
Manish Naranic344a322018-07-18 16:42:09 +05301051 u32 reg;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001052 int ret;
1053
1054 *conf = 0;
1055
1056 ret = of_property_read_string(np, "xlnx,external-mux", &external_mux);
1057 if (ret < 0 || strcasecmp(external_mux, "none") == 0)
1058 xadc->external_mux_mode = XADC_EXTERNAL_MUX_NONE;
1059 else if (strcasecmp(external_mux, "single") == 0)
1060 xadc->external_mux_mode = XADC_EXTERNAL_MUX_SINGLE;
1061 else if (strcasecmp(external_mux, "dual") == 0)
1062 xadc->external_mux_mode = XADC_EXTERNAL_MUX_DUAL;
1063 else
1064 return -EINVAL;
1065
1066 if (xadc->external_mux_mode != XADC_EXTERNAL_MUX_NONE) {
1067 ret = of_property_read_u32(np, "xlnx,external-mux-channel",
1068 &ext_mux_chan);
1069 if (ret < 0)
1070 return ret;
1071
1072 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_SINGLE) {
1073 if (ext_mux_chan == 0)
1074 ext_mux_chan = XADC_REG_VPVN;
1075 else if (ext_mux_chan <= 16)
1076 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1077 else
1078 return -EINVAL;
1079 } else {
1080 if (ext_mux_chan > 0 && ext_mux_chan <= 8)
1081 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1082 else
1083 return -EINVAL;
1084 }
1085
1086 *conf |= XADC_CONF0_MUX | XADC_CONF0_CHAN(ext_mux_chan);
1087 }
1088
1089 channels = kmemdup(xadc_channels, sizeof(xadc_channels), GFP_KERNEL);
1090 if (!channels)
1091 return -ENOMEM;
1092
1093 num_channels = 9;
1094 chan = &channels[9];
1095
1096 chan_node = of_get_child_by_name(np, "xlnx,channels");
1097 if (chan_node) {
1098 for_each_child_of_node(chan_node, child) {
1099 if (num_channels >= ARRAY_SIZE(xadc_channels)) {
1100 of_node_put(child);
1101 break;
1102 }
1103
1104 ret = of_property_read_u32(child, "reg", &reg);
1105 if (ret || reg > 16)
1106 continue;
1107
1108 if (of_property_read_bool(child, "xlnx,bipolar"))
1109 chan->scan_type.sign = 's';
1110
1111 if (reg == 0) {
1112 chan->scan_index = 11;
1113 chan->address = XADC_REG_VPVN;
1114 } else {
1115 chan->scan_index = 15 + reg;
Subbaraya Sundeep Bhatta1887e722014-11-09 09:55:00 +00001116 chan->address = XADC_REG_VAUX(reg - 1);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001117 }
1118 num_channels++;
1119 chan++;
1120 }
1121 }
1122 of_node_put(chan_node);
1123
1124 indio_dev->num_channels = num_channels;
1125 indio_dev->channels = krealloc(channels, sizeof(*channels) *
1126 num_channels, GFP_KERNEL);
1127 /* If we can't resize the channels array, just use the original */
1128 if (!indio_dev->channels)
1129 indio_dev->channels = channels;
1130
1131 return 0;
1132}
1133
1134static int xadc_probe(struct platform_device *pdev)
1135{
1136 const struct of_device_id *id;
1137 struct iio_dev *indio_dev;
1138 unsigned int bipolar_mask;
1139 struct resource *mem;
1140 unsigned int conf0;
1141 struct xadc *xadc;
1142 int ret;
1143 int irq;
1144 int i;
1145
1146 if (!pdev->dev.of_node)
1147 return -ENODEV;
1148
1149 id = of_match_node(xadc_of_match_table, pdev->dev.of_node);
1150 if (!id)
1151 return -EINVAL;
1152
1153 irq = platform_get_irq(pdev, 0);
1154 if (irq <= 0)
1155 return -ENXIO;
1156
1157 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*xadc));
1158 if (!indio_dev)
1159 return -ENOMEM;
1160
1161 xadc = iio_priv(indio_dev);
1162 xadc->ops = id->data;
1163 init_completion(&xadc->completion);
1164 mutex_init(&xadc->mutex);
1165 spin_lock_init(&xadc->lock);
1166 INIT_DELAYED_WORK(&xadc->zynq_unmask_work, xadc_zynq_unmask_worker);
1167
1168 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1169 xadc->base = devm_ioremap_resource(&pdev->dev, mem);
1170 if (IS_ERR(xadc->base))
1171 return PTR_ERR(xadc->base);
1172
1173 indio_dev->dev.parent = &pdev->dev;
1174 indio_dev->dev.of_node = pdev->dev.of_node;
1175 indio_dev->name = "xadc";
1176 indio_dev->modes = INDIO_DIRECT_MODE;
1177 indio_dev->info = &xadc_info;
1178
1179 ret = xadc_parse_dt(indio_dev, pdev->dev.of_node, &conf0);
1180 if (ret)
1181 goto err_device_free;
1182
1183 if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1184 ret = iio_triggered_buffer_setup(indio_dev,
1185 &iio_pollfunc_store_time, &xadc_trigger_handler,
1186 &xadc_buffer_ops);
1187 if (ret)
1188 goto err_device_free;
1189
1190 xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst");
Julia Lawall889c5582014-06-08 21:12:00 +01001191 if (IS_ERR(xadc->convst_trigger)) {
1192 ret = PTR_ERR(xadc->convst_trigger);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001193 goto err_triggered_buffer_cleanup;
Julia Lawall889c5582014-06-08 21:12:00 +01001194 }
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001195 xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev,
1196 "samplerate");
Julia Lawall889c5582014-06-08 21:12:00 +01001197 if (IS_ERR(xadc->samplerate_trigger)) {
1198 ret = PTR_ERR(xadc->samplerate_trigger);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001199 goto err_free_convst_trigger;
Julia Lawall889c5582014-06-08 21:12:00 +01001200 }
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001201 }
1202
1203 xadc->clk = devm_clk_get(&pdev->dev, NULL);
1204 if (IS_ERR(xadc->clk)) {
1205 ret = PTR_ERR(xadc->clk);
1206 goto err_free_samplerate_trigger;
1207 }
Arvind Yadav0a178fd2017-05-26 12:07:41 +05301208
1209 ret = clk_prepare_enable(xadc->clk);
1210 if (ret)
1211 goto err_free_samplerate_trigger;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001212
1213 ret = xadc->ops->setup(pdev, indio_dev, irq);
1214 if (ret)
Christophe JAILLETca1c39e2017-02-21 07:34:00 +01001215 goto err_clk_disable_unprepare;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001216
Xander Huff70581e02015-08-11 18:00:49 -05001217 ret = request_irq(irq, xadc->ops->interrupt_handler, 0,
1218 dev_name(&pdev->dev), indio_dev);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001219 if (ret)
1220 goto err_clk_disable_unprepare;
1221
1222 for (i = 0; i < 16; i++)
1223 xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1224 &xadc->threshold[i]);
1225
1226 ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0);
1227 if (ret)
1228 goto err_free_irq;
1229
1230 bipolar_mask = 0;
1231 for (i = 0; i < indio_dev->num_channels; i++) {
1232 if (indio_dev->channels[i].scan_type.sign == 's')
1233 bipolar_mask |= BIT(indio_dev->channels[i].scan_index);
1234 }
1235
1236 ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask);
1237 if (ret)
1238 goto err_free_irq;
1239 ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1),
1240 bipolar_mask >> 16);
1241 if (ret)
1242 goto err_free_irq;
1243
1244 /* Disable all alarms */
Manish Narani0a846092018-07-23 20:32:00 +05301245 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_ALARM_MASK,
1246 XADC_CONF1_ALARM_MASK);
1247 if (ret)
1248 goto err_free_irq;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001249
1250 /* Set thresholds to min/max */
1251 for (i = 0; i < 16; i++) {
1252 /*
1253 * Set max voltage threshold and both temperature thresholds to
1254 * 0xffff, min voltage threshold to 0.
1255 */
1256 if (i % 8 < 4 || i == 7)
1257 xadc->threshold[i] = 0xffff;
1258 else
1259 xadc->threshold[i] = 0;
1260 xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1261 xadc->threshold[i]);
1262 }
1263
1264 /* Go to non-buffered mode */
1265 xadc_postdisable(indio_dev);
1266
1267 ret = iio_device_register(indio_dev);
1268 if (ret)
1269 goto err_free_irq;
1270
1271 platform_set_drvdata(pdev, indio_dev);
1272
1273 return 0;
1274
1275err_free_irq:
1276 free_irq(irq, indio_dev);
Christophe JAILLETca1c39e2017-02-21 07:34:00 +01001277err_clk_disable_unprepare:
1278 clk_disable_unprepare(xadc->clk);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001279err_free_samplerate_trigger:
1280 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1281 iio_trigger_free(xadc->samplerate_trigger);
1282err_free_convst_trigger:
1283 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1284 iio_trigger_free(xadc->convst_trigger);
1285err_triggered_buffer_cleanup:
1286 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1287 iio_triggered_buffer_cleanup(indio_dev);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001288err_device_free:
1289 kfree(indio_dev->channels);
1290
1291 return ret;
1292}
1293
1294static int xadc_remove(struct platform_device *pdev)
1295{
1296 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1297 struct xadc *xadc = iio_priv(indio_dev);
1298 int irq = platform_get_irq(pdev, 0);
1299
1300 iio_device_unregister(indio_dev);
1301 if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1302 iio_trigger_free(xadc->samplerate_trigger);
1303 iio_trigger_free(xadc->convst_trigger);
1304 iio_triggered_buffer_cleanup(indio_dev);
1305 }
1306 free_irq(irq, indio_dev);
1307 clk_disable_unprepare(xadc->clk);
1308 cancel_delayed_work(&xadc->zynq_unmask_work);
1309 kfree(xadc->data);
1310 kfree(indio_dev->channels);
1311
1312 return 0;
1313}
1314
1315static struct platform_driver xadc_driver = {
1316 .probe = xadc_probe,
1317 .remove = xadc_remove,
1318 .driver = {
1319 .name = "xadc",
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001320 .of_match_table = xadc_of_match_table,
1321 },
1322};
1323module_platform_driver(xadc_driver);
1324
1325MODULE_LICENSE("GPL v2");
1326MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1327MODULE_DESCRIPTION("Xilinx XADC IIO driver");