rcar-cpg-lib.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R-Car Gen3 Clock Pulse Generator Library
  4. *
  5. * Copyright (C) 2015-2018 Glider bvba
  6. * Copyright (C) 2019 Renesas Electronics Corp.
  7. *
  8. * Based on clk-rcar-gen3.c
  9. *
  10. * Copyright (C) 2015 Renesas Electronics Corp.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/pm.h>
  19. #include <linux/slab.h>
  20. #include <linux/sys_soc.h>
  21. #include "rcar-cpg-lib.h"
  22. spinlock_t cpg_lock;
  23. void cpg_reg_modify(void __iomem *reg, u32 clear, u32 set)
  24. {
  25. unsigned long flags;
  26. u32 val;
  27. spin_lock_irqsave(&cpg_lock, flags);
  28. val = readl(reg);
  29. val &= ~clear;
  30. val |= set;
  31. writel(val, reg);
  32. spin_unlock_irqrestore(&cpg_lock, flags);
  33. };
  34. static int cpg_simple_notifier_call(struct notifier_block *nb,
  35. unsigned long action, void *data)
  36. {
  37. struct cpg_simple_notifier *csn =
  38. container_of(nb, struct cpg_simple_notifier, nb);
  39. switch (action) {
  40. case PM_EVENT_SUSPEND:
  41. csn->saved = readl(csn->reg);
  42. return NOTIFY_OK;
  43. case PM_EVENT_RESUME:
  44. writel(csn->saved, csn->reg);
  45. return NOTIFY_OK;
  46. }
  47. return NOTIFY_DONE;
  48. }
  49. void cpg_simple_notifier_register(struct raw_notifier_head *notifiers,
  50. struct cpg_simple_notifier *csn)
  51. {
  52. csn->nb.notifier_call = cpg_simple_notifier_call;
  53. raw_notifier_chain_register(notifiers, &csn->nb);
  54. }
  55. /*
  56. * SDn Clock
  57. */
  58. #define SDnSRCFC_SHIFT 2
  59. #define STPnHCK BIT(9 - SDnSRCFC_SHIFT)
  60. static const struct clk_div_table cpg_sdh_div_table[] = {
  61. /*
  62. * These values are recommended by the datasheet. Because they come
  63. * first, Linux will only use these.
  64. */
  65. { 0, 1 }, { 1, 2 }, { STPnHCK | 2, 4 }, { STPnHCK | 3, 8 },
  66. { STPnHCK | 4, 16 },
  67. /*
  68. * These values are not recommended because STPnHCK is wrong. But they
  69. * have been seen because of broken firmware. So, we support reading
  70. * them but Linux will sanitize them when initializing through
  71. * recalc_rate.
  72. */
  73. { STPnHCK | 0, 1 }, { STPnHCK | 1, 2 }, { 2, 4 }, { 3, 8 }, { 4, 16 },
  74. /* Sentinel */
  75. { 0, 0 }
  76. };
  77. struct clk * __init cpg_sdh_clk_register(const char *name,
  78. void __iomem *sdnckcr, const char *parent_name,
  79. struct raw_notifier_head *notifiers)
  80. {
  81. struct cpg_simple_notifier *csn;
  82. struct clk *clk;
  83. csn = kzalloc(sizeof(*csn), GFP_KERNEL);
  84. if (!csn)
  85. return ERR_PTR(-ENOMEM);
  86. csn->reg = sdnckcr;
  87. clk = clk_register_divider_table(NULL, name, parent_name, 0, sdnckcr,
  88. SDnSRCFC_SHIFT, 8, 0, cpg_sdh_div_table,
  89. &cpg_lock);
  90. if (IS_ERR(clk)) {
  91. kfree(csn);
  92. return clk;
  93. }
  94. cpg_simple_notifier_register(notifiers, csn);
  95. return clk;
  96. }
  97. static const struct clk_div_table cpg_sd_div_table[] = {
  98. { 0, 2 }, { 1, 4 }, { 0, 0 },
  99. };
  100. struct clk * __init cpg_sd_clk_register(const char *name,
  101. void __iomem *sdnckcr, const char *parent_name)
  102. {
  103. return clk_register_divider_table(NULL, name, parent_name, 0, sdnckcr,
  104. 0, 2, 0, cpg_sd_div_table, &cpg_lock);
  105. }
  106. struct rpc_clock {
  107. struct clk_divider div;
  108. struct clk_gate gate;
  109. /*
  110. * One notifier covers both RPC and RPCD2 clocks as they are both
  111. * controlled by the same RPCCKCR register...
  112. */
  113. struct cpg_simple_notifier csn;
  114. };
  115. static const struct clk_div_table cpg_rpc_div_table[] = {
  116. { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 0, 0 },
  117. };
  118. struct clk * __init cpg_rpc_clk_register(const char *name,
  119. void __iomem *rpcckcr, const char *parent_name,
  120. struct raw_notifier_head *notifiers)
  121. {
  122. struct rpc_clock *rpc;
  123. struct clk *clk;
  124. rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
  125. if (!rpc)
  126. return ERR_PTR(-ENOMEM);
  127. rpc->div.reg = rpcckcr;
  128. rpc->div.width = 3;
  129. rpc->div.table = cpg_rpc_div_table;
  130. rpc->div.lock = &cpg_lock;
  131. rpc->gate.reg = rpcckcr;
  132. rpc->gate.bit_idx = 8;
  133. rpc->gate.flags = CLK_GATE_SET_TO_DISABLE;
  134. rpc->gate.lock = &cpg_lock;
  135. rpc->csn.reg = rpcckcr;
  136. clk = clk_register_composite(NULL, name, &parent_name, 1, NULL, NULL,
  137. &rpc->div.hw, &clk_divider_ops,
  138. &rpc->gate.hw, &clk_gate_ops,
  139. CLK_SET_RATE_PARENT);
  140. if (IS_ERR(clk)) {
  141. kfree(rpc);
  142. return clk;
  143. }
  144. cpg_simple_notifier_register(notifiers, &rpc->csn);
  145. return clk;
  146. }
  147. struct rpcd2_clock {
  148. struct clk_fixed_factor fixed;
  149. struct clk_gate gate;
  150. };
  151. struct clk * __init cpg_rpcd2_clk_register(const char *name,
  152. void __iomem *rpcckcr,
  153. const char *parent_name)
  154. {
  155. struct rpcd2_clock *rpcd2;
  156. struct clk *clk;
  157. rpcd2 = kzalloc(sizeof(*rpcd2), GFP_KERNEL);
  158. if (!rpcd2)
  159. return ERR_PTR(-ENOMEM);
  160. rpcd2->fixed.mult = 1;
  161. rpcd2->fixed.div = 2;
  162. rpcd2->gate.reg = rpcckcr;
  163. rpcd2->gate.bit_idx = 9;
  164. rpcd2->gate.flags = CLK_GATE_SET_TO_DISABLE;
  165. rpcd2->gate.lock = &cpg_lock;
  166. clk = clk_register_composite(NULL, name, &parent_name, 1, NULL, NULL,
  167. &rpcd2->fixed.hw, &clk_fixed_factor_ops,
  168. &rpcd2->gate.hw, &clk_gate_ops,
  169. CLK_SET_RATE_PARENT);
  170. if (IS_ERR(clk))
  171. kfree(rpcd2);
  172. return clk;
  173. }