WIP FPC-III support
[linux/fpc-iii.git] / drivers / input / keyboard / st-keyscan.c
bloba045d61165acc25e5efcaa7cac2307a64d787c1a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
9 */
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>
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
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;
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;
44 int bit_nr;
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);
56 return IRQ_HANDLED;
59 static int keyscan_start(struct st_keyscan *keypad)
61 int error;
63 error = clk_enable(keypad->clk);
64 if (error)
65 return error;
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);
76 return 0;
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);
97 keyscan_stop(keypad);
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;
104 int error;
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;
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);
119 return 0;
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;
127 int error;
129 if (!pdev->dev.of_node) {
130 dev_err(&pdev->dev, "no DT data present\n");
131 return -EINVAL;
134 keypad_data = devm_kzalloc(&pdev->dev, sizeof(*keypad_data),
135 GFP_KERNEL);
136 if (!keypad_data)
137 return -ENOMEM;
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;
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);
156 if (error)
157 return error;
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;
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);
182 if (error) {
183 dev_err(&pdev->dev, "failed to enable clock\n");
184 return error;
187 keyscan_stop(keypad_data);
189 keypad_data->irq = platform_get_irq(pdev, 0);
190 if (keypad_data->irq < 0)
191 return -EINVAL;
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;
200 error = input_register_device(input_dev);
201 if (error) {
202 dev_err(&pdev->dev, "failed to register input device\n");
203 return error;
206 platform_set_drvdata(pdev, keypad_data);
208 device_set_wakeup_capable(&pdev->dev, 1);
210 return 0;
213 #ifdef CONFIG_PM_SLEEP
214 static int keyscan_suspend(struct device *dev)
216 struct platform_device *pdev = to_platform_device(dev);
217 struct st_keyscan *keypad = platform_get_drvdata(pdev);
218 struct input_dev *input = keypad->input_dev;
220 mutex_lock(&input->mutex);
222 if (device_may_wakeup(dev))
223 enable_irq_wake(keypad->irq);
224 else if (input_device_enabled(input))
225 keyscan_stop(keypad);
227 mutex_unlock(&input->mutex);
228 return 0;
231 static int keyscan_resume(struct device *dev)
233 struct platform_device *pdev = to_platform_device(dev);
234 struct st_keyscan *keypad = platform_get_drvdata(pdev);
235 struct input_dev *input = keypad->input_dev;
236 int retval = 0;
238 mutex_lock(&input->mutex);
240 if (device_may_wakeup(dev))
241 disable_irq_wake(keypad->irq);
242 else if (input_device_enabled(input))
243 retval = keyscan_start(keypad);
245 mutex_unlock(&input->mutex);
246 return retval;
248 #endif
250 static SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops, keyscan_suspend, keyscan_resume);
252 static const struct of_device_id keyscan_of_match[] = {
253 { .compatible = "st,sti-keyscan" },
254 { },
256 MODULE_DEVICE_TABLE(of, keyscan_of_match);
258 static struct platform_driver keyscan_device_driver = {
259 .probe = keyscan_probe,
260 .driver = {
261 .name = "st-keyscan",
262 .pm = &keyscan_dev_pm_ops,
263 .of_match_table = of_match_ptr(keyscan_of_match),
267 module_platform_driver(keyscan_device_driver);
269 MODULE_AUTHOR("Stuart Menefy <stuart.menefy@st.com>");
270 MODULE_DESCRIPTION("STMicroelectronics keyscan device driver");
271 MODULE_LICENSE("GPL");