openwrt MT7688 led/key configuration

Configure the LED process:
1. Modify the device tree, add led nodes
2. View gpio multiplexing, configure the port corresponding to the led to gpio mode
3. Add led trigger mode

MT7688 has 47 IOs, divided into two groups of control, gpio0[0-31], gpio1[32-46]

1. Add led node
led-wifi: gpio42 corresponds to gpio1_10
led-usb: gpio41 corresponds to gpio1_9
(here led-usb just defines the name of the led, which can be used for the usb state or other, such as the lan port state)

	leds {
    
    
		compatible = "gpio-leds";
		
		// led_wifi: wifi {
    
    
		// 	label = "linkit-smart-7688:orange:wifi";
		// 	gpios = <&gpio1 12 GPIO_ACTIVE_LOW>;
		// };

		// gpio42-gpio1_10
		led_wifi: wifi {
    
    
			label = "led-wifi";
			gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "phy0tpt";
		};

		// gpio41-gpio1_9 
		led_usb: usb {
    
    
			label = "led-usb";
			gpios = <&gpio1 9 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "usbport";
		};
	};

2. Modify gpio multiplexing
view build_dir/target-mipsel_24kc_musl/linux-ramips_mt76x8/linux-4.14.180/arch/mips/ralink/mt7620.c

From prom_soc_init, it can be determined that 7688 is using mt7628an_pinmux_data. Different models use different pinmaps.

	if (is_mt76x8())
		rt2880_pinmux_data = mt7628an_pinmux_data;
	else
		rt2880_pinmux_data = mt7620a_pinmux_data;

mt7628an_pinmux_data is defined as follows:

static struct rt2880_pmx_group mt7628an_pinmux_data[] = {
    
    
	GRP_G("pwm1", pwm1_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_PWM1),
	GRP_G("pwm0", pwm0_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_PWM0),
	GRP_G("uart2", uart2_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_UART2),
	GRP_G("uart1", uart1_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_UART1),
	GRP_G("i2c", i2c_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_I2C),
	GRP("refclk", refclk_grp_mt7628, 1, MT7628_GPIO_MODE_REFCLK),
	GRP("perst", perst_grp_mt7628, 1, MT7628_GPIO_MODE_PERST),
	GRP("wdt", wdt_grp_mt7628, 1, MT7628_GPIO_MODE_WDT),
	GRP("spi", spi_grp_mt7628, 1, MT7628_GPIO_MODE_SPI),
	GRP_G("sdmode", sd_mode_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_SDMODE),
	GRP_G("uart0", uart0_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_UART0),
	GRP_G("i2s", i2s_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_I2S),
	GRP_G("spi cs1", spi_cs1_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_CS1),
	GRP_G("spis", spis_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_SPIS),
	GRP_G("gpio", gpio_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_GPIO),
	GRP_G("wled_an", wled_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_WLED_AN),
	GRP_G("p0led_an", p0led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P0LED_AN),
	GRP_G("p1led_an", p1led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P1LED_AN),
	GRP_G("p2led_an", p2led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P2LED_AN),
	GRP_G("p3led_an", p3led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P3LED_AN),
	GRP_G("p4led_an", p4led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P4LED_AN),
	GRP_G("wled_kn", wled_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_WLED_KN),
	GRP_G("p0led_kn", p0led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P0LED_KN),
	GRP_G("p1led_kn", p1led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P1LED_KN),
	GRP_G("p2led_kn", p2led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P2LED_KN),
	GRP_G("p3led_kn", p3led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P3LED_KN),
	GRP_G("p4led_kn", p4led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
				1, MT7628_GPIO_MODE_P4LED_KN),
	{
    
     0 }
};

gpio41 and gpio42 correspond to the above p1led_an and p2led_an
device trees respectively, set these two groups of gpio to gpio mode

state_default: pinctrl0 {
    
    
		gpio {
    
    
			// eric++  p0led_an as gpio
			ralink,group = "gpio","p0led_an","p1led_an","p2led_an","p3led_an","p4led_an";
			ralink,function = "gpio";
		};

	};

3. Menuconfig, open the corresponding led driver module

 .config - OpenWrt Configuration
 > Kernel modules > LED modules ───────────────────────────────────────────────────────────────
  ┌────────────────────────────────────── LED modules ──────────────────────────────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----). │  
  │  Highlighted letters are hotkeys.  Pressing <Y> includes, <N> excludes, <M> modularizes │  
  │  features.  Press <Esc><Esc> to exit, <?> for Help, </> for Search.  Legend: [*]        │  
  │  built-in  [ ] excluded  <M> module  < > module capable                                 │  
  │ ┌─────────────────────────────────────────────────────────────────────────────────────┐ │  
  │ │       <*> kmod-leds-gpio.......................................... GPIO LED support │ │  
  │ │       < > kmod-leds-pca963x.................................... PCA963x LED support │ │  
  │ │       <*> kmod-ledtrig-default-on........................... LED Default ON Trigger │ │  
  │ │       <*> kmod-ledtrig-gpio....................................... LED GPIO Trigger │ │  
  │ │       <*> kmod-ledtrig-heartbeat............................. LED Heartbeat Trigger │ │  
  │ │       <*> kmod-ledtrig-netdev................................... LED NETDEV Trigger │ │  
  │ │       <*> kmod-ledtrig-oneshot................................ LED One-Shot Trigger │ │  
  │ │       <*> kmod-ledtrig-timer..................................... LED Timer Trigger │ │  
  │ └───────v(+)──────────────────────────────────────────────────────────────────────────┘ │  
  ├─────────────────────────────────────────────────────────────────────────────────────────┤  
  │                <Select>    < Exit >    < Help >    < Save >    < Load >                 │  
  └─────────────────────────────────────────────────────────────────────────────────────────┘  
    

4. Compile and view device nodes

root@OpenWrt:/sys/devices/platform/leds/leds# ls
led-usb   led-wifi

5. Test the led control
1. Turn off the trigger mode, echo none> trigger
2. Turn on the led, echo 1> brightness
3. Turn off the led, echo 0> brightness

root@OpenWrt:/sys/devices/platform/leds/leds/led-usb# ls
brightness      interval        rx              tx
device          link            subsystem       uevent
device_name     max_brightness  trigger
root@OpenWrt:/sys/devices/platform/leds/leds/led-usb# cat trigger
none switch0 timer default-on [netdev] gpio heartbeat oneshot usbport phy0rx phy0tx phy0assoc phy0radio phy0tpt
root@OpenWrt:/sys/devices/platform/leds/leds/led-usb#
root@OpenWrt:/sys/devices/platform/leds/leds/led-usb#
root@OpenWrt:/sys/devices/platform/leds/leds/led-usb# echo none > trigger
root@OpenWrt:/sys/devices/platform/leds/leds/led-usb# echo 1 > brightness
root@OpenWrt:/sys/devices/platform/leds/leds/led-usb# echo 0 > brightness

6. Configure led to automatically trigger
target/linux/ramips/base-files/etc/board.d/01_leds
Configure led-wifi, wifi data transmission flashing
Configure led-usb, flashing data transmission on lan port

linkits7688)
	# ucidef_set_led_wlan "wifi" "wifi" "linkit-smart-7688:orange:wifi" "phy0tpt"
	# eric++
	ucidef_set_led_wlan "wifi" "wifi" "led-wifi" "phy0tpt"
	ucidef_set_led_netdev "lan" "lan" "led-usb" "eth0"
	;;

7. Button configuration

For details, please refer to https://openwrt.org/docs/guide-user/hardware/hardware.button?s[]=rc&s[]=button

If it is found that the following log appears during system startup

kernel: [    3.590000] Error: Driver 'gpio-keys-polled' is already registered, aborting...

Need to turn off the conflicting configuration, only keep kmod-input-gpio-keys-polled
CONFIG_PACKAGE_kmod-input-gpio-*
CONFIG_PACKAGE_kmod-button-hotplug

8. Modify the device tree and add the corresponding button description

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

		// gpio38----gpio1_6
		// wps {
    
    
		// 	label = "wps";
		// 	gpios = <&gpio1 6 GPIO_ACTIVE_LOW>;
		// 	linux,code = <KEY_WPS_BUTTON>;
		// };
		// reboot {
    
    
		// 	label = "reboot";
		// 	gpios = <&gpio1 6 GPIO_ACTIVE_LOW>;
		// 	linux,code = <KEY_POWER2>;
		// };
		reset {
    
    
			label = "reset";
			gpios = <&gpio1 6 GPIO_ACTIVE_LOW>;
			linux,code = <KEY_RESTART>;
		};
	};

9. Enter the system to view gpio status

root@OpenWrt:/sys/kernel/debug# cat gpio
gpiochip0: GPIOs 0-31, parent: platform/10000600.gpio, 10000600.gpio:

gpiochip1: GPIOs 32-63, parent: platform/10000600.gpio, 10000600.gpio:
 gpio-38  (                    |reboot              ) in  hi
 gpio-41  (                    |led-usb             ) out lo
 gpio-42  (                    |led-wifi            ) out lo

gpiochip2: GPIOs 64-95, parent: platform/10000600.gpio, 10000600.gpio:

10. You can view files for specific key-press actions

package/base-files/files/etc/rc.button/reboot
package/base-files/files/etc/rc.button/reset


Only this record

Guess you like

Origin blog.csdn.net/pyt1234567890/article/details/107683203