一、原理圖
二、設備樹
在iomuxc節點下的imx6ul-evk子節點下添加pinctrl_beep: beepgrp節點
在根節點下添加devicetree-beep-pincrl子節點
三、驅動程序
#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 DeviceName "pinctrl-beep" /* 設備名稱 */
#define DeviceNodes_CNT 1 /* 設備號個數 */
#define BEEPOFF 0 /* 關蜂鳴器 */
#define BEEPON 1 /* 開蜂鳴器 */
/* 定義一個設備結構體 */
struct mod_struct {
dev_t devid; /* 設備號*/
struct cdev cdev; /* cdev */
struct class *class; /* 類 */
struct device *device; /* 設備 */
int major; /* 主設備號 */
int minor; /* 次設備號 */
struct device_node *nd; /* 設備節點 */
int beep_gpio; /* beep所使用的GPIO編號*/
};
struct mod_struct mod_device; /* led設備 */
/*
* @description : 打開設備
* @param - inode : 傳遞給驅動的inode
* @param - filp : 設備文件,file結構體有個叫做private_data的成員變量
* 一般在open的時候將private_data指向設備結構體。
* @return : 0 成功;其他 失敗
*/
static int beep_open(struct inode *inode, struct file *filp)
{
filp->private_data = &mod_device; /* 設置私有數據 */
return 0;
}
/*
* @description : 從設備讀取數據
* @param - filp : 要打開的設備文件(文件描述符)
* @param - buf : 返回給用戶空間的數據緩衝區
* @param - cnt : 要讀取的數據長度
* @param - offt : 相對於文件首地址的偏移
* @return : 讀取的字節數,如果爲負值,表示讀取失敗
*/
static ssize_t beep_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
/*
* @description : 向設備寫數據
* @param - filp : 設備文件,表示打開的文件描述符
* @param - buf : 要寫給設備寫入的數據
* @param - cnt : 要寫入的數據長度
* @param - offt : 相對於文件首地址的偏移
* @return : 寫入的字節數,如果爲負值,表示寫入失敗
*/
static ssize_t beep_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[1];
unsigned char beepstat;
struct mod_struct *dev = filp->private_data;
retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}
beepstat = databuf[0]; /* 獲取狀態值 */
if(beepstat == BEEPON) {
gpio_set_value(dev->beep_gpio, 1); /* 打開蜂鳴器 */
} else if(beepstat == BEEPOFF) {
gpio_set_value(dev->beep_gpio, 0); /* 關閉蜂鳴器 */
}
return 0;
}
/*
* @description : 關閉/釋放設備
* @param - filp : 要關閉的設備文件(文件描述符)
* @return : 0 成功;其他 失敗
*/
static int beep_release(struct inode *inode, struct file *filp)
{
return 0;
}
/* 設備操作函數 */
static struct file_operations beep_fops = {
.owner = THIS_MODULE,
.open = beep_open,
.read = beep_read,
.write = beep_write,
.release = beep_release,
};
/*
* @description : 驅動出口函數
* @param : 無
* @return : 無
*/
static int __init drv_init(void)
{
int ret = 0;
/*一、設置BEEP所使用的GPIO */
/* 1、獲取設備節點:/devicetree-beep-pincrl */
mod_device.nd = of_find_node_by_path("/devicetree-beep-pincrl");
if(mod_device.nd == NULL) {
printk("devicetree-beep-pincrl node not find!\r\n");
return -EINVAL;
} else {
printk("devicetree-beep-pincrl node find!\r\n");
}
/* 2、從設備節點下的led-gpio屬性裏獲取gpio編號 */
mod_device.beep_gpio = of_get_named_gpio(mod_device.nd, "beep-gpio", 0);
if(mod_device.beep_gpio < 0) {
printk("can't get beep-gpio");
return -EINVAL;
}
printk("beep-gpio num = %d\r\n", mod_device.beep_gpio);
/* 3、設置GPIO爲輸出,默認*/
ret = gpio_direction_output(mod_device.beep_gpio,0);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
/*二、 註冊字符設備驅動 */
/* 1、創建設備號 */
if (mod_device.major)
{
mod_device.devid = MKDEV(mod_device.major,0);//設備號起始點(major,0)
register_chrdev_region(mod_device.devid,DeviceNodes_CNT,DeviceName);//設備號起始點開始,申請DeviceNodes_CNT個設備號
}
else {
alloc_chrdev_region(&mod_device.devid,0,DeviceNodes_CNT,DeviceName); /* 申請設備號 */
mod_device.major = MAJOR(mod_device.devid); /* 獲取分配號的主設備號 */
mod_device.minor = MINOR(mod_device.devid); /* 獲取分配號的次設備號 */
}
printk("mod_device major=%d,minor=%d\r\n",mod_device.major,mod_device.minor);
/* 2、初始化cdev */
mod_device.cdev.owner = THIS_MODULE;
cdev_init(&mod_device.cdev,&beep_fops);
/* 3、添加一個cdev */
cdev_add(&mod_device.cdev,mod_device.devid,DeviceNodes_CNT);
/* 4、創建類 */
mod_device.class = class_create(THIS_MODULE,DeviceName);
if (IS_ERR(mod_device.class)) {
return PTR_ERR(mod_device.class);
}
/* 5、創建設備節點*/
mod_device.device = device_create(mod_device.class,NULL,mod_device.devid,NULL,"pinctrl-beep0");
if (IS_ERR(mod_device.device)) {
return PTR_ERR(mod_device.device);
}
return 0;
}
/*
* @description : 驅動出口函數
* @param : 無
* @return : 無
*/
static void __exit drv_exit(void)
{
/* 註銷字符設備驅動 */
cdev_del(&mod_device.cdev);/* 刪除cdev */
unregister_chrdev_region(mod_device.devid,DeviceNodes_CNT); /* 註銷設備號 */
device_destroy(mod_device.class,mod_device.devid);
class_destroy(mod_device.class);
}
module_init(drv_init);
module_exit(drv_exit);
MODULE_LICENSE("GPL");
四、app測試程序
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#define LEDOFF 0
#define LEDON 1
/*
* @description : main主程序
* @param - argc : argv數組元素個數
* @param - argv : 具體參數
* @return : 0 成功;其他 失敗
*/
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[1];
if(argc != 3){
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
/* 打開led驅動 */
fd = open(filename, O_RDWR);
if(fd < 0){
printf("file %s open failed!\r\n", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]); /* 要執行的操作:打開或關閉 */
/* 向/dev/led文件寫入數據 */
retvalue = write(fd, databuf, sizeof(databuf));
if(retvalue < 0){
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
retvalue = close(fd); /* 關閉文件 */
if(retvalue < 0){
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}