基於Dragonboard410c的智能音箱(三)

OK,前面硬件已經準備並組裝好了,接下來需要把軟件實現。

首先,dragonboard410c的android版本默認的音頻輸出是HDMI輸出,因此我們需要先把該默認輸出改爲我們的speaker輸出。

直接修改arch/arm/boot/dts/qcom/apq8016-sbc.dtsi文件即可

這裏寫圖片描述

然後,需要實現按鍵的驅動。我們利用的是Dragonboard410c預留的ALPS_INT腳了作爲按鍵的信號輸出腳,因此可以直接在apq8016-sbc.dtsi文件中進行相關的配置。

這裏寫圖片描述

附上按鍵的驅動代碼

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/sysfs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/jiffies.h>
#include <linux/irq.h>
#include <linux/of_gpio.h>

struct button_data {
    int gpio;
    int irq;
    struct input_dev *input;
    struct delayed_work report_work;
};

static irqreturn_t button_interrupt_handler(int irq, void *ptr) {
    struct button_data* data = (struct button_data*)ptr;

    if (!data) {
        printk("%s: data is NULL!\n", __func__);
        return IRQ_HANDLED;
    }

    schedule_delayed_work(&data->report_work, 0);

    return IRQ_HANDLED;
}

static void report_work_func(struct work_struct *work) {
    struct button_data *obj = (struct button_data *)container_of(work, struct button_data, report_work.work);

    printk("%s\n", __func__);
    if (gpio_get_value(obj->gpio)) {
        input_report_key(obj->input, KEY_ENTER, 1);
        input_sync(obj->input);
        input_report_key(obj->input, KEY_ENTER, 0);
        input_sync(obj->input);
    }
}

static int parse_dt(struct platform_device* pdev, struct button_data* data) {
    int rc;
    struct device_node* node = pdev->dev.of_node;

    data->gpio = of_get_named_gpio(node,"thunder,gpio", 0);
    printk("%s: gpio = %d\n", __func__, data->gpio);
    if (gpio_is_valid(data->gpio)) {
        rc = gpio_request(data->gpio, "button_gpio");
        if (rc < 0) {
            printk("%s: unable to request gpio\n", __func__);
            return -EINVAL;
        }
        rc = gpio_direction_input(data->gpio);
    }

    if (data->gpio < 0) {
        printk("%s: error gpio\n", __func__);
        return -EINVAL;
    }

    return 0;
}

static ssize_t button_show_value(struct device *dev, struct device_attribute* attr, char* buf) {
    struct button_data *data = dev_get_drvdata(dev);

    printk("%s: gpio value is %d\n", __func__, gpio_get_value(data->gpio));
    return 0;
}

static DEVICE_ATTR(value, 0644, button_show_value, NULL);

static int button_probe(struct platform_device *pdev) {
    struct button_data* data;
    int result;

    printk("%s\n", __func__);
    data = kmalloc(sizeof(struct button_data), GFP_KERNEL);
    if (!data) {
        printk("%s kmalloc error\n", __func__);
        return -ENOMEM;  
    }
    dev_set_drvdata(&pdev->dev, data);

    data->input = input_allocate_device();
    if (!data->input) {
        printk("%s: input_allocate_device failed!\n", __func__);
        goto err_input_allocate;
    }
    data->input->name = "button";

    set_bit(EV_SYN, data->input->evbit);
    set_bit(EV_KEY, data->input->evbit);
    set_bit(KEY_ENTER, data->input->keybit);

    result = input_register_device(data->input);
    if (result < 0) {
        printk("%s: input_register_device() failed!\n", __func__);
        goto err_input_register;
    }

    result = parse_dt(pdev, data);
    if (result < 0) {
        printk("%s:error when parse dt\n", __func__);
        result = -EINVAL;
        goto err_parse_dt;
    }

    data->irq = gpio_to_irq(data->gpio);

    result = request_irq(data->irq, button_interrupt_handler, IRQF_TRIGGER_RISING, "button_int", data);
    if (result < 0) {
        printk("%s: Unable to request irq\n", __func__);
        goto err_parse_dt;
    }
    printk("%s: gpio=%d, irq=%d\n", __func__, data->gpio, data->irq);

    INIT_DELAYED_WORK(&data->report_work, report_work_func);

    result = sysfs_create_file(&pdev->dev.kobj, &dev_attr_value.attr);
    printk("%s: button probe success\n", __func__);
    return 0;

err_parse_dt:
err_input_register:
    input_free_device(data->input);
err_input_allocate:
    kfree(data);
    printk("%s: button probe failed\n", __func__);
    return result;
}

static int button_remove(struct platform_device *pdev){
    return 0;
}

static struct of_device_id button_match_table[] = {
    { .compatible = "thundersoft,button",},
    { },
};

static struct platform_driver button_driver = {
    .probe  = button_probe,
    .remove = button_remove,
    .driver = {
        .owner = THIS_MODULE,
        .name = "button_module",
        .of_match_table = button_match_table,
    },
};

module_platform_driver(button_driver);
MODULE_AUTHOR("Chen Guangxiang([email protected])");
MODULE_LICENSE("GPL v2");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章