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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bio.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/endian.h>
#include <sys/fcntl.h>
#include <sys/ioccom.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/priv.h>
#include <sys/slicer.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/extres/regulator/regulator.h>
#include <dev/backlight/backlight.h>
#include <dev/pwm/ofw_pwm.h>
#include "backlight_if.h"
#include "pwmbus_if.h"
struct pwm_backlight_softc {
device_t pwmdev;
struct cdev *cdev;
pwm_channel_t channel;
uint32_t *levels;
ssize_t nlevels;
int default_level;
ssize_t current_level;
regulator_t power_supply;
uint64_t period;
uint64_t duty;
bool enabled;
};
static int pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent);
static struct ofw_compat_data compat_data[] = {
{ "pwm-backlight", 1 },
{ NULL, 0 }
};
static int
pwm_backlight_probe(device_t dev)
{
if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
return (ENXIO);
device_set_desc(dev, "PWM Backlight");
return (BUS_PROBE_DEFAULT);
}
static int
pwm_backlight_attach(device_t dev)
{
struct pwm_backlight_softc *sc;
phandle_t node;
int rv;
sc = device_get_softc(dev);
node = ofw_bus_get_node(dev);
rv = pwm_get_by_ofw_propidx(dev, node, "pwms", 0, &sc->channel);
if (rv != 0) {
device_printf(dev, "Cannot map pwm channel %d\n", rv);
return (ENXIO);
}
if (regulator_get_by_ofw_property(dev, 0, "power-supply",
&sc->power_supply) != 0) {
device_printf(dev, "No power-supply property\n");
return (ENXIO);
}
if (OF_hasprop(node, "brightness-levels")) {
sc->nlevels = OF_getencprop_alloc(node, "brightness-levels",
(void **)&sc->levels);
if (sc->nlevels <= 0) {
device_printf(dev, "Cannot parse brightness levels\n");
return (ENXIO);
}
sc->nlevels /= sizeof(uint32_t);
if (OF_getencprop(node, "default-brightness-level",
&sc->default_level, sizeof(uint32_t)) <= 0) {
device_printf(dev, "No default-brightness-level while brightness-levels is specified\n");
return (ENXIO);
} else {
if (sc->default_level > sc->nlevels) {
device_printf(dev, "default-brightness-level isn't present in brightness-levels range\n");
return (ENXIO);
}
sc->channel->duty = sc->channel->period * sc->levels[sc->default_level] / 100;
}
if (bootverbose) {
device_printf(dev, "Number of levels: %zd\n", sc->nlevels);
device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period);
device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty);
}
} else {
/* Get the current backlight level */
PWMBUS_CHANNEL_GET_CONFIG(sc->channel->dev,
sc->channel->channel,
(unsigned int *)&sc->channel->period,
(unsigned int *)&sc->channel->duty);
if (sc->channel->duty > sc->channel->period)
sc->channel->duty = sc->channel->period;
if (bootverbose) {
device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period);
device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty);
}
}
regulator_enable(sc->power_supply);
sc->channel->enabled = true;
PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel,
sc->channel->period, sc->channel->duty);
PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel,
sc->channel->enabled);
sc->current_level = pwm_backlight_find_level_per_percent(sc,
sc->channel->period / sc->channel->duty);
sc->cdev = backlight_register("pwm_backlight", dev);
if (sc->cdev == NULL)
device_printf(dev, "Cannot register as a backlight\n");
return (0);
}
static int
pwm_backlight_detach(device_t dev)
{
struct pwm_backlight_softc *sc;
sc = device_get_softc(dev);
if (sc->nlevels > 0)
OF_prop_free(sc->levels);
regulator_disable(sc->power_supply);
backlight_destroy(sc->cdev);
return (0);
}
static int
pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent)
{
int i;
int diff;
if (percent < 0 || percent > 100)
return (-1);
for (i = 0, diff = 0; i < sc->nlevels; i++) {
if (sc->levels[i] == percent)
return (i);
else if (sc->levels[i] < percent)
diff = percent - sc->levels[i];
else {
if (diff < abs((percent - sc->levels[i])))
return (i - 1);
else
return (i);
}
}
return (-1);
}
static int
pwm_backlight_update_status(device_t dev, struct backlight_props *props)
{
struct pwm_backlight_softc *sc;
int reg_status;
int error;
sc = device_get_softc(dev);
if (sc->nlevels != 0) {
error = pwm_backlight_find_level_per_percent(sc,
props->brightness);
if (error < 0)
return (ERANGE);
sc->current_level = error;
sc->channel->duty = sc->channel->period *
sc->levels[sc->current_level] / 100;
} else {
if (props->brightness > 100 || props->brightness < 0)
return (ERANGE);
sc->channel->duty = sc->channel->period *
props->brightness / 100;
}
sc->channel->enabled = true;
PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel,
sc->channel->period, sc->channel->duty);
PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel,
sc->channel->enabled);
error = regulator_status(sc->power_supply, ®_status);
if (error != 0)
device_printf(dev,
"Cannot get power-supply status: %d\n", error);
else {
if (props->brightness > 0) {
if (reg_status != REGULATOR_STATUS_ENABLED)
regulator_enable(sc->power_supply);
} else {
if (reg_status == REGULATOR_STATUS_ENABLED)
regulator_disable(sc->power_supply);
}
}
return (0);
}
static int
pwm_backlight_get_status(device_t dev, struct backlight_props *props)
{
struct pwm_backlight_softc *sc;
int i;
sc = device_get_softc(dev);
if (sc->nlevels != 0) {
props->brightness = sc->levels[sc->current_level];
props->nlevels = sc->nlevels;
for (i = 0; i < sc->nlevels; i++)
props->levels[i] = sc->levels[i];
} else {
props->brightness = sc->channel->duty * 100 / sc->channel->period;
props->nlevels = 0;
}
return (0);
}
static int
pwm_backlight_get_info(device_t dev, struct backlight_info *info)
{
info->type = BACKLIGHT_TYPE_PANEL;
strlcpy(info->name, "pwm-backlight", BACKLIGHTMAXNAMELENGTH);
return (0);
}
static device_method_t pwm_backlight_methods[] = {
/* device_if */
DEVMETHOD(device_probe, pwm_backlight_probe),
DEVMETHOD(device_attach, pwm_backlight_attach),
DEVMETHOD(device_detach, pwm_backlight_detach),
/* backlight interface */
DEVMETHOD(backlight_update_status, pwm_backlight_update_status),
DEVMETHOD(backlight_get_status, pwm_backlight_get_status),
DEVMETHOD(backlight_get_info, pwm_backlight_get_info),
DEVMETHOD_END
};
driver_t pwm_backlight_driver = {
"pwm_backlight",
pwm_backlight_methods,
sizeof(struct pwm_backlight_softc),
};
devclass_t pwm_backlight_devclass;
DRIVER_MODULE(pwm_backlight, simplebus, pwm_backlight_driver,
pwm_backlight_devclass, 0, 0);
MODULE_DEPEND(pwm_backlight, backlight, 1, 1, 1);
OFWBUS_PNP_INFO(compat_data);
|