blob: 411d3033a96e9a729fd9b289cf390344a17f415f [file] [log] [blame]
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +08001/*
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
Stephen Boyd9dfefe82015-06-19 15:00:46 -070017#include <linux/clk.h>
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080018#include <linux/clkdev.h>
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +080019#include <linux/clk-provider.h>
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080020#include <linux/slab.h>
21#include <linux/spinlock.h>
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +080022#include <linux/of_address.h>
23
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080024#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
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +080030
31static DEFINE_SPINLOCK(sun8i_a23_mbus_lock);
32
33static void __init sun8i_a23_mbus_setup(struct device_node *node)
34{
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080035 int num_parents = of_clk_get_parent_count(node);
Stephen Boyd1295e362016-03-04 09:18:41 -080036 const char **parents;
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080037 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;
Hans de Goede7c74c222014-11-23 14:38:07 +010043 void __iomem *reg;
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080044 int err;
Hans de Goede7c74c222014-11-23 14:38:07 +010045
Stephen Boyd1295e362016-03-04 09:18:41 -080046 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
47 if (!parents)
48 return;
49
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080050 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
Hans de Goede7c74c222014-11-23 14:38:07 +010051 if (!reg) {
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080052 pr_err("Could not get registers for sun8i-mbus-clk\n");
Stephen Boyd1295e362016-03-04 09:18:41 -080053 goto err_free_parents;
Hans de Goede7c74c222014-11-23 14:38:07 +010054 }
55
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080056 div = kzalloc(sizeof(*div), GFP_KERNEL);
57 if (!div)
58 goto err_unmap;
59
60 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
61 if (!mux)
62 goto err_free_div;
63
64 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
65 if (!gate)
66 goto err_free_mux;
67
68 of_property_read_string(node, "clock-output-names", &clk_name);
69 of_clk_parent_fill(node, parents, num_parents);
70
71 gate->reg = reg;
72 gate->bit_idx = SUN8I_MBUS_ENABLE;
73 gate->lock = &sun8i_a23_mbus_lock;
74
75 div->reg = reg;
76 div->shift = SUN8I_MBUS_DIV_SHIFT;
77 div->width = SUN8I_MBUS_DIV_WIDTH;
78 div->lock = &sun8i_a23_mbus_lock;
79
80 mux->reg = reg;
81 mux->shift = SUN8I_MBUS_MUX_SHIFT;
82 mux->mask = SUN8I_MBUS_MUX_MASK;
83 mux->lock = &sun8i_a23_mbus_lock;
84
85 clk = clk_register_composite(NULL, clk_name, parents, num_parents,
86 &mux->hw, &clk_mux_ops,
87 &div->hw, &clk_divider_ops,
88 &gate->hw, &clk_gate_ops,
89 0);
90 if (IS_ERR(clk))
91 goto err_free_gate;
92
93 err = of_clk_add_provider(node, of_clk_src_simple_get, clk);
94 if (err)
95 goto err_unregister_clk;
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +080096
Stephen Boyd1295e362016-03-04 09:18:41 -080097 kfree(parents); /* parents is deep copied */
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +080098 /* The MBUS clocks needs to be always enabled */
Chen-Yu Tsai8f2bf2a2016-01-25 21:15:47 +080099 __clk_get(clk);
100 clk_prepare_enable(clk);
101
102 return;
103
104err_unregister_clk:
105 /* TODO: The composite clock stuff will leak a bit here. */
106 clk_unregister(clk);
107err_free_gate:
108 kfree(gate);
109err_free_mux:
110 kfree(mux);
111err_free_div:
112 kfree(div);
113err_unmap:
114 iounmap(reg);
115 of_address_to_resource(node, 0, &res);
116 release_mem_region(res.start, resource_size(&res));
Stephen Boyd1295e362016-03-04 09:18:41 -0800117err_free_parents:
118 kfree(parents);
Chen-Yu Tsai9c8176b2014-09-16 18:04:01 +0800119}
120CLK_OF_DECLARE(sun8i_a23_mbus, "allwinner,sun8i-a23-mbus-clk", sun8i_a23_mbus_setup);