Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
   4 *	      monitoring
   5 *
   6 * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
   7 *
   8 * (derived from w83781d.c)
   9 *
  10 * Copyright (C) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
  11 *			      Philip Edelbrock <phil@netroedge.com>, and
  12 *			      Mark Studebaker <mdsxyz123@yahoo.com>
  13 */
  14
  15/*
  16 * This driver supports the hardware sensor chips: Asus ASB100 and
  17 * ASB100-A "BACH".
  18 *
  19 * ASB100-A supports pwm1, while plain ASB100 does not.  There is no known
  20 * way for the driver to tell which one is there.
  21 *
  22 * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
  23 * asb100	7	3	1	4	0x31	0x0694	yes	no
  24 */
  25
  26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27
  28#include <linux/module.h>
  29#include <linux/slab.h>
  30#include <linux/i2c.h>
  31#include <linux/hwmon.h>
  32#include <linux/hwmon-sysfs.h>
  33#include <linux/hwmon-vid.h>
  34#include <linux/err.h>
  35#include <linux/init.h>
  36#include <linux/jiffies.h>
  37#include <linux/mutex.h>
  38#include "lm75.h"
  39
  40/* I2C addresses to scan */
  41static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
  42
  43static unsigned short force_subclients[4];
  44module_param_array(force_subclients, short, NULL, 0);
  45MODULE_PARM_DESC(force_subclients,
  46	"List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
  47
  48/* Voltage IN registers 0-6 */
  49#define ASB100_REG_IN(nr)	(0x20 + (nr))
  50#define ASB100_REG_IN_MAX(nr)	(0x2b + (nr * 2))
  51#define ASB100_REG_IN_MIN(nr)	(0x2c + (nr * 2))
  52
  53/* FAN IN registers 1-3 */
  54#define ASB100_REG_FAN(nr)	(0x28 + (nr))
  55#define ASB100_REG_FAN_MIN(nr)	(0x3b + (nr))
  56
  57/* TEMPERATURE registers 1-4 */
  58static const u16 asb100_reg_temp[]	= {0, 0x27, 0x150, 0x250, 0x17};
  59static const u16 asb100_reg_temp_max[]	= {0, 0x39, 0x155, 0x255, 0x18};
  60static const u16 asb100_reg_temp_hyst[]	= {0, 0x3a, 0x153, 0x253, 0x19};
  61
  62#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
  63#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
  64#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
  65
  66#define ASB100_REG_TEMP2_CONFIG	0x0152
  67#define ASB100_REG_TEMP3_CONFIG	0x0252
  68
  69
  70#define ASB100_REG_CONFIG	0x40
  71#define ASB100_REG_ALARM1	0x41
  72#define ASB100_REG_ALARM2	0x42
  73#define ASB100_REG_SMIM1	0x43
  74#define ASB100_REG_SMIM2	0x44
  75#define ASB100_REG_VID_FANDIV	0x47
  76#define ASB100_REG_I2C_ADDR	0x48
  77#define ASB100_REG_CHIPID	0x49
  78#define ASB100_REG_I2C_SUBADDR	0x4a
  79#define ASB100_REG_PIN		0x4b
  80#define ASB100_REG_IRQ		0x4c
  81#define ASB100_REG_BANK		0x4e
  82#define ASB100_REG_CHIPMAN	0x4f
  83
  84#define ASB100_REG_WCHIPID	0x58
  85
  86/* bit 7 -> enable, bits 0-3 -> duty cycle */
  87#define ASB100_REG_PWM1		0x59
  88
  89/*
  90 * CONVERSIONS
  91 * Rounding and limit checking is only done on the TO_REG variants.
  92 */
  93
  94/* These constants are a guess, consistent w/ w83781d */
  95#define ASB100_IN_MIN		0
  96#define ASB100_IN_MAX		4080
  97
  98/*
  99 * IN: 1/1000 V (0V to 4.08V)
 100 * REG: 16mV/bit
 101 */
 102static u8 IN_TO_REG(unsigned val)
 103{
 104	unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
 105	return (nval + 8) / 16;
 106}
 107
 108static unsigned IN_FROM_REG(u8 reg)
 109{
 110	return reg * 16;
 111}
 112
 113static u8 FAN_TO_REG(long rpm, int div)
 114{
 115	if (rpm == -1)
 116		return 0;
 117	if (rpm == 0)
 118		return 255;
 119	rpm = clamp_val(rpm, 1, 1000000);
 120	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 121}
 122
 123static int FAN_FROM_REG(u8 val, int div)
 124{
 125	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
 126}
 127
 128/* These constants are a guess, consistent w/ w83781d */
 129#define ASB100_TEMP_MIN		-128000
 130#define ASB100_TEMP_MAX		127000
 131
 132/*
 133 * TEMP: 0.001C/bit (-128C to +127C)
 134 * REG: 1C/bit, two's complement
 135 */
 136static u8 TEMP_TO_REG(long temp)
 137{
 138	int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
 139	ntemp += (ntemp < 0 ? -500 : 500);
 140	return (u8)(ntemp / 1000);
 141}
 142
 143static int TEMP_FROM_REG(u8 reg)
 144{
 145	return (s8)reg * 1000;
 146}
 147
 148/*
 149 * PWM: 0 - 255 per sensors documentation
 150 * REG: (6.25% duty cycle per bit)
 151 */
 152static u8 ASB100_PWM_TO_REG(int pwm)
 153{
 154	pwm = clamp_val(pwm, 0, 255);
 155	return (u8)(pwm / 16);
 156}
 157
 158static int ASB100_PWM_FROM_REG(u8 reg)
 159{
 160	return reg * 16;
 161}
 162
 163#define DIV_FROM_REG(val) (1 << (val))
 164
 165/*
 166 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
 167 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
 168 */
 169static u8 DIV_TO_REG(long val)
 170{
 171	return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
 172}
 173
 174/*
 175 * For each registered client, we need to keep some data in memory. That
 176 * data is pointed to by client->data. The structure itself is
 177 * dynamically allocated, at the same time the client itself is allocated.
 178 */
 179struct asb100_data {
 180	struct device *hwmon_dev;
 181	struct mutex lock;
 182
 183	struct mutex update_lock;
 184	unsigned long last_updated;	/* In jiffies */
 185
 186	/* array of 2 pointers to subclients */
 187	struct i2c_client *lm75[2];
 188
 189	char valid;		/* !=0 if following fields are valid */
 190	u8 in[7];		/* Register value */
 191	u8 in_max[7];		/* Register value */
 192	u8 in_min[7];		/* Register value */
 193	u8 fan[3];		/* Register value */
 194	u8 fan_min[3];		/* Register value */
 195	u16 temp[4];		/* Register value (0 and 3 are u8 only) */
 196	u16 temp_max[4];	/* Register value (0 and 3 are u8 only) */
 197	u16 temp_hyst[4];	/* Register value (0 and 3 are u8 only) */
 198	u8 fan_div[3];		/* Register encoding, right justified */
 199	u8 pwm;			/* Register encoding */
 200	u8 vid;			/* Register encoding, combined */
 201	u32 alarms;		/* Register encoding, combined */
 202	u8 vrm;
 203};
 204
 205static int asb100_read_value(struct i2c_client *client, u16 reg);
 206static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
 207
 208static int asb100_probe(struct i2c_client *client,
 209			const struct i2c_device_id *id);
 210static int asb100_detect(struct i2c_client *client,
 211			 struct i2c_board_info *info);
 212static int asb100_remove(struct i2c_client *client);
 213static struct asb100_data *asb100_update_device(struct device *dev);
 214static void asb100_init_client(struct i2c_client *client);
 215
 216static const struct i2c_device_id asb100_id[] = {
 217	{ "asb100", 0 },
 218	{ }
 219};
 220MODULE_DEVICE_TABLE(i2c, asb100_id);
 221
 222static struct i2c_driver asb100_driver = {
 223	.class		= I2C_CLASS_HWMON,
 224	.driver = {
 225		.name	= "asb100",
 226	},
 227	.probe		= asb100_probe,
 228	.remove		= asb100_remove,
 229	.id_table	= asb100_id,
 230	.detect		= asb100_detect,
 231	.address_list	= normal_i2c,
 232};
 233
 234/* 7 Voltages */
 235#define show_in_reg(reg) \
 236static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
 237		char *buf) \
 238{ \
 239	int nr = to_sensor_dev_attr(attr)->index; \
 240	struct asb100_data *data = asb100_update_device(dev); \
 241	return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
 242}
 243
 244show_in_reg(in)
 245show_in_reg(in_min)
 246show_in_reg(in_max)
 247
 248#define set_in_reg(REG, reg) \
 249static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
 250		const char *buf, size_t count) \
 251{ \
 252	int nr = to_sensor_dev_attr(attr)->index; \
 253	struct i2c_client *client = to_i2c_client(dev); \
 254	struct asb100_data *data = i2c_get_clientdata(client); \
 255	unsigned long val; \
 256	int err = kstrtoul(buf, 10, &val); \
 257	if (err) \
 258		return err; \
 259	mutex_lock(&data->update_lock); \
 260	data->in_##reg[nr] = IN_TO_REG(val); \
 261	asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
 262		data->in_##reg[nr]); \
 263	mutex_unlock(&data->update_lock); \
 264	return count; \
 265}
 266
 267set_in_reg(MIN, min)
 268set_in_reg(MAX, max)
 269
 270#define sysfs_in(offset) \
 271static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
 272		show_in, NULL, offset); \
 273static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
 274		show_in_min, set_in_min, offset); \
 275static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
 276		show_in_max, set_in_max, offset)
 277
 278sysfs_in(0);
 279sysfs_in(1);
 280sysfs_in(2);
 281sysfs_in(3);
 282sysfs_in(4);
 283sysfs_in(5);
 284sysfs_in(6);
 285
 286/* 3 Fans */
 287static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 288		char *buf)
 289{
 290	int nr = to_sensor_dev_attr(attr)->index;
 291	struct asb100_data *data = asb100_update_device(dev);
 292	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
 293		DIV_FROM_REG(data->fan_div[nr])));
 294}
 295
 296static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 297		char *buf)
 298{
 299	int nr = to_sensor_dev_attr(attr)->index;
 300	struct asb100_data *data = asb100_update_device(dev);
 301	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
 302		DIV_FROM_REG(data->fan_div[nr])));
 303}
 304
 305static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
 306		char *buf)
 307{
 308	int nr = to_sensor_dev_attr(attr)->index;
 309	struct asb100_data *data = asb100_update_device(dev);
 310	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
 311}
 312
 313static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 314		const char *buf, size_t count)
 315{
 316	int nr = to_sensor_dev_attr(attr)->index;
 317	struct i2c_client *client = to_i2c_client(dev);
 318	struct asb100_data *data = i2c_get_clientdata(client);
 319	unsigned long val;
 320	int err;
 321
 322	err = kstrtoul(buf, 10, &val);
 323	if (err)
 324		return err;
 325
 326	mutex_lock(&data->update_lock);
 327	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 328	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
 329	mutex_unlock(&data->update_lock);
 330	return count;
 331}
 332
 333/*
 334 * Note: we save and restore the fan minimum here, because its value is
 335 * determined in part by the fan divisor.  This follows the principle of
 336 * least surprise; the user doesn't expect the fan minimum to change just
 337 * because the divisor changed.
 338 */
 339static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
 340		const char *buf, size_t count)
 341{
 342	int nr = to_sensor_dev_attr(attr)->index;
 343	struct i2c_client *client = to_i2c_client(dev);
 344	struct asb100_data *data = i2c_get_clientdata(client);
 345	unsigned long min;
 346	int reg;
 347	unsigned long val;
 348	int err;
 349
 350	err = kstrtoul(buf, 10, &val);
 351	if (err)
 352		return err;
 353
 354	mutex_lock(&data->update_lock);
 355
 356	min = FAN_FROM_REG(data->fan_min[nr],
 357			DIV_FROM_REG(data->fan_div[nr]));
 358	data->fan_div[nr] = DIV_TO_REG(val);
 359
 360	switch (nr) {
 361	case 0:	/* fan 1 */
 362		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
 363		reg = (reg & 0xcf) | (data->fan_div[0] << 4);
 364		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
 365		break;
 366
 367	case 1:	/* fan 2 */
 368		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
 369		reg = (reg & 0x3f) | (data->fan_div[1] << 6);
 370		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
 371		break;
 372
 373	case 2:	/* fan 3 */
 374		reg = asb100_read_value(client, ASB100_REG_PIN);
 375		reg = (reg & 0x3f) | (data->fan_div[2] << 6);
 376		asb100_write_value(client, ASB100_REG_PIN, reg);
 377		break;
 378	}
 379
 380	data->fan_min[nr] =
 381		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 382	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
 383
 384	mutex_unlock(&data->update_lock);
 385
 386	return count;
 387}
 388
 389#define sysfs_fan(offset) \
 390static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
 391		show_fan, NULL, offset - 1); \
 392static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
 393		show_fan_min, set_fan_min, offset - 1); \
 394static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
 395		show_fan_div, set_fan_div, offset - 1)
 396
 397sysfs_fan(1);
 398sysfs_fan(2);
 399sysfs_fan(3);
 400
 401/* 4 Temp. Sensors */
 402static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
 403{
 404	int ret = 0;
 405
 406	switch (nr) {
 407	case 1: case 2:
 408		ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
 409		break;
 410	case 0: case 3: default:
 411		ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
 412		break;
 413	}
 414	return ret;
 415}
 416
 417#define show_temp_reg(reg) \
 418static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
 419		char *buf) \
 420{ \
 421	int nr = to_sensor_dev_attr(attr)->index; \
 422	struct asb100_data *data = asb100_update_device(dev); \
 423	return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
 424}
 425
 426show_temp_reg(temp);
 427show_temp_reg(temp_max);
 428show_temp_reg(temp_hyst);
 429
 430#define set_temp_reg(REG, reg) \
 431static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
 432		const char *buf, size_t count) \
 433{ \
 434	int nr = to_sensor_dev_attr(attr)->index; \
 435	struct i2c_client *client = to_i2c_client(dev); \
 436	struct asb100_data *data = i2c_get_clientdata(client); \
 437	long val; \
 438	int err = kstrtol(buf, 10, &val); \
 439	if (err) \
 440		return err; \
 441	mutex_lock(&data->update_lock); \
 442	switch (nr) { \
 443	case 1: case 2: \
 444		data->reg[nr] = LM75_TEMP_TO_REG(val); \
 445		break; \
 446	case 0: case 3: default: \
 447		data->reg[nr] = TEMP_TO_REG(val); \
 448		break; \
 449	} \
 450	asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
 451			data->reg[nr]); \
 452	mutex_unlock(&data->update_lock); \
 453	return count; \
 454}
 455
 456set_temp_reg(MAX, temp_max);
 457set_temp_reg(HYST, temp_hyst);
 458
 459#define sysfs_temp(num) \
 460static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
 461		show_temp, NULL, num - 1); \
 462static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
 463		show_temp_max, set_temp_max, num - 1); \
 464static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
 465		show_temp_hyst, set_temp_hyst, num - 1)
 466
 467sysfs_temp(1);
 468sysfs_temp(2);
 469sysfs_temp(3);
 470sysfs_temp(4);
 471
 472/* VID */
 473static ssize_t cpu0_vid_show(struct device *dev,
 474			     struct device_attribute *attr, char *buf)
 475{
 476	struct asb100_data *data = asb100_update_device(dev);
 477	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
 478}
 479
 480static DEVICE_ATTR_RO(cpu0_vid);
 481
 482/* VRM */
 483static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
 484		char *buf)
 485{
 486	struct asb100_data *data = dev_get_drvdata(dev);
 487	return sprintf(buf, "%d\n", data->vrm);
 488}
 489
 490static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
 491			 const char *buf, size_t count)
 492{
 493	struct asb100_data *data = dev_get_drvdata(dev);
 494	unsigned long val;
 495	int err;
 496
 497	err = kstrtoul(buf, 10, &val);
 498	if (err)
 499		return err;
 500
 501	if (val > 255)
 502		return -EINVAL;
 503
 504	data->vrm = val;
 505	return count;
 506}
 507
 508/* Alarms */
 509static DEVICE_ATTR_RW(vrm);
 510
 511static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
 512		char *buf)
 513{
 514	struct asb100_data *data = asb100_update_device(dev);
 515	return sprintf(buf, "%u\n", data->alarms);
 516}
 517
 518static DEVICE_ATTR_RO(alarms);
 519
 520static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
 521		char *buf)
 522{
 523	int bitnr = to_sensor_dev_attr(attr)->index;
 524	struct asb100_data *data = asb100_update_device(dev);
 525	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
 526}
 527static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
 528static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
 529static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
 530static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
 531static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
 532static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
 533static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
 534static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
 535static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
 536static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
 537static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
 538
 539/* 1 PWM */
 540static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
 541		char *buf)
 542{
 543	struct asb100_data *data = asb100_update_device(dev);
 544	return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
 545}
 546
 547static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
 548			  const char *buf, size_t count)
 549{
 550	struct i2c_client *client = to_i2c_client(dev);
 551	struct asb100_data *data = i2c_get_clientdata(client);
 552	unsigned long val;
 553	int err;
 554
 555	err = kstrtoul(buf, 10, &val);
 556	if (err)
 557		return err;
 558
 559	mutex_lock(&data->update_lock);
 560	data->pwm &= 0x80; /* keep the enable bit */
 561	data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
 562	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
 563	mutex_unlock(&data->update_lock);
 564	return count;
 565}
 566
 567static ssize_t pwm1_enable_show(struct device *dev,
 568		struct device_attribute *attr, char *buf)
 569{
 570	struct asb100_data *data = asb100_update_device(dev);
 571	return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
 572}
 573
 574static ssize_t pwm1_enable_store(struct device *dev,
 575				 struct device_attribute *attr,
 576				 const char *buf, size_t count)
 577{
 578	struct i2c_client *client = to_i2c_client(dev);
 579	struct asb100_data *data = i2c_get_clientdata(client);
 580	unsigned long val;
 581	int err;
 582
 583	err = kstrtoul(buf, 10, &val);
 584	if (err)
 585		return err;
 586
 587	mutex_lock(&data->update_lock);
 588	data->pwm &= 0x0f; /* keep the duty cycle bits */
 589	data->pwm |= (val ? 0x80 : 0x00);
 590	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
 591	mutex_unlock(&data->update_lock);
 592	return count;
 593}
 594
 595static DEVICE_ATTR_RW(pwm1);
 596static DEVICE_ATTR_RW(pwm1_enable);
 597
 598static struct attribute *asb100_attributes[] = {
 599	&sensor_dev_attr_in0_input.dev_attr.attr,
 600	&sensor_dev_attr_in0_min.dev_attr.attr,
 601	&sensor_dev_attr_in0_max.dev_attr.attr,
 602	&sensor_dev_attr_in1_input.dev_attr.attr,
 603	&sensor_dev_attr_in1_min.dev_attr.attr,
 604	&sensor_dev_attr_in1_max.dev_attr.attr,
 605	&sensor_dev_attr_in2_input.dev_attr.attr,
 606	&sensor_dev_attr_in2_min.dev_attr.attr,
 607	&sensor_dev_attr_in2_max.dev_attr.attr,
 608	&sensor_dev_attr_in3_input.dev_attr.attr,
 609	&sensor_dev_attr_in3_min.dev_attr.attr,
 610	&sensor_dev_attr_in3_max.dev_attr.attr,
 611	&sensor_dev_attr_in4_input.dev_attr.attr,
 612	&sensor_dev_attr_in4_min.dev_attr.attr,
 613	&sensor_dev_attr_in4_max.dev_attr.attr,
 614	&sensor_dev_attr_in5_input.dev_attr.attr,
 615	&sensor_dev_attr_in5_min.dev_attr.attr,
 616	&sensor_dev_attr_in5_max.dev_attr.attr,
 617	&sensor_dev_attr_in6_input.dev_attr.attr,
 618	&sensor_dev_attr_in6_min.dev_attr.attr,
 619	&sensor_dev_attr_in6_max.dev_attr.attr,
 620
 621	&sensor_dev_attr_fan1_input.dev_attr.attr,
 622	&sensor_dev_attr_fan1_min.dev_attr.attr,
 623	&sensor_dev_attr_fan1_div.dev_attr.attr,
 624	&sensor_dev_attr_fan2_input.dev_attr.attr,
 625	&sensor_dev_attr_fan2_min.dev_attr.attr,
 626	&sensor_dev_attr_fan2_div.dev_attr.attr,
 627	&sensor_dev_attr_fan3_input.dev_attr.attr,
 628	&sensor_dev_attr_fan3_min.dev_attr.attr,
 629	&sensor_dev_attr_fan3_div.dev_attr.attr,
 630
 631	&sensor_dev_attr_temp1_input.dev_attr.attr,
 632	&sensor_dev_attr_temp1_max.dev_attr.attr,
 633	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
 634	&sensor_dev_attr_temp2_input.dev_attr.attr,
 635	&sensor_dev_attr_temp2_max.dev_attr.attr,
 636	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
 637	&sensor_dev_attr_temp3_input.dev_attr.attr,
 638	&sensor_dev_attr_temp3_max.dev_attr.attr,
 639	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
 640	&sensor_dev_attr_temp4_input.dev_attr.attr,
 641	&sensor_dev_attr_temp4_max.dev_attr.attr,
 642	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
 643
 644	&sensor_dev_attr_in0_alarm.dev_attr.attr,
 645	&sensor_dev_attr_in1_alarm.dev_attr.attr,
 646	&sensor_dev_attr_in2_alarm.dev_attr.attr,
 647	&sensor_dev_attr_in3_alarm.dev_attr.attr,
 648	&sensor_dev_attr_in4_alarm.dev_attr.attr,
 649	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
 650	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
 651	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
 652	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
 653	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
 654	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
 655
 656	&dev_attr_cpu0_vid.attr,
 657	&dev_attr_vrm.attr,
 658	&dev_attr_alarms.attr,
 659	&dev_attr_pwm1.attr,
 660	&dev_attr_pwm1_enable.attr,
 661
 662	NULL
 663};
 664
 665static const struct attribute_group asb100_group = {
 666	.attrs = asb100_attributes,
 667};
 668
 669static int asb100_detect_subclients(struct i2c_client *client)
 670{
 671	int i, id, err;
 672	int address = client->addr;
 673	unsigned short sc_addr[2];
 674	struct asb100_data *data = i2c_get_clientdata(client);
 675	struct i2c_adapter *adapter = client->adapter;
 676
 677	id = i2c_adapter_id(adapter);
 678
 679	if (force_subclients[0] == id && force_subclients[1] == address) {
 680		for (i = 2; i <= 3; i++) {
 681			if (force_subclients[i] < 0x48 ||
 682			    force_subclients[i] > 0x4f) {
 683				dev_err(&client->dev,
 684					"invalid subclient address %d; must be 0x48-0x4f\n",
 685					force_subclients[i]);
 686				err = -ENODEV;
 687				goto ERROR_SC_2;
 688			}
 689		}
 690		asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
 691					(force_subclients[2] & 0x07) |
 692					((force_subclients[3] & 0x07) << 4));
 693		sc_addr[0] = force_subclients[2];
 694		sc_addr[1] = force_subclients[3];
 695	} else {
 696		int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
 697		sc_addr[0] = 0x48 + (val & 0x07);
 698		sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
 699	}
 700
 701	if (sc_addr[0] == sc_addr[1]) {
 702		dev_err(&client->dev,
 703			"duplicate addresses 0x%x for subclients\n",
 704			sc_addr[0]);
 705		err = -ENODEV;
 706		goto ERROR_SC_2;
 707	}
 708
 709	data->lm75[0] = i2c_new_dummy_device(adapter, sc_addr[0]);
 710	if (IS_ERR(data->lm75[0])) {
 711		dev_err(&client->dev,
 712			"subclient %d registration at address 0x%x failed.\n",
 713			1, sc_addr[0]);
 714		err = PTR_ERR(data->lm75[0]);
 715		goto ERROR_SC_2;
 716	}
 717
 718	data->lm75[1] = i2c_new_dummy_device(adapter, sc_addr[1]);
 719	if (IS_ERR(data->lm75[1])) {
 720		dev_err(&client->dev,
 721			"subclient %d registration at address 0x%x failed.\n",
 722			2, sc_addr[1]);
 723		err = PTR_ERR(data->lm75[1]);
 724		goto ERROR_SC_3;
 725	}
 726
 727	return 0;
 728
 729/* Undo inits in case of errors */
 730ERROR_SC_3:
 731	i2c_unregister_device(data->lm75[0]);
 732ERROR_SC_2:
 733	return err;
 734}
 735
 736/* Return 0 if detection is successful, -ENODEV otherwise */
 737static int asb100_detect(struct i2c_client *client,
 738			 struct i2c_board_info *info)
 739{
 740	struct i2c_adapter *adapter = client->adapter;
 741	int val1, val2;
 742
 743	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 744		pr_debug("detect failed, smbus byte data not supported!\n");
 745		return -ENODEV;
 746	}
 747
 748	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
 749	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
 750
 751	/* If we're in bank 0 */
 752	if ((!(val1 & 0x07)) &&
 753			/* Check for ASB100 ID (low byte) */
 754			(((!(val1 & 0x80)) && (val2 != 0x94)) ||
 755			/* Check for ASB100 ID (high byte ) */
 756			((val1 & 0x80) && (val2 != 0x06)))) {
 757		pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
 758		return -ENODEV;
 759	}
 760
 761	/* Put it now into bank 0 and Vendor ID High Byte */
 762	i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
 763		(i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
 764		| 0x80);
 765
 766	/* Determine the chip type. */
 767	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
 768	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
 769
 770	if (val1 != 0x31 || val2 != 0x06)
 771		return -ENODEV;
 772
 773	strlcpy(info->type, "asb100", I2C_NAME_SIZE);
 774
 775	return 0;
 776}
 777
 778static int asb100_probe(struct i2c_client *client,
 779			const struct i2c_device_id *id)
 780{
 781	int err;
 782	struct asb100_data *data;
 783
 784	data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
 785			    GFP_KERNEL);
 786	if (!data)
 787		return -ENOMEM;
 788
 789	i2c_set_clientdata(client, data);
 790	mutex_init(&data->lock);
 791	mutex_init(&data->update_lock);
 792
 793	/* Attach secondary lm75 clients */
 794	err = asb100_detect_subclients(client);
 795	if (err)
 796		return err;
 797
 798	/* Initialize the chip */
 799	asb100_init_client(client);
 800
 801	/* A few vars need to be filled upon startup */
 802	data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
 803	data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
 804	data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
 805
 806	/* Register sysfs hooks */
 807	err = sysfs_create_group(&client->dev.kobj, &asb100_group);
 808	if (err)
 809		goto ERROR3;
 810
 811	data->hwmon_dev = hwmon_device_register(&client->dev);
 812	if (IS_ERR(data->hwmon_dev)) {
 813		err = PTR_ERR(data->hwmon_dev);
 814		goto ERROR4;
 815	}
 816
 817	return 0;
 818
 819ERROR4:
 820	sysfs_remove_group(&client->dev.kobj, &asb100_group);
 821ERROR3:
 822	i2c_unregister_device(data->lm75[1]);
 823	i2c_unregister_device(data->lm75[0]);
 824	return err;
 825}
 826
 827static int asb100_remove(struct i2c_client *client)
 828{
 829	struct asb100_data *data = i2c_get_clientdata(client);
 830
 831	hwmon_device_unregister(data->hwmon_dev);
 832	sysfs_remove_group(&client->dev.kobj, &asb100_group);
 833
 834	i2c_unregister_device(data->lm75[1]);
 835	i2c_unregister_device(data->lm75[0]);
 836
 837	return 0;
 838}
 839
 840/*
 841 * The SMBus locks itself, usually, but nothing may access the chip between
 842 * bank switches.
 843 */
 844static int asb100_read_value(struct i2c_client *client, u16 reg)
 845{
 846	struct asb100_data *data = i2c_get_clientdata(client);
 847	struct i2c_client *cl;
 848	int res, bank;
 849
 850	mutex_lock(&data->lock);
 851
 852	bank = (reg >> 8) & 0x0f;
 853	if (bank > 2)
 854		/* switch banks */
 855		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
 856
 857	if (bank == 0 || bank > 2) {
 858		res = i2c_smbus_read_byte_data(client, reg & 0xff);
 859	} else {
 860		/* switch to subclient */
 861		cl = data->lm75[bank - 1];
 862
 863		/* convert from ISA to LM75 I2C addresses */
 864		switch (reg & 0xff) {
 865		case 0x50: /* TEMP */
 866			res = i2c_smbus_read_word_swapped(cl, 0);
 867			break;
 868		case 0x52: /* CONFIG */
 869			res = i2c_smbus_read_byte_data(cl, 1);
 870			break;
 871		case 0x53: /* HYST */
 872			res = i2c_smbus_read_word_swapped(cl, 2);
 873			break;
 874		case 0x55: /* MAX */
 875		default:
 876			res = i2c_smbus_read_word_swapped(cl, 3);
 877			break;
 878		}
 879	}
 880
 881	if (bank > 2)
 882		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
 883
 884	mutex_unlock(&data->lock);
 885
 886	return res;
 887}
 888
 889static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
 890{
 891	struct asb100_data *data = i2c_get_clientdata(client);
 892	struct i2c_client *cl;
 893	int bank;
 894
 895	mutex_lock(&data->lock);
 896
 897	bank = (reg >> 8) & 0x0f;
 898	if (bank > 2)
 899		/* switch banks */
 900		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
 901
 902	if (bank == 0 || bank > 2) {
 903		i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
 904	} else {
 905		/* switch to subclient */
 906		cl = data->lm75[bank - 1];
 907
 908		/* convert from ISA to LM75 I2C addresses */
 909		switch (reg & 0xff) {
 910		case 0x52: /* CONFIG */
 911			i2c_smbus_write_byte_data(cl, 1, value & 0xff);
 912			break;
 913		case 0x53: /* HYST */
 914			i2c_smbus_write_word_swapped(cl, 2, value);
 915			break;
 916		case 0x55: /* MAX */
 917			i2c_smbus_write_word_swapped(cl, 3, value);
 918			break;
 919		}
 920	}
 921
 922	if (bank > 2)
 923		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
 924
 925	mutex_unlock(&data->lock);
 926}
 927
 928static void asb100_init_client(struct i2c_client *client)
 929{
 930	struct asb100_data *data = i2c_get_clientdata(client);
 931
 932	data->vrm = vid_which_vrm();
 933
 934	/* Start monitoring */
 935	asb100_write_value(client, ASB100_REG_CONFIG,
 936		(asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
 937}
 938
 939static struct asb100_data *asb100_update_device(struct device *dev)
 940{
 941	struct i2c_client *client = to_i2c_client(dev);
 942	struct asb100_data *data = i2c_get_clientdata(client);
 943	int i;
 944
 945	mutex_lock(&data->update_lock);
 946
 947	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
 948		|| !data->valid) {
 949
 950		dev_dbg(&client->dev, "starting device update...\n");
 951
 952		/* 7 voltage inputs */
 953		for (i = 0; i < 7; i++) {
 954			data->in[i] = asb100_read_value(client,
 955				ASB100_REG_IN(i));
 956			data->in_min[i] = asb100_read_value(client,
 957				ASB100_REG_IN_MIN(i));
 958			data->in_max[i] = asb100_read_value(client,
 959				ASB100_REG_IN_MAX(i));
 960		}
 961
 962		/* 3 fan inputs */
 963		for (i = 0; i < 3; i++) {
 964			data->fan[i] = asb100_read_value(client,
 965					ASB100_REG_FAN(i));
 966			data->fan_min[i] = asb100_read_value(client,
 967					ASB100_REG_FAN_MIN(i));
 968		}
 969
 970		/* 4 temperature inputs */
 971		for (i = 1; i <= 4; i++) {
 972			data->temp[i-1] = asb100_read_value(client,
 973					ASB100_REG_TEMP(i));
 974			data->temp_max[i-1] = asb100_read_value(client,
 975					ASB100_REG_TEMP_MAX(i));
 976			data->temp_hyst[i-1] = asb100_read_value(client,
 977					ASB100_REG_TEMP_HYST(i));
 978		}
 979
 980		/* VID and fan divisors */
 981		i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
 982		data->vid = i & 0x0f;
 983		data->vid |= (asb100_read_value(client,
 984				ASB100_REG_CHIPID) & 0x01) << 4;
 985		data->fan_div[0] = (i >> 4) & 0x03;
 986		data->fan_div[1] = (i >> 6) & 0x03;
 987		data->fan_div[2] = (asb100_read_value(client,
 988				ASB100_REG_PIN) >> 6) & 0x03;
 989
 990		/* PWM */
 991		data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
 992
 993		/* alarms */
 994		data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
 995			(asb100_read_value(client, ASB100_REG_ALARM2) << 8);
 996
 997		data->last_updated = jiffies;
 998		data->valid = 1;
 999
1000		dev_dbg(&client->dev, "... device update complete\n");
1001	}
1002
1003	mutex_unlock(&data->update_lock);
1004
1005	return data;
1006}
1007
1008module_i2c_driver(asb100_driver);
1009
1010MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1011MODULE_DESCRIPTION("ASB100 Bach driver");
1012MODULE_LICENSE("GPL");
v6.2
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
   4 *	      monitoring
   5 *
   6 * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
   7 *
   8 * (derived from w83781d.c)
   9 *
  10 * Copyright (C) 1998 - 2003  Frodo Looijaard <frodol@dds.nl>,
  11 *			      Philip Edelbrock <phil@netroedge.com>, and
  12 *			      Mark Studebaker <mdsxyz123@yahoo.com>
  13 */
  14
  15/*
  16 * This driver supports the hardware sensor chips: Asus ASB100 and
  17 * ASB100-A "BACH".
  18 *
  19 * ASB100-A supports pwm1, while plain ASB100 does not.  There is no known
  20 * way for the driver to tell which one is there.
  21 *
  22 * Chip		#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
  23 * asb100	7	3	1	4	0x31	0x0694	yes	no
  24 */
  25
  26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27
  28#include <linux/module.h>
  29#include <linux/slab.h>
  30#include <linux/i2c.h>
  31#include <linux/hwmon.h>
  32#include <linux/hwmon-sysfs.h>
  33#include <linux/hwmon-vid.h>
  34#include <linux/err.h>
  35#include <linux/init.h>
  36#include <linux/jiffies.h>
  37#include <linux/mutex.h>
  38#include "lm75.h"
  39
  40/* I2C addresses to scan */
  41static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
  42
  43static unsigned short force_subclients[4];
  44module_param_array(force_subclients, short, NULL, 0);
  45MODULE_PARM_DESC(force_subclients,
  46	"List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
  47
  48/* Voltage IN registers 0-6 */
  49#define ASB100_REG_IN(nr)	(0x20 + (nr))
  50#define ASB100_REG_IN_MAX(nr)	(0x2b + (nr * 2))
  51#define ASB100_REG_IN_MIN(nr)	(0x2c + (nr * 2))
  52
  53/* FAN IN registers 1-3 */
  54#define ASB100_REG_FAN(nr)	(0x28 + (nr))
  55#define ASB100_REG_FAN_MIN(nr)	(0x3b + (nr))
  56
  57/* TEMPERATURE registers 1-4 */
  58static const u16 asb100_reg_temp[]	= {0, 0x27, 0x150, 0x250, 0x17};
  59static const u16 asb100_reg_temp_max[]	= {0, 0x39, 0x155, 0x255, 0x18};
  60static const u16 asb100_reg_temp_hyst[]	= {0, 0x3a, 0x153, 0x253, 0x19};
  61
  62#define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
  63#define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
  64#define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
  65
  66#define ASB100_REG_TEMP2_CONFIG	0x0152
  67#define ASB100_REG_TEMP3_CONFIG	0x0252
  68
  69
  70#define ASB100_REG_CONFIG	0x40
  71#define ASB100_REG_ALARM1	0x41
  72#define ASB100_REG_ALARM2	0x42
  73#define ASB100_REG_SMIM1	0x43
  74#define ASB100_REG_SMIM2	0x44
  75#define ASB100_REG_VID_FANDIV	0x47
  76#define ASB100_REG_I2C_ADDR	0x48
  77#define ASB100_REG_CHIPID	0x49
  78#define ASB100_REG_I2C_SUBADDR	0x4a
  79#define ASB100_REG_PIN		0x4b
  80#define ASB100_REG_IRQ		0x4c
  81#define ASB100_REG_BANK		0x4e
  82#define ASB100_REG_CHIPMAN	0x4f
  83
  84#define ASB100_REG_WCHIPID	0x58
  85
  86/* bit 7 -> enable, bits 0-3 -> duty cycle */
  87#define ASB100_REG_PWM1		0x59
  88
  89/*
  90 * CONVERSIONS
  91 * Rounding and limit checking is only done on the TO_REG variants.
  92 */
  93
  94/* These constants are a guess, consistent w/ w83781d */
  95#define ASB100_IN_MIN		0
  96#define ASB100_IN_MAX		4080
  97
  98/*
  99 * IN: 1/1000 V (0V to 4.08V)
 100 * REG: 16mV/bit
 101 */
 102static u8 IN_TO_REG(unsigned val)
 103{
 104	unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
 105	return (nval + 8) / 16;
 106}
 107
 108static unsigned IN_FROM_REG(u8 reg)
 109{
 110	return reg * 16;
 111}
 112
 113static u8 FAN_TO_REG(long rpm, int div)
 114{
 115	if (rpm == -1)
 116		return 0;
 117	if (rpm == 0)
 118		return 255;
 119	rpm = clamp_val(rpm, 1, 1000000);
 120	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
 121}
 122
 123static int FAN_FROM_REG(u8 val, int div)
 124{
 125	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
 126}
 127
 128/* These constants are a guess, consistent w/ w83781d */
 129#define ASB100_TEMP_MIN		-128000
 130#define ASB100_TEMP_MAX		127000
 131
 132/*
 133 * TEMP: 0.001C/bit (-128C to +127C)
 134 * REG: 1C/bit, two's complement
 135 */
 136static u8 TEMP_TO_REG(long temp)
 137{
 138	int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
 139	ntemp += (ntemp < 0 ? -500 : 500);
 140	return (u8)(ntemp / 1000);
 141}
 142
 143static int TEMP_FROM_REG(u8 reg)
 144{
 145	return (s8)reg * 1000;
 146}
 147
 148/*
 149 * PWM: 0 - 255 per sensors documentation
 150 * REG: (6.25% duty cycle per bit)
 151 */
 152static u8 ASB100_PWM_TO_REG(int pwm)
 153{
 154	pwm = clamp_val(pwm, 0, 255);
 155	return (u8)(pwm / 16);
 156}
 157
 158static int ASB100_PWM_FROM_REG(u8 reg)
 159{
 160	return reg * 16;
 161}
 162
 163#define DIV_FROM_REG(val) (1 << (val))
 164
 165/*
 166 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
 167 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
 168 */
 169static u8 DIV_TO_REG(long val)
 170{
 171	return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
 172}
 173
 174/*
 175 * For each registered client, we need to keep some data in memory. That
 176 * data is pointed to by client->data. The structure itself is
 177 * dynamically allocated, at the same time the client itself is allocated.
 178 */
 179struct asb100_data {
 180	struct device *hwmon_dev;
 181	struct mutex lock;
 182
 183	struct mutex update_lock;
 184	unsigned long last_updated;	/* In jiffies */
 185
 186	/* array of 2 pointers to subclients */
 187	struct i2c_client *lm75[2];
 188
 189	bool valid;		/* true if following fields are valid */
 190	u8 in[7];		/* Register value */
 191	u8 in_max[7];		/* Register value */
 192	u8 in_min[7];		/* Register value */
 193	u8 fan[3];		/* Register value */
 194	u8 fan_min[3];		/* Register value */
 195	u16 temp[4];		/* Register value (0 and 3 are u8 only) */
 196	u16 temp_max[4];	/* Register value (0 and 3 are u8 only) */
 197	u16 temp_hyst[4];	/* Register value (0 and 3 are u8 only) */
 198	u8 fan_div[3];		/* Register encoding, right justified */
 199	u8 pwm;			/* Register encoding */
 200	u8 vid;			/* Register encoding, combined */
 201	u32 alarms;		/* Register encoding, combined */
 202	u8 vrm;
 203};
 204
 205static int asb100_read_value(struct i2c_client *client, u16 reg);
 206static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
 207
 208static int asb100_probe(struct i2c_client *client);
 
 209static int asb100_detect(struct i2c_client *client,
 210			 struct i2c_board_info *info);
 211static void asb100_remove(struct i2c_client *client);
 212static struct asb100_data *asb100_update_device(struct device *dev);
 213static void asb100_init_client(struct i2c_client *client);
 214
 215static const struct i2c_device_id asb100_id[] = {
 216	{ "asb100", 0 },
 217	{ }
 218};
 219MODULE_DEVICE_TABLE(i2c, asb100_id);
 220
 221static struct i2c_driver asb100_driver = {
 222	.class		= I2C_CLASS_HWMON,
 223	.driver = {
 224		.name	= "asb100",
 225	},
 226	.probe_new	= asb100_probe,
 227	.remove		= asb100_remove,
 228	.id_table	= asb100_id,
 229	.detect		= asb100_detect,
 230	.address_list	= normal_i2c,
 231};
 232
 233/* 7 Voltages */
 234#define show_in_reg(reg) \
 235static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
 236		char *buf) \
 237{ \
 238	int nr = to_sensor_dev_attr(attr)->index; \
 239	struct asb100_data *data = asb100_update_device(dev); \
 240	return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
 241}
 242
 243show_in_reg(in)
 244show_in_reg(in_min)
 245show_in_reg(in_max)
 246
 247#define set_in_reg(REG, reg) \
 248static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
 249		const char *buf, size_t count) \
 250{ \
 251	int nr = to_sensor_dev_attr(attr)->index; \
 252	struct i2c_client *client = to_i2c_client(dev); \
 253	struct asb100_data *data = i2c_get_clientdata(client); \
 254	unsigned long val; \
 255	int err = kstrtoul(buf, 10, &val); \
 256	if (err) \
 257		return err; \
 258	mutex_lock(&data->update_lock); \
 259	data->in_##reg[nr] = IN_TO_REG(val); \
 260	asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
 261		data->in_##reg[nr]); \
 262	mutex_unlock(&data->update_lock); \
 263	return count; \
 264}
 265
 266set_in_reg(MIN, min)
 267set_in_reg(MAX, max)
 268
 269#define sysfs_in(offset) \
 270static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
 271		show_in, NULL, offset); \
 272static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
 273		show_in_min, set_in_min, offset); \
 274static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
 275		show_in_max, set_in_max, offset)
 276
 277sysfs_in(0);
 278sysfs_in(1);
 279sysfs_in(2);
 280sysfs_in(3);
 281sysfs_in(4);
 282sysfs_in(5);
 283sysfs_in(6);
 284
 285/* 3 Fans */
 286static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 287		char *buf)
 288{
 289	int nr = to_sensor_dev_attr(attr)->index;
 290	struct asb100_data *data = asb100_update_device(dev);
 291	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
 292		DIV_FROM_REG(data->fan_div[nr])));
 293}
 294
 295static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 296		char *buf)
 297{
 298	int nr = to_sensor_dev_attr(attr)->index;
 299	struct asb100_data *data = asb100_update_device(dev);
 300	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
 301		DIV_FROM_REG(data->fan_div[nr])));
 302}
 303
 304static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
 305		char *buf)
 306{
 307	int nr = to_sensor_dev_attr(attr)->index;
 308	struct asb100_data *data = asb100_update_device(dev);
 309	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
 310}
 311
 312static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 313		const char *buf, size_t count)
 314{
 315	int nr = to_sensor_dev_attr(attr)->index;
 316	struct i2c_client *client = to_i2c_client(dev);
 317	struct asb100_data *data = i2c_get_clientdata(client);
 318	unsigned long val;
 319	int err;
 320
 321	err = kstrtoul(buf, 10, &val);
 322	if (err)
 323		return err;
 324
 325	mutex_lock(&data->update_lock);
 326	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
 327	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
 328	mutex_unlock(&data->update_lock);
 329	return count;
 330}
 331
 332/*
 333 * Note: we save and restore the fan minimum here, because its value is
 334 * determined in part by the fan divisor.  This follows the principle of
 335 * least surprise; the user doesn't expect the fan minimum to change just
 336 * because the divisor changed.
 337 */
 338static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
 339		const char *buf, size_t count)
 340{
 341	int nr = to_sensor_dev_attr(attr)->index;
 342	struct i2c_client *client = to_i2c_client(dev);
 343	struct asb100_data *data = i2c_get_clientdata(client);
 344	unsigned long min;
 345	int reg;
 346	unsigned long val;
 347	int err;
 348
 349	err = kstrtoul(buf, 10, &val);
 350	if (err)
 351		return err;
 352
 353	mutex_lock(&data->update_lock);
 354
 355	min = FAN_FROM_REG(data->fan_min[nr],
 356			DIV_FROM_REG(data->fan_div[nr]));
 357	data->fan_div[nr] = DIV_TO_REG(val);
 358
 359	switch (nr) {
 360	case 0:	/* fan 1 */
 361		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
 362		reg = (reg & 0xcf) | (data->fan_div[0] << 4);
 363		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
 364		break;
 365
 366	case 1:	/* fan 2 */
 367		reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
 368		reg = (reg & 0x3f) | (data->fan_div[1] << 6);
 369		asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
 370		break;
 371
 372	case 2:	/* fan 3 */
 373		reg = asb100_read_value(client, ASB100_REG_PIN);
 374		reg = (reg & 0x3f) | (data->fan_div[2] << 6);
 375		asb100_write_value(client, ASB100_REG_PIN, reg);
 376		break;
 377	}
 378
 379	data->fan_min[nr] =
 380		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
 381	asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
 382
 383	mutex_unlock(&data->update_lock);
 384
 385	return count;
 386}
 387
 388#define sysfs_fan(offset) \
 389static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
 390		show_fan, NULL, offset - 1); \
 391static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
 392		show_fan_min, set_fan_min, offset - 1); \
 393static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
 394		show_fan_div, set_fan_div, offset - 1)
 395
 396sysfs_fan(1);
 397sysfs_fan(2);
 398sysfs_fan(3);
 399
 400/* 4 Temp. Sensors */
 401static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
 402{
 403	int ret = 0;
 404
 405	switch (nr) {
 406	case 1: case 2:
 407		ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
 408		break;
 409	case 0: case 3: default:
 410		ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
 411		break;
 412	}
 413	return ret;
 414}
 415
 416#define show_temp_reg(reg) \
 417static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
 418		char *buf) \
 419{ \
 420	int nr = to_sensor_dev_attr(attr)->index; \
 421	struct asb100_data *data = asb100_update_device(dev); \
 422	return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
 423}
 424
 425show_temp_reg(temp);
 426show_temp_reg(temp_max);
 427show_temp_reg(temp_hyst);
 428
 429#define set_temp_reg(REG, reg) \
 430static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
 431		const char *buf, size_t count) \
 432{ \
 433	int nr = to_sensor_dev_attr(attr)->index; \
 434	struct i2c_client *client = to_i2c_client(dev); \
 435	struct asb100_data *data = i2c_get_clientdata(client); \
 436	long val; \
 437	int err = kstrtol(buf, 10, &val); \
 438	if (err) \
 439		return err; \
 440	mutex_lock(&data->update_lock); \
 441	switch (nr) { \
 442	case 1: case 2: \
 443		data->reg[nr] = LM75_TEMP_TO_REG(val); \
 444		break; \
 445	case 0: case 3: default: \
 446		data->reg[nr] = TEMP_TO_REG(val); \
 447		break; \
 448	} \
 449	asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
 450			data->reg[nr]); \
 451	mutex_unlock(&data->update_lock); \
 452	return count; \
 453}
 454
 455set_temp_reg(MAX, temp_max);
 456set_temp_reg(HYST, temp_hyst);
 457
 458#define sysfs_temp(num) \
 459static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
 460		show_temp, NULL, num - 1); \
 461static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
 462		show_temp_max, set_temp_max, num - 1); \
 463static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
 464		show_temp_hyst, set_temp_hyst, num - 1)
 465
 466sysfs_temp(1);
 467sysfs_temp(2);
 468sysfs_temp(3);
 469sysfs_temp(4);
 470
 471/* VID */
 472static ssize_t cpu0_vid_show(struct device *dev,
 473			     struct device_attribute *attr, char *buf)
 474{
 475	struct asb100_data *data = asb100_update_device(dev);
 476	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
 477}
 478
 479static DEVICE_ATTR_RO(cpu0_vid);
 480
 481/* VRM */
 482static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
 483		char *buf)
 484{
 485	struct asb100_data *data = dev_get_drvdata(dev);
 486	return sprintf(buf, "%d\n", data->vrm);
 487}
 488
 489static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
 490			 const char *buf, size_t count)
 491{
 492	struct asb100_data *data = dev_get_drvdata(dev);
 493	unsigned long val;
 494	int err;
 495
 496	err = kstrtoul(buf, 10, &val);
 497	if (err)
 498		return err;
 499
 500	if (val > 255)
 501		return -EINVAL;
 502
 503	data->vrm = val;
 504	return count;
 505}
 506
 507/* Alarms */
 508static DEVICE_ATTR_RW(vrm);
 509
 510static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
 511		char *buf)
 512{
 513	struct asb100_data *data = asb100_update_device(dev);
 514	return sprintf(buf, "%u\n", data->alarms);
 515}
 516
 517static DEVICE_ATTR_RO(alarms);
 518
 519static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
 520		char *buf)
 521{
 522	int bitnr = to_sensor_dev_attr(attr)->index;
 523	struct asb100_data *data = asb100_update_device(dev);
 524	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
 525}
 526static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
 527static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
 528static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
 529static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
 530static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
 531static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
 532static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
 533static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
 534static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
 535static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
 536static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
 537
 538/* 1 PWM */
 539static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
 540		char *buf)
 541{
 542	struct asb100_data *data = asb100_update_device(dev);
 543	return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
 544}
 545
 546static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
 547			  const char *buf, size_t count)
 548{
 549	struct i2c_client *client = to_i2c_client(dev);
 550	struct asb100_data *data = i2c_get_clientdata(client);
 551	unsigned long val;
 552	int err;
 553
 554	err = kstrtoul(buf, 10, &val);
 555	if (err)
 556		return err;
 557
 558	mutex_lock(&data->update_lock);
 559	data->pwm &= 0x80; /* keep the enable bit */
 560	data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
 561	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
 562	mutex_unlock(&data->update_lock);
 563	return count;
 564}
 565
 566static ssize_t pwm1_enable_show(struct device *dev,
 567		struct device_attribute *attr, char *buf)
 568{
 569	struct asb100_data *data = asb100_update_device(dev);
 570	return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
 571}
 572
 573static ssize_t pwm1_enable_store(struct device *dev,
 574				 struct device_attribute *attr,
 575				 const char *buf, size_t count)
 576{
 577	struct i2c_client *client = to_i2c_client(dev);
 578	struct asb100_data *data = i2c_get_clientdata(client);
 579	unsigned long val;
 580	int err;
 581
 582	err = kstrtoul(buf, 10, &val);
 583	if (err)
 584		return err;
 585
 586	mutex_lock(&data->update_lock);
 587	data->pwm &= 0x0f; /* keep the duty cycle bits */
 588	data->pwm |= (val ? 0x80 : 0x00);
 589	asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
 590	mutex_unlock(&data->update_lock);
 591	return count;
 592}
 593
 594static DEVICE_ATTR_RW(pwm1);
 595static DEVICE_ATTR_RW(pwm1_enable);
 596
 597static struct attribute *asb100_attributes[] = {
 598	&sensor_dev_attr_in0_input.dev_attr.attr,
 599	&sensor_dev_attr_in0_min.dev_attr.attr,
 600	&sensor_dev_attr_in0_max.dev_attr.attr,
 601	&sensor_dev_attr_in1_input.dev_attr.attr,
 602	&sensor_dev_attr_in1_min.dev_attr.attr,
 603	&sensor_dev_attr_in1_max.dev_attr.attr,
 604	&sensor_dev_attr_in2_input.dev_attr.attr,
 605	&sensor_dev_attr_in2_min.dev_attr.attr,
 606	&sensor_dev_attr_in2_max.dev_attr.attr,
 607	&sensor_dev_attr_in3_input.dev_attr.attr,
 608	&sensor_dev_attr_in3_min.dev_attr.attr,
 609	&sensor_dev_attr_in3_max.dev_attr.attr,
 610	&sensor_dev_attr_in4_input.dev_attr.attr,
 611	&sensor_dev_attr_in4_min.dev_attr.attr,
 612	&sensor_dev_attr_in4_max.dev_attr.attr,
 613	&sensor_dev_attr_in5_input.dev_attr.attr,
 614	&sensor_dev_attr_in5_min.dev_attr.attr,
 615	&sensor_dev_attr_in5_max.dev_attr.attr,
 616	&sensor_dev_attr_in6_input.dev_attr.attr,
 617	&sensor_dev_attr_in6_min.dev_attr.attr,
 618	&sensor_dev_attr_in6_max.dev_attr.attr,
 619
 620	&sensor_dev_attr_fan1_input.dev_attr.attr,
 621	&sensor_dev_attr_fan1_min.dev_attr.attr,
 622	&sensor_dev_attr_fan1_div.dev_attr.attr,
 623	&sensor_dev_attr_fan2_input.dev_attr.attr,
 624	&sensor_dev_attr_fan2_min.dev_attr.attr,
 625	&sensor_dev_attr_fan2_div.dev_attr.attr,
 626	&sensor_dev_attr_fan3_input.dev_attr.attr,
 627	&sensor_dev_attr_fan3_min.dev_attr.attr,
 628	&sensor_dev_attr_fan3_div.dev_attr.attr,
 629
 630	&sensor_dev_attr_temp1_input.dev_attr.attr,
 631	&sensor_dev_attr_temp1_max.dev_attr.attr,
 632	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
 633	&sensor_dev_attr_temp2_input.dev_attr.attr,
 634	&sensor_dev_attr_temp2_max.dev_attr.attr,
 635	&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
 636	&sensor_dev_attr_temp3_input.dev_attr.attr,
 637	&sensor_dev_attr_temp3_max.dev_attr.attr,
 638	&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
 639	&sensor_dev_attr_temp4_input.dev_attr.attr,
 640	&sensor_dev_attr_temp4_max.dev_attr.attr,
 641	&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
 642
 643	&sensor_dev_attr_in0_alarm.dev_attr.attr,
 644	&sensor_dev_attr_in1_alarm.dev_attr.attr,
 645	&sensor_dev_attr_in2_alarm.dev_attr.attr,
 646	&sensor_dev_attr_in3_alarm.dev_attr.attr,
 647	&sensor_dev_attr_in4_alarm.dev_attr.attr,
 648	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
 649	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
 650	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
 651	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
 652	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
 653	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
 654
 655	&dev_attr_cpu0_vid.attr,
 656	&dev_attr_vrm.attr,
 657	&dev_attr_alarms.attr,
 658	&dev_attr_pwm1.attr,
 659	&dev_attr_pwm1_enable.attr,
 660
 661	NULL
 662};
 663
 664static const struct attribute_group asb100_group = {
 665	.attrs = asb100_attributes,
 666};
 667
 668static int asb100_detect_subclients(struct i2c_client *client)
 669{
 670	int i, id, err;
 671	int address = client->addr;
 672	unsigned short sc_addr[2];
 673	struct asb100_data *data = i2c_get_clientdata(client);
 674	struct i2c_adapter *adapter = client->adapter;
 675
 676	id = i2c_adapter_id(adapter);
 677
 678	if (force_subclients[0] == id && force_subclients[1] == address) {
 679		for (i = 2; i <= 3; i++) {
 680			if (force_subclients[i] < 0x48 ||
 681			    force_subclients[i] > 0x4f) {
 682				dev_err(&client->dev,
 683					"invalid subclient address %d; must be 0x48-0x4f\n",
 684					force_subclients[i]);
 685				err = -ENODEV;
 686				goto ERROR_SC_2;
 687			}
 688		}
 689		asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
 690					(force_subclients[2] & 0x07) |
 691					((force_subclients[3] & 0x07) << 4));
 692		sc_addr[0] = force_subclients[2];
 693		sc_addr[1] = force_subclients[3];
 694	} else {
 695		int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
 696		sc_addr[0] = 0x48 + (val & 0x07);
 697		sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
 698	}
 699
 700	if (sc_addr[0] == sc_addr[1]) {
 701		dev_err(&client->dev,
 702			"duplicate addresses 0x%x for subclients\n",
 703			sc_addr[0]);
 704		err = -ENODEV;
 705		goto ERROR_SC_2;
 706	}
 707
 708	data->lm75[0] = i2c_new_dummy_device(adapter, sc_addr[0]);
 709	if (IS_ERR(data->lm75[0])) {
 710		dev_err(&client->dev,
 711			"subclient %d registration at address 0x%x failed.\n",
 712			1, sc_addr[0]);
 713		err = PTR_ERR(data->lm75[0]);
 714		goto ERROR_SC_2;
 715	}
 716
 717	data->lm75[1] = i2c_new_dummy_device(adapter, sc_addr[1]);
 718	if (IS_ERR(data->lm75[1])) {
 719		dev_err(&client->dev,
 720			"subclient %d registration at address 0x%x failed.\n",
 721			2, sc_addr[1]);
 722		err = PTR_ERR(data->lm75[1]);
 723		goto ERROR_SC_3;
 724	}
 725
 726	return 0;
 727
 728/* Undo inits in case of errors */
 729ERROR_SC_3:
 730	i2c_unregister_device(data->lm75[0]);
 731ERROR_SC_2:
 732	return err;
 733}
 734
 735/* Return 0 if detection is successful, -ENODEV otherwise */
 736static int asb100_detect(struct i2c_client *client,
 737			 struct i2c_board_info *info)
 738{
 739	struct i2c_adapter *adapter = client->adapter;
 740	int val1, val2;
 741
 742	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 743		pr_debug("detect failed, smbus byte data not supported!\n");
 744		return -ENODEV;
 745	}
 746
 747	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
 748	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
 749
 750	/* If we're in bank 0 */
 751	if ((!(val1 & 0x07)) &&
 752			/* Check for ASB100 ID (low byte) */
 753			(((!(val1 & 0x80)) && (val2 != 0x94)) ||
 754			/* Check for ASB100 ID (high byte ) */
 755			((val1 & 0x80) && (val2 != 0x06)))) {
 756		pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
 757		return -ENODEV;
 758	}
 759
 760	/* Put it now into bank 0 and Vendor ID High Byte */
 761	i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
 762		(i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
 763		| 0x80);
 764
 765	/* Determine the chip type. */
 766	val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
 767	val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
 768
 769	if (val1 != 0x31 || val2 != 0x06)
 770		return -ENODEV;
 771
 772	strscpy(info->type, "asb100", I2C_NAME_SIZE);
 773
 774	return 0;
 775}
 776
 777static int asb100_probe(struct i2c_client *client)
 
 778{
 779	int err;
 780	struct asb100_data *data;
 781
 782	data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
 783			    GFP_KERNEL);
 784	if (!data)
 785		return -ENOMEM;
 786
 787	i2c_set_clientdata(client, data);
 788	mutex_init(&data->lock);
 789	mutex_init(&data->update_lock);
 790
 791	/* Attach secondary lm75 clients */
 792	err = asb100_detect_subclients(client);
 793	if (err)
 794		return err;
 795
 796	/* Initialize the chip */
 797	asb100_init_client(client);
 798
 799	/* A few vars need to be filled upon startup */
 800	data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
 801	data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
 802	data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
 803
 804	/* Register sysfs hooks */
 805	err = sysfs_create_group(&client->dev.kobj, &asb100_group);
 806	if (err)
 807		goto ERROR3;
 808
 809	data->hwmon_dev = hwmon_device_register(&client->dev);
 810	if (IS_ERR(data->hwmon_dev)) {
 811		err = PTR_ERR(data->hwmon_dev);
 812		goto ERROR4;
 813	}
 814
 815	return 0;
 816
 817ERROR4:
 818	sysfs_remove_group(&client->dev.kobj, &asb100_group);
 819ERROR3:
 820	i2c_unregister_device(data->lm75[1]);
 821	i2c_unregister_device(data->lm75[0]);
 822	return err;
 823}
 824
 825static void asb100_remove(struct i2c_client *client)
 826{
 827	struct asb100_data *data = i2c_get_clientdata(client);
 828
 829	hwmon_device_unregister(data->hwmon_dev);
 830	sysfs_remove_group(&client->dev.kobj, &asb100_group);
 831
 832	i2c_unregister_device(data->lm75[1]);
 833	i2c_unregister_device(data->lm75[0]);
 
 
 834}
 835
 836/*
 837 * The SMBus locks itself, usually, but nothing may access the chip between
 838 * bank switches.
 839 */
 840static int asb100_read_value(struct i2c_client *client, u16 reg)
 841{
 842	struct asb100_data *data = i2c_get_clientdata(client);
 843	struct i2c_client *cl;
 844	int res, bank;
 845
 846	mutex_lock(&data->lock);
 847
 848	bank = (reg >> 8) & 0x0f;
 849	if (bank > 2)
 850		/* switch banks */
 851		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
 852
 853	if (bank == 0 || bank > 2) {
 854		res = i2c_smbus_read_byte_data(client, reg & 0xff);
 855	} else {
 856		/* switch to subclient */
 857		cl = data->lm75[bank - 1];
 858
 859		/* convert from ISA to LM75 I2C addresses */
 860		switch (reg & 0xff) {
 861		case 0x50: /* TEMP */
 862			res = i2c_smbus_read_word_swapped(cl, 0);
 863			break;
 864		case 0x52: /* CONFIG */
 865			res = i2c_smbus_read_byte_data(cl, 1);
 866			break;
 867		case 0x53: /* HYST */
 868			res = i2c_smbus_read_word_swapped(cl, 2);
 869			break;
 870		case 0x55: /* MAX */
 871		default:
 872			res = i2c_smbus_read_word_swapped(cl, 3);
 873			break;
 874		}
 875	}
 876
 877	if (bank > 2)
 878		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
 879
 880	mutex_unlock(&data->lock);
 881
 882	return res;
 883}
 884
 885static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
 886{
 887	struct asb100_data *data = i2c_get_clientdata(client);
 888	struct i2c_client *cl;
 889	int bank;
 890
 891	mutex_lock(&data->lock);
 892
 893	bank = (reg >> 8) & 0x0f;
 894	if (bank > 2)
 895		/* switch banks */
 896		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
 897
 898	if (bank == 0 || bank > 2) {
 899		i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
 900	} else {
 901		/* switch to subclient */
 902		cl = data->lm75[bank - 1];
 903
 904		/* convert from ISA to LM75 I2C addresses */
 905		switch (reg & 0xff) {
 906		case 0x52: /* CONFIG */
 907			i2c_smbus_write_byte_data(cl, 1, value & 0xff);
 908			break;
 909		case 0x53: /* HYST */
 910			i2c_smbus_write_word_swapped(cl, 2, value);
 911			break;
 912		case 0x55: /* MAX */
 913			i2c_smbus_write_word_swapped(cl, 3, value);
 914			break;
 915		}
 916	}
 917
 918	if (bank > 2)
 919		i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
 920
 921	mutex_unlock(&data->lock);
 922}
 923
 924static void asb100_init_client(struct i2c_client *client)
 925{
 926	struct asb100_data *data = i2c_get_clientdata(client);
 927
 928	data->vrm = vid_which_vrm();
 929
 930	/* Start monitoring */
 931	asb100_write_value(client, ASB100_REG_CONFIG,
 932		(asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
 933}
 934
 935static struct asb100_data *asb100_update_device(struct device *dev)
 936{
 937	struct i2c_client *client = to_i2c_client(dev);
 938	struct asb100_data *data = i2c_get_clientdata(client);
 939	int i;
 940
 941	mutex_lock(&data->update_lock);
 942
 943	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
 944		|| !data->valid) {
 945
 946		dev_dbg(&client->dev, "starting device update...\n");
 947
 948		/* 7 voltage inputs */
 949		for (i = 0; i < 7; i++) {
 950			data->in[i] = asb100_read_value(client,
 951				ASB100_REG_IN(i));
 952			data->in_min[i] = asb100_read_value(client,
 953				ASB100_REG_IN_MIN(i));
 954			data->in_max[i] = asb100_read_value(client,
 955				ASB100_REG_IN_MAX(i));
 956		}
 957
 958		/* 3 fan inputs */
 959		for (i = 0; i < 3; i++) {
 960			data->fan[i] = asb100_read_value(client,
 961					ASB100_REG_FAN(i));
 962			data->fan_min[i] = asb100_read_value(client,
 963					ASB100_REG_FAN_MIN(i));
 964		}
 965
 966		/* 4 temperature inputs */
 967		for (i = 1; i <= 4; i++) {
 968			data->temp[i-1] = asb100_read_value(client,
 969					ASB100_REG_TEMP(i));
 970			data->temp_max[i-1] = asb100_read_value(client,
 971					ASB100_REG_TEMP_MAX(i));
 972			data->temp_hyst[i-1] = asb100_read_value(client,
 973					ASB100_REG_TEMP_HYST(i));
 974		}
 975
 976		/* VID and fan divisors */
 977		i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
 978		data->vid = i & 0x0f;
 979		data->vid |= (asb100_read_value(client,
 980				ASB100_REG_CHIPID) & 0x01) << 4;
 981		data->fan_div[0] = (i >> 4) & 0x03;
 982		data->fan_div[1] = (i >> 6) & 0x03;
 983		data->fan_div[2] = (asb100_read_value(client,
 984				ASB100_REG_PIN) >> 6) & 0x03;
 985
 986		/* PWM */
 987		data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
 988
 989		/* alarms */
 990		data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
 991			(asb100_read_value(client, ASB100_REG_ALARM2) << 8);
 992
 993		data->last_updated = jiffies;
 994		data->valid = true;
 995
 996		dev_dbg(&client->dev, "... device update complete\n");
 997	}
 998
 999	mutex_unlock(&data->update_lock);
1000
1001	return data;
1002}
1003
1004module_i2c_driver(asb100_driver);
1005
1006MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1007MODULE_DESCRIPTION("ASB100 Bach driver");
1008MODULE_LICENSE("GPL");