clk-pll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2014 MundoReader S.L.
  3. * Author: Heiko Stuebner <heiko@sntech.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <asm/div64.h>
  16. #include <linux/slab.h>
  17. #include <linux/io.h>
  18. #include <linux/delay.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/regmap.h>
  21. #include "clk.h"
  22. #define PLL_MODE_MASK 0x3
  23. #define PLL_MODE_SLOW 0x0
  24. #define PLL_MODE_NORM 0x1
  25. #define PLL_MODE_DEEP 0x2
  26. struct rockchip_clk_pll {
  27. struct clk_hw hw;
  28. struct clk_mux pll_mux;
  29. const struct clk_ops *pll_mux_ops;
  30. struct notifier_block clk_nb;
  31. void __iomem *reg_base;
  32. int lock_offset;
  33. unsigned int lock_shift;
  34. enum rockchip_pll_type type;
  35. u8 flags;
  36. const struct rockchip_pll_rate_table *rate_table;
  37. unsigned int rate_count;
  38. spinlock_t *lock;
  39. };
  40. #define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw)
  41. #define to_rockchip_clk_pll_nb(nb) \
  42. container_of(nb, struct rockchip_clk_pll, clk_nb)
  43. static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
  44. struct rockchip_clk_pll *pll, unsigned long rate)
  45. {
  46. const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
  47. int i;
  48. for (i = 0; i < pll->rate_count; i++) {
  49. if (rate == rate_table[i].rate)
  50. return &rate_table[i];
  51. }
  52. return NULL;
  53. }
  54. static long rockchip_pll_round_rate(struct clk_hw *hw,
  55. unsigned long drate, unsigned long *prate)
  56. {
  57. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  58. const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
  59. int i;
  60. /* Assumming rate_table is in descending order */
  61. for (i = 0; i < pll->rate_count; i++) {
  62. if (drate >= rate_table[i].rate)
  63. return rate_table[i].rate;
  64. }
  65. /* return minimum supported value */
  66. return rate_table[i - 1].rate;
  67. }
  68. /*
  69. * Wait for the pll to reach the locked state.
  70. * The calling set_rate function is responsible for making sure the
  71. * grf regmap is available.
  72. */
  73. static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
  74. {
  75. struct regmap *grf = rockchip_clk_get_grf();
  76. unsigned int val;
  77. int delay = 24000000, ret;
  78. while (delay > 0) {
  79. ret = regmap_read(grf, pll->lock_offset, &val);
  80. if (ret) {
  81. pr_err("%s: failed to read pll lock status: %d\n",
  82. __func__, ret);
  83. return ret;
  84. }
  85. if (val & BIT(pll->lock_shift))
  86. return 0;
  87. delay--;
  88. }
  89. pr_err("%s: timeout waiting for pll to lock\n", __func__);
  90. return -ETIMEDOUT;
  91. }
  92. /**
  93. * PLL used in RK3066, RK3188 and RK3288
  94. */
  95. #define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
  96. #define RK3066_PLLCON(i) (i * 0x4)
  97. #define RK3066_PLLCON0_OD_MASK 0xf
  98. #define RK3066_PLLCON0_OD_SHIFT 0
  99. #define RK3066_PLLCON0_NR_MASK 0x3f
  100. #define RK3066_PLLCON0_NR_SHIFT 8
  101. #define RK3066_PLLCON1_NF_MASK 0x1fff
  102. #define RK3066_PLLCON1_NF_SHIFT 0
  103. #define RK3066_PLLCON2_NB_MASK 0xfff
  104. #define RK3066_PLLCON2_NB_SHIFT 0
  105. #define RK3066_PLLCON3_RESET (1 << 5)
  106. #define RK3066_PLLCON3_PWRDOWN (1 << 1)
  107. #define RK3066_PLLCON3_BYPASS (1 << 0)
  108. static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
  109. struct rockchip_pll_rate_table *rate)
  110. {
  111. u32 pllcon;
  112. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
  113. rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
  114. & RK3066_PLLCON0_NR_MASK) + 1;
  115. rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
  116. & RK3066_PLLCON0_OD_MASK) + 1;
  117. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
  118. rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
  119. & RK3066_PLLCON1_NF_MASK) + 1;
  120. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
  121. rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
  122. & RK3066_PLLCON2_NB_MASK) + 1;
  123. }
  124. static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
  125. unsigned long prate)
  126. {
  127. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  128. struct rockchip_pll_rate_table cur;
  129. u64 rate64 = prate;
  130. u32 pllcon;
  131. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
  132. if (pllcon & RK3066_PLLCON3_BYPASS) {
  133. pr_debug("%s: pll %s is bypassed\n", __func__,
  134. clk_hw_get_name(hw));
  135. return prate;
  136. }
  137. rockchip_rk3066_pll_get_params(pll, &cur);
  138. rate64 *= cur.nf;
  139. do_div(rate64, cur.nr);
  140. do_div(rate64, cur.no);
  141. return (unsigned long)rate64;
  142. }
  143. static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
  144. const struct rockchip_pll_rate_table *rate)
  145. {
  146. const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
  147. struct clk_mux *pll_mux = &pll->pll_mux;
  148. struct rockchip_pll_rate_table cur;
  149. int rate_change_remuxed = 0;
  150. int cur_parent;
  151. int ret;
  152. pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
  153. __func__, rate->rate, rate->nr, rate->no, rate->nf);
  154. rockchip_rk3066_pll_get_params(pll, &cur);
  155. cur.rate = 0;
  156. cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
  157. if (cur_parent == PLL_MODE_NORM) {
  158. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
  159. rate_change_remuxed = 1;
  160. }
  161. /* enter reset mode */
  162. writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
  163. pll->reg_base + RK3066_PLLCON(3));
  164. /* update pll values */
  165. writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
  166. RK3066_PLLCON0_NR_SHIFT) |
  167. HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
  168. RK3066_PLLCON0_OD_SHIFT),
  169. pll->reg_base + RK3066_PLLCON(0));
  170. writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
  171. RK3066_PLLCON1_NF_SHIFT),
  172. pll->reg_base + RK3066_PLLCON(1));
  173. writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
  174. RK3066_PLLCON2_NB_SHIFT),
  175. pll->reg_base + RK3066_PLLCON(2));
  176. /* leave reset and wait the reset_delay */
  177. writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
  178. pll->reg_base + RK3066_PLLCON(3));
  179. udelay(RK3066_PLL_RESET_DELAY(rate->nr));
  180. /* wait for the pll to lock */
  181. ret = rockchip_pll_wait_lock(pll);
  182. if (ret) {
  183. pr_warn("%s: pll update unsucessful, trying to restore old params\n",
  184. __func__);
  185. rockchip_rk3066_pll_set_params(pll, &cur);
  186. }
  187. if (rate_change_remuxed)
  188. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
  189. return ret;
  190. }
  191. static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  192. unsigned long prate)
  193. {
  194. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  195. const struct rockchip_pll_rate_table *rate;
  196. unsigned long old_rate = rockchip_rk3066_pll_recalc_rate(hw, prate);
  197. struct regmap *grf = rockchip_clk_get_grf();
  198. if (IS_ERR(grf)) {
  199. pr_debug("%s: grf regmap not available, aborting rate change\n",
  200. __func__);
  201. return PTR_ERR(grf);
  202. }
  203. pr_debug("%s: changing %s from %lu to %lu with a parent rate of %lu\n",
  204. __func__, clk_hw_get_name(hw), old_rate, drate, prate);
  205. /* Get required rate settings from table */
  206. rate = rockchip_get_pll_settings(pll, drate);
  207. if (!rate) {
  208. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  209. drate, clk_hw_get_name(hw));
  210. return -EINVAL;
  211. }
  212. return rockchip_rk3066_pll_set_params(pll, rate);
  213. }
  214. static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
  215. {
  216. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  217. writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
  218. pll->reg_base + RK3066_PLLCON(3));
  219. return 0;
  220. }
  221. static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
  222. {
  223. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  224. writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
  225. RK3066_PLLCON3_PWRDOWN, 0),
  226. pll->reg_base + RK3066_PLLCON(3));
  227. }
  228. static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
  229. {
  230. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  231. u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
  232. return !(pllcon & RK3066_PLLCON3_PWRDOWN);
  233. }
  234. static void rockchip_rk3066_pll_init(struct clk_hw *hw)
  235. {
  236. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  237. const struct rockchip_pll_rate_table *rate;
  238. struct rockchip_pll_rate_table cur;
  239. unsigned long drate;
  240. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  241. return;
  242. drate = clk_hw_get_rate(hw);
  243. rate = rockchip_get_pll_settings(pll, drate);
  244. /* when no rate setting for the current rate, rely on clk_set_rate */
  245. if (!rate)
  246. return;
  247. rockchip_rk3066_pll_get_params(pll, &cur);
  248. pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
  249. __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr,
  250. rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb);
  251. if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf
  252. || rate->nb != cur.nb) {
  253. struct regmap *grf = rockchip_clk_get_grf();
  254. if (IS_ERR(grf))
  255. return;
  256. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  257. __func__, clk_hw_get_name(hw));
  258. rockchip_rk3066_pll_set_params(pll, rate);
  259. }
  260. }
  261. static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
  262. .recalc_rate = rockchip_rk3066_pll_recalc_rate,
  263. .enable = rockchip_rk3066_pll_enable,
  264. .disable = rockchip_rk3066_pll_disable,
  265. .is_enabled = rockchip_rk3066_pll_is_enabled,
  266. };
  267. static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
  268. .recalc_rate = rockchip_rk3066_pll_recalc_rate,
  269. .round_rate = rockchip_pll_round_rate,
  270. .set_rate = rockchip_rk3066_pll_set_rate,
  271. .enable = rockchip_rk3066_pll_enable,
  272. .disable = rockchip_rk3066_pll_disable,
  273. .is_enabled = rockchip_rk3066_pll_is_enabled,
  274. .init = rockchip_rk3066_pll_init,
  275. };
  276. /*
  277. * Common registering of pll clocks
  278. */
  279. struct clk *rockchip_clk_register_pll(enum rockchip_pll_type pll_type,
  280. const char *name, const char *const *parent_names,
  281. u8 num_parents, void __iomem *base, int con_offset,
  282. int grf_lock_offset, int lock_shift, int mode_offset,
  283. int mode_shift, struct rockchip_pll_rate_table *rate_table,
  284. u8 clk_pll_flags, spinlock_t *lock)
  285. {
  286. const char *pll_parents[3];
  287. struct clk_init_data init;
  288. struct rockchip_clk_pll *pll;
  289. struct clk_mux *pll_mux;
  290. struct clk *pll_clk, *mux_clk;
  291. char pll_name[20];
  292. if (num_parents != 2) {
  293. pr_err("%s: needs two parent clocks\n", __func__);
  294. return ERR_PTR(-EINVAL);
  295. }
  296. /* name the actual pll */
  297. snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
  298. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  299. if (!pll)
  300. return ERR_PTR(-ENOMEM);
  301. /* create the mux on top of the real pll */
  302. pll->pll_mux_ops = &clk_mux_ops;
  303. pll_mux = &pll->pll_mux;
  304. pll_mux->reg = base + mode_offset;
  305. pll_mux->shift = mode_shift;
  306. pll_mux->mask = PLL_MODE_MASK;
  307. pll_mux->flags = 0;
  308. pll_mux->lock = lock;
  309. pll_mux->hw.init = &init;
  310. if (pll_type == pll_rk3066)
  311. pll_mux->flags |= CLK_MUX_HIWORD_MASK;
  312. /* the actual muxing is xin24m, pll-output, xin32k */
  313. pll_parents[0] = parent_names[0];
  314. pll_parents[1] = pll_name;
  315. pll_parents[2] = parent_names[1];
  316. init.name = name;
  317. init.flags = CLK_SET_RATE_PARENT;
  318. init.ops = pll->pll_mux_ops;
  319. init.parent_names = pll_parents;
  320. init.num_parents = ARRAY_SIZE(pll_parents);
  321. mux_clk = clk_register(NULL, &pll_mux->hw);
  322. if (IS_ERR(mux_clk))
  323. goto err_mux;
  324. /* now create the actual pll */
  325. init.name = pll_name;
  326. /* keep all plls untouched for now */
  327. init.flags = CLK_IGNORE_UNUSED;
  328. init.parent_names = &parent_names[0];
  329. init.num_parents = 1;
  330. if (rate_table) {
  331. int len;
  332. /* find count of rates in rate_table */
  333. for (len = 0; rate_table[len].rate != 0; )
  334. len++;
  335. pll->rate_count = len;
  336. pll->rate_table = kmemdup(rate_table,
  337. pll->rate_count *
  338. sizeof(struct rockchip_pll_rate_table),
  339. GFP_KERNEL);
  340. WARN(!pll->rate_table,
  341. "%s: could not allocate rate table for %s\n",
  342. __func__, name);
  343. }
  344. switch (pll_type) {
  345. case pll_rk3066:
  346. if (!pll->rate_table)
  347. init.ops = &rockchip_rk3066_pll_clk_norate_ops;
  348. else
  349. init.ops = &rockchip_rk3066_pll_clk_ops;
  350. break;
  351. default:
  352. pr_warn("%s: Unknown pll type for pll clk %s\n",
  353. __func__, name);
  354. }
  355. pll->hw.init = &init;
  356. pll->type = pll_type;
  357. pll->reg_base = base + con_offset;
  358. pll->lock_offset = grf_lock_offset;
  359. pll->lock_shift = lock_shift;
  360. pll->flags = clk_pll_flags;
  361. pll->lock = lock;
  362. pll_clk = clk_register(NULL, &pll->hw);
  363. if (IS_ERR(pll_clk)) {
  364. pr_err("%s: failed to register pll clock %s : %ld\n",
  365. __func__, name, PTR_ERR(pll_clk));
  366. goto err_pll;
  367. }
  368. return mux_clk;
  369. err_pll:
  370. clk_unregister(mux_clk);
  371. mux_clk = pll_clk;
  372. err_mux:
  373. kfree(pll);
  374. return mux_clk;
  375. }