Platform总线设备驱动未采用设备树的方式之参考:
https://blog.csdn.net/qq_34968572/article/details/89915200
一:设备树下的platform驱动简介
platform驱动框架分为总线,设备和驱动,其中总线不需要我们这些驱动程序员去管理,这个是Linux内核提供的,我们在编写驱动的时候只需要关注设备和驱动的具体实现即可。在以前没有设备树的内核下,我们需要分别编写并注册platform_device和platform_driver。使用设备树的情况下,设备的描述被放到设备树中,因此platform_device就不需要我们去编写了,我们只需要实现platform_driver即可。
1、设备树中创建设备节点
重点是设置好compatible属性的值,因为platform总线需要通过设备节点的compatible属性值来匹配驱动。‘’
gpioled {
#address-cells = <1>;
#size-cells = <1>;
compatible = "my-led";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_led>;
led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;
status = "okay";
};
compatible属性值为“my-led”,因此在platform驱动的时候of_match_table属性表中要有“my-led”
2、编写platform驱动的时候注意兼容属性
static const struct of_device_id leds_of_match[] = {
{ .compatible = "my-led" }, /* 兼容属性 */
{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, leds_of_match);
static struct platform_driver leds_platform_driver = {
.driver = {
.name = "imx6ul-led",
.of_match_table = leds_of_match,
},
.probe = leds_probe,
.remove = leds_remove,
};
(1)of_device_id表也就是驱动兼容表,是一个数组,每个数组元素为of_device_id类型。驱动中的compatible属性和设备中的compatible属性相匹配,因此驱动中对应的probe函数就会执行。注意of_device_id列表中最后一个元素一定要为空!
(2)MODULE_DEVICE_TABLE声明leds_of_match这个设备匹配表
(3)设置platform_driver中的of_match_table匹配表为上面创建的leds_of_match
二:注册和删除platform驱动的另一种办法
include/linux/platform_device.h :
module_platform_driver(__platform_driver);
#define module_platform_driver(__platform_driver) \
module_driver(__platform_driver, platform_driver_register, \
platform_driver_unregister)
#define module_driver(__driver, __register, __unregister, ...) \
static int __init __driver##_init(void) \
{ \
return __register(&(__driver) , ##__VA_ARGS__); \
} \
module_init(__driver##_init); \
static void __exit __driver##_exit(void) \
{ \
__unregister(&(__driver) , ##__VA_ARGS__); \
} \
module_exit(__driver##_exit);
宏定义module_platform_driver的功能完成了platform驱动的注册和删除。
三:示例
#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_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#define LEDDEV_CNT 1
#define LEDDEV_NAME "platformled"
#define LEDOFF 0
#define LEDON 1
struct leddev_dev {
dev_t devid;
struct cdev cdev;
struct class *class;
struct device *device;
int major;
struct device_node *node;
int led0;
};
struct leddev_dev leddev;
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &leddev;
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retval;
unsigned char databuf[2];
unsigned char buff;
retval = copy_from_user(databuf, buf, cnt);
if(retval < 0){
printk("kernel write failed!\n");
return -EFAULT;
}
buff = databuf[0];
if(buff == LEDON)
{
gpio_set_value(leddev.led0, 0);
}
else
{
gpio_set_value(leddev.led0, 1);
}
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
static int led_probe(struct platform_device *dev)
{
printk("led driver and device was matched!\n");
if(leddev.major)
{
leddev.devid = MKDEV(leddev.major, 0);
register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);
}
else
{
alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
leddev.major = MAJOR(leddev.devid);
}
cdev_init(&leddev.cdev, &led_fops);
cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);
leddev.node = of_find_node_by_path("/gpioled");
leddev.led0 = of_get_named_gpio(leddev.node, "led-gpio", 0);
gpio_request(leddev.led0, "led0");
gpio_direction_output(leddev.led0, 1);
return 0;
}
static int led_remove(struct platform_device *dev)
{
gpio_set_value(leddev.led0, 1);
cdev_del(&leddev.cdev);
unregister_chrdev_region(leddev.devid, LEDDEV_CNT);
device_destroy(leddev.class, leddev.devid);
class_destroy(leddev.class);
return 0;
}
static const struct of_device_id led_of_match[] = {
{.compatible = "my-led"},
{ /* Sentinel */ }
};
static struct platform_driver led_driver = {
.driver = {
.name = "imx6ul-led",
.of_match_table = led_of_match,
},
.probe = led_probe,
.remove= led_remove,
};
static int platformled_init(void)
{
return platform_driver_register(&led_driver);
}
static void platformled_exit(void)
{
platform_driver_unregister(&led_driver);
}
module_init(platformled_init);
module_exit(platformled_exit);
MODULE_LICENSE("GPL");