mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-23 12:15:32 +08:00
[DM/Input] Add ADC joystick, keyboard, and touchscreen drivers (#11679)
* [DM/Input] Add ADC joystick, keyboard, and touchscreen drivers Add polling input drivers for ADC-connected joysticks and resistor- ladder keys. Parse Linux-compatible device-tree bindings, acquire ADC channels through the IIO framework, and report absolute-axis and key events. Add an SPI touchscreen driver for ADS7843, ADS7845, ADS7846, ADS7873, TSC2046, and XPT2046-compatible controllers. Support pen interrupts, coordinate and pressure reporting, sample filtering, regulator control, power management, and common touchscreen properties. Add an I2C touchscreen driver for Goodix GT9xx controllers with multi-touch reporting, GPIO-based reset and address selection, interrupt handling with polling fallback, regulator control, panel configuration, and alternate-address recovery. Adapt the drivers to the RT-Thread device model and input framework, following the corresponding Linux input drivers and device-tree bindings. Signed-off-by: GuEe-GUI <2991707448@qq.com> * style: format code with clang-format * drivers: input: address review feedback * style: format input changes for current master --------- Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
@@ -2,6 +2,13 @@ menuconfig RT_INPUT_JOYSTICK
|
||||
bool "Joystick"
|
||||
default n
|
||||
|
||||
config RT_INPUT_JOYSTICK_ADC
|
||||
bool "Simple joystick connected over ADC"
|
||||
depends on RT_INPUT_JOYSTICK
|
||||
depends on RT_USING_ADC
|
||||
depends on RT_USING_OFW
|
||||
default n
|
||||
|
||||
if RT_INPUT_JOYSTICK
|
||||
osource "$(SOC_DM_INPUT_JOYSTICK_DIR)/Kconfig"
|
||||
endif
|
||||
|
||||
@@ -10,6 +10,9 @@ CPPPATH = [cwd + '/../../include']
|
||||
|
||||
src = []
|
||||
|
||||
if GetDepend(['RT_INPUT_JOYSTICK_ADC']):
|
||||
src += ['js-adc.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-3-08 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define DBG_TAG "input.js.adc"
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct adc_joystick_axis
|
||||
{
|
||||
rt_uint32_t code;
|
||||
rt_uint32_t range[2];
|
||||
rt_uint32_t fuzz;
|
||||
rt_uint32_t flat;
|
||||
rt_uint32_t channel;
|
||||
rt_uint32_t inverted;
|
||||
|
||||
struct rt_adc_device *adc_dev;
|
||||
};
|
||||
|
||||
struct adc_joysticks
|
||||
{
|
||||
struct rt_input_device parent;
|
||||
|
||||
rt_uint32_t num_axis;
|
||||
struct adc_joystick_axis axis[];
|
||||
};
|
||||
|
||||
static void adc_joysticks_poll(struct rt_input_device *idev)
|
||||
{
|
||||
int value;
|
||||
struct adc_joysticks *aj = rt_container_of(idev, struct adc_joysticks, parent);
|
||||
|
||||
for (int i = 0; i < aj->num_axis; ++i)
|
||||
{
|
||||
struct adc_joystick_axis *axis = &aj->axis[i];
|
||||
|
||||
value = rt_adc_read(axis->adc_dev, axis->channel);
|
||||
if (value < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (axis->inverted)
|
||||
{
|
||||
value = (int)(axis->range[0] + axis->range[1]) - value;
|
||||
}
|
||||
|
||||
rt_input_report_abs(&aj->parent, axis->code, value);
|
||||
}
|
||||
|
||||
rt_input_sync(&aj->parent);
|
||||
}
|
||||
|
||||
static rt_err_t adc_joystick_probe(struct rt_platform_device *pdev)
|
||||
{
|
||||
rt_err_t err;
|
||||
rt_uint32_t interval;
|
||||
rt_uint32_t num_axis;
|
||||
struct adc_joysticks *aj;
|
||||
struct adc_joystick_axis *axis;
|
||||
struct rt_device *dev = &pdev->parent;
|
||||
struct rt_ofw_node *np = dev->ofw_node, *axis_np;
|
||||
|
||||
num_axis = rt_ofw_get_child_count(np);
|
||||
|
||||
if (!num_axis)
|
||||
{
|
||||
LOG_E("Keymap is missing");
|
||||
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
aj = rt_calloc(1, sizeof(*aj) + sizeof(struct adc_joystick_axis) * num_axis);
|
||||
|
||||
if (!aj)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_ofw_foreach_child_node(np, axis_np)
|
||||
{
|
||||
rt_uint32_t reg;
|
||||
const char *propname;
|
||||
|
||||
if (rt_ofw_prop_read_u32(axis_np, "reg", ®))
|
||||
{
|
||||
err = -RT_EINVAL;
|
||||
rt_ofw_node_put(axis_np);
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
if (reg >= num_axis)
|
||||
{
|
||||
LOG_E("%s: reg %u out of range (num_axis %u)",
|
||||
rt_ofw_node_full_name(axis_np), reg, num_axis);
|
||||
rt_ofw_node_put(axis_np);
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
axis = &aj->axis[reg];
|
||||
|
||||
if (axis->adc_dev)
|
||||
{
|
||||
LOG_E("%s: duplicate reg %u", rt_ofw_node_full_name(axis_np), reg);
|
||||
rt_ofw_node_put(axis_np);
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
axis->adc_dev = rt_iio_channel_get_by_index(dev, reg, &axis->channel);
|
||||
|
||||
if (!axis->adc_dev)
|
||||
{
|
||||
rt_ofw_node_put(axis_np);
|
||||
|
||||
LOG_E("ADC device not found");
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
if (rt_ofw_prop_read_u32(axis_np, "abs-flat", &axis->flat))
|
||||
{
|
||||
axis->flat = 0;
|
||||
}
|
||||
|
||||
if (rt_ofw_prop_read_u32(axis_np, "abs-fuzz", &axis->fuzz))
|
||||
{
|
||||
axis->fuzz = 0;
|
||||
}
|
||||
|
||||
if (rt_ofw_prop_read_u32_array_index(axis_np, "abs-range", 0, 2, axis->range))
|
||||
{
|
||||
LOG_E("%s: Axis[%d] missing %s", rt_ofw_node_full_name(axis_np), reg, "abs-range");
|
||||
rt_ofw_node_put(axis_np);
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
axis->inverted = 0;
|
||||
if (axis->range[0] > axis->range[1])
|
||||
{
|
||||
rt_uint32_t t = axis->range[0];
|
||||
|
||||
axis->range[0] = axis->range[1];
|
||||
axis->range[1] = t;
|
||||
axis->inverted = 1;
|
||||
}
|
||||
|
||||
if (!(propname = rt_ofw_get_prop_fuzzy_name(axis_np, ",code$")) ||
|
||||
rt_ofw_prop_read_u32(axis_np, propname, &axis->code))
|
||||
{
|
||||
LOG_E("%s: Axis[%d] missing %s", rt_ofw_node_full_name(axis_np), reg, "*,code");
|
||||
rt_ofw_node_put(axis_np);
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rt_input_set_absinfo(&aj->parent, axis->code,
|
||||
axis->range[0], axis->range[1], axis->fuzz, axis->flat);
|
||||
rt_input_set_capability(&aj->parent, EV_ABS, axis->code);
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_axis; ++i)
|
||||
{
|
||||
if (!aj->axis[i].adc_dev)
|
||||
{
|
||||
LOG_E("Axis[%d] is missing", i);
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
}
|
||||
|
||||
aj->num_axis = num_axis;
|
||||
|
||||
if (rt_ofw_prop_read_u32(np, "poll-interval", &interval))
|
||||
{
|
||||
interval = 200;
|
||||
}
|
||||
|
||||
if ((err = rt_input_setup_polling(&aj->parent, adc_joysticks_poll)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rt_input_set_poll_interval(&aj->parent, interval);
|
||||
|
||||
if ((err = rt_input_device_register(&aj->parent)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
dev->user_data = aj;
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
_fail:
|
||||
rt_input_remove_config(&aj->parent);
|
||||
rt_free(aj);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t adc_joystick_remove(struct rt_platform_device *pdev)
|
||||
{
|
||||
struct adc_joysticks *aj = pdev->parent.user_data;
|
||||
|
||||
pdev->parent.user_data = RT_NULL;
|
||||
rt_input_device_unregister(&aj->parent);
|
||||
|
||||
rt_free(aj);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static const struct rt_ofw_node_id adc_joystick_ofw_ids[] = {
|
||||
{ .compatible = "adc-joystick" },
|
||||
{ /* sentinel */ }
|
||||
};
|
||||
|
||||
static struct rt_platform_driver adc_joystick_driver = {
|
||||
.name = "adc-joystick",
|
||||
.ids = adc_joystick_ofw_ids,
|
||||
|
||||
.probe = adc_joystick_probe,
|
||||
.remove = adc_joystick_remove,
|
||||
};
|
||||
RT_PLATFORM_DRIVER_EXPORT(adc_joystick_driver);
|
||||
@@ -2,6 +2,13 @@ menuconfig RT_INPUT_KEYBOARD
|
||||
bool "Keyboards"
|
||||
default n
|
||||
|
||||
config RT_INPUT_KEYBOARD_ADC
|
||||
bool "ADC Ladder Buttons"
|
||||
depends on RT_INPUT_KEYBOARD
|
||||
depends on RT_USING_OFW
|
||||
depends on RT_USING_ADC
|
||||
default n
|
||||
|
||||
config RT_INPUT_KEYBOARD_GPIO
|
||||
bool "GPIO"
|
||||
depends on RT_INPUT_KEYBOARD
|
||||
|
||||
@@ -10,6 +10,9 @@ CPPPATH = [cwd + '/../../include']
|
||||
|
||||
src = []
|
||||
|
||||
if GetDepend(['RT_INPUT_KEYBOARD_ADC']):
|
||||
src += ['keys-adc.c']
|
||||
|
||||
if GetDepend(['RT_INPUT_KEYBOARD_GPIO']):
|
||||
src += ['keys-gpio.c']
|
||||
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-3-08 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define DBG_TAG "input.keyboard.adc"
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct adc_keys_button
|
||||
{
|
||||
rt_uint32_t voltage;
|
||||
rt_uint32_t keycode;
|
||||
};
|
||||
|
||||
struct adc_keys
|
||||
{
|
||||
struct rt_input_device parent;
|
||||
struct rt_adc_device *adc_dev;
|
||||
|
||||
int channel;
|
||||
rt_uint32_t num_keys;
|
||||
rt_uint32_t last_key;
|
||||
rt_uint32_t keyup_voltage;
|
||||
struct adc_keys_button kbtn[];
|
||||
};
|
||||
|
||||
static void adc_keys_poll(struct rt_input_device *idev)
|
||||
{
|
||||
int value, keycode = 0;
|
||||
rt_uint32_t diff, closest = 0xffffffff;
|
||||
struct adc_keys *tk = rt_container_of(idev, struct adc_keys, parent);
|
||||
|
||||
value = rt_adc_read(tk->adc_dev, tk->channel);
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
/* Forcibly release key if any was pressed */
|
||||
value = tk->keyup_voltage;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < tk->num_keys; ++i)
|
||||
{
|
||||
rt_uint32_t sample = (rt_uint32_t)value;
|
||||
|
||||
diff = tk->kbtn[i].voltage > sample ? tk->kbtn[i].voltage - sample : sample - tk->kbtn[i].voltage;
|
||||
|
||||
if (diff < closest)
|
||||
{
|
||||
closest = diff;
|
||||
keycode = tk->kbtn[i].keycode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (value >= 0)
|
||||
{
|
||||
rt_uint32_t sample = (rt_uint32_t)value;
|
||||
rt_uint32_t keyup_diff = tk->keyup_voltage > sample ? tk->keyup_voltage - sample : sample - tk->keyup_voltage;
|
||||
|
||||
if (keyup_diff < closest)
|
||||
{
|
||||
keycode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (tk->last_key && tk->last_key != keycode)
|
||||
{
|
||||
rt_input_report_key(&tk->parent, tk->last_key, 0);
|
||||
}
|
||||
|
||||
if (keycode && tk->last_key != keycode)
|
||||
{
|
||||
rt_input_report_key(&tk->parent, keycode, 1);
|
||||
}
|
||||
|
||||
if (tk->last_key != keycode)
|
||||
{
|
||||
rt_input_sync(&tk->parent);
|
||||
}
|
||||
|
||||
tk->last_key = keycode;
|
||||
}
|
||||
|
||||
static rt_err_t adc_key_probe(struct rt_platform_device *pdev)
|
||||
{
|
||||
int i = 0;
|
||||
rt_err_t err;
|
||||
rt_uint32_t interval;
|
||||
rt_uint32_t num_keys;
|
||||
struct adc_keys *tk;
|
||||
struct rt_device *dev = &pdev->parent;
|
||||
struct rt_ofw_node *np = dev->ofw_node, *key_np;
|
||||
|
||||
num_keys = rt_ofw_get_child_count(np);
|
||||
|
||||
if (!num_keys)
|
||||
{
|
||||
LOG_E("Keymap is missing");
|
||||
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
tk = rt_calloc(1, sizeof(*tk) + sizeof(struct adc_keys_button) * num_keys);
|
||||
|
||||
if (!tk)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
tk->adc_dev = rt_iio_channel_get_by_name(dev, "buttons", &tk->channel);
|
||||
|
||||
if (!tk->adc_dev)
|
||||
{
|
||||
LOG_E("ADC device not found");
|
||||
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rt_ofw_foreach_child_node(np, key_np)
|
||||
{
|
||||
const char *propname;
|
||||
|
||||
if (rt_ofw_prop_read_u32(key_np, "press-threshold-microvolt",
|
||||
&tk->kbtn[i].voltage))
|
||||
{
|
||||
LOG_E("%s: Key with invalid or missing %s",
|
||||
rt_ofw_node_full_name(key_np), "press-threshold-microvolt");
|
||||
rt_ofw_node_put(key_np);
|
||||
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
tk->kbtn[i].voltage /= 1000;
|
||||
|
||||
if (!(propname = rt_ofw_get_prop_fuzzy_name(key_np, ",code$")) ||
|
||||
rt_ofw_prop_read_u32(key_np, propname, &tk->kbtn[i].keycode))
|
||||
{
|
||||
LOG_E("%s: Key with invalid or missing %s",
|
||||
rt_ofw_node_full_name(key_np), "*,code");
|
||||
rt_ofw_node_put(key_np);
|
||||
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rt_input_set_capability(&tk->parent, EV_KEY, tk->kbtn[i].keycode);
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
tk->num_keys = num_keys;
|
||||
|
||||
if (rt_ofw_prop_read_u32(np, "keyup-threshold-microvolt", &tk->keyup_voltage))
|
||||
{
|
||||
LOG_E("Invalid or missing keyup voltage");
|
||||
|
||||
err = -RT_EINVAL;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
tk->keyup_voltage /= 1000;
|
||||
|
||||
if (rt_ofw_prop_read_u32(np, "poll-interval", &interval))
|
||||
{
|
||||
interval = 200;
|
||||
}
|
||||
|
||||
if ((err = rt_input_setup_polling(&tk->parent, adc_keys_poll)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rt_input_set_poll_interval(&tk->parent, interval);
|
||||
|
||||
if ((err = rt_input_device_register(&tk->parent)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
dev->user_data = tk;
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
_fail:
|
||||
rt_input_remove_config(&tk->parent);
|
||||
rt_free(tk);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t adc_key_remove(struct rt_platform_device *pdev)
|
||||
{
|
||||
struct adc_keys *tk = pdev->parent.user_data;
|
||||
|
||||
pdev->parent.user_data = RT_NULL;
|
||||
rt_input_device_unregister(&tk->parent);
|
||||
|
||||
rt_free(tk);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static const struct rt_ofw_node_id adc_key_ofw_ids[] = {
|
||||
{ .compatible = "adc-keys" },
|
||||
{ /* sentinel */ }
|
||||
};
|
||||
|
||||
static struct rt_platform_driver adc_key_driver = {
|
||||
.name = "adc-keys",
|
||||
.ids = adc_key_ofw_ids,
|
||||
|
||||
.probe = adc_key_probe,
|
||||
.remove = adc_key_remove,
|
||||
};
|
||||
RT_PLATFORM_DRIVER_EXPORT(adc_key_driver);
|
||||
@@ -3,6 +3,24 @@ menuconfig RT_INPUT_TOUCHSCREEN
|
||||
select RT_USING_TOUCH
|
||||
default n
|
||||
|
||||
config RT_INPUT_TOUCHSCREEN_ADS7846
|
||||
bool "TI ADS7843/45/46/73 XPT/TSC2046 touch screen support"
|
||||
depends on RT_INPUT_TOUCHSCREEN
|
||||
depends on RT_USING_SPI
|
||||
depends on RT_USING_OFW
|
||||
depends on RT_USING_PIN
|
||||
depends on RT_USING_REGULATOR
|
||||
default n
|
||||
|
||||
config RT_INPUT_TOUCHSCREEN_GOODIX
|
||||
bool "Goodix GT9xx capacitive touch screen support"
|
||||
depends on RT_INPUT_TOUCHSCREEN
|
||||
depends on RT_USING_I2C
|
||||
depends on RT_USING_OFW
|
||||
depends on RT_USING_PIN
|
||||
depends on RT_USING_REGULATOR
|
||||
default n
|
||||
|
||||
if RT_INPUT_TOUCHSCREEN
|
||||
osource "$(SOC_DM_INPUT_TOUCHSCREEN_DIR)/Kconfig"
|
||||
endif
|
||||
|
||||
@@ -10,6 +10,12 @@ CPPPATH = [cwd + '/../../include']
|
||||
|
||||
src = []
|
||||
|
||||
if GetDepend(['RT_INPUT_TOUCHSCREEN_ADS7846']):
|
||||
src += ['ts-ads7846.c']
|
||||
|
||||
if GetDepend(['RT_INPUT_TOUCHSCREEN_GOODIX']):
|
||||
src += ['ts-goodix.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user