OpenWRT 系统的 gpio 可用配置为键盘输入、led输出、控制输出等,以 mt7621 为例,相互关系说明如下:

第一步 查看设备树配置文件

/OpenWrt/mtk7621-19.07$ ls target/linux/ramips/dts/ | grep 7621
AP-MT7621A-V60.dts
MT7621.dts
mt7621.dtsi				         #(1) mt7621 芯片资源的基本配置文件
U7621-06-256M-16M.dts            # (3) U7621-06-256M-16M 用户板级 分区配置文件
U7621-06-256M-64M.dts
U7621-06-512M-64M.dts
U7621-06.dtsi                   # (2) U7621-06 用户板级的配置文件

(1) mt7621 芯片资源的基本配置文件

#include <dt-bindings/interrupt-controller/mips-gic.h>
#include <dt-bindings/clock/mt7621-clk.h>

/ {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "mediatek,mt7621-soc";

        cpus {
                #address-cells = <1>;
                #size-cells = <0>;

                cpu@0 {
                        device_type = "cpu";
                        compatible = "mips,mips1004Kc";
                        reg = <0>;
                };

                cpu@1 {
                        device_type = "cpu";
                        compatible = "mips,mips1004Kc";
                        reg = <1>;
                };
        };

        cpuintc: cpuintc {
                #address-cells = <0>;
                #interrupt-cells = <1>;
                interrupt-controller;
                compatible = "mti,cpu-interrupt-controller";
        };

        aliases {
                serial0 = &uartlite;
        };

        pll: pll {
                compatible = "mediatek,mt7621-pll", "syscon";

                #clock-cells = <1>;
                clock-output-names = "cpu", "bus";
        };

        sysclock: sysclock {
                #clock-cells = <0>;
                compatible = "fixed-clock";

                /* FIXME: there should be way to detect this */
                clock-frequency = <50000000>;
        };
省略部分

(2) U7621-06 用户板级的配置文件

注意配置文件中 leds 、keys 和 pinctrl 中的 state_default 此部分是对,mt7621.dtsi 中的gpio属性的重写,意思是可用作为gpio端口使用的特殊声明。


#include "mt7621.dtsi"

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>

/ {
        compatible = "unielec,u7621-06", "mediatek,mt7621-soc";
       /* compatible = "mediatek,mt7621-soc";*/

        aliases {
                led-boot = &led_status;
                led-failsafe = &led_status;
                led-running = &led_status;
                led-upgrade = &led_status;
        };

        chosen {
                bootargs = "console=ttyS0,57600";
        };

        gpio-export {
                compatible = "gpio-export";
                #size-cells = <0>;

                modem_reset {
                        gpio-export,name = "modem_reset";
                        gpio-export,output = <1>;
                        gpios = <&gpio0 16 GPIO_ACTIVE_LOW>;
                };
        };

        keys {
                compatible = "gpio-keys-polled";
                poll-interval = <20>;

                reset {
                        label = "reset";
                        gpios = <&gpio0 18 GPIO_ACTIVE_LOW>;
                        linux,code = <KEY_RESTART>;
                };
        };

        leds {
                compatible = "gpio-leds";

                led_status: status {
                        label = "u7621-06:green:status";
                        gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
                };

                led4 {
                        label = "u7621-06:green:led4";
                        gpios = <&gpio0 14 GPIO_ACTIVE_HIGH>;
                };

                led5 {
                        label = "u7621-06:green:led5";
                        gpios = <&gpio0 15 GPIO_ACTIVE_HIGH>;
                };

                lan1 {
                        label = "u7621-06:green:lan1";
                        gpios = <&gpio0 17 GPIO_ACTIVE_HIGH>;
                };

        };
};

&gpio0 {
        status = "okay";
};

&sdhci {
        status = "okay";
};

&pcie {
        status = "okay";
};

&ethernet {
        mtd-mac-address = <&factory 0xe000>;
        mediatek,portmap = "llllw";
};

//Date: 2021-5-28 

&uartlite2 {
        status = "okay";
};

&uartlite3 {
        status = "okay";
};

&gdma {
        status = "okay";
};

&hsdma {
        status = "okay";
};


&pinctrl {
        state_default: pinctrl0 {
                gpio {
                //      ralink,group = "jtag", "uart2", "wdt";
                        ralink,group = "jtag","wdt";
                        ralink,function = "gpio";
                };
        };
};

如 jtag 口做为普通 io 使用;如果笔者封闭那行中,包含 uart2 口;如果不取出的话,uart2 就无法打开成功。

第二步 对应的驱动文件

compatible = “gpio-keys-polled”;驱动文件位置:

build_dir/target-mipsel_24kc_musl/linux-ramips_mt7621/linux-4.14.200$ ls drivers/input/keyboard/gpio_keys.c 
drivers/input/keyboard/gpio_keys.c

compatible = “gpio-leds”; 驱动文件位置:

build_dir/target-mipsel_24kc_musl/linux-ramips_mt7621/linux-4.14.200$ ls drivers/leds/leds-gpio.c
drivers/leds/leds-gpio.c

第三步 配置 led 的对应关系

配置 led 的对应关系,请参考此路径文件:

 ls target/linux/ramips/base-files/etc/board.d/01_leds 
target/linux/ramips/base-files/etc/board.d/01_leds

详细内容如下:

#!/bin/sh

. /lib/functions/uci-defaults.sh

set_wifi_led() {
        ucidef_set_led_netdev "wifi_led" "wifi" "${1}" "${2:-wlan0}"
}


board=$(board_name)
boardname="${board##*,}"

board_config_update

case $board in
3g-6200n|\
br-6475nd|\
mzk-w300nh2)
        set_wifi_led "$boardname:amber:wlan"
        ;;
3g-6200nl|\
air3gii|\
hilink,hlk-7628n|\
mr-102n|\
skylab,skw92a|\
wnce2001|\
zbt-we2026)
        set_wifi_led "$boardname:green:wlan"
        ;;
ai-br100)
        ucidef_set_led_netdev "wan" "wan" "$boardname:blue:wan" "eth0.2"
        set_wifi_led "$boardname:blue:wlan"
        ;;
alfa-network,ac1200rm)
        set_wifi_led "$boardname:green:wlan2g" "wlan1"
        ;;
alfa-network,awusfree1|\
edimax,br-6478ac-v2)
        set_wifi_led "$boardname:blue:wlan"
        ;;
alfa-network,r36m-e4g)
        ucidef_set_led_netdev "4g" "4g" "$boardname:orange:4g" "wwan0"
        ucidef_set_led_switch "lan" "lan" "$boardname:green:lan" "switch0" "0x8"
        ucidef_set_led_switch "wan" "wan" "$boardname:green:wan" "switch0" "0x10"
        ;;
alfa-network,tube-e4g)
        ucidef_set_led_netdev "4g" "4g" "$boardname:green:4g" "wwan0"
        ucidef_set_led_netdev "lan" "lan" "$boardname:blue:lan" "eth0"
        ;;
all0256n-4M|\
all0256n-8M)
        ucidef_set_rssimon "wlan0" "200000" "1"
        ucidef_set_led_rssi "rssilow" "RSSILOW" "all0256n:green:rssilow" "wlan0" "1" "40" "0" "6"
        ucidef_set_led_rssi "rssimedium" "RSSIMEDIUM" "all0256n:green:rssimed" "wlan0" "30" "80" "-29" "5"
        ucidef_set_led_rssi "rssihigh" "RSSIHIGH" "all0256n:green:rssihigh" "wlan0" "70" "100" "-69" "8"
        set_wifi_led "rt2800pci-phy0::radio"
        ;;
zbt-we826-16M|\
zbt-we826-32M)
        set_wifi_led "zbt-we826:green:wifi"
        ;;
zbtlink,zbt-we1226)
        set_wifi_led "$boardname:green:wlan"
        ucidef_set_led_switch "lan1" "LAN1" "$boardname:green:lan1" "switch0" "0x01"
        ucidef_set_led_switch "lan2" "LAN2" "$boardname:green:lan2" "switch0" "0x02"
        ucidef_set_led_switch "wan" "WAN" "$boardname:green:wan" "switch0" "0x10"
        ;;
zorlik,zl5900v2)
        ucidef_set_led_netdev "lan" "lan" "$boardname:green:lan" eth0
        ;;
zyxel,keenetic-extra-ii)
        set_wifi_led "$boardname:green:wifi"
        ucidef_set_led_switch "internet" "internet" "$boardname:green:internet" "switch0" "0x01"
        ;;
youku-yk1)
        set_wifi_led "$boardname:blue:air"
        ucidef_set_led_switch "wan" "wan" "$boardname:blue:wan" "switch0" "0x10"
        ;;
esac

board_config_flush

exit 0

此文件目的是备忘使用。

Logo

更多推荐