1/* $NetBSD: fancontrol.c,v 1.3 2021/07/30 22:07:14 macallan Exp $ */ 2 3/*- 4 * Copyright (c) 2021 Michael Lorenz 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: fancontrol.c,v 1.3 2021/07/30 22:07:14 macallan Exp $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/device.h> 35#include <sys/conf.h> 36#include <sys/bus.h> 37#include <sys/sysctl.h> 38#include <dev/sysmon/sysmonvar.h> 39 40#include <macppc/dev/fancontrolvar.h> 41#include "opt_fancontrol.h" 42 43#ifdef FANCONTROL_DEBUG 44#define DPRINTF printf 45#else 46#define DPRINTF while (0) printf 47#endif 48 49int 50fancontrol_adjust_zone(fancontrol_zone_t *z) 51{ 52 int temp, i, speed, diff, step; 53 54 if (z->nfans <= 0) 55 return -1; 56 57 temp = sysmon_envsys_get_max_value(z->filter, true); 58 if (temp == 0) { 59 /* no sensor data - leave fan alone */ 60 DPRINTF("nodata\n"); 61 return -1; 62 } 63 64 if (z->Tmin < 30) z->Tmin = 30; 65 if (z->Tmin > 60) z->Tmin = 60; 66 if (z->Tmax > 95) z->Tmax = 95; 67 if (z->Tmax < (z->Tmin + 10)) z->Tmax = z->Tmin + 10; 68 temp = (temp - 273150000) / 1000000; 69 diff = temp - z->Tmin; 70 DPRINTF("%s %d %d\n", z->name, temp, z->Tmin); 71 if (diff < 0) diff = 0; 72 diff = (100 * diff) / (z->Tmax - z->Tmin); 73 74 /* now adjust each fan to the new speed */ 75 for (i = 0; i < z->nfans; i++) { 76 step = (z->fans[i].max_rpm - z->fans[i].min_rpm) / 100; 77 speed = z->fans[i].min_rpm + diff * step; 78 DPRINTF("diff %d base %d %d sp %d\n", 79 diff, z->fans[i].min_rpm, z->fans[i].max_rpm, speed); 80 z->set_rpm(z->cookie, z->fans[i].num, speed); 81 } 82 return 0; 83} 84 85int 86fancontrol_init_zone(fancontrol_zone_t *z, struct sysctlnode *me) 87{ 88 struct sysctlnode *zone_node, *node; 89 90 if (z->nfans <= 0) return 0; 91 92 sysctl_createv(NULL, 0, NULL, (void *) &zone_node, 93 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 94 CTLTYPE_NODE, z->name, NULL, 95 NULL, 0, NULL, 0, 96 CTL_MACHDEP, 97 me->sysctl_num, 98 CTL_CREATE, CTL_EOL); 99 100 sysctl_createv(NULL, 0, NULL, (void *) &node, 101 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 102 CTLTYPE_INT, "Tmin", "minimum temperature", 103 NULL, 0, (void *)&z->Tmin, 0, 104 CTL_MACHDEP, 105 me->sysctl_num, 106 zone_node->sysctl_num, 107 CTL_CREATE, CTL_EOL); 108 109 sysctl_createv(NULL, 0, NULL, (void *) &node, 110 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 111 CTLTYPE_INT, "Tmax", "maximum temperature", 112 NULL, 0, (void *)&z->Tmax, 0, 113 CTL_MACHDEP, 114 me->sysctl_num, 115 zone_node->sysctl_num, 116 CTL_CREATE, CTL_EOL); 117 118 return 0; 119} 120