• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * STMicroelectronics STUSB160x Type-C controller family driver
4  *
5  * Copyright (C) 2020, STMicroelectronics
6  * Author(s): Amelie Delaunay <amelie.delaunay@st.com>
7  */
8 
9 #include <linux/bitfield.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/usb/role.h>
17 #include <linux/usb/typec.h>
18 
19 #define STUSB160X_ALERT_STATUS			0x0B /* RC */
20 #define STUSB160X_ALERT_STATUS_MASK_CTRL	0x0C /* RW */
21 #define STUSB160X_CC_CONNECTION_STATUS_TRANS	0x0D /* RC */
22 #define STUSB160X_CC_CONNECTION_STATUS		0x0E /* RO */
23 #define STUSB160X_MONITORING_STATUS_TRANS	0x0F /* RC */
24 #define STUSB160X_MONITORING_STATUS		0x10 /* RO */
25 #define STUSB160X_CC_OPERATION_STATUS		0x11 /* RO */
26 #define STUSB160X_HW_FAULT_STATUS_TRANS		0x12 /* RC */
27 #define STUSB160X_HW_FAULT_STATUS		0x13 /* RO */
28 #define STUSB160X_CC_CAPABILITY_CTRL		0x18 /* RW */
29 #define STUSB160X_CC_VCONN_SWITCH_CTRL		0x1E /* RW */
30 #define STUSB160X_VCONN_MONITORING_CTRL		0x20 /* RW */
31 #define STUSB160X_VBUS_MONITORING_RANGE_CTRL	0x22 /* RW */
32 #define STUSB160X_RESET_CTRL			0x23 /* RW */
33 #define STUSB160X_VBUS_DISCHARGE_TIME_CTRL	0x25 /* RW */
34 #define STUSB160X_VBUS_DISCHARGE_STATUS		0x26 /* RO */
35 #define STUSB160X_VBUS_ENABLE_STATUS		0x27 /* RO */
36 #define STUSB160X_CC_POWER_MODE_CTRL		0x28 /* RW */
37 #define STUSB160X_VBUS_MONITORING_CTRL		0x2E /* RW */
38 #define STUSB1600_REG_MAX			0x2F /* RO - Reserved */
39 
40 /* STUSB160X_ALERT_STATUS/STUSB160X_ALERT_STATUS_MASK_CTRL bitfields */
41 #define STUSB160X_HW_FAULT			BIT(4)
42 #define STUSB160X_MONITORING			BIT(5)
43 #define STUSB160X_CC_CONNECTION			BIT(6)
44 #define STUSB160X_ALL_ALERTS			GENMASK(6, 4)
45 
46 /* STUSB160X_CC_CONNECTION_STATUS_TRANS bitfields */
47 #define STUSB160X_CC_ATTACH_TRANS		BIT(0)
48 
49 /* STUSB160X_CC_CONNECTION_STATUS bitfields */
50 #define STUSB160X_CC_ATTACH			BIT(0)
51 #define STUSB160X_CC_VCONN_SUPPLY		BIT(1)
52 #define STUSB160X_CC_DATA_ROLE(s)		(!!((s) & BIT(2)))
53 #define STUSB160X_CC_POWER_ROLE(s)		(!!((s) & BIT(3)))
54 #define STUSB160X_CC_ATTACHED_MODE		GENMASK(7, 5)
55 
56 /* STUSB160X_MONITORING_STATUS_TRANS bitfields */
57 #define STUSB160X_VCONN_PRESENCE_TRANS		BIT(0)
58 #define STUSB160X_VBUS_PRESENCE_TRANS		BIT(1)
59 #define STUSB160X_VBUS_VSAFE0V_TRANS		BIT(2)
60 #define STUSB160X_VBUS_VALID_TRANS		BIT(3)
61 
62 /* STUSB160X_MONITORING_STATUS bitfields */
63 #define STUSB160X_VCONN_PRESENCE		BIT(0)
64 #define STUSB160X_VBUS_PRESENCE			BIT(1)
65 #define STUSB160X_VBUS_VSAFE0V			BIT(2)
66 #define STUSB160X_VBUS_VALID			BIT(3)
67 
68 /* STUSB160X_CC_OPERATION_STATUS bitfields */
69 #define STUSB160X_TYPEC_FSM_STATE		GENMASK(4, 0)
70 #define STUSB160X_SINK_POWER_STATE		GENMASK(6, 5)
71 #define STUSB160X_CC_ATTACHED			BIT(7)
72 
73 /* STUSB160X_HW_FAULT_STATUS_TRANS bitfields */
74 #define STUSB160X_VCONN_SW_OVP_FAULT_TRANS	BIT(0)
75 #define STUSB160X_VCONN_SW_OCP_FAULT_TRANS	BIT(1)
76 #define STUSB160X_VCONN_SW_RVP_FAULT_TRANS	BIT(2)
77 #define STUSB160X_VPU_VALID_TRANS		BIT(4)
78 #define STUSB160X_VPU_OVP_FAULT_TRANS		BIT(5)
79 #define STUSB160X_THERMAL_FAULT			BIT(7)
80 
81 /* STUSB160X_HW_FAULT_STATUS bitfields */
82 #define STUSB160X_VCONN_SW_OVP_FAULT_CC2	BIT(0)
83 #define STUSB160X_VCONN_SW_OVP_FAULT_CC1	BIT(1)
84 #define STUSB160X_VCONN_SW_OCP_FAULT_CC2	BIT(2)
85 #define STUSB160X_VCONN_SW_OCP_FAULT_CC1	BIT(3)
86 #define STUSB160X_VCONN_SW_RVP_FAULT_CC2	BIT(4)
87 #define STUSB160X_VCONN_SW_RVP_FAULT_CC1	BIT(5)
88 #define STUSB160X_VPU_VALID			BIT(6)
89 #define STUSB160X_VPU_OVP_FAULT			BIT(7)
90 
91 /* STUSB160X_CC_CAPABILITY_CTRL bitfields */
92 #define STUSB160X_CC_VCONN_SUPPLY_EN		BIT(0)
93 #define STUSB160X_CC_VCONN_DISCHARGE_EN		BIT(4)
94 #define STUSB160X_CC_CURRENT_ADVERTISED		GENMASK(7, 6)
95 
96 /* STUSB160X_VCONN_SWITCH_CTRL bitfields */
97 #define STUSB160X_CC_VCONN_SWITCH_ILIM		GENMASK(3, 0)
98 
99 /* STUSB160X_VCONN_MONITORING_CTRL bitfields */
100 #define STUSB160X_VCONN_UVLO_THRESHOLD		BIT(6)
101 #define STUSB160X_VCONN_MONITORING_EN		BIT(7)
102 
103 /* STUSB160X_VBUS_MONITORING_RANGE_CTRL bitfields */
104 #define STUSB160X_SHIFT_LOW_VBUS_LIMIT		GENMASK(3, 0)
105 #define STUSB160X_SHIFT_HIGH_VBUS_LIMIT		GENMASK(7, 4)
106 
107 /* STUSB160X_RESET_CTRL bitfields */
108 #define STUSB160X_SW_RESET_EN			BIT(0)
109 
110 /* STUSB160X_VBUS_DISCHARGE_TIME_CTRL bitfields */
111 #define STUSBXX02_VBUS_DISCHARGE_TIME_TO_PDO	GENMASK(3, 0)
112 #define STUSB160X_VBUS_DISCHARGE_TIME_TO_0V	GENMASK(7, 4)
113 
114 /* STUSB160X_VBUS_DISCHARGE_STATUS bitfields */
115 #define STUSB160X_VBUS_DISCHARGE_EN		BIT(7)
116 
117 /* STUSB160X_VBUS_ENABLE_STATUS bitfields */
118 #define STUSB160X_VBUS_SOURCE_EN		BIT(0)
119 #define STUSB160X_VBUS_SINK_EN			BIT(1)
120 
121 /* STUSB160X_CC_POWER_MODE_CTRL bitfields */
122 #define STUSB160X_CC_POWER_MODE			GENMASK(2, 0)
123 
124 /* STUSB160X_VBUS_MONITORING_CTRL bitfields */
125 #define STUSB160X_VDD_UVLO_DISABLE		BIT(0)
126 #define STUSB160X_VBUS_VSAFE0V_THRESHOLD	GENMASK(2, 1)
127 #define STUSB160X_VBUS_RANGE_DISABLE		BIT(4)
128 #define STUSB160X_VDD_OVLO_DISABLE		BIT(6)
129 
130 enum stusb160x_pwr_mode {
131 	SOURCE_WITH_ACCESSORY,
132 	SINK_WITH_ACCESSORY,
133 	SINK_WITHOUT_ACCESSORY,
134 	DUAL_WITH_ACCESSORY,
135 	DUAL_WITH_ACCESSORY_AND_TRY_SRC,
136 	DUAL_WITH_ACCESSORY_AND_TRY_SNK,
137 };
138 
139 enum stusb160x_attached_mode {
140 	NO_DEVICE_ATTACHED,
141 	SINK_ATTACHED,
142 	SOURCE_ATTACHED,
143 	DEBUG_ACCESSORY_ATTACHED,
144 	AUDIO_ACCESSORY_ATTACHED,
145 };
146 
147 struct stusb160x {
148 	struct device		*dev;
149 	struct regmap		*regmap;
150 	struct regulator	*vdd_supply;
151 	struct regulator	*vsys_supply;
152 	struct regulator	*vconn_supply;
153 	struct regulator	*main_supply;
154 
155 	struct typec_port	*port;
156 	struct typec_capability capability;
157 	struct typec_partner	*partner;
158 
159 	enum typec_port_type	port_type;
160 	enum typec_pwr_opmode	pwr_opmode;
161 	bool			vbus_on;
162 
163 	struct usb_role_switch	*role_sw;
164 };
165 
stusb160x_reg_writeable(struct device * dev,unsigned int reg)166 static bool stusb160x_reg_writeable(struct device *dev, unsigned int reg)
167 {
168 	switch (reg) {
169 	case STUSB160X_ALERT_STATUS_MASK_CTRL:
170 	case STUSB160X_CC_CAPABILITY_CTRL:
171 	case STUSB160X_CC_VCONN_SWITCH_CTRL:
172 	case STUSB160X_VCONN_MONITORING_CTRL:
173 	case STUSB160X_VBUS_MONITORING_RANGE_CTRL:
174 	case STUSB160X_RESET_CTRL:
175 	case STUSB160X_VBUS_DISCHARGE_TIME_CTRL:
176 	case STUSB160X_CC_POWER_MODE_CTRL:
177 	case STUSB160X_VBUS_MONITORING_CTRL:
178 		return true;
179 	default:
180 		return false;
181 	}
182 }
183 
stusb160x_reg_readable(struct device * dev,unsigned int reg)184 static bool stusb160x_reg_readable(struct device *dev, unsigned int reg)
185 {
186 	if (reg <= 0x0A ||
187 	    (reg >= 0x14 && reg <= 0x17) ||
188 	    (reg >= 0x19 && reg <= 0x1D) ||
189 	    (reg >= 0x29 && reg <= 0x2D) ||
190 	    (reg == 0x1F || reg == 0x21 || reg == 0x24 || reg == 0x2F))
191 		return false;
192 	else
193 		return true;
194 }
195 
stusb160x_reg_volatile(struct device * dev,unsigned int reg)196 static bool stusb160x_reg_volatile(struct device *dev, unsigned int reg)
197 {
198 	switch (reg) {
199 	case STUSB160X_ALERT_STATUS:
200 	case STUSB160X_CC_CONNECTION_STATUS_TRANS:
201 	case STUSB160X_CC_CONNECTION_STATUS:
202 	case STUSB160X_MONITORING_STATUS_TRANS:
203 	case STUSB160X_MONITORING_STATUS:
204 	case STUSB160X_CC_OPERATION_STATUS:
205 	case STUSB160X_HW_FAULT_STATUS_TRANS:
206 	case STUSB160X_HW_FAULT_STATUS:
207 	case STUSB160X_VBUS_DISCHARGE_STATUS:
208 	case STUSB160X_VBUS_ENABLE_STATUS:
209 		return true;
210 	default:
211 		return false;
212 	}
213 }
214 
stusb160x_reg_precious(struct device * dev,unsigned int reg)215 static bool stusb160x_reg_precious(struct device *dev, unsigned int reg)
216 {
217 	switch (reg) {
218 	case STUSB160X_ALERT_STATUS:
219 	case STUSB160X_CC_CONNECTION_STATUS_TRANS:
220 	case STUSB160X_MONITORING_STATUS_TRANS:
221 	case STUSB160X_HW_FAULT_STATUS_TRANS:
222 		return true;
223 	default:
224 		return false;
225 	}
226 }
227 
228 static const struct regmap_config stusb1600_regmap_config = {
229 	.reg_bits	= 8,
230 	.reg_stride	= 1,
231 	.val_bits	= 8,
232 	.max_register	= STUSB1600_REG_MAX,
233 	.writeable_reg	= stusb160x_reg_writeable,
234 	.readable_reg	= stusb160x_reg_readable,
235 	.volatile_reg	= stusb160x_reg_volatile,
236 	.precious_reg	= stusb160x_reg_precious,
237 	.cache_type	= REGCACHE_RBTREE,
238 };
239 
stusb160x_get_vconn(struct stusb160x * chip)240 static bool stusb160x_get_vconn(struct stusb160x *chip)
241 {
242 	u32 val;
243 	int ret;
244 
245 	ret = regmap_read(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL, &val);
246 	if (ret) {
247 		dev_err(chip->dev, "Unable to get Vconn status: %d\n", ret);
248 		return false;
249 	}
250 
251 	return !!FIELD_GET(STUSB160X_CC_VCONN_SUPPLY_EN, val);
252 }
253 
stusb160x_set_vconn(struct stusb160x * chip,bool on)254 static int stusb160x_set_vconn(struct stusb160x *chip, bool on)
255 {
256 	int ret;
257 
258 	/* Manage VCONN input supply */
259 	if (chip->vconn_supply) {
260 		if (on) {
261 			ret = regulator_enable(chip->vconn_supply);
262 			if (ret) {
263 				dev_err(chip->dev,
264 					"failed to enable vconn supply: %d\n",
265 					ret);
266 				return ret;
267 			}
268 		} else {
269 			regulator_disable(chip->vconn_supply);
270 		}
271 	}
272 
273 	/* Manage VCONN monitoring and power path */
274 	ret = regmap_update_bits(chip->regmap, STUSB160X_VCONN_MONITORING_CTRL,
275 				 STUSB160X_VCONN_MONITORING_EN,
276 				 on ? STUSB160X_VCONN_MONITORING_EN : 0);
277 	if (ret)
278 		goto vconn_reg_disable;
279 
280 	return 0;
281 
282 vconn_reg_disable:
283 	if (chip->vconn_supply && on)
284 		regulator_disable(chip->vconn_supply);
285 
286 	return ret;
287 }
288 
stusb160x_get_pwr_opmode(struct stusb160x * chip)289 static enum typec_pwr_opmode stusb160x_get_pwr_opmode(struct stusb160x *chip)
290 {
291 	u32 val;
292 	int ret;
293 
294 	ret = regmap_read(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL, &val);
295 	if (ret) {
296 		dev_err(chip->dev, "Unable to get pwr opmode: %d\n", ret);
297 		return TYPEC_PWR_MODE_USB;
298 	}
299 
300 	return FIELD_GET(STUSB160X_CC_CURRENT_ADVERTISED, val);
301 }
302 
stusb160x_get_accessory(u32 status)303 static enum typec_accessory stusb160x_get_accessory(u32 status)
304 {
305 	enum stusb160x_attached_mode mode;
306 
307 	mode = FIELD_GET(STUSB160X_CC_ATTACHED_MODE, status);
308 
309 	switch (mode) {
310 	case DEBUG_ACCESSORY_ATTACHED:
311 		return TYPEC_ACCESSORY_DEBUG;
312 	case AUDIO_ACCESSORY_ATTACHED:
313 		return TYPEC_ACCESSORY_AUDIO;
314 	default:
315 		return TYPEC_ACCESSORY_NONE;
316 	}
317 }
318 
stusb160x_get_vconn_role(u32 status)319 static enum typec_role stusb160x_get_vconn_role(u32 status)
320 {
321 	if (FIELD_GET(STUSB160X_CC_VCONN_SUPPLY, status))
322 		return TYPEC_SOURCE;
323 
324 	return TYPEC_SINK;
325 }
326 
stusb160x_set_data_role(struct stusb160x * chip,enum typec_data_role data_role,bool attached)327 static void stusb160x_set_data_role(struct stusb160x *chip,
328 				    enum typec_data_role data_role,
329 				    bool attached)
330 {
331 	enum usb_role usb_role = USB_ROLE_NONE;
332 
333 	if (attached) {
334 		if (data_role == TYPEC_HOST)
335 			usb_role = USB_ROLE_HOST;
336 		else
337 			usb_role = USB_ROLE_DEVICE;
338 	}
339 
340 	usb_role_switch_set_role(chip->role_sw, usb_role);
341 	typec_set_data_role(chip->port, data_role);
342 }
343 
stusb160x_attach(struct stusb160x * chip,u32 status)344 static int stusb160x_attach(struct stusb160x *chip, u32 status)
345 {
346 	struct typec_partner_desc desc;
347 	int ret;
348 
349 	if ((STUSB160X_CC_POWER_ROLE(status) == TYPEC_SOURCE) &&
350 	    chip->vdd_supply) {
351 		ret = regulator_enable(chip->vdd_supply);
352 		if (ret) {
353 			dev_err(chip->dev,
354 				"Failed to enable Vbus supply: %d\n", ret);
355 			return ret;
356 		}
357 		chip->vbus_on = true;
358 	}
359 
360 	desc.usb_pd = false;
361 	desc.accessory = stusb160x_get_accessory(status);
362 	desc.identity = NULL;
363 
364 	chip->partner = typec_register_partner(chip->port, &desc);
365 	if (IS_ERR(chip->partner)) {
366 		ret = PTR_ERR(chip->partner);
367 		goto vbus_disable;
368 	}
369 
370 	typec_set_pwr_role(chip->port, STUSB160X_CC_POWER_ROLE(status));
371 	typec_set_pwr_opmode(chip->port, stusb160x_get_pwr_opmode(chip));
372 	typec_set_vconn_role(chip->port, stusb160x_get_vconn_role(status));
373 	stusb160x_set_data_role(chip, STUSB160X_CC_DATA_ROLE(status), true);
374 
375 	return 0;
376 
377 vbus_disable:
378 	if (chip->vbus_on) {
379 		regulator_disable(chip->vdd_supply);
380 		chip->vbus_on = false;
381 	}
382 
383 	return ret;
384 }
385 
stusb160x_detach(struct stusb160x * chip,u32 status)386 static void stusb160x_detach(struct stusb160x *chip, u32 status)
387 {
388 	typec_unregister_partner(chip->partner);
389 	chip->partner = NULL;
390 
391 	typec_set_pwr_role(chip->port, STUSB160X_CC_POWER_ROLE(status));
392 	typec_set_pwr_opmode(chip->port, TYPEC_PWR_MODE_USB);
393 	typec_set_vconn_role(chip->port, stusb160x_get_vconn_role(status));
394 	stusb160x_set_data_role(chip, STUSB160X_CC_DATA_ROLE(status), false);
395 
396 	if (chip->vbus_on) {
397 		regulator_disable(chip->vdd_supply);
398 		chip->vbus_on = false;
399 	}
400 }
401 
stusb160x_irq_handler(int irq,void * data)402 static irqreturn_t stusb160x_irq_handler(int irq, void *data)
403 {
404 	struct stusb160x *chip = data;
405 	u32 pending, trans, status;
406 	int ret;
407 
408 	ret = regmap_read(chip->regmap, STUSB160X_ALERT_STATUS, &pending);
409 	if (ret)
410 		goto err;
411 
412 	if (pending & STUSB160X_CC_CONNECTION) {
413 		ret = regmap_read(chip->regmap,
414 				  STUSB160X_CC_CONNECTION_STATUS_TRANS, &trans);
415 		if (ret)
416 			goto err;
417 		ret = regmap_read(chip->regmap,
418 				  STUSB160X_CC_CONNECTION_STATUS, &status);
419 		if (ret)
420 			goto err;
421 
422 		if (trans & STUSB160X_CC_ATTACH_TRANS) {
423 			if (status & STUSB160X_CC_ATTACH) {
424 				ret = stusb160x_attach(chip, status);
425 				if (ret)
426 					goto err;
427 			} else {
428 				stusb160x_detach(chip, status);
429 			}
430 		}
431 	}
432 err:
433 	return IRQ_HANDLED;
434 }
435 
stusb160x_irq_init(struct stusb160x * chip,int irq)436 static int stusb160x_irq_init(struct stusb160x *chip, int irq)
437 {
438 	u32 status;
439 	int ret;
440 
441 	ret = regmap_read(chip->regmap,
442 			  STUSB160X_CC_CONNECTION_STATUS, &status);
443 	if (ret)
444 		return ret;
445 
446 	if (status & STUSB160X_CC_ATTACH) {
447 		ret = stusb160x_attach(chip, status);
448 		if (ret)
449 			dev_err(chip->dev, "attach failed: %d\n", ret);
450 	}
451 
452 	ret = devm_request_threaded_irq(chip->dev, irq, NULL,
453 					stusb160x_irq_handler, IRQF_ONESHOT,
454 					dev_name(chip->dev), chip);
455 	if (ret)
456 		goto partner_unregister;
457 
458 	/* Unmask CC_CONNECTION events */
459 	ret = regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
460 				STUSB160X_CC_CONNECTION, 0);
461 	if (ret)
462 		goto partner_unregister;
463 
464 	return 0;
465 
466 partner_unregister:
467 	if (chip->partner) {
468 		typec_unregister_partner(chip->partner);
469 		chip->partner = NULL;
470 	}
471 
472 	return ret;
473 }
474 
stusb160x_chip_init(struct stusb160x * chip)475 static int stusb160x_chip_init(struct stusb160x *chip)
476 {
477 	u32 val;
478 	int ret;
479 
480 	/* Change the default Type-C power mode */
481 	if (chip->port_type == TYPEC_PORT_SRC)
482 		ret = regmap_update_bits(chip->regmap,
483 					 STUSB160X_CC_POWER_MODE_CTRL,
484 					 STUSB160X_CC_POWER_MODE,
485 					 SOURCE_WITH_ACCESSORY);
486 	else if (chip->port_type == TYPEC_PORT_SNK)
487 		ret = regmap_update_bits(chip->regmap,
488 					 STUSB160X_CC_POWER_MODE_CTRL,
489 					 STUSB160X_CC_POWER_MODE,
490 					 SINK_WITH_ACCESSORY);
491 	else /* (chip->port_type == TYPEC_PORT_DRP) */
492 		ret = regmap_update_bits(chip->regmap,
493 					 STUSB160X_CC_POWER_MODE_CTRL,
494 					 STUSB160X_CC_POWER_MODE,
495 					 DUAL_WITH_ACCESSORY);
496 	if (ret)
497 		return ret;
498 
499 	if (chip->port_type == TYPEC_PORT_SNK)
500 		goto skip_src;
501 
502 	/* Change the default Type-C Source power operation mode capability */
503 	ret = regmap_update_bits(chip->regmap, STUSB160X_CC_CAPABILITY_CTRL,
504 				 STUSB160X_CC_CURRENT_ADVERTISED,
505 				 FIELD_PREP(STUSB160X_CC_CURRENT_ADVERTISED,
506 					    chip->pwr_opmode));
507 	if (ret)
508 		return ret;
509 
510 	/* Manage Type-C Source Vconn supply */
511 	if (stusb160x_get_vconn(chip)) {
512 		ret = stusb160x_set_vconn(chip, true);
513 		if (ret)
514 			return ret;
515 	}
516 
517 skip_src:
518 	/* Mask all events interrupts - to be unmasked with interrupt support */
519 	ret = regmap_update_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
520 				 STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
521 	if (ret)
522 		return ret;
523 
524 	/* Read status at least once to clear any stale interrupts */
525 	regmap_read(chip->regmap, STUSB160X_ALERT_STATUS, &val);
526 	regmap_read(chip->regmap, STUSB160X_CC_CONNECTION_STATUS_TRANS, &val);
527 	regmap_read(chip->regmap, STUSB160X_MONITORING_STATUS_TRANS, &val);
528 	regmap_read(chip->regmap, STUSB160X_HW_FAULT_STATUS_TRANS, &val);
529 
530 	return 0;
531 }
532 
stusb160x_get_fw_caps(struct stusb160x * chip,struct fwnode_handle * fwnode)533 static int stusb160x_get_fw_caps(struct stusb160x *chip,
534 				 struct fwnode_handle *fwnode)
535 {
536 	const char *cap_str;
537 	int ret;
538 
539 	chip->capability.fwnode = fwnode;
540 
541 	/*
542 	 * Supported port type can be configured through device tree
543 	 * else it is read from chip registers in stusb160x_get_caps.
544 	 */
545 	ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
546 	if (!ret) {
547 		ret = typec_find_port_power_role(cap_str);
548 		if (ret < 0)
549 			return ret;
550 		chip->port_type = ret;
551 	}
552 	chip->capability.type = chip->port_type;
553 
554 	/* Skip DRP/Source capabilities in case of Sink only */
555 	if (chip->port_type == TYPEC_PORT_SNK)
556 		return 0;
557 
558 	if (chip->port_type == TYPEC_PORT_DRP)
559 		chip->capability.prefer_role = TYPEC_SINK;
560 
561 	/*
562 	 * Supported power operation mode can be configured through device tree
563 	 * else it is read from chip registers in stusb160x_get_caps.
564 	 */
565 	ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &cap_str);
566 	if (!ret) {
567 		ret = typec_find_pwr_opmode(cap_str);
568 		/* Power delivery not yet supported */
569 		if (ret < 0 || ret == TYPEC_PWR_MODE_PD) {
570 			dev_err(chip->dev, "bad power operation mode: %d\n", ret);
571 			return -EINVAL;
572 		}
573 		chip->pwr_opmode = ret;
574 	}
575 
576 	return 0;
577 }
578 
stusb160x_get_caps(struct stusb160x * chip)579 static int stusb160x_get_caps(struct stusb160x *chip)
580 {
581 	enum typec_port_type *type = &chip->capability.type;
582 	enum typec_port_data *data = &chip->capability.data;
583 	enum typec_accessory *accessory = chip->capability.accessory;
584 	u32 val;
585 	int ret;
586 
587 	chip->capability.revision = USB_TYPEC_REV_1_2;
588 
589 	ret = regmap_read(chip->regmap, STUSB160X_CC_POWER_MODE_CTRL, &val);
590 	if (ret)
591 		return ret;
592 
593 	switch (FIELD_GET(STUSB160X_CC_POWER_MODE, val)) {
594 	case SOURCE_WITH_ACCESSORY:
595 		*type = TYPEC_PORT_SRC;
596 		*data = TYPEC_PORT_DFP;
597 		*accessory++ = TYPEC_ACCESSORY_AUDIO;
598 		*accessory++ = TYPEC_ACCESSORY_DEBUG;
599 		break;
600 	case SINK_WITH_ACCESSORY:
601 		*type = TYPEC_PORT_SNK;
602 		*data = TYPEC_PORT_UFP;
603 		*accessory++ = TYPEC_ACCESSORY_AUDIO;
604 		*accessory++ = TYPEC_ACCESSORY_DEBUG;
605 		break;
606 	case SINK_WITHOUT_ACCESSORY:
607 		*type = TYPEC_PORT_SNK;
608 		*data = TYPEC_PORT_UFP;
609 		break;
610 	case DUAL_WITH_ACCESSORY:
611 	case DUAL_WITH_ACCESSORY_AND_TRY_SRC:
612 	case DUAL_WITH_ACCESSORY_AND_TRY_SNK:
613 		*type = TYPEC_PORT_DRP;
614 		*data = TYPEC_PORT_DRD;
615 		*accessory++ = TYPEC_ACCESSORY_AUDIO;
616 		*accessory++ = TYPEC_ACCESSORY_DEBUG;
617 		break;
618 	default:
619 		return -EINVAL;
620 	}
621 
622 	chip->port_type = *type;
623 	chip->pwr_opmode = stusb160x_get_pwr_opmode(chip);
624 
625 	return 0;
626 }
627 
628 static const struct of_device_id stusb160x_of_match[] = {
629 	{ .compatible = "st,stusb1600", .data = &stusb1600_regmap_config},
630 	{},
631 };
632 MODULE_DEVICE_TABLE(of, stusb160x_of_match);
633 
stusb160x_probe(struct i2c_client * client)634 static int stusb160x_probe(struct i2c_client *client)
635 {
636 	struct stusb160x *chip;
637 	const struct of_device_id *match;
638 	struct regmap_config *regmap_config;
639 	struct fwnode_handle *fwnode;
640 	int ret;
641 
642 	chip = devm_kzalloc(&client->dev, sizeof(struct stusb160x), GFP_KERNEL);
643 	if (!chip)
644 		return -ENOMEM;
645 
646 	i2c_set_clientdata(client, chip);
647 
648 	match = i2c_of_match_device(stusb160x_of_match, client);
649 	regmap_config = (struct regmap_config *)match->data;
650 	chip->regmap = devm_regmap_init_i2c(client, regmap_config);
651 	if (IS_ERR(chip->regmap)) {
652 		ret = PTR_ERR(chip->regmap);
653 		dev_err(&client->dev,
654 			"Failed to allocate register map:%d\n", ret);
655 		return ret;
656 	}
657 
658 	chip->dev = &client->dev;
659 
660 	chip->vsys_supply = devm_regulator_get_optional(chip->dev, "vsys");
661 	if (IS_ERR(chip->vsys_supply)) {
662 		ret = PTR_ERR(chip->vsys_supply);
663 		if (ret != -ENODEV)
664 			return ret;
665 		chip->vsys_supply = NULL;
666 	}
667 
668 	chip->vdd_supply = devm_regulator_get_optional(chip->dev, "vdd");
669 	if (IS_ERR(chip->vdd_supply)) {
670 		ret = PTR_ERR(chip->vdd_supply);
671 		if (ret != -ENODEV)
672 			return ret;
673 		chip->vdd_supply = NULL;
674 	}
675 
676 	chip->vconn_supply = devm_regulator_get_optional(chip->dev, "vconn");
677 	if (IS_ERR(chip->vconn_supply)) {
678 		ret = PTR_ERR(chip->vconn_supply);
679 		if (ret != -ENODEV)
680 			return ret;
681 		chip->vconn_supply = NULL;
682 	}
683 
684 	fwnode = device_get_named_child_node(chip->dev, "connector");
685 	if (!fwnode)
686 		return -ENODEV;
687 
688 	/*
689 	 * This fwnode has a "compatible" property, but is never populated as a
690 	 * struct device. Instead we simply parse it to read the properties.
691 	 * This it breaks fw_devlink=on. To maintain backward compatibility
692 	 * with existing DT files, we work around this by deleting any
693 	 * fwnode_links to/from this fwnode.
694 	 */
695 	fw_devlink_purge_absent_suppliers(fwnode);
696 
697 	/*
698 	 * When both VDD and VSYS power supplies are present, the low power
699 	 * supply VSYS is selected when VSYS voltage is above 3.1 V.
700 	 * Otherwise VDD is selected.
701 	 */
702 	if (chip->vdd_supply &&
703 	    (!chip->vsys_supply ||
704 	     (regulator_get_voltage(chip->vsys_supply) <= 3100000)))
705 		chip->main_supply = chip->vdd_supply;
706 	else
707 		chip->main_supply = chip->vsys_supply;
708 
709 	if (chip->main_supply) {
710 		ret = regulator_enable(chip->main_supply);
711 		if (ret) {
712 			dev_err(chip->dev,
713 				"Failed to enable main supply: %d\n", ret);
714 			goto fwnode_put;
715 		}
716 	}
717 
718 	/* Get configuration from chip */
719 	ret = stusb160x_get_caps(chip);
720 	if (ret) {
721 		dev_err(chip->dev, "Failed to get port caps: %d\n", ret);
722 		goto main_reg_disable;
723 	}
724 
725 	/* Get optional re-configuration from device tree */
726 	ret = stusb160x_get_fw_caps(chip, fwnode);
727 	if (ret) {
728 		dev_err(chip->dev, "Failed to get connector caps: %d\n", ret);
729 		goto main_reg_disable;
730 	}
731 
732 	ret = stusb160x_chip_init(chip);
733 	if (ret) {
734 		dev_err(chip->dev, "Failed to init port: %d\n", ret);
735 		goto main_reg_disable;
736 	}
737 
738 	chip->port = typec_register_port(chip->dev, &chip->capability);
739 	if (IS_ERR(chip->port)) {
740 		ret = PTR_ERR(chip->port);
741 		goto all_reg_disable;
742 	}
743 
744 	/*
745 	 * Default power operation mode initialization: will be updated upon
746 	 * attach/detach interrupt
747 	 */
748 	typec_set_pwr_opmode(chip->port, chip->pwr_opmode);
749 
750 	if (client->irq) {
751 		chip->role_sw = fwnode_usb_role_switch_get(fwnode);
752 		if (IS_ERR(chip->role_sw)) {
753 			ret = dev_err_probe(chip->dev, PTR_ERR(chip->role_sw),
754 					    "Failed to get usb role switch\n");
755 			goto port_unregister;
756 		}
757 
758 		ret = stusb160x_irq_init(chip, client->irq);
759 		if (ret)
760 			goto role_sw_put;
761 	} else {
762 		/*
763 		 * If Source or Dual power role, need to enable VDD supply
764 		 * providing Vbus if present. In case of interrupt support,
765 		 * VDD supply will be dynamically managed upon attach/detach
766 		 * interrupt.
767 		 */
768 		if (chip->port_type != TYPEC_PORT_SNK && chip->vdd_supply) {
769 			ret = regulator_enable(chip->vdd_supply);
770 			if (ret) {
771 				dev_err(chip->dev,
772 					"Failed to enable VDD supply: %d\n",
773 					ret);
774 				goto port_unregister;
775 			}
776 			chip->vbus_on = true;
777 		}
778 	}
779 
780 	fwnode_handle_put(fwnode);
781 
782 	return 0;
783 
784 role_sw_put:
785 	if (chip->role_sw)
786 		usb_role_switch_put(chip->role_sw);
787 port_unregister:
788 	typec_unregister_port(chip->port);
789 all_reg_disable:
790 	if (stusb160x_get_vconn(chip))
791 		stusb160x_set_vconn(chip, false);
792 main_reg_disable:
793 	if (chip->main_supply)
794 		regulator_disable(chip->main_supply);
795 fwnode_put:
796 	fwnode_handle_put(fwnode);
797 
798 	return ret;
799 }
800 
stusb160x_remove(struct i2c_client * client)801 static void stusb160x_remove(struct i2c_client *client)
802 {
803 	struct stusb160x *chip = i2c_get_clientdata(client);
804 
805 	if (chip->partner) {
806 		typec_unregister_partner(chip->partner);
807 		chip->partner = NULL;
808 	}
809 
810 	if (chip->vbus_on)
811 		regulator_disable(chip->vdd_supply);
812 
813 	if (chip->role_sw)
814 		usb_role_switch_put(chip->role_sw);
815 
816 	typec_unregister_port(chip->port);
817 
818 	if (stusb160x_get_vconn(chip))
819 		stusb160x_set_vconn(chip, false);
820 
821 	if (chip->main_supply)
822 		regulator_disable(chip->main_supply);
823 }
824 
stusb160x_suspend(struct device * dev)825 static int __maybe_unused stusb160x_suspend(struct device *dev)
826 {
827 	struct stusb160x *chip = dev_get_drvdata(dev);
828 
829 	/* Mask interrupts */
830 	return regmap_update_bits(chip->regmap,
831 				  STUSB160X_ALERT_STATUS_MASK_CTRL,
832 				  STUSB160X_ALL_ALERTS, STUSB160X_ALL_ALERTS);
833 }
834 
stusb160x_resume(struct device * dev)835 static int __maybe_unused stusb160x_resume(struct device *dev)
836 {
837 	struct stusb160x *chip = dev_get_drvdata(dev);
838 	u32 status;
839 	int ret;
840 
841 	ret = regcache_sync(chip->regmap);
842 	if (ret)
843 		return ret;
844 
845 	/* Check if attach/detach occurred during low power */
846 	ret = regmap_read(chip->regmap,
847 			  STUSB160X_CC_CONNECTION_STATUS, &status);
848 	if (ret)
849 		return ret;
850 
851 	if (chip->partner && !(status & STUSB160X_CC_ATTACH))
852 		stusb160x_detach(chip, status);
853 
854 	if (!chip->partner && (status & STUSB160X_CC_ATTACH)) {
855 		ret = stusb160x_attach(chip, status);
856 		if (ret)
857 			dev_err(chip->dev, "attach failed: %d\n", ret);
858 	}
859 
860 	/* Unmask interrupts */
861 	return regmap_write_bits(chip->regmap, STUSB160X_ALERT_STATUS_MASK_CTRL,
862 				 STUSB160X_CC_CONNECTION, 0);
863 }
864 
865 static SIMPLE_DEV_PM_OPS(stusb160x_pm_ops, stusb160x_suspend, stusb160x_resume);
866 
867 static struct i2c_driver stusb160x_driver = {
868 	.driver = {
869 		.name = "stusb160x",
870 		.pm = &stusb160x_pm_ops,
871 		.of_match_table = stusb160x_of_match,
872 	},
873 	.probe_new = stusb160x_probe,
874 	.remove = stusb160x_remove,
875 };
876 module_i2c_driver(stusb160x_driver);
877 
878 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
879 MODULE_DESCRIPTION("STMicroelectronics STUSB160x Type-C controller driver");
880 MODULE_LICENSE("GPL v2");
881