zynqmp_pm_domains.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ZynqMP Generic PM domain support
  4. *
  5. * Copyright (C) 2015-2019 Xilinx, Inc.
  6. *
  7. * Davorin Mista <[email protected]>
  8. * Jolly Shah <[email protected]>
  9. * Rajan Vaja <[email protected]>
  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. static int min_capability;
  21. /**
  22. * struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain
  23. * @gpd: Generic power domain
  24. * @node_id: PM node ID corresponding to device inside PM domain
  25. * @requested: The PM node mapped to the PM domain has been requested
  26. */
  27. struct zynqmp_pm_domain {
  28. struct generic_pm_domain gpd;
  29. u32 node_id;
  30. bool requested;
  31. };
  32. #define to_zynqmp_pm_domain(pm_domain) \
  33. container_of(pm_domain, struct zynqmp_pm_domain, gpd)
  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. struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain);
  66. int ret;
  67. ret = zynqmp_pm_set_requirement(pd->node_id,
  68. ZYNQMP_PM_CAPABILITY_ACCESS,
  69. ZYNQMP_PM_MAX_QOS,
  70. ZYNQMP_PM_REQUEST_ACK_BLOCKING);
  71. if (ret) {
  72. dev_err(&domain->dev,
  73. "failed to set requirement to 0x%x for PM node id %d: %d\n",
  74. ZYNQMP_PM_CAPABILITY_ACCESS, pd->node_id, ret);
  75. return ret;
  76. }
  77. dev_dbg(&domain->dev, "set requirement to 0x%x for PM node id %d\n",
  78. ZYNQMP_PM_CAPABILITY_ACCESS, pd->node_id);
  79. return 0;
  80. }
  81. /**
  82. * zynqmp_gpd_power_off() - Power off PM domain
  83. * @domain: Generic PM domain
  84. *
  85. * This function is called after devices inside a PM domain are suspended, to
  86. * power off PM domain.
  87. *
  88. * Return: 0 on success, error code otherwise
  89. */
  90. static int zynqmp_gpd_power_off(struct generic_pm_domain *domain)
  91. {
  92. struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain);
  93. int ret;
  94. struct pm_domain_data *pdd, *tmp;
  95. u32 capabilities = min_capability;
  96. bool may_wakeup;
  97. /* If domain is already released there is nothing to be done */
  98. if (!pd->requested) {
  99. dev_dbg(&domain->dev, "PM node id %d is already released\n",
  100. pd->node_id);
  101. return 0;
  102. }
  103. list_for_each_entry_safe(pdd, tmp, &domain->dev_list, list_node) {
  104. /* If device is in wakeup path, set capability to WAKEUP */
  105. may_wakeup = zynqmp_gpd_is_active_wakeup_path(pdd->dev, NULL);
  106. if (may_wakeup) {
  107. dev_dbg(pdd->dev, "device is in wakeup path in %s\n",
  108. domain->name);
  109. capabilities = ZYNQMP_PM_CAPABILITY_WAKEUP;
  110. break;
  111. }
  112. }
  113. ret = zynqmp_pm_set_requirement(pd->node_id, capabilities, 0,
  114. ZYNQMP_PM_REQUEST_ACK_NO);
  115. if (ret) {
  116. dev_err(&domain->dev,
  117. "failed to set requirement to 0x%x for PM node id %d: %d\n",
  118. capabilities, pd->node_id, ret);
  119. return ret;
  120. }
  121. dev_dbg(&domain->dev, "set requirement to 0x%x for PM node id %d\n",
  122. capabilities, pd->node_id);
  123. return 0;
  124. }
  125. /**
  126. * zynqmp_gpd_attach_dev() - Attach device to the PM domain
  127. * @domain: Generic PM domain
  128. * @dev: Device to attach
  129. *
  130. * Return: 0 on success, error code otherwise
  131. */
  132. static int zynqmp_gpd_attach_dev(struct generic_pm_domain *domain,
  133. struct device *dev)
  134. {
  135. struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain);
  136. struct device_link *link;
  137. int ret;
  138. link = device_link_add(dev, &domain->dev, DL_FLAG_SYNC_STATE_ONLY);
  139. if (!link)
  140. dev_dbg(&domain->dev, "failed to create device link for %s\n",
  141. dev_name(dev));
  142. /* If this is not the first device to attach there is nothing to do */
  143. if (domain->device_count)
  144. return 0;
  145. ret = zynqmp_pm_request_node(pd->node_id, 0, 0,
  146. ZYNQMP_PM_REQUEST_ACK_BLOCKING);
  147. if (ret) {
  148. dev_err(&domain->dev, "%s request failed for node %d: %d\n",
  149. domain->name, pd->node_id, ret);
  150. return ret;
  151. }
  152. pd->requested = true;
  153. dev_dbg(&domain->dev, "%s requested PM node id %d\n",
  154. dev_name(dev), pd->node_id);
  155. return 0;
  156. }
  157. /**
  158. * zynqmp_gpd_detach_dev() - Detach device from the PM domain
  159. * @domain: Generic PM domain
  160. * @dev: Device to detach
  161. */
  162. static void zynqmp_gpd_detach_dev(struct generic_pm_domain *domain,
  163. struct device *dev)
  164. {
  165. struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain);
  166. int ret;
  167. /* If this is not the last device to detach there is nothing to do */
  168. if (domain->device_count)
  169. return;
  170. ret = zynqmp_pm_release_node(pd->node_id);
  171. if (ret) {
  172. dev_err(&domain->dev, "failed to release PM node id %d: %d\n",
  173. pd->node_id, ret);
  174. return;
  175. }
  176. pd->requested = false;
  177. dev_dbg(&domain->dev, "%s released PM node id %d\n",
  178. dev_name(dev), pd->node_id);
  179. }
  180. static struct generic_pm_domain *zynqmp_gpd_xlate
  181. (struct of_phandle_args *genpdspec, void *data)
  182. {
  183. struct genpd_onecell_data *genpd_data = data;
  184. unsigned int i, idx = genpdspec->args[0];
  185. struct zynqmp_pm_domain *pd;
  186. pd = to_zynqmp_pm_domain(genpd_data->domains[0]);
  187. if (genpdspec->args_count != 1)
  188. return ERR_PTR(-EINVAL);
  189. /* Check for existing pm domains */
  190. for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
  191. if (pd[i].node_id == idx)
  192. goto done;
  193. }
  194. /**
  195. * Add index in empty node_id of power domain list as no existing
  196. * power domain found for current index.
  197. */
  198. for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) {
  199. if (pd[i].node_id == 0) {
  200. pd[i].node_id = idx;
  201. break;
  202. }
  203. }
  204. done:
  205. if (!genpd_data->domains[i] || i == ZYNQMP_NUM_DOMAINS)
  206. return ERR_PTR(-ENOENT);
  207. return genpd_data->domains[i];
  208. }
  209. static int zynqmp_gpd_probe(struct platform_device *pdev)
  210. {
  211. int i;
  212. struct genpd_onecell_data *zynqmp_pd_data;
  213. struct generic_pm_domain **domains;
  214. struct zynqmp_pm_domain *pd;
  215. struct device *dev = &pdev->dev;
  216. pd = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*pd), GFP_KERNEL);
  217. if (!pd)
  218. return -ENOMEM;
  219. zynqmp_pd_data = devm_kzalloc(dev, sizeof(*zynqmp_pd_data), GFP_KERNEL);
  220. if (!zynqmp_pd_data)
  221. return -ENOMEM;
  222. zynqmp_pd_data->xlate = zynqmp_gpd_xlate;
  223. domains = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*domains),
  224. GFP_KERNEL);
  225. if (!domains)
  226. return -ENOMEM;
  227. if (!of_device_is_compatible(dev->parent->of_node,
  228. "xlnx,zynqmp-firmware"))
  229. min_capability = ZYNQMP_PM_CAPABILITY_UNUSABLE;
  230. for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++, pd++) {
  231. pd->node_id = 0;
  232. pd->gpd.name = kasprintf(GFP_KERNEL, "domain%d", i);
  233. pd->gpd.power_off = zynqmp_gpd_power_off;
  234. pd->gpd.power_on = zynqmp_gpd_power_on;
  235. pd->gpd.attach_dev = zynqmp_gpd_attach_dev;
  236. pd->gpd.detach_dev = zynqmp_gpd_detach_dev;
  237. domains[i] = &pd->gpd;
  238. /* Mark all PM domains as initially powered off */
  239. pm_genpd_init(&pd->gpd, NULL, true);
  240. }
  241. zynqmp_pd_data->domains = domains;
  242. zynqmp_pd_data->num_domains = ZYNQMP_NUM_DOMAINS;
  243. of_genpd_add_provider_onecell(dev->parent->of_node, zynqmp_pd_data);
  244. return 0;
  245. }
  246. static int zynqmp_gpd_remove(struct platform_device *pdev)
  247. {
  248. of_genpd_del_provider(pdev->dev.parent->of_node);
  249. return 0;
  250. }
  251. static void zynqmp_gpd_sync_state(struct device *dev)
  252. {
  253. int ret;
  254. ret = zynqmp_pm_init_finalize();
  255. if (ret)
  256. dev_warn(dev, "failed to release power management to firmware\n");
  257. }
  258. static struct platform_driver zynqmp_power_domain_driver = {
  259. .driver = {
  260. .name = "zynqmp_power_controller",
  261. .sync_state = zynqmp_gpd_sync_state,
  262. },
  263. .probe = zynqmp_gpd_probe,
  264. .remove = zynqmp_gpd_remove,
  265. };
  266. module_platform_driver(zynqmp_power_domain_driver);
  267. MODULE_ALIAS("platform:zynqmp_power_controller");