Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * DaVinci Key Scan Driver for TI platforms
0004  *
0005  * Copyright (C) 2009 Texas Instruments, Inc
0006  *
0007  * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
0008  *
0009  * Initial Code: Sandeep Paulraj <s-paulraj@ti.com>
0010  */
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/types.h>
0015 #include <linux/input.h>
0016 #include <linux/kernel.h>
0017 #include <linux/delay.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/errno.h>
0020 #include <linux/slab.h>
0021 
0022 #include <linux/platform_data/keyscan-davinci.h>
0023 
0024 /* Key scan registers */
0025 #define DAVINCI_KEYSCAN_KEYCTRL     0x0000
0026 #define DAVINCI_KEYSCAN_INTENA      0x0004
0027 #define DAVINCI_KEYSCAN_INTFLAG     0x0008
0028 #define DAVINCI_KEYSCAN_INTCLR      0x000c
0029 #define DAVINCI_KEYSCAN_STRBWIDTH   0x0010
0030 #define DAVINCI_KEYSCAN_INTERVAL    0x0014
0031 #define DAVINCI_KEYSCAN_CONTTIME    0x0018
0032 #define DAVINCI_KEYSCAN_CURRENTST   0x001c
0033 #define DAVINCI_KEYSCAN_PREVSTATE   0x0020
0034 #define DAVINCI_KEYSCAN_EMUCTRL     0x0024
0035 #define DAVINCI_KEYSCAN_IODFTCTRL   0x002c
0036 
0037 /* Key Control Register (KEYCTRL) */
0038 #define DAVINCI_KEYSCAN_KEYEN       0x00000001
0039 #define DAVINCI_KEYSCAN_PREVMODE    0x00000002
0040 #define DAVINCI_KEYSCAN_CHATOFF     0x00000004
0041 #define DAVINCI_KEYSCAN_AUTODET     0x00000008
0042 #define DAVINCI_KEYSCAN_SCANMODE    0x00000010
0043 #define DAVINCI_KEYSCAN_OUTTYPE     0x00000020
0044 
0045 /* Masks for the interrupts */
0046 #define DAVINCI_KEYSCAN_INT_CONT    0x00000008
0047 #define DAVINCI_KEYSCAN_INT_OFF     0x00000004
0048 #define DAVINCI_KEYSCAN_INT_ON      0x00000002
0049 #define DAVINCI_KEYSCAN_INT_CHANGE  0x00000001
0050 #define DAVINCI_KEYSCAN_INT_ALL     0x0000000f
0051 
0052 struct davinci_ks {
0053     struct input_dev        *input;
0054     struct davinci_ks_platform_data *pdata;
0055     int             irq;
0056     void __iomem            *base;
0057     resource_size_t         pbase;
0058     size_t              base_size;
0059     unsigned short          keymap[];
0060 };
0061 
0062 /* Initializing the kp Module */
0063 static int __init davinci_ks_initialize(struct davinci_ks *davinci_ks)
0064 {
0065     struct device *dev = &davinci_ks->input->dev;
0066     struct davinci_ks_platform_data *pdata = davinci_ks->pdata;
0067     u32 matrix_ctrl;
0068 
0069     /* Enable all interrupts */
0070     __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
0071              davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
0072 
0073     /* Clear interrupts if any */
0074     __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
0075              davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
0076 
0077     /* Setup the scan period = strobe + interval */
0078     __raw_writel(pdata->strobe,
0079              davinci_ks->base + DAVINCI_KEYSCAN_STRBWIDTH);
0080     __raw_writel(pdata->interval,
0081              davinci_ks->base + DAVINCI_KEYSCAN_INTERVAL);
0082     __raw_writel(0x01,
0083              davinci_ks->base + DAVINCI_KEYSCAN_CONTTIME);
0084 
0085     /* Define matrix type */
0086     switch (pdata->matrix_type) {
0087     case DAVINCI_KEYSCAN_MATRIX_4X4:
0088         matrix_ctrl = 0;
0089         break;
0090     case DAVINCI_KEYSCAN_MATRIX_5X3:
0091         matrix_ctrl = (1 << 6);
0092         break;
0093     default:
0094         dev_err(dev->parent, "wrong matrix type\n");
0095         return -EINVAL;
0096     }
0097 
0098     /* Enable key scan module and set matrix type */
0099     __raw_writel(DAVINCI_KEYSCAN_AUTODET | DAVINCI_KEYSCAN_KEYEN |
0100              matrix_ctrl, davinci_ks->base + DAVINCI_KEYSCAN_KEYCTRL);
0101 
0102     return 0;
0103 }
0104 
0105 static irqreturn_t davinci_ks_interrupt(int irq, void *dev_id)
0106 {
0107     struct davinci_ks *davinci_ks = dev_id;
0108     struct device *dev = &davinci_ks->input->dev;
0109     unsigned short *keymap = davinci_ks->keymap;
0110     int keymapsize = davinci_ks->pdata->keymapsize;
0111     u32 prev_status, new_status, changed;
0112     bool release;
0113     int keycode = KEY_UNKNOWN;
0114     int i;
0115 
0116     /* Disable interrupt */
0117     __raw_writel(0x0, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
0118 
0119     /* Reading previous and new status of the key scan */
0120     prev_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_PREVSTATE);
0121     new_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_CURRENTST);
0122 
0123     changed = prev_status ^ new_status;
0124 
0125     if (changed) {
0126         /*
0127          * It goes through all bits in 'changed' to ensure
0128          * that no key changes are being missed
0129          */
0130         for (i = 0 ; i < keymapsize; i++) {
0131             if ((changed>>i) & 0x1) {
0132                 keycode = keymap[i];
0133                 release = (new_status >> i) & 0x1;
0134                 dev_dbg(dev->parent, "key %d %s\n", keycode,
0135                     release ? "released" : "pressed");
0136                 input_report_key(davinci_ks->input, keycode,
0137                          !release);
0138                 input_sync(davinci_ks->input);
0139             }
0140         }
0141         /* Clearing interrupt */
0142         __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
0143                  davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
0144     }
0145 
0146     /* Enable interrupts */
0147     __raw_writel(0x1, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
0148 
0149     return IRQ_HANDLED;
0150 }
0151 
0152 static int __init davinci_ks_probe(struct platform_device *pdev)
0153 {
0154     struct davinci_ks *davinci_ks;
0155     struct input_dev *key_dev;
0156     struct resource *res, *mem;
0157     struct device *dev = &pdev->dev;
0158     struct davinci_ks_platform_data *pdata = dev_get_platdata(dev);
0159     int error, i;
0160 
0161     if (pdata->device_enable) {
0162         error = pdata->device_enable(dev);
0163         if (error < 0) {
0164             dev_dbg(dev, "device enable function failed\n");
0165             return error;
0166         }
0167     }
0168 
0169     if (!pdata->keymap) {
0170         dev_dbg(dev, "no keymap from pdata\n");
0171         return -EINVAL;
0172     }
0173 
0174     davinci_ks = kzalloc(sizeof(struct davinci_ks) +
0175         sizeof(unsigned short) * pdata->keymapsize, GFP_KERNEL);
0176     if (!davinci_ks) {
0177         dev_dbg(dev, "could not allocate memory for private data\n");
0178         return -ENOMEM;
0179     }
0180 
0181     memcpy(davinci_ks->keymap, pdata->keymap,
0182         sizeof(unsigned short) * pdata->keymapsize);
0183 
0184     key_dev = input_allocate_device();
0185     if (!key_dev) {
0186         dev_dbg(dev, "could not allocate input device\n");
0187         error = -ENOMEM;
0188         goto fail1;
0189     }
0190 
0191     davinci_ks->input = key_dev;
0192 
0193     davinci_ks->irq = platform_get_irq(pdev, 0);
0194     if (davinci_ks->irq < 0) {
0195         error = davinci_ks->irq;
0196         goto fail2;
0197     }
0198 
0199     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0200     if (!res) {
0201         dev_err(dev, "no mem resource\n");
0202         error = -EINVAL;
0203         goto fail2;
0204     }
0205 
0206     davinci_ks->pbase = res->start;
0207     davinci_ks->base_size = resource_size(res);
0208 
0209     mem = request_mem_region(davinci_ks->pbase, davinci_ks->base_size,
0210                  pdev->name);
0211     if (!mem) {
0212         dev_err(dev, "key scan registers at %08x are not free\n",
0213             davinci_ks->pbase);
0214         error = -EBUSY;
0215         goto fail2;
0216     }
0217 
0218     davinci_ks->base = ioremap(davinci_ks->pbase, davinci_ks->base_size);
0219     if (!davinci_ks->base) {
0220         dev_err(dev, "can't ioremap MEM resource.\n");
0221         error = -ENOMEM;
0222         goto fail3;
0223     }
0224 
0225     /* Enable auto repeat feature of Linux input subsystem */
0226     if (pdata->rep)
0227         __set_bit(EV_REP, key_dev->evbit);
0228 
0229     /* Setup input device */
0230     __set_bit(EV_KEY, key_dev->evbit);
0231 
0232     /* Setup the platform data */
0233     davinci_ks->pdata = pdata;
0234 
0235     for (i = 0; i < davinci_ks->pdata->keymapsize; i++)
0236         __set_bit(davinci_ks->pdata->keymap[i], key_dev->keybit);
0237 
0238     key_dev->name = "davinci_keyscan";
0239     key_dev->phys = "davinci_keyscan/input0";
0240     key_dev->dev.parent = dev;
0241     key_dev->id.bustype = BUS_HOST;
0242     key_dev->id.vendor = 0x0001;
0243     key_dev->id.product = 0x0001;
0244     key_dev->id.version = 0x0001;
0245     key_dev->keycode = davinci_ks->keymap;
0246     key_dev->keycodesize = sizeof(davinci_ks->keymap[0]);
0247     key_dev->keycodemax = davinci_ks->pdata->keymapsize;
0248 
0249     error = input_register_device(davinci_ks->input);
0250     if (error < 0) {
0251         dev_err(dev, "unable to register davinci key scan device\n");
0252         goto fail4;
0253     }
0254 
0255     error = request_irq(davinci_ks->irq, davinci_ks_interrupt,
0256               0, pdev->name, davinci_ks);
0257     if (error < 0) {
0258         dev_err(dev, "unable to register davinci key scan interrupt\n");
0259         goto fail5;
0260     }
0261 
0262     error = davinci_ks_initialize(davinci_ks);
0263     if (error < 0) {
0264         dev_err(dev, "unable to initialize davinci key scan device\n");
0265         goto fail6;
0266     }
0267 
0268     platform_set_drvdata(pdev, davinci_ks);
0269     return 0;
0270 
0271 fail6:
0272     free_irq(davinci_ks->irq, davinci_ks);
0273 fail5:
0274     input_unregister_device(davinci_ks->input);
0275     key_dev = NULL;
0276 fail4:
0277     iounmap(davinci_ks->base);
0278 fail3:
0279     release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
0280 fail2:
0281     input_free_device(key_dev);
0282 fail1:
0283     kfree(davinci_ks);
0284 
0285     return error;
0286 }
0287 
0288 static int davinci_ks_remove(struct platform_device *pdev)
0289 {
0290     struct davinci_ks *davinci_ks = platform_get_drvdata(pdev);
0291 
0292     free_irq(davinci_ks->irq, davinci_ks);
0293 
0294     input_unregister_device(davinci_ks->input);
0295 
0296     iounmap(davinci_ks->base);
0297     release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
0298 
0299     kfree(davinci_ks);
0300 
0301     return 0;
0302 }
0303 
0304 static struct platform_driver davinci_ks_driver = {
0305     .driver = {
0306         .name = "davinci_keyscan",
0307     },
0308     .remove = davinci_ks_remove,
0309 };
0310 
0311 module_platform_driver_probe(davinci_ks_driver, davinci_ks_probe);
0312 
0313 MODULE_AUTHOR("Miguel Aguilar");
0314 MODULE_DESCRIPTION("Texas Instruments DaVinci Key Scan Driver");
0315 MODULE_LICENSE("GPL");