1 /*
2  * Copyright (C) 2014 Broadcom Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9  * kind, whether express or implied; without even the implied warranty
10  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/bitops.h>
15 #include <linux/clk.h>
16 #include <linux/gfp.h>
17 #include <linux/io.h>
18 #include <linux/input.h>
19 #include <linux/input/matrix_keypad.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/stddef.h>
25 #include <linux/types.h>
26 
27 #define DEFAULT_CLK_HZ			31250
28 #define MAX_ROWS			8
29 #define MAX_COLS			8
30 
31 /* Register/field definitions */
32 #define KPCR_OFFSET			0x00000080
33 #define KPCR_MODE			0x00000002
34 #define KPCR_MODE_SHIFT			1
35 #define KPCR_MODE_MASK			1
36 #define KPCR_ENABLE			0x00000001
37 #define KPCR_STATUSFILTERENABLE		0x00008000
38 #define KPCR_STATUSFILTERTYPE_SHIFT	12
39 #define KPCR_COLFILTERENABLE		0x00000800
40 #define KPCR_COLFILTERTYPE_SHIFT	8
41 #define KPCR_ROWWIDTH_SHIFT		20
42 #define KPCR_COLUMNWIDTH_SHIFT		16
43 
44 #define KPIOR_OFFSET			0x00000084
45 #define KPIOR_ROWOCONTRL_SHIFT		24
46 #define KPIOR_ROWOCONTRL_MASK		0xFF000000
47 #define KPIOR_COLUMNOCONTRL_SHIFT	16
48 #define KPIOR_COLUMNOCONTRL_MASK	0x00FF0000
49 #define KPIOR_COLUMN_IO_DATA_SHIFT	0
50 
51 #define KPEMR0_OFFSET			0x00000090
52 #define KPEMR1_OFFSET			0x00000094
53 #define KPEMR2_OFFSET			0x00000098
54 #define KPEMR3_OFFSET			0x0000009C
55 #define KPEMR_EDGETYPE_BOTH		3
56 
57 #define KPSSR0_OFFSET			0x000000A0
58 #define KPSSR1_OFFSET			0x000000A4
59 #define KPSSRN_OFFSET(reg_n)		(KPSSR0_OFFSET + 4 * (reg_n))
60 #define KPIMR0_OFFSET			0x000000B0
61 #define KPIMR1_OFFSET			0x000000B4
62 #define KPICR0_OFFSET			0x000000B8
63 #define KPICR1_OFFSET			0x000000BC
64 #define KPICRN_OFFSET(reg_n)		(KPICR0_OFFSET + 4 * (reg_n))
65 #define KPISR0_OFFSET			0x000000C0
66 #define KPISR1_OFFSET			0x000000C4
67 
68 #define KPCR_STATUSFILTERTYPE_MAX	7
69 #define KPCR_COLFILTERTYPE_MAX		7
70 
71 /* Macros to determine the row/column from a bit that is set in SSR0/1. */
72 #define BIT_TO_ROW_SSRN(bit_nr, reg_n)	(((bit_nr) >> 3) + 4 * (reg_n))
73 #define BIT_TO_COL(bit_nr)		((bit_nr) % 8)
74 
75 /* Structure representing various run-time entities */
76 struct bcm_kp {
77 	void __iomem *base;
78 	int irq;
79 	struct clk *clk;
80 	struct input_dev *input_dev;
81 	unsigned long last_state[2];
82 	unsigned int n_rows;
83 	unsigned int n_cols;
84 	u32 kpcr;
85 	u32 kpior;
86 	u32 kpemr;
87 	u32 imr0_val;
88 	u32 imr1_val;
89 };
90 
91 /*
92  * Returns the keycode from the input device keymap given the row and
93  * column.
94  */
95 static int bcm_kp_get_keycode(struct bcm_kp *kp, int row, int col)
96 {
97 	unsigned int row_shift = get_count_order(kp->n_cols);
98 	unsigned short *keymap = kp->input_dev->keycode;
99 
100 	return keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
101 }
102 
103 static void bcm_kp_report_keys(struct bcm_kp *kp, int reg_num, int pull_mode)
104 {
105 	unsigned long state, change;
106 	int bit_nr;
107 	int key_press;
108 	int row, col;
109 	unsigned int keycode;
110 
111 	/* Clear interrupts */
112 	writel(0xFFFFFFFF, kp->base + KPICRN_OFFSET(reg_num));
113 
114 	state = readl(kp->base + KPSSRN_OFFSET(reg_num));
115 	change = kp->last_state[reg_num] ^ state;
116 	kp->last_state[reg_num] = state;
117 
118 	for_each_set_bit(bit_nr, &change, BITS_PER_LONG) {
119 		key_press = state & BIT(bit_nr);
120 		/* The meaning of SSR register depends on pull mode. */
121 		key_press = pull_mode ? !key_press : key_press;
122 		row = BIT_TO_ROW_SSRN(bit_nr, reg_num);
123 		col = BIT_TO_COL(bit_nr);
124 		keycode = bcm_kp_get_keycode(kp, row, col);
125 		input_report_key(kp->input_dev, keycode, key_press);
126 	}
127 }
128 
129 static irqreturn_t bcm_kp_isr_thread(int irq, void *dev_id)
130 {
131 	struct bcm_kp *kp = dev_id;
132 	int pull_mode = (kp->kpcr >> KPCR_MODE_SHIFT) & KPCR_MODE_MASK;
133 	int reg_num;
134 
135 	for (reg_num = 0; reg_num <= 1; reg_num++)
136 		bcm_kp_report_keys(kp, reg_num, pull_mode);
137 
138 	input_sync(kp->input_dev);
139 
140 	return IRQ_HANDLED;
141 }
142 
143 static int bcm_kp_start(struct bcm_kp *kp)
144 {
145 	int error;
146 
147 	if (kp->clk) {
148 		error = clk_prepare_enable(kp->clk);
149 		if (error)
150 			return error;
151 	}
152 
153 	writel(kp->kpior, kp->base + KPIOR_OFFSET);
154 
155 	writel(kp->imr0_val, kp->base + KPIMR0_OFFSET);
156 	writel(kp->imr1_val, kp->base + KPIMR1_OFFSET);
157 
158 	writel(kp->kpemr, kp->base + KPEMR0_OFFSET);
159 	writel(kp->kpemr, kp->base + KPEMR1_OFFSET);
160 	writel(kp->kpemr, kp->base + KPEMR2_OFFSET);
161 	writel(kp->kpemr, kp->base + KPEMR3_OFFSET);
162 
163 	writel(0xFFFFFFFF, kp->base + KPICR0_OFFSET);
164 	writel(0xFFFFFFFF, kp->base + KPICR1_OFFSET);
165 
166 	kp->last_state[0] = readl(kp->base + KPSSR0_OFFSET);
167 	kp->last_state[0] = readl(kp->base + KPSSR1_OFFSET);
168 
169 	writel(kp->kpcr | KPCR_ENABLE, kp->base + KPCR_OFFSET);
170 
171 	return 0;
172 }
173 
174 static void bcm_kp_stop(const struct bcm_kp *kp)
175 {
176 	u32 val;
177 
178 	val = readl(kp->base + KPCR_OFFSET);
179 	val &= ~KPCR_ENABLE;
180 	writel(0, kp->base + KPCR_OFFSET);
181 	writel(0, kp->base + KPIMR0_OFFSET);
182 	writel(0, kp->base + KPIMR1_OFFSET);
183 	writel(0xFFFFFFFF, kp->base + KPICR0_OFFSET);
184 	writel(0xFFFFFFFF, kp->base + KPICR1_OFFSET);
185 
186 	clk_disable_unprepare(kp->clk);
187 }
188 
189 static int bcm_kp_open(struct input_dev *dev)
190 {
191 	struct bcm_kp *kp = input_get_drvdata(dev);
192 
193 	return bcm_kp_start(kp);
194 }
195 
196 static void bcm_kp_close(struct input_dev *dev)
197 {
198 	struct bcm_kp *kp = input_get_drvdata(dev);
199 
200 	bcm_kp_stop(kp);
201 }
202 
203 static int bcm_kp_matrix_key_parse_dt(struct bcm_kp *kp)
204 {
205 	struct device *dev = kp->input_dev->dev.parent;
206 	struct device_node *np = dev->of_node;
207 	int error;
208 	unsigned int dt_val;
209 	unsigned int i;
210 	unsigned int num_rows, col_mask, rows_set;
211 
212 	/* Initialize the KPCR Keypad Configuration Register */
213 	kp->kpcr = KPCR_STATUSFILTERENABLE | KPCR_COLFILTERENABLE;
214 
215 	error = matrix_keypad_parse_properties(dev, &kp->n_rows, &kp->n_cols);
216 	if (error) {
217 		dev_err(dev, "failed to parse kp params\n");
218 		return error;
219 	}
220 
221 	/* Set row width for the ASIC block. */
222 	kp->kpcr |= (kp->n_rows - 1) << KPCR_ROWWIDTH_SHIFT;
223 
224 	/* Set column width for the ASIC block. */
225 	kp->kpcr |= (kp->n_cols - 1) << KPCR_COLUMNWIDTH_SHIFT;
226 
227 	/* Configure the IMR registers */
228 
229 	/*
230 	 * IMR registers contain interrupt enable bits for 8x8 matrix
231 	 * IMR0 register format: <row3> <row2> <row1> <row0>
232 	 * IMR1 register format: <row7> <row6> <row5> <row4>
233 	 */
234 	col_mask = (1 << (kp->n_cols)) - 1;
235 	num_rows = kp->n_rows;
236 
237 	/* Set column bits in rows 0 to 3 in IMR0 */
238 	kp->imr0_val = col_mask;
239 
240 	rows_set = 1;
241 	while (--num_rows && rows_set++ < 4)
242 		kp->imr0_val |= kp->imr0_val << MAX_COLS;
243 
244 	/* Set column bits in rows 4 to 7 in IMR1 */
245 	kp->imr1_val = 0;
246 	if (num_rows) {
247 		kp->imr1_val = col_mask;
248 		while (--num_rows)
249 			kp->imr1_val |= kp->imr1_val << MAX_COLS;
250 	}
251 
252 	/* Initialize the KPEMR Keypress Edge Mode Registers */
253 	/* Trigger on both edges */
254 	kp->kpemr = 0;
255 	for (i = 0; i <= 30; i += 2)
256 		kp->kpemr |= (KPEMR_EDGETYPE_BOTH << i);
257 
258 	/*
259 	 * Obtain the Status filter debounce value and verify against the
260 	 * possible values specified in the DT binding.
261 	 */
262 	of_property_read_u32(np, "status-debounce-filter-period", &dt_val);
263 
264 	if (dt_val > KPCR_STATUSFILTERTYPE_MAX) {
265 		dev_err(dev, "Invalid Status filter debounce value %d\n",
266 			dt_val);
267 		return -EINVAL;
268 	}
269 
270 	kp->kpcr |= dt_val << KPCR_STATUSFILTERTYPE_SHIFT;
271 
272 	/*
273 	 * Obtain the Column filter debounce value and verify against the
274 	 * possible values specified in the DT binding.
275 	 */
276 	of_property_read_u32(np, "col-debounce-filter-period", &dt_val);
277 
278 	if (dt_val > KPCR_COLFILTERTYPE_MAX) {
279 		dev_err(dev, "Invalid Column filter debounce value %d\n",
280 			dt_val);
281 		return -EINVAL;
282 	}
283 
284 	kp->kpcr |= dt_val << KPCR_COLFILTERTYPE_SHIFT;
285 
286 	/*
287 	 * Determine between the row and column,
288 	 * which should be configured as output.
289 	 */
290 	if (of_property_read_bool(np, "row-output-enabled")) {
291 		/*
292 		* Set RowOContrl or ColumnOContrl in KPIOR
293 		* to the number of pins to drive as outputs
294 		*/
295 		kp->kpior = ((1 << kp->n_rows) - 1) <<
296 				KPIOR_ROWOCONTRL_SHIFT;
297 	} else {
298 		kp->kpior = ((1 << kp->n_cols) - 1) <<
299 				KPIOR_COLUMNOCONTRL_SHIFT;
300 	}
301 
302 	/*
303 	 * Determine if the scan pull up needs to be enabled
304 	 */
305 	if (of_property_read_bool(np, "pull-up-enabled"))
306 		kp->kpcr |= KPCR_MODE;
307 
308 	dev_dbg(dev, "n_rows=%d n_col=%d kpcr=%x kpior=%x kpemr=%x\n",
309 		kp->n_rows, kp->n_cols,
310 		kp->kpcr, kp->kpior, kp->kpemr);
311 
312 	return 0;
313 }
314 
315 
316 static int bcm_kp_probe(struct platform_device *pdev)
317 {
318 	struct bcm_kp *kp;
319 	struct input_dev *input_dev;
320 	struct resource *res;
321 	int error;
322 
323 	kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
324 	if (!kp)
325 		return -ENOMEM;
326 
327 	input_dev = devm_input_allocate_device(&pdev->dev);
328 	if (!input_dev) {
329 		dev_err(&pdev->dev, "failed to allocate the input device\n");
330 		return -ENOMEM;
331 	}
332 
333 	__set_bit(EV_KEY, input_dev->evbit);
334 
335 	/* Enable auto repeat feature of Linux input subsystem */
336 	if (of_property_read_bool(pdev->dev.of_node, "autorepeat"))
337 		__set_bit(EV_REP, input_dev->evbit);
338 
339 	input_dev->name = pdev->name;
340 	input_dev->phys = "keypad/input0";
341 	input_dev->dev.parent = &pdev->dev;
342 	input_dev->open = bcm_kp_open;
343 	input_dev->close = bcm_kp_close;
344 
345 	input_dev->id.bustype = BUS_HOST;
346 	input_dev->id.vendor = 0x0001;
347 	input_dev->id.product = 0x0001;
348 	input_dev->id.version = 0x0100;
349 
350 	input_set_drvdata(input_dev, kp);
351 
352 	kp->input_dev = input_dev;
353 
354 	error = bcm_kp_matrix_key_parse_dt(kp);
355 	if (error)
356 		return error;
357 
358 	error = matrix_keypad_build_keymap(NULL, NULL,
359 					   kp->n_rows, kp->n_cols,
360 					   NULL, input_dev);
361 	if (error) {
362 		dev_err(&pdev->dev, "failed to build keymap\n");
363 		return error;
364 	}
365 
366 	/* Get the KEYPAD base address */
367 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
368 	if (!res) {
369 		dev_err(&pdev->dev, "Missing keypad base address resource\n");
370 		return -ENODEV;
371 	}
372 
373 	kp->base = devm_ioremap_resource(&pdev->dev, res);
374 	if (IS_ERR(kp->base))
375 		return PTR_ERR(kp->base);
376 
377 	/* Enable clock */
378 	kp->clk = devm_clk_get(&pdev->dev, "peri_clk");
379 	if (IS_ERR(kp->clk)) {
380 		error = PTR_ERR(kp->clk);
381 		if (error != -ENOENT) {
382 			if (error != -EPROBE_DEFER)
383 				dev_err(&pdev->dev, "Failed to get clock\n");
384 			return error;
385 		}
386 		dev_dbg(&pdev->dev,
387 			"No clock specified. Assuming it's enabled\n");
388 		kp->clk = NULL;
389 	} else {
390 		unsigned int desired_rate;
391 		long actual_rate;
392 
393 		error = of_property_read_u32(pdev->dev.of_node,
394 					     "clock-frequency", &desired_rate);
395 		if (error < 0)
396 			desired_rate = DEFAULT_CLK_HZ;
397 
398 		actual_rate = clk_round_rate(kp->clk, desired_rate);
399 		if (actual_rate <= 0)
400 			return -EINVAL;
401 
402 		error = clk_set_rate(kp->clk, actual_rate);
403 		if (error)
404 			return error;
405 
406 		error = clk_prepare_enable(kp->clk);
407 		if (error)
408 			return error;
409 	}
410 
411 	/* Put the kp into a known sane state */
412 	bcm_kp_stop(kp);
413 
414 	kp->irq = platform_get_irq(pdev, 0);
415 	if (kp->irq < 0)
416 		return -EINVAL;
417 
418 	error = devm_request_threaded_irq(&pdev->dev, kp->irq,
419 					  NULL, bcm_kp_isr_thread,
420 					  IRQF_ONESHOT, pdev->name, kp);
421 	if (error) {
422 		dev_err(&pdev->dev, "failed to request IRQ\n");
423 		return error;
424 	}
425 
426 	error = input_register_device(input_dev);
427 	if (error) {
428 		dev_err(&pdev->dev, "failed to register input device\n");
429 		return error;
430 	}
431 
432 	return 0;
433 }
434 
435 static const struct of_device_id bcm_kp_of_match[] = {
436 	{ .compatible = "brcm,bcm-keypad" },
437 	{ },
438 };
439 MODULE_DEVICE_TABLE(of, bcm_kp_of_match);
440 
441 static struct platform_driver bcm_kp_device_driver = {
442 	.probe		= bcm_kp_probe,
443 	.driver		= {
444 		.name	= "bcm-keypad",
445 		.of_match_table = of_match_ptr(bcm_kp_of_match),
446 	}
447 };
448 
449 module_platform_driver(bcm_kp_device_driver);
450 
451 MODULE_AUTHOR("Broadcom Corporation");
452 MODULE_DESCRIPTION("BCM Keypad Driver");
453 MODULE_LICENSE("GPL v2");
454