pwm-cadence-ttc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2021 Xilinx, Inc. Michal Simek
  4. */
  5. #define LOG_CATEGORY UCLASS_PWM
  6. #include <clk.h>
  7. #include <common.h>
  8. #include <div64.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <pwm.h>
  12. #include <asm/io.h>
  13. #include <log.h>
  14. #include <div64.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/math64.h>
  17. #include <linux/log2.h>
  18. #include <dm/device_compat.h>
  19. #define CLOCK_CONTROL 0
  20. #define COUNTER_CONTROL 0xc
  21. #define INTERVAL_COUNTER 0x24
  22. #define MATCH_1_COUNTER 0x30
  23. #define CLK_FALLING_EDGE BIT(6)
  24. #define CLK_SRC_EXTERNAL BIT(5)
  25. #define CLK_PRESCALE_MASK GENMASK(4, 1)
  26. #define CLK_PRESCALE_ENABLE BIT(0)
  27. #define COUNTER_WAVE_POL BIT(6)
  28. #define COUNTER_WAVE_DISABLE BIT(5)
  29. #define COUNTER_RESET BIT(4)
  30. #define COUNTER_MATCH_ENABLE BIT(3)
  31. #define COUNTER_DECREMENT_ENABLE BIT(2)
  32. #define COUNTER_INTERVAL_ENABLE BIT(1)
  33. #define COUNTER_COUNTING_DISABLE BIT(0)
  34. #define NSEC_PER_SEC 1000000000L
  35. #define TTC_REG(reg, channel) ((reg) + (channel) * sizeof(u32))
  36. #define TTC_CLOCK_CONTROL(reg, channel) \
  37. TTC_REG((reg) + CLOCK_CONTROL, (channel))
  38. #define TTC_COUNTER_CONTROL(reg, channel) \
  39. TTC_REG((reg) + COUNTER_CONTROL, (channel))
  40. #define TTC_INTERVAL_COUNTER(reg, channel) \
  41. TTC_REG((reg) + INTERVAL_COUNTER, (channel))
  42. #define TTC_MATCH_1_COUNTER(reg, channel) \
  43. TTC_REG((reg) + MATCH_1_COUNTER, (channel))
  44. struct cadence_ttc_pwm_plat {
  45. u8 *regs;
  46. u32 timer_width;
  47. };
  48. struct cadence_ttc_pwm_priv {
  49. u8 *regs;
  50. u32 timer_width;
  51. u32 timer_mask;
  52. unsigned long frequency;
  53. bool invert[2];
  54. };
  55. static int cadence_ttc_pwm_set_invert(struct udevice *dev, uint channel,
  56. bool polarity)
  57. {
  58. struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
  59. if (channel > 2) {
  60. dev_err(dev, "Unsupported channel number %d(max 2)\n", channel);
  61. return -EINVAL;
  62. }
  63. priv->invert[channel] = polarity;
  64. dev_dbg(dev, "polarity=%u. Please config PWM again\n", polarity);
  65. return 0;
  66. }
  67. static int cadence_ttc_pwm_set_config(struct udevice *dev, uint channel,
  68. uint period_ns, uint duty_ns)
  69. {
  70. struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
  71. u32 counter_ctrl, clock_ctrl;
  72. int period_clocks, duty_clocks, prescaler;
  73. dev_dbg(dev, "channel %d, duty %d/period %d ns\n", channel,
  74. duty_ns, period_ns);
  75. if (channel > 2) {
  76. dev_err(dev, "Unsupported channel number %d(max 2)\n", channel);
  77. return -EINVAL;
  78. }
  79. /* Make sure counter is stopped */
  80. counter_ctrl = readl(TTC_COUNTER_CONTROL(priv->regs, channel));
  81. setbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
  82. COUNTER_COUNTING_DISABLE | COUNTER_WAVE_DISABLE);
  83. /* Calculate period, prescaler and set clock control register */
  84. period_clocks = div64_u64(((int64_t)period_ns * priv->frequency),
  85. NSEC_PER_SEC);
  86. prescaler = ilog2(period_clocks) + 1 - priv->timer_width;
  87. if (prescaler < 0)
  88. prescaler = 0;
  89. clock_ctrl = readl(TTC_CLOCK_CONTROL(priv->regs, channel));
  90. if (!prescaler) {
  91. clock_ctrl &= ~(CLK_PRESCALE_ENABLE | CLK_PRESCALE_MASK);
  92. } else {
  93. clock_ctrl &= ~CLK_PRESCALE_MASK;
  94. clock_ctrl |= CLK_PRESCALE_ENABLE;
  95. clock_ctrl |= FIELD_PREP(CLK_PRESCALE_MASK, prescaler - 1);
  96. };
  97. /* External source is not handled by this driver now */
  98. clock_ctrl &= ~CLK_SRC_EXTERNAL;
  99. writel(clock_ctrl, TTC_CLOCK_CONTROL(priv->regs, channel));
  100. /* Calculate interval and set counter control value */
  101. duty_clocks = div64_u64(((int64_t)duty_ns * priv->frequency),
  102. NSEC_PER_SEC);
  103. writel((period_clocks >> prescaler) & priv->timer_mask,
  104. TTC_INTERVAL_COUNTER(priv->regs, channel));
  105. writel((duty_clocks >> prescaler) & priv->timer_mask,
  106. TTC_MATCH_1_COUNTER(priv->regs, channel));
  107. /* Restore/reset counter */
  108. counter_ctrl &= ~COUNTER_DECREMENT_ENABLE;
  109. counter_ctrl |= COUNTER_INTERVAL_ENABLE |
  110. COUNTER_RESET |
  111. COUNTER_MATCH_ENABLE;
  112. if (priv->invert[channel])
  113. counter_ctrl |= COUNTER_WAVE_POL;
  114. else
  115. counter_ctrl &= ~COUNTER_WAVE_POL;
  116. writel(counter_ctrl, TTC_COUNTER_CONTROL(priv->regs, channel));
  117. dev_dbg(dev, "%d/%d clocks, prescaler 2^%d\n", duty_clocks,
  118. period_clocks, prescaler);
  119. return 0;
  120. };
  121. static int cadence_ttc_pwm_set_enable(struct udevice *dev, uint channel,
  122. bool enable)
  123. {
  124. struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
  125. if (channel > 2) {
  126. dev_err(dev, "Unsupported channel number %d(max 2)\n", channel);
  127. return -EINVAL;
  128. }
  129. dev_dbg(dev, "Enable: %d, channel %d\n", enable, channel);
  130. if (enable) {
  131. clrbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
  132. COUNTER_COUNTING_DISABLE |
  133. COUNTER_WAVE_DISABLE);
  134. setbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
  135. COUNTER_RESET);
  136. } else {
  137. setbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
  138. COUNTER_COUNTING_DISABLE |
  139. COUNTER_WAVE_DISABLE);
  140. }
  141. return 0;
  142. };
  143. static int cadence_ttc_pwm_probe(struct udevice *dev)
  144. {
  145. struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
  146. struct cadence_ttc_pwm_plat *plat = dev_get_plat(dev);
  147. struct clk clk;
  148. int ret;
  149. priv->regs = plat->regs;
  150. priv->timer_width = plat->timer_width;
  151. priv->timer_mask = GENMASK(priv->timer_width - 1, 0);
  152. ret = clk_get_by_index(dev, 0, &clk);
  153. if (ret < 0) {
  154. dev_err(dev, "failed to get clock\n");
  155. return ret;
  156. }
  157. priv->frequency = clk_get_rate(&clk);
  158. if (IS_ERR_VALUE(priv->frequency)) {
  159. dev_err(dev, "failed to get rate\n");
  160. return priv->frequency;
  161. }
  162. dev_dbg(dev, "Clk frequency: %ld\n", priv->frequency);
  163. ret = clk_enable(&clk);
  164. if (ret) {
  165. dev_err(dev, "failed to enable clock\n");
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static int cadence_ttc_pwm_of_to_plat(struct udevice *dev)
  171. {
  172. struct cadence_ttc_pwm_plat *plat = dev_get_plat(dev);
  173. const char *cells;
  174. cells = dev_read_prop(dev, "#pwm-cells", NULL);
  175. if (!cells)
  176. return -EINVAL;
  177. plat->regs = dev_read_addr_ptr(dev);
  178. plat->timer_width = dev_read_u32_default(dev, "timer-width", 16);
  179. return 0;
  180. }
  181. static int cadence_ttc_pwm_bind(struct udevice *dev)
  182. {
  183. const char *cells;
  184. cells = dev_read_prop(dev, "#pwm-cells", NULL);
  185. if (!cells)
  186. return -ENODEV;
  187. return 0;
  188. }
  189. static const struct pwm_ops cadence_ttc_pwm_ops = {
  190. .set_invert = cadence_ttc_pwm_set_invert,
  191. .set_config = cadence_ttc_pwm_set_config,
  192. .set_enable = cadence_ttc_pwm_set_enable,
  193. };
  194. static const struct udevice_id cadence_ttc_pwm_ids[] = {
  195. { .compatible = "cdns,ttc" },
  196. { }
  197. };
  198. U_BOOT_DRIVER(cadence_ttc_pwm) = {
  199. .name = "cadence_ttc_pwm",
  200. .id = UCLASS_PWM,
  201. .of_match = cadence_ttc_pwm_ids,
  202. .ops = &cadence_ttc_pwm_ops,
  203. .bind = cadence_ttc_pwm_bind,
  204. .of_to_plat = cadence_ttc_pwm_of_to_plat,
  205. .probe = cadence_ttc_pwm_probe,
  206. .priv_auto = sizeof(struct cadence_ttc_pwm_priv),
  207. .plat_auto = sizeof(struct cadence_ttc_pwm_plat),
  208. };