xref: /netbsd-src/sys/dev/fdt/pwm_fan.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: pwm_fan.c,v 1.2 2021/01/27 03:10:21 thorpej Exp $ */
2aba8b2b2Sjmcneill 
3aba8b2b2Sjmcneill /*-
4aba8b2b2Sjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5aba8b2b2Sjmcneill  * All rights reserved.
6aba8b2b2Sjmcneill  *
7aba8b2b2Sjmcneill  * Redistribution and use in source and binary forms, with or without
8aba8b2b2Sjmcneill  * modification, are permitted provided that the following conditions
9aba8b2b2Sjmcneill  * are met:
10aba8b2b2Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11aba8b2b2Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12aba8b2b2Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13aba8b2b2Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14aba8b2b2Sjmcneill  *    documentation and/or other materials provided with the distribution.
15aba8b2b2Sjmcneill  *
16aba8b2b2Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17aba8b2b2Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18aba8b2b2Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19aba8b2b2Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20aba8b2b2Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21aba8b2b2Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22aba8b2b2Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23aba8b2b2Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24aba8b2b2Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25aba8b2b2Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26aba8b2b2Sjmcneill  * SUCH DAMAGE.
27aba8b2b2Sjmcneill  */
28aba8b2b2Sjmcneill 
29aba8b2b2Sjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: pwm_fan.c,v 1.2 2021/01/27 03:10:21 thorpej Exp $");
31aba8b2b2Sjmcneill 
32aba8b2b2Sjmcneill #include <sys/param.h>
33aba8b2b2Sjmcneill #include <sys/bus.h>
34aba8b2b2Sjmcneill #include <sys/device.h>
35aba8b2b2Sjmcneill #include <sys/systm.h>
36aba8b2b2Sjmcneill #include <sys/sysctl.h>
37aba8b2b2Sjmcneill #include <sys/kmem.h>
38aba8b2b2Sjmcneill #include <sys/gpio.h>
39aba8b2b2Sjmcneill 
40aba8b2b2Sjmcneill #include <dev/pwm/pwmvar.h>
41aba8b2b2Sjmcneill 
42aba8b2b2Sjmcneill #include <dev/fdt/fdtvar.h>
43aba8b2b2Sjmcneill 
44aba8b2b2Sjmcneill struct pwm_fan_softc {
45aba8b2b2Sjmcneill 	device_t		sc_dev;
46aba8b2b2Sjmcneill 	pwm_tag_t		sc_pwm;
47aba8b2b2Sjmcneill 	struct fdtbus_gpio_pin *sc_pin;
48aba8b2b2Sjmcneill 
49aba8b2b2Sjmcneill 	u_int			*sc_levels;
50aba8b2b2Sjmcneill 	u_int			sc_nlevels;
51aba8b2b2Sjmcneill };
52aba8b2b2Sjmcneill 
53aba8b2b2Sjmcneill static int	pwm_fan_match(device_t, cfdata_t, void *);
54aba8b2b2Sjmcneill static void	pwm_fan_attach(device_t, device_t, void *);
55aba8b2b2Sjmcneill 
56aba8b2b2Sjmcneill static void	pwm_fan_set(struct pwm_fan_softc *, u_int);
57aba8b2b2Sjmcneill 
58*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
59*6e54367aSthorpej 	{ .compat = "pwm-fan" },
60*6e54367aSthorpej 	DEVICE_COMPAT_EOL
61aba8b2b2Sjmcneill };
62aba8b2b2Sjmcneill 
63aba8b2b2Sjmcneill CFATTACH_DECL_NEW(pwmfan, sizeof(struct pwm_fan_softc),
64aba8b2b2Sjmcneill 	pwm_fan_match, pwm_fan_attach, NULL, NULL);
65aba8b2b2Sjmcneill 
66aba8b2b2Sjmcneill static int
pwm_fan_match(device_t parent,cfdata_t cf,void * aux)67aba8b2b2Sjmcneill pwm_fan_match(device_t parent, cfdata_t cf, void *aux)
68aba8b2b2Sjmcneill {
69aba8b2b2Sjmcneill 	struct fdt_attach_args * const faa = aux;
70aba8b2b2Sjmcneill 
71*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
72aba8b2b2Sjmcneill }
73aba8b2b2Sjmcneill 
74aba8b2b2Sjmcneill static void
pwm_fan_attach(device_t parent,device_t self,void * aux)75aba8b2b2Sjmcneill pwm_fan_attach(device_t parent, device_t self, void *aux)
76aba8b2b2Sjmcneill {
77aba8b2b2Sjmcneill 	struct pwm_fan_softc * const sc = device_private(self);
78aba8b2b2Sjmcneill 	struct fdt_attach_args * const faa = aux;
79aba8b2b2Sjmcneill 	const int phandle = faa->faa_phandle;
80aba8b2b2Sjmcneill 	const u_int *levels;
81aba8b2b2Sjmcneill 	u_int n;
82aba8b2b2Sjmcneill 	int len;
83aba8b2b2Sjmcneill 
84aba8b2b2Sjmcneill 	sc->sc_dev = self;
85aba8b2b2Sjmcneill 	sc->sc_pwm = fdtbus_pwm_acquire(phandle, "pwms");
86aba8b2b2Sjmcneill 	if (sc->sc_pwm == NULL) {
87aba8b2b2Sjmcneill 		aprint_error(": couldn't acquire pwm\n");
88aba8b2b2Sjmcneill 		return;
89aba8b2b2Sjmcneill 	}
90aba8b2b2Sjmcneill 
91aba8b2b2Sjmcneill 	levels = fdtbus_get_prop(phandle, "cooling-levels", &len);
92aba8b2b2Sjmcneill 	if (len < 4) {
93aba8b2b2Sjmcneill 		aprint_error(": couldn't get 'cooling-levels' property\n");
94aba8b2b2Sjmcneill 		return;
95aba8b2b2Sjmcneill 	}
96aba8b2b2Sjmcneill 	sc->sc_levels = kmem_alloc(len, KM_SLEEP);
97aba8b2b2Sjmcneill 	sc->sc_nlevels = len / 4;
98aba8b2b2Sjmcneill 	for (n = 0; n < sc->sc_nlevels; n++)
99aba8b2b2Sjmcneill 		sc->sc_levels[n] = be32toh(levels[n]);
100aba8b2b2Sjmcneill 
101aba8b2b2Sjmcneill 	aprint_naive("\n");
102aba8b2b2Sjmcneill 	aprint_normal(": PWM Fan");
103aba8b2b2Sjmcneill 	aprint_normal(" (levels");
104aba8b2b2Sjmcneill 	for (n = 0; n < sc->sc_nlevels; n++) {
105aba8b2b2Sjmcneill 		aprint_normal(" %u%%", (sc->sc_levels[n] * 100) / 255);
106aba8b2b2Sjmcneill 	}
107aba8b2b2Sjmcneill 	aprint_normal(")\n");
108aba8b2b2Sjmcneill 
109aba8b2b2Sjmcneill 	/* Select the highest cooling level */
110aba8b2b2Sjmcneill 	pwm_fan_set(sc, 255);
111aba8b2b2Sjmcneill }
112aba8b2b2Sjmcneill 
113aba8b2b2Sjmcneill static void
pwm_fan_set(struct pwm_fan_softc * sc,u_int level)114aba8b2b2Sjmcneill pwm_fan_set(struct pwm_fan_softc *sc, u_int level)
115aba8b2b2Sjmcneill {
116aba8b2b2Sjmcneill 	struct pwm_config conf;
117aba8b2b2Sjmcneill 
118aba8b2b2Sjmcneill 	if (level > 255)
119aba8b2b2Sjmcneill 		level = 255;
120aba8b2b2Sjmcneill 
121aba8b2b2Sjmcneill 	aprint_debug_dev(sc->sc_dev, "set duty cycle to %u%%\n", (level * 100) / 255);
122aba8b2b2Sjmcneill 
123aba8b2b2Sjmcneill 	pwm_disable(sc->sc_pwm);
124aba8b2b2Sjmcneill 	pwm_get_config(sc->sc_pwm, &conf);
125aba8b2b2Sjmcneill 	conf.duty_cycle = (conf.period * level) / 255;
126aba8b2b2Sjmcneill 	pwm_set_config(sc->sc_pwm, &conf);
127aba8b2b2Sjmcneill 	pwm_enable(sc->sc_pwm);
128aba8b2b2Sjmcneill }
129