cgu.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Ingenic SoC CGU driver
  4. *
  5. * Copyright (c) 2013-2015 Imagination Technologies
  6. * Author: Paul Burton <paul.burton@mips.com>
  7. */
  8. #ifndef __DRIVERS_CLK_INGENIC_CGU_H__
  9. #define __DRIVERS_CLK_INGENIC_CGU_H__
  10. #include <linux/bitops.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/of.h>
  13. #include <linux/spinlock.h>
  14. /**
  15. * struct ingenic_cgu_pll_info - information about a PLL
  16. * @reg: the offset of the PLL's control register within the CGU
  17. * @rate_multiplier: the multiplier needed by pll rate calculation
  18. * @m_shift: the number of bits to shift the multiplier value by (ie. the
  19. * index of the lowest bit of the multiplier value in the PLL's
  20. * control register)
  21. * @m_bits: the size of the multiplier field in bits
  22. * @m_offset: the multiplier value which encodes to 0 in the PLL's control
  23. * register
  24. * @n_shift: the number of bits to shift the divider value by (ie. the
  25. * index of the lowest bit of the divider value in the PLL's
  26. * control register)
  27. * @n_bits: the size of the divider field in bits
  28. * @n_offset: the divider value which encodes to 0 in the PLL's control
  29. * register
  30. * @od_shift: the number of bits to shift the post-VCO divider value by (ie.
  31. * the index of the lowest bit of the post-VCO divider value in
  32. * the PLL's control register)
  33. * @od_bits: the size of the post-VCO divider field in bits, or 0 if no
  34. * OD field exists (then the OD is fixed to 1)
  35. * @od_max: the maximum post-VCO divider value
  36. * @od_encoding: a pointer to an array mapping post-VCO divider values to
  37. * their encoded values in the PLL control register, or -1 for
  38. * unsupported values
  39. * @bypass_reg: the offset of the bypass control register within the CGU
  40. * @bypass_bit: the index of the bypass bit in the PLL control register, or
  41. * -1 if there is no bypass bit
  42. * @enable_bit: the index of the enable bit in the PLL control register, or
  43. * -1 if there is no enable bit (ie, the PLL is always on)
  44. * @stable_bit: the index of the stable bit in the PLL control register, or
  45. * -1 if there is no stable bit
  46. * @set_rate_hook: hook called immediately after updating the CGU register,
  47. * before releasing the spinlock
  48. */
  49. struct ingenic_cgu_pll_info {
  50. unsigned reg;
  51. unsigned rate_multiplier;
  52. const s8 *od_encoding;
  53. u8 m_shift, m_bits, m_offset;
  54. u8 n_shift, n_bits, n_offset;
  55. u8 od_shift, od_bits, od_max;
  56. unsigned bypass_reg;
  57. s8 bypass_bit;
  58. s8 enable_bit;
  59. s8 stable_bit;
  60. void (*calc_m_n_od)(const struct ingenic_cgu_pll_info *pll_info,
  61. unsigned long rate, unsigned long parent_rate,
  62. unsigned int *m, unsigned int *n, unsigned int *od);
  63. void (*set_rate_hook)(const struct ingenic_cgu_pll_info *pll_info,
  64. unsigned long rate, unsigned long parent_rate);
  65. };
  66. /**
  67. * struct ingenic_cgu_mux_info - information about a clock mux
  68. * @reg: offset of the mux control register within the CGU
  69. * @shift: number of bits to shift the mux value by (ie. the index of
  70. * the lowest bit of the mux value within its control register)
  71. * @bits: the size of the mux value in bits
  72. */
  73. struct ingenic_cgu_mux_info {
  74. unsigned reg;
  75. u8 shift;
  76. u8 bits;
  77. };
  78. /**
  79. * struct ingenic_cgu_div_info - information about a divider
  80. * @reg: offset of the divider control register within the CGU
  81. * @shift: number of bits to left shift the divide value by (ie. the index of
  82. * the lowest bit of the divide value within its control register)
  83. * @div: number to divide the divider value by (i.e. if the
  84. * effective divider value is the value written to the register
  85. * multiplied by some constant)
  86. * @bits: the size of the divide value in bits
  87. * @ce_bit: the index of the change enable bit within reg, or -1 if there
  88. * isn't one
  89. * @busy_bit: the index of the busy bit within reg, or -1 if there isn't one
  90. * @stop_bit: the index of the stop bit within reg, or -1 if there isn't one
  91. * @bypass_mask: mask of parent clocks for which the divider does not apply
  92. * @div_table: optional table to map the value read from the register to the
  93. * actual divider value
  94. */
  95. struct ingenic_cgu_div_info {
  96. unsigned reg;
  97. u8 shift;
  98. u8 div;
  99. u8 bits;
  100. s8 ce_bit;
  101. s8 busy_bit;
  102. s8 stop_bit;
  103. u8 bypass_mask;
  104. const u8 *div_table;
  105. };
  106. /**
  107. * struct ingenic_cgu_fixdiv_info - information about a fixed divider
  108. * @div: the divider applied to the parent clock
  109. */
  110. struct ingenic_cgu_fixdiv_info {
  111. unsigned div;
  112. };
  113. /**
  114. * struct ingenic_cgu_gate_info - information about a clock gate
  115. * @reg: offset of the gate control register within the CGU
  116. * @bit: offset of the bit in the register that controls the gate
  117. * @clear_to_gate: if set, the clock is gated when the bit is cleared
  118. * @delay_us: delay in microseconds after which the clock is considered stable
  119. */
  120. struct ingenic_cgu_gate_info {
  121. unsigned reg;
  122. u8 bit;
  123. bool clear_to_gate;
  124. u16 delay_us;
  125. };
  126. /**
  127. * struct ingenic_cgu_custom_info - information about a custom (SoC) clock
  128. * @clk_ops: custom clock operation callbacks
  129. */
  130. struct ingenic_cgu_custom_info {
  131. const struct clk_ops *clk_ops;
  132. };
  133. /**
  134. * struct ingenic_cgu_clk_info - information about a clock
  135. * @name: name of the clock
  136. * @type: a bitmask formed from CGU_CLK_* values
  137. * @flags: common clock flags to set on this clock
  138. * @parents: an array of the indices of potential parents of this clock
  139. * within the clock_info array of the CGU, or -1 in entries
  140. * which correspond to no valid parent
  141. * @pll: information valid if type includes CGU_CLK_PLL
  142. * @gate: information valid if type includes CGU_CLK_GATE
  143. * @mux: information valid if type includes CGU_CLK_MUX
  144. * @div: information valid if type includes CGU_CLK_DIV
  145. * @fixdiv: information valid if type includes CGU_CLK_FIXDIV
  146. * @custom: information valid if type includes CGU_CLK_CUSTOM
  147. */
  148. struct ingenic_cgu_clk_info {
  149. const char *name;
  150. enum {
  151. CGU_CLK_NONE = 0,
  152. CGU_CLK_EXT = BIT(0),
  153. CGU_CLK_PLL = BIT(1),
  154. CGU_CLK_GATE = BIT(2),
  155. CGU_CLK_MUX = BIT(3),
  156. CGU_CLK_MUX_GLITCHFREE = BIT(4),
  157. CGU_CLK_DIV = BIT(5),
  158. CGU_CLK_FIXDIV = BIT(6),
  159. CGU_CLK_CUSTOM = BIT(7),
  160. } type;
  161. unsigned long flags;
  162. int parents[4];
  163. union {
  164. struct ingenic_cgu_pll_info pll;
  165. struct {
  166. struct ingenic_cgu_gate_info gate;
  167. struct ingenic_cgu_mux_info mux;
  168. struct ingenic_cgu_div_info div;
  169. struct ingenic_cgu_fixdiv_info fixdiv;
  170. };
  171. struct ingenic_cgu_custom_info custom;
  172. };
  173. };
  174. /**
  175. * struct ingenic_cgu - data about the CGU
  176. * @np: the device tree node that caused the CGU to be probed
  177. * @base: the ioremap'ed base address of the CGU registers
  178. * @clock_info: an array containing information about implemented clocks
  179. * @clocks: used to provide clocks to DT, allows lookup of struct clk*
  180. * @lock: lock to be held whilst manipulating CGU registers
  181. */
  182. struct ingenic_cgu {
  183. struct device_node *np;
  184. void __iomem *base;
  185. const struct ingenic_cgu_clk_info *clock_info;
  186. struct clk_onecell_data clocks;
  187. spinlock_t lock;
  188. };
  189. /**
  190. * struct ingenic_clk - private data for a clock
  191. * @hw: see Documentation/driver-api/clk.rst
  192. * @cgu: a pointer to the CGU data
  193. * @idx: the index of this clock in cgu->clock_info
  194. */
  195. struct ingenic_clk {
  196. struct clk_hw hw;
  197. struct ingenic_cgu *cgu;
  198. unsigned idx;
  199. };
  200. #define to_ingenic_clk(_hw) container_of(_hw, struct ingenic_clk, hw)
  201. /**
  202. * ingenic_cgu_new() - create a new CGU instance
  203. * @clock_info: an array of clock information structures describing the clocks
  204. * which are implemented by the CGU
  205. * @num_clocks: the number of entries in clock_info
  206. * @np: the device tree node which causes this CGU to be probed
  207. *
  208. * Return: a pointer to the CGU instance if initialisation is successful,
  209. * otherwise NULL.
  210. */
  211. struct ingenic_cgu *
  212. ingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info,
  213. unsigned num_clocks, struct device_node *np);
  214. /**
  215. * ingenic_cgu_register_clocks() - Registers the clocks
  216. * @cgu: pointer to cgu data
  217. *
  218. * Register the clocks described by the CGU with the common clock framework.
  219. *
  220. * Return: 0 on success or -errno if unsuccesful.
  221. */
  222. int ingenic_cgu_register_clocks(struct ingenic_cgu *cgu);
  223. #endif /* __DRIVERS_CLK_INGENIC_CGU_H__ */