| 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 <[email protected]> |
| 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 |
/* Temp in C = (val * 503.975) / 2**bits - 273.15 */ |
| 460 |
.temp_scale = 503975, |
| 461 |
.temp_offset = 273150, |
| 462 |
}; |
| 463 |
|
| 464 |
static const unsigned int xadc_axi_reg_offsets[] = { |
| 465 |
[XADC_TYPE_S7] = XADC_7S_AXI_ADC_REG_OFFSET, |
| 466 |
[XADC_TYPE_US] = XADC_US_AXI_ADC_REG_OFFSET, |
| 467 |
}; |
| 468 |
|
| 469 |
static int xadc_axi_read_adc_reg(struct xadc *xadc, unsigned int reg, |
| 470 |
uint16_t *val) |
| 471 |
{ |
| 472 |
uint32_t val32; |
| 473 |
|
| 474 |
xadc_read_reg(xadc, xadc_axi_reg_offsets[xadc->ops->type] + reg * 4, |
| 475 |
&val32); |
| 476 |
*val = val32 & 0xffff; |
| 477 |
|
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
static int xadc_axi_write_adc_reg(struct xadc *xadc, unsigned int reg, |
| 482 |
uint16_t val) |
| 483 |
{ |
| 484 |
xadc_write_reg(xadc, xadc_axi_reg_offsets[xadc->ops->type] + reg * 4, |
| 485 |
val); |
| 486 |
|
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
static int xadc_axi_setup(struct platform_device *pdev, |
| 491 |
struct iio_dev *indio_dev, int irq) |
| 492 |
{ |
| 493 |
struct xadc *xadc = iio_priv(indio_dev); |
| 494 |
|
| 495 |
xadc_write_reg(xadc, XADC_AXI_REG_RESET, XADC_AXI_RESET_MAGIC); |
| 496 |
xadc_write_reg(xadc, XADC_AXI_REG_GIER, XADC_AXI_GIER_ENABLE); |
| 497 |
|
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid) |
| 502 |
{ |
| 503 |
struct iio_dev *indio_dev = devid; |
| 504 |
struct xadc *xadc = iio_priv(indio_dev); |
| 505 |
uint32_t status, mask; |
| 506 |
unsigned int events; |
| 507 |
|
| 508 |
xadc_read_reg(xadc, XADC_AXI_REG_IPISR, &status); |
| 509 |
xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &mask); |
| 510 |
status &= mask; |
| 511 |
|
| 512 |
if (!status) |
| 513 |
return IRQ_NONE; |
| 514 |
|
| 515 |
if ((status & XADC_AXI_INT_EOS) && xadc->trigger) |
| 516 |
iio_trigger_poll(xadc->trigger); |
| 517 |
|
| 518 |
if (status & XADC_AXI_INT_ALARM_MASK) { |
| 519 |
/* |
| 520 |
* The order of the bits in the AXI-XADC status register does |
| 521 |
* not match the order of the bits in the XADC alarm enable |
| 522 |
* register. xadc_handle_events() expects the events to be in |
| 523 |
* the same order as the XADC alarm enable register. |
| 524 |
*/ |
| 525 |
events = (status & 0x000e) >> 1; |
| 526 |
events |= (status & 0x0001) << 3; |
| 527 |
events |= (status & 0x3c00) >> 6; |
| 528 |
xadc_handle_events(indio_dev, events); |
| 529 |
} |
| 530 |
|
| 531 |
xadc_write_reg(xadc, XADC_AXI_REG_IPISR, status); |
| 532 |
|
| 533 |
return IRQ_HANDLED; |
| 534 |
} |
| 535 |
|
| 536 |
static void xadc_axi_update_alarm(struct xadc *xadc, unsigned int alarm) |
| 537 |
{ |
| 538 |
uint32_t val; |
| 539 |
unsigned long flags; |
| 540 |
|
| 541 |
/* |
| 542 |
* The order of the bits in the AXI-XADC status register does not match |
| 543 |
* the order of the bits in the XADC alarm enable register. We get |
| 544 |
* passed the alarm mask in the same order as in the XADC alarm enable |
| 545 |
* register. |
| 546 |
*/ |
| 547 |
alarm = ((alarm & 0x07) << 1) | ((alarm & 0x08) >> 3) | |
| 548 |
((alarm & 0xf0) << 6); |
| 549 |
|
| 550 |
spin_lock_irqsave(&xadc->lock, flags); |
| 551 |
xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val); |
| 552 |
val &= ~XADC_AXI_INT_ALARM_MASK; |
| 553 |
val |= alarm; |
| 554 |
xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val); |
| 555 |
spin_unlock_irqrestore(&xadc->lock, flags); |
| 556 |
} |
| 557 |
|
| 558 |
static unsigned long xadc_axi_get_dclk(struct xadc *xadc) |
| 559 |
{ |
| 560 |
return clk_get_rate(xadc->clk); |
| 561 |
} |
| 562 |
|
| 563 |
static const struct xadc_ops xadc_7s_axi_ops = { |
| 564 |
.read = xadc_axi_read_adc_reg, |
| 565 |
.write = xadc_axi_write_adc_reg, |
| 566 |
.setup = xadc_axi_setup, |
| 567 |
.get_dclk_rate = xadc_axi_get_dclk, |
| 568 |
.update_alarm = xadc_axi_update_alarm, |
| 569 |
.interrupt_handler = xadc_axi_interrupt_handler, |
| 570 |
.flags = XADC_FLAGS_BUFFERED | XADC_FLAGS_IRQ_OPTIONAL, |
| 571 |
.type = XADC_TYPE_S7, |
| 572 |
/* Temp in C = (val * 503.975) / 2**bits - 273.15 */ |
| 573 |
.temp_scale = 503975, |
| 574 |
.temp_offset = 273150, |
| 575 |
}; |
| 576 |
|
| 577 |
static const struct xadc_ops xadc_us_axi_ops = { |
| 578 |
.read = xadc_axi_read_adc_reg, |
| 579 |
.write = xadc_axi_write_adc_reg, |
| 580 |
.setup = xadc_axi_setup, |
| 581 |
.get_dclk_rate = xadc_axi_get_dclk, |
| 582 |
.update_alarm = xadc_axi_update_alarm, |
| 583 |
.interrupt_handler = xadc_axi_interrupt_handler, |
| 584 |
.flags = XADC_FLAGS_BUFFERED | XADC_FLAGS_IRQ_OPTIONAL, |
| 585 |
.type = XADC_TYPE_US, |
| 586 |
/** |
| 587 |
* Values below are for UltraScale+ (SYSMONE4) using internal reference. |
| 588 |
* See https://docs.xilinx.com/v/u/en-US/ug580-ultrascale-sysmon |
| 589 |
*/ |
| 590 |
.temp_scale = 509314, |
| 591 |
.temp_offset = 280231, |
| 592 |
}; |
| 593 |
|
| 594 |
static int _xadc_update_adc_reg(struct xadc *xadc, unsigned int reg, |
| 595 |
uint16_t mask, uint16_t val) |
| 596 |
{ |
| 597 |
uint16_t tmp; |
| 598 |
int ret; |
| 599 |
|
| 600 |
ret = _xadc_read_adc_reg(xadc, reg, &tmp); |
| 601 |
if (ret) |
| 602 |
return ret; |
| 603 |
|
| 604 |
return _xadc_write_adc_reg(xadc, reg, (tmp & ~mask) | val); |
| 605 |
} |
| 606 |
|
| 607 |
static int xadc_update_adc_reg(struct xadc *xadc, unsigned int reg, |
| 608 |
uint16_t mask, uint16_t val) |
| 609 |
{ |
| 610 |
int ret; |
| 611 |
|
| 612 |
mutex_lock(&xadc->mutex); |
| 613 |
ret = _xadc_update_adc_reg(xadc, reg, mask, val); |
| 614 |
mutex_unlock(&xadc->mutex); |
| 615 |
|
| 616 |
return ret; |
| 617 |
} |
| 618 |
|
| 619 |
static unsigned long xadc_get_dclk_rate(struct xadc *xadc) |
| 620 |
{ |
| 621 |
return xadc->ops->get_dclk_rate(xadc); |
| 622 |
} |
| 623 |
|
| 624 |
static int xadc_update_scan_mode(struct iio_dev *indio_dev, |
| 625 |
const unsigned long *mask) |
| 626 |
{ |
| 627 |
struct xadc *xadc = iio_priv(indio_dev); |
| 628 |
size_t n; |
| 629 |
void *data; |
| 630 |
|
| 631 |
n = bitmap_weight(mask, iio_get_masklength(indio_dev)); |
| 632 |
|
| 633 |
data = devm_krealloc_array(indio_dev->dev.parent, xadc->data, |
| 634 |
n, sizeof(*xadc->data), GFP_KERNEL); |
| 635 |
if (!data) |
| 636 |
return -ENOMEM; |
| 637 |
|
| 638 |
memset(data, 0, n * sizeof(*xadc->data)); |
| 639 |
xadc->data = data; |
| 640 |
|
| 641 |
return 0; |
| 642 |
} |
| 643 |
|
| 644 |
static unsigned int xadc_scan_index_to_channel(unsigned int scan_index) |
| 645 |
{ |
| 646 |
switch (scan_index) { |
| 647 |
case 5: |
| 648 |
return XADC_REG_VCCPINT; |
| 649 |
case 6: |
| 650 |
return XADC_REG_VCCPAUX; |
| 651 |
case 7: |
| 652 |
return XADC_REG_VCCO_DDR; |
| 653 |
case 8: |
| 654 |
return XADC_REG_TEMP; |
| 655 |
case 9: |
| 656 |
return XADC_REG_VCCINT; |
| 657 |
case 10: |
| 658 |
return XADC_REG_VCCAUX; |
| 659 |
case 11: |
| 660 |
return XADC_REG_VPVN; |
| 661 |
case 12: |
| 662 |
return XADC_REG_VREFP; |
| 663 |
case 13: |
| 664 |
return XADC_REG_VREFN; |
| 665 |
case 14: |
| 666 |
return XADC_REG_VCCBRAM; |
| 667 |
default: |
| 668 |
return XADC_REG_VAUX(scan_index - 16); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
static irqreturn_t xadc_trigger_handler(int irq, void *p) |
| 673 |
{ |
| 674 |
struct iio_poll_func *pf = p; |
| 675 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 676 |
struct xadc *xadc = iio_priv(indio_dev); |
| 677 |
unsigned int chan; |
| 678 |
int i, j; |
| 679 |
|
| 680 |
if (!xadc->data) |
| 681 |
goto out; |
| 682 |
|
| 683 |
j = 0; |
| 684 |
iio_for_each_active_channel(indio_dev, i) { |
| 685 |
chan = xadc_scan_index_to_channel(i); |
| 686 |
xadc_read_adc_reg(xadc, chan, &xadc->data[j]); |
| 687 |
j++; |
| 688 |
} |
| 689 |
|
| 690 |
iio_push_to_buffers(indio_dev, xadc->data); |
| 691 |
|
| 692 |
out: |
| 693 |
iio_trigger_notify_done(indio_dev->trig); |
| 694 |
|
| 695 |
return IRQ_HANDLED; |
| 696 |
} |
| 697 |
|
| 698 |
static int xadc_trigger_set_state(struct iio_trigger *trigger, bool state) |
| 699 |
{ |
| 700 |
struct xadc *xadc = iio_trigger_get_drvdata(trigger); |
| 701 |
unsigned long flags; |
| 702 |
unsigned int convst; |
| 703 |
unsigned int val; |
| 704 |
int ret = 0; |
| 705 |
|
| 706 |
mutex_lock(&xadc->mutex); |
| 707 |
|
| 708 |
if (state) { |
| 709 |
/* Only one of the two triggers can be active at a time. */ |
| 710 |
if (xadc->trigger != NULL) { |
| 711 |
ret = -EBUSY; |
| 712 |
goto err_out; |
| 713 |
} else { |
| 714 |
xadc->trigger = trigger; |
| 715 |
if (trigger == xadc->convst_trigger) |
| 716 |
convst = XADC_CONF0_EC; |
| 717 |
else |
| 718 |
convst = 0; |
| 719 |
} |
| 720 |
ret = _xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF0_EC, |
| 721 |
convst); |
| 722 |
if (ret) |
| 723 |
goto err_out; |
| 724 |
} else { |
| 725 |
xadc->trigger = NULL; |
| 726 |
} |
| 727 |
|
| 728 |
spin_lock_irqsave(&xadc->lock, flags); |
| 729 |
xadc_read_reg(xadc, XADC_AXI_REG_IPIER, &val); |
| 730 |
xadc_write_reg(xadc, XADC_AXI_REG_IPISR, XADC_AXI_INT_EOS); |
| 731 |
if (state) |
| 732 |
val |= XADC_AXI_INT_EOS; |
| 733 |
else |
| 734 |
val &= ~XADC_AXI_INT_EOS; |
| 735 |
xadc_write_reg(xadc, XADC_AXI_REG_IPIER, val); |
| 736 |
spin_unlock_irqrestore(&xadc->lock, flags); |
| 737 |
|
| 738 |
err_out: |
| 739 |
mutex_unlock(&xadc->mutex); |
| 740 |
|
| 741 |
return ret; |
| 742 |
} |
| 743 |
|
| 744 |
static const struct iio_trigger_ops xadc_trigger_ops = { |
| 745 |
.set_trigger_state = &xadc_trigger_set_state, |
| 746 |
}; |
| 747 |
|
| 748 |
static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev, |
| 749 |
const char *name) |
| 750 |
{ |
| 751 |
struct device *dev = indio_dev->dev.parent; |
| 752 |
struct iio_trigger *trig; |
| 753 |
int ret; |
| 754 |
|
| 755 |
trig = devm_iio_trigger_alloc(dev, "%s%d-%s", indio_dev->name, |
| 756 |
iio_device_id(indio_dev), name); |
| 757 |
if (trig == NULL) |
| 758 |
return ERR_PTR(-ENOMEM); |
| 759 |
|
| 760 |
trig->ops = &xadc_trigger_ops; |
| 761 |
iio_trigger_set_drvdata(trig, iio_priv(indio_dev)); |
| 762 |
|
| 763 |
ret = devm_iio_trigger_register(dev, trig); |
| 764 |
if (ret) |
| 765 |
return ERR_PTR(ret); |
| 766 |
|
| 767 |
return trig; |
| 768 |
} |
| 769 |
|
| 770 |
static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode) |
| 771 |
{ |
| 772 |
uint16_t val; |
| 773 |
|
| 774 |
/* |
| 775 |
* As per datasheet the power-down bits are don't care in the |
| 776 |
* UltraScale, but as per reality setting the power-down bit for the |
| 777 |
* non-existing ADC-B powers down the main ADC, so just return and don't |
| 778 |
* do anything. |
| 779 |
*/ |
| 780 |
if (xadc->ops->type == XADC_TYPE_US) |
| 781 |
return 0; |
| 782 |
|
| 783 |
/* Powerdown the ADC-B when it is not needed. */ |
| 784 |
switch (seq_mode) { |
| 785 |
case XADC_CONF1_SEQ_SIMULTANEOUS: |
| 786 |
case XADC_CONF1_SEQ_INDEPENDENT: |
| 787 |
val = 0; |
| 788 |
break; |
| 789 |
default: |
| 790 |
val = XADC_CONF2_PD_ADC_B; |
| 791 |
break; |
| 792 |
} |
| 793 |
|
| 794 |
return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_PD_MASK, |
| 795 |
val); |
| 796 |
} |
| 797 |
|
| 798 |
static int xadc_get_seq_mode(struct xadc *xadc, unsigned long scan_mode) |
| 799 |
{ |
| 800 |
unsigned int aux_scan_mode = scan_mode >> 16; |
| 801 |
|
| 802 |
/* UltraScale has only one ADC and supports only continuous mode */ |
| 803 |
if (xadc->ops->type == XADC_TYPE_US) |
| 804 |
return XADC_CONF1_SEQ_CONTINUOUS; |
| 805 |
|
| 806 |
if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_DUAL) |
| 807 |
return XADC_CONF1_SEQ_SIMULTANEOUS; |
| 808 |
|
| 809 |
if ((aux_scan_mode & 0xff00) == 0 || |
| 810 |
(aux_scan_mode & 0x00ff) == 0) |
| 811 |
return XADC_CONF1_SEQ_CONTINUOUS; |
| 812 |
|
| 813 |
return XADC_CONF1_SEQ_SIMULTANEOUS; |
| 814 |
} |
| 815 |
|
| 816 |
static int xadc_postdisable(struct iio_dev *indio_dev) |
| 817 |
{ |
| 818 |
struct xadc *xadc = iio_priv(indio_dev); |
| 819 |
unsigned long scan_mask; |
| 820 |
int seq_mode; |
| 821 |
int ret; |
| 822 |
int i; |
| 823 |
|
| 824 |
scan_mask = 1; /* Run calibration as part of the sequence */ |
| 825 |
for (i = 0; i < indio_dev->num_channels; i++) |
| 826 |
scan_mask |= BIT(indio_dev->channels[i].scan_index); |
| 827 |
|
| 828 |
/* |
| 829 |
* Use the correct sequencer mode for the idle state: simultaneous |
| 830 |
* mode for dual external mux configurations, continuous otherwise. |
| 831 |
*/ |
| 832 |
seq_mode = xadc_get_seq_mode(xadc, scan_mask); |
| 833 |
|
| 834 |
/* Enable all channels and calibration */ |
| 835 |
ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff); |
| 836 |
if (ret) |
| 837 |
return ret; |
| 838 |
|
| 839 |
ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16); |
| 840 |
if (ret) |
| 841 |
return ret; |
| 842 |
|
| 843 |
ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK, |
| 844 |
seq_mode); |
| 845 |
if (ret) |
| 846 |
return ret; |
| 847 |
|
| 848 |
return xadc_power_adc_b(xadc, seq_mode); |
| 849 |
} |
| 850 |
|
| 851 |
static int xadc_preenable(struct iio_dev *indio_dev) |
| 852 |
{ |
| 853 |
struct xadc *xadc = iio_priv(indio_dev); |
| 854 |
unsigned long scan_mask; |
| 855 |
int seq_mode; |
| 856 |
int ret; |
| 857 |
|
| 858 |
ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK, |
| 859 |
XADC_CONF1_SEQ_DEFAULT); |
| 860 |
if (ret) |
| 861 |
goto err; |
| 862 |
|
| 863 |
scan_mask = *indio_dev->active_scan_mask; |
| 864 |
seq_mode = xadc_get_seq_mode(xadc, scan_mask); |
| 865 |
|
| 866 |
ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(0), scan_mask & 0xffff); |
| 867 |
if (ret) |
| 868 |
goto err; |
| 869 |
|
| 870 |
/* |
| 871 |
* In simultaneous mode the upper and lower aux channels are samples at |
| 872 |
* the same time. In this mode the upper 8 bits in the sequencer |
| 873 |
* register are don't care and the lower 8 bits control two channels |
| 874 |
* each. As such we must set the bit if either the channel in the lower |
| 875 |
* group or the upper group is enabled. |
| 876 |
*/ |
| 877 |
if (seq_mode == XADC_CONF1_SEQ_SIMULTANEOUS) |
| 878 |
scan_mask = ((scan_mask >> 8) | scan_mask) & 0xff0000; |
| 879 |
|
| 880 |
ret = xadc_write_adc_reg(xadc, XADC_REG_SEQ(1), scan_mask >> 16); |
| 881 |
if (ret) |
| 882 |
goto err; |
| 883 |
|
| 884 |
ret = xadc_power_adc_b(xadc, seq_mode); |
| 885 |
if (ret) |
| 886 |
goto err; |
| 887 |
|
| 888 |
ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_SEQ_MASK, |
| 889 |
seq_mode); |
| 890 |
if (ret) |
| 891 |
goto err; |
| 892 |
|
| 893 |
return 0; |
| 894 |
err: |
| 895 |
xadc_postdisable(indio_dev); |
| 896 |
return ret; |
| 897 |
} |
| 898 |
|
| 899 |
static const struct iio_buffer_setup_ops xadc_buffer_ops = { |
| 900 |
.preenable = &xadc_preenable, |
| 901 |
.postdisable = &xadc_postdisable, |
| 902 |
}; |
| 903 |
|
| 904 |
static int xadc_read_samplerate(struct xadc *xadc) |
| 905 |
{ |
| 906 |
unsigned int div; |
| 907 |
uint16_t val16; |
| 908 |
int ret; |
| 909 |
|
| 910 |
ret = xadc_read_adc_reg(xadc, XADC_REG_CONF2, &val16); |
| 911 |
if (ret) |
| 912 |
return ret; |
| 913 |
|
| 914 |
div = (val16 & XADC_CONF2_DIV_MASK) >> XADC_CONF2_DIV_OFFSET; |
| 915 |
if (div < 2) |
| 916 |
div = 2; |
| 917 |
|
| 918 |
return xadc_get_dclk_rate(xadc) / div / 26; |
| 919 |
} |
| 920 |
|
| 921 |
static int xadc_read_raw(struct iio_dev *indio_dev, |
| 922 |
struct iio_chan_spec const *chan, int *val, int *val2, long info) |
| 923 |
{ |
| 924 |
struct xadc *xadc = iio_priv(indio_dev); |
| 925 |
unsigned int bits = chan->scan_type.realbits; |
| 926 |
uint16_t val16; |
| 927 |
int ret; |
| 928 |
|
| 929 |
switch (info) { |
| 930 |
case IIO_CHAN_INFO_RAW: |
| 931 |
if (iio_buffer_enabled(indio_dev)) |
| 932 |
return -EBUSY; |
| 933 |
ret = xadc_read_adc_reg(xadc, chan->address, &val16); |
| 934 |
if (ret < 0) |
| 935 |
return ret; |
| 936 |
|
| 937 |
val16 >>= chan->scan_type.shift; |
| 938 |
if (chan->scan_type.sign == 'u') |
| 939 |
*val = val16; |
| 940 |
else |
| 941 |
*val = sign_extend32(val16, bits - 1); |
| 942 |
|
| 943 |
return IIO_VAL_INT; |
| 944 |
case IIO_CHAN_INFO_SCALE: |
| 945 |
switch (chan->type) { |
| 946 |
case IIO_VOLTAGE: |
| 947 |
/* V = (val * 3.0) / 2**bits */ |
| 948 |
switch (chan->address) { |
| 949 |
case XADC_REG_VCCINT: |
| 950 |
case XADC_REG_VCCAUX: |
| 951 |
case XADC_REG_VREFP: |
| 952 |
case XADC_REG_VREFN: |
| 953 |
case XADC_REG_VCCBRAM: |
| 954 |
case XADC_REG_VCCPINT: |
| 955 |
case XADC_REG_VCCPAUX: |
| 956 |
case XADC_REG_VCCO_DDR: |
| 957 |
*val = 3000; |
| 958 |
break; |
| 959 |
default: |
| 960 |
*val = 1000; |
| 961 |
break; |
| 962 |
} |
| 963 |
*val2 = bits; |
| 964 |
return IIO_VAL_FRACTIONAL_LOG2; |
| 965 |
case IIO_TEMP: |
| 966 |
*val = xadc->ops->temp_scale; |
| 967 |
*val2 = bits; |
| 968 |
return IIO_VAL_FRACTIONAL_LOG2; |
| 969 |
default: |
| 970 |
return -EINVAL; |
| 971 |
} |
| 972 |
case IIO_CHAN_INFO_OFFSET: |
| 973 |
/* Only the temperature channel has an offset */ |
| 974 |
*val = -((xadc->ops->temp_offset << bits) / xadc->ops->temp_scale); |
| 975 |
return IIO_VAL_INT; |
| 976 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 977 |
ret = xadc_read_samplerate(xadc); |
| 978 |
if (ret < 0) |
| 979 |
return ret; |
| 980 |
|
| 981 |
*val = ret; |
| 982 |
return IIO_VAL_INT; |
| 983 |
default: |
| 984 |
return -EINVAL; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
static int xadc_write_samplerate(struct xadc *xadc, int val) |
| 989 |
{ |
| 990 |
unsigned long clk_rate = xadc_get_dclk_rate(xadc); |
| 991 |
unsigned int div; |
| 992 |
|
| 993 |
if (!clk_rate) |
| 994 |
return -EINVAL; |
| 995 |
|
| 996 |
if (val <= 0) |
| 997 |
return -EINVAL; |
| 998 |
|
| 999 |
/* Max. 150 kSPS */ |
| 1000 |
if (val > XADC_MAX_SAMPLERATE) |
| 1001 |
val = XADC_MAX_SAMPLERATE; |
| 1002 |
|
| 1003 |
val *= 26; |
| 1004 |
|
| 1005 |
/* Min 1MHz */ |
| 1006 |
if (val < 1000000) |
| 1007 |
val = 1000000; |
| 1008 |
|
| 1009 |
/* |
| 1010 |
* We want to round down, but only if we do not exceed the 150 kSPS |
| 1011 |
* limit. |
| 1012 |
*/ |
| 1013 |
div = clk_rate / val; |
| 1014 |
if (clk_rate / div / 26 > XADC_MAX_SAMPLERATE) |
| 1015 |
div++; |
| 1016 |
if (div < 2) |
| 1017 |
div = 2; |
| 1018 |
else if (div > 0xff) |
| 1019 |
div = 0xff; |
| 1020 |
|
| 1021 |
return xadc_update_adc_reg(xadc, XADC_REG_CONF2, XADC_CONF2_DIV_MASK, |
| 1022 |
div << XADC_CONF2_DIV_OFFSET); |
| 1023 |
} |
| 1024 |
|
| 1025 |
static int xadc_write_raw(struct iio_dev *indio_dev, |
| 1026 |
struct iio_chan_spec const *chan, int val, int val2, long info) |
| 1027 |
{ |
| 1028 |
struct xadc *xadc = iio_priv(indio_dev); |
| 1029 |
|
| 1030 |
if (info != IIO_CHAN_INFO_SAMP_FREQ) |
| 1031 |
return -EINVAL; |
| 1032 |
|
| 1033 |
return xadc_write_samplerate(xadc, val); |
| 1034 |
} |
| 1035 |
|
| 1036 |
static const struct iio_event_spec xadc_temp_events[] = { |
| 1037 |
{ |
| 1038 |
.type = IIO_EV_TYPE_THRESH, |
| 1039 |
.dir = IIO_EV_DIR_RISING, |
| 1040 |
.mask_separate = BIT(IIO_EV_INFO_ENABLE) | |
| 1041 |
BIT(IIO_EV_INFO_VALUE) | |
| 1042 |
BIT(IIO_EV_INFO_HYSTERESIS), |
| 1043 |
}, |
| 1044 |
}; |
| 1045 |
|
| 1046 |
/* Separate values for upper and lower thresholds, but only a shared enabled */ |
| 1047 |
static const struct iio_event_spec xadc_voltage_events[] = { |
| 1048 |
{ |
| 1049 |
.type = IIO_EV_TYPE_THRESH, |
| 1050 |
.dir = IIO_EV_DIR_RISING, |
| 1051 |
.mask_separate = BIT(IIO_EV_INFO_VALUE), |
| 1052 |
}, { |
| 1053 |
.type = IIO_EV_TYPE_THRESH, |
| 1054 |
.dir = IIO_EV_DIR_FALLING, |
| 1055 |
.mask_separate = BIT(IIO_EV_INFO_VALUE), |
| 1056 |
}, { |
| 1057 |
.type = IIO_EV_TYPE_THRESH, |
| 1058 |
.dir = IIO_EV_DIR_EITHER, |
| 1059 |
.mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 1060 |
}, |
| 1061 |
}; |
| 1062 |
|
| 1063 |
#define XADC_CHAN_TEMP(_chan, _scan_index, _addr, _bits) { \ |
| 1064 |
.type = IIO_TEMP, \ |
| 1065 |
.indexed = 1, \ |
| 1066 |
.channel = (_chan), \ |
| 1067 |
.address = (_addr), \ |
| 1068 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 1069 |
BIT(IIO_CHAN_INFO_SCALE) | \ |
| 1070 |
BIT(IIO_CHAN_INFO_OFFSET), \ |
| 1071 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 1072 |
.event_spec = xadc_temp_events, \ |
| 1073 |
.num_event_specs = ARRAY_SIZE(xadc_temp_events), \ |
| 1074 |
.scan_index = (_scan_index), \ |
| 1075 |
.scan_type = { \ |
| 1076 |
.sign = 'u', \ |
| 1077 |
.realbits = (_bits), \ |
| 1078 |
.storagebits = 16, \ |
| 1079 |
.shift = 16 - (_bits), \ |
| 1080 |
.endianness = IIO_CPU, \ |
| 1081 |
}, \ |
| 1082 |
} |
| 1083 |
|
| 1084 |
#define XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, _bits, _ext, _alarm) { \ |
| 1085 |
.type = IIO_VOLTAGE, \ |
| 1086 |
.indexed = 1, \ |
| 1087 |
.channel = (_chan), \ |
| 1088 |
.address = (_addr), \ |
| 1089 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 1090 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 1091 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 1092 |
.event_spec = (_alarm) ? xadc_voltage_events : NULL, \ |
| 1093 |
.num_event_specs = (_alarm) ? ARRAY_SIZE(xadc_voltage_events) : 0, \ |
| 1094 |
.scan_index = (_scan_index), \ |
| 1095 |
.scan_type = { \ |
| 1096 |
.sign = ((_addr) == XADC_REG_VREFN) ? 's' : 'u', \ |
| 1097 |
.realbits = (_bits), \ |
| 1098 |
.storagebits = 16, \ |
| 1099 |
.shift = 16 - (_bits), \ |
| 1100 |
.endianness = IIO_CPU, \ |
| 1101 |
}, \ |
| 1102 |
.extend_name = _ext, \ |
| 1103 |
} |
| 1104 |
|
| 1105 |
/* 7 Series */ |
| 1106 |
#define XADC_7S_CHAN_TEMP(_chan, _scan_index, _addr) \ |
| 1107 |
XADC_CHAN_TEMP(_chan, _scan_index, _addr, 12) |
| 1108 |
#define XADC_7S_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) \ |
| 1109 |
XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, 12, _ext, _alarm) |
| 1110 |
|
| 1111 |
static const struct iio_chan_spec xadc_7s_channels[] = { |
| 1112 |
XADC_7S_CHAN_TEMP(0, 8, XADC_REG_TEMP), |
| 1113 |
XADC_7S_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true), |
| 1114 |
XADC_7S_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX, "vccaux", true), |
| 1115 |
XADC_7S_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true), |
| 1116 |
XADC_7S_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpint", true), |
| 1117 |
XADC_7S_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpaux", true), |
| 1118 |
XADC_7S_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccoddr", true), |
| 1119 |
XADC_7S_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false), |
| 1120 |
XADC_7S_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false), |
| 1121 |
XADC_7S_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false), |
| 1122 |
XADC_7S_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false), |
| 1123 |
XADC_7S_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false), |
| 1124 |
XADC_7S_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false), |
| 1125 |
XADC_7S_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false), |
| 1126 |
XADC_7S_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false), |
| 1127 |
XADC_7S_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false), |
| 1128 |
XADC_7S_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false), |
| 1129 |
XADC_7S_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false), |
| 1130 |
XADC_7S_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false), |
| 1131 |
XADC_7S_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false), |
| 1132 |
XADC_7S_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false), |
| 1133 |
XADC_7S_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false), |
| 1134 |
XADC_7S_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false), |
| 1135 |
XADC_7S_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false), |
| 1136 |
XADC_7S_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false), |
| 1137 |
XADC_7S_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false), |
| 1138 |
}; |
| 1139 |
|
| 1140 |
/* UltraScale */ |
| 1141 |
#define XADC_US_CHAN_TEMP(_chan, _scan_index, _addr) \ |
| 1142 |
XADC_CHAN_TEMP(_chan, _scan_index, _addr, 10) |
| 1143 |
#define XADC_US_CHAN_VOLTAGE(_chan, _scan_index, _addr, _ext, _alarm) \ |
| 1144 |
XADC_CHAN_VOLTAGE(_chan, _scan_index, _addr, 10, _ext, _alarm) |
| 1145 |
|
| 1146 |
static const struct iio_chan_spec xadc_us_channels[] = { |
| 1147 |
XADC_US_CHAN_TEMP(0, 8, XADC_REG_TEMP), |
| 1148 |
XADC_US_CHAN_VOLTAGE(0, 9, XADC_REG_VCCINT, "vccint", true), |
| 1149 |
XADC_US_CHAN_VOLTAGE(1, 10, XADC_REG_VCCAUX, "vccaux", true), |
| 1150 |
XADC_US_CHAN_VOLTAGE(2, 14, XADC_REG_VCCBRAM, "vccbram", true), |
| 1151 |
XADC_US_CHAN_VOLTAGE(3, 5, XADC_REG_VCCPINT, "vccpsintlp", true), |
| 1152 |
XADC_US_CHAN_VOLTAGE(4, 6, XADC_REG_VCCPAUX, "vccpsintfp", true), |
| 1153 |
XADC_US_CHAN_VOLTAGE(5, 7, XADC_REG_VCCO_DDR, "vccpsaux", true), |
| 1154 |
XADC_US_CHAN_VOLTAGE(6, 12, XADC_REG_VREFP, "vrefp", false), |
| 1155 |
XADC_US_CHAN_VOLTAGE(7, 13, XADC_REG_VREFN, "vrefn", false), |
| 1156 |
XADC_US_CHAN_VOLTAGE(8, 11, XADC_REG_VPVN, NULL, false), |
| 1157 |
XADC_US_CHAN_VOLTAGE(9, 16, XADC_REG_VAUX(0), NULL, false), |
| 1158 |
XADC_US_CHAN_VOLTAGE(10, 17, XADC_REG_VAUX(1), NULL, false), |
| 1159 |
XADC_US_CHAN_VOLTAGE(11, 18, XADC_REG_VAUX(2), NULL, false), |
| 1160 |
XADC_US_CHAN_VOLTAGE(12, 19, XADC_REG_VAUX(3), NULL, false), |
| 1161 |
XADC_US_CHAN_VOLTAGE(13, 20, XADC_REG_VAUX(4), NULL, false), |
| 1162 |
XADC_US_CHAN_VOLTAGE(14, 21, XADC_REG_VAUX(5), NULL, false), |
| 1163 |
XADC_US_CHAN_VOLTAGE(15, 22, XADC_REG_VAUX(6), NULL, false), |
| 1164 |
XADC_US_CHAN_VOLTAGE(16, 23, XADC_REG_VAUX(7), NULL, false), |
| 1165 |
XADC_US_CHAN_VOLTAGE(17, 24, XADC_REG_VAUX(8), NULL, false), |
| 1166 |
XADC_US_CHAN_VOLTAGE(18, 25, XADC_REG_VAUX(9), NULL, false), |
| 1167 |
XADC_US_CHAN_VOLTAGE(19, 26, XADC_REG_VAUX(10), NULL, false), |
| 1168 |
XADC_US_CHAN_VOLTAGE(20, 27, XADC_REG_VAUX(11), NULL, false), |
| 1169 |
XADC_US_CHAN_VOLTAGE(21, 28, XADC_REG_VAUX(12), NULL, false), |
| 1170 |
XADC_US_CHAN_VOLTAGE(22, 29, XADC_REG_VAUX(13), NULL, false), |
| 1171 |
XADC_US_CHAN_VOLTAGE(23, 30, XADC_REG_VAUX(14), NULL, false), |
| 1172 |
XADC_US_CHAN_VOLTAGE(24, 31, XADC_REG_VAUX(15), NULL, false), |
| 1173 |
}; |
| 1174 |
|
| 1175 |
static const struct iio_info xadc_info = { |
| 1176 |
.read_raw = &xadc_read_raw, |
| 1177 |
.write_raw = &xadc_write_raw, |
| 1178 |
.read_event_config = &xadc_read_event_config, |
| 1179 |
.write_event_config = &xadc_write_event_config, |
| 1180 |
.read_event_value = &xadc_read_event_value, |
| 1181 |
.write_event_value = &xadc_write_event_value, |
| 1182 |
.update_scan_mode = &xadc_update_scan_mode, |
| 1183 |
}; |
| 1184 |
|
| 1185 |
static const struct of_device_id xadc_of_match_table[] = { |
| 1186 |
{ |
| 1187 |
.compatible = "xlnx,zynq-xadc-1.00.a", |
| 1188 |
.data = &xadc_zynq_ops |
| 1189 |
}, { |
| 1190 |
.compatible = "xlnx,axi-xadc-1.00.a", |
| 1191 |
.data = &xadc_7s_axi_ops |
| 1192 |
}, { |
| 1193 |
.compatible = "xlnx,system-management-wiz-1.3", |
| 1194 |
.data = &xadc_us_axi_ops |
| 1195 |
}, |
| 1196 |
{ } |
| 1197 |
}; |
| 1198 |
MODULE_DEVICE_TABLE(of, xadc_of_match_table); |
| 1199 |
|
| 1200 |
static int xadc_parse_dt(struct iio_dev *indio_dev, unsigned int *conf, int irq) |
| 1201 |
{ |
| 1202 |
struct device *dev = indio_dev->dev.parent; |
| 1203 |
struct xadc *xadc = iio_priv(indio_dev); |
| 1204 |
const struct iio_chan_spec *channel_templates; |
| 1205 |
struct iio_chan_spec *channels, *chan; |
| 1206 |
struct fwnode_handle *chan_node, *child; |
| 1207 |
unsigned int max_channels; |
| 1208 |
unsigned int num_channels; |
| 1209 |
const char *external_mux; |
| 1210 |
u32 ext_mux_chan; |
| 1211 |
u32 reg; |
| 1212 |
int ret; |
| 1213 |
int i; |
| 1214 |
|
| 1215 |
*conf = 0; |
| 1216 |
|
| 1217 |
ret = device_property_read_string(dev, "xlnx,external-mux", &external_mux); |
| 1218 |
if (ret < 0 || strcasecmp(external_mux, "none") == 0) |
| 1219 |
xadc->external_mux_mode = XADC_EXTERNAL_MUX_NONE; |
| 1220 |
else if (strcasecmp(external_mux, "single") == 0) |
| 1221 |
xadc->external_mux_mode = XADC_EXTERNAL_MUX_SINGLE; |
| 1222 |
else if (strcasecmp(external_mux, "dual") == 0) |
| 1223 |
xadc->external_mux_mode = XADC_EXTERNAL_MUX_DUAL; |
| 1224 |
else |
| 1225 |
return -EINVAL; |
| 1226 |
|
| 1227 |
if (xadc->external_mux_mode != XADC_EXTERNAL_MUX_NONE) { |
| 1228 |
ret = device_property_read_u32(dev, "xlnx,external-mux-channel", &ext_mux_chan); |
| 1229 |
if (ret < 0) |
| 1230 |
return ret; |
| 1231 |
|
| 1232 |
if (xadc->external_mux_mode == XADC_EXTERNAL_MUX_SINGLE) { |
| 1233 |
if (ext_mux_chan == 0) |
| 1234 |
ext_mux_chan = XADC_REG_VPVN; |
| 1235 |
else if (ext_mux_chan <= 16) |
| 1236 |
ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1); |
| 1237 |
else |
| 1238 |
return -EINVAL; |
| 1239 |
} else { |
| 1240 |
if (ext_mux_chan > 0 && ext_mux_chan <= 8) |
| 1241 |
ext_mux_chan = XADC_REG_VAUX(ext_mux_chan - 1); |
| 1242 |
else |
| 1243 |
return -EINVAL; |
| 1244 |
} |
| 1245 |
|
| 1246 |
*conf |= XADC_CONF0_MUX | XADC_CONF0_CHAN(ext_mux_chan); |
| 1247 |
} |
| 1248 |
if (xadc->ops->type == XADC_TYPE_S7) { |
| 1249 |
channel_templates = xadc_7s_channels; |
| 1250 |
max_channels = ARRAY_SIZE(xadc_7s_channels); |
| 1251 |
} else { |
| 1252 |
channel_templates = xadc_us_channels; |
| 1253 |
max_channels = ARRAY_SIZE(xadc_us_channels); |
| 1254 |
} |
| 1255 |
channels = devm_kmemdup_array(dev, channel_templates, max_channels, |
| 1256 |
sizeof(*channel_templates), GFP_KERNEL); |
| 1257 |
if (!channels) |
| 1258 |
return -ENOMEM; |
| 1259 |
|
| 1260 |
num_channels = 9; |
| 1261 |
chan = &channels[9]; |
| 1262 |
|
| 1263 |
chan_node = device_get_named_child_node(dev, "xlnx,channels"); |
| 1264 |
fwnode_for_each_child_node(chan_node, child) { |
| 1265 |
if (num_channels >= max_channels) { |
| 1266 |
fwnode_handle_put(child); |
| 1267 |
break; |
| 1268 |
} |
| 1269 |
|
| 1270 |
ret = fwnode_property_read_u32(child, "reg", ®); |
| 1271 |
if (ret || reg > 16) |
| 1272 |
continue; |
| 1273 |
|
| 1274 |
if (fwnode_property_read_bool(child, "xlnx,bipolar")) |
| 1275 |
chan->scan_type.sign = 's'; |
| 1276 |
|
| 1277 |
if (reg == 0) { |
| 1278 |
chan->scan_index = 11; |
| 1279 |
chan->address = XADC_REG_VPVN; |
| 1280 |
} else { |
| 1281 |
chan->scan_index = 15 + reg; |
| 1282 |
chan->address = XADC_REG_VAUX(reg - 1); |
| 1283 |
} |
| 1284 |
num_channels++; |
| 1285 |
chan++; |
| 1286 |
} |
| 1287 |
fwnode_handle_put(chan_node); |
| 1288 |
|
| 1289 |
/* No IRQ => no events */ |
| 1290 |
if (irq <= 0) { |
| 1291 |
for (i = 0; i < num_channels; i++) { |
| 1292 |
channels[i].event_spec = NULL; |
| 1293 |
channels[i].num_event_specs = 0; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
indio_dev->num_channels = num_channels; |
| 1298 |
indio_dev->channels = devm_krealloc_array(dev, channels, |
| 1299 |
num_channels, sizeof(*channels), |
| 1300 |
GFP_KERNEL); |
| 1301 |
/* If we can't resize the channels array, just use the original */ |
| 1302 |
if (!indio_dev->channels) |
| 1303 |
indio_dev->channels = channels; |
| 1304 |
|
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
static const char * const xadc_type_names[] = { |
| 1309 |
[XADC_TYPE_S7] = "xadc", |
| 1310 |
[XADC_TYPE_US] = "xilinx-system-monitor", |
| 1311 |
}; |
| 1312 |
|
| 1313 |
static void xadc_cancel_delayed_work(void *data) |
| 1314 |
{ |
| 1315 |
struct delayed_work *work = data; |
| 1316 |
|
| 1317 |
cancel_delayed_work_sync(work); |
| 1318 |
} |
| 1319 |
|
| 1320 |
static int xadc_probe(struct platform_device *pdev) |
| 1321 |
{ |
| 1322 |
struct device *dev = &pdev->dev; |
| 1323 |
const struct xadc_ops *ops; |
| 1324 |
struct iio_dev *indio_dev; |
| 1325 |
unsigned int bipolar_mask; |
| 1326 |
unsigned int conf0; |
| 1327 |
struct xadc *xadc; |
| 1328 |
int ret; |
| 1329 |
int irq; |
| 1330 |
int i; |
| 1331 |
|
| 1332 |
ops = device_get_match_data(dev); |
| 1333 |
if (!ops) |
| 1334 |
return -EINVAL; |
| 1335 |
|
| 1336 |
irq = platform_get_irq_optional(pdev, 0); |
| 1337 |
if (irq < 0 && |
| 1338 |
(irq != -ENXIO || !(ops->flags & XADC_FLAGS_IRQ_OPTIONAL))) |
| 1339 |
return irq; |
| 1340 |
|
| 1341 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*xadc)); |
| 1342 |
if (!indio_dev) |
| 1343 |
return -ENOMEM; |
| 1344 |
|
| 1345 |
xadc = iio_priv(indio_dev); |
| 1346 |
xadc->ops = ops; |
| 1347 |
init_completion(&xadc->completion); |
| 1348 |
mutex_init(&xadc->mutex); |
| 1349 |
spin_lock_init(&xadc->lock); |
| 1350 |
INIT_DELAYED_WORK(&xadc->zynq_unmask_work, xadc_zynq_unmask_worker); |
| 1351 |
|
| 1352 |
xadc->base = devm_platform_ioremap_resource(pdev, 0); |
| 1353 |
if (IS_ERR(xadc->base)) |
| 1354 |
return PTR_ERR(xadc->base); |
| 1355 |
|
| 1356 |
indio_dev->name = xadc_type_names[xadc->ops->type]; |
| 1357 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1358 |
indio_dev->info = &xadc_info; |
| 1359 |
|
| 1360 |
ret = xadc_parse_dt(indio_dev, &conf0, irq); |
| 1361 |
if (ret) |
| 1362 |
return ret; |
| 1363 |
|
| 1364 |
if (xadc->ops->flags & XADC_FLAGS_BUFFERED) { |
| 1365 |
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, |
| 1366 |
&iio_pollfunc_store_time, |
| 1367 |
&xadc_trigger_handler, |
| 1368 |
&xadc_buffer_ops); |
| 1369 |
if (ret) |
| 1370 |
return ret; |
| 1371 |
|
| 1372 |
if (irq > 0) { |
| 1373 |
xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst"); |
| 1374 |
if (IS_ERR(xadc->convst_trigger)) |
| 1375 |
return PTR_ERR(xadc->convst_trigger); |
| 1376 |
|
| 1377 |
xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev, |
| 1378 |
"samplerate"); |
| 1379 |
if (IS_ERR(xadc->samplerate_trigger)) |
| 1380 |
return PTR_ERR(xadc->samplerate_trigger); |
| 1381 |
} |
| 1382 |
} |
| 1383 |
|
| 1384 |
xadc->clk = devm_clk_get_enabled(dev, NULL); |
| 1385 |
if (IS_ERR(xadc->clk)) |
| 1386 |
return PTR_ERR(xadc->clk); |
| 1387 |
|
| 1388 |
/* |
| 1389 |
* Make sure not to exceed the maximum samplerate since otherwise the |
| 1390 |
* resulting interrupt storm will soft-lock the system. |
| 1391 |
*/ |
| 1392 |
if (xadc->ops->flags & XADC_FLAGS_BUFFERED) { |
| 1393 |
ret = xadc_read_samplerate(xadc); |
| 1394 |
if (ret < 0) |
| 1395 |
return ret; |
| 1396 |
|
| 1397 |
if (ret > XADC_MAX_SAMPLERATE) { |
| 1398 |
ret = xadc_write_samplerate(xadc, XADC_MAX_SAMPLERATE); |
| 1399 |
if (ret < 0) |
| 1400 |
return ret; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
if (irq > 0) { |
| 1405 |
ret = devm_request_irq(dev, irq, xadc->ops->interrupt_handler, |
| 1406 |
0, dev_name(dev), indio_dev); |
| 1407 |
if (ret) |
| 1408 |
return ret; |
| 1409 |
|
| 1410 |
ret = devm_add_action_or_reset(dev, xadc_cancel_delayed_work, |
| 1411 |
&xadc->zynq_unmask_work); |
| 1412 |
if (ret) |
| 1413 |
return ret; |
| 1414 |
} |
| 1415 |
|
| 1416 |
ret = xadc->ops->setup(pdev, indio_dev, irq); |
| 1417 |
if (ret) |
| 1418 |
return ret; |
| 1419 |
|
| 1420 |
for (i = 0; i < 16; i++) |
| 1421 |
xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i), |
| 1422 |
&xadc->threshold[i]); |
| 1423 |
|
| 1424 |
ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0); |
| 1425 |
if (ret) |
| 1426 |
return ret; |
| 1427 |
|
| 1428 |
bipolar_mask = 0; |
| 1429 |
for (i = 0; i < indio_dev->num_channels; i++) { |
| 1430 |
if (indio_dev->channels[i].scan_type.sign == 's') |
| 1431 |
bipolar_mask |= BIT(indio_dev->channels[i].scan_index); |
| 1432 |
} |
| 1433 |
|
| 1434 |
ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask); |
| 1435 |
if (ret) |
| 1436 |
return ret; |
| 1437 |
|
| 1438 |
ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1), |
| 1439 |
bipolar_mask >> 16); |
| 1440 |
if (ret) |
| 1441 |
return ret; |
| 1442 |
|
| 1443 |
/* Go to non-buffered mode */ |
| 1444 |
xadc_postdisable(indio_dev); |
| 1445 |
|
| 1446 |
return devm_iio_device_register(dev, indio_dev); |
| 1447 |
} |
| 1448 |
|
| 1449 |
static struct platform_driver xadc_driver = { |
| 1450 |
.probe = xadc_probe, |
| 1451 |
.driver = { |
| 1452 |
.name = "xadc", |
| 1453 |
.of_match_table = xadc_of_match_table, |
| 1454 |
}, |
| 1455 |
}; |
| 1456 |
module_platform_driver(xadc_driver); |
| 1457 |
|
| 1458 |
MODULE_LICENSE("GPL v2"); |
| 1459 |
MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>"); |
| 1460 |
MODULE_DESCRIPTION("Xilinx XADC IIO driver"); |