需要做的事情
- 表明GPIO和Pinctrl间的联系
- 解析这些联系
- 编写代码
编写设备树
/ {
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", },
{ }
};
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 },
{ 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;
}
static void virtual_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned offset)
{
seq_printf(s, "%s", dev_name(pctldev->dev));
}
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;
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++) {
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);
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;
}
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__);
pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
pinctrl->name = dev_name(&pdev->dev);
pinctrl->owner = THIS_MODULE;
pinctrl->pins = pins;
pinctrl->npins = ARRAY_SIZE(pins);
pinctrl->pctlops = &virtual_pinctrl_ops;
pinctrl->pmxops = &virtual_pmx_ops;
pinctrl->confops = &virtual_pinconf_ops;
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),
}
};
static int __init virtual_pinctrl_init(void)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return platform_driver_register(&virtual_pinctrl_driver);
}
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
[ 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
[ 62.476357] virtual_gpio_init 93
[ 62.486355] virtual_gpio_probe 54
root@npi:/mnt/home/picture/gpio/pinctrl_gpio
[ 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
[ 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
[ 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