文章目录
- 1、核心数据结构
- 1.1 gpio_led_platform_data
- 1.2 gpio_leds_priv
- 1.3 gpio_led
- 1.4 gpio_led_data
- 1.5 led_classdev
- 1.6 led_trigger
- 2、数据结构之间联系
上篇文章,我们熟悉了
LED
子系统的框架以及其相关的目录结构,接下来我们进一步分析LED
子系统的核心数据结构
1、核心数据结构
1.1 gpio_led_platform_data
struct gpio_led_platform_data {
int num_leds;
const struct gpio_led *leds;
#define GPIO_LED_NO_BLINK_LOW 0 /* No blink GPIO state low */
#define GPIO_LED_NO_BLINK_HIGH 1 /* No blink GPIO state high */
#define GPIO_LED_BLINK 2 /* Please, blink */
gpio_blink_set_t gpio_blink_set;
};
结构体名称:gpio_led_platform_data
文件位置:include/linux/leds.h
主要作用:LED
的平台数据,用于对LED
硬件设备的统一管理
这个结构体用于父节点向子节点传递的数据时使用
1.2 gpio_leds_priv
struct gpio_leds_priv {
int num_leds;
struct gpio_led_data leds[];
};
结构体名称:gpio_leds_priv
文件位置:drivers/leds/leds-gpio.c
主要作用:LED
驱动的私有数据类型,管理全部的LED
设备。
这里的
num_leds
通过解析设备树的子节点的个数来获取
leds[]
根据获取的num_leds
个数,分配对应的空间,来初始化相关数据
1.3 gpio_led
/* For the leds-gpio driver */
struct gpio_led {
const char *name; // LED名称
const char *default_trigger; // 默认触发类型
unsigned gpio; // GPIO编号
unsigned active_low : 1; // 低电平有效
unsigned retain_state_suspended : 1;
unsigned panic_indicator : 1;
unsigned default_state : 2; // 默认状态
unsigned retain_state_shutdown : 1;
/* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
struct gpio_desc *gpiod; // GPIO Group
};
结构体名称:gpio_led
文件位置:include/linux/leds.h
主要作用:LED
的硬件描述结构,包括名称,GPIO
编号,有效电平等等信息。
该结构体的信息大多由解析设备树获得,将设备树中
label
解析为name
,gpios
解析为gpiod
,linux,default-trigger
解析为default_trigger
等
1.4 gpio_led_data
struct gpio_led_data {
struct led_classdev cdev; // LED Class
struct gpio_desc *gpiod; // GPIO description
u8 can_sleep;
u8 blinking; // 闪烁
gpio_blink_set_t platform_gpio_blink_set; // 闪烁设置
};
结构体名称:gpio_led_data
文件位置:drivers/leds/leds-gpio.c
主要作用:LED
相关数据信息,主要在于led_classdev
,用于注册设备节点信息
由设备树解析出来的
gpio_led
,然后将部分属性赋值到gpio_led_data
中,并且初始化led_classdev
相关属性,并且实现led_classdev
结构体中的部分函数。
1.5 led_classdev
struct led_classdev {
const char *name;
enum led_brightness brightness;
enum led_brightness max_brightness;
int flags;
/* Lower 16 bits reflect status */
#define LED_SUSPENDED BIT(0)
#define LED_UNREGISTERING BIT(1)
/* Upper 16 bits reflect control information */
#define LED_CORE_SUSPENDRESUME BIT(16)
#define LED_SYSFS_DISABLE BIT(17)
#define LED_DEV_CAP_FLASH BIT(18)
#define LED_HW_PLUGGABLE BIT(19)
#define LED_PANIC_INDICATOR BIT(20)
#define LED_BRIGHT_HW_CHANGED BIT(21)
#define LED_RETAIN_AT_SHUTDOWN BIT(22)
/* set_brightness_work / blink_timer flags, atomic, private. */
unsigned long work_flags;
#define LED_BLINK_SW 0
#define LED_BLINK_ONESHOT 1
#define LED_BLINK_ONESHOT_STOP 2
#define LED_BLINK_INVERT 3
#define LED_BLINK_BRIGHTNESS_CHANGE 4
#define LED_BLINK_DISABLE 5
/* Set LED brightness level
* Must not sleep. Use brightness_set_blocking for drivers
* that can sleep while setting brightness.
*/
void (*brightness_set)(struct led_classdev *led_cdev,
enum led_brightness brightness);
/*
* Set LED brightness level immediately - it can block the caller for
* the time required for accessing a LED device register.
*/
int (*brightness_set_blocking)(struct led_classdev *led_cdev,
enum led_brightness brightness);
/* Get LED brightness level */
enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);
/*
* Activate hardware accelerated blink, delays are in milliseconds
* and if both are zero then a sensible default should be chosen.
* The call should adjust the timings in that case and if it can't
* match the values specified exactly.
* Deactivate blinking again when the brightness is set to LED_OFF
* via the brightness_set() callback.
*/
int (*blink_set)(struct led_classdev *led_cdev,
unsigned long *delay_on,
unsigned long *delay_off);
struct device *dev;
const struct attribute_group **groups;
struct list_head node; /* LED Device list */
const char *default_trigger; /* Trigger to use */
unsigned long blink_delay_on, blink_delay_off;
struct timer_list blink_timer;
int blink_brightness;
int new_blink_brightness;
void (*flash_resume)(struct led_classdev *led_cdev);
struct work_struct set_brightness_work;
int delayed_set_value;
#ifdef CONFIG_LEDS_TRIGGERS
/* Protects the trigger data below */
struct rw_semaphore trigger_lock;
struct led_trigger *trigger;
struct list_head trig_list;
void *trigger_data;
/* true if activated - deactivate routine uses it to do cleanup */
bool activated;
#endif
#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
int brightness_hw_changed;
struct kernfs_node *brightness_hw_changed_kn;
#endif
/* Ensures consistent access to the LED Flash Class device */
struct mutex led_access;
};
结构体名称:led_classdev
文件位置:include/linux/leds.h
主要作用:该结构体所包括的内容较多,主要有以下几个功能
LED
亮度控制功能LED
闪烁功能控制- 创建
sysfs
文件节点,向上提供用户访问接口
由上面可知,在创建
gpio_led_data
时,顺便初始化led_classdev
结构体,赋值相关属性以及部分回调函数,最终将led_classdev
注册进入LED
子系统框架中,在sysfs
中创建对应的文件节点。
1.6 led_trigger
struct led_trigger {
/* Trigger Properties */
const char *name;
int (*activate)(struct led_classdev *led_cdev);
void (*deactivate)(struct led_classdev *led_cdev);
/* LEDs under control by this trigger (for simple triggers) */
rwlock_t leddev_list_lock;
struct list_head led_cdevs;
/* Link to next registered trigger */
struct list_head next_trig;
const struct attribute_group **groups;
};
结构体名称:led_trigger
文件位置:include/linux/leds.h
主要作用:提供触发控制策略及功能
该结构体,由打开相应的
trigger
触发状态后创建,并与led_classdev
结构体关联。
2、数据结构之间联系
上文已经大致说明了各个结构体之间的关系,下面再展开说明一下
- 设备树解析:通过
gpio_led_probe
接口,解析设备树信息,分配给结构体gpio_leds_priv
LED
设备的创建:解析完设备树后,要创建LED
设备,通过接口create_gpio_led
,将gpio_leds_priv
的部分信息赋值给gpio_led_data
,并且初始化led_classdev
属性信息和回调函数LED
设备注册:创建完led_classdev
结构体后,调用devm_of_led_classdev_register
将LED
设备注册进入LED
子系统LED
触发:如果设置了某种触发模式,将会分配并赋值led_trigger
结构体,并于led_classdev
关联起来,注册进入LED
子系统中。
最后,绘制一份各个数据结构之间的关系图,如下:
点赞+关注,永远不迷路