owl-sps.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Actions Semi Owl Smart Power System (SPS)
  3. *
  4. * Copyright 2012 Actions Semi Inc.
  5. * Author: Actions Semi, Inc.
  6. *
  7. * Copyright (c) 2017 Andreas Färber
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/of_address.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/pm_domain.h>
  17. #include <linux/soc/actions/owl-sps.h>
  18. #include <dt-bindings/power/owl-s500-powergate.h>
  19. struct owl_sps_domain_info {
  20. const char *name;
  21. int pwr_bit;
  22. int ack_bit;
  23. unsigned int genpd_flags;
  24. };
  25. struct owl_sps_info {
  26. unsigned num_domains;
  27. const struct owl_sps_domain_info *domains;
  28. };
  29. struct owl_sps {
  30. struct device *dev;
  31. const struct owl_sps_info *info;
  32. void __iomem *base;
  33. struct genpd_onecell_data genpd_data;
  34. struct generic_pm_domain *domains[];
  35. };
  36. #define to_owl_pd(gpd) container_of(gpd, struct owl_sps_domain, genpd)
  37. struct owl_sps_domain {
  38. struct generic_pm_domain genpd;
  39. const struct owl_sps_domain_info *info;
  40. struct owl_sps *sps;
  41. };
  42. static int owl_sps_set_power(struct owl_sps_domain *pd, bool enable)
  43. {
  44. u32 pwr_mask, ack_mask;
  45. ack_mask = BIT(pd->info->ack_bit);
  46. pwr_mask = BIT(pd->info->pwr_bit);
  47. return owl_sps_set_pg(pd->sps->base, pwr_mask, ack_mask, enable);
  48. }
  49. static int owl_sps_power_on(struct generic_pm_domain *domain)
  50. {
  51. struct owl_sps_domain *pd = to_owl_pd(domain);
  52. dev_dbg(pd->sps->dev, "%s power on", pd->info->name);
  53. return owl_sps_set_power(pd, true);
  54. }
  55. static int owl_sps_power_off(struct generic_pm_domain *domain)
  56. {
  57. struct owl_sps_domain *pd = to_owl_pd(domain);
  58. dev_dbg(pd->sps->dev, "%s power off", pd->info->name);
  59. return owl_sps_set_power(pd, false);
  60. }
  61. static int owl_sps_init_domain(struct owl_sps *sps, int index)
  62. {
  63. struct owl_sps_domain *pd;
  64. pd = devm_kzalloc(sps->dev, sizeof(*pd), GFP_KERNEL);
  65. if (!pd)
  66. return -ENOMEM;
  67. pd->info = &sps->info->domains[index];
  68. pd->sps = sps;
  69. pd->genpd.name = pd->info->name;
  70. pd->genpd.power_on = owl_sps_power_on;
  71. pd->genpd.power_off = owl_sps_power_off;
  72. pd->genpd.flags = pd->info->genpd_flags;
  73. pm_genpd_init(&pd->genpd, NULL, false);
  74. sps->genpd_data.domains[index] = &pd->genpd;
  75. return 0;
  76. }
  77. static int owl_sps_probe(struct platform_device *pdev)
  78. {
  79. const struct of_device_id *match;
  80. const struct owl_sps_info *sps_info;
  81. struct owl_sps *sps;
  82. int i, ret;
  83. if (!pdev->dev.of_node) {
  84. dev_err(&pdev->dev, "no device node\n");
  85. return -ENODEV;
  86. }
  87. match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
  88. if (!match || !match->data) {
  89. dev_err(&pdev->dev, "unknown compatible or missing data\n");
  90. return -EINVAL;
  91. }
  92. sps_info = match->data;
  93. sps = devm_kzalloc(&pdev->dev, sizeof(*sps) +
  94. sps_info->num_domains * sizeof(sps->domains[0]),
  95. GFP_KERNEL);
  96. if (!sps)
  97. return -ENOMEM;
  98. sps->base = of_io_request_and_map(pdev->dev.of_node, 0, "owl-sps");
  99. if (IS_ERR(sps->base)) {
  100. dev_err(&pdev->dev, "failed to map sps registers\n");
  101. return PTR_ERR(sps->base);
  102. }
  103. sps->dev = &pdev->dev;
  104. sps->info = sps_info;
  105. sps->genpd_data.domains = sps->domains;
  106. sps->genpd_data.num_domains = sps_info->num_domains;
  107. for (i = 0; i < sps_info->num_domains; i++) {
  108. ret = owl_sps_init_domain(sps, i);
  109. if (ret)
  110. return ret;
  111. }
  112. ret = of_genpd_add_provider_onecell(pdev->dev.of_node, &sps->genpd_data);
  113. if (ret) {
  114. dev_err(&pdev->dev, "failed to add provider (%d)", ret);
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. static const struct owl_sps_domain_info s500_sps_domains[] = {
  120. [S500_PD_VDE] = {
  121. .name = "VDE",
  122. .pwr_bit = 0,
  123. .ack_bit = 16,
  124. },
  125. [S500_PD_VCE_SI] = {
  126. .name = "VCE_SI",
  127. .pwr_bit = 1,
  128. .ack_bit = 17,
  129. },
  130. [S500_PD_USB2_1] = {
  131. .name = "USB2_1",
  132. .pwr_bit = 2,
  133. .ack_bit = 18,
  134. },
  135. [S500_PD_CPU2] = {
  136. .name = "CPU2",
  137. .pwr_bit = 5,
  138. .ack_bit = 21,
  139. .genpd_flags = GENPD_FLAG_ALWAYS_ON,
  140. },
  141. [S500_PD_CPU3] = {
  142. .name = "CPU3",
  143. .pwr_bit = 6,
  144. .ack_bit = 22,
  145. .genpd_flags = GENPD_FLAG_ALWAYS_ON,
  146. },
  147. [S500_PD_DMA] = {
  148. .name = "DMA",
  149. .pwr_bit = 8,
  150. .ack_bit = 12,
  151. },
  152. [S500_PD_DS] = {
  153. .name = "DS",
  154. .pwr_bit = 9,
  155. .ack_bit = 13,
  156. },
  157. [S500_PD_USB3] = {
  158. .name = "USB3",
  159. .pwr_bit = 10,
  160. .ack_bit = 14,
  161. },
  162. [S500_PD_USB2_0] = {
  163. .name = "USB2_0",
  164. .pwr_bit = 11,
  165. .ack_bit = 15,
  166. },
  167. };
  168. static const struct owl_sps_info s500_sps_info = {
  169. .num_domains = ARRAY_SIZE(s500_sps_domains),
  170. .domains = s500_sps_domains,
  171. };
  172. static const struct of_device_id owl_sps_of_matches[] = {
  173. { .compatible = "actions,s500-sps", .data = &s500_sps_info },
  174. { }
  175. };
  176. static struct platform_driver owl_sps_platform_driver = {
  177. .probe = owl_sps_probe,
  178. .driver = {
  179. .name = "owl-sps",
  180. .of_match_table = owl_sps_of_matches,
  181. .suppress_bind_attrs = true,
  182. },
  183. };
  184. static int __init owl_sps_init(void)
  185. {
  186. return platform_driver_register(&owl_sps_platform_driver);
  187. }
  188. postcore_initcall(owl_sps_init);