Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/common.c - Common device power management code. |
| 3 | * |
| 4 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp. |
| 5 | * |
| 6 | * This file is released under the GPLv2. |
| 7 | */ |
| 8 | |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 9 | #include <linux/kernel.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 10 | #include <linux/device.h> |
Paul Gortmaker | aaf1954 | 2011-09-28 18:23:03 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 12 | #include <linux/slab.h> |
Rafael J. Wysocki | b5e8d26 | 2011-08-25 15:34:19 +0200 | [diff] [blame] | 13 | #include <linux/pm_clock.h> |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 14 | #include <linux/acpi.h> |
| 15 | #include <linux/pm_domain.h> |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 16 | |
Tomeu Vizoso | aa8e54b5 | 2016-01-07 16:46:14 +0100 | [diff] [blame] | 17 | #include "power.h" |
| 18 | |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 19 | /** |
| 20 | * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device. |
| 21 | * @dev: Device to handle. |
| 22 | * |
| 23 | * If power.subsys_data is NULL, point it to a new object, otherwise increment |
Ulf Hansson | 766bb53 | 2015-01-29 18:39:04 +0100 | [diff] [blame] | 24 | * its reference counter. Return 0 if new object has been created or refcount |
| 25 | * increased, otherwise negative error code. |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 26 | */ |
| 27 | int dev_pm_get_subsys_data(struct device *dev) |
| 28 | { |
| 29 | struct pm_subsys_data *psd; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 30 | |
| 31 | psd = kzalloc(sizeof(*psd), GFP_KERNEL); |
| 32 | if (!psd) |
| 33 | return -ENOMEM; |
| 34 | |
| 35 | spin_lock_irq(&dev->power.lock); |
| 36 | |
| 37 | if (dev->power.subsys_data) { |
| 38 | dev->power.subsys_data->refcount++; |
| 39 | } else { |
| 40 | spin_lock_init(&psd->lock); |
| 41 | psd->refcount = 1; |
| 42 | dev->power.subsys_data = psd; |
| 43 | pm_clk_init(dev); |
| 44 | psd = NULL; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | spin_unlock_irq(&dev->power.lock); |
| 48 | |
| 49 | /* kfree() verifies that its argument is nonzero. */ |
| 50 | kfree(psd); |
| 51 | |
Rafael J. Wysocki | 7725495 | 2012-08-07 13:50:14 +0200 | [diff] [blame] | 52 | return 0; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 53 | } |
| 54 | EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data); |
| 55 | |
| 56 | /** |
| 57 | * dev_pm_put_subsys_data - Drop reference to power.subsys_data. |
| 58 | * @dev: Device to handle. |
| 59 | * |
| 60 | * If the reference counter of power.subsys_data is zero after dropping the |
Ulf Hansson | 1e95e3b | 2015-01-29 18:39:05 +0100 | [diff] [blame] | 61 | * reference, power.subsys_data is removed. |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 62 | */ |
Ulf Hansson | 1e95e3b | 2015-01-29 18:39:05 +0100 | [diff] [blame] | 63 | void dev_pm_put_subsys_data(struct device *dev) |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 64 | { |
| 65 | struct pm_subsys_data *psd; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 66 | |
| 67 | spin_lock_irq(&dev->power.lock); |
| 68 | |
| 69 | psd = dev_to_psd(dev); |
Shuah Khan | d5e1670a | 2013-05-08 01:14:32 +0200 | [diff] [blame] | 70 | if (!psd) |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 71 | goto out; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 72 | |
Ulf Hansson | 1e95e3b | 2015-01-29 18:39:05 +0100 | [diff] [blame] | 73 | if (--psd->refcount == 0) |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 74 | dev->power.subsys_data = NULL; |
Ulf Hansson | 1e95e3b | 2015-01-29 18:39:05 +0100 | [diff] [blame] | 75 | else |
Shuah Khan | d5e1670a | 2013-05-08 01:14:32 +0200 | [diff] [blame] | 76 | psd = NULL; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 77 | |
| 78 | out: |
| 79 | spin_unlock_irq(&dev->power.lock); |
Shuah Khan | d5e1670a | 2013-05-08 01:14:32 +0200 | [diff] [blame] | 80 | kfree(psd); |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data); |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * dev_pm_domain_attach - Attach a device to its PM domain. |
| 86 | * @dev: Device to attach. |
| 87 | * @power_on: Used to indicate whether we should power on the device. |
| 88 | * |
| 89 | * The @dev may only be attached to a single PM domain. By iterating through |
| 90 | * the available alternatives we try to find a valid PM domain for the device. |
| 91 | * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain |
| 92 | * should be assigned by the corresponding attach function. |
| 93 | * |
| 94 | * This function should typically be invoked from subsystem level code during |
| 95 | * the probe phase. Especially for those that holds devices which requires |
| 96 | * power management through PM domains. |
| 97 | * |
| 98 | * Callers must ensure proper synchronization of this function with power |
| 99 | * management callbacks. |
| 100 | * |
Geert Uytterhoeven | 49072f9 | 2018-05-15 15:21:43 +0200 | [diff] [blame] | 101 | * Returns 0 on successfully attached PM domain, or when it is found that the |
| 102 | * device doesn't need a PM domain, else a negative error code. |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 103 | */ |
| 104 | int dev_pm_domain_attach(struct device *dev, bool power_on) |
| 105 | { |
| 106 | int ret; |
| 107 | |
Ulf Hansson | 4f68874 | 2018-04-26 10:53:03 +0200 | [diff] [blame] | 108 | if (dev->pm_domain) |
Ulf Hansson | 94ef9b8 | 2018-05-14 16:52:37 +0200 | [diff] [blame] | 109 | return 0; |
Ulf Hansson | 4f68874 | 2018-04-26 10:53:03 +0200 | [diff] [blame] | 110 | |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 111 | ret = acpi_dev_pm_attach(dev, power_on); |
Ulf Hansson | 919b730 | 2018-05-09 12:17:52 +0200 | [diff] [blame] | 112 | if (!ret) |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 113 | ret = genpd_dev_pm_attach(dev); |
| 114 | |
Ulf Hansson | 919b730 | 2018-05-09 12:17:52 +0200 | [diff] [blame] | 115 | return ret < 0 ? ret : 0; |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 116 | } |
| 117 | EXPORT_SYMBOL_GPL(dev_pm_domain_attach); |
| 118 | |
| 119 | /** |
Ulf Hansson | 82e12d9 | 2018-05-31 12:59:59 +0200 | [diff] [blame] | 120 | * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains. |
| 121 | * @dev: The device used to lookup the PM domain. |
| 122 | * @index: The index of the PM domain. |
| 123 | * |
| 124 | * As @dev may only be attached to a single PM domain, the backend PM domain |
| 125 | * provider creates a virtual device to attach instead. If attachment succeeds, |
| 126 | * the ->detach() callback in the struct dev_pm_domain are assigned by the |
| 127 | * corresponding backend attach function, as to deal with detaching of the |
| 128 | * created virtual device. |
| 129 | * |
| 130 | * This function should typically be invoked by a driver during the probe phase, |
| 131 | * in case its device requires power management through multiple PM domains. The |
| 132 | * driver may benefit from using the received device, to configure device-links |
| 133 | * towards its original device. Depending on the use-case and if needed, the |
| 134 | * links may be dynamically changed by the driver, which allows it to control |
| 135 | * the power to the PM domains independently from each other. |
| 136 | * |
| 137 | * Callers must ensure proper synchronization of this function with power |
| 138 | * management callbacks. |
| 139 | * |
| 140 | * Returns the virtual created device when successfully attached to its PM |
| 141 | * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR(). |
| 142 | * Note that, to detach the returned virtual device, the driver shall call |
| 143 | * dev_pm_domain_detach() on it, typically during the remove phase. |
| 144 | */ |
| 145 | struct device *dev_pm_domain_attach_by_id(struct device *dev, |
| 146 | unsigned int index) |
| 147 | { |
| 148 | if (dev->pm_domain) |
| 149 | return ERR_PTR(-EEXIST); |
| 150 | |
| 151 | return genpd_dev_pm_attach_by_id(dev, index); |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id); |
| 154 | |
| 155 | /** |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 156 | * dev_pm_domain_detach - Detach a device from its PM domain. |
Manuel Pégourié-Gonnard | 4295733 | 2015-12-29 11:03:21 +0100 | [diff] [blame] | 157 | * @dev: Device to detach. |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 158 | * @power_off: Used to indicate whether we should power off the device. |
| 159 | * |
Ulf Hansson | 82e12d9 | 2018-05-31 12:59:59 +0200 | [diff] [blame] | 160 | * This functions will reverse the actions from dev_pm_domain_attach() and |
| 161 | * dev_pm_domain_attach_by_id(), thus it detaches @dev from its PM domain. |
| 162 | * Typically it should be invoked during the remove phase, either from |
| 163 | * subsystem level code or from drivers. |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 164 | * |
| 165 | * Callers must ensure proper synchronization of this function with power |
| 166 | * management callbacks. |
| 167 | */ |
| 168 | void dev_pm_domain_detach(struct device *dev, bool power_off) |
| 169 | { |
| 170 | if (dev->pm_domain && dev->pm_domain->detach) |
| 171 | dev->pm_domain->detach(dev, power_off); |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(dev_pm_domain_detach); |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * dev_pm_domain_set - Set PM domain of a device. |
| 177 | * @dev: Device whose PM domain is to be set. |
| 178 | * @pd: PM domain to be set, or NULL. |
| 179 | * |
| 180 | * Sets the PM domain the device belongs to. The PM domain of a device needs |
| 181 | * to be set before its probe finishes (it's bound to a driver). |
| 182 | * |
| 183 | * This function must be called with the device lock held. |
| 184 | */ |
| 185 | void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd) |
| 186 | { |
| 187 | if (dev->pm_domain == pd) |
| 188 | return; |
| 189 | |
Rafael J. Wysocki | e79aee4 | 2016-01-30 12:54:29 +0100 | [diff] [blame] | 190 | WARN(pd && device_is_bound(dev), |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 191 | "PM domains can only be changed for unbound devices\n"); |
| 192 | dev->pm_domain = pd; |
Tomeu Vizoso | aa8e54b5 | 2016-01-07 16:46:14 +0100 | [diff] [blame] | 193 | device_pm_check_callbacks(dev); |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 194 | } |
| 195 | EXPORT_SYMBOL_GPL(dev_pm_domain_set); |