Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm80.c - From lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
| 5 | * and Philip Edelbrock <phil@netroedge.com> |
| 6 | * |
| 7 | * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org> |
| 8 | * |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 30 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /* Addresses to scan */ |
| 34 | static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, |
| 35 | 0x2d, 0x2e, 0x2f, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 38 | I2C_CLIENT_INSMOD_1(lm80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* Many LM80 constants specified below */ |
| 41 | |
| 42 | /* The LM80 registers */ |
| 43 | #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2) |
| 44 | #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2) |
| 45 | #define LM80_REG_IN(nr) (0x20 + (nr)) |
| 46 | |
| 47 | #define LM80_REG_FAN1 0x28 |
| 48 | #define LM80_REG_FAN2 0x29 |
| 49 | #define LM80_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 50 | |
| 51 | #define LM80_REG_TEMP 0x27 |
| 52 | #define LM80_REG_TEMP_HOT_MAX 0x38 |
| 53 | #define LM80_REG_TEMP_HOT_HYST 0x39 |
| 54 | #define LM80_REG_TEMP_OS_MAX 0x3a |
| 55 | #define LM80_REG_TEMP_OS_HYST 0x3b |
| 56 | |
| 57 | #define LM80_REG_CONFIG 0x00 |
| 58 | #define LM80_REG_ALARM1 0x01 |
| 59 | #define LM80_REG_ALARM2 0x02 |
| 60 | #define LM80_REG_MASK1 0x03 |
| 61 | #define LM80_REG_MASK2 0x04 |
| 62 | #define LM80_REG_FANDIV 0x05 |
| 63 | #define LM80_REG_RES 0x06 |
| 64 | |
| 65 | |
| 66 | /* Conversions. Rounding and limit checking is only done on the TO_REG |
| 67 | variants. Note that you should be a bit careful with which arguments |
| 68 | these macros are called: arguments may be evaluated more than once. |
| 69 | Fixing this is just not worth it. */ |
| 70 | |
| 71 | #define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255)) |
| 72 | #define IN_FROM_REG(val) ((val)*10) |
| 73 | |
| 74 | static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div) |
| 75 | { |
| 76 | if (rpm == 0) |
| 77 | return 255; |
| 78 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); |
| 79 | return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254); |
| 80 | } |
| 81 | |
| 82 | #define FAN_FROM_REG(val,div) ((val)==0?-1:\ |
| 83 | (val)==255?0:1350000/((div)*(val))) |
| 84 | |
| 85 | static inline long TEMP_FROM_REG(u16 temp) |
| 86 | { |
| 87 | long res; |
| 88 | |
| 89 | temp >>= 4; |
| 90 | if (temp < 0x0800) |
| 91 | res = 625 * (long) temp; |
| 92 | else |
| 93 | res = ((long) temp - 0x01000) * 625; |
| 94 | |
| 95 | return res / 10; |
| 96 | } |
| 97 | |
| 98 | #define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000) |
| 99 | |
| 100 | #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\ |
| 101 | ((val)-500)/1000:((val)+500)/1000,0,255) |
| 102 | |
| 103 | #define DIV_FROM_REG(val) (1 << (val)) |
| 104 | |
| 105 | /* |
| 106 | * Client data (each client gets its own) |
| 107 | */ |
| 108 | |
| 109 | struct lm80_data { |
| 110 | struct i2c_client client; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 111 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 112 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | char valid; /* !=0 if following fields are valid */ |
| 114 | unsigned long last_updated; /* In jiffies */ |
| 115 | |
| 116 | u8 in[7]; /* Register value */ |
| 117 | u8 in_max[7]; /* Register value */ |
| 118 | u8 in_min[7]; /* Register value */ |
| 119 | u8 fan[2]; /* Register value */ |
| 120 | u8 fan_min[2]; /* Register value */ |
| 121 | u8 fan_div[2]; /* Register encoding, shifted right */ |
| 122 | u16 temp; /* Register values, shifted right */ |
| 123 | u8 temp_hot_max; /* Register value */ |
| 124 | u8 temp_hot_hyst; /* Register value */ |
| 125 | u8 temp_os_max; /* Register value */ |
| 126 | u8 temp_os_hyst; /* Register value */ |
| 127 | u16 alarms; /* Register encoding, combined */ |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * Functions declaration |
| 132 | */ |
| 133 | |
| 134 | static int lm80_attach_adapter(struct i2c_adapter *adapter); |
| 135 | static int lm80_detect(struct i2c_adapter *adapter, int address, int kind); |
| 136 | static void lm80_init_client(struct i2c_client *client); |
| 137 | static int lm80_detach_client(struct i2c_client *client); |
| 138 | static struct lm80_data *lm80_update_device(struct device *dev); |
| 139 | static int lm80_read_value(struct i2c_client *client, u8 reg); |
| 140 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); |
| 141 | |
| 142 | /* |
| 143 | * Driver data (common to all clients) |
| 144 | */ |
| 145 | |
| 146 | static struct i2c_driver lm80_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 147 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 148 | .name = "lm80", |
| 149 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | .id = I2C_DRIVERID_LM80, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | .attach_adapter = lm80_attach_adapter, |
| 152 | .detach_client = lm80_detach_client, |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * Sysfs stuff |
| 157 | */ |
| 158 | |
| 159 | #define show_in(suffix, value) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 160 | static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { \ |
| 162 | struct lm80_data *data = lm80_update_device(dev); \ |
| 163 | return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \ |
| 164 | } |
| 165 | show_in(min0, in_min[0]); |
| 166 | show_in(min1, in_min[1]); |
| 167 | show_in(min2, in_min[2]); |
| 168 | show_in(min3, in_min[3]); |
| 169 | show_in(min4, in_min[4]); |
| 170 | show_in(min5, in_min[5]); |
| 171 | show_in(min6, in_min[6]); |
| 172 | show_in(max0, in_max[0]); |
| 173 | show_in(max1, in_max[1]); |
| 174 | show_in(max2, in_max[2]); |
| 175 | show_in(max3, in_max[3]); |
| 176 | show_in(max4, in_max[4]); |
| 177 | show_in(max5, in_max[5]); |
| 178 | show_in(max6, in_max[6]); |
| 179 | show_in(input0, in[0]); |
| 180 | show_in(input1, in[1]); |
| 181 | show_in(input2, in[2]); |
| 182 | show_in(input3, in[3]); |
| 183 | show_in(input4, in[4]); |
| 184 | show_in(input5, in[5]); |
| 185 | show_in(input6, in[6]); |
| 186 | |
| 187 | #define set_in(suffix, value, reg) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 188 | static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | size_t count) \ |
| 190 | { \ |
| 191 | struct i2c_client *client = to_i2c_client(dev); \ |
| 192 | struct lm80_data *data = i2c_get_clientdata(client); \ |
| 193 | long val = simple_strtol(buf, NULL, 10); \ |
| 194 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 195 | mutex_lock(&data->update_lock);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | data->value = IN_TO_REG(val); \ |
| 197 | lm80_write_value(client, reg, data->value); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 198 | mutex_unlock(&data->update_lock);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return count; \ |
| 200 | } |
| 201 | set_in(min0, in_min[0], LM80_REG_IN_MIN(0)); |
| 202 | set_in(min1, in_min[1], LM80_REG_IN_MIN(1)); |
| 203 | set_in(min2, in_min[2], LM80_REG_IN_MIN(2)); |
| 204 | set_in(min3, in_min[3], LM80_REG_IN_MIN(3)); |
| 205 | set_in(min4, in_min[4], LM80_REG_IN_MIN(4)); |
| 206 | set_in(min5, in_min[5], LM80_REG_IN_MIN(5)); |
| 207 | set_in(min6, in_min[6], LM80_REG_IN_MIN(6)); |
| 208 | set_in(max0, in_max[0], LM80_REG_IN_MAX(0)); |
| 209 | set_in(max1, in_max[1], LM80_REG_IN_MAX(1)); |
| 210 | set_in(max2, in_max[2], LM80_REG_IN_MAX(2)); |
| 211 | set_in(max3, in_max[3], LM80_REG_IN_MAX(3)); |
| 212 | set_in(max4, in_max[4], LM80_REG_IN_MAX(4)); |
| 213 | set_in(max5, in_max[5], LM80_REG_IN_MAX(5)); |
| 214 | set_in(max6, in_max[6], LM80_REG_IN_MAX(6)); |
| 215 | |
| 216 | #define show_fan(suffix, value, div) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 217 | static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { \ |
| 219 | struct lm80_data *data = lm80_update_device(dev); \ |
| 220 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \ |
| 221 | DIV_FROM_REG(data->div))); \ |
| 222 | } |
| 223 | show_fan(min1, fan_min[0], fan_div[0]); |
| 224 | show_fan(min2, fan_min[1], fan_div[1]); |
| 225 | show_fan(input1, fan[0], fan_div[0]); |
| 226 | show_fan(input2, fan[1], fan_div[1]); |
| 227 | |
| 228 | #define show_fan_div(suffix, value) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 229 | static ssize_t show_fan_div##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { \ |
| 231 | struct lm80_data *data = lm80_update_device(dev); \ |
| 232 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \ |
| 233 | } |
| 234 | show_fan_div(1, fan_div[0]); |
| 235 | show_fan_div(2, fan_div[1]); |
| 236 | |
| 237 | #define set_fan(suffix, value, reg, div) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 238 | static ssize_t set_fan_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | size_t count) \ |
| 240 | { \ |
| 241 | struct i2c_client *client = to_i2c_client(dev); \ |
| 242 | struct lm80_data *data = i2c_get_clientdata(client); \ |
| 243 | long val = simple_strtoul(buf, NULL, 10); \ |
| 244 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 245 | mutex_lock(&data->update_lock);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \ |
| 247 | lm80_write_value(client, reg, data->value); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 248 | mutex_unlock(&data->update_lock);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return count; \ |
| 250 | } |
| 251 | set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]); |
| 252 | set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]); |
| 253 | |
| 254 | /* Note: we save and restore the fan minimum here, because its value is |
| 255 | determined in part by the fan divisor. This follows the principle of |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 256 | least surprise; the user doesn't expect the fan minimum to change just |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | because the divisor changed. */ |
| 258 | static ssize_t set_fan_div(struct device *dev, const char *buf, |
| 259 | size_t count, int nr) |
| 260 | { |
| 261 | struct i2c_client *client = to_i2c_client(dev); |
| 262 | struct lm80_data *data = i2c_get_clientdata(client); |
| 263 | unsigned long min, val = simple_strtoul(buf, NULL, 10); |
| 264 | u8 reg; |
| 265 | |
| 266 | /* Save fan_min */ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 267 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | min = FAN_FROM_REG(data->fan_min[nr], |
| 269 | DIV_FROM_REG(data->fan_div[nr])); |
| 270 | |
| 271 | switch (val) { |
| 272 | case 1: data->fan_div[nr] = 0; break; |
| 273 | case 2: data->fan_div[nr] = 1; break; |
| 274 | case 4: data->fan_div[nr] = 2; break; |
| 275 | case 8: data->fan_div[nr] = 3; break; |
| 276 | default: |
| 277 | dev_err(&client->dev, "fan_div value %ld not " |
| 278 | "supported. Choose one of 1, 2, 4 or 8!\n", val); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 279 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1)))) |
| 284 | | (data->fan_div[nr] << (2 * (nr + 1))); |
| 285 | lm80_write_value(client, LM80_REG_FANDIV, reg); |
| 286 | |
| 287 | /* Restore fan_min */ |
| 288 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 289 | lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 290 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | return count; |
| 293 | } |
| 294 | |
| 295 | #define set_fan_div(number) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 296 | static ssize_t set_fan_div##number(struct device *dev, struct device_attribute *attr, const char *buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | size_t count) \ |
| 298 | { \ |
| 299 | return set_fan_div(dev, buf, count, number - 1); \ |
| 300 | } |
| 301 | set_fan_div(1); |
| 302 | set_fan_div(2); |
| 303 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 304 | static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | struct lm80_data *data = lm80_update_device(dev); |
| 307 | return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp)); |
| 308 | } |
| 309 | |
| 310 | #define show_temp(suffix, value) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 311 | static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { \ |
| 313 | struct lm80_data *data = lm80_update_device(dev); \ |
| 314 | return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \ |
| 315 | } |
| 316 | show_temp(hot_max, temp_hot_max); |
| 317 | show_temp(hot_hyst, temp_hot_hyst); |
| 318 | show_temp(os_max, temp_os_max); |
| 319 | show_temp(os_hyst, temp_os_hyst); |
| 320 | |
| 321 | #define set_temp(suffix, value, reg) \ |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 322 | static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | size_t count) \ |
| 324 | { \ |
| 325 | struct i2c_client *client = to_i2c_client(dev); \ |
| 326 | struct lm80_data *data = i2c_get_clientdata(client); \ |
| 327 | long val = simple_strtoul(buf, NULL, 10); \ |
| 328 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 329 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | data->value = TEMP_LIMIT_TO_REG(val); \ |
| 331 | lm80_write_value(client, reg, data->value); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 332 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return count; \ |
| 334 | } |
| 335 | set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX); |
| 336 | set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST); |
| 337 | set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX); |
| 338 | set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST); |
| 339 | |
Yani Ioannou | 8627f9b | 2005-05-17 06:42:03 -0400 | [diff] [blame] | 340 | 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] | 341 | { |
| 342 | struct lm80_data *data = lm80_update_device(dev); |
| 343 | return sprintf(buf, "%u\n", data->alarms); |
| 344 | } |
| 345 | |
| 346 | static DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min0, set_in_min0); |
| 347 | static DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min1, set_in_min1); |
| 348 | static DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min2, set_in_min2); |
| 349 | static DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min3, set_in_min3); |
| 350 | static DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min4, set_in_min4); |
| 351 | static DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min5, set_in_min5); |
| 352 | static DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min6, set_in_min6); |
| 353 | static DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max0, set_in_max0); |
| 354 | static DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max1, set_in_max1); |
| 355 | static DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max2, set_in_max2); |
| 356 | static DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max3, set_in_max3); |
| 357 | static DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max4, set_in_max4); |
| 358 | static DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max5, set_in_max5); |
| 359 | static DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max6, set_in_max6); |
| 360 | static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL); |
| 361 | static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL); |
| 362 | static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL); |
| 363 | static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL); |
| 364 | static DEVICE_ATTR(in4_input, S_IRUGO, show_in_input4, NULL); |
| 365 | static DEVICE_ATTR(in5_input, S_IRUGO, show_in_input5, NULL); |
| 366 | static DEVICE_ATTR(in6_input, S_IRUGO, show_in_input6, NULL); |
| 367 | static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min1, |
| 368 | set_fan_min1); |
| 369 | static DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min2, |
| 370 | set_fan_min2); |
| 371 | static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL); |
| 372 | static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL); |
| 373 | static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div1, set_fan_div1); |
| 374 | static DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div2, set_fan_div2); |
| 375 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); |
| 376 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max, |
| 377 | set_temp_hot_max); |
| 378 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst, |
| 379 | set_temp_hot_hyst); |
| 380 | static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max, |
| 381 | set_temp_os_max); |
| 382 | static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst, |
| 383 | set_temp_os_hyst); |
| 384 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 385 | |
| 386 | /* |
| 387 | * Real code |
| 388 | */ |
| 389 | |
| 390 | static int lm80_attach_adapter(struct i2c_adapter *adapter) |
| 391 | { |
| 392 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 393 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 394 | return i2c_probe(adapter, &addr_data, lm80_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 397 | static struct attribute *lm80_attributes[] = { |
| 398 | &dev_attr_in0_min.attr, |
| 399 | &dev_attr_in1_min.attr, |
| 400 | &dev_attr_in2_min.attr, |
| 401 | &dev_attr_in3_min.attr, |
| 402 | &dev_attr_in4_min.attr, |
| 403 | &dev_attr_in5_min.attr, |
| 404 | &dev_attr_in6_min.attr, |
| 405 | &dev_attr_in0_max.attr, |
| 406 | &dev_attr_in1_max.attr, |
| 407 | &dev_attr_in2_max.attr, |
| 408 | &dev_attr_in3_max.attr, |
| 409 | &dev_attr_in4_max.attr, |
| 410 | &dev_attr_in5_max.attr, |
| 411 | &dev_attr_in6_max.attr, |
| 412 | &dev_attr_in0_input.attr, |
| 413 | &dev_attr_in1_input.attr, |
| 414 | &dev_attr_in2_input.attr, |
| 415 | &dev_attr_in3_input.attr, |
| 416 | &dev_attr_in4_input.attr, |
| 417 | &dev_attr_in5_input.attr, |
| 418 | &dev_attr_in6_input.attr, |
| 419 | &dev_attr_fan1_min.attr, |
| 420 | &dev_attr_fan2_min.attr, |
| 421 | &dev_attr_fan1_input.attr, |
| 422 | &dev_attr_fan2_input.attr, |
| 423 | &dev_attr_fan1_div.attr, |
| 424 | &dev_attr_fan2_div.attr, |
| 425 | &dev_attr_temp1_input.attr, |
| 426 | &dev_attr_temp1_max.attr, |
| 427 | &dev_attr_temp1_max_hyst.attr, |
| 428 | &dev_attr_temp1_crit.attr, |
| 429 | &dev_attr_temp1_crit_hyst.attr, |
| 430 | &dev_attr_alarms.attr, |
| 431 | |
| 432 | NULL |
| 433 | }; |
| 434 | |
| 435 | static const struct attribute_group lm80_group = { |
| 436 | .attrs = lm80_attributes, |
| 437 | }; |
| 438 | |
Ben Dooks | c49efce | 2005-10-26 21:07:25 +0200 | [diff] [blame] | 439 | static int lm80_detect(struct i2c_adapter *adapter, int address, int kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | int i, cur; |
| 442 | struct i2c_client *new_client; |
| 443 | struct lm80_data *data; |
| 444 | int err = 0; |
| 445 | const char *name; |
| 446 | |
| 447 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 448 | goto exit; |
| 449 | |
| 450 | /* OK. For now, we presume we have a valid client. We now create the |
| 451 | client structure, even though we cannot fill it completely yet. |
| 452 | But it allows us to access lm80_{read,write}_value. */ |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 453 | if (!(data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | err = -ENOMEM; |
| 455 | goto exit; |
| 456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | new_client = &data->client; |
| 459 | i2c_set_clientdata(new_client, data); |
| 460 | new_client->addr = address; |
| 461 | new_client->adapter = adapter; |
| 462 | new_client->driver = &lm80_driver; |
| 463 | new_client->flags = 0; |
| 464 | |
| 465 | /* Now, we do the remaining detection. It is lousy. */ |
| 466 | if (lm80_read_value(new_client, LM80_REG_ALARM2) & 0xc0) |
| 467 | goto error_free; |
| 468 | for (i = 0x2a; i <= 0x3d; i++) { |
| 469 | cur = i2c_smbus_read_byte_data(new_client, i); |
| 470 | if ((i2c_smbus_read_byte_data(new_client, i + 0x40) != cur) |
| 471 | || (i2c_smbus_read_byte_data(new_client, i + 0x80) != cur) |
| 472 | || (i2c_smbus_read_byte_data(new_client, i + 0xc0) != cur)) |
| 473 | goto error_free; |
| 474 | } |
| 475 | |
| 476 | /* Determine the chip type - only one kind supported! */ |
| 477 | kind = lm80; |
| 478 | name = "lm80"; |
| 479 | |
| 480 | /* Fill in the remaining client fields and put it into the global list */ |
| 481 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 482 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 483 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 485 | /* Tell the I2C layer a new client has arrived */ |
| 486 | if ((err = i2c_attach_client(new_client))) |
| 487 | goto error_free; |
| 488 | |
| 489 | /* Initialize the LM80 chip */ |
| 490 | lm80_init_client(new_client); |
| 491 | |
| 492 | /* A few vars need to be filled upon startup */ |
| 493 | data->fan_min[0] = lm80_read_value(new_client, LM80_REG_FAN_MIN(1)); |
| 494 | data->fan_min[1] = lm80_read_value(new_client, LM80_REG_FAN_MIN(2)); |
| 495 | |
| 496 | /* Register sysfs hooks */ |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 497 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm80_group))) |
| 498 | goto error_detach; |
| 499 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 500 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 501 | if (IS_ERR(data->hwmon_dev)) { |
| 502 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 503 | goto error_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 504 | } |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 508 | error_remove: |
| 509 | sysfs_remove_group(&new_client->dev.kobj, &lm80_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 510 | error_detach: |
| 511 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | error_free: |
| 513 | kfree(data); |
| 514 | exit: |
| 515 | return err; |
| 516 | } |
| 517 | |
| 518 | static int lm80_detach_client(struct i2c_client *client) |
| 519 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 520 | struct lm80_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | int err; |
| 522 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 523 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 524 | sysfs_remove_group(&client->dev.kobj, &lm80_group); |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 525 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 528 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int lm80_read_value(struct i2c_client *client, u8 reg) |
| 533 | { |
| 534 | return i2c_smbus_read_byte_data(client, reg); |
| 535 | } |
| 536 | |
| 537 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 538 | { |
| 539 | return i2c_smbus_write_byte_data(client, reg, value); |
| 540 | } |
| 541 | |
| 542 | /* Called when we have found a new LM80. */ |
| 543 | static void lm80_init_client(struct i2c_client *client) |
| 544 | { |
| 545 | /* Reset all except Watchdog values and last conversion values |
| 546 | This sets fan-divs to 2, among others. This makes most other |
| 547 | initializations unnecessary */ |
| 548 | lm80_write_value(client, LM80_REG_CONFIG, 0x80); |
| 549 | /* Set 11-bit temperature resolution */ |
| 550 | lm80_write_value(client, LM80_REG_RES, 0x08); |
| 551 | |
| 552 | /* Start monitoring */ |
| 553 | lm80_write_value(client, LM80_REG_CONFIG, 0x01); |
| 554 | } |
| 555 | |
| 556 | static struct lm80_data *lm80_update_device(struct device *dev) |
| 557 | { |
| 558 | struct i2c_client *client = to_i2c_client(dev); |
| 559 | struct lm80_data *data = i2c_get_clientdata(client); |
| 560 | int i; |
| 561 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 562 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { |
| 565 | dev_dbg(&client->dev, "Starting lm80 update\n"); |
| 566 | for (i = 0; i <= 6; i++) { |
| 567 | data->in[i] = |
| 568 | lm80_read_value(client, LM80_REG_IN(i)); |
| 569 | data->in_min[i] = |
| 570 | lm80_read_value(client, LM80_REG_IN_MIN(i)); |
| 571 | data->in_max[i] = |
| 572 | lm80_read_value(client, LM80_REG_IN_MAX(i)); |
| 573 | } |
| 574 | data->fan[0] = lm80_read_value(client, LM80_REG_FAN1); |
| 575 | data->fan_min[0] = |
| 576 | lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
| 577 | data->fan[1] = lm80_read_value(client, LM80_REG_FAN2); |
| 578 | data->fan_min[1] = |
| 579 | lm80_read_value(client, LM80_REG_FAN_MIN(2)); |
| 580 | |
| 581 | data->temp = |
| 582 | (lm80_read_value(client, LM80_REG_TEMP) << 8) | |
| 583 | (lm80_read_value(client, LM80_REG_RES) & 0xf0); |
| 584 | data->temp_os_max = |
| 585 | lm80_read_value(client, LM80_REG_TEMP_OS_MAX); |
| 586 | data->temp_os_hyst = |
| 587 | lm80_read_value(client, LM80_REG_TEMP_OS_HYST); |
| 588 | data->temp_hot_max = |
| 589 | lm80_read_value(client, LM80_REG_TEMP_HOT_MAX); |
| 590 | data->temp_hot_hyst = |
| 591 | lm80_read_value(client, LM80_REG_TEMP_HOT_HYST); |
| 592 | |
| 593 | i = lm80_read_value(client, LM80_REG_FANDIV); |
| 594 | data->fan_div[0] = (i >> 2) & 0x03; |
| 595 | data->fan_div[1] = (i >> 4) & 0x03; |
| 596 | data->alarms = lm80_read_value(client, LM80_REG_ALARM1) + |
| 597 | (lm80_read_value(client, LM80_REG_ALARM2) << 8); |
| 598 | data->last_updated = jiffies; |
| 599 | data->valid = 1; |
| 600 | } |
| 601 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 602 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | return data; |
| 605 | } |
| 606 | |
| 607 | static int __init sensors_lm80_init(void) |
| 608 | { |
| 609 | return i2c_add_driver(&lm80_driver); |
| 610 | } |
| 611 | |
| 612 | static void __exit sensors_lm80_exit(void) |
| 613 | { |
| 614 | i2c_del_driver(&lm80_driver); |
| 615 | } |
| 616 | |
| 617 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
| 618 | "Philip Edelbrock <phil@netroedge.com>"); |
| 619 | MODULE_DESCRIPTION("LM80 driver"); |
| 620 | MODULE_LICENSE("GPL"); |
| 621 | |
| 622 | module_init(sensors_lm80_init); |
| 623 | module_exit(sensors_lm80_exit); |