blob: 85a449cf61e3fa79b36849f84a831e05683d48dc [file] [log] [blame]
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +02001/*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3 *
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.
8 *
9 */
10
11#include <linux/clk-provider.h>
12#include <linux/clkdev.h>
13#include <linux/clk/at91_pmc.h>
14#include <linux/of.h>
Boris Brezillon1bdf0232014-09-07 08:14:29 +020015#include <linux/mfd/syscon.h>
16#include <linux/regmap.h>
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020017
18#include "pmc.h"
19
20#define PROG_SOURCE_MAX 5
21#define PROG_ID_MAX 7
22
23#define PROG_STATUS_MASK(id) (1 << ((id) + 8))
24#define PROG_PRES_MASK 0x7
Boris Brezillon1bdf0232014-09-07 08:14:29 +020025#define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & PROG_PRES_MASK)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020026#define PROG_MAX_RM9200_CSS 3
27
28struct clk_programmable_layout {
29 u8 pres_shift;
30 u8 css_mask;
31 u8 have_slck_mck;
32};
33
34struct clk_programmable {
35 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020036 struct regmap *regmap;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020037 u8 id;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020038 const struct clk_programmable_layout *layout;
39};
40
41#define to_clk_programmable(hw) container_of(hw, struct clk_programmable, hw)
42
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020043static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
44 unsigned long parent_rate)
45{
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020046 struct clk_programmable *prog = to_clk_programmable(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020047 unsigned int pckr;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020048
Boris Brezillon1bdf0232014-09-07 08:14:29 +020049 regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
50
51 return parent_rate >> PROG_PRES(prog->layout, pckr);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020052}
53
Boris Brezillon0817b622015-07-07 20:48:08 +020054static int clk_programmable_determine_rate(struct clk_hw *hw,
55 struct clk_rate_request *req)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020056{
Stephen Boydd0979332015-07-30 17:20:57 -070057 struct clk_hw *parent;
Boris BREZILLON419f6122014-03-11 10:00:32 +010058 long best_rate = -EINVAL;
59 unsigned long parent_rate;
60 unsigned long tmp_rate;
61 int shift;
62 int i;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020063
Stephen Boyd497295a2015-06-25 16:53:23 -070064 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
Stephen Boydd0979332015-07-30 17:20:57 -070065 parent = clk_hw_get_parent_by_index(hw, i);
Boris BREZILLON419f6122014-03-11 10:00:32 +010066 if (!parent)
67 continue;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020068
Stephen Boydd0979332015-07-30 17:20:57 -070069 parent_rate = clk_hw_get_rate(parent);
Boris BREZILLON419f6122014-03-11 10:00:32 +010070 for (shift = 0; shift < PROG_PRES_MASK; shift++) {
71 tmp_rate = parent_rate >> shift;
Boris Brezillon0817b622015-07-07 20:48:08 +020072 if (tmp_rate <= req->rate)
Boris BREZILLON419f6122014-03-11 10:00:32 +010073 break;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020074 }
75
Boris Brezillon0817b622015-07-07 20:48:08 +020076 if (tmp_rate > req->rate)
Boris BREZILLON419f6122014-03-11 10:00:32 +010077 continue;
78
Boris Brezillon0817b622015-07-07 20:48:08 +020079 if (best_rate < 0 ||
80 (req->rate - tmp_rate) < (req->rate - best_rate)) {
Boris BREZILLON419f6122014-03-11 10:00:32 +010081 best_rate = tmp_rate;
Boris Brezillon0817b622015-07-07 20:48:08 +020082 req->best_parent_rate = parent_rate;
Stephen Boydd0979332015-07-30 17:20:57 -070083 req->best_parent_hw = parent;
Boris BREZILLON419f6122014-03-11 10:00:32 +010084 }
85
86 if (!best_rate)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020087 break;
88 }
89
Boris Brezillon0817b622015-07-07 20:48:08 +020090 if (best_rate < 0)
91 return best_rate;
92
93 req->rate = best_rate;
94 return 0;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +020095}
96
97static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
98{
99 struct clk_programmable *prog = to_clk_programmable(hw);
100 const struct clk_programmable_layout *layout = prog->layout;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200101 unsigned int mask = layout->css_mask;
Boris Brezillonf96423f42016-07-18 09:49:12 +0200102 unsigned int pckr = index;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100103
104 if (layout->have_slck_mck)
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200105 mask |= AT91_PMC_CSSMCK_MCK;
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100106
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200107 if (index > layout->css_mask) {
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200108 if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200109 return -EINVAL;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200110
111 pckr |= AT91_PMC_CSSMCK_MCK;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200112 }
113
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200114 regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
115
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200116 return 0;
117}
118
119static u8 clk_programmable_get_parent(struct clk_hw *hw)
120{
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200121 struct clk_programmable *prog = to_clk_programmable(hw);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200122 const struct clk_programmable_layout *layout = prog->layout;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200123 unsigned int pckr;
124 u8 ret;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200125
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200126 regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
127
128 ret = pckr & layout->css_mask;
129
130 if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100131 ret = PROG_MAX_RM9200_CSS + 1;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200132
133 return ret;
134}
135
136static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
137 unsigned long parent_rate)
138{
139 struct clk_programmable *prog = to_clk_programmable(hw);
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100140 const struct clk_programmable_layout *layout = prog->layout;
Jean-Jacques Hiblot141c71d2014-03-11 10:00:35 +0100141 unsigned long div = parent_rate / rate;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200142 unsigned int pckr;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200143 int shift = 0;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200144
145 regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200146
Jean-Jacques Hiblot141c71d2014-03-11 10:00:35 +0100147 if (!div)
148 return -EINVAL;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200149
Jean-Jacques Hiblot141c71d2014-03-11 10:00:35 +0100150 shift = fls(div) - 1;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200151
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200152 if (div != (1 << shift))
Jean-Jacques Hiblot141c71d2014-03-11 10:00:35 +0100153 return -EINVAL;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200154
Jean-Jacques Hiblot141c71d2014-03-11 10:00:35 +0100155 if (shift >= PROG_PRES_MASK)
156 return -EINVAL;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200157
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200158 regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
159 PROG_PRES_MASK << layout->pres_shift,
160 shift << layout->pres_shift);
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +0100161
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200162 return 0;
163}
164
165static const struct clk_ops programmable_ops = {
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200166 .recalc_rate = clk_programmable_recalc_rate,
Boris BREZILLON419f6122014-03-11 10:00:32 +0100167 .determine_rate = clk_programmable_determine_rate,
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200168 .get_parent = clk_programmable_get_parent,
169 .set_parent = clk_programmable_set_parent,
170 .set_rate = clk_programmable_set_rate,
171};
172
Stephen Boydf5644f12016-06-01 14:31:22 -0700173static struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200174at91_clk_register_programmable(struct regmap *regmap,
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200175 const char *name, const char **parent_names,
176 u8 num_parents, u8 id,
177 const struct clk_programmable_layout *layout)
178{
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200179 struct clk_programmable *prog;
Stephen Boydf5644f12016-06-01 14:31:22 -0700180 struct clk_hw *hw;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200181 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700182 int ret;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200183
184 if (id > PROG_ID_MAX)
185 return ERR_PTR(-EINVAL);
186
187 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
188 if (!prog)
189 return ERR_PTR(-ENOMEM);
190
191 init.name = name;
192 init.ops = &programmable_ops;
193 init.parent_names = parent_names;
194 init.num_parents = num_parents;
195 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
196
197 prog->id = id;
198 prog->layout = layout;
199 prog->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200200 prog->regmap = regmap;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200201
Stephen Boydf5644f12016-06-01 14:31:22 -0700202 hw = &prog->hw;
203 ret = clk_hw_register(NULL, &prog->hw);
204 if (ret) {
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200205 kfree(prog);
Christophe JAILLET91bbc172016-09-25 13:53:58 +0200206 hw = ERR_PTR(ret);
Stephen Boydf5644f12016-06-01 14:31:22 -0700207 }
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200208
Stephen Boydf5644f12016-06-01 14:31:22 -0700209 return hw;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200210}
211
212static const struct clk_programmable_layout at91rm9200_programmable_layout = {
213 .pres_shift = 2,
214 .css_mask = 0x3,
215 .have_slck_mck = 0,
216};
217
218static const struct clk_programmable_layout at91sam9g45_programmable_layout = {
219 .pres_shift = 2,
220 .css_mask = 0x3,
221 .have_slck_mck = 1,
222};
223
224static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
225 .pres_shift = 4,
226 .css_mask = 0x7,
227 .have_slck_mck = 0,
228};
229
230static void __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200231of_at91_clk_prog_setup(struct device_node *np,
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200232 const struct clk_programmable_layout *layout)
233{
234 int num;
235 u32 id;
Stephen Boydf5644f12016-06-01 14:31:22 -0700236 struct clk_hw *hw;
Stephen Boyd8c1b1e52016-02-19 17:29:17 -0800237 unsigned int num_parents;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200238 const char *parent_names[PROG_SOURCE_MAX];
239 const char *name;
240 struct device_node *progclknp;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200241 struct regmap *regmap;
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200242
Geert Uytterhoeven51a43be2015-05-29 11:25:45 +0200243 num_parents = of_clk_get_parent_count(np);
Stephen Boyd8c1b1e52016-02-19 17:29:17 -0800244 if (num_parents == 0 || num_parents > PROG_SOURCE_MAX)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200245 return;
246
Dinh Nguyenf0557fb2015-07-06 22:59:01 -0500247 of_clk_parent_fill(np, parent_names, num_parents);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200248
249 num = of_get_child_count(np);
250 if (!num || num > (PROG_ID_MAX + 1))
251 return;
252
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200253 regmap = syscon_node_to_regmap(of_get_parent(np));
254 if (IS_ERR(regmap))
255 return;
256
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200257 for_each_child_of_node(np, progclknp) {
258 if (of_property_read_u32(progclknp, "reg", &id))
259 continue;
260
261 if (of_property_read_string(np, "clock-output-names", &name))
262 name = progclknp->name;
263
Stephen Boydf5644f12016-06-01 14:31:22 -0700264 hw = at91_clk_register_programmable(regmap, name,
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200265 parent_names, num_parents,
266 id, layout);
Stephen Boydf5644f12016-06-01 14:31:22 -0700267 if (IS_ERR(hw))
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200268 continue;
269
Stephen Boydf5644f12016-06-01 14:31:22 -0700270 of_clk_add_hw_provider(progclknp, of_clk_hw_simple_get, hw);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200271 }
272}
273
274
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200275static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200276{
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200277 of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200278}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200279CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
280 of_at91rm9200_clk_prog_setup);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200281
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200282static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200283{
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200284 of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200285}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200286CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
287 of_at91sam9g45_clk_prog_setup);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200288
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200289static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200290{
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200291 of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200292}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200293CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
294 of_at91sam9x5_clk_prog_setup);