1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
4  */
5 
6 #include <linux/input/matrix_keypad.h>
7 #include <linux/platform_device.h>
8 #include <linux/interrupt.h>
9 #include <linux/io.h>
10 #include <linux/delay.h>
11 #include <linux/input.h>
12 #include <linux/slab.h>
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 
17 #define KEYPAD_SCAN_MODE	0x00
18 #define KEYPAD_CNTL		0x04
19 #define KEYPAD_INT		0x08
20 #define KEYPAD_INTMSK		0x0C
21 
22 #define KEYPAD_DATA		0x10
23 #define KEYPAD_GPIO		0x30
24 
25 #define KEYPAD_UNKNOWN_INT	0x40
26 #define KEYPAD_UNKNOWN_INT_STS	0x44
27 
28 #define KEYPAD_BITMASK_COLS	11
29 #define KEYPAD_BITMASK_ROWS	8
30 
31 struct nspire_keypad {
32 	void __iomem *reg_base;
33 	u32 int_mask;
34 
35 	struct input_dev *input;
36 	struct clk *clk;
37 
38 	struct matrix_keymap_data *keymap;
39 	int row_shift;
40 
41 	/* Maximum delay estimated assuming 33MHz APB */
42 	u32 scan_interval;	/* In microseconds (~2000us max) */
43 	u32 row_delay;		/* In microseconds (~500us max) */
44 
45 	u16 state[KEYPAD_BITMASK_ROWS];
46 
47 	bool active_low;
48 };
49 
nspire_keypad_irq(int irq,void * dev_id)50 static irqreturn_t nspire_keypad_irq(int irq, void *dev_id)
51 {
52 	struct nspire_keypad *keypad = dev_id;
53 	struct input_dev *input = keypad->input;
54 	unsigned short *keymap = input->keycode;
55 	unsigned int code;
56 	int row, col;
57 	u32 int_sts;
58 	u16 state[8];
59 	u16 bits, changed;
60 
61 	int_sts = readl(keypad->reg_base + KEYPAD_INT) & keypad->int_mask;
62 	if (!int_sts)
63 		return IRQ_NONE;
64 
65 	memcpy_fromio(state, keypad->reg_base + KEYPAD_DATA, sizeof(state));
66 
67 	for (row = 0; row < KEYPAD_BITMASK_ROWS; row++) {
68 		bits = state[row];
69 		if (keypad->active_low)
70 			bits = ~bits;
71 
72 		changed = bits ^ keypad->state[row];
73 		if (!changed)
74 			continue;
75 
76 		keypad->state[row] = bits;
77 
78 		for (col = 0; col < KEYPAD_BITMASK_COLS; col++) {
79 			if (!(changed & (1U << col)))
80 				continue;
81 
82 			code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
83 			input_event(input, EV_MSC, MSC_SCAN, code);
84 			input_report_key(input, keymap[code],
85 					 bits & (1U << col));
86 		}
87 	}
88 
89 	input_sync(input);
90 
91 	writel(0x3, keypad->reg_base + KEYPAD_INT);
92 
93 	return IRQ_HANDLED;
94 }
95 
nspire_keypad_open(struct input_dev * input)96 static int nspire_keypad_open(struct input_dev *input)
97 {
98 	struct nspire_keypad *keypad = input_get_drvdata(input);
99 	unsigned long val = 0, cycles_per_us, delay_cycles, row_delay_cycles;
100 	int error;
101 
102 	error = clk_prepare_enable(keypad->clk);
103 	if (error)
104 		return error;
105 
106 	cycles_per_us = (clk_get_rate(keypad->clk) / 1000000);
107 	if (cycles_per_us == 0)
108 		cycles_per_us = 1;
109 
110 	delay_cycles = cycles_per_us * keypad->scan_interval;
111 	WARN_ON(delay_cycles >= (1 << 16)); /* Overflow */
112 	delay_cycles &= 0xffff;
113 
114 	row_delay_cycles = cycles_per_us * keypad->row_delay;
115 	WARN_ON(row_delay_cycles >= (1 << 14)); /* Overflow */
116 	row_delay_cycles &= 0x3fff;
117 
118 	val |= 3 << 0; /* Set scan mode to 3 (continuous scan) */
119 	val |= row_delay_cycles << 2; /* Delay between scanning each row */
120 	val |= delay_cycles << 16; /* Delay between scans */
121 	writel(val, keypad->reg_base + KEYPAD_SCAN_MODE);
122 
123 	val = (KEYPAD_BITMASK_ROWS & 0xff) | (KEYPAD_BITMASK_COLS & 0xff)<<8;
124 	writel(val, keypad->reg_base + KEYPAD_CNTL);
125 
126 	/* Enable interrupts */
127 	keypad->int_mask = 1 << 1;
128 	writel(keypad->int_mask, keypad->reg_base + KEYPAD_INTMSK);
129 
130 	return 0;
131 }
132 
nspire_keypad_close(struct input_dev * input)133 static void nspire_keypad_close(struct input_dev *input)
134 {
135 	struct nspire_keypad *keypad = input_get_drvdata(input);
136 
137 	/* Disable interrupts */
138 	writel(0, keypad->reg_base + KEYPAD_INTMSK);
139 	/* Acknowledge existing interrupts */
140 	writel(~0, keypad->reg_base + KEYPAD_INT);
141 
142 	clk_disable_unprepare(keypad->clk);
143 }
144 
nspire_keypad_probe(struct platform_device * pdev)145 static int nspire_keypad_probe(struct platform_device *pdev)
146 {
147 	const struct device_node *of_node = pdev->dev.of_node;
148 	struct nspire_keypad *keypad;
149 	struct input_dev *input;
150 	struct resource *res;
151 	int irq;
152 	int error;
153 
154 	irq = platform_get_irq(pdev, 0);
155 	if (irq < 0)
156 		return -EINVAL;
157 
158 	keypad = devm_kzalloc(&pdev->dev, sizeof(struct nspire_keypad),
159 			      GFP_KERNEL);
160 	if (!keypad) {
161 		dev_err(&pdev->dev, "failed to allocate keypad memory\n");
162 		return -ENOMEM;
163 	}
164 
165 	keypad->row_shift = get_count_order(KEYPAD_BITMASK_COLS);
166 
167 	error = of_property_read_u32(of_node, "scan-interval",
168 				     &keypad->scan_interval);
169 	if (error) {
170 		dev_err(&pdev->dev, "failed to get scan-interval\n");
171 		return error;
172 	}
173 
174 	error = of_property_read_u32(of_node, "row-delay",
175 				     &keypad->row_delay);
176 	if (error) {
177 		dev_err(&pdev->dev, "failed to get row-delay\n");
178 		return error;
179 	}
180 
181 	keypad->active_low = of_property_read_bool(of_node, "active-low");
182 
183 	keypad->clk = devm_clk_get(&pdev->dev, NULL);
184 	if (IS_ERR(keypad->clk)) {
185 		dev_err(&pdev->dev, "unable to get clock\n");
186 		return PTR_ERR(keypad->clk);
187 	}
188 
189 	keypad->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
190 	if (IS_ERR(keypad->reg_base))
191 		return PTR_ERR(keypad->reg_base);
192 
193 	keypad->input = input = devm_input_allocate_device(&pdev->dev);
194 	if (!input) {
195 		dev_err(&pdev->dev, "failed to allocate input device\n");
196 		return -ENOMEM;
197 	}
198 
199 	error = clk_prepare_enable(keypad->clk);
200 	if (error) {
201 		dev_err(&pdev->dev, "failed to enable clock\n");
202 		return error;
203 	}
204 
205 	/* Disable interrupts */
206 	writel(0, keypad->reg_base + KEYPAD_INTMSK);
207 	/* Acknowledge existing interrupts */
208 	writel(~0, keypad->reg_base + KEYPAD_INT);
209 
210 	/* Disable GPIO interrupts to prevent hanging on touchpad */
211 	/* Possibly used to detect touchpad events */
212 	writel(0, keypad->reg_base + KEYPAD_UNKNOWN_INT);
213 	/* Acknowledge existing GPIO interrupts */
214 	writel(~0, keypad->reg_base + KEYPAD_UNKNOWN_INT_STS);
215 
216 	clk_disable_unprepare(keypad->clk);
217 
218 	input_set_drvdata(input, keypad);
219 
220 	input->id.bustype = BUS_HOST;
221 	input->name = "nspire-keypad";
222 	input->open = nspire_keypad_open;
223 	input->close = nspire_keypad_close;
224 
225 	__set_bit(EV_KEY, input->evbit);
226 	__set_bit(EV_REP, input->evbit);
227 	input_set_capability(input, EV_MSC, MSC_SCAN);
228 
229 	error = matrix_keypad_build_keymap(NULL, NULL,
230 					   KEYPAD_BITMASK_ROWS,
231 					   KEYPAD_BITMASK_COLS,
232 					   NULL, input);
233 	if (error) {
234 		dev_err(&pdev->dev, "building keymap failed\n");
235 		return error;
236 	}
237 
238 	error = devm_request_irq(&pdev->dev, irq, nspire_keypad_irq, 0,
239 				 "nspire_keypad", keypad);
240 	if (error) {
241 		dev_err(&pdev->dev, "allocate irq %d failed\n", irq);
242 		return error;
243 	}
244 
245 	error = input_register_device(input);
246 	if (error) {
247 		dev_err(&pdev->dev,
248 			"unable to register input device: %d\n", error);
249 		return error;
250 	}
251 
252 	dev_dbg(&pdev->dev,
253 		"TI-NSPIRE keypad at %pR (scan_interval=%uus, row_delay=%uus%s)\n",
254 		res, keypad->row_delay, keypad->scan_interval,
255 		keypad->active_low ? ", active_low" : "");
256 
257 	return 0;
258 }
259 
260 static const struct of_device_id nspire_keypad_dt_match[] = {
261 	{ .compatible = "ti,nspire-keypad" },
262 	{ },
263 };
264 MODULE_DEVICE_TABLE(of, nspire_keypad_dt_match);
265 
266 static struct platform_driver nspire_keypad_driver = {
267 	.driver = {
268 		.name = "nspire-keypad",
269 		.of_match_table = nspire_keypad_dt_match,
270 	},
271 	.probe = nspire_keypad_probe,
272 };
273 
274 module_platform_driver(nspire_keypad_driver);
275 
276 MODULE_LICENSE("GPL");
277 MODULE_DESCRIPTION("TI-NSPIRE Keypad Driver");
278