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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * NXP LPC32xx SoC Key Scan Interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *    Kevin Wells <kevin.wells@nxp.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *    Roland Stigge <stigge@antcom.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2010 NXP Semiconductors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright (C) 2012 Roland Stigge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * This controller supports square key matrices from 1x1 up to 8x8
^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) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/of.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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define DRV_NAME				"lpc32xx_keys"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Key scanner register offsets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define LPC32XX_KS_DEB(x)			((x) + 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define LPC32XX_KS_STATE_COND(x)		((x) + 0x04)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define LPC32XX_KS_IRQ(x)			((x) + 0x08)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define LPC32XX_KS_SCAN_CTL(x)			((x) + 0x0C)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define LPC32XX_KS_FAST_TST(x)			((x) + 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define LPC32XX_KS_MATRIX_DIM(x)		((x) + 0x14) /* 1..8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define LPC32XX_KS_DATA(x, y)			((x) + 0x40 + ((y) << 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define LPC32XX_KSCAN_DEB_NUM_DEB_PASS(n)	((n) & 0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define LPC32XX_KSCAN_SCOND_IN_IDLE		0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define LPC32XX_KSCAN_SCOND_IN_SCANONCE		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define LPC32XX_KSCAN_SCOND_IN_IRQGEN		0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define LPC32XX_KSCAN_SCOND_IN_SCAN_MATRIX	0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define LPC32XX_KSCAN_IRQ_PENDING_CLR		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define LPC32XX_KSCAN_SCTRL_SCAN_DELAY(n)	((n) & 0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define LPC32XX_KSCAN_FTST_FORCESCANONCE	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define LPC32XX_KSCAN_FTST_USE32K_CLK		0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define LPC32XX_KSCAN_MSEL_SELECT(n)		((n) & 0xF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) struct lpc32xx_kscan_drv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	void __iomem *kscan_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	u32 matrix_sz;		/* Size of matrix in XxY, ie. 3 = 3x3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	u32 deb_clks;		/* Debounce clocks (based on 32KHz clock) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u32 scan_delay;		/* Scan delay (based on 32KHz clock) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	unsigned short *keymap;	/* Pointer to key map for the scan matrix */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned int row_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u8 lastkeystates[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static void lpc32xx_mod_states(struct lpc32xx_kscan_drv *kscandat, int col)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct input_dev *input = kscandat->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	unsigned row, changed, scancode, keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u8 key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	key = readl(LPC32XX_KS_DATA(kscandat->kscan_base, col));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	changed = key ^ kscandat->lastkeystates[col];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	kscandat->lastkeystates[col] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	for (row = 0; changed; row++, changed >>= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (changed & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			/* Key state changed, signal an event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			scancode = MATRIX_SCAN_CODE(row, col,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 						    kscandat->row_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			keycode = kscandat->keymap[scancode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			input_event(input, EV_MSC, MSC_SCAN, scancode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			input_report_key(input, keycode, key & (1 << row));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static irqreturn_t lpc32xx_kscan_irq(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct lpc32xx_kscan_drv *kscandat = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	for (i = 0; i < kscandat->matrix_sz; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		lpc32xx_mod_states(kscandat, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	input_sync(kscandat->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int lpc32xx_kscan_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	error = clk_prepare_enable(kscandat->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^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) static void lpc32xx_kscan_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	clk_disable_unprepare(kscandat->clk);
^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) static int lpc32xx_parse_dt(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				      struct lpc32xx_kscan_drv *kscandat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	u32 rows = 0, columns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	err = matrix_keypad_parse_properties(dev, &rows, &columns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (rows != columns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		dev_err(dev, "rows and columns must be equal!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	kscandat->matrix_sz = rows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	kscandat->row_shift = get_count_order(columns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	of_property_read_u32(np, "nxp,debounce-delay-ms", &kscandat->deb_clks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	of_property_read_u32(np, "nxp,scan-delay-ms", &kscandat->scan_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!kscandat->deb_clks || !kscandat->scan_delay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		dev_err(dev, "debounce or scan delay not specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int lpc32xx_kscan_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct lpc32xx_kscan_drv *kscandat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	size_t keymap_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		dev_err(&pdev->dev, "failed to get platform I/O memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	kscandat = devm_kzalloc(&pdev->dev, sizeof(*kscandat),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (!kscandat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	error = lpc32xx_parse_dt(&pdev->dev, kscandat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		dev_err(&pdev->dev, "failed to parse device tree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	keymap_size = sizeof(kscandat->keymap[0]) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				(kscandat->matrix_sz << kscandat->row_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	kscandat->keymap = devm_kzalloc(&pdev->dev, keymap_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!kscandat->keymap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	kscandat->input = input = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (!input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		dev_err(&pdev->dev, "failed to allocate input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	/* Setup key input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	input->name		= pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	input->phys		= "lpc32xx/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	input->id.vendor	= 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	input->id.product	= 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	input->id.version	= 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	input->open		= lpc32xx_kscan_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	input->close		= lpc32xx_kscan_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	input->dev.parent	= &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	input_set_capability(input, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	error = matrix_keypad_build_keymap(NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 					   kscandat->matrix_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 					   kscandat->matrix_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 					   kscandat->keymap, kscandat->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		dev_err(&pdev->dev, "failed to build keymap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	input_set_drvdata(kscandat->input, kscandat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	kscandat->kscan_base = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (IS_ERR(kscandat->kscan_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return PTR_ERR(kscandat->kscan_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/* Get the key scanner clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	kscandat->clk = devm_clk_get(&pdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (IS_ERR(kscandat->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		dev_err(&pdev->dev, "failed to get clock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return PTR_ERR(kscandat->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	/* Configure the key scanner */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	error = clk_prepare_enable(kscandat->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	writel(kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	writel(kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	writel(LPC32XX_KSCAN_FTST_USE32K_CLK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	       LPC32XX_KS_FAST_TST(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	writel(kscandat->matrix_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	       LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	clk_disable_unprepare(kscandat->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	error = devm_request_irq(&pdev->dev, irq, lpc32xx_kscan_irq, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				 pdev->name, kscandat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		dev_err(&pdev->dev, "failed to request irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	error = input_register_device(kscandat->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		dev_err(&pdev->dev, "failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	platform_set_drvdata(pdev, kscandat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static int lpc32xx_kscan_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct input_dev *input = kscandat->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	mutex_lock(&input->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (input->users) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		/* Clear IRQ and disable clock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		clk_disable_unprepare(kscandat->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	mutex_unlock(&input->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^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) static int lpc32xx_kscan_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct input_dev *input = kscandat->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	mutex_lock(&input->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (input->users) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		/* Enable clock and clear IRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		retval = clk_prepare_enable(kscandat->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if (retval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	mutex_unlock(&input->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			 lpc32xx_kscan_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static const struct of_device_id lpc32xx_kscan_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	{ .compatible = "nxp,lpc3220-key" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_DEVICE_TABLE(of, lpc32xx_kscan_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static struct platform_driver lpc32xx_kscan_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	.probe		= lpc32xx_kscan_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		.name	= DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		.pm	= &lpc32xx_kscan_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		.of_match_table = lpc32xx_kscan_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) module_platform_driver(lpc32xx_kscan_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_DESCRIPTION("Key scanner driver for LPC32XX devices");