Skip to content

Commit 249d69b

Browse files
committed
[dm][input] add new drivers
1. NI Ettus Research USRP E3xx Button support. 2. GPIO Keyboards support. Signed-off-by: GuEe-GUI <[email protected]>
1 parent ed7ebf7 commit 249d69b

File tree

6 files changed

+290
-0
lines changed

6 files changed

+290
-0
lines changed

components/drivers/input/keyboard/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ menuconfig RT_INPUT_KEYBOARD
22
bool "Keyboards"
33
default n
44

5+
config RT_INPUT_KEYBOARD_GPIO
6+
bool "GPIO"
7+
depends on RT_INPUT_KEYBOARD
8+
depends on RT_USING_OFW
9+
default n
10+
511
if RT_INPUT_KEYBOARD
612
osource "$(SOC_DM_INPUT_KEYBOARD_DIR)/Kconfig"
713
endif

components/drivers/input/keyboard/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ CPPPATH = [cwd + '/../../include']
1010

1111
src = []
1212

13+
if GetDepend(['RT_INPUT_KEYBOARD_GPIO']):
14+
src += ['keys-gpio.c']
15+
1316
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
1417

1518
Return('group')
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-3-08 GuEe-GUI the first version
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rtdevice.h>
13+
14+
#define DBG_TAG "input.keyboard.gpio"
15+
#define DBG_LVL DBG_INFO
16+
#include <rtdbg.h>
17+
18+
struct gpio_key
19+
{
20+
struct rt_input_device parent;
21+
22+
rt_base_t pin;
23+
rt_uint8_t mode;
24+
rt_uint32_t code;
25+
};
26+
27+
static void gpio_key_event(void *data)
28+
{
29+
struct gpio_key *gkey = data;
30+
31+
rt_input_report_key(&gkey->parent, gkey->code, 1);
32+
rt_input_sync(&gkey->parent);
33+
34+
rt_input_report_key(&gkey->parent, gkey->code, 0);
35+
rt_input_sync(&gkey->parent);
36+
}
37+
38+
static rt_err_t ofw_append_gpio_key(struct rt_ofw_node *np)
39+
{
40+
rt_err_t err = RT_EOK;
41+
rt_uint32_t debounce;
42+
const char *propname;
43+
struct gpio_key *gkey = rt_calloc(1, sizeof(*gkey));
44+
45+
gkey->pin = rt_ofw_get_named_pin(np, RT_NULL, 0, &gkey->mode, RT_NULL);
46+
47+
if (gkey->pin < 0)
48+
{
49+
err = gkey->pin;
50+
51+
goto _fail;
52+
}
53+
54+
if ((propname = rt_ofw_get_prop_fuzzy_name(np, ",code$")) &&
55+
!rt_ofw_prop_read_u32(np, propname, &gkey->code))
56+
{
57+
rt_input_set_capability(&gkey->parent, EV_KEY, gkey->code);
58+
59+
if (!(err = rt_input_device_register(&gkey->parent)))
60+
{
61+
err = rt_pin_attach_irq(gkey->pin, gkey->mode, gpio_key_event, gkey);
62+
63+
if (err)
64+
{
65+
rt_input_device_unregister(&gkey->parent);
66+
goto _fail;
67+
}
68+
69+
rt_pin_irq_enable(gkey->pin, RT_TRUE);
70+
}
71+
}
72+
73+
if (err)
74+
{
75+
goto _fail;
76+
}
77+
78+
if (!rt_ofw_prop_read_u32(np, "debounce-interval", &debounce))
79+
{
80+
rt_pin_debounce(gkey->pin, debounce);
81+
}
82+
83+
rt_ofw_data(np) = gkey;
84+
85+
return RT_EOK;
86+
87+
_fail:
88+
rt_free(gkey);
89+
90+
return err;
91+
}
92+
93+
static rt_err_t gpio_key_probe(struct rt_platform_device *pdev)
94+
{
95+
rt_err_t err = RT_EOK;
96+
struct rt_ofw_node *key_np, *np = pdev->parent.ofw_node;
97+
98+
rt_ofw_foreach_available_child_node(np, key_np)
99+
{
100+
err = ofw_append_gpio_key(key_np);
101+
102+
if (err == -RT_ENOMEM)
103+
{
104+
rt_ofw_node_put(key_np);
105+
106+
return err;
107+
}
108+
else if (err)
109+
{
110+
LOG_E("%s: create KEY fail", rt_ofw_node_full_name(key_np));
111+
continue;
112+
}
113+
}
114+
115+
return err;
116+
}
117+
118+
static rt_err_t gpio_key_remove(struct rt_platform_device *pdev)
119+
{
120+
struct rt_ofw_node *key_np, *np = pdev->parent.ofw_node;
121+
122+
rt_ofw_foreach_available_child_node(np, key_np)
123+
{
124+
struct gpio_key *gkey = rt_ofw_data(key_np);
125+
126+
if (!gkey)
127+
{
128+
continue;
129+
}
130+
131+
rt_ofw_data(key_np) = RT_NULL;
132+
133+
rt_pin_irq_enable(gkey->pin, RT_FALSE);
134+
rt_pin_detach_irq(gkey->pin);
135+
136+
rt_input_device_unregister(&gkey->parent);
137+
138+
rt_free(gkey);
139+
}
140+
141+
return RT_EOK;
142+
}
143+
144+
static const struct rt_ofw_node_id gpio_key_ofw_ids[] =
145+
{
146+
{ .compatible = "gpio-keys" },
147+
{ /* sentinel */ }
148+
};
149+
150+
static struct rt_platform_driver gpio_key_driver =
151+
{
152+
.name = "gpio-keys",
153+
.ids = gpio_key_ofw_ids,
154+
155+
.probe = gpio_key_probe,
156+
.remove = gpio_key_remove,
157+
};
158+
RT_PLATFORM_DRIVER_EXPORT(gpio_key_driver);

components/drivers/input/misc/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ menuconfig RT_INPUT_MISC
22
bool "Misc"
33
default n
44

5+
config RT_INPUT_MISC_BUTTON_E3X0
6+
bool "NI Ettus Research USRP E3xx Button support"
7+
depends on RT_INPUT_MISC
8+
default n
9+
510
if RT_INPUT_MISC
611
osource "$(SOC_DM_INPUT_MISC_DIR)/Kconfig"
712
endif

components/drivers/input/misc/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ CPPPATH = [cwd + '/../../include']
1010

1111
src = []
1212

13+
if GetDepend(['RT_INPUT_MISC_BUTTON_E3X0']):
14+
src += ['button-e3x0.c']
15+
1316
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
1417

1518
Return('group')
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-3-08 GuEe-GUI the first version
9+
*/
10+
11+
#include <rthw.h>
12+
#include <rtthread.h>
13+
#include <rtdevice.h>
14+
15+
struct e3x0_button
16+
{
17+
struct rt_input_device parent;
18+
19+
int press_irq, release_irq;
20+
};
21+
22+
static void e3x0_button_press_isr(int irqno, void *param)
23+
{
24+
struct e3x0_button *btn = param;
25+
26+
rt_input_report_key(&btn->parent, KEY_POWER, 1);
27+
rt_input_sync(&btn->parent);
28+
}
29+
30+
static void e3x0_button_release_isr(int irqno, void *param)
31+
{
32+
struct e3x0_button *btn = param;
33+
34+
rt_input_report_key(&btn->parent, KEY_POWER, 0);
35+
rt_input_sync(&btn->parent);
36+
}
37+
38+
static rt_err_t e3x0_button_probe(struct rt_platform_device *pdev)
39+
{
40+
rt_err_t err;
41+
struct rt_device *dev = &pdev->parent;
42+
struct e3x0_button *btn = rt_calloc(1, sizeof(*btn));
43+
44+
if (!btn)
45+
{
46+
return -RT_ENOMEM;
47+
}
48+
49+
if ((btn->press_irq = rt_dm_dev_get_irq_by_name(dev, "press")) < 0)
50+
{
51+
err = btn->press_irq;
52+
goto _fail;
53+
}
54+
55+
if ((btn->release_irq = rt_dm_dev_get_irq_by_name(dev, "release")) < 0)
56+
{
57+
err = btn->release_irq;
58+
goto _fail;
59+
}
60+
61+
rt_input_set_capability(&btn->parent, EV_KEY, KEY_POWER);
62+
63+
if ((err = rt_input_device_register(&btn->parent)))
64+
{
65+
goto _fail;
66+
}
67+
68+
dev->user_data = btn;
69+
70+
rt_hw_interrupt_install(btn->press_irq, e3x0_button_press_isr, btn, "button-e3x0-press");
71+
rt_hw_interrupt_umask(btn->press_irq);
72+
73+
rt_hw_interrupt_install(btn->release_irq, e3x0_button_release_isr, btn, "button-e3x0-release");
74+
rt_hw_interrupt_umask(btn->release_irq);
75+
76+
return RT_EOK;
77+
78+
_fail:
79+
rt_free(btn);
80+
81+
return err;
82+
}
83+
84+
static rt_err_t e3x0_button_remove(struct rt_platform_device *pdev)
85+
{
86+
struct e3x0_button *btn = pdev->parent.user_data;
87+
88+
rt_hw_interrupt_mask(btn->press_irq);
89+
rt_pic_detach_irq(btn->press_irq, btn);
90+
91+
rt_hw_interrupt_mask(btn->release_irq);
92+
rt_pic_detach_irq(btn->release_irq, btn);
93+
94+
rt_input_device_unregister(&btn->parent);
95+
96+
rt_free(btn);
97+
98+
return RT_EOK;
99+
}
100+
101+
static const struct rt_ofw_node_id e3x0_button_ofw_ids[] =
102+
{
103+
{ .compatible = "ettus,e3x0-button" },
104+
{ /* sentinel */ }
105+
};
106+
107+
static struct rt_platform_driver e3x0_button_driver =
108+
{
109+
.name = "e3x0-button",
110+
.ids = e3x0_button_ofw_ids,
111+
112+
.probe = e3x0_button_probe,
113+
.remove = e3x0_button_remove,
114+
};
115+
RT_PLATFORM_DRIVER_EXPORT(e3x0_button_driver);

0 commit comments

Comments
 (0)