fancontrol.c revision 1.2
1/* $NetBSD: fancontrol.c,v 1.2 2021/07/28 00:36:00 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.2 2021/07/28 00:36:00 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 <dev/sysmon/sysmonvar.h> 38 39#include <macppc/dev/fancontrolvar.h> 40#include "opt_fancontrol.h" 41 42#ifdef FANCONTROL_DEBUG 43#define DPRINTF printf 44#else 45#define DPRINTF while (0) printf 46#endif 47 48int 49fancontrol_adjust_zone(fancontrol_zone_t *z) 50{ 51 int temp, i, speed, diff, step; 52 53 if (z->nfans <= 0) 54 return -1; 55 56 temp = sysmon_envsys_get_max_value(z->filter, true); 57 if (temp == 0) { 58 /* no sensor data - leave fan alone */ 59 DPRINTF("nodata\n"); 60 return -1; 61 } 62 63 temp = (temp - 273150000) / 1000000; 64 diff = temp - z->Tmin; 65 DPRINTF("%s %d %d\n", z->name, temp, z->Tmin); 66 if (diff < 0) diff = 0; 67 diff = (100 * diff) / (z->Tmax - z->Tmin); 68 69 /* now adjust each fan to the new speed */ 70 for (i = 0; i < z->nfans; i++) { 71 step = (z->fans[i].max_rpm - z->fans[i].min_rpm) / 100; 72 speed = z->fans[i].min_rpm + diff * step; 73 DPRINTF("diff %d base %d %d sp %d\n", 74 diff, z->fans[i].min_rpm, z->fans[i].max_rpm, speed); 75 z->set_rpm(z->cookie, z->fans[i].num, speed); 76 } 77 return 0; 78} 79