blob: e25b022692cd48d4d1f14f111f54a32adc01e2c8 [file] [log] [blame]
Abraham Arcea17f7952010-08-31 17:05:27 -07001/*
2 * OMAP4 Keypad Driver
3 *
4 * Copyright (C) 2010 Texas Instruments
5 *
6 * Author: Abraham Arce <x0066660@ti.com>
7 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
8 *
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.
13 *
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.
18 *
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
22 */
23
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>
29#include <linux/io.h>
Sourav Poddar13987432012-07-13 00:10:47 -070030#include <linux/of.h>
Abraham Arcea17f7952010-08-31 17:05:27 -070031#include <linux/input.h>
32#include <linux/slab.h>
Abraham Arce5ad567f2011-02-22 22:25:59 -080033#include <linux/pm_runtime.h>
Abraham Arcea17f7952010-08-31 17:05:27 -070034
Felipe Balbi0f1142a2012-03-16 22:47:48 -070035#include <linux/platform_data/omap4-keypad.h>
Abraham Arcea17f7952010-08-31 17:05:27 -070036
37/* OMAP4 registers */
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
54
55/* OMAP4 bit definitions */
Abraham Arcea17f7952010-08-31 17:05:27 -070056#define OMAP4_DEF_IRQENABLE_EVENTEN (1 << 0)
57#define OMAP4_DEF_IRQENABLE_LONGKEY (1 << 1)
58#define OMAP4_DEF_IRQENABLE_TIMEOUTEN (1 << 2)
Mike Turquette9a34bc62010-09-29 23:38:01 -070059#define OMAP4_DEF_WUP_EVENT_ENA (1 << 0)
60#define OMAP4_DEF_WUP_LONG_KEY_ENA (1 << 1)
Abraham Arcea17f7952010-08-31 17:05:27 -070061#define OMAP4_DEF_CTRL_NOSOFTMODE (1 << 1)
62#define OMAP4_DEF_CTRLPTVVALUE (1 << 2)
63#define OMAP4_DEF_CTRLPTV (1 << 1)
Abraham Arcea17f7952010-08-31 17:05:27 -070064
65/* OMAP4 values */
Abraham Arce05362f42010-09-29 23:36:48 -070066#define OMAP4_VAL_IRQDISABLE 0x00
Abraham Arcea17f7952010-08-31 17:05:27 -070067#define OMAP4_VAL_DEBOUNCINGTIME 0x07
68#define OMAP4_VAL_FUNCTIONALCFG 0x1E
69
70#define OMAP4_MASK_IRQSTATUSDISABLE 0xFFFF
71
Poddar, Souravf77621c2012-05-10 22:32:00 -070072enum {
73 KBD_REVISION_OMAP4 = 0,
74 KBD_REVISION_OMAP5,
75};
76
Abraham Arcea17f7952010-08-31 17:05:27 -070077struct omap4_keypad {
78 struct input_dev *input;
79
80 void __iomem *base;
Poddar, Souravf77621c2012-05-10 22:32:00 -070081 unsigned int irq;
Abraham Arcea17f7952010-08-31 17:05:27 -070082
83 unsigned int rows;
84 unsigned int cols;
Poddar, Souravf77621c2012-05-10 22:32:00 -070085 u32 reg_offset;
86 u32 irqreg_offset;
Abraham Arcea17f7952010-08-31 17:05:27 -070087 unsigned int row_shift;
Sourav Poddar13987432012-07-13 00:10:47 -070088 bool no_autorepeat;
Abraham Arcea17f7952010-08-31 17:05:27 -070089 unsigned char key_state[8];
Sourav Poddar13987432012-07-13 00:10:47 -070090 unsigned short *keymap;
Abraham Arcea17f7952010-08-31 17:05:27 -070091};
92
Poddar, Souravf77621c2012-05-10 22:32:00 -070093static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
94{
95 return __raw_readl(keypad_data->base +
96 keypad_data->reg_offset + offset);
97}
98
99static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
100{
101 __raw_writel(value,
102 keypad_data->base + keypad_data->reg_offset + offset);
103}
104
105static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
106{
107 return __raw_readl(keypad_data->base +
108 keypad_data->irqreg_offset + offset);
109}
110
111static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
112 u32 offset, u32 value)
113{
114 __raw_writel(value,
115 keypad_data->base + keypad_data->irqreg_offset + offset);
116}
117
118
Abraham Arcea17f7952010-08-31 17:05:27 -0700119/* Interrupt handler */
120static irqreturn_t omap4_keypad_interrupt(int irq, void *dev_id)
121{
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;
127
128 /* Disable interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700129 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
130 OMAP4_VAL_IRQDISABLE);
Abraham Arcea17f7952010-08-31 17:05:27 -0700131
Poddar, Souravf77621c2012-05-10 22:32:00 -0700132 *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
133 *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
Abraham Arcea17f7952010-08-31 17:05:27 -0700134
135 for (row = 0; row < keypad_data->rows; row++) {
136 changed = key_state[row] ^ keypad_data->key_state[row];
137 if (!changed)
138 continue;
139
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));
148 }
149 }
150 }
151
152 input_sync(input_dev);
153
154 memcpy(keypad_data->key_state, key_state,
155 sizeof(keypad_data->key_state));
156
157 /* clear pending interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700158 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
159 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
Abraham Arcea17f7952010-08-31 17:05:27 -0700160
161 /* enable interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700162 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
163 OMAP4_DEF_IRQENABLE_EVENTEN |
164 OMAP4_DEF_IRQENABLE_LONGKEY);
Abraham Arcea17f7952010-08-31 17:05:27 -0700165
166 return IRQ_HANDLED;
167}
168
Abraham Arce5ad567f2011-02-22 22:25:59 -0800169static int omap4_keypad_open(struct input_dev *input)
170{
171 struct omap4_keypad *keypad_data = input_get_drvdata(input);
172
173 pm_runtime_get_sync(input->dev.parent);
174
175 disable_irq(keypad_data->irq);
176
Poddar, Souravf77621c2012-05-10 22:32:00 -0700177 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);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800188
189 enable_irq(keypad_data->irq);
190
191 return 0;
192}
193
194static void omap4_keypad_close(struct input_dev *input)
195{
196 struct omap4_keypad *keypad_data = input_get_drvdata(input);
197
198 disable_irq(keypad_data->irq);
199
200 /* Disable interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700201 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
202 OMAP4_VAL_IRQDISABLE);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800203
204 /* clear pending interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700205 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
206 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
Abraham Arce5ad567f2011-02-22 22:25:59 -0800207
208 enable_irq(keypad_data->irq);
209
210 pm_runtime_put_sync(input->dev.parent);
211}
212
Sourav Poddar13987432012-07-13 00:10:47 -0700213#ifdef CONFIG_OF
Bill Pemberton5298cc42012-11-23 21:38:25 -0800214static int omap4_keypad_parse_dt(struct device *dev,
215 struct omap4_keypad *keypad_data)
Sourav Poddar13987432012-07-13 00:10:47 -0700216{
217 struct device_node *np = dev->of_node;
218
219 if (!np) {
220 dev_err(dev, "missing DT data");
221 return -EINVAL;
222 }
223
224 of_property_read_u32(np, "keypad,num-rows", &keypad_data->rows);
225 of_property_read_u32(np, "keypad,num-columns", &keypad_data->cols);
226 if (!keypad_data->rows || !keypad_data->cols) {
227 dev_err(dev, "number of keypad rows/columns not specified\n");
228 return -EINVAL;
229 }
230
231 if (of_get_property(np, "linux,input-no-autorepeat", NULL))
232 keypad_data->no_autorepeat = true;
233
234 return 0;
235}
236#else
237static inline int omap4_keypad_parse_dt(struct device *dev,
238 struct omap4_keypad *keypad_data)
239{
240 return -ENOSYS;
241}
242#endif
243
Bill Pemberton5298cc42012-11-23 21:38:25 -0800244static int omap4_keypad_probe(struct platform_device *pdev)
Abraham Arcea17f7952010-08-31 17:05:27 -0700245{
Sourav Poddar13987432012-07-13 00:10:47 -0700246 const struct omap4_keypad_platform_data *pdata =
247 dev_get_platdata(&pdev->dev);
248 const struct matrix_keymap_data *keymap_data =
249 pdata ? pdata->keymap_data : NULL;
Abraham Arcea17f7952010-08-31 17:05:27 -0700250 struct omap4_keypad *keypad_data;
251 struct input_dev *input_dev;
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700252 struct resource *res;
Sourav Poddar13987432012-07-13 00:10:47 -0700253 unsigned int max_keys;
Poddar, Souravf77621c2012-05-10 22:32:00 -0700254 int rev;
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700255 int irq;
Abraham Arcea17f7952010-08-31 17:05:27 -0700256 int error;
257
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700258 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
259 if (!res) {
Abraham Arcea17f7952010-08-31 17:05:27 -0700260 dev_err(&pdev->dev, "no base address specified\n");
261 return -EINVAL;
262 }
263
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700264 irq = platform_get_irq(pdev, 0);
265 if (!irq) {
Abraham Arcea17f7952010-08-31 17:05:27 -0700266 dev_err(&pdev->dev, "no keyboard irq assigned\n");
267 return -EINVAL;
268 }
269
Sourav Poddar13987432012-07-13 00:10:47 -0700270 keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
Abraham Arcea17f7952010-08-31 17:05:27 -0700271 if (!keypad_data) {
272 dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
273 return -ENOMEM;
274 }
275
Sourav Poddar13987432012-07-13 00:10:47 -0700276 keypad_data->irq = irq;
Abraham Arcea17f7952010-08-31 17:05:27 -0700277
Sourav Poddar13987432012-07-13 00:10:47 -0700278 if (pdata) {
279 keypad_data->rows = pdata->rows;
280 keypad_data->cols = pdata->cols;
281 } else {
282 error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
283 if (error)
284 return error;
285 }
286
287 res = request_mem_region(res->start, resource_size(res), pdev->name);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700288 if (!res) {
289 dev_err(&pdev->dev, "can't request mem region\n");
290 error = -EBUSY;
291 goto err_free_keypad;
292 }
293
294 keypad_data->base = ioremap(res->start, resource_size(res));
295 if (!keypad_data->base) {
296 dev_err(&pdev->dev, "can't ioremap mem resource\n");
297 error = -ENOMEM;
298 goto err_release_mem;
299 }
300
Abraham Arcea17f7952010-08-31 17:05:27 -0700301
Poddar, Souravf77621c2012-05-10 22:32:00 -0700302 /*
Sourav Poddar13987432012-07-13 00:10:47 -0700303 * Enable clocks for the keypad module so that we can read
304 * revision register.
305 */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700306 pm_runtime_enable(&pdev->dev);
307 error = pm_runtime_get_sync(&pdev->dev);
308 if (error) {
309 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
310 goto err_unmap;
311 }
312 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
313 rev &= 0x03 << 30;
314 rev >>= 30;
315 switch (rev) {
316 case KBD_REVISION_OMAP4:
317 keypad_data->reg_offset = 0x00;
318 keypad_data->irqreg_offset = 0x00;
319 break;
320 case KBD_REVISION_OMAP5:
321 keypad_data->reg_offset = 0x10;
322 keypad_data->irqreg_offset = 0x0c;
323 break;
324 default:
325 dev_err(&pdev->dev,
326 "Keypad reports unsupported revision %d", rev);
327 error = -EINVAL;
328 goto err_pm_put_sync;
329 }
330
Abraham Arcea17f7952010-08-31 17:05:27 -0700331 /* input device allocation */
332 keypad_data->input = input_dev = input_allocate_device();
333 if (!input_dev) {
334 error = -ENOMEM;
Poddar, Souravf77621c2012-05-10 22:32:00 -0700335 goto err_pm_put_sync;
Abraham Arcea17f7952010-08-31 17:05:27 -0700336 }
337
338 input_dev->name = pdev->name;
339 input_dev->dev.parent = &pdev->dev;
340 input_dev->id.bustype = BUS_HOST;
341 input_dev->id.vendor = 0x0001;
342 input_dev->id.product = 0x0001;
343 input_dev->id.version = 0x0001;
344
Abraham Arce5ad567f2011-02-22 22:25:59 -0800345 input_dev->open = omap4_keypad_open;
346 input_dev->close = omap4_keypad_close;
347
Sourav Poddar13987432012-07-13 00:10:47 -0700348 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
349 if (!keypad_data->no_autorepeat)
350 __set_bit(EV_REP, input_dev->evbit);
351
352 input_set_drvdata(input_dev, keypad_data);
353
354 keypad_data->row_shift = get_count_order(keypad_data->cols);
355 max_keys = keypad_data->rows << keypad_data->row_shift;
356 keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]),
357 GFP_KERNEL);
358 if (!keypad_data->keymap) {
359 dev_err(&pdev->dev, "Not enough memory for keymap\n");
360 error = -ENOMEM;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700361 goto err_free_input;
362 }
Abraham Arcea17f7952010-08-31 17:05:27 -0700363
Sourav Poddar13987432012-07-13 00:10:47 -0700364 error = matrix_keypad_build_keymap(keymap_data, NULL,
365 keypad_data->rows, keypad_data->cols,
366 keypad_data->keymap, input_dev);
367 if (error) {
368 dev_err(&pdev->dev, "failed to build keymap\n");
369 goto err_free_keymap;
370 }
Abraham Arcea17f7952010-08-31 17:05:27 -0700371
Abraham Arcea17f7952010-08-31 17:05:27 -0700372 error = request_irq(keypad_data->irq, omap4_keypad_interrupt,
Abraham Arcef8038c42010-09-29 23:37:04 -0700373 IRQF_TRIGGER_RISING,
Abraham Arcea17f7952010-08-31 17:05:27 -0700374 "omap4-keypad", keypad_data);
375 if (error) {
376 dev_err(&pdev->dev, "failed to register interrupt\n");
377 goto err_free_input;
378 }
379
Poddar, Souravf77621c2012-05-10 22:32:00 -0700380 pm_runtime_put_sync(&pdev->dev);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800381
Abraham Arcea17f7952010-08-31 17:05:27 -0700382 error = input_register_device(keypad_data->input);
383 if (error < 0) {
384 dev_err(&pdev->dev, "failed to register input device\n");
Abraham Arce5ad567f2011-02-22 22:25:59 -0800385 goto err_pm_disable;
Abraham Arcea17f7952010-08-31 17:05:27 -0700386 }
387
Abraham Arcea17f7952010-08-31 17:05:27 -0700388 platform_set_drvdata(pdev, keypad_data);
389 return 0;
390
Abraham Arce5ad567f2011-02-22 22:25:59 -0800391err_pm_disable:
392 pm_runtime_disable(&pdev->dev);
Abraham Arcea17f7952010-08-31 17:05:27 -0700393 free_irq(keypad_data->irq, keypad_data);
Sourav Poddar13987432012-07-13 00:10:47 -0700394err_free_keymap:
395 kfree(keypad_data->keymap);
Abraham Arcea17f7952010-08-31 17:05:27 -0700396err_free_input:
397 input_free_device(input_dev);
Poddar, Souravf77621c2012-05-10 22:32:00 -0700398err_pm_put_sync:
399 pm_runtime_put_sync(&pdev->dev);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700400err_unmap:
401 iounmap(keypad_data->base);
402err_release_mem:
Sourav Poddar13987432012-07-13 00:10:47 -0700403 release_mem_region(res->start, resource_size(res));
Abraham Arcea17f7952010-08-31 17:05:27 -0700404err_free_keypad:
405 kfree(keypad_data);
406 return error;
407}
408
Bill Pembertone2619cf2012-11-23 21:50:47 -0800409static int omap4_keypad_remove(struct platform_device *pdev)
Abraham Arcea17f7952010-08-31 17:05:27 -0700410{
411 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700412 struct resource *res;
Abraham Arcea17f7952010-08-31 17:05:27 -0700413
414 free_irq(keypad_data->irq, keypad_data);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800415
416 pm_runtime_disable(&pdev->dev);
417
Abraham Arcea17f7952010-08-31 17:05:27 -0700418 input_unregister_device(keypad_data->input);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700419
420 iounmap(keypad_data->base);
421
422 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
423 release_mem_region(res->start, resource_size(res));
424
Sourav Poddar13987432012-07-13 00:10:47 -0700425 kfree(keypad_data->keymap);
Abraham Arcea17f7952010-08-31 17:05:27 -0700426 kfree(keypad_data);
Sourav Poddar13987432012-07-13 00:10:47 -0700427
Abraham Arcea17f7952010-08-31 17:05:27 -0700428 platform_set_drvdata(pdev, NULL);
429
430 return 0;
431}
432
Sourav Poddar13987432012-07-13 00:10:47 -0700433#ifdef CONFIG_OF
434static const struct of_device_id omap_keypad_dt_match[] = {
435 { .compatible = "ti,omap4-keypad" },
436 {},
437};
438MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
439#endif
440
Abraham Arcea17f7952010-08-31 17:05:27 -0700441static struct platform_driver omap4_keypad_driver = {
442 .probe = omap4_keypad_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800443 .remove = omap4_keypad_remove,
Abraham Arcea17f7952010-08-31 17:05:27 -0700444 .driver = {
445 .name = "omap4-keypad",
446 .owner = THIS_MODULE,
Sourav Poddar13987432012-07-13 00:10:47 -0700447 .of_match_table = of_match_ptr(omap_keypad_dt_match),
Abraham Arcea17f7952010-08-31 17:05:27 -0700448 },
449};
JJ Ding5146c842011-11-29 11:08:39 -0800450module_platform_driver(omap4_keypad_driver);
Abraham Arcea17f7952010-08-31 17:05:27 -0700451
452MODULE_AUTHOR("Texas Instruments");
453MODULE_DESCRIPTION("OMAP4 Keypad Driver");
454MODULE_LICENSE("GPL");
455MODULE_ALIAS("platform:omap4-keypad");