1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics Key Scanning driver
5 * Copyright (c) 2014 STMicroelectonics Ltd.
6 * Author: Stuart Menefy <stuart.menefy@st.com>
8 * Based on sh_keysc.c, copyright 2008 Magnus Damm
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
16 #include <linux/input/matrix_keypad.h>
18 #define ST_KEYSCAN_MAXKEYS 16
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
32 struct input_dev *input_dev;
33 unsigned long last_state;
36 unsigned int debounce_us;
39 static irqreturn_t keyscan_isr(int irq, void *dev_id)
41 struct st_keyscan *keypad = dev_id;
42 unsigned short *keycode = keypad->input_dev->keycode;
43 unsigned long state, change;
46 state = readl(keypad->base + KEYSCAN_MATRIX_STATE_OFF) & 0xffff;
47 change = keypad->last_state ^ state;
48 keypad->last_state = state;
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));
54 input_sync(keypad->input_dev);
59 static int keyscan_start(struct st_keyscan *keypad)
63 error = clk_enable(keypad->clk);
67 writel(keypad->debounce_us * (clk_get_rate(keypad->clk) / 1000000),
68 keypad->base + KEYSCAN_DEBOUNCE_TIME_OFF);
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);
74 writel(KEYSCAN_CONFIG_ENABLE, keypad->base + KEYSCAN_CONFIG_OFF);
79 static void keyscan_stop(struct st_keyscan *keypad)
81 writel(0, keypad->base + KEYSCAN_CONFIG_OFF);
83 clk_disable(keypad->clk);
86 static int keyscan_open(struct input_dev *dev)
88 struct st_keyscan *keypad = input_get_drvdata(dev);
90 return keyscan_start(keypad);
93 static void keyscan_close(struct input_dev *dev)
95 struct st_keyscan *keypad = input_get_drvdata(dev);
100 static int keypad_matrix_key_parse_dt(struct st_keyscan *keypad_data)
102 struct device *dev = keypad_data->input_dev->dev.parent;
103 struct device_node *np = dev->of_node;
106 error = matrix_keypad_parse_properties(dev, &keypad_data->n_rows,
107 &keypad_data->n_cols);
109 dev_err(dev, "failed to parse keypad params\n");
113 of_property_read_u32(np, "st,debounce-us", &keypad_data->debounce_us);
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);
122 static int keyscan_probe(struct platform_device *pdev)
124 struct st_keyscan *keypad_data;
125 struct input_dev *input_dev;
126 struct resource *res;
129 if (!pdev->dev.of_node) {
130 dev_err(&pdev->dev, "no DT data present\n");
134 keypad_data = devm_kzalloc(&pdev->dev, sizeof(*keypad_data),
139 input_dev = devm_input_allocate_device(&pdev->dev);
141 dev_err(&pdev->dev, "failed to allocate the input device\n");
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;
151 input_dev->id.bustype = BUS_HOST;
153 keypad_data->input_dev = input_dev;
155 error = keypad_matrix_key_parse_dt(keypad_data);
159 error = matrix_keypad_build_keymap(NULL, NULL,
164 dev_err(&pdev->dev, "failed to build keymap\n");
168 input_set_drvdata(input_dev, keypad_data);
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);
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);
181 error = clk_enable(keypad_data->clk);
183 dev_err(&pdev->dev, "failed to enable clock\n");
187 keyscan_stop(keypad_data);
189 keypad_data->irq = platform_get_irq(pdev, 0);
190 if (keypad_data->irq < 0) {
191 dev_err(&pdev->dev, "no IRQ specified\n");
195 error = devm_request_irq(&pdev->dev, keypad_data->irq, keyscan_isr, 0,
196 pdev->name, keypad_data);
198 dev_err(&pdev->dev, "failed to request IRQ\n");
202 error = input_register_device(input_dev);
204 dev_err(&pdev->dev, "failed to register input device\n");
208 platform_set_drvdata(pdev, keypad_data);
210 device_set_wakeup_capable(&pdev->dev, 1);
215 #ifdef CONFIG_PM_SLEEP
216 static int keyscan_suspend(struct device *dev)
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;
222 mutex_lock(&input->mutex);
224 if (device_may_wakeup(dev))
225 enable_irq_wake(keypad->irq);
226 else if (input->users)
227 keyscan_stop(keypad);
229 mutex_unlock(&input->mutex);
233 static int keyscan_resume(struct device *dev)
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;
240 mutex_lock(&input->mutex);
242 if (device_may_wakeup(dev))
243 disable_irq_wake(keypad->irq);
244 else if (input->users)
245 retval = keyscan_start(keypad);
247 mutex_unlock(&input->mutex);
252 static SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops, keyscan_suspend, keyscan_resume);
254 static const struct of_device_id keyscan_of_match[] = {
255 { .compatible = "st,sti-keyscan" },
258 MODULE_DEVICE_TABLE(of, keyscan_of_match);
260 static struct platform_driver keyscan_device_driver = {
261 .probe = keyscan_probe,
263 .name = "st-keyscan",
264 .pm = &keyscan_dev_pm_ops,
265 .of_match_table = of_match_ptr(keyscan_of_match),
269 module_platform_driver(keyscan_device_driver);
271 MODULE_AUTHOR("Stuart Menefy <stuart.menefy@st.com>");
272 MODULE_DESCRIPTION("STMicroelectronics keyscan device driver");
273 MODULE_LICENSE("GPL");