drivers: sensor: isl29035: Update driver to use i2c_dt_spec

Simplify driver by using i2c_dt_spec for bus access.

Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
Benjamin Björnsson 2022-06-20 20:14:45 +02:00 committed by Maureen Helm
parent 1e51107577
commit 6c8161a00d
3 changed files with 53 additions and 44 deletions

View file

@ -23,17 +23,18 @@ static int isl29035_sample_fetch(const struct device *dev,
enum sensor_channel chan)
{
struct isl29035_driver_data *drv_data = dev->data;
const struct isl29035_config *config = dev->config;
uint8_t msb, lsb;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
if (i2c_reg_read_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_DATA_MSB_REG, &msb) < 0) {
if (i2c_reg_read_byte_dt(&config->i2c,
ISL29035_DATA_MSB_REG, &msb) < 0) {
return -EIO;
}
if (i2c_reg_read_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_DATA_LSB_REG, &lsb) < 0) {
if (i2c_reg_read_byte_dt(&config->i2c, ISL29035_DATA_LSB_REG,
&lsb) < 0) {
return -EIO;
}
@ -76,58 +77,58 @@ static const struct sensor_driver_api isl29035_api = {
static int isl29035_init(const struct device *dev)
{
struct isl29035_driver_data *drv_data = dev->data;
const struct isl29035_config *config = dev->config;
drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
if (drv_data->i2c == NULL) {
LOG_DBG("Failed to get I2C device.");
return -EINVAL;
if (!device_is_ready(config->i2c.bus)) {
LOG_ERR("I2C bus device not ready");
return -ENODEV;
}
drv_data->data_sample = 0U;
/* clear blownout status bit */
if (i2c_reg_update_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_ID_REG, ISL29035_BOUT_MASK, 0) < 0) {
if (i2c_reg_update_byte_dt(&config->i2c, ISL29035_ID_REG,
ISL29035_BOUT_MASK, 0) < 0) {
LOG_DBG("Failed to clear blownout status bit.");
return -EIO;
}
/* set command registers to set default attributes */
if (i2c_reg_write_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_I_REG, 0) < 0) {
if (i2c_reg_write_byte_dt(&config->i2c,
ISL29035_COMMAND_I_REG, 0) < 0) {
LOG_DBG("Failed to clear COMMAND-I.");
return -EIO;
}
if (i2c_reg_write_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_II_REG, 0) < 0) {
if (i2c_reg_write_byte_dt(&config->i2c,
ISL29035_COMMAND_II_REG, 0) < 0) {
LOG_DBG("Failed to clear COMMAND-II.");
return -EIO;
}
/* set operation mode */
if (i2c_reg_update_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_I_REG,
ISL29035_OPMODE_MASK,
ISL29035_ACTIVE_OPMODE_BITS) < 0) {
if (i2c_reg_update_byte_dt(&config->i2c,
ISL29035_COMMAND_I_REG,
ISL29035_OPMODE_MASK,
ISL29035_ACTIVE_OPMODE_BITS) < 0) {
LOG_DBG("Failed to set opmode.");
return -EIO;
}
/* set lux range */
if (i2c_reg_update_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_II_REG,
ISL29035_LUX_RANGE_MASK,
ISL29035_LUX_RANGE_BITS) < 0) {
if (i2c_reg_update_byte_dt(&config->i2c,
ISL29035_COMMAND_II_REG,
ISL29035_LUX_RANGE_MASK,
ISL29035_LUX_RANGE_BITS) < 0) {
LOG_DBG("Failed to set lux range.");
return -EIO;
}
/* set ADC resolution */
if (i2c_reg_update_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_II_REG,
ISL29035_ADC_RES_MASK,
ISL29035_ADC_RES_BITS) < 0) {
if (i2c_reg_update_byte_dt(&config->i2c,
ISL29035_COMMAND_II_REG,
ISL29035_ADC_RES_MASK,
ISL29035_ADC_RES_BITS) < 0) {
LOG_DBG("Failed to set ADC resolution.");
return -EIO;
}
@ -142,8 +143,12 @@ static int isl29035_init(const struct device *dev)
return 0;
}
struct isl29035_driver_data isl29035_data;
static struct isl29035_driver_data isl29035_data_inst;
DEVICE_DT_INST_DEFINE(0, &isl29035_init, NULL,
&isl29035_data, NULL, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &isl29035_api);
static const struct isl29035_config isl29035_config_inst = {
.i2c = I2C_DT_SPEC_INST_GET(0),
};
DEVICE_DT_INST_DEFINE(0, &isl29035_init, NULL, &isl29035_data_inst,
&isl29035_config_inst, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &isl29035_api);

View file

@ -12,10 +12,9 @@
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/gpio.h>
#define ISL29035_I2C_ADDRESS DT_INST_REG_ADDR(0)
#define ISL29035_COMMAND_I_REG 0x00
#define ISL29035_OPMODE_SHIFT 5
#define ISL29035_OPMODE_MASK (7 << ISL29035_OPMODE_SHIFT)
@ -111,7 +110,6 @@
(ISL29035_INT_PRST_IDX << ISL29035_INT_PRST_SHIFT)
struct isl29035_driver_data {
const struct device *i2c;
uint16_t data_sample;
#if CONFIG_ISL29035_TRIGGER
@ -133,6 +131,10 @@ struct isl29035_driver_data {
#endif /* CONFIG_ISL29035_TRIGGER */
};
struct isl29035_config {
struct i2c_dt_spec i2c;
};
#ifdef CONFIG_ISL29035_TRIGGER
int isl29035_attr_set(const struct device *dev,
enum sensor_channel chan,

View file

@ -60,7 +60,7 @@ int isl29035_attr_set(const struct device *dev,
enum sensor_attribute attr,
const struct sensor_value *val)
{
struct isl29035_driver_data *drv_data = dev->data;
const struct isl29035_config *config = dev->config;
uint8_t lsb_reg, msb_reg;
uint16_t raw_val;
@ -76,10 +76,10 @@ int isl29035_attr_set(const struct device *dev,
raw_val = isl29035_lux_processed_to_raw(val);
if (i2c_reg_write_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
lsb_reg, raw_val & 0xFF) < 0 ||
i2c_reg_write_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
msb_reg, raw_val >> 8) < 0) {
if (i2c_reg_write_byte_dt(&config->i2c,
lsb_reg, raw_val & 0xFF) < 0 ||
i2c_reg_write_byte_dt(&config->i2c,
msb_reg, raw_val >> 8) < 0) {
LOG_DBG("Failed to set attribute.");
return -EIO;
}
@ -101,11 +101,12 @@ static void isl29035_gpio_callback(const struct device *dev,
static void isl29035_thread_cb(const struct device *dev)
{
struct isl29035_driver_data *drv_data = dev->data;
const struct isl29035_config *config = dev->config;
uint8_t val;
/* clear interrupt */
if (i2c_reg_read_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_I_REG, &val) < 0) {
if (i2c_reg_read_byte_dt(&config->i2c,
ISL29035_COMMAND_I_REG, &val) < 0) {
LOG_ERR("isl29035: Error reading command register");
return;
}
@ -164,12 +165,13 @@ int isl29035_trigger_set(const struct device *dev,
int isl29035_init_interrupt(const struct device *dev)
{
struct isl29035_driver_data *drv_data = dev->data;
const struct isl29035_config *config = dev->config;
/* set interrupt persistence */
if (i2c_reg_update_byte(drv_data->i2c, ISL29035_I2C_ADDRESS,
ISL29035_COMMAND_I_REG,
ISL29035_INT_PRST_MASK,
ISL29035_INT_PRST_BITS) < 0) {
if (i2c_reg_update_byte_dt(&config->i2c,
ISL29035_COMMAND_I_REG,
ISL29035_INT_PRST_MASK,
ISL29035_INT_PRST_BITS) < 0) {
LOG_DBG("Failed to set interrupt persistence cycles.");
return -EIO;
}