1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2062589b1SGabriel FERNANDEZ /*
3062589b1SGabriel FERNANDEZ * STMicroelectronics Key Scanning driver
4062589b1SGabriel FERNANDEZ *
5062589b1SGabriel FERNANDEZ * Copyright (c) 2014 STMicroelectonics Ltd.
6062589b1SGabriel FERNANDEZ * Author: Stuart Menefy <stuart.menefy@st.com>
7062589b1SGabriel FERNANDEZ *
8062589b1SGabriel FERNANDEZ * Based on sh_keysc.c, copyright 2008 Magnus Damm
9062589b1SGabriel FERNANDEZ */
10062589b1SGabriel FERNANDEZ
11062589b1SGabriel FERNANDEZ #include <linux/clk.h>
12d25a9d8fSDmitry Torokhov #include <linux/input.h>
13062589b1SGabriel FERNANDEZ #include <linux/input/matrix_keypad.h>
14d25a9d8fSDmitry Torokhov #include <linux/io.h>
15d25a9d8fSDmitry Torokhov #include <linux/interrupt.h>
16d25a9d8fSDmitry Torokhov #include <linux/module.h>
17d25a9d8fSDmitry Torokhov #include <linux/of.h>
18d25a9d8fSDmitry Torokhov #include <linux/platform_device.h>
19062589b1SGabriel FERNANDEZ
20062589b1SGabriel FERNANDEZ #define ST_KEYSCAN_MAXKEYS 16
21062589b1SGabriel FERNANDEZ
22062589b1SGabriel FERNANDEZ #define KEYSCAN_CONFIG_OFF 0x0
23062589b1SGabriel FERNANDEZ #define KEYSCAN_CONFIG_ENABLE 0x1
24062589b1SGabriel FERNANDEZ #define KEYSCAN_DEBOUNCE_TIME_OFF 0x4
25062589b1SGabriel FERNANDEZ #define KEYSCAN_MATRIX_STATE_OFF 0x8
26062589b1SGabriel FERNANDEZ #define KEYSCAN_MATRIX_DIM_OFF 0xc
27062589b1SGabriel FERNANDEZ #define KEYSCAN_MATRIX_DIM_X_SHIFT 0x0
28062589b1SGabriel FERNANDEZ #define KEYSCAN_MATRIX_DIM_Y_SHIFT 0x2
29062589b1SGabriel FERNANDEZ
30062589b1SGabriel FERNANDEZ struct st_keyscan {
31062589b1SGabriel FERNANDEZ void __iomem *base;
32062589b1SGabriel FERNANDEZ int irq;
33062589b1SGabriel FERNANDEZ struct clk *clk;
34062589b1SGabriel FERNANDEZ struct input_dev *input_dev;
35062589b1SGabriel FERNANDEZ unsigned long last_state;
36062589b1SGabriel FERNANDEZ unsigned int n_rows;
37062589b1SGabriel FERNANDEZ unsigned int n_cols;
38062589b1SGabriel FERNANDEZ unsigned int debounce_us;
39062589b1SGabriel FERNANDEZ };
40062589b1SGabriel FERNANDEZ
keyscan_isr(int irq,void * dev_id)41062589b1SGabriel FERNANDEZ static irqreturn_t keyscan_isr(int irq, void *dev_id)
42062589b1SGabriel FERNANDEZ {
43062589b1SGabriel FERNANDEZ struct st_keyscan *keypad = dev_id;
44062589b1SGabriel FERNANDEZ unsigned short *keycode = keypad->input_dev->keycode;
45062589b1SGabriel FERNANDEZ unsigned long state, change;
46062589b1SGabriel FERNANDEZ int bit_nr;
47062589b1SGabriel FERNANDEZ
48062589b1SGabriel FERNANDEZ state = readl(keypad->base + KEYSCAN_MATRIX_STATE_OFF) & 0xffff;
49062589b1SGabriel FERNANDEZ change = keypad->last_state ^ state;
50062589b1SGabriel FERNANDEZ keypad->last_state = state;
51062589b1SGabriel FERNANDEZ
52062589b1SGabriel FERNANDEZ for_each_set_bit(bit_nr, &change, BITS_PER_LONG)
53062589b1SGabriel FERNANDEZ input_report_key(keypad->input_dev,
54062589b1SGabriel FERNANDEZ keycode[bit_nr], state & BIT(bit_nr));
55062589b1SGabriel FERNANDEZ
56062589b1SGabriel FERNANDEZ input_sync(keypad->input_dev);
57062589b1SGabriel FERNANDEZ
58062589b1SGabriel FERNANDEZ return IRQ_HANDLED;
59062589b1SGabriel FERNANDEZ }
60062589b1SGabriel FERNANDEZ
keyscan_start(struct st_keyscan * keypad)61062589b1SGabriel FERNANDEZ static int keyscan_start(struct st_keyscan *keypad)
62062589b1SGabriel FERNANDEZ {
63062589b1SGabriel FERNANDEZ int error;
64062589b1SGabriel FERNANDEZ
65062589b1SGabriel FERNANDEZ error = clk_enable(keypad->clk);
66062589b1SGabriel FERNANDEZ if (error)
67062589b1SGabriel FERNANDEZ return error;
68062589b1SGabriel FERNANDEZ
69062589b1SGabriel FERNANDEZ writel(keypad->debounce_us * (clk_get_rate(keypad->clk) / 1000000),
70062589b1SGabriel FERNANDEZ keypad->base + KEYSCAN_DEBOUNCE_TIME_OFF);
71062589b1SGabriel FERNANDEZ
72062589b1SGabriel FERNANDEZ writel(((keypad->n_cols - 1) << KEYSCAN_MATRIX_DIM_X_SHIFT) |
73062589b1SGabriel FERNANDEZ ((keypad->n_rows - 1) << KEYSCAN_MATRIX_DIM_Y_SHIFT),
74062589b1SGabriel FERNANDEZ keypad->base + KEYSCAN_MATRIX_DIM_OFF);
75062589b1SGabriel FERNANDEZ
76062589b1SGabriel FERNANDEZ writel(KEYSCAN_CONFIG_ENABLE, keypad->base + KEYSCAN_CONFIG_OFF);
77062589b1SGabriel FERNANDEZ
78062589b1SGabriel FERNANDEZ return 0;
79062589b1SGabriel FERNANDEZ }
80062589b1SGabriel FERNANDEZ
keyscan_stop(struct st_keyscan * keypad)81062589b1SGabriel FERNANDEZ static void keyscan_stop(struct st_keyscan *keypad)
82062589b1SGabriel FERNANDEZ {
83062589b1SGabriel FERNANDEZ writel(0, keypad->base + KEYSCAN_CONFIG_OFF);
84062589b1SGabriel FERNANDEZ
85062589b1SGabriel FERNANDEZ clk_disable(keypad->clk);
86062589b1SGabriel FERNANDEZ }
87062589b1SGabriel FERNANDEZ
keyscan_open(struct input_dev * dev)88062589b1SGabriel FERNANDEZ static int keyscan_open(struct input_dev *dev)
89062589b1SGabriel FERNANDEZ {
90062589b1SGabriel FERNANDEZ struct st_keyscan *keypad = input_get_drvdata(dev);
91062589b1SGabriel FERNANDEZ
92062589b1SGabriel FERNANDEZ return keyscan_start(keypad);
93062589b1SGabriel FERNANDEZ }
94062589b1SGabriel FERNANDEZ
keyscan_close(struct input_dev * dev)95062589b1SGabriel FERNANDEZ static void keyscan_close(struct input_dev *dev)
96062589b1SGabriel FERNANDEZ {
97062589b1SGabriel FERNANDEZ struct st_keyscan *keypad = input_get_drvdata(dev);
98062589b1SGabriel FERNANDEZ
99062589b1SGabriel FERNANDEZ keyscan_stop(keypad);
100062589b1SGabriel FERNANDEZ }
101062589b1SGabriel FERNANDEZ
keypad_matrix_key_parse_dt(struct st_keyscan * keypad_data)102062589b1SGabriel FERNANDEZ static int keypad_matrix_key_parse_dt(struct st_keyscan *keypad_data)
103062589b1SGabriel FERNANDEZ {
104062589b1SGabriel FERNANDEZ struct device *dev = keypad_data->input_dev->dev.parent;
105062589b1SGabriel FERNANDEZ struct device_node *np = dev->of_node;
106062589b1SGabriel FERNANDEZ int error;
107062589b1SGabriel FERNANDEZ
108aef01aadSDmitry Torokhov error = matrix_keypad_parse_properties(dev, &keypad_data->n_rows,
109062589b1SGabriel FERNANDEZ &keypad_data->n_cols);
110062589b1SGabriel FERNANDEZ if (error) {
111062589b1SGabriel FERNANDEZ dev_err(dev, "failed to parse keypad params\n");
112062589b1SGabriel FERNANDEZ return error;
113062589b1SGabriel FERNANDEZ }
114062589b1SGabriel FERNANDEZ
115062589b1SGabriel FERNANDEZ of_property_read_u32(np, "st,debounce-us", &keypad_data->debounce_us);
116062589b1SGabriel FERNANDEZ
117062589b1SGabriel FERNANDEZ dev_dbg(dev, "n_rows=%d n_col=%d debounce=%d\n",
118062589b1SGabriel FERNANDEZ keypad_data->n_rows, keypad_data->n_cols,
119062589b1SGabriel FERNANDEZ keypad_data->debounce_us);
120062589b1SGabriel FERNANDEZ
121062589b1SGabriel FERNANDEZ return 0;
122062589b1SGabriel FERNANDEZ }
123062589b1SGabriel FERNANDEZ
keyscan_probe(struct platform_device * pdev)124062589b1SGabriel FERNANDEZ static int keyscan_probe(struct platform_device *pdev)
125062589b1SGabriel FERNANDEZ {
126062589b1SGabriel FERNANDEZ struct st_keyscan *keypad_data;
127062589b1SGabriel FERNANDEZ struct input_dev *input_dev;
128062589b1SGabriel FERNANDEZ int error;
129062589b1SGabriel FERNANDEZ
130062589b1SGabriel FERNANDEZ if (!pdev->dev.of_node) {
131062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "no DT data present\n");
132062589b1SGabriel FERNANDEZ return -EINVAL;
133062589b1SGabriel FERNANDEZ }
134062589b1SGabriel FERNANDEZ
135062589b1SGabriel FERNANDEZ keypad_data = devm_kzalloc(&pdev->dev, sizeof(*keypad_data),
136062589b1SGabriel FERNANDEZ GFP_KERNEL);
137062589b1SGabriel FERNANDEZ if (!keypad_data)
138062589b1SGabriel FERNANDEZ return -ENOMEM;
139062589b1SGabriel FERNANDEZ
140062589b1SGabriel FERNANDEZ input_dev = devm_input_allocate_device(&pdev->dev);
141062589b1SGabriel FERNANDEZ if (!input_dev) {
142062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "failed to allocate the input device\n");
143062589b1SGabriel FERNANDEZ return -ENOMEM;
144062589b1SGabriel FERNANDEZ }
145062589b1SGabriel FERNANDEZ
146062589b1SGabriel FERNANDEZ input_dev->name = pdev->name;
147062589b1SGabriel FERNANDEZ input_dev->phys = "keyscan-keys/input0";
148062589b1SGabriel FERNANDEZ input_dev->dev.parent = &pdev->dev;
149062589b1SGabriel FERNANDEZ input_dev->open = keyscan_open;
150062589b1SGabriel FERNANDEZ input_dev->close = keyscan_close;
151062589b1SGabriel FERNANDEZ
152062589b1SGabriel FERNANDEZ input_dev->id.bustype = BUS_HOST;
153062589b1SGabriel FERNANDEZ
1542439d37eSGabriel Fernandez keypad_data->input_dev = input_dev;
1552439d37eSGabriel Fernandez
156062589b1SGabriel FERNANDEZ error = keypad_matrix_key_parse_dt(keypad_data);
157062589b1SGabriel FERNANDEZ if (error)
158062589b1SGabriel FERNANDEZ return error;
159062589b1SGabriel FERNANDEZ
160062589b1SGabriel FERNANDEZ error = matrix_keypad_build_keymap(NULL, NULL,
161062589b1SGabriel FERNANDEZ keypad_data->n_rows,
162062589b1SGabriel FERNANDEZ keypad_data->n_cols,
163062589b1SGabriel FERNANDEZ NULL, input_dev);
164062589b1SGabriel FERNANDEZ if (error) {
165062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "failed to build keymap\n");
166062589b1SGabriel FERNANDEZ return error;
167062589b1SGabriel FERNANDEZ }
168062589b1SGabriel FERNANDEZ
169062589b1SGabriel FERNANDEZ input_set_drvdata(input_dev, keypad_data);
170062589b1SGabriel FERNANDEZ
171babc94daSye xingchen keypad_data->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
172062589b1SGabriel FERNANDEZ if (IS_ERR(keypad_data->base))
173062589b1SGabriel FERNANDEZ return PTR_ERR(keypad_data->base);
174062589b1SGabriel FERNANDEZ
175062589b1SGabriel FERNANDEZ keypad_data->clk = devm_clk_get(&pdev->dev, NULL);
176062589b1SGabriel FERNANDEZ if (IS_ERR(keypad_data->clk)) {
177062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "cannot get clock\n");
178062589b1SGabriel FERNANDEZ return PTR_ERR(keypad_data->clk);
179062589b1SGabriel FERNANDEZ }
180062589b1SGabriel FERNANDEZ
181062589b1SGabriel FERNANDEZ error = clk_enable(keypad_data->clk);
182062589b1SGabriel FERNANDEZ if (error) {
183062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "failed to enable clock\n");
184062589b1SGabriel FERNANDEZ return error;
185062589b1SGabriel FERNANDEZ }
186062589b1SGabriel FERNANDEZ
187062589b1SGabriel FERNANDEZ keyscan_stop(keypad_data);
188062589b1SGabriel FERNANDEZ
189062589b1SGabriel FERNANDEZ keypad_data->irq = platform_get_irq(pdev, 0);
1900bec8b7eSStephen Boyd if (keypad_data->irq < 0)
191062589b1SGabriel FERNANDEZ return -EINVAL;
192062589b1SGabriel FERNANDEZ
193062589b1SGabriel FERNANDEZ error = devm_request_irq(&pdev->dev, keypad_data->irq, keyscan_isr, 0,
194062589b1SGabriel FERNANDEZ pdev->name, keypad_data);
195062589b1SGabriel FERNANDEZ if (error) {
196062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "failed to request IRQ\n");
197062589b1SGabriel FERNANDEZ return error;
198062589b1SGabriel FERNANDEZ }
199062589b1SGabriel FERNANDEZ
200062589b1SGabriel FERNANDEZ error = input_register_device(input_dev);
201062589b1SGabriel FERNANDEZ if (error) {
202062589b1SGabriel FERNANDEZ dev_err(&pdev->dev, "failed to register input device\n");
203062589b1SGabriel FERNANDEZ return error;
204062589b1SGabriel FERNANDEZ }
205062589b1SGabriel FERNANDEZ
206062589b1SGabriel FERNANDEZ platform_set_drvdata(pdev, keypad_data);
207062589b1SGabriel FERNANDEZ
208062589b1SGabriel FERNANDEZ device_set_wakeup_capable(&pdev->dev, 1);
209062589b1SGabriel FERNANDEZ
210062589b1SGabriel FERNANDEZ return 0;
211062589b1SGabriel FERNANDEZ }
212062589b1SGabriel FERNANDEZ
keyscan_suspend(struct device * dev)213062589b1SGabriel FERNANDEZ static int keyscan_suspend(struct device *dev)
214062589b1SGabriel FERNANDEZ {
215062589b1SGabriel FERNANDEZ struct platform_device *pdev = to_platform_device(dev);
216062589b1SGabriel FERNANDEZ struct st_keyscan *keypad = platform_get_drvdata(pdev);
217062589b1SGabriel FERNANDEZ struct input_dev *input = keypad->input_dev;
218062589b1SGabriel FERNANDEZ
219062589b1SGabriel FERNANDEZ mutex_lock(&input->mutex);
220062589b1SGabriel FERNANDEZ
221062589b1SGabriel FERNANDEZ if (device_may_wakeup(dev))
222062589b1SGabriel FERNANDEZ enable_irq_wake(keypad->irq);
223d69f0a43SAndrzej Pietrasiewicz else if (input_device_enabled(input))
224062589b1SGabriel FERNANDEZ keyscan_stop(keypad);
225062589b1SGabriel FERNANDEZ
226062589b1SGabriel FERNANDEZ mutex_unlock(&input->mutex);
227062589b1SGabriel FERNANDEZ return 0;
228062589b1SGabriel FERNANDEZ }
229062589b1SGabriel FERNANDEZ
keyscan_resume(struct device * dev)230062589b1SGabriel FERNANDEZ static int keyscan_resume(struct device *dev)
231062589b1SGabriel FERNANDEZ {
232062589b1SGabriel FERNANDEZ struct platform_device *pdev = to_platform_device(dev);
233062589b1SGabriel FERNANDEZ struct st_keyscan *keypad = platform_get_drvdata(pdev);
234062589b1SGabriel FERNANDEZ struct input_dev *input = keypad->input_dev;
235062589b1SGabriel FERNANDEZ int retval = 0;
236062589b1SGabriel FERNANDEZ
237062589b1SGabriel FERNANDEZ mutex_lock(&input->mutex);
238062589b1SGabriel FERNANDEZ
239062589b1SGabriel FERNANDEZ if (device_may_wakeup(dev))
240062589b1SGabriel FERNANDEZ disable_irq_wake(keypad->irq);
241d69f0a43SAndrzej Pietrasiewicz else if (input_device_enabled(input))
242062589b1SGabriel FERNANDEZ retval = keyscan_start(keypad);
243062589b1SGabriel FERNANDEZ
244062589b1SGabriel FERNANDEZ mutex_unlock(&input->mutex);
245062589b1SGabriel FERNANDEZ return retval;
246062589b1SGabriel FERNANDEZ }
247062589b1SGabriel FERNANDEZ
248a473a763SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops,
249a473a763SJonathan Cameron keyscan_suspend, keyscan_resume);
250062589b1SGabriel FERNANDEZ
251062589b1SGabriel FERNANDEZ static const struct of_device_id keyscan_of_match[] = {
252062589b1SGabriel FERNANDEZ { .compatible = "st,sti-keyscan" },
253062589b1SGabriel FERNANDEZ { },
254062589b1SGabriel FERNANDEZ };
255062589b1SGabriel FERNANDEZ MODULE_DEVICE_TABLE(of, keyscan_of_match);
256062589b1SGabriel FERNANDEZ
257062589b1SGabriel FERNANDEZ static struct platform_driver keyscan_device_driver = {
258062589b1SGabriel FERNANDEZ .probe = keyscan_probe,
259062589b1SGabriel FERNANDEZ .driver = {
260062589b1SGabriel FERNANDEZ .name = "st-keyscan",
261a473a763SJonathan Cameron .pm = pm_sleep_ptr(&keyscan_dev_pm_ops),
262*bb5e4f3eSKrzysztof Kozlowski .of_match_table = keyscan_of_match,
263062589b1SGabriel FERNANDEZ }
264062589b1SGabriel FERNANDEZ };
265062589b1SGabriel FERNANDEZ
266062589b1SGabriel FERNANDEZ module_platform_driver(keyscan_device_driver);
267062589b1SGabriel FERNANDEZ
268062589b1SGabriel FERNANDEZ MODULE_AUTHOR("Stuart Menefy <stuart.menefy@st.com>");
269062589b1SGabriel FERNANDEZ MODULE_DESCRIPTION("STMicroelectronics keyscan device driver");
270062589b1SGabriel FERNANDEZ MODULE_LICENSE("GPL");
271