2019-06-04 11:11:33 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2010-03-06 00:17:23 +03:00
/*
* Driver for the ADT7411 ( I2C / SPI 8 channel 10 bit ADC & temperature - sensor )
*
* Copyright ( C ) 2008 , 2010 Pengutronix
*
2016-07-25 12:12:06 +03:00
* TODO : SPI , use power - down mode for suspend ? , interrupt handling ?
2010-03-06 00:17:23 +03:00
*/
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/init.h>
# include <linux/err.h>
# include <linux/mutex.h>
# include <linux/jiffies.h>
# include <linux/i2c.h>
# include <linux/hwmon.h>
# include <linux/hwmon-sysfs.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2010-03-06 00:17:23 +03:00
2016-12-15 20:38:17 +03:00
# define ADT7411_REG_STAT_1 0x00
# define ADT7411_STAT_1_INT_TEMP_HIGH BIT(0)
# define ADT7411_STAT_1_INT_TEMP_LOW BIT(1)
# define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 BIT(2)
# define ADT7411_STAT_1_EXT_TEMP_LOW BIT(3)
# define ADT7411_STAT_1_EXT_TEMP_FAULT BIT(4)
# define ADT7411_STAT_1_AIN2 BIT(5)
# define ADT7411_STAT_1_AIN3 BIT(6)
# define ADT7411_STAT_1_AIN4 BIT(7)
# define ADT7411_REG_STAT_2 0x01
# define ADT7411_STAT_2_AIN5 BIT(0)
# define ADT7411_STAT_2_AIN6 BIT(1)
# define ADT7411_STAT_2_AIN7 BIT(2)
# define ADT7411_STAT_2_AIN8 BIT(3)
# define ADT7411_STAT_2_VDD BIT(4)
2010-03-06 00:17:23 +03:00
# define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
# define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
# define ADT7411_REG_VDD_MSB 0x06
# define ADT7411_REG_INT_TEMP_MSB 0x07
# define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
# define ADT7411_REG_CFG1 0x18
2016-12-15 20:38:17 +03:00
# define ADT7411_CFG1_START_MONITOR BIT(0)
# define ADT7411_CFG1_RESERVED_BIT1 BIT(1)
# define ADT7411_CFG1_EXT_TDM BIT(2)
# define ADT7411_CFG1_RESERVED_BIT3 BIT(3)
2010-03-06 00:17:23 +03:00
# define ADT7411_REG_CFG2 0x19
2016-12-15 20:38:17 +03:00
# define ADT7411_CFG2_DISABLE_AVG BIT(5)
2010-03-06 00:17:23 +03:00
# define ADT7411_REG_CFG3 0x1a
2016-12-15 20:38:17 +03:00
# define ADT7411_CFG3_ADC_CLK_225 BIT(0)
# define ADT7411_CFG3_RESERVED_BIT1 BIT(1)
# define ADT7411_CFG3_RESERVED_BIT2 BIT(2)
# define ADT7411_CFG3_RESERVED_BIT3 BIT(3)
# define ADT7411_CFG3_REF_VDD BIT(4)
# define ADT7411_REG_VDD_HIGH 0x23
# define ADT7411_REG_VDD_LOW 0x24
# define ADT7411_REG_TEMP_HIGH(nr) (0x25 + 2 * (nr))
# define ADT7411_REG_TEMP_LOW(nr) (0x26 + 2 * (nr))
# define ADT7411_REG_IN_HIGH(nr) ((nr) > 1 \
? 0x2b + 2 * ( ( nr ) - 2 ) \
: 0x27 )
# define ADT7411_REG_IN_LOW(nr) ((nr) > 1 \
? 0x2c + 2 * ( ( nr ) - 2 ) \
: 0x28 )
2010-03-06 00:17:23 +03:00
# define ADT7411_REG_DEVICE_ID 0x4d
# define ADT7411_REG_MANUFACTURER_ID 0x4e
# define ADT7411_DEVICE_ID 0x2
# define ADT7411_MANUFACTURER_ID 0x41
static const unsigned short normal_i2c [ ] = { 0x48 , 0x4a , 0x4b , I2C_CLIENT_END } ;
2016-12-15 20:38:17 +03:00
static const u8 adt7411_in_alarm_reg [ ] = {
ADT7411_REG_STAT_2 ,
ADT7411_REG_STAT_1 ,
ADT7411_REG_STAT_1 ,
ADT7411_REG_STAT_1 ,
ADT7411_REG_STAT_1 ,
ADT7411_REG_STAT_2 ,
ADT7411_REG_STAT_2 ,
ADT7411_REG_STAT_2 ,
ADT7411_REG_STAT_2 ,
} ;
static const u8 adt7411_in_alarm_bits [ ] = {
ADT7411_STAT_2_VDD ,
ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1 ,
ADT7411_STAT_1_AIN2 ,
ADT7411_STAT_1_AIN3 ,
ADT7411_STAT_1_AIN4 ,
ADT7411_STAT_2_AIN5 ,
ADT7411_STAT_2_AIN6 ,
ADT7411_STAT_2_AIN7 ,
ADT7411_STAT_2_AIN8 ,
} ;
2010-03-06 00:17:23 +03:00
struct adt7411_data {
struct mutex device_lock ; /* for "atomic" device accesses */
2010-03-06 00:17:23 +03:00
struct mutex update_lock ;
2010-03-06 00:17:23 +03:00
unsigned long next_update ;
2016-10-14 12:43:34 +03:00
long vref_cached ;
2014-07-19 18:42:33 +04:00
struct i2c_client * client ;
2016-07-25 12:12:06 +03:00
bool use_ext_temp ;
2010-03-06 00:17:23 +03:00
} ;
/*
* When reading a register containing ( up to 4 ) lsb , all associated
* msb - registers get locked by the hardware . After _one_ of those msb is read ,
* _all_ are unlocked . In order to use this locking correctly , reading lsb / msb
* is protected here with a mutex , too .
*/
static int adt7411_read_10_bit ( struct i2c_client * client , u8 lsb_reg ,
u8 msb_reg , u8 lsb_shift )
{
struct adt7411_data * data = i2c_get_clientdata ( client ) ;
int val , tmp ;
mutex_lock ( & data - > device_lock ) ;
val = i2c_smbus_read_byte_data ( client , lsb_reg ) ;
if ( val < 0 )
goto exit_unlock ;
tmp = ( val > > lsb_shift ) & 3 ;
val = i2c_smbus_read_byte_data ( client , msb_reg ) ;
if ( val > = 0 )
val = ( val < < 2 ) | tmp ;
exit_unlock :
mutex_unlock ( & data - > device_lock ) ;
return val ;
}
static int adt7411_modify_bit ( struct i2c_client * client , u8 reg , u8 bit ,
bool flag )
{
struct adt7411_data * data = i2c_get_clientdata ( client ) ;
int ret , val ;
mutex_lock ( & data - > device_lock ) ;
ret = i2c_smbus_read_byte_data ( client , reg ) ;
if ( ret < 0 )
goto exit_unlock ;
if ( flag )
val = ret | bit ;
else
val = ret & ~ bit ;
ret = i2c_smbus_write_byte_data ( client , reg , val ) ;
exit_unlock :
mutex_unlock ( & data - > device_lock ) ;
return ret ;
}
static ssize_t adt7411_show_bit ( struct device * dev ,
struct device_attribute * attr , char * buf )
{
struct sensor_device_attribute_2 * attr2 = to_sensor_dev_attr_2 ( attr ) ;
2014-07-19 18:42:33 +04:00
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
2010-03-06 00:17:23 +03:00
int ret = i2c_smbus_read_byte_data ( client , attr2 - > index ) ;
return ret < 0 ? ret : sprintf ( buf , " %u \n " , ! ! ( ret & attr2 - > nr ) ) ;
}
static ssize_t adt7411_set_bit ( struct device * dev ,
struct device_attribute * attr , const char * buf ,
size_t count )
{
struct sensor_device_attribute_2 * s_attr2 = to_sensor_dev_attr_2 ( attr ) ;
2014-07-19 18:42:33 +04:00
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
2010-03-06 00:17:23 +03:00
int ret ;
unsigned long flag ;
2012-01-04 23:58:52 +04:00
ret = kstrtoul ( buf , 0 , & flag ) ;
2010-03-06 00:17:23 +03:00
if ( ret | | flag > 1 )
return - EINVAL ;
ret = adt7411_modify_bit ( client , s_attr2 - > index , s_attr2 - > nr , flag ) ;
/* force update */
2010-03-06 00:17:23 +03:00
mutex_lock ( & data - > update_lock ) ;
2010-03-06 00:17:23 +03:00
data - > next_update = jiffies ;
2010-03-06 00:17:23 +03:00
mutex_unlock ( & data - > update_lock ) ;
2010-03-06 00:17:23 +03:00
return ret < 0 ? ret : count ;
}
# define ADT7411_BIT_ATTR(__name, __reg, __bit) \
SENSOR_DEVICE_ATTR_2 ( __name , S_IRUGO | S_IWUSR , adt7411_show_bit , \
adt7411_set_bit , __bit , __reg )
static ADT7411_BIT_ATTR ( no_average , ADT7411_REG_CFG2 , ADT7411_CFG2_DISABLE_AVG ) ;
static ADT7411_BIT_ATTR ( fast_sampling , ADT7411_REG_CFG3 , ADT7411_CFG3_ADC_CLK_225 ) ;
static ADT7411_BIT_ATTR ( adc_ref_vdd , ADT7411_REG_CFG3 , ADT7411_CFG3_REF_VDD ) ;
static struct attribute * adt7411_attrs [ ] = {
& sensor_dev_attr_no_average . dev_attr . attr ,
& sensor_dev_attr_fast_sampling . dev_attr . attr ,
& sensor_dev_attr_adc_ref_vdd . dev_attr . attr ,
NULL
} ;
2016-10-14 12:43:34 +03:00
ATTRIBUTE_GROUPS ( adt7411 ) ;
2010-03-06 00:17:23 +03:00
2016-12-15 20:38:17 +03:00
static int adt7411_read_in_alarm ( struct device * dev , int channel , long * val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
int ret ;
ret = i2c_smbus_read_byte_data ( client , adt7411_in_alarm_reg [ channel ] ) ;
if ( ret < 0 )
return ret ;
* val = ! ! ( ret & adt7411_in_alarm_bits [ channel ] ) ;
return 0 ;
}
2016-10-14 12:43:34 +03:00
static int adt7411_read_in_vdd ( struct device * dev , u32 attr , long * val )
2016-07-25 12:12:06 +03:00
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
2016-10-14 12:43:34 +03:00
struct i2c_client * client = data - > client ;
int ret ;
2016-07-25 12:12:06 +03:00
2016-10-14 12:43:34 +03:00
switch ( attr ) {
case hwmon_in_input :
ret = adt7411_read_10_bit ( client , ADT7411_REG_INT_TEMP_VDD_LSB ,
ADT7411_REG_VDD_MSB , 2 ) ;
if ( ret < 0 )
return ret ;
* val = ret * 7000 / 1024 ;
return 0 ;
2016-12-15 20:38:17 +03:00
case hwmon_in_min :
ret = i2c_smbus_read_byte_data ( client , ADT7411_REG_VDD_LOW ) ;
if ( ret < 0 )
return ret ;
* val = ret * 7000 / 256 ;
return 0 ;
case hwmon_in_max :
ret = i2c_smbus_read_byte_data ( client , ADT7411_REG_VDD_HIGH ) ;
if ( ret < 0 )
return ret ;
* val = ret * 7000 / 256 ;
return 0 ;
case hwmon_in_alarm :
return adt7411_read_in_alarm ( dev , 0 , val ) ;
2016-10-14 12:43:34 +03:00
default :
return - EOPNOTSUPP ;
}
}
2016-12-15 20:38:17 +03:00
static int adt7411_update_vref ( struct device * dev )
2016-10-14 12:43:34 +03:00
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
2016-12-15 20:38:17 +03:00
int val ;
2016-07-25 12:12:06 +03:00
2016-10-14 12:43:34 +03:00
if ( time_after_eq ( jiffies , data - > next_update ) ) {
2016-12-15 20:38:17 +03:00
val = i2c_smbus_read_byte_data ( client , ADT7411_REG_CFG3 ) ;
if ( val < 0 )
return val ;
2016-10-14 12:43:34 +03:00
2016-12-15 20:38:17 +03:00
if ( val & ADT7411_CFG3_REF_VDD ) {
val = adt7411_read_in_vdd ( dev , hwmon_in_input ,
2016-10-14 12:43:34 +03:00
& data - > vref_cached ) ;
2016-12-15 20:38:17 +03:00
if ( val < 0 )
return val ;
2016-10-14 12:43:34 +03:00
} else {
data - > vref_cached = 2250 ;
}
data - > next_update = jiffies + HZ ;
}
2016-12-15 20:38:17 +03:00
return 0 ;
}
static int adt7411_read_in_chan ( struct device * dev , u32 attr , int channel ,
long * val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
int ret ;
int reg , lsb_reg , lsb_shift ;
int nr = channel - 1 ;
mutex_lock ( & data - > update_lock ) ;
ret = adt7411_update_vref ( dev ) ;
if ( ret < 0 )
goto exit_unlock ;
2016-10-14 12:43:34 +03:00
switch ( attr ) {
case hwmon_in_input :
lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + ( nr > > 2 ) ;
lsb_shift = 2 * ( nr & 0x03 ) ;
ret = adt7411_read_10_bit ( client , lsb_reg ,
ADT7411_REG_EXT_TEMP_AIN1_MSB + nr ,
lsb_shift ) ;
if ( ret < 0 )
goto exit_unlock ;
* val = ret * data - > vref_cached / 1024 ;
ret = 0 ;
break ;
2016-12-15 20:38:17 +03:00
case hwmon_in_min :
case hwmon_in_max :
reg = ( attr = = hwmon_in_min )
? ADT7411_REG_IN_LOW ( channel )
: ADT7411_REG_IN_HIGH ( channel ) ;
ret = i2c_smbus_read_byte_data ( client , reg ) ;
if ( ret < 0 )
goto exit_unlock ;
* val = ret * data - > vref_cached / 256 ;
ret = 0 ;
break ;
case hwmon_in_alarm :
ret = adt7411_read_in_alarm ( dev , channel , val ) ;
break ;
2016-10-14 12:43:34 +03:00
default :
ret = - EOPNOTSUPP ;
break ;
}
exit_unlock :
mutex_unlock ( & data - > update_lock ) ;
return ret ;
2016-07-25 12:12:06 +03:00
}
2016-10-14 12:43:34 +03:00
static int adt7411_read_in ( struct device * dev , u32 attr , int channel ,
long * val )
{
if ( channel = = 0 )
return adt7411_read_in_vdd ( dev , attr , val ) ;
else
return adt7411_read_in_chan ( dev , attr , channel , val ) ;
}
2016-12-15 20:38:17 +03:00
static int adt7411_read_temp_alarm ( struct device * dev , u32 attr , int channel ,
long * val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
int ret , bit ;
ret = i2c_smbus_read_byte_data ( client , ADT7411_REG_STAT_1 ) ;
if ( ret < 0 )
return ret ;
switch ( attr ) {
case hwmon_temp_min_alarm :
bit = channel ? ADT7411_STAT_1_EXT_TEMP_LOW
: ADT7411_STAT_1_INT_TEMP_LOW ;
break ;
case hwmon_temp_max_alarm :
bit = channel ? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
: ADT7411_STAT_1_INT_TEMP_HIGH ;
break ;
case hwmon_temp_fault :
bit = ADT7411_STAT_1_EXT_TEMP_FAULT ;
break ;
default :
return - EOPNOTSUPP ;
}
* val = ! ! ( ret & bit ) ;
return 0 ;
}
2016-10-14 12:43:34 +03:00
static int adt7411_read_temp ( struct device * dev , u32 attr , int channel ,
long * val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
2016-12-15 20:38:17 +03:00
int ret , reg , regl , regh ;
2016-10-14 12:43:34 +03:00
switch ( attr ) {
case hwmon_temp_input :
regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB :
ADT7411_REG_INT_TEMP_VDD_LSB ;
regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB :
ADT7411_REG_INT_TEMP_MSB ;
ret = adt7411_read_10_bit ( client , regl , regh , 0 ) ;
if ( ret < 0 )
return ret ;
ret = ret & 0x200 ? ret - 0x400 : ret ; /* 10 bit signed */
* val = ret * 250 ;
return 0 ;
2016-12-15 20:38:17 +03:00
case hwmon_temp_min :
case hwmon_temp_max :
reg = ( attr = = hwmon_temp_min )
? ADT7411_REG_TEMP_LOW ( channel )
: ADT7411_REG_TEMP_HIGH ( channel ) ;
ret = i2c_smbus_read_byte_data ( client , reg ) ;
if ( ret < 0 )
return ret ;
ret = ret & 0x80 ? ret - 0x100 : ret ; /* 8 bit signed */
* val = ret * 1000 ;
return 0 ;
case hwmon_temp_min_alarm :
case hwmon_temp_max_alarm :
case hwmon_temp_fault :
return adt7411_read_temp_alarm ( dev , attr , channel , val ) ;
2016-10-14 12:43:34 +03:00
default :
return - EOPNOTSUPP ;
}
}
static int adt7411_read ( struct device * dev , enum hwmon_sensor_types type ,
u32 attr , int channel , long * val )
{
switch ( type ) {
case hwmon_in :
return adt7411_read_in ( dev , attr , channel , val ) ;
case hwmon_temp :
return adt7411_read_temp ( dev , attr , channel , val ) ;
default :
return - EOPNOTSUPP ;
}
}
2016-12-15 20:38:17 +03:00
static int adt7411_write_in_vdd ( struct device * dev , u32 attr , long val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
int reg ;
val = clamp_val ( val , 0 , 255 * 7000 / 256 ) ;
val = DIV_ROUND_CLOSEST ( val * 256 , 7000 ) ;
switch ( attr ) {
case hwmon_in_min :
reg = ADT7411_REG_VDD_LOW ;
break ;
case hwmon_in_max :
reg = ADT7411_REG_VDD_HIGH ;
break ;
default :
return - EOPNOTSUPP ;
}
return i2c_smbus_write_byte_data ( client , reg , val ) ;
}
static int adt7411_write_in_chan ( struct device * dev , u32 attr , int channel ,
long val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
int ret , reg ;
mutex_lock ( & data - > update_lock ) ;
ret = adt7411_update_vref ( dev ) ;
if ( ret < 0 )
goto exit_unlock ;
val = clamp_val ( val , 0 , 255 * data - > vref_cached / 256 ) ;
val = DIV_ROUND_CLOSEST ( val * 256 , data - > vref_cached ) ;
switch ( attr ) {
case hwmon_in_min :
reg = ADT7411_REG_IN_LOW ( channel ) ;
break ;
case hwmon_in_max :
reg = ADT7411_REG_IN_HIGH ( channel ) ;
break ;
default :
ret = - EOPNOTSUPP ;
goto exit_unlock ;
}
ret = i2c_smbus_write_byte_data ( client , reg , val ) ;
exit_unlock :
mutex_unlock ( & data - > update_lock ) ;
return ret ;
}
static int adt7411_write_in ( struct device * dev , u32 attr , int channel ,
long val )
{
if ( channel = = 0 )
return adt7411_write_in_vdd ( dev , attr , val ) ;
else
return adt7411_write_in_chan ( dev , attr , channel , val ) ;
}
static int adt7411_write_temp ( struct device * dev , u32 attr , int channel ,
long val )
{
struct adt7411_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
int reg ;
val = clamp_val ( val , - 128000 , 127000 ) ;
val = DIV_ROUND_CLOSEST ( val , 1000 ) ;
switch ( attr ) {
case hwmon_temp_min :
reg = ADT7411_REG_TEMP_LOW ( channel ) ;
break ;
case hwmon_temp_max :
reg = ADT7411_REG_TEMP_HIGH ( channel ) ;
break ;
default :
return - EOPNOTSUPP ;
}
return i2c_smbus_write_byte_data ( client , reg , val ) ;
}
static int adt7411_write ( struct device * dev , enum hwmon_sensor_types type ,
u32 attr , int channel , long val )
{
switch ( type ) {
case hwmon_in :
return adt7411_write_in ( dev , attr , channel , val ) ;
case hwmon_temp :
return adt7411_write_temp ( dev , attr , channel , val ) ;
default :
return - EOPNOTSUPP ;
}
}
2016-10-14 12:43:34 +03:00
static umode_t adt7411_is_visible ( const void * _data ,
enum hwmon_sensor_types type ,
u32 attr , int channel )
{
const struct adt7411_data * data = _data ;
2016-12-15 20:38:17 +03:00
bool visible ;
2016-10-14 12:43:34 +03:00
switch ( type ) {
case hwmon_in :
2016-12-15 20:38:17 +03:00
visible = channel = = 0 | | channel > = 3 | | ! data - > use_ext_temp ;
switch ( attr ) {
case hwmon_in_input :
case hwmon_in_alarm :
return visible ? S_IRUGO : 0 ;
case hwmon_in_min :
case hwmon_in_max :
return visible ? S_IRUGO | S_IWUSR : 0 ;
}
break ;
2016-10-14 12:43:34 +03:00
case hwmon_temp :
2016-12-15 20:38:17 +03:00
visible = channel = = 0 | | data - > use_ext_temp ;
switch ( attr ) {
case hwmon_temp_input :
case hwmon_temp_min_alarm :
case hwmon_temp_max_alarm :
case hwmon_temp_fault :
return visible ? S_IRUGO : 0 ;
case hwmon_temp_min :
case hwmon_temp_max :
return visible ? S_IRUGO | S_IWUSR : 0 ;
}
break ;
2016-10-14 12:43:34 +03:00
default :
2016-12-15 20:38:17 +03:00
break ;
2016-10-14 12:43:34 +03:00
}
2016-12-15 20:38:17 +03:00
return 0 ;
2016-10-14 12:43:34 +03:00
}
2010-03-06 00:17:23 +03:00
static int adt7411_detect ( struct i2c_client * client ,
struct i2c_board_info * info )
{
int val ;
if ( ! i2c_check_functionality ( client - > adapter , I2C_FUNC_SMBUS_BYTE_DATA ) )
return - ENODEV ;
val = i2c_smbus_read_byte_data ( client , ADT7411_REG_MANUFACTURER_ID ) ;
if ( val < 0 | | val ! = ADT7411_MANUFACTURER_ID ) {
2013-01-10 22:01:24 +04:00
dev_dbg ( & client - > dev ,
" Wrong manufacturer ID. Got %d, expected %d \n " ,
val , ADT7411_MANUFACTURER_ID ) ;
2010-03-06 00:17:23 +03:00
return - ENODEV ;
}
val = i2c_smbus_read_byte_data ( client , ADT7411_REG_DEVICE_ID ) ;
if ( val < 0 | | val ! = ADT7411_DEVICE_ID ) {
2013-01-10 22:01:24 +04:00
dev_dbg ( & client - > dev ,
" Wrong device ID. Got %d, expected %d \n " ,
val , ADT7411_DEVICE_ID ) ;
2010-03-06 00:17:23 +03:00
return - ENODEV ;
}
strlcpy ( info - > type , " adt7411 " , I2C_NAME_SIZE ) ;
return 0 ;
}
2016-07-25 12:12:05 +03:00
static int adt7411_init_device ( struct adt7411_data * data )
{
int ret ;
u8 val ;
ret = i2c_smbus_read_byte_data ( data - > client , ADT7411_REG_CFG3 ) ;
if ( ret < 0 )
return ret ;
/*
* We must only write zero to bit 1 and bit 2 and only one to bit 3
* according to the datasheet .
*/
val = ret ;
val & = ~ ( ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2 ) ;
val | = ADT7411_CFG3_RESERVED_BIT3 ;
ret = i2c_smbus_write_byte_data ( data - > client , ADT7411_REG_CFG3 , val ) ;
if ( ret < 0 )
return ret ;
ret = i2c_smbus_read_byte_data ( data - > client , ADT7411_REG_CFG1 ) ;
if ( ret < 0 )
return ret ;
2016-07-25 12:12:06 +03:00
data - > use_ext_temp = ret & ADT7411_CFG1_EXT_TDM ;
2016-07-25 12:12:05 +03:00
/*
* We must only write zero to bit 1 and only one to bit 3 according to
* the datasheet .
*/
val = ret ;
val & = ~ ADT7411_CFG1_RESERVED_BIT1 ;
val | = ADT7411_CFG1_RESERVED_BIT3 ;
/* enable monitoring */
val | = ADT7411_CFG1_START_MONITOR ;
return i2c_smbus_write_byte_data ( data - > client , ADT7411_REG_CFG1 , val ) ;
}
2016-10-14 12:43:34 +03:00
static const struct hwmon_channel_info * adt7411_info [ ] = {
hwmon: (adt7411) Use HWMON_CHANNEL_INFO macro
The HWMON_CHANNEL_INFO macro simplifies the code, reduces the likelihood
of errors, and makes the code easier to read.
The conversion was done automatically with coccinelle. The semantic patch
used to make this change is as follows.
@r@
initializer list elements;
identifier i;
@@
-u32 i[] = {
- elements,
- 0
-};
@s@
identifier r.i,j,ty;
@@
-struct hwmon_channel_info j = {
- .type = ty,
- .config = i,
-};
@script:ocaml t@
ty << s.ty;
elements << r.elements;
shorter;
elems;
@@
shorter :=
make_ident (List.hd(List.rev (Str.split (Str.regexp "_") ty)));
elems :=
make_ident
(String.concat ","
(List.map (fun x -> Printf.sprintf "\n\t\t\t %s" x)
(Str.split (Str.regexp " , ") elements)))
@@
identifier s.j,t.shorter;
identifier t.elems;
@@
- &j
+ HWMON_CHANNEL_INFO(shorter,elems)
This patch does not introduce functional changes. Many thanks to
Julia Lawall for providing the semantic patch.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2019-03-31 20:53:46 +03:00
HWMON_CHANNEL_INFO ( in ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ,
HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM ) ,
HWMON_CHANNEL_INFO ( temp ,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
HWMON_T_MAX | HWMON_T_MAX_ALARM ,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
HWMON_T_MAX | HWMON_T_MAX_ALARM | HWMON_T_FAULT ) ,
2016-10-14 12:43:34 +03:00
NULL
} ;
static const struct hwmon_ops adt7411_hwmon_ops = {
. is_visible = adt7411_is_visible ,
. read = adt7411_read ,
2016-12-15 20:38:17 +03:00
. write = adt7411_write ,
2016-10-14 12:43:34 +03:00
} ;
static const struct hwmon_chip_info adt7411_chip_info = {
. ops = & adt7411_hwmon_ops ,
. info = adt7411_info ,
} ;
2020-08-13 19:02:22 +03:00
static int adt7411_probe ( struct i2c_client * client )
2010-03-06 00:17:23 +03:00
{
2014-07-19 18:42:33 +04:00
struct device * dev = & client - > dev ;
2010-03-06 00:17:23 +03:00
struct adt7411_data * data ;
2014-07-19 18:42:33 +04:00
struct device * hwmon_dev ;
2010-03-06 00:17:23 +03:00
int ret ;
2014-07-19 18:42:33 +04:00
data = devm_kzalloc ( dev , sizeof ( * data ) , GFP_KERNEL ) ;
2010-03-06 00:17:23 +03:00
if ( ! data )
return - ENOMEM ;
i2c_set_clientdata ( client , data ) ;
2014-07-19 18:42:33 +04:00
data - > client = client ;
2010-03-06 00:17:23 +03:00
mutex_init ( & data - > device_lock ) ;
2010-03-06 00:17:23 +03:00
mutex_init ( & data - > update_lock ) ;
2010-03-06 00:17:23 +03:00
2016-07-25 12:12:05 +03:00
ret = adt7411_init_device ( data ) ;
2010-03-06 00:17:23 +03:00
if ( ret < 0 )
2012-06-02 20:58:01 +04:00
return ret ;
2010-03-06 00:17:23 +03:00
/* force update on first occasion */
data - > next_update = jiffies ;
2016-10-14 12:43:34 +03:00
hwmon_dev = devm_hwmon_device_register_with_info ( dev , client - > name ,
data ,
& adt7411_chip_info ,
adt7411_groups ) ;
2014-07-19 18:42:33 +04:00
return PTR_ERR_OR_ZERO ( hwmon_dev ) ;
2010-03-06 00:17:23 +03:00
}
static const struct i2c_device_id adt7411_id [ ] = {
{ " adt7411 " , 0 } ,
{ }
} ;
2011-02-24 05:22:01 +03:00
MODULE_DEVICE_TABLE ( i2c , adt7411_id ) ;
2010-03-06 00:17:23 +03:00
static struct i2c_driver adt7411_driver = {
. driver = {
. name = " adt7411 " ,
} ,
2020-08-13 19:02:22 +03:00
. probe_new = adt7411_probe ,
2010-03-06 00:17:23 +03:00
. id_table = adt7411_id ,
. detect = adt7411_detect ,
. address_list = normal_i2c ,
. class = I2C_CLASS_HWMON ,
} ;
2012-01-20 11:38:18 +04:00
module_i2c_driver ( adt7411_driver ) ;
2010-03-06 00:17:23 +03:00
2020-05-02 17:27:00 +03:00
MODULE_AUTHOR ( " Sascha Hauer, Wolfram Sang <kernel@pengutronix.de> " ) ;
2010-03-06 00:17:23 +03:00
MODULE_DESCRIPTION ( " ADT7411 driver " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;