1 --- a/drivers/clk/clk-devres.c
2 +++ b/drivers/clk/clk-devres.c
3 @@ -34,6 +34,17 @@ struct clk *devm_clk_get(struct device *
5 EXPORT_SYMBOL(devm_clk_get);
7 +struct clk *devm_clk_get_optional(struct device *dev, const char *id)
9 + struct clk *clk = devm_clk_get(dev, id);
11 + if (clk == ERR_PTR(-ENOENT))
16 +EXPORT_SYMBOL(devm_clk_get_optional);
18 struct clk_bulk_devres {
19 struct clk_bulk_data *clks;
21 --- a/drivers/pci/controller/pcie-mediatek.c
22 +++ b/drivers/pci/controller/pcie-mediatek.c
24 #include <linux/irqdomain.h>
25 #include <linux/kernel.h>
26 #include <linux/msi.h>
27 +#include <linux/module.h>
28 #include <linux/of_address.h>
29 #include <linux/of_pci.h>
30 #include <linux/of_platform.h>
31 @@ -162,6 +163,7 @@ struct mtk_pcie_soc {
32 * @phy: pointer to PHY control block
36 * @irq_domain: legacy INTx IRQ domain
37 * @inner_domain: inner IRQ domain
38 * @msi_domain: MSI IRQ domain
39 @@ -182,6 +184,7 @@ struct mtk_pcie_port {
44 struct irq_domain *irq_domain;
45 struct irq_domain *inner_domain;
46 struct irq_domain *msi_domain;
47 @@ -225,10 +228,8 @@ static void mtk_pcie_subsys_powerdown(st
49 clk_disable_unprepare(pcie->free_ck);
51 - if (dev->pm_domain) {
52 - pm_runtime_put_sync(dev);
53 - pm_runtime_disable(dev);
55 + pm_runtime_put_sync(dev);
56 + pm_runtime_disable(dev);
59 static void mtk_pcie_port_free(struct mtk_pcie_port *port)
60 @@ -532,6 +533,27 @@ static void mtk_pcie_enable_msi(struct m
61 writel(val, port->base + PCIE_INT_MASK);
64 +static void mtk_pcie_irq_teardown(struct mtk_pcie *pcie)
66 + struct mtk_pcie_port *port, *tmp;
68 + list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
69 + irq_set_chained_handler_and_data(port->irq, NULL, NULL);
71 + if (port->irq_domain)
72 + irq_domain_remove(port->irq_domain);
74 + if (IS_ENABLED(CONFIG_PCI_MSI)) {
75 + if (port->msi_domain)
76 + irq_domain_remove(port->msi_domain);
77 + if (port->inner_domain)
78 + irq_domain_remove(port->inner_domain);
81 + irq_dispose_mapping(port->irq);
85 static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
86 irq_hw_number_t hwirq)
88 @@ -561,6 +583,7 @@ static int mtk_pcie_init_irq_domain(stru
90 port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
91 &intx_domain_ops, port);
92 + of_node_put(pcie_intc_node);
93 if (!port->irq_domain) {
94 dev_err(dev, "failed to get INTx IRQ domain\n");
96 @@ -622,7 +645,7 @@ static int mtk_pcie_setup_irq(struct mtk
97 struct mtk_pcie *pcie = port->pcie;
98 struct device *dev = pcie->dev;
99 struct platform_device *pdev = to_platform_device(dev);
103 err = mtk_pcie_init_irq_domain(port, node);
105 @@ -630,8 +653,9 @@ static int mtk_pcie_setup_irq(struct mtk
109 - irq = platform_get_irq(pdev, port->slot);
110 - irq_set_chained_handler_and_data(irq, mtk_pcie_intr_handler, port);
111 + port->irq = platform_get_irq(pdev, port->slot);
112 + irq_set_chained_handler_and_data(port->irq,
113 + mtk_pcie_intr_handler, port);
117 @@ -904,49 +928,29 @@ static int mtk_pcie_parse_port(struct mt
119 /* sys_ck might be divided into the following parts in some chips */
120 snprintf(name, sizeof(name), "ahb_ck%d", slot);
121 - port->ahb_ck = devm_clk_get(dev, name);
122 - if (IS_ERR(port->ahb_ck)) {
123 - if (PTR_ERR(port->ahb_ck) == -EPROBE_DEFER)
124 - return -EPROBE_DEFER;
126 - port->ahb_ck = NULL;
128 + port->ahb_ck = devm_clk_get_optional(dev, name);
129 + if (IS_ERR(port->ahb_ck))
130 + return PTR_ERR(port->ahb_ck);
132 snprintf(name, sizeof(name), "axi_ck%d", slot);
133 - port->axi_ck = devm_clk_get(dev, name);
134 - if (IS_ERR(port->axi_ck)) {
135 - if (PTR_ERR(port->axi_ck) == -EPROBE_DEFER)
136 - return -EPROBE_DEFER;
138 - port->axi_ck = NULL;
140 + port->axi_ck = devm_clk_get_optional(dev, name);
141 + if (IS_ERR(port->axi_ck))
142 + return PTR_ERR(port->axi_ck);
144 snprintf(name, sizeof(name), "aux_ck%d", slot);
145 - port->aux_ck = devm_clk_get(dev, name);
146 - if (IS_ERR(port->aux_ck)) {
147 - if (PTR_ERR(port->aux_ck) == -EPROBE_DEFER)
148 - return -EPROBE_DEFER;
150 - port->aux_ck = NULL;
152 + port->aux_ck = devm_clk_get_optional(dev, name);
153 + if (IS_ERR(port->aux_ck))
154 + return PTR_ERR(port->aux_ck);
156 snprintf(name, sizeof(name), "obff_ck%d", slot);
157 - port->obff_ck = devm_clk_get(dev, name);
158 - if (IS_ERR(port->obff_ck)) {
159 - if (PTR_ERR(port->obff_ck) == -EPROBE_DEFER)
160 - return -EPROBE_DEFER;
162 - port->obff_ck = NULL;
164 + port->obff_ck = devm_clk_get_optional(dev, name);
165 + if (IS_ERR(port->obff_ck))
166 + return PTR_ERR(port->obff_ck);
168 snprintf(name, sizeof(name), "pipe_ck%d", slot);
169 - port->pipe_ck = devm_clk_get(dev, name);
170 - if (IS_ERR(port->pipe_ck)) {
171 - if (PTR_ERR(port->pipe_ck) == -EPROBE_DEFER)
172 - return -EPROBE_DEFER;
174 - port->pipe_ck = NULL;
176 + port->pipe_ck = devm_clk_get_optional(dev, name);
177 + if (IS_ERR(port->pipe_ck))
178 + return PTR_ERR(port->pipe_ck);
180 snprintf(name, sizeof(name), "pcie-rst%d", slot);
181 port->reset = devm_reset_control_get_optional_exclusive(dev, name);
182 @@ -999,10 +1003,8 @@ static int mtk_pcie_subsys_powerup(struc
183 pcie->free_ck = NULL;
186 - if (dev->pm_domain) {
187 - pm_runtime_enable(dev);
188 - pm_runtime_get_sync(dev);
190 + pm_runtime_enable(dev);
191 + pm_runtime_get_sync(dev);
193 /* enable top level clock */
194 err = clk_prepare_enable(pcie->free_ck);
195 @@ -1014,10 +1016,8 @@ static int mtk_pcie_subsys_powerup(struc
199 - if (dev->pm_domain) {
200 - pm_runtime_put_sync(dev);
201 - pm_runtime_disable(dev);
203 + pm_runtime_put_sync(dev);
204 + pm_runtime_disable(dev);
208 @@ -1122,36 +1122,6 @@ static int mtk_pcie_request_resources(st
211 err = devm_pci_remap_iospace(dev, &pcie->pio, pcie->io.start);
218 -static int mtk_pcie_register_host(struct pci_host_bridge *host)
220 - struct mtk_pcie *pcie = pci_host_bridge_priv(host);
221 - struct pci_bus *child;
224 - host->busnr = pcie->busn.start;
225 - host->dev.parent = pcie->dev;
226 - host->ops = pcie->soc->ops;
227 - host->map_irq = of_irq_parse_and_map_pci;
228 - host->swizzle_irq = pci_common_swizzle;
229 - host->sysdata = pcie;
231 - err = pci_scan_root_bus_bridge(host);
235 - pci_bus_size_bridges(host->bus);
236 - pci_bus_assign_resources(host->bus);
238 - list_for_each_entry(child, &host->bus->children, node)
239 - pcie_bus_configure_settings(child);
241 - pci_bus_add_devices(host->bus);
245 @@ -1182,7 +1152,14 @@ static int mtk_pcie_probe(struct platfor
249 - err = mtk_pcie_register_host(host);
250 + host->busnr = pcie->busn.start;
251 + host->dev.parent = pcie->dev;
252 + host->ops = pcie->soc->ops;
253 + host->map_irq = of_irq_parse_and_map_pci;
254 + host->swizzle_irq = pci_common_swizzle;
255 + host->sysdata = pcie;
257 + err = pci_host_probe(host);
261 @@ -1195,6 +1172,80 @@ put_resources:
266 +static void mtk_pcie_free_resources(struct mtk_pcie *pcie)
268 + struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
269 + struct list_head *windows = &host->windows;
271 + pci_free_resource_list(windows);
274 +static int mtk_pcie_remove(struct platform_device *pdev)
276 + struct mtk_pcie *pcie = platform_get_drvdata(pdev);
277 + struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
279 + pci_stop_root_bus(host->bus);
280 + pci_remove_root_bus(host->bus);
281 + mtk_pcie_free_resources(pcie);
283 + mtk_pcie_irq_teardown(pcie);
285 + mtk_pcie_put_resources(pcie);
290 +static int __maybe_unused mtk_pcie_suspend_noirq(struct device *dev)
292 + struct mtk_pcie *pcie = dev_get_drvdata(dev);
293 + struct mtk_pcie_port *port;
295 + if (list_empty(&pcie->ports))
298 + list_for_each_entry(port, &pcie->ports, list) {
299 + clk_disable_unprepare(port->pipe_ck);
300 + clk_disable_unprepare(port->obff_ck);
301 + clk_disable_unprepare(port->axi_ck);
302 + clk_disable_unprepare(port->aux_ck);
303 + clk_disable_unprepare(port->ahb_ck);
304 + clk_disable_unprepare(port->sys_ck);
305 + phy_power_off(port->phy);
306 + phy_exit(port->phy);
309 + clk_disable_unprepare(pcie->free_ck);
314 +static int __maybe_unused mtk_pcie_resume_noirq(struct device *dev)
316 + struct mtk_pcie *pcie = dev_get_drvdata(dev);
317 + struct mtk_pcie_port *port, *tmp;
319 + if (list_empty(&pcie->ports))
322 + clk_prepare_enable(pcie->free_ck);
324 + list_for_each_entry_safe(port, tmp, &pcie->ports, list)
325 + mtk_pcie_enable_port(port);
327 + /* In case of EP was removed while system suspend. */
328 + if (list_empty(&pcie->ports))
329 + clk_disable_unprepare(pcie->free_ck);
334 +static const struct dev_pm_ops mtk_pcie_pm_ops = {
335 + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq,
336 + mtk_pcie_resume_noirq)
339 static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
340 .ops = &mtk_pcie_ops,
341 .startup = mtk_pcie_startup_port,
342 @@ -1223,10 +1274,13 @@ static const struct of_device_id mtk_pci
344 static struct platform_driver mtk_pcie_driver = {
345 .probe = mtk_pcie_probe,
346 + .remove = mtk_pcie_remove,
349 .of_match_table = mtk_pcie_ids,
350 .suppress_bind_attrs = true,
351 + .pm = &mtk_pcie_pm_ops,
354 -builtin_platform_driver(mtk_pcie_driver);
355 +module_platform_driver(mtk_pcie_driver);
356 +MODULE_LICENSE("GPL v2");
357 --- a/include/linux/clk.h
358 +++ b/include/linux/clk.h
359 @@ -349,6 +349,17 @@ int __must_check devm_clk_bulk_get(struc
360 struct clk *devm_clk_get(struct device *dev, const char *id);
363 + * devm_clk_get_optional - lookup and obtain a managed reference to an optional
365 + * @dev: device for clock "consumer"
366 + * @id: clock consumer ID
368 + * Behaves the same as devm_clk_get() except where there is no clock producer.
369 + * In this case, instead of returning -ENOENT, the function returns NULL.
371 +struct clk *devm_clk_get_optional(struct device *dev, const char *id);
374 * devm_get_clk_from_child - lookup and obtain a managed reference to a
375 * clock producer from child node.
376 * @dev: device for clock "consumer"