common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include "power.h"
  15. /**
  16. * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
  17. * @dev: Device to handle.
  18. *
  19. * If power.subsys_data is NULL, point it to a new object, otherwise increment
  20. * its reference counter. Return 0 if new object has been created or refcount
  21. * increased, otherwise negative error code.
  22. */
  23. int dev_pm_get_subsys_data(struct device *dev)
  24. {
  25. struct pm_subsys_data *psd;
  26. psd = kzalloc(sizeof(*psd), GFP_KERNEL);
  27. if (!psd)
  28. return -ENOMEM;
  29. spin_lock_irq(&dev->power.lock);
  30. if (dev->power.subsys_data) {
  31. dev->power.subsys_data->refcount++;
  32. } else {
  33. spin_lock_init(&psd->lock);
  34. psd->refcount = 1;
  35. dev->power.subsys_data = psd;
  36. pm_clk_init(dev);
  37. psd = NULL;
  38. }
  39. spin_unlock_irq(&dev->power.lock);
  40. /* kfree() verifies that its argument is nonzero. */
  41. kfree(psd);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
  45. /**
  46. * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
  47. * @dev: Device to handle.
  48. *
  49. * If the reference counter of power.subsys_data is zero after dropping the
  50. * reference, power.subsys_data is removed.
  51. */
  52. void dev_pm_put_subsys_data(struct device *dev)
  53. {
  54. struct pm_subsys_data *psd;
  55. spin_lock_irq(&dev->power.lock);
  56. psd = dev_to_psd(dev);
  57. if (!psd)
  58. goto out;
  59. if (--psd->refcount == 0)
  60. dev->power.subsys_data = NULL;
  61. else
  62. psd = NULL;
  63. out:
  64. spin_unlock_irq(&dev->power.lock);
  65. kfree(psd);
  66. }
  67. EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
  68. /**
  69. * dev_pm_domain_attach - Attach a device to its PM domain.
  70. * @dev: Device to attach.
  71. * @power_on: Used to indicate whether we should power on the device.
  72. *
  73. * The @dev may only be attached to a single PM domain. By iterating through
  74. * the available alternatives we try to find a valid PM domain for the device.
  75. * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
  76. * should be assigned by the corresponding attach function.
  77. *
  78. * This function should typically be invoked from subsystem level code during
  79. * the probe phase. Especially for those that holds devices which requires
  80. * power management through PM domains.
  81. *
  82. * Callers must ensure proper synchronization of this function with power
  83. * management callbacks.
  84. *
  85. * Returns 0 on successfully attached PM domain, or when it is found that the
  86. * device doesn't need a PM domain, else a negative error code.
  87. */
  88. int dev_pm_domain_attach(struct device *dev, bool power_on)
  89. {
  90. int ret;
  91. if (dev->pm_domain)
  92. return 0;
  93. ret = acpi_dev_pm_attach(dev, power_on);
  94. if (!ret)
  95. ret = genpd_dev_pm_attach(dev);
  96. return ret < 0 ? ret : 0;
  97. }
  98. EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
  99. /**
  100. * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.
  101. * @dev: The device used to lookup the PM domain.
  102. * @index: The index of the PM domain.
  103. *
  104. * As @dev may only be attached to a single PM domain, the backend PM domain
  105. * provider creates a virtual device to attach instead. If attachment succeeds,
  106. * the ->detach() callback in the struct dev_pm_domain are assigned by the
  107. * corresponding backend attach function, as to deal with detaching of the
  108. * created virtual device.
  109. *
  110. * This function should typically be invoked by a driver during the probe phase,
  111. * in case its device requires power management through multiple PM domains. The
  112. * driver may benefit from using the received device, to configure device-links
  113. * towards its original device. Depending on the use-case and if needed, the
  114. * links may be dynamically changed by the driver, which allows it to control
  115. * the power to the PM domains independently from each other.
  116. *
  117. * Callers must ensure proper synchronization of this function with power
  118. * management callbacks.
  119. *
  120. * Returns the virtual created device when successfully attached to its PM
  121. * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().
  122. * Note that, to detach the returned virtual device, the driver shall call
  123. * dev_pm_domain_detach() on it, typically during the remove phase.
  124. */
  125. struct device *dev_pm_domain_attach_by_id(struct device *dev,
  126. unsigned int index)
  127. {
  128. if (dev->pm_domain)
  129. return ERR_PTR(-EEXIST);
  130. return genpd_dev_pm_attach_by_id(dev, index);
  131. }
  132. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);
  133. /**
  134. * dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.
  135. * @dev: The device used to lookup the PM domain.
  136. * @name: The name of the PM domain.
  137. *
  138. * For a detailed function description, see dev_pm_domain_attach_by_id().
  139. */
  140. struct device *dev_pm_domain_attach_by_name(struct device *dev,
  141. const char *name)
  142. {
  143. if (dev->pm_domain)
  144. return ERR_PTR(-EEXIST);
  145. return genpd_dev_pm_attach_by_name(dev, name);
  146. }
  147. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);
  148. /**
  149. * dev_pm_domain_attach_list - Associate a device with its PM domains.
  150. * @dev: The device used to lookup the PM domains for.
  151. * @data: The data used for attaching to the PM domains.
  152. * @list: An out-parameter with an allocated list of attached PM domains.
  153. *
  154. * This function helps to attach a device to its multiple PM domains. The
  155. * caller, which is typically a driver's probe function, may provide a list of
  156. * names for the PM domains that we should try to attach the device to, but it
  157. * may also provide an empty list, in case the attach should be done for all of
  158. * the available PM domains.
  159. *
  160. * Callers must ensure proper synchronization of this function with power
  161. * management callbacks.
  162. *
  163. * Returns the number of attached PM domains or a negative error code in case of
  164. * a failure. Note that, to detach the list of PM domains, the driver shall call
  165. * dev_pm_domain_detach_list(), typically during the remove phase.
  166. */
  167. int dev_pm_domain_attach_list(struct device *dev,
  168. const struct dev_pm_domain_attach_data *data,
  169. struct dev_pm_domain_list **list)
  170. {
  171. struct device_node *np = dev->of_node;
  172. struct dev_pm_domain_list *pds;
  173. struct device *pd_dev = NULL;
  174. int ret, i, num_pds = 0;
  175. bool by_id = true;
  176. size_t size;
  177. u32 pd_flags = data ? data->pd_flags : 0;
  178. u32 link_flags = pd_flags & PD_FLAG_NO_DEV_LINK ? 0 :
  179. DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME;
  180. if (dev->pm_domain)
  181. return -EEXIST;
  182. /* For now this is limited to OF based platforms. */
  183. if (!np)
  184. return 0;
  185. if (data && data->pd_names) {
  186. num_pds = data->num_pd_names;
  187. by_id = false;
  188. } else {
  189. num_pds = of_count_phandle_with_args(np, "power-domains",
  190. "#power-domain-cells");
  191. }
  192. if (num_pds <= 0)
  193. return 0;
  194. pds = kzalloc(sizeof(*pds), GFP_KERNEL);
  195. if (!pds)
  196. return -ENOMEM;
  197. size = sizeof(*pds->pd_devs) + sizeof(*pds->pd_links);
  198. pds->pd_devs = kcalloc(num_pds, size, GFP_KERNEL);
  199. if (!pds->pd_devs) {
  200. ret = -ENOMEM;
  201. goto free_pds;
  202. }
  203. pds->pd_links = (void *)(pds->pd_devs + num_pds);
  204. if (link_flags && pd_flags & PD_FLAG_DEV_LINK_ON)
  205. link_flags |= DL_FLAG_RPM_ACTIVE;
  206. for (i = 0; i < num_pds; i++) {
  207. if (by_id)
  208. pd_dev = dev_pm_domain_attach_by_id(dev, i);
  209. else
  210. pd_dev = dev_pm_domain_attach_by_name(dev,
  211. data->pd_names[i]);
  212. if (IS_ERR_OR_NULL(pd_dev)) {
  213. ret = pd_dev ? PTR_ERR(pd_dev) : -ENODEV;
  214. goto err_attach;
  215. }
  216. if (link_flags) {
  217. struct device_link *link;
  218. link = device_link_add(dev, pd_dev, link_flags);
  219. if (!link) {
  220. ret = -ENODEV;
  221. goto err_link;
  222. }
  223. pds->pd_links[i] = link;
  224. }
  225. pds->pd_devs[i] = pd_dev;
  226. }
  227. pds->num_pds = num_pds;
  228. *list = pds;
  229. return num_pds;
  230. err_link:
  231. dev_pm_domain_detach(pd_dev, true);
  232. err_attach:
  233. while (--i >= 0) {
  234. if (pds->pd_links[i])
  235. device_link_del(pds->pd_links[i]);
  236. dev_pm_domain_detach(pds->pd_devs[i], true);
  237. }
  238. kfree(pds->pd_devs);
  239. free_pds:
  240. kfree(pds);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_list);
  244. /**
  245. * devm_pm_domain_detach_list - devres-enabled version of dev_pm_domain_detach_list.
  246. * @_list: The list of PM domains to detach.
  247. *
  248. * This function reverse the actions from devm_pm_domain_attach_list().
  249. * it will be invoked during the remove phase from drivers implicitly if driver
  250. * uses devm_pm_domain_attach_list() to attach the PM domains.
  251. */
  252. static void devm_pm_domain_detach_list(void *_list)
  253. {
  254. struct dev_pm_domain_list *list = _list;
  255. dev_pm_domain_detach_list(list);
  256. }
  257. /**
  258. * devm_pm_domain_attach_list - devres-enabled version of dev_pm_domain_attach_list
  259. * @dev: The device used to lookup the PM domains for.
  260. * @data: The data used for attaching to the PM domains.
  261. * @list: An out-parameter with an allocated list of attached PM domains.
  262. *
  263. * NOTE: this will also handle calling devm_pm_domain_detach_list() for
  264. * you during remove phase.
  265. *
  266. * Returns the number of attached PM domains or a negative error code in case of
  267. * a failure.
  268. */
  269. int devm_pm_domain_attach_list(struct device *dev,
  270. const struct dev_pm_domain_attach_data *data,
  271. struct dev_pm_domain_list **list)
  272. {
  273. int ret, num_pds;
  274. num_pds = dev_pm_domain_attach_list(dev, data, list);
  275. if (num_pds <= 0)
  276. return num_pds;
  277. ret = devm_add_action_or_reset(dev, devm_pm_domain_detach_list, *list);
  278. if (ret)
  279. return ret;
  280. return num_pds;
  281. }
  282. EXPORT_SYMBOL_GPL(devm_pm_domain_attach_list);
  283. /**
  284. * dev_pm_domain_detach - Detach a device from its PM domain.
  285. * @dev: Device to detach.
  286. * @power_off: Used to indicate whether we should power off the device.
  287. *
  288. * This functions will reverse the actions from dev_pm_domain_attach(),
  289. * dev_pm_domain_attach_by_id() and dev_pm_domain_attach_by_name(), thus it
  290. * detaches @dev from its PM domain. Typically it should be invoked during the
  291. * remove phase, either from subsystem level code or from drivers.
  292. *
  293. * Callers must ensure proper synchronization of this function with power
  294. * management callbacks.
  295. */
  296. void dev_pm_domain_detach(struct device *dev, bool power_off)
  297. {
  298. if (dev->pm_domain && dev->pm_domain->detach)
  299. dev->pm_domain->detach(dev, power_off);
  300. }
  301. EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
  302. /**
  303. * dev_pm_domain_detach_list - Detach a list of PM domains.
  304. * @list: The list of PM domains to detach.
  305. *
  306. * This function reverse the actions from dev_pm_domain_attach_list().
  307. * Typically it should be invoked during the remove phase from drivers.
  308. *
  309. * Callers must ensure proper synchronization of this function with power
  310. * management callbacks.
  311. */
  312. void dev_pm_domain_detach_list(struct dev_pm_domain_list *list)
  313. {
  314. int i;
  315. if (!list)
  316. return;
  317. for (i = 0; i < list->num_pds; i++) {
  318. if (list->pd_links[i])
  319. device_link_del(list->pd_links[i]);
  320. dev_pm_domain_detach(list->pd_devs[i], true);
  321. }
  322. kfree(list->pd_devs);
  323. kfree(list);
  324. }
  325. EXPORT_SYMBOL_GPL(dev_pm_domain_detach_list);
  326. /**
  327. * dev_pm_domain_start - Start the device through its PM domain.
  328. * @dev: Device to start.
  329. *
  330. * This function should typically be called during probe by a subsystem/driver,
  331. * when it needs to start its device from the PM domain's perspective. Note
  332. * that, it's assumed that the PM domain is already powered on when this
  333. * function is called.
  334. *
  335. * Returns 0 on success and negative error values on failures.
  336. */
  337. int dev_pm_domain_start(struct device *dev)
  338. {
  339. if (dev->pm_domain && dev->pm_domain->start)
  340. return dev->pm_domain->start(dev);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL_GPL(dev_pm_domain_start);
  344. /**
  345. * dev_pm_domain_set - Set PM domain of a device.
  346. * @dev: Device whose PM domain is to be set.
  347. * @pd: PM domain to be set, or NULL.
  348. *
  349. * Sets the PM domain the device belongs to. The PM domain of a device needs
  350. * to be set before its probe finishes (it's bound to a driver).
  351. *
  352. * This function must be called with the device lock held.
  353. */
  354. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
  355. {
  356. if (dev->pm_domain == pd)
  357. return;
  358. WARN(pd && device_is_bound(dev),
  359. "PM domains can only be changed for unbound devices\n");
  360. dev->pm_domain = pd;
  361. device_pm_check_callbacks(dev);
  362. }
  363. EXPORT_SYMBOL_GPL(dev_pm_domain_set);
  364. /**
  365. * dev_pm_domain_set_performance_state - Request a new performance state.
  366. * @dev: The device to make the request for.
  367. * @state: Target performance state for the device.
  368. *
  369. * This function should be called when a new performance state needs to be
  370. * requested for a device that is attached to a PM domain. Note that, the
  371. * support for performance scaling for PM domains is optional.
  372. *
  373. * Returns 0 on success and when performance scaling isn't supported, negative
  374. * error code on failure.
  375. */
  376. int dev_pm_domain_set_performance_state(struct device *dev, unsigned int state)
  377. {
  378. if (dev->pm_domain && dev->pm_domain->set_performance_state)
  379. return dev->pm_domain->set_performance_state(dev, state);
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(dev_pm_domain_set_performance_state);