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