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