基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)

时间:2024-01-07 10:42:02

作者

彭東林

pengdonglin137@163.com

平臺

tiny4412 ADK

Linux-4.4.4

u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uImage做了稍許改動

概述

這篇博客以一個簡單的按鍵中斷來演示一下有了設備樹後的中斷的使用,其中涉及到新版kernel的pinctrl和gpio子系統。

正文

在tiny4412的底板上有四個key,如下:

基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)

基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)

上圖中,在沒有按鍵的時候,對應的GPIO是被拉高的,當按下鍵的時候,對應的GPIO被拉低,從而產生一個下降沿中斷。

有了上面的準備,首先我們需要修改設備樹,添加相應的節點和相關的屬性:

 diff --git a/arch/arm/boot/dts/exynos4412-tiny4412.dts b/arch/arm/boot/dts/exynos4412-tiny4412.dts
index 610202a..a130047
--- a/arch/arm/boot/dts/exynos4412-tiny4412.dts
+++ b/arch/arm/boot/dts/exynos4412-tiny4412.dts
@@ -, +, @@
};
};
#endif
+
+ interrupt_demo: interrupt_demo {
+ compatible = "tiny4412,interrupt_demo";
+ tiny4412,int_gpio1 = <&gpx3 GPIO_ACTIVE_HIGH>;
+ tiny4412,int_gpio2 = <&gpx3 GPIO_ACTIVE_HIGH>;
+ tiny4412,int_gpio3 = <&gpx3 GPIO_ACTIVE_HIGH>;
+ tiny4412,int_gpio4 = <&gpx3 GPIO_ACTIVE_HIGH>;
+ };
};

上面的代碼中,我們添加了四個屬性,對應的就是那四個key對應的gpio,參考gpx3的實現(exynos4x12-pinctrl.dtsi):

        gpx3: gpx3 {
gpio-controller;
#gpio-cells = <>; interrupt-controller;
#interrupt-cells = <>;
};

可以看到,gpx3含有gpio-controller和interrupt-controller屬性,表示它是一個gpio控制器和中斷控制器,它的gpio-cell爲2,意味着應該給這個gpx3傳遞兩個參數,以

tiny4412,int_gpio1 = <&gpx3 2 GPIO_ACTIVE_HIGH>

爲例,這兩個參數就是2和GPIO_ACTIVE_HIGH,如果參數不對的話,比如只傳了一個參數2,那麼在調用of_get_named_gpio會出現如下錯誤:

[   31.133799] /interrupt_demo: arguments longer than property
[ 31.133935] interrupt_demo interrupt_demo: Looking up tiny4412,int_gpio4 property in node /interrupt_demo failed -
[ 31.144562] interrupt_demo: probe of interrupt_demo failed with error -

使用 make dtbs 編譯完設備樹。

在Samsung的pinctrl驅動中加一些log:

@@ -, +, @@ static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
unsigned int virq; + printk("%s enter.\n", __func__);
+
if (!bank->irq_domain)
return -ENXIO;

然後我們編寫對應的驅動程序:

 #include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h> typedef struct
{
int gpio;
int irq;
char name[];
}int_demo_data_t; static irqreturn_t int_demo_isr(int irq, void *dev_id)
{
int_demo_data_t *data = dev_id; printk("%s enter, %s: gpio:%d, irq: %d\n", __func__, data->name, data->gpio, data->irq); return IRQ_HANDLED;
} static int int_demo_probe(struct platform_device *pdev) {
struct device *dev = &pdev->dev;
int irq_gpio = -;
int irq = -;
int ret = ;
int i = ;
int_demo_data_t *data = NULL; printk("%s enter.\n", __func__); if (!dev->of_node) {
dev_err(dev, "no platform data.\n");
goto err1;
} data = devm_kmalloc(dev, sizeof(*data)*, GFP_KERNEL);
if (!data) {
dev_err(dev, "no memory.\n");
goto err0;
} #if 0
for (i = ; i >= ; i--) {
sprintf(data[i].name, "tiny4412,int_gpio%d", i+);
#else
for (i = ; i < ; i++) {
#endif
irq_gpio = of_get_named_gpio(dev->of_node,
data[i].name, );
if (irq_gpio < ) {
dev_err(dev, "Looking up %s property in node %s failed %d\n",
data[i].name, dev->of_node->full_name, irq_gpio);
goto err1;
} data[i].gpio = irq_gpio; irq = gpio_to_irq(irq_gpio);
if (irq < ) {
dev_err(dev,
"Unable to get irq number for GPIO %d, error %d\n",
irq_gpio, irq);
goto err1;
} data[i].irq = irq; printk("%s: gpio: %d ---> irq (%d)\n", __func__, irq_gpio, irq); ret = devm_request_any_context_irq(dev, irq,
int_demo_isr, IRQF_TRIGGER_FALLING, data[i].name, data+i);
if (ret < ) {
dev_err(dev, "Unable to claim irq %d; error %d\n",
irq, ret);
goto err1;
}
} return ; err1:
devm_kfree(dev, data);
err0:
return -EINVAL;
} static int int_demo_remove(struct platform_device *pdev) { printk("%s enter.\n", __func__); return ;
} static const struct of_device_id int_demo_dt_ids[] = {
{ .compatible = "tiny4412,interrupt_demo", },
{},
}; MODULE_DEVICE_TABLE(of, int_demo_dt_ids); static struct platform_driver int_demo_driver = {
.driver = {
.name = "interrupt_demo",
.of_match_table = of_match_ptr(int_demo_dt_ids),
},
.probe = int_demo_probe,
.remove = int_demo_remove,
}; static int __init int_demo_init(void)
{
int ret; ret = platform_driver_register(&int_demo_driver);
if (ret)
printk(KERN_ERR "int demo: probe failed: %d\n", ret); return ret;
}
module_init(int_demo_init); static void __exit int_demo_exit(void)
{
platform_driver_unregister(&int_demo_driver);
}
module_exit(int_demo_exit); MODULE_LICENSE("GPL");

編譯驅動後,將ko文件拷貝到nfs目錄下,然後在開發板上執行 mount -t nfs -o nolock 192.168.2.6:/nfsroot /mnt 將共享目錄從PC上掛載到開發板上,然後insmod這個驅動:

[root@tiny4412 ]# mount -t nfs -o nolock 192.168.2.6:/nfsroot /mnt
[root@tiny4412 ]# cd /mnt
[root@tiny4412 mnt]# ls
fdt interrupt_demo.ko tiny4412.dts
[root@tiny4412 mnt]# insmod interrupt_demo.ko
[ 1655.872546] int_demo_probe enter.
[ 1655.872841] samsung_gpio_to_irq enter.
[ 1655.873061] int_demo_probe: gpio: 238 ---> irq (105)
[ 1655.873716] samsung_gpio_to_irq enter.
[ 1655.873906] int_demo_probe: gpio: 239 ---> irq (106)
[ 1655.874424] samsung_gpio_to_irq enter.
[ 1655.874773] int_demo_probe: gpio: 240 ---> irq (107)
[ 1655.879981] samsung_gpio_to_irq enter.
[ 1655.883485] int_demo_probe: gpio: 241 ---> irq (108)
[root@tiny4412 mnt]#
// 然後我們嘗試按底板上的按鍵,會看到相應的中斷log
[root@tiny4412 mnt]# [ 33.462207] int_demo_isr enter, tiny4412,int_gpio1: gpio:, irq:
[ 33.657304] int_demo_isr enter, tiny4412,int_gpio1: gpio:, irq:
[ 35.769955] int_demo_isr enter, tiny4412,int_gpio3: gpio:, irq:
[ 35.951373] int_demo_isr enter, tiny4412,int_gpio3: gpio:, irq:
[ 36.525804] int_demo_isr enter, tiny4412,int_gpio4: gpio:, irq:
[ 36.698501] int_demo_isr enter, tiny4412,int_gpio4: gpio:, irq:
[ 41.710481] int_demo_isr enter, tiny4412,int_gpio2: gpio:, irq:
[ 41.857190] int_demo_isr enter, tiny4412,int_gpio2: gpio:, irq: [root@tiny4412 mnt]# cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
: GIC Edge mct_comp_irq
: GIC Edge MCT
: GIC Edge mmc0
: GIC Edge .hsotg, .hsotg, dwc2_hsotg:usb1
: GIC Edge ehci_hcd:usb2, ohci_hcd:usb3
: GIC Edge .serial
: GIC Edge .i2c
: GIC Edge .pdma
: GIC Edge .pdma
: GIC Edge .mdma
: GIC Edge .pinctrl
: GIC Edge .pinctrl
: COMBINER Edge .pinctrl
: GIC Edge 106e0000.pinctrl
: GIC Edge dw-mci
: exynos4210_wkup_irq_chip Edge mma7660
: exynos_gpio_irq_chip Edge .sdhci cd
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio1
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio2
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio3
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio4
IPI0: CPU wakeup interrupts
IPI1: Timer broadcast interrupts
IPI2: Rescheduling interrupts
IPI3: Function call interrupts
IPI4: Single function call interrupts
IPI5: CPU stop interrupts
IPI6: IRQ work interrupts
IPI7: completion interrupts
Err:

如果我們修改驅動驅動,改變一下中斷的申請順序,然後重啓系統(測試發現,如果不重啓系統,而是重新加載新的驅動,gpio跟irq的對應關系沒有發生改變)

#if 1
for (i = ; i >= ; i--) {
sprintf(data[i].name, "tiny4412,int_gpio%d", i+);
#else
for (i = ; i < ; i++) {
#endif

此時,會看到如下的log:

[root@tiny4412 mnt]# insmod interrupt_demo.ko
[  119.735726] int_demo_probe enter.
[  119.736295] samsung_gpio_to_irq enter.
[  119.736417] int_demo_probe: gpio: 241 ---> irq (105)
[  119.736852] samsung_gpio_to_irq enter.
[  119.736985] int_demo_probe: gpio: 240 ---> irq (106)
[  119.737254] samsung_gpio_to_irq enter.
[  119.737893] int_demo_probe: gpio: 239 ---> irq (107)
[  119.743029] samsung_gpio_to_irq enter.
[  119.746631] int_demo_probe: gpio: 238 ---> irq (108)
[root@tiny4412 mnt]# cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
: GIC Edge mct_comp_irq
: GIC Edge MCT
... ...
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio4
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio3
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio2
: exynos4210_wkup_irq_chip Edge tiny4412,int_gpio1
... ...

未完待續... ...