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