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