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