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