Commit | Line | Data |
---|---|---|
1de9e371 RL |
1 | /* |
2 | vt8231.c - Part of lm_sensors, Linux kernel modules | |
3 | for hardware monitoring | |
4 | ||
af865765 | 5 | Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk> |
1de9e371 RL |
6 | Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com> |
7 | Aaron M. Marsh <amarsh@sdf.lonestar.org> | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | */ | |
23 | ||
24 | /* Supports VIA VT8231 South Bridge embedded sensors | |
25 | */ | |
26 | ||
9d72be0d JP |
27 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
28 | ||
1de9e371 RL |
29 | #include <linux/module.h> |
30 | #include <linux/init.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/pci.h> | |
33 | #include <linux/jiffies.h> | |
ec5e1a4b | 34 | #include <linux/platform_device.h> |
1de9e371 RL |
35 | #include <linux/hwmon.h> |
36 | #include <linux/hwmon-sysfs.h> | |
37 | #include <linux/hwmon-vid.h> | |
38 | #include <linux/err.h> | |
9a61bf63 | 39 | #include <linux/mutex.h> |
b9acb64a | 40 | #include <linux/acpi.h> |
6055fae8 | 41 | #include <linux/io.h> |
1de9e371 RL |
42 | |
43 | static int force_addr; | |
44 | module_param(force_addr, int, 0); | |
45 | MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors"); | |
46 | ||
ec5e1a4b | 47 | static struct platform_device *pdev; |
1de9e371 RL |
48 | |
49 | #define VT8231_EXTENT 0x80 | |
50 | #define VT8231_BASE_REG 0x70 | |
51 | #define VT8231_ENABLE_REG 0x74 | |
52 | ||
53 | /* The VT8231 registers | |
54 | ||
55 | The reset value for the input channel configuration is used (Reg 0x4A=0x07) | |
56 | which sets the selected inputs marked with '*' below if multiple options are | |
57 | possible: | |
58 | ||
65fe5c79 GR |
59 | Voltage Mode Temperature Mode |
60 | Sensor Linux Id Linux Id VIA Id | |
61 | -------- -------- -------- ------ | |
1de9e371 RL |
62 | CPU Diode N/A temp1 0 |
63 | UIC1 in0 temp2 * 1 | |
65fe5c79 | 64 | UIC2 in1 * temp3 2 |
1de9e371 RL |
65 | UIC3 in2 * temp4 3 |
66 | UIC4 in3 * temp5 4 | |
67 | UIC5 in4 * temp6 5 | |
68 | 3.3V in5 N/A | |
69 | ||
70 | Note that the BIOS may set the configuration register to a different value | |
71 | to match the motherboard configuration. | |
72 | */ | |
73 | ||
74 | /* fans numbered 0-1 */ | |
75 | #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr)) | |
76 | #define VT8231_REG_FAN(nr) (0x29 + (nr)) | |
77 | ||
78 | /* Voltage inputs numbered 0-5 */ | |
79 | ||
80 | static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 }; | |
81 | static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 }; | |
82 | static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 }; | |
83 | ||
84 | /* Temperatures are numbered 1-6 according to the Linux kernel specification. | |
85 | ** | |
86 | ** In the VIA datasheet, however, the temperatures are numbered from zero. | |
87 | ** Since it is important that this driver can easily be compared to the VIA | |
88 | ** datasheet, we will use the VIA numbering within this driver and map the | |
89 | ** kernel sysfs device name to the VIA number in the sysfs callback. | |
90 | */ | |
91 | ||
92 | #define VT8231_REG_TEMP_LOW01 0x49 | |
93 | #define VT8231_REG_TEMP_LOW25 0x4d | |
94 | ||
95 | static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 }; | |
96 | static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 }; | |
97 | static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 }; | |
98 | ||
99 | #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210) | |
100 | #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210) | |
101 | #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200) | |
102 | ||
103 | #define VT8231_REG_CONFIG 0x40 | |
104 | #define VT8231_REG_ALARM1 0x41 | |
105 | #define VT8231_REG_ALARM2 0x42 | |
106 | #define VT8231_REG_FANDIV 0x47 | |
107 | #define VT8231_REG_UCH_CONFIG 0x4a | |
108 | #define VT8231_REG_TEMP1_CONFIG 0x4b | |
109 | #define VT8231_REG_TEMP2_CONFIG 0x4c | |
110 | ||
111 | /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux | |
112 | ** numbering | |
113 | */ | |
114 | #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \ | |
115 | ((ch_config) >> ((i)+1)) & 0x01) | |
116 | /* voltages 0-5 */ | |
117 | #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \ | |
118 | !(((ch_config) >> ((i)+2)) & 0x01)) | |
119 | ||
120 | #define DIV_FROM_REG(val) (1 << (val)) | |
121 | ||
122 | /* NB The values returned here are NOT temperatures. The calibration curves | |
123 | ** for the thermistor curves are board-specific and must go in the | |
124 | ** sensors.conf file. Temperature sensors are actually ten bits, but the | |
125 | ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[] | |
126 | ** register. The temperature value returned should have a magnitude of 3, | |
127 | ** so we use the VIA scaling as the "true" scaling and use the remaining 2 | |
128 | ** LSBs as fractional precision. | |
129 | ** | |
130 | ** All the on-chip hardware temperature comparisons for the alarms are only | |
131 | ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits | |
132 | ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are | |
133 | ** ignored. | |
134 | */ | |
135 | ||
136 | /******** FAN RPM CONVERSIONS ******** | |
137 | ** This chip saturates back at 0, not at 255 like many the other chips. | |
138 | ** So, 0 means 0 RPM | |
139 | */ | |
140 | static inline u8 FAN_TO_REG(long rpm, int div) | |
141 | { | |
142 | if (rpm == 0) | |
143 | return 0; | |
144 | return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255); | |
145 | } | |
146 | ||
147 | #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div))) | |
148 | ||
149 | struct vt8231_data { | |
ec5e1a4b RL |
150 | unsigned short addr; |
151 | const char *name; | |
152 | ||
9a61bf63 | 153 | struct mutex update_lock; |
1beeffe4 | 154 | struct device *hwmon_dev; |
1de9e371 RL |
155 | char valid; /* !=0 if following fields are valid */ |
156 | unsigned long last_updated; /* In jiffies */ | |
157 | ||
158 | u8 in[6]; /* Register value */ | |
159 | u8 in_max[6]; /* Register value */ | |
160 | u8 in_min[6]; /* Register value */ | |
161 | u16 temp[6]; /* Register value 10 bit, right aligned */ | |
162 | u8 temp_max[6]; /* Register value */ | |
163 | u8 temp_min[6]; /* Register value */ | |
164 | u8 fan[2]; /* Register value */ | |
165 | u8 fan_min[2]; /* Register value */ | |
166 | u8 fan_div[2]; /* Register encoding, shifted right */ | |
167 | u16 alarms; /* Register encoding */ | |
168 | u8 uch_config; | |
169 | }; | |
170 | ||
171 | static struct pci_dev *s_bridge; | |
ec5e1a4b | 172 | static int vt8231_probe(struct platform_device *pdev); |
d0546128 | 173 | static int __devexit vt8231_remove(struct platform_device *pdev); |
1de9e371 | 174 | static struct vt8231_data *vt8231_update_device(struct device *dev); |
ec5e1a4b | 175 | static void vt8231_init_device(struct vt8231_data *data); |
1de9e371 | 176 | |
ec5e1a4b | 177 | static inline int vt8231_read_value(struct vt8231_data *data, u8 reg) |
1de9e371 | 178 | { |
ec5e1a4b | 179 | return inb_p(data->addr + reg); |
1de9e371 RL |
180 | } |
181 | ||
ec5e1a4b | 182 | static inline void vt8231_write_value(struct vt8231_data *data, u8 reg, |
1de9e371 RL |
183 | u8 value) |
184 | { | |
ec5e1a4b | 185 | outb_p(value, data->addr + reg); |
1de9e371 RL |
186 | } |
187 | ||
188 | /* following are the sysfs callback functions */ | |
189 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, | |
190 | char *buf) | |
191 | { | |
192 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
193 | int nr = sensor_attr->index; | |
194 | struct vt8231_data *data = vt8231_update_device(dev); | |
195 | ||
196 | return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958); | |
197 | } | |
198 | ||
199 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, | |
200 | char *buf) | |
201 | { | |
202 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
203 | int nr = sensor_attr->index; | |
204 | struct vt8231_data *data = vt8231_update_device(dev); | |
205 | ||
206 | return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958); | |
207 | } | |
208 | ||
209 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, | |
210 | char *buf) | |
211 | { | |
212 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
213 | int nr = sensor_attr->index; | |
214 | struct vt8231_data *data = vt8231_update_device(dev); | |
215 | ||
216 | return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958)); | |
217 | } | |
218 | ||
219 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, | |
220 | const char *buf, size_t count) | |
221 | { | |
222 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
223 | int nr = sensor_attr->index; | |
ec5e1a4b | 224 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
225 | unsigned long val; |
226 | int err; | |
227 | ||
228 | err = kstrtoul(buf, 10, &val); | |
229 | if (err) | |
230 | return err; | |
1de9e371 | 231 | |
9a61bf63 | 232 | mutex_lock(&data->update_lock); |
1de9e371 | 233 | data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255); |
ec5e1a4b | 234 | vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]); |
9a61bf63 | 235 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
236 | return count; |
237 | } | |
238 | ||
239 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, | |
240 | const char *buf, size_t count) | |
241 | { | |
242 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
243 | int nr = sensor_attr->index; | |
ec5e1a4b | 244 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
245 | unsigned long val; |
246 | int err; | |
247 | ||
248 | err = kstrtoul(buf, 10, &val); | |
249 | if (err) | |
250 | return err; | |
1de9e371 | 251 | |
9a61bf63 | 252 | mutex_lock(&data->update_lock); |
1de9e371 | 253 | data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255); |
ec5e1a4b | 254 | vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]); |
9a61bf63 | 255 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
256 | return count; |
257 | } | |
258 | ||
259 | /* Special case for input 5 as this has 3.3V scaling built into the chip */ | |
260 | static ssize_t show_in5(struct device *dev, struct device_attribute *attr, | |
261 | char *buf) | |
262 | { | |
263 | struct vt8231_data *data = vt8231_update_device(dev); | |
264 | ||
265 | return sprintf(buf, "%d\n", | |
266 | (((data->in[5] - 3) * 10000 * 54) / (958 * 34))); | |
267 | } | |
268 | ||
269 | static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr, | |
270 | char *buf) | |
271 | { | |
272 | struct vt8231_data *data = vt8231_update_device(dev); | |
273 | ||
274 | return sprintf(buf, "%d\n", | |
275 | (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34))); | |
276 | } | |
277 | ||
278 | static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr, | |
279 | char *buf) | |
280 | { | |
281 | struct vt8231_data *data = vt8231_update_device(dev); | |
282 | ||
283 | return sprintf(buf, "%d\n", | |
284 | (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34))); | |
285 | } | |
286 | ||
287 | static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr, | |
288 | const char *buf, size_t count) | |
289 | { | |
ec5e1a4b | 290 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
291 | unsigned long val; |
292 | int err; | |
293 | ||
294 | err = kstrtoul(buf, 10, &val); | |
295 | if (err) | |
296 | return err; | |
1de9e371 | 297 | |
9a61bf63 | 298 | mutex_lock(&data->update_lock); |
1de9e371 RL |
299 | data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3, |
300 | 0, 255); | |
ec5e1a4b | 301 | vt8231_write_value(data, regvoltmin[5], data->in_min[5]); |
9a61bf63 | 302 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
303 | return count; |
304 | } | |
305 | ||
306 | static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr, | |
307 | const char *buf, size_t count) | |
308 | { | |
ec5e1a4b | 309 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
310 | unsigned long val; |
311 | int err; | |
312 | ||
313 | err = kstrtoul(buf, 10, &val); | |
314 | if (err) | |
315 | return err; | |
1de9e371 | 316 | |
9a61bf63 | 317 | mutex_lock(&data->update_lock); |
1de9e371 RL |
318 | data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3, |
319 | 0, 255); | |
ec5e1a4b | 320 | vt8231_write_value(data, regvoltmax[5], data->in_max[5]); |
9a61bf63 | 321 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
322 | return count; |
323 | } | |
324 | ||
325 | #define define_voltage_sysfs(offset) \ | |
326 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ | |
327 | show_in, NULL, offset); \ | |
328 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ | |
329 | show_in_min, set_in_min, offset); \ | |
330 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ | |
331 | show_in_max, set_in_max, offset) | |
332 | ||
333 | define_voltage_sysfs(0); | |
334 | define_voltage_sysfs(1); | |
335 | define_voltage_sysfs(2); | |
336 | define_voltage_sysfs(3); | |
337 | define_voltage_sysfs(4); | |
338 | ||
339 | static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL); | |
340 | static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min); | |
341 | static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max); | |
342 | ||
343 | /* Temperatures */ | |
344 | static ssize_t show_temp0(struct device *dev, struct device_attribute *attr, | |
345 | char *buf) | |
346 | { | |
347 | struct vt8231_data *data = vt8231_update_device(dev); | |
348 | return sprintf(buf, "%d\n", data->temp[0] * 250); | |
349 | } | |
350 | ||
351 | static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr, | |
352 | char *buf) | |
353 | { | |
354 | struct vt8231_data *data = vt8231_update_device(dev); | |
355 | return sprintf(buf, "%d\n", data->temp_max[0] * 1000); | |
356 | } | |
357 | ||
358 | static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr, | |
359 | char *buf) | |
360 | { | |
361 | struct vt8231_data *data = vt8231_update_device(dev); | |
362 | return sprintf(buf, "%d\n", data->temp_min[0] * 1000); | |
363 | } | |
364 | ||
365 | static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr, | |
366 | const char *buf, size_t count) | |
367 | { | |
ec5e1a4b | 368 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
369 | long val; |
370 | int err; | |
371 | ||
372 | err = kstrtol(buf, 10, &val); | |
373 | if (err) | |
374 | return err; | |
1de9e371 | 375 | |
9a61bf63 | 376 | mutex_lock(&data->update_lock); |
1de9e371 | 377 | data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255); |
ec5e1a4b | 378 | vt8231_write_value(data, regtempmax[0], data->temp_max[0]); |
9a61bf63 | 379 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
380 | return count; |
381 | } | |
382 | static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr, | |
383 | const char *buf, size_t count) | |
384 | { | |
ec5e1a4b | 385 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
386 | long val; |
387 | int err; | |
388 | ||
389 | err = kstrtol(buf, 10, &val); | |
390 | if (err) | |
391 | return err; | |
1de9e371 | 392 | |
9a61bf63 | 393 | mutex_lock(&data->update_lock); |
1de9e371 | 394 | data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255); |
ec5e1a4b | 395 | vt8231_write_value(data, regtempmin[0], data->temp_min[0]); |
9a61bf63 | 396 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
397 | return count; |
398 | } | |
399 | ||
400 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, | |
401 | char *buf) | |
402 | { | |
403 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
404 | int nr = sensor_attr->index; | |
405 | struct vt8231_data *data = vt8231_update_device(dev); | |
406 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); | |
407 | } | |
408 | ||
409 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, | |
410 | char *buf) | |
411 | { | |
412 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
413 | int nr = sensor_attr->index; | |
414 | struct vt8231_data *data = vt8231_update_device(dev); | |
415 | return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr])); | |
416 | } | |
417 | ||
418 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, | |
419 | char *buf) | |
420 | { | |
421 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
422 | int nr = sensor_attr->index; | |
423 | struct vt8231_data *data = vt8231_update_device(dev); | |
424 | return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr])); | |
425 | } | |
426 | ||
427 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | |
428 | const char *buf, size_t count) | |
429 | { | |
430 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
431 | int nr = sensor_attr->index; | |
ec5e1a4b | 432 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
433 | long val; |
434 | int err; | |
435 | ||
436 | err = kstrtol(buf, 10, &val); | |
437 | if (err) | |
438 | return err; | |
1de9e371 | 439 | |
9a61bf63 | 440 | mutex_lock(&data->update_lock); |
1de9e371 | 441 | data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255); |
ec5e1a4b | 442 | vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]); |
9a61bf63 | 443 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
444 | return count; |
445 | } | |
446 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, | |
447 | const char *buf, size_t count) | |
448 | { | |
449 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
450 | int nr = sensor_attr->index; | |
ec5e1a4b | 451 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
452 | long val; |
453 | int err; | |
454 | ||
455 | err = kstrtol(buf, 10, &val); | |
456 | if (err) | |
457 | return err; | |
1de9e371 | 458 | |
9a61bf63 | 459 | mutex_lock(&data->update_lock); |
1de9e371 | 460 | data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255); |
ec5e1a4b | 461 | vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]); |
9a61bf63 | 462 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
463 | return count; |
464 | } | |
465 | ||
466 | /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA | |
467 | ** temperature sensor numbering (0-5) | |
468 | */ | |
469 | #define define_temperature_sysfs(offset) \ | |
470 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ | |
471 | show_temp, NULL, offset - 1); \ | |
472 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ | |
473 | show_temp_max, set_temp_max, offset - 1); \ | |
e3efa5a7 | 474 | static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \ |
1de9e371 RL |
475 | show_temp_min, set_temp_min, offset - 1) |
476 | ||
477 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL); | |
478 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max); | |
65fe5c79 GR |
479 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, |
480 | set_temp0_min); | |
1de9e371 RL |
481 | |
482 | define_temperature_sysfs(2); | |
483 | define_temperature_sysfs(3); | |
484 | define_temperature_sysfs(4); | |
485 | define_temperature_sysfs(5); | |
486 | define_temperature_sysfs(6); | |
487 | ||
1de9e371 RL |
488 | /* Fans */ |
489 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, | |
490 | char *buf) | |
491 | { | |
492 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
493 | int nr = sensor_attr->index; | |
494 | struct vt8231_data *data = vt8231_update_device(dev); | |
495 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], | |
496 | DIV_FROM_REG(data->fan_div[nr]))); | |
497 | } | |
498 | ||
499 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, | |
500 | char *buf) | |
501 | { | |
502 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
503 | int nr = sensor_attr->index; | |
504 | struct vt8231_data *data = vt8231_update_device(dev); | |
505 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], | |
506 | DIV_FROM_REG(data->fan_div[nr]))); | |
507 | } | |
508 | ||
509 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, | |
510 | char *buf) | |
511 | { | |
512 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
513 | int nr = sensor_attr->index; | |
514 | struct vt8231_data *data = vt8231_update_device(dev); | |
515 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); | |
516 | } | |
517 | ||
518 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, | |
519 | const char *buf, size_t count) | |
520 | { | |
521 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); | |
522 | int nr = sensor_attr->index; | |
ec5e1a4b | 523 | struct vt8231_data *data = dev_get_drvdata(dev); |
65fe5c79 GR |
524 | unsigned long val; |
525 | int err; | |
526 | ||
527 | err = kstrtoul(buf, 10, &val); | |
528 | if (err) | |
529 | return err; | |
1de9e371 | 530 | |
9a61bf63 | 531 | mutex_lock(&data->update_lock); |
1de9e371 | 532 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
ec5e1a4b | 533 | vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]); |
9a61bf63 | 534 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
535 | return count; |
536 | } | |
537 | ||
538 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, | |
539 | const char *buf, size_t count) | |
540 | { | |
ec5e1a4b | 541 | struct vt8231_data *data = dev_get_drvdata(dev); |
1de9e371 | 542 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
65fe5c79 | 543 | unsigned long val; |
1de9e371 | 544 | int nr = sensor_attr->index; |
ec5e1a4b | 545 | int old = vt8231_read_value(data, VT8231_REG_FANDIV); |
1de9e371 RL |
546 | long min = FAN_FROM_REG(data->fan_min[nr], |
547 | DIV_FROM_REG(data->fan_div[nr])); | |
65fe5c79 GR |
548 | int err; |
549 | ||
550 | err = kstrtoul(buf, 10, &val); | |
551 | if (err) | |
552 | return err; | |
1de9e371 | 553 | |
9a61bf63 | 554 | mutex_lock(&data->update_lock); |
1de9e371 | 555 | switch (val) { |
65fe5c79 GR |
556 | case 1: |
557 | data->fan_div[nr] = 0; | |
558 | break; | |
559 | case 2: | |
560 | data->fan_div[nr] = 1; | |
561 | break; | |
562 | case 4: | |
563 | data->fan_div[nr] = 2; | |
564 | break; | |
565 | case 8: | |
566 | data->fan_div[nr] = 3; | |
567 | break; | |
1de9e371 | 568 | default: |
b20ff13a | 569 | dev_err(dev, "fan_div value %ld not supported. " |
65fe5c79 | 570 | "Choose one of 1, 2, 4 or 8!\n", val); |
9a61bf63 | 571 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
572 | return -EINVAL; |
573 | } | |
574 | ||
575 | /* Correct the fan minimum speed */ | |
576 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); | |
ec5e1a4b | 577 | vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]); |
1de9e371 RL |
578 | |
579 | old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4); | |
ec5e1a4b | 580 | vt8231_write_value(data, VT8231_REG_FANDIV, old); |
9a61bf63 | 581 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
582 | return count; |
583 | } | |
584 | ||
585 | ||
586 | #define define_fan_sysfs(offset) \ | |
587 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ | |
588 | show_fan, NULL, offset - 1); \ | |
589 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ | |
590 | show_fan_div, set_fan_div, offset - 1); \ | |
591 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ | |
592 | show_fan_min, set_fan_min, offset - 1) | |
593 | ||
594 | define_fan_sysfs(1); | |
595 | define_fan_sysfs(2); | |
596 | ||
597 | /* Alarms */ | |
598 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, | |
599 | char *buf) | |
600 | { | |
601 | struct vt8231_data *data = vt8231_update_device(dev); | |
602 | return sprintf(buf, "%d\n", data->alarms); | |
603 | } | |
1de9e371 RL |
604 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
605 | ||
2d1374ca JD |
606 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
607 | char *buf) | |
608 | { | |
609 | int bitnr = to_sensor_dev_attr(attr)->index; | |
610 | struct vt8231_data *data = vt8231_update_device(dev); | |
611 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
612 | } | |
613 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); | |
614 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11); | |
615 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0); | |
616 | static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1); | |
617 | static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3); | |
618 | static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8); | |
619 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11); | |
620 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0); | |
621 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1); | |
622 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); | |
623 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); | |
624 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2); | |
625 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); | |
626 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); | |
627 | ||
ec5e1a4b RL |
628 | static ssize_t show_name(struct device *dev, struct device_attribute |
629 | *devattr, char *buf) | |
630 | { | |
631 | struct vt8231_data *data = dev_get_drvdata(dev); | |
632 | return sprintf(buf, "%s\n", data->name); | |
633 | } | |
634 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | |
635 | ||
2d1374ca | 636 | static struct attribute *vt8231_attributes_temps[6][5] = { |
cbeeb5b7 RL |
637 | { |
638 | &dev_attr_temp1_input.attr, | |
639 | &dev_attr_temp1_max_hyst.attr, | |
640 | &dev_attr_temp1_max.attr, | |
2d1374ca | 641 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
cbeeb5b7 RL |
642 | NULL |
643 | }, { | |
644 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
645 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, | |
646 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
2d1374ca | 647 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
cbeeb5b7 RL |
648 | NULL |
649 | }, { | |
650 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
651 | &sensor_dev_attr_temp3_max_hyst.dev_attr.attr, | |
652 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
2d1374ca | 653 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
cbeeb5b7 RL |
654 | NULL |
655 | }, { | |
656 | &sensor_dev_attr_temp4_input.dev_attr.attr, | |
657 | &sensor_dev_attr_temp4_max_hyst.dev_attr.attr, | |
658 | &sensor_dev_attr_temp4_max.dev_attr.attr, | |
2d1374ca | 659 | &sensor_dev_attr_temp4_alarm.dev_attr.attr, |
cbeeb5b7 RL |
660 | NULL |
661 | }, { | |
662 | &sensor_dev_attr_temp5_input.dev_attr.attr, | |
663 | &sensor_dev_attr_temp5_max_hyst.dev_attr.attr, | |
664 | &sensor_dev_attr_temp5_max.dev_attr.attr, | |
2d1374ca | 665 | &sensor_dev_attr_temp5_alarm.dev_attr.attr, |
cbeeb5b7 RL |
666 | NULL |
667 | }, { | |
668 | &sensor_dev_attr_temp6_input.dev_attr.attr, | |
669 | &sensor_dev_attr_temp6_max_hyst.dev_attr.attr, | |
670 | &sensor_dev_attr_temp6_max.dev_attr.attr, | |
2d1374ca | 671 | &sensor_dev_attr_temp6_alarm.dev_attr.attr, |
cbeeb5b7 RL |
672 | NULL |
673 | } | |
674 | }; | |
675 | ||
676 | static const struct attribute_group vt8231_group_temps[6] = { | |
677 | { .attrs = vt8231_attributes_temps[0] }, | |
678 | { .attrs = vt8231_attributes_temps[1] }, | |
679 | { .attrs = vt8231_attributes_temps[2] }, | |
680 | { .attrs = vt8231_attributes_temps[3] }, | |
681 | { .attrs = vt8231_attributes_temps[4] }, | |
682 | { .attrs = vt8231_attributes_temps[5] }, | |
683 | }; | |
684 | ||
2d1374ca | 685 | static struct attribute *vt8231_attributes_volts[6][5] = { |
cbeeb5b7 RL |
686 | { |
687 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
688 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
689 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
2d1374ca | 690 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
cbeeb5b7 RL |
691 | NULL |
692 | }, { | |
693 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
694 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
695 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
2d1374ca | 696 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
cbeeb5b7 RL |
697 | NULL |
698 | }, { | |
699 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
700 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
701 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
2d1374ca | 702 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
cbeeb5b7 RL |
703 | NULL |
704 | }, { | |
705 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
706 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
707 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
2d1374ca | 708 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
cbeeb5b7 RL |
709 | NULL |
710 | }, { | |
711 | &sensor_dev_attr_in4_input.dev_attr.attr, | |
712 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
713 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
2d1374ca | 714 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
cbeeb5b7 RL |
715 | NULL |
716 | }, { | |
717 | &dev_attr_in5_input.attr, | |
718 | &dev_attr_in5_min.attr, | |
719 | &dev_attr_in5_max.attr, | |
2d1374ca | 720 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
cbeeb5b7 RL |
721 | NULL |
722 | } | |
723 | }; | |
724 | ||
725 | static const struct attribute_group vt8231_group_volts[6] = { | |
726 | { .attrs = vt8231_attributes_volts[0] }, | |
727 | { .attrs = vt8231_attributes_volts[1] }, | |
728 | { .attrs = vt8231_attributes_volts[2] }, | |
729 | { .attrs = vt8231_attributes_volts[3] }, | |
730 | { .attrs = vt8231_attributes_volts[4] }, | |
731 | { .attrs = vt8231_attributes_volts[5] }, | |
732 | }; | |
733 | ||
734 | static struct attribute *vt8231_attributes[] = { | |
735 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
736 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
737 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
738 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
739 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
740 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
2d1374ca JD |
741 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
742 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
cbeeb5b7 | 743 | &dev_attr_alarms.attr, |
ec5e1a4b | 744 | &dev_attr_name.attr, |
cbeeb5b7 RL |
745 | NULL |
746 | }; | |
747 | ||
748 | static const struct attribute_group vt8231_group = { | |
749 | .attrs = vt8231_attributes, | |
750 | }; | |
751 | ||
ec5e1a4b | 752 | static struct platform_driver vt8231_driver = { |
cdaf7934 | 753 | .driver = { |
87218842 | 754 | .owner = THIS_MODULE, |
cdaf7934 LR |
755 | .name = "vt8231", |
756 | }, | |
ec5e1a4b RL |
757 | .probe = vt8231_probe, |
758 | .remove = __devexit_p(vt8231_remove), | |
1de9e371 RL |
759 | }; |
760 | ||
600151b9 | 761 | static DEFINE_PCI_DEVICE_TABLE(vt8231_pci_ids) = { |
1de9e371 RL |
762 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) }, |
763 | { 0, } | |
764 | }; | |
765 | ||
766 | MODULE_DEVICE_TABLE(pci, vt8231_pci_ids); | |
767 | ||
768 | static int __devinit vt8231_pci_probe(struct pci_dev *dev, | |
65fe5c79 | 769 | const struct pci_device_id *id); |
1de9e371 RL |
770 | |
771 | static struct pci_driver vt8231_pci_driver = { | |
772 | .name = "vt8231", | |
773 | .id_table = vt8231_pci_ids, | |
774 | .probe = vt8231_pci_probe, | |
775 | }; | |
776 | ||
a022fef5 | 777 | static int vt8231_probe(struct platform_device *pdev) |
1de9e371 | 778 | { |
ec5e1a4b | 779 | struct resource *res; |
1de9e371 RL |
780 | struct vt8231_data *data; |
781 | int err = 0, i; | |
1de9e371 RL |
782 | |
783 | /* Reserve the ISA region */ | |
ec5e1a4b RL |
784 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
785 | if (!request_region(res->start, VT8231_EXTENT, | |
786 | vt8231_driver.driver.name)) { | |
787 | dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n", | |
788 | (unsigned long)res->start, (unsigned long)res->end); | |
1de9e371 RL |
789 | return -ENODEV; |
790 | } | |
791 | ||
65fe5c79 GR |
792 | data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL); |
793 | if (!data) { | |
1de9e371 RL |
794 | err = -ENOMEM; |
795 | goto exit_release; | |
796 | } | |
797 | ||
ec5e1a4b RL |
798 | platform_set_drvdata(pdev, data); |
799 | data->addr = res->start; | |
800 | data->name = "vt8231"; | |
1de9e371 | 801 | |
9a61bf63 | 802 | mutex_init(&data->update_lock); |
ec5e1a4b | 803 | vt8231_init_device(data); |
1de9e371 RL |
804 | |
805 | /* Register sysfs hooks */ | |
65fe5c79 GR |
806 | err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group); |
807 | if (err) | |
ec5e1a4b | 808 | goto exit_free; |
1de9e371 RL |
809 | |
810 | /* Must update device information to find out the config field */ | |
ec5e1a4b | 811 | data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG); |
1de9e371 | 812 | |
cbeeb5b7 | 813 | for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) { |
1de9e371 | 814 | if (ISTEMP(i, data->uch_config)) { |
65fe5c79 GR |
815 | err = sysfs_create_group(&pdev->dev.kobj, |
816 | &vt8231_group_temps[i]); | |
817 | if (err) | |
cbeeb5b7 | 818 | goto exit_remove_files; |
1de9e371 RL |
819 | } |
820 | } | |
821 | ||
cbeeb5b7 | 822 | for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) { |
1de9e371 | 823 | if (ISVOLT(i, data->uch_config)) { |
65fe5c79 GR |
824 | err = sysfs_create_group(&pdev->dev.kobj, |
825 | &vt8231_group_volts[i]); | |
826 | if (err) | |
cbeeb5b7 | 827 | goto exit_remove_files; |
1de9e371 RL |
828 | } |
829 | } | |
830 | ||
1beeffe4 TJ |
831 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
832 | if (IS_ERR(data->hwmon_dev)) { | |
833 | err = PTR_ERR(data->hwmon_dev); | |
cbeeb5b7 RL |
834 | goto exit_remove_files; |
835 | } | |
1de9e371 RL |
836 | return 0; |
837 | ||
cbeeb5b7 RL |
838 | exit_remove_files: |
839 | for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) | |
ec5e1a4b | 840 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]); |
cbeeb5b7 RL |
841 | |
842 | for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) | |
ec5e1a4b RL |
843 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]); |
844 | ||
845 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group); | |
cbeeb5b7 | 846 | |
1de9e371 | 847 | exit_free: |
04a6217d | 848 | platform_set_drvdata(pdev, NULL); |
1de9e371 | 849 | kfree(data); |
ec5e1a4b | 850 | |
1de9e371 | 851 | exit_release: |
ec5e1a4b | 852 | release_region(res->start, VT8231_EXTENT); |
1de9e371 RL |
853 | return err; |
854 | } | |
855 | ||
d0546128 | 856 | static int __devexit vt8231_remove(struct platform_device *pdev) |
1de9e371 | 857 | { |
ec5e1a4b RL |
858 | struct vt8231_data *data = platform_get_drvdata(pdev); |
859 | int i; | |
1de9e371 | 860 | |
1beeffe4 | 861 | hwmon_device_unregister(data->hwmon_dev); |
1de9e371 | 862 | |
cbeeb5b7 | 863 | for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) |
ec5e1a4b | 864 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]); |
cbeeb5b7 RL |
865 | |
866 | for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) | |
ec5e1a4b | 867 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]); |
cbeeb5b7 | 868 | |
ec5e1a4b | 869 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group); |
cbeeb5b7 | 870 | |
ec5e1a4b RL |
871 | release_region(data->addr, VT8231_EXTENT); |
872 | platform_set_drvdata(pdev, NULL); | |
1de9e371 | 873 | kfree(data); |
1de9e371 RL |
874 | return 0; |
875 | } | |
876 | ||
ec5e1a4b | 877 | static void vt8231_init_device(struct vt8231_data *data) |
1de9e371 | 878 | { |
ec5e1a4b RL |
879 | vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0); |
880 | vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0); | |
1de9e371 RL |
881 | } |
882 | ||
883 | static struct vt8231_data *vt8231_update_device(struct device *dev) | |
884 | { | |
ec5e1a4b | 885 | struct vt8231_data *data = dev_get_drvdata(dev); |
1de9e371 RL |
886 | int i; |
887 | u16 low; | |
888 | ||
9a61bf63 | 889 | mutex_lock(&data->update_lock); |
1de9e371 RL |
890 | |
891 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
892 | || !data->valid) { | |
893 | for (i = 0; i < 6; i++) { | |
894 | if (ISVOLT(i, data->uch_config)) { | |
ec5e1a4b | 895 | data->in[i] = vt8231_read_value(data, |
1de9e371 | 896 | regvolt[i]); |
ec5e1a4b | 897 | data->in_min[i] = vt8231_read_value(data, |
1de9e371 | 898 | regvoltmin[i]); |
ec5e1a4b | 899 | data->in_max[i] = vt8231_read_value(data, |
1de9e371 RL |
900 | regvoltmax[i]); |
901 | } | |
902 | } | |
903 | for (i = 0; i < 2; i++) { | |
ec5e1a4b | 904 | data->fan[i] = vt8231_read_value(data, |
1de9e371 | 905 | VT8231_REG_FAN(i)); |
ec5e1a4b | 906 | data->fan_min[i] = vt8231_read_value(data, |
1de9e371 RL |
907 | VT8231_REG_FAN_MIN(i)); |
908 | } | |
909 | ||
ec5e1a4b | 910 | low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01); |
1de9e371 | 911 | low = (low >> 6) | ((low & 0x30) >> 2) |
ec5e1a4b | 912 | | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4); |
1de9e371 RL |
913 | for (i = 0; i < 6; i++) { |
914 | if (ISTEMP(i, data->uch_config)) { | |
ec5e1a4b | 915 | data->temp[i] = (vt8231_read_value(data, |
1de9e371 RL |
916 | regtemp[i]) << 2) |
917 | | ((low >> (2 * i)) & 0x03); | |
ec5e1a4b | 918 | data->temp_max[i] = vt8231_read_value(data, |
1de9e371 | 919 | regtempmax[i]); |
ec5e1a4b | 920 | data->temp_min[i] = vt8231_read_value(data, |
1de9e371 RL |
921 | regtempmin[i]); |
922 | } | |
923 | } | |
924 | ||
ec5e1a4b | 925 | i = vt8231_read_value(data, VT8231_REG_FANDIV); |
1de9e371 RL |
926 | data->fan_div[0] = (i >> 4) & 0x03; |
927 | data->fan_div[1] = i >> 6; | |
ec5e1a4b RL |
928 | data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) | |
929 | (vt8231_read_value(data, VT8231_REG_ALARM2) << 8); | |
1de9e371 RL |
930 | |
931 | /* Set alarm flags correctly */ | |
65fe5c79 | 932 | if (!data->fan[0] && data->fan_min[0]) |
1de9e371 | 933 | data->alarms |= 0x40; |
65fe5c79 | 934 | else if (data->fan[0] && !data->fan_min[0]) |
1de9e371 | 935 | data->alarms &= ~0x40; |
1de9e371 | 936 | |
65fe5c79 | 937 | if (!data->fan[1] && data->fan_min[1]) |
1de9e371 | 938 | data->alarms |= 0x80; |
65fe5c79 | 939 | else if (data->fan[1] && !data->fan_min[1]) |
1de9e371 | 940 | data->alarms &= ~0x80; |
1de9e371 RL |
941 | |
942 | data->last_updated = jiffies; | |
943 | data->valid = 1; | |
944 | } | |
945 | ||
9a61bf63 | 946 | mutex_unlock(&data->update_lock); |
1de9e371 RL |
947 | |
948 | return data; | |
949 | } | |
950 | ||
ec5e1a4b RL |
951 | static int __devinit vt8231_device_add(unsigned short address) |
952 | { | |
953 | struct resource res = { | |
954 | .start = address, | |
955 | .end = address + VT8231_EXTENT - 1, | |
956 | .name = "vt8231", | |
957 | .flags = IORESOURCE_IO, | |
958 | }; | |
959 | int err; | |
960 | ||
b9acb64a JD |
961 | err = acpi_check_resource_conflict(&res); |
962 | if (err) | |
963 | goto exit; | |
964 | ||
ec5e1a4b RL |
965 | pdev = platform_device_alloc("vt8231", address); |
966 | if (!pdev) { | |
967 | err = -ENOMEM; | |
9d72be0d | 968 | pr_err("Device allocation failed\n"); |
ec5e1a4b RL |
969 | goto exit; |
970 | } | |
971 | ||
972 | err = platform_device_add_resources(pdev, &res, 1); | |
973 | if (err) { | |
9d72be0d | 974 | pr_err("Device resource addition failed (%d)\n", err); |
ec5e1a4b RL |
975 | goto exit_device_put; |
976 | } | |
977 | ||
978 | err = platform_device_add(pdev); | |
979 | if (err) { | |
9d72be0d | 980 | pr_err("Device addition failed (%d)\n", err); |
ec5e1a4b RL |
981 | goto exit_device_put; |
982 | } | |
983 | ||
984 | return 0; | |
985 | ||
986 | exit_device_put: | |
987 | platform_device_put(pdev); | |
988 | exit: | |
989 | return err; | |
990 | } | |
991 | ||
1de9e371 RL |
992 | static int __devinit vt8231_pci_probe(struct pci_dev *dev, |
993 | const struct pci_device_id *id) | |
994 | { | |
ec5e1a4b RL |
995 | u16 address, val; |
996 | if (force_addr) { | |
997 | address = force_addr & 0xff00; | |
998 | dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", | |
999 | address); | |
1000 | ||
1001 | if (PCIBIOS_SUCCESSFUL != | |
1002 | pci_write_config_word(dev, VT8231_BASE_REG, address | 1)) | |
1003 | return -ENODEV; | |
1004 | } | |
1de9e371 RL |
1005 | |
1006 | if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG, | |
1007 | &val)) | |
1008 | return -ENODEV; | |
1009 | ||
ec5e1a4b RL |
1010 | address = val & ~(VT8231_EXTENT - 1); |
1011 | if (address == 0) { | |
4cae7878 | 1012 | dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n"); |
1de9e371 RL |
1013 | return -ENODEV; |
1014 | } | |
1015 | ||
ec5e1a4b RL |
1016 | if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG, |
1017 | &val)) | |
1018 | return -ENODEV; | |
1de9e371 | 1019 | |
ec5e1a4b RL |
1020 | if (!(val & 0x0001)) { |
1021 | dev_warn(&dev->dev, "enabling sensors\n"); | |
1022 | if (PCIBIOS_SUCCESSFUL != | |
1023 | pci_write_config_word(dev, VT8231_ENABLE_REG, | |
1024 | val | 0x0001)) | |
1025 | return -ENODEV; | |
1de9e371 RL |
1026 | } |
1027 | ||
ec5e1a4b RL |
1028 | if (platform_driver_register(&vt8231_driver)) |
1029 | goto exit; | |
1030 | ||
1031 | /* Sets global pdev as a side effect */ | |
1032 | if (vt8231_device_add(address)) | |
1033 | goto exit_unregister; | |
1034 | ||
1de9e371 RL |
1035 | /* Always return failure here. This is to allow other drivers to bind |
1036 | * to this pci device. We don't really want to have control over the | |
1037 | * pci device, we only wanted to read as few register values from it. | |
1038 | */ | |
ec5e1a4b RL |
1039 | |
1040 | /* We do, however, mark ourselves as using the PCI device to stop it | |
1041 | getting unloaded. */ | |
1042 | s_bridge = pci_dev_get(dev); | |
1043 | return -ENODEV; | |
1044 | ||
1045 | exit_unregister: | |
1046 | platform_driver_unregister(&vt8231_driver); | |
1047 | exit: | |
1de9e371 RL |
1048 | return -ENODEV; |
1049 | } | |
1050 | ||
1051 | static int __init sm_vt8231_init(void) | |
1052 | { | |
93b47684 | 1053 | return pci_register_driver(&vt8231_pci_driver); |
1de9e371 RL |
1054 | } |
1055 | ||
1056 | static void __exit sm_vt8231_exit(void) | |
1057 | { | |
1058 | pci_unregister_driver(&vt8231_pci_driver); | |
1059 | if (s_bridge != NULL) { | |
ec5e1a4b RL |
1060 | platform_device_unregister(pdev); |
1061 | platform_driver_unregister(&vt8231_driver); | |
1de9e371 RL |
1062 | pci_dev_put(s_bridge); |
1063 | s_bridge = NULL; | |
1064 | } | |
1065 | } | |
1066 | ||
af865765 | 1067 | MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>"); |
1de9e371 RL |
1068 | MODULE_DESCRIPTION("VT8231 sensors"); |
1069 | MODULE_LICENSE("GPL"); | |
1070 | ||
1071 | module_init(sm_vt8231_init); | |
1072 | module_exit(sm_vt8231_exit); |