xref: /freebsd/sys/dev/backlight/backlight.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1675aae73SEmmanuel Vadot /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3675aae73SEmmanuel Vadot  *
4675aae73SEmmanuel Vadot  * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
5675aae73SEmmanuel Vadot  *
6675aae73SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7675aae73SEmmanuel Vadot  * modification, are permitted provided that the following conditions
8675aae73SEmmanuel Vadot  * are met:
9675aae73SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10675aae73SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11675aae73SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12675aae73SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13675aae73SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14675aae73SEmmanuel Vadot  *
15675aae73SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16675aae73SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17675aae73SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18675aae73SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19675aae73SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20675aae73SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21675aae73SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22675aae73SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23675aae73SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24675aae73SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25675aae73SEmmanuel Vadot  * SUCH DAMAGE.
26675aae73SEmmanuel Vadot  */
27675aae73SEmmanuel Vadot 
28675aae73SEmmanuel Vadot #include <sys/param.h>
29675aae73SEmmanuel Vadot #include <sys/systm.h>
30675aae73SEmmanuel Vadot #include <sys/limits.h>
31675aae73SEmmanuel Vadot #include <sys/bus.h>
32675aae73SEmmanuel Vadot #include <sys/conf.h>
33675aae73SEmmanuel Vadot #include <sys/kernel.h>
34675aae73SEmmanuel Vadot #include <sys/malloc.h>
35675aae73SEmmanuel Vadot #include <sys/module.h>
36675aae73SEmmanuel Vadot #include <sys/lock.h>
37675aae73SEmmanuel Vadot #include <sys/sx.h>
38675aae73SEmmanuel Vadot 
39675aae73SEmmanuel Vadot #include <dev/backlight/backlight.h>
40675aae73SEmmanuel Vadot 
41675aae73SEmmanuel Vadot #include "backlight_if.h"
42675aae73SEmmanuel Vadot 
43675aae73SEmmanuel Vadot static struct sx backlight_sx;
44675aae73SEmmanuel Vadot static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
45675aae73SEmmanuel Vadot static struct unrhdr *backlight_unit;
46675aae73SEmmanuel Vadot 
47675aae73SEmmanuel Vadot struct backlight_softc {
48675aae73SEmmanuel Vadot 	struct cdev *cdev;
49675aae73SEmmanuel Vadot 	struct cdev *alias;
50675aae73SEmmanuel Vadot 	int unit;
51675aae73SEmmanuel Vadot 	device_t dev;
52675aae73SEmmanuel Vadot 	uint32_t cached_brightness;
53675aae73SEmmanuel Vadot };
54675aae73SEmmanuel Vadot 
55675aae73SEmmanuel Vadot static int
backlight_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)56675aae73SEmmanuel Vadot backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
57675aae73SEmmanuel Vadot     int fflag, struct thread *td)
58675aae73SEmmanuel Vadot {
59675aae73SEmmanuel Vadot 	struct backlight_softc *sc;
60675aae73SEmmanuel Vadot 	struct backlight_props props;
61675aae73SEmmanuel Vadot 	struct backlight_info info;
62675aae73SEmmanuel Vadot 	int error;
63675aae73SEmmanuel Vadot 
64675aae73SEmmanuel Vadot 	sc = dev->si_drv1;
65675aae73SEmmanuel Vadot 
66675aae73SEmmanuel Vadot 	switch (cmd) {
67675aae73SEmmanuel Vadot 	case BACKLIGHTGETSTATUS:
68675aae73SEmmanuel Vadot 		/* Call the driver function so it fills up the props */
69675aae73SEmmanuel Vadot 		bcopy(data, &props, sizeof(struct backlight_props));
70675aae73SEmmanuel Vadot 		error = BACKLIGHT_GET_STATUS(sc->dev, &props);
71e26ef41fSEmmanuel Vadot 		if (error == 0) {
72675aae73SEmmanuel Vadot 			bcopy(&props, data, sizeof(struct backlight_props));
73e26ef41fSEmmanuel Vadot 			sc->cached_brightness = props.brightness;
74e26ef41fSEmmanuel Vadot 		}
75675aae73SEmmanuel Vadot 		break;
76675aae73SEmmanuel Vadot 	case BACKLIGHTUPDATESTATUS:
77675aae73SEmmanuel Vadot 		bcopy(data, &props, sizeof(struct backlight_props));
78675aae73SEmmanuel Vadot 		if (props.brightness == sc->cached_brightness)
79675aae73SEmmanuel Vadot 			return (0);
80675aae73SEmmanuel Vadot 		error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
81675aae73SEmmanuel Vadot 		if (error == 0) {
82675aae73SEmmanuel Vadot 			bcopy(&props, data, sizeof(struct backlight_props));
83675aae73SEmmanuel Vadot 			sc->cached_brightness = props.brightness;
84675aae73SEmmanuel Vadot 		}
85675aae73SEmmanuel Vadot 		break;
86675aae73SEmmanuel Vadot 	case BACKLIGHTGETINFO:
87675aae73SEmmanuel Vadot 		memset(&info, 0, sizeof(info));
88675aae73SEmmanuel Vadot 		error = BACKLIGHT_GET_INFO(sc->dev, &info);
89675aae73SEmmanuel Vadot 		if (error == 0)
90675aae73SEmmanuel Vadot 			bcopy(&info, data, sizeof(struct backlight_info));
91675aae73SEmmanuel Vadot 		break;
92675aae73SEmmanuel Vadot 	}
93675aae73SEmmanuel Vadot 
94675aae73SEmmanuel Vadot 	return (error);
95675aae73SEmmanuel Vadot }
96675aae73SEmmanuel Vadot 
97675aae73SEmmanuel Vadot static struct cdevsw backlight_cdevsw = {
98675aae73SEmmanuel Vadot 	.d_version =	D_VERSION,
99675aae73SEmmanuel Vadot 	.d_ioctl =	backlight_ioctl,
100675aae73SEmmanuel Vadot 	.d_name =	"backlight",
101675aae73SEmmanuel Vadot };
102675aae73SEmmanuel Vadot 
103675aae73SEmmanuel Vadot struct cdev *
backlight_register(const char * name,device_t dev)104675aae73SEmmanuel Vadot backlight_register(const char *name, device_t dev)
105675aae73SEmmanuel Vadot {
106675aae73SEmmanuel Vadot 	struct make_dev_args args;
107675aae73SEmmanuel Vadot 	struct backlight_softc *sc;
108675aae73SEmmanuel Vadot 	struct backlight_props props;
109675aae73SEmmanuel Vadot 	int error;
110675aae73SEmmanuel Vadot 
111675aae73SEmmanuel Vadot 	sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
112675aae73SEmmanuel Vadot 
113675aae73SEmmanuel Vadot 	sx_xlock(&backlight_sx);
114675aae73SEmmanuel Vadot 	sc->unit = alloc_unr(backlight_unit);
115675aae73SEmmanuel Vadot 	sc->dev = dev;
116675aae73SEmmanuel Vadot 	make_dev_args_init(&args);
117675aae73SEmmanuel Vadot 	args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
118675aae73SEmmanuel Vadot 	args.mda_devsw = &backlight_cdevsw;
119675aae73SEmmanuel Vadot 	args.mda_uid = UID_ROOT;
120675aae73SEmmanuel Vadot 	args.mda_gid = GID_VIDEO;
121675aae73SEmmanuel Vadot 	args.mda_mode = 0660;
122675aae73SEmmanuel Vadot 	args.mda_si_drv1 = sc;
123675aae73SEmmanuel Vadot 	error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
124675aae73SEmmanuel Vadot 
125675aae73SEmmanuel Vadot 	if (error != 0)
126675aae73SEmmanuel Vadot 		goto fail;
127675aae73SEmmanuel Vadot 
128675aae73SEmmanuel Vadot 	error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
129675aae73SEmmanuel Vadot 	  &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
130675aae73SEmmanuel Vadot 	if (error != 0)
131675aae73SEmmanuel Vadot 		device_printf(dev, "Cannot register with alias %s%d\n", name,
132675aae73SEmmanuel Vadot 		    sc->unit);
133675aae73SEmmanuel Vadot 
134675aae73SEmmanuel Vadot 	sx_xunlock(&backlight_sx);
135675aae73SEmmanuel Vadot 
136675aae73SEmmanuel Vadot 	error = BACKLIGHT_GET_STATUS(sc->dev, &props);
137675aae73SEmmanuel Vadot 	sc->cached_brightness = props.brightness;
138675aae73SEmmanuel Vadot 
139675aae73SEmmanuel Vadot 	return (sc->cdev);
140675aae73SEmmanuel Vadot fail:
141675aae73SEmmanuel Vadot 	sx_xunlock(&backlight_sx);
142675aae73SEmmanuel Vadot 	return (NULL);
143675aae73SEmmanuel Vadot }
144675aae73SEmmanuel Vadot 
145675aae73SEmmanuel Vadot int
backlight_destroy(struct cdev * dev)146675aae73SEmmanuel Vadot backlight_destroy(struct cdev *dev)
147675aae73SEmmanuel Vadot {
148675aae73SEmmanuel Vadot 	struct backlight_softc *sc;
149675aae73SEmmanuel Vadot 
150675aae73SEmmanuel Vadot 	sc = dev->si_drv1;
151675aae73SEmmanuel Vadot 	sx_xlock(&backlight_sx);
152675aae73SEmmanuel Vadot 	free_unr(backlight_unit, sc->unit);
153675aae73SEmmanuel Vadot 	destroy_dev(dev);
154675aae73SEmmanuel Vadot 	sx_xunlock(&backlight_sx);
155675aae73SEmmanuel Vadot 	return (0);
156675aae73SEmmanuel Vadot }
157675aae73SEmmanuel Vadot 
158675aae73SEmmanuel Vadot static void
backlight_drvinit(void * unused)159675aae73SEmmanuel Vadot backlight_drvinit(void *unused)
160675aae73SEmmanuel Vadot {
161675aae73SEmmanuel Vadot 
162675aae73SEmmanuel Vadot 	backlight_unit = new_unrhdr(0, INT_MAX, NULL);
163675aae73SEmmanuel Vadot 	sx_init(&backlight_sx, "Backlight sx");
164675aae73SEmmanuel Vadot }
165675aae73SEmmanuel Vadot 
166675aae73SEmmanuel Vadot SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
167675aae73SEmmanuel Vadot MODULE_VERSION(backlight, 1);
168