ti_sci_pm_domains.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI SCI Generic Power Domain Driver
  4. *
  5. * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
  6. * J Keerthy <j-keerthy@ti.com>
  7. * Dave Gerlach <d-gerlach@ti.com>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_domain.h>
  14. #include <linux/slab.h>
  15. #include <linux/soc/ti/ti_sci_protocol.h>
  16. #include <dt-bindings/soc/ti,sci_pm_domain.h>
  17. /**
  18. * struct ti_sci_genpd_provider: holds common TI SCI genpd provider data
  19. * @ti_sci: handle to TI SCI protocol driver that provides ops to
  20. * communicate with system control processor.
  21. * @dev: pointer to dev for the driver for devm allocs
  22. * @pd_list: list of all the power domains on the device
  23. * @data: onecell data for genpd core
  24. */
  25. struct ti_sci_genpd_provider {
  26. const struct ti_sci_handle *ti_sci;
  27. struct device *dev;
  28. struct list_head pd_list;
  29. struct genpd_onecell_data data;
  30. };
  31. /**
  32. * struct ti_sci_pm_domain: TI specific data needed for power domain
  33. * @idx: index of the device that identifies it with the system
  34. * control processor.
  35. * @exclusive: Permissions for exclusive request or shared request of the
  36. * device.
  37. * @pd: generic_pm_domain for use with the genpd framework
  38. * @node: link for the genpd list
  39. * @parent: link to the parent TI SCI genpd provider
  40. */
  41. struct ti_sci_pm_domain {
  42. int idx;
  43. u8 exclusive;
  44. struct generic_pm_domain pd;
  45. struct list_head node;
  46. struct ti_sci_genpd_provider *parent;
  47. };
  48. #define genpd_to_ti_sci_pd(gpd) container_of(gpd, struct ti_sci_pm_domain, pd)
  49. /*
  50. * ti_sci_pd_power_off(): genpd power down hook
  51. * @domain: pointer to the powerdomain to power off
  52. */
  53. static int ti_sci_pd_power_off(struct generic_pm_domain *domain)
  54. {
  55. struct ti_sci_pm_domain *pd = genpd_to_ti_sci_pd(domain);
  56. const struct ti_sci_handle *ti_sci = pd->parent->ti_sci;
  57. return ti_sci->ops.dev_ops.put_device(ti_sci, pd->idx);
  58. }
  59. /*
  60. * ti_sci_pd_power_on(): genpd power up hook
  61. * @domain: pointer to the powerdomain to power on
  62. */
  63. static int ti_sci_pd_power_on(struct generic_pm_domain *domain)
  64. {
  65. struct ti_sci_pm_domain *pd = genpd_to_ti_sci_pd(domain);
  66. const struct ti_sci_handle *ti_sci = pd->parent->ti_sci;
  67. if (pd->exclusive)
  68. return ti_sci->ops.dev_ops.get_device_exclusive(ti_sci,
  69. pd->idx);
  70. else
  71. return ti_sci->ops.dev_ops.get_device(ti_sci, pd->idx);
  72. }
  73. /*
  74. * ti_sci_pd_xlate(): translation service for TI SCI genpds
  75. * @genpdspec: DT identification data for the genpd
  76. * @data: genpd core data for all the powerdomains on the device
  77. */
  78. static struct generic_pm_domain *ti_sci_pd_xlate(
  79. struct of_phandle_args *genpdspec,
  80. void *data)
  81. {
  82. struct genpd_onecell_data *genpd_data = data;
  83. unsigned int idx = genpdspec->args[0];
  84. if (genpdspec->args_count != 1 && genpdspec->args_count != 2)
  85. return ERR_PTR(-EINVAL);
  86. if (idx >= genpd_data->num_domains) {
  87. pr_err("%s: invalid domain index %u\n", __func__, idx);
  88. return ERR_PTR(-EINVAL);
  89. }
  90. if (!genpd_data->domains[idx])
  91. return ERR_PTR(-ENOENT);
  92. genpd_to_ti_sci_pd(genpd_data->domains[idx])->exclusive =
  93. genpdspec->args[1];
  94. return genpd_data->domains[idx];
  95. }
  96. static const struct of_device_id ti_sci_pm_domain_matches[] = {
  97. { .compatible = "ti,sci-pm-domain", },
  98. { },
  99. };
  100. MODULE_DEVICE_TABLE(of, ti_sci_pm_domain_matches);
  101. static int ti_sci_pm_domain_probe(struct platform_device *pdev)
  102. {
  103. struct device *dev = &pdev->dev;
  104. struct ti_sci_genpd_provider *pd_provider;
  105. struct ti_sci_pm_domain *pd;
  106. struct device_node *np = NULL;
  107. struct of_phandle_args args;
  108. int ret;
  109. u32 max_id = 0;
  110. int index;
  111. pd_provider = devm_kzalloc(dev, sizeof(*pd_provider), GFP_KERNEL);
  112. if (!pd_provider)
  113. return -ENOMEM;
  114. pd_provider->ti_sci = devm_ti_sci_get_handle(dev);
  115. if (IS_ERR(pd_provider->ti_sci))
  116. return PTR_ERR(pd_provider->ti_sci);
  117. pd_provider->dev = dev;
  118. INIT_LIST_HEAD(&pd_provider->pd_list);
  119. /* Find highest device ID used for power domains */
  120. while (1) {
  121. np = of_find_node_with_property(np, "power-domains");
  122. if (!np)
  123. break;
  124. index = 0;
  125. while (1) {
  126. ret = of_parse_phandle_with_args(np, "power-domains",
  127. "#power-domain-cells",
  128. index, &args);
  129. if (ret)
  130. break;
  131. if (args.args_count >= 1 && args.np == dev->of_node) {
  132. if (args.args[0] > max_id)
  133. max_id = args.args[0];
  134. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  135. if (!pd)
  136. return -ENOMEM;
  137. pd->pd.name = devm_kasprintf(dev, GFP_KERNEL,
  138. "pd:%d",
  139. args.args[0]);
  140. if (!pd->pd.name)
  141. return -ENOMEM;
  142. pd->pd.power_off = ti_sci_pd_power_off;
  143. pd->pd.power_on = ti_sci_pd_power_on;
  144. pd->idx = args.args[0];
  145. pd->parent = pd_provider;
  146. pm_genpd_init(&pd->pd, NULL, true);
  147. list_add(&pd->node, &pd_provider->pd_list);
  148. }
  149. index++;
  150. }
  151. }
  152. pd_provider->data.domains =
  153. devm_kcalloc(dev, max_id + 1,
  154. sizeof(*pd_provider->data.domains),
  155. GFP_KERNEL);
  156. pd_provider->data.num_domains = max_id + 1;
  157. pd_provider->data.xlate = ti_sci_pd_xlate;
  158. list_for_each_entry(pd, &pd_provider->pd_list, node)
  159. pd_provider->data.domains[pd->idx] = &pd->pd;
  160. return of_genpd_add_provider_onecell(dev->of_node, &pd_provider->data);
  161. }
  162. static struct platform_driver ti_sci_pm_domains_driver = {
  163. .probe = ti_sci_pm_domain_probe,
  164. .driver = {
  165. .name = "ti_sci_pm_domains",
  166. .of_match_table = ti_sci_pm_domain_matches,
  167. },
  168. };
  169. module_platform_driver(ti_sci_pm_domains_driver);
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_DESCRIPTION("TI System Control Interface (SCI) Power Domain driver");
  172. MODULE_AUTHOR("Dave Gerlach");