syscon-clk.c 4.9 KB

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