From patchwork Sun May 12 04:23:00 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Tang X-Patchwork-Id: 2555171 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) by patchwork1.kernel.org (Postfix) with ESMTP id 675103FC5A for ; Sun, 12 May 2013 04:27:01 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UbNqU-00032A-KM; Sun, 12 May 2013 04:25:17 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UbNps-0008QP-UO; Sun, 12 May 2013 04:24:36 +0000 Received: from mail-da0-x229.google.com ([2607:f8b0:400e:c00::229]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UbNpf-0008N8-G0 for linux-arm-kernel@lists.infradead.org; Sun, 12 May 2013 04:24:27 +0000 Received: by mail-da0-f41.google.com with SMTP id y19so2984983dan.28 for ; Sat, 11 May 2013 21:24:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=E/6jWY3nD1wGRplK0UwTEHySLMf8kKVxRFO1+J6hO/g=; b=wYUoDjHhvrzaxPNVLcyKXm0NtnpLw3wq0bn5foOB5uHwTRAHEyrDc7NZSThbrON8W2 zMgF1ZlNfLwDtgPaIqD9kck6vXCMwQ96cpvBBDPTaMlOt9t2HgVaudPfflxxTe3RAyZI RVBKqNQkeMpXAYAtNnNiw2bRvoMLqkoTGmv1bKt20oR7iYbHbOzoICMBJI6k3nZz1apQ wvNQITg6SBPBOzEJDaTYKqp1Q93W5J4prohkzXfoywOH/8fk6/hESBosTa5my1xTUMUu 0YvDrpH7Jsz3ractrskWgWtSOO51ZwC3B2VFA3Os8mFUNfIKXb+ZiXZMlT7nNcswKf+1 AtsQ== X-Received: by 10.68.106.229 with SMTP id gx5mr13244142pbb.95.1368332641929; Sat, 11 May 2013 21:24:01 -0700 (PDT) Received: from tangrs.lan (110-175-69-66.static.tpgi.com.au. [110.175.69.66]) by mx.google.com with ESMTPSA id 10sm8737984pbm.0.2013.05.11.21.23.53 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 11 May 2013 21:23:58 -0700 (PDT) From: Daniel Tang To: linux-arm-kernel@lists.infradead.org, linux@arm.linux.org.uk Subject: [RFC PATCHv3 5/6] input: Add TI-Nspire keypad driver Date: Sun, 12 May 2013 14:23:00 +1000 Message-Id: <1368332581-94691-6-git-send-email-dt.tangr@gmail.com> X-Mailer: git-send-email 1.8.1.3 In-Reply-To: <1368332581-94691-1-git-send-email-dt.tangr@gmail.com> References: <1368332581-94691-1-git-send-email-dt.tangr@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130512_002423_844076_54C13CF3 X-CRM114-Status: GOOD ( 24.63 ) X-Spam-Score: -2.0 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (dt.tangr[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature Cc: Arnd Bergmann , Linus Walleij , linux-kernel@vger.kernel.org, "fabian@ritter-vogt.de Vogt" , Daniel Tang , Lionel Debroux X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Signed-off-by: Daniel Tang --- drivers/input/keyboard/Kconfig | 10 ++ drivers/input/keyboard/Makefile | 1 + drivers/input/keyboard/nspire-keypad.c | 315 +++++++++++++++++++++++++++++++++ 3 files changed, 326 insertions(+) create mode 100644 drivers/input/keyboard/nspire-keypad.c diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig index 62a2c0e..bdbda87 100644 --- a/drivers/input/keyboard/Kconfig +++ b/drivers/input/keyboard/Kconfig @@ -418,6 +418,16 @@ config KEYBOARD_NOMADIK To compile this driver as a module, choose M here: the module will be called nmk-ske-keypad. +config KEYBOARD_NSPIRE + tristate "TI-NSPIRE builtin keyboard" + depends on ARCH_NSPIRE + select INPUT_MATRIXKMAP + help + Say Y here if you want to use the builtin keypad on the TI-NSPIRE. + + To compile this driver as a module, choose M here: the + module will be called nspire-keypad. + config KEYBOARD_TEGRA tristate "NVIDIA Tegra internal matrix keyboard controller support" depends on ARCH_TEGRA && OF diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile index 0c43e8c..a699b61 100644 --- a/drivers/input/keyboard/Makefile +++ b/drivers/input/keyboard/Makefile @@ -36,6 +36,7 @@ obj-$(CONFIG_KEYBOARD_MCS) += mcs_touchkey.o obj-$(CONFIG_KEYBOARD_MPR121) += mpr121_touchkey.o obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o obj-$(CONFIG_KEYBOARD_NOMADIK) += nomadik-ske-keypad.o +obj-$(CONFIG_KEYBOARD_NSPIRE) += nspire-keypad.o obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o obj-$(CONFIG_KEYBOARD_OMAP4) += omap4-keypad.o obj-$(CONFIG_KEYBOARD_OPENCORES) += opencores-kbd.o diff --git a/drivers/input/keyboard/nspire-keypad.c b/drivers/input/keyboard/nspire-keypad.c new file mode 100644 index 0000000..e863f14 --- /dev/null +++ b/drivers/input/keyboard/nspire-keypad.c @@ -0,0 +1,315 @@ +/* + * linux/drivers/input/keyboard/nspire-keypad.c + * + * Copyright (C) 2013 Daniel Tang + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define KEYPAD_SCAN_MODE 0x00 +#define KEYPAD_CNTL 0x04 +#define KEYPAD_INT 0x08 +#define KEYPAD_INTMSK 0x0C + +#define KEYPAD_DATA 0x10 +#define KEYPAD_GPIO 0x30 + +#define KEYPAD_UNKNOWN_INT 0x40 +#define KEYPAD_UNKNOWN_INT_STS 0x44 + +#define KEYPAD_BITMASK_COLS 11 +#define KEYPAD_BITMASK_ROWS 8 + +struct nspire_keypad { + spinlock_t lock; + + void __iomem *reg_base; + int irq; + u32 int_mask; + + struct input_dev *input; + struct clk *clk; + + struct matrix_keymap_data *keymap; + int row_shift; + + /* Maximum delay estimated assuming 33MHz APB */ + u32 scan_interval; /* In microseconds (~2000us max) */ + u32 row_delay; /* In microseconds (~500us max) */ + + bool active_low; +}; + +static inline void nspire_report_state(struct nspire_keypad *keypad, + int row, int col, unsigned int state) +{ + int code = MATRIX_SCAN_CODE(row, col, keypad->row_shift); + unsigned short *keymap = keypad->input->keycode; + + state = keypad->active_low ? !state : !!state; + input_report_key(keypad->input, keymap[code], state); +} + +static irqreturn_t nspire_keypad_irq(int irq, void *dev_id) +{ + struct nspire_keypad *keypad = dev_id; + u32 int_sts; + u16 state[8]; + int row, col; + + int_sts = readl(keypad->reg_base + KEYPAD_INT) & keypad->int_mask; + + if (!int_sts) + return IRQ_NONE; + + spin_lock(&keypad->lock); + + memcpy_fromio(state, keypad->reg_base + KEYPAD_DATA, sizeof(state)); + + for (row = 0; row < KEYPAD_BITMASK_ROWS; row++) { + u16 bits = state[row]; + for (col = 0; col < KEYPAD_BITMASK_COLS; col++) + nspire_report_state(keypad, row, col, bits & (1<input); + writel(0x3, keypad->reg_base + KEYPAD_INT); + + spin_unlock(&keypad->lock); + + return IRQ_HANDLED; +} + +static int nspire_keypad_chip_init(struct nspire_keypad *keypad) +{ + unsigned long val = 0, cycles_per_us, delay_cycles, row_delay_cycles; + + cycles_per_us = (clk_get_rate(keypad->clk) / 1000000); + if (cycles_per_us == 0) + cycles_per_us = 1; + + delay_cycles = cycles_per_us * keypad->scan_interval; + WARN_ON(delay_cycles >= (1<<16)); /* Overflow */ + delay_cycles &= 0xffff; + + row_delay_cycles = cycles_per_us * keypad->row_delay; + WARN_ON(row_delay_cycles >= (1<<14)); /* Overflow */ + row_delay_cycles &= 0x3fff; + + + val |= (3<<0); /* Set scan mode to 3 (continuous scan) */ + val |= (row_delay_cycles<<2); /* Delay between scanning each row */ + val |= (delay_cycles<<16); /* Delay between scans */ + writel(val, keypad->reg_base + KEYPAD_SCAN_MODE); + + val = (KEYPAD_BITMASK_ROWS & 0xff) | (KEYPAD_BITMASK_COLS & 0xff)<<8; + writel(val, keypad->reg_base + KEYPAD_CNTL); + + /* Enable interrupts */ + keypad->int_mask = (1<<1); + writel(keypad->int_mask, keypad->reg_base + 0xc); + + /* Disable GPIO interrupts to prevent hanging on touchpad */ + /* Possibly used to detect touchpad events */ + writel(0, keypad->reg_base + KEYPAD_UNKNOWN_INT); + /* Acknowledge existing interrupts */ + writel(~0, keypad->reg_base + KEYPAD_UNKNOWN_INT_STS); + + return 0; +} + +static int nspire_keypad_probe(struct platform_device *pdev) +{ + const struct device_node *of_node = pdev->dev.of_node; + struct nspire_keypad *keypad; + struct input_dev *input; + struct resource *res; + struct clk *clk; + int irq, error; + + irq = platform_get_irq(pdev, 0); + if (irq < 0) { + dev_err(&pdev->dev, "failed to get keypad irq\n"); + return -EINVAL; + } + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!res) { + dev_err(&pdev->dev, "missing platform resources\n"); + return -EINVAL; + } + + clk = clk_get(&pdev->dev, NULL); + if (IS_ERR(clk)) { + dev_err(&pdev->dev, "unable to get clock\n"); + return -EINVAL; + } + + clk_prepare(clk); + error = clk_enable(clk); + if (error) + goto err_put_clk; + + keypad = kzalloc(sizeof(struct nspire_keypad), GFP_KERNEL); + input = input_allocate_device(); + if (!keypad || !input) { + dev_err(&pdev->dev, "failed to allocate keypad memory\n"); + error = -ENOMEM; + goto err_free_mem; + } + + error = of_property_read_u32(of_node, "scan-interval", + &keypad->scan_interval); + if (error) { + dev_err(&pdev->dev, "failed to get scan-interval\n"); + goto err_free_mem; + } + + error = of_property_read_u32(of_node, "row-delay", + &keypad->row_delay); + if (error) { + dev_err(&pdev->dev, "failed to get row-delay\n"); + goto err_free_mem; + } + + keypad->active_low = of_property_read_bool(of_node, + "active-low"); + + keypad->keymap = NULL; + + keypad->row_shift = get_count_order(KEYPAD_BITMASK_COLS); + keypad->irq = irq; + keypad->clk = clk; + keypad->input = input; + spin_lock_init(&keypad->lock); + + if (!request_mem_region(res->start, resource_size(res), pdev->name)) { + dev_err(&pdev->dev, "failed to request I/O memory\n"); + error = -EBUSY; + goto err_free_mem; + } + + keypad->reg_base = ioremap(res->start, resource_size(res)); + if (!keypad->reg_base) { + dev_err(&pdev->dev, "failed to remap I/O memory\n"); + error = -ENXIO; + goto err_free_mem_region; + } + + input->id.bustype = BUS_HOST; + input->name = "nspire-keypad"; + input->dev.parent = &pdev->dev; + + set_bit(EV_KEY, input->evbit); + set_bit(EV_REP, input->evbit); + + error = matrix_keypad_build_keymap(keypad->keymap, "keymap", + KEYPAD_BITMASK_ROWS, KEYPAD_BITMASK_COLS, NULL, input); + if (error) { + dev_err(&pdev->dev, "building keymap failed\n"); + goto err_iounmap; + } + + error = nspire_keypad_chip_init(keypad); + if (error) { + dev_err(&pdev->dev, "unable to init keypad hardware\n"); + goto err_iounmap; + } + + error = request_irq(keypad->irq, nspire_keypad_irq, 0, + "nspire_keypad", keypad); + if (error) { + dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq); + goto err_iounmap; + } + + error = input_register_device(input); + if (error) { + dev_err(&pdev->dev, + "unable to register input device: %d\n", error); + goto err_free_irq; + } + + platform_set_drvdata(pdev, keypad); + + dev_info(&pdev->dev, "TI-NSPIRE keypad at %#08x (" + "scan_interval=%uus,row_delay=%uus" + "%s)\n", res->start, + keypad->row_delay, + keypad->scan_interval, + keypad->active_low ? ",active_low" : ""); + + return 0; + +err_free_irq: + free_irq(keypad->irq, keypad); +err_iounmap: + iounmap(keypad->reg_base); +err_free_mem_region: + release_mem_region(res->start, resource_size(res)); +err_free_mem: + input_free_device(input); + kfree(keypad); + + clk_disable(clk); +err_put_clk: + clk_unprepare(clk); + clk_put(clk); + return error; +} + +static int nspire_keypad_remove(struct platform_device *pdev) +{ + struct nspire_keypad *keypad = platform_get_drvdata(pdev); + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + + free_irq(keypad->irq, keypad); + + input_unregister_device(keypad->input); + + iounmap(keypad->reg_base); + release_mem_region(res->start, resource_size(res)); + kfree(keypad); + + clk_disable(keypad->clk); + clk_unprepare(keypad->clk); + clk_put(keypad->clk); + + return 0; +} +#ifdef CONFIG_OF +static const struct of_device_id nspire_keypad_dt_match[] = { + { .compatible = "nspire-keypad" }, + { }, +}; +MODULE_DEVICE_TABLE(of, nspire_keypad_dt_match); +#endif + +static struct platform_driver nspire_keypad_driver = { + .driver = { + .name = "nspire-keypad", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(nspire_keypad_dt_match), + }, + .remove = nspire_keypad_remove, + .probe = nspire_keypad_probe, +}; + +module_platform_driver(nspire_keypad_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("TI-NSPIRE Keypad Driver");