tegra_pwm.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016 Google Inc.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <pwm.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/clock.h>
  10. #include <asm/arch/pwm.h>
  11. struct tegra_pwm_priv {
  12. struct pwm_ctlr *regs;
  13. };
  14. static int tegra_pwm_set_config(struct udevice *dev, uint channel,
  15. uint period_ns, uint duty_ns)
  16. {
  17. struct tegra_pwm_priv *priv = dev_get_priv(dev);
  18. struct pwm_ctlr *regs = priv->regs;
  19. uint pulse_width;
  20. u32 reg;
  21. if (channel >= 4)
  22. return -EINVAL;
  23. debug("%s: Configure '%s' channel %u\n", __func__, dev->name, channel);
  24. /* We ignore the period here and just use 32KHz */
  25. clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ, 32768);
  26. pulse_width = duty_ns * 255 / period_ns;
  27. reg = pulse_width << PWM_WIDTH_SHIFT;
  28. reg |= 1 << PWM_DIVIDER_SHIFT;
  29. writel(reg, &regs[channel].control);
  30. debug("%s: pulse_width=%u\n", __func__, pulse_width);
  31. return 0;
  32. }
  33. static int tegra_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
  34. {
  35. struct tegra_pwm_priv *priv = dev_get_priv(dev);
  36. struct pwm_ctlr *regs = priv->regs;
  37. if (channel >= 4)
  38. return -EINVAL;
  39. debug("%s: Enable '%s' channel %u\n", __func__, dev->name, channel);
  40. clrsetbits_le32(&regs[channel].control, PWM_ENABLE_MASK,
  41. enable ? PWM_ENABLE_MASK : 0);
  42. return 0;
  43. }
  44. static int tegra_pwm_ofdata_to_platdata(struct udevice *dev)
  45. {
  46. struct tegra_pwm_priv *priv = dev_get_priv(dev);
  47. priv->regs = (struct pwm_ctlr *)dev_read_addr(dev);
  48. return 0;
  49. }
  50. static const struct pwm_ops tegra_pwm_ops = {
  51. .set_config = tegra_pwm_set_config,
  52. .set_enable = tegra_pwm_set_enable,
  53. };
  54. static const struct udevice_id tegra_pwm_ids[] = {
  55. { .compatible = "nvidia,tegra124-pwm" },
  56. { .compatible = "nvidia,tegra20-pwm" },
  57. { }
  58. };
  59. U_BOOT_DRIVER(tegra_pwm) = {
  60. .name = "tegra_pwm",
  61. .id = UCLASS_PWM,
  62. .of_match = tegra_pwm_ids,
  63. .ops = &tegra_pwm_ops,
  64. .ofdata_to_platdata = tegra_pwm_ofdata_to_platdata,
  65. .priv_auto_alloc_size = sizeof(struct tegra_pwm_priv),
  66. };