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 {
69 unsigned long temp_period;
71 struct input_dev *input;
72 struct timer_list timer;
73 struct tasklet_struct remote_tasklet;
74 struct wake_lock remotectl_wake_lock;
77 static struct rkxx_remotectl_button *remotectl_button;
79 static int remotectl_keybd_num_lookup(struct rkxx_remotectl_drvdata *ddata)
84 num = ddata->maxkeybdnum;
85 for (i = 0; i < num; i++) {
86 if (remotectl_button[i].usercode == (ddata->scandata&0xFFFF)) {
95 static int remotectl_keycode_lookup(struct rkxx_remotectl_drvdata *ddata)
98 unsigned char keydata = (unsigned char)((ddata->scandata >> 8) & 0xff);
100 for (i = 0; i < remotectl_button[ddata->keynum].nbuttons; i++) {
101 if (remotectl_button[ddata->keynum].key_table[i].scancode ==
104 remotectl_button[ddata->keynum].key_table[i].keycode;
111 static int rk_remotectl_get_irkeybd_count(struct platform_device *pdev)
113 struct device_node *node = pdev->dev.of_node;
114 struct device_node *child_node;
119 for_each_child_of_node(node, child_node) {
120 if(of_property_read_u32(child_node, "rockchip,usercode",
122 DBG("get keybd num error.\n");
127 DBG("get keybd num = 0x%x.\n", boardnum);
132 static int rk_remotectl_parse_ir_keys(struct platform_device *pdev)
134 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
135 struct device_node *node = pdev->dev.of_node;
136 struct device_node *child_node;
143 for_each_child_of_node(node, child_node) {
144 if(of_property_read_u32(child_node, "rockchip,usercode",
145 &remotectl_button[boardnum].usercode)) {
146 dev_err(&pdev->dev, "Missing usercode property in the DTS.\n");
150 DBG("remotectl_button[0].usercode=0x%x\n",
151 remotectl_button[boardnum].usercode);
152 of_get_property(child_node, "rockchip,key_table", &len);
154 DBG("len=0x%x\n",len);
155 remotectl_button[boardnum].nbuttons = len/2;
156 if(of_property_read_u32_array(child_node, "rockchip,key_table",
157 (u32 *)remotectl_button[boardnum].key_table, len)) {
158 dev_err(&pdev->dev, "Missing key_table property in the DTS.\n");
162 for (loop=0; loop<(len/2); loop++) {
163 DBG("board[%d].scanCode[%d]=0x%x\n", boardnum, loop,
164 remotectl_button[boardnum].key_table[loop].scancode);
165 DBG("board[%d].keyCode[%d]=%d\n", boardnum, loop,
166 remotectl_button[boardnum].key_table[loop].keycode);
169 if (boardnum > ddata->maxkeybdnum)
172 DBG("keybdNum=0x%x\n",boardnum);
178 static void rk_pwm_remotectl_do_something(unsigned long data)
180 struct rkxx_remotectl_drvdata *ddata;
182 ddata = (struct rkxx_remotectl_drvdata *)data;
183 switch (ddata->state) {
189 mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(140));
190 if ((RK_PWM_TIME_PRE_MIN < ddata->period) &&
191 (ddata->period < RK_PWM_TIME_PRE_MAX)) {
194 ddata->state = RMC_USERCODE;
196 ddata->state = RMC_PRELOAD;
201 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
202 (ddata->period < RK_PWM_TIME_BIT1_MAX))
203 ddata->scandata |= (0x01 << ddata->count);
205 if (ddata->count == 0x10) {
206 DBG_CODE("USERCODE=0x%x\n", ddata->scandata);
207 if (remotectl_keybd_num_lookup(ddata)) {
208 ddata->state = RMC_GETDATA;
212 if (rk_remote_print_code){
213 ddata->state = RMC_GETDATA;
217 ddata->state = RMC_PRELOAD;
223 if ((RK_PWM_TIME_BIT1_MIN < ddata->period) &&
224 (ddata->period < RK_PWM_TIME_BIT1_MAX))
225 ddata->scandata |= (0x01<<ddata->count);
227 if (ddata->count < 0x10)
229 DBG_CODE("RMC_GETDATA=%x\n", (ddata->scandata>>8));
230 if ((ddata->scandata&0x0ff) ==
231 ((~ddata->scandata >> 8) & 0x0ff)) {
232 if (remotectl_keycode_lookup(ddata)) {
234 input_event(ddata->input, EV_KEY,
236 input_sync(ddata->input);
237 ddata->state = RMC_SEQUENCE;
239 ddata->state = RMC_PRELOAD;
242 ddata->state = RMC_PRELOAD;
247 DBG("S=%ld\n", ddata->period);
248 if ((RK_PWM_TIME_RPT_MIN < ddata->period) &&
249 (ddata->period < RK_PWM_TIME_RPT_MAX)) {
251 mod_timer(&ddata->timer, jiffies
252 + msecs_to_jiffies(130));
253 } else if ((RK_PWM_TIME_SEQ1_MIN < ddata->period) &&
254 (ddata->period < RK_PWM_TIME_SEQ1_MAX)) {
256 mod_timer(&ddata->timer, jiffies
257 + msecs_to_jiffies(130));
258 } else if ((RK_PWM_TIME_SEQ2_MIN < ddata->period) &&
259 (ddata->period < RK_PWM_TIME_SEQ2_MAX)) {
261 mod_timer(&ddata->timer, jiffies
262 + msecs_to_jiffies(130));
265 input_event(ddata->input, EV_KEY,
267 input_sync(ddata->input);
268 ddata->state = RMC_PRELOAD;
278 static void rk_pwm_remotectl_timer(unsigned long _data)
280 struct rkxx_remotectl_drvdata *ddata;
282 ddata = (struct rkxx_remotectl_drvdata *)_data;
283 if (ddata->press != ddata->pre_press) {
284 ddata->pre_press = 0;
286 input_event(ddata->input, EV_KEY, ddata->keycode, 0);
287 input_sync(ddata->input);
289 ddata->state = RMC_PRELOAD;
293 static irqreturn_t rockchip_pwm_irq(int irq, void *dev_id)
295 struct rkxx_remotectl_drvdata *ddata = dev_id;
300 unsigned int id = ddata->remote_pwm_id;
304 val = readl_relaxed(ddata->base + PWM_REG_INTSTS(id));
305 if ((val & PWM_CH_INT(id)) == 0)
307 if ((val & PWM_CH_POL(id)) == 0) {
308 temp_hpr = readl_relaxed(ddata->base + PWM_REG_HPR);
309 DBG("hpr=%d\n", temp_hpr);
310 temp_lpr = readl_relaxed(ddata->base + PWM_REG_LPR);
311 DBG("lpr=%d\n", temp_lpr);
312 temp_period = ddata->pwm_freq_nstime * temp_lpr / 1000;
313 if (temp_period > RK_PWM_TIME_BIT0_MIN) {
314 ddata->period = ddata->temp_period
315 + ddata->pwm_freq_nstime * temp_hpr / 1000;
316 tasklet_hi_schedule(&ddata->remote_tasklet);
317 ddata->temp_period = 0;
318 DBG("period+ =%ld\n", ddata->period);
320 ddata->temp_period += ddata->pwm_freq_nstime
321 * (temp_hpr + temp_lpr) / 1000;
324 writel_relaxed(PWM_CH_INT(id), ddata->base + PWM_REG_INTSTS(id));
325 if (ddata->state == RMC_PRELOAD)
326 wake_lock_timeout(&ddata->remotectl_wake_lock, HZ);
332 static int rk_pwm_remotectl_hw_init(struct rkxx_remotectl_drvdata *ddata)
336 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
337 val = (val & 0xFFFFFFFE) | PWM_DISABLE;
338 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
339 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
340 val = (val & 0xFFFFFFF9) | PWM_MODE_CAPTURE;
341 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
342 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
343 val = (val & 0xFF008DFF) | 0x0006000;
344 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
345 switch (ddata->remote_pwm_id) {
347 val = readl_relaxed(ddata->base + PWM0_REG_INT_EN);
348 val = (val & 0xFFFFFFFE) | PWM_CH0_INT_ENABLE;
349 writel_relaxed(val, ddata->base + PWM0_REG_INT_EN);
353 val = readl_relaxed(ddata->base + PWM1_REG_INT_EN);
354 val = (val & 0xFFFFFFFD) | PWM_CH1_INT_ENABLE;
355 writel_relaxed(val, ddata->base + PWM1_REG_INT_EN);
359 val = readl_relaxed(ddata->base + PWM2_REG_INT_EN);
360 val = (val & 0xFFFFFFFB) | PWM_CH2_INT_ENABLE;
361 writel_relaxed(val, ddata->base + PWM2_REG_INT_EN);
365 val = readl_relaxed(ddata->base + PWM3_REG_INT_EN);
366 val = (val & 0xFFFFFFF7) | PWM_CH3_INT_ENABLE;
367 writel_relaxed(val, ddata->base + PWM3_REG_INT_EN);
373 val = readl_relaxed(ddata->base + PWM_REG_CTRL);
374 val = (val & 0xFFFFFFFE) | PWM_ENABLE;
375 writel_relaxed(val, ddata->base + PWM_REG_CTRL);
380 static int rk_pwm_probe(struct platform_device *pdev)
382 struct rkxx_remotectl_drvdata *ddata;
383 struct device_node *np = pdev->dev.of_node;
385 struct input_dev *input;
387 struct cpumask cpumask;
396 pr_err(".. rk pwm remotectl v1.1 init\n");
397 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
399 dev_err(&pdev->dev, "no memory resources defined\n");
402 ddata = devm_kzalloc(&pdev->dev, sizeof(struct rkxx_remotectl_drvdata),
405 dev_err(&pdev->dev, "failed to allocate memory\n");
408 ddata->state = RMC_PRELOAD;
409 ddata->temp_period = 0;
410 ddata->base = devm_ioremap_resource(&pdev->dev, r);
411 if (IS_ERR(ddata->base))
412 return PTR_ERR(ddata->base);
413 clk = devm_clk_get(&pdev->dev, NULL);
416 platform_set_drvdata(pdev, ddata);
417 num = rk_remotectl_get_irkeybd_count(pdev);
419 pr_err("remotectl: no ir keyboard add in dts!!\n");
422 ddata->maxkeybdnum = num;
423 remotectl_button = kmalloc(
424 num*sizeof(struct rkxx_remotectl_button),
426 if (!remotectl_button) {
427 pr_err("failed to malloc remote button memory\n");
430 input = input_allocate_device();
431 input->name = pdev->name;
432 input->phys = "gpio-keys/remotectl";
433 input->dev.parent = &pdev->dev;
434 input->id.bustype = BUS_HOST;
435 input->id.vendor = 0x0001;
436 input->id.product = 0x0001;
437 input->id.version = 0x0100;
438 ddata->input = input;
439 ddata->input = input;
440 wake_lock_init(&ddata->remotectl_wake_lock,
441 WAKE_LOCK_SUSPEND, "rk29_pwm_remote");
442 ret = clk_prepare_enable(clk);
445 irq = platform_get_irq(pdev, 0);
447 dev_err(&pdev->dev, "cannot find IRQ\n");
452 of_property_read_u32(np, "remote_pwm_id", &pwm_id);
453 ddata->remote_pwm_id = pwm_id;
454 DBG("remotectl: remote pwm id=0x%x\n", pwm_id);
455 of_property_read_u32(np, "handle_cpu_id", &cpu_id);
456 ddata->handle_cpu_id = cpu_id;
457 DBG("remotectl: handle cpu id=0x%x\n", cpu_id);
458 rk_remotectl_parse_ir_keys(pdev);
459 tasklet_init(&ddata->remote_tasklet, rk_pwm_remotectl_do_something,
460 (unsigned long)ddata);
461 for (j = 0; j < num; j++) {
462 DBG("remotectl probe j = 0x%x\n", j);
463 for (i = 0; i < remotectl_button[j].nbuttons; i++) {
464 unsigned int type = EV_KEY;
466 input_set_capability(input, type, remotectl_button[j].
467 key_table[i].keycode);
470 ret = input_register_device(input);
472 pr_err("remotectl: register input device err, ret: %d\n", ret);
473 input_set_capability(input, EV_KEY, KEY_WAKEUP);
474 device_init_wakeup(&pdev->dev, 1);
475 enable_irq_wake(irq);
476 setup_timer(&ddata->timer, rk_pwm_remotectl_timer,
477 (unsigned long)ddata);
478 //mod_timer(&ddata->timer, jiffies + msecs_to_jiffies(1000));
479 cpumask_clear(&cpumask);
480 cpumask_set_cpu(cpu_id, &cpumask);
481 irq_set_affinity(irq, &cpumask);
482 ret = devm_request_irq(&pdev->dev, irq, rockchip_pwm_irq,
483 IRQF_NO_SUSPEND, "rk_pwm_irq", ddata);
485 dev_err(&pdev->dev, "cannot claim IRQ %d\n", irq);
488 rk_pwm_remotectl_hw_init(ddata);
489 pwm_freq = clk_get_rate(clk) / 64;
490 ddata->pwm_freq_nstime = 1000000000 / pwm_freq;
494 static int rk_pwm_remove(struct platform_device *pdev)
500 static int remotectl_suspend(struct device *dev)
503 struct cpumask cpumask;
504 struct platform_device *pdev = to_platform_device(dev);
505 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
507 cpumask_clear(&cpumask);
508 cpumask_set_cpu(cpu, &cpumask);
509 irq_set_affinity(ddata->irq, &cpumask);
514 static int remotectl_resume(struct device *dev)
516 struct cpumask cpumask;
517 struct platform_device *pdev = to_platform_device(dev);
518 struct rkxx_remotectl_drvdata *ddata = platform_get_drvdata(pdev);
520 cpumask_clear(&cpumask);
521 cpumask_set_cpu(ddata->handle_cpu_id, &cpumask);
522 irq_set_affinity(ddata->irq, &cpumask);
526 static const struct dev_pm_ops remotectl_pm_ops = {
527 .suspend_late = remotectl_suspend,
528 .resume_early = remotectl_resume,
532 static const struct of_device_id rk_pwm_of_match[] = {
533 { .compatible = "rockchip,remotectl-pwm"},
537 MODULE_DEVICE_TABLE(of, rk_pwm_of_match);
539 static struct platform_driver rk_pwm_driver = {
541 .name = "remotectl-pwm",
542 .of_match_table = rk_pwm_of_match,
544 .pm = &remotectl_pm_ops,
547 .probe = rk_pwm_probe,
548 .remove = rk_pwm_remove,
551 module_platform_driver(rk_pwm_driver);
553 MODULE_LICENSE("GPL");