3 #include <linux/module.h>
5 #include <linux/platform_device.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/platform_device.h>
10 #include <linux/input.h>
11 #include <linux/workqueue.h>
12 #include <linux/wakelock.h>
13 #include <linux/slab.h>
14 #include "rockchip_pwm_remotectl.h"
18 /*sys/module/rk_pwm_remotectl/parameters,
19 modify code_print to change the value*/
21 static int rk_remote_print_code;
22 module_param_named(code_print, rk_remote_print_code, int, 0644);
23 #define DBG_CODE(args...) \
25 if (rk_remote_print_code) { \
30 static int rk_remote_pwm_dbg_level;
31 module_param_named(dbg_level, rk_remote_pwm_dbg_level, int, 0644);
32 #define DBG(args...) \
34 if (rk_remote_pwm_dbg_level) { \
40 struct rkxx_remote_key_table {
45 struct rkxx_remotectl_button {
48 struct rkxx_remote_key_table key_table[MAX_NUM_KEYS];
51 struct rkxx_remotectl_drvdata {
68 struct input_dev *input;
69 struct timer_list timer;
70 struct tasklet_struct remote_tasklet;
71 struct wake_lock remotectl_wake_lock;
74 static struct rkxx_remotectl_button *remotectl_button;
77 static int remotectl_keybd_num_lookup(struct rkxx_remotectl_drvdata *ddata)
82 num = ddata->maxkeybdnum;
83 for (i = 0; i < num; i++) {
84 if (remotectl_button[i].usercode == (ddata->scandata&0xFFFF)) {
93 static int remotectl_keycode_lookup(struct rkxx_remotectl_drvdata *ddata)
96 unsigned char keydata = (unsigned char)((ddata->scandata >> 8) & 0xff);
98 for (i = 0; i < remotectl_button[ddata->keynum].nbuttons; i++) {
99 if (remotectl_button[ddata->keynum].key_table[i].scancode ==
102 remotectl_button[ddata->keynum].key_table[i].keycode;
110 static int rk_remotectl_parse_ir_keys(struct platform_device *pdev)
112 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
113 struct device_node *node = pdev->dev.of_node;
114 struct device_node *child_node;
121 for_each_child_of_node(node, child_node) {
122 if(of_property_read_u32(child_node, "rockchip,usercode",
123 &remotectl_button[boardnum].usercode)) {
124 dev_err(&pdev->dev, "Missing usercode property in the DTS.\n");
128 DBG("remotectl_button[0].usercode=0x%x\n",
129 remotectl_button[boardnum].usercode);
130 of_get_property(child_node, "rockchip,key_table", &len);
132 DBG("len=0x%x\n",len);
133 remotectl_button[boardnum].nbuttons = len/2;
134 if(of_property_read_u32_array(child_node, "rockchip,key_table",
135 (u32 *)remotectl_button[boardnum].key_table, len)) {
136 dev_err(&pdev->dev, "Missing key_table property in the DTS.\n");
140 for (loop=0; loop<(len/2); loop++) {
141 DBG("board[%d].scanCode[%d]=0x%x\n", boardnum, loop,
142 remotectl_button[boardnum].key_table[loop].scancode);
143 DBG("board[%d].keyCode[%d]=%d\n", boardnum, loop,
144 remotectl_button[boardnum].key_table[loop].keycode);
147 if (boardnum > ddata->maxkeybdnum)
150 DBG("keybdNum=0x%x\n",boardnum);
156 static void rk_pwm_remotectl_do_something(unsigned long data)
158 struct rkxx_remotectl_drvdata *ddata;
160 ddata = (struct rkxx_remotectl_drvdata *)data;
161 switch (ddata->state) {
167 mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(130));
168 if ((RK_PWM_TIME_PRE_MIN < ddata->period) &&
169 (ddata->period < RK_PWM_TIME_PRE_MAX)) {
172 ddata->state = RMC_USERCODE;
174 ddata->state = RMC_PRELOAD;
179 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
180 (ddata->period < RK_PWM_TIME_BIT1_MAX))
181 ddata->scandata |= (0x01 << ddata->count);
183 if (ddata->count == 0x10) {
184 DBG_CODE("USERCODE=0x%x\n", ddata->scandata);
185 if (remotectl_keybd_num_lookup(ddata)) {
186 ddata->state = RMC_GETDATA;
190 ddata->state = RMC_PRELOAD;
196 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
197 (ddata->period < RK_PWM_TIME_BIT1_MAX))
198 ddata->scandata |= (0x01<<ddata->count);
200 if (ddata->count < 0x10)
202 DBG_CODE("RMC_GETDATA=%x\n", (ddata->scandata>>8));
203 if ((ddata->scandata&0x0ff) ==
204 ((~ddata->scandata >> 8) & 0x0ff)) {
205 if (remotectl_keycode_lookup(ddata)) {
207 input_event(ddata->input, EV_KEY,
209 input_sync(ddata->input);
210 ddata->state = RMC_SEQUENCE;
212 ddata->state = RMC_PRELOAD;
215 ddata->state = RMC_PRELOAD;
220 DBG("S=%d\n", ddata->period);
221 if ((RK_PWM_TIME_RPT_MIN < ddata->period) &&
222 (ddata->period < RK_PWM_TIME_RPT_MAX)) {
224 mod_timer(&ddata->timer, jiffies
225 + msecs_to_jiffies(130));
226 } else if ((RK_PWM_TIME_SEQ1_MIN < ddata->period) &&
227 (ddata->period < RK_PWM_TIME_SEQ1_MAX)) {
229 mod_timer(&ddata->timer, jiffies
230 + msecs_to_jiffies(130));
231 } else if ((RK_PWM_TIME_SEQ2_MIN < ddata->period) &&
232 (ddata->period < RK_PWM_TIME_SEQ2_MAX)) {
234 mod_timer(&ddata->timer, jiffies
235 + msecs_to_jiffies(130));
238 input_event(ddata->input, EV_KEY,
240 input_sync(ddata->input);
241 ddata->state = RMC_PRELOAD;
251 static void rk_pwm_remotectl_timer(unsigned long _data)
253 struct rkxx_remotectl_drvdata *ddata;
255 ddata = (struct rkxx_remotectl_drvdata *)_data;
256 if (ddata->press != ddata->pre_press) {
257 ddata->pre_press = 0;
259 input_event(ddata->input, EV_KEY, ddata->keycode, 0);
260 input_sync(ddata->input);
262 ddata->state = RMC_PRELOAD;
266 static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id)
268 struct rkxx_remotectl_drvdata *ddata;
271 ddata = (struct rkxx_remotectl_drvdata *)dev_id;
272 switch (ddata->remote_pwm_id) {
274 val = readl_relaxed(ddata->base + PWM0_REG_INTSTS);
275 if (val & PWM_CH0_INT) {
276 if ((val & PWM_CH0_POL) == 0) {
277 val = readl_relaxed(ddata->base + PWM_REG_HPR);
279 tasklet_hi_schedule(&ddata->remote_tasklet);
280 DBG("hpr=0x%x\n", val);
282 val = readl_relaxed(ddata->base + PWM_REG_LPR);
283 DBG("lpr=0x%x\n", val);
285 writel_relaxed(PWM_CH0_INT, ddata->base + PWM0_REG_INTSTS);
286 if (ddata->state == RMC_PRELOAD)
287 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
293 val = readl_relaxed(ddata->base + PWM1_REG_INTSTS);
294 if (val & PWM_CH1_INT) {
295 if ((val & PWM_CH1_POL) == 0) {
296 val = readl_relaxed(ddata->base + PWM_REG_HPR);
298 tasklet_hi_schedule(&ddata->remote_tasklet);
299 DBG("hpr=0x%x\n", val);
301 val = readl_relaxed(ddata->base + PWM_REG_LPR);
302 DBG("lpr=0x%x\n", val);
304 writel_relaxed(PWM_CH1_INT, ddata->base + PWM1_REG_INTSTS);
305 if (ddata->state == RMC_PRELOAD)
306 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
312 val = readl_relaxed(ddata->base + PWM2_REG_INTSTS);
313 if (val & PWM_CH2_INT) {
314 if ((val & PWM_CH2_POL) == 0) {
315 val = readl_relaxed(ddata->base + PWM_REG_HPR);
317 tasklet_hi_schedule(&ddata->remote_tasklet);
318 DBG("hpr=0x%x\n", val);
320 val = readl_relaxed(ddata->base + PWM_REG_LPR);
321 DBG("lpr=0x%x\n", val);
323 writel_relaxed(PWM_CH2_INT, ddata->base + PWM2_REG_INTSTS);
324 if (ddata->state == RMC_PRELOAD)
325 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
331 val = readl_relaxed(ddata->base + PWM3_REG_INTSTS);
332 if (val & PWM_CH3_INT) {
333 if ((val & PWM_CH3_POL) == 0) {
334 val = readl_relaxed(ddata->base + PWM_REG_HPR);
336 tasklet_hi_schedule(&ddata->remote_tasklet);
337 DBG("hpr=0x%x\n", val);
339 val = readl_relaxed(ddata->base + PWM_REG_LPR);
340 DBG("lpr=0x%x\n", val);
342 writel_relaxed(PWM_CH3_INT, ddata->base + PWM3_REG_INTSTS);
343 if (ddata->state == RMC_PRELOAD)
344 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
355 static int rk_pwm_remotectl_hw_init(struct rkxx_remotectl_drvdata *ddata)
359 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
360 val = (val & 0xFFFFFFFE) | PWM_DISABLE;
361 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
362 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
363 val = (val & 0xFFFFFFF9) | PWM_MODE_CAPTURE;
364 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
365 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
366 val = (val & 0xFF008DFF) | 0x00646200;
367 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
368 switch (ddata->remote_pwm_id) {
370 val = readl_relaxed(ddata->base + PWM0_REG_INT_EN);
371 val = (val & 0xFFFFFFFE) | PWM_CH0_INT_ENABLE;
372 writel_relaxed(val, ddata->base + PWM0_REG_INT_EN);
376 val = readl_relaxed(ddata->base + PWM1_REG_INT_EN);
377 val = (val & 0xFFFFFFFD) | PWM_CH1_INT_ENABLE;
378 writel_relaxed(val, ddata->base + PWM1_REG_INT_EN);
382 val = readl_relaxed(ddata->base + PWM2_REG_INT_EN);
383 val = (val & 0xFFFFFFFB) | PWM_CH2_INT_ENABLE;
384 writel_relaxed(val, ddata->base + PWM2_REG_INT_EN);
388 val = readl_relaxed(ddata->base + PWM3_REG_INT_EN);
389 val = (val & 0xFFFFFFF7) | PWM_CH3_INT_ENABLE;
390 writel_relaxed(val, ddata->base + PWM3_REG_INT_EN);
396 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
397 val = (val & 0xFFFFFFFE) | PWM_ENABLE;
398 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
404 static int rk_pwm_probe(struct platform_device *pdev)
406 struct rkxx_remotectl_drvdata *ddata;
407 struct device_node *np = pdev->dev.of_node;
408 struct device_node *child_node;
410 struct input_dev *input;
412 struct cpumask cpumask;
420 pr_err(".. rk pwm remotectl v1.1 init\n");
421 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
423 dev_err(&pdev->dev, "no memory resources defined\n");
426 ddata = devm_kzalloc(&pdev->dev, sizeof(struct rkxx_remotectl_drvdata),
429 dev_err(&pdev->dev, "failed to allocate memory\n");
432 ddata->state = RMC_PRELOAD;
433 ddata->base = devm_ioremap_resource(&pdev->dev, r);
434 if (IS_ERR(ddata->base))
435 return PTR_ERR(ddata->base);
436 clk = devm_clk_get(&pdev->dev, "pclk_pwm");
439 platform_set_drvdata(pdev, ddata);
441 for_each_child_of_node(np, child_node){
442 if(of_device_is_compatible(child_node, "rockchip,ir_key"))
446 pr_err("remotectl: no ir keyboard add in dts!!\n");
449 ddata->maxkeybdnum = num;
450 remotectl_button = kmalloc(
451 num*sizeof(struct rkxx_remotectl_button),
453 if (!remotectl_button) {
454 pr_err("failed to malloc remote button memory\n");
457 input = input_allocate_device();
458 input->name = pdev->name;
459 input->phys = "gpio-keys/remotectl";
460 input->dev.parent = &pdev->dev;
461 input->id.bustype = BUS_HOST;
462 input->id.vendor = 0x0001;
463 input->id.product = 0x0001;
464 input->id.version = 0x0100;
465 ddata->input = input;
466 ddata->input = input;
467 wake_lock_init(&ddata->remotectl_wake_lock,
468 WAKE_LOCK_SUSPEND, "rk29_pwm_remote");
469 ret = clk_prepare_enable(clk);
472 irq = platform_get_irq(pdev, 0);
474 dev_err(&pdev->dev, "cannot find IRQ\n");
479 of_property_read_u32(np, "remote_pwm_id", &pwm_id);
480 ddata->remote_pwm_id = pwm_id;
481 DBG("remotectl: remote pwm id=0x%x\n", pwm_id);
482 of_property_read_u32(np, "handle_cpu_id", &cpu_id);
483 ddata->handle_cpu_id = cpu_id;
484 DBG("remotectl: handle cpu id=0x%x\n", cpu_id);
485 rk_remotectl_parse_ir_keys(pdev);
486 tasklet_init(&ddata->remote_tasklet, rk_pwm_remotectl_do_something,
487 (unsigned long)ddata);
488 for (j = 0; j < num; j++) {
489 DBG("remotectl probe j = 0x%x\n", j);
490 for (i = 0; i < remotectl_button[j].nbuttons; i++) {
491 unsigned int type = EV_KEY;
493 input_set_capability(input, type, remotectl_button[j].
494 key_table[i].keycode);
497 ret = input_register_device(input);
499 pr_err("remotectl: register input device err, ret: %d\n", ret);
500 input_set_capability(input, EV_KEY, KEY_WAKEUP);
501 device_init_wakeup(&pdev->dev, 1);
502 ret = devm_request_irq(&pdev->dev, irq, rockchip_pwm_irq,
503 0, "rk_pwm_irq", ddata);
505 dev_err(&pdev->dev, "cannot claim IRQ %d\n", irq);
508 enable_irq_wake(irq);
509 setup_timer(&ddata->timer, rk_pwm_remotectl_timer,
510 (unsigned long)ddata);
511 mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(1000));
512 cpumask_clear(&cpumask);
513 cpumask_set_cpu(cpu_id, &cpumask);
514 irq_set_affinity(irq, &cpumask);
515 rk_pwm_remotectl_hw_init(ddata);
519 static int rk_pwm_remove(struct platform_device *pdev)
525 static int remotectl_suspend(struct device *dev)
528 struct cpumask cpumask;
529 struct platform_device *pdev = to_platform_device(dev);
530 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
532 cpumask_clear(&cpumask);
533 cpumask_set_cpu(cpu, &cpumask);
534 irq_set_affinity(ddata->irq, &cpumask);
539 static int remotectl_resume(struct device *dev)
541 struct cpumask cpumask;
542 struct platform_device *pdev = to_platform_device(dev);
543 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
545 cpumask_clear(&cpumask);
546 cpumask_set_cpu(ddata->handle_cpu_id, &cpumask);
547 irq_set_affinity(ddata->irq, &cpumask);
551 static const struct dev_pm_ops remotectl_pm_ops = {
552 .suspend = remotectl_suspend,
553 .resume = remotectl_resume,
557 static const struct of_device_id rk_pwm_of_match[] = {
558 { .compatible = "rockchip,remotectl-pwm"},
562 MODULE_DEVICE_TABLE(of, rk_pwm_of_match);
564 static struct platform_driver rk_pwm_driver = {
566 .name = "remotectl-pwm",
567 .of_match_table = rk_pwm_of_match,
569 .pm = &remotectl_pm_ops,
572 .probe = rk_pwm_probe,
573 .remove = rk_pwm_remove,
576 module_platform_driver(rk_pwm_driver);
578 MODULE_LICENSE("GPL");