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