xref: /openbmc/linux/drivers/input/keyboard/clps711x-keypad.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e70f18e1SAlexander Shiyan /*
3e70f18e1SAlexander Shiyan  * Cirrus Logic CLPS711X Keypad driver
4e70f18e1SAlexander Shiyan  *
5e70f18e1SAlexander Shiyan  * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
6e70f18e1SAlexander Shiyan  */
7e70f18e1SAlexander Shiyan 
8e70f18e1SAlexander Shiyan #include <linux/input.h>
9*c5872d6aSAndy Shevchenko #include <linux/mod_devicetable.h>
10e70f18e1SAlexander Shiyan #include <linux/module.h>
11*c5872d6aSAndy Shevchenko #include <linux/gpio/consumer.h>
12e70f18e1SAlexander Shiyan #include <linux/platform_device.h>
13*c5872d6aSAndy Shevchenko #include <linux/property.h>
14e70f18e1SAlexander Shiyan #include <linux/regmap.h>
15e70f18e1SAlexander Shiyan #include <linux/sched.h>
16e70f18e1SAlexander Shiyan #include <linux/input/matrix_keypad.h>
17e70f18e1SAlexander Shiyan #include <linux/mfd/syscon.h>
18e70f18e1SAlexander Shiyan #include <linux/mfd/syscon/clps711x.h>
19e70f18e1SAlexander Shiyan 
20e70f18e1SAlexander Shiyan #define CLPS711X_KEYPAD_COL_COUNT	8
21e70f18e1SAlexander Shiyan 
22e70f18e1SAlexander Shiyan struct clps711x_gpio_data {
23e70f18e1SAlexander Shiyan 	struct gpio_desc *desc;
24e70f18e1SAlexander Shiyan 	DECLARE_BITMAP(last_state, CLPS711X_KEYPAD_COL_COUNT);
25e70f18e1SAlexander Shiyan };
26e70f18e1SAlexander Shiyan 
27e70f18e1SAlexander Shiyan struct clps711x_keypad_data {
28e70f18e1SAlexander Shiyan 	struct regmap			*syscon;
29e70f18e1SAlexander Shiyan 	int				row_count;
30e70f18e1SAlexander Shiyan 	unsigned int			row_shift;
31e70f18e1SAlexander Shiyan 	struct clps711x_gpio_data	*gpio_data;
32e70f18e1SAlexander Shiyan };
33e70f18e1SAlexander Shiyan 
clps711x_keypad_poll(struct input_dev * input)34ea6c52aeSDmitry Torokhov static void clps711x_keypad_poll(struct input_dev *input)
35e70f18e1SAlexander Shiyan {
36ea6c52aeSDmitry Torokhov 	const unsigned short *keycodes = input->keycode;
37ea6c52aeSDmitry Torokhov 	struct clps711x_keypad_data *priv = input_get_drvdata(input);
38e70f18e1SAlexander Shiyan 	bool sync = false;
39e70f18e1SAlexander Shiyan 	int col, row;
40e70f18e1SAlexander Shiyan 
41e70f18e1SAlexander Shiyan 	for (col = 0; col < CLPS711X_KEYPAD_COL_COUNT; col++) {
42e70f18e1SAlexander Shiyan 		/* Assert column */
43e70f18e1SAlexander Shiyan 		regmap_update_bits(priv->syscon, SYSCON_OFFSET,
44e70f18e1SAlexander Shiyan 				   SYSCON1_KBDSCAN_MASK,
45e70f18e1SAlexander Shiyan 				   SYSCON1_KBDSCAN(8 + col));
46e70f18e1SAlexander Shiyan 
47e70f18e1SAlexander Shiyan 		/* Scan rows */
48e70f18e1SAlexander Shiyan 		for (row = 0; row < priv->row_count; row++) {
49e70f18e1SAlexander Shiyan 			struct clps711x_gpio_data *data = &priv->gpio_data[row];
50e70f18e1SAlexander Shiyan 			bool state, state1;
51e70f18e1SAlexander Shiyan 
52e70f18e1SAlexander Shiyan 			/* Read twice for protection against fluctuations */
53e70f18e1SAlexander Shiyan 			do {
54e70f18e1SAlexander Shiyan 				state = gpiod_get_value_cansleep(data->desc);
55e70f18e1SAlexander Shiyan 				cond_resched();
56e70f18e1SAlexander Shiyan 				state1 = gpiod_get_value_cansleep(data->desc);
57e70f18e1SAlexander Shiyan 			} while (state != state1);
58e70f18e1SAlexander Shiyan 
59e70f18e1SAlexander Shiyan 			if (test_bit(col, data->last_state) != state) {
60e70f18e1SAlexander Shiyan 				int code = MATRIX_SCAN_CODE(row, col,
61e70f18e1SAlexander Shiyan 							    priv->row_shift);
62e70f18e1SAlexander Shiyan 
63e70f18e1SAlexander Shiyan 				if (state) {
64e70f18e1SAlexander Shiyan 					set_bit(col, data->last_state);
65ea6c52aeSDmitry Torokhov 					input_event(input,
66ea6c52aeSDmitry Torokhov 						    EV_MSC, MSC_SCAN, code);
67e70f18e1SAlexander Shiyan 				} else {
68e70f18e1SAlexander Shiyan 					clear_bit(col, data->last_state);
69e70f18e1SAlexander Shiyan 				}
70e70f18e1SAlexander Shiyan 
71e70f18e1SAlexander Shiyan 				if (keycodes[code])
72ea6c52aeSDmitry Torokhov 					input_report_key(input,
73e70f18e1SAlexander Shiyan 							 keycodes[code], state);
74e70f18e1SAlexander Shiyan 				sync = true;
75e70f18e1SAlexander Shiyan 			}
76e70f18e1SAlexander Shiyan 		}
77e70f18e1SAlexander Shiyan 
78e70f18e1SAlexander Shiyan 		/* Set all columns to low */
79e70f18e1SAlexander Shiyan 		regmap_update_bits(priv->syscon, SYSCON_OFFSET,
80e70f18e1SAlexander Shiyan 				   SYSCON1_KBDSCAN_MASK, SYSCON1_KBDSCAN(1));
81e70f18e1SAlexander Shiyan 	}
82e70f18e1SAlexander Shiyan 
83e70f18e1SAlexander Shiyan 	if (sync)
84ea6c52aeSDmitry Torokhov 		input_sync(input);
85e70f18e1SAlexander Shiyan }
86e70f18e1SAlexander Shiyan 
clps711x_keypad_probe(struct platform_device * pdev)87e70f18e1SAlexander Shiyan static int clps711x_keypad_probe(struct platform_device *pdev)
88e70f18e1SAlexander Shiyan {
89e70f18e1SAlexander Shiyan 	struct clps711x_keypad_data *priv;
90e70f18e1SAlexander Shiyan 	struct device *dev = &pdev->dev;
91ea6c52aeSDmitry Torokhov 	struct input_dev *input;
92e70f18e1SAlexander Shiyan 	u32 poll_interval;
93e70f18e1SAlexander Shiyan 	int i, err;
94e70f18e1SAlexander Shiyan 
95e70f18e1SAlexander Shiyan 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
96e70f18e1SAlexander Shiyan 	if (!priv)
97e70f18e1SAlexander Shiyan 		return -ENOMEM;
98e70f18e1SAlexander Shiyan 
99*c5872d6aSAndy Shevchenko 	priv->syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
100e70f18e1SAlexander Shiyan 	if (IS_ERR(priv->syscon))
101e70f18e1SAlexander Shiyan 		return PTR_ERR(priv->syscon);
102e70f18e1SAlexander Shiyan 
103*c5872d6aSAndy Shevchenko 	priv->row_count = gpiod_count(dev, "row");
104e70f18e1SAlexander Shiyan 	if (priv->row_count < 1)
105e70f18e1SAlexander Shiyan 		return -EINVAL;
106e70f18e1SAlexander Shiyan 
107a86854d0SKees Cook 	priv->gpio_data = devm_kcalloc(dev,
108a86854d0SKees Cook 				priv->row_count, sizeof(*priv->gpio_data),
109e70f18e1SAlexander Shiyan 				GFP_KERNEL);
110e70f18e1SAlexander Shiyan 	if (!priv->gpio_data)
111e70f18e1SAlexander Shiyan 		return -ENOMEM;
112e70f18e1SAlexander Shiyan 
113e70f18e1SAlexander Shiyan 	priv->row_shift = get_count_order(CLPS711X_KEYPAD_COL_COUNT);
114e70f18e1SAlexander Shiyan 
115e70f18e1SAlexander Shiyan 	for (i = 0; i < priv->row_count; i++) {
116e70f18e1SAlexander Shiyan 		struct clps711x_gpio_data *data = &priv->gpio_data[i];
117e70f18e1SAlexander Shiyan 
118ea0afac4SUwe Kleine-König 		data->desc = devm_gpiod_get_index(dev, "row", i, GPIOD_IN);
119e70f18e1SAlexander Shiyan 		if (IS_ERR(data->desc))
120e70f18e1SAlexander Shiyan 			return PTR_ERR(data->desc);
121e70f18e1SAlexander Shiyan 	}
122e70f18e1SAlexander Shiyan 
123*c5872d6aSAndy Shevchenko 	err = device_property_read_u32(dev, "poll-interval", &poll_interval);
124e70f18e1SAlexander Shiyan 	if (err)
125e70f18e1SAlexander Shiyan 		return err;
126e70f18e1SAlexander Shiyan 
127ea6c52aeSDmitry Torokhov 	input = devm_input_allocate_device(dev);
128ea6c52aeSDmitry Torokhov 	if (!input)
129e70f18e1SAlexander Shiyan 		return -ENOMEM;
130e70f18e1SAlexander Shiyan 
131ea6c52aeSDmitry Torokhov 	input_set_drvdata(input, priv);
132ea6c52aeSDmitry Torokhov 
133ea6c52aeSDmitry Torokhov 	input->name		= pdev->name;
134ea6c52aeSDmitry Torokhov 	input->dev.parent	= dev;
135ea6c52aeSDmitry Torokhov 	input->id.bustype	= BUS_HOST;
136ea6c52aeSDmitry Torokhov 	input->id.vendor	= 0x0001;
137ea6c52aeSDmitry Torokhov 	input->id.product	= 0x0001;
138ea6c52aeSDmitry Torokhov 	input->id.version	= 0x0100;
139e70f18e1SAlexander Shiyan 
140e70f18e1SAlexander Shiyan 	err = matrix_keypad_build_keymap(NULL, NULL, priv->row_count,
141e70f18e1SAlexander Shiyan 					 CLPS711X_KEYPAD_COL_COUNT,
142ea6c52aeSDmitry Torokhov 					 NULL, input);
143e70f18e1SAlexander Shiyan 	if (err)
144ea6c52aeSDmitry Torokhov 		return err;
145e70f18e1SAlexander Shiyan 
146ea6c52aeSDmitry Torokhov 	input_set_capability(input, EV_MSC, MSC_SCAN);
147*c5872d6aSAndy Shevchenko 	if (device_property_read_bool(dev, "autorepeat"))
148ea6c52aeSDmitry Torokhov 		__set_bit(EV_REP, input->evbit);
149e70f18e1SAlexander Shiyan 
150e70f18e1SAlexander Shiyan 	/* Set all columns to low */
151e70f18e1SAlexander Shiyan 	regmap_update_bits(priv->syscon, SYSCON_OFFSET, SYSCON1_KBDSCAN_MASK,
152e70f18e1SAlexander Shiyan 			   SYSCON1_KBDSCAN(1));
153e70f18e1SAlexander Shiyan 
154ea6c52aeSDmitry Torokhov 
155ea6c52aeSDmitry Torokhov 	err = input_setup_polling(input, clps711x_keypad_poll);
156e70f18e1SAlexander Shiyan 	if (err)
157e70f18e1SAlexander Shiyan 		return err;
158e70f18e1SAlexander Shiyan 
159ea6c52aeSDmitry Torokhov 	input_set_poll_interval(input, poll_interval);
160e70f18e1SAlexander Shiyan 
161ea6c52aeSDmitry Torokhov 	err = input_register_device(input);
162ea6c52aeSDmitry Torokhov 	if (err)
163ea6c52aeSDmitry Torokhov 		return err;
164e70f18e1SAlexander Shiyan 
165e70f18e1SAlexander Shiyan 	return 0;
166e70f18e1SAlexander Shiyan }
167e70f18e1SAlexander Shiyan 
168af4cf6d3SJingoo Han static const struct of_device_id clps711x_keypad_of_match[] = {
169e7c38f22SAlexander Shiyan 	{ .compatible = "cirrus,ep7209-keypad", },
170e70f18e1SAlexander Shiyan 	{ }
171e70f18e1SAlexander Shiyan };
172e70f18e1SAlexander Shiyan MODULE_DEVICE_TABLE(of, clps711x_keypad_of_match);
173e70f18e1SAlexander Shiyan 
174e70f18e1SAlexander Shiyan static struct platform_driver clps711x_keypad_driver = {
175e70f18e1SAlexander Shiyan 	.driver	= {
176e70f18e1SAlexander Shiyan 		.name		= "clps711x-keypad",
177e70f18e1SAlexander Shiyan 		.of_match_table	= clps711x_keypad_of_match,
178e70f18e1SAlexander Shiyan 	},
179e70f18e1SAlexander Shiyan 	.probe	= clps711x_keypad_probe,
180e70f18e1SAlexander Shiyan };
181e70f18e1SAlexander Shiyan module_platform_driver(clps711x_keypad_driver);
182e70f18e1SAlexander Shiyan 
183e70f18e1SAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
184e70f18e1SAlexander Shiyan MODULE_DESCRIPTION("Cirrus Logic CLPS711X Keypad driver");
185e70f18e1SAlexander Shiyan MODULE_LICENSE("GPL");
186