xref: /freebsd/sys/dev/backlight/backlight.c (revision 675aae732d3dd2ec0f12f39df3a1fbfe693a4ebd)
1*675aae73SEmmanuel Vadot /*-
2*675aae73SEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*675aae73SEmmanuel Vadot  *
4*675aae73SEmmanuel Vadot  * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
5*675aae73SEmmanuel Vadot  *
6*675aae73SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7*675aae73SEmmanuel Vadot  * modification, are permitted provided that the following conditions
8*675aae73SEmmanuel Vadot  * are met:
9*675aae73SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10*675aae73SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11*675aae73SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12*675aae73SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13*675aae73SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14*675aae73SEmmanuel Vadot  *
15*675aae73SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*675aae73SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*675aae73SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*675aae73SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*675aae73SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*675aae73SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*675aae73SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*675aae73SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*675aae73SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*675aae73SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*675aae73SEmmanuel Vadot  * SUCH DAMAGE.
26*675aae73SEmmanuel Vadot  *
27*675aae73SEmmanuel Vadot  * $FreeBSD$
28*675aae73SEmmanuel Vadot  */
29*675aae73SEmmanuel Vadot 
30*675aae73SEmmanuel Vadot #include <sys/cdefs.h>
31*675aae73SEmmanuel Vadot __FBSDID("$FreeBSD$");
32*675aae73SEmmanuel Vadot 
33*675aae73SEmmanuel Vadot #include <sys/param.h>
34*675aae73SEmmanuel Vadot #include <sys/systm.h>
35*675aae73SEmmanuel Vadot #include <sys/limits.h>
36*675aae73SEmmanuel Vadot #include <sys/bus.h>
37*675aae73SEmmanuel Vadot #include <sys/conf.h>
38*675aae73SEmmanuel Vadot #include <sys/kernel.h>
39*675aae73SEmmanuel Vadot #include <sys/malloc.h>
40*675aae73SEmmanuel Vadot #include <sys/module.h>
41*675aae73SEmmanuel Vadot #include <sys/lock.h>
42*675aae73SEmmanuel Vadot #include <sys/sx.h>
43*675aae73SEmmanuel Vadot 
44*675aae73SEmmanuel Vadot #include <dev/backlight/backlight.h>
45*675aae73SEmmanuel Vadot 
46*675aae73SEmmanuel Vadot #include "backlight_if.h"
47*675aae73SEmmanuel Vadot 
48*675aae73SEmmanuel Vadot static struct sx backlight_sx;
49*675aae73SEmmanuel Vadot static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
50*675aae73SEmmanuel Vadot static struct unrhdr *backlight_unit;
51*675aae73SEmmanuel Vadot 
52*675aae73SEmmanuel Vadot struct backlight_softc {
53*675aae73SEmmanuel Vadot 	struct cdev *cdev;
54*675aae73SEmmanuel Vadot 	struct cdev *alias;
55*675aae73SEmmanuel Vadot 	int unit;
56*675aae73SEmmanuel Vadot 	device_t dev;
57*675aae73SEmmanuel Vadot 	uint32_t cached_brightness;
58*675aae73SEmmanuel Vadot };
59*675aae73SEmmanuel Vadot 
60*675aae73SEmmanuel Vadot static int
61*675aae73SEmmanuel Vadot backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
62*675aae73SEmmanuel Vadot     int fflag, struct thread *td)
63*675aae73SEmmanuel Vadot {
64*675aae73SEmmanuel Vadot 	struct backlight_softc *sc;
65*675aae73SEmmanuel Vadot 	struct backlight_props props;
66*675aae73SEmmanuel Vadot 	struct backlight_info info;
67*675aae73SEmmanuel Vadot 	int error;
68*675aae73SEmmanuel Vadot 
69*675aae73SEmmanuel Vadot 	sc = dev->si_drv1;
70*675aae73SEmmanuel Vadot 
71*675aae73SEmmanuel Vadot 	switch (cmd) {
72*675aae73SEmmanuel Vadot 	case BACKLIGHTGETSTATUS:
73*675aae73SEmmanuel Vadot 		/* Call the driver function so it fills up the props */
74*675aae73SEmmanuel Vadot 		bcopy(data, &props, sizeof(struct backlight_props));
75*675aae73SEmmanuel Vadot 		error = BACKLIGHT_GET_STATUS(sc->dev, &props);
76*675aae73SEmmanuel Vadot 		if (error == 0)
77*675aae73SEmmanuel Vadot 			bcopy(&props, data, sizeof(struct backlight_props));
78*675aae73SEmmanuel Vadot 		break;
79*675aae73SEmmanuel Vadot 	case BACKLIGHTUPDATESTATUS:
80*675aae73SEmmanuel Vadot 		bcopy(data, &props, sizeof(struct backlight_props));
81*675aae73SEmmanuel Vadot 		if (props.brightness == sc->cached_brightness)
82*675aae73SEmmanuel Vadot 			return (0);
83*675aae73SEmmanuel Vadot 		error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
84*675aae73SEmmanuel Vadot 		if (error == 0) {
85*675aae73SEmmanuel Vadot 			bcopy(&props, data, sizeof(struct backlight_props));
86*675aae73SEmmanuel Vadot 			sc->cached_brightness = props.brightness;
87*675aae73SEmmanuel Vadot 		}
88*675aae73SEmmanuel Vadot 		break;
89*675aae73SEmmanuel Vadot 	case BACKLIGHTGETINFO:
90*675aae73SEmmanuel Vadot 		memset(&info, 0, sizeof(info));
91*675aae73SEmmanuel Vadot 		error = BACKLIGHT_GET_INFO(sc->dev, &info);
92*675aae73SEmmanuel Vadot 		if (error == 0)
93*675aae73SEmmanuel Vadot 			bcopy(&info, data, sizeof(struct backlight_info));
94*675aae73SEmmanuel Vadot 		break;
95*675aae73SEmmanuel Vadot 	}
96*675aae73SEmmanuel Vadot 
97*675aae73SEmmanuel Vadot 	return (error);
98*675aae73SEmmanuel Vadot }
99*675aae73SEmmanuel Vadot 
100*675aae73SEmmanuel Vadot static struct cdevsw backlight_cdevsw = {
101*675aae73SEmmanuel Vadot 	.d_version =	D_VERSION,
102*675aae73SEmmanuel Vadot 	.d_ioctl =	backlight_ioctl,
103*675aae73SEmmanuel Vadot 	.d_name =	"backlight",
104*675aae73SEmmanuel Vadot };
105*675aae73SEmmanuel Vadot 
106*675aae73SEmmanuel Vadot struct cdev *
107*675aae73SEmmanuel Vadot backlight_register(const char *name, device_t dev)
108*675aae73SEmmanuel Vadot {
109*675aae73SEmmanuel Vadot 	struct make_dev_args args;
110*675aae73SEmmanuel Vadot 	struct backlight_softc *sc;
111*675aae73SEmmanuel Vadot 	struct backlight_props props;
112*675aae73SEmmanuel Vadot 	int error;
113*675aae73SEmmanuel Vadot 
114*675aae73SEmmanuel Vadot 	sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
115*675aae73SEmmanuel Vadot 
116*675aae73SEmmanuel Vadot 	sx_xlock(&backlight_sx);
117*675aae73SEmmanuel Vadot 	sc->unit = alloc_unr(backlight_unit);
118*675aae73SEmmanuel Vadot 	sc->dev = dev;
119*675aae73SEmmanuel Vadot 	make_dev_args_init(&args);
120*675aae73SEmmanuel Vadot 	args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
121*675aae73SEmmanuel Vadot 	args.mda_devsw = &backlight_cdevsw;
122*675aae73SEmmanuel Vadot 	args.mda_uid = UID_ROOT;
123*675aae73SEmmanuel Vadot 	args.mda_gid = GID_VIDEO;
124*675aae73SEmmanuel Vadot 	args.mda_mode = 0660;
125*675aae73SEmmanuel Vadot 	args.mda_si_drv1 = sc;
126*675aae73SEmmanuel Vadot 	error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
127*675aae73SEmmanuel Vadot 
128*675aae73SEmmanuel Vadot 	if (error != 0)
129*675aae73SEmmanuel Vadot 		goto fail;
130*675aae73SEmmanuel Vadot 
131*675aae73SEmmanuel Vadot 	error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
132*675aae73SEmmanuel Vadot 	  &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
133*675aae73SEmmanuel Vadot 	if (error != 0)
134*675aae73SEmmanuel Vadot 		device_printf(dev, "Cannot register with alias %s%d\n", name,
135*675aae73SEmmanuel Vadot 		    sc->unit);
136*675aae73SEmmanuel Vadot 
137*675aae73SEmmanuel Vadot 	sx_xunlock(&backlight_sx);
138*675aae73SEmmanuel Vadot 
139*675aae73SEmmanuel Vadot 	error = BACKLIGHT_GET_STATUS(sc->dev, &props);
140*675aae73SEmmanuel Vadot 	sc->cached_brightness = props.brightness;
141*675aae73SEmmanuel Vadot 
142*675aae73SEmmanuel Vadot 	return (sc->cdev);
143*675aae73SEmmanuel Vadot fail:
144*675aae73SEmmanuel Vadot 	sx_xunlock(&backlight_sx);
145*675aae73SEmmanuel Vadot 	return (NULL);
146*675aae73SEmmanuel Vadot }
147*675aae73SEmmanuel Vadot 
148*675aae73SEmmanuel Vadot int
149*675aae73SEmmanuel Vadot backlight_destroy(struct cdev *dev)
150*675aae73SEmmanuel Vadot {
151*675aae73SEmmanuel Vadot 	struct backlight_softc *sc;
152*675aae73SEmmanuel Vadot 
153*675aae73SEmmanuel Vadot 	sc = dev->si_drv1;
154*675aae73SEmmanuel Vadot 	sx_xlock(&backlight_sx);
155*675aae73SEmmanuel Vadot 	free_unr(backlight_unit, sc->unit);
156*675aae73SEmmanuel Vadot 	destroy_dev(dev);
157*675aae73SEmmanuel Vadot 	sx_xunlock(&backlight_sx);
158*675aae73SEmmanuel Vadot 	return (0);
159*675aae73SEmmanuel Vadot }
160*675aae73SEmmanuel Vadot 
161*675aae73SEmmanuel Vadot static void
162*675aae73SEmmanuel Vadot backlight_drvinit(void *unused)
163*675aae73SEmmanuel Vadot {
164*675aae73SEmmanuel Vadot 
165*675aae73SEmmanuel Vadot 	backlight_unit = new_unrhdr(0, INT_MAX, NULL);
166*675aae73SEmmanuel Vadot 	sx_init(&backlight_sx, "Backlight sx");
167*675aae73SEmmanuel Vadot }
168*675aae73SEmmanuel Vadot 
169*675aae73SEmmanuel Vadot SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
170*675aae73SEmmanuel Vadot MODULE_VERSION(backlight, 1);
171