blob: 89c23d6add7be174190cbc90f2cf2f1b25d2be10 [file] [log] [blame]
Juerg Haefligerab413192006-09-24 20:54:04 +02001/*
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
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100181/* Configuration index port registers
182 * The vt1211 can live at 2 different addresses so we need to probe both */
183#define SIO_REG_CIP1 0x2e
184#define SIO_REG_CIP2 0x4e
Juerg Haefligerab413192006-09-24 20:54:04 +0200185
186/* Configuration registers */
187#define SIO_VT1211_LDN 0x07 /* logical device number */
188#define SIO_VT1211_DEVID 0x20 /* device ID */
189#define SIO_VT1211_DEVREV 0x21 /* device revision */
190#define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
191#define SIO_VT1211_BADDR 0x60 /* base I/O address */
192#define SIO_VT1211_ID 0x3c /* VT1211 device ID */
193
194/* VT1211 logical device numbers */
195#define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
196
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100197static inline void superio_outb(int sio_cip, int reg, int val)
Juerg Haefligerab413192006-09-24 20:54:04 +0200198{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100199 outb(reg, sio_cip);
200 outb(val, sio_cip + 1);
Juerg Haefligerab413192006-09-24 20:54:04 +0200201}
202
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100203static inline int superio_inb(int sio_cip, int reg)
Juerg Haefligerab413192006-09-24 20:54:04 +0200204{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100205 outb(reg, sio_cip);
206 return inb(sio_cip + 1);
Juerg Haefligerab413192006-09-24 20:54:04 +0200207}
208
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100209static inline void superio_select(int sio_cip, int ldn)
Juerg Haefligerab413192006-09-24 20:54:04 +0200210{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100211 outb(SIO_VT1211_LDN, sio_cip);
212 outb(ldn, sio_cip + 1);
Juerg Haefligerab413192006-09-24 20:54:04 +0200213}
214
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100215static inline void superio_enter(int sio_cip)
Juerg Haefligerab413192006-09-24 20:54:04 +0200216{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100217 outb(0x87, sio_cip);
218 outb(0x87, sio_cip);
Juerg Haefligerab413192006-09-24 20:54:04 +0200219}
220
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100221static inline void superio_exit(int sio_cip)
Juerg Haefligerab413192006-09-24 20:54:04 +0200222{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100223 outb(0xaa, sio_cip);
Juerg Haefligerab413192006-09-24 20:54:04 +0200224}
225
226/* ---------------------------------------------------------------------
227 * Device I/O access
228 * --------------------------------------------------------------------- */
229
230static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
231{
232 return inb(data->addr + reg);
233}
234
235static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
236{
237 outb(val, data->addr + reg);
238}
239
240static struct vt1211_data *vt1211_update_device(struct device *dev)
241{
242 struct vt1211_data *data = dev_get_drvdata(dev);
243 int ix, val;
244
245 mutex_lock(&data->update_lock);
246
247 /* registers cache is refreshed after 1 second */
248 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
249 /* read VID */
250 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
251
252 /* voltage (in) registers */
253 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
254 if (ISVOLT(ix, data->uch_config)) {
255 data->in[ix] = vt1211_read8(data,
256 VT1211_REG_IN(ix));
257 data->in_min[ix] = vt1211_read8(data,
258 VT1211_REG_IN_MIN(ix));
259 data->in_max[ix] = vt1211_read8(data,
260 VT1211_REG_IN_MAX(ix));
261 }
262 }
263
264 /* temp registers */
265 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
266 if (ISTEMP(ix, data->uch_config)) {
267 data->temp[ix] = vt1211_read8(data,
268 regtemp[ix]);
269 data->temp_max[ix] = vt1211_read8(data,
270 regtempmax[ix]);
271 data->temp_hyst[ix] = vt1211_read8(data,
272 regtemphyst[ix]);
273 }
274 }
275
276 /* fan & pwm registers */
277 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
278 data->fan[ix] = vt1211_read8(data,
279 VT1211_REG_FAN(ix));
280 data->fan_min[ix] = vt1211_read8(data,
281 VT1211_REG_FAN_MIN(ix));
282 data->pwm[ix] = vt1211_read8(data,
283 VT1211_REG_PWM(ix));
284 }
285 val = vt1211_read8(data, VT1211_REG_FAN_DIV);
286 data->fan_div[0] = (val >> 4) & 3;
287 data->fan_div[1] = (val >> 6) & 3;
288 data->fan_ctl = val & 0xf;
289
290 val = vt1211_read8(data, VT1211_REG_PWM_CTL);
291 data->pwm_ctl[0] = val & 0xf;
292 data->pwm_ctl[1] = (val >> 4) & 0xf;
293
294 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
295
296 /* pwm & temp auto point registers */
297 data->pwm_auto_pwm[0][1] = vt1211_read8(data,
298 VT1211_REG_PWM_AUTO_PWM(0, 1));
299 data->pwm_auto_pwm[0][2] = vt1211_read8(data,
300 VT1211_REG_PWM_AUTO_PWM(0, 2));
301 data->pwm_auto_pwm[1][1] = vt1211_read8(data,
302 VT1211_REG_PWM_AUTO_PWM(1, 1));
303 data->pwm_auto_pwm[1][2] = vt1211_read8(data,
304 VT1211_REG_PWM_AUTO_PWM(1, 2));
305 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
306 data->pwm_auto_temp[ix] = vt1211_read8(data,
307 VT1211_REG_PWM_AUTO_TEMP(ix));
308 }
309
310 /* alarm registers */
311 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
312 vt1211_read8(data, VT1211_REG_ALARM1);
313
314 data->last_updated = jiffies;
315 data->valid = 1;
316 }
317
318 mutex_unlock(&data->update_lock);
319
320 return data;
321}
322
323/* ---------------------------------------------------------------------
324 * Voltage sysfs interfaces
325 * ix = [0-5]
326 * --------------------------------------------------------------------- */
327
328#define SHOW_IN_INPUT 0
329#define SHOW_SET_IN_MIN 1
330#define SHOW_SET_IN_MAX 2
331#define SHOW_IN_ALARM 3
332
333static ssize_t show_in(struct device *dev, struct device_attribute *attr,
334 char *buf)
335{
336 struct vt1211_data *data = vt1211_update_device(dev);
337 struct sensor_device_attribute_2 *sensor_attr_2 =
338 to_sensor_dev_attr_2(attr);
339 int ix = sensor_attr_2->index;
340 int fn = sensor_attr_2->nr;
341 int res;
342
343 switch (fn) {
344 case SHOW_IN_INPUT:
345 res = IN_FROM_REG(ix, data->in[ix]);
346 break;
347 case SHOW_SET_IN_MIN:
348 res = IN_FROM_REG(ix, data->in_min[ix]);
349 break;
350 case SHOW_SET_IN_MAX:
351 res = IN_FROM_REG(ix, data->in_max[ix]);
352 break;
353 case SHOW_IN_ALARM:
354 res = (data->alarms >> bitalarmin[ix]) & 1;
355 break;
356 default:
357 res = 0;
358 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
359 }
360
361 return sprintf(buf, "%d\n", res);
362}
363
364static ssize_t set_in(struct device *dev, struct device_attribute *attr,
365 const char *buf, size_t count)
366{
367 struct vt1211_data *data = dev_get_drvdata(dev);
368 struct sensor_device_attribute_2 *sensor_attr_2 =
369 to_sensor_dev_attr_2(attr);
370 int ix = sensor_attr_2->index;
371 int fn = sensor_attr_2->nr;
372 long val = simple_strtol(buf, NULL, 10);
373
374 mutex_lock(&data->update_lock);
375 switch (fn) {
376 case SHOW_SET_IN_MIN:
377 data->in_min[ix] = IN_TO_REG(ix, val);
378 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
379 break;
380 case SHOW_SET_IN_MAX:
381 data->in_max[ix] = IN_TO_REG(ix, val);
382 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
383 break;
384 default:
385 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
386 }
387 mutex_unlock(&data->update_lock);
388
389 return count;
390}
391
392/* ---------------------------------------------------------------------
393 * Temperature sysfs interfaces
394 * ix = [0-6]
395 * --------------------------------------------------------------------- */
396
397#define SHOW_TEMP_INPUT 0
398#define SHOW_SET_TEMP_MAX 1
399#define SHOW_SET_TEMP_MAX_HYST 2
400#define SHOW_TEMP_ALARM 3
401
402static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
403 char *buf)
404{
405 struct vt1211_data *data = vt1211_update_device(dev);
406 struct sensor_device_attribute_2 *sensor_attr_2 =
407 to_sensor_dev_attr_2(attr);
408 int ix = sensor_attr_2->index;
409 int fn = sensor_attr_2->nr;
410 int res;
411
412 switch (fn) {
413 case SHOW_TEMP_INPUT:
414 res = TEMP_FROM_REG(ix, data->temp[ix]);
415 break;
416 case SHOW_SET_TEMP_MAX:
417 res = TEMP_FROM_REG(ix, data->temp_max[ix]);
418 break;
419 case SHOW_SET_TEMP_MAX_HYST:
420 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
421 break;
422 case SHOW_TEMP_ALARM:
423 res = (data->alarms >> bitalarmtemp[ix]) & 1;
424 break;
425 default:
426 res = 0;
427 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
428 }
429
430 return sprintf(buf, "%d\n", res);
431}
432
433static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
435{
436 struct vt1211_data *data = dev_get_drvdata(dev);
437 struct sensor_device_attribute_2 *sensor_attr_2 =
438 to_sensor_dev_attr_2(attr);
439 int ix = sensor_attr_2->index;
440 int fn = sensor_attr_2->nr;
441 long val = simple_strtol(buf, NULL, 10);
442
443 mutex_lock(&data->update_lock);
444 switch (fn) {
445 case SHOW_SET_TEMP_MAX:
446 data->temp_max[ix] = TEMP_TO_REG(ix, val);
447 vt1211_write8(data, regtempmax[ix],
448 data->temp_max[ix]);
449 break;
450 case SHOW_SET_TEMP_MAX_HYST:
451 data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
452 vt1211_write8(data, regtemphyst[ix],
453 data->temp_hyst[ix]);
454 break;
455 default:
456 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
457 }
458 mutex_unlock(&data->update_lock);
459
460 return count;
461}
462
463/* ---------------------------------------------------------------------
464 * Fan sysfs interfaces
465 * ix = [0-1]
466 * --------------------------------------------------------------------- */
467
468#define SHOW_FAN_INPUT 0
469#define SHOW_SET_FAN_MIN 1
470#define SHOW_SET_FAN_DIV 2
471#define SHOW_FAN_ALARM 3
472
473static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
474 char *buf)
475{
476 struct vt1211_data *data = vt1211_update_device(dev);
477 struct sensor_device_attribute_2 *sensor_attr_2 =
478 to_sensor_dev_attr_2(attr);
479 int ix = sensor_attr_2->index;
480 int fn = sensor_attr_2->nr;
481 int res;
482
483 switch (fn) {
484 case SHOW_FAN_INPUT:
485 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
486 break;
487 case SHOW_SET_FAN_MIN:
488 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
489 break;
490 case SHOW_SET_FAN_DIV:
491 res = DIV_FROM_REG(data->fan_div[ix]);
492 break;
493 case SHOW_FAN_ALARM:
494 res = (data->alarms >> bitalarmfan[ix]) & 1;
495 break;
496 default:
497 res = 0;
498 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
499 }
500
501 return sprintf(buf, "%d\n", res);
502}
503
504static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
505 const char *buf, size_t count)
506{
507 struct vt1211_data *data = dev_get_drvdata(dev);
508 struct sensor_device_attribute_2 *sensor_attr_2 =
509 to_sensor_dev_attr_2(attr);
510 int ix = sensor_attr_2->index;
511 int fn = sensor_attr_2->nr;
512 long val = simple_strtol(buf, NULL, 10);
513 int reg;
514
515 mutex_lock(&data->update_lock);
516
517 /* sync the data cache */
518 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
519 data->fan_div[0] = (reg >> 4) & 3;
520 data->fan_div[1] = (reg >> 6) & 3;
521 data->fan_ctl = reg & 0xf;
522
523 switch (fn) {
524 case SHOW_SET_FAN_MIN:
525 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
526 vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
527 data->fan_min[ix]);
528 break;
529 case SHOW_SET_FAN_DIV:
530 switch (val) {
531 case 1: data->fan_div[ix] = 0; break;
532 case 2: data->fan_div[ix] = 1; break;
533 case 4: data->fan_div[ix] = 2; break;
534 case 8: data->fan_div[ix] = 3; break;
535 default:
536 count = -EINVAL;
537 dev_warn(dev, "fan div value %ld not "
538 "supported. Choose one of 1, 2, "
539 "4, or 8.\n", val);
540 goto EXIT;
541 }
542 vt1211_write8(data, VT1211_REG_FAN_DIV,
543 ((data->fan_div[1] << 6) |
544 (data->fan_div[0] << 4) |
545 data->fan_ctl));
546 break;
547 default:
548 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
549 }
550
551EXIT:
552 mutex_unlock(&data->update_lock);
553 return count;
554}
555
556/* ---------------------------------------------------------------------
557 * PWM sysfs interfaces
558 * ix = [0-1]
559 * --------------------------------------------------------------------- */
560
561#define SHOW_PWM 0
562#define SHOW_SET_PWM_ENABLE 1
563#define SHOW_SET_PWM_FREQ 2
564#define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
565
566static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
567 char *buf)
568{
569 struct vt1211_data *data = vt1211_update_device(dev);
570 struct sensor_device_attribute_2 *sensor_attr_2 =
571 to_sensor_dev_attr_2(attr);
572 int ix = sensor_attr_2->index;
573 int fn = sensor_attr_2->nr;
574 int res;
575
576 switch (fn) {
577 case SHOW_PWM:
578 res = data->pwm[ix];
579 break;
580 case SHOW_SET_PWM_ENABLE:
581 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
582 break;
583 case SHOW_SET_PWM_FREQ:
584 res = 90000 >> (data->pwm_clk & 7);
585 break;
586 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
587 res = (data->pwm_ctl[ix] & 7) + 1;
588 break;
589 default:
590 res = 0;
591 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
592 }
593
594 return sprintf(buf, "%d\n", res);
595}
596
597static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
598 const char *buf, size_t count)
599{
600 struct vt1211_data *data = dev_get_drvdata(dev);
601 struct sensor_device_attribute_2 *sensor_attr_2 =
602 to_sensor_dev_attr_2(attr);
603 int ix = sensor_attr_2->index;
604 int fn = sensor_attr_2->nr;
605 long val = simple_strtol(buf, NULL, 10);
606 int tmp, reg;
607
608 mutex_lock(&data->update_lock);
609
610 switch (fn) {
611 case SHOW_SET_PWM_ENABLE:
612 /* sync the data cache */
613 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
614 data->fan_div[0] = (reg >> 4) & 3;
615 data->fan_div[1] = (reg >> 6) & 3;
616 data->fan_ctl = reg & 0xf;
617 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
618 data->pwm_ctl[0] = reg & 0xf;
619 data->pwm_ctl[1] = (reg >> 4) & 0xf;
620 switch (val) {
621 case 0:
622 data->pwm_ctl[ix] &= 7;
623 /* disable SmartGuardian if both PWM outputs are
624 * disabled */
625 if ((data->pwm_ctl[ix ^ 1] & 1) == 0) {
626 data->fan_ctl &= 0xe;
627 }
628 break;
629 case 2:
630 data->pwm_ctl[ix] |= 8;
631 data->fan_ctl |= 1;
632 break;
633 default:
634 count = -EINVAL;
635 dev_warn(dev, "pwm mode %ld not supported. "
636 "Choose one of 0 or 2.\n", val);
637 goto EXIT;
638 }
639 vt1211_write8(data, VT1211_REG_PWM_CTL,
640 ((data->pwm_ctl[1] << 4) |
641 data->pwm_ctl[0]));
642 vt1211_write8(data, VT1211_REG_FAN_DIV,
643 ((data->fan_div[1] << 6) |
644 (data->fan_div[0] << 4) |
645 data->fan_ctl));
646 break;
647 case SHOW_SET_PWM_FREQ:
648 val = 135000 / SENSORS_LIMIT(val, 135000 >> 7, 135000);
649 /* calculate tmp = log2(val) */
650 tmp = 0;
651 for (val >>= 1; val > 0; val >>= 1) {
652 tmp++;
653 }
654 /* sync the data cache */
655 reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
656 data->pwm_clk = (reg & 0xf8) | tmp;
657 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
658 break;
659 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
660 if ((val < 1) || (val > 7)) {
661 count = -EINVAL;
662 dev_warn(dev, "temp channel %ld not supported. "
663 "Choose a value between 1 and 7.\n", val);
664 goto EXIT;
665 }
666 if (!ISTEMP(val - 1, data->uch_config)) {
667 count = -EINVAL;
668 dev_warn(dev, "temp channel %ld is not available.\n",
669 val);
670 goto EXIT;
671 }
672 /* sync the data cache */
673 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
674 data->pwm_ctl[0] = reg & 0xf;
675 data->pwm_ctl[1] = (reg >> 4) & 0xf;
676 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
677 vt1211_write8(data, VT1211_REG_PWM_CTL,
678 ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
679 break;
680 default:
681 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
682 }
683
684EXIT:
685 mutex_unlock(&data->update_lock);
686 return count;
687}
688
689/* ---------------------------------------------------------------------
690 * PWM auto point definitions
691 * ix = [0-1]
692 * ap = [0-3]
693 * --------------------------------------------------------------------- */
694
695/*
696 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
697 * Note that there is only a single set of temp auto points that controls both
698 * PWM controllers. We still create 2 sets of sysfs files to make it look
699 * more consistent even though they map to the same registers.
700 *
701 * ix ap : description
702 * -------------------
703 * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
704 * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
705 * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
706 * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
707 * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
708 * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
709 * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
710 * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
711 */
712
713static ssize_t show_pwm_auto_point_temp(struct device *dev,
714 struct device_attribute *attr,
715 char *buf)
716{
717 struct vt1211_data *data = vt1211_update_device(dev);
718 struct sensor_device_attribute_2 *sensor_attr_2 =
719 to_sensor_dev_attr_2(attr);
720 int ix = sensor_attr_2->index;
721 int ap = sensor_attr_2->nr;
722
723 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
724 data->pwm_auto_temp[ap]));
725}
726
727static ssize_t set_pwm_auto_point_temp(struct device *dev,
728 struct device_attribute *attr,
729 const char *buf, size_t count)
730{
731 struct vt1211_data *data = dev_get_drvdata(dev);
732 struct sensor_device_attribute_2 *sensor_attr_2 =
733 to_sensor_dev_attr_2(attr);
734 int ix = sensor_attr_2->index;
735 int ap = sensor_attr_2->nr;
736 long val = simple_strtol(buf, NULL, 10);
737 int reg;
738
739 mutex_lock(&data->update_lock);
740
741 /* sync the data cache */
742 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
743 data->pwm_ctl[0] = reg & 0xf;
744 data->pwm_ctl[1] = (reg >> 4) & 0xf;
745
746 data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
747 vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
748 data->pwm_auto_temp[ap]);
749 mutex_unlock(&data->update_lock);
750
751 return count;
752}
753
754/*
755 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
756 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
757 * be changed.
758 *
759 * ix ap : description
760 * -------------------
761 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
762 * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
763 * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
764 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
765 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
766 * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
767 * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
768 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
769*/
770
771static ssize_t show_pwm_auto_point_pwm(struct device *dev,
772 struct device_attribute *attr,
773 char *buf)
774{
775 struct vt1211_data *data = vt1211_update_device(dev);
776 struct sensor_device_attribute_2 *sensor_attr_2 =
777 to_sensor_dev_attr_2(attr);
778 int ix = sensor_attr_2->index;
779 int ap = sensor_attr_2->nr;
780
781 return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
782}
783
784static ssize_t set_pwm_auto_point_pwm(struct device *dev,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
787{
788 struct vt1211_data *data = dev_get_drvdata(dev);
789 struct sensor_device_attribute_2 *sensor_attr_2 =
790 to_sensor_dev_attr_2(attr);
791 int ix = sensor_attr_2->index;
792 int ap = sensor_attr_2->nr;
793 long val = simple_strtol(buf, NULL, 10);
794
795 if ((val < 0) || (val > 255)) {
796 dev_err(dev, "pwm value %ld is out of range. "
797 "Choose a value between 0 and 255." , val);
798 return -EINVAL;
799 }
800
801 mutex_lock(&data->update_lock);
802 data->pwm_auto_pwm[ix][ap] = val;
803 vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
804 data->pwm_auto_pwm[ix][ap]);
805 mutex_unlock(&data->update_lock);
806
807 return count;
808}
809
810/* ---------------------------------------------------------------------
811 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
812 * --------------------------------------------------------------------- */
813
814static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
815 char *buf)
816{
817 struct vt1211_data *data = dev_get_drvdata(dev);
818
819 return sprintf(buf, "%d\n", data->vrm);
820}
821
822static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
823 const char *buf, size_t count)
824{
825 struct vt1211_data *data = dev_get_drvdata(dev);
826 long val = simple_strtol(buf, NULL, 10);
827
828 data->vrm = val;
829
830 return count;
831}
832
833static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
834 char *buf)
835{
836 struct vt1211_data *data = dev_get_drvdata(dev);
837
838 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
839}
840
841static ssize_t show_name(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 struct vt1211_data *data = dev_get_drvdata(dev);
845
846 return sprintf(buf, "%s\n", data->name);
847}
848
849static ssize_t show_alarms(struct device *dev,
850 struct device_attribute *attr, char *buf)
851{
852 struct vt1211_data *data = vt1211_update_device(dev);
853
854 return sprintf(buf, "%d\n", data->alarms);
855}
856
857/* ---------------------------------------------------------------------
858 * Device attribute structs
859 * --------------------------------------------------------------------- */
860
861#define SENSOR_ATTR_IN_INPUT(ix) \
862 SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
863 show_in, NULL, SHOW_IN_INPUT, ix)
864
865static struct sensor_device_attribute_2 vt1211_sysfs_in_input[] = {
866 SENSOR_ATTR_IN_INPUT(0),
867 SENSOR_ATTR_IN_INPUT(1),
868 SENSOR_ATTR_IN_INPUT(2),
869 SENSOR_ATTR_IN_INPUT(3),
870 SENSOR_ATTR_IN_INPUT(4),
871 SENSOR_ATTR_IN_INPUT(5),
872};
873
874#define SENSOR_ATTR_IN_MIN(ix) \
875 SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
876 show_in, set_in, SHOW_SET_IN_MIN, ix)
877
878static struct sensor_device_attribute_2 vt1211_sysfs_in_min[] = {
879 SENSOR_ATTR_IN_MIN(0),
880 SENSOR_ATTR_IN_MIN(1),
881 SENSOR_ATTR_IN_MIN(2),
882 SENSOR_ATTR_IN_MIN(3),
883 SENSOR_ATTR_IN_MIN(4),
884 SENSOR_ATTR_IN_MIN(5),
885};
886
887#define SENSOR_ATTR_IN_MAX(ix) \
888 SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
889 show_in, set_in, SHOW_SET_IN_MAX, ix)
890
891static struct sensor_device_attribute_2 vt1211_sysfs_in_max[] = {
892 SENSOR_ATTR_IN_MAX(0),
893 SENSOR_ATTR_IN_MAX(1),
894 SENSOR_ATTR_IN_MAX(2),
895 SENSOR_ATTR_IN_MAX(3),
896 SENSOR_ATTR_IN_MAX(4),
897 SENSOR_ATTR_IN_MAX(5),
898};
899
900#define SENSOR_ATTR_IN_ALARM(ix) \
901 SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
902 show_in, NULL, SHOW_IN_ALARM, ix)
903
904static struct sensor_device_attribute_2 vt1211_sysfs_in_alarm[] = {
905 SENSOR_ATTR_IN_ALARM(0),
906 SENSOR_ATTR_IN_ALARM(1),
907 SENSOR_ATTR_IN_ALARM(2),
908 SENSOR_ATTR_IN_ALARM(3),
909 SENSOR_ATTR_IN_ALARM(4),
910 SENSOR_ATTR_IN_ALARM(5),
911};
912
913#define SENSOR_ATTR_TEMP_INPUT(ix) \
914 SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
915 show_temp, NULL, SHOW_TEMP_INPUT, ix-1)
916
917static struct sensor_device_attribute_2 vt1211_sysfs_temp_input[] = {
918 SENSOR_ATTR_TEMP_INPUT(1),
919 SENSOR_ATTR_TEMP_INPUT(2),
920 SENSOR_ATTR_TEMP_INPUT(3),
921 SENSOR_ATTR_TEMP_INPUT(4),
922 SENSOR_ATTR_TEMP_INPUT(5),
923 SENSOR_ATTR_TEMP_INPUT(6),
924 SENSOR_ATTR_TEMP_INPUT(7),
925};
926
927#define SENSOR_ATTR_TEMP_MAX(ix) \
928 SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
929 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1)
930
931static struct sensor_device_attribute_2 vt1211_sysfs_temp_max[] = {
932 SENSOR_ATTR_TEMP_MAX(1),
933 SENSOR_ATTR_TEMP_MAX(2),
934 SENSOR_ATTR_TEMP_MAX(3),
935 SENSOR_ATTR_TEMP_MAX(4),
936 SENSOR_ATTR_TEMP_MAX(5),
937 SENSOR_ATTR_TEMP_MAX(6),
938 SENSOR_ATTR_TEMP_MAX(7),
939};
940
941#define SENSOR_ATTR_TEMP_MAX_HYST(ix) \
942 SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
943 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1)
944
945static struct sensor_device_attribute_2 vt1211_sysfs_temp_max_hyst[] = {
946 SENSOR_ATTR_TEMP_MAX_HYST(1),
947 SENSOR_ATTR_TEMP_MAX_HYST(2),
948 SENSOR_ATTR_TEMP_MAX_HYST(3),
949 SENSOR_ATTR_TEMP_MAX_HYST(4),
950 SENSOR_ATTR_TEMP_MAX_HYST(5),
951 SENSOR_ATTR_TEMP_MAX_HYST(6),
952 SENSOR_ATTR_TEMP_MAX_HYST(7),
953};
954
955#define SENSOR_ATTR_TEMP_ALARM(ix) \
956 SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
957 show_temp, NULL, SHOW_TEMP_ALARM, ix-1)
958
959static struct sensor_device_attribute_2 vt1211_sysfs_temp_alarm[] = {
960 SENSOR_ATTR_TEMP_ALARM(1),
961 SENSOR_ATTR_TEMP_ALARM(2),
962 SENSOR_ATTR_TEMP_ALARM(3),
963 SENSOR_ATTR_TEMP_ALARM(4),
964 SENSOR_ATTR_TEMP_ALARM(5),
965 SENSOR_ATTR_TEMP_ALARM(6),
966 SENSOR_ATTR_TEMP_ALARM(7),
967};
968
969#define SENSOR_ATTR_FAN(ix) \
970 SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
971 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
972 SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
973 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
974 SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
975 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
976 SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
977 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
978
979#define SENSOR_ATTR_PWM(ix) \
980 SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
981 show_pwm, NULL, SHOW_PWM, ix-1), \
982 SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
983 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
984 SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
985 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
986
987#define SENSOR_ATTR_PWM_FREQ(ix) \
988 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
989 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
990
991#define SENSOR_ATTR_PWM_FREQ_RO(ix) \
992 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
993 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
994
995#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
996 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
997 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
998 ap-1, ix-1)
999
1000#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1001 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1002 show_pwm_auto_point_temp, NULL, \
1003 ap-1, ix-1)
1004
1005#define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1006 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1007 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1008 ap-1, ix-1)
1009
1010#define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1011 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1012 show_pwm_auto_point_pwm, NULL, \
1013 ap-1, ix-1)
1014
1015static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1016 SENSOR_ATTR_FAN(1),
1017 SENSOR_ATTR_FAN(2),
1018 SENSOR_ATTR_PWM(1),
1019 SENSOR_ATTR_PWM(2),
1020 SENSOR_ATTR_PWM_FREQ(1),
1021 SENSOR_ATTR_PWM_FREQ_RO(2),
1022 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1023 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1024 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1025 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1026 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1027 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1028 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1029 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1030 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1031 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1032 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1033 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1034 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1035 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1036 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1037 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1038};
1039
1040static struct device_attribute vt1211_sysfs_misc[] = {
1041 __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1042 __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1043 __ATTR(name, S_IRUGO, show_name, NULL),
1044 __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1045};
1046
1047/* ---------------------------------------------------------------------
1048 * Device registration and initialization
1049 * --------------------------------------------------------------------- */
1050
1051static void __devinit vt1211_init_device(struct vt1211_data *data)
1052{
1053 /* set VRM */
1054 data->vrm = vid_which_vrm();
1055
1056 /* Read (and initialize) UCH config */
1057 data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1058 if (uch_config > -1) {
1059 data->uch_config = (data->uch_config & 0x83) |
1060 (uch_config << 2);
1061 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1062 }
1063
1064 /* Initialize the interrupt mode (if request at module load time).
1065 * The VT1211 implements 3 different modes for clearing interrupts:
1066 * 0: Clear INT when status register is read. Regenerate INT as long
1067 * as temp stays above hysteresis limit.
1068 * 1: Clear INT when status register is read. DON'T regenerate INT
1069 * until temp falls below hysteresis limit and exceeds hot limit
1070 * again.
1071 * 2: Clear INT when temp falls below max limit.
1072 *
1073 * The driver only allows to force mode 0 since that's the only one
1074 * that makes sense for 'sensors' */
1075 if (int_mode == 0) {
1076 vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1077 vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1078 }
1079
1080 /* Fill in some hard wired values into our data struct */
1081 data->pwm_auto_pwm[0][3] = 255;
1082 data->pwm_auto_pwm[1][3] = 255;
1083}
1084
1085static void vt1211_remove_sysfs(struct platform_device *pdev)
1086{
1087 struct device *dev = &pdev->dev;
1088 int i;
1089
1090 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1091 device_remove_file(dev,
1092 &vt1211_sysfs_in_input[i].dev_attr);
1093 device_remove_file(dev,
1094 &vt1211_sysfs_in_min[i].dev_attr);
1095 device_remove_file(dev,
1096 &vt1211_sysfs_in_max[i].dev_attr);
1097 device_remove_file(dev,
1098 &vt1211_sysfs_in_alarm[i].dev_attr);
1099 }
1100 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1101 device_remove_file(dev,
1102 &vt1211_sysfs_temp_input[i].dev_attr);
1103 device_remove_file(dev,
1104 &vt1211_sysfs_temp_max[i].dev_attr);
1105 device_remove_file(dev,
1106 &vt1211_sysfs_temp_max_hyst[i].dev_attr);
1107 device_remove_file(dev,
1108 &vt1211_sysfs_temp_alarm[i].dev_attr);
1109 }
1110 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1111 device_remove_file(dev,
1112 &vt1211_sysfs_fan_pwm[i].dev_attr);
1113 }
1114 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1115 device_remove_file(dev, &vt1211_sysfs_misc[i]);
1116 }
1117}
1118
1119static int __devinit vt1211_probe(struct platform_device *pdev)
1120{
1121 struct device *dev = &pdev->dev;
1122 struct vt1211_data *data;
1123 struct resource *res;
1124 int i, err;
1125
1126 if (!(data = kzalloc(sizeof(struct vt1211_data), GFP_KERNEL))) {
1127 err = -ENOMEM;
1128 dev_err(dev, "Out of memory\n");
1129 goto EXIT;
1130 }
1131
1132 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1133 data->addr = res->start;
1134 data->name = DRVNAME;
1135 mutex_init(&data->update_lock);
1136
1137 platform_set_drvdata(pdev, data);
1138
1139 /* Initialize the VT1211 chip */
1140 vt1211_init_device(data);
1141
1142 /* Create sysfs interface files */
1143 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1144 if (ISVOLT(i, data->uch_config)) {
1145 if ((err = device_create_file(dev,
1146 &vt1211_sysfs_in_input[i].dev_attr)) ||
1147 (err = device_create_file(dev,
1148 &vt1211_sysfs_in_min[i].dev_attr)) ||
1149 (err = device_create_file(dev,
1150 &vt1211_sysfs_in_max[i].dev_attr)) ||
1151 (err = device_create_file(dev,
1152 &vt1211_sysfs_in_alarm[i].dev_attr))) {
1153 goto EXIT_DEV_REMOVE;
1154 }
1155 }
1156 }
1157 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1158 if (ISTEMP(i, data->uch_config)) {
1159 if ((err = device_create_file(dev,
1160 &vt1211_sysfs_temp_input[i].dev_attr)) ||
1161 (err = device_create_file(dev,
1162 &vt1211_sysfs_temp_max[i].dev_attr)) ||
1163 (err = device_create_file(dev,
1164 &vt1211_sysfs_temp_max_hyst[i].dev_attr)) ||
1165 (err = device_create_file(dev,
1166 &vt1211_sysfs_temp_alarm[i].dev_attr))) {
1167 goto EXIT_DEV_REMOVE;
1168 }
1169 }
1170 }
1171 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1172 err = device_create_file(dev,
1173 &vt1211_sysfs_fan_pwm[i].dev_attr);
1174 if (err) {
1175 goto EXIT_DEV_REMOVE;
1176 }
1177 }
1178 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1179 err = device_create_file(dev,
1180 &vt1211_sysfs_misc[i]);
1181 if (err) {
1182 goto EXIT_DEV_REMOVE;
1183 }
1184 }
1185
1186 /* Register device */
1187 data->class_dev = hwmon_device_register(dev);
1188 if (IS_ERR(data->class_dev)) {
1189 err = PTR_ERR(data->class_dev);
1190 dev_err(dev, "Class registration failed (%d)\n", err);
1191 goto EXIT_DEV_REMOVE_SILENT;
1192 }
1193
1194 return 0;
1195
1196EXIT_DEV_REMOVE:
1197 dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1198EXIT_DEV_REMOVE_SILENT:
1199 vt1211_remove_sysfs(pdev);
1200 platform_set_drvdata(pdev, NULL);
1201 kfree(data);
1202EXIT:
1203 return err;
1204}
1205
1206static int __devexit vt1211_remove(struct platform_device *pdev)
1207{
1208 struct vt1211_data *data = platform_get_drvdata(pdev);
1209
1210 hwmon_device_unregister(data->class_dev);
1211 vt1211_remove_sysfs(pdev);
1212 platform_set_drvdata(pdev, NULL);
1213 kfree(data);
1214
1215 return 0;
1216}
1217
1218static struct platform_driver vt1211_driver = {
1219 .driver = {
1220 .owner = THIS_MODULE,
1221 .name = DRVNAME,
1222 },
1223 .probe = vt1211_probe,
1224 .remove = __devexit_p(vt1211_remove),
1225};
1226
1227static int __init vt1211_device_add(unsigned short address)
1228{
1229 struct resource res = {
1230 .start = address,
1231 .end = address + 0x7f,
1232 .flags = IORESOURCE_IO,
1233 };
1234 int err;
1235
1236 pdev = platform_device_alloc(DRVNAME, address);
1237 if (!pdev) {
1238 err = -ENOMEM;
1239 printk(KERN_ERR DRVNAME ": Device allocation failed (%d)\n",
1240 err);
1241 goto EXIT;
1242 }
1243
1244 res.name = pdev->name;
1245 err = platform_device_add_resources(pdev, &res, 1);
1246 if (err) {
1247 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1248 "(%d)\n", err);
1249 goto EXIT_DEV_PUT;
1250 }
1251
1252 err = platform_device_add(pdev);
1253 if (err) {
1254 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1255 err);
1256 goto EXIT_DEV_PUT;
1257 }
1258
1259 return 0;
1260
1261EXIT_DEV_PUT:
1262 platform_device_put(pdev);
1263EXIT:
1264 return err;
1265}
1266
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001267static int __init vt1211_find(int sio_cip, unsigned short *address)
Juerg Haefligerab413192006-09-24 20:54:04 +02001268{
1269 int err = -ENODEV;
1270
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001271 superio_enter(sio_cip);
Juerg Haefligerab413192006-09-24 20:54:04 +02001272
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001273 if (superio_inb(sio_cip, SIO_VT1211_DEVID) != SIO_VT1211_ID) {
Juerg Haefligerab413192006-09-24 20:54:04 +02001274 goto EXIT;
1275 }
1276
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001277 superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
Juerg Haefligerab413192006-09-24 20:54:04 +02001278
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001279 if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
Juerg Haefligerab413192006-09-24 20:54:04 +02001280 printk(KERN_WARNING DRVNAME ": HW monitor is disabled, "
1281 "skipping\n");
1282 goto EXIT;
1283 }
1284
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001285 *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1286 (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
Juerg Haefligerab413192006-09-24 20:54:04 +02001287 if (*address == 0) {
1288 printk(KERN_WARNING DRVNAME ": Base address is not set, "
1289 "skipping\n");
1290 goto EXIT;
1291 }
1292
1293 err = 0;
1294 printk(KERN_INFO DRVNAME ": Found VT1211 chip at 0x%04x, "
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001295 "revision %u\n", *address,
1296 superio_inb(sio_cip, SIO_VT1211_DEVREV));
Juerg Haefligerab413192006-09-24 20:54:04 +02001297
1298EXIT:
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001299 superio_exit(sio_cip);
Juerg Haefligerab413192006-09-24 20:54:04 +02001300 return err;
1301}
1302
1303static int __init vt1211_init(void)
1304{
1305 int err;
1306 unsigned short address = 0;
1307
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001308 if ((err = vt1211_find(SIO_REG_CIP1, &address)) &&
1309 (err = vt1211_find(SIO_REG_CIP2, &address))) {
Juerg Haefligerab413192006-09-24 20:54:04 +02001310 goto EXIT;
1311 }
1312
1313 if ((uch_config < -1) || (uch_config > 31)) {
1314 err = -EINVAL;
1315 printk(KERN_WARNING DRVNAME ": Invalid UCH configuration %d. "
1316 "Choose a value between 0 and 31.\n", uch_config);
1317 goto EXIT;
1318 }
1319
1320 if ((int_mode < -1) || (int_mode > 0)) {
1321 err = -EINVAL;
1322 printk(KERN_WARNING DRVNAME ": Invalid interrupt mode %d. "
1323 "Only mode 0 is supported.\n", int_mode);
1324 goto EXIT;
1325 }
1326
1327 err = platform_driver_register(&vt1211_driver);
1328 if (err) {
1329 goto EXIT;
1330 }
1331
1332 /* Sets global pdev as a side effect */
1333 err = vt1211_device_add(address);
1334 if (err) {
1335 goto EXIT_DRV_UNREGISTER;
1336 }
1337
1338 return 0;
1339
1340EXIT_DRV_UNREGISTER:
1341 platform_driver_unregister(&vt1211_driver);
1342EXIT:
1343 return err;
1344}
1345
1346static void __exit vt1211_exit(void)
1347{
1348 platform_device_unregister(pdev);
1349 platform_driver_unregister(&vt1211_driver);
1350}
1351
1352MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1353MODULE_DESCRIPTION("VT1211 sensors");
1354MODULE_LICENSE("GPL");
1355
1356module_init(vt1211_init);
1357module_exit(vt1211_exit);