Merge tag 'reset-for-v5.3' of git://git.pengutronix.de/git/pza/linux into arm/drivers
[sfrench/cifs-2.6.git] / drivers / clk / versatile / clk-vexpress-osc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2012 ARM Limited
5  */
6
7 #include <linux/clkdev.h>
8 #include <linux/clk-provider.h>
9 #include <linux/err.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/vexpress.h>
14
15 struct vexpress_osc {
16         struct regmap *reg;
17         struct clk_hw hw;
18         unsigned long rate_min;
19         unsigned long rate_max;
20 };
21
22 #define to_vexpress_osc(osc) container_of(osc, struct vexpress_osc, hw)
23
24 static unsigned long vexpress_osc_recalc_rate(struct clk_hw *hw,
25                 unsigned long parent_rate)
26 {
27         struct vexpress_osc *osc = to_vexpress_osc(hw);
28         u32 rate;
29
30         regmap_read(osc->reg, 0, &rate);
31
32         return rate;
33 }
34
35 static long vexpress_osc_round_rate(struct clk_hw *hw, unsigned long rate,
36                 unsigned long *parent_rate)
37 {
38         struct vexpress_osc *osc = to_vexpress_osc(hw);
39
40         if (osc->rate_min && rate < osc->rate_min)
41                 rate = osc->rate_min;
42
43         if (osc->rate_max && rate > osc->rate_max)
44                 rate = osc->rate_max;
45
46         return rate;
47 }
48
49 static int vexpress_osc_set_rate(struct clk_hw *hw, unsigned long rate,
50                 unsigned long parent_rate)
51 {
52         struct vexpress_osc *osc = to_vexpress_osc(hw);
53
54         return regmap_write(osc->reg, 0, rate);
55 }
56
57 static const struct clk_ops vexpress_osc_ops = {
58         .recalc_rate = vexpress_osc_recalc_rate,
59         .round_rate = vexpress_osc_round_rate,
60         .set_rate = vexpress_osc_set_rate,
61 };
62
63
64 static int vexpress_osc_probe(struct platform_device *pdev)
65 {
66         struct clk_init_data init;
67         struct vexpress_osc *osc;
68         struct clk *clk;
69         u32 range[2];
70
71         osc = devm_kzalloc(&pdev->dev, sizeof(*osc), GFP_KERNEL);
72         if (!osc)
73                 return -ENOMEM;
74
75         osc->reg = devm_regmap_init_vexpress_config(&pdev->dev);
76         if (IS_ERR(osc->reg))
77                 return PTR_ERR(osc->reg);
78
79         if (of_property_read_u32_array(pdev->dev.of_node, "freq-range", range,
80                         ARRAY_SIZE(range)) == 0) {
81                 osc->rate_min = range[0];
82                 osc->rate_max = range[1];
83         }
84
85         if (of_property_read_string(pdev->dev.of_node, "clock-output-names",
86                         &init.name) != 0)
87                 init.name = dev_name(&pdev->dev);
88
89         init.ops = &vexpress_osc_ops;
90         init.flags = 0;
91         init.num_parents = 0;
92
93         osc->hw.init = &init;
94
95         clk = clk_register(NULL, &osc->hw);
96         if (IS_ERR(clk))
97                 return PTR_ERR(clk);
98
99         of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get, clk);
100         clk_hw_set_rate_range(&osc->hw, osc->rate_min, osc->rate_max);
101
102         dev_dbg(&pdev->dev, "Registered clock '%s'\n", init.name);
103
104         return 0;
105 }
106
107 static const struct of_device_id vexpress_osc_of_match[] = {
108         { .compatible = "arm,vexpress-osc", },
109         {}
110 };
111
112 static struct platform_driver vexpress_osc_driver = {
113         .driver = {
114                 .name = "vexpress-osc",
115                 .of_match_table = vexpress_osc_of_match,
116         },
117         .probe = vexpress_osc_probe,
118 };
119
120 static int __init vexpress_osc_init(void)
121 {
122         return platform_driver_register(&vexpress_osc_driver);
123 }
124 core_initcall(vexpress_osc_init);