blob: 4b50b19c618d0318b5b494ef42a84e34afc79cd0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse62f4be2017-04-16 21:01:11 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasse62f4be2017-04-16 21:01:11 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <pwm.h>
11#include <asm/test.h>
12
Simon Glasse62f4be2017-04-16 21:01:11 -060013enum {
14 NUM_CHANNELS = 3,
15};
16
17struct sandbox_pwm_chan {
18 uint period_ns;
19 uint duty_ns;
20 bool enable;
Kever Yang809c3e62017-04-24 10:27:52 +080021 bool polarity;
Simon Glasse62f4be2017-04-16 21:01:11 -060022};
23
24struct sandbox_pwm_priv {
25 struct sandbox_pwm_chan chan[NUM_CHANNELS];
26};
27
28static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
29 uint period_ns, uint duty_ns)
30{
31 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
32 struct sandbox_pwm_chan *chan;
33
34 if (channel >= NUM_CHANNELS)
35 return -ENOSPC;
36 chan = &priv->chan[channel];
37 chan->period_ns = period_ns;
38 chan->duty_ns = duty_ns;
39
40 return 0;
41}
42
43static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
44 bool enable)
45{
46 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
47 struct sandbox_pwm_chan *chan;
48
49 if (channel >= NUM_CHANNELS)
50 return -ENOSPC;
51 chan = &priv->chan[channel];
52 chan->enable = enable;
53
54 return 0;
55}
56
Kever Yang809c3e62017-04-24 10:27:52 +080057static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
58 bool polarity)
59{
60 struct sandbox_pwm_priv *priv = dev_get_priv(dev);
61 struct sandbox_pwm_chan *chan;
62
63 if (channel >= NUM_CHANNELS)
64 return -ENOSPC;
65 chan = &priv->chan[channel];
66 chan->polarity = polarity;
67
68 return 0;
69}
70
Simon Glasse62f4be2017-04-16 21:01:11 -060071static const struct pwm_ops sandbox_pwm_ops = {
72 .set_config = sandbox_pwm_set_config,
73 .set_enable = sandbox_pwm_set_enable,
Kever Yang809c3e62017-04-24 10:27:52 +080074 .set_invert = sandbox_pwm_set_invert,
Simon Glasse62f4be2017-04-16 21:01:11 -060075};
76
77static const struct udevice_id sandbox_pwm_ids[] = {
78 { .compatible = "sandbox,pwm" },
79 { }
80};
81
82U_BOOT_DRIVER(warm_pwm_sandbox) = {
83 .name = "pwm_sandbox",
84 .id = UCLASS_PWM,
85 .of_match = sandbox_pwm_ids,
86 .ops = &sandbox_pwm_ops,
87 .priv_auto_alloc_size = sizeof(struct sandbox_pwm_priv),
88};