LCOV - code coverage report
Current view: top level - drivers/base/power - common.c (source / functions) Hit Total Coverage
Test: Real Lines: 22 36 61.1 %
Date: 2020-10-17 15:46:43 Functions: 0 7 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  * drivers/base/power/common.c - Common device power management code.
       4                 :            :  *
       5                 :            :  * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
       6                 :            :  */
       7                 :            : #include <linux/kernel.h>
       8                 :            : #include <linux/device.h>
       9                 :            : #include <linux/export.h>
      10                 :            : #include <linux/slab.h>
      11                 :            : #include <linux/pm_clock.h>
      12                 :            : #include <linux/acpi.h>
      13                 :            : #include <linux/pm_domain.h>
      14                 :            : 
      15                 :            : #include "power.h"
      16                 :            : 
      17                 :            : /**
      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 increment
      22                 :            :  * its reference counter.  Return 0 if new object has been created or refcount
      23                 :            :  * increased, otherwise negative error code.
      24                 :            :  */
      25                 :          3 : int dev_pm_get_subsys_data(struct device *dev)
      26                 :            : {
      27                 :            :         struct pm_subsys_data *psd;
      28                 :            : 
      29                 :          3 :         psd = kzalloc(sizeof(*psd), GFP_KERNEL);
      30                 :          3 :         if (!psd)
      31                 :            :                 return -ENOMEM;
      32                 :            : 
      33                 :            :         spin_lock_irq(&dev->power.lock);
      34                 :            : 
      35                 :          3 :         if (dev->power.subsys_data) {
      36                 :          0 :                 dev->power.subsys_data->refcount++;
      37                 :            :         } else {
      38                 :          3 :                 spin_lock_init(&psd->lock);
      39                 :          3 :                 psd->refcount = 1;
      40                 :          3 :                 dev->power.subsys_data = psd;
      41                 :          3 :                 pm_clk_init(dev);
      42                 :            :                 psd = NULL;
      43                 :            :         }
      44                 :            : 
      45                 :            :         spin_unlock_irq(&dev->power.lock);
      46                 :            : 
      47                 :            :         /* kfree() verifies that its argument is nonzero. */
      48                 :          3 :         kfree(psd);
      49                 :            : 
      50                 :          3 :         return 0;
      51                 :            : }
      52                 :            : EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
      53                 :            : 
      54                 :            : /**
      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 the
      59                 :            :  * reference, power.subsys_data is removed.
      60                 :            :  */
      61                 :          0 : void dev_pm_put_subsys_data(struct device *dev)
      62                 :            : {
      63                 :            :         struct pm_subsys_data *psd;
      64                 :            : 
      65                 :            :         spin_lock_irq(&dev->power.lock);
      66                 :            : 
      67                 :            :         psd = dev_to_psd(dev);
      68                 :          0 :         if (!psd)
      69                 :            :                 goto out;
      70                 :            : 
      71                 :          0 :         if (--psd->refcount == 0)
      72                 :          0 :                 dev->power.subsys_data = NULL;
      73                 :            :         else
      74                 :            :                 psd = NULL;
      75                 :            : 
      76                 :            :  out:
      77                 :            :         spin_unlock_irq(&dev->power.lock);
      78                 :          0 :         kfree(psd);
      79                 :          0 : }
      80                 :            : EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
      81                 :            : 
      82                 :            : /**
      83                 :            :  * dev_pm_domain_attach - Attach a device to its PM domain.
      84                 :            :  * @dev: Device to attach.
      85                 :            :  * @power_on: Used to indicate whether we should power on the device.
      86                 :            :  *
      87                 :            :  * The @dev may only be attached to a single PM domain. By iterating through
      88                 :            :  * 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_domain
      90                 :            :  * should be assigned by the corresponding attach function.
      91                 :            :  *
      92                 :            :  * This function should typically be invoked from subsystem level code during
      93                 :            :  * the probe phase. Especially for those that holds devices which requires
      94                 :            :  * power management through PM domains.
      95                 :            :  *
      96                 :            :  * Callers must ensure proper synchronization of this function with power
      97                 :            :  * management callbacks.
      98                 :            :  *
      99                 :            :  * Returns 0 on successfully attached PM domain, or when it is found that the
     100                 :            :  * device doesn't need a PM domain, else a negative error code.
     101                 :            :  */
     102                 :          3 : int dev_pm_domain_attach(struct device *dev, bool power_on)
     103                 :            : {
     104                 :            :         int ret;
     105                 :            : 
     106                 :          3 :         if (dev->pm_domain)
     107                 :            :                 return 0;
     108                 :            : 
     109                 :            :         ret = acpi_dev_pm_attach(dev, power_on);
     110                 :            :         if (!ret)
     111                 :          3 :                 ret = genpd_dev_pm_attach(dev);
     112                 :            : 
     113                 :          3 :         return ret < 0 ? ret : 0;
     114                 :            : }
     115                 :            : EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
     116                 :            : 
     117                 :            : /**
     118                 :            :  * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.
     119                 :            :  * @dev: The device used to lookup the PM domain.
     120                 :            :  * @index: The index of the PM domain.
     121                 :            :  *
     122                 :            :  * As @dev may only be attached to a single PM domain, the backend PM domain
     123                 :            :  * provider creates a virtual device to attach instead. If attachment succeeds,
     124                 :            :  * the ->detach() callback in the struct dev_pm_domain are assigned by the
     125                 :            :  * corresponding backend attach function, as to deal with detaching of the
     126                 :            :  * created virtual device.
     127                 :            :  *
     128                 :            :  * This function should typically be invoked by a driver during the probe phase,
     129                 :            :  * in case its device requires power management through multiple PM domains. The
     130                 :            :  * driver may benefit from using the received device, to configure device-links
     131                 :            :  * towards its original device. Depending on the use-case and if needed, the
     132                 :            :  * links may be dynamically changed by the driver, which allows it to control
     133                 :            :  * the power to the PM domains independently from each other.
     134                 :            :  *
     135                 :            :  * Callers must ensure proper synchronization of this function with power
     136                 :            :  * management callbacks.
     137                 :            :  *
     138                 :            :  * Returns the virtual created device when successfully attached to its PM
     139                 :            :  * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().
     140                 :            :  * Note that, to detach the returned virtual device, the driver shall call
     141                 :            :  * dev_pm_domain_detach() on it, typically during the remove phase.
     142                 :            :  */
     143                 :          0 : struct device *dev_pm_domain_attach_by_id(struct device *dev,
     144                 :            :                                           unsigned int index)
     145                 :            : {
     146                 :          0 :         if (dev->pm_domain)
     147                 :            :                 return ERR_PTR(-EEXIST);
     148                 :            : 
     149                 :          0 :         return genpd_dev_pm_attach_by_id(dev, index);
     150                 :            : }
     151                 :            : EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);
     152                 :            : 
     153                 :            : /**
     154                 :            :  * dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.
     155                 :            :  * @dev: The device used to lookup the PM domain.
     156                 :            :  * @name: The name of the PM domain.
     157                 :            :  *
     158                 :            :  * For a detailed function description, see dev_pm_domain_attach_by_id().
     159                 :            :  */
     160                 :          0 : struct device *dev_pm_domain_attach_by_name(struct device *dev,
     161                 :            :                                             const char *name)
     162                 :            : {
     163                 :          0 :         if (dev->pm_domain)
     164                 :            :                 return ERR_PTR(-EEXIST);
     165                 :            : 
     166                 :          0 :         return genpd_dev_pm_attach_by_name(dev, name);
     167                 :            : }
     168                 :            : EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);
     169                 :            : 
     170                 :            : /**
     171                 :            :  * dev_pm_domain_detach - Detach a device from its PM domain.
     172                 :            :  * @dev: Device to detach.
     173                 :            :  * @power_off: Used to indicate whether we should power off the device.
     174                 :            :  *
     175                 :            :  * This functions will reverse the actions from dev_pm_domain_attach() and
     176                 :            :  * dev_pm_domain_attach_by_id(), thus it detaches @dev from its PM domain.
     177                 :            :  * Typically it should be invoked during the remove phase, either from
     178                 :            :  * subsystem level code or from drivers.
     179                 :            :  *
     180                 :            :  * Callers must ensure proper synchronization of this function with power
     181                 :            :  * management callbacks.
     182                 :            :  */
     183                 :          3 : void dev_pm_domain_detach(struct device *dev, bool power_off)
     184                 :            : {
     185                 :          3 :         if (dev->pm_domain && dev->pm_domain->detach)
     186                 :          0 :                 dev->pm_domain->detach(dev, power_off);
     187                 :          3 : }
     188                 :            : EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
     189                 :            : 
     190                 :            : /**
     191                 :            :  * dev_pm_domain_set - Set PM domain of a device.
     192                 :            :  * @dev: Device whose PM domain is to be set.
     193                 :            :  * @pd: PM domain to be set, or NULL.
     194                 :            :  *
     195                 :            :  * Sets the PM domain the device belongs to. The PM domain of a device needs
     196                 :            :  * to be set before its probe finishes (it's bound to a driver).
     197                 :            :  *
     198                 :            :  * This function must be called with the device lock held.
     199                 :            :  */
     200                 :          3 : void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
     201                 :            : {
     202                 :          3 :         if (dev->pm_domain == pd)
     203                 :          3 :                 return;
     204                 :            : 
     205                 :          3 :         WARN(pd && device_is_bound(dev),
     206                 :            :              "PM domains can only be changed for unbound devices\n");
     207                 :          3 :         dev->pm_domain = pd;
     208                 :            :         device_pm_check_callbacks(dev);
     209                 :            : }
     210                 :            : EXPORT_SYMBOL_GPL(dev_pm_domain_set);
    

Generated by: LCOV version 1.14