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 <peppe.cavallaro@st.com>
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	1
22 #define PHYCTRL_REG	2
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 
stih407_usb2_pico_ctrl(struct stih407_usb2_picophy * phy_dev)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 
stih407_usb2_init_port(struct phy * phy)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 
stih407_usb2_exit_port(struct phy * phy)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 
stih407_usb2_picophy_probe(struct platform_device * pdev)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 	struct phy *phy;
95 	int ret;
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(np, "st,syscfg");
120 	if (IS_ERR(phy_dev->regmap)) {
121 		dev_err(dev, "No syscfg phandle specified\n");
122 		return PTR_ERR(phy_dev->regmap);
123 	}
124 
125 	ret = of_property_read_u32_index(np, "st,syscfg", PHYPARAM_REG,
126 					&phy_dev->param);
127 	if (ret) {
128 		dev_err(dev, "can't get phyparam offset (%d)\n", ret);
129 		return ret;
130 	}
131 
132 	ret = of_property_read_u32_index(np, "st,syscfg", PHYCTRL_REG,
133 					&phy_dev->ctrl);
134 	if (ret) {
135 		dev_err(dev, "can't get phyctrl offset (%d)\n", ret);
136 		return ret;
137 	}
138 
139 	phy = devm_phy_create(dev, NULL, &stih407_usb2_picophy_data);
140 	if (IS_ERR(phy)) {
141 		dev_err(dev, "failed to create Display Port PHY\n");
142 		return PTR_ERR(phy);
143 	}
144 
145 	phy_dev->phy = phy;
146 	phy_set_drvdata(phy, phy_dev);
147 
148 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
149 	if (IS_ERR(phy_provider))
150 		return PTR_ERR(phy_provider);
151 
152 	dev_info(dev, "STiH407 USB Generic picoPHY driver probed!");
153 
154 	return 0;
155 }
156 
157 static const struct of_device_id stih407_usb2_picophy_of_match[] = {
158 	{ .compatible = "st,stih407-usb2-phy" },
159 	{ /*sentinel */ },
160 };
161 
162 MODULE_DEVICE_TABLE(of, stih407_usb2_picophy_of_match);
163 
164 static struct platform_driver stih407_usb2_picophy_driver = {
165 	.probe = stih407_usb2_picophy_probe,
166 	.driver = {
167 		   .name = "stih407-usb-genphy",
168 		   .of_match_table = stih407_usb2_picophy_of_match,
169 		   }
170 };
171 
172 module_platform_driver(stih407_usb2_picophy_driver);
173 
174 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
175 MODULE_DESCRIPTION("STMicroelectronics Generic picoPHY driver for STiH407");
176 MODULE_LICENSE("GPL v2");
177