xref: /openbmc/u-boot/drivers/pwm/sandbox_pwm.c (revision ebce73f0)
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 DECLARE_GLOBAL_DATA_PTR;
15 
16 enum {
17 	NUM_CHANNELS	= 3,
18 };
19 
20 struct sandbox_pwm_chan {
21 	uint period_ns;
22 	uint duty_ns;
23 	bool enable;
24 	bool polarity;
25 };
26 
27 struct sandbox_pwm_priv {
28 	struct sandbox_pwm_chan chan[NUM_CHANNELS];
29 };
30 
31 static 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 
46 static 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 
60 static 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 
74 static const struct pwm_ops sandbox_pwm_ops = {
75 	.set_config	= sandbox_pwm_set_config,
76 	.set_enable	= sandbox_pwm_set_enable,
77 	.set_invert	= sandbox_pwm_set_invert,
78 };
79 
80 static const struct udevice_id sandbox_pwm_ids[] = {
81 	{ .compatible = "sandbox,pwm" },
82 	{ }
83 };
84 
85 U_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 };
92