clk-usb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Boris BREZILLON <[email protected]>
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/clkdev.h>
  7. #include <linux/clk/at91_pmc.h>
  8. #include <linux/of.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/regmap.h>
  11. #include "pmc.h"
  12. #define SAM9X5_USB_DIV_SHIFT 8
  13. #define SAM9X5_USB_MAX_DIV 0xf
  14. #define RM9200_USB_DIV_SHIFT 28
  15. #define RM9200_USB_DIV_TAB_SIZE 4
  16. #define SAM9X5_USBS_MASK GENMASK(0, 0)
  17. #define SAM9X60_USBS_MASK GENMASK(1, 0)
  18. struct at91sam9x5_clk_usb {
  19. struct clk_hw hw;
  20. struct regmap *regmap;
  21. struct at91_clk_pms pms;
  22. u32 usbs_mask;
  23. u8 num_parents;
  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. if (!tmp_parent_rate)
  64. continue;
  65. tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div);
  66. if (tmp_rate < req->rate)
  67. tmp_diff = req->rate - tmp_rate;
  68. else
  69. tmp_diff = tmp_rate - req->rate;
  70. if (best_diff < 0 || best_diff > tmp_diff) {
  71. best_rate = tmp_rate;
  72. best_diff = tmp_diff;
  73. req->best_parent_rate = tmp_parent_rate;
  74. req->best_parent_hw = parent;
  75. }
  76. if (!best_diff || tmp_rate < req->rate)
  77. break;
  78. }
  79. if (!best_diff)
  80. break;
  81. }
  82. if (best_rate < 0)
  83. return best_rate;
  84. req->rate = best_rate;
  85. return 0;
  86. }
  87. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  88. {
  89. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  90. if (index >= usb->num_parents)
  91. return -EINVAL;
  92. regmap_update_bits(usb->regmap, AT91_PMC_USB, usb->usbs_mask, index);
  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 & usb->usbs_mask;
  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 int at91sam9x5_usb_save_context(struct clk_hw *hw)
  117. {
  118. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  119. struct clk_hw *parent_hw = clk_hw_get_parent(hw);
  120. usb->pms.parent = at91sam9x5_clk_usb_get_parent(hw);
  121. usb->pms.parent_rate = clk_hw_get_rate(parent_hw);
  122. usb->pms.rate = at91sam9x5_clk_usb_recalc_rate(hw, usb->pms.parent_rate);
  123. return 0;
  124. }
  125. static void at91sam9x5_usb_restore_context(struct clk_hw *hw)
  126. {
  127. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  128. int ret;
  129. ret = at91sam9x5_clk_usb_set_parent(hw, usb->pms.parent);
  130. if (ret)
  131. return;
  132. at91sam9x5_clk_usb_set_rate(hw, usb->pms.rate, usb->pms.parent_rate);
  133. }
  134. static const struct clk_ops at91sam9x5_usb_ops = {
  135. .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
  136. .determine_rate = at91sam9x5_clk_usb_determine_rate,
  137. .get_parent = at91sam9x5_clk_usb_get_parent,
  138. .set_parent = at91sam9x5_clk_usb_set_parent,
  139. .set_rate = at91sam9x5_clk_usb_set_rate,
  140. .save_context = at91sam9x5_usb_save_context,
  141. .restore_context = at91sam9x5_usb_restore_context,
  142. };
  143. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  144. {
  145. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  146. regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  147. AT91_PMC_USBS);
  148. return 0;
  149. }
  150. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  151. {
  152. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  153. regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
  154. }
  155. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  156. {
  157. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  158. unsigned int usbr;
  159. regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  160. return usbr & AT91_PMC_USBS;
  161. }
  162. static const struct clk_ops at91sam9n12_usb_ops = {
  163. .enable = at91sam9n12_clk_usb_enable,
  164. .disable = at91sam9n12_clk_usb_disable,
  165. .is_enabled = at91sam9n12_clk_usb_is_enabled,
  166. .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
  167. .determine_rate = at91sam9x5_clk_usb_determine_rate,
  168. .set_rate = at91sam9x5_clk_usb_set_rate,
  169. };
  170. static struct clk_hw * __init
  171. _at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  172. const char **parent_names, u8 num_parents,
  173. u32 usbs_mask)
  174. {
  175. struct at91sam9x5_clk_usb *usb;
  176. struct clk_hw *hw;
  177. struct clk_init_data init;
  178. int ret;
  179. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  180. if (!usb)
  181. return ERR_PTR(-ENOMEM);
  182. init.name = name;
  183. init.ops = &at91sam9x5_usb_ops;
  184. init.parent_names = parent_names;
  185. init.num_parents = num_parents;
  186. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  187. CLK_SET_RATE_PARENT;
  188. usb->hw.init = &init;
  189. usb->regmap = regmap;
  190. usb->usbs_mask = usbs_mask;
  191. usb->num_parents = num_parents;
  192. hw = &usb->hw;
  193. ret = clk_hw_register(NULL, &usb->hw);
  194. if (ret) {
  195. kfree(usb);
  196. hw = ERR_PTR(ret);
  197. }
  198. return hw;
  199. }
  200. struct clk_hw * __init
  201. at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  202. const char **parent_names, u8 num_parents)
  203. {
  204. return _at91sam9x5_clk_register_usb(regmap, name, parent_names,
  205. num_parents, SAM9X5_USBS_MASK);
  206. }
  207. struct clk_hw * __init
  208. sam9x60_clk_register_usb(struct regmap *regmap, const char *name,
  209. const char **parent_names, u8 num_parents)
  210. {
  211. return _at91sam9x5_clk_register_usb(regmap, name, parent_names,
  212. num_parents, SAM9X60_USBS_MASK);
  213. }
  214. struct clk_hw * __init
  215. at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  216. const char *parent_name)
  217. {
  218. struct at91sam9x5_clk_usb *usb;
  219. struct clk_hw *hw;
  220. struct clk_init_data init;
  221. int ret;
  222. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  223. if (!usb)
  224. return ERR_PTR(-ENOMEM);
  225. init.name = name;
  226. init.ops = &at91sam9n12_usb_ops;
  227. init.parent_names = &parent_name;
  228. init.num_parents = 1;
  229. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  230. usb->hw.init = &init;
  231. usb->regmap = regmap;
  232. hw = &usb->hw;
  233. ret = clk_hw_register(NULL, &usb->hw);
  234. if (ret) {
  235. kfree(usb);
  236. hw = ERR_PTR(ret);
  237. }
  238. return hw;
  239. }
  240. static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  241. unsigned long parent_rate)
  242. {
  243. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  244. unsigned int pllbr;
  245. u8 usbdiv;
  246. regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  247. usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  248. if (usb->divisors[usbdiv])
  249. return parent_rate / usb->divisors[usbdiv];
  250. return 0;
  251. }
  252. static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  253. unsigned long *parent_rate)
  254. {
  255. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  256. struct clk_hw *parent = clk_hw_get_parent(hw);
  257. unsigned long bestrate = 0;
  258. int bestdiff = -1;
  259. unsigned long tmprate;
  260. int tmpdiff;
  261. int i = 0;
  262. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  263. unsigned long tmp_parent_rate;
  264. if (!usb->divisors[i])
  265. continue;
  266. tmp_parent_rate = rate * usb->divisors[i];
  267. tmp_parent_rate = clk_hw_round_rate(parent, tmp_parent_rate);
  268. tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
  269. if (tmprate < rate)
  270. tmpdiff = rate - tmprate;
  271. else
  272. tmpdiff = tmprate - rate;
  273. if (bestdiff < 0 || bestdiff > tmpdiff) {
  274. bestrate = tmprate;
  275. bestdiff = tmpdiff;
  276. *parent_rate = tmp_parent_rate;
  277. }
  278. if (!bestdiff)
  279. break;
  280. }
  281. return bestrate;
  282. }
  283. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  284. unsigned long parent_rate)
  285. {
  286. int i;
  287. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  288. unsigned long div;
  289. if (!rate)
  290. return -EINVAL;
  291. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  292. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  293. if (usb->divisors[i] == div) {
  294. regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  295. AT91_PMC_USBDIV,
  296. i << RM9200_USB_DIV_SHIFT);
  297. return 0;
  298. }
  299. }
  300. return -EINVAL;
  301. }
  302. static const struct clk_ops at91rm9200_usb_ops = {
  303. .recalc_rate = at91rm9200_clk_usb_recalc_rate,
  304. .round_rate = at91rm9200_clk_usb_round_rate,
  305. .set_rate = at91rm9200_clk_usb_set_rate,
  306. };
  307. struct clk_hw * __init
  308. at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  309. const char *parent_name, const u32 *divisors)
  310. {
  311. struct at91rm9200_clk_usb *usb;
  312. struct clk_hw *hw;
  313. struct clk_init_data init;
  314. int ret;
  315. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  316. if (!usb)
  317. return ERR_PTR(-ENOMEM);
  318. init.name = name;
  319. init.ops = &at91rm9200_usb_ops;
  320. init.parent_names = &parent_name;
  321. init.num_parents = 1;
  322. init.flags = CLK_SET_RATE_PARENT;
  323. usb->hw.init = &init;
  324. usb->regmap = regmap;
  325. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  326. hw = &usb->hw;
  327. ret = clk_hw_register(NULL, &usb->hw);
  328. if (ret) {
  329. kfree(usb);
  330. hw = ERR_PTR(ret);
  331. }
  332. return hw;
  333. }