Linux Audio

Check our new training course

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