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