在100ask_imx6ull上机实验

确定引脚并生成设备树节点

NXP公司对于IMX6ULL芯片,有设备树生成工具。安装Pins_Tool_for_i.MX_Process_v6_x64.exe后运行,打开IMX6ULL的配置文件MCIMX62Y2xx08.mex就可以在GUI界面中选择引脚,配置它的功能,这就i可以自动生成Pinctrl的子节点信息。
100ASK_IMX6ULL使用的LED原理图如下,可知因骄傲是GPIO5_3:
在100ask_imx6ull上机实验_第1张图片

然而不幸的是我只有野火的板子,而野火的板子的LED居然IO口也是一样的:
在100ask_imx6ull上机实验_第2张图片
可以使用NXP的设备树工具纪念性配置,然后把配置出来的代码放在内核源码里。

在pinctrol中设置引脚的配置信息:

&iomuxc_snvs {
//....
    imx6ul-evk {    
    myled_for_gpio_subsys: myled_for_gpio_subsys{ 
            fsl,pins = <
                MX6ULL_PAD_SNVS_TAMPER3__GPIO5_IO03        0x000110A0
            >;
        };
//....
}

然后添加led的设备节点:

        myled {
            compatible = "100ask,leddrv";
            pinctrl-names = "default";
            pinctrl-0 = <&myled_for_gpio_subsys>;
            led-gpios = <&gpio5 3 GPIO_ACTIVE_LOW>;
        };

编译程序

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


static int major = 0;
static struct class *led_class;
struct gpio_desc *led_gpio;

static int led_drv_open(struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	gpiod_direction_output(led_gpio, 0);
	return 0;
}

static ssize_t led_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	char status;

	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(&status, buf, 1);
	printk("write status:%x \n", status);

	gpiod_set_value(led_gpio, status);
	
	return 1;
}

static int led_drv_close(struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}


static struct file_operations led_drv = {
	.owner = THIS_MODULE,
	.open = led_drv_open,
	.read = led_drv_read,
	.write = led_drv_write,
	.release = led_drv_close,
};


static const struct of_device_id myleds[] = {
	{ .compatible = "100ask,leddrv"},
	{ },
};


static int chip_demoo_gpio_probe(struct platform_device *pdev)
{
	led_gpio = gpiod_get(&pdev->dev, "led", 0);
	if(IS_ERR(led_gpio)) {
		dev_err(&pdev->dev, "Failed to get GPIO for led\n");
		return PTR_ERR(led_gpio);
	}

	major = register_chrdev(0, "myled", &led_drv);
	led_class = class_create(THIS_MODULE, "led_class");
	if(IS_ERR(led_class)) {
		unregister_chrdev(major, "myled");	
		gpiod_put(led_gpio);
		printk(KERN_WARNING "class create failed\n");
		return PTR_ERR(led_class);
	}

	device_create(led_class, NULL, MKDEV(major, 0), NULL, "myled%d", 0);

	return 0;
}

static int chip_demoo_gpio_remove(struct platform_device *pdev)
{
	device_destroy(led_class, MKDEV(major, 0));
	class_destroy(led_class); 
	unregister_chrdev(major, "myled");	
	gpiod_put(led_gpio);
	return 0;
}

static struct platform_driver chip_demoo_gpio_driver = {
	.probe    = chip_demoo_gpio_probe,
	.remove   = chip_demoo_gpio_remove,
	.driver   = {
		.name = "myled",
		.of_match_table = myleds,
	},
};

static int __init led_drv_init(void)
{
	int err;
	err = platform_driver_register(&chip_demoo_gpio_driver);

	printk("%s %sled_drv_init\n", __FILE__, __FUNCTION__);
	return 0;
}

static void __exit led_drv_exit(void)
{
	printk("%s %sled_drv_exit\n", __FILE__, __FUNCTION__);

	platform_driver_unregister(&chip_demoo_gpio_driver);
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chen");

app:

#include 
#include 
#include 
#include 
#include 
#include 

void showUsage(void)
{
	printf("app [dev_path] [on,off]\n");
}

int main(int argc, char *argv[])
{
	char status;
	if(argc < 3) {
		showUsage();
		return -1;
	}

	int fd = open(argv[1], O_RDWR);
	if(fd < 0) {
		printf("app open device failed path:%s", argv[1]);
		return -1;
	}

	if(0 == strcmp(argv[2], "on")) {
		status = 1;
		int ret = write(fd, &status, 1);
		if(ret <= 0) {
			printf("app write device fialed %s",argv[2]);
			return -1;
		} else {
			printf("app write device %x", status);
		}

	} else if(0 == strcmp(argv[2], "off")) {
		status = 0;
		int ret = write(fd, &status, 1);
		if(ret <= 0) {
			printf("app write device fialed %s",argv[2]);
			return -1;
		} else {
			printf("app write device %x", status);
		}
	}


	return 0;
}

你可能感兴趣的:(#,GPIO子系统,100ask,imx6ull,gpio,led,kernel)