adm1031.c 33.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3 4 5 6
 * 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>
7
 * Reworked by Jean Delvare <jdelvare@suse.de>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */
L
Linus Torvalds 已提交
23 24 25 26 27 28

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
29
#include <linux/hwmon.h>
30
#include <linux/hwmon-sysfs.h>
31
#include <linux/err.h>
32
#include <linux/mutex.h>
L
Linus Torvalds 已提交
33 34 35

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

41
#define ADM1031_REG_TEMP_OFFSET(nr)	(0x0d + (nr))
J
Jean Delvare 已提交
42 43 44
#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 已提交
45

J
Jean Delvare 已提交
46
#define ADM1031_REG_TEMP(nr)		(0x0a + (nr))
L
Linus Torvalds 已提交
47 48 49 50
#define ADM1031_REG_AUTO_TEMP(nr)	(0x24 + (nr))

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

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

#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))

65 66 67
#define ADM1031_UPDATE_RATE_MASK	0x1c
#define ADM1031_UPDATE_RATE_SHIFT	2

L
Linus Torvalds 已提交
68
/* Addresses to scan */
69
static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
L
Linus Torvalds 已提交
70

J
Jean Delvare 已提交
71
enum chips { adm1030, adm1031 };
L
Linus Torvalds 已提交
72 73 74 75 76

typedef u8 auto_chan_table_t[8][2];

/* Each client has this additional data */
struct adm1031_data {
77
	struct device *hwmon_dev;
78
	struct mutex update_lock;
L
Linus Torvalds 已提交
79 80 81
	int chip_type;
	char valid;		/* !=0 if following fields are valid */
	unsigned long last_updated;	/* In jiffies */
82
	unsigned int update_interval;	/* In milliseconds */
83 84
	/*
	 * The chan_select_table contains the possible configurations for
L
Linus Torvalds 已提交
85 86
	 * auto fan control.
	 */
J
Jean Delvare 已提交
87
	const auto_chan_table_t *chan_select_table;
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101
	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];
102
	s8 temp_offset[3];
L
Linus Torvalds 已提交
103 104 105 106 107
	s8 temp_min[3];
	s8 temp_max[3];
	s8 temp_crit[3];
};

108 109
static int adm1031_probe(struct i2c_client *client,
			 const struct i2c_device_id *id);
110
static int adm1031_detect(struct i2c_client *client,
111
			  struct i2c_board_info *info);
L
Linus Torvalds 已提交
112
static void adm1031_init_client(struct i2c_client *client);
113
static int adm1031_remove(struct i2c_client *client);
L
Linus Torvalds 已提交
114 115
static struct adm1031_data *adm1031_update_device(struct device *dev);

116 117 118 119 120 121 122
static const struct i2c_device_id adm1031_id[] = {
	{ "adm1030", adm1030 },
	{ "adm1031", adm1031 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, adm1031_id);

L
Linus Torvalds 已提交
123 124
/* This is the driver that will be inserted */
static struct i2c_driver adm1031_driver = {
125
	.class		= I2C_CLASS_HWMON,
126 127 128
	.driver = {
		.name = "adm1031",
	},
129 130 131 132
	.probe		= adm1031_probe,
	.remove		= adm1031_remove,
	.id_table	= adm1031_id,
	.detect		= adm1031_detect,
133
	.address_list	= normal_i2c,
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
};

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)

155 156 157 158
#define TEMP_OFFSET_TO_REG(val)		(TEMP_TO_REG(val) & 0x8f)
#define TEMP_OFFSET_FROM_REG(val)	TEMP_FROM_REG((val) < 0 ? \
						      (val) | 0x70 : (val))

159 160
#define FAN_FROM_REG(reg, div)		((reg) ? \
					 (11250 * 60) / ((reg) * (div)) : 0)
L
Linus Torvalds 已提交
161 162 163 164

static int FAN_TO_REG(int reg, int div)
{
	int tmp;
165
	tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div);
L
Linus Torvalds 已提交
166 167 168 169 170
	return tmp > 255 ? 255 : tmp;
}

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

171
#define PWM_TO_REG(val)			(clamp_val((val), 0, 255) >> 4)
L
Linus Torvalds 已提交
172 173 174 175 176 177 178
#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)	\
179 180
	((((val) / 500) & 0xf8) | ((reg) & 0x7))
#define AUTO_TEMP_RANGE_FROM_REG(reg)	(5000 * (1 << ((reg) & 0x7)))
L
Linus Torvalds 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#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)	\
207
	(*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
L
Linus Torvalds 已提交
208

209 210
/*
 * The tables below contains the possible values for the auto fan
L
Linus Torvalds 已提交
211 212 213 214
 * 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 已提交
215 216 217 218 219 220
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 已提交
221 222
};

J
Jean Delvare 已提交
223 224 225 226 227 228
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 已提交
229 230
};

231 232
/*
 * That function checks if a bitfield is valid and returns the other bitfield
L
Linus Torvalds 已提交
233 234 235
 * nearest match if no exact match where found.
 */
static int
236
get_fan_auto_nearest(struct adm1031_data *data, int chan, u8 val, u8 reg)
L
Linus Torvalds 已提交
237 238 239 240 241 242
{
	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];

243
	if (val == 0)
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254
		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) {
255 256
			/*
			 * Save the first match in case of an exact match has
J
Jean Delvare 已提交
257
			 * not been found
L
Linus Torvalds 已提交
258 259 260 261 262
			 */
			first_match = i;
		}
	}

263
	if (exact_match >= 0)
264
		return exact_match;
265
	else if (first_match >= 0)
266
		return first_match;
267

268
	return -EINVAL;
L
Linus Torvalds 已提交
269 270
}

271 272
static ssize_t show_fan_auto_channel(struct device *dev,
				     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
273
{
274
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
275 276 277 278 279
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
}

static ssize_t
280 281
set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
		     const char *buf, size_t count)
L
Linus Torvalds 已提交
282 283 284
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
285
	int nr = to_sensor_dev_attr(attr)->index;
286
	long val;
L
Linus Torvalds 已提交
287 288 289 290
	u8 reg;
	int ret;
	u8 old_fan_mode;

291 292 293 294
	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;

L
Linus Torvalds 已提交
295 296
	old_fan_mode = data->conf1;

297
	mutex_lock(&data->update_lock);
J
Jean Delvare 已提交
298

299 300
	ret = get_fan_auto_nearest(data, nr, val, data->conf1);
	if (ret < 0) {
301
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
302 303
		return ret;
	}
304
	reg = ret;
J
Jean Delvare 已提交
305 306
	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
L
Linus Torvalds 已提交
307
	    (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
308
		if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
309 310
			/*
			 * Switch to Auto Fan Mode
J
Jean Delvare 已提交
311
			 * Save PWM registers
312 313
			 * Set PWM registers to 33% Both
			 */
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321
			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 已提交
322
			adm1031_write_value(client, ADM1031_REG_PWM,
L
Linus Torvalds 已提交
323 324 325 326 327
					    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);
328
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
329 330 331
	return count;
}

332 333 334 335
static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
		show_fan_auto_channel, set_fan_auto_channel, 0);
static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
		show_fan_auto_channel, set_fan_auto_channel, 1);
L
Linus Torvalds 已提交
336 337

/* Auto Temps */
338 339
static ssize_t show_auto_temp_off(struct device *dev,
				  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
340
{
341
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
342
	struct adm1031_data *data = adm1031_update_device(dev);
J
Jean Delvare 已提交
343
	return sprintf(buf, "%d\n",
L
Linus Torvalds 已提交
344 345
		       AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
}
346 347
static ssize_t show_auto_temp_min(struct device *dev,
				  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
348
{
349
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
350 351 352 353 354
	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
355 356
set_auto_temp_min(struct device *dev, struct device_attribute *attr,
		  const char *buf, size_t count)
L
Linus Torvalds 已提交
357 358 359
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
360
	int nr = to_sensor_dev_attr(attr)->index;
361 362 363 364 365 366
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
367

368
	val = clamp_val(val, 0, 127000);
369
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
370 371 372
	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]);
373
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
374 375
	return count;
}
376 377
static ssize_t show_auto_temp_max(struct device *dev,
				  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
378
{
379
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
380 381 382 383 384
	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
385 386
set_auto_temp_max(struct device *dev, struct device_attribute *attr,
		  const char *buf, size_t count)
L
Linus Torvalds 已提交
387 388 389
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
390
	int nr = to_sensor_dev_attr(attr)->index;
391 392 393 394 395 396
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
397

398
	val = clamp_val(val, 0, 127000);
399
	mutex_lock(&data->update_lock);
400 401
	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
						  data->pwm[nr]);
L
Linus Torvalds 已提交
402 403
	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
			    data->temp_max[nr]);
404
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
405 406 407
	return count;
}

408 409 410 411 412 413 414
#define auto_temp_reg(offset)						\
static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO,		\
		show_auto_temp_off, NULL, offset - 1);			\
static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR,	\
		show_auto_temp_min, set_auto_temp_min, offset - 1);	\
static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR,	\
		show_auto_temp_max, set_auto_temp_max, offset - 1)
L
Linus Torvalds 已提交
415 416 417 418 419 420

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

/* pwm */
421 422
static ssize_t show_pwm(struct device *dev,
			struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
423
{
424
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
425 426 427
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
}
428 429
static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
		       const char *buf, size_t count)
L
Linus Torvalds 已提交
430 431 432
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
433
	int nr = to_sensor_dev_attr(attr)->index;
434 435 436 437 438 439
	long val;
	int ret, reg;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
440

441
	mutex_lock(&data->update_lock);
J
Jean Delvare 已提交
442
	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
L
Linus Torvalds 已提交
443 444
	    (((val>>4) & 0xf) != 5)) {
		/* In automatic mode, the only PWM accepted is 33% */
445
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
446 447 448 449 450 451 452
		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));
453
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
454 455 456
	return count;
}

457 458 459 460 461 462
static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
		show_pwm, set_pwm, 0);
static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
		show_pwm, set_pwm, 1);
L
Linus Torvalds 已提交
463 464 465 466 467

/* Fans */

/*
 * That function checks the cases where the fan reading is not
468
 * relevant.  It is used to provide 0 as fan reading when the fan is
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476
 * 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) {
477 478 479 480 481
		case 0x00:
			/*
			 * remote temp1 controls fan1,
			 * remote temp2 controls fan2
			 */
L
Linus Torvalds 已提交
482
			res = data->temp[chan+1] >=
483
			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
			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 已提交
501
			    || (data->chip_type == adm1031
L
Linus Torvalds 已提交
502 503 504 505 506 507 508 509 510 511 512
				&& data->temp[2] >=
				AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
			break;
		}
	} else {
		res = data->pwm[chan] > 0;
	}
	return res;
}


513 514
static ssize_t show_fan(struct device *dev,
			struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
515
{
516
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524
	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);
}

525 526
static ssize_t show_fan_div(struct device *dev,
			    struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
527
{
528
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
529 530 531
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
}
532 533
static ssize_t show_fan_min(struct device *dev,
			    struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
534
{
535
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
536 537 538 539 540
	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])));
}
541 542
static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
L
Linus Torvalds 已提交
543 544 545
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
546
	int nr = to_sensor_dev_attr(attr)->index;
547 548 549 550 551 552
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
553

554
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
555
	if (val) {
J
Jean Delvare 已提交
556
		data->fan_min[nr] =
L
Linus Torvalds 已提交
557 558 559 560 561
			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]);
562
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
563 564
	return count;
}
565 566
static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
L
Linus Torvalds 已提交
567 568 569
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
570
	int nr = to_sensor_dev_attr(attr)->index;
571
	long val;
L
Linus Torvalds 已提交
572 573 574
	u8 tmp;
	int old_div;
	int new_min;
575 576 577 578 579
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
580 581 582

	tmp = val == 8 ? 0xc0 :
	      val == 4 ? 0x80 :
J
Jean Delvare 已提交
583 584
	      val == 2 ? 0x40 :
	      val == 1 ? 0x00 :
L
Linus Torvalds 已提交
585 586 587
	      0xff;
	if (tmp == 0xff)
		return -EINVAL;
J
Jean Delvare 已提交
588

589
	mutex_lock(&data->update_lock);
590 591 592 593 594 595 596
	/* 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 已提交
597
	old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
J
Jean Delvare 已提交
598 599
	data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
	new_min = data->fan_min[nr] * old_div / val;
L
Linus Torvalds 已提交
600 601
	data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;

J
Jean Delvare 已提交
602
	adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
L
Linus Torvalds 已提交
603
			    data->fan_div[nr]);
J
Jean Delvare 已提交
604
	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
L
Linus Torvalds 已提交
605
			    data->fan_min[nr]);
606 607 608

	/* Invalidate the cache: fan speed is no longer valid */
	data->valid = 0;
609
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
610 611 612 613
	return count;
}

#define fan_offset(offset)						\
614 615 616 617 618 619
static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,			\
		show_fan, NULL, offset - 1);				\
static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\
		show_fan_min, set_fan_min, offset - 1);			\
static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,		\
		show_fan_div, set_fan_div, offset - 1)
L
Linus Torvalds 已提交
620 621 622 623 624 625

fan_offset(1);
fan_offset(2);


/* Temps */
626 627
static ssize_t show_temp(struct device *dev,
			 struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
628
{
629
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
630 631 632 633 634 635 636
	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));
}
637 638 639 640 641 642 643 644
static ssize_t show_temp_offset(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int nr = to_sensor_dev_attr(attr)->index;
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n",
		       TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
}
645 646
static ssize_t show_temp_min(struct device *dev,
			     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
647
{
648
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
649 650 651
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
}
652 653
static ssize_t show_temp_max(struct device *dev,
			     struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
654
{
655
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
656 657 658
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
}
659 660
static ssize_t show_temp_crit(struct device *dev,
			      struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
661
{
662
	int nr = to_sensor_dev_attr(attr)->index;
L
Linus Torvalds 已提交
663 664 665
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
}
666 667 668 669 670 671 672
static ssize_t set_temp_offset(struct device *dev,
			       struct device_attribute *attr, const char *buf,
			       size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	int nr = to_sensor_dev_attr(attr)->index;
673 674 675 676 677 678
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
679

680
	val = clamp_val(val, -15000, 15000);
681 682 683 684 685 686 687
	mutex_lock(&data->update_lock);
	data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
			    data->temp_offset[nr]);
	mutex_unlock(&data->update_lock);
	return count;
}
688 689
static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
L
Linus Torvalds 已提交
690 691 692
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
693
	int nr = to_sensor_dev_attr(attr)->index;
694 695 696 697 698 699
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
700

701
	val = clamp_val(val, -55000, 127000);
702
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
703 704 705
	data->temp_min[nr] = TEMP_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
			    data->temp_min[nr]);
706
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
707 708
	return count;
}
709 710
static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
L
Linus Torvalds 已提交
711 712 713
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
714
	int nr = to_sensor_dev_attr(attr)->index;
715 716 717 718 719 720
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
721

722
	val = clamp_val(val, -55000, 127000);
723
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
724 725 726
	data->temp_max[nr] = TEMP_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
			    data->temp_max[nr]);
727
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
728 729
	return count;
}
730 731
static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t count)
L
Linus Torvalds 已提交
732 733 734
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
735
	int nr = to_sensor_dev_attr(attr)->index;
736 737 738 739 740 741
	long val;
	int ret;

	ret = kstrtol(buf, 10, &val);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
742

743
	val = clamp_val(val, -55000, 127000);
744
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
745 746 747
	data->temp_crit[nr] = TEMP_TO_REG(val);
	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
			    data->temp_crit[nr]);
748
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
749 750 751
	return count;
}

752 753 754
#define temp_reg(offset)						\
static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,		\
		show_temp, NULL, offset - 1);				\
755 756
static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR,	\
		show_temp_offset, set_temp_offset, offset - 1);		\
757 758 759 760 761 762
static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\
		show_temp_min, set_temp_min, offset - 1);		\
static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\
		show_temp_max, set_temp_max, offset - 1);		\
static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,	\
		show_temp_crit, set_temp_crit, offset - 1)
L
Linus Torvalds 已提交
763 764 765 766 767 768

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

/* Alarms */
769 770
static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
			   char *buf)
L
Linus Torvalds 已提交
771 772 773 774 775 776 777
{
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", data->alarm);
}

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

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
static ssize_t show_alarm(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	int bitnr = to_sensor_dev_attr(attr)->index;
	struct adm1031_data *data = adm1031_update_device(dev);
	return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
}

static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
L
Linus Torvalds 已提交
801

802 803
/* Update Interval */
static const unsigned int update_intervals[] = {
804 805 806
	16000, 8000, 4000, 2000, 1000, 500, 250, 125,
};

807 808
static ssize_t show_update_interval(struct device *dev,
				    struct device_attribute *attr, char *buf)
809 810 811 812
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);

813
	return sprintf(buf, "%u\n", data->update_interval);
814 815
}

816 817 818
static ssize_t set_update_interval(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
819 820 821 822 823 824 825
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1031_data *data = i2c_get_clientdata(client);
	unsigned long val;
	int i, err;
	u8 reg;

826
	err = kstrtoul(buf, 10, &val);
827 828 829
	if (err)
		return err;

830 831 832 833 834 835
	/*
	 * Find the nearest update interval from the table.
	 * Use it to determine the matching update rate.
	 */
	for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
		if (val >= update_intervals[i])
836 837
			break;
	}
838
	/* if not found, we point to the last entry (lowest update interval) */
839 840 841 842 843 844 845 846

	/* set the new update rate while preserving other settings */
	reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
	reg &= ~ADM1031_UPDATE_RATE_MASK;
	reg |= i << ADM1031_UPDATE_RATE_SHIFT;
	adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);

	mutex_lock(&data->update_lock);
847
	data->update_interval = update_intervals[i];
848 849 850 851 852
	mutex_unlock(&data->update_lock);

	return count;
}

853 854
static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
		   set_update_interval);
855

856
static struct attribute *adm1031_attributes[] = {
857 858 859
	&sensor_dev_attr_fan1_input.dev_attr.attr,
	&sensor_dev_attr_fan1_div.dev_attr.attr,
	&sensor_dev_attr_fan1_min.dev_attr.attr,
860 861
	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
	&sensor_dev_attr_fan1_fault.dev_attr.attr,
862 863 864
	&sensor_dev_attr_pwm1.dev_attr.attr,
	&sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
	&sensor_dev_attr_temp1_input.dev_attr.attr,
865
	&sensor_dev_attr_temp1_offset.dev_attr.attr,
866
	&sensor_dev_attr_temp1_min.dev_attr.attr,
867
	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
868
	&sensor_dev_attr_temp1_max.dev_attr.attr,
869
	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
870
	&sensor_dev_attr_temp1_crit.dev_attr.attr,
871
	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
872
	&sensor_dev_attr_temp2_input.dev_attr.attr,
873
	&sensor_dev_attr_temp2_offset.dev_attr.attr,
874
	&sensor_dev_attr_temp2_min.dev_attr.attr,
875
	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
876
	&sensor_dev_attr_temp2_max.dev_attr.attr,
877
	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
878
	&sensor_dev_attr_temp2_crit.dev_attr.attr,
879 880
	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
	&sensor_dev_attr_temp2_fault.dev_attr.attr,
881 882 883 884 885 886 887 888 889 890

	&sensor_dev_attr_auto_temp1_off.dev_attr.attr,
	&sensor_dev_attr_auto_temp1_min.dev_attr.attr,
	&sensor_dev_attr_auto_temp1_max.dev_attr.attr,

	&sensor_dev_attr_auto_temp2_off.dev_attr.attr,
	&sensor_dev_attr_auto_temp2_min.dev_attr.attr,
	&sensor_dev_attr_auto_temp2_max.dev_attr.attr,

	&sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
891

892
	&dev_attr_update_interval.attr,
893 894 895 896 897 898 899 900 901 902
	&dev_attr_alarms.attr,

	NULL
};

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

static struct attribute *adm1031_attributes_opt[] = {
903 904 905
	&sensor_dev_attr_fan2_input.dev_attr.attr,
	&sensor_dev_attr_fan2_div.dev_attr.attr,
	&sensor_dev_attr_fan2_min.dev_attr.attr,
906 907
	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
	&sensor_dev_attr_fan2_fault.dev_attr.attr,
908 909 910
	&sensor_dev_attr_pwm2.dev_attr.attr,
	&sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
	&sensor_dev_attr_temp3_input.dev_attr.attr,
911
	&sensor_dev_attr_temp3_offset.dev_attr.attr,
912
	&sensor_dev_attr_temp3_min.dev_attr.attr,
913
	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
914
	&sensor_dev_attr_temp3_max.dev_attr.attr,
915
	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
916
	&sensor_dev_attr_temp3_crit.dev_attr.attr,
917 918
	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
	&sensor_dev_attr_temp3_fault.dev_attr.attr,
919 920 921 922
	&sensor_dev_attr_auto_temp3_off.dev_attr.attr,
	&sensor_dev_attr_auto_temp3_min.dev_attr.attr,
	&sensor_dev_attr_auto_temp3_max.dev_attr.attr,
	&sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
923 924 925 926 927 928 929
	NULL
};

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

930
/* Return 0 if detection is successful, -ENODEV otherwise */
931
static int adm1031_detect(struct i2c_client *client,
932
			  struct i2c_board_info *info)
L
Linus Torvalds 已提交
933
{
934
	struct i2c_adapter *adapter = client->adapter;
J
Jean Delvare 已提交
935 936
	const char *name;
	int id, co;
L
Linus Torvalds 已提交
937 938

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
939
		return -ENODEV;
L
Linus Torvalds 已提交
940

J
Jean Delvare 已提交
941 942
	id = i2c_smbus_read_byte_data(client, 0x3d);
	co = i2c_smbus_read_byte_data(client, 0x3e);
L
Linus Torvalds 已提交
943

J
Jean Delvare 已提交
944 945 946
	if (!((id == 0x31 || id == 0x30) && co == 0x41))
		return -ENODEV;
	name = (id == 0x30) ? "adm1030" : "adm1031";
L
Linus Torvalds 已提交
947

948
	strlcpy(info->type, name, I2C_NAME_SIZE);
L
Linus Torvalds 已提交
949

950 951 952 953 954 955 956 957 958
	return 0;
}

static int adm1031_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct adm1031_data *data;
	int err;

959 960 961 962
	data = devm_kzalloc(&client->dev, sizeof(struct adm1031_data),
			    GFP_KERNEL);
	if (!data)
		return -ENOMEM;
963 964 965

	i2c_set_clientdata(client, data);
	data->chip_type = id->driver_data;
966
	mutex_init(&data->update_lock);
L
Linus Torvalds 已提交
967

968 969 970 971
	if (data->chip_type == adm1030)
		data->chan_select_table = &auto_channel_select_table_adm1030;
	else
		data->chan_select_table = &auto_channel_select_table_adm1031;
L
Linus Torvalds 已提交
972 973

	/* Initialize the ADM1031 chip */
J
Jean Delvare 已提交
974
	adm1031_init_client(client);
L
Linus Torvalds 已提交
975 976

	/* Register sysfs hooks */
977 978
	err = sysfs_create_group(&client->dev.kobj, &adm1031_group);
	if (err)
979
		return err;
L
Linus Torvalds 已提交
980

981
	if (data->chip_type == adm1031) {
982 983
		err = sysfs_create_group(&client->dev.kobj, &adm1031_group_opt);
		if (err)
984 985 986
			goto exit_remove;
	}

J
Jean Delvare 已提交
987
	data->hwmon_dev = hwmon_device_register(&client->dev);
988 989
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
990
		goto exit_remove;
L
Linus Torvalds 已提交
991 992 993 994
	}

	return 0;

995
exit_remove:
J
Jean Delvare 已提交
996 997
	sysfs_remove_group(&client->dev.kobj, &adm1031_group);
	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
L
Linus Torvalds 已提交
998 999 1000
	return err;
}

1001
static int adm1031_remove(struct i2c_client *client)
L
Linus Torvalds 已提交
1002
{
1003 1004
	struct adm1031_data *data = i2c_get_clientdata(client);

1005
	hwmon_device_unregister(data->hwmon_dev);
1006 1007
	sysfs_remove_group(&client->dev.kobj, &adm1031_group);
	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014
	return 0;
}

static void adm1031_init_client(struct i2c_client *client)
{
	unsigned int read_val;
	unsigned int mask;
1015
	int i;
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021
	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 已提交
1022
	}
L
Linus Torvalds 已提交
1023 1024
	/* Initialize the ADM1031 chip (enables fan speed reading ) */
	read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
1025 1026
	if ((read_val | mask) != read_val)
		adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
L
Linus Torvalds 已提交
1027 1028 1029

	read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
	if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
1030 1031
		adm1031_write_value(client, ADM1031_REG_CONF1,
				    read_val | ADM1031_CONF1_MONITOR_ENABLE);
L
Linus Torvalds 已提交
1032 1033
	}

1034 1035 1036 1037
	/* Read the chip's update rate */
	mask = ADM1031_UPDATE_RATE_MASK;
	read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
	i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
1038 1039
	/* Save it as update interval */
	data->update_interval = update_intervals[i];
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045
}

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);
1046
	unsigned long next_update;
L
Linus Torvalds 已提交
1047 1048
	int chan;

1049
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1050

1051 1052
	next_update = data->last_updated
	  + msecs_to_jiffies(data->update_interval);
1053
	if (time_after(jiffies, next_update) || !data->valid) {
L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

		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,
1078
					  "Remote temperature may be wrong.\n");
L
Linus Torvalds 已提交
1079 1080 1081 1082
#endif
			}
			data->temp[chan] = newh;

1083 1084 1085
			data->temp_offset[chan] =
			    adm1031_read_value(client,
					       ADM1031_REG_TEMP_OFFSET(chan));
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
			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))
1105 1106
		    | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
		if (data->chip_type == adm1030)
L
Linus Torvalds 已提交
1107
			data->alarm &= 0xc0ff;
J
Jean Delvare 已提交
1108

1109 1110
		for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
		     chan++) {
L
Linus Torvalds 已提交
1111
			data->fan_div[chan] =
1112 1113
			    adm1031_read_value(client,
					       ADM1031_REG_FAN_DIV(chan));
L
Linus Torvalds 已提交
1114
			data->fan_min[chan] =
1115 1116
			    adm1031_read_value(client,
					       ADM1031_REG_FAN_MIN(chan));
L
Linus Torvalds 已提交
1117
			data->fan[chan] =
1118 1119
			    adm1031_read_value(client,
					       ADM1031_REG_FAN_SPEED(chan));
L
Linus Torvalds 已提交
1120
			data->pwm[chan] =
1121 1122
			  (adm1031_read_value(client,
					ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
L
Linus Torvalds 已提交
1123 1124 1125 1126 1127
		}
		data->last_updated = jiffies;
		data->valid = 1;
	}

1128
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1129 1130 1131 1132

	return data;
}

1133
module_i2c_driver(adm1031_driver);
L
Linus Torvalds 已提交
1134 1135 1136 1137

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