添加pinctl节点
进入arch/arm/boot/dts目录下dts文件
在iomuxc下添加pinctl+led节点
将 GPIO1_IO03 这个 PIN 复用为 GPIO1_IO03,电气属性(配置GPIO一些列寄存器)值为 0X10B0
添加led设备节点
与上一节一样,在 / 下面添加设备节点
pinctrl-0 属性设置 LED 灯所使用的 PIN 对应的 pinctrl 节点。
led-gpio 属性指定了 LED 灯所使用的 GPIO,在这里就是 GPIO1 的 IO03,低电平
有效。
检查IO有没有被其他外设占用
一个引脚一次只能实现一个功能,否则的话驱动程序在申请 GPIO 的时候就会失败。
搜索GPIO1_IO03,发现被tsc这个外设占用了,我们没用到这个外设,直接把他注释了
除了pinctl的占用,还有设备IO的占用
除了我们使用到的,找到一个注释一个,暂时只有这两个
编译复制
设备树文件就修改好了
驱动代码
gpioled.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>
#define GPIOLED_CNT 1 //device number
#define GPIOLED_NAME "gpioled" //device name
//gpioled设备结构体
struct gpioled_dev{
dev_t devid; //设备号
struct cdev cdev; //CDEV
struct class *class; //类
struct device *device; //设备
int major; //主设备号
int minor; //次设备号
struct device_node *nd; //设备节点
int led_gpio; // //led使用的GPIO编号
};
struct gpioled_dev gpioled; //设备
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &gpioled; //设置私有数据
return 0;
}
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int ret;
unsigned char databuf[1];
unsigned char ledstat;
struct gpioled_dev *dev = filp->private_data;
ret = copy_from_user(databuf, buf, cnt);
if(ret < 0)
{
printk("kernel copy_from_user failed: %d\n", ret);
return -EFAULT;
}
ledstat = databuf[0];
if(1 == ledstat) //开
{
gpio_set_value(dev->led_gpio, 0);
}
else if(0 == databuf[0]) //关
{
gpio_set_value(dev->led_gpio, 1);
}
return 0;
}
static int led_close(struct inode *inode, struct file *file)
{
return 0;
}
static struct file_operations gpioled_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_close,
};
static int __init led_init(void)
{
int ret;
gpioled.nd = of_find_node_by_path("/gpioled"); //通过路径查找指定节点,返回节点
if(gpioled.nd == NULL)
{
printk("find_node_by_path failed\n");
return -EINVAL;
}
printk("gpioled node has been found\n");
/* 2、 获取设备树中的 gpio 属性,得到 LED 所使用的 LED 编号 */
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
if(gpioled.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
printk("led-gpio num = %d\r\n", gpioled.led_gpio);
/* 3、设置 GPIO1_IO03 为输出,并且输出高电平,默认关闭 LED 灯 */
ret = gpio_direction_output(gpioled.led_gpio, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
/* 注册字符设备驱动 */
if(gpioled.major) //如果定义了设备号用它
{
gpioled.devid = MKDEV(gpioled.major, 0);
register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
}
else //没定义设备号,自动创建
{
alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("gpioled major = %d minor = %d\n", gpioled.major, gpioled.minor); //打印主次设备号
gpioled.cdev.owner = THIS_MODULE; //初始化cdev
cdev_init(&gpioled.cdev, &gpioled_fops);
cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT); //添加cdev
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME); //创建类
if(IS_ERR(gpioled.class))
{
return PTR_ERR(gpioled.class);
}
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);//创建设备
if(IS_ERR(gpioled.device))
{
return PTR_ERR(gpioled.device);
}
return 0;
}
static void __exit led_exit(void)
{
cdev_del(&gpioled.cdev);
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
device_destroy(gpioled.class, gpioled.devid);
class_destroy(gpioled.class);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
用户层代码和上一节一样
编译运行