blob: 95d5e8ec8b7fcbf84275e9ca15476ab3ecee0e22 [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
Joe Perches5ed9ba62010-10-20 06:51:52 +000024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Juerg Haefligerab413192006-09-24 20:54:04 +020026#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>
Jean Delvarece7ee4e2007-05-08 17:21:59 +020036#include <linux/ioport.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010037#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020038#include <linux/io.h>
Juerg Haefligerab413192006-09-24 20:54:04 +020039
40static int uch_config = -1;
41module_param(uch_config, int, 0);
42MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
43
44static int int_mode = -1;
45module_param(int_mode, int, 0);
46MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
47
Jean Delvare67b671b2007-12-06 23:13:42 +010048static unsigned short force_id;
49module_param(force_id, ushort, 0);
50MODULE_PARM_DESC(force_id, "Override the detected device ID");
51
Juerg Haefligerab413192006-09-24 20:54:04 +020052static struct platform_device *pdev;
53
54#define DRVNAME "vt1211"
55
56/* ---------------------------------------------------------------------
57 * Registers
58 *
59 * The sensors are defined as follows.
60 *
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)
71 *
72 * --------------------------------------------------------------------- */
73
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))
78
79/* Temperatures (temp) numbered 0-6 (ix) */
80static u8 regtemp[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
81static u8 regtempmax[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
82static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
83
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
88
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))
96
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
105
106/* In, temp & fan alarm bits */
107static const u8 bitalarmin[] = {11, 0, 1, 3, 8, 2, 9};
108static const u8 bitalarmtemp[] = {4, 15, 11, 0, 1, 3, 8};
109static const u8 bitalarmfan[] = {6, 7};
110
111/* ---------------------------------------------------------------------
112 * Data structures and manipulation thereof
113 * --------------------------------------------------------------------- */
114
115struct vt1211_data {
116 unsigned short addr;
117 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700118 struct device *hwmon_dev;
Juerg Haefligerab413192006-09-24 20:54:04 +0200119
120 struct mutex update_lock;
121 char valid; /* !=0 if following fields are valid */
122 unsigned long last_updated; /* In jiffies */
123
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;
144};
145
146/* ix = [0-5] */
147#define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \
148 !(((uch_config) >> ((ix) + 2)) & 1))
149
150/* ix = [0-6] */
151#define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \
152 ((uch_config) >> (ix)) & 1)
153
Guenter Roeckb162c032012-01-15 06:52:33 -0800154/*
155 * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
156 * driver according to the VT1211 BIOS porting guide
157 */
Juerg Haefligerab413192006-09-24 20:54:04 +0200158#define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \
159 (((reg) - 3) * 15882 + 479) / 958 : \
160 (((reg) - 3) * 10000 + 479) / 958)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800161#define IN_TO_REG(ix, val) (clamp_val((ix) == 5 ? \
Juerg Haefligerab413192006-09-24 20:54:04 +0200162 ((val) * 958 + 7941) / 15882 + 3 : \
163 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
164
Guenter Roeckb162c032012-01-15 06:52:33 -0800165/*
166 * temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
167 * temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
168 * according to some measurements that I took on an EPIA M10000.
169 * temp3-7 are thermistor based so the driver returns the voltage measured at
170 * the pin (range 0V - 2.2V).
171 */
Juerg Haefligerab413192006-09-24 20:54:04 +0200172#define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \
173 (ix) == 1 ? (reg) < 51 ? 0 : \
174 ((reg) - 51) * 1000 : \
175 ((253 - (reg)) * 2200 + 105) / 210)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800176#define TEMP_TO_REG(ix, val) clamp_val( \
Juerg Haefligerab413192006-09-24 20:54:04 +0200177 ((ix) == 0 ? ((val) + 500) / 1000 : \
178 (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
179 253 - ((val) * 210 + 1100) / 2200), 0, 255)
180
181#define DIV_FROM_REG(reg) (1 << (reg))
182
183#define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \
184 1310720 / (reg) / DIV_FROM_REG(div))
185#define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800186 clamp_val((1310720 / (val) / \
Juerg Haefligerab413192006-09-24 20:54:04 +0200187 DIV_FROM_REG(div)), 1, 254))
188
189/* ---------------------------------------------------------------------
190 * Super-I/O constants and functions
191 * --------------------------------------------------------------------- */
192
Guenter Roeckb162c032012-01-15 06:52:33 -0800193/*
194 * Configuration index port registers
195 * The vt1211 can live at 2 different addresses so we need to probe both
196 */
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100197#define SIO_REG_CIP1 0x2e
198#define SIO_REG_CIP2 0x4e
Juerg Haefligerab413192006-09-24 20:54:04 +0200199
200/* Configuration registers */
201#define SIO_VT1211_LDN 0x07 /* logical device number */
202#define SIO_VT1211_DEVID 0x20 /* device ID */
203#define SIO_VT1211_DEVREV 0x21 /* device revision */
204#define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
205#define SIO_VT1211_BADDR 0x60 /* base I/O address */
206#define SIO_VT1211_ID 0x3c /* VT1211 device ID */
207
208/* VT1211 logical device numbers */
209#define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
210
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100211static inline void superio_outb(int sio_cip, int reg, int val)
Juerg Haefligerab413192006-09-24 20:54:04 +0200212{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100213 outb(reg, sio_cip);
214 outb(val, sio_cip + 1);
Juerg Haefligerab413192006-09-24 20:54:04 +0200215}
216
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100217static inline int superio_inb(int sio_cip, int reg)
Juerg Haefligerab413192006-09-24 20:54:04 +0200218{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100219 outb(reg, sio_cip);
220 return inb(sio_cip + 1);
Juerg Haefligerab413192006-09-24 20:54:04 +0200221}
222
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100223static inline void superio_select(int sio_cip, int ldn)
Juerg Haefligerab413192006-09-24 20:54:04 +0200224{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100225 outb(SIO_VT1211_LDN, sio_cip);
226 outb(ldn, sio_cip + 1);
Juerg Haefligerab413192006-09-24 20:54:04 +0200227}
228
Guenter Roeckf418d002019-04-05 08:53:08 -0700229static inline int superio_enter(int sio_cip)
Juerg Haefligerab413192006-09-24 20:54:04 +0200230{
Guenter Roeckf418d002019-04-05 08:53:08 -0700231 if (!request_muxed_region(sio_cip, 2, DRVNAME))
232 return -EBUSY;
233
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100234 outb(0x87, sio_cip);
235 outb(0x87, sio_cip);
Guenter Roeckf418d002019-04-05 08:53:08 -0700236
237 return 0;
Juerg Haefligerab413192006-09-24 20:54:04 +0200238}
239
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100240static inline void superio_exit(int sio_cip)
Juerg Haefligerab413192006-09-24 20:54:04 +0200241{
Juerg Haefliger2219cd82007-02-14 21:15:05 +0100242 outb(0xaa, sio_cip);
Guenter Roeckf418d002019-04-05 08:53:08 -0700243 release_region(sio_cip, 2);
Juerg Haefligerab413192006-09-24 20:54:04 +0200244}
245
246/* ---------------------------------------------------------------------
247 * Device I/O access
248 * --------------------------------------------------------------------- */
249
250static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
251{
252 return inb(data->addr + reg);
253}
254
255static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
256{
257 outb(val, data->addr + reg);
258}
259
260static struct vt1211_data *vt1211_update_device(struct device *dev)
261{
262 struct vt1211_data *data = dev_get_drvdata(dev);
263 int ix, val;
264
265 mutex_lock(&data->update_lock);
266
267 /* registers cache is refreshed after 1 second */
268 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
269 /* read VID */
270 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
271
272 /* voltage (in) registers */
273 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
274 if (ISVOLT(ix, data->uch_config)) {
275 data->in[ix] = vt1211_read8(data,
276 VT1211_REG_IN(ix));
277 data->in_min[ix] = vt1211_read8(data,
278 VT1211_REG_IN_MIN(ix));
279 data->in_max[ix] = vt1211_read8(data,
280 VT1211_REG_IN_MAX(ix));
281 }
282 }
283
284 /* temp registers */
285 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
286 if (ISTEMP(ix, data->uch_config)) {
287 data->temp[ix] = vt1211_read8(data,
288 regtemp[ix]);
289 data->temp_max[ix] = vt1211_read8(data,
290 regtempmax[ix]);
291 data->temp_hyst[ix] = vt1211_read8(data,
292 regtemphyst[ix]);
293 }
294 }
295
296 /* fan & pwm registers */
297 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
298 data->fan[ix] = vt1211_read8(data,
299 VT1211_REG_FAN(ix));
300 data->fan_min[ix] = vt1211_read8(data,
301 VT1211_REG_FAN_MIN(ix));
302 data->pwm[ix] = vt1211_read8(data,
303 VT1211_REG_PWM(ix));
304 }
305 val = vt1211_read8(data, VT1211_REG_FAN_DIV);
306 data->fan_div[0] = (val >> 4) & 3;
307 data->fan_div[1] = (val >> 6) & 3;
308 data->fan_ctl = val & 0xf;
309
310 val = vt1211_read8(data, VT1211_REG_PWM_CTL);
311 data->pwm_ctl[0] = val & 0xf;
312 data->pwm_ctl[1] = (val >> 4) & 0xf;
313
314 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
315
316 /* pwm & temp auto point registers */
317 data->pwm_auto_pwm[0][1] = vt1211_read8(data,
318 VT1211_REG_PWM_AUTO_PWM(0, 1));
319 data->pwm_auto_pwm[0][2] = vt1211_read8(data,
320 VT1211_REG_PWM_AUTO_PWM(0, 2));
321 data->pwm_auto_pwm[1][1] = vt1211_read8(data,
322 VT1211_REG_PWM_AUTO_PWM(1, 1));
323 data->pwm_auto_pwm[1][2] = vt1211_read8(data,
324 VT1211_REG_PWM_AUTO_PWM(1, 2));
325 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
326 data->pwm_auto_temp[ix] = vt1211_read8(data,
327 VT1211_REG_PWM_AUTO_TEMP(ix));
328 }
329
330 /* alarm registers */
331 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
332 vt1211_read8(data, VT1211_REG_ALARM1);
333
334 data->last_updated = jiffies;
335 data->valid = 1;
336 }
337
338 mutex_unlock(&data->update_lock);
339
340 return data;
341}
342
343/* ---------------------------------------------------------------------
344 * Voltage sysfs interfaces
345 * ix = [0-5]
346 * --------------------------------------------------------------------- */
347
348#define SHOW_IN_INPUT 0
349#define SHOW_SET_IN_MIN 1
350#define SHOW_SET_IN_MAX 2
351#define SHOW_IN_ALARM 3
352
353static ssize_t show_in(struct device *dev, struct device_attribute *attr,
354 char *buf)
355{
356 struct vt1211_data *data = vt1211_update_device(dev);
357 struct sensor_device_attribute_2 *sensor_attr_2 =
358 to_sensor_dev_attr_2(attr);
359 int ix = sensor_attr_2->index;
360 int fn = sensor_attr_2->nr;
361 int res;
362
363 switch (fn) {
364 case SHOW_IN_INPUT:
365 res = IN_FROM_REG(ix, data->in[ix]);
366 break;
367 case SHOW_SET_IN_MIN:
368 res = IN_FROM_REG(ix, data->in_min[ix]);
369 break;
370 case SHOW_SET_IN_MAX:
371 res = IN_FROM_REG(ix, data->in_max[ix]);
372 break;
373 case SHOW_IN_ALARM:
374 res = (data->alarms >> bitalarmin[ix]) & 1;
375 break;
376 default:
377 res = 0;
378 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
379 }
380
381 return sprintf(buf, "%d\n", res);
382}
383
384static ssize_t set_in(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 struct vt1211_data *data = dev_get_drvdata(dev);
388 struct sensor_device_attribute_2 *sensor_attr_2 =
389 to_sensor_dev_attr_2(attr);
390 int ix = sensor_attr_2->index;
391 int fn = sensor_attr_2->nr;
Guenter Roeckb162c032012-01-15 06:52:33 -0800392 long val;
393 int err;
394
395 err = kstrtol(buf, 10, &val);
396 if (err)
397 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200398
399 mutex_lock(&data->update_lock);
400 switch (fn) {
401 case SHOW_SET_IN_MIN:
402 data->in_min[ix] = IN_TO_REG(ix, val);
403 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
404 break;
405 case SHOW_SET_IN_MAX:
406 data->in_max[ix] = IN_TO_REG(ix, val);
407 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
408 break;
409 default:
410 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
411 }
412 mutex_unlock(&data->update_lock);
413
414 return count;
415}
416
417/* ---------------------------------------------------------------------
418 * Temperature sysfs interfaces
419 * ix = [0-6]
420 * --------------------------------------------------------------------- */
421
422#define SHOW_TEMP_INPUT 0
423#define SHOW_SET_TEMP_MAX 1
424#define SHOW_SET_TEMP_MAX_HYST 2
425#define SHOW_TEMP_ALARM 3
426
427static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
428 char *buf)
429{
430 struct vt1211_data *data = vt1211_update_device(dev);
431 struct sensor_device_attribute_2 *sensor_attr_2 =
432 to_sensor_dev_attr_2(attr);
433 int ix = sensor_attr_2->index;
434 int fn = sensor_attr_2->nr;
435 int res;
436
437 switch (fn) {
438 case SHOW_TEMP_INPUT:
439 res = TEMP_FROM_REG(ix, data->temp[ix]);
440 break;
441 case SHOW_SET_TEMP_MAX:
442 res = TEMP_FROM_REG(ix, data->temp_max[ix]);
443 break;
444 case SHOW_SET_TEMP_MAX_HYST:
445 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
446 break;
447 case SHOW_TEMP_ALARM:
448 res = (data->alarms >> bitalarmtemp[ix]) & 1;
449 break;
450 default:
451 res = 0;
452 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
453 }
454
455 return sprintf(buf, "%d\n", res);
456}
457
458static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
459 const char *buf, size_t count)
460{
461 struct vt1211_data *data = dev_get_drvdata(dev);
462 struct sensor_device_attribute_2 *sensor_attr_2 =
463 to_sensor_dev_attr_2(attr);
464 int ix = sensor_attr_2->index;
465 int fn = sensor_attr_2->nr;
Guenter Roeckb162c032012-01-15 06:52:33 -0800466 long val;
467 int err;
468
469 err = kstrtol(buf, 10, &val);
470 if (err)
471 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200472
473 mutex_lock(&data->update_lock);
474 switch (fn) {
475 case SHOW_SET_TEMP_MAX:
476 data->temp_max[ix] = TEMP_TO_REG(ix, val);
477 vt1211_write8(data, regtempmax[ix],
478 data->temp_max[ix]);
479 break;
480 case SHOW_SET_TEMP_MAX_HYST:
481 data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
482 vt1211_write8(data, regtemphyst[ix],
483 data->temp_hyst[ix]);
484 break;
485 default:
486 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
487 }
488 mutex_unlock(&data->update_lock);
489
490 return count;
491}
492
493/* ---------------------------------------------------------------------
494 * Fan sysfs interfaces
495 * ix = [0-1]
496 * --------------------------------------------------------------------- */
497
498#define SHOW_FAN_INPUT 0
499#define SHOW_SET_FAN_MIN 1
500#define SHOW_SET_FAN_DIV 2
501#define SHOW_FAN_ALARM 3
502
503static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
504 char *buf)
505{
506 struct vt1211_data *data = vt1211_update_device(dev);
507 struct sensor_device_attribute_2 *sensor_attr_2 =
508 to_sensor_dev_attr_2(attr);
509 int ix = sensor_attr_2->index;
510 int fn = sensor_attr_2->nr;
511 int res;
512
513 switch (fn) {
514 case SHOW_FAN_INPUT:
515 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
516 break;
517 case SHOW_SET_FAN_MIN:
518 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
519 break;
520 case SHOW_SET_FAN_DIV:
521 res = DIV_FROM_REG(data->fan_div[ix]);
522 break;
523 case SHOW_FAN_ALARM:
524 res = (data->alarms >> bitalarmfan[ix]) & 1;
525 break;
526 default:
527 res = 0;
528 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
529 }
530
531 return sprintf(buf, "%d\n", res);
532}
533
534static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
536{
537 struct vt1211_data *data = dev_get_drvdata(dev);
538 struct sensor_device_attribute_2 *sensor_attr_2 =
539 to_sensor_dev_attr_2(attr);
540 int ix = sensor_attr_2->index;
541 int fn = sensor_attr_2->nr;
Juerg Haefligerab413192006-09-24 20:54:04 +0200542 int reg;
Guenter Roeckb162c032012-01-15 06:52:33 -0800543 unsigned long val;
544 int err;
545
546 err = kstrtoul(buf, 10, &val);
547 if (err)
548 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200549
550 mutex_lock(&data->update_lock);
551
552 /* sync the data cache */
553 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
554 data->fan_div[0] = (reg >> 4) & 3;
555 data->fan_div[1] = (reg >> 6) & 3;
556 data->fan_ctl = reg & 0xf;
557
558 switch (fn) {
559 case SHOW_SET_FAN_MIN:
560 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
561 vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
562 data->fan_min[ix]);
563 break;
564 case SHOW_SET_FAN_DIV:
565 switch (val) {
Guenter Roeckb162c032012-01-15 06:52:33 -0800566 case 1:
567 data->fan_div[ix] = 0;
568 break;
569 case 2:
570 data->fan_div[ix] = 1;
571 break;
572 case 4:
573 data->fan_div[ix] = 2;
574 break;
575 case 8:
576 data->fan_div[ix] = 3;
577 break;
578 default:
579 count = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -0800580 dev_warn(dev,
581 "fan div value %ld not supported. Choose one of 1, 2, 4, or 8.\n",
582 val);
Guenter Roeckb162c032012-01-15 06:52:33 -0800583 goto EXIT;
Juerg Haefligerab413192006-09-24 20:54:04 +0200584 }
585 vt1211_write8(data, VT1211_REG_FAN_DIV,
586 ((data->fan_div[1] << 6) |
587 (data->fan_div[0] << 4) |
588 data->fan_ctl));
589 break;
590 default:
591 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
592 }
593
594EXIT:
595 mutex_unlock(&data->update_lock);
596 return count;
597}
598
599/* ---------------------------------------------------------------------
600 * PWM sysfs interfaces
601 * ix = [0-1]
602 * --------------------------------------------------------------------- */
603
604#define SHOW_PWM 0
605#define SHOW_SET_PWM_ENABLE 1
606#define SHOW_SET_PWM_FREQ 2
607#define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
608
609static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
610 char *buf)
611{
612 struct vt1211_data *data = vt1211_update_device(dev);
613 struct sensor_device_attribute_2 *sensor_attr_2 =
614 to_sensor_dev_attr_2(attr);
615 int ix = sensor_attr_2->index;
616 int fn = sensor_attr_2->nr;
617 int res;
618
619 switch (fn) {
620 case SHOW_PWM:
621 res = data->pwm[ix];
622 break;
623 case SHOW_SET_PWM_ENABLE:
624 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
625 break;
626 case SHOW_SET_PWM_FREQ:
627 res = 90000 >> (data->pwm_clk & 7);
628 break;
629 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
630 res = (data->pwm_ctl[ix] & 7) + 1;
631 break;
632 default:
633 res = 0;
634 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
635 }
636
637 return sprintf(buf, "%d\n", res);
638}
639
640static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
642{
643 struct vt1211_data *data = dev_get_drvdata(dev);
644 struct sensor_device_attribute_2 *sensor_attr_2 =
645 to_sensor_dev_attr_2(attr);
646 int ix = sensor_attr_2->index;
647 int fn = sensor_attr_2->nr;
Juerg Haefligerab413192006-09-24 20:54:04 +0200648 int tmp, reg;
Guenter Roeckb162c032012-01-15 06:52:33 -0800649 unsigned long val;
650 int err;
651
652 err = kstrtoul(buf, 10, &val);
653 if (err)
654 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200655
656 mutex_lock(&data->update_lock);
657
658 switch (fn) {
659 case SHOW_SET_PWM_ENABLE:
660 /* sync the data cache */
661 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
662 data->fan_div[0] = (reg >> 4) & 3;
663 data->fan_div[1] = (reg >> 6) & 3;
664 data->fan_ctl = reg & 0xf;
665 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
666 data->pwm_ctl[0] = reg & 0xf;
667 data->pwm_ctl[1] = (reg >> 4) & 0xf;
668 switch (val) {
669 case 0:
670 data->pwm_ctl[ix] &= 7;
Guenter Roeckb162c032012-01-15 06:52:33 -0800671 /*
672 * disable SmartGuardian if both PWM outputs are
673 * disabled
674 */
675 if ((data->pwm_ctl[ix ^ 1] & 1) == 0)
Juerg Haefligerab413192006-09-24 20:54:04 +0200676 data->fan_ctl &= 0xe;
Juerg Haefligerab413192006-09-24 20:54:04 +0200677 break;
678 case 2:
679 data->pwm_ctl[ix] |= 8;
680 data->fan_ctl |= 1;
681 break;
682 default:
683 count = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -0800684 dev_warn(dev,
685 "pwm mode %ld not supported. Choose one of 0 or 2.\n",
686 val);
Juerg Haefligerab413192006-09-24 20:54:04 +0200687 goto EXIT;
688 }
689 vt1211_write8(data, VT1211_REG_PWM_CTL,
690 ((data->pwm_ctl[1] << 4) |
691 data->pwm_ctl[0]));
692 vt1211_write8(data, VT1211_REG_FAN_DIV,
693 ((data->fan_div[1] << 6) |
694 (data->fan_div[0] << 4) |
695 data->fan_ctl));
696 break;
697 case SHOW_SET_PWM_FREQ:
Guenter Roeck2a844c12013-01-09 08:09:34 -0800698 val = 135000 / clamp_val(val, 135000 >> 7, 135000);
Juerg Haefligerab413192006-09-24 20:54:04 +0200699 /* calculate tmp = log2(val) */
700 tmp = 0;
Guenter Roeckb162c032012-01-15 06:52:33 -0800701 for (val >>= 1; val > 0; val >>= 1)
Juerg Haefligerab413192006-09-24 20:54:04 +0200702 tmp++;
Juerg Haefligerab413192006-09-24 20:54:04 +0200703 /* sync the data cache */
704 reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
705 data->pwm_clk = (reg & 0xf8) | tmp;
706 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
707 break;
708 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
Guenter Roeckb162c032012-01-15 06:52:33 -0800709 if (val < 1 || val > 7) {
Juerg Haefligerab413192006-09-24 20:54:04 +0200710 count = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -0800711 dev_warn(dev,
712 "temp channel %ld not supported. Choose a value between 1 and 7.\n",
713 val);
Juerg Haefligerab413192006-09-24 20:54:04 +0200714 goto EXIT;
715 }
716 if (!ISTEMP(val - 1, data->uch_config)) {
717 count = -EINVAL;
718 dev_warn(dev, "temp channel %ld is not available.\n",
719 val);
720 goto EXIT;
721 }
722 /* sync the data cache */
723 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
724 data->pwm_ctl[0] = reg & 0xf;
725 data->pwm_ctl[1] = (reg >> 4) & 0xf;
726 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
727 vt1211_write8(data, VT1211_REG_PWM_CTL,
728 ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
729 break;
730 default:
731 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
732 }
733
734EXIT:
735 mutex_unlock(&data->update_lock);
736 return count;
737}
738
739/* ---------------------------------------------------------------------
740 * PWM auto point definitions
741 * ix = [0-1]
742 * ap = [0-3]
743 * --------------------------------------------------------------------- */
744
745/*
746 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
747 * Note that there is only a single set of temp auto points that controls both
748 * PWM controllers. We still create 2 sets of sysfs files to make it look
749 * more consistent even though they map to the same registers.
750 *
751 * ix ap : description
752 * -------------------
753 * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
754 * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
755 * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
756 * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
757 * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
758 * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
759 * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
760 * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
761 */
762
763static ssize_t show_pwm_auto_point_temp(struct device *dev,
764 struct device_attribute *attr,
765 char *buf)
766{
767 struct vt1211_data *data = vt1211_update_device(dev);
768 struct sensor_device_attribute_2 *sensor_attr_2 =
769 to_sensor_dev_attr_2(attr);
770 int ix = sensor_attr_2->index;
771 int ap = sensor_attr_2->nr;
772
773 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
774 data->pwm_auto_temp[ap]));
775}
776
777static ssize_t set_pwm_auto_point_temp(struct device *dev,
778 struct device_attribute *attr,
779 const char *buf, size_t count)
780{
781 struct vt1211_data *data = dev_get_drvdata(dev);
782 struct sensor_device_attribute_2 *sensor_attr_2 =
783 to_sensor_dev_attr_2(attr);
784 int ix = sensor_attr_2->index;
785 int ap = sensor_attr_2->nr;
Juerg Haefligerab413192006-09-24 20:54:04 +0200786 int reg;
Guenter Roeckb162c032012-01-15 06:52:33 -0800787 long val;
788 int err;
789
790 err = kstrtol(buf, 10, &val);
791 if (err)
792 return err;
793
Juerg Haefligerab413192006-09-24 20:54:04 +0200794
795 mutex_lock(&data->update_lock);
796
797 /* sync the data cache */
798 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
799 data->pwm_ctl[0] = reg & 0xf;
800 data->pwm_ctl[1] = (reg >> 4) & 0xf;
801
802 data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
803 vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
804 data->pwm_auto_temp[ap]);
805 mutex_unlock(&data->update_lock);
806
807 return count;
808}
809
810/*
811 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
812 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
813 * be changed.
814 *
815 * ix ap : description
816 * -------------------
817 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
818 * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
819 * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
820 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
821 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
822 * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
823 * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
824 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
Guenter Roeckb162c032012-01-15 06:52:33 -0800825 */
Juerg Haefligerab413192006-09-24 20:54:04 +0200826
827static ssize_t show_pwm_auto_point_pwm(struct device *dev,
828 struct device_attribute *attr,
829 char *buf)
830{
831 struct vt1211_data *data = vt1211_update_device(dev);
832 struct sensor_device_attribute_2 *sensor_attr_2 =
833 to_sensor_dev_attr_2(attr);
834 int ix = sensor_attr_2->index;
835 int ap = sensor_attr_2->nr;
836
837 return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
838}
839
840static ssize_t set_pwm_auto_point_pwm(struct device *dev,
841 struct device_attribute *attr,
842 const char *buf, size_t count)
843{
844 struct vt1211_data *data = dev_get_drvdata(dev);
845 struct sensor_device_attribute_2 *sensor_attr_2 =
846 to_sensor_dev_attr_2(attr);
847 int ix = sensor_attr_2->index;
848 int ap = sensor_attr_2->nr;
Guenter Roeckb162c032012-01-15 06:52:33 -0800849 unsigned long val;
850 int err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200851
Guenter Roeckb162c032012-01-15 06:52:33 -0800852 err = kstrtoul(buf, 10, &val);
853 if (err)
854 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200855
856 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800857 data->pwm_auto_pwm[ix][ap] = clamp_val(val, 0, 255);
Juerg Haefligerab413192006-09-24 20:54:04 +0200858 vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
859 data->pwm_auto_pwm[ix][ap]);
860 mutex_unlock(&data->update_lock);
861
862 return count;
863}
864
865/* ---------------------------------------------------------------------
866 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
867 * --------------------------------------------------------------------- */
868
869static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
870 char *buf)
871{
872 struct vt1211_data *data = dev_get_drvdata(dev);
873
874 return sprintf(buf, "%d\n", data->vrm);
875}
876
877static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
878 const char *buf, size_t count)
879{
880 struct vt1211_data *data = dev_get_drvdata(dev);
Guenter Roeckb162c032012-01-15 06:52:33 -0800881 unsigned long val;
882 int err;
883
884 err = kstrtoul(buf, 10, &val);
885 if (err)
886 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +0200887
Axel Lin5c570b92014-08-06 08:25:44 +0800888 if (val > 255)
889 return -EINVAL;
890
Juerg Haefligerab413192006-09-24 20:54:04 +0200891 data->vrm = val;
892
893 return count;
894}
895
896static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
897 char *buf)
898{
899 struct vt1211_data *data = dev_get_drvdata(dev);
900
901 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
902}
903
904static ssize_t show_name(struct device *dev,
905 struct device_attribute *attr, char *buf)
906{
907 struct vt1211_data *data = dev_get_drvdata(dev);
908
909 return sprintf(buf, "%s\n", data->name);
910}
911
912static ssize_t show_alarms(struct device *dev,
913 struct device_attribute *attr, char *buf)
914{
915 struct vt1211_data *data = vt1211_update_device(dev);
916
917 return sprintf(buf, "%d\n", data->alarms);
918}
919
920/* ---------------------------------------------------------------------
921 * Device attribute structs
922 * --------------------------------------------------------------------- */
923
Guenter Roeck21856962012-01-19 21:49:20 -0800924#define SENSOR_ATTR_IN(ix) \
925{ SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
926 show_in, NULL, SHOW_IN_INPUT, ix), \
Juerg Haefligerab413192006-09-24 20:54:04 +0200927 SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
Guenter Roeck21856962012-01-19 21:49:20 -0800928 show_in, set_in, SHOW_SET_IN_MIN, ix), \
Juerg Haefligerab413192006-09-24 20:54:04 +0200929 SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
Guenter Roeck21856962012-01-19 21:49:20 -0800930 show_in, set_in, SHOW_SET_IN_MAX, ix), \
Juerg Haefligerab413192006-09-24 20:54:04 +0200931 SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
Guenter Roeck21856962012-01-19 21:49:20 -0800932 show_in, NULL, SHOW_IN_ALARM, ix) \
933}
Juerg Haefligerab413192006-09-24 20:54:04 +0200934
Guenter Roeck21856962012-01-19 21:49:20 -0800935static struct sensor_device_attribute_2 vt1211_sysfs_in[][4] = {
936 SENSOR_ATTR_IN(0),
937 SENSOR_ATTR_IN(1),
938 SENSOR_ATTR_IN(2),
939 SENSOR_ATTR_IN(3),
940 SENSOR_ATTR_IN(4),
941 SENSOR_ATTR_IN(5)
Juerg Haefligerab413192006-09-24 20:54:04 +0200942};
943
Guenter Roeck21856962012-01-19 21:49:20 -0800944#define IN_UNIT_ATTRS(X) \
945{ &vt1211_sysfs_in[X][0].dev_attr.attr, \
946 &vt1211_sysfs_in[X][1].dev_attr.attr, \
947 &vt1211_sysfs_in[X][2].dev_attr.attr, \
948 &vt1211_sysfs_in[X][3].dev_attr.attr, \
949 NULL \
950}
Juerg Haefligerab413192006-09-24 20:54:04 +0200951
Guenter Roeck21856962012-01-19 21:49:20 -0800952static struct attribute *vt1211_in_attr[][5] = {
953 IN_UNIT_ATTRS(0),
954 IN_UNIT_ATTRS(1),
955 IN_UNIT_ATTRS(2),
956 IN_UNIT_ATTRS(3),
957 IN_UNIT_ATTRS(4),
958 IN_UNIT_ATTRS(5)
Juerg Haefligerab413192006-09-24 20:54:04 +0200959};
960
Guenter Roeck21856962012-01-19 21:49:20 -0800961static const struct attribute_group vt1211_in_attr_group[] = {
962 { .attrs = vt1211_in_attr[0] },
963 { .attrs = vt1211_in_attr[1] },
964 { .attrs = vt1211_in_attr[2] },
965 { .attrs = vt1211_in_attr[3] },
966 { .attrs = vt1211_in_attr[4] },
967 { .attrs = vt1211_in_attr[5] }
968};
969
970#define SENSOR_ATTR_TEMP(ix) \
971{ SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
972 show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \
Juerg Haefligerab413192006-09-24 20:54:04 +0200973 SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
Guenter Roeck21856962012-01-19 21:49:20 -0800974 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \
Juerg Haefligerab413192006-09-24 20:54:04 +0200975 SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
Guenter Roeck21856962012-01-19 21:49:20 -0800976 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \
977 SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
978 show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \
979}
Juerg Haefligerab413192006-09-24 20:54:04 +0200980
Guenter Roeck21856962012-01-19 21:49:20 -0800981static struct sensor_device_attribute_2 vt1211_sysfs_temp[][4] = {
982 SENSOR_ATTR_TEMP(1),
983 SENSOR_ATTR_TEMP(2),
984 SENSOR_ATTR_TEMP(3),
985 SENSOR_ATTR_TEMP(4),
986 SENSOR_ATTR_TEMP(5),
987 SENSOR_ATTR_TEMP(6),
988 SENSOR_ATTR_TEMP(7),
Juerg Haefligerab413192006-09-24 20:54:04 +0200989};
990
Guenter Roeck21856962012-01-19 21:49:20 -0800991#define TEMP_UNIT_ATTRS(X) \
992{ &vt1211_sysfs_temp[X][0].dev_attr.attr, \
993 &vt1211_sysfs_temp[X][1].dev_attr.attr, \
994 &vt1211_sysfs_temp[X][2].dev_attr.attr, \
995 &vt1211_sysfs_temp[X][3].dev_attr.attr, \
996 NULL \
997}
Juerg Haefligerab413192006-09-24 20:54:04 +0200998
Guenter Roeck21856962012-01-19 21:49:20 -0800999static struct attribute *vt1211_temp_attr[][5] = {
1000 TEMP_UNIT_ATTRS(0),
1001 TEMP_UNIT_ATTRS(1),
1002 TEMP_UNIT_ATTRS(2),
1003 TEMP_UNIT_ATTRS(3),
1004 TEMP_UNIT_ATTRS(4),
1005 TEMP_UNIT_ATTRS(5),
1006 TEMP_UNIT_ATTRS(6)
1007};
1008
1009static const struct attribute_group vt1211_temp_attr_group[] = {
1010 { .attrs = vt1211_temp_attr[0] },
1011 { .attrs = vt1211_temp_attr[1] },
1012 { .attrs = vt1211_temp_attr[2] },
1013 { .attrs = vt1211_temp_attr[3] },
1014 { .attrs = vt1211_temp_attr[4] },
1015 { .attrs = vt1211_temp_attr[5] },
1016 { .attrs = vt1211_temp_attr[6] }
Juerg Haefligerab413192006-09-24 20:54:04 +02001017};
1018
1019#define SENSOR_ATTR_FAN(ix) \
1020 SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
1021 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
1022 SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1023 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
1024 SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
1025 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
1026 SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1027 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
1028
1029#define SENSOR_ATTR_PWM(ix) \
1030 SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
1031 show_pwm, NULL, SHOW_PWM, ix-1), \
1032 SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
1033 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
1034 SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
1035 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
1036
1037#define SENSOR_ATTR_PWM_FREQ(ix) \
1038 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1039 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
1040
1041#define SENSOR_ATTR_PWM_FREQ_RO(ix) \
1042 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1043 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1044
1045#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1046 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1047 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1048 ap-1, ix-1)
1049
1050#define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1051 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1052 show_pwm_auto_point_temp, NULL, \
1053 ap-1, ix-1)
1054
1055#define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1056 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1057 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1058 ap-1, ix-1)
1059
1060#define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1061 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1062 show_pwm_auto_point_pwm, NULL, \
1063 ap-1, ix-1)
1064
1065static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1066 SENSOR_ATTR_FAN(1),
1067 SENSOR_ATTR_FAN(2),
1068 SENSOR_ATTR_PWM(1),
1069 SENSOR_ATTR_PWM(2),
1070 SENSOR_ATTR_PWM_FREQ(1),
1071 SENSOR_ATTR_PWM_FREQ_RO(2),
1072 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1073 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1074 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1075 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1076 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1077 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1078 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1079 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1080 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1081 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1082 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1083 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1084 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1085 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1086 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1087 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1088};
1089
1090static struct device_attribute vt1211_sysfs_misc[] = {
1091 __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1092 __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1093 __ATTR(name, S_IRUGO, show_name, NULL),
1094 __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1095};
1096
1097/* ---------------------------------------------------------------------
1098 * Device registration and initialization
1099 * --------------------------------------------------------------------- */
1100
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001101static void vt1211_init_device(struct vt1211_data *data)
Juerg Haefligerab413192006-09-24 20:54:04 +02001102{
1103 /* set VRM */
1104 data->vrm = vid_which_vrm();
1105
1106 /* Read (and initialize) UCH config */
1107 data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1108 if (uch_config > -1) {
1109 data->uch_config = (data->uch_config & 0x83) |
1110 (uch_config << 2);
1111 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1112 }
1113
Guenter Roeckb162c032012-01-15 06:52:33 -08001114 /*
1115 * Initialize the interrupt mode (if request at module load time).
Juerg Haefligerab413192006-09-24 20:54:04 +02001116 * The VT1211 implements 3 different modes for clearing interrupts:
1117 * 0: Clear INT when status register is read. Regenerate INT as long
1118 * as temp stays above hysteresis limit.
1119 * 1: Clear INT when status register is read. DON'T regenerate INT
1120 * until temp falls below hysteresis limit and exceeds hot limit
1121 * again.
1122 * 2: Clear INT when temp falls below max limit.
1123 *
1124 * The driver only allows to force mode 0 since that's the only one
Guenter Roeckb162c032012-01-15 06:52:33 -08001125 * that makes sense for 'sensors'
1126 */
Juerg Haefligerab413192006-09-24 20:54:04 +02001127 if (int_mode == 0) {
1128 vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1129 vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1130 }
1131
1132 /* Fill in some hard wired values into our data struct */
1133 data->pwm_auto_pwm[0][3] = 255;
1134 data->pwm_auto_pwm[1][3] = 255;
1135}
1136
1137static void vt1211_remove_sysfs(struct platform_device *pdev)
1138{
1139 struct device *dev = &pdev->dev;
1140 int i;
1141
Guenter Roeck21856962012-01-19 21:49:20 -08001142 for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++)
1143 sysfs_remove_group(&dev->kobj, &vt1211_in_attr_group[i]);
1144
1145 for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++)
1146 sysfs_remove_group(&dev->kobj, &vt1211_temp_attr_group[i]);
1147
Juerg Haefligerab413192006-09-24 20:54:04 +02001148 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1149 device_remove_file(dev,
1150 &vt1211_sysfs_fan_pwm[i].dev_attr);
1151 }
Guenter Roeckb162c032012-01-15 06:52:33 -08001152 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++)
Juerg Haefligerab413192006-09-24 20:54:04 +02001153 device_remove_file(dev, &vt1211_sysfs_misc[i]);
Juerg Haefligerab413192006-09-24 20:54:04 +02001154}
1155
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001156static int vt1211_probe(struct platform_device *pdev)
Juerg Haefligerab413192006-09-24 20:54:04 +02001157{
1158 struct device *dev = &pdev->dev;
1159 struct vt1211_data *data;
1160 struct resource *res;
1161 int i, err;
1162
Guenter Roeck84269ed2012-06-02 11:35:54 -07001163 data = devm_kzalloc(dev, sizeof(struct vt1211_data), GFP_KERNEL);
Jingoo Han5b209952014-04-29 17:13:50 +09001164 if (!data)
Guenter Roeck84269ed2012-06-02 11:35:54 -07001165 return -ENOMEM;
Juerg Haefligerab413192006-09-24 20:54:04 +02001166
1167 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck84269ed2012-06-02 11:35:54 -07001168 if (!devm_request_region(dev, res->start, resource_size(res),
1169 DRVNAME)) {
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001170 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1171 (unsigned long)res->start, (unsigned long)res->end);
Guenter Roeck84269ed2012-06-02 11:35:54 -07001172 return -EBUSY;
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001173 }
Juerg Haefligerab413192006-09-24 20:54:04 +02001174 data->addr = res->start;
1175 data->name = DRVNAME;
1176 mutex_init(&data->update_lock);
1177
1178 platform_set_drvdata(pdev, data);
1179
1180 /* Initialize the VT1211 chip */
1181 vt1211_init_device(data);
1182
1183 /* Create sysfs interface files */
Guenter Roeck21856962012-01-19 21:49:20 -08001184 for (i = 0; i < ARRAY_SIZE(vt1211_in_attr_group); i++) {
Juerg Haefligerab413192006-09-24 20:54:04 +02001185 if (ISVOLT(i, data->uch_config)) {
Guenter Roeck21856962012-01-19 21:49:20 -08001186 err = sysfs_create_group(&dev->kobj,
1187 &vt1211_in_attr_group[i]);
1188 if (err)
Juerg Haefligerab413192006-09-24 20:54:04 +02001189 goto EXIT_DEV_REMOVE;
Juerg Haefligerab413192006-09-24 20:54:04 +02001190 }
1191 }
Guenter Roeck21856962012-01-19 21:49:20 -08001192 for (i = 0; i < ARRAY_SIZE(vt1211_temp_attr_group); i++) {
Juerg Haefligerab413192006-09-24 20:54:04 +02001193 if (ISTEMP(i, data->uch_config)) {
Guenter Roeck21856962012-01-19 21:49:20 -08001194 err = sysfs_create_group(&dev->kobj,
1195 &vt1211_temp_attr_group[i]);
1196 if (err)
Juerg Haefligerab413192006-09-24 20:54:04 +02001197 goto EXIT_DEV_REMOVE;
Juerg Haefligerab413192006-09-24 20:54:04 +02001198 }
1199 }
1200 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1201 err = device_create_file(dev,
1202 &vt1211_sysfs_fan_pwm[i].dev_attr);
Guenter Roeckb162c032012-01-15 06:52:33 -08001203 if (err)
Juerg Haefligerab413192006-09-24 20:54:04 +02001204 goto EXIT_DEV_REMOVE;
Juerg Haefligerab413192006-09-24 20:54:04 +02001205 }
1206 for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1207 err = device_create_file(dev,
1208 &vt1211_sysfs_misc[i]);
Guenter Roeckb162c032012-01-15 06:52:33 -08001209 if (err)
Juerg Haefligerab413192006-09-24 20:54:04 +02001210 goto EXIT_DEV_REMOVE;
Juerg Haefligerab413192006-09-24 20:54:04 +02001211 }
1212
1213 /* Register device */
Tony Jones1beeffe2007-08-20 13:46:20 -07001214 data->hwmon_dev = hwmon_device_register(dev);
1215 if (IS_ERR(data->hwmon_dev)) {
1216 err = PTR_ERR(data->hwmon_dev);
Juerg Haefligerab413192006-09-24 20:54:04 +02001217 dev_err(dev, "Class registration failed (%d)\n", err);
1218 goto EXIT_DEV_REMOVE_SILENT;
1219 }
1220
1221 return 0;
1222
1223EXIT_DEV_REMOVE:
1224 dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1225EXIT_DEV_REMOVE_SILENT:
1226 vt1211_remove_sysfs(pdev);
Juerg Haefligerab413192006-09-24 20:54:04 +02001227 return err;
1228}
1229
Bill Pemberton281dfd02012-11-19 13:25:51 -05001230static int vt1211_remove(struct platform_device *pdev)
Juerg Haefligerab413192006-09-24 20:54:04 +02001231{
1232 struct vt1211_data *data = platform_get_drvdata(pdev);
1233
Tony Jones1beeffe2007-08-20 13:46:20 -07001234 hwmon_device_unregister(data->hwmon_dev);
Juerg Haefligerab413192006-09-24 20:54:04 +02001235 vt1211_remove_sysfs(pdev);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001236
Juerg Haefligerab413192006-09-24 20:54:04 +02001237 return 0;
1238}
1239
1240static struct platform_driver vt1211_driver = {
1241 .driver = {
Juerg Haefligerab413192006-09-24 20:54:04 +02001242 .name = DRVNAME,
1243 },
1244 .probe = vt1211_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001245 .remove = vt1211_remove,
Juerg Haefligerab413192006-09-24 20:54:04 +02001246};
1247
1248static int __init vt1211_device_add(unsigned short address)
1249{
1250 struct resource res = {
1251 .start = address,
1252 .end = address + 0x7f,
1253 .flags = IORESOURCE_IO,
1254 };
1255 int err;
1256
1257 pdev = platform_device_alloc(DRVNAME, address);
1258 if (!pdev) {
1259 err = -ENOMEM;
Joe Perches5ed9ba62010-10-20 06:51:52 +00001260 pr_err("Device allocation failed (%d)\n", err);
Juerg Haefligerab413192006-09-24 20:54:04 +02001261 goto EXIT;
1262 }
1263
1264 res.name = pdev->name;
Jean Delvareb9acb642009-01-07 16:37:35 +01001265 err = acpi_check_resource_conflict(&res);
1266 if (err)
Hans de Goede18632f82009-02-17 19:59:54 +01001267 goto EXIT_DEV_PUT;
Jean Delvareb9acb642009-01-07 16:37:35 +01001268
Juerg Haefligerab413192006-09-24 20:54:04 +02001269 err = platform_device_add_resources(pdev, &res, 1);
1270 if (err) {
Joe Perches5ed9ba62010-10-20 06:51:52 +00001271 pr_err("Device resource addition failed (%d)\n", err);
Juerg Haefligerab413192006-09-24 20:54:04 +02001272 goto EXIT_DEV_PUT;
1273 }
1274
1275 err = platform_device_add(pdev);
1276 if (err) {
Joe Perches5ed9ba62010-10-20 06:51:52 +00001277 pr_err("Device addition failed (%d)\n", err);
Juerg Haefligerab413192006-09-24 20:54:04 +02001278 goto EXIT_DEV_PUT;
1279 }
1280
1281 return 0;
1282
1283EXIT_DEV_PUT:
1284 platform_device_put(pdev);
1285EXIT:
1286 return err;
1287}
1288
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001289static int __init vt1211_find(int sio_cip, unsigned short *address)
Juerg Haefligerab413192006-09-24 20:54:04 +02001290{
Guenter Roeckf418d002019-04-05 08:53:08 -07001291 int err;
Jean Delvare67b671b2007-12-06 23:13:42 +01001292 int devid;
Juerg Haefligerab413192006-09-24 20:54:04 +02001293
Guenter Roeckf418d002019-04-05 08:53:08 -07001294 err = superio_enter(sio_cip);
1295 if (err)
1296 return err;
Juerg Haefligerab413192006-09-24 20:54:04 +02001297
Guenter Roeckf418d002019-04-05 08:53:08 -07001298 err = -ENODEV;
Jean Delvare67b671b2007-12-06 23:13:42 +01001299 devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID);
Guenter Roeckb162c032012-01-15 06:52:33 -08001300 if (devid != SIO_VT1211_ID)
Juerg Haefligerab413192006-09-24 20:54:04 +02001301 goto EXIT;
Juerg Haefligerab413192006-09-24 20:54:04 +02001302
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001303 superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
Juerg Haefligerab413192006-09-24 20:54:04 +02001304
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001305 if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
Joe Perches5ed9ba62010-10-20 06:51:52 +00001306 pr_warn("HW monitor is disabled, skipping\n");
Juerg Haefligerab413192006-09-24 20:54:04 +02001307 goto EXIT;
1308 }
1309
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001310 *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1311 (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
Juerg Haefligerab413192006-09-24 20:54:04 +02001312 if (*address == 0) {
Joe Perches5ed9ba62010-10-20 06:51:52 +00001313 pr_warn("Base address is not set, skipping\n");
Juerg Haefligerab413192006-09-24 20:54:04 +02001314 goto EXIT;
1315 }
1316
1317 err = 0;
Joe Perches5ed9ba62010-10-20 06:51:52 +00001318 pr_info("Found VT1211 chip at 0x%04x, revision %u\n",
1319 *address, superio_inb(sio_cip, SIO_VT1211_DEVREV));
Juerg Haefligerab413192006-09-24 20:54:04 +02001320
1321EXIT:
Juerg Haefliger2219cd82007-02-14 21:15:05 +01001322 superio_exit(sio_cip);
Juerg Haefligerab413192006-09-24 20:54:04 +02001323 return err;
1324}
1325
1326static int __init vt1211_init(void)
1327{
1328 int err;
1329 unsigned short address = 0;
1330
Guenter Roeckb162c032012-01-15 06:52:33 -08001331 err = vt1211_find(SIO_REG_CIP1, &address);
1332 if (err) {
1333 err = vt1211_find(SIO_REG_CIP2, &address);
1334 if (err)
1335 goto EXIT;
Juerg Haefligerab413192006-09-24 20:54:04 +02001336 }
1337
1338 if ((uch_config < -1) || (uch_config > 31)) {
1339 err = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001340 pr_warn("Invalid UCH configuration %d. Choose a value between 0 and 31.\n",
1341 uch_config);
Guenter Roeckb162c032012-01-15 06:52:33 -08001342 goto EXIT;
Juerg Haefligerab413192006-09-24 20:54:04 +02001343 }
1344
1345 if ((int_mode < -1) || (int_mode > 0)) {
1346 err = -EINVAL;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001347 pr_warn("Invalid interrupt mode %d. Only mode 0 is supported.\n",
1348 int_mode);
Juerg Haefligerab413192006-09-24 20:54:04 +02001349 goto EXIT;
1350 }
1351
Guenter Roeckb162c032012-01-15 06:52:33 -08001352 err = platform_driver_register(&vt1211_driver);
1353 if (err)
1354 goto EXIT;
1355
Juerg Haefligerab413192006-09-24 20:54:04 +02001356 /* Sets global pdev as a side effect */
1357 err = vt1211_device_add(address);
Guenter Roeckb162c032012-01-15 06:52:33 -08001358 if (err)
Juerg Haefligerab413192006-09-24 20:54:04 +02001359 goto EXIT_DRV_UNREGISTER;
Juerg Haefligerab413192006-09-24 20:54:04 +02001360
1361 return 0;
1362
1363EXIT_DRV_UNREGISTER:
1364 platform_driver_unregister(&vt1211_driver);
1365EXIT:
1366 return err;
1367}
1368
1369static void __exit vt1211_exit(void)
1370{
1371 platform_device_unregister(pdev);
1372 platform_driver_unregister(&vt1211_driver);
1373}
1374
1375MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1376MODULE_DESCRIPTION("VT1211 sensors");
1377MODULE_LICENSE("GPL");
1378
1379module_init(vt1211_init);
1380module_exit(vt1211_exit);