1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics Key Scanning driver
4  *
5  * Copyright (c) 2014 STMicroelectonics Ltd.
6  * Author: Stuart Menefy <stuart.menefy@st.com>
7  *
8  * Based on sh_keysc.c, copyright 2008 Magnus Damm
9  */
10 
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/io.h>
16 #include <linux/input/matrix_keypad.h>
17 
18 #define ST_KEYSCAN_MAXKEYS 16
19 
20 #define KEYSCAN_CONFIG_OFF		0x0
21 #define KEYSCAN_CONFIG_ENABLE		0x1
22 #define KEYSCAN_DEBOUNCE_TIME_OFF	0x4
23 #define KEYSCAN_MATRIX_STATE_OFF	0x8
24 #define KEYSCAN_MATRIX_DIM_OFF		0xc
25 #define KEYSCAN_MATRIX_DIM_X_SHIFT	0x0
26 #define KEYSCAN_MATRIX_DIM_Y_SHIFT	0x2
27 
28 struct st_keyscan {
29 	void __iomem *base;
30 	int irq;
31 	struct clk *clk;
32 	struct input_dev *input_dev;
33 	unsigned long last_state;
34 	unsigned int n_rows;
35 	unsigned int n_cols;
36 	unsigned int debounce_us;
37 };
38 
39 static irqreturn_t keyscan_isr(int irq, void *dev_id)
40 {
41 	struct st_keyscan *keypad = dev_id;
42 	unsigned short *keycode = keypad->input_dev->keycode;
43 	unsigned long state, change;
44 	int bit_nr;
45 
46 	state = readl(keypad->base + KEYSCAN_MATRIX_STATE_OFF) & 0xffff;
47 	change = keypad->last_state ^ state;
48 	keypad->last_state = state;
49 
50 	for_each_set_bit(bit_nr, &change, BITS_PER_LONG)
51 		input_report_key(keypad->input_dev,
52 				 keycode[bit_nr], state & BIT(bit_nr));
53 
54 	input_sync(keypad->input_dev);
55 
56 	return IRQ_HANDLED;
57 }
58 
59 static int keyscan_start(struct st_keyscan *keypad)
60 {
61 	int error;
62 
63 	error = clk_enable(keypad->clk);
64 	if (error)
65 		return error;
66 
67 	writel(keypad->debounce_us * (clk_get_rate(keypad->clk) / 1000000),
68 	       keypad->base + KEYSCAN_DEBOUNCE_TIME_OFF);
69 
70 	writel(((keypad->n_cols - 1) << KEYSCAN_MATRIX_DIM_X_SHIFT) |
71 	       ((keypad->n_rows - 1) << KEYSCAN_MATRIX_DIM_Y_SHIFT),
72 	       keypad->base + KEYSCAN_MATRIX_DIM_OFF);
73 
74 	writel(KEYSCAN_CONFIG_ENABLE, keypad->base + KEYSCAN_CONFIG_OFF);
75 
76 	return 0;
77 }
78 
79 static void keyscan_stop(struct st_keyscan *keypad)
80 {
81 	writel(0, keypad->base + KEYSCAN_CONFIG_OFF);
82 
83 	clk_disable(keypad->clk);
84 }
85 
86 static int keyscan_open(struct input_dev *dev)
87 {
88 	struct st_keyscan *keypad = input_get_drvdata(dev);
89 
90 	return keyscan_start(keypad);
91 }
92 
93 static void keyscan_close(struct input_dev *dev)
94 {
95 	struct st_keyscan *keypad = input_get_drvdata(dev);
96 
97 	keyscan_stop(keypad);
98 }
99 
100 static int keypad_matrix_key_parse_dt(struct st_keyscan *keypad_data)
101 {
102 	struct device *dev = keypad_data->input_dev->dev.parent;
103 	struct device_node *np = dev->of_node;
104 	int error;
105 
106 	error = matrix_keypad_parse_properties(dev, &keypad_data->n_rows,
107 					       &keypad_data->n_cols);
108 	if (error) {
109 		dev_err(dev, "failed to parse keypad params\n");
110 		return error;
111 	}
112 
113 	of_property_read_u32(np, "st,debounce-us", &keypad_data->debounce_us);
114 
115 	dev_dbg(dev, "n_rows=%d n_col=%d debounce=%d\n",
116 		keypad_data->n_rows, keypad_data->n_cols,
117 		keypad_data->debounce_us);
118 
119 	return 0;
120 }
121 
122 static int keyscan_probe(struct platform_device *pdev)
123 {
124 	struct st_keyscan *keypad_data;
125 	struct input_dev *input_dev;
126 	struct resource *res;
127 	int error;
128 
129 	if (!pdev->dev.of_node) {
130 		dev_err(&pdev->dev, "no DT data present\n");
131 		return -EINVAL;
132 	}
133 
134 	keypad_data = devm_kzalloc(&pdev->dev, sizeof(*keypad_data),
135 				   GFP_KERNEL);
136 	if (!keypad_data)
137 		return -ENOMEM;
138 
139 	input_dev = devm_input_allocate_device(&pdev->dev);
140 	if (!input_dev) {
141 		dev_err(&pdev->dev, "failed to allocate the input device\n");
142 		return -ENOMEM;
143 	}
144 
145 	input_dev->name = pdev->name;
146 	input_dev->phys = "keyscan-keys/input0";
147 	input_dev->dev.parent = &pdev->dev;
148 	input_dev->open = keyscan_open;
149 	input_dev->close = keyscan_close;
150 
151 	input_dev->id.bustype = BUS_HOST;
152 
153 	keypad_data->input_dev = input_dev;
154 
155 	error = keypad_matrix_key_parse_dt(keypad_data);
156 	if (error)
157 		return error;
158 
159 	error = matrix_keypad_build_keymap(NULL, NULL,
160 					   keypad_data->n_rows,
161 					   keypad_data->n_cols,
162 					   NULL, input_dev);
163 	if (error) {
164 		dev_err(&pdev->dev, "failed to build keymap\n");
165 		return error;
166 	}
167 
168 	input_set_drvdata(input_dev, keypad_data);
169 
170 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
171 	keypad_data->base = devm_ioremap_resource(&pdev->dev, res);
172 	if (IS_ERR(keypad_data->base))
173 		return PTR_ERR(keypad_data->base);
174 
175 	keypad_data->clk = devm_clk_get(&pdev->dev, NULL);
176 	if (IS_ERR(keypad_data->clk)) {
177 		dev_err(&pdev->dev, "cannot get clock\n");
178 		return PTR_ERR(keypad_data->clk);
179 	}
180 
181 	error = clk_enable(keypad_data->clk);
182 	if (error) {
183 		dev_err(&pdev->dev, "failed to enable clock\n");
184 		return error;
185 	}
186 
187 	keyscan_stop(keypad_data);
188 
189 	keypad_data->irq = platform_get_irq(pdev, 0);
190 	if (keypad_data->irq < 0) {
191 		dev_err(&pdev->dev, "no IRQ specified\n");
192 		return -EINVAL;
193 	}
194 
195 	error = devm_request_irq(&pdev->dev, keypad_data->irq, keyscan_isr, 0,
196 				 pdev->name, keypad_data);
197 	if (error) {
198 		dev_err(&pdev->dev, "failed to request IRQ\n");
199 		return error;
200 	}
201 
202 	error = input_register_device(input_dev);
203 	if (error) {
204 		dev_err(&pdev->dev, "failed to register input device\n");
205 		return error;
206 	}
207 
208 	platform_set_drvdata(pdev, keypad_data);
209 
210 	device_set_wakeup_capable(&pdev->dev, 1);
211 
212 	return 0;
213 }
214 
215 #ifdef CONFIG_PM_SLEEP
216 static int keyscan_suspend(struct device *dev)
217 {
218 	struct platform_device *pdev = to_platform_device(dev);
219 	struct st_keyscan *keypad = platform_get_drvdata(pdev);
220 	struct input_dev *input = keypad->input_dev;
221 
222 	mutex_lock(&input->mutex);
223 
224 	if (device_may_wakeup(dev))
225 		enable_irq_wake(keypad->irq);
226 	else if (input->users)
227 		keyscan_stop(keypad);
228 
229 	mutex_unlock(&input->mutex);
230 	return 0;
231 }
232 
233 static int keyscan_resume(struct device *dev)
234 {
235 	struct platform_device *pdev = to_platform_device(dev);
236 	struct st_keyscan *keypad = platform_get_drvdata(pdev);
237 	struct input_dev *input = keypad->input_dev;
238 	int retval = 0;
239 
240 	mutex_lock(&input->mutex);
241 
242 	if (device_may_wakeup(dev))
243 		disable_irq_wake(keypad->irq);
244 	else if (input->users)
245 		retval = keyscan_start(keypad);
246 
247 	mutex_unlock(&input->mutex);
248 	return retval;
249 }
250 #endif
251 
252 static SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops, keyscan_suspend, keyscan_resume);
253 
254 static const struct of_device_id keyscan_of_match[] = {
255 	{ .compatible = "st,sti-keyscan" },
256 	{ },
257 };
258 MODULE_DEVICE_TABLE(of, keyscan_of_match);
259 
260 static struct platform_driver keyscan_device_driver = {
261 	.probe		= keyscan_probe,
262 	.driver		= {
263 		.name	= "st-keyscan",
264 		.pm	= &keyscan_dev_pm_ops,
265 		.of_match_table = of_match_ptr(keyscan_of_match),
266 	}
267 };
268 
269 module_platform_driver(keyscan_device_driver);
270 
271 MODULE_AUTHOR("Stuart Menefy <stuart.menefy@st.com>");
272 MODULE_DESCRIPTION("STMicroelectronics keyscan device driver");
273 MODULE_LICENSE("GPL");
274