硬件
king3288
两个led:RED、BLUE
使用LED1进行解析:32*4 + 8*3 + 3=155 0:低电平有效
设备树
rpdzkj_config.dtsi
led_gpio{
compatible = "led_gpio";//匹配
status = "okay";//使能
led_gpio =<&gpio4 GPIO_D3 0>;//管脚 有效电平
};
驱动
kernel/drivers/rongpin/led-gpio.c
参考我的另一篇博文定时器:https://blog.csdn.net/qq_34738528/article/details/105239915
/*
* GPIO driver for RICOH583 power management chip.
*
* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
* Author: Laxman dewangan <ldewangan@nvidia.com>
*
* Based on code
* Copyright (C) 2011 RICOH COMPANY,LTD
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include<linux/device.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include "rpdzkj-sysfs.h"
#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#endif
#include <linux/timer.h>
#include <asm/uaccess.h>
struct led_data *led;
static struct timer_list mytimer;//定义一个核心结构 timer_list 变量
static int led_recovery_flag = 0;
static ssize_t recovery_write(struct device* dev, struct device_attribute* attr, const char* buf, size_t count){
led_recovery_flag = buf[0] - 0x30;
printk("led_flash_status = %d\n",led_recovery_flag);
if(led_recovery_flag == 2){
gpio_direction_output(led->led_gpio.gpio,1);
led->led_gpio.enable = 1;
}else if(led_recovery_flag == 1){
gpio_direction_output(led->led_gpio.gpio,0);
led->led_gpio.enable = 0;
}
return count;
}
static ssize_t recovery_read(struct device* dev, struct device_attribute* attr, char* buf){
printk("led_flash_status = %d\n",led_recovery_flag);
return 0;
}
//在sys目录自动创建文件
/*
DEVICE_ATTR(_name, _mode, _show, _store)
_name:名称,也就是将在sys fs中生成的文件名称。
_mode:上述文件的访问权限,与普通文件相同,UGO的格式。
_show:显示函数,cat该文件时,此函数被调用。
_store:写函数,echo内容到该文件时,此函数被调用。
*/
static DEVICE_ATTR(recovery, 0666, recovery_read, recovery_write);
//定时器超时函数
void function(unsigned long data){
if(led_recovery_flag == 2){
gpio_direction_output(led->led_gpio.gpio,1);//设置为高,熄灭
led->led_gpio.enable = 1;
//修改定时器定时时间值, 并且重新注册
mod_timer(&mytimer, jiffies + msecs_to_jiffies(2000));//2s
return;
}else if(led_recovery_flag == 1){
gpio_direction_output(led->led_gpio.gpio,0);//设置为低,电亮
led->led_gpio.enable = 0;
mod_timer(&mytimer, jiffies + msecs_to_jiffies(2000));
return;
}
led->led_gpio.enable ^= 1;
gpio_direction_output(led->led_gpio.gpio,led->led_gpio.enable);
mod_timer(&mytimer, jiffies + msecs_to_jiffies(2000));
return;
}
static int led_gpio_probe(struct platform_device *pdev)
{
struct class *led_class;
int ret;
#ifdef CONFIG_OF
enum of_gpio_flags flags;
struct device_node *node = pdev->dev.of_node;//设备节点
#endif
if(!led){
led = kzalloc(sizeof(struct led_data), GFP_KERNEL);
if (!led)
return -ENOMEM;
memset(led, 0, sizeof(struct led_data));
}
if(!node){
if(!led)
kfree(led);
return 0;
}
#ifdef CONFIG_OF
//获得led的gpio和gpio配置信息
//设备树中led_gpio =<&gpio4 GPIO_D3 0>; 32*4 + 8*3 + 3 0:低电平有效
led->led_gpio.gpio = of_get_named_gpio_flags(node, "led_gpio", 0, &flags);//155
if (gpio_is_valid(led->led_gpio.gpio)){
led->led_gpio.enable = (flags == 1)? 1:0;//0低电平有效
}else{
printk("led_gpio invalid gpio: %d\n",led->led_gpio.gpio);
gpio_free(led->led_gpio.gpio);
}
#endif
//申请gpio管脚,并取一个名字
gpio_request(led->led_gpio.gpio, "led_gpio");
//向gpio写入0,并设置将端口为输出模式
gpio_direction_output(led->led_gpio.gpio,led->led_gpio.enable);
init_timer(&mytimer);//初始化一个定时器结构体
//设置结构体成员
mytimer.expires = jiffies + jiffies_to_msecs(2);//毫秒转换成时钟节拍 2ms
mytimer.function = function;//超时后执行的函数
mytimer.data = 0;
add_timer(&mytimer);//注册定时器到内核
led_class = class_create(THIS_MODULE, "recovery_led");//创建/sys/class/recovery_led目录
//创建/sys/class/recovery_led/recovery 文件
ret = class_create_file(led_class, (struct class_attribute *)&dev_attr_recovery);
//if (ret)
//{
// printk("Fail to class recovery/recovery_led.\n");
//}
return 0;
}
static int led_gpio_remove(struct platform_device *pdev)
{
del_timer(&mytimer);//从内核注销定时器
gpio_free(led->led_gpio.gpio);
if(!led){
kfree(led);
}
return 0;
}
static int led_gpio_suspend(struct platform_device *pdev, pm_message_t state)
{
del_timer(&mytimer);
if(led->led_gpio.enable == 1){
gpio_direction_output(led->led_gpio.gpio,1);
}
return 0;
}
static int led_gpio_resume(struct platform_device *pdev)
{
gpio_direction_output(led->led_gpio.gpio,0);
led->led_gpio.enable = 1;
add_timer(&mytimer);
return 0;
}
#ifdef CONFIG_OF
static struct of_device_id gpio_dt_ids[] = {
{ .compatible = "led_gpio" },
{}
};
MODULE_DEVICE_TABLE(of, gpio_dt_ids);
#endif
static struct platform_driver led_gpio_driver = {
.driver = {
.name = "led_gpio",
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = of_match_ptr(gpio_dt_ids),//设备树匹配
#endif
},
.suspend = led_gpio_suspend,
.resume = led_gpio_resume,
.probe = led_gpio_probe,//匹配成功调用probe
.remove = led_gpio_remove,
};
static int __init led_gpio_init(void)
{
return platform_driver_register(&led_gpio_driver);//注册平台驱动
}
subsys_initcall(led_gpio_init);
static void __exit led_gpio_exit(void)
{
platform_driver_unregister(&led_gpio_driver);
}
module_exit(led_gpio_exit);
MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
MODULE_DESCRIPTION("GPIO interface for RC5T583");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:rc5t583-gpio");
Linux-DEVICE_ATTR()介绍及使用示例
1.介绍
使用DEVICE_ATTR,可以实现驱动在sys目录自动创建文件,我们只需要实现show和store函数即可.
然后在应用层就能通过cat和echo命令来对sys创建出来的文件进行读写驱动设备,实现交互.
先看看DEVICE_ATTR的原型:
DEVICE_ATTR(_name, _mode, _show, _store)
_name:名称,也就是将在sys fs中生成的文件名称。
_mode:上述文件的访问权限,与普通文件相同,UGO的格式。
_show:显示函数,cat该文件时,此函数被调用。
_store:写函数,echo内容到该文件时,此函数被调用。
补充:
对设备的使用 DEVICE_ATTR
对总线使用 BUS_ATTR
对驱动使用 DRIVER_ATTR
对类使用 CLASS_ATTR
这四个高级的宏来自于<include/linux/device.h>
DEVICE_ATTR 宏声明有四个参数,分别是名称、权限位、读函数、写函数。其中读函数和写函数是读写功能函数的函数名
DEVICE_ATTR
SENSOR_DEVICE_ATTR (是DEVICE_ATTR的进一步分装)include/linux/hwmon-sysfs.h
2.DEVICE_ATTR()宏定义
DEVICE_ATTR()定义位于include/linux/device.h中,定义如下所示:
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
其中_mode定义如下:
- 400 拥有者能够读,其他任何人不能进行任何操作;
- 644 拥有者都能够读,但只有拥有者可以编辑;
- 660 拥有者和组用户都可读和写,其他人不能进行任何操作;
- 664 所有人都可读,但只有拥有者和组用户可编辑;
- 700 拥有者能够读、写和执行,其他用户不能任何操作;
- 744 所有人都能读,但只有拥有者才能编辑和执行;
- 755 所有人都能读和执行,但只有拥有者才能编辑;
- 777 所有人都能读、写和执行(该设置通常不是好想法)。
当然也可以用S_IWUSR(用户可写),S_IRUSR(用户可读)等宏代替.
以下面DEVICE_ATTR()定义为例:
static ssize_t show_my_device(struct device *dev,
struct device_attribute *attr, char *buf) //cat命令时,将会调用该函数
{
return buf;
}
static ssize_t set_my_device(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len) //echo命令时,将会调用该函数.
{
return len;
}
static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, show_my_device, set_my_device); //定义一个名字为my_device_test的设备属性文件
最终将宏展开为:
struct device_attribute dev_attr_my_device_test ={
.attr = {.name = "my_device_test", .mode = S_IWUSR|S_IRUSR },
.show = show_my_device,
.store = set_my_device,
}
然后再通过device_create_file()或者sysfs_create_file()便来创建上面my_device_test设备文件.
3.使用示例
#include <board.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/regulator/consumer.h>
#include <sound/soc.h>
#include <sound/jack.h>
static char mybuf[100]="123";
static ssize_t show_my_device(struct device *dev,
struct device_attribute *attr, char *buf) //cat命令时,将会调用该函数
{
return sprintf(buf, "%s\n", mybuf);
}
static ssize_t set_my_device(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len) //echo命令时,将会调用该函数
{
sprintf(mybuf, "%s", buf);
return len;
}
static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, show_my_device, set_my_device);
//定义一个名字为my_device_test的设备属性文件
struct file_operations mytest_ops={
.owner = THIS_MODULE,
};
static int major;
static struct class *cls;
static int mytest_init(void)
{
struct device *mydev;
major=register_chrdev(0,"mytest", &mytest_ops);
cls=class_create(THIS_MODULE, "mytest_class");
mydev = device_create(cls, 0, MKDEV(major,0),NULL,"mytest_device"); //创建mytest_device设备
if(sysfs_create_file(&(mydev->kobj), &dev_attr_my_device_test.attr)){ //在mytest_device设备目录下创建一个my_device_test属性文件
return -1;}
return 0;
}
static void mytest_exit(void)
{
device_destroy(cls, MKDEV(major,0));
class_destroy(cls);
unregister_chrdev(major, "mytest");
}
module_init(mytest_init);
module_exit(mytest_exit);
MODULE_LICENSE("GPL");
效果如下:
以后就可以无需写测试程序来读写驱动了,是不是很方便