在设备树中的GPIO信息
/{
myled {
compatible = "myled,led_drv";
label = "myled:yellow:user";
myled-gpio = <&gpio8 2 GPIO_ACTIVE_LOW>;
pinctrl-names = "default";
pinctrl-0 = <&my_led>;
};
&pinctrl {
myled {
my_led: my-led {
rockchip,pins = <8 2 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};
};
代码的改动
首先对于gpio的操作不再使用对具体硬件寄存器来操作,而是使用一些通用的gpio子系统的接口。
其次在platform_driver里,需要配置设备树的结构,并且设置compatible和设备树中的myled一致,“myled,led_drv”。
然后在platform的probe中通过获取设备树中的gpio的属性名来注册对应的led设备。
gpiod_get来获取gpio_desc结构体,使用gpiod_put来释放gpio_desc结构体
当设备文件被open时,用gpiod_direction_output函数来初始化引脚的工作方向。
当设备文件被write时,用gpiod_set_value函数来设置引脚的电平值。
源码
led_drv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/platform_device.h>
//#include "led_ops.h"
//#include "led_drv.h"
static int major = 0;
static struct class *led_class;
//struct led_operations *p_led_ops;
struct gpio_desc *led_gpio;
static int led_drv_open(struct inode *node, struct file *file)
{
// int minor = iminor(node);
printk("%s %s line %d\n", FILE, FUNCTION, LINE);
// p_led_ops->init(minor);
gpiod_direction_output(led_gpio, 0);
return 0;
}
static ssize_t led_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s line %d\n", FILE, FUNCTION, LINE);
return 0;
}
static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
char status;
// struct inode *inode = file_inode(file);
// int minor = iminor(inode);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_from_user(&status, buf, 1);
printk("write status:%x \n", status);
// p_led_ops->ctl(minor, status);
gpiod_set_value(led_gpio, status);
return 1;
}
static int led_drv_close(struct inode *node, struct file *file)
{
printk("%s %s line %d\n", FILE, FUNCTION, LINE);
return 0;
}
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
static const struct of_device_id myleds[] = {
{ .compatible = "myled,led_drv"},
{ },
};
static int chip_demoo_gpio_probe(struct platform_device *pdev)
{
led_gpio = gpiod_get(&pdev->dev, "myled", 0);
if(IS_ERR(led_gpio)) {
dev_err(&pdev->dev, "Failed to get GPIO for led\n");
return PTR_ERR(led_gpio);
}
major = register_chrdev(0, "myled", &led_drv);
led_class = class_create(THIS_MODULE, "led_class");
if(IS_ERR(led_class)) {
unregister_chrdev(major, "myled");
gpiod_put(led_gpio);
printk(KERN_WARNING "class create failed\n");
return PTR_ERR(led_class);
}
device_create(led_class, NULL, MKDEV(major, 0), NULL, "myled%d", 0);
return 0;
}
static int chip_demoo_gpio_remove(struct platform_device *pdev)
{
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
unregister_chrdev(major, "myled");
gpiod_put(led_gpio);
return 0;
}
static struct platform_driver chip_demoo_gpio_driver = {
.probe = chip_demoo_gpio_probe,
.remove = chip_demoo_gpio_remove,
.driver = {
.name = "myled",
.of_match_table = myleds,
},
};
static int __init led_drv_init(void)
{
int err;
err = platform_driver_register(&chip_demoo_gpio_driver);
printk("%s %sled_drv_init\n", __FILE__, __FUNCTION__);
return 0;
}
static void __exit led_drv_exit(void)
{
printk("%s %sled_drv_exit\n", FILE, FUNCTION);
platform_driver_unregister(&chip_demoo_gpio_driver);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chen");
原作者:习惯就好zz