w83781d.c 57.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
/*
    w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
                monitoring
    Copyright (c) 1998 - 2001  Frodo Looijaard <frodol@dds.nl>,
5 6
                               Philip Edelbrock <phil@netroedge.com>,
                               and Mark Studebaker <mdsxyz123@yahoo.com>
7
    Copyright (c) 2007 - 2008  Jean Delvare <khali@linux-fr.org>
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
    Supports following chips:

    Chip	#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
    as99127f	7	3	0	3	0x31	0x12c3	yes	no
    as99127f rev.2 (type_name = as99127f)	0x31	0x5ca3	yes	no
    w83781d	7	3	0	3	0x10-1	0x5ca3	yes	yes
    w83782d	9	3	2-4	3	0x30	0x5ca3	yes	yes
    w83783s	5-6	3	2	1-2	0x40	0x5ca3	yes	no

*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
41
#include <linux/hwmon.h>
42
#include <linux/hwmon-vid.h>
43
#include <linux/hwmon-sysfs.h>
44
#include <linux/sysfs.h>
45
#include <linux/err.h>
46
#include <linux/mutex.h>
47 48 49 50

#ifdef CONFIG_ISA
#include <linux/platform_device.h>
#include <linux/ioport.h>
51
#include <linux/io.h>
52
#endif
Linus Torvalds's avatar
Linus Torvalds committed
53

54
#include "lm75.h"
55

Linus Torvalds's avatar
Linus Torvalds committed
56
/* Addresses to scan */
57 58
static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
						0x2e, 0x2f, I2C_CLIENT_END };
59

60 61 62
enum chips { w83781d, w83782d, w83783s, as99127f };

/* Insmod parameters */
63 64 65
static unsigned short force_subclients[4];
module_param_array(force_subclients, short, NULL, 0);
MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
Linus Torvalds's avatar
Linus Torvalds committed
66 67
		    "{bus, clientaddr, subclientaddr1, subclientaddr2}");

68 69 70 71
static int reset;
module_param(reset, bool, 0);
MODULE_PARM_DESC(reset, "Set to one to reset chip on load");

Linus Torvalds's avatar
Linus Torvalds committed
72 73 74 75 76 77 78 79 80 81 82 83 84
static int init = 1;
module_param(init, bool, 0);
MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");

/* Constants specified below */

/* Length of ISA address segment */
#define W83781D_EXTENT			8

/* Where are the ISA address/data registers relative to the base address */
#define W83781D_ADDR_REG_OFFSET		5
#define W83781D_DATA_REG_OFFSET		6

85 86
/* The device registers */
/* in nr from 0 to 8 */
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90 91 92 93
#define W83781D_REG_IN_MAX(nr)		((nr < 7) ? (0x2b + (nr) * 2) : \
						    (0x554 + (((nr) - 7) * 2)))
#define W83781D_REG_IN_MIN(nr)		((nr < 7) ? (0x2c + (nr) * 2) : \
						    (0x555 + (((nr) - 7) * 2)))
#define W83781D_REG_IN(nr)		((nr < 7) ? (0x20 + (nr)) : \
						    (0x550 + (nr) - 7))

94 95 96
/* fan nr from 0 to 2 */
#define W83781D_REG_FAN_MIN(nr)		(0x3b + (nr))
#define W83781D_REG_FAN(nr)		(0x28 + (nr))
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100

#define W83781D_REG_BANK		0x4E
#define W83781D_REG_TEMP2_CONFIG	0x152
#define W83781D_REG_TEMP3_CONFIG	0x252
101
/* temp nr from 1 to 3 */
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106 107 108 109 110 111 112
#define W83781D_REG_TEMP(nr)		((nr == 3) ? (0x0250) : \
					((nr == 2) ? (0x0150) : \
						     (0x27)))
#define W83781D_REG_TEMP_HYST(nr)	((nr == 3) ? (0x253) : \
					((nr == 2) ? (0x153) : \
						     (0x3A)))
#define W83781D_REG_TEMP_OVER(nr)	((nr == 3) ? (0x255) : \
					((nr == 2) ? (0x155) : \
						     (0x39)))

#define W83781D_REG_CONFIG		0x40
113 114

/* Interrupt status (W83781D, AS99127F) */
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117
#define W83781D_REG_ALARM1		0x41
#define W83781D_REG_ALARM2		0x42

118
/* Real-time status (W83782D, W83783S) */
119 120 121 122
#define W83782D_REG_ALARM1		0x459
#define W83782D_REG_ALARM2		0x45A
#define W83782D_REG_ALARM3		0x45B

Linus Torvalds's avatar
Linus Torvalds committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#define W83781D_REG_BEEP_CONFIG		0x4D
#define W83781D_REG_BEEP_INTS1		0x56
#define W83781D_REG_BEEP_INTS2		0x57
#define W83781D_REG_BEEP_INTS3		0x453	/* not on W83781D */

#define W83781D_REG_VID_FANDIV		0x47

#define W83781D_REG_CHIPID		0x49
#define W83781D_REG_WCHIPID		0x58
#define W83781D_REG_CHIPMAN		0x4F
#define W83781D_REG_PIN			0x4B

/* 782D/783S only */
#define W83781D_REG_VBAT		0x5D

/* PWM 782D (1-4) and 783S (1-2) only */
139
static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#define W83781D_REG_PWMCLK12		0x5C
#define W83781D_REG_PWMCLK34		0x45C

#define W83781D_REG_I2C_ADDR		0x48
#define W83781D_REG_I2C_SUBADDR		0x4A

/* The following are undocumented in the data sheets however we
   received the information in an email from Winbond tech support */
/* Sensor selection - not on 781d */
#define W83781D_REG_SCFG1		0x5D
static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };

#define W83781D_REG_SCFG2		0x59
static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };

#define W83781D_DEFAULT_BETA		3435

157 158 159
/* Conversions */
#define IN_TO_REG(val)			SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
#define IN_FROM_REG(val)		((val) * 16)
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164 165 166 167 168 169

static inline u8
FAN_TO_REG(long rpm, int div)
{
	if (rpm == 0)
		return 255;
	rpm = SENSORS_LIMIT(rpm, 1, 1000000);
	return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
}

170 171 172 173 174 175 176 177 178
static inline long
FAN_FROM_REG(u8 val, int div)
{
	if (val == 0)
		return -1;
	if (val == 255)
		return 0;
	return 1350000 / (val * div);
}
Linus Torvalds's avatar
Linus Torvalds committed
179

180 181
#define TEMP_TO_REG(val)		SENSORS_LIMIT((val) / 1000, -127, 128)
#define TEMP_FROM_REG(val)		((val) * 1000)
Linus Torvalds's avatar
Linus Torvalds committed
182 183

#define BEEP_MASK_FROM_REG(val,type)	((type) == as99127f ? \
184
					 (~(val)) & 0x7fff : (val) & 0xff7fff)
Linus Torvalds's avatar
Linus Torvalds committed
185
#define BEEP_MASK_TO_REG(val,type)	((type) == as99127f ? \
186
					 (~(val)) & 0x7fff : (val) & 0xff7fff)
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190 191 192 193 194 195 196

#define DIV_FROM_REG(val)		(1 << (val))

static inline u8
DIV_TO_REG(long val, enum chips type)
{
	int i;
	val = SENSORS_LIMIT(val, 1,
			    ((type == w83781d
			      || type == as99127f) ? 8 : 128)) >> 1;
197
	for (i = 0; i < 7; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201
		if (val == 0)
			break;
		val >>= 1;
	}
202
	return i;
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205
}

struct w83781d_data {
206
	struct i2c_client *client;
207
	struct device *hwmon_dev;
208
	struct mutex lock;
Linus Torvalds's avatar
Linus Torvalds committed
209 210
	enum chips type;

211 212 213 214
	/* For ISA device only */
	const char *name;
	int isa_addr;

215
	struct mutex update_lock;
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219 220 221 222 223 224 225 226
	char valid;		/* !=0 if following fields are valid */
	unsigned long last_updated;	/* In jiffies */

	struct i2c_client *lm75[2];	/* for secondary I2C addresses */
	/* array of 2 pointers to subclients */

	u8 in[9];		/* Register value - 8 & 9 for 782D only */
	u8 in_max[9];		/* Register value - 8 & 9 for 782D only */
	u8 in_min[9];		/* Register value - 8 & 9 for 782D only */
	u8 fan[3];		/* Register value */
	u8 fan_min[3];		/* Register value */
227 228 229
	s8 temp;		/* Register value */
	s8 temp_max;		/* Register value */
	s8 temp_max_hyst;	/* Register value */
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232 233 234 235 236 237
	u16 temp_add[2];	/* Register value */
	u16 temp_max_add[2];	/* Register value */
	u16 temp_max_hyst_add[2];	/* Register value */
	u8 fan_div[3];		/* Register encoding, shifted right */
	u8 vid;			/* Register encoding, combined */
	u32 alarms;		/* Register encoding, combined */
	u32 beep_mask;		/* Register encoding, combined */
	u8 pwm[4];		/* Register value */
238
	u8 pwm2_enable;		/* Boolean */
Linus Torvalds's avatar
Linus Torvalds committed
239 240
	u16 sens[3];		/* 782D/783S only.
				   1 = pentium diode; 2 = 3904 diode;
241
				   4 = thermistor */
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244
	u8 vrm;
};

245 246 247
static struct w83781d_data *w83781d_data_if_isa(void);
static int w83781d_alias_detect(struct i2c_client *client, u8 chipid);

248 249
static int w83781d_read_value(struct w83781d_data *data, u16 reg);
static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
Linus Torvalds's avatar
Linus Torvalds committed
250
static struct w83781d_data *w83781d_update_device(struct device *dev);
251
static void w83781d_init_device(struct device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254

/* following are the sysfs callback functions */
#define show_in_reg(reg) \
255 256
static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
		char *buf) \
Linus Torvalds's avatar
Linus Torvalds committed
257
{ \
258
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds's avatar
Linus Torvalds committed
259
	struct w83781d_data *data = w83781d_update_device(dev); \
260 261
	return sprintf(buf, "%ld\n", \
		       (long)IN_FROM_REG(data->reg[attr->index])); \
Linus Torvalds's avatar
Linus Torvalds committed
262 263 264 265 266 267
}
show_in_reg(in);
show_in_reg(in_min);
show_in_reg(in_max);

#define store_in_reg(REG, reg) \
268 269
static ssize_t store_in_##reg (struct device *dev, struct device_attribute \
		*da, const char *buf, size_t count) \
Linus Torvalds's avatar
Linus Torvalds committed
270
{ \
271
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
272
	struct w83781d_data *data = dev_get_drvdata(dev); \
273
	int nr = attr->index; \
Linus Torvalds's avatar
Linus Torvalds committed
274 275
	u32 val; \
	 \
276
	val = simple_strtoul(buf, NULL, 10); \
Linus Torvalds's avatar
Linus Torvalds committed
277
	 \
278
	mutex_lock(&data->update_lock); \
Linus Torvalds's avatar
Linus Torvalds committed
279
	data->in_##reg[nr] = IN_TO_REG(val); \
280
	w83781d_write_value(data, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
Linus Torvalds's avatar
Linus Torvalds committed
281
	 \
282
	mutex_unlock(&data->update_lock); \
Linus Torvalds's avatar
Linus Torvalds committed
283 284 285 286 287 288
	return count; \
}
store_in_reg(MIN, min);
store_in_reg(MAX, max);

#define sysfs_in_offsets(offset) \
289 290 291 292 293 294
static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
		show_in, NULL, offset); \
static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
		show_in_min, store_in_min, offset); \
static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
		show_in_max, store_in_max, offset)
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298 299 300 301 302 303 304 305 306

sysfs_in_offsets(0);
sysfs_in_offsets(1);
sysfs_in_offsets(2);
sysfs_in_offsets(3);
sysfs_in_offsets(4);
sysfs_in_offsets(5);
sysfs_in_offsets(6);
sysfs_in_offsets(7);
sysfs_in_offsets(8);

#define show_fan_reg(reg) \
307 308
static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
		char *buf) \
Linus Torvalds's avatar
Linus Torvalds committed
309
{ \
310
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds's avatar
Linus Torvalds committed
311 312
	struct w83781d_data *data = w83781d_update_device(dev); \
	return sprintf(buf,"%ld\n", \
313 314
		FAN_FROM_REG(data->reg[attr->index], \
			DIV_FROM_REG(data->fan_div[attr->index]))); \
Linus Torvalds's avatar
Linus Torvalds committed
315 316 317 318 319
}
show_fan_reg(fan);
show_fan_reg(fan_min);

static ssize_t
320 321
store_fan_min(struct device *dev, struct device_attribute *da,
		const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
322
{
323
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
324
	struct w83781d_data *data = dev_get_drvdata(dev);
325
	int nr = attr->index;
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328 329
	u32 val;

	val = simple_strtoul(buf, NULL, 10);

330
	mutex_lock(&data->update_lock);
331 332
	data->fan_min[nr] =
	    FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
333
	w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
334
			    data->fan_min[nr]);
Linus Torvalds's avatar
Linus Torvalds committed
335

336
	mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
337 338 339
	return count;
}

340 341 342 343 344 345 346 347 348
static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
		show_fan_min, store_fan_min, 0);
static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
		show_fan_min, store_fan_min, 1);
static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
		show_fan_min, store_fan_min, 2);
Linus Torvalds's avatar
Linus Torvalds committed
349 350

#define show_temp_reg(reg) \
351 352
static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
		char *buf) \
Linus Torvalds's avatar
Linus Torvalds committed
353
{ \
354
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds's avatar
Linus Torvalds committed
355
	struct w83781d_data *data = w83781d_update_device(dev); \
356
	int nr = attr->index; \
Linus Torvalds's avatar
Linus Torvalds committed
357 358 359 360 361 362 363 364 365 366 367 368
	if (nr >= 2) {	/* TEMP2 and TEMP3 */ \
		return sprintf(buf,"%d\n", \
			LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
	} else {	/* TEMP1 */ \
		return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
	} \
}
show_temp_reg(temp);
show_temp_reg(temp_max);
show_temp_reg(temp_max_hyst);

#define store_temp_reg(REG, reg) \
369 370
static ssize_t store_temp_##reg (struct device *dev, \
		struct device_attribute *da, const char *buf, size_t count) \
Linus Torvalds's avatar
Linus Torvalds committed
371
{ \
372
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
373
	struct w83781d_data *data = dev_get_drvdata(dev); \
374
	int nr = attr->index; \
375
	long val; \
Linus Torvalds's avatar
Linus Torvalds committed
376 377 378
	 \
	val = simple_strtol(buf, NULL, 10); \
	 \
379
	mutex_lock(&data->update_lock); \
Linus Torvalds's avatar
Linus Torvalds committed
380 381 382
	 \
	if (nr >= 2) {	/* TEMP2 and TEMP3 */ \
		data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
383
		w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds's avatar
Linus Torvalds committed
384 385 386
				data->temp_##reg##_add[nr-2]); \
	} else {	/* TEMP1 */ \
		data->temp_##reg = TEMP_TO_REG(val); \
387
		w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds's avatar
Linus Torvalds committed
388 389 390
			data->temp_##reg); \
	} \
	 \
391
	mutex_unlock(&data->update_lock); \
Linus Torvalds's avatar
Linus Torvalds committed
392 393 394 395 396 397
	return count; \
}
store_temp_reg(OVER, max);
store_temp_reg(HYST, max_hyst);

#define sysfs_temp_offsets(offset) \
398 399 400 401 402 403
static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
		show_temp, NULL, offset); \
static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
		show_temp_max, store_temp_max, offset); \
static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
		show_temp_max_hyst, store_temp_max_hyst, offset);
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408 409

sysfs_temp_offsets(1);
sysfs_temp_offsets(2);
sysfs_temp_offsets(3);

static ssize_t
410
show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414 415
{
	struct w83781d_data *data = w83781d_update_device(dev);
	return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
}

416 417
static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);

Linus Torvalds's avatar
Linus Torvalds committed
418
static ssize_t
419
show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
420
{
421
	struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425
	return sprintf(buf, "%ld\n", (long) data->vrm);
}

static ssize_t
426
store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
427
{
428
	struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431 432 433 434 435 436
	u32 val;

	val = simple_strtoul(buf, NULL, 10);
	data->vrm = val;

	return count;
}

437 438
static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);

Linus Torvalds's avatar
Linus Torvalds committed
439
static ssize_t
440
show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
441 442
{
	struct w83781d_data *data = w83781d_update_device(dev);
443
	return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds's avatar
Linus Torvalds committed
444 445
}

446 447
static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);

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 475 476 477 478 479 480
static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	struct w83781d_data *data = w83781d_update_device(dev);
	int bitnr = to_sensor_dev_attr(attr)->index;
	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
}

/* The W83781D has a single alarm bit for temp2 and temp3 */
static ssize_t show_temp3_alarm(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct w83781d_data *data = w83781d_update_device(dev);
	int bitnr = (data->type == w83781d) ? 5 : 13;
	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(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0);

481
static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
482 483 484 485 486 487 488
{
	struct w83781d_data *data = w83781d_update_device(dev);
	return sprintf(buf, "%ld\n",
		       (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
}

static ssize_t
489 490
store_beep_mask(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
491
{
492
	struct w83781d_data *data = dev_get_drvdata(dev);
493
	u32 val;
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496

	val = simple_strtoul(buf, NULL, 10);

497
	mutex_lock(&data->update_lock);
498 499
	data->beep_mask &= 0x8000; /* preserve beep enable */
	data->beep_mask |= BEEP_MASK_TO_REG(val, data->type);
500 501 502
	w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
			    data->beep_mask & 0xff);
	w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
503
			    (data->beep_mask >> 8) & 0xff);
504 505 506 507 508
	if (data->type != w83781d && data->type != as99127f) {
		w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
				    ((data->beep_mask) >> 16) & 0xff);
	}
	mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
509

510 511
	return count;
}
Linus Torvalds's avatar
Linus Torvalds committed
512

513 514
static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
		show_beep_mask, store_beep_mask);
Linus Torvalds's avatar
Linus Torvalds committed
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	struct w83781d_data *data = w83781d_update_device(dev);
	int bitnr = to_sensor_dev_attr(attr)->index;
	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
}

static ssize_t
store_beep(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
{
	struct w83781d_data *data = dev_get_drvdata(dev);
	int bitnr = to_sensor_dev_attr(attr)->index;
	unsigned long bit;
	u8 reg;

	bit = simple_strtoul(buf, NULL, 10);
	if (bit & ~1)
		return -EINVAL;

	mutex_lock(&data->update_lock);
	if (bit)
		data->beep_mask |= (1 << bitnr);
	else
		data->beep_mask &= ~(1 << bitnr);

	if (bitnr < 8) {
		reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
		if (bit)
			reg |= (1 << bitnr);
		else
			reg &= ~(1 << bitnr);
		w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg);
	} else if (bitnr < 16) {
		reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
		if (bit)
			reg |= (1 << (bitnr - 8));
		else
			reg &= ~(1 << (bitnr - 8));
		w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg);
	} else {
		reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3);
		if (bit)
			reg |= (1 << (bitnr - 16));
		else
			reg &= ~(1 << (bitnr - 16));
		w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg);
	}
	mutex_unlock(&data->update_lock);

	return count;
}

/* The W83781D has a single beep bit for temp2 and temp3 */
static ssize_t show_temp3_beep(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct w83781d_data *data = w83781d_update_device(dev);
	int bitnr = (data->type == w83781d) ? 5 : 13;
	return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
}

static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 0);
static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 1);
static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 2);
static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 3);
static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 8);
static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 9);
static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 10);
static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 16);
static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 17);
static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 6);
static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 7);
static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 11);
static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 4);
static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 5);
static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO,
			show_temp3_beep, store_beep, 13);
609 610
static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
			show_beep, store_beep, 15);
611

Linus Torvalds's avatar
Linus Torvalds committed
612
static ssize_t
613
show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
614
{
615
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds's avatar
Linus Torvalds committed
616 617
	struct w83781d_data *data = w83781d_update_device(dev);
	return sprintf(buf, "%ld\n",
618
		       (long) DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds's avatar
Linus Torvalds committed
619 620 621 622
}

/* Note: we save and restore the fan minimum here, because its value is
   determined in part by the fan divisor.  This follows the principle of
Andreas Mohr's avatar
Andreas Mohr committed
623
   least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds's avatar
Linus Torvalds committed
624 625
   because the divisor changed. */
static ssize_t
626 627
store_fan_div(struct device *dev, struct device_attribute *da,
		const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
628
{
629
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
630
	struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
631
	unsigned long min;
632
	int nr = attr->index;
Linus Torvalds's avatar
Linus Torvalds committed
633 634 635
	u8 reg;
	unsigned long val = simple_strtoul(buf, NULL, 10);

636
	mutex_lock(&data->update_lock);
Jean Delvare's avatar
Jean Delvare committed
637

Linus Torvalds's avatar
Linus Torvalds committed
638 639 640 641 642 643
	/* Save fan_min */
	min = FAN_FROM_REG(data->fan_min[nr],
			   DIV_FROM_REG(data->fan_div[nr]));

	data->fan_div[nr] = DIV_TO_REG(val, data->type);

644
	reg = (w83781d_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
Linus Torvalds's avatar
Linus Torvalds committed
645 646
	       & (nr==0 ? 0xcf : 0x3f))
	    | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
647
	w83781d_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds's avatar
Linus Torvalds committed
648 649 650

	/* w83781d and as99127f don't have extended divisor bits */
	if (data->type != w83781d && data->type != as99127f) {
651
		reg = (w83781d_read_value(data, W83781D_REG_VBAT)
Linus Torvalds's avatar
Linus Torvalds committed
652 653
		       & ~(1 << (5 + nr)))
		    | ((data->fan_div[nr] & 0x04) << (3 + nr));
654
		w83781d_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds's avatar
Linus Torvalds committed
655 656 657 658
	}

	/* Restore fan_min */
	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
659
	w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds's avatar
Linus Torvalds committed
660

661
	mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
662 663 664
	return count;
}

665 666 667 668 669 670
static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
		show_fan_div, store_fan_div, 0);
static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
		show_fan_div, store_fan_div, 1);
static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
		show_fan_div, store_fan_div, 2);
Linus Torvalds's avatar
Linus Torvalds committed
671 672

static ssize_t
673
show_pwm(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
674
{
675
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds's avatar
Linus Torvalds committed
676
	struct w83781d_data *data = w83781d_update_device(dev);
677
	return sprintf(buf, "%d\n", (int)data->pwm[attr->index]);
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680
}

static ssize_t
681
show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
682 683
{
	struct w83781d_data *data = w83781d_update_device(dev);
684
	return sprintf(buf, "%d\n", (int)data->pwm2_enable);
Linus Torvalds's avatar
Linus Torvalds committed
685 686 687
}

static ssize_t
688 689
store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
		size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
690
{
691
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
692
	struct w83781d_data *data = dev_get_drvdata(dev);
693
	int nr = attr->index;
Linus Torvalds's avatar
Linus Torvalds committed
694 695 696 697
	u32 val;

	val = simple_strtoul(buf, NULL, 10);

698
	mutex_lock(&data->update_lock);
699 700
	data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
	w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
701
	mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705
	return count;
}

static ssize_t
706 707
store_pwm2_enable(struct device *dev, struct device_attribute *da,
		const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
708
{
709
	struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712 713
	u32 val, reg;

	val = simple_strtoul(buf, NULL, 10);

714
	mutex_lock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
715 716 717 718

	switch (val) {
	case 0:
	case 1:
719 720
		reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
		w83781d_write_value(data, W83781D_REG_PWMCLK12,
Linus Torvalds's avatar
Linus Torvalds committed
721 722
				    (reg & 0xf7) | (val << 3));

723 724
		reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
		w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
Linus Torvalds's avatar
Linus Torvalds committed
725 726
				    (reg & 0xef) | (!val << 4));

727
		data->pwm2_enable = val;
Linus Torvalds's avatar
Linus Torvalds committed
728 729 730
		break;

	default:
731
		mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
732 733 734
		return -EINVAL;
	}

735
	mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
736 737 738
	return count;
}

739 740 741 742 743 744 745
static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
/* only PWM2 can be enabled/disabled */
static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
		show_pwm2_enable, store_pwm2_enable);
Linus Torvalds's avatar
Linus Torvalds committed
746 747

static ssize_t
748
show_sensor(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
749
{
750
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds's avatar
Linus Torvalds committed
751
	struct w83781d_data *data = w83781d_update_device(dev);
752
	return sprintf(buf, "%d\n", (int)data->sens[attr->index]);
Linus Torvalds's avatar
Linus Torvalds committed
753 754 755
}

static ssize_t
756 757
store_sensor(struct device *dev, struct device_attribute *da,
		const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
758
{
759
	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
760
	struct w83781d_data *data = dev_get_drvdata(dev);
761
	int nr = attr->index;
Linus Torvalds's avatar
Linus Torvalds committed
762 763 764 765
	u32 val, tmp;

	val = simple_strtoul(buf, NULL, 10);

766
	mutex_lock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769

	switch (val) {
	case 1:		/* PII/Celeron diode */
770 771
		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
		w83781d_write_value(data, W83781D_REG_SCFG1,
772
				    tmp | BIT_SCFG1[nr]);
773 774
		tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
		w83781d_write_value(data, W83781D_REG_SCFG2,
775 776
				    tmp | BIT_SCFG2[nr]);
		data->sens[nr] = val;
Linus Torvalds's avatar
Linus Torvalds committed
777 778
		break;
	case 2:		/* 3904 */
779 780
		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
		w83781d_write_value(data, W83781D_REG_SCFG1,
781
				    tmp | BIT_SCFG1[nr]);
782 783
		tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
		w83781d_write_value(data, W83781D_REG_SCFG2,
784 785
				    tmp & ~BIT_SCFG2[nr]);
		data->sens[nr] = val;
Linus Torvalds's avatar
Linus Torvalds committed
786
		break;
787 788 789 790 791
	case W83781D_DEFAULT_BETA:
		dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
			 "instead\n", W83781D_DEFAULT_BETA);
		/* fall through */
	case 4:		/* thermistor */
792 793
		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
		w83781d_write_value(data, W83781D_REG_SCFG1,
794 795
				    tmp & ~BIT_SCFG1[nr]);
		data->sens[nr] = val;
Linus Torvalds's avatar
Linus Torvalds committed
796 797
		break;
	default:
798 799
		dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n",
		       (long) val);
Linus Torvalds's avatar
Linus Torvalds committed
800 801 802
		break;
	}

803
	mutex_unlock(&data->update_lock);
Linus Torvalds's avatar
Linus Torvalds committed
804 805 806
	return count;
}

807 808 809
static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
	show_sensor, store_sensor, 0);
static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
810
	show_sensor, store_sensor, 1);
811
static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
812
	show_sensor, store_sensor, 2);
Linus Torvalds's avatar
Linus Torvalds committed
813 814 815 816 817

/* Assumes that adapter is of I2C, not ISA variety.
 * OTHERWISE DON'T CALL THIS
 */
static int
818
w83781d_detect_subclients(struct i2c_client *new_client)
Linus Torvalds's avatar
Linus Torvalds committed
819 820 821
{
	int i, val1 = 0, id;
	int err;
822 823 824
	int address = new_client->addr;
	unsigned short sc_addr[2];
	struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds's avatar
Linus Torvalds committed
825
	struct w83781d_data *data = i2c_get_clientdata(new_client);
826
	enum chips kind = data->type;
Linus Torvalds's avatar
Linus Torvalds committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840

	id = i2c_adapter_id(adapter);

	if (force_subclients[0] == id && force_subclients[1] == address) {
		for (i = 2; i <= 3; i++) {
			if (force_subclients[i] < 0x48 ||
			    force_subclients[i] > 0x4f) {
				dev_err(&new_client->dev, "Invalid subclient "
					"address %d; must be 0x48-0x4f\n",
					force_subclients[i]);
				err = -EINVAL;
				goto ERROR_SC_1;
			}
		}
841
		w83781d_write_value(data, W83781D_REG_I2C_SUBADDR,
Linus Torvalds's avatar
Linus Torvalds committed
842 843
				(force_subclients[2] & 0x07) |
				((force_subclients[3] & 0x07) << 4));
844
		sc_addr[0] = force_subclients[2];
Linus Torvalds's avatar
Linus Torvalds committed
845
	} else {
846
		val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR);
847
		sc_addr[0] = 0x48 + (val1 & 0x07);
Linus Torvalds's avatar
Linus Torvalds committed
848 849 850 851 852
	}

	if (kind != w83783s) {
		if (force_subclients[0] == id &&
		    force_subclients[1] == address) {
853
			sc_addr[1] = force_subclients[3];
Linus Torvalds's avatar
Linus Torvalds committed
854
		} else {
855
			sc_addr[1] = 0x48 + ((val1 >> 4) & 0x07);
Linus Torvalds's avatar
Linus Torvalds committed
856
		}
857
		if (sc_addr[0] == sc_addr[1]) {
Linus Torvalds's avatar
Linus Torvalds committed
858 859
			dev_err(&new_client->dev,
			       "Duplicate addresses 0x%x for subclients.\n",
860
			       sc_addr[0]);
Linus Torvalds's avatar
Linus Torvalds committed
861 862 863 864 865 866
			err = -EBUSY;
			goto ERROR_SC_2;
		}
	}

	for (i = 0; i <= 1; i++) {
867 868
		data->lm75[i] = i2c_new_dummy(adapter, sc_addr[i]);
		if (!data->lm75[i]) {
Linus Torvalds's avatar
Linus Torvalds committed
869 870
			dev_err(&new_client->dev, "Subclient %d "
				"registration at address 0x%x "
871 872
				"failed.\n", i, sc_addr[i]);
			err = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
873 874 875 876 877 878 879 880 881 882 883 884
			if (i == 1)
				goto ERROR_SC_3;
			goto ERROR_SC_2;
		}
		if (kind == w83783s)
			break;
	}

	return 0;

/* Undo inits in case of errors */
ERROR_SC_3:
885
	i2c_unregister_device(data->lm75[0]);
Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889 890
ERROR_SC_2:
ERROR_SC_1:
	return err;
}

891 892 893
#define IN_UNIT_ATTRS(X)					\
	&sensor_dev_attr_in##X##_input.dev_attr.attr,		\
	&sensor_dev_attr_in##X##_min.dev_attr.attr,		\
Jean Delvare's avatar
Jean Delvare committed
894
	&sensor_dev_attr_in##X##_max.dev_attr.attr,		\
895 896
	&sensor_dev_attr_in##X##_alarm.dev_attr.attr,		\
	&sensor_dev_attr_in##X##_beep.dev_attr.attr
897

898 899 900
#define FAN_UNIT_ATTRS(X)					\
	&sensor_dev_attr_fan##X##_input.dev_attr.attr,		\
	&sensor_dev_attr_fan##X##_min.dev_attr.attr,		\
901 902 903
	&sensor_dev_attr_fan##X##_div.dev_attr.attr,		\
	&sensor_dev_attr_fan##X##_alarm.dev_attr.attr,		\
	&sensor_dev_attr_fan##X##_beep.dev_attr.attr
904

905 906 907
#define TEMP_UNIT_ATTRS(X)					\
	&sensor_dev_attr_temp##X##_input.dev_attr.attr,		\
	&sensor_dev_attr_temp##X##_max.dev_attr.attr,		\
908 909 910
	&sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr,	\
	&sensor_dev_attr_temp##X##_alarm.dev_attr.attr,		\
	&sensor_dev_attr_temp##X##_beep.dev_attr.attr
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

static struct attribute* w83781d_attributes[] = {
	IN_UNIT_ATTRS(0),
	IN_UNIT_ATTRS(2),
	IN_UNIT_ATTRS(3),
	IN_UNIT_ATTRS(4),
	IN_UNIT_ATTRS(5),
	IN_UNIT_ATTRS(6),
	FAN_UNIT_ATTRS(1),
	FAN_UNIT_ATTRS(2),
	FAN_UNIT_ATTRS(3),
	TEMP_UNIT_ATTRS(1),
	TEMP_UNIT_ATTRS(2),
	&dev_attr_cpu0_vid.attr,
	&dev_attr_vrm.attr,
	&dev_attr_alarms.attr,
	&dev_attr_beep_mask.attr,
928
	&sensor_dev_attr_beep_enable.dev_attr.attr,
929 930 931 932 933 934 935 936 937 938 939
	NULL
};
static const struct attribute_group w83781d_group = {
	.attrs = w83781d_attributes,
};

static struct attribute *w83781d_attributes_opt[] = {
	IN_UNIT_ATTRS(1),
	IN_UNIT_ATTRS(7),
	IN_UNIT_ATTRS(8),
	TEMP_UNIT_ATTRS(3),
940 941 942 943
	&sensor_dev_attr_pwm1.dev_attr.attr,
	&sensor_dev_attr_pwm2.dev_attr.attr,
	&sensor_dev_attr_pwm3.dev_attr.attr,
	&sensor_dev_attr_pwm4.dev_attr.attr,
944
	&dev_attr_pwm2_enable.attr,
945 946 947
	&sensor_dev_attr_temp1_type.dev_attr.attr,
	&sensor_dev_attr_temp2_type.dev_attr.attr,
	&sensor_dev_attr_temp3_type.dev_attr.attr,
948 949 950 951 952 953
	NULL
};
static const struct attribute_group w83781d_group_opt = {
	.attrs = w83781d_attributes_opt,
};

954
/* No clean up is done on error, it's up to the caller */
Linus Torvalds's avatar
Linus Torvalds committed
955
static int
956
w83781d_create_files(struct device *dev, int kind, int is_isa)
Linus Torvalds's avatar
Linus Torvalds committed
957 958 959
{
	int err;

960 961 962 963
	if ((err = sysfs_create_group(&dev->kobj, &w83781d_group)))
		return err;

	if (kind != w83783s) {
964 965 966 967 968
		if ((err = device_create_file(dev,
				&sensor_dev_attr_in1_input.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in1_min.dev_attr))
		    || (err = device_create_file(dev,
969 970 971 972 973
				&sensor_dev_attr_in1_max.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in1_alarm.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in1_beep.dev_attr)))
974 975 976
			return err;
	}
	if (kind != as99127f && kind != w83781d && kind != w83783s) {
977 978 979 980 981 982
		if ((err = device_create_file(dev,
				&sensor_dev_attr_in7_input.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in7_min.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in7_max.dev_attr))
983 984 985 986
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in7_alarm.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in7_beep.dev_attr))
987 988 989 990 991
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in8_input.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in8_min.dev_attr))
		    || (err = device_create_file(dev,
992 993 994 995 996
				&sensor_dev_attr_in8_max.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in8_alarm.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_in8_beep.dev_attr)))
997 998 999
			return err;
	}
	if (kind != w83783s) {
1000 1001 1002 1003
		if ((err = device_create_file(dev,
				&sensor_dev_attr_temp3_input.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_temp3_max.dev_attr))
1004
		    || (err = device_create_file(dev,
1005 1006 1007 1008 1009
				&sensor_dev_attr_temp3_max_hyst.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_temp3_alarm.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_temp3_beep.dev_attr)))
1010
			return err;
1011

1012
		if (kind != w83781d) {
1013 1014 1015 1016 1017
			err = sysfs_chmod_file(&dev->kobj,
				&sensor_dev_attr_temp3_alarm.dev_attr.attr,
				S_IRUGO | S_IWUSR);
			if (err)
				return err;
1018
		}
Linus Torvalds's avatar
Linus Torvalds committed
1019 1020
	}

1021
	if (kind != w83781d && kind != as99127f) {
1022 1023 1024 1025
		if ((err = device_create_file(dev,
				&sensor_dev_attr_pwm1.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_pwm2.dev_attr))
1026 1027
		    || (err = device_create_file(dev, &dev_attr_pwm2_enable)))
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1028
	}
1029
	if (kind == w83782d && !is_isa) {
1030 1031 1032 1033
		if ((err = device_create_file(dev,
				&sensor_dev_attr_pwm3.dev_attr))
		    || (err = device_create_file(dev,
				&sensor_dev_attr_pwm4.dev_attr)))
1034 1035 1036 1037
			return err;
	}

	if (kind != as99127f && kind != w83781d) {
1038 1039
		if ((err = device_create_file(dev,
				&sensor_dev_attr_temp1_type.dev_attr))
1040
		    || (err = device_create_file(dev,
1041
				&sensor_dev_attr_temp2_type.dev_attr)))
1042 1043 1044
			return err;
		if (kind != w83783s) {
			if ((err = device_create_file(dev,
1045
					&sensor_dev_attr_temp3_type.dev_attr)))
1046
				return err;
Linus Torvalds's avatar
Linus Torvalds committed
1047
		}
1048
	}
Linus Torvalds's avatar
Linus Torvalds committed
1049

1050 1051
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1052

1053
/* Return 0 if detection is successful, -ENODEV otherwise */
1054
static int
1055
w83781d_detect(struct i2c_client *client, struct i2c_board_info *info)