12aec85b2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22aec85b2SThomas Gleixner // Copyright (C) 2014 Broadcom Corporation
30c7e67a9SScott Branden 
40c7e67a9SScott Branden #include <linux/bitops.h>
50c7e67a9SScott Branden #include <linux/clk.h>
60c7e67a9SScott Branden #include <linux/gfp.h>
70c7e67a9SScott Branden #include <linux/io.h>
80c7e67a9SScott Branden #include <linux/input.h>
90c7e67a9SScott Branden #include <linux/input/matrix_keypad.h>
100c7e67a9SScott Branden #include <linux/interrupt.h>
110c7e67a9SScott Branden #include <linux/module.h>
120c7e67a9SScott Branden #include <linux/of.h>
130c7e67a9SScott Branden #include <linux/platform_device.h>
140c7e67a9SScott Branden #include <linux/stddef.h>
150c7e67a9SScott Branden #include <linux/types.h>
160c7e67a9SScott Branden 
170c7e67a9SScott Branden #define DEFAULT_CLK_HZ			31250
180c7e67a9SScott Branden #define MAX_ROWS			8
190c7e67a9SScott Branden #define MAX_COLS			8
200c7e67a9SScott Branden 
210c7e67a9SScott Branden /* Register/field definitions */
220c7e67a9SScott Branden #define KPCR_OFFSET			0x00000080
230c7e67a9SScott Branden #define KPCR_MODE			0x00000002
240c7e67a9SScott Branden #define KPCR_MODE_SHIFT			1
250c7e67a9SScott Branden #define KPCR_MODE_MASK			1
260c7e67a9SScott Branden #define KPCR_ENABLE			0x00000001
270c7e67a9SScott Branden #define KPCR_STATUSFILTERENABLE		0x00008000
280c7e67a9SScott Branden #define KPCR_STATUSFILTERTYPE_SHIFT	12
290c7e67a9SScott Branden #define KPCR_COLFILTERENABLE		0x00000800
300c7e67a9SScott Branden #define KPCR_COLFILTERTYPE_SHIFT	8
310c7e67a9SScott Branden #define KPCR_ROWWIDTH_SHIFT		20
320c7e67a9SScott Branden #define KPCR_COLUMNWIDTH_SHIFT		16
330c7e67a9SScott Branden 
340c7e67a9SScott Branden #define KPIOR_OFFSET			0x00000084
350c7e67a9SScott Branden #define KPIOR_ROWOCONTRL_SHIFT		24
360c7e67a9SScott Branden #define KPIOR_ROWOCONTRL_MASK		0xFF000000
370c7e67a9SScott Branden #define KPIOR_COLUMNOCONTRL_SHIFT	16
380c7e67a9SScott Branden #define KPIOR_COLUMNOCONTRL_MASK	0x00FF0000
390c7e67a9SScott Branden #define KPIOR_COLUMN_IO_DATA_SHIFT	0
400c7e67a9SScott Branden 
410c7e67a9SScott Branden #define KPEMR0_OFFSET			0x00000090
420c7e67a9SScott Branden #define KPEMR1_OFFSET			0x00000094
430c7e67a9SScott Branden #define KPEMR2_OFFSET			0x00000098
440c7e67a9SScott Branden #define KPEMR3_OFFSET			0x0000009C
450c7e67a9SScott Branden #define KPEMR_EDGETYPE_BOTH		3
460c7e67a9SScott Branden 
470c7e67a9SScott Branden #define KPSSR0_OFFSET			0x000000A0
480c7e67a9SScott Branden #define KPSSR1_OFFSET			0x000000A4
490c7e67a9SScott Branden #define KPSSRN_OFFSET(reg_n)		(KPSSR0_OFFSET + 4 * (reg_n))
500c7e67a9SScott Branden #define KPIMR0_OFFSET			0x000000B0
510c7e67a9SScott Branden #define KPIMR1_OFFSET			0x000000B4
520c7e67a9SScott Branden #define KPICR0_OFFSET			0x000000B8
530c7e67a9SScott Branden #define KPICR1_OFFSET			0x000000BC
540c7e67a9SScott Branden #define KPICRN_OFFSET(reg_n)		(KPICR0_OFFSET + 4 * (reg_n))
550c7e67a9SScott Branden #define KPISR0_OFFSET			0x000000C0
560c7e67a9SScott Branden #define KPISR1_OFFSET			0x000000C4
570c7e67a9SScott Branden 
580c7e67a9SScott Branden #define KPCR_STATUSFILTERTYPE_MAX	7
590c7e67a9SScott Branden #define KPCR_COLFILTERTYPE_MAX		7
600c7e67a9SScott Branden 
610c7e67a9SScott Branden /* Macros to determine the row/column from a bit that is set in SSR0/1. */
620c7e67a9SScott Branden #define BIT_TO_ROW_SSRN(bit_nr, reg_n)	(((bit_nr) >> 3) + 4 * (reg_n))
630c7e67a9SScott Branden #define BIT_TO_COL(bit_nr)		((bit_nr) % 8)
640c7e67a9SScott Branden 
650c7e67a9SScott Branden /* Structure representing various run-time entities */
660c7e67a9SScott Branden struct bcm_kp {
670c7e67a9SScott Branden 	void __iomem *base;
680c7e67a9SScott Branden 	int irq;
690c7e67a9SScott Branden 	struct clk *clk;
700c7e67a9SScott Branden 	struct input_dev *input_dev;
710c7e67a9SScott Branden 	unsigned long last_state[2];
720c7e67a9SScott Branden 	unsigned int n_rows;
730c7e67a9SScott Branden 	unsigned int n_cols;
740c7e67a9SScott Branden 	u32 kpcr;
750c7e67a9SScott Branden 	u32 kpior;
760c7e67a9SScott Branden 	u32 kpemr;
770c7e67a9SScott Branden 	u32 imr0_val;
780c7e67a9SScott Branden 	u32 imr1_val;
790c7e67a9SScott Branden };
800c7e67a9SScott Branden 
810c7e67a9SScott Branden /*
820c7e67a9SScott Branden  * Returns the keycode from the input device keymap given the row and
830c7e67a9SScott Branden  * column.
840c7e67a9SScott Branden  */
bcm_kp_get_keycode(struct bcm_kp * kp,int row,int col)850c7e67a9SScott Branden static int bcm_kp_get_keycode(struct bcm_kp *kp, int row, int col)
860c7e67a9SScott Branden {
870c7e67a9SScott Branden 	unsigned int row_shift = get_count_order(kp->n_cols);
880c7e67a9SScott Branden 	unsigned short *keymap = kp->input_dev->keycode;
890c7e67a9SScott Branden 
900c7e67a9SScott Branden 	return keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
910c7e67a9SScott Branden }
920c7e67a9SScott Branden 
bcm_kp_report_keys(struct bcm_kp * kp,int reg_num,int pull_mode)930c7e67a9SScott Branden static void bcm_kp_report_keys(struct bcm_kp *kp, int reg_num, int pull_mode)
940c7e67a9SScott Branden {
950c7e67a9SScott Branden 	unsigned long state, change;
960c7e67a9SScott Branden 	int bit_nr;
970c7e67a9SScott Branden 	int key_press;
980c7e67a9SScott Branden 	int row, col;
990c7e67a9SScott Branden 	unsigned int keycode;
1000c7e67a9SScott Branden 
1010c7e67a9SScott Branden 	/* Clear interrupts */
1020c7e67a9SScott Branden 	writel(0xFFFFFFFF, kp->base + KPICRN_OFFSET(reg_num));
1030c7e67a9SScott Branden 
1040c7e67a9SScott Branden 	state = readl(kp->base + KPSSRN_OFFSET(reg_num));
1050c7e67a9SScott Branden 	change = kp->last_state[reg_num] ^ state;
1060c7e67a9SScott Branden 	kp->last_state[reg_num] = state;
1070c7e67a9SScott Branden 
1080c7e67a9SScott Branden 	for_each_set_bit(bit_nr, &change, BITS_PER_LONG) {
1090c7e67a9SScott Branden 		key_press = state & BIT(bit_nr);
1100c7e67a9SScott Branden 		/* The meaning of SSR register depends on pull mode. */
1110c7e67a9SScott Branden 		key_press = pull_mode ? !key_press : key_press;
1120c7e67a9SScott Branden 		row = BIT_TO_ROW_SSRN(bit_nr, reg_num);
1130c7e67a9SScott Branden 		col = BIT_TO_COL(bit_nr);
1140c7e67a9SScott Branden 		keycode = bcm_kp_get_keycode(kp, row, col);
1150c7e67a9SScott Branden 		input_report_key(kp->input_dev, keycode, key_press);
1160c7e67a9SScott Branden 	}
1170c7e67a9SScott Branden }
1180c7e67a9SScott Branden 
bcm_kp_isr_thread(int irq,void * dev_id)1190c7e67a9SScott Branden static irqreturn_t bcm_kp_isr_thread(int irq, void *dev_id)
1200c7e67a9SScott Branden {
1210c7e67a9SScott Branden 	struct bcm_kp *kp = dev_id;
1220c7e67a9SScott Branden 	int pull_mode = (kp->kpcr >> KPCR_MODE_SHIFT) & KPCR_MODE_MASK;
1230c7e67a9SScott Branden 	int reg_num;
1240c7e67a9SScott Branden 
1250c7e67a9SScott Branden 	for (reg_num = 0; reg_num <= 1; reg_num++)
1260c7e67a9SScott Branden 		bcm_kp_report_keys(kp, reg_num, pull_mode);
1270c7e67a9SScott Branden 
1280c7e67a9SScott Branden 	input_sync(kp->input_dev);
1290c7e67a9SScott Branden 
1300c7e67a9SScott Branden 	return IRQ_HANDLED;
1310c7e67a9SScott Branden }
1320c7e67a9SScott Branden 
bcm_kp_start(struct bcm_kp * kp)1330c7e67a9SScott Branden static int bcm_kp_start(struct bcm_kp *kp)
1340c7e67a9SScott Branden {
1350c7e67a9SScott Branden 	int error;
1360c7e67a9SScott Branden 
1370c7e67a9SScott Branden 	if (kp->clk) {
1380c7e67a9SScott Branden 		error = clk_prepare_enable(kp->clk);
1390c7e67a9SScott Branden 		if (error)
1400c7e67a9SScott Branden 			return error;
1410c7e67a9SScott Branden 	}
1420c7e67a9SScott Branden 
1430c7e67a9SScott Branden 	writel(kp->kpior, kp->base + KPIOR_OFFSET);
1440c7e67a9SScott Branden 
1450c7e67a9SScott Branden 	writel(kp->imr0_val, kp->base + KPIMR0_OFFSET);
1460c7e67a9SScott Branden 	writel(kp->imr1_val, kp->base + KPIMR1_OFFSET);
1470c7e67a9SScott Branden 
1480c7e67a9SScott Branden 	writel(kp->kpemr, kp->base + KPEMR0_OFFSET);
1490c7e67a9SScott Branden 	writel(kp->kpemr, kp->base + KPEMR1_OFFSET);
1500c7e67a9SScott Branden 	writel(kp->kpemr, kp->base + KPEMR2_OFFSET);
1510c7e67a9SScott Branden 	writel(kp->kpemr, kp->base + KPEMR3_OFFSET);
1520c7e67a9SScott Branden 
1530c7e67a9SScott Branden 	writel(0xFFFFFFFF, kp->base + KPICR0_OFFSET);
1540c7e67a9SScott Branden 	writel(0xFFFFFFFF, kp->base + KPICR1_OFFSET);
1550c7e67a9SScott Branden 
1560c7e67a9SScott Branden 	kp->last_state[0] = readl(kp->base + KPSSR0_OFFSET);
1570c7e67a9SScott Branden 	kp->last_state[0] = readl(kp->base + KPSSR1_OFFSET);
1580c7e67a9SScott Branden 
1590c7e67a9SScott Branden 	writel(kp->kpcr | KPCR_ENABLE, kp->base + KPCR_OFFSET);
1600c7e67a9SScott Branden 
1610c7e67a9SScott Branden 	return 0;
1620c7e67a9SScott Branden }
1630c7e67a9SScott Branden 
bcm_kp_stop(const struct bcm_kp * kp)1640c7e67a9SScott Branden static void bcm_kp_stop(const struct bcm_kp *kp)
1650c7e67a9SScott Branden {
1660c7e67a9SScott Branden 	u32 val;
1670c7e67a9SScott Branden 
1680c7e67a9SScott Branden 	val = readl(kp->base + KPCR_OFFSET);
1690c7e67a9SScott Branden 	val &= ~KPCR_ENABLE;
1700c7e67a9SScott Branden 	writel(0, kp->base + KPCR_OFFSET);
1710c7e67a9SScott Branden 	writel(0, kp->base + KPIMR0_OFFSET);
1720c7e67a9SScott Branden 	writel(0, kp->base + KPIMR1_OFFSET);
1730c7e67a9SScott Branden 	writel(0xFFFFFFFF, kp->base + KPICR0_OFFSET);
1740c7e67a9SScott Branden 	writel(0xFFFFFFFF, kp->base + KPICR1_OFFSET);
1750c7e67a9SScott Branden 
1760c7e67a9SScott Branden 	clk_disable_unprepare(kp->clk);
1770c7e67a9SScott Branden }
1780c7e67a9SScott Branden 
bcm_kp_open(struct input_dev * dev)1790c7e67a9SScott Branden static int bcm_kp_open(struct input_dev *dev)
1800c7e67a9SScott Branden {
1810c7e67a9SScott Branden 	struct bcm_kp *kp = input_get_drvdata(dev);
1820c7e67a9SScott Branden 
1830c7e67a9SScott Branden 	return bcm_kp_start(kp);
1840c7e67a9SScott Branden }
1850c7e67a9SScott Branden 
bcm_kp_close(struct input_dev * dev)1860c7e67a9SScott Branden static void bcm_kp_close(struct input_dev *dev)
1870c7e67a9SScott Branden {
1880c7e67a9SScott Branden 	struct bcm_kp *kp = input_get_drvdata(dev);
1890c7e67a9SScott Branden 
1900c7e67a9SScott Branden 	bcm_kp_stop(kp);
1910c7e67a9SScott Branden }
1920c7e67a9SScott Branden 
bcm_kp_matrix_key_parse_dt(struct bcm_kp * kp)1930c7e67a9SScott Branden static int bcm_kp_matrix_key_parse_dt(struct bcm_kp *kp)
1940c7e67a9SScott Branden {
1950c7e67a9SScott Branden 	struct device *dev = kp->input_dev->dev.parent;
1960c7e67a9SScott Branden 	struct device_node *np = dev->of_node;
1970c7e67a9SScott Branden 	int error;
1980c7e67a9SScott Branden 	unsigned int dt_val;
1990c7e67a9SScott Branden 	unsigned int i;
2000c7e67a9SScott Branden 	unsigned int num_rows, col_mask, rows_set;
2010c7e67a9SScott Branden 
2020c7e67a9SScott Branden 	/* Initialize the KPCR Keypad Configuration Register */
2030c7e67a9SScott Branden 	kp->kpcr = KPCR_STATUSFILTERENABLE | KPCR_COLFILTERENABLE;
2040c7e67a9SScott Branden 
205aef01aadSDmitry Torokhov 	error = matrix_keypad_parse_properties(dev, &kp->n_rows, &kp->n_cols);
2060c7e67a9SScott Branden 	if (error) {
2070c7e67a9SScott Branden 		dev_err(dev, "failed to parse kp params\n");
2080c7e67a9SScott Branden 		return error;
2090c7e67a9SScott Branden 	}
2100c7e67a9SScott Branden 
2110c7e67a9SScott Branden 	/* Set row width for the ASIC block. */
2120c7e67a9SScott Branden 	kp->kpcr |= (kp->n_rows - 1) << KPCR_ROWWIDTH_SHIFT;
2130c7e67a9SScott Branden 
2140c7e67a9SScott Branden 	/* Set column width for the ASIC block. */
2150c7e67a9SScott Branden 	kp->kpcr |= (kp->n_cols - 1) << KPCR_COLUMNWIDTH_SHIFT;
2160c7e67a9SScott Branden 
2170c7e67a9SScott Branden 	/* Configure the IMR registers */
2180c7e67a9SScott Branden 
2190c7e67a9SScott Branden 	/*
2200c7e67a9SScott Branden 	 * IMR registers contain interrupt enable bits for 8x8 matrix
2210c7e67a9SScott Branden 	 * IMR0 register format: <row3> <row2> <row1> <row0>
2220c7e67a9SScott Branden 	 * IMR1 register format: <row7> <row6> <row5> <row4>
2230c7e67a9SScott Branden 	 */
2240c7e67a9SScott Branden 	col_mask = (1 << (kp->n_cols)) - 1;
2250c7e67a9SScott Branden 	num_rows = kp->n_rows;
2260c7e67a9SScott Branden 
2270c7e67a9SScott Branden 	/* Set column bits in rows 0 to 3 in IMR0 */
2280c7e67a9SScott Branden 	kp->imr0_val = col_mask;
2290c7e67a9SScott Branden 
2300c7e67a9SScott Branden 	rows_set = 1;
2310c7e67a9SScott Branden 	while (--num_rows && rows_set++ < 4)
2320c7e67a9SScott Branden 		kp->imr0_val |= kp->imr0_val << MAX_COLS;
2330c7e67a9SScott Branden 
2340c7e67a9SScott Branden 	/* Set column bits in rows 4 to 7 in IMR1 */
2350c7e67a9SScott Branden 	kp->imr1_val = 0;
2360c7e67a9SScott Branden 	if (num_rows) {
2370c7e67a9SScott Branden 		kp->imr1_val = col_mask;
2380c7e67a9SScott Branden 		while (--num_rows)
2390c7e67a9SScott Branden 			kp->imr1_val |= kp->imr1_val << MAX_COLS;
2400c7e67a9SScott Branden 	}
2410c7e67a9SScott Branden 
2420c7e67a9SScott Branden 	/* Initialize the KPEMR Keypress Edge Mode Registers */
2430c7e67a9SScott Branden 	/* Trigger on both edges */
2440c7e67a9SScott Branden 	kp->kpemr = 0;
2450c7e67a9SScott Branden 	for (i = 0; i <= 30; i += 2)
2460c7e67a9SScott Branden 		kp->kpemr |= (KPEMR_EDGETYPE_BOTH << i);
2470c7e67a9SScott Branden 
2480c7e67a9SScott Branden 	/*
2490c7e67a9SScott Branden 	 * Obtain the Status filter debounce value and verify against the
2500c7e67a9SScott Branden 	 * possible values specified in the DT binding.
2510c7e67a9SScott Branden 	 */
2520c7e67a9SScott Branden 	of_property_read_u32(np, "status-debounce-filter-period", &dt_val);
2530c7e67a9SScott Branden 
2540c7e67a9SScott Branden 	if (dt_val > KPCR_STATUSFILTERTYPE_MAX) {
2550c7e67a9SScott Branden 		dev_err(dev, "Invalid Status filter debounce value %d\n",
2560c7e67a9SScott Branden 			dt_val);
2570c7e67a9SScott Branden 		return -EINVAL;
2580c7e67a9SScott Branden 	}
2590c7e67a9SScott Branden 
2600c7e67a9SScott Branden 	kp->kpcr |= dt_val << KPCR_STATUSFILTERTYPE_SHIFT;
2610c7e67a9SScott Branden 
2620c7e67a9SScott Branden 	/*
2630c7e67a9SScott Branden 	 * Obtain the Column filter debounce value and verify against the
2640c7e67a9SScott Branden 	 * possible values specified in the DT binding.
2650c7e67a9SScott Branden 	 */
2660c7e67a9SScott Branden 	of_property_read_u32(np, "col-debounce-filter-period", &dt_val);
2670c7e67a9SScott Branden 
2680c7e67a9SScott Branden 	if (dt_val > KPCR_COLFILTERTYPE_MAX) {
2690c7e67a9SScott Branden 		dev_err(dev, "Invalid Column filter debounce value %d\n",
2700c7e67a9SScott Branden 			dt_val);
2710c7e67a9SScott Branden 		return -EINVAL;
2720c7e67a9SScott Branden 	}
2730c7e67a9SScott Branden 
2740c7e67a9SScott Branden 	kp->kpcr |= dt_val << KPCR_COLFILTERTYPE_SHIFT;
2750c7e67a9SScott Branden 
2760c7e67a9SScott Branden 	/*
2770c7e67a9SScott Branden 	 * Determine between the row and column,
2780c7e67a9SScott Branden 	 * which should be configured as output.
2790c7e67a9SScott Branden 	 */
2800c7e67a9SScott Branden 	if (of_property_read_bool(np, "row-output-enabled")) {
2810c7e67a9SScott Branden 		/*
2820c7e67a9SScott Branden 		* Set RowOContrl or ColumnOContrl in KPIOR
2830c7e67a9SScott Branden 		* to the number of pins to drive as outputs
2840c7e67a9SScott Branden 		*/
2850c7e67a9SScott Branden 		kp->kpior = ((1 << kp->n_rows) - 1) <<
2860c7e67a9SScott Branden 				KPIOR_ROWOCONTRL_SHIFT;
2870c7e67a9SScott Branden 	} else {
2880c7e67a9SScott Branden 		kp->kpior = ((1 << kp->n_cols) - 1) <<
2890c7e67a9SScott Branden 				KPIOR_COLUMNOCONTRL_SHIFT;
2900c7e67a9SScott Branden 	}
2910c7e67a9SScott Branden 
2920c7e67a9SScott Branden 	/*
2930c7e67a9SScott Branden 	 * Determine if the scan pull up needs to be enabled
2940c7e67a9SScott Branden 	 */
2950c7e67a9SScott Branden 	if (of_property_read_bool(np, "pull-up-enabled"))
2960c7e67a9SScott Branden 		kp->kpcr |= KPCR_MODE;
2970c7e67a9SScott Branden 
2980c7e67a9SScott Branden 	dev_dbg(dev, "n_rows=%d n_col=%d kpcr=%x kpior=%x kpemr=%x\n",
2990c7e67a9SScott Branden 		kp->n_rows, kp->n_cols,
3000c7e67a9SScott Branden 		kp->kpcr, kp->kpior, kp->kpemr);
3010c7e67a9SScott Branden 
3020c7e67a9SScott Branden 	return 0;
3030c7e67a9SScott Branden }
3040c7e67a9SScott Branden 
3050c7e67a9SScott Branden 
bcm_kp_probe(struct platform_device * pdev)3060c7e67a9SScott Branden static int bcm_kp_probe(struct platform_device *pdev)
3070c7e67a9SScott Branden {
3080c7e67a9SScott Branden 	struct bcm_kp *kp;
3090c7e67a9SScott Branden 	struct input_dev *input_dev;
3100c7e67a9SScott Branden 	int error;
3110c7e67a9SScott Branden 
3120c7e67a9SScott Branden 	kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
3130c7e67a9SScott Branden 	if (!kp)
3140c7e67a9SScott Branden 		return -ENOMEM;
3150c7e67a9SScott Branden 
3160c7e67a9SScott Branden 	input_dev = devm_input_allocate_device(&pdev->dev);
3170c7e67a9SScott Branden 	if (!input_dev) {
3180c7e67a9SScott Branden 		dev_err(&pdev->dev, "failed to allocate the input device\n");
3190c7e67a9SScott Branden 		return -ENOMEM;
3200c7e67a9SScott Branden 	}
3210c7e67a9SScott Branden 
3220c7e67a9SScott Branden 	__set_bit(EV_KEY, input_dev->evbit);
3230c7e67a9SScott Branden 
3240c7e67a9SScott Branden 	/* Enable auto repeat feature of Linux input subsystem */
3250c7e67a9SScott Branden 	if (of_property_read_bool(pdev->dev.of_node, "autorepeat"))
3260c7e67a9SScott Branden 		__set_bit(EV_REP, input_dev->evbit);
3270c7e67a9SScott Branden 
3280c7e67a9SScott Branden 	input_dev->name = pdev->name;
3290c7e67a9SScott Branden 	input_dev->phys = "keypad/input0";
3300c7e67a9SScott Branden 	input_dev->dev.parent = &pdev->dev;
3310c7e67a9SScott Branden 	input_dev->open = bcm_kp_open;
3320c7e67a9SScott Branden 	input_dev->close = bcm_kp_close;
3330c7e67a9SScott Branden 
3340c7e67a9SScott Branden 	input_dev->id.bustype = BUS_HOST;
3350c7e67a9SScott Branden 	input_dev->id.vendor = 0x0001;
3360c7e67a9SScott Branden 	input_dev->id.product = 0x0001;
3370c7e67a9SScott Branden 	input_dev->id.version = 0x0100;
3380c7e67a9SScott Branden 
3390c7e67a9SScott Branden 	input_set_drvdata(input_dev, kp);
3400c7e67a9SScott Branden 
3410c7e67a9SScott Branden 	kp->input_dev = input_dev;
3420c7e67a9SScott Branden 
3430c7e67a9SScott Branden 	error = bcm_kp_matrix_key_parse_dt(kp);
3440c7e67a9SScott Branden 	if (error)
3450c7e67a9SScott Branden 		return error;
3460c7e67a9SScott Branden 
3470c7e67a9SScott Branden 	error = matrix_keypad_build_keymap(NULL, NULL,
3480c7e67a9SScott Branden 					   kp->n_rows, kp->n_cols,
3490c7e67a9SScott Branden 					   NULL, input_dev);
3500c7e67a9SScott Branden 	if (error) {
3510c7e67a9SScott Branden 		dev_err(&pdev->dev, "failed to build keymap\n");
3520c7e67a9SScott Branden 		return error;
3530c7e67a9SScott Branden 	}
3540c7e67a9SScott Branden 
3555abb3241SYangtao Li 	kp->base = devm_platform_ioremap_resource(pdev, 0);
3560c7e67a9SScott Branden 	if (IS_ERR(kp->base))
3570c7e67a9SScott Branden 		return PTR_ERR(kp->base);
3580c7e67a9SScott Branden 
3590c7e67a9SScott Branden 	/* Enable clock */
360a2c795b6SKrzysztof Kozlowski 	kp->clk = devm_clk_get_optional(&pdev->dev, "peri_clk");
3610c7e67a9SScott Branden 	if (IS_ERR(kp->clk)) {
362*0b64150cSKrzysztof Kozlowski 		return dev_err_probe(&pdev->dev, PTR_ERR(kp->clk), "Failed to get clock\n");
363a2c795b6SKrzysztof Kozlowski 	} else if (!kp->clk) {
364a2c795b6SKrzysztof Kozlowski 		dev_dbg(&pdev->dev, "No clock specified. Assuming it's enabled\n");
3650c7e67a9SScott Branden 	} else {
3660c7e67a9SScott Branden 		unsigned int desired_rate;
3670c7e67a9SScott Branden 		long actual_rate;
3680c7e67a9SScott Branden 
3690c7e67a9SScott Branden 		error = of_property_read_u32(pdev->dev.of_node,
3700c7e67a9SScott Branden 					     "clock-frequency", &desired_rate);
3710c7e67a9SScott Branden 		if (error < 0)
3720c7e67a9SScott Branden 			desired_rate = DEFAULT_CLK_HZ;
3730c7e67a9SScott Branden 
3740c7e67a9SScott Branden 		actual_rate = clk_round_rate(kp->clk, desired_rate);
3750c7e67a9SScott Branden 		if (actual_rate <= 0)
3760c7e67a9SScott Branden 			return -EINVAL;
3770c7e67a9SScott Branden 
3780c7e67a9SScott Branden 		error = clk_set_rate(kp->clk, actual_rate);
3790c7e67a9SScott Branden 		if (error)
3800c7e67a9SScott Branden 			return error;
3810c7e67a9SScott Branden 
3820c7e67a9SScott Branden 		error = clk_prepare_enable(kp->clk);
3830c7e67a9SScott Branden 		if (error)
3840c7e67a9SScott Branden 			return error;
3850c7e67a9SScott Branden 	}
3860c7e67a9SScott Branden 
3870c7e67a9SScott Branden 	/* Put the kp into a known sane state */
3880c7e67a9SScott Branden 	bcm_kp_stop(kp);
3890c7e67a9SScott Branden 
3900c7e67a9SScott Branden 	kp->irq = platform_get_irq(pdev, 0);
3910bec8b7eSStephen Boyd 	if (kp->irq < 0)
3920c7e67a9SScott Branden 		return -EINVAL;
3930c7e67a9SScott Branden 
3940c7e67a9SScott Branden 	error = devm_request_threaded_irq(&pdev->dev, kp->irq,
3950c7e67a9SScott Branden 					  NULL, bcm_kp_isr_thread,
3960c7e67a9SScott Branden 					  IRQF_ONESHOT, pdev->name, kp);
3970c7e67a9SScott Branden 	if (error) {
3980c7e67a9SScott Branden 		dev_err(&pdev->dev, "failed to request IRQ\n");
3990c7e67a9SScott Branden 		return error;
4000c7e67a9SScott Branden 	}
4010c7e67a9SScott Branden 
4020c7e67a9SScott Branden 	error = input_register_device(input_dev);
4030c7e67a9SScott Branden 	if (error) {
4040c7e67a9SScott Branden 		dev_err(&pdev->dev, "failed to register input device\n");
4050c7e67a9SScott Branden 		return error;
4060c7e67a9SScott Branden 	}
4070c7e67a9SScott Branden 
4080c7e67a9SScott Branden 	return 0;
4090c7e67a9SScott Branden }
4100c7e67a9SScott Branden 
4110c7e67a9SScott Branden static const struct of_device_id bcm_kp_of_match[] = {
4120c7e67a9SScott Branden 	{ .compatible = "brcm,bcm-keypad" },
4130c7e67a9SScott Branden 	{ },
4140c7e67a9SScott Branden };
4150c7e67a9SScott Branden MODULE_DEVICE_TABLE(of, bcm_kp_of_match);
4160c7e67a9SScott Branden 
4170c7e67a9SScott Branden static struct platform_driver bcm_kp_device_driver = {
4180c7e67a9SScott Branden 	.probe		= bcm_kp_probe,
4190c7e67a9SScott Branden 	.driver		= {
4200c7e67a9SScott Branden 		.name	= "bcm-keypad",
4210c7e67a9SScott Branden 		.of_match_table = of_match_ptr(bcm_kp_of_match),
4220c7e67a9SScott Branden 	}
4230c7e67a9SScott Branden };
4240c7e67a9SScott Branden 
4250c7e67a9SScott Branden module_platform_driver(bcm_kp_device_driver);
4260c7e67a9SScott Branden 
4270c7e67a9SScott Branden MODULE_AUTHOR("Broadcom Corporation");
4280c7e67a9SScott Branden MODULE_DESCRIPTION("BCM Keypad Driver");
4290c7e67a9SScott Branden MODULE_LICENSE("GPL v2");
430