DeviceDriver(9):プラットフォームバス(デバイスツリー)

プラットフォームバスデバイスドライバーは、デバイスツリー参照を使用しません。

https://blog.csdn.net/qq_34968572/article/details/89915200

1:デバイスツリーの下のプラットフォームドライバーの概要

        プラットフォームドライバーフレームワークは、バス、デバイス、ドライバーに分かれています。バスは、ドライバープログラマーが管理する必要はありません。これは、Linuxカーネルによって提供されます。ドライバーを作成するときは、の特定の実装に注意を払うだけで済みます。デバイスとドライバー。以前はデバイスツリーのないカーネルでは、platform_deviceとplatform_driverを別々に記述して登録する必要があります。デバイスツリーを使用する場合、デバイスの説明はデバイスツリーに配置されるため、platform_deviceを記述する必要はなく、platform_driverを実装するだけで済みます。

1.デバイスツリーにデバイスノードを作成します

プラットフォームバスはデバイスノードの互換性のある属性値を介してドライバーと一致する必要があるため、重要なのは互換性のある属性の値を設定することです。''

	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";
	};

互換性のある属性値は「my-led」であるため、プラットフォームを駆動する場合、of_match_tableの属性テーブルには「my-led」が必要です。

2.プラットフォームドライバーを作成するときは、互換性属性に注意してください

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タイプです。ドライバの互換性のある属性はデバイスの互換性のある属性と一致するため、ドライバの対応するプローブ機能が実行されます。of_device_idリストの最後の要素は空でなければならないことに注意してください!

(2)MODULE_DEVICE_TABLEは、デバイスマッチングテーブルleds_of_matchを宣言します

(3)platform_driverのof_match_tableマッチングテーブルを上記で作成したleds_of_matchに設定します

2:プラットフォームドライバーを登録および削除する別の方法

 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の関数は、プラットフォームドライバーの登録と削除を完了します。

3:例

#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");

 

おすすめ

転載: blog.csdn.net/qq_34968572/article/details/104360300