2 * SPEAr Keyboard Driver
3 * Based on omap-keypad driver
5 * Copyright (C) 2010 ST Microelectronics
6 * Rajeev Kumar<rajeev-dlh.kumar@st.com>
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.
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>
19 #include <linux/irq.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_wakeup.h>
25 #include <linux/slab.h>
26 #include <linux/types.h>
27 #include <linux/platform_data/keyboard-spear.h>
29 /* Keyboard Registers */
30 #define MODE_CTL_REG 0x00
31 #define STATUS_REG 0x0C
33 #define INTR_MASK 0x54
38 #define MODE_CTL_PCLK_FREQ_SHIFT 9
39 #define MODE_CTL_PCLK_FREQ_MSK 0x7F
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)
49 #define STATUS_DATA_AVAIL (0x1 << 1)
51 #define DATA_ROW_MASK 0xF0
52 #define DATA_COLUMN_MASK 0x0F
57 struct input_dev
*input
;
58 void __iomem
*io_base
;
62 unsigned int suspended_rate
;
63 unsigned short last_key
;
64 unsigned short keycodes
[NUM_ROWS
* NUM_COLS
];
66 bool irq_wake_enabled
;
70 static irqreturn_t
spear_kbd_interrupt(int irq
, void *dev_id
)
72 struct spear_kbd
*kbd
= dev_id
;
73 struct input_dev
*input
= kbd
->input
;
77 sts
= readl_relaxed(kbd
->io_base
+ STATUS_REG
);
78 if (!(sts
& STATUS_DATA_AVAIL
))
81 if (kbd
->last_key
!= KEY_RESERVED
) {
82 input_report_key(input
, kbd
->last_key
, 0);
83 kbd
->last_key
= KEY_RESERVED
;
86 /* following reads active (row, col) pair */
87 val
= readl_relaxed(kbd
->io_base
+ DATA_REG
) &
88 (DATA_ROW_MASK
| DATA_COLUMN_MASK
);
89 key
= kbd
->keycodes
[val
];
91 input_event(input
, EV_MSC
, MSC_SCAN
, val
);
92 input_report_key(input
, key
, 1);
98 writel_relaxed(0, kbd
->io_base
+ STATUS_REG
);
103 static int spear_kbd_open(struct input_dev
*dev
)
105 struct spear_kbd
*kbd
= input_get_drvdata(dev
);
109 kbd
->last_key
= KEY_RESERVED
;
111 error
= clk_enable(kbd
->clk
);
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
;
119 /* program keyboard */
120 val
= MODE_CTL_SCAN_RATE_80
| MODE_CTL_KEYBOARD
| val
|
121 (kbd
->mode
<< MODE_CTL_KEYNUM_SHIFT
);
122 writel_relaxed(val
, kbd
->io_base
+ MODE_CTL_REG
);
123 writel_relaxed(1, kbd
->io_base
+ STATUS_REG
);
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
);
133 static void spear_kbd_close(struct input_dev
*dev
)
135 struct spear_kbd
*kbd
= input_get_drvdata(dev
);
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
);
143 clk_disable(kbd
->clk
);
145 kbd
->last_key
= KEY_RESERVED
;
149 static int spear_kbd_parse_dt(struct platform_device
*pdev
,
150 struct spear_kbd
*kbd
)
152 struct device_node
*np
= pdev
->dev
.of_node
;
154 u32 val
, suspended_rate
;
157 dev_err(&pdev
->dev
, "Missing DT data\n");
161 if (of_property_read_bool(np
, "autorepeat"))
164 if (of_property_read_u32(np
, "suspended_rate", &suspended_rate
))
165 kbd
->suspended_rate
= suspended_rate
;
167 error
= of_property_read_u32(np
, "st,mode", &val
);
169 dev_err(&pdev
->dev
, "DT: Invalid or missing mode\n");
177 static inline int spear_kbd_parse_dt(struct platform_device
*pdev
,
178 struct spear_kbd
*kbd
)
184 static int spear_kbd_probe(struct platform_device
*pdev
)
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
;
194 irq
= platform_get_irq(pdev
, 0);
196 dev_err(&pdev
->dev
, "not able to get irq for the device\n");
200 kbd
= devm_kzalloc(&pdev
->dev
, sizeof(*kbd
), GFP_KERNEL
);
202 dev_err(&pdev
->dev
, "not enough memory for driver data\n");
206 input_dev
= devm_input_allocate_device(&pdev
->dev
);
208 dev_err(&pdev
->dev
, "unable to allocate input device\n");
212 kbd
->input
= input_dev
;
216 error
= spear_kbd_parse_dt(pdev
, kbd
);
220 kbd
->mode
= pdata
->mode
;
221 kbd
->rep
= pdata
->rep
;
222 kbd
->suspended_rate
= pdata
->suspended_rate
;
225 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
226 kbd
->io_base
= devm_ioremap_resource(&pdev
->dev
, res
);
227 if (IS_ERR(kbd
->io_base
))
228 return PTR_ERR(kbd
->io_base
);
230 kbd
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
231 if (IS_ERR(kbd
->clk
))
232 return PTR_ERR(kbd
->clk
);
234 input_dev
->name
= "Spear Keyboard";
235 input_dev
->phys
= "keyboard/input0";
236 input_dev
->id
.bustype
= BUS_HOST
;
237 input_dev
->id
.vendor
= 0x0001;
238 input_dev
->id
.product
= 0x0001;
239 input_dev
->id
.version
= 0x0100;
240 input_dev
->open
= spear_kbd_open
;
241 input_dev
->close
= spear_kbd_close
;
243 error
= matrix_keypad_build_keymap(keymap
, NULL
, NUM_ROWS
, NUM_COLS
,
244 kbd
->keycodes
, input_dev
);
246 dev_err(&pdev
->dev
, "Failed to build keymap\n");
251 __set_bit(EV_REP
, input_dev
->evbit
);
252 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
254 input_set_drvdata(input_dev
, kbd
);
256 error
= devm_request_irq(&pdev
->dev
, irq
, spear_kbd_interrupt
, 0,
259 dev_err(&pdev
->dev
, "request_irq failed\n");
263 error
= clk_prepare(kbd
->clk
);
267 error
= input_register_device(input_dev
);
269 dev_err(&pdev
->dev
, "Unable to register keyboard device\n");
270 clk_unprepare(kbd
->clk
);
274 device_init_wakeup(&pdev
->dev
, 1);
275 platform_set_drvdata(pdev
, kbd
);
280 static int spear_kbd_remove(struct platform_device
*pdev
)
282 struct spear_kbd
*kbd
= platform_get_drvdata(pdev
);
284 input_unregister_device(kbd
->input
);
285 clk_unprepare(kbd
->clk
);
287 device_init_wakeup(&pdev
->dev
, 0);
293 static int spear_kbd_suspend(struct device
*dev
)
295 struct platform_device
*pdev
= to_platform_device(dev
);
296 struct spear_kbd
*kbd
= platform_get_drvdata(pdev
);
297 struct input_dev
*input_dev
= kbd
->input
;
298 unsigned int rate
= 0, mode_ctl_reg
, val
;
300 mutex_lock(&input_dev
->mutex
);
302 /* explicitly enable clock as we may program device */
303 clk_enable(kbd
->clk
);
305 mode_ctl_reg
= readl_relaxed(kbd
->io_base
+ MODE_CTL_REG
);
307 if (device_may_wakeup(&pdev
->dev
)) {
308 if (!enable_irq_wake(kbd
->irq
))
309 kbd
->irq_wake_enabled
= true;
312 * reprogram the keyboard operating frequency as on some
313 * platform it may change during system suspended
315 if (kbd
->suspended_rate
)
316 rate
= kbd
->suspended_rate
/ 1000000 - 1;
318 rate
= clk_get_rate(kbd
->clk
) / 1000000 - 1;
321 ~(MODE_CTL_PCLK_FREQ_MSK
<< MODE_CTL_PCLK_FREQ_SHIFT
);
322 val
|= (rate
& MODE_CTL_PCLK_FREQ_MSK
)
323 << MODE_CTL_PCLK_FREQ_SHIFT
;
324 writel_relaxed(val
, kbd
->io_base
+ MODE_CTL_REG
);
327 if (input_dev
->users
) {
328 writel_relaxed(mode_ctl_reg
& ~MODE_CTL_START_SCAN
,
329 kbd
->io_base
+ MODE_CTL_REG
);
330 clk_disable(kbd
->clk
);
334 /* store current configuration */
335 if (input_dev
->users
)
336 kbd
->mode_ctl_reg
= mode_ctl_reg
;
338 /* restore previous clk state */
339 clk_disable(kbd
->clk
);
341 mutex_unlock(&input_dev
->mutex
);
346 static int spear_kbd_resume(struct device
*dev
)
348 struct platform_device
*pdev
= to_platform_device(dev
);
349 struct spear_kbd
*kbd
= platform_get_drvdata(pdev
);
350 struct input_dev
*input_dev
= kbd
->input
;
352 mutex_lock(&input_dev
->mutex
);
354 if (device_may_wakeup(&pdev
->dev
)) {
355 if (kbd
->irq_wake_enabled
) {
356 kbd
->irq_wake_enabled
= false;
357 disable_irq_wake(kbd
->irq
);
360 if (input_dev
->users
)
361 clk_enable(kbd
->clk
);
364 /* restore current configuration */
365 if (input_dev
->users
)
366 writel_relaxed(kbd
->mode_ctl_reg
, kbd
->io_base
+ MODE_CTL_REG
);
368 mutex_unlock(&input_dev
->mutex
);
374 static SIMPLE_DEV_PM_OPS(spear_kbd_pm_ops
, spear_kbd_suspend
, spear_kbd_resume
);
377 static const struct of_device_id spear_kbd_id_table
[] = {
378 { .compatible
= "st,spear300-kbd" },
381 MODULE_DEVICE_TABLE(of
, spear_kbd_id_table
);
384 static struct platform_driver spear_kbd_driver
= {
385 .probe
= spear_kbd_probe
,
386 .remove
= spear_kbd_remove
,
389 .owner
= THIS_MODULE
,
390 .pm
= &spear_kbd_pm_ops
,
391 .of_match_table
= of_match_ptr(spear_kbd_id_table
),
394 module_platform_driver(spear_kbd_driver
);
396 MODULE_AUTHOR("Rajeev Kumar");
397 MODULE_DESCRIPTION("SPEAr Keyboard Driver");
398 MODULE_LICENSE("GPL");