Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * linux/drivers/input/keyboard/pxa27x_keypad.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Driver for the pxa27x matrix keyboard controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Created:	Feb 22, 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Author:	Rodolfo Giometti <giometti@linux.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Based on a previous implementations by Kevin O'Connor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * on some suggestions by Nicolas Pitre <nico@fluxnic.net>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/input/matrix_keypad.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/platform_data/keypad-pxa27x.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * Keypad Controller registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define KPC             0x0000 /* Keypad Control register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define KPDK            0x0008 /* Keypad Direct Key register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define KPREC           0x0010 /* Keypad Rotary Encoder register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define KPMK            0x0018 /* Keypad Matrix Key register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define KPAS            0x0020 /* Keypad Automatic Scan register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define KPASMKP0        0x0028
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define KPASMKP1        0x0030
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define KPASMKP2        0x0038
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define KPASMKP3        0x0040
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define KPKDI           0x0048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /* bit definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define KPC_MKRN(n)	((((n) - 1) & 0x7) << 26) /* matrix key row number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define KPC_MKCN(n)	((((n) - 1) & 0x7) << 23) /* matrix key column number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define KPC_DKN(n)	((((n) - 1) & 0x7) << 6)  /* direct key number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define KPC_MS(n)	(0x1 << (13 + (n)))	/* Matrix scan line 'n' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define KPC_MS_ALL      (0xff << 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define KPC_DK_DEB_SEL	(0x1 <<  9)  /* Direct Keypad Debounce Select */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define KPDK_DKP        (0x1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define KPDK_DK(n)	((n) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define KPREC_OF1       (0x1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define kPREC_UF1       (0x1 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define KPREC_OF0       (0x1 << 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define KPREC_UF0       (0x1 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #define KPREC_RECOUNT0(n)	((n) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #define KPREC_RECOUNT1(n)	(((n) >> 16) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define KPMK_MKP        (0x1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define KPAS_SO         (0x1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define KPASMKPx_SO     (0x1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define KPAS_MUKP(n)	(((n) >> 26) & 0x1f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define KPAS_RP(n)	(((n) >> 4) & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define KPAS_CP(n)	((n) & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define KPASMKP_MKC_MASK	(0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define keypad_readl(off)	__raw_readl(keypad->mmio_base + (off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define keypad_writel(off, v)	__raw_writel((v), keypad->mmio_base + (off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) #define MAX_MATRIX_KEY_NUM	(MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define MAX_KEYPAD_KEYS		(MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) struct pxa27x_keypad {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	const struct pxa27x_keypad_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	void __iomem *mmio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	unsigned short keycodes[MAX_KEYPAD_KEYS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	int rotary_rel_code[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned int row_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* state row bits of each column scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	uint32_t direct_key_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned int direct_key_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				struct pxa27x_keypad_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct device *dev = input_dev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	u32 rows, cols;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	error = matrix_keypad_parse_properties(dev, &rows, &cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		dev_err(dev, "rows or cols exceeds maximum value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	pdata->matrix_key_rows = rows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	pdata->matrix_key_cols = cols;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	error = matrix_keypad_build_keymap(NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 					   pdata->matrix_key_rows,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 					   pdata->matrix_key_cols,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 					   keypad->keycodes, input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				struct pxa27x_keypad_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct device *dev = input_dev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	const __be16 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	unsigned short code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	unsigned int proplen, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	error = of_property_read_u32(np, "marvell,direct-key-count",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				     &pdata->direct_key_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		 * If do not have marvel,direct-key-count defined,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		 * it means direct key is not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return error == -EINVAL ? 0 : error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	error = of_property_read_u32(np, "marvell,direct-key-mask",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				     &pdata->direct_key_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		if (error != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		 * If marvell,direct-key-mask is not defined, driver will use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		 * default value. Default value is set when configure the keypad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		pdata->direct_key_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	pdata->direct_key_low_active = of_property_read_bool(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 					"marvell,direct-key-low-active");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	prop = of_get_property(np, "marvell,direct-key-map", &proplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (proplen % sizeof(u16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	size = proplen / sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/* Only MAX_DIRECT_KEY_NUM is accepted.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (size > MAX_DIRECT_KEY_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		code = be16_to_cpup(prop + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		__set_bit(code, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				struct pxa27x_keypad_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int i, relkey_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	unsigned int code, proplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	const char *rotaryname[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			"marvell,rotary0", "marvell,rotary1"};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	const char relkeyname[] = {"marvell,rotary-rel-key"};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct device *dev = input_dev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	relkey_ret = of_property_read_u32(np, relkeyname, &code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* if can read correct rotary key-code, we do not need this. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (relkey_ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		unsigned short relcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		/* rotary0 taks lower half, rotary1 taks upper half. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		relcode = code & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		pdata->rotary0_rel_code = (code & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		__set_bit(relcode, input_dev->relbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		relcode = code >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		pdata->rotary1_rel_code = relcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		__set_bit(relcode, input_dev->relbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		prop = of_get_property(np, rotaryname[i], &proplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		 * If the prop is not set, it means keypad does not need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		 * initialize the rotaryX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		code = be32_to_cpup(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		 * Not all up/down key code are valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		 * Now we depends on direct-rel-code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			return relkey_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			unsigned short keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			keycode = code & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			keypad->keycodes[n] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			keycode = code >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			keypad->keycodes[n + 1] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				pdata->rotary0_rel_code = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				pdata->rotary1_rel_code = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			pdata->enable_rotary0 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			pdata->enable_rotary1 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct device *dev = input_dev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct pxa27x_keypad_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		dev_err(dev, "failed to allocate memory for pdata\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		dev_err(dev, "failed to parse matrix key\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		dev_err(dev, "failed to parse direct key\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	error = pxa27x_keypad_rotary_parse_dt(keypad, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		dev_err(dev, "failed to parse rotary key\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	error = of_property_read_u32(np, "marvell,debounce-interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				     &pdata->debounce_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		dev_err(dev, "failed to parse debounce-interval\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	 * The keycodes may not only includes matrix key but also the direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	 * key or rotary key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	keypad->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	unsigned short keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	error = matrix_keypad_build_keymap(pdata->matrix_keymap_data, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 					   pdata->matrix_key_rows,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 					   pdata->matrix_key_cols,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 					   keypad->keycodes, input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * The keycodes may not only include matrix keys but also the direct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * or rotary keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	/* For direct keys. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	for (i = 0; i < pdata->direct_key_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		keycode = pdata->direct_key_map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (pdata->enable_rotary0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			keycode = pdata->rotary0_up_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			keycode = pdata->rotary0_down_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			keypad->rotary_rel_code[0] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			__set_bit(pdata->rotary0_rel_code, input_dev->relbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (pdata->enable_rotary1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			keycode = pdata->rotary1_up_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			keycode = pdata->rotary1_down_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			__set_bit(keycode, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			keypad->rotary_rel_code[1] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			__set_bit(pdata->rotary1_rel_code, input_dev->relbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	__clear_bit(KEY_RESERVED, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	int row, col, num_keys_pressed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	uint32_t kpas = keypad_readl(KPAS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	num_keys_pressed = KPAS_MUKP(kpas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	memset(new_state, 0, sizeof(new_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (num_keys_pressed == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (num_keys_pressed == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		col = KPAS_CP(kpas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		row = KPAS_RP(kpas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		/* if invalid row/col, treat as no key pressed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		if (col >= pdata->matrix_key_cols ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		    row >= pdata->matrix_key_rows)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		new_state[col] = (1 << row);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (num_keys_pressed > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) scan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	for (col = 0; col < pdata->matrix_key_cols; col++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		uint32_t bits_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		if (bits_changed == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		for (row = 0; row < pdata->matrix_key_rows; row++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			if ((bits_changed & (1 << row)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			input_report_key(input_dev, keypad->keycodes[code],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 					 new_state[col] & (1 << row));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #define DEFAULT_KPREC	(0x007f007f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static inline int rotary_delta(uint32_t kprec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (kprec & KPREC_OF0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		return (kprec & 0xff) + 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	else if (kprec & KPREC_UF0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		return (kprec & 0xff) - 0x7f - 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		return (kprec & 0xff) - 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	struct input_dev *dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	if (delta == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (keypad->rotary_rel_code[r] == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		unsigned char keycode = keypad->keycodes[code];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		/* simulate a press-n-release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		input_event(dev, EV_MSC, MSC_SCAN, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		input_report_key(dev, keycode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		input_event(dev, EV_MSC, MSC_SCAN, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		input_report_key(dev, keycode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	uint32_t kprec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	/* read and reset to default count value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	kprec = keypad_readl(KPREC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	keypad_writel(KPREC, DEFAULT_KPREC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	if (pdata->enable_rotary0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		report_rotary_event(keypad, 0, rotary_delta(kprec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	if (pdata->enable_rotary1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	unsigned int new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	uint32_t kpdk, bits_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	kpdk = keypad_readl(KPDK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		pxa27x_keypad_scan_rotary(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	 * The KPDR_DK only output the key pin level, so it relates to board,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	 * and low level may be active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (pdata->direct_key_low_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	bits_changed = keypad->direct_key_state ^ new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (bits_changed == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	for (i = 0; i < pdata->direct_key_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		if (bits_changed & (1 << i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			int code = MAX_MATRIX_KEY_NUM + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			input_report_key(input_dev, keypad->keycodes[code],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 					 new_state & (1 << i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	keypad->direct_key_state = new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static void clear_wakeup_event(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (pdata->clear_wakeup_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		(pdata->clear_wakeup_event)();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	struct pxa27x_keypad *keypad = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	unsigned long kpc = keypad_readl(KPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	clear_wakeup_event(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	if (kpc & KPC_DI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		pxa27x_keypad_scan_direct(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (kpc & KPC_MI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		pxa27x_keypad_scan_matrix(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	unsigned int mask = 0, direct_key_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	unsigned long kpc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	/* clear pending interrupt bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	keypad_readl(KPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	/* enable matrix keys with automatic scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		       KPC_MKCN(pdata->matrix_key_cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	/* enable rotary key, debounce interval same as direct keys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (pdata->enable_rotary0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		mask |= 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		direct_key_num = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		kpc |= KPC_REE0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (pdata->enable_rotary1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		mask |= 0x0c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		direct_key_num = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		kpc |= KPC_REE1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (pdata->direct_key_num > direct_key_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		direct_key_num = pdata->direct_key_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	 * Direct keys usage may not start from KP_DKIN0, check the platfrom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	 * mask data to config the specific.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	if (pdata->direct_key_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		keypad->direct_key_mask = pdata->direct_key_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	/* enable direct key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (direct_key_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	keypad_writel(KPREC, DEFAULT_KPREC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	keypad_writel(KPKDI, pdata->debounce_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static int pxa27x_keypad_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	/* Enable unit clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	ret = clk_prepare_enable(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	pxa27x_keypad_config(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static void pxa27x_keypad_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	/* Disable clock unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	clk_disable_unprepare(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) static int pxa27x_keypad_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	 * If the keypad is used a wake up source, clock can not be disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	 * Or it can not detect the key pressing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (device_may_wakeup(&pdev->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		enable_irq_wake(keypad->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		clk_disable_unprepare(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) static int pxa27x_keypad_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	 * If the keypad is used as wake up source, the clock is not turned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	 * off. So do not need configure it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	if (device_may_wakeup(&pdev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		disable_irq_wake(keypad->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		mutex_lock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		if (input_dev->users) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 			/* Enable unit clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			ret = clk_prepare_enable(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 				pxa27x_keypad_config(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		mutex_unlock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 			 pxa27x_keypad_suspend, pxa27x_keypad_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) static int pxa27x_keypad_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	const struct pxa27x_keypad_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 					dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	struct pxa27x_keypad *keypad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	int irq, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	/* Driver need build keycode from device tree or pdata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	if (!np && !pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	if (res == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		dev_err(&pdev->dev, "failed to get I/O memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 			      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	if (!keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	keypad->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	keypad->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	keypad->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	if (IS_ERR(keypad->mmio_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		return PTR_ERR(keypad->mmio_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	keypad->clk = devm_clk_get(&pdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	if (IS_ERR(keypad->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		dev_err(&pdev->dev, "failed to get keypad clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		return PTR_ERR(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	input_dev->name = pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	input_dev->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	input_dev->open = pxa27x_keypad_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	input_dev->close = pxa27x_keypad_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	input_dev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	input_dev->keycode = keypad->keycodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	input_dev->keycodesize = sizeof(keypad->keycodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	input_set_drvdata(input_dev, keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		error = pxa27x_keypad_build_keycode(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		error = pxa27x_keypad_build_keycode_from_dt(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		 * Data that we get from DT resides in dynamically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		 * allocated memory so we need to update our pdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		 * pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 		pdata = keypad->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 		dev_err(&pdev->dev, "failed to build keycode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	keypad->row_shift = get_count_order(pdata->matrix_key_cols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	    (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		input_dev->evbit[0] |= BIT_MASK(EV_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	error = devm_request_irq(&pdev->dev, irq, pxa27x_keypad_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 				 0, pdev->name, keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 		dev_err(&pdev->dev, "failed to request IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	/* Register the input device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	error = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		dev_err(&pdev->dev, "failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	platform_set_drvdata(pdev, keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	device_init_wakeup(&pdev->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static const struct of_device_id pxa27x_keypad_dt_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	{ .compatible = "marvell,pxa27x-keypad" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static struct platform_driver pxa27x_keypad_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	.probe		= pxa27x_keypad_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 		.name	= "pxa27x-keypad",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 		.of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 		.pm	= &pxa27x_keypad_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) module_platform_driver(pxa27x_keypad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /* work with hotplug and coldplug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) MODULE_ALIAS("platform:pxa27x-keypad");