Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | it87.c - Part of lm_sensors, Linux kernel modules for hardware | |
3 | monitoring. | |
4 | ||
87b4b663 BH |
5 | The IT8705F is an LPC-based Super I/O part that contains UARTs, a |
6 | parallel port, an IR port, a MIDI port, a floppy controller, etc., in | |
7 | addition to an Environment Controller (Enhanced Hardware Monitor and | |
8 | Fan Controller) | |
9 | ||
10 | This driver supports only the Environment Controller in the IT8705F and | |
11 | similar parts. The other devices are supported by different drivers. | |
12 | ||
91749996 | 13 | Supports: IT8705F Super I/O chip w/LPC interface |
8e9afcbb | 14 | IT8712F Super I/O chip w/LPC interface |
17d648bf | 15 | IT8716F Super I/O chip w/LPC interface |
87673dd7 | 16 | IT8718F Super I/O chip w/LPC interface |
08a8f6e9 | 17 | IT8726F Super I/O chip w/LPC interface |
1da177e4 LT |
18 | Sis950 A clone of the IT8705F |
19 | ||
f1d8e332 | 20 | Copyright (C) 2001 Chris Gauthron |
0124dd78 | 21 | Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org> |
1da177e4 LT |
22 | |
23 | This program is free software; you can redistribute it and/or modify | |
24 | it under the terms of the GNU General Public License as published by | |
25 | the Free Software Foundation; either version 2 of the License, or | |
26 | (at your option) any later version. | |
27 | ||
28 | This program is distributed in the hope that it will be useful, | |
29 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
30 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
31 | GNU General Public License for more details. | |
32 | ||
33 | You should have received a copy of the GNU General Public License | |
34 | along with this program; if not, write to the Free Software | |
35 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
36 | */ | |
37 | ||
1da177e4 LT |
38 | #include <linux/module.h> |
39 | #include <linux/init.h> | |
40 | #include <linux/slab.h> | |
41 | #include <linux/jiffies.h> | |
b74f3fdd | 42 | #include <linux/platform_device.h> |
943b0830 | 43 | #include <linux/hwmon.h> |
303760b4 JD |
44 | #include <linux/hwmon-sysfs.h> |
45 | #include <linux/hwmon-vid.h> | |
943b0830 | 46 | #include <linux/err.h> |
9a61bf63 | 47 | #include <linux/mutex.h> |
87808be4 | 48 | #include <linux/sysfs.h> |
1da177e4 LT |
49 | #include <asm/io.h> |
50 | ||
b74f3fdd | 51 | #define DRVNAME "it87" |
1da177e4 | 52 | |
8e9afcbb | 53 | enum chips { it87, it8712, it8716, it8718 }; |
1da177e4 | 54 | |
67b671bc JD |
55 | static unsigned short force_id; |
56 | module_param(force_id, ushort, 0); | |
57 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); | |
58 | ||
b74f3fdd | 59 | static struct platform_device *pdev; |
60 | ||
1da177e4 LT |
61 | #define REG 0x2e /* The register to read/write */ |
62 | #define DEV 0x07 /* Register: Logical device select */ | |
63 | #define VAL 0x2f /* The value to read/write */ | |
64 | #define PME 0x04 /* The device with the fan registers in it */ | |
87673dd7 | 65 | #define GPIO 0x07 /* The device with the IT8718F VID value in it */ |
1da177e4 LT |
66 | #define DEVID 0x20 /* Register: Device ID */ |
67 | #define DEVREV 0x22 /* Register: Device Revision */ | |
68 | ||
69 | static inline int | |
70 | superio_inb(int reg) | |
71 | { | |
72 | outb(reg, REG); | |
73 | return inb(VAL); | |
74 | } | |
75 | ||
76 | static int superio_inw(int reg) | |
77 | { | |
78 | int val; | |
79 | outb(reg++, REG); | |
80 | val = inb(VAL) << 8; | |
81 | outb(reg, REG); | |
82 | val |= inb(VAL); | |
83 | return val; | |
84 | } | |
85 | ||
86 | static inline void | |
87673dd7 | 87 | superio_select(int ldn) |
1da177e4 LT |
88 | { |
89 | outb(DEV, REG); | |
87673dd7 | 90 | outb(ldn, VAL); |
1da177e4 LT |
91 | } |
92 | ||
93 | static inline void | |
94 | superio_enter(void) | |
95 | { | |
96 | outb(0x87, REG); | |
97 | outb(0x01, REG); | |
98 | outb(0x55, REG); | |
99 | outb(0x55, REG); | |
100 | } | |
101 | ||
102 | static inline void | |
103 | superio_exit(void) | |
104 | { | |
105 | outb(0x02, REG); | |
106 | outb(0x02, VAL); | |
107 | } | |
108 | ||
87673dd7 | 109 | /* Logical device 4 registers */ |
1da177e4 LT |
110 | #define IT8712F_DEVID 0x8712 |
111 | #define IT8705F_DEVID 0x8705 | |
17d648bf | 112 | #define IT8716F_DEVID 0x8716 |
87673dd7 | 113 | #define IT8718F_DEVID 0x8718 |
08a8f6e9 | 114 | #define IT8726F_DEVID 0x8726 |
1da177e4 LT |
115 | #define IT87_ACT_REG 0x30 |
116 | #define IT87_BASE_REG 0x60 | |
117 | ||
87673dd7 JD |
118 | /* Logical device 7 registers (IT8712F and later) */ |
119 | #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */ | |
120 | #define IT87_SIO_VID_REG 0xfc /* VID value */ | |
121 | ||
1da177e4 LT |
122 | /* Update battery voltage after every reading if true */ |
123 | static int update_vbat; | |
124 | ||
125 | /* Not all BIOSes properly configure the PWM registers */ | |
126 | static int fix_pwm_polarity; | |
127 | ||
1da177e4 LT |
128 | /* Many IT87 constants specified below */ |
129 | ||
130 | /* Length of ISA address segment */ | |
131 | #define IT87_EXTENT 8 | |
132 | ||
87b4b663 BH |
133 | /* Length of ISA address segment for Environmental Controller */ |
134 | #define IT87_EC_EXTENT 2 | |
135 | ||
136 | /* Offset of EC registers from ISA base address */ | |
137 | #define IT87_EC_OFFSET 5 | |
138 | ||
139 | /* Where are the ISA address/data registers relative to the EC base address */ | |
140 | #define IT87_ADDR_REG_OFFSET 0 | |
141 | #define IT87_DATA_REG_OFFSET 1 | |
1da177e4 LT |
142 | |
143 | /*----- The IT87 registers -----*/ | |
144 | ||
145 | #define IT87_REG_CONFIG 0x00 | |
146 | ||
147 | #define IT87_REG_ALARM1 0x01 | |
148 | #define IT87_REG_ALARM2 0x02 | |
149 | #define IT87_REG_ALARM3 0x03 | |
150 | ||
87673dd7 JD |
151 | /* The IT8718F has the VID value in a different register, in Super-I/O |
152 | configuration space. */ | |
1da177e4 | 153 | #define IT87_REG_VID 0x0a |
0475169c AP |
154 | /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b |
155 | for fan divisors. Later IT8712F revisions must use 16-bit tachometer | |
156 | mode. */ | |
1da177e4 | 157 | #define IT87_REG_FAN_DIV 0x0b |
17d648bf | 158 | #define IT87_REG_FAN_16BIT 0x0c |
1da177e4 LT |
159 | |
160 | /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */ | |
161 | ||
c7f1f716 JD |
162 | static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 }; |
163 | static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 }; | |
164 | static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 }; | |
165 | static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 }; | |
1da177e4 LT |
166 | #define IT87_REG_FAN_MAIN_CTRL 0x13 |
167 | #define IT87_REG_FAN_CTL 0x14 | |
168 | #define IT87_REG_PWM(nr) (0x15 + (nr)) | |
169 | ||
170 | #define IT87_REG_VIN(nr) (0x20 + (nr)) | |
171 | #define IT87_REG_TEMP(nr) (0x29 + (nr)) | |
172 | ||
173 | #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2) | |
174 | #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2) | |
175 | #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2) | |
176 | #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2) | |
177 | ||
1da177e4 LT |
178 | #define IT87_REG_VIN_ENABLE 0x50 |
179 | #define IT87_REG_TEMP_ENABLE 0x51 | |
180 | ||
181 | #define IT87_REG_CHIPID 0x58 | |
182 | ||
183 | #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255)) | |
184 | #define IN_FROM_REG(val) ((val) * 16) | |
185 | ||
186 | static inline u8 FAN_TO_REG(long rpm, int div) | |
187 | { | |
188 | if (rpm == 0) | |
189 | return 255; | |
190 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); | |
191 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, | |
192 | 254); | |
193 | } | |
194 | ||
17d648bf JD |
195 | static inline u16 FAN16_TO_REG(long rpm) |
196 | { | |
197 | if (rpm == 0) | |
198 | return 0xffff; | |
199 | return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe); | |
200 | } | |
201 | ||
1da177e4 | 202 | #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div))) |
17d648bf JD |
203 | /* The divider is fixed to 2 in 16-bit mode */ |
204 | #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2)) | |
1da177e4 LT |
205 | |
206 | #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\ | |
207 | ((val)+500)/1000),-128,127)) | |
208 | #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000) | |
209 | ||
1da177e4 LT |
210 | #define PWM_TO_REG(val) ((val) >> 1) |
211 | #define PWM_FROM_REG(val) (((val)&0x7f) << 1) | |
212 | ||
213 | static int DIV_TO_REG(int val) | |
214 | { | |
215 | int answer = 0; | |
b9e349f7 | 216 | while (answer < 7 && (val >>= 1)) |
1da177e4 LT |
217 | answer++; |
218 | return answer; | |
219 | } | |
220 | #define DIV_FROM_REG(val) (1 << (val)) | |
221 | ||
f8d0c19a JD |
222 | static const unsigned int pwm_freq[8] = { |
223 | 48000000 / 128, | |
224 | 24000000 / 128, | |
225 | 12000000 / 128, | |
226 | 8000000 / 128, | |
227 | 6000000 / 128, | |
228 | 3000000 / 128, | |
229 | 1500000 / 128, | |
230 | 750000 / 128, | |
231 | }; | |
232 | ||
1da177e4 | 233 | |
b74f3fdd | 234 | struct it87_sio_data { |
235 | enum chips type; | |
236 | /* Values read from Super-I/O config space */ | |
0475169c | 237 | u8 revision; |
b74f3fdd | 238 | u8 vid_value; |
239 | }; | |
240 | ||
ed6bafbf JD |
241 | /* For each registered chip, we need to keep some data in memory. |
242 | The structure is dynamically allocated. */ | |
1da177e4 | 243 | struct it87_data { |
1beeffe4 | 244 | struct device *hwmon_dev; |
1da177e4 | 245 | enum chips type; |
0475169c | 246 | u8 revision; |
1da177e4 | 247 | |
b74f3fdd | 248 | unsigned short addr; |
249 | const char *name; | |
9a61bf63 | 250 | struct mutex update_lock; |
1da177e4 LT |
251 | char valid; /* !=0 if following fields are valid */ |
252 | unsigned long last_updated; /* In jiffies */ | |
253 | ||
254 | u8 in[9]; /* Register value */ | |
3543a53f JD |
255 | u8 in_max[8]; /* Register value */ |
256 | u8 in_min[8]; /* Register value */ | |
9060f8bd | 257 | u8 has_fan; /* Bitfield, fans enabled */ |
c7f1f716 JD |
258 | u16 fan[5]; /* Register values, possibly combined */ |
259 | u16 fan_min[5]; /* Register values, possibly combined */ | |
1da177e4 LT |
260 | u8 temp[3]; /* Register value */ |
261 | u8 temp_high[3]; /* Register value */ | |
262 | u8 temp_low[3]; /* Register value */ | |
263 | u8 sensor; /* Register value */ | |
264 | u8 fan_div[3]; /* Register encoding, shifted right */ | |
265 | u8 vid; /* Register encoding, combined */ | |
a7be58a1 | 266 | u8 vrm; |
1da177e4 LT |
267 | u32 alarms; /* Register encoding, combined */ |
268 | u8 fan_main_ctrl; /* Register value */ | |
f8d0c19a | 269 | u8 fan_ctl; /* Register value */ |
1da177e4 LT |
270 | u8 manual_pwm_ctl[3]; /* manual PWM value set by user */ |
271 | }; | |
272 | ||
0475169c AP |
273 | static inline int has_16bit_fans(const struct it87_data *data) |
274 | { | |
275 | /* IT8712F Datasheet 0.9.1, section 8.3.5 indicates 7h == Version I. | |
276 | This is the first revision with 16bit tachometer support. */ | |
277 | return (data->type == it8712 && data->revision >= 0x07) | |
278 | || data->type == it8716 | |
279 | || data->type == it8718; | |
280 | } | |
1da177e4 | 281 | |
b74f3fdd | 282 | static int it87_probe(struct platform_device *pdev); |
d0546128 | 283 | static int __devexit it87_remove(struct platform_device *pdev); |
1da177e4 | 284 | |
b74f3fdd | 285 | static int it87_read_value(struct it87_data *data, u8 reg); |
286 | static void it87_write_value(struct it87_data *data, u8 reg, u8 value); | |
1da177e4 | 287 | static struct it87_data *it87_update_device(struct device *dev); |
b74f3fdd | 288 | static int it87_check_pwm(struct device *dev); |
289 | static void it87_init_device(struct platform_device *pdev); | |
1da177e4 LT |
290 | |
291 | ||
b74f3fdd | 292 | static struct platform_driver it87_driver = { |
cdaf7934 | 293 | .driver = { |
87218842 | 294 | .owner = THIS_MODULE, |
b74f3fdd | 295 | .name = DRVNAME, |
cdaf7934 | 296 | }, |
b74f3fdd | 297 | .probe = it87_probe, |
298 | .remove = __devexit_p(it87_remove), | |
fde09509 JD |
299 | }; |
300 | ||
20ad93d4 JD |
301 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
302 | char *buf) | |
1da177e4 | 303 | { |
20ad93d4 JD |
304 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
305 | int nr = sensor_attr->index; | |
306 | ||
1da177e4 LT |
307 | struct it87_data *data = it87_update_device(dev); |
308 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])); | |
309 | } | |
310 | ||
20ad93d4 JD |
311 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
312 | char *buf) | |
1da177e4 | 313 | { |
20ad93d4 JD |
314 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
315 | int nr = sensor_attr->index; | |
316 | ||
1da177e4 LT |
317 | struct it87_data *data = it87_update_device(dev); |
318 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])); | |
319 | } | |
320 | ||
20ad93d4 JD |
321 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
322 | char *buf) | |
1da177e4 | 323 | { |
20ad93d4 JD |
324 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
325 | int nr = sensor_attr->index; | |
326 | ||
1da177e4 LT |
327 | struct it87_data *data = it87_update_device(dev); |
328 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])); | |
329 | } | |
330 | ||
20ad93d4 JD |
331 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
332 | const char *buf, size_t count) | |
1da177e4 | 333 | { |
20ad93d4 JD |
334 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
335 | int nr = sensor_attr->index; | |
336 | ||
b74f3fdd | 337 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
338 | unsigned long val = simple_strtoul(buf, NULL, 10); |
339 | ||
9a61bf63 | 340 | mutex_lock(&data->update_lock); |
1da177e4 | 341 | data->in_min[nr] = IN_TO_REG(val); |
b74f3fdd | 342 | it87_write_value(data, IT87_REG_VIN_MIN(nr), |
1da177e4 | 343 | data->in_min[nr]); |
9a61bf63 | 344 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
345 | return count; |
346 | } | |
20ad93d4 JD |
347 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
348 | const char *buf, size_t count) | |
1da177e4 | 349 | { |
20ad93d4 JD |
350 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
351 | int nr = sensor_attr->index; | |
352 | ||
b74f3fdd | 353 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
354 | unsigned long val = simple_strtoul(buf, NULL, 10); |
355 | ||
9a61bf63 | 356 | mutex_lock(&data->update_lock); |
1da177e4 | 357 | data->in_max[nr] = IN_TO_REG(val); |
b74f3fdd | 358 | it87_write_value(data, IT87_REG_VIN_MAX(nr), |
1da177e4 | 359 | data->in_max[nr]); |
9a61bf63 | 360 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
361 | return count; |
362 | } | |
363 | ||
364 | #define show_in_offset(offset) \ | |
20ad93d4 JD |
365 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
366 | show_in, NULL, offset); | |
1da177e4 LT |
367 | |
368 | #define limit_in_offset(offset) \ | |
20ad93d4 JD |
369 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
370 | show_in_min, set_in_min, offset); \ | |
371 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
372 | show_in_max, set_in_max, offset); | |
1da177e4 LT |
373 | |
374 | show_in_offset(0); | |
375 | limit_in_offset(0); | |
376 | show_in_offset(1); | |
377 | limit_in_offset(1); | |
378 | show_in_offset(2); | |
379 | limit_in_offset(2); | |
380 | show_in_offset(3); | |
381 | limit_in_offset(3); | |
382 | show_in_offset(4); | |
383 | limit_in_offset(4); | |
384 | show_in_offset(5); | |
385 | limit_in_offset(5); | |
386 | show_in_offset(6); | |
387 | limit_in_offset(6); | |
388 | show_in_offset(7); | |
389 | limit_in_offset(7); | |
390 | show_in_offset(8); | |
391 | ||
392 | /* 3 temperatures */ | |
20ad93d4 JD |
393 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
394 | char *buf) | |
1da177e4 | 395 | { |
20ad93d4 JD |
396 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
397 | int nr = sensor_attr->index; | |
398 | ||
1da177e4 LT |
399 | struct it87_data *data = it87_update_device(dev); |
400 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); | |
401 | } | |
20ad93d4 JD |
402 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
403 | char *buf) | |
1da177e4 | 404 | { |
20ad93d4 JD |
405 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
406 | int nr = sensor_attr->index; | |
407 | ||
1da177e4 LT |
408 | struct it87_data *data = it87_update_device(dev); |
409 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])); | |
410 | } | |
20ad93d4 JD |
411 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
412 | char *buf) | |
1da177e4 | 413 | { |
20ad93d4 JD |
414 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
415 | int nr = sensor_attr->index; | |
416 | ||
1da177e4 LT |
417 | struct it87_data *data = it87_update_device(dev); |
418 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])); | |
419 | } | |
20ad93d4 JD |
420 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
421 | const char *buf, size_t count) | |
1da177e4 | 422 | { |
20ad93d4 JD |
423 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
424 | int nr = sensor_attr->index; | |
425 | ||
b74f3fdd | 426 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
427 | int val = simple_strtol(buf, NULL, 10); |
428 | ||
9a61bf63 | 429 | mutex_lock(&data->update_lock); |
1da177e4 | 430 | data->temp_high[nr] = TEMP_TO_REG(val); |
b74f3fdd | 431 | it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]); |
9a61bf63 | 432 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
433 | return count; |
434 | } | |
20ad93d4 JD |
435 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
436 | const char *buf, size_t count) | |
1da177e4 | 437 | { |
20ad93d4 JD |
438 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
439 | int nr = sensor_attr->index; | |
440 | ||
b74f3fdd | 441 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
442 | int val = simple_strtol(buf, NULL, 10); |
443 | ||
9a61bf63 | 444 | mutex_lock(&data->update_lock); |
1da177e4 | 445 | data->temp_low[nr] = TEMP_TO_REG(val); |
b74f3fdd | 446 | it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]); |
9a61bf63 | 447 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
448 | return count; |
449 | } | |
450 | #define show_temp_offset(offset) \ | |
20ad93d4 JD |
451 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
452 | show_temp, NULL, offset - 1); \ | |
453 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
454 | show_temp_max, set_temp_max, offset - 1); \ | |
455 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | |
456 | show_temp_min, set_temp_min, offset - 1); | |
1da177e4 LT |
457 | |
458 | show_temp_offset(1); | |
459 | show_temp_offset(2); | |
460 | show_temp_offset(3); | |
461 | ||
20ad93d4 JD |
462 | static ssize_t show_sensor(struct device *dev, struct device_attribute *attr, |
463 | char *buf) | |
1da177e4 | 464 | { |
20ad93d4 JD |
465 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
466 | int nr = sensor_attr->index; | |
467 | ||
1da177e4 LT |
468 | struct it87_data *data = it87_update_device(dev); |
469 | u8 reg = data->sensor; /* In case the value is updated while we use it */ | |
470 | ||
471 | if (reg & (1 << nr)) | |
472 | return sprintf(buf, "3\n"); /* thermal diode */ | |
473 | if (reg & (8 << nr)) | |
474 | return sprintf(buf, "2\n"); /* thermistor */ | |
475 | return sprintf(buf, "0\n"); /* disabled */ | |
476 | } | |
20ad93d4 JD |
477 | static ssize_t set_sensor(struct device *dev, struct device_attribute *attr, |
478 | const char *buf, size_t count) | |
1da177e4 | 479 | { |
20ad93d4 JD |
480 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
481 | int nr = sensor_attr->index; | |
482 | ||
b74f3fdd | 483 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
484 | int val = simple_strtol(buf, NULL, 10); |
485 | ||
9a61bf63 | 486 | mutex_lock(&data->update_lock); |
1da177e4 LT |
487 | |
488 | data->sensor &= ~(1 << nr); | |
489 | data->sensor &= ~(8 << nr); | |
490 | /* 3 = thermal diode; 2 = thermistor; 0 = disabled */ | |
491 | if (val == 3) | |
492 | data->sensor |= 1 << nr; | |
493 | else if (val == 2) | |
494 | data->sensor |= 8 << nr; | |
495 | else if (val != 0) { | |
9a61bf63 | 496 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
497 | return -EINVAL; |
498 | } | |
b74f3fdd | 499 | it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor); |
9a61bf63 | 500 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
501 | return count; |
502 | } | |
503 | #define show_sensor_offset(offset) \ | |
20ad93d4 JD |
504 | static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \ |
505 | show_sensor, set_sensor, offset - 1); | |
1da177e4 LT |
506 | |
507 | show_sensor_offset(1); | |
508 | show_sensor_offset(2); | |
509 | show_sensor_offset(3); | |
510 | ||
511 | /* 3 Fans */ | |
20ad93d4 JD |
512 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
513 | char *buf) | |
1da177e4 | 514 | { |
20ad93d4 JD |
515 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
516 | int nr = sensor_attr->index; | |
517 | ||
1da177e4 LT |
518 | struct it87_data *data = it87_update_device(dev); |
519 | return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr], | |
520 | DIV_FROM_REG(data->fan_div[nr]))); | |
521 | } | |
20ad93d4 JD |
522 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
523 | char *buf) | |
1da177e4 | 524 | { |
20ad93d4 JD |
525 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
526 | int nr = sensor_attr->index; | |
527 | ||
1da177e4 LT |
528 | struct it87_data *data = it87_update_device(dev); |
529 | return sprintf(buf,"%d\n", | |
530 | FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]))); | |
531 | } | |
20ad93d4 JD |
532 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
533 | char *buf) | |
1da177e4 | 534 | { |
20ad93d4 JD |
535 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
536 | int nr = sensor_attr->index; | |
537 | ||
1da177e4 LT |
538 | struct it87_data *data = it87_update_device(dev); |
539 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); | |
540 | } | |
20ad93d4 JD |
541 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, |
542 | char *buf) | |
1da177e4 | 543 | { |
20ad93d4 JD |
544 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
545 | int nr = sensor_attr->index; | |
546 | ||
1da177e4 LT |
547 | struct it87_data *data = it87_update_device(dev); |
548 | return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0); | |
549 | } | |
20ad93d4 JD |
550 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
551 | char *buf) | |
1da177e4 | 552 | { |
20ad93d4 JD |
553 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
554 | int nr = sensor_attr->index; | |
555 | ||
1da177e4 LT |
556 | struct it87_data *data = it87_update_device(dev); |
557 | return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]); | |
558 | } | |
f8d0c19a JD |
559 | static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr, |
560 | char *buf) | |
561 | { | |
562 | struct it87_data *data = it87_update_device(dev); | |
563 | int index = (data->fan_ctl >> 4) & 0x07; | |
564 | ||
565 | return sprintf(buf, "%u\n", pwm_freq[index]); | |
566 | } | |
20ad93d4 JD |
567 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
568 | const char *buf, size_t count) | |
1da177e4 | 569 | { |
20ad93d4 JD |
570 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
571 | int nr = sensor_attr->index; | |
572 | ||
b74f3fdd | 573 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 | 574 | int val = simple_strtol(buf, NULL, 10); |
7f999aa7 | 575 | u8 reg; |
1da177e4 | 576 | |
9a61bf63 | 577 | mutex_lock(&data->update_lock); |
b74f3fdd | 578 | reg = it87_read_value(data, IT87_REG_FAN_DIV); |
07eab46d JD |
579 | switch (nr) { |
580 | case 0: data->fan_div[nr] = reg & 0x07; break; | |
581 | case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break; | |
582 | case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break; | |
583 | } | |
584 | ||
1da177e4 | 585 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
c7f1f716 | 586 | it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]); |
9a61bf63 | 587 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
588 | return count; |
589 | } | |
20ad93d4 JD |
590 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
591 | const char *buf, size_t count) | |
1da177e4 | 592 | { |
20ad93d4 JD |
593 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
594 | int nr = sensor_attr->index; | |
595 | ||
b74f3fdd | 596 | struct it87_data *data = dev_get_drvdata(dev); |
b9e349f7 | 597 | unsigned long val = simple_strtoul(buf, NULL, 10); |
8ab4ec3e | 598 | int min; |
1da177e4 LT |
599 | u8 old; |
600 | ||
9a61bf63 | 601 | mutex_lock(&data->update_lock); |
b74f3fdd | 602 | old = it87_read_value(data, IT87_REG_FAN_DIV); |
1da177e4 | 603 | |
8ab4ec3e JD |
604 | /* Save fan min limit */ |
605 | min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); | |
1da177e4 LT |
606 | |
607 | switch (nr) { | |
608 | case 0: | |
609 | case 1: | |
610 | data->fan_div[nr] = DIV_TO_REG(val); | |
611 | break; | |
612 | case 2: | |
613 | if (val < 8) | |
614 | data->fan_div[nr] = 1; | |
615 | else | |
616 | data->fan_div[nr] = 3; | |
617 | } | |
618 | val = old & 0x80; | |
619 | val |= (data->fan_div[0] & 0x07); | |
620 | val |= (data->fan_div[1] & 0x07) << 3; | |
621 | if (data->fan_div[2] == 3) | |
622 | val |= 0x1 << 6; | |
b74f3fdd | 623 | it87_write_value(data, IT87_REG_FAN_DIV, val); |
1da177e4 | 624 | |
8ab4ec3e JD |
625 | /* Restore fan min limit */ |
626 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | |
c7f1f716 | 627 | it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]); |
8ab4ec3e | 628 | |
9a61bf63 | 629 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
630 | return count; |
631 | } | |
20ad93d4 JD |
632 | static ssize_t set_pwm_enable(struct device *dev, |
633 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 634 | { |
20ad93d4 JD |
635 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
636 | int nr = sensor_attr->index; | |
637 | ||
b74f3fdd | 638 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
639 | int val = simple_strtol(buf, NULL, 10); |
640 | ||
9a61bf63 | 641 | mutex_lock(&data->update_lock); |
1da177e4 LT |
642 | |
643 | if (val == 0) { | |
644 | int tmp; | |
645 | /* make sure the fan is on when in on/off mode */ | |
b74f3fdd | 646 | tmp = it87_read_value(data, IT87_REG_FAN_CTL); |
647 | it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr)); | |
1da177e4 LT |
648 | /* set on/off mode */ |
649 | data->fan_main_ctrl &= ~(1 << nr); | |
b74f3fdd | 650 | it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); |
1da177e4 LT |
651 | } else if (val == 1) { |
652 | /* set SmartGuardian mode */ | |
653 | data->fan_main_ctrl |= (1 << nr); | |
b74f3fdd | 654 | it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); |
1da177e4 | 655 | /* set saved pwm value, clear FAN_CTLX PWM mode bit */ |
b74f3fdd | 656 | it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr])); |
1da177e4 | 657 | } else { |
9a61bf63 | 658 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
659 | return -EINVAL; |
660 | } | |
661 | ||
9a61bf63 | 662 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
663 | return count; |
664 | } | |
20ad93d4 JD |
665 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
666 | const char *buf, size_t count) | |
1da177e4 | 667 | { |
20ad93d4 JD |
668 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
669 | int nr = sensor_attr->index; | |
670 | ||
b74f3fdd | 671 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
672 | int val = simple_strtol(buf, NULL, 10); |
673 | ||
674 | if (val < 0 || val > 255) | |
675 | return -EINVAL; | |
676 | ||
9a61bf63 | 677 | mutex_lock(&data->update_lock); |
1da177e4 LT |
678 | data->manual_pwm_ctl[nr] = val; |
679 | if (data->fan_main_ctrl & (1 << nr)) | |
b74f3fdd | 680 | it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr])); |
9a61bf63 | 681 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
682 | return count; |
683 | } | |
f8d0c19a JD |
684 | static ssize_t set_pwm_freq(struct device *dev, |
685 | struct device_attribute *attr, const char *buf, size_t count) | |
686 | { | |
b74f3fdd | 687 | struct it87_data *data = dev_get_drvdata(dev); |
f8d0c19a JD |
688 | unsigned long val = simple_strtoul(buf, NULL, 10); |
689 | int i; | |
690 | ||
691 | /* Search for the nearest available frequency */ | |
692 | for (i = 0; i < 7; i++) { | |
693 | if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2) | |
694 | break; | |
695 | } | |
696 | ||
697 | mutex_lock(&data->update_lock); | |
b74f3fdd | 698 | data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f; |
f8d0c19a | 699 | data->fan_ctl |= i << 4; |
b74f3fdd | 700 | it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl); |
f8d0c19a JD |
701 | mutex_unlock(&data->update_lock); |
702 | ||
703 | return count; | |
704 | } | |
1da177e4 | 705 | |
20ad93d4 JD |
706 | #define show_fan_offset(offset) \ |
707 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ | |
708 | show_fan, NULL, offset - 1); \ | |
709 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
710 | show_fan_min, set_fan_min, offset - 1); \ | |
711 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
712 | show_fan_div, set_fan_div, offset - 1); | |
1da177e4 LT |
713 | |
714 | show_fan_offset(1); | |
715 | show_fan_offset(2); | |
716 | show_fan_offset(3); | |
717 | ||
718 | #define show_pwm_offset(offset) \ | |
20ad93d4 JD |
719 | static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
720 | show_pwm_enable, set_pwm_enable, offset - 1); \ | |
721 | static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ | |
f8d0c19a JD |
722 | show_pwm, set_pwm, offset - 1); \ |
723 | static DEVICE_ATTR(pwm##offset##_freq, \ | |
724 | (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \ | |
725 | show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); | |
1da177e4 LT |
726 | |
727 | show_pwm_offset(1); | |
728 | show_pwm_offset(2); | |
729 | show_pwm_offset(3); | |
730 | ||
17d648bf JD |
731 | /* A different set of callbacks for 16-bit fans */ |
732 | static ssize_t show_fan16(struct device *dev, struct device_attribute *attr, | |
733 | char *buf) | |
734 | { | |
735 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
736 | int nr = sensor_attr->index; | |
737 | struct it87_data *data = it87_update_device(dev); | |
738 | return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr])); | |
739 | } | |
740 | ||
741 | static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr, | |
742 | char *buf) | |
743 | { | |
744 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
745 | int nr = sensor_attr->index; | |
746 | struct it87_data *data = it87_update_device(dev); | |
747 | return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr])); | |
748 | } | |
749 | ||
750 | static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr, | |
751 | const char *buf, size_t count) | |
752 | { | |
753 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
754 | int nr = sensor_attr->index; | |
b74f3fdd | 755 | struct it87_data *data = dev_get_drvdata(dev); |
17d648bf JD |
756 | int val = simple_strtol(buf, NULL, 10); |
757 | ||
758 | mutex_lock(&data->update_lock); | |
759 | data->fan_min[nr] = FAN16_TO_REG(val); | |
c7f1f716 | 760 | it87_write_value(data, IT87_REG_FAN_MIN[nr], |
17d648bf | 761 | data->fan_min[nr] & 0xff); |
c7f1f716 | 762 | it87_write_value(data, IT87_REG_FANX_MIN[nr], |
17d648bf JD |
763 | data->fan_min[nr] >> 8); |
764 | mutex_unlock(&data->update_lock); | |
765 | return count; | |
766 | } | |
767 | ||
768 | /* We want to use the same sysfs file names as 8-bit fans, but we need | |
769 | different variable names, so we have to use SENSOR_ATTR instead of | |
770 | SENSOR_DEVICE_ATTR. */ | |
771 | #define show_fan16_offset(offset) \ | |
772 | static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \ | |
773 | = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \ | |
774 | show_fan16, NULL, offset - 1); \ | |
775 | static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \ | |
776 | = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
777 | show_fan16_min, set_fan16_min, offset - 1) | |
778 | ||
779 | show_fan16_offset(1); | |
780 | show_fan16_offset(2); | |
781 | show_fan16_offset(3); | |
c7f1f716 JD |
782 | show_fan16_offset(4); |
783 | show_fan16_offset(5); | |
17d648bf | 784 | |
1da177e4 | 785 | /* Alarms */ |
30f74292 | 786 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
787 | { |
788 | struct it87_data *data = it87_update_device(dev); | |
68188ba7 | 789 | return sprintf(buf, "%u\n", data->alarms); |
1da177e4 | 790 | } |
1d66c64c | 791 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
1da177e4 | 792 | |
0124dd78 JD |
793 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
794 | char *buf) | |
795 | { | |
796 | int bitnr = to_sensor_dev_attr(attr)->index; | |
797 | struct it87_data *data = it87_update_device(dev); | |
798 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
799 | } | |
800 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8); | |
801 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9); | |
802 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10); | |
803 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11); | |
804 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12); | |
805 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13); | |
806 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14); | |
807 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15); | |
808 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0); | |
809 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1); | |
810 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2); | |
811 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3); | |
812 | static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6); | |
813 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16); | |
814 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17); | |
815 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18); | |
816 | ||
1da177e4 | 817 | static ssize_t |
30f74292 | 818 | show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 | 819 | { |
90d6619a | 820 | struct it87_data *data = dev_get_drvdata(dev); |
a7be58a1 | 821 | return sprintf(buf, "%u\n", data->vrm); |
1da177e4 LT |
822 | } |
823 | static ssize_t | |
30f74292 | 824 | store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 | 825 | { |
b74f3fdd | 826 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
827 | u32 val; |
828 | ||
829 | val = simple_strtoul(buf, NULL, 10); | |
830 | data->vrm = val; | |
831 | ||
832 | return count; | |
833 | } | |
834 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); | |
1da177e4 LT |
835 | |
836 | static ssize_t | |
30f74292 | 837 | show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
838 | { |
839 | struct it87_data *data = it87_update_device(dev); | |
840 | return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm)); | |
841 | } | |
842 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); | |
87808be4 | 843 | |
b74f3fdd | 844 | static ssize_t show_name(struct device *dev, struct device_attribute |
845 | *devattr, char *buf) | |
846 | { | |
847 | struct it87_data *data = dev_get_drvdata(dev); | |
848 | return sprintf(buf, "%s\n", data->name); | |
849 | } | |
850 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | |
851 | ||
87808be4 JD |
852 | static struct attribute *it87_attributes[] = { |
853 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
854 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
855 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
856 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
857 | &sensor_dev_attr_in4_input.dev_attr.attr, | |
858 | &sensor_dev_attr_in5_input.dev_attr.attr, | |
859 | &sensor_dev_attr_in6_input.dev_attr.attr, | |
860 | &sensor_dev_attr_in7_input.dev_attr.attr, | |
861 | &sensor_dev_attr_in8_input.dev_attr.attr, | |
862 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
863 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
864 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
865 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
866 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
867 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
868 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
869 | &sensor_dev_attr_in7_min.dev_attr.attr, | |
870 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
871 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
872 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
873 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
874 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
875 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
876 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
877 | &sensor_dev_attr_in7_max.dev_attr.attr, | |
0124dd78 JD |
878 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
879 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | |
880 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
881 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
882 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | |
883 | &sensor_dev_attr_in5_alarm.dev_attr.attr, | |
884 | &sensor_dev_attr_in6_alarm.dev_attr.attr, | |
885 | &sensor_dev_attr_in7_alarm.dev_attr.attr, | |
87808be4 JD |
886 | |
887 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
888 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
889 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
890 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
891 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
892 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
893 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
894 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
895 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
896 | &sensor_dev_attr_temp1_type.dev_attr.attr, | |
897 | &sensor_dev_attr_temp2_type.dev_attr.attr, | |
898 | &sensor_dev_attr_temp3_type.dev_attr.attr, | |
0124dd78 JD |
899 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
900 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, | |
901 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
87808be4 JD |
902 | |
903 | &dev_attr_alarms.attr, | |
b74f3fdd | 904 | &dev_attr_name.attr, |
87808be4 JD |
905 | NULL |
906 | }; | |
907 | ||
908 | static const struct attribute_group it87_group = { | |
909 | .attrs = it87_attributes, | |
910 | }; | |
911 | ||
912 | static struct attribute *it87_attributes_opt[] = { | |
913 | &sensor_dev_attr_fan1_input16.dev_attr.attr, | |
914 | &sensor_dev_attr_fan1_min16.dev_attr.attr, | |
915 | &sensor_dev_attr_fan2_input16.dev_attr.attr, | |
916 | &sensor_dev_attr_fan2_min16.dev_attr.attr, | |
917 | &sensor_dev_attr_fan3_input16.dev_attr.attr, | |
918 | &sensor_dev_attr_fan3_min16.dev_attr.attr, | |
c7f1f716 JD |
919 | &sensor_dev_attr_fan4_input16.dev_attr.attr, |
920 | &sensor_dev_attr_fan4_min16.dev_attr.attr, | |
921 | &sensor_dev_attr_fan5_input16.dev_attr.attr, | |
922 | &sensor_dev_attr_fan5_min16.dev_attr.attr, | |
87808be4 JD |
923 | |
924 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
925 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
926 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
927 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
928 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
929 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
930 | &sensor_dev_attr_fan3_input.dev_attr.attr, | |
931 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
932 | &sensor_dev_attr_fan3_div.dev_attr.attr, | |
933 | ||
0124dd78 JD |
934 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
935 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
936 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | |
937 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, | |
938 | &sensor_dev_attr_fan5_alarm.dev_attr.attr, | |
939 | ||
87808be4 JD |
940 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
941 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, | |
942 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, | |
943 | &sensor_dev_attr_pwm1.dev_attr.attr, | |
944 | &sensor_dev_attr_pwm2.dev_attr.attr, | |
945 | &sensor_dev_attr_pwm3.dev_attr.attr, | |
d5b0b5d6 JD |
946 | &dev_attr_pwm1_freq.attr, |
947 | &dev_attr_pwm2_freq.attr, | |
948 | &dev_attr_pwm3_freq.attr, | |
87808be4 JD |
949 | |
950 | &dev_attr_vrm.attr, | |
951 | &dev_attr_cpu0_vid.attr, | |
952 | NULL | |
953 | }; | |
954 | ||
955 | static const struct attribute_group it87_group_opt = { | |
956 | .attrs = it87_attributes_opt, | |
957 | }; | |
1da177e4 | 958 | |
2d8672c5 | 959 | /* SuperIO detection - will change isa_address if a chip is found */ |
b74f3fdd | 960 | static int __init it87_find(unsigned short *address, |
961 | struct it87_sio_data *sio_data) | |
1da177e4 LT |
962 | { |
963 | int err = -ENODEV; | |
b74f3fdd | 964 | u16 chip_type; |
1da177e4 LT |
965 | |
966 | superio_enter(); | |
67b671bc | 967 | chip_type = force_id ? force_id : superio_inw(DEVID); |
b74f3fdd | 968 | |
969 | switch (chip_type) { | |
970 | case IT8705F_DEVID: | |
971 | sio_data->type = it87; | |
972 | break; | |
973 | case IT8712F_DEVID: | |
974 | sio_data->type = it8712; | |
975 | break; | |
976 | case IT8716F_DEVID: | |
977 | case IT8726F_DEVID: | |
978 | sio_data->type = it8716; | |
979 | break; | |
980 | case IT8718F_DEVID: | |
981 | sio_data->type = it8718; | |
982 | break; | |
983 | case 0xffff: /* No device at all */ | |
984 | goto exit; | |
985 | default: | |
986 | pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n", | |
987 | chip_type); | |
988 | goto exit; | |
989 | } | |
1da177e4 | 990 | |
87673dd7 | 991 | superio_select(PME); |
1da177e4 LT |
992 | if (!(superio_inb(IT87_ACT_REG) & 0x01)) { |
993 | pr_info("it87: Device not activated, skipping\n"); | |
994 | goto exit; | |
995 | } | |
996 | ||
997 | *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1); | |
998 | if (*address == 0) { | |
999 | pr_info("it87: Base address not set, skipping\n"); | |
1000 | goto exit; | |
1001 | } | |
1002 | ||
1003 | err = 0; | |
0475169c | 1004 | sio_data->revision = superio_inb(DEVREV) & 0x0f; |
1da177e4 | 1005 | pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n", |
0475169c | 1006 | chip_type, *address, sio_data->revision); |
1da177e4 | 1007 | |
87673dd7 JD |
1008 | /* Read GPIO config and VID value from LDN 7 (GPIO) */ |
1009 | if (chip_type != IT8705F_DEVID) { | |
1010 | int reg; | |
1011 | ||
1012 | superio_select(GPIO); | |
1013 | if (chip_type == it8718) | |
b74f3fdd | 1014 | sio_data->vid_value = superio_inb(IT87_SIO_VID_REG); |
87673dd7 JD |
1015 | |
1016 | reg = superio_inb(IT87_SIO_PINX2_REG); | |
1017 | if (reg & (1 << 0)) | |
1018 | pr_info("it87: in3 is VCC (+5V)\n"); | |
1019 | if (reg & (1 << 1)) | |
1020 | pr_info("it87: in7 is VCCH (+5V Stand-By)\n"); | |
1021 | } | |
1022 | ||
1da177e4 LT |
1023 | exit: |
1024 | superio_exit(); | |
1025 | return err; | |
1026 | } | |
1027 | ||
b74f3fdd | 1028 | static int __devinit it87_probe(struct platform_device *pdev) |
1da177e4 | 1029 | { |
1da177e4 | 1030 | struct it87_data *data; |
b74f3fdd | 1031 | struct resource *res; |
1032 | struct device *dev = &pdev->dev; | |
1033 | struct it87_sio_data *sio_data = dev->platform_data; | |
1da177e4 | 1034 | int err = 0; |
1da177e4 | 1035 | int enable_pwm_interface; |
b74f3fdd | 1036 | static const char *names[] = { |
1037 | "it87", | |
1038 | "it8712", | |
1039 | "it8716", | |
1040 | "it8718", | |
1041 | }; | |
1042 | ||
1043 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | |
87b4b663 | 1044 | if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) { |
b74f3fdd | 1045 | dev_err(dev, "Failed to request region 0x%lx-0x%lx\n", |
1046 | (unsigned long)res->start, | |
87b4b663 | 1047 | (unsigned long)(res->start + IT87_EC_EXTENT - 1)); |
8e9afcbb JD |
1048 | err = -EBUSY; |
1049 | goto ERROR0; | |
1050 | } | |
1da177e4 | 1051 | |
ba9c2e8d | 1052 | if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) { |
1da177e4 LT |
1053 | err = -ENOMEM; |
1054 | goto ERROR1; | |
1055 | } | |
1da177e4 | 1056 | |
b74f3fdd | 1057 | data->addr = res->start; |
1058 | data->type = sio_data->type; | |
0475169c | 1059 | data->revision = sio_data->revision; |
b74f3fdd | 1060 | data->name = names[sio_data->type]; |
1da177e4 LT |
1061 | |
1062 | /* Now, we do the remaining detection. */ | |
b74f3fdd | 1063 | if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) |
1064 | || it87_read_value(data, IT87_REG_CHIPID) != 0x90) { | |
8e9afcbb JD |
1065 | err = -ENODEV; |
1066 | goto ERROR2; | |
1da177e4 LT |
1067 | } |
1068 | ||
b74f3fdd | 1069 | platform_set_drvdata(pdev, data); |
1da177e4 | 1070 | |
9a61bf63 | 1071 | mutex_init(&data->update_lock); |
1da177e4 | 1072 | |
1da177e4 | 1073 | /* Check PWM configuration */ |
b74f3fdd | 1074 | enable_pwm_interface = it87_check_pwm(dev); |
1da177e4 LT |
1075 | |
1076 | /* Initialize the IT87 chip */ | |
b74f3fdd | 1077 | it87_init_device(pdev); |
1da177e4 LT |
1078 | |
1079 | /* Register sysfs hooks */ | |
b74f3fdd | 1080 | if ((err = sysfs_create_group(&dev->kobj, &it87_group))) |
1081 | goto ERROR2; | |
17d648bf | 1082 | |
9060f8bd | 1083 | /* Do not create fan files for disabled fans */ |
0475169c | 1084 | if (has_16bit_fans(data)) { |
87673dd7 | 1085 | /* 16-bit tachometers */ |
9060f8bd | 1086 | if (data->has_fan & (1 << 0)) { |
b74f3fdd | 1087 | if ((err = device_create_file(dev, |
87808be4 | 1088 | &sensor_dev_attr_fan1_input16.dev_attr)) |
b74f3fdd | 1089 | || (err = device_create_file(dev, |
0124dd78 JD |
1090 | &sensor_dev_attr_fan1_min16.dev_attr)) |
1091 | || (err = device_create_file(dev, | |
1092 | &sensor_dev_attr_fan1_alarm.dev_attr))) | |
87808be4 | 1093 | goto ERROR4; |
9060f8bd JD |
1094 | } |
1095 | if (data->has_fan & (1 << 1)) { | |
b74f3fdd | 1096 | if ((err = device_create_file(dev, |
87808be4 | 1097 | &sensor_dev_attr_fan2_input16.dev_attr)) |
b74f3fdd | 1098 | || (err = device_create_file(dev, |
0124dd78 JD |
1099 | &sensor_dev_attr_fan2_min16.dev_attr)) |
1100 | || (err = device_create_file(dev, | |
1101 | &sensor_dev_attr_fan2_alarm.dev_attr))) | |
87808be4 | 1102 | goto ERROR4; |
9060f8bd JD |
1103 | } |
1104 | if (data->has_fan & (1 << 2)) { | |
b74f3fdd | 1105 | if ((err = device_create_file(dev, |
87808be4 | 1106 | &sensor_dev_attr_fan3_input16.dev_attr)) |
b74f3fdd | 1107 | || (err = device_create_file(dev, |
0124dd78 JD |
1108 | &sensor_dev_attr_fan3_min16.dev_attr)) |
1109 | || (err = device_create_file(dev, | |
1110 | &sensor_dev_attr_fan3_alarm.dev_attr))) | |
87808be4 | 1111 | goto ERROR4; |
9060f8bd | 1112 | } |
c7f1f716 JD |
1113 | if (data->has_fan & (1 << 3)) { |
1114 | if ((err = device_create_file(dev, | |
1115 | &sensor_dev_attr_fan4_input16.dev_attr)) | |
1116 | || (err = device_create_file(dev, | |
0124dd78 JD |
1117 | &sensor_dev_attr_fan4_min16.dev_attr)) |
1118 | || (err = device_create_file(dev, | |
1119 | &sensor_dev_attr_fan4_alarm.dev_attr))) | |
c7f1f716 JD |
1120 | goto ERROR4; |
1121 | } | |
1122 | if (data->has_fan & (1 << 4)) { | |
1123 | if ((err = device_create_file(dev, | |
1124 | &sensor_dev_attr_fan5_input16.dev_attr)) | |
1125 | || (err = device_create_file(dev, | |
0124dd78 JD |
1126 | &sensor_dev_attr_fan5_min16.dev_attr)) |
1127 | || (err = device_create_file(dev, | |
1128 | &sensor_dev_attr_fan5_alarm.dev_attr))) | |
c7f1f716 JD |
1129 | goto ERROR4; |
1130 | } | |
17d648bf | 1131 | } else { |
87673dd7 | 1132 | /* 8-bit tachometers with clock divider */ |
9060f8bd | 1133 | if (data->has_fan & (1 << 0)) { |
b74f3fdd | 1134 | if ((err = device_create_file(dev, |
87808be4 | 1135 | &sensor_dev_attr_fan1_input.dev_attr)) |
b74f3fdd | 1136 | || (err = device_create_file(dev, |
87808be4 | 1137 | &sensor_dev_attr_fan1_min.dev_attr)) |
b74f3fdd | 1138 | || (err = device_create_file(dev, |
0124dd78 JD |
1139 | &sensor_dev_attr_fan1_div.dev_attr)) |
1140 | || (err = device_create_file(dev, | |
1141 | &sensor_dev_attr_fan1_alarm.dev_attr))) | |
87808be4 | 1142 | goto ERROR4; |
9060f8bd JD |
1143 | } |
1144 | if (data->has_fan & (1 << 1)) { | |
b74f3fdd | 1145 | if ((err = device_create_file(dev, |
87808be4 | 1146 | &sensor_dev_attr_fan2_input.dev_attr)) |
b74f3fdd | 1147 | || (err = device_create_file(dev, |
87808be4 | 1148 | &sensor_dev_attr_fan2_min.dev_attr)) |
b74f3fdd | 1149 | || (err = device_create_file(dev, |
0124dd78 JD |
1150 | &sensor_dev_attr_fan2_div.dev_attr)) |
1151 | || (err = device_create_file(dev, | |
1152 | &sensor_dev_attr_fan2_alarm.dev_attr))) | |
87808be4 | 1153 | goto ERROR4; |
9060f8bd JD |
1154 | } |
1155 | if (data->has_fan & (1 << 2)) { | |
b74f3fdd | 1156 | if ((err = device_create_file(dev, |
87808be4 | 1157 | &sensor_dev_attr_fan3_input.dev_attr)) |
b74f3fdd | 1158 | || (err = device_create_file(dev, |
87808be4 | 1159 | &sensor_dev_attr_fan3_min.dev_attr)) |
b74f3fdd | 1160 | || (err = device_create_file(dev, |
0124dd78 JD |
1161 | &sensor_dev_attr_fan3_div.dev_attr)) |
1162 | || (err = device_create_file(dev, | |
1163 | &sensor_dev_attr_fan3_alarm.dev_attr))) | |
87808be4 | 1164 | goto ERROR4; |
9060f8bd | 1165 | } |
17d648bf JD |
1166 | } |
1167 | ||
1da177e4 | 1168 | if (enable_pwm_interface) { |
b74f3fdd | 1169 | if ((err = device_create_file(dev, |
87808be4 | 1170 | &sensor_dev_attr_pwm1_enable.dev_attr)) |
b74f3fdd | 1171 | || (err = device_create_file(dev, |
87808be4 | 1172 | &sensor_dev_attr_pwm2_enable.dev_attr)) |
b74f3fdd | 1173 | || (err = device_create_file(dev, |
87808be4 | 1174 | &sensor_dev_attr_pwm3_enable.dev_attr)) |
b74f3fdd | 1175 | || (err = device_create_file(dev, |
87808be4 | 1176 | &sensor_dev_attr_pwm1.dev_attr)) |
b74f3fdd | 1177 | || (err = device_create_file(dev, |
87808be4 | 1178 | &sensor_dev_attr_pwm2.dev_attr)) |
b74f3fdd | 1179 | || (err = device_create_file(dev, |
f8d0c19a | 1180 | &sensor_dev_attr_pwm3.dev_attr)) |
b74f3fdd | 1181 | || (err = device_create_file(dev, |
f8d0c19a | 1182 | &dev_attr_pwm1_freq)) |
b74f3fdd | 1183 | || (err = device_create_file(dev, |
f8d0c19a | 1184 | &dev_attr_pwm2_freq)) |
b74f3fdd | 1185 | || (err = device_create_file(dev, |
f8d0c19a | 1186 | &dev_attr_pwm3_freq))) |
87808be4 | 1187 | goto ERROR4; |
1da177e4 LT |
1188 | } |
1189 | ||
87673dd7 JD |
1190 | if (data->type == it8712 || data->type == it8716 |
1191 | || data->type == it8718) { | |
303760b4 | 1192 | data->vrm = vid_which_vrm(); |
87673dd7 | 1193 | /* VID reading from Super-I/O config space if available */ |
b74f3fdd | 1194 | data->vid = sio_data->vid_value; |
1195 | if ((err = device_create_file(dev, | |
87808be4 | 1196 | &dev_attr_vrm)) |
b74f3fdd | 1197 | || (err = device_create_file(dev, |
87808be4 JD |
1198 | &dev_attr_cpu0_vid))) |
1199 | goto ERROR4; | |
1200 | } | |
1201 | ||
1beeffe4 TJ |
1202 | data->hwmon_dev = hwmon_device_register(dev); |
1203 | if (IS_ERR(data->hwmon_dev)) { | |
1204 | err = PTR_ERR(data->hwmon_dev); | |
87808be4 | 1205 | goto ERROR4; |
1da177e4 LT |
1206 | } |
1207 | ||
1208 | return 0; | |
1209 | ||
87808be4 | 1210 | ERROR4: |
b74f3fdd | 1211 | sysfs_remove_group(&dev->kobj, &it87_group); |
1212 | sysfs_remove_group(&dev->kobj, &it87_group_opt); | |
1da177e4 | 1213 | ERROR2: |
b74f3fdd | 1214 | platform_set_drvdata(pdev, NULL); |
1da177e4 LT |
1215 | kfree(data); |
1216 | ERROR1: | |
87b4b663 | 1217 | release_region(res->start, IT87_EC_EXTENT); |
1da177e4 LT |
1218 | ERROR0: |
1219 | return err; | |
1220 | } | |
1221 | ||
b74f3fdd | 1222 | static int __devexit it87_remove(struct platform_device *pdev) |
1da177e4 | 1223 | { |
b74f3fdd | 1224 | struct it87_data *data = platform_get_drvdata(pdev); |
1da177e4 | 1225 | |
1beeffe4 | 1226 | hwmon_device_unregister(data->hwmon_dev); |
b74f3fdd | 1227 | sysfs_remove_group(&pdev->dev.kobj, &it87_group); |
1228 | sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt); | |
943b0830 | 1229 | |
87b4b663 | 1230 | release_region(data->addr, IT87_EC_EXTENT); |
b74f3fdd | 1231 | platform_set_drvdata(pdev, NULL); |
943b0830 | 1232 | kfree(data); |
1da177e4 LT |
1233 | |
1234 | return 0; | |
1235 | } | |
1236 | ||
7f999aa7 | 1237 | /* Must be called with data->update_lock held, except during initialization. |
1da177e4 LT |
1238 | We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks, |
1239 | would slow down the IT87 access and should not be necessary. */ | |
b74f3fdd | 1240 | static int it87_read_value(struct it87_data *data, u8 reg) |
1da177e4 | 1241 | { |
b74f3fdd | 1242 | outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET); |
1243 | return inb_p(data->addr + IT87_DATA_REG_OFFSET); | |
1da177e4 LT |
1244 | } |
1245 | ||
7f999aa7 | 1246 | /* Must be called with data->update_lock held, except during initialization. |
1da177e4 LT |
1247 | We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks, |
1248 | would slow down the IT87 access and should not be necessary. */ | |
b74f3fdd | 1249 | static void it87_write_value(struct it87_data *data, u8 reg, u8 value) |
1da177e4 | 1250 | { |
b74f3fdd | 1251 | outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET); |
1252 | outb_p(value, data->addr + IT87_DATA_REG_OFFSET); | |
1da177e4 LT |
1253 | } |
1254 | ||
1255 | /* Return 1 if and only if the PWM interface is safe to use */ | |
b74f3fdd | 1256 | static int __devinit it87_check_pwm(struct device *dev) |
1da177e4 | 1257 | { |
b74f3fdd | 1258 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
1259 | /* Some BIOSes fail to correctly configure the IT87 fans. All fans off |
1260 | * and polarity set to active low is sign that this is the case so we | |
1261 | * disable pwm control to protect the user. */ | |
b74f3fdd | 1262 | int tmp = it87_read_value(data, IT87_REG_FAN_CTL); |
1da177e4 LT |
1263 | if ((tmp & 0x87) == 0) { |
1264 | if (fix_pwm_polarity) { | |
1265 | /* The user asks us to attempt a chip reconfiguration. | |
1266 | * This means switching to active high polarity and | |
1267 | * inverting all fan speed values. */ | |
1268 | int i; | |
1269 | u8 pwm[3]; | |
1270 | ||
1271 | for (i = 0; i < 3; i++) | |
b74f3fdd | 1272 | pwm[i] = it87_read_value(data, |
1da177e4 LT |
1273 | IT87_REG_PWM(i)); |
1274 | ||
1275 | /* If any fan is in automatic pwm mode, the polarity | |
1276 | * might be correct, as suspicious as it seems, so we | |
1277 | * better don't change anything (but still disable the | |
1278 | * PWM interface). */ | |
1279 | if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) { | |
b74f3fdd | 1280 | dev_info(dev, "Reconfiguring PWM to " |
1da177e4 | 1281 | "active high polarity\n"); |
b74f3fdd | 1282 | it87_write_value(data, IT87_REG_FAN_CTL, |
1da177e4 LT |
1283 | tmp | 0x87); |
1284 | for (i = 0; i < 3; i++) | |
b74f3fdd | 1285 | it87_write_value(data, |
1da177e4 LT |
1286 | IT87_REG_PWM(i), |
1287 | 0x7f & ~pwm[i]); | |
1288 | return 1; | |
1289 | } | |
1290 | ||
b74f3fdd | 1291 | dev_info(dev, "PWM configuration is " |
1da177e4 LT |
1292 | "too broken to be fixed\n"); |
1293 | } | |
1294 | ||
b74f3fdd | 1295 | dev_info(dev, "Detected broken BIOS " |
1da177e4 LT |
1296 | "defaults, disabling PWM interface\n"); |
1297 | return 0; | |
1298 | } else if (fix_pwm_polarity) { | |
b74f3fdd | 1299 | dev_info(dev, "PWM configuration looks " |
1da177e4 LT |
1300 | "sane, won't touch\n"); |
1301 | } | |
1302 | ||
1303 | return 1; | |
1304 | } | |
1305 | ||
1306 | /* Called when we have found a new IT87. */ | |
b74f3fdd | 1307 | static void __devinit it87_init_device(struct platform_device *pdev) |
1da177e4 | 1308 | { |
b74f3fdd | 1309 | struct it87_data *data = platform_get_drvdata(pdev); |
1da177e4 LT |
1310 | int tmp, i; |
1311 | ||
1312 | /* initialize to sane defaults: | |
1313 | * - if the chip is in manual pwm mode, this will be overwritten with | |
1314 | * the actual settings on the chip (so in this case, initialization | |
1315 | * is not needed) | |
1316 | * - if in automatic or on/off mode, we could switch to manual mode, | |
1317 | * read the registers and set manual_pwm_ctl accordingly, but currently | |
1318 | * this is not implemented, so we initialize to something sane */ | |
1319 | for (i = 0; i < 3; i++) { | |
1320 | data->manual_pwm_ctl[i] = 0xff; | |
1321 | } | |
1322 | ||
c5df9b7a JD |
1323 | /* Some chips seem to have default value 0xff for all limit |
1324 | * registers. For low voltage limits it makes no sense and triggers | |
1325 | * alarms, so change to 0 instead. For high temperature limits, it | |
1326 | * means -1 degree C, which surprisingly doesn't trigger an alarm, | |
1327 | * but is still confusing, so change to 127 degrees C. */ | |
1328 | for (i = 0; i < 8; i++) { | |
b74f3fdd | 1329 | tmp = it87_read_value(data, IT87_REG_VIN_MIN(i)); |
c5df9b7a | 1330 | if (tmp == 0xff) |
b74f3fdd | 1331 | it87_write_value(data, IT87_REG_VIN_MIN(i), 0); |
c5df9b7a JD |
1332 | } |
1333 | for (i = 0; i < 3; i++) { | |
b74f3fdd | 1334 | tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i)); |
c5df9b7a | 1335 | if (tmp == 0xff) |
b74f3fdd | 1336 | it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127); |
c5df9b7a JD |
1337 | } |
1338 | ||
1da177e4 | 1339 | /* Check if temperature channnels are reset manually or by some reason */ |
b74f3fdd | 1340 | tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE); |
1da177e4 LT |
1341 | if ((tmp & 0x3f) == 0) { |
1342 | /* Temp1,Temp3=thermistor; Temp2=thermal diode */ | |
1343 | tmp = (tmp & 0xc0) | 0x2a; | |
b74f3fdd | 1344 | it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp); |
1da177e4 LT |
1345 | } |
1346 | data->sensor = tmp; | |
1347 | ||
1348 | /* Check if voltage monitors are reset manually or by some reason */ | |
b74f3fdd | 1349 | tmp = it87_read_value(data, IT87_REG_VIN_ENABLE); |
1da177e4 LT |
1350 | if ((tmp & 0xff) == 0) { |
1351 | /* Enable all voltage monitors */ | |
b74f3fdd | 1352 | it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff); |
1da177e4 LT |
1353 | } |
1354 | ||
1355 | /* Check if tachometers are reset manually or by some reason */ | |
b74f3fdd | 1356 | data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL); |
1da177e4 LT |
1357 | if ((data->fan_main_ctrl & 0x70) == 0) { |
1358 | /* Enable all fan tachometers */ | |
1359 | data->fan_main_ctrl |= 0x70; | |
b74f3fdd | 1360 | it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); |
1da177e4 | 1361 | } |
9060f8bd | 1362 | data->has_fan = (data->fan_main_ctrl >> 4) & 0x07; |
1da177e4 | 1363 | |
17d648bf | 1364 | /* Set tachometers to 16-bit mode if needed */ |
0475169c | 1365 | if (has_16bit_fans(data)) { |
b74f3fdd | 1366 | tmp = it87_read_value(data, IT87_REG_FAN_16BIT); |
9060f8bd | 1367 | if (~tmp & 0x07 & data->has_fan) { |
b74f3fdd | 1368 | dev_dbg(&pdev->dev, |
17d648bf | 1369 | "Setting fan1-3 to 16-bit mode\n"); |
b74f3fdd | 1370 | it87_write_value(data, IT87_REG_FAN_16BIT, |
17d648bf JD |
1371 | tmp | 0x07); |
1372 | } | |
c7f1f716 JD |
1373 | if (tmp & (1 << 4)) |
1374 | data->has_fan |= (1 << 3); /* fan4 enabled */ | |
1375 | if (tmp & (1 << 5)) | |
1376 | data->has_fan |= (1 << 4); /* fan5 enabled */ | |
17d648bf JD |
1377 | } |
1378 | ||
1da177e4 LT |
1379 | /* Set current fan mode registers and the default settings for the |
1380 | * other mode registers */ | |
1381 | for (i = 0; i < 3; i++) { | |
1382 | if (data->fan_main_ctrl & (1 << i)) { | |
1383 | /* pwm mode */ | |
b74f3fdd | 1384 | tmp = it87_read_value(data, IT87_REG_PWM(i)); |
1da177e4 LT |
1385 | if (tmp & 0x80) { |
1386 | /* automatic pwm - not yet implemented, but | |
1387 | * leave the settings made by the BIOS alone | |
1388 | * until a change is requested via the sysfs | |
1389 | * interface */ | |
1390 | } else { | |
1391 | /* manual pwm */ | |
1392 | data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp); | |
1393 | } | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | /* Start monitoring */ | |
b74f3fdd | 1398 | it87_write_value(data, IT87_REG_CONFIG, |
1399 | (it87_read_value(data, IT87_REG_CONFIG) & 0x36) | |
1da177e4 LT |
1400 | | (update_vbat ? 0x41 : 0x01)); |
1401 | } | |
1402 | ||
1403 | static struct it87_data *it87_update_device(struct device *dev) | |
1404 | { | |
b74f3fdd | 1405 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
1406 | int i; |
1407 | ||
9a61bf63 | 1408 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1409 | |
1410 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
1411 | || !data->valid) { | |
1412 | ||
1413 | if (update_vbat) { | |
1414 | /* Cleared after each update, so reenable. Value | |
1415 | returned by this read will be previous value */ | |
b74f3fdd | 1416 | it87_write_value(data, IT87_REG_CONFIG, |
1417 | it87_read_value(data, IT87_REG_CONFIG) | 0x40); | |
1da177e4 LT |
1418 | } |
1419 | for (i = 0; i <= 7; i++) { | |
1420 | data->in[i] = | |
b74f3fdd | 1421 | it87_read_value(data, IT87_REG_VIN(i)); |
1da177e4 | 1422 | data->in_min[i] = |
b74f3fdd | 1423 | it87_read_value(data, IT87_REG_VIN_MIN(i)); |
1da177e4 | 1424 | data->in_max[i] = |
b74f3fdd | 1425 | it87_read_value(data, IT87_REG_VIN_MAX(i)); |
1da177e4 | 1426 | } |
3543a53f | 1427 | /* in8 (battery) has no limit registers */ |
1da177e4 | 1428 | data->in[8] = |
b74f3fdd | 1429 | it87_read_value(data, IT87_REG_VIN(8)); |
1da177e4 | 1430 | |
c7f1f716 | 1431 | for (i = 0; i < 5; i++) { |
9060f8bd JD |
1432 | /* Skip disabled fans */ |
1433 | if (!(data->has_fan & (1 << i))) | |
1434 | continue; | |
1435 | ||
1da177e4 | 1436 | data->fan_min[i] = |
c7f1f716 | 1437 | it87_read_value(data, IT87_REG_FAN_MIN[i]); |
b74f3fdd | 1438 | data->fan[i] = it87_read_value(data, |
c7f1f716 | 1439 | IT87_REG_FAN[i]); |
17d648bf | 1440 | /* Add high byte if in 16-bit mode */ |
0475169c | 1441 | if (has_16bit_fans(data)) { |
b74f3fdd | 1442 | data->fan[i] |= it87_read_value(data, |
c7f1f716 | 1443 | IT87_REG_FANX[i]) << 8; |
b74f3fdd | 1444 | data->fan_min[i] |= it87_read_value(data, |
c7f1f716 | 1445 | IT87_REG_FANX_MIN[i]) << 8; |
17d648bf | 1446 | } |
1da177e4 LT |
1447 | } |
1448 | for (i = 0; i < 3; i++) { | |
1449 | data->temp[i] = | |
b74f3fdd | 1450 | it87_read_value(data, IT87_REG_TEMP(i)); |
1da177e4 | 1451 | data->temp_high[i] = |
b74f3fdd | 1452 | it87_read_value(data, IT87_REG_TEMP_HIGH(i)); |
1da177e4 | 1453 | data->temp_low[i] = |
b74f3fdd | 1454 | it87_read_value(data, IT87_REG_TEMP_LOW(i)); |
1da177e4 LT |
1455 | } |
1456 | ||
17d648bf | 1457 | /* Newer chips don't have clock dividers */ |
0475169c | 1458 | if ((data->has_fan & 0x07) && !has_16bit_fans(data)) { |
b74f3fdd | 1459 | i = it87_read_value(data, IT87_REG_FAN_DIV); |
17d648bf JD |
1460 | data->fan_div[0] = i & 0x07; |
1461 | data->fan_div[1] = (i >> 3) & 0x07; | |
1462 | data->fan_div[2] = (i & 0x40) ? 3 : 1; | |
1463 | } | |
1da177e4 LT |
1464 | |
1465 | data->alarms = | |
b74f3fdd | 1466 | it87_read_value(data, IT87_REG_ALARM1) | |
1467 | (it87_read_value(data, IT87_REG_ALARM2) << 8) | | |
1468 | (it87_read_value(data, IT87_REG_ALARM3) << 16); | |
1469 | data->fan_main_ctrl = it87_read_value(data, | |
1470 | IT87_REG_FAN_MAIN_CTRL); | |
1471 | data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL); | |
1472 | ||
1473 | data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE); | |
0475169c AP |
1474 | /* The 8705 does not have VID capability. |
1475 | The 8718 does not use IT87_REG_VID for the same purpose. */ | |
17d648bf | 1476 | if (data->type == it8712 || data->type == it8716) { |
b74f3fdd | 1477 | data->vid = it87_read_value(data, IT87_REG_VID); |
17d648bf JD |
1478 | /* The older IT8712F revisions had only 5 VID pins, |
1479 | but we assume it is always safe to read 6 bits. */ | |
1480 | data->vid &= 0x3f; | |
1da177e4 LT |
1481 | } |
1482 | data->last_updated = jiffies; | |
1483 | data->valid = 1; | |
1484 | } | |
1485 | ||
9a61bf63 | 1486 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1487 | |
1488 | return data; | |
1489 | } | |
1490 | ||
b74f3fdd | 1491 | static int __init it87_device_add(unsigned short address, |
1492 | const struct it87_sio_data *sio_data) | |
1493 | { | |
1494 | struct resource res = { | |
87b4b663 BH |
1495 | .start = address + IT87_EC_OFFSET, |
1496 | .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1, | |
b74f3fdd | 1497 | .name = DRVNAME, |
1498 | .flags = IORESOURCE_IO, | |
1499 | }; | |
1500 | int err; | |
1501 | ||
1502 | pdev = platform_device_alloc(DRVNAME, address); | |
1503 | if (!pdev) { | |
1504 | err = -ENOMEM; | |
1505 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); | |
1506 | goto exit; | |
1507 | } | |
1508 | ||
1509 | err = platform_device_add_resources(pdev, &res, 1); | |
1510 | if (err) { | |
1511 | printk(KERN_ERR DRVNAME ": Device resource addition failed " | |
1512 | "(%d)\n", err); | |
1513 | goto exit_device_put; | |
1514 | } | |
1515 | ||
1516 | err = platform_device_add_data(pdev, sio_data, | |
1517 | sizeof(struct it87_sio_data)); | |
1518 | if (err) { | |
1519 | printk(KERN_ERR DRVNAME ": Platform data allocation failed\n"); | |
1520 | goto exit_device_put; | |
1521 | } | |
1522 | ||
1523 | err = platform_device_add(pdev); | |
1524 | if (err) { | |
1525 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", | |
1526 | err); | |
1527 | goto exit_device_put; | |
1528 | } | |
1529 | ||
1530 | return 0; | |
1531 | ||
1532 | exit_device_put: | |
1533 | platform_device_put(pdev); | |
1534 | exit: | |
1535 | return err; | |
1536 | } | |
1537 | ||
1da177e4 LT |
1538 | static int __init sm_it87_init(void) |
1539 | { | |
b74f3fdd | 1540 | int err; |
1541 | unsigned short isa_address=0; | |
1542 | struct it87_sio_data sio_data; | |
1543 | ||
1544 | err = it87_find(&isa_address, &sio_data); | |
1545 | if (err) | |
1546 | return err; | |
1547 | err = platform_driver_register(&it87_driver); | |
1548 | if (err) | |
1549 | return err; | |
fde09509 | 1550 | |
b74f3fdd | 1551 | err = it87_device_add(isa_address, &sio_data); |
1552 | if (err){ | |
1553 | platform_driver_unregister(&it87_driver); | |
1554 | return err; | |
1555 | } | |
1556 | ||
1557 | return 0; | |
1da177e4 LT |
1558 | } |
1559 | ||
1560 | static void __exit sm_it87_exit(void) | |
1561 | { | |
b74f3fdd | 1562 | platform_device_unregister(pdev); |
1563 | platform_driver_unregister(&it87_driver); | |
1da177e4 LT |
1564 | } |
1565 | ||
1566 | ||
f1d8e332 | 1567 | MODULE_AUTHOR("Chris Gauthron, " |
b19367c6 | 1568 | "Jean Delvare <khali@linux-fr.org>"); |
08a8f6e9 | 1569 | MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver"); |
1da177e4 LT |
1570 | module_param(update_vbat, bool, 0); |
1571 | MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value"); | |
1572 | module_param(fix_pwm_polarity, bool, 0); | |
1573 | MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)"); | |
1574 | MODULE_LICENSE("GPL"); | |
1575 | ||
1576 | module_init(sm_it87_init); | |
1577 | module_exit(sm_it87_exit); |