2014年9月23日 星期二

GPIO觸發LED

前置條件:
1. LED是用keypad 的backlight LED
2. GPIO 為INPUT (似按button後導通)

 整體行為:
當GPIO為 PULL_HIGH時, LED亮
當GPIO為 PULL_DOWN時, LED滅

相關的driver code:
kernel/drivers/led/led-class.c, led-core.c, leds-qpnp.c, led-triggers.c, ledtrig-gpio.c
看完code可發現, 此功能若依kernel的code 只能用 device node的設定來變更GPIO的pin值,
無法用devicetree 來直接指定 (所以此文是修改啟動script來設定GPIO pin number)



1. 開啟 kernel 功能 (修改 default_config 檔: kernel/arch/arm/configs/msm8974_defconfig )
 加入: 
 CONFIG_LEDS_QPNP=y
 CONFIG_LEDS_TRIGGERS=y
 CONFIG_LEDS_TRIGGER_BACKLIGHT=y
 CONFIG_LEDS_TRIGGER_GPIO=y
 CONFIG_LEDS_TRIGGER_DEFAULT_ON=y

2. 設定GPIO行為 (修改 kernel/arch/arm/mach-msm/board-msm8974-gpiomux.c)
static struct gpiomux_setting button_act_cfg = {
        .func = GPIOMUX_FUNC_GPIO,
        .drv = GPIOMUX_DRV_2MA,
        .pull = GPIOMUX_PULL_NONE,
        .dir = GPIOMUX_IN,
};

static struct gpiomux_setting button_sus_cfg = {
        .func = GPIOMUX_FUNC_GPIO,
        .drv = GPIOMUX_DRV_2MA,
        .pull = GPIOMUX_PULL_DOWN,
};

static struct msm_gpiomux_config msm_BUTTON_config[] __initdata = {
        {
                .gpio = GPIO_BUTTON,  //79
                .settings = {
                        [GPIOMUX_ACTIVE]  = &button_act_cfg,
                        [GPIOMUX_SUSPENDED]  = &button_sus_cfg,
                },
        },
};

void __init msm_msmd8974_init_gpiomux(void)
{
...
msm_gpiomux_install(msm_BUTTON_config, ARRAY_SIZE(msm_BUTTON_config));
}

3. 設定 LED與指定用GPIO觸發 (kernel/arch/arm/boot/dts/msm8974-leds.dtsi)
&pm8941_lsid1 {
...
    qcom,leds@e200 {
    ...
    qcom,kpdbl4 {
                        label = "kpdbl";
                        linux,name = "kpdbl-pwm-4";
                        qcom,mode = "pwm";
                        qcom,pwm-channel = <11>;
                        qcom,pwm-us = <1000>;
                        qcom,id = <7>;
                        qcom,max-current = <20>;
                        qcom,row-id = <3>;
                        qcom,row-src-en;
                        qcom,always-on;
                        qcom,default-state = "off";
                        linux,default-trigger = "gpio";
      };

4. 指定GPIO 值與設定亮度等, 這部份自己找 啟動scripts 放, 我是放在 (device/qcom/common/rootdir/etc/init.qcom.sh)
        echo gpio > /sys/class/leds/kpdbl-pwm-4/trigger
        echo 255 > /sys/class/leds/kpdbl-pwm-4/desired_brightness
        echo 255 > /sys/class/leds/kpdbl-pwm-4/brightness
        echo 79 > /sys/class/leds/kpdbl-pwm-4/gpio

沒有留言:

張貼留言