From patchwork Tue Feb 10 09:10:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangfei Gao X-Patchwork-Id: 44528 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ee0-f72.google.com (mail-ee0-f72.google.com [74.125.83.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id BA2602151F for ; Tue, 10 Feb 2015 09:10:43 +0000 (UTC) Received: by mail-ee0-f72.google.com with SMTP id e53sf10963222eek.3 for ; Tue, 10 Feb 2015 01:10:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=exfWvFf6MOK4Z0Xnsptz9k2e7qISVS8i2Wt36FTzQRI=; b=BaTmlqVAVovR89wTTUKSH0PoEWmnROEVpKrk9JrxJLZFs7WhTW5/Dz9DcMdKGmottG WSezSMe8ipfR3DD5N0FlvIwjgXihSgX4/aHyCS7PV1QVrqZuES+vnN/SSFd1OLFTVIPx xh47+Q3vivosRiZnead6o7wS0jqFLxKajYmZO2Igt4xyViP1/2TgifadaiHQODwXg47U AhD54SvLArqqG0I6AEoFyxX1uWAcjkNqt6B9CVU535HHytfpkopb7ST3013lE6EcuQaa oeJO42PTkb8BuECFs/P87yJft2wlFsz5M9TZs07FlK5neLeYRGCzzzpTjxRoofeTb9EW PqOw== X-Gm-Message-State: ALoCoQmA++XLTA2BAcN+FUlJ+H3JivD9SeMWeX4HveTNMdCqzyq7vo5+iL8YThnvU31sjc+Hrpao X-Received: by 10.194.178.234 with SMTP id db10mr2570194wjc.2.1423559443050; Tue, 10 Feb 2015 01:10:43 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.8.167 with SMTP id s7ls669348laa.80.gmail; Tue, 10 Feb 2015 01:10:42 -0800 (PST) X-Received: by 10.112.40.65 with SMTP id v1mr21798987lbk.26.1423559442885; Tue, 10 Feb 2015 01:10:42 -0800 (PST) Received: from mail-lb0-f172.google.com (mail-lb0-f172.google.com. [209.85.217.172]) by mx.google.com with ESMTPS id yd10si11425095lbb.131.2015.02.10.01.10.42 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Feb 2015 01:10:42 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) client-ip=209.85.217.172; Received: by mail-lb0-f172.google.com with SMTP id l4so34922556lbv.3 for ; Tue, 10 Feb 2015 01:10:42 -0800 (PST) X-Received: by 10.112.181.41 with SMTP id dt9mr22373997lbc.56.1423559442744; Tue, 10 Feb 2015 01:10:42 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.35.133 with SMTP id h5csp480859lbj; Tue, 10 Feb 2015 01:10:41 -0800 (PST) X-Received: by 10.70.37.134 with SMTP id y6mr9455797pdj.13.1423559440685; Tue, 10 Feb 2015 01:10:40 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id zi5si25710233pbb.211.2015.02.10.01.10.39; Tue, 10 Feb 2015 01:10:40 -0800 (PST) Received-SPF: none (google.com: devicetree-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753670AbbBJJKi (ORCPT + 5 others); Tue, 10 Feb 2015 04:10:38 -0500 Received: from mail-pd0-f172.google.com ([209.85.192.172]:47006 "EHLO mail-pd0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753583AbbBJJKf (ORCPT ); Tue, 10 Feb 2015 04:10:35 -0500 Received: by pdjy10 with SMTP id y10so7161958pdj.13 for ; Tue, 10 Feb 2015 01:10:35 -0800 (PST) X-Received: by 10.70.102.176 with SMTP id fp16mr36341506pdb.55.1423559435076; Tue, 10 Feb 2015 01:10:35 -0800 (PST) Received: from localhost.localdomain ([210.177.145.249]) by mx.google.com with ESMTPSA id kl11sm18621053pbd.89.2015.02.10.01.10.30 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 10 Feb 2015 01:10:34 -0800 (PST) From: Zhangfei Gao To: balbi@ti.com, john.youn@synopsys.com, Mian Yousaf Kaukab , "dan . zhao" , Wangbinghui , Peter Chen , Sergei Shtylyov Cc: linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org, linux-usb@vger.kernel.org, Zhangfei Gao Subject: [resend PATCH v3 4/4] usb: phy: add phy-hi6220-usb Date: Tue, 10 Feb 2015 17:10:04 +0800 Message-Id: <1423559404-13058-5-git-send-email-zhangfei.gao@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1423559404-13058-1-git-send-email-zhangfei.gao@linaro.org> References: <1423559404-13058-1-git-send-email-zhangfei.gao@linaro.org> Sender: devicetree-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: devicetree@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: zhangfei.gao@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add usb phy controller for hi6220 platform Signed-off-by: Zhangfei Gao --- drivers/usb/phy/Kconfig | 9 ++ drivers/usb/phy/Makefile | 1 + drivers/usb/phy/phy-hi6220-usb.c | 306 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 316 insertions(+) create mode 100644 drivers/usb/phy/phy-hi6220-usb.c diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig index c6d0c8e..405a3d0 100644 --- a/drivers/usb/phy/Kconfig +++ b/drivers/usb/phy/Kconfig @@ -173,6 +173,15 @@ config USB_MXS_PHY MXS Phy is used by some of the i.MX SoCs, for example imx23/28/6x. +config USB_HI6220_PHY + tristate "hi6220 USB PHY support" + select USB_PHY + select MFD_SYSCON + help + Enable this to support the HISILICON HI6220 USB PHY. + + To compile this driver as a module, choose M here. + config USB_RCAR_PHY tristate "Renesas R-Car USB PHY support" depends on USB || USB_GADGET diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile index 75f2bba..00172d3 100644 --- a/drivers/usb/phy/Makefile +++ b/drivers/usb/phy/Makefile @@ -18,6 +18,7 @@ obj-$(CONFIG_SAMSUNG_USBPHY) += phy-samsung-usb.o obj-$(CONFIG_TWL6030_USB) += phy-twl6030-usb.o obj-$(CONFIG_USB_EHCI_TEGRA) += phy-tegra-usb.o obj-$(CONFIG_USB_GPIO_VBUS) += phy-gpio-vbus-usb.o +obj-$(CONFIG_USB_HI6220_PHY) += phy-hi6220-usb.o obj-$(CONFIG_USB_ISP1301) += phy-isp1301.o obj-$(CONFIG_USB_MSM_OTG) += phy-msm-usb.o obj-$(CONFIG_USB_MV_OTG) += phy-mv-usb.o diff --git a/drivers/usb/phy/phy-hi6220-usb.c b/drivers/usb/phy/phy-hi6220-usb.c new file mode 100644 index 0000000..efb4bbb --- /dev/null +++ b/drivers/usb/phy/phy-hi6220-usb.c @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2015 Linaro Ltd. + * Copyright (c) 2015 Hisilicon Limited. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define SC_PERIPH_CTRL4 0x00c + +#define CTRL4_PICO_SIDDQ BIT(6) +#define CTRL4_PICO_OGDISABLE BIT(8) +#define CTRL4_PICO_VBUSVLDEXT BIT(10) +#define CTRL4_PICO_VBUSVLDEXTSEL BIT(11) +#define CTRL4_OTG_PHY_SEL BIT(21) + +#define SC_PERIPH_CTRL5 0x010 + +#define CTRL5_USBOTG_RES_SEL BIT(3) +#define CTRL5_PICOPHY_ACAENB BIT(4) +#define CTRL5_PICOPHY_BC_MODE BIT(5) +#define CTRL5_PICOPHY_CHRGSEL BIT(6) +#define CTRL5_PICOPHY_VDATSRCEND BIT(7) +#define CTRL5_PICOPHY_VDATDETENB BIT(8) +#define CTRL5_PICOPHY_DCDENB BIT(9) +#define CTRL5_PICOPHY_IDDIG BIT(10) + +#define SC_PERIPH_CTRL8 0x018 +#define SC_PERIPH_RSTEN0 0x300 +#define SC_PERIPH_RSTDIS0 0x304 + +#define RST0_USBOTG_BUS BIT(4) +#define RST0_POR_PICOPHY BIT(5) +#define RST0_USBOTG BIT(6) +#define RST0_USBOTG_32K BIT(7) + +#define EYE_PATTERN_PARA 0x7053348c + +struct hi6220_priv { + struct usb_phy phy; + struct delayed_work work; + struct regmap *reg; + struct clk *clk; + struct regulator *vcc; + struct device *dev; + int gpio_vbus; + int gpio_id; + enum usb_otg_state state; +}; + +static void hi6220_start_periphrals(struct hi6220_priv *priv, bool on) +{ + struct usb_otg *otg = priv->phy.otg; + + if (!otg->gadget) + return; + + if (on) + usb_gadget_connect(otg->gadget); + else + usb_gadget_disconnect(otg->gadget); +} + +static void hi6220_detect_work(struct work_struct *work) +{ + struct hi6220_priv *priv = + container_of(work, struct hi6220_priv, work.work); + int gpio_id, gpio_vbus; + enum usb_otg_state state; + + if (!gpio_is_valid(priv->gpio_id) || !gpio_is_valid(priv->gpio_vbus)) + return; + + gpio_id = gpio_get_value_cansleep(priv->gpio_id); + gpio_vbus = gpio_get_value_cansleep(priv->gpio_vbus); + + if (gpio_vbus == 0) { + if (gpio_id == 1) + state = OTG_STATE_B_PERIPHERAL; + else + state = OTG_STATE_A_HOST; + } else { + state = OTG_STATE_A_HOST; + } + + if (priv->state != state) { + hi6220_start_periphrals(priv, state == OTG_STATE_B_PERIPHERAL); + priv->state = state; + } +} + +static irqreturn_t hiusb_gpio_intr(int irq, void *data) +{ + struct hi6220_priv *priv = (struct hi6220_priv *)data; + + /* add debounce time */ + schedule_delayed_work(&priv->work, msecs_to_jiffies(100)); + return IRQ_HANDLED; +} + +static int hi6220_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget) +{ + otg->gadget = gadget; + return 0; +} + +static int hi6220_phy_setup(struct hi6220_priv *priv, bool on) +{ + struct regmap *reg = priv->reg; + u32 val, mask; + int ret; + + if (priv->reg == NULL) + return 0; + + if (on) { + val = RST0_USBOTG_BUS | RST0_POR_PICOPHY | + RST0_USBOTG | RST0_USBOTG_32K; + mask = val; + ret = regmap_update_bits(reg, SC_PERIPH_RSTDIS0, mask, val); + if (ret) + goto out; + + val = CTRL5_USBOTG_RES_SEL | CTRL5_PICOPHY_ACAENB; + mask = val | CTRL5_PICOPHY_BC_MODE; + ret = regmap_update_bits(reg, SC_PERIPH_CTRL5, mask, val); + if (ret) + goto out; + + val = CTRL4_PICO_VBUSVLDEXT | CTRL4_PICO_VBUSVLDEXTSEL | + CTRL4_OTG_PHY_SEL; + mask = val | CTRL4_PICO_SIDDQ | CTRL4_PICO_OGDISABLE; + ret = regmap_update_bits(reg, SC_PERIPH_CTRL4, mask, val); + if (ret) + goto out; + + ret = regmap_write(reg, SC_PERIPH_CTRL8, EYE_PATTERN_PARA); + if (ret) + goto out; + } else { + val = CTRL4_PICO_SIDDQ; + mask = val; + ret = regmap_update_bits(reg, SC_PERIPH_CTRL4, mask, val); + if (ret) + goto out; + + val = RST0_USBOTG_BUS | RST0_POR_PICOPHY | + RST0_USBOTG | RST0_USBOTG_32K; + mask = val; + ret = regmap_update_bits(reg, SC_PERIPH_RSTEN0, mask, val); + if (ret) + goto out; + } + + return 0; +out: + dev_err(priv->dev, "failed to setup phy ret: %d\n", ret); + return ret; +} + +static int hi6220_phy_probe(struct platform_device *pdev) +{ + struct hi6220_priv *priv; + struct usb_otg *otg; + struct device_node *np = pdev->dev.of_node; + struct device *dev = &pdev->dev; + int ret, irq; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + otg = devm_kzalloc(dev, sizeof(*otg), GFP_KERNEL); + if (!otg) + return -ENOMEM; + + priv->dev = dev; + priv->phy.dev = &pdev->dev; + priv->phy.otg = otg; + priv->phy.label = "hi6220"; + priv->phy.type = USB_PHY_TYPE_USB2; + otg->set_peripheral = hi6220_set_peripheral; + platform_set_drvdata(pdev, priv); + + priv->gpio_vbus = of_get_named_gpio(np, "hisilicon,gpio-vbus", 0); + if (priv->gpio_vbus == -EPROBE_DEFER) + return -EPROBE_DEFER; + if (!gpio_is_valid(priv->gpio_vbus)) { + dev_err(dev, "invalid gpio %d\n", priv->gpio_vbus); + return -ENODEV; + } + + priv->gpio_id = of_get_named_gpio(np, "hisilicon,gpio-id", 0); + if (priv->gpio_id == -EPROBE_DEFER) + return -EPROBE_DEFER; + if (!gpio_is_valid(priv->gpio_id)) { + dev_err(dev, "invalid gpio %d\n", priv->gpio_id); + return -ENODEV; + } + + priv->reg = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, + "hisilicon,peripheral-syscon"); + if (IS_ERR(priv->reg)) + priv->reg = NULL; + + ret = devm_gpio_request_one(dev, priv->gpio_vbus, + GPIOF_IN, "gpio_vbus"); + if (ret < 0) { + dev_err(dev, "gpio request failed for gpio_vbus\n"); + return ret; + } + + ret = devm_gpio_request_one(dev, priv->gpio_id, GPIOF_IN, "gpio_id"); + if (ret < 0) { + dev_err(dev, "gpio request failed for gpio_id\n"); + return ret; + } + + priv->vcc = devm_regulator_get(dev, "vcc"); + if (IS_ERR(priv->vcc)) { + if (PTR_ERR(priv->vcc) == -EPROBE_DEFER) + return -EPROBE_DEFER; + dev_info(dev, "No regulator found\n"); + } else { + ret = regulator_enable(priv->vcc); + if (ret) { + dev_err(dev, "Failed to enable regulator\n"); + return -ENODEV; + } + } + + priv->clk = devm_clk_get(dev, NULL); + if (IS_ERR(priv->clk)) { + regulator_disable(priv->vcc); + return PTR_ERR(priv->clk); + } + clk_prepare_enable(priv->clk); + INIT_DELAYED_WORK(&priv->work, hi6220_detect_work); + + irq = gpio_to_irq(priv->gpio_vbus); + ret = devm_request_irq(dev, gpio_to_irq(priv->gpio_vbus), + hiusb_gpio_intr, IRQF_NO_SUSPEND | + IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, + "vbus_gpio_intr", priv); + if (ret) { + dev_err(dev, "request gpio irq failed.\n"); + goto err_irq; + } + + hi6220_phy_setup(priv, true); + ret = usb_add_phy_dev(&priv->phy); + if (ret) { + dev_err(dev, "Can't register transceiver\n"); + goto err_irq; + } + schedule_delayed_work(&priv->work, 0); + + return 0; +err_irq: + cancel_delayed_work_sync(&priv->work); + clk_disable_unprepare(priv->clk); + regulator_disable(priv->vcc); + return ret; +} + +static int hi6220_phy_remove(struct platform_device *pdev) +{ + struct hi6220_priv *priv = platform_get_drvdata(pdev); + + cancel_delayed_work_sync(&priv->work); + hi6220_phy_setup(priv, false); + clk_disable_unprepare(priv->clk); + regulator_disable(priv->vcc); + return 0; +} + +static const struct of_device_id hi6220_phy_of_match[] = { + {.compatible = "hisilicon,hi6220-usb-phy",}, + { }, +}; +MODULE_DEVICE_TABLE(of, hi6220_phy_of_match); + +static struct platform_driver hi6220_phy_driver = { + .probe = hi6220_phy_probe, + .remove = hi6220_phy_remove, + .driver = { + .name = "hi6220-usb-phy", + .of_match_table = hi6220_phy_of_match, + } +}; +module_platform_driver(hi6220_phy_driver); + +MODULE_DESCRIPTION("HISILICON HI6220 USB PHY driver"); +MODULE_ALIAS("platform:hi6220-usb-phy"); +MODULE_LICENSE("GPL");