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)
193 error
= devm_request_irq(&pdev
->dev
, keypad_data
->irq
, keyscan_isr
, 0,
194 pdev
->name
, keypad_data
);
196 dev_err(&pdev
->dev
, "failed to request IRQ\n");
200 error
= input_register_device(input_dev
);
202 dev_err(&pdev
->dev
, "failed to register input device\n");
206 platform_set_drvdata(pdev
, keypad_data
);
208 device_set_wakeup_capable(&pdev
->dev
, 1);
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
->users
)
225 keyscan_stop(keypad
);
227 mutex_unlock(&input
->mutex
);
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
;
238 mutex_lock(&input
->mutex
);
240 if (device_may_wakeup(dev
))
241 disable_irq_wake(keypad
->irq
);
242 else if (input
->users
)
243 retval
= keyscan_start(keypad
);
245 mutex_unlock(&input
->mutex
);
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" },
256 MODULE_DEVICE_TABLE(of
, keyscan_of_match
);
258 static struct platform_driver keyscan_device_driver
= {
259 .probe
= keyscan_probe
,
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");