xref: /linux/drivers/clk/sunxi/clk-a10-hosc.c (revision c942fddf)
1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ff01df28SMaxime Ripard /*
3ff01df28SMaxime Ripard  * Copyright 2013 Emilio López
4ff01df28SMaxime Ripard  *
5ff01df28SMaxime Ripard  * Emilio López <emilio@elopez.com.ar>
6ff01df28SMaxime Ripard  */
7ff01df28SMaxime Ripard 
8ff01df28SMaxime Ripard #include <linux/clk-provider.h>
9ff01df28SMaxime Ripard #include <linux/of.h>
10ff01df28SMaxime Ripard #include <linux/of_address.h>
11b0b6413fSMaxime Ripard #include <linux/slab.h>
12ff01df28SMaxime Ripard 
13ff01df28SMaxime Ripard #define SUNXI_OSC24M_GATE	0
14ff01df28SMaxime Ripard 
15ff01df28SMaxime Ripard static DEFINE_SPINLOCK(hosc_lock);
16ff01df28SMaxime Ripard 
sun4i_osc_clk_setup(struct device_node * node)17ff01df28SMaxime Ripard static void __init sun4i_osc_clk_setup(struct device_node *node)
18ff01df28SMaxime Ripard {
19ff01df28SMaxime Ripard 	struct clk *clk;
20ff01df28SMaxime Ripard 	struct clk_fixed_rate *fixed;
21ff01df28SMaxime Ripard 	struct clk_gate *gate;
22ff01df28SMaxime Ripard 	const char *clk_name = node->name;
23ff01df28SMaxime Ripard 	u32 rate;
24ff01df28SMaxime Ripard 
25ff01df28SMaxime Ripard 	if (of_property_read_u32(node, "clock-frequency", &rate))
26ff01df28SMaxime Ripard 		return;
27ff01df28SMaxime Ripard 
28ff01df28SMaxime Ripard 	/* allocate fixed-rate and gate clock structs */
29ff01df28SMaxime Ripard 	fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
30ff01df28SMaxime Ripard 	if (!fixed)
31ff01df28SMaxime Ripard 		return;
32ff01df28SMaxime Ripard 	gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
33ff01df28SMaxime Ripard 	if (!gate)
34ff01df28SMaxime Ripard 		goto err_free_fixed;
35ff01df28SMaxime Ripard 
36ff01df28SMaxime Ripard 	of_property_read_string(node, "clock-output-names", &clk_name);
37ff01df28SMaxime Ripard 
38ff01df28SMaxime Ripard 	/* set up gate and fixed rate properties */
39ff01df28SMaxime Ripard 	gate->reg = of_iomap(node, 0);
40ff01df28SMaxime Ripard 	gate->bit_idx = SUNXI_OSC24M_GATE;
41ff01df28SMaxime Ripard 	gate->lock = &hosc_lock;
42ff01df28SMaxime Ripard 	fixed->fixed_rate = rate;
43ff01df28SMaxime Ripard 
44ff01df28SMaxime Ripard 	clk = clk_register_composite(NULL, clk_name,
45ff01df28SMaxime Ripard 			NULL, 0,
46ff01df28SMaxime Ripard 			NULL, NULL,
47ff01df28SMaxime Ripard 			&fixed->hw, &clk_fixed_rate_ops,
48260c37f9SStephen Boyd 			&gate->hw, &clk_gate_ops, 0);
49ff01df28SMaxime Ripard 
50ff01df28SMaxime Ripard 	if (IS_ERR(clk))
51ff01df28SMaxime Ripard 		goto err_free_gate;
52ff01df28SMaxime Ripard 
53ff01df28SMaxime Ripard 	of_clk_add_provider(node, of_clk_src_simple_get, clk);
54ff01df28SMaxime Ripard 
55ff01df28SMaxime Ripard 	return;
56ff01df28SMaxime Ripard 
57ff01df28SMaxime Ripard err_free_gate:
58ff01df28SMaxime Ripard 	kfree(gate);
59ff01df28SMaxime Ripard err_free_fixed:
60ff01df28SMaxime Ripard 	kfree(fixed);
61ff01df28SMaxime Ripard }
62ff01df28SMaxime Ripard CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-a10-osc-clk", sun4i_osc_clk_setup);
63