pc87360.c 42.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 *  pc87360.c - Part of lm_sensors, Linux kernel modules
 *              for hardware monitoring
 *  Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
 *
 *  Copied from smsc47m1.c:
 *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Supports the following chips:
 *
 *  Chip        #vin    #fan    #pwm    #temp   devid
 *  PC87360     -       2       2       -       0xE1
 *  PC87363     -       2       2       -       0xE8
 *  PC87364     -       3       3       -       0xE4
 *  PC87365     11      3       3       2       0xE5
 *  PC87366     11      3       3       3-4     0xE9
 *
 *  This driver assumes that no more than one chip is present, and one of
 *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
41
#include <linux/i2c-isa.h>
L
Linus Torvalds 已提交
42
#include <linux/i2c-vid.h>
43 44
#include <linux/hwmon.h>
#include <linux/err.h>
L
Linus Torvalds 已提交
45 46 47
#include <asm/io.h>

static u8 devid;
48 49
static unsigned short address;
static unsigned short extra_isa[3];
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static u8 confreg[4];

enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };

static int init = 1;
module_param(init, int, 0);
MODULE_PARM_DESC(init,
 "Chip initialization level:\n"
 " 0: None\n"
 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
 " 3: Forcibly enable all voltage and temperature channels, including in9");

/*
 * Super-I/O registers and operations
 */

#define DEV	0x07	/* Register: Logical device select */
#define DEVID	0x20	/* Register: Device ID */
#define ACT	0x30	/* Register: Device activation */
#define BASE	0x60	/* Register: Base address */

#define FSCM	0x09	/* Logical device: fans */
#define VLM	0x0d	/* Logical device: voltages */
#define TMS	0x0e	/* Logical device: temperatures */
static const u8 logdev[3] = { FSCM, VLM, TMS };

#define LD_FAN		0
#define LD_IN		1
#define LD_TEMP		2

static inline void superio_outb(int sioaddr, int reg, int val)
{
	outb(reg, sioaddr);
	outb(val, sioaddr+1);
}

static inline int superio_inb(int sioaddr, int reg)
{
	outb(reg, sioaddr);
	return inb(sioaddr+1);
}

static inline void superio_exit(int sioaddr)
{
	outb(0x02, sioaddr);
	outb(0x02, sioaddr+1);
}

/*
 * Logical devices
 */

#define PC87360_EXTENT		0x10
#define PC87365_REG_BANK	0x09
#define NO_BANK			0xff

/*
 * Fan registers and conversions
 */

/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
#define PC87360_REG_PRESCALE(nr)	(0x00 + 2 * (nr))
#define PC87360_REG_PWM(nr)		(0x01 + 2 * (nr))
#define PC87360_REG_FAN_MIN(nr)		(0x06 + 3 * (nr))
#define PC87360_REG_FAN(nr)		(0x07 + 3 * (nr))
#define PC87360_REG_FAN_STATUS(nr)	(0x08 + 3 * (nr))

#define FAN_FROM_REG(val,div)		((val) == 0 ? 0: \
					 480000 / ((val)*(div)))
#define FAN_TO_REG(val,div)		((val) <= 100 ? 0 : \
					 480000 / ((val)*(div)))
#define FAN_DIV_FROM_REG(val)		(1 << ((val >> 5) & 0x03))
#define FAN_STATUS_FROM_REG(val)	((val) & 0x07)

#define FAN_CONFIG_MONITOR(val,nr)	(((val) >> (2 + nr * 3)) & 1)
#define FAN_CONFIG_CONTROL(val,nr)	(((val) >> (3 + nr * 3)) & 1)
#define FAN_CONFIG_INVERT(val,nr)	(((val) >> (4 + nr * 3)) & 1)

#define PWM_FROM_REG(val,inv)		((inv) ? 255 - (val) : (val))
static inline u8 PWM_TO_REG(int val, int inv)
{
	if (inv)
		val = 255 - val;
	if (val < 0)
		return 0;
	if (val > 255)
		return 255;
	return val;
}

/*
 * Voltage registers and conversions
 */

#define PC87365_REG_IN_CONVRATE		0x07
#define PC87365_REG_IN_CONFIG		0x08
#define PC87365_REG_IN			0x0B
#define PC87365_REG_IN_MIN		0x0D
#define PC87365_REG_IN_MAX		0x0C
#define PC87365_REG_IN_STATUS		0x0A
#define PC87365_REG_IN_ALARMS1		0x00
#define PC87365_REG_IN_ALARMS2		0x01
#define PC87365_REG_VID			0x06

#define IN_FROM_REG(val,ref)		(((val) * (ref) + 128) / 256)
#define IN_TO_REG(val,ref)		((val) < 0 ? 0 : \
					 (val)*256 >= (ref)*255 ? 255: \
					 ((val) * 256 + (ref)/2) / (ref))

/*
 * Temperature registers and conversions
 */

#define PC87365_REG_TEMP_CONFIG		0x08
#define PC87365_REG_TEMP		0x0B
#define PC87365_REG_TEMP_MIN		0x0D
#define PC87365_REG_TEMP_MAX		0x0C
#define PC87365_REG_TEMP_CRIT		0x0E
#define PC87365_REG_TEMP_STATUS		0x0A
#define PC87365_REG_TEMP_ALARMS		0x00

#define TEMP_FROM_REG(val)		((val) * 1000)
#define TEMP_TO_REG(val)		((val) < -55000 ? -55 : \
					 (val) > 127000 ? 127 : \
					 (val) < 0 ? ((val) - 500) / 1000 : \
					 ((val) + 500) / 1000)

/*
 * Client data (each client gets its own)
 */

struct pc87360_data {
	struct i2c_client client;
184
	struct class_device *class_dev;
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	struct semaphore lock;
	struct semaphore update_lock;
	char valid;		/* !=0 if following fields are valid */
	unsigned long last_updated;	/* In jiffies */

	int address[3];

	u8 fannr, innr, tempnr;

	u8 fan[3];		/* Register value */
	u8 fan_min[3];		/* Register value */
	u8 fan_status[3];	/* Register value */
	u8 pwm[3];		/* Register value */
	u16 fan_conf;		/* Configuration register values, combined */

	u16 in_vref;		/* 1 mV/bit */
	u8 in[14];		/* Register value */
	u8 in_min[14];		/* Register value */
	u8 in_max[14];		/* Register value */
	u8 in_crit[3];		/* Register value */
	u8 in_status[14];	/* Register value */
	u16 in_alarms;		/* Register values, combined, masked */
	u8 vid_conf;		/* Configuration register value */
	u8 vrm;
	u8 vid;			/* Register value */

	s8 temp[3];		/* Register value */
	s8 temp_min[3];		/* Register value */
	s8 temp_max[3];		/* Register value */
	s8 temp_crit[3];	/* Register value */
	u8 temp_status[3];	/* Register value */
	u8 temp_alarms;		/* Register value, masked */
};

/*
 * Functions declaration
 */

223
static int pc87360_detect(struct i2c_adapter *adapter);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static int pc87360_detach_client(struct i2c_client *client);

static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
			      u8 reg);
static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
				u8 reg, u8 value);
static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
static struct pc87360_data *pc87360_update_device(struct device *dev);

/*
 * Driver data (common to all clients)
 */

static struct i2c_driver pc87360_driver = {
	.owner		= THIS_MODULE,
	.name		= "pc87360",
240
	.attach_adapter	= pc87360_detect,
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	.detach_client	= pc87360_detach_client,
};

/*
 * Sysfs stuff
 */

static ssize_t set_fan_min(struct device *dev, const char *buf,
	size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pc87360_data *data = i2c_get_clientdata(client);
	long fan_min = simple_strtol(buf, NULL, 10);

	down(&data->update_lock);
	fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));

	/* If it wouldn't fit, change clock divisor */
	while (fan_min > 255
	    && (data->fan_status[nr] & 0x60) != 0x60) {
		fan_min >>= 1;
		data->fan[nr] >>= 1;
		data->fan_status[nr] += 0x20;
	}
	data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
			    data->fan_min[nr]);

	/* Write new divider, preserve alarm bits */
	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
			    data->fan_status[nr] & 0xF9);
	up(&data->update_lock);

	return count;
}

#define show_and_set_fan(offset) \
278
static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
279 280 281 282 283
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
		       FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
} \
284
static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
285 286 287 288 289
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
		       FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
} \
290
static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
291 292 293 294 295
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", \
		       FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
} \
296
static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
297 298 299 300 301
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", \
		       FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
} \
302
static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	size_t count) \
{ \
	return set_fan_min(dev, buf, count, offset-1); \
} \
static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
	show_fan##offset##_input, NULL); \
static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
	show_fan##offset##_min, set_fan##offset##_min); \
static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
	show_fan##offset##_div, NULL); \
static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
	show_fan##offset##_status, NULL);
show_and_set_fan(1)
show_and_set_fan(2)
show_and_set_fan(3)

#define show_and_set_pwm(offset) \
320
static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
321 322 323 324 325 326 327
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", \
		       PWM_FROM_REG(data->pwm[offset-1], \
				    FAN_CONFIG_INVERT(data->fan_conf, \
						      offset-1))); \
} \
328
static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->pwm[offset-1] = PWM_TO_REG(val, \
			      FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
			    data->pwm[offset-1]); \
	up(&data->update_lock); \
	return count; \
} \
static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
	show_pwm##offset, set_pwm##offset);
show_and_set_pwm(1)
show_and_set_pwm(2)
show_and_set_pwm(3)

#define show_and_set_in(offset) \
350
static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
351 352 353 354 355
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
		       data->in_vref)); \
} \
356
static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
357 358 359 360 361
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
		       data->in_vref)); \
} \
362
static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
363 364 365 366 367
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
		       data->in_vref)); \
} \
368
static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
369 370 371 372
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", data->in_status[offset]); \
} \
373
static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
	pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
			    data->in_min[offset]); \
	up(&data->update_lock); \
	return count; \
} \
387
static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->in_max[offset] = IN_TO_REG(val, \
			       data->in_vref); \
	pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
			    data->in_max[offset]); \
	up(&data->update_lock); \
	return count; \
} \
static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
	show_in##offset##_input, NULL); \
static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
	show_in##offset##_min, set_in##offset##_min); \
static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
	show_in##offset##_max, set_in##offset##_max); \
static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
	show_in##offset##_status, NULL);
show_and_set_in(0)
show_and_set_in(1)
show_and_set_in(2)
show_and_set_in(3)
show_and_set_in(4)
show_and_set_in(5)
show_and_set_in(6)
show_and_set_in(7)
show_and_set_in(8)
show_and_set_in(9)
show_and_set_in(10)

#define show_and_set_therm(offset) \
423
static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
424 425 426 427 428
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
		       data->in_vref)); \
} \
429
static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
430 431 432 433 434
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
		       data->in_vref)); \
} \
435
static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
436 437 438 439 440
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
		       data->in_vref)); \
} \
441
static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
442 443 444 445 446
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
		       data->in_vref)); \
} \
447
static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
448 449 450 451
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%u\n", data->in_status[offset+7]); \
} \
452
static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
	pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
			    data->in_min[offset+7]); \
	up(&data->update_lock); \
	return count; \
} \
466
static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
	pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
			    data->in_max[offset+7]); \
	up(&data->update_lock); \
	return count; \
} \
480
static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
	pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
			    data->in_crit[offset-4]); \
	up(&data->update_lock); \
	return count; \
} \
static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
	show_temp##offset##_input, NULL); \
static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
	show_temp##offset##_min, set_temp##offset##_min); \
static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
	show_temp##offset##_max, set_temp##offset##_max); \
static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
	show_temp##offset##_crit, set_temp##offset##_crit); \
static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
	show_temp##offset##_status, NULL);
show_and_set_therm(4)
show_and_set_therm(5)
show_and_set_therm(6)

508
static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
509 510 511 512 513 514
{
	struct pc87360_data *data = pc87360_update_device(dev);
	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
}
static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);

515
static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
516 517 518 519
{
	struct pc87360_data *data = pc87360_update_device(dev);
	return sprintf(buf, "%u\n", data->vrm);
}
520
static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pc87360_data *data = i2c_get_clientdata(client);
	data->vrm = simple_strtoul(buf, NULL, 10);
	return count;
}
static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);

529
static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
530 531 532 533 534 535 536
{
	struct pc87360_data *data = pc87360_update_device(dev);
	return sprintf(buf, "%u\n", data->in_alarms);
}
static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);

#define show_and_set_temp(offset) \
537
static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
538 539 540 541
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
} \
542
static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
543 544 545 546
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
} \
547
static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
548 549 550 551
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
}\
552
static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
553 554 555 556
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
}\
557
static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
L
Linus Torvalds 已提交
558 559 560 561
{ \
	struct pc87360_data *data = pc87360_update_device(dev); \
	return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
}\
562
static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->temp_min[offset-1] = TEMP_TO_REG(val); \
	pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
			    data->temp_min[offset-1]); \
	up(&data->update_lock); \
	return count; \
} \
576
static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->temp_max[offset-1] = TEMP_TO_REG(val); \
	pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
			    data->temp_max[offset-1]); \
	up(&data->update_lock); \
	return count; \
} \
590
static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	size_t count) \
{ \
	struct i2c_client *client = to_i2c_client(dev); \
	struct pc87360_data *data = i2c_get_clientdata(client); \
	long val = simple_strtol(buf, NULL, 10); \
 \
	down(&data->update_lock); \
	data->temp_crit[offset-1] = TEMP_TO_REG(val); \
	pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
			    data->temp_crit[offset-1]); \
	up(&data->update_lock); \
	return count; \
} \
static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
	show_temp##offset##_input, NULL); \
static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
	show_temp##offset##_min, set_temp##offset##_min); \
static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
	show_temp##offset##_max, set_temp##offset##_max); \
static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
	show_temp##offset##_crit, set_temp##offset##_crit); \
static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
	show_temp##offset##_status, NULL);
show_and_set_temp(1)
show_and_set_temp(2)
show_and_set_temp(3)

618
static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627 628
{
	struct pc87360_data *data = pc87360_update_device(dev);
	return sprintf(buf, "%u\n", data->temp_alarms);
}
static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);

/*
 * Device detection, registration and update
 */

629
static int pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
{
	u16 val;
	int i;
	int nrdev; /* logical device count */

	/* No superio_enter */

	/* Identify device */
	val = superio_inb(sioaddr, DEVID);
	switch (val) {
	case 0xE1: /* PC87360 */
	case 0xE8: /* PC87363 */
	case 0xE4: /* PC87364 */
		nrdev = 1;
		break;
	case 0xE5: /* PC87365 */
	case 0xE9: /* PC87366 */
		nrdev = 3;
		break;
	default:
		superio_exit(sioaddr);
		return -ENODEV;
	}
	/* Remember the device id */
	*devid = val;

	for (i = 0; i < nrdev; i++) {
		/* select logical device */
		superio_outb(sioaddr, DEV, logdev[i]);

		val = superio_inb(sioaddr, ACT);
		if (!(val & 0x01)) {
			printk(KERN_INFO "pc87360: Device 0x%02x not "
			       "activated\n", logdev[i]);
			continue;
		}

		val = (superio_inb(sioaddr, BASE) << 8)
		    | superio_inb(sioaddr, BASE + 1);
		if (!val) {
			printk(KERN_INFO "pc87360: Base address not set for "
			       "device 0x%02x\n", logdev[i]);
			continue;
		}

675
		addresses[i] = val;
L
Linus Torvalds 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

		if (i==0) { /* Fans */
			confreg[0] = superio_inb(sioaddr, 0xF0);
			confreg[1] = superio_inb(sioaddr, 0xF1);

#ifdef DEBUG
			printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
			       "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
			       (confreg[0]>>3)&1, (confreg[0]>>4)&1);
			printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
			       "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
			       (confreg[0]>>6)&1, (confreg[0]>>7)&1);
			printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
			       "ctrl=%d inv=%d\n", confreg[1]&1,
			       (confreg[1]>>1)&1, (confreg[1]>>2)&1);
#endif
		} else if (i==1) { /* Voltages */
			/* Are we using thermistors? */
			if (*devid == 0xE9) { /* PC87366 */
				/* These registers are not logical-device
				   specific, just that we won't need them if
				   we don't use the VLM device */
				confreg[2] = superio_inb(sioaddr, 0x2B);
				confreg[3] = superio_inb(sioaddr, 0x25);

				if (confreg[2] & 0x40) {
					printk(KERN_INFO "pc87360: Using "
					       "thermistors for temperature "
					       "monitoring\n");
				}
				if (confreg[3] & 0xE0) {
					printk(KERN_INFO "pc87360: VID "
					       "inputs routed (mode %u)\n",
					       confreg[3] >> 5);
				}
			}
		}
	}

	superio_exit(sioaddr);
	return 0;
}

719
static int pc87360_detect(struct i2c_adapter *adapter)
L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
{
	int i;
	struct i2c_client *new_client;
	struct pc87360_data *data;
	int err = 0;
	const char *name = "pc87360";
	int use_thermistors = 0;

	if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
		return -ENOMEM;
	memset(data, 0x00, sizeof(struct pc87360_data));

	new_client = &data->client;
	i2c_set_clientdata(new_client, data);
	new_client->addr = address;
	init_MUTEX(&data->lock);
	new_client->adapter = adapter;
	new_client->driver = &pc87360_driver;
	new_client->flags = 0;

	data->fannr = 2;
	data->innr = 0;
	data->tempnr = 0;

	switch (devid) {
	case 0xe8:
		name = "pc87363";
		break;
	case 0xe4:
		name = "pc87364";
		data->fannr = 3;
		break;
	case 0xe5:
		name = "pc87365";
		data->fannr = extra_isa[0] ? 3 : 0;
		data->innr = extra_isa[1] ? 11 : 0;
		data->tempnr = extra_isa[2] ? 2 : 0;
		break;
	case 0xe9:
		name = "pc87366";
		data->fannr = extra_isa[0] ? 3 : 0;
		data->innr = extra_isa[1] ? 14 : 0;
		data->tempnr = extra_isa[2] ? 3 : 0;
		break;
	}

	strcpy(new_client->name, name);
	data->valid = 0;
	init_MUTEX(&data->update_lock);

	for (i = 0; i < 3; i++) {
		if (((data->address[i] = extra_isa[i]))
		 && !request_region(extra_isa[i], PC87360_EXTENT,
		 		    pc87360_driver.name)) {
			dev_err(&new_client->dev, "Region 0x%x-0x%x already "
				"in use!\n", extra_isa[i],
				extra_isa[i]+PC87360_EXTENT-1);
			for (i--; i >= 0; i--)
				release_region(extra_isa[i], PC87360_EXTENT);
			err = -EBUSY;
			goto ERROR1;
		}
	}

	/* Retrieve the fans configuration from Super-I/O space */
	if (data->fannr)
		data->fan_conf = confreg[0] | (confreg[1] << 8);

	if ((err = i2c_attach_client(new_client)))
		goto ERROR2;

	/* Use the correct reference voltage
	   Unless both the VLM and the TMS logical devices agree to
	   use an external Vref, the internal one is used. */
	if (data->innr) {
		i = pc87360_read_value(data, LD_IN, NO_BANK,
				       PC87365_REG_IN_CONFIG);
		if (data->tempnr) {
			i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
						PC87365_REG_TEMP_CONFIG);
		}
		data->in_vref = (i&0x02) ? 3025 : 2966;
		dev_dbg(&new_client->dev, "Using %s reference voltage\n",
			(i&0x02) ? "external" : "internal");

		data->vid_conf = confreg[3];
		data->vrm = 90;
	}

	/* Fan clock dividers may be needed before any data is read */
	for (i = 0; i < data->fannr; i++) {
		if (FAN_CONFIG_MONITOR(data->fan_conf, i))
			data->fan_status[i] = pc87360_read_value(data,
					      LD_FAN, NO_BANK,
					      PC87360_REG_FAN_STATUS(i));
	}

	if (init > 0) {
		if (devid == 0xe9 && data->address[1]) /* PC87366 */
			use_thermistors = confreg[2] & 0x40;

		pc87360_init_client(new_client, use_thermistors);
	}

	/* Register sysfs hooks */
825 826 827 828 829 830
	data->class_dev = hwmon_device_register(&new_client->dev);
	if (IS_ERR(data->class_dev)) {
		err = PTR_ERR(data->class_dev);
		goto ERROR3;
	}

L
Linus Torvalds 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
	if (data->innr) {
		device_create_file(&new_client->dev, &dev_attr_in0_input);
		device_create_file(&new_client->dev, &dev_attr_in1_input);
		device_create_file(&new_client->dev, &dev_attr_in2_input);
		device_create_file(&new_client->dev, &dev_attr_in3_input);
		device_create_file(&new_client->dev, &dev_attr_in4_input);
		device_create_file(&new_client->dev, &dev_attr_in5_input);
		device_create_file(&new_client->dev, &dev_attr_in6_input);
		device_create_file(&new_client->dev, &dev_attr_in7_input);
		device_create_file(&new_client->dev, &dev_attr_in8_input);
		device_create_file(&new_client->dev, &dev_attr_in9_input);
		device_create_file(&new_client->dev, &dev_attr_in10_input);
		device_create_file(&new_client->dev, &dev_attr_in0_min);
		device_create_file(&new_client->dev, &dev_attr_in1_min);
		device_create_file(&new_client->dev, &dev_attr_in2_min);
		device_create_file(&new_client->dev, &dev_attr_in3_min);
		device_create_file(&new_client->dev, &dev_attr_in4_min);
		device_create_file(&new_client->dev, &dev_attr_in5_min);
		device_create_file(&new_client->dev, &dev_attr_in6_min);
		device_create_file(&new_client->dev, &dev_attr_in7_min);
		device_create_file(&new_client->dev, &dev_attr_in8_min);
		device_create_file(&new_client->dev, &dev_attr_in9_min);
		device_create_file(&new_client->dev, &dev_attr_in10_min);
		device_create_file(&new_client->dev, &dev_attr_in0_max);
		device_create_file(&new_client->dev, &dev_attr_in1_max);
		device_create_file(&new_client->dev, &dev_attr_in2_max);
		device_create_file(&new_client->dev, &dev_attr_in3_max);
		device_create_file(&new_client->dev, &dev_attr_in4_max);
		device_create_file(&new_client->dev, &dev_attr_in5_max);
		device_create_file(&new_client->dev, &dev_attr_in6_max);
		device_create_file(&new_client->dev, &dev_attr_in7_max);
		device_create_file(&new_client->dev, &dev_attr_in8_max);
		device_create_file(&new_client->dev, &dev_attr_in9_max);
		device_create_file(&new_client->dev, &dev_attr_in10_max);
		device_create_file(&new_client->dev, &dev_attr_in0_status);
		device_create_file(&new_client->dev, &dev_attr_in1_status);
		device_create_file(&new_client->dev, &dev_attr_in2_status);
		device_create_file(&new_client->dev, &dev_attr_in3_status);
		device_create_file(&new_client->dev, &dev_attr_in4_status);
		device_create_file(&new_client->dev, &dev_attr_in5_status);
		device_create_file(&new_client->dev, &dev_attr_in6_status);
		device_create_file(&new_client->dev, &dev_attr_in7_status);
		device_create_file(&new_client->dev, &dev_attr_in8_status);
		device_create_file(&new_client->dev, &dev_attr_in9_status);
		device_create_file(&new_client->dev, &dev_attr_in10_status);

		device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
		device_create_file(&new_client->dev, &dev_attr_vrm);
		device_create_file(&new_client->dev, &dev_attr_alarms_in);
	}

	if (data->tempnr) {
		device_create_file(&new_client->dev, &dev_attr_temp1_input);
		device_create_file(&new_client->dev, &dev_attr_temp2_input);
		device_create_file(&new_client->dev, &dev_attr_temp1_min);
		device_create_file(&new_client->dev, &dev_attr_temp2_min);
		device_create_file(&new_client->dev, &dev_attr_temp1_max);
		device_create_file(&new_client->dev, &dev_attr_temp2_max);
		device_create_file(&new_client->dev, &dev_attr_temp1_crit);
		device_create_file(&new_client->dev, &dev_attr_temp2_crit);
		device_create_file(&new_client->dev, &dev_attr_temp1_status);
		device_create_file(&new_client->dev, &dev_attr_temp2_status);

		device_create_file(&new_client->dev, &dev_attr_alarms_temp);
	}
	if (data->tempnr == 3) {
		device_create_file(&new_client->dev, &dev_attr_temp3_input);
		device_create_file(&new_client->dev, &dev_attr_temp3_min);
		device_create_file(&new_client->dev, &dev_attr_temp3_max);
		device_create_file(&new_client->dev, &dev_attr_temp3_crit);
		device_create_file(&new_client->dev, &dev_attr_temp3_status);
	}
	if (data->innr == 14) {
		device_create_file(&new_client->dev, &dev_attr_temp4_input);
		device_create_file(&new_client->dev, &dev_attr_temp5_input);
		device_create_file(&new_client->dev, &dev_attr_temp6_input);
		device_create_file(&new_client->dev, &dev_attr_temp4_min);
		device_create_file(&new_client->dev, &dev_attr_temp5_min);
		device_create_file(&new_client->dev, &dev_attr_temp6_min);
		device_create_file(&new_client->dev, &dev_attr_temp4_max);
		device_create_file(&new_client->dev, &dev_attr_temp5_max);
		device_create_file(&new_client->dev, &dev_attr_temp6_max);
		device_create_file(&new_client->dev, &dev_attr_temp4_crit);
		device_create_file(&new_client->dev, &dev_attr_temp5_crit);
		device_create_file(&new_client->dev, &dev_attr_temp6_crit);
		device_create_file(&new_client->dev, &dev_attr_temp4_status);
		device_create_file(&new_client->dev, &dev_attr_temp5_status);
		device_create_file(&new_client->dev, &dev_attr_temp6_status);
	}

	if (data->fannr) {
		if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
			device_create_file(&new_client->dev,
					   &dev_attr_fan1_input);
			device_create_file(&new_client->dev,
					   &dev_attr_fan1_min);
			device_create_file(&new_client->dev,
					   &dev_attr_fan1_div);
			device_create_file(&new_client->dev,
					   &dev_attr_fan1_status);
		}

		if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
			device_create_file(&new_client->dev,
					   &dev_attr_fan2_input);
			device_create_file(&new_client->dev,
					   &dev_attr_fan2_min);
			device_create_file(&new_client->dev,
					   &dev_attr_fan2_div);
			device_create_file(&new_client->dev,
					   &dev_attr_fan2_status);
		}

		if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
			device_create_file(&new_client->dev, &dev_attr_pwm1);
		if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
			device_create_file(&new_client->dev, &dev_attr_pwm2);
	}
	if (data->fannr == 3) {
		if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
			device_create_file(&new_client->dev,
					   &dev_attr_fan3_input);
			device_create_file(&new_client->dev,
					   &dev_attr_fan3_min);
			device_create_file(&new_client->dev,
					   &dev_attr_fan3_div);
			device_create_file(&new_client->dev,
					   &dev_attr_fan3_status);
		}

		if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
			device_create_file(&new_client->dev, &dev_attr_pwm3);
	}

	return 0;

967 968
ERROR3:
	i2c_detach_client(new_client);
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
ERROR2:
	for (i = 0; i < 3; i++) {
		if (data->address[i]) {
			release_region(data->address[i], PC87360_EXTENT);
		}
	}
ERROR1:
	kfree(data);
	return err;
}

static int pc87360_detach_client(struct i2c_client *client)
{
	struct pc87360_data *data = i2c_get_clientdata(client);
	int i;

985 986
	hwmon_device_unregister(data->class_dev);

987
	if ((i = i2c_detach_client(client)))
L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		return i;

	for (i = 0; i < 3; i++) {
		if (data->address[i]) {
			release_region(data->address[i], PC87360_EXTENT);
		}
	}
	kfree(data);

	return 0;
}

/* ldi is the logical device index
   bank is for voltages and temperatures only */
static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
			      u8 reg)
{
	int res;

	down(&(data->lock));
	if (bank != NO_BANK)
		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
	res = inb_p(data->address[ldi] + reg);
	up(&(data->lock));

	return res;
}

static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
				u8 reg, u8 value)
{
	down(&(data->lock));
	if (bank != NO_BANK)
		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
	outb_p(value, data->address[ldi] + reg);
	up(&(data->lock));
}

static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
{
	struct pc87360_data *data = i2c_get_clientdata(client);
	int i, nr;
	const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
	const u8 init_temp[3] = { 2, 2, 1 };
	u8 reg;

	if (init >= 2 && data->innr) {
		reg = pc87360_read_value(data, LD_IN, NO_BANK,
					 PC87365_REG_IN_CONVRATE);
1037
		dev_info(&client->dev, "VLM conversion set to "
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
			 "1s period, 160us delay\n");
		pc87360_write_value(data, LD_IN, NO_BANK,
				    PC87365_REG_IN_CONVRATE,
				    (reg & 0xC0) | 0x11);
	}

	nr = data->innr < 11 ? data->innr : 11;
	for (i=0; i<nr; i++) {
		if (init >= init_in[i]) {
			/* Forcibly enable voltage channel */
			reg = pc87360_read_value(data, LD_IN, i,
						 PC87365_REG_IN_STATUS);
			if (!(reg & 0x01)) {
				dev_dbg(&client->dev, "Forcibly "
					"enabling in%d\n", i);
				pc87360_write_value(data, LD_IN, i,
						    PC87365_REG_IN_STATUS,
						    (reg & 0x68) | 0x87);
			}
		}
	}

	/* We can't blindly trust the Super-I/O space configuration bit,
	   most BIOS won't set it properly */
	for (i=11; i<data->innr; i++) {
		reg = pc87360_read_value(data, LD_IN, i,
					 PC87365_REG_TEMP_STATUS);
		use_thermistors = use_thermistors || (reg & 0x01);
	}

	i = use_thermistors ? 2 : 0;
	for (; i<data->tempnr; i++) {
		if (init >= init_temp[i]) {
			/* Forcibly enable temperature channel */
			reg = pc87360_read_value(data, LD_TEMP, i,
						 PC87365_REG_TEMP_STATUS);
			if (!(reg & 0x01)) {
				dev_dbg(&client->dev, "Forcibly "
					"enabling temp%d\n", i+1);
				pc87360_write_value(data, LD_TEMP, i,
						    PC87365_REG_TEMP_STATUS,
						    0xCF);
			}
		}
	}

	if (use_thermistors) {
		for (i=11; i<data->innr; i++) {
			if (init >= init_in[i]) {
				/* The pin may already be used by thermal
				   diodes */
				reg = pc87360_read_value(data, LD_TEMP,
				      (i-11)/2, PC87365_REG_TEMP_STATUS);
				if (reg & 0x01) {
					dev_dbg(&client->dev, "Skipping "
						"temp%d, pin already in use "
						"by temp%d\n", i-7, (i-11)/2);
					continue;
				}

				/* Forcibly enable thermistor channel */
				reg = pc87360_read_value(data, LD_IN, i,
							 PC87365_REG_IN_STATUS);
				if (!(reg & 0x01)) {
					dev_dbg(&client->dev, "Forcibly "
						"enabling temp%d\n", i-7);
					pc87360_write_value(data, LD_IN, i,
						PC87365_REG_TEMP_STATUS,
						(reg & 0x60) | 0x8F);
				}
			}
		}
	}

	if (data->innr) {
		reg = pc87360_read_value(data, LD_IN, NO_BANK,
					 PC87365_REG_IN_CONFIG);
		if (reg & 0x01) {
			dev_dbg(&client->dev, "Forcibly "
				"enabling monitoring (VLM)\n");
			pc87360_write_value(data, LD_IN, NO_BANK,
					    PC87365_REG_IN_CONFIG,
					    reg & 0xFE);
		}
	}

	if (data->tempnr) {
		reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
					 PC87365_REG_TEMP_CONFIG);
		if (reg & 0x01) {
			dev_dbg(&client->dev, "Forcibly enabling "
				"monitoring (TMS)\n");
			pc87360_write_value(data, LD_TEMP, NO_BANK,
					    PC87365_REG_TEMP_CONFIG,
					    reg & 0xFE);
		}

		if (init >= 2) {
			/* Chip config as documented by National Semi. */
			pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
			/* We voluntarily omit the bank here, in case the
			   sequence itself matters. It shouldn't be a problem,
			   since nobody else is supposed to access the
			   device at that point. */
			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
		}
	}
}

static void pc87360_autodiv(struct i2c_client *client, int nr)
{
	struct pc87360_data *data = i2c_get_clientdata(client);
	u8 old_min = data->fan_min[nr];

	/* Increase clock divider if needed and possible */
	if ((data->fan_status[nr] & 0x04) /* overflow flag */
	 || (data->fan[nr] >= 224)) { /* next to overflow */
		if ((data->fan_status[nr] & 0x60) != 0x60) {
			data->fan_status[nr] += 0x20;
			data->fan_min[nr] >>= 1;
			data->fan[nr] >>= 1;
			dev_dbg(&client->dev, "Increasing "
				"clock divider to %d for fan %d\n",
				FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
		}
	} else {
		/* Decrease clock divider if possible */
		while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
		 && data->fan[nr] < 85 /* bad accuracy */
		 && (data->fan_status[nr] & 0x60) != 0x00) {
			data->fan_status[nr] -= 0x20;
			data->fan_min[nr] <<= 1;
			data->fan[nr] <<= 1;
			dev_dbg(&client->dev, "Decreasing "
				"clock divider to %d for fan %d\n",
				FAN_DIV_FROM_REG(data->fan_status[nr]),
				nr+1);
		}
	}

	/* Write new fan min if it changed */
	if (old_min != data->fan_min[nr]) {
		pc87360_write_value(data, LD_FAN, NO_BANK,
				    PC87360_REG_FAN_MIN(nr),
				    data->fan_min[nr]);
	}
}

static struct pc87360_data *pc87360_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct pc87360_data *data = i2c_get_clientdata(client);
	u8 i;

	down(&data->update_lock);

	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
		dev_dbg(&client->dev, "Data update\n");

		/* Fans */
		for (i = 0; i < data->fannr; i++) {
			if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
				data->fan_status[i] =
					pc87360_read_value(data, LD_FAN,
					NO_BANK, PC87360_REG_FAN_STATUS(i));
				data->fan[i] = pc87360_read_value(data, LD_FAN,
					       NO_BANK, PC87360_REG_FAN(i));
				data->fan_min[i] = pc87360_read_value(data,
						   LD_FAN, NO_BANK,
						   PC87360_REG_FAN_MIN(i));
				/* Change clock divider if needed */
				pc87360_autodiv(client, i);
				/* Clear bits and write new divider */
				pc87360_write_value(data, LD_FAN, NO_BANK,
						    PC87360_REG_FAN_STATUS(i),
						    data->fan_status[i]);
			}
			if (FAN_CONFIG_CONTROL(data->fan_conf, i))
				data->pwm[i] = pc87360_read_value(data, LD_FAN,
					       NO_BANK, PC87360_REG_PWM(i));
		}

		/* Voltages */
		for (i = 0; i < data->innr; i++) {
			data->in_status[i] = pc87360_read_value(data, LD_IN, i,
					     PC87365_REG_IN_STATUS);
			/* Clear bits */
			pc87360_write_value(data, LD_IN, i,
					    PC87365_REG_IN_STATUS,
					    data->in_status[i]);
			if ((data->in_status[i] & 0x81) == 0x81) {
				data->in[i] = pc87360_read_value(data, LD_IN,
					      i, PC87365_REG_IN);
			}
			if (data->in_status[i] & 0x01) {
				data->in_min[i] = pc87360_read_value(data,
						  LD_IN, i,
						  PC87365_REG_IN_MIN);
				data->in_max[i] = pc87360_read_value(data,
						  LD_IN, i,
						  PC87365_REG_IN_MAX);
				if (i >= 11)
					data->in_crit[i-11] =
						pc87360_read_value(data, LD_IN,
						i, PC87365_REG_TEMP_CRIT);
			}
		}
		if (data->innr) {
			data->in_alarms = pc87360_read_value(data, LD_IN,
					  NO_BANK, PC87365_REG_IN_ALARMS1)
					| ((pc87360_read_value(data, LD_IN,
					    NO_BANK, PC87365_REG_IN_ALARMS2)
					    & 0x07) << 8);
			data->vid = (data->vid_conf & 0xE0) ?
				    pc87360_read_value(data, LD_IN,
				    NO_BANK, PC87365_REG_VID) : 0x1F;
		}

		/* Temperatures */
		for (i = 0; i < data->tempnr; i++) {
			data->temp_status[i] = pc87360_read_value(data,
					       LD_TEMP, i,
					       PC87365_REG_TEMP_STATUS);
			/* Clear bits */
			pc87360_write_value(data, LD_TEMP, i,
					    PC87365_REG_TEMP_STATUS,
					    data->temp_status[i]);
			if ((data->temp_status[i] & 0x81) == 0x81) {
				data->temp[i] = pc87360_read_value(data,
						LD_TEMP, i,
						PC87365_REG_TEMP);
			}
			if (data->temp_status[i] & 0x01) {
				data->temp_min[i] = pc87360_read_value(data,
						    LD_TEMP, i,
						    PC87365_REG_TEMP_MIN);
				data->temp_max[i] = pc87360_read_value(data,
						    LD_TEMP, i,
						    PC87365_REG_TEMP_MAX);
				data->temp_crit[i] = pc87360_read_value(data,
						     LD_TEMP, i,
						     PC87365_REG_TEMP_CRIT);
			}
		}
		if (data->tempnr) {
			data->temp_alarms = pc87360_read_value(data, LD_TEMP,
					    NO_BANK, PC87365_REG_TEMP_ALARMS)
					    & 0x3F;
		}

		data->last_updated = jiffies;
		data->valid = 1;
	}

	up(&data->update_lock);

	return data;
}

static int __init pc87360_init(void)
{
	int i;

	if (pc87360_find(0x2e, &devid, extra_isa)
	 && pc87360_find(0x4e, &devid, extra_isa)) {
		printk(KERN_WARNING "pc87360: PC8736x not detected, "
		       "module not inserted.\n");
		return -ENODEV;
	}

	/* Arbitrarily pick one of the addresses */
	for (i = 0; i < 3; i++) {
		if (extra_isa[i] != 0x0000) {
1314
			address = extra_isa[i];
L
Linus Torvalds 已提交
1315 1316 1317 1318
			break;
		}
	}

1319
	if (address == 0x0000) {
L
Linus Torvalds 已提交
1320 1321 1322 1323 1324
		printk(KERN_WARNING "pc87360: No active logical device, "
		       "module not inserted.\n");
		return -ENODEV;
	}

1325
	return i2c_isa_add_driver(&pc87360_driver);
L
Linus Torvalds 已提交
1326 1327 1328 1329
}

static void __exit pc87360_exit(void)
{
1330
	i2c_isa_del_driver(&pc87360_driver);
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339
}


MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
MODULE_DESCRIPTION("PC8736x hardware monitor");
MODULE_LICENSE("GPL");

module_init(pc87360_init);
module_exit(pc87360_exit);