xref: /openbmc/u-boot/drivers/pwm/sandbox_pwm.c (revision 3ebd892f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <pwm.h>
11 #include <asm/test.h>
12 
13 enum {
14 	NUM_CHANNELS	= 3,
15 };
16 
17 struct sandbox_pwm_chan {
18 	uint period_ns;
19 	uint duty_ns;
20 	bool enable;
21 	bool polarity;
22 };
23 
24 struct sandbox_pwm_priv {
25 	struct sandbox_pwm_chan chan[NUM_CHANNELS];
26 };
27 
28 static 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 
43 static 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 
57 static 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 
71 static const struct pwm_ops sandbox_pwm_ops = {
72 	.set_config	= sandbox_pwm_set_config,
73 	.set_enable	= sandbox_pwm_set_enable,
74 	.set_invert	= sandbox_pwm_set_invert,
75 };
76 
77 static const struct udevice_id sandbox_pwm_ids[] = {
78 	{ .compatible = "sandbox,pwm" },
79 	{ }
80 };
81 
82 U_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 };
89