clk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014 MundoReader S.L.
  4. * Author: Heiko Stuebner <heiko@sntech.de>
  5. *
  6. * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
  7. * Author: Xing Zheng <zhengxing@rock-chips.com>
  8. *
  9. * based on
  10. *
  11. * samsung/clk.c
  12. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  13. * Copyright (c) 2013 Linaro Ltd.
  14. * Author: Thomas Abraham <thomas.ab@samsung.com>
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/io.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/reboot.h>
  24. #include "../clk-fractional-divider.h"
  25. #include "clk.h"
  26. /*
  27. * Register a clock branch.
  28. * Most clock branches have a form like
  29. *
  30. * src1 --|--\
  31. * |M |--[GATE]-[DIV]-
  32. * src2 --|--/
  33. *
  34. * sometimes without one of those components.
  35. */
  36. static struct clk *rockchip_clk_register_branch(const char *name,
  37. const char *const *parent_names, u8 num_parents,
  38. void __iomem *base,
  39. int muxdiv_offset, u8 mux_shift, u8 mux_width, u8 mux_flags,
  40. u32 *mux_table,
  41. int div_offset, u8 div_shift, u8 div_width, u8 div_flags,
  42. struct clk_div_table *div_table, int gate_offset,
  43. u8 gate_shift, u8 gate_flags, unsigned long flags,
  44. spinlock_t *lock)
  45. {
  46. struct clk_hw *hw;
  47. struct clk_mux *mux = NULL;
  48. struct clk_gate *gate = NULL;
  49. struct clk_divider *div = NULL;
  50. const struct clk_ops *mux_ops = NULL, *div_ops = NULL,
  51. *gate_ops = NULL;
  52. int ret;
  53. if (num_parents > 1) {
  54. mux = kzalloc_obj(*mux);
  55. if (!mux)
  56. return ERR_PTR(-ENOMEM);
  57. mux->reg = base + muxdiv_offset;
  58. mux->shift = mux_shift;
  59. mux->mask = BIT(mux_width) - 1;
  60. mux->flags = mux_flags;
  61. mux->table = mux_table;
  62. mux->lock = lock;
  63. mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops
  64. : &clk_mux_ops;
  65. }
  66. if (gate_offset >= 0) {
  67. gate = kzalloc_obj(*gate);
  68. if (!gate) {
  69. ret = -ENOMEM;
  70. goto err_gate;
  71. }
  72. gate->flags = gate_flags;
  73. gate->reg = base + gate_offset;
  74. gate->bit_idx = gate_shift;
  75. gate->lock = lock;
  76. gate_ops = &clk_gate_ops;
  77. }
  78. if (div_width > 0) {
  79. div = kzalloc_obj(*div);
  80. if (!div) {
  81. ret = -ENOMEM;
  82. goto err_div;
  83. }
  84. div->flags = div_flags;
  85. if (div_offset)
  86. div->reg = base + div_offset;
  87. else
  88. div->reg = base + muxdiv_offset;
  89. div->shift = div_shift;
  90. div->width = div_width;
  91. div->lock = lock;
  92. div->table = div_table;
  93. div_ops = (div_flags & CLK_DIVIDER_READ_ONLY)
  94. ? &clk_divider_ro_ops
  95. : &clk_divider_ops;
  96. }
  97. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  98. mux ? &mux->hw : NULL, mux_ops,
  99. div ? &div->hw : NULL, div_ops,
  100. gate ? &gate->hw : NULL, gate_ops,
  101. flags);
  102. if (IS_ERR(hw)) {
  103. kfree(div);
  104. kfree(gate);
  105. return ERR_CAST(hw);
  106. }
  107. return hw->clk;
  108. err_div:
  109. kfree(gate);
  110. err_gate:
  111. kfree(mux);
  112. return ERR_PTR(ret);
  113. }
  114. struct rockchip_clk_frac {
  115. struct notifier_block clk_nb;
  116. struct clk_fractional_divider div;
  117. struct clk_gate gate;
  118. struct clk_mux mux;
  119. const struct clk_ops *mux_ops;
  120. int mux_frac_idx;
  121. bool rate_change_remuxed;
  122. int rate_change_idx;
  123. };
  124. #define to_rockchip_clk_frac_nb(nb) \
  125. container_of(nb, struct rockchip_clk_frac, clk_nb)
  126. static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb,
  127. unsigned long event, void *data)
  128. {
  129. struct clk_notifier_data *ndata = data;
  130. struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb);
  131. struct clk_mux *frac_mux = &frac->mux;
  132. int ret = 0;
  133. pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
  134. __func__, event, ndata->old_rate, ndata->new_rate);
  135. if (event == PRE_RATE_CHANGE) {
  136. frac->rate_change_idx =
  137. frac->mux_ops->get_parent(&frac_mux->hw);
  138. if (frac->rate_change_idx != frac->mux_frac_idx) {
  139. frac->mux_ops->set_parent(&frac_mux->hw,
  140. frac->mux_frac_idx);
  141. frac->rate_change_remuxed = 1;
  142. }
  143. } else if (event == POST_RATE_CHANGE) {
  144. /*
  145. * The POST_RATE_CHANGE notifier runs directly after the
  146. * divider clock is set in clk_change_rate, so we'll have
  147. * remuxed back to the original parent before clk_change_rate
  148. * reaches the mux itself.
  149. */
  150. if (frac->rate_change_remuxed) {
  151. frac->mux_ops->set_parent(&frac_mux->hw,
  152. frac->rate_change_idx);
  153. frac->rate_change_remuxed = 0;
  154. }
  155. }
  156. return notifier_from_errno(ret);
  157. }
  158. /*
  159. * fractional divider must set that denominator is 20 times larger than
  160. * numerator to generate precise clock frequency.
  161. */
  162. static void rockchip_fractional_approximation(struct clk_hw *hw,
  163. unsigned long rate, unsigned long *parent_rate,
  164. unsigned long *m, unsigned long *n)
  165. {
  166. struct clk_fractional_divider *fd = to_clk_fd(hw);
  167. unsigned long p_rate, p_parent_rate;
  168. struct clk_hw *p_parent;
  169. p_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
  170. if ((rate * 20 > p_rate) && (p_rate % rate != 0)) {
  171. p_parent = clk_hw_get_parent(clk_hw_get_parent(hw));
  172. p_parent_rate = clk_hw_get_rate(p_parent);
  173. *parent_rate = p_parent_rate;
  174. }
  175. fd->flags |= CLK_FRAC_DIVIDER_POWER_OF_TWO_PS;
  176. clk_fractional_divider_general_approximation(hw, rate, parent_rate, m, n);
  177. }
  178. static struct clk *rockchip_clk_register_frac_branch(
  179. struct rockchip_clk_provider *ctx, const char *name,
  180. const char *const *parent_names, u8 num_parents,
  181. void __iomem *base, int muxdiv_offset, u8 div_flags,
  182. int gate_offset, u8 gate_shift, u8 gate_flags,
  183. unsigned long flags, struct rockchip_clk_branch *child,
  184. spinlock_t *lock)
  185. {
  186. struct clk_hw *hw;
  187. struct rockchip_clk_frac *frac;
  188. struct clk_gate *gate = NULL;
  189. struct clk_fractional_divider *div = NULL;
  190. const struct clk_ops *div_ops = NULL, *gate_ops = NULL;
  191. if (muxdiv_offset < 0)
  192. return ERR_PTR(-EINVAL);
  193. if (child && child->branch_type != branch_mux) {
  194. pr_err("%s: fractional child clock for %s can only be a mux\n",
  195. __func__, name);
  196. return ERR_PTR(-EINVAL);
  197. }
  198. frac = kzalloc_obj(*frac);
  199. if (!frac)
  200. return ERR_PTR(-ENOMEM);
  201. if (gate_offset >= 0) {
  202. gate = &frac->gate;
  203. gate->flags = gate_flags;
  204. gate->reg = base + gate_offset;
  205. gate->bit_idx = gate_shift;
  206. gate->lock = lock;
  207. gate_ops = &clk_gate_ops;
  208. }
  209. div = &frac->div;
  210. div->flags = div_flags;
  211. div->reg = base + muxdiv_offset;
  212. div->mshift = 16;
  213. div->mwidth = 16;
  214. div->nshift = 0;
  215. div->nwidth = 16;
  216. div->lock = lock;
  217. div->approximation = rockchip_fractional_approximation;
  218. div_ops = &clk_fractional_divider_ops;
  219. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  220. NULL, NULL,
  221. &div->hw, div_ops,
  222. gate ? &gate->hw : NULL, gate_ops,
  223. flags | CLK_SET_RATE_UNGATE);
  224. if (IS_ERR(hw)) {
  225. kfree(frac);
  226. return ERR_CAST(hw);
  227. }
  228. if (child) {
  229. struct clk_mux *frac_mux = &frac->mux;
  230. struct clk_init_data init;
  231. struct clk *mux_clk;
  232. int ret;
  233. frac->mux_frac_idx = match_string(child->parent_names,
  234. child->num_parents, name);
  235. frac->mux_ops = &clk_mux_ops;
  236. frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb;
  237. frac_mux->reg = base + child->muxdiv_offset;
  238. frac_mux->shift = child->mux_shift;
  239. frac_mux->mask = BIT(child->mux_width) - 1;
  240. frac_mux->flags = child->mux_flags;
  241. if (child->mux_table)
  242. frac_mux->table = child->mux_table;
  243. frac_mux->lock = lock;
  244. frac_mux->hw.init = &init;
  245. init.name = child->name;
  246. init.flags = child->flags | CLK_SET_RATE_PARENT;
  247. init.ops = frac->mux_ops;
  248. init.parent_names = child->parent_names;
  249. init.num_parents = child->num_parents;
  250. mux_clk = clk_register(NULL, &frac_mux->hw);
  251. if (IS_ERR(mux_clk)) {
  252. kfree(frac);
  253. return mux_clk;
  254. }
  255. rockchip_clk_set_lookup(ctx, mux_clk, child->id);
  256. /* notifier on the fraction divider to catch rate changes */
  257. if (frac->mux_frac_idx >= 0) {
  258. pr_debug("%s: found fractional parent in mux at pos %d\n",
  259. __func__, frac->mux_frac_idx);
  260. ret = clk_notifier_register(hw->clk, &frac->clk_nb);
  261. if (ret)
  262. pr_err("%s: failed to register clock notifier for %s\n",
  263. __func__, name);
  264. } else {
  265. pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n",
  266. __func__, name, child->name);
  267. }
  268. }
  269. return hw->clk;
  270. }
  271. static struct clk *rockchip_clk_register_factor_branch(const char *name,
  272. const char *const *parent_names, u8 num_parents,
  273. void __iomem *base, unsigned int mult, unsigned int div,
  274. int gate_offset, u8 gate_shift, u8 gate_flags,
  275. unsigned long flags, spinlock_t *lock)
  276. {
  277. struct clk_hw *hw;
  278. struct clk_gate *gate = NULL;
  279. struct clk_fixed_factor *fix = NULL;
  280. /* without gate, register a simple factor clock */
  281. if (gate_offset == 0) {
  282. return clk_register_fixed_factor(NULL, name,
  283. parent_names[0], flags, mult,
  284. div);
  285. }
  286. gate = kzalloc_obj(*gate);
  287. if (!gate)
  288. return ERR_PTR(-ENOMEM);
  289. gate->flags = gate_flags;
  290. gate->reg = base + gate_offset;
  291. gate->bit_idx = gate_shift;
  292. gate->lock = lock;
  293. fix = kzalloc_obj(*fix);
  294. if (!fix) {
  295. kfree(gate);
  296. return ERR_PTR(-ENOMEM);
  297. }
  298. fix->mult = mult;
  299. fix->div = div;
  300. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  301. NULL, NULL,
  302. &fix->hw, &clk_fixed_factor_ops,
  303. &gate->hw, &clk_gate_ops, flags);
  304. if (IS_ERR(hw)) {
  305. kfree(fix);
  306. kfree(gate);
  307. return ERR_CAST(hw);
  308. }
  309. return hw->clk;
  310. }
  311. static struct rockchip_clk_provider *rockchip_clk_init_base(
  312. struct device_node *np, void __iomem *base,
  313. unsigned long nr_clks, bool has_late_clocks)
  314. {
  315. struct rockchip_clk_provider *ctx;
  316. struct clk **clk_table;
  317. struct clk *default_clk_val;
  318. int i;
  319. default_clk_val = ERR_PTR(has_late_clocks ? -EPROBE_DEFER : -ENOENT);
  320. ctx = kzalloc_obj(struct rockchip_clk_provider);
  321. if (!ctx)
  322. return ERR_PTR(-ENOMEM);
  323. clk_table = kzalloc_objs(struct clk *, nr_clks);
  324. if (!clk_table)
  325. goto err_free;
  326. for (i = 0; i < nr_clks; ++i)
  327. clk_table[i] = default_clk_val;
  328. ctx->reg_base = base;
  329. ctx->clk_data.clks = clk_table;
  330. ctx->clk_data.clk_num = nr_clks;
  331. ctx->cru_node = np;
  332. spin_lock_init(&ctx->lock);
  333. hash_init(ctx->aux_grf_table);
  334. ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node,
  335. "rockchip,grf");
  336. return ctx;
  337. err_free:
  338. kfree(ctx);
  339. return ERR_PTR(-ENOMEM);
  340. }
  341. struct rockchip_clk_provider *rockchip_clk_init(struct device_node *np,
  342. void __iomem *base,
  343. unsigned long nr_clks)
  344. {
  345. return rockchip_clk_init_base(np, base, nr_clks, false);
  346. }
  347. EXPORT_SYMBOL_GPL(rockchip_clk_init);
  348. struct rockchip_clk_provider *rockchip_clk_init_early(struct device_node *np,
  349. void __iomem *base,
  350. unsigned long nr_clks)
  351. {
  352. return rockchip_clk_init_base(np, base, nr_clks, true);
  353. }
  354. EXPORT_SYMBOL_GPL(rockchip_clk_init_early);
  355. void rockchip_clk_finalize(struct rockchip_clk_provider *ctx)
  356. {
  357. int i;
  358. for (i = 0; i < ctx->clk_data.clk_num; ++i)
  359. if (ctx->clk_data.clks[i] == ERR_PTR(-EPROBE_DEFER))
  360. ctx->clk_data.clks[i] = ERR_PTR(-ENOENT);
  361. }
  362. EXPORT_SYMBOL_GPL(rockchip_clk_finalize);
  363. void rockchip_clk_of_add_provider(struct device_node *np,
  364. struct rockchip_clk_provider *ctx)
  365. {
  366. if (of_clk_add_provider(np, of_clk_src_onecell_get,
  367. &ctx->clk_data))
  368. pr_err("%s: could not register clk provider\n", __func__);
  369. }
  370. EXPORT_SYMBOL_GPL(rockchip_clk_of_add_provider);
  371. void rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
  372. struct rockchip_pll_clock *list,
  373. unsigned int nr_pll, int grf_lock_offset)
  374. {
  375. struct clk *clk;
  376. int idx;
  377. for (idx = 0; idx < nr_pll; idx++, list++) {
  378. clk = rockchip_clk_register_pll(ctx, list->type, list->name,
  379. list->parent_names, list->num_parents,
  380. list->con_offset, grf_lock_offset,
  381. list->lock_shift, list->mode_offset,
  382. list->mode_shift, list->rate_table,
  383. list->flags, list->pll_flags);
  384. if (IS_ERR(clk)) {
  385. pr_err("%s: failed to register clock %s\n", __func__,
  386. list->name);
  387. continue;
  388. }
  389. rockchip_clk_set_lookup(ctx, clk, list->id);
  390. }
  391. }
  392. EXPORT_SYMBOL_GPL(rockchip_clk_register_plls);
  393. unsigned long rockchip_clk_find_max_clk_id(struct rockchip_clk_branch *list,
  394. unsigned int nr_clk)
  395. {
  396. unsigned long max = 0;
  397. unsigned int idx;
  398. for (idx = 0; idx < nr_clk; idx++, list++) {
  399. if (list->id > max)
  400. max = list->id;
  401. if (list->child && list->child->id > max)
  402. max = list->child->id;
  403. }
  404. return max;
  405. }
  406. EXPORT_SYMBOL_GPL(rockchip_clk_find_max_clk_id);
  407. static struct platform_device *rockchip_clk_register_gate_link(
  408. struct device *parent_dev,
  409. struct rockchip_clk_provider *ctx,
  410. struct rockchip_clk_branch *clkbr)
  411. {
  412. struct rockchip_gate_link_platdata gate_link_pdata = {
  413. .ctx = ctx,
  414. .clkbr = clkbr,
  415. };
  416. struct platform_device_info pdevinfo = {
  417. .parent = parent_dev,
  418. .name = "rockchip-gate-link-clk",
  419. .id = clkbr->id,
  420. .fwnode = dev_fwnode(parent_dev),
  421. .of_node_reused = true,
  422. .data = &gate_link_pdata,
  423. .size_data = sizeof(gate_link_pdata),
  424. };
  425. return platform_device_register_full(&pdevinfo);
  426. }
  427. void rockchip_clk_register_branches(struct rockchip_clk_provider *ctx,
  428. struct rockchip_clk_branch *list,
  429. unsigned int nr_clk)
  430. {
  431. struct regmap *grf = ctx->grf;
  432. struct rockchip_aux_grf *agrf;
  433. struct clk *clk;
  434. unsigned int idx;
  435. unsigned long flags;
  436. for (idx = 0; idx < nr_clk; idx++, list++) {
  437. flags = list->flags;
  438. clk = NULL;
  439. /* for GRF-dependent branches, choose the right grf first */
  440. if ((list->branch_type == branch_grf_mux ||
  441. list->branch_type == branch_grf_gate ||
  442. list->branch_type == branch_grf_mmc) &&
  443. list->grf_type != grf_type_sys) {
  444. hash_for_each_possible(ctx->aux_grf_table, agrf, node, list->grf_type) {
  445. if (agrf->type == list->grf_type) {
  446. grf = agrf->grf;
  447. break;
  448. }
  449. }
  450. }
  451. /* catch simple muxes */
  452. switch (list->branch_type) {
  453. case branch_mux:
  454. if (list->mux_table)
  455. clk = clk_register_mux_table(NULL, list->name,
  456. list->parent_names, list->num_parents,
  457. flags,
  458. ctx->reg_base + list->muxdiv_offset,
  459. list->mux_shift, list->mux_width,
  460. list->mux_flags, list->mux_table,
  461. &ctx->lock);
  462. else
  463. clk = clk_register_mux(NULL, list->name,
  464. list->parent_names, list->num_parents,
  465. flags,
  466. ctx->reg_base + list->muxdiv_offset,
  467. list->mux_shift, list->mux_width,
  468. list->mux_flags, &ctx->lock);
  469. break;
  470. case branch_grf_mux:
  471. clk = rockchip_clk_register_muxgrf(list->name,
  472. list->parent_names, list->num_parents,
  473. flags, grf, list->muxdiv_offset,
  474. list->mux_shift, list->mux_width,
  475. list->mux_flags);
  476. break;
  477. case branch_divider:
  478. if (list->div_table)
  479. clk = clk_register_divider_table(NULL,
  480. list->name, list->parent_names[0],
  481. flags,
  482. ctx->reg_base + list->muxdiv_offset,
  483. list->div_shift, list->div_width,
  484. list->div_flags, list->div_table,
  485. &ctx->lock);
  486. else
  487. clk = clk_register_divider(NULL, list->name,
  488. list->parent_names[0], flags,
  489. ctx->reg_base + list->muxdiv_offset,
  490. list->div_shift, list->div_width,
  491. list->div_flags, &ctx->lock);
  492. break;
  493. case branch_fraction_divider:
  494. clk = rockchip_clk_register_frac_branch(ctx, list->name,
  495. list->parent_names, list->num_parents,
  496. ctx->reg_base, list->muxdiv_offset,
  497. list->div_flags,
  498. list->gate_offset, list->gate_shift,
  499. list->gate_flags, flags, list->child,
  500. &ctx->lock);
  501. break;
  502. case branch_half_divider:
  503. clk = rockchip_clk_register_halfdiv(list->name,
  504. list->parent_names, list->num_parents,
  505. ctx->reg_base, list->muxdiv_offset,
  506. list->mux_shift, list->mux_width,
  507. list->mux_flags, list->div_shift,
  508. list->div_width, list->div_flags,
  509. list->gate_offset, list->gate_shift,
  510. list->gate_flags, flags, &ctx->lock);
  511. break;
  512. case branch_gate:
  513. flags |= CLK_SET_RATE_PARENT;
  514. clk = clk_register_gate(NULL, list->name,
  515. list->parent_names[0], flags,
  516. ctx->reg_base + list->gate_offset,
  517. list->gate_shift, list->gate_flags, &ctx->lock);
  518. break;
  519. case branch_grf_gate:
  520. flags |= CLK_SET_RATE_PARENT;
  521. clk = rockchip_clk_register_gate_grf(list->name,
  522. list->parent_names[0], flags, grf,
  523. list->gate_offset, list->gate_shift,
  524. list->gate_flags);
  525. break;
  526. case branch_composite:
  527. clk = rockchip_clk_register_branch(list->name,
  528. list->parent_names, list->num_parents,
  529. ctx->reg_base, list->muxdiv_offset,
  530. list->mux_shift,
  531. list->mux_width, list->mux_flags,
  532. list->mux_table, list->div_offset,
  533. list->div_shift, list->div_width,
  534. list->div_flags, list->div_table,
  535. list->gate_offset, list->gate_shift,
  536. list->gate_flags, flags, &ctx->lock);
  537. break;
  538. case branch_mmc:
  539. clk = rockchip_clk_register_mmc(
  540. list->name,
  541. list->parent_names, list->num_parents,
  542. ctx->reg_base + list->muxdiv_offset,
  543. NULL, 0,
  544. list->div_shift
  545. );
  546. break;
  547. case branch_grf_mmc:
  548. clk = rockchip_clk_register_mmc(
  549. list->name,
  550. list->parent_names, list->num_parents,
  551. NULL,
  552. grf, list->muxdiv_offset,
  553. list->div_shift
  554. );
  555. break;
  556. case branch_inverter:
  557. clk = rockchip_clk_register_inverter(
  558. list->name, list->parent_names,
  559. list->num_parents,
  560. ctx->reg_base + list->muxdiv_offset,
  561. list->div_shift, list->div_flags, &ctx->lock);
  562. break;
  563. case branch_factor:
  564. clk = rockchip_clk_register_factor_branch(
  565. list->name, list->parent_names,
  566. list->num_parents, ctx->reg_base,
  567. list->div_shift, list->div_width,
  568. list->gate_offset, list->gate_shift,
  569. list->gate_flags, flags, &ctx->lock);
  570. break;
  571. case branch_ddrclk:
  572. clk = rockchip_clk_register_ddrclk(
  573. list->name, list->flags,
  574. list->parent_names, list->num_parents,
  575. list->muxdiv_offset, list->mux_shift,
  576. list->mux_width, list->div_shift,
  577. list->div_width, list->div_flags,
  578. ctx->reg_base, &ctx->lock);
  579. break;
  580. case branch_linked_gate:
  581. /* must be registered late, fall-through for error message */
  582. break;
  583. }
  584. /* none of the cases above matched */
  585. if (!clk) {
  586. pr_err("%s: unknown clock type %d\n",
  587. __func__, list->branch_type);
  588. continue;
  589. }
  590. if (IS_ERR(clk)) {
  591. pr_err("%s: failed to register clock %s: %ld\n",
  592. __func__, list->name, PTR_ERR(clk));
  593. continue;
  594. }
  595. rockchip_clk_set_lookup(ctx, clk, list->id);
  596. }
  597. }
  598. EXPORT_SYMBOL_GPL(rockchip_clk_register_branches);
  599. void rockchip_clk_register_late_branches(struct device *dev,
  600. struct rockchip_clk_provider *ctx,
  601. struct rockchip_clk_branch *list,
  602. unsigned int nr_clk)
  603. {
  604. unsigned int idx;
  605. for (idx = 0; idx < nr_clk; idx++, list++) {
  606. struct platform_device *pdev = NULL;
  607. switch (list->branch_type) {
  608. case branch_linked_gate:
  609. pdev = rockchip_clk_register_gate_link(dev, ctx, list);
  610. break;
  611. default:
  612. dev_err(dev, "unknown clock type %d\n", list->branch_type);
  613. break;
  614. }
  615. if (IS_ERR_OR_NULL(pdev))
  616. dev_err(dev, "failed to register device for clock %s\n", list->name);
  617. }
  618. }
  619. EXPORT_SYMBOL_GPL(rockchip_clk_register_late_branches);
  620. void rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
  621. unsigned int lookup_id,
  622. const char *name, const char *const *parent_names,
  623. u8 num_parents,
  624. const struct rockchip_cpuclk_reg_data *reg_data,
  625. const struct rockchip_cpuclk_rate_table *rates,
  626. int nrates)
  627. {
  628. struct clk *clk;
  629. clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
  630. reg_data, rates, nrates,
  631. ctx->reg_base, &ctx->lock);
  632. if (IS_ERR(clk)) {
  633. pr_err("%s: failed to register clock %s: %ld\n",
  634. __func__, name, PTR_ERR(clk));
  635. return;
  636. }
  637. rockchip_clk_set_lookup(ctx, clk, lookup_id);
  638. }
  639. EXPORT_SYMBOL_GPL(rockchip_clk_register_armclk);
  640. void rockchip_clk_register_armclk_multi_pll(struct rockchip_clk_provider *ctx,
  641. struct rockchip_clk_branch *list,
  642. const struct rockchip_cpuclk_rate_table *rates,
  643. int nrates)
  644. {
  645. struct clk *clk;
  646. clk = rockchip_clk_register_cpuclk_multi_pll(list->name, list->parent_names,
  647. list->num_parents, ctx->reg_base,
  648. list->muxdiv_offset, list->mux_shift,
  649. list->mux_width, list->mux_flags,
  650. list->div_offset, list->div_shift,
  651. list->div_width, list->div_flags,
  652. list->flags, &ctx->lock, rates, nrates);
  653. if (IS_ERR(clk)) {
  654. pr_err("%s: failed to register clock %s: %ld\n",
  655. __func__, list->name, PTR_ERR(clk));
  656. return;
  657. }
  658. rockchip_clk_set_lookup(ctx, clk, list->id);
  659. }
  660. EXPORT_SYMBOL_GPL(rockchip_clk_register_armclk_multi_pll);
  661. void rockchip_clk_protect_critical(const char *const clocks[],
  662. int nclocks)
  663. {
  664. int i;
  665. /* Protect the clocks that needs to stay on */
  666. for (i = 0; i < nclocks; i++) {
  667. struct clk *clk = __clk_lookup(clocks[i]);
  668. clk_prepare_enable(clk);
  669. }
  670. }
  671. EXPORT_SYMBOL_GPL(rockchip_clk_protect_critical);
  672. static void __iomem *rst_base;
  673. static unsigned int reg_restart;
  674. static void (*cb_restart)(void);
  675. static int rockchip_restart_notify(struct notifier_block *this,
  676. unsigned long mode, void *cmd)
  677. {
  678. if (cb_restart)
  679. cb_restart();
  680. writel(0xfdb9, rst_base + reg_restart);
  681. return NOTIFY_DONE;
  682. }
  683. static struct notifier_block rockchip_restart_handler = {
  684. .notifier_call = rockchip_restart_notify,
  685. .priority = 128,
  686. };
  687. void
  688. rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
  689. unsigned int reg,
  690. void (*cb)(void))
  691. {
  692. int ret;
  693. rst_base = ctx->reg_base;
  694. reg_restart = reg;
  695. cb_restart = cb;
  696. ret = register_restart_handler(&rockchip_restart_handler);
  697. if (ret)
  698. pr_err("%s: cannot register restart handler, %d\n",
  699. __func__, ret);
  700. }
  701. EXPORT_SYMBOL_GPL(rockchip_register_restart_notifier);