Linux Audio

Check our new training course

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