xref: /linux/drivers/input/keyboard/spear-keyboard.c (revision 3e51108c72e8adbcf3180ed40527a2a9d2d0123b)
1 /*
2  * SPEAr Keyboard Driver
3  * Based on omap-keypad driver
4  *
5  * Copyright (C) 2010 ST Microelectronics
6  * Rajeev Kumar <rajeevkumar.linux@gmail.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2. This program is licensed "as is" without any
10  * warranty of any kind, whether express or implied.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/input.h>
17 #include <linux/io.h>
18 #include <linux/irq.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/platform_data/keyboard-spear.h>
26 
27 /* Keyboard Registers */
28 #define MODE_CTL_REG	0x00
29 #define STATUS_REG	0x0C
30 #define DATA_REG	0x10
31 #define INTR_MASK	0x54
32 
33 /* Register Values */
34 #define NUM_ROWS	16
35 #define NUM_COLS	16
36 #define MODE_CTL_PCLK_FREQ_SHIFT	9
37 #define MODE_CTL_PCLK_FREQ_MSK		0x7F
38 
39 #define MODE_CTL_KEYBOARD	(0x2 << 0)
40 #define MODE_CTL_SCAN_RATE_10	(0x0 << 2)
41 #define MODE_CTL_SCAN_RATE_20	(0x1 << 2)
42 #define MODE_CTL_SCAN_RATE_40	(0x2 << 2)
43 #define MODE_CTL_SCAN_RATE_80	(0x3 << 2)
44 #define MODE_CTL_KEYNUM_SHIFT	6
45 #define MODE_CTL_START_SCAN	(0x1 << 8)
46 
47 #define STATUS_DATA_AVAIL	(0x1 << 1)
48 
49 #define DATA_ROW_MASK		0xF0
50 #define DATA_COLUMN_MASK	0x0F
51 
52 #define ROW_SHIFT		4
53 
54 struct spear_kbd {
55 	struct input_dev *input;
56 	void __iomem *io_base;
57 	struct clk *clk;
58 	unsigned int irq;
59 	unsigned int mode;
60 	unsigned int suspended_rate;
61 	unsigned short last_key;
62 	unsigned short keycodes[NUM_ROWS * NUM_COLS];
63 	bool rep;
64 	bool irq_wake_enabled;
65 	u32 mode_ctl_reg;
66 };
67 
spear_kbd_interrupt(int irq,void * dev_id)68 static irqreturn_t spear_kbd_interrupt(int irq, void *dev_id)
69 {
70 	struct spear_kbd *kbd = dev_id;
71 	struct input_dev *input = kbd->input;
72 	unsigned int key;
73 	u32 sts, val;
74 
75 	sts = readl_relaxed(kbd->io_base + STATUS_REG);
76 	if (!(sts & STATUS_DATA_AVAIL))
77 		return IRQ_NONE;
78 
79 	if (kbd->last_key != KEY_RESERVED) {
80 		input_report_key(input, kbd->last_key, 0);
81 		kbd->last_key = KEY_RESERVED;
82 	}
83 
84 	/* following reads active (row, col) pair */
85 	val = readl_relaxed(kbd->io_base + DATA_REG) &
86 		(DATA_ROW_MASK | DATA_COLUMN_MASK);
87 	key = kbd->keycodes[val];
88 
89 	input_event(input, EV_MSC, MSC_SCAN, val);
90 	input_report_key(input, key, 1);
91 	input_sync(input);
92 
93 	kbd->last_key = key;
94 
95 	/* clear interrupt */
96 	writel_relaxed(0, kbd->io_base + STATUS_REG);
97 
98 	return IRQ_HANDLED;
99 }
100 
spear_kbd_open(struct input_dev * dev)101 static int spear_kbd_open(struct input_dev *dev)
102 {
103 	struct spear_kbd *kbd = input_get_drvdata(dev);
104 	int error;
105 	u32 val;
106 
107 	kbd->last_key = KEY_RESERVED;
108 
109 	error = clk_enable(kbd->clk);
110 	if (error)
111 		return error;
112 
113 	/* keyboard rate to be programmed is input clock (in MHz) - 1 */
114 	val = clk_get_rate(kbd->clk) / 1000000 - 1;
115 	val = (val & MODE_CTL_PCLK_FREQ_MSK) << MODE_CTL_PCLK_FREQ_SHIFT;
116 
117 	/* program keyboard */
118 	val = MODE_CTL_SCAN_RATE_80 | MODE_CTL_KEYBOARD | val |
119 		(kbd->mode << MODE_CTL_KEYNUM_SHIFT);
120 	writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
121 	writel_relaxed(1, kbd->io_base + STATUS_REG);
122 
123 	/* start key scan */
124 	val = readl_relaxed(kbd->io_base + MODE_CTL_REG);
125 	val |= MODE_CTL_START_SCAN;
126 	writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
127 
128 	return 0;
129 }
130 
spear_kbd_close(struct input_dev * dev)131 static void spear_kbd_close(struct input_dev *dev)
132 {
133 	struct spear_kbd *kbd = input_get_drvdata(dev);
134 	u32 val;
135 
136 	/* stop key scan */
137 	val = readl_relaxed(kbd->io_base + MODE_CTL_REG);
138 	val &= ~MODE_CTL_START_SCAN;
139 	writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
140 
141 	clk_disable(kbd->clk);
142 
143 	kbd->last_key = KEY_RESERVED;
144 }
145 
146 #ifdef CONFIG_OF
spear_kbd_parse_dt(struct platform_device * pdev,struct spear_kbd * kbd)147 static int spear_kbd_parse_dt(struct platform_device *pdev,
148                                         struct spear_kbd *kbd)
149 {
150 	struct device_node *np = pdev->dev.of_node;
151 	int error;
152 	u32 val, suspended_rate;
153 
154 	if (!np) {
155 		dev_err(&pdev->dev, "Missing DT data\n");
156 		return -EINVAL;
157 	}
158 
159 	if (of_property_read_bool(np, "autorepeat"))
160 		kbd->rep = true;
161 
162 	if (of_property_read_u32(np, "suspended_rate", &suspended_rate))
163 		kbd->suspended_rate = suspended_rate;
164 
165 	error = of_property_read_u32(np, "st,mode", &val);
166 	if (error) {
167 		dev_err(&pdev->dev, "DT: Invalid or missing mode\n");
168 		return error;
169 	}
170 
171 	kbd->mode = val;
172 	return 0;
173 }
174 #else
spear_kbd_parse_dt(struct platform_device * pdev,struct spear_kbd * kbd)175 static inline int spear_kbd_parse_dt(struct platform_device *pdev,
176 				     struct spear_kbd *kbd)
177 {
178 	return -ENOSYS;
179 }
180 #endif
181 
spear_kbd_probe(struct platform_device * pdev)182 static int spear_kbd_probe(struct platform_device *pdev)
183 {
184 	struct kbd_platform_data *pdata = dev_get_platdata(&pdev->dev);
185 	const struct matrix_keymap_data *keymap = pdata ? pdata->keymap : NULL;
186 	struct spear_kbd *kbd;
187 	struct input_dev *input_dev;
188 	int irq;
189 	int error;
190 
191 	irq = platform_get_irq(pdev, 0);
192 	if (irq < 0)
193 		return irq;
194 
195 	kbd = devm_kzalloc(&pdev->dev, sizeof(*kbd), GFP_KERNEL);
196 	if (!kbd) {
197 		dev_err(&pdev->dev, "not enough memory for driver data\n");
198 		return -ENOMEM;
199 	}
200 
201 	input_dev = devm_input_allocate_device(&pdev->dev);
202 	if (!input_dev) {
203 		dev_err(&pdev->dev, "unable to allocate input device\n");
204 		return -ENOMEM;
205 	}
206 
207 	kbd->input = input_dev;
208 	kbd->irq = irq;
209 
210 	if (!pdata) {
211 		error = spear_kbd_parse_dt(pdev, kbd);
212 		if (error)
213 			return error;
214 	} else {
215 		kbd->mode = pdata->mode;
216 		kbd->rep = pdata->rep;
217 		kbd->suspended_rate = pdata->suspended_rate;
218 	}
219 
220 	kbd->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
221 	if (IS_ERR(kbd->io_base))
222 		return PTR_ERR(kbd->io_base);
223 
224 	kbd->clk = devm_clk_get_prepared(&pdev->dev, NULL);
225 	if (IS_ERR(kbd->clk))
226 		return PTR_ERR(kbd->clk);
227 
228 	input_dev->name = "Spear Keyboard";
229 	input_dev->phys = "keyboard/input0";
230 	input_dev->id.bustype = BUS_HOST;
231 	input_dev->id.vendor = 0x0001;
232 	input_dev->id.product = 0x0001;
233 	input_dev->id.version = 0x0100;
234 	input_dev->open = spear_kbd_open;
235 	input_dev->close = spear_kbd_close;
236 
237 	error = matrix_keypad_build_keymap(keymap, NULL, NUM_ROWS, NUM_COLS,
238 					   kbd->keycodes, input_dev);
239 	if (error) {
240 		dev_err(&pdev->dev, "Failed to build keymap\n");
241 		return error;
242 	}
243 
244 	if (kbd->rep)
245 		__set_bit(EV_REP, input_dev->evbit);
246 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
247 
248 	input_set_drvdata(input_dev, kbd);
249 
250 	error = devm_request_irq(&pdev->dev, irq, spear_kbd_interrupt, 0,
251 			"keyboard", kbd);
252 	if (error) {
253 		dev_err(&pdev->dev, "request_irq failed\n");
254 		return error;
255 	}
256 
257 	error = input_register_device(input_dev);
258 	if (error) {
259 		dev_err(&pdev->dev, "Unable to register keyboard device\n");
260 		return error;
261 	}
262 
263 	device_init_wakeup(&pdev->dev, 1);
264 	platform_set_drvdata(pdev, kbd);
265 
266 	return 0;
267 }
268 
spear_kbd_suspend(struct device * dev)269 static int spear_kbd_suspend(struct device *dev)
270 {
271 	struct platform_device *pdev = to_platform_device(dev);
272 	struct spear_kbd *kbd = platform_get_drvdata(pdev);
273 	struct input_dev *input_dev = kbd->input;
274 	unsigned int rate = 0, mode_ctl_reg, val;
275 
276 	guard(mutex)(&input_dev->mutex);
277 
278 	/* explicitly enable clock as we may program device */
279 	clk_enable(kbd->clk);
280 
281 	mode_ctl_reg = readl_relaxed(kbd->io_base + MODE_CTL_REG);
282 
283 	if (device_may_wakeup(&pdev->dev)) {
284 		if (!enable_irq_wake(kbd->irq))
285 			kbd->irq_wake_enabled = true;
286 
287 		/*
288 		 * reprogram the keyboard operating frequency as on some
289 		 * platform it may change during system suspended
290 		 */
291 		if (kbd->suspended_rate)
292 			rate = kbd->suspended_rate / 1000000 - 1;
293 		else
294 			rate = clk_get_rate(kbd->clk) / 1000000 - 1;
295 
296 		val = mode_ctl_reg &
297 			~(MODE_CTL_PCLK_FREQ_MSK << MODE_CTL_PCLK_FREQ_SHIFT);
298 		val |= (rate & MODE_CTL_PCLK_FREQ_MSK)
299 			<< MODE_CTL_PCLK_FREQ_SHIFT;
300 		writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
301 
302 	} else {
303 		if (input_device_enabled(input_dev)) {
304 			writel_relaxed(mode_ctl_reg & ~MODE_CTL_START_SCAN,
305 					kbd->io_base + MODE_CTL_REG);
306 			clk_disable(kbd->clk);
307 		}
308 	}
309 
310 	/* store current configuration */
311 	if (input_device_enabled(input_dev))
312 		kbd->mode_ctl_reg = mode_ctl_reg;
313 
314 	/* restore previous clk state */
315 	clk_disable(kbd->clk);
316 
317 	return 0;
318 }
319 
spear_kbd_resume(struct device * dev)320 static int spear_kbd_resume(struct device *dev)
321 {
322 	struct platform_device *pdev = to_platform_device(dev);
323 	struct spear_kbd *kbd = platform_get_drvdata(pdev);
324 	struct input_dev *input_dev = kbd->input;
325 
326 	guard(mutex)(&input_dev->mutex);
327 
328 	if (device_may_wakeup(&pdev->dev)) {
329 		if (kbd->irq_wake_enabled) {
330 			kbd->irq_wake_enabled = false;
331 			disable_irq_wake(kbd->irq);
332 		}
333 	} else {
334 		if (input_device_enabled(input_dev))
335 			clk_enable(kbd->clk);
336 	}
337 
338 	/* restore current configuration */
339 	if (input_device_enabled(input_dev))
340 		writel_relaxed(kbd->mode_ctl_reg, kbd->io_base + MODE_CTL_REG);
341 
342 	return 0;
343 }
344 
345 static DEFINE_SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops,
346 				spear_kbd_suspend, spear_kbd_resume);
347 
348 #ifdef CONFIG_OF
349 static const struct of_device_id spear_kbd_id_table[] = {
350 	{ .compatible = "st,spear300-kbd" },
351 	{}
352 };
353 MODULE_DEVICE_TABLE(of, spear_kbd_id_table);
354 #endif
355 
356 static struct platform_driver spear_kbd_driver = {
357 	.probe		= spear_kbd_probe,
358 	.driver		= {
359 		.name	= "keyboard",
360 		.pm	= pm_sleep_ptr(&spear_kbd_pm_ops),
361 		.of_match_table = of_match_ptr(spear_kbd_id_table),
362 	},
363 };
364 module_platform_driver(spear_kbd_driver);
365 
366 MODULE_AUTHOR("Rajeev Kumar");
367 MODULE_DESCRIPTION("SPEAr Keyboard Driver");
368 MODULE_LICENSE("GPL");
369