// SPDX-License-Identifier: GPL-2.01/*2* drivers/base/power/common.c - Common device power management code.3*4* Copyright (C) 2011 Rafael J. Wysocki <[email protected]>, Renesas Electronics Corp.5*/6#include <linux/kernel.h>7#include <linux/device.h>8#include <linux/export.h>9#include <linux/slab.h>10#include <linux/pm_clock.h>11#include <linux/acpi.h>12#include <linux/pm_domain.h>13#include <linux/pm_opp.h>1415#include "power.h"1617/**18* dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.19* @dev: Device to handle.20*21* If power.subsys_data is NULL, point it to a new object, otherwise increment22* its reference counter. Return 0 if new object has been created or refcount23* increased, otherwise negative error code.24*/25int dev_pm_get_subsys_data(struct device *dev)26{27struct pm_subsys_data *psd;2829psd = kzalloc(sizeof(*psd), GFP_KERNEL);30if (!psd)31return -ENOMEM;3233spin_lock_irq(&dev->power.lock);3435if (dev->power.subsys_data) {36dev->power.subsys_data->refcount++;37} else {38spin_lock_init(&psd->lock);39psd->refcount = 1;40dev->power.subsys_data = psd;41pm_clk_init(dev);42psd = NULL;43}4445spin_unlock_irq(&dev->power.lock);4647/* kfree() verifies that its argument is nonzero. */48kfree(psd);4950return 0;51}52EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);5354/**55* dev_pm_put_subsys_data - Drop reference to power.subsys_data.56* @dev: Device to handle.57*58* If the reference counter of power.subsys_data is zero after dropping the59* reference, power.subsys_data is removed.60*/61void dev_pm_put_subsys_data(struct device *dev)62{63struct pm_subsys_data *psd;6465spin_lock_irq(&dev->power.lock);6667psd = dev_to_psd(dev);68if (!psd)69goto out;7071if (--psd->refcount == 0)72dev->power.subsys_data = NULL;73else74psd = NULL;7576out:77spin_unlock_irq(&dev->power.lock);78kfree(psd);79}80EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);8182/**83* dev_pm_domain_attach - Attach a device to its PM domain.84* @dev: Device to attach.85* @flags: indicate whether we should power on/off the device on attach/detach86*87* The @dev may only be attached to a single PM domain. By iterating through88* the available alternatives we try to find a valid PM domain for the device.89* As attachment succeeds, the ->detach() callback in the struct dev_pm_domain90* should be assigned by the corresponding attach function.91*92* This function should typically be invoked from subsystem level code during93* the probe phase. Especially for those that holds devices which requires94* power management through PM domains.95*96* Callers must ensure proper synchronization of this function with power97* management callbacks.98*99* Returns 0 on successfully attached PM domain, or when it is found that the100* device doesn't need a PM domain, else a negative error code.101*/102int dev_pm_domain_attach(struct device *dev, u32 flags)103{104int ret;105106if (dev->pm_domain)107return 0;108109ret = acpi_dev_pm_attach(dev, !!(flags & PD_FLAG_ATTACH_POWER_ON));110if (!ret)111ret = genpd_dev_pm_attach(dev);112113if (dev->pm_domain)114dev->power.detach_power_off = !!(flags & PD_FLAG_DETACH_POWER_OFF);115116return ret < 0 ? ret : 0;117}118EXPORT_SYMBOL_GPL(dev_pm_domain_attach);119120/**121* dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.122* @dev: The device used to lookup the PM domain.123* @index: The index of the PM domain.124*125* As @dev may only be attached to a single PM domain, the backend PM domain126* provider creates a virtual device to attach instead. If attachment succeeds,127* the ->detach() callback in the struct dev_pm_domain are assigned by the128* corresponding backend attach function, as to deal with detaching of the129* created virtual device.130*131* This function should typically be invoked by a driver during the probe phase,132* in case its device requires power management through multiple PM domains. The133* driver may benefit from using the received device, to configure device-links134* towards its original device. Depending on the use-case and if needed, the135* links may be dynamically changed by the driver, which allows it to control136* the power to the PM domains independently from each other.137*138* Callers must ensure proper synchronization of this function with power139* management callbacks.140*141* Returns the virtual created device when successfully attached to its PM142* domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().143* Note that, to detach the returned virtual device, the driver shall call144* dev_pm_domain_detach() on it, typically during the remove phase.145*/146struct device *dev_pm_domain_attach_by_id(struct device *dev,147unsigned int index)148{149if (dev->pm_domain)150return ERR_PTR(-EEXIST);151152return genpd_dev_pm_attach_by_id(dev, index);153}154EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);155156/**157* dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.158* @dev: The device used to lookup the PM domain.159* @name: The name of the PM domain.160*161* For a detailed function description, see dev_pm_domain_attach_by_id().162*/163struct device *dev_pm_domain_attach_by_name(struct device *dev,164const char *name)165{166if (dev->pm_domain)167return ERR_PTR(-EEXIST);168169return genpd_dev_pm_attach_by_name(dev, name);170}171EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);172173/**174* dev_pm_domain_attach_list - Associate a device with its PM domains.175* @dev: The device used to lookup the PM domains for.176* @data: The data used for attaching to the PM domains.177* @list: An out-parameter with an allocated list of attached PM domains.178*179* This function helps to attach a device to its multiple PM domains. The180* caller, which is typically a driver's probe function, may provide a list of181* names for the PM domains that we should try to attach the device to, but it182* may also provide an empty list, in case the attach should be done for all of183* the available PM domains.184*185* Callers must ensure proper synchronization of this function with power186* management callbacks.187*188* Returns the number of attached PM domains or a negative error code in case of189* a failure. Note that, to detach the list of PM domains, the driver shall call190* dev_pm_domain_detach_list(), typically during the remove phase.191*/192int dev_pm_domain_attach_list(struct device *dev,193const struct dev_pm_domain_attach_data *data,194struct dev_pm_domain_list **list)195{196struct device_node *np = dev->of_node;197struct dev_pm_domain_list *pds;198struct device *pd_dev = NULL;199int ret, i, num_pds = 0;200bool by_id = true;201size_t size;202u32 pd_flags = data ? data->pd_flags : 0;203u32 link_flags = pd_flags & PD_FLAG_NO_DEV_LINK ? 0 :204DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME;205206if (dev->pm_domain)207return -EEXIST;208209/* For now this is limited to OF based platforms. */210if (!np)211return 0;212213if (data && data->pd_names) {214num_pds = data->num_pd_names;215by_id = false;216} else {217num_pds = of_count_phandle_with_args(np, "power-domains",218"#power-domain-cells");219}220221if (num_pds <= 0)222return 0;223224pds = kzalloc(sizeof(*pds), GFP_KERNEL);225if (!pds)226return -ENOMEM;227228size = sizeof(*pds->pd_devs) + sizeof(*pds->pd_links) +229sizeof(*pds->opp_tokens);230pds->pd_devs = kcalloc(num_pds, size, GFP_KERNEL);231if (!pds->pd_devs) {232ret = -ENOMEM;233goto free_pds;234}235pds->pd_links = (void *)(pds->pd_devs + num_pds);236pds->opp_tokens = (void *)(pds->pd_links + num_pds);237238if (link_flags && pd_flags & PD_FLAG_DEV_LINK_ON)239link_flags |= DL_FLAG_RPM_ACTIVE;240241for (i = 0; i < num_pds; i++) {242if (by_id)243pd_dev = dev_pm_domain_attach_by_id(dev, i);244else245pd_dev = dev_pm_domain_attach_by_name(dev,246data->pd_names[i]);247if (IS_ERR_OR_NULL(pd_dev)) {248ret = pd_dev ? PTR_ERR(pd_dev) : -ENODEV;249goto err_attach;250}251252if (pd_flags & PD_FLAG_REQUIRED_OPP) {253struct dev_pm_opp_config config = {254.required_dev = pd_dev,255.required_dev_index = i,256};257258ret = dev_pm_opp_set_config(dev, &config);259if (ret < 0)260goto err_link;261262pds->opp_tokens[i] = ret;263}264265if (link_flags) {266struct device_link *link;267268link = device_link_add(dev, pd_dev, link_flags);269if (!link) {270ret = -ENODEV;271goto err_link;272}273274pds->pd_links[i] = link;275}276277pds->pd_devs[i] = pd_dev;278}279280pds->num_pds = num_pds;281*list = pds;282return num_pds;283284err_link:285dev_pm_opp_clear_config(pds->opp_tokens[i]);286dev_pm_domain_detach(pd_dev, true);287err_attach:288while (--i >= 0) {289dev_pm_opp_clear_config(pds->opp_tokens[i]);290if (pds->pd_links[i])291device_link_del(pds->pd_links[i]);292dev_pm_domain_detach(pds->pd_devs[i], true);293}294kfree(pds->pd_devs);295free_pds:296kfree(pds);297return ret;298}299EXPORT_SYMBOL_GPL(dev_pm_domain_attach_list);300301/**302* devm_pm_domain_detach_list - devres-enabled version of dev_pm_domain_detach_list.303* @_list: The list of PM domains to detach.304*305* This function reverse the actions from devm_pm_domain_attach_list().306* it will be invoked during the remove phase from drivers implicitly if driver307* uses devm_pm_domain_attach_list() to attach the PM domains.308*/309static void devm_pm_domain_detach_list(void *_list)310{311struct dev_pm_domain_list *list = _list;312313dev_pm_domain_detach_list(list);314}315316/**317* devm_pm_domain_attach_list - devres-enabled version of dev_pm_domain_attach_list318* @dev: The device used to lookup the PM domains for.319* @data: The data used for attaching to the PM domains.320* @list: An out-parameter with an allocated list of attached PM domains.321*322* NOTE: this will also handle calling devm_pm_domain_detach_list() for323* you during remove phase.324*325* Returns the number of attached PM domains or a negative error code in case of326* a failure.327*/328int devm_pm_domain_attach_list(struct device *dev,329const struct dev_pm_domain_attach_data *data,330struct dev_pm_domain_list **list)331{332int ret, num_pds;333334num_pds = dev_pm_domain_attach_list(dev, data, list);335if (num_pds <= 0)336return num_pds;337338ret = devm_add_action_or_reset(dev, devm_pm_domain_detach_list, *list);339if (ret)340return ret;341342return num_pds;343}344EXPORT_SYMBOL_GPL(devm_pm_domain_attach_list);345346/**347* dev_pm_domain_detach - Detach a device from its PM domain.348* @dev: Device to detach.349* @power_off: Used to indicate whether we should power off the device.350*351* This functions will reverse the actions from dev_pm_domain_attach(),352* dev_pm_domain_attach_by_id() and dev_pm_domain_attach_by_name(), thus it353* detaches @dev from its PM domain. Typically it should be invoked during the354* remove phase, either from subsystem level code or from drivers.355*356* Callers must ensure proper synchronization of this function with power357* management callbacks.358*/359void dev_pm_domain_detach(struct device *dev, bool power_off)360{361if (dev->pm_domain && dev->pm_domain->detach)362dev->pm_domain->detach(dev, power_off);363}364EXPORT_SYMBOL_GPL(dev_pm_domain_detach);365366/**367* dev_pm_domain_detach_list - Detach a list of PM domains.368* @list: The list of PM domains to detach.369*370* This function reverse the actions from dev_pm_domain_attach_list().371* Typically it should be invoked during the remove phase from drivers.372*373* Callers must ensure proper synchronization of this function with power374* management callbacks.375*/376void dev_pm_domain_detach_list(struct dev_pm_domain_list *list)377{378int i;379380if (!list)381return;382383for (i = 0; i < list->num_pds; i++) {384dev_pm_opp_clear_config(list->opp_tokens[i]);385if (list->pd_links[i])386device_link_del(list->pd_links[i]);387dev_pm_domain_detach(list->pd_devs[i], true);388}389390kfree(list->pd_devs);391kfree(list);392}393EXPORT_SYMBOL_GPL(dev_pm_domain_detach_list);394395/**396* dev_pm_domain_start - Start the device through its PM domain.397* @dev: Device to start.398*399* This function should typically be called during probe by a subsystem/driver,400* when it needs to start its device from the PM domain's perspective. Note401* that, it's assumed that the PM domain is already powered on when this402* function is called.403*404* Returns 0 on success and negative error values on failures.405*/406int dev_pm_domain_start(struct device *dev)407{408if (dev->pm_domain && dev->pm_domain->start)409return dev->pm_domain->start(dev);410411return 0;412}413EXPORT_SYMBOL_GPL(dev_pm_domain_start);414415/**416* dev_pm_domain_set - Set PM domain of a device.417* @dev: Device whose PM domain is to be set.418* @pd: PM domain to be set, or NULL.419*420* Sets the PM domain the device belongs to. The PM domain of a device needs421* to be set before its probe finishes (it's bound to a driver).422*423* This function must be called with the device lock held.424*/425void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)426{427if (dev->pm_domain == pd)428return;429430WARN(pd && device_is_bound(dev),431"PM domains can only be changed for unbound devices\n");432dev->pm_domain = pd;433device_pm_check_callbacks(dev);434}435EXPORT_SYMBOL_GPL(dev_pm_domain_set);436437/**438* dev_pm_domain_set_performance_state - Request a new performance state.439* @dev: The device to make the request for.440* @state: Target performance state for the device.441*442* This function should be called when a new performance state needs to be443* requested for a device that is attached to a PM domain. Note that, the444* support for performance scaling for PM domains is optional.445*446* Returns 0 on success and when performance scaling isn't supported, negative447* error code on failure.448*/449int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state)450{451if (dev->pm_domain && dev->pm_domain->set_performance_state)452return dev->pm_domain->set_performance_state(dev, state);453454return 0;455}456EXPORT_SYMBOL_GPL(dev_pm_domain_set_performance_state);457458459