RK系统自带GPIO控制驱动,以及GPIO计算方法

546 阅读3分钟

系统自带GPIO控制驱动:
内核已经自带了通用GPIO驱动,可以直接在用户空间操作.
路径: /sys/class/gpio
root@rk3288:/sys/class/gpio # ls
export
gpiochip0
gpiochip120
gpiochip152
gpiochip184
gpiochip216
gpiochip24
gpiochip248
gpiochip280
gpiochip56
gpiochip88
unexport

比如我要操作GPIO8_A6作为高电平输出有效, 那么有以下问题:
1. 对应的gpio number是多少呢?
2. 如何设置成输出
3. 如何输出高电平

问题1:
可以通过/sys/kernel/debug/gpio查询信息:
root@rk3288:/sys/kernel/debug # cat gpio                                       
//snip
GPIOs 184-215, platform/ff770000.pinctrl, gpio6:
gpio-193 (?                   ) in  hi
gpio-194 (?                   ) in  hi

GPIOs 216-247, platform/ff770000.pinctrl, gpio7:
gpio-218 (enable              ) out hi
gpio-219 (lcd_en              ) in  hi
gpio-220 (lcd_cs              ) in  hi
gpio-221 (gslX680 wake pin    ) out hi
gpio-222 (gslX680 irq pin     ) out lo
gpio-223 (headset_gpio        ) in  hi
gpio-233 (?                   ) in  hi
gpio-234 (?                   ) in  hi

GPIOs 248-279, platform/ff770000.pinctrl, gpio8:

GPIOs 280-311, platform/ff770000.pinctrl, gpio15:

可以看到gpio8是以nubmer为248开始, 那么GPIO8_A6就是 248(32*8) +6(0*8+6)  = 254


接下来就可以导出gpio了
root@rk3288:/sys/class/gpio # echo 254 > export                          
root@rk3288:/sys/class/gpio # ls
export
gpio254
......

问题2
root@rk3288:/sys/class/gpio/gpio254 # echo out > direction                     
root@rk3288:/sys/class/gpio/gpio254 # cat direction                            
out

问题3:
root@rk3288:/sys/class/gpio/gpio254 # echo 1 > value                           
root@rk3288:/sys/class/gpio/gpio254 # cat value                                
1

解决方法:
init.rc
[kris@:~/rk3288/device/rockchip/common/recovery/etc]$ g df
diff --git a/recovery/etc/init.rc b/recovery/etc/init.rc
index efe2a5d..75d7554 100755
--- a/recovery/etc/init.rc
+++ b/recovery/etc/init.rc
@@ -85,6 +85,12 @@ on late-init
on property:sys.powerctl=*
powerctl ${sys.powerctl}

+#Kris, 170407, GPIO8_A6 -> gpio254, used to inform sd upgrade complete.
+on property:sdupgrade.complete=true
+       write /sys/class/gpio/export 254
+       write /sys/class/gpio/gpio254/direction out
+       write /sys/class/gpio/gpio254/value 1
+
service ueventd /sbin/ueventd
critical
seclabel u:r:ueventd:s0

recovery.cpp
[kris@:~/rk3288/bootable/recovery]$ g df
diff --git a/recovery.cpp b/recovery.cpp
index 8299672..a0edc15 100755
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -2285,6 +2285,10 @@ HANDLE_STATUS :
strcat(kernellogPath, "/kernel.txt");
save_kernel_log(kernellogPath);
#endif
+
+       //Kris, 170407, light led on after sd upgrade complete.
+       property_set("sdupgrade.complete", "true");
+
if (bSDMounted)
checkSDRemoved();

内核C语言代码

#include <linux/of_gpio.h>

 gpio_request(84,"red_led"); //red led  向CPU请求GPIO
gpio_request(85,"green_led"); //green led 向CPU请求GPIO
gpio_direction_output(84, 1);  //close red led  输出GPIO84高电平
gpio_direction_output(85, 0);  //open green led  输出GPIO85低电平

Uboot 写法

 gpio_direction_output(GPIO_BANK2 | GPIO_C4, 1); //red led
gpio_direction_output(GPIO_BANK2 | GPIO_C5, 1); //green led

freamwork 写法控制

diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/se
index 348af1f..13b87c5f 100755
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -94,7 +94,14 @@ import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING;
import android.hardware.hdmi.*;
import android.os.ServiceManager;

-
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.RandomAccessFile;
+import java.io.FileOutputStream;
/**
* The power manager service is responsible for coordinating power management
* functions on the device.
@@ -526,7 +533,59 @@ public final class PowerManagerService extends SystemService
private static native void nativeSetAutoSuspend(boolean enable);
private static native void nativeSendPowerHint(int hintId, int data);
private static native void nativeSetFeature(int featureId, int data);
-
+        //add by hason
+public  void Led_On_Off_my(int i, String led)
+     {    // String led0="0";
+          // String led1="1"
+             //if (levle==true)i=1;
+          File file = new File("/sys/class/leds/red/brightness");
+          File file1 = new File("/sys/class/leds/green/brightness");
+           if(i==1)
+           {
+           if (file.exists())
+              {
+                try{
+                if (file.canWrite())
+                   {     Slog.d("lhm-write", file.toString() + "   can  write");
+                        FileOutputStream fout = new FileOutputStream(file);
+                       byte[]  bytes =led.getBytes();
+                         fout.write(bytes);
+
+                        fout.close();
+                   }
+                   else
+                  Slog.d("lhm", file.toString() + "   can not write");
+                }catch(Exception e)
+                {
+                        Slog.d("hjc", file.toString() + "can not write");
+                }
+              }
+         }
+
+        else
+
+ {
+           if (file1.exists())
+              {
+                try{
+                if (file1.canWrite())
+                   {     Slog.d("lhm-write", file1.toString() + "   can  write");
+                        FileOutputStream fout = new FileOutputStream(file1);
+                       byte[]  bytes =led.getBytes();
+                         fout.write(bytes);
+
+                        fout.close();
+                   }
+                   else
+                  Slog.d("lhm", file1.toString() + "   can not write");
+                }catch(Exception e)
+                {
+                        Slog.d("hjc", file1.toString() + "can not write");
+                }
+              }
+         }
+        }
+//end
public PowerManagerService(Context context) {
super(context);
mContext = context;
@@ -1241,6 +1300,10 @@ public final class PowerManagerService extends SystemService
try {
switch (mWakefulness) {
case WAKEFULNESS_ASLEEP:
+                                        Led_On_Off_my(2,"1");
+                  Slog.i(TAG, "LED GREEN ON");
+                    Led_On_Off_my(1,"1");
+                  Slog.i(TAG, "LED RED OFF");
Slog.i(TAG, "Waking up from sleep (uid " + reasonUid +")...");
break;
case WAKEFULNESS_DREAMING:
@@ -1317,6 +1380,10 @@ public final class PowerManagerService extends SystemService
Slog.i(TAG, "Going to sleep due to lid switch (uid " + uid +")...");
break;
case PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON:
+                        Led_On_Off_my(1,"0");
+                  Slog.i(TAG, "LED RED ON");
+                    Led_On_Off_my(2,"0");
+                  Slog.i(TAG, "LED GREEN OFF");
Slog.i(TAG, "Going to sleep due to power button (uid " + uid +")...");
break;
case PowerManager.GO_TO_SLEEP_REASON_SLEEP_BUTTON:
(END)