/*
* A hwmon driver for the Analog Devices ADT7473
* Copyright (C) 2007 IBM
*
* Author: Darrick J. Wong <djwong@us.ibm.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/log2.h>
/* Addresses to scan */
static const unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END };
/* ADT7473 registers */
#define ADT7473_REG_BASE_ADDR 0x20
#define ADT7473_REG_VOLT_BASE_ADDR 0x21
#define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
#define ADT7473_REG_TEMP_BASE_ADDR 0x25
#define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
#define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
#define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
#define ADT7473_REG_FAN_BASE_ADDR 0x28
#define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
#define ADT7473_REG_PWM_BASE_ADDR 0x30
#define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
#define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
#define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
#define ADT7473_PWM_BHVR_MASK 0xE0
#define ADT7473_PWM_BHVR_SHIFT 5
#define ADT7473_REG_CFG1 0x40
#define ADT7473_CFG1_START 0x01
#define ADT7473_CFG1_READY 0x04
#define ADT7473_REG_CFG2 0x73
#define ADT7473_REG_CFG3 0x78
#define ADT7473_REG_CFG4 0x7D
#define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
#define ADT7473_REG_CFG5 0x7C
#define ADT7473_CFG5_TEMP_TWOS 0x01
#define ADT7473_CFG5_TEMP_OFFSET 0x02
#define ADT7473_REG_DEVICE 0x3D
#define ADT7473_VENDOR 0x41
#define ADT7473_REG_VENDOR 0x3E
#define ADT7473_DEVICE 0x73
#define ADT7473_REG_REVISION 0x3F
#define ADT7473_REV_68 0x68
#define ADT7473_REV_69 0x69
#define ADT7473_REG_ALARM1 0x41
#define ADT7473_VCCP_ALARM 0x02
#define ADT7473_VCC_ALARM 0x04
#define ADT7473_R1T_ALARM 0x10
#define ADT7473_LT_ALARM 0x20
#define ADT7473_R2T_ALARM 0x40
#define ADT7473_OOL 0x80
#define ADT7473_REG_ALARM2 0x42
#define ADT7473_OVT_ALARM 0x02
#define ADT7473_FAN1_ALARM 0x04
#define ADT7473_FAN2_ALARM 0x08
#define ADT7473_FAN3_ALARM 0x10
#define ADT7473_FAN4_ALARM 0x20
#define ADT7473_R1T_SHORT 0x40
#define ADT7473_R2T_SHORT 0x80
#define ALARM2(x) ((x) << 8)
#define ADT7473_VOLT_COUNT 2
#define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
#define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
#define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
((x) * 2) + 1)
#define ADT7473_TEMP_COUNT 3
#define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
#define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
#define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
((x) * 2) + 1)
#define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
#define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
#define ADT7473_FAN_COUNT 4
#define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
#define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
#define ADT7473_PWM_COUNT 3
#define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
#define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
#define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
#define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
/* How often do we reread sensors values? (In jiffies) */
#define SENSOR_REFRESH_INTERVAL (2 * HZ)
/* How often do we reread sensor limit values? (In jiffies) */
#define LIMIT_REFRESH_INTERVAL (60 * HZ)
/* datasheet says to divide this number by the fan reading to get fan rpm */
#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
#define FAN_PERIOD_INVALID 65535
#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
struct adt7473_data {
struct device *hwmon_dev;
struct attribute_group attrs;
struct mutex lock;
char sensors_valid;
char limits_valid;
unsigned long sensors_last_updated; /* In jiffies */
unsigned long limits_last_updated; /* In jiffies */
u8 volt[ADT7473_VOLT_COUNT];
s8 volt_min[ADT7473_VOLT_COUNT];
s8 volt_max[ADT7473_VOLT_COUNT];
s8 temp[ADT7473_TEMP_COUNT];
s8 temp_min[ADT7473_TEMP_COUNT];
s8 temp_max[ADT7473_TEMP_COUNT];
s8 temp_tmin[ADT7473_TEMP_COUNT];
/* This is called the !THERM limit in the datasheet */
s8 temp_tmax[ADT7473_TEMP_COUNT];
u16 fan[ADT7473_FAN_COUNT];
u16 fan_min[ADT7473_FAN_COUNT];
u8 pwm[ADT7473_PWM_COUNT];
u8 pwm_max[ADT7473_PWM_COUNT];
u8 pwm_min[ADT7473_PWM_COUNT];
u8 pwm_behavior[ADT7473_PWM_COUNT];
u8 temp_twos_complement;
u8 temp_offset;
u16 alarm;
u8 max_duty_at_overheat;
};
static int adt7473_probe(struct i2c_client *client,
const struct i2c_device_id *id);
static int adt7473_detect(struct i2c_client *client,
struct i2c_board_info *info);
static int adt7473_remove(struct i2c_client *client);
static const struct i2c_device_id adt7473_id[] = {
{ "adt7473", 0 },
{ }
};
static struct i2c_driver adt7473_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "adt7473"