<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: GPIO debounce on IMX8MP in i.MX Processors</title>
    <link>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1680509#M208548</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/219683"&gt;@growbook&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="im"&gt;I hope you are doing well.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Please share the dmesg logs or error logs also to debug the issue further.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks &amp;amp; Regards,&lt;BR /&gt;Dhruvit Vasavada&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Jul 2023 12:31:22 GMT</pubDate>
    <dc:creator>Dhruvit</dc:creator>
    <dc:date>2023-07-03T12:31:22Z</dc:date>
    <item>
      <title>GPIO debounce on IMX8MP</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1679311#M208404</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm trying to set GPIO debounce time on Linux made from Yocto.&lt;/P&gt;&lt;P&gt;I was going to do it using gpio_set_debounce() function.&lt;/P&gt;&lt;P&gt;Like this :&lt;/P&gt;&lt;DIV class=""&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;SPAN class=""&gt;if&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;gpio_set_debounce&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;GPIO_25_IN, &lt;/SPAN&gt;&lt;SPAN class=""&gt;200&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt; &lt;SPAN class=""&gt;&amp;lt;&lt;/SPAN&gt; &lt;SPAN class=""&gt;0&lt;/SPAN&gt;&lt;SPAN class=""&gt;){&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;DIV class="lia-indent-padding-left-30px"&gt;&lt;DIV class="lia-indent-padding-left-30px"&gt;&lt;SPAN class=""&gt;pr_err&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;"ERROR: gpio_set_debounce - %d\n"&lt;/SPAN&gt;&lt;SPAN class=""&gt;, GPIO_25_IN&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt;&lt;SPAN class=""&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="lia-indent-padding-left-30px"&gt;&lt;DIV class="lia-indent-padding-left-30px"&gt;&lt;SPAN class=""&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, it always makes an error.&lt;/P&gt;&lt;P&gt;Is it possible to enable GPIO debounce? or Is there any pre-defined debounce time?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;growbook&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2023 06:57:32 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1679311#M208404</guid>
      <dc:creator>growbook</dc:creator>
      <dc:date>2023-06-30T06:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: GPIO debounce on IMX8MP</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1680509#M208548</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/219683"&gt;@growbook&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="im"&gt;I hope you are doing well.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;Please share the dmesg logs or error logs also to debug the issue further.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks &amp;amp; Regards,&lt;BR /&gt;Dhruvit Vasavada&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 12:31:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1680509#M208548</guid>
      <dc:creator>Dhruvit</dc:creator>
      <dc:date>2023-07-03T12:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: GPIO debounce on IMX8MP</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1680919#M208597</link>
      <description>&lt;P&gt;Sorry that I've offered a few clues for debugging.&lt;/P&gt;&lt;P&gt;This is my code.&lt;/P&gt;&lt;P&gt;(You may not need to read the whole code. I'll explain it in the next paragraph)&lt;/P&gt;&lt;LI-CODE lang="c"&gt;/***************************************************************************//**
*  \file       driver.c
*
*  \details    Simple GPIO driver explanation (GPIO Interrupt)
*
*  \author     EmbeTronicX
*
*  \Tested with Linux raspberrypi 5.4.51-v7l+
*
*******************************************************************************/
#include &amp;lt;linux/kernel.h&amp;gt;
#include &amp;lt;linux/init.h&amp;gt;
#include &amp;lt;linux/module.h&amp;gt;
#include &amp;lt;linux/kdev_t.h&amp;gt;
#include &amp;lt;linux/fs.h&amp;gt;
#include &amp;lt;linux/cdev.h&amp;gt;
#include &amp;lt;linux/device.h&amp;gt;
#include &amp;lt;linux/delay.h&amp;gt;
#include &amp;lt;linux/uaccess.h&amp;gt;  //copy_to/from_user()
#include &amp;lt;linux/gpio.h&amp;gt;     //GPIO
#include &amp;lt;linux/interrupt.h&amp;gt;
#include &amp;lt;linux/err.h&amp;gt;
/* Since debounce is not supported in Raspberry pi, I have addded this to disable 
** the false detection (multiple IRQ trigger for one interrupt).
** Many other hardware supports GPIO debounce, I don't want care about this even 
** if this has any overhead. Our intention is to explain the GPIO interrupt.
** If you want to disable this extra coding, you can comment the below macro.
** This has been taken from : https://raspberrypi.stackexchange.com/questions/8544/gpio-interrupt-debounce
**
** If you want to use Hardaware Debounce, then comment this EN_DEBOUNCE.
**
*/
// #define EN_DEBOUNCE
#ifdef EN_DEBOUNCE
#include &amp;lt;linux/jiffies.h&amp;gt;
extern unsigned long volatile jiffies;
unsigned long old_jiffie = 0;
#endif
//LED is connected to this GPIO
#define GPIO_137_OUT 137
//LED is connected to this GPIO
#define GPIO_135_IN  135
//GPIO_135_IN value toggle
unsigned int led_toggle = 0; 
//This used for storing the IRQ number for the GPIO
unsigned int GPIO_irqNumber;
//Interrupt handler for GPIO 135. This will be called whenever there is a raising edge detected. 
static irqreturn_t gpio_irq_handler(int irq,void *dev_id) 
{
  static unsigned long flags = 0;
#ifdef EN_DEBOUNCE
   unsigned long diff = jiffies - old_jiffie;
   pr_info("diff calculation is called");
   if (diff &amp;lt; 20)
   {
     return IRQ_HANDLED;
   }
  
  old_jiffie = jiffies;
#endif  
  local_irq_save(flags);
  led_toggle = (0x01 ^ led_toggle);                             // toggle the old value
  gpio_set_value(GPIO_137_OUT, led_toggle);                      // toggle the GPIO_137_OUT
  pr_info("Interrupt Occurred : GPIO_137_OUT : %d ",gpio_get_value(GPIO_137_OUT));
  local_irq_restore(flags);
  return IRQ_HANDLED;
}
 
dev_t dev = 0;
static struct class *dev_class;
static struct cdev etx_cdev;
 
static int __init etx_driver_init(void);
static void __exit etx_driver_exit(void);
 
 
/*************** Driver functions **********************/
static int etx_open(struct inode *inode, struct file *file);
static int etx_release(struct inode *inode, struct file *file);
static ssize_t etx_read(struct file *filp, 
                char __user *buf, size_t len,loff_t * off);
static ssize_t etx_write(struct file *filp, 
                const char *buf, size_t len, loff_t * off);
/******************************************************/
//File operation structure 
static struct file_operations fops =
{
  .owner          = THIS_MODULE,
  .read           = etx_read,
  .write          = etx_write,
  .open           = etx_open,
  .release        = etx_release,
};
/*
** This function will be called when we open the Device file
*/ 
static int etx_open(struct inode *inode, struct file *file)
{
  pr_info("Device File Opened...!!!\n");
  return 0;
}
/*
** This function will be called when we close the Device file
*/ 
static int etx_release(struct inode *inode, struct file *file)
{
  pr_info("Device File Closed...!!!\n");
  return 0;
}
/*
** This function will be called when we read the Device file
*/ 
static ssize_t etx_read(struct file *filp, 
                char __user *buf, size_t len, loff_t *off)
{
  uint8_t gpio_state = 0;
  
  //reading GPIO value
  gpio_state = gpio_get_value(GPIO_137_OUT);
  
  //write to user
  len = 1;
  if( copy_to_user(buf, &amp;amp;gpio_state, len) &amp;gt; 0) {
    pr_err("ERROR: Not all the bytes have been copied to user\n");
  }
  
  pr_info("Read function : GPIO_137 = %d \n", gpio_state);
  pr_info("Read function : GPIO_135 = %d \n", gpio_get_value(GPIO_135_IN));
  
  return 0;
}
/*
** This function will be called when we write the Device file
*/
static ssize_t etx_write(struct file *filp, 
                const char __user *buf, size_t len, loff_t *off)
{
  uint8_t rec_buf[10] = {0};
  
  if( copy_from_user( rec_buf, buf, len ) &amp;gt; 0) {
    pr_err("ERROR: Not all the bytes have been copied from user\n");
  }
  

  if (rec_buf[0]=='1') {
    //set the GPIO value to HIGH
    gpio_set_value(GPIO_137_OUT, 1);
  } else if (rec_buf[0]=='0') {
    //set the GPIO value to LOW
    gpio_set_value(GPIO_137_OUT, 0);
  } else {
    pr_err("Unknown command : Please provide either 1 or 0 \n");
  }

  pr_info("Write Function : GPIO_137 Set = %c\n", rec_buf[0]);
  
  return len;
}
/*
** Module Init function
*/ 
static int __init etx_driver_init(void)
{
  /*Allocating Major number*/
  if((alloc_chrdev_region(&amp;amp;dev, 0, 1, "etx_Dev")) &amp;lt;0){
    pr_err("Cannot allocate major number\n");
    goto r_unreg;
  }
  pr_info("Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
  /*Creating cdev structure*/
  cdev_init(&amp;amp;etx_cdev,&amp;amp;fops);
  /*Adding character device to the system*/
  if((cdev_add(&amp;amp;etx_cdev,dev,1)) &amp;lt; 0){
    pr_err("Cannot add the device to the system\n");
    goto r_del;
  }
  /*Creating struct class*/
  if(IS_ERR(dev_class = class_create(THIS_MODULE,"etx_class"))){
    pr_err("Cannot create the struct class\n");
    goto r_class;
  }
  /*Creating device*/
  if(IS_ERR(device_create(dev_class,NULL,dev,NULL,"etx_device"))){
    pr_err( "Cannot create the Device \n");
    goto r_device;
  }
  
  //Output GPIO configuration
  //Checking the GPIO is valid or not
  if(gpio_is_valid(GPIO_137_OUT) == false){
    pr_err("GPIO %d is not valid\n", GPIO_137_OUT);
    goto r_device;
  }
  
  //Requesting the GPIO
  if(gpio_request(GPIO_137_OUT,"GPIO_137_OUT") &amp;lt; 0){
    pr_err("ERROR: GPIO %d request\n", GPIO_137_OUT);
    goto r_gpio_out;
  }
  
  //configure the GPIO as output
  gpio_direction_output(GPIO_137_OUT, 0);
  
  //Input GPIO configuratioin
  //Checking the GPIO is valid or not
  if(gpio_is_valid(GPIO_135_IN) == false){
    pr_err("GPIO %d is not valid\n", GPIO_135_IN);
    goto r_gpio_in;
  }
  
  //Requesting the GPIO
  if(gpio_request(GPIO_135_IN,"GPIO_135_IN") &amp;lt; 0){
    pr_err("ERROR: GPIO %d request\n", GPIO_135_IN);
    goto r_gpio_in;
  }
  
  //configure the GPIO as input
  gpio_direction_input(GPIO_135_IN);
  
  /*
  ** I have commented the below few lines, as gpio_set_debounce is not supported 
  ** in the Raspberry pi. So we are using EN_DEBOUNCE to handle this in this driver.
  */ 
#ifndef EN_DEBOUNCE
  //Debounce the button with a delay of 200ms
  pr_info("gpio_set_debounce is called\n");
  if(gpio_set_debounce(GPIO_135_IN, 1000) &amp;lt; 0){
    pr_err("ERROR: gpio_set_debounce - %d\n", GPIO_135_IN);
    //goto r_gpio_in;
  }
#endif
  
  //Get the IRQ number for our GPIO
  GPIO_irqNumber = gpio_to_irq(GPIO_135_IN);
  pr_info("GPIO_irqNumber = %d\n", GPIO_irqNumber);
  
  if (request_irq(GPIO_irqNumber,             //IRQ number
                  (void *)gpio_irq_handler,   //IRQ handler
                  IRQF_TRIGGER_RISING,        //Handler will be called in raising edge
                  "etx_device",               //used to identify the device name using this IRQ
                  NULL)) {                    //device id for shared IRQ
    pr_err("my_device: cannot register IRQ ");
    goto r_gpio_in;
  }
  
  
 
  pr_info("Device Driver Insert...Done!!!\n");
  return 0;
r_gpio_in:
  gpio_free(GPIO_135_IN);
r_gpio_out:
  gpio_free(GPIO_137_OUT);
r_device:
  device_destroy(dev_class,dev);
r_class:
  class_destroy(dev_class);
r_del:
  cdev_del(&amp;amp;etx_cdev);
r_unreg:
  unregister_chrdev_region(dev,1);
  
  return -1;
}
/*
** Module exit function
*/
static void __exit etx_driver_exit(void)
{
  free_irq(GPIO_irqNumber,NULL);
  gpio_free(GPIO_135_IN);
  gpio_free(GPIO_137_OUT);
  device_destroy(dev_class,dev);
  class_destroy(dev_class);
  cdev_del(&amp;amp;etx_cdev);
  unregister_chrdev_region(dev, 1);
  pr_info("Device Driver Remove...Done!!\n");
}
 
module_init(etx_driver_init);
module_exit(etx_driver_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("EmbeTronicX &amp;lt;embetronicx@gmail.com&amp;gt;");
MODULE_DESCRIPTION("A simple device driver - GPIO Driver (GPIO Interrupt) ");
MODULE_VERSION("1.33");

&lt;/LI-CODE&gt;&lt;P&gt;&lt;SPAN&gt;This code is a simple GPIO driver for handling interrupts on a Linux system. Although comments in the code seem like targeting the Raspberry Pi platform, I've tested it on Imx8mp board. It allows users to control an output GPIO (GPIO_137) and receive interrupt notifications when a rising edge is detected on an input GPIO (GPIO_135).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The functionality I want to implement is setting debounce time. I've tried to search 'debounce' on every datasheet of imx8mp, but I could not find it. So, I was wondering that Imx8mp support debounce time or not.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Also, Here is dmesg.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[  124.721829] gpio_irq: loading out-of-tree module taints kernel.
[  124.728804] Major = 510 Minor = 0
[  124.732897] gpio_set_debounce is called
[  124.736778] ERROR: gpio_set_debounce - 135
[  124.740951] GPIO_irqNumber = 224
[  124.744225] Device Driver Insert...Done!!!&lt;/LI-CODE&gt;&lt;P&gt;This is a message when the device driver module started.&lt;/P&gt;&lt;P&gt;At the fourth line, gpio_set_debounce returns an error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, now I'm trying to set the debounce time at device tree like this.&lt;/P&gt;&lt;LI-CODE lang="c"&gt; gpio-keys {
                compatible = "gpio-keys";
                pinctrl-names = "default";
                pinctrl-0 = &amp;lt;&amp;amp;pinctrl_gpio_switch&amp;gt;;

                switch {
                        label = "button";
                        gpios = &amp;lt;&amp;amp;gpio5 7  GPIO_ACTIVE_HIGH&amp;gt;;
                        debounce-interval = &amp;lt;100&amp;gt;;
                };
        };

(emitted)


pinctrl_gpio_switch: gpioswitchgrp {
                fsl,pins = &amp;lt;
                        MX8MP_IOMUXC_ECSPI1_MOSI__GPIO5_IO07    0x16
                &amp;gt;;
        };

&lt;/LI-CODE&gt;&lt;P&gt;And it also doesn't work as I expected.&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;growbook91&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2023 05:43:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1680919#M208597</guid>
      <dc:creator>growbook</dc:creator>
      <dc:date>2023-07-04T05:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: GPIO debounce on IMX8MP</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1681844#M208679</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/219683"&gt;@growbook&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="im"&gt;I hope you are doing well.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;-&amp;gt;Thanks for sharing the code &amp;amp; further details.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-&amp;gt;Please check with the pad settings you made in the code are being used by other nodes or not in the same device tree.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-&amp;gt;I found out that uart3 also uses the same pad so please disable the uart3 node from the device tree by doing the below settings.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;status = "disabled"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now it should work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks &amp;amp; Regards,&lt;BR /&gt;Dhruvit Vasavada&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 08:40:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/GPIO-debounce-on-IMX8MP/m-p/1681844#M208679</guid>
      <dc:creator>Dhruvit</dc:creator>
      <dc:date>2023-07-05T08:40:55Z</dc:date>
    </item>
  </channel>
</rss>

