4 * Copyright (C) 2010 Texas Instruments
6 * Author: Abraham Arce <x0066660@ti.com>
7 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/errno.h>
31 #include <linux/input.h>
32 #include <linux/slab.h>
33 #include <linux/pm_runtime.h>
35 #include <linux/platform_data/omap4-keypad.h>
38 #define OMAP4_KBD_REVISION 0x00
39 #define OMAP4_KBD_SYSCONFIG 0x10
40 #define OMAP4_KBD_SYSSTATUS 0x14
41 #define OMAP4_KBD_IRQSTATUS 0x18
42 #define OMAP4_KBD_IRQENABLE 0x1C
43 #define OMAP4_KBD_WAKEUPENABLE 0x20
44 #define OMAP4_KBD_PENDING 0x24
45 #define OMAP4_KBD_CTRL 0x28
46 #define OMAP4_KBD_DEBOUNCINGTIME 0x2C
47 #define OMAP4_KBD_LONGKEYTIME 0x30
48 #define OMAP4_KBD_TIMEOUT 0x34
49 #define OMAP4_KBD_STATEMACHINE 0x38
50 #define OMAP4_KBD_ROWINPUTS 0x3C
51 #define OMAP4_KBD_COLUMNOUTPUTS 0x40
52 #define OMAP4_KBD_FULLCODE31_0 0x44
53 #define OMAP4_KBD_FULLCODE63_32 0x48
55 /* OMAP4 bit definitions */
56 #define OMAP4_DEF_IRQENABLE_EVENTEN (1 << 0)
57 #define OMAP4_DEF_IRQENABLE_LONGKEY (1 << 1)
58 #define OMAP4_DEF_IRQENABLE_TIMEOUTEN (1 << 2)
59 #define OMAP4_DEF_WUP_EVENT_ENA (1 << 0)
60 #define OMAP4_DEF_WUP_LONG_KEY_ENA (1 << 1)
61 #define OMAP4_DEF_CTRL_NOSOFTMODE (1 << 1)
62 #define OMAP4_DEF_CTRLPTVVALUE (1 << 2)
63 #define OMAP4_DEF_CTRLPTV (1 << 1)
66 #define OMAP4_VAL_IRQDISABLE 0x00
67 #define OMAP4_VAL_DEBOUNCINGTIME 0x07
68 #define OMAP4_VAL_FUNCTIONALCFG 0x1E
70 #define OMAP4_MASK_IRQSTATUSDISABLE 0xFFFF
73 KBD_REVISION_OMAP4 = 0,
78 struct input_dev *input;
87 unsigned int row_shift;
89 unsigned char key_state[8];
90 unsigned short *keymap;
93 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
95 return __raw_readl(keypad_data->base +
96 keypad_data->reg_offset + offset);
99 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
102 keypad_data->base + keypad_data->reg_offset + offset);
105 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
107 return __raw_readl(keypad_data->base +
108 keypad_data->irqreg_offset + offset);
111 static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
112 u32 offset, u32 value)
115 keypad_data->base + keypad_data->irqreg_offset + offset);
119 /* Interrupt handler */
120 static irqreturn_t omap4_keypad_interrupt(int irq, void *dev_id)
122 struct omap4_keypad *keypad_data = dev_id;
123 struct input_dev *input_dev = keypad_data->input;
124 unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
125 unsigned int col, row, code, changed;
126 u32 *new_state = (u32 *) key_state;
128 /* Disable interrupts */
129 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
130 OMAP4_VAL_IRQDISABLE);
132 *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
133 *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
135 for (row = 0; row < keypad_data->rows; row++) {
136 changed = key_state[row] ^ keypad_data->key_state[row];
140 for (col = 0; col < keypad_data->cols; col++) {
141 if (changed & (1 << col)) {
142 code = MATRIX_SCAN_CODE(row, col,
143 keypad_data->row_shift);
144 input_event(input_dev, EV_MSC, MSC_SCAN, code);
145 input_report_key(input_dev,
146 keypad_data->keymap[code],
147 key_state[row] & (1 << col));
152 input_sync(input_dev);
154 memcpy(keypad_data->key_state, key_state,
155 sizeof(keypad_data->key_state));
157 /* clear pending interrupts */
158 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
159 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
161 /* enable interrupts */
162 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
163 OMAP4_DEF_IRQENABLE_EVENTEN |
164 OMAP4_DEF_IRQENABLE_LONGKEY);
169 static int omap4_keypad_open(struct input_dev *input)
171 struct omap4_keypad *keypad_data = input_get_drvdata(input);
173 pm_runtime_get_sync(input->dev.parent);
175 disable_irq(keypad_data->irq);
177 kbd_writel(keypad_data, OMAP4_KBD_CTRL,
178 OMAP4_VAL_FUNCTIONALCFG);
179 kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
180 OMAP4_VAL_DEBOUNCINGTIME);
181 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
182 OMAP4_VAL_IRQDISABLE);
183 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
184 OMAP4_DEF_IRQENABLE_EVENTEN |
185 OMAP4_DEF_IRQENABLE_LONGKEY);
186 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
187 OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
189 enable_irq(keypad_data->irq);
194 static void omap4_keypad_close(struct input_dev *input)
196 struct omap4_keypad *keypad_data = input_get_drvdata(input);
198 disable_irq(keypad_data->irq);
200 /* Disable interrupts */
201 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
202 OMAP4_VAL_IRQDISABLE);
204 /* clear pending interrupts */
205 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
206 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
208 enable_irq(keypad_data->irq);
210 pm_runtime_put_sync(input->dev.parent);
214 static int omap4_keypad_parse_dt(struct device *dev,
215 struct omap4_keypad *keypad_data)
217 struct device_node *np = dev->of_node;
220 err = matrix_keypad_parse_of_params(dev, &keypad_data->rows,
225 if (of_get_property(np, "linux,input-no-autorepeat", NULL))
226 keypad_data->no_autorepeat = true;
231 static inline int omap4_keypad_parse_dt(struct device *dev,
232 struct omap4_keypad *keypad_data)
238 static int omap4_keypad_probe(struct platform_device *pdev)
240 const struct omap4_keypad_platform_data *pdata =
241 dev_get_platdata(&pdev->dev);
242 const struct matrix_keymap_data *keymap_data =
243 pdata ? pdata->keymap_data : NULL;
244 struct omap4_keypad *keypad_data;
245 struct input_dev *input_dev;
246 struct resource *res;
247 unsigned int max_keys;
252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
254 dev_err(&pdev->dev, "no base address specified\n");
258 irq = platform_get_irq(pdev, 0);
260 dev_err(&pdev->dev, "no keyboard irq assigned\n");
264 keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
266 dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
270 keypad_data->irq = irq;
273 keypad_data->rows = pdata->rows;
274 keypad_data->cols = pdata->cols;
276 error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
281 res = request_mem_region(res->start, resource_size(res), pdev->name);
283 dev_err(&pdev->dev, "can't request mem region\n");
285 goto err_free_keypad;
288 keypad_data->base = ioremap(res->start, resource_size(res));
289 if (!keypad_data->base) {
290 dev_err(&pdev->dev, "can't ioremap mem resource\n");
292 goto err_release_mem;
297 * Enable clocks for the keypad module so that we can read
300 pm_runtime_enable(&pdev->dev);
301 error = pm_runtime_get_sync(&pdev->dev);
303 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
306 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
310 case KBD_REVISION_OMAP4:
311 keypad_data->reg_offset = 0x00;
312 keypad_data->irqreg_offset = 0x00;
314 case KBD_REVISION_OMAP5:
315 keypad_data->reg_offset = 0x10;
316 keypad_data->irqreg_offset = 0x0c;
320 "Keypad reports unsupported revision %d", rev);
322 goto err_pm_put_sync;
325 /* input device allocation */
326 keypad_data->input = input_dev = input_allocate_device();
329 goto err_pm_put_sync;
332 input_dev->name = pdev->name;
333 input_dev->dev.parent = &pdev->dev;
334 input_dev->id.bustype = BUS_HOST;
335 input_dev->id.vendor = 0x0001;
336 input_dev->id.product = 0x0001;
337 input_dev->id.version = 0x0001;
339 input_dev->open = omap4_keypad_open;
340 input_dev->close = omap4_keypad_close;
342 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
343 if (!keypad_data->no_autorepeat)
344 __set_bit(EV_REP, input_dev->evbit);
346 input_set_drvdata(input_dev, keypad_data);
348 keypad_data->row_shift = get_count_order(keypad_data->cols);
349 max_keys = keypad_data->rows << keypad_data->row_shift;
350 keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]),
352 if (!keypad_data->keymap) {
353 dev_err(&pdev->dev, "Not enough memory for keymap\n");
358 error = matrix_keypad_build_keymap(keymap_data, NULL,
359 keypad_data->rows, keypad_data->cols,
360 keypad_data->keymap, input_dev);
362 dev_err(&pdev->dev, "failed to build keymap\n");
363 goto err_free_keymap;
366 error = request_irq(keypad_data->irq, omap4_keypad_interrupt,
368 "omap4-keypad", keypad_data);
370 dev_err(&pdev->dev, "failed to register interrupt\n");
374 pm_runtime_put_sync(&pdev->dev);
376 error = input_register_device(keypad_data->input);
378 dev_err(&pdev->dev, "failed to register input device\n");
382 platform_set_drvdata(pdev, keypad_data);
386 pm_runtime_disable(&pdev->dev);
387 free_irq(keypad_data->irq, keypad_data);
389 kfree(keypad_data->keymap);
391 input_free_device(input_dev);
393 pm_runtime_put_sync(&pdev->dev);
395 iounmap(keypad_data->base);
397 release_mem_region(res->start, resource_size(res));
403 static int omap4_keypad_remove(struct platform_device *pdev)
405 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
406 struct resource *res;
408 free_irq(keypad_data->irq, keypad_data);
410 pm_runtime_disable(&pdev->dev);
412 input_unregister_device(keypad_data->input);
414 iounmap(keypad_data->base);
416 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
417 release_mem_region(res->start, resource_size(res));
419 kfree(keypad_data->keymap);
422 platform_set_drvdata(pdev, NULL);
428 static const struct of_device_id omap_keypad_dt_match[] = {
429 { .compatible = "ti,omap4-keypad" },
432 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
435 static struct platform_driver omap4_keypad_driver = {
436 .probe = omap4_keypad_probe,
437 .remove = omap4_keypad_remove,
439 .name = "omap4-keypad",
440 .owner = THIS_MODULE,
441 .of_match_table = of_match_ptr(omap_keypad_dt_match),
444 module_platform_driver(omap4_keypad_driver);
446 MODULE_AUTHOR("Texas Instruments");
447 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
448 MODULE_LICENSE("GPL");
449 MODULE_ALIAS("platform:omap4-keypad");