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 | |
| 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 1 if a new object has been created, otherwise |
| 23 | * return 0 or error code. |
| 24 | */ |
| 25 | int dev_pm_get_subsys_data(struct device *dev) |
| 26 | { |
| 27 | struct pm_subsys_data *psd; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 28 | |
| 29 | psd = kzalloc(sizeof(*psd), GFP_KERNEL); |
| 30 | if (!psd) |
| 31 | return -ENOMEM; |
| 32 | |
| 33 | spin_lock_irq(&dev->power.lock); |
| 34 | |
| 35 | if (dev->power.subsys_data) { |
| 36 | dev->power.subsys_data->refcount++; |
| 37 | } else { |
| 38 | spin_lock_init(&psd->lock); |
| 39 | psd->refcount = 1; |
| 40 | dev->power.subsys_data = psd; |
| 41 | pm_clk_init(dev); |
| 42 | psd = NULL; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | spin_unlock_irq(&dev->power.lock); |
| 46 | |
| 47 | /* kfree() verifies that its argument is nonzero. */ |
| 48 | kfree(psd); |
| 49 | |
Rafael J. Wysocki | 7725495 | 2012-08-07 13:50:14 +0200 | [diff] [blame] | 50 | return 0; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 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. Return 1 if that happens or 0 |
| 60 | * otherwise. |
| 61 | */ |
| 62 | int dev_pm_put_subsys_data(struct device *dev) |
| 63 | { |
| 64 | struct pm_subsys_data *psd; |
Shuah Khan | d5e1670 | 2013-05-08 01:14:32 +0200 | [diff] [blame] | 65 | int ret = 1; |
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 | d5e1670 | 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 | |
| 73 | if (--psd->refcount == 0) { |
| 74 | dev->power.subsys_data = NULL; |
Shuah Khan | d5e1670 | 2013-05-08 01:14:32 +0200 | [diff] [blame] | 75 | } else { |
| 76 | psd = NULL; |
| 77 | ret = 0; |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | out: |
| 81 | spin_unlock_irq(&dev->power.lock); |
Shuah Khan | d5e1670 | 2013-05-08 01:14:32 +0200 | [diff] [blame] | 82 | kfree(psd); |
Rafael J. Wysocki | ef27bed | 2011-08-25 15:34:01 +0200 | [diff] [blame] | 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data); |
Ulf Hansson | 46420dd | 2014-09-19 20:27:37 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * dev_pm_domain_attach - Attach a device to its PM domain. |
| 90 | * @dev: Device to attach. |
| 91 | * @power_on: Used to indicate whether we should power on the device. |
| 92 | * |
| 93 | * The @dev may only be attached to a single PM domain. By iterating through |
| 94 | * the available alternatives we try to find a valid PM domain for the device. |
| 95 | * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain |
| 96 | * should be assigned by the corresponding attach function. |
| 97 | * |
| 98 | * This function should typically be invoked from subsystem level code during |
| 99 | * the probe phase. Especially for those that holds devices which requires |
| 100 | * power management through PM domains. |
| 101 | * |
| 102 | * Callers must ensure proper synchronization of this function with power |
| 103 | * management callbacks. |
| 104 | * |
| 105 | * Returns 0 on successfully attached PM domain or negative error code. |
| 106 | */ |
| 107 | int dev_pm_domain_attach(struct device *dev, bool power_on) |
| 108 | { |
| 109 | int ret; |
| 110 | |
| 111 | ret = acpi_dev_pm_attach(dev, power_on); |
| 112 | if (ret) |
| 113 | ret = genpd_dev_pm_attach(dev); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(dev_pm_domain_attach); |
| 118 | |
| 119 | /** |
| 120 | * dev_pm_domain_detach - Detach a device from its PM domain. |
| 121 | * @dev: Device to attach. |
| 122 | * @power_off: Used to indicate whether we should power off the device. |
| 123 | * |
| 124 | * This functions will reverse the actions from dev_pm_domain_attach() and thus |
| 125 | * try to detach the @dev from its PM domain. Typically it should be invoked |
| 126 | * from subsystem level code during the remove phase. |
| 127 | * |
| 128 | * Callers must ensure proper synchronization of this function with power |
| 129 | * management callbacks. |
| 130 | */ |
| 131 | void dev_pm_domain_detach(struct device *dev, bool power_off) |
| 132 | { |
| 133 | if (dev->pm_domain && dev->pm_domain->detach) |
| 134 | dev->pm_domain->detach(dev, power_off); |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(dev_pm_domain_detach); |