Path: blob/master/drivers/input/keyboard/sh_keysc.c
15111 views
/*1* SuperH KEYSC Keypad Driver2*3* Copyright (C) 2008 Magnus Damm4*5* Based on gpio_keys.c, Copyright 2005 Phil Blundell6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License version 2 as9* published by the Free Software Foundation.10*/1112#include <linux/kernel.h>13#include <linux/module.h>14#include <linux/init.h>15#include <linux/interrupt.h>16#include <linux/irq.h>17#include <linux/delay.h>18#include <linux/platform_device.h>19#include <linux/input.h>20#include <linux/input/sh_keysc.h>21#include <linux/bitmap.h>22#include <linux/pm_runtime.h>23#include <linux/io.h>24#include <linux/slab.h>2526static const struct {27unsigned char kymd, keyout, keyin;28} sh_keysc_mode[] = {29[SH_KEYSC_MODE_1] = { 0, 6, 5 },30[SH_KEYSC_MODE_2] = { 1, 5, 6 },31[SH_KEYSC_MODE_3] = { 2, 4, 7 },32[SH_KEYSC_MODE_4] = { 3, 6, 6 },33[SH_KEYSC_MODE_5] = { 4, 6, 7 },34[SH_KEYSC_MODE_6] = { 5, 8, 8 },35};3637struct sh_keysc_priv {38void __iomem *iomem_base;39DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);40struct input_dev *input;41struct sh_keysc_info pdata;42};4344#define KYCR1 045#define KYCR2 146#define KYINDR 247#define KYOUTDR 34849#define KYCR2_IRQ_LEVEL 0x1050#define KYCR2_IRQ_DISABLED 0x005152static unsigned long sh_keysc_read(struct sh_keysc_priv *p, int reg_nr)53{54return ioread16(p->iomem_base + (reg_nr << 2));55}5657static void sh_keysc_write(struct sh_keysc_priv *p, int reg_nr,58unsigned long value)59{60iowrite16(value, p->iomem_base + (reg_nr << 2));61}6263static void sh_keysc_level_mode(struct sh_keysc_priv *p,64unsigned long keys_set)65{66struct sh_keysc_info *pdata = &p->pdata;6768sh_keysc_write(p, KYOUTDR, 0);69sh_keysc_write(p, KYCR2, KYCR2_IRQ_LEVEL | (keys_set << 8));7071if (pdata->kycr2_delay)72udelay(pdata->kycr2_delay);73}7475static void sh_keysc_map_dbg(struct device *dev, unsigned long *map,76const char *str)77{78int k;7980for (k = 0; k < BITS_TO_LONGS(SH_KEYSC_MAXKEYS); k++)81dev_dbg(dev, "%s[%d] 0x%lx\n", str, k, map[k]);82}8384static irqreturn_t sh_keysc_isr(int irq, void *dev_id)85{86struct platform_device *pdev = dev_id;87struct sh_keysc_priv *priv = platform_get_drvdata(pdev);88struct sh_keysc_info *pdata = &priv->pdata;89int keyout_nr = sh_keysc_mode[pdata->mode].keyout;90int keyin_nr = sh_keysc_mode[pdata->mode].keyin;91DECLARE_BITMAP(keys, SH_KEYSC_MAXKEYS);92DECLARE_BITMAP(keys0, SH_KEYSC_MAXKEYS);93DECLARE_BITMAP(keys1, SH_KEYSC_MAXKEYS);94unsigned char keyin_set, tmp;95int i, k, n;9697dev_dbg(&pdev->dev, "isr!\n");9899bitmap_fill(keys1, SH_KEYSC_MAXKEYS);100bitmap_zero(keys0, SH_KEYSC_MAXKEYS);101102do {103bitmap_zero(keys, SH_KEYSC_MAXKEYS);104keyin_set = 0;105106sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);107108for (i = 0; i < keyout_nr; i++) {109n = keyin_nr * i;110111/* drive one KEYOUT pin low, read KEYIN pins */112sh_keysc_write(priv, KYOUTDR, 0xffff ^ (3 << (i * 2)));113udelay(pdata->delay);114tmp = sh_keysc_read(priv, KYINDR);115116/* set bit if key press has been detected */117for (k = 0; k < keyin_nr; k++) {118if (tmp & (1 << k))119__set_bit(n + k, keys);120}121122/* keep track of which KEYIN bits that have been set */123keyin_set |= tmp ^ ((1 << keyin_nr) - 1);124}125126sh_keysc_level_mode(priv, keyin_set);127128bitmap_complement(keys, keys, SH_KEYSC_MAXKEYS);129bitmap_and(keys1, keys1, keys, SH_KEYSC_MAXKEYS);130bitmap_or(keys0, keys0, keys, SH_KEYSC_MAXKEYS);131132sh_keysc_map_dbg(&pdev->dev, keys, "keys");133134} while (sh_keysc_read(priv, KYCR2) & 0x01);135136sh_keysc_map_dbg(&pdev->dev, priv->last_keys, "last_keys");137sh_keysc_map_dbg(&pdev->dev, keys0, "keys0");138sh_keysc_map_dbg(&pdev->dev, keys1, "keys1");139140for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {141k = pdata->keycodes[i];142if (!k)143continue;144145if (test_bit(i, keys0) == test_bit(i, priv->last_keys))146continue;147148if (test_bit(i, keys1) || test_bit(i, keys0)) {149input_event(priv->input, EV_KEY, k, 1);150__set_bit(i, priv->last_keys);151}152153if (!test_bit(i, keys1)) {154input_event(priv->input, EV_KEY, k, 0);155__clear_bit(i, priv->last_keys);156}157158}159input_sync(priv->input);160161return IRQ_HANDLED;162}163164static int __devinit sh_keysc_probe(struct platform_device *pdev)165{166struct sh_keysc_priv *priv;167struct sh_keysc_info *pdata;168struct resource *res;169struct input_dev *input;170int i;171int irq, error;172173if (!pdev->dev.platform_data) {174dev_err(&pdev->dev, "no platform data defined\n");175error = -EINVAL;176goto err0;177}178179error = -ENXIO;180res = platform_get_resource(pdev, IORESOURCE_MEM, 0);181if (res == NULL) {182dev_err(&pdev->dev, "failed to get I/O memory\n");183goto err0;184}185186irq = platform_get_irq(pdev, 0);187if (irq < 0) {188dev_err(&pdev->dev, "failed to get irq\n");189goto err0;190}191192priv = kzalloc(sizeof(*priv), GFP_KERNEL);193if (priv == NULL) {194dev_err(&pdev->dev, "failed to allocate driver data\n");195error = -ENOMEM;196goto err0;197}198199platform_set_drvdata(pdev, priv);200memcpy(&priv->pdata, pdev->dev.platform_data, sizeof(priv->pdata));201pdata = &priv->pdata;202203priv->iomem_base = ioremap_nocache(res->start, resource_size(res));204if (priv->iomem_base == NULL) {205dev_err(&pdev->dev, "failed to remap I/O memory\n");206error = -ENXIO;207goto err1;208}209210priv->input = input_allocate_device();211if (!priv->input) {212dev_err(&pdev->dev, "failed to allocate input device\n");213error = -ENOMEM;214goto err2;215}216217input = priv->input;218input->evbit[0] = BIT_MASK(EV_KEY);219220input->name = pdev->name;221input->phys = "sh-keysc-keys/input0";222input->dev.parent = &pdev->dev;223224input->id.bustype = BUS_HOST;225input->id.vendor = 0x0001;226input->id.product = 0x0001;227input->id.version = 0x0100;228229input->keycode = pdata->keycodes;230input->keycodesize = sizeof(pdata->keycodes[0]);231input->keycodemax = ARRAY_SIZE(pdata->keycodes);232233error = request_threaded_irq(irq, NULL, sh_keysc_isr, IRQF_ONESHOT,234dev_name(&pdev->dev), pdev);235if (error) {236dev_err(&pdev->dev, "failed to request IRQ\n");237goto err3;238}239240for (i = 0; i < SH_KEYSC_MAXKEYS; i++)241__set_bit(pdata->keycodes[i], input->keybit);242__clear_bit(KEY_RESERVED, input->keybit);243244error = input_register_device(input);245if (error) {246dev_err(&pdev->dev, "failed to register input device\n");247goto err4;248}249250pm_runtime_enable(&pdev->dev);251pm_runtime_get_sync(&pdev->dev);252253sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |254pdata->scan_timing);255sh_keysc_level_mode(priv, 0);256257device_init_wakeup(&pdev->dev, 1);258259return 0;260261err4:262free_irq(irq, pdev);263err3:264input_free_device(input);265err2:266iounmap(priv->iomem_base);267err1:268platform_set_drvdata(pdev, NULL);269kfree(priv);270err0:271return error;272}273274static int __devexit sh_keysc_remove(struct platform_device *pdev)275{276struct sh_keysc_priv *priv = platform_get_drvdata(pdev);277278sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);279280input_unregister_device(priv->input);281free_irq(platform_get_irq(pdev, 0), pdev);282iounmap(priv->iomem_base);283284pm_runtime_put_sync(&pdev->dev);285pm_runtime_disable(&pdev->dev);286287platform_set_drvdata(pdev, NULL);288kfree(priv);289290return 0;291}292293#if CONFIG_PM_SLEEP294static int sh_keysc_suspend(struct device *dev)295{296struct platform_device *pdev = to_platform_device(dev);297struct sh_keysc_priv *priv = platform_get_drvdata(pdev);298int irq = platform_get_irq(pdev, 0);299unsigned short value;300301value = sh_keysc_read(priv, KYCR1);302303if (device_may_wakeup(dev)) {304sh_keysc_write(priv, KYCR1, value | 0x80);305enable_irq_wake(irq);306} else {307sh_keysc_write(priv, KYCR1, value & ~0x80);308pm_runtime_put_sync(dev);309}310311return 0;312}313314static int sh_keysc_resume(struct device *dev)315{316struct platform_device *pdev = to_platform_device(dev);317int irq = platform_get_irq(pdev, 0);318319if (device_may_wakeup(dev))320disable_irq_wake(irq);321else322pm_runtime_get_sync(dev);323324return 0;325}326#endif327328static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops,329sh_keysc_suspend, sh_keysc_resume);330331static struct platform_driver sh_keysc_device_driver = {332.probe = sh_keysc_probe,333.remove = __devexit_p(sh_keysc_remove),334.driver = {335.name = "sh_keysc",336.pm = &sh_keysc_dev_pm_ops,337}338};339340static int __init sh_keysc_init(void)341{342return platform_driver_register(&sh_keysc_device_driver);343}344345static void __exit sh_keysc_exit(void)346{347platform_driver_unregister(&sh_keysc_device_driver);348}349350module_init(sh_keysc_init);351module_exit(sh_keysc_exit);352353MODULE_AUTHOR("Magnus Damm");354MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");355MODULE_LICENSE("GPL");356357358