mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Felipe Balbi <balbi@ti.com>
To: Tomoya MORINAGA <tomoya.rohm@gmail.com>
Cc: Felipe Balbi <balbi@ti.com>, Greg Kroah-Hartman <gregkh@suse.de>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	qi.wang@intel.com, yong.y.wang@intel.com, joel.clark@intel.com,
	kok.howg.ewe@intel.com
Subject: Re: [PATCH 2/3] pch_udc: Detecting VBUS through GPIO with interrupt
Date: Thu, 9 Feb 2012 09:58:43 +0200	[thread overview]
Message-ID: <20120209075842.GK1121@legolas.emea.dhcp.ti.com> (raw)
In-Reply-To: <1328253259-9343-2-git-send-email-tomoya.rohm@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5811 bytes --]

On Fri, Feb 03, 2012 at 04:14:18PM +0900, Tomoya MORINAGA wrote:
> Problem:
>  pch_udc continues operation even if VBUS becomes Low.
>  pch_udc performs D+ pulling up before VBUS becomes High.
>  USB device should be controlled according to VBUS state.
> 
> Root cause:
>  The current pch_udc is not always monitoring VBUS.
> 
> Solution:
>  The change of VBUS is detected using an interrupt of GPIO.
>  If VBUS became Low, pch_udc handles 'disconnect'.
>  After VBUS became High, a pull improves D+, and pch_udc
>  handles 'connect'.
> 
> Signed-off-by: Tomoya MORINAGA <tomoya.rohm@gmail.com>
> ---
>  drivers/usb/gadget/pch_udc.c |   85 ++++++++++++++++++++++++++++++++++++++++-
>  1 files changed, 82 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/gadget/pch_udc.c b/drivers/usb/gadget/pch_udc.c
> index d77211c..942fe92 100644
> --- a/drivers/usb/gadget/pch_udc.c
> +++ b/drivers/usb/gadget/pch_udc.c
> @@ -306,11 +306,15 @@ struct pch_udc_ep {
>   * struct pch_vbus_gpio_data - Structure holding GPIO informaton
>   *					for detecting VBUS
>   * @port:		gpio port number
> + * @intr:		gpio interrupt number
>   * @irq_work_fall	Structure for WorkQueue
> + * @irq_work_rise	Structure for WorkQueue
>   */
>  struct pch_vbus_gpio_data {
>  	int			port;
> +	int			intr;
>  	struct work_struct	irq_work_fall;
> +	struct work_struct	irq_work_rise;
>  };
>  
>  /**
> @@ -1296,8 +1300,10 @@ static void pch_vbus_gpio_work_fall(struct work_struct *irq_work)
>  				dev->driver->disconnect(
>  					&dev->gadget);
>  			}
> -			pch_udc_reconnect(dev);
> -			dev_dbg(&dev->pdev->dev, "VBUS fell");
> +			if (dev->vbus_gpio.intr)
> +				pch_udc_init(dev);
> +			else
> +				pch_udc_reconnect(dev);
>  			return;
>  		}
>  		vbus_saved = vbus;
> @@ -1306,6 +1312,57 @@ static void pch_vbus_gpio_work_fall(struct work_struct *irq_work)
>  }
>  
>  /**
> + * pch_vbus_gpio_work_rise() - This API checks VBUS is High.
> + *                             If VBUS is High, connect is processed
> + * @irq_work:	Structure for WorkQueue
> + *
> + */
> +static void pch_vbus_gpio_work_rise(struct work_struct *irq_work)
> +{
> +	struct pch_vbus_gpio_data *vbus_gpio = container_of(irq_work,
> +		struct pch_vbus_gpio_data, irq_work_rise);
> +	struct pch_udc_dev *dev =
> +		container_of(vbus_gpio, struct pch_udc_dev, vbus_gpio);
> +	int vbus;
> +
> +	if (!dev->vbus_gpio.port)
> +		return;
> +
> +	mdelay(PCH_VBUS_INTERVAL);
> +	vbus = pch_vbus_gpio_get_value(dev);
> +
> +	if (vbus == 1) {
> +		dev_dbg(&dev->pdev->dev, "VBUS rose");
> +		pch_udc_reconnect(dev);
> +		return;
> +	}
> +}
> +
> +/**
> + * pch_vbus_gpio_irq() - IRQ handler for GPIO intrerrupt for changing VBUS
> + * @irq:	Interrupt request number
> + * @dev:	Reference to the device structure
> + *
> + * Return codes:
> + *	0: Success
> + *	-EINVAL: GPIO port is invalid or can't be initialized.
> + */
> +static irqreturn_t pch_vbus_gpio_irq(int irq, void *data)
> +{
> +	struct pch_udc_dev *dev = (struct pch_udc_dev *)data;
> +
> +	if (!dev->vbus_gpio.port || !dev->vbus_gpio.intr)
> +		return IRQ_NONE;
> +
> +	if (pch_vbus_gpio_get_value(dev))
> +		schedule_work(&dev->vbus_gpio.irq_work_rise);
> +	else
> +		schedule_work(&dev->vbus_gpio.irq_work_fall);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +/**
>   * pch_vbus_gpio_init() - This API initializes GPIO port detecting VBUS.
>   * @dev:	Reference to the driver structure
>   * @vbus_gpio	Number of GPIO port to detect gpio
> @@ -1317,8 +1374,10 @@ static void pch_vbus_gpio_work_fall(struct work_struct *irq_work)
>  static int pch_vbus_gpio_init(struct pch_udc_dev *dev, int vbus_gpio_port)
>  {
>  	int err;
> +	int irq_num = 0;
>  
>  	dev->vbus_gpio.port = 0;
> +	dev->vbus_gpio.intr = 0;
>  
>  	if (vbus_gpio_port <= -1)
>  		return -EINVAL;
> @@ -1341,6 +1400,21 @@ static int pch_vbus_gpio_init(struct pch_udc_dev *dev, int vbus_gpio_port)
>  	gpio_direction_input(vbus_gpio_port);
>  	INIT_WORK(&dev->vbus_gpio.irq_work_fall, pch_vbus_gpio_work_fall);
>  
> +	irq_num = gpio_to_irq(vbus_gpio_port);
> +	if (irq_num > 0) {
> +		irq_set_irq_type(irq_num, IRQ_TYPE_EDGE_BOTH);
> +		err = request_irq(irq_num, pch_vbus_gpio_irq, 0,
> +			"vbus_detect", dev);
> +		if (!err) {
> +			dev->vbus_gpio.intr = irq_num;
> +			INIT_WORK(&dev->vbus_gpio.irq_work_rise,
> +				pch_vbus_gpio_work_rise);
> +		} else {
> +			pr_err("%s: can't request irq %d, err: %d\n",
> +				__func__, irq_num, err);
> +		}
> +	}
> +
>  	return 0;
>  }
>  
> @@ -1350,6 +1424,9 @@ static int pch_vbus_gpio_init(struct pch_udc_dev *dev, int vbus_gpio_port)
>   */
>  static void pch_vbus_gpio_free(struct pch_udc_dev *dev)
>  {
> +	if (dev->vbus_gpio.intr)
> +		free_irq(dev->vbus_gpio.intr, dev);
> +
>  	if (dev->vbus_gpio.port)
>  		gpio_free(dev->vbus_gpio.port);
>  }
> @@ -2677,6 +2754,7 @@ static void pch_udc_dev_isr(struct pch_udc_dev *dev, u32 dev_intr)
>  			pch_udc_reconnect(dev);
>  		} else if ((dev->vbus_session == 0)
>  			&& (vbus == 1))
> +			&&!dev->vbus_gpio.intr)

This causes a compile error which I have fixed this time. Next time I
will silently drop the patch:

diff --git a/drivers/usb/gadget/pch_udc.c b/drivers/usb/gadget/pch_udc.c
index fb74cc8..a992084 100644
--- a/drivers/usb/gadget/pch_udc.c
+++ b/drivers/usb/gadget/pch_udc.c
@@ -2753,8 +2753,8 @@ static void pch_udc_dev_isr(struct pch_udc_dev *dev, u32 dev_intr)
 			}
 			pch_udc_reconnect(dev);
 		} else if ((dev->vbus_session == 0)
-			&& (vbus == 1))
-			&&!dev->vbus_gpio.intr)
+			&& (vbus == 1)
+			&& !dev->vbus_gpio.intr)
 			schedule_work(&dev->vbus_gpio.irq_work_fall);
 
 		dev_dbg(&dev->pdev->dev, "USB_SUSPEND\n");

-- 
balbi

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

  reply	other threads:[~2012-02-09  7:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-03  7:14 [PATCH 1/3] pch_udc: Detecting VBUS through GPIO Tomoya MORINAGA
2012-02-03  7:14 ` [PATCH 2/3] pch_udc: Detecting VBUS through GPIO with interrupt Tomoya MORINAGA
2012-02-09  7:58   ` Felipe Balbi [this message]
2012-02-09  7:59   ` Felipe Balbi
2012-02-03  7:14 ` [PATCH 3/3] pch_udc: Specifies GPI port number at configuration Tomoya MORINAGA
2012-02-03 10:50   ` Sergei Shtylyov
2012-02-09  7:56     ` Felipe Balbi
2012-02-21  0:27     ` Tomoya MORINAGA

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120209075842.GK1121@legolas.emea.dhcp.ti.com \
    --to=balbi@ti.com \
    --cc=gregkh@suse.de \
    --cc=joel.clark@intel.com \
    --cc=kok.howg.ewe@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=qi.wang@intel.com \
    --cc=tomoya.rohm@gmail.com \
    --cc=yong.y.wang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®