phy-samsung-usb.c 7.41 KB
Newer Older
1
/* linux/drivers/usb/phy/phy-samsung-usb.c
2 3 4 5 6 7
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com
 *
 * Author: Praveen Paneri <p.paneri@samsung.com>
 *
8 9 10
 * Samsung USB-PHY helper driver with common function calls;
 * interacts with Samsung USB 2.0 PHY controller driver and later
 * with Samsung USB 3.0 PHY driver.
11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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/module.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
25
#include <linux/device.h>
26 27 28
#include <linux/err.h>
#include <linux/io.h>
#include <linux/of.h>
29
#include <linux/of_address.h>
30
#include <linux/usb/samsung_usb_phy.h>
31

32
#include "phy-samsung-usb.h"
33

Mauro Ribeiro's avatar
Mauro Ribeiro committed
34 35 36
static struct raw_notifier_head usb_lpa_nh =
		RAW_NOTIFIER_INIT(usb_lpa_nh);

37
int samsung_usbphy_parse_dt(struct samsung_usbphy *sphy)
38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
	struct device_node *usbphy_sys;

	/* Getting node for system controller interface for usb-phy */
	usbphy_sys = of_get_child_by_name(sphy->dev->of_node, "usbphy-sys");
	if (!usbphy_sys) {
		dev_err(sphy->dev, "No sys-controller interface for usb-phy\n");
		return -ENODEV;
	}

	sphy->pmuregs = of_iomap(usbphy_sys, 0);

	if (sphy->pmuregs == NULL) {
		dev_err(sphy->dev, "Can't get usb-phy pmu control register\n");
52
		goto err0;
53 54
	}

55 56 57 58 59 60 61
	sphy->sysreg = of_iomap(usbphy_sys, 1);

	/*
	 * Not returning error code here, since this situation is not fatal.
	 * Few SoCs may not have this switch available
	 */
	if (sphy->sysreg == NULL)
Mauro Ribeiro's avatar
Mauro Ribeiro committed
62
		dev_dbg(sphy->dev, "Can't get usb-phy sysreg cfg register\n");
63 64 65

	of_node_put(usbphy_sys);

66
	return 0;
67 68 69 70

err0:
	of_node_put(usbphy_sys);
	return -ENXIO;
71
}
72
EXPORT_SYMBOL_GPL(samsung_usbphy_parse_dt);
73 74 75 76 77 78

/*
 * Set isolation here for phy.
 * Here 'on = true' would mean USB PHY block is isolated, hence
 * de-activated and vice-versa.
 */
79
void samsung_usbphy_set_isolation(struct samsung_usbphy *sphy, bool on)
80
{
81
	void __iomem *reg = NULL;
82
	u32 reg_val;
83
	u32 en_mask = 0;
84 85 86 87 88 89

	if (!sphy->pmuregs) {
		dev_warn(sphy->dev, "Can't set pmu isolation\n");
		return;
	}

90 91 92 93 94 95 96 97 98 99 100 101 102
	switch (sphy->drv_data->cpu_type) {
	case TYPE_S3C64XX:
		/*
		 * Do nothing: We will add here once S3C64xx goes for DT support
		 */
		break;
	case TYPE_EXYNOS4210:
		/*
		 * Fall through since exynos4210 and exynos5250 have similar
		 * register architecture: two separate registers for host and
		 * device phy control with enable bit at position 0.
		 */
	case TYPE_EXYNOS5250:
Mauro Ribeiro's avatar
Mauro Ribeiro committed
103
	case TYPE_EXYNOS5:
104 105 106 107 108 109 110 111 112 113 114 115 116 117
		if (sphy->phy_type == USB_PHY_TYPE_DEVICE) {
			reg = sphy->pmuregs +
				sphy->drv_data->devphy_reg_offset;
			en_mask = sphy->drv_data->devphy_en_mask;
		} else if (sphy->phy_type == USB_PHY_TYPE_HOST) {
			reg = sphy->pmuregs +
				sphy->drv_data->hostphy_reg_offset;
			en_mask = sphy->drv_data->hostphy_en_mask;
		}
		break;
	default:
		dev_err(sphy->dev, "Invalid SoC type\n");
		return;
	}
118

Mauro Ribeiro's avatar
Mauro Ribeiro committed
119 120
	if (reg) {
		reg_val = readl(reg);
121

Mauro Ribeiro's avatar
Mauro Ribeiro committed
122 123 124 125
		if (on)
			reg_val &= ~en_mask;
		else
			reg_val |= en_mask;
126

Mauro Ribeiro's avatar
Mauro Ribeiro committed
127 128
		writel(reg_val, reg);
	}
129
}
130
EXPORT_SYMBOL_GPL(samsung_usbphy_set_isolation);
131

Mauro Ribeiro's avatar
Mauro Ribeiro committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
void samsung_hsicphy_set_isolation(struct samsung_usbphy *sphy, bool on)
{
	void __iomem *reg = NULL;
	u32 reg_val;
	u32 en_mask = 0;

	if (!sphy->pmuregs) {
		dev_warn(sphy->dev, "Can't set pmu isolation\n");
		return;
	}

	switch (sphy->drv_data->cpu_type) {
	case TYPE_EXYNOS5:
		if (sphy->phy_type == USB_PHY_TYPE_HOST) {
			reg = sphy->pmuregs +
				sphy->drv_data->hsicphy_reg_offset;
			en_mask = sphy->drv_data->hsicphy_en_mask;
		} else {
			dev_err(sphy->dev, "Invalid phy type\n");
		}
		break;
	default:
		dev_dbg(sphy->dev, "It is not needed in this SoC type");
		return;
	}

	if (reg) {
		reg_val = readl(reg);

		if (on)
			reg_val &= ~en_mask;
		else
			reg_val |= en_mask;

		writel(reg_val, reg);
	}
}
EXPORT_SYMBOL_GPL(samsung_hsicphy_set_isolation);

171 172 173
/*
 * Configure the mode of working of usb-phy here: HOST/DEVICE.
 */
174
void samsung_usbphy_cfg_sel(struct samsung_usbphy *sphy)
175 176 177 178
{
	u32 reg;

	if (!sphy->sysreg) {
Mauro Ribeiro's avatar
Mauro Ribeiro committed
179
		dev_dbg(sphy->dev, "Can't configure specified phy mode\n");
180 181 182 183 184 185 186 187 188 189 190 191
		return;
	}

	reg = readl(sphy->sysreg);

	if (sphy->phy_type == USB_PHY_TYPE_DEVICE)
		reg &= ~EXYNOS_USB20PHY_CFG_HOST_LINK;
	else if (sphy->phy_type == USB_PHY_TYPE_HOST)
		reg |= EXYNOS_USB20PHY_CFG_HOST_LINK;

	writel(reg, sphy->sysreg);
}
192
EXPORT_SYMBOL_GPL(samsung_usbphy_cfg_sel);
193 194 195 196 197 198

/*
 * PHYs are different for USB Device and USB Host.
 * This make sure that correct PHY type is selected before
 * any operation on PHY.
 */
199
int samsung_usbphy_set_type(struct usb_phy *phy,
200 201 202 203 204 205 206 207
				enum samsung_usb_phy_type phy_type)
{
	struct samsung_usbphy *sphy = phy_to_sphy(phy);

	sphy->phy_type = phy_type;

	return 0;
}
208
EXPORT_SYMBOL_GPL(samsung_usbphy_set_type);
209

210 211 212
/*
 * Returns reference clock frequency selection value
 */
213
int samsung_usbphy_get_refclk_freq(struct samsung_usbphy *sphy)
214 215 216 217
{
	struct clk *ref_clk;
	int refclk_freq = 0;

218 219 220 221
	/*
	 * In exynos5250 USB host and device PHY use
	 * external crystal clock XXTI
	 */
Mauro Ribeiro's avatar
Mauro Ribeiro committed
222 223
	if (sphy->drv_data->cpu_type == TYPE_EXYNOS5250 ||
		sphy->drv_data->cpu_type == TYPE_EXYNOS5)
224
		ref_clk = devm_clk_get(sphy->dev, "ext_xtal");
225
	else
226
		ref_clk = devm_clk_get(sphy->dev, "xusbxti");
227 228 229 230 231
	if (IS_ERR(ref_clk)) {
		dev_err(sphy->dev, "Failed to get reference clock\n");
		return PTR_ERR(ref_clk);
	}

Mauro Ribeiro's avatar
Mauro Ribeiro committed
232 233
	if (sphy->drv_data->cpu_type == TYPE_EXYNOS5250 ||
		sphy->drv_data->cpu_type == TYPE_EXYNOS5) {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		/* set clock frequency for PLL */
		switch (clk_get_rate(ref_clk)) {
		case 9600 * KHZ:
			refclk_freq = FSEL_CLKSEL_9600K;
			break;
		case 10 * MHZ:
			refclk_freq = FSEL_CLKSEL_10M;
			break;
		case 12 * MHZ:
			refclk_freq = FSEL_CLKSEL_12M;
			break;
		case 19200 * KHZ:
			refclk_freq = FSEL_CLKSEL_19200K;
			break;
		case 20 * MHZ:
			refclk_freq = FSEL_CLKSEL_20M;
			break;
		case 50 * MHZ:
			refclk_freq = FSEL_CLKSEL_50M;
			break;
		case 24 * MHZ:
		default:
			/* default reference clock */
			refclk_freq = FSEL_CLKSEL_24M;
			break;
		}
	} else {
		switch (clk_get_rate(ref_clk)) {
		case 12 * MHZ:
			refclk_freq = PHYCLK_CLKSEL_12M;
			break;
		case 24 * MHZ:
266
			refclk_freq = PHYCLK_CLKSEL_24M;
267 268 269 270 271 272 273 274 275 276 277
			break;
		case 48 * MHZ:
			refclk_freq = PHYCLK_CLKSEL_48M;
			break;
		default:
			if (sphy->drv_data->cpu_type == TYPE_S3C64XX)
				refclk_freq = PHYCLK_CLKSEL_48M;
			else
				refclk_freq = PHYCLK_CLKSEL_24M;
			break;
		}
278 279 280 281 282
	}
	clk_put(ref_clk);

	return refclk_freq;
}
283
EXPORT_SYMBOL_GPL(samsung_usbphy_get_refclk_freq);
Mauro Ribeiro's avatar
Mauro Ribeiro committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

int samsung_usbphy_check_op(void)
{
	int op;

	op = usb_phy_check_op();
	/* REVISIT: this also can be done by cpuidle code */
	if (!op) {
		/* System is going to enter LPA, so notify all subscribers */
		raw_notifier_call_chain(&usb_lpa_nh,
				USB_LPA_PREPARE, NULL);
	}

	return op;
}
EXPORT_SYMBOL_GPL(samsung_usbphy_check_op);

void samsung_usb_lpa_resume(void)
{
	raw_notifier_call_chain(&usb_lpa_nh, USB_LPA_RESUME, NULL);
}
EXPORT_SYMBOL_GPL(samsung_usb_lpa_resume);

int register_samsung_usb_lpa_notifier(struct notifier_block *nb)
{
	return raw_notifier_chain_register(&usb_lpa_nh, nb);
}
EXPORT_SYMBOL_GPL(register_samsung_usb_lpa_notifier);

int unregister_samsung_usb_lpa_notifier(struct notifier_block *nb)
{
	return raw_notifier_chain_unregister(&usb_lpa_nh, nb);
}
EXPORT_SYMBOL_GPL(unregister_samsung_usb_lpa_notifier);