Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * SuperH KEYSC Keypad Driver
0004  *
0005  * Copyright (C) 2008 Magnus Damm
0006  *
0007  * Based on gpio_keys.c, Copyright 2005 Phil Blundell
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/irq.h>
0014 #include <linux/delay.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/input.h>
0017 #include <linux/input/sh_keysc.h>
0018 #include <linux/bitmap.h>
0019 #include <linux/pm_runtime.h>
0020 #include <linux/io.h>
0021 #include <linux/slab.h>
0022 
0023 static const struct {
0024     unsigned char kymd, keyout, keyin;
0025 } sh_keysc_mode[] = {
0026     [SH_KEYSC_MODE_1] = { 0, 6, 5 },
0027     [SH_KEYSC_MODE_2] = { 1, 5, 6 },
0028     [SH_KEYSC_MODE_3] = { 2, 4, 7 },
0029     [SH_KEYSC_MODE_4] = { 3, 6, 6 },
0030     [SH_KEYSC_MODE_5] = { 4, 6, 7 },
0031     [SH_KEYSC_MODE_6] = { 5, 8, 8 },
0032 };
0033 
0034 struct sh_keysc_priv {
0035     void __iomem *iomem_base;
0036     DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);
0037     struct input_dev *input;
0038     struct sh_keysc_info pdata;
0039 };
0040 
0041 #define KYCR1 0
0042 #define KYCR2 1
0043 #define KYINDR 2
0044 #define KYOUTDR 3
0045 
0046 #define KYCR2_IRQ_LEVEL    0x10
0047 #define KYCR2_IRQ_DISABLED 0x00
0048 
0049 static unsigned long sh_keysc_read(struct sh_keysc_priv *p, int reg_nr)
0050 {
0051     return ioread16(p->iomem_base + (reg_nr << 2));
0052 }
0053 
0054 static void sh_keysc_write(struct sh_keysc_priv *p, int reg_nr,
0055                unsigned long value)
0056 {
0057     iowrite16(value, p->iomem_base + (reg_nr << 2));
0058 }
0059 
0060 static void sh_keysc_level_mode(struct sh_keysc_priv *p,
0061                 unsigned long keys_set)
0062 {
0063     struct sh_keysc_info *pdata = &p->pdata;
0064 
0065     sh_keysc_write(p, KYOUTDR, 0);
0066     sh_keysc_write(p, KYCR2, KYCR2_IRQ_LEVEL | (keys_set << 8));
0067 
0068     if (pdata->kycr2_delay)
0069         udelay(pdata->kycr2_delay);
0070 }
0071 
0072 static void sh_keysc_map_dbg(struct device *dev, unsigned long *map,
0073                  const char *str)
0074 {
0075     int k;
0076 
0077     for (k = 0; k < BITS_TO_LONGS(SH_KEYSC_MAXKEYS); k++)
0078         dev_dbg(dev, "%s[%d] 0x%lx\n", str, k, map[k]);
0079 }
0080 
0081 static irqreturn_t sh_keysc_isr(int irq, void *dev_id)
0082 {
0083     struct platform_device *pdev = dev_id;
0084     struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
0085     struct sh_keysc_info *pdata = &priv->pdata;
0086     int keyout_nr = sh_keysc_mode[pdata->mode].keyout;
0087     int keyin_nr = sh_keysc_mode[pdata->mode].keyin;
0088     DECLARE_BITMAP(keys, SH_KEYSC_MAXKEYS);
0089     DECLARE_BITMAP(keys0, SH_KEYSC_MAXKEYS);
0090     DECLARE_BITMAP(keys1, SH_KEYSC_MAXKEYS);
0091     unsigned char keyin_set, tmp;
0092     int i, k, n;
0093 
0094     dev_dbg(&pdev->dev, "isr!\n");
0095 
0096     bitmap_fill(keys1, SH_KEYSC_MAXKEYS);
0097     bitmap_zero(keys0, SH_KEYSC_MAXKEYS);
0098 
0099     do {
0100         bitmap_zero(keys, SH_KEYSC_MAXKEYS);
0101         keyin_set = 0;
0102 
0103         sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
0104 
0105         for (i = 0; i < keyout_nr; i++) {
0106             n = keyin_nr * i;
0107 
0108             /* drive one KEYOUT pin low, read KEYIN pins */
0109             sh_keysc_write(priv, KYOUTDR, 0xffff ^ (3 << (i * 2)));
0110             udelay(pdata->delay);
0111             tmp = sh_keysc_read(priv, KYINDR);
0112 
0113             /* set bit if key press has been detected */
0114             for (k = 0; k < keyin_nr; k++) {
0115                 if (tmp & (1 << k))
0116                     __set_bit(n + k, keys);
0117             }
0118 
0119             /* keep track of which KEYIN bits that have been set */
0120             keyin_set |= tmp ^ ((1 << keyin_nr) - 1);
0121         }
0122 
0123         sh_keysc_level_mode(priv, keyin_set);
0124 
0125         bitmap_complement(keys, keys, SH_KEYSC_MAXKEYS);
0126         bitmap_and(keys1, keys1, keys, SH_KEYSC_MAXKEYS);
0127         bitmap_or(keys0, keys0, keys, SH_KEYSC_MAXKEYS);
0128 
0129         sh_keysc_map_dbg(&pdev->dev, keys, "keys");
0130 
0131     } while (sh_keysc_read(priv, KYCR2) & 0x01);
0132 
0133     sh_keysc_map_dbg(&pdev->dev, priv->last_keys, "last_keys");
0134     sh_keysc_map_dbg(&pdev->dev, keys0, "keys0");
0135     sh_keysc_map_dbg(&pdev->dev, keys1, "keys1");
0136 
0137     for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {
0138         k = pdata->keycodes[i];
0139         if (!k)
0140             continue;
0141 
0142         if (test_bit(i, keys0) == test_bit(i, priv->last_keys))
0143             continue;
0144 
0145         if (test_bit(i, keys1) || test_bit(i, keys0)) {
0146             input_event(priv->input, EV_KEY, k, 1);
0147             __set_bit(i, priv->last_keys);
0148         }
0149 
0150         if (!test_bit(i, keys1)) {
0151             input_event(priv->input, EV_KEY, k, 0);
0152             __clear_bit(i, priv->last_keys);
0153         }
0154 
0155     }
0156     input_sync(priv->input);
0157 
0158     return IRQ_HANDLED;
0159 }
0160 
0161 static int sh_keysc_probe(struct platform_device *pdev)
0162 {
0163     struct sh_keysc_priv *priv;
0164     struct sh_keysc_info *pdata;
0165     struct resource *res;
0166     struct input_dev *input;
0167     int i;
0168     int irq, error;
0169 
0170     if (!dev_get_platdata(&pdev->dev)) {
0171         dev_err(&pdev->dev, "no platform data defined\n");
0172         error = -EINVAL;
0173         goto err0;
0174     }
0175 
0176     error = -ENXIO;
0177     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0178     if (res == NULL) {
0179         dev_err(&pdev->dev, "failed to get I/O memory\n");
0180         goto err0;
0181     }
0182 
0183     irq = platform_get_irq(pdev, 0);
0184     if (irq < 0)
0185         goto err0;
0186 
0187     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0188     if (priv == NULL) {
0189         dev_err(&pdev->dev, "failed to allocate driver data\n");
0190         error = -ENOMEM;
0191         goto err0;
0192     }
0193 
0194     platform_set_drvdata(pdev, priv);
0195     memcpy(&priv->pdata, dev_get_platdata(&pdev->dev), sizeof(priv->pdata));
0196     pdata = &priv->pdata;
0197 
0198     priv->iomem_base = ioremap(res->start, resource_size(res));
0199     if (priv->iomem_base == NULL) {
0200         dev_err(&pdev->dev, "failed to remap I/O memory\n");
0201         error = -ENXIO;
0202         goto err1;
0203     }
0204 
0205     priv->input = input_allocate_device();
0206     if (!priv->input) {
0207         dev_err(&pdev->dev, "failed to allocate input device\n");
0208         error = -ENOMEM;
0209         goto err2;
0210     }
0211 
0212     input = priv->input;
0213     input->evbit[0] = BIT_MASK(EV_KEY);
0214 
0215     input->name = pdev->name;
0216     input->phys = "sh-keysc-keys/input0";
0217     input->dev.parent = &pdev->dev;
0218 
0219     input->id.bustype = BUS_HOST;
0220     input->id.vendor = 0x0001;
0221     input->id.product = 0x0001;
0222     input->id.version = 0x0100;
0223 
0224     input->keycode = pdata->keycodes;
0225     input->keycodesize = sizeof(pdata->keycodes[0]);
0226     input->keycodemax = ARRAY_SIZE(pdata->keycodes);
0227 
0228     error = request_threaded_irq(irq, NULL, sh_keysc_isr, IRQF_ONESHOT,
0229                      dev_name(&pdev->dev), pdev);
0230     if (error) {
0231         dev_err(&pdev->dev, "failed to request IRQ\n");
0232         goto err3;
0233     }
0234 
0235     for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
0236         __set_bit(pdata->keycodes[i], input->keybit);
0237     __clear_bit(KEY_RESERVED, input->keybit);
0238 
0239     error = input_register_device(input);
0240     if (error) {
0241         dev_err(&pdev->dev, "failed to register input device\n");
0242         goto err4;
0243     }
0244 
0245     pm_runtime_enable(&pdev->dev);
0246     pm_runtime_get_sync(&pdev->dev);
0247 
0248     sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
0249                pdata->scan_timing);
0250     sh_keysc_level_mode(priv, 0);
0251 
0252     device_init_wakeup(&pdev->dev, 1);
0253 
0254     return 0;
0255 
0256  err4:
0257     free_irq(irq, pdev);
0258  err3:
0259     input_free_device(input);
0260  err2:
0261     iounmap(priv->iomem_base);
0262  err1:
0263     kfree(priv);
0264  err0:
0265     return error;
0266 }
0267 
0268 static int sh_keysc_remove(struct platform_device *pdev)
0269 {
0270     struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
0271 
0272     sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
0273 
0274     input_unregister_device(priv->input);
0275     free_irq(platform_get_irq(pdev, 0), pdev);
0276     iounmap(priv->iomem_base);
0277 
0278     pm_runtime_put_sync(&pdev->dev);
0279     pm_runtime_disable(&pdev->dev);
0280 
0281     kfree(priv);
0282 
0283     return 0;
0284 }
0285 
0286 #ifdef CONFIG_PM_SLEEP
0287 static int sh_keysc_suspend(struct device *dev)
0288 {
0289     struct platform_device *pdev = to_platform_device(dev);
0290     struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
0291     int irq = platform_get_irq(pdev, 0);
0292     unsigned short value;
0293 
0294     value = sh_keysc_read(priv, KYCR1);
0295 
0296     if (device_may_wakeup(dev)) {
0297         sh_keysc_write(priv, KYCR1, value | 0x80);
0298         enable_irq_wake(irq);
0299     } else {
0300         sh_keysc_write(priv, KYCR1, value & ~0x80);
0301         pm_runtime_put_sync(dev);
0302     }
0303 
0304     return 0;
0305 }
0306 
0307 static int sh_keysc_resume(struct device *dev)
0308 {
0309     struct platform_device *pdev = to_platform_device(dev);
0310     int irq = platform_get_irq(pdev, 0);
0311 
0312     if (device_may_wakeup(dev))
0313         disable_irq_wake(irq);
0314     else
0315         pm_runtime_get_sync(dev);
0316 
0317     return 0;
0318 }
0319 #endif
0320 
0321 static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops,
0322              sh_keysc_suspend, sh_keysc_resume);
0323 
0324 static struct platform_driver sh_keysc_device_driver = {
0325     .probe      = sh_keysc_probe,
0326     .remove     = sh_keysc_remove,
0327     .driver     = {
0328         .name   = "sh_keysc",
0329         .pm = &sh_keysc_dev_pm_ops,
0330     }
0331 };
0332 module_platform_driver(sh_keysc_device_driver);
0333 
0334 MODULE_AUTHOR("Magnus Damm");
0335 MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
0336 MODULE_LICENSE("GPL");