Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / sunxi / clk-sun6i-ar100.c
blobe1b7d0929cf7f816b5fa36380ee29ca2a1aa6c24
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2014 Free Electrons
5 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
7 * Allwinner A31 AR100 clock driver
8 */
10 #include <linux/bitops.h>
11 #include <linux/clk-provider.h>
12 #include <linux/init.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/spinlock.h>
17 #include "clk-factors.h"
19 /**
20 * sun6i_get_ar100_factors - Calculates factors p, m for AR100
22 * AR100 rate is calculated as follows
23 * rate = (parent_rate >> p) / (m + 1);
25 static void sun6i_get_ar100_factors(struct factors_request *req)
27 unsigned long div;
28 int shift;
30 /* clock only divides */
31 if (req->rate > req->parent_rate)
32 req->rate = req->parent_rate;
34 div = DIV_ROUND_UP(req->parent_rate, req->rate);
36 if (div < 32)
37 shift = 0;
38 else if (div >> 1 < 32)
39 shift = 1;
40 else if (div >> 2 < 32)
41 shift = 2;
42 else
43 shift = 3;
45 div >>= shift;
47 if (div > 32)
48 div = 32;
50 req->rate = (req->parent_rate >> shift) / div;
51 req->m = div - 1;
52 req->p = shift;
55 static const struct clk_factors_config sun6i_ar100_config = {
56 .mwidth = 5,
57 .mshift = 8,
58 .pwidth = 2,
59 .pshift = 4,
62 static const struct factors_data sun6i_ar100_data = {
63 .mux = 16,
64 .muxmask = GENMASK(1, 0),
65 .table = &sun6i_ar100_config,
66 .getter = sun6i_get_ar100_factors,
69 static DEFINE_SPINLOCK(sun6i_ar100_lock);
71 static int sun6i_a31_ar100_clk_probe(struct platform_device *pdev)
73 struct device_node *np = pdev->dev.of_node;
74 struct resource *r;
75 void __iomem *reg;
76 struct clk *clk;
78 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
79 reg = devm_ioremap_resource(&pdev->dev, r);
80 if (IS_ERR(reg))
81 return PTR_ERR(reg);
83 clk = sunxi_factors_register(np, &sun6i_ar100_data, &sun6i_ar100_lock,
84 reg);
85 if (!clk)
86 return -ENOMEM;
88 platform_set_drvdata(pdev, clk);
90 return 0;
93 static const struct of_device_id sun6i_a31_ar100_clk_dt_ids[] = {
94 { .compatible = "allwinner,sun6i-a31-ar100-clk" },
95 { /* sentinel */ }
98 static struct platform_driver sun6i_a31_ar100_clk_driver = {
99 .driver = {
100 .name = "sun6i-a31-ar100-clk",
101 .of_match_table = sun6i_a31_ar100_clk_dt_ids,
102 .suppress_bind_attrs = true,
104 .probe = sun6i_a31_ar100_clk_probe,
106 builtin_platform_driver(sun6i_a31_ar100_clk_driver);