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