2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/openfirm.h>
46 #include <powerpc/powermac/macgpiovar.h>
48 static int vcoregpio_probe(device_t);
49 static int vcoregpio_attach(device_t);
50 static void vcoregpio_pre_change(device_t, const struct cf_level *level);
51 static void vcoregpio_post_change(device_t, const struct cf_level *level);
53 static device_method_t vcoregpio_methods[] = {
54 /* Device interface */
55 DEVMETHOD(device_probe, vcoregpio_probe),
56 DEVMETHOD(device_attach, vcoregpio_attach),
60 static driver_t vcoregpio_driver = {
66 static devclass_t vcoregpio_devclass;
68 DRIVER_MODULE(vcoregpio, macgpio, vcoregpio_driver, vcoregpio_devclass, 0, 0);
71 vcoregpio_probe(device_t dev)
73 const char *name = ofw_bus_get_name(dev);
75 if (strcmp(name, "cpu-vcore-select") != 0)
78 device_set_desc(dev, "CPU Core Voltage Control");
83 vcoregpio_attach(device_t dev)
85 EVENTHANDLER_REGISTER(cpufreq_pre_change, vcoregpio_pre_change, dev,
86 EVENTHANDLER_PRI_ANY);
87 EVENTHANDLER_REGISTER(cpufreq_post_change, vcoregpio_post_change, dev,
88 EVENTHANDLER_PRI_ANY);
94 vcoregpio_pre_change(device_t dev, const struct cf_level *level)
96 if (level->rel_set[0].freq == 10000 /* max */) {
98 * Make sure the CPU voltage is raised before we raise
101 macgpio_write(dev, GPIO_DDR_OUTPUT | 1);
107 vcoregpio_post_change(device_t dev, const struct cf_level *level)
109 if (level->rel_set[0].freq < 10000 /* max */) {
111 /* We are safe to reduce CPU voltage now */
112 macgpio_write(dev, GPIO_DDR_OUTPUT | 0);