Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
5f2dc798 JD |
2 | * it87.c - Part of lm_sensors, Linux kernel modules for hardware |
3 | * monitoring. | |
4 | * | |
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 | * | |
13 | * Supports: IT8705F Super I/O chip w/LPC interface | |
14 | * IT8712F Super I/O chip w/LPC interface | |
15 | * IT8716F Super I/O chip w/LPC interface | |
16 | * IT8718F Super I/O chip w/LPC interface | |
17 | * IT8720F Super I/O chip w/LPC interface | |
18 | * IT8726F Super I/O chip w/LPC interface | |
19 | * Sis950 A clone of the IT8705F | |
20 | * | |
21 | * Copyright (C) 2001 Chris Gauthron | |
22 | * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org> | |
23 | * | |
24 | * This program is free software; you can redistribute it and/or modify | |
25 | * it under the terms of the GNU General Public License as published by | |
26 | * the Free Software Foundation; either version 2 of the License, or | |
27 | * (at your option) any later version. | |
28 | * | |
29 | * This program is distributed in the hope that it will be useful, | |
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
32 | * GNU General Public License for more details. | |
33 | * | |
34 | * You should have received a copy of the GNU General Public License | |
35 | * along with this program; if not, write to the Free Software | |
36 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
37 | */ | |
1da177e4 | 38 | |
1da177e4 LT |
39 | #include <linux/module.h> |
40 | #include <linux/init.h> | |
41 | #include <linux/slab.h> | |
42 | #include <linux/jiffies.h> | |
b74f3fdd | 43 | #include <linux/platform_device.h> |
943b0830 | 44 | #include <linux/hwmon.h> |
303760b4 JD |
45 | #include <linux/hwmon-sysfs.h> |
46 | #include <linux/hwmon-vid.h> | |
943b0830 | 47 | #include <linux/err.h> |
9a61bf63 | 48 | #include <linux/mutex.h> |
87808be4 | 49 | #include <linux/sysfs.h> |
98dd22c3 JD |
50 | #include <linux/string.h> |
51 | #include <linux/dmi.h> | |
b9acb64a | 52 | #include <linux/acpi.h> |
6055fae8 | 53 | #include <linux/io.h> |
1da177e4 | 54 | |
b74f3fdd | 55 | #define DRVNAME "it87" |
1da177e4 | 56 | |
b4da93e4 | 57 | enum chips { it87, it8712, it8716, it8718, it8720 }; |
1da177e4 | 58 | |
67b671bc JD |
59 | static unsigned short force_id; |
60 | module_param(force_id, ushort, 0); | |
61 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); | |
62 | ||
b74f3fdd | 63 | static struct platform_device *pdev; |
64 | ||
1da177e4 LT |
65 | #define REG 0x2e /* The register to read/write */ |
66 | #define DEV 0x07 /* Register: Logical device select */ | |
67 | #define VAL 0x2f /* The value to read/write */ | |
68 | #define PME 0x04 /* The device with the fan registers in it */ | |
b4da93e4 JMS |
69 | |
70 | /* The device with the IT8718F/IT8720F VID value in it */ | |
71 | #define GPIO 0x07 | |
72 | ||
1da177e4 LT |
73 | #define DEVID 0x20 /* Register: Device ID */ |
74 | #define DEVREV 0x22 /* Register: Device Revision */ | |
75 | ||
76 | static inline int | |
77 | superio_inb(int reg) | |
78 | { | |
79 | outb(reg, REG); | |
80 | return inb(VAL); | |
81 | } | |
82 | ||
83 | static int superio_inw(int reg) | |
84 | { | |
85 | int val; | |
86 | outb(reg++, REG); | |
87 | val = inb(VAL) << 8; | |
88 | outb(reg, REG); | |
89 | val |= inb(VAL); | |
90 | return val; | |
91 | } | |
92 | ||
93 | static inline void | |
87673dd7 | 94 | superio_select(int ldn) |
1da177e4 LT |
95 | { |
96 | outb(DEV, REG); | |
87673dd7 | 97 | outb(ldn, VAL); |
1da177e4 LT |
98 | } |
99 | ||
100 | static inline void | |
101 | superio_enter(void) | |
102 | { | |
103 | outb(0x87, REG); | |
104 | outb(0x01, REG); | |
105 | outb(0x55, REG); | |
106 | outb(0x55, REG); | |
107 | } | |
108 | ||
109 | static inline void | |
110 | superio_exit(void) | |
111 | { | |
112 | outb(0x02, REG); | |
113 | outb(0x02, VAL); | |
114 | } | |
115 | ||
87673dd7 | 116 | /* Logical device 4 registers */ |
1da177e4 LT |
117 | #define IT8712F_DEVID 0x8712 |
118 | #define IT8705F_DEVID 0x8705 | |
17d648bf | 119 | #define IT8716F_DEVID 0x8716 |
87673dd7 | 120 | #define IT8718F_DEVID 0x8718 |
b4da93e4 | 121 | #define IT8720F_DEVID 0x8720 |
08a8f6e9 | 122 | #define IT8726F_DEVID 0x8726 |
1da177e4 LT |
123 | #define IT87_ACT_REG 0x30 |
124 | #define IT87_BASE_REG 0x60 | |
125 | ||
87673dd7 | 126 | /* Logical device 7 registers (IT8712F and later) */ |
895ff267 | 127 | #define IT87_SIO_GPIO3_REG 0x27 |
591ec650 | 128 | #define IT87_SIO_GPIO5_REG 0x29 |
87673dd7 JD |
129 | #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */ |
130 | #define IT87_SIO_VID_REG 0xfc /* VID value */ | |
d9b327c3 | 131 | #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */ |
87673dd7 | 132 | |
1da177e4 LT |
133 | /* Update battery voltage after every reading if true */ |
134 | static int update_vbat; | |
135 | ||
136 | /* Not all BIOSes properly configure the PWM registers */ | |
137 | static int fix_pwm_polarity; | |
138 | ||
1da177e4 LT |
139 | /* Many IT87 constants specified below */ |
140 | ||
141 | /* Length of ISA address segment */ | |
142 | #define IT87_EXTENT 8 | |
143 | ||
87b4b663 BH |
144 | /* Length of ISA address segment for Environmental Controller */ |
145 | #define IT87_EC_EXTENT 2 | |
146 | ||
147 | /* Offset of EC registers from ISA base address */ | |
148 | #define IT87_EC_OFFSET 5 | |
149 | ||
150 | /* Where are the ISA address/data registers relative to the EC base address */ | |
151 | #define IT87_ADDR_REG_OFFSET 0 | |
152 | #define IT87_DATA_REG_OFFSET 1 | |
1da177e4 LT |
153 | |
154 | /*----- The IT87 registers -----*/ | |
155 | ||
156 | #define IT87_REG_CONFIG 0x00 | |
157 | ||
158 | #define IT87_REG_ALARM1 0x01 | |
159 | #define IT87_REG_ALARM2 0x02 | |
160 | #define IT87_REG_ALARM3 0x03 | |
161 | ||
b4da93e4 JMS |
162 | /* The IT8718F and IT8720F have the VID value in a different register, in |
163 | Super-I/O configuration space. */ | |
1da177e4 | 164 | #define IT87_REG_VID 0x0a |
0475169c AP |
165 | /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b |
166 | for fan divisors. Later IT8712F revisions must use 16-bit tachometer | |
167 | mode. */ | |
1da177e4 | 168 | #define IT87_REG_FAN_DIV 0x0b |
17d648bf | 169 | #define IT87_REG_FAN_16BIT 0x0c |
1da177e4 LT |
170 | |
171 | /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */ | |
172 | ||
c7f1f716 JD |
173 | static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 }; |
174 | static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 }; | |
175 | static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 }; | |
176 | static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 }; | |
1da177e4 LT |
177 | #define IT87_REG_FAN_MAIN_CTRL 0x13 |
178 | #define IT87_REG_FAN_CTL 0x14 | |
179 | #define IT87_REG_PWM(nr) (0x15 + (nr)) | |
180 | ||
181 | #define IT87_REG_VIN(nr) (0x20 + (nr)) | |
182 | #define IT87_REG_TEMP(nr) (0x29 + (nr)) | |
183 | ||
184 | #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2) | |
185 | #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2) | |
186 | #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2) | |
187 | #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2) | |
188 | ||
1da177e4 LT |
189 | #define IT87_REG_VIN_ENABLE 0x50 |
190 | #define IT87_REG_TEMP_ENABLE 0x51 | |
d9b327c3 | 191 | #define IT87_REG_BEEP_ENABLE 0x5c |
1da177e4 LT |
192 | |
193 | #define IT87_REG_CHIPID 0x58 | |
194 | ||
4f3f51bc JD |
195 | #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i)) |
196 | #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i)) | |
197 | ||
1da177e4 LT |
198 | #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255)) |
199 | #define IN_FROM_REG(val) ((val) * 16) | |
200 | ||
201 | static inline u8 FAN_TO_REG(long rpm, int div) | |
202 | { | |
203 | if (rpm == 0) | |
204 | return 255; | |
205 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); | |
206 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, | |
207 | 254); | |
208 | } | |
209 | ||
17d648bf JD |
210 | static inline u16 FAN16_TO_REG(long rpm) |
211 | { | |
212 | if (rpm == 0) | |
213 | return 0xffff; | |
214 | return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe); | |
215 | } | |
216 | ||
1da177e4 | 217 | #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div))) |
17d648bf JD |
218 | /* The divider is fixed to 2 in 16-bit mode */ |
219 | #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2)) | |
1da177e4 LT |
220 | |
221 | #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\ | |
222 | ((val)+500)/1000),-128,127)) | |
e267d250 | 223 | #define TEMP_FROM_REG(val) ((val) * 1000) |
1da177e4 | 224 | |
1da177e4 LT |
225 | #define PWM_TO_REG(val) ((val) >> 1) |
226 | #define PWM_FROM_REG(val) (((val)&0x7f) << 1) | |
227 | ||
228 | static int DIV_TO_REG(int val) | |
229 | { | |
230 | int answer = 0; | |
b9e349f7 | 231 | while (answer < 7 && (val >>= 1)) |
1da177e4 LT |
232 | answer++; |
233 | return answer; | |
234 | } | |
235 | #define DIV_FROM_REG(val) (1 << (val)) | |
236 | ||
f8d0c19a JD |
237 | static const unsigned int pwm_freq[8] = { |
238 | 48000000 / 128, | |
239 | 24000000 / 128, | |
240 | 12000000 / 128, | |
241 | 8000000 / 128, | |
242 | 6000000 / 128, | |
243 | 3000000 / 128, | |
244 | 1500000 / 128, | |
245 | 750000 / 128, | |
246 | }; | |
247 | ||
1da177e4 | 248 | |
b74f3fdd | 249 | struct it87_sio_data { |
250 | enum chips type; | |
251 | /* Values read from Super-I/O config space */ | |
0475169c | 252 | u8 revision; |
b74f3fdd | 253 | u8 vid_value; |
d9b327c3 | 254 | u8 beep_pin; |
591ec650 | 255 | /* Features skipped based on config or DMI */ |
895ff267 | 256 | u8 skip_vid; |
591ec650 | 257 | u8 skip_fan; |
98dd22c3 | 258 | u8 skip_pwm; |
b74f3fdd | 259 | }; |
260 | ||
ed6bafbf JD |
261 | /* For each registered chip, we need to keep some data in memory. |
262 | The structure is dynamically allocated. */ | |
1da177e4 | 263 | struct it87_data { |
1beeffe4 | 264 | struct device *hwmon_dev; |
1da177e4 | 265 | enum chips type; |
0475169c | 266 | u8 revision; |
1da177e4 | 267 | |
b74f3fdd | 268 | unsigned short addr; |
269 | const char *name; | |
9a61bf63 | 270 | struct mutex update_lock; |
1da177e4 LT |
271 | char valid; /* !=0 if following fields are valid */ |
272 | unsigned long last_updated; /* In jiffies */ | |
273 | ||
274 | u8 in[9]; /* Register value */ | |
3543a53f JD |
275 | u8 in_max[8]; /* Register value */ |
276 | u8 in_min[8]; /* Register value */ | |
9060f8bd | 277 | u8 has_fan; /* Bitfield, fans enabled */ |
c7f1f716 JD |
278 | u16 fan[5]; /* Register values, possibly combined */ |
279 | u16 fan_min[5]; /* Register values, possibly combined */ | |
e267d250 JD |
280 | s8 temp[3]; /* Register value */ |
281 | s8 temp_high[3]; /* Register value */ | |
282 | s8 temp_low[3]; /* Register value */ | |
1da177e4 LT |
283 | u8 sensor; /* Register value */ |
284 | u8 fan_div[3]; /* Register encoding, shifted right */ | |
285 | u8 vid; /* Register encoding, combined */ | |
a7be58a1 | 286 | u8 vrm; |
1da177e4 | 287 | u32 alarms; /* Register encoding, combined */ |
d9b327c3 | 288 | u8 beeps; /* Register encoding */ |
1da177e4 | 289 | u8 fan_main_ctrl; /* Register value */ |
f8d0c19a | 290 | u8 fan_ctl; /* Register value */ |
b99883dc JD |
291 | |
292 | /* The following 3 arrays correspond to the same registers. The | |
293 | * meaning of bits 6-0 depends on the value of bit 7, and we want | |
294 | * to preserve settings on mode changes, so we have to track all | |
295 | * values separately. */ | |
296 | u8 pwm_ctrl[3]; /* Register value */ | |
297 | u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */ | |
298 | u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */ | |
4f3f51bc JD |
299 | |
300 | /* Automatic fan speed control registers */ | |
301 | u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */ | |
302 | s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */ | |
1da177e4 LT |
303 | }; |
304 | ||
0475169c AP |
305 | static inline int has_16bit_fans(const struct it87_data *data) |
306 | { | |
816d8c6a | 307 | /* IT8705F Datasheet 0.4.1, 3h == Version G. |
859b9ef3 | 308 | IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J. |
816d8c6a AP |
309 | These are the first revisions with 16bit tachometer support. */ |
310 | return (data->type == it87 && data->revision >= 0x03) | |
859b9ef3 | 311 | || (data->type == it8712 && data->revision >= 0x08) |
0475169c | 312 | || data->type == it8716 |
b4da93e4 JMS |
313 | || data->type == it8718 |
314 | || data->type == it8720; | |
0475169c | 315 | } |
1da177e4 | 316 | |
4f3f51bc JD |
317 | static inline int has_old_autopwm(const struct it87_data *data) |
318 | { | |
319 | /* The old automatic fan speed control interface is implemented | |
320 | by IT8705F chips up to revision F and IT8712F chips up to | |
321 | revision G. */ | |
322 | return (data->type == it87 && data->revision < 0x03) | |
323 | || (data->type == it8712 && data->revision < 0x08); | |
324 | } | |
325 | ||
b74f3fdd | 326 | static int it87_probe(struct platform_device *pdev); |
d0546128 | 327 | static int __devexit it87_remove(struct platform_device *pdev); |
1da177e4 | 328 | |
b74f3fdd | 329 | static int it87_read_value(struct it87_data *data, u8 reg); |
330 | static void it87_write_value(struct it87_data *data, u8 reg, u8 value); | |
1da177e4 | 331 | static struct it87_data *it87_update_device(struct device *dev); |
b74f3fdd | 332 | static int it87_check_pwm(struct device *dev); |
333 | static void it87_init_device(struct platform_device *pdev); | |
1da177e4 LT |
334 | |
335 | ||
b74f3fdd | 336 | static struct platform_driver it87_driver = { |
cdaf7934 | 337 | .driver = { |
87218842 | 338 | .owner = THIS_MODULE, |
b74f3fdd | 339 | .name = DRVNAME, |
cdaf7934 | 340 | }, |
b74f3fdd | 341 | .probe = it87_probe, |
342 | .remove = __devexit_p(it87_remove), | |
fde09509 JD |
343 | }; |
344 | ||
20ad93d4 JD |
345 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
346 | char *buf) | |
1da177e4 | 347 | { |
20ad93d4 JD |
348 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
349 | int nr = sensor_attr->index; | |
350 | ||
1da177e4 LT |
351 | struct it87_data *data = it87_update_device(dev); |
352 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])); | |
353 | } | |
354 | ||
20ad93d4 JD |
355 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
356 | char *buf) | |
1da177e4 | 357 | { |
20ad93d4 JD |
358 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
359 | int nr = sensor_attr->index; | |
360 | ||
1da177e4 LT |
361 | struct it87_data *data = it87_update_device(dev); |
362 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])); | |
363 | } | |
364 | ||
20ad93d4 JD |
365 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
366 | char *buf) | |
1da177e4 | 367 | { |
20ad93d4 JD |
368 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
369 | int nr = sensor_attr->index; | |
370 | ||
1da177e4 LT |
371 | struct it87_data *data = it87_update_device(dev); |
372 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])); | |
373 | } | |
374 | ||
20ad93d4 JD |
375 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
376 | const char *buf, size_t count) | |
1da177e4 | 377 | { |
20ad93d4 JD |
378 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
379 | int nr = sensor_attr->index; | |
380 | ||
b74f3fdd | 381 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
382 | unsigned long val; |
383 | ||
384 | if (strict_strtoul(buf, 10, &val) < 0) | |
385 | return -EINVAL; | |
1da177e4 | 386 | |
9a61bf63 | 387 | mutex_lock(&data->update_lock); |
1da177e4 | 388 | data->in_min[nr] = IN_TO_REG(val); |
b74f3fdd | 389 | it87_write_value(data, IT87_REG_VIN_MIN(nr), |
1da177e4 | 390 | data->in_min[nr]); |
9a61bf63 | 391 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
392 | return count; |
393 | } | |
20ad93d4 JD |
394 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
395 | const char *buf, size_t count) | |
1da177e4 | 396 | { |
20ad93d4 JD |
397 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
398 | int nr = sensor_attr->index; | |
399 | ||
b74f3fdd | 400 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
401 | unsigned long val; |
402 | ||
403 | if (strict_strtoul(buf, 10, &val) < 0) | |
404 | return -EINVAL; | |
1da177e4 | 405 | |
9a61bf63 | 406 | mutex_lock(&data->update_lock); |
1da177e4 | 407 | data->in_max[nr] = IN_TO_REG(val); |
b74f3fdd | 408 | it87_write_value(data, IT87_REG_VIN_MAX(nr), |
1da177e4 | 409 | data->in_max[nr]); |
9a61bf63 | 410 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
411 | return count; |
412 | } | |
413 | ||
414 | #define show_in_offset(offset) \ | |
20ad93d4 JD |
415 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
416 | show_in, NULL, offset); | |
1da177e4 LT |
417 | |
418 | #define limit_in_offset(offset) \ | |
20ad93d4 JD |
419 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
420 | show_in_min, set_in_min, offset); \ | |
421 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
422 | show_in_max, set_in_max, offset); | |
1da177e4 LT |
423 | |
424 | show_in_offset(0); | |
425 | limit_in_offset(0); | |
426 | show_in_offset(1); | |
427 | limit_in_offset(1); | |
428 | show_in_offset(2); | |
429 | limit_in_offset(2); | |
430 | show_in_offset(3); | |
431 | limit_in_offset(3); | |
432 | show_in_offset(4); | |
433 | limit_in_offset(4); | |
434 | show_in_offset(5); | |
435 | limit_in_offset(5); | |
436 | show_in_offset(6); | |
437 | limit_in_offset(6); | |
438 | show_in_offset(7); | |
439 | limit_in_offset(7); | |
440 | show_in_offset(8); | |
441 | ||
442 | /* 3 temperatures */ | |
20ad93d4 JD |
443 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
444 | char *buf) | |
1da177e4 | 445 | { |
20ad93d4 JD |
446 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
447 | int nr = sensor_attr->index; | |
448 | ||
1da177e4 LT |
449 | struct it87_data *data = it87_update_device(dev); |
450 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); | |
451 | } | |
20ad93d4 JD |
452 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
453 | char *buf) | |
1da177e4 | 454 | { |
20ad93d4 JD |
455 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
456 | int nr = sensor_attr->index; | |
457 | ||
1da177e4 LT |
458 | struct it87_data *data = it87_update_device(dev); |
459 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])); | |
460 | } | |
20ad93d4 JD |
461 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
462 | char *buf) | |
1da177e4 | 463 | { |
20ad93d4 JD |
464 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
465 | int nr = sensor_attr->index; | |
466 | ||
1da177e4 LT |
467 | struct it87_data *data = it87_update_device(dev); |
468 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])); | |
469 | } | |
20ad93d4 JD |
470 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
471 | const char *buf, size_t count) | |
1da177e4 | 472 | { |
20ad93d4 JD |
473 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
474 | int nr = sensor_attr->index; | |
475 | ||
b74f3fdd | 476 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
477 | long val; |
478 | ||
479 | if (strict_strtol(buf, 10, &val) < 0) | |
480 | return -EINVAL; | |
1da177e4 | 481 | |
9a61bf63 | 482 | mutex_lock(&data->update_lock); |
1da177e4 | 483 | data->temp_high[nr] = TEMP_TO_REG(val); |
b74f3fdd | 484 | it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]); |
9a61bf63 | 485 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
486 | return count; |
487 | } | |
20ad93d4 JD |
488 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
489 | const char *buf, size_t count) | |
1da177e4 | 490 | { |
20ad93d4 JD |
491 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
492 | int nr = sensor_attr->index; | |
493 | ||
b74f3fdd | 494 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
495 | long val; |
496 | ||
497 | if (strict_strtol(buf, 10, &val) < 0) | |
498 | return -EINVAL; | |
1da177e4 | 499 | |
9a61bf63 | 500 | mutex_lock(&data->update_lock); |
1da177e4 | 501 | data->temp_low[nr] = TEMP_TO_REG(val); |
b74f3fdd | 502 | it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]); |
9a61bf63 | 503 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
504 | return count; |
505 | } | |
506 | #define show_temp_offset(offset) \ | |
20ad93d4 JD |
507 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
508 | show_temp, NULL, offset - 1); \ | |
509 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
510 | show_temp_max, set_temp_max, offset - 1); \ | |
511 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ | |
512 | show_temp_min, set_temp_min, offset - 1); | |
1da177e4 LT |
513 | |
514 | show_temp_offset(1); | |
515 | show_temp_offset(2); | |
516 | show_temp_offset(3); | |
517 | ||
20ad93d4 JD |
518 | static ssize_t show_sensor(struct device *dev, struct device_attribute *attr, |
519 | char *buf) | |
1da177e4 | 520 | { |
20ad93d4 JD |
521 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
522 | int nr = sensor_attr->index; | |
523 | ||
1da177e4 | 524 | struct it87_data *data = it87_update_device(dev); |
5f2dc798 JD |
525 | u8 reg = data->sensor; /* In case the value is updated while |
526 | we use it */ | |
527 | ||
1da177e4 LT |
528 | if (reg & (1 << nr)) |
529 | return sprintf(buf, "3\n"); /* thermal diode */ | |
530 | if (reg & (8 << nr)) | |
4ed10779 | 531 | return sprintf(buf, "4\n"); /* thermistor */ |
1da177e4 LT |
532 | return sprintf(buf, "0\n"); /* disabled */ |
533 | } | |
20ad93d4 JD |
534 | static ssize_t set_sensor(struct device *dev, struct device_attribute *attr, |
535 | const char *buf, size_t count) | |
1da177e4 | 536 | { |
20ad93d4 JD |
537 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
538 | int nr = sensor_attr->index; | |
539 | ||
b74f3fdd | 540 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
541 | long val; |
542 | ||
543 | if (strict_strtol(buf, 10, &val) < 0) | |
544 | return -EINVAL; | |
1da177e4 | 545 | |
9a61bf63 | 546 | mutex_lock(&data->update_lock); |
1da177e4 LT |
547 | |
548 | data->sensor &= ~(1 << nr); | |
549 | data->sensor &= ~(8 << nr); | |
4ed10779 JD |
550 | if (val == 2) { /* backwards compatibility */ |
551 | dev_warn(dev, "Sensor type 2 is deprecated, please use 4 " | |
552 | "instead\n"); | |
553 | val = 4; | |
554 | } | |
555 | /* 3 = thermal diode; 4 = thermistor; 0 = disabled */ | |
1da177e4 | 556 | if (val == 3) |
5f2dc798 | 557 | data->sensor |= 1 << nr; |
4ed10779 | 558 | else if (val == 4) |
5f2dc798 | 559 | data->sensor |= 8 << nr; |
1da177e4 | 560 | else if (val != 0) { |
9a61bf63 | 561 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
562 | return -EINVAL; |
563 | } | |
b74f3fdd | 564 | it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor); |
9a61bf63 | 565 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
566 | return count; |
567 | } | |
568 | #define show_sensor_offset(offset) \ | |
20ad93d4 JD |
569 | static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \ |
570 | show_sensor, set_sensor, offset - 1); | |
1da177e4 LT |
571 | |
572 | show_sensor_offset(1); | |
573 | show_sensor_offset(2); | |
574 | show_sensor_offset(3); | |
575 | ||
576 | /* 3 Fans */ | |
b99883dc JD |
577 | |
578 | static int pwm_mode(const struct it87_data *data, int nr) | |
579 | { | |
580 | int ctrl = data->fan_main_ctrl & (1 << nr); | |
581 | ||
582 | if (ctrl == 0) /* Full speed */ | |
583 | return 0; | |
584 | if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */ | |
585 | return 2; | |
586 | else /* Manual mode */ | |
587 | return 1; | |
588 | } | |
589 | ||
20ad93d4 JD |
590 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
591 | char *buf) | |
1da177e4 | 592 | { |
20ad93d4 JD |
593 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
594 | int nr = sensor_attr->index; | |
595 | ||
1da177e4 | 596 | struct it87_data *data = it87_update_device(dev); |
5f2dc798 | 597 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
1da177e4 LT |
598 | DIV_FROM_REG(data->fan_div[nr]))); |
599 | } | |
20ad93d4 JD |
600 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
601 | char *buf) | |
1da177e4 | 602 | { |
20ad93d4 JD |
603 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
604 | int nr = sensor_attr->index; | |
605 | ||
1da177e4 | 606 | struct it87_data *data = it87_update_device(dev); |
5f2dc798 JD |
607 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
608 | DIV_FROM_REG(data->fan_div[nr]))); | |
1da177e4 | 609 | } |
20ad93d4 JD |
610 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
611 | char *buf) | |
1da177e4 | 612 | { |
20ad93d4 JD |
613 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
614 | int nr = sensor_attr->index; | |
615 | ||
1da177e4 LT |
616 | struct it87_data *data = it87_update_device(dev); |
617 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); | |
618 | } | |
5f2dc798 JD |
619 | static ssize_t show_pwm_enable(struct device *dev, |
620 | struct device_attribute *attr, char *buf) | |
1da177e4 | 621 | { |
20ad93d4 JD |
622 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
623 | int nr = sensor_attr->index; | |
624 | ||
1da177e4 | 625 | struct it87_data *data = it87_update_device(dev); |
b99883dc | 626 | return sprintf(buf, "%d\n", pwm_mode(data, nr)); |
1da177e4 | 627 | } |
20ad93d4 JD |
628 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
629 | char *buf) | |
1da177e4 | 630 | { |
20ad93d4 JD |
631 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
632 | int nr = sensor_attr->index; | |
633 | ||
1da177e4 | 634 | struct it87_data *data = it87_update_device(dev); |
b99883dc | 635 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr])); |
1da177e4 | 636 | } |
f8d0c19a JD |
637 | static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr, |
638 | char *buf) | |
639 | { | |
640 | struct it87_data *data = it87_update_device(dev); | |
641 | int index = (data->fan_ctl >> 4) & 0x07; | |
642 | ||
643 | return sprintf(buf, "%u\n", pwm_freq[index]); | |
644 | } | |
20ad93d4 JD |
645 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
646 | const char *buf, size_t count) | |
1da177e4 | 647 | { |
20ad93d4 JD |
648 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
649 | int nr = sensor_attr->index; | |
650 | ||
b74f3fdd | 651 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 | 652 | long val; |
7f999aa7 | 653 | u8 reg; |
1da177e4 | 654 | |
f5f64501 JD |
655 | if (strict_strtol(buf, 10, &val) < 0) |
656 | return -EINVAL; | |
657 | ||
9a61bf63 | 658 | mutex_lock(&data->update_lock); |
b74f3fdd | 659 | reg = it87_read_value(data, IT87_REG_FAN_DIV); |
07eab46d | 660 | switch (nr) { |
5f2dc798 JD |
661 | case 0: |
662 | data->fan_div[nr] = reg & 0x07; | |
663 | break; | |
664 | case 1: | |
665 | data->fan_div[nr] = (reg >> 3) & 0x07; | |
666 | break; | |
667 | case 2: | |
668 | data->fan_div[nr] = (reg & 0x40) ? 3 : 1; | |
669 | break; | |
07eab46d JD |
670 | } |
671 | ||
1da177e4 | 672 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
c7f1f716 | 673 | it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]); |
9a61bf63 | 674 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
675 | return count; |
676 | } | |
20ad93d4 JD |
677 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
678 | const char *buf, size_t count) | |
1da177e4 | 679 | { |
20ad93d4 JD |
680 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
681 | int nr = sensor_attr->index; | |
682 | ||
b74f3fdd | 683 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 | 684 | unsigned long val; |
8ab4ec3e | 685 | int min; |
1da177e4 LT |
686 | u8 old; |
687 | ||
f5f64501 JD |
688 | if (strict_strtoul(buf, 10, &val) < 0) |
689 | return -EINVAL; | |
690 | ||
9a61bf63 | 691 | mutex_lock(&data->update_lock); |
b74f3fdd | 692 | old = it87_read_value(data, IT87_REG_FAN_DIV); |
1da177e4 | 693 | |
8ab4ec3e JD |
694 | /* Save fan min limit */ |
695 | min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); | |
1da177e4 LT |
696 | |
697 | switch (nr) { | |
698 | case 0: | |
699 | case 1: | |
700 | data->fan_div[nr] = DIV_TO_REG(val); | |
701 | break; | |
702 | case 2: | |
703 | if (val < 8) | |
704 | data->fan_div[nr] = 1; | |
705 | else | |
706 | data->fan_div[nr] = 3; | |
707 | } | |
708 | val = old & 0x80; | |
709 | val |= (data->fan_div[0] & 0x07); | |
710 | val |= (data->fan_div[1] & 0x07) << 3; | |
711 | if (data->fan_div[2] == 3) | |
712 | val |= 0x1 << 6; | |
b74f3fdd | 713 | it87_write_value(data, IT87_REG_FAN_DIV, val); |
1da177e4 | 714 | |
8ab4ec3e JD |
715 | /* Restore fan min limit */ |
716 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | |
c7f1f716 | 717 | it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]); |
8ab4ec3e | 718 | |
9a61bf63 | 719 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
720 | return count; |
721 | } | |
20ad93d4 JD |
722 | static ssize_t set_pwm_enable(struct device *dev, |
723 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 724 | { |
20ad93d4 JD |
725 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
726 | int nr = sensor_attr->index; | |
727 | ||
b74f3fdd | 728 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 | 729 | long val; |
1da177e4 | 730 | |
f5f64501 | 731 | if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2) |
b99883dc JD |
732 | return -EINVAL; |
733 | ||
9a61bf63 | 734 | mutex_lock(&data->update_lock); |
1da177e4 LT |
735 | |
736 | if (val == 0) { | |
737 | int tmp; | |
738 | /* make sure the fan is on when in on/off mode */ | |
b74f3fdd | 739 | tmp = it87_read_value(data, IT87_REG_FAN_CTL); |
740 | it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr)); | |
1da177e4 LT |
741 | /* set on/off mode */ |
742 | data->fan_main_ctrl &= ~(1 << nr); | |
5f2dc798 JD |
743 | it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, |
744 | data->fan_main_ctrl); | |
b99883dc JD |
745 | } else { |
746 | if (val == 1) /* Manual mode */ | |
747 | data->pwm_ctrl[nr] = data->pwm_duty[nr]; | |
748 | else /* Automatic mode */ | |
749 | data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr]; | |
750 | it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]); | |
1da177e4 LT |
751 | /* set SmartGuardian mode */ |
752 | data->fan_main_ctrl |= (1 << nr); | |
5f2dc798 JD |
753 | it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, |
754 | data->fan_main_ctrl); | |
1da177e4 LT |
755 | } |
756 | ||
9a61bf63 | 757 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
758 | return count; |
759 | } | |
20ad93d4 JD |
760 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
761 | const char *buf, size_t count) | |
1da177e4 | 762 | { |
20ad93d4 JD |
763 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
764 | int nr = sensor_attr->index; | |
765 | ||
b74f3fdd | 766 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 | 767 | long val; |
1da177e4 | 768 | |
f5f64501 | 769 | if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255) |
1da177e4 LT |
770 | return -EINVAL; |
771 | ||
9a61bf63 | 772 | mutex_lock(&data->update_lock); |
b99883dc JD |
773 | data->pwm_duty[nr] = PWM_TO_REG(val); |
774 | /* If we are in manual mode, write the duty cycle immediately; | |
775 | * otherwise, just store it for later use. */ | |
776 | if (!(data->pwm_ctrl[nr] & 0x80)) { | |
777 | data->pwm_ctrl[nr] = data->pwm_duty[nr]; | |
778 | it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]); | |
779 | } | |
9a61bf63 | 780 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
781 | return count; |
782 | } | |
f8d0c19a JD |
783 | static ssize_t set_pwm_freq(struct device *dev, |
784 | struct device_attribute *attr, const char *buf, size_t count) | |
785 | { | |
b74f3fdd | 786 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 | 787 | unsigned long val; |
f8d0c19a JD |
788 | int i; |
789 | ||
f5f64501 JD |
790 | if (strict_strtoul(buf, 10, &val) < 0) |
791 | return -EINVAL; | |
792 | ||
f8d0c19a JD |
793 | /* Search for the nearest available frequency */ |
794 | for (i = 0; i < 7; i++) { | |
795 | if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2) | |
796 | break; | |
797 | } | |
798 | ||
799 | mutex_lock(&data->update_lock); | |
b74f3fdd | 800 | data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f; |
f8d0c19a | 801 | data->fan_ctl |= i << 4; |
b74f3fdd | 802 | it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl); |
f8d0c19a JD |
803 | mutex_unlock(&data->update_lock); |
804 | ||
805 | return count; | |
806 | } | |
94ac7ee6 JD |
807 | static ssize_t show_pwm_temp_map(struct device *dev, |
808 | struct device_attribute *attr, char *buf) | |
809 | { | |
810 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
811 | int nr = sensor_attr->index; | |
812 | ||
813 | struct it87_data *data = it87_update_device(dev); | |
814 | int map; | |
815 | ||
816 | if (data->pwm_temp_map[nr] < 3) | |
817 | map = 1 << data->pwm_temp_map[nr]; | |
818 | else | |
819 | map = 0; /* Should never happen */ | |
820 | return sprintf(buf, "%d\n", map); | |
821 | } | |
822 | static ssize_t set_pwm_temp_map(struct device *dev, | |
823 | struct device_attribute *attr, const char *buf, size_t count) | |
824 | { | |
825 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
826 | int nr = sensor_attr->index; | |
827 | ||
828 | struct it87_data *data = dev_get_drvdata(dev); | |
829 | long val; | |
830 | u8 reg; | |
831 | ||
4f3f51bc JD |
832 | /* This check can go away if we ever support automatic fan speed |
833 | control on newer chips. */ | |
834 | if (!has_old_autopwm(data)) { | |
835 | dev_notice(dev, "Mapping change disabled for safety reasons\n"); | |
836 | return -EINVAL; | |
837 | } | |
838 | ||
94ac7ee6 JD |
839 | if (strict_strtol(buf, 10, &val) < 0) |
840 | return -EINVAL; | |
841 | ||
842 | switch (val) { | |
843 | case (1 << 0): | |
844 | reg = 0x00; | |
845 | break; | |
846 | case (1 << 1): | |
847 | reg = 0x01; | |
848 | break; | |
849 | case (1 << 2): | |
850 | reg = 0x02; | |
851 | break; | |
852 | default: | |
853 | return -EINVAL; | |
854 | } | |
855 | ||
856 | mutex_lock(&data->update_lock); | |
857 | data->pwm_temp_map[nr] = reg; | |
858 | /* If we are in automatic mode, write the temp mapping immediately; | |
859 | * otherwise, just store it for later use. */ | |
860 | if (data->pwm_ctrl[nr] & 0x80) { | |
861 | data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr]; | |
862 | it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]); | |
863 | } | |
864 | mutex_unlock(&data->update_lock); | |
865 | return count; | |
866 | } | |
1da177e4 | 867 | |
4f3f51bc JD |
868 | static ssize_t show_auto_pwm(struct device *dev, |
869 | struct device_attribute *attr, char *buf) | |
870 | { | |
871 | struct it87_data *data = it87_update_device(dev); | |
872 | struct sensor_device_attribute_2 *sensor_attr = | |
873 | to_sensor_dev_attr_2(attr); | |
874 | int nr = sensor_attr->nr; | |
875 | int point = sensor_attr->index; | |
876 | ||
877 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point])); | |
878 | } | |
879 | ||
880 | static ssize_t set_auto_pwm(struct device *dev, | |
881 | struct device_attribute *attr, const char *buf, size_t count) | |
882 | { | |
883 | struct it87_data *data = dev_get_drvdata(dev); | |
884 | struct sensor_device_attribute_2 *sensor_attr = | |
885 | to_sensor_dev_attr_2(attr); | |
886 | int nr = sensor_attr->nr; | |
887 | int point = sensor_attr->index; | |
888 | long val; | |
889 | ||
890 | if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255) | |
891 | return -EINVAL; | |
892 | ||
893 | mutex_lock(&data->update_lock); | |
894 | data->auto_pwm[nr][point] = PWM_TO_REG(val); | |
895 | it87_write_value(data, IT87_REG_AUTO_PWM(nr, point), | |
896 | data->auto_pwm[nr][point]); | |
897 | mutex_unlock(&data->update_lock); | |
898 | return count; | |
899 | } | |
900 | ||
901 | static ssize_t show_auto_temp(struct device *dev, | |
902 | struct device_attribute *attr, char *buf) | |
903 | { | |
904 | struct it87_data *data = it87_update_device(dev); | |
905 | struct sensor_device_attribute_2 *sensor_attr = | |
906 | to_sensor_dev_attr_2(attr); | |
907 | int nr = sensor_attr->nr; | |
908 | int point = sensor_attr->index; | |
909 | ||
910 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point])); | |
911 | } | |
912 | ||
913 | static ssize_t set_auto_temp(struct device *dev, | |
914 | struct device_attribute *attr, const char *buf, size_t count) | |
915 | { | |
916 | struct it87_data *data = dev_get_drvdata(dev); | |
917 | struct sensor_device_attribute_2 *sensor_attr = | |
918 | to_sensor_dev_attr_2(attr); | |
919 | int nr = sensor_attr->nr; | |
920 | int point = sensor_attr->index; | |
921 | long val; | |
922 | ||
923 | if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000) | |
924 | return -EINVAL; | |
925 | ||
926 | mutex_lock(&data->update_lock); | |
927 | data->auto_temp[nr][point] = TEMP_TO_REG(val); | |
928 | it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), | |
929 | data->auto_temp[nr][point]); | |
930 | mutex_unlock(&data->update_lock); | |
931 | return count; | |
932 | } | |
933 | ||
20ad93d4 JD |
934 | #define show_fan_offset(offset) \ |
935 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ | |
936 | show_fan, NULL, offset - 1); \ | |
937 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
938 | show_fan_min, set_fan_min, offset - 1); \ | |
939 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
940 | show_fan_div, set_fan_div, offset - 1); | |
1da177e4 LT |
941 | |
942 | show_fan_offset(1); | |
943 | show_fan_offset(2); | |
944 | show_fan_offset(3); | |
945 | ||
946 | #define show_pwm_offset(offset) \ | |
20ad93d4 JD |
947 | static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
948 | show_pwm_enable, set_pwm_enable, offset - 1); \ | |
949 | static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ | |
f8d0c19a JD |
950 | show_pwm, set_pwm, offset - 1); \ |
951 | static DEVICE_ATTR(pwm##offset##_freq, \ | |
952 | (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \ | |
94ac7ee6 JD |
953 | show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \ |
954 | static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \ | |
4f3f51bc JD |
955 | S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \ |
956 | offset - 1); \ | |
957 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \ | |
958 | S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \ | |
959 | offset - 1, 0); \ | |
960 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \ | |
961 | S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \ | |
962 | offset - 1, 1); \ | |
963 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \ | |
964 | S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \ | |
965 | offset - 1, 2); \ | |
966 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \ | |
967 | S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \ | |
968 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \ | |
969 | S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \ | |
970 | offset - 1, 1); \ | |
971 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \ | |
972 | S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \ | |
973 | offset - 1, 0); \ | |
974 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \ | |
975 | S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \ | |
976 | offset - 1, 2); \ | |
977 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \ | |
978 | S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \ | |
979 | offset - 1, 3); \ | |
980 | static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \ | |
981 | S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \ | |
982 | offset - 1, 4); | |
1da177e4 LT |
983 | |
984 | show_pwm_offset(1); | |
985 | show_pwm_offset(2); | |
986 | show_pwm_offset(3); | |
987 | ||
17d648bf JD |
988 | /* A different set of callbacks for 16-bit fans */ |
989 | static ssize_t show_fan16(struct device *dev, struct device_attribute *attr, | |
990 | char *buf) | |
991 | { | |
992 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
993 | int nr = sensor_attr->index; | |
994 | struct it87_data *data = it87_update_device(dev); | |
995 | return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr])); | |
996 | } | |
997 | ||
998 | static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr, | |
999 | char *buf) | |
1000 | { | |
1001 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
1002 | int nr = sensor_attr->index; | |
1003 | struct it87_data *data = it87_update_device(dev); | |
1004 | return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr])); | |
1005 | } | |
1006 | ||
1007 | static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr, | |
1008 | const char *buf, size_t count) | |
1009 | { | |
1010 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
1011 | int nr = sensor_attr->index; | |
b74f3fdd | 1012 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
1013 | long val; |
1014 | ||
1015 | if (strict_strtol(buf, 10, &val) < 0) | |
1016 | return -EINVAL; | |
17d648bf JD |
1017 | |
1018 | mutex_lock(&data->update_lock); | |
1019 | data->fan_min[nr] = FAN16_TO_REG(val); | |
c7f1f716 | 1020 | it87_write_value(data, IT87_REG_FAN_MIN[nr], |
17d648bf | 1021 | data->fan_min[nr] & 0xff); |
c7f1f716 | 1022 | it87_write_value(data, IT87_REG_FANX_MIN[nr], |
17d648bf JD |
1023 | data->fan_min[nr] >> 8); |
1024 | mutex_unlock(&data->update_lock); | |
1025 | return count; | |
1026 | } | |
1027 | ||
1028 | /* We want to use the same sysfs file names as 8-bit fans, but we need | |
1029 | different variable names, so we have to use SENSOR_ATTR instead of | |
1030 | SENSOR_DEVICE_ATTR. */ | |
1031 | #define show_fan16_offset(offset) \ | |
1032 | static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \ | |
1033 | = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \ | |
1034 | show_fan16, NULL, offset - 1); \ | |
1035 | static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \ | |
1036 | = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
1037 | show_fan16_min, set_fan16_min, offset - 1) | |
1038 | ||
1039 | show_fan16_offset(1); | |
1040 | show_fan16_offset(2); | |
1041 | show_fan16_offset(3); | |
c7f1f716 JD |
1042 | show_fan16_offset(4); |
1043 | show_fan16_offset(5); | |
17d648bf | 1044 | |
1da177e4 | 1045 | /* Alarms */ |
5f2dc798 JD |
1046 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
1047 | char *buf) | |
1da177e4 LT |
1048 | { |
1049 | struct it87_data *data = it87_update_device(dev); | |
68188ba7 | 1050 | return sprintf(buf, "%u\n", data->alarms); |
1da177e4 | 1051 | } |
1d66c64c | 1052 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
1da177e4 | 1053 | |
0124dd78 JD |
1054 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
1055 | char *buf) | |
1056 | { | |
1057 | int bitnr = to_sensor_dev_attr(attr)->index; | |
1058 | struct it87_data *data = it87_update_device(dev); | |
1059 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
1060 | } | |
1061 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8); | |
1062 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9); | |
1063 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10); | |
1064 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11); | |
1065 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12); | |
1066 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13); | |
1067 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14); | |
1068 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15); | |
1069 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0); | |
1070 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1); | |
1071 | static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2); | |
1072 | static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3); | |
1073 | static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6); | |
1074 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16); | |
1075 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17); | |
1076 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18); | |
1077 | ||
d9b327c3 JD |
1078 | static ssize_t show_beep(struct device *dev, struct device_attribute *attr, |
1079 | char *buf) | |
1080 | { | |
1081 | int bitnr = to_sensor_dev_attr(attr)->index; | |
1082 | struct it87_data *data = it87_update_device(dev); | |
1083 | return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1); | |
1084 | } | |
1085 | static ssize_t set_beep(struct device *dev, struct device_attribute *attr, | |
1086 | const char *buf, size_t count) | |
1087 | { | |
1088 | int bitnr = to_sensor_dev_attr(attr)->index; | |
1089 | struct it87_data *data = dev_get_drvdata(dev); | |
1090 | long val; | |
1091 | ||
1092 | if (strict_strtol(buf, 10, &val) < 0 | |
1093 | || (val != 0 && val != 1)) | |
1094 | return -EINVAL; | |
1095 | ||
1096 | mutex_lock(&data->update_lock); | |
1097 | data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE); | |
1098 | if (val) | |
1099 | data->beeps |= (1 << bitnr); | |
1100 | else | |
1101 | data->beeps &= ~(1 << bitnr); | |
1102 | it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps); | |
1103 | mutex_unlock(&data->update_lock); | |
1104 | return count; | |
1105 | } | |
1106 | ||
1107 | static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, | |
1108 | show_beep, set_beep, 1); | |
1109 | static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1); | |
1110 | static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1); | |
1111 | static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1); | |
1112 | static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1); | |
1113 | static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1); | |
1114 | static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1); | |
1115 | static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1); | |
1116 | /* fanX_beep writability is set later */ | |
1117 | static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0); | |
1118 | static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0); | |
1119 | static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0); | |
1120 | static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0); | |
1121 | static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0); | |
1122 | static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, | |
1123 | show_beep, set_beep, 2); | |
1124 | static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2); | |
1125 | static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2); | |
1126 | ||
5f2dc798 JD |
1127 | static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, |
1128 | char *buf) | |
1da177e4 | 1129 | { |
90d6619a | 1130 | struct it87_data *data = dev_get_drvdata(dev); |
a7be58a1 | 1131 | return sprintf(buf, "%u\n", data->vrm); |
1da177e4 | 1132 | } |
5f2dc798 JD |
1133 | static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, |
1134 | const char *buf, size_t count) | |
1da177e4 | 1135 | { |
b74f3fdd | 1136 | struct it87_data *data = dev_get_drvdata(dev); |
f5f64501 JD |
1137 | unsigned long val; |
1138 | ||
1139 | if (strict_strtoul(buf, 10, &val) < 0) | |
1140 | return -EINVAL; | |
1da177e4 | 1141 | |
1da177e4 LT |
1142 | data->vrm = val; |
1143 | ||
1144 | return count; | |
1145 | } | |
1146 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); | |
1da177e4 | 1147 | |
5f2dc798 JD |
1148 | static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, |
1149 | char *buf) | |
1da177e4 LT |
1150 | { |
1151 | struct it87_data *data = it87_update_device(dev); | |
1152 | return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm)); | |
1153 | } | |
1154 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); | |
87808be4 | 1155 | |
b74f3fdd | 1156 | static ssize_t show_name(struct device *dev, struct device_attribute |
1157 | *devattr, char *buf) | |
1158 | { | |
1159 | struct it87_data *data = dev_get_drvdata(dev); | |
1160 | return sprintf(buf, "%s\n", data->name); | |
1161 | } | |
1162 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | |
1163 | ||
87808be4 JD |
1164 | static struct attribute *it87_attributes[] = { |
1165 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
1166 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
1167 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
1168 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
1169 | &sensor_dev_attr_in4_input.dev_attr.attr, | |
1170 | &sensor_dev_attr_in5_input.dev_attr.attr, | |
1171 | &sensor_dev_attr_in6_input.dev_attr.attr, | |
1172 | &sensor_dev_attr_in7_input.dev_attr.attr, | |
1173 | &sensor_dev_attr_in8_input.dev_attr.attr, | |
1174 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
1175 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
1176 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
1177 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
1178 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
1179 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
1180 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
1181 | &sensor_dev_attr_in7_min.dev_attr.attr, | |
1182 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
1183 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
1184 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
1185 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
1186 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
1187 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
1188 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
1189 | &sensor_dev_attr_in7_max.dev_attr.attr, | |
0124dd78 JD |
1190 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
1191 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | |
1192 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
1193 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
1194 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | |
1195 | &sensor_dev_attr_in5_alarm.dev_attr.attr, | |
1196 | &sensor_dev_attr_in6_alarm.dev_attr.attr, | |
1197 | &sensor_dev_attr_in7_alarm.dev_attr.attr, | |
87808be4 JD |
1198 | |
1199 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
1200 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
1201 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
1202 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
1203 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
1204 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
1205 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
1206 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
1207 | &sensor_dev_attr_temp3_min.dev_attr.attr, | |
1208 | &sensor_dev_attr_temp1_type.dev_attr.attr, | |
1209 | &sensor_dev_attr_temp2_type.dev_attr.attr, | |
1210 | &sensor_dev_attr_temp3_type.dev_attr.attr, | |
0124dd78 JD |
1211 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
1212 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, | |
1213 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, | |
87808be4 JD |
1214 | |
1215 | &dev_attr_alarms.attr, | |
b74f3fdd | 1216 | &dev_attr_name.attr, |
87808be4 JD |
1217 | NULL |
1218 | }; | |
1219 | ||
1220 | static const struct attribute_group it87_group = { | |
1221 | .attrs = it87_attributes, | |
1222 | }; | |
1223 | ||
d9b327c3 JD |
1224 | static struct attribute *it87_attributes_beep[] = { |
1225 | &sensor_dev_attr_in0_beep.dev_attr.attr, | |
1226 | &sensor_dev_attr_in1_beep.dev_attr.attr, | |
1227 | &sensor_dev_attr_in2_beep.dev_attr.attr, | |
1228 | &sensor_dev_attr_in3_beep.dev_attr.attr, | |
1229 | &sensor_dev_attr_in4_beep.dev_attr.attr, | |
1230 | &sensor_dev_attr_in5_beep.dev_attr.attr, | |
1231 | &sensor_dev_attr_in6_beep.dev_attr.attr, | |
1232 | &sensor_dev_attr_in7_beep.dev_attr.attr, | |
1233 | ||
1234 | &sensor_dev_attr_temp1_beep.dev_attr.attr, | |
1235 | &sensor_dev_attr_temp2_beep.dev_attr.attr, | |
1236 | &sensor_dev_attr_temp3_beep.dev_attr.attr, | |
1237 | NULL | |
1238 | }; | |
1239 | ||
1240 | static const struct attribute_group it87_group_beep = { | |
1241 | .attrs = it87_attributes_beep, | |
1242 | }; | |
1243 | ||
723a0aa0 | 1244 | static struct attribute *it87_attributes_fan16[5][3+1] = { { |
87808be4 JD |
1245 | &sensor_dev_attr_fan1_input16.dev_attr.attr, |
1246 | &sensor_dev_attr_fan1_min16.dev_attr.attr, | |
723a0aa0 JD |
1247 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
1248 | NULL | |
1249 | }, { | |
87808be4 JD |
1250 | &sensor_dev_attr_fan2_input16.dev_attr.attr, |
1251 | &sensor_dev_attr_fan2_min16.dev_attr.attr, | |
723a0aa0 JD |
1252 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
1253 | NULL | |
1254 | }, { | |
87808be4 JD |
1255 | &sensor_dev_attr_fan3_input16.dev_attr.attr, |
1256 | &sensor_dev_attr_fan3_min16.dev_attr.attr, | |
723a0aa0 JD |
1257 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
1258 | NULL | |
1259 | }, { | |
c7f1f716 JD |
1260 | &sensor_dev_attr_fan4_input16.dev_attr.attr, |
1261 | &sensor_dev_attr_fan4_min16.dev_attr.attr, | |
723a0aa0 JD |
1262 | &sensor_dev_attr_fan4_alarm.dev_attr.attr, |
1263 | NULL | |
1264 | }, { | |
c7f1f716 JD |
1265 | &sensor_dev_attr_fan5_input16.dev_attr.attr, |
1266 | &sensor_dev_attr_fan5_min16.dev_attr.attr, | |
723a0aa0 JD |
1267 | &sensor_dev_attr_fan5_alarm.dev_attr.attr, |
1268 | NULL | |
1269 | } }; | |
1270 | ||
1271 | static const struct attribute_group it87_group_fan16[5] = { | |
1272 | { .attrs = it87_attributes_fan16[0] }, | |
1273 | { .attrs = it87_attributes_fan16[1] }, | |
1274 | { .attrs = it87_attributes_fan16[2] }, | |
1275 | { .attrs = it87_attributes_fan16[3] }, | |
1276 | { .attrs = it87_attributes_fan16[4] }, | |
1277 | }; | |
87808be4 | 1278 | |
723a0aa0 | 1279 | static struct attribute *it87_attributes_fan[3][4+1] = { { |
87808be4 JD |
1280 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
1281 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
1282 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
723a0aa0 JD |
1283 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
1284 | NULL | |
1285 | }, { | |
87808be4 JD |
1286 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
1287 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
1288 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
723a0aa0 JD |
1289 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
1290 | NULL | |
1291 | }, { | |
87808be4 JD |
1292 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
1293 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
1294 | &sensor_dev_attr_fan3_div.dev_attr.attr, | |
0124dd78 | 1295 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, |
723a0aa0 JD |
1296 | NULL |
1297 | } }; | |
1298 | ||
1299 | static const struct attribute_group it87_group_fan[3] = { | |
1300 | { .attrs = it87_attributes_fan[0] }, | |
1301 | { .attrs = it87_attributes_fan[1] }, | |
1302 | { .attrs = it87_attributes_fan[2] }, | |
1303 | }; | |
1304 | ||
1305 | static const struct attribute_group * | |
1306 | it87_get_fan_group(const struct it87_data *data) | |
1307 | { | |
1308 | return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan; | |
1309 | } | |
0124dd78 | 1310 | |
723a0aa0 | 1311 | static struct attribute *it87_attributes_pwm[3][4+1] = { { |
87808be4 | 1312 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
87808be4 | 1313 | &sensor_dev_attr_pwm1.dev_attr.attr, |
d5b0b5d6 | 1314 | &dev_attr_pwm1_freq.attr, |
94ac7ee6 | 1315 | &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr, |
723a0aa0 JD |
1316 | NULL |
1317 | }, { | |
1318 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, | |
1319 | &sensor_dev_attr_pwm2.dev_attr.attr, | |
1320 | &dev_attr_pwm2_freq.attr, | |
94ac7ee6 | 1321 | &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr, |
723a0aa0 JD |
1322 | NULL |
1323 | }, { | |
1324 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, | |
1325 | &sensor_dev_attr_pwm3.dev_attr.attr, | |
1326 | &dev_attr_pwm3_freq.attr, | |
94ac7ee6 | 1327 | &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr, |
723a0aa0 JD |
1328 | NULL |
1329 | } }; | |
87808be4 | 1330 | |
723a0aa0 JD |
1331 | static const struct attribute_group it87_group_pwm[3] = { |
1332 | { .attrs = it87_attributes_pwm[0] }, | |
1333 | { .attrs = it87_attributes_pwm[1] }, | |
1334 | { .attrs = it87_attributes_pwm[2] }, | |
1335 | }; | |
1336 | ||
4f3f51bc JD |
1337 | static struct attribute *it87_attributes_autopwm[3][9+1] = { { |
1338 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, | |
1339 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, | |
1340 | &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, | |
1341 | &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, | |
1342 | &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, | |
1343 | &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr, | |
1344 | &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, | |
1345 | &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, | |
1346 | &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, | |
1347 | NULL | |
1348 | }, { | |
1349 | &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, | |
1350 | &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr, | |
1351 | &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr, | |
1352 | &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr, | |
1353 | &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr, | |
1354 | &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr, | |
1355 | &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr, | |
1356 | &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr, | |
1357 | &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr, | |
1358 | NULL | |
1359 | }, { | |
1360 | &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, | |
1361 | &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr, | |
1362 | &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr, | |
1363 | &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr, | |
1364 | &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr, | |
1365 | &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr, | |
1366 | &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr, | |
1367 | &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr, | |
1368 | &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr, | |
1369 | NULL | |
1370 | } }; | |
1371 | ||
1372 | static const struct attribute_group it87_group_autopwm[3] = { | |
1373 | { .attrs = it87_attributes_autopwm[0] }, | |
1374 | { .attrs = it87_attributes_autopwm[1] }, | |
1375 | { .attrs = it87_attributes_autopwm[2] }, | |
1376 | }; | |
1377 | ||
d9b327c3 JD |
1378 | static struct attribute *it87_attributes_fan_beep[] = { |
1379 | &sensor_dev_attr_fan1_beep.dev_attr.attr, | |
1380 | &sensor_dev_attr_fan2_beep.dev_attr.attr, | |
1381 | &sensor_dev_attr_fan3_beep.dev_attr.attr, | |
1382 | &sensor_dev_attr_fan4_beep.dev_attr.attr, | |
1383 | &sensor_dev_attr_fan5_beep.dev_attr.attr, | |
1384 | }; | |
1385 | ||
6a8d7acf | 1386 | static struct attribute *it87_attributes_vid[] = { |
87808be4 JD |
1387 | &dev_attr_vrm.attr, |
1388 | &dev_attr_cpu0_vid.attr, | |
1389 | NULL | |
1390 | }; | |
1391 | ||
6a8d7acf JD |
1392 | static const struct attribute_group it87_group_vid = { |
1393 | .attrs = it87_attributes_vid, | |
87808be4 | 1394 | }; |
1da177e4 | 1395 | |
2d8672c5 | 1396 | /* SuperIO detection - will change isa_address if a chip is found */ |
b74f3fdd | 1397 | static int __init it87_find(unsigned short *address, |
1398 | struct it87_sio_data *sio_data) | |
1da177e4 LT |
1399 | { |
1400 | int err = -ENODEV; | |
b74f3fdd | 1401 | u16 chip_type; |
98dd22c3 | 1402 | const char *board_vendor, *board_name; |
1da177e4 LT |
1403 | |
1404 | superio_enter(); | |
67b671bc | 1405 | chip_type = force_id ? force_id : superio_inw(DEVID); |
b74f3fdd | 1406 | |
1407 | switch (chip_type) { | |
1408 | case IT8705F_DEVID: | |
1409 | sio_data->type = it87; | |
1410 | break; | |
1411 | case IT8712F_DEVID: | |
1412 | sio_data->type = it8712; | |
1413 | break; | |
1414 | case IT8716F_DEVID: | |
1415 | case IT8726F_DEVID: | |
1416 | sio_data->type = it8716; | |
1417 | break; | |
1418 | case IT8718F_DEVID: | |
1419 | sio_data->type = it8718; | |
1420 | break; | |
b4da93e4 JMS |
1421 | case IT8720F_DEVID: |
1422 | sio_data->type = it8720; | |
1423 | break; | |
b74f3fdd | 1424 | case 0xffff: /* No device at all */ |
1425 | goto exit; | |
1426 | default: | |
1427 | pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n", | |
1428 | chip_type); | |
1429 | goto exit; | |
1430 | } | |
1da177e4 | 1431 | |
87673dd7 | 1432 | superio_select(PME); |
1da177e4 LT |
1433 | if (!(superio_inb(IT87_ACT_REG) & 0x01)) { |
1434 | pr_info("it87: Device not activated, skipping\n"); | |
1435 | goto exit; | |
1436 | } | |
1437 | ||
1438 | *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1); | |
1439 | if (*address == 0) { | |
1440 | pr_info("it87: Base address not set, skipping\n"); | |
1441 | goto exit; | |
1442 | } | |
1443 | ||
1444 | err = 0; | |
0475169c | 1445 | sio_data->revision = superio_inb(DEVREV) & 0x0f; |
1da177e4 | 1446 | pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n", |
0475169c | 1447 | chip_type, *address, sio_data->revision); |
1da177e4 | 1448 | |
87673dd7 | 1449 | /* Read GPIO config and VID value from LDN 7 (GPIO) */ |
895ff267 JD |
1450 | if (sio_data->type == it87) { |
1451 | /* The IT8705F doesn't have VID pins at all */ | |
1452 | sio_data->skip_vid = 1; | |
d9b327c3 JD |
1453 | |
1454 | /* The IT8705F has a different LD number for GPIO */ | |
1455 | superio_select(5); | |
1456 | sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f; | |
895ff267 | 1457 | } else { |
87673dd7 JD |
1458 | int reg; |
1459 | ||
1460 | superio_select(GPIO); | |
895ff267 JD |
1461 | /* We need at least 4 VID pins */ |
1462 | reg = superio_inb(IT87_SIO_GPIO3_REG); | |
1463 | if (reg & 0x0f) { | |
1464 | pr_info("it87: VID is disabled (pins used for GPIO)\n"); | |
1465 | sio_data->skip_vid = 1; | |
1466 | } | |
1467 | ||
591ec650 JD |
1468 | /* Check if fan3 is there or not */ |
1469 | if (reg & (1 << 6)) | |
1470 | sio_data->skip_pwm |= (1 << 2); | |
1471 | if (reg & (1 << 7)) | |
1472 | sio_data->skip_fan |= (1 << 2); | |
1473 | ||
1474 | /* Check if fan2 is there or not */ | |
1475 | reg = superio_inb(IT87_SIO_GPIO5_REG); | |
1476 | if (reg & (1 << 1)) | |
1477 | sio_data->skip_pwm |= (1 << 1); | |
1478 | if (reg & (1 << 2)) | |
1479 | sio_data->skip_fan |= (1 << 1); | |
1480 | ||
895ff267 JD |
1481 | if ((sio_data->type == it8718 || sio_data->type == it8720) |
1482 | && !(sio_data->skip_vid)) | |
b74f3fdd | 1483 | sio_data->vid_value = superio_inb(IT87_SIO_VID_REG); |
87673dd7 JD |
1484 | |
1485 | reg = superio_inb(IT87_SIO_PINX2_REG); | |
1486 | if (reg & (1 << 0)) | |
1487 | pr_info("it87: in3 is VCC (+5V)\n"); | |
1488 | if (reg & (1 << 1)) | |
1489 | pr_info("it87: in7 is VCCH (+5V Stand-By)\n"); | |
d9b327c3 JD |
1490 | |
1491 | sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f; | |
87673dd7 | 1492 | } |
d9b327c3 JD |
1493 | if (sio_data->beep_pin) |
1494 | pr_info("it87: Beeping is supported\n"); | |
87673dd7 | 1495 | |
98dd22c3 JD |
1496 | /* Disable specific features based on DMI strings */ |
1497 | board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); | |
1498 | board_name = dmi_get_system_info(DMI_BOARD_NAME); | |
1499 | if (board_vendor && board_name) { | |
1500 | if (strcmp(board_vendor, "nVIDIA") == 0 | |
1501 | && strcmp(board_name, "FN68PT") == 0) { | |
1502 | /* On the Shuttle SN68PT, FAN_CTL2 is apparently not | |
1503 | connected to a fan, but to something else. One user | |
1504 | has reported instant system power-off when changing | |
1505 | the PWM2 duty cycle, so we disable it. | |
1506 | I use the board name string as the trigger in case | |
1507 | the same board is ever used in other systems. */ | |
1508 | pr_info("it87: Disabling pwm2 due to " | |
1509 | "hardware constraints\n"); | |
1510 | sio_data->skip_pwm = (1 << 1); | |
1511 | } | |
1512 | } | |
1513 | ||
1da177e4 LT |
1514 | exit: |
1515 | superio_exit(); | |
1516 | return err; | |
1517 | } | |
1518 | ||
723a0aa0 JD |
1519 | static void it87_remove_files(struct device *dev) |
1520 | { | |
1521 | struct it87_data *data = platform_get_drvdata(pdev); | |
1522 | struct it87_sio_data *sio_data = dev->platform_data; | |
1523 | const struct attribute_group *fan_group = it87_get_fan_group(data); | |
1524 | int i; | |
1525 | ||
1526 | sysfs_remove_group(&dev->kobj, &it87_group); | |
d9b327c3 JD |
1527 | if (sio_data->beep_pin) |
1528 | sysfs_remove_group(&dev->kobj, &it87_group_beep); | |
723a0aa0 JD |
1529 | for (i = 0; i < 5; i++) { |
1530 | if (!(data->has_fan & (1 << i))) | |
1531 | continue; | |
1532 | sysfs_remove_group(&dev->kobj, &fan_group[i]); | |
d9b327c3 JD |
1533 | if (sio_data->beep_pin) |
1534 | sysfs_remove_file(&dev->kobj, | |
1535 | it87_attributes_fan_beep[i]); | |
723a0aa0 JD |
1536 | } |
1537 | for (i = 0; i < 3; i++) { | |
1538 | if (sio_data->skip_pwm & (1 << 0)) | |
1539 | continue; | |
1540 | sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]); | |
4f3f51bc JD |
1541 | if (has_old_autopwm(data)) |
1542 | sysfs_remove_group(&dev->kobj, | |
1543 | &it87_group_autopwm[i]); | |
723a0aa0 | 1544 | } |
6a8d7acf JD |
1545 | if (!sio_data->skip_vid) |
1546 | sysfs_remove_group(&dev->kobj, &it87_group_vid); | |
723a0aa0 JD |
1547 | } |
1548 | ||
b74f3fdd | 1549 | static int __devinit it87_probe(struct platform_device *pdev) |
1da177e4 | 1550 | { |
1da177e4 | 1551 | struct it87_data *data; |
b74f3fdd | 1552 | struct resource *res; |
1553 | struct device *dev = &pdev->dev; | |
1554 | struct it87_sio_data *sio_data = dev->platform_data; | |
723a0aa0 JD |
1555 | const struct attribute_group *fan_group; |
1556 | int err = 0, i; | |
1da177e4 | 1557 | int enable_pwm_interface; |
d9b327c3 | 1558 | int fan_beep_need_rw; |
b74f3fdd | 1559 | static const char *names[] = { |
1560 | "it87", | |
1561 | "it8712", | |
1562 | "it8716", | |
1563 | "it8718", | |
b4da93e4 | 1564 | "it8720", |
b74f3fdd | 1565 | }; |
1566 | ||
1567 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | |
87b4b663 | 1568 | if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) { |
b74f3fdd | 1569 | dev_err(dev, "Failed to request region 0x%lx-0x%lx\n", |
1570 | (unsigned long)res->start, | |
87b4b663 | 1571 | (unsigned long)(res->start + IT87_EC_EXTENT - 1)); |
8e9afcbb JD |
1572 | err = -EBUSY; |
1573 | goto ERROR0; | |
1574 | } | |
1da177e4 | 1575 | |
5f2dc798 JD |
1576 | data = kzalloc(sizeof(struct it87_data), GFP_KERNEL); |
1577 | if (!data) { | |
1da177e4 LT |
1578 | err = -ENOMEM; |
1579 | goto ERROR1; | |
1580 | } | |
1da177e4 | 1581 | |
b74f3fdd | 1582 | data->addr = res->start; |
1583 | data->type = sio_data->type; | |
0475169c | 1584 | data->revision = sio_data->revision; |
b74f3fdd | 1585 | data->name = names[sio_data->type]; |
1da177e4 LT |
1586 | |
1587 | /* Now, we do the remaining detection. */ | |
b74f3fdd | 1588 | if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) |
1589 | || it87_read_value(data, IT87_REG_CHIPID) != 0x90) { | |
8e9afcbb JD |
1590 | err = -ENODEV; |
1591 | goto ERROR2; | |
1da177e4 LT |
1592 | } |
1593 | ||
b74f3fdd | 1594 | platform_set_drvdata(pdev, data); |
1da177e4 | 1595 | |
9a61bf63 | 1596 | mutex_init(&data->update_lock); |
1da177e4 | 1597 | |
1da177e4 | 1598 | /* Check PWM configuration */ |
b74f3fdd | 1599 | enable_pwm_interface = it87_check_pwm(dev); |
1da177e4 LT |
1600 | |
1601 | /* Initialize the IT87 chip */ | |
b74f3fdd | 1602 | it87_init_device(pdev); |
1da177e4 LT |
1603 | |
1604 | /* Register sysfs hooks */ | |
5f2dc798 JD |
1605 | err = sysfs_create_group(&dev->kobj, &it87_group); |
1606 | if (err) | |
b74f3fdd | 1607 | goto ERROR2; |
17d648bf | 1608 | |
d9b327c3 JD |
1609 | if (sio_data->beep_pin) { |
1610 | err = sysfs_create_group(&dev->kobj, &it87_group_beep); | |
1611 | if (err) | |
1612 | goto ERROR4; | |
1613 | } | |
1614 | ||
9060f8bd | 1615 | /* Do not create fan files for disabled fans */ |
723a0aa0 | 1616 | fan_group = it87_get_fan_group(data); |
d9b327c3 | 1617 | fan_beep_need_rw = 1; |
723a0aa0 JD |
1618 | for (i = 0; i < 5; i++) { |
1619 | if (!(data->has_fan & (1 << i))) | |
1620 | continue; | |
1621 | err = sysfs_create_group(&dev->kobj, &fan_group[i]); | |
1622 | if (err) | |
1623 | goto ERROR4; | |
d9b327c3 JD |
1624 | |
1625 | if (sio_data->beep_pin) { | |
1626 | err = sysfs_create_file(&dev->kobj, | |
1627 | it87_attributes_fan_beep[i]); | |
1628 | if (err) | |
1629 | goto ERROR4; | |
1630 | if (!fan_beep_need_rw) | |
1631 | continue; | |
1632 | ||
1633 | /* As we have a single beep enable bit for all fans, | |
1634 | * only the first enabled fan has a writable attribute | |
1635 | * for it. */ | |
1636 | if (sysfs_chmod_file(&dev->kobj, | |
1637 | it87_attributes_fan_beep[i], | |
1638 | S_IRUGO | S_IWUSR)) | |
1639 | dev_dbg(dev, "chmod +w fan%d_beep failed\n", | |
1640 | i + 1); | |
1641 | fan_beep_need_rw = 0; | |
1642 | } | |
17d648bf JD |
1643 | } |
1644 | ||
1da177e4 | 1645 | if (enable_pwm_interface) { |
723a0aa0 JD |
1646 | for (i = 0; i < 3; i++) { |
1647 | if (sio_data->skip_pwm & (1 << i)) | |
1648 | continue; | |
1649 | err = sysfs_create_group(&dev->kobj, | |
1650 | &it87_group_pwm[i]); | |
1651 | if (err) | |
98dd22c3 | 1652 | goto ERROR4; |
4f3f51bc JD |
1653 | |
1654 | if (!has_old_autopwm(data)) | |
1655 | continue; | |
1656 | err = sysfs_create_group(&dev->kobj, | |
1657 | &it87_group_autopwm[i]); | |
1658 | if (err) | |
1659 | goto ERROR4; | |
98dd22c3 | 1660 | } |
1da177e4 LT |
1661 | } |
1662 | ||
895ff267 | 1663 | if (!sio_data->skip_vid) { |
303760b4 | 1664 | data->vrm = vid_which_vrm(); |
87673dd7 | 1665 | /* VID reading from Super-I/O config space if available */ |
b74f3fdd | 1666 | data->vid = sio_data->vid_value; |
6a8d7acf JD |
1667 | err = sysfs_create_group(&dev->kobj, &it87_group_vid); |
1668 | if (err) | |
87808be4 JD |
1669 | goto ERROR4; |
1670 | } | |
1671 | ||
1beeffe4 TJ |
1672 | data->hwmon_dev = hwmon_device_register(dev); |
1673 | if (IS_ERR(data->hwmon_dev)) { | |
1674 | err = PTR_ERR(data->hwmon_dev); | |
87808be4 | 1675 | goto ERROR4; |
1da177e4 LT |
1676 | } |
1677 | ||
1678 | return 0; | |
1679 | ||
87808be4 | 1680 | ERROR4: |
723a0aa0 | 1681 | it87_remove_files(dev); |
1da177e4 | 1682 | ERROR2: |
b74f3fdd | 1683 | platform_set_drvdata(pdev, NULL); |
1da177e4 LT |
1684 | kfree(data); |
1685 | ERROR1: | |
87b4b663 | 1686 | release_region(res->start, IT87_EC_EXTENT); |
1da177e4 LT |
1687 | ERROR0: |
1688 | return err; | |
1689 | } | |
1690 | ||
b74f3fdd | 1691 | static int __devexit it87_remove(struct platform_device *pdev) |
1da177e4 | 1692 | { |
b74f3fdd | 1693 | struct it87_data *data = platform_get_drvdata(pdev); |
1da177e4 | 1694 | |
1beeffe4 | 1695 | hwmon_device_unregister(data->hwmon_dev); |
723a0aa0 | 1696 | it87_remove_files(&pdev->dev); |
943b0830 | 1697 | |
87b4b663 | 1698 | release_region(data->addr, IT87_EC_EXTENT); |
b74f3fdd | 1699 | platform_set_drvdata(pdev, NULL); |
943b0830 | 1700 | kfree(data); |
1da177e4 LT |
1701 | |
1702 | return 0; | |
1703 | } | |
1704 | ||
7f999aa7 | 1705 | /* Must be called with data->update_lock held, except during initialization. |
1da177e4 LT |
1706 | We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks, |
1707 | would slow down the IT87 access and should not be necessary. */ | |
b74f3fdd | 1708 | static int it87_read_value(struct it87_data *data, u8 reg) |
1da177e4 | 1709 | { |
b74f3fdd | 1710 | outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET); |
1711 | return inb_p(data->addr + IT87_DATA_REG_OFFSET); | |
1da177e4 LT |
1712 | } |
1713 | ||
7f999aa7 | 1714 | /* Must be called with data->update_lock held, except during initialization. |
1da177e4 LT |
1715 | We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks, |
1716 | would slow down the IT87 access and should not be necessary. */ | |
b74f3fdd | 1717 | static void it87_write_value(struct it87_data *data, u8 reg, u8 value) |
1da177e4 | 1718 | { |
b74f3fdd | 1719 | outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET); |
1720 | outb_p(value, data->addr + IT87_DATA_REG_OFFSET); | |
1da177e4 LT |
1721 | } |
1722 | ||
1723 | /* Return 1 if and only if the PWM interface is safe to use */ | |
b74f3fdd | 1724 | static int __devinit it87_check_pwm(struct device *dev) |
1da177e4 | 1725 | { |
b74f3fdd | 1726 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
1727 | /* Some BIOSes fail to correctly configure the IT87 fans. All fans off |
1728 | * and polarity set to active low is sign that this is the case so we | |
1729 | * disable pwm control to protect the user. */ | |
b74f3fdd | 1730 | int tmp = it87_read_value(data, IT87_REG_FAN_CTL); |
1da177e4 LT |
1731 | if ((tmp & 0x87) == 0) { |
1732 | if (fix_pwm_polarity) { | |
1733 | /* The user asks us to attempt a chip reconfiguration. | |
1734 | * This means switching to active high polarity and | |
1735 | * inverting all fan speed values. */ | |
1736 | int i; | |
1737 | u8 pwm[3]; | |
1738 | ||
1739 | for (i = 0; i < 3; i++) | |
b74f3fdd | 1740 | pwm[i] = it87_read_value(data, |
1da177e4 LT |
1741 | IT87_REG_PWM(i)); |
1742 | ||
1743 | /* If any fan is in automatic pwm mode, the polarity | |
1744 | * might be correct, as suspicious as it seems, so we | |
1745 | * better don't change anything (but still disable the | |
1746 | * PWM interface). */ | |
1747 | if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) { | |
b74f3fdd | 1748 | dev_info(dev, "Reconfiguring PWM to " |
1da177e4 | 1749 | "active high polarity\n"); |
b74f3fdd | 1750 | it87_write_value(data, IT87_REG_FAN_CTL, |
1da177e4 LT |
1751 | tmp | 0x87); |
1752 | for (i = 0; i < 3; i++) | |
b74f3fdd | 1753 | it87_write_value(data, |
1da177e4 LT |
1754 | IT87_REG_PWM(i), |
1755 | 0x7f & ~pwm[i]); | |
1756 | return 1; | |
1757 | } | |
1758 | ||
b74f3fdd | 1759 | dev_info(dev, "PWM configuration is " |
1da177e4 LT |
1760 | "too broken to be fixed\n"); |
1761 | } | |
1762 | ||
b74f3fdd | 1763 | dev_info(dev, "Detected broken BIOS " |
1da177e4 LT |
1764 | "defaults, disabling PWM interface\n"); |
1765 | return 0; | |
1766 | } else if (fix_pwm_polarity) { | |
b74f3fdd | 1767 | dev_info(dev, "PWM configuration looks " |
1da177e4 LT |
1768 | "sane, won't touch\n"); |
1769 | } | |
1770 | ||
1771 | return 1; | |
1772 | } | |
1773 | ||
1774 | /* Called when we have found a new IT87. */ | |
b74f3fdd | 1775 | static void __devinit it87_init_device(struct platform_device *pdev) |
1da177e4 | 1776 | { |
591ec650 | 1777 | struct it87_sio_data *sio_data = pdev->dev.platform_data; |
b74f3fdd | 1778 | struct it87_data *data = platform_get_drvdata(pdev); |
1da177e4 | 1779 | int tmp, i; |
591ec650 | 1780 | u8 mask; |
1da177e4 | 1781 | |
b99883dc JD |
1782 | /* For each PWM channel: |
1783 | * - If it is in automatic mode, setting to manual mode should set | |
1784 | * the fan to full speed by default. | |
1785 | * - If it is in manual mode, we need a mapping to temperature | |
1786 | * channels to use when later setting to automatic mode later. | |
1787 | * Use a 1:1 mapping by default (we are clueless.) | |
1788 | * In both cases, the value can (and should) be changed by the user | |
1789 | * prior to switching to a different mode. */ | |
1da177e4 | 1790 | for (i = 0; i < 3; i++) { |
b99883dc JD |
1791 | data->pwm_temp_map[i] = i; |
1792 | data->pwm_duty[i] = 0x7f; /* Full speed */ | |
4f3f51bc | 1793 | data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */ |
1da177e4 LT |
1794 | } |
1795 | ||
c5df9b7a JD |
1796 | /* Some chips seem to have default value 0xff for all limit |
1797 | * registers. For low voltage limits it makes no sense and triggers | |
1798 | * alarms, so change to 0 instead. For high temperature limits, it | |
1799 | * means -1 degree C, which surprisingly doesn't trigger an alarm, | |
1800 | * but is still confusing, so change to 127 degrees C. */ | |
1801 | for (i = 0; i < 8; i++) { | |
b74f3fdd | 1802 | tmp = it87_read_value(data, IT87_REG_VIN_MIN(i)); |
c5df9b7a | 1803 | if (tmp == 0xff) |
b74f3fdd | 1804 | it87_write_value(data, IT87_REG_VIN_MIN(i), 0); |
c5df9b7a JD |
1805 | } |
1806 | for (i = 0; i < 3; i++) { | |
b74f3fdd | 1807 | tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i)); |
c5df9b7a | 1808 | if (tmp == 0xff) |
b74f3fdd | 1809 | it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127); |
c5df9b7a JD |
1810 | } |
1811 | ||
77fa49d9 | 1812 | /* Check if temperature channels are reset manually or by some reason */ |
b74f3fdd | 1813 | tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE); |
1da177e4 LT |
1814 | if ((tmp & 0x3f) == 0) { |
1815 | /* Temp1,Temp3=thermistor; Temp2=thermal diode */ | |
1816 | tmp = (tmp & 0xc0) | 0x2a; | |
b74f3fdd | 1817 | it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp); |
1da177e4 LT |
1818 | } |
1819 | data->sensor = tmp; | |
1820 | ||
1821 | /* Check if voltage monitors are reset manually or by some reason */ | |
b74f3fdd | 1822 | tmp = it87_read_value(data, IT87_REG_VIN_ENABLE); |
1da177e4 LT |
1823 | if ((tmp & 0xff) == 0) { |
1824 | /* Enable all voltage monitors */ | |
b74f3fdd | 1825 | it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff); |
1da177e4 LT |
1826 | } |
1827 | ||
1828 | /* Check if tachometers are reset manually or by some reason */ | |
591ec650 | 1829 | mask = 0x70 & ~(sio_data->skip_fan << 4); |
b74f3fdd | 1830 | data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL); |
591ec650 | 1831 | if ((data->fan_main_ctrl & mask) == 0) { |
1da177e4 | 1832 | /* Enable all fan tachometers */ |
591ec650 | 1833 | data->fan_main_ctrl |= mask; |
5f2dc798 JD |
1834 | it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, |
1835 | data->fan_main_ctrl); | |
1da177e4 | 1836 | } |
9060f8bd | 1837 | data->has_fan = (data->fan_main_ctrl >> 4) & 0x07; |
1da177e4 | 1838 | |
17d648bf | 1839 | /* Set tachometers to 16-bit mode if needed */ |
0475169c | 1840 | if (has_16bit_fans(data)) { |
b74f3fdd | 1841 | tmp = it87_read_value(data, IT87_REG_FAN_16BIT); |
9060f8bd | 1842 | if (~tmp & 0x07 & data->has_fan) { |
b74f3fdd | 1843 | dev_dbg(&pdev->dev, |
17d648bf | 1844 | "Setting fan1-3 to 16-bit mode\n"); |
b74f3fdd | 1845 | it87_write_value(data, IT87_REG_FAN_16BIT, |
17d648bf JD |
1846 | tmp | 0x07); |
1847 | } | |
816d8c6a AP |
1848 | /* IT8705F only supports three fans. */ |
1849 | if (data->type != it87) { | |
1850 | if (tmp & (1 << 4)) | |
1851 | data->has_fan |= (1 << 3); /* fan4 enabled */ | |
1852 | if (tmp & (1 << 5)) | |
1853 | data->has_fan |= (1 << 4); /* fan5 enabled */ | |
1854 | } | |
17d648bf JD |
1855 | } |
1856 | ||
591ec650 JD |
1857 | /* Fan input pins may be used for alternative functions */ |
1858 | data->has_fan &= ~sio_data->skip_fan; | |
1859 | ||
1da177e4 | 1860 | /* Start monitoring */ |
b74f3fdd | 1861 | it87_write_value(data, IT87_REG_CONFIG, |
1862 | (it87_read_value(data, IT87_REG_CONFIG) & 0x36) | |
1da177e4 LT |
1863 | | (update_vbat ? 0x41 : 0x01)); |
1864 | } | |
1865 | ||
b99883dc JD |
1866 | static void it87_update_pwm_ctrl(struct it87_data *data, int nr) |
1867 | { | |
1868 | data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr)); | |
1869 | if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */ | |
1870 | data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03; | |
1871 | else /* Manual mode */ | |
1872 | data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f; | |
4f3f51bc JD |
1873 | |
1874 | if (has_old_autopwm(data)) { | |
1875 | int i; | |
1876 | ||
1877 | for (i = 0; i < 5 ; i++) | |
1878 | data->auto_temp[nr][i] = it87_read_value(data, | |
1879 | IT87_REG_AUTO_TEMP(nr, i)); | |
1880 | for (i = 0; i < 3 ; i++) | |
1881 | data->auto_pwm[nr][i] = it87_read_value(data, | |
1882 | IT87_REG_AUTO_PWM(nr, i)); | |
1883 | } | |
b99883dc JD |
1884 | } |
1885 | ||
1da177e4 LT |
1886 | static struct it87_data *it87_update_device(struct device *dev) |
1887 | { | |
b74f3fdd | 1888 | struct it87_data *data = dev_get_drvdata(dev); |
1da177e4 LT |
1889 | int i; |
1890 | ||
9a61bf63 | 1891 | mutex_lock(&data->update_lock); |
1da177e4 LT |
1892 | |
1893 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
1894 | || !data->valid) { | |
1da177e4 LT |
1895 | if (update_vbat) { |
1896 | /* Cleared after each update, so reenable. Value | |
5f2dc798 | 1897 | returned by this read will be previous value */ |
b74f3fdd | 1898 | it87_write_value(data, IT87_REG_CONFIG, |
5f2dc798 | 1899 | it87_read_value(data, IT87_REG_CONFIG) | 0x40); |
1da177e4 LT |
1900 | } |
1901 | for (i = 0; i <= 7; i++) { | |
1902 | data->in[i] = | |
5f2dc798 | 1903 | it87_read_value(data, IT87_REG_VIN(i)); |
1da177e4 | 1904 | data->in_min[i] = |
5f2dc798 | 1905 | it87_read_value(data, IT87_REG_VIN_MIN(i)); |
1da177e4 | 1906 | data->in_max[i] = |
5f2dc798 | 1907 | it87_read_value(data, IT87_REG_VIN_MAX(i)); |
1da177e4 | 1908 | } |
3543a53f | 1909 | /* in8 (battery) has no limit registers */ |
5f2dc798 | 1910 | data->in[8] = it87_read_value(data, IT87_REG_VIN(8)); |
1da177e4 | 1911 | |
c7f1f716 | 1912 | for (i = 0; i < 5; i++) { |
9060f8bd JD |
1913 | /* Skip disabled fans */ |
1914 | if (!(data->has_fan & (1 << i))) | |
1915 | continue; | |
1916 | ||
1da177e4 | 1917 | data->fan_min[i] = |
5f2dc798 | 1918 | it87_read_value(data, IT87_REG_FAN_MIN[i]); |
b74f3fdd | 1919 | data->fan[i] = it87_read_value(data, |
c7f1f716 | 1920 | IT87_REG_FAN[i]); |
17d648bf | 1921 | /* Add high byte if in 16-bit mode */ |
0475169c | 1922 | if (has_16bit_fans(data)) { |
b74f3fdd | 1923 | data->fan[i] |= it87_read_value(data, |
c7f1f716 | 1924 | IT87_REG_FANX[i]) << 8; |
b74f3fdd | 1925 | data->fan_min[i] |= it87_read_value(data, |
c7f1f716 | 1926 | IT87_REG_FANX_MIN[i]) << 8; |
17d648bf | 1927 | } |
1da177e4 LT |
1928 | } |
1929 | for (i = 0; i < 3; i++) { | |
1930 | data->temp[i] = | |
5f2dc798 | 1931 | it87_read_value(data, IT87_REG_TEMP(i)); |
1da177e4 | 1932 | data->temp_high[i] = |
5f2dc798 | 1933 | it87_read_value(data, IT87_REG_TEMP_HIGH(i)); |
1da177e4 | 1934 | data->temp_low[i] = |
5f2dc798 | 1935 | it87_read_value(data, IT87_REG_TEMP_LOW(i)); |
1da177e4 LT |
1936 | } |
1937 | ||
17d648bf | 1938 | /* Newer chips don't have clock dividers */ |
0475169c | 1939 | if ((data->has_fan & 0x07) && !has_16bit_fans(data)) { |
b74f3fdd | 1940 | i = it87_read_value(data, IT87_REG_FAN_DIV); |
17d648bf JD |
1941 | data->fan_div[0] = i & 0x07; |
1942 | data->fan_div[1] = (i >> 3) & 0x07; | |
1943 | data->fan_div[2] = (i & 0x40) ? 3 : 1; | |
1944 | } | |
1da177e4 LT |
1945 | |
1946 | data->alarms = | |
b74f3fdd | 1947 | it87_read_value(data, IT87_REG_ALARM1) | |
1948 | (it87_read_value(data, IT87_REG_ALARM2) << 8) | | |
1949 | (it87_read_value(data, IT87_REG_ALARM3) << 16); | |
d9b327c3 | 1950 | data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE); |
b99883dc | 1951 | |
b74f3fdd | 1952 | data->fan_main_ctrl = it87_read_value(data, |
1953 | IT87_REG_FAN_MAIN_CTRL); | |
1954 | data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL); | |
b99883dc JD |
1955 | for (i = 0; i < 3; i++) |
1956 | it87_update_pwm_ctrl(data, i); | |
b74f3fdd | 1957 | |
1958 | data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE); | |
0475169c | 1959 | /* The 8705 does not have VID capability. |
b4da93e4 JMS |
1960 | The 8718 and the 8720 don't use IT87_REG_VID for the |
1961 | same purpose. */ | |
17d648bf | 1962 | if (data->type == it8712 || data->type == it8716) { |
b74f3fdd | 1963 | data->vid = it87_read_value(data, IT87_REG_VID); |
17d648bf JD |
1964 | /* The older IT8712F revisions had only 5 VID pins, |
1965 | but we assume it is always safe to read 6 bits. */ | |
1966 | data->vid &= 0x3f; | |
1da177e4 LT |
1967 | } |
1968 | data->last_updated = jiffies; | |
1969 | data->valid = 1; | |
1970 | } | |
1971 | ||
9a61bf63 | 1972 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
1973 | |
1974 | return data; | |
1975 | } | |
1976 | ||
b74f3fdd | 1977 | static int __init it87_device_add(unsigned short address, |
1978 | const struct it87_sio_data *sio_data) | |
1979 | { | |
1980 | struct resource res = { | |
87b4b663 BH |
1981 | .start = address + IT87_EC_OFFSET, |
1982 | .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1, | |
b74f3fdd | 1983 | .name = DRVNAME, |
1984 | .flags = IORESOURCE_IO, | |
1985 | }; | |
1986 | int err; | |
1987 | ||
b9acb64a JD |
1988 | err = acpi_check_resource_conflict(&res); |
1989 | if (err) | |
1990 | goto exit; | |
1991 | ||
b74f3fdd | 1992 | pdev = platform_device_alloc(DRVNAME, address); |
1993 | if (!pdev) { | |
1994 | err = -ENOMEM; | |
1995 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); | |
1996 | goto exit; | |
1997 | } | |
1998 | ||
1999 | err = platform_device_add_resources(pdev, &res, 1); | |
2000 | if (err) { | |
2001 | printk(KERN_ERR DRVNAME ": Device resource addition failed " | |
2002 | "(%d)\n", err); | |
2003 | goto exit_device_put; | |
2004 | } | |
2005 | ||
2006 | err = platform_device_add_data(pdev, sio_data, | |
2007 | sizeof(struct it87_sio_data)); | |
2008 | if (err) { | |
2009 | printk(KERN_ERR DRVNAME ": Platform data allocation failed\n"); | |
2010 | goto exit_device_put; | |
2011 | } | |
2012 | ||
2013 | err = platform_device_add(pdev); | |
2014 | if (err) { | |
2015 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", | |
2016 | err); | |
2017 | goto exit_device_put; | |
2018 | } | |
2019 | ||
2020 | return 0; | |
2021 | ||
2022 | exit_device_put: | |
2023 | platform_device_put(pdev); | |
2024 | exit: | |
2025 | return err; | |
2026 | } | |
2027 | ||
1da177e4 LT |
2028 | static int __init sm_it87_init(void) |
2029 | { | |
b74f3fdd | 2030 | int err; |
5f2dc798 | 2031 | unsigned short isa_address = 0; |
b74f3fdd | 2032 | struct it87_sio_data sio_data; |
2033 | ||
98dd22c3 | 2034 | memset(&sio_data, 0, sizeof(struct it87_sio_data)); |
b74f3fdd | 2035 | err = it87_find(&isa_address, &sio_data); |
2036 | if (err) | |
2037 | return err; | |
2038 | err = platform_driver_register(&it87_driver); | |
2039 | if (err) | |
2040 | return err; | |
fde09509 | 2041 | |
b74f3fdd | 2042 | err = it87_device_add(isa_address, &sio_data); |
5f2dc798 | 2043 | if (err) { |
b74f3fdd | 2044 | platform_driver_unregister(&it87_driver); |
2045 | return err; | |
2046 | } | |
2047 | ||
2048 | return 0; | |
1da177e4 LT |
2049 | } |
2050 | ||
2051 | static void __exit sm_it87_exit(void) | |
2052 | { | |
b74f3fdd | 2053 | platform_device_unregister(pdev); |
2054 | platform_driver_unregister(&it87_driver); | |
1da177e4 LT |
2055 | } |
2056 | ||
2057 | ||
f1d8e332 | 2058 | MODULE_AUTHOR("Chris Gauthron, " |
b19367c6 | 2059 | "Jean Delvare <khali@linux-fr.org>"); |
b4da93e4 | 2060 | MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver"); |
1da177e4 LT |
2061 | module_param(update_vbat, bool, 0); |
2062 | MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value"); | |
2063 | module_param(fix_pwm_polarity, bool, 0); | |
5f2dc798 JD |
2064 | MODULE_PARM_DESC(fix_pwm_polarity, |
2065 | "Force PWM polarity to active high (DANGEROUS)"); | |
1da177e4 LT |
2066 | MODULE_LICENSE("GPL"); |
2067 | ||
2068 | module_init(sm_it87_init); | |
2069 | module_exit(sm_it87_exit); |