android13/kernel-5.10/drivers/input/keyboard/adc-keys.c

314 lines
7.0 KiB
C
Executable File

// SPDX-License-Identifier: GPL-2.0-only
/*
* Input driver for resistor ladder connected on ADC
*
* Copyright (c) 2016 Alexandre Belloni
*/
#include <linux/err.h>
#include <linux/iio/consumer.h>
#include <linux/iio/types.h>
#include <linux/input.h>
#include <linux/input-polldev.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
static struct proc_dir_entry *root_entry_adckey;
static int g_key_counter = 0;
static int adckey_status = 0;
static int adckey_open(struct inode *inode, struct file *file)
{
return 0;
}
static ssize_t adckey_write(struct file *file, const char *buffer,size_t count, loff_t *data)
{
adckey_status = 1;
return count;
}
static ssize_t adckey_read(struct file *file, char __user * buffer, size_t count, loff_t *data)
{
//char adc = 0;
int len = 0;
char s[10] = {0};
if(*data)
return 0;
// printk("adckey_status %d\n",adckey_status);
len = sprintf(s+len, "%d\n",adckey_status);
return simple_read_from_buffer(buffer, count, data, s, 2);
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
static const struct proc_ops adckey_ops = {
.proc_open = adckey_open,
.proc_write = adckey_write,
.proc_read = adckey_read,
};
#else
static const struct file_operations adckey_ops = {
.owner = THIS_MODULE,
.open = adckey_open,
.write = adckey_write,
.read = adckey_read,
};
#endif
struct adc_keys_button {
u32 voltage;
u32 keycode;
};
struct adc_keys_state {
struct iio_channel *channel;
u32 num_keys;
u32 last_key;
u32 keyup_voltage;
const struct adc_keys_button *map;
};
//---------rpdzkj --
static int usb_mode_change(void)
{
int fd;
static char *mode_state = "host";
mm_segment_t old_fs=get_fs();
set_fs(KERNEL_DS);
fd=do_sys_open(AT_FDCWD,"/sys/kernel/debug/usb/fc000000.usb/mode",O_CREAT|O_RDWR, 0666);
if(fd>=0)
{
int i;
char after_buff[32]="0";
char before_buff[32]="0";
ksys_read(fd,before_buff,sizeof(before_buff));
if (!strncmp(before_buff,"host",4))
mode_state = "device";
else
mode_state = "host";
for ( i = 0; i < 3; i++){
ksys_write(fd,mode_state,sizeof(mode_state));
msleep(1000);
ksys_read(fd,after_buff,sizeof(after_buff));
if (!strncmp(after_buff, before_buff, 4))
break;
}
ksys_close(fd);
}
set_fs(old_fs);
return 0;
}
//---------
static void adc_keys_poll(struct input_dev *input)
{
struct adc_keys_state *st = input_get_drvdata(input);
int i, value, ret;
u32 diff, closest = 0xffffffff;
int keycode = 0;
ret = iio_read_channel_processed(st->channel, &value);
if (unlikely(ret < 0)) {
/* Forcibly release key if any was pressed */
value = st->keyup_voltage;
} else {
for (i = 0; i < st->num_keys; i++) {
diff = abs(st->map[i].voltage - value);
if (diff < closest) {
closest = diff;
keycode = st->map[i].keycode;
}
}
}
if (abs(st->keyup_voltage - value) < closest)
keycode = 0;
if (st->last_key && st->last_key != keycode)
input_report_key(input, st->last_key, 0);
if (keycode)
input_report_key(input, keycode, 1);
if (keycode == KEY_VOLUMEUP || keycode == KEY_VOLUMEDOWN)
{
g_key_counter++;
if (g_key_counter == 20)
usb_mode_change();
else if (g_key_counter == 60)
adckey_status = 1;
}
else
{
g_key_counter = 0;
}
input_sync(input);
st->last_key = keycode;
}
static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
{
struct adc_keys_button *map;
struct fwnode_handle *child;
int i;
st->num_keys = device_get_child_node_count(dev);
if (st->num_keys == 0) {
dev_err(dev, "keymap is missing\n");
return -EINVAL;
}
map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
if (!map)
return -ENOMEM;
i = 0;
device_for_each_child_node(dev, child) {
if (fwnode_property_read_u32(child, "press-threshold-microvolt",
&map[i].voltage)) {
dev_err(dev, "Key with invalid or missing voltage\n");
fwnode_handle_put(child);
return -EINVAL;
}
map[i].voltage /= 1000;
if (fwnode_property_read_u32(child, "linux,code",
&map[i].keycode)) {
dev_err(dev, "Key with invalid or missing linux,code\n");
fwnode_handle_put(child);
return -EINVAL;
}
i++;
}
st->map = map;
return 0;
}
static int adc_keys_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct adc_keys_state *st;
struct input_dev *input;
enum iio_chan_type type;
int i, value;
int error;
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
if (!st)
return -ENOMEM;
st->channel = devm_iio_channel_get(dev, "buttons");
if (IS_ERR(st->channel))
return PTR_ERR(st->channel);
if (!st->channel->indio_dev)
return -ENXIO;
error = iio_get_channel_type(st->channel, &type);
if (error < 0)
return error;
if (type != IIO_VOLTAGE) {
dev_err(dev, "Incompatible channel type %d\n", type);
return -EINVAL;
}
if (device_property_read_u32(dev, "keyup-threshold-microvolt",
&st->keyup_voltage)) {
dev_err(dev, "Invalid or missing keyup voltage\n");
return -EINVAL;
}
st->keyup_voltage /= 1000;
error = adc_keys_load_keymap(dev, st);
if (error)
return error;
input = devm_input_allocate_device(dev);
if (!input) {
dev_err(dev, "failed to allocate input device\n");
return -ENOMEM;
}
input_set_drvdata(input, st);
input->name = pdev->name;
input->phys = "adc-keys/input0";
input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;
__set_bit(EV_KEY, input->evbit);
for (i = 0; i < st->num_keys; i++)
__set_bit(st->map[i].keycode, input->keybit);
if (device_property_read_bool(dev, "autorepeat"))
__set_bit(EV_REP, input->evbit);
error = input_setup_polling(input, adc_keys_poll);
if (error) {
dev_err(dev, "Unable to set up polling: %d\n", error);
return error;
}
if (!device_property_read_u32(dev, "poll-interval", &value))
input_set_poll_interval(input, value);
error = input_register_device(input);
if (error) {
dev_err(dev, "Unable to register input device: %d\n", error);
return error;
}
root_entry_adckey = proc_mkdir("adckey", NULL);
proc_create("status", 0666 , root_entry_adckey , &adckey_ops);
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id adc_keys_of_match[] = {
{ .compatible = "adc-keys", },
{ }
};
MODULE_DEVICE_TABLE(of, adc_keys_of_match);
#endif
static struct platform_driver __refdata adc_keys_driver = {
.driver = {
.name = "adc_keys",
.of_match_table = of_match_ptr(adc_keys_of_match),
},
.probe = adc_keys_probe,
};
module_platform_driver(adc_keys_driver);
MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
MODULE_LICENSE("GPL v2");