2 * Texas Instruments TNETV107X Keypad Driver
4 * Copyright (C) 2010 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/errno.h>
19 #include <linux/input.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
25 #include <linux/clk.h>
26 #include <linux/input/matrix_keypad.h>
28 #define BITS(x) (BIT(x) - 1)
33 #define DEBOUNCE_MIN 0x400ul
34 #define DEBOUNCE_MAX 0x3ffffffful
52 #define keypad_read(kp, reg) __raw_readl(&(kp)->regs->reg)
53 #define keypad_write(kp, reg, val) __raw_writel(val, &(kp)->regs->reg)
56 struct input_dev
*input_dev
;
58 struct keypad_regs __iomem
*regs
;
64 int rows
, cols
, row_shift
;
65 int debounce_ms
, active_low
;
67 unsigned short keycodes
[];
70 static irqreturn_t
keypad_irq(int irq
, void *data
)
72 struct keypad_data
*kp
= data
;
73 int i
, bit
, val
, row
, col
, code
;
78 spin_lock_irqsave(&kp
->lock
, flags
);
80 memset(curr_keys
, 0, sizeof(curr_keys
));
81 if (irq
== kp
->irq_press
)
82 for (i
= 0; i
< 3; i
++)
83 curr_keys
[i
] = keypad_read(kp
, pres
[i
]);
85 for (i
= 0; i
< 3; i
++) {
86 change
= curr_keys
[i
] ^ kp
->prev_keys
[i
];
89 bit
= fls(change
) - 1;
91 val
= curr_keys
[i
] & BIT(bit
);
93 row
= bit
/ KEYPAD_COLS
;
94 col
= bit
% KEYPAD_COLS
;
96 code
= MATRIX_SCAN_CODE(row
, col
, kp
->row_shift
);
97 input_event(kp
->input_dev
, EV_MSC
, MSC_SCAN
, code
);
98 input_report_key(kp
->input_dev
, kp
->keycodes
[code
],
102 input_sync(kp
->input_dev
);
103 memcpy(kp
->prev_keys
, curr_keys
, sizeof(curr_keys
));
105 if (irq
== kp
->irq_press
)
106 keypad_write(kp
, lock
, 0); /* Allow hardware updates */
108 spin_unlock_irqrestore(&kp
->lock
, flags
);
113 static int keypad_start(struct input_dev
*dev
)
115 struct keypad_data
*kp
= input_get_drvdata(dev
);
116 unsigned long mask
, debounce
, clk_rate_khz
;
120 clk_rate_khz
= clk_get_rate(kp
->clk
) / 1000;
122 spin_lock_irqsave(&kp
->lock
, flags
);
124 /* Initialize device registers */
125 keypad_write(kp
, mode
, 0);
127 mask
= BITS(kp
->rows
) << KEYPAD_COLS
;
128 mask
|= BITS(kp
->cols
);
129 keypad_write(kp
, mask
, ~mask
);
131 keypad_write(kp
, pol
, kp
->active_low
? 0 : 0x3ffff);
132 keypad_write(kp
, stable_cnt
, 3);
134 debounce
= kp
->debounce_ms
* clk_rate_khz
;
135 debounce
= clamp(debounce
, DEBOUNCE_MIN
, DEBOUNCE_MAX
);
136 keypad_write(kp
, dclock
, debounce
);
137 keypad_write(kp
, rclock
, 4 * debounce
);
139 keypad_write(kp
, in_en
, 1);
141 spin_unlock_irqrestore(&kp
->lock
, flags
);
146 static void keypad_stop(struct input_dev
*dev
)
148 struct keypad_data
*kp
= input_get_drvdata(dev
);
150 synchronize_irq(kp
->irq_press
);
151 synchronize_irq(kp
->irq_release
);
152 clk_disable(kp
->clk
);
155 static int __devinit
keypad_probe(struct platform_device
*pdev
)
157 const struct matrix_keypad_platform_data
*pdata
;
158 const struct matrix_keymap_data
*keymap_data
;
159 struct device
*dev
= &pdev
->dev
;
160 struct keypad_data
*kp
;
161 int error
= 0, sz
, row_shift
;
164 pdata
= pdev
->dev
.platform_data
;
166 dev_err(dev
, "cannot find device data\n");
170 keymap_data
= pdata
->keymap_data
;
172 dev_err(dev
, "cannot find keymap data\n");
176 row_shift
= get_count_order(pdata
->num_col_gpios
);
177 sz
= offsetof(struct keypad_data
, keycodes
);
178 sz
+= (pdata
->num_row_gpios
<< row_shift
) * sizeof(kp
->keycodes
[0]);
179 kp
= kzalloc(sz
, GFP_KERNEL
);
181 dev_err(dev
, "cannot allocate device info\n");
186 kp
->rows
= pdata
->num_row_gpios
;
187 kp
->cols
= pdata
->num_col_gpios
;
188 kp
->row_shift
= row_shift
;
189 platform_set_drvdata(pdev
, kp
);
190 spin_lock_init(&kp
->lock
);
192 kp
->irq_press
= platform_get_irq_byname(pdev
, "press");
193 kp
->irq_release
= platform_get_irq_byname(pdev
, "release");
194 if (kp
->irq_press
< 0 || kp
->irq_release
< 0) {
195 dev_err(dev
, "cannot determine device interrupts\n");
200 kp
->res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
202 dev_err(dev
, "cannot determine register area\n");
207 if (!request_mem_region(kp
->res
->start
, resource_size(kp
->res
),
209 dev_err(dev
, "cannot claim register memory\n");
215 kp
->regs
= ioremap(kp
->res
->start
, resource_size(kp
->res
));
217 dev_err(dev
, "cannot map register memory\n");
222 kp
->clk
= clk_get(dev
, NULL
);
223 if (IS_ERR(kp
->clk
)) {
224 dev_err(dev
, "cannot claim device clock\n");
225 error
= PTR_ERR(kp
->clk
);
229 error
= request_threaded_irq(kp
->irq_press
, NULL
, keypad_irq
, 0,
232 dev_err(kp
->dev
, "Could not allocate keypad press key irq\n");
233 goto error_irq_press
;
236 error
= request_threaded_irq(kp
->irq_release
, NULL
, keypad_irq
, 0,
239 dev_err(kp
->dev
, "Could not allocate keypad release key irq\n");
240 goto error_irq_release
;
243 kp
->input_dev
= input_allocate_device();
244 if (!kp
->input_dev
) {
245 dev_err(dev
, "cannot allocate input device\n");
249 input_set_drvdata(kp
->input_dev
, kp
);
251 kp
->input_dev
->name
= pdev
->name
;
252 kp
->input_dev
->dev
.parent
= &pdev
->dev
;
253 kp
->input_dev
->open
= keypad_start
;
254 kp
->input_dev
->close
= keypad_stop
;
255 kp
->input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
256 if (!pdata
->no_autorepeat
)
257 kp
->input_dev
->evbit
[0] |= BIT_MASK(EV_REP
);
260 rev
= keypad_read(kp
, rev
);
261 kp
->input_dev
->id
.bustype
= BUS_HOST
;
262 kp
->input_dev
->id
.product
= ((rev
>> 8) & 0x07);
263 kp
->input_dev
->id
.version
= ((rev
>> 16) & 0xfff);
264 clk_disable(kp
->clk
);
266 kp
->input_dev
->keycode
= kp
->keycodes
;
267 kp
->input_dev
->keycodesize
= sizeof(kp
->keycodes
[0]);
268 kp
->input_dev
->keycodemax
= kp
->rows
<< kp
->row_shift
;
270 matrix_keypad_build_keymap(keymap_data
, kp
->row_shift
, kp
->keycodes
,
271 kp
->input_dev
->keybit
);
273 input_set_capability(kp
->input_dev
, EV_MSC
, MSC_SCAN
);
275 error
= input_register_device(kp
->input_dev
);
277 dev_err(dev
, "Could not register input device\n");
285 input_free_device(kp
->input_dev
);
287 free_irq(kp
->irq_release
, kp
);
289 free_irq(kp
->irq_press
, kp
);
295 release_mem_region(kp
->res
->start
, resource_size(kp
->res
));
297 platform_set_drvdata(pdev
, NULL
);
302 static int __devexit
keypad_remove(struct platform_device
*pdev
)
304 struct keypad_data
*kp
= platform_get_drvdata(pdev
);
306 free_irq(kp
->irq_press
, kp
);
307 free_irq(kp
->irq_release
, kp
);
308 input_unregister_device(kp
->input_dev
);
311 release_mem_region(kp
->res
->start
, resource_size(kp
->res
));
312 platform_set_drvdata(pdev
, NULL
);
318 static struct platform_driver keypad_driver
= {
319 .probe
= keypad_probe
,
320 .remove
= __devexit_p(keypad_remove
),
321 .driver
.name
= "tnetv107x-keypad",
322 .driver
.owner
= THIS_MODULE
,
325 static int __init
keypad_init(void)
327 return platform_driver_register(&keypad_driver
);
330 static void __exit
keypad_exit(void)
332 platform_driver_unregister(&keypad_driver
);
335 module_init(keypad_init
);
336 module_exit(keypad_exit
);
338 MODULE_AUTHOR("Cyril Chemparathy");
339 MODULE_DESCRIPTION("TNETV107X Keypad Driver");
340 MODULE_ALIAS("platform:tnetv107x-keypad");
341 MODULE_LICENSE("GPL");