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
keyscan_isr(int irq,void * dev_id)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
keyscan_start(struct st_keyscan * keypad)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
keyscan_stop(struct st_keyscan * keypad)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
keyscan_open(struct input_dev * dev)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
keyscan_close(struct input_dev * dev)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
keypad_matrix_key_parse_dt(struct st_keyscan * keypad_data)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
keyscan_probe(struct platform_device * pdev)124 static int keyscan_probe(struct platform_device *pdev)
125 {
126 struct st_keyscan *keypad_data;
127 struct input_dev *input_dev;
128 int error;
129
130 if (!pdev->dev.of_node) {
131 dev_err(&pdev->dev, "no DT data present\n");
132 return -EINVAL;
133 }
134
135 keypad_data = devm_kzalloc(&pdev->dev, sizeof(*keypad_data),
136 GFP_KERNEL);
137 if (!keypad_data)
138 return -ENOMEM;
139
140 input_dev = devm_input_allocate_device(&pdev->dev);
141 if (!input_dev) {
142 dev_err(&pdev->dev, "failed to allocate the input device\n");
143 return -ENOMEM;
144 }
145
146 input_dev->name = pdev->name;
147 input_dev->phys = "keyscan-keys/input0";
148 input_dev->dev.parent = &pdev->dev;
149 input_dev->open = keyscan_open;
150 input_dev->close = keyscan_close;
151
152 input_dev->id.bustype = BUS_HOST;
153
154 keypad_data->input_dev = input_dev;
155
156 error = keypad_matrix_key_parse_dt(keypad_data);
157 if (error)
158 return error;
159
160 error = matrix_keypad_build_keymap(NULL, NULL,
161 keypad_data->n_rows,
162 keypad_data->n_cols,
163 NULL, input_dev);
164 if (error) {
165 dev_err(&pdev->dev, "failed to build keymap\n");
166 return error;
167 }
168
169 input_set_drvdata(input_dev, keypad_data);
170
171 keypad_data->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
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 return -EINVAL;
192
193 error = devm_request_irq(&pdev->dev, keypad_data->irq, keyscan_isr, 0,
194 pdev->name, keypad_data);
195 if (error) {
196 dev_err(&pdev->dev, "failed to request IRQ\n");
197 return error;
198 }
199
200 error = input_register_device(input_dev);
201 if (error) {
202 dev_err(&pdev->dev, "failed to register input device\n");
203 return error;
204 }
205
206 platform_set_drvdata(pdev, keypad_data);
207
208 device_set_wakeup_capable(&pdev->dev, 1);
209
210 return 0;
211 }
212
keyscan_suspend(struct device * dev)213 static int keyscan_suspend(struct device *dev)
214 {
215 struct platform_device *pdev = to_platform_device(dev);
216 struct st_keyscan *keypad = platform_get_drvdata(pdev);
217 struct input_dev *input = keypad->input_dev;
218
219 mutex_lock(&input->mutex);
220
221 if (device_may_wakeup(dev))
222 enable_irq_wake(keypad->irq);
223 else if (input_device_enabled(input))
224 keyscan_stop(keypad);
225
226 mutex_unlock(&input->mutex);
227 return 0;
228 }
229
keyscan_resume(struct device * dev)230 static int keyscan_resume(struct device *dev)
231 {
232 struct platform_device *pdev = to_platform_device(dev);
233 struct st_keyscan *keypad = platform_get_drvdata(pdev);
234 struct input_dev *input = keypad->input_dev;
235 int retval = 0;
236
237 mutex_lock(&input->mutex);
238
239 if (device_may_wakeup(dev))
240 disable_irq_wake(keypad->irq);
241 else if (input_device_enabled(input))
242 retval = keyscan_start(keypad);
243
244 mutex_unlock(&input->mutex);
245 return retval;
246 }
247
248 static DEFINE_SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops,
249 keyscan_suspend, keyscan_resume);
250
251 static const struct of_device_id keyscan_of_match[] = {
252 { .compatible = "st,sti-keyscan" },
253 { },
254 };
255 MODULE_DEVICE_TABLE(of, keyscan_of_match);
256
257 static struct platform_driver keyscan_device_driver = {
258 .probe = keyscan_probe,
259 .driver = {
260 .name = "st-keyscan",
261 .pm = pm_sleep_ptr(&keyscan_dev_pm_ops),
262 .of_match_table = of_match_ptr(keyscan_of_match),
263 }
264 };
265
266 module_platform_driver(keyscan_device_driver);
267
268 MODULE_AUTHOR("Stuart Menefy <stuart.menefy@st.com>");
269 MODULE_DESCRIPTION("STMicroelectronics keyscan device driver");
270 MODULE_LICENSE("GPL");
271