Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933248AbcKHLQ0 (ORCPT ); Tue, 8 Nov 2016 06:16:26 -0500 Received: from relmlor2.renesas.com ([210.160.252.172]:23505 "EHLO relmlie1.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932607AbcKHLQX (ORCPT ); Tue, 8 Nov 2016 06:16:23 -0500 X-IronPort-AV: E=Sophos;i="5.31,609,1473087600"; d="scan'";a="225396672" Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=<>; From: Yoshihiro Shimoda To: CC: , , , , , , Yoshihiro Shimoda Subject: [PATCH v4] phy: rcar-gen3-usb2: add sysfs for usb role swap Date: Tue, 8 Nov 2016 20:14:58 +0900 Message-ID: <1478603698-21133-1-git-send-email-yoshihiro.shimoda.uh@renesas.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [211.11.155.144] X-ClientProxiedBy: TY1PR0201CA0019.apcprd02.prod.outlook.com (10.164.90.157) To HK2PR06MB0548.apcprd06.prod.outlook.com (10.161.183.153) X-MS-Office365-Filtering-Correlation-Id: 0ee3f821-5ab7-46fe-51c5-08d407c8a957 X-Microsoft-Exchange-Diagnostics: 1;HK2PR06MB0548;2:Wyr3sZDaGnqHd0OQCRGcm4ZjvHZWcPmFYLBPmvmD0EGmxvXc41fhy6gxwmVcHb5C23Soom7xFlj0LeX+zNxlKO3GMA9gx3437DZAZ98fsLvsTEwsTxmiVIe2ZdTm9wRC0SRvByxJ5h2BXjfUV/6jINMF41iMQWBJC079i2kE4gkaQDIjNeJiUo66C/P7rMbg8xrDiw+30oeV0pnTfH7Uuw==;3:tdS3i6oPVdSvif2uSe5gPxgkSlfMLBoZn/1SlX3ZmilU2URO8gPgG/fp4pK9K/dgec+qjvGZfUGEFkJGDAEq7E9mDKP9ijr6lTcIxClQUYnRko9DqGh1G/QN/diY3nlzZdN2JEv7n8KN8qOrC/6Hig==;25:qm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T5uIUWNsdjqO/87ORKeuj0DgjgzirSIskqv09PlQmQLjWAbFv2DRYQ545o2Ol3ZDqNEg3e5PUwnXjbmeU0rTtDuP55J+MwFPb2DZbjzjy6+30weKtfKy1RzQObyWcdH3lwlatpnJwJWzxpWJuvUwKLb9x62+ad/FCWWWZuqYzckDtP0rKo4In9A9htBAHupuzp5tSfjYtIAUoLYMgFCtTbMTxz6sNs20FOGdzExB X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(21532816269658); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026);SRVR:HK2PR06MB0548;BCL:0;PCL:0;RULEID:;SRVR:HK2PR06MB0548; X-Microsoft-Exchange-Diagnostics: 1;HK2PR06MB0548;4:+fUpltz5z9N9DMx+vFP3U4mrh0BDnWgqYCmNTHhtvPduFAm8fzSnIRmYhZnV6pCnGE70rj0uHJXlF0hzh4BhaR0C8B3O41a6wWxxA7tOTcJe8O5BxqjmmoJuZhgdeMxedBnixE+JZeFmx87GE6o54dWp2hHzZi0/Y2oJ7o+VWhHJBw6a2yzi4JV7rW+4Kso3xHBt6NYnSSKHV6o9wQwYammZudmkWrdJW5PV4dVXYUX3yOhG/AxH7OkhqjFEPhWhgDM5NziBfI1GDpBRM4I0W4MimjxzOjKgMQfilHRrCUzUS9bywv/U6M5IKzOnVZC1xMKQWO2grxdeu0VZYWevJlqxOGJCYfty0CVQ8H0ZYqSWzzR/J+U4P24OCtf+RDBmTXVqZkn+e01IzPjE8BYWzDLHN80f8sk711HL3oSzo8DwgyBgY8Vw5Bua658xr4TNzGowLZXR+iYoxym1loer1w== X-Forefront-PRVS: 01208B1E18 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6009001)(6069001)(7916002)(199003)(189002)(50944005)(2906002)(50226002)(50986999)(5660300001)(110136003)(68736007)(81166006)(81156014)(50466002)(77096005)(92566002)(48376002)(8676002)(5003940100001)(33646002)(6116002)(101416001)(3846002)(230783001)(586003)(6916009)(4326007)(78352004)(97736004)(7846002)(42186005)(189998001)(7736002)(305945005)(6666003)(8666005)(107886002)(106356001)(66066001)(36756003)(105586002)(4001430100002)(229853001)(42882006)(2351001)(47776003)(7059030);DIR:OUT;SFP:1102;SCL:1;SRVR:HK2PR06MB0548;H:localhost.localdomain;FPR:;SPF:None;PTR:InfoNoRecords;A:0;MX:0;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;HK2PR06MB0548;23:ncX6fkwW5EGl0iCtqZrUcHQ5nzvFJ+MccRW2OzLSt?= =?us-ascii?Q?7/NtSViP5lLOxuYH5JfocJop0fl0VsJktz64F0OA144y+AJDQF2YQUL0+w52?= =?us-ascii?Q?4Rs30Wvo5ujMFENyutcPxPO0KCPgdQbaPUTEPZPNjKGRTUEPHcm06HjKYQWp?= =?us-ascii?Q?vgved4pX3pTdE9MmmH7HsKqj/xMgqpmcOzaiS51Ibfa58z+0tFVXY7sd/EZD?= =?us-ascii?Q?YoY90rUXYyxc/kTECUUwfb6T33rK6a/PqFr+YEDtGUlij5PcXZ1oYDbjYbku?= =?us-ascii?Q?7roU30zaINMzEfSeiabwZT1ik3M8GBwkYy2azSaXk2i+IA3CLnd6Zsyz2wVX?= =?us-ascii?Q?r/ZA7RXykTS/gU3iRwT2MylXNQWieyAvAB/D7lbfsIFbZpoD42RyLMz/dh35?= =?us-ascii?Q?mjDBeZmMTpPjAiY2rtbSDqOJsRRgFX2Nyosp+3w/C9YFGOtqrOuiGJvmRj67?= =?us-ascii?Q?WyoWGGFFAZCJ9vpPaNtSnlPa5K+5vdQGGrc71laxTCGzJypYo5qvgejwKBvc?= =?us-ascii?Q?qIhMgUGpENe+FSHSatiWKSKshcNMIsCR+otUVXzbRvkJpjZP6CnBD/eBtEPz?= =?us-ascii?Q?b4P9eLV7DV/2/eAYEAyz1Fer36nOboPNHhuaLkMbW4GzeFSp1jXzAFHVFfN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88e1RZMZans0OX+CGWBxcg1HgUi851wcHIKbgZecI0kg4Ag=;5:hwYbObMMMtAp+fSSg7pLBdktNkk8taf1WiunzRP3NXxgkcIKNm9ke2EfmaFjpPknTLhPsvqjwwp2WCJLCJJ3WCgh+1vPfg6E645d+Q5h+KkQ2a+e5lZ2L0OMhgfXwMFjmmqacdAdlV0WqsizcgRGyQ==;24:oLuVzJZhmshHSG61M70pk4sH4cpzTtqTzzduoD9rLMyqmRrvC4LPc1CAzVtwTxq7EeDCDgiIkCNV1p1/l1FaHj0cc/7xzaaEPImXzPuekCo= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;HK2PR06MB0548;7:D4EbGmEKGNkti7mTO7+bGBDo4wDIT4sZHe2FHZyuBNykix0RzSV+d3xh/GaApjRjsB2pWG3eJ+Q+ygFgiX8prkHEQdWUAntVN4MWQIHTHC0pM9P21IdiL97b9gqlrMJW1cUea49+ZvLroAdWXEva1Xk8mN5xdTqluVXxOuwkXROd+ys1IQxMPz+gtUJD2YfVByj6h6mJkh+qANE4godubfMsS3h29HaKi5WkJsnnE+tTMNNf62UBP1700NcKSnSWUQwGu/HZEh0Q7pgeIjc4mIEnZ+JO1r9kRkayC+o1yfVXGUNyzjkXYWeneQjhneSUPheK+WSa6ZT5nx8Z14m08Kpas8iAWdBKN5SRvfBeyuI=;20:u/9aNW8TZ2WpCQ8ZmG1jl8puXtgYKdwHH4oyo5nNcQ2+4yaqoLWzZG/W1uAvsV7RokXhJTqOwBTMeyh1gqNJrOyU7fQr8abgK2OfPvsKgw1r97aWWDbL8vdsAyS7BOqwwA2NhjXZS4cNmdvaucdEt80DtVxjJJHjoqJBKujtS4k= X-OriginatorOrg: renesas.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2016 11:16:17.5853 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HK2PR06MB0548 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 7291 Lines: 228 This patch adds sysfs "role" for usb role swap. This parameter can be read and write. If you use this file as the following, you can swap the usb role. For example: 1) Connect a usb cable using 2 Salvator-x boards 2) On A-Device (ID pin is low), you input the following command: # echo peripheral > /sys/devices/platform/soc/ee080200.usb-phy/role 3) On B-Device (ID pin is high), you input the following command: # echo host > /sys/devices/platform/soc/ee080200.usb-phy/role Then, the A-device acts as a peripheral and the B-device acts as a host. Please note that A-Device must input the following command if you want the board to act as a host again. (even if you disconnect the usb cable, since id state may be the same, the A-Device keeps to act as peripheral.) # echo host > /sys/devices/platform/soc/ee080200.usb-phy/role Signed-off-by: Yoshihiro Shimoda --- This patch is based on the latest linux-phy.git / next branch. (commit id = 7809cd2ce6abd4f431e4b14e6b1276a7cc842ac4) Since this patch is related to usb, I added email addresses of Greg, Felipe, Peter and USB ML as CC. (This patch doesn't use USB OTG FSM though.) Changes from v3: - Clean up redundant conditions in role_store(). Changes from v2: - Modify the sysfs file name to "role", and the argument is "host" or "peripheral". Peter suggested this. Thank you! Changes from v1: - rebase the latest next branch. .../ABI/testing/sysfs-platform-phy-rcar-gen3-usb2 | 15 +++ drivers/phy/phy-rcar-gen3-usb2.c | 118 ++++++++++++++++++++- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-platform-phy-rcar-gen3-usb2 diff --git a/Documentation/ABI/testing/sysfs-platform-phy-rcar-gen3-usb2 b/Documentation/ABI/testing/sysfs-platform-phy-rcar-gen3-usb2 new file mode 100644 index 0000000..6212697 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-platform-phy-rcar-gen3-usb2 @@ -0,0 +1,15 @@ +What: /sys/devices/platform//role +Date: October 2016 +KernelVersion: 4.10 +Contact: Yoshihiro Shimoda +Description: + This file can be read and write. + The file can show/change the phy mode for role swap of usb. + + Write the following strings to change the mode: + "host" - switching mode from peripheral to host. + "peripheral" - switching mode from host to peripheral. + + Read the file, then it shows the following strings: + "host" - The mode is host now. + "peripheral" - The mode is peripheral now. diff --git a/drivers/phy/phy-rcar-gen3-usb2.c b/drivers/phy/phy-rcar-gen3-usb2.c index 3d97ead..3f712b4 100644 --- a/drivers/phy/phy-rcar-gen3-usb2.c +++ b/drivers/phy/phy-rcar-gen3-usb2.c @@ -70,6 +70,7 @@ #define USB2_LINECTRL1_DP_RPD BIT(18) #define USB2_LINECTRL1_DMRPD_EN BIT(17) #define USB2_LINECTRL1_DM_RPD BIT(16) +#define USB2_LINECTRL1_OPMODE_NODRV BIT(6) /* ADPCTRL */ #define USB2_ADPCTRL_OTGSESSVLD BIT(20) @@ -161,6 +162,43 @@ static void rcar_gen3_init_for_peri(struct rcar_gen3_chan *ch) schedule_work(&ch->work); } +static void rcar_gen3_init_for_b_host(struct rcar_gen3_chan *ch) +{ + void __iomem *usb2_base = ch->base; + u32 val; + + val = readl(usb2_base + USB2_LINECTRL1); + writel(val | USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1); + + rcar_gen3_set_linectrl(ch, 1, 1); + rcar_gen3_set_host_mode(ch, 1); + rcar_gen3_enable_vbus_ctrl(ch, 0); + + val = readl(usb2_base + USB2_LINECTRL1); + writel(val & ~USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1); +} + +static void rcar_gen3_init_for_a_peri(struct rcar_gen3_chan *ch) +{ + rcar_gen3_set_linectrl(ch, 0, 1); + rcar_gen3_set_host_mode(ch, 0); + rcar_gen3_enable_vbus_ctrl(ch, 1); +} + +static void rcar_gen3_init_from_a_peri_to_a_host(struct rcar_gen3_chan *ch) +{ + void __iomem *usb2_base = ch->base; + u32 val; + + val = readl(usb2_base + USB2_OBINTEN); + writel(val & ~USB2_OBINT_BITS, usb2_base + USB2_OBINTEN); + + rcar_gen3_enable_vbus_ctrl(ch, 0); + rcar_gen3_init_for_host(ch); + + writel(val | USB2_OBINT_BITS, usb2_base + USB2_OBINTEN); +} + static bool rcar_gen3_check_id(struct rcar_gen3_chan *ch) { return !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG); @@ -174,6 +212,65 @@ static void rcar_gen3_device_recognition(struct rcar_gen3_chan *ch) rcar_gen3_init_for_peri(ch); } +static bool rcar_gen3_is_host(struct rcar_gen3_chan *ch) +{ + return !(readl(ch->base + USB2_COMMCTRL) & USB2_COMMCTRL_OTG_PERI); +} + +static ssize_t role_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct rcar_gen3_chan *ch = dev_get_drvdata(dev); + bool is_b_device, is_host, new_mode_is_host; + + if (!ch->has_otg || !ch->phy->init_count) + return -EIO; + + /* + * is_b_device: true is B-Device. false is A-Device. + * If {new_mode_}is_host: true is Host mode. false is Peripheral mode. + */ + is_b_device = rcar_gen3_check_id(ch); + is_host = rcar_gen3_is_host(ch); + if (!strncmp(buf, "host", strlen("host"))) + new_mode_is_host = true; + else if (!strncmp(buf, "peripheral", strlen("peripheral"))) + new_mode_is_host = false; + else + return -EINVAL; + + /* If current and new mode is the same, this returns the error */ + if (is_host == new_mode_is_host) + return -EINVAL; + + if (new_mode_is_host) { /* And is_host must be false */ + if (!is_b_device) /* A-Peripheral */ + rcar_gen3_init_from_a_peri_to_a_host(ch); + if (is_b_device) /* B-Peripheral */ + rcar_gen3_init_for_b_host(ch); + } else { /* And is_host must be true */ + if (!is_b_device) /* A-Host */ + rcar_gen3_init_for_a_peri(ch); + if (is_b_device) /* B-Host */ + rcar_gen3_init_for_peri(ch); + } + + return count; +} + +static ssize_t role_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct rcar_gen3_chan *ch = dev_get_drvdata(dev); + + if (!ch->has_otg || !ch->phy->init_count) + return -EIO; + + return sprintf(buf, "%s\n", rcar_gen3_is_host(ch) ? "host" : + "peripheral"); +} +static DEVICE_ATTR_RW(role); + static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch) { void __iomem *usb2_base = ch->base; @@ -351,21 +448,40 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) channel->vbus = NULL; } + platform_set_drvdata(pdev, channel); phy_set_drvdata(channel->phy, channel); provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); - if (IS_ERR(provider)) + if (IS_ERR(provider)) { dev_err(dev, "Failed to register PHY provider\n"); + } else if (channel->has_otg) { + int ret; + + ret = device_create_file(dev, &dev_attr_role); + if (ret < 0) + return ret; + } return PTR_ERR_OR_ZERO(provider); } +static int rcar_gen3_phy_usb2_remove(struct platform_device *pdev) +{ + struct rcar_gen3_chan *channel = platform_get_drvdata(pdev); + + if (channel->has_otg) + device_remove_file(&pdev->dev, &dev_attr_role); + + return 0; +}; + static struct platform_driver rcar_gen3_phy_usb2_driver = { .driver = { .name = "phy_rcar_gen3_usb2", .of_match_table = rcar_gen3_phy_usb2_match_table, }, .probe = rcar_gen3_phy_usb2_probe, + .remove = rcar_gen3_phy_usb2_remove, }; module_platform_driver(rcar_gen3_phy_usb2_driver); -- 1.9.1