clk-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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_hw * __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_hw *hw;
  156. struct clk_init_data init;
  157. int ret;
  158. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  159. if (!usb)
  160. return ERR_PTR(-ENOMEM);
  161. init.name = name;
  162. init.ops = &at91sam9x5_usb_ops;
  163. init.parent_names = parent_names;
  164. init.num_parents = num_parents;
  165. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  166. CLK_SET_RATE_PARENT;
  167. usb->hw.init = &init;
  168. usb->regmap = regmap;
  169. hw = &usb->hw;
  170. ret = clk_hw_register(NULL, &usb->hw);
  171. if (ret) {
  172. kfree(usb);
  173. hw = ERR_PTR(ret);
  174. }
  175. return hw;
  176. }
  177. static struct clk_hw * __init
  178. at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  179. const char *parent_name)
  180. {
  181. struct at91sam9x5_clk_usb *usb;
  182. struct clk_hw *hw;
  183. struct clk_init_data init;
  184. int ret;
  185. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  186. if (!usb)
  187. return ERR_PTR(-ENOMEM);
  188. init.name = name;
  189. init.ops = &at91sam9n12_usb_ops;
  190. init.parent_names = &parent_name;
  191. init.num_parents = 1;
  192. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  193. usb->hw.init = &init;
  194. usb->regmap = regmap;
  195. hw = &usb->hw;
  196. ret = clk_hw_register(NULL, &usb->hw);
  197. if (ret) {
  198. kfree(usb);
  199. hw = ERR_PTR(ret);
  200. }
  201. return hw;
  202. }
  203. static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  204. unsigned long parent_rate)
  205. {
  206. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  207. unsigned int pllbr;
  208. u8 usbdiv;
  209. regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  210. usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  211. if (usb->divisors[usbdiv])
  212. return parent_rate / usb->divisors[usbdiv];
  213. return 0;
  214. }
  215. static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  216. unsigned long *parent_rate)
  217. {
  218. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  219. struct clk_hw *parent = clk_hw_get_parent(hw);
  220. unsigned long bestrate = 0;
  221. int bestdiff = -1;
  222. unsigned long tmprate;
  223. int tmpdiff;
  224. int i = 0;
  225. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  226. unsigned long tmp_parent_rate;
  227. if (!usb->divisors[i])
  228. continue;
  229. tmp_parent_rate = rate * usb->divisors[i];
  230. tmp_parent_rate = clk_hw_round_rate(parent, tmp_parent_rate);
  231. tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
  232. if (tmprate < rate)
  233. tmpdiff = rate - tmprate;
  234. else
  235. tmpdiff = tmprate - rate;
  236. if (bestdiff < 0 || bestdiff > tmpdiff) {
  237. bestrate = tmprate;
  238. bestdiff = tmpdiff;
  239. *parent_rate = tmp_parent_rate;
  240. }
  241. if (!bestdiff)
  242. break;
  243. }
  244. return bestrate;
  245. }
  246. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  247. unsigned long parent_rate)
  248. {
  249. int i;
  250. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  251. unsigned long div;
  252. if (!rate)
  253. return -EINVAL;
  254. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  255. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  256. if (usb->divisors[i] == div) {
  257. regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  258. AT91_PMC_USBDIV,
  259. i << RM9200_USB_DIV_SHIFT);
  260. return 0;
  261. }
  262. }
  263. return -EINVAL;
  264. }
  265. static const struct clk_ops at91rm9200_usb_ops = {
  266. .recalc_rate = at91rm9200_clk_usb_recalc_rate,
  267. .round_rate = at91rm9200_clk_usb_round_rate,
  268. .set_rate = at91rm9200_clk_usb_set_rate,
  269. };
  270. static struct clk_hw * __init
  271. at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  272. const char *parent_name, const u32 *divisors)
  273. {
  274. struct at91rm9200_clk_usb *usb;
  275. struct clk_hw *hw;
  276. struct clk_init_data init;
  277. int ret;
  278. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  279. if (!usb)
  280. return ERR_PTR(-ENOMEM);
  281. init.name = name;
  282. init.ops = &at91rm9200_usb_ops;
  283. init.parent_names = &parent_name;
  284. init.num_parents = 1;
  285. init.flags = CLK_SET_RATE_PARENT;
  286. usb->hw.init = &init;
  287. usb->regmap = regmap;
  288. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  289. hw = &usb->hw;
  290. ret = clk_hw_register(NULL, &usb->hw);
  291. if (ret) {
  292. kfree(usb);
  293. hw = ERR_PTR(ret);
  294. }
  295. return hw;
  296. }
  297. static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  298. {
  299. struct clk_hw *hw;
  300. unsigned int num_parents;
  301. const char *parent_names[USB_SOURCE_MAX];
  302. const char *name = np->name;
  303. struct regmap *regmap;
  304. num_parents = of_clk_get_parent_count(np);
  305. if (num_parents == 0 || num_parents > USB_SOURCE_MAX)
  306. return;
  307. of_clk_parent_fill(np, parent_names, num_parents);
  308. of_property_read_string(np, "clock-output-names", &name);
  309. regmap = syscon_node_to_regmap(of_get_parent(np));
  310. if (IS_ERR(regmap))
  311. return;
  312. hw = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  313. num_parents);
  314. if (IS_ERR(hw))
  315. return;
  316. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  317. }
  318. CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  319. of_at91sam9x5_clk_usb_setup);
  320. static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  321. {
  322. struct clk_hw *hw;
  323. const char *parent_name;
  324. const char *name = np->name;
  325. struct regmap *regmap;
  326. parent_name = of_clk_get_parent_name(np, 0);
  327. if (!parent_name)
  328. return;
  329. of_property_read_string(np, "clock-output-names", &name);
  330. regmap = syscon_node_to_regmap(of_get_parent(np));
  331. if (IS_ERR(regmap))
  332. return;
  333. hw = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  334. if (IS_ERR(hw))
  335. return;
  336. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  337. }
  338. CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  339. of_at91sam9n12_clk_usb_setup);
  340. static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  341. {
  342. struct clk_hw *hw;
  343. const char *parent_name;
  344. const char *name = np->name;
  345. u32 divisors[4] = {0, 0, 0, 0};
  346. struct regmap *regmap;
  347. parent_name = of_clk_get_parent_name(np, 0);
  348. if (!parent_name)
  349. return;
  350. of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
  351. if (!divisors[0])
  352. return;
  353. of_property_read_string(np, "clock-output-names", &name);
  354. regmap = syscon_node_to_regmap(of_get_parent(np));
  355. if (IS_ERR(regmap))
  356. return;
  357. hw = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  358. if (IS_ERR(hw))
  359. return;
  360. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  361. }
  362. CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  363. of_at91rm9200_clk_usb_setup);