drivers: sensor: amg88xx: Update driver to use i2c_dt_spec

Move driver to use i2c_dt_spec for I2C bus access.

Signed-off-by: Kumar Gala <galak@kernel.org>
This commit is contained in:
Kumar Gala 2022-06-13 17:42:03 -05:00 committed by Maureen Helm
commit 1fbc9632c1
3 changed files with 14 additions and 23 deletions

View file

@ -29,7 +29,7 @@ static int amg88xx_sample_fetch(const struct device *dev,
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP); __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
if (i2c_burst_read(drv_data->i2c, config->i2c_address, if (i2c_burst_read_dt(&config->i2c,
AMG88XX_OUTPUT_BASE, AMG88XX_OUTPUT_BASE,
(uint8_t *)drv_data->sample, (uint8_t *)drv_data->sample,
sizeof(drv_data->sample))) { sizeof(drv_data->sample))) {
@ -66,11 +66,10 @@ static int amg88xx_channel_get(const struct device *dev,
static int amg88xx_init_device(const struct device *dev) static int amg88xx_init_device(const struct device *dev)
{ {
struct amg88xx_data *drv_data = dev->data;
const struct amg88xx_config *config = dev->config; const struct amg88xx_config *config = dev->config;
uint8_t tmp; uint8_t tmp;
if (i2c_reg_read_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c,
AMG88XX_PCLT, &tmp)) { AMG88XX_PCLT, &tmp)) {
LOG_ERR("Failed to read Power mode"); LOG_ERR("Failed to read Power mode");
return -EIO; return -EIO;
@ -78,8 +77,7 @@ static int amg88xx_init_device(const struct device *dev)
LOG_DBG("Power mode 0x%02x", tmp); LOG_DBG("Power mode 0x%02x", tmp);
if (tmp != AMG88XX_PCLT_NORMAL_MODE) { if (tmp != AMG88XX_PCLT_NORMAL_MODE) {
if (i2c_reg_write_byte(drv_data->i2c, if (i2c_reg_write_byte_dt(&config->i2c,
config->i2c_address,
AMG88XX_PCLT, AMG88XX_PCLT,
AMG88XX_PCLT_NORMAL_MODE)) { AMG88XX_PCLT_NORMAL_MODE)) {
return -EIO; return -EIO;
@ -87,14 +85,14 @@ static int amg88xx_init_device(const struct device *dev)
k_busy_wait(AMG88XX_WAIT_MODE_CHANGE_US); k_busy_wait(AMG88XX_WAIT_MODE_CHANGE_US);
} }
if (i2c_reg_write_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
AMG88XX_RST, AMG88XX_RST_INITIAL_RST)) { AMG88XX_RST, AMG88XX_RST_INITIAL_RST)) {
return -EIO; return -EIO;
} }
k_busy_wait(AMG88XX_WAIT_INITIAL_RESET_US); k_busy_wait(AMG88XX_WAIT_INITIAL_RESET_US);
if (i2c_reg_write_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
AMG88XX_FPSC, AMG88XX_FPSC_10FPS)) { AMG88XX_FPSC, AMG88XX_FPSC_10FPS)) {
return -EIO; return -EIO;
} }
@ -106,13 +104,10 @@ static int amg88xx_init_device(const struct device *dev)
int amg88xx_init(const struct device *dev) int amg88xx_init(const struct device *dev)
{ {
struct amg88xx_data *drv_data = dev->data;
const struct amg88xx_config *config = dev->config; const struct amg88xx_config *config = dev->config;
drv_data->i2c = device_get_binding(config->i2c_name); if (!device_is_ready(config->i2c.bus)) {
if (drv_data->i2c == NULL) { LOG_ERR("Bus device is not ready");
LOG_ERR("Failed to get pointer to %s device!",
config->i2c_name);
return -EINVAL; return -EINVAL;
} }
@ -144,8 +139,7 @@ static const struct sensor_driver_api amg88xx_driver_api = {
}; };
static const struct amg88xx_config amg88xx_config = { static const struct amg88xx_config amg88xx_config = {
.i2c_name = DT_INST_BUS_LABEL(0), .i2c = I2C_DT_SPEC_INST_GET(0),
.i2c_address = DT_INST_REG_ADDR(0),
#ifdef CONFIG_AMG88XX_TRIGGER #ifdef CONFIG_AMG88XX_TRIGGER
.gpio_name = DT_INST_GPIO_LABEL(0, int_gpios), .gpio_name = DT_INST_GPIO_LABEL(0, int_gpios),
.gpio_pin = DT_INST_GPIO_PIN(0, int_gpios), .gpio_pin = DT_INST_GPIO_PIN(0, int_gpios),

View file

@ -67,17 +67,15 @@
#define AMG88XX_WAIT_INITIAL_RESET_US 2000 #define AMG88XX_WAIT_INITIAL_RESET_US 2000
struct amg88xx_config { struct amg88xx_config {
char *i2c_name; const struct i2c_dt_spec i2c;
#ifdef CONFIG_AMG88XX_TRIGGER #ifdef CONFIG_AMG88XX_TRIGGER
char *gpio_name; char *gpio_name;
uint8_t gpio_pin; uint8_t gpio_pin;
gpio_dt_flags_t gpio_flags; gpio_dt_flags_t gpio_flags;
#endif #endif
uint8_t i2c_address;
}; };
struct amg88xx_data { struct amg88xx_data {
const struct device *i2c;
int16_t sample[64]; int16_t sample[64];
#ifdef CONFIG_AMG88XX_TRIGGER #ifdef CONFIG_AMG88XX_TRIGGER

View file

@ -35,7 +35,6 @@ int amg88xx_attr_set(const struct device *dev,
enum sensor_attribute attr, enum sensor_attribute attr,
const struct sensor_value *val) const struct sensor_value *val)
{ {
struct amg88xx_data *drv_data = dev->data;
const struct amg88xx_config *config = dev->config; const struct amg88xx_config *config = dev->config;
int16_t int_level = (val->val1 * 1000000 + val->val2) / int16_t int_level = (val->val1 * 1000000 + val->val2) /
AMG88XX_TREG_LSB_SCALING; AMG88XX_TREG_LSB_SCALING;
@ -58,13 +57,13 @@ int amg88xx_attr_set(const struct device *dev,
return -ENOTSUP; return -ENOTSUP;
} }
if (i2c_reg_write_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
intl_reg, (uint8_t)int_level)) { intl_reg, (uint8_t)int_level)) {
LOG_DBG("Failed to set INTxL attribute!"); LOG_DBG("Failed to set INTxL attribute!");
return -EIO; return -EIO;
} }
if (i2c_reg_write_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
inth_reg, (uint8_t)(int_level >> 8))) { inth_reg, (uint8_t)(int_level >> 8))) {
LOG_DBG("Failed to set INTxH attribute!"); LOG_DBG("Failed to set INTxH attribute!");
return -EIO; return -EIO;
@ -94,7 +93,7 @@ static void amg88xx_thread_cb(const struct device *dev)
const struct amg88xx_config *config = dev->config; const struct amg88xx_config *config = dev->config;
uint8_t status; uint8_t status;
if (i2c_reg_read_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_read_byte_dt(&config->i2c,
AMG88XX_STAT, &status) < 0) { AMG88XX_STAT, &status) < 0) {
return; return;
} }
@ -136,7 +135,7 @@ int amg88xx_trigger_set(const struct device *dev,
struct amg88xx_data *drv_data = dev->data; struct amg88xx_data *drv_data = dev->data;
const struct amg88xx_config *config = dev->config; const struct amg88xx_config *config = dev->config;
if (i2c_reg_write_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
AMG88XX_INTC, AMG88XX_INTC_DISABLED)) { AMG88XX_INTC, AMG88XX_INTC_DISABLED)) {
return -EIO; return -EIO;
} }
@ -153,7 +152,7 @@ int amg88xx_trigger_set(const struct device *dev,
amg88xx_setup_int(drv_data, true); amg88xx_setup_int(drv_data, true);
if (i2c_reg_write_byte(drv_data->i2c, config->i2c_address, if (i2c_reg_write_byte_dt(&config->i2c,
AMG88XX_INTC, AMG88XX_INTC_ABS_MODE)) { AMG88XX_INTC, AMG88XX_INTC_ABS_MODE)) {
return -EIO; return -EIO;
} }