core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 Linaro Ltd.
  4. */
  5. #define dev_fmt(fmt) "pwrctrl: " fmt
  6. #include <linux/device.h>
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <linux/of.h>
  10. #include <linux/of_graph.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci-pwrctrl.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/slab.h>
  17. #include "../pci.h"
  18. static int pci_pwrctrl_notify(struct notifier_block *nb, unsigned long action,
  19. void *data)
  20. {
  21. struct pci_pwrctrl *pwrctrl = container_of(nb, struct pci_pwrctrl, nb);
  22. struct device *dev = data;
  23. if (dev_fwnode(dev) != dev_fwnode(pwrctrl->dev))
  24. return NOTIFY_DONE;
  25. switch (action) {
  26. case BUS_NOTIFY_ADD_DEVICE:
  27. /*
  28. * We will have two struct device objects bound to two different
  29. * drivers on different buses but consuming the same DT node. We
  30. * must not bind the pins twice in this case but only once for
  31. * the first device to be added.
  32. *
  33. * If we got here then the PCI device is the second after the
  34. * power control platform device. Mark its OF node as reused.
  35. */
  36. dev->of_node_reused = true;
  37. break;
  38. }
  39. return NOTIFY_DONE;
  40. }
  41. /**
  42. * pci_pwrctrl_init() - Initialize the PCI power control context struct
  43. *
  44. * @pwrctrl: PCI power control data
  45. * @dev: Parent device
  46. */
  47. void pci_pwrctrl_init(struct pci_pwrctrl *pwrctrl, struct device *dev)
  48. {
  49. pwrctrl->dev = dev;
  50. dev_set_drvdata(dev, pwrctrl);
  51. }
  52. EXPORT_SYMBOL_GPL(pci_pwrctrl_init);
  53. /**
  54. * pci_pwrctrl_device_set_ready() - Notify the pwrctrl subsystem that the PCI
  55. * device is powered-up and ready to be detected.
  56. *
  57. * @pwrctrl: PCI power control data.
  58. *
  59. * Returns:
  60. * 0 on success, negative error number on error.
  61. *
  62. * Note:
  63. * This function returning 0 doesn't mean the device was detected. It means,
  64. * that the bus rescan was successfully started. The device will get bound to
  65. * its PCI driver asynchronously.
  66. */
  67. int pci_pwrctrl_device_set_ready(struct pci_pwrctrl *pwrctrl)
  68. {
  69. int ret;
  70. if (!pwrctrl->dev)
  71. return -ENODEV;
  72. pwrctrl->nb.notifier_call = pci_pwrctrl_notify;
  73. ret = bus_register_notifier(&pci_bus_type, &pwrctrl->nb);
  74. if (ret)
  75. return ret;
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(pci_pwrctrl_device_set_ready);
  79. /**
  80. * pci_pwrctrl_device_unset_ready() - Notify the pwrctrl subsystem that the PCI
  81. * device is about to be powered-down.
  82. *
  83. * @pwrctrl: PCI power control data.
  84. */
  85. void pci_pwrctrl_device_unset_ready(struct pci_pwrctrl *pwrctrl)
  86. {
  87. /*
  88. * We don't have to delete the link here. Typically, this function
  89. * is only called when the power control device is being detached. If
  90. * it is being detached then the child PCI device must have already
  91. * been unbound too or the device core wouldn't let us unbind.
  92. */
  93. bus_unregister_notifier(&pci_bus_type, &pwrctrl->nb);
  94. }
  95. EXPORT_SYMBOL_GPL(pci_pwrctrl_device_unset_ready);
  96. static void devm_pci_pwrctrl_device_unset_ready(void *data)
  97. {
  98. struct pci_pwrctrl *pwrctrl = data;
  99. pci_pwrctrl_device_unset_ready(pwrctrl);
  100. }
  101. /**
  102. * devm_pci_pwrctrl_device_set_ready - Managed variant of
  103. * pci_pwrctrl_device_set_ready().
  104. *
  105. * @dev: Device managing this pwrctrl provider.
  106. * @pwrctrl: PCI power control data.
  107. *
  108. * Returns:
  109. * 0 on success, negative error number on error.
  110. */
  111. int devm_pci_pwrctrl_device_set_ready(struct device *dev,
  112. struct pci_pwrctrl *pwrctrl)
  113. {
  114. int ret;
  115. ret = pci_pwrctrl_device_set_ready(pwrctrl);
  116. if (ret)
  117. return ret;
  118. return devm_add_action_or_reset(dev,
  119. devm_pci_pwrctrl_device_unset_ready,
  120. pwrctrl);
  121. }
  122. EXPORT_SYMBOL_GPL(devm_pci_pwrctrl_device_set_ready);
  123. static int __pci_pwrctrl_power_off_device(struct device *dev)
  124. {
  125. struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
  126. if (!pwrctrl)
  127. return 0;
  128. return pwrctrl->power_off(pwrctrl);
  129. }
  130. static void pci_pwrctrl_power_off_device(struct device_node *np)
  131. {
  132. struct platform_device *pdev;
  133. int ret;
  134. for_each_available_child_of_node_scoped(np, child)
  135. pci_pwrctrl_power_off_device(child);
  136. pdev = of_find_device_by_node(np);
  137. if (!pdev)
  138. return;
  139. if (device_is_bound(&pdev->dev)) {
  140. ret = __pci_pwrctrl_power_off_device(&pdev->dev);
  141. if (ret)
  142. dev_err(&pdev->dev, "Failed to power off device: %d", ret);
  143. }
  144. platform_device_put(pdev);
  145. }
  146. /**
  147. * pci_pwrctrl_power_off_devices - Power off pwrctrl devices
  148. *
  149. * @parent: PCI host controller device
  150. *
  151. * Recursively traverse all pwrctrl devices for the devicetree hierarchy
  152. * below the specified PCI host controller and power them off in a depth
  153. * first manner.
  154. */
  155. void pci_pwrctrl_power_off_devices(struct device *parent)
  156. {
  157. struct device_node *np = parent->of_node;
  158. for_each_available_child_of_node_scoped(np, child)
  159. pci_pwrctrl_power_off_device(child);
  160. }
  161. EXPORT_SYMBOL_GPL(pci_pwrctrl_power_off_devices);
  162. static int __pci_pwrctrl_power_on_device(struct device *dev)
  163. {
  164. struct pci_pwrctrl *pwrctrl = dev_get_drvdata(dev);
  165. if (!pwrctrl)
  166. return 0;
  167. return pwrctrl->power_on(pwrctrl);
  168. }
  169. /*
  170. * Power on the devices in a depth first manner. Before powering on the device,
  171. * make sure its driver is bound.
  172. */
  173. static int pci_pwrctrl_power_on_device(struct device_node *np)
  174. {
  175. struct platform_device *pdev;
  176. int ret;
  177. for_each_available_child_of_node_scoped(np, child) {
  178. ret = pci_pwrctrl_power_on_device(child);
  179. if (ret)
  180. return ret;
  181. }
  182. pdev = of_find_device_by_node(np);
  183. if (!pdev)
  184. return 0;
  185. if (device_is_bound(&pdev->dev)) {
  186. ret = __pci_pwrctrl_power_on_device(&pdev->dev);
  187. } else {
  188. /* FIXME: Use blocking wait instead of probe deferral */
  189. dev_dbg(&pdev->dev, "driver is not bound\n");
  190. ret = -EPROBE_DEFER;
  191. }
  192. platform_device_put(pdev);
  193. return ret;
  194. }
  195. /**
  196. * pci_pwrctrl_power_on_devices - Power on pwrctrl devices
  197. *
  198. * @parent: PCI host controller device
  199. *
  200. * Recursively traverse all pwrctrl devices for the devicetree hierarchy
  201. * below the specified PCI host controller and power them on in a depth
  202. * first manner. On error, all powered on devices will be powered off.
  203. *
  204. * Return: 0 on success, -EPROBE_DEFER if any pwrctrl driver is not bound, an
  205. * appropriate error code otherwise.
  206. */
  207. int pci_pwrctrl_power_on_devices(struct device *parent)
  208. {
  209. struct device_node *np = parent->of_node;
  210. struct device_node *child = NULL;
  211. int ret;
  212. for_each_available_child_of_node(np, child) {
  213. ret = pci_pwrctrl_power_on_device(child);
  214. if (ret)
  215. goto err_power_off;
  216. }
  217. return 0;
  218. err_power_off:
  219. for_each_available_child_of_node_scoped(np, tmp) {
  220. if (tmp == child)
  221. break;
  222. pci_pwrctrl_power_off_device(tmp);
  223. }
  224. of_node_put(child);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(pci_pwrctrl_power_on_devices);
  228. /*
  229. * Check whether the pwrctrl device really needs to be created or not. The
  230. * pwrctrl device will only be created if the node satisfies below requirements:
  231. *
  232. * 1. Presence of compatible property with "pci" prefix to match against the
  233. * pwrctrl driver (AND)
  234. * 2. At least one of the power supplies defined in the devicetree node of the
  235. * device (OR) in the remote endpoint parent node to indicate pwrctrl
  236. * requirement.
  237. */
  238. static bool pci_pwrctrl_is_required(struct device_node *np)
  239. {
  240. struct device_node *endpoint;
  241. const char *compat;
  242. int ret;
  243. ret = of_property_read_string(np, "compatible", &compat);
  244. if (ret < 0)
  245. return false;
  246. if (!strstarts(compat, "pci"))
  247. return false;
  248. if (of_pci_supply_present(np))
  249. return true;
  250. if (of_graph_is_present(np)) {
  251. for_each_endpoint_of_node(np, endpoint) {
  252. struct device_node *remote __free(device_node) =
  253. of_graph_get_remote_port_parent(endpoint);
  254. if (remote) {
  255. if (of_pci_supply_present(remote)) {
  256. of_node_put(endpoint);
  257. return true;
  258. }
  259. }
  260. }
  261. }
  262. return false;
  263. }
  264. static int pci_pwrctrl_create_device(struct device_node *np,
  265. struct device *parent)
  266. {
  267. struct platform_device *pdev;
  268. int ret;
  269. for_each_available_child_of_node_scoped(np, child) {
  270. ret = pci_pwrctrl_create_device(child, parent);
  271. if (ret)
  272. return ret;
  273. }
  274. /* Bail out if the platform device is already available for the node */
  275. pdev = of_find_device_by_node(np);
  276. if (pdev) {
  277. platform_device_put(pdev);
  278. return 0;
  279. }
  280. if (!pci_pwrctrl_is_required(np)) {
  281. dev_dbg(parent, "Skipping OF node: %s\n", np->name);
  282. return 0;
  283. }
  284. /* Now create the pwrctrl device */
  285. pdev = of_platform_device_create(np, NULL, parent);
  286. if (!pdev) {
  287. dev_err(parent, "Failed to create pwrctrl device for node: %s\n", np->name);
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. /**
  293. * pci_pwrctrl_create_devices - Create pwrctrl devices
  294. *
  295. * @parent: PCI host controller device
  296. *
  297. * Recursively create pwrctrl devices for the devicetree hierarchy below
  298. * the specified PCI host controller in a depth first manner. On error, all
  299. * created devices will be destroyed.
  300. *
  301. * Return: 0 on success, negative error number on error.
  302. */
  303. int pci_pwrctrl_create_devices(struct device *parent)
  304. {
  305. int ret;
  306. for_each_available_child_of_node_scoped(parent->of_node, child) {
  307. ret = pci_pwrctrl_create_device(child, parent);
  308. if (ret) {
  309. pci_pwrctrl_destroy_devices(parent);
  310. return ret;
  311. }
  312. }
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(pci_pwrctrl_create_devices);
  316. static void pci_pwrctrl_destroy_device(struct device_node *np)
  317. {
  318. struct platform_device *pdev;
  319. for_each_available_child_of_node_scoped(np, child)
  320. pci_pwrctrl_destroy_device(child);
  321. pdev = of_find_device_by_node(np);
  322. if (!pdev)
  323. return;
  324. of_device_unregister(pdev);
  325. platform_device_put(pdev);
  326. of_node_clear_flag(np, OF_POPULATED);
  327. }
  328. /**
  329. * pci_pwrctrl_destroy_devices - Destroy pwrctrl devices
  330. *
  331. * @parent: PCI host controller device
  332. *
  333. * Recursively destroy pwrctrl devices for the devicetree hierarchy below
  334. * the specified PCI host controller in a depth first manner.
  335. */
  336. void pci_pwrctrl_destroy_devices(struct device *parent)
  337. {
  338. struct device_node *np = parent->of_node;
  339. for_each_available_child_of_node_scoped(np, child)
  340. pci_pwrctrl_destroy_device(child);
  341. }
  342. EXPORT_SYMBOL_GPL(pci_pwrctrl_destroy_devices);
  343. MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
  344. MODULE_DESCRIPTION("PCI Device Power Control core driver");
  345. MODULE_LICENSE("GPL");