2 * Copyright (c) 2011 Justin Hibbits
3 * Copyright (c) 2010 Andreas Tobler
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/callout.h>
38 #include <sys/ctype.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/limits.h>
42 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
47 #include <machine/bus.h>
48 #include <machine/md_var.h>
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/iicbus/iiconf.h>
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <powerpc/powermac/powermac_thermal.h>
57 struct adm1030_softc {
60 struct intr_config_hook enum_hook;
62 phandle_t sc_thermostat_phandle;
63 device_t sc_thermostat_dev;
66 /* Regular bus attachment functions */
67 static int adm1030_probe(device_t);
68 static int adm1030_attach(device_t);
70 /* Utility functions */
71 static void adm1030_start(void *xdev);
72 static int adm1030_write_byte(device_t dev, uint32_t addr, uint8_t reg, uint8_t buf);
73 static int adm1030_set(struct adm1030_softc *fan, int pwm);
75 static device_method_t adm1030_methods[] = {
76 /* Device interface */
77 DEVMETHOD(device_probe, adm1030_probe),
78 DEVMETHOD(device_attach, adm1030_attach),
82 static driver_t adm1030_driver = {
85 sizeof(struct adm1030_softc)
88 static devclass_t adm1030_devclass;
90 DRIVER_MODULE(adm1030, iicbus, adm1030_driver, adm1030_devclass, 0, 0);
93 adm1030_write_byte(device_t dev, uint32_t addr, uint8_t reg, uint8_t byte)
98 struct iic_msg msg[] = {
99 {addr, IIC_M_WR, 0, buf}
108 if (iicbus_transfer(dev, msg, 1) == 0)
112 device_printf(dev, "iicbus write failed\n");
115 pause("adm1030_write_byte", hz);
120 adm1030_probe(device_t dev)
122 const char *name, *compatible;
123 struct adm1030_softc *sc;
125 phandle_t thermostat;
127 name = ofw_bus_get_name(dev);
128 compatible = ofw_bus_get_compat(dev);
129 handle = ofw_bus_get_node(dev);
134 if (strcmp(name, "fan") != 0 || strcmp(compatible, "adm1030") != 0)
137 /* This driver can only be used if there's an associated temp sensor. */
138 if (OF_getprop(handle, "platform-getTemp", &thermostat, sizeof(thermostat)) < 0)
141 sc = device_get_softc(dev);
143 sc->sc_addr = iicbus_get_addr(dev);
145 device_set_desc(dev, "G4 MDD Fan driver");
151 adm1030_attach(device_t dev)
153 struct adm1030_softc *sc;
155 sc = device_get_softc(dev);
157 sc->enum_hook.ich_func = adm1030_start;
158 sc->enum_hook.ich_arg = dev;
161 * We have to wait until interrupts are enabled. I2C read and write
162 * only works if the interrupts are available. The unin/i2c is
163 * controlled by the htpic on unin. But this is not the master. The
164 * openpic on mac-io is controlling the htpic. This one gets attached
165 * after the mac-io probing and then the interrupts will be
169 if (config_intrhook_establish(&sc->enum_hook) != 0)
176 adm1030_start(void *xdev)
178 struct adm1030_softc *sc;
180 device_t dev = (device_t) xdev;
182 sc = device_get_softc(dev);
184 /* Start the adm1030 device. */
185 adm1030_write_byte(sc->sc_dev, sc->sc_addr, 0x1, 0x1);
186 adm1030_write_byte(sc->sc_dev, sc->sc_addr, 0x0, 0x95);
187 adm1030_write_byte(sc->sc_dev, sc->sc_addr, 0x23, 0x91);
189 /* Use the RPM fields as PWM duty cycles. */
191 sc->fan.max_rpm = 15;
192 sc->fan.default_rpm = 2;
194 strcpy(sc->fan.name, "MDD Case fan");
197 sc->fan.set = (int (*)(struct pmac_fan *, int))adm1030_set;
198 config_intrhook_disestablish(&sc->enum_hook);
200 pmac_thermal_fan_register(&sc->fan);
203 static int adm1030_set(struct adm1030_softc *fan, int pwm)
205 /* Clamp the PWM to 0-0xF, one nibble. */
211 if (adm1030_write_byte(fan->sc_dev, fan->sc_addr, 0x22, pwm) < 0)