@ -4,31 +4,31 @@
@@ -4,31 +4,31 @@
* SPDX - License - Identifier : Apache - 2.0
*/
# define DT_DRV_COMPAT ti_tmp116
# define DT_DRV_COMPAT ti_tmp11x
# include <zephyr/device.h>
# include <zephyr/drivers/i2c.h>
# include <zephyr/drivers/sensor.h>
# include <zephyr/drivers/sensor/tmp116 .h>
# include <zephyr/dt-bindings/sensor/tmp116 .h>
# include <zephyr/drivers/sensor/tmp11x .h>
# include <zephyr/dt-bindings/sensor/tmp11x .h>
# include <zephyr/sys/util.h>
# include <zephyr/sys/byteorder.h>
# include <zephyr/sys/__assert.h>
# include <zephyr/logging/log.h>
# include <zephyr/kernel.h>
# include "tmp116 .h"
# include "tmp11x .h"
# define EEPROM_SIZE_REG sizeof(uint16_t)
# define EEPROM_TMP117_RESERVED (2 * sizeof(uint16_t))
# define EEPROM_MIN_BUSY_MS 7
LOG_MODULE_REGISTER ( TMP116 , CONFIG_SENSOR_LOG_LEVEL ) ;
LOG_MODULE_REGISTER ( TMP11X , CONFIG_SENSOR_LOG_LEVEL ) ;
static int tmp116 _reg_read ( const struct device * dev , uint8_t reg ,
static int tmp11x _reg_read ( const struct device * dev , uint8_t reg ,
uint16_t * val )
{
const struct tmp116 _dev_config * cfg = dev - > config ;
const struct tmp11x _dev_config * cfg = dev - > config ;
if ( i2c_burst_read_dt ( & cfg - > bus , reg , ( uint8_t * ) val , 2 )
< 0 ) {
@ -40,21 +40,21 @@ static int tmp116_reg_read(const struct device *dev, uint8_t reg,
@@ -40,21 +40,21 @@ static int tmp116_reg_read(const struct device *dev, uint8_t reg,
return 0 ;
}
static int tmp116 _reg_write ( const struct device * dev , uint8_t reg ,
static int tmp11x _reg_write ( const struct device * dev , uint8_t reg ,
uint16_t val )
{
const struct tmp116 _dev_config * cfg = dev - > config ;
const struct tmp11x _dev_config * cfg = dev - > config ;
uint8_t tx_buf [ 3 ] = { reg , val > > 8 , val & 0xFF } ;
return i2c_write_dt ( & cfg - > bus , tx_buf , sizeof ( tx_buf ) ) ;
}
int tmp116 _write_config ( const struct device * dev , uint16_t mask , uint16_t conf )
int tmp11x _write_config ( const struct device * dev , uint16_t mask , uint16_t conf )
{
uint16_t config = 0 ;
int result ;
result = tmp116 _reg_read ( dev , TMP116 _REG_CFGR , & config ) ;
result = tmp11x _reg_read ( dev , TMP11X _REG_CFGR , & config ) ;
if ( result < 0 ) {
return result ;
@ -63,15 +63,15 @@ int tmp116_write_config(const struct device *dev, uint16_t mask, uint16_t conf)
@@ -63,15 +63,15 @@ int tmp116_write_config(const struct device *dev, uint16_t mask, uint16_t conf)
config & = ~ mask ;
config | = conf ;
return tmp116 _reg_write ( dev , TMP116 _REG_CFGR , config ) ;
return tmp11x _reg_write ( dev , TMP11X _REG_CFGR , config ) ;
}
static bool check_eeprom_bounds ( const struct device * dev , off_t offset ,
size_t len )
{
struct tmp116 _data * drv_data = dev - > data ;
struct tmp11x _data * drv_data = dev - > data ;
if ( ( offset + len ) > EEPROM_TMP116 _SIZE | |
if ( ( offset + len ) > EEPROM_TMP11X _SIZE | |
offset % EEPROM_SIZE_REG ! = 0 | |
len % EEPROM_SIZE_REG ! = 0 ) {
return false ;
@ -86,7 +86,7 @@ static bool check_eeprom_bounds(const struct device *dev, off_t offset,
@@ -86,7 +86,7 @@ static bool check_eeprom_bounds(const struct device *dev, off_t offset,
return true ;
}
int tmp116 _eeprom_write ( const struct device * dev , off_t offset ,
int tmp11x _eeprom_write ( const struct device * dev , off_t offset ,
const void * data , size_t len )
{
uint8_t reg ;
@ -97,7 +97,7 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
@@ -97,7 +97,7 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
return - EINVAL ;
}
res = tmp116 _reg_write ( dev , TMP116_REG_EEPROM_UL , TMP116 _EEPROM_UL_UNLOCK ) ;
res = tmp11x _reg_write ( dev , TMP11X_REG_EEPROM_UL , TMP11X _EEPROM_UL_UNLOCK ) ;
if ( res ) {
return res ;
}
@ -105,7 +105,7 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
@@ -105,7 +105,7 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
for ( reg = ( offset / 2 ) ; reg < offset / 2 + len / 2 ; reg + + ) {
uint16_t val = * src ;
res = tmp116 _reg_write ( dev , reg + TMP116 _REG_EEPROM1 , val ) ;
res = tmp11x _reg_write ( dev , reg + TMP11X _REG_EEPROM1 , val ) ;
if ( res ! = 0 ) {
break ;
}
@ -113,11 +113,11 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
@@ -113,11 +113,11 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
k_sleep ( K_MSEC ( EEPROM_MIN_BUSY_MS ) ) ;
do {
res = tmp116 _reg_read ( dev , TMP116 _REG_EEPROM_UL , & val ) ;
res = tmp11x _reg_read ( dev , TMP11X _REG_EEPROM_UL , & val ) ;
if ( res ! = 0 ) {
break ;
}
} while ( val & TMP116 _EEPROM_UL_BUSY ) ;
} while ( val & TMP11X _EEPROM_UL_BUSY ) ;
src + + ;
if ( res ! = 0 ) {
@ -125,12 +125,12 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
@@ -125,12 +125,12 @@ int tmp116_eeprom_write(const struct device *dev, off_t offset,
}
}
res = tmp116 _reg_write ( dev , TMP116 _REG_EEPROM_UL , 0 ) ;
res = tmp11x _reg_write ( dev , TMP11X _REG_EEPROM_UL , 0 ) ;
return res ;
}
int tmp116 _eeprom_read ( const struct device * dev , off_t offset , void * data ,
int tmp11x _eeprom_read ( const struct device * dev , off_t offset , void * data ,
size_t len )
{
uint8_t reg ;
@ -142,7 +142,7 @@ int tmp116_eeprom_read(const struct device *dev, off_t offset, void *data,
@@ -142,7 +142,7 @@ int tmp116_eeprom_read(const struct device *dev, off_t offset, void *data,
}
for ( reg = ( offset / 2 ) ; reg < offset / 2 + len / 2 ; reg + + ) {
res = tmp116 _reg_read ( dev , reg + TMP116 _REG_EEPROM1 , dst ) ;
res = tmp11x _reg_read ( dev , reg + TMP11X _REG_EEPROM1 , dst ) ;
if ( res ! = 0 ) {
break ;
}
@ -162,9 +162,9 @@ int tmp116_eeprom_read(const struct device *dev, off_t offset, void *data,
@@ -162,9 +162,9 @@ int tmp116_eeprom_read(const struct device *dev, off_t offset, void *data,
* @ retval 0 on success
* @ retval - EIO Otherwise
*/
static inline int tmp116 _device_id_check ( const struct device * dev , uint16_t * id )
static inline int tmp11x _device_id_check ( const struct device * dev , uint16_t * id )
{
if ( tmp116 _reg_read ( dev , TMP116 _REG_DEVICE_ID , id ) ! = 0 ) {
if ( tmp11x _reg_read ( dev , TMP11X _REG_DEVICE_ID , id ) ! = 0 ) {
LOG_ERR ( " %s: Failed to get Device ID register! " ,
dev - > name ) ;
return - EIO ;
@ -179,10 +179,10 @@ static inline int tmp116_device_id_check(const struct device *dev, uint16_t *id)
@@ -179,10 +179,10 @@ static inline int tmp116_device_id_check(const struct device *dev, uint16_t *id)
return 0 ;
}
static int tmp116 _sample_fetch ( const struct device * dev ,
static int tmp11x _sample_fetch ( const struct device * dev ,
enum sensor_channel chan )
{
struct tmp116 _data * drv_data = dev - > data ;
struct tmp11x _data * drv_data = dev - > data ;
uint16_t value ;
uint16_t cfg_reg = 0 ;
int rc ;
@ -194,20 +194,20 @@ static int tmp116_sample_fetch(const struct device *dev,
@@ -194,20 +194,20 @@ static int tmp116_sample_fetch(const struct device *dev,
drv_data - > sample = 0U ;
/* Make sure that a data is available */
rc = tmp116 _reg_read ( dev , TMP116 _REG_CFGR , & cfg_reg ) ;
rc = tmp11x _reg_read ( dev , TMP11X _REG_CFGR , & cfg_reg ) ;
if ( rc < 0 ) {
LOG_ERR ( " %s, Failed to read from CFGR register " ,
dev - > name ) ;
return rc ;
}
if ( ( cfg_reg & TMP116 _CFGR_DATA_READY ) = = 0 ) {
if ( ( cfg_reg & TMP11X _CFGR_DATA_READY ) = = 0 ) {
LOG_DBG ( " %s: no data ready " , dev - > name ) ;
return - EBUSY ;
}
/* Get the most recent temperature measurement */
rc = tmp116 _reg_read ( dev , TMP116 _REG_TEMP , & value ) ;
rc = tmp11x _reg_read ( dev , TMP11X _REG_TEMP , & value ) ;
if ( rc < 0 ) {
LOG_ERR ( " %s: Failed to read from TEMP register! " ,
dev - > name ) ;
@ -220,11 +220,11 @@ static int tmp116_sample_fetch(const struct device *dev,
@@ -220,11 +220,11 @@ static int tmp116_sample_fetch(const struct device *dev,
return 0 ;
}
static int tmp116 _channel_get ( const struct device * dev ,
static int tmp11x _channel_get ( const struct device * dev ,
enum sensor_channel chan ,
struct sensor_value * val )
{
struct tmp116 _data * drv_data = dev - > data ;
struct tmp11x _data * drv_data = dev - > data ;
int32_t tmp ;
if ( chan ! = SENSOR_CHAN_AMBIENT_TEMP ) {
@ -235,46 +235,46 @@ static int tmp116_channel_get(const struct device *dev,
@@ -235,46 +235,46 @@ static int tmp116_channel_get(const struct device *dev,
* See datasheet " Temperature Results and Limits " section for more
* details on processing sample data .
*/
tmp = ( ( int16_t ) drv_data - > sample * ( int32_t ) TMP116 _RESOLUTION ) / 10 ;
tmp = ( ( int16_t ) drv_data - > sample * ( int32_t ) TMP11X _RESOLUTION ) / 10 ;
val - > val1 = tmp / 1000000 ; /* uCelsius */
val - > val2 = tmp % 1000000 ;
return 0 ;
}
static int16_t tmp116 _conv_value ( const struct sensor_value * val )
static int16_t tmp11x _conv_value ( const struct sensor_value * val )
{
uint32_t freq_micro = sensor_value_to_micro ( val ) ;
switch ( freq_micro ) {
case 64000000 : /* 1 / 15.5 ms has been rounded down */
return TMP116 _DT_ODR_15_5_MS ;
return TMP11X _DT_ODR_15_5_MS ;
case 8000000 :
return TMP116 _DT_ODR_125_MS ;
return TMP11X _DT_ODR_125_MS ;
case 4000000 :
return TMP116 _DT_ODR_250_MS ;
return TMP11X _DT_ODR_250_MS ;
case 2000000 :
return TMP116 _DT_ODR_500_MS ;
return TMP11X _DT_ODR_500_MS ;
case 1000000 :
return TMP116 _DT_ODR_1000_MS ;
return TMP11X _DT_ODR_1000_MS ;
case 250000 :
return TMP116 _DT_ODR_4000_MS ;
return TMP11X _DT_ODR_4000_MS ;
case 125000 :
return TMP116 _DT_ODR_8000_MS ;
return TMP11X _DT_ODR_8000_MS ;
case 62500 :
return TMP116 _DT_ODR_16000_MS ;
return TMP11X _DT_ODR_16000_MS ;
default :
LOG_ERR ( " % " PRIu32 " uHz not supported " , freq_micro ) ;
return - EINVAL ;
}
}
static int tmp116 _attr_set ( const struct device * dev ,
static int tmp11x _attr_set ( const struct device * dev ,
enum sensor_channel chan ,
enum sensor_attribute attr ,
const struct sensor_value * val )
{
struct tmp116 _data * drv_data = dev - > data ;
struct tmp11x _data * drv_data = dev - > data ;
int16_t value ;
uint16_t avg ;
@ -284,12 +284,12 @@ static int tmp116_attr_set(const struct device *dev,
@@ -284,12 +284,12 @@ static int tmp116_attr_set(const struct device *dev,
switch ( ( int ) attr ) {
case SENSOR_ATTR_SAMPLING_FREQUENCY :
value = tmp116 _conv_value ( val ) ;
value = tmp11x _conv_value ( val ) ;
if ( value < 0 ) {
return value ;
}
return tmp116 _write_config ( dev , TMP116 _CFGR_CONV , value ) ;
return tmp11x _write_config ( dev , TMP11X _CFGR_CONV , value ) ;
case SENSOR_ATTR_OFFSET :
if ( drv_data - > id ! = TMP117_DEVICE_ID & & drv_data - > id ! = TMP119_DEVICE_ID ) {
@ -301,48 +301,48 @@ static int tmp116_attr_set(const struct device *dev,
@@ -301,48 +301,48 @@ static int tmp116_attr_set(const struct device *dev,
* The offset is encoded into the temperature register format .
*/
value = ( ( ( val - > val1 ) * 10000000 ) + ( ( val - > val2 ) * 10 ) )
/ ( int32_t ) TMP116 _RESOLUTION ;
/ ( int32_t ) TMP11X _RESOLUTION ;
return tmp116 _reg_write ( dev , TMP117_REG_TEMP_OFFSET , value ) ;
return tmp11x _reg_write ( dev , TMP117_REG_TEMP_OFFSET , value ) ;
case SENSOR_ATTR_OVERSAMPLING :
/* sensor supports averaging 1, 8, 32 and 64 samples */
switch ( val - > val1 ) {
case 1 :
avg = TMP116 _AVG_1_SAMPLE ;
avg = TMP11X _AVG_1_SAMPLE ;
break ;
case 8 :
avg = TMP116 _AVG_8_SAMPLES ;
avg = TMP11X _AVG_8_SAMPLES ;
break ;
case 32 :
avg = TMP116 _AVG_32_SAMPLES ;
avg = TMP11X _AVG_32_SAMPLES ;
break ;
case 64 :
avg = TMP116 _AVG_64_SAMPLES ;
avg = TMP11X _AVG_64_SAMPLES ;
break ;
default :
return - EINVAL ;
}
return tmp116 _write_config ( dev , TMP116 _CFGR_AVG , avg ) ;
case SENSOR_ATTR_TMP116 _SHUTDOWN_MODE :
return tmp116 _write_config ( dev , TMP116_CFGR_MODE , TMP116 _MODE_SHUTDOWN ) ;
return tmp11x _write_config ( dev , TMP11X _CFGR_AVG , avg ) ;
case SENSOR_ATTR_TMP11X _SHUTDOWN_MODE :
return tmp11x _write_config ( dev , TMP11X_CFGR_MODE , TMP11X _MODE_SHUTDOWN ) ;
case SENSOR_ATTR_TMP116 _CONTINUOUS_CONVERSION_MODE :
return tmp116 _write_config ( dev , TMP116_CFGR_MODE , TMP116 _MODE_CONTINUOUS ) ;
case SENSOR_ATTR_TMP11X _CONTINUOUS_CONVERSION_MODE :
return tmp11x _write_config ( dev , TMP11X_CFGR_MODE , TMP11X _MODE_CONTINUOUS ) ;
case SENSOR_ATTR_TMP116 _ONE_SHOT_MODE :
return tmp116 _write_config ( dev , TMP116_CFGR_MODE , TMP116 _MODE_ONE_SHOT ) ;
case SENSOR_ATTR_TMP11X _ONE_SHOT_MODE :
return tmp11x _write_config ( dev , TMP11X_CFGR_MODE , TMP11X _MODE_ONE_SHOT ) ;
default :
return - ENOTSUP ;
}
}
static int tmp116 _attr_get ( const struct device * dev , enum sensor_channel chan ,
static int tmp11x _attr_get ( const struct device * dev , enum sensor_channel chan ,
enum sensor_attribute attr , struct sensor_value * val )
{
uint16_t data ;
@ -354,7 +354,7 @@ static int tmp116_attr_get(const struct device *dev, enum sensor_channel chan,
@@ -354,7 +354,7 @@ static int tmp116_attr_get(const struct device *dev, enum sensor_channel chan,
switch ( attr ) {
case SENSOR_ATTR_CONFIGURATION :
rc = tmp116 _reg_read ( dev , TMP116 _REG_CFGR , & data ) ;
rc = tmp11x _reg_read ( dev , TMP11X _REG_CFGR , & data ) ;
if ( rc < 0 ) {
return rc ;
}
@ -369,17 +369,17 @@ static int tmp116_attr_get(const struct device *dev, enum sensor_channel chan,
@@ -369,17 +369,17 @@ static int tmp116_attr_get(const struct device *dev, enum sensor_channel chan,
return 0 ;
}
static DEVICE_API ( sensor , tmp116 _driver_api ) = {
. attr_set = tmp116 _attr_set ,
. attr_get = tmp116 _attr_get ,
. sample_fetch = tmp116 _sample_fetch ,
. channel_get = tmp116 _channel_get
static DEVICE_API ( sensor , tmp11x _driver_api ) = {
. attr_set = tmp11x _attr_set ,
. attr_get = tmp11x _attr_get ,
. sample_fetch = tmp11x _sample_fetch ,
. channel_get = tmp11x _channel_get
} ;
static int tmp116 _init ( const struct device * dev )
static int tmp11x _init ( const struct device * dev )
{
struct tmp116 _data * drv_data = dev - > data ;
const struct tmp116 _dev_config * cfg = dev - > config ;
struct tmp11x _data * drv_data = dev - > data ;
const struct tmp11x _dev_config * cfg = dev - > config ;
int rc ;
uint16_t id ;
@ -389,32 +389,32 @@ static int tmp116_init(const struct device *dev)
@@ -389,32 +389,32 @@ static int tmp116_init(const struct device *dev)
}
/* Check the Device ID */
rc = tmp116 _device_id_check ( dev , & id ) ;
rc = tmp11x _device_id_check ( dev , & id ) ;
if ( rc < 0 ) {
return rc ;
}
LOG_DBG ( " Got device ID: %x " , id ) ;
drv_data - > id = id ;
rc = tmp116 _write_config ( dev , TMP116 _CFGR_CONV , cfg - > odr ) ;
rc = tmp11x _write_config ( dev , TMP11X _CFGR_CONV , cfg - > odr ) ;
if ( rc < 0 ) {
return rc ;
}
rc = tmp116 _write_config ( dev , TMP116 _CFGR_AVG , cfg - > oversampling ) ;
rc = tmp11x _write_config ( dev , TMP11X _CFGR_AVG , cfg - > oversampling ) ;
return rc ;
}
# define DEFINE_TMP116 (_num) \
static struct tmp116_data tmp116 _data_ # # _num ; \
static const struct tmp116_dev_config tmp116 _config_ # # _num = { \
# define DEFINE_TMP11X (_num) \
static struct tmp11x_data tmp11x _data_ # # _num ; \
static const struct tmp11x_dev_config tmp11x _config_ # # _num = { \
. bus = I2C_DT_SPEC_INST_GET ( _num ) , \
. odr = DT_INST_PROP ( _num , odr ) , \
. oversampling = DT_INST_PROP ( _num , oversampling ) , \
} ; \
SENSOR_DEVICE_DT_INST_DEFINE ( _num , tmp116 _init , NULL , \
& tmp116 _data_ # # _num , & tmp116 _config_ # # _num , POST_KERNEL , \
CONFIG_SENSOR_INIT_PRIORITY , & tmp116 _driver_api ) ;
SENSOR_DEVICE_DT_INST_DEFINE ( _num , tmp11x _init , NULL , \
& tmp11x _data_ # # _num , & tmp11x _config_ # # _num , POST_KERNEL , \
CONFIG_SENSOR_INIT_PRIORITY , & tmp11x _driver_api ) ;
DT_INST_FOREACH_STATUS_OKAY ( DEFINE_TMP116 )
DT_INST_FOREACH_STATUS_OKAY ( DEFINE_TMP11X )