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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Driver for the IMX keypad port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) // Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/input/matrix_keypad.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * Keypad Controller registers (halfword)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define KPCR		0x00 /* Keypad Control Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define KPSR		0x02 /* Keypad Status Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define KBD_STAT_KPKD	(0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define KBD_STAT_KPKR	(0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define KBD_STAT_KDSC	(0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define KBD_STAT_KRSS	(0x1 << 3) /* Key Release Synch Status bit (w1c)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define KBD_STAT_KDIE	(0x1 << 8) /* Key Depress Interrupt Enable Status bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define KBD_STAT_KRIE	(0x1 << 9) /* Key Release Interrupt Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define KBD_STAT_KPPEN	(0x1 << 10) /* Keypad Clock Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define KDDR		0x04 /* Keypad Data Direction Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define KPDR		0x06 /* Keypad Data Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define MAX_MATRIX_KEY_ROWS	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define MAX_MATRIX_KEY_COLS	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define MATRIX_ROW_SHIFT	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define MAX_MATRIX_KEY_NUM	(MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) struct imx_keypad {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	void __iomem *mmio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int			irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct timer_list	check_matrix_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	 * The matrix is stable only if no changes are detected after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int			stable_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	bool			enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	/* Masks for enabled rows/cols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	unsigned short		rows_en_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned short		cols_en_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	unsigned short		keycodes[MAX_MATRIX_KEY_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * Matrix states:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 * -stable: achieved after a complete debounce process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	 * -unstable: used in the debouncing process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned short		matrix_stable_state[MAX_MATRIX_KEY_COLS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	unsigned short		matrix_unstable_state[MAX_MATRIX_KEY_COLS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) /* Scan the matrix and return the new state in *matrix_volatile_state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void imx_keypad_scan_matrix(struct imx_keypad *keypad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 				  unsigned short *matrix_volatile_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int col;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	unsigned short reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		if ((keypad->cols_en_mask & (1 << col)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		 * Discharge keypad capacitance:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		 * 2. write 1s on column data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		 * 3. configure columns as totem-pole to discharge capacitance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		 * 4. configure columns as open-drain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		reg_val = readw(keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		reg_val |= 0xff00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		writew(reg_val, keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		reg_val = readw(keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		reg_val &= ~((keypad->cols_en_mask & 0xff) << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		writew(reg_val, keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		udelay(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		reg_val = readw(keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		reg_val |= (keypad->cols_en_mask & 0xff) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		writew(reg_val, keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		 * 5. Write a single column to 0, others to 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		 * 6. Sample row inputs and save data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		 * 7. Repeat steps 2 - 6 for remaining columns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		reg_val = readw(keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		reg_val &= ~(1 << (8 + col));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		writew(reg_val, keypad->mmio_base + KPDR);
^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) 		 * Delay added to avoid propagating the 0 from column to row
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		 * when scanning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		udelay(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		 * 1s in matrix_volatile_state[col] means key pressures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		 * throw data from non enabled rows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		reg_val = readw(keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * Return in standby mode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * 9. write 0s to columns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	reg_val = readw(keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	reg_val &= 0x00ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	writew(reg_val, keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * Compare the new matrix state (volatile) with the stable one stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * keypad->matrix_stable_state and fire events if changes are detected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static void imx_keypad_fire_events(struct imx_keypad *keypad,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				   unsigned short *matrix_volatile_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	struct input_dev *input_dev = keypad->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int row, col;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		unsigned short bits_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		if ((keypad->cols_en_mask & (1 << col)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			continue; /* Column is not enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		bits_changed = keypad->matrix_stable_state[col] ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 						matrix_volatile_state[col];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (bits_changed == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			continue; /* Column does not contain changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			if ((keypad->rows_en_mask & (1 << row)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				continue; /* Row is not enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			if ((bits_changed & (1 << row)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				continue; /* Row does not contain changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			input_report_key(input_dev, keypad->keycodes[code],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				matrix_volatile_state[col] & (1 << row));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			dev_dbg(&input_dev->dev, "Event code: %d, val: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				keypad->keycodes[code],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				matrix_volatile_state[col] & (1 << row));
^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) 	input_sync(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * imx_keypad_check_for_events is the timer handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void imx_keypad_check_for_events(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct imx_keypad *keypad = from_timer(keypad, t, check_matrix_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	unsigned short reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	bool state_changed, is_zero_matrix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	imx_keypad_scan_matrix(keypad, matrix_volatile_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	state_changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if ((keypad->cols_en_mask & (1 << i)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			state_changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			break;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * If the matrix state is changed from the previous scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 *   (Re)Begin the debouncing process, saving the new state in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 *    keypad->matrix_unstable_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 *   Increase the count of number of scans with a stable state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (state_changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			sizeof(matrix_volatile_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		keypad->stable_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		keypad->stable_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	 * If the matrix is not as stable as we want reschedule scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	 * in the near future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		mod_timer(&keypad->check_matrix_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			  jiffies + msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	 * If the matrix state is stable, fire the events and save the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 * stable state. Note, if the matrix is kept stable for longer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * events have already been generated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		imx_keypad_fire_events(keypad, matrix_volatile_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		memcpy(keypad->matrix_stable_state, matrix_volatile_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			sizeof(matrix_volatile_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	is_zero_matrix = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (matrix_volatile_state[i] != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			is_zero_matrix = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (is_zero_matrix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 * All keys have been released. Enable only the KDI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		 * interrupt for future key presses (clear the KDI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		 * status bit and its sync chain before that).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		reg_val |= KBD_STAT_KDIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		reg_val &= ~KBD_STAT_KRIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		 * Some keys are still pressed. Schedule a rescan in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		 * attempt to detect multiple key presses and enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		 * the KRI interrupt to react quickly to key release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		 * event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		mod_timer(&keypad->check_matrix_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			  jiffies + msecs_to_jiffies(60));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		reg_val |= KBD_STAT_KRIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		reg_val &= ~KBD_STAT_KDIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct imx_keypad *keypad = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	unsigned short reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	/* Disable both interrupt types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	/* Clear interrupts status bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (keypad->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		/* The matrix is supposed to be changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		keypad->stable_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		/* Schedule the scanning procedure near in the future */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		mod_timer(&keypad->check_matrix_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			  jiffies + msecs_to_jiffies(2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static void imx_keypad_config(struct imx_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	unsigned short reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	 * Include enabled rows in interrupt generation (KPCR[7:0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	 * Configure keypad columns as open-drain (KPCR[15:8])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	reg_val = readw(keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	reg_val |= keypad->rows_en_mask & 0xff;		/* rows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	reg_val |= (keypad->cols_en_mask & 0xff) << 8;	/* cols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	writew(reg_val, keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	/* Write 0's to KPDR[15:8] (Colums) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	reg_val = readw(keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	reg_val &= 0x00ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	writew(reg_val, keypad->mmio_base + KPDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	/* Configure columns as output, rows as input (KDDR[15:0]) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	writew(0xff00, keypad->mmio_base + KDDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	 * Clear Key Depress and Key Release status bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * Clear both synchronizer chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		   KBD_STAT_KDSC | KBD_STAT_KRSS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	/* Enable KDI and disable KRI (avoid false release events). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	reg_val |= KBD_STAT_KDIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	reg_val &= ~KBD_STAT_KRIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static void imx_keypad_inhibit(struct imx_keypad *keypad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	unsigned short reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	/* Inhibit KDI and KRI interrupts. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	reg_val = readw(keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	writew(reg_val, keypad->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	/* Colums as open drain and disable all rows */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	reg_val = (keypad->cols_en_mask & 0xff) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	writew(reg_val, keypad->mmio_base + KPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static void imx_keypad_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct imx_keypad *keypad = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	dev_dbg(&dev->dev, ">%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	/* Mark keypad as being inactive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	keypad->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	synchronize_irq(keypad->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	del_timer_sync(&keypad->check_matrix_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	imx_keypad_inhibit(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	/* Disable clock unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	clk_disable_unprepare(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int imx_keypad_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct imx_keypad *keypad = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	dev_dbg(&dev->dev, ">%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	/* Enable the kpp clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	error = clk_prepare_enable(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* We became active from now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	keypad->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	imx_keypad_config(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/* Sanity control, not all the rows must be actived now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			"too many keys pressed, control pins initialisation\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		goto open_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) open_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	imx_keypad_close(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static const struct of_device_id imx_keypad_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	{ .compatible = "fsl,imx21-kpp", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	{ /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static int imx_keypad_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	const struct matrix_keymap_data *keymap_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct imx_keypad *keypad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	int irq, error, i, row, col;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (!keymap_data && !pdev->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		dev_err(&pdev->dev, "no keymap defined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (!input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		dev_err(&pdev->dev, "failed to allocate the input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (!keypad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		dev_err(&pdev->dev, "not enough memory for driver data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	keypad->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	keypad->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	keypad->stable_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	timer_setup(&keypad->check_matrix_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		    imx_keypad_check_for_events, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	keypad->mmio_base = devm_platform_ioremap_resource(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (IS_ERR(keypad->mmio_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		return PTR_ERR(keypad->mmio_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	keypad->clk = devm_clk_get(&pdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (IS_ERR(keypad->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		dev_err(&pdev->dev, "failed to get keypad clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		return PTR_ERR(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	/* Init the Input device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	input_dev->name = pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	input_dev->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	input_dev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	input_dev->open = imx_keypad_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	input_dev->close = imx_keypad_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	error = matrix_keypad_build_keymap(keymap_data, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 					   MAX_MATRIX_KEY_ROWS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 					   MAX_MATRIX_KEY_COLS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 					   keypad->keycodes, input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		dev_err(&pdev->dev, "failed to build keymap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	/* Search for rows and cols enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			if (keypad->keycodes[i] != KEY_RESERVED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				keypad->rows_en_mask |= 1 << row;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 				keypad->cols_en_mask |= 1 << col;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	__set_bit(EV_REP, input_dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	input_set_drvdata(input_dev, keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	/* Ensure that the keypad will stay dormant until opened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	error = clk_prepare_enable(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	imx_keypad_inhibit(keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	clk_disable_unprepare(keypad->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			    pdev->name, keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		dev_err(&pdev->dev, "failed to request IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/* Register the input device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	error = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		dev_err(&pdev->dev, "failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	platform_set_drvdata(pdev, keypad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	device_init_wakeup(&pdev->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static int __maybe_unused imx_kbd_noirq_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	struct imx_keypad *kbd = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	struct input_dev *input_dev = kbd->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	unsigned short reg_val = readw(kbd->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	/* imx kbd can wake up system even clock is disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	mutex_lock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	if (input_dev->users)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		clk_disable_unprepare(kbd->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	mutex_unlock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (device_may_wakeup(&pdev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		if (reg_val & KBD_STAT_KPKD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			reg_val |= KBD_STAT_KRIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		if (reg_val & KBD_STAT_KPKR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			reg_val |= KBD_STAT_KDIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		writew(reg_val, kbd->mmio_base + KPSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		enable_irq_wake(kbd->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) static int __maybe_unused imx_kbd_noirq_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	struct imx_keypad *kbd = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	struct input_dev *input_dev = kbd->input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (device_may_wakeup(&pdev->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		disable_irq_wake(kbd->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	mutex_lock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (input_dev->users) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		ret = clk_prepare_enable(kbd->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			goto err_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) err_clk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	mutex_unlock(&input_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static const struct dev_pm_ops imx_kbd_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_kbd_noirq_suspend, imx_kbd_noirq_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) static struct platform_driver imx_keypad_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		.name	= "imx-keypad",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		.pm	= &imx_kbd_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		.of_match_table = of_match_ptr(imx_keypad_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	.probe		= imx_keypad_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) module_platform_driver(imx_keypad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) MODULE_DESCRIPTION("IMX Keypad Port Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) MODULE_ALIAS("platform:imx-keypad");