1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
18 * struct sandbox_pwm_chan - a sandbox PWM channel
20 * @period_ns: Period of the PWM in nanoseconds
21 * @duty_ns: Current duty cycle of the PWM in nanoseconds
22 * @enable: true if the PWM is enabled
23 * @polarity: true if the PWM polarity is active high
25 struct sandbox_pwm_chan {
32 struct sandbox_pwm_priv {
33 struct sandbox_pwm_chan chan[NUM_CHANNELS];
36 int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp,
37 uint *duty_nsp, bool *enablep, bool *polarityp)
39 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
40 struct sandbox_pwm_chan *chan;
42 if (channel >= NUM_CHANNELS)
44 chan = &priv->chan[channel];
45 *period_nsp = chan->period_ns;
46 *duty_nsp = chan->duty_ns;
47 *enablep = chan->enable;
48 *polarityp = chan->polarity;
53 static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
54 uint period_ns, uint duty_ns)
56 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
57 struct sandbox_pwm_chan *chan;
59 if (channel >= NUM_CHANNELS)
61 chan = &priv->chan[channel];
62 chan->period_ns = period_ns;
63 chan->duty_ns = duty_ns;
68 static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
71 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
72 struct sandbox_pwm_chan *chan;
74 if (channel >= NUM_CHANNELS)
76 chan = &priv->chan[channel];
77 chan->enable = enable;
82 static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
85 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
86 struct sandbox_pwm_chan *chan;
88 if (channel >= NUM_CHANNELS)
90 chan = &priv->chan[channel];
91 chan->polarity = polarity;
96 static const struct pwm_ops sandbox_pwm_ops = {
97 .set_config = sandbox_pwm_set_config,
98 .set_enable = sandbox_pwm_set_enable,
99 .set_invert = sandbox_pwm_set_invert,
102 static const struct udevice_id sandbox_pwm_ids[] = {
103 { .compatible = "sandbox,pwm" },
107 U_BOOT_DRIVER(warm_pwm_sandbox) = {
108 .name = "pwm_sandbox",
110 .of_match = sandbox_pwm_ids,
111 .ops = &sandbox_pwm_ops,
112 .priv_auto_alloc_size = sizeof(struct sandbox_pwm_priv),