2019-05-31 11:09:32 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2007-05-08 19:22:02 +04:00
/*
* An hwmon driver for the Analog Devices AD7416 / 17 / 18
* Copyright ( C ) 2006 - 07 Tower Technologies
*
* Author : Alessandro Zummo < a . zummo @ towertech . it >
*
* Based on lm75 . c
* Copyright ( C ) 1998 - 99 Frodo Looijaard < frodol @ dds . nl >
*/
# 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>
2019-01-29 00:06:37 +03:00
# include <linux/of_device.h>
2007-05-08 19:22:02 +04:00
# include <linux/delay.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>
2007-05-08 19:22:02 +04:00
# include "lm75.h"
2008-07-16 21:30:08 +04:00
# define DRV_VERSION "0.4"
2007-05-08 19:22:02 +04:00
2008-07-16 21:30:08 +04:00
enum chips { ad7416 , ad7417 , ad7418 } ;
2007-05-08 19:22:02 +04:00
/* AD7418 registers */
# define AD7418_REG_TEMP_IN 0x00
# define AD7418_REG_CONF 0x01
# define AD7418_REG_TEMP_HYST 0x02
# define AD7418_REG_TEMP_OS 0x03
# define AD7418_REG_ADC 0x04
# define AD7418_REG_CONF2 0x05
# define AD7418_REG_ADC_CH(x) ((x) << 5)
# define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
static const u8 AD7418_REG_TEMP [ ] = { AD7418_REG_TEMP_IN ,
AD7418_REG_TEMP_HYST ,
AD7418_REG_TEMP_OS } ;
struct ad7418_data {
2014-07-01 18:40:29 +04:00
struct i2c_client * client ;
2007-05-08 19:22:02 +04:00
enum chips type ;
struct mutex lock ;
int adc_max ; /* number of ADC channels */
char valid ;
unsigned long last_updated ; /* In jiffies */
s16 temp [ 3 ] ; /* Register values */
u16 in [ 4 ] ;
} ;
2019-02-08 00:50:36 +03:00
static int ad7418_update_device ( struct device * dev )
2007-05-08 19:22:02 +04:00
{
2014-07-01 18:40:29 +04:00
struct ad7418_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
2019-02-08 00:50:36 +03:00
s32 val ;
2007-05-08 19:22:02 +04:00
mutex_lock ( & data - > lock ) ;
if ( time_after ( jiffies , data - > last_updated + HZ + HZ / 2 )
| | ! data - > valid ) {
u8 cfg ;
int i , ch ;
/* read config register and clear channel bits */
2019-02-08 00:50:36 +03:00
val = i2c_smbus_read_byte_data ( client , AD7418_REG_CONF ) ;
if ( val < 0 )
goto abort ;
cfg = val ;
2007-05-08 19:22:02 +04:00
cfg & = 0x1F ;
2019-02-08 00:50:36 +03:00
val = i2c_smbus_write_byte_data ( client , AD7418_REG_CONF ,
2007-05-08 19:22:02 +04:00
cfg | AD7418_CH_TEMP ) ;
2019-02-08 00:50:36 +03:00
if ( val < 0 )
goto abort ;
2007-05-08 19:22:02 +04:00
udelay ( 30 ) ;
for ( i = 0 ; i < 3 ; i + + ) {
2019-02-08 00:50:36 +03:00
val = i2c_smbus_read_word_swapped ( client ,
AD7418_REG_TEMP [ i ] ) ;
if ( val < 0 )
goto abort ;
data - > temp [ i ] = val ;
2007-05-08 19:22:02 +04:00
}
for ( i = 0 , ch = 4 ; i < data - > adc_max ; i + + , ch - - ) {
2019-02-08 00:50:36 +03:00
val = i2c_smbus_write_byte_data ( client , AD7418_REG_CONF ,
2007-05-08 19:22:02 +04:00
cfg | AD7418_REG_ADC_CH ( ch ) ) ;
2019-02-08 00:50:36 +03:00
if ( val < 0 )
goto abort ;
2007-05-08 19:22:02 +04:00
udelay ( 15 ) ;
2019-02-08 00:50:36 +03:00
val = i2c_smbus_read_word_swapped ( client ,
AD7418_REG_ADC ) ;
if ( val < 0 )
goto abort ;
data - > in [ data - > adc_max - 1 - i ] = val ;
2007-05-08 19:22:02 +04:00
}
/* restore old configuration value */
2019-02-08 00:50:36 +03:00
val = i2c_smbus_write_word_swapped ( client , AD7418_REG_CONF ,
cfg ) ;
if ( val < 0 )
goto abort ;
2007-05-08 19:22:02 +04:00
data - > last_updated = jiffies ;
data - > valid = 1 ;
}
mutex_unlock ( & data - > lock ) ;
2019-02-08 00:50:36 +03:00
return 0 ;
2007-05-08 19:22:02 +04:00
2019-02-08 00:50:36 +03:00
abort :
data - > valid = 0 ;
mutex_unlock ( & data - > lock ) ;
return val ;
2007-05-08 19:22:02 +04:00
}
2018-12-11 01:01:59 +03:00
static ssize_t temp_show ( struct device * dev , struct device_attribute * devattr ,
char * buf )
2007-05-08 19:22:02 +04:00
{
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2019-02-08 00:50:36 +03:00
struct ad7418_data * data = dev_get_drvdata ( dev ) ;
int ret ;
ret = ad7418_update_device ( dev ) ;
if ( ret < 0 )
return ret ;
2007-05-08 19:22:02 +04:00
return sprintf ( buf , " %d \n " ,
LM75_TEMP_FROM_REG ( data - > temp [ attr - > index ] ) ) ;
}
2018-12-11 01:01:59 +03:00
static ssize_t adc_show ( struct device * dev , struct device_attribute * devattr ,
2007-05-08 19:22:02 +04:00
char * buf )
{
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2019-02-08 00:50:36 +03:00
struct ad7418_data * data = dev_get_drvdata ( dev ) ;
int ret ;
ret = ad7418_update_device ( dev ) ;
if ( ret < 0 )
return ret ;
2007-05-08 19:22:02 +04:00
return sprintf ( buf , " %d \n " ,
( ( data - > in [ attr - > index ] > > 6 ) * 2500 + 512 ) / 1024 ) ;
}
2018-12-11 01:01:59 +03:00
static ssize_t temp_store ( struct device * dev ,
struct device_attribute * devattr , const char * buf ,
size_t count )
2007-05-08 19:22:02 +04:00
{
struct sensor_device_attribute * attr = to_sensor_dev_attr ( devattr ) ;
2014-07-01 18:40:29 +04:00
struct ad7418_data * data = dev_get_drvdata ( dev ) ;
struct i2c_client * client = data - > client ;
2012-01-08 22:34:17 +04:00
long temp ;
int ret = kstrtol ( buf , 10 , & temp ) ;
if ( ret < 0 )
return ret ;
2007-05-08 19:22:02 +04:00
mutex_lock ( & data - > lock ) ;
data - > temp [ attr - > index ] = LM75_TEMP_TO_REG ( temp ) ;
2011-11-04 15:00:47 +04:00
i2c_smbus_write_word_swapped ( client ,
AD7418_REG_TEMP [ attr - > index ] ,
data - > temp [ attr - > index ] ) ;
2007-05-08 19:22:02 +04:00
mutex_unlock ( & data - > lock ) ;
return count ;
}
2018-12-11 01:01:59 +03:00
static SENSOR_DEVICE_ATTR_RO ( temp1_input , temp , 0 ) ;
static SENSOR_DEVICE_ATTR_RW ( temp1_max_hyst , temp , 1 ) ;
static SENSOR_DEVICE_ATTR_RW ( temp1_max , temp , 2 ) ;
2007-05-08 19:22:02 +04:00
2018-12-11 01:01:59 +03:00
static SENSOR_DEVICE_ATTR_RO ( in1_input , adc , 0 ) ;
static SENSOR_DEVICE_ATTR_RO ( in2_input , adc , 1 ) ;
static SENSOR_DEVICE_ATTR_RO ( in3_input , adc , 2 ) ;
static SENSOR_DEVICE_ATTR_RO ( in4_input , adc , 3 ) ;
2007-05-08 19:22:02 +04:00
2014-07-01 18:40:29 +04:00
static struct attribute * ad7416_attrs [ ] = {
2007-05-08 19:22:02 +04:00
& sensor_dev_attr_temp1_max . dev_attr . attr ,
& sensor_dev_attr_temp1_max_hyst . dev_attr . attr ,
& sensor_dev_attr_temp1_input . dev_attr . attr ,
NULL
} ;
2014-07-01 18:40:29 +04:00
ATTRIBUTE_GROUPS ( ad7416 ) ;
2007-05-08 19:22:02 +04:00
2014-07-01 18:40:29 +04:00
static struct attribute * ad7417_attrs [ ] = {
2007-05-08 19:22:02 +04:00
& sensor_dev_attr_temp1_max . dev_attr . attr ,
& sensor_dev_attr_temp1_max_hyst . dev_attr . attr ,
& sensor_dev_attr_temp1_input . dev_attr . attr ,
& sensor_dev_attr_in1_input . dev_attr . attr ,
& sensor_dev_attr_in2_input . dev_attr . attr ,
& sensor_dev_attr_in3_input . dev_attr . attr ,
& sensor_dev_attr_in4_input . dev_attr . attr ,
NULL
} ;
2014-07-01 18:40:29 +04:00
ATTRIBUTE_GROUPS ( ad7417 ) ;
2007-05-08 19:22:02 +04:00
2014-07-01 18:40:29 +04:00
static struct attribute * ad7418_attrs [ ] = {
2007-05-08 19:22:02 +04:00
& sensor_dev_attr_temp1_max . dev_attr . attr ,
& sensor_dev_attr_temp1_max_hyst . dev_attr . attr ,
& sensor_dev_attr_temp1_input . dev_attr . attr ,
& sensor_dev_attr_in1_input . dev_attr . attr ,
NULL
} ;
2014-07-01 18:40:29 +04:00
ATTRIBUTE_GROUPS ( ad7418 ) ;
2007-05-08 19:22:02 +04:00
2014-07-01 18:39:08 +04:00
static void ad7418_init_client ( struct i2c_client * client )
{
struct ad7418_data * data = i2c_get_clientdata ( client ) ;
int reg = i2c_smbus_read_byte_data ( client , AD7418_REG_CONF ) ;
if ( reg < 0 ) {
dev_err ( & client - > dev , " cannot read configuration register \n " ) ;
} else {
dev_info ( & client - > dev , " configuring for mode 1 \n " ) ;
i2c_smbus_write_byte_data ( client , AD7418_REG_CONF , reg & 0xfe ) ;
if ( data - > type = = ad7417 | | data - > type = = ad7418 )
i2c_smbus_write_byte_data ( client ,
AD7418_REG_CONF2 , 0x00 ) ;
}
}
2020-08-13 19:02:22 +03:00
static const struct i2c_device_id ad7418_id [ ] ;
static int ad7418_probe ( struct i2c_client * client )
2007-05-08 19:22:02 +04:00
{
2014-07-01 18:40:29 +04:00
struct device * dev = & client - > dev ;
2008-07-16 21:30:08 +04:00
struct i2c_adapter * adapter = client - > adapter ;
2007-05-08 19:22:02 +04:00
struct ad7418_data * data ;
2014-07-01 18:40:29 +04:00
struct device * hwmon_dev ;
const struct attribute_group * * attr_groups = NULL ;
2007-05-08 19:22:02 +04:00
if ( ! i2c_check_functionality ( adapter , I2C_FUNC_SMBUS_BYTE_DATA |
2012-06-02 20:57:58 +04:00
I2C_FUNC_SMBUS_WORD_DATA ) )
return - EOPNOTSUPP ;
2007-05-08 19:22:02 +04:00
2014-07-01 18:40:29 +04:00
data = devm_kzalloc ( dev , sizeof ( struct ad7418_data ) , GFP_KERNEL ) ;
2012-06-02 20:57:58 +04:00
if ( ! data )
return - ENOMEM ;
2007-05-08 19:22:02 +04:00
i2c_set_clientdata ( client , data ) ;
mutex_init ( & data - > lock ) ;
2014-07-01 18:40:29 +04:00
data - > client = client ;
2019-01-29 00:06:37 +03:00
if ( dev - > of_node )
data - > type = ( enum chips ) of_device_get_match_data ( dev ) ;
else
2020-08-13 19:02:22 +03:00
data - > type = i2c_match_id ( ad7418_id , client ) - > driver_data ;
2007-05-08 19:22:02 +04:00
switch ( data - > type ) {
case ad7416 :
data - > adc_max = 0 ;
2014-07-01 18:40:29 +04:00
attr_groups = ad7416_groups ;
2007-05-08 19:22:02 +04:00
break ;
case ad7417 :
data - > adc_max = 4 ;
2014-07-01 18:40:29 +04:00
attr_groups = ad7417_groups ;
2007-05-08 19:22:02 +04:00
break ;
case ad7418 :
data - > adc_max = 1 ;
2014-07-01 18:40:29 +04:00
attr_groups = ad7418_groups ;
2007-05-08 19:22:02 +04:00
break ;
}
2014-07-01 18:40:29 +04:00
dev_info ( dev , " %s chip found \n " , client - > name ) ;
2007-05-08 19:22:02 +04:00
/* Initialize the AD7418 chip */
ad7418_init_client ( client ) ;
2014-07-01 18:40:29 +04:00
hwmon_dev = devm_hwmon_device_register_with_groups ( dev ,
client - > name ,
data , attr_groups ) ;
return PTR_ERR_OR_ZERO ( hwmon_dev ) ;
2007-05-08 19:22:02 +04:00
}
2014-07-01 18:39:08 +04:00
static const struct i2c_device_id ad7418_id [ ] = {
{ " ad7416 " , ad7416 } ,
{ " ad7417 " , ad7417 } ,
{ " ad7418 " , ad7418 } ,
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , ad7418_id ) ;
2019-01-29 00:06:37 +03:00
static const struct of_device_id ad7418_dt_ids [ ] = {
{ . compatible = " adi,ad7416 " , . data = ( void * ) ad7416 , } ,
{ . compatible = " adi,ad7417 " , . data = ( void * ) ad7417 , } ,
{ . compatible = " adi,ad7418 " , . data = ( void * ) ad7418 , } ,
{ }
} ;
MODULE_DEVICE_TABLE ( of , ad7418_dt_ids ) ;
2014-07-01 18:39:08 +04:00
static struct i2c_driver ad7418_driver = {
. driver = {
. name = " ad7418 " ,
2019-01-29 00:06:37 +03:00
. of_match_table = ad7418_dt_ids ,
2014-07-01 18:39:08 +04:00
} ,
2020-08-13 19:02:22 +03:00
. probe_new = ad7418_probe ,
2014-07-01 18:39:08 +04:00
. id_table = ad7418_id ,
} ;
2012-01-20 11:38:18 +04:00
module_i2c_driver ( ad7418_driver ) ;
2007-05-08 19:22:02 +04:00
MODULE_AUTHOR ( " Alessandro Zummo <a.zummo@towertech.it> " ) ;
MODULE_DESCRIPTION ( " AD7416/17/18 driver " ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_VERSION ( DRV_VERSION ) ;