2 * Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2, as
6 * published by the Free Software Foundation.
9 #include <linux/input/matrix_keypad.h>
10 #include <linux/platform_device.h>
11 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/input.h>
15 #include <linux/slab.h>
16 #include <linux/clk.h>
17 #include <linux/module.h>
20 #define KEYPAD_SCAN_MODE 0x00
21 #define KEYPAD_CNTL 0x04
22 #define KEYPAD_INT 0x08
23 #define KEYPAD_INTMSK 0x0C
25 #define KEYPAD_DATA 0x10
26 #define KEYPAD_GPIO 0x30
28 #define KEYPAD_UNKNOWN_INT 0x40
29 #define KEYPAD_UNKNOWN_INT_STS 0x44
31 #define KEYPAD_BITMASK_COLS 11
32 #define KEYPAD_BITMASK_ROWS 8
34 struct nspire_keypad {
35 void __iomem *reg_base;
38 struct input_dev *input;
41 struct matrix_keymap_data *keymap;
44 /* Maximum delay estimated assuming 33MHz APB */
45 u32 scan_interval; /* In microseconds (~2000us max) */
46 u32 row_delay; /* In microseconds (~500us max) */
48 u16 state[KEYPAD_BITMASK_ROWS];
53 static irqreturn_t nspire_keypad_irq(int irq, void *dev_id)
55 struct nspire_keypad *keypad = dev_id;
56 struct input_dev *input = keypad->input;
57 unsigned short *keymap = input->keycode;
64 int_sts = readl(keypad->reg_base + KEYPAD_INT) & keypad->int_mask;
68 memcpy_fromio(state, keypad->reg_base + KEYPAD_DATA, sizeof(state));
70 for (row = 0; row < KEYPAD_BITMASK_ROWS; row++) {
72 if (keypad->active_low)
75 changed = bits ^ keypad->state[row];
79 keypad->state[row] = bits;
81 for (col = 0; col < KEYPAD_BITMASK_COLS; col++) {
82 if (!(changed & (1U << col)))
85 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
86 input_event(input, EV_MSC, MSC_SCAN, code);
87 input_report_key(input, keymap[code],
94 writel(0x3, keypad->reg_base + KEYPAD_INT);
99 static int nspire_keypad_open(struct input_dev *input)
101 struct nspire_keypad *keypad = input_get_drvdata(input);
102 unsigned long val = 0, cycles_per_us, delay_cycles, row_delay_cycles;
105 error = clk_prepare_enable(keypad->clk);
109 cycles_per_us = (clk_get_rate(keypad->clk) / 1000000);
110 if (cycles_per_us == 0)
113 delay_cycles = cycles_per_us * keypad->scan_interval;
114 WARN_ON(delay_cycles >= (1 << 16)); /* Overflow */
115 delay_cycles &= 0xffff;
117 row_delay_cycles = cycles_per_us * keypad->row_delay;
118 WARN_ON(row_delay_cycles >= (1 << 14)); /* Overflow */
119 row_delay_cycles &= 0x3fff;
121 val |= 3 << 0; /* Set scan mode to 3 (continuous scan) */
122 val |= row_delay_cycles << 2; /* Delay between scanning each row */
123 val |= delay_cycles << 16; /* Delay between scans */
124 writel(val, keypad->reg_base + KEYPAD_SCAN_MODE);
126 val = (KEYPAD_BITMASK_ROWS & 0xff) | (KEYPAD_BITMASK_COLS & 0xff)<<8;
127 writel(val, keypad->reg_base + KEYPAD_CNTL);
129 /* Enable interrupts */
130 keypad->int_mask = 1 << 1;
131 writel(keypad->int_mask, keypad->reg_base + KEYPAD_INTMSK);
136 static void nspire_keypad_close(struct input_dev *input)
138 struct nspire_keypad *keypad = input_get_drvdata(input);
140 /* Disable interrupts */
141 writel(0, keypad->reg_base + KEYPAD_INTMSK);
142 /* Acknowledge existing interrupts */
143 writel(~0, keypad->reg_base + KEYPAD_INT);
145 clk_disable_unprepare(keypad->clk);
148 static int nspire_keypad_probe(struct platform_device *pdev)
150 const struct device_node *of_node = pdev->dev.of_node;
151 struct nspire_keypad *keypad;
152 struct input_dev *input;
153 struct resource *res;
157 irq = platform_get_irq(pdev, 0);
159 dev_err(&pdev->dev, "failed to get keypad irq\n");
163 keypad = devm_kzalloc(&pdev->dev, sizeof(struct nspire_keypad),
166 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
170 keypad->row_shift = get_count_order(KEYPAD_BITMASK_COLS);
172 error = of_property_read_u32(of_node, "scan-interval",
173 &keypad->scan_interval);
175 dev_err(&pdev->dev, "failed to get scan-interval\n");
179 error = of_property_read_u32(of_node, "row-delay",
182 dev_err(&pdev->dev, "failed to get row-delay\n");
186 keypad->active_low = of_property_read_bool(of_node, "active-low");
188 keypad->clk = devm_clk_get(&pdev->dev, NULL);
189 if (IS_ERR(keypad->clk)) {
190 dev_err(&pdev->dev, "unable to get clock\n");
191 return PTR_ERR(keypad->clk);
194 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
195 keypad->reg_base = devm_ioremap_resource(&pdev->dev, res);
196 if (IS_ERR(keypad->reg_base))
197 return PTR_ERR(keypad->reg_base);
199 keypad->input = input = devm_input_allocate_device(&pdev->dev);
201 dev_err(&pdev->dev, "failed to allocate input device\n");
205 error = clk_prepare_enable(keypad->clk);
207 dev_err(&pdev->dev, "failed to enable clock\n");
211 /* Disable interrupts */
212 writel(0, keypad->reg_base + KEYPAD_INTMSK);
213 /* Acknowledge existing interrupts */
214 writel(~0, keypad->reg_base + KEYPAD_INT);
216 /* Disable GPIO interrupts to prevent hanging on touchpad */
217 /* Possibly used to detect touchpad events */
218 writel(0, keypad->reg_base + KEYPAD_UNKNOWN_INT);
219 /* Acknowledge existing GPIO interrupts */
220 writel(~0, keypad->reg_base + KEYPAD_UNKNOWN_INT_STS);
222 clk_disable_unprepare(keypad->clk);
224 input_set_drvdata(input, keypad);
226 input->id.bustype = BUS_HOST;
227 input->name = "nspire-keypad";
228 input->open = nspire_keypad_open;
229 input->close = nspire_keypad_close;
231 __set_bit(EV_KEY, input->evbit);
232 __set_bit(EV_REP, input->evbit);
233 input_set_capability(input, EV_MSC, MSC_SCAN);
235 error = matrix_keypad_build_keymap(NULL, NULL,
240 dev_err(&pdev->dev, "building keymap failed\n");
244 error = devm_request_irq(&pdev->dev, irq, nspire_keypad_irq, 0,
245 "nspire_keypad", keypad);
247 dev_err(&pdev->dev, "allocate irq %d failed\n", irq);
251 error = input_register_device(input);
254 "unable to register input device: %d\n", error);
259 "TI-NSPIRE keypad at %pR (scan_interval=%uus, row_delay=%uus%s)\n",
260 res, keypad->row_delay, keypad->scan_interval,
261 keypad->active_low ? ", active_low" : "");
266 static const struct of_device_id nspire_keypad_dt_match[] = {
267 { .compatible = "ti,nspire-keypad" },
270 MODULE_DEVICE_TABLE(of, nspire_keypad_dt_match);
272 static struct platform_driver nspire_keypad_driver = {
274 .name = "nspire-keypad",
275 .of_match_table = nspire_keypad_dt_match,
277 .probe = nspire_keypad_probe,
280 module_platform_driver(nspire_keypad_driver);
282 MODULE_LICENSE("GPL");
283 MODULE_DESCRIPTION("TI-NSPIRE Keypad Driver");