i.MX 6ULL 驱动开发 六:beep 驱动

一、原理分析

i.MX 6ULL 驱动开发 六:beep 驱动_第1张图片
i.MX 6ULL 驱动开发 六:beep 驱动_第2张图片
在这里插入图片描述

通过原理图可以确定 beep 连接到 SNVS_TAMPER1 引脚上。根据 beep 原理,当 SNVS_TAMPER1 输出低电平时,beep 鸣叫。

通过数据手册确定 SNVS_TAMPER1 可以复用为 GPIO5 IO1

二、pinctrl 子系统和gpio子系统概念

i.MX 6ULL 驱动开发 五:LED 驱动_lqonlylove的博客-CSDN博客

三、修改设备树

1、确定引脚

通过原理图可以确定 beep 使用 GPIO5_IO1 引脚。

2、添加 pinctrl 子系统相关配置

pinctrl_beep: beepgrp {
	fsl,pins = <
		MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01 	0x10B0 /* beep */	
	>;
};

3、添加 gpio 子系统相关配置

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

4、测试

1、编译设备树

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ make dtbs
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
make[1]: 'include/generated/mach-types.h' is up to date.
  CHK     include/generated/bounds.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  DTC     arch/arm/boot/dts/imx6ull-alientek-emmc.dtb
  DTC     arch/arm/boot/dts/imx6ull-alientek-nand.dtb
onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$

2、拷贝编译成功的设备树文件

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ ls /home/onlylove/my/tftp/ -l
total 11528
-rwxrwxr-x 1 onlylove onlylove 5901744 Sep 17 04:04 zImage
-rwxrwxr-x 1 onlylove onlylove 5901752 Aug 20 01:24 zImage.bak
onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cp arch/arm/boot/dts/imx6ull-alientek-emmc.dtb /home/onlylove/my/tftp
onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ ls /home/onlylove/my/tftp/ -l
total 11568
-rw-rw-r-- 1 onlylove onlylove   39092 Sep 17 06:50 imx6ull-alientek-emmc.dtb
-rwxrwxr-x 1 onlylove onlylove 5901744 Sep 17 04:04 zImage
-rwxrwxr-x 1 onlylove onlylove 5901752 Aug 20 01:24 zImage.bak
onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$

3、启动 linux 查看设备树解析是否成功

/sys/firmware/devicetree/base # pwd
/proc/device-tree
/sys/firmware/devicetree/base # ls
#address-cells                 interrupt-controller@00a01000
#size-cells                    key
aliases                        memory
alphaled                       model
backlight                      name
beep                           pxp_v4l2
chosen                         regulators
clocks                         reserved-memory
compatible                     sii902x-reset
cpus                           soc
gpio-keys                      sound
gpioled                        spi4
/sys/firmware/devicetree/base # cd beep/
/sys/firmware/devicetree/base/beep # ls
#address-cells  beep-gpio       name            pinctrl-names
#size-cells     compatible      pinctrl-0       status
/sys/firmware/devicetree/base/beep # ls -l
total 0
-r--r--r--    1 root     0                4 Jan  1 05:02 #address-cells
-r--r--r--    1 root     0                4 Jan  1 05:02 #size-cells
-r--r--r--    1 root     0               12 Jan  1 05:02 beep-gpio
-r--r--r--    1 root     0                8 Jan  1 05:02 compatible
-r--r--r--    1 root     0                5 Jan  1 05:02 name
-r--r--r--    1 root     0                4 Jan  1 05:02 pinctrl-0
-r--r--r--    1 root     0                8 Jan  1 05:02 pinctrl-names
-r--r--r--    1 root     0                5 Jan  1 05:02 status
/sys/firmware/devicetree/base/beep # cat compatible
/sys/firmware/devicetree/base/beep # cat name
beep/sys/firmware/devicetree/base/beep #
/sys/firmware/devicetree/base/beep #

四、驱动编写

1、makefile

KERNELDIR := /home/onlylove/my/linux/linux-imx-4.1.15
CURRENT_PATH := $(shell pwd)
obj-m := beep.o

build: kernel_modules

kernel_modules:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean

2、beep.c

#include "linux/init.h"
#include "linux/module.h"
#include "linux/kdev_t.h"
#include "linux/fs.h"
#include "linux/cdev.h"
#include "linux/device.h"
#include 
#include 
#include 
#include 

#define NEWCHRDEV_MAJOR 0   		/* 主设备号(如果为0则让系统自动分配,如果大于0则使用指定设备号) */
#define NEWCHRDEV_MINOR 0   		/* 次设备号 */
#define NEWCHRDEV_COUNT 1   		/* 设备号个数 */
#define NEWCHRDEV_NAME  "lq-beep" /* 名子 */

#define BEEPOFF 			0			/* 关灯 */
#define BEEPON 				1			/* 开灯 */

typedef struct{
    struct cdev dev;        /* cdev 结构体 */
    int major;              /* 主设备号 */
    int minor;              /* 次设备号 */
    dev_t devid;            /* 设备号 */
    struct class *class;    /* 类 */
    struct device *device;  /* 设备 */
    struct device_node	*nd; /* 设备节点 */
	int beep_gpio;			/* beep所使用的GPIO编号 */
}newchrdev_t;

newchrdev_t newchrdev;

int beep_init(void)
{
    int ret = 0;
    /***** 处理设备树 *****/
    /* 1、获取设备节点:beep */
	newchrdev.nd = of_find_node_by_path("/beep");
	if(newchrdev.nd == NULL) {
		printk("beep node not find!\r\n");
		return 1;
	} else {
		printk("beep node find!\r\n");
	}
    /* 2、 获取设备树中的gpio属性,得到 beep 所使用的 GPIO 编号 */
	newchrdev.beep_gpio = of_get_named_gpio(newchrdev.nd, "beep-gpio", 0);
	if(newchrdev.beep_gpio < 0) {
		printk("can't get beep");
		return 2;
	}
	printk("beep num = %d\r\n", newchrdev.beep_gpio);
    /***** 使用gpio子系统设置引脚 *****/
    /* 1、向 gpio 子系统申请 GPIO 管脚 */
    ret = gpio_request(newchrdev.beep_gpio,"beep");
    if(ret){
        printk("can't request gpio!\r\n");
    }
    /* 2、设置GPIO5_IO1为输出 */
	ret = gpio_direction_output(newchrdev.beep_gpio, 1);
	if(ret < 0) {
		printk("can't set gpio!\r\n");
        goto beep_init_error;
	}
    /* 3、设置beep默认状态(默认不鸣叫) */
    gpio_set_value(newchrdev.beep_gpio,1);

    return 0;
beep_init_error:
    gpio_free(newchrdev.beep_gpio);
    return -1;
}

int beep_exit(void)
{
    /* 1、设置beep退出状态(不鸣叫) */
    gpio_set_value(newchrdev.beep_gpio,1);
    /* 2、释放从gpio子系统申请的GPIO管脚 */
    gpio_free(newchrdev.beep_gpio);
    return 0;
}

/*
 * @description		: 打开设备
 * @param - inode 	: 传递给驱动的inode
 * @param - filp 	: 设备文件,file结构体有个叫做private_data的成员变量
 * 					  一般在open的时候将private_data指向设备结构体。
 * @return 			: 0 成功;其他 失败
 */
static int beep_open(struct inode *inode, struct file *filp)
{
    printk("beep_open!\r\n");
    filp->private_data = &newchrdev; /* 设置私有数据 */
    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)
{
    printk("beep_read!\r\n");
	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)
{
    long retvalue = 0;
	unsigned char databuf[1];
	unsigned char ledstat;
    newchrdev_t *dev = filp->private_data;

    printk("beep_write!\r\n");
    retvalue = copy_from_user(databuf, buf, cnt);
	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}
    printk("databuf = %d\r\n",databuf[0]);
	ledstat = databuf[0];		/* 获取状态值 */
    if(ledstat == BEEPON) {	
		gpio_set_value(dev->beep_gpio, 0);	/* 打开LED灯 */
	} else if(ledstat == BEEPOFF) {
		gpio_set_value(dev->beep_gpio, 1);	/* 关闭LED灯 */
	}

    return 0;
}

/*
 * @description		: 关闭/释放设备
 * @param - filp 	: 要关闭的设备文件(文件描述符)
 * @return 			: 0 成功;其他 失败
 */
static int beep_release(struct inode *inode, struct file *filp)
{
    printk("beep_release!\r\n");
	return 0;
}

static const struct file_operations beepops = {
    .owner   = THIS_MODULE,
    .open = beep_open,
	.read = beep_read,
	.write = beep_write,
	.release = beep_release,
};

/* 驱动入口函数 */
static int __init newchrdev_init(void)
{
    /* 驱动入口函数具体内容 */
    /* 1、字符设备号分配 */
    int ret;
    newchrdev.major = NEWCHRDEV_MAJOR;
    if(newchrdev.major){
        newchrdev.minor = NEWCHRDEV_MINOR;
        newchrdev.devid = MKDEV(newchrdev.major, newchrdev.minor);
        ret = register_chrdev_region(newchrdev.devid,NEWCHRDEV_COUNT,NEWCHRDEV_NAME);
    }else{
        ret = alloc_chrdev_region(&newchrdev.devid,0,NEWCHRDEV_COUNT,NEWCHRDEV_NAME);
        newchrdev.major = MAJOR(newchrdev.devid);
        newchrdev.minor = MINOR(newchrdev.devid);
    }
    if(ret < 0){
        printk("newchrdev xxx_chrdev_region failed!\r\n");
        goto newchrdev_chrdev_region_failed;
    }
    printk("newchrdev major=%d,minor=%d\r\n",newchrdev.major,newchrdev.minor);

    /* 2、注册字符设备 */
    newchrdev.dev.owner = THIS_MODULE;
    cdev_init(&newchrdev.dev,&beepops);
    ret = cdev_add(&newchrdev.dev,newchrdev.devid,NEWCHRDEV_COUNT);
    if(ret < 0){
        printk("newchrdev cdev_add failed!\r\n");
        goto newchrdev_cdev_add_failed;
    }

    /* 3、创建类 */
    newchrdev.class = class_create(THIS_MODULE,NEWCHRDEV_NAME);
    if(IS_ERR(newchrdev.class)) {
        printk("newchrdev class_create failed!\r\n");
        goto newchrdev_class_create_failed;
    }

    /* 4、创建设备 */
    newchrdev.device = device_create(newchrdev.class,NULL,newchrdev.devid,NULL,NEWCHRDEV_NAME);
    if(IS_ERR(newchrdev.device)){
        printk("newchrdev device_create failed!\r\n");
        goto neschrdev_device_creat_failed;
    }

    /* beep 初始化 */
    beep_init();

    return 0;
neschrdev_device_creat_failed:
    class_destroy(newchrdev.class);
newchrdev_class_create_failed:
    cdev_del(&newchrdev.dev);
newchrdev_cdev_add_failed:
    unregister_chrdev_region(newchrdev.devid,NEWCHRDEV_COUNT);

newchrdev_chrdev_region_failed:   /* 字符设备号分配失败处理函数(未分配资源,因此不做处理) */
    return ret;
}

/* 驱动卸载函数 */
static void __exit newchrdev_exit(void)
{
    /* beep 操作 */
    beep_exit();
    /* 驱动卸载函数具体内容 */
    /* 4、删除设备 */
    device_destroy(newchrdev.class,newchrdev.devid);
    /* 3、删除类 */
    class_destroy(newchrdev.class);
    /* 2、注销字符设备 */
    cdev_del(&newchrdev.dev);
    /* 1、释放设备号 */
    unregister_chrdev_region(newchrdev.devid,NEWCHRDEV_COUNT);
}

module_init(newchrdev_init);
module_exit(newchrdev_exit);

MODULE_LICENSE("GPL");

五、应用程序编写

1、makefile

beep_app:
	arm-linux-gnueabihf-gcc beep_app.c -o beep_app

clean:
	rm -rf beep_app

2、beep_app.c

#include 
#include 
#include 
#include "stdio.h"
#include "stdlib.h"

int main(int argc, char *argv[])
{
    int fd = 0, retvalue = 0;
    char writebuf[1] = {0};
    unsigned int connect = 0;
    if(argc != 3){
		printf("Error Usage!\r\n");
		return -1;
	}
    writebuf[0] = atoi(argv[2]);
    fd = open(argv[1],O_RDWR);
    if(fd < 0){
        printf("Can't open file %s\r\n", argv[1]);
        return -1;
    }
    retvalue = write(fd, writebuf, sizeof(writebuf));
    close(fd);
    return 0;
}

六、测试

/ # ls
beep.ko        home           minicom.log    root           var
beep_app       include        mnt            sbin           video
bin            led.ko         music          share
dev            led_app        newchrdev.ko   sys
drivers        lib            newchrdev_app  tmp
etc            linuxrc        proc           usr
/ # insmod beep.ko
newchrdev major=248,minor=0
beep node find!
beep num = 129
/ # ls -l /dev/lq-beep
crw-rw----    1 root     0         248,   0 Jan  1 05:29 /dev/lq-beep
/ # rmmod beep.ko
/ # ls -l /dev/lq-beep
ls: /dev/lq-beep: No such file or directory
/ # insmod beep.ko
newchrdev major=248,minor=0
beep node find!
beep num = 129
/ # ./beep_app /dev/lq-beep 0
beep_open!
beep_write!
databuf = 0
beep_release!
/ # ./beep_app /dev/lq-beep 1
beep_open!
beep_write!
databuf = 1
beep_release!
/ # ./beep_app /dev/lq-beep 0
beep_open!
beep_write!
databuf = 0
beep_release!
/ # ./beep_app /dev/lq-beep 1
beep_open!
beep_write!
databuf = 1
beep_release!
/ # rmmod beep.ko
/ # ls -l /dev/lq-beep
ls: /dev/lq-beep: No such file or directory
/ #

你可能感兴趣的:(i.MX,6ULL,驱动开发,驱动开发,linux,imx6ull)