sandbox_pwm.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <pwm.h>
  10. #include <asm/test.h>
  11. enum {
  12. NUM_CHANNELS = 3,
  13. };
  14. /**
  15. * struct sandbox_pwm_chan - a sandbox PWM channel
  16. *
  17. * @period_ns: Period of the PWM in nanoseconds
  18. * @duty_ns: Current duty cycle of the PWM in nanoseconds
  19. * @enable: true if the PWM is enabled
  20. * @polarity: true if the PWM polarity is active high
  21. */
  22. struct sandbox_pwm_chan {
  23. uint period_ns;
  24. uint duty_ns;
  25. bool enable;
  26. bool polarity;
  27. };
  28. struct sandbox_pwm_priv {
  29. struct sandbox_pwm_chan chan[NUM_CHANNELS];
  30. };
  31. int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp,
  32. uint *duty_nsp, bool *enablep, bool *polarityp)
  33. {
  34. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  35. struct sandbox_pwm_chan *chan;
  36. if (channel >= NUM_CHANNELS)
  37. return -ENOSPC;
  38. chan = &priv->chan[channel];
  39. *period_nsp = chan->period_ns;
  40. *duty_nsp = chan->duty_ns;
  41. *enablep = chan->enable;
  42. *polarityp = chan->polarity;
  43. return 0;
  44. }
  45. static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
  46. uint period_ns, uint duty_ns)
  47. {
  48. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  49. struct sandbox_pwm_chan *chan;
  50. if (channel >= NUM_CHANNELS)
  51. return -ENOSPC;
  52. chan = &priv->chan[channel];
  53. chan->period_ns = period_ns;
  54. chan->duty_ns = duty_ns;
  55. return 0;
  56. }
  57. static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
  58. bool enable)
  59. {
  60. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  61. struct sandbox_pwm_chan *chan;
  62. if (channel >= NUM_CHANNELS)
  63. return -ENOSPC;
  64. chan = &priv->chan[channel];
  65. chan->enable = enable;
  66. return 0;
  67. }
  68. static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
  69. bool polarity)
  70. {
  71. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  72. struct sandbox_pwm_chan *chan;
  73. if (channel >= NUM_CHANNELS)
  74. return -ENOSPC;
  75. chan = &priv->chan[channel];
  76. chan->polarity = polarity;
  77. return 0;
  78. }
  79. static const struct pwm_ops sandbox_pwm_ops = {
  80. .set_config = sandbox_pwm_set_config,
  81. .set_enable = sandbox_pwm_set_enable,
  82. .set_invert = sandbox_pwm_set_invert,
  83. };
  84. static const struct udevice_id sandbox_pwm_ids[] = {
  85. { .compatible = "sandbox,pwm" },
  86. { }
  87. };
  88. U_BOOT_DRIVER(warm_pwm_sandbox) = {
  89. .name = "pwm_sandbox",
  90. .id = UCLASS_PWM,
  91. .of_match = sandbox_pwm_ids,
  92. .ops = &sandbox_pwm_ops,
  93. .priv_auto_alloc_size = sizeof(struct sandbox_pwm_priv),
  94. };