clk-usb.c 11 KB

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