如何从内核模块提供中断生成GPIO

发布于 2025-02-04 02:30:34 字数 1656 浏览 3 评论 0原文

我已经编写了一个(工作)内核模块,该模块使用返回struct gpio_chip实例来指定应处理读取和写入的方式(使用模块提供的GPIO)。

static struct gpio_chip template_chip = {
        .label                  = "my_module",
        .owner                  = THIS_MODULE,
        .get_direction          = my_module_gpio_get_direction,
        .direction_input        = my_module_gpio_direction_in,
        .get                    = my_module_gpio_get,
        .direction_output       = my_module_gpio_direction_out,
        .set                    = my_module_gpio_set,
        .to_irq                 = my_module_gpio_to_irq,
        .can_sleep              = true,
};

static int my_module_gpio_probe(struct platform_device *pdev)
{
        struct my_module *my_module = dev_get_drvdata(pdev->dev.parent);
        struct my_module_platform_data *pdata = dev_get_platdata(my_module->dev);
        struct my_module_gpio_data *my_module_gpio;
        int ret;

        printk(KERN_INFO "my_module_gpio_probe\n");

        my_module_gpio = devm_kzalloc(&pdev->dev, sizeof(*my_module_gpio), GFP_KERNEL);
        if (my_module_gpio == NULL)
                return -ENOMEM;

        my_module_gpio->my_module = my_module;
        my_module_gpio->gpio_chip = template_chip;
        my_module_gpio->gpio_chip.ngpio = NUM_GPIOS;
        my_module_gpio->gpio_chip.parent = &pdev->dev;
        ...

我现在希望我的(软件生成的)gpios能够产生当我的模块更改GPIO值时,中断。

我想支持旧的/sys/class/gpio接口和新的Chardev接口。

在谷歌搜索时,我发现了许多消费中断内核模块中的示例。

我需要为制作中断我的模块的GPIO吗?

I've written a (working) kernel module which uses returns a struct gpio_chip instance to specify how reads and writes (using the module-provided GPIOs) should be handled.

static struct gpio_chip template_chip = {
        .label                  = "my_module",
        .owner                  = THIS_MODULE,
        .get_direction          = my_module_gpio_get_direction,
        .direction_input        = my_module_gpio_direction_in,
        .get                    = my_module_gpio_get,
        .direction_output       = my_module_gpio_direction_out,
        .set                    = my_module_gpio_set,
        .to_irq                 = my_module_gpio_to_irq,
        .can_sleep              = true,
};

static int my_module_gpio_probe(struct platform_device *pdev)
{
        struct my_module *my_module = dev_get_drvdata(pdev->dev.parent);
        struct my_module_platform_data *pdata = dev_get_platdata(my_module->dev);
        struct my_module_gpio_data *my_module_gpio;
        int ret;

        printk(KERN_INFO "my_module_gpio_probe\n");

        my_module_gpio = devm_kzalloc(&pdev->dev, sizeof(*my_module_gpio), GFP_KERNEL);
        if (my_module_gpio == NULL)
                return -ENOMEM;

        my_module_gpio->my_module = my_module;
        my_module_gpio->gpio_chip = template_chip;
        my_module_gpio->gpio_chip.ngpio = NUM_GPIOS;
        my_module_gpio->gpio_chip.parent = &pdev->dev;
        ...

I now want my (software generated) GPIOs to be able to produce an interrupt when my module changes the GPIO values.

I want to support both the old /sys/class/gpio interface and the new chardev interface.

While googling, I've found lots of examples of how to consume interrupts in a kernel module.

What do I need to do to produce interrupts for my module's GPIOs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2025-02-11 02:30:34

为了测试内核内的GPIO库,已经创建了gpio-sim模块,该模块模拟了常规GPIO芯片可以执行的操作,包括中断生成。

负责此的代码位于 <代码> gpio_sim_apply_pull() 。不要查看函数名称,因为它只是应用人造线的完整状态。

请注意,在较旧的内核中,出于类似目的,存在gpio-mockup驱动程序。

In order to test GPIO library inside the kernel the gpio-sim module has been created which emulates what regular GPIO chip can do, including interrupt generation.

The code responsible for that is located in the gpio_sim_apply_pull(). Don't look at the function name, because it just applies the full state of the artificial line.

Note, that in the older kernels the gpio-mockup driver is present for the similar purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文