zynqmp_pm_domains.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ZynqMP Generic PM domain support
  4. *
  5. * Copyright (C) 2015-2018 Xilinx, Inc.
  6. *
  7. * Davorin Mista <davorin.mista@aggios.com>
  8. * Jolly Shah <jollys@xilinx.com>
  9. * Rajan Vaja <rajan.vaja@xilinx.com>
  10. */
  11. #include <linux/err.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_domain.h>
  17. #include <linux/slab.h>
  18. #include <linux/firmware/xlnx-zynqmp.h>
  19. #define ZYNQMP_NUM_DOMAINS (100)
  20. /* Flag stating if PM nodes mapped to the PM domain has been requested */
  21. #define ZYNQMP_PM_DOMAIN_REQUESTED BIT(0)
  22. static const struct zynqmp_eemi_ops *eemi_ops;
  23. /**
  24. * struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain
  25. * @gpd: Generic power domain
  26. * @node_id: PM node ID corresponding to device inside PM domain
  27. * @flags: ZynqMP PM domain flags
  28. */
  29. struct zynqmp_pm_domain {
  30. struct generic_pm_domain gpd;
  31. u32 node_id;
  32. u8 flags;
  33. };
  34. /**
  35. * zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source
  36. * path
  37. * @dev: Device to check for wakeup source path
  38. * @not_used: Data member (not required)
  39. *
  40. * This function is checks device's child hierarchy and checks if any device is
  41. * set as wakeup source.
  42. *
  43. * Return: 1 if device is in wakeup source path else 0
  44. */
  45. static int zynqmp_gpd_is_active_wakeup_path(struct device *dev, void *not_used)
  46. {
  47. int may_wakeup;
  48. may_wakeup = device_may_wakeup(dev);
  49. if (may_wakeup)
  50. return may_wakeup;
  51. return device_for_each_child(dev, NULL,
  52. zynqmp_gpd_is_active_wakeup_path);
  53. }
  54. /**
  55. * zynqmp_gpd_power_on() - Power on PM domain
  56. * @domain: Generic PM domain
  57. *
  58. * This function is called before devices inside a PM domain are resumed, to
  59. * power on PM domain.
  60. *
  61. * Return: 0 on success, error code otherwise
  62. */
  63. static int zynqmp_gpd_power_on(struct generic_pm_domain *domain)
  64. {
  65. int ret;
  66. struct zynqmp_pm_domain *pd;
  67. if (!eemi_ops->set_requirement)
  68. return -ENXIO;
  69. pd = container_of(domain, struct zynqmp_pm_domain, gpd);
  70. ret = eemi_ops->set_requirement(pd->node_id,
  71. ZYNQMP_PM_CAPABILITY_ACCESS,
  72. ZYNQMP_PM_MAX_QOS,
  73. ZYNQMP_PM_REQUEST_ACK_BLOCKING);
  74. if (ret) {
  75. pr_err("%s() %s set requirement for node %d failed: %d\n",
  76. __func__, domain->name, pd->node_id, ret);
  77. return ret;
  78. }
  79. pr_debug("%s() Powered on %s domain\n", __func__, domain->name);
  80. return 0;
  81. }
  82. /**
  83. * zynqmp_gpd_power_off() - Power off PM domain
  84. * @domain: Generic PM domain
  85. *
  86. * This function is called after devices inside a PM domain are suspended, to
  87. * power off PM domain.
  88. *
  89. * Return: 0 on success, error code otherwise
  90. */
  91. static int zynqmp_gpd_power_off(struct generic_pm_domain *domain)
  92. {
  93. int ret;
  94. struct pm_domain_data *pdd, *tmp;
  95. struct zynqmp_pm_domain *pd;
  96. u32 capabilities = 0;
  97. bool may_wakeup;
  98. if (!eemi_ops->set_requirement)
  99. return -ENXIO;
  100. pd = container_of(domain, struct zynqmp_pm_domain, gpd);
  101. /* If domain is already released there is nothing to be done */
  102. if (!(pd->flags & ZYNQMP_PM_DOMAIN_REQUESTED)) {
  103. pr_debug("%s() %s domain is already released\n",
  104. __func__, domain->name);
  105. return 0;
  106. }
  107. list_for_each_entry_safe(pdd, tmp, &domain->dev_list, list_node) {
  108. /* If device is in wakeup path, set capability to WAKEUP */
  109. may_wakeup = zynqmp_gpd_is_active_wakeup_path(pdd->dev, NULL);
  110. if (may_wakeup) {
  111. dev_dbg(pdd->dev, "device is in wakeup path in %s\n",
  112. domain->name);
  113. capabilities = ZYNQMP_PM_CAPABILITY_WAKEUP;
  114. break;
  115. }
  116. }
  117. ret = eemi_ops->set_requirement(pd->node_id, capabilities, 0,
  118. ZYNQMP_PM_REQUEST_ACK_NO);
  119. /**
  120. * If powering down of any node inside this domain fails,
  121. * report and return the error
  122. */
  123. if (ret) {
  124. pr_err("%s() %s set requirement for node %d failed: %d\n",
  125. __func__, domain->name, pd->node_id, ret);
  126. return ret;
  127. }
  128. pr_debug("%s() Powered off %s domain\n", __func__, domain->name);
  129. return 0;
  130. }
  131. /**
  132. * zynqmp_gpd_attach_dev() - Attach device to the PM domain
  133. * @domain: Generic PM domain
  134. * @dev: Device to attach
  135. *
  136. * Return: 0 on success, error code otherwise
  137. */
  138. static int zynqmp_gpd_attach_dev(struct generic_pm_domain *domain,
  139. struct device *dev)
  140. {
  141. int ret;
  142. struct zynqmp_pm_domain *pd;
  143. if (!eemi_ops->request_node)
  144. return -ENXIO;
  145. pd = container_of(domain, struct zynqmp_pm_domain, gpd);
  146. /* If this is not the first device to attach there is nothing to do */
  147. if (domain->device_count)
  148. return 0;
  149. ret = eemi_ops->request_node(pd->node_id, 0, 0,
  150. ZYNQMP_PM_REQUEST_ACK_BLOCKING);
  151. /* If requesting a node fails print and return the error */
  152. if (ret) {
  153. pr_err("%s() %s request failed for node %d: %d\n",
  154. __func__, domain->name, pd->node_id, ret);
  155. return ret;
  156. }
  157. pd->flags |= ZYNQMP_PM_DOMAIN_REQUESTED;
  158. pr_debug("%s() %s attached to %s domain\n", __func__,
  159. dev_name(dev), domain->name);
  160. return 0;
  161. }
  162. /**
  163. * zynqmp_gpd_detach_dev() - Detach device from the PM domain
  164. * @domain: Generic PM domain
  165. * @dev: Device to detach
  166. */
  167. static void zynqmp_gpd_detach_dev(struct generic_pm_domain *domain,
  168. struct device *dev)
  169. {
  170. int ret;
  171. struct zynqmp_pm_domain *pd;
  172. if (!eemi_ops->release_node)
  173. return;
  174. pd = container_of(domain, struct zynqmp_pm_domain, gpd);
  175. /* If this is not the last device to detach there is nothing to do */
  176. if (domain->device_count)
  177. return;
  178. ret = eemi_ops->release_node(pd->node_id);
  179. /* If releasing a node fails print the error and return */
  180. if (ret) {
  181. pr_err("%s() %s release failed for node %d: %d\n",
  182. __func__, domain->name, pd->node_id, ret);
  183. return;
  184. }
  185. pd->flags &= ~ZYNQMP_PM_DOMAIN_REQUESTED;
  186. pr_debug("%s() %s detached from %s domain\n", __func__,
  187. dev_name(dev), domain->name);
  188. }
  189. static struct generic_pm_domain *zynqmp_gpd_xlate
  190. (struct of_phandle_args *genpdspec, void *data)
  191. {
  192. struct genpd_onecell_data *genpd_data = data;
  193. unsigned int i, idx = genpdspec->args[0];
  194. struct zynqmp_pm_domain *pd;
  195. pd = container_of(genpd_data->domains[0], struct zynqmp_pm_domain, gpd);
  196. if (genpdspec->args_count != 1)
  197. return ERR_PTR(-EINVAL);
  198. /* Check for existing pm domains */
  199. for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
  200. if (pd[i].node_id == idx)
  201. goto done;
  202. }
  203. /**
  204. * Add index in empty node_id of power domain list as no existing
  205. * power domain found for current index.
  206. */
  207. for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
  208. if (pd[i].node_id == 0) {
  209. pd[i].node_id = idx;
  210. break;
  211. }
  212. }
  213. done:
  214. if (!genpd_data->domains[i] || i == ZYNQMP_NUM_DOMAINS)
  215. return ERR_PTR(-ENOENT);
  216. return genpd_data->domains[i];
  217. }
  218. static int zynqmp_gpd_probe(struct platform_device *pdev)
  219. {
  220. int i;
  221. struct genpd_onecell_data *zynqmp_pd_data;
  222. struct generic_pm_domain **domains;
  223. struct zynqmp_pm_domain *pd;
  224. struct device *dev = &pdev->dev;
  225. eemi_ops = zynqmp_pm_get_eemi_ops();
  226. if (IS_ERR(eemi_ops))
  227. return PTR_ERR(eemi_ops);
  228. pd = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*pd), GFP_KERNEL);
  229. if (!pd)
  230. return -ENOMEM;
  231. zynqmp_pd_data = devm_kzalloc(dev, sizeof(*zynqmp_pd_data), GFP_KERNEL);
  232. if (!zynqmp_pd_data)
  233. return -ENOMEM;
  234. zynqmp_pd_data->xlate = zynqmp_gpd_xlate;
  235. domains = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*domains),
  236. GFP_KERNEL);
  237. if (!domains)
  238. return -ENOMEM;
  239. for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++, pd++) {
  240. pd->node_id = 0;
  241. pd->gpd.name = kasprintf(GFP_KERNEL, "domain%d", i);
  242. pd->gpd.power_off = zynqmp_gpd_power_off;
  243. pd->gpd.power_on = zynqmp_gpd_power_on;
  244. pd->gpd.attach_dev = zynqmp_gpd_attach_dev;
  245. pd->gpd.detach_dev = zynqmp_gpd_detach_dev;
  246. domains[i] = &pd->gpd;
  247. /* Mark all PM domains as initially powered off */
  248. pm_genpd_init(&pd->gpd, NULL, true);
  249. }
  250. zynqmp_pd_data->domains = domains;
  251. zynqmp_pd_data->num_domains = ZYNQMP_NUM_DOMAINS;
  252. of_genpd_add_provider_onecell(dev->parent->of_node, zynqmp_pd_data);
  253. return 0;
  254. }
  255. static int zynqmp_gpd_remove(struct platform_device *pdev)
  256. {
  257. of_genpd_del_provider(pdev->dev.parent->of_node);
  258. return 0;
  259. }
  260. static struct platform_driver zynqmp_power_domain_driver = {
  261. .driver = {
  262. .name = "zynqmp_power_controller",
  263. },
  264. .probe = zynqmp_gpd_probe,
  265. .remove = zynqmp_gpd_remove,
  266. };
  267. module_platform_driver(zynqmp_power_domain_driver);
  268. MODULE_ALIAS("platform:zynqmp_power_controller");