clk-wm831x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * WM831x clock control
  3. *
  4. * Copyright 2011-2 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mfd/wm831x/core.h>
  21. struct wm831x_clk {
  22. struct wm831x *wm831x;
  23. struct clk_hw xtal_hw;
  24. struct clk_hw fll_hw;
  25. struct clk_hw clkout_hw;
  26. struct clk *xtal;
  27. struct clk *fll;
  28. struct clk *clkout;
  29. bool xtal_ena;
  30. };
  31. static int wm831x_xtal_is_enabled(struct clk_hw *hw)
  32. {
  33. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  34. xtal_hw);
  35. return clkdata->xtal_ena;
  36. }
  37. static unsigned long wm831x_xtal_recalc_rate(struct clk_hw *hw,
  38. unsigned long parent_rate)
  39. {
  40. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  41. xtal_hw);
  42. if (clkdata->xtal_ena)
  43. return 32768;
  44. else
  45. return 0;
  46. }
  47. static const struct clk_ops wm831x_xtal_ops = {
  48. .is_enabled = wm831x_xtal_is_enabled,
  49. .recalc_rate = wm831x_xtal_recalc_rate,
  50. };
  51. static struct clk_init_data wm831x_xtal_init = {
  52. .name = "xtal",
  53. .ops = &wm831x_xtal_ops,
  54. .flags = CLK_IS_ROOT,
  55. };
  56. static const unsigned long wm831x_fll_auto_rates[] = {
  57. 2048000,
  58. 11289600,
  59. 12000000,
  60. 12288000,
  61. 19200000,
  62. 22579600,
  63. 24000000,
  64. 24576000,
  65. };
  66. static int wm831x_fll_is_enabled(struct clk_hw *hw)
  67. {
  68. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  69. fll_hw);
  70. struct wm831x *wm831x = clkdata->wm831x;
  71. int ret;
  72. ret = wm831x_reg_read(wm831x, WM831X_FLL_CONTROL_1);
  73. if (ret < 0) {
  74. dev_err(wm831x->dev, "Unable to read FLL_CONTROL_1: %d\n",
  75. ret);
  76. return true;
  77. }
  78. return (ret & WM831X_FLL_ENA) != 0;
  79. }
  80. static int wm831x_fll_prepare(struct clk_hw *hw)
  81. {
  82. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  83. fll_hw);
  84. struct wm831x *wm831x = clkdata->wm831x;
  85. int ret;
  86. ret = wm831x_set_bits(wm831x, WM831X_FLL_CONTROL_2,
  87. WM831X_FLL_ENA, WM831X_FLL_ENA);
  88. if (ret != 0)
  89. dev_crit(wm831x->dev, "Failed to enable FLL: %d\n", ret);
  90. usleep_range(2000, 2000);
  91. return ret;
  92. }
  93. static void wm831x_fll_unprepare(struct clk_hw *hw)
  94. {
  95. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  96. fll_hw);
  97. struct wm831x *wm831x = clkdata->wm831x;
  98. int ret;
  99. ret = wm831x_set_bits(wm831x, WM831X_FLL_CONTROL_2, WM831X_FLL_ENA, 0);
  100. if (ret != 0)
  101. dev_crit(wm831x->dev, "Failed to disaable FLL: %d\n", ret);
  102. }
  103. static unsigned long wm831x_fll_recalc_rate(struct clk_hw *hw,
  104. unsigned long parent_rate)
  105. {
  106. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  107. fll_hw);
  108. struct wm831x *wm831x = clkdata->wm831x;
  109. int ret;
  110. ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_2);
  111. if (ret < 0) {
  112. dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_2: %d\n",
  113. ret);
  114. return 0;
  115. }
  116. if (ret & WM831X_FLL_AUTO)
  117. return wm831x_fll_auto_rates[ret & WM831X_FLL_AUTO_FREQ_MASK];
  118. dev_err(wm831x->dev, "FLL only supported in AUTO mode\n");
  119. return 0;
  120. }
  121. static long wm831x_fll_round_rate(struct clk_hw *hw, unsigned long rate,
  122. unsigned long *unused)
  123. {
  124. int best = 0;
  125. int i;
  126. for (i = 0; i < ARRAY_SIZE(wm831x_fll_auto_rates); i++)
  127. if (abs(wm831x_fll_auto_rates[i] - rate) <
  128. abs(wm831x_fll_auto_rates[best] - rate))
  129. best = i;
  130. return wm831x_fll_auto_rates[best];
  131. }
  132. static int wm831x_fll_set_rate(struct clk_hw *hw, unsigned long rate,
  133. unsigned long parent_rate)
  134. {
  135. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  136. fll_hw);
  137. struct wm831x *wm831x = clkdata->wm831x;
  138. int i;
  139. for (i = 0; i < ARRAY_SIZE(wm831x_fll_auto_rates); i++)
  140. if (wm831x_fll_auto_rates[i] == rate)
  141. break;
  142. if (i == ARRAY_SIZE(wm831x_fll_auto_rates))
  143. return -EINVAL;
  144. if (wm831x_fll_is_enabled(hw))
  145. return -EPERM;
  146. return wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_2,
  147. WM831X_FLL_AUTO_FREQ_MASK, i);
  148. }
  149. static const char *wm831x_fll_parents[] = {
  150. "xtal",
  151. "clkin",
  152. };
  153. static u8 wm831x_fll_get_parent(struct clk_hw *hw)
  154. {
  155. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  156. fll_hw);
  157. struct wm831x *wm831x = clkdata->wm831x;
  158. int ret;
  159. /* AUTO mode is always clocked from the crystal */
  160. ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_2);
  161. if (ret < 0) {
  162. dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_2: %d\n",
  163. ret);
  164. return 0;
  165. }
  166. if (ret & WM831X_FLL_AUTO)
  167. return 0;
  168. ret = wm831x_reg_read(wm831x, WM831X_FLL_CONTROL_5);
  169. if (ret < 0) {
  170. dev_err(wm831x->dev, "Unable to read FLL_CONTROL_5: %d\n",
  171. ret);
  172. return 0;
  173. }
  174. switch (ret & WM831X_FLL_CLK_SRC_MASK) {
  175. case 0:
  176. return 0;
  177. case 1:
  178. return 1;
  179. default:
  180. dev_err(wm831x->dev, "Unsupported FLL clock source %d\n",
  181. ret & WM831X_FLL_CLK_SRC_MASK);
  182. return 0;
  183. }
  184. }
  185. static const struct clk_ops wm831x_fll_ops = {
  186. .is_enabled = wm831x_fll_is_enabled,
  187. .prepare = wm831x_fll_prepare,
  188. .unprepare = wm831x_fll_unprepare,
  189. .round_rate = wm831x_fll_round_rate,
  190. .recalc_rate = wm831x_fll_recalc_rate,
  191. .set_rate = wm831x_fll_set_rate,
  192. .get_parent = wm831x_fll_get_parent,
  193. };
  194. static struct clk_init_data wm831x_fll_init = {
  195. .name = "fll",
  196. .ops = &wm831x_fll_ops,
  197. .parent_names = wm831x_fll_parents,
  198. .num_parents = ARRAY_SIZE(wm831x_fll_parents),
  199. .flags = CLK_SET_RATE_GATE,
  200. };
  201. static int wm831x_clkout_is_enabled(struct clk_hw *hw)
  202. {
  203. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  204. clkout_hw);
  205. struct wm831x *wm831x = clkdata->wm831x;
  206. int ret;
  207. ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_1);
  208. if (ret < 0) {
  209. dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_1: %d\n",
  210. ret);
  211. return true;
  212. }
  213. return (ret & WM831X_CLKOUT_ENA) != 0;
  214. }
  215. static int wm831x_clkout_prepare(struct clk_hw *hw)
  216. {
  217. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  218. clkout_hw);
  219. struct wm831x *wm831x = clkdata->wm831x;
  220. int ret;
  221. ret = wm831x_reg_unlock(wm831x);
  222. if (ret != 0) {
  223. dev_crit(wm831x->dev, "Failed to lock registers: %d\n", ret);
  224. return ret;
  225. }
  226. ret = wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_1,
  227. WM831X_CLKOUT_ENA, WM831X_CLKOUT_ENA);
  228. if (ret != 0)
  229. dev_crit(wm831x->dev, "Failed to enable CLKOUT: %d\n", ret);
  230. wm831x_reg_lock(wm831x);
  231. return ret;
  232. }
  233. static void wm831x_clkout_unprepare(struct clk_hw *hw)
  234. {
  235. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  236. clkout_hw);
  237. struct wm831x *wm831x = clkdata->wm831x;
  238. int ret;
  239. ret = wm831x_reg_unlock(wm831x);
  240. if (ret != 0) {
  241. dev_crit(wm831x->dev, "Failed to lock registers: %d\n", ret);
  242. return;
  243. }
  244. ret = wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_1,
  245. WM831X_CLKOUT_ENA, 0);
  246. if (ret != 0)
  247. dev_crit(wm831x->dev, "Failed to disable CLKOUT: %d\n", ret);
  248. wm831x_reg_lock(wm831x);
  249. }
  250. static const char *wm831x_clkout_parents[] = {
  251. "xtal",
  252. "fll",
  253. };
  254. static u8 wm831x_clkout_get_parent(struct clk_hw *hw)
  255. {
  256. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  257. clkout_hw);
  258. struct wm831x *wm831x = clkdata->wm831x;
  259. int ret;
  260. ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_1);
  261. if (ret < 0) {
  262. dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_1: %d\n",
  263. ret);
  264. return 0;
  265. }
  266. if (ret & WM831X_CLKOUT_SRC)
  267. return 0;
  268. else
  269. return 1;
  270. }
  271. static int wm831x_clkout_set_parent(struct clk_hw *hw, u8 parent)
  272. {
  273. struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
  274. clkout_hw);
  275. struct wm831x *wm831x = clkdata->wm831x;
  276. return wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_1,
  277. WM831X_CLKOUT_SRC,
  278. parent << WM831X_CLKOUT_SRC_SHIFT);
  279. }
  280. static const struct clk_ops wm831x_clkout_ops = {
  281. .is_enabled = wm831x_clkout_is_enabled,
  282. .prepare = wm831x_clkout_prepare,
  283. .unprepare = wm831x_clkout_unprepare,
  284. .get_parent = wm831x_clkout_get_parent,
  285. .set_parent = wm831x_clkout_set_parent,
  286. };
  287. static struct clk_init_data wm831x_clkout_init = {
  288. .name = "clkout",
  289. .ops = &wm831x_clkout_ops,
  290. .parent_names = wm831x_clkout_parents,
  291. .num_parents = ARRAY_SIZE(wm831x_clkout_parents),
  292. .flags = CLK_SET_RATE_PARENT,
  293. };
  294. static int wm831x_clk_probe(struct platform_device *pdev)
  295. {
  296. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  297. struct wm831x_clk *clkdata;
  298. int ret;
  299. clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
  300. if (!clkdata)
  301. return -ENOMEM;
  302. /* XTAL_ENA can only be set via OTP/InstantConfig so just read once */
  303. ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_2);
  304. if (ret < 0) {
  305. dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_2: %d\n",
  306. ret);
  307. return ret;
  308. }
  309. clkdata->xtal_ena = ret & WM831X_XTAL_ENA;
  310. clkdata->xtal_hw.init = &wm831x_xtal_init;
  311. clkdata->xtal = devm_clk_register(&pdev->dev, &clkdata->xtal_hw);
  312. if (IS_ERR(clkdata->xtal))
  313. return PTR_ERR(clkdata->xtal);
  314. clkdata->fll_hw.init = &wm831x_fll_init;
  315. clkdata->fll = devm_clk_register(&pdev->dev, &clkdata->fll_hw);
  316. if (IS_ERR(clkdata->fll))
  317. return PTR_ERR(clkdata->fll);
  318. clkdata->clkout_hw.init = &wm831x_clkout_init;
  319. clkdata->clkout = devm_clk_register(&pdev->dev, &clkdata->clkout_hw);
  320. if (IS_ERR(clkdata->clkout))
  321. return PTR_ERR(clkdata->clkout);
  322. dev_set_drvdata(&pdev->dev, clkdata);
  323. return 0;
  324. }
  325. static int wm831x_clk_remove(struct platform_device *pdev)
  326. {
  327. return 0;
  328. }
  329. static struct platform_driver wm831x_clk_driver = {
  330. .probe = wm831x_clk_probe,
  331. .remove = wm831x_clk_remove,
  332. .driver = {
  333. .name = "wm831x-clk",
  334. .owner = THIS_MODULE,
  335. },
  336. };
  337. module_platform_driver(wm831x_clk_driver);
  338. /* Module information */
  339. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  340. MODULE_DESCRIPTION("WM831x clock driver");
  341. MODULE_LICENSE("GPL");
  342. MODULE_ALIAS("platform:wm831x-clk");