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;
109 static int rk_remotectl_get_irkeybd_count(struct platform_device *pdev)
111 struct device_node *node = pdev->dev.of_node;
112 struct device_node *child_node;
117 for_each_child_of_node(node, child_node) {
118 if(of_property_read_u32(child_node, "rockchip,usercode",
120 DBG("get keybd num error.\n");
125 DBG("get keybd num = 0x%x.\n", boardnum);
130 static int rk_remotectl_parse_ir_keys(struct platform_device *pdev)
132 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
133 struct device_node *node = pdev->dev.of_node;
134 struct device_node *child_node;
141 for_each_child_of_node(node, child_node) {
142 if(of_property_read_u32(child_node, "rockchip,usercode",
143 &remotectl_button[boardnum].usercode)) {
144 dev_err(&pdev->dev, "Missing usercode property in the DTS.\n");
148 DBG("remotectl_button[0].usercode=0x%x\n",
149 remotectl_button[boardnum].usercode);
150 of_get_property(child_node, "rockchip,key_table", &len);
152 DBG("len=0x%x\n",len);
153 remotectl_button[boardnum].nbuttons = len/2;
154 if(of_property_read_u32_array(child_node, "rockchip,key_table",
155 (u32 *)remotectl_button[boardnum].key_table, len)) {
156 dev_err(&pdev->dev, "Missing key_table property in the DTS.\n");
160 for (loop=0; loop<(len/2); loop++) {
161 DBG("board[%d].scanCode[%d]=0x%x\n", boardnum, loop,
162 remotectl_button[boardnum].key_table[loop].scancode);
163 DBG("board[%d].keyCode[%d]=%d\n", boardnum, loop,
164 remotectl_button[boardnum].key_table[loop].keycode);
167 if (boardnum > ddata->maxkeybdnum)
170 DBG("keybdNum=0x%x\n",boardnum);
176 static void rk_pwm_remotectl_do_something(unsigned long data)
178 struct rkxx_remotectl_drvdata *ddata;
180 ddata = (struct rkxx_remotectl_drvdata *)data;
181 switch (ddata->state) {
187 mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(140));
188 if ((RK_PWM_TIME_PRE_MIN < ddata->period) &&
189 (ddata->period < RK_PWM_TIME_PRE_MAX)) {
192 ddata->state = RMC_USERCODE;
194 ddata->state = RMC_PRELOAD;
199 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
200 (ddata->period < RK_PWM_TIME_BIT1_MAX))
201 ddata->scandata |= (0x01 << ddata->count);
203 if (ddata->count == 0x10) {
204 DBG_CODE("USERCODE=0x%x\n", ddata->scandata);
205 if (remotectl_keybd_num_lookup(ddata)) {
206 ddata->state = RMC_GETDATA;
210 if (rk_remote_print_code){
211 ddata->state = RMC_GETDATA;
215 ddata->state = RMC_PRELOAD;
221 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
222 (ddata->period < RK_PWM_TIME_BIT1_MAX))
223 ddata->scandata |= (0x01<<ddata->count);
225 if (ddata->count < 0x10)
227 DBG_CODE("RMC_GETDATA=%x\n", (ddata->scandata>>8));
228 if ((ddata->scandata&0x0ff) ==
229 ((~ddata->scandata >> 8) & 0x0ff)) {
230 if (remotectl_keycode_lookup(ddata)) {
232 input_event(ddata->input, EV_KEY,
234 input_sync(ddata->input);
235 ddata->state = RMC_SEQUENCE;
237 ddata->state = RMC_PRELOAD;
240 ddata->state = RMC_PRELOAD;
245 DBG("S=%d\n", ddata->period);
246 if ((RK_PWM_TIME_RPT_MIN < ddata->period) &&
247 (ddata->period < RK_PWM_TIME_RPT_MAX)) {
249 mod_timer(&ddata->timer, jiffies
250 + msecs_to_jiffies(130));
251 } else if ((RK_PWM_TIME_SEQ1_MIN < ddata->period) &&
252 (ddata->period < RK_PWM_TIME_SEQ1_MAX)) {
254 mod_timer(&ddata->timer, jiffies
255 + msecs_to_jiffies(130));
256 } else if ((RK_PWM_TIME_SEQ2_MIN < ddata->period) &&
257 (ddata->period < RK_PWM_TIME_SEQ2_MAX)) {
259 mod_timer(&ddata->timer, jiffies
260 + msecs_to_jiffies(130));
263 input_event(ddata->input, EV_KEY,
265 input_sync(ddata->input);
266 ddata->state = RMC_PRELOAD;
276 static void rk_pwm_remotectl_timer(unsigned long _data)
278 struct rkxx_remotectl_drvdata *ddata;
280 ddata = (struct rkxx_remotectl_drvdata *)_data;
281 if (ddata->press != ddata->pre_press) {
282 ddata->pre_press = 0;
284 input_event(ddata->input, EV_KEY, ddata->keycode, 0);
285 input_sync(ddata->input);
287 ddata->state = RMC_PRELOAD;
291 static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id)
293 struct rkxx_remotectl_drvdata *ddata;
296 ddata = (struct rkxx_remotectl_drvdata *)dev_id;
297 switch (ddata->remote_pwm_id) {
299 val = readl_relaxed(ddata->base + PWM0_REG_INTSTS);
300 if (val & PWM_CH0_INT) {
301 if ((val & PWM_CH0_POL) == 0) {
302 val = readl_relaxed(ddata->base + PWM_REG_HPR);
304 tasklet_hi_schedule(&ddata->remote_tasklet);
305 DBG("hpr=0x%x\n", val);
307 val = readl_relaxed(ddata->base + PWM_REG_LPR);
308 DBG("lpr=0x%x\n", val);
310 writel_relaxed(PWM_CH0_INT, ddata->base + PWM0_REG_INTSTS);
311 if (ddata->state == RMC_PRELOAD)
312 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
318 val = readl_relaxed(ddata->base + PWM1_REG_INTSTS);
319 if (val & PWM_CH1_INT) {
320 if ((val & PWM_CH1_POL) == 0) {
321 val = readl_relaxed(ddata->base + PWM_REG_HPR);
323 tasklet_hi_schedule(&ddata->remote_tasklet);
324 DBG("hpr=0x%x\n", val);
326 val = readl_relaxed(ddata->base + PWM_REG_LPR);
327 DBG("lpr=0x%x\n", val);
329 writel_relaxed(PWM_CH1_INT, ddata->base + PWM1_REG_INTSTS);
330 if (ddata->state == RMC_PRELOAD)
331 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
337 val = readl_relaxed(ddata->base + PWM2_REG_INTSTS);
338 if (val & PWM_CH2_INT) {
339 if ((val & PWM_CH2_POL) == 0) {
340 val = readl_relaxed(ddata->base + PWM_REG_HPR);
342 tasklet_hi_schedule(&ddata->remote_tasklet);
343 DBG("hpr=0x%x\n", val);
345 val = readl_relaxed(ddata->base + PWM_REG_LPR);
346 DBG("lpr=0x%x\n", val);
348 writel_relaxed(PWM_CH2_INT, ddata->base + PWM2_REG_INTSTS);
349 if (ddata->state == RMC_PRELOAD)
350 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
356 val = readl_relaxed(ddata->base + PWM3_REG_INTSTS);
357 if (val & PWM_CH3_INT) {
358 if ((val & PWM_CH3_POL) == 0) {
359 val = readl_relaxed(ddata->base + PWM_REG_HPR);
361 tasklet_hi_schedule(&ddata->remote_tasklet);
362 DBG("hpr=0x%x\n", val);
364 val = readl_relaxed(ddata->base + PWM_REG_LPR);
365 DBG("lpr=0x%x\n", val);
367 writel_relaxed(PWM_CH3_INT, ddata->base + PWM3_REG_INTSTS);
368 if (ddata->state == RMC_PRELOAD)
369 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
380 static int rk_pwm_remotectl_hw_init(struct rkxx_remotectl_drvdata *ddata)
384 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
385 val = (val & 0xFFFFFFFE) | PWM_DISABLE;
386 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
387 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
388 val = (val & 0xFFFFFFF9) | PWM_MODE_CAPTURE;
389 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
390 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
391 val = (val & 0xFF008DFF) | 0x00646200;
392 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
393 switch (ddata->remote_pwm_id) {
395 val = readl_relaxed(ddata->base + PWM0_REG_INT_EN);
396 val = (val & 0xFFFFFFFE) | PWM_CH0_INT_ENABLE;
397 writel_relaxed(val, ddata->base + PWM0_REG_INT_EN);
401 val = readl_relaxed(ddata->base + PWM1_REG_INT_EN);
402 val = (val & 0xFFFFFFFD) | PWM_CH1_INT_ENABLE;
403 writel_relaxed(val, ddata->base + PWM1_REG_INT_EN);
407 val = readl_relaxed(ddata->base + PWM2_REG_INT_EN);
408 val = (val & 0xFFFFFFFB) | PWM_CH2_INT_ENABLE;
409 writel_relaxed(val, ddata->base + PWM2_REG_INT_EN);
413 val = readl_relaxed(ddata->base + PWM3_REG_INT_EN);
414 val = (val & 0xFFFFFFF7) | PWM_CH3_INT_ENABLE;
415 writel_relaxed(val, ddata->base + PWM3_REG_INT_EN);
421 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
422 val = (val & 0xFFFFFFFE) | PWM_ENABLE;
423 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
429 static int rk_pwm_probe(struct platform_device *pdev)
431 struct rkxx_remotectl_drvdata *ddata;
432 struct device_node *np = pdev->dev.of_node;
434 struct input_dev *input;
436 struct cpumask cpumask;
444 pr_err(".. rk pwm remotectl v1.1 init\n");
445 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
447 dev_err(&pdev->dev, "no memory resources defined\n");
450 ddata = devm_kzalloc(&pdev->dev, sizeof(struct rkxx_remotectl_drvdata),
453 dev_err(&pdev->dev, "failed to allocate memory\n");
456 ddata->state = RMC_PRELOAD;
457 ddata->base = devm_ioremap_resource(&pdev->dev, r);
458 if (IS_ERR(ddata->base))
459 return PTR_ERR(ddata->base);
460 clk = devm_clk_get(&pdev->dev, "pclk_pwm");
463 platform_set_drvdata(pdev, ddata);
464 num = rk_remotectl_get_irkeybd_count(pdev);
466 pr_err("remotectl: no ir keyboard add in dts!!\n");
469 ddata->maxkeybdnum = num;
470 remotectl_button = kmalloc(
471 num*sizeof(struct rkxx_remotectl_button),
473 if (!remotectl_button) {
474 pr_err("failed to malloc remote button memory\n");
477 input = input_allocate_device();
478 input->name = pdev->name;
479 input->phys = "gpio-keys/remotectl";
480 input->dev.parent = &pdev->dev;
481 input->id.bustype = BUS_HOST;
482 input->id.vendor = 0x0001;
483 input->id.product = 0x0001;
484 input->id.version = 0x0100;
485 ddata->input = input;
486 ddata->input = input;
487 wake_lock_init(&ddata->remotectl_wake_lock,
488 WAKE_LOCK_SUSPEND, "rk29_pwm_remote");
489 ret = clk_prepare_enable(clk);
492 irq = platform_get_irq(pdev, 0);
494 dev_err(&pdev->dev, "cannot find IRQ\n");
499 of_property_read_u32(np, "remote_pwm_id", &pwm_id);
500 ddata->remote_pwm_id = pwm_id;
501 DBG("remotectl: remote pwm id=0x%x\n", pwm_id);
502 of_property_read_u32(np, "handle_cpu_id", &cpu_id);
503 ddata->handle_cpu_id = cpu_id;
504 DBG("remotectl: handle cpu id=0x%x\n", cpu_id);
505 rk_remotectl_parse_ir_keys(pdev);
506 tasklet_init(&ddata->remote_tasklet, rk_pwm_remotectl_do_something,
507 (unsigned long)ddata);
508 for (j = 0; j < num; j++) {
509 DBG("remotectl probe j = 0x%x\n", j);
510 for (i = 0; i < remotectl_button[j].nbuttons; i++) {
511 unsigned int type = EV_KEY;
513 input_set_capability(input, type, remotectl_button[j].
514 key_table[i].keycode);
517 ret = input_register_device(input);
519 pr_err("remotectl: register input device err, ret: %d\n", ret);
520 input_set_capability(input, EV_KEY, KEY_WAKEUP);
521 device_init_wakeup(&pdev->dev, 1);
522 enable_irq_wake(irq);
523 setup_timer(&ddata->timer, rk_pwm_remotectl_timer,
524 (unsigned long)ddata);
525 //mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(1000));
526 cpumask_clear(&cpumask);
527 cpumask_set_cpu(cpu_id, &cpumask);
528 irq_set_affinity(irq, &cpumask);
529 ret = devm_request_irq(&pdev->dev, irq, rockchip_pwm_irq,
530 IRQF_NO_SUSPEND, "rk_pwm_irq", ddata);
532 dev_err(&pdev->dev, "cannot claim IRQ %d\n", irq);
535 rk_pwm_remotectl_hw_init(ddata);
539 static int rk_pwm_remove(struct platform_device *pdev)
545 static int remotectl_suspend(struct device *dev)
548 struct cpumask cpumask;
549 struct platform_device *pdev = to_platform_device(dev);
550 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
552 cpumask_clear(&cpumask);
553 cpumask_set_cpu(cpu, &cpumask);
554 irq_set_affinity(ddata->irq, &cpumask);
559 static int remotectl_resume(struct device *dev)
561 struct cpumask cpumask;
562 struct platform_device *pdev = to_platform_device(dev);
563 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
565 cpumask_clear(&cpumask);
566 cpumask_set_cpu(ddata->handle_cpu_id, &cpumask);
567 irq_set_affinity(ddata->irq, &cpumask);
571 static const struct dev_pm_ops remotectl_pm_ops = {
572 .suspend_late = remotectl_suspend,
573 .resume_early = remotectl_resume,
577 static const struct of_device_id rk_pwm_of_match[] = {
578 { .compatible = "rockchip,remotectl-pwm"},
582 MODULE_DEVICE_TABLE(of, rk_pwm_of_match);
584 static struct platform_driver rk_pwm_driver = {
586 .name = "remotectl-pwm",
587 .of_match_table = rk_pwm_of_match,
589 .pm = &remotectl_pm_ops,
592 .probe = rk_pwm_probe,
593 .remove = rk_pwm_remove,
596 module_platform_driver(rk_pwm_driver);
598 MODULE_LICENSE("GPL");