blob: c7ca97f44bfb0f106edd549e9edb0ed6eb4d0221 [file] [log] [blame]
Rajeev Kumarbc95df72010-11-19 12:41:19 -08001/*
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>
Viresh Kumar829c4f92012-05-10 22:37:16 -070022#include <linux/of.h>
Rajeev Kumarbc95df72010-11-19 12:41:19 -080023#include <linux/platform_device.h>
24#include <linux/pm_wakeup.h>
25#include <linux/slab.h>
26#include <linux/types.h>
Arnd Bergmann6884b682012-08-24 15:22:23 +020027#include <linux/platform_data/keyboard-spear.h>
Rajeev Kumarbc95df72010-11-19 12:41:19 -080028
29/* Keyboard Registers */
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -070030#define MODE_CTL_REG 0x00
Shiraz Hashime99191f2012-07-07 18:00:36 -070031#define STATUS_REG 0x0C
32#define DATA_REG 0x10
Rajeev Kumarbc95df72010-11-19 12:41:19 -080033#define INTR_MASK 0x54
34
35/* Register Values */
Dmitry Torokhov19328112012-05-10 22:37:08 -070036#define NUM_ROWS 16
37#define NUM_COLS 16
Shiraz Hashim98e4d4d2012-07-07 18:01:07 -070038#define MODE_CTL_PCLK_FREQ_SHIFT 9
39#define MODE_CTL_PCLK_FREQ_MSK 0x7F
Dmitry Torokhov19328112012-05-10 22:37:08 -070040
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -070041#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
Rajeev Kumarbc95df72010-11-19 12:41:19 -080055
56struct spear_kbd {
57 struct input_dev *input;
58 struct resource *res;
59 void __iomem *io_base;
60 struct clk *clk;
61 unsigned int irq;
Rajeev Kumarf8354c62012-02-24 00:51:40 -080062 unsigned int mode;
Rajeev Kumarbc95df72010-11-19 12:41:19 -080063 unsigned short last_key;
Dmitry Torokhov19328112012-05-10 22:37:08 -070064 unsigned short keycodes[NUM_ROWS * NUM_COLS];
Viresh Kumar829c4f92012-05-10 22:37:16 -070065 bool rep;
Shiraz Hashim8314f532012-07-13 00:11:10 -070066 unsigned int suspended_rate;
67 u32 mode_ctl_reg;
Rajeev Kumarbc95df72010-11-19 12:41:19 -080068};
69
70static 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;
Shiraz Hashime99191f2012-07-07 18:00:36 -070075 u32 sts, val;
Rajeev Kumarbc95df72010-11-19 12:41:19 -080076
Shiraz Hashime99191f2012-07-07 18:00:36 -070077 sts = readl_relaxed(kbd->io_base + STATUS_REG);
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -070078 if (!(sts & STATUS_DATA_AVAIL))
Rajeev Kumarbc95df72010-11-19 12:41:19 -080079 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 */
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -070087 val = readl_relaxed(kbd->io_base + DATA_REG) &
88 (DATA_ROW_MASK | DATA_COLUMN_MASK);
Rajeev Kumarbc95df72010-11-19 12:41:19 -080089 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 */
Shiraz Hashime99191f2012-07-07 18:00:36 -070098 writel_relaxed(0, kbd->io_base + STATUS_REG);
Rajeev Kumarbc95df72010-11-19 12:41:19 -080099
100 return IRQ_HANDLED;
101}
102
103static int spear_kbd_open(struct input_dev *dev)
104{
105 struct spear_kbd *kbd = input_get_drvdata(dev);
106 int error;
Shiraz Hashime99191f2012-07-07 18:00:36 -0700107 u32 val;
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800108
109 kbd->last_key = KEY_RESERVED;
110
111 error = clk_enable(kbd->clk);
112 if (error)
113 return error;
114
Shiraz Hashim98e4d4d2012-07-07 18:01:07 -0700115 /* 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
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800119 /* program keyboard */
Shiraz Hashim98e4d4d2012-07-07 18:01:07 -0700120 val = MODE_CTL_SCAN_RATE_80 | MODE_CTL_KEYBOARD | val |
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -0700121 (kbd->mode << MODE_CTL_KEYNUM_SHIFT);
122 writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
Shiraz Hashime99191f2012-07-07 18:00:36 -0700123 writel_relaxed(1, kbd->io_base + STATUS_REG);
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800124
125 /* start key scan */
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -0700126 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);
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800129
130 return 0;
131}
132
133static void spear_kbd_close(struct input_dev *dev)
134{
135 struct spear_kbd *kbd = input_get_drvdata(dev);
Shiraz Hashime99191f2012-07-07 18:00:36 -0700136 u32 val;
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800137
138 /* stop key scan */
Shiraz Hashimf6f2efa2012-07-07 18:00:54 -0700139 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);
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800142
143 clk_disable(kbd->clk);
144
145 kbd->last_key = KEY_RESERVED;
146}
147
Viresh Kumar829c4f92012-05-10 22:37:16 -0700148#ifdef CONFIG_OF
149static int __devinit spear_kbd_parse_dt(struct platform_device *pdev,
150 struct spear_kbd *kbd)
151{
152 struct device_node *np = pdev->dev.of_node;
153 int error;
Shiraz Hashim8314f532012-07-13 00:11:10 -0700154 u32 val, suspended_rate;
Viresh Kumar829c4f92012-05-10 22:37:16 -0700155
156 if (!np) {
157 dev_err(&pdev->dev, "Missing DT data\n");
158 return -EINVAL;
159 }
160
161 if (of_property_read_bool(np, "autorepeat"))
162 kbd->rep = true;
163
Shiraz Hashim8314f532012-07-13 00:11:10 -0700164 if (of_property_read_u32(np, "suspended_rate", &suspended_rate))
165 kbd->suspended_rate = suspended_rate;
166
Viresh Kumar829c4f92012-05-10 22:37:16 -0700167 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;
171 }
172
173 kbd->mode = val;
174 return 0;
175}
176#else
177static inline int spear_kbd_parse_dt(struct platform_device *pdev,
178 struct spear_kbd *kbd)
179{
180 return -ENOSYS;
181}
182#endif
183
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800184static int __devinit spear_kbd_probe(struct platform_device *pdev)
185{
Viresh Kumar829c4f92012-05-10 22:37:16 -0700186 struct kbd_platform_data *pdata = dev_get_platdata(&pdev->dev);
187 const struct matrix_keymap_data *keymap = pdata ? pdata->keymap : NULL;
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800188 struct spear_kbd *kbd;
189 struct input_dev *input_dev;
190 struct resource *res;
191 int irq;
192 int error;
193
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800194 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
206 kbd = kzalloc(sizeof(*kbd), GFP_KERNEL);
207 input_dev = input_allocate_device();
208 if (!kbd || !input_dev) {
209 dev_err(&pdev->dev, "out of memory\n");
210 error = -ENOMEM;
211 goto err_free_mem;
212 }
213
214 kbd->input = input_dev;
215 kbd->irq = irq;
Viresh Kumar829c4f92012-05-10 22:37:16 -0700216
217 if (!pdata) {
218 error = spear_kbd_parse_dt(pdev, kbd);
219 if (error)
220 goto err_free_mem;
221 } else {
222 kbd->mode = pdata->mode;
223 kbd->rep = pdata->rep;
Shiraz Hashim8314f532012-07-13 00:11:10 -0700224 kbd->suspended_rate = pdata->suspended_rate;
Viresh Kumar829c4f92012-05-10 22:37:16 -0700225 }
Rajeev Kumarf8354c62012-02-24 00:51:40 -0800226
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800227 kbd->res = request_mem_region(res->start, resource_size(res),
228 pdev->name);
229 if (!kbd->res) {
230 dev_err(&pdev->dev, "keyboard region already claimed\n");
231 error = -EBUSY;
232 goto err_free_mem;
233 }
234
235 kbd->io_base = ioremap(res->start, resource_size(res));
236 if (!kbd->io_base) {
237 dev_err(&pdev->dev, "ioremap failed for kbd_region\n");
238 error = -ENOMEM;
239 goto err_release_mem_region;
240 }
241
242 kbd->clk = clk_get(&pdev->dev, NULL);
243 if (IS_ERR(kbd->clk)) {
244 error = PTR_ERR(kbd->clk);
245 goto err_iounmap;
246 }
247
248 input_dev->name = "Spear Keyboard";
249 input_dev->phys = "keyboard/input0";
250 input_dev->dev.parent = &pdev->dev;
251 input_dev->id.bustype = BUS_HOST;
252 input_dev->id.vendor = 0x0001;
253 input_dev->id.product = 0x0001;
254 input_dev->id.version = 0x0100;
255 input_dev->open = spear_kbd_open;
256 input_dev->close = spear_kbd_close;
257
Dmitry Torokhov19328112012-05-10 22:37:08 -0700258 error = matrix_keypad_build_keymap(keymap, NULL, NUM_ROWS, NUM_COLS,
259 kbd->keycodes, input_dev);
260 if (error) {
261 dev_err(&pdev->dev, "Failed to build keymap\n");
262 goto err_put_clk;
263 }
264
Viresh Kumar829c4f92012-05-10 22:37:16 -0700265 if (kbd->rep)
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800266 __set_bit(EV_REP, input_dev->evbit);
267 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
268
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800269 input_set_drvdata(input_dev, kbd);
270
271 error = request_irq(irq, spear_kbd_interrupt, 0, "keyboard", kbd);
272 if (error) {
273 dev_err(&pdev->dev, "request_irq fail\n");
274 goto err_put_clk;
275 }
276
277 error = input_register_device(input_dev);
278 if (error) {
279 dev_err(&pdev->dev, "Unable to register keyboard device\n");
280 goto err_free_irq;
281 }
282
283 device_init_wakeup(&pdev->dev, 1);
284 platform_set_drvdata(pdev, kbd);
285
286 return 0;
287
288err_free_irq:
289 free_irq(kbd->irq, kbd);
290err_put_clk:
291 clk_put(kbd->clk);
292err_iounmap:
293 iounmap(kbd->io_base);
294err_release_mem_region:
295 release_mem_region(res->start, resource_size(res));
296err_free_mem:
297 input_free_device(input_dev);
298 kfree(kbd);
299
300 return error;
301}
302
303static int __devexit spear_kbd_remove(struct platform_device *pdev)
304{
305 struct spear_kbd *kbd = platform_get_drvdata(pdev);
306
307 free_irq(kbd->irq, kbd);
308 input_unregister_device(kbd->input);
309 clk_put(kbd->clk);
310 iounmap(kbd->io_base);
311 release_mem_region(kbd->res->start, resource_size(kbd->res));
312 kfree(kbd);
313
Vipul Kumar Samar5bdea832012-07-07 18:00:10 -0700314 device_init_wakeup(&pdev->dev, 0);
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800315 platform_set_drvdata(pdev, NULL);
316
317 return 0;
318}
319
320#ifdef CONFIG_PM
321static int spear_kbd_suspend(struct device *dev)
322{
323 struct platform_device *pdev = to_platform_device(dev);
324 struct spear_kbd *kbd = platform_get_drvdata(pdev);
325 struct input_dev *input_dev = kbd->input;
Shiraz Hashim8314f532012-07-13 00:11:10 -0700326 unsigned int rate = 0, mode_ctl_reg, val;
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800327
328 mutex_lock(&input_dev->mutex);
329
Shiraz Hashim8314f532012-07-13 00:11:10 -0700330 /* explicitly enable clock as we may program device */
331 clk_enable(kbd->clk);
332
333 mode_ctl_reg = readl_relaxed(kbd->io_base + MODE_CTL_REG);
334
Shiraz Hashim53fe62852012-07-13 00:11:10 -0700335 if (device_may_wakeup(&pdev->dev)) {
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800336 enable_irq_wake(kbd->irq);
Shiraz Hashim8314f532012-07-13 00:11:10 -0700337
338 /*
339 * reprogram the keyboard operating frequency as on some
340 * platform it may change during system suspended
341 */
342 if (kbd->suspended_rate)
343 rate = kbd->suspended_rate / 1000000 - 1;
344 else
345 rate = clk_get_rate(kbd->clk) / 1000000 - 1;
346
347 val = mode_ctl_reg &
348 ~(MODE_CTL_PCLK_FREQ_MSK << MODE_CTL_PCLK_FREQ_SHIFT);
349 val |= (rate & MODE_CTL_PCLK_FREQ_MSK)
350 << MODE_CTL_PCLK_FREQ_SHIFT;
351 writel_relaxed(val, kbd->io_base + MODE_CTL_REG);
352
Shiraz Hashim53fe62852012-07-13 00:11:10 -0700353 } else {
Shiraz Hashim8314f532012-07-13 00:11:10 -0700354 if (input_dev->users) {
355 writel_relaxed(mode_ctl_reg & ~MODE_CTL_START_SCAN,
356 kbd->io_base + MODE_CTL_REG);
Shiraz Hashim53fe62852012-07-13 00:11:10 -0700357 clk_disable(kbd->clk);
Shiraz Hashim8314f532012-07-13 00:11:10 -0700358 }
Shiraz Hashim53fe62852012-07-13 00:11:10 -0700359 }
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800360
Shiraz Hashim8314f532012-07-13 00:11:10 -0700361 /* store current configuration */
362 if (input_dev->users)
363 kbd->mode_ctl_reg = mode_ctl_reg;
364
365 /* restore previous clk state */
366 clk_disable(kbd->clk);
367
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800368 mutex_unlock(&input_dev->mutex);
369
370 return 0;
371}
372
373static int spear_kbd_resume(struct device *dev)
374{
375 struct platform_device *pdev = to_platform_device(dev);
376 struct spear_kbd *kbd = platform_get_drvdata(pdev);
377 struct input_dev *input_dev = kbd->input;
378
379 mutex_lock(&input_dev->mutex);
380
Shiraz Hashim53fe62852012-07-13 00:11:10 -0700381 if (device_may_wakeup(&pdev->dev)) {
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800382 disable_irq_wake(kbd->irq);
Shiraz Hashim53fe62852012-07-13 00:11:10 -0700383 } else {
384 if (input_dev->users)
385 clk_enable(kbd->clk);
386 }
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800387
Shiraz Hashim8314f532012-07-13 00:11:10 -0700388 /* restore current configuration */
389 if (input_dev->users)
390 writel_relaxed(kbd->mode_ctl_reg, kbd->io_base + MODE_CTL_REG);
391
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800392 mutex_unlock(&input_dev->mutex);
393
394 return 0;
395}
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800396#endif
397
Viresh Kumarf79e30a2012-02-24 00:51:37 -0800398static SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops, spear_kbd_suspend, spear_kbd_resume);
399
Viresh Kumar829c4f92012-05-10 22:37:16 -0700400#ifdef CONFIG_OF
401static const struct of_device_id spear_kbd_id_table[] = {
402 { .compatible = "st,spear300-kbd" },
403 {}
404};
405MODULE_DEVICE_TABLE(of, spear_kbd_id_table);
406#endif
407
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800408static struct platform_driver spear_kbd_driver = {
409 .probe = spear_kbd_probe,
410 .remove = __devexit_p(spear_kbd_remove),
411 .driver = {
412 .name = "keyboard",
413 .owner = THIS_MODULE,
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800414 .pm = &spear_kbd_pm_ops,
Viresh Kumar829c4f92012-05-10 22:37:16 -0700415 .of_match_table = of_match_ptr(spear_kbd_id_table),
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800416 },
417};
JJ Ding5146c842011-11-29 11:08:39 -0800418module_platform_driver(spear_kbd_driver);
Rajeev Kumarbc95df72010-11-19 12:41:19 -0800419
420MODULE_AUTHOR("Rajeev Kumar");
421MODULE_DESCRIPTION("SPEAr Keyboard Driver");
422MODULE_LICENSE("GPL");