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