xref: /linux/drivers/phy/qualcomm/phy-qcom-eusb2-repeater.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023, Linaro Limited
4  */
5 
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/regulator/consumer.h>
9 #include <linux/regmap.h>
10 #include <linux/of.h>
11 #include <linux/phy/phy.h>
12 
13 /* eUSB2 status registers */
14 #define EUSB2_RPTR_STATUS		0x08
15 #define	RPTR_OK				BIT(7)
16 
17 /* eUSB2 control registers */
18 #define EUSB2_EN_CTL1			0x46
19 #define EUSB2_RPTR_EN			BIT(7)
20 
21 #define EUSB2_FORCE_EN_5		0xe8
22 #define F_CLK_19P2M_EN			BIT(6)
23 
24 #define EUSB2_FORCE_VAL_5		0xeD
25 #define V_CLK_19P2M_EN			BIT(6)
26 
27 #define EUSB2_TUNE_IUSB2		0x51
28 #define EUSB2_TUNE_SQUELCH_U		0x54
29 #define EUSB2_TUNE_USB2_PREEM		0x57
30 
31 #define QCOM_EUSB2_REPEATER_INIT_CFG(o, v)	\
32 	{					\
33 		.offset = o,			\
34 		.val = v,			\
35 	}
36 
37 struct eusb2_repeater_init_tbl {
38 	unsigned int offset;
39 	unsigned int val;
40 };
41 
42 struct eusb2_repeater_cfg {
43 	const struct eusb2_repeater_init_tbl *init_tbl;
44 	int init_tbl_num;
45 	const char * const *vreg_list;
46 	int num_vregs;
47 };
48 
49 struct eusb2_repeater {
50 	struct device *dev;
51 	struct regmap *regmap;
52 	struct phy *phy;
53 	struct regulator_bulk_data *vregs;
54 	const struct eusb2_repeater_cfg *cfg;
55 	u16 base;
56 	enum phy_mode mode;
57 };
58 
59 static const char * const pm8550b_vreg_l[] = {
60 	"vdd18", "vdd3",
61 };
62 
63 static const struct eusb2_repeater_init_tbl pm8550b_init_tbl[] = {
64 	QCOM_EUSB2_REPEATER_INIT_CFG(EUSB2_TUNE_IUSB2, 0x8),
65 	QCOM_EUSB2_REPEATER_INIT_CFG(EUSB2_TUNE_SQUELCH_U, 0x3),
66 	QCOM_EUSB2_REPEATER_INIT_CFG(EUSB2_TUNE_USB2_PREEM, 0x5),
67 };
68 
69 static const struct eusb2_repeater_cfg pm8550b_eusb2_cfg = {
70 	.init_tbl	= pm8550b_init_tbl,
71 	.init_tbl_num	= ARRAY_SIZE(pm8550b_init_tbl),
72 	.vreg_list	= pm8550b_vreg_l,
73 	.num_vregs	= ARRAY_SIZE(pm8550b_vreg_l),
74 };
75 
76 static int eusb2_repeater_init_vregs(struct eusb2_repeater *rptr)
77 {
78 	int num = rptr->cfg->num_vregs;
79 	struct device *dev = rptr->dev;
80 	int i;
81 
82 	rptr->vregs = devm_kcalloc(dev, num, sizeof(*rptr->vregs), GFP_KERNEL);
83 	if (!rptr->vregs)
84 		return -ENOMEM;
85 
86 	for (i = 0; i < num; i++)
87 		rptr->vregs[i].supply = rptr->cfg->vreg_list[i];
88 
89 	return devm_regulator_bulk_get(dev, num, rptr->vregs);
90 }
91 
92 static int eusb2_repeater_init(struct phy *phy)
93 {
94 	struct eusb2_repeater *rptr = phy_get_drvdata(phy);
95 	const struct eusb2_repeater_init_tbl *init_tbl = rptr->cfg->init_tbl;
96 	int num = rptr->cfg->init_tbl_num;
97 	u32 val;
98 	int ret;
99 	int i;
100 
101 	ret = regulator_bulk_enable(rptr->cfg->num_vregs, rptr->vregs);
102 	if (ret)
103 		return ret;
104 
105 	regmap_update_bits(rptr->regmap, rptr->base + EUSB2_EN_CTL1,
106 			   EUSB2_RPTR_EN, EUSB2_RPTR_EN);
107 
108 	for (i = 0; i < num; i++)
109 		regmap_update_bits(rptr->regmap,
110 				   rptr->base + init_tbl[i].offset,
111 				   init_tbl[i].val, init_tbl[i].val);
112 
113 	ret = regmap_read_poll_timeout(rptr->regmap,
114 				       rptr->base + EUSB2_RPTR_STATUS, val,
115 				       val & RPTR_OK, 10, 5);
116 	if (ret)
117 		dev_err(rptr->dev, "initialization timed-out\n");
118 
119 	return ret;
120 }
121 
122 static int eusb2_repeater_set_mode(struct phy *phy,
123 				   enum phy_mode mode, int submode)
124 {
125 	struct eusb2_repeater *rptr = phy_get_drvdata(phy);
126 
127 	switch (mode) {
128 	case PHY_MODE_USB_HOST:
129 		/*
130 		 * CM.Lx is prohibited when repeater is already into Lx state as
131 		 * per eUSB 1.2 Spec. Below implement software workaround until
132 		 * PHY and controller is fixing seen observation.
133 		 */
134 		regmap_update_bits(rptr->regmap, rptr->base + EUSB2_FORCE_EN_5,
135 				   F_CLK_19P2M_EN, F_CLK_19P2M_EN);
136 		regmap_update_bits(rptr->regmap, rptr->base + EUSB2_FORCE_VAL_5,
137 				   V_CLK_19P2M_EN, V_CLK_19P2M_EN);
138 		break;
139 	case PHY_MODE_USB_DEVICE:
140 		/*
141 		 * In device mode clear host mode related workaround as there
142 		 * is no repeater reset available, and enable/disable of
143 		 * repeater doesn't clear previous value due to shared
144 		 * regulators (say host <-> device mode switch).
145 		 */
146 		regmap_update_bits(rptr->regmap, rptr->base + EUSB2_FORCE_EN_5,
147 				   F_CLK_19P2M_EN, 0);
148 		regmap_update_bits(rptr->regmap, rptr->base + EUSB2_FORCE_VAL_5,
149 				   V_CLK_19P2M_EN, 0);
150 		break;
151 	default:
152 		return -EINVAL;
153 	}
154 
155 	return 0;
156 }
157 
158 static int eusb2_repeater_exit(struct phy *phy)
159 {
160 	struct eusb2_repeater *rptr = phy_get_drvdata(phy);
161 
162 	return regulator_bulk_disable(rptr->cfg->num_vregs, rptr->vregs);
163 }
164 
165 static const struct phy_ops eusb2_repeater_ops = {
166 	.init		= eusb2_repeater_init,
167 	.exit		= eusb2_repeater_exit,
168 	.set_mode	= eusb2_repeater_set_mode,
169 	.owner		= THIS_MODULE,
170 };
171 
172 static int eusb2_repeater_probe(struct platform_device *pdev)
173 {
174 	struct eusb2_repeater *rptr;
175 	struct device *dev = &pdev->dev;
176 	struct phy_provider *phy_provider;
177 	struct device_node *np = dev->of_node;
178 	u32 res;
179 	int ret;
180 
181 	rptr = devm_kzalloc(dev, sizeof(*rptr), GFP_KERNEL);
182 	if (!rptr)
183 		return -ENOMEM;
184 
185 	rptr->dev = dev;
186 	dev_set_drvdata(dev, rptr);
187 
188 	rptr->cfg = of_device_get_match_data(dev);
189 	if (!rptr->cfg)
190 		return -EINVAL;
191 
192 	rptr->regmap = dev_get_regmap(dev->parent, NULL);
193 	if (!rptr->regmap)
194 		return -ENODEV;
195 
196 	ret = of_property_read_u32(np, "reg", &res);
197 	if (ret < 0)
198 		return ret;
199 
200 	rptr->base = res;
201 
202 	ret = eusb2_repeater_init_vregs(rptr);
203 	if (ret < 0) {
204 		dev_err(dev, "unable to get supplies\n");
205 		return ret;
206 	}
207 
208 	rptr->phy = devm_phy_create(dev, np, &eusb2_repeater_ops);
209 	if (IS_ERR(rptr->phy)) {
210 		dev_err(dev, "failed to create PHY: %d\n", ret);
211 		return PTR_ERR(rptr->phy);
212 	}
213 
214 	phy_set_drvdata(rptr->phy, rptr);
215 
216 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
217 	if (IS_ERR(phy_provider))
218 		return PTR_ERR(phy_provider);
219 
220 	dev_info(dev, "Registered Qcom-eUSB2 repeater\n");
221 
222 	return 0;
223 }
224 
225 static void eusb2_repeater_remove(struct platform_device *pdev)
226 {
227 	struct eusb2_repeater *rptr = platform_get_drvdata(pdev);
228 
229 	if (!rptr)
230 		return;
231 
232 	eusb2_repeater_exit(rptr->phy);
233 }
234 
235 static const struct of_device_id eusb2_repeater_of_match_table[] = {
236 	{
237 		.compatible = "qcom,pm8550b-eusb2-repeater",
238 		.data = &pm8550b_eusb2_cfg,
239 	},
240 	{ },
241 };
242 MODULE_DEVICE_TABLE(of, eusb2_repeater_of_match_table);
243 
244 static struct platform_driver eusb2_repeater_driver = {
245 	.probe		= eusb2_repeater_probe,
246 	.remove_new	= eusb2_repeater_remove,
247 	.driver = {
248 		.name	= "qcom-eusb2-repeater",
249 		.of_match_table = eusb2_repeater_of_match_table,
250 	},
251 };
252 
253 module_platform_driver(eusb2_repeater_driver);
254 
255 MODULE_DESCRIPTION("Qualcomm PMIC eUSB2 Repeater driver");
256 MODULE_LICENSE("GPL");
257