adm9240.c 23.8 KB
Newer Older
1 2 3 4 5 6 7
/*
 * adm9240.c	Part of lm_sensors, Linux kernel modules for hardware
 * 		monitoring
 *
 * Copyright (C) 1999	Frodo Looijaard <frodol@dds.nl>
 *			Philip Edelbrock <phil@netroedge.com>
 * Copyright (C) 2003	Michiel Rook <michiel@grendelproject.nl>
8
 * Copyright (C) 2005	Grant Coady <gcoady.lk@gmail.com> with valuable
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 				guidance from Jean Delvare
 *
 * Driver supports	Analog Devices		ADM9240
 *			Dallas Semiconductor	DS1780
 *			National Semiconductor	LM81
 *
 * ADM9240 is the reference, DS1780 and LM81 are register compatibles
 *
 * Voltage	Six inputs are scaled by chip, VID also reported
 * Temperature	Chip temperature to 0.5'C, maximum and max_hysteris
 * Fans		2 fans, low speed alarm, automatic fan clock divider
 * Alarms	16-bit map of active alarms
 * Analog Out	0..1250 mV output
 *
23
 * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
 *
 * LM81 extended temp reading not implemented
 *
 * 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.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
48
#include <linux/hwmon-sysfs.h>
49
#include <linux/hwmon.h>
50
#include <linux/hwmon-vid.h>
51
#include <linux/err.h>
52
#include <linux/mutex.h>
53 54

/* Addresses to scan */
55
static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
56 57
					I2C_CLIENT_END };

J
Jean Delvare 已提交
58
enum chips { adm9240, ds1780, lm81 };
59 60 61 62 63 64 65 66 67 68 69 70 71 72

/* ADM9240 registers */
#define ADM9240_REG_MAN_ID		0x3e
#define ADM9240_REG_DIE_REV		0x3f
#define ADM9240_REG_CONFIG		0x40

#define ADM9240_REG_IN(nr)		(0x20 + (nr))   /* 0..5 */
#define ADM9240_REG_IN_MAX(nr)		(0x2b + (nr) * 2)
#define ADM9240_REG_IN_MIN(nr)		(0x2c + (nr) * 2)
#define ADM9240_REG_FAN(nr)		(0x28 + (nr))   /* 0..1 */
#define ADM9240_REG_FAN_MIN(nr)		(0x3b + (nr))
#define ADM9240_REG_INT(nr)		(0x41 + (nr))
#define ADM9240_REG_INT_MASK(nr)	(0x43 + (nr))
#define ADM9240_REG_TEMP		0x27
73
#define ADM9240_REG_TEMP_MAX(nr)	(0x39 + (nr)) /* 0, 1 = high, hyst */
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
#define ADM9240_REG_ANALOG_OUT		0x19
#define ADM9240_REG_CHASSIS_CLEAR	0x46
#define ADM9240_REG_VID_FAN_DIV		0x47
#define ADM9240_REG_I2C_ADDR		0x48
#define ADM9240_REG_VID4		0x49
#define ADM9240_REG_TEMP_CONF		0x4b

/* generalised scaling with integer rounding */
static inline int SCALE(long val, int mul, int div)
{
	if (val < 0)
		return (val * mul - div / 2) / div;
	else
		return (val * mul + div / 2) / div;
}

/* adm9240 internally scales voltage measurements */
static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };

static inline unsigned int IN_FROM_REG(u8 reg, int n)
{
	return SCALE(reg, nom_mv[n], 192);
}

static inline u8 IN_TO_REG(unsigned long val, int n)
{
	return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
}

/* temperature range: -40..125, 127 disables temperature alarm */
static inline s8 TEMP_TO_REG(long val)
{
	return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
}

/* two fans, each with low fan speed limit */
static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
{
	if (!reg) /* error */
		return -1;

	if (reg == 255)
		return 0;

	return SCALE(1350000, 1, reg * div);
}

/* analog out 0..1250mV */
static inline u8 AOUT_TO_REG(unsigned long val)
{
	return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
}

static inline unsigned int AOUT_FROM_REG(u8 reg)
{
	return SCALE(reg, 1250, 255);
}

132 133
static int adm9240_probe(struct i2c_client *client,
			 const struct i2c_device_id *id);
134
static int adm9240_detect(struct i2c_client *client,
135
			  struct i2c_board_info *info);
136
static void adm9240_init_client(struct i2c_client *client);
137
static int adm9240_remove(struct i2c_client *client);
138 139 140
static struct adm9240_data *adm9240_update_device(struct device *dev);

/* driver data */
141 142 143 144 145 146 147 148
static const struct i2c_device_id adm9240_id[] = {
	{ "adm9240", adm9240 },
	{ "ds1780", ds1780 },
	{ "lm81", lm81 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, adm9240_id);

149
static struct i2c_driver adm9240_driver = {
150
	.class		= I2C_CLASS_HWMON,
151 152 153
	.driver = {
		.name	= "adm9240",
	},
154 155 156 157
	.probe		= adm9240_probe,
	.remove		= adm9240_remove,
	.id_table	= adm9240_id,
	.detect		= adm9240_detect,
158
	.address_list	= normal_i2c,
159 160 161 162
};

/* per client data */
struct adm9240_data {
163
	struct device *hwmon_dev;
164
	struct mutex update_lock;
165 166 167 168 169 170 171 172 173 174 175
	char valid;
	unsigned long last_updated_measure;
	unsigned long last_updated_config;

	u8 in[6];		/* ro	in0_input */
	u8 in_max[6];		/* rw	in0_max */
	u8 in_min[6];		/* rw	in0_min */
	u8 fan[2];		/* ro	fan1_input */
	u8 fan_min[2];		/* rw	fan1_min */
	u8 fan_div[2];		/* rw	fan1_div, read-only accessor */
	s16 temp;		/* ro	temp1_input, 9-bit sign-extended */
176
	s8 temp_max[2];		/* rw	0 -> temp_max, 1 -> temp_max_hyst */
177
	u16 alarms;		/* ro	alarms */
178
	u8 aout;		/* rw	aout_output */
179 180 181 182 183 184 185
	u8 vid;			/* ro	vid */
	u8 vrm;			/* --	vrm set on startup, no accessor */
};

/*** sysfs accessors ***/

/* temperature */
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
		char *buf)
{
	struct adm9240_data *data = adm9240_update_device(dev);
	return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
}

static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
		char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct adm9240_data *data = adm9240_update_device(dev);
	return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
}

static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
		const char *buf, size_t count)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	long val = simple_strtol(buf, NULL, 10);

209
	mutex_lock(&data->update_lock);
210 211 212
	data->temp_max[attr->index] = TEMP_TO_REG(val);
	i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
			data->temp_max[attr->index]);
213
	mutex_unlock(&data->update_lock);
214 215 216
	return count;
}

217
static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
218 219 220 221
static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
		show_max, set_max, 0);
static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
		show_max, set_max, 1);
222 223

/* voltage */
224 225
static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
		char *buf)
226
{
227
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
228
	struct adm9240_data *data = adm9240_update_device(dev);
229 230
	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
				attr->index));
231 232
}

233 234
static ssize_t show_in_min(struct device *dev,
		struct device_attribute *devattr, char *buf)
235
{
236
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
237
	struct adm9240_data *data = adm9240_update_device(dev);
238 239
	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
				attr->index));
240 241
}

242 243
static ssize_t show_in_max(struct device *dev,
		struct device_attribute *devattr, char *buf)
244
{
245
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
246
	struct adm9240_data *data = adm9240_update_device(dev);
247 248
	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
				attr->index));
249 250
}

251 252
static ssize_t set_in_min(struct device *dev,
		struct device_attribute *devattr,
253
		const char *buf, size_t count)
254
{
255
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
256 257 258 259
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	unsigned long val = simple_strtoul(buf, NULL, 10);

260
	mutex_lock(&data->update_lock);
261 262 263
	data->in_min[attr->index] = IN_TO_REG(val, attr->index);
	i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
			data->in_min[attr->index]);
264
	mutex_unlock(&data->update_lock);
265 266 267
	return count;
}

268 269
static ssize_t set_in_max(struct device *dev,
		struct device_attribute *devattr,
270
		const char *buf, size_t count)
271
{
272
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
273 274 275 276
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	unsigned long val = simple_strtoul(buf, NULL, 10);

277
	mutex_lock(&data->update_lock);
278 279 280
	data->in_max[attr->index] = IN_TO_REG(val, attr->index);
	i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
			data->in_max[attr->index]);
281
	mutex_unlock(&data->update_lock);
282 283 284
	return count;
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298
#define vin(nr)							\
static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, 		\
		show_in, NULL, nr);				\
static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR,	\
		show_in_min, set_in_min, nr);			\
static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR,	\
		show_in_max, set_in_max, nr);

vin(0);
vin(1);
vin(2);
vin(3);
vin(4);
vin(5);
299 300

/* fans */
301 302
static ssize_t show_fan(struct device *dev,
		struct device_attribute *devattr, char *buf)
303
{
304
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
305
	struct adm9240_data *data = adm9240_update_device(dev);
306 307
	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
				1 << data->fan_div[attr->index]));
308 309
}

310 311
static ssize_t show_fan_min(struct device *dev,
		struct device_attribute *devattr, char *buf)
312
{
313
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
314
	struct adm9240_data *data = adm9240_update_device(dev);
315 316
	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
				1 << data->fan_div[attr->index]));
317 318
}

319 320
static ssize_t show_fan_div(struct device *dev,
		struct device_attribute *devattr, char *buf)
321
{
322
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
323
	struct adm9240_data *data = adm9240_update_device(dev);
324
	return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
325 326 327 328 329 330 331 332
}

/* write new fan div, callers must hold data->update_lock */
static void adm9240_write_fan_div(struct i2c_client *client, int nr,
		u8 fan_div)
{
	u8 reg, old, shift = (nr + 2) * 2;

333
	reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
334 335 336
	old = (reg >> shift) & 3;
	reg &= ~(3 << shift);
	reg |= (fan_div << shift);
337
	i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
338 339 340 341
	dev_dbg(&client->dev, "fan%d clock divider changed from %u "
			"to %u\n", nr + 1, 1 << old, 1 << fan_div);
}

342
/*
343 344 345 346 347 348 349 350 351 352
 * set fan speed low limit:
 *
 * - value is zero: disable fan speed low limit alarm
 *
 * - value is below fan speed measurement range: enable fan speed low
 *   limit alarm to be asserted while fan speed too slow to measure
 *
 * - otherwise: select fan clock divider to suit fan speed low limit,
 *   measurement code may adjust registers to ensure fan speed reading
 */
353 354 355
static ssize_t set_fan_min(struct device *dev,
		struct device_attribute *devattr,
		const char *buf, size_t count)
356
{
357
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
358 359 360
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	unsigned long val = simple_strtoul(buf, NULL, 10);
361
	int nr = attr->index;
362 363
	u8 new_div;

364
	mutex_lock(&data->update_lock);
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

	if (!val) {
		data->fan_min[nr] = 255;
		new_div = data->fan_div[nr];

		dev_dbg(&client->dev, "fan%u low limit set disabled\n",
				nr + 1);

	} else if (val < 1350000 / (8 * 254)) {
		new_div = 3;
		data->fan_min[nr] = 254;

		dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
				nr + 1, FAN_FROM_REG(254, 1 << new_div));

	} else {
		unsigned int new_min = 1350000 / val;

		new_div = 0;
		while (new_min > 192 && new_div < 3) {
			new_div++;
			new_min /= 2;
		}
		if (!new_min) /* keep > 0 */
			new_min++;

		data->fan_min[nr] = new_min;

		dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
				nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
	}

	if (new_div != data->fan_div[nr]) {
		data->fan_div[nr] = new_div;
		adm9240_write_fan_div(client, nr, new_div);
	}
401
	i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
402 403
			data->fan_min[nr]);

404
	mutex_unlock(&data->update_lock);
405 406 407
	return count;
}

408 409 410 411 412 413 414 415 416 417
#define fan(nr)							\
static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO,		\
		show_fan, NULL, nr - 1);			\
static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO,		\
		show_fan_div, NULL, nr - 1);			\
static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR,	\
		show_fan_min, set_fan_min, nr - 1);

fan(1);
fan(2);
418 419

/* alarms */
420 421
static ssize_t show_alarms(struct device *dev,
		struct device_attribute *attr, char *buf)
422 423 424 425 426 427
{
	struct adm9240_data *data = adm9240_update_device(dev);
	return sprintf(buf, "%u\n", data->alarms);
}
static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
static ssize_t show_alarm(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int bitnr = to_sensor_dev_attr(attr)->index;
	struct adm9240_data *data = adm9240_update_device(dev);
	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
}
static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);

445
/* vid */
446 447
static ssize_t show_vid(struct device *dev,
		struct device_attribute *attr, char *buf)
448 449 450 451 452 453 454
{
	struct adm9240_data *data = adm9240_update_device(dev);
	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
}
static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);

/* analog output */
455 456
static ssize_t show_aout(struct device *dev,
		struct device_attribute *attr, char *buf)
457 458 459 460 461
{
	struct adm9240_data *data = adm9240_update_device(dev);
	return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
}

462 463 464
static ssize_t set_aout(struct device *dev,
		struct device_attribute *attr,
		const char *buf, size_t count)
465 466 467 468 469
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	unsigned long val = simple_strtol(buf, NULL, 10);

470
	mutex_lock(&data->update_lock);
471
	data->aout = AOUT_TO_REG(val);
472
	i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
473
	mutex_unlock(&data->update_lock);
474 475 476 477 478
	return count;
}
static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);

/* chassis_clear */
479
static ssize_t chassis_clear_legacy(struct device *dev,
480 481
		struct device_attribute *attr,
		const char *buf, size_t count)
482 483 484 485
{
	struct i2c_client *client = to_i2c_client(dev);
	unsigned long val = simple_strtol(buf, NULL, 10);

486 487 488
	dev_warn(dev, "Attribute chassis_clear is deprecated, "
		 "use intrusion0_alarm instead\n");

489
	if (val == 1) {
490 491
		i2c_smbus_write_byte_data(client,
				ADM9240_REG_CHASSIS_CLEAR, 0x80);
492 493 494 495
		dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
	}
	return count;
}
496 497 498 499 500 501 502 503 504 505
static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear_legacy);

static ssize_t chassis_clear(struct device *dev,
		struct device_attribute *attr,
		const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	unsigned long val;

506
	if (kstrtoul(buf, 10, &val) || val != 0)
507 508 509 510 511 512 513 514 515 516 517 518
		return -EINVAL;

	mutex_lock(&data->update_lock);
	i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
	data->valid = 0;		/* Force cache refresh */
	mutex_unlock(&data->update_lock);
	dev_dbg(&client->dev, "chassis intrusion latch cleared\n");

	return count;
}
static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm,
		chassis_clear, 12);
519

520 521 522 523
static struct attribute *adm9240_attributes[] = {
	&sensor_dev_attr_in0_input.dev_attr.attr,
	&sensor_dev_attr_in0_min.dev_attr.attr,
	&sensor_dev_attr_in0_max.dev_attr.attr,
524
	&sensor_dev_attr_in0_alarm.dev_attr.attr,
525 526 527
	&sensor_dev_attr_in1_input.dev_attr.attr,
	&sensor_dev_attr_in1_min.dev_attr.attr,
	&sensor_dev_attr_in1_max.dev_attr.attr,
528
	&sensor_dev_attr_in1_alarm.dev_attr.attr,
529 530 531
	&sensor_dev_attr_in2_input.dev_attr.attr,
	&sensor_dev_attr_in2_min.dev_attr.attr,
	&sensor_dev_attr_in2_max.dev_attr.attr,
532
	&sensor_dev_attr_in2_alarm.dev_attr.attr,
533 534 535
	&sensor_dev_attr_in3_input.dev_attr.attr,
	&sensor_dev_attr_in3_min.dev_attr.attr,
	&sensor_dev_attr_in3_max.dev_attr.attr,
536
	&sensor_dev_attr_in3_alarm.dev_attr.attr,
537 538 539
	&sensor_dev_attr_in4_input.dev_attr.attr,
	&sensor_dev_attr_in4_min.dev_attr.attr,
	&sensor_dev_attr_in4_max.dev_attr.attr,
540
	&sensor_dev_attr_in4_alarm.dev_attr.attr,
541 542 543
	&sensor_dev_attr_in5_input.dev_attr.attr,
	&sensor_dev_attr_in5_min.dev_attr.attr,
	&sensor_dev_attr_in5_max.dev_attr.attr,
544
	&sensor_dev_attr_in5_alarm.dev_attr.attr,
545 546 547
	&dev_attr_temp1_input.attr,
	&sensor_dev_attr_temp1_max.dev_attr.attr,
	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
548
	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
549 550 551
	&sensor_dev_attr_fan1_input.dev_attr.attr,
	&sensor_dev_attr_fan1_div.dev_attr.attr,
	&sensor_dev_attr_fan1_min.dev_attr.attr,
552
	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
553 554 555
	&sensor_dev_attr_fan2_input.dev_attr.attr,
	&sensor_dev_attr_fan2_div.dev_attr.attr,
	&sensor_dev_attr_fan2_min.dev_attr.attr,
556
	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
557 558 559
	&dev_attr_alarms.attr,
	&dev_attr_aout_output.attr,
	&dev_attr_chassis_clear.attr,
560
	&sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
561 562 563 564 565 566 567 568
	&dev_attr_cpu0_vid.attr,
	NULL
};

static const struct attribute_group adm9240_group = {
	.attrs = adm9240_attributes,
};

569 570 571

/*** sensor chip detect and driver install ***/

572
/* Return 0 if detection is successful, -ENODEV otherwise */
573
static int adm9240_detect(struct i2c_client *new_client,
574
			  struct i2c_board_info *info)
575
{
576
	struct i2c_adapter *adapter = new_client->adapter;
577
	const char *name = "";
578
	int address = new_client->addr;
579 580 581
	u8 man_id, die_rev;

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
582
		return -ENODEV;
583

J
Jean Delvare 已提交
584 585 586 587 588 589
	/* verify chip: reg address should match i2c address */
	if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
			!= address) {
		dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
			address);
		return -ENODEV;
590 591
	}

J
Jean Delvare 已提交
592 593 594
	/* check known chip manufacturer */
	man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
	if (man_id == 0x23) {
595
		name = "adm9240";
J
Jean Delvare 已提交
596
	} else if (man_id == 0xda) {
597
		name = "ds1780";
J
Jean Delvare 已提交
598
	} else if (man_id == 0x01) {
599
		name = "lm81";
J
Jean Delvare 已提交
600 601 602 603
	} else {
		dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
			man_id);
		return -ENODEV;
604
	}
J
Jean Delvare 已提交
605 606 607 608 609 610 611

	/* successful detect, print chip info */
	die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
	dev_info(&adapter->dev, "found %s revision %u\n",
		 man_id == 0x23 ? "ADM9240" :
		 man_id == 0xda ? "DS1780" : "LM81", die_rev);

612
	strlcpy(info->type, name, I2C_NAME_SIZE);
613

614 615
	return 0;
}
616

617 618 619 620 621 622 623 624 625 626 627 628 629 630
static int adm9240_probe(struct i2c_client *new_client,
			 const struct i2c_device_id *id)
{
	struct adm9240_data *data;
	int err;

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (!data) {
		err = -ENOMEM;
		goto exit;
	}

	i2c_set_clientdata(new_client, data);
	mutex_init(&data->update_lock);
631 632 633 634

	adm9240_init_client(new_client);

	/* populate sysfs filesystem */
635
	if ((err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group)))
636
		goto exit_free;
637

638 639 640
	data->hwmon_dev = hwmon_device_register(&new_client->dev);
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
641
		goto exit_remove;
642 643
	}

644
	return 0;
645

646 647
exit_remove:
	sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
648
exit_free:
649
	kfree(data);
650 651 652 653
exit:
	return err;
}

654
static int adm9240_remove(struct i2c_client *client)
655
{
656
	struct adm9240_data *data = i2c_get_clientdata(client);
657

658
	hwmon_device_unregister(data->hwmon_dev);
659
	sysfs_remove_group(&client->dev.kobj, &adm9240_group);
660 661

	kfree(data);
662 663 664 665 666 667
	return 0;
}

static void adm9240_init_client(struct i2c_client *client)
{
	struct adm9240_data *data = i2c_get_clientdata(client);
668 669
	u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
	u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
670

671
	data->vrm = vid_which_vrm(); /* need this to report vid as mV */
672

673 674 675
	dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
			data->vrm % 10);

676 677 678 679 680 681 682 683 684 685
	if (conf & 1) { /* measurement cycle running: report state */

		dev_info(&client->dev, "status: config 0x%02x mode %u\n",
				conf, mode);

	} else { /* cold start: open limits before starting chip */
		int i;

		for (i = 0; i < 6; i++)
		{
686
			i2c_smbus_write_byte_data(client,
687
					ADM9240_REG_IN_MIN(i), 0);
688
			i2c_smbus_write_byte_data(client,
689 690
					ADM9240_REG_IN_MAX(i), 255);
		}
691 692 693 694 695
		i2c_smbus_write_byte_data(client,
				ADM9240_REG_FAN_MIN(0), 255);
		i2c_smbus_write_byte_data(client,
				ADM9240_REG_FAN_MIN(1), 255);
		i2c_smbus_write_byte_data(client,
696
				ADM9240_REG_TEMP_MAX(0), 127);
697
		i2c_smbus_write_byte_data(client,
698
				ADM9240_REG_TEMP_MAX(1), 127);
699 700

		/* start measurement cycle */
701
		i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
702 703 704 705 706 707 708 709 710 711 712 713

		dev_info(&client->dev, "cold start: config was 0x%02x "
				"mode %u\n", conf, mode);
	}
}

static struct adm9240_data *adm9240_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm9240_data *data = i2c_get_clientdata(client);
	int i;

714
	mutex_lock(&data->update_lock);
715 716 717 718 719 720 721

	/* minimum measurement cycle: 1.75 seconds */
	if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
			|| !data->valid) {

		for (i = 0; i < 6; i++) /* read voltages */
		{
722
			data->in[i] = i2c_smbus_read_byte_data(client,
723 724
					ADM9240_REG_IN(i));
		}
725
		data->alarms = i2c_smbus_read_byte_data(client,
726
					ADM9240_REG_INT(0)) |
727
					i2c_smbus_read_byte_data(client,
728 729 730 731 732
					ADM9240_REG_INT(1)) << 8;

		/* read temperature: assume temperature changes less than
		 * 0.5'C per two measurement cycles thus ignore possible
		 * but unlikely aliasing error on lsb reading. --Grant */
733
		data->temp = ((i2c_smbus_read_byte_data(client,
734
					ADM9240_REG_TEMP) << 8) |
735
					i2c_smbus_read_byte_data(client,
736 737 738 739
					ADM9240_REG_TEMP_CONF)) / 128;

		for (i = 0; i < 2; i++) /* read fans */
		{
740
			data->fan[i] = i2c_smbus_read_byte_data(client,
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
					ADM9240_REG_FAN(i));

			/* adjust fan clock divider on overflow */
			if (data->valid && data->fan[i] == 255 &&
					data->fan_div[i] < 3) {

				adm9240_write_fan_div(client, i,
						++data->fan_div[i]);

				/* adjust fan_min if active, but not to 0 */
				if (data->fan_min[i] < 255 &&
						data->fan_min[i] >= 2)
					data->fan_min[i] /= 2;
			}
		}
		data->last_updated_measure = jiffies;
	}

	/* minimum config reading cycle: 300 seconds */
	if (time_after(jiffies, data->last_updated_config + (HZ * 300))
			|| !data->valid) {

		for (i = 0; i < 6; i++)
		{
765
			data->in_min[i] = i2c_smbus_read_byte_data(client,
766
					ADM9240_REG_IN_MIN(i));
767
			data->in_max[i] = i2c_smbus_read_byte_data(client,
768 769 770 771
					ADM9240_REG_IN_MAX(i));
		}
		for (i = 0; i < 2; i++)
		{
772
			data->fan_min[i] = i2c_smbus_read_byte_data(client,
773 774
					ADM9240_REG_FAN_MIN(i));
		}
775 776 777 778
		data->temp_max[0] = i2c_smbus_read_byte_data(client,
				ADM9240_REG_TEMP_MAX(0));
		data->temp_max[1] = i2c_smbus_read_byte_data(client,
				ADM9240_REG_TEMP_MAX(1));
779 780

		/* read fan divs and 5-bit VID */
781
		i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
782 783 784
		data->fan_div[0] = (i >> 4) & 3;
		data->fan_div[1] = (i >> 6) & 3;
		data->vid = i & 0x0f;
785
		data->vid |= (i2c_smbus_read_byte_data(client,
786 787
					ADM9240_REG_VID4) & 1) << 4;
		/* read analog out */
788
		data->aout = i2c_smbus_read_byte_data(client,
789 790 791 792 793
				ADM9240_REG_ANALOG_OUT);

		data->last_updated_config = jiffies;
		data->valid = 1;
	}
794
	mutex_unlock(&data->update_lock);
795 796 797
	return data;
}

798
module_i2c_driver(adm9240_driver);
799 800

MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
801
		"Grant Coady <gcoady.lk@gmail.com> and others");
802 803
MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
MODULE_LICENSE("GPL");