Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 1 | /* |
| 2 | * adm9240.c Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * |
| 5 | * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl> |
| 6 | * Philip Edelbrock <phil@netroedge.com> |
| 7 | * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl> |
| 8 | * Copyright (C) 2005 Grant Coady <gcoady@gmail.com> with valuable |
| 9 | * guidance from Jean Delvare |
| 10 | * |
| 11 | * Driver supports Analog Devices ADM9240 |
| 12 | * Dallas Semiconductor DS1780 |
| 13 | * National Semiconductor LM81 |
| 14 | * |
| 15 | * ADM9240 is the reference, DS1780 and LM81 are register compatibles |
| 16 | * |
| 17 | * Voltage Six inputs are scaled by chip, VID also reported |
| 18 | * Temperature Chip temperature to 0.5'C, maximum and max_hysteris |
| 19 | * Fans 2 fans, low speed alarm, automatic fan clock divider |
| 20 | * Alarms 16-bit map of active alarms |
| 21 | * Analog Out 0..1250 mV output |
| 22 | * |
| 23 | * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear' |
| 24 | * |
| 25 | * Test hardware: Intel SE440BX-2 desktop motherboard --Grant |
| 26 | * |
| 27 | * LM81 extended temp reading not implemented |
| 28 | * |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License as published by |
| 31 | * the Free Software Foundation; either version 2 of the License, or |
| 32 | * (at your option) any later version. |
| 33 | * |
| 34 | * This program is distributed in the hope that it will be useful, |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | * GNU General Public License for more details. |
| 38 | * |
| 39 | * You should have received a copy of the GNU General Public License |
| 40 | * along with this program; if not, write to the Free Software |
| 41 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 42 | */ |
| 43 | |
| 44 | #include <linux/init.h> |
| 45 | #include <linux/module.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/i2c.h> |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 48 | #include <linux/i2c-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 49 | #include <linux/hwmon.h> |
| 50 | #include <linux/err.h> |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 51 | |
| 52 | /* Addresses to scan */ |
| 53 | static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, |
| 54 | I2C_CLIENT_END }; |
| 55 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 56 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame^] | 57 | I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 58 | |
| 59 | /* ADM9240 registers */ |
| 60 | #define ADM9240_REG_MAN_ID 0x3e |
| 61 | #define ADM9240_REG_DIE_REV 0x3f |
| 62 | #define ADM9240_REG_CONFIG 0x40 |
| 63 | |
| 64 | #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */ |
| 65 | #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2) |
| 66 | #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2) |
| 67 | #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */ |
| 68 | #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 69 | #define ADM9240_REG_INT(nr) (0x41 + (nr)) |
| 70 | #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr)) |
| 71 | #define ADM9240_REG_TEMP 0x27 |
| 72 | #define ADM9240_REG_TEMP_HIGH 0x39 |
| 73 | #define ADM9240_REG_TEMP_HYST 0x3a |
| 74 | #define ADM9240_REG_ANALOG_OUT 0x19 |
| 75 | #define ADM9240_REG_CHASSIS_CLEAR 0x46 |
| 76 | #define ADM9240_REG_VID_FAN_DIV 0x47 |
| 77 | #define ADM9240_REG_I2C_ADDR 0x48 |
| 78 | #define ADM9240_REG_VID4 0x49 |
| 79 | #define ADM9240_REG_TEMP_CONF 0x4b |
| 80 | |
| 81 | /* generalised scaling with integer rounding */ |
| 82 | static inline int SCALE(long val, int mul, int div) |
| 83 | { |
| 84 | if (val < 0) |
| 85 | return (val * mul - div / 2) / div; |
| 86 | else |
| 87 | return (val * mul + div / 2) / div; |
| 88 | } |
| 89 | |
| 90 | /* adm9240 internally scales voltage measurements */ |
| 91 | static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 }; |
| 92 | |
| 93 | static inline unsigned int IN_FROM_REG(u8 reg, int n) |
| 94 | { |
| 95 | return SCALE(reg, nom_mv[n], 192); |
| 96 | } |
| 97 | |
| 98 | static inline u8 IN_TO_REG(unsigned long val, int n) |
| 99 | { |
| 100 | return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255); |
| 101 | } |
| 102 | |
| 103 | /* temperature range: -40..125, 127 disables temperature alarm */ |
| 104 | static inline s8 TEMP_TO_REG(long val) |
| 105 | { |
| 106 | return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127); |
| 107 | } |
| 108 | |
| 109 | /* two fans, each with low fan speed limit */ |
| 110 | static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) |
| 111 | { |
| 112 | if (!reg) /* error */ |
| 113 | return -1; |
| 114 | |
| 115 | if (reg == 255) |
| 116 | return 0; |
| 117 | |
| 118 | return SCALE(1350000, 1, reg * div); |
| 119 | } |
| 120 | |
| 121 | /* analog out 0..1250mV */ |
| 122 | static inline u8 AOUT_TO_REG(unsigned long val) |
| 123 | { |
| 124 | return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255); |
| 125 | } |
| 126 | |
| 127 | static inline unsigned int AOUT_FROM_REG(u8 reg) |
| 128 | { |
| 129 | return SCALE(reg, 1250, 255); |
| 130 | } |
| 131 | |
| 132 | static int adm9240_attach_adapter(struct i2c_adapter *adapter); |
| 133 | static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind); |
| 134 | static void adm9240_init_client(struct i2c_client *client); |
| 135 | static int adm9240_detach_client(struct i2c_client *client); |
| 136 | static struct adm9240_data *adm9240_update_device(struct device *dev); |
| 137 | |
| 138 | /* driver data */ |
| 139 | static struct i2c_driver adm9240_driver = { |
| 140 | .owner = THIS_MODULE, |
| 141 | .name = "adm9240", |
| 142 | .id = I2C_DRIVERID_ADM9240, |
| 143 | .flags = I2C_DF_NOTIFY, |
| 144 | .attach_adapter = adm9240_attach_adapter, |
| 145 | .detach_client = adm9240_detach_client, |
| 146 | }; |
| 147 | |
| 148 | /* per client data */ |
| 149 | struct adm9240_data { |
| 150 | enum chips type; |
| 151 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 152 | struct class_device *class_dev; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 153 | struct semaphore update_lock; |
| 154 | char valid; |
| 155 | unsigned long last_updated_measure; |
| 156 | unsigned long last_updated_config; |
| 157 | |
| 158 | u8 in[6]; /* ro in0_input */ |
| 159 | u8 in_max[6]; /* rw in0_max */ |
| 160 | u8 in_min[6]; /* rw in0_min */ |
| 161 | u8 fan[2]; /* ro fan1_input */ |
| 162 | u8 fan_min[2]; /* rw fan1_min */ |
| 163 | u8 fan_div[2]; /* rw fan1_div, read-only accessor */ |
| 164 | s16 temp; /* ro temp1_input, 9-bit sign-extended */ |
| 165 | s8 temp_high; /* rw temp1_max */ |
| 166 | s8 temp_hyst; /* rw temp1_max_hyst */ |
| 167 | u16 alarms; /* ro alarms */ |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 168 | u8 aout; /* rw aout_output */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 169 | u8 vid; /* ro vid */ |
| 170 | u8 vrm; /* -- vrm set on startup, no accessor */ |
| 171 | }; |
| 172 | |
| 173 | /* i2c byte read/write interface */ |
| 174 | static int adm9240_read_value(struct i2c_client *client, u8 reg) |
| 175 | { |
| 176 | return i2c_smbus_read_byte_data(client, reg); |
| 177 | } |
| 178 | |
| 179 | static int adm9240_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 180 | { |
| 181 | return i2c_smbus_write_byte_data(client, reg, value); |
| 182 | } |
| 183 | |
| 184 | /*** sysfs accessors ***/ |
| 185 | |
| 186 | /* temperature */ |
| 187 | #define show_temp(value, scale) \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 188 | static ssize_t show_##value(struct device *dev, \ |
| 189 | struct device_attribute *attr, \ |
| 190 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 191 | { \ |
| 192 | struct adm9240_data *data = adm9240_update_device(dev); \ |
| 193 | return sprintf(buf, "%d\n", data->value * scale); \ |
| 194 | } |
| 195 | show_temp(temp_high, 1000); |
| 196 | show_temp(temp_hyst, 1000); |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 197 | show_temp(temp, 500); /* 0.5'C per bit */ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 198 | |
| 199 | #define set_temp(value, reg) \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 200 | static ssize_t set_##value(struct device *dev, \ |
| 201 | struct device_attribute *attr, \ |
| 202 | const char *buf, size_t count) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 203 | { \ |
| 204 | struct i2c_client *client = to_i2c_client(dev); \ |
| 205 | struct adm9240_data *data = adm9240_update_device(dev); \ |
| 206 | long temp = simple_strtoul(buf, NULL, 10); \ |
| 207 | \ |
| 208 | down(&data->update_lock); \ |
| 209 | data->value = TEMP_TO_REG(temp); \ |
| 210 | adm9240_write_value(client, reg, data->value); \ |
| 211 | up(&data->update_lock); \ |
| 212 | return count; \ |
| 213 | } |
| 214 | |
| 215 | set_temp(temp_high, ADM9240_REG_TEMP_HIGH); |
| 216 | set_temp(temp_hyst, ADM9240_REG_TEMP_HYST); |
| 217 | |
| 218 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, |
| 219 | show_temp_high, set_temp_high); |
| 220 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, |
| 221 | show_temp_hyst, set_temp_hyst); |
| 222 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL); |
| 223 | |
| 224 | /* voltage */ |
| 225 | static ssize_t show_in(struct device *dev, char *buf, int nr) |
| 226 | { |
| 227 | struct adm9240_data *data = adm9240_update_device(dev); |
| 228 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr)); |
| 229 | } |
| 230 | |
| 231 | static ssize_t show_in_min(struct device *dev, char *buf, int nr) |
| 232 | { |
| 233 | struct adm9240_data *data = adm9240_update_device(dev); |
| 234 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr)); |
| 235 | } |
| 236 | |
| 237 | static ssize_t show_in_max(struct device *dev, char *buf, int nr) |
| 238 | { |
| 239 | struct adm9240_data *data = adm9240_update_device(dev); |
| 240 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr)); |
| 241 | } |
| 242 | |
| 243 | static ssize_t set_in_min(struct device *dev, const char *buf, |
| 244 | size_t count, int nr) |
| 245 | { |
| 246 | struct i2c_client *client = to_i2c_client(dev); |
| 247 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 248 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 249 | |
| 250 | down(&data->update_lock); |
| 251 | data->in_min[nr] = IN_TO_REG(val, nr); |
| 252 | adm9240_write_value(client, ADM9240_REG_IN_MIN(nr), data->in_min[nr]); |
| 253 | up(&data->update_lock); |
| 254 | return count; |
| 255 | } |
| 256 | |
| 257 | static ssize_t set_in_max(struct device *dev, const char *buf, |
| 258 | size_t count, int nr) |
| 259 | { |
| 260 | struct i2c_client *client = to_i2c_client(dev); |
| 261 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 262 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 263 | |
| 264 | down(&data->update_lock); |
| 265 | data->in_max[nr] = IN_TO_REG(val, nr); |
| 266 | adm9240_write_value(client, ADM9240_REG_IN_MAX(nr), data->in_max[nr]); |
| 267 | up(&data->update_lock); |
| 268 | return count; |
| 269 | } |
| 270 | |
| 271 | #define show_in_offset(offset) \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 272 | static ssize_t show_in##offset(struct device *dev, \ |
| 273 | struct device_attribute *attr, \ |
| 274 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 275 | { \ |
| 276 | return show_in(dev, buf, offset); \ |
| 277 | } \ |
| 278 | static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 279 | static ssize_t show_in##offset##_min(struct device *dev, \ |
| 280 | struct device_attribute *attr, \ |
| 281 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 282 | { \ |
| 283 | return show_in_min(dev, buf, offset); \ |
| 284 | } \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 285 | static ssize_t show_in##offset##_max(struct device *dev, \ |
| 286 | struct device_attribute *attr, \ |
| 287 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 288 | { \ |
| 289 | return show_in_max(dev, buf, offset); \ |
| 290 | } \ |
| 291 | static ssize_t \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 292 | set_in##offset##_min(struct device *dev, \ |
| 293 | struct device_attribute *attr, const char *buf, \ |
| 294 | size_t count) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 295 | { \ |
| 296 | return set_in_min(dev, buf, count, offset); \ |
| 297 | } \ |
| 298 | static ssize_t \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 299 | set_in##offset##_max(struct device *dev, \ |
| 300 | struct device_attribute *attr, const char *buf, \ |
| 301 | size_t count) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 302 | { \ |
| 303 | return set_in_max(dev, buf, count, offset); \ |
| 304 | } \ |
| 305 | static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 306 | show_in##offset##_min, set_in##offset##_min); \ |
| 307 | static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 308 | show_in##offset##_max, set_in##offset##_max); |
| 309 | |
| 310 | show_in_offset(0); |
| 311 | show_in_offset(1); |
| 312 | show_in_offset(2); |
| 313 | show_in_offset(3); |
| 314 | show_in_offset(4); |
| 315 | show_in_offset(5); |
| 316 | |
| 317 | /* fans */ |
| 318 | static ssize_t show_fan(struct device *dev, char *buf, int nr) |
| 319 | { |
| 320 | struct adm9240_data *data = adm9240_update_device(dev); |
| 321 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
| 322 | 1 << data->fan_div[nr])); |
| 323 | } |
| 324 | |
| 325 | static ssize_t show_fan_min(struct device *dev, char *buf, int nr) |
| 326 | { |
| 327 | struct adm9240_data *data = adm9240_update_device(dev); |
| 328 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 329 | 1 << data->fan_div[nr])); |
| 330 | } |
| 331 | |
| 332 | static ssize_t show_fan_div(struct device *dev, char *buf, int nr) |
| 333 | { |
| 334 | struct adm9240_data *data = adm9240_update_device(dev); |
| 335 | return sprintf(buf, "%d\n", 1 << data->fan_div[nr]); |
| 336 | } |
| 337 | |
| 338 | /* write new fan div, callers must hold data->update_lock */ |
| 339 | static void adm9240_write_fan_div(struct i2c_client *client, int nr, |
| 340 | u8 fan_div) |
| 341 | { |
| 342 | u8 reg, old, shift = (nr + 2) * 2; |
| 343 | |
| 344 | reg = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV); |
| 345 | old = (reg >> shift) & 3; |
| 346 | reg &= ~(3 << shift); |
| 347 | reg |= (fan_div << shift); |
| 348 | adm9240_write_value(client, ADM9240_REG_VID_FAN_DIV, reg); |
| 349 | dev_dbg(&client->dev, "fan%d clock divider changed from %u " |
| 350 | "to %u\n", nr + 1, 1 << old, 1 << fan_div); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * set fan speed low limit: |
| 355 | * |
| 356 | * - value is zero: disable fan speed low limit alarm |
| 357 | * |
| 358 | * - value is below fan speed measurement range: enable fan speed low |
| 359 | * limit alarm to be asserted while fan speed too slow to measure |
| 360 | * |
| 361 | * - otherwise: select fan clock divider to suit fan speed low limit, |
| 362 | * measurement code may adjust registers to ensure fan speed reading |
| 363 | */ |
| 364 | static ssize_t set_fan_min(struct device *dev, const char *buf, |
| 365 | size_t count, int nr) |
| 366 | { |
| 367 | struct i2c_client *client = to_i2c_client(dev); |
| 368 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 369 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 370 | u8 new_div; |
| 371 | |
| 372 | down(&data->update_lock); |
| 373 | |
| 374 | if (!val) { |
| 375 | data->fan_min[nr] = 255; |
| 376 | new_div = data->fan_div[nr]; |
| 377 | |
| 378 | dev_dbg(&client->dev, "fan%u low limit set disabled\n", |
| 379 | nr + 1); |
| 380 | |
| 381 | } else if (val < 1350000 / (8 * 254)) { |
| 382 | new_div = 3; |
| 383 | data->fan_min[nr] = 254; |
| 384 | |
| 385 | dev_dbg(&client->dev, "fan%u low limit set minimum %u\n", |
| 386 | nr + 1, FAN_FROM_REG(254, 1 << new_div)); |
| 387 | |
| 388 | } else { |
| 389 | unsigned int new_min = 1350000 / val; |
| 390 | |
| 391 | new_div = 0; |
| 392 | while (new_min > 192 && new_div < 3) { |
| 393 | new_div++; |
| 394 | new_min /= 2; |
| 395 | } |
| 396 | if (!new_min) /* keep > 0 */ |
| 397 | new_min++; |
| 398 | |
| 399 | data->fan_min[nr] = new_min; |
| 400 | |
| 401 | dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n", |
| 402 | nr + 1, FAN_FROM_REG(new_min, 1 << new_div)); |
| 403 | } |
| 404 | |
| 405 | if (new_div != data->fan_div[nr]) { |
| 406 | data->fan_div[nr] = new_div; |
| 407 | adm9240_write_fan_div(client, nr, new_div); |
| 408 | } |
| 409 | adm9240_write_value(client, ADM9240_REG_FAN_MIN(nr), |
| 410 | data->fan_min[nr]); |
| 411 | |
| 412 | up(&data->update_lock); |
| 413 | return count; |
| 414 | } |
| 415 | |
| 416 | #define show_fan_offset(offset) \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 417 | static ssize_t show_fan_##offset (struct device *dev, \ |
| 418 | struct device_attribute *attr, \ |
| 419 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 420 | { \ |
| 421 | return show_fan(dev, buf, offset - 1); \ |
| 422 | } \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 423 | static ssize_t show_fan_##offset##_div (struct device *dev, \ |
| 424 | struct device_attribute *attr, \ |
| 425 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 426 | { \ |
| 427 | return show_fan_div(dev, buf, offset - 1); \ |
| 428 | } \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 429 | static ssize_t show_fan_##offset##_min (struct device *dev, \ |
| 430 | struct device_attribute *attr, \ |
| 431 | char *buf) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 432 | { \ |
| 433 | return show_fan_min(dev, buf, offset - 1); \ |
| 434 | } \ |
| 435 | static ssize_t set_fan_##offset##_min (struct device *dev, \ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 436 | struct device_attribute *attr, \ |
| 437 | const char *buf, size_t count) \ |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 438 | { \ |
| 439 | return set_fan_min(dev, buf, count, offset - 1); \ |
| 440 | } \ |
| 441 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 442 | show_fan_##offset, NULL); \ |
| 443 | static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \ |
| 444 | show_fan_##offset##_div, NULL); \ |
| 445 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 446 | show_fan_##offset##_min, set_fan_##offset##_min); |
| 447 | |
| 448 | show_fan_offset(1); |
| 449 | show_fan_offset(2); |
| 450 | |
| 451 | /* alarms */ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 452 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 453 | { |
| 454 | struct adm9240_data *data = adm9240_update_device(dev); |
| 455 | return sprintf(buf, "%u\n", data->alarms); |
| 456 | } |
| 457 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 458 | |
| 459 | /* vid */ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 460 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 461 | { |
| 462 | struct adm9240_data *data = adm9240_update_device(dev); |
| 463 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 464 | } |
| 465 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 466 | |
| 467 | /* analog output */ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 468 | static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 469 | { |
| 470 | struct adm9240_data *data = adm9240_update_device(dev); |
| 471 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 472 | } |
| 473 | |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 474 | static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 475 | { |
| 476 | struct i2c_client *client = to_i2c_client(dev); |
| 477 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 478 | unsigned long val = simple_strtol(buf, NULL, 10); |
| 479 | |
| 480 | down(&data->update_lock); |
| 481 | data->aout = AOUT_TO_REG(val); |
| 482 | adm9240_write_value(client, ADM9240_REG_ANALOG_OUT, data->aout); |
| 483 | up(&data->update_lock); |
| 484 | return count; |
| 485 | } |
| 486 | static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout); |
| 487 | |
| 488 | /* chassis_clear */ |
Greg Kroah-Hartman | 6f637a6 | 2005-06-21 21:01:59 -0700 | [diff] [blame] | 489 | static ssize_t chassis_clear(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 490 | { |
| 491 | struct i2c_client *client = to_i2c_client(dev); |
| 492 | unsigned long val = simple_strtol(buf, NULL, 10); |
| 493 | |
| 494 | if (val == 1) { |
| 495 | adm9240_write_value(client, ADM9240_REG_CHASSIS_CLEAR, 0x80); |
| 496 | dev_dbg(&client->dev, "chassis intrusion latch cleared\n"); |
| 497 | } |
| 498 | return count; |
| 499 | } |
| 500 | static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear); |
| 501 | |
| 502 | |
| 503 | /*** sensor chip detect and driver install ***/ |
| 504 | |
| 505 | static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind) |
| 506 | { |
| 507 | struct i2c_client *new_client; |
| 508 | struct adm9240_data *data; |
| 509 | int err = 0; |
| 510 | const char *name = ""; |
| 511 | u8 man_id, die_rev; |
| 512 | |
| 513 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 514 | goto exit; |
| 515 | |
| 516 | if (!(data = kmalloc(sizeof(struct adm9240_data), GFP_KERNEL))) { |
| 517 | err = -ENOMEM; |
| 518 | goto exit; |
| 519 | } |
| 520 | memset(data, 0, sizeof(struct adm9240_data)); |
| 521 | |
| 522 | new_client = &data->client; |
| 523 | i2c_set_clientdata(new_client, data); |
| 524 | new_client->addr = address; |
| 525 | new_client->adapter = adapter; |
| 526 | new_client->driver = &adm9240_driver; |
| 527 | new_client->flags = 0; |
| 528 | |
| 529 | if (kind == 0) { |
| 530 | kind = adm9240; |
| 531 | } |
| 532 | |
| 533 | if (kind < 0) { |
| 534 | |
| 535 | /* verify chip: reg address should match i2c address */ |
| 536 | if (adm9240_read_value(new_client, ADM9240_REG_I2C_ADDR) |
| 537 | != address) { |
| 538 | dev_err(&adapter->dev, "detect fail: address match, " |
| 539 | "0x%02x\n", address); |
| 540 | goto exit_free; |
| 541 | } |
| 542 | |
| 543 | /* check known chip manufacturer */ |
| 544 | man_id = adm9240_read_value(new_client, ADM9240_REG_MAN_ID); |
| 545 | |
| 546 | if (man_id == 0x23) { |
| 547 | kind = adm9240; |
| 548 | } else if (man_id == 0xda) { |
| 549 | kind = ds1780; |
| 550 | } else if (man_id == 0x01) { |
| 551 | kind = lm81; |
| 552 | } else { |
| 553 | dev_err(&adapter->dev, "detect fail: unknown manuf, " |
| 554 | "0x%02x\n", man_id); |
| 555 | goto exit_free; |
| 556 | } |
| 557 | |
| 558 | /* successful detect, print chip info */ |
| 559 | die_rev = adm9240_read_value(new_client, ADM9240_REG_DIE_REV); |
| 560 | dev_info(&adapter->dev, "found %s revision %u\n", |
| 561 | man_id == 0x23 ? "ADM9240" : |
| 562 | man_id == 0xda ? "DS1780" : "LM81", die_rev); |
| 563 | } |
| 564 | |
| 565 | /* either forced or detected chip kind */ |
| 566 | if (kind == adm9240) { |
| 567 | name = "adm9240"; |
| 568 | } else if (kind == ds1780) { |
| 569 | name = "ds1780"; |
| 570 | } else if (kind == lm81) { |
| 571 | name = "lm81"; |
| 572 | } |
| 573 | |
| 574 | /* fill in the remaining client fields and attach */ |
| 575 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 576 | data->type = kind; |
| 577 | init_MUTEX(&data->update_lock); |
| 578 | |
| 579 | if ((err = i2c_attach_client(new_client))) |
| 580 | goto exit_free; |
| 581 | |
| 582 | adm9240_init_client(new_client); |
| 583 | |
| 584 | /* populate sysfs filesystem */ |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 585 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 586 | if (IS_ERR(data->class_dev)) { |
| 587 | err = PTR_ERR(data->class_dev); |
| 588 | goto exit_detach; |
| 589 | } |
| 590 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 591 | device_create_file(&new_client->dev, &dev_attr_in0_input); |
| 592 | device_create_file(&new_client->dev, &dev_attr_in0_min); |
| 593 | device_create_file(&new_client->dev, &dev_attr_in0_max); |
| 594 | device_create_file(&new_client->dev, &dev_attr_in1_input); |
| 595 | device_create_file(&new_client->dev, &dev_attr_in1_min); |
| 596 | device_create_file(&new_client->dev, &dev_attr_in1_max); |
| 597 | device_create_file(&new_client->dev, &dev_attr_in2_input); |
| 598 | device_create_file(&new_client->dev, &dev_attr_in2_min); |
| 599 | device_create_file(&new_client->dev, &dev_attr_in2_max); |
| 600 | device_create_file(&new_client->dev, &dev_attr_in3_input); |
| 601 | device_create_file(&new_client->dev, &dev_attr_in3_min); |
| 602 | device_create_file(&new_client->dev, &dev_attr_in3_max); |
| 603 | device_create_file(&new_client->dev, &dev_attr_in4_input); |
| 604 | device_create_file(&new_client->dev, &dev_attr_in4_min); |
| 605 | device_create_file(&new_client->dev, &dev_attr_in4_max); |
| 606 | device_create_file(&new_client->dev, &dev_attr_in5_input); |
| 607 | device_create_file(&new_client->dev, &dev_attr_in5_min); |
| 608 | device_create_file(&new_client->dev, &dev_attr_in5_max); |
| 609 | device_create_file(&new_client->dev, &dev_attr_temp1_max); |
| 610 | device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst); |
| 611 | device_create_file(&new_client->dev, &dev_attr_temp1_input); |
| 612 | device_create_file(&new_client->dev, &dev_attr_fan1_input); |
| 613 | device_create_file(&new_client->dev, &dev_attr_fan1_div); |
| 614 | device_create_file(&new_client->dev, &dev_attr_fan1_min); |
| 615 | device_create_file(&new_client->dev, &dev_attr_fan2_input); |
| 616 | device_create_file(&new_client->dev, &dev_attr_fan2_div); |
| 617 | device_create_file(&new_client->dev, &dev_attr_fan2_min); |
| 618 | device_create_file(&new_client->dev, &dev_attr_alarms); |
| 619 | device_create_file(&new_client->dev, &dev_attr_aout_output); |
| 620 | device_create_file(&new_client->dev, &dev_attr_chassis_clear); |
| 621 | device_create_file(&new_client->dev, &dev_attr_cpu0_vid); |
| 622 | |
| 623 | return 0; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 624 | |
| 625 | exit_detach: |
| 626 | i2c_detach_client(new_client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 627 | exit_free: |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 628 | kfree(data); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 629 | exit: |
| 630 | return err; |
| 631 | } |
| 632 | |
| 633 | static int adm9240_attach_adapter(struct i2c_adapter *adapter) |
| 634 | { |
| 635 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 636 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 637 | return i2c_probe(adapter, &addr_data, adm9240_detect); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static int adm9240_detach_client(struct i2c_client *client) |
| 641 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 642 | struct adm9240_data *data = i2c_get_clientdata(client); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 643 | int err; |
| 644 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 645 | hwmon_device_unregister(data->class_dev); |
| 646 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 647 | if ((err = i2c_detach_client(client))) |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 648 | return err; |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 649 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 650 | kfree(data); |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static void adm9240_init_client(struct i2c_client *client) |
| 655 | { |
| 656 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 657 | u8 conf = adm9240_read_value(client, ADM9240_REG_CONFIG); |
| 658 | u8 mode = adm9240_read_value(client, ADM9240_REG_TEMP_CONF) & 3; |
| 659 | |
| 660 | data->vrm = i2c_which_vrm(); /* need this to report vid as mV */ |
| 661 | |
Grant Coady | 8e8f928 | 2005-05-13 20:26:10 +1000 | [diff] [blame] | 662 | dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10, |
| 663 | data->vrm % 10); |
| 664 | |
Grant Coady | 40b5cda | 2005-04-30 21:41:29 +1000 | [diff] [blame] | 665 | if (conf & 1) { /* measurement cycle running: report state */ |
| 666 | |
| 667 | dev_info(&client->dev, "status: config 0x%02x mode %u\n", |
| 668 | conf, mode); |
| 669 | |
| 670 | } else { /* cold start: open limits before starting chip */ |
| 671 | int i; |
| 672 | |
| 673 | for (i = 0; i < 6; i++) |
| 674 | { |
| 675 | adm9240_write_value(client, |
| 676 | ADM9240_REG_IN_MIN(i), 0); |
| 677 | adm9240_write_value(client, |
| 678 | ADM9240_REG_IN_MAX(i), 255); |
| 679 | } |
| 680 | adm9240_write_value(client, ADM9240_REG_FAN_MIN(0), 255); |
| 681 | adm9240_write_value(client, ADM9240_REG_FAN_MIN(1), 255); |
| 682 | adm9240_write_value(client, ADM9240_REG_TEMP_HIGH, 127); |
| 683 | adm9240_write_value(client, ADM9240_REG_TEMP_HYST, 127); |
| 684 | |
| 685 | /* start measurement cycle */ |
| 686 | adm9240_write_value(client, ADM9240_REG_CONFIG, 1); |
| 687 | |
| 688 | dev_info(&client->dev, "cold start: config was 0x%02x " |
| 689 | "mode %u\n", conf, mode); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | static struct adm9240_data *adm9240_update_device(struct device *dev) |
| 694 | { |
| 695 | struct i2c_client *client = to_i2c_client(dev); |
| 696 | struct adm9240_data *data = i2c_get_clientdata(client); |
| 697 | int i; |
| 698 | |
| 699 | down(&data->update_lock); |
| 700 | |
| 701 | /* minimum measurement cycle: 1.75 seconds */ |
| 702 | if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4)) |
| 703 | || !data->valid) { |
| 704 | |
| 705 | for (i = 0; i < 6; i++) /* read voltages */ |
| 706 | { |
| 707 | data->in[i] = adm9240_read_value(client, |
| 708 | ADM9240_REG_IN(i)); |
| 709 | } |
| 710 | data->alarms = adm9240_read_value(client, |
| 711 | ADM9240_REG_INT(0)) | |
| 712 | adm9240_read_value(client, |
| 713 | ADM9240_REG_INT(1)) << 8; |
| 714 | |
| 715 | /* read temperature: assume temperature changes less than |
| 716 | * 0.5'C per two measurement cycles thus ignore possible |
| 717 | * but unlikely aliasing error on lsb reading. --Grant */ |
| 718 | data->temp = ((adm9240_read_value(client, |
| 719 | ADM9240_REG_TEMP) << 8) | |
| 720 | adm9240_read_value(client, |
| 721 | ADM9240_REG_TEMP_CONF)) / 128; |
| 722 | |
| 723 | for (i = 0; i < 2; i++) /* read fans */ |
| 724 | { |
| 725 | data->fan[i] = adm9240_read_value(client, |
| 726 | ADM9240_REG_FAN(i)); |
| 727 | |
| 728 | /* adjust fan clock divider on overflow */ |
| 729 | if (data->valid && data->fan[i] == 255 && |
| 730 | data->fan_div[i] < 3) { |
| 731 | |
| 732 | adm9240_write_fan_div(client, i, |
| 733 | ++data->fan_div[i]); |
| 734 | |
| 735 | /* adjust fan_min if active, but not to 0 */ |
| 736 | if (data->fan_min[i] < 255 && |
| 737 | data->fan_min[i] >= 2) |
| 738 | data->fan_min[i] /= 2; |
| 739 | } |
| 740 | } |
| 741 | data->last_updated_measure = jiffies; |
| 742 | } |
| 743 | |
| 744 | /* minimum config reading cycle: 300 seconds */ |
| 745 | if (time_after(jiffies, data->last_updated_config + (HZ * 300)) |
| 746 | || !data->valid) { |
| 747 | |
| 748 | for (i = 0; i < 6; i++) |
| 749 | { |
| 750 | data->in_min[i] = adm9240_read_value(client, |
| 751 | ADM9240_REG_IN_MIN(i)); |
| 752 | data->in_max[i] = adm9240_read_value(client, |
| 753 | ADM9240_REG_IN_MAX(i)); |
| 754 | } |
| 755 | for (i = 0; i < 2; i++) |
| 756 | { |
| 757 | data->fan_min[i] = adm9240_read_value(client, |
| 758 | ADM9240_REG_FAN_MIN(i)); |
| 759 | } |
| 760 | data->temp_high = adm9240_read_value(client, |
| 761 | ADM9240_REG_TEMP_HIGH); |
| 762 | data->temp_hyst = adm9240_read_value(client, |
| 763 | ADM9240_REG_TEMP_HYST); |
| 764 | |
| 765 | /* read fan divs and 5-bit VID */ |
| 766 | i = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV); |
| 767 | data->fan_div[0] = (i >> 4) & 3; |
| 768 | data->fan_div[1] = (i >> 6) & 3; |
| 769 | data->vid = i & 0x0f; |
| 770 | data->vid |= (adm9240_read_value(client, |
| 771 | ADM9240_REG_VID4) & 1) << 4; |
| 772 | /* read analog out */ |
| 773 | data->aout = adm9240_read_value(client, |
| 774 | ADM9240_REG_ANALOG_OUT); |
| 775 | |
| 776 | data->last_updated_config = jiffies; |
| 777 | data->valid = 1; |
| 778 | } |
| 779 | up(&data->update_lock); |
| 780 | return data; |
| 781 | } |
| 782 | |
| 783 | static int __init sensors_adm9240_init(void) |
| 784 | { |
| 785 | return i2c_add_driver(&adm9240_driver); |
| 786 | } |
| 787 | |
| 788 | static void __exit sensors_adm9240_exit(void) |
| 789 | { |
| 790 | i2c_del_driver(&adm9240_driver); |
| 791 | } |
| 792 | |
| 793 | MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, " |
| 794 | "Grant Coady <gcoady@gmail.com> and others"); |
| 795 | MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver"); |
| 796 | MODULE_LICENSE("GPL"); |
| 797 | |
| 798 | module_init(sensors_adm9240_init); |
| 799 | module_exit(sensors_adm9240_exit); |
| 800 | |