xref: /linux/drivers/power/supply/cros_usbpd-charger.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Power supply driver for ChromeOS EC based USB PD Charger.
4  *
5  * Copyright (c) 2014 - 2018 Google, Inc
6  */
7 
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 #include <linux/platform_data/cros_usbpd_notify.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/slab.h>
16 
17 #define CHARGER_USBPD_DIR_NAME			"CROS_USBPD_CHARGER%d"
18 #define CHARGER_DEDICATED_DIR_NAME		"CROS_DEDICATED_CHARGER"
19 #define CHARGER_DIR_NAME_LENGTH		(sizeof(CHARGER_USBPD_DIR_NAME) >= \
20 					 sizeof(CHARGER_DEDICATED_DIR_NAME) ? \
21 					 sizeof(CHARGER_USBPD_DIR_NAME) : \
22 					 sizeof(CHARGER_DEDICATED_DIR_NAME))
23 #define CHARGER_CACHE_UPDATE_DELAY		msecs_to_jiffies(500)
24 #define CHARGER_MANUFACTURER_MODEL_LENGTH	32
25 
26 #define DRV_NAME "cros-usbpd-charger"
27 
28 struct port_data {
29 	int port_number;
30 	char name[CHARGER_DIR_NAME_LENGTH];
31 	char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH];
32 	char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH];
33 	struct power_supply *psy;
34 	struct power_supply_desc psy_desc;
35 	int psy_usb_type;
36 	int psy_online;
37 	int psy_status;
38 	int psy_current_max;
39 	int psy_voltage_max_design;
40 	int psy_voltage_now;
41 	int psy_power_max;
42 	struct charger_data *charger;
43 	unsigned long last_update;
44 };
45 
46 struct charger_data {
47 	struct device *dev;
48 	struct cros_ec_dev *ec_dev;
49 	struct cros_ec_device *ec_device;
50 	int num_charger_ports;
51 	int num_usbpd_ports;
52 	int num_registered_psy;
53 	struct port_data *ports[EC_USB_PD_MAX_PORTS];
54 	struct notifier_block notifier;
55 };
56 
57 static enum power_supply_property cros_usbpd_charger_props[] = {
58 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
59 	POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
60 	POWER_SUPPLY_PROP_ONLINE,
61 	POWER_SUPPLY_PROP_STATUS,
62 	POWER_SUPPLY_PROP_CURRENT_MAX,
63 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
64 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
65 	POWER_SUPPLY_PROP_MODEL_NAME,
66 	POWER_SUPPLY_PROP_MANUFACTURER,
67 	POWER_SUPPLY_PROP_USB_TYPE
68 };
69 
70 static enum power_supply_property cros_usbpd_dedicated_charger_props[] = {
71 	POWER_SUPPLY_PROP_ONLINE,
72 	POWER_SUPPLY_PROP_STATUS,
73 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
74 };
75 
76 static enum power_supply_usb_type cros_usbpd_charger_usb_types[] = {
77 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
78 	POWER_SUPPLY_USB_TYPE_SDP,
79 	POWER_SUPPLY_USB_TYPE_DCP,
80 	POWER_SUPPLY_USB_TYPE_CDP,
81 	POWER_SUPPLY_USB_TYPE_C,
82 	POWER_SUPPLY_USB_TYPE_PD,
83 	POWER_SUPPLY_USB_TYPE_PD_DRP,
84 	POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
85 };
86 
87 /* Input voltage/current limit in mV/mA. Default to none. */
88 static u16 input_voltage_limit = EC_POWER_LIMIT_NONE;
89 static u16 input_current_limit = EC_POWER_LIMIT_NONE;
90 
91 static bool cros_usbpd_charger_port_is_dedicated(struct port_data *port)
92 {
93 	return port->port_number >= port->charger->num_usbpd_ports;
94 }
95 
96 static int cros_usbpd_charger_ec_command(struct charger_data *charger,
97 					 unsigned int version,
98 					 unsigned int command,
99 					 void *outdata,
100 					 unsigned int outsize,
101 					 void *indata,
102 					 unsigned int insize)
103 {
104 	struct cros_ec_dev *ec_dev = charger->ec_dev;
105 	struct cros_ec_command *msg;
106 	int ret;
107 
108 	msg = kzalloc(struct_size(msg, data, max(outsize, insize)), GFP_KERNEL);
109 	if (!msg)
110 		return -ENOMEM;
111 
112 	msg->version = version;
113 	msg->command = ec_dev->cmd_offset + command;
114 	msg->outsize = outsize;
115 	msg->insize = insize;
116 
117 	if (outsize)
118 		memcpy(msg->data, outdata, outsize);
119 
120 	ret = cros_ec_cmd_xfer_status(charger->ec_device, msg);
121 	if (ret >= 0 && insize)
122 		memcpy(indata, msg->data, insize);
123 
124 	kfree(msg);
125 	return ret;
126 }
127 
128 static int cros_usbpd_charger_get_num_ports(struct charger_data *charger)
129 {
130 	struct ec_response_charge_port_count resp;
131 	int ret;
132 
133 	ret = cros_usbpd_charger_ec_command(charger, 0,
134 					    EC_CMD_CHARGE_PORT_COUNT,
135 					    NULL, 0, &resp, sizeof(resp));
136 	if (ret < 0)
137 		return ret;
138 
139 	return resp.port_count;
140 }
141 
142 static int cros_usbpd_charger_get_usbpd_num_ports(struct charger_data *charger)
143 {
144 	struct ec_response_usb_pd_ports resp;
145 	int ret;
146 
147 	ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS,
148 					    NULL, 0, &resp, sizeof(resp));
149 	if (ret < 0)
150 		return ret;
151 
152 	return resp.num_ports;
153 }
154 
155 static int cros_usbpd_charger_get_discovery_info(struct port_data *port)
156 {
157 	struct charger_data *charger = port->charger;
158 	struct ec_params_usb_pd_discovery_entry resp;
159 	struct ec_params_usb_pd_info_request req;
160 	int ret;
161 
162 	req.port = port->port_number;
163 
164 	ret = cros_usbpd_charger_ec_command(charger, 0,
165 					    EC_CMD_USB_PD_DISCOVERY,
166 					    &req, sizeof(req),
167 					    &resp, sizeof(resp));
168 	if (ret < 0) {
169 		dev_err(charger->dev,
170 			"Unable to query discovery info (err:0x%x)\n", ret);
171 		return ret;
172 	}
173 
174 	dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
175 		port->port_number, resp.vid, resp.pid, resp.ptype);
176 
177 	snprintf(port->manufacturer, sizeof(port->manufacturer), "%x",
178 		 resp.vid);
179 	snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid);
180 
181 	return 0;
182 }
183 
184 static int cros_usbpd_charger_get_power_info(struct port_data *port)
185 {
186 	struct charger_data *charger = port->charger;
187 	struct ec_response_usb_pd_power_info resp;
188 	struct ec_params_usb_pd_power_info req;
189 	int last_psy_status, last_psy_usb_type;
190 	struct device *dev = charger->dev;
191 	int ret;
192 
193 	req.port = port->port_number;
194 	ret = cros_usbpd_charger_ec_command(charger, 0,
195 					    EC_CMD_USB_PD_POWER_INFO,
196 					    &req, sizeof(req),
197 					    &resp, sizeof(resp));
198 	if (ret < 0) {
199 		dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret);
200 		return ret;
201 	}
202 
203 	last_psy_status = port->psy_status;
204 	last_psy_usb_type = port->psy_usb_type;
205 
206 	switch (resp.role) {
207 	case USB_PD_PORT_POWER_DISCONNECTED:
208 		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
209 		port->psy_online = 0;
210 		break;
211 	case USB_PD_PORT_POWER_SOURCE:
212 		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
213 		port->psy_online = 0;
214 		break;
215 	case USB_PD_PORT_POWER_SINK:
216 		port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
217 		port->psy_online = 1;
218 		break;
219 	case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
220 		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
221 		port->psy_online = 1;
222 		break;
223 	default:
224 		dev_err(dev, "Unknown role %d\n", resp.role);
225 		break;
226 	}
227 
228 	port->psy_voltage_max_design = resp.meas.voltage_max;
229 	port->psy_voltage_now = resp.meas.voltage_now;
230 	port->psy_current_max = resp.meas.current_max;
231 	port->psy_power_max = resp.max_power;
232 
233 	switch (resp.type) {
234 	case USB_CHG_TYPE_BC12_SDP:
235 	case USB_CHG_TYPE_VBUS:
236 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
237 		break;
238 	case USB_CHG_TYPE_NONE:
239 		/*
240 		 * For dual-role devices when we are a source, the firmware
241 		 * reports the type as NONE. Report such chargers as type
242 		 * USB_PD_DRP.
243 		 */
244 		if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole)
245 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
246 		else
247 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
248 		break;
249 	case USB_CHG_TYPE_OTHER:
250 	case USB_CHG_TYPE_PROPRIETARY:
251 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
252 		break;
253 	case USB_CHG_TYPE_C:
254 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C;
255 		break;
256 	case USB_CHG_TYPE_BC12_DCP:
257 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
258 		break;
259 	case USB_CHG_TYPE_BC12_CDP:
260 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP;
261 		break;
262 	case USB_CHG_TYPE_PD:
263 		if (resp.dualrole)
264 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
265 		else
266 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD;
267 		break;
268 	case USB_CHG_TYPE_UNKNOWN:
269 		/*
270 		 * While the EC is trying to determine the type of charger that
271 		 * has been plugged in, it will report the charger type as
272 		 * unknown. Additionally since the power capabilities are
273 		 * unknown, report the max current and voltage as zero.
274 		 */
275 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
276 		port->psy_voltage_max_design = 0;
277 		port->psy_current_max = 0;
278 		break;
279 	default:
280 		dev_dbg(dev, "Port %d: default case!\n", port->port_number);
281 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
282 	}
283 
284 	if (cros_usbpd_charger_port_is_dedicated(port))
285 		port->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
286 	else
287 		port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
288 
289 	dev_dbg(dev,
290 		"Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
291 		port->port_number, resp.type, resp.meas.voltage_max,
292 		resp.meas.voltage_now, resp.meas.current_max,
293 		resp.meas.current_lim, resp.max_power);
294 
295 	/*
296 	 * If power supply type or status changed, explicitly call
297 	 * power_supply_changed. This results in udev event getting generated
298 	 * and allows user mode apps to react quicker instead of waiting for
299 	 * their next poll of power supply status.
300 	 */
301 	if (last_psy_usb_type != port->psy_usb_type ||
302 	    last_psy_status != port->psy_status)
303 		power_supply_changed(port->psy);
304 
305 	return 0;
306 }
307 
308 static int cros_usbpd_charger_get_port_status(struct port_data *port,
309 					      bool ratelimit)
310 {
311 	int ret;
312 
313 	if (ratelimit &&
314 	    time_is_after_jiffies(port->last_update +
315 				  CHARGER_CACHE_UPDATE_DELAY))
316 		return 0;
317 
318 	ret = cros_usbpd_charger_get_power_info(port);
319 	if (ret < 0)
320 		return ret;
321 
322 	if (!cros_usbpd_charger_port_is_dedicated(port))
323 		ret = cros_usbpd_charger_get_discovery_info(port);
324 	port->last_update = jiffies;
325 
326 	return ret;
327 }
328 
329 static int cros_usbpd_charger_set_ext_power_limit(struct charger_data *charger,
330 						  u16 current_lim,
331 						  u16 voltage_lim)
332 {
333 	struct ec_params_external_power_limit_v1 req;
334 	int ret;
335 
336 	req.current_lim = current_lim;
337 	req.voltage_lim = voltage_lim;
338 
339 	ret = cros_usbpd_charger_ec_command(charger, 0,
340 					    EC_CMD_EXTERNAL_POWER_LIMIT,
341 					    &req, sizeof(req), NULL, 0);
342 	if (ret < 0)
343 		dev_err(charger->dev,
344 			"Unable to set the 'External Power Limit': %d\n", ret);
345 
346 	return ret;
347 }
348 
349 static void cros_usbpd_charger_power_changed(struct power_supply *psy)
350 {
351 	struct port_data *port = power_supply_get_drvdata(psy);
352 	struct charger_data *charger = port->charger;
353 	int i;
354 
355 	for (i = 0; i < charger->num_registered_psy; i++)
356 		cros_usbpd_charger_get_port_status(charger->ports[i], false);
357 }
358 
359 static int cros_usbpd_charger_get_prop(struct power_supply *psy,
360 				       enum power_supply_property psp,
361 				       union power_supply_propval *val)
362 {
363 	struct port_data *port = power_supply_get_drvdata(psy);
364 	struct charger_data *charger = port->charger;
365 	struct cros_ec_device *ec_device = charger->ec_device;
366 	struct device *dev = charger->dev;
367 	int ret;
368 
369 	/* Only refresh ec_port_status for dynamic properties */
370 	switch (psp) {
371 	case POWER_SUPPLY_PROP_ONLINE:
372 		/*
373 		 * If mkbp_event_supported, then we can be assured that
374 		 * the driver's state for the online property is consistent
375 		 * with the hardware. However, if we aren't event driven,
376 		 * the optimization before to skip an ec_port_status get
377 		 * and only returned cached values of the online property will
378 		 * cause a delay in detecting a cable attach until one of the
379 		 * other properties are read.
380 		 *
381 		 * Allow an ec_port_status refresh for online property check
382 		 * if we're not already online to check for plug events if
383 		 * not mkbp_event_supported.
384 		 */
385 		if (ec_device->mkbp_event_supported || port->psy_online)
386 			break;
387 		fallthrough;
388 	case POWER_SUPPLY_PROP_CURRENT_MAX:
389 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
390 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
391 		ret = cros_usbpd_charger_get_port_status(port, true);
392 		if (ret < 0) {
393 			dev_err(dev, "Failed to get port status (err:0x%x)\n",
394 				ret);
395 			return -EINVAL;
396 		}
397 		break;
398 	default:
399 		break;
400 	}
401 
402 	switch (psp) {
403 	case POWER_SUPPLY_PROP_ONLINE:
404 		val->intval = port->psy_online;
405 		break;
406 	case POWER_SUPPLY_PROP_STATUS:
407 		val->intval = port->psy_status;
408 		break;
409 	case POWER_SUPPLY_PROP_CURRENT_MAX:
410 		val->intval = port->psy_current_max * 1000;
411 		break;
412 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
413 		val->intval = port->psy_voltage_max_design * 1000;
414 		break;
415 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
416 		val->intval = port->psy_voltage_now * 1000;
417 		break;
418 	case POWER_SUPPLY_PROP_USB_TYPE:
419 		val->intval = port->psy_usb_type;
420 		break;
421 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
422 		if (input_current_limit == EC_POWER_LIMIT_NONE)
423 			val->intval = -1;
424 		else
425 			val->intval = input_current_limit * 1000;
426 		break;
427 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
428 		if (input_voltage_limit == EC_POWER_LIMIT_NONE)
429 			val->intval = -1;
430 		else
431 			val->intval = input_voltage_limit * 1000;
432 		break;
433 	case POWER_SUPPLY_PROP_MODEL_NAME:
434 		val->strval = port->model_name;
435 		break;
436 	case POWER_SUPPLY_PROP_MANUFACTURER:
437 		val->strval = port->manufacturer;
438 		break;
439 	default:
440 		return -EINVAL;
441 	}
442 
443 	return 0;
444 }
445 
446 static int cros_usbpd_charger_set_prop(struct power_supply *psy,
447 				       enum power_supply_property psp,
448 				       const union power_supply_propval *val)
449 {
450 	struct port_data *port = power_supply_get_drvdata(psy);
451 	struct charger_data *charger = port->charger;
452 	struct device *dev = charger->dev;
453 	u16 intval;
454 	int ret;
455 
456 	/* U16_MAX in mV/mA is the maximum supported value */
457 	if (val->intval >= U16_MAX * 1000)
458 		return -EINVAL;
459 	/* A negative number is used to clear the limit */
460 	if (val->intval < 0)
461 		intval = EC_POWER_LIMIT_NONE;
462 	else	/* Convert from uA/uV to mA/mV */
463 		intval = val->intval / 1000;
464 
465 	switch (psp) {
466 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
467 		ret = cros_usbpd_charger_set_ext_power_limit(charger, intval,
468 							input_voltage_limit);
469 		if (ret < 0)
470 			break;
471 
472 		input_current_limit = intval;
473 		if (input_current_limit == EC_POWER_LIMIT_NONE)
474 			dev_info(dev,
475 			  "External Current Limit cleared for all ports\n");
476 		else
477 			dev_info(dev,
478 			  "External Current Limit set to %dmA for all ports\n",
479 			  input_current_limit);
480 		break;
481 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
482 		ret = cros_usbpd_charger_set_ext_power_limit(charger,
483 							input_current_limit,
484 							intval);
485 		if (ret < 0)
486 			break;
487 
488 		input_voltage_limit = intval;
489 		if (input_voltage_limit == EC_POWER_LIMIT_NONE)
490 			dev_info(dev,
491 			  "External Voltage Limit cleared for all ports\n");
492 		else
493 			dev_info(dev,
494 			  "External Voltage Limit set to %dmV for all ports\n",
495 			  input_voltage_limit);
496 		break;
497 	default:
498 		ret = -EINVAL;
499 	}
500 
501 	return ret;
502 }
503 
504 static int cros_usbpd_charger_property_is_writeable(struct power_supply *psy,
505 						enum power_supply_property psp)
506 {
507 	int ret;
508 
509 	switch (psp) {
510 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
511 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
512 		ret = 1;
513 		break;
514 	default:
515 		ret = 0;
516 	}
517 
518 	return ret;
519 }
520 
521 static int cros_usbpd_charger_ec_event(struct notifier_block *nb,
522 				       unsigned long host_event,
523 				       void *_notify)
524 {
525 	struct charger_data *charger = container_of(nb, struct charger_data,
526 						    notifier);
527 
528 	cros_usbpd_charger_power_changed(charger->ports[0]->psy);
529 	return NOTIFY_OK;
530 }
531 
532 static void cros_usbpd_charger_unregister_notifier(void *data)
533 {
534 	struct charger_data *charger = data;
535 
536 	cros_usbpd_unregister_notify(&charger->notifier);
537 }
538 
539 static int cros_usbpd_charger_probe(struct platform_device *pd)
540 {
541 	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
542 	struct cros_ec_device *ec_device = ec_dev->ec_dev;
543 	struct power_supply_desc *psy_desc;
544 	struct device *dev = &pd->dev;
545 	struct charger_data *charger;
546 	struct power_supply *psy;
547 	struct port_data *port;
548 	int ret = -EINVAL;
549 	int i;
550 
551 	charger = devm_kzalloc(dev, sizeof(struct charger_data),
552 			       GFP_KERNEL);
553 	if (!charger)
554 		return -ENOMEM;
555 
556 	charger->dev = dev;
557 	charger->ec_dev = ec_dev;
558 	charger->ec_device = ec_device;
559 
560 	platform_set_drvdata(pd, charger);
561 
562 	/*
563 	 * We need to know the number of USB PD ports in order to know whether
564 	 * there is a dedicated port. The dedicated port will always be
565 	 * after the USB PD ports, and there should be only one.
566 	 */
567 	charger->num_usbpd_ports =
568 		cros_usbpd_charger_get_usbpd_num_ports(charger);
569 	if (charger->num_usbpd_ports <= 0) {
570 		/*
571 		 * This can happen on a system that doesn't support USB PD.
572 		 * Log a message, but no need to warn.
573 		 */
574 		dev_info(dev, "No USB PD charging ports found\n");
575 	}
576 
577 	charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger);
578 	if (charger->num_charger_ports < 0) {
579 		/*
580 		 * This can happen on a system that doesn't support USB PD.
581 		 * Log a message, but no need to warn.
582 		 * Older ECs do not support the above command, in that case
583 		 * let's set up the number of charger ports equal to the number
584 		 * of USB PD ports
585 		 */
586 		dev_info(dev, "Could not get charger port count\n");
587 		charger->num_charger_ports = charger->num_usbpd_ports;
588 	}
589 
590 	if (charger->num_charger_ports <= 0) {
591 		/*
592 		 * This can happen on a system that doesn't support USB PD and
593 		 * doesn't have a dedicated port.
594 		 * Log a message, but no need to warn.
595 		 */
596 		dev_info(dev, "No charging ports found\n");
597 		ret = -ENODEV;
598 		goto fail_nowarn;
599 	}
600 
601 	/*
602 	 * Sanity checks on the number of ports:
603 	 *  there should be at most 1 dedicated port
604 	 */
605 	if (charger->num_charger_ports < charger->num_usbpd_ports ||
606 	    charger->num_charger_ports > (charger->num_usbpd_ports + 1)) {
607 		dev_err(dev, "Unexpected number of charge port count\n");
608 		ret = -EPROTO;
609 		goto fail_nowarn;
610 	}
611 
612 	for (i = 0; i < charger->num_charger_ports; i++) {
613 		struct power_supply_config psy_cfg = {};
614 
615 		port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
616 		if (!port) {
617 			ret = -ENOMEM;
618 			goto fail;
619 		}
620 
621 		port->charger = charger;
622 		port->port_number = i;
623 
624 		psy_desc = &port->psy_desc;
625 		psy_desc->get_property = cros_usbpd_charger_get_prop;
626 		psy_desc->set_property = cros_usbpd_charger_set_prop;
627 		psy_desc->property_is_writeable =
628 				cros_usbpd_charger_property_is_writeable;
629 		psy_desc->external_power_changed =
630 					cros_usbpd_charger_power_changed;
631 		psy_cfg.drv_data = port;
632 
633 		if (cros_usbpd_charger_port_is_dedicated(port)) {
634 			sprintf(port->name, CHARGER_DEDICATED_DIR_NAME);
635 			psy_desc->type = POWER_SUPPLY_TYPE_MAINS;
636 			psy_desc->properties =
637 				cros_usbpd_dedicated_charger_props;
638 			psy_desc->num_properties =
639 				ARRAY_SIZE(cros_usbpd_dedicated_charger_props);
640 		} else {
641 			sprintf(port->name, CHARGER_USBPD_DIR_NAME, i);
642 			psy_desc->type = POWER_SUPPLY_TYPE_USB;
643 			psy_desc->properties = cros_usbpd_charger_props;
644 			psy_desc->num_properties =
645 				ARRAY_SIZE(cros_usbpd_charger_props);
646 			psy_desc->usb_types = cros_usbpd_charger_usb_types;
647 			psy_desc->num_usb_types =
648 				ARRAY_SIZE(cros_usbpd_charger_usb_types);
649 		}
650 
651 		psy_desc->name = port->name;
652 
653 		psy = devm_power_supply_register_no_ws(dev, psy_desc,
654 						       &psy_cfg);
655 		if (IS_ERR(psy)) {
656 			dev_err(dev, "Failed to register power supply\n");
657 			continue;
658 		}
659 		port->psy = psy;
660 
661 		charger->ports[charger->num_registered_psy++] = port;
662 	}
663 
664 	if (!charger->num_registered_psy) {
665 		ret = -ENODEV;
666 		dev_err(dev, "No power supplies registered\n");
667 		goto fail;
668 	}
669 
670 	/* Get PD events from the EC */
671 	charger->notifier.notifier_call = cros_usbpd_charger_ec_event;
672 	ret = cros_usbpd_register_notify(&charger->notifier);
673 	if (ret < 0) {
674 		dev_warn(dev, "failed to register notifier\n");
675 	} else {
676 		ret = devm_add_action_or_reset(dev,
677 				cros_usbpd_charger_unregister_notifier,
678 				charger);
679 		if (ret < 0)
680 			goto fail;
681 	}
682 
683 	return 0;
684 
685 fail:
686 	WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);
687 
688 fail_nowarn:
689 	dev_info(dev, "Failing probe (err:0x%x)\n", ret);
690 	return ret;
691 }
692 
693 #ifdef CONFIG_PM_SLEEP
694 static int cros_usbpd_charger_resume(struct device *dev)
695 {
696 	struct charger_data *charger = dev_get_drvdata(dev);
697 	int i;
698 
699 	if (!charger)
700 		return 0;
701 
702 	for (i = 0; i < charger->num_registered_psy; i++) {
703 		power_supply_changed(charger->ports[i]->psy);
704 		charger->ports[i]->last_update =
705 				jiffies - CHARGER_CACHE_UPDATE_DELAY;
706 	}
707 
708 	return 0;
709 }
710 #endif
711 
712 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL,
713 			 cros_usbpd_charger_resume);
714 
715 static const struct platform_device_id cros_usbpd_charger_id[] = {
716 	{ DRV_NAME, 0 },
717 	{}
718 };
719 MODULE_DEVICE_TABLE(platform, cros_usbpd_charger_id);
720 
721 static struct platform_driver cros_usbpd_charger_driver = {
722 	.driver = {
723 		.name = DRV_NAME,
724 		.pm = &cros_usbpd_charger_pm_ops,
725 	},
726 	.probe = cros_usbpd_charger_probe,
727 	.id_table = cros_usbpd_charger_id,
728 };
729 
730 module_platform_driver(cros_usbpd_charger_driver);
731 
732 MODULE_LICENSE("GPL");
733 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
734