linuxGPIO的设置-使用LED的驱动

因为我的引脚控制比较简单,只需要固定的输出高低电平就可以了,也没有其他需求。所以就搭上leds-gpio的驱动吧,自己就不用写了。

首先,内核中增加leds-gpio的驱动。

根据Kconfig和Make file 和帮助信息,可以定位到图中的选项。

第一步先定位到led的驱动是哪一个

第二步,查看该目录中的Makefile

vim drivers/leds/Makefile

找到这个相关的配置项

第三步,make menuconfig的时候找到对应的CONFIG就行,右斜杆,弹出搜索界面

 在帮助信息中可以看到我需要勾选的选项了。

第四步,保存退出,重新编译内核

第五步,修改dts,可以参考其他dts文件修改。

 

这里我修改PMON的dts,因为我的linux加载时使用的是pmon的dtb

	gpio-conf422 {
		compatible = "gpio-leds";

		re0 {
			label = "re0";
			gpios = <&pioA 50 0>;
		};
		de0 {
			label = "de0";
			gpios = <&pioA 51 0>;
			default-state = "on";
		};
		re1 {
			label = "re1";
			gpios = <&pioA 48 0>;
		};
		de1 {
			label = "de1";
			gpios = <&pioA 49 0>;
			default-state = "on";			
		};
	};

全部dts如下:

/*
 * 2K1000 Device Tree Source for LS2K1000 boards
 */

/dts-v1/;
/ {
    model = "loongson,LS2k1000-EVP";
    compatible = "loongson,ls2k";
    #address-cells = <2>;
    #size-cells = <2>;

    // Since 2K1000 support > 4GiB DIMM, use 2 cell for and address size
    memory {
        name = "memory";
        device_type = "memory";
//#ifdef NOGPU
        reg =  <0 0x00200000 0 0x0ce00000    // 200MiB at 2MiB
//#else
//        reg =  <0 0x00200000 0 0x0ee00000    // 200MiB at 2MiB
//#endif
//            1 0x10000000 1 0xb0000000>;    // 7424 - 512 MiB at 4352MiB
            1 0x10000000 0 0xd0000000>;    // 4G 7424 - 512 MiB at 4352MiB
    };

    aliases {
        ethernet0 = &gmac0;
        ethernet1 = &gmac1;
    };

    // all soc device use 32bit address

    soc {
        compatible = "ls,nbus", "simple-bus";
        #address-cells = <1>;
        #size-cells = <1>;
        ranges = <0x10000000 0 0x10000000 0x10000000
              0x40000000 0 0x40000000 0x40000000
              0x20000000 0 0x20000000 0x20000000
                        0x0d000000 0 0x0d000000 0x02000000>;

        dma-coherent;
        nr_cpus_loongson = <2>;
        cores_per_package = <2>;

        icu: interrupt-controller@1fe11400 {
            compatible = "loongson,2k-icu";
            interrupt-controller;
            #interrupt-cells = <1>;
            reg = <0x1fe11400 0x40>;
        };

        msi: pci-msi-controller@1fe114a0 {
            compatible = "loongson,2k-pci-msi";
            msi-controller;
            interrupt-parent = <&icu>;
            msi-mask = <0x000000c0 0x40000c00>;
            reg = <0x1fe114a0 0x60>;
        };

        /* HDA controller nodes:
         * If you want to use the "hda" function,enable the "hda"
         * controller by configure general configuration register 0.
         */
        /*hda@0x400d0000 {
            compatible = "loongson,ls-audio";
            reg = <0x400d0000 0xffff>;
            interrupt-parent = <&icu>;
            interrupts = <12>;
        };*/

        dc@0x400c0000 {
            compatible = "loongson,ls-fb";
            reg = <0x400c0000 0x10000
//#ifdef NOGPU
                   0x0d000000 0x2000000>;
//#else
//                   0x20000000 0x8000000>;
//#endif
            interrupt-parent = <&icu>;
            interrupts = <36>;
        };

#ifndef NOGPU
//        gpu@0x40080000 {
//            compatible = "loongson,galcore";
//            reg = <0x40080000 0x40000
//                0x2a000000 0x15000000>;
//            interrupt-parent = <&icu>;
//            interrupts = <37>;
//        };
#endif

        ohci@0x40070000 {
            compatible = "loongson,ls2k-ohci", "usb-ohci", "generic-ohci";
            reg = <0x40070000 0x10000>;
            interrupt-parent = <&icu>;
            interrupts = <59>;
            dma-mask = <0x0 0xffffffff>;
        };

        ehci@0x40060000 {
            compatible = "loongson,ls2k-ehci", "usb-ehci";
            reg = <0x40060000 0x10000>;
            interrupt-parent = <&icu>;
            interrupts = <58>;
            dma-mask = <0xffffffff 0xffffffff>;
        };

        otg@0x40000000 {
            compatible = "loongson,dwc-otg", "dwc-otg";
            reg = <0x40000000 0x40000>;
            interrupt-parent = <&icu>;
            interrupts = <57>;
            dma-mask = <0x0 0xffffffff>;
        };

        ahci@0x400e0000{
            compatible = "snps,spear-ahci";
            reg = <0x400e0000 0x10000>;
            interrupt-parent = <&icu>;
            interrupts = <27>;
            dma-mask = <0x0 0xffffffff>;
        };

        gmac0: ethernet@0x40040000 {
            compatible = "snps,dwmac-3.70a", "ls,ls-gmac";
            reg = <0x40040000 0x10000>;
            interrupt-parent = <&icu>;
            interrupts = <20 21>;
            interrupt-names = "macirq", "eth_wake_irq";
            mac-address = [ 64 48 48 48 48 60 ];/* [>mac 64:48:48:48:48:60 <]*/
            phy-mode = "rgmii";
            bus_id = <0x0>;
            phy_addr = <0xffffffff>;
            dma-mask = <0xffffffff 0xffffffff>;
        };
#if 1
        gmac1: ethernet@0x40050000 {
            compatible = "snps,dwmac-3.70a", "ls,ls-gmac";
            reg = <0x40050000 0x10000>;
            interrupt-parent = <&icu>;
            interrupts = <22 23>;
            interrupt-names = "macirq", "eth_wake_irq";
            mac-address = [ 64 48 48 48 48 61 ];/* [>mac 64:48:48:48:48:61 <]*/
            phy-mode = "rgmii";
            bus_id = <0x1>;
            phy_addr = <0xffffffff>;
            dma-mask = <0xffffffff 0xffffffff>;
        };
#endif


        pcie0_port0@40100000 {
            compatible = "loongson,ls-pcie";
            interrupt-parent = <&icu>;
            interrupts = <40>;
            #address-cells = <3>;
            #size-cells = <2>;
            bus-range = <0x1 0x3>;
#if 1
            ranges = <0x02000000 0x0 0x40100000 0x40100000 0x0 0x8000000  //mem
                        0x01000000 0x0 0x18001000 0x18001000 0x0 0x0ff000>;  //io
#else
              ranges = <0x02000000 0x0 0x40260000 0x40260000 0x0 0x8000000  //mem
                        0x01000000 0x0 0x4028c000 0x4028c000 0x0 0x0ff000>;  //io
#endif
        };
#if 0

        pcie0_port1@50000000 {
            compatible = "loongson,ls-pcie";
            interrupt-parent = <&icu>;
            interrupts = <41>;
            #address-cells = <3>;
            #size-cells = <2>;
            bus-range = <0x4 0x6>;
              ranges = <0x02000000 0x0 0x50000000 0x50000000 0x0 0x4000000  //mem
                        0x01000000 0x0 0x18100000 0x18100000 0x0 0x100000>;  //io
        };

        pcie0_port2@54000000 {
            compatible = "loongson,ls-pcie";
            interrupt-parent = <&icu>;
            interrupts = <42>;
            #address-cells = <3>;
            #size-cells = <2>;
            bus-range = <0x8 0xa>;
              ranges = <0x02000000 0x0 0x54000000 0x54000000 0x0 0x4000000  //mem
                        0x01000000 0x0 0x18200000 0x18200000 0x0 0x100000>;  //io
        };

        pcie0_port3@58000000 {
            compatible = "loongson,ls-pcie";
            interrupt-parent = <&icu>;
            interrupts = <43>;
            #address-cells = <3>;
            #size-cells = <2>;
            bus-range = <0xc 0xe>;
              ranges = <0x02000000 0x0 0x58000000 0x58000000 0x0 0x8000000  //mem
                        0x01000000 0x0 0x18300000 0x18300000 0x0 0x100000>;  //io
        };

        pcie1_port0@60000000 {
            compatible = "loongson,ls-pcie";
            interrupt-parent = <&icu>;
            interrupts = <44>;
            #address-cells = <3>;
            #size-cells = <2>;
            bus-range = <0x10 0x12>;
              ranges = <0x02000000 0x0 0x60000000 0x60000000 0x0 0x18000000  //mem
                        0x01000000 0x0 0x18400000 0x18400000 0x0 0x100000>;  //io
        };

        pcie1_port1@78000000 {
            compatible = "loongson,ls-pcie";
            interrupt-parent = <&icu>;
            interrupts = <45>;
            #address-cells = <3>;
            #size-cells = <2>;
            bus-range = <0x14 0x16>;
              ranges = <0x02000000 0x0 0x78000000 0x78000000 0x0 0x8000000  //mem
                        0x01000000 0x0 0x18500000 0x18500000 0x0 0x100000>;  //io
        };
#endif
        serial0x@0x1fe00000{
            device_type = "serial";
            compatible = "ns16550";
            reg = <0x1fe00000 0x100>;
            clock-frequency = <125000000>;
            interrupt-parent = <&icu>;
            interrupts = <8>;
        };
#if 0
        serial3x@0x1fe00300{
            device_type = "serial";
            compatible = "ns16550";
            reg = <0x1fe00300 0x100>;
            clock-frequency = <125000000>;
            interrupt-parent = <&icu>;
            interrupts = <8>;
        };
        serial4x@0x1fe00400{
            device_type = "serial";
            compatible = "ns16550";
            reg = <0x1fe00400 0x100>;
            clock-frequency = <125000000>;
            interrupt-parent = <&icu>;
            interrupts = <9>;
        };
#endif
        serial5x@0x1fe00500{
            device_type = "serial";
            compatible = "ns16550";
            reg = <0x1fe00500 0x100>;
            clock-frequency = <125000000>;
            interrupt-parent = <&icu>;
            interrupts = <9>;
        };

        pioA:gpio@0x1fe10500{
            compatible = "ls,ls2k-gpio", "ls,ls-gpio";
            reg = <0x1fe10500 0x38>;
            ngpios = <64>;
            gpio-controller;
            #gpio-cells = <2>;
            
            
        };
        

        i2c0: i2c@1fe01000{
            compatible = "loongson,ls-i2c";
            reg = <0x1fe01000 0x8>;
            interrupt-parent = <&icu>;
            interrupts = <30>;
            #address-cells = <1>;
            #size-cells = <0>;
            
            tvp5150@5d{
                compatible = "tvp5150";
                reg = <0x5d>;
            };
/*            rtc@51{
                compatible = "nxp,pcf8563";
                reg = <0x51>;
            };*/
#if 0
            rtc@68{
                compatible = "dallas,ds1338";
                reg = <0x68>;
            };

            tmp421@4d{
                compatible = "tmp422";
                reg = <0x4d>;
            };

            eeprom@57{
                compatible = "atmel,24c16";
                reg = <0x57>;
                pagesize = <16>;
            };
#endif
        };
#if 0
        i2c1: i2c@1fe01800{
            #address-cells = <1>;
            #size-cells = <0>;

            compatible = "loongson,ls-i2c";
            reg = <0x1fe01800 0x8>;
            interrupt-parent = <&icu>;
            interrupts = <31>;
            codec@1a{
                compatible = "codec_uda1342";
                reg = <0x1a>;
            };
        };
#endif
#if 0
        i2c_dvo0:i2c@2{
            compatible = "i2c-gpio";
            gpios = <&pioA 1 0 /* sda */
                 &pioA 0 0 /* scl */
                >;
            /*i2c-gpio,sda-open-drain;*/
            /*i2c-gpio,scl-open-drain;*/
            i2c-gpio,delay-us = <5>;    /* ~100 kHz */
            #address-cells = <1>;
            #size-cells = <0>;
            eeprom@50{
                compatible = "dvi-eeprom-edid";
                reg = <0x50>;
            };
        };

        i2c_dvo1:i2c@3{
            compatible = "i2c-gpio";
            gpios = <&pioA 32 0 /* sda */
                 &pioA 33 0 /* scl */
                >;
            /*i2c-gpio,sda-open-drain;*/
            /*i2c-gpio,scl-open-drain;*/
            i2c-gpio,delay-us = <5>;    /* ~100 kHz */
            eeprom@50{
                compatible = "eeprom-edid";
                reg = <0x50>;
            };
        };

        spi0: spi@1fff0220{
            compatible = "loongson,ls2k-spi";
            #address-cells = <1>;
            #size-cells = <0>;
            reg = <0x1fff0220 0x10>;
            spidev@0{
                compatible = "rohm,dh2228fv";
                spi-max-frequency = <100000000>;
                reg = <0>;
            };
        };
#endif
#if 1
        rtc0: rtc@1fe07800{
            compatible = "loongson,ls-rtc";
            reg = <0x1fe07800 0x100>;
            interrupt-parent = <&icu>;
            interrupts = <60>;
        };
#endif
#if 1
        /* CAN controller nodes:
         * If you want to use the "can" function,enable the "can"
         * controller by configure general configuration register 0.
         */
        can0: can@1fe00c00{
            compatible = "nxp,sja1000";
            reg = <0x1fe00c00 0xff>;
            nxp,external-clock-frequency = <125000000>;
            interrupt-parent = <&icu>;
            interrupts = <24>;
        };
        can1: can@1fe00d00{
            compatible = "nxp,sja1000";
            reg = <0x1fe00d00 0xff>;
            nxp,external-clock-frequency = <125000000>;
            interrupt-parent = <&icu>;
            interrupts = <25>;
        };
#endif
#if 0
        pwm0: pwm@1fe02000{
            compatible = "loongson,ls2k-pwm";
            reg = <0x1fe02000 0x10>;
            interrupt-parent = <&icu>;
            interrupts = <32>;
        };
        pwm1: pwm@1fe02010{
            compatible = "loongson,ls2k-pwm";
            reg = <0x1fe02010 0x10>;
            interrupt-parent = <&icu>;
            interrupts = <33>;
        };
        pwm2: pwm@1fe02020{
            compatible = "loongson,ls2k-pwm";
            reg = <0x1fe02020 0x10>;
            interrupt-parent = <&icu>;
            interrupts = <34>;
        };
        pwm3: pwm@1fe02030{
            compatible = "loongson,ls2k-pwm";
            reg = <0x1fe02030 0x10>;
            interrupt-parent = <&icu>;
            interrupts = <35>;
        };

#endif
        /* APB DMA controller nodes:
         * apbdma node specify the commom property for dma node.
         * the #config-nr must be 2,Used to provide APB sel region
         * and APB DMA controler information.
         */
        apbdma: apbdma@1fe10438{
            compatible = "loongson,ls-apbdma";
            reg = <0x1fe10438 0x8>;
            #config-nr = <2>;
        };
        /* DMA node should specify the apbdma-sel property using a
         * phandle to the controller followed by number of APB sel
         * region(max 9) and number of APB DMA controller(max 4).
        */
        dma0: dma@1fe10c00 {
            compatible = "loongson,ls-apbdma-0";
            reg = <0x1fe10c00 0x8>;
            apbdma-sel = <&apbdma 0x0 0x0>;
            #dma-cells = <1>;
            dma-channels = <1>;
            dma-requests = <1>;
        };
        dma1: dma@1fe10c10 {
            compatible = "loongson,ls-apbdma-1";
            reg = <0x1fe10c10 0x8>;
            apbdma-sel = <&apbdma 0x5 0x1>;
            #dma-cells = <1>;
            dma-channels = <1>;
            dma-requests = <1>;
        };
        dma2: dma@1fe10c20 {
            compatible = "loongson,ls-apbdma-2";
            reg = <0x1fe10c20 0x8>;
            apbdma-sel = <&apbdma 0x6 0x2>;
            #dma-cells = <1>;
            dma-channels = <1>;
            dma-requests = <1>;
        };
        dma3: dma@1fe10c30 {
            compatible = "loongson,ls-apbdma-3";
            reg = <0x1fe10c30 0x8>;
            apbdma-sel = <&apbdma 0x7 0x3>;
            #dma-cells = <1>;
            dma-channels = <1>;
            dma-requests = <1>;
        };
        /*
        dma4: dma@1fe10c40 {
            compatible = "loongson,ls-apbdma-4";
            apbdma-sel = <&apbdma 0x0 0x0>;
            reg = <0x1fe10c40 0x8>;
            #dma-cells = <1>;
            dma-channels = <1>;
            dma-requests = <1>;
        };
        */
#if 0
        sdio@0x1fe0c000 {
            #address-cells = <1>;
            #size-cells = <1>;

            compatible = "loongson,ls2k_sdio";
            reg = <0x1fe0c000 0x1000>;
            interrupt-parent = <&icu>;
            interrupts = <39>;
            interrupt-names = "ls2k_mci_irq";

            dmas = <&dma1 1>;
            dma-names = "sdio_rw";
            dma-mask = <0xffffffff 0xffffffff>;
        };
#endif
#if 0
        i2s: i2s@0x1fe0d000 {
            compatible = "loongson,ls-i2s";
            reg = <0x1fe0d000 0x10>;
        };
        audio: audio@0x1fe0d000 {
            compatible = "loongson,ls-pcm-audio";
            reg = <0x1fe0d000 0x10>;
            interrupt-parent = <&icu>;
            interrupts = <54 55>;
            interrupt-names = "i2s_irq0", "i2s_irq1";

            dmas = <&dma2 1
                    &dma3 1>;
            dma-names = "i2s_play", "i2s_record";
            dma-mask = <0xffffffff 0xffffffff>;
        };
        sound {
            compatible = "loongson,ls-sound";
            loongson,i2s-controller = <&i2s>;
            loongson,audio-codec = <&audio>;
        };
#endif
#if 0
        nand@0x1fe06040{
            #address-cells = <1>;
            #size-cells = <1>;
            compatible = "loongson,ls-nand";
            reg = <0x1fe06040 0x0
                0x1fe06000 0x20>;
            interrupt-parent = <&icu>;
            interrupts = <52>;
            interrupt-names = "nand_irq";

            dmas = <&dma0 1>;
            dma-names = "nand_rw";
            dma-mask = <0xffffffff 0xffffffff>;

            number-of-parts = <0x2>;

            partition@0 {
                label = "kernel_partition";
                reg = <0x0000000 0x01400000>;
            };

            partition@0x01400000 {
                label = "os_partition";
                reg = <0x01400000 0x0>;
            };
        };
#endif
        hwmon@0x1fe07000{
            #address-cells = <1>;
            #size-cells = <1>;

            compatible = "loongson,ls-hwmon";
            reg = <0x1fe07000 0x1>;
            max-id = <0>;
            id = <0>;
        };
    };

    suspend_to_ram {
        suspend_addr = <0x1fc00500>;
    };

    gpio-conf422 {
        compatible = "gpio-leds";

        re0 {
            label = "re0";
            gpios = <&pioA 50 0>;
        };
        de0 {
            label = "de0";
            gpios = <&pioA 51 0>;
            default-state = "on";
        };
        re1 {
            label = "re1";
            gpios = <&pioA 48 0>;
        };
        de1 {
            label = "de1";
            gpios = <&pioA 49 0>;
            default-state = "on";            
        };
    };

    chosen {
        /* the redundant spaces are used for save new boot args */
//        bootargs = "console=ttyS0,115200 root=/dev/sda1                                                                                                                                                                                      ";
    };
};
 

编译pmon,

我开始使用load_dtb 似乎有些问题,最后只好更新一把pmon

print_dtb就有了

 

编译好内核之后,直接用新的内核启动就可以了。在class中多了leds的目录,目录中有这4个引脚。

 因为我需要DE为高电平,RE为低电平,看到打印的结果确实也是。用万用表测量了,也是对应的,没问题了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大智兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值