aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/vt1211.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/vt1211.c')
-rw-r--r--drivers/hwmon/vt1211.c1355
1 files changed, 1355 insertions, 0 deletions
diff --git a/drivers/hwmon/vt1211.c b/drivers/hwmon/vt1211.c
new file mode 100644
index 000000000000..25cc56003d7a
--- /dev/null
+++ b/drivers/hwmon/vt1211.c
@@ -0,0 +1,1355 @@
1/*
2 * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
3 * monitoring features
4 * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
5 *
6 * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
7 * and its port to kernel 2.6 by Lars Ekman.
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#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/platform_device.h>
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31#include <linux/hwmon-vid.h>
32#include <linux/err.h>
33#include <linux/mutex.h>
34#include <asm/io.h>
35
36static int uch_config = -1;
37module_param(uch_config, int, 0);
38MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
39
40static int int_mode = -1;
41module_param(int_mode, int, 0);
42MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
43
44static struct platform_device *pdev;
45
46#define DRVNAME "vt1211"
47
48/* ---------------------------------------------------------------------
49 * Registers
50 *
51 * The sensors are defined as follows.
52 *
53 * Sensor Voltage Mode Temp Mode Notes (from the datasheet)
54 * -------- ------------ --------- --------------------------
55 * Reading 1 temp1 Intel thermal diode
56 * Reading 3 temp2 Internal thermal diode
57 * UCH1/Reading2 in0 temp3 NTC type thermistor
58 * UCH2 in1 temp4 +2.5V
59 * UCH3 in2 temp5 VccP
60 * UCH4 in3 temp6 +5V
61 * UCH5 in4 temp7 +12V
62 * 3.3V in5 Internal VDD (+3.3V)
63 *
64 * --------------------------------------------------------------------- */
65
66/* Voltages (in) numbered 0-5 (ix) */
67#define VT1211_REG_IN(ix) (0x21 + (ix))
68#define VT1211_REG_IN_MIN(ix) ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
69#define VT1211_REG_IN_MAX(ix) ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
70
71/* Temperatures (temp) numbered 0-6 (ix) */
72static u8 regtemp[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
73static u8 regtempmax[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
74static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
75
76/* Fans numbered 0-1 (ix) */
77#define VT1211_REG_FAN(ix) (0x29 + (ix))
78#define VT1211_REG_FAN_MIN(ix) (0x3b + (ix))
79#define VT1211_REG_FAN_DIV 0x47
80
81/* PWMs numbered 0-1 (ix) */
82/* Auto points numbered 0-3 (ap) */
83#define VT1211_REG_PWM(ix) (0x60 + (ix))
84#define VT1211_REG_PWM_CLK 0x50
85#define VT1211_REG_PWM_CTL 0x51
86#define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap))
87#define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
88
89/* Miscellaneous registers */
90#define VT1211_REG_CONFIG 0x40
91#define VT1211_REG_ALARM1 0x41
92#define VT1211_REG_ALARM2 0x42
93#define VT1211_REG_VID 0x45
94#define VT1211_REG_UCH_CONFIG 0x4a
95#define VT1211_REG_TEMP1_CONFIG 0x4b
96#define VT1211_REG_TEMP2_CONFIG 0x4c
97
98/* In, temp & fan alarm bits */
99static const u8 bitalarmin[] = {11, 0, 1, 3, 8, 2, 9};
100static const u8 bitalarmtemp[] = {4, 15, 11, 0, 1, 3, 8};
101static const u8 bitalarmfan[] = {6, 7};
102
103/* ---------------------------------------------------------------------
104 * Data structures and manipulation thereof
105 * --------------------------------------------------------------------- */
106
107struct vt1211_data {
108 unsigned short addr;
109 const char *name;
110 struct class_device *class_dev;
111
112 struct mutex update_lock;
113 char valid; /* !=0 if following fields are valid */
114 unsigned long last_updated; /* In jiffies */
115
116 /* Register values */
117 u8 in[6];
118 u8 in_max[6];
119 u8 in_min[6];
120 u8 temp[7];
121 u8 temp_max[7];
122 u8 temp_hyst[7];
123 u8 fan[2];
124 u8 fan_min[2];
125 u8 fan_div[2];
126 u8 fan_ctl;
127 u8 pwm[2];
128 u8 pwm_ctl[2];
129 u8 pwm_clk;
130 u8 pwm_auto_temp[4];
131 u8 pwm_auto_pwm[2][4];
132 u8 vid; /* Read once at init time */
133 u8 vrm;
134 u8 uch_config; /* Read once at init time */
135 u16 alarms;
136};
137
138/* ix = [0-5] */
139#define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \
140 !(((uch_config) >> ((ix) + 2)) & 1))
141
142/* ix = [0-6] */
143#define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \
144 ((uch_config) >> (ix)) & 1)
145
146/* in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
147 driver according to the VT1211 BIOS porting guide */
148#define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \
149 (((reg) - 3) * 15882 + 479) / 958 : \
150 (((reg) - 3) * 10000 + 479) / 958)
151#define IN_TO_REG(ix, val) (SENSORS_LIMIT((ix) == 5 ? \
152 ((val) * 958 + 7941) / 15882 + 3 : \
153 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
154
155/* temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
156 temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
157 according to some measurements that I took on an EPIA M10000.
158 temp3-7 are thermistor based so the driver returns the voltage measured at
159 the pin (range 0V - 2.2V). */
160#define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \
161 (ix) == 1 ? (reg) < 51 ? 0 : \
162 ((reg) - 51) * 1000 : \
163 ((253 - (reg)) * 2200 + 105) / 210)
164#define TEMP_TO_REG(ix, val) SENSORS_LIMIT( \
165 ((ix) == 0 ? ((val) + 500) / 1000 : \
166 (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
167 253 - ((val) * 210 + 1100) / 2200), 0, 255)
168
169#define DIV_FROM_REG(reg) (1 << (reg))
170
171#define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \
172 1310720 / (reg) / DIV_FROM_REG(div))
173#define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \
174 SENSORS_LIMIT((1310720 / (val) / \
175 DIV_FROM_REG(div)), 1, 254))
176
177/* ---------------------------------------------------------------------
178 * Super-I/O constants and functions
179 * --------------------------------------------------------------------- */
180
181/* Configuration & data index port registers */
182#define SIO_REG_CIP 0x2e
183#define SIO_REG_DIP 0x2f
184
185/* Configuration registers */
186#define SIO_VT1211_LDN 0x07 /* logical device number */
187#define SIO_VT1211_DEVID 0x20 /* device ID */
188#define SIO_VT1211_DEVREV 0x21 /* device revision */
189#define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
190#define SIO_VT1211_BADDR 0x60 /* base I/O address */
191#define SIO_VT1211_ID 0x3c /* VT1211 device ID */
192
193/* VT1211 logical device numbers */
194#define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
195
196static inline void superio_outb(int reg, int val)
197{
198 outb(reg, SIO_REG_CIP);
199 outb(val, SIO_REG_DIP);
200}
201
202static inline int superio_inb(int reg)
203{
204 outb(reg, SIO_REG_CIP);
205 return inb(SIO_REG_DIP);
206}
207
208static inline void superio_select(int ldn)
209{
210 outb(SIO_VT1211_LDN, SIO_REG_CIP);
211 outb(ldn, SIO_REG_DIP);
212}
213
214static inline void superio_enter(void)
215{
216 outb(0x87, SIO_REG_CIP);
217 outb(0x87, SIO_REG_CIP);
218}
219
220static inline void superio_exit(void)
221{
222 outb(0xaa, SIO_REG_CIP);
223}
224
225/* ---------------------------------------------------------------------
226 * Device I/O access
227 * --------------------------------------------------------------------- */
228
229static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
230{
231 return inb(data->addr + reg);
232}
233
234static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
235{
236 outb(val, data->addr + reg);
237}
238
239static struct vt1211_data *vt1211_update_device(struct device *dev)
240{
241 struct vt1211_data *data = dev_get_drvdata(dev);
242 int ix, val;
243
244 mutex_lock(&data->update_lock);
245
246 /* registers cache is refreshed after 1 second */
247 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
248 /* read VID */
249 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
250
251 /* voltage (in) registers */
252 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
253 if (ISVOLT(ix, data->uch_config)) {
254 data->in[ix] = vt1211_read8(data,
255 VT1211_REG_IN(ix));
256 data->in_min[ix] = vt1211_read8(data,
257 VT1211_REG_IN_MIN(ix));
258 data->in_max[ix] = vt1211_read8(data,
259 VT1211_REG_IN_MAX(ix));
260 }
261 }
262
263 /* temp registers */
264 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
265 if (ISTEMP(ix, data->uch_config)) {
266 data->temp[ix] = vt1211_read8(data,
267 regtemp[ix]);
268 data->temp_max[ix] = vt1211_read8(data,
269 regtempmax[ix]);
270 data->temp_hyst[ix] = vt1211_read8(data,
271 regtemphyst[ix]);
272 }
273 }
274
275 /* fan & pwm registers */
276 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
277 data->fan[ix] = vt1211_read8(data,
278 VT1211_REG_FAN(ix));
279 data->fan_min[ix] = vt1211_read8(data,
280 VT1211_REG_FAN_MIN(ix));
281 data->pwm[ix] = vt1211_read8(data,
282 VT1211_REG_PWM(ix));
283 }
284 val = vt1211_read8(data, VT1211_REG_FAN_DIV);
285 data->fan_div[0] = (val >> 4) & 3;
286 data->fan_div[1] = (val >> 6) & 3;
287 data->fan_ctl = val & 0xf;
288
289 val = vt1211_read8(data, VT1211_REG_PWM_CTL);
290 data->pwm_ctl[0] = val & 0xf;
291 data->pwm_ctl[1] = (val >> 4) & 0xf;
292
293 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
294
295 /* pwm & temp auto point registers */
296 data->pwm_auto_pwm[0][1] = vt1211_read8(data,
297 VT1211_REG_PWM_AUTO_PWM(0, 1));
298 data->pwm_auto_pwm[0][2] = vt1211_read8(data,
299 VT1211_REG_PWM_AUTO_PWM(0, 2));
300 data->pwm_auto_pwm[1][1] = vt1211_read8(data,
301 VT1211_REG_PWM_AUTO_PWM(1, 1));
302 data->pwm_auto_pwm[1][2] = vt1211_read8(data,
303 VT1211_REG_PWM_AUTO_PWM(1, 2));
304 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
305 data->pwm_auto_temp[ix] = vt1211_read8(data,
306 VT1211_REG_PWM_AUTO_TEMP(ix));
307 }
308
309 /* alarm registers */
310 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
311 vt1211_read8(data, VT1211_REG_ALARM1);
312
313 data->last_updated = jiffies;
314 data->valid = 1;
315 }
316
317 mutex_unlock(&data->update_lock);
318
319 return data;
320}
321
322/* ---------------------------------------------------------------------
323 * Voltage sysfs interfaces
324 * ix = [0-5]
325 * --------------------------------------------------------------------- */
326
327#define SHOW_IN_INPUT 0
328#define SHOW_SET_IN_MIN 1
329#define SHOW_SET_IN_MAX 2
330#define SHOW_IN_ALARM 3
331
332static ssize_t show_in(struct device *dev, struct device_attribute *attr,
333 char *buf)
334{
335 struct vt1211_data *data = vt1211_update_device(dev);
336 struct sensor_device_attribute_2 *sensor_attr_2 =
337 to_sensor_dev_attr_2(attr);
338 int ix = sensor_attr_2->index;
339 int fn = sensor_attr_2->nr;
340 int res;
341
342 switch (fn) {
343 case SHOW_IN_INPUT:
344 res = IN_FROM_REG(ix, data->in[ix]);
345 break;
346 case SHOW_SET_IN_MIN:
347 res = IN_FROM_REG(ix, data->in_min[ix]);
348 break;
349 case SHOW_SET_IN_MAX:
350 res = IN_FROM_REG(ix, data->in_max[ix]);
351 break;
352 case SHOW_IN_ALARM:
353 res = (data->alarms >> bitalarmin[ix]) & 1;
354 break;
355 default:
356 res = 0;
357 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
358 }
359
360 return sprintf(buf, "%d\n", res);
361}
362
363static ssize_t set_in(struct device *dev, struct device_attribute *attr,
364 const char *buf, size_t count)
365{
366 struct vt1211_data *data = dev_get_drvdata(dev);
367 struct sensor_device_attribute_2 *sensor_attr_2 =
368 to_sensor_dev_attr_2(attr);
369 int ix = sensor_attr_2->index;
370 int fn = sensor_attr_2->nr;
371 long val = simple_strtol(buf, NULL, 10);
372
373 mutex_lock(&data->update_lock);
374 switch (fn) {
375 case SHOW_SET_IN_MIN:
376 data->in_min[ix] = IN_TO_REG(ix, val);
377 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
378 break;
379 case SHOW_SET_IN_MAX:
380 data->in_max[ix] = IN_TO_REG(ix, val);
381 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
382 break;
383 default:
384 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
385 }
386 mutex_unlock(&data->update_lock);
387
388 return count;
389}
390
391/* ---------------------------------------------------------------------
392 * Temperature sysfs interfaces
393 * ix = [0-6]
394 * --------------------------------------------------------------------- */
395
396#define SHOW_TEMP_INPUT 0
397#define SHOW_SET_TEMP_MAX 1
398#define SHOW_SET_TEMP_MAX_HYST 2
399#define SHOW_TEMP_ALARM 3
400
401static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
402 char *buf)
403{
404 struct vt1211_data *data = vt1211_update_device(dev);
405 struct sensor_device_attribute_2 *sensor_attr_2 =
406 to_sensor_dev_attr_2(attr);
407 int ix = sensor_attr_2->index;
408 int fn = sensor_attr_2->nr;
409 int res;
410
411 switch (fn) {
412 case SHOW_TEMP_INPUT:
413 res = TEMP_FROM_REG(ix, data->temp[ix]);
414 break;
415 case SHOW_SET_TEMP_MAX:
416 res = TEMP_FROM_REG(ix, data->temp_max[ix]);
417 break;
418 case SHOW_SET_TEMP_MAX_HYST:
419 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
420 break;
421 case SHOW_TEMP_ALARM:
422 res = (data->alarms >> bitalarmtemp[ix]) & 1;
423 break;
424 default:
425 res = 0;
426 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
427 }
428
429 return sprintf(buf, "%d\n", res);
430}
431
432static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
433 const char *buf, size_t count)
434{
435 struct vt1211_data *data = dev_get_drvdata(dev);
436 struct sensor_device_attribute_2 *sensor_attr_2 =
437 to_sensor_dev_attr_2(attr);
438 int ix = sensor_attr_2->index;
439 int fn = sensor_attr_2->nr;
440 long val = simple_strtol(buf, NULL, 10);
441
442 mutex_lock(&data->update_lock);
443 switch (fn) {
444 case SHOW_SET_TEMP_MAX:
445 data->temp_max[ix] = TEMP_TO_REG(ix, val);
446 vt1211_write8(data, regtempmax[ix],
447 data->temp_max[ix]);
448 break;
449 case SHOW_SET_TEMP_MAX_HYST:
450 data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
451 vt1211_write8(data, regtemphyst[ix],
452 data->temp_hyst[ix]);
453 break;
454 default:
455 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
456 }
457 mutex_unlock(&data->update_lock);
458
459 return count;
460}
461
462/* ---------------------------------------------------------------------
463 * Fan sysfs interfaces
464 * ix = [0-1]
465 * --------------------------------------------------------------------- */
466
467#define SHOW_FAN_INPUT 0
468#define SHOW_SET_FAN_MIN 1
469#define SHOW_SET_FAN_DIV 2
470#define SHOW_FAN_ALARM 3
471
472static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
473 char *buf)
474{
475 struct vt1211_data *data = vt1211_update_device(dev);
476 struct sensor_device_attribute_2 *sensor_attr_2 =
477 to_sensor_dev_attr_2(attr);
478 int ix = sensor_attr_2->index;
479 int fn = sensor_attr_2->nr;
480 int res;
481
482 switch (fn) {
483 case SHOW_FAN_INPUT:
484 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
485 break;
486 case SHOW_SET_FAN_MIN:
487 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
488 break;
489 case SHOW_SET_FAN_DIV:
490 res = DIV_FROM_REG(data->fan_div[ix]);
491 break;
492 case SHOW_FAN_ALARM:
493 res = (data->alarms >> bitalarmfan[ix]) & 1;
494 break;
495 default:
496 res = 0;
497 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
498 }
499
500 return sprintf(buf, "%d\n", res);
501}
502
503static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
504 const char *buf, size_t count)
505{
506 struct vt1211_data *data = dev_get_drvdata(dev);
507 struct sensor_device_attribute_2 *sensor_attr_2 =
508 to_sensor_dev_attr_2(attr);
509 int ix = sensor_attr_2->index;
510 int fn = sensor_attr_2->nr;
511 long val = simple_strtol(buf, NULL, 10);
512 int reg;
513
514 mutex_lock(&data->update_lock);
515
516 /* sync the data cache */
517 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
518 data->fan_div[0] = (reg >> 4) & 3;
519 data->fan_div[1] = (reg >> 6) & 3;
520 data->fan_ctl = reg & 0xf;
521
522 switch (fn) {
523 case SHOW_SET_FAN_MIN:
524 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
525 vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
526 data->fan_min[ix]);
527 break;
528 case SHOW_SET_FAN_DIV:
529 switch (val) {
530 case 1: data->fan_div[ix] = 0; break;
531 case 2: data->fan_div[ix] = 1; break;
532 case 4: data->fan_div[ix] = 2; break;
533 case 8: data->fan_div[ix] = 3; break;
534 default:
535 count = -EINVAL;
536 dev_warn(dev, "fan div value %ld not "
537 "supported. Choose one of 1, 2, "
538 "4, or 8.\n", val);
539 goto EXIT;
540 }
541 vt1211_write8(data, VT1211_REG_FAN_DIV,
542 ((data->fan_div[1] << 6) |
543 (data->fan_div[0] << 4) |
544 data->fan_ctl));
545 break;
546 default:
547 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
548 }
549
550EXIT:
551 mutex_unlock(&data->update_lock);
552 return count;
553}
554
555/* ---------------------------------------------------------------------
556 * PWM sysfs interfaces
557 * ix = [0-1]
558 * --------------------------------------------------------------------- */
559
560#define SHOW_PWM 0
561#define SHOW_SET_PWM_ENABLE 1
562#define SHOW_SET_PWM_FREQ 2
563#define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
564
565static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
566 char *buf)
567{
568 struct vt1211_data *data = vt1211_update_device(dev);
569 struct sensor_device_attribute_2 *sensor_attr_2 =
570 to_sensor_dev_attr_2(attr);
571 int ix = sensor_attr_2->index;
572 int fn = sensor_attr_2->nr;
573 int res;
574
575 switch (fn) {
576 case SHOW_PWM:
577 res = data->pwm[ix];
578 break;
579 case SHOW_SET_PWM_ENABLE:
580 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
581 break;
582 case SHOW_SET_PWM_FREQ:
583 res = 90000 >> (data->pwm_clk & 7);
584 break;
585 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
586 res = (data->pwm_ctl[ix] & 7) + 1;
587 break;
588 default:
589 res = 0;
590 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
591 }
592
593 return sprintf(buf, "%d\n", res);
594}
595
596static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
598{
599 struct vt1211_data *data = dev_get_drvdata(dev);
600 struct sensor_device_attribute_2 *sensor_attr_2 =
601 to_sensor_dev_attr_2(attr);
602 int ix = sensor_attr_2->index;
603 int fn = sensor_attr_2->nr;
604 long val = simple_strtol(buf, NULL, 10);
605 int tmp, reg;
606
607 mutex_lock(&data->update_lock);
608
609 switch (fn) {
610 case SHOW_SET_PWM_ENABLE:
611 /* sync the data cache */
612 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
613 data->fan_div[0] = (reg >> 4) & 3;
614 data->fan_div[1] = (reg >> 6) & 3;
615 data->fan_ctl = reg & 0xf;
616 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
617 data->pwm_ctl[0] = reg & 0xf;
618 data->pwm_ctl[1] = (reg >> 4) & 0xf;
619 switch (val) {
620 case 0:
621 data->pwm_ctl[ix] &= 7;
622 /* disable SmartGuardian if both PWM outputs are
623 * disabled */
624 if ((data->pwm_ctl[ix ^ 1] & 1) == 0) {
625 data->fan_ctl &= 0xe;
626 }
627 break;
628 case 2:
629 data->pwm_ctl[ix] |= 8;
630 data->fan_ctl |= 1;
631 break;
632 default:
633 count = -EINVAL;
634 dev_warn(dev, "pwm mode %ld not supported. "
635 "Choose one of 0 or 2.\n", val);
636 goto EXIT;
637 }
638 vt1211_write8(data, VT1211_REG_PWM_CTL,
639 ((data->pwm_ctl[1] << 4) |
640 data->pwm_ctl[0]));
641 vt1211_write8(data, VT1211_REG_FAN_DIV,
642 ((data->fan_div[1] << 6) |
643 (data->fan_div[0] << 4) |
644 data->fan_ctl));
645 break;
646 case SHOW_SET_PWM_FREQ:
647 val = 135000 / SENSORS_LIMIT(val, 135000 >> 7, 135000);
648 /* calculate tmp = log2(val) */
649 tmp = 0;
650 for (val >>= 1; val > 0; val >>= 1) {
651 tmp++;
652 }
653 /* sync the data cache */
654 reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
655 data->pwm_clk = (reg & 0xf8) | tmp;
656 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
657 break;
658 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
659 if ((val < 1) || (val > 7)) {
660 count = -EINVAL;
661 dev_warn(dev, "temp channel %ld not supported. "
662 "Choose a value between 1 and 7.\n", val);
663 goto EXIT;
664 }
665 if (!ISTEMP(val - 1, data->uch_config)) {
666 count = -EINVAL;
667 dev_warn(dev, "temp channel %ld is not available.\n",
668 val);
669 goto EXIT;
670 }
671 /* sync the data cache */
672 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
673 data->pwm_ctl[0] = reg & 0xf;
674 data->pwm_ctl[1] = (reg >> 4) & 0xf;
675 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
676 vt1211_write8(data, VT1211_REG_PWM_CTL,
677 ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
678 break;
679 default:
680 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
681 }
682
683EXIT:
684 mutex_unlock(&data->update_lock);
685 return count;
686}
687
688/* ---------------------------------------------------------------------
689 * PWM auto point definitions
690 * ix = [0-1]
691 * ap = [0-3]
692 * --------------------------------------------------------------------- */
693
694/*
695 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
696 * Note that there is only a single set of temp auto points that controls both
697 * PWM controllers. We still create 2 sets of sysfs files to make it look
698 * more consistent even though they map to the same registers.
699 *
700 * ix ap : description
701 * -------------------
702 * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
703 * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
704 * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
705 * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
706 * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
707 * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
708 * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
709 * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
710 */
711
712static ssize_t show_pwm_auto_point_temp(struct device *dev,
713 struct device_attribute *attr,
714 char *buf)
715{
716 struct vt1211_data *data = vt1211_update_device(dev);
717 struct sensor_device_attribute_2 *sensor_attr_2 =
718 to_sensor_dev_attr_2(attr);
719 int ix = sensor_attr_2->index;
720 int ap = sensor_attr_2->nr;
721
722 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
723 data->pwm_auto_temp[ap]));
724}
725
726static ssize_t set_pwm_auto_point_temp(struct device *dev,
727 struct device_attribute *attr,
728 const char *buf, size_t count)
729{
730 struct vt1211_data *data = dev_get_drvdata(dev);
731 struct sensor_device_attribute_2 *sensor_attr_2 =
732 to_sensor_dev_attr_2(attr);
733 int ix = sensor_attr_2->index;
734 int ap = sensor_attr_2->nr;
735 long val = simple_strtol(buf, NULL, 10);
736 int reg;
737
738 mutex_lock(&data->update_lock);
739
740 /* sync the data cache */
741 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
742 data->pwm_ctl[0] = reg & 0xf;
743 data->pwm_ctl[1] = (reg >> 4) & 0xf;
744
745 data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
746 vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
747 data->pwm_auto_temp[ap]);
748 mutex_unlock(&data->update_lock);
749
750 return count;
751}
752
753/*
754 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
755 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
756 * be changed.
757 *
758 * ix ap : description
759 * -------------------
760 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
761 * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
762 * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
763 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
764 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
765 * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
766 * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
767 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
768*/
769
770static ssize_t show_pwm_auto_point_pwm(struct device *dev,
771 struct device_attribute *attr,
772 char *buf)
773{
774 struct vt1211_data *data = vt1211_update_device(dev);
775 struct sensor_device_attribute_2 *sensor_attr_2 =
776 to_sensor_dev_attr_2(attr);
777 int ix = sensor_attr_2->index;
778 int ap = sensor_attr_2->nr;
779
780 return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
781}
782
783static ssize_t set_pwm_auto_point_pwm(struct device *dev,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
786{
787 struct vt1211_data *data = dev_get_drvdata(dev);
788 struct sensor_device_attribute_2 *sensor_attr_2 =
789 to_sensor_dev_attr_2(attr);
790 int ix = sensor_attr_2->index;
791 int ap = sensor_attr_2->nr;
792 long val = simple_strtol(buf, NULL, 10);
793
794 if ((val < 0) || (val > 255)) {
795 dev_err(dev, "pwm value %ld is out of range. "
796 "Choose a value between 0 and 255." , val);
797 return -EINVAL;
798 }
799
800 mutex_lock(&data->update_lock);
801 data->pwm_auto_pwm[ix][ap] = val;
802 vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
803 data->pwm_auto_pwm[ix][ap]);
804 mutex_unlock(&data->update_lock);
805
806 return count;
807}
808
809/* ---------------------------------------------------------------------
810 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
811 * --------------------------------------------------------------------- */
812
813static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
814 char *buf)
815{
816 struct vt1211_data *data = dev_get_drvdata(dev);
817
818 return sprintf(buf, "%d\n", data->vrm);
819}
820
821static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
822 const char *buf, size_t count)
823{
824 struct vt1211_data *data = dev_get_drvdata(dev);
825 long val = simple_strtol(buf, NULL, 10);
826
827 data->vrm = val;
828
829 return count;
830}
831
832static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
833 char *buf)
834{
835 struct vt1211_data *data = dev_get_drvdata(dev);
836
837 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
838}
839
840static ssize_t show_name(struct device *dev,
841 struct device_attribute *attr, char *buf)
842{
843 struct vt1211_data *data = dev_get_drvdata(dev);
844
845 return sprintf(buf, "%s\n", data->name);
846}
847
848static ssize_t show_alarms(struct device *dev,
849 struct device_attribute *attr, char *buf)
850{
851 struct vt1211_data *data = vt1211_update_device(dev);
852
853 return sprintf(buf, "%d\n", data->alarms);
854}
855
856/* ---------------------------------------------------------------------
857 * Device attribute structs
858 * --------------------------------------------------------------------- */
859
860#define SENSOR_ATTR_IN_INPUT(ix) \
861 SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
862 show_in, NULL, SHOW_IN_INPUT, ix)
863
864static struct sensor_device_attribute_2 vt1211_sysfs_in_input[] = {
865 SENSOR_ATTR_IN_INPUT(0),
866 SENSOR_ATTR_IN_INPUT(1),
867 SENSOR_ATTR_IN_INPUT(2),
868 SENSOR_ATTR_IN_INPUT(3),
869 SENSOR_ATTR_IN_INPUT(4),
870 SENSOR_ATTR_IN_INPUT(5),
871};
872
873#define SENSOR_ATTR_IN_MIN(ix) \
874 SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
875 show_in, set_in, SHOW_SET_IN_MIN, ix)
876
877static struct sensor_device_attribute_2 vt1211_sysfs_in_min[] = {
878 SENSOR_ATTR_IN_MIN(0),
879 SENSOR_ATTR_IN_MIN(1),
880 SENSOR_ATTR_IN_MIN(2),
881 SENSOR_ATTR_IN_MIN(3),
882 SENSOR_ATTR_IN_MIN(4),
883 SENSOR_ATTR_IN_MIN(5),
884};
885
886#define SENSOR_ATTR_IN_MAX(ix) \
887 SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
888 show_in, set_in, SHOW_SET_IN_MAX, ix)
889
890static struct sensor_device_attribute_2 vt1211_sysfs_in_max[] = {
891 SENSOR_ATTR_IN_MAX(0),
892 SENSOR_ATTR_IN_MAX(1),
893 SENSOR_ATTR_IN_MAX(2),
894 SENSOR_ATTR_IN_MAX(3),
895 SENSOR_ATTR_IN_MAX(4),
896 SENSOR_ATTR_IN_MAX(5),
897};
898
899#define SENSOR_ATTR_IN_ALARM(ix) \
900 SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
901 show_in, NULL, SHOW_IN_ALARM, ix)
902
903static struct sensor_device_attribute_2 vt1211_sysfs_in_alarm[] = {
904 SENSOR_ATTR_IN_ALARM(0),
905 SENSOR_ATTR_IN_ALARM(1),
906 SENSOR_ATTR_IN_ALARM(2),
907 SENSOR_ATTR_IN_ALARM(3),
908 SENSOR_ATTR_IN_ALARM(4),
909 SENSOR_ATTR_IN_ALARM(5),
910};
911
912#define SENSOR_ATTR_TEMP_INPUT(ix) \
913 SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
914 show_temp, NULL, SHOW_TEMP_INPUT, ix-1)
915
916static struct sensor_device_attribute_2 vt1211_sysfs_temp_input[] = {
917 SENSOR_ATTR_TEMP_INPUT(1),
918 SENSOR_ATTR_TEMP_INPUT(2),
919 SENSOR_ATTR_TEMP_INPUT(3),
920 SENSOR_ATTR_TEMP_INPUT(4),
921 SENSOR_ATTR_TEMP_INPUT(5),
922 SENSOR_ATTR_TEMP_INPUT(6),
923 SENSOR_ATTR_TEMP_INPUT(7),
924};
925
926#define SENSOR_ATTR_TEMP_MAX(ix) \
927 SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
928 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1)
929
930static struct sensor_device_attribute_2 vt1211_sysfs_temp_max[] = {
931 SENSOR_ATTR_TEMP_MAX(1),
932 SENSOR_ATTR_TEMP_MAX(2),
933 SENSOR_ATTR_TEMP_MAX(3),
934 SENSOR_ATTR_TEMP_MAX(4),
935 SENSOR_ATTR_TEMP_MAX(5),
936 SENSOR_ATTR_TEMP_MAX(6),
937 SENSOR_ATTR_TEMP_MAX(7),
938};
939
940#define SENSOR_ATTR_TEMP_MAX_HYST(ix) \
941 SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
942 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1)
943
944static struct sensor_device_attribute_2 vt1211_sysfs_temp_max_hyst[] = {
945 SENSOR_ATTR_TEMP_MAX_HYST(1),
946 SENSOR_ATTR_TEMP_MAX_HYST(2),
947 SENSOR_ATTR_TEMP_MAX_HYST(3),
948 SENSOR_ATTR_TEMP_MAX_HYST(4),
949 SENSOR_ATTR_TEMP_MAX_HYST(5),
950 SENSOR_ATTR_TEMP_MAX_HYST(6),
951 SENSOR_ATTR_TEMP_MAX_HYST(7),
952};
953
954#define SENSOR_ATTR_TEMP_ALARM(ix) \
955 SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
956 show_temp, NULL, SHOW_TEMP_ALARM, ix-1)
957
958static struct sensor_device_attribute_2 vt1211_sysfs_temp_alarm[] = {
959 SENSOR_ATTR_TEMP_ALARM(1),
960 SENSOR_ATTR_TEMP_ALARM(2),
961 SENSOR_ATTR_TEMP_ALARM(3),
962 SENSOR_ATTR_TEMP_ALARM(4),
963 SENSOR_ATTR_TEMP_ALARM(5),
964 SENSOR_ATTR_TEMP_ALARM(6),
965 SENSOR_ATTR_TEMP_ALARM(7),
966};
967
968#define SENSOR_ATTR_FAN(ix) \
969 SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
970 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
971 SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
972 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
973 SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
974 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
975 SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
976 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
977
978#define SENSOR_ATTR_PWM(ix) \
979 SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
980 show_pwm, NULL, SHOW_PWM, ix-1), \
981 SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
982 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
983 SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
984 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
985
986#define SENSOR_ATTR_PWM_FREQ(ix) \
987 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
988 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
989
990#define SENSOR_ATTR_PWM_FREQ_RO(ix) \
991 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
992 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
993
994#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
995 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
996 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
997 ap-1, ix-1)
998
999#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1000 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1001 show_pwm_auto_point_temp, NULL, \
1002 ap-1, ix-1)
1003
1004#define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1005 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1006 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1007 ap-1, ix-1)
1008
1009#define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1010 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1011 show_pwm_auto_point_pwm, NULL, \
1012 ap-1, ix-1)
1013
1014static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1015 SENSOR_ATTR_FAN(1),
1016 SENSOR_ATTR_FAN(2),
1017 SENSOR_ATTR_PWM(1),
1018 SENSOR_ATTR_PWM(2),
1019 SENSOR_ATTR_PWM_FREQ(1),
1020 SENSOR_ATTR_PWM_FREQ_RO(2),
1021 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1022 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1023 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1024 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1025 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1026 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1027 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1028 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1029 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1030 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1031 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1032 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1033 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1034 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1035 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1036 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1037};
1038
1039static struct device_attribute vt1211_sysfs_misc[] = {
1040 __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1041 __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1042 __ATTR(name, S_IRUGO, show_name, NULL),
1043 __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1044};
1045
1046/* ---------------------------------------------------------------------
1047 * Device registration and initialization
1048 * --------------------------------------------------------------------- */
1049
1050static void __devinit vt1211_init_device(struct vt1211_data *data)
1051{
1052 /* set VRM */
1053 data->vrm = vid_which_vrm();
1054
1055 /* Read (and initialize) UCH config */
1056 data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1057 if (uch_config > -1) {
1058 data->uch_config = (data->uch_config & 0x83) |
1059 (uch_config << 2);
1060 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1061 }
1062
1063 /* Initialize the interrupt mode (if request at module load time).
1064 * The VT1211 implements 3 different modes for clearing interrupts:
1065 * 0: Clear INT when status register is read. Regenerate INT as long
1066 * as temp stays above hysteresis limit.
1067 * 1: Clear INT when status register is read. DON'T regenerate INT
1068 * until temp falls below hysteresis limit and exceeds hot limit
1069 * again.
1070 * 2: Clear INT when temp falls below max limit.
1071 *
1072 * The driver only allows to force mode 0 since that's the only one
1073 * that makes sense for 'sensors' */
1074 if (int_mode == 0) {
1075 vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1076 vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1077 }
1078
1079 /* Fill in some hard wired values into our data struct */
1080 data->pwm_auto_pwm[0][3] = 255;
1081 data->pwm_auto_pwm[1][3] = 255;
1082}
1083
1084static void vt1211_remove_sysfs(struct platform_device *pdev)
1085{
1086 struct device *dev = &pdev->dev;
1087 int i;
1088
1089 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1090 device_remove_file(dev,
1091 &vt1211_sysfs_in_input[i].dev_attr);
1092 device_remove_file(dev,
1093 &vt1211_sysfs_in_min[i].dev_attr);
1094 device_remove_file(dev,
1095 &vt1211_sysfs_in_max[i].dev_attr);
1096 device_remove_file(dev,
1097 &vt1211_sysfs_in_alarm[i].dev_attr);
1098 }
1099 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1100 device_remove_file(dev,
1101 &vt1211_sysfs_temp_input[i].dev_attr);
1102 device_remove_file(dev,
1103 &vt1211_sysfs_temp_max[i].dev_attr);
1104 device_remove_file(dev,
1105 &vt1211_sysfs_temp_max_hyst[i].dev_attr);
1106 device_remove_file(dev,
1107 &vt1211_sysfs_temp_alarm[i].dev_attr);
1108 }
1109 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1110 device_remove_file(dev,
1111 &vt1211_sysfs_fan_pwm[i].dev_attr);
1112 }
1113 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1114 device_remove_file(dev, &vt1211_sysfs_misc[i]);
1115 }
1116}
1117
1118static int __devinit vt1211_probe(struct platform_device *pdev)
1119{
1120 struct device *dev = &pdev->dev;
1121 struct vt1211_data *data;
1122 struct resource *res;
1123 int i, err;
1124
1125 if (!(data = kzalloc(sizeof(struct vt1211_data), GFP_KERNEL))) {
1126 err = -ENOMEM;
1127 dev_err(dev, "Out of memory\n");
1128 goto EXIT;
1129 }
1130
1131 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1132 data->addr = res->start;
1133 data->name = DRVNAME;
1134 mutex_init(&data->update_lock);
1135
1136 platform_set_drvdata(pdev, data);
1137
1138 /* Initialize the VT1211 chip */
1139 vt1211_init_device(data);
1140
1141 /* Create sysfs interface files */
1142 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1143 if (ISVOLT(i, data->uch_config)) {
1144 if ((err = device_create_file(dev,
1145 &vt1211_sysfs_in_input[i].dev_attr)) ||
1146 (err = device_create_file(dev,
1147 &vt1211_sysfs_in_min[i].dev_attr)) ||
1148 (err = device_create_file(dev,
1149 &vt1211_sysfs_in_max[i].dev_attr)) ||
1150 (err = device_create_file(dev,
1151 &vt1211_sysfs_in_alarm[i].dev_attr))) {
1152 goto EXIT_DEV_REMOVE;
1153 }
1154 }
1155 }
1156 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1157 if (ISTEMP(i, data->uch_config)) {
1158 if ((err = device_create_file(dev,
1159 &vt1211_sysfs_temp_input[i].dev_attr)) ||
1160 (err = device_create_file(dev,
1161 &vt1211_sysfs_temp_max[i].dev_attr)) ||
1162 (err = device_create_file(dev,
1163 &vt1211_sysfs_temp_max_hyst[i].dev_attr)) ||
1164 (err = device_create_file(dev,
1165 &vt1211_sysfs_temp_alarm[i].dev_attr))) {
1166 goto EXIT_DEV_REMOVE;
1167 }
1168 }
1169 }
1170 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1171 err = device_create_file(dev,
1172 &vt1211_sysfs_fan_pwm[i].dev_attr);
1173 if (err) {
1174 goto EXIT_DEV_REMOVE;
1175 }
1176 }
1177 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1178 err = device_create_file(dev,
1179 &vt1211_sysfs_misc[i]);
1180 if (err) {
1181 goto EXIT_DEV_REMOVE;
1182 }
1183 }
1184
1185 /* Register device */
1186 data->class_dev = hwmon_device_register(dev);
1187 if (IS_ERR(data->class_dev)) {
1188 err = PTR_ERR(data->class_dev);
1189 dev_err(dev, "Class registration failed (%d)\n", err);
1190 goto EXIT_DEV_REMOVE_SILENT;
1191 }
1192
1193 return 0;
1194
1195EXIT_DEV_REMOVE:
1196 dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1197EXIT_DEV_REMOVE_SILENT:
1198 vt1211_remove_sysfs(pdev);
1199 platform_set_drvdata(pdev, NULL);
1200 kfree(data);
1201EXIT:
1202 return err;
1203}
1204
1205static int __devexit vt1211_remove(struct platform_device *pdev)
1206{
1207 struct vt1211_data *data = platform_get_drvdata(pdev);
1208
1209 hwmon_device_unregister(data->class_dev);
1210 vt1211_remove_sysfs(pdev);
1211 platform_set_drvdata(pdev, NULL);
1212 kfree(data);
1213
1214 return 0;
1215}
1216
1217static struct platform_driver vt1211_driver = {
1218 .driver = {
1219 .owner = THIS_MODULE,
1220 .name = DRVNAME,
1221 },
1222 .probe = vt1211_probe,
1223 .remove = __devexit_p(vt1211_remove),
1224};
1225
1226static int __init vt1211_device_add(unsigned short address)
1227{
1228 struct resource res = {
1229 .start = address,
1230 .end = address + 0x7f,
1231 .flags = IORESOURCE_IO,
1232 };
1233 int err;
1234
1235 pdev = platform_device_alloc(DRVNAME, address);
1236 if (!pdev) {
1237 err = -ENOMEM;
1238 printk(KERN_ERR DRVNAME ": Device allocation failed (%d)\n",
1239 err);
1240 goto EXIT;
1241 }
1242
1243 res.name = pdev->name;
1244 err = platform_device_add_resources(pdev, &res, 1);
1245 if (err) {
1246 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1247 "(%d)\n", err);
1248 goto EXIT_DEV_PUT;
1249 }
1250
1251 err = platform_device_add(pdev);
1252 if (err) {
1253 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1254 err);
1255 goto EXIT_DEV_PUT;
1256 }
1257
1258 return 0;
1259
1260EXIT_DEV_PUT:
1261 platform_device_put(pdev);
1262EXIT:
1263 return err;
1264}
1265
1266static int __init vt1211_find(unsigned short *address)
1267{
1268 int err = -ENODEV;
1269
1270 superio_enter();
1271
1272 if (superio_inb(SIO_VT1211_DEVID) != SIO_VT1211_ID) {
1273 goto EXIT;
1274 }
1275
1276 superio_select(SIO_VT1211_LDN_HWMON);
1277
1278 if ((superio_inb(SIO_VT1211_ACTIVE) & 1) == 0) {
1279 printk(KERN_WARNING DRVNAME ": HW monitor is disabled, "
1280 "skipping\n");
1281 goto EXIT;
1282 }
1283
1284 *address = ((superio_inb(SIO_VT1211_BADDR) << 8) |
1285 (superio_inb(SIO_VT1211_BADDR + 1))) & 0xff00;
1286 if (*address == 0) {
1287 printk(KERN_WARNING DRVNAME ": Base address is not set, "
1288 "skipping\n");
1289 goto EXIT;
1290 }
1291
1292 err = 0;
1293 printk(KERN_INFO DRVNAME ": Found VT1211 chip at 0x%04x, "
1294 "revision %u\n", *address, superio_inb(SIO_VT1211_DEVREV));
1295
1296EXIT:
1297 superio_exit();
1298 return err;
1299}
1300
1301static int __init vt1211_init(void)
1302{
1303 int err;
1304 unsigned short address = 0;
1305
1306 err = vt1211_find(&address);
1307 if (err) {
1308 goto EXIT;
1309 }
1310
1311 if ((uch_config < -1) || (uch_config > 31)) {
1312 err = -EINVAL;
1313 printk(KERN_WARNING DRVNAME ": Invalid UCH configuration %d. "
1314 "Choose a value between 0 and 31.\n", uch_config);
1315 goto EXIT;
1316 }
1317
1318 if ((int_mode < -1) || (int_mode > 0)) {
1319 err = -EINVAL;
1320 printk(KERN_WARNING DRVNAME ": Invalid interrupt mode %d. "
1321 "Only mode 0 is supported.\n", int_mode);
1322 goto EXIT;
1323 }
1324
1325 err = platform_driver_register(&vt1211_driver);
1326 if (err) {
1327 goto EXIT;
1328 }
1329
1330 /* Sets global pdev as a side effect */
1331 err = vt1211_device_add(address);
1332 if (err) {
1333 goto EXIT_DRV_UNREGISTER;
1334 }
1335
1336 return 0;
1337
1338EXIT_DRV_UNREGISTER:
1339 platform_driver_unregister(&vt1211_driver);
1340EXIT:
1341 return err;
1342}
1343
1344static void __exit vt1211_exit(void)
1345{
1346 platform_device_unregister(pdev);
1347 platform_driver_unregister(&vt1211_driver);
1348}
1349
1350MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1351MODULE_DESCRIPTION("VT1211 sensors");
1352MODULE_LICENSE("GPL");
1353
1354module_init(vt1211_init);
1355module_exit(vt1211_exit);