]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
find gpio by base + number
authorseantywork <seantywork@gmail.com>
Sun, 16 Nov 2025 08:03:19 +0000 (08:03 +0000)
committerseantywork <seantywork@gmail.com>
Sun, 16 Nov 2025 08:03:19 +0000 (08:03 +0000)
README.md
kdev-gpio/.gitignore [new file with mode: 0644]
kdev-gpio/kdev_gpio.c

index 61707c0bce7362f051722d3ccfa30c851c6f7211..6f3ad8a3ecbba1c43f7beae3aa613f15cb83f59c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -11684,7 +11684,37 @@ sudo irsend SEND_ONCE LG_AC AC_ON
 
 ```
 
+# DTS DT DTB 
 
+```shell
+# gpiosk.dts
+/dts-v1/;
+/plugin/;
+
+/ {
+    compatible = "brcm,bcm2711";
+
+    fragment@0 {
+        target = <&gpio>;
+        __overlay__ {
+            pinctrl-names = "default";
+            pinctrl-0 = <&gpiosk_pins_in>;
+
+            gpiosk_pins_in: gpiosk_pins_in {
+                brcm,pins = <17>;     /* GPIO17 */
+                brcm,function = <0>; /* Input */
+            };
+        };
+    };
+};
+
+```
+
+```shell
+# compile 
+dtc -@ -I dts -O dtb -o gpiosk.dtb gpiosk.dts
+
+```
 
 # JETSON XAVIER NX
 
diff --git a/kdev-gpio/.gitignore b/kdev-gpio/.gitignore
new file mode 100644 (file)
index 0000000..53492d4
--- /dev/null
@@ -0,0 +1 @@
+*.dtb
\ No newline at end of file
index 06bc9f50d2c887211f43fca19560311ed4781148..1b1fce3eceefa4449dfeef828936d2d7472a36ea 100644 (file)
 #include <linux/string.h>
 #include <linux/version.h> 
 
-#include <linux/of.h>
-#include <linux/of_gpio.h>
-#include <linux/device.h>
-#include <linux/container_of.h>
-#include <linux/gpio/machine.h>
+#include <linux/gpio/driver.h>
 #include <linux/gpio/consumer.h>
 
-#define TARGET_NAME    "GPIO17"
+#define TARGET_LABEL "pinctrl-bcm2711"
+#define TARGET_PIN_NAME        "GPIO17"
 
-static int __init kdev_gpio_init(void){
-    int n = 0;
-    int gpio_target = 0;
-    struct device_node* dn = NULL;
-    struct device* d = NULL;
-    struct gpio_desc* gd = NULL;
-    dn = of_find_node_by_name(NULL, "fe200000.gpio");
-    if(dn == NULL){
-        printk("failed to find device node: gpio\n");
-        return -1;
-    }
-    d = container_of(&dn, struct device, of_node);
-    if(IS_ERR(d)){
-        printk("failed to get device from node\n");
-        return -1;
-    }
-    gd = gpiod_get(d, NULL, 0);
-    if(IS_ERR(gd)){
-        printk("failed to get desc\n");
-        return -1;
-    }
-    printk("got desc\n");
 
-    /*
-    for(int i = 0; i < n; i++){
-        gpio_target = of_get_named_gpio(dn, TARGET_NAME, i);
-        printk("gpio_target: %d\n", gpio_target);
-        if(gpio_target == -EPROBE_DEFER){
-            printk("EPROBE DEFER\n");
-            continue;
-        }
-        if(gpio_is_valid(gpio_target)){
-            // deprecated
-            //gpio_target = devm_gpio_request(d, gpio_target, TARGET_NAME);
-            printk("SUCESS: gpio_target: %d\n", gpio_target);
-        } else {
-            printk("gpio is not valid\n");
-            continue;
+static int _gc_match(struct gpio_chip *gc, const void *data){
+    int n = 0;
+    if(gc->label != NULL){
+        printk("kdev_gpio: label: %s\n", gc->label);
+        if(strcmp(gc->label, TARGET_LABEL) == 0){
+            printk("base: %d\n", gc->base);
+            printk("ngpio: %d\n", gc->ngpio);
         }
+    } else {
+        printk("kdev_gpio: label: not available\n");
     }
-    */
+
+    return 0;
+}
+static int __init kdev_gpio_init(void){
+    gpio_device_find(NULL, _gc_match);
+
     return 0;
 }
 
 static void __exit kdev_gpio_exit(void){
-    printk("bye\n");
+    printk("kdev_gpio: bye\n");
 }
 
 module_init(kdev_gpio_init);