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/bitops.h>
12 #include <linux/clk-provider.h>
13 #include <linux/clkdev.h>
14 #include <linux/clk/at91_pmc.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
21 DEFINE_SPINLOCK(pmc_pcr_lock);
23 #define PERIPHERAL_ID_MIN 2
24 #define PERIPHERAL_ID_MAX 31
25 #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
27 #define PERIPHERAL_MAX_SHIFT 3
29 struct clk_peripheral {
31 struct regmap *regmap;
35 #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
37 struct clk_sam9x5_peripheral {
39 struct regmap *regmap;
40 struct clk_range range;
44 const struct clk_pcr_layout *layout;
48 #define to_clk_sam9x5_peripheral(hw) \
49 container_of(hw, struct clk_sam9x5_peripheral, hw)
51 static int clk_peripheral_enable(struct clk_hw *hw)
53 struct clk_peripheral *periph = to_clk_peripheral(hw);
54 int offset = AT91_PMC_PCER;
57 if (id < PERIPHERAL_ID_MIN)
59 if (id > PERIPHERAL_ID_MAX)
60 offset = AT91_PMC_PCER1;
61 regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
66 static void clk_peripheral_disable(struct clk_hw *hw)
68 struct clk_peripheral *periph = to_clk_peripheral(hw);
69 int offset = AT91_PMC_PCDR;
72 if (id < PERIPHERAL_ID_MIN)
74 if (id > PERIPHERAL_ID_MAX)
75 offset = AT91_PMC_PCDR1;
76 regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
79 static int clk_peripheral_is_enabled(struct clk_hw *hw)
81 struct clk_peripheral *periph = to_clk_peripheral(hw);
82 int offset = AT91_PMC_PCSR;
86 if (id < PERIPHERAL_ID_MIN)
88 if (id > PERIPHERAL_ID_MAX)
89 offset = AT91_PMC_PCSR1;
90 regmap_read(periph->regmap, offset, &status);
92 return status & PERIPHERAL_MASK(id) ? 1 : 0;
95 static const struct clk_ops peripheral_ops = {
96 .enable = clk_peripheral_enable,
97 .disable = clk_peripheral_disable,
98 .is_enabled = clk_peripheral_is_enabled,
101 struct clk_hw * __init
102 at91_clk_register_peripheral(struct regmap *regmap, const char *name,
103 const char *parent_name, u32 id)
105 struct clk_peripheral *periph;
106 struct clk_init_data init;
110 if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
111 return ERR_PTR(-EINVAL);
113 periph = kzalloc(sizeof(*periph), GFP_KERNEL);
115 return ERR_PTR(-ENOMEM);
118 init.ops = &peripheral_ops;
119 init.parent_names = (parent_name ? &parent_name : NULL);
120 init.num_parents = (parent_name ? 1 : 0);
124 periph->hw.init = &init;
125 periph->regmap = regmap;
128 ret = clk_hw_register(NULL, &periph->hw);
137 static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
139 struct clk_hw *parent;
140 unsigned long parent_rate;
143 if (!periph->auto_div)
146 if (periph->range.max) {
147 parent = clk_hw_get_parent_by_index(&periph->hw, 0);
148 parent_rate = clk_hw_get_rate(parent);
152 for (; shift < PERIPHERAL_MAX_SHIFT; shift++) {
153 if (parent_rate >> shift <= periph->range.max)
158 periph->auto_div = false;
162 static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
164 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
167 if (periph->id < PERIPHERAL_ID_MIN)
170 spin_lock_irqsave(periph->lock, flags);
171 regmap_write(periph->regmap, periph->layout->offset,
172 (periph->id & periph->layout->pid_mask));
173 regmap_update_bits(periph->regmap, periph->layout->offset,
174 periph->layout->div_mask | periph->layout->cmd |
176 field_prep(periph->layout->div_mask, periph->div) |
177 periph->layout->cmd |
179 spin_unlock_irqrestore(periph->lock, flags);
184 static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
186 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
189 if (periph->id < PERIPHERAL_ID_MIN)
192 spin_lock_irqsave(periph->lock, flags);
193 regmap_write(periph->regmap, periph->layout->offset,
194 (periph->id & periph->layout->pid_mask));
195 regmap_update_bits(periph->regmap, periph->layout->offset,
196 AT91_PMC_PCR_EN | periph->layout->cmd,
197 periph->layout->cmd);
198 spin_unlock_irqrestore(periph->lock, flags);
201 static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
203 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
207 if (periph->id < PERIPHERAL_ID_MIN)
210 spin_lock_irqsave(periph->lock, flags);
211 regmap_write(periph->regmap, periph->layout->offset,
212 (periph->id & periph->layout->pid_mask));
213 regmap_read(periph->regmap, periph->layout->offset, &status);
214 spin_unlock_irqrestore(periph->lock, flags);
216 return status & AT91_PMC_PCR_EN ? 1 : 0;
220 clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
221 unsigned long parent_rate)
223 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
227 if (periph->id < PERIPHERAL_ID_MIN)
230 spin_lock_irqsave(periph->lock, flags);
231 regmap_write(periph->regmap, periph->layout->offset,
232 (periph->id & periph->layout->pid_mask));
233 regmap_read(periph->regmap, periph->layout->offset, &status);
234 spin_unlock_irqrestore(periph->lock, flags);
236 if (status & AT91_PMC_PCR_EN) {
237 periph->div = field_get(periph->layout->div_mask, status);
238 periph->auto_div = false;
240 clk_sam9x5_peripheral_autodiv(periph);
243 return parent_rate >> periph->div;
246 static long clk_sam9x5_peripheral_round_rate(struct clk_hw *hw,
248 unsigned long *parent_rate)
251 unsigned long best_rate;
252 unsigned long best_diff;
253 unsigned long cur_rate = *parent_rate;
254 unsigned long cur_diff;
255 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
257 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max)
260 if (periph->range.max) {
261 for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
262 cur_rate = *parent_rate >> shift;
263 if (cur_rate <= periph->range.max)
268 if (rate >= cur_rate)
271 best_diff = cur_rate - rate;
272 best_rate = cur_rate;
273 for (; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
274 cur_rate = *parent_rate >> shift;
276 cur_diff = rate - cur_rate;
278 cur_diff = cur_rate - rate;
280 if (cur_diff < best_diff) {
281 best_diff = cur_diff;
282 best_rate = cur_rate;
285 if (!best_diff || cur_rate < rate)
292 static int clk_sam9x5_peripheral_set_rate(struct clk_hw *hw,
294 unsigned long parent_rate)
297 struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
298 if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
299 if (parent_rate == rate)
305 if (periph->range.max && rate > periph->range.max)
308 for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
309 if (parent_rate >> shift == rate) {
310 periph->auto_div = false;
319 static const struct clk_ops sam9x5_peripheral_ops = {
320 .enable = clk_sam9x5_peripheral_enable,
321 .disable = clk_sam9x5_peripheral_disable,
322 .is_enabled = clk_sam9x5_peripheral_is_enabled,
323 .recalc_rate = clk_sam9x5_peripheral_recalc_rate,
324 .round_rate = clk_sam9x5_peripheral_round_rate,
325 .set_rate = clk_sam9x5_peripheral_set_rate,
328 struct clk_hw * __init
329 at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
330 const struct clk_pcr_layout *layout,
331 const char *name, const char *parent_name,
332 u32 id, const struct clk_range *range)
334 struct clk_sam9x5_peripheral *periph;
335 struct clk_init_data init;
339 if (!name || !parent_name)
340 return ERR_PTR(-EINVAL);
342 periph = kzalloc(sizeof(*periph), GFP_KERNEL);
344 return ERR_PTR(-ENOMEM);
347 init.ops = &sam9x5_peripheral_ops;
348 init.parent_names = (parent_name ? &parent_name : NULL);
349 init.num_parents = (parent_name ? 1 : 0);
353 periph->hw.init = &init;
355 periph->regmap = regmap;
357 if (layout->div_mask)
358 periph->auto_div = true;
359 periph->layout = layout;
360 periph->range = *range;
363 ret = clk_hw_register(NULL, &periph->hw);
368 clk_sam9x5_peripheral_autodiv(periph);