xref: /freebsd/sys/dev/iicbus/pwm/adm1030.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1*22d7dd83SEmmanuel Vadot /*-
2*22d7dd83SEmmanuel Vadot  * Copyright (c) 2011 Justin Hibbits
3*22d7dd83SEmmanuel Vadot  * Copyright (c) 2010 Andreas Tobler
4*22d7dd83SEmmanuel Vadot  * All rights reserved.
5*22d7dd83SEmmanuel Vadot  *
6*22d7dd83SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7*22d7dd83SEmmanuel Vadot  * modification, are permitted provided that the following conditions
8*22d7dd83SEmmanuel Vadot  * are met:
9*22d7dd83SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10*22d7dd83SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11*22d7dd83SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12*22d7dd83SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13*22d7dd83SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14*22d7dd83SEmmanuel Vadot  *
15*22d7dd83SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*22d7dd83SEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*22d7dd83SEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*22d7dd83SEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*22d7dd83SEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20*22d7dd83SEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21*22d7dd83SEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22*22d7dd83SEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23*22d7dd83SEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*22d7dd83SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*22d7dd83SEmmanuel Vadot  * SUCH DAMAGE.
26*22d7dd83SEmmanuel Vadot  */
27*22d7dd83SEmmanuel Vadot 
28*22d7dd83SEmmanuel Vadot #include <sys/param.h>
29*22d7dd83SEmmanuel Vadot #include <sys/bus.h>
30*22d7dd83SEmmanuel Vadot #include <sys/systm.h>
31*22d7dd83SEmmanuel Vadot #include <sys/module.h>
32*22d7dd83SEmmanuel Vadot #include <sys/callout.h>
33*22d7dd83SEmmanuel Vadot #include <sys/conf.h>
34*22d7dd83SEmmanuel Vadot #include <sys/cpu.h>
35*22d7dd83SEmmanuel Vadot #include <sys/ctype.h>
36*22d7dd83SEmmanuel Vadot #include <sys/kernel.h>
37*22d7dd83SEmmanuel Vadot #include <sys/kthread.h>
38*22d7dd83SEmmanuel Vadot #include <sys/limits.h>
39*22d7dd83SEmmanuel Vadot #include <sys/reboot.h>
40*22d7dd83SEmmanuel Vadot #include <sys/rman.h>
41*22d7dd83SEmmanuel Vadot #include <sys/sysctl.h>
42*22d7dd83SEmmanuel Vadot #include <sys/unistd.h>
43*22d7dd83SEmmanuel Vadot 
44*22d7dd83SEmmanuel Vadot #include <machine/bus.h>
45*22d7dd83SEmmanuel Vadot #include <machine/md_var.h>
46*22d7dd83SEmmanuel Vadot 
47*22d7dd83SEmmanuel Vadot #include <dev/iicbus/iicbus.h>
48*22d7dd83SEmmanuel Vadot #include <dev/iicbus/iiconf.h>
49*22d7dd83SEmmanuel Vadot 
50*22d7dd83SEmmanuel Vadot #include <dev/ofw/openfirm.h>
51*22d7dd83SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
52*22d7dd83SEmmanuel Vadot #include <powerpc/powermac/powermac_thermal.h>
53*22d7dd83SEmmanuel Vadot 
54*22d7dd83SEmmanuel Vadot struct adm1030_softc {
55*22d7dd83SEmmanuel Vadot 	struct pmac_fan fan;
56*22d7dd83SEmmanuel Vadot 	device_t	sc_dev;
57*22d7dd83SEmmanuel Vadot 	struct intr_config_hook enum_hook;
58*22d7dd83SEmmanuel Vadot 	uint32_t	sc_addr;
59*22d7dd83SEmmanuel Vadot 	int		sc_pwm;
60*22d7dd83SEmmanuel Vadot };
61*22d7dd83SEmmanuel Vadot 
62*22d7dd83SEmmanuel Vadot /* Regular bus attachment functions */
63*22d7dd83SEmmanuel Vadot static int	adm1030_probe(device_t);
64*22d7dd83SEmmanuel Vadot static int	adm1030_attach(device_t);
65*22d7dd83SEmmanuel Vadot 
66*22d7dd83SEmmanuel Vadot /* Utility functions */
67*22d7dd83SEmmanuel Vadot static void	adm1030_start(void *xdev);
68*22d7dd83SEmmanuel Vadot static int	adm1030_write_byte(device_t dev, uint32_t addr, uint8_t reg, uint8_t buf);
69*22d7dd83SEmmanuel Vadot static int	adm1030_set(struct adm1030_softc *fan, int pwm);
70*22d7dd83SEmmanuel Vadot static int	adm1030_sysctl(SYSCTL_HANDLER_ARGS);
71*22d7dd83SEmmanuel Vadot 
72*22d7dd83SEmmanuel Vadot static device_method_t adm1030_methods[] = {
73*22d7dd83SEmmanuel Vadot 	/* Device interface */
74*22d7dd83SEmmanuel Vadot 	DEVMETHOD(device_probe, adm1030_probe),
75*22d7dd83SEmmanuel Vadot 	DEVMETHOD(device_attach, adm1030_attach),
76*22d7dd83SEmmanuel Vadot 	{0, 0},
77*22d7dd83SEmmanuel Vadot };
78*22d7dd83SEmmanuel Vadot 
79*22d7dd83SEmmanuel Vadot static driver_t	adm1030_driver = {
80*22d7dd83SEmmanuel Vadot 	"adm1030",
81*22d7dd83SEmmanuel Vadot 	adm1030_methods,
82*22d7dd83SEmmanuel Vadot 	sizeof(struct adm1030_softc)
83*22d7dd83SEmmanuel Vadot };
84*22d7dd83SEmmanuel Vadot 
85*22d7dd83SEmmanuel Vadot DRIVER_MODULE(adm1030, iicbus, adm1030_driver, 0, 0);
86*22d7dd83SEmmanuel Vadot 
87*22d7dd83SEmmanuel Vadot static int
adm1030_write_byte(device_t dev,uint32_t addr,uint8_t reg,uint8_t byte)88*22d7dd83SEmmanuel Vadot adm1030_write_byte(device_t dev, uint32_t addr, uint8_t reg, uint8_t byte)
89*22d7dd83SEmmanuel Vadot {
90*22d7dd83SEmmanuel Vadot 	unsigned char	buf[4];
91*22d7dd83SEmmanuel Vadot 	int try = 0;
92*22d7dd83SEmmanuel Vadot 
93*22d7dd83SEmmanuel Vadot 	struct iic_msg	msg[] = {
94*22d7dd83SEmmanuel Vadot 		{addr, IIC_M_WR, 0, buf}
95*22d7dd83SEmmanuel Vadot 	};
96*22d7dd83SEmmanuel Vadot 
97*22d7dd83SEmmanuel Vadot 	msg[0].len = 2;
98*22d7dd83SEmmanuel Vadot 	buf[0] = reg;
99*22d7dd83SEmmanuel Vadot 	buf[1] = byte;
100*22d7dd83SEmmanuel Vadot 
101*22d7dd83SEmmanuel Vadot 	for (;;)
102*22d7dd83SEmmanuel Vadot 	{
103*22d7dd83SEmmanuel Vadot 		if (iicbus_transfer(dev, msg, 1) == 0)
104*22d7dd83SEmmanuel Vadot 			return (0);
105*22d7dd83SEmmanuel Vadot 
106*22d7dd83SEmmanuel Vadot 		if (++try > 5) {
107*22d7dd83SEmmanuel Vadot 			device_printf(dev, "iicbus write failed\n");
108*22d7dd83SEmmanuel Vadot 			return (-1);
109*22d7dd83SEmmanuel Vadot 		}
110*22d7dd83SEmmanuel Vadot 		pause("adm1030_write_byte", hz);
111*22d7dd83SEmmanuel Vadot 	}
112*22d7dd83SEmmanuel Vadot }
113*22d7dd83SEmmanuel Vadot 
114*22d7dd83SEmmanuel Vadot static int
adm1030_probe(device_t dev)115*22d7dd83SEmmanuel Vadot adm1030_probe(device_t dev)
116*22d7dd83SEmmanuel Vadot {
117*22d7dd83SEmmanuel Vadot 	const char     *name, *compatible;
118*22d7dd83SEmmanuel Vadot 	struct adm1030_softc *sc;
119*22d7dd83SEmmanuel Vadot 	phandle_t	handle;
120*22d7dd83SEmmanuel Vadot 	phandle_t	thermostat;
121*22d7dd83SEmmanuel Vadot 
122*22d7dd83SEmmanuel Vadot 	name = ofw_bus_get_name(dev);
123*22d7dd83SEmmanuel Vadot 	compatible = ofw_bus_get_compat(dev);
124*22d7dd83SEmmanuel Vadot 	handle = ofw_bus_get_node(dev);
125*22d7dd83SEmmanuel Vadot 
126*22d7dd83SEmmanuel Vadot 	if (!name)
127*22d7dd83SEmmanuel Vadot 		return (ENXIO);
128*22d7dd83SEmmanuel Vadot 
129*22d7dd83SEmmanuel Vadot 	if (strcmp(name, "fan") != 0 || strcmp(compatible, "adm1030") != 0)
130*22d7dd83SEmmanuel Vadot 		return (ENXIO);
131*22d7dd83SEmmanuel Vadot 
132*22d7dd83SEmmanuel Vadot 	/* This driver can only be used if there's an associated temp sensor. */
133*22d7dd83SEmmanuel Vadot 	if (OF_getprop(handle, "platform-getTemp", &thermostat, sizeof(thermostat)) < 0)
134*22d7dd83SEmmanuel Vadot 		return (ENXIO);
135*22d7dd83SEmmanuel Vadot 
136*22d7dd83SEmmanuel Vadot 	sc = device_get_softc(dev);
137*22d7dd83SEmmanuel Vadot 	sc->sc_dev = dev;
138*22d7dd83SEmmanuel Vadot 	sc->sc_addr = iicbus_get_addr(dev);
139*22d7dd83SEmmanuel Vadot 
140*22d7dd83SEmmanuel Vadot 	device_set_desc(dev, "G4 MDD Fan driver");
141*22d7dd83SEmmanuel Vadot 
142*22d7dd83SEmmanuel Vadot 	return (0);
143*22d7dd83SEmmanuel Vadot }
144*22d7dd83SEmmanuel Vadot 
145*22d7dd83SEmmanuel Vadot static int
adm1030_attach(device_t dev)146*22d7dd83SEmmanuel Vadot adm1030_attach(device_t dev)
147*22d7dd83SEmmanuel Vadot {
148*22d7dd83SEmmanuel Vadot 	struct adm1030_softc *sc;
149*22d7dd83SEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
150*22d7dd83SEmmanuel Vadot 	struct sysctl_oid *tree;
151*22d7dd83SEmmanuel Vadot 
152*22d7dd83SEmmanuel Vadot 	sc = device_get_softc(dev);
153*22d7dd83SEmmanuel Vadot 
154*22d7dd83SEmmanuel Vadot 	sc->enum_hook.ich_func = adm1030_start;
155*22d7dd83SEmmanuel Vadot 	sc->enum_hook.ich_arg = dev;
156*22d7dd83SEmmanuel Vadot 
157*22d7dd83SEmmanuel Vadot 	/*
158*22d7dd83SEmmanuel Vadot 	 * Wait until interrupts are available, which won't be until the openpic is
159*22d7dd83SEmmanuel Vadot 	 * intialized.
160*22d7dd83SEmmanuel Vadot 	 */
161*22d7dd83SEmmanuel Vadot 
162*22d7dd83SEmmanuel Vadot 	if (config_intrhook_establish(&sc->enum_hook) != 0)
163*22d7dd83SEmmanuel Vadot 		return (ENOMEM);
164*22d7dd83SEmmanuel Vadot 
165*22d7dd83SEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
166*22d7dd83SEmmanuel Vadot 	tree = device_get_sysctl_tree(dev);
167*22d7dd83SEmmanuel Vadot 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "pwm",
168*22d7dd83SEmmanuel Vadot 			CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev,
169*22d7dd83SEmmanuel Vadot 			0, adm1030_sysctl, "I", "Fan PWM Rate");
170*22d7dd83SEmmanuel Vadot 
171*22d7dd83SEmmanuel Vadot 	return (0);
172*22d7dd83SEmmanuel Vadot }
173*22d7dd83SEmmanuel Vadot 
174*22d7dd83SEmmanuel Vadot static void
adm1030_start(void * xdev)175*22d7dd83SEmmanuel Vadot adm1030_start(void *xdev)
176*22d7dd83SEmmanuel Vadot {
177*22d7dd83SEmmanuel Vadot 	struct adm1030_softc *sc;
178*22d7dd83SEmmanuel Vadot 
179*22d7dd83SEmmanuel Vadot 	device_t	dev = (device_t) xdev;
180*22d7dd83SEmmanuel Vadot 
181*22d7dd83SEmmanuel Vadot 	sc = device_get_softc(dev);
182*22d7dd83SEmmanuel Vadot 
183*22d7dd83SEmmanuel Vadot 	/* Start the adm1030 device. */
184*22d7dd83SEmmanuel Vadot 	adm1030_write_byte(sc->sc_dev, sc->sc_addr, 0x1, 0x1);
185*22d7dd83SEmmanuel Vadot 	adm1030_write_byte(sc->sc_dev, sc->sc_addr, 0x0, 0x95);
186*22d7dd83SEmmanuel Vadot 	adm1030_write_byte(sc->sc_dev, sc->sc_addr, 0x23, 0x91);
187*22d7dd83SEmmanuel Vadot 
188*22d7dd83SEmmanuel Vadot 	/* Use the RPM fields as PWM duty cycles. */
189*22d7dd83SEmmanuel Vadot 	sc->fan.min_rpm = 0;
190*22d7dd83SEmmanuel Vadot 	sc->fan.max_rpm = 0x0F;
191*22d7dd83SEmmanuel Vadot 	sc->fan.default_rpm = 2;
192*22d7dd83SEmmanuel Vadot 
193*22d7dd83SEmmanuel Vadot 	strcpy(sc->fan.name, "MDD Case fan");
194*22d7dd83SEmmanuel Vadot 	sc->fan.zone = 0;
195*22d7dd83SEmmanuel Vadot 	sc->fan.read = NULL;
196*22d7dd83SEmmanuel Vadot 	sc->fan.set = (int (*)(struct pmac_fan *, int))adm1030_set;
197*22d7dd83SEmmanuel Vadot 	config_intrhook_disestablish(&sc->enum_hook);
198*22d7dd83SEmmanuel Vadot 
199*22d7dd83SEmmanuel Vadot 	pmac_thermal_fan_register(&sc->fan);
200*22d7dd83SEmmanuel Vadot }
201*22d7dd83SEmmanuel Vadot 
adm1030_set(struct adm1030_softc * fan,int pwm)202*22d7dd83SEmmanuel Vadot static int adm1030_set(struct adm1030_softc *fan, int pwm)
203*22d7dd83SEmmanuel Vadot {
204*22d7dd83SEmmanuel Vadot 	/* Clamp the PWM to 0-0xF, one nibble. */
205*22d7dd83SEmmanuel Vadot 	if (pwm > 0xF)
206*22d7dd83SEmmanuel Vadot 		pwm = 0xF;
207*22d7dd83SEmmanuel Vadot 	if (pwm < 0)
208*22d7dd83SEmmanuel Vadot 		pwm = 0;
209*22d7dd83SEmmanuel Vadot 
210*22d7dd83SEmmanuel Vadot 	if (adm1030_write_byte(fan->sc_dev, fan->sc_addr, 0x22, pwm) < 0)
211*22d7dd83SEmmanuel Vadot 		return (-1);
212*22d7dd83SEmmanuel Vadot 
213*22d7dd83SEmmanuel Vadot 	fan->sc_pwm = pwm;
214*22d7dd83SEmmanuel Vadot 	return (0);
215*22d7dd83SEmmanuel Vadot }
216*22d7dd83SEmmanuel Vadot 
217*22d7dd83SEmmanuel Vadot static int
adm1030_sysctl(SYSCTL_HANDLER_ARGS)218*22d7dd83SEmmanuel Vadot adm1030_sysctl(SYSCTL_HANDLER_ARGS)
219*22d7dd83SEmmanuel Vadot {
220*22d7dd83SEmmanuel Vadot 	device_t adm1030;
221*22d7dd83SEmmanuel Vadot 	struct adm1030_softc *sc;
222*22d7dd83SEmmanuel Vadot 	int pwm, error;
223*22d7dd83SEmmanuel Vadot 
224*22d7dd83SEmmanuel Vadot 	adm1030 = arg1;
225*22d7dd83SEmmanuel Vadot 	sc = device_get_softc(adm1030);
226*22d7dd83SEmmanuel Vadot 
227*22d7dd83SEmmanuel Vadot 	pwm = sc->sc_pwm;
228*22d7dd83SEmmanuel Vadot 
229*22d7dd83SEmmanuel Vadot 	error = sysctl_handle_int(oidp, &pwm, 0, req);
230*22d7dd83SEmmanuel Vadot 
231*22d7dd83SEmmanuel Vadot 	if (error || !req->newptr)
232*22d7dd83SEmmanuel Vadot 		return (error);
233*22d7dd83SEmmanuel Vadot 
234*22d7dd83SEmmanuel Vadot 	return (adm1030_set(sc, pwm));
235*22d7dd83SEmmanuel Vadot }
236