一、硬件电路分析
先来看pmuc rk817 power按键的电路原理图,power 按键是连接到pmic,pmic 检测到按键事件后在通过PMIC_INT_L (GPIO0-A3)发信号给cpu。
二、软件上的实现
1、从上面的原理图知道PMIC_INT_L 是连接到GPIO0-A3,cpu需要处理这个gpio的中断事件,首先得配置dts。interrupt-parent = <&gpio0>; interrupts = <3 IRQ_TYPE_LEVEL_LOW>;定义中断pin。
2、检测到是这个pmic的id就调用对应的的cells,这个方法之前的都没有用过,用这种方法可以调用不同的驱动。
3、注意这个name是"rk805-pwrkey",会通过这个name去打开一个平台设备文件处理按键事件。
4、这个地方定义检测中断pin的上升和下降沿。
5、处理按键事件的驱动文件\kernel\drivers\input\misc\rk805-pwrkey.c 注意name = "rk805-pwrkey"和上面的是对应的。
/** Rockchip RK805 PMIC Power Key driver** Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd** Author: Joseph Chen <chenjh@rock-chips.com>** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation; either version 2 of the License, or (at your* option) any later version.*/#include <linux/errno.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>static irqreturn_t pwrkey_fall_irq(int irq, void *_pwr)
{struct input_dev *pwr = _pwr;input_report_key(pwr, KEY_POWER, 1);input_sync(pwr);printk("pwrkey_fall_irq \n");return IRQ_HANDLED;
}static irqreturn_t pwrkey_rise_irq(int irq, void *_pwr)
{struct input_dev *pwr = _pwr;printk("pwrkey_rise_irq \n");input_report_key(pwr, KEY_POWER, 0);input_sync(pwr);return IRQ_HANDLED;
}static int rk805_pwrkey_probe(struct platform_device *pdev)
{struct input_dev *pwr;int fall_irq, rise_irq;int err;pwr = devm_input_allocate_device(&pdev->dev);if (!pwr) {dev_err(&pdev->dev, "Can't allocate power button\n");return -ENOMEM;}pwr->name = "rk805 pwrkey";pwr->phys = "rk805_pwrkey/input0";pwr->id.bustype = BUS_HOST;input_set_capability(pwr, EV_KEY, KEY_POWER);fall_irq = platform_get_irq(pdev, 0);if (fall_irq < 0) {dev_err(&pdev->dev, "Can't get fall irq: %d\n", fall_irq);return fall_irq;}rise_irq = platform_get_irq(pdev, 1);if (rise_irq < 0) {dev_err(&pdev->dev, "Can't get rise irq: %d\n", rise_irq);return rise_irq;}err = devm_request_any_context_irq(&pwr->dev, fall_irq,pwrkey_fall_irq,IRQF_TRIGGER_FALLING | IRQF_ONESHOT,"rk805_pwrkey_fall", pwr);if (err < 0) {dev_err(&pdev->dev, "Can't register fall irq: %d\n", err);return err;}err = devm_request_any_context_irq(&pwr->dev, rise_irq,pwrkey_rise_irq,IRQF_TRIGGER_RISING | IRQF_ONESHOT,"rk805_pwrkey_rise", pwr);if (err < 0) {dev_err(&pdev->dev, "Can't register rise irq: %d\n", err);return err;}err = input_register_device(pwr);if (err) {dev_err(&pdev->dev, "Can't register power button: %d\n", err);return err;}platform_set_drvdata(pdev, pwr);device_init_wakeup(&pdev->dev, true);return 0;
}static struct platform_driver rk805_pwrkey_driver = {.probe = rk805_pwrkey_probe,.driver = {.name = "rk805-pwrkey",},
};
module_platform_driver(rk805_pwrkey_driver);MODULE_AUTHOR("Joseph Chen <chenjh@rock-chips.com>");
MODULE_DESCRIPTION("RK805 PMIC Power Key driver");
MODULE_LICENSE("GPL");
6、中断处理函数。
三、查看log,侦测按键中断事件。
1、执行cat /proc/interrupts 查看硬件中断是否注册成功,查看软中断命令/proc/softirqs
这里我没按一下power键,对应的值会加2,下图是16加到18,如果我一直按着不松口,就会加1.
2、看串口log,发生中断事件后调用中断函数。
四、参考文章
imx6q gpio中断 以DTS为切入点-yaoqigui-ChinaUnix博客
linux dts interrupt_gaoguoxin2的专栏-CSDN博客_dts interrupts
中断映射和中断DTS解析 - 灰信网(软件开发博客聚合)
中断映射和中断DTS解析_bruk_spp的博客-CSDN博客
韦东山:在Linux设备树(DTS)中指定中断_在代码中获得中断 - 灰信网(软件开发博客聚合)
查看linux 中断_weixin_34062329的博客-CSDN博客