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