clk-pll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  3. * Copyright (c) 2013 Linaro Ltd.
  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 version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This file contains the utility functions to register the pll clocks.
  10. */
  11. #include <linux/errno.h>
  12. #include "clk.h"
  13. #include "clk-pll.h"
  14. /*
  15. * PLL35xx Clock Type
  16. */
  17. #define PLL35XX_MDIV_MASK (0x3FF)
  18. #define PLL35XX_PDIV_MASK (0x3F)
  19. #define PLL35XX_SDIV_MASK (0x7)
  20. #define PLL35XX_MDIV_SHIFT (16)
  21. #define PLL35XX_PDIV_SHIFT (8)
  22. #define PLL35XX_SDIV_SHIFT (0)
  23. struct samsung_clk_pll35xx {
  24. struct clk_hw hw;
  25. const void __iomem *con_reg;
  26. };
  27. #define to_clk_pll35xx(_hw) container_of(_hw, struct samsung_clk_pll35xx, hw)
  28. static unsigned long samsung_pll35xx_recalc_rate(struct clk_hw *hw,
  29. unsigned long parent_rate)
  30. {
  31. struct samsung_clk_pll35xx *pll = to_clk_pll35xx(hw);
  32. u32 mdiv, pdiv, sdiv, pll_con;
  33. u64 fvco = parent_rate;
  34. pll_con = __raw_readl(pll->con_reg);
  35. mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK;
  36. pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK;
  37. sdiv = (pll_con >> PLL35XX_SDIV_SHIFT) & PLL35XX_SDIV_MASK;
  38. fvco *= mdiv;
  39. do_div(fvco, (pdiv << sdiv));
  40. return (unsigned long)fvco;
  41. }
  42. /* todo: implement pl35xx clock round rate operation */
  43. static long samsung_pll35xx_round_rate(struct clk_hw *hw,
  44. unsigned long drate, unsigned long *prate)
  45. {
  46. return -ENOTSUPP;
  47. }
  48. /* todo: implement pl35xx clock set rate */
  49. static int samsung_pll35xx_set_rate(struct clk_hw *hw, unsigned long drate,
  50. unsigned long prate)
  51. {
  52. return -ENOTSUPP;
  53. }
  54. static const struct clk_ops samsung_pll35xx_clk_ops = {
  55. .recalc_rate = samsung_pll35xx_recalc_rate,
  56. .round_rate = samsung_pll35xx_round_rate,
  57. .set_rate = samsung_pll35xx_set_rate,
  58. };
  59. struct clk * __init samsung_clk_register_pll35xx(const char *name,
  60. const char *pname, const void __iomem *con_reg)
  61. {
  62. struct samsung_clk_pll35xx *pll;
  63. struct clk *clk;
  64. struct clk_init_data init;
  65. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  66. if (!pll) {
  67. pr_err("%s: could not allocate pll clk %s\n", __func__, name);
  68. return NULL;
  69. }
  70. init.name = name;
  71. init.ops = &samsung_pll35xx_clk_ops;
  72. init.flags = CLK_GET_RATE_NOCACHE;
  73. init.parent_names = &pname;
  74. init.num_parents = 1;
  75. pll->hw.init = &init;
  76. pll->con_reg = con_reg;
  77. clk = clk_register(NULL, &pll->hw);
  78. if (IS_ERR(clk)) {
  79. pr_err("%s: failed to register pll clock %s\n", __func__,
  80. name);
  81. kfree(pll);
  82. }
  83. if (clk_register_clkdev(clk, name, NULL))
  84. pr_err("%s: failed to register lookup for %s", __func__, name);
  85. return clk;
  86. }
  87. /*
  88. * PLL36xx Clock Type
  89. */
  90. #define PLL36XX_KDIV_MASK (0xFFFF)
  91. #define PLL36XX_MDIV_MASK (0x1FF)
  92. #define PLL36XX_PDIV_MASK (0x3F)
  93. #define PLL36XX_SDIV_MASK (0x7)
  94. #define PLL36XX_MDIV_SHIFT (16)
  95. #define PLL36XX_PDIV_SHIFT (8)
  96. #define PLL36XX_SDIV_SHIFT (0)
  97. struct samsung_clk_pll36xx {
  98. struct clk_hw hw;
  99. const void __iomem *con_reg;
  100. };
  101. #define to_clk_pll36xx(_hw) container_of(_hw, struct samsung_clk_pll36xx, hw)
  102. static unsigned long samsung_pll36xx_recalc_rate(struct clk_hw *hw,
  103. unsigned long parent_rate)
  104. {
  105. struct samsung_clk_pll36xx *pll = to_clk_pll36xx(hw);
  106. u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1;
  107. u64 fvco = parent_rate;
  108. pll_con0 = __raw_readl(pll->con_reg);
  109. pll_con1 = __raw_readl(pll->con_reg + 4);
  110. mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK;
  111. pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK;
  112. sdiv = (pll_con0 >> PLL36XX_SDIV_SHIFT) & PLL36XX_SDIV_MASK;
  113. kdiv = pll_con1 & PLL36XX_KDIV_MASK;
  114. fvco *= (mdiv << 16) + kdiv;
  115. do_div(fvco, (pdiv << sdiv));
  116. fvco >>= 16;
  117. return (unsigned long)fvco;
  118. }
  119. /* todo: implement pl36xx clock round rate operation */
  120. static long samsung_pll36xx_round_rate(struct clk_hw *hw,
  121. unsigned long drate, unsigned long *prate)
  122. {
  123. return -ENOTSUPP;
  124. }
  125. /* todo: implement pl36xx clock set rate */
  126. static int samsung_pll36xx_set_rate(struct clk_hw *hw, unsigned long drate,
  127. unsigned long prate)
  128. {
  129. return -ENOTSUPP;
  130. }
  131. static const struct clk_ops samsung_pll36xx_clk_ops = {
  132. .recalc_rate = samsung_pll36xx_recalc_rate,
  133. .round_rate = samsung_pll36xx_round_rate,
  134. .set_rate = samsung_pll36xx_set_rate,
  135. };
  136. struct clk * __init samsung_clk_register_pll36xx(const char *name,
  137. const char *pname, const void __iomem *con_reg)
  138. {
  139. struct samsung_clk_pll36xx *pll;
  140. struct clk *clk;
  141. struct clk_init_data init;
  142. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  143. if (!pll) {
  144. pr_err("%s: could not allocate pll clk %s\n", __func__, name);
  145. return NULL;
  146. }
  147. init.name = name;
  148. init.ops = &samsung_pll36xx_clk_ops;
  149. init.flags = CLK_GET_RATE_NOCACHE;
  150. init.parent_names = &pname;
  151. init.num_parents = 1;
  152. pll->hw.init = &init;
  153. pll->con_reg = con_reg;
  154. clk = clk_register(NULL, &pll->hw);
  155. if (IS_ERR(clk)) {
  156. pr_err("%s: failed to register pll clock %s\n", __func__,
  157. name);
  158. kfree(pll);
  159. }
  160. if (clk_register_clkdev(clk, name, NULL))
  161. pr_err("%s: failed to register lookup for %s", __func__, name);
  162. return clk;
  163. }
  164. /*
  165. * PLL45xx Clock Type
  166. */
  167. #define PLL45XX_MDIV_MASK (0x3FF)
  168. #define PLL45XX_PDIV_MASK (0x3F)
  169. #define PLL45XX_SDIV_MASK (0x7)
  170. #define PLL45XX_MDIV_SHIFT (16)
  171. #define PLL45XX_PDIV_SHIFT (8)
  172. #define PLL45XX_SDIV_SHIFT (0)
  173. struct samsung_clk_pll45xx {
  174. struct clk_hw hw;
  175. enum pll45xx_type type;
  176. const void __iomem *con_reg;
  177. };
  178. #define to_clk_pll45xx(_hw) container_of(_hw, struct samsung_clk_pll45xx, hw)
  179. static unsigned long samsung_pll45xx_recalc_rate(struct clk_hw *hw,
  180. unsigned long parent_rate)
  181. {
  182. struct samsung_clk_pll45xx *pll = to_clk_pll45xx(hw);
  183. u32 mdiv, pdiv, sdiv, pll_con;
  184. u64 fvco = parent_rate;
  185. pll_con = __raw_readl(pll->con_reg);
  186. mdiv = (pll_con >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK;
  187. pdiv = (pll_con >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK;
  188. sdiv = (pll_con >> PLL45XX_SDIV_SHIFT) & PLL45XX_SDIV_MASK;
  189. if (pll->type == pll_4508)
  190. sdiv = sdiv - 1;
  191. fvco *= mdiv;
  192. do_div(fvco, (pdiv << sdiv));
  193. return (unsigned long)fvco;
  194. }
  195. /* todo: implement pl45xx clock round rate operation */
  196. static long samsung_pll45xx_round_rate(struct clk_hw *hw,
  197. unsigned long drate, unsigned long *prate)
  198. {
  199. return -ENOTSUPP;
  200. }
  201. /* todo: implement pl45xx clock set rate */
  202. static int samsung_pll45xx_set_rate(struct clk_hw *hw, unsigned long drate,
  203. unsigned long prate)
  204. {
  205. return -ENOTSUPP;
  206. }
  207. static const struct clk_ops samsung_pll45xx_clk_ops = {
  208. .recalc_rate = samsung_pll45xx_recalc_rate,
  209. .round_rate = samsung_pll45xx_round_rate,
  210. .set_rate = samsung_pll45xx_set_rate,
  211. };
  212. struct clk * __init samsung_clk_register_pll45xx(const char *name,
  213. const char *pname, const void __iomem *con_reg,
  214. enum pll45xx_type type)
  215. {
  216. struct samsung_clk_pll45xx *pll;
  217. struct clk *clk;
  218. struct clk_init_data init;
  219. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  220. if (!pll) {
  221. pr_err("%s: could not allocate pll clk %s\n", __func__, name);
  222. return NULL;
  223. }
  224. init.name = name;
  225. init.ops = &samsung_pll45xx_clk_ops;
  226. init.flags = CLK_GET_RATE_NOCACHE;
  227. init.parent_names = &pname;
  228. init.num_parents = 1;
  229. pll->hw.init = &init;
  230. pll->con_reg = con_reg;
  231. pll->type = type;
  232. clk = clk_register(NULL, &pll->hw);
  233. if (IS_ERR(clk)) {
  234. pr_err("%s: failed to register pll clock %s\n", __func__,
  235. name);
  236. kfree(pll);
  237. }
  238. if (clk_register_clkdev(clk, name, NULL))
  239. pr_err("%s: failed to register lookup for %s", __func__, name);
  240. return clk;
  241. }
  242. /*
  243. * PLL46xx Clock Type
  244. */
  245. #define PLL46XX_MDIV_MASK (0x1FF)
  246. #define PLL46XX_PDIV_MASK (0x3F)
  247. #define PLL46XX_SDIV_MASK (0x7)
  248. #define PLL46XX_MDIV_SHIFT (16)
  249. #define PLL46XX_PDIV_SHIFT (8)
  250. #define PLL46XX_SDIV_SHIFT (0)
  251. #define PLL46XX_KDIV_MASK (0xFFFF)
  252. #define PLL4650C_KDIV_MASK (0xFFF)
  253. #define PLL46XX_KDIV_SHIFT (0)
  254. struct samsung_clk_pll46xx {
  255. struct clk_hw hw;
  256. enum pll46xx_type type;
  257. const void __iomem *con_reg;
  258. };
  259. #define to_clk_pll46xx(_hw) container_of(_hw, struct samsung_clk_pll46xx, hw)
  260. static unsigned long samsung_pll46xx_recalc_rate(struct clk_hw *hw,
  261. unsigned long parent_rate)
  262. {
  263. struct samsung_clk_pll46xx *pll = to_clk_pll46xx(hw);
  264. u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1, shift;
  265. u64 fvco = parent_rate;
  266. pll_con0 = __raw_readl(pll->con_reg);
  267. pll_con1 = __raw_readl(pll->con_reg + 4);
  268. mdiv = (pll_con0 >> PLL46XX_MDIV_SHIFT) & PLL46XX_MDIV_MASK;
  269. pdiv = (pll_con0 >> PLL46XX_PDIV_SHIFT) & PLL46XX_PDIV_MASK;
  270. sdiv = (pll_con0 >> PLL46XX_SDIV_SHIFT) & PLL46XX_SDIV_MASK;
  271. kdiv = pll->type == pll_4650c ? pll_con1 & PLL4650C_KDIV_MASK :
  272. pll_con1 & PLL46XX_KDIV_MASK;
  273. shift = pll->type == pll_4600 ? 16 : 10;
  274. fvco *= (mdiv << shift) + kdiv;
  275. do_div(fvco, (pdiv << sdiv));
  276. fvco >>= shift;
  277. return (unsigned long)fvco;
  278. }
  279. /* todo: implement pl46xx clock round rate operation */
  280. static long samsung_pll46xx_round_rate(struct clk_hw *hw,
  281. unsigned long drate, unsigned long *prate)
  282. {
  283. return -ENOTSUPP;
  284. }
  285. /* todo: implement pl46xx clock set rate */
  286. static int samsung_pll46xx_set_rate(struct clk_hw *hw, unsigned long drate,
  287. unsigned long prate)
  288. {
  289. return -ENOTSUPP;
  290. }
  291. static const struct clk_ops samsung_pll46xx_clk_ops = {
  292. .recalc_rate = samsung_pll46xx_recalc_rate,
  293. .round_rate = samsung_pll46xx_round_rate,
  294. .set_rate = samsung_pll46xx_set_rate,
  295. };
  296. struct clk * __init samsung_clk_register_pll46xx(const char *name,
  297. const char *pname, const void __iomem *con_reg,
  298. enum pll46xx_type type)
  299. {
  300. struct samsung_clk_pll46xx *pll;
  301. struct clk *clk;
  302. struct clk_init_data init;
  303. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  304. if (!pll) {
  305. pr_err("%s: could not allocate pll clk %s\n", __func__, name);
  306. return NULL;
  307. }
  308. init.name = name;
  309. init.ops = &samsung_pll46xx_clk_ops;
  310. init.flags = CLK_GET_RATE_NOCACHE;
  311. init.parent_names = &pname;
  312. init.num_parents = 1;
  313. pll->hw.init = &init;
  314. pll->con_reg = con_reg;
  315. pll->type = type;
  316. clk = clk_register(NULL, &pll->hw);
  317. if (IS_ERR(clk)) {
  318. pr_err("%s: failed to register pll clock %s\n", __func__,
  319. name);
  320. kfree(pll);
  321. }
  322. if (clk_register_clkdev(clk, name, NULL))
  323. pr_err("%s: failed to register lookup for %s", __func__, name);
  324. return clk;
  325. }
  326. /*
  327. * PLL2550x Clock Type
  328. */
  329. #define PLL2550X_R_MASK (0x1)
  330. #define PLL2550X_P_MASK (0x3F)
  331. #define PLL2550X_M_MASK (0x3FF)
  332. #define PLL2550X_S_MASK (0x7)
  333. #define PLL2550X_R_SHIFT (20)
  334. #define PLL2550X_P_SHIFT (14)
  335. #define PLL2550X_M_SHIFT (4)
  336. #define PLL2550X_S_SHIFT (0)
  337. struct samsung_clk_pll2550x {
  338. struct clk_hw hw;
  339. const void __iomem *reg_base;
  340. unsigned long offset;
  341. };
  342. #define to_clk_pll2550x(_hw) container_of(_hw, struct samsung_clk_pll2550x, hw)
  343. static unsigned long samsung_pll2550x_recalc_rate(struct clk_hw *hw,
  344. unsigned long parent_rate)
  345. {
  346. struct samsung_clk_pll2550x *pll = to_clk_pll2550x(hw);
  347. u32 r, p, m, s, pll_stat;
  348. u64 fvco = parent_rate;
  349. pll_stat = __raw_readl(pll->reg_base + pll->offset * 3);
  350. r = (pll_stat >> PLL2550X_R_SHIFT) & PLL2550X_R_MASK;
  351. if (!r)
  352. return 0;
  353. p = (pll_stat >> PLL2550X_P_SHIFT) & PLL2550X_P_MASK;
  354. m = (pll_stat >> PLL2550X_M_SHIFT) & PLL2550X_M_MASK;
  355. s = (pll_stat >> PLL2550X_S_SHIFT) & PLL2550X_S_MASK;
  356. fvco *= m;
  357. do_div(fvco, (p << s));
  358. return (unsigned long)fvco;
  359. }
  360. /* todo: implement pl2550x clock round rate operation */
  361. static long samsung_pll2550x_round_rate(struct clk_hw *hw,
  362. unsigned long drate, unsigned long *prate)
  363. {
  364. return -ENOTSUPP;
  365. }
  366. /* todo: implement pl2550x clock set rate */
  367. static int samsung_pll2550x_set_rate(struct clk_hw *hw, unsigned long drate,
  368. unsigned long prate)
  369. {
  370. return -ENOTSUPP;
  371. }
  372. static const struct clk_ops samsung_pll2550x_clk_ops = {
  373. .recalc_rate = samsung_pll2550x_recalc_rate,
  374. .round_rate = samsung_pll2550x_round_rate,
  375. .set_rate = samsung_pll2550x_set_rate,
  376. };
  377. struct clk * __init samsung_clk_register_pll2550x(const char *name,
  378. const char *pname, const void __iomem *reg_base,
  379. const unsigned long offset)
  380. {
  381. struct samsung_clk_pll2550x *pll;
  382. struct clk *clk;
  383. struct clk_init_data init;
  384. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  385. if (!pll) {
  386. pr_err("%s: could not allocate pll clk %s\n", __func__, name);
  387. return NULL;
  388. }
  389. init.name = name;
  390. init.ops = &samsung_pll2550x_clk_ops;
  391. init.flags = CLK_GET_RATE_NOCACHE;
  392. init.parent_names = &pname;
  393. init.num_parents = 1;
  394. pll->hw.init = &init;
  395. pll->reg_base = reg_base;
  396. pll->offset = offset;
  397. clk = clk_register(NULL, &pll->hw);
  398. if (IS_ERR(clk)) {
  399. pr_err("%s: failed to register pll clock %s\n", __func__,
  400. name);
  401. kfree(pll);
  402. }
  403. if (clk_register_clkdev(clk, name, NULL))
  404. pr_err("%s: failed to register lookup for %s", __func__, name);
  405. return clk;
  406. }