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