xref: /linux/drivers/input/keyboard/pxa27x_keypad.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20e5f11aaSEric Miao /*
30e5f11aaSEric Miao  * linux/drivers/input/keyboard/pxa27x_keypad.c
40e5f11aaSEric Miao  *
50e5f11aaSEric Miao  * Driver for the pxa27x matrix keyboard controller.
60e5f11aaSEric Miao  *
70e5f11aaSEric Miao  * Created:	Feb 22, 2007
80e5f11aaSEric Miao  * Author:	Rodolfo Giometti <giometti@linux.it>
90e5f11aaSEric Miao  *
100e5f11aaSEric Miao  * Based on a previous implementations by Kevin O'Connor
110e5f11aaSEric Miao  * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
122f82af08SNicolas Pitre  * on some suggestions by Nicolas Pitre <nico@fluxnic.net>.
130e5f11aaSEric Miao  */
140e5f11aaSEric Miao 
150e5f11aaSEric Miao 
160e5f11aaSEric Miao #include <linux/kernel.h>
170e5f11aaSEric Miao #include <linux/module.h>
180e5f11aaSEric Miao #include <linux/interrupt.h>
190e5f11aaSEric Miao #include <linux/input.h>
20d0542677SRob Herring #include <linux/io.h>
210e5f11aaSEric Miao #include <linux/device.h>
220e5f11aaSEric Miao #include <linux/platform_device.h>
230e5f11aaSEric Miao #include <linux/clk.h>
240e5f11aaSEric Miao #include <linux/err.h>
2552ec7752SDmitry Torokhov #include <linux/input/matrix_keypad.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
2795ba7407SSachin Kamat #include <linux/of.h>
280e5f11aaSEric Miao 
29293b2da1SArnd Bergmann #include <linux/platform_data/keypad-pxa27x.h>
309c60debdSEric Miao /*
319c60debdSEric Miao  * Keypad Controller registers
329c60debdSEric Miao  */
339c60debdSEric Miao #define KPC             0x0000 /* Keypad Control register */
349c60debdSEric Miao #define KPDK            0x0008 /* Keypad Direct Key register */
359c60debdSEric Miao #define KPREC           0x0010 /* Keypad Rotary Encoder register */
369c60debdSEric Miao #define KPMK            0x0018 /* Keypad Matrix Key register */
379c60debdSEric Miao #define KPAS            0x0020 /* Keypad Automatic Scan register */
380e5f11aaSEric Miao 
399c60debdSEric Miao /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
409c60debdSEric Miao #define KPASMKP0        0x0028
419c60debdSEric Miao #define KPASMKP1        0x0030
429c60debdSEric Miao #define KPASMKP2        0x0038
439c60debdSEric Miao #define KPASMKP3        0x0040
449c60debdSEric Miao #define KPKDI           0x0048
459c60debdSEric Miao 
469c60debdSEric Miao /* bit definitions */
47688dad4fSSamuel Ortiz #define KPC_MKRN(n)	((((n) - 1) & 0x7) << 26) /* matrix key row number */
48688dad4fSSamuel Ortiz #define KPC_MKCN(n)	((((n) - 1) & 0x7) << 23) /* matrix key column number */
49688dad4fSSamuel Ortiz #define KPC_DKN(n)	((((n) - 1) & 0x7) << 6)  /* direct key number */
50d7416f9eSEric Miao 
519c60debdSEric Miao #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
529c60debdSEric Miao #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
539c60debdSEric Miao #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
549c60debdSEric Miao #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
559c60debdSEric Miao 
569c60debdSEric Miao #define KPC_MS(n)	(0x1 << (13 + (n)))	/* Matrix scan line 'n' */
579c60debdSEric Miao #define KPC_MS_ALL      (0xff << 13)
589c60debdSEric Miao 
599c60debdSEric Miao #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
609c60debdSEric Miao #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
619c60debdSEric Miao #define KPC_DK_DEB_SEL	(0x1 <<  9)  /* Direct Keypad Debounce Select */
629c60debdSEric Miao #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
639c60debdSEric Miao #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
649c60debdSEric Miao #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
659c60debdSEric Miao #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
669c60debdSEric Miao #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
679c60debdSEric Miao #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
689c60debdSEric Miao 
6962059d9eSEric Miao #define KPDK_DKP        (0x1 << 31)
7062059d9eSEric Miao #define KPDK_DK(n)	((n) & 0xff)
7162059d9eSEric Miao 
729c60debdSEric Miao #define KPREC_OF1       (0x1 << 31)
739c60debdSEric Miao #define kPREC_UF1       (0x1 << 30)
749c60debdSEric Miao #define KPREC_OF0       (0x1 << 15)
759c60debdSEric Miao #define KPREC_UF0       (0x1 << 14)
769c60debdSEric Miao 
779c60debdSEric Miao #define KPREC_RECOUNT0(n)	((n) & 0xff)
789c60debdSEric Miao #define KPREC_RECOUNT1(n)	(((n) >> 16) & 0xff)
799c60debdSEric Miao 
809c60debdSEric Miao #define KPMK_MKP        (0x1 << 31)
819c60debdSEric Miao #define KPAS_SO         (0x1 << 31)
829c60debdSEric Miao #define KPASMKPx_SO     (0x1 << 31)
839c60debdSEric Miao 
841814db69SEric Miao #define KPAS_MUKP(n)	(((n) >> 26) & 0x1f)
851814db69SEric Miao #define KPAS_RP(n)	(((n) >> 4) & 0xf)
861814db69SEric Miao #define KPAS_CP(n)	((n) & 0xf)
870e5f11aaSEric Miao 
881814db69SEric Miao #define KPASMKP_MKC_MASK	(0xff)
891814db69SEric Miao 
909c60debdSEric Miao #define keypad_readl(off)	__raw_readl(keypad->mmio_base + (off))
919c60debdSEric Miao #define keypad_writel(off, v)	__raw_writel((v), keypad->mmio_base + (off))
929c60debdSEric Miao 
93bd96f378SDmitry Torokhov #define MAX_MATRIX_KEY_NUM	(MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
94bd96f378SDmitry Torokhov #define MAX_KEYPAD_KEYS		(MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
951814db69SEric Miao 
961814db69SEric Miao struct pxa27x_keypad {
979eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata;
981814db69SEric Miao 
991814db69SEric Miao 	struct clk *clk;
1001814db69SEric Miao 	struct input_dev *input_dev;
1019c60debdSEric Miao 	void __iomem *mmio_base;
1021814db69SEric Miao 
10339ab9ddeSEric Miao 	int irq;
10439ab9ddeSEric Miao 
105bd96f378SDmitry Torokhov 	unsigned short keycodes[MAX_KEYPAD_KEYS];
106bd96f378SDmitry Torokhov 	int rotary_rel_code[2];
1071814db69SEric Miao 
108c5ec1948SChao Xie 	unsigned int row_shift;
109c5ec1948SChao Xie 
1101814db69SEric Miao 	/* state row bits of each column scan */
1111814db69SEric Miao 	uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
11262059d9eSEric Miao 	uint32_t direct_key_state;
11362059d9eSEric Miao 
11462059d9eSEric Miao 	unsigned int direct_key_mask;
1151814db69SEric Miao };
1161814db69SEric Miao 
117e4156979SChao Xie #ifdef CONFIG_OF
pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad * keypad,struct pxa27x_keypad_platform_data * pdata)1189eb52139SDmitry Torokhov static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad,
1199eb52139SDmitry Torokhov 				struct pxa27x_keypad_platform_data *pdata)
120e4156979SChao Xie {
121e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
122e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
123e4156979SChao Xie 	u32 rows, cols;
124e4156979SChao Xie 	int error;
125e4156979SChao Xie 
126aef01aadSDmitry Torokhov 	error = matrix_keypad_parse_properties(dev, &rows, &cols);
127e4156979SChao Xie 	if (error)
128e4156979SChao Xie 		return error;
129e4156979SChao Xie 
130e4156979SChao Xie 	if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
131e4156979SChao Xie 		dev_err(dev, "rows or cols exceeds maximum value\n");
132e4156979SChao Xie 		return -EINVAL;
133e4156979SChao Xie 	}
134e4156979SChao Xie 
135e4156979SChao Xie 	pdata->matrix_key_rows = rows;
136e4156979SChao Xie 	pdata->matrix_key_cols = cols;
137e4156979SChao Xie 
138e4156979SChao Xie 	error = matrix_keypad_build_keymap(NULL, NULL,
139e4156979SChao Xie 					   pdata->matrix_key_rows,
140e4156979SChao Xie 					   pdata->matrix_key_cols,
141e4156979SChao Xie 					   keypad->keycodes, input_dev);
142e4156979SChao Xie 	if (error)
143e4156979SChao Xie 		return error;
144e4156979SChao Xie 
145e4156979SChao Xie 	return 0;
146e4156979SChao Xie }
147e4156979SChao Xie 
pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad * keypad,struct pxa27x_keypad_platform_data * pdata)1489eb52139SDmitry Torokhov static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad,
1499eb52139SDmitry Torokhov 				struct pxa27x_keypad_platform_data *pdata)
150e4156979SChao Xie {
151e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
152e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
153e4156979SChao Xie 	struct device_node *np = dev->of_node;
154e4156979SChao Xie 	const __be16 *prop;
155e4156979SChao Xie 	unsigned short code;
156e4156979SChao Xie 	unsigned int proplen, size;
157e4156979SChao Xie 	int i;
158e4156979SChao Xie 	int error;
159e4156979SChao Xie 
160e4156979SChao Xie 	error = of_property_read_u32(np, "marvell,direct-key-count",
161e4156979SChao Xie 				     &pdata->direct_key_num);
162e4156979SChao Xie 	if (error) {
163e4156979SChao Xie 		/*
164e4156979SChao Xie 		 * If do not have marvel,direct-key-count defined,
165e4156979SChao Xie 		 * it means direct key is not supported.
166e4156979SChao Xie 		 */
167e4156979SChao Xie 		return error == -EINVAL ? 0 : error;
168e4156979SChao Xie 	}
169e4156979SChao Xie 
170e4156979SChao Xie 	error = of_property_read_u32(np, "marvell,direct-key-mask",
171e4156979SChao Xie 				     &pdata->direct_key_mask);
172e4156979SChao Xie 	if (error) {
173e4156979SChao Xie 		if (error != -EINVAL)
174e4156979SChao Xie 			return error;
175e4156979SChao Xie 
176e4156979SChao Xie 		/*
177e4156979SChao Xie 		 * If marvell,direct-key-mask is not defined, driver will use
178e4156979SChao Xie 		 * default value. Default value is set when configure the keypad.
179e4156979SChao Xie 		 */
180e4156979SChao Xie 		pdata->direct_key_mask = 0;
181e4156979SChao Xie 	}
182e4156979SChao Xie 
183e4156979SChao Xie 	pdata->direct_key_low_active = of_property_read_bool(np,
184e4156979SChao Xie 					"marvell,direct-key-low-active");
185e4156979SChao Xie 
186e4156979SChao Xie 	prop = of_get_property(np, "marvell,direct-key-map", &proplen);
187e4156979SChao Xie 	if (!prop)
188e4156979SChao Xie 		return -EINVAL;
189e4156979SChao Xie 
190e4156979SChao Xie 	if (proplen % sizeof(u16))
191e4156979SChao Xie 		return -EINVAL;
192e4156979SChao Xie 
193e4156979SChao Xie 	size = proplen / sizeof(u16);
194e4156979SChao Xie 
195e4156979SChao Xie 	/* Only MAX_DIRECT_KEY_NUM is accepted.*/
196e4156979SChao Xie 	if (size > MAX_DIRECT_KEY_NUM)
197e4156979SChao Xie 		return -EINVAL;
198e4156979SChao Xie 
199e4156979SChao Xie 	for (i = 0; i < size; i++) {
200e4156979SChao Xie 		code = be16_to_cpup(prop + i);
201e4156979SChao Xie 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
202e4156979SChao Xie 		__set_bit(code, input_dev->keybit);
203e4156979SChao Xie 	}
204e4156979SChao Xie 
205e4156979SChao Xie 	return 0;
206e4156979SChao Xie }
207e4156979SChao Xie 
pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad * keypad,struct pxa27x_keypad_platform_data * pdata)2089eb52139SDmitry Torokhov static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad,
2099eb52139SDmitry Torokhov 				struct pxa27x_keypad_platform_data *pdata)
210e4156979SChao Xie {
211e4156979SChao Xie 	const __be32 *prop;
212e4156979SChao Xie 	int i, relkey_ret;
213e4156979SChao Xie 	unsigned int code, proplen;
214e4156979SChao Xie 	const char *rotaryname[2] = {
215e4156979SChao Xie 			"marvell,rotary0", "marvell,rotary1"};
216e4156979SChao Xie 	const char relkeyname[] = {"marvell,rotary-rel-key"};
217e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
218e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
219e4156979SChao Xie 	struct device_node *np = dev->of_node;
220e4156979SChao Xie 
221e4156979SChao Xie 	relkey_ret = of_property_read_u32(np, relkeyname, &code);
222e4156979SChao Xie 	/* if can read correct rotary key-code, we do not need this. */
223e4156979SChao Xie 	if (relkey_ret == 0) {
224e4156979SChao Xie 		unsigned short relcode;
225e4156979SChao Xie 
226e4156979SChao Xie 		/* rotary0 taks lower half, rotary1 taks upper half. */
227e4156979SChao Xie 		relcode = code & 0xffff;
228e4156979SChao Xie 		pdata->rotary0_rel_code = (code & 0xffff);
229e4156979SChao Xie 		__set_bit(relcode, input_dev->relbit);
230e4156979SChao Xie 
231e4156979SChao Xie 		relcode = code >> 16;
232e4156979SChao Xie 		pdata->rotary1_rel_code = relcode;
233e4156979SChao Xie 		__set_bit(relcode, input_dev->relbit);
234e4156979SChao Xie 	}
235e4156979SChao Xie 
236e4156979SChao Xie 	for (i = 0; i < 2; i++) {
237e4156979SChao Xie 		prop = of_get_property(np, rotaryname[i], &proplen);
238e4156979SChao Xie 		/*
239e4156979SChao Xie 		 * If the prop is not set, it means keypad does not need
240e4156979SChao Xie 		 * initialize the rotaryX.
241e4156979SChao Xie 		 */
242e4156979SChao Xie 		if (!prop)
243e4156979SChao Xie 			continue;
244e4156979SChao Xie 
245e4156979SChao Xie 		code = be32_to_cpup(prop);
246e4156979SChao Xie 		/*
247e4156979SChao Xie 		 * Not all up/down key code are valid.
248e4156979SChao Xie 		 * Now we depends on direct-rel-code.
249e4156979SChao Xie 		 */
250e4156979SChao Xie 		if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
251e4156979SChao Xie 			return relkey_ret;
252e4156979SChao Xie 		} else {
253e4156979SChao Xie 			unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
254e4156979SChao Xie 			unsigned short keycode;
255e4156979SChao Xie 
256e4156979SChao Xie 			keycode = code & 0xffff;
257e4156979SChao Xie 			keypad->keycodes[n] = keycode;
258e4156979SChao Xie 			__set_bit(keycode, input_dev->keybit);
259e4156979SChao Xie 
260e4156979SChao Xie 			keycode = code >> 16;
261e4156979SChao Xie 			keypad->keycodes[n + 1] = keycode;
262e4156979SChao Xie 			__set_bit(keycode, input_dev->keybit);
263e4156979SChao Xie 
264e4156979SChao Xie 			if (i == 0)
265e4156979SChao Xie 				pdata->rotary0_rel_code = -1;
266e4156979SChao Xie 			else
267e4156979SChao Xie 				pdata->rotary1_rel_code = -1;
268e4156979SChao Xie 		}
269e4156979SChao Xie 		if (i == 0)
270e4156979SChao Xie 			pdata->enable_rotary0 = 1;
271e4156979SChao Xie 		else
272e4156979SChao Xie 			pdata->enable_rotary1 = 1;
273e4156979SChao Xie 	}
274e4156979SChao Xie 
275e4156979SChao Xie 	keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
276e4156979SChao Xie 	keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
277e4156979SChao Xie 
278e4156979SChao Xie 	return 0;
279e4156979SChao Xie }
280e4156979SChao Xie 
pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad * keypad)281e4156979SChao Xie static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
282e4156979SChao Xie {
283e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
284e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
285e4156979SChao Xie 	struct device_node *np = dev->of_node;
2869eb52139SDmitry Torokhov 	struct pxa27x_keypad_platform_data *pdata;
287e4156979SChao Xie 	int error;
288e4156979SChao Xie 
2899eb52139SDmitry Torokhov 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2909eb52139SDmitry Torokhov 	if (!pdata) {
291e4156979SChao Xie 		dev_err(dev, "failed to allocate memory for pdata\n");
292e4156979SChao Xie 		return -ENOMEM;
293e4156979SChao Xie 	}
294e4156979SChao Xie 
2959eb52139SDmitry Torokhov 	error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata);
296e4156979SChao Xie 	if (error) {
297e4156979SChao Xie 		dev_err(dev, "failed to parse matrix key\n");
298e4156979SChao Xie 		return error;
299e4156979SChao Xie 	}
300e4156979SChao Xie 
3019eb52139SDmitry Torokhov 	error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata);
302e4156979SChao Xie 	if (error) {
303e4156979SChao Xie 		dev_err(dev, "failed to parse direct key\n");
304e4156979SChao Xie 		return error;
305e4156979SChao Xie 	}
306e4156979SChao Xie 
3079eb52139SDmitry Torokhov 	error = pxa27x_keypad_rotary_parse_dt(keypad, pdata);
308e4156979SChao Xie 	if (error) {
309e4156979SChao Xie 		dev_err(dev, "failed to parse rotary key\n");
310e4156979SChao Xie 		return error;
311e4156979SChao Xie 	}
312e4156979SChao Xie 
313e4156979SChao Xie 	error = of_property_read_u32(np, "marvell,debounce-interval",
3149eb52139SDmitry Torokhov 				     &pdata->debounce_interval);
315e4156979SChao Xie 	if (error) {
3166d86c471SColin Ian King 		dev_err(dev, "failed to parse debounce-interval\n");
317e4156979SChao Xie 		return error;
318e4156979SChao Xie 	}
319e4156979SChao Xie 
320e4156979SChao Xie 	/*
321e4156979SChao Xie 	 * The keycodes may not only includes matrix key but also the direct
322e4156979SChao Xie 	 * key or rotary key.
323e4156979SChao Xie 	 */
324e4156979SChao Xie 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
325e4156979SChao Xie 
3269eb52139SDmitry Torokhov 	keypad->pdata = pdata;
327e4156979SChao Xie 	return 0;
328e4156979SChao Xie }
329e4156979SChao Xie 
330e4156979SChao Xie #else
331e4156979SChao Xie 
pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad * keypad)332e4156979SChao Xie static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
333e4156979SChao Xie {
334e4156979SChao Xie 	dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
335e4156979SChao Xie 
336e4156979SChao Xie 	return -EINVAL;
337e4156979SChao Xie }
338e4156979SChao Xie 
339e4156979SChao Xie #endif
340e4156979SChao Xie 
pxa27x_keypad_build_keycode(struct pxa27x_keypad * keypad)3410a085a94SChao Xie static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
3421814db69SEric Miao {
3439eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
3441814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
345bd96f378SDmitry Torokhov 	unsigned short keycode;
3461814db69SEric Miao 	int i;
3470a085a94SChao Xie 	int error;
3481814db69SEric Miao 
3492b2f5147SDan Carpenter 	error = matrix_keypad_build_keymap(pdata->matrix_keymap_data, NULL,
3500a085a94SChao Xie 					   pdata->matrix_key_rows,
3510a085a94SChao Xie 					   pdata->matrix_key_cols,
3520a085a94SChao Xie 					   keypad->keycodes, input_dev);
3530a085a94SChao Xie 	if (error)
3540a085a94SChao Xie 		return error;
3551814db69SEric Miao 
3560a085a94SChao Xie 	/*
3570a085a94SChao Xie 	 * The keycodes may not only include matrix keys but also the direct
3580a085a94SChao Xie 	 * or rotary keys.
3590a085a94SChao Xie 	 */
3600a085a94SChao Xie 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
36162059d9eSEric Miao 
3620a085a94SChao Xie 	/* For direct keys. */
363bd96f378SDmitry Torokhov 	for (i = 0; i < pdata->direct_key_num; i++) {
364bd96f378SDmitry Torokhov 		keycode = pdata->direct_key_map[i];
365bd96f378SDmitry Torokhov 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
366bd96f378SDmitry Torokhov 		__set_bit(keycode, input_dev->keybit);
367bd96f378SDmitry Torokhov 	}
36862059d9eSEric Miao 
369471637a5SAntonio Ospite 	if (pdata->enable_rotary0) {
37062059d9eSEric Miao 		if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
371bd96f378SDmitry Torokhov 			keycode = pdata->rotary0_up_key;
372bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
373bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
374bd96f378SDmitry Torokhov 
375bd96f378SDmitry Torokhov 			keycode = pdata->rotary0_down_key;
376bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
377bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
378bd96f378SDmitry Torokhov 
379bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[0] = -1;
380bd96f378SDmitry Torokhov 		} else {
381bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
38252ec7752SDmitry Torokhov 			__set_bit(pdata->rotary0_rel_code, input_dev->relbit);
383471637a5SAntonio Ospite 		}
384bd96f378SDmitry Torokhov 	}
38562059d9eSEric Miao 
386471637a5SAntonio Ospite 	if (pdata->enable_rotary1) {
38762059d9eSEric Miao 		if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
388bd96f378SDmitry Torokhov 			keycode = pdata->rotary1_up_key;
389bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
390bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
391bd96f378SDmitry Torokhov 
392bd96f378SDmitry Torokhov 			keycode = pdata->rotary1_down_key;
393bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
394bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
395bd96f378SDmitry Torokhov 
396bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[1] = -1;
397bd96f378SDmitry Torokhov 		} else {
398bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
39952ec7752SDmitry Torokhov 			__set_bit(pdata->rotary1_rel_code, input_dev->relbit);
4001814db69SEric Miao 		}
401471637a5SAntonio Ospite 	}
4021814db69SEric Miao 
403bd96f378SDmitry Torokhov 	__clear_bit(KEY_RESERVED, input_dev->keybit);
4040a085a94SChao Xie 
4050a085a94SChao Xie 	return 0;
4061814db69SEric Miao }
4071814db69SEric Miao 
pxa27x_keypad_scan_matrix(struct pxa27x_keypad * keypad)4081814db69SEric Miao static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
4091814db69SEric Miao {
4109eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
411bd96f378SDmitry Torokhov 	struct input_dev *input_dev = keypad->input_dev;
4121814db69SEric Miao 	int row, col, num_keys_pressed = 0;
4131814db69SEric Miao 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
4149c60debdSEric Miao 	uint32_t kpas = keypad_readl(KPAS);
4151814db69SEric Miao 
4161814db69SEric Miao 	num_keys_pressed = KPAS_MUKP(kpas);
4171814db69SEric Miao 
4181814db69SEric Miao 	memset(new_state, 0, sizeof(new_state));
4191814db69SEric Miao 
4201814db69SEric Miao 	if (num_keys_pressed == 0)
4211814db69SEric Miao 		goto scan;
4221814db69SEric Miao 
4231814db69SEric Miao 	if (num_keys_pressed == 1) {
4241814db69SEric Miao 		col = KPAS_CP(kpas);
4251814db69SEric Miao 		row = KPAS_RP(kpas);
4261814db69SEric Miao 
4271814db69SEric Miao 		/* if invalid row/col, treat as no key pressed */
4281814db69SEric Miao 		if (col >= pdata->matrix_key_cols ||
4291814db69SEric Miao 		    row >= pdata->matrix_key_rows)
4301814db69SEric Miao 			goto scan;
4311814db69SEric Miao 
4321814db69SEric Miao 		new_state[col] = (1 << row);
4331814db69SEric Miao 		goto scan;
4341814db69SEric Miao 	}
4351814db69SEric Miao 
4361814db69SEric Miao 	if (num_keys_pressed > 1) {
4379c60debdSEric Miao 		uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
4389c60debdSEric Miao 		uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
4399c60debdSEric Miao 		uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
4409c60debdSEric Miao 		uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
4411814db69SEric Miao 
4421814db69SEric Miao 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
4431814db69SEric Miao 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
4441814db69SEric Miao 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
4451814db69SEric Miao 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
4461814db69SEric Miao 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
4471814db69SEric Miao 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
4481814db69SEric Miao 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
4491814db69SEric Miao 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
4501814db69SEric Miao 	}
4511814db69SEric Miao scan:
4521814db69SEric Miao 	for (col = 0; col < pdata->matrix_key_cols; col++) {
4531814db69SEric Miao 		uint32_t bits_changed;
454bd96f378SDmitry Torokhov 		int code;
4551814db69SEric Miao 
4561814db69SEric Miao 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
4571814db69SEric Miao 		if (bits_changed == 0)
4581814db69SEric Miao 			continue;
4591814db69SEric Miao 
4601814db69SEric Miao 		for (row = 0; row < pdata->matrix_key_rows; row++) {
4611814db69SEric Miao 			if ((bits_changed & (1 << row)) == 0)
4621814db69SEric Miao 				continue;
4631814db69SEric Miao 
464c5ec1948SChao Xie 			code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
465c5ec1948SChao Xie 
466bd96f378SDmitry Torokhov 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
467bd96f378SDmitry Torokhov 			input_report_key(input_dev, keypad->keycodes[code],
4681814db69SEric Miao 					 new_state[col] & (1 << row));
4691814db69SEric Miao 		}
4701814db69SEric Miao 	}
471bd96f378SDmitry Torokhov 	input_sync(input_dev);
4721814db69SEric Miao 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
4731814db69SEric Miao }
4740e5f11aaSEric Miao 
475d7416f9eSEric Miao #define DEFAULT_KPREC	(0x007f007f)
476d7416f9eSEric Miao 
rotary_delta(uint32_t kprec)47762059d9eSEric Miao static inline int rotary_delta(uint32_t kprec)
47862059d9eSEric Miao {
47962059d9eSEric Miao 	if (kprec & KPREC_OF0)
48062059d9eSEric Miao 		return (kprec & 0xff) + 0x7f;
48162059d9eSEric Miao 	else if (kprec & KPREC_UF0)
48262059d9eSEric Miao 		return (kprec & 0xff) - 0x7f - 0xff;
48362059d9eSEric Miao 	else
48462059d9eSEric Miao 		return (kprec & 0xff) - 0x7f;
48562059d9eSEric Miao }
48662059d9eSEric Miao 
report_rotary_event(struct pxa27x_keypad * keypad,int r,int delta)48762059d9eSEric Miao static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
48862059d9eSEric Miao {
48962059d9eSEric Miao 	struct input_dev *dev = keypad->input_dev;
49062059d9eSEric Miao 
49162059d9eSEric Miao 	if (delta == 0)
49262059d9eSEric Miao 		return;
49362059d9eSEric Miao 
494bd96f378SDmitry Torokhov 	if (keypad->rotary_rel_code[r] == -1) {
495bd96f378SDmitry Torokhov 		int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
496bd96f378SDmitry Torokhov 		unsigned char keycode = keypad->keycodes[code];
49762059d9eSEric Miao 
49862059d9eSEric Miao 		/* simulate a press-n-release */
499bd96f378SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
50062059d9eSEric Miao 		input_report_key(dev, keycode, 1);
50162059d9eSEric Miao 		input_sync(dev);
502bd96f378SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
50362059d9eSEric Miao 		input_report_key(dev, keycode, 0);
50462059d9eSEric Miao 		input_sync(dev);
50562059d9eSEric Miao 	} else {
50662059d9eSEric Miao 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
50762059d9eSEric Miao 		input_sync(dev);
50862059d9eSEric Miao 	}
50962059d9eSEric Miao }
51062059d9eSEric Miao 
pxa27x_keypad_scan_rotary(struct pxa27x_keypad * keypad)51162059d9eSEric Miao static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
51262059d9eSEric Miao {
5139eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
51462059d9eSEric Miao 	uint32_t kprec;
51562059d9eSEric Miao 
51662059d9eSEric Miao 	/* read and reset to default count value */
5179c60debdSEric Miao 	kprec = keypad_readl(KPREC);
5189c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
51962059d9eSEric Miao 
52062059d9eSEric Miao 	if (pdata->enable_rotary0)
52162059d9eSEric Miao 		report_rotary_event(keypad, 0, rotary_delta(kprec));
52262059d9eSEric Miao 
52362059d9eSEric Miao 	if (pdata->enable_rotary1)
52462059d9eSEric Miao 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
52562059d9eSEric Miao }
52662059d9eSEric Miao 
pxa27x_keypad_scan_direct(struct pxa27x_keypad * keypad)52762059d9eSEric Miao static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
52862059d9eSEric Miao {
5299eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
530bd96f378SDmitry Torokhov 	struct input_dev *input_dev = keypad->input_dev;
53162059d9eSEric Miao 	unsigned int new_state;
53262059d9eSEric Miao 	uint32_t kpdk, bits_changed;
53362059d9eSEric Miao 	int i;
53462059d9eSEric Miao 
5359c60debdSEric Miao 	kpdk = keypad_readl(KPDK);
53662059d9eSEric Miao 
53762059d9eSEric Miao 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
53862059d9eSEric Miao 		pxa27x_keypad_scan_rotary(keypad);
53962059d9eSEric Miao 
540ee1d8040SChao Xie 	/*
541ee1d8040SChao Xie 	 * The KPDR_DK only output the key pin level, so it relates to board,
542ee1d8040SChao Xie 	 * and low level may be active.
543ee1d8040SChao Xie 	 */
544ee1d8040SChao Xie 	if (pdata->direct_key_low_active)
545ee1d8040SChao Xie 		new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
546ee1d8040SChao Xie 	else
54762059d9eSEric Miao 		new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
548ee1d8040SChao Xie 
54962059d9eSEric Miao 	bits_changed = keypad->direct_key_state ^ new_state;
55062059d9eSEric Miao 
55162059d9eSEric Miao 	if (bits_changed == 0)
55262059d9eSEric Miao 		return;
55362059d9eSEric Miao 
55462059d9eSEric Miao 	for (i = 0; i < pdata->direct_key_num; i++) {
555bd96f378SDmitry Torokhov 		if (bits_changed & (1 << i)) {
556bd96f378SDmitry Torokhov 			int code = MAX_MATRIX_KEY_NUM + i;
557bd96f378SDmitry Torokhov 
558bd96f378SDmitry Torokhov 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
559bd96f378SDmitry Torokhov 			input_report_key(input_dev, keypad->keycodes[code],
560bd96f378SDmitry Torokhov 					 new_state & (1 << i));
56162059d9eSEric Miao 		}
562bd96f378SDmitry Torokhov 	}
563bd96f378SDmitry Torokhov 	input_sync(input_dev);
56462059d9eSEric Miao 	keypad->direct_key_state = new_state;
56562059d9eSEric Miao }
56662059d9eSEric Miao 
clear_wakeup_event(struct pxa27x_keypad * keypad)5673587c258SMark F. Brown static void clear_wakeup_event(struct pxa27x_keypad *keypad)
5683587c258SMark F. Brown {
5699eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
5703587c258SMark F. Brown 
5713587c258SMark F. Brown 	if (pdata->clear_wakeup_event)
5723587c258SMark F. Brown 		(pdata->clear_wakeup_event)();
5733587c258SMark F. Brown }
5743587c258SMark F. Brown 
pxa27x_keypad_irq_handler(int irq,void * dev_id)5750e5f11aaSEric Miao static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
5760e5f11aaSEric Miao {
5771814db69SEric Miao 	struct pxa27x_keypad *keypad = dev_id;
5789c60debdSEric Miao 	unsigned long kpc = keypad_readl(KPC);
5790e5f11aaSEric Miao 
5803587c258SMark F. Brown 	clear_wakeup_event(keypad);
5813587c258SMark F. Brown 
58262059d9eSEric Miao 	if (kpc & KPC_DI)
58362059d9eSEric Miao 		pxa27x_keypad_scan_direct(keypad);
5840e5f11aaSEric Miao 
5851814db69SEric Miao 	if (kpc & KPC_MI)
5861814db69SEric Miao 		pxa27x_keypad_scan_matrix(keypad);
5870e5f11aaSEric Miao 
5880e5f11aaSEric Miao 	return IRQ_HANDLED;
5890e5f11aaSEric Miao }
5900e5f11aaSEric Miao 
pxa27x_keypad_config(struct pxa27x_keypad * keypad)591d7416f9eSEric Miao static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
592d7416f9eSEric Miao {
5939eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
59462059d9eSEric Miao 	unsigned int mask = 0, direct_key_num = 0;
595d7416f9eSEric Miao 	unsigned long kpc = 0;
596d7416f9eSEric Miao 
597904adedeSVasily Khoruzhick 	/* clear pending interrupt bit */
598904adedeSVasily Khoruzhick 	keypad_readl(KPC);
599904adedeSVasily Khoruzhick 
600d7416f9eSEric Miao 	/* enable matrix keys with automatic scan */
601d7416f9eSEric Miao 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
602d7416f9eSEric Miao 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
603d7416f9eSEric Miao 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
604d7416f9eSEric Miao 		       KPC_MKCN(pdata->matrix_key_cols);
605d7416f9eSEric Miao 	}
606d7416f9eSEric Miao 
60762059d9eSEric Miao 	/* enable rotary key, debounce interval same as direct keys */
60862059d9eSEric Miao 	if (pdata->enable_rotary0) {
60962059d9eSEric Miao 		mask |= 0x03;
61062059d9eSEric Miao 		direct_key_num = 2;
61162059d9eSEric Miao 		kpc |= KPC_REE0;
61262059d9eSEric Miao 	}
613d7416f9eSEric Miao 
61462059d9eSEric Miao 	if (pdata->enable_rotary1) {
61562059d9eSEric Miao 		mask |= 0x0c;
61662059d9eSEric Miao 		direct_key_num = 4;
61762059d9eSEric Miao 		kpc |= KPC_REE1;
61862059d9eSEric Miao 	}
61962059d9eSEric Miao 
62062059d9eSEric Miao 	if (pdata->direct_key_num > direct_key_num)
62162059d9eSEric Miao 		direct_key_num = pdata->direct_key_num;
62262059d9eSEric Miao 
623fb054bf2SChao Xie 	/*
624fb054bf2SChao Xie 	 * Direct keys usage may not start from KP_DKIN0, check the platfrom
625fb054bf2SChao Xie 	 * mask data to config the specific.
626fb054bf2SChao Xie 	 */
627fb054bf2SChao Xie 	if (pdata->direct_key_mask)
628fb054bf2SChao Xie 		keypad->direct_key_mask = pdata->direct_key_mask;
629fb054bf2SChao Xie 	else
6305545fa89SChao Xie 		keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
63162059d9eSEric Miao 
63262059d9eSEric Miao 	/* enable direct key */
63362059d9eSEric Miao 	if (direct_key_num)
63462059d9eSEric Miao 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
63562059d9eSEric Miao 
6369c60debdSEric Miao 	keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
6379c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
63876cb44e1SEric Miao 	keypad_writel(KPKDI, pdata->debounce_interval);
639d7416f9eSEric Miao }
640d7416f9eSEric Miao 
pxa27x_keypad_open(struct input_dev * dev)6410e5f11aaSEric Miao static int pxa27x_keypad_open(struct input_dev *dev)
6420e5f11aaSEric Miao {
6431814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
644117b2dc5SArvind Yadav 	int ret;
6450e5f11aaSEric Miao 	/* Enable unit clock */
646117b2dc5SArvind Yadav 	ret = clk_prepare_enable(keypad->clk);
647117b2dc5SArvind Yadav 	if (ret)
648117b2dc5SArvind Yadav 		return ret;
649117b2dc5SArvind Yadav 
650d7416f9eSEric Miao 	pxa27x_keypad_config(keypad);
6510e5f11aaSEric Miao 
6520e5f11aaSEric Miao 	return 0;
6530e5f11aaSEric Miao }
6540e5f11aaSEric Miao 
pxa27x_keypad_close(struct input_dev * dev)6550e5f11aaSEric Miao static void pxa27x_keypad_close(struct input_dev *dev)
6560e5f11aaSEric Miao {
6571814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
6581814db69SEric Miao 
6590e5f11aaSEric Miao 	/* Disable clock unit */
6606ce34a5fSChao Xie 	clk_disable_unprepare(keypad->clk);
6610e5f11aaSEric Miao }
6620e5f11aaSEric Miao 
pxa27x_keypad_suspend(struct device * dev)663b0010911SDmitry Torokhov static int pxa27x_keypad_suspend(struct device *dev)
6640e5f11aaSEric Miao {
665b0010911SDmitry Torokhov 	struct platform_device *pdev = to_platform_device(dev);
6661814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
6670e5f11aaSEric Miao 
6686ce34a5fSChao Xie 	/*
6696ce34a5fSChao Xie 	 * If the keypad is used a wake up source, clock can not be disabled.
6706ce34a5fSChao Xie 	 * Or it can not detect the key pressing.
6716ce34a5fSChao Xie 	 */
67239ab9ddeSEric Miao 	if (device_may_wakeup(&pdev->dev))
67339ab9ddeSEric Miao 		enable_irq_wake(keypad->irq);
6746ce34a5fSChao Xie 	else
6756ce34a5fSChao Xie 		clk_disable_unprepare(keypad->clk);
67639ab9ddeSEric Miao 
6770e5f11aaSEric Miao 	return 0;
6780e5f11aaSEric Miao }
6790e5f11aaSEric Miao 
pxa27x_keypad_resume(struct device * dev)680b0010911SDmitry Torokhov static int pxa27x_keypad_resume(struct device *dev)
6810e5f11aaSEric Miao {
682b0010911SDmitry Torokhov 	struct platform_device *pdev = to_platform_device(dev);
6831814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
6841814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
685117b2dc5SArvind Yadav 	int ret = 0;
6860e5f11aaSEric Miao 
6876ce34a5fSChao Xie 	/*
6886ce34a5fSChao Xie 	 * If the keypad is used as wake up source, the clock is not turned
6896ce34a5fSChao Xie 	 * off. So do not need configure it again.
6906ce34a5fSChao Xie 	 */
6916ce34a5fSChao Xie 	if (device_may_wakeup(&pdev->dev)) {
69239ab9ddeSEric Miao 		disable_irq_wake(keypad->irq);
6936ce34a5fSChao Xie 	} else {
6940e5f11aaSEric Miao 		mutex_lock(&input_dev->mutex);
6950e5f11aaSEric Miao 
696d69f0a43SAndrzej Pietrasiewicz 		if (input_device_enabled(input_dev)) {
6970e5f11aaSEric Miao 			/* Enable unit clock */
698117b2dc5SArvind Yadav 			ret = clk_prepare_enable(keypad->clk);
699117b2dc5SArvind Yadav 			if (!ret)
700d7416f9eSEric Miao 				pxa27x_keypad_config(keypad);
7010e5f11aaSEric Miao 		}
7020e5f11aaSEric Miao 
7030e5f11aaSEric Miao 		mutex_unlock(&input_dev->mutex);
7046ce34a5fSChao Xie 	}
7050e5f11aaSEric Miao 
706117b2dc5SArvind Yadav 	return ret;
7070e5f11aaSEric Miao }
7080e5f11aaSEric Miao 
709a4b4fa51SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
710f9f6def8SDmitry Torokhov 				pxa27x_keypad_suspend, pxa27x_keypad_resume);
711f9f6def8SDmitry Torokhov 
712f9f6def8SDmitry Torokhov 
pxa27x_keypad_probe(struct platform_device * pdev)7135298cc4cSBill Pemberton static int pxa27x_keypad_probe(struct platform_device *pdev)
7140e5f11aaSEric Miao {
7159eb52139SDmitry Torokhov 	const struct pxa27x_keypad_platform_data *pdata =
7169eb52139SDmitry Torokhov 					dev_get_platdata(&pdev->dev);
717e4156979SChao Xie 	struct device_node *np = pdev->dev.of_node;
7181814db69SEric Miao 	struct pxa27x_keypad *keypad;
7190e5f11aaSEric Miao 	struct input_dev *input_dev;
7209c60debdSEric Miao 	int irq, error;
7210e5f11aaSEric Miao 
722e4156979SChao Xie 	/* Driver need build keycode from device tree or pdata */
723e4156979SChao Xie 	if (!np && !pdata)
724bd96f378SDmitry Torokhov 		return -EINVAL;
7251814db69SEric Miao 
7269c60debdSEric Miao 	irq = platform_get_irq(pdev, 0);
7270bec8b7eSStephen Boyd 	if (irq < 0)
728bd96f378SDmitry Torokhov 		return -ENXIO;
7299c60debdSEric Miao 
73038c38072SPramod Gurav 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad),
73138c38072SPramod Gurav 			      GFP_KERNEL);
73238c38072SPramod Gurav 	if (!keypad)
73338c38072SPramod Gurav 		return -ENOMEM;
73438c38072SPramod Gurav 
73538c38072SPramod Gurav 	input_dev = devm_input_allocate_device(&pdev->dev);
73638c38072SPramod Gurav 	if (!input_dev)
73738c38072SPramod Gurav 		return -ENOMEM;
7389c60debdSEric Miao 
739bd96f378SDmitry Torokhov 	keypad->pdata = pdata;
740bd96f378SDmitry Torokhov 	keypad->input_dev = input_dev;
741bd96f378SDmitry Torokhov 	keypad->irq = irq;
742bd96f378SDmitry Torokhov 
743*198a2ccaSYangtao Li 	keypad->mmio_base = devm_platform_ioremap_resource(pdev, 0);
74438c38072SPramod Gurav 	if (IS_ERR(keypad->mmio_base))
74538c38072SPramod Gurav 		return PTR_ERR(keypad->mmio_base);
7469c60debdSEric Miao 
74738c38072SPramod Gurav 	keypad->clk = devm_clk_get(&pdev->dev, NULL);
7481814db69SEric Miao 	if (IS_ERR(keypad->clk)) {
7491814db69SEric Miao 		dev_err(&pdev->dev, "failed to get keypad clock\n");
75038c38072SPramod Gurav 		return PTR_ERR(keypad->clk);
7510e5f11aaSEric Miao 	}
7520e5f11aaSEric Miao 
7539c60debdSEric Miao 	input_dev->name = pdev->name;
7540e5f11aaSEric Miao 	input_dev->id.bustype = BUS_HOST;
7550e5f11aaSEric Miao 	input_dev->open = pxa27x_keypad_open;
7560e5f11aaSEric Miao 	input_dev->close = pxa27x_keypad_close;
7570e5f11aaSEric Miao 	input_dev->dev.parent = &pdev->dev;
7580e5f11aaSEric Miao 
759bd96f378SDmitry Torokhov 	input_dev->keycode = keypad->keycodes;
760bd96f378SDmitry Torokhov 	input_dev->keycodesize = sizeof(keypad->keycodes[0]);
761bd96f378SDmitry Torokhov 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
762bd96f378SDmitry Torokhov 
7631814db69SEric Miao 	input_set_drvdata(input_dev, keypad);
7641814db69SEric Miao 
765471637a5SAntonio Ospite 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
766bd96f378SDmitry Torokhov 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
7671814db69SEric Miao 
768049d75f7SMike Dunn 	if (pdata) {
7690a085a94SChao Xie 		error = pxa27x_keypad_build_keycode(keypad);
770049d75f7SMike Dunn 	} else {
771e4156979SChao Xie 		error = pxa27x_keypad_build_keycode_from_dt(keypad);
772049d75f7SMike Dunn 		/*
773049d75f7SMike Dunn 		 * Data that we get from DT resides in dynamically
774049d75f7SMike Dunn 		 * allocated memory so we need to update our pdata
775049d75f7SMike Dunn 		 * pointer.
776049d75f7SMike Dunn 		 */
777049d75f7SMike Dunn 		pdata = keypad->pdata;
778049d75f7SMike Dunn 	}
7790a085a94SChao Xie 	if (error) {
7800a085a94SChao Xie 		dev_err(&pdev->dev, "failed to build keycode\n");
78138c38072SPramod Gurav 		return error;
7820a085a94SChao Xie 	}
783bd96f378SDmitry Torokhov 
784c5ec1948SChao Xie 	keypad->row_shift = get_count_order(pdata->matrix_key_cols);
785c5ec1948SChao Xie 
786bd96f378SDmitry Torokhov 	if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
787bd96f378SDmitry Torokhov 	    (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
788bd96f378SDmitry Torokhov 		input_dev->evbit[0] |= BIT_MASK(EV_REL);
789bd96f378SDmitry Torokhov 	}
7900e5f11aaSEric Miao 
79138c38072SPramod Gurav 	error = devm_request_irq(&pdev->dev, irq, pxa27x_keypad_irq_handler,
79238c38072SPramod Gurav 				 0, pdev->name, keypad);
7939c60debdSEric Miao 	if (error) {
7949c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request IRQ\n");
79538c38072SPramod Gurav 		return error;
7969c60debdSEric Miao 	}
7979c60debdSEric Miao 
7980e5f11aaSEric Miao 	/* Register the input device */
7990e5f11aaSEric Miao 	error = input_register_device(input_dev);
8009c60debdSEric Miao 	if (error) {
8019c60debdSEric Miao 		dev_err(&pdev->dev, "failed to register input device\n");
80238c38072SPramod Gurav 		return error;
8039c60debdSEric Miao 	}
8040e5f11aaSEric Miao 
805bd96f378SDmitry Torokhov 	platform_set_drvdata(pdev, keypad);
80639ab9ddeSEric Miao 	device_init_wakeup(&pdev->dev, 1);
80739ab9ddeSEric Miao 
8080e5f11aaSEric Miao 	return 0;
8090e5f11aaSEric Miao }
8100e5f11aaSEric Miao 
811e4156979SChao Xie #ifdef CONFIG_OF
812e4156979SChao Xie static const struct of_device_id pxa27x_keypad_dt_match[] = {
813e4156979SChao Xie 	{ .compatible = "marvell,pxa27x-keypad" },
814e4156979SChao Xie 	{},
815e4156979SChao Xie };
816e4156979SChao Xie MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
817e4156979SChao Xie #endif
818e4156979SChao Xie 
8190e5f11aaSEric Miao static struct platform_driver pxa27x_keypad_driver = {
8200e5f11aaSEric Miao 	.probe		= pxa27x_keypad_probe,
8210e5f11aaSEric Miao 	.driver		= {
8229c60debdSEric Miao 		.name	= "pxa27x-keypad",
823e4156979SChao Xie 		.of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
824a4b4fa51SJonathan Cameron 		.pm	= pm_sleep_ptr(&pxa27x_keypad_pm_ops),
8250e5f11aaSEric Miao 	},
8260e5f11aaSEric Miao };
8275146c84fSJJ Ding module_platform_driver(pxa27x_keypad_driver);
8280e5f11aaSEric Miao 
8290e5f11aaSEric Miao MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
8300e5f11aaSEric Miao MODULE_LICENSE("GPL");
83138c38072SPramod Gurav /* work with hotplug and coldplug */
83238c38072SPramod Gurav MODULE_ALIAS("platform:pxa27x-keypad");
833