xref: /linux/drivers/usb/typec/mux/ps883x.c (revision f5e9d31e79c1ce8ba948ecac74d75e9c8d2f0c87)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Parade ps883x usb retimer driver
4  *
5  * Copyright (C) 2024 Linaro Ltd.
6  */
7 
8 #include <drm/bridge/aux-bridge.h>
9 #include <linux/clk.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/usb/pd.h>
18 #include <linux/usb/typec_altmode.h>
19 #include <linux/usb/typec_dp.h>
20 #include <linux/usb/typec_mux.h>
21 #include <linux/usb/typec_retimer.h>
22 #include <linux/usb/typec_tbt.h>
23 
24 #define REG_USB_PORT_CONN_STATUS_0		0x00
25 
26 #define CONN_STATUS_0_CONNECTION_PRESENT	BIT(0)
27 #define CONN_STATUS_0_ORIENTATION_REVERSED	BIT(1)
28 #define CONN_STATUS_0_ACTIVE_CABLE		BIT(2)
29 #define CONN_STATUS_0_USB_3_1_CONNECTED		BIT(5)
30 
31 #define REG_USB_PORT_CONN_STATUS_1		0x01
32 
33 #define CONN_STATUS_1_DP_CONNECTED		BIT(0)
34 #define CONN_STATUS_1_DP_SINK_REQUESTED		BIT(1)
35 #define CONN_STATUS_1_DP_PIN_ASSIGNMENT_C_D	BIT(2)
36 #define CONN_STATUS_1_DP_HPD_LEVEL		BIT(7)
37 
38 #define REG_USB_PORT_CONN_STATUS_2		0x02
39 
40 #define CONN_STATUS_2_TBT_CONNECTED		BIT(0)
41 #define CONN_STATUS_2_TBT_UNIDIR_LSRX_ACT_LT	BIT(4)
42 #define CONN_STATUS_2_USB4_CONNECTED		BIT(7)
43 
44 struct ps883x_retimer {
45 	struct i2c_client *client;
46 	struct gpio_desc *reset_gpio;
47 	struct regmap *regmap;
48 	struct typec_switch_dev *sw;
49 	struct typec_retimer *retimer;
50 	struct clk *xo_clk;
51 	struct regulator *vdd_supply;
52 	struct regulator *vdd33_supply;
53 	struct regulator *vdd33_cap_supply;
54 	struct regulator *vddat_supply;
55 	struct regulator *vddar_supply;
56 	struct regulator *vddio_supply;
57 
58 	struct typec_switch *typec_switch;
59 	struct typec_mux *typec_mux;
60 
61 	struct mutex lock; /* protect non-concurrent retimer & switch */
62 
63 	enum typec_orientation orientation;
64 	u8 cfg0;
65 	u8 cfg1;
66 	u8 cfg2;
67 };
68 
ps883x_configure(struct ps883x_retimer * retimer,int cfg0,int cfg1,int cfg2)69 static int ps883x_configure(struct ps883x_retimer *retimer, int cfg0,
70 			    int cfg1, int cfg2)
71 {
72 	struct device *dev = &retimer->client->dev;
73 	int ret;
74 
75 	if (retimer->cfg0 == cfg0 && retimer->cfg1 == cfg1 && retimer->cfg2 == cfg2)
76 		return 0;
77 
78 	ret = regmap_write(retimer->regmap, REG_USB_PORT_CONN_STATUS_0, cfg0);
79 	if (ret) {
80 		dev_err(dev, "failed to write conn_status_0: %d\n", ret);
81 		return ret;
82 	}
83 
84 	ret = regmap_write(retimer->regmap, REG_USB_PORT_CONN_STATUS_1, cfg1);
85 	if (ret) {
86 		dev_err(dev, "failed to write conn_status_1: %d\n", ret);
87 		return ret;
88 	}
89 
90 	ret = regmap_write(retimer->regmap, REG_USB_PORT_CONN_STATUS_2, cfg2);
91 	if (ret) {
92 		dev_err(dev, "failed to write conn_status_2: %d\n", ret);
93 		return ret;
94 	}
95 
96 	retimer->cfg0 = cfg0;
97 	retimer->cfg1 = cfg1;
98 	retimer->cfg2 = cfg2;
99 
100 	return 0;
101 }
102 
ps883x_set(struct ps883x_retimer * retimer,struct typec_retimer_state * state)103 static int ps883x_set(struct ps883x_retimer *retimer, struct typec_retimer_state *state)
104 {
105 	struct typec_thunderbolt_data *tb_data;
106 	const struct enter_usb_data *eudo_data;
107 	int cfg0 = CONN_STATUS_0_CONNECTION_PRESENT;
108 	int cfg1 = 0x00;
109 	int cfg2 = 0x00;
110 
111 	if (retimer->orientation == TYPEC_ORIENTATION_REVERSE)
112 		cfg0 |= CONN_STATUS_0_ORIENTATION_REVERSED;
113 
114 	if (state->alt) {
115 		switch (state->alt->svid) {
116 		case USB_TYPEC_DP_SID:
117 			cfg1 |= CONN_STATUS_1_DP_CONNECTED |
118 				CONN_STATUS_1_DP_HPD_LEVEL;
119 
120 			switch (state->mode)  {
121 			case TYPEC_DP_STATE_C:
122 				cfg1 |= CONN_STATUS_1_DP_SINK_REQUESTED |
123 					CONN_STATUS_1_DP_PIN_ASSIGNMENT_C_D;
124 				fallthrough;
125 			case TYPEC_DP_STATE_D:
126 				cfg1 |= CONN_STATUS_0_USB_3_1_CONNECTED;
127 				break;
128 			default: /* MODE_E */
129 				break;
130 			}
131 			break;
132 		case USB_TYPEC_TBT_SID:
133 			tb_data = state->data;
134 
135 			/* Unconditional */
136 			cfg2 |= CONN_STATUS_2_TBT_CONNECTED;
137 
138 			if (tb_data->cable_mode & TBT_CABLE_ACTIVE_PASSIVE)
139 				cfg0 |= CONN_STATUS_0_ACTIVE_CABLE;
140 
141 			if (tb_data->enter_vdo & TBT_ENTER_MODE_UNI_DIR_LSRX)
142 				cfg2 |= CONN_STATUS_2_TBT_UNIDIR_LSRX_ACT_LT;
143 			break;
144 		default:
145 			dev_err(&retimer->client->dev, "Got unsupported SID: 0x%x\n",
146 				state->alt->svid);
147 			return -EOPNOTSUPP;
148 		}
149 	} else {
150 		switch (state->mode) {
151 		case TYPEC_STATE_SAFE:
152 		/* USB2 pins don't even go through this chip */
153 		case TYPEC_MODE_USB2:
154 			break;
155 		case TYPEC_STATE_USB:
156 		case TYPEC_MODE_USB3:
157 			cfg0 |= CONN_STATUS_0_USB_3_1_CONNECTED;
158 			break;
159 		case TYPEC_MODE_USB4:
160 			eudo_data = state->data;
161 
162 			cfg2 |= CONN_STATUS_2_USB4_CONNECTED;
163 
164 			if (FIELD_GET(EUDO_CABLE_TYPE_MASK, eudo_data->eudo) != EUDO_CABLE_TYPE_PASSIVE)
165 				cfg0 |= CONN_STATUS_0_ACTIVE_CABLE;
166 			break;
167 		default:
168 			dev_err(&retimer->client->dev, "Got unsupported mode: %lu\n",
169 				state->mode);
170 			return -EOPNOTSUPP;
171 		}
172 	}
173 
174 	return ps883x_configure(retimer, cfg0, cfg1, cfg2);
175 }
176 
ps883x_sw_set(struct typec_switch_dev * sw,enum typec_orientation orientation)177 static int ps883x_sw_set(struct typec_switch_dev *sw,
178 			 enum typec_orientation orientation)
179 {
180 	struct ps883x_retimer *retimer = typec_switch_get_drvdata(sw);
181 	int ret = 0;
182 
183 	ret = typec_switch_set(retimer->typec_switch, orientation);
184 	if (ret)
185 		return ret;
186 
187 	mutex_lock(&retimer->lock);
188 
189 	if (retimer->orientation != orientation) {
190 		retimer->orientation = orientation;
191 
192 		ret = regmap_assign_bits(retimer->regmap, REG_USB_PORT_CONN_STATUS_0,
193 					 CONN_STATUS_0_ORIENTATION_REVERSED,
194 					 orientation == TYPEC_ORIENTATION_REVERSE);
195 		if (ret)
196 			dev_err(&retimer->client->dev, "failed to set orientation: %d\n", ret);
197 	}
198 
199 	mutex_unlock(&retimer->lock);
200 
201 	return ret;
202 }
203 
ps883x_retimer_set(struct typec_retimer * rtmr,struct typec_retimer_state * state)204 static int ps883x_retimer_set(struct typec_retimer *rtmr,
205 			      struct typec_retimer_state *state)
206 {
207 	struct ps883x_retimer *retimer = typec_retimer_get_drvdata(rtmr);
208 	struct typec_mux_state mux_state;
209 	int ret = 0;
210 
211 	mutex_lock(&retimer->lock);
212 	ret = ps883x_set(retimer, state);
213 	mutex_unlock(&retimer->lock);
214 
215 	if (ret)
216 		return ret;
217 
218 	mux_state.alt = state->alt;
219 	mux_state.data = state->data;
220 	mux_state.mode = state->mode;
221 
222 	return typec_mux_set(retimer->typec_mux, &mux_state);
223 }
224 
ps883x_enable_vregs(struct ps883x_retimer * retimer)225 static int ps883x_enable_vregs(struct ps883x_retimer *retimer)
226 {
227 	struct device *dev = &retimer->client->dev;
228 	int ret;
229 
230 	ret = regulator_enable(retimer->vdd33_supply);
231 	if (ret) {
232 		dev_err(dev, "cannot enable VDD 3.3V regulator: %d\n", ret);
233 		return ret;
234 	}
235 
236 	ret = regulator_enable(retimer->vdd33_cap_supply);
237 	if (ret) {
238 		dev_err(dev, "cannot enable VDD 3.3V CAP regulator: %d\n", ret);
239 		goto err_vdd33_disable;
240 	}
241 
242 	usleep_range(4000, 10000);
243 
244 	ret = regulator_enable(retimer->vdd_supply);
245 	if (ret) {
246 		dev_err(dev, "cannot enable VDD regulator: %d\n", ret);
247 		goto err_vdd33_cap_disable;
248 	}
249 
250 	ret = regulator_enable(retimer->vddar_supply);
251 	if (ret) {
252 		dev_err(dev, "cannot enable VDD AR regulator: %d\n", ret);
253 		goto err_vdd_disable;
254 	}
255 
256 	ret = regulator_enable(retimer->vddat_supply);
257 	if (ret) {
258 		dev_err(dev, "cannot enable VDD AT regulator: %d\n", ret);
259 		goto err_vddar_disable;
260 	}
261 
262 	ret = regulator_enable(retimer->vddio_supply);
263 	if (ret) {
264 		dev_err(dev, "cannot enable VDD IO regulator: %d\n", ret);
265 		goto err_vddat_disable;
266 	}
267 
268 	return 0;
269 
270 err_vddat_disable:
271 	regulator_disable(retimer->vddat_supply);
272 err_vddar_disable:
273 	regulator_disable(retimer->vddar_supply);
274 err_vdd_disable:
275 	regulator_disable(retimer->vdd_supply);
276 err_vdd33_cap_disable:
277 	regulator_disable(retimer->vdd33_cap_supply);
278 err_vdd33_disable:
279 	regulator_disable(retimer->vdd33_supply);
280 
281 	return ret;
282 }
283 
ps883x_disable_vregs(struct ps883x_retimer * retimer)284 static void ps883x_disable_vregs(struct ps883x_retimer *retimer)
285 {
286 	regulator_disable(retimer->vddio_supply);
287 	regulator_disable(retimer->vddat_supply);
288 	regulator_disable(retimer->vddar_supply);
289 	regulator_disable(retimer->vdd_supply);
290 	regulator_disable(retimer->vdd33_cap_supply);
291 	regulator_disable(retimer->vdd33_supply);
292 }
293 
ps883x_get_vregs(struct ps883x_retimer * retimer)294 static int ps883x_get_vregs(struct ps883x_retimer *retimer)
295 {
296 	struct device *dev = &retimer->client->dev;
297 
298 	retimer->vdd_supply = devm_regulator_get(dev, "vdd");
299 	if (IS_ERR(retimer->vdd_supply))
300 		return dev_err_probe(dev, PTR_ERR(retimer->vdd_supply),
301 				     "failed to get VDD\n");
302 
303 	retimer->vdd33_supply = devm_regulator_get(dev, "vdd33");
304 	if (IS_ERR(retimer->vdd33_supply))
305 		return dev_err_probe(dev, PTR_ERR(retimer->vdd33_supply),
306 				     "failed to get VDD 3.3V\n");
307 
308 	retimer->vdd33_cap_supply = devm_regulator_get(dev, "vdd33-cap");
309 	if (IS_ERR(retimer->vdd33_cap_supply))
310 		return dev_err_probe(dev, PTR_ERR(retimer->vdd33_cap_supply),
311 				     "failed to get VDD CAP 3.3V\n");
312 
313 	retimer->vddat_supply = devm_regulator_get(dev, "vddat");
314 	if (IS_ERR(retimer->vddat_supply))
315 		return dev_err_probe(dev, PTR_ERR(retimer->vddat_supply),
316 				     "failed to get VDD AT\n");
317 
318 	retimer->vddar_supply = devm_regulator_get(dev, "vddar");
319 	if (IS_ERR(retimer->vddar_supply))
320 		return dev_err_probe(dev, PTR_ERR(retimer->vddar_supply),
321 				     "failed to get VDD AR\n");
322 
323 	retimer->vddio_supply = devm_regulator_get(dev, "vddio");
324 	if (IS_ERR(retimer->vddio_supply))
325 		return dev_err_probe(dev, PTR_ERR(retimer->vddio_supply),
326 				     "failed to get VDD IO\n");
327 
328 	return 0;
329 }
330 
331 static const struct regmap_config ps883x_retimer_regmap = {
332 	.max_register = 0x1f,
333 	.reg_bits = 8,
334 	.val_bits = 8,
335 };
336 
ps883x_retimer_probe(struct i2c_client * client)337 static int ps883x_retimer_probe(struct i2c_client *client)
338 {
339 	struct device *dev = &client->dev;
340 	struct typec_switch_desc sw_desc = { };
341 	struct typec_retimer_desc rtmr_desc = { };
342 	struct ps883x_retimer *retimer;
343 	unsigned int val;
344 	int ret;
345 
346 	retimer = devm_kzalloc(dev, sizeof(*retimer), GFP_KERNEL);
347 	if (!retimer)
348 		return -ENOMEM;
349 
350 	retimer->client = client;
351 
352 	mutex_init(&retimer->lock);
353 
354 	retimer->regmap = devm_regmap_init_i2c(client, &ps883x_retimer_regmap);
355 	if (IS_ERR(retimer->regmap))
356 		return dev_err_probe(dev, PTR_ERR(retimer->regmap),
357 				     "failed to allocate register map\n");
358 
359 	ret = ps883x_get_vregs(retimer);
360 	if (ret)
361 		return ret;
362 
363 	retimer->xo_clk = devm_clk_get(dev, NULL);
364 	if (IS_ERR(retimer->xo_clk))
365 		return dev_err_probe(dev, PTR_ERR(retimer->xo_clk),
366 				     "failed to get xo clock\n");
367 
368 	retimer->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_ASIS);
369 	if (IS_ERR(retimer->reset_gpio))
370 		return dev_err_probe(dev, PTR_ERR(retimer->reset_gpio),
371 				     "failed to get reset gpio\n");
372 
373 	retimer->typec_switch = typec_switch_get(dev);
374 	if (IS_ERR(retimer->typec_switch))
375 		return dev_err_probe(dev, PTR_ERR(retimer->typec_switch),
376 				     "failed to acquire orientation-switch\n");
377 
378 	retimer->typec_mux = typec_mux_get(dev);
379 	if (IS_ERR(retimer->typec_mux)) {
380 		ret = dev_err_probe(dev, PTR_ERR(retimer->typec_mux),
381 				    "failed to acquire mode-mux\n");
382 		goto err_switch_put;
383 	}
384 
385 	ret = drm_aux_bridge_register(dev);
386 	if (ret)
387 		goto err_mux_put;
388 
389 	ret = ps883x_enable_vregs(retimer);
390 	if (ret)
391 		goto err_mux_put;
392 
393 	ret = clk_prepare_enable(retimer->xo_clk);
394 	if (ret) {
395 		dev_err(dev, "failed to enable XO: %d\n", ret);
396 		goto err_vregs_disable;
397 	}
398 
399 	/* skip resetting if already configured */
400 	if (regmap_test_bits(retimer->regmap, REG_USB_PORT_CONN_STATUS_0,
401 			     CONN_STATUS_0_CONNECTION_PRESENT) == 1) {
402 		gpiod_direction_output(retimer->reset_gpio, 0);
403 	} else {
404 		gpiod_direction_output(retimer->reset_gpio, 1);
405 
406 		/* VDD IO supply enable to reset release delay */
407 		usleep_range(4000, 14000);
408 
409 		gpiod_set_value(retimer->reset_gpio, 0);
410 
411 		/* firmware initialization delay */
412 		msleep(60);
413 
414 		/* make sure device is accessible */
415 		ret = regmap_read(retimer->regmap, REG_USB_PORT_CONN_STATUS_0,
416 				  &val);
417 		if (ret) {
418 			dev_err(dev, "failed to read conn_status_0: %d\n", ret);
419 			if (ret == -ENXIO)
420 				ret = -EIO;
421 			goto err_clk_disable;
422 		}
423 	}
424 
425 	sw_desc.drvdata = retimer;
426 	sw_desc.fwnode = dev_fwnode(dev);
427 	sw_desc.set = ps883x_sw_set;
428 
429 	retimer->sw = typec_switch_register(dev, &sw_desc);
430 	if (IS_ERR(retimer->sw)) {
431 		ret = PTR_ERR(retimer->sw);
432 		dev_err(dev, "failed to register typec switch: %d\n", ret);
433 		goto err_clk_disable;
434 	}
435 
436 	rtmr_desc.drvdata = retimer;
437 	rtmr_desc.fwnode = dev_fwnode(dev);
438 	rtmr_desc.set = ps883x_retimer_set;
439 
440 	retimer->retimer = typec_retimer_register(dev, &rtmr_desc);
441 	if (IS_ERR(retimer->retimer)) {
442 		ret = PTR_ERR(retimer->retimer);
443 		dev_err(dev, "failed to register typec retimer: %d\n", ret);
444 		goto err_switch_unregister;
445 	}
446 
447 	return 0;
448 
449 err_switch_unregister:
450 	typec_switch_unregister(retimer->sw);
451 err_clk_disable:
452 	clk_disable_unprepare(retimer->xo_clk);
453 err_vregs_disable:
454 	gpiod_set_value(retimer->reset_gpio, 1);
455 	ps883x_disable_vregs(retimer);
456 err_mux_put:
457 	typec_mux_put(retimer->typec_mux);
458 err_switch_put:
459 	typec_switch_put(retimer->typec_switch);
460 
461 	return ret;
462 }
463 
ps883x_retimer_remove(struct i2c_client * client)464 static void ps883x_retimer_remove(struct i2c_client *client)
465 {
466 	struct ps883x_retimer *retimer = i2c_get_clientdata(client);
467 
468 	typec_retimer_unregister(retimer->retimer);
469 	typec_switch_unregister(retimer->sw);
470 
471 	gpiod_set_value(retimer->reset_gpio, 1);
472 
473 	clk_disable_unprepare(retimer->xo_clk);
474 
475 	ps883x_disable_vregs(retimer);
476 
477 	typec_mux_put(retimer->typec_mux);
478 	typec_switch_put(retimer->typec_switch);
479 }
480 
481 static const struct of_device_id ps883x_retimer_of_table[] = {
482 	{ .compatible = "parade,ps8830" },
483 	{ }
484 };
485 MODULE_DEVICE_TABLE(of, ps883x_retimer_of_table);
486 
487 static struct i2c_driver ps883x_retimer_driver = {
488 	.driver = {
489 		.name = "ps883x_retimer",
490 		.of_match_table = ps883x_retimer_of_table,
491 	},
492 	.probe		= ps883x_retimer_probe,
493 	.remove		= ps883x_retimer_remove,
494 };
495 
496 module_i2c_driver(ps883x_retimer_driver);
497 
498 MODULE_DESCRIPTION("Parade ps883x Type-C Retimer driver");
499 MODULE_LICENSE("GPL");
500