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