一、驅動程序分離的思想
【IMX6ULL驅動開發學習】05.字符設備驅動開發模板(包括讀寫函數、poll機制、異步通知、定時器、中斷、自動創建設備節點和環形緩沖區)_阿龍還在寫代碼的博客-CSDN博客
之前編寫驅動程序的代碼存在不少弊端:移植性差,驅動程序移植到別的板子上時,開發者需要修改引腳。,并且還要重新編譯驅動程序或內核。為提高移植和開發效率,驅動程序分離編程的思想尤為重要。
首先我們要知道:?內核里有個結構體platform_bus_type(虛擬的總線),總線上抽象出兩個鏈表:設備鏈表和驅動鏈表。
我們在寫驅動程序時,可以構造platfrom_device結構體,然后把它添加進內核里(platform_device_register函數),就是放入platform_bus_type結構體的設備鏈表中。
驅動程序會調用兩個函數,注冊platform_device結構體和platform_driver結構體。platform_device結構體里含有硬件資源,包括寄存器地址、內存地址、中斷號;platform_driver結構體里有通用的代碼。 以前寫驅動程序時,只寫成一個.c文件,在入口函數里注冊字符設備驅動程序;現在需要故意拆分成兩個文件gpio_drv.c和gpio_dev.c。
在gpio_drv.c的入口函數里注冊platform_driver結構體(用到platform_driver_register函數),在gpio_dev.c的入口函數里注冊platform_device結構體(用到platform_device_register函數),該函數會把要注冊的platform_device結構體放入內核中platform_bus_type結構體(虛擬總線)的設備鏈表,并且會遍歷platform_bus_type結構體(虛擬總線)的驅動鏈表,將platform_device結構體和每一個platform_driver結構體進行比較(為硬件設備找驅動程序),匹配成功后就不會往后比較了。
如果匹配成功,會調用platform_driver結構體中的probe函數。在probe函數中完成:①從platform_device結構體中得到引腳編號 ②注冊字符設備驅動程序。
如果事先添加了設備(platform_device結構體),但并沒找到與之匹配的驅動程序(platform_driver結構體)。之后添加驅動時,會遍歷設備鏈表,若匹配成功則調用驅動的probe函數。且一個驅動可能支持多個設備。
二、設備樹
gpio_dev.c和設備樹的目的都是為了構造platform_device結構體。如果用gpio_dev.c時需要每次都修改引腳,重新編譯和安裝,導致內核里有很多冗余的gpio_dev.c文件和platform_device結構體。 使用設備樹:在設備樹文件中添加節點信息,根據節點信息,內核會構造出platform_device結構體。
板子啟動時有個uboot,uboot會做兩件事:①板子上如果有SD卡,SD卡中存放有設備樹dtb文件,uboot會把設備樹文件讀入到內存中;②SD卡中還有內核,uboot會把內核讀入內存;③啟動內核,uboot會把設備樹的地址傳入內核,內核會這個地址上把設備樹文件解析成各種platform_device結構體。
?
以后產品修改了引腳,我們只需要修改設備樹dtb文件就可以了。內核不變,變設備樹文件。
2.1 使用設備樹
- 修改設備樹:
arch/arm/boot/dts/100ask_imx6ull-14x14.dts
?在設備樹文件中添加節點信息,注意compatible與驅動的compatible匹配
motor {compatible = "100ask,gpiodemo";gpios = <&gpio4 19 GPIO_ACTIVE_HIGH>, <&gpio4 20 GPIO_ACTIVE_HIGH>,<&gpio4 21 GPIO_ACTIVE_HIGH>,<&gpio4 22 GPIO_ACTIVE_HIGH>;
};
- 編譯:make dtbs
- 復制到板子上?
PC:
cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/開發板:
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
cp /mnt/100ask_imx6ull-14x14.dtb /boot
reboot
- ?測試
insmod gpio_drv.ko
./button_test /dev/gpio ...
三、平臺總線設備驅動模板
支持platfrom_device來自自己寫的.c文件和更改的設備樹文件,包括中斷、定時器、讀寫、poll機制、異步通知。驅動程序如下:
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>struct gpio_desc{int gpio;int irq;char name[128];int key;struct timer_list key_timer;
} ;static struct gpio_desc *gpios;
static int count;/* 主設備號 */
static int major = 0;
static struct class *gpio_class;/* 環形緩沖區 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;struct fasync_struct *button_fasync;#define NEXT_POS(x) ((x+1) % BUF_LEN)static int is_key_buf_empty(void)
{return (r == w);
}static int is_key_buf_full(void)
{return (r == NEXT_POS(w));
}static void put_key(int key)
{if (!is_key_buf_full()){g_keys[w] = key;w = NEXT_POS(w);}
}static int get_key(void)
{int key = 0;if (!is_key_buf_empty()){key = g_keys[r];r = NEXT_POS(r);}return key;
}static DECLARE_WAIT_QUEUE_HEAD(gpio_wait);// static void key_timer_expire(struct timer_list *t)
static void key_timer_expire(unsigned long data)
{/* data ==> gpio */// struct gpio_desc *gpio_desc = from_timer(gpio_desc, t, key_timer);struct gpio_desc *gpio_desc = (struct gpio_desc *)data;int val;int key;val = gpio_get_value(gpio_desc->gpio);//printk("key_timer_expire key %d %d\n", gpio_desc->gpio, val);key = (gpio_desc->key) | (val<<8);put_key(key);wake_up_interruptible(&gpio_wait);kill_fasync(&button_fasync, SIGIO, POLL_IN);
}/* 實現對應的open/read/write等函數,填入file_operations結構體 */
static ssize_t gpio_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);int err;int key;if (is_key_buf_empty() && (file->f_flags & O_NONBLOCK))return -EAGAIN;wait_event_interruptible(gpio_wait, !is_key_buf_empty());key = get_key();err = copy_to_user(buf, &key, 4);return 4;
}static ssize_t gpio_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{unsigned char ker_buf[2];int err;if (size != 2)return -EINVAL;err = copy_from_user(ker_buf, buf, size);if (ker_buf[0] >= sizeof(gpios)/sizeof(gpios[0]))return -EINVAL;gpio_set_value(gpios[ker_buf[0]].gpio, ker_buf[1]);return 2;
}static unsigned int gpio_drv_poll(struct file *fp, poll_table * wait)
{//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, &gpio_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}static int gpio_drv_fasync(int fd, struct file *file, int on)
{if (fasync_helper(fd, file, on, &button_fasync) >= 0)return 0;elsereturn -EIO;
}/* 定義自己的file_operations結構體 */
static struct file_operations gpio_key_drv = {.owner = THIS_MODULE,.read = gpio_drv_read,.write = gpio_drv_write,.poll = gpio_drv_poll,.fasync = gpio_drv_fasync,
};static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{struct gpio_desc *gpio_desc = dev_id;printk("gpio_key_isr key %d irq happened\n", gpio_desc->gpio);mod_timer(&gpio_desc->key_timer, jiffies + HZ/5);return IRQ_HANDLED;
}/* 在入口函數 */
static int gpio_drv_probe(struct platform_device *pdev)
{int err = 0;int i;//平臺設備里面有設備樹節點信息//如果平臺設備platform_device來自設備樹的話,np就不是NULLstruct device_node *np = pdev->dev.of_node;//資源指針 struct resource *res;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);/* 從platfrom_device獲得引腳信息 * 1. pdev來自自己寫的c文件* 2. pdev來自設備樹(在設備樹文件中添加硬件的節點信息)*/if (np){/* pdev來自設備樹 設備樹節點信息示例:reg_usb_ltemodule: regulator@1 {compatible = "100ask,gpiodemo";gpios = <&gpio5 5 GPIO_ACTIVE_HIGH>, <&gpio5 3 GPIO_ACTIVE_HIGH>;};*/count = of_gpio_count(np);//獲得這個設備信息:多少個引腳if (!count)return -EINVAL;gpios = kmalloc(count * sizeof(struct gpio_desc), GFP_KERNEL);for (i = 0; i < count; i++){gpios[i].gpio = of_get_gpio(np, i);//取出這個設備的第i個引腳的引腳編號sprintf(gpios[i].name, "%s_pin_%d", np->name, i);//給對應引腳取名字 申請gpio時需要用到名字}}else{/* pdev來自c文件 static struct resource omap16xx_gpio3_resources[] = {{.start = 115,.end = 115,.flags = IORESOURCE_IRQ,},{.start = 118,.end = 118,.flags = IORESOURCE_IRQ,}, }; */count = 0;while (1){ //獲得平臺設備里面的,這種IORESOURCE_IRQ類型的資源//@dev:platform_device @type:resource type @num:resource indexres = platform_get_resource(pdev, IORESOURCE_IRQ, count);if (res){count++;}else{break;}}if (!count)return -EINVAL;gpios = kmalloc(count * sizeof(struct gpio_desc), GFP_KERNEL);for (i = 0; i < count; i++){res = platform_get_resource(pdev, IORESOURCE_IRQ, i);gpios[i].gpio = res->start;//取出這個設備的第i個引腳的引腳編號sprintf(gpios[i].name, "%s_pin_%d", pdev->name, i);//給對應引腳取名字 申請gpio時需要用到名字}}for (i = 0; i < count; i++){ gpios[i].irq = gpio_to_irq(gpios[i].gpio);setup_timer(&gpios[i].key_timer, key_timer_expire, (unsigned long)&gpios[i]);//timer_setup(&gpios[i].key_timer, key_timer_expire, 0);gpios[i].key_timer.expires = ~0;add_timer(&gpios[i].key_timer);err = request_irq(gpios[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "100ask_gpio_key", &gpios[i]);}/* 注冊file_operations */major = register_chrdev(0, "100ask_gpio_key", &gpio_key_drv); /* /dev/gpio_desc */gpio_class = class_create(THIS_MODULE, "100ask_gpio_key_class");if (IS_ERR(gpio_class)) {printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, "100ask_gpio_key");return PTR_ERR(gpio_class);}device_create(gpio_class, NULL, MKDEV(major, 0), NULL, "100ask_gpio"); /* /dev/100ask_gpio */return err;
}/* 有入口函數就應該有出口函數:卸載驅動程序時,就會去調用這個出口函數*/
//這里應該free gpio這個數組 但是沒加上不知道為啥
static int gpio_drv_remove(struct platform_device *pdev)
{int i;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);device_destroy(gpio_class, MKDEV(major, 0));class_destroy(gpio_class);unregister_chrdev(major, "100ask_gpio_key");for (i = 0; i < count; i++){free_irq(gpios[i].irq, &gpios[i]);del_timer(&gpios[i].key_timer);}return 0;
}//支持的設備
//只要設備樹節點的信息它的compatible與下面的compatible相同,
//即platfrom_device和platfrom_driver匹配成功,成功后probe函數就被調用
static const struct of_device_id gpio_dt_ids[] = {{ .compatible = "100ask,gpiodemo", },{ /* sentinel */ }
};static struct platform_driver gpio_platform_driver = {.driver = {.name = "100ask_gpio_plat_drv",.of_match_table = gpio_dt_ids,},.probe = gpio_drv_probe,.remove = gpio_drv_remove,
};static int __init gpio_drv_init(void)
{/* 注冊platform_driver */return platform_driver_register(&gpio_platform_driver);
}static void __exit gpio_drv_exit(void)
{/* 反注冊platform_driver */platform_driver_unregister(&gpio_platform_driver);
}/* 7. 其他完善:提供設備信息,自動創建設備節點 */module_init(gpio_drv_init);
module_exit(gpio_drv_exit);MODULE_LICENSE("GPL");