2 * Copyright 2015 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
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.
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.
17 #include <linux/clk-provider.h>
19 #include <linux/of_address.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
23 #define TCON_CH1_SCLK2_PARENTS 4
25 #define TCON_CH1_SCLK2_GATE_BIT BIT(31)
26 #define TCON_CH1_SCLK2_MUX_MASK 3
27 #define TCON_CH1_SCLK2_MUX_SHIFT 24
28 #define TCON_CH1_SCLK2_DIV_MASK 0xf
29 #define TCON_CH1_SCLK2_DIV_SHIFT 0
31 #define TCON_CH1_SCLK1_GATE_BIT BIT(15)
32 #define TCON_CH1_SCLK1_HALF_BIT BIT(11)
40 #define hw_to_tclk(hw) container_of(hw, struct tcon_ch1_clk, hw)
42 static void tcon_ch1_disable(struct clk_hw *hw)
44 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
48 spin_lock_irqsave(&tclk->lock, flags);
49 reg = readl(tclk->reg);
50 reg &= ~(TCON_CH1_SCLK2_GATE_BIT | TCON_CH1_SCLK1_GATE_BIT);
51 writel(reg, tclk->reg);
52 spin_unlock_irqrestore(&tclk->lock, flags);
55 static int tcon_ch1_enable(struct clk_hw *hw)
57 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
61 spin_lock_irqsave(&tclk->lock, flags);
62 reg = readl(tclk->reg);
63 reg |= TCON_CH1_SCLK2_GATE_BIT | TCON_CH1_SCLK1_GATE_BIT;
64 writel(reg, tclk->reg);
65 spin_unlock_irqrestore(&tclk->lock, flags);
70 static int tcon_ch1_is_enabled(struct clk_hw *hw)
72 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
75 reg = readl(tclk->reg);
76 return reg & (TCON_CH1_SCLK2_GATE_BIT | TCON_CH1_SCLK1_GATE_BIT);
79 static u8 tcon_ch1_get_parent(struct clk_hw *hw)
81 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
82 int num_parents = clk_hw_get_num_parents(hw);
85 reg = readl(tclk->reg) >> TCON_CH1_SCLK2_MUX_SHIFT;
86 reg &= reg >> TCON_CH1_SCLK2_MUX_MASK;
88 if (reg >= num_parents)
94 static int tcon_ch1_set_parent(struct clk_hw *hw, u8 index)
96 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
100 spin_lock_irqsave(&tclk->lock, flags);
101 reg = readl(tclk->reg);
102 reg &= ~(TCON_CH1_SCLK2_MUX_MASK << TCON_CH1_SCLK2_MUX_SHIFT);
103 reg |= index << TCON_CH1_SCLK2_MUX_SHIFT;
104 writel(reg, tclk->reg);
105 spin_unlock_irqrestore(&tclk->lock, flags);
110 static unsigned long tcon_ch1_calc_divider(unsigned long rate,
111 unsigned long parent_rate,
115 unsigned long best_rate = 0;
119 for (m = 1; m < 16; m++) {
122 for (d = 1; d < 3; d++) {
123 unsigned long tmp_rate;
125 tmp_rate = parent_rate / m / d;
131 (rate - tmp_rate) < (rate - best_rate)) {
132 best_rate = tmp_rate;
147 static int tcon_ch1_determine_rate(struct clk_hw *hw,
148 struct clk_rate_request *req)
150 long best_rate = -EINVAL;
153 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
154 unsigned long parent_rate;
155 unsigned long tmp_rate;
156 struct clk_hw *parent;
158 parent = clk_hw_get_parent_by_index(hw, i);
162 parent_rate = clk_hw_get_rate(parent);
164 tmp_rate = tcon_ch1_calc_divider(req->rate, parent_rate,
168 (req->rate - tmp_rate) < (req->rate - best_rate)) {
169 best_rate = tmp_rate;
170 req->best_parent_rate = parent_rate;
171 req->best_parent_hw = parent;
178 req->rate = best_rate;
182 static unsigned long tcon_ch1_recalc_rate(struct clk_hw *hw,
183 unsigned long parent_rate)
185 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
188 reg = readl(tclk->reg);
190 parent_rate /= (reg & TCON_CH1_SCLK2_DIV_MASK) + 1;
192 if (reg & TCON_CH1_SCLK1_HALF_BIT)
198 static int tcon_ch1_set_rate(struct clk_hw *hw, unsigned long rate,
199 unsigned long parent_rate)
201 struct tcon_ch1_clk *tclk = hw_to_tclk(hw);
207 tcon_ch1_calc_divider(rate, parent_rate, &div_m, &half);
209 spin_lock_irqsave(&tclk->lock, flags);
210 reg = readl(tclk->reg);
211 reg &= ~(TCON_CH1_SCLK2_DIV_MASK | TCON_CH1_SCLK1_HALF_BIT);
212 reg |= (div_m - 1) & TCON_CH1_SCLK2_DIV_MASK;
215 reg |= TCON_CH1_SCLK1_HALF_BIT;
217 writel(reg, tclk->reg);
218 spin_unlock_irqrestore(&tclk->lock, flags);
223 static const struct clk_ops tcon_ch1_ops = {
224 .disable = tcon_ch1_disable,
225 .enable = tcon_ch1_enable,
226 .is_enabled = tcon_ch1_is_enabled,
228 .get_parent = tcon_ch1_get_parent,
229 .set_parent = tcon_ch1_set_parent,
231 .determine_rate = tcon_ch1_determine_rate,
232 .recalc_rate = tcon_ch1_recalc_rate,
233 .set_rate = tcon_ch1_set_rate,
236 static void __init tcon_ch1_setup(struct device_node *node)
238 const char *parents[TCON_CH1_SCLK2_PARENTS];
239 const char *clk_name = node->name;
240 struct clk_init_data init;
241 struct tcon_ch1_clk *tclk;
247 of_property_read_string(node, "clock-output-names", &clk_name);
249 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
251 pr_err("%s: Could not map the clock registers\n", clk_name);
255 ret = of_clk_parent_fill(node, parents, TCON_CH1_SCLK2_PARENTS);
256 if (ret != TCON_CH1_SCLK2_PARENTS) {
257 pr_err("%s Could not retrieve the parents\n", clk_name);
261 tclk = kzalloc(sizeof(*tclk), GFP_KERNEL);
265 init.name = clk_name;
266 init.ops = &tcon_ch1_ops;
267 init.parent_names = parents;
268 init.num_parents = TCON_CH1_SCLK2_PARENTS;
269 init.flags = CLK_SET_RATE_PARENT;
272 tclk->hw.init = &init;
273 spin_lock_init(&tclk->lock);
275 clk = clk_register(NULL, &tclk->hw);
277 pr_err("%s: Couldn't register the clock\n", clk_name);
281 ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
283 pr_err("%s: Couldn't register our clock provider\n", clk_name);
284 goto err_unregister_clk;
295 of_address_to_resource(node, 0, &res);
296 release_mem_region(res.start, resource_size(&res));
299 CLK_OF_DECLARE(tcon_ch1, "allwinner,sun4i-a10-tcon-ch1-clk",