Input: add qci keyboard driver From: Neil Leeder This driver is for the QCI i2c keyboard used on Quanta smartbooks. Signed-off-by: Horace Fu [nleeder@codeaurora.org: cleanup to latest kernel standards] Signed-off-by: Neil Leeder Signed-off-by: Dmitry Torokhov --- drivers/input/keyboard/Kconfig | 12 ++ drivers/input/keyboard/Makefile | 1 drivers/input/keyboard/qci_gkbd.c | 259 ++++++++++++++++++++++++++++++++++++ include/linux/input/qci_keyboard.h | 25 +++ 4 files changed, 297 insertions(+), 0 deletions(-) create mode 100644 drivers/input/keyboard/qci_gkbd.c create mode 100644 include/linux/input/qci_keyboard.h diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig index d63f566..2f38feb 100644 --- a/drivers/input/keyboard/Kconfig +++ b/drivers/input/keyboard/Kconfig @@ -373,6 +373,18 @@ config KEYBOARD_SAMSUNG To compile this driver as a module, choose M here: the module will be called samsung-keypad. +config KEYBOARD_QCIGKBD + tristate "Quanta Computer Inc. keyboard" + depends on I2C + help + This driver supports the i2c keyboard on Quanta smartbook devices. + + Say Y here if you have a Quanta-based smartbook or notepad + device and want to use the Quanta i2c keyboard driver. + + To compile this driver as a module, choose M here: the + module will be called qci_gkbd. + config KEYBOARD_STOWAWAY tristate "Stowaway keyboard" select SERIO diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile index c13809c..82f69f7 100644 --- a/drivers/input/keyboard/Makefile +++ b/drivers/input/keyboard/Makefile @@ -34,6 +34,7 @@ obj-$(CONFIG_KEYBOARD_OMAP4) += omap4-keypad.o obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o obj-$(CONFIG_KEYBOARD_PXA930_ROTARY) += pxa930_rotary.o +obj-$(CONFIG_KEYBOARD_QCIGKBD) += qci_gkbd.o obj-$(CONFIG_KEYBOARD_QT2160) += qt2160.o obj-$(CONFIG_KEYBOARD_SAMSUNG) += samsung-keypad.o obj-$(CONFIG_KEYBOARD_SH_KEYSC) += sh_keysc.o diff --git a/drivers/input/keyboard/qci_gkbd.c b/drivers/input/keyboard/qci_gkbd.c new file mode 100644 index 0000000..6cc06c4 --- /dev/null +++ b/drivers/input/keyboard/qci_gkbd.c @@ -0,0 +1,259 @@ +/* Quanta I2C Keyboard Driver + * + * Copyright (C) 2009 Quanta Computer Inc. + * Author: Hsin Wu + * Author: Austin Lai + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +/* + * This driver communicates via I2C to the nuvoTon WPCE775x Embedded + * Controller, which has the keyboard attached to it. + * + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define QCI_KEYBOARD_ID_NAME "qci-gkbd" +#define QCI_KEYBOARD_NAME "Quanta Keyboard" +#define QCI_KEYBOARD_DEVICE "/qci-gkbd/input0" +#define QCI_KEYBOARD_CMD_ENABLE 0xF4 +#define QCI_KEYBOARD_CMD_DISABLE 0xF5 +#define QCI_KEYBOARD_ACK 0xFA +#define QCI_KEYBOARD_MAX_KEY 127 + +struct qci_keyboard { + struct i2c_client *client; + struct input_dev *input; + unsigned int gpio; + unsigned int irq; +}; + +#ifdef CONFIG_PM +static int qcikbd_suspend(struct device *dev) +{ + struct i2c_client *client = container_of(dev, struct i2c_client, dev); + struct qci_keyboard *qcikbd = i2c_get_clientdata(client); + + disable_irq(qcikbd->irq); + + return 0; +} + +static int qcikbd_resume(struct device *dev) +{ + struct i2c_client *client = container_of(dev, struct i2c_client, dev); + struct qci_keyboard *qcikbd = i2c_get_clientdata(client); + + enable_irq(qcikbd->irq); + + return 0; +} + +static const struct dev_pm_ops qcikbd_pm_ops = { + .suspend = qcikbd_suspend, + .resume = qcikbd_resume, +}; +#endif + +static irqreturn_t qcikbd_interrupt(int irq, void *dev_id) +{ + struct qci_keyboard *qcikbd = dev_id; + struct input_dev *input = qcikbd->input; + int rc; + unsigned int scancode; + unsigned char data; + bool down; + + rc = i2c_master_recv(qcikbd->client, &data, 1); + if (rc != 1) + goto irq_exit; + + if (data == QCI_KEYBOARD_ACK) + goto irq_exit; + + scancode = data & 0x7f; + down = !(data & 0x80); + + if (scancode) { + input_event(input, EV_MSC, MSC_SCAN, scancode); + input_report_key(input, scancode, down); + input_sync(input); + } + +irq_exit: + return IRQ_HANDLED; +} + +static int qcikbd_open(struct input_dev *dev) +{ + struct qci_keyboard *qcikbd = input_get_drvdata(dev); + u8 buf[1] = { QCI_KEYBOARD_CMD_ENABLE }; + + i2c_master_send(qcikbd->client, buf, 1); + + return 0; +} + +static void qcikbd_close(struct input_dev *dev) +{ + struct qci_keyboard *qcikbd = input_get_drvdata(dev); + u8 buf[1] = { QCI_KEYBOARD_CMD_DISABLE }; + + i2c_master_send(qcikbd->client, buf, 1); +} + +static int __devinit qcikbd_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + const struct qci_keyboard_platform_data *pd = client->dev.platform_data; + struct qci_keyboard *qcikbd; + struct input_dev *input; + int irq_trigger_type; + int err; + int i; + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { + pr_err("i2c functionality failed\n"); + return -ENODEV; + } + + qcikbd = kzalloc(sizeof(struct qci_keyboard), GFP_KERNEL); + input = input_allocate_device(); + if (!qcikbd || input) { + err = -ENOMEM; + goto err_free_mem; + } + + qcikbd->client = client; + qcikbd->input = input; + qcikbd->gpio = client->irq; + + input->name = QCI_KEYBOARD_NAME; + input->phys = QCI_KEYBOARD_DEVICE; + input->id.bustype = BUS_I2C; + input->id.vendor = 0x1050; + input->id.product = 0x0006; + input->id.version = 0x0004; + input->open = qcikbd_open; + input->close = qcikbd_close; + + __set_bit(EV_KEY, input->evbit); + __set_bit(EV_REP, input->evbit); + __set_bit(MSC_SCAN, input->mscbit); + + /* Enable all supported keys */ + for (i = 1; i <= QCI_KEYBOARD_MAX_KEY; i++) + __set_bit(i, input->keybit); + + input_set_drvdata(qcikbd->input, qcikbd); + + err = gpio_request(qcikbd->gpio, "qci-gkbd"); + if (err) { + pr_err("gpio request failed\n"); + goto err_free_mem; + } + + err = gpio_direction_input(qcikbd->gpio); + if (err) { + pr_err("gpio direction failed\n"); + goto err_free_gpio; + } + + qcikbd->irq = gpio_to_irq(qcikbd->gpio); + irq_trigger_type = pd ? pd->irq_trigger_type : IRQF_TRIGGER_FALLING; + err = request_threaded_irq(qcikbd->irq, NULL, qcikbd_interrupt, + irq_trigger_type, client->name, qcikbd); + if (err) { + pr_err("unable to get IRQ\n"); + goto err_free_gpio; + } + + err = input_register_device(qcikbd->input); + if (err) { + pr_err("input register device failed\n"); + goto err_free_irq; + } + + i2c_set_clientdata(client, qcikbd); + return 0; + +err_free_irq: + free_irq(qcikbd->irq, qcikbd); +err_free_gpio: + gpio_free(qcikbd->gpio); +err_free_mem: + input_free_device(input); + kfree(qcikbd); + + return err; +} + +static int __devexit qcikbd_remove(struct i2c_client *dev) +{ + struct qci_keyboard *qcikbd = i2c_get_clientdata(dev); + + free_irq(qcikbd->irq, qcikbd); + gpio_free(qcikbd->gpio); + input_unregister_device(qcikbd->input); + kfree(qcikbd); + + return 0; +} + +static const struct i2c_device_id qcikbd_idtable[] = { + { "wpce775-keyboard", 0 }, + { } +}; + +MODULE_DEVICE_TABLE(i2c, qcikbd_idtable); + +static struct i2c_driver i2ckbd_driver = { + .driver = { + .owner = THIS_MODULE, + .name = QCI_KEYBOARD_ID_NAME, +#ifdef CONFIG_PM + .pm = &qcikbd_pm_ops, +#endif + }, + .probe = qcikbd_probe, + .remove = __devexit_p(qcikbd_remove), + .id_table = qcikbd_idtable, +}; + +static int __init qcikbd_init(void) +{ + return i2c_add_driver(&i2ckbd_driver); +} + +static void __exit qcikbd_exit(void) +{ + i2c_del_driver(&i2ckbd_driver); +} + +module_init(qcikbd_init); +module_exit(qcikbd_exit); + +MODULE_AUTHOR("Quanta Computer Inc."); +MODULE_DESCRIPTION("Quanta Embedded Controller I2C Keyboard Driver"); +MODULE_ALIAS("platform:qci-gkbd"); +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/input/qci_keyboard.h b/include/linux/input/qci_keyboard.h new file mode 100644 index 0000000..b32b7fc --- /dev/null +++ b/include/linux/input/qci_keyboard.h @@ -0,0 +1,25 @@ +/* Copyright (c) 2010, Code Aurora Forum. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef __QCI_KEYBOARD_H +#define __QCI_KEYBOARD_H + +struct qci_keyboard_platform_data { + unsigned long irq_trigger_type; +}; + +#endif