Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TOPIC-GPIO]: Convert LMP90xxx ADC and GPIO drivers to new GPIO API #22212

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion boards/shields/lmp90100_evb/lmp90100_evb.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
/* drdyb-gpios = <&arduino_header 15 GPIO_ACTIVE_LOW>; */
#io-channel-cells = <2>;

gpio {
lmp90100_gpio: gpio {
compatible = "ti,lmp90xxx-gpio";
gpio-controller;
label = "LMP90100_GPIO";
/* Reduce to 6 if drdyb is used */
ngpios = <7>;
#gpio-cells = <2>;
};
};
Expand Down
116 changes: 109 additions & 7 deletions drivers/adc/adc_lmp90xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ LOG_MODULE_REGISTER(adc_lmp90xxx);
#define LMP90XXX_ODR_SEL(x) ((x & BIT_MASK(3)) << 4)
#define LMP90XXX_GAIN_SEL(x) ((x & BIT_MASK(3)) << 1)
#define LMP90XXX_BUF_EN(x) (x & BIT(0))
#define LMP90XXX_GPIO_DAT_MASK BIT_MASK(LMP90XXX_GPIO_MAX)

/* Invalid (never used) Upper Register Address */
#define LMP90XXX_INVALID_URA UINT8_MAX
Expand All @@ -110,8 +111,8 @@ struct lmp90xxx_config {
u8_t spi_cs_pin;
struct spi_config spi_cfg;
const char *drdyb_dev_name;
u32_t drdyb_pin;
int drdyb_flags;
gpio_pin_t drdyb_pin;
gpio_devicetree_flags_t drdyb_flags;
u8_t rtd_current;
u8_t resolution;
u8_t channels;
Expand Down Expand Up @@ -807,6 +808,108 @@ int lmp90xxx_gpio_get_pin_value(struct device *dev, u8_t pin, bool *value)

return err;
}

int lmp90xxx_gpio_port_get_raw(struct device *dev, gpio_port_value_t *value)
{
struct lmp90xxx_data *data = dev->driver_data;
u8_t tmp;
int err;

mnkp marked this conversation as resolved.
Show resolved Hide resolved
k_mutex_lock(&data->gpio_lock, K_FOREVER);
err = lmp90xxx_read_reg8(dev, LMP90XXX_REG_GPIO_DAT, &tmp);
tmp &= ~(data->gpio_dircn);
k_mutex_unlock(&data->gpio_lock);

*value = tmp;

return err;
}

int lmp90xxx_gpio_port_set_masked_raw(struct device *dev,
gpio_port_pins_t mask,
gpio_port_value_t value)
{
struct lmp90xxx_data *data = dev->driver_data;
int err = 0;
u8_t tmp;

mask &= LMP90XXX_GPIO_DAT_MASK;

k_mutex_lock(&data->gpio_lock, K_FOREVER);
tmp = (data->gpio_dat & ~mask) | (value & mask);
if (tmp != data->gpio_dat) {
err = lmp90xxx_write_reg8(dev, LMP90XXX_REG_GPIO_DAT, tmp);
if (!err) {
data->gpio_dat = tmp;
}
}
k_mutex_unlock(&data->gpio_lock);

return err;
}

int lmp90xxx_gpio_port_set_bits_raw(struct device *dev, gpio_port_pins_t pins)
{
struct lmp90xxx_data *data = dev->driver_data;
int err = 0;
u8_t tmp;

tmp = pins & LMP90XXX_GPIO_DAT_MASK;

k_mutex_lock(&data->gpio_lock, K_FOREVER);
if (tmp != data->gpio_dat) {
tmp |= data->gpio_dat;
err = lmp90xxx_write_reg8(dev, LMP90XXX_REG_GPIO_DAT, tmp);
if (!err) {
data->gpio_dat = tmp;
}
}
k_mutex_unlock(&data->gpio_lock);

return err;
}

int lmp90xxx_gpio_port_clear_bits_raw(struct device *dev,
gpio_port_pins_t pins)
{
struct lmp90xxx_data *data = dev->driver_data;
int err = 0;
u8_t tmp;

tmp = pins & LMP90XXX_GPIO_DAT_MASK;

k_mutex_lock(&data->gpio_lock, K_FOREVER);
if ((tmp & data->gpio_dat) != 0) {
tmp = data->gpio_dat & ~tmp;
err = lmp90xxx_write_reg8(dev, LMP90XXX_REG_GPIO_DAT, tmp);
if (!err) {
data->gpio_dat = tmp;
}
}
k_mutex_unlock(&data->gpio_lock);

return err;
}

int lmp90xxx_gpio_port_toggle_bits(struct device *dev, gpio_port_pins_t pins)
{
struct lmp90xxx_data *data = dev->driver_data;
u8_t tmp;
int err;

tmp = pins & LMP90XXX_GPIO_DAT_MASK;

k_mutex_lock(&data->gpio_lock, K_FOREVER);
tmp ^= data->gpio_dat;
err = lmp90xxx_write_reg8(dev, LMP90XXX_REG_GPIO_DAT, tmp);
if (!err) {
data->gpio_dat = tmp;
}
k_mutex_unlock(&data->gpio_lock);

return err;
}

#endif /* CONFIG_ADC_LMP90XXX_GPIO */

static int lmp90xxx_init(struct device *dev)
Expand Down Expand Up @@ -887,8 +990,7 @@ static int lmp90xxx_init(struct device *dev)
}

err = gpio_pin_configure(drdyb_dev, config->drdyb_pin,
(GPIO_DIR_IN | GPIO_INT |
GPIO_INT_EDGE | config->drdyb_flags));
GPIO_INPUT | config->drdyb_flags);
if (err) {
LOG_ERR("failed to configure DRDYB GPIO pin (err %d)",
err);
Expand All @@ -912,10 +1014,10 @@ static int lmp90xxx_init(struct device *dev)
return err;
}

err = gpio_pin_enable_callback(drdyb_dev,
config->drdyb_pin);
err = gpio_pin_interrupt_configure(drdyb_dev, config->drdyb_pin,
GPIO_INT_EDGE_TO_ACTIVE);
if (err) {
LOG_ERR("failed to enable DRDBY callback (err %d)",
LOG_ERR("failed to configure DRDBY interrupt (err %d)",
err);
return -EINVAL;
}
Expand Down
103 changes: 98 additions & 5 deletions drivers/gpio/gpio_lmp90xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ LOG_MODULE_REGISTER(gpio_lmp90xxx);
#include "gpio_utils.h"

struct gpio_lmp90xxx_config {
/* gpio_driver_config needs to be first */
struct gpio_driver_config common;
char *parent_dev_name;
};

struct gpio_lmp90xxx_data {
/* gpio_driver_data needs to be first */
struct gpio_driver_data common;
struct device *parent;
};

static int gpio_lmp90xxx_config(struct device *dev, int access_op,
u32_t pin, int flags)
{
struct gpio_lmp90xxx_data *data = dev->driver_data;
int err;
int err = 0;

if (access_op != GPIO_ACCESS_BY_PIN) {
return -ENOTSUP;
Expand All @@ -42,15 +46,39 @@ static int gpio_lmp90xxx_config(struct device *dev, int access_op,
return -EINVAL;
}

if (flags & GPIO_INT) {
if ((flags & GPIO_SINGLE_ENDED) != 0) {
return -ENOTSUP;
}

if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0) {
return -ENOTSUP;
}

if (flags & GPIO_INT_ENABLE) {
/* LMP90xxx GPIOs do not support interrupts */
return -ENOTSUP;
}

if (flags & GPIO_DIR_OUT) {
err = lmp90xxx_gpio_set_output(data->parent, pin);
} else {
switch (flags & GPIO_DIR_MASK) {
case GPIO_INPUT:
err = lmp90xxx_gpio_set_input(data->parent, pin);
break;
case GPIO_OUTPUT:
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
err = lmp90xxx_gpio_set_pin_value(data->parent, pin,
true);
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
err = lmp90xxx_gpio_set_pin_value(data->parent, pin,
false);
}

if (err) {
return err;
}
err = lmp90xxx_gpio_set_output(data->parent, pin);
break;
default:
return -ENOTSUP;
}

return err;
Expand Down Expand Up @@ -96,6 +124,60 @@ static int gpio_lmp90xxx_read(struct device *dev, int access_op,
return err;
}

static int gpio_lmp90xxx_port_get_raw(struct device *dev,
gpio_port_value_t *value)
{
struct gpio_lmp90xxx_data *data = dev->driver_data;

return lmp90xxx_gpio_port_get_raw(data->parent, value);
}

static int gpio_lmp90xxx_port_set_masked_raw(struct device *dev,
gpio_port_pins_t mask,
gpio_port_value_t value)
{
struct gpio_lmp90xxx_data *data = dev->driver_data;

return lmp90xxx_gpio_port_set_masked_raw(data->parent, mask, value);
}

static int gpio_lmp90xxx_port_set_bits_raw(struct device *dev,
gpio_port_pins_t pins)
{
struct gpio_lmp90xxx_data *data = dev->driver_data;

return lmp90xxx_gpio_port_set_bits_raw(data->parent, pins);
}

static int gpio_lmp90xxx_port_clear_bits_raw(struct device *dev,
gpio_port_pins_t pins)
{
struct gpio_lmp90xxx_data *data = dev->driver_data;

return lmp90xxx_gpio_port_clear_bits_raw(data->parent, pins);
}

static int gpio_lmp90xxx_port_toggle_bits(struct device *dev,
gpio_port_pins_t pins)
{
struct gpio_lmp90xxx_data *data = dev->driver_data;

return lmp90xxx_gpio_port_toggle_bits(data->parent, pins);
}

static int gpio_lmp90xxx_pin_interrupt_configure(struct device *dev,
unsigned int pin,
enum gpio_int_mode mode,
enum gpio_int_trig trig)
{
ARG_UNUSED(dev);
ARG_UNUSED(pin);
ARG_UNUSED(mode);
ARG_UNUSED(trig);

return -ENOTSUP;
}

static int gpio_lmp90xxx_init(struct device *dev)
{
const struct gpio_lmp90xxx_config *config = dev->config->config_info;
Expand All @@ -115,6 +197,12 @@ static const struct gpio_driver_api gpio_lmp90xxx_api = {
.config = gpio_lmp90xxx_config,
.write = gpio_lmp90xxx_write,
.read = gpio_lmp90xxx_read,
.port_set_masked_raw = gpio_lmp90xxx_port_set_masked_raw,
.port_set_bits_raw = gpio_lmp90xxx_port_set_bits_raw,
.port_clear_bits_raw = gpio_lmp90xxx_port_clear_bits_raw,
.port_toggle_bits = gpio_lmp90xxx_port_toggle_bits,
.pin_interrupt_configure = gpio_lmp90xxx_pin_interrupt_configure,
.port_get_raw = gpio_lmp90xxx_port_get_raw,
};

BUILD_ASSERT_MSG(CONFIG_GPIO_LMP90XXX_INIT_PRIORITY >
Expand All @@ -124,6 +212,11 @@ BUILD_ASSERT_MSG(CONFIG_GPIO_LMP90XXX_INIT_PRIORITY >

#define GPIO_LMP90XXX_DEVICE(id) \
static const struct gpio_lmp90xxx_config gpio_lmp90xxx_##id##_cfg = {\
.common = { \
.port_pin_mask = \
GPIO_PORT_PIN_MASK_FROM_NGPIOS( \
DT_INST_##id##_TI_LMP90XXX_GPIO_NGPIOS)\
}, \
.parent_dev_name = \
DT_INST_##id##_TI_LMP90XXX_GPIO_BUS_NAME, \
}; \
Expand Down
15 changes: 14 additions & 1 deletion include/drivers/adc/lmp90xxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define ZEPHYR_INCLUDE_DRIVERS_ADC_LMP90XXX_H_

#include <device.h>
#include <zephyr/types.h>
#include <drivers/gpio.h>

/* LMP90xxx supports GPIO D0..D6 */
#define LMP90XXX_GPIO_MAX 6
Expand All @@ -21,4 +21,17 @@ int lmp90xxx_gpio_set_pin_value(struct device *dev, u8_t pin, bool value);

int lmp90xxx_gpio_get_pin_value(struct device *dev, u8_t pin, bool *value);

int lmp90xxx_gpio_port_get_raw(struct device *dev, gpio_port_value_t *value);

int lmp90xxx_gpio_port_set_masked_raw(struct device *dev,
gpio_port_pins_t mask,
gpio_port_value_t value);

int lmp90xxx_gpio_port_set_bits_raw(struct device *dev, gpio_port_pins_t pins);

int lmp90xxx_gpio_port_clear_bits_raw(struct device *dev,
gpio_port_pins_t pins);

int lmp90xxx_gpio_port_toggle_bits(struct device *dev, gpio_port_pins_t pins);

#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_LMP90XXX_H_ */