linux power_supply

内核提供给电池驱动的接口就是结构体power_supply。

Battery驱动程序需要通过sys文件系统向用户空间提供接口,sys文件系统的路径是由上层的程序指定的。

Linux标准的Power Supply驱动程序所使用的文件系统路径问/sys/class/power_supply,其中的每个子目录表示一种能源供应设备的名称。

Power Supply驱动程序的头文件在include/linux/power_supply.h中定义,注册和注销驱动程序的函数如下所示:

struct power_supply *power_supply_register(struct device *parent,
    const struct power_supply_desc *desc,
    const struct power_supply_config *cfg)
void power_supply_unregister(struct power_supply *psy)

其中power_supply结构体为驱动程序需要实现的部分:

struct power_supply {
    const struct power_supply_desc *desc;

    char **supplied_to;
    size_t num_supplicants;

    char **supplied_from;
    size_t num_supplies;
    struct device_node *of_node;

    /* Driver private data */
    void *drv_data;

    /* private */
    struct device dev;
    struct work_struct changed_work;
    struct delayed_work deferred_register_work;
    spinlock_t changed_lock;
    bool changed;
    atomic_t use_cnt;
#ifdef CONFIG_THERMAL
    struct thermal_zone_device *tzd;
    struct thermal_cooling_device *tcd;
#endif

#ifdef CONFIG_LEDS_TRIGGERS
    struct led_trigger *charging_full_trig;
    char *charging_full_trig_name;
    struct led_trigger *charging_trig;
    char *charging_trig_name;
    struct led_trigger *full_trig;
    char *full_trig_name;
    struct led_trigger *online_trig;
    char *online_trig_name;
    struct led_trigger *charging_blink_full_solid_trig;
    char *charging_blink_full_solid_trig_name;
#endif
};

其中power_supply_desc 为设备供电的描述结构体:


/* Description of power supply */
struct power_supply_desc {
    const char *name;   /*供电设备名*/
    enum power_supply_type type;/*设备类型*/
    enum power_supply_property *properties;/*设备属性*/
    size_t num_properties;

    /*
     * Functions for drivers implementing power supply class.
     * These shouldn't be called directly by other drivers for accessing
     * this power supply. Instead use power_supply_*() functions (for
     * example power_supply_get_property()).
     */
    int (*get_property)(struct power_supply *psy,
                enum power_supply_property psp,
                union power_supply_propval *val);
    int (*set_property)(struct power_supply *psy,
                enum power_supply_property psp,
                const union power_supply_propval *val);
    /*
     * property_is_writeable() will be called during registration
     * of power supply. If this happens during device probe then it must
     * not access internal data of device (because probe did not end).
     */
    int (*property_is_writeable)(struct power_supply *psy,
                     enum power_supply_property psp);
    void (*external_power_changed)(struct power_supply *psy);
    void (*set_charged)(struct power_supply *psy);

    /*
     * Set if thermal zone should not be created for this power supply.
     * For example for virtual supplies forwarding calls to actual
     * sensors or other supplies.
     */
    bool no_thermal;
    /* For APM emulation, think legacy userspace. */
    int use_for_apm;
};

其中内核主要通过get_property这个函数指针来获得驱动中的有关电池的信息。

内核中提供给用户接口,通过power_supply_register 函数在/sys/class/power_supply下分别注册ac、usb、 battery,注册完成可发现在设备目录/sys/class/power_supply下分别出现ac、 usb、 battery三个文件夹,通过读取这些文件的内容,就可以得到电源的信息。
内核主要通过两个文件power_supply_class.cpower_supply_core.c,调用其中的函数就可以把电源(电池,USB或AC)的信息展现给用户!

power supply例子:
http://blog.csdn.net/sgmenghuo/article/details/44061715

你可能感兴趣的:(linux设备驱动,kernel)