xref: /linux/drivers/pci/pwrctrl/generic.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
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 
7 #include <linux/clk.h>
8 #include <linux/device.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/pci-pwrctrl.h>
13 #include <linux/platform_device.h>
14 #include <linux/pwrseq/consumer.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 
18 struct slot_pwrctrl {
19 	struct pci_pwrctrl pwrctrl;
20 	struct regulator_bulk_data *supplies;
21 	int num_supplies;
22 	struct clk *clk;
23 	struct pwrseq_desc *pwrseq;
24 };
25 
26 static int slot_pwrctrl_power_on(struct pci_pwrctrl *pwrctrl)
27 {
28 	struct slot_pwrctrl *slot = container_of(pwrctrl,
29 						struct slot_pwrctrl, pwrctrl);
30 	int ret;
31 
32 	if (slot->pwrseq) {
33 		pwrseq_power_on(slot->pwrseq);
34 		return 0;
35 	}
36 
37 	ret = regulator_bulk_enable(slot->num_supplies, slot->supplies);
38 	if (ret < 0) {
39 		dev_err(slot->pwrctrl.dev, "Failed to enable slot regulators\n");
40 		return ret;
41 	}
42 
43 	return clk_prepare_enable(slot->clk);
44 }
45 
46 static int slot_pwrctrl_power_off(struct pci_pwrctrl *pwrctrl)
47 {
48 	struct slot_pwrctrl *slot = container_of(pwrctrl,
49 						struct slot_pwrctrl, pwrctrl);
50 
51 	if (slot->pwrseq) {
52 		pwrseq_power_off(slot->pwrseq);
53 		return 0;
54 	}
55 
56 	regulator_bulk_disable(slot->num_supplies, slot->supplies);
57 	clk_disable_unprepare(slot->clk);
58 
59 	return 0;
60 }
61 
62 static void devm_slot_pwrctrl_release(void *data)
63 {
64 	struct slot_pwrctrl *slot = data;
65 
66 	regulator_bulk_free(slot->num_supplies, slot->supplies);
67 }
68 
69 static int slot_pwrctrl_probe(struct platform_device *pdev)
70 {
71 	struct slot_pwrctrl *slot;
72 	struct device *dev = &pdev->dev;
73 	int ret;
74 
75 	slot = devm_kzalloc(dev, sizeof(*slot), GFP_KERNEL);
76 	if (!slot)
77 		return -ENOMEM;
78 
79 	if (of_graph_is_present(dev_of_node(dev))) {
80 		slot->pwrseq = devm_pwrseq_get(dev, "pcie");
81 		if (IS_ERR(slot->pwrseq))
82 			return dev_err_probe(dev, PTR_ERR(slot->pwrseq),
83 				     "Failed to get the power sequencer\n");
84 
85 		goto skip_resources;
86 	}
87 
88 	ret = of_regulator_bulk_get_all(dev, dev_of_node(dev),
89 					&slot->supplies);
90 	if (ret < 0)
91 		return dev_err_probe(dev, ret, "Failed to get slot regulators\n");
92 
93 	slot->num_supplies = ret;
94 
95 	slot->clk = devm_clk_get_optional(dev, NULL);
96 	if (IS_ERR(slot->clk))
97 		return dev_err_probe(dev, PTR_ERR(slot->clk),
98 				     "Failed to enable slot clock\n");
99 
100 skip_resources:
101 	slot->pwrctrl.power_on = slot_pwrctrl_power_on;
102 	slot->pwrctrl.power_off = slot_pwrctrl_power_off;
103 
104 	ret = devm_add_action_or_reset(dev, devm_slot_pwrctrl_release, slot);
105 	if (ret)
106 		return ret;
107 
108 	pci_pwrctrl_init(&slot->pwrctrl, dev);
109 
110 	ret = devm_pci_pwrctrl_device_set_ready(dev, &slot->pwrctrl);
111 	if (ret)
112 		return dev_err_probe(dev, ret, "Failed to register pwrctrl driver\n");
113 
114 	return 0;
115 }
116 
117 static const struct of_device_id slot_pwrctrl_of_match[] = {
118 	{
119 		.compatible = "pciclass,0604",
120 	},
121 	/* Renesas UPD720201/UPD720202 USB 3.0 xHCI Host Controller */
122 	{
123 		.compatible = "pci1912,0014",
124 	},
125 	{ }
126 };
127 MODULE_DEVICE_TABLE(of, slot_pwrctrl_of_match);
128 
129 static struct platform_driver slot_pwrctrl_driver = {
130 	.driver = {
131 		.name = "pci-pwrctrl-slot",
132 		.of_match_table = slot_pwrctrl_of_match,
133 	},
134 	.probe = slot_pwrctrl_probe,
135 };
136 module_platform_driver(slot_pwrctrl_driver);
137 
138 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
139 MODULE_DESCRIPTION("Generic PCI Power Control driver for PCI Slots");
140 MODULE_LICENSE("GPL");
141