编写GPIO使用Pinctrl

需要做的事情

  1. 表明GPIO和Pinctrl间的联系
  2. 解析这些联系
  3. 编写代码

编写设备树

/ {
	    pinctrl_virt: virtual_pincontroller {
        compatible = "100ask,virtual_pinctrl";
        i2cgrp: i2cgrp {
                functions = "i2c", "i2c";
                groups = "pin0", "pin1";
                configs = <0x11223344  0x55667788>;
        };
    };

    gpio_virt: virtual_gpiocontroller {
        compatible = "100ask,virtual_gpio";
        gpio-controller;
        #gpio-cells = <2>;
        ngpios = <4>;
        gpio-ranges = <&pinctrl_virt 0 0 4>;
    };

    myled {
        compatible = "100ask,led_drv";
        myled-gpios = <&gpio_virt 0 GPIO_ACTIVE_LOW>;
    };
};

编写代码

GPIO控制器编程

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/gpio/driver.h>
#include <linux/gpio.h>

static struct gpio_chip *g_virt_gpio;
static int g_gpio_val = 0;

static const struct of_device_id virtual_gpio_of_match[] = {
	{ .compatible = "100ask,virtual_gpio", },
	{ /* sentinel */ }
};

static int virt_gpio_direction_output(struct gpio_chip *gc,
		unsigned offset, int val)
{
	printk("%s %d\n", __FUNCTION__, __LINE__);
	printk("set pin %d as output %s\n", offset, val ? "high" : "low");
	return 0;
}

static int virt_gpio_direction_input(struct gpio_chip *gc,
		unsigned offset)
{
	printk("%s %d\n", __FUNCTION__, __LINE__);
	printk("set pin %d as input\n", offset);
	return 0;
}

static int virt_gpio_get_value(struct gpio_chip *gc, unsigned offset)
{
	printk("%s %d\n", __FUNCTION__, __LINE__);
	return (g_gpio_val & (1<<offset)) ? 1: 0;
}

static void virt_gpio_set_value(struct gpio_chip *gc,
		unsigned offset, int val)
{
	printk("%s %d val:%d\n", __FUNCTION__, __LINE__, val);
	if(val) {
		g_gpio_val |= (1<<offset);
	} else {
		g_gpio_val &= ~(1<<offset);
	}
}

static int virtual_gpio_probe(struct platform_device *pdev)
{
	int ret = 0;
	unsigned int val;
	printk("%s %d\n", __FUNCTION__, __LINE__);
	g_virt_gpio = devm_kzalloc(&pdev->dev, sizeof(*g_virt_gpio), GFP_KERNEL);

	g_virt_gpio->label = pdev->name;
	g_virt_gpio->direction_output = virt_gpio_direction_output;
	g_virt_gpio->direction_input = virt_gpio_direction_input;
	g_virt_gpio->get = virt_gpio_get_value;
	g_virt_gpio->set = virt_gpio_set_value;
	g_virt_gpio->request = gpiochip_generic_request;

	g_virt_gpio->parent = &pdev->dev;
	g_virt_gpio->owner = THIS_MODULE;

	g_virt_gpio->base = -1;
	ret = of_property_read_u32(pdev->dev.of_node, "ngpios", &val);
	g_virt_gpio->ngpio = val;

	ret = devm_gpiochip_add_data(&pdev->dev, g_virt_gpio, NULL);

	return 0;
}

static int virtual_gpio_remove(struct platform_device *pdev)
{
	printk("%s %d\n", __FUNCTION__, __LINE__);
	return 0;
}

static struct platform_driver virtual_gpio_driver = {
	.driver = {
		.name = "vitual-gpio",
		.of_match_table = of_match_ptr(virtual_gpio_of_match),
	},
	.probe = virtual_gpio_probe,
	.remove = virtual_gpio_remove,
};

static int virtual_gpio_init(void)
{
	printk("%s %d\n", __FUNCTION__, __LINE__);
	return platform_driver_register(&virtual_gpio_driver);
}

static void virtual_gpio_exit(void)
{
	printk("%s %d\n", __FUNCTION__, __LINE__);
	platform_driver_unregister(&virtual_gpio_driver);
}

module_init(virtual_gpio_init);
module_exit(virtual_gpio_exit);
MODULE_LICENSE("GPL");

Pinctrl编程

#include <linux/init.h>
#include <linux/module.h>
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/slab.h>
#include "core.h"

static struct pinctrl_dev *g_pinctrl_dev;
static const struct pinctrl_pin_desc pins[] = {
	{ 0, "pin0", NULL },	//描述4个引脚,有num,名字,私有数据
	{ 1, "pin1", NULL },
	{ 2, "pin2", NULL },
	{ 3, "pin3", NULL },
};

static unsigned long g_configs[4];

struct virtual_functions_desc {
	const char *func_name;
	const char **groups;
	int num_groups;
};

static const char *function_group0[] = {"pin0", "pin1", "pin2", "pin3"};
static const char *function_group1[] = {"pin0", "pin1"};
static const char *function_group2[] = {"pin2", "pin3"};
static struct virtual_functions_desc g_funcs_des[] = {	//不同的复用功能对应的引脚
	{ "gpio", function_group0, 4 },
	{ "i2c",  function_group1, 2 },
	{ "uart", function_group2, 2 },
};

//从引脚信息中获取引脚个数
static int virtual_get_groups_count(struct pinctrl_dev *pctldev)
{
	return pctldev->desc->npins;
}

//直接返回引脚的名字
static const char *virtual_get_group_name(struct pinctrl_dev *pctldev, unsigned selector)
{
	return pctldev->desc->pins[selector].name;
}

//获取组的所有引脚信息
static int virtual_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
		const unsigned **pins, unsigned *npins)
{
	if(selector >= pctldev->desc->npins)
		return -EINVAL;

	*pins = &pctldev->desc->pins[selector].number;	//引脚组的序号
	*npins = 1;

	return 0;
}

//打印debug信息
static void virtual_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned offset)
{
	seq_printf(s, "%s", dev_name(pctldev->dev));
}

/*
	i2cgrp: i2cgrp {
			functions = "i2c", "i2c";
			groups = "pin0", "pin1";
			configs = <0x11223344  0x55667788>;
	};

	one pin ===> two pinctrl_map (one for mux, one for config)
*/

//把设备树中的节点转换成pinctrl_map
static int virtual_dt_node_to_map(struct pinctrl_dev *pctldev, struct device_node *np,
		struct pinctrl_map **map, unsigned *num_maps)
{
	int i;
	int num_pins = 0;
	const char *pin;
	const char *function;
	unsigned int config;	
	struct pinctrl_map *new_map;
	unsigned long *configs;

	/* 1.确定引脚个数 */	
	while(1) {
		if(of_property_read_string_index(np, "groups", num_pins, &pin) == 0)
			num_pins++;
		else
			break;
	}

	new_map = kmalloc(sizeof(struct pinctrl_map) * num_pins * 2, GFP_KERNEL);
	printk("num_pins %d\n", num_pins);

	for(i=0;i<num_pins;i++) {
		/* 2.取出引脚 */
		of_property_read_string_index(np, "groups", i, &pin);
		of_property_read_string_index(np, "functions", i, &function);
		of_property_read_u32_index(np, "configs", i, &config);

		printk("%d:%s %s %x\n", i,pin, function, config);
		/* 3.存入pinctrol_map */
		configs = kmalloc(sizeof(*configs), GFP_KERNEL);

		/* 设置复用值 */
		new_map[i*2].type = PIN_MAP_TYPE_MUX_GROUP;
		new_map[i*2].data.mux.function= function;
		new_map[i*2].data.mux.group = pin;
		/* 设置配置值 */
		new_map[i*2+1].type = PIN_MAP_TYPE_CONFIGS_PIN;
		new_map[i*2+1].data.configs.group_or_pin = pin;
		new_map[i*2+1].data.configs.configs = configs;
		configs[0] = config;
		new_map[i*2+1].data.configs.num_configs = 1;
	}

	*map = new_map;
	*num_maps = num_pins * 2;

	return 0;
}

//释放pinctrl_map
static void virtual_dt_free_map(struct pinctrl_dev *pctldev,
		struct pinctrl_map *map,
		unsigned num_maps)
{
	while(num_maps--) {
		if(map->type == PIN_MAP_TYPE_CONFIGS_PIN)
			kfree(map->data.configs.configs);

		kfree(map);
		map++;
	}
}

//把每个引脚作为一组
static const struct pinctrl_ops virtual_pinctrl_ops = {
	.get_groups_count = virtual_get_groups_count,
	.get_group_name = virtual_get_group_name,
	.get_group_pins = virtual_get_group_pins,
	.pin_dbg_show = virtual_pin_dbg_show,
	.dt_node_to_map = virtual_dt_node_to_map,
	.dt_free_map = virtual_dt_free_map,
};

//可以复用的功能种类
static int virtual_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
{
	return ARRAY_SIZE(g_funcs_des);
}

//指定序号中,可以复用的功能名字
static const char *virtual_pmx_get_func_name(struct pinctrl_dev *pctldev, unsigned selector)
{
	return g_funcs_des[selector].func_name;
}

//获得这个功能下,支持的组的信息
static int virtual_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
		const char *const **groups,
		unsigned * const num_groups)
{
	*groups = g_funcs_des[selector].groups;
	*num_groups = g_funcs_des[selector].num_groups;

	return 0;
}

//对指定组设置为指定功能
static int virtual_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
		unsigned group)
{
	printk("set %s as %s\n", pctldev->desc->pins[group].name,
			g_funcs_des[selector].func_name);
	return 0;
}

static int virtual_pmx_request_enable(struct pinctrl_dev *pctldev,
                    struct pinctrl_gpio_range *range,
                    unsigned offset)
{
	printk("set and enable pin %s as GPIO\n", pctldev->desc->pins[offset].name);
	return 0;
}

//复用功能的结构体
static const struct pinmux_ops virtual_pmx_ops = {
	.get_functions_count = virtual_pmx_get_funcs_count,
	.get_function_name = virtual_pmx_get_func_name,
	.get_function_groups = virtual_pmx_get_groups,
	.set_mux = virtual_pmx_set,
	.gpio_request_enable = virtual_pmx_request_enable,
};

//获取引脚的配置值
static int virtual_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin_id,
		unsigned long *config)
{
	*config = g_configs[pin_id];
	return 0;
}

//设置引脚的配置
static int virtual_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id,
		unsigned long *configs, unsigned num_configs)
{
	if(num_configs != 1)
		return -EINVAL;

	g_configs[pin_id] = *configs;
	printk("config %s as 0x%lx\n", pctldev->desc->pins[pin_id].name, *configs);
	return 0;
}

//调试信息
static void virtual_pinconf_dbg_show(struct pinctrl_dev *pctldev,
		struct seq_file *s, unsigned pin_id)
{
	seq_printf(s, "0x%lx", g_configs[pin_id]);
}

static void virtual_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
		struct seq_file *s, unsigned pin_id)
{
	seq_printf(s, "0x%lx", g_configs[pin_id]);
}

//配置引脚功能
static const struct pinconf_ops virtual_pinconf_ops = {
	.pin_config_get = virtual_pinconf_get,
	.pin_config_set = virtual_pinconf_set,
	.pin_config_dbg_show = virtual_pinconf_dbg_show,
	.pin_config_group_dbg_show = virtual_pinconf_group_dbg_show,
};

static int virtual_pinctrl_probe(struct platform_device *pdev)
{
	struct pinctrl_desc *pinctrl;
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	
	/* 1.分配pinctrl_desc */
	pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);

	/* 2.设置pinctrl_desc */
	pinctrl->name = dev_name(&pdev->dev);
	pinctrl->owner = THIS_MODULE;

	/* b.1 pins and group */
	pinctrl->pins = pins;
	pinctrl->npins = ARRAY_SIZE(pins);		//多少个引脚
	pinctrl->pctlops = &virtual_pinctrl_ops;	//组的信息

	/* b.2 pins mux */
	pinctrl->pmxops = &virtual_pmx_ops;		//设置引脚的复用

	/* b.3 pins config */
	pinctrl->confops = &virtual_pinconf_ops;	//设备引脚配置	

	/* 3.注册pinctrl_desc */
	g_pinctrl_dev = devm_pinctrl_register(&pdev->dev, pinctrl, NULL);

	return 0;
}

static int virtual_pinctrl_remove(struct platform_device *pdev)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static struct of_device_id virtual_pinctrl_of_match[] = {
	{ .compatible = "100ask,virtual_pinctrl", }, //通过这个值来查找设备树节点
	{ },
};

static struct platform_driver virtual_pinctrl_driver = {
	.probe = virtual_pinctrl_probe,
	.remove = virtual_pinctrl_remove,
	.driver = {
		.name = "100ask_virtual_pinctrl",
		.of_match_table = of_match_ptr(virtual_pinctrl_of_match),
	}
};

/* 1.入口函数 */
static int __init virtual_pinctrl_init(void)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	return  platform_driver_register(&virtual_pinctrl_driver);
}

/* 2. 出口函数 */
static void __exit virtual_pinctrl_exit(void)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	platform_driver_unregister(&virtual_pinctrl_driver);
}

module_init(virtual_pinctrl_init);
module_exit(virtual_pinctrl_exit);
MODULE_LICENSE("GPL");

led驱动和app

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/platform_device.h>


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 = "myled,led_drv"},
	{ },
};


static int chip_demoo_gpio_probe(struct platform_device *pdev)
{
	led_gpio = gpiod_get(&pdev->dev, "myled", 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");
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

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;
}

实机测试

root@npi:/mnt/home/picture/gpio/pinctrl_gpio# insmod virtual_pinctrl_driver.ko
[   55.733930] virtual_pinctrl_driver: loading out-of-tree module taints kernel.
[   55.744671] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/virtual_pinctrl_driver.c virtual_pinctrl_init 295
[   55.762975] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/virtual_pinctrl_driver.c virtual_pinctrl_probe 246
root@npi:/mnt/home/picture/gpio/pinctrl_gpio# insmod virtual_gpio.ko
[   62.476357] virtual_gpio_init 93
[   62.486355] virtual_gpio_probe 54
root@npi:/mnt/home/picture/gpio/pinctrl_gpio# insmod led_drv.ko
[   65.890330] set and enable pin pin0 as GPIO
[   65.904123] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_initled_drv_init
root@npi:/mnt/home/picture/gpio/pinctrl_gpio# ./led_app /dev/myled0 on
[   75.980249] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_open line 21
[   75.989419] virt_gpio_direction_output 20
[   75.993441] set pin 0 as output high
[   75.997044] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_write line 38
[   76.009667] write status:1
[   76.013337] virt_gpio_set_value 42 val:0
app write device 1[   76.018402] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_close line 49
root@npi:/mnt/home/picture/gpio/pinctrl_gpio# ./led_app /dev/myled0 off
[   78.048167] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_open line 21
[   78.058208] virt_gpio_direction_output 20
[   78.064544] set pin 0 as output high
[   78.068170] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_write line 38
[   78.079450] write status:0
[   78.082276] virt_gpio_set_value 42 val:1
app write device 0[   78.087349] /home/book/100ask_rk3288_module/15_gpio/04_gpio_pinctrl/led_drv.c led_drv_close line 49
root@npi:/mnt/home/picture/gpio/pinctrl_gpio#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

习惯就好zz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值