Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: wan: Add framer framework support

A framer is a component in charge of an E1/T1 line interface.
Connected usually to a TDM bus, it converts TDM frames to/from E1/T1
frames. It also provides information related to the E1/T1 line.

The framer framework provides a set of APIs for the framer drivers
(framer provider) to create/destroy a framer and APIs for the framer
users (framer consumer) to obtain a reference to the framer, and
use the framer.

This basic implementation provides a framer abstraction for:
- power on/off the framer
- get the framer status (line state)
- be notified on framer status changes
- get/set the framer configuration

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Acked-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/r/20231128132534.258459-2-herve.codina@bootlin.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Herve Codina and committed by
Linus Walleij
82c944d0 b85ea95d

+1316
+2
drivers/net/wan/Kconfig
··· 95 95 comment "X.25/LAPB support is disabled" 96 96 depends on HDLC && (LAPB!=m || HDLC!=m) && LAPB!=y 97 97 98 + source "drivers/net/wan/framer/Kconfig" 99 + 98 100 config PCI200SYN 99 101 tristate "Goramo PCI200SYN support" 100 102 depends on HDLC && PCI
+2
drivers/net/wan/Makefile
··· 14 14 obj-$(CONFIG_HDLC_PPP) += hdlc_ppp.o 15 15 obj-$(CONFIG_HDLC_X25) += hdlc_x25.o 16 16 17 + obj-y += framer/ 18 + 17 19 obj-$(CONFIG_FARSYNC) += farsync.o 18 20 19 21 obj-$(CONFIG_LAPBETHER) += lapbether.o
+25
drivers/net/wan/framer/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + # 3 + # FRAMER 4 + # 5 + 6 + menuconfig FRAMER 7 + tristate "Framer Subsystem" 8 + help 9 + A framer is a component in charge of an E1/T1 line interface. 10 + Connected usually to a TDM bus, it converts TDM frames to/from E1/T1 11 + frames. It also provides information related to the E1/T1 line. 12 + Used with HDLC, the network can be reached through the E1/T1 line. 13 + 14 + This framework is designed to provide a generic interface for framer 15 + devices present in the kernel. This layer will have the generic 16 + API by which framer drivers can create framer using the framer 17 + framework and framer users can obtain reference to the framer. 18 + All the users of this framework should select this config. 19 + 20 + if FRAMER 21 + 22 + config GENERIC_FRAMER 23 + bool 24 + 25 + endif # FRAMER
+6
drivers/net/wan/framer/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + # 3 + # Makefile for the framer drivers. 4 + # 5 + 6 + obj-$(CONFIG_GENERIC_FRAMER) += framer-core.o
+882
drivers/net/wan/framer/framer-core.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Generic Framer framework. 4 + * 5 + * Copyright 2023 CS GROUP France 6 + * 7 + * Author: Herve Codina <herve.codina@bootlin.com> 8 + */ 9 + 10 + #include <linux/device.h> 11 + #include <linux/framer/framer.h> 12 + #include <linux/framer/framer-provider.h> 13 + #include <linux/idr.h> 14 + #include <linux/module.h> 15 + #include <linux/notifier.h> 16 + #include <linux/of.h> 17 + #include <linux/pm_runtime.h> 18 + #include <linux/regulator/consumer.h> 19 + #include <linux/slab.h> 20 + 21 + static struct class *framer_class; 22 + static DEFINE_MUTEX(framer_provider_mutex); 23 + static LIST_HEAD(framer_provider_list); 24 + static DEFINE_IDA(framer_ida); 25 + 26 + #define dev_to_framer(a) (container_of((a), struct framer, dev)) 27 + 28 + int framer_pm_runtime_get(struct framer *framer) 29 + { 30 + int ret; 31 + 32 + if (!pm_runtime_enabled(&framer->dev)) 33 + return -EOPNOTSUPP; 34 + 35 + ret = pm_runtime_get(&framer->dev); 36 + if (ret < 0 && ret != -EINPROGRESS) 37 + pm_runtime_put_noidle(&framer->dev); 38 + 39 + return ret; 40 + } 41 + EXPORT_SYMBOL_GPL(framer_pm_runtime_get); 42 + 43 + int framer_pm_runtime_get_sync(struct framer *framer) 44 + { 45 + int ret; 46 + 47 + if (!pm_runtime_enabled(&framer->dev)) 48 + return -EOPNOTSUPP; 49 + 50 + ret = pm_runtime_get_sync(&framer->dev); 51 + if (ret < 0) 52 + pm_runtime_put_sync(&framer->dev); 53 + 54 + return ret; 55 + } 56 + EXPORT_SYMBOL_GPL(framer_pm_runtime_get_sync); 57 + 58 + int framer_pm_runtime_put(struct framer *framer) 59 + { 60 + if (!pm_runtime_enabled(&framer->dev)) 61 + return -EOPNOTSUPP; 62 + 63 + return pm_runtime_put(&framer->dev); 64 + } 65 + EXPORT_SYMBOL_GPL(framer_pm_runtime_put); 66 + 67 + int framer_pm_runtime_put_sync(struct framer *framer) 68 + { 69 + if (!pm_runtime_enabled(&framer->dev)) 70 + return -EOPNOTSUPP; 71 + 72 + return pm_runtime_put_sync(&framer->dev); 73 + } 74 + EXPORT_SYMBOL_GPL(framer_pm_runtime_put_sync); 75 + 76 + /** 77 + * framer_init - framer internal initialization before framer operation 78 + * @framer: the framer returned by framer_get() 79 + * 80 + * Used to allow framer's driver to perform framer internal initialization, 81 + * such as PLL block powering, clock initialization or anything that's 82 + * is required by the framer to perform the start of operation. 83 + * Must be called before framer_power_on(). 84 + * 85 + * Return: %0 if successful, a negative error code otherwise 86 + */ 87 + int framer_init(struct framer *framer) 88 + { 89 + bool start_polling = false; 90 + int ret; 91 + 92 + ret = framer_pm_runtime_get_sync(framer); 93 + if (ret < 0 && ret != -EOPNOTSUPP) 94 + return ret; 95 + ret = 0; /* Override possible ret == -EOPNOTSUPP */ 96 + 97 + mutex_lock(&framer->mutex); 98 + if (framer->power_count > framer->init_count) 99 + dev_warn(&framer->dev, "framer_power_on was called before framer init\n"); 100 + 101 + if (framer->init_count == 0) { 102 + if (framer->ops->init) { 103 + ret = framer->ops->init(framer); 104 + if (ret < 0) { 105 + dev_err(&framer->dev, "framer init failed --> %d\n", ret); 106 + goto out; 107 + } 108 + } 109 + if (framer->ops->flags & FRAMER_FLAG_POLL_STATUS) 110 + start_polling = true; 111 + } 112 + ++framer->init_count; 113 + 114 + out: 115 + mutex_unlock(&framer->mutex); 116 + 117 + if (!ret && start_polling) { 118 + ret = framer_get_status(framer, &framer->prev_status); 119 + if (ret < 0) { 120 + dev_warn(&framer->dev, "framer get status failed --> %d\n", ret); 121 + /* Will be retried on polling_work */ 122 + ret = 0; 123 + } 124 + queue_delayed_work(system_power_efficient_wq, &framer->polling_work, 1 * HZ); 125 + } 126 + 127 + framer_pm_runtime_put(framer); 128 + return ret; 129 + } 130 + EXPORT_SYMBOL_GPL(framer_init); 131 + 132 + /** 133 + * framer_exit - Framer internal un-initialization 134 + * @framer: the framer returned by framer_get() 135 + * 136 + * Must be called after framer_power_off(). 137 + */ 138 + int framer_exit(struct framer *framer) 139 + { 140 + int ret; 141 + 142 + ret = framer_pm_runtime_get_sync(framer); 143 + if (ret < 0 && ret != -EOPNOTSUPP) 144 + return ret; 145 + ret = 0; /* Override possible ret == -EOPNOTSUPP */ 146 + 147 + mutex_lock(&framer->mutex); 148 + --framer->init_count; 149 + if (framer->init_count == 0) { 150 + if (framer->ops->flags & FRAMER_FLAG_POLL_STATUS) { 151 + mutex_unlock(&framer->mutex); 152 + cancel_delayed_work_sync(&framer->polling_work); 153 + mutex_lock(&framer->mutex); 154 + } 155 + 156 + if (framer->ops->exit) 157 + framer->ops->exit(framer); 158 + } 159 + 160 + mutex_unlock(&framer->mutex); 161 + framer_pm_runtime_put(framer); 162 + return ret; 163 + } 164 + EXPORT_SYMBOL_GPL(framer_exit); 165 + 166 + /** 167 + * framer_power_on - Enable the framer and enter proper operation 168 + * @framer: the framer returned by framer_get() 169 + * 170 + * Must be called after framer_init(). 171 + * 172 + * Return: %0 if successful, a negative error code otherwise 173 + */ 174 + int framer_power_on(struct framer *framer) 175 + { 176 + int ret; 177 + 178 + if (framer->pwr) { 179 + ret = regulator_enable(framer->pwr); 180 + if (ret) 181 + return ret; 182 + } 183 + 184 + ret = framer_pm_runtime_get_sync(framer); 185 + if (ret < 0 && ret != -EOPNOTSUPP) 186 + goto err_pm_sync; 187 + 188 + mutex_lock(&framer->mutex); 189 + if (framer->power_count == 0 && framer->ops->power_on) { 190 + ret = framer->ops->power_on(framer); 191 + if (ret < 0) { 192 + dev_err(&framer->dev, "framer poweron failed --> %d\n", ret); 193 + goto err_pwr_on; 194 + } 195 + } 196 + ++framer->power_count; 197 + mutex_unlock(&framer->mutex); 198 + return 0; 199 + 200 + err_pwr_on: 201 + mutex_unlock(&framer->mutex); 202 + framer_pm_runtime_put_sync(framer); 203 + err_pm_sync: 204 + if (framer->pwr) 205 + regulator_disable(framer->pwr); 206 + return ret; 207 + } 208 + EXPORT_SYMBOL_GPL(framer_power_on); 209 + 210 + /** 211 + * framer_power_off - Disable the framer. 212 + * @framer: the framer returned by framer_get() 213 + * 214 + * Must be called before framer_exit(). 215 + * 216 + * Return: %0 if successful, a negative error code otherwise 217 + */ 218 + int framer_power_off(struct framer *framer) 219 + { 220 + int ret; 221 + 222 + mutex_lock(&framer->mutex); 223 + if (framer->power_count == 1 && framer->ops->power_off) { 224 + ret = framer->ops->power_off(framer); 225 + if (ret < 0) { 226 + dev_err(&framer->dev, "framer poweroff failed --> %d\n", ret); 227 + mutex_unlock(&framer->mutex); 228 + return ret; 229 + } 230 + } 231 + --framer->power_count; 232 + mutex_unlock(&framer->mutex); 233 + framer_pm_runtime_put(framer); 234 + 235 + if (framer->pwr) 236 + regulator_disable(framer->pwr); 237 + 238 + return 0; 239 + } 240 + EXPORT_SYMBOL_GPL(framer_power_off); 241 + 242 + /** 243 + * framer_get_status() - Gets the framer status 244 + * @framer: the framer returned by framer_get() 245 + * @status: the status to retrieve 246 + * 247 + * Used to get the framer status. framer_init() must have been called 248 + * on the framer. 249 + * 250 + * Return: %0 if successful, a negative error code otherwise 251 + */ 252 + int framer_get_status(struct framer *framer, struct framer_status *status) 253 + { 254 + int ret; 255 + 256 + if (!framer->ops->get_status) 257 + return -EOPNOTSUPP; 258 + 259 + /* Be sure to have known values (struct padding and future extensions) */ 260 + memset(status, 0, sizeof(*status)); 261 + 262 + mutex_lock(&framer->mutex); 263 + ret = framer->ops->get_status(framer, status); 264 + mutex_unlock(&framer->mutex); 265 + 266 + return ret; 267 + } 268 + EXPORT_SYMBOL_GPL(framer_get_status); 269 + 270 + /** 271 + * framer_set_config() - Sets the framer configuration 272 + * @framer: the framer returned by framer_get() 273 + * @config: the configuration to set 274 + * 275 + * Used to set the framer configuration. framer_init() must have been called 276 + * on the framer. 277 + * 278 + * Return: %0 if successful, a negative error code otherwise 279 + */ 280 + int framer_set_config(struct framer *framer, const struct framer_config *config) 281 + { 282 + int ret; 283 + 284 + if (!framer->ops->set_config) 285 + return -EOPNOTSUPP; 286 + 287 + mutex_lock(&framer->mutex); 288 + ret = framer->ops->set_config(framer, config); 289 + mutex_unlock(&framer->mutex); 290 + 291 + return ret; 292 + } 293 + EXPORT_SYMBOL_GPL(framer_set_config); 294 + 295 + /** 296 + * framer_get_config() - Gets the framer configuration 297 + * @framer: the framer returned by framer_get() 298 + * @config: the configuration to retrieve 299 + * 300 + * Used to get the framer configuration. framer_init() must have been called 301 + * on the framer. 302 + * 303 + * Return: %0 if successful, a negative error code otherwise 304 + */ 305 + int framer_get_config(struct framer *framer, struct framer_config *config) 306 + { 307 + int ret; 308 + 309 + if (!framer->ops->get_config) 310 + return -EOPNOTSUPP; 311 + 312 + mutex_lock(&framer->mutex); 313 + ret = framer->ops->get_config(framer, config); 314 + mutex_unlock(&framer->mutex); 315 + 316 + return ret; 317 + } 318 + EXPORT_SYMBOL_GPL(framer_get_config); 319 + 320 + static void framer_polling_work(struct work_struct *work) 321 + { 322 + struct framer *framer = container_of(work, struct framer, polling_work.work); 323 + struct framer_status status; 324 + int ret; 325 + 326 + ret = framer_get_status(framer, &status); 327 + if (ret) { 328 + dev_err(&framer->dev, "polling, get status failed (%d)\n", ret); 329 + goto end; 330 + } 331 + if (memcmp(&framer->prev_status, &status, sizeof(status))) { 332 + blocking_notifier_call_chain(&framer->notifier_list, 333 + FRAMER_EVENT_STATUS, NULL); 334 + memcpy(&framer->prev_status, &status, sizeof(status)); 335 + } 336 + 337 + end: 338 + /* Re-schedule task in 1 sec */ 339 + queue_delayed_work(system_power_efficient_wq, &framer->polling_work, 1 * HZ); 340 + } 341 + 342 + /** 343 + * framer_notifier_register() - Registers a notifier 344 + * @framer: the framer returned by framer_get() 345 + * @nb: the notifier block to register 346 + * 347 + * Used to register a notifier block on framer events. framer_init() must have 348 + * been called on the framer. 349 + * The available framer events are present in enum framer_events. 350 + * 351 + * Return: %0 if successful, a negative error code otherwise 352 + */ 353 + int framer_notifier_register(struct framer *framer, struct notifier_block *nb) 354 + { 355 + return blocking_notifier_chain_register(&framer->notifier_list, nb); 356 + } 357 + EXPORT_SYMBOL_GPL(framer_notifier_register); 358 + 359 + /** 360 + * framer_notifier_unregister() - Unregisters a notifier 361 + * @framer: the framer returned by framer_get() 362 + * @nb: the notifier block to unregister 363 + * 364 + * Used to unregister a notifier block. framer_init() must have 365 + * been called on the framer. 366 + * 367 + * Return: %0 if successful, a negative error code otherwise 368 + */ 369 + int framer_notifier_unregister(struct framer *framer, struct notifier_block *nb) 370 + { 371 + return blocking_notifier_chain_unregister(&framer->notifier_list, nb); 372 + } 373 + EXPORT_SYMBOL_GPL(framer_notifier_unregister); 374 + 375 + static struct framer_provider *framer_provider_of_lookup(const struct device_node *node) 376 + { 377 + struct framer_provider *framer_provider; 378 + 379 + list_for_each_entry(framer_provider, &framer_provider_list, list) { 380 + if (device_match_of_node(framer_provider->dev, node)) 381 + return framer_provider; 382 + } 383 + 384 + return ERR_PTR(-EPROBE_DEFER); 385 + } 386 + 387 + static struct framer *framer_of_get_from_provider(struct of_phandle_args *args) 388 + { 389 + struct framer_provider *framer_provider; 390 + struct framer *framer; 391 + 392 + mutex_lock(&framer_provider_mutex); 393 + framer_provider = framer_provider_of_lookup(args->np); 394 + if (IS_ERR(framer_provider) || !try_module_get(framer_provider->owner)) { 395 + framer = ERR_PTR(-EPROBE_DEFER); 396 + goto end; 397 + } 398 + 399 + framer = framer_provider->of_xlate(framer_provider->dev, args); 400 + 401 + module_put(framer_provider->owner); 402 + 403 + end: 404 + mutex_unlock(&framer_provider_mutex); 405 + 406 + return framer; 407 + } 408 + 409 + static struct framer *framer_of_get_byphandle(struct device_node *np, const char *propname, 410 + int index) 411 + { 412 + struct of_phandle_args args; 413 + struct framer *framer; 414 + int ret; 415 + 416 + ret = of_parse_phandle_with_optional_args(np, propname, "#framer-cells", index, &args); 417 + if (ret) 418 + return ERR_PTR(-ENODEV); 419 + 420 + if (!of_device_is_available(args.np)) { 421 + framer = ERR_PTR(-ENODEV); 422 + goto out_node_put; 423 + } 424 + 425 + framer = framer_of_get_from_provider(&args); 426 + 427 + out_node_put: 428 + of_node_put(args.np); 429 + 430 + return framer; 431 + } 432 + 433 + static struct framer *framer_of_get_byparent(struct device_node *np, int index) 434 + { 435 + struct of_phandle_args args; 436 + struct framer *framer; 437 + 438 + args.np = of_get_parent(np); 439 + args.args_count = 1; 440 + args.args[0] = index; 441 + 442 + while (args.np) { 443 + framer = framer_of_get_from_provider(&args); 444 + if (IS_ERR(framer) && PTR_ERR(framer) != -EPROBE_DEFER) { 445 + args.np = of_get_next_parent(args.np); 446 + continue; 447 + } 448 + of_node_put(args.np); 449 + return framer; 450 + } 451 + 452 + return ERR_PTR(-ENODEV); 453 + } 454 + 455 + /** 456 + * framer_get() - lookup and obtain a reference to a framer. 457 + * @dev: device that requests the framer 458 + * @con_id: name of the framer from device's point of view 459 + * 460 + * Returns the framer driver, after getting a refcount to it; or 461 + * -ENODEV if there is no such framer. The caller is responsible for 462 + * calling framer_put() to release that count. 463 + */ 464 + struct framer *framer_get(struct device *dev, const char *con_id) 465 + { 466 + struct framer *framer = ERR_PTR(-ENODEV); 467 + struct device_link *link; 468 + int ret; 469 + 470 + if (dev->of_node) { 471 + if (con_id) 472 + framer = framer_of_get_byphandle(dev->of_node, con_id, 0); 473 + else 474 + framer = framer_of_get_byparent(dev->of_node, 0); 475 + } 476 + 477 + if (IS_ERR(framer)) 478 + return framer; 479 + 480 + get_device(&framer->dev); 481 + 482 + if (!try_module_get(framer->ops->owner)) { 483 + ret = -EPROBE_DEFER; 484 + goto err_put_device; 485 + } 486 + 487 + link = device_link_add(dev, &framer->dev, DL_FLAG_STATELESS); 488 + if (!link) { 489 + dev_err(dev, "failed to create device_link to %s\n", dev_name(&framer->dev)); 490 + ret = -EPROBE_DEFER; 491 + goto err_module_put; 492 + } 493 + 494 + return framer; 495 + 496 + err_module_put: 497 + module_put(framer->ops->owner); 498 + err_put_device: 499 + put_device(&framer->dev); 500 + return ERR_PTR(ret); 501 + } 502 + EXPORT_SYMBOL_GPL(framer_get); 503 + 504 + /** 505 + * framer_put() - release the framer 506 + * @dev: device that wants to release this framer 507 + * @framer: the framer returned by framer_get() 508 + * 509 + * Releases a refcount the caller received from framer_get(). 510 + */ 511 + void framer_put(struct device *dev, struct framer *framer) 512 + { 513 + device_link_remove(dev, &framer->dev); 514 + 515 + module_put(framer->ops->owner); 516 + put_device(&framer->dev); 517 + } 518 + EXPORT_SYMBOL_GPL(framer_put); 519 + 520 + static void devm_framer_put(struct device *dev, void *res) 521 + { 522 + struct framer *framer = *(struct framer **)res; 523 + 524 + framer_put(dev, framer); 525 + } 526 + 527 + /** 528 + * devm_framer_get() - lookup and obtain a reference to a framer. 529 + * @dev: device that requests this framer 530 + * @con_id: name of the framer from device's point of view 531 + * 532 + * Gets the framer using framer_get(), and associates a device with it using 533 + * devres. On driver detach, framer_put() function is invoked on the devres 534 + * data, then, devres data is freed. 535 + */ 536 + struct framer *devm_framer_get(struct device *dev, const char *con_id) 537 + { 538 + struct framer **ptr, *framer; 539 + 540 + ptr = devres_alloc(devm_framer_put, sizeof(*ptr), GFP_KERNEL); 541 + if (!ptr) 542 + return ERR_PTR(-ENOMEM); 543 + 544 + framer = framer_get(dev, con_id); 545 + if (!IS_ERR(framer)) { 546 + *ptr = framer; 547 + devres_add(dev, ptr); 548 + } else { 549 + devres_free(ptr); 550 + return framer; 551 + } 552 + 553 + return framer; 554 + } 555 + EXPORT_SYMBOL_GPL(devm_framer_get); 556 + 557 + /** 558 + * devm_framer_optional_get() - lookup and obtain a reference to an optional 559 + * framer. 560 + * @dev: device that requests this framer 561 + * @con_id: name of the framer from device's point of view 562 + * 563 + * Same as devm_framer_get() except that if the framer does not exist, it is not 564 + * considered an error and -ENODEV will not be returned. Instead the NULL framer 565 + * is returned. 566 + */ 567 + struct framer *devm_framer_optional_get(struct device *dev, const char *con_id) 568 + { 569 + struct framer *framer = devm_framer_get(dev, con_id); 570 + 571 + if (PTR_ERR(framer) == -ENODEV) 572 + framer = NULL; 573 + 574 + return framer; 575 + } 576 + EXPORT_SYMBOL_GPL(devm_framer_optional_get); 577 + 578 + static void framer_notify_status_work(struct work_struct *work) 579 + { 580 + struct framer *framer = container_of(work, struct framer, notify_status_work); 581 + 582 + blocking_notifier_call_chain(&framer->notifier_list, FRAMER_EVENT_STATUS, NULL); 583 + } 584 + 585 + void framer_notify_status_change(struct framer *framer) 586 + { 587 + /* Can be called from atomic context -> just schedule a task to call 588 + * blocking notifiers 589 + */ 590 + queue_work(system_power_efficient_wq, &framer->notify_status_work); 591 + } 592 + EXPORT_SYMBOL_GPL(framer_notify_status_change); 593 + 594 + /** 595 + * framer_create() - create a new framer 596 + * @dev: device that is creating the new framer 597 + * @node: device node of the framer. default to dev->of_node. 598 + * @ops: function pointers for performing framer operations 599 + * 600 + * Called to create a framer using framer framework. 601 + */ 602 + struct framer *framer_create(struct device *dev, struct device_node *node, 603 + const struct framer_ops *ops) 604 + { 605 + struct framer *framer; 606 + int ret; 607 + int id; 608 + 609 + /* get_status() is mandatory if the provider ask for polling status */ 610 + if (WARN_ON((ops->flags & FRAMER_FLAG_POLL_STATUS) && !ops->get_status)) 611 + return ERR_PTR(-EINVAL); 612 + 613 + framer = kzalloc(sizeof(*framer), GFP_KERNEL); 614 + if (!framer) 615 + return ERR_PTR(-ENOMEM); 616 + 617 + id = ida_alloc(&framer_ida, GFP_KERNEL); 618 + if (id < 0) { 619 + dev_err(dev, "unable to get id\n"); 620 + ret = id; 621 + goto free_framer; 622 + } 623 + 624 + device_initialize(&framer->dev); 625 + mutex_init(&framer->mutex); 626 + INIT_WORK(&framer->notify_status_work, framer_notify_status_work); 627 + INIT_DELAYED_WORK(&framer->polling_work, framer_polling_work); 628 + BLOCKING_INIT_NOTIFIER_HEAD(&framer->notifier_list); 629 + 630 + framer->dev.class = framer_class; 631 + framer->dev.parent = dev; 632 + framer->dev.of_node = node ? node : dev->of_node; 633 + framer->id = id; 634 + framer->ops = ops; 635 + 636 + ret = dev_set_name(&framer->dev, "framer-%s.%d", dev_name(dev), id); 637 + if (ret) 638 + goto put_dev; 639 + 640 + /* framer-supply */ 641 + framer->pwr = regulator_get_optional(&framer->dev, "framer"); 642 + if (IS_ERR(framer->pwr)) { 643 + ret = PTR_ERR(framer->pwr); 644 + if (ret == -EPROBE_DEFER) 645 + goto put_dev; 646 + 647 + framer->pwr = NULL; 648 + } 649 + 650 + ret = device_add(&framer->dev); 651 + if (ret) 652 + goto put_dev; 653 + 654 + if (pm_runtime_enabled(dev)) { 655 + pm_runtime_enable(&framer->dev); 656 + pm_runtime_no_callbacks(&framer->dev); 657 + } 658 + 659 + return framer; 660 + 661 + put_dev: 662 + put_device(&framer->dev); /* calls framer_release() which frees resources */ 663 + return ERR_PTR(ret); 664 + 665 + free_framer: 666 + kfree(framer); 667 + return ERR_PTR(ret); 668 + } 669 + EXPORT_SYMBOL_GPL(framer_create); 670 + 671 + /** 672 + * framer_destroy() - destroy the framer 673 + * @framer: the framer to be destroyed 674 + * 675 + * Called to destroy the framer. 676 + */ 677 + void framer_destroy(struct framer *framer) 678 + { 679 + /* polling_work should already be stopped but if framer_exit() was not 680 + * called (bug), here it's the last time to do that ... 681 + */ 682 + cancel_delayed_work_sync(&framer->polling_work); 683 + cancel_work_sync(&framer->notify_status_work); 684 + pm_runtime_disable(&framer->dev); 685 + device_unregister(&framer->dev); /* calls framer_release() which frees resources */ 686 + } 687 + EXPORT_SYMBOL_GPL(framer_destroy); 688 + 689 + static void devm_framer_destroy(struct device *dev, void *res) 690 + { 691 + struct framer *framer = *(struct framer **)res; 692 + 693 + framer_destroy(framer); 694 + } 695 + 696 + /** 697 + * devm_framer_create() - create a new framer 698 + * @dev: device that is creating the new framer 699 + * @node: device node of the framer 700 + * @ops: function pointers for performing framer operations 701 + * 702 + * Creates a new framer device adding it to the framer class. 703 + * While at that, it also associates the device with the framer using devres. 704 + * On driver detach, release function is invoked on the devres data, 705 + * then, devres data is freed. 706 + */ 707 + struct framer *devm_framer_create(struct device *dev, struct device_node *node, 708 + const struct framer_ops *ops) 709 + { 710 + struct framer **ptr, *framer; 711 + 712 + ptr = devres_alloc(devm_framer_destroy, sizeof(*ptr), GFP_KERNEL); 713 + if (!ptr) 714 + return ERR_PTR(-ENOMEM); 715 + 716 + framer = framer_create(dev, node, ops); 717 + if (!IS_ERR(framer)) { 718 + *ptr = framer; 719 + devres_add(dev, ptr); 720 + } else { 721 + devres_free(ptr); 722 + } 723 + 724 + return framer; 725 + } 726 + EXPORT_SYMBOL_GPL(devm_framer_create); 727 + 728 + /** 729 + * framer_provider_simple_of_xlate() - returns the framer instance from framer provider 730 + * @dev: the framer provider device 731 + * @args: of_phandle_args (not used here) 732 + * 733 + * Intended to be used by framer provider for the common case where #framer-cells is 734 + * 0. For other cases where #framer-cells is greater than '0', the framer provider 735 + * should provide a custom of_xlate function that reads the *args* and returns 736 + * the appropriate framer. 737 + */ 738 + struct framer *framer_provider_simple_of_xlate(struct device *dev, struct of_phandle_args *args) 739 + { 740 + struct class_dev_iter iter; 741 + struct framer *framer; 742 + 743 + class_dev_iter_init(&iter, framer_class, NULL, NULL); 744 + while ((dev = class_dev_iter_next(&iter))) { 745 + framer = dev_to_framer(dev); 746 + if (args->np != framer->dev.of_node) 747 + continue; 748 + 749 + class_dev_iter_exit(&iter); 750 + return framer; 751 + } 752 + 753 + class_dev_iter_exit(&iter); 754 + return ERR_PTR(-ENODEV); 755 + } 756 + EXPORT_SYMBOL_GPL(framer_provider_simple_of_xlate); 757 + 758 + /** 759 + * __framer_provider_of_register() - create/register framer provider with the framework 760 + * @dev: struct device of the framer provider 761 + * @owner: the module owner containing of_xlate 762 + * @of_xlate: function pointer to obtain framer instance from framer provider 763 + * 764 + * Creates struct framer_provider from dev and of_xlate function pointer. 765 + * This is used in the case of dt boot for finding the framer instance from 766 + * framer provider. 767 + */ 768 + struct framer_provider * 769 + __framer_provider_of_register(struct device *dev, struct module *owner, 770 + struct framer *(*of_xlate)(struct device *dev, 771 + struct of_phandle_args *args)) 772 + { 773 + struct framer_provider *framer_provider; 774 + 775 + framer_provider = kzalloc(sizeof(*framer_provider), GFP_KERNEL); 776 + if (!framer_provider) 777 + return ERR_PTR(-ENOMEM); 778 + 779 + framer_provider->dev = dev; 780 + framer_provider->owner = owner; 781 + framer_provider->of_xlate = of_xlate; 782 + 783 + of_node_get(framer_provider->dev->of_node); 784 + 785 + mutex_lock(&framer_provider_mutex); 786 + list_add_tail(&framer_provider->list, &framer_provider_list); 787 + mutex_unlock(&framer_provider_mutex); 788 + 789 + return framer_provider; 790 + } 791 + EXPORT_SYMBOL_GPL(__framer_provider_of_register); 792 + 793 + /** 794 + * framer_provider_of_unregister() - unregister framer provider from the framework 795 + * @framer_provider: framer provider returned by framer_provider_of_register() 796 + * 797 + * Removes the framer_provider created using framer_provider_of_register(). 798 + */ 799 + void framer_provider_of_unregister(struct framer_provider *framer_provider) 800 + { 801 + mutex_lock(&framer_provider_mutex); 802 + list_del(&framer_provider->list); 803 + mutex_unlock(&framer_provider_mutex); 804 + 805 + of_node_put(framer_provider->dev->of_node); 806 + kfree(framer_provider); 807 + } 808 + EXPORT_SYMBOL_GPL(framer_provider_of_unregister); 809 + 810 + static void devm_framer_provider_of_unregister(struct device *dev, void *res) 811 + { 812 + struct framer_provider *framer_provider = *(struct framer_provider **)res; 813 + 814 + framer_provider_of_unregister(framer_provider); 815 + } 816 + 817 + /** 818 + * __devm_framer_provider_of_register() - create/register framer provider with 819 + * the framework 820 + * @dev: struct device of the framer provider 821 + * @owner: the module owner containing of_xlate 822 + * @of_xlate: function pointer to obtain framer instance from framer provider 823 + * 824 + * Creates struct framer_provider from dev and of_xlate function pointer. 825 + * This is used in the case of dt boot for finding the framer instance from 826 + * framer provider. While at that, it also associates the device with the 827 + * framer provider using devres. On driver detach, release function is invoked 828 + * on the devres data, then, devres data is freed. 829 + */ 830 + struct framer_provider * 831 + __devm_framer_provider_of_register(struct device *dev, struct module *owner, 832 + struct framer *(*of_xlate)(struct device *dev, 833 + struct of_phandle_args *args)) 834 + { 835 + struct framer_provider **ptr, *framer_provider; 836 + 837 + ptr = devres_alloc(devm_framer_provider_of_unregister, sizeof(*ptr), GFP_KERNEL); 838 + if (!ptr) 839 + return ERR_PTR(-ENOMEM); 840 + 841 + framer_provider = __framer_provider_of_register(dev, owner, of_xlate); 842 + if (!IS_ERR(framer_provider)) { 843 + *ptr = framer_provider; 844 + devres_add(dev, ptr); 845 + } else { 846 + devres_free(ptr); 847 + } 848 + 849 + return framer_provider; 850 + } 851 + EXPORT_SYMBOL_GPL(__devm_framer_provider_of_register); 852 + 853 + /** 854 + * framer_release() - release the framer 855 + * @dev: the dev member within framer 856 + * 857 + * When the last reference to the device is removed, it is called 858 + * from the embedded kobject as release method. 859 + */ 860 + static void framer_release(struct device *dev) 861 + { 862 + struct framer *framer; 863 + 864 + framer = dev_to_framer(dev); 865 + regulator_put(framer->pwr); 866 + ida_free(&framer_ida, framer->id); 867 + kfree(framer); 868 + } 869 + 870 + static int __init framer_core_init(void) 871 + { 872 + framer_class = class_create("framer"); 873 + if (IS_ERR(framer_class)) { 874 + pr_err("failed to create framer class (%pe)\n", framer_class); 875 + return PTR_ERR(framer_class); 876 + } 877 + 878 + framer_class->dev_release = framer_release; 879 + 880 + return 0; 881 + } 882 + device_initcall(framer_core_init);
+194
include/linux/framer/framer-provider.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Generic framer profider header file 4 + * 5 + * Copyright 2023 CS GROUP France 6 + * 7 + * Author: Herve Codina <herve.codina@bootlin.com> 8 + */ 9 + 10 + #ifndef __DRIVERS_PROVIDER_FRAMER_H 11 + #define __DRIVERS_PROVIDER_FRAMER_H 12 + 13 + #include <linux/export.h> 14 + #include <linux/framer/framer.h> 15 + #include <linux/types.h> 16 + 17 + #define FRAMER_FLAG_POLL_STATUS BIT(0) 18 + 19 + /** 20 + * struct framer_ops - set of function pointers for performing framer operations 21 + * @init: operation to be performed for initializing the framer 22 + * @exit: operation to be performed while exiting 23 + * @power_on: powering on the framer 24 + * @power_off: powering off the framer 25 + * @flags: OR-ed flags (FRAMER_FLAG_*) to ask for core functionality 26 + * - @FRAMER_FLAG_POLL_STATUS: 27 + * Ask the core to perform a polling to get the framer status and 28 + * notify consumers on change. 29 + * The framer should call @framer_notify_status_change() when it 30 + * detects a status change. This is usually done using interrupts. 31 + * If the framer cannot detect this change, it can ask the core for 32 + * a status polling. The core will call @get_status() periodically 33 + * and, on change detected, it will notify the consumer. 34 + * the @get_status() 35 + * @owner: the module owner containing the ops 36 + */ 37 + struct framer_ops { 38 + int (*init)(struct framer *framer); 39 + void (*exit)(struct framer *framer); 40 + int (*power_on)(struct framer *framer); 41 + int (*power_off)(struct framer *framer); 42 + 43 + /** 44 + * @get_status: 45 + * 46 + * Optional. 47 + * 48 + * Used to get the framer status. framer_init() must have 49 + * been called on the framer. 50 + * 51 + * Returns: 0 if successful, an negative error code otherwise 52 + */ 53 + int (*get_status)(struct framer *framer, struct framer_status *status); 54 + 55 + /** 56 + * @set_config: 57 + * 58 + * Optional. 59 + * 60 + * Used to set the framer configuration. framer_init() must have 61 + * been called on the framer. 62 + * 63 + * Returns: 0 if successful, an negative error code otherwise 64 + */ 65 + int (*set_config)(struct framer *framer, const struct framer_config *config); 66 + 67 + /** 68 + * @get_config: 69 + * 70 + * Optional. 71 + * 72 + * Used to get the framer configuration. framer_init() must have 73 + * been called on the framer. 74 + * 75 + * Returns: 0 if successful, an negative error code otherwise 76 + */ 77 + int (*get_config)(struct framer *framer, struct framer_config *config); 78 + 79 + u32 flags; 80 + struct module *owner; 81 + }; 82 + 83 + /** 84 + * struct framer_provider - represents the framer provider 85 + * @dev: framer provider device 86 + * @children: can be used to override the default (dev->of_node) child node 87 + * @owner: the module owner having of_xlate 88 + * @list: to maintain a linked list of framer providers 89 + * @of_xlate: function pointer to obtain framer instance from framer pointer 90 + */ 91 + struct framer_provider { 92 + struct device *dev; 93 + struct module *owner; 94 + struct list_head list; 95 + struct framer * (*of_xlate)(struct device *dev, 96 + struct of_phandle_args *args); 97 + }; 98 + 99 + static inline void framer_set_drvdata(struct framer *framer, void *data) 100 + { 101 + dev_set_drvdata(&framer->dev, data); 102 + } 103 + 104 + static inline void *framer_get_drvdata(struct framer *framer) 105 + { 106 + return dev_get_drvdata(&framer->dev); 107 + } 108 + 109 + #if IS_ENABLED(CONFIG_GENERIC_FRAMER) 110 + 111 + /* Create and destroy a framer */ 112 + struct framer *framer_create(struct device *dev, struct device_node *node, 113 + const struct framer_ops *ops); 114 + void framer_destroy(struct framer *framer); 115 + 116 + /* devm version */ 117 + struct framer *devm_framer_create(struct device *dev, struct device_node *node, 118 + const struct framer_ops *ops); 119 + 120 + struct framer *framer_provider_simple_of_xlate(struct device *dev, 121 + struct of_phandle_args *args); 122 + 123 + struct framer_provider * 124 + __framer_provider_of_register(struct device *dev, struct module *owner, 125 + struct framer *(*of_xlate)(struct device *dev, 126 + struct of_phandle_args *args)); 127 + 128 + void framer_provider_of_unregister(struct framer_provider *framer_provider); 129 + 130 + struct framer_provider * 131 + __devm_framer_provider_of_register(struct device *dev, struct module *owner, 132 + struct framer *(*of_xlate)(struct device *dev, 133 + struct of_phandle_args *args)); 134 + 135 + void framer_notify_status_change(struct framer *framer); 136 + 137 + #else /* IS_ENABLED(CONFIG_GENERIC_FRAMER) */ 138 + 139 + static inline struct framer *framer_create(struct device *dev, struct device_node *node, 140 + const struct framer_ops *ops) 141 + { 142 + return ERR_PTR(-ENOSYS); 143 + } 144 + 145 + static inline void framer_destroy(struct framer *framer) 146 + { 147 + } 148 + 149 + /* devm version */ 150 + static inline struct framer *devm_framer_create(struct device *dev, struct device_node *node, 151 + const struct framer_ops *ops) 152 + { 153 + return ERR_PTR(-ENOSYS); 154 + } 155 + 156 + static inline struct framer *framer_provider_simple_of_xlate(struct device *dev, 157 + struct of_phandle_args *args) 158 + { 159 + return ERR_PTR(-ENOSYS); 160 + } 161 + 162 + static inline struct framer_provider * 163 + __framer_provider_of_register(struct device *dev, struct module *owner, 164 + struct framer *(*of_xlate)(struct device *dev, 165 + struct of_phandle_args *args)) 166 + { 167 + return ERR_PTR(-ENOSYS); 168 + } 169 + 170 + void framer_provider_of_unregister(struct framer_provider *framer_provider) 171 + { 172 + } 173 + 174 + static inline struct framer_provider * 175 + __devm_framer_provider_of_register(struct device *dev, struct module *owner, 176 + struct framer *(*of_xlate)(struct device *dev, 177 + struct of_phandle_args *args)) 178 + { 179 + return ERR_PTR(-ENOSYS); 180 + } 181 + 182 + void framer_notify_status_change(struct framer *framer) 183 + { 184 + } 185 + 186 + #endif /* IS_ENABLED(CONFIG_GENERIC_FRAMER) */ 187 + 188 + #define framer_provider_of_register(dev, xlate) \ 189 + __framer_provider_of_register((dev), THIS_MODULE, (xlate)) 190 + 191 + #define devm_framer_provider_of_register(dev, xlate) \ 192 + __devm_framer_provider_of_register((dev), THIS_MODULE, (xlate)) 193 + 194 + #endif /* __DRIVERS_PROVIDER_FRAMER_H */
+205
include/linux/framer/framer.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Generic framer header file 4 + * 5 + * Copyright 2023 CS GROUP France 6 + * 7 + * Author: Herve Codina <herve.codina@bootlin.com> 8 + */ 9 + 10 + #ifndef __DRIVERS_FRAMER_H 11 + #define __DRIVERS_FRAMER_H 12 + 13 + #include <linux/err.h> 14 + #include <linux/mutex.h> 15 + #include <linux/notifier.h> 16 + #include <linux/of.h> 17 + #include <linux/device.h> 18 + #include <linux/workqueue.h> 19 + 20 + /** 21 + * enum framer_iface - Framer interface 22 + * @FRAMER_IFACE_E1: E1 interface 23 + * @FRAMER_IFACE_T1: T1 interface 24 + */ 25 + enum framer_iface { 26 + FRAMER_IFACE_E1, 27 + FRAMER_IFACE_T1, 28 + }; 29 + 30 + /** 31 + * enum framer_clock_type - Framer clock type 32 + * @FRAMER_CLOCK_EXT: External clock 33 + * @FRAMER_CLOCK_INT: Internal clock 34 + */ 35 + enum framer_clock_type { 36 + FRAMER_CLOCK_EXT, 37 + FRAMER_CLOCK_INT, 38 + }; 39 + 40 + /** 41 + * struct framer_config - Framer configuration 42 + * @iface: Framer line interface 43 + * @clock_type: Framer clock type 44 + * @line_clock_rate: Framer line clock rate 45 + */ 46 + struct framer_config { 47 + enum framer_iface iface; 48 + enum framer_clock_type clock_type; 49 + unsigned long line_clock_rate; 50 + }; 51 + 52 + /** 53 + * struct framer_status - Framer status 54 + * @link_is_on: Framer link state. true, the link is on, false, the link is off. 55 + */ 56 + struct framer_status { 57 + bool link_is_on; 58 + }; 59 + 60 + /** 61 + * enum framer_event - Event available for notification 62 + * @FRAMER_EVENT_STATUS: Event notified on framer_status changes 63 + */ 64 + enum framer_event { 65 + FRAMER_EVENT_STATUS, 66 + }; 67 + 68 + /** 69 + * struct framer - represents the framer device 70 + * @dev: framer device 71 + * @id: id of the framer device 72 + * @ops: function pointers for performing framer operations 73 + * @mutex: mutex to protect framer_ops 74 + * @init_count: used to protect when the framer is used by multiple consumers 75 + * @power_count: used to protect when the framer is used by multiple consumers 76 + * @pwr: power regulator associated with the framer 77 + * @notify_status_work: work structure used for status notifications 78 + * @notifier_list: notifier list used for notifications 79 + * @polling_work: delayed work structure used for the polling task 80 + * @prev_status: previous read status used by the polling task to detect changes 81 + */ 82 + struct framer { 83 + struct device dev; 84 + int id; 85 + const struct framer_ops *ops; 86 + struct mutex mutex; /* Protect framer */ 87 + int init_count; 88 + int power_count; 89 + struct regulator *pwr; 90 + struct work_struct notify_status_work; 91 + struct blocking_notifier_head notifier_list; 92 + struct delayed_work polling_work; 93 + struct framer_status prev_status; 94 + }; 95 + 96 + #if IS_ENABLED(CONFIG_GENERIC_FRAMER) 97 + int framer_pm_runtime_get(struct framer *framer); 98 + int framer_pm_runtime_get_sync(struct framer *framer); 99 + int framer_pm_runtime_put(struct framer *framer); 100 + int framer_pm_runtime_put_sync(struct framer *framer); 101 + int framer_init(struct framer *framer); 102 + int framer_exit(struct framer *framer); 103 + int framer_power_on(struct framer *framer); 104 + int framer_power_off(struct framer *framer); 105 + int framer_get_status(struct framer *framer, struct framer_status *status); 106 + int framer_get_config(struct framer *framer, struct framer_config *config); 107 + int framer_set_config(struct framer *framer, const struct framer_config *config); 108 + int framer_notifier_register(struct framer *framer, struct notifier_block *nb); 109 + int framer_notifier_unregister(struct framer *framer, struct notifier_block *nb); 110 + 111 + struct framer *framer_get(struct device *dev, const char *con_id); 112 + void framer_put(struct device *dev, struct framer *framer); 113 + 114 + struct framer *devm_framer_get(struct device *dev, const char *con_id); 115 + struct framer *devm_framer_optional_get(struct device *dev, const char *con_id); 116 + #else 117 + static inline int framer_pm_runtime_get(struct framer *framer) 118 + { 119 + return -ENOSYS; 120 + } 121 + 122 + static inline int framer_pm_runtime_get_sync(struct framer *framer) 123 + { 124 + return -ENOSYS; 125 + } 126 + 127 + static inline int framer_pm_runtime_put(struct framer *framer) 128 + { 129 + return -ENOSYS; 130 + } 131 + 132 + static inline int framer_pm_runtime_put_sync(struct framer *framer) 133 + { 134 + return -ENOSYS; 135 + } 136 + 137 + static inline int framer_init(struct framer *framer) 138 + { 139 + return -ENOSYS; 140 + } 141 + 142 + static inline int framer_exit(struct framer *framer) 143 + { 144 + return -ENOSYS; 145 + } 146 + 147 + static inline int framer_power_on(struct framer *framer) 148 + { 149 + return -ENOSYS; 150 + } 151 + 152 + static inline int framer_power_off(struct framer *framer) 153 + { 154 + return -ENOSYS; 155 + } 156 + 157 + static inline int framer_get_status(struct framer *framer, struct framer_status *status) 158 + { 159 + return -ENOSYS; 160 + } 161 + 162 + static inline int framer_get_config(struct framer *framer, struct framer_config *config) 163 + { 164 + return -ENOSYS; 165 + } 166 + 167 + static inline int framer_set_config(struct framer *framer, const struct framer_config *config) 168 + { 169 + return -ENOSYS; 170 + } 171 + 172 + static inline int framer_notifier_register(struct framer *framer, 173 + struct notifier_block *nb) 174 + { 175 + return -ENOSYS; 176 + } 177 + 178 + static inline int framer_notifier_unregister(struct framer *framer, 179 + struct notifier_block *nb) 180 + { 181 + return -ENOSYS; 182 + } 183 + 184 + struct framer *framer_get(struct device *dev, const char *con_id) 185 + { 186 + return ERR_PTR(-ENOSYS); 187 + } 188 + 189 + void framer_put(struct device *dev, struct framer *framer) 190 + { 191 + } 192 + 193 + static inline struct framer *devm_framer_get(struct device *dev, const char *con_id) 194 + { 195 + return ERR_PTR(-ENOSYS); 196 + } 197 + 198 + static inline struct framer *devm_framer_optional_get(struct device *dev, const char *con_id) 199 + { 200 + return NULL; 201 + } 202 + 203 + #endif 204 + 205 + #endif /* __DRIVERS_FRAMER_H */