adm1026.c 59.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
    adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
	     monitoring
    Copyright (C) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
    Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>

    Chip details at:

    <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>

    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>
31
#include <linux/hwmon.h>
32 33
#include <linux/hwmon-sysfs.h>
#include <linux/hwmon-vid.h>
34
#include <linux/err.h>
35
#include <linux/mutex.h>
L
Linus Torvalds 已提交
36 37

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

/* Insmod parameters */
41
I2C_CLIENT_INSMOD_1(adm1026);
L
Linus Torvalds 已提交
42

43 44
static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
				-1, -1, -1, -1, -1, -1, -1, -1 };
L
Linus Torvalds 已提交
45 46 47 48 49 50 51
static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
				-1, -1, -1, -1, -1, -1, -1, -1 };
static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
				-1, -1, -1, -1, -1, -1, -1, -1 };
static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
				-1, -1, -1, -1, -1, -1, -1, -1 };
static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
52 53 54 55
module_param_array(gpio_input, int, NULL, 0);
MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
module_param_array(gpio_output, int, NULL, 0);
MODULE_PARM_DESC(gpio_output, "List of GPIO pins (0-16) to program as "
L
Linus Torvalds 已提交
56
	"outputs");
57 58
module_param_array(gpio_inverted, int, NULL, 0);
MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
L
Linus Torvalds 已提交
59
	"inverted");
60 61
module_param_array(gpio_normal, int, NULL, 0);
MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
L
Linus Torvalds 已提交
62
	"normal/non-inverted");
63 64
module_param_array(gpio_fan, int, NULL, 0);
MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
L
Linus Torvalds 已提交
65 66 67 68

/* Many ADM1026 constants specified below */

/* The ADM1026 registers */
69 70 71 72 73 74 75 76 77 78 79
#define ADM1026_REG_CONFIG1	0x00
#define CFG1_MONITOR		0x01
#define CFG1_INT_ENABLE		0x02
#define CFG1_INT_CLEAR		0x04
#define CFG1_AIN8_9		0x08
#define CFG1_THERM_HOT		0x10
#define CFG1_DAC_AFC		0x20
#define CFG1_PWM_AFC		0x40
#define CFG1_RESET		0x80

#define ADM1026_REG_CONFIG2	0x01
L
Linus Torvalds 已提交
80
/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
81 82 83 84 85 86 87 88 89 90 91 92 93 94

#define ADM1026_REG_CONFIG3	0x07
#define CFG3_GPIO16_ENABLE	0x01
#define CFG3_CI_CLEAR		0x02
#define CFG3_VREF_250		0x04
#define CFG3_GPIO16_DIR		0x40
#define CFG3_GPIO16_POL		0x80

#define ADM1026_REG_E2CONFIG	0x13
#define E2CFG_READ		0x01
#define E2CFG_WRITE		0x02
#define E2CFG_ERASE		0x04
#define E2CFG_ROM		0x08
#define E2CFG_CLK_EXT		0x80
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

/* There are 10 general analog inputs and 7 dedicated inputs
 * They are:
 *    0 - 9  =  AIN0 - AIN9
 *       10  =  Vbat
 *       11  =  3.3V Standby
 *       12  =  3.3V Main
 *       13  =  +5V
 *       14  =  Vccp (CPU core voltage)
 *       15  =  +12V
 *       16  =  -12V
 */
static u16 ADM1026_REG_IN[] = {
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
		0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
		0x2b, 0x2c, 0x2d, 0x2e, 0x2f
	};
static u16 ADM1026_REG_IN_MIN[] = {
		0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
		0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
		0x4b, 0x4c, 0x4d, 0x4e, 0x4f
	};
static u16 ADM1026_REG_IN_MAX[] = {
		0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
		0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
		0x43, 0x44, 0x45, 0x46, 0x47
	};

/* Temperatures are:
 *    0 - Internal
 *    1 - External 1
 *    2 - External 2
 */
static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };

135 136 137 138
#define ADM1026_REG_FAN(nr)		(0x38 + (nr))
#define ADM1026_REG_FAN_MIN(nr)		(0x60 + (nr))
#define ADM1026_REG_FAN_DIV_0_3		0x02
#define ADM1026_REG_FAN_DIV_4_7		0x03
L
Linus Torvalds 已提交
139

140 141
#define ADM1026_REG_DAC			0x04
#define ADM1026_REG_PWM			0x05
L
Linus Torvalds 已提交
142

143 144 145 146
#define ADM1026_REG_GPIO_CFG_0_3	0x08
#define ADM1026_REG_GPIO_CFG_4_7	0x09
#define ADM1026_REG_GPIO_CFG_8_11	0x0a
#define ADM1026_REG_GPIO_CFG_12_15	0x0b
L
Linus Torvalds 已提交
147
/* CFG_16 in REG_CFG3 */
148 149
#define ADM1026_REG_GPIO_STATUS_0_7	0x24
#define ADM1026_REG_GPIO_STATUS_8_15	0x25
L
Linus Torvalds 已提交
150
/* STATUS_16 in REG_STATUS4 */
151 152
#define ADM1026_REG_GPIO_MASK_0_7	0x1c
#define ADM1026_REG_GPIO_MASK_8_15	0x1d
L
Linus Torvalds 已提交
153 154
/* MASK_16 in REG_MASK4 */

155 156
#define ADM1026_REG_COMPANY		0x16
#define ADM1026_REG_VERSTEP		0x17
L
Linus Torvalds 已提交
157
/* These are the recognized values for the above regs */
158 159 160
#define ADM1026_COMPANY_ANALOG_DEV	0x41
#define ADM1026_VERSTEP_GENERIC		0x40
#define ADM1026_VERSTEP_ADM1026		0x44
L
Linus Torvalds 已提交
161

162 163 164 165
#define ADM1026_REG_MASK1		0x18
#define ADM1026_REG_MASK2		0x19
#define ADM1026_REG_MASK3		0x1a
#define ADM1026_REG_MASK4		0x1b
L
Linus Torvalds 已提交
166

167 168 169 170
#define ADM1026_REG_STATUS1		0x20
#define ADM1026_REG_STATUS2		0x21
#define ADM1026_REG_STATUS3		0x22
#define ADM1026_REG_STATUS4		0x23
L
Linus Torvalds 已提交
171 172

#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
173 174
#define ADM1026_FAN_CONTROL_TEMP_RANGE	20
#define ADM1026_PWM_MAX			255
L
Linus Torvalds 已提交
175

176
/* Conversions. Rounding and limit checking is only done on the TO_REG
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
 * variants. Note that you should be a bit careful with which arguments
 * these macros are called: arguments may be evaluated more than once.
 */

/* IN are scaled acording to built-in resistors.  These are the
 *   voltages corresponding to 3/4 of full scale (192 or 0xc0)
 *   NOTE: The -12V input needs an additional factor to account
 *      for the Vref pullup resistor.
 *      NEG12_OFFSET = SCALE * Vref / V-192 - Vref
 *                   = 13875 * 2.50 / 1.875 - 2500
 *                   = 16000
 *
 * The values in this table are based on Table II, page 15 of the
 *    datasheet.
 */
192 193 194
static int adm1026_scaling[] = { /* .001 Volts */
		2250, 2250, 2250, 2250, 2250, 2250,
		1875, 1875, 1875, 1875, 3000, 3330,
L
Linus Torvalds 已提交
195 196 197
		3330, 4995, 2250, 12000, 13875
	};
#define NEG12_OFFSET  16000
198 199 200 201
#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
#define INS_TO_REG(n, val)  (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
	0, 255))
#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
L
Linus Torvalds 已提交
202 203 204 205 206

/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
 *   and we assume a 2 pulse-per-rev fan tach signal
 *      22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
 */
207 208 209 210
#define FAN_TO_REG(val, div)  ((val) <= 0 ? 0xff : \
				SENSORS_LIMIT(1350000/((val)*(div)), 1, 254))
#define FAN_FROM_REG(val, div) ((val) == 0 ? -1:(val) == 0xff ? 0 : \
				1350000/((val)*(div)))
L
Linus Torvalds 已提交
211
#define DIV_FROM_REG(val) (1<<(val))
212
#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
L
Linus Torvalds 已提交
213 214 215

/* Temperature is reported in 1 degC increments */
#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
216
	-127, 127))
L
Linus Torvalds 已提交
217 218
#define TEMP_FROM_REG(val) ((val) * 1000)
#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
219
	-127, 127))
L
Linus Torvalds 已提交
220 221
#define OFFSET_FROM_REG(val) ((val) * 1000)

222
#define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
L
Linus Torvalds 已提交
223 224 225 226 227
#define PWM_FROM_REG(val) (val)

#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))

228 229
/* Analog output is a voltage, and scaled to millivolts.  The datasheet
 *   indicates that the DAC could be used to drive the fans, but in our
L
Linus Torvalds 已提交
230 231
 *   example board (Arima HDAMA) it isn't connected to the fans at all.
 */
232
#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500), 0, 255))
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245
#define DAC_FROM_REG(val) (((val)*2500)/255)

/* Chip sampling rates
 *
 * Some sensors are not updated more frequently than once per second
 *    so it doesn't make sense to read them more often than that.
 *    We cache the results and return the saved data if the driver
 *    is called again before a second has elapsed.
 *
 * Also, there is significant configuration data for this chip
 *    So, we keep the config data up to date in the cache
 *    when it is written and only sample it once every 5 *minutes*
 */
246 247
#define ADM1026_DATA_INTERVAL		(1 * HZ)
#define ADM1026_CONFIG_INTERVAL		(5 * 60 * HZ)
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

/* We allow for multiple chips in a single system.
 *
 * For each registered ADM1026, we need to keep state information
 * at client->data. The adm1026_data structure is dynamically
 * allocated, when a new client structure is allocated. */

struct pwm_data {
	u8 pwm;
	u8 enable;
	u8 auto_pwm_min;
};

struct adm1026_data {
	struct i2c_client client;
263
	struct device *hwmon_dev;
L
Linus Torvalds 已提交
264

265
	struct mutex update_lock;
L
Linus Torvalds 已提交
266 267 268 269
	int valid;		/* !=0 if following fields are valid */
	unsigned long last_reading;	/* In jiffies */
	unsigned long last_config;	/* In jiffies */

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	u8 in[17];		/* Register value */
	u8 in_max[17];		/* Register value */
	u8 in_min[17];		/* Register value */
	s8 temp[3];		/* Register value */
	s8 temp_min[3];		/* Register value */
	s8 temp_max[3];		/* Register value */
	s8 temp_tmin[3];	/* Register value */
	s8 temp_crit[3];	/* Register value */
	s8 temp_offset[3];	/* Register value */
	u8 fan[8];		/* Register value */
	u8 fan_min[8];		/* Register value */
	u8 fan_div[8];		/* Decoded value */
	struct pwm_data pwm1;	/* Pwm control values */
	int vid;		/* Decoded value */
	u8 vrm;			/* VRM version */
L
Linus Torvalds 已提交
285
	u8 analog_out;		/* Register value (DAC) */
286 287 288 289 290 291 292 293
	long alarms;		/* Register encoding, combined */
	long alarm_mask;	/* Register encoding, combined */
	long gpio;		/* Register encoding, combined */
	long gpio_mask;		/* Register encoding, combined */
	u8 gpio_config[17];	/* Decoded value */
	u8 config1;		/* Register value */
	u8 config2;		/* Register value */
	u8 config3;		/* Register value */
L
Linus Torvalds 已提交
294 295 296 297 298 299
};

static int adm1026_attach_adapter(struct i2c_adapter *adapter);
static int adm1026_detect(struct i2c_adapter *adapter, int address,
	int kind);
static int adm1026_detach_client(struct i2c_client *client);
300 301
static int adm1026_read_value(struct i2c_client *client, u8 reg);
static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
L
Linus Torvalds 已提交
302
static void adm1026_print_gpio(struct i2c_client *client);
303
static void adm1026_fixup_gpio(struct i2c_client *client);
L
Linus Torvalds 已提交
304 305 306 307 308
static struct adm1026_data *adm1026_update_device(struct device *dev);
static void adm1026_init_client(struct i2c_client *client);


static struct i2c_driver adm1026_driver = {
309 310 311
	.driver = {
		.name	= "adm1026",
	},
L
Linus Torvalds 已提交
312
	.attach_adapter = adm1026_attach_adapter,
313
	.detach_client	= adm1026_detach_client,
L
Linus Torvalds 已提交
314 315
};

316
static int adm1026_attach_adapter(struct i2c_adapter *adapter)
L
Linus Torvalds 已提交
317 318 319 320
{
	if (!(adapter->class & I2C_CLASS_HWMON)) {
		return 0;
	}
321
	return i2c_probe(adapter, &addr_data, adm1026_detect);
L
Linus Torvalds 已提交
322 323
}

324
static int adm1026_read_value(struct i2c_client *client, u8 reg)
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337
{
	int res;

	if (reg < 0x80) {
		/* "RAM" locations */
		res = i2c_smbus_read_byte_data(client, reg) & 0xff;
	} else {
		/* EEPROM, do nothing */
		res = 0;
	}
	return res;
}

338
static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351
{
	int res;

	if (reg < 0x80) {
		/* "RAM" locations */
		res = i2c_smbus_write_byte_data(client, reg, value);
	} else {
		/* EEPROM, do nothing */
		res = 0;
	}
	return res;
}

352
static void adm1026_init_client(struct i2c_client *client)
L
Linus Torvalds 已提交
353 354 355 356
{
	int value, i;
	struct adm1026_data *data = i2c_get_clientdata(client);

357
	dev_dbg(&client->dev, "Initializing device\n");
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	/* Read chip config */
	data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
	data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
	data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);

	/* Inform user of chip config */
	dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
		data->config1);
	if ((data->config1 & CFG1_MONITOR) == 0) {
		dev_dbg(&client->dev, "Monitoring not currently "
			"enabled.\n");
	}
	if (data->config1 & CFG1_INT_ENABLE) {
		dev_dbg(&client->dev, "SMBALERT interrupts are "
			"enabled.\n");
	}
	if (data->config1 & CFG1_AIN8_9) {
		dev_dbg(&client->dev, "in8 and in9 enabled. "
			"temp3 disabled.\n");
	} else {
		dev_dbg(&client->dev, "temp3 enabled.  in8 and "
			"in9 disabled.\n");
	}
	if (data->config1 & CFG1_THERM_HOT) {
		dev_dbg(&client->dev, "Automatic THERM, PWM, "
			"and temp limits enabled.\n");
	}

	if (data->config3 & CFG3_GPIO16_ENABLE) {
387
		dev_dbg(&client->dev, "GPIO16 enabled.  THERM "
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
			"pin disabled.\n");
	} else {
		dev_dbg(&client->dev, "THERM pin enabled.  "
			"GPIO16 disabled.\n");
	}
	if (data->config3 & CFG3_VREF_250) {
		dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
	} else {
		dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
	}
	/* Read and pick apart the existing GPIO configuration */
	value = 0;
	for (i = 0;i <= 15;++i) {
		if ((i & 0x03) == 0) {
			value = adm1026_read_value(client,
					ADM1026_REG_GPIO_CFG_0_3 + i/4);
		}
		data->gpio_config[i] = value & 0x03;
		value >>= 2;
	}
	data->gpio_config[16] = (data->config3 >> 6) & 0x03;

	/* ... and then print it */
	adm1026_print_gpio(client);

	/* If the user asks us to reprogram the GPIO config, then
	 * do it now.
	 */
	if (gpio_input[0] != -1 || gpio_output[0] != -1
		|| gpio_inverted[0] != -1 || gpio_normal[0] != -1
		|| gpio_fan[0] != -1) {
		adm1026_fixup_gpio(client);
	}

	/* WE INTENTIONALLY make no changes to the limits,
	 *   offsets, pwms, fans and zones.  If they were
	 *   configured, we don't want to mess with them.
	 *   If they weren't, the default is 100% PWM, no
	 *   control and will suffice until 'sensors -s'
427
	 *   can be run by the user.  We DO set the default
L
Linus Torvalds 已提交
428 429
	 *   value for pwm1.auto_pwm_min to its maximum
	 *   so that enabling automatic pwm fan control
430
	 *   without first setting a value for pwm1.auto_pwm_min
L
Linus Torvalds 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	 *   will not result in potentially dangerous fan speed decrease.
	 */
	data->pwm1.auto_pwm_min=255;
	/* Start monitoring */
	value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
	/* Set MONITOR, clear interrupt acknowledge and s/w reset */
	value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
	dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
	data->config1 = value;
	adm1026_write_value(client, ADM1026_REG_CONFIG1, value);

	/* initialize fan_div[] to hardware defaults */
	value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
		(adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
	for (i = 0;i <= 7;++i) {
		data->fan_div[i] = DIV_FROM_REG(value & 0x03);
		value >>= 2;
	}
}

451
static void adm1026_print_gpio(struct i2c_client *client)
L
Linus Torvalds 已提交
452 453
{
	struct adm1026_data *data = i2c_get_clientdata(client);
454
	int i;
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

	dev_dbg(&client->dev, "GPIO config is:");
	for (i = 0;i <= 7;++i) {
		if (data->config2 & (1 << i)) {
			dev_dbg(&client->dev, "\t%sGP%s%d\n",
				data->gpio_config[i] & 0x02 ? "" : "!",
				data->gpio_config[i] & 0x01 ? "OUT" : "IN",
				i);
		} else {
			dev_dbg(&client->dev, "\tFAN%d\n", i);
		}
	}
	for (i = 8;i <= 15;++i) {
		dev_dbg(&client->dev, "\t%sGP%s%d\n",
			data->gpio_config[i] & 0x02 ? "" : "!",
			data->gpio_config[i] & 0x01 ? "OUT" : "IN",
			i);
	}
	if (data->config3 & CFG3_GPIO16_ENABLE) {
		dev_dbg(&client->dev, "\t%sGP%s16\n",
			data->gpio_config[16] & 0x02 ? "" : "!",
			data->gpio_config[16] & 0x01 ? "OUT" : "IN");
	} else {
478
		/* GPIO16 is THERM */
L
Linus Torvalds 已提交
479 480 481 482
		dev_dbg(&client->dev, "\tTHERM\n");
	}
}

483
static void adm1026_fixup_gpio(struct i2c_client *client)
L
Linus Torvalds 已提交
484 485
{
	struct adm1026_data *data = i2c_get_clientdata(client);
486 487
	int i;
	int value;
L
Linus Torvalds 已提交
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 516

	/* Make the changes requested. */
	/* We may need to unlock/stop monitoring or soft-reset the
	 *    chip before we can make changes.  This hasn't been
	 *    tested much.  FIXME
	 */

	/* Make outputs */
	for (i = 0;i <= 16;++i) {
		if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
			data->gpio_config[gpio_output[i]] |= 0x01;
		}
		/* if GPIO0-7 is output, it isn't a FAN tach */
		if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
			data->config2 |= 1 << gpio_output[i];
		}
	}

	/* Input overrides output */
	for (i = 0;i <= 16;++i) {
		if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
			data->gpio_config[gpio_input[i]] &= ~ 0x01;
		}
		/* if GPIO0-7 is input, it isn't a FAN tach */
		if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
			data->config2 |= 1 << gpio_input[i];
		}
	}

517
	/* Inverted */
L
Linus Torvalds 已提交
518 519 520 521 522 523
	for (i = 0;i <= 16;++i) {
		if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
			data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
		}
	}

524
	/* Normal overrides inverted */
L
Linus Torvalds 已提交
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
	for (i = 0;i <= 16;++i) {
		if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
			data->gpio_config[gpio_normal[i]] |= 0x02;
		}
	}

	/* Fan overrides input and output */
	for (i = 0;i <= 7;++i) {
		if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
			data->config2 &= ~(1 << gpio_fan[i]);
		}
	}

	/* Write new configs to registers */
	adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
	data->config3 = (data->config3 & 0x3f)
			| ((data->gpio_config[16] & 0x03) << 6);
	adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
	for (i = 15, value = 0;i >= 0;--i) {
		value <<= 2;
		value |= data->gpio_config[i] & 0x03;
		if ((i & 0x03) == 0) {
			adm1026_write_value(client,
					ADM1026_REG_GPIO_CFG_0_3 + i/4,
					value);
			value = 0;
		}
	}

	/* Print the new config */
	adm1026_print_gpio(client);
}


static struct adm1026_data *adm1026_update_device(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int i;
	long value, alarms, gpio;

566
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
567 568 569
	if (!data->valid
	    || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
		/* Things that change quickly */
570
		dev_dbg(&client->dev, "Reading sensor values\n");
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582
		for (i = 0;i <= 16;++i) {
			data->in[i] =
			    adm1026_read_value(client, ADM1026_REG_IN[i]);
		}

		for (i = 0;i <= 7;++i) {
			data->fan[i] =
			    adm1026_read_value(client, ADM1026_REG_FAN(i));
		}

		for (i = 0;i <= 2;++i) {
			/* NOTE: temp[] is s8 and we assume 2's complement
583
			 *   "conversion" in the assignment */
L
Linus Torvalds 已提交
584 585 586 587
			data->temp[i] =
			    adm1026_read_value(client, ADM1026_REG_TEMP[i]);
		}

588
		data->pwm1.pwm = adm1026_read_value(client,
L
Linus Torvalds 已提交
589
			ADM1026_REG_PWM);
590
		data->analog_out = adm1026_read_value(client,
L
Linus Torvalds 已提交
591 592 593
			ADM1026_REG_DAC);
		/* GPIO16 is MSbit of alarms, move it to gpio */
		alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
594
		gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604
		alarms &= 0x7f;
		alarms <<= 8;
		alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
		alarms <<= 8;
		alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
		alarms <<= 8;
		alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
		data->alarms = alarms;

		/* Read the GPIO values */
605
		gpio |= adm1026_read_value(client,
L
Linus Torvalds 已提交
606 607
			ADM1026_REG_GPIO_STATUS_8_15);
		gpio <<= 8;
608
		gpio |= adm1026_read_value(client,
L
Linus Torvalds 已提交
609 610 611 612
			ADM1026_REG_GPIO_STATUS_0_7);
		data->gpio = gpio;

		data->last_reading = jiffies;
613
	}; /* last_reading */
L
Linus Torvalds 已提交
614 615 616 617 618 619

	if (!data->valid ||
	    time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
		/* Things that don't change often */
		dev_dbg(&client->dev, "Reading config values\n");
		for (i = 0;i <= 16;++i) {
620
			data->in_min[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
621
				ADM1026_REG_IN_MIN[i]);
622
			data->in_max[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
623 624 625 626 627 628 629
				ADM1026_REG_IN_MAX[i]);
		}

		value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
			| (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
			<< 8);
		for (i = 0;i <= 7;++i) {
630
			data->fan_min[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
631 632 633 634 635 636
				ADM1026_REG_FAN_MIN(i));
			data->fan_div[i] = DIV_FROM_REG(value & 0x03);
			value >>= 2;
		}

		for (i = 0; i <= 2; ++i) {
637
			/* NOTE: temp_xxx[] are s8 and we assume 2's
L
Linus Torvalds 已提交
638 639
			 *    complement "conversion" in the assignment
			 */
640
			data->temp_min[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
641
				ADM1026_REG_TEMP_MIN[i]);
642
			data->temp_max[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
643
				ADM1026_REG_TEMP_MAX[i]);
644
			data->temp_tmin[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
645
				ADM1026_REG_TEMP_TMIN[i]);
646
			data->temp_crit[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
647
				ADM1026_REG_TEMP_THERM[i]);
648
			data->temp_offset[i] = adm1026_read_value(client,
L
Linus Torvalds 已提交
649 650 651 652
				ADM1026_REG_TEMP_OFFSET[i]);
		}

		/* Read the STATUS/alarm masks */
653 654 655
		alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
		gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
		alarms = (alarms & 0x7f) << 8;
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663
		alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
		alarms <<= 8;
		alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
		alarms <<= 8;
		alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
		data->alarm_mask = alarms;

		/* Read the GPIO values */
664
		gpio |= adm1026_read_value(client,
L
Linus Torvalds 已提交
665 666 667 668 669 670
			ADM1026_REG_GPIO_MASK_8_15);
		gpio <<= 8;
		gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
		data->gpio_mask = gpio;

		/* Read various values from CONFIG1 */
671
		data->config1 = adm1026_read_value(client,
L
Linus Torvalds 已提交
672 673 674
			ADM1026_REG_CONFIG1);
		if (data->config1 & CFG1_PWM_AFC) {
			data->pwm1.enable = 2;
675
			data->pwm1.auto_pwm_min =
L
Linus Torvalds 已提交
676 677 678
				PWM_MIN_FROM_REG(data->pwm1.pwm);
		}
		/* Read the GPIO config */
679
		data->config2 = adm1026_read_value(client,
L
Linus Torvalds 已提交
680
			ADM1026_REG_CONFIG2);
681
		data->config3 = adm1026_read_value(client,
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695
			ADM1026_REG_CONFIG3);
		data->gpio_config[16] = (data->config3 >> 6) & 0x03;

		value = 0;
		for (i = 0;i <= 15;++i) {
			if ((i & 0x03) == 0) {
				value = adm1026_read_value(client,
					    ADM1026_REG_GPIO_CFG_0_3 + i/4);
			}
			data->gpio_config[i] = value & 0x03;
			value >>= 2;
		}

		data->last_config = jiffies;
696
	}; /* last_config */
L
Linus Torvalds 已提交
697 698 699 700

	dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
	data->vid = (data->gpio >> 11) & 0x1f;
	data->valid = 1;
701
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
702 703 704
	return data;
}

705 706
static ssize_t show_in(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
707
{
708 709
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
710
	struct adm1026_data *data = adm1026_update_device(dev);
711
	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
L
Linus Torvalds 已提交
712
}
713 714
static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
715
{
716 717
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
718 719
	struct adm1026_data *data = adm1026_update_device(dev);
	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
L
Linus Torvalds 已提交
720
}
721 722
static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
723
{
724 725
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
726 727 728 729
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

730
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
731 732
	data->in_min[nr] = INS_TO_REG(nr, val);
	adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
733
	mutex_unlock(&data->update_lock);
734
	return count;
L
Linus Torvalds 已提交
735
}
736 737
static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
738
{
739 740
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
741
	struct adm1026_data *data = adm1026_update_device(dev);
742
	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
L
Linus Torvalds 已提交
743
}
744 745
static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
746
{
747 748
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
749 750 751 752
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

753
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
754 755
	data->in_max[nr] = INS_TO_REG(nr, val);
	adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
756
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
757 758 759
	return count;
}

760 761 762 763 764 765 766
#define in_reg(offset)						\
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, set_in_min, offset);		\
static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,	\
		show_in_max, set_in_max, offset);
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785


in_reg(0);
in_reg(1);
in_reg(2);
in_reg(3);
in_reg(4);
in_reg(5);
in_reg(6);
in_reg(7);
in_reg(8);
in_reg(9);
in_reg(10);
in_reg(11);
in_reg(12);
in_reg(13);
in_reg(14);
in_reg(15);

786
static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
787 788
{
	struct adm1026_data *data = adm1026_update_device(dev);
789
	return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
L
Linus Torvalds 已提交
790 791
		NEG12_OFFSET);
}
792
static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
793
{
794 795
	struct adm1026_data *data = adm1026_update_device(dev);
	return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
L
Linus Torvalds 已提交
796 797
		- NEG12_OFFSET);
}
798
static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
799 800 801 802 803
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

804
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
805 806
	data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
	adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
807
	mutex_unlock(&data->update_lock);
808
	return count;
L
Linus Torvalds 已提交
809
}
810
static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
811 812
{
	struct adm1026_data *data = adm1026_update_device(dev);
813
	return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
L
Linus Torvalds 已提交
814 815
			- NEG12_OFFSET);
}
816
static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
817 818 819 820 821
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

822
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
823 824
	data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
	adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
825
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
826 827 828
	return count;
}

829 830 831
static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
L
Linus Torvalds 已提交
832 833 834 835 836 837




/* Now add fan read/write functions */

838 839
static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
840
{
841 842
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
843
	struct adm1026_data *data = adm1026_update_device(dev);
844
	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
L
Linus Torvalds 已提交
845 846
		data->fan_div[nr]));
}
847 848
static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
849
{
850 851
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
852
	struct adm1026_data *data = adm1026_update_device(dev);
853
	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
L
Linus Torvalds 已提交
854 855
		data->fan_div[nr]));
}
856 857
static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
858
{
859 860
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
861 862 863 864
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

865
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
866 867 868
	data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
	adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
		data->fan_min[nr]);
869
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
870 871 872
	return count;
}

873 874 875 876
#define fan_offset(offset)						\
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,		\
877
		show_fan_min, set_fan_min, offset - 1);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

fan_offset(1);
fan_offset(2);
fan_offset(3);
fan_offset(4);
fan_offset(5);
fan_offset(6);
fan_offset(7);
fan_offset(8);

/* Adjust fan_min to account for new fan divisor */
static void fixup_fan_min(struct device *dev, int fan, int old_div)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
893 894
	int new_min;
	int new_div = data->fan_div[fan];
L
Linus Torvalds 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907

	/* 0 and 0xff are special.  Don't adjust them */
	if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
		return;
	}

	new_min = data->fan_min[fan] * old_div / new_div;
	new_min = SENSORS_LIMIT(new_min, 1, 254);
	data->fan_min[fan] = new_min;
	adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
}

/* Now add fan_div read/write functions */
908 909
static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
910
{
911 912
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
913
	struct adm1026_data *data = adm1026_update_device(dev);
914
	return sprintf(buf, "%d\n", data->fan_div[nr]);
L
Linus Torvalds 已提交
915
}
916 917
static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
918
{
919 920
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
921 922
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
923
	int val, orig_div, new_div, shift;
L
Linus Torvalds 已提交
924 925

	val = simple_strtol(buf, NULL, 10);
926
	new_div = DIV_TO_REG(val);
L
Linus Torvalds 已提交
927 928 929
	if (new_div == 0) {
		return -EINVAL;
	}
930
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	orig_div = data->fan_div[nr];
	data->fan_div[nr] = DIV_FROM_REG(new_div);

	if (nr < 4) { /* 0 <= nr < 4 */
		shift = 2 * nr;
		adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
			((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
			(new_div << shift)));
	} else { /* 3 < nr < 8 */
		shift = 2 * (nr - 4);
		adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
			((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
			(new_div << shift)));
	}

	if (data->fan_div[nr] != orig_div) {
947
		fixup_fan_min(dev, nr, orig_div);
L
Linus Torvalds 已提交
948
	}
949
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
950 951 952
	return count;
}

953 954
#define fan_offset_div(offset)						\
static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,		\
955
		show_fan_div, set_fan_div, offset - 1);
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963 964 965 966

fan_offset_div(1);
fan_offset_div(2);
fan_offset_div(3);
fan_offset_div(4);
fan_offset_div(5);
fan_offset_div(6);
fan_offset_div(7);
fan_offset_div(8);

/* Temps */
967 968
static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
969
{
970 971
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
972
	struct adm1026_data *data = adm1026_update_device(dev);
973
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
L
Linus Torvalds 已提交
974
}
975 976
static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
977
{
978 979
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
980
	struct adm1026_data *data = adm1026_update_device(dev);
981
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
L
Linus Torvalds 已提交
982
}
983 984
static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
985
{
986 987
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
988 989 990 991
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

992
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
993 994 995
	data->temp_min[nr] = TEMP_TO_REG(val);
	adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
		data->temp_min[nr]);
996
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
997 998
	return count;
}
999 1000
static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
		char *buf)
L
Linus Torvalds 已提交
1001
{
1002 1003
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1004
	struct adm1026_data *data = adm1026_update_device(dev);
1005
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
L
Linus Torvalds 已提交
1006
}
1007 1008
static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
1009
{
1010 1011
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1012 1013 1014 1015
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

1016
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1017 1018 1019
	data->temp_max[nr] = TEMP_TO_REG(val);
	adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
		data->temp_max[nr]);
1020
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1021 1022
	return count;
}
1023 1024

#define temp_reg(offset)						\
1025
static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp,	\
1026 1027 1028 1029 1030
		NULL, offset - 1);					\
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);
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036


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

1037 1038
static ssize_t show_temp_offset(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1039
{
1040 1041
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1042
	struct adm1026_data *data = adm1026_update_device(dev);
1043
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
L
Linus Torvalds 已提交
1044
}
1045 1046 1047
static ssize_t set_temp_offset(struct device *dev,
		struct device_attribute *attr, const char *buf,
		size_t count)
L
Linus Torvalds 已提交
1048
{
1049 1050
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1051 1052 1053 1054
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

1055
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1056 1057 1058
	data->temp_offset[nr] = TEMP_TO_REG(val);
	adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
		data->temp_offset[nr]);
1059
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1060 1061 1062
	return count;
}

1063 1064 1065
#define temp_offset_reg(offset)							\
static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR,		\
		show_temp_offset, set_temp_offset, offset - 1);
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070

temp_offset_reg(1);
temp_offset_reg(2);
temp_offset_reg(3);

1071 1072
static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1073
{
1074 1075
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1076
	struct adm1026_data *data = adm1026_update_device(dev);
1077
	return sprintf(buf, "%d\n", TEMP_FROM_REG(
L
Linus Torvalds 已提交
1078 1079
		ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
}
1080 1081
static ssize_t show_temp_auto_point2_temp(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1082
{
1083 1084
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1085
	struct adm1026_data *data = adm1026_update_device(dev);
1086
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
L
Linus Torvalds 已提交
1087 1088
		ADM1026_FAN_CONTROL_TEMP_RANGE));
}
1089 1090
static ssize_t show_temp_auto_point1_temp(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1091
{
1092 1093
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1094
	struct adm1026_data *data = adm1026_update_device(dev);
1095
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
L
Linus Torvalds 已提交
1096
}
1097 1098
static ssize_t set_temp_auto_point1_temp(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
1099
{
1100 1101
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1102 1103 1104 1105
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

1106
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1107 1108 1109
	data->temp_tmin[nr] = TEMP_TO_REG(val);
	adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
		data->temp_tmin[nr]);
1110
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1111 1112 1113
	return count;
}

1114 1115 1116 1117 1118 1119 1120
#define temp_auto_point(offset)						\
static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp,		\
		S_IRUGO | S_IWUSR, show_temp_auto_point1_temp,		\
		set_temp_auto_point1_temp, offset - 1);			\
static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
		show_temp_auto_point1_temp_hyst, NULL, offset - 1);	\
static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO,	\
1121
		show_temp_auto_point2_temp, NULL, offset - 1);
L
Linus Torvalds 已提交
1122 1123 1124 1125 1126

temp_auto_point(1);
temp_auto_point(2);
temp_auto_point(3);

1127 1128
static ssize_t show_temp_crit_enable(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1129 1130
{
	struct adm1026_data *data = adm1026_update_device(dev);
1131
	return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
L
Linus Torvalds 已提交
1132
}
1133 1134
static ssize_t set_temp_crit_enable(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139 1140
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

	if ((val == 1) || (val==0)) {
1141
		mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1142
		data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1143
		adm1026_write_value(client, ADM1026_REG_CONFIG1,
L
Linus Torvalds 已提交
1144
			data->config1);
1145
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1146 1147 1148 1149
	}
	return count;
}

1150 1151
#define temp_crit_enable(offset)				\
static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
L
Linus Torvalds 已提交
1152 1153
	show_temp_crit_enable, set_temp_crit_enable);

1154 1155 1156
temp_crit_enable(1);
temp_crit_enable(2);
temp_crit_enable(3);
L
Linus Torvalds 已提交
1157

1158 1159
static ssize_t show_temp_crit(struct device *dev,
		struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1160
{
1161 1162
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1163
	struct adm1026_data *data = adm1026_update_device(dev);
1164
	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
L
Linus Torvalds 已提交
1165
}
1166 1167
static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
L
Linus Torvalds 已提交
1168
{
1169 1170
	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
	int nr = sensor_attr->index;
L
Linus Torvalds 已提交
1171 1172 1173 1174
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

1175
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1176 1177 1178
	data->temp_crit[nr] = TEMP_TO_REG(val);
	adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
		data->temp_crit[nr]);
1179
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1180 1181 1182
	return count;
}

1183 1184 1185
#define temp_crit_reg(offset)						\
static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,	\
		show_temp_crit, set_temp_crit, offset - 1);
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190

temp_crit_reg(1);
temp_crit_reg(2);
temp_crit_reg(3);

1191
static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1192 1193
{
	struct adm1026_data *data = adm1026_update_device(dev);
1194
	return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
L
Linus Torvalds 已提交
1195
}
1196
static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

1203
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1204 1205
	data->analog_out = DAC_TO_REG(val);
	adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1206
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1207 1208 1209
	return count;
}

1210
static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
L
Linus Torvalds 已提交
1211 1212
	set_analog_out_reg);

1213
static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1214 1215
{
	struct adm1026_data *data = adm1026_update_device(dev);
1216
	return sprintf(buf, "%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
L
Linus Torvalds 已提交
1217
}
1218
static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
L
Linus Torvalds 已提交
1219

1220
static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1221
{
1222
	struct adm1026_data *data = dev_get_drvdata(dev);
1223
	return sprintf(buf, "%d\n", data->vrm);
L
Linus Torvalds 已提交
1224
}
1225
static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1226 1227
		size_t count)
{
1228
	struct adm1026_data *data = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235

	data->vrm = simple_strtol(buf, NULL, 10);
	return count;
}

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

1236
static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1237 1238
{
	struct adm1026_data *data = adm1026_update_device(dev);
1239
	return sprintf(buf, "%ld\n", data->alarms);
L
Linus Torvalds 已提交
1240 1241 1242 1243
}

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

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
			  char *buf)
{
	struct adm1026_data *data = adm1026_update_device(dev);
	int bitnr = to_sensor_dev_attr(attr)->index;
	return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
}

static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);

1281
static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1282 1283
{
	struct adm1026_data *data = adm1026_update_device(dev);
1284
	return sprintf(buf, "%ld\n", data->alarm_mask);
L
Linus Torvalds 已提交
1285
}
1286
static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1287 1288 1289 1290 1291 1292 1293
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
	unsigned long mask;

1294
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	data->alarm_mask = val & 0x7fffffff;
	mask = data->alarm_mask
		| (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
	adm1026_write_value(client, ADM1026_REG_MASK1,
		mask & 0xff);
	mask >>= 8;
	adm1026_write_value(client, ADM1026_REG_MASK2,
		mask & 0xff);
	mask >>= 8;
	adm1026_write_value(client, ADM1026_REG_MASK3,
		mask & 0xff);
	mask >>= 8;
	adm1026_write_value(client, ADM1026_REG_MASK4,
		mask & 0xff);
1309
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1310 1311 1312 1313 1314 1315 1316
	return count;
}

static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
	set_alarm_mask);


1317
static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1318 1319
{
	struct adm1026_data *data = adm1026_update_device(dev);
1320
	return sprintf(buf, "%ld\n", data->gpio);
L
Linus Torvalds 已提交
1321
}
1322
static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1323 1324 1325 1326 1327
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
1328
	long gpio;
L
Linus Torvalds 已提交
1329

1330
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1331 1332
	data->gpio = val & 0x1ffff;
	gpio = data->gpio;
1333
	adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
L
Linus Torvalds 已提交
1334
	gpio >>= 8;
1335
	adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
L
Linus Torvalds 已提交
1336
	gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1337
	adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
1338
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344
	return count;
}

static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);


1345
static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1346 1347
{
	struct adm1026_data *data = adm1026_update_device(dev);
1348
	return sprintf(buf, "%ld\n", data->gpio_mask);
L
Linus Torvalds 已提交
1349
}
1350
static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1351 1352 1353 1354 1355
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
1356
	long mask;
L
Linus Torvalds 已提交
1357

1358
	mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1359 1360
	data->gpio_mask = val & 0x1ffff;
	mask = data->gpio_mask;
1361
	adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
L
Linus Torvalds 已提交
1362
	mask >>= 8;
1363
	adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
L
Linus Torvalds 已提交
1364
	mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1365
	adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
1366
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1367 1368 1369 1370 1371
	return count;
}

static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);

1372
static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1373 1374
{
	struct adm1026_data *data = adm1026_update_device(dev);
1375
	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
L
Linus Torvalds 已提交
1376
}
1377
static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1378 1379 1380 1381 1382 1383 1384 1385
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);

	if (data->pwm1.enable == 1) {
		int val = simple_strtol(buf, NULL, 10);

1386
		mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1387 1388
		data->pwm1.pwm = PWM_TO_REG(val);
		adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1389
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1390 1391 1392
	}
	return count;
}
1393
static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1394 1395
{
	struct adm1026_data *data = adm1026_update_device(dev);
1396
	return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
L
Linus Torvalds 已提交
1397
}
1398
static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1399 1400 1401 1402 1403 1404
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);

1405
	mutex_lock(&data->update_lock);
1406
	data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
L
Linus Torvalds 已提交
1407 1408
	if (data->pwm1.enable == 2) { /* apply immediately */
		data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1409
			PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
L
Linus Torvalds 已提交
1410 1411
		adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
	}
1412
	mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1413 1414
	return count;
}
1415
static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1416
{
1417
	return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
L
Linus Torvalds 已提交
1418
}
1419
static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1420 1421
{
	struct adm1026_data *data = adm1026_update_device(dev);
1422
	return sprintf(buf, "%d\n", data->pwm1.enable);
L
Linus Torvalds 已提交
1423
}
1424
static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
1425 1426 1427 1428 1429
		size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct adm1026_data *data = i2c_get_clientdata(client);
	int val = simple_strtol(buf, NULL, 10);
1430
	int old_enable;
L
Linus Torvalds 已提交
1431 1432

	if ((val >= 0) && (val < 3)) {
1433
		mutex_lock(&data->update_lock);
L
Linus Torvalds 已提交
1434 1435 1436 1437 1438 1439
		old_enable = data->pwm1.enable;
		data->pwm1.enable = val;
		data->config1 = (data->config1 & ~CFG1_PWM_AFC)
				| ((val == 2) ? CFG1_PWM_AFC : 0);
		adm1026_write_value(client, ADM1026_REG_CONFIG1,
			data->config1);
1440
		if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
L
Linus Torvalds 已提交
1441
			data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1442 1443
				PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
			adm1026_write_value(client, ADM1026_REG_PWM,
L
Linus Torvalds 已提交
1444 1445 1446 1447
				data->pwm1.pwm);
		} else if (!((old_enable == 1) && (val == 1))) {
			/* set pwm to safe value */
			data->pwm1.pwm = 255;
1448
			adm1026_write_value(client, ADM1026_REG_PWM,
L
Linus Torvalds 已提交
1449 1450
				data->pwm1.pwm);
		}
1451
		mutex_unlock(&data->update_lock);
L
Linus Torvalds 已提交
1452 1453 1454 1455 1456
	}
	return count;
}

/* enable PWM fan control */
1457 1458 1459 1460
static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
L
Linus Torvalds 已提交
1461
	set_pwm_enable);
1462
static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
L
Linus Torvalds 已提交
1463
	set_pwm_enable);
1464
static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
L
Linus Torvalds 已提交
1465
	set_pwm_enable);
1466
static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
L
Linus Torvalds 已提交
1467
	show_auto_pwm_min, set_auto_pwm_min);
1468
static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
L
Linus Torvalds 已提交
1469
	show_auto_pwm_min, set_auto_pwm_min);
1470
static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
L
Linus Torvalds 已提交
1471 1472 1473 1474 1475 1476
	show_auto_pwm_min, set_auto_pwm_min);

static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);

1477 1478 1479 1480
static struct attribute *adm1026_attributes[] = {
	&sensor_dev_attr_in0_input.dev_attr.attr,
	&sensor_dev_attr_in0_max.dev_attr.attr,
	&sensor_dev_attr_in0_min.dev_attr.attr,
1481
	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1482 1483 1484
	&sensor_dev_attr_in1_input.dev_attr.attr,
	&sensor_dev_attr_in1_max.dev_attr.attr,
	&sensor_dev_attr_in1_min.dev_attr.attr,
1485
	&sensor_dev_attr_in1_alarm.dev_attr.attr,
1486 1487 1488
	&sensor_dev_attr_in2_input.dev_attr.attr,
	&sensor_dev_attr_in2_max.dev_attr.attr,
	&sensor_dev_attr_in2_min.dev_attr.attr,
1489
	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1490 1491 1492
	&sensor_dev_attr_in3_input.dev_attr.attr,
	&sensor_dev_attr_in3_max.dev_attr.attr,
	&sensor_dev_attr_in3_min.dev_attr.attr,
1493
	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1494 1495 1496
	&sensor_dev_attr_in4_input.dev_attr.attr,
	&sensor_dev_attr_in4_max.dev_attr.attr,
	&sensor_dev_attr_in4_min.dev_attr.attr,
1497
	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1498 1499 1500
	&sensor_dev_attr_in5_input.dev_attr.attr,
	&sensor_dev_attr_in5_max.dev_attr.attr,
	&sensor_dev_attr_in5_min.dev_attr.attr,
1501
	&sensor_dev_attr_in5_alarm.dev_attr.attr,
1502 1503 1504
	&sensor_dev_attr_in6_input.dev_attr.attr,
	&sensor_dev_attr_in6_max.dev_attr.attr,
	&sensor_dev_attr_in6_min.dev_attr.attr,
1505
	&sensor_dev_attr_in6_alarm.dev_attr.attr,
1506 1507 1508
	&sensor_dev_attr_in7_input.dev_attr.attr,
	&sensor_dev_attr_in7_max.dev_attr.attr,
	&sensor_dev_attr_in7_min.dev_attr.attr,
1509
	&sensor_dev_attr_in7_alarm.dev_attr.attr,
1510 1511 1512
	&sensor_dev_attr_in10_input.dev_attr.attr,
	&sensor_dev_attr_in10_max.dev_attr.attr,
	&sensor_dev_attr_in10_min.dev_attr.attr,
1513
	&sensor_dev_attr_in10_alarm.dev_attr.attr,
1514 1515 1516
	&sensor_dev_attr_in11_input.dev_attr.attr,
	&sensor_dev_attr_in11_max.dev_attr.attr,
	&sensor_dev_attr_in11_min.dev_attr.attr,
1517
	&sensor_dev_attr_in11_alarm.dev_attr.attr,
1518 1519 1520
	&sensor_dev_attr_in12_input.dev_attr.attr,
	&sensor_dev_attr_in12_max.dev_attr.attr,
	&sensor_dev_attr_in12_min.dev_attr.attr,
1521
	&sensor_dev_attr_in12_alarm.dev_attr.attr,
1522 1523 1524
	&sensor_dev_attr_in13_input.dev_attr.attr,
	&sensor_dev_attr_in13_max.dev_attr.attr,
	&sensor_dev_attr_in13_min.dev_attr.attr,
1525
	&sensor_dev_attr_in13_alarm.dev_attr.attr,
1526 1527 1528
	&sensor_dev_attr_in14_input.dev_attr.attr,
	&sensor_dev_attr_in14_max.dev_attr.attr,
	&sensor_dev_attr_in14_min.dev_attr.attr,
1529
	&sensor_dev_attr_in14_alarm.dev_attr.attr,
1530 1531 1532
	&sensor_dev_attr_in15_input.dev_attr.attr,
	&sensor_dev_attr_in15_max.dev_attr.attr,
	&sensor_dev_attr_in15_min.dev_attr.attr,
1533
	&sensor_dev_attr_in15_alarm.dev_attr.attr,
1534 1535 1536
	&sensor_dev_attr_in16_input.dev_attr.attr,
	&sensor_dev_attr_in16_max.dev_attr.attr,
	&sensor_dev_attr_in16_min.dev_attr.attr,
1537
	&sensor_dev_attr_in16_alarm.dev_attr.attr,
1538 1539 1540
	&sensor_dev_attr_fan1_input.dev_attr.attr,
	&sensor_dev_attr_fan1_div.dev_attr.attr,
	&sensor_dev_attr_fan1_min.dev_attr.attr,
1541
	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1542 1543 1544
	&sensor_dev_attr_fan2_input.dev_attr.attr,
	&sensor_dev_attr_fan2_div.dev_attr.attr,
	&sensor_dev_attr_fan2_min.dev_attr.attr,
1545
	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1546 1547 1548
	&sensor_dev_attr_fan3_input.dev_attr.attr,
	&sensor_dev_attr_fan3_div.dev_attr.attr,
	&sensor_dev_attr_fan3_min.dev_attr.attr,
1549
	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1550 1551 1552
	&sensor_dev_attr_fan4_input.dev_attr.attr,
	&sensor_dev_attr_fan4_div.dev_attr.attr,
	&sensor_dev_attr_fan4_min.dev_attr.attr,
1553
	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1554 1555 1556
	&sensor_dev_attr_fan5_input.dev_attr.attr,
	&sensor_dev_attr_fan5_div.dev_attr.attr,
	&sensor_dev_attr_fan5_min.dev_attr.attr,
1557
	&sensor_dev_attr_fan5_alarm.dev_attr.attr,
1558 1559 1560
	&sensor_dev_attr_fan6_input.dev_attr.attr,
	&sensor_dev_attr_fan6_div.dev_attr.attr,
	&sensor_dev_attr_fan6_min.dev_attr.attr,
1561
	&sensor_dev_attr_fan6_alarm.dev_attr.attr,
1562 1563 1564
	&sensor_dev_attr_fan7_input.dev_attr.attr,
	&sensor_dev_attr_fan7_div.dev_attr.attr,
	&sensor_dev_attr_fan7_min.dev_attr.attr,
1565
	&sensor_dev_attr_fan7_alarm.dev_attr.attr,
1566 1567 1568
	&sensor_dev_attr_fan8_input.dev_attr.attr,
	&sensor_dev_attr_fan8_div.dev_attr.attr,
	&sensor_dev_attr_fan8_min.dev_attr.attr,
1569
	&sensor_dev_attr_fan8_alarm.dev_attr.attr,
1570 1571 1572
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_temp1_max.dev_attr.attr,
	&sensor_dev_attr_temp1_min.dev_attr.attr,
1573
	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1574 1575 1576
	&sensor_dev_attr_temp2_input.dev_attr.attr,
	&sensor_dev_attr_temp2_max.dev_attr.attr,
	&sensor_dev_attr_temp2_min.dev_attr.attr,
1577
	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
	&sensor_dev_attr_temp1_offset.dev_attr.attr,
	&sensor_dev_attr_temp2_offset.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
	&sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
	&sensor_dev_attr_temp1_crit.dev_attr.attr,
	&sensor_dev_attr_temp2_crit.dev_attr.attr,
	&dev_attr_temp1_crit_enable.attr,
	&dev_attr_temp2_crit_enable.attr,
	&dev_attr_cpu0_vid.attr,
	&dev_attr_vrm.attr,
	&dev_attr_alarms.attr,
	&dev_attr_alarm_mask.attr,
	&dev_attr_gpio.attr,
	&dev_attr_gpio_mask.attr,
	&dev_attr_pwm1.attr,
	&dev_attr_pwm2.attr,
	&dev_attr_pwm3.attr,
	&dev_attr_pwm1_enable.attr,
	&dev_attr_pwm2_enable.attr,
	&dev_attr_pwm3_enable.attr,
	&dev_attr_temp1_auto_point1_pwm.attr,
	&dev_attr_temp2_auto_point1_pwm.attr,
	&dev_attr_temp1_auto_point2_pwm.attr,
	&dev_attr_temp2_auto_point2_pwm.attr,
	&dev_attr_analog_out.attr,
	NULL
};

static const struct attribute_group adm1026_group = {
	.attrs = adm1026_attributes,
};

1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
static struct attribute *adm1026_attributes_temp3[] = {
	&sensor_dev_attr_temp3_input.dev_attr.attr,
	&sensor_dev_attr_temp3_max.dev_attr.attr,
	&sensor_dev_attr_temp3_min.dev_attr.attr,
	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
	&sensor_dev_attr_temp3_offset.dev_attr.attr,
	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
	&sensor_dev_attr_temp3_crit.dev_attr.attr,
	&dev_attr_temp3_crit_enable.attr,
	&dev_attr_temp3_auto_point1_pwm.attr,
	&dev_attr_temp3_auto_point2_pwm.attr,
1627
	NULL
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
};

static const struct attribute_group adm1026_group_temp3 = {
	.attrs = adm1026_attributes_temp3,
};

static struct attribute *adm1026_attributes_in8_9[] = {
	&sensor_dev_attr_in8_input.dev_attr.attr,
	&sensor_dev_attr_in8_max.dev_attr.attr,
	&sensor_dev_attr_in8_min.dev_attr.attr,
	&sensor_dev_attr_in8_alarm.dev_attr.attr,
	&sensor_dev_attr_in9_input.dev_attr.attr,
	&sensor_dev_attr_in9_max.dev_attr.attr,
	&sensor_dev_attr_in9_min.dev_attr.attr,
	&sensor_dev_attr_in9_alarm.dev_attr.attr,
1643
	NULL
1644 1645 1646 1647 1648 1649
};

static const struct attribute_group adm1026_group_in8_9 = {
	.attrs = adm1026_attributes_in8_9,
};

1650 1651
static int adm1026_detect(struct i2c_adapter *adapter, int address,
			  int kind)
L
Linus Torvalds 已提交
1652 1653
{
	int company, verstep;
1654
	struct i2c_client *client;
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	struct adm1026_data *data;
	int err = 0;
	const char *type_name = "";

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
		/* We need to be able to do byte I/O */
		goto exit;
	};

	/* OK. For now, we presume we have a valid client. We now create the
	   client structure, even though we cannot fill it completely yet.
	   But it allows us to access adm1026_{read,write}_value. */

D
Deepak Saxena 已提交
1668
	if (!(data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
L
Linus Torvalds 已提交
1669 1670 1671 1672
		err = -ENOMEM;
		goto exit;
	}

1673 1674 1675 1676 1677
	client = &data->client;
	i2c_set_clientdata(client, data);
	client->addr = address;
	client->adapter = adapter;
	client->driver = &adm1026_driver;
L
Linus Torvalds 已提交
1678 1679 1680

	/* Now, we do the remaining detection. */

1681 1682
	company = adm1026_read_value(client, ADM1026_REG_COMPANY);
	verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
L
Linus Torvalds 已提交
1683

1684
	dev_dbg(&client->dev, "Detecting device at %d,0x%02x with"
L
Linus Torvalds 已提交
1685
		" COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1686
		i2c_adapter_id(client->adapter), client->addr,
L
Linus Torvalds 已提交
1687 1688 1689 1690
		company, verstep);

	/* If auto-detecting, Determine the chip type. */
	if (kind <= 0) {
1691
		dev_dbg(&client->dev, "Autodetecting device at %d,0x%02x "
L
Linus Torvalds 已提交
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
			"...\n", i2c_adapter_id(adapter), address);
		if (company == ADM1026_COMPANY_ANALOG_DEV
		    && verstep == ADM1026_VERSTEP_ADM1026) {
			kind = adm1026;
		} else if (company == ADM1026_COMPANY_ANALOG_DEV
			&& (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
			dev_err(&adapter->dev, ": Unrecognized stepping "
				"0x%02x. Defaulting to ADM1026.\n", verstep);
			kind = adm1026;
		} else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
			dev_err(&adapter->dev, ": Found version/stepping "
				"0x%02x. Assuming generic ADM1026.\n",
				verstep);
			kind = any_chip;
		} else {
1707
			dev_dbg(&client->dev, ": Autodetection "
L
Linus Torvalds 已提交
1708 1709
				"failed\n");
			/* Not an ADM1026 ... */
1710
			if (kind == 0) { /* User used force=x,y */
L
Linus Torvalds 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
				dev_err(&adapter->dev, "Generic ADM1026 not "
					"found at %d,0x%02x.  Try "
					"force_adm1026.\n",
					i2c_adapter_id(adapter), address);
			}
			goto exitfree;
		}
	}

	/* Fill in the chip specific driver values */
	switch (kind) {
	case any_chip :
		type_name = "adm1026";
		break;
	case adm1026 :
		type_name = "adm1026";
		break;
	default :
		dev_err(&adapter->dev, ": Internal error, invalid "
1730
			"kind (%d)!\n", kind);
L
Linus Torvalds 已提交
1731 1732 1733
		err = -EFAULT;
		goto exitfree;
	}
1734
	strlcpy(client->name, type_name, I2C_NAME_SIZE);
L
Linus Torvalds 已提交
1735 1736

	/* Fill in the remaining client fields */
1737
	mutex_init(&data->update_lock);
L
Linus Torvalds 已提交
1738 1739

	/* Tell the I2C layer a new client has arrived */
1740
	if ((err = i2c_attach_client(client)))
L
Linus Torvalds 已提交
1741 1742 1743
		goto exitfree;

	/* Set the VRM version */
1744
	data->vrm = vid_which_vrm();
L
Linus Torvalds 已提交
1745 1746

	/* Initialize the ADM1026 chip */
1747
	adm1026_init_client(client);
L
Linus Torvalds 已提交
1748 1749

	/* Register sysfs hooks */
1750
	if ((err = sysfs_create_group(&client->dev.kobj, &adm1026_group)))
1751
		goto exitdetach;
1752 1753 1754 1755 1756 1757 1758 1759
	if (data->config1 & CFG1_AIN8_9)
		err = sysfs_create_group(&client->dev.kobj,
					 &adm1026_group_in8_9);
	else
		err = sysfs_create_group(&client->dev.kobj,
					 &adm1026_group_temp3);
	if (err)
		goto exitremove;
1760

1761
	data->hwmon_dev = hwmon_device_register(&client->dev);
1762 1763
	if (IS_ERR(data->hwmon_dev)) {
		err = PTR_ERR(data->hwmon_dev);
1764
		goto exitremove;
1765 1766
	}

L
Linus Torvalds 已提交
1767 1768 1769
	return 0;

	/* Error out and cleanup code */
1770
exitremove:
1771
	sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1772 1773 1774 1775
	if (data->config1 & CFG1_AIN8_9)
		sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
	else
		sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1776
exitdetach:
1777
	i2c_detach_client(client);
L
Linus Torvalds 已提交
1778
exitfree:
1779
	kfree(data);
L
Linus Torvalds 已提交
1780 1781 1782
exit:
	return err;
}
1783 1784 1785 1786

static int adm1026_detach_client(struct i2c_client *client)
{
	struct adm1026_data *data = i2c_get_clientdata(client);
1787
	hwmon_device_unregister(data->hwmon_dev);
1788
	sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1789 1790 1791 1792
	if (data->config1 & CFG1_AIN8_9)
		sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
	else
		sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1793 1794 1795 1796 1797
	i2c_detach_client(client);
	kfree(data);
	return 0;
}

L
Linus Torvalds 已提交
1798 1799 1800 1801 1802
static int __init sm_adm1026_init(void)
{
	return i2c_add_driver(&adm1026_driver);
}

1803
static void __exit sm_adm1026_exit(void)
L
Linus Torvalds 已提交
1804 1805 1806 1807 1808 1809
{
	i2c_del_driver(&adm1026_driver);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1810
	      "Justin Thiessen <jthiessen@penguincomputing.com>");
L
Linus Torvalds 已提交
1811 1812 1813 1814
MODULE_DESCRIPTION("ADM1026 driver");

module_init(sm_adm1026_init);
module_exit(sm_adm1026_exit);