2014-03-01 21:09:26 +04:00
|
|
|
/*
|
|
|
|
* Allwinner sun4i USB phy driver
|
|
|
|
*
|
2015-06-13 15:37:45 +03:00
|
|
|
* Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
|
2014-03-01 21:09:26 +04:00
|
|
|
*
|
|
|
|
* Based on code from
|
|
|
|
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
|
|
|
|
*
|
|
|
|
* Modelled after: Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
|
|
|
|
* Copyright (C) 2013 Samsung Electronics Co., Ltd.
|
|
|
|
* Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
2014-05-29 10:30:49 +04:00
|
|
|
#include <linux/err.h>
|
2015-06-13 15:37:46 +03:00
|
|
|
#include <linux/extcon.h>
|
2014-03-01 21:09:26 +04:00
|
|
|
#include <linux/io.h>
|
2015-06-13 15:37:45 +03:00
|
|
|
#include <linux/interrupt.h>
|
2014-03-01 21:09:26 +04:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_address.h>
|
2015-06-13 15:37:45 +03:00
|
|
|
#include <linux/of_gpio.h>
|
2014-03-01 21:09:26 +04:00
|
|
|
#include <linux/phy/phy.h>
|
2015-03-29 13:50:46 +03:00
|
|
|
#include <linux/phy/phy-sun4i-usb.h>
|
2014-03-01 21:09:26 +04:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/regulator/consumer.h>
|
|
|
|
#include <linux/reset.h>
|
2015-06-13 15:37:45 +03:00
|
|
|
#include <linux/workqueue.h>
|
2014-03-01 21:09:26 +04:00
|
|
|
|
|
|
|
#define REG_ISCR 0x00
|
|
|
|
#define REG_PHYCTL 0x04
|
|
|
|
#define REG_PHYBIST 0x08
|
|
|
|
#define REG_PHYTUNE 0x0c
|
|
|
|
|
|
|
|
#define PHYCTL_DATA BIT(7)
|
|
|
|
|
|
|
|
#define SUNXI_AHB_ICHR8_EN BIT(10)
|
|
|
|
#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
|
|
|
|
#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
|
|
|
|
#define SUNXI_ULPI_BYPASS_EN BIT(0)
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
/* ISCR, Interface Status and Control bits */
|
|
|
|
#define ISCR_ID_PULLUP_EN (1 << 17)
|
|
|
|
#define ISCR_DPDM_PULLUP_EN (1 << 16)
|
|
|
|
/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
|
|
|
|
#define ISCR_FORCE_ID_MASK (3 << 14)
|
|
|
|
#define ISCR_FORCE_ID_LOW (2 << 14)
|
|
|
|
#define ISCR_FORCE_ID_HIGH (3 << 14)
|
|
|
|
#define ISCR_FORCE_VBUS_MASK (3 << 12)
|
|
|
|
#define ISCR_FORCE_VBUS_LOW (2 << 12)
|
|
|
|
#define ISCR_FORCE_VBUS_HIGH (3 << 12)
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
/* Common Control Bits for Both PHYs */
|
|
|
|
#define PHY_PLL_BW 0x03
|
|
|
|
#define PHY_RES45_CAL_EN 0x0c
|
|
|
|
|
|
|
|
/* Private Control Bits for Each PHY */
|
|
|
|
#define PHY_TX_AMPLITUDE_TUNE 0x20
|
|
|
|
#define PHY_TX_SLEWRATE_TUNE 0x22
|
|
|
|
#define PHY_VBUSVALID_TH_SEL 0x25
|
|
|
|
#define PHY_PULLUP_RES_SEL 0x27
|
|
|
|
#define PHY_OTG_FUNC_EN 0x28
|
|
|
|
#define PHY_VBUS_DET_EN 0x29
|
|
|
|
#define PHY_DISCON_TH_SEL 0x2a
|
2015-03-29 13:50:46 +03:00
|
|
|
#define PHY_SQUELCH_DETECT 0x3c
|
2014-03-01 21:09:26 +04:00
|
|
|
|
|
|
|
#define MAX_PHYS 3
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
/*
|
|
|
|
* Note do not raise the debounce time, we must report Vusb high within 100ms
|
|
|
|
* otherwise we get Vbus errors
|
|
|
|
*/
|
|
|
|
#define DEBOUNCE_TIME msecs_to_jiffies(50)
|
|
|
|
#define POLL_TIME msecs_to_jiffies(250)
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
struct sun4i_usb_phy_data {
|
|
|
|
void __iomem *base;
|
|
|
|
struct mutex mutex;
|
|
|
|
int num_phys;
|
|
|
|
u32 disc_thresh;
|
|
|
|
struct sun4i_usb_phy {
|
|
|
|
struct phy *phy;
|
|
|
|
void __iomem *pmu;
|
|
|
|
struct regulator *vbus;
|
|
|
|
struct reset_control *reset;
|
2014-05-13 19:44:18 +04:00
|
|
|
struct clk *clk;
|
2015-06-13 15:37:45 +03:00
|
|
|
bool regulator_on;
|
2014-03-01 21:09:26 +04:00
|
|
|
int index;
|
|
|
|
} phys[MAX_PHYS];
|
2015-06-13 15:37:45 +03:00
|
|
|
/* phy0 / otg related variables */
|
2015-06-13 15:37:46 +03:00
|
|
|
struct extcon_dev *extcon;
|
2015-06-13 15:37:45 +03:00
|
|
|
bool phy0_init;
|
|
|
|
bool phy0_poll;
|
|
|
|
struct gpio_desc *id_det_gpio;
|
|
|
|
struct gpio_desc *vbus_det_gpio;
|
|
|
|
int id_det_irq;
|
|
|
|
int vbus_det_irq;
|
|
|
|
int id_det;
|
|
|
|
int vbus_det;
|
|
|
|
struct delayed_work detect;
|
2014-03-01 21:09:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define to_sun4i_usb_phy_data(phy) \
|
|
|
|
container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
|
|
|
|
struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
|
|
|
|
u32 iscr;
|
|
|
|
|
|
|
|
iscr = readl(data->base + REG_ISCR);
|
|
|
|
iscr &= ~clr;
|
|
|
|
iscr |= set;
|
|
|
|
writel(iscr, data->base + REG_ISCR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
|
|
|
|
{
|
|
|
|
if (val)
|
|
|
|
val = ISCR_FORCE_ID_HIGH;
|
|
|
|
else
|
|
|
|
val = ISCR_FORCE_ID_LOW;
|
|
|
|
|
|
|
|
sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
|
|
|
|
{
|
|
|
|
if (val)
|
|
|
|
val = ISCR_FORCE_VBUS_HIGH;
|
|
|
|
else
|
|
|
|
val = ISCR_FORCE_VBUS_LOW;
|
|
|
|
|
|
|
|
sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
|
|
|
|
u32 temp, usbc_bit = BIT(phy->index * 2);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mutex_lock(&phy_data->mutex);
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
temp = readl(phy_data->base + REG_PHYCTL);
|
|
|
|
|
|
|
|
/* clear the address portion */
|
|
|
|
temp &= ~(0xff << 8);
|
|
|
|
|
|
|
|
/* set the address */
|
|
|
|
temp |= ((addr + i) << 8);
|
|
|
|
writel(temp, phy_data->base + REG_PHYCTL);
|
|
|
|
|
|
|
|
/* set the data bit and clear usbc bit*/
|
|
|
|
temp = readb(phy_data->base + REG_PHYCTL);
|
|
|
|
if (data & 0x1)
|
|
|
|
temp |= PHYCTL_DATA;
|
|
|
|
else
|
|
|
|
temp &= ~PHYCTL_DATA;
|
|
|
|
temp &= ~usbc_bit;
|
|
|
|
writeb(temp, phy_data->base + REG_PHYCTL);
|
|
|
|
|
|
|
|
/* pulse usbc_bit */
|
|
|
|
temp = readb(phy_data->base + REG_PHYCTL);
|
|
|
|
temp |= usbc_bit;
|
|
|
|
writeb(temp, phy_data->base + REG_PHYCTL);
|
|
|
|
|
|
|
|
temp = readb(phy_data->base + REG_PHYCTL);
|
|
|
|
temp &= ~usbc_bit;
|
|
|
|
writeb(temp, phy_data->base + REG_PHYCTL);
|
|
|
|
|
|
|
|
data >>= 1;
|
|
|
|
}
|
|
|
|
mutex_unlock(&phy_data->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
|
|
|
|
{
|
|
|
|
u32 bits, reg_value;
|
|
|
|
|
|
|
|
if (!phy->pmu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
|
|
|
|
SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
|
|
|
|
|
|
|
|
reg_value = readl(phy->pmu);
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
reg_value |= bits;
|
|
|
|
else
|
|
|
|
reg_value &= ~bits;
|
|
|
|
|
|
|
|
writel(reg_value, phy->pmu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sun4i_usb_phy_init(struct phy *_phy)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
|
|
|
|
struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
|
|
|
|
int ret;
|
|
|
|
|
2014-05-13 19:44:18 +04:00
|
|
|
ret = clk_prepare_enable(phy->clk);
|
2014-03-01 21:09:26 +04:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = reset_control_deassert(phy->reset);
|
|
|
|
if (ret) {
|
2014-05-13 19:44:18 +04:00
|
|
|
clk_disable_unprepare(phy->clk);
|
2014-03-01 21:09:26 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-10 21:55:06 +03:00
|
|
|
/* Enable USB 45 Ohm resistor calibration */
|
|
|
|
if (phy->index == 0)
|
|
|
|
sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
/* Adjust PHY's magnitude and rate */
|
|
|
|
sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
|
|
|
|
|
|
|
|
/* Disconnect threshold adjustment */
|
|
|
|
sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
|
|
|
|
|
|
|
|
sun4i_usb_phy_passby(phy, 1);
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
if (phy->index == 0) {
|
|
|
|
data->phy0_init = true;
|
|
|
|
|
|
|
|
/* Enable pull-ups */
|
|
|
|
sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
|
|
|
|
sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
|
|
|
|
|
|
|
|
if (data->id_det_gpio) {
|
|
|
|
/* OTG mode, force ISCR and cable state updates */
|
|
|
|
data->id_det = -1;
|
|
|
|
data->vbus_det = -1;
|
|
|
|
queue_delayed_work(system_wq, &data->detect, 0);
|
|
|
|
} else {
|
|
|
|
/* Host only mode */
|
|
|
|
sun4i_usb_phy0_set_id_detect(_phy, 0);
|
|
|
|
sun4i_usb_phy0_set_vbus_detect(_phy, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sun4i_usb_phy_exit(struct phy *_phy)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
|
2015-06-13 15:37:45 +03:00
|
|
|
struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
|
|
|
|
|
|
|
|
if (phy->index == 0) {
|
|
|
|
/* Disable pull-ups */
|
|
|
|
sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
|
|
|
|
sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
|
|
|
|
data->phy0_init = false;
|
|
|
|
}
|
2014-03-01 21:09:26 +04:00
|
|
|
|
|
|
|
sun4i_usb_phy_passby(phy, 0);
|
|
|
|
reset_control_assert(phy->reset);
|
2014-05-13 19:44:18 +04:00
|
|
|
clk_disable_unprepare(phy->clk);
|
2014-03-01 21:09:26 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sun4i_usb_phy_power_on(struct phy *_phy)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
|
2015-06-13 15:37:45 +03:00
|
|
|
struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!phy->vbus || phy->regulator_on)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* For phy0 only turn on Vbus if we don't have an ext. Vbus */
|
|
|
|
if (phy->index == 0 && data->vbus_det)
|
|
|
|
return 0;
|
2014-03-01 21:09:26 +04:00
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
ret = regulator_enable(phy->vbus);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-03-01 21:09:26 +04:00
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
phy->regulator_on = true;
|
|
|
|
|
|
|
|
/* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
|
|
|
|
if (phy->index == 0 && data->phy0_poll)
|
|
|
|
mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
|
|
|
|
|
|
|
|
return 0;
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sun4i_usb_phy_power_off(struct phy *_phy)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
|
2015-06-13 15:37:45 +03:00
|
|
|
struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
|
|
|
|
|
|
|
|
if (!phy->vbus || !phy->regulator_on)
|
|
|
|
return 0;
|
2014-03-01 21:09:26 +04:00
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
regulator_disable(phy->vbus);
|
|
|
|
phy->regulator_on = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* phy0 vbus typically slowly discharges, sometimes this causes the
|
|
|
|
* Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
|
|
|
|
*/
|
|
|
|
if (phy->index == 0 && !data->phy0_poll)
|
|
|
|
mod_delayed_work(system_wq, &data->detect, POLL_TIME);
|
2014-03-01 21:09:26 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-29 13:50:46 +03:00
|
|
|
void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
|
|
|
|
|
|
|
|
sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
static struct phy_ops sun4i_usb_phy_ops = {
|
|
|
|
.init = sun4i_usb_phy_init,
|
|
|
|
.exit = sun4i_usb_phy_exit,
|
|
|
|
.power_on = sun4i_usb_phy_power_on,
|
|
|
|
.power_off = sun4i_usb_phy_power_off,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy_data *data =
|
|
|
|
container_of(work, struct sun4i_usb_phy_data, detect.work);
|
|
|
|
struct phy *phy0 = data->phys[0].phy;
|
2015-06-13 15:37:46 +03:00
|
|
|
int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
|
2015-06-13 15:37:45 +03:00
|
|
|
|
|
|
|
id_det = gpiod_get_value_cansleep(data->id_det_gpio);
|
|
|
|
vbus_det = gpiod_get_value_cansleep(data->vbus_det_gpio);
|
|
|
|
|
|
|
|
mutex_lock(&phy0->mutex);
|
|
|
|
|
|
|
|
if (!data->phy0_init) {
|
|
|
|
mutex_unlock(&phy0->mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id_det != data->id_det) {
|
|
|
|
sun4i_usb_phy0_set_id_detect(phy0, id_det);
|
|
|
|
data->id_det = id_det;
|
2015-06-13 15:37:46 +03:00
|
|
|
id_notify = 1;
|
2015-06-13 15:37:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vbus_det != data->vbus_det) {
|
|
|
|
sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
|
|
|
|
data->vbus_det = vbus_det;
|
2015-06-13 15:37:46 +03:00
|
|
|
vbus_notify = 1;
|
2015-06-13 15:37:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&phy0->mutex);
|
|
|
|
|
2015-06-13 15:37:46 +03:00
|
|
|
if (id_notify)
|
|
|
|
extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
|
|
|
|
!id_det);
|
|
|
|
|
|
|
|
if (vbus_notify)
|
|
|
|
extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
if (data->phy0_poll)
|
|
|
|
queue_delayed_work(system_wq, &data->detect, POLL_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy_data *data = dev_id;
|
|
|
|
|
|
|
|
/* vbus or id changed, let the pins settle and then scan them */
|
|
|
|
mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
static struct phy *sun4i_usb_phy_xlate(struct device *dev,
|
|
|
|
struct of_phandle_args *args)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
|
|
|
|
|
2014-11-10 21:55:06 +03:00
|
|
|
if (args->args[0] >= data->num_phys)
|
2014-03-01 21:09:26 +04:00
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
|
|
|
return data->phys[args->args[0]].phy;
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
static int sun4i_usb_phy_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (data->id_det_irq >= 0)
|
|
|
|
devm_free_irq(dev, data->id_det_irq, data);
|
|
|
|
if (data->vbus_det_irq >= 0)
|
|
|
|
devm_free_irq(dev, data->vbus_det_irq, data);
|
|
|
|
|
|
|
|
cancel_delayed_work_sync(&data->detect);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:37:46 +03:00
|
|
|
static const unsigned int sun4i_usb_phy0_cable[] = {
|
|
|
|
EXTCON_USB,
|
|
|
|
EXTCON_USB_HOST,
|
|
|
|
EXTCON_NONE,
|
|
|
|
};
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
static int sun4i_usb_phy_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct sun4i_usb_phy_data *data;
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct device_node *np = dev->of_node;
|
|
|
|
struct phy_provider *phy_provider;
|
2014-05-13 19:44:18 +04:00
|
|
|
bool dedicated_clocks;
|
2014-03-01 21:09:26 +04:00
|
|
|
struct resource *res;
|
2015-06-13 15:37:45 +03:00
|
|
|
int i, ret;
|
2014-03-01 21:09:26 +04:00
|
|
|
|
|
|
|
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_init(&data->mutex);
|
2015-06-13 15:37:45 +03:00
|
|
|
INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
|
|
|
|
dev_set_drvdata(dev, data);
|
2014-03-01 21:09:26 +04:00
|
|
|
|
2015-06-13 15:37:48 +03:00
|
|
|
if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
|
|
|
|
of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy"))
|
2014-03-01 21:09:26 +04:00
|
|
|
data->num_phys = 2;
|
|
|
|
else
|
|
|
|
data->num_phys = 3;
|
|
|
|
|
phy-sun4i-usb: Swap check for disconnect threshold
Before this commit the code for determining the disconnect threshold was
checking for "allwinner,sun4i-a10-usb-phy" or "allwinner,sun6i-a31-usb-phy"
assuming that those where the exception and then newer SoCs would use a
disconnect threshold of 2 like sun7i does. But it turns out that newer
SoCs use a disconnect threshold of 3 and sun5i and sun7i are the
exceptions, so check for those instead.
Here are the settings from the various Allwinner SDK sources:
sun4i-a10: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun5i-a13: USBC_Phy_Write(usbc_no, 0x2a, 2, 2);
sun6i-a31: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun7i-a20: USBC_Phy_Write(usbc_no, 0x2a, 2, 2);
sun8i-a23: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun8i-h3: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun9i-a80: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
Note this commit makes no functional changes as currently we only support
sun4i - sun7i.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
2015-06-13 15:37:47 +03:00
|
|
|
if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
|
|
|
|
of_device_is_compatible(np, "allwinner,sun7i-a20-usb-phy"))
|
2014-03-01 21:09:26 +04:00
|
|
|
data->disc_thresh = 2;
|
phy-sun4i-usb: Swap check for disconnect threshold
Before this commit the code for determining the disconnect threshold was
checking for "allwinner,sun4i-a10-usb-phy" or "allwinner,sun6i-a31-usb-phy"
assuming that those where the exception and then newer SoCs would use a
disconnect threshold of 2 like sun7i does. But it turns out that newer
SoCs use a disconnect threshold of 3 and sun5i and sun7i are the
exceptions, so check for those instead.
Here are the settings from the various Allwinner SDK sources:
sun4i-a10: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun5i-a13: USBC_Phy_Write(usbc_no, 0x2a, 2, 2);
sun6i-a31: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun7i-a20: USBC_Phy_Write(usbc_no, 0x2a, 2, 2);
sun8i-a23: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun8i-h3: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
sun9i-a80: USBC_Phy_Write(usbc_no, 0x2a, 3, 2);
Note this commit makes no functional changes as currently we only support
sun4i - sun7i.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
2015-06-13 15:37:47 +03:00
|
|
|
else
|
|
|
|
data->disc_thresh = 3;
|
2014-03-01 21:09:26 +04:00
|
|
|
|
2015-06-13 15:37:48 +03:00
|
|
|
if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy") ||
|
|
|
|
of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy"))
|
2014-05-13 19:44:18 +04:00
|
|
|
dedicated_clocks = true;
|
|
|
|
else
|
|
|
|
dedicated_clocks = false;
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
|
|
|
|
data->base = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(data->base))
|
|
|
|
return PTR_ERR(data->base);
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
data->id_det_gpio = devm_gpiod_get(dev, "usb0_id_det", GPIOD_IN);
|
|
|
|
if (IS_ERR(data->id_det_gpio)) {
|
|
|
|
if (PTR_ERR(data->id_det_gpio) == -EPROBE_DEFER)
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
data->id_det_gpio = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->vbus_det_gpio = devm_gpiod_get(dev, "usb0_vbus_det", GPIOD_IN);
|
|
|
|
if (IS_ERR(data->vbus_det_gpio)) {
|
|
|
|
if (PTR_ERR(data->vbus_det_gpio) == -EPROBE_DEFER)
|
|
|
|
return -EPROBE_DEFER;
|
|
|
|
data->vbus_det_gpio = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We either want both gpio pins or neither (when in host mode) */
|
|
|
|
if (!data->id_det_gpio != !data->vbus_det_gpio) {
|
|
|
|
dev_err(dev, "failed to get id or vbus detect pin\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:37:46 +03:00
|
|
|
if (data->id_det_gpio) {
|
|
|
|
data->extcon = devm_extcon_dev_allocate(dev,
|
|
|
|
sun4i_usb_phy0_cable);
|
|
|
|
if (IS_ERR(data->extcon))
|
|
|
|
return PTR_ERR(data->extcon);
|
|
|
|
|
|
|
|
ret = devm_extcon_dev_register(dev, data->extcon);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to register extcon: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 21:55:06 +03:00
|
|
|
for (i = 0; i < data->num_phys; i++) {
|
2014-05-13 19:44:17 +04:00
|
|
|
struct sun4i_usb_phy *phy = data->phys + i;
|
|
|
|
char name[16];
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
snprintf(name, sizeof(name), "usb%d_vbus", i);
|
2014-05-13 19:44:17 +04:00
|
|
|
phy->vbus = devm_regulator_get_optional(dev, name);
|
|
|
|
if (IS_ERR(phy->vbus)) {
|
|
|
|
if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
|
2014-03-01 21:09:26 +04:00
|
|
|
return -EPROBE_DEFER;
|
2014-05-13 19:44:17 +04:00
|
|
|
phy->vbus = NULL;
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 19:44:18 +04:00
|
|
|
if (dedicated_clocks)
|
|
|
|
snprintf(name, sizeof(name), "usb%d_phy", i);
|
|
|
|
else
|
|
|
|
strlcpy(name, "usb_phy", sizeof(name));
|
|
|
|
|
|
|
|
phy->clk = devm_clk_get(dev, name);
|
|
|
|
if (IS_ERR(phy->clk)) {
|
|
|
|
dev_err(dev, "failed to get clock %s\n", name);
|
|
|
|
return PTR_ERR(phy->clk);
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
snprintf(name, sizeof(name), "usb%d_reset", i);
|
2014-05-13 19:44:17 +04:00
|
|
|
phy->reset = devm_reset_control_get(dev, name);
|
|
|
|
if (IS_ERR(phy->reset)) {
|
2014-03-01 21:09:26 +04:00
|
|
|
dev_err(dev, "failed to get reset %s\n", name);
|
2014-05-13 19:44:17 +04:00
|
|
|
return PTR_ERR(phy->reset);
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i) { /* No pmu for usbc0 */
|
|
|
|
snprintf(name, sizeof(name), "pmu%d", i);
|
|
|
|
res = platform_get_resource_byname(pdev,
|
|
|
|
IORESOURCE_MEM, name);
|
2014-05-13 19:44:17 +04:00
|
|
|
phy->pmu = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(phy->pmu))
|
|
|
|
return PTR_ERR(phy->pmu);
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
2014-11-19 18:28:21 +03:00
|
|
|
phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
|
2014-05-13 19:44:17 +04:00
|
|
|
if (IS_ERR(phy->phy)) {
|
2014-03-01 21:09:26 +04:00
|
|
|
dev_err(dev, "failed to create PHY %d\n", i);
|
2014-05-13 19:44:17 +04:00
|
|
|
return PTR_ERR(phy->phy);
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 19:44:17 +04:00
|
|
|
phy->index = i;
|
|
|
|
phy_set_drvdata(phy->phy, &data->phys[i]);
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
|
|
|
|
data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
|
|
|
|
if (data->id_det_irq < 0 || data->vbus_det_irq < 0)
|
|
|
|
data->phy0_poll = true;
|
|
|
|
|
|
|
|
if (data->id_det_irq >= 0) {
|
|
|
|
ret = devm_request_irq(dev, data->id_det_irq,
|
|
|
|
sun4i_usb_phy0_id_vbus_det_irq,
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
|
|
"usb0-id-det", data);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->vbus_det_irq >= 0) {
|
|
|
|
ret = devm_request_irq(dev, data->vbus_det_irq,
|
|
|
|
sun4i_usb_phy0_id_vbus_det_irq,
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
|
|
"usb0-vbus-det", data);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
|
|
|
|
data->vbus_det_irq = -1;
|
|
|
|
sun4i_usb_phy_remove(pdev); /* Stop detect work */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:09:26 +04:00
|
|
|
phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
|
2015-06-13 15:37:45 +03:00
|
|
|
if (IS_ERR(phy_provider)) {
|
|
|
|
sun4i_usb_phy_remove(pdev); /* Stop detect work */
|
|
|
|
return PTR_ERR(phy_provider);
|
|
|
|
}
|
2014-03-01 21:09:26 +04:00
|
|
|
|
2015-06-13 15:37:45 +03:00
|
|
|
return 0;
|
2014-03-01 21:09:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id sun4i_usb_phy_of_match[] = {
|
|
|
|
{ .compatible = "allwinner,sun4i-a10-usb-phy" },
|
|
|
|
{ .compatible = "allwinner,sun5i-a13-usb-phy" },
|
2014-05-13 19:44:18 +04:00
|
|
|
{ .compatible = "allwinner,sun6i-a31-usb-phy" },
|
2014-03-01 21:09:26 +04:00
|
|
|
{ .compatible = "allwinner,sun7i-a20-usb-phy" },
|
2015-06-13 15:37:48 +03:00
|
|
|
{ .compatible = "allwinner,sun8i-a23-usb-phy" },
|
2014-03-01 21:09:26 +04:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
|
|
|
|
|
|
|
|
static struct platform_driver sun4i_usb_phy_driver = {
|
|
|
|
.probe = sun4i_usb_phy_probe,
|
2015-06-13 15:37:45 +03:00
|
|
|
.remove = sun4i_usb_phy_remove,
|
2014-03-01 21:09:26 +04:00
|
|
|
.driver = {
|
|
|
|
.of_match_table = sun4i_usb_phy_of_match,
|
|
|
|
.name = "sun4i-usb-phy",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
module_platform_driver(sun4i_usb_phy_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
|
|
|
|
MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|