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