ark_pwm.c 834 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Basic support for the pwm module on Arkmicro.
  4. */
  5. #include <common.h>
  6. #include <div64.h>
  7. #include <pwm.h>
  8. #include <asm/io.h>
  9. #define PWM_EN 0x0
  10. #define PWM_DUTY 0x4
  11. #define PWM_CNTR 0x8
  12. #define PWM_REG(x) (CONFIG_PWM_BASEADDR + 0x10 * (x))
  13. int pwm_config(int pwm_id, int duty_ns, int period_ns)
  14. {
  15. unsigned int clk_mhz = CONFIG_PWM_CLKFREQ / 1000000;
  16. unsigned int duty = (unsigned long long)duty_ns * clk_mhz / 1000;
  17. unsigned int period = (unsigned long long)period_ns * clk_mhz / 1000;
  18. writel(0, PWM_REG(pwm_id) + PWM_EN);
  19. writel(duty, PWM_REG(pwm_id) + PWM_DUTY);
  20. writel(period, PWM_REG(pwm_id) + PWM_CNTR);
  21. return 0;
  22. }
  23. int pwm_enable(int pwm_id)
  24. {
  25. writel(1, PWM_REG(pwm_id) + PWM_EN);
  26. return 0;
  27. }
  28. void pwm_disable(int pwm_id)
  29. {
  30. writel(0, PWM_REG(pwm_id) + PWM_EN);
  31. }