xref: /openbmc/linux/drivers/clk/at91/clk-usb.c (revision 1fa0a7dc)
1 /*
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>
15 #include <linux/mfd/syscon.h>
16 #include <linux/regmap.h>
17 
18 #include "pmc.h"
19 
20 #define SAM9X5_USB_DIV_SHIFT	8
21 #define SAM9X5_USB_MAX_DIV	0xf
22 
23 #define RM9200_USB_DIV_SHIFT	28
24 #define RM9200_USB_DIV_TAB_SIZE	4
25 
26 #define SAM9X5_USBS_MASK	GENMASK(0, 0)
27 #define SAM9X60_USBS_MASK	GENMASK(1, 0)
28 
29 struct at91sam9x5_clk_usb {
30 	struct clk_hw hw;
31 	struct regmap *regmap;
32 	u32 usbs_mask;
33 };
34 
35 #define to_at91sam9x5_clk_usb(hw) \
36 	container_of(hw, struct at91sam9x5_clk_usb, hw)
37 
38 struct at91rm9200_clk_usb {
39 	struct clk_hw hw;
40 	struct regmap *regmap;
41 	u32 divisors[4];
42 };
43 
44 #define to_at91rm9200_clk_usb(hw) \
45 	container_of(hw, struct at91rm9200_clk_usb, hw)
46 
47 static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
48 						    unsigned long parent_rate)
49 {
50 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
51 	unsigned int usbr;
52 	u8 usbdiv;
53 
54 	regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
55 	usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
56 
57 	return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
58 }
59 
60 static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
61 					     struct clk_rate_request *req)
62 {
63 	struct clk_hw *parent;
64 	long best_rate = -EINVAL;
65 	unsigned long tmp_rate;
66 	int best_diff = -1;
67 	int tmp_diff;
68 	int i;
69 
70 	for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
71 		int div;
72 
73 		parent = clk_hw_get_parent_by_index(hw, i);
74 		if (!parent)
75 			continue;
76 
77 		for (div = 1; div < SAM9X5_USB_MAX_DIV + 2; div++) {
78 			unsigned long tmp_parent_rate;
79 
80 			tmp_parent_rate = req->rate * div;
81 			tmp_parent_rate = clk_hw_round_rate(parent,
82 							   tmp_parent_rate);
83 			tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div);
84 			if (tmp_rate < req->rate)
85 				tmp_diff = req->rate - tmp_rate;
86 			else
87 				tmp_diff = tmp_rate - req->rate;
88 
89 			if (best_diff < 0 || best_diff > tmp_diff) {
90 				best_rate = tmp_rate;
91 				best_diff = tmp_diff;
92 				req->best_parent_rate = tmp_parent_rate;
93 				req->best_parent_hw = parent;
94 			}
95 
96 			if (!best_diff || tmp_rate < req->rate)
97 				break;
98 		}
99 
100 		if (!best_diff)
101 			break;
102 	}
103 
104 	if (best_rate < 0)
105 		return best_rate;
106 
107 	req->rate = best_rate;
108 	return 0;
109 }
110 
111 static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
112 {
113 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
114 
115 	if (index > 1)
116 		return -EINVAL;
117 
118 	regmap_update_bits(usb->regmap, AT91_PMC_USB, usb->usbs_mask, index);
119 
120 	return 0;
121 }
122 
123 static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
124 {
125 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
126 	unsigned int usbr;
127 
128 	regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
129 
130 	return usbr & usb->usbs_mask;
131 }
132 
133 static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
134 				       unsigned long parent_rate)
135 {
136 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
137 	unsigned long div;
138 
139 	if (!rate)
140 		return -EINVAL;
141 
142 	div = DIV_ROUND_CLOSEST(parent_rate, rate);
143 	if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
144 		return -EINVAL;
145 
146 	regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
147 			   (div - 1) << SAM9X5_USB_DIV_SHIFT);
148 
149 	return 0;
150 }
151 
152 static const struct clk_ops at91sam9x5_usb_ops = {
153 	.recalc_rate = at91sam9x5_clk_usb_recalc_rate,
154 	.determine_rate = at91sam9x5_clk_usb_determine_rate,
155 	.get_parent = at91sam9x5_clk_usb_get_parent,
156 	.set_parent = at91sam9x5_clk_usb_set_parent,
157 	.set_rate = at91sam9x5_clk_usb_set_rate,
158 };
159 
160 static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
161 {
162 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
163 
164 	regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
165 			   AT91_PMC_USBS);
166 
167 	return 0;
168 }
169 
170 static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
171 {
172 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
173 
174 	regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
175 }
176 
177 static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
178 {
179 	struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
180 	unsigned int usbr;
181 
182 	regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
183 
184 	return usbr & AT91_PMC_USBS;
185 }
186 
187 static const struct clk_ops at91sam9n12_usb_ops = {
188 	.enable = at91sam9n12_clk_usb_enable,
189 	.disable = at91sam9n12_clk_usb_disable,
190 	.is_enabled = at91sam9n12_clk_usb_is_enabled,
191 	.recalc_rate = at91sam9x5_clk_usb_recalc_rate,
192 	.determine_rate = at91sam9x5_clk_usb_determine_rate,
193 	.set_rate = at91sam9x5_clk_usb_set_rate,
194 };
195 
196 static struct clk_hw * __init
197 _at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
198 			     const char **parent_names, u8 num_parents,
199 			     u32 usbs_mask)
200 {
201 	struct at91sam9x5_clk_usb *usb;
202 	struct clk_hw *hw;
203 	struct clk_init_data init;
204 	int ret;
205 
206 	usb = kzalloc(sizeof(*usb), GFP_KERNEL);
207 	if (!usb)
208 		return ERR_PTR(-ENOMEM);
209 
210 	init.name = name;
211 	init.ops = &at91sam9x5_usb_ops;
212 	init.parent_names = parent_names;
213 	init.num_parents = num_parents;
214 	init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
215 		     CLK_SET_RATE_PARENT;
216 
217 	usb->hw.init = &init;
218 	usb->regmap = regmap;
219 	usb->usbs_mask = SAM9X5_USBS_MASK;
220 
221 	hw = &usb->hw;
222 	ret = clk_hw_register(NULL, &usb->hw);
223 	if (ret) {
224 		kfree(usb);
225 		hw = ERR_PTR(ret);
226 	}
227 
228 	return hw;
229 }
230 
231 struct clk_hw * __init
232 at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
233 			    const char **parent_names, u8 num_parents)
234 {
235 	return _at91sam9x5_clk_register_usb(regmap, name, parent_names,
236 					    num_parents, SAM9X5_USBS_MASK);
237 }
238 
239 struct clk_hw * __init
240 sam9x60_clk_register_usb(struct regmap *regmap, const char *name,
241 			 const char **parent_names, u8 num_parents)
242 {
243 	return _at91sam9x5_clk_register_usb(regmap, name, parent_names,
244 					    num_parents, SAM9X60_USBS_MASK);
245 }
246 
247 struct clk_hw * __init
248 at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
249 			     const char *parent_name)
250 {
251 	struct at91sam9x5_clk_usb *usb;
252 	struct clk_hw *hw;
253 	struct clk_init_data init;
254 	int ret;
255 
256 	usb = kzalloc(sizeof(*usb), GFP_KERNEL);
257 	if (!usb)
258 		return ERR_PTR(-ENOMEM);
259 
260 	init.name = name;
261 	init.ops = &at91sam9n12_usb_ops;
262 	init.parent_names = &parent_name;
263 	init.num_parents = 1;
264 	init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
265 
266 	usb->hw.init = &init;
267 	usb->regmap = regmap;
268 
269 	hw = &usb->hw;
270 	ret = clk_hw_register(NULL, &usb->hw);
271 	if (ret) {
272 		kfree(usb);
273 		hw = ERR_PTR(ret);
274 	}
275 
276 	return hw;
277 }
278 
279 static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
280 						    unsigned long parent_rate)
281 {
282 	struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
283 	unsigned int pllbr;
284 	u8 usbdiv;
285 
286 	regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
287 
288 	usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
289 	if (usb->divisors[usbdiv])
290 		return parent_rate / usb->divisors[usbdiv];
291 
292 	return 0;
293 }
294 
295 static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
296 					  unsigned long *parent_rate)
297 {
298 	struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
299 	struct clk_hw *parent = clk_hw_get_parent(hw);
300 	unsigned long bestrate = 0;
301 	int bestdiff = -1;
302 	unsigned long tmprate;
303 	int tmpdiff;
304 	int i = 0;
305 
306 	for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
307 		unsigned long tmp_parent_rate;
308 
309 		if (!usb->divisors[i])
310 			continue;
311 
312 		tmp_parent_rate = rate * usb->divisors[i];
313 		tmp_parent_rate = clk_hw_round_rate(parent, tmp_parent_rate);
314 		tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
315 		if (tmprate < rate)
316 			tmpdiff = rate - tmprate;
317 		else
318 			tmpdiff = tmprate - rate;
319 
320 		if (bestdiff < 0 || bestdiff > tmpdiff) {
321 			bestrate = tmprate;
322 			bestdiff = tmpdiff;
323 			*parent_rate = tmp_parent_rate;
324 		}
325 
326 		if (!bestdiff)
327 			break;
328 	}
329 
330 	return bestrate;
331 }
332 
333 static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
334 				       unsigned long parent_rate)
335 {
336 	int i;
337 	struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
338 	unsigned long div;
339 
340 	if (!rate)
341 		return -EINVAL;
342 
343 	div = DIV_ROUND_CLOSEST(parent_rate, rate);
344 
345 	for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
346 		if (usb->divisors[i] == div) {
347 			regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
348 					   AT91_PMC_USBDIV,
349 					   i << RM9200_USB_DIV_SHIFT);
350 
351 			return 0;
352 		}
353 	}
354 
355 	return -EINVAL;
356 }
357 
358 static const struct clk_ops at91rm9200_usb_ops = {
359 	.recalc_rate = at91rm9200_clk_usb_recalc_rate,
360 	.round_rate = at91rm9200_clk_usb_round_rate,
361 	.set_rate = at91rm9200_clk_usb_set_rate,
362 };
363 
364 struct clk_hw * __init
365 at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
366 			    const char *parent_name, const u32 *divisors)
367 {
368 	struct at91rm9200_clk_usb *usb;
369 	struct clk_hw *hw;
370 	struct clk_init_data init;
371 	int ret;
372 
373 	usb = kzalloc(sizeof(*usb), GFP_KERNEL);
374 	if (!usb)
375 		return ERR_PTR(-ENOMEM);
376 
377 	init.name = name;
378 	init.ops = &at91rm9200_usb_ops;
379 	init.parent_names = &parent_name;
380 	init.num_parents = 1;
381 	init.flags = CLK_SET_RATE_PARENT;
382 
383 	usb->hw.init = &init;
384 	usb->regmap = regmap;
385 	memcpy(usb->divisors, divisors, sizeof(usb->divisors));
386 
387 	hw = &usb->hw;
388 	ret = clk_hw_register(NULL, &usb->hw);
389 	if (ret) {
390 		kfree(usb);
391 		hw = ERR_PTR(ret);
392 	}
393 
394 	return hw;
395 }
396