| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * A hwmon driver for the Analog Devices ADT7473 | 
|  | 3 | * Copyright (C) 2007 IBM | 
|  | 4 | * | 
|  | 5 | * Author: Darrick J. Wong <djwong@us.ibm.com> | 
|  | 6 | * | 
|  | 7 | * This program is free software; you can redistribute it and/or modify | 
|  | 8 | * it under the terms of the GNU General Public License as published by | 
|  | 9 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 10 | * (at your option) any later version. | 
|  | 11 | * | 
|  | 12 | * This program is distributed in the hope that it will be useful, | 
|  | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 15 | * GNU General Public License for more details. | 
|  | 16 | * | 
|  | 17 | * You should have received a copy of the GNU General Public License | 
|  | 18 | * along with this program; if not, write to the Free Software | 
|  | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|  | 20 | */ | 
|  | 21 |  | 
|  | 22 | #include <linux/module.h> | 
|  | 23 | #include <linux/jiffies.h> | 
|  | 24 | #include <linux/i2c.h> | 
|  | 25 | #include <linux/hwmon.h> | 
|  | 26 | #include <linux/hwmon-sysfs.h> | 
|  | 27 | #include <linux/err.h> | 
|  | 28 | #include <linux/mutex.h> | 
|  | 29 | #include <linux/delay.h> | 
|  | 30 | #include <linux/log2.h> | 
|  | 31 |  | 
|  | 32 | /* Addresses to scan */ | 
| Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 33 | static const unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END }; | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | /* Insmod parameters */ | 
|  | 36 | I2C_CLIENT_INSMOD_1(adt7473); | 
|  | 37 |  | 
|  | 38 | /* ADT7473 registers */ | 
|  | 39 | #define ADT7473_REG_BASE_ADDR			0x20 | 
|  | 40 |  | 
|  | 41 | #define ADT7473_REG_VOLT_BASE_ADDR		0x21 | 
|  | 42 | #define ADT7473_REG_VOLT_MAX_ADDR		0x22 | 
|  | 43 | #define ADT7473_REG_VOLT_MIN_BASE_ADDR		0x46 | 
|  | 44 | #define ADT7473_REG_VOLT_MIN_MAX_ADDR		0x49 | 
|  | 45 |  | 
|  | 46 | #define ADT7473_REG_TEMP_BASE_ADDR		0x25 | 
|  | 47 | #define ADT7473_REG_TEMP_MAX_ADDR		0x27 | 
|  | 48 | #define ADT7473_REG_TEMP_LIMITS_BASE_ADDR	0x4E | 
|  | 49 | #define ADT7473_REG_TEMP_LIMITS_MAX_ADDR	0x53 | 
|  | 50 | #define ADT7473_REG_TEMP_TMIN_BASE_ADDR		0x67 | 
|  | 51 | #define ADT7473_REG_TEMP_TMIN_MAX_ADDR		0x69 | 
|  | 52 | #define ADT7473_REG_TEMP_TMAX_BASE_ADDR		0x6A | 
|  | 53 | #define ADT7473_REG_TEMP_TMAX_MAX_ADDR		0x6C | 
|  | 54 |  | 
|  | 55 | #define ADT7473_REG_FAN_BASE_ADDR		0x28 | 
|  | 56 | #define ADT7473_REG_FAN_MAX_ADDR		0x2F | 
|  | 57 | #define ADT7473_REG_FAN_MIN_BASE_ADDR		0x54 | 
|  | 58 | #define ADT7473_REG_FAN_MIN_MAX_ADDR		0x5B | 
|  | 59 |  | 
|  | 60 | #define ADT7473_REG_PWM_BASE_ADDR		0x30 | 
|  | 61 | #define ADT7473_REG_PWM_MAX_ADDR		0x32 | 
|  | 62 | #define	ADT7473_REG_PWM_MIN_BASE_ADDR		0x64 | 
|  | 63 | #define ADT7473_REG_PWM_MIN_MAX_ADDR		0x66 | 
|  | 64 | #define ADT7473_REG_PWM_MAX_BASE_ADDR		0x38 | 
|  | 65 | #define ADT7473_REG_PWM_MAX_MAX_ADDR		0x3A | 
|  | 66 | #define ADT7473_REG_PWM_BHVR_BASE_ADDR		0x5C | 
|  | 67 | #define ADT7473_REG_PWM_BHVR_MAX_ADDR		0x5E | 
|  | 68 | #define		ADT7473_PWM_BHVR_MASK		0xE0 | 
|  | 69 | #define		ADT7473_PWM_BHVR_SHIFT		5 | 
|  | 70 |  | 
|  | 71 | #define ADT7473_REG_CFG1			0x40 | 
|  | 72 | #define 	ADT7473_CFG1_START		0x01 | 
|  | 73 | #define		ADT7473_CFG1_READY		0x04 | 
|  | 74 | #define ADT7473_REG_CFG2			0x73 | 
|  | 75 | #define ADT7473_REG_CFG3			0x78 | 
|  | 76 | #define ADT7473_REG_CFG4			0x7D | 
|  | 77 | #define		ADT7473_CFG4_MAX_DUTY_AT_OVT	0x08 | 
|  | 78 | #define ADT7473_REG_CFG5			0x7C | 
|  | 79 | #define		ADT7473_CFG5_TEMP_TWOS		0x01 | 
|  | 80 | #define		ADT7473_CFG5_TEMP_OFFSET	0x02 | 
|  | 81 |  | 
|  | 82 | #define ADT7473_REG_DEVICE			0x3D | 
|  | 83 | #define 	ADT7473_VENDOR			0x41 | 
|  | 84 | #define ADT7473_REG_VENDOR			0x3E | 
|  | 85 | #define 	ADT7473_DEVICE			0x73 | 
|  | 86 | #define ADT7473_REG_REVISION			0x3F | 
|  | 87 | #define 	ADT7473_REV_68			0x68 | 
|  | 88 | #define 	ADT7473_REV_69			0x69 | 
|  | 89 |  | 
|  | 90 | #define ADT7473_REG_ALARM1			0x41 | 
|  | 91 | #define		ADT7473_VCCP_ALARM		0x02 | 
|  | 92 | #define		ADT7473_VCC_ALARM		0x04 | 
|  | 93 | #define		ADT7473_R1T_ALARM		0x10 | 
|  | 94 | #define		ADT7473_LT_ALARM		0x20 | 
|  | 95 | #define		ADT7473_R2T_ALARM		0x40 | 
|  | 96 | #define		ADT7473_OOL			0x80 | 
|  | 97 | #define ADT7473_REG_ALARM2			0x42 | 
|  | 98 | #define		ADT7473_OVT_ALARM		0x02 | 
|  | 99 | #define		ADT7473_FAN1_ALARM		0x04 | 
|  | 100 | #define		ADT7473_FAN2_ALARM		0x08 | 
|  | 101 | #define		ADT7473_FAN3_ALARM		0x10 | 
|  | 102 | #define		ADT7473_FAN4_ALARM		0x20 | 
|  | 103 | #define		ADT7473_R1T_SHORT		0x40 | 
|  | 104 | #define		ADT7473_R2T_SHORT		0x80 | 
|  | 105 | #define ADT7473_REG_MAX_ADDR			0x80 | 
|  | 106 |  | 
|  | 107 | #define ALARM2(x)	((x) << 8) | 
|  | 108 |  | 
|  | 109 | #define ADT7473_VOLT_COUNT	2 | 
|  | 110 | #define ADT7473_REG_VOLT(x)	(ADT7473_REG_VOLT_BASE_ADDR + (x)) | 
|  | 111 | #define ADT7473_REG_VOLT_MIN(x)	(ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2)) | 
|  | 112 | #define ADT7473_REG_VOLT_MAX(x)	(ADT7473_REG_VOLT_MIN_BASE_ADDR + \ | 
|  | 113 | ((x) * 2) + 1) | 
|  | 114 |  | 
|  | 115 | #define ADT7473_TEMP_COUNT	3 | 
|  | 116 | #define ADT7473_REG_TEMP(x)	(ADT7473_REG_TEMP_BASE_ADDR + (x)) | 
|  | 117 | #define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2)) | 
|  | 118 | #define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \ | 
|  | 119 | ((x) * 2) + 1) | 
|  | 120 | #define ADT7473_REG_TEMP_TMIN(x)	(ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x)) | 
|  | 121 | #define ADT7473_REG_TEMP_TMAX(x)	(ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x)) | 
|  | 122 |  | 
|  | 123 | #define ADT7473_FAN_COUNT	4 | 
|  | 124 | #define ADT7473_REG_FAN(x)	(ADT7473_REG_FAN_BASE_ADDR + ((x) * 2)) | 
|  | 125 | #define ADT7473_REG_FAN_MIN(x)	(ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2)) | 
|  | 126 |  | 
|  | 127 | #define ADT7473_PWM_COUNT	3 | 
|  | 128 | #define ADT7473_REG_PWM(x)	(ADT7473_REG_PWM_BASE_ADDR + (x)) | 
|  | 129 | #define ADT7473_REG_PWM_MAX(x)	(ADT7473_REG_PWM_MAX_BASE_ADDR + (x)) | 
|  | 130 | #define ADT7473_REG_PWM_MIN(x)	(ADT7473_REG_PWM_MIN_BASE_ADDR + (x)) | 
|  | 131 | #define ADT7473_REG_PWM_BHVR(x)	(ADT7473_REG_PWM_BHVR_BASE_ADDR + (x)) | 
|  | 132 |  | 
|  | 133 | /* How often do we reread sensors values? (In jiffies) */ | 
|  | 134 | #define SENSOR_REFRESH_INTERVAL	(2 * HZ) | 
|  | 135 |  | 
|  | 136 | /* How often do we reread sensor limit values? (In jiffies) */ | 
|  | 137 | #define LIMIT_REFRESH_INTERVAL	(60 * HZ) | 
|  | 138 |  | 
|  | 139 | /* datasheet says to divide this number by the fan reading to get fan rpm */ | 
|  | 140 | #define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x)) | 
|  | 141 | #define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM | 
|  | 142 | #define FAN_PERIOD_INVALID	65535 | 
|  | 143 | #define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID) | 
|  | 144 |  | 
|  | 145 | struct adt7473_data { | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 146 | struct device		*hwmon_dev; | 
|  | 147 | struct attribute_group	attrs; | 
|  | 148 | struct mutex		lock; | 
|  | 149 | char			sensors_valid; | 
|  | 150 | char			limits_valid; | 
|  | 151 | unsigned long		sensors_last_updated;	/* In jiffies */ | 
|  | 152 | unsigned long		limits_last_updated;	/* In jiffies */ | 
|  | 153 |  | 
|  | 154 | u8			volt[ADT7473_VOLT_COUNT]; | 
|  | 155 | s8			volt_min[ADT7473_VOLT_COUNT]; | 
|  | 156 | s8			volt_max[ADT7473_VOLT_COUNT]; | 
|  | 157 |  | 
|  | 158 | s8			temp[ADT7473_TEMP_COUNT]; | 
|  | 159 | s8			temp_min[ADT7473_TEMP_COUNT]; | 
|  | 160 | s8			temp_max[ADT7473_TEMP_COUNT]; | 
|  | 161 | s8			temp_tmin[ADT7473_TEMP_COUNT]; | 
|  | 162 | /* This is called the !THERM limit in the datasheet */ | 
|  | 163 | s8			temp_tmax[ADT7473_TEMP_COUNT]; | 
|  | 164 |  | 
|  | 165 | u16			fan[ADT7473_FAN_COUNT]; | 
|  | 166 | u16			fan_min[ADT7473_FAN_COUNT]; | 
|  | 167 |  | 
|  | 168 | u8			pwm[ADT7473_PWM_COUNT]; | 
|  | 169 | u8			pwm_max[ADT7473_PWM_COUNT]; | 
|  | 170 | u8			pwm_min[ADT7473_PWM_COUNT]; | 
|  | 171 | u8			pwm_behavior[ADT7473_PWM_COUNT]; | 
|  | 172 |  | 
|  | 173 | u8			temp_twos_complement; | 
|  | 174 | u8			temp_offset; | 
|  | 175 |  | 
|  | 176 | u16			alarm; | 
|  | 177 | u8			max_duty_at_overheat; | 
|  | 178 | }; | 
|  | 179 |  | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 180 | static int adt7473_probe(struct i2c_client *client, | 
|  | 181 | const struct i2c_device_id *id); | 
|  | 182 | static int adt7473_detect(struct i2c_client *client, int kind, | 
|  | 183 | struct i2c_board_info *info); | 
|  | 184 | static int adt7473_remove(struct i2c_client *client); | 
|  | 185 |  | 
|  | 186 | static const struct i2c_device_id adt7473_id[] = { | 
|  | 187 | { "adt7473", adt7473 }, | 
|  | 188 | { } | 
|  | 189 | }; | 
|  | 190 | MODULE_DEVICE_TABLE(i2c, adt7473_id); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 191 |  | 
|  | 192 | static struct i2c_driver adt7473_driver = { | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 193 | .class		= I2C_CLASS_HWMON, | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 194 | .driver = { | 
|  | 195 | .name	= "adt7473", | 
|  | 196 | }, | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 197 | .probe		= adt7473_probe, | 
|  | 198 | .remove		= adt7473_remove, | 
|  | 199 | .id_table	= adt7473_id, | 
|  | 200 | .detect		= adt7473_detect, | 
|  | 201 | .address_data	= &addr_data, | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 202 | }; | 
|  | 203 |  | 
|  | 204 | /* | 
|  | 205 | * 16-bit registers on the ADT7473 are low-byte first.  The data sheet says | 
|  | 206 | * that the low byte must be read before the high byte. | 
|  | 207 | */ | 
|  | 208 | static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg) | 
|  | 209 | { | 
|  | 210 | u16 foo; | 
|  | 211 | foo = i2c_smbus_read_byte_data(client, reg); | 
|  | 212 | foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8); | 
|  | 213 | return foo; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg, | 
|  | 217 | u16 value) | 
|  | 218 | { | 
|  | 219 | return i2c_smbus_write_byte_data(client, reg, value & 0xFF) | 
|  | 220 | && i2c_smbus_write_byte_data(client, reg + 1, value >> 8); | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | static void adt7473_init_client(struct i2c_client *client) | 
|  | 224 | { | 
|  | 225 | int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1); | 
|  | 226 |  | 
|  | 227 | if (!(reg & ADT7473_CFG1_READY)) { | 
|  | 228 | dev_err(&client->dev, "Chip not ready.\n"); | 
|  | 229 | } else { | 
|  | 230 | /* start monitoring */ | 
|  | 231 | i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1, | 
|  | 232 | reg | ADT7473_CFG1_START); | 
|  | 233 | } | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | static struct adt7473_data *adt7473_update_device(struct device *dev) | 
|  | 237 | { | 
|  | 238 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 239 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 240 | unsigned long local_jiffies = jiffies; | 
|  | 241 | u8 cfg; | 
|  | 242 | int i; | 
|  | 243 |  | 
|  | 244 | mutex_lock(&data->lock); | 
|  | 245 | if (time_before(local_jiffies, data->sensors_last_updated + | 
|  | 246 | SENSOR_REFRESH_INTERVAL) | 
|  | 247 | && data->sensors_valid) | 
|  | 248 | goto no_sensor_update; | 
|  | 249 |  | 
|  | 250 | for (i = 0; i < ADT7473_VOLT_COUNT; i++) | 
|  | 251 | data->volt[i] = i2c_smbus_read_byte_data(client, | 
|  | 252 | ADT7473_REG_VOLT(i)); | 
|  | 253 |  | 
|  | 254 | /* Determine temperature encoding */ | 
|  | 255 | cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5); | 
|  | 256 | data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS); | 
|  | 257 |  | 
|  | 258 | /* | 
|  | 259 | * What does this do? it implies a variable temperature sensor | 
|  | 260 | * offset, but the datasheet doesn't say anything about this bit | 
|  | 261 | * and other parts of the datasheet imply that "offset64" mode | 
|  | 262 | * means that you shift temp values by -64 if the above bit was set. | 
|  | 263 | */ | 
|  | 264 | data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET); | 
|  | 265 |  | 
|  | 266 | for (i = 0; i < ADT7473_TEMP_COUNT; i++) | 
|  | 267 | data->temp[i] = i2c_smbus_read_byte_data(client, | 
|  | 268 | ADT7473_REG_TEMP(i)); | 
|  | 269 |  | 
|  | 270 | for (i = 0; i < ADT7473_FAN_COUNT; i++) | 
|  | 271 | data->fan[i] = adt7473_read_word_data(client, | 
|  | 272 | ADT7473_REG_FAN(i)); | 
|  | 273 |  | 
|  | 274 | for (i = 0; i < ADT7473_PWM_COUNT; i++) | 
|  | 275 | data->pwm[i] = i2c_smbus_read_byte_data(client, | 
|  | 276 | ADT7473_REG_PWM(i)); | 
|  | 277 |  | 
|  | 278 | data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1); | 
|  | 279 | if (data->alarm & ADT7473_OOL) | 
|  | 280 | data->alarm |= ALARM2(i2c_smbus_read_byte_data(client, | 
|  | 281 | ADT7473_REG_ALARM2)); | 
|  | 282 |  | 
|  | 283 | data->sensors_last_updated = local_jiffies; | 
|  | 284 | data->sensors_valid = 1; | 
|  | 285 |  | 
|  | 286 | no_sensor_update: | 
|  | 287 | if (time_before(local_jiffies, data->limits_last_updated + | 
|  | 288 | LIMIT_REFRESH_INTERVAL) | 
|  | 289 | && data->limits_valid) | 
|  | 290 | goto out; | 
|  | 291 |  | 
|  | 292 | for (i = 0; i < ADT7473_VOLT_COUNT; i++) { | 
|  | 293 | data->volt_min[i] = i2c_smbus_read_byte_data(client, | 
|  | 294 | ADT7473_REG_VOLT_MIN(i)); | 
|  | 295 | data->volt_max[i] = i2c_smbus_read_byte_data(client, | 
|  | 296 | ADT7473_REG_VOLT_MAX(i)); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | for (i = 0; i < ADT7473_TEMP_COUNT; i++) { | 
|  | 300 | data->temp_min[i] = i2c_smbus_read_byte_data(client, | 
|  | 301 | ADT7473_REG_TEMP_MIN(i)); | 
|  | 302 | data->temp_max[i] = i2c_smbus_read_byte_data(client, | 
|  | 303 | ADT7473_REG_TEMP_MAX(i)); | 
|  | 304 | data->temp_tmin[i] = i2c_smbus_read_byte_data(client, | 
|  | 305 | ADT7473_REG_TEMP_TMIN(i)); | 
|  | 306 | data->temp_tmax[i] = i2c_smbus_read_byte_data(client, | 
|  | 307 | ADT7473_REG_TEMP_TMAX(i)); | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | for (i = 0; i < ADT7473_FAN_COUNT; i++) | 
|  | 311 | data->fan_min[i] = adt7473_read_word_data(client, | 
|  | 312 | ADT7473_REG_FAN_MIN(i)); | 
|  | 313 |  | 
|  | 314 | for (i = 0; i < ADT7473_PWM_COUNT; i++) { | 
|  | 315 | data->pwm_max[i] = i2c_smbus_read_byte_data(client, | 
|  | 316 | ADT7473_REG_PWM_MAX(i)); | 
|  | 317 | data->pwm_min[i] = i2c_smbus_read_byte_data(client, | 
|  | 318 | ADT7473_REG_PWM_MIN(i)); | 
|  | 319 | data->pwm_behavior[i] = i2c_smbus_read_byte_data(client, | 
|  | 320 | ADT7473_REG_PWM_BHVR(i)); | 
|  | 321 | } | 
|  | 322 |  | 
| Jean Delvare | ed4ec81 | 2008-04-26 16:34:26 +0200 | [diff] [blame] | 323 | i = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4); | 
|  | 324 | data->max_duty_at_overheat = !!(i & ADT7473_CFG4_MAX_DUTY_AT_OVT); | 
|  | 325 |  | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 326 | data->limits_last_updated = local_jiffies; | 
|  | 327 | data->limits_valid = 1; | 
|  | 328 |  | 
|  | 329 | out: | 
|  | 330 | mutex_unlock(&data->lock); | 
|  | 331 | return data; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | /* | 
|  | 335 | * On this chip, voltages are given as a count of steps between a minimum | 
|  | 336 | * and maximum voltage, not a direct voltage. | 
|  | 337 | */ | 
|  | 338 | static const int volt_convert_table[][2] = { | 
|  | 339 | {2997, 3}, | 
|  | 340 | {4395, 4}, | 
|  | 341 | }; | 
|  | 342 |  | 
|  | 343 | static int decode_volt(int volt_index, u8 raw) | 
|  | 344 | { | 
|  | 345 | int cmax = volt_convert_table[volt_index][0]; | 
|  | 346 | int cmin = volt_convert_table[volt_index][1]; | 
|  | 347 | return ((raw * (cmax - cmin)) / 255) + cmin; | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | static u8 encode_volt(int volt_index, int cooked) | 
|  | 351 | { | 
|  | 352 | int cmax = volt_convert_table[volt_index][0]; | 
|  | 353 | int cmin = volt_convert_table[volt_index][1]; | 
|  | 354 | u8 x; | 
|  | 355 |  | 
|  | 356 | if (cooked > cmax) | 
|  | 357 | cooked = cmax; | 
|  | 358 | else if (cooked < cmin) | 
|  | 359 | cooked = cmin; | 
|  | 360 |  | 
|  | 361 | x = ((cooked - cmin) * 255) / (cmax - cmin); | 
|  | 362 |  | 
|  | 363 | return x; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | static ssize_t show_volt_min(struct device *dev, | 
|  | 367 | struct device_attribute *devattr, | 
|  | 368 | char *buf) | 
|  | 369 | { | 
|  | 370 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 371 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 372 | return sprintf(buf, "%d\n", | 
|  | 373 | decode_volt(attr->index, data->volt_min[attr->index])); | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | static ssize_t set_volt_min(struct device *dev, | 
|  | 377 | struct device_attribute *devattr, | 
|  | 378 | const char *buf, | 
|  | 379 | size_t count) | 
|  | 380 | { | 
|  | 381 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 382 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 383 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 384 | int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10)); | 
|  | 385 |  | 
|  | 386 | mutex_lock(&data->lock); | 
|  | 387 | data->volt_min[attr->index] = volt; | 
|  | 388 | i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index), | 
|  | 389 | volt); | 
|  | 390 | mutex_unlock(&data->lock); | 
|  | 391 |  | 
|  | 392 | return count; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | static ssize_t show_volt_max(struct device *dev, | 
|  | 396 | struct device_attribute *devattr, | 
|  | 397 | char *buf) | 
|  | 398 | { | 
|  | 399 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 400 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 401 | return sprintf(buf, "%d\n", | 
|  | 402 | decode_volt(attr->index, data->volt_max[attr->index])); | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | static ssize_t set_volt_max(struct device *dev, | 
|  | 406 | struct device_attribute *devattr, | 
|  | 407 | const char *buf, | 
|  | 408 | size_t count) | 
|  | 409 | { | 
|  | 410 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 411 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 412 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 413 | int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10)); | 
|  | 414 |  | 
|  | 415 | mutex_lock(&data->lock); | 
|  | 416 | data->volt_max[attr->index] = volt; | 
|  | 417 | i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index), | 
|  | 418 | volt); | 
|  | 419 | mutex_unlock(&data->lock); | 
|  | 420 |  | 
|  | 421 | return count; | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | static ssize_t show_volt(struct device *dev, struct device_attribute *devattr, | 
|  | 425 | char *buf) | 
|  | 426 | { | 
|  | 427 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 428 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 429 |  | 
|  | 430 | return sprintf(buf, "%d\n", | 
|  | 431 | decode_volt(attr->index, data->volt[attr->index])); | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | /* | 
|  | 435 | * This chip can report temperature data either as a two's complement | 
|  | 436 | * number in the range -128 to 127, or as an unsigned number that must | 
|  | 437 | * be offset by 64. | 
|  | 438 | */ | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 439 | static int decode_temp(u8 twos_complement, u8 raw) | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 440 | { | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 441 | return twos_complement ? (s8)raw : raw - 64; | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 444 | static u8 encode_temp(u8 twos_complement, int cooked) | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 445 | { | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 446 | return twos_complement ? cooked & 0xFF : cooked + 64; | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 447 | } | 
|  | 448 |  | 
|  | 449 | static ssize_t show_temp_min(struct device *dev, | 
|  | 450 | struct device_attribute *devattr, | 
|  | 451 | char *buf) | 
|  | 452 | { | 
|  | 453 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 454 | struct adt7473_data *data = adt7473_update_device(dev); | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 455 | return sprintf(buf, "%d\n", 1000 * decode_temp( | 
|  | 456 | data->temp_twos_complement, | 
|  | 457 | data->temp_min[attr->index])); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
|  | 460 | static ssize_t set_temp_min(struct device *dev, | 
|  | 461 | struct device_attribute *devattr, | 
|  | 462 | const char *buf, | 
|  | 463 | size_t count) | 
|  | 464 | { | 
|  | 465 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 466 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 467 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 468 | int temp = simple_strtol(buf, NULL, 10) / 1000; | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 469 | temp = encode_temp(data->temp_twos_complement, temp); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 470 |  | 
|  | 471 | mutex_lock(&data->lock); | 
|  | 472 | data->temp_min[attr->index] = temp; | 
|  | 473 | i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index), | 
|  | 474 | temp); | 
|  | 475 | mutex_unlock(&data->lock); | 
|  | 476 |  | 
|  | 477 | return count; | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | static ssize_t show_temp_max(struct device *dev, | 
|  | 481 | struct device_attribute *devattr, | 
|  | 482 | char *buf) | 
|  | 483 | { | 
|  | 484 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 485 | struct adt7473_data *data = adt7473_update_device(dev); | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 486 | return sprintf(buf, "%d\n", 1000 * decode_temp( | 
|  | 487 | data->temp_twos_complement, | 
|  | 488 | data->temp_max[attr->index])); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 489 | } | 
|  | 490 |  | 
|  | 491 | static ssize_t set_temp_max(struct device *dev, | 
|  | 492 | struct device_attribute *devattr, | 
|  | 493 | const char *buf, | 
|  | 494 | size_t count) | 
|  | 495 | { | 
|  | 496 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 497 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 498 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 499 | int temp = simple_strtol(buf, NULL, 10) / 1000; | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 500 | temp = encode_temp(data->temp_twos_complement, temp); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 501 |  | 
|  | 502 | mutex_lock(&data->lock); | 
|  | 503 | data->temp_max[attr->index] = temp; | 
|  | 504 | i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index), | 
|  | 505 | temp); | 
|  | 506 | mutex_unlock(&data->lock); | 
|  | 507 |  | 
|  | 508 | return count; | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, | 
|  | 512 | char *buf) | 
|  | 513 | { | 
|  | 514 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 515 | struct adt7473_data *data = adt7473_update_device(dev); | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 516 | return sprintf(buf, "%d\n", 1000 * decode_temp( | 
|  | 517 | data->temp_twos_complement, | 
|  | 518 | data->temp[attr->index])); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 519 | } | 
|  | 520 |  | 
|  | 521 | static ssize_t show_fan_min(struct device *dev, | 
|  | 522 | struct device_attribute *devattr, | 
|  | 523 | char *buf) | 
|  | 524 | { | 
|  | 525 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 526 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 527 |  | 
|  | 528 | if (FAN_DATA_VALID(data->fan_min[attr->index])) | 
|  | 529 | return sprintf(buf, "%d\n", | 
|  | 530 | FAN_PERIOD_TO_RPM(data->fan_min[attr->index])); | 
|  | 531 | else | 
|  | 532 | return sprintf(buf, "0\n"); | 
|  | 533 | } | 
|  | 534 |  | 
|  | 535 | static ssize_t set_fan_min(struct device *dev, | 
|  | 536 | struct device_attribute *devattr, | 
|  | 537 | const char *buf, size_t count) | 
|  | 538 | { | 
|  | 539 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 540 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 541 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 542 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 543 |  | 
|  | 544 | if (!temp) | 
|  | 545 | return -EINVAL; | 
|  | 546 | temp = FAN_RPM_TO_PERIOD(temp); | 
|  | 547 |  | 
|  | 548 | mutex_lock(&data->lock); | 
|  | 549 | data->fan_min[attr->index] = temp; | 
|  | 550 | adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp); | 
|  | 551 | mutex_unlock(&data->lock); | 
|  | 552 |  | 
|  | 553 | return count; | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, | 
|  | 557 | char *buf) | 
|  | 558 | { | 
|  | 559 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 560 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 561 |  | 
|  | 562 | if (FAN_DATA_VALID(data->fan[attr->index])) | 
|  | 563 | return sprintf(buf, "%d\n", | 
|  | 564 | FAN_PERIOD_TO_RPM(data->fan[attr->index])); | 
|  | 565 | else | 
|  | 566 | return sprintf(buf, "0\n"); | 
|  | 567 | } | 
|  | 568 |  | 
|  | 569 | static ssize_t show_max_duty_at_crit(struct device *dev, | 
|  | 570 | struct device_attribute *devattr, | 
|  | 571 | char *buf) | 
|  | 572 | { | 
|  | 573 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 574 | return sprintf(buf, "%d\n", data->max_duty_at_overheat); | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | static ssize_t set_max_duty_at_crit(struct device *dev, | 
|  | 578 | struct device_attribute *devattr, | 
|  | 579 | const char *buf, | 
|  | 580 | size_t count) | 
|  | 581 | { | 
|  | 582 | u8 reg; | 
|  | 583 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 584 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 585 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 586 | temp = temp && 0xFF; | 
|  | 587 |  | 
|  | 588 | mutex_lock(&data->lock); | 
|  | 589 | data->max_duty_at_overheat = temp; | 
|  | 590 | reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4); | 
|  | 591 | if (temp) | 
|  | 592 | reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT; | 
|  | 593 | else | 
|  | 594 | reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT; | 
|  | 595 | i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg); | 
|  | 596 | mutex_unlock(&data->lock); | 
|  | 597 |  | 
|  | 598 | return count; | 
|  | 599 | } | 
|  | 600 |  | 
|  | 601 | static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, | 
|  | 602 | char *buf) | 
|  | 603 | { | 
|  | 604 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 605 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 606 | return sprintf(buf, "%d\n", data->pwm[attr->index]); | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, | 
|  | 610 | const char *buf, size_t count) | 
|  | 611 | { | 
|  | 612 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 613 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 614 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 615 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 616 |  | 
|  | 617 | mutex_lock(&data->lock); | 
|  | 618 | data->pwm[attr->index] = temp; | 
|  | 619 | i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp); | 
|  | 620 | mutex_unlock(&data->lock); | 
|  | 621 |  | 
|  | 622 | return count; | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | static ssize_t show_pwm_max(struct device *dev, | 
|  | 626 | struct device_attribute *devattr, | 
|  | 627 | char *buf) | 
|  | 628 | { | 
|  | 629 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 630 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 631 | return sprintf(buf, "%d\n", data->pwm_max[attr->index]); | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | static ssize_t set_pwm_max(struct device *dev, | 
|  | 635 | struct device_attribute *devattr, | 
|  | 636 | const char *buf, | 
|  | 637 | size_t count) | 
|  | 638 | { | 
|  | 639 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 640 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 641 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 642 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 643 |  | 
|  | 644 | mutex_lock(&data->lock); | 
|  | 645 | data->pwm_max[attr->index] = temp; | 
|  | 646 | i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index), | 
|  | 647 | temp); | 
|  | 648 | mutex_unlock(&data->lock); | 
|  | 649 |  | 
|  | 650 | return count; | 
|  | 651 | } | 
|  | 652 |  | 
|  | 653 | static ssize_t show_pwm_min(struct device *dev, | 
|  | 654 | struct device_attribute *devattr, | 
|  | 655 | char *buf) | 
|  | 656 | { | 
|  | 657 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 658 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 659 | return sprintf(buf, "%d\n", data->pwm_min[attr->index]); | 
|  | 660 | } | 
|  | 661 |  | 
|  | 662 | static ssize_t set_pwm_min(struct device *dev, | 
|  | 663 | struct device_attribute *devattr, | 
|  | 664 | const char *buf, | 
|  | 665 | size_t count) | 
|  | 666 | { | 
|  | 667 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 668 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 669 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 670 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 671 |  | 
|  | 672 | mutex_lock(&data->lock); | 
|  | 673 | data->pwm_min[attr->index] = temp; | 
|  | 674 | i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index), | 
|  | 675 | temp); | 
|  | 676 | mutex_unlock(&data->lock); | 
|  | 677 |  | 
|  | 678 | return count; | 
|  | 679 | } | 
|  | 680 |  | 
|  | 681 | static ssize_t show_temp_tmax(struct device *dev, | 
|  | 682 | struct device_attribute *devattr, | 
|  | 683 | char *buf) | 
|  | 684 | { | 
|  | 685 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 686 | struct adt7473_data *data = adt7473_update_device(dev); | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 687 | return sprintf(buf, "%d\n", 1000 * decode_temp( | 
|  | 688 | data->temp_twos_complement, | 
|  | 689 | data->temp_tmax[attr->index])); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 690 | } | 
|  | 691 |  | 
|  | 692 | static ssize_t set_temp_tmax(struct device *dev, | 
|  | 693 | struct device_attribute *devattr, | 
|  | 694 | const char *buf, | 
|  | 695 | size_t count) | 
|  | 696 | { | 
|  | 697 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 698 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 699 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 700 | int temp = simple_strtol(buf, NULL, 10) / 1000; | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 701 | temp = encode_temp(data->temp_twos_complement, temp); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 702 |  | 
|  | 703 | mutex_lock(&data->lock); | 
|  | 704 | data->temp_tmax[attr->index] = temp; | 
|  | 705 | i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index), | 
|  | 706 | temp); | 
|  | 707 | mutex_unlock(&data->lock); | 
|  | 708 |  | 
|  | 709 | return count; | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | static ssize_t show_temp_tmin(struct device *dev, | 
|  | 713 | struct device_attribute *devattr, | 
|  | 714 | char *buf) | 
|  | 715 | { | 
|  | 716 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 717 | struct adt7473_data *data = adt7473_update_device(dev); | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 718 | return sprintf(buf, "%d\n", 1000 * decode_temp( | 
|  | 719 | data->temp_twos_complement, | 
|  | 720 | data->temp_tmin[attr->index])); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 721 | } | 
|  | 722 |  | 
|  | 723 | static ssize_t set_temp_tmin(struct device *dev, | 
|  | 724 | struct device_attribute *devattr, | 
|  | 725 | const char *buf, | 
|  | 726 | size_t count) | 
|  | 727 | { | 
|  | 728 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 729 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 730 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 731 | int temp = simple_strtol(buf, NULL, 10) / 1000; | 
| Mark M. Hoffman | 1852448 | 2008-03-06 08:41:09 -0500 | [diff] [blame] | 732 | temp = encode_temp(data->temp_twos_complement, temp); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 733 |  | 
|  | 734 | mutex_lock(&data->lock); | 
|  | 735 | data->temp_tmin[attr->index] = temp; | 
|  | 736 | i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index), | 
|  | 737 | temp); | 
|  | 738 | mutex_unlock(&data->lock); | 
|  | 739 |  | 
|  | 740 | return count; | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | static ssize_t show_pwm_enable(struct device *dev, | 
|  | 744 | struct device_attribute *devattr, | 
|  | 745 | char *buf) | 
|  | 746 | { | 
|  | 747 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 748 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 749 |  | 
|  | 750 | switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) { | 
|  | 751 | case 3: | 
|  | 752 | return sprintf(buf, "0\n"); | 
|  | 753 | case 7: | 
|  | 754 | return sprintf(buf, "1\n"); | 
|  | 755 | default: | 
|  | 756 | return sprintf(buf, "2\n"); | 
|  | 757 | } | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | static ssize_t set_pwm_enable(struct device *dev, | 
|  | 761 | struct device_attribute *devattr, | 
|  | 762 | const char *buf, | 
|  | 763 | size_t count) | 
|  | 764 | { | 
|  | 765 | u8 reg; | 
|  | 766 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 767 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 768 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 769 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 770 |  | 
|  | 771 | switch (temp) { | 
|  | 772 | case 0: | 
|  | 773 | temp = 3; | 
|  | 774 | break; | 
|  | 775 | case 1: | 
|  | 776 | temp = 7; | 
|  | 777 | break; | 
|  | 778 | case 2: | 
|  | 779 | /* Enter automatic mode with fans off */ | 
|  | 780 | temp = 4; | 
|  | 781 | break; | 
|  | 782 | default: | 
|  | 783 | return -EINVAL; | 
|  | 784 | } | 
|  | 785 |  | 
|  | 786 | mutex_lock(&data->lock); | 
|  | 787 | reg = i2c_smbus_read_byte_data(client, | 
|  | 788 | ADT7473_REG_PWM_BHVR(attr->index)); | 
|  | 789 | reg = (temp << ADT7473_PWM_BHVR_SHIFT) | | 
|  | 790 | (reg & ~ADT7473_PWM_BHVR_MASK); | 
|  | 791 | i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index), | 
|  | 792 | reg); | 
|  | 793 | data->pwm_behavior[attr->index] = reg; | 
|  | 794 | mutex_unlock(&data->lock); | 
|  | 795 |  | 
|  | 796 | return count; | 
|  | 797 | } | 
|  | 798 |  | 
|  | 799 | static ssize_t show_pwm_auto_temp(struct device *dev, | 
|  | 800 | struct device_attribute *devattr, | 
|  | 801 | char *buf) | 
|  | 802 | { | 
|  | 803 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 804 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 805 | int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT; | 
|  | 806 |  | 
|  | 807 | switch (bhvr) { | 
|  | 808 | case 3: | 
|  | 809 | case 4: | 
|  | 810 | case 7: | 
|  | 811 | return sprintf(buf, "0\n"); | 
|  | 812 | case 0: | 
|  | 813 | case 1: | 
|  | 814 | case 5: | 
|  | 815 | case 6: | 
|  | 816 | return sprintf(buf, "%d\n", bhvr + 1); | 
|  | 817 | case 2: | 
|  | 818 | return sprintf(buf, "4\n"); | 
|  | 819 | } | 
|  | 820 | /* shouldn't ever get here */ | 
|  | 821 | BUG(); | 
|  | 822 | } | 
|  | 823 |  | 
|  | 824 | static ssize_t set_pwm_auto_temp(struct device *dev, | 
|  | 825 | struct device_attribute *devattr, | 
|  | 826 | const char *buf, | 
|  | 827 | size_t count) | 
|  | 828 | { | 
|  | 829 | u8 reg; | 
|  | 830 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 831 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 832 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 833 | int temp = simple_strtol(buf, NULL, 10); | 
|  | 834 |  | 
|  | 835 | switch (temp) { | 
|  | 836 | case 1: | 
|  | 837 | case 2: | 
|  | 838 | case 6: | 
|  | 839 | case 7: | 
|  | 840 | temp--; | 
|  | 841 | break; | 
|  | 842 | case 0: | 
|  | 843 | temp = 4; | 
|  | 844 | break; | 
|  | 845 | default: | 
|  | 846 | return -EINVAL; | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | mutex_lock(&data->lock); | 
|  | 850 | reg = i2c_smbus_read_byte_data(client, | 
|  | 851 | ADT7473_REG_PWM_BHVR(attr->index)); | 
|  | 852 | reg = (temp << ADT7473_PWM_BHVR_SHIFT) | | 
|  | 853 | (reg & ~ADT7473_PWM_BHVR_MASK); | 
|  | 854 | i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index), | 
|  | 855 | reg); | 
|  | 856 | data->pwm_behavior[attr->index] = reg; | 
|  | 857 | mutex_unlock(&data->lock); | 
|  | 858 |  | 
|  | 859 | return count; | 
|  | 860 | } | 
|  | 861 |  | 
|  | 862 | static ssize_t show_alarm(struct device *dev, | 
|  | 863 | struct device_attribute *devattr, | 
|  | 864 | char *buf) | 
|  | 865 | { | 
|  | 866 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 867 | struct adt7473_data *data = adt7473_update_device(dev); | 
|  | 868 |  | 
|  | 869 | if (data->alarm & attr->index) | 
|  | 870 | return sprintf(buf, "1\n"); | 
|  | 871 | else | 
|  | 872 | return sprintf(buf, "0\n"); | 
|  | 873 | } | 
|  | 874 |  | 
|  | 875 |  | 
|  | 876 | static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max, | 
|  | 877 | set_volt_max, 0); | 
|  | 878 | static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max, | 
|  | 879 | set_volt_max, 1); | 
|  | 880 |  | 
|  | 881 | static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min, | 
|  | 882 | set_volt_min, 0); | 
|  | 883 | static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min, | 
|  | 884 | set_volt_min, 1); | 
|  | 885 |  | 
|  | 886 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0); | 
|  | 887 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1); | 
|  | 888 |  | 
|  | 889 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 890 | ADT7473_VCCP_ALARM); | 
|  | 891 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 892 | ADT7473_VCC_ALARM); | 
|  | 893 |  | 
|  | 894 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, | 
|  | 895 | set_temp_max, 0); | 
|  | 896 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, | 
|  | 897 | set_temp_max, 1); | 
|  | 898 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max, | 
|  | 899 | set_temp_max, 2); | 
|  | 900 |  | 
|  | 901 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, | 
|  | 902 | set_temp_min, 0); | 
|  | 903 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, | 
|  | 904 | set_temp_min, 1); | 
|  | 905 | static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min, | 
|  | 906 | set_temp_min, 2); | 
|  | 907 |  | 
|  | 908 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | 
|  | 909 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | 
|  | 910 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); | 
|  | 911 |  | 
|  | 912 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 913 | ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT)); | 
|  | 914 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 915 | ADT7473_LT_ALARM); | 
|  | 916 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 917 | ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT)); | 
|  | 918 |  | 
|  | 919 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, | 
|  | 920 | set_fan_min, 0); | 
|  | 921 | static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, | 
|  | 922 | set_fan_min, 1); | 
|  | 923 | static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, | 
|  | 924 | set_fan_min, 2); | 
|  | 925 | static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min, | 
|  | 926 | set_fan_min, 3); | 
|  | 927 |  | 
|  | 928 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); | 
|  | 929 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); | 
|  | 930 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); | 
|  | 931 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3); | 
|  | 932 |  | 
|  | 933 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 934 | ALARM2(ADT7473_FAN1_ALARM)); | 
|  | 935 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 936 | ALARM2(ADT7473_FAN2_ALARM)); | 
|  | 937 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 938 | ALARM2(ADT7473_FAN3_ALARM)); | 
|  | 939 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, | 
|  | 940 | ALARM2(ADT7473_FAN4_ALARM)); | 
|  | 941 |  | 
|  | 942 | static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO, | 
|  | 943 | show_max_duty_at_crit, set_max_duty_at_crit, 0); | 
|  | 944 |  | 
|  | 945 | static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0); | 
|  | 946 | static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1); | 
|  | 947 | static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2); | 
|  | 948 |  | 
|  | 949 | static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO, | 
|  | 950 | show_pwm_min, set_pwm_min, 0); | 
|  | 951 | static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO, | 
|  | 952 | show_pwm_min, set_pwm_min, 1); | 
|  | 953 | static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO, | 
|  | 954 | show_pwm_min, set_pwm_min, 2); | 
|  | 955 |  | 
|  | 956 | static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO, | 
|  | 957 | show_pwm_max, set_pwm_max, 0); | 
|  | 958 | static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO, | 
|  | 959 | show_pwm_max, set_pwm_max, 1); | 
|  | 960 | static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO, | 
|  | 961 | show_pwm_max, set_pwm_max, 2); | 
|  | 962 |  | 
|  | 963 | static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO, | 
|  | 964 | show_temp_tmin, set_temp_tmin, 0); | 
|  | 965 | static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO, | 
|  | 966 | show_temp_tmin, set_temp_tmin, 1); | 
|  | 967 | static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO, | 
|  | 968 | show_temp_tmin, set_temp_tmin, 2); | 
|  | 969 |  | 
|  | 970 | static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO, | 
|  | 971 | show_temp_tmax, set_temp_tmax, 0); | 
|  | 972 | static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO, | 
|  | 973 | show_temp_tmax, set_temp_tmax, 1); | 
|  | 974 | static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO, | 
|  | 975 | show_temp_tmax, set_temp_tmax, 2); | 
|  | 976 |  | 
|  | 977 | static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable, | 
|  | 978 | set_pwm_enable, 0); | 
|  | 979 | static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable, | 
|  | 980 | set_pwm_enable, 1); | 
|  | 981 | static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable, | 
|  | 982 | set_pwm_enable, 2); | 
|  | 983 |  | 
|  | 984 | static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO, | 
|  | 985 | show_pwm_auto_temp, set_pwm_auto_temp, 0); | 
|  | 986 | static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO, | 
|  | 987 | show_pwm_auto_temp, set_pwm_auto_temp, 1); | 
|  | 988 | static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO, | 
|  | 989 | show_pwm_auto_temp, set_pwm_auto_temp, 2); | 
|  | 990 |  | 
|  | 991 | static struct attribute *adt7473_attr[] = | 
|  | 992 | { | 
|  | 993 | &sensor_dev_attr_in1_max.dev_attr.attr, | 
|  | 994 | &sensor_dev_attr_in2_max.dev_attr.attr, | 
|  | 995 | &sensor_dev_attr_in1_min.dev_attr.attr, | 
|  | 996 | &sensor_dev_attr_in2_min.dev_attr.attr, | 
|  | 997 | &sensor_dev_attr_in1_input.dev_attr.attr, | 
|  | 998 | &sensor_dev_attr_in2_input.dev_attr.attr, | 
|  | 999 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | 
|  | 1000 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | 
|  | 1001 |  | 
|  | 1002 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 
|  | 1003 | &sensor_dev_attr_temp2_max.dev_attr.attr, | 
|  | 1004 | &sensor_dev_attr_temp3_max.dev_attr.attr, | 
|  | 1005 | &sensor_dev_attr_temp1_min.dev_attr.attr, | 
|  | 1006 | &sensor_dev_attr_temp2_min.dev_attr.attr, | 
|  | 1007 | &sensor_dev_attr_temp3_min.dev_attr.attr, | 
|  | 1008 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 
|  | 1009 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 
|  | 1010 | &sensor_dev_attr_temp3_input.dev_attr.attr, | 
|  | 1011 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, | 
|  | 1012 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, | 
|  | 1013 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | 
|  | 1014 | &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr, | 
|  | 1015 | &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr, | 
|  | 1016 | &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr, | 
|  | 1017 | &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr, | 
|  | 1018 | &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr, | 
|  | 1019 | &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr, | 
|  | 1020 |  | 
|  | 1021 | &sensor_dev_attr_fan1_min.dev_attr.attr, | 
|  | 1022 | &sensor_dev_attr_fan2_min.dev_attr.attr, | 
|  | 1023 | &sensor_dev_attr_fan3_min.dev_attr.attr, | 
|  | 1024 | &sensor_dev_attr_fan4_min.dev_attr.attr, | 
|  | 1025 | &sensor_dev_attr_fan1_input.dev_attr.attr, | 
|  | 1026 | &sensor_dev_attr_fan2_input.dev_attr.attr, | 
|  | 1027 | &sensor_dev_attr_fan3_input.dev_attr.attr, | 
|  | 1028 | &sensor_dev_attr_fan4_input.dev_attr.attr, | 
|  | 1029 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | 
|  | 1030 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | 
|  | 1031 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | 
|  | 1032 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, | 
|  | 1033 |  | 
|  | 1034 | &sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr, | 
|  | 1035 |  | 
|  | 1036 | &sensor_dev_attr_pwm1.dev_attr.attr, | 
|  | 1037 | &sensor_dev_attr_pwm2.dev_attr.attr, | 
|  | 1038 | &sensor_dev_attr_pwm3.dev_attr.attr, | 
|  | 1039 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, | 
|  | 1040 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, | 
|  | 1041 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, | 
|  | 1042 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, | 
|  | 1043 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, | 
|  | 1044 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, | 
|  | 1045 |  | 
|  | 1046 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, | 
|  | 1047 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, | 
|  | 1048 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, | 
|  | 1049 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, | 
|  | 1050 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, | 
|  | 1051 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, | 
|  | 1052 |  | 
|  | 1053 | NULL | 
|  | 1054 | }; | 
|  | 1055 |  | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1056 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
|  | 1057 | static int adt7473_detect(struct i2c_client *client, int kind, | 
|  | 1058 | struct i2c_board_info *info) | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1059 | { | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1060 | struct i2c_adapter *adapter = client->adapter; | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1061 |  | 
|  | 1062 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1063 | return -ENODEV; | 
|  | 1064 |  | 
|  | 1065 | if (kind <= 0) { | 
|  | 1066 | int vendor, device, revision; | 
|  | 1067 |  | 
|  | 1068 | vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR); | 
|  | 1069 | if (vendor != ADT7473_VENDOR) | 
|  | 1070 | return -ENODEV; | 
|  | 1071 |  | 
|  | 1072 | device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE); | 
|  | 1073 | if (device != ADT7473_DEVICE) | 
|  | 1074 | return -ENODEV; | 
|  | 1075 |  | 
|  | 1076 | revision = i2c_smbus_read_byte_data(client, | 
|  | 1077 | ADT7473_REG_REVISION); | 
|  | 1078 | if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69) | 
|  | 1079 | return -ENODEV; | 
|  | 1080 | } else | 
|  | 1081 | dev_dbg(&adapter->dev, "detection forced\n"); | 
|  | 1082 |  | 
|  | 1083 | strlcpy(info->type, "adt7473", I2C_NAME_SIZE); | 
|  | 1084 |  | 
|  | 1085 | return 0; | 
|  | 1086 | } | 
|  | 1087 |  | 
|  | 1088 | static int adt7473_probe(struct i2c_client *client, | 
|  | 1089 | const struct i2c_device_id *id) | 
|  | 1090 | { | 
|  | 1091 | struct adt7473_data *data; | 
|  | 1092 | int err; | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1093 |  | 
|  | 1094 | data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL); | 
|  | 1095 | if (!data) { | 
|  | 1096 | err = -ENOMEM; | 
|  | 1097 | goto exit; | 
|  | 1098 | } | 
|  | 1099 |  | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1100 | i2c_set_clientdata(client, data); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1101 | mutex_init(&data->lock); | 
|  | 1102 |  | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1103 | dev_info(&client->dev, "%s chip found\n", client->name); | 
|  | 1104 |  | 
|  | 1105 | /* Initialize the ADT7473 chip */ | 
|  | 1106 | adt7473_init_client(client); | 
|  | 1107 |  | 
|  | 1108 | /* Register sysfs hooks */ | 
|  | 1109 | data->attrs.attrs = adt7473_attr; | 
|  | 1110 | err = sysfs_create_group(&client->dev.kobj, &data->attrs); | 
|  | 1111 | if (err) | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1112 | goto exit_free; | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1113 |  | 
|  | 1114 | data->hwmon_dev = hwmon_device_register(&client->dev); | 
|  | 1115 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 1116 | err = PTR_ERR(data->hwmon_dev); | 
|  | 1117 | goto exit_remove; | 
|  | 1118 | } | 
|  | 1119 |  | 
|  | 1120 | return 0; | 
|  | 1121 |  | 
|  | 1122 | exit_remove: | 
|  | 1123 | sysfs_remove_group(&client->dev.kobj, &data->attrs); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1124 | exit_free: | 
|  | 1125 | kfree(data); | 
|  | 1126 | exit: | 
|  | 1127 | return err; | 
|  | 1128 | } | 
|  | 1129 |  | 
| Jean Delvare | eea5476 | 2008-07-16 19:30:10 +0200 | [diff] [blame] | 1130 | static int adt7473_remove(struct i2c_client *client) | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1131 | { | 
|  | 1132 | struct adt7473_data *data = i2c_get_clientdata(client); | 
|  | 1133 |  | 
|  | 1134 | hwmon_device_unregister(data->hwmon_dev); | 
|  | 1135 | sysfs_remove_group(&client->dev.kobj, &data->attrs); | 
| Darrick J. Wong | 57df46d | 2008-02-18 13:33:23 -0800 | [diff] [blame] | 1136 | kfree(data); | 
|  | 1137 | return 0; | 
|  | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | static int __init adt7473_init(void) | 
|  | 1141 | { | 
|  | 1142 | return i2c_add_driver(&adt7473_driver); | 
|  | 1143 | } | 
|  | 1144 |  | 
|  | 1145 | static void __exit adt7473_exit(void) | 
|  | 1146 | { | 
|  | 1147 | i2c_del_driver(&adt7473_driver); | 
|  | 1148 | } | 
|  | 1149 |  | 
|  | 1150 | MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>"); | 
|  | 1151 | MODULE_DESCRIPTION("ADT7473 driver"); | 
|  | 1152 | MODULE_LICENSE("GPL"); | 
|  | 1153 |  | 
|  | 1154 | module_init(adt7473_init); | 
|  | 1155 | module_exit(adt7473_exit); |