syscon-clk.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/kernel.h>
  7. #include <linux/mfd/syscon.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regmap.h>
  11. #include <linux/slab.h>
  12. struct ti_syscon_gate_clk_priv {
  13. struct clk_hw hw;
  14. struct regmap *regmap;
  15. u32 reg;
  16. u32 idx;
  17. };
  18. struct ti_syscon_gate_clk_data {
  19. char *name;
  20. u32 offset;
  21. u32 bit_idx;
  22. };
  23. static struct
  24. ti_syscon_gate_clk_priv *to_ti_syscon_gate_clk_priv(struct clk_hw *hw)
  25. {
  26. return container_of(hw, struct ti_syscon_gate_clk_priv, hw);
  27. }
  28. static int ti_syscon_gate_clk_enable(struct clk_hw *hw)
  29. {
  30. struct ti_syscon_gate_clk_priv *priv = to_ti_syscon_gate_clk_priv(hw);
  31. return regmap_write_bits(priv->regmap, priv->reg, priv->idx,
  32. priv->idx);
  33. }
  34. static void ti_syscon_gate_clk_disable(struct clk_hw *hw)
  35. {
  36. struct ti_syscon_gate_clk_priv *priv = to_ti_syscon_gate_clk_priv(hw);
  37. regmap_write_bits(priv->regmap, priv->reg, priv->idx, 0);
  38. }
  39. static int ti_syscon_gate_clk_is_enabled(struct clk_hw *hw)
  40. {
  41. unsigned int val;
  42. struct ti_syscon_gate_clk_priv *priv = to_ti_syscon_gate_clk_priv(hw);
  43. regmap_read(priv->regmap, priv->reg, &val);
  44. return !!(val & priv->idx);
  45. }
  46. static const struct clk_ops ti_syscon_gate_clk_ops = {
  47. .enable = ti_syscon_gate_clk_enable,
  48. .disable = ti_syscon_gate_clk_disable,
  49. .is_enabled = ti_syscon_gate_clk_is_enabled,
  50. };
  51. static struct clk_hw
  52. *ti_syscon_gate_clk_register(struct device *dev, struct regmap *regmap,
  53. const char *parent_name,
  54. const struct ti_syscon_gate_clk_data *data)
  55. {
  56. struct ti_syscon_gate_clk_priv *priv;
  57. struct clk_init_data init;
  58. char *name = NULL;
  59. int ret;
  60. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  61. if (!priv)
  62. return ERR_PTR(-ENOMEM);
  63. init.ops = &ti_syscon_gate_clk_ops;
  64. if (parent_name) {
  65. name = kasprintf(GFP_KERNEL, "%s:%s", data->name, parent_name);
  66. init.name = name;
  67. init.parent_names = &parent_name;
  68. init.num_parents = 1;
  69. init.flags = CLK_SET_RATE_PARENT;
  70. } else {
  71. init.name = data->name;
  72. init.parent_names = NULL;
  73. init.num_parents = 0;
  74. init.flags = 0;
  75. }
  76. priv->regmap = regmap;
  77. priv->reg = data->offset;
  78. priv->idx = BIT(data->bit_idx);
  79. priv->hw.init = &init;
  80. ret = devm_clk_hw_register(dev, &priv->hw);
  81. if (name)
  82. kfree(init.name);
  83. if (ret)
  84. return ERR_PTR(ret);
  85. return &priv->hw;
  86. }
  87. static int ti_syscon_gate_clk_probe(struct platform_device *pdev)
  88. {
  89. const struct ti_syscon_gate_clk_data *data, *p;
  90. struct clk_hw_onecell_data *hw_data;
  91. struct device *dev = &pdev->dev;
  92. int num_clks, num_parents, i;
  93. const char *parent_name;
  94. struct regmap *regmap;
  95. data = device_get_match_data(dev);
  96. if (!data)
  97. return -EINVAL;
  98. regmap = device_node_to_regmap(dev->of_node);
  99. if (IS_ERR(regmap))
  100. return dev_err_probe(dev, PTR_ERR(regmap),
  101. "failed to get regmap\n");
  102. num_clks = 0;
  103. for (p = data; p->name; p++)
  104. num_clks++;
  105. num_parents = of_clk_get_parent_count(dev->of_node);
  106. if (of_device_is_compatible(dev->of_node, "ti,am62-audio-refclk") &&
  107. num_parents == 0) {
  108. return dev_err_probe(dev, -EINVAL,
  109. "must specify a parent clock\n");
  110. }
  111. hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, num_clks),
  112. GFP_KERNEL);
  113. if (!hw_data)
  114. return -ENOMEM;
  115. hw_data->num = num_clks;
  116. parent_name = of_clk_get_parent_name(dev->of_node, 0);
  117. for (i = 0; i < num_clks; i++) {
  118. hw_data->hws[i] = ti_syscon_gate_clk_register(dev, regmap,
  119. parent_name,
  120. &data[i]);
  121. if (IS_ERR(hw_data->hws[i]))
  122. dev_warn(dev, "failed to register %s\n",
  123. data[i].name);
  124. }
  125. if (num_clks == 1)
  126. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  127. hw_data->hws[0]);
  128. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_data);
  129. }
  130. #define TI_SYSCON_CLK_GATE(_name, _offset, _bit_idx) \
  131. { \
  132. .name = _name, \
  133. .offset = (_offset), \
  134. .bit_idx = (_bit_idx), \
  135. }
  136. static const struct ti_syscon_gate_clk_data am654_clk_data[] = {
  137. TI_SYSCON_CLK_GATE("ehrpwm_tbclk0", 0x0, 0),
  138. TI_SYSCON_CLK_GATE("ehrpwm_tbclk1", 0x4, 0),
  139. TI_SYSCON_CLK_GATE("ehrpwm_tbclk2", 0x8, 0),
  140. TI_SYSCON_CLK_GATE("ehrpwm_tbclk3", 0xc, 0),
  141. TI_SYSCON_CLK_GATE("ehrpwm_tbclk4", 0x10, 0),
  142. TI_SYSCON_CLK_GATE("ehrpwm_tbclk5", 0x14, 0),
  143. { /* Sentinel */ },
  144. };
  145. static const struct ti_syscon_gate_clk_data am64_clk_data[] = {
  146. TI_SYSCON_CLK_GATE("epwm_tbclk0", 0x0, 0),
  147. TI_SYSCON_CLK_GATE("epwm_tbclk1", 0x0, 1),
  148. TI_SYSCON_CLK_GATE("epwm_tbclk2", 0x0, 2),
  149. TI_SYSCON_CLK_GATE("epwm_tbclk3", 0x0, 3),
  150. TI_SYSCON_CLK_GATE("epwm_tbclk4", 0x0, 4),
  151. TI_SYSCON_CLK_GATE("epwm_tbclk5", 0x0, 5),
  152. TI_SYSCON_CLK_GATE("epwm_tbclk6", 0x0, 6),
  153. TI_SYSCON_CLK_GATE("epwm_tbclk7", 0x0, 7),
  154. TI_SYSCON_CLK_GATE("epwm_tbclk8", 0x0, 8),
  155. { /* Sentinel */ },
  156. };
  157. static const struct ti_syscon_gate_clk_data am62_clk_data[] = {
  158. TI_SYSCON_CLK_GATE("epwm_tbclk0", 0x0, 0),
  159. TI_SYSCON_CLK_GATE("epwm_tbclk1", 0x0, 1),
  160. TI_SYSCON_CLK_GATE("epwm_tbclk2", 0x0, 2),
  161. { /* Sentinel */ },
  162. };
  163. static const struct ti_syscon_gate_clk_data am62_audio_clk_data[] = {
  164. TI_SYSCON_CLK_GATE("audio_refclk", 0x0, 15),
  165. { /* Sentinel */ },
  166. };
  167. static const struct of_device_id ti_syscon_gate_clk_ids[] = {
  168. {
  169. .compatible = "ti,am654-ehrpwm-tbclk",
  170. .data = &am654_clk_data,
  171. },
  172. {
  173. .compatible = "ti,am64-epwm-tbclk",
  174. .data = &am64_clk_data,
  175. },
  176. {
  177. .compatible = "ti,am62-epwm-tbclk",
  178. .data = &am62_clk_data,
  179. },
  180. {
  181. .compatible = "ti,am62-audio-refclk",
  182. .data = &am62_audio_clk_data,
  183. },
  184. { }
  185. };
  186. MODULE_DEVICE_TABLE(of, ti_syscon_gate_clk_ids);
  187. static struct platform_driver ti_syscon_gate_clk_driver = {
  188. .probe = ti_syscon_gate_clk_probe,
  189. .driver = {
  190. .name = "ti-syscon-gate-clk",
  191. .of_match_table = ti_syscon_gate_clk_ids,
  192. },
  193. };
  194. module_platform_driver(ti_syscon_gate_clk_driver);
  195. MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");
  196. MODULE_DESCRIPTION("Syscon backed gate-clock driver");
  197. MODULE_LICENSE("GPL");