clk-wm831x.c 9.6 KB

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