Linux Audio

Check our new training course

Loading...
v3.1
 
   1/*
   2 *  pc87360.c - Part of lm_sensors, Linux kernel modules
   3 *              for hardware monitoring
   4 *  Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
   5 *
   6 *  Copied from smsc47m1.c:
   7 *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
   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 *  Supports the following chips:
  24 *
  25 *  Chip        #vin    #fan    #pwm    #temp   devid
  26 *  PC87360     -       2       2       -       0xE1
  27 *  PC87363     -       2       2       -       0xE8
  28 *  PC87364     -       3       3       -       0xE4
  29 *  PC87365     11      3       3       2       0xE5
  30 *  PC87366     11      3       3       3-4     0xE9
  31 *
  32 *  This driver assumes that no more than one chip is present, and one of
  33 *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
  34 */
  35
  36#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  37
  38#include <linux/module.h>
  39#include <linux/init.h>
  40#include <linux/slab.h>
  41#include <linux/jiffies.h>
  42#include <linux/platform_device.h>
  43#include <linux/hwmon.h>
  44#include <linux/hwmon-sysfs.h>
  45#include <linux/hwmon-vid.h>
  46#include <linux/err.h>
  47#include <linux/mutex.h>
  48#include <linux/acpi.h>
  49#include <linux/io.h>
  50
 
 
 
 
 
 
 
 
 
 
 
 
  51static u8 devid;
  52static struct platform_device *pdev;
  53static unsigned short extra_isa[3];
  54static u8 confreg[4];
  55
  56static int init = 1;
  57module_param(init, int, 0);
  58MODULE_PARM_DESC(init,
  59 "Chip initialization level:\n"
  60 " 0: None\n"
  61 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
  62 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
  63 " 3: Forcibly enable all voltage and temperature channels, including in9");
  64
  65static unsigned short force_id;
  66module_param(force_id, ushort, 0);
  67MODULE_PARM_DESC(force_id, "Override the detected device ID");
  68
  69/*
  70 * Super-I/O registers and operations
  71 */
  72
  73#define DEV	0x07	/* Register: Logical device select */
  74#define DEVID	0x20	/* Register: Device ID */
  75#define ACT	0x30	/* Register: Device activation */
  76#define BASE	0x60	/* Register: Base address */
  77
  78#define FSCM	0x09	/* Logical device: fans */
  79#define VLM	0x0d	/* Logical device: voltages */
  80#define TMS	0x0e	/* Logical device: temperatures */
  81#define LDNI_MAX 3
  82static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
  83
  84#define LD_FAN		0
  85#define LD_IN		1
  86#define LD_TEMP		2
  87
  88static inline void superio_outb(int sioaddr, int reg, int val)
  89{
  90	outb(reg, sioaddr);
  91	outb(val, sioaddr+1);
  92}
  93
  94static inline int superio_inb(int sioaddr, int reg)
  95{
  96	outb(reg, sioaddr);
  97	return inb(sioaddr+1);
  98}
  99
 100static inline void superio_exit(int sioaddr)
 101{
 102	outb(0x02, sioaddr);
 103	outb(0x02, sioaddr+1);
 104}
 105
 106/*
 107 * Logical devices
 108 */
 109
 110#define PC87360_EXTENT		0x10
 111#define PC87365_REG_BANK	0x09
 112#define NO_BANK			0xff
 113
 114/*
 115 * Fan registers and conversions
 116 */
 117
 118/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
 119#define PC87360_REG_PRESCALE(nr)	(0x00 + 2 * (nr))
 120#define PC87360_REG_PWM(nr)		(0x01 + 2 * (nr))
 121#define PC87360_REG_FAN_MIN(nr)		(0x06 + 3 * (nr))
 122#define PC87360_REG_FAN(nr)		(0x07 + 3 * (nr))
 123#define PC87360_REG_FAN_STATUS(nr)	(0x08 + 3 * (nr))
 124
 125#define FAN_FROM_REG(val,div)		((val) == 0 ? 0: \
 126					 480000 / ((val)*(div)))
 127#define FAN_TO_REG(val,div)		((val) <= 100 ? 0 : \
 128					 480000 / ((val)*(div)))
 129#define FAN_DIV_FROM_REG(val)		(1 << ((val >> 5) & 0x03))
 130#define FAN_STATUS_FROM_REG(val)	((val) & 0x07)
 131
 132#define FAN_CONFIG_MONITOR(val,nr)	(((val) >> (2 + nr * 3)) & 1)
 133#define FAN_CONFIG_CONTROL(val,nr)	(((val) >> (3 + nr * 3)) & 1)
 134#define FAN_CONFIG_INVERT(val,nr)	(((val) >> (4 + nr * 3)) & 1)
 135
 136#define PWM_FROM_REG(val,inv)		((inv) ? 255 - (val) : (val))
 137static inline u8 PWM_TO_REG(int val, int inv)
 138{
 139	if (inv)
 140		val = 255 - val;
 141	if (val < 0)
 142		return 0;
 143	if (val > 255)
 144		return 255;
 145	return val;
 146}
 147
 148/*
 149 * Voltage registers and conversions
 150 */
 151
 152#define PC87365_REG_IN_CONVRATE		0x07
 153#define PC87365_REG_IN_CONFIG		0x08
 154#define PC87365_REG_IN			0x0B
 155#define PC87365_REG_IN_MIN		0x0D
 156#define PC87365_REG_IN_MAX		0x0C
 157#define PC87365_REG_IN_STATUS		0x0A
 158#define PC87365_REG_IN_ALARMS1		0x00
 159#define PC87365_REG_IN_ALARMS2		0x01
 160#define PC87365_REG_VID			0x06
 161
 162#define IN_FROM_REG(val,ref)		(((val) * (ref) + 128) / 256)
 163#define IN_TO_REG(val,ref)		((val) < 0 ? 0 : \
 164					 (val)*256 >= (ref)*255 ? 255: \
 165					 ((val) * 256 + (ref)/2) / (ref))
 166
 167/*
 168 * Temperature registers and conversions
 169 */
 170
 171#define PC87365_REG_TEMP_CONFIG		0x08
 172#define PC87365_REG_TEMP		0x0B
 173#define PC87365_REG_TEMP_MIN		0x0D
 174#define PC87365_REG_TEMP_MAX		0x0C
 175#define PC87365_REG_TEMP_CRIT		0x0E
 176#define PC87365_REG_TEMP_STATUS		0x0A
 177#define PC87365_REG_TEMP_ALARMS		0x00
 178
 179#define TEMP_FROM_REG(val)		((val) * 1000)
 180#define TEMP_TO_REG(val)		((val) < -55000 ? -55 : \
 181					 (val) > 127000 ? 127 : \
 182					 (val) < 0 ? ((val) - 500) / 1000 : \
 183					 ((val) + 500) / 1000)
 184
 185/*
 186 * Device data
 187 */
 188
 189struct pc87360_data {
 190	const char *name;
 191	struct device *hwmon_dev;
 192	struct mutex lock;
 193	struct mutex update_lock;
 194	char valid;		/* !=0 if following fields are valid */
 195	unsigned long last_updated;	/* In jiffies */
 196
 197	int address[3];
 198
 199	u8 fannr, innr, tempnr;
 200
 201	u8 fan[3];		/* Register value */
 202	u8 fan_min[3];		/* Register value */
 203	u8 fan_status[3];	/* Register value */
 204	u8 pwm[3];		/* Register value */
 205	u16 fan_conf;		/* Configuration register values, combined */
 206
 207	u16 in_vref;		/* 1 mV/bit */
 208	u8 in[14];		/* Register value */
 209	u8 in_min[14];		/* Register value */
 210	u8 in_max[14];		/* Register value */
 211	u8 in_crit[3];		/* Register value */
 212	u8 in_status[14];	/* Register value */
 213	u16 in_alarms;		/* Register values, combined, masked */
 214	u8 vid_conf;		/* Configuration register value */
 215	u8 vrm;
 216	u8 vid;			/* Register value */
 217
 218	s8 temp[3];		/* Register value */
 219	s8 temp_min[3];		/* Register value */
 220	s8 temp_max[3];		/* Register value */
 221	s8 temp_crit[3];	/* Register value */
 222	u8 temp_status[3];	/* Register value */
 223	u8 temp_alarms;		/* Register value, masked */
 224};
 225
 226/*
 227 * Functions declaration
 
 228 */
 229
 230static int pc87360_probe(struct platform_device *pdev);
 231static int __devexit pc87360_remove(struct platform_device *pdev);
 232
 233static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
 234			      u8 reg);
 235static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
 236				u8 reg, u8 value);
 237static void pc87360_init_device(struct platform_device *pdev,
 238				int use_thermistors);
 239static struct pc87360_data *pc87360_update_device(struct device *dev);
 240
 241/*
 242 * Driver data
 243 */
 244
 245static struct platform_driver pc87360_driver = {
 246	.driver = {
 247		.owner	= THIS_MODULE,
 248		.name	= "pc87360",
 249	},
 250	.probe		= pc87360_probe,
 251	.remove		= __devexit_p(pc87360_remove),
 252};
 253
 254/*
 255 * Sysfs stuff
 256 */
 
 
 257
 258static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
 259{
 260	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 261	struct pc87360_data *data = pc87360_update_device(dev);
 262	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
 263		       FAN_DIV_FROM_REG(data->fan_status[attr->index])));
 264}
 265static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
 266{
 267	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 268	struct pc87360_data *data = pc87360_update_device(dev);
 269	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
 270		       FAN_DIV_FROM_REG(data->fan_status[attr->index])));
 271}
 272static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
 
 
 273{
 274	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 275	struct pc87360_data *data = pc87360_update_device(dev);
 276	return sprintf(buf, "%u\n",
 277		       FAN_DIV_FROM_REG(data->fan_status[attr->index]));
 
 278}
 279static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
 
 280{
 281	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 282	struct pc87360_data *data = pc87360_update_device(dev);
 283	return sprintf(buf, "%u\n",
 284		       FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 285}
 286static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
 287	size_t count)
 288{
 289	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 290	struct pc87360_data *data = dev_get_drvdata(dev);
 291	long fan_min = simple_strtol(buf, NULL, 10);
 292
 293	mutex_lock(&data->update_lock);
 294	fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
 295
 296	/* If it wouldn't fit, change clock divisor */
 297	while (fan_min > 255
 298	    && (data->fan_status[attr->index] & 0x60) != 0x60) {
 299		fan_min >>= 1;
 300		data->fan[attr->index] >>= 1;
 301		data->fan_status[attr->index] += 0x20;
 302	}
 303	data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
 304	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
 305			    data->fan_min[attr->index]);
 306
 307	/* Write new divider, preserve alarm bits */
 308	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
 309			    data->fan_status[attr->index] & 0xF9);
 310	mutex_unlock(&data->update_lock);
 311
 312	return count;
 313}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 314
 315static struct sensor_device_attribute fan_input[] = {
 316	SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
 317	SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
 318	SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
 319};
 320static struct sensor_device_attribute fan_status[] = {
 321	SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
 322	SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
 323	SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
 324};
 325static struct sensor_device_attribute fan_div[] = {
 326	SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
 327	SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
 328	SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
 329};
 330static struct sensor_device_attribute fan_min[] = {
 331	SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
 332	SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
 333	SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
 334};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 335
 336#define FAN_UNIT_ATTRS(X)	\
 337	&fan_input[X].dev_attr.attr,	\
 338	&fan_status[X].dev_attr.attr,	\
 339	&fan_div[X].dev_attr.attr,	\
 340	&fan_min[X].dev_attr.attr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 341
 342static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
 343{
 344	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 345	struct pc87360_data *data = pc87360_update_device(dev);
 346	return sprintf(buf, "%u\n",
 347		       PWM_FROM_REG(data->pwm[attr->index],
 348				    FAN_CONFIG_INVERT(data->fan_conf,
 349						      attr->index)));
 350}
 351static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
 352	size_t count)
 353{
 354	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 355	struct pc87360_data *data = dev_get_drvdata(dev);
 356	long val = simple_strtol(buf, NULL, 10);
 357
 358	mutex_lock(&data->update_lock);
 359	data->pwm[attr->index] = PWM_TO_REG(val,
 360			      FAN_CONFIG_INVERT(data->fan_conf, attr->index));
 361	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
 362			    data->pwm[attr->index]);
 363	mutex_unlock(&data->update_lock);
 364	return count;
 365}
 366
 367static struct sensor_device_attribute pwm[] = {
 368	SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
 369	SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
 370	SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
 371};
 372
 373static struct attribute * pc8736x_fan_attr_array[] = {
 374	FAN_UNIT_ATTRS(0),
 375	FAN_UNIT_ATTRS(1),
 376	FAN_UNIT_ATTRS(2),
 377	&pwm[0].dev_attr.attr,
 378	&pwm[1].dev_attr.attr,
 379	&pwm[2].dev_attr.attr,
 380	NULL
 381};
 382static const struct attribute_group pc8736x_fan_group = {
 383	.attrs = pc8736x_fan_attr_array,
 384};
 385
 386static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
 
 387{
 388	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 389	struct pc87360_data *data = pc87360_update_device(dev);
 390	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
 391		       data->in_vref));
 392}
 393static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 394{
 395	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 396	struct pc87360_data *data = pc87360_update_device(dev);
 397	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
 398		       data->in_vref));
 399}
 400static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 401{
 402	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 403	struct pc87360_data *data = pc87360_update_device(dev);
 404	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
 405		       data->in_vref));
 406}
 407static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
 408{
 409	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 410	struct pc87360_data *data = pc87360_update_device(dev);
 411	return sprintf(buf, "%u\n", data->in_status[attr->index]);
 412}
 413static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
 414	size_t count)
 415{
 416	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 417	struct pc87360_data *data = dev_get_drvdata(dev);
 418	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 419
 420	mutex_lock(&data->update_lock);
 421	data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
 422	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
 423			    data->in_min[attr->index]);
 424	mutex_unlock(&data->update_lock);
 425	return count;
 426}
 427static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
 428	size_t count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 429{
 430	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 431	struct pc87360_data *data = dev_get_drvdata(dev);
 432	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 433
 434	mutex_lock(&data->update_lock);
 435	data->in_max[attr->index] = IN_TO_REG(val,
 436			       data->in_vref);
 437	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
 438			    data->in_max[attr->index]);
 439	mutex_unlock(&data->update_lock);
 440	return count;
 441}
 442
 443static struct sensor_device_attribute in_input[] = {
 444	SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
 445	SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
 446	SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
 447	SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
 448	SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
 449	SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
 450	SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
 451	SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
 452	SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
 453	SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
 454	SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
 455};
 456static struct sensor_device_attribute in_status[] = {
 457	SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
 458	SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
 459	SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
 460	SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
 461	SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
 462	SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
 463	SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
 464	SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
 465	SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
 466	SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
 467	SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
 468};
 469static struct sensor_device_attribute in_min[] = {
 470	SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
 471	SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
 472	SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
 473	SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
 474	SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
 475	SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
 476	SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
 477	SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
 478	SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
 479	SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
 480	SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
 481};
 482static struct sensor_device_attribute in_max[] = {
 483	SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
 484	SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
 485	SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
 486	SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
 487	SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
 488	SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
 489	SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
 490	SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
 491	SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
 492	SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
 493	SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
 494};
 495
 496/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
 497#define CHAN_ALM_MIN	0x02	/* min limit crossed */
 498#define CHAN_ALM_MAX	0x04	/* max limit exceeded */
 499#define TEMP_ALM_CRIT	0x08	/* temp crit exceeded (temp only) */
 500
 501/* show_in_min/max_alarm() reads data from the per-channel status
 502   register (sec 11.5.12), not the vin event status registers (sec
 503   11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms) */
 
 
 504
 505static ssize_t show_in_min_alarm(struct device *dev,
 506			struct device_attribute *devattr, char *buf)
 507{
 508	struct pc87360_data *data = pc87360_update_device(dev);
 509	unsigned nr = to_sensor_dev_attr(devattr)->index;
 510
 511	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
 512}
 513static ssize_t show_in_max_alarm(struct device *dev,
 514			struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 515{
 516	struct pc87360_data *data = pc87360_update_device(dev);
 517	unsigned nr = to_sensor_dev_attr(devattr)->index;
 518
 519	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
 520}
 521
 522static struct sensor_device_attribute in_min_alarm[] = {
 523	SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
 524	SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
 525	SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
 526	SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
 527	SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
 528	SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
 529	SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
 530	SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
 531	SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
 532	SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
 533	SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
 534};
 535static struct sensor_device_attribute in_max_alarm[] = {
 536	SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
 537	SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
 538	SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
 539	SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
 540	SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
 541	SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
 542	SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
 543	SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
 544	SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
 545	SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
 546	SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
 547};
 548
 549#define VIN_UNIT_ATTRS(X) \
 550	&in_input[X].dev_attr.attr,	\
 551	&in_status[X].dev_attr.attr,	\
 552	&in_min[X].dev_attr.attr,	\
 553	&in_max[X].dev_attr.attr,	\
 554	&in_min_alarm[X].dev_attr.attr,	\
 555	&in_max_alarm[X].dev_attr.attr
 556
 557static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
 
 558{
 559	struct pc87360_data *data = pc87360_update_device(dev);
 560	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
 561}
 562static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
 563
 564static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
 
 565{
 566	struct pc87360_data *data = dev_get_drvdata(dev);
 567	return sprintf(buf, "%u\n", data->vrm);
 568}
 569static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
 
 
 570{
 571	struct pc87360_data *data = dev_get_drvdata(dev);
 572	data->vrm = simple_strtoul(buf, NULL, 10);
 
 
 
 
 
 
 
 
 
 
 573	return count;
 574}
 575static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
 576
 577static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
 
 578{
 579	struct pc87360_data *data = pc87360_update_device(dev);
 580	return sprintf(buf, "%u\n", data->in_alarms);
 581}
 582static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
 583
 584static struct attribute *pc8736x_vin_attr_array[] = {
 585	VIN_UNIT_ATTRS(0),
 586	VIN_UNIT_ATTRS(1),
 587	VIN_UNIT_ATTRS(2),
 588	VIN_UNIT_ATTRS(3),
 589	VIN_UNIT_ATTRS(4),
 590	VIN_UNIT_ATTRS(5),
 591	VIN_UNIT_ATTRS(6),
 592	VIN_UNIT_ATTRS(7),
 593	VIN_UNIT_ATTRS(8),
 594	VIN_UNIT_ATTRS(9),
 595	VIN_UNIT_ATTRS(10),
 596	&dev_attr_cpu0_vid.attr,
 597	&dev_attr_vrm.attr,
 598	&dev_attr_alarms_in.attr,
 599	NULL
 600};
 601static const struct attribute_group pc8736x_vin_group = {
 602	.attrs = pc8736x_vin_attr_array,
 603};
 604
 605static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
 
 606{
 607	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 608	struct pc87360_data *data = pc87360_update_device(dev);
 609	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
 610		       data->in_vref));
 611}
 612static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
 613{
 614	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 615	struct pc87360_data *data = pc87360_update_device(dev);
 616	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
 617		       data->in_vref));
 618}
 619static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
 
 
 
 
 
 620{
 621	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 622	struct pc87360_data *data = pc87360_update_device(dev);
 623	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
 624		       data->in_vref));
 625}
 626static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 627{
 628	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 629	struct pc87360_data *data = pc87360_update_device(dev);
 630	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
 631		       data->in_vref));
 632}
 633static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
 634{
 635	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 636	struct pc87360_data *data = pc87360_update_device(dev);
 637	return sprintf(buf, "%u\n", data->in_status[attr->index]);
 638}
 639static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
 640	size_t count)
 641{
 642	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 643	struct pc87360_data *data = dev_get_drvdata(dev);
 644	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 645
 646	mutex_lock(&data->update_lock);
 647	data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
 648	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
 649			    data->in_min[attr->index]);
 650	mutex_unlock(&data->update_lock);
 651	return count;
 652}
 653static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
 654	size_t count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 655{
 656	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 657	struct pc87360_data *data = dev_get_drvdata(dev);
 658	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 659
 660	mutex_lock(&data->update_lock);
 661	data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
 662	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
 663			    data->in_max[attr->index]);
 664	mutex_unlock(&data->update_lock);
 665	return count;
 666}
 667static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
 668	size_t count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 669{
 670	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 671	struct pc87360_data *data = dev_get_drvdata(dev);
 672	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 673
 674	mutex_lock(&data->update_lock);
 675	data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
 676	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
 677			    data->in_crit[attr->index-11]);
 678	mutex_unlock(&data->update_lock);
 679	return count;
 680}
 681
 682/* the +11 term below reflects the fact that VLM units 11,12,13 are
 683   used in the chip to measure voltage across the thermistors
 684*/
 685static struct sensor_device_attribute therm_input[] = {
 686	SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
 687	SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
 688	SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
 689};
 690static struct sensor_device_attribute therm_status[] = {
 691	SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
 692	SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
 693	SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
 694};
 695static struct sensor_device_attribute therm_min[] = {
 696	SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
 697		    show_therm_min, set_therm_min, 0+11),
 698	SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
 699		    show_therm_min, set_therm_min, 1+11),
 700	SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
 701		    show_therm_min, set_therm_min, 2+11),
 702};
 703static struct sensor_device_attribute therm_max[] = {
 704	SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
 705		    show_therm_max, set_therm_max, 0+11),
 706	SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
 707		    show_therm_max, set_therm_max, 1+11),
 708	SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
 709		    show_therm_max, set_therm_max, 2+11),
 710};
 711static struct sensor_device_attribute therm_crit[] = {
 712	SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
 713		    show_therm_crit, set_therm_crit, 0+11),
 714	SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
 715		    show_therm_crit, set_therm_crit, 1+11),
 716	SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
 717		    show_therm_crit, set_therm_crit, 2+11),
 718};
 719
 720/* show_therm_min/max_alarm() reads data from the per-channel voltage
 721   status register (sec 11.5.12) */
 722
 723static ssize_t show_therm_min_alarm(struct device *dev,
 724				struct device_attribute *devattr, char *buf)
 
 
 725{
 726	struct pc87360_data *data = pc87360_update_device(dev);
 727	unsigned nr = to_sensor_dev_attr(devattr)->index;
 728
 729	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
 730}
 731static ssize_t show_therm_max_alarm(struct device *dev,
 732				struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 733{
 734	struct pc87360_data *data = pc87360_update_device(dev);
 735	unsigned nr = to_sensor_dev_attr(devattr)->index;
 736
 737	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
 738}
 739static ssize_t show_therm_crit_alarm(struct device *dev,
 740				struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 741{
 742	struct pc87360_data *data = pc87360_update_device(dev);
 743	unsigned nr = to_sensor_dev_attr(devattr)->index;
 744
 745	return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
 746}
 747
 748static struct sensor_device_attribute therm_min_alarm[] = {
 749	SENSOR_ATTR(temp4_min_alarm, S_IRUGO,
 750		    show_therm_min_alarm, NULL, 0+11),
 751	SENSOR_ATTR(temp5_min_alarm, S_IRUGO,
 752		    show_therm_min_alarm, NULL, 1+11),
 753	SENSOR_ATTR(temp6_min_alarm, S_IRUGO,
 754		    show_therm_min_alarm, NULL, 2+11),
 755};
 756static struct sensor_device_attribute therm_max_alarm[] = {
 757	SENSOR_ATTR(temp4_max_alarm, S_IRUGO,
 758		    show_therm_max_alarm, NULL, 0+11),
 759	SENSOR_ATTR(temp5_max_alarm, S_IRUGO,
 760		    show_therm_max_alarm, NULL, 1+11),
 761	SENSOR_ATTR(temp6_max_alarm, S_IRUGO,
 762		    show_therm_max_alarm, NULL, 2+11),
 763};
 764static struct sensor_device_attribute therm_crit_alarm[] = {
 765	SENSOR_ATTR(temp4_crit_alarm, S_IRUGO,
 766		    show_therm_crit_alarm, NULL, 0+11),
 767	SENSOR_ATTR(temp5_crit_alarm, S_IRUGO,
 768		    show_therm_crit_alarm, NULL, 1+11),
 769	SENSOR_ATTR(temp6_crit_alarm, S_IRUGO,
 770		    show_therm_crit_alarm, NULL, 2+11),
 771};
 772
 773#define THERM_UNIT_ATTRS(X) \
 774	&therm_input[X].dev_attr.attr,	\
 775	&therm_status[X].dev_attr.attr,	\
 776	&therm_min[X].dev_attr.attr,	\
 777	&therm_max[X].dev_attr.attr,	\
 778	&therm_crit[X].dev_attr.attr,	\
 779	&therm_min_alarm[X].dev_attr.attr, \
 780	&therm_max_alarm[X].dev_attr.attr, \
 781	&therm_crit_alarm[X].dev_attr.attr
 782
 783static struct attribute * pc8736x_therm_attr_array[] = {
 784	THERM_UNIT_ATTRS(0),
 785	THERM_UNIT_ATTRS(1),
 786	THERM_UNIT_ATTRS(2),
 787	NULL
 788};
 789static const struct attribute_group pc8736x_therm_group = {
 790	.attrs = pc8736x_therm_attr_array,
 791};
 792
 793static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
 
 794{
 795	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 796	struct pc87360_data *data = pc87360_update_device(dev);
 797	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
 798}
 799static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
 800{
 801	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 802	struct pc87360_data *data = pc87360_update_device(dev);
 803	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
 804}
 805static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
 806{
 807	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 808	struct pc87360_data *data = pc87360_update_device(dev);
 809	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
 810}
 811static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
 812{
 813	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 814	struct pc87360_data *data = pc87360_update_device(dev);
 815	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
 816}
 817static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 818{
 819	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 820	struct pc87360_data *data = pc87360_update_device(dev);
 821	return sprintf(buf, "%d\n", data->temp_status[attr->index]);
 822}
 823static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
 824	size_t count)
 
 
 825{
 826	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 827	struct pc87360_data *data = dev_get_drvdata(dev);
 828	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 829
 830	mutex_lock(&data->update_lock);
 831	data->temp_min[attr->index] = TEMP_TO_REG(val);
 832	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
 833			    data->temp_min[attr->index]);
 834	mutex_unlock(&data->update_lock);
 835	return count;
 836}
 837static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
 838	size_t count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 839{
 840	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 841	struct pc87360_data *data = dev_get_drvdata(dev);
 842	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 843
 844	mutex_lock(&data->update_lock);
 845	data->temp_max[attr->index] = TEMP_TO_REG(val);
 846	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
 847			    data->temp_max[attr->index]);
 848	mutex_unlock(&data->update_lock);
 849	return count;
 850}
 851static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
 852	size_t count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 853{
 854	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 855	struct pc87360_data *data = dev_get_drvdata(dev);
 856	long val = simple_strtol(buf, NULL, 10);
 
 
 
 
 
 857
 858	mutex_lock(&data->update_lock);
 859	data->temp_crit[attr->index] = TEMP_TO_REG(val);
 860	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
 861			    data->temp_crit[attr->index]);
 862	mutex_unlock(&data->update_lock);
 863	return count;
 864}
 865
 866static struct sensor_device_attribute temp_input[] = {
 867	SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
 868	SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
 869	SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
 870};
 871static struct sensor_device_attribute temp_status[] = {
 872	SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
 873	SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
 874	SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
 875};
 876static struct sensor_device_attribute temp_min[] = {
 877	SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
 878		    show_temp_min, set_temp_min, 0),
 879	SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
 880		    show_temp_min, set_temp_min, 1),
 881	SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
 882		    show_temp_min, set_temp_min, 2),
 883};
 884static struct sensor_device_attribute temp_max[] = {
 885	SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
 886		    show_temp_max, set_temp_max, 0),
 887	SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
 888		    show_temp_max, set_temp_max, 1),
 889	SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
 890		    show_temp_max, set_temp_max, 2),
 891};
 892static struct sensor_device_attribute temp_crit[] = {
 893	SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
 894		    show_temp_crit, set_temp_crit, 0),
 895	SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
 896		    show_temp_crit, set_temp_crit, 1),
 897	SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
 898		    show_temp_crit, set_temp_crit, 2),
 899};
 900
 901static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
 902{
 903	struct pc87360_data *data = pc87360_update_device(dev);
 904	return sprintf(buf, "%u\n", data->temp_alarms);
 905}
 906static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
 907
 908/* show_temp_min/max_alarm() reads data from the per-channel status
 909   register (sec 12.3.7), not the temp event status registers (sec
 910   12.3.2) that show_temp_alarm() reads (via data->temp_alarms) */
 911
 912static ssize_t show_temp_min_alarm(struct device *dev,
 913			struct device_attribute *devattr, char *buf)
 914{
 915	struct pc87360_data *data = pc87360_update_device(dev);
 916	unsigned nr = to_sensor_dev_attr(devattr)->index;
 917
 918	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
 919}
 920static ssize_t show_temp_max_alarm(struct device *dev,
 921			struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 922{
 923	struct pc87360_data *data = pc87360_update_device(dev);
 924	unsigned nr = to_sensor_dev_attr(devattr)->index;
 925
 926	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
 927}
 928static ssize_t show_temp_crit_alarm(struct device *dev,
 929			struct device_attribute *devattr, char *buf)
 
 
 
 
 
 
 
 
 930{
 931	struct pc87360_data *data = pc87360_update_device(dev);
 932	unsigned nr = to_sensor_dev_attr(devattr)->index;
 933
 934	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
 935}
 936
 937static struct sensor_device_attribute temp_min_alarm[] = {
 938	SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
 939	SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
 940	SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
 941};
 942static struct sensor_device_attribute temp_max_alarm[] = {
 943	SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
 944	SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
 945	SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
 946};
 947static struct sensor_device_attribute temp_crit_alarm[] = {
 948	SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
 949	SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
 950	SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
 951};
 952
 953#define TEMP_FAULT	0x40	/* open diode */
 954static ssize_t show_temp_fault(struct device *dev,
 955			struct device_attribute *devattr, char *buf)
 956{
 957	struct pc87360_data *data = pc87360_update_device(dev);
 958	unsigned nr = to_sensor_dev_attr(devattr)->index;
 959
 960	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
 961}
 
 962static struct sensor_device_attribute temp_fault[] = {
 963	SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
 964	SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
 965	SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
 966};
 967
 968#define TEMP_UNIT_ATTRS(X) \
 969	&temp_input[X].dev_attr.attr,	\
 970	&temp_status[X].dev_attr.attr,	\
 971	&temp_min[X].dev_attr.attr,	\
 972	&temp_max[X].dev_attr.attr,	\
 973	&temp_crit[X].dev_attr.attr,	\
 974	&temp_min_alarm[X].dev_attr.attr, \
 975	&temp_max_alarm[X].dev_attr.attr, \
 976	&temp_crit_alarm[X].dev_attr.attr, \
 977	&temp_fault[X].dev_attr.attr
 
 
 978
 979static struct attribute * pc8736x_temp_attr_array[] = {
 980	TEMP_UNIT_ATTRS(0),
 981	TEMP_UNIT_ATTRS(1),
 982	TEMP_UNIT_ATTRS(2),
 983	/* include the few miscellaneous atts here */
 984	&dev_attr_alarms_temp.attr,
 985	NULL
 986};
 987static const struct attribute_group pc8736x_temp_group = {
 988	.attrs = pc8736x_temp_attr_array,
 
 
 
 989};
 990
 991static ssize_t show_name(struct device *dev,
 992			struct device_attribute *devattr, char *buf)
 993{
 994	struct pc87360_data *data = dev_get_drvdata(dev);
 995	return sprintf(buf, "%s\n", data->name);
 996}
 997static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
 998
 999/*
1000 * Device detection, registration and update
1001 */
1002
1003static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
 
1004{
1005	u16 val;
1006	int i;
1007	int nrdev; /* logical device count */
1008
1009	/* No superio_enter */
1010
1011	/* Identify device */
1012	val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1013	switch (val) {
1014	case 0xE1: /* PC87360 */
1015	case 0xE8: /* PC87363 */
1016	case 0xE4: /* PC87364 */
1017		nrdev = 1;
1018		break;
1019	case 0xE5: /* PC87365 */
1020	case 0xE9: /* PC87366 */
1021		nrdev = 3;
1022		break;
1023	default:
1024		superio_exit(sioaddr);
1025		return -ENODEV;
1026	}
1027	/* Remember the device id */
1028	*devid = val;
1029
1030	for (i = 0; i < nrdev; i++) {
1031		/* select logical device */
1032		superio_outb(sioaddr, DEV, logdev[i]);
1033
1034		val = superio_inb(sioaddr, ACT);
1035		if (!(val & 0x01)) {
1036			pr_info("Device 0x%02x not activated\n", logdev[i]);
1037			continue;
1038		}
1039
1040		val = (superio_inb(sioaddr, BASE) << 8)
1041		    | superio_inb(sioaddr, BASE + 1);
1042		if (!val) {
1043			pr_info("Base address not set for device 0x%02x\n",
1044				logdev[i]);
1045			continue;
1046		}
1047
1048		addresses[i] = val;
 
 
 
 
 
 
 
1049
1050		if (i==0) { /* Fans */
1051			confreg[0] = superio_inb(sioaddr, 0xF0);
1052			confreg[1] = superio_inb(sioaddr, 0xF1);
 
 
1053
1054			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1055				 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1056				 (confreg[0] >> 4) & 1);
1057			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1058				 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1059				 (confreg[0] >> 7) & 1);
1060			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1061				 confreg[1] & 1, (confreg[1] >> 1) & 1,
1062				 (confreg[1] >> 2) & 1);
1063		} else if (i==1) { /* Voltages */
1064			/* Are we using thermistors? */
1065			if (*devid == 0xE9) { /* PC87366 */
1066				/* These registers are not logical-device
1067				   specific, just that we won't need them if
1068				   we don't use the VLM device */
1069				confreg[2] = superio_inb(sioaddr, 0x2B);
1070				confreg[3] = superio_inb(sioaddr, 0x25);
1071
1072				if (confreg[2] & 0x40) {
1073					pr_info("Using thermistors for "
1074						"temperature monitoring\n");
1075				}
1076				if (confreg[3] & 0xE0) {
1077					pr_info("VID inputs routed (mode %u)\n",
1078						confreg[3] >> 5);
1079				}
1080			}
1081		}
1082	}
1083
1084	superio_exit(sioaddr);
1085	return 0;
 
 
 
 
 
1086}
1087
1088static int __devinit pc87360_probe(struct platform_device *pdev)
 
 
1089{
1090	int i;
1091	struct pc87360_data *data;
1092	int err = 0;
1093	const char *name = "pc87360";
1094	int use_thermistors = 0;
1095	struct device *dev = &pdev->dev;
1096
1097	if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
1098		return -ENOMEM;
1099
1100	data->fannr = 2;
1101	data->innr = 0;
1102	data->tempnr = 0;
1103
1104	switch (devid) {
1105	case 0xe8:
1106		name = "pc87363";
1107		break;
1108	case 0xe4:
1109		name = "pc87364";
1110		data->fannr = 3;
1111		break;
1112	case 0xe5:
1113		name = "pc87365";
1114		data->fannr = extra_isa[0] ? 3 : 0;
1115		data->innr = extra_isa[1] ? 11 : 0;
1116		data->tempnr = extra_isa[2] ? 2 : 0;
1117		break;
1118	case 0xe9:
1119		name = "pc87366";
1120		data->fannr = extra_isa[0] ? 3 : 0;
1121		data->innr = extra_isa[1] ? 14 : 0;
1122		data->tempnr = extra_isa[2] ? 3 : 0;
1123		break;
1124	}
1125
1126	data->name = name;
1127	data->valid = 0;
1128	mutex_init(&data->lock);
1129	mutex_init(&data->update_lock);
1130	platform_set_drvdata(pdev, data);
1131
1132	for (i = 0; i < LDNI_MAX; i++) {
1133		if (((data->address[i] = extra_isa[i]))
1134		 && !request_region(extra_isa[i], PC87360_EXTENT,
1135		 		    pc87360_driver.driver.name)) {
1136			dev_err(dev, "Region 0x%x-0x%x already "
1137				"in use!\n", extra_isa[i],
1138				extra_isa[i]+PC87360_EXTENT-1);
1139			for (i--; i >= 0; i--)
1140				release_region(extra_isa[i], PC87360_EXTENT);
1141			err = -EBUSY;
1142			goto ERROR1;
1143		}
1144	}
1145
1146	/* Retrieve the fans configuration from Super-I/O space */
1147	if (data->fannr)
1148		data->fan_conf = confreg[0] | (confreg[1] << 8);
1149
1150	/* Use the correct reference voltage
1151	   Unless both the VLM and the TMS logical devices agree to
1152	   use an external Vref, the internal one is used. */
1153	if (data->innr) {
1154		i = pc87360_read_value(data, LD_IN, NO_BANK,
1155				       PC87365_REG_IN_CONFIG);
1156		if (data->tempnr) {
1157			i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1158						PC87365_REG_TEMP_CONFIG);
1159		}
1160		data->in_vref = (i&0x02) ? 3025 : 2966;
1161		dev_dbg(dev, "Using %s reference voltage\n",
1162			(i&0x02) ? "external" : "internal");
1163
1164		data->vid_conf = confreg[3];
1165		data->vrm = vid_which_vrm();
1166	}
1167
1168	/* Fan clock dividers may be needed before any data is read */
1169	for (i = 0; i < data->fannr; i++) {
1170		if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1171			data->fan_status[i] = pc87360_read_value(data,
1172					      LD_FAN, NO_BANK,
1173					      PC87360_REG_FAN_STATUS(i));
1174	}
1175
1176	if (init > 0) {
1177		if (devid == 0xe9 && data->address[1]) /* PC87366 */
1178			use_thermistors = confreg[2] & 0x40;
1179
1180		pc87360_init_device(pdev, use_thermistors);
1181	}
1182
1183	/* Register all-or-nothing sysfs groups */
1184
1185	if (data->innr &&
1186	    (err = sysfs_create_group(&dev->kobj,
1187				      &pc8736x_vin_group)))
1188		goto ERROR3;
1189
1190	if (data->innr == 14 &&
1191	    (err = sysfs_create_group(&dev->kobj,
1192				      &pc8736x_therm_group)))
1193		goto ERROR3;
1194
1195	/* create device attr-files for varying sysfs groups */
 
 
1196
1197	if (data->tempnr) {
1198		for (i = 0; i < data->tempnr; i++) {
1199			if ((err = device_create_file(dev,
1200					&temp_input[i].dev_attr))
1201			    || (err = device_create_file(dev,
1202					&temp_min[i].dev_attr))
1203			    || (err = device_create_file(dev,
1204					&temp_max[i].dev_attr))
1205			    || (err = device_create_file(dev,
1206					&temp_crit[i].dev_attr))
1207			    || (err = device_create_file(dev,
1208					&temp_status[i].dev_attr))
1209			    || (err = device_create_file(dev,
1210					&temp_min_alarm[i].dev_attr))
1211			    || (err = device_create_file(dev,
1212					&temp_max_alarm[i].dev_attr))
1213			    || (err = device_create_file(dev,
1214					&temp_crit_alarm[i].dev_attr))
1215			    || (err = device_create_file(dev,
1216					&temp_fault[i].dev_attr)))
1217				goto ERROR3;
1218		}
1219		if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1220			goto ERROR3;
1221	}
1222
1223	for (i = 0; i < data->fannr; i++) {
1224		if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1225		    && ((err = device_create_file(dev,
1226					&fan_input[i].dev_attr))
1227			|| (err = device_create_file(dev,
1228					&fan_min[i].dev_attr))
1229			|| (err = device_create_file(dev,
1230					&fan_div[i].dev_attr))
1231			|| (err = device_create_file(dev,
1232					&fan_status[i].dev_attr))))
1233			goto ERROR3;
1234
1235		if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1236		    && (err = device_create_file(dev, &pwm[i].dev_attr)))
1237			goto ERROR3;
1238	}
 
 
 
 
1239
1240	if ((err = device_create_file(dev, &dev_attr_name)))
1241		goto ERROR3;
1242
1243	data->hwmon_dev = hwmon_device_register(dev);
1244	if (IS_ERR(data->hwmon_dev)) {
1245		err = PTR_ERR(data->hwmon_dev);
1246		goto ERROR3;
1247	}
1248	return 0;
1249
1250ERROR3:
1251	device_remove_file(dev, &dev_attr_name);
1252	/* can still remove groups whose members were added individually */
1253	sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1254	sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1255	sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1256	sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1257	for (i = 0; i < 3; i++) {
1258		if (data->address[i]) {
1259			release_region(data->address[i], PC87360_EXTENT);
1260		}
1261	}
1262ERROR1:
1263	kfree(data);
1264	return err;
1265}
1266
1267static int __devexit pc87360_remove(struct platform_device *pdev)
1268{
1269	struct pc87360_data *data = platform_get_drvdata(pdev);
1270	int i;
 
1271
1272	hwmon_device_unregister(data->hwmon_dev);
 
 
 
 
 
 
1273
1274	device_remove_file(&pdev->dev, &dev_attr_name);
1275	sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1276	sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1277	sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1278	sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
1279
1280	for (i = 0; i < 3; i++) {
1281		if (data->address[i]) {
1282			release_region(data->address[i], PC87360_EXTENT);
1283		}
1284	}
1285	kfree(data);
1286
1287	return 0;
 
 
 
 
 
 
 
 
1288}
1289
1290/* ldi is the logical device index
1291   bank is for voltages and temperatures only */
1292static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1293			      u8 reg)
1294{
1295	int res;
 
 
 
1296
1297	mutex_lock(&(data->lock));
1298	if (bank != NO_BANK)
1299		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1300	res = inb_p(data->address[ldi] + reg);
1301	mutex_unlock(&(data->lock));
1302
1303	return res;
 
 
 
 
 
 
1304}
1305
1306static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1307				u8 reg, u8 value)
 
 
 
 
 
 
1308{
1309	mutex_lock(&(data->lock));
1310	if (bank != NO_BANK)
1311		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1312	outb_p(value, data->address[ldi] + reg);
1313	mutex_unlock(&(data->lock));
1314}
1315
1316/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1317#define CHAN_CNVRTD	0x80	/* new data ready */
1318#define CHAN_ENA	0x01	/* enabled channel (temp or vin) */
1319#define CHAN_ALM_ENA	0x10	/* propagate to alarms-reg ?? (chk val!) */
1320#define CHAN_READY	(CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1321
1322#define TEMP_OTS_OE	0x20	/* OTS Output Enable */
1323#define VIN_RW1C_MASK	(CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN)   /* 0x87 */
1324#define TEMP_RW1C_MASK	(VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
 
 
 
 
 
 
 
 
 
 
 
 
1325
1326static void pc87360_init_device(struct platform_device *pdev,
1327				int use_thermistors)
1328{
1329	struct pc87360_data *data = platform_get_drvdata(pdev);
1330	int i, nr;
1331	const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1332	const u8 init_temp[3] = { 2, 2, 1 };
1333	u8 reg;
1334
1335	if (init >= 2 && data->innr) {
1336		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1337					 PC87365_REG_IN_CONVRATE);
1338		dev_info(&pdev->dev, "VLM conversion set to "
1339			 "1s period, 160us delay\n");
1340		pc87360_write_value(data, LD_IN, NO_BANK,
1341				    PC87365_REG_IN_CONVRATE,
1342				    (reg & 0xC0) | 0x11);
1343	}
1344
1345	nr = data->innr < 11 ? data->innr : 11;
1346	for (i = 0; i < nr; i++) {
1347		reg = pc87360_read_value(data, LD_IN, i,
1348					 PC87365_REG_IN_STATUS);
1349		dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1350		if (init >= init_in[i]) {
1351			/* Forcibly enable voltage channel */
1352			if (!(reg & CHAN_ENA)) {
1353				dev_dbg(&pdev->dev, "Forcibly "
1354					"enabling in%d\n", i);
1355				pc87360_write_value(data, LD_IN, i,
1356						    PC87365_REG_IN_STATUS,
1357						    (reg & 0x68) | 0x87);
1358			}
1359		}
1360	}
1361
1362	/* We can't blindly trust the Super-I/O space configuration bit,
1363	   most BIOS won't set it properly */
 
 
1364	dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1365	for (i = 11; i < data->innr; i++) {
1366		reg = pc87360_read_value(data, LD_IN, i,
1367					 PC87365_REG_TEMP_STATUS);
1368		use_thermistors = use_thermistors || (reg & CHAN_ENA);
1369		/* thermistors are temp[4-6], measured on vin[11-14] */
1370		dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1371	}
1372	dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1373
1374	i = use_thermistors ? 2 : 0;
1375	for (; i < data->tempnr; i++) {
1376		reg = pc87360_read_value(data, LD_TEMP, i,
1377					 PC87365_REG_TEMP_STATUS);
1378		dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i+1, reg);
1379		if (init >= init_temp[i]) {
1380			/* Forcibly enable temperature channel */
1381			if (!(reg & CHAN_ENA)) {
1382				dev_dbg(&pdev->dev, "Forcibly "
1383					"enabling temp%d\n", i+1);
1384				pc87360_write_value(data, LD_TEMP, i,
1385						    PC87365_REG_TEMP_STATUS,
1386						    0xCF);
1387			}
1388		}
1389	}
1390
1391	if (use_thermistors) {
1392		for (i = 11; i < data->innr; i++) {
1393			if (init >= init_in[i]) {
1394				/* The pin may already be used by thermal
1395				   diodes */
 
 
1396				reg = pc87360_read_value(data, LD_TEMP,
1397				      (i-11)/2, PC87365_REG_TEMP_STATUS);
1398				if (reg & CHAN_ENA) {
1399					dev_dbg(&pdev->dev, "Skipping "
1400						"temp%d, pin already in use "
1401						"by temp%d\n", i-7, (i-11)/2);
1402					continue;
1403				}
1404
1405				/* Forcibly enable thermistor channel */
1406				reg = pc87360_read_value(data, LD_IN, i,
1407							 PC87365_REG_IN_STATUS);
1408				if (!(reg & CHAN_ENA)) {
1409					dev_dbg(&pdev->dev, "Forcibly "
1410						"enabling temp%d\n", i-7);
 
1411					pc87360_write_value(data, LD_IN, i,
1412						PC87365_REG_TEMP_STATUS,
1413						(reg & 0x60) | 0x8F);
1414				}
1415			}
1416		}
1417	}
1418
1419	if (data->innr) {
1420		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1421					 PC87365_REG_IN_CONFIG);
1422		dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1423		if (reg & CHAN_ENA) {
1424			dev_dbg(&pdev->dev, "Forcibly "
1425				"enabling monitoring (VLM)\n");
1426			pc87360_write_value(data, LD_IN, NO_BANK,
1427					    PC87365_REG_IN_CONFIG,
1428					    reg & 0xFE);
1429		}
1430	}
1431
1432	if (data->tempnr) {
1433		reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1434					 PC87365_REG_TEMP_CONFIG);
1435		dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1436		if (reg & CHAN_ENA) {
1437			dev_dbg(&pdev->dev, "Forcibly enabling "
1438				"monitoring (TMS)\n");
1439			pc87360_write_value(data, LD_TEMP, NO_BANK,
1440					    PC87365_REG_TEMP_CONFIG,
1441					    reg & 0xFE);
1442		}
1443
1444		if (init >= 2) {
1445			/* Chip config as documented by National Semi. */
1446			pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1447			/* We voluntarily omit the bank here, in case the
1448			   sequence itself matters. It shouldn't be a problem,
1449			   since nobody else is supposed to access the
1450			   device at that point. */
 
 
1451			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1452			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1453			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1454			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1455		}
1456	}
1457}
1458
1459static void pc87360_autodiv(struct device *dev, int nr)
1460{
1461	struct pc87360_data *data = dev_get_drvdata(dev);
1462	u8 old_min = data->fan_min[nr];
 
 
 
 
1463
1464	/* Increase clock divider if needed and possible */
1465	if ((data->fan_status[nr] & 0x04) /* overflow flag */
1466	 || (data->fan[nr] >= 224)) { /* next to overflow */
1467		if ((data->fan_status[nr] & 0x60) != 0x60) {
1468			data->fan_status[nr] += 0x20;
1469			data->fan_min[nr] >>= 1;
1470			data->fan[nr] >>= 1;
1471			dev_dbg(dev, "Increasing "
1472				"clock divider to %d for fan %d\n",
1473				FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1474		}
1475	} else {
1476		/* Decrease clock divider if possible */
1477		while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1478		 && data->fan[nr] < 85 /* bad accuracy */
1479		 && (data->fan_status[nr] & 0x60) != 0x00) {
1480			data->fan_status[nr] -= 0x20;
1481			data->fan_min[nr] <<= 1;
1482			data->fan[nr] <<= 1;
1483			dev_dbg(dev, "Decreasing "
1484				"clock divider to %d for fan %d\n",
1485				FAN_DIV_FROM_REG(data->fan_status[nr]),
1486				nr+1);
 
 
 
 
 
1487		}
 
 
 
 
 
 
1488	}
1489
1490	/* Write new fan min if it changed */
1491	if (old_min != data->fan_min[nr]) {
1492		pc87360_write_value(data, LD_FAN, NO_BANK,
1493				    PC87360_REG_FAN_MIN(nr),
1494				    data->fan_min[nr]);
 
1495	}
1496}
1497
1498static struct pc87360_data *pc87360_update_device(struct device *dev)
1499{
1500	struct pc87360_data *data = dev_get_drvdata(dev);
1501	u8 i;
1502
1503	mutex_lock(&data->update_lock);
 
1504
1505	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1506		dev_dbg(dev, "Data update\n");
1507
1508		/* Fans */
1509		for (i = 0; i < data->fannr; i++) {
1510			if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1511				data->fan_status[i] =
1512					pc87360_read_value(data, LD_FAN,
1513					NO_BANK, PC87360_REG_FAN_STATUS(i));
1514				data->fan[i] = pc87360_read_value(data, LD_FAN,
1515					       NO_BANK, PC87360_REG_FAN(i));
1516				data->fan_min[i] = pc87360_read_value(data,
1517						   LD_FAN, NO_BANK,
1518						   PC87360_REG_FAN_MIN(i));
1519				/* Change clock divider if needed */
1520				pc87360_autodiv(dev, i);
1521				/* Clear bits and write new divider */
1522				pc87360_write_value(data, LD_FAN, NO_BANK,
1523						    PC87360_REG_FAN_STATUS(i),
1524						    data->fan_status[i]);
1525			}
1526			if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1527				data->pwm[i] = pc87360_read_value(data, LD_FAN,
1528					       NO_BANK, PC87360_REG_PWM(i));
1529		}
 
 
 
 
1530
1531		/* Voltages */
1532		for (i = 0; i < data->innr; i++) {
1533			data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1534					     PC87365_REG_IN_STATUS);
1535			/* Clear bits */
1536			pc87360_write_value(data, LD_IN, i,
1537					    PC87365_REG_IN_STATUS,
1538					    data->in_status[i]);
1539			if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1540				data->in[i] = pc87360_read_value(data, LD_IN,
1541					      i, PC87365_REG_IN);
1542			}
1543			if (data->in_status[i] & CHAN_ENA) {
1544				data->in_min[i] = pc87360_read_value(data,
1545						  LD_IN, i,
1546						  PC87365_REG_IN_MIN);
1547				data->in_max[i] = pc87360_read_value(data,
1548						  LD_IN, i,
1549						  PC87365_REG_IN_MAX);
1550				if (i >= 11)
1551					data->in_crit[i-11] =
1552						pc87360_read_value(data, LD_IN,
1553						i, PC87365_REG_TEMP_CRIT);
1554			}
1555		}
1556		if (data->innr) {
1557			data->in_alarms = pc87360_read_value(data, LD_IN,
1558					  NO_BANK, PC87365_REG_IN_ALARMS1)
1559					| ((pc87360_read_value(data, LD_IN,
1560					    NO_BANK, PC87365_REG_IN_ALARMS2)
1561					    & 0x07) << 8);
1562			data->vid = (data->vid_conf & 0xE0) ?
1563				    pc87360_read_value(data, LD_IN,
1564				    NO_BANK, PC87365_REG_VID) : 0x1F;
1565		}
 
1566
1567		/* Temperatures */
1568		for (i = 0; i < data->tempnr; i++) {
1569			data->temp_status[i] = pc87360_read_value(data,
1570					       LD_TEMP, i,
1571					       PC87365_REG_TEMP_STATUS);
1572			/* Clear bits */
1573			pc87360_write_value(data, LD_TEMP, i,
1574					    PC87365_REG_TEMP_STATUS,
1575					    data->temp_status[i]);
1576			if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1577				data->temp[i] = pc87360_read_value(data,
1578						LD_TEMP, i,
1579						PC87365_REG_TEMP);
1580			}
1581			if (data->temp_status[i] & CHAN_ENA) {
1582				data->temp_min[i] = pc87360_read_value(data,
1583						    LD_TEMP, i,
1584						    PC87365_REG_TEMP_MIN);
1585				data->temp_max[i] = pc87360_read_value(data,
1586						    LD_TEMP, i,
1587						    PC87365_REG_TEMP_MAX);
1588				data->temp_crit[i] = pc87360_read_value(data,
1589						     LD_TEMP, i,
1590						     PC87365_REG_TEMP_CRIT);
1591			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1592		}
1593		if (data->tempnr) {
1594			data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1595					    NO_BANK, PC87365_REG_TEMP_ALARMS)
1596					    & 0x3F;
 
 
 
1597		}
1598
1599		data->last_updated = jiffies;
1600		data->valid = 1;
1601	}
1602
1603	mutex_unlock(&data->update_lock);
 
 
1604
1605	return data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1606}
1607
1608static int __init pc87360_device_add(unsigned short address)
1609{
1610	struct resource res[3];
1611	int err, i, res_count;
1612
1613	pdev = platform_device_alloc("pc87360", address);
1614	if (!pdev) {
1615		err = -ENOMEM;
1616		pr_err("Device allocation failed\n");
1617		goto exit;
1618	}
1619
1620	memset(res, 0, 3 * sizeof(struct resource));
1621	res_count = 0;
1622	for (i = 0; i < 3; i++) {
1623		if (!extra_isa[i])
1624			continue;
1625		res[res_count].start = extra_isa[i];
1626		res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1627		res[res_count].name = "pc87360",
1628		res[res_count].flags = IORESOURCE_IO,
1629
1630		err = acpi_check_resource_conflict(&res[res_count]);
1631		if (err)
1632			goto exit_device_put;
1633
1634		res_count++;
1635	}
1636
1637	err = platform_device_add_resources(pdev, res, res_count);
1638	if (err) {
1639		pr_err("Device resources addition failed (%d)\n", err);
1640		goto exit_device_put;
1641	}
1642
1643	err = platform_device_add(pdev);
1644	if (err) {
1645		pr_err("Device addition failed (%d)\n", err);
1646		goto exit_device_put;
1647	}
1648
1649	return 0;
1650
1651exit_device_put:
1652	platform_device_put(pdev);
1653exit:
1654	return err;
1655}
1656
1657static int __init pc87360_init(void)
1658{
1659	int err, i;
1660	unsigned short address = 0;
1661
1662	if (pc87360_find(0x2e, &devid, extra_isa)
1663	 && pc87360_find(0x4e, &devid, extra_isa)) {
1664		pr_warn("PC8736x not detected, module not inserted\n");
1665		return -ENODEV;
1666	}
1667
1668	/* Arbitrarily pick one of the addresses */
1669	for (i = 0; i < 3; i++) {
1670		if (extra_isa[i] != 0x0000) {
1671			address = extra_isa[i];
1672			break;
1673		}
1674	}
1675
1676	if (address == 0x0000) {
1677		pr_warn("No active logical device, module not inserted\n");
1678		return -ENODEV;
1679	}
1680
1681	err = platform_driver_register(&pc87360_driver);
1682	if (err)
1683		goto exit;
1684
1685	/* Sets global pdev as a side effect */
1686	err = pc87360_device_add(address);
1687	if (err)
1688		goto exit_driver;
1689
1690	return 0;
1691
1692 exit_driver:
1693	platform_driver_unregister(&pc87360_driver);
1694 exit:
1695	return err;
1696}
1697
1698static void __exit pc87360_exit(void)
1699{
1700	platform_device_unregister(pdev);
1701	platform_driver_unregister(&pc87360_driver);
1702}
1703
1704
1705MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1706MODULE_DESCRIPTION("PC8736x hardware monitor");
1707MODULE_LICENSE("GPL");
 
1708
1709module_init(pc87360_init);
1710module_exit(pc87360_exit);
v6.2
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  pc87360.c - Part of lm_sensors, Linux kernel modules
   4 *              for hardware monitoring
   5 *  Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
   6 *
   7 *  Copied from smsc47m1.c:
   8 *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
   9 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  10 *  Supports the following chips:
  11 *
  12 *  Chip        #vin    #fan    #pwm    #temp   devid
  13 *  PC87360     -       2       2       -       0xE1
  14 *  PC87363     -       2       2       -       0xE8
  15 *  PC87364     -       3       3       -       0xE4
  16 *  PC87365     11      3       3       2       0xE5
  17 *  PC87366     11      3       3       3-4     0xE9
  18 *
  19 *  This driver assumes that no more than one chip is present, and one of
  20 *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
  21 */
  22
  23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24
  25#include <linux/module.h>
  26#include <linux/init.h>
  27#include <linux/slab.h>
  28#include <linux/jiffies.h>
  29#include <linux/platform_device.h>
  30#include <linux/hwmon.h>
  31#include <linux/hwmon-sysfs.h>
  32#include <linux/hwmon-vid.h>
  33#include <linux/err.h>
  34#include <linux/mutex.h>
  35#include <linux/acpi.h>
  36#include <linux/io.h>
  37
  38#define DRIVER_NAME "pc87360"
  39
  40/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
  41#define CHAN_CNVRTD	0x80	/* new data ready */
  42#define CHAN_ENA	0x01	/* enabled channel (temp or vin) */
  43#define CHAN_ALM_ENA	0x10	/* propagate to alarms-reg ?? (chk val!) */
  44#define CHAN_READY	(CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
  45
  46#define TEMP_OTS_OE	0x20	/* OTS Output Enable */
  47#define VIN_RW1C_MASK	(CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN)   /* 0x87 */
  48#define TEMP_RW1C_MASK	(VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
  49
  50static u8 devid;
  51static struct platform_device *pdev;
  52static unsigned short extra_isa[3];
  53static u8 confreg[4];
  54
  55static int init = 1;
  56module_param(init, int, 0);
  57MODULE_PARM_DESC(init,
  58"Chip initialization level:\n"
  59" 0: None\n"
  60"*1: Forcibly enable internal voltage and temperature channels, except in9\n"
  61" 2: Forcibly enable all voltage and temperature channels, except in9\n"
  62" 3: Forcibly enable all voltage and temperature channels, including in9");
  63
  64static unsigned short force_id;
  65module_param(force_id, ushort, 0);
  66MODULE_PARM_DESC(force_id, "Override the detected device ID");
  67
  68/*
  69 * Super-I/O registers and operations
  70 */
  71
  72#define DEV	0x07	/* Register: Logical device select */
  73#define DEVID	0x20	/* Register: Device ID */
  74#define ACT	0x30	/* Register: Device activation */
  75#define BASE	0x60	/* Register: Base address */
  76
  77#define FSCM	0x09	/* Logical device: fans */
  78#define VLM	0x0d	/* Logical device: voltages */
  79#define TMS	0x0e	/* Logical device: temperatures */
  80#define LDNI_MAX 3
  81static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
  82
  83#define LD_FAN		0
  84#define LD_IN		1
  85#define LD_TEMP		2
  86
  87static inline void superio_outb(int sioaddr, int reg, int val)
  88{
  89	outb(reg, sioaddr);
  90	outb(val, sioaddr + 1);
  91}
  92
  93static inline int superio_inb(int sioaddr, int reg)
  94{
  95	outb(reg, sioaddr);
  96	return inb(sioaddr + 1);
  97}
  98
  99static inline void superio_exit(int sioaddr)
 100{
 101	outb(0x02, sioaddr);
 102	outb(0x02, sioaddr + 1);
 103}
 104
 105/*
 106 * Logical devices
 107 */
 108
 109#define PC87360_EXTENT		0x10
 110#define PC87365_REG_BANK	0x09
 111#define NO_BANK			0xff
 112
 113/*
 114 * Fan registers and conversions
 115 */
 116
 117/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
 118#define PC87360_REG_PRESCALE(nr)	(0x00 + 2 * (nr))
 119#define PC87360_REG_PWM(nr)		(0x01 + 2 * (nr))
 120#define PC87360_REG_FAN_MIN(nr)		(0x06 + 3 * (nr))
 121#define PC87360_REG_FAN(nr)		(0x07 + 3 * (nr))
 122#define PC87360_REG_FAN_STATUS(nr)	(0x08 + 3 * (nr))
 123
 124#define FAN_FROM_REG(val, div)		((val) == 0 ? 0 : \
 125					 480000 / ((val) * (div)))
 126#define FAN_TO_REG(val, div)		((val) <= 100 ? 0 : \
 127					 480000 / ((val) * (div)))
 128#define FAN_DIV_FROM_REG(val)		(1 << (((val) >> 5) & 0x03))
 129#define FAN_STATUS_FROM_REG(val)	((val) & 0x07)
 130
 131#define FAN_CONFIG_MONITOR(val, nr)	(((val) >> (2 + (nr) * 3)) & 1)
 132#define FAN_CONFIG_CONTROL(val, nr)	(((val) >> (3 + (nr) * 3)) & 1)
 133#define FAN_CONFIG_INVERT(val, nr)	(((val) >> (4 + (nr) * 3)) & 1)
 134
 135#define PWM_FROM_REG(val, inv)		((inv) ? 255 - (val) : (val))
 136static inline u8 PWM_TO_REG(int val, int inv)
 137{
 138	if (inv)
 139		val = 255 - val;
 140	if (val < 0)
 141		return 0;
 142	if (val > 255)
 143		return 255;
 144	return val;
 145}
 146
 147/*
 148 * Voltage registers and conversions
 149 */
 150
 151#define PC87365_REG_IN_CONVRATE		0x07
 152#define PC87365_REG_IN_CONFIG		0x08
 153#define PC87365_REG_IN			0x0B
 154#define PC87365_REG_IN_MIN		0x0D
 155#define PC87365_REG_IN_MAX		0x0C
 156#define PC87365_REG_IN_STATUS		0x0A
 157#define PC87365_REG_IN_ALARMS1		0x00
 158#define PC87365_REG_IN_ALARMS2		0x01
 159#define PC87365_REG_VID			0x06
 160
 161#define IN_FROM_REG(val, ref)		(((val) * (ref) + 128) / 256)
 162#define IN_TO_REG(val, ref)		((val) < 0 ? 0 : \
 163					 (val) * 256 >= (ref) * 255 ? 255 : \
 164					 ((val) * 256 + (ref) / 2) / (ref))
 165
 166/*
 167 * Temperature registers and conversions
 168 */
 169
 170#define PC87365_REG_TEMP_CONFIG		0x08
 171#define PC87365_REG_TEMP		0x0B
 172#define PC87365_REG_TEMP_MIN		0x0D
 173#define PC87365_REG_TEMP_MAX		0x0C
 174#define PC87365_REG_TEMP_CRIT		0x0E
 175#define PC87365_REG_TEMP_STATUS		0x0A
 176#define PC87365_REG_TEMP_ALARMS		0x00
 177
 178#define TEMP_FROM_REG(val)		((val) * 1000)
 179#define TEMP_TO_REG(val)		((val) < -55000 ? -55 : \
 180					 (val) > 127000 ? 127 : \
 181					 (val) < 0 ? ((val) - 500) / 1000 : \
 182					 ((val) + 500) / 1000)
 183
 184/*
 185 * Device data
 186 */
 187
 188struct pc87360_data {
 189	const char *name;
 190	struct device *hwmon_dev;
 191	struct mutex lock;
 192	struct mutex update_lock;
 193	bool valid;		/* true if following fields are valid */
 194	unsigned long last_updated;	/* In jiffies */
 195
 196	int address[3];
 197
 198	u8 fannr, innr, tempnr;
 199
 200	u8 fan[3];		/* Register value */
 201	u8 fan_min[3];		/* Register value */
 202	u8 fan_status[3];	/* Register value */
 203	u8 pwm[3];		/* Register value */
 204	u16 fan_conf;		/* Configuration register values, combined */
 205
 206	u16 in_vref;		/* 1 mV/bit */
 207	u8 in[14];		/* Register value */
 208	u8 in_min[14];		/* Register value */
 209	u8 in_max[14];		/* Register value */
 210	u8 in_crit[3];		/* Register value */
 211	u8 in_status[14];	/* Register value */
 212	u16 in_alarms;		/* Register values, combined, masked */
 213	u8 vid_conf;		/* Configuration register value */
 214	u8 vrm;
 215	u8 vid;			/* Register value */
 216
 217	s8 temp[3];		/* Register value */
 218	s8 temp_min[3];		/* Register value */
 219	s8 temp_max[3];		/* Register value */
 220	s8 temp_crit[3];	/* Register value */
 221	u8 temp_status[3];	/* Register value */
 222	u8 temp_alarms;		/* Register value, masked */
 223};
 224
 225/*
 226 * ldi is the logical device index
 227 * bank is for voltages and temperatures only
 228 */
 
 
 
 
 229static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
 230			      u8 reg)
 231{
 232	int res;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 233
 234	mutex_lock(&(data->lock));
 235	if (bank != NO_BANK)
 236		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
 237	res = inb_p(data->address[ldi] + reg);
 238	mutex_unlock(&(data->lock));
 239
 240	return res;
 
 
 
 
 
 
 
 
 
 
 
 
 241}
 242
 243static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
 244				u8 reg, u8 value)
 245{
 246	mutex_lock(&(data->lock));
 247	if (bank != NO_BANK)
 248		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
 249	outb_p(value, data->address[ldi] + reg);
 250	mutex_unlock(&(data->lock));
 251}
 252
 253static void pc87360_autodiv(struct device *dev, int nr)
 254{
 255	struct pc87360_data *data = dev_get_drvdata(dev);
 256	u8 old_min = data->fan_min[nr];
 257
 258	/* Increase clock divider if needed and possible */
 259	if ((data->fan_status[nr] & 0x04) /* overflow flag */
 260	 || (data->fan[nr] >= 224)) { /* next to overflow */
 261		if ((data->fan_status[nr] & 0x60) != 0x60) {
 262			data->fan_status[nr] += 0x20;
 263			data->fan_min[nr] >>= 1;
 264			data->fan[nr] >>= 1;
 265			dev_dbg(dev,
 266				"Increasing clock divider to %d for fan %d\n",
 267				FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
 268		}
 269	} else {
 270		/* Decrease clock divider if possible */
 271		while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
 272		 && data->fan[nr] < 85 /* bad accuracy */
 273		 && (data->fan_status[nr] & 0x60) != 0x00) {
 274			data->fan_status[nr] -= 0x20;
 275			data->fan_min[nr] <<= 1;
 276			data->fan[nr] <<= 1;
 277			dev_dbg(dev,
 278				"Decreasing clock divider to %d for fan %d\n",
 279				FAN_DIV_FROM_REG(data->fan_status[nr]),
 280				nr + 1);
 281		}
 282	}
 283
 284	/* Write new fan min if it changed */
 285	if (old_min != data->fan_min[nr]) {
 286		pc87360_write_value(data, LD_FAN, NO_BANK,
 287				    PC87360_REG_FAN_MIN(nr),
 288				    data->fan_min[nr]);
 289	}
 290}
 291
 292static struct pc87360_data *pc87360_update_device(struct device *dev)
 293{
 
 294	struct pc87360_data *data = dev_get_drvdata(dev);
 295	u8 i;
 296
 297	mutex_lock(&data->update_lock);
 
 298
 299	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
 300		dev_dbg(dev, "Data update\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 301
 302		/* Fans */
 303		for (i = 0; i < data->fannr; i++) {
 304			if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
 305				data->fan_status[i] =
 306					pc87360_read_value(data, LD_FAN,
 307					NO_BANK, PC87360_REG_FAN_STATUS(i));
 308				data->fan[i] = pc87360_read_value(data, LD_FAN,
 309					       NO_BANK, PC87360_REG_FAN(i));
 310				data->fan_min[i] = pc87360_read_value(data,
 311						   LD_FAN, NO_BANK,
 312						   PC87360_REG_FAN_MIN(i));
 313				/* Change clock divider if needed */
 314				pc87360_autodiv(dev, i);
 315				/* Clear bits and write new divider */
 316				pc87360_write_value(data, LD_FAN, NO_BANK,
 317						    PC87360_REG_FAN_STATUS(i),
 318						    data->fan_status[i]);
 319			}
 320			if (FAN_CONFIG_CONTROL(data->fan_conf, i))
 321				data->pwm[i] = pc87360_read_value(data, LD_FAN,
 322					       NO_BANK, PC87360_REG_PWM(i));
 323		}
 324
 325		/* Voltages */
 326		for (i = 0; i < data->innr; i++) {
 327			data->in_status[i] = pc87360_read_value(data, LD_IN, i,
 328					     PC87365_REG_IN_STATUS);
 329			/* Clear bits */
 330			pc87360_write_value(data, LD_IN, i,
 331					    PC87365_REG_IN_STATUS,
 332					    data->in_status[i]);
 333			if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
 334				data->in[i] = pc87360_read_value(data, LD_IN,
 335					      i, PC87365_REG_IN);
 336			}
 337			if (data->in_status[i] & CHAN_ENA) {
 338				data->in_min[i] = pc87360_read_value(data,
 339						  LD_IN, i,
 340						  PC87365_REG_IN_MIN);
 341				data->in_max[i] = pc87360_read_value(data,
 342						  LD_IN, i,
 343						  PC87365_REG_IN_MAX);
 344				if (i >= 11)
 345					data->in_crit[i-11] =
 346						pc87360_read_value(data, LD_IN,
 347						i, PC87365_REG_TEMP_CRIT);
 348			}
 349		}
 350		if (data->innr) {
 351			data->in_alarms = pc87360_read_value(data, LD_IN,
 352					  NO_BANK, PC87365_REG_IN_ALARMS1)
 353					| ((pc87360_read_value(data, LD_IN,
 354					    NO_BANK, PC87365_REG_IN_ALARMS2)
 355					    & 0x07) << 8);
 356			data->vid = (data->vid_conf & 0xE0) ?
 357				    pc87360_read_value(data, LD_IN,
 358				    NO_BANK, PC87365_REG_VID) : 0x1F;
 359		}
 360
 361		/* Temperatures */
 362		for (i = 0; i < data->tempnr; i++) {
 363			data->temp_status[i] = pc87360_read_value(data,
 364					       LD_TEMP, i,
 365					       PC87365_REG_TEMP_STATUS);
 366			/* Clear bits */
 367			pc87360_write_value(data, LD_TEMP, i,
 368					    PC87365_REG_TEMP_STATUS,
 369					    data->temp_status[i]);
 370			if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
 371				data->temp[i] = pc87360_read_value(data,
 372						LD_TEMP, i,
 373						PC87365_REG_TEMP);
 374			}
 375			if (data->temp_status[i] & CHAN_ENA) {
 376				data->temp_min[i] = pc87360_read_value(data,
 377						    LD_TEMP, i,
 378						    PC87365_REG_TEMP_MIN);
 379				data->temp_max[i] = pc87360_read_value(data,
 380						    LD_TEMP, i,
 381						    PC87365_REG_TEMP_MAX);
 382				data->temp_crit[i] = pc87360_read_value(data,
 383						     LD_TEMP, i,
 384						     PC87365_REG_TEMP_CRIT);
 385			}
 386		}
 387		if (data->tempnr) {
 388			data->temp_alarms = pc87360_read_value(data, LD_TEMP,
 389					    NO_BANK, PC87365_REG_TEMP_ALARMS)
 390					    & 0x3F;
 391		}
 392
 393		data->last_updated = jiffies;
 394		data->valid = true;
 395	}
 
 
 
 
 
 
 
 
 
 
 
 
 396
 
 
 
 
 
 397	mutex_unlock(&data->update_lock);
 
 
 398
 399	return data;
 400}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 401
 402static ssize_t in_input_show(struct device *dev,
 403			     struct device_attribute *devattr, char *buf)
 404{
 405	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 406	struct pc87360_data *data = pc87360_update_device(dev);
 407	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
 408		       data->in_vref));
 409}
 410
 411static struct sensor_device_attribute in_input[] = {
 412	SENSOR_ATTR_RO(in0_input, in_input, 0),
 413	SENSOR_ATTR_RO(in1_input, in_input, 1),
 414	SENSOR_ATTR_RO(in2_input, in_input, 2),
 415	SENSOR_ATTR_RO(in3_input, in_input, 3),
 416	SENSOR_ATTR_RO(in4_input, in_input, 4),
 417	SENSOR_ATTR_RO(in5_input, in_input, 5),
 418	SENSOR_ATTR_RO(in6_input, in_input, 6),
 419	SENSOR_ATTR_RO(in7_input, in_input, 7),
 420	SENSOR_ATTR_RO(in8_input, in_input, 8),
 421	SENSOR_ATTR_RO(in9_input, in_input, 9),
 422	SENSOR_ATTR_RO(in10_input, in_input, 10),
 423};
 424
 425static ssize_t in_status_show(struct device *dev,
 426			      struct device_attribute *devattr, char *buf)
 427{
 428	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 429	struct pc87360_data *data = pc87360_update_device(dev);
 430	return sprintf(buf, "%u\n", data->in_status[attr->index]);
 
 431}
 432
 433static struct sensor_device_attribute in_status[] = {
 434	SENSOR_ATTR_RO(in0_status, in_status, 0),
 435	SENSOR_ATTR_RO(in1_status, in_status, 1),
 436	SENSOR_ATTR_RO(in2_status, in_status, 2),
 437	SENSOR_ATTR_RO(in3_status, in_status, 3),
 438	SENSOR_ATTR_RO(in4_status, in_status, 4),
 439	SENSOR_ATTR_RO(in5_status, in_status, 5),
 440	SENSOR_ATTR_RO(in6_status, in_status, 6),
 441	SENSOR_ATTR_RO(in7_status, in_status, 7),
 442	SENSOR_ATTR_RO(in8_status, in_status, 8),
 443	SENSOR_ATTR_RO(in9_status, in_status, 9),
 444	SENSOR_ATTR_RO(in10_status, in_status, 10),
 445};
 446
 447static ssize_t in_min_show(struct device *dev,
 448			   struct device_attribute *devattr, char *buf)
 449{
 450	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 451	struct pc87360_data *data = pc87360_update_device(dev);
 452	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
 453		       data->in_vref));
 454}
 455
 456static ssize_t in_min_store(struct device *dev,
 457			    struct device_attribute *devattr, const char *buf,
 458			    size_t count)
 
 
 
 
 459{
 460	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 461	struct pc87360_data *data = dev_get_drvdata(dev);
 462	long val;
 463	int err;
 464
 465	err = kstrtol(buf, 10, &val);
 466	if (err)
 467		return err;
 468
 469	mutex_lock(&data->update_lock);
 470	data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
 471	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
 472			    data->in_min[attr->index]);
 473	mutex_unlock(&data->update_lock);
 474	return count;
 475}
 476
 477static struct sensor_device_attribute in_min[] = {
 478	SENSOR_ATTR_RW(in0_min, in_min, 0),
 479	SENSOR_ATTR_RW(in1_min, in_min, 1),
 480	SENSOR_ATTR_RW(in2_min, in_min, 2),
 481	SENSOR_ATTR_RW(in3_min, in_min, 3),
 482	SENSOR_ATTR_RW(in4_min, in_min, 4),
 483	SENSOR_ATTR_RW(in5_min, in_min, 5),
 484	SENSOR_ATTR_RW(in6_min, in_min, 6),
 485	SENSOR_ATTR_RW(in7_min, in_min, 7),
 486	SENSOR_ATTR_RW(in8_min, in_min, 8),
 487	SENSOR_ATTR_RW(in9_min, in_min, 9),
 488	SENSOR_ATTR_RW(in10_min, in_min, 10),
 489};
 490
 491static ssize_t in_max_show(struct device *dev,
 492			   struct device_attribute *devattr, char *buf)
 493{
 494	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 495	struct pc87360_data *data = pc87360_update_device(dev);
 496	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
 497		       data->in_vref));
 498}
 499
 500static ssize_t in_max_store(struct device *dev,
 501			    struct device_attribute *devattr, const char *buf,
 502			    size_t count)
 503{
 504	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 505	struct pc87360_data *data = dev_get_drvdata(dev);
 506	long val;
 507	int err;
 508
 509	err = kstrtol(buf, 10, &val);
 510	if (err)
 511		return err;
 512
 513	mutex_lock(&data->update_lock);
 514	data->in_max[attr->index] = IN_TO_REG(val,
 515			       data->in_vref);
 516	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
 517			    data->in_max[attr->index]);
 518	mutex_unlock(&data->update_lock);
 519	return count;
 520}
 521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 522static struct sensor_device_attribute in_max[] = {
 523	SENSOR_ATTR_RW(in0_max, in_max, 0),
 524	SENSOR_ATTR_RW(in1_max, in_max, 1),
 525	SENSOR_ATTR_RW(in2_max, in_max, 2),
 526	SENSOR_ATTR_RW(in3_max, in_max, 3),
 527	SENSOR_ATTR_RW(in4_max, in_max, 4),
 528	SENSOR_ATTR_RW(in5_max, in_max, 5),
 529	SENSOR_ATTR_RW(in6_max, in_max, 6),
 530	SENSOR_ATTR_RW(in7_max, in_max, 7),
 531	SENSOR_ATTR_RW(in8_max, in_max, 8),
 532	SENSOR_ATTR_RW(in9_max, in_max, 9),
 533	SENSOR_ATTR_RW(in10_max, in_max, 10),
 534};
 535
 536/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
 537#define CHAN_ALM_MIN	0x02	/* min limit crossed */
 538#define CHAN_ALM_MAX	0x04	/* max limit exceeded */
 539#define TEMP_ALM_CRIT	0x08	/* temp crit exceeded (temp only) */
 540
 541/*
 542 * show_in_min/max_alarm() reads data from the per-channel status
 543 * register (sec 11.5.12), not the vin event status registers (sec
 544 * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
 545 */
 546
 547static ssize_t in_min_alarm_show(struct device *dev,
 548				 struct device_attribute *devattr, char *buf)
 549{
 550	struct pc87360_data *data = pc87360_update_device(dev);
 551	unsigned nr = to_sensor_dev_attr(devattr)->index;
 552
 553	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
 554}
 555
 556static struct sensor_device_attribute in_min_alarm[] = {
 557	SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0),
 558	SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1),
 559	SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2),
 560	SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3),
 561	SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4),
 562	SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5),
 563	SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6),
 564	SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7),
 565	SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8),
 566	SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9),
 567	SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10),
 568};
 569
 570static ssize_t in_max_alarm_show(struct device *dev,
 571				 struct device_attribute *devattr, char *buf)
 572{
 573	struct pc87360_data *data = pc87360_update_device(dev);
 574	unsigned nr = to_sensor_dev_attr(devattr)->index;
 575
 576	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
 577}
 578
 
 
 
 
 
 
 
 
 
 
 
 
 
 579static struct sensor_device_attribute in_max_alarm[] = {
 580	SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0),
 581	SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1),
 582	SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2),
 583	SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3),
 584	SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4),
 585	SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5),
 586	SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6),
 587	SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7),
 588	SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8),
 589	SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9),
 590	SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10),
 591};
 592
 593#define VIN_UNIT_ATTRS(X) \
 594	&in_input[X].dev_attr.attr,	\
 595	&in_status[X].dev_attr.attr,	\
 596	&in_min[X].dev_attr.attr,	\
 597	&in_max[X].dev_attr.attr,	\
 598	&in_min_alarm[X].dev_attr.attr,	\
 599	&in_max_alarm[X].dev_attr.attr
 600
 601static ssize_t cpu0_vid_show(struct device *dev,
 602			     struct device_attribute *attr, char *buf)
 603{
 604	struct pc87360_data *data = pc87360_update_device(dev);
 605	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
 606}
 607static DEVICE_ATTR_RO(cpu0_vid);
 608
 609static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
 610			char *buf)
 611{
 612	struct pc87360_data *data = dev_get_drvdata(dev);
 613	return sprintf(buf, "%u\n", data->vrm);
 614}
 615
 616static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
 617			 const char *buf, size_t count)
 618{
 619	struct pc87360_data *data = dev_get_drvdata(dev);
 620	unsigned long val;
 621	int err;
 622
 623	err = kstrtoul(buf, 10, &val);
 624	if (err)
 625		return err;
 626
 627	if (val > 255)
 628		return -EINVAL;
 629
 630	data->vrm = val;
 631	return count;
 632}
 633static DEVICE_ATTR_RW(vrm);
 634
 635static ssize_t alarms_in_show(struct device *dev,
 636			      struct device_attribute *attr, char *buf)
 637{
 638	struct pc87360_data *data = pc87360_update_device(dev);
 639	return sprintf(buf, "%u\n", data->in_alarms);
 640}
 641static DEVICE_ATTR_RO(alarms_in);
 642
 643static struct attribute *pc8736x_vin_attr_array[] = {
 644	VIN_UNIT_ATTRS(0),
 645	VIN_UNIT_ATTRS(1),
 646	VIN_UNIT_ATTRS(2),
 647	VIN_UNIT_ATTRS(3),
 648	VIN_UNIT_ATTRS(4),
 649	VIN_UNIT_ATTRS(5),
 650	VIN_UNIT_ATTRS(6),
 651	VIN_UNIT_ATTRS(7),
 652	VIN_UNIT_ATTRS(8),
 653	VIN_UNIT_ATTRS(9),
 654	VIN_UNIT_ATTRS(10),
 655	&dev_attr_cpu0_vid.attr,
 656	&dev_attr_vrm.attr,
 657	&dev_attr_alarms_in.attr,
 658	NULL
 659};
 660static const struct attribute_group pc8736x_vin_group = {
 661	.attrs = pc8736x_vin_attr_array,
 662};
 663
 664static ssize_t therm_input_show(struct device *dev,
 665				struct device_attribute *devattr, char *buf)
 666{
 667	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 668	struct pc87360_data *data = pc87360_update_device(dev);
 669	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
 670		       data->in_vref));
 671}
 672
 673/*
 674 * the +11 term below reflects the fact that VLM units 11,12,13 are
 675 * used in the chip to measure voltage across the thermistors
 676 */
 677static struct sensor_device_attribute therm_input[] = {
 678	SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11),
 679	SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11),
 680	SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11),
 681};
 682
 683static ssize_t therm_status_show(struct device *dev,
 684				 struct device_attribute *devattr, char *buf)
 685{
 686	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 687	struct pc87360_data *data = pc87360_update_device(dev);
 688	return sprintf(buf, "%u\n", data->in_status[attr->index]);
 
 689}
 690
 691static struct sensor_device_attribute therm_status[] = {
 692	SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11),
 693	SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11),
 694	SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11),
 695};
 696
 697static ssize_t therm_min_show(struct device *dev,
 698			      struct device_attribute *devattr, char *buf)
 699{
 700	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 701	struct pc87360_data *data = pc87360_update_device(dev);
 702	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
 703		       data->in_vref));
 704}
 705
 706static ssize_t therm_min_store(struct device *dev,
 707			       struct device_attribute *devattr,
 708			       const char *buf, size_t count)
 
 
 
 
 709{
 710	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 711	struct pc87360_data *data = dev_get_drvdata(dev);
 712	long val;
 713	int err;
 714
 715	err = kstrtol(buf, 10, &val);
 716	if (err)
 717		return err;
 718
 719	mutex_lock(&data->update_lock);
 720	data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
 721	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
 722			    data->in_min[attr->index]);
 723	mutex_unlock(&data->update_lock);
 724	return count;
 725}
 726
 727static struct sensor_device_attribute therm_min[] = {
 728	SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11),
 729	SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11),
 730	SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11),
 731};
 732
 733static ssize_t therm_max_show(struct device *dev,
 734			      struct device_attribute *devattr, char *buf)
 735{
 736	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 737	struct pc87360_data *data = pc87360_update_device(dev);
 738	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
 739		       data->in_vref));
 740}
 741
 742static ssize_t therm_max_store(struct device *dev,
 743			       struct device_attribute *devattr,
 744			       const char *buf, size_t count)
 745{
 746	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 747	struct pc87360_data *data = dev_get_drvdata(dev);
 748	long val;
 749	int err;
 750
 751	err = kstrtol(buf, 10, &val);
 752	if (err)
 753		return err;
 754
 755	mutex_lock(&data->update_lock);
 756	data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
 757	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
 758			    data->in_max[attr->index]);
 759	mutex_unlock(&data->update_lock);
 760	return count;
 761}
 762
 763static struct sensor_device_attribute therm_max[] = {
 764	SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11),
 765	SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11),
 766	SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11),
 767};
 768
 769static ssize_t therm_crit_show(struct device *dev,
 770			       struct device_attribute *devattr, char *buf)
 771{
 772	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 773	struct pc87360_data *data = pc87360_update_device(dev);
 774	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
 775		       data->in_vref));
 776}
 777
 778static ssize_t therm_crit_store(struct device *dev,
 779				struct device_attribute *devattr,
 780				const char *buf, size_t count)
 781{
 782	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 783	struct pc87360_data *data = dev_get_drvdata(dev);
 784	long val;
 785	int err;
 786
 787	err = kstrtol(buf, 10, &val);
 788	if (err)
 789		return err;
 790
 791	mutex_lock(&data->update_lock);
 792	data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
 793	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
 794			    data->in_crit[attr->index-11]);
 795	mutex_unlock(&data->update_lock);
 796	return count;
 797}
 798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 799static struct sensor_device_attribute therm_crit[] = {
 800	SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11),
 801	SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11),
 802	SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11),
 
 
 
 803};
 804
 805/*
 806 * show_therm_min/max_alarm() reads data from the per-channel voltage
 807 * status register (sec 11.5.12)
 808 */
 809static ssize_t therm_min_alarm_show(struct device *dev,
 810				    struct device_attribute *devattr,
 811				    char *buf)
 812{
 813	struct pc87360_data *data = pc87360_update_device(dev);
 814	unsigned nr = to_sensor_dev_attr(devattr)->index;
 815
 816	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
 817}
 818
 819static struct sensor_device_attribute therm_min_alarm[] = {
 820	SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11),
 821	SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11),
 822	SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11),
 823};
 824
 825static ssize_t therm_max_alarm_show(struct device *dev,
 826				    struct device_attribute *devattr,
 827				    char *buf)
 828{
 829	struct pc87360_data *data = pc87360_update_device(dev);
 830	unsigned nr = to_sensor_dev_attr(devattr)->index;
 831
 832	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
 833}
 834
 835static struct sensor_device_attribute therm_max_alarm[] = {
 836	SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11),
 837	SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11),
 838	SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11),
 839};
 840
 841static ssize_t therm_crit_alarm_show(struct device *dev,
 842				     struct device_attribute *devattr,
 843				     char *buf)
 844{
 845	struct pc87360_data *data = pc87360_update_device(dev);
 846	unsigned nr = to_sensor_dev_attr(devattr)->index;
 847
 848	return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
 849}
 850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 851static struct sensor_device_attribute therm_crit_alarm[] = {
 852	SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11),
 853	SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11),
 854	SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11),
 
 
 
 855};
 856
 857#define THERM_UNIT_ATTRS(X) \
 858	&therm_input[X].dev_attr.attr,	\
 859	&therm_status[X].dev_attr.attr,	\
 860	&therm_min[X].dev_attr.attr,	\
 861	&therm_max[X].dev_attr.attr,	\
 862	&therm_crit[X].dev_attr.attr,	\
 863	&therm_min_alarm[X].dev_attr.attr, \
 864	&therm_max_alarm[X].dev_attr.attr, \
 865	&therm_crit_alarm[X].dev_attr.attr
 866
 867static struct attribute *pc8736x_therm_attr_array[] = {
 868	THERM_UNIT_ATTRS(0),
 869	THERM_UNIT_ATTRS(1),
 870	THERM_UNIT_ATTRS(2),
 871	NULL
 872};
 873static const struct attribute_group pc8736x_therm_group = {
 874	.attrs = pc8736x_therm_attr_array,
 875};
 876
 877static ssize_t temp_input_show(struct device *dev,
 878			       struct device_attribute *devattr, char *buf)
 879{
 880	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 881	struct pc87360_data *data = pc87360_update_device(dev);
 882	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
 883}
 884
 885static struct sensor_device_attribute temp_input[] = {
 886	SENSOR_ATTR_RO(temp1_input, temp_input, 0),
 887	SENSOR_ATTR_RO(temp2_input, temp_input, 1),
 888	SENSOR_ATTR_RO(temp3_input, temp_input, 2),
 889};
 890
 891static ssize_t temp_status_show(struct device *dev,
 892				struct device_attribute *devattr, char *buf)
 
 
 
 
 893{
 894	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 895	struct pc87360_data *data = pc87360_update_device(dev);
 896	return sprintf(buf, "%d\n", data->temp_status[attr->index]);
 897}
 898
 899static struct sensor_device_attribute temp_status[] = {
 900	SENSOR_ATTR_RO(temp1_status, temp_status, 0),
 901	SENSOR_ATTR_RO(temp2_status, temp_status, 1),
 902	SENSOR_ATTR_RO(temp3_status, temp_status, 2),
 903};
 904
 905static ssize_t temp_min_show(struct device *dev,
 906			     struct device_attribute *devattr, char *buf)
 907{
 908	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 909	struct pc87360_data *data = pc87360_update_device(dev);
 910	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
 911}
 912
 913static ssize_t temp_min_store(struct device *dev,
 914			      struct device_attribute *devattr,
 915			      const char *buf, size_t count)
 916{
 917	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 918	struct pc87360_data *data = dev_get_drvdata(dev);
 919	long val;
 920	int err;
 921
 922	err = kstrtol(buf, 10, &val);
 923	if (err)
 924		return err;
 925
 926	mutex_lock(&data->update_lock);
 927	data->temp_min[attr->index] = TEMP_TO_REG(val);
 928	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
 929			    data->temp_min[attr->index]);
 930	mutex_unlock(&data->update_lock);
 931	return count;
 932}
 933
 934static struct sensor_device_attribute temp_min[] = {
 935	SENSOR_ATTR_RW(temp1_min, temp_min, 0),
 936	SENSOR_ATTR_RW(temp2_min, temp_min, 1),
 937	SENSOR_ATTR_RW(temp3_min, temp_min, 2),
 938};
 939
 940static ssize_t temp_max_show(struct device *dev,
 941			     struct device_attribute *devattr, char *buf)
 942{
 943	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 944	struct pc87360_data *data = pc87360_update_device(dev);
 945	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
 946}
 947
 948static ssize_t temp_max_store(struct device *dev,
 949			      struct device_attribute *devattr,
 950			      const char *buf, size_t count)
 951{
 952	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 953	struct pc87360_data *data = dev_get_drvdata(dev);
 954	long val;
 955	int err;
 956
 957	err = kstrtol(buf, 10, &val);
 958	if (err)
 959		return err;
 960
 961	mutex_lock(&data->update_lock);
 962	data->temp_max[attr->index] = TEMP_TO_REG(val);
 963	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
 964			    data->temp_max[attr->index]);
 965	mutex_unlock(&data->update_lock);
 966	return count;
 967}
 968
 969static struct sensor_device_attribute temp_max[] = {
 970	SENSOR_ATTR_RW(temp1_max, temp_max, 0),
 971	SENSOR_ATTR_RW(temp2_max, temp_max, 1),
 972	SENSOR_ATTR_RW(temp3_max, temp_max, 2),
 973};
 974
 975static ssize_t temp_crit_show(struct device *dev,
 976			      struct device_attribute *devattr, char *buf)
 977{
 978	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 979	struct pc87360_data *data = pc87360_update_device(dev);
 980	return sprintf(buf, "%d\n",
 981		       TEMP_FROM_REG(data->temp_crit[attr->index]));
 982}
 983
 984static ssize_t temp_crit_store(struct device *dev,
 985			       struct device_attribute *devattr,
 986			       const char *buf, size_t count)
 987{
 988	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 989	struct pc87360_data *data = dev_get_drvdata(dev);
 990	long val;
 991	int err;
 992
 993	err = kstrtol(buf, 10, &val);
 994	if (err)
 995		return err;
 996
 997	mutex_lock(&data->update_lock);
 998	data->temp_crit[attr->index] = TEMP_TO_REG(val);
 999	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
1000			    data->temp_crit[attr->index]);
1001	mutex_unlock(&data->update_lock);
1002	return count;
1003}
1004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1005static struct sensor_device_attribute temp_crit[] = {
1006	SENSOR_ATTR_RW(temp1_crit, temp_crit, 0),
1007	SENSOR_ATTR_RW(temp2_crit, temp_crit, 1),
1008	SENSOR_ATTR_RW(temp3_crit, temp_crit, 2),
 
 
 
1009};
1010
1011/*
1012 * temp_min/max_alarm_show() reads data from the per-channel status
1013 * register (sec 12.3.7), not the temp event status registers (sec
1014 * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1015 */
1016static ssize_t temp_min_alarm_show(struct device *dev,
1017				   struct device_attribute *devattr,
1018				   char *buf)
 
 
 
 
 
1019{
1020	struct pc87360_data *data = pc87360_update_device(dev);
1021	unsigned nr = to_sensor_dev_attr(devattr)->index;
1022
1023	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1024}
1025
1026static struct sensor_device_attribute temp_min_alarm[] = {
1027	SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0),
1028	SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1),
1029	SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2),
1030};
1031
1032static ssize_t temp_max_alarm_show(struct device *dev,
1033				   struct device_attribute *devattr,
1034				   char *buf)
1035{
1036	struct pc87360_data *data = pc87360_update_device(dev);
1037	unsigned nr = to_sensor_dev_attr(devattr)->index;
1038
1039	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1040}
1041
1042static struct sensor_device_attribute temp_max_alarm[] = {
1043	SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0),
1044	SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1),
1045	SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2),
1046};
1047
1048static ssize_t temp_crit_alarm_show(struct device *dev,
1049				    struct device_attribute *devattr,
1050				    char *buf)
1051{
1052	struct pc87360_data *data = pc87360_update_device(dev);
1053	unsigned nr = to_sensor_dev_attr(devattr)->index;
1054
1055	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1056}
1057
 
 
 
 
 
 
 
 
 
 
1058static struct sensor_device_attribute temp_crit_alarm[] = {
1059	SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0),
1060	SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1),
1061	SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2),
1062};
1063
1064#define TEMP_FAULT	0x40	/* open diode */
1065static ssize_t temp_fault_show(struct device *dev,
1066			       struct device_attribute *devattr, char *buf)
1067{
1068	struct pc87360_data *data = pc87360_update_device(dev);
1069	unsigned nr = to_sensor_dev_attr(devattr)->index;
1070
1071	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1072}
1073
1074static struct sensor_device_attribute temp_fault[] = {
1075	SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
1076	SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
1077	SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
1078};
1079
1080#define TEMP_UNIT_ATTRS(X)			\
1081{	&temp_input[X].dev_attr.attr,		\
1082	&temp_status[X].dev_attr.attr,		\
1083	&temp_min[X].dev_attr.attr,		\
1084	&temp_max[X].dev_attr.attr,		\
1085	&temp_crit[X].dev_attr.attr,		\
1086	&temp_min_alarm[X].dev_attr.attr,	\
1087	&temp_max_alarm[X].dev_attr.attr,	\
1088	&temp_crit_alarm[X].dev_attr.attr,	\
1089	&temp_fault[X].dev_attr.attr,		\
1090	NULL					\
1091}
1092
1093static struct attribute *pc8736x_temp_attr[][10] = {
1094	TEMP_UNIT_ATTRS(0),
1095	TEMP_UNIT_ATTRS(1),
1096	TEMP_UNIT_ATTRS(2)
 
 
 
1097};
1098
1099static const struct attribute_group pc8736x_temp_attr_group[] = {
1100	{ .attrs = pc8736x_temp_attr[0] },
1101	{ .attrs = pc8736x_temp_attr[1] },
1102	{ .attrs = pc8736x_temp_attr[2] }
1103};
1104
1105static ssize_t alarms_temp_show(struct device *dev,
1106				struct device_attribute *attr, char *buf)
1107{
1108	struct pc87360_data *data = pc87360_update_device(dev);
1109	return sprintf(buf, "%u\n", data->temp_alarms);
1110}
 
1111
1112static DEVICE_ATTR_RO(alarms_temp);
 
 
1113
1114static ssize_t fan_input_show(struct device *dev,
1115			      struct device_attribute *devattr, char *buf)
1116{
1117	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1118	struct pc87360_data *data = pc87360_update_device(dev);
1119	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
1120		       FAN_DIV_FROM_REG(data->fan_status[attr->index])));
1121}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1122
1123static struct sensor_device_attribute fan_input[] = {
1124	SENSOR_ATTR_RO(fan1_input, fan_input, 0),
1125	SENSOR_ATTR_RO(fan2_input, fan_input, 1),
1126	SENSOR_ATTR_RO(fan3_input, fan_input, 2),
1127};
 
 
1128
1129static ssize_t fan_status_show(struct device *dev,
1130			       struct device_attribute *devattr, char *buf)
1131{
1132	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1133	struct pc87360_data *data = pc87360_update_device(dev);
1134	return sprintf(buf, "%u\n",
1135		       FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
1136}
1137
1138static struct sensor_device_attribute fan_status[] = {
1139	SENSOR_ATTR_RO(fan1_status, fan_status, 0),
1140	SENSOR_ATTR_RO(fan2_status, fan_status, 1),
1141	SENSOR_ATTR_RO(fan3_status, fan_status, 2),
1142};
1143
1144static ssize_t fan_div_show(struct device *dev,
1145			    struct device_attribute *devattr, char *buf)
1146{
1147	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1148	struct pc87360_data *data = pc87360_update_device(dev);
1149	return sprintf(buf, "%u\n",
1150		       FAN_DIV_FROM_REG(data->fan_status[attr->index]));
1151}
 
 
 
 
 
 
 
 
 
1152
1153static struct sensor_device_attribute fan_div[] = {
1154	SENSOR_ATTR_RO(fan1_div, fan_div, 0),
1155	SENSOR_ATTR_RO(fan2_div, fan_div, 1),
1156	SENSOR_ATTR_RO(fan3_div, fan_div, 2),
1157};
 
 
 
 
 
 
1158
1159static ssize_t fan_min_show(struct device *dev,
1160			    struct device_attribute *devattr, char *buf)
1161{
1162	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1163	struct pc87360_data *data = pc87360_update_device(dev);
1164	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
1165		       FAN_DIV_FROM_REG(data->fan_status[attr->index])));
1166}
1167
1168static ssize_t fan_min_store(struct device *dev,
1169			     struct device_attribute *devattr,
1170			     const char *buf, size_t count)
1171{
1172	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1173	struct pc87360_data *data = dev_get_drvdata(dev);
1174	long fan_min;
1175	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1176
1177	err = kstrtol(buf, 10, &fan_min);
1178	if (err)
1179		return err;
1180
1181	mutex_lock(&data->update_lock);
1182	fan_min = FAN_TO_REG(fan_min,
1183			     FAN_DIV_FROM_REG(data->fan_status[attr->index]));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1184
1185	/* If it wouldn't fit, change clock divisor */
1186	while (fan_min > 255
1187	    && (data->fan_status[attr->index] & 0x60) != 0x60) {
1188		fan_min >>= 1;
1189		data->fan[attr->index] >>= 1;
1190		data->fan_status[attr->index] += 0x20;
 
 
 
 
 
 
 
 
 
1191	}
1192	data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
1193	pc87360_write_value(data, LD_FAN, NO_BANK,
1194			    PC87360_REG_FAN_MIN(attr->index),
1195			    data->fan_min[attr->index]);
1196
1197	/* Write new divider, preserve alarm bits */
1198	pc87360_write_value(data, LD_FAN, NO_BANK,
1199			    PC87360_REG_FAN_STATUS(attr->index),
1200			    data->fan_status[attr->index] & 0xF9);
1201	mutex_unlock(&data->update_lock);
 
 
 
 
1202
1203	return count;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1204}
1205
1206static struct sensor_device_attribute fan_min[] = {
1207	SENSOR_ATTR_RW(fan1_min, fan_min, 0),
1208	SENSOR_ATTR_RW(fan2_min, fan_min, 1),
1209	SENSOR_ATTR_RW(fan3_min, fan_min, 2),
1210};
1211
1212#define FAN_UNIT_ATTRS(X)		\
1213{	&fan_input[X].dev_attr.attr,	\
1214	&fan_status[X].dev_attr.attr,	\
1215	&fan_div[X].dev_attr.attr,	\
1216	&fan_min[X].dev_attr.attr,	\
1217	NULL				\
1218}
1219
1220static struct attribute *pc8736x_fan_attr[][5] = {
1221	FAN_UNIT_ATTRS(0),
1222	FAN_UNIT_ATTRS(1),
1223	FAN_UNIT_ATTRS(2)
1224};
1225
1226static const struct attribute_group pc8736x_fan_attr_group[] = {
1227	{ .attrs = pc8736x_fan_attr[0], },
1228	{ .attrs = pc8736x_fan_attr[1], },
1229	{ .attrs = pc8736x_fan_attr[2], },
1230};
 
1231
1232static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
1233			char *buf)
1234{
1235	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1236	struct pc87360_data *data = pc87360_update_device(dev);
1237	return sprintf(buf, "%u\n",
1238		       PWM_FROM_REG(data->pwm[attr->index],
1239				    FAN_CONFIG_INVERT(data->fan_conf,
1240						      attr->index)));
1241}
1242
1243static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
1244			 const char *buf, size_t count)
 
 
1245{
1246	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1247	struct pc87360_data *data = dev_get_drvdata(dev);
1248	long val;
1249	int err;
1250
1251	err = kstrtol(buf, 10, &val);
1252	if (err)
1253		return err;
 
 
1254
1255	mutex_lock(&data->update_lock);
1256	data->pwm[attr->index] = PWM_TO_REG(val,
1257			      FAN_CONFIG_INVERT(data->fan_conf, attr->index));
1258	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
1259			    data->pwm[attr->index]);
1260	mutex_unlock(&data->update_lock);
1261	return count;
1262}
1263
1264static struct sensor_device_attribute pwm[] = {
1265	SENSOR_ATTR_RW(pwm1, pwm, 0),
1266	SENSOR_ATTR_RW(pwm2, pwm, 1),
1267	SENSOR_ATTR_RW(pwm3, pwm, 2),
1268};
1269
1270static ssize_t name_show(struct device *dev,
1271			struct device_attribute *devattr, char *buf)
1272{
1273	struct pc87360_data *data = dev_get_drvdata(dev);
1274	return sprintf(buf, "%s\n", data->name);
 
 
 
1275}
1276
1277static DEVICE_ATTR_RO(name);
 
 
 
 
1278
1279static void pc87360_remove_files(struct device *dev)
1280{
1281	int i;
1282
1283	device_remove_file(dev, &dev_attr_name);
1284	device_remove_file(dev, &dev_attr_alarms_temp);
1285	for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1286		sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1287	for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1288		sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1289		device_remove_file(dev, &pwm[i].dev_attr);
1290	}
1291	sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1292	sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1293}
1294
1295static void pc87360_init_device(struct platform_device *pdev,
1296				int use_thermistors)
1297{
1298	struct pc87360_data *data = platform_get_drvdata(pdev);
1299	int i, nr;
1300	const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1301	const u8 init_temp[3] = { 2, 2, 1 };
1302	u8 reg;
1303
1304	if (init >= 2 && data->innr) {
1305		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1306					 PC87365_REG_IN_CONVRATE);
1307		dev_info(&pdev->dev,
1308			 "VLM conversion set to 1s period, 160us delay\n");
1309		pc87360_write_value(data, LD_IN, NO_BANK,
1310				    PC87365_REG_IN_CONVRATE,
1311				    (reg & 0xC0) | 0x11);
1312	}
1313
1314	nr = data->innr < 11 ? data->innr : 11;
1315	for (i = 0; i < nr; i++) {
1316		reg = pc87360_read_value(data, LD_IN, i,
1317					 PC87365_REG_IN_STATUS);
1318		dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1319		if (init >= init_in[i]) {
1320			/* Forcibly enable voltage channel */
1321			if (!(reg & CHAN_ENA)) {
1322				dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1323					i);
1324				pc87360_write_value(data, LD_IN, i,
1325						    PC87365_REG_IN_STATUS,
1326						    (reg & 0x68) | 0x87);
1327			}
1328		}
1329	}
1330
1331	/*
1332	 * We can't blindly trust the Super-I/O space configuration bit,
1333	 * most BIOS won't set it properly
1334	 */
1335	dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1336	for (i = 11; i < data->innr; i++) {
1337		reg = pc87360_read_value(data, LD_IN, i,
1338					 PC87365_REG_TEMP_STATUS);
1339		use_thermistors = use_thermistors || (reg & CHAN_ENA);
1340		/* thermistors are temp[4-6], measured on vin[11-14] */
1341		dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1342	}
1343	dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1344
1345	i = use_thermistors ? 2 : 0;
1346	for (; i < data->tempnr; i++) {
1347		reg = pc87360_read_value(data, LD_TEMP, i,
1348					 PC87365_REG_TEMP_STATUS);
1349		dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1350		if (init >= init_temp[i]) {
1351			/* Forcibly enable temperature channel */
1352			if (!(reg & CHAN_ENA)) {
1353				dev_dbg(&pdev->dev,
1354					"Forcibly enabling temp%d\n", i + 1);
1355				pc87360_write_value(data, LD_TEMP, i,
1356						    PC87365_REG_TEMP_STATUS,
1357						    0xCF);
1358			}
1359		}
1360	}
1361
1362	if (use_thermistors) {
1363		for (i = 11; i < data->innr; i++) {
1364			if (init >= init_in[i]) {
1365				/*
1366				 * The pin may already be used by thermal
1367				 * diodes
1368				 */
1369				reg = pc87360_read_value(data, LD_TEMP,
1370				      (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1371				if (reg & CHAN_ENA) {
1372					dev_dbg(&pdev->dev,
1373			"Skipping temp%d, pin already in use by temp%d\n",
1374						i - 7, (i - 11) / 2);
1375					continue;
1376				}
1377
1378				/* Forcibly enable thermistor channel */
1379				reg = pc87360_read_value(data, LD_IN, i,
1380							 PC87365_REG_IN_STATUS);
1381				if (!(reg & CHAN_ENA)) {
1382					dev_dbg(&pdev->dev,
1383						"Forcibly enabling temp%d\n",
1384						i - 7);
1385					pc87360_write_value(data, LD_IN, i,
1386						PC87365_REG_TEMP_STATUS,
1387						(reg & 0x60) | 0x8F);
1388				}
1389			}
1390		}
1391	}
1392
1393	if (data->innr) {
1394		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1395					 PC87365_REG_IN_CONFIG);
1396		dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1397		if (reg & CHAN_ENA) {
1398			dev_dbg(&pdev->dev,
1399				"Forcibly enabling monitoring (VLM)\n");
1400			pc87360_write_value(data, LD_IN, NO_BANK,
1401					    PC87365_REG_IN_CONFIG,
1402					    reg & 0xFE);
1403		}
1404	}
1405
1406	if (data->tempnr) {
1407		reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1408					 PC87365_REG_TEMP_CONFIG);
1409		dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1410		if (reg & CHAN_ENA) {
1411			dev_dbg(&pdev->dev,
1412				"Forcibly enabling monitoring (TMS)\n");
1413			pc87360_write_value(data, LD_TEMP, NO_BANK,
1414					    PC87365_REG_TEMP_CONFIG,
1415					    reg & 0xFE);
1416		}
1417
1418		if (init >= 2) {
1419			/* Chip config as documented by National Semi. */
1420			pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1421			/*
1422			 * We voluntarily omit the bank here, in case the
1423			 * sequence itself matters. It shouldn't be a problem,
1424			 * since nobody else is supposed to access the
1425			 * device at that point.
1426			 */
1427			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1428			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1429			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1430			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1431		}
1432	}
1433}
1434
1435static int pc87360_probe(struct platform_device *pdev)
1436{
1437	int i;
1438	struct pc87360_data *data;
1439	int err = 0;
1440	const char *name;
1441	int use_thermistors = 0;
1442	struct device *dev = &pdev->dev;
1443
1444	data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1445	if (!data)
1446		return -ENOMEM;
1447
1448	switch (devid) {
1449	default:
1450		name = "pc87360";
1451		data->fannr = 2;
1452		break;
1453	case 0xe8:
1454		name = "pc87363";
1455		data->fannr = 2;
1456		break;
1457	case 0xe4:
1458		name = "pc87364";
1459		data->fannr = 3;
1460		break;
1461	case 0xe5:
1462		name = "pc87365";
1463		data->fannr = extra_isa[0] ? 3 : 0;
1464		data->innr = extra_isa[1] ? 11 : 0;
1465		data->tempnr = extra_isa[2] ? 2 : 0;
1466		break;
1467	case 0xe9:
1468		name = "pc87366";
1469		data->fannr = extra_isa[0] ? 3 : 0;
1470		data->innr = extra_isa[1] ? 14 : 0;
1471		data->tempnr = extra_isa[2] ? 3 : 0;
1472		break;
1473	}
1474
1475	data->name = name;
1476	mutex_init(&data->lock);
1477	mutex_init(&data->update_lock);
1478	platform_set_drvdata(pdev, data);
1479
1480	for (i = 0; i < LDNI_MAX; i++) {
1481		data->address[i] = extra_isa[i];
1482		if (data->address[i]
1483		 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1484					 DRIVER_NAME)) {
1485			dev_err(dev,
1486				"Region 0x%x-0x%x already in use!\n",
1487				extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1488			return -EBUSY;
1489		}
1490	}
1491
1492	/* Retrieve the fans configuration from Super-I/O space */
1493	if (data->fannr)
1494		data->fan_conf = confreg[0] | (confreg[1] << 8);
1495
1496	/*
1497	 * Use the correct reference voltage
1498	 * Unless both the VLM and the TMS logical devices agree to
1499	 * use an external Vref, the internal one is used.
1500	 */
1501	if (data->innr) {
1502		i = pc87360_read_value(data, LD_IN, NO_BANK,
1503				       PC87365_REG_IN_CONFIG);
1504		if (data->tempnr) {
1505			i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1506						PC87365_REG_TEMP_CONFIG);
1507		}
1508		data->in_vref = (i&0x02) ? 3025 : 2966;
1509		dev_dbg(dev, "Using %s reference voltage\n",
1510			(i&0x02) ? "external" : "internal");
1511
1512		data->vid_conf = confreg[3];
1513		data->vrm = vid_which_vrm();
1514	}
1515
1516	/* Fan clock dividers may be needed before any data is read */
1517	for (i = 0; i < data->fannr; i++) {
1518		if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1519			data->fan_status[i] = pc87360_read_value(data,
1520					      LD_FAN, NO_BANK,
1521					      PC87360_REG_FAN_STATUS(i));
1522	}
 
1523
1524	if (init > 0) {
1525		if (devid == 0xe9 && data->address[1]) /* PC87366 */
1526			use_thermistors = confreg[2] & 0x40;
 
1527
1528		pc87360_init_device(pdev, use_thermistors);
1529	}
1530
1531	/* Register all-or-nothing sysfs groups */
 
1532
1533	if (data->innr) {
1534		err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1535		if (err)
1536			goto error;
1537	}
1538
1539	if (data->innr == 14) {
1540		err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1541		if (err)
1542			goto error;
1543	}
1544
1545	/* create device attr-files for varying sysfs groups */
1546
1547	if (data->tempnr) {
1548		for (i = 0; i < data->tempnr; i++) {
1549			err = sysfs_create_group(&dev->kobj,
1550						 &pc8736x_temp_attr_group[i]);
1551			if (err)
1552				goto error;
 
1553		}
1554		err = device_create_file(dev, &dev_attr_alarms_temp);
1555		if (err)
1556			goto error;
1557	}
1558
1559	for (i = 0; i < data->fannr; i++) {
1560		if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1561			err = sysfs_create_group(&dev->kobj,
1562						 &pc8736x_fan_attr_group[i]);
1563			if (err)
1564				goto error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1565		}
1566		if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1567			err = device_create_file(dev, &pwm[i].dev_attr);
1568			if (err)
1569				goto error;
 
 
 
 
 
1570		}
1571	}
1572
1573	err = device_create_file(dev, &dev_attr_name);
1574	if (err)
1575		goto error;
1576
1577	data->hwmon_dev = hwmon_device_register(dev);
1578	if (IS_ERR(data->hwmon_dev)) {
1579		err = PTR_ERR(data->hwmon_dev);
1580		goto error;
1581	}
1582	return 0;
1583
1584error:
1585	pc87360_remove_files(dev);
1586	return err;
1587}
1588
1589static int pc87360_remove(struct platform_device *pdev)
1590{
1591	struct pc87360_data *data = platform_get_drvdata(pdev);
1592
1593	hwmon_device_unregister(data->hwmon_dev);
1594	pc87360_remove_files(&pdev->dev);
1595
1596	return 0;
1597}
1598
1599/*
1600 * Driver data
1601 */
1602static struct platform_driver pc87360_driver = {
1603	.driver = {
1604		.name	= DRIVER_NAME,
1605	},
1606	.probe		= pc87360_probe,
1607	.remove		= pc87360_remove,
1608};
1609
1610/*
1611 * Device detection, registration and update
1612 */
1613
1614static int __init pc87360_find(int sioaddr, u8 *devid,
1615			       unsigned short *addresses)
1616{
1617	u16 val;
1618	int i;
1619	int nrdev; /* logical device count */
1620
1621	/* No superio_enter */
1622
1623	/* Identify device */
1624	val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1625	switch (val) {
1626	case 0xE1: /* PC87360 */
1627	case 0xE8: /* PC87363 */
1628	case 0xE4: /* PC87364 */
1629		nrdev = 1;
1630		break;
1631	case 0xE5: /* PC87365 */
1632	case 0xE9: /* PC87366 */
1633		nrdev = 3;
1634		break;
1635	default:
1636		superio_exit(sioaddr);
1637		return -ENODEV;
1638	}
1639	/* Remember the device id */
1640	*devid = val;
1641
1642	for (i = 0; i < nrdev; i++) {
1643		/* select logical device */
1644		superio_outb(sioaddr, DEV, logdev[i]);
1645
1646		val = superio_inb(sioaddr, ACT);
1647		if (!(val & 0x01)) {
1648			pr_info("Device 0x%02x not activated\n", logdev[i]);
1649			continue;
1650		}
1651
1652		val = (superio_inb(sioaddr, BASE) << 8)
1653		    | superio_inb(sioaddr, BASE + 1);
1654		if (!val) {
1655			pr_info("Base address not set for device 0x%02x\n",
1656				logdev[i]);
1657			continue;
1658		}
1659
1660		addresses[i] = val;
 
 
1661
1662		if (i == 0) { /* Fans */
1663			confreg[0] = superio_inb(sioaddr, 0xF0);
1664			confreg[1] = superio_inb(sioaddr, 0xF1);
1665
1666			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1667				 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1668				 (confreg[0] >> 4) & 1);
1669			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1670				 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1671				 (confreg[0] >> 7) & 1);
1672			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1673				 confreg[1] & 1, (confreg[1] >> 1) & 1,
1674				 (confreg[1] >> 2) & 1);
1675		} else if (i == 1) { /* Voltages */
1676			/* Are we using thermistors? */
1677			if (*devid == 0xE9) { /* PC87366 */
1678				/*
1679				 * These registers are not logical-device
1680				 * specific, just that we won't need them if
1681				 * we don't use the VLM device
1682				 */
1683				confreg[2] = superio_inb(sioaddr, 0x2B);
1684				confreg[3] = superio_inb(sioaddr, 0x25);
1685
1686				if (confreg[2] & 0x40) {
1687					pr_info("Using thermistors for temperature monitoring\n");
1688				}
1689				if (confreg[3] & 0xE0) {
1690					pr_info("VID inputs routed (mode %u)\n",
1691						confreg[3] >> 5);
1692				}
1693			}
1694		}
1695	}
1696
1697	superio_exit(sioaddr);
1698	return 0;
1699}
1700
1701static int __init pc87360_device_add(unsigned short address)
1702{
1703	struct resource res[3];
1704	int err, i, res_count;
1705
1706	pdev = platform_device_alloc("pc87360", address);
1707	if (!pdev) {
1708		err = -ENOMEM;
1709		pr_err("Device allocation failed\n");
1710		goto exit;
1711	}
1712
1713	memset(res, 0, 3 * sizeof(struct resource));
1714	res_count = 0;
1715	for (i = 0; i < 3; i++) {
1716		if (!extra_isa[i])
1717			continue;
1718		res[res_count].start = extra_isa[i];
1719		res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1720		res[res_count].name = "pc87360";
1721		res[res_count].flags = IORESOURCE_IO;
1722
1723		err = acpi_check_resource_conflict(&res[res_count]);
1724		if (err)
1725			goto exit_device_put;
1726
1727		res_count++;
1728	}
1729
1730	err = platform_device_add_resources(pdev, res, res_count);
1731	if (err) {
1732		pr_err("Device resources addition failed (%d)\n", err);
1733		goto exit_device_put;
1734	}
1735
1736	err = platform_device_add(pdev);
1737	if (err) {
1738		pr_err("Device addition failed (%d)\n", err);
1739		goto exit_device_put;
1740	}
1741
1742	return 0;
1743
1744exit_device_put:
1745	platform_device_put(pdev);
1746exit:
1747	return err;
1748}
1749
1750static int __init pc87360_init(void)
1751{
1752	int err, i;
1753	unsigned short address = 0;
1754
1755	if (pc87360_find(0x2e, &devid, extra_isa)
1756	 && pc87360_find(0x4e, &devid, extra_isa)) {
1757		pr_warn("PC8736x not detected, module not inserted\n");
1758		return -ENODEV;
1759	}
1760
1761	/* Arbitrarily pick one of the addresses */
1762	for (i = 0; i < 3; i++) {
1763		if (extra_isa[i] != 0x0000) {
1764			address = extra_isa[i];
1765			break;
1766		}
1767	}
1768
1769	if (address == 0x0000) {
1770		pr_warn("No active logical device, module not inserted\n");
1771		return -ENODEV;
1772	}
1773
1774	err = platform_driver_register(&pc87360_driver);
1775	if (err)
1776		goto exit;
1777
1778	/* Sets global pdev as a side effect */
1779	err = pc87360_device_add(address);
1780	if (err)
1781		goto exit_driver;
1782
1783	return 0;
1784
1785 exit_driver:
1786	platform_driver_unregister(&pc87360_driver);
1787 exit:
1788	return err;
1789}
1790
1791static void __exit pc87360_exit(void)
1792{
1793	platform_device_unregister(pdev);
1794	platform_driver_unregister(&pc87360_driver);
1795}
1796
1797MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
 
1798MODULE_DESCRIPTION("PC8736x hardware monitor");
1799MODULE_LICENSE("GPL");
1800MODULE_ALIAS("platform:" DRIVER_NAME);
1801
1802module_init(pc87360_init);
1803module_exit(pc87360_exit);