编写一个平台驱动
mydevice_drv.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mod_devicetable.h>
#include <linux/property.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/gpio/consumer.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
static int dt_probe(struct platform_device *pdev);
static int dt_remove(struct platform_device *pdev);
static struct of_device_id my_driver_ids[] = {
{
.compatible = "mydevice, demo",
}, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, my_driver_ids);
static struct platform_driver my_driver = {
.probe = dt_probe,
.remove = dt_remove,
.driver = {
.name = "my_device_driver",
.of_match_table = my_driver_ids,
},
};
static struct gpio_desc *my_led = NULL;
static struct proc_dir_entry *proc_file;
static ssize_t my_write(struct file *File, const char *user_buffer, size_t count, loff_t *offs) {
char val = 0;
copy_from_user(&val, user_buffer, 1);
switch (val) {
case '0':
case '1':
gpiod_set_value(my_led, val - '0');
default:
break;
}
return count;
}
static struct file_operations fops = {
.write = my_write,
};
static int dt_probe(struct platform_device *pdev) {
struct device *dev = &pdev->dev;
const char *label;
int my_value, ret;
printk("dt_gpio - Now I am in the probe function!\n");
/* Check for device properties */
if(!device_property_present(dev, "label")) {
printk("dt_gpio - Error! Device property 'label' not found!\n");
return -1;
}
if(!device_property_present(dev, "my_value")) {
printk("dt_gpio - Error! Device property 'my_value' not found!\n");
return -1;
}
if(!device_property_present(dev, "green-led-gpio")) {
printk("dt_gpio - Error! Device property 'green-led-gpio' not found!\n");
return -1;
}
/* Read device properties */
ret = device_property_read_string(dev, "label", &label);
if(ret) {
printk("dt_gpio - Error! Could not read 'label'\n");
return -1;
}
printk("dt_gpio - label: %s\n", label);
ret = device_property_read_u32(dev, "my_value", &my_value);
if(ret) {
printk("dt_gpio - Error! Could not read 'my_value'\n");
return -1;
}
printk("dt_gpio - my_value: %d\n", my_value);
/* Init GPIO */
my_led = gpiod_get(dev, "green-led", GPIOD_OUT_LOW);
if(IS_ERR(my_led)) {
printk("dt_gpio - Error! Could not setup the GPIO\n");
return -1 * IS_ERR(my_led);
}
/* Creating procfs file */
proc_file = proc_create("my-led", 0666, NULL, &fops);
if(proc_file == NULL) {
printk("procfs_test - Error creating /proc/my-led\n");
gpiod_put(my_led);
return -ENOMEM;
}
return 0;
}
static int dt_remove(struct platform_device *pdev) {
printk("dt_gpio - Now I am in the remove function\n");
gpiod_put(my_led);
proc_remove(proc_file);
return 0;
}
static int __init my_init(void) {
printk("dt_gpio - Loading the driver...\n");
if(platform_driver_register(&my_driver)) {
printk("dt_gpio - Error! Could not load driver\n");
return -1;
}
return 0;
}
static void __exit my_exit(void) {
printk("dt_gpio - Unload driver");
platform_driver_unregister(&my_driver);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
Makefile
KDIR ?= /lib/modules/$(shell uname -r)/build
obj-m += mydevice.o
CC := arm-linux-gnueabi-gcc
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
dts 根中添加设备节点
mydevice {
compatible = "mydevice, demo";
status = "okay";
label = "Hello World";
my_value = <12>;
green-led-gpio = <&gpiof 8 GPIO_ACTIVE_HIGH>;
};
网友评论