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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* $OpenBSD: pwmreg.c,v 1.2 2021/10/24 17:52:26 mpi Exp $ */
/*
* Copyright (c) 2019 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/ofw_regulator.h>
#include <dev/ofw/fdt.h>
struct pwmreg_softc {
struct device sc_dev;
uint32_t *sc_pwm;
uint32_t sc_dutycycle_unit;
uint32_t sc_dutycycle_range[2];
struct regulator_device sc_rd;
};
int pwmreg_match(struct device *, void *, void *);
void pwmreg_attach(struct device *, struct device *, void *);
const struct cfattach pwmreg_ca = {
sizeof (struct pwmreg_softc), pwmreg_match, pwmreg_attach
};
struct cfdriver pwmreg_cd = {
NULL, "pwmreg", DV_DULL
};
uint32_t pwmreg_get_voltage(void *);
int pwmreg_set_voltage(void *, uint32_t);
int pwmreg_enable(void *, int);
int
pwmreg_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "pwm-regulator");
}
void
pwmreg_attach(struct device *parent, struct device *self, void *aux)
{
struct pwmreg_softc *sc = (struct pwmreg_softc *)self;
struct fdt_attach_args *faa = aux;
int len;
if (OF_getproplen(faa->fa_node, "voltage-tables") > 0) {
printf(": voltage table mode unsupported\n");
return;
}
len = OF_getproplen(faa->fa_node, "pwms");
if (len <= 4) {
printf(": no pwm\n");
return;
}
sc->sc_pwm = malloc(len, M_DEVBUF, M_WAITOK);
OF_getpropintarray(faa->fa_node, "pwms", sc->sc_pwm, len);
sc->sc_dutycycle_unit =
OF_getpropint(faa->fa_node, "pwm-dutycycle-unit", 100);
sc->sc_dutycycle_range[0] = 0;
sc->sc_dutycycle_range[1] = 100;
OF_getpropintarray(faa->fa_node, "pwm-dutycycle-range",
sc->sc_dutycycle_range, sizeof(sc->sc_dutycycle_range));
printf("\n");
sc->sc_rd.rd_node = faa->fa_node;
sc->sc_rd.rd_cookie = sc;
sc->sc_rd.rd_get_voltage = pwmreg_get_voltage;
sc->sc_rd.rd_set_voltage = pwmreg_set_voltage;
sc->sc_rd.rd_enable = pwmreg_enable;
regulator_register(&sc->sc_rd);
}
uint32_t
pwmreg_get_voltage(void *cookie)
{
struct pwmreg_softc *sc = cookie;
struct pwm_state ps;
int32_t x0, x1, y0, y1;
int32_t x, y;
if (pwm_get_state(sc->sc_pwm, &ps))
return 0;
x0 = sc->sc_dutycycle_range[0];
x1 = sc->sc_dutycycle_range[1];
y0 = sc->sc_rd.rd_volt_min;
y1 = sc->sc_rd.rd_volt_max;
x = (ps.ps_pulse_width * sc->sc_dutycycle_unit) / ps.ps_period;
y = y0 + (x - x0) * (y1 - y0) / (x1 - x0);
return y;
}
int
pwmreg_set_voltage(void *cookie, uint32_t voltage)
{
struct pwmreg_softc *sc = cookie;
struct pwm_state ps;
int32_t x0, x1, y0, y1;
int32_t x, y;
if (pwm_init_state(sc->sc_pwm, &ps))
return 0;
x0 = sc->sc_rd.rd_volt_min;
x1 = sc->sc_rd.rd_volt_max;
y0 = sc->sc_dutycycle_range[0];
y1 = sc->sc_dutycycle_range[1];
x = voltage;
y = y0 + (x - x0) * (y1 - y0) / (x1 - x0);
ps.ps_pulse_width = (y * ps.ps_period) / sc->sc_dutycycle_unit;
return pwm_set_state(sc->sc_pwm, &ps);
}
int
pwmreg_enable(void *cookie, int on)
{
struct pwmreg_softc *sc = cookie;
struct pwm_state ps;
int error;
error = pwm_get_state(sc->sc_pwm, &ps);
if (error)
return error;
if (ps.ps_enabled == on)
return 0;
ps.ps_enabled = on;
return pwm_set_state(sc->sc_pwm, &ps);
}
|