common.c 13 KB

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