1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic Framer framework.
5 * Copyright 2023 CS GROUP France
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>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
21 static void framer_release(struct device *dev);
22 static const struct class framer_class = {
24 .dev_release = framer_release,
27 static DEFINE_MUTEX(framer_provider_mutex);
28 static LIST_HEAD(framer_provider_list);
29 static DEFINE_IDA(framer_ida);
31 #define dev_to_framer(a) (container_of((a), struct framer, dev))
33 int framer_pm_runtime_get(struct framer *framer)
37 if (!pm_runtime_enabled(&framer->dev))
40 ret = pm_runtime_get(&framer->dev);
41 if (ret < 0 && ret != -EINPROGRESS)
42 pm_runtime_put_noidle(&framer->dev);
46 EXPORT_SYMBOL_GPL(framer_pm_runtime_get);
48 int framer_pm_runtime_get_sync(struct framer *framer)
52 if (!pm_runtime_enabled(&framer->dev))
55 ret = pm_runtime_get_sync(&framer->dev);
57 pm_runtime_put_sync(&framer->dev);
61 EXPORT_SYMBOL_GPL(framer_pm_runtime_get_sync);
63 int framer_pm_runtime_put(struct framer *framer)
65 if (!pm_runtime_enabled(&framer->dev))
68 return pm_runtime_put(&framer->dev);
70 EXPORT_SYMBOL_GPL(framer_pm_runtime_put);
72 int framer_pm_runtime_put_sync(struct framer *framer)
74 if (!pm_runtime_enabled(&framer->dev))
77 return pm_runtime_put_sync(&framer->dev);
79 EXPORT_SYMBOL_GPL(framer_pm_runtime_put_sync);
82 * framer_init - framer internal initialization before framer operation
83 * @framer: the framer returned by framer_get()
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().
90 * Return: %0 if successful, a negative error code otherwise
92 int framer_init(struct framer *framer)
94 bool start_polling = false;
97 ret = framer_pm_runtime_get_sync(framer);
98 if (ret < 0 && ret != -EOPNOTSUPP)
100 ret = 0; /* Override possible ret == -EOPNOTSUPP */
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");
106 if (framer->init_count == 0) {
107 if (framer->ops->init) {
108 ret = framer->ops->init(framer);
110 dev_err(&framer->dev, "framer init failed --> %d\n", ret);
114 if (framer->ops->flags & FRAMER_FLAG_POLL_STATUS)
115 start_polling = true;
117 ++framer->init_count;
120 mutex_unlock(&framer->mutex);
122 if (!ret && start_polling) {
123 ret = framer_get_status(framer, &framer->prev_status);
125 dev_warn(&framer->dev, "framer get status failed --> %d\n", ret);
126 /* Will be retried on polling_work */
129 queue_delayed_work(system_power_efficient_wq, &framer->polling_work, 1 * HZ);
132 framer_pm_runtime_put(framer);
135 EXPORT_SYMBOL_GPL(framer_init);
138 * framer_exit - Framer internal un-initialization
139 * @framer: the framer returned by framer_get()
141 * Must be called after framer_power_off().
143 int framer_exit(struct framer *framer)
147 ret = framer_pm_runtime_get_sync(framer);
148 if (ret < 0 && ret != -EOPNOTSUPP)
150 ret = 0; /* Override possible ret == -EOPNOTSUPP */
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(&framer->mutex);
157 cancel_delayed_work_sync(&framer->polling_work);
158 mutex_lock(&framer->mutex);
161 if (framer->ops->exit)
162 framer->ops->exit(framer);
165 mutex_unlock(&framer->mutex);
166 framer_pm_runtime_put(framer);
169 EXPORT_SYMBOL_GPL(framer_exit);
172 * framer_power_on - Enable the framer and enter proper operation
173 * @framer: the framer returned by framer_get()
175 * Must be called after framer_init().
177 * Return: %0 if successful, a negative error code otherwise
179 int framer_power_on(struct framer *framer)
184 ret = regulator_enable(framer->pwr);
189 ret = framer_pm_runtime_get_sync(framer);
190 if (ret < 0 && ret != -EOPNOTSUPP)
193 mutex_lock(&framer->mutex);
194 if (framer->power_count == 0 && framer->ops->power_on) {
195 ret = framer->ops->power_on(framer);
197 dev_err(&framer->dev, "framer poweron failed --> %d\n", ret);
201 ++framer->power_count;
202 mutex_unlock(&framer->mutex);
206 mutex_unlock(&framer->mutex);
207 framer_pm_runtime_put_sync(framer);
210 regulator_disable(framer->pwr);
213 EXPORT_SYMBOL_GPL(framer_power_on);
216 * framer_power_off - Disable the framer.
217 * @framer: the framer returned by framer_get()
219 * Must be called before framer_exit().
221 * Return: %0 if successful, a negative error code otherwise
223 int framer_power_off(struct framer *framer)
227 mutex_lock(&framer->mutex);
228 if (framer->power_count == 1 && framer->ops->power_off) {
229 ret = framer->ops->power_off(framer);
231 dev_err(&framer->dev, "framer poweroff failed --> %d\n", ret);
232 mutex_unlock(&framer->mutex);
236 --framer->power_count;
237 mutex_unlock(&framer->mutex);
238 framer_pm_runtime_put(framer);
241 regulator_disable(framer->pwr);
245 EXPORT_SYMBOL_GPL(framer_power_off);
248 * framer_get_status() - Gets the framer status
249 * @framer: the framer returned by framer_get()
250 * @status: the status to retrieve
252 * Used to get the framer status. framer_init() must have been called
255 * Return: %0 if successful, a negative error code otherwise
257 int framer_get_status(struct framer *framer, struct framer_status *status)
261 if (!framer->ops->get_status)
264 /* Be sure to have known values (struct padding and future extensions) */
265 memset(status, 0, sizeof(*status));
267 mutex_lock(&framer->mutex);
268 ret = framer->ops->get_status(framer, status);
269 mutex_unlock(&framer->mutex);
273 EXPORT_SYMBOL_GPL(framer_get_status);
276 * framer_set_config() - Sets the framer configuration
277 * @framer: the framer returned by framer_get()
278 * @config: the configuration to set
280 * Used to set the framer configuration. framer_init() must have been called
283 * Return: %0 if successful, a negative error code otherwise
285 int framer_set_config(struct framer *framer, const struct framer_config *config)
289 if (!framer->ops->set_config)
292 mutex_lock(&framer->mutex);
293 ret = framer->ops->set_config(framer, config);
294 mutex_unlock(&framer->mutex);
298 EXPORT_SYMBOL_GPL(framer_set_config);
301 * framer_get_config() - Gets the framer configuration
302 * @framer: the framer returned by framer_get()
303 * @config: the configuration to retrieve
305 * Used to get the framer configuration. framer_init() must have been called
308 * Return: %0 if successful, a negative error code otherwise
310 int framer_get_config(struct framer *framer, struct framer_config *config)
314 if (!framer->ops->get_config)
317 mutex_lock(&framer->mutex);
318 ret = framer->ops->get_config(framer, config);
319 mutex_unlock(&framer->mutex);
323 EXPORT_SYMBOL_GPL(framer_get_config);
325 static void framer_polling_work(struct work_struct *work)
327 struct framer *framer = container_of(work, struct framer, polling_work.work);
328 struct framer_status status;
331 ret = framer_get_status(framer, &status);
333 dev_err(&framer->dev, "polling, get status failed (%d)\n", ret);
336 if (memcmp(&framer->prev_status, &status, sizeof(status))) {
337 blocking_notifier_call_chain(&framer->notifier_list,
338 FRAMER_EVENT_STATUS, NULL);
339 memcpy(&framer->prev_status, &status, sizeof(status));
343 /* Re-schedule task in 1 sec */
344 queue_delayed_work(system_power_efficient_wq, &framer->polling_work, 1 * HZ);
348 * framer_notifier_register() - Registers a notifier
349 * @framer: the framer returned by framer_get()
350 * @nb: the notifier block to register
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.
356 * Return: %0 if successful, a negative error code otherwise
358 int framer_notifier_register(struct framer *framer, struct notifier_block *nb)
360 return blocking_notifier_chain_register(&framer->notifier_list, nb);
362 EXPORT_SYMBOL_GPL(framer_notifier_register);
365 * framer_notifier_unregister() - Unregisters a notifier
366 * @framer: the framer returned by framer_get()
367 * @nb: the notifier block to unregister
369 * Used to unregister a notifier block. framer_init() must have
370 * been called on the framer.
372 * Return: %0 if successful, a negative error code otherwise
374 int framer_notifier_unregister(struct framer *framer, struct notifier_block *nb)
376 return blocking_notifier_chain_unregister(&framer->notifier_list, nb);
378 EXPORT_SYMBOL_GPL(framer_notifier_unregister);
380 static struct framer_provider *framer_provider_of_lookup(const struct device_node *node)
382 struct framer_provider *framer_provider;
384 list_for_each_entry(framer_provider, &framer_provider_list, list) {
385 if (device_match_of_node(framer_provider->dev, node))
386 return framer_provider;
389 return ERR_PTR(-EPROBE_DEFER);
392 static struct framer *framer_of_get_from_provider(const struct of_phandle_args *args)
394 struct framer_provider *framer_provider;
395 struct framer *framer;
397 mutex_lock(&framer_provider_mutex);
398 framer_provider = framer_provider_of_lookup(args->np);
399 if (IS_ERR(framer_provider) || !try_module_get(framer_provider->owner)) {
400 framer = ERR_PTR(-EPROBE_DEFER);
404 framer = framer_provider->of_xlate(framer_provider->dev, args);
406 module_put(framer_provider->owner);
409 mutex_unlock(&framer_provider_mutex);
414 static struct framer *framer_of_get_byphandle(struct device_node *np, const char *propname,
417 struct of_phandle_args args;
418 struct framer *framer;
421 ret = of_parse_phandle_with_optional_args(np, propname, "#framer-cells", index, &args);
423 return ERR_PTR(-ENODEV);
425 if (!of_device_is_available(args.np)) {
426 framer = ERR_PTR(-ENODEV);
430 framer = framer_of_get_from_provider(&args);
433 of_node_put(args.np);
438 static struct framer *framer_of_get_byparent(struct device_node *np, int index)
440 struct of_phandle_args args;
441 struct framer *framer;
443 args.np = of_get_parent(np);
445 args.args[0] = index;
448 framer = framer_of_get_from_provider(&args);
449 if (IS_ERR(framer) && PTR_ERR(framer) != -EPROBE_DEFER) {
450 args.np = of_get_next_parent(args.np);
453 of_node_put(args.np);
457 return ERR_PTR(-ENODEV);
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
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.
469 struct framer *framer_get(struct device *dev, const char *con_id)
471 struct framer *framer = ERR_PTR(-ENODEV);
472 struct device_link *link;
477 framer = framer_of_get_byphandle(dev->of_node, con_id, 0);
479 framer = framer_of_get_byparent(dev->of_node, 0);
485 get_device(&framer->dev);
487 if (!try_module_get(framer->ops->owner)) {
492 link = device_link_add(dev, &framer->dev, DL_FLAG_STATELESS);
494 dev_err(dev, "failed to create device_link to %s\n", dev_name(&framer->dev));
502 module_put(framer->ops->owner);
504 put_device(&framer->dev);
507 EXPORT_SYMBOL_GPL(framer_get);
510 * framer_put() - release the framer
511 * @dev: device that wants to release this framer
512 * @framer: the framer returned by framer_get()
514 * Releases a refcount the caller received from framer_get().
516 void framer_put(struct device *dev, struct framer *framer)
518 device_link_remove(dev, &framer->dev);
520 module_put(framer->ops->owner);
521 put_device(&framer->dev);
523 EXPORT_SYMBOL_GPL(framer_put);
525 static void devm_framer_put(struct device *dev, void *res)
527 struct framer *framer = *(struct framer **)res;
529 framer_put(dev, framer);
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
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.
541 struct framer *devm_framer_get(struct device *dev, const char *con_id)
543 struct framer **ptr, *framer;
545 ptr = devres_alloc(devm_framer_put, sizeof(*ptr), GFP_KERNEL);
547 return ERR_PTR(-ENOMEM);
549 framer = framer_get(dev, con_id);
550 if (!IS_ERR(framer)) {
552 devres_add(dev, ptr);
560 EXPORT_SYMBOL_GPL(devm_framer_get);
563 * devm_framer_optional_get() - lookup and obtain a reference to an optional
565 * @dev: device that requests this framer
566 * @con_id: name of the framer from device's point of view
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
572 struct framer *devm_framer_optional_get(struct device *dev, const char *con_id)
574 struct framer *framer = devm_framer_get(dev, con_id);
576 if (PTR_ERR(framer) == -ENODEV)
581 EXPORT_SYMBOL_GPL(devm_framer_optional_get);
583 static void framer_notify_status_work(struct work_struct *work)
585 struct framer *framer = container_of(work, struct framer, notify_status_work);
587 blocking_notifier_call_chain(&framer->notifier_list, FRAMER_EVENT_STATUS, NULL);
590 void framer_notify_status_change(struct framer *framer)
592 /* Can be called from atomic context -> just schedule a task to call
595 queue_work(system_power_efficient_wq, &framer->notify_status_work);
597 EXPORT_SYMBOL_GPL(framer_notify_status_change);
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
605 * Called to create a framer using framer framework.
607 struct framer *framer_create(struct device *dev, struct device_node *node,
608 const struct framer_ops *ops)
610 struct framer *framer;
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(-EINVAL);
618 framer = kzalloc(sizeof(*framer), GFP_KERNEL);
620 return ERR_PTR(-ENOMEM);
622 id = ida_alloc(&framer_ida, GFP_KERNEL);
624 dev_err(dev, "unable to get id\n");
629 device_initialize(&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);
635 framer->dev.class = &framer_class;
636 framer->dev.parent = dev;
637 framer->dev.of_node = node ? node : dev->of_node;
641 ret = dev_set_name(&framer->dev, "framer-%s.%d", dev_name(dev), id);
646 framer->pwr = regulator_get_optional(&framer->dev, "framer");
647 if (IS_ERR(framer->pwr)) {
648 ret = PTR_ERR(framer->pwr);
649 if (ret == -EPROBE_DEFER)
655 ret = device_add(&framer->dev);
659 if (pm_runtime_enabled(dev)) {
660 pm_runtime_enable(&framer->dev);
661 pm_runtime_no_callbacks(&framer->dev);
667 put_device(&framer->dev); /* calls framer_release() which frees resources */
674 EXPORT_SYMBOL_GPL(framer_create);
677 * framer_destroy() - destroy the framer
678 * @framer: the framer to be destroyed
680 * Called to destroy the framer.
682 void framer_destroy(struct framer *framer)
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 ...
687 cancel_delayed_work_sync(&framer->polling_work);
688 cancel_work_sync(&framer->notify_status_work);
689 pm_runtime_disable(&framer->dev);
690 device_unregister(&framer->dev); /* calls framer_release() which frees resources */
692 EXPORT_SYMBOL_GPL(framer_destroy);
694 static void devm_framer_destroy(struct device *dev, void *res)
696 struct framer *framer = *(struct framer **)res;
698 framer_destroy(framer);
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
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.
712 struct framer *devm_framer_create(struct device *dev, struct device_node *node,
713 const struct framer_ops *ops)
715 struct framer **ptr, *framer;
717 ptr = devres_alloc(devm_framer_destroy, sizeof(*ptr), GFP_KERNEL);
719 return ERR_PTR(-ENOMEM);
721 framer = framer_create(dev, node, ops);
722 if (!IS_ERR(framer)) {
724 devres_add(dev, ptr);
731 EXPORT_SYMBOL_GPL(devm_framer_create);
734 * framer_provider_simple_of_xlate() - returns the framer instance from framer provider
735 * @dev: the framer provider device
736 * @args: of_phandle_args (not used here)
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.
743 struct framer *framer_provider_simple_of_xlate(struct device *dev,
744 const struct of_phandle_args *args)
746 struct class_dev_iter iter;
747 struct framer *framer;
749 class_dev_iter_init(&iter, &framer_class, NULL, NULL);
750 while ((dev = class_dev_iter_next(&iter))) {
751 framer = dev_to_framer(dev);
752 if (args->np != framer->dev.of_node)
755 class_dev_iter_exit(&iter);
759 class_dev_iter_exit(&iter);
760 return ERR_PTR(-ENODEV);
762 EXPORT_SYMBOL_GPL(framer_provider_simple_of_xlate);
765 * __framer_provider_of_register() - create/register framer provider with the framework
766 * @dev: struct device of the framer provider
767 * @owner: the module owner containing of_xlate
768 * @of_xlate: function pointer to obtain framer instance from framer provider
770 * Creates struct framer_provider from dev and of_xlate function pointer.
771 * This is used in the case of dt boot for finding the framer instance from
774 struct framer_provider *
775 __framer_provider_of_register(struct device *dev, struct module *owner,
776 struct framer *(*of_xlate)(struct device *dev,
777 const struct of_phandle_args *args))
779 struct framer_provider *framer_provider;
781 framer_provider = kzalloc(sizeof(*framer_provider), GFP_KERNEL);
782 if (!framer_provider)
783 return ERR_PTR(-ENOMEM);
785 framer_provider->dev = dev;
786 framer_provider->owner = owner;
787 framer_provider->of_xlate = of_xlate;
789 of_node_get(framer_provider->dev->of_node);
791 mutex_lock(&framer_provider_mutex);
792 list_add_tail(&framer_provider->list, &framer_provider_list);
793 mutex_unlock(&framer_provider_mutex);
795 return framer_provider;
797 EXPORT_SYMBOL_GPL(__framer_provider_of_register);
800 * framer_provider_of_unregister() - unregister framer provider from the framework
801 * @framer_provider: framer provider returned by framer_provider_of_register()
803 * Removes the framer_provider created using framer_provider_of_register().
805 void framer_provider_of_unregister(struct framer_provider *framer_provider)
807 mutex_lock(&framer_provider_mutex);
808 list_del(&framer_provider->list);
809 mutex_unlock(&framer_provider_mutex);
811 of_node_put(framer_provider->dev->of_node);
812 kfree(framer_provider);
814 EXPORT_SYMBOL_GPL(framer_provider_of_unregister);
816 static void devm_framer_provider_of_unregister(struct device *dev, void *res)
818 struct framer_provider *framer_provider = *(struct framer_provider **)res;
820 framer_provider_of_unregister(framer_provider);
824 * __devm_framer_provider_of_register() - create/register framer provider with
826 * @dev: struct device of the framer provider
827 * @owner: the module owner containing of_xlate
828 * @of_xlate: function pointer to obtain framer instance from framer provider
830 * Creates struct framer_provider from dev and of_xlate function pointer.
831 * This is used in the case of dt boot for finding the framer instance from
832 * framer provider. While at that, it also associates the device with the
833 * framer provider using devres. On driver detach, release function is invoked
834 * on the devres data, then, devres data is freed.
836 struct framer_provider *
837 __devm_framer_provider_of_register(struct device *dev, struct module *owner,
838 struct framer *(*of_xlate)(struct device *dev,
839 const struct of_phandle_args *args))
841 struct framer_provider **ptr, *framer_provider;
843 ptr = devres_alloc(devm_framer_provider_of_unregister, sizeof(*ptr), GFP_KERNEL);
845 return ERR_PTR(-ENOMEM);
847 framer_provider = __framer_provider_of_register(dev, owner, of_xlate);
848 if (!IS_ERR(framer_provider)) {
849 *ptr = framer_provider;
850 devres_add(dev, ptr);
855 return framer_provider;
857 EXPORT_SYMBOL_GPL(__devm_framer_provider_of_register);
860 * framer_release() - release the framer
861 * @dev: the dev member within framer
863 * When the last reference to the device is removed, it is called
864 * from the embedded kobject as release method.
866 static void framer_release(struct device *dev)
868 struct framer *framer;
870 framer = dev_to_framer(dev);
871 regulator_put(framer->pwr);
872 ida_free(&framer_ida, framer->id);
876 static int __init framer_core_init(void)
878 return class_register(&framer_class);
880 device_initcall(framer_core_init);