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];
64 /* Pretend to have some fixed period */
65 chan->period_ns = 4096;
66 chan->duty_ns = duty_ns * 4096 / period_ns;
68 chan->period_ns = period_ns;
69 chan->duty_ns = duty_ns;
75 static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
78 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
79 struct sandbox_pwm_chan *chan;
81 if (channel >= NUM_CHANNELS)
83 chan = &priv->chan[channel];
84 chan->enable = enable;
89 static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
92 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
93 struct sandbox_pwm_chan *chan;
95 if (channel >= NUM_CHANNELS)
97 chan = &priv->chan[channel];
98 chan->polarity = polarity;
103 static const struct pwm_ops sandbox_pwm_ops = {
104 .set_config = sandbox_pwm_set_config,
105 .set_enable = sandbox_pwm_set_enable,
106 .set_invert = sandbox_pwm_set_invert,
109 static const struct udevice_id sandbox_pwm_ids[] = {
110 { .compatible = "sandbox,pwm" },
114 U_BOOT_DRIVER(warm_pwm_sandbox) = {
115 .name = "pwm_sandbox",
117 .of_match = sandbox_pwm_ids,
118 .ops = &sandbox_pwm_ops,
119 .priv_auto = sizeof(struct sandbox_pwm_priv),