1、截取linux下pinctrl子系统,内容如下:
&iomuxc {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_hog_1>;
imx6ul-evk {
pinctrl_gpio_leds: gpio-leds {
fsl,pins = <
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x17059
>;
};
pinctrl_gpio_keys: gpio-keys {
fsl,pins = <
MX6UL_PAD_UART1_CTS_B__GPIO1_IO18 0x80000000
>;
};
pinctrl_flexcan1: flexcan1grp{
fsl,pins = <
MX6UL_PAD_UART3_RTS_B__FLEXCAN1_RX 0x1b020
MX6UL_PAD_UART3_CTS_B__FLEXCAN1_TX 0x1b020
>;
};
};
};
a) 根据上一节内容,我们可以知道,在iomuxc节点下,又增加了imx6ul-evk子节点,在此子节点下,又增加了各个pinctrl子节点。
pinctrl子节点的写法如下:
pinctrl_test: testgrp
{
fsl,pins = <PIN的具体内容>;
};
b) 通过查看/home/wyd/linux/kernel/arch/arm/boot/dts/imx6ul-pinfunc.h,可以分析出宏定义满足如下关系
#define MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x0068 0x02F4 0x0000 0x5 0x0
<mux_reg pad_reg input_reg mux_mode input_val>
c)找出与pinctrl相关联的设备节点文件
leds {
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gpio_leds
&pinctrl_beep>;
led1{
label = "sys-led";
gpios = <&gpio1 3 GPIO_ACTIVE_LOW>;
linux,default-trigger = "heartbeat";
default-state = "on";
};
beep{
label = "beep";
gpios = <&gpio5 1 GPIO_ACTIVE_LOW>;
default-state = "off";
};
};
gpio_keys: gpio_keys@0 {
compatible = "gpio-keys";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gpio_keys>;
#address-cells = <1>;
#size-cells = <0>;
autorepeat;
key1@1 {
label = "USER-KEY1";
linux,code = <114>;
gpios = <&gpio1 18 GPIO_ACTIVE_LOW>;
gpio-key,wakeup;
};
};
&flexcan1 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_flexcan1>;
xceiver-supply = <®_can_3v3>;
status = "okay";
};
可以分析出来其他外设引用pinctrl需要配置的属性参数
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gpio_leds>
2、因此,我们可以根据设备树的信息,抄袭内容设置自己的gpio
a) 抄袭原厂源码:pinctrl_gpio_leds: gpio-leds
/*
pinctrl_gpio_leds: gpio-leds {
fsl,pins = <
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x17059
>;
};
*/
pinctrl_led: ledgrp {
fsl,pins = <
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x10B0
>;
};
b)抄袭leds的原厂源码
gpioled {
compatible = "atkalpha-gpioled";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_led>;
led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;
};
/*
leds {
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gpio_leds
&pinctrl_beep>;
led1{
label = "sys-led";
gpios = <&gpio1 3 GPIO_ACTIVE_LOW>;
linux,default-trigger = "heartbeat";
default-state = "on";
};
beep{
label = "beep";
gpios = <&gpio5 1 GPIO_ACTIVE_LOW>;
default-state = "off";
};
};
*/
由pinctrl-0 = <&pinctrl_led>; led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;可知这两句完成了管脚和GPIO端口的绑定。
c)编写驱动程序
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
struct device_node *nd;
int led_gpio;
static int __init led_init(void)
{
int ret, i;
nd = of_find_node_by_path("/gpioled");
if( nd == NULL )
{
printk(KERN_ALERT "/gpioled not find\r\n");
return -EINVAL;
}
else
{
printk(KERN_ALERT "/gpioled dts find\r\n");
}
led_gpio = of_get_named_gpio( nd, "led-gpio", 0);
if( led_gpio < 0 )
{
printk(KERN_ALERT "led_gpio not get\r\n");
return -EINVAL;
}
else
{
printk(KERN_ALERT "led_gpio = %d\r\n", led_gpio);
}
ret = gpio_direction_output(led_gpio, 1);
for( i = 0; i < 10; i++)
{
msleep(100);
gpio_set_value(led_gpio, 0);
msleep(100);
gpio_set_value(led_gpio, 1);
}
return 0;
}
static void __exit led_exit(void)
{
}
module_init( led_init );
module_exit( led_exit );
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ddd");
加载驱动程序的时候,led灯闪亮9次。