xref: /openbmc/u-boot/drivers/pwm/sandbox_pwm.c (revision c04b9b34)
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 };
25 
26 struct sandbox_pwm_priv {
27 	struct sandbox_pwm_chan chan[NUM_CHANNELS];
28 };
29 
30 static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
31 				  uint period_ns, uint duty_ns)
32 {
33 	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
34 	struct sandbox_pwm_chan *chan;
35 
36 	if (channel >= NUM_CHANNELS)
37 		return -ENOSPC;
38 	chan = &priv->chan[channel];
39 	chan->period_ns = period_ns;
40 	chan->duty_ns = duty_ns;
41 
42 	return 0;
43 }
44 
45 static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
46 				  bool enable)
47 {
48 	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
49 	struct sandbox_pwm_chan *chan;
50 
51 	if (channel >= NUM_CHANNELS)
52 		return -ENOSPC;
53 	chan = &priv->chan[channel];
54 	chan->enable = enable;
55 
56 	return 0;
57 }
58 
59 static const struct pwm_ops sandbox_pwm_ops = {
60 	.set_config	= sandbox_pwm_set_config,
61 	.set_enable	= sandbox_pwm_set_enable,
62 };
63 
64 static const struct udevice_id sandbox_pwm_ids[] = {
65 	{ .compatible = "sandbox,pwm" },
66 	{ }
67 };
68 
69 U_BOOT_DRIVER(warm_pwm_sandbox) = {
70 	.name		= "pwm_sandbox",
71 	.id		= UCLASS_PWM,
72 	.of_match	= sandbox_pwm_ids,
73 	.ops		= &sandbox_pwm_ops,
74 	.priv_auto_alloc_size	= sizeof(struct sandbox_pwm_priv),
75 };
76