Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated |
| 3 | * hardware monitoring features |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 4 | * Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org> |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 5 | * |
| 6 | * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates |
| 7 | * complete hardware monitoring features: voltage, fan and temperature |
| 8 | * sensors, and manual and automatic fan speed control. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/hwmon.h> |
| 31 | #include <linux/hwmon-sysfs.h> |
| 32 | #include <linux/err.h> |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 34 | #include <asm/io.h> |
| 35 | |
| 36 | static struct platform_device *pdev; |
| 37 | |
| 38 | #define DRVNAME "f71805f" |
| 39 | |
| 40 | /* |
| 41 | * Super-I/O constants and functions |
| 42 | */ |
| 43 | |
| 44 | #define F71805F_LD_HWM 0x04 |
| 45 | |
| 46 | #define SIO_REG_LDSEL 0x07 /* Logical device select */ |
| 47 | #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */ |
| 48 | #define SIO_REG_DEVREV 0x22 /* Device revision */ |
| 49 | #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */ |
| 50 | #define SIO_REG_ENABLE 0x30 /* Logical device enable */ |
| 51 | #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */ |
| 52 | |
| 53 | #define SIO_FINTEK_ID 0x1934 |
| 54 | #define SIO_F71805F_ID 0x0406 |
| 55 | |
| 56 | static inline int |
| 57 | superio_inb(int base, int reg) |
| 58 | { |
| 59 | outb(reg, base); |
| 60 | return inb(base + 1); |
| 61 | } |
| 62 | |
| 63 | static int |
| 64 | superio_inw(int base, int reg) |
| 65 | { |
| 66 | int val; |
| 67 | outb(reg++, base); |
| 68 | val = inb(base + 1) << 8; |
| 69 | outb(reg, base); |
| 70 | val |= inb(base + 1); |
| 71 | return val; |
| 72 | } |
| 73 | |
| 74 | static inline void |
| 75 | superio_select(int base, int ld) |
| 76 | { |
| 77 | outb(SIO_REG_LDSEL, base); |
| 78 | outb(ld, base + 1); |
| 79 | } |
| 80 | |
| 81 | static inline void |
| 82 | superio_enter(int base) |
| 83 | { |
| 84 | outb(0x87, base); |
| 85 | outb(0x87, base); |
| 86 | } |
| 87 | |
| 88 | static inline void |
| 89 | superio_exit(int base) |
| 90 | { |
| 91 | outb(0xaa, base); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * ISA constants |
| 96 | */ |
| 97 | |
| 98 | #define REGION_LENGTH 2 |
| 99 | #define ADDR_REG_OFFSET 0 |
| 100 | #define DATA_REG_OFFSET 1 |
| 101 | |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 102 | /* |
| 103 | * Registers |
| 104 | */ |
| 105 | |
| 106 | /* in nr from 0 to 8 (8-bit values) */ |
| 107 | #define F71805F_REG_IN(nr) (0x10 + (nr)) |
| 108 | #define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr)) |
| 109 | #define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr)) |
| 110 | /* fan nr from 0 to 2 (12-bit values, two registers) */ |
| 111 | #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr)) |
| 112 | #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr)) |
| 113 | #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr)) |
| 114 | /* temp nr from 0 to 2 (8-bit values) */ |
| 115 | #define F71805F_REG_TEMP(nr) (0x1B + (nr)) |
| 116 | #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr)) |
| 117 | #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr)) |
| 118 | #define F71805F_REG_TEMP_MODE 0x01 |
| 119 | |
| 120 | #define F71805F_REG_START 0x00 |
| 121 | /* status nr from 0 to 2 */ |
| 122 | #define F71805F_REG_STATUS(nr) (0x36 + (nr)) |
| 123 | |
| 124 | /* |
| 125 | * Data structures and manipulation thereof |
| 126 | */ |
| 127 | |
| 128 | struct f71805f_data { |
| 129 | unsigned short addr; |
| 130 | const char *name; |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 131 | struct mutex lock; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 132 | struct class_device *class_dev; |
| 133 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 134 | struct mutex update_lock; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 135 | char valid; /* !=0 if following fields are valid */ |
| 136 | unsigned long last_updated; /* In jiffies */ |
| 137 | unsigned long last_limits; /* In jiffies */ |
| 138 | |
| 139 | /* Register values */ |
| 140 | u8 in[9]; |
| 141 | u8 in_high[9]; |
| 142 | u8 in_low[9]; |
| 143 | u16 fan[3]; |
| 144 | u16 fan_low[3]; |
| 145 | u8 fan_enabled; /* Read once at init time */ |
| 146 | u8 temp[3]; |
| 147 | u8 temp_high[3]; |
| 148 | u8 temp_hyst[3]; |
| 149 | u8 temp_mode; |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 150 | unsigned long alarms; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static inline long in_from_reg(u8 reg) |
| 154 | { |
| 155 | return (reg * 8); |
| 156 | } |
| 157 | |
| 158 | /* The 2 least significant bits are not used */ |
| 159 | static inline u8 in_to_reg(long val) |
| 160 | { |
| 161 | if (val <= 0) |
| 162 | return 0; |
| 163 | if (val >= 2016) |
| 164 | return 0xfc; |
| 165 | return (((val + 16) / 32) << 2); |
| 166 | } |
| 167 | |
| 168 | /* in0 is downscaled by a factor 2 internally */ |
| 169 | static inline long in0_from_reg(u8 reg) |
| 170 | { |
| 171 | return (reg * 16); |
| 172 | } |
| 173 | |
| 174 | static inline u8 in0_to_reg(long val) |
| 175 | { |
| 176 | if (val <= 0) |
| 177 | return 0; |
| 178 | if (val >= 4032) |
| 179 | return 0xfc; |
| 180 | return (((val + 32) / 64) << 2); |
| 181 | } |
| 182 | |
| 183 | /* The 4 most significant bits are not used */ |
| 184 | static inline long fan_from_reg(u16 reg) |
| 185 | { |
| 186 | reg &= 0xfff; |
| 187 | if (!reg || reg == 0xfff) |
| 188 | return 0; |
| 189 | return (1500000 / reg); |
| 190 | } |
| 191 | |
| 192 | static inline u16 fan_to_reg(long rpm) |
| 193 | { |
| 194 | /* If the low limit is set below what the chip can measure, |
| 195 | store the largest possible 12-bit value in the registers, |
| 196 | so that no alarm will ever trigger. */ |
| 197 | if (rpm < 367) |
| 198 | return 0xfff; |
| 199 | return (1500000 / rpm); |
| 200 | } |
| 201 | |
| 202 | static inline long temp_from_reg(u8 reg) |
| 203 | { |
| 204 | return (reg * 1000); |
| 205 | } |
| 206 | |
| 207 | static inline u8 temp_to_reg(long val) |
| 208 | { |
| 209 | if (val < 0) |
| 210 | val = 0; |
| 211 | else if (val > 1000 * 0xff) |
| 212 | val = 0xff; |
| 213 | return ((val + 500) / 1000); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Device I/O access |
| 218 | */ |
| 219 | |
| 220 | static u8 f71805f_read8(struct f71805f_data *data, u8 reg) |
| 221 | { |
| 222 | u8 val; |
| 223 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 224 | mutex_lock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 225 | outb(reg, data->addr + ADDR_REG_OFFSET); |
| 226 | val = inb(data->addr + DATA_REG_OFFSET); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 227 | mutex_unlock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 228 | |
| 229 | return val; |
| 230 | } |
| 231 | |
| 232 | static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val) |
| 233 | { |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 234 | mutex_lock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 235 | outb(reg, data->addr + ADDR_REG_OFFSET); |
| 236 | outb(val, data->addr + DATA_REG_OFFSET); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 237 | mutex_unlock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* It is important to read the MSB first, because doing so latches the |
| 241 | value of the LSB, so we are sure both bytes belong to the same value. */ |
| 242 | static u16 f71805f_read16(struct f71805f_data *data, u8 reg) |
| 243 | { |
| 244 | u16 val; |
| 245 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 246 | mutex_lock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 247 | outb(reg, data->addr + ADDR_REG_OFFSET); |
| 248 | val = inb(data->addr + DATA_REG_OFFSET) << 8; |
| 249 | outb(++reg, data->addr + ADDR_REG_OFFSET); |
| 250 | val |= inb(data->addr + DATA_REG_OFFSET); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 251 | mutex_unlock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 252 | |
| 253 | return val; |
| 254 | } |
| 255 | |
| 256 | static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val) |
| 257 | { |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 258 | mutex_lock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 259 | outb(reg, data->addr + ADDR_REG_OFFSET); |
| 260 | outb(val >> 8, data->addr + DATA_REG_OFFSET); |
| 261 | outb(++reg, data->addr + ADDR_REG_OFFSET); |
| 262 | outb(val & 0xff, data->addr + DATA_REG_OFFSET); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 263 | mutex_unlock(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static struct f71805f_data *f71805f_update_device(struct device *dev) |
| 267 | { |
| 268 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 269 | int nr; |
| 270 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 271 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 272 | |
| 273 | /* Limit registers cache is refreshed after 60 seconds */ |
| 274 | if (time_after(jiffies, data->last_updated + 60 * HZ) |
| 275 | || !data->valid) { |
| 276 | for (nr = 0; nr < 9; nr++) { |
| 277 | data->in_high[nr] = f71805f_read8(data, |
| 278 | F71805F_REG_IN_HIGH(nr)); |
| 279 | data->in_low[nr] = f71805f_read8(data, |
| 280 | F71805F_REG_IN_LOW(nr)); |
| 281 | } |
| 282 | for (nr = 0; nr < 3; nr++) { |
| 283 | if (data->fan_enabled & (1 << nr)) |
| 284 | data->fan_low[nr] = f71805f_read16(data, |
| 285 | F71805F_REG_FAN_LOW(nr)); |
| 286 | } |
| 287 | for (nr = 0; nr < 3; nr++) { |
| 288 | data->temp_high[nr] = f71805f_read8(data, |
| 289 | F71805F_REG_TEMP_HIGH(nr)); |
| 290 | data->temp_hyst[nr] = f71805f_read8(data, |
| 291 | F71805F_REG_TEMP_HYST(nr)); |
| 292 | } |
| 293 | data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE); |
| 294 | |
| 295 | data->last_limits = jiffies; |
| 296 | } |
| 297 | |
| 298 | /* Measurement registers cache is refreshed after 1 second */ |
| 299 | if (time_after(jiffies, data->last_updated + HZ) |
| 300 | || !data->valid) { |
| 301 | for (nr = 0; nr < 9; nr++) { |
| 302 | data->in[nr] = f71805f_read8(data, |
| 303 | F71805F_REG_IN(nr)); |
| 304 | } |
| 305 | for (nr = 0; nr < 3; nr++) { |
| 306 | if (data->fan_enabled & (1 << nr)) |
| 307 | data->fan[nr] = f71805f_read16(data, |
| 308 | F71805F_REG_FAN(nr)); |
| 309 | } |
| 310 | for (nr = 0; nr < 3; nr++) { |
| 311 | data->temp[nr] = f71805f_read8(data, |
| 312 | F71805F_REG_TEMP(nr)); |
| 313 | } |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 314 | data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0)) |
| 315 | + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8) |
| 316 | + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 317 | |
| 318 | data->last_updated = jiffies; |
| 319 | data->valid = 1; |
| 320 | } |
| 321 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 322 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 323 | |
| 324 | return data; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Sysfs interface |
| 329 | */ |
| 330 | |
| 331 | static ssize_t show_in0(struct device *dev, struct device_attribute *devattr, |
| 332 | char *buf) |
| 333 | { |
| 334 | struct f71805f_data *data = f71805f_update_device(dev); |
| 335 | |
| 336 | return sprintf(buf, "%ld\n", in0_from_reg(data->in[0])); |
| 337 | } |
| 338 | |
| 339 | static ssize_t show_in0_max(struct device *dev, struct device_attribute |
| 340 | *devattr, char *buf) |
| 341 | { |
| 342 | struct f71805f_data *data = f71805f_update_device(dev); |
| 343 | |
| 344 | return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0])); |
| 345 | } |
| 346 | |
| 347 | static ssize_t show_in0_min(struct device *dev, struct device_attribute |
| 348 | *devattr, char *buf) |
| 349 | { |
| 350 | struct f71805f_data *data = f71805f_update_device(dev); |
| 351 | |
| 352 | return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0])); |
| 353 | } |
| 354 | |
| 355 | static ssize_t set_in0_max(struct device *dev, struct device_attribute |
| 356 | *devattr, const char *buf, size_t count) |
| 357 | { |
| 358 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 359 | long val = simple_strtol(buf, NULL, 10); |
| 360 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 361 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 362 | data->in_high[0] = in0_to_reg(val); |
| 363 | f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 364 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 365 | |
| 366 | return count; |
| 367 | } |
| 368 | |
| 369 | static ssize_t set_in0_min(struct device *dev, struct device_attribute |
| 370 | *devattr, const char *buf, size_t count) |
| 371 | { |
| 372 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 373 | long val = simple_strtol(buf, NULL, 10); |
| 374 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 375 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 376 | data->in_low[0] = in0_to_reg(val); |
| 377 | f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 378 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 379 | |
| 380 | return count; |
| 381 | } |
| 382 | |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 383 | static ssize_t show_in(struct device *dev, struct device_attribute *devattr, |
| 384 | char *buf) |
| 385 | { |
| 386 | struct f71805f_data *data = f71805f_update_device(dev); |
| 387 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 388 | int nr = attr->index; |
| 389 | |
| 390 | return sprintf(buf, "%ld\n", in_from_reg(data->in[nr])); |
| 391 | } |
| 392 | |
| 393 | static ssize_t show_in_max(struct device *dev, struct device_attribute |
| 394 | *devattr, char *buf) |
| 395 | { |
| 396 | struct f71805f_data *data = f71805f_update_device(dev); |
| 397 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 398 | int nr = attr->index; |
| 399 | |
| 400 | return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr])); |
| 401 | } |
| 402 | |
| 403 | static ssize_t show_in_min(struct device *dev, struct device_attribute |
| 404 | *devattr, char *buf) |
| 405 | { |
| 406 | struct f71805f_data *data = f71805f_update_device(dev); |
| 407 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 408 | int nr = attr->index; |
| 409 | |
| 410 | return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr])); |
| 411 | } |
| 412 | |
| 413 | static ssize_t set_in_max(struct device *dev, struct device_attribute |
| 414 | *devattr, const char *buf, size_t count) |
| 415 | { |
| 416 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 417 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 418 | int nr = attr->index; |
| 419 | long val = simple_strtol(buf, NULL, 10); |
| 420 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 421 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 422 | data->in_high[nr] = in_to_reg(val); |
| 423 | f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 424 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 425 | |
| 426 | return count; |
| 427 | } |
| 428 | |
| 429 | static ssize_t set_in_min(struct device *dev, struct device_attribute |
| 430 | *devattr, const char *buf, size_t count) |
| 431 | { |
| 432 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 433 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 434 | int nr = attr->index; |
| 435 | long val = simple_strtol(buf, NULL, 10); |
| 436 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 437 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 438 | data->in_low[nr] = in_to_reg(val); |
| 439 | f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 440 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 441 | |
| 442 | return count; |
| 443 | } |
| 444 | |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 445 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
| 446 | char *buf) |
| 447 | { |
| 448 | struct f71805f_data *data = f71805f_update_device(dev); |
| 449 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 450 | int nr = attr->index; |
| 451 | |
| 452 | return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr])); |
| 453 | } |
| 454 | |
| 455 | static ssize_t show_fan_min(struct device *dev, struct device_attribute |
| 456 | *devattr, char *buf) |
| 457 | { |
| 458 | struct f71805f_data *data = f71805f_update_device(dev); |
| 459 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 460 | int nr = attr->index; |
| 461 | |
| 462 | return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr])); |
| 463 | } |
| 464 | |
| 465 | static ssize_t set_fan_min(struct device *dev, struct device_attribute |
| 466 | *devattr, const char *buf, size_t count) |
| 467 | { |
| 468 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 469 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 470 | int nr = attr->index; |
| 471 | long val = simple_strtol(buf, NULL, 10); |
| 472 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 473 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 474 | data->fan_low[nr] = fan_to_reg(val); |
| 475 | f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 476 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 477 | |
| 478 | return count; |
| 479 | } |
| 480 | |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 481 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, |
| 482 | char *buf) |
| 483 | { |
| 484 | struct f71805f_data *data = f71805f_update_device(dev); |
| 485 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 486 | int nr = attr->index; |
| 487 | |
| 488 | return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr])); |
| 489 | } |
| 490 | |
| 491 | static ssize_t show_temp_max(struct device *dev, struct device_attribute |
| 492 | *devattr, char *buf) |
| 493 | { |
| 494 | struct f71805f_data *data = f71805f_update_device(dev); |
| 495 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 496 | int nr = attr->index; |
| 497 | |
| 498 | return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr])); |
| 499 | } |
| 500 | |
| 501 | static ssize_t show_temp_hyst(struct device *dev, struct device_attribute |
| 502 | *devattr, char *buf) |
| 503 | { |
| 504 | struct f71805f_data *data = f71805f_update_device(dev); |
| 505 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 506 | int nr = attr->index; |
| 507 | |
| 508 | return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr])); |
| 509 | } |
| 510 | |
| 511 | static ssize_t show_temp_type(struct device *dev, struct device_attribute |
| 512 | *devattr, char *buf) |
| 513 | { |
| 514 | struct f71805f_data *data = f71805f_update_device(dev); |
| 515 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 516 | int nr = attr->index; |
| 517 | |
| 518 | /* 3 is diode, 4 is thermistor */ |
| 519 | return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4); |
| 520 | } |
| 521 | |
| 522 | static ssize_t set_temp_max(struct device *dev, struct device_attribute |
| 523 | *devattr, const char *buf, size_t count) |
| 524 | { |
| 525 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 526 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 527 | int nr = attr->index; |
| 528 | long val = simple_strtol(buf, NULL, 10); |
| 529 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 530 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 531 | data->temp_high[nr] = temp_to_reg(val); |
| 532 | f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 533 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 534 | |
| 535 | return count; |
| 536 | } |
| 537 | |
| 538 | static ssize_t set_temp_hyst(struct device *dev, struct device_attribute |
| 539 | *devattr, const char *buf, size_t count) |
| 540 | { |
| 541 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 542 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 543 | int nr = attr->index; |
| 544 | long val = simple_strtol(buf, NULL, 10); |
| 545 | |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 546 | mutex_lock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 547 | data->temp_hyst[nr] = temp_to_reg(val); |
| 548 | f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]); |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 549 | mutex_unlock(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 550 | |
| 551 | return count; |
| 552 | } |
| 553 | |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 554 | static ssize_t show_alarms_in(struct device *dev, struct device_attribute |
| 555 | *devattr, char *buf) |
| 556 | { |
| 557 | struct f71805f_data *data = f71805f_update_device(dev); |
| 558 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 559 | return sprintf(buf, "%lu\n", data->alarms & 0x1ff); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static ssize_t show_alarms_fan(struct device *dev, struct device_attribute |
| 563 | *devattr, char *buf) |
| 564 | { |
| 565 | struct f71805f_data *data = f71805f_update_device(dev); |
| 566 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 567 | return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static ssize_t show_alarms_temp(struct device *dev, struct device_attribute |
| 571 | *devattr, char *buf) |
| 572 | { |
| 573 | struct f71805f_data *data = f71805f_update_device(dev); |
| 574 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 575 | return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07); |
| 576 | } |
| 577 | |
| 578 | static ssize_t show_alarm(struct device *dev, struct device_attribute |
| 579 | *devattr, char *buf) |
| 580 | { |
| 581 | struct f71805f_data *data = f71805f_update_device(dev); |
| 582 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 583 | int bitnr = attr->index; |
| 584 | |
| 585 | return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 588 | static ssize_t show_name(struct device *dev, struct device_attribute |
| 589 | *devattr, char *buf) |
| 590 | { |
| 591 | struct f71805f_data *data = dev_get_drvdata(dev); |
| 592 | |
| 593 | return sprintf(buf, "%s\n", data->name); |
| 594 | } |
| 595 | |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 596 | static struct device_attribute f71805f_dev_attr[] = { |
| 597 | __ATTR(in0_input, S_IRUGO, show_in0, NULL), |
| 598 | __ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max), |
| 599 | __ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min), |
| 600 | __ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL), |
| 601 | __ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL), |
| 602 | __ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL), |
| 603 | __ATTR(name, S_IRUGO, show_name, NULL), |
| 604 | }; |
| 605 | |
| 606 | static struct sensor_device_attribute f71805f_sensor_attr[] = { |
| 607 | SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1), |
| 608 | SENSOR_ATTR(in1_max, S_IRUGO | S_IWUSR, |
| 609 | show_in_max, set_in_max, 1), |
| 610 | SENSOR_ATTR(in1_min, S_IRUGO | S_IWUSR, |
| 611 | show_in_min, set_in_min, 1), |
| 612 | SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2), |
| 613 | SENSOR_ATTR(in2_max, S_IRUGO | S_IWUSR, |
| 614 | show_in_max, set_in_max, 2), |
| 615 | SENSOR_ATTR(in2_min, S_IRUGO | S_IWUSR, |
| 616 | show_in_min, set_in_min, 2), |
| 617 | SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3), |
| 618 | SENSOR_ATTR(in3_max, S_IRUGO | S_IWUSR, |
| 619 | show_in_max, set_in_max, 3), |
| 620 | SENSOR_ATTR(in3_min, S_IRUGO | S_IWUSR, |
| 621 | show_in_min, set_in_min, 3), |
| 622 | SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4), |
| 623 | SENSOR_ATTR(in4_max, S_IRUGO | S_IWUSR, |
| 624 | show_in_max, set_in_max, 4), |
| 625 | SENSOR_ATTR(in4_min, S_IRUGO | S_IWUSR, |
| 626 | show_in_min, set_in_min, 4), |
| 627 | SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5), |
| 628 | SENSOR_ATTR(in5_max, S_IRUGO | S_IWUSR, |
| 629 | show_in_max, set_in_max, 5), |
| 630 | SENSOR_ATTR(in5_min, S_IRUGO | S_IWUSR, |
| 631 | show_in_min, set_in_min, 5), |
| 632 | SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6), |
| 633 | SENSOR_ATTR(in6_max, S_IRUGO | S_IWUSR, |
| 634 | show_in_max, set_in_max, 6), |
| 635 | SENSOR_ATTR(in6_min, S_IRUGO | S_IWUSR, |
| 636 | show_in_min, set_in_min, 6), |
| 637 | SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7), |
| 638 | SENSOR_ATTR(in7_max, S_IRUGO | S_IWUSR, |
| 639 | show_in_max, set_in_max, 7), |
| 640 | SENSOR_ATTR(in7_min, S_IRUGO | S_IWUSR, |
| 641 | show_in_min, set_in_min, 7), |
| 642 | SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8), |
| 643 | SENSOR_ATTR(in8_max, S_IRUGO | S_IWUSR, |
| 644 | show_in_max, set_in_max, 8), |
| 645 | SENSOR_ATTR(in8_min, S_IRUGO | S_IWUSR, |
| 646 | show_in_min, set_in_min, 8), |
| 647 | |
| 648 | SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0), |
| 649 | SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, |
| 650 | show_temp_max, set_temp_max, 0), |
| 651 | SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, |
| 652 | show_temp_hyst, set_temp_hyst, 0), |
| 653 | SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0), |
| 654 | SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1), |
| 655 | SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, |
| 656 | show_temp_max, set_temp_max, 1), |
| 657 | SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, |
| 658 | show_temp_hyst, set_temp_hyst, 1), |
| 659 | SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1), |
| 660 | SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2), |
| 661 | SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, |
| 662 | show_temp_max, set_temp_max, 2), |
| 663 | SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, |
| 664 | show_temp_hyst, set_temp_hyst, 2), |
| 665 | SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2), |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 666 | |
| 667 | SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0), |
| 668 | SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1), |
| 669 | SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2), |
| 670 | SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3), |
| 671 | SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4), |
| 672 | SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5), |
| 673 | SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6), |
| 674 | SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7), |
| 675 | SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8), |
| 676 | SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11), |
| 677 | SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12), |
| 678 | SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13), |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 679 | }; |
| 680 | |
| 681 | static struct sensor_device_attribute f71805f_fan_attr[] = { |
| 682 | SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0), |
| 683 | SENSOR_ATTR(fan1_min, S_IRUGO | S_IWUSR, |
| 684 | show_fan_min, set_fan_min, 0), |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 685 | SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16), |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 686 | SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1), |
| 687 | SENSOR_ATTR(fan2_min, S_IRUGO | S_IWUSR, |
| 688 | show_fan_min, set_fan_min, 1), |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 689 | SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17), |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 690 | SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2), |
| 691 | SENSOR_ATTR(fan3_min, S_IRUGO | S_IWUSR, |
| 692 | show_fan_min, set_fan_min, 2), |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 693 | SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18), |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 694 | }; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 695 | |
| 696 | /* |
| 697 | * Device registration and initialization |
| 698 | */ |
| 699 | |
| 700 | static void __devinit f71805f_init_device(struct f71805f_data *data) |
| 701 | { |
| 702 | u8 reg; |
| 703 | int i; |
| 704 | |
| 705 | reg = f71805f_read8(data, F71805F_REG_START); |
| 706 | if ((reg & 0x41) != 0x01) { |
| 707 | printk(KERN_DEBUG DRVNAME ": Starting monitoring " |
| 708 | "operations\n"); |
| 709 | f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40); |
| 710 | } |
| 711 | |
| 712 | /* Fan monitoring can be disabled. If it is, we won't be polling |
| 713 | the register values, and won't create the related sysfs files. */ |
| 714 | for (i = 0; i < 3; i++) { |
| 715 | reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(i)); |
| 716 | if (!(reg & 0x80)) |
| 717 | data->fan_enabled |= (1 << i); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | static int __devinit f71805f_probe(struct platform_device *pdev) |
| 722 | { |
| 723 | struct f71805f_data *data; |
| 724 | struct resource *res; |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 725 | int i, err; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 726 | |
| 727 | if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) { |
| 728 | err = -ENOMEM; |
| 729 | printk(KERN_ERR DRVNAME ": Out of memory\n"); |
| 730 | goto exit; |
| 731 | } |
| 732 | |
| 733 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 734 | data->addr = res->start; |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 735 | mutex_init(&data->lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 736 | data->name = "f71805f"; |
Jean Delvare | f081918 | 2006-01-18 23:20:53 +0100 | [diff] [blame] | 737 | mutex_init(&data->update_lock); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 738 | |
| 739 | platform_set_drvdata(pdev, data); |
| 740 | |
| 741 | data->class_dev = hwmon_device_register(&pdev->dev); |
| 742 | if (IS_ERR(data->class_dev)) { |
| 743 | err = PTR_ERR(data->class_dev); |
| 744 | dev_err(&pdev->dev, "Class registration failed (%d)\n", err); |
| 745 | goto exit_free; |
| 746 | } |
| 747 | |
| 748 | /* Initialize the F71805F chip */ |
| 749 | f71805f_init_device(data); |
| 750 | |
| 751 | /* Register sysfs interface files */ |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 752 | for (i = 0; i < ARRAY_SIZE(f71805f_dev_attr); i++) { |
| 753 | err = device_create_file(&pdev->dev, &f71805f_dev_attr[i]); |
| 754 | if (err) |
| 755 | goto exit_class; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 756 | } |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 757 | for (i = 0; i < ARRAY_SIZE(f71805f_sensor_attr); i++) { |
| 758 | err = device_create_file(&pdev->dev, |
| 759 | &f71805f_sensor_attr[i].dev_attr); |
| 760 | if (err) |
| 761 | goto exit_class; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 762 | } |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 763 | for (i = 0; i < ARRAY_SIZE(f71805f_fan_attr); i++) { |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame^] | 764 | if (!(data->fan_enabled & (1 << (i / 3)))) |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 765 | continue; |
| 766 | err = device_create_file(&pdev->dev, |
| 767 | &f71805f_fan_attr[i].dev_attr); |
| 768 | if (err) |
| 769 | goto exit_class; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 770 | } |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 771 | |
| 772 | return 0; |
| 773 | |
Jean Delvare | 2488a39 | 2006-01-09 23:29:11 +0100 | [diff] [blame] | 774 | exit_class: |
| 775 | dev_err(&pdev->dev, "Sysfs interface creation failed\n"); |
| 776 | hwmon_device_unregister(data->class_dev); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 777 | exit_free: |
| 778 | kfree(data); |
| 779 | exit: |
| 780 | return err; |
| 781 | } |
| 782 | |
| 783 | static int __devexit f71805f_remove(struct platform_device *pdev) |
| 784 | { |
| 785 | struct f71805f_data *data = platform_get_drvdata(pdev); |
| 786 | |
| 787 | platform_set_drvdata(pdev, NULL); |
| 788 | hwmon_device_unregister(data->class_dev); |
| 789 | kfree(data); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static struct platform_driver f71805f_driver = { |
| 795 | .driver = { |
| 796 | .owner = THIS_MODULE, |
| 797 | .name = DRVNAME, |
| 798 | }, |
| 799 | .probe = f71805f_probe, |
| 800 | .remove = __devexit_p(f71805f_remove), |
| 801 | }; |
| 802 | |
| 803 | static int __init f71805f_device_add(unsigned short address) |
| 804 | { |
Jean Delvare | 568825c | 2006-03-23 16:40:23 +0100 | [diff] [blame] | 805 | struct resource res = { |
| 806 | .start = address, |
| 807 | .end = address + REGION_LENGTH - 1, |
| 808 | .flags = IORESOURCE_IO, |
| 809 | }; |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 810 | int err; |
| 811 | |
| 812 | pdev = platform_device_alloc(DRVNAME, address); |
| 813 | if (!pdev) { |
| 814 | err = -ENOMEM; |
| 815 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); |
| 816 | goto exit; |
| 817 | } |
| 818 | |
Jean Delvare | 568825c | 2006-03-23 16:40:23 +0100 | [diff] [blame] | 819 | res.name = pdev->name; |
| 820 | err = platform_device_add_resources(pdev, &res, 1); |
Jean Delvare | e53004e | 2006-01-09 23:26:14 +0100 | [diff] [blame] | 821 | if (err) { |
| 822 | printk(KERN_ERR DRVNAME ": Device resource addition failed " |
| 823 | "(%d)\n", err); |
| 824 | goto exit_device_put; |
| 825 | } |
| 826 | |
| 827 | err = platform_device_add(pdev); |
| 828 | if (err) { |
| 829 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", |
| 830 | err); |
| 831 | goto exit_device_put; |
| 832 | } |
| 833 | |
| 834 | return 0; |
| 835 | |
| 836 | exit_device_put: |
| 837 | platform_device_put(pdev); |
| 838 | exit: |
| 839 | return err; |
| 840 | } |
| 841 | |
| 842 | static int __init f71805f_find(int sioaddr, unsigned short *address) |
| 843 | { |
| 844 | int err = -ENODEV; |
| 845 | u16 devid; |
| 846 | |
| 847 | superio_enter(sioaddr); |
| 848 | |
| 849 | devid = superio_inw(sioaddr, SIO_REG_MANID); |
| 850 | if (devid != SIO_FINTEK_ID) |
| 851 | goto exit; |
| 852 | |
| 853 | devid = superio_inw(sioaddr, SIO_REG_DEVID); |
| 854 | if (devid != SIO_F71805F_ID) { |
| 855 | printk(KERN_INFO DRVNAME ": Unsupported Fintek device, " |
| 856 | "skipping\n"); |
| 857 | goto exit; |
| 858 | } |
| 859 | |
| 860 | superio_select(sioaddr, F71805F_LD_HWM); |
| 861 | if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) { |
| 862 | printk(KERN_WARNING DRVNAME ": Device not activated, " |
| 863 | "skipping\n"); |
| 864 | goto exit; |
| 865 | } |
| 866 | |
| 867 | *address = superio_inw(sioaddr, SIO_REG_ADDR); |
| 868 | if (*address == 0) { |
| 869 | printk(KERN_WARNING DRVNAME ": Base address not set, " |
| 870 | "skipping\n"); |
| 871 | goto exit; |
| 872 | } |
| 873 | |
| 874 | err = 0; |
| 875 | printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n", |
| 876 | *address, superio_inb(sioaddr, SIO_REG_DEVREV)); |
| 877 | |
| 878 | exit: |
| 879 | superio_exit(sioaddr); |
| 880 | return err; |
| 881 | } |
| 882 | |
| 883 | static int __init f71805f_init(void) |
| 884 | { |
| 885 | int err; |
| 886 | unsigned short address; |
| 887 | |
| 888 | if (f71805f_find(0x2e, &address) |
| 889 | && f71805f_find(0x4e, &address)) |
| 890 | return -ENODEV; |
| 891 | |
| 892 | err = platform_driver_register(&f71805f_driver); |
| 893 | if (err) |
| 894 | goto exit; |
| 895 | |
| 896 | /* Sets global pdev as a side effect */ |
| 897 | err = f71805f_device_add(address); |
| 898 | if (err) |
| 899 | goto exit_driver; |
| 900 | |
| 901 | return 0; |
| 902 | |
| 903 | exit_driver: |
| 904 | platform_driver_unregister(&f71805f_driver); |
| 905 | exit: |
| 906 | return err; |
| 907 | } |
| 908 | |
| 909 | static void __exit f71805f_exit(void) |
| 910 | { |
| 911 | platform_device_unregister(pdev); |
| 912 | platform_driver_unregister(&f71805f_driver); |
| 913 | } |
| 914 | |
| 915 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr>"); |
| 916 | MODULE_LICENSE("GPL"); |
| 917 | MODULE_DESCRIPTION("F71805F hardware monitoring driver"); |
| 918 | |
| 919 | module_init(f71805f_init); |
| 920 | module_exit(f71805f_exit); |