泰山派学习14--pinctrl、gpio子系统控制设备树LED

一、pinctrl子系统

  pinctrl⼦系统主要是管理pin的电⽓属性和复⽤功能,

  1、pin的电气属性设置:例如配置pin上拉、下拉,pin的驱动能力等

  2、pin的复用功能配置:除了设置为普通的GPIO功能外,通过pin group的搭配定义该组pin为特定的功能,如I2C、SPI、UART等。

  参考DTS设备树中i2c的pinctrl与gpio的配置关联

  i2c4: i2c@ff3d0000{

    compatible = "rockchip,rk3566-i2c";

    .......

    pinctrl-names = "default", "gpio" ;

    pinctrl-0 = "i2c4_xfer"; // pinctrl-0 定义了状态 0 (即 default)时需要设置的 pinctrl: &i2c4_xfer, 设置GPIO复⽤为I2C.

    pinctrl-1 = "i2c4_gpio"; //pinctrl-1 定义了状态 1 (即 gpio)时需要设置的 pinctrl: &i2c4_gpio, 设置GPIO复⽤为普通IO.

    ......

  };

  &pinctrl{

    i2c4{

      i2c4_xfer: i2c4_xfer{

        rockchip,pins = <1 12 RK_FUNC_1 &pcfg_pull_none>, <1 11 RK_FUNC_1 &pcfg_pull_none>;

      };  

      i2c4_gpio: i2c4_gpio{

        rockchip,pins = <1 12 RK_FUNC_GPIO &pcfg_pull_none>, <1 11 RK_FUNC_GPIO &pcfg_pull_none>;

      }; 

    };

  };

二、gpio子系统

  gpio⼦系统是管理gpio的申请释放、控制输⼊输出等功能。

  参考DTS设备树上配置gpio

  gpio_led: gpio_led{

    status = "okay";

    compatible = "tspi, gpio_led";

    gpio-led = <&gpio1 RK_PB4  GPIO_LEVEL_HIGH>;

  };

 

三、gpio与pinctrl子系统下实现LED读写

1、在泰山派设备树的/根节点上添加zbl_led子节点(路径:Z:\sdk\linux\kernel\arch\arm64\boot\dts\rockchip)

  打开tspi-rk3566-user-v10-linux.dts设备树源文件

       

  在根目录下添加gpioled子节点

       

 

2、在SDK上编译kernel(./build.sh kernel)

  执行内核编译:./build.sh kernel

       

  内核编译成功输出:

       

  查看boot.img是否生成最新的

       

 

3、烧录boot.img镜像(前提已经烧录了buildroot的uboot,且正常运行的)

  仅勾选boot.img,进行烧录

      

4、查看设备树上节点是否添加成功(cd /proc/device-tree)

  ls 

  cd gpioled

  ls 

    

    

   查看对应的属性是否与设置的一致

  cat name

      

 

 5、编写设备树的字符驱动对应函数

/*
** gpioled.c
** 复用型引脚分为5组(GPIO0~4),每组里面都有32个复用型引脚,而且又分为4个小组(A、B、C、D),每个小组8个引脚(0~7)
** GPIO3_B4
** 在GPIO3大组,第二的B小组,第4个引脚,

group = 1; //GPIO3_B4 => 1, group ∈ {(A=0), (B=1), (C=2), (D=3)}

bank = 3; //GPIO3_B4 => 3, bank ∈ [0,4]

x = 4; //GPIO3_B4 => 4, X ∈ [0,7]

number = group * 8 + X = 1 * 8 + 4 = 12

pin = bank*32 + number= 3 * 32 + 28 = 108;

*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>


//设备名称
#define DEV_NAME "gpioled"
#define DEV_CNT 1
#define LED_ON 1
#define LED_OFF 0

char kbuf[128] = {0};


//定义新的一个结构体 struct chr_dev
struct gpioled_dev{
dev_t dev_id; //设备编号
struct cdev cdev; //内核字符设备
struct class *class; //设备类
struct device *device; //设备
int major; //主设备
int minor; //从设备
struct device_node *nd; //设备节点
int led_gpio;
};

static struct gpioled_dev gpioled;


int led_open(struct inode *inode, struct file *file)
{

//把文件的私有数据 private_data 指向设备结构体 dtsled
file->private_data = &gpioled;

printk(KERN_ALERT "[ KERN_ALERT ] gpioled open ...\n");

return 0;
}

ssize_t led_read(struct file *file, char __user *ubuf, size_t size, loff_t *offset)
{
printk(KERN_ALERT "[ KERN_ALERT ] gpioled read!\n");
return 0;
}

ssize_t led_write(struct file *file, const char __user *ubuf, size_t size, loff_t *offset)
{
unsigned char ret;
struct gpioled_dev *dev = file->private_data;

if(size > sizeof(kbuf)){
size = sizeof(kbuf);

}
if(copy_from_user(kbuf, ubuf, size)){
printk(KERN_ALERT "[ KERN_ALERT ] copy data form user fail!\n");
return -EIO;
}

ret = kbuf[0];

if(ret == LED_ON){
gpio_set_value(dev->led_gpio, LED_ON);
}
else if(ret == LED_OFF){
gpio_set_value(dev->led_gpio, LED_OFF);
}

printk(KERN_ALERT "[ KERN_ALERT ] gpioled write!\n");
return size;

}

int led_close(struct inode *inode, struct file *file)
{
printk(KERN_ALERT "[ KERN_ALERT ] gpioled close!\n");
return 0;
}

struct file_operations gpioled_fops= {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_close
};


static int __init ledcdev_init(void)
{
int ret = 0;
const char *str;


//读取设备树节点的属性数据
//获取设备节点:gpioled
gpioled.nd = of_find_node_by_path("/gpioled");
if(gpioled.nd == NULL){
printk(KERN_ALERT "[ KERN_ALERT ] gpioled node find failed.\n");
return -EINVAL;
}else{
printk(KERN_ALERT "[ KERN_ALERT ] gpioled node find ok.\n");
}

//获取 status 属性内容
ret = of_property_read_string(gpioled.nd, "status", &str);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] status read failed!\n");
return -EINVAL;

}else{
printk(KERN_ALERT "[ KERN_ALERT ] status = %s\n",str);
}

if(strcmp(str, "okay")){
return -EINVAL;
}

//获取 compatible 属性内容
ret = of_property_read_string(gpioled.nd, "compatible", &str);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] compatible read failed!\n");
return -EINVAL;

}else{
printk(KERN_ALERT "[ KERN_ALERT ] compatible = %s\n",str);
}

if(strcmp(str, "zbl,led")){
printk(KERN_ALERT "[ KERN_ALERT ] gpioled: Compatible match failed\n");
return -EINVAL;
}


//获取设备树中的 gpio 属性
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
if(gpioled.led_gpio < 0 ){
printk(KERN_ALERT "[ KERN_ALERT ] led-gpio find failed.\n");
return -EINVAL;
}else{
printk(KERN_ALERT "[ KERN_ALERT ] led-gpio num=%d\n", gpioled.led_gpio);
}


//向gpio子系统申请GPIO
ret = gpio_request(gpioled.led_gpio, "LED-GPIO");
if(ret){
printk(KERN_ALERT "[ KERN_ALERT ] gpioled failed to request led-gpio\n");
return ret;
}

ret = gpio_direction_output(gpioled.led_gpio, 0);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] can't set gpio output mode.\n");
}

//注册字符设备驱动
//创建设备号
if(gpioled.major){
gpioled.dev_id = MKDEV(gpioled.major, 0);
ret = register_chrdev_region(gpioled.dev_id, DEV_CNT, DEV_NAME);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] can't regsiter %s char driver [ret=%d]\n", DEV_CNT, DEV_NAME);
goto free_gpio;
}
}else{
ret = alloc_chrdev_region(&gpioled.dev_id, 0, DEV_CNT, DEV_NAME);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] %s can't alloc chrdev region, ret=%d.\n", DEV_NAME, ret);
goto free_gpio;
}
gpioled.major = MAJOR(gpioled.dev_id);
gpioled.minor = MINOR(gpioled.dev_id);
}

printk(KERN_ALERT "[ KERN_ALERT ] dtsled major=%d, minor=%d.\n", gpioled.major, gpioled.minor);

//关联结构体
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);

ret = cdev_add(&gpioled.cdev, gpioled.dev_id, DEV_CNT);
if(ret < 0){
goto del_unregister;
}

gpioled.class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(gpioled.class)){
goto del_cdev;
}


gpioled.device = device_create(gpioled.class, NULL, gpioled.dev_id, NULL, DEV_NAME);
if(IS_ERR(gpioled.device)){
goto destroy_class;
}


printk(KERN_ALERT "[ KERN_ALERT ] gpioled init ...\n");
return 0;

destroy_class:
class_destroy(gpioled.class);
del_cdev:
cdev_del(&gpioled.cdev);
del_unregister:
unregister_chrdev_region(gpioled.dev_id, DEV_CNT);
free_gpio:
gpio_free(gpioled.led_gpio);
return -EIO;

}


static void __exit ledcdev_exit(void)
{

printk(KERN_ALERT "[ KERN_ALERT ] gpioled exit ...\n");

cdev_del(&gpioled.cdev);

unregister_chrdev_region(gpioled.dev_id, DEV_CNT);

device_destroy(gpioled.class, gpioled.dev_id);

class_destroy(gpioled.class);

gpio_free(gpioled.led_gpio);

}

module_init(ledcdev_init);

module_exit(ledcdev_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("zbl");

 

 

6、编写应用函数

/*
** dtsledapp.c
**
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>


#define LED_ON 1
#define LED_OFF 0


int main(int argc, char *argv[])
{

int fd;
int ret;
unsigned char databuf[1] = {0};

if(argc != 2){
printf("Error Usage!\n");
return -1;
}

fd = open("/dev/gpioled", O_RDWR);
if(fd == -1)
{
printf("open %s failed.\n", "/dev/gpioled");
return -1;
}

databuf[0] = atoi(argv[1]);
ret = write(fd, databuf, sizeof(databuf));
if(ret < 0){
printf("LED control failed.\n");
close(fd);
return -1;
}

ret = close(fd);
if(ret < 0){
printf("close %s failed.\n", "/dev/gpioled");
return -1;
}

return 0;
}

7、编写makefile (修改对应文件名称)

     

PWD ?= $(shell pwd)

KERNELDIR := /home/zbl/tspi-rk3566/sdk/linux/kernel
CROSS_COMPILE ?= /home/zbl/tspi-rk3566/sdk/linux/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
CC := $(CROSS_COMPILE)gcc


obj-m += gpioled.o

module:
make -C $(KERNELDIR) M=$(PWD) ARCH=arm64 modules
@# -C 表示从当前目录切换到内核源码目录下,借助内核源码makefile进行make编译
@# M=$(PWD) 表示只编译当前目录下的驱动
@# ARCH=arm64 指定编译架构

$(CC) gpioledapp.c -o app
@# 交叉编译应用程序

.PHONE:clean

clean:
make -C $(KERNELDIR) M=$(PWD) ARCH=arm64 clean
rm app

 

8、拷贝到开发板(adb push xxx xxx),并对文件进行赋权(chmod 777 app gpioled.ko)

     

9、执行测试验证

  查看现有内核模块(lsmod), 并加载LED模块(insmod gpioled.ko)

     

   查看设备驱动(cat  /proc/devices)

      

  执行应用程序进行LED测试

      

   卸载驱动模块

      

 

posted @ 2024-07-15 22:45  zbl1118  阅读(757)  评论(1)    收藏  举报