PageRenderTime 82ms CodeModel.GetById 19ms app.highlight 52ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/macintosh/therm_pm72.c

https://bitbucket.org/ndreys/linux-sunxi
C | 2280 lines | 1555 code | 314 blank | 411 comment | 352 complexity | bb8ffbed94b4960c6ff282ead354f6e1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Device driver for the thermostats & fan controller of  the
   3 * Apple G5 "PowerMac7,2" desktop machines.
   4 *
   5 * (c) Copyright IBM Corp. 2003-2004
   6 *
   7 * Maintained by: Benjamin Herrenschmidt
   8 *                <benh@kernel.crashing.org>
   9 * 
  10 *
  11 * The algorithm used is the PID control algorithm, used the same
  12 * way the published Darwin code does, using the same values that
  13 * are present in the Darwin 7.0 snapshot property lists.
  14 *
  15 * As far as the CPUs control loops are concerned, I use the
  16 * calibration & PID constants provided by the EEPROM,
  17 * I do _not_ embed any value from the property lists, as the ones
  18 * provided by Darwin 7.0 seem to always have an older version that
  19 * what I've seen on the actual computers.
  20 * It would be interesting to verify that though. Darwin has a
  21 * version code of 1.0.0d11 for all control loops it seems, while
  22 * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
  23 *
  24 * Darwin doesn't provide source to all parts, some missing
  25 * bits like the AppleFCU driver or the actual scale of some
  26 * of the values returned by sensors had to be "guessed" some
  27 * way... or based on what Open Firmware does.
  28 *
  29 * I didn't yet figure out how to get the slots power consumption
  30 * out of the FCU, so that part has not been implemented yet and
  31 * the slots fan is set to a fixed 50% PWM, hoping this value is
  32 * safe enough ...
  33 *
  34 * Note: I have observed strange oscillations of the CPU control
  35 * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
  36 * oscillates slowly (over several minutes) between the minimum
  37 * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
  38 * this, it could be some incorrect constant or an error in the
  39 * way I ported the algorithm, or it could be just normal. I
  40 * don't have full understanding on the way Apple tweaked the PID
  41 * algorithm for the CPU control, it is definitely not a standard
  42 * implementation...
  43 *
  44 * TODO:  - Check MPU structure version/signature
  45 *        - Add things like /sbin/overtemp for non-critical
  46 *          overtemp conditions so userland can take some policy
  47 *          decisions, like slowing down CPUs
  48 *	  - Deal with fan and i2c failures in a better way
  49 *	  - Maybe do a generic PID based on params used for
  50 *	    U3 and Drives ? Definitely need to factor code a bit
  51 *          better... also make sensor detection more robust using
  52 *          the device-tree to probe for them
  53 *        - Figure out how to get the slots consumption and set the
  54 *          slots fan accordingly
  55 *
  56 * History:
  57 *
  58 *  Nov. 13, 2003 : 0.5
  59 *	- First release
  60 *
  61 *  Nov. 14, 2003 : 0.6
  62 *	- Read fan speed from FCU, low level fan routines now deal
  63 *	  with errors & check fan status, though higher level don't
  64 *	  do much.
  65 *	- Move a bunch of definitions to .h file
  66 *
  67 *  Nov. 18, 2003 : 0.7
  68 *	- Fix build on ppc64 kernel
  69 *	- Move back statics definitions to .c file
  70 *	- Avoid calling schedule_timeout with a negative number
  71 *
  72 *  Dec. 18, 2003 : 0.8
  73 *	- Fix typo when reading back fan speed on 2 CPU machines
  74 *
  75 *  Mar. 11, 2004 : 0.9
  76 *	- Rework code accessing the ADC chips, make it more robust and
  77 *	  closer to the chip spec. Also make sure it is configured properly,
  78 *        I've seen yet unexplained cases where on startup, I would have stale
  79 *        values in the configuration register
  80 *	- Switch back to use of target fan speed for PID, thus lowering
  81 *        pressure on i2c
  82 *
  83 *  Oct. 20, 2004 : 1.1
  84 *	- Add device-tree lookup for fan IDs, should detect liquid cooling
  85 *        pumps when present
  86 *	- Enable driver for PowerMac7,3 machines
  87 *	- Split the U3/Backside cooling on U3 & U3H versions as Darwin does
  88 *	- Add new CPU cooling algorithm for machines with liquid cooling
  89 *	- Workaround for some PowerMac7,3 with empty "fan" node in the devtree
  90 *	- Fix a signed/unsigned compare issue in some PID loops
  91 *
  92 *  Mar. 10, 2005 : 1.2
  93 *	- Add basic support for Xserve G5
  94 *	- Retrieve pumps min/max from EEPROM image in device-tree (broken)
  95 *	- Use min/max macros here or there
  96 *	- Latest darwin updated U3H min fan speed to 20% PWM
  97 *
  98 *  July. 06, 2006 : 1.3
  99 *	- Fix setting of RPM fans on Xserve G5 (they were going too fast)
 100 *      - Add missing slots fan control loop for Xserve G5
 101 *	- Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
 102 *        still can't properly implement the control loop for these, so let's
 103 *        reduce the noise a little bit, it appears that 40% still gives us
 104 *        a pretty good air flow
 105 *	- Add code to "tickle" the FCU regulary so it doesn't think that
 106 *        we are gone while in fact, the machine just didn't need any fan
 107 *        speed change lately
 108 *
 109 */
 110
 111#include <linux/types.h>
 112#include <linux/module.h>
 113#include <linux/errno.h>
 114#include <linux/kernel.h>
 115#include <linux/delay.h>
 116#include <linux/sched.h>
 117#include <linux/init.h>
 118#include <linux/spinlock.h>
 119#include <linux/wait.h>
 120#include <linux/reboot.h>
 121#include <linux/kmod.h>
 122#include <linux/i2c.h>
 123#include <linux/kthread.h>
 124#include <linux/mutex.h>
 125#include <linux/of_device.h>
 126#include <linux/of_platform.h>
 127#include <asm/prom.h>
 128#include <asm/machdep.h>
 129#include <asm/io.h>
 130#include <asm/system.h>
 131#include <asm/sections.h>
 132#include <asm/macio.h>
 133
 134#include "therm_pm72.h"
 135
 136#define VERSION "1.3"
 137
 138#undef DEBUG
 139
 140#ifdef DEBUG
 141#define DBG(args...)	printk(args)
 142#else
 143#define DBG(args...)	do { } while(0)
 144#endif
 145
 146
 147/*
 148 * Driver statics
 149 */
 150
 151static struct platform_device *		of_dev;
 152static struct i2c_adapter *		u3_0;
 153static struct i2c_adapter *		u3_1;
 154static struct i2c_adapter *		k2;
 155static struct i2c_client *		fcu;
 156static struct cpu_pid_state		processor_state[2];
 157static struct basckside_pid_params	backside_params;
 158static struct backside_pid_state	backside_state;
 159static struct drives_pid_state		drives_state;
 160static struct dimm_pid_state		dimms_state;
 161static struct slots_pid_state		slots_state;
 162static int				state;
 163static int				cpu_count;
 164static int				cpu_pid_type;
 165static struct task_struct		*ctrl_task;
 166static struct completion		ctrl_complete;
 167static int				critical_state;
 168static int				rackmac;
 169static s32				dimm_output_clamp;
 170static int 				fcu_rpm_shift;
 171static int				fcu_tickle_ticks;
 172static DEFINE_MUTEX(driver_lock);
 173
 174/*
 175 * We have 3 types of CPU PID control. One is "split" old style control
 176 * for intake & exhaust fans, the other is "combined" control for both
 177 * CPUs that also deals with the pumps when present. To be "compatible"
 178 * with OS X at this point, we only use "COMBINED" on the machines that
 179 * are identified as having the pumps (though that identification is at
 180 * least dodgy). Ultimately, we could probably switch completely to this
 181 * algorithm provided we hack it to deal with the UP case
 182 */
 183#define CPU_PID_TYPE_SPLIT	0
 184#define CPU_PID_TYPE_COMBINED	1
 185#define CPU_PID_TYPE_RACKMAC	2
 186
 187/*
 188 * This table describes all fans in the FCU. The "id" and "type" values
 189 * are defaults valid for all earlier machines. Newer machines will
 190 * eventually override the table content based on the device-tree
 191 */
 192struct fcu_fan_table
 193{
 194	char*	loc;	/* location code */
 195	int	type;	/* 0 = rpm, 1 = pwm, 2 = pump */
 196	int	id;	/* id or -1 */
 197};
 198
 199#define FCU_FAN_RPM		0
 200#define FCU_FAN_PWM		1
 201
 202#define FCU_FAN_ABSENT_ID	-1
 203
 204#define FCU_FAN_COUNT		ARRAY_SIZE(fcu_fans)
 205
 206struct fcu_fan_table	fcu_fans[] = {
 207	[BACKSIDE_FAN_PWM_INDEX] = {
 208		.loc	= "BACKSIDE,SYS CTRLR FAN",
 209		.type	= FCU_FAN_PWM,
 210		.id	= BACKSIDE_FAN_PWM_DEFAULT_ID,
 211	},
 212	[DRIVES_FAN_RPM_INDEX] = {
 213		.loc	= "DRIVE BAY",
 214		.type	= FCU_FAN_RPM,
 215		.id	= DRIVES_FAN_RPM_DEFAULT_ID,
 216	},
 217	[SLOTS_FAN_PWM_INDEX] = {
 218		.loc	= "SLOT,PCI FAN",
 219		.type	= FCU_FAN_PWM,
 220		.id	= SLOTS_FAN_PWM_DEFAULT_ID,
 221	},
 222	[CPUA_INTAKE_FAN_RPM_INDEX] = {
 223		.loc	= "CPU A INTAKE",
 224		.type	= FCU_FAN_RPM,
 225		.id	= CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
 226	},
 227	[CPUA_EXHAUST_FAN_RPM_INDEX] = {
 228		.loc	= "CPU A EXHAUST",
 229		.type	= FCU_FAN_RPM,
 230		.id	= CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
 231	},
 232	[CPUB_INTAKE_FAN_RPM_INDEX] = {
 233		.loc	= "CPU B INTAKE",
 234		.type	= FCU_FAN_RPM,
 235		.id	= CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
 236	},
 237	[CPUB_EXHAUST_FAN_RPM_INDEX] = {
 238		.loc	= "CPU B EXHAUST",
 239		.type	= FCU_FAN_RPM,
 240		.id	= CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
 241	},
 242	/* pumps aren't present by default, have to be looked up in the
 243	 * device-tree
 244	 */
 245	[CPUA_PUMP_RPM_INDEX] = {
 246		.loc	= "CPU A PUMP",
 247		.type	= FCU_FAN_RPM,		
 248		.id	= FCU_FAN_ABSENT_ID,
 249	},
 250	[CPUB_PUMP_RPM_INDEX] = {
 251		.loc	= "CPU B PUMP",
 252		.type	= FCU_FAN_RPM,
 253		.id	= FCU_FAN_ABSENT_ID,
 254	},
 255	/* Xserve fans */
 256	[CPU_A1_FAN_RPM_INDEX] = {
 257		.loc	= "CPU A 1",
 258		.type	= FCU_FAN_RPM,
 259		.id	= FCU_FAN_ABSENT_ID,
 260	},
 261	[CPU_A2_FAN_RPM_INDEX] = {
 262		.loc	= "CPU A 2",
 263		.type	= FCU_FAN_RPM,
 264		.id	= FCU_FAN_ABSENT_ID,
 265	},
 266	[CPU_A3_FAN_RPM_INDEX] = {
 267		.loc	= "CPU A 3",
 268		.type	= FCU_FAN_RPM,
 269		.id	= FCU_FAN_ABSENT_ID,
 270	},
 271	[CPU_B1_FAN_RPM_INDEX] = {
 272		.loc	= "CPU B 1",
 273		.type	= FCU_FAN_RPM,
 274		.id	= FCU_FAN_ABSENT_ID,
 275	},
 276	[CPU_B2_FAN_RPM_INDEX] = {
 277		.loc	= "CPU B 2",
 278		.type	= FCU_FAN_RPM,
 279		.id	= FCU_FAN_ABSENT_ID,
 280	},
 281	[CPU_B3_FAN_RPM_INDEX] = {
 282		.loc	= "CPU B 3",
 283		.type	= FCU_FAN_RPM,
 284		.id	= FCU_FAN_ABSENT_ID,
 285	},
 286};
 287
 288static struct i2c_driver therm_pm72_driver;
 289
 290/*
 291 * Utility function to create an i2c_client structure and
 292 * attach it to one of u3 adapters
 293 */
 294static struct i2c_client *attach_i2c_chip(int id, const char *name)
 295{
 296	struct i2c_client *clt;
 297	struct i2c_adapter *adap;
 298	struct i2c_board_info info;
 299
 300	if (id & 0x200)
 301		adap = k2;
 302	else if (id & 0x100)
 303		adap = u3_1;
 304	else
 305		adap = u3_0;
 306	if (adap == NULL)
 307		return NULL;
 308
 309	memset(&info, 0, sizeof(struct i2c_board_info));
 310	info.addr = (id >> 1) & 0x7f;
 311	strlcpy(info.type, "therm_pm72", I2C_NAME_SIZE);
 312	clt = i2c_new_device(adap, &info);
 313	if (!clt) {
 314		printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
 315		return NULL;
 316	}
 317
 318	/*
 319	 * Let i2c-core delete that device on driver removal.
 320	 * This is safe because i2c-core holds the core_lock mutex for us.
 321	 */
 322	list_add_tail(&clt->detected, &therm_pm72_driver.clients);
 323	return clt;
 324}
 325
 326/*
 327 * Here are the i2c chip access wrappers
 328 */
 329
 330static void initialize_adc(struct cpu_pid_state *state)
 331{
 332	int rc;
 333	u8 buf[2];
 334
 335	/* Read ADC the configuration register and cache it. We
 336	 * also make sure Config2 contains proper values, I've seen
 337	 * cases where we got stale grabage in there, thus preventing
 338	 * proper reading of conv. values
 339	 */
 340
 341	/* Clear Config2 */
 342	buf[0] = 5;
 343	buf[1] = 0;
 344	i2c_master_send(state->monitor, buf, 2);
 345
 346	/* Read & cache Config1 */
 347	buf[0] = 1;
 348	rc = i2c_master_send(state->monitor, buf, 1);
 349	if (rc > 0) {
 350		rc = i2c_master_recv(state->monitor, buf, 1);
 351		if (rc > 0) {
 352			state->adc_config = buf[0];
 353			DBG("ADC config reg: %02x\n", state->adc_config);
 354			/* Disable shutdown mode */
 355		       	state->adc_config &= 0xfe;
 356			buf[0] = 1;
 357			buf[1] = state->adc_config;
 358			rc = i2c_master_send(state->monitor, buf, 2);
 359		}
 360	}
 361	if (rc <= 0)
 362		printk(KERN_ERR "therm_pm72: Error reading ADC config"
 363		       " register !\n");
 364}
 365
 366static int read_smon_adc(struct cpu_pid_state *state, int chan)
 367{
 368	int rc, data, tries = 0;
 369	u8 buf[2];
 370
 371	for (;;) {
 372		/* Set channel */
 373		buf[0] = 1;
 374		buf[1] = (state->adc_config & 0x1f) | (chan << 5);
 375		rc = i2c_master_send(state->monitor, buf, 2);
 376		if (rc <= 0)
 377			goto error;
 378		/* Wait for conversion */
 379		msleep(1);
 380		/* Switch to data register */
 381		buf[0] = 4;
 382		rc = i2c_master_send(state->monitor, buf, 1);
 383		if (rc <= 0)
 384			goto error;
 385		/* Read result */
 386		rc = i2c_master_recv(state->monitor, buf, 2);
 387		if (rc < 0)
 388			goto error;
 389		data = ((u16)buf[0]) << 8 | (u16)buf[1];
 390		return data >> 6;
 391	error:
 392		DBG("Error reading ADC, retrying...\n");
 393		if (++tries > 10) {
 394			printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
 395			return -1;
 396		}
 397		msleep(10);
 398	}
 399}
 400
 401static int read_lm87_reg(struct i2c_client * chip, int reg)
 402{
 403	int rc, tries = 0;
 404	u8 buf;
 405
 406	for (;;) {
 407		/* Set address */
 408		buf = (u8)reg;
 409		rc = i2c_master_send(chip, &buf, 1);
 410		if (rc <= 0)
 411			goto error;
 412		rc = i2c_master_recv(chip, &buf, 1);
 413		if (rc <= 0)
 414			goto error;
 415		return (int)buf;
 416	error:
 417		DBG("Error reading LM87, retrying...\n");
 418		if (++tries > 10) {
 419			printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
 420			return -1;
 421		}
 422		msleep(10);
 423	}
 424}
 425
 426static int fan_read_reg(int reg, unsigned char *buf, int nb)
 427{
 428	int tries, nr, nw;
 429
 430	buf[0] = reg;
 431	tries = 0;
 432	for (;;) {
 433		nw = i2c_master_send(fcu, buf, 1);
 434		if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
 435			break;
 436		msleep(10);
 437		++tries;
 438	}
 439	if (nw <= 0) {
 440		printk(KERN_ERR "Failure writing address to FCU: %d", nw);
 441		return -EIO;
 442	}
 443	tries = 0;
 444	for (;;) {
 445		nr = i2c_master_recv(fcu, buf, nb);
 446		if (nr > 0 || (nr < 0 && nr != -ENODEV) || tries >= 100)
 447			break;
 448		msleep(10);
 449		++tries;
 450	}
 451	if (nr <= 0)
 452		printk(KERN_ERR "Failure reading data from FCU: %d", nw);
 453	return nr;
 454}
 455
 456static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
 457{
 458	int tries, nw;
 459	unsigned char buf[16];
 460
 461	buf[0] = reg;
 462	memcpy(buf+1, ptr, nb);
 463	++nb;
 464	tries = 0;
 465	for (;;) {
 466		nw = i2c_master_send(fcu, buf, nb);
 467		if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
 468			break;
 469		msleep(10);
 470		++tries;
 471	}
 472	if (nw < 0)
 473		printk(KERN_ERR "Failure writing to FCU: %d", nw);
 474	return nw;
 475}
 476
 477static int start_fcu(void)
 478{
 479	unsigned char buf = 0xff;
 480	int rc;
 481
 482	rc = fan_write_reg(0xe, &buf, 1);
 483	if (rc < 0)
 484		return -EIO;
 485	rc = fan_write_reg(0x2e, &buf, 1);
 486	if (rc < 0)
 487		return -EIO;
 488	rc = fan_read_reg(0, &buf, 1);
 489	if (rc < 0)
 490		return -EIO;
 491	fcu_rpm_shift = (buf == 1) ? 2 : 3;
 492	printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
 493	       fcu_rpm_shift);
 494
 495	return 0;
 496}
 497
 498static int set_rpm_fan(int fan_index, int rpm)
 499{
 500	unsigned char buf[2];
 501	int rc, id, min, max;
 502
 503	if (fcu_fans[fan_index].type != FCU_FAN_RPM)
 504		return -EINVAL;
 505	id = fcu_fans[fan_index].id; 
 506	if (id == FCU_FAN_ABSENT_ID)
 507		return -EINVAL;
 508
 509	min = 2400 >> fcu_rpm_shift;
 510	max = 56000 >> fcu_rpm_shift;
 511
 512	if (rpm < min)
 513		rpm = min;
 514	else if (rpm > max)
 515		rpm = max;
 516	buf[0] = rpm >> (8 - fcu_rpm_shift);
 517	buf[1] = rpm << fcu_rpm_shift;
 518	rc = fan_write_reg(0x10 + (id * 2), buf, 2);
 519	if (rc < 0)
 520		return -EIO;
 521	return 0;
 522}
 523
 524static int get_rpm_fan(int fan_index, int programmed)
 525{
 526	unsigned char failure;
 527	unsigned char active;
 528	unsigned char buf[2];
 529	int rc, id, reg_base;
 530
 531	if (fcu_fans[fan_index].type != FCU_FAN_RPM)
 532		return -EINVAL;
 533	id = fcu_fans[fan_index].id; 
 534	if (id == FCU_FAN_ABSENT_ID)
 535		return -EINVAL;
 536
 537	rc = fan_read_reg(0xb, &failure, 1);
 538	if (rc != 1)
 539		return -EIO;
 540	if ((failure & (1 << id)) != 0)
 541		return -EFAULT;
 542	rc = fan_read_reg(0xd, &active, 1);
 543	if (rc != 1)
 544		return -EIO;
 545	if ((active & (1 << id)) == 0)
 546		return -ENXIO;
 547
 548	/* Programmed value or real current speed */
 549	reg_base = programmed ? 0x10 : 0x11;
 550	rc = fan_read_reg(reg_base + (id * 2), buf, 2);
 551	if (rc != 2)
 552		return -EIO;
 553
 554	return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
 555}
 556
 557static int set_pwm_fan(int fan_index, int pwm)
 558{
 559	unsigned char buf[2];
 560	int rc, id;
 561
 562	if (fcu_fans[fan_index].type != FCU_FAN_PWM)
 563		return -EINVAL;
 564	id = fcu_fans[fan_index].id; 
 565	if (id == FCU_FAN_ABSENT_ID)
 566		return -EINVAL;
 567
 568	if (pwm < 10)
 569		pwm = 10;
 570	else if (pwm > 100)
 571		pwm = 100;
 572	pwm = (pwm * 2559) / 1000;
 573	buf[0] = pwm;
 574	rc = fan_write_reg(0x30 + (id * 2), buf, 1);
 575	if (rc < 0)
 576		return rc;
 577	return 0;
 578}
 579
 580static int get_pwm_fan(int fan_index)
 581{
 582	unsigned char failure;
 583	unsigned char active;
 584	unsigned char buf[2];
 585	int rc, id;
 586
 587	if (fcu_fans[fan_index].type != FCU_FAN_PWM)
 588		return -EINVAL;
 589	id = fcu_fans[fan_index].id; 
 590	if (id == FCU_FAN_ABSENT_ID)
 591		return -EINVAL;
 592
 593	rc = fan_read_reg(0x2b, &failure, 1);
 594	if (rc != 1)
 595		return -EIO;
 596	if ((failure & (1 << id)) != 0)
 597		return -EFAULT;
 598	rc = fan_read_reg(0x2d, &active, 1);
 599	if (rc != 1)
 600		return -EIO;
 601	if ((active & (1 << id)) == 0)
 602		return -ENXIO;
 603
 604	/* Programmed value or real current speed */
 605	rc = fan_read_reg(0x30 + (id * 2), buf, 1);
 606	if (rc != 1)
 607		return -EIO;
 608
 609	return (buf[0] * 1000) / 2559;
 610}
 611
 612static void tickle_fcu(void)
 613{
 614	int pwm;
 615
 616	pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
 617
 618	DBG("FCU Tickle, slots fan is: %d\n", pwm);
 619	if (pwm < 0)
 620		pwm = 100;
 621
 622	if (!rackmac) {
 623		pwm = SLOTS_FAN_DEFAULT_PWM;
 624	} else if (pwm < SLOTS_PID_OUTPUT_MIN)
 625		pwm = SLOTS_PID_OUTPUT_MIN;
 626
 627	/* That is hopefully enough to make the FCU happy */
 628	set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
 629}
 630
 631
 632/*
 633 * Utility routine to read the CPU calibration EEPROM data
 634 * from the device-tree
 635 */
 636static int read_eeprom(int cpu, struct mpu_data *out)
 637{
 638	struct device_node *np;
 639	char nodename[64];
 640	const u8 *data;
 641	int len;
 642
 643	/* prom.c routine for finding a node by path is a bit brain dead
 644	 * and requires exact @xxx unit numbers. This is a bit ugly but
 645	 * will work for these machines
 646	 */
 647	sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
 648	np = of_find_node_by_path(nodename);
 649	if (np == NULL) {
 650		printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
 651		return -ENODEV;
 652	}
 653	data = of_get_property(np, "cpuid", &len);
 654	if (data == NULL) {
 655		printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
 656		of_node_put(np);
 657		return -ENODEV;
 658	}
 659	memcpy(out, data, sizeof(struct mpu_data));
 660	of_node_put(np);
 661	
 662	return 0;
 663}
 664
 665static void fetch_cpu_pumps_minmax(void)
 666{
 667	struct cpu_pid_state *state0 = &processor_state[0];
 668	struct cpu_pid_state *state1 = &processor_state[1];
 669	u16 pump_min = 0, pump_max = 0xffff;
 670	u16 tmp[4];
 671
 672	/* Try to fetch pumps min/max infos from eeprom */
 673
 674	memcpy(&tmp, &state0->mpu.processor_part_num, 8);
 675	if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
 676		pump_min = max(pump_min, tmp[0]);
 677		pump_max = min(pump_max, tmp[1]);
 678	}
 679	if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
 680		pump_min = max(pump_min, tmp[2]);
 681		pump_max = min(pump_max, tmp[3]);
 682	}
 683
 684	/* Double check the values, this _IS_ needed as the EEPROM on
 685	 * some dual 2.5Ghz G5s seem, at least, to have both min & max
 686	 * same to the same value ... (grrrr)
 687	 */
 688	if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
 689		pump_min = CPU_PUMP_OUTPUT_MIN;
 690		pump_max = CPU_PUMP_OUTPUT_MAX;
 691	}
 692
 693	state0->pump_min = state1->pump_min = pump_min;
 694	state0->pump_max = state1->pump_max = pump_max;
 695}
 696
 697/* 
 698 * Now, unfortunately, sysfs doesn't give us a nice void * we could
 699 * pass around to the attribute functions, so we don't really have
 700 * choice but implement a bunch of them...
 701 *
 702 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
 703 * the input twice... I accept patches :)
 704 */
 705#define BUILD_SHOW_FUNC_FIX(name, data)				\
 706static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
 707{								\
 708	ssize_t r;						\
 709	mutex_lock(&driver_lock);					\
 710	r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data));	\
 711	mutex_unlock(&driver_lock);					\
 712	return r;						\
 713}
 714#define BUILD_SHOW_FUNC_INT(name, data)				\
 715static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
 716{								\
 717	return sprintf(buf, "%d", data);			\
 718}
 719
 720BUILD_SHOW_FUNC_FIX(cpu0_temperature, processor_state[0].last_temp)
 721BUILD_SHOW_FUNC_FIX(cpu0_voltage, processor_state[0].voltage)
 722BUILD_SHOW_FUNC_FIX(cpu0_current, processor_state[0].current_a)
 723BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, processor_state[0].rpm)
 724BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, processor_state[0].intake_rpm)
 725
 726BUILD_SHOW_FUNC_FIX(cpu1_temperature, processor_state[1].last_temp)
 727BUILD_SHOW_FUNC_FIX(cpu1_voltage, processor_state[1].voltage)
 728BUILD_SHOW_FUNC_FIX(cpu1_current, processor_state[1].current_a)
 729BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, processor_state[1].rpm)
 730BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, processor_state[1].intake_rpm)
 731
 732BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
 733BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
 734
 735BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
 736BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
 737
 738BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
 739BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
 740
 741BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
 742
 743static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
 744static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
 745static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
 746static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
 747static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
 748
 749static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
 750static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
 751static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
 752static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
 753static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
 754
 755static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
 756static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
 757
 758static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
 759static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
 760
 761static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
 762static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
 763
 764static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
 765
 766/*
 767 * CPUs fans control loop
 768 */
 769
 770static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
 771{
 772	s32 ltemp, volts, amps;
 773	int index, rc = 0;
 774
 775	/* Default (in case of error) */
 776	*temp = state->cur_temp;
 777	*power = state->cur_power;
 778
 779	if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
 780		index = (state->index == 0) ?
 781			CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
 782	else
 783		index = (state->index == 0) ?
 784			CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
 785
 786	/* Read current fan status */
 787	rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
 788	if (rc < 0) {
 789		/* XXX What do we do now ? Nothing for now, keep old value, but
 790		 * return error upstream
 791		 */
 792		DBG("  cpu %d, fan reading error !\n", state->index);
 793	} else {
 794		state->rpm = rc;
 795		DBG("  cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
 796	}
 797
 798	/* Get some sensor readings and scale it */
 799	ltemp = read_smon_adc(state, 1);
 800	if (ltemp == -1) {
 801		/* XXX What do we do now ? */
 802		state->overtemp++;
 803		if (rc == 0)
 804			rc = -EIO;
 805		DBG("  cpu %d, temp reading error !\n", state->index);
 806	} else {
 807		/* Fixup temperature according to diode calibration
 808		 */
 809		DBG("  cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
 810		    state->index,
 811		    ltemp, state->mpu.mdiode, state->mpu.bdiode);
 812		*temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
 813		state->last_temp = *temp;
 814		DBG("  temp: %d.%03d\n", FIX32TOPRINT((*temp)));
 815	}
 816
 817	/*
 818	 * Read voltage & current and calculate power
 819	 */
 820	volts = read_smon_adc(state, 3);
 821	amps = read_smon_adc(state, 4);
 822
 823	/* Scale voltage and current raw sensor values according to fixed scales
 824	 * obtained in Darwin and calculate power from I and V
 825	 */
 826	volts *= ADC_CPU_VOLTAGE_SCALE;
 827	amps *= ADC_CPU_CURRENT_SCALE;
 828	*power = (((u64)volts) * ((u64)amps)) >> 16;
 829	state->voltage = volts;
 830	state->current_a = amps;
 831	state->last_power = *power;
 832
 833	DBG("  cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
 834	    state->index, FIX32TOPRINT(state->current_a),
 835	    FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
 836
 837	return 0;
 838}
 839
 840static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
 841{
 842	s32 power_target, integral, derivative, proportional, adj_in_target, sval;
 843	s64 integ_p, deriv_p, prop_p, sum; 
 844	int i;
 845
 846	/* Calculate power target value (could be done once for all)
 847	 * and convert to a 16.16 fp number
 848	 */
 849	power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
 850	DBG("  power target: %d.%03d, error: %d.%03d\n",
 851	    FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
 852
 853	/* Store temperature and power in history array */
 854	state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
 855	state->temp_history[state->cur_temp] = temp;
 856	state->cur_power = (state->cur_power + 1) % state->count_power;
 857	state->power_history[state->cur_power] = power;
 858	state->error_history[state->cur_power] = power_target - power;
 859	
 860	/* If first loop, fill the history table */
 861	if (state->first) {
 862		for (i = 0; i < (state->count_power - 1); i++) {
 863			state->cur_power = (state->cur_power + 1) % state->count_power;
 864			state->power_history[state->cur_power] = power;
 865			state->error_history[state->cur_power] = power_target - power;
 866		}
 867		for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
 868			state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
 869			state->temp_history[state->cur_temp] = temp;			
 870		}
 871		state->first = 0;
 872	}
 873
 874	/* Calculate the integral term normally based on the "power" values */
 875	sum = 0;
 876	integral = 0;
 877	for (i = 0; i < state->count_power; i++)
 878		integral += state->error_history[i];
 879	integral *= CPU_PID_INTERVAL;
 880	DBG("  integral: %08x\n", integral);
 881
 882	/* Calculate the adjusted input (sense value).
 883	 *   G_r is 12.20
 884	 *   integ is 16.16
 885	 *   so the result is 28.36
 886	 *
 887	 * input target is mpu.ttarget, input max is mpu.tmax
 888	 */
 889	integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
 890	DBG("   integ_p: %d\n", (int)(integ_p >> 36));
 891	sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
 892	adj_in_target = (state->mpu.ttarget << 16);
 893	if (adj_in_target > sval)
 894		adj_in_target = sval;
 895	DBG("   adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
 896	    state->mpu.ttarget);
 897
 898	/* Calculate the derivative term */
 899	derivative = state->temp_history[state->cur_temp] -
 900		state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
 901				    % CPU_TEMP_HISTORY_SIZE];
 902	derivative /= CPU_PID_INTERVAL;
 903	deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
 904	DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
 905	sum += deriv_p;
 906
 907	/* Calculate the proportional term */
 908	proportional = temp - adj_in_target;
 909	prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
 910	DBG("   prop_p: %d\n", (int)(prop_p >> 36));
 911	sum += prop_p;
 912
 913	/* Scale sum */
 914	sum >>= 36;
 915
 916	DBG("   sum: %d\n", (int)sum);
 917	state->rpm += (s32)sum;
 918}
 919
 920static void do_monitor_cpu_combined(void)
 921{
 922	struct cpu_pid_state *state0 = &processor_state[0];
 923	struct cpu_pid_state *state1 = &processor_state[1];
 924	s32 temp0, power0, temp1, power1;
 925	s32 temp_combi, power_combi;
 926	int rc, intake, pump;
 927
 928	rc = do_read_one_cpu_values(state0, &temp0, &power0);
 929	if (rc < 0) {
 930		/* XXX What do we do now ? */
 931	}
 932	state1->overtemp = 0;
 933	rc = do_read_one_cpu_values(state1, &temp1, &power1);
 934	if (rc < 0) {
 935		/* XXX What do we do now ? */
 936	}
 937	if (state1->overtemp)
 938		state0->overtemp++;
 939
 940	temp_combi = max(temp0, temp1);
 941	power_combi = max(power0, power1);
 942
 943	/* Check tmax, increment overtemp if we are there. At tmax+8, we go
 944	 * full blown immediately and try to trigger a shutdown
 945	 */
 946	if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
 947		printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
 948		       temp_combi >> 16);
 949		state0->overtemp += CPU_MAX_OVERTEMP / 4;
 950	} else if (temp_combi > (state0->mpu.tmax << 16)) {
 951		state0->overtemp++;
 952		printk(KERN_WARNING "Temperature %d above max %d. overtemp %d\n",
 953		       temp_combi >> 16, state0->mpu.tmax, state0->overtemp);
 954	} else {
 955		if (state0->overtemp)
 956			printk(KERN_WARNING "Temperature back down to %d\n",
 957			       temp_combi >> 16);
 958		state0->overtemp = 0;
 959	}
 960	if (state0->overtemp >= CPU_MAX_OVERTEMP)
 961		critical_state = 1;
 962	if (state0->overtemp > 0) {
 963		state0->rpm = state0->mpu.rmaxn_exhaust_fan;
 964		state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
 965		pump = state0->pump_max;
 966		goto do_set_fans;
 967	}
 968
 969	/* Do the PID */
 970	do_cpu_pid(state0, temp_combi, power_combi);
 971
 972	/* Range check */
 973	state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
 974	state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
 975
 976	/* Calculate intake fan speed */
 977	intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
 978	intake = max(intake, (int)state0->mpu.rminn_intake_fan);
 979	intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
 980	state0->intake_rpm = intake;
 981
 982	/* Calculate pump speed */
 983	pump = (state0->rpm * state0->pump_max) /
 984		state0->mpu.rmaxn_exhaust_fan;
 985	pump = min(pump, state0->pump_max);
 986	pump = max(pump, state0->pump_min);
 987	
 988 do_set_fans:
 989	/* We copy values from state 0 to state 1 for /sysfs */
 990	state1->rpm = state0->rpm;
 991	state1->intake_rpm = state0->intake_rpm;
 992
 993	DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
 994	    state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
 995
 996	/* We should check for errors, shouldn't we ? But then, what
 997	 * do we do once the error occurs ? For FCU notified fan
 998	 * failures (-EFAULT) we probably want to notify userland
 999	 * some way...
1000	 */
1001	set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1002	set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1003	set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1004	set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
1005
1006	if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1007		set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
1008	if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
1009		set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
1010}
1011
1012static void do_monitor_cpu_split(struct cpu_pid_state *state)
1013{
1014	s32 temp, power;
1015	int rc, intake;
1016
1017	/* Read current fan status */
1018	rc = do_read_one_cpu_values(state, &temp, &power);
1019	if (rc < 0) {
1020		/* XXX What do we do now ? */
1021	}
1022
1023	/* Check tmax, increment overtemp if we are there. At tmax+8, we go
1024	 * full blown immediately and try to trigger a shutdown
1025	 */
1026	if (temp >= ((state->mpu.tmax + 8) << 16)) {
1027		printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1028		       " (%d) !\n",
1029		       state->index, temp >> 16);
1030		state->overtemp += CPU_MAX_OVERTEMP / 4;
1031	} else if (temp > (state->mpu.tmax << 16)) {
1032		state->overtemp++;
1033		printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1034		       state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1035	} else {
1036		if (state->overtemp)
1037			printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1038			       state->index, temp >> 16);
1039		state->overtemp = 0;
1040	}
1041	if (state->overtemp >= CPU_MAX_OVERTEMP)
1042		critical_state = 1;
1043	if (state->overtemp > 0) {
1044		state->rpm = state->mpu.rmaxn_exhaust_fan;
1045		state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
1046		goto do_set_fans;
1047	}
1048
1049	/* Do the PID */
1050	do_cpu_pid(state, temp, power);
1051
1052	/* Range check */
1053	state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
1054	state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
1055
1056	/* Calculate intake fan */
1057	intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
1058	intake = max(intake, (int)state->mpu.rminn_intake_fan);
1059	intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
1060	state->intake_rpm = intake;
1061
1062 do_set_fans:
1063	DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
1064	    state->index, (int)state->rpm, intake, state->overtemp);
1065
1066	/* We should check for errors, shouldn't we ? But then, what
1067	 * do we do once the error occurs ? For FCU notified fan
1068	 * failures (-EFAULT) we probably want to notify userland
1069	 * some way...
1070	 */
1071	if (state->index == 0) {
1072		set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
1073		set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
1074	} else {
1075		set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
1076		set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
1077	}
1078}
1079
1080static void do_monitor_cpu_rack(struct cpu_pid_state *state)
1081{
1082	s32 temp, power, fan_min;
1083	int rc;
1084
1085	/* Read current fan status */
1086	rc = do_read_one_cpu_values(state, &temp, &power);
1087	if (rc < 0) {
1088		/* XXX What do we do now ? */
1089	}
1090
1091	/* Check tmax, increment overtemp if we are there. At tmax+8, we go
1092	 * full blown immediately and try to trigger a shutdown
1093	 */
1094	if (temp >= ((state->mpu.tmax + 8) << 16)) {
1095		printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
1096		       " (%d) !\n",
1097		       state->index, temp >> 16);
1098		state->overtemp = CPU_MAX_OVERTEMP / 4;
1099	} else if (temp > (state->mpu.tmax << 16)) {
1100		state->overtemp++;
1101		printk(KERN_WARNING "CPU %d temperature %d above max %d. overtemp %d\n",
1102		       state->index, temp >> 16, state->mpu.tmax, state->overtemp);
1103	} else {
1104		if (state->overtemp)
1105			printk(KERN_WARNING "CPU %d temperature back down to %d\n",
1106			       state->index, temp >> 16);
1107		state->overtemp = 0;
1108	}
1109	if (state->overtemp >= CPU_MAX_OVERTEMP)
1110		critical_state = 1;
1111	if (state->overtemp > 0) {
1112		state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
1113		goto do_set_fans;
1114	}
1115
1116	/* Do the PID */
1117	do_cpu_pid(state, temp, power);
1118
1119	/* Check clamp from dimms */
1120	fan_min = dimm_output_clamp;
1121	fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
1122
1123	DBG(" CPU min mpu = %d, min dimm = %d\n",
1124	    state->mpu.rminn_intake_fan, dimm_output_clamp);
1125
1126	state->rpm = max(state->rpm, (int)fan_min);
1127	state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
1128	state->intake_rpm = state->rpm;
1129
1130 do_set_fans:
1131	DBG("** CPU %d RPM: %d overtemp: %d\n",
1132	    state->index, (int)state->rpm, state->overtemp);
1133
1134	/* We should check for errors, shouldn't we ? But then, what
1135	 * do we do once the error occurs ? For FCU notified fan
1136	 * failures (-EFAULT) we probably want to notify userland
1137	 * some way...
1138	 */
1139	if (state->index == 0) {
1140		set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
1141		set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
1142		set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
1143	} else {
1144		set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
1145		set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
1146		set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
1147	}
1148}
1149
1150/*
1151 * Initialize the state structure for one CPU control loop
1152 */
1153static int init_processor_state(struct cpu_pid_state *state, int index)
1154{
1155	int err;
1156
1157	state->index = index;
1158	state->first = 1;
1159	state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
1160	state->overtemp = 0;
1161	state->adc_config = 0x00;
1162
1163
1164	if (index == 0)
1165		state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
1166	else if (index == 1)
1167		state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
1168	if (state->monitor == NULL)
1169		goto fail;
1170
1171	if (read_eeprom(index, &state->mpu))
1172		goto fail;
1173
1174	state->count_power = state->mpu.tguardband;
1175	if (state->count_power > CPU_POWER_HISTORY_SIZE) {
1176		printk(KERN_WARNING "Warning ! too many power history slots\n");
1177		state->count_power = CPU_POWER_HISTORY_SIZE;
1178	}
1179	DBG("CPU %d Using %d power history entries\n", index, state->count_power);
1180
1181	if (index == 0) {
1182		err = device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1183		err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1184		err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
1185		err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1186		err |= device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1187	} else {
1188		err = device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1189		err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1190		err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
1191		err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1192		err |= device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1193	}
1194	if (err)
1195		printk(KERN_WARNING "Failed to create some of the attribute"
1196			"files for CPU %d\n", index);
1197
1198	return 0;
1199 fail:
1200	state->monitor = NULL;
1201	
1202	return -ENODEV;
1203}
1204
1205/*
1206 * Dispose of the state data for one CPU control loop
1207 */
1208static void dispose_processor_state(struct cpu_pid_state *state)
1209{
1210	if (state->monitor == NULL)
1211		return;
1212
1213	if (state->index == 0) {
1214		device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
1215		device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
1216		device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
1217		device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
1218		device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
1219	} else {
1220		device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
1221		device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
1222		device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
1223		device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
1224		device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
1225	}
1226
1227	state->monitor = NULL;
1228}
1229
1230/*
1231 * Motherboard backside & U3 heatsink fan control loop
1232 */
1233static void do_monitor_backside(struct backside_pid_state *state)
1234{
1235	s32 temp, integral, derivative, fan_min;
1236	s64 integ_p, deriv_p, prop_p, sum; 
1237	int i, rc;
1238
1239	if (--state->ticks != 0)
1240		return;
1241	state->ticks = backside_params.interval;
1242
1243	DBG("backside:\n");
1244
1245	/* Check fan status */
1246	rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
1247	if (rc < 0) {
1248		printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
1249		/* XXX What do we do now ? */
1250	} else
1251		state->pwm = rc;
1252	DBG("  current pwm: %d\n", state->pwm);
1253
1254	/* Get some sensor readings */
1255	temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
1256	state->last_temp = temp;
1257	DBG("  temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1258	    FIX32TOPRINT(backside_params.input_target));
1259
1260	/* Store temperature and error in history array */
1261	state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
1262	state->sample_history[state->cur_sample] = temp;
1263	state->error_history[state->cur_sample] = temp - backside_params.input_target;
1264	
1265	/* If first loop, fill the history table */
1266	if (state->first) {
1267		for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
1268			state->cur_sample = (state->cur_sample + 1) %
1269				BACKSIDE_PID_HISTORY_SIZE;
1270			state->sample_history[state->cur_sample] = temp;
1271			state->error_history[state->cur_sample] =
1272				temp - backside_params.input_target;
1273		}
1274		state->first = 0;
1275	}
1276
1277	/* Calculate the integral term */
1278	sum = 0;
1279	integral = 0;
1280	for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
1281		integral += state->error_history[i];
1282	integral *= backside_params.interval;
1283	DBG("  integral: %08x\n", integral);
1284	integ_p = ((s64)backside_params.G_r) * (s64)integral;
1285	DBG("   integ_p: %d\n", (int)(integ_p >> 36));
1286	sum += integ_p;
1287
1288	/* Calculate the derivative term */
1289	derivative = state->error_history[state->cur_sample] -
1290		state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
1291				    % BACKSIDE_PID_HISTORY_SIZE];
1292	derivative /= backside_params.interval;
1293	deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
1294	DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
1295	sum += deriv_p;
1296
1297	/* Calculate the proportional term */
1298	prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
1299	DBG("   prop_p: %d\n", (int)(prop_p >> 36));
1300	sum += prop_p;
1301
1302	/* Scale sum */
1303	sum >>= 36;
1304
1305	DBG("   sum: %d\n", (int)sum);
1306	if (backside_params.additive)
1307		state->pwm += (s32)sum;
1308	else
1309		state->pwm = sum;
1310
1311	/* Check for clamp */
1312	fan_min = (dimm_output_clamp * 100) / 14000;
1313	fan_min = max(fan_min, backside_params.output_min);
1314
1315	state->pwm = max(state->pwm, fan_min);
1316	state->pwm = min(state->pwm, backside_params.output_max);
1317
1318	DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
1319	set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
1320}
1321
1322/*
1323 * Initialize the state structure for the backside fan control loop
1324 */
1325static int init_backside_state(struct backside_pid_state *state)
1326{
1327	struct device_node *u3;
1328	int u3h = 1; /* conservative by default */
1329	int err;
1330
1331	/*
1332	 * There are different PID params for machines with U3 and machines
1333	 * with U3H, pick the right ones now
1334	 */
1335	u3 = of_find_node_by_path("/u3@0,f8000000");
1336	if (u3 != NULL) {
1337		const u32 *vers = of_get_property(u3, "device-rev", NULL);
1338		if (vers)
1339			if (((*vers) & 0x3f) < 0x34)
1340				u3h = 0;
1341		of_node_put(u3);
1342	}
1343
1344	if (rackmac) {
1345		backside_params.G_d = BACKSIDE_PID_RACK_G_d;
1346		backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
1347		backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1348		backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
1349		backside_params.G_p = BACKSIDE_PID_RACK_G_p;
1350		backside_params.G_r = BACKSIDE_PID_G_r;
1351		backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1352		backside_params.additive = 0;
1353	} else if (u3h) {
1354		backside_params.G_d = BACKSIDE_PID_U3H_G_d;
1355		backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
1356		backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
1357		backside_params.interval = BACKSIDE_PID_INTERVAL;
1358		backside_params.G_p = BACKSIDE_PID_G_p;
1359		backside_params.G_r = BACKSIDE_PID_G_r;
1360		backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1361		backside_params.additive = 1;
1362	} else {
1363		backside_params.G_d = BACKSIDE_PID_U3_G_d;
1364		backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
1365		backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
1366		backside_params.interval = BACKSIDE_PID_INTERVAL;
1367		backside_params.G_p = BACKSIDE_PID_G_p;
1368		backside_params.G_r = BACKSIDE_PID_G_r;
1369		backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
1370		backside_params.additive = 1;
1371	}
1372
1373	state->ticks = 1;
1374	state->first = 1;
1375	state->pwm = 50;
1376
1377	state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
1378	if (state->monitor == NULL)
1379		return -ENODEV;
1380
1381	err = device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
1382	err |= device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1383	if (err)
1384		printk(KERN_WARNING "Failed to create attribute file(s)"
1385			" for backside fan\n");
1386
1387	return 0;
1388}
1389
1390/*
1391 * Dispose of the state data for the backside control loop
1392 */
1393static void dispose_backside_state(struct backside_pid_state *state)
1394{
1395	if (state->monitor == NULL)
1396		return;
1397
1398	device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
1399	device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
1400
1401	state->monitor = NULL;
1402}
1403 
1404/*
1405 * Drives bay fan control loop
1406 */
1407static void do_monitor_drives(struct drives_pid_state *state)
1408{
1409	s32 temp, integral, derivative;
1410	s64 integ_p, deriv_p, prop_p, sum; 
1411	int i, rc;
1412
1413	if (--state->ticks != 0)
1414		return;
1415	state->ticks = DRIVES_PID_INTERVAL;
1416
1417	DBG("drives:\n");
1418
1419	/* Check fan status */
1420	rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
1421	if (rc < 0) {
1422		printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
1423		/* XXX What do we do now ? */
1424	} else
1425		state->rpm = rc;
1426	DBG("  current rpm: %d\n", state->rpm);
1427
1428	/* Get some sensor readings */
1429	temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1430						    DS1775_TEMP)) << 8;
1431	state->last_temp = temp;
1432	DBG("  temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1433	    FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
1434
1435	/* Store temperature and error in history array */
1436	state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
1437	state->sample_history[state->cur_sample] = temp;
1438	state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
1439	
1440	/* If first loop, fill the history table */
1441	if (state->first) {
1442		for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
1443			state->cur_sample = (state->cur_sample + 1) %
1444				DRIVES_PID_HISTORY_SIZE;
1445			state->sample_history[state->cur_sample] = temp;
1446			state->error_history[state->cur_sample] =
1447				temp - DRIVES_PID_INPUT_TARGET;
1448		}
1449		state->first = 0;
1450	}
1451
1452	/* Calculate the integral term */
1453	sum = 0;
1454	integral = 0;
1455	for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
1456		integral += state->error_history[i];
1457	integral *= DRIVES_PID_INTERVAL;
1458	DBG("  integral: %08x\n", integral);
1459	integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
1460	DBG("   integ_p: %d\n", (int)(integ_p >> 36));
1461	sum += integ_p;
1462
1463	/* Calculate the derivative term */
1464	derivative = state->error_history[state->cur_sample] -
1465		state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
1466				    % DRIVES_PID_HISTORY_SIZE];
1467	derivative /= DRIVES_PID_INTERVAL;
1468	deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
1469	DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
1470	sum += deriv_p;
1471
1472	/* Calculate the proportional term */
1473	prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1474	DBG("   prop_p: %d\n", (int)(prop_p >> 36));
1475	sum += prop_p;
1476
1477	/* Scale sum */
1478	sum >>= 36;
1479
1480	DBG("   sum: %d\n", (int)sum);
1481	state->rpm += (s32)sum;
1482
1483	state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
1484	state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
1485
1486	DBG("** DRIVES RPM: %d\n", (int)state->rpm);
1487	set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
1488}
1489
1490/*
1491 * Initialize the state structure for the drives bay fan control loop
1492 */
1493static int init_drives_state(struct drives_pid_state *state)
1494{
1495	int err;
1496
1497	state->ticks = 1;
1498	state->first = 1;
1499	state->rpm = 1000;
1500
1501	state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
1502	if (state->monitor == NULL)
1503		return -ENODEV;
1504
1505	err = device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
1506	err |= device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1507	if (err)
1508		printk(KERN_WARNING "Failed to create attribute file(s)"
1509			" for drives bay fan\n");
1510
1511	return 0;
1512}
1513
1514/*
1515 * Dispose of the state data for the drives control loop
1516 */
1517static void dispose_drives_state(struct drives_pid_state *state)
1518{
1519	if (state->monitor == NULL)
1520		return;
1521
1522	device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
1523	device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
1524
1525	state->monitor = NULL;
1526}
1527
1528/*
1529 * DIMMs temp control loop
1530 */
1531static void do_monitor_dimms(struct dimm_pid_state *state)
1532{
1533	s32 temp, integral, derivative, fan_min;
1534	s64 integ_p, deriv_p, prop_p, sum;
1535	int i;
1536
1537	if (--state->ticks != 0)
1538		return;
1539	state->ticks = DIMM_PID_INTERVAL;
1540
1541	DBG("DIMM:\n");
1542
1543	DBG("  current value: %d\n", state->output);
1544
1545	temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
1546	if (temp < 0)
1547		return;
1548	temp <<= 16;
1549	state->last_temp = temp;
1550	DBG("  temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1551	    FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
1552
1553	/* Store temperature and error in history array */
1554	state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
1555	state->sample_history[state->cur_sample] = temp;
1556	state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
1557
1558	/* If first loop, fill the history table */
1559	if (state->first) {
1560		for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
1561			state->cur_sample = (state->cur_sample + 1) %
1562				DIMM_PID_HISTORY_SIZE;
1563			state->sample_history[state->cur_sample] = temp;
1564			state->error_history[state->cur_sample] =
1565				temp - DIMM_PID_INPUT_TARGET;
1566		}
1567		state->first = 0;
1568	}
1569
1570	/* Calculate the integral term */
1571	sum = 0;
1572	integral = 0;
1573	for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
1574		integral += state->error_history[i];
1575	integral *= DIMM_PID_INTERVAL;
1576	DBG("  integral: %08x\n", integral);
1577	integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
1578	DBG("   integ_p: %d\n", (int)(integ_p >> 36));
1579	sum += integ_p;
1580
1581	/* Calculate the derivative term */
1582	derivative = state->error_history[state->cur_sample] -
1583		state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
1584				    % DIMM_PID_HISTORY_SIZE];
1585	derivative /= DIMM_PID_INTERVAL;
1586	deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
1587	DBG("   deriv_p: %d\n", (int)(deriv_p >> 36));
1588	sum += deriv_p;
1589
1590	/* Calculate the proportional term */
1591	prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
1592	DBG("   prop_p: %d\n", (int)(prop_p >> 36));
1593	sum += prop_p;
1594
1595	/* Scale sum */
1596	sum >>= 36;
1597
1598	DBG("   sum: %d\n", (int)sum);
1599	state->output = (s32)sum;
1600	state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
1601	state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
1602	dimm_output_clamp = state->output;
1603
1604	DBG("** DIMM clamp value: %d\n", (int)state->output);
1605
1606	/* Backside PID is only every 5 seconds, force backside fan clamping now */
1607	fan_min = (dimm_output_clamp * 100) / 14000;
1608	fan_min = max(fan_min, backside_params.output_min);
1609	if (backside_state.pwm < fan_min) {
1610		backside_state.pwm = fan_min;
1611		DBG(" -> applying clamp to backside fan now: %d  !\n", fan_min);
1612		set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
1613	}
1614}
1615
1616/*
1617 * Initialize the state structure for the DIMM temp control loop
1618 */
1619static int init_dimms_state(struct dimm_pid_state *state)
1620{
1621	state->ticks = 1;
1622	state->first = 1;
1623	state->output = 4000;
1624
1625	state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
1626	if (state->monitor == NULL)
1627		return -ENODEV;
1628
1629	if (device_create_file(&of_dev->dev, &dev_attr_dimms_temperature))
1630		printk(KERN_WARNING "Failed to create attribute file"
1631			" for DIMM temperature\n");
1632
1633	return 0;
1634}
1635
1636/*
1637 * Dispose of the state data for the DIMM control loop
1638 */
1639static void dispose_dimms_state(struct dimm_pid_state *state)
1640{
1641	if (state->monitor == NULL)
1642		return;
1643
1644	device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
1645
1646	state->monitor = NULL;
1647}
1648
1649/*
1650 * Slots fan control loop
1651 */
1652static void do_monitor_slots(struct slots_pid_state *state)
1653{
1654	s32 temp, integral, derivative;
1655	s64 integ_p, deriv_p, prop_p, sum;
1656	int i, rc;
1657
1658	if (--state->ticks != 0)
1659		return;
1660	state->ticks = SLOTS_PID_INTERVAL;
1661
1662	DBG("slots:\n");
1663
1664	/* Check fan status */
1665	rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
1666	if (rc < 0) {
1667		printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
1668		/* XXX What do we do now ? */
1669	} else
1670		state->pwm = rc;
1671	DBG("  current pwm: %d\n", state->pwm);
1672
1673	/* Get some sensor readings */
1674	temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
1675						    DS1775_TEMP)) << 8;
1676	state->last_temp = temp;
1677	DBG("  temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
1678	    FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
1679
1680	/* Store temperature and error in history array */
1681	state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
1682	state->sample_history[state->cur_sample] = temp;
1683	state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
1684
1685	/* If first loop, fill the history table */
1686	if (state->first) {
1687		for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
1688			state->cur_sample = (state->cur_sample + 1) %
1689				SLOTS_PID_HISTORY_SIZE;
1690			state->sample_history[state->cur_sample] = temp;
1691			state->error_history[state->cur_sample] =
1692				temp - SLOTS_PID_INPUT_TARGET;
1693		}
1694		state->first = 0;
1695	}
1696
1697	/* Calculate the integral term */
1698	sum = 0;
1699	integral = 0;
1700	for (i = 0; i < SLOTS_PI

Large files files are truncated, but you can click here to view the full file