2019-05-27 09:55:06 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2009-09-15 19:18:11 +04:00
/* tmp421.c
*
* Copyright ( C ) 2009 Andre Prendel < andre . prendel @ gmx . de >
* Preliminary support by :
* Melvin Rook , Raymond Ng
*/
/*
* Driver for the Texas Instruments TMP421 SMBus temperature sensor IC .
2014-04-13 03:12:06 +04:00
* Supported models : TMP421 , TMP422 , TMP423 , TMP441 , TMP442
2009-09-15 19:18:11 +04:00
*/
# include <linux/module.h>
# include <linux/init.h>
# include <linux/slab.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>
2023-07-14 20:46:04 +03:00
# include <linux/of.h>
2009-09-15 19:18:11 +04:00
# include <linux/sysfs.h>
/* Addresses to scan */
2010-10-28 22:31:50 +04:00
static const unsigned short normal_i2c [ ] = { 0x2a , 0x4c , 0x4d , 0x4e , 0x4f ,
I2C_CLIENT_END } ;
2009-09-15 19:18:11 +04:00
2014-04-13 03:12:06 +04:00
enum chips { tmp421 , tmp422 , tmp423 , tmp441 , tmp442 } ;
2009-09-15 19:18:11 +04:00
2021-09-24 12:39:06 +03:00
# define MAX_CHANNELS 4
2009-09-15 19:18:11 +04:00
/* The TMP421 registers */
2014-04-23 03:13:17 +04:00
# define TMP421_STATUS_REG 0x08
2009-09-15 19:18:11 +04:00
# define TMP421_CONFIG_REG_1 0x09
2021-10-14 16:09:08 +03:00
# define TMP421_CONFIG_REG_2 0x0A
# define TMP421_CONFIG_REG_REN(x) (BIT(3 + (x)))
# define TMP421_CONFIG_REG_REN_MASK GENMASK(6, 3)
2009-09-15 19:18:11 +04:00
# define TMP421_CONVERSION_RATE_REG 0x0B
2021-10-14 16:01:28 +03:00
# define TMP421_N_FACTOR_REG_1 0x21
2009-09-15 19:18:11 +04:00
# define TMP421_MANUFACTURER_ID_REG 0xFE
# define TMP421_DEVICE_ID_REG 0xFF
2021-09-24 12:39:06 +03:00
static const u8 TMP421_TEMP_MSB [ MAX_CHANNELS ] = { 0x00 , 0x01 , 0x02 , 0x03 } ;
static const u8 TMP421_TEMP_LSB [ MAX_CHANNELS ] = { 0x10 , 0x11 , 0x12 , 0x13 } ;
2009-09-15 19:18:11 +04:00
/* Flags */
# define TMP421_CONFIG_SHUTDOWN 0x40
# define TMP421_CONFIG_RANGE 0x04
/* Manufacturer / Device ID's */
# define TMP421_MANUFACTURER_ID 0x55
# define TMP421_DEVICE_ID 0x21
# define TMP422_DEVICE_ID 0x22
# define TMP423_DEVICE_ID 0x23
2014-04-13 03:12:06 +04:00
# define TMP441_DEVICE_ID 0x41
# define TMP442_DEVICE_ID 0x42
2009-09-15 19:18:11 +04:00
static const struct i2c_device_id tmp421_id [ ] = {
2010-03-06 00:17:25 +03:00
{ " tmp421 " , 2 } ,
{ " tmp422 " , 3 } ,
{ " tmp423 " , 4 } ,
2014-04-13 03:12:06 +04:00
{ " tmp441 " , 2 } ,
{ " tmp442 " , 3 } ,
2009-09-15 19:18:11 +04:00
{ }
} ;
MODULE_DEVICE_TABLE ( i2c , tmp421_id ) ;
2019-04-04 17:50:26 +03:00
static const struct of_device_id __maybe_unused tmp421_of_match [ ] = {
2017-02-24 16:13:12 +03:00
{
. compatible = " ti,tmp421 " ,
. data = ( void * ) 2
} ,
{
. compatible = " ti,tmp422 " ,
. data = ( void * ) 3
} ,
{
. compatible = " ti,tmp423 " ,
. data = ( void * ) 4
} ,
{
. compatible = " ti,tmp441 " ,
. data = ( void * ) 2
} ,
{
2019-01-07 09:29:32 +03:00
. compatible = " ti,tmp442 " ,
2017-02-24 16:13:12 +03:00
. data = ( void * ) 3
} ,
{ } ,
} ;
MODULE_DEVICE_TABLE ( of , tmp421_of_match ) ;
2021-10-12 12:26:55 +03:00
struct tmp421_channel {
2021-10-14 16:00:49 +03:00
const char * label ;
2021-10-14 16:01:09 +03:00
bool enabled ;
2021-10-12 12:26:55 +03:00
s16 temp ;
} ;
2009-09-15 19:18:11 +04:00
struct tmp421_data {
2014-04-06 08:22:46 +04:00
struct i2c_client * client ;
2009-09-15 19:18:11 +04:00
struct mutex update_lock ;
2021-09-24 12:39:06 +03:00
u32 temp_config [ MAX_CHANNELS + 1 ] ;
2016-06-25 20:41:18 +03:00
struct hwmon_channel_info temp_info ;
const struct hwmon_channel_info * info [ 2 ] ;
struct hwmon_chip_info chip ;
2021-09-24 22:52:02 +03:00
bool valid ;
2009-09-15 19:18:11 +04:00
unsigned long last_updated ;
2017-02-24 16:13:12 +03:00
unsigned long channels ;
2009-09-15 19:18:11 +04:00
u8 config ;
2021-10-12 12:26:55 +03:00
struct tmp421_channel channel [ MAX_CHANNELS ] ;
2009-09-15 19:18:11 +04:00
} ;
2021-09-24 12:30:11 +03:00
static int temp_from_raw ( u16 reg , bool extended )
2009-09-15 19:18:11 +04:00
{
2010-03-06 00:17:25 +03:00
/* Mask out status bits */
int temp = reg & ~ 0xf ;
2009-09-15 19:18:11 +04:00
2021-09-24 12:30:11 +03:00
if ( extended )
temp = temp - 64 * 256 ;
else
temp = ( s16 ) temp ;
2009-09-15 19:18:11 +04:00
2021-09-24 12:30:11 +03:00
return DIV_ROUND_CLOSEST ( temp * 1000 , 256 ) ;
2009-09-15 19:18:11 +04:00
}
2021-09-24 12:30:09 +03:00
static int tmp421_update_device ( struct tmp421_data * data )
2009-09-15 19:18:11 +04:00
{
2014-04-06 08:22:46 +04:00
struct i2c_client * client = data - > client ;
2021-09-24 12:30:09 +03:00
int ret = 0 ;
2009-09-15 19:18:11 +04:00
int i ;
mutex_lock ( & data - > update_lock ) ;
2019-10-14 17:03:10 +03:00
if ( time_after ( jiffies , data - > last_updated + ( HZ / 2 ) ) | |
! data - > valid ) {
2021-09-24 12:30:09 +03:00
ret = i2c_smbus_read_byte_data ( client , TMP421_CONFIG_REG_1 ) ;
if ( ret < 0 )
goto exit ;
data - > config = ret ;
2009-09-15 19:18:11 +04:00
2010-03-06 00:17:25 +03:00
for ( i = 0 ; i < data - > channels ; i + + ) {
2021-09-24 12:30:09 +03:00
ret = i2c_smbus_read_byte_data ( client , TMP421_TEMP_MSB [ i ] ) ;
if ( ret < 0 )
goto exit ;
2021-10-12 12:26:55 +03:00
data - > channel [ i ] . temp = ret < < 8 ;
2021-09-24 12:30:09 +03:00
ret = i2c_smbus_read_byte_data ( client , TMP421_TEMP_LSB [ i ] ) ;
if ( ret < 0 )
goto exit ;
2021-10-12 12:26:55 +03:00
data - > channel [ i ] . temp | = ret ;
2009-09-15 19:18:11 +04:00
}
data - > last_updated = jiffies ;
2021-09-24 22:52:02 +03:00
data - > valid = true ;
2009-09-15 19:18:11 +04:00
}
2021-09-24 12:30:09 +03:00
exit :
2009-09-15 19:18:11 +04:00
mutex_unlock ( & data - > update_lock ) ;
2021-09-24 12:30:09 +03:00
if ( ret < 0 ) {
2021-09-24 22:52:02 +03:00
data - > valid = false ;
2021-09-24 12:30:09 +03:00
return ret ;
}
return 0 ;
2009-09-15 19:18:11 +04:00
}
2021-10-14 16:09:08 +03:00
static int tmp421_enable_channels ( struct tmp421_data * data )
{
int err ;
struct i2c_client * client = data - > client ;
struct device * dev = & client - > dev ;
int old = i2c_smbus_read_byte_data ( client , TMP421_CONFIG_REG_2 ) ;
int new , i ;
if ( old < 0 ) {
dev_err ( dev , " error reading register, can't disable channels \n " ) ;
return old ;
}
new = old & ~ TMP421_CONFIG_REG_REN_MASK ;
for ( i = 0 ; i < data - > channels ; i + + )
if ( data - > channel [ i ] . enabled )
new | = TMP421_CONFIG_REG_REN ( i ) ;
if ( new = = old )
return 0 ;
err = i2c_smbus_write_byte_data ( client , TMP421_CONFIG_REG_2 , new ) ;
if ( err < 0 )
dev_err ( dev , " error writing register, can't disable channels \n " ) ;
return err ;
}
2016-06-25 20:41:18 +03:00
static int tmp421_read ( struct device * dev , enum hwmon_sensor_types type ,
u32 attr , int channel , long * val )
2009-09-15 19:18:11 +04:00
{
2021-09-24 12:30:09 +03:00
struct tmp421_data * tmp421 = dev_get_drvdata ( dev ) ;
int ret = 0 ;
ret = tmp421_update_device ( tmp421 ) ;
if ( ret )
return ret ;
2016-06-25 20:41:18 +03:00
switch ( attr ) {
case hwmon_temp_input :
2021-10-14 16:09:58 +03:00
if ( ! tmp421 - > channel [ channel ] . enabled )
return - ENODATA ;
2021-10-12 12:26:55 +03:00
* val = temp_from_raw ( tmp421 - > channel [ channel ] . temp ,
2021-09-24 12:30:11 +03:00
tmp421 - > config & TMP421_CONFIG_RANGE ) ;
2016-06-25 20:41:18 +03:00
return 0 ;
case hwmon_temp_fault :
2021-10-14 16:09:58 +03:00
if ( ! tmp421 - > channel [ channel ] . enabled )
return - ENODATA ;
2016-06-25 20:41:18 +03:00
/*
2021-09-24 12:30:10 +03:00
* Any of OPEN or / PVLD bits indicate a hardware mulfunction
* and the conversion result may be incorrect
2016-06-25 20:41:18 +03:00
*/
2021-10-12 12:26:55 +03:00
* val = ! ! ( tmp421 - > channel [ channel ] . temp & 0x03 ) ;
2016-06-25 20:41:18 +03:00
return 0 ;
2021-10-14 16:09:58 +03:00
case hwmon_temp_enable :
* val = tmp421 - > channel [ channel ] . enabled ;
return 0 ;
2016-06-25 20:41:18 +03:00
default :
return - EOPNOTSUPP ;
}
2009-09-15 19:18:11 +04:00
}
2021-10-14 16:00:49 +03:00
static int tmp421_read_string ( struct device * dev , enum hwmon_sensor_types type ,
u32 attr , int channel , const char * * str )
{
struct tmp421_data * data = dev_get_drvdata ( dev ) ;
* str = data - > channel [ channel ] . label ;
return 0 ;
}
2021-10-14 16:09:58 +03:00
static int tmp421_write ( struct device * dev , enum hwmon_sensor_types type ,
u32 attr , int channel , long val )
{
struct tmp421_data * data = dev_get_drvdata ( dev ) ;
int ret ;
switch ( attr ) {
case hwmon_temp_enable :
data - > channel [ channel ] . enabled = val ;
ret = tmp421_enable_channels ( data ) ;
break ;
default :
ret = - EOPNOTSUPP ;
}
return ret ;
}
2016-06-25 20:41:18 +03:00
static umode_t tmp421_is_visible ( const void * data , enum hwmon_sensor_types type ,
u32 attr , int channel )
2009-09-15 19:18:11 +04:00
{
2016-06-25 20:41:18 +03:00
switch ( attr ) {
case hwmon_temp_fault :
case hwmon_temp_input :
2018-12-11 01:02:23 +03:00
return 0444 ;
2021-10-14 16:00:49 +03:00
case hwmon_temp_label :
return 0444 ;
2021-10-14 16:09:58 +03:00
case hwmon_temp_enable :
return 0644 ;
2016-06-25 20:41:18 +03:00
default :
return 0 ;
}
2009-09-15 19:18:11 +04:00
}
2021-10-14 16:09:08 +03:00
static int tmp421_init_client ( struct tmp421_data * data )
2009-09-15 19:18:11 +04:00
{
int config , config_orig ;
2021-10-14 16:09:08 +03:00
struct i2c_client * client = data - > client ;
2009-09-15 19:18:11 +04:00
/* Set the conversion rate to 2 Hz */
i2c_smbus_write_byte_data ( client , TMP421_CONVERSION_RATE_REG , 0x05 ) ;
/* Start conversions (disable shutdown if necessary) */
config = i2c_smbus_read_byte_data ( client , TMP421_CONFIG_REG_1 ) ;
if ( config < 0 ) {
2013-01-10 22:01:24 +04:00
dev_err ( & client - > dev ,
" Could not read configuration register (%d) \n " , config ) ;
2013-09-11 08:19:52 +04:00
return config ;
2009-09-15 19:18:11 +04:00
}
config_orig = config ;
config & = ~ TMP421_CONFIG_SHUTDOWN ;
if ( config ! = config_orig ) {
dev_info ( & client - > dev , " Enable monitoring chip \n " ) ;
i2c_smbus_write_byte_data ( client , TMP421_CONFIG_REG_1 , config ) ;
}
2021-10-14 16:09:08 +03:00
return tmp421_enable_channels ( data ) ;
2009-09-15 19:18:11 +04:00
}
2009-12-14 23:17:23 +03:00
static int tmp421_detect ( struct i2c_client * client ,
2009-09-15 19:18:11 +04:00
struct i2c_board_info * info )
{
2009-12-09 22:35:54 +03:00
enum chips kind ;
2009-09-15 19:18:11 +04:00
struct i2c_adapter * adapter = client - > adapter ;
2018-10-09 15:11:57 +03:00
static const char * const names [ ] = {
" TMP421 " , " TMP422 " , " TMP423 " ,
" TMP441 " , " TMP442 "
} ;
2014-04-23 03:13:17 +04:00
int addr = client - > addr ;
2009-12-09 22:35:54 +03:00
u8 reg ;
2009-09-15 19:18:11 +04:00
if ( ! i2c_check_functionality ( adapter , I2C_FUNC_SMBUS_BYTE_DATA ) )
return - ENODEV ;
2009-12-09 22:35:54 +03:00
reg = i2c_smbus_read_byte_data ( client , TMP421_MANUFACTURER_ID_REG ) ;
if ( reg ! = TMP421_MANUFACTURER_ID )
return - ENODEV ;
2014-04-23 03:13:17 +04:00
reg = i2c_smbus_read_byte_data ( client , TMP421_CONVERSION_RATE_REG ) ;
if ( reg & 0xf8 )
return - ENODEV ;
reg = i2c_smbus_read_byte_data ( client , TMP421_STATUS_REG ) ;
if ( reg & 0x7f )
return - ENODEV ;
2009-12-09 22:35:54 +03:00
reg = i2c_smbus_read_byte_data ( client , TMP421_DEVICE_ID_REG ) ;
switch ( reg ) {
case TMP421_DEVICE_ID :
kind = tmp421 ;
break ;
case TMP422_DEVICE_ID :
2014-04-23 03:13:17 +04:00
if ( addr = = 0x2a )
return - ENODEV ;
2009-12-09 22:35:54 +03:00
kind = tmp422 ;
break ;
case TMP423_DEVICE_ID :
2014-04-23 03:13:17 +04:00
if ( addr ! = 0x4c & & addr ! = 0x4d )
return - ENODEV ;
2009-12-09 22:35:54 +03:00
kind = tmp423 ;
break ;
2014-04-13 03:12:06 +04:00
case TMP441_DEVICE_ID :
kind = tmp441 ;
break ;
case TMP442_DEVICE_ID :
if ( addr ! = 0x4c & & addr ! = 0x4d )
return - ENODEV ;
kind = tmp442 ;
break ;
2009-12-09 22:35:54 +03:00
default :
return - ENODEV ;
2009-09-15 19:18:11 +04:00
}
2009-12-09 22:35:54 +03:00
2022-08-19 00:00:11 +03:00
strscpy ( info - > type , tmp421_id [ kind ] . name , I2C_NAME_SIZE ) ;
2009-09-15 19:18:11 +04:00
dev_info ( & adapter - > dev , " Detected TI %s chip at 0x%02x \n " ,
2010-03-06 00:17:26 +03:00
names [ kind ] , client - > addr ) ;
2009-09-15 19:18:11 +04:00
return 0 ;
}
2021-10-14 16:00:49 +03:00
static int tmp421_probe_child_from_dt ( struct i2c_client * client ,
struct device_node * child ,
struct tmp421_data * data )
{
struct device * dev = & client - > dev ;
u32 i ;
2021-10-14 16:01:28 +03:00
s32 val ;
2021-10-14 16:00:49 +03:00
int err ;
err = of_property_read_u32 ( child , " reg " , & i ) ;
if ( err ) {
dev_err ( dev , " missing reg property of %pOFn \n " , child ) ;
return err ;
}
if ( i > = data - > channels ) {
dev_err ( dev , " invalid reg %d of %pOFn \n " , i , child ) ;
return - EINVAL ;
}
of_property_read_string ( child , " label " , & data - > channel [ i ] . label ) ;
if ( data - > channel [ i ] . label )
data - > temp_config [ i ] | = HWMON_T_LABEL ;
2021-10-14 16:01:09 +03:00
data - > channel [ i ] . enabled = of_device_is_available ( child ) ;
2021-10-14 16:01:28 +03:00
err = of_property_read_s32 ( child , " ti,n-factor " , & val ) ;
if ( ! err ) {
if ( i = = 0 ) {
dev_err ( dev , " n-factor can't be set for internal channel \n " ) ;
return - EINVAL ;
}
if ( val > 127 | | val < - 128 ) {
dev_err ( dev , " n-factor for channel %d invalid (%d) \n " ,
i , val ) ;
return - EINVAL ;
}
i2c_smbus_write_byte_data ( client , TMP421_N_FACTOR_REG_1 + i - 1 ,
val ) ;
}
2021-10-14 16:00:49 +03:00
return 0 ;
}
static int tmp421_probe_from_dt ( struct i2c_client * client , struct tmp421_data * data )
{
struct device * dev = & client - > dev ;
const struct device_node * np = dev - > of_node ;
struct device_node * child ;
int err ;
for_each_child_of_node ( np , child ) {
2021-10-14 16:11:02 +03:00
if ( strcmp ( child - > name , " channel " ) )
continue ;
2021-10-14 16:00:49 +03:00
err = tmp421_probe_child_from_dt ( client , child , data ) ;
2021-10-18 15:15:37 +03:00
if ( err ) {
of_node_put ( child ) ;
2021-10-14 16:00:49 +03:00
return err ;
2021-10-18 15:15:37 +03:00
}
2021-10-14 16:00:49 +03:00
}
return 0 ;
}
2016-06-25 20:41:18 +03:00
static const struct hwmon_ops tmp421_ops = {
. is_visible = tmp421_is_visible ,
. read = tmp421_read ,
2021-10-14 16:00:49 +03:00
. read_string = tmp421_read_string ,
2021-10-14 16:09:58 +03:00
. write = tmp421_write ,
2016-06-25 20:41:18 +03:00
} ;
2020-08-13 19:02:22 +03:00
static int tmp421_probe ( struct i2c_client * client )
2009-09-15 19:18:11 +04:00
{
2014-04-06 08:22:46 +04:00
struct device * dev = & client - > dev ;
struct device * hwmon_dev ;
2009-09-15 19:18:11 +04:00
struct tmp421_data * data ;
2016-06-25 20:41:18 +03:00
int i , err ;
2009-09-15 19:18:11 +04:00
2014-04-06 08:22:46 +04:00
data = devm_kzalloc ( dev , sizeof ( struct tmp421_data ) , GFP_KERNEL ) ;
2009-09-15 19:18:11 +04:00
if ( ! data )
return - ENOMEM ;
mutex_init ( & data - > update_lock ) ;
2017-02-24 16:13:12 +03:00
if ( client - > dev . of_node )
data - > channels = ( unsigned long )
of_device_get_match_data ( & client - > dev ) ;
else
2020-08-13 19:02:22 +03:00
data - > channels = i2c_match_id ( tmp421_id , client ) - > driver_data ;
2014-04-06 08:22:46 +04:00
data - > client = client ;
2009-09-15 19:18:11 +04:00
2021-10-14 16:01:09 +03:00
for ( i = 0 ; i < data - > channels ; i + + ) {
2021-10-14 16:09:58 +03:00
data - > temp_config [ i ] = HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_ENABLE ;
2021-10-14 16:01:09 +03:00
data - > channel [ i ] . enabled = true ;
}
2016-06-25 20:41:18 +03:00
2021-10-14 16:00:49 +03:00
err = tmp421_probe_from_dt ( client , data ) ;
if ( err )
return err ;
2021-10-14 16:09:08 +03:00
err = tmp421_init_client ( data ) ;
if ( err )
return err ;
2016-06-25 20:41:18 +03:00
data - > chip . ops = & tmp421_ops ;
data - > chip . info = data - > info ;
data - > info [ 0 ] = & data - > temp_info ;
data - > temp_info . type = hwmon_temp ;
data - > temp_info . config = data - > temp_config ;
hwmon_dev = devm_hwmon_device_register_with_info ( dev , client - > name ,
data ,
& data - > chip ,
NULL ) ;
2014-04-06 08:22:46 +04:00
return PTR_ERR_OR_ZERO ( hwmon_dev ) ;
2009-09-15 19:18:11 +04:00
}
static struct i2c_driver tmp421_driver = {
. class = I2C_CLASS_HWMON ,
. driver = {
. name = " tmp421 " ,
2017-02-24 16:13:12 +03:00
. of_match_table = of_match_ptr ( tmp421_of_match ) ,
2009-09-15 19:18:11 +04:00
} ,
2023-05-05 16:17:18 +03:00
. probe = tmp421_probe ,
2009-09-15 19:18:11 +04:00
. id_table = tmp421_id ,
. detect = tmp421_detect ,
2009-12-14 23:17:25 +03:00
. address_list = normal_i2c ,
2009-09-15 19:18:11 +04:00
} ;
2012-01-20 11:38:18 +04:00
module_i2c_driver ( tmp421_driver ) ;
2009-09-15 19:18:11 +04:00
MODULE_AUTHOR ( " Andre Prendel <andre.prendel@gmx.de> " ) ;
2014-04-13 03:12:06 +04:00
MODULE_DESCRIPTION ( " Texas Instruments TMP421/422/423/441/442 temperature sensor driver " ) ;
2009-09-15 19:18:11 +04:00
MODULE_LICENSE ( " GPL " ) ;