sandbox_pwm.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <pwm.h>
  11. #include <asm/test.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. enum {
  14. NUM_CHANNELS = 3,
  15. };
  16. struct sandbox_pwm_chan {
  17. uint period_ns;
  18. uint duty_ns;
  19. bool enable;
  20. bool polarity;
  21. };
  22. struct sandbox_pwm_priv {
  23. struct sandbox_pwm_chan chan[NUM_CHANNELS];
  24. };
  25. static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
  26. uint period_ns, uint duty_ns)
  27. {
  28. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  29. struct sandbox_pwm_chan *chan;
  30. if (channel >= NUM_CHANNELS)
  31. return -ENOSPC;
  32. chan = &priv->chan[channel];
  33. chan->period_ns = period_ns;
  34. chan->duty_ns = duty_ns;
  35. return 0;
  36. }
  37. static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
  38. bool enable)
  39. {
  40. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  41. struct sandbox_pwm_chan *chan;
  42. if (channel >= NUM_CHANNELS)
  43. return -ENOSPC;
  44. chan = &priv->chan[channel];
  45. chan->enable = enable;
  46. return 0;
  47. }
  48. static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
  49. bool polarity)
  50. {
  51. struct sandbox_pwm_priv *priv = dev_get_priv(dev);
  52. struct sandbox_pwm_chan *chan;
  53. if (channel >= NUM_CHANNELS)
  54. return -ENOSPC;
  55. chan = &priv->chan[channel];
  56. chan->polarity = polarity;
  57. return 0;
  58. }
  59. static const struct pwm_ops sandbox_pwm_ops = {
  60. .set_config = sandbox_pwm_set_config,
  61. .set_enable = sandbox_pwm_set_enable,
  62. .set_invert = sandbox_pwm_set_invert,
  63. };
  64. static const struct udevice_id sandbox_pwm_ids[] = {
  65. { .compatible = "sandbox,pwm" },
  66. { }
  67. };
  68. U_BOOT_DRIVER(warm_pwm_sandbox) = {
  69. .name = "pwm_sandbox",
  70. .id = UCLASS_PWM,
  71. .of_match = sandbox_pwm_ids,
  72. .ops = &sandbox_pwm_ops,
  73. .priv_auto_alloc_size = sizeof(struct sandbox_pwm_priv),
  74. };