summaryrefslogtreecommitdiff
path: root/drivers/pwm/sandbox_pwm.c
blob: 28988187e039452e51f91dff0f8818bd6778ce4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2015 Google, Inc
 * Written by Simon Glass <sjg@chromium.org>
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <pwm.h>
#include <asm/test.h>

enum {
	NUM_CHANNELS	= 3,
};

/**
 * struct sandbox_pwm_chan - a sandbox PWM channel
 *
 * @period_ns: Period of the PWM in nanoseconds
 * @duty_ns: Current duty cycle of the PWM in nanoseconds
 * @enable: true if the PWM is enabled
 * @polarity: true if the PWM polarity is active high
 */
struct sandbox_pwm_chan {
	uint period_ns;
	uint duty_ns;
	bool enable;
	bool polarity;
};

struct sandbox_pwm_priv {
	struct sandbox_pwm_chan chan[NUM_CHANNELS];
};

int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp,
			   uint *duty_nsp, bool *enablep, bool *polarityp)
{
	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
	struct sandbox_pwm_chan *chan;

	if (channel >= NUM_CHANNELS)
		return -ENOSPC;
	chan = &priv->chan[channel];
	*period_nsp = chan->period_ns;
	*duty_nsp = chan->duty_ns;
	*enablep = chan->enable;
	*polarityp = chan->polarity;

	return 0;
}

static int sandbox_pwm_set_config(struct udevice *dev, uint channel,
				  uint period_ns, uint duty_ns)
{
	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
	struct sandbox_pwm_chan *chan;

	if (channel >= NUM_CHANNELS)
		return -ENOSPC;
	chan = &priv->chan[channel];
	chan->period_ns = period_ns;
	chan->duty_ns = duty_ns;

	return 0;
}

static int sandbox_pwm_set_enable(struct udevice *dev, uint channel,
				  bool enable)
{
	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
	struct sandbox_pwm_chan *chan;

	if (channel >= NUM_CHANNELS)
		return -ENOSPC;
	chan = &priv->chan[channel];
	chan->enable = enable;

	return 0;
}

static int sandbox_pwm_set_invert(struct udevice *dev, uint channel,
				  bool polarity)
{
	struct sandbox_pwm_priv *priv = dev_get_priv(dev);
	struct sandbox_pwm_chan *chan;

	if (channel >= NUM_CHANNELS)
		return -ENOSPC;
	chan = &priv->chan[channel];
	chan->polarity = polarity;

	return 0;
}

static const struct pwm_ops sandbox_pwm_ops = {
	.set_config	= sandbox_pwm_set_config,
	.set_enable	= sandbox_pwm_set_enable,
	.set_invert	= sandbox_pwm_set_invert,
};

static const struct udevice_id sandbox_pwm_ids[] = {
	{ .compatible = "sandbox,pwm" },
	{ }
};

U_BOOT_DRIVER(warm_pwm_sandbox) = {
	.name		= "pwm_sandbox",
	.id		= UCLASS_PWM,
	.of_match	= sandbox_pwm_ids,
	.ops		= &sandbox_pwm_ops,
	.priv_auto_alloc_size	= sizeof(struct sandbox_pwm_priv),
};