| 1 |
|
| 2 |
|
| 3 |
#include <linux/bits.h> |
| 4 |
#include <linux/bitfield.h> |
| 5 |
#include <linux/bug.h> |
| 6 |
#include <linux/container_of.h> |
| 7 |
#include <linux/dev_printk.h> |
| 8 |
#include <linux/dpll.h> |
| 9 |
#include <linux/err.h> |
| 10 |
#include <linux/kthread.h> |
| 11 |
#include <linux/math64.h> |
| 12 |
#include <linux/mod_devicetable.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/netlink.h> |
| 15 |
#include <linux/platform_device.h> |
| 16 |
#include <linux/slab.h> |
| 17 |
#include <linux/sprintf.h> |
| 18 |
|
| 19 |
#include "core.h" |
| 20 |
#include "dpll.h" |
| 21 |
#include "prop.h" |
| 22 |
#include "regs.h" |
| 23 |
|
| 24 |
#define ZL3073X_DPLL_REF_NONE ZL3073X_NUM_REFS |
| 25 |
#define ZL3073X_DPLL_REF_IS_VALID(_ref) ((_ref) != ZL3073X_DPLL_REF_NONE) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
struct zl3073x_dpll_pin { |
| 45 |
struct list_head list; |
| 46 |
struct zl3073x_dpll *dpll; |
| 47 |
struct dpll_pin *dpll_pin; |
| 48 |
dpll_tracker tracker; |
| 49 |
char label[8]; |
| 50 |
enum dpll_pin_direction dir; |
| 51 |
u8 id; |
| 52 |
u8 prio; |
| 53 |
bool selectable; |
| 54 |
bool esync_control; |
| 55 |
s32 phase_gran; |
| 56 |
enum dpll_pin_state pin_state; |
| 57 |
s64 phase_offset; |
| 58 |
s64 freq_offset; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static const struct dpll_pin_frequency esync_freq_ranges[] = { |
| 65 |
DPLL_PIN_FREQUENCY_RANGE(0, 1), |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static bool |
| 75 |
zl3073x_dpll_is_input_pin(struct zl3073x_dpll_pin *pin) |
| 76 |
{ |
| 77 |
return pin->dir == DPLL_PIN_DIRECTION_INPUT; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static bool |
| 87 |
zl3073x_dpll_is_p_pin(struct zl3073x_dpll_pin *pin) |
| 88 |
{ |
| 89 |
return zl3073x_is_p_pin(pin->id); |
| 90 |
} |
| 91 |
|
| 92 |
static int |
| 93 |
zl3073x_dpll_pin_direction_get(const struct dpll_pin *dpll_pin, void *pin_priv, |
| 94 |
const struct dpll_device *dpll, void *dpll_priv, |
| 95 |
enum dpll_pin_direction *direction, |
| 96 |
struct netlink_ext_ack *extack) |
| 97 |
{ |
| 98 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 99 |
|
| 100 |
*direction = pin->dir; |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static struct zl3073x_dpll_pin * |
| 106 |
zl3073x_dpll_pin_get_by_ref(struct zl3073x_dpll *zldpll, u8 ref_id) |
| 107 |
{ |
| 108 |
struct zl3073x_dpll_pin *pin; |
| 109 |
|
| 110 |
list_for_each_entry(pin, &zldpll->pins, list) { |
| 111 |
if (zl3073x_dpll_is_input_pin(pin) && |
| 112 |
zl3073x_input_pin_ref_get(pin->id) == ref_id) |
| 113 |
return pin; |
| 114 |
} |
| 115 |
|
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
static int |
| 120 |
zl3073x_dpll_input_pin_esync_get(const struct dpll_pin *dpll_pin, |
| 121 |
void *pin_priv, |
| 122 |
const struct dpll_device *dpll, |
| 123 |
void *dpll_priv, |
| 124 |
struct dpll_pin_esync *esync, |
| 125 |
struct netlink_ext_ack *extack) |
| 126 |
{ |
| 127 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 128 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 129 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 130 |
const struct zl3073x_ref *ref; |
| 131 |
u8 ref_id; |
| 132 |
|
| 133 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 134 |
ref = zl3073x_ref_state_get(zldev, ref_id); |
| 135 |
|
| 136 |
switch (FIELD_GET(ZL_REF_SYNC_CTRL_MODE, ref->sync_ctrl)) { |
| 137 |
case ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75: |
| 138 |
esync->freq = ref->esync_n_div == ZL_REF_ESYNC_DIV_1HZ ? 1 : 0; |
| 139 |
esync->pulse = 25; |
| 140 |
break; |
| 141 |
default: |
| 142 |
esync->freq = 0; |
| 143 |
esync->pulse = 0; |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (pin->esync_control && zl3073x_ref_freq_get(ref) > 1) { |
| 151 |
esync->range = esync_freq_ranges; |
| 152 |
esync->range_num = ARRAY_SIZE(esync_freq_ranges); |
| 153 |
} else { |
| 154 |
esync->range = NULL; |
| 155 |
esync->range_num = 0; |
| 156 |
} |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static int |
| 162 |
zl3073x_dpll_input_pin_esync_set(const struct dpll_pin *dpll_pin, |
| 163 |
void *pin_priv, |
| 164 |
const struct dpll_device *dpll, |
| 165 |
void *dpll_priv, u64 freq, |
| 166 |
struct netlink_ext_ack *extack) |
| 167 |
{ |
| 168 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 169 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 170 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 171 |
struct zl3073x_ref ref; |
| 172 |
u8 ref_id, sync_mode; |
| 173 |
|
| 174 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 175 |
ref = *zl3073x_ref_state_get(zldev, ref_id); |
| 176 |
|
| 177 |
|
| 178 |
if (!freq) |
| 179 |
sync_mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF; |
| 180 |
else |
| 181 |
sync_mode = ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75; |
| 182 |
|
| 183 |
ref.sync_ctrl &= ~ZL_REF_SYNC_CTRL_MODE; |
| 184 |
ref.sync_ctrl |= FIELD_PREP(ZL_REF_SYNC_CTRL_MODE, sync_mode); |
| 185 |
|
| 186 |
if (freq) { |
| 187 |
|
| 188 |
ref.esync_n_div = ZL_REF_ESYNC_DIV_1HZ; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
return zl3073x_ref_state_set(zldev, ref_id, &ref); |
| 193 |
} |
| 194 |
|
| 195 |
static int |
| 196 |
zl3073x_dpll_input_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv, |
| 197 |
const struct dpll_device *dpll, void *dpll_priv, |
| 198 |
s64 *ffo, struct netlink_ext_ack *extack) |
| 199 |
{ |
| 200 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 201 |
|
| 202 |
*ffo = pin->freq_offset; |
| 203 |
|
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
static int |
| 208 |
zl3073x_dpll_input_pin_frequency_get(const struct dpll_pin *dpll_pin, |
| 209 |
void *pin_priv, |
| 210 |
const struct dpll_device *dpll, |
| 211 |
void *dpll_priv, u64 *frequency, |
| 212 |
struct netlink_ext_ack *extack) |
| 213 |
{ |
| 214 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 215 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 216 |
u8 ref_id; |
| 217 |
|
| 218 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 219 |
*frequency = zl3073x_dev_ref_freq_get(zldpll->dev, ref_id); |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
static int |
| 225 |
zl3073x_dpll_input_pin_frequency_set(const struct dpll_pin *dpll_pin, |
| 226 |
void *pin_priv, |
| 227 |
const struct dpll_device *dpll, |
| 228 |
void *dpll_priv, u64 frequency, |
| 229 |
struct netlink_ext_ack *extack) |
| 230 |
{ |
| 231 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 232 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 233 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 234 |
struct zl3073x_ref ref; |
| 235 |
u8 ref_id; |
| 236 |
|
| 237 |
|
| 238 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 239 |
ref = *zl3073x_ref_state_get(zldev, ref_id); |
| 240 |
|
| 241 |
|
| 242 |
zl3073x_ref_freq_set(&ref, frequency); |
| 243 |
|
| 244 |
|
| 245 |
return zl3073x_ref_state_set(zldev, ref_id, &ref); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static int |
| 259 |
zl3073x_dpll_selected_ref_get(struct zl3073x_dpll *zldpll, u8 *ref) |
| 260 |
{ |
| 261 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 262 |
u8 state, value; |
| 263 |
int rc; |
| 264 |
|
| 265 |
switch (zldpll->refsel_mode) { |
| 266 |
case ZL_DPLL_MODE_REFSEL_MODE_AUTO: |
| 267 |
|
| 268 |
rc = zl3073x_read_u8(zldev, |
| 269 |
ZL_REG_DPLL_REFSEL_STATUS(zldpll->id), |
| 270 |
&value); |
| 271 |
if (rc) |
| 272 |
return rc; |
| 273 |
|
| 274 |
|
| 275 |
state = FIELD_GET(ZL_DPLL_REFSEL_STATUS_STATE, value); |
| 276 |
|
| 277 |
|
| 278 |
if (state == ZL_DPLL_REFSEL_STATUS_STATE_LOCK) |
| 279 |
*ref = FIELD_GET(ZL_DPLL_REFSEL_STATUS_REFSEL, value); |
| 280 |
else |
| 281 |
*ref = ZL3073X_DPLL_REF_NONE; |
| 282 |
break; |
| 283 |
case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: |
| 284 |
|
| 285 |
*ref = zldpll->forced_ref; |
| 286 |
break; |
| 287 |
default: |
| 288 |
|
| 289 |
*ref = ZL3073X_DPLL_REF_NONE; |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
static int |
| 307 |
zl3073x_dpll_selected_ref_set(struct zl3073x_dpll *zldpll, u8 ref) |
| 308 |
{ |
| 309 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 310 |
u8 mode, mode_refsel; |
| 311 |
int rc; |
| 312 |
|
| 313 |
mode = zldpll->refsel_mode; |
| 314 |
|
| 315 |
switch (mode) { |
| 316 |
case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: |
| 317 |
|
| 318 |
if (ref == ZL3073X_DPLL_REF_NONE) { |
| 319 |
switch (zldpll->lock_status) { |
| 320 |
case DPLL_LOCK_STATUS_LOCKED_HO_ACQ: |
| 321 |
case DPLL_LOCK_STATUS_HOLDOVER: |
| 322 |
|
| 323 |
mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER; |
| 324 |
break; |
| 325 |
default: |
| 326 |
|
| 327 |
mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN; |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
ref = zldpll->forced_ref; |
| 332 |
} else if (ref == zldpll->forced_ref) { |
| 333 |
|
| 334 |
return 0; |
| 335 |
} |
| 336 |
break; |
| 337 |
case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: |
| 338 |
case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: |
| 339 |
|
| 340 |
if (ref == ZL3073X_DPLL_REF_NONE) |
| 341 |
|
| 342 |
return 0; |
| 343 |
|
| 344 |
|
| 345 |
mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK; |
| 346 |
break; |
| 347 |
default: |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
return -EOPNOTSUPP; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
mode_refsel = FIELD_PREP(ZL_DPLL_MODE_REFSEL_MODE, mode) | |
| 356 |
FIELD_PREP(ZL_DPLL_MODE_REFSEL_REF, ref); |
| 357 |
|
| 358 |
|
| 359 |
rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MODE_REFSEL(zldpll->id), |
| 360 |
mode_refsel); |
| 361 |
if (rc) |
| 362 |
return rc; |
| 363 |
|
| 364 |
|
| 365 |
zldpll->refsel_mode = mode; |
| 366 |
zldpll->forced_ref = ref; |
| 367 |
|
| 368 |
return rc; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static int |
| 382 |
zl3073x_dpll_connected_ref_get(struct zl3073x_dpll *zldpll, u8 *ref) |
| 383 |
{ |
| 384 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 385 |
int rc; |
| 386 |
|
| 387 |
|
| 388 |
rc = zl3073x_dpll_selected_ref_get(zldpll, ref); |
| 389 |
if (rc) |
| 390 |
return rc; |
| 391 |
|
| 392 |
|
| 393 |
if (ZL3073X_DPLL_REF_IS_VALID(*ref) && |
| 394 |
!zl3073x_dev_ref_is_status_ok(zldev, *ref)) |
| 395 |
*ref = ZL3073X_DPLL_REF_NONE; |
| 396 |
|
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
static int |
| 401 |
zl3073x_dpll_input_pin_phase_offset_get(const struct dpll_pin *dpll_pin, |
| 402 |
void *pin_priv, |
| 403 |
const struct dpll_device *dpll, |
| 404 |
void *dpll_priv, s64 *phase_offset, |
| 405 |
struct netlink_ext_ack *extack) |
| 406 |
{ |
| 407 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 408 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 409 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 410 |
const struct zl3073x_ref *ref; |
| 411 |
u8 conn_id, ref_id; |
| 412 |
s64 ref_phase; |
| 413 |
int rc; |
| 414 |
|
| 415 |
|
| 416 |
rc = zl3073x_dpll_connected_ref_get(zldpll, &conn_id); |
| 417 |
if (rc) |
| 418 |
return rc; |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 425 |
ref = zl3073x_ref_state_get(zldev, ref_id); |
| 426 |
if ((!zldpll->phase_monitor && ref_id != conn_id) || |
| 427 |
!zl3073x_ref_is_status_ok(ref)) { |
| 428 |
*phase_offset = 0; |
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
ref_phase = pin->phase_offset; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
if (ZL3073X_DPLL_REF_IS_VALID(conn_id) && conn_id != ref_id) { |
| 439 |
u32 conn_freq, ref_freq; |
| 440 |
|
| 441 |
|
| 442 |
conn_freq = zl3073x_dev_ref_freq_get(zldev, conn_id); |
| 443 |
ref_freq = zl3073x_ref_freq_get(ref); |
| 444 |
|
| 445 |
if (conn_freq > ref_freq) { |
| 446 |
s64 conn_period, div_factor; |
| 447 |
|
| 448 |
conn_period = div_s64(PSEC_PER_SEC, conn_freq); |
| 449 |
div_factor = div64_s64(ref_phase, conn_period); |
| 450 |
ref_phase -= conn_period * div_factor; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
*phase_offset = ref_phase * DPLL_PHASE_OFFSET_DIVIDER; |
| 455 |
|
| 456 |
return rc; |
| 457 |
} |
| 458 |
|
| 459 |
static int |
| 460 |
zl3073x_dpll_input_pin_phase_adjust_get(const struct dpll_pin *dpll_pin, |
| 461 |
void *pin_priv, |
| 462 |
const struct dpll_device *dpll, |
| 463 |
void *dpll_priv, |
| 464 |
s32 *phase_adjust, |
| 465 |
struct netlink_ext_ack *extack) |
| 466 |
{ |
| 467 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 468 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 469 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 470 |
const struct zl3073x_ref *ref; |
| 471 |
s64 phase_comp; |
| 472 |
u8 ref_id; |
| 473 |
|
| 474 |
|
| 475 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 476 |
ref = zl3073x_ref_state_get(zldev, ref_id); |
| 477 |
|
| 478 |
|
| 479 |
if (zl3073x_dev_is_ref_phase_comp_32bit(zldev)) |
| 480 |
phase_comp = sign_extend64(ref->phase_comp, 31); |
| 481 |
else |
| 482 |
phase_comp = sign_extend64(ref->phase_comp, 47); |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
*phase_adjust = (s32)-phase_comp; |
| 488 |
|
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
static int |
| 493 |
zl3073x_dpll_input_pin_phase_adjust_set(const struct dpll_pin *dpll_pin, |
| 494 |
void *pin_priv, |
| 495 |
const struct dpll_device *dpll, |
| 496 |
void *dpll_priv, |
| 497 |
s32 phase_adjust, |
| 498 |
struct netlink_ext_ack *extack) |
| 499 |
{ |
| 500 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 501 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 502 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 503 |
struct zl3073x_ref ref; |
| 504 |
u8 ref_id; |
| 505 |
|
| 506 |
|
| 507 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 508 |
ref = *zl3073x_ref_state_get(zldev, ref_id); |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
ref.phase_comp = -phase_adjust; |
| 514 |
|
| 515 |
|
| 516 |
return zl3073x_ref_state_set(zldev, ref_id, &ref); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
static int |
| 530 |
zl3073x_dpll_ref_prio_get(struct zl3073x_dpll_pin *pin, u8 *prio) |
| 531 |
{ |
| 532 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 533 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 534 |
u8 ref, ref_prio; |
| 535 |
int rc; |
| 536 |
|
| 537 |
guard(mutex)(&zldev->multiop_lock); |
| 538 |
|
| 539 |
|
| 540 |
rc = zl3073x_mb_op(zldev, ZL_REG_DPLL_MB_SEM, ZL_DPLL_MB_SEM_RD, |
| 541 |
ZL_REG_DPLL_MB_MASK, BIT(zldpll->id)); |
| 542 |
if (rc) |
| 543 |
return rc; |
| 544 |
|
| 545 |
|
| 546 |
ref = zl3073x_input_pin_ref_get(pin->id); |
| 547 |
rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_REF_PRIO(ref / 2), |
| 548 |
&ref_prio); |
| 549 |
if (rc) |
| 550 |
return rc; |
| 551 |
|
| 552 |
|
| 553 |
if (zl3073x_dpll_is_p_pin(pin)) |
| 554 |
*prio = FIELD_GET(ZL_DPLL_REF_PRIO_REF_P, ref_prio); |
| 555 |
else |
| 556 |
*prio = FIELD_GET(ZL_DPLL_REF_PRIO_REF_N, ref_prio); |
| 557 |
|
| 558 |
return rc; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
static int |
| 571 |
zl3073x_dpll_ref_prio_set(struct zl3073x_dpll_pin *pin, u8 prio) |
| 572 |
{ |
| 573 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 574 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 575 |
u8 ref, ref_prio; |
| 576 |
int rc; |
| 577 |
|
| 578 |
guard(mutex)(&zldev->multiop_lock); |
| 579 |
|
| 580 |
|
| 581 |
rc = zl3073x_mb_op(zldev, ZL_REG_DPLL_MB_SEM, ZL_DPLL_MB_SEM_RD, |
| 582 |
ZL_REG_DPLL_MB_MASK, BIT(zldpll->id)); |
| 583 |
if (rc) |
| 584 |
return rc; |
| 585 |
|
| 586 |
|
| 587 |
ref = zl3073x_input_pin_ref_get(pin->id); |
| 588 |
rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_REF_PRIO(ref / 2), &ref_prio); |
| 589 |
if (rc) |
| 590 |
return rc; |
| 591 |
|
| 592 |
|
| 593 |
if (zl3073x_dpll_is_p_pin(pin)) { |
| 594 |
ref_prio &= ~ZL_DPLL_REF_PRIO_REF_P; |
| 595 |
ref_prio |= FIELD_PREP(ZL_DPLL_REF_PRIO_REF_P, prio); |
| 596 |
} else { |
| 597 |
ref_prio &= ~ZL_DPLL_REF_PRIO_REF_N; |
| 598 |
ref_prio |= FIELD_PREP(ZL_DPLL_REF_PRIO_REF_N, prio); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_REF_PRIO(ref / 2), ref_prio); |
| 603 |
if (rc) |
| 604 |
return rc; |
| 605 |
|
| 606 |
|
| 607 |
return zl3073x_mb_op(zldev, ZL_REG_DPLL_MB_SEM, ZL_DPLL_MB_SEM_WR, |
| 608 |
ZL_REG_DPLL_MB_MASK, BIT(zldpll->id)); |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
static int |
| 622 |
zl3073x_dpll_ref_state_get(struct zl3073x_dpll_pin *pin, |
| 623 |
enum dpll_pin_state *state) |
| 624 |
{ |
| 625 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 626 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 627 |
u8 ref, ref_conn; |
| 628 |
int rc; |
| 629 |
|
| 630 |
ref = zl3073x_input_pin_ref_get(pin->id); |
| 631 |
|
| 632 |
|
| 633 |
rc = zl3073x_dpll_connected_ref_get(zldpll, &ref_conn); |
| 634 |
if (rc) |
| 635 |
return rc; |
| 636 |
|
| 637 |
if (ref == ref_conn) { |
| 638 |
*state = DPLL_PIN_STATE_CONNECTED; |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if (zldpll->refsel_mode == ZL_DPLL_MODE_REFSEL_MODE_AUTO && |
| 647 |
zl3073x_dev_ref_is_status_ok(zldev, ref) && pin->selectable) { |
| 648 |
*state = DPLL_PIN_STATE_SELECTABLE; |
| 649 |
return 0; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
*state = DPLL_PIN_STATE_DISCONNECTED; |
| 654 |
|
| 655 |
return 0; |
| 656 |
} |
| 657 |
|
| 658 |
static int |
| 659 |
zl3073x_dpll_input_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, |
| 660 |
void *pin_priv, |
| 661 |
const struct dpll_device *dpll, |
| 662 |
void *dpll_priv, |
| 663 |
enum dpll_pin_state *state, |
| 664 |
struct netlink_ext_ack *extack) |
| 665 |
{ |
| 666 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 667 |
|
| 668 |
return zl3073x_dpll_ref_state_get(pin, state); |
| 669 |
} |
| 670 |
|
| 671 |
static int |
| 672 |
zl3073x_dpll_input_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin, |
| 673 |
void *pin_priv, |
| 674 |
const struct dpll_device *dpll, |
| 675 |
void *dpll_priv, |
| 676 |
enum dpll_pin_state state, |
| 677 |
struct netlink_ext_ack *extack) |
| 678 |
{ |
| 679 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 680 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 681 |
u8 new_ref; |
| 682 |
int rc; |
| 683 |
|
| 684 |
switch (zldpll->refsel_mode) { |
| 685 |
case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: |
| 686 |
case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: |
| 687 |
case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: |
| 688 |
if (state == DPLL_PIN_STATE_CONNECTED) { |
| 689 |
|
| 690 |
new_ref = zl3073x_input_pin_ref_get(pin->id); |
| 691 |
} else if (state == DPLL_PIN_STATE_DISCONNECTED) { |
| 692 |
|
| 693 |
new_ref = ZL3073X_DPLL_REF_NONE; |
| 694 |
} else { |
| 695 |
NL_SET_ERR_MSG_MOD(extack, |
| 696 |
"Invalid pin state for manual mode"); |
| 697 |
return -EINVAL; |
| 698 |
} |
| 699 |
|
| 700 |
rc = zl3073x_dpll_selected_ref_set(zldpll, new_ref); |
| 701 |
break; |
| 702 |
|
| 703 |
case ZL_DPLL_MODE_REFSEL_MODE_AUTO: |
| 704 |
if (state == DPLL_PIN_STATE_SELECTABLE) { |
| 705 |
if (pin->selectable) |
| 706 |
return 0; |
| 707 |
|
| 708 |
|
| 709 |
rc = zl3073x_dpll_ref_prio_set(pin, pin->prio); |
| 710 |
if (rc) |
| 711 |
return rc; |
| 712 |
|
| 713 |
|
| 714 |
pin->selectable = true; |
| 715 |
} else if (state == DPLL_PIN_STATE_DISCONNECTED) { |
| 716 |
if (!pin->selectable) |
| 717 |
return 0; |
| 718 |
|
| 719 |
|
| 720 |
rc = zl3073x_dpll_ref_prio_set(pin, |
| 721 |
ZL_DPLL_REF_PRIO_NONE); |
| 722 |
if (rc) |
| 723 |
return rc; |
| 724 |
|
| 725 |
|
| 726 |
pin->selectable = false; |
| 727 |
} else { |
| 728 |
NL_SET_ERR_MSG(extack, |
| 729 |
"Invalid pin state for automatic mode"); |
| 730 |
return -EINVAL; |
| 731 |
} |
| 732 |
break; |
| 733 |
|
| 734 |
default: |
| 735 |
|
| 736 |
NL_SET_ERR_MSG(extack, |
| 737 |
"Pin state cannot be changed in current mode"); |
| 738 |
rc = -EOPNOTSUPP; |
| 739 |
break; |
| 740 |
} |
| 741 |
|
| 742 |
return rc; |
| 743 |
} |
| 744 |
|
| 745 |
static int |
| 746 |
zl3073x_dpll_input_pin_prio_get(const struct dpll_pin *dpll_pin, void *pin_priv, |
| 747 |
const struct dpll_device *dpll, void *dpll_priv, |
| 748 |
u32 *prio, struct netlink_ext_ack *extack) |
| 749 |
{ |
| 750 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 751 |
|
| 752 |
*prio = pin->prio; |
| 753 |
|
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
static int |
| 758 |
zl3073x_dpll_input_pin_prio_set(const struct dpll_pin *dpll_pin, void *pin_priv, |
| 759 |
const struct dpll_device *dpll, void *dpll_priv, |
| 760 |
u32 prio, struct netlink_ext_ack *extack) |
| 761 |
{ |
| 762 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 763 |
int rc; |
| 764 |
|
| 765 |
if (prio > ZL_DPLL_REF_PRIO_MAX) |
| 766 |
return -EINVAL; |
| 767 |
|
| 768 |
|
| 769 |
if (pin->selectable) { |
| 770 |
rc = zl3073x_dpll_ref_prio_set(pin, prio); |
| 771 |
if (rc) |
| 772 |
return rc; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
pin->prio = prio; |
| 777 |
|
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
static int |
| 782 |
zl3073x_dpll_output_pin_esync_get(const struct dpll_pin *dpll_pin, |
| 783 |
void *pin_priv, |
| 784 |
const struct dpll_device *dpll, |
| 785 |
void *dpll_priv, |
| 786 |
struct dpll_pin_esync *esync, |
| 787 |
struct netlink_ext_ack *extack) |
| 788 |
{ |
| 789 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 790 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 791 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 792 |
const struct zl3073x_synth *synth; |
| 793 |
const struct zl3073x_out *out; |
| 794 |
u8 clock_type, out_id; |
| 795 |
u32 synth_freq; |
| 796 |
|
| 797 |
out_id = zl3073x_output_pin_out_get(pin->id); |
| 798 |
out = zl3073x_out_state_get(zldev, out_id); |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
switch (zl3073x_out_signal_format_get(out)) { |
| 805 |
case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV: |
| 806 |
case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV_INV: |
| 807 |
return -EOPNOTSUPP; |
| 808 |
default: |
| 809 |
break; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(out)); |
| 814 |
synth_freq = zl3073x_synth_freq_get(synth); |
| 815 |
|
| 816 |
clock_type = FIELD_GET(ZL_OUTPUT_MODE_CLOCK_TYPE, out->mode); |
| 817 |
if (clock_type != ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC) { |
| 818 |
|
| 819 |
esync->freq = 0; |
| 820 |
esync->pulse = 0; |
| 821 |
|
| 822 |
goto finish; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
esync->freq = synth_freq / out->div / out->esync_n_period; |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
esync->pulse = (50 * out->esync_n_width) / out->div; |
| 834 |
|
| 835 |
finish: |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
if (pin->esync_control && (synth_freq / out->div) > 1) { |
| 840 |
esync->range = esync_freq_ranges; |
| 841 |
esync->range_num = ARRAY_SIZE(esync_freq_ranges); |
| 842 |
} else { |
| 843 |
esync->range = NULL; |
| 844 |
esync->range_num = 0; |
| 845 |
} |
| 846 |
|
| 847 |
return 0; |
| 848 |
} |
| 849 |
|
| 850 |
static int |
| 851 |
zl3073x_dpll_output_pin_esync_set(const struct dpll_pin *dpll_pin, |
| 852 |
void *pin_priv, |
| 853 |
const struct dpll_device *dpll, |
| 854 |
void *dpll_priv, u64 freq, |
| 855 |
struct netlink_ext_ack *extack) |
| 856 |
{ |
| 857 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 858 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 859 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 860 |
const struct zl3073x_synth *synth; |
| 861 |
struct zl3073x_out out; |
| 862 |
u8 clock_type, out_id; |
| 863 |
u32 synth_freq; |
| 864 |
|
| 865 |
out_id = zl3073x_output_pin_out_get(pin->id); |
| 866 |
out = *zl3073x_out_state_get(zldev, out_id); |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
switch (zl3073x_out_signal_format_get(&out)) { |
| 873 |
case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV: |
| 874 |
case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV_INV: |
| 875 |
return -EOPNOTSUPP; |
| 876 |
default: |
| 877 |
break; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
if (freq) |
| 882 |
clock_type = ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC; |
| 883 |
else |
| 884 |
clock_type = ZL_OUTPUT_MODE_CLOCK_TYPE_NORMAL; |
| 885 |
|
| 886 |
|
| 887 |
out.mode &= ~ZL_OUTPUT_MODE_CLOCK_TYPE; |
| 888 |
out.mode |= FIELD_PREP(ZL_OUTPUT_MODE_CLOCK_TYPE, clock_type); |
| 889 |
|
| 890 |
|
| 891 |
if (!freq) |
| 892 |
goto write_mailbox; |
| 893 |
|
| 894 |
|
| 895 |
synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out)); |
| 896 |
synth_freq = zl3073x_synth_freq_get(synth); |
| 897 |
|
| 898 |
|
| 899 |
out.esync_n_period = synth_freq / (u32)freq / out.div; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
out.esync_n_width = out.div / 2; |
| 908 |
|
| 909 |
write_mailbox: |
| 910 |
|
| 911 |
return zl3073x_out_state_set(zldev, out_id, &out); |
| 912 |
} |
| 913 |
|
| 914 |
static int |
| 915 |
zl3073x_dpll_output_pin_frequency_get(const struct dpll_pin *dpll_pin, |
| 916 |
void *pin_priv, |
| 917 |
const struct dpll_device *dpll, |
| 918 |
void *dpll_priv, u64 *frequency, |
| 919 |
struct netlink_ext_ack *extack) |
| 920 |
{ |
| 921 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 922 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 923 |
|
| 924 |
*frequency = zl3073x_dev_output_pin_freq_get(zldpll->dev, pin->id); |
| 925 |
|
| 926 |
return 0; |
| 927 |
} |
| 928 |
|
| 929 |
static int |
| 930 |
zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin, |
| 931 |
void *pin_priv, |
| 932 |
const struct dpll_device *dpll, |
| 933 |
void *dpll_priv, u64 frequency, |
| 934 |
struct netlink_ext_ack *extack) |
| 935 |
{ |
| 936 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 937 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 938 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 939 |
const struct zl3073x_synth *synth; |
| 940 |
u8 out_id, signal_format; |
| 941 |
u32 new_div, synth_freq; |
| 942 |
struct zl3073x_out out; |
| 943 |
|
| 944 |
out_id = zl3073x_output_pin_out_get(pin->id); |
| 945 |
out = *zl3073x_out_state_get(zldev, out_id); |
| 946 |
|
| 947 |
|
| 948 |
synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out)); |
| 949 |
synth_freq = zl3073x_synth_freq_get(synth); |
| 950 |
new_div = synth_freq / (u32)frequency; |
| 951 |
|
| 952 |
|
| 953 |
signal_format = zl3073x_out_signal_format_get(&out); |
| 954 |
|
| 955 |
|
| 956 |
if (signal_format != ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV && |
| 957 |
signal_format != ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV_INV) { |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
out.div = new_div; |
| 962 |
|
| 963 |
|
| 964 |
out.width = new_div; |
| 965 |
|
| 966 |
|
| 967 |
return zl3073x_out_state_set(zldev, out_id, &out); |
| 968 |
} |
| 969 |
|
| 970 |
if (zl3073x_dpll_is_p_pin(pin)) { |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
out.esync_n_period = (out.esync_n_period * out.div) / new_div; |
| 979 |
if (!out.esync_n_period) |
| 980 |
return -EINVAL; |
| 981 |
|
| 982 |
|
| 983 |
out.div = new_div; |
| 984 |
|
| 985 |
|
| 986 |
out.width = out.div; |
| 987 |
} else { |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
out.esync_n_period = div64_u64(synth_freq, frequency * out.div); |
| 996 |
if (!out.esync_n_period) |
| 997 |
return -EINVAL; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
out.esync_n_width = out.esync_n_period; |
| 1002 |
|
| 1003 |
|
| 1004 |
return zl3073x_out_state_set(zldev, out_id, &out); |
| 1005 |
} |
| 1006 |
|
| 1007 |
static int |
| 1008 |
zl3073x_dpll_output_pin_phase_adjust_get(const struct dpll_pin *dpll_pin, |
| 1009 |
void *pin_priv, |
| 1010 |
const struct dpll_device *dpll, |
| 1011 |
void *dpll_priv, |
| 1012 |
s32 *phase_adjust, |
| 1013 |
struct netlink_ext_ack *extack) |
| 1014 |
{ |
| 1015 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1016 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1017 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 1018 |
const struct zl3073x_out *out; |
| 1019 |
u8 out_id; |
| 1020 |
|
| 1021 |
out_id = zl3073x_output_pin_out_get(pin->id); |
| 1022 |
out = zl3073x_out_state_get(zldev, out_id); |
| 1023 |
|
| 1024 |
|
| 1025 |
*phase_adjust = out->phase_comp * pin->phase_gran; |
| 1026 |
|
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
|
| 1030 |
static int |
| 1031 |
zl3073x_dpll_output_pin_phase_adjust_set(const struct dpll_pin *dpll_pin, |
| 1032 |
void *pin_priv, |
| 1033 |
const struct dpll_device *dpll, |
| 1034 |
void *dpll_priv, |
| 1035 |
s32 phase_adjust, |
| 1036 |
struct netlink_ext_ack *extack) |
| 1037 |
{ |
| 1038 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1039 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1040 |
struct zl3073x_dpll_pin *pin = pin_priv; |
| 1041 |
struct zl3073x_out out; |
| 1042 |
u8 out_id; |
| 1043 |
|
| 1044 |
out_id = zl3073x_output_pin_out_get(pin->id); |
| 1045 |
out = *zl3073x_out_state_get(zldev, out_id); |
| 1046 |
|
| 1047 |
|
| 1048 |
out.phase_comp = phase_adjust / pin->phase_gran; |
| 1049 |
|
| 1050 |
|
| 1051 |
return zl3073x_out_state_set(zldev, out_id, &out); |
| 1052 |
} |
| 1053 |
|
| 1054 |
static int |
| 1055 |
zl3073x_dpll_output_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, |
| 1056 |
void *pin_priv, |
| 1057 |
const struct dpll_device *dpll, |
| 1058 |
void *dpll_priv, |
| 1059 |
enum dpll_pin_state *state, |
| 1060 |
struct netlink_ext_ack *extack) |
| 1061 |
{ |
| 1062 |
|
| 1063 |
*state = DPLL_PIN_STATE_CONNECTED; |
| 1064 |
|
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static int |
| 1069 |
zl3073x_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv, |
| 1070 |
enum dpll_lock_status *status, |
| 1071 |
enum dpll_lock_status_error *status_error, |
| 1072 |
struct netlink_ext_ack *extack) |
| 1073 |
{ |
| 1074 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1075 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1076 |
u8 mon_status, state; |
| 1077 |
int rc; |
| 1078 |
|
| 1079 |
switch (zldpll->refsel_mode) { |
| 1080 |
case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: |
| 1081 |
case ZL_DPLL_MODE_REFSEL_MODE_NCO: |
| 1082 |
|
| 1083 |
*status = DPLL_LOCK_STATUS_UNLOCKED; |
| 1084 |
|
| 1085 |
return 0; |
| 1086 |
default: |
| 1087 |
break; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MON_STATUS(zldpll->id), |
| 1092 |
&mon_status); |
| 1093 |
if (rc) |
| 1094 |
return rc; |
| 1095 |
state = FIELD_GET(ZL_DPLL_MON_STATUS_STATE, mon_status); |
| 1096 |
|
| 1097 |
switch (state) { |
| 1098 |
case ZL_DPLL_MON_STATUS_STATE_LOCK: |
| 1099 |
if (FIELD_GET(ZL_DPLL_MON_STATUS_HO_READY, mon_status)) |
| 1100 |
*status = DPLL_LOCK_STATUS_LOCKED_HO_ACQ; |
| 1101 |
else |
| 1102 |
*status = DPLL_LOCK_STATUS_LOCKED; |
| 1103 |
break; |
| 1104 |
case ZL_DPLL_MON_STATUS_STATE_HOLDOVER: |
| 1105 |
case ZL_DPLL_MON_STATUS_STATE_ACQUIRING: |
| 1106 |
*status = DPLL_LOCK_STATUS_HOLDOVER; |
| 1107 |
break; |
| 1108 |
default: |
| 1109 |
dev_warn(zldev->dev, "Unknown DPLL monitor status: 0x%02x\n", |
| 1110 |
mon_status); |
| 1111 |
*status = DPLL_LOCK_STATUS_UNLOCKED; |
| 1112 |
break; |
| 1113 |
} |
| 1114 |
|
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static int |
| 1119 |
zl3073x_dpll_supported_modes_get(const struct dpll_device *dpll, |
| 1120 |
void *dpll_priv, unsigned long *modes, |
| 1121 |
struct netlink_ext_ack *extack) |
| 1122 |
{ |
| 1123 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
if (zldpll->refsel_mode != ZL_DPLL_MODE_REFSEL_MODE_NCO) |
| 1131 |
__set_bit(DPLL_MODE_AUTOMATIC, modes); |
| 1132 |
|
| 1133 |
__set_bit(DPLL_MODE_MANUAL, modes); |
| 1134 |
|
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
static int |
| 1139 |
zl3073x_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv, |
| 1140 |
enum dpll_mode *mode, struct netlink_ext_ack *extack) |
| 1141 |
{ |
| 1142 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1143 |
|
| 1144 |
switch (zldpll->refsel_mode) { |
| 1145 |
case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: |
| 1146 |
case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: |
| 1147 |
case ZL_DPLL_MODE_REFSEL_MODE_NCO: |
| 1148 |
case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
*mode = DPLL_MODE_MANUAL; |
| 1153 |
break; |
| 1154 |
case ZL_DPLL_MODE_REFSEL_MODE_AUTO: |
| 1155 |
|
| 1156 |
*mode = DPLL_MODE_AUTOMATIC; |
| 1157 |
break; |
| 1158 |
default: |
| 1159 |
return -EINVAL; |
| 1160 |
} |
| 1161 |
|
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static int |
| 1166 |
zl3073x_dpll_phase_offset_avg_factor_get(const struct dpll_device *dpll, |
| 1167 |
void *dpll_priv, u32 *factor, |
| 1168 |
struct netlink_ext_ack *extack) |
| 1169 |
{ |
| 1170 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1171 |
|
| 1172 |
*factor = zl3073x_dev_phase_avg_factor_get(zldpll->dev); |
| 1173 |
|
| 1174 |
return 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static void |
| 1178 |
zl3073x_dpll_change_work(struct work_struct *work) |
| 1179 |
{ |
| 1180 |
struct zl3073x_dpll *zldpll; |
| 1181 |
|
| 1182 |
zldpll = container_of(work, struct zl3073x_dpll, change_work); |
| 1183 |
dpll_device_change_ntf(zldpll->dpll_dev); |
| 1184 |
} |
| 1185 |
|
| 1186 |
static int |
| 1187 |
zl3073x_dpll_phase_offset_avg_factor_set(const struct dpll_device *dpll, |
| 1188 |
void *dpll_priv, u32 factor, |
| 1189 |
struct netlink_ext_ack *extack) |
| 1190 |
{ |
| 1191 |
struct zl3073x_dpll *item, *zldpll = dpll_priv; |
| 1192 |
int rc; |
| 1193 |
|
| 1194 |
if (factor > 15) { |
| 1195 |
NL_SET_ERR_MSG_FMT(extack, |
| 1196 |
"Phase offset average factor has to be from range <0,15>"); |
| 1197 |
return -EINVAL; |
| 1198 |
} |
| 1199 |
|
| 1200 |
rc = zl3073x_dev_phase_avg_factor_set(zldpll->dev, factor); |
| 1201 |
if (rc) { |
| 1202 |
NL_SET_ERR_MSG_FMT(extack, |
| 1203 |
"Failed to set phase offset averaging factor"); |
| 1204 |
return rc; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
list_for_each_entry(item, &zldpll->dev->dplls, list) { |
| 1211 |
if (item != zldpll) |
| 1212 |
schedule_work(&item->change_work); |
| 1213 |
} |
| 1214 |
|
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static int |
| 1219 |
zl3073x_dpll_mode_set(const struct dpll_device *dpll, void *dpll_priv, |
| 1220 |
enum dpll_mode mode, struct netlink_ext_ack *extack) |
| 1221 |
{ |
| 1222 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1223 |
u8 hw_mode, mode_refsel, ref; |
| 1224 |
int rc; |
| 1225 |
|
| 1226 |
rc = zl3073x_dpll_selected_ref_get(zldpll, &ref); |
| 1227 |
if (rc) { |
| 1228 |
NL_SET_ERR_MSG_MOD(extack, "failed to get selected reference"); |
| 1229 |
return rc; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (mode == DPLL_MODE_MANUAL) { |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
if (ZL3073X_DPLL_REF_IS_VALID(ref)) |
| 1242 |
hw_mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK; |
| 1243 |
else if (zldpll->lock_status == DPLL_LOCK_STATUS_UNLOCKED) |
| 1244 |
hw_mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN; |
| 1245 |
else |
| 1246 |
hw_mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER; |
| 1247 |
} else { |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
struct zl3073x_dpll_pin *pin; |
| 1254 |
|
| 1255 |
pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref); |
| 1256 |
if (pin && !pin->selectable) { |
| 1257 |
|
| 1258 |
rc = zl3073x_dpll_ref_prio_set(pin, pin->prio); |
| 1259 |
if (rc) { |
| 1260 |
NL_SET_ERR_MSG_MOD(extack, |
| 1261 |
"failed to restore pin priority"); |
| 1262 |
return rc; |
| 1263 |
} |
| 1264 |
|
| 1265 |
pin->selectable = true; |
| 1266 |
} |
| 1267 |
|
| 1268 |
hw_mode = ZL_DPLL_MODE_REFSEL_MODE_AUTO; |
| 1269 |
} |
| 1270 |
|
| 1271 |
|
| 1272 |
mode_refsel = FIELD_PREP(ZL_DPLL_MODE_REFSEL_MODE, hw_mode); |
| 1273 |
|
| 1274 |
if (ZL3073X_DPLL_REF_IS_VALID(ref)) |
| 1275 |
mode_refsel |= FIELD_PREP(ZL_DPLL_MODE_REFSEL_REF, ref); |
| 1276 |
|
| 1277 |
|
| 1278 |
rc = zl3073x_write_u8(zldpll->dev, ZL_REG_DPLL_MODE_REFSEL(zldpll->id), |
| 1279 |
mode_refsel); |
| 1280 |
if (rc) { |
| 1281 |
NL_SET_ERR_MSG_MOD(extack, |
| 1282 |
"failed to set reference selection mode"); |
| 1283 |
return rc; |
| 1284 |
} |
| 1285 |
|
| 1286 |
zldpll->refsel_mode = hw_mode; |
| 1287 |
|
| 1288 |
if (ZL3073X_DPLL_REF_IS_VALID(ref)) |
| 1289 |
zldpll->forced_ref = ref; |
| 1290 |
|
| 1291 |
return 0; |
| 1292 |
} |
| 1293 |
|
| 1294 |
static int |
| 1295 |
zl3073x_dpll_phase_offset_monitor_get(const struct dpll_device *dpll, |
| 1296 |
void *dpll_priv, |
| 1297 |
enum dpll_feature_state *state, |
| 1298 |
struct netlink_ext_ack *extack) |
| 1299 |
{ |
| 1300 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1301 |
|
| 1302 |
if (zldpll->phase_monitor) |
| 1303 |
*state = DPLL_FEATURE_STATE_ENABLE; |
| 1304 |
else |
| 1305 |
*state = DPLL_FEATURE_STATE_DISABLE; |
| 1306 |
|
| 1307 |
return 0; |
| 1308 |
} |
| 1309 |
|
| 1310 |
static int |
| 1311 |
zl3073x_dpll_phase_offset_monitor_set(const struct dpll_device *dpll, |
| 1312 |
void *dpll_priv, |
| 1313 |
enum dpll_feature_state state, |
| 1314 |
struct netlink_ext_ack *extack) |
| 1315 |
{ |
| 1316 |
struct zl3073x_dpll *zldpll = dpll_priv; |
| 1317 |
|
| 1318 |
zldpll->phase_monitor = (state == DPLL_FEATURE_STATE_ENABLE); |
| 1319 |
|
| 1320 |
return 0; |
| 1321 |
} |
| 1322 |
|
| 1323 |
static const struct dpll_pin_ops zl3073x_dpll_input_pin_ops = { |
| 1324 |
.direction_get = zl3073x_dpll_pin_direction_get, |
| 1325 |
.esync_get = zl3073x_dpll_input_pin_esync_get, |
| 1326 |
.esync_set = zl3073x_dpll_input_pin_esync_set, |
| 1327 |
.ffo_get = zl3073x_dpll_input_pin_ffo_get, |
| 1328 |
.frequency_get = zl3073x_dpll_input_pin_frequency_get, |
| 1329 |
.frequency_set = zl3073x_dpll_input_pin_frequency_set, |
| 1330 |
.phase_offset_get = zl3073x_dpll_input_pin_phase_offset_get, |
| 1331 |
.phase_adjust_get = zl3073x_dpll_input_pin_phase_adjust_get, |
| 1332 |
.phase_adjust_set = zl3073x_dpll_input_pin_phase_adjust_set, |
| 1333 |
.prio_get = zl3073x_dpll_input_pin_prio_get, |
| 1334 |
.prio_set = zl3073x_dpll_input_pin_prio_set, |
| 1335 |
.state_on_dpll_get = zl3073x_dpll_input_pin_state_on_dpll_get, |
| 1336 |
.state_on_dpll_set = zl3073x_dpll_input_pin_state_on_dpll_set, |
| 1337 |
}; |
| 1338 |
|
| 1339 |
static const struct dpll_pin_ops zl3073x_dpll_output_pin_ops = { |
| 1340 |
.direction_get = zl3073x_dpll_pin_direction_get, |
| 1341 |
.esync_get = zl3073x_dpll_output_pin_esync_get, |
| 1342 |
.esync_set = zl3073x_dpll_output_pin_esync_set, |
| 1343 |
.frequency_get = zl3073x_dpll_output_pin_frequency_get, |
| 1344 |
.frequency_set = zl3073x_dpll_output_pin_frequency_set, |
| 1345 |
.phase_adjust_get = zl3073x_dpll_output_pin_phase_adjust_get, |
| 1346 |
.phase_adjust_set = zl3073x_dpll_output_pin_phase_adjust_set, |
| 1347 |
.state_on_dpll_get = zl3073x_dpll_output_pin_state_on_dpll_get, |
| 1348 |
}; |
| 1349 |
|
| 1350 |
static const struct dpll_device_ops zl3073x_dpll_device_ops = { |
| 1351 |
.lock_status_get = zl3073x_dpll_lock_status_get, |
| 1352 |
.mode_get = zl3073x_dpll_mode_get, |
| 1353 |
.mode_set = zl3073x_dpll_mode_set, |
| 1354 |
.phase_offset_avg_factor_get = zl3073x_dpll_phase_offset_avg_factor_get, |
| 1355 |
.phase_offset_avg_factor_set = zl3073x_dpll_phase_offset_avg_factor_set, |
| 1356 |
.phase_offset_monitor_get = zl3073x_dpll_phase_offset_monitor_get, |
| 1357 |
.phase_offset_monitor_set = zl3073x_dpll_phase_offset_monitor_set, |
| 1358 |
.supported_modes_get = zl3073x_dpll_supported_modes_get, |
| 1359 |
}; |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
static struct zl3073x_dpll_pin * |
| 1373 |
zl3073x_dpll_pin_alloc(struct zl3073x_dpll *zldpll, enum dpll_pin_direction dir, |
| 1374 |
u8 id) |
| 1375 |
{ |
| 1376 |
struct zl3073x_dpll_pin *pin; |
| 1377 |
|
| 1378 |
pin = kzalloc_obj(*pin); |
| 1379 |
if (!pin) |
| 1380 |
return ERR_PTR(-ENOMEM); |
| 1381 |
|
| 1382 |
pin->dpll = zldpll; |
| 1383 |
pin->dir = dir; |
| 1384 |
pin->id = id; |
| 1385 |
|
| 1386 |
return pin; |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
static void |
| 1396 |
zl3073x_dpll_pin_free(struct zl3073x_dpll_pin *pin) |
| 1397 |
{ |
| 1398 |
WARN(pin->dpll_pin, "DPLL pin is still registered\n"); |
| 1399 |
|
| 1400 |
kfree(pin); |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
static int |
| 1413 |
zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index) |
| 1414 |
{ |
| 1415 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 1416 |
struct zl3073x_pin_props *props; |
| 1417 |
const struct dpll_pin_ops *ops; |
| 1418 |
int rc; |
| 1419 |
|
| 1420 |
|
| 1421 |
props = zl3073x_pin_props_get(zldpll->dev, pin->dir, pin->id); |
| 1422 |
if (IS_ERR(props)) |
| 1423 |
return PTR_ERR(props); |
| 1424 |
|
| 1425 |
|
| 1426 |
strscpy(pin->label, props->package_label); |
| 1427 |
pin->esync_control = props->esync_control; |
| 1428 |
pin->phase_gran = props->dpll_props.phase_gran; |
| 1429 |
|
| 1430 |
if (zl3073x_dpll_is_input_pin(pin)) { |
| 1431 |
rc = zl3073x_dpll_ref_prio_get(pin, &pin->prio); |
| 1432 |
if (rc) |
| 1433 |
goto err_prio_get; |
| 1434 |
|
| 1435 |
if (pin->prio == ZL_DPLL_REF_PRIO_NONE) { |
| 1436 |
|
| 1437 |
pin->prio = ZL_DPLL_REF_PRIO_MAX; |
| 1438 |
pin->selectable = false; |
| 1439 |
} else { |
| 1440 |
|
| 1441 |
pin->selectable = true; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, index, THIS_MODULE, |
| 1447 |
&props->dpll_props, &pin->tracker); |
| 1448 |
if (IS_ERR(pin->dpll_pin)) { |
| 1449 |
rc = PTR_ERR(pin->dpll_pin); |
| 1450 |
goto err_pin_get; |
| 1451 |
} |
| 1452 |
dpll_pin_fwnode_set(pin->dpll_pin, props->fwnode); |
| 1453 |
|
| 1454 |
if (zl3073x_dpll_is_input_pin(pin)) |
| 1455 |
ops = &zl3073x_dpll_input_pin_ops; |
| 1456 |
else |
| 1457 |
ops = &zl3073x_dpll_output_pin_ops; |
| 1458 |
|
| 1459 |
|
| 1460 |
rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin, ops, pin); |
| 1461 |
if (rc) |
| 1462 |
goto err_register; |
| 1463 |
|
| 1464 |
|
| 1465 |
zl3073x_pin_props_put(props); |
| 1466 |
|
| 1467 |
return 0; |
| 1468 |
|
| 1469 |
err_register: |
| 1470 |
dpll_pin_put(pin->dpll_pin, &pin->tracker); |
| 1471 |
err_prio_get: |
| 1472 |
pin->dpll_pin = NULL; |
| 1473 |
err_pin_get: |
| 1474 |
zl3073x_pin_props_put(props); |
| 1475 |
|
| 1476 |
return rc; |
| 1477 |
} |
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
static void |
| 1486 |
zl3073x_dpll_pin_unregister(struct zl3073x_dpll_pin *pin) |
| 1487 |
{ |
| 1488 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 1489 |
const struct dpll_pin_ops *ops; |
| 1490 |
|
| 1491 |
WARN(!pin->dpll_pin, "DPLL pin is not registered\n"); |
| 1492 |
|
| 1493 |
if (zl3073x_dpll_is_input_pin(pin)) |
| 1494 |
ops = &zl3073x_dpll_input_pin_ops; |
| 1495 |
else |
| 1496 |
ops = &zl3073x_dpll_output_pin_ops; |
| 1497 |
|
| 1498 |
|
| 1499 |
dpll_pin_unregister(zldpll->dpll_dev, pin->dpll_pin, ops, pin); |
| 1500 |
|
| 1501 |
dpll_pin_put(pin->dpll_pin, &pin->tracker); |
| 1502 |
pin->dpll_pin = NULL; |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
static void |
| 1513 |
zl3073x_dpll_pins_unregister(struct zl3073x_dpll *zldpll) |
| 1514 |
{ |
| 1515 |
struct zl3073x_dpll_pin *pin, *next; |
| 1516 |
|
| 1517 |
list_for_each_entry_safe(pin, next, &zldpll->pins, list) { |
| 1518 |
zl3073x_dpll_pin_unregister(pin); |
| 1519 |
list_del(&pin->list); |
| 1520 |
zl3073x_dpll_pin_free(pin); |
| 1521 |
} |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
static bool |
| 1539 |
zl3073x_dpll_pin_is_registrable(struct zl3073x_dpll *zldpll, |
| 1540 |
enum dpll_pin_direction dir, u8 index) |
| 1541 |
{ |
| 1542 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1543 |
bool is_diff, is_enabled; |
| 1544 |
const char *name; |
| 1545 |
|
| 1546 |
if (dir == DPLL_PIN_DIRECTION_INPUT) { |
| 1547 |
u8 ref_id = zl3073x_input_pin_ref_get(index); |
| 1548 |
const struct zl3073x_ref *ref; |
| 1549 |
|
| 1550 |
|
| 1551 |
if (zldpll->refsel_mode == ZL_DPLL_MODE_REFSEL_MODE_NCO) |
| 1552 |
return false; |
| 1553 |
|
| 1554 |
name = "REF"; |
| 1555 |
ref = zl3073x_ref_state_get(zldev, ref_id); |
| 1556 |
is_diff = zl3073x_ref_is_diff(ref); |
| 1557 |
is_enabled = zl3073x_ref_is_enabled(ref); |
| 1558 |
} else { |
| 1559 |
|
| 1560 |
u8 out = zl3073x_output_pin_out_get(index); |
| 1561 |
|
| 1562 |
|
| 1563 |
if (zl3073x_dev_out_dpll_get(zldev, out) != zldpll->id) { |
| 1564 |
dev_dbg(zldev->dev, |
| 1565 |
"OUT%u is driven by different DPLL\n", out); |
| 1566 |
|
| 1567 |
return false; |
| 1568 |
} |
| 1569 |
|
| 1570 |
name = "OUT"; |
| 1571 |
is_diff = zl3073x_dev_out_is_diff(zldev, out); |
| 1572 |
is_enabled = zl3073x_dev_output_pin_is_enabled(zldev, index); |
| 1573 |
} |
| 1574 |
|
| 1575 |
|
| 1576 |
if (is_diff && zl3073x_is_n_pin(index)) { |
| 1577 |
dev_dbg(zldev->dev, "%s%u is differential, skipping N-pin\n", |
| 1578 |
name, index / 2); |
| 1579 |
|
| 1580 |
return false; |
| 1581 |
} |
| 1582 |
|
| 1583 |
|
| 1584 |
if (!is_enabled) { |
| 1585 |
dev_dbg(zldev->dev, "%s%u%c is disabled\n", name, index / 2, |
| 1586 |
zl3073x_is_p_pin(index) ? 'P' : 'N'); |
| 1587 |
|
| 1588 |
return false; |
| 1589 |
} |
| 1590 |
|
| 1591 |
return true; |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
static int |
| 1604 |
zl3073x_dpll_pins_register(struct zl3073x_dpll *zldpll) |
| 1605 |
{ |
| 1606 |
struct zl3073x_dpll_pin *pin; |
| 1607 |
enum dpll_pin_direction dir; |
| 1608 |
u8 id, index; |
| 1609 |
int rc; |
| 1610 |
|
| 1611 |
|
| 1612 |
for (index = 0; index < ZL3073X_NUM_PINS; index++) { |
| 1613 |
|
| 1614 |
if (index < ZL3073X_NUM_INPUT_PINS) { |
| 1615 |
id = index; |
| 1616 |
dir = DPLL_PIN_DIRECTION_INPUT; |
| 1617 |
} else { |
| 1618 |
id = index - ZL3073X_NUM_INPUT_PINS; |
| 1619 |
dir = DPLL_PIN_DIRECTION_OUTPUT; |
| 1620 |
} |
| 1621 |
|
| 1622 |
|
| 1623 |
if (!zl3073x_dpll_pin_is_registrable(zldpll, dir, id)) |
| 1624 |
continue; |
| 1625 |
|
| 1626 |
pin = zl3073x_dpll_pin_alloc(zldpll, dir, id); |
| 1627 |
if (IS_ERR(pin)) { |
| 1628 |
rc = PTR_ERR(pin); |
| 1629 |
goto error; |
| 1630 |
} |
| 1631 |
|
| 1632 |
rc = zl3073x_dpll_pin_register(pin, index); |
| 1633 |
if (rc) |
| 1634 |
goto error; |
| 1635 |
|
| 1636 |
list_add(&pin->list, &zldpll->pins); |
| 1637 |
} |
| 1638 |
|
| 1639 |
return 0; |
| 1640 |
|
| 1641 |
error: |
| 1642 |
zl3073x_dpll_pins_unregister(zldpll); |
| 1643 |
|
| 1644 |
return rc; |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
static int |
| 1656 |
zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll) |
| 1657 |
{ |
| 1658 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1659 |
u8 dpll_mode_refsel; |
| 1660 |
int rc; |
| 1661 |
|
| 1662 |
|
| 1663 |
rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MODE_REFSEL(zldpll->id), |
| 1664 |
&dpll_mode_refsel); |
| 1665 |
if (rc) |
| 1666 |
return rc; |
| 1667 |
|
| 1668 |
|
| 1669 |
zldpll->refsel_mode = FIELD_GET(ZL_DPLL_MODE_REFSEL_MODE, |
| 1670 |
dpll_mode_refsel); |
| 1671 |
zldpll->forced_ref = FIELD_GET(ZL_DPLL_MODE_REFSEL_REF, |
| 1672 |
dpll_mode_refsel); |
| 1673 |
|
| 1674 |
zldpll->dpll_dev = dpll_device_get(zldev->clock_id, zldpll->id, |
| 1675 |
THIS_MODULE, &zldpll->tracker); |
| 1676 |
if (IS_ERR(zldpll->dpll_dev)) { |
| 1677 |
rc = PTR_ERR(zldpll->dpll_dev); |
| 1678 |
zldpll->dpll_dev = NULL; |
| 1679 |
|
| 1680 |
return rc; |
| 1681 |
} |
| 1682 |
|
| 1683 |
rc = dpll_device_register(zldpll->dpll_dev, |
| 1684 |
zl3073x_prop_dpll_type_get(zldev, zldpll->id), |
| 1685 |
&zl3073x_dpll_device_ops, zldpll); |
| 1686 |
if (rc) { |
| 1687 |
dpll_device_put(zldpll->dpll_dev, &zldpll->tracker); |
| 1688 |
zldpll->dpll_dev = NULL; |
| 1689 |
} |
| 1690 |
|
| 1691 |
return rc; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
static void |
| 1702 |
zl3073x_dpll_device_unregister(struct zl3073x_dpll *zldpll) |
| 1703 |
{ |
| 1704 |
WARN(!zldpll->dpll_dev, "DPLL device is not registered\n"); |
| 1705 |
|
| 1706 |
cancel_work_sync(&zldpll->change_work); |
| 1707 |
|
| 1708 |
dpll_device_unregister(zldpll->dpll_dev, &zl3073x_dpll_device_ops, |
| 1709 |
zldpll); |
| 1710 |
dpll_device_put(zldpll->dpll_dev, &zldpll->tracker); |
| 1711 |
zldpll->dpll_dev = NULL; |
| 1712 |
} |
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
static bool |
| 1723 |
zl3073x_dpll_pin_phase_offset_check(struct zl3073x_dpll_pin *pin) |
| 1724 |
{ |
| 1725 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 1726 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1727 |
unsigned int reg; |
| 1728 |
s64 phase_offset; |
| 1729 |
u8 ref_id; |
| 1730 |
int rc; |
| 1731 |
|
| 1732 |
|
| 1733 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 1734 |
if (!zl3073x_dev_ref_is_status_ok(zldev, ref_id)) |
| 1735 |
return false; |
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
if (pin->pin_state == DPLL_PIN_STATE_CONNECTED) |
| 1744 |
reg = ZL_REG_DPLL_PHASE_ERR_DATA(zldpll->id); |
| 1745 |
else if (zldpll->phase_monitor) |
| 1746 |
reg = ZL_REG_REF_PHASE(ref_id); |
| 1747 |
else |
| 1748 |
return false; |
| 1749 |
|
| 1750 |
|
| 1751 |
rc = zl3073x_read_u48(zldev, reg, &phase_offset); |
| 1752 |
if (rc) { |
| 1753 |
dev_err(zldev->dev, "Failed to read ref phase offset: %pe\n", |
| 1754 |
ERR_PTR(rc)); |
| 1755 |
|
| 1756 |
return false; |
| 1757 |
} |
| 1758 |
|
| 1759 |
|
| 1760 |
phase_offset = div_s64(sign_extend64(phase_offset, 47), 100); |
| 1761 |
|
| 1762 |
|
| 1763 |
if (phase_offset != pin->phase_offset) { |
| 1764 |
dev_dbg(zldev->dev, "%s phase offset changed: %lld -> %lld\n", |
| 1765 |
pin->label, pin->phase_offset, phase_offset); |
| 1766 |
pin->phase_offset = phase_offset; |
| 1767 |
|
| 1768 |
return true; |
| 1769 |
} |
| 1770 |
|
| 1771 |
return false; |
| 1772 |
} |
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
static bool |
| 1783 |
zl3073x_dpll_pin_ffo_check(struct zl3073x_dpll_pin *pin) |
| 1784 |
{ |
| 1785 |
struct zl3073x_dpll *zldpll = pin->dpll; |
| 1786 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1787 |
const struct zl3073x_ref *ref; |
| 1788 |
u8 ref_id; |
| 1789 |
|
| 1790 |
|
| 1791 |
ref_id = zl3073x_input_pin_ref_get(pin->id); |
| 1792 |
ref = zl3073x_ref_state_get(zldev, ref_id); |
| 1793 |
|
| 1794 |
|
| 1795 |
if (!zl3073x_ref_is_status_ok(ref)) |
| 1796 |
return false; |
| 1797 |
|
| 1798 |
|
| 1799 |
if (pin->freq_offset != ref->ffo) { |
| 1800 |
dev_dbg(zldev->dev, "%s freq offset changed: %lld -> %lld\n", |
| 1801 |
pin->label, pin->freq_offset, ref->ffo); |
| 1802 |
pin->freq_offset = ref->ffo; |
| 1803 |
|
| 1804 |
return true; |
| 1805 |
} |
| 1806 |
|
| 1807 |
return false; |
| 1808 |
} |
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
void |
| 1820 |
zl3073x_dpll_changes_check(struct zl3073x_dpll *zldpll) |
| 1821 |
{ |
| 1822 |
struct zl3073x_dev *zldev = zldpll->dev; |
| 1823 |
enum dpll_lock_status lock_status; |
| 1824 |
struct device *dev = zldev->dev; |
| 1825 |
struct zl3073x_dpll_pin *pin; |
| 1826 |
int rc; |
| 1827 |
|
| 1828 |
zldpll->check_count++; |
| 1829 |
|
| 1830 |
|
| 1831 |
rc = zl3073x_dpll_lock_status_get(zldpll->dpll_dev, zldpll, |
| 1832 |
&lock_status, NULL, NULL); |
| 1833 |
if (rc) { |
| 1834 |
dev_err(dev, "Failed to get DPLL%u lock status: %pe\n", |
| 1835 |
zldpll->id, ERR_PTR(rc)); |
| 1836 |
return; |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
if (zldpll->lock_status != lock_status) { |
| 1841 |
zldpll->lock_status = lock_status; |
| 1842 |
dpll_device_change_ntf(zldpll->dpll_dev); |
| 1843 |
} |
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
if (zldpll->refsel_mode != ZL_DPLL_MODE_REFSEL_MODE_AUTO && |
| 1849 |
zldpll->refsel_mode != ZL_DPLL_MODE_REFSEL_MODE_REFLOCK) |
| 1850 |
return; |
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
if (zldpll->phase_monitor) { |
| 1856 |
rc = zl3073x_ref_phase_offsets_update(zldev, zldpll->id); |
| 1857 |
if (rc) { |
| 1858 |
dev_err(zldev->dev, |
| 1859 |
"Failed to update phase offsets: %pe\n", |
| 1860 |
ERR_PTR(rc)); |
| 1861 |
return; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
list_for_each_entry(pin, &zldpll->pins, list) { |
| 1866 |
enum dpll_pin_state state; |
| 1867 |
bool pin_changed = false; |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
if (!zl3073x_dpll_is_input_pin(pin)) |
| 1873 |
continue; |
| 1874 |
|
| 1875 |
rc = zl3073x_dpll_ref_state_get(pin, &state); |
| 1876 |
if (rc) { |
| 1877 |
dev_err(dev, |
| 1878 |
"Failed to get %s on DPLL%u state: %pe\n", |
| 1879 |
pin->label, zldpll->id, ERR_PTR(rc)); |
| 1880 |
return; |
| 1881 |
} |
| 1882 |
|
| 1883 |
if (state != pin->pin_state) { |
| 1884 |
dev_dbg(dev, "%s state changed: %u->%u\n", pin->label, |
| 1885 |
pin->pin_state, state); |
| 1886 |
pin->pin_state = state; |
| 1887 |
pin_changed = true; |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
if (zldpll->check_count % 2 == 0) { |
| 1892 |
if (zl3073x_dpll_pin_phase_offset_check(pin)) |
| 1893 |
pin_changed = true; |
| 1894 |
|
| 1895 |
if (zl3073x_dpll_pin_ffo_check(pin)) |
| 1896 |
pin_changed = true; |
| 1897 |
} |
| 1898 |
|
| 1899 |
if (pin_changed) |
| 1900 |
dpll_pin_change_ntf(pin->dpll_pin); |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
int |
| 1913 |
zl3073x_dpll_init_fine_phase_adjust(struct zl3073x_dev *zldev) |
| 1914 |
{ |
| 1915 |
int rc; |
| 1916 |
|
| 1917 |
rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_MASK, 0x1f); |
| 1918 |
if (rc) |
| 1919 |
return rc; |
| 1920 |
|
| 1921 |
rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_INTVL, 0x01); |
| 1922 |
if (rc) |
| 1923 |
return rc; |
| 1924 |
|
| 1925 |
rc = zl3073x_write_u16(zldev, ZL_REG_SYNTH_PHASE_SHIFT_DATA, 0xffff); |
| 1926 |
if (rc) |
| 1927 |
return rc; |
| 1928 |
|
| 1929 |
rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_CTRL, 0x01); |
| 1930 |
if (rc) |
| 1931 |
return rc; |
| 1932 |
|
| 1933 |
return rc; |
| 1934 |
} |
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
struct zl3073x_dpll * |
| 1946 |
zl3073x_dpll_alloc(struct zl3073x_dev *zldev, u8 ch) |
| 1947 |
{ |
| 1948 |
struct zl3073x_dpll *zldpll; |
| 1949 |
|
| 1950 |
zldpll = kzalloc_obj(*zldpll); |
| 1951 |
if (!zldpll) |
| 1952 |
return ERR_PTR(-ENOMEM); |
| 1953 |
|
| 1954 |
zldpll->dev = zldev; |
| 1955 |
zldpll->id = ch; |
| 1956 |
INIT_LIST_HEAD(&zldpll->pins); |
| 1957 |
INIT_WORK(&zldpll->change_work, zl3073x_dpll_change_work); |
| 1958 |
|
| 1959 |
return zldpll; |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
void |
| 1969 |
zl3073x_dpll_free(struct zl3073x_dpll *zldpll) |
| 1970 |
{ |
| 1971 |
WARN(zldpll->dpll_dev, "DPLL device is still registered\n"); |
| 1972 |
|
| 1973 |
kfree(zldpll); |
| 1974 |
} |
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
int |
| 1985 |
zl3073x_dpll_register(struct zl3073x_dpll *zldpll) |
| 1986 |
{ |
| 1987 |
int rc; |
| 1988 |
|
| 1989 |
rc = zl3073x_dpll_device_register(zldpll); |
| 1990 |
if (rc) |
| 1991 |
return rc; |
| 1992 |
|
| 1993 |
rc = zl3073x_dpll_pins_register(zldpll); |
| 1994 |
if (rc) { |
| 1995 |
zl3073x_dpll_device_unregister(zldpll); |
| 1996 |
return rc; |
| 1997 |
} |
| 1998 |
|
| 1999 |
return 0; |
| 2000 |
} |
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
void |
| 2010 |
zl3073x_dpll_unregister(struct zl3073x_dpll *zldpll) |
| 2011 |
{ |
| 2012 |
|
| 2013 |
zl3073x_dpll_pins_unregister(zldpll); |
| 2014 |
zl3073x_dpll_device_unregister(zldpll); |
| 2015 |
} |