| 1 |
|
| 2 |
|
| 3 |
#include <linux/array_size.h> |
| 4 |
#include <linux/dev_printk.h> |
| 5 |
#include <linux/err.h> |
| 6 |
#include <linux/errno.h> |
| 7 |
#include <linux/fwnode.h> |
| 8 |
#include <linux/property.h> |
| 9 |
#include <linux/slab.h> |
| 10 |
#include <linux/string.h> |
| 11 |
|
| 12 |
#include "core.h" |
| 13 |
#include "prop.h" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static bool |
| 30 |
zl3073x_pin_check_freq(struct zl3073x_dev *zldev, enum dpll_pin_direction dir, |
| 31 |
u8 id, u64 freq) |
| 32 |
{ |
| 33 |
if (freq > U32_MAX) |
| 34 |
goto err_inv_freq; |
| 35 |
|
| 36 |
if (dir == DPLL_PIN_DIRECTION_INPUT) { |
| 37 |
int rc; |
| 38 |
|
| 39 |
|
| 40 |
rc = zl3073x_ref_freq_factorize(freq, NULL, NULL); |
| 41 |
if (rc) |
| 42 |
goto err_inv_freq; |
| 43 |
} else { |
| 44 |
u32 synth_freq; |
| 45 |
u8 out, synth; |
| 46 |
|
| 47 |
|
| 48 |
out = zl3073x_output_pin_out_get(id); |
| 49 |
synth = zl3073x_dev_out_synth_get(zldev, out); |
| 50 |
|
| 51 |
|
| 52 |
synth_freq = zl3073x_dev_synth_freq_get(zldev, synth); |
| 53 |
|
| 54 |
|
| 55 |
if (synth_freq % (u32)freq) |
| 56 |
goto err_inv_freq; |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
|
| 61 |
err_inv_freq: |
| 62 |
dev_warn(zldev->dev, |
| 63 |
"Unsupported frequency %llu Hz in firmware node\n", freq); |
| 64 |
|
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static void |
| 84 |
zl3073x_prop_pin_package_label_set(struct zl3073x_dev *zldev, |
| 85 |
struct zl3073x_pin_props *props, |
| 86 |
enum dpll_pin_direction dir, u8 id) |
| 87 |
{ |
| 88 |
const char *prefix, *suffix; |
| 89 |
bool is_diff; |
| 90 |
|
| 91 |
if (dir == DPLL_PIN_DIRECTION_INPUT) { |
| 92 |
u8 ref; |
| 93 |
|
| 94 |
prefix = "REF"; |
| 95 |
ref = zl3073x_input_pin_ref_get(id); |
| 96 |
is_diff = zl3073x_dev_ref_is_diff(zldev, ref); |
| 97 |
} else { |
| 98 |
u8 out; |
| 99 |
|
| 100 |
prefix = "OUT"; |
| 101 |
out = zl3073x_output_pin_out_get(id); |
| 102 |
is_diff = zl3073x_dev_out_is_diff(zldev, out); |
| 103 |
} |
| 104 |
|
| 105 |
if (!is_diff) |
| 106 |
suffix = zl3073x_is_p_pin(id) ? "P" : "N"; |
| 107 |
else |
| 108 |
suffix = ""; |
| 109 |
|
| 110 |
snprintf(props->package_label, sizeof(props->package_label), "%s%u%s", |
| 111 |
prefix, id / 2, suffix); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
props->dpll_props.package_label = props->package_label; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
static int |
| 129 |
zl3073x_prop_pin_fwnode_get(struct zl3073x_dev *zldev, |
| 130 |
struct zl3073x_pin_props *props, |
| 131 |
enum dpll_pin_direction dir, u8 id) |
| 132 |
{ |
| 133 |
struct fwnode_handle *pins_node, *pin_node; |
| 134 |
const char *node_name; |
| 135 |
|
| 136 |
if (dir == DPLL_PIN_DIRECTION_INPUT) |
| 137 |
node_name = "input-pins"; |
| 138 |
else |
| 139 |
node_name = "output-pins"; |
| 140 |
|
| 141 |
|
| 142 |
pins_node = device_get_named_child_node(zldev->dev, node_name); |
| 143 |
if (!pins_node) { |
| 144 |
dev_dbg(zldev->dev, "'%s' sub-node is missing\n", node_name); |
| 145 |
return -ENOENT; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
fwnode_for_each_child_node(pins_node, pin_node) { |
| 150 |
u32 reg; |
| 151 |
|
| 152 |
if (fwnode_property_read_u32(pin_node, "reg", ®)) |
| 153 |
continue; |
| 154 |
|
| 155 |
if (id == reg) |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
fwnode_handle_put(pins_node); |
| 161 |
|
| 162 |
|
| 163 |
props->fwnode = pin_node; |
| 164 |
|
| 165 |
dev_dbg(zldev->dev, "Firmware node for %s %sfound\n", |
| 166 |
props->package_label, pin_node ? "" : "NOT "); |
| 167 |
|
| 168 |
return pin_node ? 0 : -ENOENT; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
struct zl3073x_pin_props *zl3073x_pin_props_get(struct zl3073x_dev *zldev, |
| 191 |
enum dpll_pin_direction dir, |
| 192 |
u8 index) |
| 193 |
{ |
| 194 |
struct dpll_pin_frequency *ranges; |
| 195 |
struct zl3073x_pin_props *props; |
| 196 |
int i, j, num_freqs = 0, rc; |
| 197 |
u64 *freqs = NULL; |
| 198 |
const char *type; |
| 199 |
u32 curr_freq; |
| 200 |
|
| 201 |
props = kzalloc_obj(*props); |
| 202 |
if (!props) |
| 203 |
return ERR_PTR(-ENOMEM); |
| 204 |
|
| 205 |
|
| 206 |
if (dir == DPLL_PIN_DIRECTION_INPUT) { |
| 207 |
props->dpll_props.type = DPLL_PIN_TYPE_EXT; |
| 208 |
props->dpll_props.capabilities = |
| 209 |
DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE | |
| 210 |
DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE; |
| 211 |
curr_freq = zl3073x_dev_ref_freq_get(zldev, index); |
| 212 |
} else { |
| 213 |
u8 out, synth; |
| 214 |
u32 f; |
| 215 |
|
| 216 |
props->dpll_props.type = DPLL_PIN_TYPE_GNSS; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
out = zl3073x_output_pin_out_get(index); |
| 222 |
synth = zl3073x_dev_out_synth_get(zldev, out); |
| 223 |
f = 2 * zl3073x_dev_synth_freq_get(zldev, synth); |
| 224 |
props->dpll_props.phase_gran = f ? div_u64(PSEC_PER_SEC, f) : 1; |
| 225 |
curr_freq = zl3073x_dev_output_pin_freq_get(zldev, index); |
| 226 |
} |
| 227 |
|
| 228 |
props->dpll_props.phase_range.min = S32_MIN; |
| 229 |
props->dpll_props.phase_range.max = S32_MAX; |
| 230 |
|
| 231 |
zl3073x_prop_pin_package_label_set(zldev, props, dir, index); |
| 232 |
|
| 233 |
|
| 234 |
rc = zl3073x_prop_pin_fwnode_get(zldev, props, dir, index); |
| 235 |
if (rc) |
| 236 |
goto skip_fwnode_props; |
| 237 |
|
| 238 |
|
| 239 |
fwnode_property_read_string(props->fwnode, "label", |
| 240 |
&props->dpll_props.board_label); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
if (!fwnode_property_read_string(props->fwnode, "connection-type", |
| 246 |
&type)) { |
| 247 |
if (!strcmp(type, "ext")) |
| 248 |
props->dpll_props.type = DPLL_PIN_TYPE_EXT; |
| 249 |
else if (!strcmp(type, "gnss")) |
| 250 |
props->dpll_props.type = DPLL_PIN_TYPE_GNSS; |
| 251 |
else if (!strcmp(type, "int")) |
| 252 |
props->dpll_props.type = DPLL_PIN_TYPE_INT_OSCILLATOR; |
| 253 |
else if (!strcmp(type, "synce")) |
| 254 |
props->dpll_props.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT; |
| 255 |
else if (!strcmp(type, "mux")) |
| 256 |
props->dpll_props.type = DPLL_PIN_TYPE_MUX; |
| 257 |
else |
| 258 |
dev_warn(zldev->dev, |
| 259 |
"Unknown or unsupported pin type '%s'\n", |
| 260 |
type); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
props->esync_control = fwnode_property_read_bool(props->fwnode, |
| 265 |
"esync-control"); |
| 266 |
|
| 267 |
|
| 268 |
num_freqs = fwnode_property_count_u64(props->fwnode, |
| 269 |
"supported-frequencies-hz"); |
| 270 |
if (num_freqs <= 0) { |
| 271 |
num_freqs = 0; |
| 272 |
goto skip_fwnode_props; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
freqs = kcalloc(num_freqs, sizeof(*freqs), GFP_KERNEL); |
| 280 |
if (!freqs) { |
| 281 |
rc = -ENOMEM; |
| 282 |
goto err_alloc_freqs; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
fwnode_property_read_u64_array(props->fwnode, |
| 287 |
"supported-frequencies-hz", freqs, |
| 288 |
num_freqs); |
| 289 |
|
| 290 |
skip_fwnode_props: |
| 291 |
|
| 292 |
ranges = kzalloc_objs(*ranges, num_freqs + 1); |
| 293 |
if (!ranges) { |
| 294 |
rc = -ENOMEM; |
| 295 |
goto err_alloc_ranges; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
ranges[0] = (struct dpll_pin_frequency)DPLL_PIN_FREQUENCY(curr_freq); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
for (i = 0, j = 1; i < num_freqs; i++) { |
| 305 |
struct dpll_pin_frequency freq = DPLL_PIN_FREQUENCY(freqs[i]); |
| 306 |
|
| 307 |
if (freqs[i] == curr_freq) |
| 308 |
continue; |
| 309 |
if (zl3073x_pin_check_freq(zldev, dir, index, freqs[i])) { |
| 310 |
ranges[j] = freq; |
| 311 |
j++; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
props->dpll_props.freq_supported = ranges; |
| 317 |
props->dpll_props.freq_supported_num = j; |
| 318 |
|
| 319 |
|
| 320 |
kfree(freqs); |
| 321 |
|
| 322 |
return props; |
| 323 |
|
| 324 |
err_alloc_ranges: |
| 325 |
kfree(freqs); |
| 326 |
err_alloc_freqs: |
| 327 |
fwnode_handle_put(props->fwnode); |
| 328 |
kfree(props); |
| 329 |
|
| 330 |
return ERR_PTR(rc); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
void zl3073x_pin_props_put(struct zl3073x_pin_props *props) |
| 340 |
{ |
| 341 |
|
| 342 |
kfree(props->dpll_props.freq_supported); |
| 343 |
|
| 344 |
|
| 345 |
if (props->fwnode) |
| 346 |
fwnode_handle_put(props->fwnode); |
| 347 |
|
| 348 |
kfree(props); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
enum dpll_type |
| 359 |
zl3073x_prop_dpll_type_get(struct zl3073x_dev *zldev, u8 index) |
| 360 |
{ |
| 361 |
const char *types[ZL3073X_MAX_CHANNELS]; |
| 362 |
int count; |
| 363 |
|
| 364 |
|
| 365 |
count = device_property_read_string_array(zldev->dev, "dpll-types", |
| 366 |
types, ARRAY_SIZE(types)); |
| 367 |
|
| 368 |
|
| 369 |
if (index >= count) |
| 370 |
return DPLL_TYPE_PPS; |
| 371 |
|
| 372 |
if (!strcmp(types[index], "pps")) |
| 373 |
return DPLL_TYPE_PPS; |
| 374 |
else if (!strcmp(types[index], "eec")) |
| 375 |
return DPLL_TYPE_EEC; |
| 376 |
|
| 377 |
dev_info(zldev->dev, "Unknown DPLL type '%s', using default\n", |
| 378 |
types[index]); |
| 379 |
|
| 380 |
return DPLL_TYPE_PPS; |
| 381 |
} |