]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/clk/sunxi/clk-sun8i-mbus.c
Merge remote-tracking branch 'hwmon-staging/hwmon-next'
[karo-tx-linux.git] / drivers / clk / sunxi / clk-sun8i-mbus.c
1 /*
2  * Copyright 2014 Chen-Yu Tsai
3  *
4  * Chen-Yu Tsai <wens@csie.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/clk.h>
18 #include <linux/clkdev.h>
19 #include <linux/clk-provider.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/of_address.h>
23
24 #define SUN8I_MBUS_ENABLE       31
25 #define SUN8I_MBUS_MUX_SHIFT    24
26 #define SUN8I_MBUS_MUX_MASK     0x3
27 #define SUN8I_MBUS_DIV_SHIFT    0
28 #define SUN8I_MBUS_DIV_WIDTH    3
29 #define SUN8I_MBUS_MAX_PARENTS  4
30
31 static DEFINE_SPINLOCK(sun8i_a23_mbus_lock);
32
33 static void __init sun8i_a23_mbus_setup(struct device_node *node)
34 {
35         int num_parents = of_clk_get_parent_count(node);
36         const char *parents[num_parents];
37         const char *clk_name = node->name;
38         struct resource res;
39         struct clk_divider *div;
40         struct clk_gate *gate;
41         struct clk_mux *mux;
42         struct clk *clk;
43         void __iomem *reg;
44         int err;
45
46         reg = of_io_request_and_map(node, 0, of_node_full_name(node));
47         if (!reg) {
48                 pr_err("Could not get registers for sun8i-mbus-clk\n");
49                 return;
50         }
51
52         div = kzalloc(sizeof(*div), GFP_KERNEL);
53         if (!div)
54                 goto err_unmap;
55
56         mux = kzalloc(sizeof(*mux), GFP_KERNEL);
57         if (!mux)
58                 goto err_free_div;
59
60         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
61         if (!gate)
62                 goto err_free_mux;
63
64         of_property_read_string(node, "clock-output-names", &clk_name);
65         of_clk_parent_fill(node, parents, num_parents);
66
67         gate->reg = reg;
68         gate->bit_idx = SUN8I_MBUS_ENABLE;
69         gate->lock = &sun8i_a23_mbus_lock;
70
71         div->reg = reg;
72         div->shift = SUN8I_MBUS_DIV_SHIFT;
73         div->width = SUN8I_MBUS_DIV_WIDTH;
74         div->lock = &sun8i_a23_mbus_lock;
75
76         mux->reg = reg;
77         mux->shift = SUN8I_MBUS_MUX_SHIFT;
78         mux->mask = SUN8I_MBUS_MUX_MASK;
79         mux->lock = &sun8i_a23_mbus_lock;
80
81         clk = clk_register_composite(NULL, clk_name, parents, num_parents,
82                                      &mux->hw, &clk_mux_ops,
83                                      &div->hw, &clk_divider_ops,
84                                      &gate->hw, &clk_gate_ops,
85                                      0);
86         if (IS_ERR(clk))
87                 goto err_free_gate;
88
89         err = of_clk_add_provider(node, of_clk_src_simple_get, clk);
90         if (err)
91                 goto err_unregister_clk;
92
93         /* The MBUS clocks needs to be always enabled */
94         __clk_get(clk);
95         clk_prepare_enable(clk);
96
97         return;
98
99 err_unregister_clk:
100         /* TODO: The composite clock stuff will leak a bit here. */
101         clk_unregister(clk);
102 err_free_gate:
103         kfree(gate);
104 err_free_mux:
105         kfree(mux);
106 err_free_div:
107         kfree(div);
108 err_unmap:
109         iounmap(reg);
110         of_address_to_resource(node, 0, &res);
111         release_mem_region(res.start, resource_size(&res));
112 }
113 CLK_OF_DECLARE(sun8i_a23_mbus, "allwinner,sun8i-a23-mbus-clk", sun8i_a23_mbus_setup);