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