| 1 |
|
| 2 |
|
| 3 |
#include <linux/array_size.h> |
| 4 |
#include <linux/bitfield.h> |
| 5 |
#include <linux/bits.h> |
| 6 |
#include <linux/dev_printk.h> |
| 7 |
#include <linux/device.h> |
| 8 |
#include <linux/export.h> |
| 9 |
#include <linux/math64.h> |
| 10 |
#include <linux/module.h> |
| 11 |
#include <linux/netlink.h> |
| 12 |
#include <linux/regmap.h> |
| 13 |
#include <linux/sprintf.h> |
| 14 |
#include <linux/string_choices.h> |
| 15 |
#include <linux/unaligned.h> |
| 16 |
#include <net/devlink.h> |
| 17 |
|
| 18 |
#include "core.h" |
| 19 |
#include "devlink.h" |
| 20 |
#include "dpll.h" |
| 21 |
#include "regs.h" |
| 22 |
|
| 23 |
|
| 24 |
static const u16 zl30731_ids[] = { |
| 25 |
0x0E93, |
| 26 |
0x1E93, |
| 27 |
0x2E93, |
| 28 |
}; |
| 29 |
|
| 30 |
const struct zl3073x_chip_info zl30731_chip_info = { |
| 31 |
.ids = zl30731_ids, |
| 32 |
.num_ids = ARRAY_SIZE(zl30731_ids), |
| 33 |
.num_channels = 1, |
| 34 |
}; |
| 35 |
EXPORT_SYMBOL_NS_GPL(zl30731_chip_info, "ZL3073X"); |
| 36 |
|
| 37 |
|
| 38 |
static const u16 zl30732_ids[] = { |
| 39 |
0x0E30, |
| 40 |
0x0E94, |
| 41 |
0x1E94, |
| 42 |
0x1F60, |
| 43 |
0x2E94, |
| 44 |
0x3FC4, |
| 45 |
}; |
| 46 |
|
| 47 |
const struct zl3073x_chip_info zl30732_chip_info = { |
| 48 |
.ids = zl30732_ids, |
| 49 |
.num_ids = ARRAY_SIZE(zl30732_ids), |
| 50 |
.num_channels = 2, |
| 51 |
}; |
| 52 |
EXPORT_SYMBOL_NS_GPL(zl30732_chip_info, "ZL3073X"); |
| 53 |
|
| 54 |
|
| 55 |
static const u16 zl30733_ids[] = { |
| 56 |
0x0E95, |
| 57 |
0x1E95, |
| 58 |
0x2E95, |
| 59 |
}; |
| 60 |
|
| 61 |
const struct zl3073x_chip_info zl30733_chip_info = { |
| 62 |
.ids = zl30733_ids, |
| 63 |
.num_ids = ARRAY_SIZE(zl30733_ids), |
| 64 |
.num_channels = 3, |
| 65 |
}; |
| 66 |
EXPORT_SYMBOL_NS_GPL(zl30733_chip_info, "ZL3073X"); |
| 67 |
|
| 68 |
|
| 69 |
static const u16 zl30734_ids[] = { |
| 70 |
0x0E96, |
| 71 |
0x1E96, |
| 72 |
0x2E96, |
| 73 |
}; |
| 74 |
|
| 75 |
const struct zl3073x_chip_info zl30734_chip_info = { |
| 76 |
.ids = zl30734_ids, |
| 77 |
.num_ids = ARRAY_SIZE(zl30734_ids), |
| 78 |
.num_channels = 4, |
| 79 |
}; |
| 80 |
EXPORT_SYMBOL_NS_GPL(zl30734_chip_info, "ZL3073X"); |
| 81 |
|
| 82 |
|
| 83 |
static const u16 zl30735_ids[] = { |
| 84 |
0x0E97, |
| 85 |
0x1E97, |
| 86 |
0x2E97, |
| 87 |
}; |
| 88 |
|
| 89 |
const struct zl3073x_chip_info zl30735_chip_info = { |
| 90 |
.ids = zl30735_ids, |
| 91 |
.num_ids = ARRAY_SIZE(zl30735_ids), |
| 92 |
.num_channels = 5, |
| 93 |
}; |
| 94 |
EXPORT_SYMBOL_NS_GPL(zl30735_chip_info, "ZL3073X"); |
| 95 |
|
| 96 |
#define ZL_RANGE_OFFSET 0x80 |
| 97 |
#define ZL_PAGE_SIZE 0x80 |
| 98 |
#define ZL_NUM_PAGES 256 |
| 99 |
#define ZL_PAGE_SEL 0x7F |
| 100 |
#define ZL_PAGE_SEL_MASK GENMASK(7, 0) |
| 101 |
#define ZL_NUM_REGS (ZL_NUM_PAGES * ZL_PAGE_SIZE) |
| 102 |
|
| 103 |
|
| 104 |
static const struct regmap_range_cfg zl3073x_regmap_range = { |
| 105 |
.range_min = ZL_RANGE_OFFSET, |
| 106 |
.range_max = ZL_RANGE_OFFSET + ZL_NUM_REGS - 1, |
| 107 |
.selector_reg = ZL_PAGE_SEL, |
| 108 |
.selector_mask = ZL_PAGE_SEL_MASK, |
| 109 |
.selector_shift = 0, |
| 110 |
.window_start = 0, |
| 111 |
.window_len = ZL_PAGE_SIZE, |
| 112 |
}; |
| 113 |
|
| 114 |
static bool |
| 115 |
zl3073x_is_volatile_reg(struct device *dev __maybe_unused, unsigned int reg) |
| 116 |
{ |
| 117 |
|
| 118 |
return reg != ZL_PAGE_SEL; |
| 119 |
} |
| 120 |
|
| 121 |
const struct regmap_config zl3073x_regmap_config = { |
| 122 |
.reg_bits = 8, |
| 123 |
.val_bits = 8, |
| 124 |
.max_register = ZL_RANGE_OFFSET + ZL_NUM_REGS - 1, |
| 125 |
.ranges = &zl3073x_regmap_range, |
| 126 |
.num_ranges = 1, |
| 127 |
.cache_type = REGCACHE_MAPLE, |
| 128 |
.volatile_reg = zl3073x_is_volatile_reg, |
| 129 |
}; |
| 130 |
EXPORT_SYMBOL_NS_GPL(zl3073x_regmap_config, "ZL3073X"); |
| 131 |
|
| 132 |
static bool |
| 133 |
zl3073x_check_reg(struct zl3073x_dev *zldev, unsigned int reg, size_t size) |
| 134 |
{ |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if (ZL_REG_PAGE(reg) >= 10 && ZL_REG_PAGE(reg) < 255) |
| 140 |
lockdep_assert_held(&zldev->multiop_lock); |
| 141 |
|
| 142 |
|
| 143 |
if (ZL_REG_OFFSET(reg) > ZL_REG_MAX_OFFSET(reg)) { |
| 144 |
dev_err(zldev->dev, "Index out of range for reg 0x%04lx\n", |
| 145 |
ZL_REG_ADDR(reg)); |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
if (ZL_REG_SIZE(reg) != size) { |
| 150 |
dev_err(zldev->dev, "Invalid size %zu for reg 0x%04lx\n", |
| 151 |
size, ZL_REG_ADDR(reg)); |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
static int |
| 159 |
zl3073x_read_reg(struct zl3073x_dev *zldev, unsigned int reg, void *val, |
| 160 |
size_t size) |
| 161 |
{ |
| 162 |
int rc; |
| 163 |
|
| 164 |
if (!zl3073x_check_reg(zldev, reg, size)) |
| 165 |
return -EINVAL; |
| 166 |
|
| 167 |
|
| 168 |
reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; |
| 169 |
|
| 170 |
rc = regmap_bulk_read(zldev->regmap, reg, val, size); |
| 171 |
if (rc) { |
| 172 |
dev_err(zldev->dev, "Failed to read reg 0x%04x: %pe\n", reg, |
| 173 |
ERR_PTR(rc)); |
| 174 |
return rc; |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static int |
| 181 |
zl3073x_write_reg(struct zl3073x_dev *zldev, unsigned int reg, const void *val, |
| 182 |
size_t size) |
| 183 |
{ |
| 184 |
int rc; |
| 185 |
|
| 186 |
if (!zl3073x_check_reg(zldev, reg, size)) |
| 187 |
return -EINVAL; |
| 188 |
|
| 189 |
|
| 190 |
reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; |
| 191 |
|
| 192 |
rc = regmap_bulk_write(zldev->regmap, reg, val, size); |
| 193 |
if (rc) { |
| 194 |
dev_err(zldev->dev, "Failed to write reg 0x%04x: %pe\n", reg, |
| 195 |
ERR_PTR(rc)); |
| 196 |
return rc; |
| 197 |
} |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
int zl3073x_read_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 *val) |
| 213 |
{ |
| 214 |
return zl3073x_read_reg(zldev, reg, val, sizeof(*val)); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
int zl3073x_write_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 val) |
| 228 |
{ |
| 229 |
return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
int zl3073x_read_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 *val) |
| 243 |
{ |
| 244 |
int rc; |
| 245 |
|
| 246 |
rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val)); |
| 247 |
if (!rc) |
| 248 |
be16_to_cpus(val); |
| 249 |
|
| 250 |
return rc; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
int zl3073x_write_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 val) |
| 264 |
{ |
| 265 |
cpu_to_be16s(&val); |
| 266 |
|
| 267 |
return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
int zl3073x_read_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 *val) |
| 281 |
{ |
| 282 |
int rc; |
| 283 |
|
| 284 |
rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val)); |
| 285 |
if (!rc) |
| 286 |
be32_to_cpus(val); |
| 287 |
|
| 288 |
return rc; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
int zl3073x_write_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 val) |
| 302 |
{ |
| 303 |
cpu_to_be32s(&val); |
| 304 |
|
| 305 |
return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
int zl3073x_read_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 *val) |
| 319 |
{ |
| 320 |
u8 buf[6]; |
| 321 |
int rc; |
| 322 |
|
| 323 |
rc = zl3073x_read_reg(zldev, reg, buf, sizeof(buf)); |
| 324 |
if (!rc) |
| 325 |
*val = get_unaligned_be48(buf); |
| 326 |
|
| 327 |
return rc; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
int zl3073x_write_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 val) |
| 342 |
{ |
| 343 |
u8 buf[6]; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
if (val > GENMASK_ULL(47, 0) && val < GENMASK_ULL(63, 47)) { |
| 349 |
dev_err(zldev->dev, "Value 0x%0llx out of range\n", val); |
| 350 |
return -EINVAL; |
| 351 |
} |
| 352 |
|
| 353 |
put_unaligned_be48(val, buf); |
| 354 |
|
| 355 |
return zl3073x_write_reg(zldev, reg, buf, sizeof(buf)); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
int zl3073x_poll_zero_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 mask) |
| 370 |
{ |
| 371 |
|
| 372 |
#define ZL_POLL_SLEEP_US 10 |
| 373 |
#define ZL_POLL_TIMEOUT_US 2000000 |
| 374 |
unsigned int val; |
| 375 |
|
| 376 |
|
| 377 |
if (ZL_REG_SIZE(reg) != 1) { |
| 378 |
dev_err(zldev->dev, "Invalid reg 0x%04lx size for polling\n", |
| 379 |
ZL_REG_ADDR(reg)); |
| 380 |
return -EINVAL; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; |
| 385 |
|
| 386 |
return regmap_read_poll_timeout(zldev->regmap, reg, val, !(val & mask), |
| 387 |
ZL_POLL_SLEEP_US, ZL_POLL_TIMEOUT_US); |
| 388 |
} |
| 389 |
|
| 390 |
int zl3073x_mb_op(struct zl3073x_dev *zldev, unsigned int op_reg, u8 op_val, |
| 391 |
unsigned int mask_reg, u16 mask_val) |
| 392 |
{ |
| 393 |
int rc; |
| 394 |
|
| 395 |
|
| 396 |
rc = zl3073x_write_u16(zldev, mask_reg, mask_val); |
| 397 |
if (rc) |
| 398 |
return rc; |
| 399 |
|
| 400 |
|
| 401 |
rc = zl3073x_write_u8(zldev, op_reg, op_val); |
| 402 |
if (rc) |
| 403 |
return rc; |
| 404 |
|
| 405 |
|
| 406 |
return zl3073x_poll_zero_u8(zldev, op_reg, op_val); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
static int |
| 419 |
zl3073x_do_hwreg_op(struct zl3073x_dev *zldev, u8 op) |
| 420 |
{ |
| 421 |
int rc; |
| 422 |
|
| 423 |
|
| 424 |
rc = zl3073x_write_u8(zldev, ZL_REG_HWREG_OP, op | ZL_HWREG_OP_PENDING); |
| 425 |
if (rc) |
| 426 |
return rc; |
| 427 |
|
| 428 |
|
| 429 |
return zl3073x_poll_zero_u8(zldev, ZL_REG_HWREG_OP, |
| 430 |
ZL_HWREG_OP_PENDING); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
int zl3073x_read_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 *value) |
| 444 |
{ |
| 445 |
int rc; |
| 446 |
|
| 447 |
|
| 448 |
rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr); |
| 449 |
if (rc) |
| 450 |
return rc; |
| 451 |
|
| 452 |
|
| 453 |
rc = zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_READ); |
| 454 |
if (rc) |
| 455 |
return rc; |
| 456 |
|
| 457 |
|
| 458 |
return zl3073x_read_u32(zldev, ZL_REG_HWREG_READ_DATA, value); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
int zl3073x_write_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value) |
| 472 |
{ |
| 473 |
int rc; |
| 474 |
|
| 475 |
|
| 476 |
rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr); |
| 477 |
if (rc) |
| 478 |
return rc; |
| 479 |
|
| 480 |
|
| 481 |
rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_WRITE_DATA, value); |
| 482 |
if (rc) |
| 483 |
return rc; |
| 484 |
|
| 485 |
|
| 486 |
return zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_WRITE); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
int zl3073x_update_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value, |
| 502 |
u32 mask) |
| 503 |
{ |
| 504 |
u32 tmp; |
| 505 |
int rc; |
| 506 |
|
| 507 |
rc = zl3073x_read_hwreg(zldev, addr, &tmp); |
| 508 |
if (rc) |
| 509 |
return rc; |
| 510 |
|
| 511 |
tmp &= ~mask; |
| 512 |
tmp |= value & mask; |
| 513 |
|
| 514 |
return zl3073x_write_hwreg(zldev, addr, tmp); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
int zl3073x_write_hwreg_seq(struct zl3073x_dev *zldev, |
| 528 |
const struct zl3073x_hwreg_seq_item *seq, |
| 529 |
size_t num_items) |
| 530 |
{ |
| 531 |
int i, rc = 0; |
| 532 |
|
| 533 |
for (i = 0; i < num_items; i++) { |
| 534 |
dev_dbg(zldev->dev, "Write 0x%0x [0x%0x] to 0x%0x", |
| 535 |
seq[i].value, seq[i].mask, seq[i].addr); |
| 536 |
|
| 537 |
if (seq[i].mask == U32_MAX) |
| 538 |
|
| 539 |
rc = zl3073x_write_hwreg(zldev, seq[i].addr, |
| 540 |
seq[i].value); |
| 541 |
else |
| 542 |
|
| 543 |
rc = zl3073x_update_hwreg(zldev, seq[i].addr, |
| 544 |
seq[i].value, seq[i].mask); |
| 545 |
if (rc) |
| 546 |
return rc; |
| 547 |
|
| 548 |
if (seq->wait) |
| 549 |
msleep(seq->wait); |
| 550 |
} |
| 551 |
|
| 552 |
return rc; |
| 553 |
} |
| 554 |
|
| 555 |
static int |
| 556 |
zl3073x_dev_state_fetch(struct zl3073x_dev *zldev) |
| 557 |
{ |
| 558 |
int rc; |
| 559 |
u8 i; |
| 560 |
|
| 561 |
for (i = 0; i < ZL3073X_NUM_REFS; i++) { |
| 562 |
rc = zl3073x_ref_state_fetch(zldev, i); |
| 563 |
if (rc) { |
| 564 |
dev_err(zldev->dev, |
| 565 |
"Failed to fetch input state: %pe\n", |
| 566 |
ERR_PTR(rc)); |
| 567 |
return rc; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) { |
| 572 |
rc = zl3073x_synth_state_fetch(zldev, i); |
| 573 |
if (rc) { |
| 574 |
dev_err(zldev->dev, |
| 575 |
"Failed to fetch synth state: %pe\n", |
| 576 |
ERR_PTR(rc)); |
| 577 |
return rc; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
for (i = 0; i < ZL3073X_NUM_OUTS; i++) { |
| 582 |
rc = zl3073x_out_state_fetch(zldev, i); |
| 583 |
if (rc) { |
| 584 |
dev_err(zldev->dev, |
| 585 |
"Failed to fetch output state: %pe\n", |
| 586 |
ERR_PTR(rc)); |
| 587 |
return rc; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
return rc; |
| 592 |
} |
| 593 |
|
| 594 |
static void |
| 595 |
zl3073x_dev_ref_status_update(struct zl3073x_dev *zldev) |
| 596 |
{ |
| 597 |
int i, rc; |
| 598 |
|
| 599 |
for (i = 0; i < ZL3073X_NUM_REFS; i++) { |
| 600 |
rc = zl3073x_read_u8(zldev, ZL_REG_REF_MON_STATUS(i), |
| 601 |
&zldev->ref[i].mon_status); |
| 602 |
if (rc) |
| 603 |
dev_warn(zldev->dev, |
| 604 |
"Failed to get REF%u status: %pe\n", i, |
| 605 |
ERR_PTR(rc)); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
int zl3073x_ref_phase_offsets_update(struct zl3073x_dev *zldev, int channel) |
| 631 |
{ |
| 632 |
int rc; |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST, |
| 638 |
ZL_REF_PHASE_ERR_READ_RQST_RD); |
| 639 |
if (rc) |
| 640 |
return rc; |
| 641 |
|
| 642 |
|
| 643 |
if (channel != -1) { |
| 644 |
rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_IDX, channel); |
| 645 |
if (rc) |
| 646 |
return rc; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
rc = zl3073x_write_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST, |
| 651 |
ZL_REF_PHASE_ERR_READ_RQST_RD); |
| 652 |
if (rc) |
| 653 |
return rc; |
| 654 |
|
| 655 |
|
| 656 |
return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST, |
| 657 |
ZL_REF_PHASE_ERR_READ_RQST_RD); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
static int |
| 670 |
zl3073x_ref_ffo_update(struct zl3073x_dev *zldev) |
| 671 |
{ |
| 672 |
int i, rc; |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, |
| 678 |
ZL_REF_FREQ_MEAS_CTRL); |
| 679 |
if (rc) |
| 680 |
return rc; |
| 681 |
|
| 682 |
|
| 683 |
rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_3_0, |
| 684 |
GENMASK(7, 0)); |
| 685 |
if (rc) |
| 686 |
return rc; |
| 687 |
rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_4, |
| 688 |
GENMASK(1, 0)); |
| 689 |
if (rc) |
| 690 |
return rc; |
| 691 |
|
| 692 |
|
| 693 |
rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, |
| 694 |
ZL_REF_FREQ_MEAS_CTRL_REF_FREQ_OFF); |
| 695 |
if (rc) |
| 696 |
return rc; |
| 697 |
|
| 698 |
|
| 699 |
rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, |
| 700 |
ZL_REF_FREQ_MEAS_CTRL); |
| 701 |
if (rc) |
| 702 |
return rc; |
| 703 |
|
| 704 |
|
| 705 |
for (i = 0; i < ZL3073X_NUM_REFS; i++) { |
| 706 |
s32 value; |
| 707 |
|
| 708 |
|
| 709 |
rc = zl3073x_read_u32(zldev, ZL_REG_REF_FREQ(i), &value); |
| 710 |
if (rc) |
| 711 |
return rc; |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
zldev->ref[i].ffo = mul_s64_u64_shr(value, 244140625, 20); |
| 718 |
} |
| 719 |
|
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
static void |
| 724 |
zl3073x_dev_periodic_work(struct kthread_work *work) |
| 725 |
{ |
| 726 |
struct zl3073x_dev *zldev = container_of(work, struct zl3073x_dev, |
| 727 |
work.work); |
| 728 |
struct zl3073x_dpll *zldpll; |
| 729 |
int rc; |
| 730 |
|
| 731 |
|
| 732 |
zl3073x_dev_ref_status_update(zldev); |
| 733 |
|
| 734 |
|
| 735 |
rc = zl3073x_ref_phase_offsets_update(zldev, -1); |
| 736 |
if (rc) |
| 737 |
dev_warn(zldev->dev, "Failed to update phase offsets: %pe\n", |
| 738 |
ERR_PTR(rc)); |
| 739 |
|
| 740 |
|
| 741 |
rc = zl3073x_ref_ffo_update(zldev); |
| 742 |
if (rc) |
| 743 |
dev_warn(zldev->dev, |
| 744 |
"Failed to update fractional frequency offsets: %pe\n", |
| 745 |
ERR_PTR(rc)); |
| 746 |
|
| 747 |
list_for_each_entry(zldpll, &zldev->dplls, list) |
| 748 |
zl3073x_dpll_changes_check(zldpll); |
| 749 |
|
| 750 |
|
| 751 |
kthread_queue_delayed_work(zldev->kworker, &zldev->work, |
| 752 |
msecs_to_jiffies(500)); |
| 753 |
} |
| 754 |
|
| 755 |
int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor) |
| 756 |
{ |
| 757 |
u8 dpll_meas_ctrl, value; |
| 758 |
int rc; |
| 759 |
|
| 760 |
|
| 761 |
rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); |
| 762 |
if (rc) |
| 763 |
return rc; |
| 764 |
|
| 765 |
|
| 766 |
value = (factor + 1) & 0x0f; |
| 767 |
|
| 768 |
|
| 769 |
dpll_meas_ctrl &= ~ZL_DPLL_MEAS_CTRL_AVG_FACTOR; |
| 770 |
dpll_meas_ctrl |= FIELD_PREP(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, value); |
| 771 |
rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); |
| 772 |
if (rc) |
| 773 |
return rc; |
| 774 |
|
| 775 |
|
| 776 |
zldev->phase_avg_factor = factor; |
| 777 |
|
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
static int |
| 791 |
zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev) |
| 792 |
{ |
| 793 |
struct zl3073x_dpll *zldpll; |
| 794 |
u8 dpll_meas_ctrl, mask = 0; |
| 795 |
int rc; |
| 796 |
|
| 797 |
|
| 798 |
rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor); |
| 799 |
if (rc) |
| 800 |
return rc; |
| 801 |
|
| 802 |
|
| 803 |
rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); |
| 804 |
if (rc) |
| 805 |
return rc; |
| 806 |
|
| 807 |
|
| 808 |
dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN; |
| 809 |
|
| 810 |
|
| 811 |
rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); |
| 812 |
if (rc) |
| 813 |
return rc; |
| 814 |
|
| 815 |
|
| 816 |
list_for_each_entry(zldpll, &zldev->dplls, list) |
| 817 |
mask |= BIT(zldpll->id); |
| 818 |
|
| 819 |
return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask); |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full) |
| 835 |
{ |
| 836 |
struct zl3073x_dpll *zldpll; |
| 837 |
u8 info; |
| 838 |
int rc; |
| 839 |
|
| 840 |
rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info); |
| 841 |
if (rc) { |
| 842 |
dev_err(zldev->dev, "Failed to read device status info\n"); |
| 843 |
return rc; |
| 844 |
} |
| 845 |
|
| 846 |
if (!FIELD_GET(ZL_INFO_READY, info)) { |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
dev_info(zldev->dev, |
| 855 |
"FW not fully ready - missing or corrupted config\n"); |
| 856 |
|
| 857 |
return 0; |
| 858 |
} |
| 859 |
|
| 860 |
if (full) { |
| 861 |
|
| 862 |
rc = zl3073x_dev_state_fetch(zldev); |
| 863 |
if (rc) |
| 864 |
return rc; |
| 865 |
|
| 866 |
|
| 867 |
rc = zl3073x_dev_phase_meas_setup(zldev); |
| 868 |
if (rc) { |
| 869 |
dev_err(zldev->dev, |
| 870 |
"Failed to setup phase measurement\n"); |
| 871 |
return rc; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
list_for_each_entry(zldpll, &zldev->dplls, list) { |
| 877 |
rc = zl3073x_dpll_register(zldpll); |
| 878 |
if (rc) { |
| 879 |
dev_err_probe(zldev->dev, rc, |
| 880 |
"Failed to register DPLL%u\n", |
| 881 |
zldpll->id); |
| 882 |
return rc; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
rc = zl3073x_dpll_init_fine_phase_adjust(zldev); |
| 888 |
if (rc) { |
| 889 |
dev_err_probe(zldev->dev, rc, |
| 890 |
"Failed to init fine phase correction\n"); |
| 891 |
return rc; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0); |
| 896 |
|
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
void zl3073x_dev_stop(struct zl3073x_dev *zldev) |
| 910 |
{ |
| 911 |
struct zl3073x_dpll *zldpll; |
| 912 |
|
| 913 |
|
| 914 |
kthread_cancel_delayed_work_sync(&zldev->work); |
| 915 |
|
| 916 |
|
| 917 |
list_for_each_entry(zldpll, &zldev->dplls, list) { |
| 918 |
if (zldpll->dpll_dev) |
| 919 |
zl3073x_dpll_unregister(zldpll); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
static void zl3073x_dev_dpll_fini(void *ptr) |
| 924 |
{ |
| 925 |
struct zl3073x_dpll *zldpll, *next; |
| 926 |
struct zl3073x_dev *zldev = ptr; |
| 927 |
|
| 928 |
|
| 929 |
zl3073x_dev_stop(zldev); |
| 930 |
|
| 931 |
|
| 932 |
if (zldev->kworker) { |
| 933 |
kthread_destroy_worker(zldev->kworker); |
| 934 |
zldev->kworker = NULL; |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) { |
| 939 |
list_del(&zldpll->list); |
| 940 |
zl3073x_dpll_free(zldpll); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
static int |
| 945 |
zl3073x_devm_dpll_init(struct zl3073x_dev *zldev, u8 num_dplls) |
| 946 |
{ |
| 947 |
struct kthread_worker *kworker; |
| 948 |
struct zl3073x_dpll *zldpll; |
| 949 |
unsigned int i; |
| 950 |
int rc; |
| 951 |
|
| 952 |
INIT_LIST_HEAD(&zldev->dplls); |
| 953 |
|
| 954 |
|
| 955 |
for (i = 0; i < num_dplls; i++) { |
| 956 |
zldpll = zl3073x_dpll_alloc(zldev, i); |
| 957 |
if (IS_ERR(zldpll)) { |
| 958 |
dev_err_probe(zldev->dev, PTR_ERR(zldpll), |
| 959 |
"Failed to alloc DPLL%u\n", i); |
| 960 |
rc = PTR_ERR(zldpll); |
| 961 |
goto error; |
| 962 |
} |
| 963 |
|
| 964 |
list_add_tail(&zldpll->list, &zldev->dplls); |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work); |
| 969 |
kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev)); |
| 970 |
if (IS_ERR(kworker)) { |
| 971 |
rc = PTR_ERR(kworker); |
| 972 |
goto error; |
| 973 |
} |
| 974 |
zldev->kworker = kworker; |
| 975 |
|
| 976 |
|
| 977 |
rc = zl3073x_dev_start(zldev, true); |
| 978 |
if (rc) { |
| 979 |
dev_err_probe(zldev->dev, rc, "Failed to start device\n"); |
| 980 |
goto error; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
return devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev); |
| 985 |
|
| 986 |
error: |
| 987 |
zl3073x_dev_dpll_fini(zldev); |
| 988 |
|
| 989 |
return rc; |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
int zl3073x_dev_probe(struct zl3073x_dev *zldev, |
| 1002 |
const struct zl3073x_chip_info *chip_info) |
| 1003 |
{ |
| 1004 |
u16 id, revision, fw_ver; |
| 1005 |
unsigned int i; |
| 1006 |
u32 cfg_ver; |
| 1007 |
int rc; |
| 1008 |
|
| 1009 |
|
| 1010 |
rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id); |
| 1011 |
if (rc) |
| 1012 |
return rc; |
| 1013 |
|
| 1014 |
|
| 1015 |
for (i = 0; i < chip_info->num_ids; i++) { |
| 1016 |
if (id == chip_info->ids[i]) |
| 1017 |
break; |
| 1018 |
} |
| 1019 |
|
| 1020 |
if (i == chip_info->num_ids) { |
| 1021 |
return dev_err_probe(zldev->dev, -ENODEV, |
| 1022 |
"Unknown or non-match chip ID: 0x%0x\n", |
| 1023 |
id); |
| 1024 |
} |
| 1025 |
zldev->chip_id = id; |
| 1026 |
|
| 1027 |
|
| 1028 |
rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision); |
| 1029 |
if (rc) |
| 1030 |
return rc; |
| 1031 |
rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver); |
| 1032 |
if (rc) |
| 1033 |
return rc; |
| 1034 |
rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver); |
| 1035 |
if (rc) |
| 1036 |
return rc; |
| 1037 |
|
| 1038 |
dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id, |
| 1039 |
revision, fw_ver); |
| 1040 |
dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n", |
| 1041 |
FIELD_GET(GENMASK(31, 24), cfg_ver), |
| 1042 |
FIELD_GET(GENMASK(23, 16), cfg_ver), |
| 1043 |
FIELD_GET(GENMASK(15, 8), cfg_ver), |
| 1044 |
FIELD_GET(GENMASK(7, 0), cfg_ver)); |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
zldev->clock_id = get_random_u64(); |
| 1051 |
|
| 1052 |
|
| 1053 |
zldev->phase_avg_factor = 2; |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock); |
| 1059 |
if (rc) |
| 1060 |
return dev_err_probe(zldev->dev, rc, |
| 1061 |
"Failed to initialize mutex\n"); |
| 1062 |
|
| 1063 |
|
| 1064 |
rc = zl3073x_devm_dpll_init(zldev, chip_info->num_channels); |
| 1065 |
if (rc) |
| 1066 |
return rc; |
| 1067 |
|
| 1068 |
|
| 1069 |
rc = zl3073x_devlink_register(zldev); |
| 1070 |
if (rc) |
| 1071 |
return dev_err_probe(zldev->dev, rc, |
| 1072 |
"Failed to register devlink instance\n"); |
| 1073 |
|
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X"); |
| 1077 |
|
| 1078 |
MODULE_AUTHOR("Ivan Vecera <[email protected]>"); |
| 1079 |
MODULE_DESCRIPTION("Microchip ZL3073x core driver"); |
| 1080 |
MODULE_LICENSE("GPL"); |