drivers: led_strip: ws2812_gpio: use gpio_dt_spec
Simplify device driver implementation by using gpio_dt_spec. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
parent
9395ee4a04
commit
e2431fa25b
|
@ -24,21 +24,12 @@ LOG_MODULE_REGISTER(ws2812_gpio);
|
|||
#include <drivers/clock_control/nrf_clock_control.h>
|
||||
#include <dt-bindings/led/led.h>
|
||||
|
||||
struct ws2812_gpio_data {
|
||||
const struct device *gpio;
|
||||
};
|
||||
|
||||
struct ws2812_gpio_cfg {
|
||||
uint8_t pin;
|
||||
struct gpio_dt_spec in_gpio;
|
||||
uint8_t num_colors;
|
||||
const uint8_t *color_mapping;
|
||||
};
|
||||
|
||||
static struct ws2812_gpio_data *dev_data(const struct device *dev)
|
||||
{
|
||||
return dev->data;
|
||||
}
|
||||
|
||||
static const struct ws2812_gpio_cfg *dev_cfg(const struct device *dev)
|
||||
{
|
||||
return dev->config;
|
||||
|
@ -72,7 +63,7 @@ static const struct ws2812_gpio_cfg *dev_cfg(const struct device *dev)
|
|||
* We should be able to make this portable using the results of
|
||||
* https://github.com/zephyrproject-rtos/zephyr/issues/11917.
|
||||
*
|
||||
* We already have the GPIO device stashed in ws2812_gpio_data, so
|
||||
* We already have the GPIO device stashed in ws2812_gpio_config, so
|
||||
* this driver can be used as a test case for the optimized API.
|
||||
*
|
||||
* Per Arm docs, both Rd and Rn must be r0-r7, so we use the "l"
|
||||
|
@ -104,7 +95,7 @@ static const struct ws2812_gpio_cfg *dev_cfg(const struct device *dev)
|
|||
static int send_buf(const struct device *dev, uint8_t *buf, size_t len)
|
||||
{
|
||||
volatile uint32_t *base = (uint32_t *)&NRF_GPIO->OUTSET;
|
||||
const uint32_t val = BIT(dev_cfg(dev)->pin);
|
||||
const uint32_t val = BIT(dev_cfg(dev)->in_gpio.pin);
|
||||
struct onoff_manager *mgr =
|
||||
z_nrf_clock_control_get_onoff(CLOCK_CONTROL_NRF_SUBSYS_HF);
|
||||
struct onoff_client cli;
|
||||
|
@ -205,13 +196,6 @@ static const struct led_strip_driver_api ws2812_gpio_api = {
|
|||
.update_channels = ws2812_gpio_update_channels,
|
||||
};
|
||||
|
||||
#define WS2812_GPIO_DEV(idx) \
|
||||
(DT_INST_GPIO_LABEL(idx, in_gpios))
|
||||
#define WS2812_GPIO_PIN(idx) \
|
||||
(DT_INST_GPIO_PIN(idx, in_gpios))
|
||||
#define WS2812_GPIO_FLAGS(idx) \
|
||||
(DT_INST_GPIO_FLAGS(idx, in_gpios))
|
||||
|
||||
/*
|
||||
* Retrieve the channel to color mapping (e.g. RGB, BGR, GRB, ...) from the
|
||||
* "color-mapping" DT property.
|
||||
|
@ -234,15 +218,12 @@ static const uint8_t ws2812_gpio_##idx##_color_mapping[] = \
|
|||
\
|
||||
static int ws2812_gpio_##idx##_init(const struct device *dev) \
|
||||
{ \
|
||||
struct ws2812_gpio_data *data = dev_data(dev); \
|
||||
const struct ws2812_gpio_cfg *cfg = dev_cfg(dev); \
|
||||
uint8_t i; \
|
||||
\
|
||||
data->gpio = device_get_binding(WS2812_GPIO_DEV(idx)); \
|
||||
if (!data->gpio) { \
|
||||
LOG_ERR("Unable to find GPIO controller %s", \
|
||||
WS2812_GPIO_DEV(idx)); \
|
||||
return -ENODEV; \
|
||||
if (!device_is_ready(cfg->in_gpio.port)) { \
|
||||
LOG_ERR("GPIO device not ready"); \
|
||||
return -ENODEV; \
|
||||
} \
|
||||
\
|
||||
for (i = 0; i < cfg->num_colors; i++) { \
|
||||
|
@ -260,18 +241,13 @@ static const uint8_t ws2812_gpio_##idx##_color_mapping[] = \
|
|||
} \
|
||||
} \
|
||||
\
|
||||
return gpio_pin_configure(data->gpio, \
|
||||
WS2812_GPIO_PIN(idx), \
|
||||
WS2812_GPIO_FLAGS(idx) | \
|
||||
GPIO_OUTPUT); \
|
||||
return gpio_pin_configure_dt(&cfg->in_gpio, GPIO_OUTPUT); \
|
||||
} \
|
||||
\
|
||||
static struct ws2812_gpio_data ws2812_gpio_##idx##_data; \
|
||||
\
|
||||
WS2812_COLOR_MAPPING(idx); \
|
||||
\
|
||||
static const struct ws2812_gpio_cfg ws2812_gpio_##idx##_cfg = { \
|
||||
.pin = WS2812_GPIO_PIN(idx), \
|
||||
.in_gpio = GPIO_DT_SPEC_INST_GET(idx, in_gpios), \
|
||||
.num_colors = WS2812_NUM_COLORS(idx), \
|
||||
.color_mapping = ws2812_gpio_##idx##_color_mapping, \
|
||||
}; \
|
||||
|
@ -279,7 +255,7 @@ static const uint8_t ws2812_gpio_##idx##_color_mapping[] = \
|
|||
DEVICE_DT_INST_DEFINE(idx, \
|
||||
ws2812_gpio_##idx##_init, \
|
||||
NULL, \
|
||||
&ws2812_gpio_##idx##_data, \
|
||||
NULL, \
|
||||
&ws2812_gpio_##idx##_cfg, POST_KERNEL, \
|
||||
CONFIG_LED_STRIP_INIT_PRIORITY, \
|
||||
&ws2812_gpio_api);
|
||||
|
|
Loading…
Reference in a new issue