cgu.h 7.7 KB

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