For one of my projects I'm slightly modifying the Linux serial driver, so I can drive a GPIO pin to 1 right before a Tx session starts and to 0 again after the session ends. I'm doing this by including the gpio header in the driver and calling the appropriate functions:
EDIT: After the suggestions of @sawdust in the comments the new code looks like this
In Tx start:
static void imx_start_tx(struct uart_port *port)
{
struct imx_port *sport = (struct imx_port *)port;
unsigned long temp;
gpio_set_value(140, 1);
In Tx stop:
static void imx_stop_tx(struct uart_port *port)
{
struct imx_port *sport = (struct imx_port *)port;
unsigned long temp;
gpio_set_value(140, 0);
While the GPIO request is being done in the imx_startup:
static int imx_startup(struct uart_port *port)
{
struct imx_port *sport = (struct imx_port *)port;
int retval, i;
unsigned long flags, temp;
/* Request GPIO #140, used for control flow */
int gpio_num = 140;
const char* label = "f";
if (!gpio_is_valid(gpio_num)){
printk(KERN_INFO "GPIO_RS485: invalid GPIO pin\n");
return -ENODEV;
}
int gpio_req = gpio_request(gpio_num, label);
if(gpio_req != 0){
printk(KERN_INFO "GPIO_RS485: GPIO access request failed with %d\n", gpio_req);
} else {
printk(KERN_INFO "GPIO_RS485: GPIO access request succeeded!\n");
}
And the gpio_free(); is being called in imx_shutdown
static void imx_shutdown(struct uart_port *port)
{
struct imx_port *sport = (struct imx_port *)port;
unsigned long temp;
unsigned long flags;
gpio_free(140);
However the issue persists. Another thing is that now I'm getting the following messages in dmesg, which means that the driver is being initialized several times?
EDIT ENDS HERE
The problem with this code is that I can confirm that the pin is correctly being driven to 1, but then it never gets back to 0. Why is this happening and how can I fix this?