1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * linux/drivers/input/keyboard/omap-keypad.c
4  *
5  * OMAP Keypad Driver
6  *
7  * Copyright (C) 2003 Nokia Corporation
8  * Written by Timo Teräs <ext-timo.teras@nokia.com>
9  *
10  * Added support for H2 & H3 Keypad
11  * Copyright (C) 2004 Texas Instruments
12  */
13 
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/types.h>
17 #include <linux/input.h>
18 #include <linux/kernel.h>
19 #include <linux/delay.h>
20 #include <linux/platform_device.h>
21 #include <linux/mutex.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
25 #include <linux/platform_data/gpio-omap.h>
26 #include <linux/platform_data/keypad-omap.h>
27 
28 #undef NEW_BOARD_LEARNING_MODE
29 
30 static void omap_kp_tasklet(unsigned long);
31 static void omap_kp_timer(struct timer_list *);
32 
33 static unsigned char keypad_state[8];
34 static DEFINE_MUTEX(kp_enable_mutex);
35 static int kp_enable = 1;
36 static int kp_cur_group = -1;
37 
38 struct omap_kp {
39 	struct input_dev *input;
40 	struct timer_list timer;
41 	int irq;
42 	unsigned int rows;
43 	unsigned int cols;
44 	unsigned long delay;
45 	unsigned int debounce;
46 	unsigned short keymap[];
47 };
48 
49 static DECLARE_TASKLET_DISABLED_OLD(kp_tasklet, omap_kp_tasklet);
50 
51 static unsigned int *row_gpios;
52 static unsigned int *col_gpios;
53 
54 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id)
55 {
56 	/* disable keyboard interrupt and schedule for handling */
57 	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
58 
59 	tasklet_schedule(&kp_tasklet);
60 
61 	return IRQ_HANDLED;
62 }
63 
64 static void omap_kp_timer(struct timer_list *unused)
65 {
66 	tasklet_schedule(&kp_tasklet);
67 }
68 
69 static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
70 {
71 	int col = 0;
72 
73 	/* disable keyboard interrupt and schedule for handling */
74 	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
75 
76 	/* read the keypad status */
77 	omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
78 	for (col = 0; col < omap_kp->cols; col++) {
79 		omap_writew(~(1 << col) & 0xff,
80 			    OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
81 
82 		udelay(omap_kp->delay);
83 
84 		state[col] = ~omap_readw(OMAP1_MPUIO_BASE +
85 					 OMAP_MPUIO_KBR_LATCH) & 0xff;
86 	}
87 	omap_writew(0x00, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBC);
88 	udelay(2);
89 }
90 
91 static void omap_kp_tasklet(unsigned long data)
92 {
93 	struct omap_kp *omap_kp_data = (struct omap_kp *) data;
94 	unsigned short *keycodes = omap_kp_data->input->keycode;
95 	unsigned int row_shift = get_count_order(omap_kp_data->cols);
96 	unsigned char new_state[8], changed, key_down = 0;
97 	int col, row;
98 
99 	/* check for any changes */
100 	omap_kp_scan_keypad(omap_kp_data, new_state);
101 
102 	/* check for changes and print those */
103 	for (col = 0; col < omap_kp_data->cols; col++) {
104 		changed = new_state[col] ^ keypad_state[col];
105 		key_down |= new_state[col];
106 		if (changed == 0)
107 			continue;
108 
109 		for (row = 0; row < omap_kp_data->rows; row++) {
110 			int key;
111 			if (!(changed & (1 << row)))
112 				continue;
113 #ifdef NEW_BOARD_LEARNING_MODE
114 			printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col,
115 			       row, (new_state[col] & (1 << row)) ?
116 			       "pressed" : "released");
117 #else
118 			key = keycodes[MATRIX_SCAN_CODE(row, col, row_shift)];
119 
120 			if (!(kp_cur_group == (key & GROUP_MASK) ||
121 			      kp_cur_group == -1))
122 				continue;
123 
124 			kp_cur_group = key & GROUP_MASK;
125 			input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
126 					 new_state[col] & (1 << row));
127 #endif
128 		}
129 	}
130 	input_sync(omap_kp_data->input);
131 	memcpy(keypad_state, new_state, sizeof(keypad_state));
132 
133 	if (key_down) {
134 		/* some key is pressed - keep irq disabled and use timer
135 		 * to poll the keypad */
136 		mod_timer(&omap_kp_data->timer, jiffies + HZ / 20);
137 	} else {
138 		/* enable interrupts */
139 		omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
140 		kp_cur_group = -1;
141 	}
142 }
143 
144 static ssize_t omap_kp_enable_show(struct device *dev,
145 				   struct device_attribute *attr, char *buf)
146 {
147 	return sprintf(buf, "%u\n", kp_enable);
148 }
149 
150 static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
151 				    const char *buf, size_t count)
152 {
153 	struct omap_kp *omap_kp = dev_get_drvdata(dev);
154 	int state;
155 
156 	if (sscanf(buf, "%u", &state) != 1)
157 		return -EINVAL;
158 
159 	if ((state != 1) && (state != 0))
160 		return -EINVAL;
161 
162 	mutex_lock(&kp_enable_mutex);
163 	if (state != kp_enable) {
164 		if (state)
165 			enable_irq(omap_kp->irq);
166 		else
167 			disable_irq(omap_kp->irq);
168 		kp_enable = state;
169 	}
170 	mutex_unlock(&kp_enable_mutex);
171 
172 	return strnlen(buf, count);
173 }
174 
175 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
176 
177 static int omap_kp_probe(struct platform_device *pdev)
178 {
179 	struct omap_kp *omap_kp;
180 	struct input_dev *input_dev;
181 	struct omap_kp_platform_data *pdata = dev_get_platdata(&pdev->dev);
182 	int i, col_idx, row_idx, ret;
183 	unsigned int row_shift, keycodemax;
184 
185 	if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
186 		printk(KERN_ERR "No rows, cols or keymap_data from pdata\n");
187 		return -EINVAL;
188 	}
189 
190 	row_shift = get_count_order(pdata->cols);
191 	keycodemax = pdata->rows << row_shift;
192 
193 	omap_kp = kzalloc(struct_size(omap_kp, keymap, keycodemax), GFP_KERNEL);
194 	input_dev = input_allocate_device();
195 	if (!omap_kp || !input_dev) {
196 		kfree(omap_kp);
197 		input_free_device(input_dev);
198 		return -ENOMEM;
199 	}
200 
201 	platform_set_drvdata(pdev, omap_kp);
202 
203 	omap_kp->input = input_dev;
204 
205 	/* Disable the interrupt for the MPUIO keyboard */
206 	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
207 
208 	if (pdata->delay)
209 		omap_kp->delay = pdata->delay;
210 
211 	if (pdata->row_gpios && pdata->col_gpios) {
212 		row_gpios = pdata->row_gpios;
213 		col_gpios = pdata->col_gpios;
214 	}
215 
216 	omap_kp->rows = pdata->rows;
217 	omap_kp->cols = pdata->cols;
218 
219 	col_idx = 0;
220 	row_idx = 0;
221 
222 	timer_setup(&omap_kp->timer, omap_kp_timer, 0);
223 
224 	/* get the irq and init timer*/
225 	kp_tasklet.data = (unsigned long) omap_kp;
226 	tasklet_enable(&kp_tasklet);
227 
228 	ret = device_create_file(&pdev->dev, &dev_attr_enable);
229 	if (ret < 0)
230 		goto err2;
231 
232 	/* setup input device */
233 	input_dev->name = "omap-keypad";
234 	input_dev->phys = "omap-keypad/input0";
235 	input_dev->dev.parent = &pdev->dev;
236 
237 	input_dev->id.bustype = BUS_HOST;
238 	input_dev->id.vendor = 0x0001;
239 	input_dev->id.product = 0x0001;
240 	input_dev->id.version = 0x0100;
241 
242 	if (pdata->rep)
243 		__set_bit(EV_REP, input_dev->evbit);
244 
245 	ret = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
246 					 pdata->rows, pdata->cols,
247 					 omap_kp->keymap, input_dev);
248 	if (ret < 0)
249 		goto err3;
250 
251 	ret = input_register_device(omap_kp->input);
252 	if (ret < 0) {
253 		printk(KERN_ERR "Unable to register omap-keypad input device\n");
254 		goto err3;
255 	}
256 
257 	if (pdata->dbounce)
258 		omap_writew(0xff, OMAP1_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
259 
260 	/* scan current status and enable interrupt */
261 	omap_kp_scan_keypad(omap_kp, keypad_state);
262 	omap_kp->irq = platform_get_irq(pdev, 0);
263 	if (omap_kp->irq >= 0) {
264 		if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
265 				"omap-keypad", omap_kp) < 0)
266 			goto err4;
267 	}
268 	omap_writew(0, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
269 
270 	return 0;
271 
272 err4:
273 	input_unregister_device(omap_kp->input);
274 	input_dev = NULL;
275 err3:
276 	device_remove_file(&pdev->dev, &dev_attr_enable);
277 err2:
278 	for (i = row_idx - 1; i >= 0; i--)
279 		gpio_free(row_gpios[i]);
280 	for (i = col_idx - 1; i >= 0; i--)
281 		gpio_free(col_gpios[i]);
282 
283 	kfree(omap_kp);
284 	input_free_device(input_dev);
285 
286 	return -EINVAL;
287 }
288 
289 static int omap_kp_remove(struct platform_device *pdev)
290 {
291 	struct omap_kp *omap_kp = platform_get_drvdata(pdev);
292 
293 	/* disable keypad interrupt handling */
294 	tasklet_disable(&kp_tasklet);
295 	omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
296 	free_irq(omap_kp->irq, omap_kp);
297 
298 	del_timer_sync(&omap_kp->timer);
299 	tasklet_kill(&kp_tasklet);
300 
301 	/* unregister everything */
302 	input_unregister_device(omap_kp->input);
303 
304 	kfree(omap_kp);
305 
306 	return 0;
307 }
308 
309 static struct platform_driver omap_kp_driver = {
310 	.probe		= omap_kp_probe,
311 	.remove		= omap_kp_remove,
312 	.driver		= {
313 		.name	= "omap-keypad",
314 	},
315 };
316 module_platform_driver(omap_kp_driver);
317 
318 MODULE_AUTHOR("Timo Teräs");
319 MODULE_DESCRIPTION("OMAP Keypad Driver");
320 MODULE_LICENSE("GPL");
321 MODULE_ALIAS("platform:omap-keypad");
322