Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / sunxi / clk-sun8i-apb0.c
blobfc5d6e3b77d1b4941dd4f03ea575bd00e5f6b90a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2014 Chen-Yu Tsai
4 * Author: Chen-Yu Tsai <wens@csie.org>
6 * Allwinner A23 APB0 clock driver
8 * Based on clk-sun6i-apb0.c
9 * Allwinner A31 APB0 clock driver
11 * Copyright (C) 2014 Free Electrons
12 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
15 #include <linux/clk-provider.h>
16 #include <linux/init.h>
17 #include <linux/io.h>
18 #include <linux/of.h>
19 #include <linux/of_address.h>
20 #include <linux/platform_device.h>
22 static struct clk *sun8i_a23_apb0_register(struct device_node *node,
23 void __iomem *reg)
25 const char *clk_name = node->name;
26 const char *clk_parent;
27 struct clk *clk;
28 int ret;
30 clk_parent = of_clk_get_parent_name(node, 0);
31 if (!clk_parent)
32 return ERR_PTR(-EINVAL);
34 of_property_read_string(node, "clock-output-names", &clk_name);
36 /* The A23 APB0 clock is a standard 2 bit wide divider clock */
37 clk = clk_register_divider(NULL, clk_name, clk_parent, 0, reg,
38 0, 2, 0, NULL);
39 if (IS_ERR(clk))
40 return clk;
42 ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
43 if (ret)
44 goto err_unregister;
46 return clk;
48 err_unregister:
49 clk_unregister_divider(clk);
51 return ERR_PTR(ret);
54 static void sun8i_a23_apb0_setup(struct device_node *node)
56 void __iomem *reg;
57 struct resource res;
58 struct clk *clk;
60 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
61 if (IS_ERR(reg)) {
63 * This happens with clk nodes instantiated through mfd,
64 * as those do not have their resources assigned in the
65 * device tree. Do not print an error in this case.
67 if (PTR_ERR(reg) != -EINVAL)
68 pr_err("Could not get registers for a23-apb0-clk\n");
70 return;
73 clk = sun8i_a23_apb0_register(node, reg);
74 if (IS_ERR(clk))
75 goto err_unmap;
77 return;
79 err_unmap:
80 iounmap(reg);
81 of_address_to_resource(node, 0, &res);
82 release_mem_region(res.start, resource_size(&res));
84 CLK_OF_DECLARE_DRIVER(sun8i_a23_apb0, "allwinner,sun8i-a23-apb0-clk",
85 sun8i_a23_apb0_setup);
87 static int sun8i_a23_apb0_clk_probe(struct platform_device *pdev)
89 struct device_node *np = pdev->dev.of_node;
90 struct resource *r;
91 void __iomem *reg;
92 struct clk *clk;
94 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95 reg = devm_ioremap_resource(&pdev->dev, r);
96 if (IS_ERR(reg))
97 return PTR_ERR(reg);
99 clk = sun8i_a23_apb0_register(np, reg);
100 return PTR_ERR_OR_ZERO(clk);
103 static const struct of_device_id sun8i_a23_apb0_clk_dt_ids[] = {
104 { .compatible = "allwinner,sun8i-a23-apb0-clk" },
105 { /* sentinel */ }
108 static struct platform_driver sun8i_a23_apb0_clk_driver = {
109 .driver = {
110 .name = "sun8i-a23-apb0-clk",
111 .of_match_table = sun8i_a23_apb0_clk_dt_ids,
113 .probe = sun8i_a23_apb0_clk_probe,
115 builtin_platform_driver(sun8i_a23_apb0_clk_driver);