All of lore.kernel.org
 help / color / mirror / Atom feed
From: chao.xie@marvell.com (Chao Xie)
To: linux-arm-kernel@lists.infradead.org
Subject: [V9 PATCH 01/12] usb: phy: protect phy init and shutdown for mutiple deivces
Date: Wed, 24 Apr 2013 02:23:15 -0400	[thread overview]
Message-ID: <1366784606-1813-2-git-send-email-chao.xie@marvell.com> (raw)
In-Reply-To: <1366784606-1813-1-git-send-email-chao.xie@marvell.com>

Some USB devices will share same phy, so make the ->init
and ->shutdown to be protected.
Only first device will initialize the phy, and only last device
can shutdown phy.

Signed-off-by: Chao Xie <chao.xie@marvell.com>
---
 drivers/usb/phy/phy.c   |    6 ++++++
 include/linux/usb/phy.h |   22 ++++++++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index f52c006..168332b 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -329,6 +329,9 @@ int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
 		return -EINVAL;
 	}
 
+	mutex_init(&x->phy_mutex);
+	x->refcount = 0;
+
 	spin_lock_irqsave(&phy_lock, flags);
 
 	list_for_each_entry(phy, &phy_list, head) {
@@ -367,6 +370,9 @@ int usb_add_phy_dev(struct usb_phy *x)
 		return -EINVAL;
 	}
 
+	mutex_init(&x->phy_mutex);
+	x->refcount = 0;
+
 	spin_lock_irqsave(&phy_lock, flags);
 	list_for_each_entry(phy_bind, &phy_bind_list, list)
 		if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 6b5978f..98d7e60 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -87,6 +87,14 @@ struct usb_phy {
 	/* to support controllers that have multiple transceivers */
 	struct list_head	head;
 
+	/*
+	 * PHY may be shared by multiple devices.
+	 * mutex and refcount are used to make sure PHY only initialize or
+	 * shutdown once.
+	 */
+	struct mutex		phy_mutex;
+	unsigned int		refcount;
+
 	/* initialize/shutdown the OTG controller */
 	int	(*init)(struct usb_phy *x);
 	void	(*shutdown)(struct usb_phy *x);
@@ -150,17 +158,23 @@ static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
 static inline int
 usb_phy_init(struct usb_phy *x)
 {
-	if (x->init)
-		return x->init(x);
+	int ret = 0;
 
-	return 0;
+	mutex_lock(&x->phy_mutex);
+	if (x->refcount++ == 0 && x->init)
+		ret = x->init(x);
+	mutex_unlock(&x->phy_mutex);
+
+	return ret;
 }
 
 static inline void
 usb_phy_shutdown(struct usb_phy *x)
 {
-	if (x->shutdown)
+	mutex_lock(&x->phy_mutex);
+	if (--x->refcount == 0 && x->shutdown)
 		x->shutdown(x);
+	mutex_unlock(&x->phy_mutex);
 }
 
 static inline int
-- 
1.7.4.1

  reply	other threads:[~2013-04-24  6:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-24  6:23 [V9 PATCH 00/12] mv-usb phy driver Chao Xie
2013-04-24  6:23 ` Chao Xie [this message]
2013-04-29 18:24   ` [V9 PATCH 01/12] usb: phy: protect phy init and shutdown for mutiple deivces Felipe Balbi
2013-04-29 19:26     ` Russell King - ARM Linux
2013-05-06  7:22     ` Chao Xie
2013-04-24  6:23 ` [V9 PATCH 02/12] usb: phy: mv_usb2: add PHY driver for marvell usb2 controller Chao Xie
2013-04-24  6:23 ` [V9 PATCH 03/12] usb: gadget: mv_udc: use PHY driver for udc Chao Xie
2013-04-24  6:23 ` [V9 PATCH 04/12] usb: ehci: ehci-mv: use PHY driver for ehci Chao Xie
2013-04-24  6:23 ` [V9 PATCH 05/12] usb: phy: phy-mv-usb: use USB2 PHY driver for otg Chao Xie
2013-04-24  6:23 ` [V9 PATCH 06/12] usb: mv_u3d: usb phy drivers for phy operation Chao Xie
2013-04-24  6:23 ` [V9 PATCH 07/12] arm: mmp2: change the defintion of usb devices Chao Xie
2013-04-24  6:23 ` [V9 PATCH 08/12] arm: pxa910: " Chao Xie
2013-04-24  6:23 ` [V9 PATCH 09/12] arm: ttc_dkb: modify usb support Chao Xie
2013-04-24  6:23 ` [V9 PATCH 10/12] arm: mmp: remove unused usb devices Chao Xie
2013-04-24  6:23 ` [V9 PATCH 11/12] arm: brownstone: add usb support for the board Chao Xie
2013-04-24  6:23 ` [V9 PATCH 12/12] usb: mv_usb: remove the phy callbacks in pdata Chao Xie
  -- strict thread matches above, loose matches on Subject: below --
2013-04-07 10:29 [V9 PATCH 00/12] mv-usb phy driver Chao Xie
2013-04-07 10:29 ` [V9 PATCH 01/12] usb: phy: protect phy init and shutdown for mutiple deivces Chao Xie

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=1366784606-1813-2-git-send-email-chao.xie@marvell.com \
    --to=chao.xie@marvell.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.