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>
20 #define USB_SOURCE_MAX 2
22 #define SAM9X5_USB_DIV_SHIFT 8
23 #define SAM9X5_USB_MAX_DIV 0xf
25 #define RM9200_USB_DIV_SHIFT 28
26 #define RM9200_USB_DIV_TAB_SIZE 4
28 struct at91sam9x5_clk_usb {
33 #define to_at91sam9x5_clk_usb(hw) \
34 container_of(hw, struct at91sam9x5_clk_usb, hw)
36 struct at91rm9200_clk_usb {
42 #define to_at91rm9200_clk_usb(hw) \
43 container_of(hw, struct at91rm9200_clk_usb, hw)
45 static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
46 unsigned long parent_rate)
50 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
51 struct at91_pmc *pmc = usb->pmc;
53 tmp = pmc_read(pmc, AT91_PMC_USB);
54 usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
56 return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
59 static long at91sam9x5_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
60 unsigned long *parent_rate)
67 if (rate >= *parent_rate)
70 div = DIV_ROUND_CLOSEST(*parent_rate, rate);
71 if (div > SAM9X5_USB_MAX_DIV + 1)
72 div = SAM9X5_USB_MAX_DIV + 1;
74 return DIV_ROUND_CLOSEST(*parent_rate, div);
77 static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
80 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
81 struct at91_pmc *pmc = usb->pmc;
85 tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
88 pmc_write(pmc, AT91_PMC_USB, tmp);
92 static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
94 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
95 struct at91_pmc *pmc = usb->pmc;
97 return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
100 static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
101 unsigned long parent_rate)
104 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
105 struct at91_pmc *pmc = usb->pmc;
111 div = DIV_ROUND_CLOSEST(parent_rate, rate);
112 if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
115 tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
116 tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
117 pmc_write(pmc, AT91_PMC_USB, tmp);
122 static const struct clk_ops at91sam9x5_usb_ops = {
123 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
124 .round_rate = at91sam9x5_clk_usb_round_rate,
125 .get_parent = at91sam9x5_clk_usb_get_parent,
126 .set_parent = at91sam9x5_clk_usb_set_parent,
127 .set_rate = at91sam9x5_clk_usb_set_rate,
130 static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
132 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
133 struct at91_pmc *pmc = usb->pmc;
135 pmc_write(pmc, AT91_PMC_USB,
136 pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
140 static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
142 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
143 struct at91_pmc *pmc = usb->pmc;
145 pmc_write(pmc, AT91_PMC_USB,
146 pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
149 static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
151 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
152 struct at91_pmc *pmc = usb->pmc;
154 return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
157 static const struct clk_ops at91sam9n12_usb_ops = {
158 .enable = at91sam9n12_clk_usb_enable,
159 .disable = at91sam9n12_clk_usb_disable,
160 .is_enabled = at91sam9n12_clk_usb_is_enabled,
161 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
162 .round_rate = at91sam9x5_clk_usb_round_rate,
163 .set_rate = at91sam9x5_clk_usb_set_rate,
166 static struct clk * __init
167 at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
168 const char **parent_names, u8 num_parents)
170 struct at91sam9x5_clk_usb *usb;
171 struct clk *clk = NULL;
172 struct clk_init_data init;
174 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
176 return ERR_PTR(-ENOMEM);
179 init.ops = &at91sam9x5_usb_ops;
180 init.parent_names = parent_names;
181 init.num_parents = num_parents;
182 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
184 usb->hw.init = &init;
187 clk = clk_register(NULL, &usb->hw);
194 static struct clk * __init
195 at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
196 const char *parent_name)
198 struct at91sam9x5_clk_usb *usb;
199 struct clk *clk = NULL;
200 struct clk_init_data init;
202 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
207 init.ops = &at91sam9n12_usb_ops;
208 init.parent_names = &parent_name;
209 init.num_parents = 1;
210 init.flags = CLK_SET_RATE_GATE;
212 usb->hw.init = &init;
215 clk = clk_register(NULL, &usb->hw);
222 static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
223 unsigned long parent_rate)
225 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
226 struct at91_pmc *pmc = usb->pmc;
230 tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
231 usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
232 if (usb->divisors[usbdiv])
233 return parent_rate / usb->divisors[usbdiv];
238 static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
239 unsigned long *parent_rate)
241 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
242 struct clk *parent = __clk_get_parent(hw->clk);
243 unsigned long bestrate = 0;
245 unsigned long tmprate;
249 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
250 unsigned long tmp_parent_rate;
252 if (!usb->divisors[i])
255 tmp_parent_rate = rate * usb->divisors[i];
256 tmp_parent_rate = __clk_round_rate(parent, tmp_parent_rate);
257 tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
259 tmpdiff = rate - tmprate;
261 tmpdiff = tmprate - rate;
263 if (bestdiff < 0 || bestdiff > tmpdiff) {
266 *parent_rate = tmp_parent_rate;
276 static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
277 unsigned long parent_rate)
281 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
282 struct at91_pmc *pmc = usb->pmc;
288 div = DIV_ROUND_CLOSEST(parent_rate, rate);
290 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
291 if (usb->divisors[i] == div) {
292 tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
294 tmp |= i << RM9200_USB_DIV_SHIFT;
295 pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
303 static const struct clk_ops at91rm9200_usb_ops = {
304 .recalc_rate = at91rm9200_clk_usb_recalc_rate,
305 .round_rate = at91rm9200_clk_usb_round_rate,
306 .set_rate = at91rm9200_clk_usb_set_rate,
309 static struct clk * __init
310 at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
311 const char *parent_name, const u32 *divisors)
313 struct at91rm9200_clk_usb *usb;
314 struct clk *clk = NULL;
315 struct clk_init_data init;
317 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
319 return ERR_PTR(-ENOMEM);
322 init.ops = &at91rm9200_usb_ops;
323 init.parent_names = &parent_name;
324 init.num_parents = 1;
325 init.flags = CLK_SET_RATE_PARENT;
327 usb->hw.init = &init;
329 memcpy(usb->divisors, divisors, sizeof(usb->divisors));
331 clk = clk_register(NULL, &usb->hw);
338 void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
339 struct at91_pmc *pmc)
344 const char *parent_names[USB_SOURCE_MAX];
345 const char *name = np->name;
347 num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
348 if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
351 for (i = 0; i < num_parents; i++) {
352 parent_names[i] = of_clk_get_parent_name(np, i);
353 if (!parent_names[i])
357 of_property_read_string(np, "clock-output-names", &name);
359 clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
363 of_clk_add_provider(np, of_clk_src_simple_get, clk);
366 void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
367 struct at91_pmc *pmc)
370 const char *parent_name;
371 const char *name = np->name;
373 parent_name = of_clk_get_parent_name(np, 0);
377 of_property_read_string(np, "clock-output-names", &name);
379 clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
383 of_clk_add_provider(np, of_clk_src_simple_get, clk);
386 void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
387 struct at91_pmc *pmc)
390 const char *parent_name;
391 const char *name = np->name;
392 u32 divisors[4] = {0, 0, 0, 0};
394 parent_name = of_clk_get_parent_name(np, 0);
398 of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
402 of_property_read_string(np, "clock-output-names", &name);
404 clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
408 of_clk_add_provider(np, of_clk_src_simple_get, clk);