| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2014 STMicroelectronics |
| 4 |
* |
| 5 |
* STMicroelectronics Generic PHY driver for STiH407 USB2. |
| 6 |
* |
| 7 |
* Author: Giuseppe Cavallaro <[email protected]> |
| 8 |
*/ |
| 9 |
#include <linux/platform_device.h> |
| 10 |
#include <linux/io.h> |
| 11 |
#include <linux/kernel.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/of.h> |
| 14 |
#include <linux/of_platform.h> |
| 15 |
#include <linux/clk.h> |
| 16 |
#include <linux/regmap.h> |
| 17 |
#include <linux/reset.h> |
| 18 |
#include <linux/mfd/syscon.h> |
| 19 |
#include <linux/phy/phy.h> |
| 20 |
|
| 21 |
#define PHYPARAM_REG 0 |
| 22 |
#define PHYCTRL_REG 1 |
| 23 |
|
| 24 |
/* Default PHY_SEL and REFCLKSEL configuration */ |
| 25 |
#define STIH407_USB_PICOPHY_CTRL_PORT_CONF 0x6 |
| 26 |
#define STIH407_USB_PICOPHY_CTRL_PORT_MASK 0x1f |
| 27 |
|
| 28 |
/* ports parameters overriding */ |
| 29 |
#define STIH407_USB_PICOPHY_PARAM_DEF 0x39a4dc |
| 30 |
#define STIH407_USB_PICOPHY_PARAM_MASK 0xffffffff |
| 31 |
|
| 32 |
struct stih407_usb2_picophy { |
| 33 |
struct phy *phy; |
| 34 |
struct regmap *regmap; |
| 35 |
struct device *dev; |
| 36 |
struct reset_control *rstc; |
| 37 |
struct reset_control *rstport; |
| 38 |
int ctrl; |
| 39 |
int param; |
| 40 |
}; |
| 41 |
|
| 42 |
static int stih407_usb2_pico_ctrl(struct stih407_usb2_picophy *phy_dev) |
| 43 |
{ |
| 44 |
reset_control_deassert(phy_dev->rstc); |
| 45 |
|
| 46 |
return regmap_update_bits(phy_dev->regmap, phy_dev->ctrl, |
| 47 |
STIH407_USB_PICOPHY_CTRL_PORT_MASK, |
| 48 |
STIH407_USB_PICOPHY_CTRL_PORT_CONF); |
| 49 |
} |
| 50 |
|
| 51 |
static int stih407_usb2_init_port(struct phy *phy) |
| 52 |
{ |
| 53 |
int ret; |
| 54 |
struct stih407_usb2_picophy *phy_dev = phy_get_drvdata(phy); |
| 55 |
|
| 56 |
stih407_usb2_pico_ctrl(phy_dev); |
| 57 |
|
| 58 |
ret = regmap_update_bits(phy_dev->regmap, |
| 59 |
phy_dev->param, |
| 60 |
STIH407_USB_PICOPHY_PARAM_MASK, |
| 61 |
STIH407_USB_PICOPHY_PARAM_DEF); |
| 62 |
if (ret) |
| 63 |
return ret; |
| 64 |
|
| 65 |
return reset_control_deassert(phy_dev->rstport); |
| 66 |
} |
| 67 |
|
| 68 |
static int stih407_usb2_exit_port(struct phy *phy) |
| 69 |
{ |
| 70 |
struct stih407_usb2_picophy *phy_dev = phy_get_drvdata(phy); |
| 71 |
|
| 72 |
/* |
| 73 |
* Only port reset is asserted, phy global reset is kept untouched |
| 74 |
* as other ports may still be active. When all ports are in reset |
| 75 |
* state, assumption is made that power will be cut off on the phy, in |
| 76 |
* case of suspend for instance. Theoretically, asserting individual |
| 77 |
* reset (like here) or global reset should be equivalent. |
| 78 |
*/ |
| 79 |
return reset_control_assert(phy_dev->rstport); |
| 80 |
} |
| 81 |
|
| 82 |
static const struct phy_ops stih407_usb2_picophy_data = { |
| 83 |
.init = stih407_usb2_init_port, |
| 84 |
.exit = stih407_usb2_exit_port, |
| 85 |
.owner = THIS_MODULE, |
| 86 |
}; |
| 87 |
|
| 88 |
static int stih407_usb2_picophy_probe(struct platform_device *pdev) |
| 89 |
{ |
| 90 |
struct stih407_usb2_picophy *phy_dev; |
| 91 |
struct device *dev = &pdev->dev; |
| 92 |
struct device_node *np = dev->of_node; |
| 93 |
struct phy_provider *phy_provider; |
| 94 |
unsigned int syscon_args[2]; |
| 95 |
struct phy *phy; |
| 96 |
|
| 97 |
phy_dev = devm_kzalloc(dev, sizeof(*phy_dev), GFP_KERNEL); |
| 98 |
if (!phy_dev) |
| 99 |
return -ENOMEM; |
| 100 |
|
| 101 |
phy_dev->dev = dev; |
| 102 |
dev_set_drvdata(dev, phy_dev); |
| 103 |
|
| 104 |
phy_dev->rstc = devm_reset_control_get_shared(dev, "global"); |
| 105 |
if (IS_ERR(phy_dev->rstc)) { |
| 106 |
dev_err(dev, "failed to ctrl picoPHY reset\n"); |
| 107 |
return PTR_ERR(phy_dev->rstc); |
| 108 |
} |
| 109 |
|
| 110 |
phy_dev->rstport = devm_reset_control_get_exclusive(dev, "port"); |
| 111 |
if (IS_ERR(phy_dev->rstport)) { |
| 112 |
dev_err(dev, "failed to ctrl picoPHY reset\n"); |
| 113 |
return PTR_ERR(phy_dev->rstport); |
| 114 |
} |
| 115 |
|
| 116 |
/* Reset port by default: only deassert it in phy init */ |
| 117 |
reset_control_assert(phy_dev->rstport); |
| 118 |
|
| 119 |
phy_dev->regmap = syscon_regmap_lookup_by_phandle_args(np, "st,syscfg", |
| 120 |
2, syscon_args); |
| 121 |
if (IS_ERR(phy_dev->regmap)) { |
| 122 |
dev_err(dev, "No syscfg phandle specified\n"); |
| 123 |
return PTR_ERR(phy_dev->regmap); |
| 124 |
} |
| 125 |
|
| 126 |
phy_dev->param = syscon_args[PHYPARAM_REG]; |
| 127 |
phy_dev->ctrl = syscon_args[PHYCTRL_REG]; |
| 128 |
|
| 129 |
phy = devm_phy_create(dev, NULL, &stih407_usb2_picophy_data); |
| 130 |
if (IS_ERR(phy)) { |
| 131 |
dev_err(dev, "failed to create Display Port PHY\n"); |
| 132 |
return PTR_ERR(phy); |
| 133 |
} |
| 134 |
|
| 135 |
phy_dev->phy = phy; |
| 136 |
phy_set_drvdata(phy, phy_dev); |
| 137 |
|
| 138 |
phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); |
| 139 |
if (IS_ERR(phy_provider)) |
| 140 |
return PTR_ERR(phy_provider); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static const struct of_device_id stih407_usb2_picophy_of_match[] = { |
| 146 |
{ .compatible = "st,stih407-usb2-phy" }, |
| 147 |
{ /*sentinel */ }, |
| 148 |
}; |
| 149 |
|
| 150 |
MODULE_DEVICE_TABLE(of, stih407_usb2_picophy_of_match); |
| 151 |
|
| 152 |
static struct platform_driver stih407_usb2_picophy_driver = { |
| 153 |
.probe = stih407_usb2_picophy_probe, |
| 154 |
.driver = { |
| 155 |
.name = "stih407-usb-genphy", |
| 156 |
.of_match_table = stih407_usb2_picophy_of_match, |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
module_platform_driver(stih407_usb2_picophy_driver); |
| 161 |
|
| 162 |
MODULE_AUTHOR("Giuseppe Cavallaro <[email protected]>"); |
| 163 |
MODULE_DESCRIPTION("STMicroelectronics Generic picoPHY driver for STiH407"); |
| 164 |
MODULE_LICENSE("GPL v2"); |