slot.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 Linaro Ltd.
  4. * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/of_graph.h>
  11. #include <linux/pci-pwrctrl.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pwrseq/consumer.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/slab.h>
  16. struct slot_pwrctrl {
  17. struct pci_pwrctrl pwrctrl;
  18. struct regulator_bulk_data *supplies;
  19. int num_supplies;
  20. struct clk *clk;
  21. struct pwrseq_desc *pwrseq;
  22. };
  23. static int slot_pwrctrl_power_on(struct pci_pwrctrl *pwrctrl)
  24. {
  25. struct slot_pwrctrl *slot = container_of(pwrctrl,
  26. struct slot_pwrctrl, pwrctrl);
  27. int ret;
  28. if (slot->pwrseq) {
  29. pwrseq_power_on(slot->pwrseq);
  30. return 0;
  31. }
  32. ret = regulator_bulk_enable(slot->num_supplies, slot->supplies);
  33. if (ret < 0) {
  34. dev_err(slot->pwrctrl.dev, "Failed to enable slot regulators\n");
  35. return ret;
  36. }
  37. return clk_prepare_enable(slot->clk);
  38. }
  39. static int slot_pwrctrl_power_off(struct pci_pwrctrl *pwrctrl)
  40. {
  41. struct slot_pwrctrl *slot = container_of(pwrctrl,
  42. struct slot_pwrctrl, pwrctrl);
  43. if (slot->pwrseq) {
  44. pwrseq_power_off(slot->pwrseq);
  45. return 0;
  46. }
  47. regulator_bulk_disable(slot->num_supplies, slot->supplies);
  48. clk_disable_unprepare(slot->clk);
  49. return 0;
  50. }
  51. static void devm_slot_pwrctrl_release(void *data)
  52. {
  53. struct slot_pwrctrl *slot = data;
  54. regulator_bulk_free(slot->num_supplies, slot->supplies);
  55. }
  56. static int slot_pwrctrl_probe(struct platform_device *pdev)
  57. {
  58. struct slot_pwrctrl *slot;
  59. struct device *dev = &pdev->dev;
  60. int ret;
  61. slot = devm_kzalloc(dev, sizeof(*slot), GFP_KERNEL);
  62. if (!slot)
  63. return -ENOMEM;
  64. if (of_graph_is_present(dev_of_node(dev))) {
  65. slot->pwrseq = devm_pwrseq_get(dev, "pcie");
  66. if (IS_ERR(slot->pwrseq))
  67. return dev_err_probe(dev, PTR_ERR(slot->pwrseq),
  68. "Failed to get the power sequencer\n");
  69. goto skip_resources;
  70. }
  71. ret = of_regulator_bulk_get_all(dev, dev_of_node(dev),
  72. &slot->supplies);
  73. if (ret < 0) {
  74. dev_err_probe(dev, ret, "Failed to get slot regulators\n");
  75. return ret;
  76. }
  77. slot->num_supplies = ret;
  78. slot->clk = devm_clk_get_optional(dev, NULL);
  79. if (IS_ERR(slot->clk)) {
  80. return dev_err_probe(dev, PTR_ERR(slot->clk),
  81. "Failed to enable slot clock\n");
  82. }
  83. skip_resources:
  84. slot->pwrctrl.power_on = slot_pwrctrl_power_on;
  85. slot->pwrctrl.power_off = slot_pwrctrl_power_off;
  86. ret = devm_add_action_or_reset(dev, devm_slot_pwrctrl_release, slot);
  87. if (ret)
  88. return ret;
  89. pci_pwrctrl_init(&slot->pwrctrl, dev);
  90. ret = devm_pci_pwrctrl_device_set_ready(dev, &slot->pwrctrl);
  91. if (ret)
  92. return dev_err_probe(dev, ret, "Failed to register pwrctrl driver\n");
  93. return 0;
  94. }
  95. static const struct of_device_id slot_pwrctrl_of_match[] = {
  96. {
  97. .compatible = "pciclass,0604",
  98. },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(of, slot_pwrctrl_of_match);
  102. static struct platform_driver slot_pwrctrl_driver = {
  103. .driver = {
  104. .name = "pci-pwrctrl-slot",
  105. .of_match_table = slot_pwrctrl_of_match,
  106. },
  107. .probe = slot_pwrctrl_probe,
  108. };
  109. module_platform_driver(slot_pwrctrl_driver);
  110. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  111. MODULE_DESCRIPTION("Generic PCI Power Control driver for PCI Slots");
  112. MODULE_LICENSE("GPL");