blob: aed5f6999ce25d1277d6d709ec710beffc39841f [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>
30#include <linux/input.h>
31#include <linux/slab.h>
Abraham Arce5ad567f2011-02-22 22:25:59 -080032#include <linux/pm_runtime.h>
Abraham Arcea17f7952010-08-31 17:05:27 -070033
Felipe Balbi0f1142a2012-03-16 22:47:48 -070034#include <linux/platform_data/omap4-keypad.h>
Abraham Arcea17f7952010-08-31 17:05:27 -070035
36/* OMAP4 registers */
37#define OMAP4_KBD_REVISION 0x00
38#define OMAP4_KBD_SYSCONFIG 0x10
39#define OMAP4_KBD_SYSSTATUS 0x14
40#define OMAP4_KBD_IRQSTATUS 0x18
41#define OMAP4_KBD_IRQENABLE 0x1C
42#define OMAP4_KBD_WAKEUPENABLE 0x20
43#define OMAP4_KBD_PENDING 0x24
44#define OMAP4_KBD_CTRL 0x28
45#define OMAP4_KBD_DEBOUNCINGTIME 0x2C
46#define OMAP4_KBD_LONGKEYTIME 0x30
47#define OMAP4_KBD_TIMEOUT 0x34
48#define OMAP4_KBD_STATEMACHINE 0x38
49#define OMAP4_KBD_ROWINPUTS 0x3C
50#define OMAP4_KBD_COLUMNOUTPUTS 0x40
51#define OMAP4_KBD_FULLCODE31_0 0x44
52#define OMAP4_KBD_FULLCODE63_32 0x48
53
54/* OMAP4 bit definitions */
Abraham Arcea17f7952010-08-31 17:05:27 -070055#define OMAP4_DEF_IRQENABLE_EVENTEN (1 << 0)
56#define OMAP4_DEF_IRQENABLE_LONGKEY (1 << 1)
57#define OMAP4_DEF_IRQENABLE_TIMEOUTEN (1 << 2)
Mike Turquette9a34bc62010-09-29 23:38:01 -070058#define OMAP4_DEF_WUP_EVENT_ENA (1 << 0)
59#define OMAP4_DEF_WUP_LONG_KEY_ENA (1 << 1)
Abraham Arcea17f7952010-08-31 17:05:27 -070060#define OMAP4_DEF_CTRL_NOSOFTMODE (1 << 1)
61#define OMAP4_DEF_CTRLPTVVALUE (1 << 2)
62#define OMAP4_DEF_CTRLPTV (1 << 1)
Abraham Arcea17f7952010-08-31 17:05:27 -070063
64/* OMAP4 values */
Abraham Arce05362f42010-09-29 23:36:48 -070065#define OMAP4_VAL_IRQDISABLE 0x00
Abraham Arcea17f7952010-08-31 17:05:27 -070066#define OMAP4_VAL_DEBOUNCINGTIME 0x07
67#define OMAP4_VAL_FUNCTIONALCFG 0x1E
68
69#define OMAP4_MASK_IRQSTATUSDISABLE 0xFFFF
70
Poddar, Souravf77621c2012-05-10 22:32:00 -070071enum {
72 KBD_REVISION_OMAP4 = 0,
73 KBD_REVISION_OMAP5,
74};
75
Abraham Arcea17f7952010-08-31 17:05:27 -070076struct omap4_keypad {
77 struct input_dev *input;
78
79 void __iomem *base;
Poddar, Souravf77621c2012-05-10 22:32:00 -070080 unsigned int irq;
Abraham Arcea17f7952010-08-31 17:05:27 -070081
82 unsigned int rows;
83 unsigned int cols;
Poddar, Souravf77621c2012-05-10 22:32:00 -070084 u32 reg_offset;
85 u32 irqreg_offset;
Abraham Arcea17f7952010-08-31 17:05:27 -070086 unsigned int row_shift;
87 unsigned char key_state[8];
88 unsigned short keymap[];
89};
90
Poddar, Souravf77621c2012-05-10 22:32:00 -070091static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
92{
93 return __raw_readl(keypad_data->base +
94 keypad_data->reg_offset + offset);
95}
96
97static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
98{
99 __raw_writel(value,
100 keypad_data->base + keypad_data->reg_offset + offset);
101}
102
103static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
104{
105 return __raw_readl(keypad_data->base +
106 keypad_data->irqreg_offset + offset);
107}
108
109static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
110 u32 offset, u32 value)
111{
112 __raw_writel(value,
113 keypad_data->base + keypad_data->irqreg_offset + offset);
114}
115
116
Abraham Arcea17f7952010-08-31 17:05:27 -0700117/* Interrupt handler */
118static irqreturn_t omap4_keypad_interrupt(int irq, void *dev_id)
119{
120 struct omap4_keypad *keypad_data = dev_id;
121 struct input_dev *input_dev = keypad_data->input;
122 unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
123 unsigned int col, row, code, changed;
124 u32 *new_state = (u32 *) key_state;
125
126 /* Disable interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700127 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
128 OMAP4_VAL_IRQDISABLE);
Abraham Arcea17f7952010-08-31 17:05:27 -0700129
Poddar, Souravf77621c2012-05-10 22:32:00 -0700130 *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
131 *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
Abraham Arcea17f7952010-08-31 17:05:27 -0700132
133 for (row = 0; row < keypad_data->rows; row++) {
134 changed = key_state[row] ^ keypad_data->key_state[row];
135 if (!changed)
136 continue;
137
138 for (col = 0; col < keypad_data->cols; col++) {
139 if (changed & (1 << col)) {
140 code = MATRIX_SCAN_CODE(row, col,
141 keypad_data->row_shift);
142 input_event(input_dev, EV_MSC, MSC_SCAN, code);
143 input_report_key(input_dev,
144 keypad_data->keymap[code],
145 key_state[row] & (1 << col));
146 }
147 }
148 }
149
150 input_sync(input_dev);
151
152 memcpy(keypad_data->key_state, key_state,
153 sizeof(keypad_data->key_state));
154
155 /* clear pending interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700156 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
157 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
Abraham Arcea17f7952010-08-31 17:05:27 -0700158
159 /* enable interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700160 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
161 OMAP4_DEF_IRQENABLE_EVENTEN |
162 OMAP4_DEF_IRQENABLE_LONGKEY);
Abraham Arcea17f7952010-08-31 17:05:27 -0700163
164 return IRQ_HANDLED;
165}
166
Abraham Arce5ad567f2011-02-22 22:25:59 -0800167static int omap4_keypad_open(struct input_dev *input)
168{
169 struct omap4_keypad *keypad_data = input_get_drvdata(input);
170
171 pm_runtime_get_sync(input->dev.parent);
172
173 disable_irq(keypad_data->irq);
174
Poddar, Souravf77621c2012-05-10 22:32:00 -0700175 kbd_writel(keypad_data, OMAP4_KBD_CTRL,
176 OMAP4_VAL_FUNCTIONALCFG);
177 kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
178 OMAP4_VAL_DEBOUNCINGTIME);
179 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
180 OMAP4_VAL_IRQDISABLE);
181 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
182 OMAP4_DEF_IRQENABLE_EVENTEN |
183 OMAP4_DEF_IRQENABLE_LONGKEY);
184 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
185 OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800186
187 enable_irq(keypad_data->irq);
188
189 return 0;
190}
191
192static void omap4_keypad_close(struct input_dev *input)
193{
194 struct omap4_keypad *keypad_data = input_get_drvdata(input);
195
196 disable_irq(keypad_data->irq);
197
198 /* Disable interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700199 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
200 OMAP4_VAL_IRQDISABLE);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800201
202 /* clear pending interrupts */
Poddar, Souravf77621c2012-05-10 22:32:00 -0700203 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
204 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
Abraham Arce5ad567f2011-02-22 22:25:59 -0800205
206 enable_irq(keypad_data->irq);
207
208 pm_runtime_put_sync(input->dev.parent);
209}
210
Abraham Arcea17f7952010-08-31 17:05:27 -0700211static int __devinit omap4_keypad_probe(struct platform_device *pdev)
212{
213 const struct omap4_keypad_platform_data *pdata;
214 struct omap4_keypad *keypad_data;
215 struct input_dev *input_dev;
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700216 struct resource *res;
217 resource_size_t size;
Abraham Arcea17f7952010-08-31 17:05:27 -0700218 unsigned int row_shift, max_keys;
Poddar, Souravf77621c2012-05-10 22:32:00 -0700219 int rev;
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700220 int irq;
Abraham Arcea17f7952010-08-31 17:05:27 -0700221 int error;
222
223 /* platform data */
224 pdata = pdev->dev.platform_data;
225 if (!pdata) {
226 dev_err(&pdev->dev, "no platform data defined\n");
227 return -EINVAL;
228 }
229
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 if (!res) {
Abraham Arcea17f7952010-08-31 17:05:27 -0700232 dev_err(&pdev->dev, "no base address specified\n");
233 return -EINVAL;
234 }
235
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700236 irq = platform_get_irq(pdev, 0);
237 if (!irq) {
Abraham Arcea17f7952010-08-31 17:05:27 -0700238 dev_err(&pdev->dev, "no keyboard irq assigned\n");
239 return -EINVAL;
240 }
241
242 if (!pdata->keymap_data) {
243 dev_err(&pdev->dev, "no keymap data defined\n");
244 return -EINVAL;
245 }
246
247 row_shift = get_count_order(pdata->cols);
248 max_keys = pdata->rows << row_shift;
249
250 keypad_data = kzalloc(sizeof(struct omap4_keypad) +
251 max_keys * sizeof(keypad_data->keymap[0]),
252 GFP_KERNEL);
253 if (!keypad_data) {
254 dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
255 return -ENOMEM;
256 }
257
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700258 size = resource_size(res);
Abraham Arcea17f7952010-08-31 17:05:27 -0700259
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700260 res = request_mem_region(res->start, size, pdev->name);
261 if (!res) {
262 dev_err(&pdev->dev, "can't request mem region\n");
263 error = -EBUSY;
264 goto err_free_keypad;
265 }
266
267 keypad_data->base = ioremap(res->start, resource_size(res));
268 if (!keypad_data->base) {
269 dev_err(&pdev->dev, "can't ioremap mem resource\n");
270 error = -ENOMEM;
271 goto err_release_mem;
272 }
273
274 keypad_data->irq = irq;
Abraham Arcea17f7952010-08-31 17:05:27 -0700275 keypad_data->row_shift = row_shift;
276 keypad_data->rows = pdata->rows;
277 keypad_data->cols = pdata->cols;
278
Poddar, Souravf77621c2012-05-10 22:32:00 -0700279 /*
280 * Enable clocks for the keypad module so that we can read
281 * revision register.
282 */
283 pm_runtime_enable(&pdev->dev);
284 error = pm_runtime_get_sync(&pdev->dev);
285 if (error) {
286 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
287 goto err_unmap;
288 }
289 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
290 rev &= 0x03 << 30;
291 rev >>= 30;
292 switch (rev) {
293 case KBD_REVISION_OMAP4:
294 keypad_data->reg_offset = 0x00;
295 keypad_data->irqreg_offset = 0x00;
296 break;
297 case KBD_REVISION_OMAP5:
298 keypad_data->reg_offset = 0x10;
299 keypad_data->irqreg_offset = 0x0c;
300 break;
301 default:
302 dev_err(&pdev->dev,
303 "Keypad reports unsupported revision %d", rev);
304 error = -EINVAL;
305 goto err_pm_put_sync;
306 }
307
Abraham Arcea17f7952010-08-31 17:05:27 -0700308 /* input device allocation */
309 keypad_data->input = input_dev = input_allocate_device();
310 if (!input_dev) {
311 error = -ENOMEM;
Poddar, Souravf77621c2012-05-10 22:32:00 -0700312 goto err_pm_put_sync;
Abraham Arcea17f7952010-08-31 17:05:27 -0700313 }
314
315 input_dev->name = pdev->name;
316 input_dev->dev.parent = &pdev->dev;
317 input_dev->id.bustype = BUS_HOST;
318 input_dev->id.vendor = 0x0001;
319 input_dev->id.product = 0x0001;
320 input_dev->id.version = 0x0001;
321
Abraham Arce5ad567f2011-02-22 22:25:59 -0800322 input_dev->open = omap4_keypad_open;
323 input_dev->close = omap4_keypad_close;
324
Dmitry Torokhov19328112012-05-10 22:37:08 -0700325 error = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
326 pdata->rows, pdata->cols,
327 keypad_data->keymap, input_dev);
328 if (error) {
329 dev_err(&pdev->dev, "failed to build keymap\n");
330 goto err_free_input;
331 }
Abraham Arcea17f7952010-08-31 17:05:27 -0700332
Abraham Arcea17f7952010-08-31 17:05:27 -0700333 __set_bit(EV_REP, input_dev->evbit);
Abraham Arcea17f7952010-08-31 17:05:27 -0700334 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
335
336 input_set_drvdata(input_dev, keypad_data);
337
Abraham Arcea17f7952010-08-31 17:05:27 -0700338 error = request_irq(keypad_data->irq, omap4_keypad_interrupt,
Abraham Arcef8038c42010-09-29 23:37:04 -0700339 IRQF_TRIGGER_RISING,
Abraham Arcea17f7952010-08-31 17:05:27 -0700340 "omap4-keypad", keypad_data);
341 if (error) {
342 dev_err(&pdev->dev, "failed to register interrupt\n");
343 goto err_free_input;
344 }
345
Poddar, Souravf77621c2012-05-10 22:32:00 -0700346 pm_runtime_put_sync(&pdev->dev);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800347
Abraham Arcea17f7952010-08-31 17:05:27 -0700348 error = input_register_device(keypad_data->input);
349 if (error < 0) {
350 dev_err(&pdev->dev, "failed to register input device\n");
Abraham Arce5ad567f2011-02-22 22:25:59 -0800351 goto err_pm_disable;
Abraham Arcea17f7952010-08-31 17:05:27 -0700352 }
353
Abraham Arcea17f7952010-08-31 17:05:27 -0700354 platform_set_drvdata(pdev, keypad_data);
355 return 0;
356
Abraham Arce5ad567f2011-02-22 22:25:59 -0800357err_pm_disable:
358 pm_runtime_disable(&pdev->dev);
Abraham Arcea17f7952010-08-31 17:05:27 -0700359 free_irq(keypad_data->irq, keypad_data);
360err_free_input:
361 input_free_device(input_dev);
Poddar, Souravf77621c2012-05-10 22:32:00 -0700362err_pm_put_sync:
363 pm_runtime_put_sync(&pdev->dev);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700364err_unmap:
365 iounmap(keypad_data->base);
366err_release_mem:
367 release_mem_region(res->start, size);
Abraham Arcea17f7952010-08-31 17:05:27 -0700368err_free_keypad:
369 kfree(keypad_data);
370 return error;
371}
372
373static int __devexit omap4_keypad_remove(struct platform_device *pdev)
374{
375 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700376 struct resource *res;
Abraham Arcea17f7952010-08-31 17:05:27 -0700377
378 free_irq(keypad_data->irq, keypad_data);
Abraham Arce5ad567f2011-02-22 22:25:59 -0800379
380 pm_runtime_disable(&pdev->dev);
381
Abraham Arcea17f7952010-08-31 17:05:27 -0700382 input_unregister_device(keypad_data->input);
Abraham Arcef3a1ba62010-09-29 23:35:57 -0700383
384 iounmap(keypad_data->base);
385
386 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
387 release_mem_region(res->start, resource_size(res));
388
Abraham Arcea17f7952010-08-31 17:05:27 -0700389 kfree(keypad_data);
390 platform_set_drvdata(pdev, NULL);
391
392 return 0;
393}
394
395static struct platform_driver omap4_keypad_driver = {
396 .probe = omap4_keypad_probe,
397 .remove = __devexit_p(omap4_keypad_remove),
398 .driver = {
399 .name = "omap4-keypad",
400 .owner = THIS_MODULE,
401 },
402};
JJ Ding5146c842011-11-29 11:08:39 -0800403module_platform_driver(omap4_keypad_driver);
Abraham Arcea17f7952010-08-31 17:05:27 -0700404
405MODULE_AUTHOR("Texas Instruments");
406MODULE_DESCRIPTION("OMAP4 Keypad Driver");
407MODULE_LICENSE("GPL");
408MODULE_ALIAS("platform:omap4-keypad");