基于rk3128的leds灯闪问题

http://blog.csdn.net/wangjianzhongfj/article/details/42764437

http://www.docin.com/p-970537368.html

rk3128原生安卓代码里。按红外遥控器的时候,灯是不闪的。在这里,我把如何进行灯闪的问题解决一下。哈!

在\kernel\src\arch\arm\boot\dts\rk3128-box-rk88.dts增加gpio控制led的节点
rockchip_leds_gpio {
        compatible = "rockchip-leds-gpio";
led-gpios = <&gpio0 GPIO_D2 GPIO_ACTIVE_LOW>, <&gpio0 GPIO_B0 GPIO_ACTIVE_LOW>;
status = "okay";
};
 gpio_poweroff {
compatible = "gpio-poweroff";
gpios = <&gpio1 GPIO_A2 GPIO_ACTIVE_LOW>;
};
在\kernel\src\arch\arm\configs\rockchip_defconfig里,配置leds。

CONFIG_LEDS_TRIGGERS=y
CONFIG_LEDS_TRIGGER_BACKLIGHT=y
CONFIG_LEDS_TRIGGER_DEFAULT_ON=y

然后增加代码目录路径     \kernel\src\drivers\leds\rockchip-leds-gpio.c
[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * LEDs driver for GPIOs 
  3.  * 
  4.  * Copyright (C) 2007 8D Technologies inc. 
  5.  * Raphael Assenat <raph@8d.com> 
  6.  * Copyright (C) 2008 Freescale Semiconductor, Inc. 
  7.  * 
  8.  * This program is free software; you can redistribute it and/or modify 
  9.  * it under the terms of the GNU General Public License version 2 as 
  10.  * published by the Free Software Foundation. 
  11.  * 
  12.  */  
  13. #include <linux/kernel.h>  
  14. #include <linux/init.h>  
  15. #include <linux/platform_device.h>  
  16. #include <linux/gpio.h>  
  17. #include <linux/leds.h>  
  18. #include <linux/of_platform.h>  
  19. #include <linux/of_gpio.h>  
  20. #include <linux/rk_fb.h>  
  21. #include <linux/slab.h>  
  22. #include <linux/workqueue.h>  
  23. #include <linux/module.h>  
  24. #include <linux/pinctrl/consumer.h>  
  25. #include <linux/err.h>  
  26.  
  27. #define TAG "ROCKCHIP_LEDS_GPIO"  
  28. #if 0  
  29. #define D(fmt, arg...) printk("D>>> %s->%s(%d): " fmt, TAG, __FUNCTION__, __LINE__, ##arg)  
  30. #else  
  31. #define D(fmt, arg...)  
  32. #endif  
  33. #define E(fmt, arg...) printk("E>>> %s->%s(%d): " fmt, TAG, __FUNCTION__, __LINE__, ##arg)  
  34. #define I(fmt, arg...) printk("I>>> %s->%s(%d): " fmt, TAG, __FUNCTION__, __LINE__, ##arg)  
  35.  
  36. #define GPIO_LOW        0  
  37. #define GPIO_HIGH       1  
  38.  
  39. #define LED_ON          0  
  40. #define LED_OFF         1  
  41.   
  42. struct rk_gpio_leds_ctl_io {  
  43.     unsigned int power_green;  
  44.     unsigned int power_red;  
  45.       
  46.     unsigned int ir_red;  
  47. };  
  48.   
  49. struct rk_gpio_leds_priv {  
  50.     struct rk_gpio_leds_ctl_io leds;  
  51.     struct timer_list led_timer;  
  52.     unsigned int ir_red_status;  
  53.     unsigned int ir_red_first_resume;  
  54.     unsigned int suspend_state;   
  55. };  
  56.   
  57. struct rk_gpio_leds_priv *g_privdata = NULL;  
  58.   
  59. static ssize_t led_store(struct device *dev, struct device_attribute *attr, const char *cmd, size_t count)  
  60. {  
  61.     struct platform_device *pdev = to_platform_device(dev);  
  62.     struct rk_gpio_leds_priv *priv = platform_get_drvdata(pdev);  
  63.       
  64.     // I("led_store(): cmd = %s \n", cmd);  
  65.       
  66.     if (true == priv->suspend_state)  
  67.     {  
  68.         return count;  
  69.     }  
  70.   
  71.     if(0 == strcmp("IR_LED_ON", cmd))  
  72.     {  
  73.         gpio_set_value(priv->leds.ir_red, GPIO_HIGH);  
  74.     }  
  75.     else if(0 == strcmp("IR_LED_OFF", cmd))  
  76.     {  
  77.         gpio_set_value(priv->leds.ir_red, GPIO_LOW);  
  78.     }  
  79.       
  80.     return count;  
  81. }  
  82. static ssize_t led_show(struct device *dev, struct device_attribute *attr, char *buf)  
  83. {     
  84.     return 0;   
  85. }  
  86. static DEVICE_ATTR(led_ctl, 0777, led_show, led_store);  
  87.   
  88.   
  89. static void rk_gpio_led_early_suspend(void)  
  90. {  
  91.     struct rk_gpio_leds_priv *priv = g_privdata;  
  92.       
  93.     if (priv)  
  94.     {  
  95.         //POWER_LED_RED  
  96.         gpio_set_value(priv->leds.power_green, GPIO_LOW);  
  97.         gpio_set_value(priv->leds.power_red, GPIO_HIGH);  
  98.     }  
  99.       
  100.     return;  
  101. }  
  102.   
  103. static void rk_gpio_led_early_resume(void)  
  104. {  
  105.     struct rk_gpio_leds_priv *priv = g_privdata;  
  106.       
  107.     if (priv)  
  108.     {  
  109.         //POWER_LED_GREEN     
  110.         gpio_set_value(priv->leds.power_green, GPIO_HIGH);  
  111.         gpio_set_value(priv->leds.power_red, GPIO_LOW);  
  112.           
  113.         // IR_LED  
  114.         if (false == priv->ir_red_first_resume) // 开机时,系统会调用 rk_gpio_led_early_resume ,此时红外灯不应该闪烁。  
  115.         {  
  116.             mod_timer(&priv->led_timer, jiffies + msecs_to_jiffies(50));  
  117.         }  
  118.         else  
  119.         {  
  120.             priv->ir_red_first_resume = false;  
  121.         }  
  122.     }  
  123.           
  124.     return;  
  125. }  
  126.   
  127. static int rk_gpio_led_event_notify(struct notifier_block *self,  
  128.                       unsigned long action, void *data)  
  129. {  
  130.     struct fb_event *event = data;  
  131.     int blank_mode = *((int *)event->data);  
  132.   
  133.     if (action == FB_EARLY_EVENT_BLANK) {  
  134.         switch (blank_mode) {  
  135.         case FB_BLANK_UNBLANK:  
  136.             break;  
  137.         default:  
  138.             rk_gpio_led_early_suspend();  
  139.             break;  
  140.         }  
  141.     } else if (action == FB_EVENT_BLANK) {  
  142.         switch (blank_mode) {  
  143.         case FB_BLANK_UNBLANK:  
  144.             rk_gpio_led_early_resume();  
  145.             break;  
  146.         default:  
  147.             break;  
  148.         }  
  149.     }  
  150.   
  151.     return NOTIFY_OK;  
  152. }  
  153.   
  154. static struct notifier_block rk_gpio_led_notifier = {  
  155.     .notifier_call = rk_gpio_led_event_notify,  
  156. };  
  157.   
  158. static void rk_gpio_led_timer(unsigned long _data)  
  159. {  
  160.     struct rk_gpio_leds_priv *priv;  
  161.       
  162.     priv =  (struct rk_gpio_leds_priv *)_data;  
  163.       
  164.     if (LED_OFF == priv->ir_red_status)   // turn on  
  165.     {  
  166.         priv->ir_red_status = LED_ON;  
  167.         gpio_set_value(priv->leds.ir_red, GPIO_HIGH);  
  168.           
  169.         mod_timer(&priv->led_timer, jiffies + msecs_to_jiffies(300));  
  170.     }  
  171.     else    // turn off  
  172.     {  
  173.         priv->ir_red_status = LED_OFF;  
  174.         gpio_set_value(priv->leds.ir_red, GPIO_LOW);  
  175.     }  
  176. }  
  177.   
  178. static int rk_gpio_led_probe(struct platform_device *pdev)  
  179. {  
  180.     struct rk_gpio_leds_priv *priv;  
  181.     struct device_node *np = pdev->dev.of_node;  
  182.     int ret;  
  183.       
  184.     I ("rk_gpio_led v1.0 init");  
  185.   
  186.     priv = devm_kzalloc(&pdev->dev, sizeof(struct rk_gpio_leds_priv),  
  187.                  GFP_KERNEL);  
  188.     if (!priv) {  
  189.         dev_err(&pdev->dev, "failed to allocate memory\n");  
  190.         return -ENOMEM;  
  191.     }  
  192.       
  193.     g_privdata = priv;  
  194.       
  195.     priv->leds.power_green = of_get_named_gpio(np,"led-gpios", 0);  
  196.     priv->leds.power_red   = of_get_named_gpio(np,"led-gpios", 1);  
  197. //  priv->leds.ir_red      = of_get_named_gpio(np,"led-gpios", 2);  
  198.       
  199.     if(priv->leds.power_green)  
  200.     {  
  201.         ret = gpio_request(priv->leds.power_green, "power_green");  
  202.         if (!ret)  
  203.         {  
  204.             gpio_direction_output(priv->leds.power_green, GPIO_HIGH);  
  205.         }  
  206.         else  
  207.         {  
  208.             I("power_green request gpio fail!");  
  209.         }  
  210.     }  
  211.   
  212.     if(priv->leds.power_red)  
  213.     {  
  214.         ret = gpio_request(priv->leds.power_red, "power_red");  
  215.         if (!ret)  
  216.         {  
  217.             gpio_direction_output(priv->leds.power_red, GPIO_LOW);  
  218.         }  
  219.         else  
  220.         {  
  221.             I("power_red request gpio fail!");  
  222.         }  
  223.     }  
  224.       
  225.     if(priv->leds.ir_red)  
  226.     {  
  227.         ret = gpio_request(priv->leds.ir_red, "ir_red");  
  228.         if (!ret)  
  229.         {  
  230.             gpio_direction_output(priv->leds.ir_red, GPIO_LOW);  
  231.         }  
  232.         else  
  233.         {  
  234.             I("ir_red request gpio fail!");  
  235.         }  
  236.     }  
  237.     priv->ir_red_status = LED_OFF;  
  238.     priv->ir_red_first_resume = true;  
  239.     priv->suspend_state = false;  
  240.     setup_timer(&priv->led_timer, rk_gpio_led_timer, (unsigned long)priv);  
  241.       
  242.     ret = device_create_file(&pdev->dev, &dev_attr_led_ctl);  
  243.     if (ret) {  
  244.         return ret;  
  245.     }  
  246.       
  247.     fb_register_client(&rk_gpio_led_notifier);  
  248.       
  249.     platform_set_drvdata(pdev, priv);  
  250.   
  251.     return 0;  
  252. }  
  253.   
  254. static int rk_gpio_led_remove(struct platform_device *pdev)  
  255. {  
  256.     D("Enter function! \n");  
  257.   
  258.     platform_set_drvdata(pdev, NULL);  
  259.   
  260.     return 0;  
  261. }  
  262.  
  263. #ifdef CONFIG_PM  
  264. static int rk_gpio_led_suspend(struct device *dev)  
  265. {  
  266.     struct platform_device *pdev = to_platform_device(dev);  
  267.     struct rk_gpio_leds_priv *priv = platform_get_drvdata(pdev);  
  268.       
  269.     priv->suspend_state = true;  
  270.       
  271.     return 0;  
  272. }  
  273.   
  274.   
  275. static int rk_gpio_led_resume(struct device *dev)  
  276. {  
  277.     struct platform_device *pdev = to_platform_device(dev);  
  278.     struct rk_gpio_leds_priv *priv = platform_get_drvdata(pdev);  
  279.       
  280.     priv->suspend_state = false;  
  281.       
  282.     return 0;  
  283. }  
  284.   
  285. static const struct dev_pm_ops rk_gpio_led_pm_ops = {  
  286.     .suspend_late = rk_gpio_led_suspend,  
  287.     .resume_early = rk_gpio_led_resume,  
  288. };  
  289. #endif  
  290.   
  291. static const struct of_device_id of_gpio_leds_match[] = {  
  292.     { .compatible = "rockchip-leds-gpio", },  
  293.     {},  
  294. };  
  295. MODULE_DEVICE_TABLE(of, of_gpio_leds_match);  
  296.   
  297. static struct platform_driver gpio_led_driver = {  
  298.     .probe      = rk_gpio_led_probe,  
  299.     .remove     = rk_gpio_led_remove,  
  300.     .driver     = {  
  301.         .name   = "rockchip-leds-gpio",  
  302.         .owner  = THIS_MODULE,  
  303.         .of_match_table = of_match_ptr(of_gpio_leds_match),  
  304. #ifdef CONFIG_PM  
  305.         .pm = &rk_gpio_led_pm_ops,  
  306. #endif  
  307.     },  
  308. };  
  309.   
  310. module_platform_driver(gpio_led_driver);  
  311.   
  312. MODULE_AUTHOR("Fengminxi <fmx@rock-chips.com>");  
  313. MODULE_DESCRIPTION("Rockchip GPIO LED driver");  
  314. MODULE_LICENSE("GPL");  
  315. MODULE_ALIAS("platform:rockchip-leds-gpio");  
在\kernel\src\drivers\leds\makefile文件里增加下面代码。
# LED Triggers
obj-$(CONFIG_LEDS_TRIGGERS) += trigger/

在\kernel\src\drivers\leds\Kconfig里增加
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. config LEDS_GPIO_REGISTER  
  2.     bool  
  3.     help  
  4.       This option provides the function gpio_led_register_device.  
  5.       As this function is used by arch code it must not be compiled as a  
  6.       module.  
  7.   
  8. menuconfig NEW_LEDS  
  9.     bool "LED Support"  
  10.     help  
  11.       Say Y to enable Linux LED support.  This allows control of supported  
  12.       LEDs from both userspace and optionally, by kernel events (triggers).  
  13.   
  14.       This is not related to standard keyboard LEDs which are controlled  
  15.       via the input system.  
  16.   
  17. if NEW_LEDS  
  18.   
  19. config LEDS_CLASS  
  20.     tristate "LED Class Support"  
  21.     help  
  22.       This option enables the led sysfs class in /sys/class/leds.  You'll  
  23.       need this to do anything useful with LEDs.  If unsure, say N.  
  24.   
  25. comment "LED drivers"  

这样linux层配置好。我们需要通过安卓层来调用。

在/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java,直接去控制底层的gpio的leds。
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     static final int FREQUENCY = 10;  
  2.      private int mKeyDownCount = 0;  
  3.      private long mKeyDownTime = 0;  
  4. +  
  5. +    private void updateLEDState(String cmd) {  
  6. +        File f = new File("/sys/devices/rockchip_leds_gpio.17/led_ctl");  
  7. +        OutputStream output = null;  
  8. +        OutputStreamWriter outputWrite = null;  
  9. +        PrintWriter print = null;  
  10. +        StringBuffer strbuf = new StringBuffer("");  
  11. +        strbuf.append(cmd);  
  12. +        try {  
  13. +            output = new FileOutputStream(f);  
  14. +            outputWrite = new OutputStreamWriter(output);  
  15. +            print = new PrintWriter(outputWrite);  
  16. +            print.print(strbuf.toString());  
  17. +            print.flush();  
  18. +            output.close();  
  19. +        }catch (FileNotFoundException e){  
  20. +            e.printStackTrace();  
  21. +        }catch (IOException e){  
  22. +            e.printStackTrace();  
  23. +        }  
  24. +    }  
  25. +  
  26.  //$_rbox_$_modify_$_end  
  27.      @Override  
  28.      public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {  
  29. @@ -4267,6 +4289,16 @@ public class PhoneWindowManager implements WindowManagerPolicy {  
  30.          final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;  
  31.          final boolean canceled = event.isCanceled();  
  32.          final int keyCode = event.getKeyCode();  
  33. +          
  34. +        if (down)  
  35. +        {  
  36. +            updateLEDState ("IR_LED_ON");  
  37. +        }  
  38. +        else  
  39. +        {  
  40. +            updateLEDState ("IR_LED_OFF");  
  41. +        }  
  42. +          
  43.          //<!-- $_rbox_$_modify_$_huangjc -->  
  44.          if("true".equals(SystemProperties.get("persist.sys.realsleep"))){  
  45.            if((SystemProperties.getInt("persist.sys.poweroff", 0) == 1)&&!(keyCode == KeyEvent.KEYCODE_POWER)){  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值