Linux 驅動學習筆記 - beep(九)

Linux 驅動學習筆記 - beep(九)

本系列均爲正點原子 Linux 驅動的學習筆記, 以便加深筆者記憶。如讀者想進一步學習,可以到正點原子官網中下載資料進行學習。

添加 pinctrl 節點

在 iomuxc 節點的 imx6ul-evk 子節點下創建一個名爲 pinctrl_beep 的子節點,節點內容如下所示:

/ {
&iomuxc {
    imx6ul-evk {
            pinctrl_beep: beepgrp {
                fsl,pins = <
                    MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01 	0x10B0 /* beep */
                >;
            };
        };
    };
};

添加 BEEP 設備節點

在根節點 / 下創建 BEEP 節點,節點名爲 beep ,節點內容如下:

/ {
    beep {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "atkalpha-beep";
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_beep>;
        beep-gpio = <&gpio5 1 GPIO_ACTIVE_HIGH>;
        status = "okay";
    };
};

檢查 PIN 是否被其他外設使用

檢查 PIN 爲 SNVS_TAMPER1 這個 PIN 有沒有被其他的 pinctrl 節點使用,如果有使用的話就要屏蔽掉,然後再檢查 GPIO5_IO01 這個 GPIO 有沒有被其他外設使用,如果有的話也要屏蔽掉。

程序編寫

#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 BEEP_CNT 1       /* 設備號個數 */
#define BEEP_NAME "beep" /* 名字 */
#define BEEPOFF 0        /* 關蜂鳴器 */
#define BEEPON 1         /* 開蜂鳴器 */

/* beep設備結構體 */
struct beep_dev
{
    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 beep_dev beep; /* beep設備 */

/*
 * @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 = &beep; /* 設置私有數據 */
    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 beep_dev *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, 0); /* 打開蜂鳴器 */
    }
    else if (beepstat == BEEPOFF)
    {
        gpio_set_value(dev->beep_gpio, 1); /* 關閉蜂鳴器 */
    }
    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,
    .write = beep_write,
    .release = beep_release,
};

/*
 * @description	: 驅動出口函數
 * @param 		: 無
 * @return 		: 無
 */
static int __init beep_init(void)
{
    int ret = 0;

    /* 設置BEEP所使用的GPIO */
    /* 1、獲取設備節點:beep */
    beep.nd = of_find_node_by_path("/beep");
    if (beep.nd == NULL)
    {
        printk("beep node not find!\r\n");
        return -EINVAL;
    }
    else
    {
        printk("beep node find!\r\n");
    }

    /* 2、 獲取設備樹中的gpio屬性,得到BEEP所使用的BEEP編號 */
    beep.beep_gpio = of_get_named_gpio(beep.nd, "beep-gpio", 0);
    if (beep.beep_gpio < 0)
    {
        printk("can't get beep-gpio");
        return -EINVAL;
    }
    printk("led-gpio num = %d\r\n", beep.beep_gpio);

    /* 3、設置GPIO5_IO01爲輸出,並且輸出高電平,默認關閉BEEP */
    ret = gpio_direction_output(beep.beep_gpio, 1);
    if (ret < 0)
    {
        printk("can't set gpio!\r\n");
    }

    /* 註冊字符設備驅動 */
    /* 1、創建設備號 */
    if (beep.major)
    { /*  定義了設備號 */
        beep.devid = MKDEV(beep.major, 0);
        register_chrdev_region(beep.devid, BEEP_CNT, BEEP_NAME);
    }
    else
    {                                                             /* 沒有定義設備號 */
        alloc_chrdev_region(&beep.devid, 0, BEEP_CNT, BEEP_NAME); /* 申請設備號 */
        beep.major = MAJOR(beep.devid);                           /* 獲取分配號的主設備號 */
        beep.minor = MINOR(beep.devid);                           /* 獲取分配號的次設備號 */
    }
    printk("beep major=%d,minor=%d\r\n", beep.major, beep.minor);

    /* 2、初始化cdev */
    beep.cdev.owner = THIS_MODULE;
    cdev_init(&beep.cdev, &beep_fops);

    /* 3、添加一個cdev */
    cdev_add(&beep.cdev, beep.devid, BEEP_CNT);

    /* 4、創建類 */
    beep.class = class_create(THIS_MODULE, BEEP_NAME);
    if (IS_ERR(beep.class))
    {
        return PTR_ERR(beep.class);
    }

    /* 5、創建設備 */
    beep.device = device_create(beep.class, NULL, beep.devid, NULL, BEEP_NAME);
    if (IS_ERR(beep.device))
    {
        return PTR_ERR(beep.device);
    }

    return 0;
}

/*
 * @description	: 驅動出口函數
 * @param 		: 無
 * @return 		: 無
 */
static void __exit beep_exit(void)
{
    /* 釋放 gpio */
    gpio_free(gpioled.gpioled);

    /* 註銷字符設備驅動 */
    cdev_del(&beep.cdev);                           /*  刪除cdev */
    unregister_chrdev_region(beep.devid, BEEP_CNT); /* 註銷設備號 */

    device_destroy(beep.class, beep.devid);
    class_destroy(beep.class);
}

module_init(beep_init);
module_exit(beep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("tyustli");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章