sandbox_pwm.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. struct sandbox_pwm_chan {
  15. uint period_ns;
  16. uint duty_ns;
  17. bool enable;
  18. bool polarity;
  19. };
  20. struct sandbox_pwm_priv {
  21. struct sandbox_pwm_chan chan[NUM_CHANNELS];
  22. };
  23. static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
  24. uint period_ns, uint duty_ns)
  25. {
  26. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  27. struct sandbox_pwm_chan *chan;
  28. if (channel >= NUM_CHANNELS)
  29. return -ENOSPC;
  30. chan = &priv->chan[channel];
  31. chan->period_ns = period_ns;
  32. chan->duty_ns = duty_ns;
  33. return 0;
  34. }
  35. static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
  36. bool enable)
  37. {
  38. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  39. struct sandbox_pwm_chan *chan;
  40. if (channel >= NUM_CHANNELS)
  41. return -ENOSPC;
  42. chan = &priv->chan[channel];
  43. chan->enable = enable;
  44. return 0;
  45. }
  46. static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
  47. bool polarity)
  48. {
  49. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  50. struct sandbox_pwm_chan *chan;
  51. if (channel >= NUM_CHANNELS)
  52. return -ENOSPC;
  53. chan = &priv->chan[channel];
  54. chan->polarity = polarity;
  55. return 0;
  56. }
  57. static const struct pwm_ops sandbox_pwm_ops = {
  58. .set_config = sandbox_pwm_set_config,
  59. .set_enable = sandbox_pwm_set_enable,
  60. .set_invert = sandbox_pwm_set_invert,
  61. };
  62. static const struct udevice_id sandbox_pwm_ids[] = {
  63. { .compatible = "sandbox,pwm" },
  64. { }
  65. };
  66. U_BOOT_DRIVER(warm_pwm_sandbox) = {
  67. .name = "pwm_sandbox",
  68. .id = UCLASS_PWM,
  69. .of_match = sandbox_pwm_ids,
  70. .ops = &sandbox_pwm_ops,
  71. .priv_auto_alloc_size = sizeof(struct sandbox_pwm_priv),
  72. };