2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
15 #include <linux/of_address.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/regmap.h>
22 DEFINE_SPINLOCK(pmc_pcr_lock);
24 #define PERIPHERAL_MAX 64
26 #define PERIPHERAL_AT91RM9200 0
27 #define PERIPHERAL_AT91SAM9X5 1
29 #define PERIPHERAL_ID_MIN 2
30 #define PERIPHERAL_ID_MAX 31
31 #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
33 #define PERIPHERAL_RSHIFT_MASK 0x3
34 #define PERIPHERAL_RSHIFT(val) (((val) >> 16) & PERIPHERAL_RSHIFT_MASK)
36 #define PERIPHERAL_MAX_SHIFT 3
38 struct clk_peripheral {
40 struct regmap *regmap;
44 #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
46 struct clk_sam9x5_peripheral {
48 struct regmap *regmap;
49 struct clk_range range;
56 #define to_clk_sam9x5_peripheral(hw) \
57 container_of(hw, struct clk_sam9x5_peripheral, hw)
59 static int clk_peripheral_enable(struct clk_hw *hw)
61 struct clk_peripheral *periph = to_clk_peripheral(hw);
62 int offset = AT91_PMC_PCER;
65 if (id < PERIPHERAL_ID_MIN)
67 if (id > PERIPHERAL_ID_MAX)
68 offset = AT91_PMC_PCER1;
69 regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
74 static void clk_peripheral_disable(struct clk_hw *hw)
76 struct clk_peripheral *periph = to_clk_peripheral(hw);
77 int offset = AT91_PMC_PCDR;
80 if (id < PERIPHERAL_ID_MIN)
82 if (id > PERIPHERAL_ID_MAX)
83 offset = AT91_PMC_PCDR1;
84 regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
87 static int clk_peripheral_is_enabled(struct clk_hw *hw)
89 struct clk_peripheral *periph = to_clk_peripheral(hw);
90 int offset = AT91_PMC_PCSR;
94 if (id < PERIPHERAL_ID_MIN)
96 if (id > PERIPHERAL_ID_MAX)
97 offset = AT91_PMC_PCSR1;
98 regmap_read(periph->regmap, offset, &status);
100 return status & PERIPHERAL_MASK(id) ? 1 : 0;
103 static const struct clk_ops peripheral_ops = {
104 .enable = clk_peripheral_enable,
105 .disable = clk_peripheral_disable,
106 .is_enabled = clk_peripheral_is_enabled,
109 static struct clk * __init
110 at91_clk_register_peripheral(struct regmap *regmap, const char *name,
111 const char *parent_name, u32 id)
113 struct clk_peripheral *periph;
114 struct clk *clk = NULL;
115 struct clk_init_data init;
117 if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
118 return ERR_PTR(-EINVAL);
120 periph = kzalloc(sizeof(*periph), GFP_KERNEL);
122 return ERR_PTR(-ENOMEM);
125 init.ops = &peripheral_ops;
126 init.parent_names = (parent_name ? &parent_name : NULL);
127 init.num_parents = (parent_name ? 1 : 0);
131 periph->hw.init = &init;
132 periph->regmap = regmap;
134 clk = clk_register(NULL, &periph->hw);
141 static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
143 struct clk_hw *parent;
144 unsigned long parent_rate;
147 if (!periph->auto_div)
150 if (periph->range.max) {
151 parent = clk_hw_get_parent_by_index(&periph->hw, 0);
152 parent_rate = clk_hw_get_rate(parent);
156 for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
157 if (parent_rate >> shift <= periph->range.max)
162 periph->auto_div = false;
166 static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
168 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
171 if (periph->id < PERIPHERAL_ID_MIN)
174 spin_lock_irqsave(periph->lock, flags);
175 regmap_write(periph->regmap, AT91_PMC_PCR,
176 (periph->id & AT91_PMC_PCR_PID_MASK));
177 regmap_update_bits(periph->regmap, AT91_PMC_PCR,
178 AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
180 AT91_PMC_PCR_DIV(periph->div) |
183 spin_unlock_irqrestore(periph->lock, flags);
188 static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
190 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
193 if (periph->id < PERIPHERAL_ID_MIN)
196 spin_lock_irqsave(periph->lock, flags);
197 regmap_write(periph->regmap, AT91_PMC_PCR,
198 (periph->id & AT91_PMC_PCR_PID_MASK));
199 regmap_update_bits(periph->regmap, AT91_PMC_PCR,
200 AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
202 spin_unlock_irqrestore(periph->lock, flags);
205 static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
207 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
211 if (periph->id < PERIPHERAL_ID_MIN)
214 spin_lock_irqsave(periph->lock, flags);
215 regmap_write(periph->regmap, AT91_PMC_PCR,
216 (periph->id & AT91_PMC_PCR_PID_MASK));
217 regmap_read(periph->regmap, AT91_PMC_PCR, &status);
218 spin_unlock_irqrestore(periph->lock, flags);
220 return status & AT91_PMC_PCR_EN ? 1 : 0;
224 clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
225 unsigned long parent_rate)
227 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
231 if (periph->id < PERIPHERAL_ID_MIN)
234 spin_lock_irqsave(periph->lock, flags);
235 regmap_write(periph->regmap, AT91_PMC_PCR,
236 (periph->id & AT91_PMC_PCR_PID_MASK));
237 regmap_read(periph->regmap, AT91_PMC_PCR, &status);
238 spin_unlock_irqrestore(periph->lock, flags);
240 if (status & AT91_PMC_PCR_EN) {
241 periph->div = PERIPHERAL_RSHIFT(status);
242 periph->auto_div = false;
244 clk_sam9x5_peripheral_autodiv(periph);
247 return parent_rate >> periph->div;
250 static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
252 unsigned long *parent_rate)
255 unsigned long best_rate;
256 unsigned long best_diff;
257 unsigned long cur_rate = *parent_rate;
258 unsigned long cur_diff;
259 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
261 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
264 if (periph->range.max) {
265 for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
266 cur_rate = *parent_rate >> shift;
267 if (cur_rate <= periph->range.max)
272 if (rate >= cur_rate)
275 best_diff = cur_rate - rate;
276 best_rate = cur_rate;
277 for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
278 cur_rate = *parent_rate >> shift;
280 cur_diff = rate - cur_rate;
282 cur_diff = cur_rate - rate;
284 if (cur_diff < best_diff) {
285 best_diff = cur_diff;
286 best_rate = cur_rate;
289 if (!best_diff || cur_rate < rate)
296 static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
298 unsigned long parent_rate)
301 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
302 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
303 if (parent_rate == rate)
309 if (periph->range.max && rate > periph->range.max)
312 for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
313 if (parent_rate >> shift == rate) {
314 periph->auto_div = false;
323 static const struct clk_ops sam9x5_peripheral_ops = {
324 .enable = clk_sam9x5_peripheral_enable,
325 .disable = clk_sam9x5_peripheral_disable,
326 .is_enabled = clk_sam9x5_peripheral_is_enabled,
327 .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
328 .round_rate = clk_sam9x5_peripheral_round_rate,
329 .set_rate = clk_sam9x5_peripheral_set_rate,
332 static struct clk * __init
333 at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
334 const char *name, const char *parent_name,
335 u32 id, const struct clk_range *range)
337 struct clk_sam9x5_peripheral *periph;
338 struct clk *clk = NULL;
339 struct clk_init_data init;
341 if (!name || !parent_name)
342 return ERR_PTR(-EINVAL);
344 periph = kzalloc(sizeof(*periph), GFP_KERNEL);
346 return ERR_PTR(-ENOMEM);
349 init.ops = &sam9x5_peripheral_ops;
350 init.parent_names = (parent_name ? &parent_name : NULL);
351 init.num_parents = (parent_name ? 1 : 0);
355 periph->hw.init = &init;
357 periph->regmap = regmap;
359 periph->auto_div = true;
360 periph->range = *range;
362 clk = clk_register(NULL, &periph->hw);
366 clk_sam9x5_peripheral_autodiv(periph);
372 of_at91_clk_periph_setup(struct device_node *np, u8 type)
377 const char *parent_name;
379 struct device_node *periphclknp;
380 struct regmap *regmap;
382 parent_name = of_clk_get_parent_name(np, 0);
386 num = of_get_child_count(np);
387 if (!num || num > PERIPHERAL_MAX)
390 regmap = syscon_node_to_regmap(of_get_parent(np));
394 for_each_child_of_node(np, periphclknp) {
395 if (of_property_read_u32(periphclknp, "reg", &id))
398 if (id >= PERIPHERAL_MAX)
401 if (of_property_read_string(np, "clock-output-names", &name))
402 name = periphclknp->name;
404 if (type == PERIPHERAL_AT91RM9200) {
405 clk = at91_clk_register_peripheral(regmap, name,
408 struct clk_range range = CLK_RANGE(0, 0);
410 of_at91_get_clk_range(periphclknp,
411 "atmel,clk-output-range",
414 clk = at91_clk_register_sam9x5_peripheral(regmap,
424 of_clk_add_provider(periphclknp, of_clk_src_simple_get, clk);
428 static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
430 of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
432 CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
433 of_at91rm9200_clk_periph_setup);
435 static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
437 of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
439 CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
440 of_at91sam9x5_clk_periph_setup);