clk-usb.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 SAM9X5_USB_DIV_SHIFT 8
  18. #define SAM9X5_USB_MAX_DIV 0xf
  19. #define RM9200_USB_DIV_SHIFT 28
  20. #define RM9200_USB_DIV_TAB_SIZE 4
  21. struct at91sam9x5_clk_usb {
  22. struct clk_hw hw;
  23. struct regmap *regmap;
  24. };
  25. #define to_at91sam9x5_clk_usb(hw) \
  26. container_of(hw, struct at91sam9x5_clk_usb, hw)
  27. struct at91rm9200_clk_usb {
  28. struct clk_hw hw;
  29. struct regmap *regmap;
  30. u32 divisors[4];
  31. };
  32. #define to_at91rm9200_clk_usb(hw) \
  33. container_of(hw, struct at91rm9200_clk_usb, hw)
  34. static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
  35. unsigned long parent_rate)
  36. {
  37. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  38. unsigned int usbr;
  39. u8 usbdiv;
  40. regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  41. usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  42. return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
  43. }
  44. static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
  45. struct clk_rate_request *req)
  46. {
  47. struct clk_hw *parent;
  48. long best_rate = -EINVAL;
  49. unsigned long tmp_rate;
  50. int best_diff = -1;
  51. int tmp_diff;
  52. int i;
  53. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  54. int div;
  55. parent = clk_hw_get_parent_by_index(hw, i);
  56. if (!parent)
  57. continue;
  58. for (div = 1; div < SAM9X5_USB_MAX_DIV + 2; div++) {
  59. unsigned long tmp_parent_rate;
  60. tmp_parent_rate = req->rate * div;
  61. tmp_parent_rate = clk_hw_round_rate(parent,
  62. tmp_parent_rate);
  63. tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div);
  64. if (tmp_rate < req->rate)
  65. tmp_diff = req->rate - tmp_rate;
  66. else
  67. tmp_diff = tmp_rate - req->rate;
  68. if (best_diff < 0 || best_diff > tmp_diff) {
  69. best_rate = tmp_rate;
  70. best_diff = tmp_diff;
  71. req->best_parent_rate = tmp_parent_rate;
  72. req->best_parent_hw = parent;
  73. }
  74. if (!best_diff || tmp_rate < req->rate)
  75. break;
  76. }
  77. if (!best_diff)
  78. break;
  79. }
  80. if (best_rate < 0)
  81. return best_rate;
  82. req->rate = best_rate;
  83. return 0;
  84. }
  85. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  86. {
  87. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  88. if (index > 1)
  89. return -EINVAL;
  90. regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  91. index ? AT91_PMC_USBS : 0);
  92. return 0;
  93. }
  94. static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
  95. {
  96. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  97. unsigned int usbr;
  98. regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  99. return usbr & AT91_PMC_USBS;
  100. }
  101. static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  102. unsigned long parent_rate)
  103. {
  104. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  105. unsigned long div;
  106. if (!rate)
  107. return -EINVAL;
  108. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  109. if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
  110. return -EINVAL;
  111. regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
  112. (div - 1) << SAM9X5_USB_DIV_SHIFT);
  113. return 0;
  114. }
  115. static const struct clk_ops at91sam9x5_usb_ops = {
  116. .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
  117. .determine_rate = at91sam9x5_clk_usb_determine_rate,
  118. .get_parent = at91sam9x5_clk_usb_get_parent,
  119. .set_parent = at91sam9x5_clk_usb_set_parent,
  120. .set_rate = at91sam9x5_clk_usb_set_rate,
  121. };
  122. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  123. {
  124. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  125. regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  126. AT91_PMC_USBS);
  127. return 0;
  128. }
  129. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  130. {
  131. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  132. regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
  133. }
  134. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  135. {
  136. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  137. unsigned int usbr;
  138. regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  139. return usbr & AT91_PMC_USBS;
  140. }
  141. static const struct clk_ops at91sam9n12_usb_ops = {
  142. .enable = at91sam9n12_clk_usb_enable,
  143. .disable = at91sam9n12_clk_usb_disable,
  144. .is_enabled = at91sam9n12_clk_usb_is_enabled,
  145. .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
  146. .determine_rate = at91sam9x5_clk_usb_determine_rate,
  147. .set_rate = at91sam9x5_clk_usb_set_rate,
  148. };
  149. struct clk_hw * __init
  150. at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  151. const char **parent_names, u8 num_parents)
  152. {
  153. struct at91sam9x5_clk_usb *usb;
  154. struct clk_hw *hw;
  155. struct clk_init_data init;
  156. int ret;
  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. hw = &usb->hw;
  169. ret = clk_hw_register(NULL, &usb->hw);
  170. if (ret) {
  171. kfree(usb);
  172. hw = ERR_PTR(ret);
  173. }
  174. return hw;
  175. }
  176. struct clk_hw * __init
  177. at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  178. const char *parent_name)
  179. {
  180. struct at91sam9x5_clk_usb *usb;
  181. struct clk_hw *hw;
  182. struct clk_init_data init;
  183. int ret;
  184. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  185. if (!usb)
  186. return ERR_PTR(-ENOMEM);
  187. init.name = name;
  188. init.ops = &at91sam9n12_usb_ops;
  189. init.parent_names = &parent_name;
  190. init.num_parents = 1;
  191. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  192. usb->hw.init = &init;
  193. usb->regmap = regmap;
  194. hw = &usb->hw;
  195. ret = clk_hw_register(NULL, &usb->hw);
  196. if (ret) {
  197. kfree(usb);
  198. hw = ERR_PTR(ret);
  199. }
  200. return hw;
  201. }
  202. static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  203. unsigned long parent_rate)
  204. {
  205. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  206. unsigned int pllbr;
  207. u8 usbdiv;
  208. regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  209. usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  210. if (usb->divisors[usbdiv])
  211. return parent_rate / usb->divisors[usbdiv];
  212. return 0;
  213. }
  214. static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  215. unsigned long *parent_rate)
  216. {
  217. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  218. struct clk_hw *parent = clk_hw_get_parent(hw);
  219. unsigned long bestrate = 0;
  220. int bestdiff = -1;
  221. unsigned long tmprate;
  222. int tmpdiff;
  223. int i = 0;
  224. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  225. unsigned long tmp_parent_rate;
  226. if (!usb->divisors[i])
  227. continue;
  228. tmp_parent_rate = rate * usb->divisors[i];
  229. tmp_parent_rate = clk_hw_round_rate(parent, tmp_parent_rate);
  230. tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
  231. if (tmprate < rate)
  232. tmpdiff = rate - tmprate;
  233. else
  234. tmpdiff = tmprate - rate;
  235. if (bestdiff < 0 || bestdiff > tmpdiff) {
  236. bestrate = tmprate;
  237. bestdiff = tmpdiff;
  238. *parent_rate = tmp_parent_rate;
  239. }
  240. if (!bestdiff)
  241. break;
  242. }
  243. return bestrate;
  244. }
  245. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  246. unsigned long parent_rate)
  247. {
  248. int i;
  249. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  250. unsigned long div;
  251. if (!rate)
  252. return -EINVAL;
  253. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  254. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  255. if (usb->divisors[i] == div) {
  256. regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  257. AT91_PMC_USBDIV,
  258. i << RM9200_USB_DIV_SHIFT);
  259. return 0;
  260. }
  261. }
  262. return -EINVAL;
  263. }
  264. static const struct clk_ops at91rm9200_usb_ops = {
  265. .recalc_rate = at91rm9200_clk_usb_recalc_rate,
  266. .round_rate = at91rm9200_clk_usb_round_rate,
  267. .set_rate = at91rm9200_clk_usb_set_rate,
  268. };
  269. struct clk_hw * __init
  270. at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  271. const char *parent_name, const u32 *divisors)
  272. {
  273. struct at91rm9200_clk_usb *usb;
  274. struct clk_hw *hw;
  275. struct clk_init_data init;
  276. int ret;
  277. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  278. if (!usb)
  279. return ERR_PTR(-ENOMEM);
  280. init.name = name;
  281. init.ops = &at91rm9200_usb_ops;
  282. init.parent_names = &parent_name;
  283. init.num_parents = 1;
  284. init.flags = CLK_SET_RATE_PARENT;
  285. usb->hw.init = &init;
  286. usb->regmap = regmap;
  287. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  288. hw = &usb->hw;
  289. ret = clk_hw_register(NULL, &usb->hw);
  290. if (ret) {
  291. kfree(usb);
  292. hw = ERR_PTR(ret);
  293. }
  294. return hw;
  295. }