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