clk-mtk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author: James Liao <[email protected]>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include "clk-mtk.h"
  17. #include "clk-gate.h"
  18. static void mtk_init_clk_data(struct clk_hw_onecell_data *clk_data,
  19. unsigned int clk_num)
  20. {
  21. int i;
  22. clk_data->num = clk_num;
  23. for (i = 0; i < clk_num; i++)
  24. clk_data->hws[i] = ERR_PTR(-ENOENT);
  25. }
  26. struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
  27. unsigned int clk_num)
  28. {
  29. struct clk_hw_onecell_data *clk_data;
  30. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, clk_num),
  31. GFP_KERNEL);
  32. if (!clk_data)
  33. return NULL;
  34. mtk_init_clk_data(clk_data, clk_num);
  35. return clk_data;
  36. }
  37. EXPORT_SYMBOL_GPL(mtk_devm_alloc_clk_data);
  38. struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
  39. {
  40. struct clk_hw_onecell_data *clk_data;
  41. clk_data = kzalloc(struct_size(clk_data, hws, clk_num), GFP_KERNEL);
  42. if (!clk_data)
  43. return NULL;
  44. mtk_init_clk_data(clk_data, clk_num);
  45. return clk_data;
  46. }
  47. EXPORT_SYMBOL_GPL(mtk_alloc_clk_data);
  48. void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data)
  49. {
  50. kfree(clk_data);
  51. }
  52. EXPORT_SYMBOL_GPL(mtk_free_clk_data);
  53. int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
  54. struct clk_hw_onecell_data *clk_data)
  55. {
  56. int i;
  57. struct clk_hw *hw;
  58. if (!clk_data)
  59. return -ENOMEM;
  60. for (i = 0; i < num; i++) {
  61. const struct mtk_fixed_clk *rc = &clks[i];
  62. if (!IS_ERR_OR_NULL(clk_data->hws[rc->id])) {
  63. pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
  64. continue;
  65. }
  66. hw = clk_hw_register_fixed_rate(NULL, rc->name, rc->parent, 0,
  67. rc->rate);
  68. if (IS_ERR(hw)) {
  69. pr_err("Failed to register clk %s: %pe\n", rc->name,
  70. hw);
  71. goto err;
  72. }
  73. clk_data->hws[rc->id] = hw;
  74. }
  75. return 0;
  76. err:
  77. while (--i >= 0) {
  78. const struct mtk_fixed_clk *rc = &clks[i];
  79. if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
  80. continue;
  81. clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
  82. clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
  83. }
  84. return PTR_ERR(hw);
  85. }
  86. EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
  87. void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
  88. struct clk_hw_onecell_data *clk_data)
  89. {
  90. int i;
  91. if (!clk_data)
  92. return;
  93. for (i = num; i > 0; i--) {
  94. const struct mtk_fixed_clk *rc = &clks[i - 1];
  95. if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
  96. continue;
  97. clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
  98. clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
  99. }
  100. }
  101. EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
  102. int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
  103. struct clk_hw_onecell_data *clk_data)
  104. {
  105. int i;
  106. struct clk_hw *hw;
  107. if (!clk_data)
  108. return -ENOMEM;
  109. for (i = 0; i < num; i++) {
  110. const struct mtk_fixed_factor *ff = &clks[i];
  111. if (!IS_ERR_OR_NULL(clk_data->hws[ff->id])) {
  112. pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
  113. continue;
  114. }
  115. hw = clk_hw_register_fixed_factor(NULL, ff->name, ff->parent_name,
  116. CLK_SET_RATE_PARENT, ff->mult, ff->div);
  117. if (IS_ERR(hw)) {
  118. pr_err("Failed to register clk %s: %pe\n", ff->name,
  119. hw);
  120. goto err;
  121. }
  122. clk_data->hws[ff->id] = hw;
  123. }
  124. return 0;
  125. err:
  126. while (--i >= 0) {
  127. const struct mtk_fixed_factor *ff = &clks[i];
  128. if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
  129. continue;
  130. clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
  131. clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
  132. }
  133. return PTR_ERR(hw);
  134. }
  135. EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
  136. void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
  137. struct clk_hw_onecell_data *clk_data)
  138. {
  139. int i;
  140. if (!clk_data)
  141. return;
  142. for (i = num; i > 0; i--) {
  143. const struct mtk_fixed_factor *ff = &clks[i - 1];
  144. if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
  145. continue;
  146. clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
  147. clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
  148. }
  149. }
  150. EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
  151. static struct clk_hw *mtk_clk_register_composite(const struct mtk_composite *mc,
  152. void __iomem *base, spinlock_t *lock)
  153. {
  154. struct clk_hw *hw;
  155. struct clk_mux *mux = NULL;
  156. struct clk_gate *gate = NULL;
  157. struct clk_divider *div = NULL;
  158. struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
  159. const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
  160. const char * const *parent_names;
  161. const char *parent;
  162. int num_parents;
  163. int ret;
  164. if (mc->mux_shift >= 0) {
  165. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  166. if (!mux)
  167. return ERR_PTR(-ENOMEM);
  168. mux->reg = base + mc->mux_reg;
  169. mux->mask = BIT(mc->mux_width) - 1;
  170. mux->shift = mc->mux_shift;
  171. mux->lock = lock;
  172. mux->flags = mc->mux_flags;
  173. mux_hw = &mux->hw;
  174. mux_ops = &clk_mux_ops;
  175. parent_names = mc->parent_names;
  176. num_parents = mc->num_parents;
  177. } else {
  178. parent = mc->parent;
  179. parent_names = &parent;
  180. num_parents = 1;
  181. }
  182. if (mc->gate_shift >= 0) {
  183. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  184. if (!gate) {
  185. ret = -ENOMEM;
  186. goto err_out;
  187. }
  188. gate->reg = base + mc->gate_reg;
  189. gate->bit_idx = mc->gate_shift;
  190. gate->flags = CLK_GATE_SET_TO_DISABLE;
  191. gate->lock = lock;
  192. gate_hw = &gate->hw;
  193. gate_ops = &clk_gate_ops;
  194. }
  195. if (mc->divider_shift >= 0) {
  196. div = kzalloc(sizeof(*div), GFP_KERNEL);
  197. if (!div) {
  198. ret = -ENOMEM;
  199. goto err_out;
  200. }
  201. div->reg = base + mc->divider_reg;
  202. div->shift = mc->divider_shift;
  203. div->width = mc->divider_width;
  204. div->lock = lock;
  205. div_hw = &div->hw;
  206. div_ops = &clk_divider_ops;
  207. }
  208. hw = clk_hw_register_composite(NULL, mc->name, parent_names, num_parents,
  209. mux_hw, mux_ops,
  210. div_hw, div_ops,
  211. gate_hw, gate_ops,
  212. mc->flags);
  213. if (IS_ERR(hw)) {
  214. ret = PTR_ERR(hw);
  215. goto err_out;
  216. }
  217. return hw;
  218. err_out:
  219. kfree(div);
  220. kfree(gate);
  221. kfree(mux);
  222. return ERR_PTR(ret);
  223. }
  224. static void mtk_clk_unregister_composite(struct clk_hw *hw)
  225. {
  226. struct clk_composite *composite;
  227. struct clk_mux *mux = NULL;
  228. struct clk_gate *gate = NULL;
  229. struct clk_divider *div = NULL;
  230. if (!hw)
  231. return;
  232. composite = to_clk_composite(hw);
  233. if (composite->mux_hw)
  234. mux = to_clk_mux(composite->mux_hw);
  235. if (composite->gate_hw)
  236. gate = to_clk_gate(composite->gate_hw);
  237. if (composite->rate_hw)
  238. div = to_clk_divider(composite->rate_hw);
  239. clk_hw_unregister_composite(hw);
  240. kfree(div);
  241. kfree(gate);
  242. kfree(mux);
  243. }
  244. int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
  245. void __iomem *base, spinlock_t *lock,
  246. struct clk_hw_onecell_data *clk_data)
  247. {
  248. struct clk_hw *hw;
  249. int i;
  250. if (!clk_data)
  251. return -ENOMEM;
  252. for (i = 0; i < num; i++) {
  253. const struct mtk_composite *mc = &mcs[i];
  254. if (!IS_ERR_OR_NULL(clk_data->hws[mc->id])) {
  255. pr_warn("Trying to register duplicate clock ID: %d\n",
  256. mc->id);
  257. continue;
  258. }
  259. hw = mtk_clk_register_composite(mc, base, lock);
  260. if (IS_ERR(hw)) {
  261. pr_err("Failed to register clk %s: %pe\n", mc->name,
  262. hw);
  263. goto err;
  264. }
  265. clk_data->hws[mc->id] = hw;
  266. }
  267. return 0;
  268. err:
  269. while (--i >= 0) {
  270. const struct mtk_composite *mc = &mcs[i];
  271. if (IS_ERR_OR_NULL(clk_data->hws[mcs->id]))
  272. continue;
  273. mtk_clk_unregister_composite(clk_data->hws[mc->id]);
  274. clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
  275. }
  276. return PTR_ERR(hw);
  277. }
  278. EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
  279. void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
  280. struct clk_hw_onecell_data *clk_data)
  281. {
  282. int i;
  283. if (!clk_data)
  284. return;
  285. for (i = num; i > 0; i--) {
  286. const struct mtk_composite *mc = &mcs[i - 1];
  287. if (IS_ERR_OR_NULL(clk_data->hws[mc->id]))
  288. continue;
  289. mtk_clk_unregister_composite(clk_data->hws[mc->id]);
  290. clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
  291. }
  292. }
  293. EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
  294. int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
  295. void __iomem *base, spinlock_t *lock,
  296. struct clk_hw_onecell_data *clk_data)
  297. {
  298. struct clk_hw *hw;
  299. int i;
  300. if (!clk_data)
  301. return -ENOMEM;
  302. for (i = 0; i < num; i++) {
  303. const struct mtk_clk_divider *mcd = &mcds[i];
  304. if (!IS_ERR_OR_NULL(clk_data->hws[mcd->id])) {
  305. pr_warn("Trying to register duplicate clock ID: %d\n",
  306. mcd->id);
  307. continue;
  308. }
  309. hw = clk_hw_register_divider(NULL, mcd->name, mcd->parent_name,
  310. mcd->flags, base + mcd->div_reg, mcd->div_shift,
  311. mcd->div_width, mcd->clk_divider_flags, lock);
  312. if (IS_ERR(hw)) {
  313. pr_err("Failed to register clk %s: %pe\n", mcd->name,
  314. hw);
  315. goto err;
  316. }
  317. clk_data->hws[mcd->id] = hw;
  318. }
  319. return 0;
  320. err:
  321. while (--i >= 0) {
  322. const struct mtk_clk_divider *mcd = &mcds[i];
  323. if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
  324. continue;
  325. clk_hw_unregister_divider(clk_data->hws[mcd->id]);
  326. clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
  327. }
  328. return PTR_ERR(hw);
  329. }
  330. EXPORT_SYMBOL_GPL(mtk_clk_register_dividers);
  331. void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
  332. struct clk_hw_onecell_data *clk_data)
  333. {
  334. int i;
  335. if (!clk_data)
  336. return;
  337. for (i = num; i > 0; i--) {
  338. const struct mtk_clk_divider *mcd = &mcds[i - 1];
  339. if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
  340. continue;
  341. clk_hw_unregister_divider(clk_data->hws[mcd->id]);
  342. clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
  343. }
  344. }
  345. EXPORT_SYMBOL_GPL(mtk_clk_unregister_dividers);
  346. int mtk_clk_simple_probe(struct platform_device *pdev)
  347. {
  348. const struct mtk_clk_desc *mcd;
  349. struct clk_hw_onecell_data *clk_data;
  350. struct device_node *node = pdev->dev.of_node;
  351. int r;
  352. mcd = of_device_get_match_data(&pdev->dev);
  353. if (!mcd)
  354. return -EINVAL;
  355. clk_data = mtk_alloc_clk_data(mcd->num_clks);
  356. if (!clk_data)
  357. return -ENOMEM;
  358. r = mtk_clk_register_gates_with_dev(node, mcd->clks, mcd->num_clks,
  359. clk_data, &pdev->dev);
  360. if (r)
  361. goto free_data;
  362. r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  363. if (r)
  364. goto unregister_clks;
  365. platform_set_drvdata(pdev, clk_data);
  366. if (mcd->rst_desc) {
  367. r = mtk_register_reset_controller_with_dev(&pdev->dev,
  368. mcd->rst_desc);
  369. if (r)
  370. goto unregister_clks;
  371. }
  372. return r;
  373. unregister_clks:
  374. mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
  375. free_data:
  376. mtk_free_clk_data(clk_data);
  377. return r;
  378. }
  379. EXPORT_SYMBOL_GPL(mtk_clk_simple_probe);
  380. int mtk_clk_simple_remove(struct platform_device *pdev)
  381. {
  382. const struct mtk_clk_desc *mcd = of_device_get_match_data(&pdev->dev);
  383. struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
  384. struct device_node *node = pdev->dev.of_node;
  385. of_clk_del_provider(node);
  386. mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
  387. mtk_free_clk_data(clk_data);
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(mtk_clk_simple_remove);
  391. MODULE_LICENSE("GPL");