xref: /openbmc/u-boot/drivers/pwm/sandbox_pwm.c (revision 699e831e158a5846778d8bd6af054d4276277cb6)
1 /*
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 
14 enum {
15 	NUM_CHANNELS	= 3,
16 };
17 
18 struct sandbox_pwm_chan {
19 	uint period_ns;
20 	uint duty_ns;
21 	bool enable;
22 	bool polarity;
23 };
24 
25 struct sandbox_pwm_priv {
26 	struct sandbox_pwm_chan chan[NUM_CHANNELS];
27 };
28 
29 static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
30 				  uint period_ns, uint duty_ns)
31 {
32 	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
33 	struct sandbox_pwm_chan *chan;
34 
35 	if (channel >= NUM_CHANNELS)
36 		return -ENOSPC;
37 	chan = &priv->chan[channel];
38 	chan->period_ns = period_ns;
39 	chan->duty_ns = duty_ns;
40 
41 	return 0;
42 }
43 
44 static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
45 				  bool enable)
46 {
47 	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
48 	struct sandbox_pwm_chan *chan;
49 
50 	if (channel >= NUM_CHANNELS)
51 		return -ENOSPC;
52 	chan = &priv->chan[channel];
53 	chan->enable = enable;
54 
55 	return 0;
56 }
57 
58 static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
59 				  bool polarity)
60 {
61 	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
62 	struct sandbox_pwm_chan *chan;
63 
64 	if (channel >= NUM_CHANNELS)
65 		return -ENOSPC;
66 	chan = &priv->chan[channel];
67 	chan->polarity = polarity;
68 
69 	return 0;
70 }
71 
72 static const struct pwm_ops sandbox_pwm_ops = {
73 	.set_config	= sandbox_pwm_set_config,
74 	.set_enable	= sandbox_pwm_set_enable,
75 	.set_invert	= sandbox_pwm_set_invert,
76 };
77 
78 static const struct udevice_id sandbox_pwm_ids[] = {
79 	{ .compatible = "sandbox,pwm" },
80 	{ }
81 };
82 
83 U_BOOT_DRIVER(warm_pwm_sandbox) = {
84 	.name		= "pwm_sandbox",
85 	.id		= UCLASS_PWM,
86 	.of_match	= sandbox_pwm_ids,
87 	.ops		= &sandbox_pwm_ops,
88 	.priv_auto_alloc_size	= sizeof(struct sandbox_pwm_priv),
89 };
90