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