blob: 0127e8513166748e6804b850921079a67cb300ff [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);
343
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000344 if (tck_rate > pcap_rate / 2) {
345 div = 2;
346 } else {
347 div = pcap_rate / tck_rate;
348 if (pcap_rate / div > XADC_ZYNQ_TCK_RATE_MAX)
349 div++;
350 }
351
352 if (div <= 3)
353 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV2;
354 else if (div <= 7)
355 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV4;
356 else if (div <= 15)
357 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV8;
358 else
359 tck_div = XADC_ZYNQ_CFG_TCKRATE_DIV16;
360
361 xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, XADC_ZYNQ_CTL_RESET);
362 xadc_write_reg(xadc, XADC_ZYNQ_REG_CTL, 0);
363 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, ~0);
364 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTMSK, xadc->zynq_intmask);
365 xadc_write_reg(xadc, XADC_ZYNQ_REG_CFG, XADC_ZYNQ_CFG_ENABLE |
366 XADC_ZYNQ_CFG_REDGE | XADC_ZYNQ_CFG_WEDGE |
367 tck_div | XADC_ZYNQ_CFG_IGAP(igap));
368
369 return 0;
370}
371
372static unsigned long xadc_zynq_get_dclk_rate(struct xadc *xadc)
373{
374 unsigned int div;
375 uint32_t val;
376
377 xadc_read_reg(xadc, XADC_ZYNQ_REG_CFG, &val);
378
379 switch (val & XADC_ZYNQ_CFG_TCKRATE_MASK) {
380 case XADC_ZYNQ_CFG_TCKRATE_DIV4:
381 div = 4;
382 break;
383 case XADC_ZYNQ_CFG_TCKRATE_DIV8:
384 div = 8;
385 break;
386 case XADC_ZYNQ_CFG_TCKRATE_DIV16:
387 div = 16;
388 break;
389 default:
390 div = 2;
391 break;
392 }
393
394 return clk_get_rate(xadc->clk) / div;
395}
396
397static void xadc_zynq_update_alarm(struct xadc *xadc, unsigned int alarm)
398{
399 unsigned long flags;
400 uint32_t status;
401
402 /* Move OT to bit 7 */
403 alarm = ((alarm & 0x08) << 4) | ((alarm & 0xf0) >> 1) | (alarm & 0x07);
404
405 spin_lock_irqsave(&xadc->lock, flags);
406
407 /* Clear previous interrupts if any. */
408 xadc_read_reg(xadc, XADC_ZYNQ_REG_INTSTS, &status);
409 xadc_write_reg(xadc, XADC_ZYNQ_REG_INTSTS, status & alarm);
410
411 xadc_zynq_update_intmsk(xadc, XADC_ZYNQ_INT_ALARM_MASK,
412 ~alarm & XADC_ZYNQ_INT_ALARM_MASK);
413
414 spin_unlock_irqrestore(&xadc->lock, flags);
415}
416
417static const struct xadc_ops xadc_zynq_ops = {
418 .read = xadc_zynq_read_adc_reg,
419 .write = xadc_zynq_write_adc_reg,
420 .setup = xadc_zynq_setup,
421 .get_dclk_rate = xadc_zynq_get_dclk_rate,
422 .interrupt_handler = xadc_zynq_interrupt_handler,
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000423 .update_alarm = xadc_zynq_update_alarm,
424};
425
426static int xadc_axi_read_adc_reg(struct xadc *xadc, unsigned int reg,
427 uint16_t *val)
428{
429 uint32_t val32;
430
431 xadc_read_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, &val32);
432 *val = val32 & 0xffff;
433
434 return 0;
435}
436
437static int xadc_axi_write_adc_reg(struct xadc *xadc, unsigned int reg,
438 uint16_t val)
439{
440 xadc_write_reg(xadc, XADC_AXI_ADC_REG_OFFSET + reg * 4, val);
441
442 return 0;
443}
444
445static int xadc_axi_setup(struct platform_device *pdev,
446 struct iio_dev *indio_dev, int irq)
447{
448 struct xadc *xadc = iio_priv(indio_dev);
449
450 xadc_write_reg(xadc, XADC_AXI_REG_RESET, XADC_AXI_RESET_MAGIC);
451 xadc_write_reg(xadc, XADC_AXI_REG_GIER, XADC_AXI_GIER_ENABLE);
452
453 return 0;
454}
455
456static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid)
457{
458 struct iio_dev *indio_dev = devid;
459 struct xadc *xadc = iio_priv(indio_dev);
460 uint32_t status, mask;
461 unsigned int events;
462
463 xadc_read_reg(xadc, XADC_AXI_REG_IPISR, &status);
464 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &mask);
465 status &= mask;
466
467 if (!status)
468 return IRQ_NONE;
469
470 if ((status & XADC_AXI_INT_EOS) && xadc->trigger)
Peter Meerwald398fd222014-12-06 06:46:00 +0000471 iio_trigger_poll(xadc->trigger);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000472
473 if (status & XADC_AXI_INT_ALARM_MASK) {
474 /*
475 * The order of the bits in the AXI-XADC status register does
476 * not match the order of the bits in the XADC alarm enable
477 * register. xadc_handle_events() expects the events to be in
478 * the same order as the XADC alarm enable register.
479 */
480 events = (status & 0x000e) >> 1;
481 events |= (status & 0x0001) << 3;
482 events |= (status & 0x3c00) >> 6;
483 xadc_handle_events(indio_dev, events);
484 }
485
486 xadc_write_reg(xadc, XADC_AXI_REG_IPISR, status);
487
488 return IRQ_HANDLED;
489}
490
491static void xadc_axi_update_alarm(struct xadc *xadc, unsigned int alarm)
492{
493 uint32_t val;
494 unsigned long flags;
495
496 /*
497 * The order of the bits in the AXI-XADC status register does not match
498 * the order of the bits in the XADC alarm enable register. We get
499 * passed the alarm mask in the same order as in the XADC alarm enable
500 * register.
501 */
502 alarm = ((alarm & 0x07) << 1) | ((alarm & 0x08) >> 3) |
503 ((alarm & 0xf0) << 6);
504
505 spin_lock_irqsave(&xadc->lock, flags);
506 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
507 val &= ~XADC_AXI_INT_ALARM_MASK;
508 val |= alarm;
509 xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
510 spin_unlock_irqrestore(&xadc->lock, flags);
511}
512
513static unsigned long xadc_axi_get_dclk(struct xadc *xadc)
514{
515 return clk_get_rate(xadc->clk);
516}
517
518static const struct xadc_ops xadc_axi_ops = {
519 .read = xadc_axi_read_adc_reg,
520 .write = xadc_axi_write_adc_reg,
521 .setup = xadc_axi_setup,
522 .get_dclk_rate = xadc_axi_get_dclk,
523 .update_alarm = xadc_axi_update_alarm,
524 .interrupt_handler = xadc_axi_interrupt_handler,
525 .flags = XADC_FLAGS_BUFFERED,
526};
527
528static int _xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
529 uint16_t mask, uint16_t val)
530{
531 uint16_t tmp;
532 int ret;
533
534 ret = _xadc_read_adc_reg(xadc, reg, &tmp);
535 if (ret)
536 return ret;
537
538 return _xadc_write_adc_reg(xadc, reg, (tmp & ~mask) | val);
539}
540
541static int xadc_update_adc_reg(struct xadc *xadc, unsigned int reg,
542 uint16_t mask, uint16_t val)
543{
544 int ret;
545
546 mutex_lock(&xadc->mutex);
547 ret = _xadc_update_adc_reg(xadc, reg, mask, val);
548 mutex_unlock(&xadc->mutex);
549
550 return ret;
551}
552
553static unsigned long xadc_get_dclk_rate(struct xadc *xadc)
554{
555 return xadc->ops->get_dclk_rate(xadc);
556}
557
558static int xadc_update_scan_mode(struct iio_dev *indio_dev,
559 const unsigned long *mask)
560{
561 struct xadc *xadc = iio_priv(indio_dev);
562 unsigned int n;
563
564 n = bitmap_weight(mask, indio_dev->masklength);
565
566 kfree(xadc->data);
567 xadc->data = kcalloc(n, sizeof(*xadc->data), GFP_KERNEL);
568 if (!xadc->data)
569 return -ENOMEM;
570
571 return 0;
572}
573
574static unsigned int xadc_scan_index_to_channel(unsigned int scan_index)
575{
576 switch (scan_index) {
577 case 5:
578 return XADC_REG_VCCPINT;
579 case 6:
580 return XADC_REG_VCCPAUX;
581 case 7:
582 return XADC_REG_VCCO_DDR;
583 case 8:
584 return XADC_REG_TEMP;
585 case 9:
586 return XADC_REG_VCCINT;
587 case 10:
588 return XADC_REG_VCCAUX;
589 case 11:
590 return XADC_REG_VPVN;
591 case 12:
592 return XADC_REG_VREFP;
593 case 13:
594 return XADC_REG_VREFN;
595 case 14:
596 return XADC_REG_VCCBRAM;
597 default:
598 return XADC_REG_VAUX(scan_index - 16);
599 }
600}
601
602static irqreturn_t xadc_trigger_handler(int irq, void *p)
603{
604 struct iio_poll_func *pf = p;
605 struct iio_dev *indio_dev = pf->indio_dev;
606 struct xadc *xadc = iio_priv(indio_dev);
607 unsigned int chan;
608 int i, j;
609
610 if (!xadc->data)
611 goto out;
612
613 j = 0;
614 for_each_set_bit(i, indio_dev->active_scan_mask,
615 indio_dev->masklength) {
616 chan = xadc_scan_index_to_channel(i);
617 xadc_read_adc_reg(xadc, chan, &xadc->data[j]);
618 j++;
619 }
620
621 iio_push_to_buffers(indio_dev, xadc->data);
622
623out:
624 iio_trigger_notify_done(indio_dev->trig);
625
626 return IRQ_HANDLED;
627}
628
629static int xadc_trigger_set_state(struct iio_trigger *trigger, bool state)
630{
631 struct xadc *xadc = iio_trigger_get_drvdata(trigger);
632 unsigned long flags;
633 unsigned int convst;
634 unsigned int val;
635 int ret = 0;
636
637 mutex_lock(&xadc->mutex);
638
639 if (state) {
640 /* Only one of the two triggers can be active at the a time. */
641 if (xadc->trigger != NULL) {
642 ret = -EBUSY;
643 goto err_out;
644 } else {
645 xadc->trigger = trigger;
646 if (trigger == xadc->convst_trigger)
647 convst = XADC_CONF0_EC;
648 else
649 convst = 0;
650 }
651 ret = _xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF0_EC,
652 convst);
653 if (ret)
654 goto err_out;
655 } else {
656 xadc->trigger = NULL;
657 }
658
659 spin_lock_irqsave(&xadc->lock, flags);
660 xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val);
661 xadc_write_reg(xadc, XADC_AXI_REG_IPISR, val & XADC_AXI_INT_EOS);
662 if (state)
663 val |= XADC_AXI_INT_EOS;
664 else
665 val &= ~XADC_AXI_INT_EOS;
666 xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val);
667 spin_unlock_irqrestore(&xadc->lock, flags);
668
669err_out:
670 mutex_unlock(&xadc->mutex);
671
672 return ret;
673}
674
675static const struct iio_trigger_ops xadc_trigger_ops = {
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000676 .set_trigger_state = &xadc_trigger_set_state,
677};
678
679static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev,
680 const char *name)
681{
682 struct iio_trigger *trig;
683 int ret;
684
685 trig = iio_trigger_alloc("%s%d-%s", indio_dev->name,
686 indio_dev->id, name);
687 if (trig == NULL)
688 return ERR_PTR(-ENOMEM);
689
690 trig->dev.parent = indio_dev->dev.parent;
691 trig->ops = &xadc_trigger_ops;
692 iio_trigger_set_drvdata(trig, iio_priv(indio_dev));
693
694 ret = iio_trigger_register(trig);
695 if (ret)
696 goto error_free_trig;
697
698 return trig;
699
700error_free_trig:
701 iio_trigger_free(trig);
702 return ERR_PTR(ret);
703}
704
705static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode)
706{
707 uint16_t val;
708
709 switch (seq_mode) {
710 case XADC_CONF1_SEQ_SIMULTANEOUS:
711 case XADC_CONF1_SEQ_INDEPENDENT:
712 val = XADC_CONF2_PD_ADC_B;
713 break;
714 default:
715 val = 0;
716 break;
717 }
718
719 return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_PD_MASK,
720 val);
721}
722
723static int xadc_get_seq_mode(struct xadc *xadc, unsigned long scan_mode)
724{
725 unsigned int aux_scan_mode = scan_mode >> 16;
726
727 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_DUAL)
728 return XADC_CONF1_SEQ_SIMULTANEOUS;
729
730 if ((aux_scan_mode & 0xff00) == 0 ||
731 (aux_scan_mode & 0x00ff) == 0)
732 return XADC_CONF1_SEQ_CONTINUOUS;
733
734 return XADC_CONF1_SEQ_SIMULTANEOUS;
735}
736
737static int xadc_postdisable(struct iio_dev *indio_dev)
738{
739 struct xadc *xadc = iio_priv(indio_dev);
740 unsigned long scan_mask;
741 int ret;
742 int i;
743
744 scan_mask = 1; /* Run calibration as part of the sequence */
745 for (i = 0; i < indio_dev->num_channels; i++)
746 scan_mask |= BIT(indio_dev->channels[i].scan_index);
747
748 /* Enable all channels and calibration */
749 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
750 if (ret)
751 return ret;
752
753 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
754 if (ret)
755 return ret;
756
757 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
758 XADC_CONF1_SEQ_CONTINUOUS);
759 if (ret)
760 return ret;
761
762 return xadc_power_adc_b(xadc, XADC_CONF1_SEQ_CONTINUOUS);
763}
764
765static int xadc_preenable(struct iio_dev *indio_dev)
766{
767 struct xadc *xadc = iio_priv(indio_dev);
768 unsigned long scan_mask;
769 int seq_mode;
770 int ret;
771
772 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
773 XADC_CONF1_SEQ_DEFAULT);
774 if (ret)
775 goto err;
776
777 scan_mask = *indio_dev->active_scan_mask;
778 seq_mode = xadc_get_seq_mode(xadc, scan_mask);
779
780 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff);
781 if (ret)
782 goto err;
783
784 ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16);
785 if (ret)
786 goto err;
787
788 ret = xadc_power_adc_b(xadc, seq_mode);
789 if (ret)
790 goto err;
791
792 ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK,
793 seq_mode);
794 if (ret)
795 goto err;
796
797 return 0;
798err:
799 xadc_postdisable(indio_dev);
800 return ret;
801}
802
Julia Lawallcef7e122015-11-15 21:00:02 +0100803static const struct iio_buffer_setup_ops xadc_buffer_ops = {
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000804 .preenable = &xadc_preenable,
805 .postenable = &iio_triggered_buffer_postenable,
806 .predisable = &iio_triggered_buffer_predisable,
807 .postdisable = &xadc_postdisable,
808};
809
810static int xadc_read_raw(struct iio_dev *indio_dev,
811 struct iio_chan_spec const *chan, int *val, int *val2, long info)
812{
813 struct xadc *xadc = iio_priv(indio_dev);
814 unsigned int div;
815 uint16_t val16;
816 int ret;
817
818 switch (info) {
819 case IIO_CHAN_INFO_RAW:
820 if (iio_buffer_enabled(indio_dev))
821 return -EBUSY;
822 ret = xadc_read_adc_reg(xadc, chan->address, &val16);
823 if (ret < 0)
824 return ret;
825
826 val16 >>= 4;
827 if (chan->scan_type.sign == 'u')
828 *val = val16;
829 else
830 *val = sign_extend32(val16, 11);
831
832 return IIO_VAL_INT;
833 case IIO_CHAN_INFO_SCALE:
834 switch (chan->type) {
835 case IIO_VOLTAGE:
836 /* V = (val * 3.0) / 4096 */
837 switch (chan->address) {
838 case XADC_REG_VCCINT:
839 case XADC_REG_VCCAUX:
Thomas Betker00db4e52015-04-15 21:11:49 +0200840 case XADC_REG_VREFP:
Thomas Betkera57f8da2015-11-11 21:24:38 +0100841 case XADC_REG_VREFN:
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000842 case XADC_REG_VCCBRAM:
843 case XADC_REG_VCCPINT:
844 case XADC_REG_VCCPAUX:
845 case XADC_REG_VCCO_DDR:
846 *val = 3000;
847 break;
848 default:
849 *val = 1000;
850 break;
851 }
852 *val2 = 12;
853 return IIO_VAL_FRACTIONAL_LOG2;
854 case IIO_TEMP:
855 /* Temp in C = (val * 503.975) / 4096 - 273.15 */
856 *val = 503975;
857 *val2 = 12;
858 return IIO_VAL_FRACTIONAL_LOG2;
859 default:
860 return -EINVAL;
861 }
862 case IIO_CHAN_INFO_OFFSET:
863 /* Only the temperature channel has an offset */
864 *val = -((273150 << 12) / 503975);
865 return IIO_VAL_INT;
866 case IIO_CHAN_INFO_SAMP_FREQ:
867 ret = xadc_read_adc_reg(xadc, XADC_REG_CONF2, &val16);
868 if (ret)
869 return ret;
870
871 div = (val16 & XADC_CONF2_DIV_MASK) >> XADC_CONF2_DIV_OFFSET;
872 if (div < 2)
873 div = 2;
874
875 *val = xadc_get_dclk_rate(xadc) / div / 26;
876
877 return IIO_VAL_INT;
878 default:
879 return -EINVAL;
880 }
881}
882
883static int xadc_write_raw(struct iio_dev *indio_dev,
884 struct iio_chan_spec const *chan, int val, int val2, long info)
885{
886 struct xadc *xadc = iio_priv(indio_dev);
887 unsigned long clk_rate = xadc_get_dclk_rate(xadc);
888 unsigned int div;
889
890 if (info != IIO_CHAN_INFO_SAMP_FREQ)
891 return -EINVAL;
892
893 if (val <= 0)
894 return -EINVAL;
895
896 /* Max. 150 kSPS */
897 if (val > 150000)
898 val = 150000;
899
900 val *= 26;
901
902 /* Min 1MHz */
903 if (val < 1000000)
904 val = 1000000;
905
906 /*
907 * We want to round down, but only if we do not exceed the 150 kSPS
908 * limit.
909 */
910 div = clk_rate / val;
911 if (clk_rate / div / 26 > 150000)
912 div++;
913 if (div < 2)
914 div = 2;
915 else if (div > 0xff)
916 div = 0xff;
917
918 return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_DIV_MASK,
919 div << XADC_CONF2_DIV_OFFSET);
920}
921
922static const struct iio_event_spec xadc_temp_events[] = {
923 {
924 .type = IIO_EV_TYPE_THRESH,
925 .dir = IIO_EV_DIR_RISING,
926 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
927 BIT(IIO_EV_INFO_VALUE) |
928 BIT(IIO_EV_INFO_HYSTERESIS),
929 },
930};
931
932/* Separate values for upper and lower thresholds, but only a shared enabled */
933static const struct iio_event_spec xadc_voltage_events[] = {
934 {
935 .type = IIO_EV_TYPE_THRESH,
936 .dir = IIO_EV_DIR_RISING,
937 .mask_separate = BIT(IIO_EV_INFO_VALUE),
938 }, {
939 .type = IIO_EV_TYPE_THRESH,
940 .dir = IIO_EV_DIR_FALLING,
941 .mask_separate = BIT(IIO_EV_INFO_VALUE),
942 }, {
943 .type = IIO_EV_TYPE_THRESH,
944 .dir = IIO_EV_DIR_EITHER,
945 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
946 },
947};
948
949#define XADC_CHAN_TEMP(_chan, _scan_index, _addr) { \
950 .type = IIO_TEMP, \
951 .indexed = 1, \
952 .channel = (_chan), \
953 .address = (_addr), \
954 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
955 BIT(IIO_CHAN_INFO_SCALE) | \
956 BIT(IIO_CHAN_INFO_OFFSET), \
957 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
958 .event_spec = xadc_temp_events, \
959 .num_event_specs = ARRAY_SIZE(xadc_temp_events), \
960 .scan_index = (_scan_index), \
961 .scan_type = { \
962 .sign = 'u', \
963 .realbits = 12, \
964 .storagebits = 16, \
965 .shift = 4, \
966 .endianness = IIO_CPU, \
967 }, \
968}
969
970#define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) { \
971 .type = IIO_VOLTAGE, \
972 .indexed = 1, \
973 .channel = (_chan), \
974 .address = (_addr), \
975 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
976 BIT(IIO_CHAN_INFO_SCALE), \
977 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
978 .event_spec = (_alarm) ? xadc_voltage_events : NULL, \
979 .num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \
980 .scan_index = (_scan_index), \
981 .scan_type = { \
Thomas Betker97ffae12015-04-15 21:11:50 +0200982 .sign = ((_addr) == XADC_REG_VREFN) ? 's' : 'u', \
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000983 .realbits = 12, \
984 .storagebits = 16, \
985 .shift = 4, \
986 .endianness = IIO_CPU, \
987 }, \
988 .extend_name = _ext, \
989}
990
991static const struct iio_chan_spec xadc_channels[] = {
992 XADC_CHAN_TEMP(0, 8, XADC_REG_TEMP),
993 XADC_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true),
Thomas Betkerd6c96c42015-04-15 21:11:48 +0200994 XADC_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX, "vccaux", true),
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +0000995 XADC_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true),
996 XADC_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpint", true),
997 XADC_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpaux", true),
998 XADC_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccoddr", true),
999 XADC_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false),
1000 XADC_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false),
1001 XADC_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false),
1002 XADC_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false),
1003 XADC_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false),
1004 XADC_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false),
1005 XADC_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false),
1006 XADC_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false),
1007 XADC_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false),
1008 XADC_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false),
1009 XADC_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false),
1010 XADC_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false),
1011 XADC_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false),
1012 XADC_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false),
1013 XADC_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false),
1014 XADC_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false),
1015 XADC_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false),
1016 XADC_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false),
1017 XADC_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false),
1018};
1019
1020static const struct iio_info xadc_info = {
1021 .read_raw = &xadc_read_raw,
1022 .write_raw = &xadc_write_raw,
1023 .read_event_config = &xadc_read_event_config,
1024 .write_event_config = &xadc_write_event_config,
1025 .read_event_value = &xadc_read_event_value,
1026 .write_event_value = &xadc_write_event_value,
1027 .update_scan_mode = &xadc_update_scan_mode,
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001028};
1029
1030static const struct of_device_id xadc_of_match_table[] = {
1031 { .compatible = "xlnx,zynq-xadc-1.00.a", (void *)&xadc_zynq_ops },
1032 { .compatible = "xlnx,axi-xadc-1.00.a", (void *)&xadc_axi_ops },
1033 { },
1034};
1035MODULE_DEVICE_TABLE(of, xadc_of_match_table);
1036
1037static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np,
1038 unsigned int *conf)
1039{
1040 struct xadc *xadc = iio_priv(indio_dev);
1041 struct iio_chan_spec *channels, *chan;
1042 struct device_node *chan_node, *child;
1043 unsigned int num_channels;
1044 const char *external_mux;
1045 u32 ext_mux_chan;
Manish Naranic344a322018-07-18 16:42:09 +05301046 u32 reg;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001047 int ret;
1048
1049 *conf = 0;
1050
1051 ret = of_property_read_string(np, "xlnx,external-mux", &external_mux);
1052 if (ret < 0 || strcasecmp(external_mux, "none") == 0)
1053 xadc->external_mux_mode = XADC_EXTERNAL_MUX_NONE;
1054 else if (strcasecmp(external_mux, "single") == 0)
1055 xadc->external_mux_mode = XADC_EXTERNAL_MUX_SINGLE;
1056 else if (strcasecmp(external_mux, "dual") == 0)
1057 xadc->external_mux_mode = XADC_EXTERNAL_MUX_DUAL;
1058 else
1059 return -EINVAL;
1060
1061 if (xadc->external_mux_mode != XADC_EXTERNAL_MUX_NONE) {
1062 ret = of_property_read_u32(np, "xlnx,external-mux-channel",
1063 &ext_mux_chan);
1064 if (ret < 0)
1065 return ret;
1066
1067 if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_SINGLE) {
1068 if (ext_mux_chan == 0)
1069 ext_mux_chan = XADC_REG_VPVN;
1070 else if (ext_mux_chan <= 16)
1071 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1072 else
1073 return -EINVAL;
1074 } else {
1075 if (ext_mux_chan > 0 && ext_mux_chan <= 8)
1076 ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1);
1077 else
1078 return -EINVAL;
1079 }
1080
1081 *conf |= XADC_CONF0_MUX | XADC_CONF0_CHAN(ext_mux_chan);
1082 }
1083
1084 channels = kmemdup(xadc_channels, sizeof(xadc_channels), GFP_KERNEL);
1085 if (!channels)
1086 return -ENOMEM;
1087
1088 num_channels = 9;
1089 chan = &channels[9];
1090
1091 chan_node = of_get_child_by_name(np, "xlnx,channels");
1092 if (chan_node) {
1093 for_each_child_of_node(chan_node, child) {
1094 if (num_channels >= ARRAY_SIZE(xadc_channels)) {
1095 of_node_put(child);
1096 break;
1097 }
1098
1099 ret = of_property_read_u32(child, "reg", &reg);
1100 if (ret || reg > 16)
1101 continue;
1102
1103 if (of_property_read_bool(child, "xlnx,bipolar"))
1104 chan->scan_type.sign = 's';
1105
1106 if (reg == 0) {
1107 chan->scan_index = 11;
1108 chan->address = XADC_REG_VPVN;
1109 } else {
1110 chan->scan_index = 15 + reg;
Subbaraya Sundeep Bhatta1887e722014-11-09 09:55:00 +00001111 chan->address = XADC_REG_VAUX(reg - 1);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001112 }
1113 num_channels++;
1114 chan++;
1115 }
1116 }
1117 of_node_put(chan_node);
1118
1119 indio_dev->num_channels = num_channels;
1120 indio_dev->channels = krealloc(channels, sizeof(*channels) *
1121 num_channels, GFP_KERNEL);
1122 /* If we can't resize the channels array, just use the original */
1123 if (!indio_dev->channels)
1124 indio_dev->channels = channels;
1125
1126 return 0;
1127}
1128
1129static int xadc_probe(struct platform_device *pdev)
1130{
1131 const struct of_device_id *id;
1132 struct iio_dev *indio_dev;
1133 unsigned int bipolar_mask;
1134 struct resource *mem;
1135 unsigned int conf0;
1136 struct xadc *xadc;
1137 int ret;
1138 int irq;
1139 int i;
1140
1141 if (!pdev->dev.of_node)
1142 return -ENODEV;
1143
1144 id = of_match_node(xadc_of_match_table, pdev->dev.of_node);
1145 if (!id)
1146 return -EINVAL;
1147
1148 irq = platform_get_irq(pdev, 0);
1149 if (irq <= 0)
1150 return -ENXIO;
1151
1152 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*xadc));
1153 if (!indio_dev)
1154 return -ENOMEM;
1155
1156 xadc = iio_priv(indio_dev);
1157 xadc->ops = id->data;
1158 init_completion(&xadc->completion);
1159 mutex_init(&xadc->mutex);
1160 spin_lock_init(&xadc->lock);
1161 INIT_DELAYED_WORK(&xadc->zynq_unmask_work, xadc_zynq_unmask_worker);
1162
1163 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1164 xadc->base = devm_ioremap_resource(&pdev->dev, mem);
1165 if (IS_ERR(xadc->base))
1166 return PTR_ERR(xadc->base);
1167
1168 indio_dev->dev.parent = &pdev->dev;
1169 indio_dev->dev.of_node = pdev->dev.of_node;
1170 indio_dev->name = "xadc";
1171 indio_dev->modes = INDIO_DIRECT_MODE;
1172 indio_dev->info = &xadc_info;
1173
1174 ret = xadc_parse_dt(indio_dev, pdev->dev.of_node, &conf0);
1175 if (ret)
1176 goto err_device_free;
1177
1178 if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1179 ret = iio_triggered_buffer_setup(indio_dev,
1180 &iio_pollfunc_store_time, &xadc_trigger_handler,
1181 &xadc_buffer_ops);
1182 if (ret)
1183 goto err_device_free;
1184
1185 xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst");
Julia Lawall889c5582014-06-08 21:12:00 +01001186 if (IS_ERR(xadc->convst_trigger)) {
1187 ret = PTR_ERR(xadc->convst_trigger);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001188 goto err_triggered_buffer_cleanup;
Julia Lawall889c5582014-06-08 21:12:00 +01001189 }
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001190 xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev,
1191 "samplerate");
Julia Lawall889c5582014-06-08 21:12:00 +01001192 if (IS_ERR(xadc->samplerate_trigger)) {
1193 ret = PTR_ERR(xadc->samplerate_trigger);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001194 goto err_free_convst_trigger;
Julia Lawall889c5582014-06-08 21:12:00 +01001195 }
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001196 }
1197
1198 xadc->clk = devm_clk_get(&pdev->dev, NULL);
1199 if (IS_ERR(xadc->clk)) {
1200 ret = PTR_ERR(xadc->clk);
1201 goto err_free_samplerate_trigger;
1202 }
Arvind Yadav0a178fd2017-05-26 12:07:41 +05301203
1204 ret = clk_prepare_enable(xadc->clk);
1205 if (ret)
1206 goto err_free_samplerate_trigger;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001207
1208 ret = xadc->ops->setup(pdev, indio_dev, irq);
1209 if (ret)
Christophe JAILLETca1c39e2017-02-21 07:34:00 +01001210 goto err_clk_disable_unprepare;
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001211
Xander Huff70581e02015-08-11 18:00:49 -05001212 ret = request_irq(irq, xadc->ops->interrupt_handler, 0,
1213 dev_name(&pdev->dev), indio_dev);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001214 if (ret)
1215 goto err_clk_disable_unprepare;
1216
1217 for (i = 0; i < 16; i++)
1218 xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1219 &xadc->threshold[i]);
1220
1221 ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0);
1222 if (ret)
1223 goto err_free_irq;
1224
1225 bipolar_mask = 0;
1226 for (i = 0; i < indio_dev->num_channels; i++) {
1227 if (indio_dev->channels[i].scan_type.sign == 's')
1228 bipolar_mask |= BIT(indio_dev->channels[i].scan_index);
1229 }
1230
1231 ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask);
1232 if (ret)
1233 goto err_free_irq;
1234 ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1),
1235 bipolar_mask >> 16);
1236 if (ret)
1237 goto err_free_irq;
1238
1239 /* Disable all alarms */
1240 xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_ALARM_MASK,
1241 XADC_CONF1_ALARM_MASK);
1242
1243 /* Set thresholds to min/max */
1244 for (i = 0; i < 16; i++) {
1245 /*
1246 * Set max voltage threshold and both temperature thresholds to
1247 * 0xffff, min voltage threshold to 0.
1248 */
1249 if (i % 8 < 4 || i == 7)
1250 xadc->threshold[i] = 0xffff;
1251 else
1252 xadc->threshold[i] = 0;
1253 xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(i),
1254 xadc->threshold[i]);
1255 }
1256
1257 /* Go to non-buffered mode */
1258 xadc_postdisable(indio_dev);
1259
1260 ret = iio_device_register(indio_dev);
1261 if (ret)
1262 goto err_free_irq;
1263
1264 platform_set_drvdata(pdev, indio_dev);
1265
1266 return 0;
1267
1268err_free_irq:
1269 free_irq(irq, indio_dev);
Christophe JAILLETca1c39e2017-02-21 07:34:00 +01001270err_clk_disable_unprepare:
1271 clk_disable_unprepare(xadc->clk);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001272err_free_samplerate_trigger:
1273 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1274 iio_trigger_free(xadc->samplerate_trigger);
1275err_free_convst_trigger:
1276 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1277 iio_trigger_free(xadc->convst_trigger);
1278err_triggered_buffer_cleanup:
1279 if (xadc->ops->flags & XADC_FLAGS_BUFFERED)
1280 iio_triggered_buffer_cleanup(indio_dev);
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001281err_device_free:
1282 kfree(indio_dev->channels);
1283
1284 return ret;
1285}
1286
1287static int xadc_remove(struct platform_device *pdev)
1288{
1289 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1290 struct xadc *xadc = iio_priv(indio_dev);
1291 int irq = platform_get_irq(pdev, 0);
1292
1293 iio_device_unregister(indio_dev);
1294 if (xadc->ops->flags & XADC_FLAGS_BUFFERED) {
1295 iio_trigger_free(xadc->samplerate_trigger);
1296 iio_trigger_free(xadc->convst_trigger);
1297 iio_triggered_buffer_cleanup(indio_dev);
1298 }
1299 free_irq(irq, indio_dev);
1300 clk_disable_unprepare(xadc->clk);
1301 cancel_delayed_work(&xadc->zynq_unmask_work);
1302 kfree(xadc->data);
1303 kfree(indio_dev->channels);
1304
1305 return 0;
1306}
1307
1308static struct platform_driver xadc_driver = {
1309 .probe = xadc_probe,
1310 .remove = xadc_remove,
1311 .driver = {
1312 .name = "xadc",
Lars-Peter Clausenbdc8cda2014-02-17 14:10:00 +00001313 .of_match_table = xadc_of_match_table,
1314 },
1315};
1316module_platform_driver(xadc_driver);
1317
1318MODULE_LICENSE("GPL v2");
1319MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1320MODULE_DESCRIPTION("Xilinx XADC IIO driver");