| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |   adm1031.c - Part of lm_sensors, Linux kernel modules for hardware | 
 | 3 |   monitoring | 
 | 4 |   Based on lm75.c and lm85.c | 
 | 5 |   Supports adm1030 / adm1031 | 
 | 6 |   Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org> | 
 | 7 |   Reworked by Jean Delvare <khali@linux-fr.org> | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 8 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 |   This program is free software; you can redistribute it and/or modify | 
 | 10 |   it under the terms of the GNU General Public License as published by | 
 | 11 |   the Free Software Foundation; either version 2 of the License, or | 
 | 12 |   (at your option) any later version. | 
 | 13 |  | 
 | 14 |   This program is distributed in the hope that it will be useful, | 
 | 15 |   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 16 |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 17 |   GNU General Public License for more details. | 
 | 18 |  | 
 | 19 |   You should have received a copy of the GNU General Public License | 
 | 20 |   along with this program; if not, write to the Free Software | 
 | 21 |   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 22 | */ | 
 | 23 |  | 
 | 24 | #include <linux/module.h> | 
 | 25 | #include <linux/init.h> | 
 | 26 | #include <linux/slab.h> | 
 | 27 | #include <linux/jiffies.h> | 
 | 28 | #include <linux/i2c.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 30 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 31 | #include <linux/err.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 |  | 
 | 34 | /* Following macros takes channel parameter starting from 0 to 2 */ | 
 | 35 | #define ADM1031_REG_FAN_SPEED(nr)	(0x08 + (nr)) | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 36 | #define ADM1031_REG_FAN_DIV(nr)		(0x20 + (nr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define ADM1031_REG_PWM			(0x22) | 
 | 38 | #define ADM1031_REG_FAN_MIN(nr)		(0x10 + (nr)) | 
 | 39 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 40 | #define ADM1031_REG_TEMP_MAX(nr)	(0x14 + 4 * (nr)) | 
 | 41 | #define ADM1031_REG_TEMP_MIN(nr)	(0x15 + 4 * (nr)) | 
 | 42 | #define ADM1031_REG_TEMP_CRIT(nr)	(0x16 + 4 * (nr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 44 | #define ADM1031_REG_TEMP(nr)		(0x0a + (nr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define ADM1031_REG_AUTO_TEMP(nr)	(0x24 + (nr)) | 
 | 46 |  | 
 | 47 | #define ADM1031_REG_STATUS(nr)		(0x2 + (nr)) | 
 | 48 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 49 | #define ADM1031_REG_CONF1		0x00 | 
 | 50 | #define ADM1031_REG_CONF2		0x01 | 
 | 51 | #define ADM1031_REG_EXT_TEMP		0x06 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 |  | 
 | 53 | #define ADM1031_CONF1_MONITOR_ENABLE	0x01	/* Monitoring enable */ | 
 | 54 | #define ADM1031_CONF1_PWM_INVERT	0x08	/* PWM Invert */ | 
 | 55 | #define ADM1031_CONF1_AUTO_MODE		0x80	/* Auto FAN */ | 
 | 56 |  | 
 | 57 | #define ADM1031_CONF2_PWM1_ENABLE	0x01 | 
 | 58 | #define ADM1031_CONF2_PWM2_ENABLE	0x02 | 
 | 59 | #define ADM1031_CONF2_TACH1_ENABLE	0x04 | 
 | 60 | #define ADM1031_CONF2_TACH2_ENABLE	0x08 | 
 | 61 | #define ADM1031_CONF2_TEMP_ENABLE(chan)	(0x10 << (chan)) | 
 | 62 |  | 
 | 63 | /* Addresses to scan */ | 
| Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 64 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
 | 66 | /* Insmod parameters */ | 
| Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 67 | I2C_CLIENT_INSMOD_2(adm1030, adm1031); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 |  | 
 | 69 | typedef u8 auto_chan_table_t[8][2]; | 
 | 70 |  | 
 | 71 | /* Each client has this additional data */ | 
 | 72 | struct adm1031_data { | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 73 | 	struct device *hwmon_dev; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 74 | 	struct mutex update_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 	int chip_type; | 
 | 76 | 	char valid;		/* !=0 if following fields are valid */ | 
 | 77 | 	unsigned long last_updated;	/* In jiffies */ | 
 | 78 | 	/* The chan_select_table contains the possible configurations for | 
 | 79 | 	 * auto fan control. | 
 | 80 | 	 */ | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 81 | 	const auto_chan_table_t *chan_select_table; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | 	u16 alarm; | 
 | 83 | 	u8 conf1; | 
 | 84 | 	u8 conf2; | 
 | 85 | 	u8 fan[2]; | 
 | 86 | 	u8 fan_div[2]; | 
 | 87 | 	u8 fan_min[2]; | 
 | 88 | 	u8 pwm[2]; | 
 | 89 | 	u8 old_pwm[2]; | 
 | 90 | 	s8 temp[3]; | 
 | 91 | 	u8 ext_temp[3]; | 
 | 92 | 	u8 auto_temp[3]; | 
 | 93 | 	u8 auto_temp_min[3]; | 
 | 94 | 	u8 auto_temp_off[3]; | 
 | 95 | 	u8 auto_temp_max[3]; | 
 | 96 | 	s8 temp_min[3]; | 
 | 97 | 	s8 temp_max[3]; | 
 | 98 | 	s8 temp_crit[3]; | 
 | 99 | }; | 
 | 100 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 101 | static int adm1031_probe(struct i2c_client *client, | 
 | 102 | 			 const struct i2c_device_id *id); | 
 | 103 | static int adm1031_detect(struct i2c_client *client, int kind, | 
 | 104 | 			  struct i2c_board_info *info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | static void adm1031_init_client(struct i2c_client *client); | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 106 | static int adm1031_remove(struct i2c_client *client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static struct adm1031_data *adm1031_update_device(struct device *dev); | 
 | 108 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 109 | static const struct i2c_device_id adm1031_id[] = { | 
 | 110 | 	{ "adm1030", adm1030 }, | 
 | 111 | 	{ "adm1031", adm1031 }, | 
 | 112 | 	{ } | 
 | 113 | }; | 
 | 114 | MODULE_DEVICE_TABLE(i2c, adm1031_id); | 
 | 115 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* This is the driver that will be inserted */ | 
 | 117 | static struct i2c_driver adm1031_driver = { | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 118 | 	.class		= I2C_CLASS_HWMON, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 119 | 	.driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 120 | 		.name = "adm1031", | 
 | 121 | 	}, | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 122 | 	.probe		= adm1031_probe, | 
 | 123 | 	.remove		= adm1031_remove, | 
 | 124 | 	.id_table	= adm1031_id, | 
 | 125 | 	.detect		= adm1031_detect, | 
 | 126 | 	.address_data	= &addr_data, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | }; | 
 | 128 |  | 
 | 129 | static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg) | 
 | 130 | { | 
 | 131 | 	return i2c_smbus_read_byte_data(client, reg); | 
 | 132 | } | 
 | 133 |  | 
 | 134 | static inline int | 
 | 135 | adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value) | 
 | 136 | { | 
 | 137 | 	return i2c_smbus_write_byte_data(client, reg, value); | 
 | 138 | } | 
 | 139 |  | 
 | 140 |  | 
 | 141 | #define TEMP_TO_REG(val)		(((val) < 0 ? ((val - 500) / 1000) : \ | 
 | 142 | 					((val + 500) / 1000))) | 
 | 143 |  | 
 | 144 | #define TEMP_FROM_REG(val)		((val) * 1000) | 
 | 145 |  | 
 | 146 | #define TEMP_FROM_REG_EXT(val, ext)	(TEMP_FROM_REG(val) + (ext) * 125) | 
 | 147 |  | 
 | 148 | #define FAN_FROM_REG(reg, div)		((reg) ? (11250 * 60) / ((reg) * (div)) : 0) | 
 | 149 |  | 
 | 150 | static int FAN_TO_REG(int reg, int div) | 
 | 151 | { | 
 | 152 | 	int tmp; | 
 | 153 | 	tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div); | 
 | 154 | 	return tmp > 255 ? 255 : tmp; | 
 | 155 | } | 
 | 156 |  | 
 | 157 | #define FAN_DIV_FROM_REG(reg)		(1<<(((reg)&0xc0)>>6)) | 
 | 158 |  | 
 | 159 | #define PWM_TO_REG(val)			(SENSORS_LIMIT((val), 0, 255) >> 4) | 
 | 160 | #define PWM_FROM_REG(val)		((val) << 4) | 
 | 161 |  | 
 | 162 | #define FAN_CHAN_FROM_REG(reg)		(((reg) >> 5) & 7) | 
 | 163 | #define FAN_CHAN_TO_REG(val, reg)	\ | 
 | 164 | 	(((reg) & 0x1F) | (((val) << 5) & 0xe0)) | 
 | 165 |  | 
 | 166 | #define AUTO_TEMP_MIN_TO_REG(val, reg)	\ | 
 | 167 | 	((((val)/500) & 0xf8)|((reg) & 0x7)) | 
 | 168 | #define AUTO_TEMP_RANGE_FROM_REG(reg)	(5000 * (1<< ((reg)&0x7))) | 
 | 169 | #define AUTO_TEMP_MIN_FROM_REG(reg)	(1000 * ((((reg) >> 3) & 0x1f) << 2)) | 
 | 170 |  | 
 | 171 | #define AUTO_TEMP_MIN_FROM_REG_DEG(reg)	((((reg) >> 3) & 0x1f) << 2) | 
 | 172 |  | 
 | 173 | #define AUTO_TEMP_OFF_FROM_REG(reg)		\ | 
 | 174 | 	(AUTO_TEMP_MIN_FROM_REG(reg) - 5000) | 
 | 175 |  | 
 | 176 | #define AUTO_TEMP_MAX_FROM_REG(reg)		\ | 
 | 177 | 	(AUTO_TEMP_RANGE_FROM_REG(reg) +	\ | 
 | 178 | 	AUTO_TEMP_MIN_FROM_REG(reg)) | 
 | 179 |  | 
 | 180 | static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm) | 
 | 181 | { | 
 | 182 | 	int ret; | 
 | 183 | 	int range = val - AUTO_TEMP_MIN_FROM_REG(reg); | 
 | 184 |  | 
 | 185 | 	range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm); | 
 | 186 | 	ret = ((reg & 0xf8) | | 
 | 187 | 	       (range < 10000 ? 0 : | 
 | 188 | 		range < 20000 ? 1 : | 
 | 189 | 		range < 40000 ? 2 : range < 80000 ? 3 : 4)); | 
 | 190 | 	return ret; | 
 | 191 | } | 
 | 192 |  | 
 | 193 | /* FAN auto control */ | 
 | 194 | #define GET_FAN_AUTO_BITFIELD(data, idx)	\ | 
 | 195 | 	(*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2] | 
 | 196 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 197 | /* The tables below contains the possible values for the auto fan | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 |  * control bitfields. the index in the table is the register value. | 
 | 199 |  * MSb is the auto fan control enable bit, so the four first entries | 
 | 200 |  * in the table disables auto fan control when both bitfields are zero. | 
 | 201 |  */ | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 202 | static const auto_chan_table_t auto_channel_select_table_adm1031 = { | 
 | 203 | 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, | 
 | 204 | 	{ 2 /* 0b010 */ , 4 /* 0b100 */ }, | 
 | 205 | 	{ 2 /* 0b010 */ , 2 /* 0b010 */ }, | 
 | 206 | 	{ 4 /* 0b100 */ , 4 /* 0b100 */ }, | 
 | 207 | 	{ 7 /* 0b111 */ , 7 /* 0b111 */ }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | }; | 
 | 209 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 210 | static const auto_chan_table_t auto_channel_select_table_adm1030 = { | 
 | 211 | 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, | 
 | 212 | 	{ 2 /* 0b10 */		, 0 }, | 
 | 213 | 	{ 0xff /* invalid */	, 0 }, | 
 | 214 | 	{ 0xff /* invalid */	, 0 }, | 
 | 215 | 	{ 3 /* 0b11 */		, 0 }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | }; | 
 | 217 |  | 
 | 218 | /* That function checks if a bitfield is valid and returns the other bitfield | 
 | 219 |  * nearest match if no exact match where found. | 
 | 220 |  */ | 
 | 221 | static int | 
 | 222 | get_fan_auto_nearest(struct adm1031_data *data, | 
 | 223 | 		     int chan, u8 val, u8 reg, u8 * new_reg) | 
 | 224 | { | 
 | 225 | 	int i; | 
 | 226 | 	int first_match = -1, exact_match = -1; | 
 | 227 | 	u8 other_reg_val = | 
 | 228 | 	    (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1]; | 
 | 229 |  | 
 | 230 | 	if (val == 0) { | 
 | 231 | 		*new_reg = 0; | 
 | 232 | 		return 0; | 
 | 233 | 	} | 
 | 234 |  | 
 | 235 | 	for (i = 0; i < 8; i++) { | 
 | 236 | 		if ((val == (*data->chan_select_table)[i][chan]) && | 
 | 237 | 		    ((*data->chan_select_table)[i][chan ? 0 : 1] == | 
 | 238 | 		     other_reg_val)) { | 
 | 239 | 			/* We found an exact match */ | 
 | 240 | 			exact_match = i; | 
 | 241 | 			break; | 
 | 242 | 		} else if (val == (*data->chan_select_table)[i][chan] && | 
 | 243 | 			   first_match == -1) { | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 244 | 			/* Save the first match in case of an exact match has | 
 | 245 | 			 * not been found | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | 			 */ | 
 | 247 | 			first_match = i; | 
 | 248 | 		} | 
 | 249 | 	} | 
 | 250 |  | 
 | 251 | 	if (exact_match >= 0) { | 
 | 252 | 		*new_reg = exact_match; | 
 | 253 | 	} else if (first_match >= 0) { | 
 | 254 | 		*new_reg = first_match; | 
 | 255 | 	} else { | 
 | 256 | 		return -EINVAL; | 
 | 257 | 	} | 
 | 258 | 	return 0; | 
 | 259 | } | 
 | 260 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 261 | static ssize_t show_fan_auto_channel(struct device *dev, | 
 | 262 | 				     struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 264 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 266 | 	return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr)); | 
 | 267 | } | 
 | 268 |  | 
 | 269 | static ssize_t | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 270 | set_fan_auto_channel(struct device *dev, struct device_attribute *attr, | 
 | 271 | 		     const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { | 
 | 273 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 274 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 275 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | 	int val = simple_strtol(buf, NULL, 10); | 
 | 277 | 	u8 reg; | 
 | 278 | 	int ret; | 
 | 279 | 	u8 old_fan_mode; | 
 | 280 |  | 
 | 281 | 	old_fan_mode = data->conf1; | 
 | 282 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 283 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 284 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | 	if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, ®))) { | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 286 | 		mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | 		return ret; | 
 | 288 | 	} | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 289 | 	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1); | 
 | 290 | 	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | 	    (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) { | 
 | 292 | 		if (data->conf1 & ADM1031_CONF1_AUTO_MODE){ | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 293 | 			/* Switch to Auto Fan Mode | 
 | 294 | 			 * Save PWM registers | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | 			 * Set PWM registers to 33% Both */ | 
 | 296 | 			data->old_pwm[0] = data->pwm[0]; | 
 | 297 | 			data->old_pwm[1] = data->pwm[1]; | 
 | 298 | 			adm1031_write_value(client, ADM1031_REG_PWM, 0x55); | 
 | 299 | 		} else { | 
 | 300 | 			/* Switch to Manual Mode */ | 
 | 301 | 			data->pwm[0] = data->old_pwm[0]; | 
 | 302 | 			data->pwm[1] = data->old_pwm[1]; | 
 | 303 | 			/* Restore PWM registers */ | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 304 | 			adm1031_write_value(client, ADM1031_REG_PWM, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | 					    data->pwm[0] | (data->pwm[1] << 4)); | 
 | 306 | 		} | 
 | 307 | 	} | 
 | 308 | 	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1); | 
 | 309 | 	adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 310 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | 	return count; | 
 | 312 | } | 
 | 313 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 314 | static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR, | 
 | 315 | 		show_fan_auto_channel, set_fan_auto_channel, 0); | 
 | 316 | static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR, | 
 | 317 | 		show_fan_auto_channel, set_fan_auto_channel, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 |  | 
 | 319 | /* Auto Temps */ | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 320 | static ssize_t show_auto_temp_off(struct device *dev, | 
 | 321 | 				  struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 323 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 325 | 	return sprintf(buf, "%d\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | 		       AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr])); | 
 | 327 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 328 | static ssize_t show_auto_temp_min(struct device *dev, | 
 | 329 | 				  struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 331 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 333 | 	return sprintf(buf, "%d\n", | 
 | 334 | 		       AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr])); | 
 | 335 | } | 
 | 336 | static ssize_t | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 337 | set_auto_temp_min(struct device *dev, struct device_attribute *attr, | 
 | 338 | 		  const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { | 
 | 340 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 341 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 342 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | 	int val = simple_strtol(buf, NULL, 10); | 
 | 344 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 345 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | 	data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]); | 
 | 347 | 	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr), | 
 | 348 | 			    data->auto_temp[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 349 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | 	return count; | 
 | 351 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 352 | static ssize_t show_auto_temp_max(struct device *dev, | 
 | 353 | 				  struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 355 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 357 | 	return sprintf(buf, "%d\n", | 
 | 358 | 		       AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr])); | 
 | 359 | } | 
 | 360 | static ssize_t | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 361 | set_auto_temp_max(struct device *dev, struct device_attribute *attr, | 
 | 362 | 		  const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { | 
 | 364 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 365 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 366 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | 	int val = simple_strtol(buf, NULL, 10); | 
 | 368 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 369 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | 	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]); | 
 | 371 | 	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr), | 
 | 372 | 			    data->temp_max[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 373 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | 	return count; | 
 | 375 | } | 
 | 376 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 377 | #define auto_temp_reg(offset)						\ | 
 | 378 | static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO,		\ | 
 | 379 | 		show_auto_temp_off, NULL, offset - 1);			\ | 
 | 380 | static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR,	\ | 
 | 381 | 		show_auto_temp_min, set_auto_temp_min, offset - 1);	\ | 
 | 382 | static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR,	\ | 
 | 383 | 		show_auto_temp_max, set_auto_temp_max, offset - 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 |  | 
 | 385 | auto_temp_reg(1); | 
 | 386 | auto_temp_reg(2); | 
 | 387 | auto_temp_reg(3); | 
 | 388 |  | 
 | 389 | /* pwm */ | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 390 | static ssize_t show_pwm(struct device *dev, | 
 | 391 | 			struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 393 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 395 | 	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); | 
 | 396 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 397 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, | 
 | 398 | 		       const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { | 
 | 400 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 401 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 402 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | 	int val = simple_strtol(buf, NULL, 10); | 
 | 404 | 	int reg; | 
 | 405 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 406 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 407 | 	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | 	    (((val>>4) & 0xf) != 5)) { | 
 | 409 | 		/* In automatic mode, the only PWM accepted is 33% */ | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 410 | 		mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | 		return -EINVAL; | 
 | 412 | 	} | 
 | 413 | 	data->pwm[nr] = PWM_TO_REG(val); | 
 | 414 | 	reg = adm1031_read_value(client, ADM1031_REG_PWM); | 
 | 415 | 	adm1031_write_value(client, ADM1031_REG_PWM, | 
 | 416 | 			    nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf) | 
 | 417 | 			    : (data->pwm[nr] & 0xf) | (reg & 0xf0)); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 418 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | 	return count; | 
 | 420 | } | 
 | 421 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 422 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0); | 
 | 423 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1); | 
 | 424 | static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR, | 
 | 425 | 		show_pwm, set_pwm, 0); | 
 | 426 | static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR, | 
 | 427 | 		show_pwm, set_pwm, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 |  | 
 | 429 | /* Fans */ | 
 | 430 |  | 
 | 431 | /* | 
 | 432 |  * That function checks the cases where the fan reading is not | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 433 |  * relevant.  It is used to provide 0 as fan reading when the fan is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 |  * not supposed to run | 
 | 435 |  */ | 
 | 436 | static int trust_fan_readings(struct adm1031_data *data, int chan) | 
 | 437 | { | 
 | 438 | 	int res = 0; | 
 | 439 |  | 
 | 440 | 	if (data->conf1 & ADM1031_CONF1_AUTO_MODE) { | 
 | 441 | 		switch (data->conf1 & 0x60) { | 
 | 442 | 		case 0x00:	/* remote temp1 controls fan1 remote temp2 controls fan2 */ | 
 | 443 | 			res = data->temp[chan+1] >= | 
 | 444 | 			      AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]); | 
 | 445 | 			break; | 
 | 446 | 		case 0x20:	/* remote temp1 controls both fans */ | 
 | 447 | 			res = | 
 | 448 | 			    data->temp[1] >= | 
 | 449 | 			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]); | 
 | 450 | 			break; | 
 | 451 | 		case 0x40:	/* remote temp2 controls both fans */ | 
 | 452 | 			res = | 
 | 453 | 			    data->temp[2] >= | 
 | 454 | 			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]); | 
 | 455 | 			break; | 
 | 456 | 		case 0x60:	/* max controls both fans */ | 
 | 457 | 			res = | 
 | 458 | 			    data->temp[0] >= | 
 | 459 | 			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0]) | 
 | 460 | 			    || data->temp[1] >= | 
 | 461 | 			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]) | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 462 | 			    || (data->chip_type == adm1031 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | 				&& data->temp[2] >= | 
 | 464 | 				AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2])); | 
 | 465 | 			break; | 
 | 466 | 		} | 
 | 467 | 	} else { | 
 | 468 | 		res = data->pwm[chan] > 0; | 
 | 469 | 	} | 
 | 470 | 	return res; | 
 | 471 | } | 
 | 472 |  | 
 | 473 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 474 | static ssize_t show_fan(struct device *dev, | 
 | 475 | 			struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 477 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 479 | 	int value; | 
 | 480 |  | 
 | 481 | 	value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr], | 
 | 482 | 				 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0; | 
 | 483 | 	return sprintf(buf, "%d\n", value); | 
 | 484 | } | 
 | 485 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 486 | static ssize_t show_fan_div(struct device *dev, | 
 | 487 | 			    struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 489 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 491 | 	return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr])); | 
 | 492 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 493 | static ssize_t show_fan_min(struct device *dev, | 
 | 494 | 			    struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 496 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 498 | 	return sprintf(buf, "%d\n", | 
 | 499 | 		       FAN_FROM_REG(data->fan_min[nr], | 
 | 500 | 				    FAN_DIV_FROM_REG(data->fan_div[nr]))); | 
 | 501 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 502 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, | 
 | 503 | 			   const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { | 
 | 505 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 506 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 507 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | 	int val = simple_strtol(buf, NULL, 10); | 
 | 509 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 510 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | 	if (val) { | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 512 | 		data->fan_min[nr] = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | 			FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr])); | 
 | 514 | 	} else { | 
 | 515 | 		data->fan_min[nr] = 0xff; | 
 | 516 | 	} | 
 | 517 | 	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 518 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | 	return count; | 
 | 520 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 521 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, | 
 | 522 | 			   const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { | 
 | 524 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 525 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 526 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | 	int val = simple_strtol(buf, NULL, 10); | 
 | 528 | 	u8 tmp; | 
 | 529 | 	int old_div; | 
 | 530 | 	int new_min; | 
 | 531 |  | 
 | 532 | 	tmp = val == 8 ? 0xc0 : | 
 | 533 | 	      val == 4 ? 0x80 : | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 534 | 	      val == 2 ? 0x40 : | 
 | 535 | 	      val == 1 ? 0x00 : | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | 	      0xff; | 
 | 537 | 	if (tmp == 0xff) | 
 | 538 | 		return -EINVAL; | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 539 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 540 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 38a1f0e | 2007-12-02 23:32:42 +0100 | [diff] [blame] | 541 | 	/* Get fresh readings */ | 
 | 542 | 	data->fan_div[nr] = adm1031_read_value(client, | 
 | 543 | 					       ADM1031_REG_FAN_DIV(nr)); | 
 | 544 | 	data->fan_min[nr] = adm1031_read_value(client, | 
 | 545 | 					       ADM1031_REG_FAN_MIN(nr)); | 
 | 546 |  | 
 | 547 | 	/* Write the new clock divider and fan min */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | 	old_div = FAN_DIV_FROM_REG(data->fan_div[nr]); | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 549 | 	data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]); | 
 | 550 | 	new_min = data->fan_min[nr] * old_div / val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | 	data->fan_min[nr] = new_min > 0xff ? 0xff : new_min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 553 | 	adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | 			    data->fan_div[nr]); | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 555 | 	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | 			    data->fan_min[nr]); | 
| Jean Delvare | 38a1f0e | 2007-12-02 23:32:42 +0100 | [diff] [blame] | 557 |  | 
 | 558 | 	/* Invalidate the cache: fan speed is no longer valid */ | 
 | 559 | 	data->valid = 0; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 560 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | 	return count; | 
 | 562 | } | 
 | 563 |  | 
 | 564 | #define fan_offset(offset)						\ | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 565 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,			\ | 
 | 566 | 		show_fan, NULL, offset - 1);				\ | 
 | 567 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\ | 
 | 568 | 		show_fan_min, set_fan_min, offset - 1);			\ | 
 | 569 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,		\ | 
 | 570 | 		show_fan_div, set_fan_div, offset - 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 |  | 
 | 572 | fan_offset(1); | 
 | 573 | fan_offset(2); | 
 | 574 |  | 
 | 575 |  | 
 | 576 | /* Temps */ | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 577 | static ssize_t show_temp(struct device *dev, | 
 | 578 | 			 struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 580 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 582 | 	int ext; | 
 | 583 | 	ext = nr == 0 ? | 
 | 584 | 	    ((data->ext_temp[nr] >> 6) & 0x3) * 2 : | 
 | 585 | 	    (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7)); | 
 | 586 | 	return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext)); | 
 | 587 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 588 | static ssize_t show_temp_min(struct device *dev, | 
 | 589 | 			     struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 591 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 593 | 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); | 
 | 594 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 595 | static ssize_t show_temp_max(struct device *dev, | 
 | 596 | 			     struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 598 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 600 | 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); | 
 | 601 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 602 | static ssize_t show_temp_crit(struct device *dev, | 
 | 603 | 			      struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 605 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 607 | 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr])); | 
 | 608 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 609 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, | 
 | 610 | 			    const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { | 
 | 612 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 613 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 614 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | 	int val; | 
 | 616 |  | 
 | 617 | 	val = simple_strtol(buf, NULL, 10); | 
 | 618 | 	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 619 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | 	data->temp_min[nr] = TEMP_TO_REG(val); | 
 | 621 | 	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr), | 
 | 622 | 			    data->temp_min[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 623 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | 	return count; | 
 | 625 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 626 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | 
 | 627 | 			    const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { | 
 | 629 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 630 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 631 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | 	int val; | 
 | 633 |  | 
 | 634 | 	val = simple_strtol(buf, NULL, 10); | 
 | 635 | 	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 636 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | 	data->temp_max[nr] = TEMP_TO_REG(val); | 
 | 638 | 	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr), | 
 | 639 | 			    data->temp_max[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 640 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | 	return count; | 
 | 642 | } | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 643 | static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, | 
 | 644 | 			     const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { | 
 | 646 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 647 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 648 | 	int nr = to_sensor_dev_attr(attr)->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | 	int val; | 
 | 650 |  | 
 | 651 | 	val = simple_strtol(buf, NULL, 10); | 
 | 652 | 	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 653 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | 	data->temp_crit[nr] = TEMP_TO_REG(val); | 
 | 655 | 	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr), | 
 | 656 | 			    data->temp_crit[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 657 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | 	return count; | 
 | 659 | } | 
 | 660 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 661 | #define temp_reg(offset)						\ | 
 | 662 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,		\ | 
 | 663 | 		show_temp, NULL, offset - 1);				\ | 
 | 664 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\ | 
 | 665 | 		show_temp_min, set_temp_min, offset - 1);		\ | 
 | 666 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\ | 
 | 667 | 		show_temp_max, set_temp_max, offset - 1);		\ | 
 | 668 | static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,	\ | 
 | 669 | 		show_temp_crit, set_temp_crit, offset - 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 |  | 
 | 671 | temp_reg(1); | 
 | 672 | temp_reg(2); | 
 | 673 | temp_reg(3); | 
 | 674 |  | 
 | 675 | /* Alarms */ | 
| Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 676 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { | 
 | 678 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 679 | 	return sprintf(buf, "%d\n", data->alarm); | 
 | 680 | } | 
 | 681 |  | 
 | 682 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
 | 683 |  | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 684 | static ssize_t show_alarm(struct device *dev, | 
 | 685 | 			  struct device_attribute *attr, char *buf) | 
 | 686 | { | 
 | 687 | 	int bitnr = to_sensor_dev_attr(attr)->index; | 
 | 688 | 	struct adm1031_data *data = adm1031_update_device(dev); | 
 | 689 | 	return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1); | 
 | 690 | } | 
 | 691 |  | 
 | 692 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0); | 
 | 693 | static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1); | 
 | 694 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2); | 
 | 695 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | 
 | 696 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4); | 
 | 697 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5); | 
 | 698 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | 
 | 699 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7); | 
 | 700 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8); | 
 | 701 | static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9); | 
 | 702 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10); | 
 | 703 | static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11); | 
 | 704 | static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12); | 
 | 705 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13); | 
 | 706 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 |  | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 708 | static struct attribute *adm1031_attributes[] = { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 709 | 	&sensor_dev_attr_fan1_input.dev_attr.attr, | 
 | 710 | 	&sensor_dev_attr_fan1_div.dev_attr.attr, | 
 | 711 | 	&sensor_dev_attr_fan1_min.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 712 | 	&sensor_dev_attr_fan1_alarm.dev_attr.attr, | 
 | 713 | 	&sensor_dev_attr_fan1_fault.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 714 | 	&sensor_dev_attr_pwm1.dev_attr.attr, | 
 | 715 | 	&sensor_dev_attr_auto_fan1_channel.dev_attr.attr, | 
 | 716 | 	&sensor_dev_attr_temp1_input.dev_attr.attr, | 
 | 717 | 	&sensor_dev_attr_temp1_min.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 718 | 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 719 | 	&sensor_dev_attr_temp1_max.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 720 | 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 721 | 	&sensor_dev_attr_temp1_crit.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 722 | 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 723 | 	&sensor_dev_attr_temp2_input.dev_attr.attr, | 
 | 724 | 	&sensor_dev_attr_temp2_min.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 725 | 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 726 | 	&sensor_dev_attr_temp2_max.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 727 | 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 728 | 	&sensor_dev_attr_temp2_crit.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 729 | 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | 
 | 730 | 	&sensor_dev_attr_temp2_fault.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 731 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 732 | 	&sensor_dev_attr_auto_temp1_off.dev_attr.attr, | 
 | 733 | 	&sensor_dev_attr_auto_temp1_min.dev_attr.attr, | 
 | 734 | 	&sensor_dev_attr_auto_temp1_max.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 735 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 736 | 	&sensor_dev_attr_auto_temp2_off.dev_attr.attr, | 
 | 737 | 	&sensor_dev_attr_auto_temp2_min.dev_attr.attr, | 
 | 738 | 	&sensor_dev_attr_auto_temp2_max.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 739 |  | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 740 | 	&sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 741 |  | 
 | 742 | 	&dev_attr_alarms.attr, | 
 | 743 |  | 
 | 744 | 	NULL | 
 | 745 | }; | 
 | 746 |  | 
 | 747 | static const struct attribute_group adm1031_group = { | 
 | 748 | 	.attrs = adm1031_attributes, | 
 | 749 | }; | 
 | 750 |  | 
 | 751 | static struct attribute *adm1031_attributes_opt[] = { | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 752 | 	&sensor_dev_attr_fan2_input.dev_attr.attr, | 
 | 753 | 	&sensor_dev_attr_fan2_div.dev_attr.attr, | 
 | 754 | 	&sensor_dev_attr_fan2_min.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 755 | 	&sensor_dev_attr_fan2_alarm.dev_attr.attr, | 
 | 756 | 	&sensor_dev_attr_fan2_fault.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 757 | 	&sensor_dev_attr_pwm2.dev_attr.attr, | 
 | 758 | 	&sensor_dev_attr_auto_fan2_channel.dev_attr.attr, | 
 | 759 | 	&sensor_dev_attr_temp3_input.dev_attr.attr, | 
 | 760 | 	&sensor_dev_attr_temp3_min.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 761 | 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 762 | 	&sensor_dev_attr_temp3_max.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 763 | 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 764 | 	&sensor_dev_attr_temp3_crit.dev_attr.attr, | 
| Jean Delvare | 050ab87 | 2007-12-02 23:42:24 +0100 | [diff] [blame] | 765 | 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | 
 | 766 | 	&sensor_dev_attr_temp3_fault.dev_attr.attr, | 
| Jean Delvare | c801082 | 2007-12-02 23:39:38 +0100 | [diff] [blame] | 767 | 	&sensor_dev_attr_auto_temp3_off.dev_attr.attr, | 
 | 768 | 	&sensor_dev_attr_auto_temp3_min.dev_attr.attr, | 
 | 769 | 	&sensor_dev_attr_auto_temp3_max.dev_attr.attr, | 
 | 770 | 	&sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 771 | 	NULL | 
 | 772 | }; | 
 | 773 |  | 
 | 774 | static const struct attribute_group adm1031_group_opt = { | 
 | 775 | 	.attrs = adm1031_attributes_opt, | 
 | 776 | }; | 
 | 777 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 778 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
 | 779 | static int adm1031_detect(struct i2c_client *client, int kind, | 
 | 780 | 			  struct i2c_board_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 782 | 	struct i2c_adapter *adapter = client->adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | 	const char *name = ""; | 
 | 784 |  | 
 | 785 | 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 786 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 |  | 
 | 788 | 	if (kind < 0) { | 
 | 789 | 		int id, co; | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 790 | 		id = i2c_smbus_read_byte_data(client, 0x3d); | 
 | 791 | 		co = i2c_smbus_read_byte_data(client, 0x3e); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 |  | 
 | 793 | 		if (!((id == 0x31 || id == 0x30) && co == 0x41)) | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 794 | 			return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | 		kind = (id == 0x30) ? adm1030 : adm1031; | 
 | 796 | 	} | 
 | 797 |  | 
 | 798 | 	if (kind <= 0) | 
 | 799 | 		kind = adm1031; | 
 | 800 |  | 
 | 801 | 	/* Given the detected chip type, set the chip name and the | 
 | 802 | 	 * auto fan control helper table. */ | 
 | 803 | 	if (kind == adm1030) { | 
 | 804 | 		name = "adm1030"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | 	} else if (kind == adm1031) { | 
 | 806 | 		name = "adm1031"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | 	} | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 808 | 	strlcpy(info->type, name, I2C_NAME_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 810 | 	return 0; | 
 | 811 | } | 
 | 812 |  | 
 | 813 | static int adm1031_probe(struct i2c_client *client, | 
 | 814 | 			 const struct i2c_device_id *id) | 
 | 815 | { | 
 | 816 | 	struct adm1031_data *data; | 
 | 817 | 	int err; | 
 | 818 |  | 
 | 819 | 	data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL); | 
 | 820 | 	if (!data) { | 
 | 821 | 		err = -ENOMEM; | 
 | 822 | 		goto exit; | 
 | 823 | 	} | 
 | 824 |  | 
 | 825 | 	i2c_set_clientdata(client, data); | 
 | 826 | 	data->chip_type = id->driver_data; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 827 | 	mutex_init(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 829 | 	if (data->chip_type == adm1030) | 
 | 830 | 		data->chan_select_table = &auto_channel_select_table_adm1030; | 
 | 831 | 	else | 
 | 832 | 		data->chan_select_table = &auto_channel_select_table_adm1031; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 |  | 
 | 834 | 	/* Initialize the ADM1031 chip */ | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 835 | 	adm1031_init_client(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 |  | 
 | 837 | 	/* Register sysfs hooks */ | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 838 | 	if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group))) | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 839 | 		goto exit_free; | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 840 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 841 | 	if (data->chip_type == adm1031) { | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 842 | 		if ((err = sysfs_create_group(&client->dev.kobj, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 843 | 						&adm1031_group_opt))) | 
 | 844 | 			goto exit_remove; | 
 | 845 | 	} | 
 | 846 |  | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 847 | 	data->hwmon_dev = hwmon_device_register(&client->dev); | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 848 | 	if (IS_ERR(data->hwmon_dev)) { | 
 | 849 | 		err = PTR_ERR(data->hwmon_dev); | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 850 | 		goto exit_remove; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | 	} | 
 | 852 |  | 
 | 853 | 	return 0; | 
 | 854 |  | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 855 | exit_remove: | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 856 | 	sysfs_remove_group(&client->dev.kobj, &adm1031_group); | 
 | 857 | 	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | exit_free: | 
| Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 859 | 	kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | exit: | 
 | 861 | 	return err; | 
 | 862 | } | 
 | 863 |  | 
| Jean Delvare | af200f8 | 2008-07-16 19:30:09 +0200 | [diff] [blame] | 864 | static int adm1031_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 866 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 867 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 868 | 	hwmon_device_unregister(data->hwmon_dev); | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 869 | 	sysfs_remove_group(&client->dev.kobj, &adm1031_group); | 
 | 870 | 	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 871 | 	kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | 	return 0; | 
 | 873 | } | 
 | 874 |  | 
 | 875 | static void adm1031_init_client(struct i2c_client *client) | 
 | 876 | { | 
 | 877 | 	unsigned int read_val; | 
 | 878 | 	unsigned int mask; | 
 | 879 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
 | 880 |  | 
 | 881 | 	mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE); | 
 | 882 | 	if (data->chip_type == adm1031) { | 
 | 883 | 		mask |= (ADM1031_CONF2_PWM2_ENABLE | | 
 | 884 | 			ADM1031_CONF2_TACH2_ENABLE); | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 885 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | 	/* Initialize the ADM1031 chip (enables fan speed reading ) */ | 
 | 887 | 	read_val = adm1031_read_value(client, ADM1031_REG_CONF2); | 
 | 888 | 	if ((read_val | mask) != read_val) { | 
 | 889 | 	    adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask); | 
 | 890 | 	} | 
 | 891 |  | 
 | 892 | 	read_val = adm1031_read_value(client, ADM1031_REG_CONF1); | 
 | 893 | 	if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) { | 
 | 894 | 	    adm1031_write_value(client, ADM1031_REG_CONF1, read_val | | 
 | 895 | 				ADM1031_CONF1_MONITOR_ENABLE); | 
 | 896 | 	} | 
 | 897 |  | 
 | 898 | } | 
 | 899 |  | 
 | 900 | static struct adm1031_data *adm1031_update_device(struct device *dev) | 
 | 901 | { | 
 | 902 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 903 | 	struct adm1031_data *data = i2c_get_clientdata(client); | 
 | 904 | 	int chan; | 
 | 905 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 906 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 |  | 
 | 908 | 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | 
 | 909 | 	    || !data->valid) { | 
 | 910 |  | 
 | 911 | 		dev_dbg(&client->dev, "Starting adm1031 update\n"); | 
 | 912 | 		for (chan = 0; | 
 | 913 | 		     chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) { | 
 | 914 | 			u8 oldh, newh; | 
 | 915 |  | 
 | 916 | 			oldh = | 
 | 917 | 			    adm1031_read_value(client, ADM1031_REG_TEMP(chan)); | 
 | 918 | 			data->ext_temp[chan] = | 
 | 919 | 			    adm1031_read_value(client, ADM1031_REG_EXT_TEMP); | 
 | 920 | 			newh = | 
 | 921 | 			    adm1031_read_value(client, ADM1031_REG_TEMP(chan)); | 
 | 922 | 			if (newh != oldh) { | 
 | 923 | 				data->ext_temp[chan] = | 
 | 924 | 				    adm1031_read_value(client, | 
 | 925 | 						       ADM1031_REG_EXT_TEMP); | 
 | 926 | #ifdef DEBUG | 
 | 927 | 				oldh = | 
 | 928 | 				    adm1031_read_value(client, | 
 | 929 | 						       ADM1031_REG_TEMP(chan)); | 
 | 930 |  | 
 | 931 | 				/* oldh is actually newer */ | 
 | 932 | 				if (newh != oldh) | 
 | 933 | 					dev_warn(&client->dev, | 
 | 934 | 						 "Remote temperature may be " | 
 | 935 | 						 "wrong.\n"); | 
 | 936 | #endif | 
 | 937 | 			} | 
 | 938 | 			data->temp[chan] = newh; | 
 | 939 |  | 
 | 940 | 			data->temp_min[chan] = | 
 | 941 | 			    adm1031_read_value(client, | 
 | 942 | 					       ADM1031_REG_TEMP_MIN(chan)); | 
 | 943 | 			data->temp_max[chan] = | 
 | 944 | 			    adm1031_read_value(client, | 
 | 945 | 					       ADM1031_REG_TEMP_MAX(chan)); | 
 | 946 | 			data->temp_crit[chan] = | 
 | 947 | 			    adm1031_read_value(client, | 
 | 948 | 					       ADM1031_REG_TEMP_CRIT(chan)); | 
 | 949 | 			data->auto_temp[chan] = | 
 | 950 | 			    adm1031_read_value(client, | 
 | 951 | 					       ADM1031_REG_AUTO_TEMP(chan)); | 
 | 952 |  | 
 | 953 | 		} | 
 | 954 |  | 
 | 955 | 		data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1); | 
 | 956 | 		data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2); | 
 | 957 |  | 
 | 958 | 		data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0)) | 
 | 959 | 			     | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) | 
 | 960 | 				<< 8); | 
 | 961 | 		if (data->chip_type == adm1030) { | 
 | 962 | 			data->alarm &= 0xc0ff; | 
 | 963 | 		} | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 964 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | 		for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) { | 
 | 966 | 			data->fan_div[chan] = | 
 | 967 | 			    adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan)); | 
 | 968 | 			data->fan_min[chan] = | 
 | 969 | 			    adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan)); | 
 | 970 | 			data->fan[chan] = | 
 | 971 | 			    adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan)); | 
 | 972 | 			data->pwm[chan] = | 
| Jean Delvare | 6d6006b | 2007-12-02 23:33:57 +0100 | [diff] [blame] | 973 | 			    0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | 				   (4*chan)); | 
 | 975 | 		} | 
 | 976 | 		data->last_updated = jiffies; | 
 | 977 | 		data->valid = 1; | 
 | 978 | 	} | 
 | 979 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 980 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 |  | 
 | 982 | 	return data; | 
 | 983 | } | 
 | 984 |  | 
 | 985 | static int __init sensors_adm1031_init(void) | 
 | 986 | { | 
 | 987 | 	return i2c_add_driver(&adm1031_driver); | 
 | 988 | } | 
 | 989 |  | 
 | 990 | static void __exit sensors_adm1031_exit(void) | 
 | 991 | { | 
 | 992 | 	i2c_del_driver(&adm1031_driver); | 
 | 993 | } | 
 | 994 |  | 
 | 995 | MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>"); | 
 | 996 | MODULE_DESCRIPTION("ADM1031/ADM1030 driver"); | 
 | 997 | MODULE_LICENSE("GPL"); | 
 | 998 |  | 
 | 999 | module_init(sensors_adm1031_init); | 
 | 1000 | module_exit(sensors_adm1031_exit); |