Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2	amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
   3	monitoring
   4	Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
   5
   6	Based on max6650.c:
   7	Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
   8
   9	This program is free software; you can redistribute it and/or modify
  10	it under the terms of the GNU General Public License as published by
  11	the Free Software Foundation; either version 2 of the License, or
  12	(at your option) any later version.
  13
  14	This program is distributed in the hope that it will be useful,
  15	but WITHOUT ANY WARRANTY; without even the implied warranty of
  16	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17	GNU General Public License for more details.
  18
  19	You should have received a copy of the GNU General Public License
  20	along with this program; if not, write to the Free Software
  21	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22*/
  23
  24
  25#include <linux/kernel.h>	/* Needed for KERN_INFO */
  26#include <linux/module.h>
  27#include <linux/init.h>
  28#include <linux/slab.h>
  29#include <linux/jiffies.h>
  30#include <linux/i2c.h>
  31#include <linux/hwmon.h>
  32#include <linux/hwmon-sysfs.h>
  33#include <linux/err.h>
  34#include <linux/mutex.h>
  35
  36
  37/*
  38 * Addresses to scan.
  39 */
  40
  41static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
  42	0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
  43
  44
  45
  46/*
  47 * Insmod parameters
  48 */
  49
  50static int pwminv = 0;	/*Inverted PWM output. */
  51module_param(pwminv, int, S_IRUGO);
  52
  53static int init = 1; /*Power-on initialization.*/
  54module_param(init, int, S_IRUGO);
  55
  56
  57enum chips { amc6821 };
  58
  59#define AMC6821_REG_DEV_ID 0x3D
  60#define AMC6821_REG_COMP_ID 0x3E
  61#define AMC6821_REG_CONF1 0x00
  62#define AMC6821_REG_CONF2 0x01
  63#define AMC6821_REG_CONF3 0x3F
  64#define AMC6821_REG_CONF4 0x04
  65#define AMC6821_REG_STAT1 0x02
  66#define AMC6821_REG_STAT2 0x03
  67#define AMC6821_REG_TDATA_LOW 0x08
  68#define AMC6821_REG_TDATA_HI 0x09
  69#define AMC6821_REG_LTEMP_HI 0x0A
  70#define AMC6821_REG_RTEMP_HI 0x0B
  71#define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
  72#define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
  73#define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
  74#define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
  75#define AMC6821_REG_LTEMP_CRIT 0x1B
  76#define AMC6821_REG_RTEMP_CRIT 0x1D
  77#define AMC6821_REG_PSV_TEMP 0x1C
  78#define AMC6821_REG_DCY 0x22
  79#define AMC6821_REG_LTEMP_FAN_CTRL 0x24
  80#define AMC6821_REG_RTEMP_FAN_CTRL 0x25
  81#define AMC6821_REG_DCY_LOW_TEMP 0x21
  82
  83#define AMC6821_REG_TACH_LLIMITL 0x10
  84#define AMC6821_REG_TACH_LLIMITH 0x11
  85#define AMC6821_REG_TACH_HLIMITL 0x12
  86#define AMC6821_REG_TACH_HLIMITH 0x13
  87
  88#define AMC6821_CONF1_START 0x01
  89#define AMC6821_CONF1_FAN_INT_EN 0x02
  90#define AMC6821_CONF1_FANIE 0x04
  91#define AMC6821_CONF1_PWMINV 0x08
  92#define AMC6821_CONF1_FAN_FAULT_EN 0x10
  93#define AMC6821_CONF1_FDRC0 0x20
  94#define AMC6821_CONF1_FDRC1 0x40
  95#define AMC6821_CONF1_THERMOVIE 0x80
  96
  97#define AMC6821_CONF2_PWM_EN 0x01
  98#define AMC6821_CONF2_TACH_MODE 0x02
  99#define AMC6821_CONF2_TACH_EN 0x04
 100#define AMC6821_CONF2_RTFIE 0x08
 101#define AMC6821_CONF2_LTOIE 0x10
 102#define AMC6821_CONF2_RTOIE 0x20
 103#define AMC6821_CONF2_PSVIE 0x40
 104#define AMC6821_CONF2_RST 0x80
 105
 106#define AMC6821_CONF3_THERM_FAN_EN 0x80
 107#define AMC6821_CONF3_REV_MASK 0x0F
 108
 109#define AMC6821_CONF4_OVREN 0x10
 110#define AMC6821_CONF4_TACH_FAST 0x20
 111#define AMC6821_CONF4_PSPR 0x40
 112#define AMC6821_CONF4_MODE 0x80
 113
 114#define AMC6821_STAT1_RPM_ALARM 0x01
 115#define AMC6821_STAT1_FANS 0x02
 116#define AMC6821_STAT1_RTH 0x04
 117#define AMC6821_STAT1_RTL 0x08
 118#define AMC6821_STAT1_R_THERM 0x10
 119#define AMC6821_STAT1_RTF 0x20
 120#define AMC6821_STAT1_LTH 0x40
 121#define AMC6821_STAT1_LTL 0x80
 122
 123#define AMC6821_STAT2_RTC 0x08
 124#define AMC6821_STAT2_LTC 0x10
 125#define AMC6821_STAT2_LPSV 0x20
 126#define AMC6821_STAT2_L_THERM 0x40
 127#define AMC6821_STAT2_THERM_IN 0x80
 128
 129enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
 130	IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
 131	IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
 132	TEMP_IDX_LEN, };
 133
 134static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
 135			AMC6821_REG_LTEMP_LIMIT_MIN,
 136			AMC6821_REG_LTEMP_LIMIT_MAX,
 137			AMC6821_REG_LTEMP_CRIT,
 138			AMC6821_REG_RTEMP_HI,
 139			AMC6821_REG_RTEMP_LIMIT_MIN,
 140			AMC6821_REG_RTEMP_LIMIT_MAX,
 141			AMC6821_REG_RTEMP_CRIT, };
 142
 143enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
 144	FAN1_IDX_LEN, };
 145
 146static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
 147			AMC6821_REG_TACH_LLIMITL,
 148			AMC6821_REG_TACH_HLIMITL, };
 149
 150
 151static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
 152			AMC6821_REG_TACH_LLIMITH,
 153			AMC6821_REG_TACH_HLIMITH, };
 154
 155static int amc6821_probe(
 156		struct i2c_client *client,
 157		const struct i2c_device_id *id);
 158static int amc6821_detect(
 159		struct i2c_client *client,
 160		struct i2c_board_info *info);
 161static int amc6821_init_client(struct i2c_client *client);
 162static int amc6821_remove(struct i2c_client *client);
 163static struct amc6821_data *amc6821_update_device(struct device *dev);
 164
 165/*
 166 * Driver data (common to all clients)
 167 */
 168
 169static const struct i2c_device_id amc6821_id[] = {
 170	{ "amc6821", amc6821 },
 171	{ }
 172};
 173
 174MODULE_DEVICE_TABLE(i2c, amc6821_id);
 175
 176static struct i2c_driver amc6821_driver = {
 177	.class = I2C_CLASS_HWMON,
 178	.driver = {
 179		.name	= "amc6821",
 180	},
 181	.probe = amc6821_probe,
 182	.remove = amc6821_remove,
 183	.id_table = amc6821_id,
 184	.detect = amc6821_detect,
 185	.address_list = normal_i2c,
 186};
 187
 188
 189/*
 190 * Client data (each client gets its own)
 191  */
 192
 193struct amc6821_data {
 194	struct device *hwmon_dev;
 195	struct mutex update_lock;
 196	char valid; /* zero until following fields are valid */
 197	unsigned long last_updated; /* in jiffies */
 198
 199	/* register values */
 200	int temp[TEMP_IDX_LEN];
 201
 202	u16 fan[FAN1_IDX_LEN];
 203	u8 fan1_div;
 204
 205	u8 pwm1;
 206	u8 temp1_auto_point_temp[3];
 207	u8 temp2_auto_point_temp[3];
 208	u8 pwm1_auto_point_pwm[3];
 209	u8 pwm1_enable;
 210	u8 pwm1_auto_channels_temp;
 211
 212	u8 stat1;
 213	u8 stat2;
 214};
 215
 216
 217static ssize_t get_temp(
 218		struct device *dev,
 219		struct device_attribute *devattr,
 220		char *buf)
 221{
 222	struct amc6821_data *data = amc6821_update_device(dev);
 223	int ix = to_sensor_dev_attr(devattr)->index;
 224
 225	return sprintf(buf, "%d\n", data->temp[ix] * 1000);
 226}
 227
 228
 229
 230static ssize_t set_temp(
 231		struct device *dev,
 232		struct device_attribute *attr,
 233		const char *buf,
 234		size_t count)
 235{
 236	struct i2c_client *client = to_i2c_client(dev);
 237	struct amc6821_data *data = i2c_get_clientdata(client);
 238	int ix = to_sensor_dev_attr(attr)->index;
 239	long val;
 240
 241	int ret = strict_strtol(buf, 10, &val);
 242	if (ret)
 243		return ret;
 244	val = SENSORS_LIMIT(val / 1000, -128, 127);
 245
 246	mutex_lock(&data->update_lock);
 247	data->temp[ix] = val;
 248	if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
 249		dev_err(&client->dev, "Register write error, aborting.\n");
 250		count = -EIO;
 251	}
 252	mutex_unlock(&data->update_lock);
 253	return count;
 254}
 255
 256
 257
 258
 259static ssize_t get_temp_alarm(
 260	struct device *dev,
 261	struct device_attribute *devattr,
 262	char *buf)
 263{
 264	struct amc6821_data *data = amc6821_update_device(dev);
 265	int ix = to_sensor_dev_attr(devattr)->index;
 266	u8 flag;
 267
 268	switch (ix) {
 269	case IDX_TEMP1_MIN:
 270		flag = data->stat1 & AMC6821_STAT1_LTL;
 271		break;
 272	case IDX_TEMP1_MAX:
 273		flag = data->stat1 & AMC6821_STAT1_LTH;
 274		break;
 275	case IDX_TEMP1_CRIT:
 276		flag = data->stat2 & AMC6821_STAT2_LTC;
 277		break;
 278	case IDX_TEMP2_MIN:
 279		flag = data->stat1 & AMC6821_STAT1_RTL;
 280		break;
 281	case IDX_TEMP2_MAX:
 282		flag = data->stat1 & AMC6821_STAT1_RTH;
 283		break;
 284	case IDX_TEMP2_CRIT:
 285		flag = data->stat2 & AMC6821_STAT2_RTC;
 286		break;
 287	default:
 288		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
 289		return -EINVAL;
 290	}
 291	if (flag)
 292		return sprintf(buf, "1");
 293	else
 294		return sprintf(buf, "0");
 295}
 296
 297
 298
 299
 300static ssize_t get_temp2_fault(
 301		struct device *dev,
 302		struct device_attribute *devattr,
 303		char *buf)
 304{
 305	struct amc6821_data *data = amc6821_update_device(dev);
 306	if (data->stat1 & AMC6821_STAT1_RTF)
 307		return sprintf(buf, "1");
 308	else
 309		return sprintf(buf, "0");
 310}
 311
 312static ssize_t get_pwm1(
 313		struct device *dev,
 314		struct device_attribute *devattr,
 315		char *buf)
 316{
 317	struct amc6821_data *data = amc6821_update_device(dev);
 318	return sprintf(buf, "%d\n", data->pwm1);
 319}
 320
 321static ssize_t set_pwm1(
 322		struct device *dev,
 323		struct device_attribute *devattr,
 324		const char *buf,
 325		size_t count)
 326{
 327	struct i2c_client *client = to_i2c_client(dev);
 328	struct amc6821_data *data = i2c_get_clientdata(client);
 329	long val;
 330	int ret = strict_strtol(buf, 10, &val);
 331	if (ret)
 332		return ret;
 333
 334	mutex_lock(&data->update_lock);
 335	data->pwm1 = SENSORS_LIMIT(val , 0, 255);
 336	i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
 337	mutex_unlock(&data->update_lock);
 338	return count;
 339}
 340
 341static ssize_t get_pwm1_enable(
 342		struct device *dev,
 343		struct device_attribute *devattr,
 344		char *buf)
 345{
 346	struct amc6821_data *data = amc6821_update_device(dev);
 347	return sprintf(buf, "%d\n", data->pwm1_enable);
 348}
 349
 350static ssize_t set_pwm1_enable(
 351		struct device *dev,
 352		struct device_attribute *attr,
 353		const char *buf,
 354		size_t count)
 355{
 356	struct i2c_client *client = to_i2c_client(dev);
 357	struct amc6821_data *data = i2c_get_clientdata(client);
 358	long val;
 359	int config = strict_strtol(buf, 10, &val);
 360	if (config)
 361		return config;
 362
 363	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
 364	if (config < 0) {
 365			dev_err(&client->dev,
 366			"Error reading configuration register, aborting.\n");
 367			return -EIO;
 368	}
 369
 370	switch (val) {
 371	case 1:
 372		config &= ~AMC6821_CONF1_FDRC0;
 373		config &= ~AMC6821_CONF1_FDRC1;
 374		break;
 375	case 2:
 376		config &= ~AMC6821_CONF1_FDRC0;
 377		config |= AMC6821_CONF1_FDRC1;
 378		break;
 379	case 3:
 380		config |= AMC6821_CONF1_FDRC0;
 381		config |= AMC6821_CONF1_FDRC1;
 382		break;
 383	default:
 384		return -EINVAL;
 385	}
 386	mutex_lock(&data->update_lock);
 387	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
 388			dev_err(&client->dev,
 389			"Configuration register write error, aborting.\n");
 390			count = -EIO;
 391	}
 392	mutex_unlock(&data->update_lock);
 393	return count;
 394}
 395
 396
 397static ssize_t get_pwm1_auto_channels_temp(
 398		struct device *dev,
 399		struct device_attribute *devattr,
 400		char *buf)
 401{
 402	struct amc6821_data *data = amc6821_update_device(dev);
 403	return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
 404}
 405
 406
 407static ssize_t get_temp_auto_point_temp(
 408		struct device *dev,
 409		struct device_attribute *devattr,
 410		char *buf)
 411{
 412	int ix = to_sensor_dev_attr_2(devattr)->index;
 413	int nr = to_sensor_dev_attr_2(devattr)->nr;
 414	struct amc6821_data *data = amc6821_update_device(dev);
 415	switch (nr) {
 416	case 1:
 417		return sprintf(buf, "%d\n",
 418			data->temp1_auto_point_temp[ix] * 1000);
 419		break;
 420	case 2:
 421		return sprintf(buf, "%d\n",
 422			data->temp2_auto_point_temp[ix] * 1000);
 423		break;
 424	default:
 425		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
 426		return -EINVAL;
 427	}
 428}
 429
 430
 431static ssize_t get_pwm1_auto_point_pwm(
 432		struct device *dev,
 433		struct device_attribute *devattr,
 434		char *buf)
 435{
 436	int ix = to_sensor_dev_attr(devattr)->index;
 437	struct amc6821_data *data = amc6821_update_device(dev);
 438	return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
 439}
 440
 441
 442static inline ssize_t set_slope_register(struct i2c_client *client,
 443		u8 reg,
 444		u8 dpwm,
 445		u8 *ptemp)
 446{
 447	int dt;
 448	u8 tmp;
 449
 450	dt = ptemp[2]-ptemp[1];
 451	for (tmp = 4; tmp > 0; tmp--) {
 452		if (dt * (0x20 >> tmp) >= dpwm)
 453			break;
 454	}
 455	tmp |= (ptemp[1] & 0x7C) << 1;
 456	if (i2c_smbus_write_byte_data(client,
 457			reg, tmp)) {
 458		dev_err(&client->dev, "Register write error, aborting.\n");
 459		return -EIO;
 460	}
 461	return 0;
 462}
 463
 464
 465
 466static ssize_t set_temp_auto_point_temp(
 467		struct device *dev,
 468		struct device_attribute *attr,
 469		const char *buf,
 470		size_t count)
 471{
 472	struct i2c_client *client = to_i2c_client(dev);
 473	struct amc6821_data *data = amc6821_update_device(dev);
 474	int ix = to_sensor_dev_attr_2(attr)->index;
 475	int nr = to_sensor_dev_attr_2(attr)->nr;
 476	u8 *ptemp;
 477	u8 reg;
 478	int dpwm;
 479	long val;
 480	int ret = strict_strtol(buf, 10, &val);
 481	if (ret)
 482		return ret;
 483
 484	switch (nr) {
 485	case 1:
 486		ptemp = data->temp1_auto_point_temp;
 487		reg = AMC6821_REG_LTEMP_FAN_CTRL;
 488		break;
 489	case 2:
 490		ptemp = data->temp2_auto_point_temp;
 491		reg = AMC6821_REG_RTEMP_FAN_CTRL;
 492		break;
 493	default:
 494		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
 495		return -EINVAL;
 496	}
 497
 498	data->valid = 0;
 499	mutex_lock(&data->update_lock);
 500	switch (ix) {
 501	case 0:
 502		ptemp[0] = SENSORS_LIMIT(val / 1000, 0,
 503				data->temp1_auto_point_temp[1]);
 504		ptemp[0] = SENSORS_LIMIT(ptemp[0], 0,
 505				data->temp2_auto_point_temp[1]);
 506		ptemp[0] = SENSORS_LIMIT(ptemp[0], 0, 63);
 507		if (i2c_smbus_write_byte_data(
 508					client,
 509					AMC6821_REG_PSV_TEMP,
 510					ptemp[0])) {
 511				dev_err(&client->dev,
 512					"Register write error, aborting.\n");
 513				count = -EIO;
 514		}
 515		goto EXIT;
 516		break;
 517	case 1:
 518		ptemp[1] = SENSORS_LIMIT(
 519					val / 1000,
 520					(ptemp[0] & 0x7C) + 4,
 521					124);
 522		ptemp[1] &= 0x7C;
 523		ptemp[2] = SENSORS_LIMIT(
 524					ptemp[2], ptemp[1] + 1,
 525					255);
 526		break;
 527	case 2:
 528		ptemp[2] = SENSORS_LIMIT(
 529					val / 1000,
 530					ptemp[1]+1,
 531					255);
 532		break;
 533	default:
 534		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
 535		count = -EINVAL;
 536		goto EXIT;
 537	}
 538	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
 539	if (set_slope_register(client, reg, dpwm, ptemp))
 540		count = -EIO;
 541
 542EXIT:
 543	mutex_unlock(&data->update_lock);
 544	return count;
 545}
 546
 547
 548
 549static ssize_t set_pwm1_auto_point_pwm(
 550		struct device *dev,
 551		struct device_attribute *attr,
 552		const char *buf,
 553		size_t count)
 554{
 555	struct i2c_client *client = to_i2c_client(dev);
 556	struct amc6821_data *data = i2c_get_clientdata(client);
 557	int dpwm;
 558	long val;
 559	int ret = strict_strtol(buf, 10, &val);
 560	if (ret)
 561		return ret;
 562
 563	mutex_lock(&data->update_lock);
 564	data->pwm1_auto_point_pwm[1] = SENSORS_LIMIT(val, 0, 254);
 565	if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
 566			data->pwm1_auto_point_pwm[1])) {
 567		dev_err(&client->dev, "Register write error, aborting.\n");
 568		count = -EIO;
 569		goto EXIT;
 570	}
 571	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
 572	if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
 573			data->temp1_auto_point_temp)) {
 574		count = -EIO;
 575		goto EXIT;
 576	}
 577	if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
 578			data->temp2_auto_point_temp)) {
 579		count = -EIO;
 580		goto EXIT;
 581	}
 582
 583EXIT:
 584	data->valid = 0;
 585	mutex_unlock(&data->update_lock);
 586	return count;
 587}
 588
 589static ssize_t get_fan(
 590		struct device *dev,
 591		struct device_attribute *devattr,
 592		char *buf)
 593{
 594	struct amc6821_data *data = amc6821_update_device(dev);
 595	int ix = to_sensor_dev_attr(devattr)->index;
 596	if (0 == data->fan[ix])
 597		return sprintf(buf, "0");
 598	return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
 599}
 600
 601
 602
 603static ssize_t get_fan1_fault(
 604		struct device *dev,
 605		struct device_attribute *devattr,
 606		char *buf)
 607{
 608	struct amc6821_data *data = amc6821_update_device(dev);
 609	if (data->stat1 & AMC6821_STAT1_FANS)
 610		return sprintf(buf, "1");
 611	else
 612		return sprintf(buf, "0");
 613}
 614
 615
 616
 617static ssize_t set_fan(
 618		struct device *dev,
 619		struct device_attribute *attr,
 620		const char *buf, size_t count)
 621{
 622	struct i2c_client *client = to_i2c_client(dev);
 623	struct amc6821_data *data = i2c_get_clientdata(client);
 624	long val;
 625	int ix = to_sensor_dev_attr(attr)->index;
 626	int ret = strict_strtol(buf, 10, &val);
 627	if (ret)
 628		return ret;
 629	val = 1 > val ? 0xFFFF : 6000000/val;
 630
 631	mutex_lock(&data->update_lock);
 632	data->fan[ix] = (u16) SENSORS_LIMIT(val, 1, 0xFFFF);
 633	if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
 634			data->fan[ix] & 0xFF)) {
 635		dev_err(&client->dev, "Register write error, aborting.\n");
 636		count = -EIO;
 637		goto EXIT;
 638	}
 639	if (i2c_smbus_write_byte_data(client,
 640			fan_reg_hi[ix], data->fan[ix] >> 8)) {
 641		dev_err(&client->dev, "Register write error, aborting.\n");
 642		count = -EIO;
 643	}
 644EXIT:
 645	mutex_unlock(&data->update_lock);
 646	return count;
 647}
 648
 649
 650
 651static ssize_t get_fan1_div(
 652		struct device *dev,
 653		struct device_attribute *devattr,
 654		char *buf)
 655{
 656	struct amc6821_data *data = amc6821_update_device(dev);
 657	return sprintf(buf, "%d\n", data->fan1_div);
 658}
 659
 660static ssize_t set_fan1_div(
 661		struct device *dev,
 662		struct device_attribute *attr,
 663		const char *buf, size_t count)
 664{
 665	struct i2c_client *client = to_i2c_client(dev);
 666	struct amc6821_data *data = i2c_get_clientdata(client);
 667	long val;
 668	int config = strict_strtol(buf, 10, &val);
 669	if (config)
 670		return config;
 671
 672	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
 673	if (config < 0) {
 674		dev_err(&client->dev,
 675			"Error reading configuration register, aborting.\n");
 676		return -EIO;
 677	}
 678	mutex_lock(&data->update_lock);
 679	switch (val) {
 680	case 2:
 681		config &= ~AMC6821_CONF4_PSPR;
 682		data->fan1_div = 2;
 683		break;
 684	case 4:
 685		config |= AMC6821_CONF4_PSPR;
 686		data->fan1_div = 4;
 687		break;
 688	default:
 689		count = -EINVAL;
 690		goto EXIT;
 691	}
 692	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
 693		dev_err(&client->dev,
 694			"Configuration register write error, aborting.\n");
 695		count = -EIO;
 696	}
 697EXIT:
 698	mutex_unlock(&data->update_lock);
 699	return count;
 700}
 701
 702
 703
 704static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
 705	get_temp, NULL, IDX_TEMP1_INPUT);
 706static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, get_temp,
 707	set_temp, IDX_TEMP1_MIN);
 708static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, get_temp,
 709	set_temp, IDX_TEMP1_MAX);
 710static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, get_temp,
 711	set_temp, IDX_TEMP1_CRIT);
 712static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
 713	get_temp_alarm, NULL, IDX_TEMP1_MIN);
 714static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
 715	get_temp_alarm, NULL, IDX_TEMP1_MAX);
 716static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
 717	get_temp_alarm, NULL, IDX_TEMP1_CRIT);
 718static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO | S_IWUSR,
 719	get_temp, NULL, IDX_TEMP2_INPUT);
 720static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, get_temp,
 721	set_temp, IDX_TEMP2_MIN);
 722static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, get_temp,
 723	set_temp, IDX_TEMP2_MAX);
 724static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, get_temp,
 725	set_temp, IDX_TEMP2_CRIT);
 726static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO,
 727	get_temp2_fault, NULL, 0);
 728static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
 729	get_temp_alarm, NULL, IDX_TEMP2_MIN);
 730static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
 731	get_temp_alarm, NULL, IDX_TEMP2_MAX);
 732static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
 733	get_temp_alarm, NULL, IDX_TEMP2_CRIT);
 734static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, IDX_FAN1_INPUT);
 735static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
 736	get_fan, set_fan, IDX_FAN1_MIN);
 737static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO | S_IWUSR,
 738	get_fan, set_fan, IDX_FAN1_MAX);
 739static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan1_fault, NULL, 0);
 740static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
 741	get_fan1_div, set_fan1_div, 0);
 742
 743static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm1, set_pwm1, 0);
 744static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
 745	get_pwm1_enable, set_pwm1_enable, 0);
 746static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO,
 747	get_pwm1_auto_point_pwm, NULL, 0);
 748static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
 749	get_pwm1_auto_point_pwm, set_pwm1_auto_point_pwm, 1);
 750static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO,
 751	get_pwm1_auto_point_pwm, NULL, 2);
 752static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
 753	get_pwm1_auto_channels_temp, NULL, 0);
 754static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO,
 755	get_temp_auto_point_temp, NULL, 1, 0);
 756static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
 757	get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 1);
 758static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp, S_IWUSR | S_IRUGO,
 759	get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 2);
 760
 761static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
 762	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 0);
 763static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
 764	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 1);
 765static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp, S_IWUSR | S_IRUGO,
 766	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 2);
 767
 768
 769
 770static struct attribute *amc6821_attrs[] = {
 771	&sensor_dev_attr_temp1_input.dev_attr.attr,
 772	&sensor_dev_attr_temp1_min.dev_attr.attr,
 773	&sensor_dev_attr_temp1_max.dev_attr.attr,
 774	&sensor_dev_attr_temp1_crit.dev_attr.attr,
 775	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
 776	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
 777	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
 778	&sensor_dev_attr_temp2_input.dev_attr.attr,
 779	&sensor_dev_attr_temp2_min.dev_attr.attr,
 780	&sensor_dev_attr_temp2_max.dev_attr.attr,
 781	&sensor_dev_attr_temp2_crit.dev_attr.attr,
 782	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
 783	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
 784	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
 785	&sensor_dev_attr_temp2_fault.dev_attr.attr,
 786	&sensor_dev_attr_fan1_input.dev_attr.attr,
 787	&sensor_dev_attr_fan1_min.dev_attr.attr,
 788	&sensor_dev_attr_fan1_max.dev_attr.attr,
 789	&sensor_dev_attr_fan1_fault.dev_attr.attr,
 790	&sensor_dev_attr_fan1_div.dev_attr.attr,
 791	&sensor_dev_attr_pwm1.dev_attr.attr,
 792	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
 793	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
 794	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
 795	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
 796	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
 797	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
 798	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
 799	&sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
 800	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
 801	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
 802	&sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
 803	NULL
 804};
 805
 806static struct attribute_group amc6821_attr_grp = {
 807	.attrs = amc6821_attrs,
 808};
 809
 810
 811
 812/* Return 0 if detection is successful, -ENODEV otherwise */
 813static int amc6821_detect(
 814		struct i2c_client *client,
 815		struct i2c_board_info *info)
 816{
 817	struct i2c_adapter *adapter = client->adapter;
 818	int address = client->addr;
 819	int dev_id, comp_id;
 820
 821	dev_dbg(&adapter->dev, "amc6821_detect called.\n");
 822
 823	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 824		dev_dbg(&adapter->dev,
 825			"amc6821: I2C bus doesn't support byte mode, "
 826			"skipping.\n");
 827		return -ENODEV;
 828	}
 829
 830	dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
 831	comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
 832	if (dev_id != 0x21 || comp_id != 0x49) {
 833		dev_dbg(&adapter->dev,
 834			"amc6821: detection failed at 0x%02x.\n",
 835			address);
 836		return -ENODEV;
 837	}
 838
 839	/* Bit 7 of the address register is ignored, so we can check the
 840	   ID registers again */
 
 
 841	dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
 842	comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
 843	if (dev_id != 0x21 || comp_id != 0x49) {
 844		dev_dbg(&adapter->dev,
 845			"amc6821: detection failed at 0x%02x.\n",
 846			address);
 847		return -ENODEV;
 848	}
 849
 850	dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
 851	strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
 852
 853	return 0;
 854}
 855
 856static int amc6821_probe(
 857	struct i2c_client *client,
 858	const struct i2c_device_id *id)
 859{
 860	struct amc6821_data *data;
 861	int err;
 862
 863	data = kzalloc(sizeof(struct amc6821_data), GFP_KERNEL);
 864	if (!data) {
 865		dev_err(&client->dev, "out of memory.\n");
 866		return -ENOMEM;
 867	}
 868
 869
 870	i2c_set_clientdata(client, data);
 871	mutex_init(&data->update_lock);
 872
 873	/*
 874	 * Initialize the amc6821 chip
 875	 */
 876	err = amc6821_init_client(client);
 877	if (err)
 878		goto err_free;
 879
 880	err = sysfs_create_group(&client->dev.kobj, &amc6821_attr_grp);
 881	if (err)
 882		goto err_free;
 883
 884	data->hwmon_dev = hwmon_device_register(&client->dev);
 885	if (!IS_ERR(data->hwmon_dev))
 886		return 0;
 887
 888	err = PTR_ERR(data->hwmon_dev);
 889	dev_err(&client->dev, "error registering hwmon device.\n");
 890	sysfs_remove_group(&client->dev.kobj, &amc6821_attr_grp);
 891err_free:
 892	kfree(data);
 893	return err;
 894}
 895
 896static int amc6821_remove(struct i2c_client *client)
 897{
 898	struct amc6821_data *data = i2c_get_clientdata(client);
 899
 900	hwmon_device_unregister(data->hwmon_dev);
 901	sysfs_remove_group(&client->dev.kobj, &amc6821_attr_grp);
 902
 903	kfree(data);
 904
 905	return 0;
 906}
 907
 908
 909static int amc6821_init_client(struct i2c_client *client)
 910{
 911	int config;
 912	int err = -EIO;
 913
 914	if (init) {
 915		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
 916
 917		if (config < 0) {
 918				dev_err(&client->dev,
 919			"Error reading configuration register, aborting.\n");
 920				return err;
 921		}
 922
 923		config |= AMC6821_CONF4_MODE;
 924
 925		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
 926				config)) {
 927			dev_err(&client->dev,
 928			"Configuration register write error, aborting.\n");
 929			return err;
 930		}
 931
 932		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
 933
 934		if (config < 0) {
 935			dev_err(&client->dev,
 936			"Error reading configuration register, aborting.\n");
 937			return err;
 938		}
 939
 940		dev_info(&client->dev, "Revision %d\n", config & 0x0f);
 941
 942		config &= ~AMC6821_CONF3_THERM_FAN_EN;
 943
 944		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
 945				config)) {
 946			dev_err(&client->dev,
 947			"Configuration register write error, aborting.\n");
 948			return err;
 949		}
 950
 951		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
 952
 953		if (config < 0) {
 954			dev_err(&client->dev,
 955			"Error reading configuration register, aborting.\n");
 956			return err;
 957		}
 958
 959		config &= ~AMC6821_CONF2_RTFIE;
 960		config &= ~AMC6821_CONF2_LTOIE;
 961		config &= ~AMC6821_CONF2_RTOIE;
 962		if (i2c_smbus_write_byte_data(client,
 963				AMC6821_REG_CONF2, config)) {
 964			dev_err(&client->dev,
 965			"Configuration register write error, aborting.\n");
 966			return err;
 967		}
 968
 969		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
 970
 971		if (config < 0) {
 972			dev_err(&client->dev,
 973			"Error reading configuration register, aborting.\n");
 974			return err;
 975		}
 976
 977		config &= ~AMC6821_CONF1_THERMOVIE;
 978		config &= ~AMC6821_CONF1_FANIE;
 979		config |= AMC6821_CONF1_START;
 980		if (pwminv)
 981			config |= AMC6821_CONF1_PWMINV;
 982		else
 983			config &= ~AMC6821_CONF1_PWMINV;
 984
 985		if (i2c_smbus_write_byte_data(
 986				client, AMC6821_REG_CONF1, config)) {
 987			dev_err(&client->dev,
 988			"Configuration register write error, aborting.\n");
 989			return err;
 990		}
 991	}
 992	return 0;
 993}
 994
 995
 996static struct amc6821_data *amc6821_update_device(struct device *dev)
 997{
 998	struct i2c_client *client = to_i2c_client(dev);
 999	struct amc6821_data *data = i2c_get_clientdata(client);
1000	int timeout = HZ;
1001	u8 reg;
1002	int i;
1003
1004	mutex_lock(&data->update_lock);
1005
1006	if (time_after(jiffies, data->last_updated + timeout) ||
1007			!data->valid) {
1008
1009		for (i = 0; i < TEMP_IDX_LEN; i++)
1010			data->temp[i] = i2c_smbus_read_byte_data(client,
1011				temp_reg[i]);
1012
1013		data->stat1 = i2c_smbus_read_byte_data(client,
1014			AMC6821_REG_STAT1);
1015		data->stat2 = i2c_smbus_read_byte_data(client,
1016			AMC6821_REG_STAT2);
1017
1018		data->pwm1 = i2c_smbus_read_byte_data(client,
1019			AMC6821_REG_DCY);
1020		for (i = 0; i < FAN1_IDX_LEN; i++) {
1021			data->fan[i] = i2c_smbus_read_byte_data(
1022					client,
1023					fan_reg_low[i]);
1024			data->fan[i] += i2c_smbus_read_byte_data(
1025					client,
1026					fan_reg_hi[i]) << 8;
1027		}
1028		data->fan1_div = i2c_smbus_read_byte_data(client,
1029			AMC6821_REG_CONF4);
1030		data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
1031
1032		data->pwm1_auto_point_pwm[0] = 0;
1033		data->pwm1_auto_point_pwm[2] = 255;
1034		data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
1035			AMC6821_REG_DCY_LOW_TEMP);
1036
1037		data->temp1_auto_point_temp[0] =
1038			i2c_smbus_read_byte_data(client,
1039					AMC6821_REG_PSV_TEMP);
1040		data->temp2_auto_point_temp[0] =
1041				data->temp1_auto_point_temp[0];
1042		reg = i2c_smbus_read_byte_data(client,
1043			AMC6821_REG_LTEMP_FAN_CTRL);
1044		data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
1045		reg &= 0x07;
1046		reg = 0x20 >> reg;
1047		if (reg > 0)
1048			data->temp1_auto_point_temp[2] =
1049				data->temp1_auto_point_temp[1] +
1050				(data->pwm1_auto_point_pwm[2] -
1051				data->pwm1_auto_point_pwm[1]) / reg;
1052		else
1053			data->temp1_auto_point_temp[2] = 255;
1054
1055		reg = i2c_smbus_read_byte_data(client,
1056			AMC6821_REG_RTEMP_FAN_CTRL);
1057		data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
1058		reg &= 0x07;
1059		reg = 0x20 >> reg;
1060		if (reg > 0)
1061			data->temp2_auto_point_temp[2] =
1062				data->temp2_auto_point_temp[1] +
1063				(data->pwm1_auto_point_pwm[2] -
1064				data->pwm1_auto_point_pwm[1]) / reg;
1065		else
1066			data->temp2_auto_point_temp[2] = 255;
1067
1068		reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
1069		reg = (reg >> 5) & 0x3;
1070		switch (reg) {
1071		case 0: /*open loop: software sets pwm1*/
1072			data->pwm1_auto_channels_temp = 0;
1073			data->pwm1_enable = 1;
1074			break;
1075		case 2: /*closed loop: remote T (temp2)*/
1076			data->pwm1_auto_channels_temp = 2;
1077			data->pwm1_enable = 2;
1078			break;
1079		case 3: /*closed loop: local and remote T (temp2)*/
1080			data->pwm1_auto_channels_temp = 3;
1081			data->pwm1_enable = 3;
1082			break;
1083		case 1: /*semi-open loop: software sets rpm, chip controls pwm1,
1084			  *currently not implemented
1085			  */
 
1086			data->pwm1_auto_channels_temp = 0;
1087			data->pwm1_enable = 0;
1088			break;
1089		}
1090
1091		data->last_updated = jiffies;
1092		data->valid = 1;
1093	}
1094	mutex_unlock(&data->update_lock);
1095	return data;
1096}
1097
1098
1099static int __init amc6821_init(void)
1100{
1101	return i2c_add_driver(&amc6821_driver);
1102}
1103
1104static void __exit amc6821_exit(void)
1105{
1106	i2c_del_driver(&amc6821_driver);
1107}
1108
1109module_init(amc6821_init);
1110module_exit(amc6821_exit);
1111
1112
1113MODULE_LICENSE("GPL");
1114MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
1115MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");
v3.15
   1/*
   2 * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
   3 *	       monitoring
   4 * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
   5 *
   6 * Based on max6650.c:
   7 * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22 */
  23
  24
  25#include <linux/kernel.h>	/* Needed for KERN_INFO */
  26#include <linux/module.h>
  27#include <linux/init.h>
  28#include <linux/slab.h>
  29#include <linux/jiffies.h>
  30#include <linux/i2c.h>
  31#include <linux/hwmon.h>
  32#include <linux/hwmon-sysfs.h>
  33#include <linux/err.h>
  34#include <linux/mutex.h>
  35
  36
  37/*
  38 * Addresses to scan.
  39 */
  40
  41static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
  42	0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
  43
  44
  45
  46/*
  47 * Insmod parameters
  48 */
  49
  50static int pwminv;	/*Inverted PWM output. */
  51module_param(pwminv, int, S_IRUGO);
  52
  53static int init = 1; /*Power-on initialization.*/
  54module_param(init, int, S_IRUGO);
  55
  56
  57enum chips { amc6821 };
  58
  59#define AMC6821_REG_DEV_ID 0x3D
  60#define AMC6821_REG_COMP_ID 0x3E
  61#define AMC6821_REG_CONF1 0x00
  62#define AMC6821_REG_CONF2 0x01
  63#define AMC6821_REG_CONF3 0x3F
  64#define AMC6821_REG_CONF4 0x04
  65#define AMC6821_REG_STAT1 0x02
  66#define AMC6821_REG_STAT2 0x03
  67#define AMC6821_REG_TDATA_LOW 0x08
  68#define AMC6821_REG_TDATA_HI 0x09
  69#define AMC6821_REG_LTEMP_HI 0x0A
  70#define AMC6821_REG_RTEMP_HI 0x0B
  71#define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
  72#define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
  73#define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
  74#define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
  75#define AMC6821_REG_LTEMP_CRIT 0x1B
  76#define AMC6821_REG_RTEMP_CRIT 0x1D
  77#define AMC6821_REG_PSV_TEMP 0x1C
  78#define AMC6821_REG_DCY 0x22
  79#define AMC6821_REG_LTEMP_FAN_CTRL 0x24
  80#define AMC6821_REG_RTEMP_FAN_CTRL 0x25
  81#define AMC6821_REG_DCY_LOW_TEMP 0x21
  82
  83#define AMC6821_REG_TACH_LLIMITL 0x10
  84#define AMC6821_REG_TACH_LLIMITH 0x11
  85#define AMC6821_REG_TACH_HLIMITL 0x12
  86#define AMC6821_REG_TACH_HLIMITH 0x13
  87
  88#define AMC6821_CONF1_START 0x01
  89#define AMC6821_CONF1_FAN_INT_EN 0x02
  90#define AMC6821_CONF1_FANIE 0x04
  91#define AMC6821_CONF1_PWMINV 0x08
  92#define AMC6821_CONF1_FAN_FAULT_EN 0x10
  93#define AMC6821_CONF1_FDRC0 0x20
  94#define AMC6821_CONF1_FDRC1 0x40
  95#define AMC6821_CONF1_THERMOVIE 0x80
  96
  97#define AMC6821_CONF2_PWM_EN 0x01
  98#define AMC6821_CONF2_TACH_MODE 0x02
  99#define AMC6821_CONF2_TACH_EN 0x04
 100#define AMC6821_CONF2_RTFIE 0x08
 101#define AMC6821_CONF2_LTOIE 0x10
 102#define AMC6821_CONF2_RTOIE 0x20
 103#define AMC6821_CONF2_PSVIE 0x40
 104#define AMC6821_CONF2_RST 0x80
 105
 106#define AMC6821_CONF3_THERM_FAN_EN 0x80
 107#define AMC6821_CONF3_REV_MASK 0x0F
 108
 109#define AMC6821_CONF4_OVREN 0x10
 110#define AMC6821_CONF4_TACH_FAST 0x20
 111#define AMC6821_CONF4_PSPR 0x40
 112#define AMC6821_CONF4_MODE 0x80
 113
 114#define AMC6821_STAT1_RPM_ALARM 0x01
 115#define AMC6821_STAT1_FANS 0x02
 116#define AMC6821_STAT1_RTH 0x04
 117#define AMC6821_STAT1_RTL 0x08
 118#define AMC6821_STAT1_R_THERM 0x10
 119#define AMC6821_STAT1_RTF 0x20
 120#define AMC6821_STAT1_LTH 0x40
 121#define AMC6821_STAT1_LTL 0x80
 122
 123#define AMC6821_STAT2_RTC 0x08
 124#define AMC6821_STAT2_LTC 0x10
 125#define AMC6821_STAT2_LPSV 0x20
 126#define AMC6821_STAT2_L_THERM 0x40
 127#define AMC6821_STAT2_THERM_IN 0x80
 128
 129enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
 130	IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
 131	IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
 132	TEMP_IDX_LEN, };
 133
 134static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
 135			AMC6821_REG_LTEMP_LIMIT_MIN,
 136			AMC6821_REG_LTEMP_LIMIT_MAX,
 137			AMC6821_REG_LTEMP_CRIT,
 138			AMC6821_REG_RTEMP_HI,
 139			AMC6821_REG_RTEMP_LIMIT_MIN,
 140			AMC6821_REG_RTEMP_LIMIT_MAX,
 141			AMC6821_REG_RTEMP_CRIT, };
 142
 143enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
 144	FAN1_IDX_LEN, };
 145
 146static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
 147			AMC6821_REG_TACH_LLIMITL,
 148			AMC6821_REG_TACH_HLIMITL, };
 149
 150
 151static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
 152			AMC6821_REG_TACH_LLIMITH,
 153			AMC6821_REG_TACH_HLIMITH, };
 154
 155static int amc6821_probe(
 156		struct i2c_client *client,
 157		const struct i2c_device_id *id);
 158static int amc6821_detect(
 159		struct i2c_client *client,
 160		struct i2c_board_info *info);
 161static int amc6821_init_client(struct i2c_client *client);
 162static int amc6821_remove(struct i2c_client *client);
 163static struct amc6821_data *amc6821_update_device(struct device *dev);
 164
 165/*
 166 * Driver data (common to all clients)
 167 */
 168
 169static const struct i2c_device_id amc6821_id[] = {
 170	{ "amc6821", amc6821 },
 171	{ }
 172};
 173
 174MODULE_DEVICE_TABLE(i2c, amc6821_id);
 175
 176static struct i2c_driver amc6821_driver = {
 177	.class = I2C_CLASS_HWMON,
 178	.driver = {
 179		.name	= "amc6821",
 180	},
 181	.probe = amc6821_probe,
 182	.remove = amc6821_remove,
 183	.id_table = amc6821_id,
 184	.detect = amc6821_detect,
 185	.address_list = normal_i2c,
 186};
 187
 188
 189/*
 190 * Client data (each client gets its own)
 191 */
 192
 193struct amc6821_data {
 194	struct device *hwmon_dev;
 195	struct mutex update_lock;
 196	char valid; /* zero until following fields are valid */
 197	unsigned long last_updated; /* in jiffies */
 198
 199	/* register values */
 200	int temp[TEMP_IDX_LEN];
 201
 202	u16 fan[FAN1_IDX_LEN];
 203	u8 fan1_div;
 204
 205	u8 pwm1;
 206	u8 temp1_auto_point_temp[3];
 207	u8 temp2_auto_point_temp[3];
 208	u8 pwm1_auto_point_pwm[3];
 209	u8 pwm1_enable;
 210	u8 pwm1_auto_channels_temp;
 211
 212	u8 stat1;
 213	u8 stat2;
 214};
 215
 216
 217static ssize_t get_temp(
 218		struct device *dev,
 219		struct device_attribute *devattr,
 220		char *buf)
 221{
 222	struct amc6821_data *data = amc6821_update_device(dev);
 223	int ix = to_sensor_dev_attr(devattr)->index;
 224
 225	return sprintf(buf, "%d\n", data->temp[ix] * 1000);
 226}
 227
 228
 229
 230static ssize_t set_temp(
 231		struct device *dev,
 232		struct device_attribute *attr,
 233		const char *buf,
 234		size_t count)
 235{
 236	struct i2c_client *client = to_i2c_client(dev);
 237	struct amc6821_data *data = i2c_get_clientdata(client);
 238	int ix = to_sensor_dev_attr(attr)->index;
 239	long val;
 240
 241	int ret = kstrtol(buf, 10, &val);
 242	if (ret)
 243		return ret;
 244	val = clamp_val(val / 1000, -128, 127);
 245
 246	mutex_lock(&data->update_lock);
 247	data->temp[ix] = val;
 248	if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
 249		dev_err(&client->dev, "Register write error, aborting.\n");
 250		count = -EIO;
 251	}
 252	mutex_unlock(&data->update_lock);
 253	return count;
 254}
 255
 256
 257
 258
 259static ssize_t get_temp_alarm(
 260	struct device *dev,
 261	struct device_attribute *devattr,
 262	char *buf)
 263{
 264	struct amc6821_data *data = amc6821_update_device(dev);
 265	int ix = to_sensor_dev_attr(devattr)->index;
 266	u8 flag;
 267
 268	switch (ix) {
 269	case IDX_TEMP1_MIN:
 270		flag = data->stat1 & AMC6821_STAT1_LTL;
 271		break;
 272	case IDX_TEMP1_MAX:
 273		flag = data->stat1 & AMC6821_STAT1_LTH;
 274		break;
 275	case IDX_TEMP1_CRIT:
 276		flag = data->stat2 & AMC6821_STAT2_LTC;
 277		break;
 278	case IDX_TEMP2_MIN:
 279		flag = data->stat1 & AMC6821_STAT1_RTL;
 280		break;
 281	case IDX_TEMP2_MAX:
 282		flag = data->stat1 & AMC6821_STAT1_RTH;
 283		break;
 284	case IDX_TEMP2_CRIT:
 285		flag = data->stat2 & AMC6821_STAT2_RTC;
 286		break;
 287	default:
 288		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
 289		return -EINVAL;
 290	}
 291	if (flag)
 292		return sprintf(buf, "1");
 293	else
 294		return sprintf(buf, "0");
 295}
 296
 297
 298
 299
 300static ssize_t get_temp2_fault(
 301		struct device *dev,
 302		struct device_attribute *devattr,
 303		char *buf)
 304{
 305	struct amc6821_data *data = amc6821_update_device(dev);
 306	if (data->stat1 & AMC6821_STAT1_RTF)
 307		return sprintf(buf, "1");
 308	else
 309		return sprintf(buf, "0");
 310}
 311
 312static ssize_t get_pwm1(
 313		struct device *dev,
 314		struct device_attribute *devattr,
 315		char *buf)
 316{
 317	struct amc6821_data *data = amc6821_update_device(dev);
 318	return sprintf(buf, "%d\n", data->pwm1);
 319}
 320
 321static ssize_t set_pwm1(
 322		struct device *dev,
 323		struct device_attribute *devattr,
 324		const char *buf,
 325		size_t count)
 326{
 327	struct i2c_client *client = to_i2c_client(dev);
 328	struct amc6821_data *data = i2c_get_clientdata(client);
 329	long val;
 330	int ret = kstrtol(buf, 10, &val);
 331	if (ret)
 332		return ret;
 333
 334	mutex_lock(&data->update_lock);
 335	data->pwm1 = clamp_val(val , 0, 255);
 336	i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
 337	mutex_unlock(&data->update_lock);
 338	return count;
 339}
 340
 341static ssize_t get_pwm1_enable(
 342		struct device *dev,
 343		struct device_attribute *devattr,
 344		char *buf)
 345{
 346	struct amc6821_data *data = amc6821_update_device(dev);
 347	return sprintf(buf, "%d\n", data->pwm1_enable);
 348}
 349
 350static ssize_t set_pwm1_enable(
 351		struct device *dev,
 352		struct device_attribute *attr,
 353		const char *buf,
 354		size_t count)
 355{
 356	struct i2c_client *client = to_i2c_client(dev);
 357	struct amc6821_data *data = i2c_get_clientdata(client);
 358	long val;
 359	int config = kstrtol(buf, 10, &val);
 360	if (config)
 361		return config;
 362
 363	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
 364	if (config < 0) {
 365			dev_err(&client->dev,
 366			"Error reading configuration register, aborting.\n");
 367			return config;
 368	}
 369
 370	switch (val) {
 371	case 1:
 372		config &= ~AMC6821_CONF1_FDRC0;
 373		config &= ~AMC6821_CONF1_FDRC1;
 374		break;
 375	case 2:
 376		config &= ~AMC6821_CONF1_FDRC0;
 377		config |= AMC6821_CONF1_FDRC1;
 378		break;
 379	case 3:
 380		config |= AMC6821_CONF1_FDRC0;
 381		config |= AMC6821_CONF1_FDRC1;
 382		break;
 383	default:
 384		return -EINVAL;
 385	}
 386	mutex_lock(&data->update_lock);
 387	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
 388			dev_err(&client->dev,
 389			"Configuration register write error, aborting.\n");
 390			count = -EIO;
 391	}
 392	mutex_unlock(&data->update_lock);
 393	return count;
 394}
 395
 396
 397static ssize_t get_pwm1_auto_channels_temp(
 398		struct device *dev,
 399		struct device_attribute *devattr,
 400		char *buf)
 401{
 402	struct amc6821_data *data = amc6821_update_device(dev);
 403	return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
 404}
 405
 406
 407static ssize_t get_temp_auto_point_temp(
 408		struct device *dev,
 409		struct device_attribute *devattr,
 410		char *buf)
 411{
 412	int ix = to_sensor_dev_attr_2(devattr)->index;
 413	int nr = to_sensor_dev_attr_2(devattr)->nr;
 414	struct amc6821_data *data = amc6821_update_device(dev);
 415	switch (nr) {
 416	case 1:
 417		return sprintf(buf, "%d\n",
 418			data->temp1_auto_point_temp[ix] * 1000);
 
 419	case 2:
 420		return sprintf(buf, "%d\n",
 421			data->temp2_auto_point_temp[ix] * 1000);
 
 422	default:
 423		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
 424		return -EINVAL;
 425	}
 426}
 427
 428
 429static ssize_t get_pwm1_auto_point_pwm(
 430		struct device *dev,
 431		struct device_attribute *devattr,
 432		char *buf)
 433{
 434	int ix = to_sensor_dev_attr(devattr)->index;
 435	struct amc6821_data *data = amc6821_update_device(dev);
 436	return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
 437}
 438
 439
 440static inline ssize_t set_slope_register(struct i2c_client *client,
 441		u8 reg,
 442		u8 dpwm,
 443		u8 *ptemp)
 444{
 445	int dt;
 446	u8 tmp;
 447
 448	dt = ptemp[2]-ptemp[1];
 449	for (tmp = 4; tmp > 0; tmp--) {
 450		if (dt * (0x20 >> tmp) >= dpwm)
 451			break;
 452	}
 453	tmp |= (ptemp[1] & 0x7C) << 1;
 454	if (i2c_smbus_write_byte_data(client,
 455			reg, tmp)) {
 456		dev_err(&client->dev, "Register write error, aborting.\n");
 457		return -EIO;
 458	}
 459	return 0;
 460}
 461
 462
 463
 464static ssize_t set_temp_auto_point_temp(
 465		struct device *dev,
 466		struct device_attribute *attr,
 467		const char *buf,
 468		size_t count)
 469{
 470	struct i2c_client *client = to_i2c_client(dev);
 471	struct amc6821_data *data = amc6821_update_device(dev);
 472	int ix = to_sensor_dev_attr_2(attr)->index;
 473	int nr = to_sensor_dev_attr_2(attr)->nr;
 474	u8 *ptemp;
 475	u8 reg;
 476	int dpwm;
 477	long val;
 478	int ret = kstrtol(buf, 10, &val);
 479	if (ret)
 480		return ret;
 481
 482	switch (nr) {
 483	case 1:
 484		ptemp = data->temp1_auto_point_temp;
 485		reg = AMC6821_REG_LTEMP_FAN_CTRL;
 486		break;
 487	case 2:
 488		ptemp = data->temp2_auto_point_temp;
 489		reg = AMC6821_REG_RTEMP_FAN_CTRL;
 490		break;
 491	default:
 492		dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
 493		return -EINVAL;
 494	}
 495
 496	data->valid = 0;
 497	mutex_lock(&data->update_lock);
 498	switch (ix) {
 499	case 0:
 500		ptemp[0] = clamp_val(val / 1000, 0,
 501				     data->temp1_auto_point_temp[1]);
 502		ptemp[0] = clamp_val(ptemp[0], 0,
 503				     data->temp2_auto_point_temp[1]);
 504		ptemp[0] = clamp_val(ptemp[0], 0, 63);
 505		if (i2c_smbus_write_byte_data(
 506					client,
 507					AMC6821_REG_PSV_TEMP,
 508					ptemp[0])) {
 509				dev_err(&client->dev,
 510					"Register write error, aborting.\n");
 511				count = -EIO;
 512		}
 513		goto EXIT;
 
 514	case 1:
 515		ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
 
 
 
 516		ptemp[1] &= 0x7C;
 517		ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
 
 
 518		break;
 519	case 2:
 520		ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
 
 
 
 521		break;
 522	default:
 523		dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
 524		count = -EINVAL;
 525		goto EXIT;
 526	}
 527	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
 528	if (set_slope_register(client, reg, dpwm, ptemp))
 529		count = -EIO;
 530
 531EXIT:
 532	mutex_unlock(&data->update_lock);
 533	return count;
 534}
 535
 536
 537
 538static ssize_t set_pwm1_auto_point_pwm(
 539		struct device *dev,
 540		struct device_attribute *attr,
 541		const char *buf,
 542		size_t count)
 543{
 544	struct i2c_client *client = to_i2c_client(dev);
 545	struct amc6821_data *data = i2c_get_clientdata(client);
 546	int dpwm;
 547	long val;
 548	int ret = kstrtol(buf, 10, &val);
 549	if (ret)
 550		return ret;
 551
 552	mutex_lock(&data->update_lock);
 553	data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
 554	if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
 555			data->pwm1_auto_point_pwm[1])) {
 556		dev_err(&client->dev, "Register write error, aborting.\n");
 557		count = -EIO;
 558		goto EXIT;
 559	}
 560	dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
 561	if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
 562			data->temp1_auto_point_temp)) {
 563		count = -EIO;
 564		goto EXIT;
 565	}
 566	if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
 567			data->temp2_auto_point_temp)) {
 568		count = -EIO;
 569		goto EXIT;
 570	}
 571
 572EXIT:
 573	data->valid = 0;
 574	mutex_unlock(&data->update_lock);
 575	return count;
 576}
 577
 578static ssize_t get_fan(
 579		struct device *dev,
 580		struct device_attribute *devattr,
 581		char *buf)
 582{
 583	struct amc6821_data *data = amc6821_update_device(dev);
 584	int ix = to_sensor_dev_attr(devattr)->index;
 585	if (0 == data->fan[ix])
 586		return sprintf(buf, "0");
 587	return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
 588}
 589
 590
 591
 592static ssize_t get_fan1_fault(
 593		struct device *dev,
 594		struct device_attribute *devattr,
 595		char *buf)
 596{
 597	struct amc6821_data *data = amc6821_update_device(dev);
 598	if (data->stat1 & AMC6821_STAT1_FANS)
 599		return sprintf(buf, "1");
 600	else
 601		return sprintf(buf, "0");
 602}
 603
 604
 605
 606static ssize_t set_fan(
 607		struct device *dev,
 608		struct device_attribute *attr,
 609		const char *buf, size_t count)
 610{
 611	struct i2c_client *client = to_i2c_client(dev);
 612	struct amc6821_data *data = i2c_get_clientdata(client);
 613	long val;
 614	int ix = to_sensor_dev_attr(attr)->index;
 615	int ret = kstrtol(buf, 10, &val);
 616	if (ret)
 617		return ret;
 618	val = 1 > val ? 0xFFFF : 6000000/val;
 619
 620	mutex_lock(&data->update_lock);
 621	data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
 622	if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
 623			data->fan[ix] & 0xFF)) {
 624		dev_err(&client->dev, "Register write error, aborting.\n");
 625		count = -EIO;
 626		goto EXIT;
 627	}
 628	if (i2c_smbus_write_byte_data(client,
 629			fan_reg_hi[ix], data->fan[ix] >> 8)) {
 630		dev_err(&client->dev, "Register write error, aborting.\n");
 631		count = -EIO;
 632	}
 633EXIT:
 634	mutex_unlock(&data->update_lock);
 635	return count;
 636}
 637
 638
 639
 640static ssize_t get_fan1_div(
 641		struct device *dev,
 642		struct device_attribute *devattr,
 643		char *buf)
 644{
 645	struct amc6821_data *data = amc6821_update_device(dev);
 646	return sprintf(buf, "%d\n", data->fan1_div);
 647}
 648
 649static ssize_t set_fan1_div(
 650		struct device *dev,
 651		struct device_attribute *attr,
 652		const char *buf, size_t count)
 653{
 654	struct i2c_client *client = to_i2c_client(dev);
 655	struct amc6821_data *data = i2c_get_clientdata(client);
 656	long val;
 657	int config = kstrtol(buf, 10, &val);
 658	if (config)
 659		return config;
 660
 661	config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
 662	if (config < 0) {
 663		dev_err(&client->dev,
 664			"Error reading configuration register, aborting.\n");
 665		return config;
 666	}
 667	mutex_lock(&data->update_lock);
 668	switch (val) {
 669	case 2:
 670		config &= ~AMC6821_CONF4_PSPR;
 671		data->fan1_div = 2;
 672		break;
 673	case 4:
 674		config |= AMC6821_CONF4_PSPR;
 675		data->fan1_div = 4;
 676		break;
 677	default:
 678		count = -EINVAL;
 679		goto EXIT;
 680	}
 681	if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
 682		dev_err(&client->dev,
 683			"Configuration register write error, aborting.\n");
 684		count = -EIO;
 685	}
 686EXIT:
 687	mutex_unlock(&data->update_lock);
 688	return count;
 689}
 690
 691
 692
 693static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
 694	get_temp, NULL, IDX_TEMP1_INPUT);
 695static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, get_temp,
 696	set_temp, IDX_TEMP1_MIN);
 697static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, get_temp,
 698	set_temp, IDX_TEMP1_MAX);
 699static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, get_temp,
 700	set_temp, IDX_TEMP1_CRIT);
 701static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
 702	get_temp_alarm, NULL, IDX_TEMP1_MIN);
 703static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
 704	get_temp_alarm, NULL, IDX_TEMP1_MAX);
 705static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
 706	get_temp_alarm, NULL, IDX_TEMP1_CRIT);
 707static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO | S_IWUSR,
 708	get_temp, NULL, IDX_TEMP2_INPUT);
 709static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, get_temp,
 710	set_temp, IDX_TEMP2_MIN);
 711static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, get_temp,
 712	set_temp, IDX_TEMP2_MAX);
 713static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, get_temp,
 714	set_temp, IDX_TEMP2_CRIT);
 715static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO,
 716	get_temp2_fault, NULL, 0);
 717static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
 718	get_temp_alarm, NULL, IDX_TEMP2_MIN);
 719static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
 720	get_temp_alarm, NULL, IDX_TEMP2_MAX);
 721static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
 722	get_temp_alarm, NULL, IDX_TEMP2_CRIT);
 723static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, IDX_FAN1_INPUT);
 724static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
 725	get_fan, set_fan, IDX_FAN1_MIN);
 726static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO | S_IWUSR,
 727	get_fan, set_fan, IDX_FAN1_MAX);
 728static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan1_fault, NULL, 0);
 729static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
 730	get_fan1_div, set_fan1_div, 0);
 731
 732static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm1, set_pwm1, 0);
 733static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
 734	get_pwm1_enable, set_pwm1_enable, 0);
 735static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO,
 736	get_pwm1_auto_point_pwm, NULL, 0);
 737static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
 738	get_pwm1_auto_point_pwm, set_pwm1_auto_point_pwm, 1);
 739static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO,
 740	get_pwm1_auto_point_pwm, NULL, 2);
 741static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
 742	get_pwm1_auto_channels_temp, NULL, 0);
 743static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO,
 744	get_temp_auto_point_temp, NULL, 1, 0);
 745static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
 746	get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 1);
 747static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp, S_IWUSR | S_IRUGO,
 748	get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 2);
 749
 750static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
 751	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 0);
 752static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
 753	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 1);
 754static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp, S_IWUSR | S_IRUGO,
 755	get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 2);
 756
 757
 758
 759static struct attribute *amc6821_attrs[] = {
 760	&sensor_dev_attr_temp1_input.dev_attr.attr,
 761	&sensor_dev_attr_temp1_min.dev_attr.attr,
 762	&sensor_dev_attr_temp1_max.dev_attr.attr,
 763	&sensor_dev_attr_temp1_crit.dev_attr.attr,
 764	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
 765	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
 766	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
 767	&sensor_dev_attr_temp2_input.dev_attr.attr,
 768	&sensor_dev_attr_temp2_min.dev_attr.attr,
 769	&sensor_dev_attr_temp2_max.dev_attr.attr,
 770	&sensor_dev_attr_temp2_crit.dev_attr.attr,
 771	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
 772	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
 773	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
 774	&sensor_dev_attr_temp2_fault.dev_attr.attr,
 775	&sensor_dev_attr_fan1_input.dev_attr.attr,
 776	&sensor_dev_attr_fan1_min.dev_attr.attr,
 777	&sensor_dev_attr_fan1_max.dev_attr.attr,
 778	&sensor_dev_attr_fan1_fault.dev_attr.attr,
 779	&sensor_dev_attr_fan1_div.dev_attr.attr,
 780	&sensor_dev_attr_pwm1.dev_attr.attr,
 781	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
 782	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
 783	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
 784	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
 785	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
 786	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
 787	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
 788	&sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
 789	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
 790	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
 791	&sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
 792	NULL
 793};
 794
 795static struct attribute_group amc6821_attr_grp = {
 796	.attrs = amc6821_attrs,
 797};
 798
 799
 800
 801/* Return 0 if detection is successful, -ENODEV otherwise */
 802static int amc6821_detect(
 803		struct i2c_client *client,
 804		struct i2c_board_info *info)
 805{
 806	struct i2c_adapter *adapter = client->adapter;
 807	int address = client->addr;
 808	int dev_id, comp_id;
 809
 810	dev_dbg(&adapter->dev, "amc6821_detect called.\n");
 811
 812	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 813		dev_dbg(&adapter->dev,
 814			"amc6821: I2C bus doesn't support byte mode, "
 815			"skipping.\n");
 816		return -ENODEV;
 817	}
 818
 819	dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
 820	comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
 821	if (dev_id != 0x21 || comp_id != 0x49) {
 822		dev_dbg(&adapter->dev,
 823			"amc6821: detection failed at 0x%02x.\n",
 824			address);
 825		return -ENODEV;
 826	}
 827
 828	/*
 829	 * Bit 7 of the address register is ignored, so we can check the
 830	 * ID registers again
 831	 */
 832	dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
 833	comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
 834	if (dev_id != 0x21 || comp_id != 0x49) {
 835		dev_dbg(&adapter->dev,
 836			"amc6821: detection failed at 0x%02x.\n",
 837			address);
 838		return -ENODEV;
 839	}
 840
 841	dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
 842	strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
 843
 844	return 0;
 845}
 846
 847static int amc6821_probe(
 848	struct i2c_client *client,
 849	const struct i2c_device_id *id)
 850{
 851	struct amc6821_data *data;
 852	int err;
 853
 854	data = devm_kzalloc(&client->dev, sizeof(struct amc6821_data),
 855			    GFP_KERNEL);
 856	if (!data)
 857		return -ENOMEM;
 
 
 858
 859	i2c_set_clientdata(client, data);
 860	mutex_init(&data->update_lock);
 861
 862	/*
 863	 * Initialize the amc6821 chip
 864	 */
 865	err = amc6821_init_client(client);
 866	if (err)
 867		return err;
 868
 869	err = sysfs_create_group(&client->dev.kobj, &amc6821_attr_grp);
 870	if (err)
 871		return err;
 872
 873	data->hwmon_dev = hwmon_device_register(&client->dev);
 874	if (!IS_ERR(data->hwmon_dev))
 875		return 0;
 876
 877	err = PTR_ERR(data->hwmon_dev);
 878	dev_err(&client->dev, "error registering hwmon device.\n");
 879	sysfs_remove_group(&client->dev.kobj, &amc6821_attr_grp);
 
 
 880	return err;
 881}
 882
 883static int amc6821_remove(struct i2c_client *client)
 884{
 885	struct amc6821_data *data = i2c_get_clientdata(client);
 886
 887	hwmon_device_unregister(data->hwmon_dev);
 888	sysfs_remove_group(&client->dev.kobj, &amc6821_attr_grp);
 889
 
 
 890	return 0;
 891}
 892
 893
 894static int amc6821_init_client(struct i2c_client *client)
 895{
 896	int config;
 897	int err = -EIO;
 898
 899	if (init) {
 900		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
 901
 902		if (config < 0) {
 903				dev_err(&client->dev,
 904			"Error reading configuration register, aborting.\n");
 905				return err;
 906		}
 907
 908		config |= AMC6821_CONF4_MODE;
 909
 910		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
 911				config)) {
 912			dev_err(&client->dev,
 913			"Configuration register write error, aborting.\n");
 914			return err;
 915		}
 916
 917		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
 918
 919		if (config < 0) {
 920			dev_err(&client->dev,
 921			"Error reading configuration register, aborting.\n");
 922			return err;
 923		}
 924
 925		dev_info(&client->dev, "Revision %d\n", config & 0x0f);
 926
 927		config &= ~AMC6821_CONF3_THERM_FAN_EN;
 928
 929		if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
 930				config)) {
 931			dev_err(&client->dev,
 932			"Configuration register write error, aborting.\n");
 933			return err;
 934		}
 935
 936		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
 937
 938		if (config < 0) {
 939			dev_err(&client->dev,
 940			"Error reading configuration register, aborting.\n");
 941			return err;
 942		}
 943
 944		config &= ~AMC6821_CONF2_RTFIE;
 945		config &= ~AMC6821_CONF2_LTOIE;
 946		config &= ~AMC6821_CONF2_RTOIE;
 947		if (i2c_smbus_write_byte_data(client,
 948				AMC6821_REG_CONF2, config)) {
 949			dev_err(&client->dev,
 950			"Configuration register write error, aborting.\n");
 951			return err;
 952		}
 953
 954		config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
 955
 956		if (config < 0) {
 957			dev_err(&client->dev,
 958			"Error reading configuration register, aborting.\n");
 959			return err;
 960		}
 961
 962		config &= ~AMC6821_CONF1_THERMOVIE;
 963		config &= ~AMC6821_CONF1_FANIE;
 964		config |= AMC6821_CONF1_START;
 965		if (pwminv)
 966			config |= AMC6821_CONF1_PWMINV;
 967		else
 968			config &= ~AMC6821_CONF1_PWMINV;
 969
 970		if (i2c_smbus_write_byte_data(
 971				client, AMC6821_REG_CONF1, config)) {
 972			dev_err(&client->dev,
 973			"Configuration register write error, aborting.\n");
 974			return err;
 975		}
 976	}
 977	return 0;
 978}
 979
 980
 981static struct amc6821_data *amc6821_update_device(struct device *dev)
 982{
 983	struct i2c_client *client = to_i2c_client(dev);
 984	struct amc6821_data *data = i2c_get_clientdata(client);
 985	int timeout = HZ;
 986	u8 reg;
 987	int i;
 988
 989	mutex_lock(&data->update_lock);
 990
 991	if (time_after(jiffies, data->last_updated + timeout) ||
 992			!data->valid) {
 993
 994		for (i = 0; i < TEMP_IDX_LEN; i++)
 995			data->temp[i] = i2c_smbus_read_byte_data(client,
 996				temp_reg[i]);
 997
 998		data->stat1 = i2c_smbus_read_byte_data(client,
 999			AMC6821_REG_STAT1);
1000		data->stat2 = i2c_smbus_read_byte_data(client,
1001			AMC6821_REG_STAT2);
1002
1003		data->pwm1 = i2c_smbus_read_byte_data(client,
1004			AMC6821_REG_DCY);
1005		for (i = 0; i < FAN1_IDX_LEN; i++) {
1006			data->fan[i] = i2c_smbus_read_byte_data(
1007					client,
1008					fan_reg_low[i]);
1009			data->fan[i] += i2c_smbus_read_byte_data(
1010					client,
1011					fan_reg_hi[i]) << 8;
1012		}
1013		data->fan1_div = i2c_smbus_read_byte_data(client,
1014			AMC6821_REG_CONF4);
1015		data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
1016
1017		data->pwm1_auto_point_pwm[0] = 0;
1018		data->pwm1_auto_point_pwm[2] = 255;
1019		data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
1020			AMC6821_REG_DCY_LOW_TEMP);
1021
1022		data->temp1_auto_point_temp[0] =
1023			i2c_smbus_read_byte_data(client,
1024					AMC6821_REG_PSV_TEMP);
1025		data->temp2_auto_point_temp[0] =
1026				data->temp1_auto_point_temp[0];
1027		reg = i2c_smbus_read_byte_data(client,
1028			AMC6821_REG_LTEMP_FAN_CTRL);
1029		data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
1030		reg &= 0x07;
1031		reg = 0x20 >> reg;
1032		if (reg > 0)
1033			data->temp1_auto_point_temp[2] =
1034				data->temp1_auto_point_temp[1] +
1035				(data->pwm1_auto_point_pwm[2] -
1036				data->pwm1_auto_point_pwm[1]) / reg;
1037		else
1038			data->temp1_auto_point_temp[2] = 255;
1039
1040		reg = i2c_smbus_read_byte_data(client,
1041			AMC6821_REG_RTEMP_FAN_CTRL);
1042		data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
1043		reg &= 0x07;
1044		reg = 0x20 >> reg;
1045		if (reg > 0)
1046			data->temp2_auto_point_temp[2] =
1047				data->temp2_auto_point_temp[1] +
1048				(data->pwm1_auto_point_pwm[2] -
1049				data->pwm1_auto_point_pwm[1]) / reg;
1050		else
1051			data->temp2_auto_point_temp[2] = 255;
1052
1053		reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
1054		reg = (reg >> 5) & 0x3;
1055		switch (reg) {
1056		case 0: /*open loop: software sets pwm1*/
1057			data->pwm1_auto_channels_temp = 0;
1058			data->pwm1_enable = 1;
1059			break;
1060		case 2: /*closed loop: remote T (temp2)*/
1061			data->pwm1_auto_channels_temp = 2;
1062			data->pwm1_enable = 2;
1063			break;
1064		case 3: /*closed loop: local and remote T (temp2)*/
1065			data->pwm1_auto_channels_temp = 3;
1066			data->pwm1_enable = 3;
1067			break;
1068		case 1: /*
1069			 * semi-open loop: software sets rpm, chip controls
1070			 * pwm1, currently not implemented
1071			 */
1072			data->pwm1_auto_channels_temp = 0;
1073			data->pwm1_enable = 0;
1074			break;
1075		}
1076
1077		data->last_updated = jiffies;
1078		data->valid = 1;
1079	}
1080	mutex_unlock(&data->update_lock);
1081	return data;
1082}
1083
1084module_i2c_driver(amc6821_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
1085
1086MODULE_LICENSE("GPL");
1087MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
1088MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");