therm_pm72.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,340 行 · 第 1/3 页
C
1,340 行
/* * Device driver for the thermostats & fan controller of the * Apple G5 "PowerMac7,2" desktop machines. * * (c) Copyright IBM Corp. 2003-2004 * * Maintained by: Benjamin Herrenschmidt * <benh@kernel.crashing.org> * * * The algorithm used is the PID control algorithm, used the same * way the published Darwin code does, using the same values that * are present in the Darwin 7.0 snapshot property lists. * * As far as the CPUs control loops are concerned, I use the * calibration & PID constants provided by the EEPROM, * I do _not_ embed any value from the property lists, as the ones * provided by Darwin 7.0 seem to always have an older version that * what I've seen on the actual computers. * It would be interesting to verify that though. Darwin has a * version code of 1.0.0d11 for all control loops it seems, while * so far, the machines EEPROMs contain a dataset versioned 1.0.0f * * Darwin doesn't provide source to all parts, some missing * bits like the AppleFCU driver or the actual scale of some * of the values returned by sensors had to be "guessed" some * way... or based on what Open Firmware does. * * I didn't yet figure out how to get the slots power consumption * out of the FCU, so that part has not been implemented yet and * the slots fan is set to a fixed 50% PWM, hoping this value is * safe enough ... * * Note: I have observed strange oscillations of the CPU control * loop on a dual G5 here. When idle, the CPU exhaust fan tend to * oscillates slowly (over several minutes) between the minimum * of 300RPMs and approx. 1000 RPMs. I don't know what is causing * this, it could be some incorrect constant or an error in the * way I ported the algorithm, or it could be just normal. I * don't have full understanding on the way Apple tweaked the PID * algorithm for the CPU control, it is definitely not a standard * implementation... * * TODO: - Check MPU structure version/signature * - Add things like /sbin/overtemp for non-critical * overtemp conditions so userland can take some policy * decisions, like slewing down CPUs * - Deal with fan and i2c failures in a better way * * History: * * Nov. 13, 2003 : 0.5 * - First release * * Nov. 14, 2003 : 0.6 * - Read fan speed from FCU, low level fan routines now deal * with errors & check fan status, though higher level don't * do much. * - Move a bunch of definitions to .h file * * Nov. 18, 2003 : 0.7 * - Fix build on ppc64 kernel * - Move back statics definitions to .c file * - Avoid calling schedule_timeout with a negative number * * Dec. 18, 2003 : 0.8 * - Fix typo when reading back fan speed on 2 CPU machines * * Mar. 11, 2004 : 0.9 * - Rework code accessing the ADC chips, make it more robust and * closer to the chip spec. Also make sure it is configured properly, * I've seen yet unexplained cases where on startup, I would have stale * values in the configuration register * - Switch back to use of target fan speed for PID, thus lowering * pressure on i2c */#include <linux/config.h>#include <linux/types.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/delay.h>#include <linux/sched.h>#include <linux/i2c.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/smp_lock.h>#include <linux/wait.h>#include <linux/reboot.h>#include <linux/kmod.h>#include <linux/i2c.h>#include <linux/i2c-dev.h>#include <asm/prom.h>#include <asm/machdep.h>#include <asm/io.h>#include <asm/system.h>#include <asm/sections.h>#include <asm/of_device.h>#include "therm_pm72.h"#define VERSION "0.9"#undef DEBUG#ifdef DEBUG#define DBG(args...) printk(args)#else#define DBG(args...) do { } while(0)#endif/* * Driver statics */static struct of_device * of_dev;static struct i2c_adapter * u3_0;static struct i2c_adapter * u3_1;static struct i2c_client * fcu;static struct cpu_pid_state cpu_state[2];static struct backside_pid_state backside_state;static struct drives_pid_state drives_state;static int state;static int cpu_count;static pid_t ctrl_task;static struct completion ctrl_complete;static int critical_state;static DECLARE_MUTEX(driver_lock);/* * i2c_driver structure to attach to the host i2c controller */static int therm_pm72_attach(struct i2c_adapter *adapter);static int therm_pm72_detach(struct i2c_adapter *adapter);static struct i2c_driver therm_pm72_driver ={ .name = "therm_pm72", .id = 0xDEADBEEF, .flags = I2C_DF_NOTIFY, .attach_adapter = therm_pm72_attach, .detach_adapter = therm_pm72_detach,};/* * Utility function to create an i2c_client structure and * attach it to one of u3 adapters */static struct i2c_client *attach_i2c_chip(int id, const char *name){ struct i2c_client *clt; struct i2c_adapter *adap; if (id & 0x100) adap = u3_1; else adap = u3_0; if (adap == NULL) return NULL; clt = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); if (clt == NULL) return NULL; memset(clt, 0, sizeof(struct i2c_client)); clt->addr = (id >> 1) & 0x7f; clt->adapter = adap; clt->driver = &therm_pm72_driver; clt->id = 0xDEADBEEF; strncpy(clt->name, name, I2C_NAME_SIZE-1); if (i2c_attach_client(clt)) { printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id); kfree(clt); return NULL; } return clt;}/* * Utility function to get rid of the i2c_client structure * (will also detach from the adapter hopepfully) */static void detach_i2c_chip(struct i2c_client *clt){ i2c_detach_client(clt); kfree(clt);}/* * Here are the i2c chip access wrappers */static void initialize_adc(struct cpu_pid_state *state){ int rc; u8 buf[2]; /* Read ADC the configuration register and cache it. We * also make sure Config2 contains proper values, I've seen * cases where we got stale grabage in there, thus preventing * proper reading of conv. values */ /* Clear Config2 */ buf[0] = 5; buf[1] = 0; i2c_master_send(state->monitor, buf, 2); /* Read & cache Config1 */ buf[0] = 1; rc = i2c_master_send(state->monitor, buf, 1); if (rc > 0) { rc = i2c_master_recv(state->monitor, buf, 1); if (rc > 0) { state->adc_config = buf[0]; DBG("ADC config reg: %02x\n", state->adc_config); /* Disable shutdown mode */ state->adc_config &= 0xfe; buf[0] = 1; buf[1] = state->adc_config; rc = i2c_master_send(state->monitor, buf, 2); } } if (rc <= 0) printk(KERN_ERR "therm_pm72: Error reading ADC config" " register !\n");}static int read_smon_adc(struct cpu_pid_state *state, int chan){ int rc, data, tries = 0; u8 buf[2]; for (;;) { /* Set channel */ buf[0] = 1; buf[1] = (state->adc_config & 0x1f) | (chan << 5); rc = i2c_master_send(state->monitor, buf, 2); if (rc <= 0) goto error; /* Wait for convertion */ msleep(1); /* Switch to data register */ buf[0] = 4; rc = i2c_master_send(state->monitor, buf, 1); if (rc <= 0) goto error; /* Read result */ rc = i2c_master_recv(state->monitor, buf, 2); if (rc < 0) goto error; data = ((u16)buf[0]) << 8 | (u16)buf[1]; return data >> 6; error: DBG("Error reading ADC, retrying...\n"); if (++tries > 10) { printk(KERN_ERR "therm_pm72: Error reading ADC !\n"); return -1; } msleep(10); }}static int fan_read_reg(int reg, unsigned char *buf, int nb){ int tries, nr, nw; buf[0] = reg; tries = 0; for (;;) { nw = i2c_master_send(fcu, buf, 1); if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100) break; msleep(10); ++tries; } if (nw <= 0) { printk(KERN_ERR "Failure writing address to FCU: %d", nw); return -EIO; } tries = 0; for (;;) { nr = i2c_master_recv(fcu, buf, nb); if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100) break; msleep(10); ++tries; } if (nr <= 0) printk(KERN_ERR "Failure reading data from FCU: %d", nw); return nr;}static int fan_write_reg(int reg, const unsigned char *ptr, int nb){ int tries, nw; unsigned char buf[16]; buf[0] = reg; memcpy(buf+1, ptr, nb); ++nb; tries = 0; for (;;) { nw = i2c_master_send(fcu, buf, nb); if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100) break; msleep(10); ++tries; } if (nw < 0) printk(KERN_ERR "Failure writing to FCU: %d", nw); return nw;}static int start_fcu(void){ unsigned char buf = 0xff; int rc; rc = fan_write_reg(0xe, &buf, 1); if (rc < 0) return -EIO; rc = fan_write_reg(0x2e, &buf, 1); if (rc < 0) return -EIO; return 0;}static int set_rpm_fan(int fan, int rpm){ unsigned char buf[2]; int rc; if (rpm < 300) rpm = 300; else if (rpm > 8191) rpm = 8191; buf[0] = rpm >> 5; buf[1] = rpm << 3; rc = fan_write_reg(0x10 + (fan * 2), buf, 2); if (rc < 0) return -EIO; return 0;}static int get_rpm_fan(int fan, int programmed){ unsigned char failure; unsigned char active; unsigned char buf[2]; int rc, reg_base; rc = fan_read_reg(0xb, &failure, 1); if (rc != 1) return -EIO; if ((failure & (1 << fan)) != 0) return -EFAULT; rc = fan_read_reg(0xd, &active, 1); if (rc != 1) return -EIO; if ((active & (1 << fan)) == 0) return -ENXIO; /* Programmed value or real current speed */ reg_base = programmed ? 0x10 : 0x11; rc = fan_read_reg(reg_base + (fan * 2), buf, 2); if (rc != 2) return -EIO; return (buf[0] << 5) | buf[1] >> 3;}static int set_pwm_fan(int fan, int pwm){ unsigned char buf[2]; int rc; if (pwm < 10) pwm = 10; else if (pwm > 100) pwm = 100; pwm = (pwm * 2559) / 1000; buf[0] = pwm; rc = fan_write_reg(0x30 + (fan * 2), buf, 1); if (rc < 0) return rc; return 0;}static int get_pwm_fan(int fan){ unsigned char failure; unsigned char active; unsigned char buf[2]; int rc; rc = fan_read_reg(0x2b, &failure, 1); if (rc != 1) return -EIO; if ((failure & (1 << fan)) != 0) return -EFAULT; rc = fan_read_reg(0x2d, &active, 1); if (rc != 1) return -EIO; if ((active & (1 << fan)) == 0) return -ENXIO; /* Programmed value or real current speed */ rc = fan_read_reg(0x30 + (fan * 2), buf, 1); if (rc != 1) return -EIO; return (buf[0] * 1000) / 2559;}/* * Utility routine to read the CPU calibration EEPROM data * from the device-tree */static int read_eeprom(int cpu, struct mpu_data *out){ struct device_node *np; char nodename[64]; u8 *data; int len; /* prom.c routine for finding a node by path is a bit brain dead * and requires exact @xxx unit numbers. This is a bit ugly but * will work for these machines */ sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0); np = of_find_node_by_path(nodename); if (np == NULL) { printk(KERN_ERR "therm_pm72: Failed to retreive cpuid node from device-tree\n"); return -ENODEV; } data = (u8 *)get_property(np, "cpuid", &len); if (data == NULL) { printk(KERN_ERR "therm_pm72: Failed to retreive cpuid property from device-tree\n"); of_node_put(np); return -ENODEV; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?