基于Dragonboard 410c的智能窗帘设计(二)

       在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");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值