在https://blog.csdn.net/weixin_40109283/article/details/80167342博客中已经介绍了硬件设计部分,下面给出硬件驱动部代码部分:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sysfs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/of_gpio.h>
#include <asm/uaccess.h>
#include <linux/kdev_t.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
struct mach_data {
struct platform_device *pdev;
int mach_pos;
int mach_neg;
int mach_limit;
int irq;
};
static struct mach_data* data;
static void func(struct work_struct *work)
{
msleep(10);
gpio_set_value(data->mach_pos, 0);
gpio_set_value(data->mach_neg, 0);
}
DECLARE_WORK(mywork, func);
static irqreturn_t mach_limit_interrupt_handler(int irq, void *ptr)
{
schedule_work(&mywork);
return IRQ_HANDLED;
}
static void mach_pos(void)
{
gpio_set_value(data->mach_pos, 1);
gpio_set_value(data->mach_neg, 0);
}
static void mach_neg(void)
{
gpio_set_value(data->mach_pos, 0);
gpio_set_value(data->mach_neg, 1);
}
static void mach_close(void)
{
gpio_set_value(data->mach_pos, 0);
gpio_set_value(data->mach_neg, 0);
}
static ssize_t mach_store_value(struct device *dev, struct device_attribute* attr,
const char *buf, size_t len)
{
int ret;
unsigned long v;
ret = kstrtoul(buf, 10, &v);
switch (v) {
case 0:
mach_neg();
break;
case 1:
mach_pos();
break;
case 2:
mach_close();
break;
default:
return -EINVAL;
}
return len;
}
static DEVICE_ATTR(value, 0664, NULL, mach_store_value);
static int mach_probe(struct platform_device *pdev)
{
int result;
struct device_node* node = pdev->dev.of_node;
printk("mach probe enter\n");
data = devm_kzalloc(&pdev->dev, sizeof(struct mach_data), GFP_KERNEL);
if (!data) {
pr_err("%s kzalloc error\n", __FUNCTION__);
return -ENOMEM;
}
dev_set_drvdata(&pdev->dev, data);
data->mach_pos = of_get_named_gpio(node, "thundersoft,mach_pos", 0);
if (!gpio_is_valid(data->mach_pos)) {
pr_err("mach gpio_pos not specified\n");
goto err1;
} else {
result = gpio_request(data->mach_pos, "mach_pos");
if (result < 0) {
pr_err("Unable to request mach_pos gpio\n");
goto err1;
} else {
gpio_direction_output(data->mach_pos, 0);
}
}
data->mach_neg = of_get_named_gpio(node, "thundersoft,mach_neg", 0);
if (!gpio_is_valid(data->mach_neg)) {
pr_err("mach_neg gpio not specified\n");
goto err2;
} else {
result = gpio_request(data->mach_neg, "mach_pos");
if (result < 0) {
pr_err("Unable to request mach_neg gpio\n");
goto err2;
} else {
gpio_direction_output(data->mach_neg, 0);
}
}
data->mach_limit = of_get_named_gpio(node, "thundersoft,mach_limit", 0);
if (!gpio_is_valid(data->mach_limit)) {
pr_err("mach_limit gpio not specified\n");
goto err3;
} else {
result = gpio_request(data->mach_limit, "mach_limit");
if (result < 0) {
pr_err("Unable to request mach_limit gpio\n");
goto err3;
} else {
gpio_direction_input(data->mach_limit);
}
}
data->irq = gpio_to_irq(data->mach_limit);
result = request_irq(data->irq, mach_limit_interrupt_handler,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "mach_limit_intr", data);
if (result < 0) {
pr_err("Unable to request irq\n");
goto err4;
}
result = sysfs_create_file(&pdev->dev.kobj, &dev_attr_value.attr);
if (result < 0) {
printk("sysfs create file failed\n");
goto err4;
}
printk(KERN_INFO "mach probe success\n");
return 0;
err4:
gpio_free(data->mach_limit);
err3:
gpio_free(data->mach_neg);
err2:
gpio_free(data->mach_pos);
err1:
kfree(data);
printk(KERN_ERR "mach probe failed\n");
return -EINVAL;
}
static int mach_remove(struct platform_device *pdev)
{
gpio_free(data->mach_pos);
kfree(data);
return 0;
}
static struct of_device_id mach_match_table[] = {
{ .compatible = "thundersoft,machine"},
{ },
};
static struct platform_driver mach_driver = {
.probe = mach_probe,
.remove = mach_remove,
.driver = {
.owner = THIS_MODULE,
.name = "machine",
.of_match_table = mach_match_table,
},
};
module_platform_driver(mach_driver);
MODULE_AUTHOR("heql0703@thundersoft.com");
MODULE_LICENSE("GPL");