raspberrypi-power.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 as
  5. * published by the Free Software Foundation.
  6. *
  7. * Authors:
  8. * Alexander Aring <aar@pengutronix.de>
  9. * Eric Anholt <eric@anholt.net>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_domain.h>
  15. #include <dt-bindings/power/raspberrypi-power.h>
  16. #include <soc/bcm2835/raspberrypi-firmware.h>
  17. /*
  18. * Firmware indices for the old power domains interface. Only a few
  19. * of them were actually implemented.
  20. */
  21. #define RPI_OLD_POWER_DOMAIN_USB 3
  22. #define RPI_OLD_POWER_DOMAIN_V3D 10
  23. struct rpi_power_domain {
  24. u32 domain;
  25. bool enabled;
  26. bool old_interface;
  27. struct generic_pm_domain base;
  28. struct rpi_firmware *fw;
  29. };
  30. struct rpi_power_domains {
  31. bool has_new_interface;
  32. struct genpd_onecell_data xlate;
  33. struct rpi_firmware *fw;
  34. struct rpi_power_domain domains[RPI_POWER_DOMAIN_COUNT];
  35. };
  36. /*
  37. * Packet definition used by RPI_FIRMWARE_SET_POWER_STATE and
  38. * RPI_FIRMWARE_SET_DOMAIN_STATE
  39. */
  40. struct rpi_power_domain_packet {
  41. u32 domain;
  42. u32 on;
  43. } __packet;
  44. /*
  45. * Asks the firmware to enable or disable power on a specific power
  46. * domain.
  47. */
  48. static int rpi_firmware_set_power(struct rpi_power_domain *rpi_domain, bool on)
  49. {
  50. struct rpi_power_domain_packet packet;
  51. packet.domain = rpi_domain->domain;
  52. packet.on = on;
  53. return rpi_firmware_property(rpi_domain->fw,
  54. rpi_domain->old_interface ?
  55. RPI_FIRMWARE_SET_POWER_STATE :
  56. RPI_FIRMWARE_SET_DOMAIN_STATE,
  57. &packet, sizeof(packet));
  58. }
  59. static int rpi_domain_off(struct generic_pm_domain *domain)
  60. {
  61. struct rpi_power_domain *rpi_domain =
  62. container_of(domain, struct rpi_power_domain, base);
  63. return rpi_firmware_set_power(rpi_domain, false);
  64. }
  65. static int rpi_domain_on(struct generic_pm_domain *domain)
  66. {
  67. struct rpi_power_domain *rpi_domain =
  68. container_of(domain, struct rpi_power_domain, base);
  69. return rpi_firmware_set_power(rpi_domain, true);
  70. }
  71. static void rpi_common_init_power_domain(struct rpi_power_domains *rpi_domains,
  72. int xlate_index, const char *name)
  73. {
  74. struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
  75. dom->fw = rpi_domains->fw;
  76. dom->base.name = name;
  77. dom->base.power_on = rpi_domain_on;
  78. dom->base.power_off = rpi_domain_off;
  79. /*
  80. * Treat all power domains as off at boot.
  81. *
  82. * The firmware itself may be keeping some domains on, but
  83. * from Linux's perspective all we control is the refcounts
  84. * that we give to the firmware, and we can't ask the firmware
  85. * to turn off something that we haven't ourselves turned on.
  86. */
  87. pm_genpd_init(&dom->base, NULL, true);
  88. rpi_domains->xlate.domains[xlate_index] = &dom->base;
  89. }
  90. static void rpi_init_power_domain(struct rpi_power_domains *rpi_domains,
  91. int xlate_index, const char *name)
  92. {
  93. struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
  94. if (!rpi_domains->has_new_interface)
  95. return;
  96. /* The DT binding index is the firmware's domain index minus one. */
  97. dom->domain = xlate_index + 1;
  98. rpi_common_init_power_domain(rpi_domains, xlate_index, name);
  99. }
  100. static void rpi_init_old_power_domain(struct rpi_power_domains *rpi_domains,
  101. int xlate_index, int domain,
  102. const char *name)
  103. {
  104. struct rpi_power_domain *dom = &rpi_domains->domains[xlate_index];
  105. dom->old_interface = true;
  106. dom->domain = domain;
  107. rpi_common_init_power_domain(rpi_domains, xlate_index, name);
  108. }
  109. /*
  110. * Detects whether the firmware supports the new power domains interface.
  111. *
  112. * The firmware doesn't actually return an error on an unknown tag,
  113. * and just skips over it, so we do the detection by putting an
  114. * unexpected value in the return field and checking if it was
  115. * unchanged.
  116. */
  117. static bool
  118. rpi_has_new_domain_support(struct rpi_power_domains *rpi_domains)
  119. {
  120. struct rpi_power_domain_packet packet;
  121. int ret;
  122. packet.domain = RPI_POWER_DOMAIN_ARM;
  123. packet.on = ~0;
  124. ret = rpi_firmware_property(rpi_domains->fw,
  125. RPI_FIRMWARE_GET_DOMAIN_STATE,
  126. &packet, sizeof(packet));
  127. return ret == 0 && packet.on != ~0;
  128. }
  129. static int rpi_power_probe(struct platform_device *pdev)
  130. {
  131. struct device_node *fw_np;
  132. struct device *dev = &pdev->dev;
  133. struct rpi_power_domains *rpi_domains;
  134. rpi_domains = devm_kzalloc(dev, sizeof(*rpi_domains), GFP_KERNEL);
  135. if (!rpi_domains)
  136. return -ENOMEM;
  137. rpi_domains->xlate.domains =
  138. devm_kzalloc(dev, sizeof(*rpi_domains->xlate.domains) *
  139. RPI_POWER_DOMAIN_COUNT, GFP_KERNEL);
  140. if (!rpi_domains->xlate.domains)
  141. return -ENOMEM;
  142. rpi_domains->xlate.num_domains = RPI_POWER_DOMAIN_COUNT;
  143. fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
  144. if (!fw_np) {
  145. dev_err(&pdev->dev, "no firmware node\n");
  146. return -ENODEV;
  147. }
  148. rpi_domains->fw = rpi_firmware_get(fw_np);
  149. of_node_put(fw_np);
  150. if (!rpi_domains->fw)
  151. return -EPROBE_DEFER;
  152. rpi_domains->has_new_interface =
  153. rpi_has_new_domain_support(rpi_domains);
  154. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C0, "I2C0");
  155. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C1, "I2C1");
  156. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_I2C2, "I2C2");
  157. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VIDEO_SCALER,
  158. "VIDEO_SCALER");
  159. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VPU1, "VPU1");
  160. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_HDMI, "HDMI");
  161. /*
  162. * Use the old firmware interface for USB power, so that we
  163. * can turn it on even if the firmware hasn't been updated.
  164. */
  165. rpi_init_old_power_domain(rpi_domains, RPI_POWER_DOMAIN_USB,
  166. RPI_OLD_POWER_DOMAIN_USB, "USB");
  167. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_VEC, "VEC");
  168. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_JPEG, "JPEG");
  169. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_H264, "H264");
  170. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_V3D, "V3D");
  171. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ISP, "ISP");
  172. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM0, "UNICAM0");
  173. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_UNICAM1, "UNICAM1");
  174. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2RX, "CCP2RX");
  175. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CSI2, "CSI2");
  176. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CPI, "CPI");
  177. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI0, "DSI0");
  178. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_DSI1, "DSI1");
  179. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_TRANSPOSER,
  180. "TRANSPOSER");
  181. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CCP2TX, "CCP2TX");
  182. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_CDP, "CDP");
  183. rpi_init_power_domain(rpi_domains, RPI_POWER_DOMAIN_ARM, "ARM");
  184. of_genpd_add_provider_onecell(dev->of_node, &rpi_domains->xlate);
  185. platform_set_drvdata(pdev, rpi_domains);
  186. return 0;
  187. }
  188. static const struct of_device_id rpi_power_of_match[] = {
  189. { .compatible = "raspberrypi,bcm2835-power", },
  190. {},
  191. };
  192. MODULE_DEVICE_TABLE(of, rpi_power_of_match);
  193. static struct platform_driver rpi_power_driver = {
  194. .driver = {
  195. .name = "raspberrypi-power",
  196. .of_match_table = rpi_power_of_match,
  197. },
  198. .probe = rpi_power_probe,
  199. };
  200. builtin_platform_driver(rpi_power_driver);
  201. MODULE_AUTHOR("Alexander Aring <aar@pengutronix.de>");
  202. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  203. MODULE_DESCRIPTION("Raspberry Pi power domain driver");
  204. MODULE_LICENSE("GPL v2");