ti_sci_pm_domains.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/mutex.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/slab.h>
  16. #include <linux/soc/ti/ti_sci_protocol.h>
  17. #include <dt-bindings/soc/ti,sci_pm_domain.h>
  18. /**
  19. * struct ti_sci_genpd_dev_data: holds data needed for every device attached
  20. * to this genpd
  21. * @idx: index of the device that identifies it with the system
  22. * control processor.
  23. * @exclusive: Permissions for exclusive request or shared request of the
  24. * device.
  25. */
  26. struct ti_sci_genpd_dev_data {
  27. int idx;
  28. u8 exclusive;
  29. };
  30. /**
  31. * struct ti_sci_pm_domain: TI specific data needed for power domain
  32. * @ti_sci: handle to TI SCI protocol driver that provides ops to
  33. * communicate with system control processor.
  34. * @dev: pointer to dev for the driver for devm allocs
  35. * @pd: generic_pm_domain for use with the genpd framework
  36. */
  37. struct ti_sci_pm_domain {
  38. const struct ti_sci_handle *ti_sci;
  39. struct device *dev;
  40. struct generic_pm_domain pd;
  41. };
  42. #define genpd_to_ti_sci_pd(gpd) container_of(gpd, struct ti_sci_pm_domain, pd)
  43. /**
  44. * ti_sci_dev_id(): get prepopulated ti_sci id from struct dev
  45. * @dev: pointer to device associated with this genpd
  46. *
  47. * Returns device_id stored from ti,sci_id property
  48. */
  49. static int ti_sci_dev_id(struct device *dev)
  50. {
  51. struct generic_pm_domain_data *genpd_data = dev_gpd_data(dev);
  52. struct ti_sci_genpd_dev_data *sci_dev_data = genpd_data->data;
  53. return sci_dev_data->idx;
  54. }
  55. static u8 is_ti_sci_dev_exclusive(struct device *dev)
  56. {
  57. struct generic_pm_domain_data *genpd_data = dev_gpd_data(dev);
  58. struct ti_sci_genpd_dev_data *sci_dev_data = genpd_data->data;
  59. return sci_dev_data->exclusive;
  60. }
  61. /**
  62. * ti_sci_dev_to_sci_handle(): get pointer to ti_sci_handle
  63. * @dev: pointer to device associated with this genpd
  64. *
  65. * Returns ti_sci_handle to be used to communicate with system
  66. * control processor.
  67. */
  68. static const struct ti_sci_handle *ti_sci_dev_to_sci_handle(struct device *dev)
  69. {
  70. struct generic_pm_domain *pd = pd_to_genpd(dev->pm_domain);
  71. struct ti_sci_pm_domain *ti_sci_genpd = genpd_to_ti_sci_pd(pd);
  72. return ti_sci_genpd->ti_sci;
  73. }
  74. /**
  75. * ti_sci_dev_start(): genpd device start hook called to turn device on
  76. * @dev: pointer to device associated with this genpd to be powered on
  77. */
  78. static int ti_sci_dev_start(struct device *dev)
  79. {
  80. const struct ti_sci_handle *ti_sci = ti_sci_dev_to_sci_handle(dev);
  81. int idx = ti_sci_dev_id(dev);
  82. if (is_ti_sci_dev_exclusive(dev))
  83. return ti_sci->ops.dev_ops.get_device_exclusive(ti_sci, idx);
  84. else
  85. return ti_sci->ops.dev_ops.get_device(ti_sci, idx);
  86. }
  87. /**
  88. * ti_sci_dev_stop(): genpd device stop hook called to turn device off
  89. * @dev: pointer to device associated with this genpd to be powered off
  90. */
  91. static int ti_sci_dev_stop(struct device *dev)
  92. {
  93. const struct ti_sci_handle *ti_sci = ti_sci_dev_to_sci_handle(dev);
  94. int idx = ti_sci_dev_id(dev);
  95. return ti_sci->ops.dev_ops.put_device(ti_sci, idx);
  96. }
  97. static int ti_sci_pd_attach_dev(struct generic_pm_domain *domain,
  98. struct device *dev)
  99. {
  100. struct device_node *np = dev->of_node;
  101. struct of_phandle_args pd_args;
  102. struct ti_sci_pm_domain *ti_sci_genpd = genpd_to_ti_sci_pd(domain);
  103. const struct ti_sci_handle *ti_sci = ti_sci_genpd->ti_sci;
  104. struct ti_sci_genpd_dev_data *sci_dev_data;
  105. struct generic_pm_domain_data *genpd_data;
  106. int idx, ret = 0;
  107. ret = of_parse_phandle_with_args(np, "power-domains",
  108. "#power-domain-cells", 0, &pd_args);
  109. if (ret < 0)
  110. return ret;
  111. if (pd_args.args_count != 1 && pd_args.args_count != 2)
  112. return -EINVAL;
  113. idx = pd_args.args[0];
  114. /*
  115. * Check the validity of the requested idx, if the index is not valid
  116. * the PMMC will return a NAK here and we will not allocate it.
  117. */
  118. ret = ti_sci->ops.dev_ops.is_valid(ti_sci, idx);
  119. if (ret)
  120. return -EINVAL;
  121. sci_dev_data = kzalloc(sizeof(*sci_dev_data), GFP_KERNEL);
  122. if (!sci_dev_data)
  123. return -ENOMEM;
  124. sci_dev_data->idx = idx;
  125. /* Enable the exclusive permissions by default */
  126. sci_dev_data->exclusive = TI_SCI_PD_EXCLUSIVE;
  127. if (pd_args.args_count == 2)
  128. sci_dev_data->exclusive = pd_args.args[1] & 0x1;
  129. genpd_data = dev_gpd_data(dev);
  130. genpd_data->data = sci_dev_data;
  131. return 0;
  132. }
  133. static void ti_sci_pd_detach_dev(struct generic_pm_domain *domain,
  134. struct device *dev)
  135. {
  136. struct generic_pm_domain_data *genpd_data = dev_gpd_data(dev);
  137. struct ti_sci_genpd_dev_data *sci_dev_data = genpd_data->data;
  138. kfree(sci_dev_data);
  139. genpd_data->data = NULL;
  140. }
  141. static const struct of_device_id ti_sci_pm_domain_matches[] = {
  142. { .compatible = "ti,sci-pm-domain", },
  143. { },
  144. };
  145. MODULE_DEVICE_TABLE(of, ti_sci_pm_domain_matches);
  146. static int ti_sci_pm_domain_probe(struct platform_device *pdev)
  147. {
  148. struct device *dev = &pdev->dev;
  149. struct device_node *np = dev->of_node;
  150. struct ti_sci_pm_domain *ti_sci_pd;
  151. int ret;
  152. ti_sci_pd = devm_kzalloc(dev, sizeof(*ti_sci_pd), GFP_KERNEL);
  153. if (!ti_sci_pd)
  154. return -ENOMEM;
  155. ti_sci_pd->ti_sci = devm_ti_sci_get_handle(dev);
  156. if (IS_ERR(ti_sci_pd->ti_sci))
  157. return PTR_ERR(ti_sci_pd->ti_sci);
  158. ti_sci_pd->dev = dev;
  159. ti_sci_pd->pd.name = "ti_sci_pd";
  160. ti_sci_pd->pd.attach_dev = ti_sci_pd_attach_dev;
  161. ti_sci_pd->pd.detach_dev = ti_sci_pd_detach_dev;
  162. ti_sci_pd->pd.dev_ops.start = ti_sci_dev_start;
  163. ti_sci_pd->pd.dev_ops.stop = ti_sci_dev_stop;
  164. pm_genpd_init(&ti_sci_pd->pd, NULL, true);
  165. ret = of_genpd_add_provider_simple(np, &ti_sci_pd->pd);
  166. return ret;
  167. }
  168. static struct platform_driver ti_sci_pm_domains_driver = {
  169. .probe = ti_sci_pm_domain_probe,
  170. .driver = {
  171. .name = "ti_sci_pm_domains",
  172. .of_match_table = ti_sci_pm_domain_matches,
  173. },
  174. };
  175. module_platform_driver(ti_sci_pm_domains_driver);
  176. MODULE_LICENSE("GPL v2");
  177. MODULE_DESCRIPTION("TI System Control Interface (SCI) Power Domain driver");
  178. MODULE_AUTHOR("Dave Gerlach");