| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* |
| 4 |
* h3600 atmel micro companion support, key subdevice |
| 5 |
* based on previous kernel 2.4 version |
| 6 |
* Author : Alessandro Gardich <[email protected]> |
| 7 |
* Author : Linus Walleij <[email protected]> |
| 8 |
*/ |
| 9 |
#include <linux/module.h> |
| 10 |
#include <linux/init.h> |
| 11 |
#include <linux/fs.h> |
| 12 |
#include <linux/interrupt.h> |
| 13 |
#include <linux/sched.h> |
| 14 |
#include <linux/pm.h> |
| 15 |
#include <linux/sysctl.h> |
| 16 |
#include <linux/proc_fs.h> |
| 17 |
#include <linux/delay.h> |
| 18 |
#include <linux/device.h> |
| 19 |
#include <linux/input.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/mfd/ipaq-micro.h> |
| 22 |
|
| 23 |
struct ipaq_micro_keys { |
| 24 |
struct ipaq_micro *micro; |
| 25 |
struct input_dev *input; |
| 26 |
u16 *codes; |
| 27 |
}; |
| 28 |
|
| 29 |
static const u16 micro_keycodes[] = { |
| 30 |
KEY_RECORD, /* 1: Record button */ |
| 31 |
KEY_CALENDAR, /* 2: Calendar */ |
| 32 |
KEY_ADDRESSBOOK, /* 3: Contacts (looks like Outlook) */ |
| 33 |
KEY_MAIL, /* 4: Envelope (Q on older iPAQs) */ |
| 34 |
KEY_HOMEPAGE, /* 5: Start (looks like swoopy arrow) */ |
| 35 |
KEY_UP, /* 6: Up */ |
| 36 |
KEY_RIGHT, /* 7: Right */ |
| 37 |
KEY_LEFT, /* 8: Left */ |
| 38 |
KEY_DOWN, /* 9: Down */ |
| 39 |
}; |
| 40 |
|
| 41 |
static void micro_key_receive(void *data, int len, unsigned char *msg) |
| 42 |
{ |
| 43 |
struct ipaq_micro_keys *keys = data; |
| 44 |
int key, down; |
| 45 |
|
| 46 |
down = 0x80 & msg[0]; |
| 47 |
key = 0x7f & msg[0]; |
| 48 |
|
| 49 |
if (key < ARRAY_SIZE(micro_keycodes)) { |
| 50 |
input_report_key(keys->input, keys->codes[key], down); |
| 51 |
input_sync(keys->input); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void micro_key_start(struct ipaq_micro_keys *keys) |
| 56 |
{ |
| 57 |
guard(spinlock)(&keys->micro->lock); |
| 58 |
|
| 59 |
keys->micro->key = micro_key_receive; |
| 60 |
keys->micro->key_data = keys; |
| 61 |
} |
| 62 |
|
| 63 |
static void micro_key_stop(struct ipaq_micro_keys *keys) |
| 64 |
{ |
| 65 |
guard(spinlock)(&keys->micro->lock); |
| 66 |
|
| 67 |
keys->micro->key = NULL; |
| 68 |
keys->micro->key_data = NULL; |
| 69 |
} |
| 70 |
|
| 71 |
static int micro_key_open(struct input_dev *input) |
| 72 |
{ |
| 73 |
struct ipaq_micro_keys *keys = input_get_drvdata(input); |
| 74 |
|
| 75 |
micro_key_start(keys); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static void micro_key_close(struct input_dev *input) |
| 81 |
{ |
| 82 |
struct ipaq_micro_keys *keys = input_get_drvdata(input); |
| 83 |
|
| 84 |
micro_key_stop(keys); |
| 85 |
} |
| 86 |
|
| 87 |
static int micro_key_probe(struct platform_device *pdev) |
| 88 |
{ |
| 89 |
struct ipaq_micro_keys *keys; |
| 90 |
int error; |
| 91 |
int i; |
| 92 |
|
| 93 |
keys = devm_kzalloc(&pdev->dev, sizeof(*keys), GFP_KERNEL); |
| 94 |
if (!keys) |
| 95 |
return -ENOMEM; |
| 96 |
|
| 97 |
keys->micro = dev_get_drvdata(pdev->dev.parent); |
| 98 |
|
| 99 |
keys->input = devm_input_allocate_device(&pdev->dev); |
| 100 |
if (!keys->input) |
| 101 |
return -ENOMEM; |
| 102 |
|
| 103 |
keys->input->keycodesize = sizeof(micro_keycodes[0]); |
| 104 |
keys->input->keycodemax = ARRAY_SIZE(micro_keycodes); |
| 105 |
keys->codes = devm_kmemdup_array(&pdev->dev, micro_keycodes, keys->input->keycodemax, |
| 106 |
keys->input->keycodesize, GFP_KERNEL); |
| 107 |
if (!keys->codes) |
| 108 |
return -ENOMEM; |
| 109 |
|
| 110 |
keys->input->keycode = keys->codes; |
| 111 |
|
| 112 |
__set_bit(EV_KEY, keys->input->evbit); |
| 113 |
for (i = 0; i < ARRAY_SIZE(micro_keycodes); i++) |
| 114 |
__set_bit(micro_keycodes[i], keys->input->keybit); |
| 115 |
|
| 116 |
keys->input->name = "h3600 micro keys"; |
| 117 |
keys->input->open = micro_key_open; |
| 118 |
keys->input->close = micro_key_close; |
| 119 |
input_set_drvdata(keys->input, keys); |
| 120 |
|
| 121 |
error = input_register_device(keys->input); |
| 122 |
if (error) |
| 123 |
return error; |
| 124 |
|
| 125 |
platform_set_drvdata(pdev, keys); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static int micro_key_suspend(struct device *dev) |
| 130 |
{ |
| 131 |
struct ipaq_micro_keys *keys = dev_get_drvdata(dev); |
| 132 |
|
| 133 |
micro_key_stop(keys); |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static int micro_key_resume(struct device *dev) |
| 139 |
{ |
| 140 |
struct ipaq_micro_keys *keys = dev_get_drvdata(dev); |
| 141 |
struct input_dev *input = keys->input; |
| 142 |
|
| 143 |
guard(mutex)(&input->mutex); |
| 144 |
|
| 145 |
if (input_device_enabled(input)) |
| 146 |
micro_key_start(keys); |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static DEFINE_SIMPLE_DEV_PM_OPS(micro_key_dev_pm_ops, |
| 152 |
micro_key_suspend, micro_key_resume); |
| 153 |
|
| 154 |
static struct platform_driver micro_key_device_driver = { |
| 155 |
.driver = { |
| 156 |
.name = "ipaq-micro-keys", |
| 157 |
.pm = pm_sleep_ptr(µ_key_dev_pm_ops), |
| 158 |
}, |
| 159 |
.probe = micro_key_probe, |
| 160 |
}; |
| 161 |
module_platform_driver(micro_key_device_driver); |
| 162 |
|
| 163 |
MODULE_LICENSE("GPL"); |
| 164 |
MODULE_DESCRIPTION("driver for iPAQ Atmel micro keys"); |
| 165 |
MODULE_ALIAS("platform:ipaq-micro-keys"); |