2005-04-30 15:41:29 +04:00
/*
* adm9240 . c Part of lm_sensors , Linux kernel modules for hardware
2012-01-15 01:12:38 +04:00
* monitoring
2005-04-30 15:41:29 +04:00
*
* Copyright ( C ) 1999 Frodo Looijaard < frodol @ dds . nl >
* Philip Edelbrock < phil @ netroedge . com >
* Copyright ( C ) 2003 Michiel Rook < michiel @ grendelproject . nl >
2006-10-08 23:57:41 +04:00
* Copyright ( C ) 2005 Grant Coady < gcoady . lk @ gmail . com > with valuable
2012-01-15 01:12:38 +04:00
* guidance from Jean Delvare
2005-04-30 15:41:29 +04:00
*
* Driver supports Analog Devices ADM9240
* Dallas Semiconductor DS1780
* National Semiconductor LM81
*
* ADM9240 is the reference , DS1780 and LM81 are register compatibles
*
* Voltage Six inputs are scaled by chip , VID also reported
* Temperature Chip temperature to 0.5 ' C , maximum and max_hysteris
* Fans 2 fans , low speed alarm , automatic fan clock divider
* Alarms 16 - bit map of active alarms
* Analog Out 0. .1250 mV output
*
2011-01-12 23:55:09 +03:00
* Chassis Intrusion : clear CI latch with ' echo 0 > intrusion0_alarm '
2005-04-30 15:41:29 +04:00
*
* Test hardware : Intel SE440BX - 2 desktop motherboard - - Grant
*
* LM81 extended temp reading not implemented
*
* 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 . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include <linux/init.h>
# include <linux/module.h>
# include <linux/slab.h>
# include <linux/i2c.h>
2005-09-16 23:32:57 +04:00
# include <linux/hwmon-sysfs.h>
2005-07-16 05:39:18 +04:00
# include <linux/hwmon.h>
2005-07-31 23:52:01 +04:00
# include <linux/hwmon-vid.h>
2005-07-16 05:39:18 +04:00
# include <linux/err.h>
2006-01-19 01:19:26 +03:00
# include <linux/mutex.h>
2005-04-30 15:41:29 +04:00
/* Addresses to scan */
2008-02-18 06:28:03 +03:00
static const unsigned short normal_i2c [ ] = { 0x2c , 0x2d , 0x2e , 0x2f ,
2005-04-30 15:41:29 +04:00
I2C_CLIENT_END } ;
2009-12-14 23:17:27 +03:00
enum chips { adm9240 , ds1780 , lm81 } ;
2005-04-30 15:41:29 +04:00
/* ADM9240 registers */
# define ADM9240_REG_MAN_ID 0x3e
# define ADM9240_REG_DIE_REV 0x3f
# define ADM9240_REG_CONFIG 0x40
# define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
# define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
# define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
# define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
# define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
# define ADM9240_REG_INT(nr) (0x41 + (nr))
# define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
# define ADM9240_REG_TEMP 0x27
2005-09-16 23:32:57 +04:00
# define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
2005-04-30 15:41:29 +04:00
# define ADM9240_REG_ANALOG_OUT 0x19
# define ADM9240_REG_CHASSIS_CLEAR 0x46
# define ADM9240_REG_VID_FAN_DIV 0x47
# define ADM9240_REG_I2C_ADDR 0x48
# define ADM9240_REG_VID4 0x49
# define ADM9240_REG_TEMP_CONF 0x4b
/* generalised scaling with integer rounding */
static inline int SCALE ( long val , int mul , int div )
{
if ( val < 0 )
return ( val * mul - div / 2 ) / div ;
else
return ( val * mul + div / 2 ) / div ;
}
/* adm9240 internally scales voltage measurements */
static const u16 nom_mv [ ] = { 2500 , 2700 , 3300 , 5000 , 12000 , 2700 } ;
static inline unsigned int IN_FROM_REG ( u8 reg , int n )
{
return SCALE ( reg , nom_mv [ n ] , 192 ) ;
}
static inline u8 IN_TO_REG ( unsigned long val , int n )
{
return SENSORS_LIMIT ( SCALE ( val , 192 , nom_mv [ n ] ) , 0 , 255 ) ;
}
/* temperature range: -40..125, 127 disables temperature alarm */
static inline s8 TEMP_TO_REG ( long val )
{
return SENSORS_LIMIT ( SCALE ( val , 1 , 1000 ) , - 40 , 127 ) ;
}
/* two fans, each with low fan speed limit */
static inline unsigned int FAN_FROM_REG ( u8 reg , u8 div )
{
if ( ! reg ) /* error */
return - 1 ;
if ( reg = = 255 )
return 0 ;
return SCALE ( 1350000 , 1 , reg * div ) ;
}
/* analog out 0..1250mV */
static inline u8 AOUT_TO_REG ( unsigned long val )
{
return SENSORS_LIMIT ( SCALE ( val , 255 , 1250 ) , 0 , 255 ) ;
}
static inline unsigned int AOUT_FROM_REG ( u8 reg )
{
return SCALE ( reg , 1250 , 255 ) ;
}
2008-07-16 21:30:09 +04:00
static int adm9240_probe ( struct i2c_client * client ,
const struct i2c_device_id * id ) ;
2009-12-14 23:17:23 +03:00
static int adm9240_detect ( struct i2c_client * client ,
2008-07-16 21:30:09 +04:00
struct i2c_board_info * info ) ;
2005-04-30 15:41:29 +04:00
static void adm9240_init_client ( struct i2c_client * client ) ;
2008-07-16 21:30:09 +04:00
static int adm9240_remove ( struct i2c_client * client ) ;
2005-04-30 15:41:29 +04:00
static struct adm9240_data * adm9240_update_device ( struct device * dev ) ;
/* driver data */
2008-07-16 21:30:09 +04:00
static const struct i2c_device_id adm9240_id [ ] = {
{ " adm9240 " , adm9240 } ,
{ " ds1780 " , ds1780 } ,
{ " lm81 " , lm81 } ,
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , adm9240_id ) ;
2005-04-30 15:41:29 +04:00
static struct i2c_driver adm9240_driver = {
2008-07-16 21:30:09 +04:00
. class = I2C_CLASS_HWMON ,
2005-11-26 22:37:41 +03:00
. driver = {
. name = " adm9240 " ,
} ,
2008-07-16 21:30:09 +04:00
. probe = adm9240_probe ,
. remove = adm9240_remove ,
. id_table = adm9240_id ,
. detect = adm9240_detect ,
2009-12-14 23:17:25 +03:00
. address_list = normal_i2c ,
2005-04-30 15:41:29 +04:00
} ;
/* per client data */
struct adm9240_data {
2007-08-21 00:46:20 +04:00
struct device * hwmon_dev ;
2006-01-19 01:19:26 +03:00
struct mutex update_lock ;
2005-04-30 15:41:29 +04:00
char valid ;
unsigned long last_updated_measure ;
unsigned long last_updated_config ;
u8 in [ 6 ] ; /* ro in0_input */
u8 in_max [ 6 ] ; /* rw in0_max */
u8 in_min [ 6 ] ; /* rw in0_min */
u8 fan [ 2 ] ; /* ro fan1_input */
u8 fan_min [ 2 ] ; /* rw fan1_min */
u8 fan_div [ 2 ] ; /* rw fan1_div, read-only accessor */
s16 temp ; /* ro temp1_input, 9-bit sign-extended */
2005-09-16 23:32:57 +04:00
s8 temp_max [ 2 ] ; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
2005-04-30 15:41:29 +04:00
u16 alarms ; /* ro alarms */
2005-05-13 14:26:10 +04:00
u8 aout ; /* rw aout_output */
2005-04-30 15:41:29 +04:00
u8 vid ; /* ro vid */
u8 vrm ; /* -- vrm set on startup, no accessor */
} ;
/*** sysfs accessors ***/
/* temperature */
2005-09-16 23:32:57 +04:00
static ssize_t show_temp ( struct device * dev , struct device_attribute * dummy ,
char * buf )
{
struct adm9240_data * data = adm9240_update_device ( dev ) ;
return sprintf ( buf , " %d \n " , data - > temp * 500 ) ; /* 9-bit value */
}
static ssize_t show_max ( struct device * dev , struct device_attribute * devattr ,
char * buf )
{
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
struct adm9240_data * data = adm9240_update_device ( dev ) ;
return sprintf ( buf , " %d \n " , data - > temp_max [ attr - > index ] * 1000 ) ;
}
static ssize_t set_max ( struct device * dev , struct device_attribute * devattr ,
const char * buf , size_t count )
{
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2012-01-15 01:12:38 +04:00
long val ;
int err ;
err = kstrtol ( buf , 10 , & val ) ;
if ( err )
return err ;
2005-09-16 23:32:57 +04:00
2006-01-19 01:19:26 +03:00
mutex_lock ( & data - > update_lock ) ;
2005-09-16 23:32:57 +04:00
data - > temp_max [ attr - > index ] = TEMP_TO_REG ( val ) ;
i2c_smbus_write_byte_data ( client , ADM9240_REG_TEMP_MAX ( attr - > index ) ,
data - > temp_max [ attr - > index ] ) ;
2006-01-19 01:19:26 +03:00
mutex_unlock ( & data - > update_lock ) ;
2005-09-16 23:32:57 +04:00
return count ;
}
2005-04-30 15:41:29 +04:00
static DEVICE_ATTR ( temp1_input , S_IRUGO , show_temp , NULL ) ;
2005-09-16 23:32:57 +04:00
static SENSOR_DEVICE_ATTR ( temp1_max , S_IWUSR | S_IRUGO ,
show_max , set_max , 0 ) ;
static SENSOR_DEVICE_ATTR ( temp1_max_hyst , S_IWUSR | S_IRUGO ,
show_max , set_max , 1 ) ;
2005-04-30 15:41:29 +04:00
/* voltage */
2005-09-16 23:32:57 +04:00
static ssize_t show_in ( struct device * dev , struct device_attribute * devattr ,
char * buf )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct adm9240_data * data = adm9240_update_device ( dev ) ;
2005-09-16 23:32:57 +04:00
return sprintf ( buf , " %d \n " , IN_FROM_REG ( data - > in [ attr - > index ] ,
attr - > index ) ) ;
2005-04-30 15:41:29 +04:00
}
2005-09-25 18:14:18 +04:00
static ssize_t show_in_min ( struct device * dev ,
struct device_attribute * devattr , char * buf )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct adm9240_data * data = adm9240_update_device ( dev ) ;
2005-09-16 23:32:57 +04:00
return sprintf ( buf , " %d \n " , IN_FROM_REG ( data - > in_min [ attr - > index ] ,
attr - > index ) ) ;
2005-04-30 15:41:29 +04:00
}
2005-09-25 18:14:18 +04:00
static ssize_t show_in_max ( struct device * dev ,
struct device_attribute * devattr , char * buf )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct adm9240_data * data = adm9240_update_device ( dev ) ;
2005-09-16 23:32:57 +04:00
return sprintf ( buf , " %d \n " , IN_FROM_REG ( data - > in_max [ attr - > index ] ,
attr - > index ) ) ;
2005-04-30 15:41:29 +04:00
}
2005-09-25 18:14:18 +04:00
static ssize_t set_in_min ( struct device * dev ,
struct device_attribute * devattr ,
2005-09-16 23:32:57 +04:00
const char * buf , size_t count )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2012-01-15 01:12:38 +04:00
unsigned long val ;
int err ;
err = kstrtoul ( buf , 10 , & val ) ;
if ( err )
return err ;
2005-04-30 15:41:29 +04:00
2006-01-19 01:19:26 +03:00
mutex_lock ( & data - > update_lock ) ;
2005-09-16 23:32:57 +04:00
data - > in_min [ attr - > index ] = IN_TO_REG ( val , attr - > index ) ;
i2c_smbus_write_byte_data ( client , ADM9240_REG_IN_MIN ( attr - > index ) ,
data - > in_min [ attr - > index ] ) ;
2006-01-19 01:19:26 +03:00
mutex_unlock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
return count ;
}
2005-09-25 18:14:18 +04:00
static ssize_t set_in_max ( struct device * dev ,
struct device_attribute * devattr ,
2005-09-16 23:32:57 +04:00
const char * buf , size_t count )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2012-01-15 01:12:38 +04:00
unsigned long val ;
int err ;
err = kstrtoul ( buf , 10 , & val ) ;
if ( err )
return err ;
2005-04-30 15:41:29 +04:00
2006-01-19 01:19:26 +03:00
mutex_lock ( & data - > update_lock ) ;
2005-09-16 23:32:57 +04:00
data - > in_max [ attr - > index ] = IN_TO_REG ( val , attr - > index ) ;
i2c_smbus_write_byte_data ( client , ADM9240_REG_IN_MAX ( attr - > index ) ,
data - > in_max [ attr - > index ] ) ;
2006-01-19 01:19:26 +03:00
mutex_unlock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
return count ;
}
2005-09-16 23:32:57 +04:00
# define vin(nr) \
2012-01-15 01:12:38 +04:00
static SENSOR_DEVICE_ATTR ( in # # nr # # _input , S_IRUGO , \
2005-09-16 23:32:57 +04:00
show_in , NULL , nr ) ; \
static SENSOR_DEVICE_ATTR ( in # # nr # # _min , S_IRUGO | S_IWUSR , \
show_in_min , set_in_min , nr ) ; \
static SENSOR_DEVICE_ATTR ( in # # nr # # _max , S_IRUGO | S_IWUSR , \
show_in_max , set_in_max , nr ) ;
vin ( 0 ) ;
vin ( 1 ) ;
vin ( 2 ) ;
vin ( 3 ) ;
vin ( 4 ) ;
vin ( 5 ) ;
2005-04-30 15:41:29 +04:00
/* fans */
2005-09-16 23:32:57 +04:00
static ssize_t show_fan ( struct device * dev ,
struct device_attribute * devattr , char * buf )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct adm9240_data * data = adm9240_update_device ( dev ) ;
2005-09-16 23:32:57 +04:00
return sprintf ( buf , " %d \n " , FAN_FROM_REG ( data - > fan [ attr - > index ] ,
1 < < data - > fan_div [ attr - > index ] ) ) ;
2005-04-30 15:41:29 +04:00
}
2005-09-16 23:32:57 +04:00
static ssize_t show_fan_min ( struct device * dev ,
struct device_attribute * devattr , char * buf )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct adm9240_data * data = adm9240_update_device ( dev ) ;
2005-09-16 23:32:57 +04:00
return sprintf ( buf , " %d \n " , FAN_FROM_REG ( data - > fan_min [ attr - > index ] ,
1 < < data - > fan_div [ attr - > index ] ) ) ;
2005-04-30 15:41:29 +04:00
}
2005-09-16 23:32:57 +04:00
static ssize_t show_fan_div ( struct device * dev ,
struct device_attribute * devattr , char * buf )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct adm9240_data * data = adm9240_update_device ( dev ) ;
2005-09-16 23:32:57 +04:00
return sprintf ( buf , " %d \n " , 1 < < data - > fan_div [ attr - > index ] ) ;
2005-04-30 15:41:29 +04:00
}
/* write new fan div, callers must hold data->update_lock */
static void adm9240_write_fan_div ( struct i2c_client * client , int nr ,
u8 fan_div )
{
u8 reg , old , shift = ( nr + 2 ) * 2 ;
2005-09-16 23:32:55 +04:00
reg = i2c_smbus_read_byte_data ( client , ADM9240_REG_VID_FAN_DIV ) ;
2005-04-30 15:41:29 +04:00
old = ( reg > > shift ) & 3 ;
reg & = ~ ( 3 < < shift ) ;
reg | = ( fan_div < < shift ) ;
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client , ADM9240_REG_VID_FAN_DIV , reg ) ;
2005-04-30 15:41:29 +04:00
dev_dbg ( & client - > dev , " fan%d clock divider changed from %u "
" to %u \n " , nr + 1 , 1 < < old , 1 < < fan_div ) ;
}
2005-09-25 18:14:18 +04:00
/*
2005-04-30 15:41:29 +04:00
* set fan speed low limit :
*
* - value is zero : disable fan speed low limit alarm
*
* - value is below fan speed measurement range : enable fan speed low
* limit alarm to be asserted while fan speed too slow to measure
*
* - otherwise : select fan clock divider to suit fan speed low limit ,
* measurement code may adjust registers to ensure fan speed reading
*/
2005-09-16 23:32:57 +04:00
static ssize_t set_fan_min ( struct device * dev ,
struct device_attribute * devattr ,
const char * buf , size_t count )
2005-04-30 15:41:29 +04:00
{
2005-09-16 23:32:57 +04:00
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2005-04-30 15:41:29 +04:00
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2005-09-16 23:32:57 +04:00
int nr = attr - > index ;
2005-04-30 15:41:29 +04:00
u8 new_div ;
2012-01-15 01:12:38 +04:00
unsigned long val ;
int err ;
err = kstrtoul ( buf , 10 , & val ) ;
if ( err )
return err ;
2005-04-30 15:41:29 +04:00
2006-01-19 01:19:26 +03:00
mutex_lock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
if ( ! val ) {
data - > fan_min [ nr ] = 255 ;
new_div = data - > fan_div [ nr ] ;
dev_dbg ( & client - > dev , " fan%u low limit set disabled \n " ,
nr + 1 ) ;
} else if ( val < 1350000 / ( 8 * 254 ) ) {
new_div = 3 ;
data - > fan_min [ nr ] = 254 ;
dev_dbg ( & client - > dev , " fan%u low limit set minimum %u \n " ,
nr + 1 , FAN_FROM_REG ( 254 , 1 < < new_div ) ) ;
} else {
unsigned int new_min = 1350000 / val ;
new_div = 0 ;
while ( new_min > 192 & & new_div < 3 ) {
new_div + + ;
new_min / = 2 ;
}
if ( ! new_min ) /* keep > 0 */
new_min + + ;
data - > fan_min [ nr ] = new_min ;
dev_dbg ( & client - > dev , " fan%u low limit set fan speed %u \n " ,
nr + 1 , FAN_FROM_REG ( new_min , 1 < < new_div ) ) ;
}
if ( new_div ! = data - > fan_div [ nr ] ) {
data - > fan_div [ nr ] = new_div ;
adm9240_write_fan_div ( client , nr , new_div ) ;
}
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client , ADM9240_REG_FAN_MIN ( nr ) ,
2005-04-30 15:41:29 +04:00
data - > fan_min [ nr ] ) ;
2006-01-19 01:19:26 +03:00
mutex_unlock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
return count ;
}
2005-09-16 23:32:57 +04:00
# define fan(nr) \
static SENSOR_DEVICE_ATTR ( fan # # nr # # _input , S_IRUGO , \
show_fan , NULL , nr - 1 ) ; \
static SENSOR_DEVICE_ATTR ( fan # # nr # # _div , S_IRUGO , \
show_fan_div , NULL , nr - 1 ) ; \
static SENSOR_DEVICE_ATTR ( fan # # nr # # _min , S_IRUGO | S_IWUSR , \
show_fan_min , set_fan_min , nr - 1 ) ;
fan ( 1 ) ;
fan ( 2 ) ;
2005-04-30 15:41:29 +04:00
/* alarms */
2005-09-25 18:14:18 +04:00
static ssize_t show_alarms ( struct device * dev ,
struct device_attribute * attr , char * buf )
2005-04-30 15:41:29 +04:00
{
struct adm9240_data * data = adm9240_update_device ( dev ) ;
return sprintf ( buf , " %u \n " , data - > alarms ) ;
}
static DEVICE_ATTR ( alarms , S_IRUGO , show_alarms , NULL ) ;
2008-01-06 17:49:19 +03:00
static ssize_t show_alarm ( struct device * dev ,
struct device_attribute * attr , char * buf )
{
int bitnr = to_sensor_dev_attr ( attr ) - > index ;
struct adm9240_data * data = adm9240_update_device ( dev ) ;
return sprintf ( buf , " %u \n " , ( data - > alarms > > bitnr ) & 1 ) ;
}
static SENSOR_DEVICE_ATTR ( in0_alarm , S_IRUGO , show_alarm , NULL , 0 ) ;
static SENSOR_DEVICE_ATTR ( in1_alarm , S_IRUGO , show_alarm , NULL , 1 ) ;
static SENSOR_DEVICE_ATTR ( in2_alarm , S_IRUGO , show_alarm , NULL , 2 ) ;
static SENSOR_DEVICE_ATTR ( in3_alarm , S_IRUGO , show_alarm , NULL , 3 ) ;
static SENSOR_DEVICE_ATTR ( in4_alarm , S_IRUGO , show_alarm , NULL , 8 ) ;
static SENSOR_DEVICE_ATTR ( in5_alarm , S_IRUGO , show_alarm , NULL , 9 ) ;
static SENSOR_DEVICE_ATTR ( temp1_alarm , S_IRUGO , show_alarm , NULL , 4 ) ;
static SENSOR_DEVICE_ATTR ( fan1_alarm , S_IRUGO , show_alarm , NULL , 6 ) ;
static SENSOR_DEVICE_ATTR ( fan2_alarm , S_IRUGO , show_alarm , NULL , 7 ) ;
2005-04-30 15:41:29 +04:00
/* vid */
2005-09-25 18:14:18 +04:00
static ssize_t show_vid ( struct device * dev ,
struct device_attribute * attr , char * buf )
2005-04-30 15:41:29 +04:00
{
struct adm9240_data * data = adm9240_update_device ( dev ) ;
return sprintf ( buf , " %d \n " , vid_from_reg ( data - > vid , data - > vrm ) ) ;
}
static DEVICE_ATTR ( cpu0_vid , S_IRUGO , show_vid , NULL ) ;
/* analog output */
2005-09-25 18:14:18 +04:00
static ssize_t show_aout ( struct device * dev ,
struct device_attribute * attr , char * buf )
2005-04-30 15:41:29 +04:00
{
struct adm9240_data * data = adm9240_update_device ( dev ) ;
return sprintf ( buf , " %d \n " , AOUT_FROM_REG ( data - > aout ) ) ;
}
2005-09-25 18:14:18 +04:00
static ssize_t set_aout ( struct device * dev ,
struct device_attribute * attr ,
const char * buf , size_t count )
2005-04-30 15:41:29 +04:00
{
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2012-01-15 01:12:38 +04:00
long val ;
int err ;
err = kstrtol ( buf , 10 , & val ) ;
if ( err )
return err ;
2005-04-30 15:41:29 +04:00
2006-01-19 01:19:26 +03:00
mutex_lock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
data - > aout = AOUT_TO_REG ( val ) ;
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client , ADM9240_REG_ANALOG_OUT , data - > aout ) ;
2006-01-19 01:19:26 +03:00
mutex_unlock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
return count ;
}
static DEVICE_ATTR ( aout_output , S_IRUGO | S_IWUSR , show_aout , set_aout ) ;
/* chassis_clear */
2011-01-12 23:55:09 +03:00
static ssize_t chassis_clear_legacy ( struct device * dev ,
2005-09-25 18:14:18 +04:00
struct device_attribute * attr ,
const char * buf , size_t count )
2005-04-30 15:41:29 +04:00
{
struct i2c_client * client = to_i2c_client ( dev ) ;
2012-01-15 01:12:38 +04:00
long val ;
int err ;
err = kstrtol ( buf , 10 , & val ) ;
if ( err )
return err ;
2005-04-30 15:41:29 +04:00
2011-01-12 23:55:09 +03:00
dev_warn ( dev , " Attribute chassis_clear is deprecated, "
" use intrusion0_alarm instead \n " ) ;
2005-04-30 15:41:29 +04:00
if ( val = = 1 ) {
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client ,
ADM9240_REG_CHASSIS_CLEAR , 0x80 ) ;
2005-04-30 15:41:29 +04:00
dev_dbg ( & client - > dev , " chassis intrusion latch cleared \n " ) ;
}
return count ;
}
2011-01-12 23:55:09 +03:00
static DEVICE_ATTR ( chassis_clear , S_IWUSR , NULL , chassis_clear_legacy ) ;
static ssize_t chassis_clear ( struct device * dev ,
struct device_attribute * attr ,
const char * buf , size_t count )
{
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
unsigned long val ;
2012-01-04 23:58:52 +04:00
if ( kstrtoul ( buf , 10 , & val ) | | val ! = 0 )
2011-01-12 23:55:09 +03:00
return - EINVAL ;
mutex_lock ( & data - > update_lock ) ;
i2c_smbus_write_byte_data ( client , ADM9240_REG_CHASSIS_CLEAR , 0x80 ) ;
data - > valid = 0 ; /* Force cache refresh */
mutex_unlock ( & data - > update_lock ) ;
dev_dbg ( & client - > dev , " chassis intrusion latch cleared \n " ) ;
return count ;
}
static SENSOR_DEVICE_ATTR ( intrusion0_alarm , S_IRUGO | S_IWUSR , show_alarm ,
chassis_clear , 12 ) ;
2005-04-30 15:41:29 +04:00
2006-09-24 23:15:35 +04:00
static struct attribute * adm9240_attributes [ ] = {
& sensor_dev_attr_in0_input . dev_attr . attr ,
& sensor_dev_attr_in0_min . dev_attr . attr ,
& sensor_dev_attr_in0_max . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_in0_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_in1_input . dev_attr . attr ,
& sensor_dev_attr_in1_min . dev_attr . attr ,
& sensor_dev_attr_in1_max . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_in1_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_in2_input . dev_attr . attr ,
& sensor_dev_attr_in2_min . dev_attr . attr ,
& sensor_dev_attr_in2_max . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_in2_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_in3_input . dev_attr . attr ,
& sensor_dev_attr_in3_min . dev_attr . attr ,
& sensor_dev_attr_in3_max . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_in3_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_in4_input . dev_attr . attr ,
& sensor_dev_attr_in4_min . dev_attr . attr ,
& sensor_dev_attr_in4_max . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_in4_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_in5_input . dev_attr . attr ,
& sensor_dev_attr_in5_min . dev_attr . attr ,
& sensor_dev_attr_in5_max . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_in5_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& dev_attr_temp1_input . attr ,
& sensor_dev_attr_temp1_max . dev_attr . attr ,
& sensor_dev_attr_temp1_max_hyst . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_temp1_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_fan1_input . dev_attr . attr ,
& sensor_dev_attr_fan1_div . dev_attr . attr ,
& sensor_dev_attr_fan1_min . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_fan1_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& sensor_dev_attr_fan2_input . dev_attr . attr ,
& sensor_dev_attr_fan2_div . dev_attr . attr ,
& sensor_dev_attr_fan2_min . dev_attr . attr ,
2008-01-06 17:49:19 +03:00
& sensor_dev_attr_fan2_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& dev_attr_alarms . attr ,
& dev_attr_aout_output . attr ,
& dev_attr_chassis_clear . attr ,
2011-01-12 23:55:09 +03:00
& sensor_dev_attr_intrusion0_alarm . dev_attr . attr ,
2006-09-24 23:15:35 +04:00
& dev_attr_cpu0_vid . attr ,
NULL
} ;
static const struct attribute_group adm9240_group = {
. attrs = adm9240_attributes ,
} ;
2005-04-30 15:41:29 +04:00
/*** sensor chip detect and driver install ***/
2008-07-16 21:30:09 +04:00
/* Return 0 if detection is successful, -ENODEV otherwise */
2009-12-14 23:17:23 +03:00
static int adm9240_detect ( struct i2c_client * new_client ,
2008-07-16 21:30:09 +04:00
struct i2c_board_info * info )
2005-04-30 15:41:29 +04:00
{
2008-07-16 21:30:09 +04:00
struct i2c_adapter * adapter = new_client - > adapter ;
2005-04-30 15:41:29 +04:00
const char * name = " " ;
2008-07-16 21:30:09 +04:00
int address = new_client - > addr ;
2005-04-30 15:41:29 +04:00
u8 man_id , die_rev ;
if ( ! i2c_check_functionality ( adapter , I2C_FUNC_SMBUS_BYTE_DATA ) )
2008-07-16 21:30:09 +04:00
return - ENODEV ;
2005-04-30 15:41:29 +04:00
2009-12-09 22:35:57 +03:00
/* verify chip: reg address should match i2c address */
if ( i2c_smbus_read_byte_data ( new_client , ADM9240_REG_I2C_ADDR )
! = address ) {
dev_err ( & adapter - > dev , " detect fail: address match, 0x%02x \n " ,
address ) ;
return - ENODEV ;
2005-04-30 15:41:29 +04:00
}
2009-12-09 22:35:57 +03:00
/* check known chip manufacturer */
man_id = i2c_smbus_read_byte_data ( new_client , ADM9240_REG_MAN_ID ) ;
if ( man_id = = 0x23 ) {
2005-04-30 15:41:29 +04:00
name = " adm9240 " ;
2009-12-09 22:35:57 +03:00
} else if ( man_id = = 0xda ) {
2005-04-30 15:41:29 +04:00
name = " ds1780 " ;
2009-12-09 22:35:57 +03:00
} else if ( man_id = = 0x01 ) {
2005-04-30 15:41:29 +04:00
name = " lm81 " ;
2009-12-09 22:35:57 +03:00
} else {
dev_err ( & adapter - > dev , " detect fail: unknown manuf, 0x%02x \n " ,
man_id ) ;
return - ENODEV ;
2005-04-30 15:41:29 +04:00
}
2009-12-09 22:35:57 +03:00
/* successful detect, print chip info */
die_rev = i2c_smbus_read_byte_data ( new_client , ADM9240_REG_DIE_REV ) ;
dev_info ( & adapter - > dev , " found %s revision %u \n " ,
man_id = = 0x23 ? " ADM9240 " :
man_id = = 0xda ? " DS1780 " : " LM81 " , die_rev ) ;
2008-07-16 21:30:09 +04:00
strlcpy ( info - > type , name , I2C_NAME_SIZE ) ;
2005-04-30 15:41:29 +04:00
2008-07-16 21:30:09 +04:00
return 0 ;
}
2005-04-30 15:41:29 +04:00
2008-07-16 21:30:09 +04:00
static int adm9240_probe ( struct i2c_client * new_client ,
const struct i2c_device_id * id )
{
struct adm9240_data * data ;
int err ;
data = kzalloc ( sizeof ( * data ) , GFP_KERNEL ) ;
if ( ! data ) {
err = - ENOMEM ;
goto exit ;
}
i2c_set_clientdata ( new_client , data ) ;
mutex_init ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
adm9240_init_client ( new_client ) ;
/* populate sysfs filesystem */
2012-01-15 01:12:38 +04:00
err = sysfs_create_group ( & new_client - > dev . kobj , & adm9240_group ) ;
if ( err )
2008-07-16 21:30:09 +04:00
goto exit_free ;
2006-09-24 23:15:35 +04:00
2007-08-21 00:46:20 +04:00
data - > hwmon_dev = hwmon_device_register ( & new_client - > dev ) ;
if ( IS_ERR ( data - > hwmon_dev ) ) {
err = PTR_ERR ( data - > hwmon_dev ) ;
2006-09-24 23:15:35 +04:00
goto exit_remove ;
2005-07-16 05:39:18 +04:00
}
2005-04-30 15:41:29 +04:00
return 0 ;
2005-07-16 05:39:18 +04:00
2006-09-24 23:15:35 +04:00
exit_remove :
sysfs_remove_group ( & new_client - > dev . kobj , & adm9240_group ) ;
2005-04-30 15:41:29 +04:00
exit_free :
2005-08-26 01:49:14 +04:00
kfree ( data ) ;
2005-04-30 15:41:29 +04:00
exit :
return err ;
}
2008-07-16 21:30:09 +04:00
static int adm9240_remove ( struct i2c_client * client )
2005-04-30 15:41:29 +04:00
{
2005-07-16 05:39:18 +04:00
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2005-04-30 15:41:29 +04:00
2007-08-21 00:46:20 +04:00
hwmon_device_unregister ( data - > hwmon_dev ) ;
2006-09-24 23:15:35 +04:00
sysfs_remove_group ( & client - > dev . kobj , & adm9240_group ) ;
2005-07-16 05:39:18 +04:00
kfree ( data ) ;
2005-04-30 15:41:29 +04:00
return 0 ;
}
static void adm9240_init_client ( struct i2c_client * client )
{
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
2005-09-16 23:32:55 +04:00
u8 conf = i2c_smbus_read_byte_data ( client , ADM9240_REG_CONFIG ) ;
u8 mode = i2c_smbus_read_byte_data ( client , ADM9240_REG_TEMP_CONF ) & 3 ;
2005-04-30 15:41:29 +04:00
2005-07-31 23:52:01 +04:00
data - > vrm = vid_which_vrm ( ) ; /* need this to report vid as mV */
2005-04-30 15:41:29 +04:00
2005-05-13 14:26:10 +04:00
dev_info ( & client - > dev , " Using VRM: %d.%d \n " , data - > vrm / 10 ,
data - > vrm % 10 ) ;
2005-04-30 15:41:29 +04:00
if ( conf & 1 ) { /* measurement cycle running: report state */
dev_info ( & client - > dev , " status: config 0x%02x mode %u \n " ,
conf , mode ) ;
} else { /* cold start: open limits before starting chip */
int i ;
2012-01-15 01:12:38 +04:00
for ( i = 0 ; i < 6 ; i + + ) {
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_IN_MIN ( i ) , 0 ) ;
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_IN_MAX ( i ) , 255 ) ;
}
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client ,
ADM9240_REG_FAN_MIN ( 0 ) , 255 ) ;
i2c_smbus_write_byte_data ( client ,
ADM9240_REG_FAN_MIN ( 1 ) , 255 ) ;
i2c_smbus_write_byte_data ( client ,
2005-09-16 23:32:57 +04:00
ADM9240_REG_TEMP_MAX ( 0 ) , 127 ) ;
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client ,
2005-09-16 23:32:57 +04:00
ADM9240_REG_TEMP_MAX ( 1 ) , 127 ) ;
2005-04-30 15:41:29 +04:00
/* start measurement cycle */
2005-09-16 23:32:55 +04:00
i2c_smbus_write_byte_data ( client , ADM9240_REG_CONFIG , 1 ) ;
2005-04-30 15:41:29 +04:00
dev_info ( & client - > dev , " cold start: config was 0x%02x "
" mode %u \n " , conf , mode ) ;
}
}
static struct adm9240_data * adm9240_update_device ( struct device * dev )
{
struct i2c_client * client = to_i2c_client ( dev ) ;
struct adm9240_data * data = i2c_get_clientdata ( client ) ;
int i ;
2006-01-19 01:19:26 +03:00
mutex_lock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
/* minimum measurement cycle: 1.75 seconds */
if ( time_after ( jiffies , data - > last_updated_measure + ( HZ * 7 / 4 ) )
| | ! data - > valid ) {
2012-01-15 01:12:38 +04:00
for ( i = 0 ; i < 6 ; i + + ) { /* read voltages */
2005-09-16 23:32:55 +04:00
data - > in [ i ] = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_IN ( i ) ) ;
}
2005-09-16 23:32:55 +04:00
data - > alarms = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_INT ( 0 ) ) |
2005-09-16 23:32:55 +04:00
i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_INT ( 1 ) ) < < 8 ;
2012-01-15 01:12:38 +04:00
/*
* read temperature : assume temperature changes less than
2005-04-30 15:41:29 +04:00
* 0.5 ' C per two measurement cycles thus ignore possible
2012-01-15 01:12:38 +04:00
* but unlikely aliasing error on lsb reading . - - Grant
*/
2005-09-16 23:32:55 +04:00
data - > temp = ( ( i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_TEMP ) < < 8 ) |
2005-09-16 23:32:55 +04:00
i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_TEMP_CONF ) ) / 128 ;
2012-01-15 01:12:38 +04:00
for ( i = 0 ; i < 2 ; i + + ) { /* read fans */
2005-09-16 23:32:55 +04:00
data - > fan [ i ] = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_FAN ( i ) ) ;
/* adjust fan clock divider on overflow */
if ( data - > valid & & data - > fan [ i ] = = 255 & &
data - > fan_div [ i ] < 3 ) {
adm9240_write_fan_div ( client , i ,
+ + data - > fan_div [ i ] ) ;
/* adjust fan_min if active, but not to 0 */
if ( data - > fan_min [ i ] < 255 & &
data - > fan_min [ i ] > = 2 )
data - > fan_min [ i ] / = 2 ;
}
}
data - > last_updated_measure = jiffies ;
}
/* minimum config reading cycle: 300 seconds */
if ( time_after ( jiffies , data - > last_updated_config + ( HZ * 300 ) )
| | ! data - > valid ) {
2012-01-15 01:12:38 +04:00
for ( i = 0 ; i < 6 ; i + + ) {
2005-09-16 23:32:55 +04:00
data - > in_min [ i ] = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_IN_MIN ( i ) ) ;
2005-09-16 23:32:55 +04:00
data - > in_max [ i ] = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_IN_MAX ( i ) ) ;
}
2012-01-15 01:12:38 +04:00
for ( i = 0 ; i < 2 ; i + + ) {
2005-09-16 23:32:55 +04:00
data - > fan_min [ i ] = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_FAN_MIN ( i ) ) ;
}
2005-09-16 23:32:57 +04:00
data - > temp_max [ 0 ] = i2c_smbus_read_byte_data ( client ,
ADM9240_REG_TEMP_MAX ( 0 ) ) ;
data - > temp_max [ 1 ] = i2c_smbus_read_byte_data ( client ,
ADM9240_REG_TEMP_MAX ( 1 ) ) ;
2005-04-30 15:41:29 +04:00
/* read fan divs and 5-bit VID */
2005-09-16 23:32:55 +04:00
i = i2c_smbus_read_byte_data ( client , ADM9240_REG_VID_FAN_DIV ) ;
2005-04-30 15:41:29 +04:00
data - > fan_div [ 0 ] = ( i > > 4 ) & 3 ;
data - > fan_div [ 1 ] = ( i > > 6 ) & 3 ;
data - > vid = i & 0x0f ;
2005-09-16 23:32:55 +04:00
data - > vid | = ( i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_VID4 ) & 1 ) < < 4 ;
/* read analog out */
2005-09-16 23:32:55 +04:00
data - > aout = i2c_smbus_read_byte_data ( client ,
2005-04-30 15:41:29 +04:00
ADM9240_REG_ANALOG_OUT ) ;
data - > last_updated_config = jiffies ;
data - > valid = 1 ;
}
2006-01-19 01:19:26 +03:00
mutex_unlock ( & data - > update_lock ) ;
2005-04-30 15:41:29 +04:00
return data ;
}
2012-01-20 11:38:18 +04:00
module_i2c_driver ( adm9240_driver ) ;
2005-04-30 15:41:29 +04:00
MODULE_AUTHOR ( " Michiel Rook <michiel@grendelproject.nl>, "
2006-10-08 23:57:41 +04:00
" Grant Coady <gcoady.lk@gmail.com> and others " ) ;
2005-04-30 15:41:29 +04:00
MODULE_DESCRIPTION ( " ADM9240/DS1780/LM81 driver " ) ;
MODULE_LICENSE ( " GPL " ) ;