pci-pwrctrl-pwrseq.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 Linaro Ltd.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/module.h>
  8. #include <linux/pci-pwrctrl.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/property.h>
  11. #include <linux/pwrseq/consumer.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. struct pwrseq_pwrctrl {
  15. struct pci_pwrctrl pwrctrl;
  16. struct pwrseq_desc *pwrseq;
  17. };
  18. struct pwrseq_pwrctrl_pdata {
  19. const char *target;
  20. /*
  21. * Called before doing anything else to perform device-specific
  22. * verification between requesting the power sequencing handle.
  23. */
  24. int (*validate_device)(struct device *dev);
  25. };
  26. static int pwrseq_pwrctrl_qcm_wcn_validate_device(struct device *dev)
  27. {
  28. /*
  29. * Old device trees for some platforms already define wifi nodes for
  30. * the WCN family of chips since before power sequencing was added
  31. * upstream.
  32. *
  33. * These nodes don't consume the regulator outputs from the PMU, and
  34. * if we allow this driver to bind to one of such "incomplete" nodes,
  35. * we'll see a kernel log error about the indefinite probe deferral.
  36. *
  37. * Check the existence of the regulator supply that exists on all
  38. * WCN models before moving forward.
  39. */
  40. if (!device_property_present(dev, "vddaon-supply"))
  41. return -ENODEV;
  42. return 0;
  43. }
  44. static const struct pwrseq_pwrctrl_pdata pwrseq_pwrctrl_qcom_wcn_pdata = {
  45. .target = "wlan",
  46. .validate_device = pwrseq_pwrctrl_qcm_wcn_validate_device,
  47. };
  48. static int pwrseq_pwrctrl_power_on(struct pci_pwrctrl *pwrctrl)
  49. {
  50. struct pwrseq_pwrctrl *pwrseq = container_of(pwrctrl,
  51. struct pwrseq_pwrctrl, pwrctrl);
  52. return pwrseq_power_on(pwrseq->pwrseq);
  53. }
  54. static int pwrseq_pwrctrl_power_off(struct pci_pwrctrl *pwrctrl)
  55. {
  56. struct pwrseq_pwrctrl *pwrseq = container_of(pwrctrl,
  57. struct pwrseq_pwrctrl, pwrctrl);
  58. return pwrseq_power_off(pwrseq->pwrseq);
  59. }
  60. static int pwrseq_pwrctrl_probe(struct platform_device *pdev)
  61. {
  62. const struct pwrseq_pwrctrl_pdata *pdata;
  63. struct pwrseq_pwrctrl *pwrseq;
  64. struct device *dev = &pdev->dev;
  65. int ret;
  66. pdata = device_get_match_data(dev);
  67. if (!pdata || !pdata->target)
  68. return -EINVAL;
  69. if (pdata->validate_device) {
  70. ret = pdata->validate_device(dev);
  71. if (ret)
  72. return ret;
  73. }
  74. pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
  75. if (!pwrseq)
  76. return -ENOMEM;
  77. pwrseq->pwrseq = devm_pwrseq_get(dev, pdata->target);
  78. if (IS_ERR(pwrseq->pwrseq))
  79. return dev_err_probe(dev, PTR_ERR(pwrseq->pwrseq),
  80. "Failed to get the power sequencer\n");
  81. pwrseq->pwrctrl.power_on = pwrseq_pwrctrl_power_on;
  82. pwrseq->pwrctrl.power_off = pwrseq_pwrctrl_power_off;
  83. pci_pwrctrl_init(&pwrseq->pwrctrl, dev);
  84. ret = devm_pci_pwrctrl_device_set_ready(dev, &pwrseq->pwrctrl);
  85. if (ret)
  86. return dev_err_probe(dev, ret,
  87. "Failed to register the pwrctrl wrapper\n");
  88. return 0;
  89. }
  90. static const struct of_device_id pwrseq_pwrctrl_of_match[] = {
  91. {
  92. /* ATH11K in QCA6390 package. */
  93. .compatible = "pci17cb,1101",
  94. .data = &pwrseq_pwrctrl_qcom_wcn_pdata,
  95. },
  96. {
  97. /* ATH11K in WCN6855 package. */
  98. .compatible = "pci17cb,1103",
  99. .data = &pwrseq_pwrctrl_qcom_wcn_pdata,
  100. },
  101. {
  102. /* ATH12K in WCN7850 package. */
  103. .compatible = "pci17cb,1107",
  104. .data = &pwrseq_pwrctrl_qcom_wcn_pdata,
  105. },
  106. { }
  107. };
  108. MODULE_DEVICE_TABLE(of, pwrseq_pwrctrl_of_match);
  109. static struct platform_driver pwrseq_pwrctrl_driver = {
  110. .driver = {
  111. .name = "pci-pwrctrl-pwrseq",
  112. .of_match_table = pwrseq_pwrctrl_of_match,
  113. },
  114. .probe = pwrseq_pwrctrl_probe,
  115. };
  116. module_platform_driver(pwrseq_pwrctrl_driver);
  117. MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
  118. MODULE_DESCRIPTION("Generic PCI Power Control module for power sequenced devices");
  119. MODULE_LICENSE("GPL");