scpi_pm_domain.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SCPI Generic power domain support.
  3. *
  4. * Copyright (C) 2016 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/pm_domain.h>
  23. #include <linux/scpi_protocol.h>
  24. struct scpi_pm_domain {
  25. struct generic_pm_domain genpd;
  26. struct scpi_ops *ops;
  27. u32 domain;
  28. char name[30];
  29. };
  30. /*
  31. * These device power state values are not well-defined in the specification.
  32. * In case, different implementations use different values, we can make these
  33. * specific to compatibles rather than getting these values from device tree.
  34. */
  35. enum scpi_power_domain_state {
  36. SCPI_PD_STATE_ON = 0,
  37. SCPI_PD_STATE_OFF = 3,
  38. };
  39. #define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd)
  40. static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on)
  41. {
  42. int ret;
  43. enum scpi_power_domain_state state;
  44. if (power_on)
  45. state = SCPI_PD_STATE_ON;
  46. else
  47. state = SCPI_PD_STATE_OFF;
  48. ret = pd->ops->device_set_power_state(pd->domain, state);
  49. if (ret)
  50. return ret;
  51. return !(state == pd->ops->device_get_power_state(pd->domain));
  52. }
  53. static int scpi_pd_power_on(struct generic_pm_domain *domain)
  54. {
  55. struct scpi_pm_domain *pd = to_scpi_pd(domain);
  56. return scpi_pd_power(pd, true);
  57. }
  58. static int scpi_pd_power_off(struct generic_pm_domain *domain)
  59. {
  60. struct scpi_pm_domain *pd = to_scpi_pd(domain);
  61. return scpi_pd_power(pd, false);
  62. }
  63. static int scpi_pm_domain_probe(struct platform_device *pdev)
  64. {
  65. struct device *dev = &pdev->dev;
  66. struct device_node *np = dev->of_node;
  67. struct scpi_pm_domain *scpi_pd;
  68. struct genpd_onecell_data *scpi_pd_data;
  69. struct generic_pm_domain **domains;
  70. struct scpi_ops *scpi_ops;
  71. int ret, num_domains, i;
  72. scpi_ops = get_scpi_ops();
  73. if (!scpi_ops)
  74. return -EPROBE_DEFER;
  75. if (!np) {
  76. dev_err(dev, "device tree node not found\n");
  77. return -ENODEV;
  78. }
  79. if (!scpi_ops->device_set_power_state ||
  80. !scpi_ops->device_get_power_state) {
  81. dev_err(dev, "power domains not supported in the firmware\n");
  82. return -ENODEV;
  83. }
  84. ret = of_property_read_u32(np, "num-domains", &num_domains);
  85. if (ret) {
  86. dev_err(dev, "number of domains not found\n");
  87. return -EINVAL;
  88. }
  89. scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL);
  90. if (!scpi_pd)
  91. return -ENOMEM;
  92. scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL);
  93. if (!scpi_pd_data)
  94. return -ENOMEM;
  95. domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL);
  96. if (!domains)
  97. return -ENOMEM;
  98. for (i = 0; i < num_domains; i++, scpi_pd++) {
  99. domains[i] = &scpi_pd->genpd;
  100. scpi_pd->domain = i;
  101. scpi_pd->ops = scpi_ops;
  102. sprintf(scpi_pd->name, "%s.%d", np->name, i);
  103. scpi_pd->genpd.name = scpi_pd->name;
  104. scpi_pd->genpd.power_off = scpi_pd_power_off;
  105. scpi_pd->genpd.power_on = scpi_pd_power_on;
  106. /*
  107. * Treat all power domains as off at boot.
  108. *
  109. * The SCP firmware itself may have switched on some domains,
  110. * but for reference counting purpose, keep it this way.
  111. */
  112. pm_genpd_init(&scpi_pd->genpd, NULL, true);
  113. }
  114. scpi_pd_data->domains = domains;
  115. scpi_pd_data->num_domains = num_domains;
  116. of_genpd_add_provider_onecell(np, scpi_pd_data);
  117. return 0;
  118. }
  119. static const struct of_device_id scpi_power_domain_ids[] = {
  120. { .compatible = "arm,scpi-power-domains", },
  121. { /* sentinel */ }
  122. };
  123. MODULE_DEVICE_TABLE(of, scpi_power_domain_ids);
  124. static struct platform_driver scpi_power_domain_driver = {
  125. .driver = {
  126. .name = "scpi_power_domain",
  127. .of_match_table = scpi_power_domain_ids,
  128. },
  129. .probe = scpi_pm_domain_probe,
  130. };
  131. module_platform_driver(scpi_power_domain_driver);
  132. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  133. MODULE_DESCRIPTION("ARM SCPI power domain driver");
  134. MODULE_LICENSE("GPL v2");