Loading...
1/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * Author: Jayeeta Banerjee <jayeeta.banerjee@stericsson.com>
5 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
6 *
7 * License Terms: GNU General Public License, version 2
8 *
9 * TC35893 MFD Keypad Controller driver
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/input.h>
16#include <linux/platform_device.h>
17#include <linux/input/matrix_keypad.h>
18#include <linux/i2c.h>
19#include <linux/slab.h>
20#include <linux/mfd/tc3589x.h>
21
22/* Maximum supported keypad matrix row/columns size */
23#define TC3589x_MAX_KPROW 8
24#define TC3589x_MAX_KPCOL 12
25
26/* keypad related Constants */
27#define TC3589x_MAX_DEBOUNCE_SETTLE 0xFF
28#define DEDICATED_KEY_VAL 0xFF
29
30/* Pull up/down masks */
31#define TC3589x_NO_PULL_MASK 0x0
32#define TC3589x_PULL_DOWN_MASK 0x1
33#define TC3589x_PULL_UP_MASK 0x2
34#define TC3589x_PULLUP_ALL_MASK 0xAA
35#define TC3589x_IO_PULL_VAL(index, mask) ((mask)<<((index)%4)*2))
36
37/* Bit masks for IOCFG register */
38#define IOCFG_BALLCFG 0x01
39#define IOCFG_IG 0x08
40
41#define KP_EVCODE_COL_MASK 0x0F
42#define KP_EVCODE_ROW_MASK 0x70
43#define KP_RELEASE_EVT_MASK 0x80
44
45#define KP_ROW_SHIFT 4
46
47#define KP_NO_VALID_KEY_MASK 0x7F
48
49/* bit masks for RESTCTRL register */
50#define TC3589x_KBDRST 0x2
51#define TC3589x_IRQRST 0x10
52#define TC3589x_RESET_ALL 0x1B
53
54/* KBDMFS register bit mask */
55#define TC3589x_KBDMFS_EN 0x1
56
57/* CLKEN register bitmask */
58#define KPD_CLK_EN 0x1
59
60/* RSTINTCLR register bit mask */
61#define IRQ_CLEAR 0x1
62
63/* bit masks for keyboard interrupts*/
64#define TC3589x_EVT_LOSS_INT 0x8
65#define TC3589x_EVT_INT 0x4
66#define TC3589x_KBD_LOSS_INT 0x2
67#define TC3589x_KBD_INT 0x1
68
69/* bit masks for keyboard interrupt clear*/
70#define TC3589x_EVT_INT_CLR 0x2
71#define TC3589x_KBD_INT_CLR 0x1
72
73#define TC3589x_KBD_KEYMAP_SIZE 64
74
75/**
76 * struct tc_keypad - data structure used by keypad driver
77 * @input: pointer to input device object
78 * @board: keypad platform device
79 * @krow: number of rows
80 * @kcol: number of coloumns
81 * @keymap: matrix scan code table for keycodes
82 */
83struct tc_keypad {
84 struct tc3589x *tc3589x;
85 struct input_dev *input;
86 const struct tc3589x_keypad_platform_data *board;
87 unsigned int krow;
88 unsigned int kcol;
89 unsigned short keymap[TC3589x_KBD_KEYMAP_SIZE];
90 bool keypad_stopped;
91};
92
93static int __devinit tc3589x_keypad_init_key_hardware(struct tc_keypad *keypad)
94{
95 int ret;
96 struct tc3589x *tc3589x = keypad->tc3589x;
97 u8 settle_time = keypad->board->settle_time;
98 u8 dbounce_period = keypad->board->debounce_period;
99 u8 rows = keypad->board->krow & 0xf; /* mask out the nibble */
100 u8 column = keypad->board->kcol & 0xf; /* mask out the nibble */
101
102 /* validate platform configurations */
103 if (keypad->board->kcol > TC3589x_MAX_KPCOL ||
104 keypad->board->krow > TC3589x_MAX_KPROW ||
105 keypad->board->debounce_period > TC3589x_MAX_DEBOUNCE_SETTLE ||
106 keypad->board->settle_time > TC3589x_MAX_DEBOUNCE_SETTLE)
107 return -EINVAL;
108
109 /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
110 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
111 (rows << KP_ROW_SHIFT) | column);
112 if (ret < 0)
113 return ret;
114
115 /* configure dedicated key config, no dedicated key selected */
116 ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
117 if (ret < 0)
118 return ret;
119
120 ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
121 if (ret < 0)
122 return ret;
123
124 /* Configure settle time */
125 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG, settle_time);
126 if (ret < 0)
127 return ret;
128
129 /* Configure debounce time */
130 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE, dbounce_period);
131 if (ret < 0)
132 return ret;
133
134 /* Start of initialise keypad GPIOs */
135 ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
136 if (ret < 0)
137 return ret;
138
139 /* Configure pull-up resistors for all row GPIOs */
140 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
141 TC3589x_PULLUP_ALL_MASK);
142 if (ret < 0)
143 return ret;
144
145 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
146 TC3589x_PULLUP_ALL_MASK);
147 if (ret < 0)
148 return ret;
149
150 /* Configure pull-up resistors for all column GPIOs */
151 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
152 TC3589x_PULLUP_ALL_MASK);
153 if (ret < 0)
154 return ret;
155
156 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
157 TC3589x_PULLUP_ALL_MASK);
158 if (ret < 0)
159 return ret;
160
161 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
162 TC3589x_PULLUP_ALL_MASK);
163
164 return ret;
165}
166
167#define TC35893_DATA_REGS 4
168#define TC35893_KEYCODE_FIFO_EMPTY 0x7f
169#define TC35893_KEYCODE_FIFO_CLEAR 0xff
170#define TC35893_KEYPAD_ROW_SHIFT 0x3
171
172static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
173{
174 struct tc_keypad *keypad = dev;
175 struct tc3589x *tc3589x = keypad->tc3589x;
176 u8 i, row_index, col_index, kbd_code, up;
177 u8 code;
178
179 for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
180 kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
181
182 /* loop till fifo is empty and no more keys are pressed */
183 if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
184 kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
185 continue;
186
187 /* valid key is found */
188 col_index = kbd_code & KP_EVCODE_COL_MASK;
189 row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
190 code = MATRIX_SCAN_CODE(row_index, col_index,
191 TC35893_KEYPAD_ROW_SHIFT);
192 up = kbd_code & KP_RELEASE_EVT_MASK;
193
194 input_event(keypad->input, EV_MSC, MSC_SCAN, code);
195 input_report_key(keypad->input, keypad->keymap[code], !up);
196 input_sync(keypad->input);
197 }
198
199 /* clear IRQ */
200 tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
201 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
202 /* enable IRQ */
203 tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
204 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
205
206 return IRQ_HANDLED;
207}
208
209static int tc3589x_keypad_enable(struct tc_keypad *keypad)
210{
211 struct tc3589x *tc3589x = keypad->tc3589x;
212 int ret;
213
214 /* pull the keypad module out of reset */
215 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
216 if (ret < 0)
217 return ret;
218
219 /* configure KBDMFS */
220 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
221 if (ret < 0)
222 return ret;
223
224 /* enable the keypad clock */
225 ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
226 if (ret < 0)
227 return ret;
228
229 /* clear pending IRQs */
230 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
231 if (ret < 0)
232 return ret;
233
234 /* enable the IRQs */
235 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
236 TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
237 if (ret < 0)
238 return ret;
239
240 keypad->keypad_stopped = false;
241
242 return ret;
243}
244
245static int tc3589x_keypad_disable(struct tc_keypad *keypad)
246{
247 struct tc3589x *tc3589x = keypad->tc3589x;
248 int ret;
249
250 /* clear IRQ */
251 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
252 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
253 if (ret < 0)
254 return ret;
255
256 /* disable all interrupts */
257 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
258 ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
259 if (ret < 0)
260 return ret;
261
262 /* disable the keypad module */
263 ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
264 if (ret < 0)
265 return ret;
266
267 /* put the keypad module into reset */
268 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
269
270 keypad->keypad_stopped = true;
271
272 return ret;
273}
274
275static int tc3589x_keypad_open(struct input_dev *input)
276{
277 int error;
278 struct tc_keypad *keypad = input_get_drvdata(input);
279
280 /* enable the keypad module */
281 error = tc3589x_keypad_enable(keypad);
282 if (error < 0) {
283 dev_err(&input->dev, "failed to enable keypad module\n");
284 return error;
285 }
286
287 error = tc3589x_keypad_init_key_hardware(keypad);
288 if (error < 0) {
289 dev_err(&input->dev, "failed to configure keypad module\n");
290 return error;
291 }
292
293 return 0;
294}
295
296static void tc3589x_keypad_close(struct input_dev *input)
297{
298 struct tc_keypad *keypad = input_get_drvdata(input);
299
300 /* disable the keypad module */
301 tc3589x_keypad_disable(keypad);
302}
303
304static int __devinit tc3589x_keypad_probe(struct platform_device *pdev)
305{
306 struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
307 struct tc_keypad *keypad;
308 struct input_dev *input;
309 const struct tc3589x_keypad_platform_data *plat;
310 int error, irq;
311
312 plat = tc3589x->pdata->keypad;
313 if (!plat) {
314 dev_err(&pdev->dev, "invalid keypad platform data\n");
315 return -EINVAL;
316 }
317
318 irq = platform_get_irq(pdev, 0);
319 if (irq < 0)
320 return irq;
321
322 keypad = kzalloc(sizeof(struct tc_keypad), GFP_KERNEL);
323 input = input_allocate_device();
324 if (!keypad || !input) {
325 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
326 error = -ENOMEM;
327 goto err_free_mem;
328 }
329
330 keypad->board = plat;
331 keypad->input = input;
332 keypad->tc3589x = tc3589x;
333
334 input->id.bustype = BUS_I2C;
335 input->name = pdev->name;
336 input->dev.parent = &pdev->dev;
337
338 input->keycode = keypad->keymap;
339 input->keycodesize = sizeof(keypad->keymap[0]);
340 input->keycodemax = ARRAY_SIZE(keypad->keymap);
341
342 input->open = tc3589x_keypad_open;
343 input->close = tc3589x_keypad_close;
344
345 input_set_drvdata(input, keypad);
346
347 input_set_capability(input, EV_MSC, MSC_SCAN);
348
349 __set_bit(EV_KEY, input->evbit);
350 if (!plat->no_autorepeat)
351 __set_bit(EV_REP, input->evbit);
352
353 matrix_keypad_build_keymap(plat->keymap_data, 0x3,
354 input->keycode, input->keybit);
355
356 error = request_threaded_irq(irq, NULL,
357 tc3589x_keypad_irq, plat->irqtype,
358 "tc3589x-keypad", keypad);
359 if (error < 0) {
360 dev_err(&pdev->dev,
361 "Could not allocate irq %d,error %d\n",
362 irq, error);
363 goto err_free_mem;
364 }
365
366 error = input_register_device(input);
367 if (error) {
368 dev_err(&pdev->dev, "Could not register input device\n");
369 goto err_free_irq;
370 }
371
372 /* let platform decide if keypad is a wakeup source or not */
373 device_init_wakeup(&pdev->dev, plat->enable_wakeup);
374 device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
375
376 platform_set_drvdata(pdev, keypad);
377
378 return 0;
379
380err_free_irq:
381 free_irq(irq, keypad);
382err_free_mem:
383 input_free_device(input);
384 kfree(keypad);
385 return error;
386}
387
388static int __devexit tc3589x_keypad_remove(struct platform_device *pdev)
389{
390 struct tc_keypad *keypad = platform_get_drvdata(pdev);
391 int irq = platform_get_irq(pdev, 0);
392
393 if (!keypad->keypad_stopped)
394 tc3589x_keypad_disable(keypad);
395
396 free_irq(irq, keypad);
397
398 input_unregister_device(keypad->input);
399
400 kfree(keypad);
401
402 return 0;
403}
404
405#ifdef CONFIG_PM_SLEEP
406static int tc3589x_keypad_suspend(struct device *dev)
407{
408 struct platform_device *pdev = to_platform_device(dev);
409 struct tc_keypad *keypad = platform_get_drvdata(pdev);
410 int irq = platform_get_irq(pdev, 0);
411
412 /* keypad is already off; we do nothing */
413 if (keypad->keypad_stopped)
414 return 0;
415
416 /* if device is not a wakeup source, disable it for powersave */
417 if (!device_may_wakeup(&pdev->dev))
418 tc3589x_keypad_disable(keypad);
419 else
420 enable_irq_wake(irq);
421
422 return 0;
423}
424
425static int tc3589x_keypad_resume(struct device *dev)
426{
427 struct platform_device *pdev = to_platform_device(dev);
428 struct tc_keypad *keypad = platform_get_drvdata(pdev);
429 int irq = platform_get_irq(pdev, 0);
430
431 if (!keypad->keypad_stopped)
432 return 0;
433
434 /* enable the device to resume normal operations */
435 if (!device_may_wakeup(&pdev->dev))
436 tc3589x_keypad_enable(keypad);
437 else
438 disable_irq_wake(irq);
439
440 return 0;
441}
442#endif
443
444static SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
445 tc3589x_keypad_suspend, tc3589x_keypad_resume);
446
447static struct platform_driver tc3589x_keypad_driver = {
448 .driver = {
449 .name = "tc3589x-keypad",
450 .owner = THIS_MODULE,
451 .pm = &tc3589x_keypad_dev_pm_ops,
452 },
453 .probe = tc3589x_keypad_probe,
454 .remove = __devexit_p(tc3589x_keypad_remove),
455};
456
457static int __init tc3589x_keypad_init(void)
458{
459 return platform_driver_register(&tc3589x_keypad_driver);
460}
461module_init(tc3589x_keypad_init);
462
463static void __exit tc3589x_keypad_exit(void)
464{
465 return platform_driver_unregister(&tc3589x_keypad_driver);
466}
467module_exit(tc3589x_keypad_exit);
468
469MODULE_LICENSE("GPL v2");
470MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
471MODULE_DESCRIPTION("TC35893 Keypad Driver");
472MODULE_ALIAS("platform:tc3589x-keypad");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) ST-Ericsson SA 2010
4 *
5 * Author: Jayeeta Banerjee <jayeeta.banerjee@stericsson.com>
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
7 *
8 * TC35893 MFD Keypad Controller driver
9 */
10
11#include <linux/module.h>
12#include <linux/interrupt.h>
13#include <linux/input.h>
14#include <linux/platform_device.h>
15#include <linux/input/matrix_keypad.h>
16#include <linux/i2c.h>
17#include <linux/slab.h>
18#include <linux/mfd/tc3589x.h>
19#include <linux/device.h>
20
21/* Maximum supported keypad matrix row/columns size */
22#define TC3589x_MAX_KPROW 8
23#define TC3589x_MAX_KPCOL 12
24
25/* keypad related Constants */
26#define TC3589x_MAX_DEBOUNCE_SETTLE 0xFF
27#define DEDICATED_KEY_VAL 0xFF
28
29/* Pull up/down masks */
30#define TC3589x_NO_PULL_MASK 0x0
31#define TC3589x_PULL_DOWN_MASK 0x1
32#define TC3589x_PULL_UP_MASK 0x2
33#define TC3589x_PULLUP_ALL_MASK 0xAA
34#define TC3589x_IO_PULL_VAL(index, mask) ((mask)<<((index)%4)*2)
35
36/* Bit masks for IOCFG register */
37#define IOCFG_BALLCFG 0x01
38#define IOCFG_IG 0x08
39
40#define KP_EVCODE_COL_MASK 0x0F
41#define KP_EVCODE_ROW_MASK 0x70
42#define KP_RELEASE_EVT_MASK 0x80
43
44#define KP_ROW_SHIFT 4
45
46#define KP_NO_VALID_KEY_MASK 0x7F
47
48/* bit masks for RESTCTRL register */
49#define TC3589x_KBDRST 0x2
50#define TC3589x_IRQRST 0x10
51#define TC3589x_RESET_ALL 0x1B
52
53/* KBDMFS register bit mask */
54#define TC3589x_KBDMFS_EN 0x1
55
56/* CLKEN register bitmask */
57#define KPD_CLK_EN 0x1
58
59/* RSTINTCLR register bit mask */
60#define IRQ_CLEAR 0x1
61
62/* bit masks for keyboard interrupts*/
63#define TC3589x_EVT_LOSS_INT 0x8
64#define TC3589x_EVT_INT 0x4
65#define TC3589x_KBD_LOSS_INT 0x2
66#define TC3589x_KBD_INT 0x1
67
68/* bit masks for keyboard interrupt clear*/
69#define TC3589x_EVT_INT_CLR 0x2
70#define TC3589x_KBD_INT_CLR 0x1
71
72/**
73 * struct tc3589x_keypad_platform_data - platform specific keypad data
74 * @keymap_data: matrix scan code table for keycodes
75 * @krow: mask for available rows, value is 0xFF
76 * @kcol: mask for available columns, value is 0xFF
77 * @debounce_period: platform specific debounce time
78 * @settle_time: platform specific settle down time
79 * @irqtype: type of interrupt, falling or rising edge
80 * @enable_wakeup: specifies if keypad event can wake up system from sleep
81 * @no_autorepeat: flag for auto repetition
82 */
83struct tc3589x_keypad_platform_data {
84 const struct matrix_keymap_data *keymap_data;
85 u8 krow;
86 u8 kcol;
87 u8 debounce_period;
88 u8 settle_time;
89 unsigned long irqtype;
90 bool enable_wakeup;
91 bool no_autorepeat;
92};
93
94/**
95 * struct tc_keypad - data structure used by keypad driver
96 * @tc3589x: pointer to tc35893
97 * @input: pointer to input device object
98 * @board: keypad platform device
99 * @krow: number of rows
100 * @kcol: number of columns
101 * @keymap: matrix scan code table for keycodes
102 * @keypad_stopped: holds keypad status
103 */
104struct tc_keypad {
105 struct tc3589x *tc3589x;
106 struct input_dev *input;
107 const struct tc3589x_keypad_platform_data *board;
108 unsigned int krow;
109 unsigned int kcol;
110 unsigned short *keymap;
111 bool keypad_stopped;
112};
113
114static int tc3589x_keypad_init_key_hardware(struct tc_keypad *keypad)
115{
116 int ret;
117 struct tc3589x *tc3589x = keypad->tc3589x;
118 const struct tc3589x_keypad_platform_data *board = keypad->board;
119
120 /* validate platform configuration */
121 if (board->kcol > TC3589x_MAX_KPCOL || board->krow > TC3589x_MAX_KPROW)
122 return -EINVAL;
123
124 /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
125 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
126 (board->krow << KP_ROW_SHIFT) | board->kcol);
127 if (ret < 0)
128 return ret;
129
130 /* configure dedicated key config, no dedicated key selected */
131 ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
132 if (ret < 0)
133 return ret;
134
135 ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
136 if (ret < 0)
137 return ret;
138
139 /* Configure settle time */
140 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG,
141 board->settle_time);
142 if (ret < 0)
143 return ret;
144
145 /* Configure debounce time */
146 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE,
147 board->debounce_period);
148 if (ret < 0)
149 return ret;
150
151 /* Start of initialise keypad GPIOs */
152 ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
153 if (ret < 0)
154 return ret;
155
156 /* Configure pull-up resistors for all row GPIOs */
157 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
158 TC3589x_PULLUP_ALL_MASK);
159 if (ret < 0)
160 return ret;
161
162 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
163 TC3589x_PULLUP_ALL_MASK);
164 if (ret < 0)
165 return ret;
166
167 /* Configure pull-up resistors for all column GPIOs */
168 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
169 TC3589x_PULLUP_ALL_MASK);
170 if (ret < 0)
171 return ret;
172
173 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
174 TC3589x_PULLUP_ALL_MASK);
175 if (ret < 0)
176 return ret;
177
178 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
179 TC3589x_PULLUP_ALL_MASK);
180
181 return ret;
182}
183
184#define TC35893_DATA_REGS 4
185#define TC35893_KEYCODE_FIFO_EMPTY 0x7f
186#define TC35893_KEYCODE_FIFO_CLEAR 0xff
187#define TC35893_KEYPAD_ROW_SHIFT 0x3
188
189static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
190{
191 struct tc_keypad *keypad = dev;
192 struct tc3589x *tc3589x = keypad->tc3589x;
193 u8 i, row_index, col_index, kbd_code, up;
194 u8 code;
195
196 for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
197 kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
198
199 /* loop till fifo is empty and no more keys are pressed */
200 if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
201 kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
202 continue;
203
204 /* valid key is found */
205 col_index = kbd_code & KP_EVCODE_COL_MASK;
206 row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
207 code = MATRIX_SCAN_CODE(row_index, col_index,
208 TC35893_KEYPAD_ROW_SHIFT);
209 up = kbd_code & KP_RELEASE_EVT_MASK;
210
211 input_event(keypad->input, EV_MSC, MSC_SCAN, code);
212 input_report_key(keypad->input, keypad->keymap[code], !up);
213 input_sync(keypad->input);
214 }
215
216 /* clear IRQ */
217 tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
218 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
219 /* enable IRQ */
220 tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
221 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
222
223 return IRQ_HANDLED;
224}
225
226static int tc3589x_keypad_enable(struct tc_keypad *keypad)
227{
228 struct tc3589x *tc3589x = keypad->tc3589x;
229 int ret;
230
231 /* pull the keypad module out of reset */
232 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
233 if (ret < 0)
234 return ret;
235
236 /* configure KBDMFS */
237 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
238 if (ret < 0)
239 return ret;
240
241 /* enable the keypad clock */
242 ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
243 if (ret < 0)
244 return ret;
245
246 /* clear pending IRQs */
247 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
248 if (ret < 0)
249 return ret;
250
251 /* enable the IRQs */
252 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
253 TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
254 if (ret < 0)
255 return ret;
256
257 keypad->keypad_stopped = false;
258
259 return ret;
260}
261
262static int tc3589x_keypad_disable(struct tc_keypad *keypad)
263{
264 struct tc3589x *tc3589x = keypad->tc3589x;
265 int ret;
266
267 /* clear IRQ */
268 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
269 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
270 if (ret < 0)
271 return ret;
272
273 /* disable all interrupts */
274 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
275 ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
276 if (ret < 0)
277 return ret;
278
279 /* disable the keypad module */
280 ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
281 if (ret < 0)
282 return ret;
283
284 /* put the keypad module into reset */
285 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
286
287 keypad->keypad_stopped = true;
288
289 return ret;
290}
291
292static int tc3589x_keypad_open(struct input_dev *input)
293{
294 int error;
295 struct tc_keypad *keypad = input_get_drvdata(input);
296
297 /* enable the keypad module */
298 error = tc3589x_keypad_enable(keypad);
299 if (error < 0) {
300 dev_err(&input->dev, "failed to enable keypad module\n");
301 return error;
302 }
303
304 error = tc3589x_keypad_init_key_hardware(keypad);
305 if (error < 0) {
306 dev_err(&input->dev, "failed to configure keypad module\n");
307 return error;
308 }
309
310 return 0;
311}
312
313static void tc3589x_keypad_close(struct input_dev *input)
314{
315 struct tc_keypad *keypad = input_get_drvdata(input);
316
317 /* disable the keypad module */
318 tc3589x_keypad_disable(keypad);
319}
320
321static const struct tc3589x_keypad_platform_data *
322tc3589x_keypad_of_probe(struct device *dev)
323{
324 struct device_node *np = dev->of_node;
325 struct tc3589x_keypad_platform_data *plat;
326 u32 cols, rows;
327 u32 debounce_ms;
328 int proplen;
329
330 if (!np)
331 return ERR_PTR(-ENODEV);
332
333 plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
334 if (!plat)
335 return ERR_PTR(-ENOMEM);
336
337 of_property_read_u32(np, "keypad,num-columns", &cols);
338 of_property_read_u32(np, "keypad,num-rows", &rows);
339 plat->kcol = (u8) cols;
340 plat->krow = (u8) rows;
341 if (!plat->krow || !plat->kcol ||
342 plat->krow > TC_KPD_ROWS || plat->kcol > TC_KPD_COLUMNS) {
343 dev_err(dev,
344 "keypad columns/rows not properly specified (%ux%u)\n",
345 plat->kcol, plat->krow);
346 return ERR_PTR(-EINVAL);
347 }
348
349 if (!of_get_property(np, "linux,keymap", &proplen)) {
350 dev_err(dev, "property linux,keymap not found\n");
351 return ERR_PTR(-ENOENT);
352 }
353
354 plat->no_autorepeat = of_property_read_bool(np, "linux,no-autorepeat");
355
356 plat->enable_wakeup = of_property_read_bool(np, "wakeup-source") ||
357 /* legacy name */
358 of_property_read_bool(np, "linux,wakeup");
359
360 /* The custom delay format is ms/16 */
361 of_property_read_u32(np, "debounce-delay-ms", &debounce_ms);
362 if (debounce_ms)
363 plat->debounce_period = debounce_ms * 16;
364 else
365 plat->debounce_period = TC_KPD_DEBOUNCE_PERIOD;
366
367 plat->settle_time = TC_KPD_SETTLE_TIME;
368 /* FIXME: should be property of the IRQ resource? */
369 plat->irqtype = IRQF_TRIGGER_FALLING;
370
371 return plat;
372}
373
374static int tc3589x_keypad_probe(struct platform_device *pdev)
375{
376 struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
377 struct tc_keypad *keypad;
378 struct input_dev *input;
379 const struct tc3589x_keypad_platform_data *plat;
380 int error, irq;
381
382 plat = tc3589x_keypad_of_probe(&pdev->dev);
383 if (IS_ERR(plat)) {
384 dev_err(&pdev->dev, "invalid keypad platform data\n");
385 return PTR_ERR(plat);
386 }
387
388 irq = platform_get_irq(pdev, 0);
389 if (irq < 0)
390 return irq;
391
392 keypad = devm_kzalloc(&pdev->dev, sizeof(struct tc_keypad),
393 GFP_KERNEL);
394 if (!keypad)
395 return -ENOMEM;
396
397 input = devm_input_allocate_device(&pdev->dev);
398 if (!input) {
399 dev_err(&pdev->dev, "failed to allocate input device\n");
400 return -ENOMEM;
401 }
402
403 keypad->board = plat;
404 keypad->input = input;
405 keypad->tc3589x = tc3589x;
406
407 input->id.bustype = BUS_I2C;
408 input->name = pdev->name;
409 input->dev.parent = &pdev->dev;
410
411 input->open = tc3589x_keypad_open;
412 input->close = tc3589x_keypad_close;
413
414 error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
415 TC3589x_MAX_KPROW, TC3589x_MAX_KPCOL,
416 NULL, input);
417 if (error) {
418 dev_err(&pdev->dev, "Failed to build keymap\n");
419 return error;
420 }
421
422 keypad->keymap = input->keycode;
423
424 input_set_capability(input, EV_MSC, MSC_SCAN);
425 if (!plat->no_autorepeat)
426 __set_bit(EV_REP, input->evbit);
427
428 input_set_drvdata(input, keypad);
429
430 tc3589x_keypad_disable(keypad);
431
432 error = devm_request_threaded_irq(&pdev->dev, irq,
433 NULL, tc3589x_keypad_irq,
434 plat->irqtype | IRQF_ONESHOT,
435 "tc3589x-keypad", keypad);
436 if (error) {
437 dev_err(&pdev->dev,
438 "Could not allocate irq %d,error %d\n",
439 irq, error);
440 return error;
441 }
442
443 error = input_register_device(input);
444 if (error) {
445 dev_err(&pdev->dev, "Could not register input device\n");
446 return error;
447 }
448
449 /* let platform decide if keypad is a wakeup source or not */
450 device_init_wakeup(&pdev->dev, plat->enable_wakeup);
451 device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
452
453 platform_set_drvdata(pdev, keypad);
454
455 return 0;
456}
457
458static int tc3589x_keypad_suspend(struct device *dev)
459{
460 struct platform_device *pdev = to_platform_device(dev);
461 struct tc_keypad *keypad = platform_get_drvdata(pdev);
462 int irq = platform_get_irq(pdev, 0);
463
464 /* keypad is already off; we do nothing */
465 if (keypad->keypad_stopped)
466 return 0;
467
468 /* if device is not a wakeup source, disable it for powersave */
469 if (!device_may_wakeup(&pdev->dev))
470 tc3589x_keypad_disable(keypad);
471 else
472 enable_irq_wake(irq);
473
474 return 0;
475}
476
477static int tc3589x_keypad_resume(struct device *dev)
478{
479 struct platform_device *pdev = to_platform_device(dev);
480 struct tc_keypad *keypad = platform_get_drvdata(pdev);
481 int irq = platform_get_irq(pdev, 0);
482
483 if (!keypad->keypad_stopped)
484 return 0;
485
486 /* enable the device to resume normal operations */
487 if (!device_may_wakeup(&pdev->dev))
488 tc3589x_keypad_enable(keypad);
489 else
490 disable_irq_wake(irq);
491
492 return 0;
493}
494
495static DEFINE_SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
496 tc3589x_keypad_suspend, tc3589x_keypad_resume);
497
498static struct platform_driver tc3589x_keypad_driver = {
499 .driver = {
500 .name = "tc3589x-keypad",
501 .pm = pm_sleep_ptr(&tc3589x_keypad_dev_pm_ops),
502 },
503 .probe = tc3589x_keypad_probe,
504};
505module_platform_driver(tc3589x_keypad_driver);
506
507MODULE_LICENSE("GPL v2");
508MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
509MODULE_DESCRIPTION("TC35893 Keypad Driver");
510MODULE_ALIAS("platform:tc3589x-keypad");