adm1031.c 30.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
  adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
  monitoring
  Based on lm75.c and lm85.c
  Supports adm1030 / adm1031
  Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
  Reworked by Jean Delvare <khali@linux-fr.org>
J
Jean Delvare 已提交
8

L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  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/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
29 30
#include <linux/hwmon.h>
#include <linux/err.h>
31
#include <linux/mutex.h>
L
Linus Torvalds 已提交
32 33 34

/* Following macros takes channel parameter starting from 0 to 2 */
#define ADM1031_REG_FAN_SPEED(nr)	(0x08 + (nr))
J
Jean Delvare 已提交
35
#define ADM1031_REG_FAN_DIV(nr)		(0x20 + (nr))
L
Linus Torvalds 已提交
36 37 38
#define ADM1031_REG_PWM			(0x22)
#define ADM1031_REG_FAN_MIN(nr)		(0x10 + (nr))

J
Jean Delvare 已提交
39 40 41
#define ADM1031_REG_TEMP_MAX(nr)	(0x14 + 4 * (nr))
#define ADM1031_REG_TEMP_MIN(nr)	(0x15 + 4 * (nr))
#define ADM1031_REG_TEMP_CRIT(nr)	(0x16 + 4 * (nr))
L
Linus Torvalds 已提交
42

J
Jean Delvare 已提交
43
#define ADM1031_REG_TEMP(nr)		(0x0a + (nr))
L
Linus Torvalds 已提交
44 45 46 47
#define ADM1031_REG_AUTO_TEMP(nr)	(0x24 + (nr))

#define ADM1031_REG_STATUS(nr)		(0x2 + (nr))

J
Jean Delvare 已提交
48 49 50
#define ADM1031_REG_CONF1		0x00
#define ADM1031_REG_CONF2		0x01
#define ADM1031_REG_EXT_TEMP		0x06
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

#define ADM1031_CONF1_MONITOR_ENABLE	0x01	/* Monitoring enable */
#define ADM1031_CONF1_PWM_INVERT	0x08	/* PWM Invert */
#define ADM1031_CONF1_AUTO_MODE		0x80	/* Auto FAN */

#define ADM1031_CONF2_PWM1_ENABLE	0x01
#define ADM1031_CONF2_PWM2_ENABLE	0x02
#define ADM1031_CONF2_TACH1_ENABLE	0x04
#define ADM1031_CONF2_TACH2_ENABLE	0x08
#define ADM1031_CONF2_TEMP_ENABLE(chan)	(0x10 << (chan))

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

/* Insmod parameters */
66
I2C_CLIENT_INSMOD_2(adm1030, adm1031);
L
Linus Torvalds 已提交
67 68 69 70 71 72

typedef u8 auto_chan_table_t[8][2];

/* Each client has this additional data */
struct adm1031_data {
	struct i2c_client client;
73
	struct device *hwmon_dev;
74
	struct mutex update_lock;
L
Linus Torvalds 已提交
75 76 77 78 79 80
	int chip_type;
	char valid;		/* !=0 if following fields are valid */
	unsigned long last_updated;	/* In jiffies */
	/* The chan_select_table contains the possible configurations for
	 * auto fan control.
	 */
J
Jean Delvare 已提交
81
	const auto_chan_table_t *chan_select_table;
L
Linus Torvalds 已提交
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
	u16 alarm;
	u8 conf1;
	u8 conf2;
	u8 fan[2];
	u8 fan_div[2];
	u8 fan_min[2];
	u8 pwm[2];
	u8 old_pwm[2];
	s8 temp[3];
	u8 ext_temp[3];
	u8 auto_temp[3];
	u8 auto_temp_min[3];
	u8 auto_temp_off[3];
	u8 auto_temp_max[3];
	s8 temp_min[3];
	s8 temp_max[3];
	s8 temp_crit[3];
};

static int adm1031_attach_adapter(struct i2c_adapter *adapter);
static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
static void adm1031_init_client(struct i2c_client *client);
static int adm1031_detach_client(struct i2c_client *client);
static struct adm1031_data *adm1031_update_device(struct device *dev);

/* This is the driver that will be inserted */
static struct i2c_driver adm1031_driver = {
109 110 111
	.driver = {
		.name = "adm1031",
	},
L
Linus Torvalds 已提交
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
	.attach_adapter = adm1031_attach_adapter,
	.detach_client = adm1031_detach_client,
};

static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
{
	return i2c_smbus_read_byte_data(client, reg);
}

static inline int
adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
{
	return i2c_smbus_write_byte_data(client, reg, value);
}


#define TEMP_TO_REG(val)		(((val) < 0 ? ((val - 500) / 1000) : \
					((val + 500) / 1000)))

#define TEMP_FROM_REG(val)		((val) * 1000)

#define TEMP_FROM_REG_EXT(val, ext)	(TEMP_FROM_REG(val) + (ext) * 125)

#define FAN_FROM_REG(reg, div)		((reg) ? (11250 * 60) / ((reg) * (div)) : 0)

static int FAN_TO_REG(int reg, int div)
{
	int tmp;
	tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
	return tmp > 255 ? 255 : tmp;
}

#define FAN_DIV_FROM_REG(reg)		(1<<(((reg)&0xc0)>>6))

#define PWM_TO_REG(val)			(SENSORS_LIMIT((val), 0, 255) >> 4)
#define PWM_FROM_REG(val)		((val) << 4)

#define FAN_CHAN_FROM_REG(reg)		(((reg) >> 5) & 7)
#define FAN_CHAN_TO_REG(val, reg)	\
	(((reg) & 0x1F) | (((val) << 5) & 0xe0))

#define AUTO_TEMP_MIN_TO_REG(val, reg)	\
	((((val)/500) & 0xf8)|((reg) & 0x7))
#define AUTO_TEMP_RANGE_FROM_REG(reg)	(5000 * (1<< ((reg)&0x7)))
#define AUTO_TEMP_MIN_FROM_REG(reg)	(1000 * ((((reg) >> 3) & 0x1f) << 2))

#define AUTO_TEMP_MIN_FROM_REG_DEG(reg)	((((reg) >> 3) & 0x1f) << 2)

#define AUTO_TEMP_OFF_FROM_REG(reg)		\
	(AUTO_TEMP_MIN_FROM_REG(reg) - 5000)

#define AUTO_TEMP_MAX_FROM_REG(reg)		\
	(AUTO_TEMP_RANGE_FROM_REG(reg) +	\
	AUTO_TEMP_MIN_FROM_REG(reg))

static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
{
	int ret;
	int range = val - AUTO_TEMP_MIN_FROM_REG(reg);

	range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
	ret = ((reg & 0xf8) |
	       (range < 10000 ? 0 :
		range < 20000 ? 1 :
		range < 40000 ? 2 : range < 80000 ? 3 : 4));
	return ret;
}

/* FAN auto control */
#define GET_FAN_AUTO_BITFIELD(data, idx)	\
	(*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]

J
Jean Delvare 已提交
184
/* The tables below contains the possible values for the auto fan
L
Linus Torvalds 已提交
185 186 187 188
 * control bitfields. the index in the table is the register value.
 * MSb is the auto fan control enable bit, so the four first entries
 * in the table disables auto fan control when both bitfields are zero.
 */
J
Jean Delvare 已提交
189 190 191 192 193 194
static const auto_chan_table_t auto_channel_select_table_adm1031 = {
	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
	{ 2 /* 0b010 */ , 4 /* 0b100 */ },
	{ 2 /* 0b010 */ , 2 /* 0b010 */ },
	{ 4 /* 0b100 */ , 4 /* 0b100 */ },
	{ 7 /* 0b111 */ , 7 /* 0b111 */ },
L
Linus Torvalds 已提交
195 196
};

J
Jean Delvare 已提交
197 198 199 200 201 202
static const auto_chan_table_t auto_channel_select_table_adm1030 = {
	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
	{ 2 /* 0b10 */		, 0 },
	{ 0xff /* invalid */	, 0 },
	{ 0xff /* invalid */	, 0 },
	{ 3 /* 0b11 */		, 0 },
L
Linus Torvalds 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
};

/* That function checks if a bitfield is valid and returns the other bitfield
 * nearest match if no exact match where found.
 */
static int
get_fan_auto_nearest(struct adm1031_data *data,
		     int chan, u8 val, u8 reg, u8 * new_reg)
{
	int i;
	int first_match = -1, exact_match = -1;
	u8 other_reg_val =
	    (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];

	if (val == 0) {
		*new_reg = 0;
		return 0;
	}

	for (i = 0; i < 8; i++) {
		if ((val == (*data->chan_select_table)[i][chan]) &&
		    ((*data->chan_select_table)[i][chan ? 0 : 1] ==
		     other_reg_val)) {
			/* We found an exact match */
			exact_match = i;
			break;
		} else if (val == (*data->chan_select_table)[i][chan] &&
			   first_match == -1) {
J
Jean Delvare 已提交
231 232
			/* Save the first match in case of an exact match has
			 * not been found
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 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
			 */
			first_match = i;
		}
	}

	if (exact_match >= 0) {
		*new_reg = exact_match;
	} else if (first_match >= 0) {
		*new_reg = first_match;
	} else {
		return -EINVAL;
	}
	return 0;
}

static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
}

static ssize_t
set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
	u8 reg;
	int ret;
	u8 old_fan_mode;

	old_fan_mode = data->conf1;

266
	mutex_lock(&data->update_lock);
J
Jean Delvare 已提交
267

L
Linus Torvalds 已提交
268
	if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
269
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
270 271
		return ret;
	}
J
Jean Delvare 已提交
272 273
	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
L
Linus Torvalds 已提交
274 275
	    (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
		if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
J
Jean Delvare 已提交
276 277
			/* Switch to Auto Fan Mode
			 * Save PWM registers
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286
			 * Set PWM registers to 33% Both */
			data->old_pwm[0] = data->pwm[0];
			data->old_pwm[1] = data->pwm[1];
			adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
		} else {
			/* Switch to Manual Mode */
			data->pwm[0] = data->old_pwm[0];
			data->pwm[1] = data->old_pwm[1];
			/* Restore PWM registers */
J
Jean Delvare 已提交
287
			adm1031_write_value(client, ADM1031_REG_PWM,
L
Linus Torvalds 已提交
288 289 290 291 292
					    data->pwm[0] | (data->pwm[1] << 4));
		}
	}
	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
	adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
293
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
294 295 296 297
	return count;
}

#define fan_auto_channel_offset(offset)						\
298
static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
299 300 301
{										\
	return show_fan_auto_channel(dev, buf, offset - 1);			\
}										\
302
static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr,		\
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	const char *buf, size_t count)						\
{										\
	return set_fan_auto_channel(dev, buf, count, offset - 1);		\
}										\
static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR,		\
		   show_fan_auto_channel_##offset,				\
		   set_fan_auto_channel_##offset)

fan_auto_channel_offset(1);
fan_auto_channel_offset(2);

/* Auto Temps */
static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
J
Jean Delvare 已提交
318
	return sprintf(buf, "%d\n",
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
		       AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
}
static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n",
		       AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
}
static ssize_t
set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

334
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
335 336 337
	data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
			    data->auto_temp[nr]);
338
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	return count;
}
static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n",
		       AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
}
static ssize_t
set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

354
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
355 356 357
	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
			    data->temp_max[nr]);
358
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
359 360 361 362
	return count;
}

#define auto_temp_reg(offset)							\
363
static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
364 365 366
{										\
	return show_auto_temp_off(dev, buf, offset - 1);			\
}										\
367
static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
368 369 370
{										\
	return show_auto_temp_min(dev, buf, offset - 1);			\
}										\
371
static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
372 373 374
{										\
	return show_auto_temp_max(dev, buf, offset - 1);			\
}										\
375
static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr,		\
L
Linus Torvalds 已提交
376 377 378 379
					     const char *buf, size_t count)	\
{										\
	return set_auto_temp_min(dev, buf, count, offset - 1);		\
}										\
380
static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr,		\
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
					     const char *buf, size_t count)	\
{										\
	return set_auto_temp_max(dev, buf, count, offset - 1);		\
}										\
static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO,				\
		   show_auto_temp_##offset##_off, NULL);			\
static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR,			\
		   show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR,			\
		   show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)

auto_temp_reg(1);
auto_temp_reg(2);
auto_temp_reg(3);

/* pwm */
static ssize_t show_pwm(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
}
static ssize_t
set_pwm(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
	int reg;

410
	mutex_lock(&data->update_lock);
J
Jean Delvare 已提交
411
	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
L
Linus Torvalds 已提交
412 413
	    (((val>>4) & 0xf) != 5)) {
		/* In automatic mode, the only PWM accepted is 33% */
414
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
415 416 417 418 419 420 421
		return -EINVAL;
	}
	data->pwm[nr] = PWM_TO_REG(val);
	reg = adm1031_read_value(client, ADM1031_REG_PWM);
	adm1031_write_value(client, ADM1031_REG_PWM,
			    nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
			    : (data->pwm[nr] & 0xf) | (reg & 0xf0));
422
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
423 424 425 426
	return count;
}

#define pwm_reg(offset)							\
427
static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
428 429 430
{									\
	return show_pwm(dev, buf, offset - 1);			\
}									\
431
static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr,			\
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445
				 const char *buf, size_t count)		\
{									\
	return set_pwm(dev, buf, count, offset - 1);		\
}									\
static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,			\
		   show_pwm_##offset, set_pwm_##offset)

pwm_reg(1);
pwm_reg(2);

/* Fans */

/*
 * That function checks the cases where the fan reading is not
446
 * relevant.  It is used to provide 0 as fan reading when the fan is
L
Linus Torvalds 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
 * not supposed to run
 */
static int trust_fan_readings(struct adm1031_data *data, int chan)
{
	int res = 0;

	if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
		switch (data->conf1 & 0x60) {
		case 0x00:	/* remote temp1 controls fan1 remote temp2 controls fan2 */
			res = data->temp[chan+1] >=
			      AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
			break;
		case 0x20:	/* remote temp1 controls both fans */
			res =
			    data->temp[1] >=
			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
			break;
		case 0x40:	/* remote temp2 controls both fans */
			res =
			    data->temp[2] >=
			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
			break;
		case 0x60:	/* max controls both fans */
			res =
			    data->temp[0] >=
			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
			    || data->temp[1] >=
			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
J
Jean Delvare 已提交
475
			    || (data->chip_type == adm1031
L
Linus Torvalds 已提交
476 477 478 479 480 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 508 509 510 511 512 513 514 515
				&& data->temp[2] >=
				AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
			break;
		}
	} else {
		res = data->pwm[chan] > 0;
	}
	return res;
}


static ssize_t show_fan(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	int value;

	value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
				 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
	return sprintf(buf, "%d\n", value);
}

static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
}
static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n",
		       FAN_FROM_REG(data->fan_min[nr],
				    FAN_DIV_FROM_REG(data->fan_div[nr])));
}
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 adm1031_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

516
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
517
	if (val) {
J
Jean Delvare 已提交
518
		data->fan_min[nr] =
L
Linus Torvalds 已提交
519 520 521 522 523
			FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
	} else {
		data->fan_min[nr] = 0xff;
	}
	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
524
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538
	return count;
}
static ssize_t
set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
	u8 tmp;
	int old_div;
	int new_min;

	tmp = val == 8 ? 0xc0 :
	      val == 4 ? 0x80 :
J
Jean Delvare 已提交
539 540
	      val == 2 ? 0x40 :
	      val == 1 ? 0x00 :
L
Linus Torvalds 已提交
541 542 543
	      0xff;
	if (tmp == 0xff)
		return -EINVAL;
J
Jean Delvare 已提交
544

545
	mutex_lock(&data->update_lock);
546 547 548 549 550 551 552
	/* Get fresh readings */
	data->fan_div[nr] = adm1031_read_value(client,
					       ADM1031_REG_FAN_DIV(nr));
	data->fan_min[nr] = adm1031_read_value(client,
					       ADM1031_REG_FAN_MIN(nr));

	/* Write the new clock divider and fan min */
L
Linus Torvalds 已提交
553
	old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
J
Jean Delvare 已提交
554 555
	data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
	new_min = data->fan_min[nr] * old_div / val;
L
Linus Torvalds 已提交
556 557
	data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;

J
Jean Delvare 已提交
558
	adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
L
Linus Torvalds 已提交
559
			    data->fan_div[nr]);
J
Jean Delvare 已提交
560
	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
L
Linus Torvalds 已提交
561
			    data->fan_min[nr]);
562 563 564

	/* Invalidate the cache: fan speed is no longer valid */
	data->valid = 0;
565
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
566 567 568 569
	return count;
}

#define fan_offset(offset)						\
570
static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
571 572 573
{									\
	return show_fan(dev, buf, offset - 1);			\
}									\
574
static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
575 576 577
{									\
	return show_fan_min(dev, buf, offset - 1);			\
}									\
578
static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
579 580 581
{									\
	return show_fan_div(dev, buf, offset - 1);			\
}									\
582
static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr,		\
L
Linus Torvalds 已提交
583 584 585 586
	const char *buf, size_t count)					\
{									\
	return set_fan_min(dev, buf, count, offset - 1);		\
}									\
587
static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr,		\
L
Linus Torvalds 已提交
588 589 590 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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	const char *buf, size_t count)					\
{									\
	return set_fan_div(dev, buf, count, offset - 1);		\
}									\
static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset,	\
		   NULL);						\
static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\
		   show_fan_##offset##_min, set_fan_##offset##_min);	\
static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,		\
		   show_fan_##offset##_div, set_fan_##offset##_div);	\
static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR,	\
		   show_pwm_##offset, set_pwm_##offset)

fan_offset(1);
fan_offset(2);


/* Temps */
static ssize_t show_temp(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	int ext;
	ext = nr == 0 ?
	    ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
	    (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
	return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
}
static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
}
static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
}
static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
}
static ssize_t
set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val;

	val = simple_strtol(buf, NULL, 10);
	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
639
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
640 641 642
	data->temp_min[nr] = TEMP_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
			    data->temp_min[nr]);
643
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652 653 654
	return count;
}
static ssize_t
set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val;

	val = simple_strtol(buf, NULL, 10);
	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
655
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
656 657 658
	data->temp_max[nr] = TEMP_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
			    data->temp_max[nr]);
659
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667 668 669 670
	return count;
}
static ssize_t
set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int val;

	val = simple_strtol(buf, NULL, 10);
	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
671
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
672 673 674
	data->temp_crit[nr] = TEMP_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
			    data->temp_crit[nr]);
675
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
676 677 678 679
	return count;
}

#define temp_reg(offset)							\
680
static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
681 682 683
{										\
	return show_temp(dev, buf, offset - 1);				\
}										\
684
static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
685 686 687
{										\
	return show_temp_min(dev, buf, offset - 1);				\
}										\
688
static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)		\
L
Linus Torvalds 已提交
689 690 691
{										\
	return show_temp_max(dev, buf, offset - 1);				\
}										\
692
static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
693 694 695
{										\
	return show_temp_crit(dev, buf, offset - 1);			\
}										\
696
static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr,			\
L
Linus Torvalds 已提交
697 698 699 700
					const char *buf, size_t count)		\
{										\
	return set_temp_min(dev, buf, count, offset - 1);			\
}										\
701
static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr,			\
L
Linus Torvalds 已提交
702 703 704 705
					const char *buf, size_t count)		\
{										\
	return set_temp_max(dev, buf, count, offset - 1);			\
}										\
706
static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr,			\
L
Linus Torvalds 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
					 const char *buf, size_t count)		\
{										\
	return set_temp_crit(dev, buf, count, offset - 1);			\
}										\
static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset,		\
		   NULL);							\
static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,			\
		   show_temp_##offset##_min, set_temp_##offset##_min);		\
static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,			\
		   show_temp_##offset##_max, set_temp_##offset##_max);		\
static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,			\
		   show_temp_##offset##_crit, set_temp_##offset##_crit)

temp_reg(1);
temp_reg(2);
temp_reg(3);

/* Alarms */
725
static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733 734 735 736 737
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", data->alarm);
}

static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);


static int adm1031_attach_adapter(struct i2c_adapter *adapter)
{
	if (!(adapter->class & I2C_CLASS_HWMON))
		return 0;
738
	return i2c_probe(adapter, &addr_data, adm1031_detect);
L
Linus Torvalds 已提交
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
static struct attribute *adm1031_attributes[] = {
	&dev_attr_fan1_input.attr,
	&dev_attr_fan1_div.attr,
	&dev_attr_fan1_min.attr,
	&dev_attr_pwm1.attr,
	&dev_attr_auto_fan1_channel.attr,
	&dev_attr_temp1_input.attr,
	&dev_attr_temp1_min.attr,
	&dev_attr_temp1_max.attr,
	&dev_attr_temp1_crit.attr,
	&dev_attr_temp2_input.attr,
	&dev_attr_temp2_min.attr,
	&dev_attr_temp2_max.attr,
	&dev_attr_temp2_crit.attr,

	&dev_attr_auto_temp1_off.attr,
	&dev_attr_auto_temp1_min.attr,
	&dev_attr_auto_temp1_max.attr,

	&dev_attr_auto_temp2_off.attr,
	&dev_attr_auto_temp2_min.attr,
	&dev_attr_auto_temp2_max.attr,

	&dev_attr_auto_fan1_min_pwm.attr,

	&dev_attr_alarms.attr,

	NULL
};

static const struct attribute_group adm1031_group = {
	.attrs = adm1031_attributes,
};

static struct attribute *adm1031_attributes_opt[] = {
	&dev_attr_fan2_input.attr,
	&dev_attr_fan2_div.attr,
	&dev_attr_fan2_min.attr,
	&dev_attr_pwm2.attr,
	&dev_attr_auto_fan2_channel.attr,
	&dev_attr_temp3_input.attr,
	&dev_attr_temp3_min.attr,
	&dev_attr_temp3_max.attr,
	&dev_attr_temp3_crit.attr,
	&dev_attr_auto_temp3_off.attr,
	&dev_attr_auto_temp3_min.attr,
	&dev_attr_auto_temp3_max.attr,
	&dev_attr_auto_fan2_min_pwm.attr,
	NULL
};

static const struct attribute_group adm1031_group_opt = {
	.attrs = adm1031_attributes_opt,
};

796
/* This function is called by i2c_probe */
L
Linus Torvalds 已提交
797 798
static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
{
J
Jean Delvare 已提交
799
	struct i2c_client *client;
L
Linus Torvalds 已提交
800 801 802 803 804 805 806
	struct adm1031_data *data;
	int err = 0;
	const char *name = "";

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		goto exit;

D
Deepak Saxena 已提交
807
	if (!(data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
L
Linus Torvalds 已提交
808 809 810 811
		err = -ENOMEM;
		goto exit;
	}

J
Jean Delvare 已提交
812 813 814 815 816
	client = &data->client;
	i2c_set_clientdata(client, data);
	client->addr = address;
	client->adapter = adapter;
	client->driver = &adm1031_driver;
L
Linus Torvalds 已提交
817 818 819

	if (kind < 0) {
		int id, co;
J
Jean Delvare 已提交
820 821
		id = i2c_smbus_read_byte_data(client, 0x3d);
		co = i2c_smbus_read_byte_data(client, 0x3e);
L
Linus Torvalds 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841

		if (!((id == 0x31 || id == 0x30) && co == 0x41))
			goto exit_free;
		kind = (id == 0x30) ? adm1030 : adm1031;
	}

	if (kind <= 0)
		kind = adm1031;

	/* Given the detected chip type, set the chip name and the
	 * auto fan control helper table. */
	if (kind == adm1030) {
		name = "adm1030";
		data->chan_select_table = &auto_channel_select_table_adm1030;
	} else if (kind == adm1031) {
		name = "adm1031";
		data->chan_select_table = &auto_channel_select_table_adm1031;
	}
	data->chip_type = kind;

J
Jean Delvare 已提交
842
	strlcpy(client->name, name, I2C_NAME_SIZE);
843
	mutex_init(&data->update_lock);
L
Linus Torvalds 已提交
844 845

	/* Tell the I2C layer a new client has arrived */
J
Jean Delvare 已提交
846
	if ((err = i2c_attach_client(client)))
L
Linus Torvalds 已提交
847 848 849
		goto exit_free;

	/* Initialize the ADM1031 chip */
J
Jean Delvare 已提交
850
	adm1031_init_client(client);
L
Linus Torvalds 已提交
851 852

	/* Register sysfs hooks */
J
Jean Delvare 已提交
853
	if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
854
		goto exit_detach;
L
Linus Torvalds 已提交
855 856

	if (kind == adm1031) {
J
Jean Delvare 已提交
857
		if ((err = sysfs_create_group(&client->dev.kobj,
858 859 860 861
						&adm1031_group_opt)))
			goto exit_remove;
	}

J
Jean Delvare 已提交
862
	data->hwmon_dev = hwmon_device_register(&client->dev);
863 864
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
865
		goto exit_remove;
L
Linus Torvalds 已提交
866 867 868 869
	}

	return 0;

870
exit_remove:
J
Jean Delvare 已提交
871 872
	sysfs_remove_group(&client->dev.kobj, &adm1031_group);
	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
873
exit_detach:
J
Jean Delvare 已提交
874
	i2c_detach_client(client);
L
Linus Torvalds 已提交
875
exit_free:
876
	kfree(data);
L
Linus Torvalds 已提交
877 878 879 880 881 882
exit:
	return err;
}

static int adm1031_detach_client(struct i2c_client *client)
{
883
	struct adm1031_data *data = i2c_get_clientdata(client);
L
Linus Torvalds 已提交
884
	int ret;
885

886
	hwmon_device_unregister(data->hwmon_dev);
887 888
	sysfs_remove_group(&client->dev.kobj, &adm1031_group);
	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
L
Linus Torvalds 已提交
889 890 891
	if ((ret = i2c_detach_client(client)) != 0) {
		return ret;
	}
892
	kfree(data);
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905
	return 0;
}

static void adm1031_init_client(struct i2c_client *client)
{
	unsigned int read_val;
	unsigned int mask;
	struct adm1031_data *data = i2c_get_clientdata(client);

	mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
	if (data->chip_type == adm1031) {
		mask |= (ADM1031_CONF2_PWM2_ENABLE |
			ADM1031_CONF2_TACH2_ENABLE);
J
Jean Delvare 已提交
906
	}
L
Linus Torvalds 已提交
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
	/* Initialize the ADM1031 chip (enables fan speed reading ) */
	read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
	if ((read_val | mask) != read_val) {
	    adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
	}

	read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
	if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
	    adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
				ADM1031_CONF1_MONITOR_ENABLE);
	}

}

static struct adm1031_data *adm1031_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int chan;

927
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
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 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
	    || !data->valid) {

		dev_dbg(&client->dev, "Starting adm1031 update\n");
		for (chan = 0;
		     chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
			u8 oldh, newh;

			oldh =
			    adm1031_read_value(client, ADM1031_REG_TEMP(chan));
			data->ext_temp[chan] =
			    adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
			newh =
			    adm1031_read_value(client, ADM1031_REG_TEMP(chan));
			if (newh != oldh) {
				data->ext_temp[chan] =
				    adm1031_read_value(client,
						       ADM1031_REG_EXT_TEMP);
#ifdef DEBUG
				oldh =
				    adm1031_read_value(client,
						       ADM1031_REG_TEMP(chan));

				/* oldh is actually newer */
				if (newh != oldh)
					dev_warn(&client->dev,
						 "Remote temperature may be "
						 "wrong.\n");
#endif
			}
			data->temp[chan] = newh;

			data->temp_min[chan] =
			    adm1031_read_value(client,
					       ADM1031_REG_TEMP_MIN(chan));
			data->temp_max[chan] =
			    adm1031_read_value(client,
					       ADM1031_REG_TEMP_MAX(chan));
			data->temp_crit[chan] =
			    adm1031_read_value(client,
					       ADM1031_REG_TEMP_CRIT(chan));
			data->auto_temp[chan] =
			    adm1031_read_value(client,
					       ADM1031_REG_AUTO_TEMP(chan));

		}

		data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
		data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);

		data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
			     | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
				<< 8);
		if (data->chip_type == adm1030) {
			data->alarm &= 0xc0ff;
		}
J
Jean Delvare 已提交
985

L
Linus Torvalds 已提交
986 987 988 989 990 991 992 993
		for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
			data->fan_div[chan] =
			    adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
			data->fan_min[chan] =
			    adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
			data->fan[chan] =
			    adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
			data->pwm[chan] =
J
Jean Delvare 已提交
994
			    0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
L
Linus Torvalds 已提交
995 996 997 998 999 1000
				   (4*chan));
		}
		data->last_updated = jiffies;
		data->valid = 1;
	}

1001
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	return data;
}

static int __init sensors_adm1031_init(void)
{
	return i2c_add_driver(&adm1031_driver);
}

static void __exit sensors_adm1031_exit(void)
{
	i2c_del_driver(&adm1031_driver);
}

MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
MODULE_LICENSE("GPL");

module_init(sensors_adm1031_init);
module_exit(sensors_adm1031_exit);