drivers: sensor: vcnl4040: Update driver to use gpio_dt_spec
Move driver to use gpio_dt_spec for GPIO interrupt handling. Added check to vcnl4040_trigger_set to handle case of trigger mode enabled but the devicetree doesn't have an int_gpios property. Also, moved up the checking/handling of gpio port device in vcnl4040_trigger_init so that if we error out its before we setup any threads and such. Signed-off-by: Kumar Gala <galak@kernel.org>
This commit is contained in:
parent
a31c6070de
commit
97f0570c67
3 changed files with 31 additions and 35 deletions
|
@ -341,15 +341,7 @@ static const struct sensor_driver_api vcnl4040_driver_api = {
|
|||
static const struct vcnl4040_config vcnl4040_config = {
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
#ifdef CONFIG_VCNL4040_TRIGGER
|
||||
#if DT_INST_NODE_HAS_PROP(0, int_gpios)
|
||||
.gpio_name = DT_INST_GPIO_LABEL(0, int_gpios),
|
||||
.gpio_pin = DT_INST_GPIO_PIN(0, int_gpios),
|
||||
.gpio_flags = DT_INST_GPIO_FLAGS(0, int_gpios),
|
||||
#else
|
||||
.gpio_name = NULL,
|
||||
.gpio_pin = 0,
|
||||
.gpio_flags = 0,
|
||||
#endif
|
||||
.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),
|
||||
#endif
|
||||
.led_i = DT_INST_ENUM_IDX(0, led_current),
|
||||
.led_dc = DT_INST_ENUM_IDX(0, led_duty_cycle),
|
||||
|
|
|
@ -92,9 +92,7 @@ enum interrupt_type {
|
|||
struct vcnl4040_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
#ifdef CONFIG_VCNL4040_TRIGGER
|
||||
const char *gpio_name;
|
||||
gpio_pin_t gpio_pin;
|
||||
gpio_dt_flags_t gpio_flags;
|
||||
struct gpio_dt_spec int_gpio;
|
||||
#endif
|
||||
enum led_current led_i;
|
||||
enum led_duty_cycle led_dc;
|
||||
|
@ -107,8 +105,6 @@ struct vcnl4040_data {
|
|||
struct k_sem sem;
|
||||
#ifdef CONFIG_VCNL4040_TRIGGER
|
||||
const struct device *dev;
|
||||
const struct device *gpio;
|
||||
uint8_t gpio_pin;
|
||||
struct gpio_callback gpio_cb;
|
||||
enum interrupt_type int_type;
|
||||
sensor_trigger_handler_t proxy_handler;
|
||||
|
|
|
@ -11,8 +11,9 @@ LOG_MODULE_DECLARE(vcnl4040, CONFIG_SENSOR_LOG_LEVEL);
|
|||
|
||||
static void vcnl4040_handle_cb(struct vcnl4040_data *data)
|
||||
{
|
||||
gpio_pin_interrupt_configure(data->gpio, data->gpio_pin,
|
||||
GPIO_INT_DISABLE);
|
||||
const struct vcnl4040_config *config = data->dev->config;
|
||||
|
||||
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
|
||||
|
||||
#if defined(CONFIG_VCNL4040_TRIGGER_OWN_THREAD)
|
||||
k_sem_give(&data->trig_sem);
|
||||
|
@ -27,8 +28,9 @@ static void vcnl4040_gpio_callback(const struct device *dev,
|
|||
{
|
||||
struct vcnl4040_data *data =
|
||||
CONTAINER_OF(cb, struct vcnl4040_data, gpio_cb);
|
||||
const struct vcnl4040_config *config = data->dev->config;
|
||||
|
||||
if ((pin_mask & BIT(data->gpio_pin)) == 0U) {
|
||||
if ((pin_mask & BIT(config->int_gpio.pin)) == 0U) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -92,8 +94,7 @@ static void vcnl4040_handle_int(const struct device *dev)
|
|||
LOG_ERR("Unknown interrupt source %d", data->int_type);
|
||||
}
|
||||
|
||||
gpio_pin_interrupt_configure(data->gpio, config->gpio_pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_VCNL4040_TRIGGER_OWN_THREAD
|
||||
|
@ -188,6 +189,10 @@ int vcnl4040_trigger_set(const struct device *dev,
|
|||
uint16_t conf;
|
||||
int ret = 0;
|
||||
|
||||
if (!config->int_gpio.port) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
k_sem_take(&data->sem, K_FOREVER);
|
||||
|
||||
switch (trig->type) {
|
||||
|
@ -250,6 +255,20 @@ int vcnl4040_trigger_init(const struct device *dev)
|
|||
|
||||
data->dev = dev;
|
||||
|
||||
/* dts doesn't have GPIO int pin set, so we dont support
|
||||
* trigger mode for this instance
|
||||
*/
|
||||
if (!config->int_gpio.port) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the GPIO device */
|
||||
if (!device_is_ready(config->int_gpio.port)) {
|
||||
LOG_ERR("%s: device %s is not ready", dev->name,
|
||||
config->int_gpio.port->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_VCNL4040_TRIGGER_OWN_THREAD)
|
||||
k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
|
||||
k_thread_create(&data->thread, data->thread_stack,
|
||||
|
@ -263,30 +282,19 @@ int vcnl4040_trigger_init(const struct device *dev)
|
|||
data->work.handler = vcnl4040_work_handler;
|
||||
#endif
|
||||
|
||||
/* Get the GPIO device */
|
||||
data->gpio = device_get_binding(config->gpio_name);
|
||||
if (data->gpio == NULL) {
|
||||
LOG_ERR("Could not find GPIO device");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->gpio_pin = config->gpio_pin;
|
||||
|
||||
gpio_pin_configure(data->gpio, config->gpio_pin,
|
||||
GPIO_INPUT | config->gpio_flags);
|
||||
gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
|
||||
|
||||
gpio_init_callback(&data->gpio_cb, vcnl4040_gpio_callback,
|
||||
BIT(config->gpio_pin));
|
||||
BIT(config->int_gpio.pin));
|
||||
|
||||
if (gpio_add_callback(data->gpio, &data->gpio_cb) < 0) {
|
||||
if (gpio_add_callback(config->int_gpio.port, &data->gpio_cb) < 0) {
|
||||
LOG_DBG("Failed to set gpio callback!");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
gpio_pin_interrupt_configure(data->gpio, config->gpio_pin,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
|
||||
|
||||
if (gpio_pin_get(data->gpio, config->gpio_pin) > 0) {
|
||||
if (gpio_pin_get_dt(&config->int_gpio) > 0) {
|
||||
vcnl4040_handle_cb(data);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue