Skip to content

drivers: allow to retrieve device structure by the name prefix #21709

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

Closed
Closed
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
3 changes: 0 additions & 3 deletions boards/arm/mimxrt1050_evk/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ endif # NETWORKING

if LVGL

config LVGL_DISPLAY_DEV_NAME
default "ELCDIF_1"

config LVGL_HOR_RES
default 480

Expand Down
3 changes: 0 additions & 3 deletions boards/arm/mimxrt1060_evk/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ endif # NETWORKING

if LVGL

config LVGL_DISPLAY_DEV_NAME
default "ELCDIF_1"

config LVGL_HOR_RES
default 480

Expand Down
3 changes: 0 additions & 3 deletions boards/arm/mimxrt1064_evk/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ endif # NETWORKING

if LVGL

config LVGL_DISPLAY_DEV_NAME
default "ELCDIF_1"

config LVGL_HOR_RES
default 480

Expand Down
2 changes: 1 addition & 1 deletion boards/arm/reel_board/reel_board.dts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

ssd16xxfb@0 {
compatible = "solomon,ssd16xxfb", "gooddisplay,gdeh0213b1";
label = "SSD16XX";
label = "DISPLAY_SSD16XX";
spi-max-frequency = <4000000>;
reg = <0>;
width = <250>;
Expand Down
2 changes: 1 addition & 1 deletion boards/arm/reel_board/reel_board_v2.dts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

ssd16xxfb@0 {
compatible = "solomon,ssd16xxfb", "gooddisplay,gdeh0213b72";
label = "SSD16XX";
label = "DISPLAY_SSD16XX";
spi-max-frequency = <4000000>;
reg = <0>;
width = <250>;
Expand Down
3 changes: 0 additions & 3 deletions boards/shields/adafruit_2_8_tft_touch_v2/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ config ILI9340

if LVGL

config LVGL_DISPLAY_DEV_NAME
default "ILI9340"

config LVGL_HOR_RES
default 320

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

ili9340@0 {
compatible = "ilitek,ili9340";
label = "ILI9340";
label = "DISPLAY_ILI9340";
spi-max-frequency = <15151515>;
reg = <0>;
cmd-data-gpios = <&arduino_header 15 0>; /* D9 */
Expand Down
3 changes: 0 additions & 3 deletions boards/shields/ssd1306/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ config SSD1306

if LVGL

config LVGL_DISPLAY_DEV_NAME
default "SSD1306"

config LVGL_HOR_RES
default 128

Expand Down
2 changes: 1 addition & 1 deletion boards/shields/ssd1306/ssd1306_128x32.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ssd1306@3c {
compatible = "solomon,ssd1306fb";
reg = <0x3c>;
label = "SSD1306";
label = "DISPLAY_SSD1306";
width = <128>;
height = <32>;
segment-offset = <0>;
Expand Down
2 changes: 1 addition & 1 deletion boards/shields/ssd1306/ssd1306_128x64.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ssd1306@3c {
compatible = "solomon,ssd1306fb";
reg = <0x3c>;
label = "SSD1306";
label = "DISPLAY_SSD1306";
width = <128>;
height = <64>;
segment-offset = <0>;
Expand Down
2 changes: 0 additions & 2 deletions boards/shields/st7789v_generic/st7789v_tl019fqv01.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ CONFIG_ST7789V_RGB888=y
CONFIG_LVGL_BITS_PER_PIXEL=24
CONFIG_LVGL_HOR_RES=320
CONFIG_LVGL_VER_RES=170

CONFIG_LVGL_DISPLAY_DEV_NAME="ST7789V"
2 changes: 1 addition & 1 deletion boards/shields/st7789v_generic/st7789v_tl019fqv01.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

st7789v@0 {
compatible = "sitronix,st7789v";
label = "ST7789V";
label = "DISPLAY_ST7789V";
spi-max-frequency = <20000000>;
reg = <0>;
cmd-data-gpios = <&arduino_header 15 0>; /* D9 */
Expand Down
2 changes: 0 additions & 2 deletions boards/shields/st7789v_generic/st7789v_waveshare_240x240.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ CONFIG_ST7789V_RGB565=y
CONFIG_LVGL_BITS_PER_PIXEL=16
CONFIG_LVGL_HOR_RES=240
CONFIG_LVGL_VER_RES=240

CONFIG_LVGL_DISPLAY_DEV_NAME="ST7789V"
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

st7789v@0 {
compatible = "sitronix,st7789v";
label = "ST7789V";
label = "DISPLAY_ST7789V";
spi-max-frequency = <20000000>;
reg = <0>;
cmd-data-gpios = <&arduino_header 15 0>; /* D9 */
Expand Down
3 changes: 0 additions & 3 deletions boards/shields/waveshare_epaper/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ config SSD16XX

if LVGL

config LVGL_DISPLAY_DEV_NAME
default "SSD16XX"

config LVGL_HOR_RES
default 296 if SHIELD_WAVESHARE_EPAPER_GDEH029A1
default 250 if SHIELD_WAVESHARE_EPAPER_GDEH0213B1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

ssd16xxfb@0 {
compatible = "solomon,ssd16xxfb", "gooddisplay,gdeh0213b1";
label = "SSD16XX";
label = "DISPLAY_SSD16XX";
spi-max-frequency = <4000000>;
reg = <0>;
width = <250>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

ssd16xxfb@0 {
compatible = "solomon,ssd16xxfb", "gooddisplay,gdeh0213b72";
label = "SSD16XX";
label = "DISPLAY_SSD16XX";
spi-max-frequency = <4000000>;
reg = <0>;
width = <250>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

ssd16xxfb@0 {
compatible = "solomon,ssd16xxfb", "gooddisplay,gdeh029a1";
label = "SSD16XX";
label = "DISPLAY_SSD16XX";
spi-max-frequency = <4000000>;
reg = <0>;
width = <296>;
Expand Down
2 changes: 1 addition & 1 deletion drivers/display/Kconfig.dummy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if DUMMY_DISPLAY

config DUMMY_DISPLAY_DEV_NAME
string "Dummy display device name"
default "DUMMY_DISPLAY"
default "DISPLAY_DUMMY"

config DUMMY_DISPLAY_X_RES
int "X resolution for dummy display"
Expand Down
2 changes: 1 addition & 1 deletion drivers/display/Kconfig.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if SDL_DISPLAY

config SDL_DISPLAY_DEV_NAME
string "SDL display device name"
default "SDL_DISPLAY"
default "DISPLAY_SDL"

config SDL_DISPLAY_X_RES
int "X resolution for SDL display"
Expand Down
2 changes: 1 addition & 1 deletion dts/arm/nxp/nxp_rt.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
compatible = "fsl,imx6sx-lcdif";
reg = <0x402b8000 0x4000>;
interrupts = <42 0>;
label = "ELCDIF_1";
label = "DISPLAY_ELCDIF_1";
status = "disabled";
};

Expand Down
15 changes: 15 additions & 0 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ void z_sys_device_do_config_level(s32_t level);
*/
__syscall struct device *device_get_binding(const char *name);

/**
* @brief Retrieve the device structure for a driver by prefix
*
* @details Device objects are created via the DEVICE_INIT() macro and
* placed in memory by the linker. If a driver needs to bind to another driver
* it can use this function to retrieve the device structure of the lower level
* driver by the prefix from the name the driver exposes to the system.
*
* @param prefix device name prefix to search for.
* @param idx device index to search for.
*
* @return pointer to device structure; NULL if not found or cannot be used.
*/
__syscall struct device *device_get_by_prefix(const char *prefix, int idx);

/**
* @}
*/
Expand Down
38 changes: 38 additions & 0 deletions kernel/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ struct device *z_impl_device_get_binding(const char *name)
return NULL;
}

struct device *z_impl_device_get_by_prefix(const char *prefix, int idx)
{
struct device *info;
int n = 0;
int len;

for (info = __device_init_start; info != __device_init_end; info++) {
if (info->driver_api == NULL || info->config->name == NULL) {
continue;
}

len = strnlen(prefix, Z_DEVICE_MAX_NAME_LEN);
if (strncmp(info->config->name, prefix, len) == 0) {
if (idx == n) {
return info;
}

n++;
}
}

return NULL;
}

#ifdef CONFIG_USERSPACE
static inline struct device *z_vrfy_device_get_binding(const char *name)
{
Expand All @@ -108,6 +132,20 @@ static inline struct device *z_vrfy_device_get_binding(const char *name)
return z_impl_device_get_binding(name_copy);
}
#include <syscalls/device_get_binding_mrsh.c>

static inline struct device *z_vrfy_device_get_by_prefix(const char *prefix,
int idx)
{
char prefix_copy[Z_DEVICE_MAX_NAME_LEN];

if (z_user_string_copy(prefix_copy, (char *)prefix, sizeof(prefix_copy))
!= 0) {
return 0;
}

return z_impl_device_get_by_prefix(prefix_copy, idx);
}
#include <syscalls/device_get_by_prefix_mrsh.c>
#endif /* CONFIG_USERSPACE */

#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
Expand Down
6 changes: 0 additions & 6 deletions lib/gui/lvgl/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ module = LVGL
module-str = lvgl
source "subsys/logging/Kconfig.template.log_config"

config LVGL_DISPLAY_DEV_NAME
string "Display device name"
default "DISPLAY"
help
Name of the display device to use for rendering.

config LVGL_HOR_RES
int "Horizontal Screen Resolution"
default 320
Expand Down
3 changes: 1 addition & 2 deletions lib/gui/lvgl/lvgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ static int lvgl_init(struct device *dev)
{
ARG_UNUSED(dev);

struct device *display_dev =
device_get_binding(CONFIG_LVGL_DISPLAY_DEV_NAME);
struct device *display_dev = device_get_by_prefix("DISPLAY", 0);
lv_disp_drv_t disp_drv;

if (display_dev == NULL) {
Expand Down
16 changes: 2 additions & 14 deletions samples/display/cfb/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
#include <display/cfb.h>
#include <stdio.h>

#if defined(CONFIG_SSD16XX)
#define DISPLAY_DRIVER "SSD16XX"
#endif

#if defined(CONFIG_SSD1306)
#define DISPLAY_DRIVER "SSD1306"
#endif

#ifndef DISPLAY_DRIVER
#define DISPLAY_DRIVER "DISPLAY"
#endif

void main(void)
{
struct device *dev;
Expand All @@ -29,7 +17,7 @@ void main(void)
u8_t font_width;
u8_t font_height;

dev = device_get_binding(DISPLAY_DRIVER);
dev = device_get_by_prefix("DISPLAY", 0);

if (dev == NULL) {
printf("Device not found\n");
Expand All @@ -41,7 +29,7 @@ void main(void)
return;
}

printf("initialized %s\n", DISPLAY_DRIVER);
printf("Device label %s\n", dev->config->name);

if (cfb_framebuffer_init(dev)) {
printf("Framebuffer initialization failed!\n");
Expand Down
1 change: 0 additions & 1 deletion samples/gui/lvgl/boards/native_posix.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CONFIG_SDL_DISPLAY=y
CONFIG_SDL_DISPLAY_DEV_NAME="DISPLAY"
CONFIG_SDL_DISPLAY_X_RES=320
CONFIG_SDL_DISPLAY_Y_RES=240

1 change: 0 additions & 1 deletion samples/gui/lvgl/boards/native_posix_64.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CONFIG_SDL_DISPLAY=y
CONFIG_SDL_DISPLAY_DEV_NAME="DISPLAY"
CONFIG_SDL_DISPLAY_X_RES=320
CONFIG_SDL_DISPLAY_Y_RES=240

11 changes: 0 additions & 11 deletions samples/gui/lvgl/boards/reel_board.overlay

This file was deleted.

2 changes: 1 addition & 1 deletion samples/gui/lvgl/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main(void)
lv_obj_t *hello_world_label;
lv_obj_t *count_label;

display_dev = device_get_binding(CONFIG_LVGL_DISPLAY_DEV_NAME);
display_dev = device_get_by_prefix("DISPLAY", 0);

if (display_dev == NULL) {
LOG_ERR("device not found. Aborting test.");
Expand Down