tegra_pwm.c 2.0 KB

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