Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/drivers/input/keyboard/pxa27x_keypad.c
0004  *
0005  * Driver for the pxa27x matrix keyboard controller.
0006  *
0007  * Created: Feb 22, 2007
0008  * Author:  Rodolfo Giometti <giometti@linux.it>
0009  *
0010  * Based on a previous implementations by Kevin O'Connor
0011  * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
0012  * on some suggestions by Nicolas Pitre <nico@fluxnic.net>.
0013  */
0014 
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/input.h>
0020 #include <linux/io.h>
0021 #include <linux/device.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/clk.h>
0024 #include <linux/err.h>
0025 #include <linux/input/matrix_keypad.h>
0026 #include <linux/slab.h>
0027 #include <linux/of.h>
0028 
0029 #include <linux/platform_data/keypad-pxa27x.h>
0030 /*
0031  * Keypad Controller registers
0032  */
0033 #define KPC             0x0000 /* Keypad Control register */
0034 #define KPDK            0x0008 /* Keypad Direct Key register */
0035 #define KPREC           0x0010 /* Keypad Rotary Encoder register */
0036 #define KPMK            0x0018 /* Keypad Matrix Key register */
0037 #define KPAS            0x0020 /* Keypad Automatic Scan register */
0038 
0039 /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
0040 #define KPASMKP0        0x0028
0041 #define KPASMKP1        0x0030
0042 #define KPASMKP2        0x0038
0043 #define KPASMKP3        0x0040
0044 #define KPKDI           0x0048
0045 
0046 /* bit definitions */
0047 #define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */
0048 #define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */
0049 #define KPC_DKN(n)  ((((n) - 1) & 0x7) << 6)  /* direct key number */
0050 
0051 #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
0052 #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
0053 #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
0054 #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
0055 
0056 #define KPC_MS(n)   (0x1 << (13 + (n))) /* Matrix scan line 'n' */
0057 #define KPC_MS_ALL      (0xff << 13)
0058 
0059 #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
0060 #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
0061 #define KPC_DK_DEB_SEL  (0x1 <<  9)  /* Direct Keypad Debounce Select */
0062 #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
0063 #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
0064 #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
0065 #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
0066 #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
0067 #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
0068 
0069 #define KPDK_DKP        (0x1 << 31)
0070 #define KPDK_DK(n)  ((n) & 0xff)
0071 
0072 #define KPREC_OF1       (0x1 << 31)
0073 #define kPREC_UF1       (0x1 << 30)
0074 #define KPREC_OF0       (0x1 << 15)
0075 #define KPREC_UF0       (0x1 << 14)
0076 
0077 #define KPREC_RECOUNT0(n)   ((n) & 0xff)
0078 #define KPREC_RECOUNT1(n)   (((n) >> 16) & 0xff)
0079 
0080 #define KPMK_MKP        (0x1 << 31)
0081 #define KPAS_SO         (0x1 << 31)
0082 #define KPASMKPx_SO     (0x1 << 31)
0083 
0084 #define KPAS_MUKP(n)    (((n) >> 26) & 0x1f)
0085 #define KPAS_RP(n)  (((n) >> 4) & 0xf)
0086 #define KPAS_CP(n)  ((n) & 0xf)
0087 
0088 #define KPASMKP_MKC_MASK    (0xff)
0089 
0090 #define keypad_readl(off)   __raw_readl(keypad->mmio_base + (off))
0091 #define keypad_writel(off, v)   __raw_writel((v), keypad->mmio_base + (off))
0092 
0093 #define MAX_MATRIX_KEY_NUM  (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
0094 #define MAX_KEYPAD_KEYS     (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
0095 
0096 struct pxa27x_keypad {
0097     const struct pxa27x_keypad_platform_data *pdata;
0098 
0099     struct clk *clk;
0100     struct input_dev *input_dev;
0101     void __iomem *mmio_base;
0102 
0103     int irq;
0104 
0105     unsigned short keycodes[MAX_KEYPAD_KEYS];
0106     int rotary_rel_code[2];
0107 
0108     unsigned int row_shift;
0109 
0110     /* state row bits of each column scan */
0111     uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
0112     uint32_t direct_key_state;
0113 
0114     unsigned int direct_key_mask;
0115 };
0116 
0117 #ifdef CONFIG_OF
0118 static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad,
0119                 struct pxa27x_keypad_platform_data *pdata)
0120 {
0121     struct input_dev *input_dev = keypad->input_dev;
0122     struct device *dev = input_dev->dev.parent;
0123     u32 rows, cols;
0124     int error;
0125 
0126     error = matrix_keypad_parse_properties(dev, &rows, &cols);
0127     if (error)
0128         return error;
0129 
0130     if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
0131         dev_err(dev, "rows or cols exceeds maximum value\n");
0132         return -EINVAL;
0133     }
0134 
0135     pdata->matrix_key_rows = rows;
0136     pdata->matrix_key_cols = cols;
0137 
0138     error = matrix_keypad_build_keymap(NULL, NULL,
0139                        pdata->matrix_key_rows,
0140                        pdata->matrix_key_cols,
0141                        keypad->keycodes, input_dev);
0142     if (error)
0143         return error;
0144 
0145     return 0;
0146 }
0147 
0148 static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad,
0149                 struct pxa27x_keypad_platform_data *pdata)
0150 {
0151     struct input_dev *input_dev = keypad->input_dev;
0152     struct device *dev = input_dev->dev.parent;
0153     struct device_node *np = dev->of_node;
0154     const __be16 *prop;
0155     unsigned short code;
0156     unsigned int proplen, size;
0157     int i;
0158     int error;
0159 
0160     error = of_property_read_u32(np, "marvell,direct-key-count",
0161                      &pdata->direct_key_num);
0162     if (error) {
0163         /*
0164          * If do not have marvel,direct-key-count defined,
0165          * it means direct key is not supported.
0166          */
0167         return error == -EINVAL ? 0 : error;
0168     }
0169 
0170     error = of_property_read_u32(np, "marvell,direct-key-mask",
0171                      &pdata->direct_key_mask);
0172     if (error) {
0173         if (error != -EINVAL)
0174             return error;
0175 
0176         /*
0177          * If marvell,direct-key-mask is not defined, driver will use
0178          * default value. Default value is set when configure the keypad.
0179          */
0180         pdata->direct_key_mask = 0;
0181     }
0182 
0183     pdata->direct_key_low_active = of_property_read_bool(np,
0184                     "marvell,direct-key-low-active");
0185 
0186     prop = of_get_property(np, "marvell,direct-key-map", &proplen);
0187     if (!prop)
0188         return -EINVAL;
0189 
0190     if (proplen % sizeof(u16))
0191         return -EINVAL;
0192 
0193     size = proplen / sizeof(u16);
0194 
0195     /* Only MAX_DIRECT_KEY_NUM is accepted.*/
0196     if (size > MAX_DIRECT_KEY_NUM)
0197         return -EINVAL;
0198 
0199     for (i = 0; i < size; i++) {
0200         code = be16_to_cpup(prop + i);
0201         keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
0202         __set_bit(code, input_dev->keybit);
0203     }
0204 
0205     return 0;
0206 }
0207 
0208 static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad,
0209                 struct pxa27x_keypad_platform_data *pdata)
0210 {
0211     const __be32 *prop;
0212     int i, relkey_ret;
0213     unsigned int code, proplen;
0214     const char *rotaryname[2] = {
0215             "marvell,rotary0", "marvell,rotary1"};
0216     const char relkeyname[] = {"marvell,rotary-rel-key"};
0217     struct input_dev *input_dev = keypad->input_dev;
0218     struct device *dev = input_dev->dev.parent;
0219     struct device_node *np = dev->of_node;
0220 
0221     relkey_ret = of_property_read_u32(np, relkeyname, &code);
0222     /* if can read correct rotary key-code, we do not need this. */
0223     if (relkey_ret == 0) {
0224         unsigned short relcode;
0225 
0226         /* rotary0 taks lower half, rotary1 taks upper half. */
0227         relcode = code & 0xffff;
0228         pdata->rotary0_rel_code = (code & 0xffff);
0229         __set_bit(relcode, input_dev->relbit);
0230 
0231         relcode = code >> 16;
0232         pdata->rotary1_rel_code = relcode;
0233         __set_bit(relcode, input_dev->relbit);
0234     }
0235 
0236     for (i = 0; i < 2; i++) {
0237         prop = of_get_property(np, rotaryname[i], &proplen);
0238         /*
0239          * If the prop is not set, it means keypad does not need
0240          * initialize the rotaryX.
0241          */
0242         if (!prop)
0243             continue;
0244 
0245         code = be32_to_cpup(prop);
0246         /*
0247          * Not all up/down key code are valid.
0248          * Now we depends on direct-rel-code.
0249          */
0250         if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
0251             return relkey_ret;
0252         } else {
0253             unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
0254             unsigned short keycode;
0255 
0256             keycode = code & 0xffff;
0257             keypad->keycodes[n] = keycode;
0258             __set_bit(keycode, input_dev->keybit);
0259 
0260             keycode = code >> 16;
0261             keypad->keycodes[n + 1] = keycode;
0262             __set_bit(keycode, input_dev->keybit);
0263 
0264             if (i == 0)
0265                 pdata->rotary0_rel_code = -1;
0266             else
0267                 pdata->rotary1_rel_code = -1;
0268         }
0269         if (i == 0)
0270             pdata->enable_rotary0 = 1;
0271         else
0272             pdata->enable_rotary1 = 1;
0273     }
0274 
0275     keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
0276     keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
0277 
0278     return 0;
0279 }
0280 
0281 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
0282 {
0283     struct input_dev *input_dev = keypad->input_dev;
0284     struct device *dev = input_dev->dev.parent;
0285     struct device_node *np = dev->of_node;
0286     struct pxa27x_keypad_platform_data *pdata;
0287     int error;
0288 
0289     pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0290     if (!pdata) {
0291         dev_err(dev, "failed to allocate memory for pdata\n");
0292         return -ENOMEM;
0293     }
0294 
0295     error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata);
0296     if (error) {
0297         dev_err(dev, "failed to parse matrix key\n");
0298         return error;
0299     }
0300 
0301     error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata);
0302     if (error) {
0303         dev_err(dev, "failed to parse direct key\n");
0304         return error;
0305     }
0306 
0307     error = pxa27x_keypad_rotary_parse_dt(keypad, pdata);
0308     if (error) {
0309         dev_err(dev, "failed to parse rotary key\n");
0310         return error;
0311     }
0312 
0313     error = of_property_read_u32(np, "marvell,debounce-interval",
0314                      &pdata->debounce_interval);
0315     if (error) {
0316         dev_err(dev, "failed to parse debounce-interval\n");
0317         return error;
0318     }
0319 
0320     /*
0321      * The keycodes may not only includes matrix key but also the direct
0322      * key or rotary key.
0323      */
0324     input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
0325 
0326     keypad->pdata = pdata;
0327     return 0;
0328 }
0329 
0330 #else
0331 
0332 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
0333 {
0334     dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
0335 
0336     return -EINVAL;
0337 }
0338 
0339 #endif
0340 
0341 static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
0342 {
0343     const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
0344     struct input_dev *input_dev = keypad->input_dev;
0345     unsigned short keycode;
0346     int i;
0347     int error;
0348 
0349     error = matrix_keypad_build_keymap(pdata->matrix_keymap_data, NULL,
0350                        pdata->matrix_key_rows,
0351                        pdata->matrix_key_cols,
0352                        keypad->keycodes, input_dev);
0353     if (error)
0354         return error;
0355 
0356     /*
0357      * The keycodes may not only include matrix keys but also the direct
0358      * or rotary keys.
0359      */
0360     input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
0361 
0362     /* For direct keys. */
0363     for (i = 0; i < pdata->direct_key_num; i++) {
0364         keycode = pdata->direct_key_map[i];
0365         keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
0366         __set_bit(keycode, input_dev->keybit);
0367     }
0368 
0369     if (pdata->enable_rotary0) {
0370         if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
0371             keycode = pdata->rotary0_up_key;
0372             keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
0373             __set_bit(keycode, input_dev->keybit);
0374 
0375             keycode = pdata->rotary0_down_key;
0376             keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
0377             __set_bit(keycode, input_dev->keybit);
0378 
0379             keypad->rotary_rel_code[0] = -1;
0380         } else {
0381             keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
0382             __set_bit(pdata->rotary0_rel_code, input_dev->relbit);
0383         }
0384     }
0385 
0386     if (pdata->enable_rotary1) {
0387         if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
0388             keycode = pdata->rotary1_up_key;
0389             keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
0390             __set_bit(keycode, input_dev->keybit);
0391 
0392             keycode = pdata->rotary1_down_key;
0393             keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
0394             __set_bit(keycode, input_dev->keybit);
0395 
0396             keypad->rotary_rel_code[1] = -1;
0397         } else {
0398             keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
0399             __set_bit(pdata->rotary1_rel_code, input_dev->relbit);
0400         }
0401     }
0402 
0403     __clear_bit(KEY_RESERVED, input_dev->keybit);
0404 
0405     return 0;
0406 }
0407 
0408 static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
0409 {
0410     const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
0411     struct input_dev *input_dev = keypad->input_dev;
0412     int row, col, num_keys_pressed = 0;
0413     uint32_t new_state[MAX_MATRIX_KEY_COLS];
0414     uint32_t kpas = keypad_readl(KPAS);
0415 
0416     num_keys_pressed = KPAS_MUKP(kpas);
0417 
0418     memset(new_state, 0, sizeof(new_state));
0419 
0420     if (num_keys_pressed == 0)
0421         goto scan;
0422 
0423     if (num_keys_pressed == 1) {
0424         col = KPAS_CP(kpas);
0425         row = KPAS_RP(kpas);
0426 
0427         /* if invalid row/col, treat as no key pressed */
0428         if (col >= pdata->matrix_key_cols ||
0429             row >= pdata->matrix_key_rows)
0430             goto scan;
0431 
0432         new_state[col] = (1 << row);
0433         goto scan;
0434     }
0435 
0436     if (num_keys_pressed > 1) {
0437         uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
0438         uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
0439         uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
0440         uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
0441 
0442         new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
0443         new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
0444         new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
0445         new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
0446         new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
0447         new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
0448         new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
0449         new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
0450     }
0451 scan:
0452     for (col = 0; col < pdata->matrix_key_cols; col++) {
0453         uint32_t bits_changed;
0454         int code;
0455 
0456         bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
0457         if (bits_changed == 0)
0458             continue;
0459 
0460         for (row = 0; row < pdata->matrix_key_rows; row++) {
0461             if ((bits_changed & (1 << row)) == 0)
0462                 continue;
0463 
0464             code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
0465 
0466             input_event(input_dev, EV_MSC, MSC_SCAN, code);
0467             input_report_key(input_dev, keypad->keycodes[code],
0468                      new_state[col] & (1 << row));
0469         }
0470     }
0471     input_sync(input_dev);
0472     memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
0473 }
0474 
0475 #define DEFAULT_KPREC   (0x007f007f)
0476 
0477 static inline int rotary_delta(uint32_t kprec)
0478 {
0479     if (kprec & KPREC_OF0)
0480         return (kprec & 0xff) + 0x7f;
0481     else if (kprec & KPREC_UF0)
0482         return (kprec & 0xff) - 0x7f - 0xff;
0483     else
0484         return (kprec & 0xff) - 0x7f;
0485 }
0486 
0487 static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
0488 {
0489     struct input_dev *dev = keypad->input_dev;
0490 
0491     if (delta == 0)
0492         return;
0493 
0494     if (keypad->rotary_rel_code[r] == -1) {
0495         int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
0496         unsigned char keycode = keypad->keycodes[code];
0497 
0498         /* simulate a press-n-release */
0499         input_event(dev, EV_MSC, MSC_SCAN, code);
0500         input_report_key(dev, keycode, 1);
0501         input_sync(dev);
0502         input_event(dev, EV_MSC, MSC_SCAN, code);
0503         input_report_key(dev, keycode, 0);
0504         input_sync(dev);
0505     } else {
0506         input_report_rel(dev, keypad->rotary_rel_code[r], delta);
0507         input_sync(dev);
0508     }
0509 }
0510 
0511 static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
0512 {
0513     const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
0514     uint32_t kprec;
0515 
0516     /* read and reset to default count value */
0517     kprec = keypad_readl(KPREC);
0518     keypad_writel(KPREC, DEFAULT_KPREC);
0519 
0520     if (pdata->enable_rotary0)
0521         report_rotary_event(keypad, 0, rotary_delta(kprec));
0522 
0523     if (pdata->enable_rotary1)
0524         report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
0525 }
0526 
0527 static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
0528 {
0529     const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
0530     struct input_dev *input_dev = keypad->input_dev;
0531     unsigned int new_state;
0532     uint32_t kpdk, bits_changed;
0533     int i;
0534 
0535     kpdk = keypad_readl(KPDK);
0536 
0537     if (pdata->enable_rotary0 || pdata->enable_rotary1)
0538         pxa27x_keypad_scan_rotary(keypad);
0539 
0540     /*
0541      * The KPDR_DK only output the key pin level, so it relates to board,
0542      * and low level may be active.
0543      */
0544     if (pdata->direct_key_low_active)
0545         new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
0546     else
0547         new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
0548 
0549     bits_changed = keypad->direct_key_state ^ new_state;
0550 
0551     if (bits_changed == 0)
0552         return;
0553 
0554     for (i = 0; i < pdata->direct_key_num; i++) {
0555         if (bits_changed & (1 << i)) {
0556             int code = MAX_MATRIX_KEY_NUM + i;
0557 
0558             input_event(input_dev, EV_MSC, MSC_SCAN, code);
0559             input_report_key(input_dev, keypad->keycodes[code],
0560                      new_state & (1 << i));
0561         }
0562     }
0563     input_sync(input_dev);
0564     keypad->direct_key_state = new_state;
0565 }
0566 
0567 static void clear_wakeup_event(struct pxa27x_keypad *keypad)
0568 {
0569     const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
0570 
0571     if (pdata->clear_wakeup_event)
0572         (pdata->clear_wakeup_event)();
0573 }
0574 
0575 static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
0576 {
0577     struct pxa27x_keypad *keypad = dev_id;
0578     unsigned long kpc = keypad_readl(KPC);
0579 
0580     clear_wakeup_event(keypad);
0581 
0582     if (kpc & KPC_DI)
0583         pxa27x_keypad_scan_direct(keypad);
0584 
0585     if (kpc & KPC_MI)
0586         pxa27x_keypad_scan_matrix(keypad);
0587 
0588     return IRQ_HANDLED;
0589 }
0590 
0591 static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
0592 {
0593     const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
0594     unsigned int mask = 0, direct_key_num = 0;
0595     unsigned long kpc = 0;
0596 
0597     /* clear pending interrupt bit */
0598     keypad_readl(KPC);
0599 
0600     /* enable matrix keys with automatic scan */
0601     if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
0602         kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
0603         kpc |= KPC_MKRN(pdata->matrix_key_rows) |
0604                KPC_MKCN(pdata->matrix_key_cols);
0605     }
0606 
0607     /* enable rotary key, debounce interval same as direct keys */
0608     if (pdata->enable_rotary0) {
0609         mask |= 0x03;
0610         direct_key_num = 2;
0611         kpc |= KPC_REE0;
0612     }
0613 
0614     if (pdata->enable_rotary1) {
0615         mask |= 0x0c;
0616         direct_key_num = 4;
0617         kpc |= KPC_REE1;
0618     }
0619 
0620     if (pdata->direct_key_num > direct_key_num)
0621         direct_key_num = pdata->direct_key_num;
0622 
0623     /*
0624      * Direct keys usage may not start from KP_DKIN0, check the platfrom
0625      * mask data to config the specific.
0626      */
0627     if (pdata->direct_key_mask)
0628         keypad->direct_key_mask = pdata->direct_key_mask;
0629     else
0630         keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
0631 
0632     /* enable direct key */
0633     if (direct_key_num)
0634         kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
0635 
0636     keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
0637     keypad_writel(KPREC, DEFAULT_KPREC);
0638     keypad_writel(KPKDI, pdata->debounce_interval);
0639 }
0640 
0641 static int pxa27x_keypad_open(struct input_dev *dev)
0642 {
0643     struct pxa27x_keypad *keypad = input_get_drvdata(dev);
0644     int ret;
0645     /* Enable unit clock */
0646     ret = clk_prepare_enable(keypad->clk);
0647     if (ret)
0648         return ret;
0649 
0650     pxa27x_keypad_config(keypad);
0651 
0652     return 0;
0653 }
0654 
0655 static void pxa27x_keypad_close(struct input_dev *dev)
0656 {
0657     struct pxa27x_keypad *keypad = input_get_drvdata(dev);
0658 
0659     /* Disable clock unit */
0660     clk_disable_unprepare(keypad->clk);
0661 }
0662 
0663 #ifdef CONFIG_PM_SLEEP
0664 static int pxa27x_keypad_suspend(struct device *dev)
0665 {
0666     struct platform_device *pdev = to_platform_device(dev);
0667     struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
0668 
0669     /*
0670      * If the keypad is used a wake up source, clock can not be disabled.
0671      * Or it can not detect the key pressing.
0672      */
0673     if (device_may_wakeup(&pdev->dev))
0674         enable_irq_wake(keypad->irq);
0675     else
0676         clk_disable_unprepare(keypad->clk);
0677 
0678     return 0;
0679 }
0680 
0681 static int pxa27x_keypad_resume(struct device *dev)
0682 {
0683     struct platform_device *pdev = to_platform_device(dev);
0684     struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
0685     struct input_dev *input_dev = keypad->input_dev;
0686     int ret = 0;
0687 
0688     /*
0689      * If the keypad is used as wake up source, the clock is not turned
0690      * off. So do not need configure it again.
0691      */
0692     if (device_may_wakeup(&pdev->dev)) {
0693         disable_irq_wake(keypad->irq);
0694     } else {
0695         mutex_lock(&input_dev->mutex);
0696 
0697         if (input_device_enabled(input_dev)) {
0698             /* Enable unit clock */
0699             ret = clk_prepare_enable(keypad->clk);
0700             if (!ret)
0701                 pxa27x_keypad_config(keypad);
0702         }
0703 
0704         mutex_unlock(&input_dev->mutex);
0705     }
0706 
0707     return ret;
0708 }
0709 #endif
0710 
0711 static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
0712              pxa27x_keypad_suspend, pxa27x_keypad_resume);
0713 
0714 
0715 static int pxa27x_keypad_probe(struct platform_device *pdev)
0716 {
0717     const struct pxa27x_keypad_platform_data *pdata =
0718                     dev_get_platdata(&pdev->dev);
0719     struct device_node *np = pdev->dev.of_node;
0720     struct pxa27x_keypad *keypad;
0721     struct input_dev *input_dev;
0722     struct resource *res;
0723     int irq, error;
0724 
0725     /* Driver need build keycode from device tree or pdata */
0726     if (!np && !pdata)
0727         return -EINVAL;
0728 
0729     irq = platform_get_irq(pdev, 0);
0730     if (irq < 0)
0731         return -ENXIO;
0732 
0733     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0734     if (res == NULL) {
0735         dev_err(&pdev->dev, "failed to get I/O memory\n");
0736         return -ENXIO;
0737     }
0738 
0739     keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad),
0740                   GFP_KERNEL);
0741     if (!keypad)
0742         return -ENOMEM;
0743 
0744     input_dev = devm_input_allocate_device(&pdev->dev);
0745     if (!input_dev)
0746         return -ENOMEM;
0747 
0748     keypad->pdata = pdata;
0749     keypad->input_dev = input_dev;
0750     keypad->irq = irq;
0751 
0752     keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
0753     if (IS_ERR(keypad->mmio_base))
0754         return PTR_ERR(keypad->mmio_base);
0755 
0756     keypad->clk = devm_clk_get(&pdev->dev, NULL);
0757     if (IS_ERR(keypad->clk)) {
0758         dev_err(&pdev->dev, "failed to get keypad clock\n");
0759         return PTR_ERR(keypad->clk);
0760     }
0761 
0762     input_dev->name = pdev->name;
0763     input_dev->id.bustype = BUS_HOST;
0764     input_dev->open = pxa27x_keypad_open;
0765     input_dev->close = pxa27x_keypad_close;
0766     input_dev->dev.parent = &pdev->dev;
0767 
0768     input_dev->keycode = keypad->keycodes;
0769     input_dev->keycodesize = sizeof(keypad->keycodes[0]);
0770     input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
0771 
0772     input_set_drvdata(input_dev, keypad);
0773 
0774     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
0775     input_set_capability(input_dev, EV_MSC, MSC_SCAN);
0776 
0777     if (pdata) {
0778         error = pxa27x_keypad_build_keycode(keypad);
0779     } else {
0780         error = pxa27x_keypad_build_keycode_from_dt(keypad);
0781         /*
0782          * Data that we get from DT resides in dynamically
0783          * allocated memory so we need to update our pdata
0784          * pointer.
0785          */
0786         pdata = keypad->pdata;
0787     }
0788     if (error) {
0789         dev_err(&pdev->dev, "failed to build keycode\n");
0790         return error;
0791     }
0792 
0793     keypad->row_shift = get_count_order(pdata->matrix_key_cols);
0794 
0795     if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
0796         (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
0797         input_dev->evbit[0] |= BIT_MASK(EV_REL);
0798     }
0799 
0800     error = devm_request_irq(&pdev->dev, irq, pxa27x_keypad_irq_handler,
0801                  0, pdev->name, keypad);
0802     if (error) {
0803         dev_err(&pdev->dev, "failed to request IRQ\n");
0804         return error;
0805     }
0806 
0807     /* Register the input device */
0808     error = input_register_device(input_dev);
0809     if (error) {
0810         dev_err(&pdev->dev, "failed to register input device\n");
0811         return error;
0812     }
0813 
0814     platform_set_drvdata(pdev, keypad);
0815     device_init_wakeup(&pdev->dev, 1);
0816 
0817     return 0;
0818 }
0819 
0820 #ifdef CONFIG_OF
0821 static const struct of_device_id pxa27x_keypad_dt_match[] = {
0822     { .compatible = "marvell,pxa27x-keypad" },
0823     {},
0824 };
0825 MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
0826 #endif
0827 
0828 static struct platform_driver pxa27x_keypad_driver = {
0829     .probe      = pxa27x_keypad_probe,
0830     .driver     = {
0831         .name   = "pxa27x-keypad",
0832         .of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
0833         .pm = &pxa27x_keypad_pm_ops,
0834     },
0835 };
0836 module_platform_driver(pxa27x_keypad_driver);
0837 
0838 MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
0839 MODULE_LICENSE("GPL");
0840 /* work with hotplug and coldplug */
0841 MODULE_ALIAS("platform:pxa27x-keypad");