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/mfd/cros_ec.h>
10 #include <linux/mfd/cros_ec_commands.h>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/slab.h>
14 
15 #define CHARGER_DIR_NAME			"CROS_USBPD_CHARGER%d"
16 #define CHARGER_DIR_NAME_LENGTH			sizeof(CHARGER_DIR_NAME)
17 #define CHARGER_CACHE_UPDATE_DELAY		msecs_to_jiffies(500)
18 #define CHARGER_MANUFACTURER_MODEL_LENGTH	32
19 
20 #define DRV_NAME "cros-usbpd-charger"
21 
22 struct port_data {
23 	int port_number;
24 	char name[CHARGER_DIR_NAME_LENGTH];
25 	char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH];
26 	char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH];
27 	struct power_supply *psy;
28 	struct power_supply_desc psy_desc;
29 	int psy_usb_type;
30 	int psy_online;
31 	int psy_status;
32 	int psy_current_max;
33 	int psy_voltage_max_design;
34 	int psy_voltage_now;
35 	int psy_power_max;
36 	struct charger_data *charger;
37 	unsigned long last_update;
38 };
39 
40 struct charger_data {
41 	struct device *dev;
42 	struct cros_ec_dev *ec_dev;
43 	struct cros_ec_device *ec_device;
44 	int num_charger_ports;
45 	int num_registered_psy;
46 	struct port_data *ports[EC_USB_PD_MAX_PORTS];
47 	struct notifier_block notifier;
48 };
49 
50 static enum power_supply_property cros_usbpd_charger_props[] = {
51 	POWER_SUPPLY_PROP_ONLINE,
52 	POWER_SUPPLY_PROP_STATUS,
53 	POWER_SUPPLY_PROP_CURRENT_MAX,
54 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
55 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
56 	POWER_SUPPLY_PROP_MODEL_NAME,
57 	POWER_SUPPLY_PROP_MANUFACTURER,
58 	POWER_SUPPLY_PROP_USB_TYPE
59 };
60 
61 static enum power_supply_usb_type cros_usbpd_charger_usb_types[] = {
62 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
63 	POWER_SUPPLY_USB_TYPE_SDP,
64 	POWER_SUPPLY_USB_TYPE_DCP,
65 	POWER_SUPPLY_USB_TYPE_CDP,
66 	POWER_SUPPLY_USB_TYPE_C,
67 	POWER_SUPPLY_USB_TYPE_PD,
68 	POWER_SUPPLY_USB_TYPE_PD_DRP,
69 	POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
70 };
71 
72 static int cros_usbpd_charger_ec_command(struct charger_data *charger,
73 					 unsigned int version,
74 					 unsigned int command,
75 					 void *outdata,
76 					 unsigned int outsize,
77 					 void *indata,
78 					 unsigned int insize)
79 {
80 	struct cros_ec_dev *ec_dev = charger->ec_dev;
81 	struct cros_ec_command *msg;
82 	int ret;
83 
84 	msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
85 	if (!msg)
86 		return -ENOMEM;
87 
88 	msg->version = version;
89 	msg->command = ec_dev->cmd_offset + command;
90 	msg->outsize = outsize;
91 	msg->insize = insize;
92 
93 	if (outsize)
94 		memcpy(msg->data, outdata, outsize);
95 
96 	ret = cros_ec_cmd_xfer_status(charger->ec_device, msg);
97 	if (ret >= 0 && insize)
98 		memcpy(indata, msg->data, insize);
99 
100 	kfree(msg);
101 	return ret;
102 }
103 
104 static int cros_usbpd_charger_get_num_ports(struct charger_data *charger)
105 {
106 	struct ec_response_usb_pd_ports resp;
107 	int ret;
108 
109 	ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS,
110 					    NULL, 0, &resp, sizeof(resp));
111 	if (ret < 0) {
112 		dev_err(charger->dev,
113 			"Unable to get the number or ports (err:0x%x)\n", ret);
114 		return ret;
115 	}
116 
117 	return resp.num_ports;
118 }
119 
120 static int cros_usbpd_charger_get_discovery_info(struct port_data *port)
121 {
122 	struct charger_data *charger = port->charger;
123 	struct ec_params_usb_pd_discovery_entry resp;
124 	struct ec_params_usb_pd_info_request req;
125 	int ret;
126 
127 	req.port = port->port_number;
128 
129 	ret = cros_usbpd_charger_ec_command(charger, 0,
130 					    EC_CMD_USB_PD_DISCOVERY,
131 					    &req, sizeof(req),
132 					    &resp, sizeof(resp));
133 	if (ret < 0) {
134 		dev_err(charger->dev,
135 			"Unable to query discovery info (err:0x%x)\n", ret);
136 		return ret;
137 	}
138 
139 	dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
140 		port->port_number, resp.vid, resp.pid, resp.ptype);
141 
142 	snprintf(port->manufacturer, sizeof(port->manufacturer), "%x",
143 		 resp.vid);
144 	snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid);
145 
146 	return 0;
147 }
148 
149 static int cros_usbpd_charger_get_power_info(struct port_data *port)
150 {
151 	struct charger_data *charger = port->charger;
152 	struct ec_response_usb_pd_power_info resp;
153 	struct ec_params_usb_pd_power_info req;
154 	int last_psy_status, last_psy_usb_type;
155 	struct device *dev = charger->dev;
156 	int ret;
157 
158 	req.port = port->port_number;
159 	ret = cros_usbpd_charger_ec_command(charger, 0,
160 					    EC_CMD_USB_PD_POWER_INFO,
161 					    &req, sizeof(req),
162 					    &resp, sizeof(resp));
163 	if (ret < 0) {
164 		dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret);
165 		return ret;
166 	}
167 
168 	last_psy_status = port->psy_status;
169 	last_psy_usb_type = port->psy_usb_type;
170 
171 	switch (resp.role) {
172 	case USB_PD_PORT_POWER_DISCONNECTED:
173 		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
174 		port->psy_online = 0;
175 		break;
176 	case USB_PD_PORT_POWER_SOURCE:
177 		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
178 		port->psy_online = 0;
179 		break;
180 	case USB_PD_PORT_POWER_SINK:
181 		port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
182 		port->psy_online = 1;
183 		break;
184 	case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
185 		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
186 		port->psy_online = 1;
187 		break;
188 	default:
189 		dev_err(dev, "Unknown role %d\n", resp.role);
190 		break;
191 	}
192 
193 	port->psy_voltage_max_design = resp.meas.voltage_max;
194 	port->psy_voltage_now = resp.meas.voltage_now;
195 	port->psy_current_max = resp.meas.current_max;
196 	port->psy_power_max = resp.max_power;
197 
198 	switch (resp.type) {
199 	case USB_CHG_TYPE_BC12_SDP:
200 	case USB_CHG_TYPE_VBUS:
201 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
202 		break;
203 	case USB_CHG_TYPE_NONE:
204 		/*
205 		 * For dual-role devices when we are a source, the firmware
206 		 * reports the type as NONE. Report such chargers as type
207 		 * USB_PD_DRP.
208 		 */
209 		if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole)
210 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
211 		else
212 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
213 		break;
214 	case USB_CHG_TYPE_OTHER:
215 	case USB_CHG_TYPE_PROPRIETARY:
216 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
217 		break;
218 	case USB_CHG_TYPE_C:
219 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C;
220 		break;
221 	case USB_CHG_TYPE_BC12_DCP:
222 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
223 		break;
224 	case USB_CHG_TYPE_BC12_CDP:
225 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP;
226 		break;
227 	case USB_CHG_TYPE_PD:
228 		if (resp.dualrole)
229 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
230 		else
231 			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD;
232 		break;
233 	case USB_CHG_TYPE_UNKNOWN:
234 		/*
235 		 * While the EC is trying to determine the type of charger that
236 		 * has been plugged in, it will report the charger type as
237 		 * unknown. Additionally since the power capabilities are
238 		 * unknown, report the max current and voltage as zero.
239 		 */
240 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
241 		port->psy_voltage_max_design = 0;
242 		port->psy_current_max = 0;
243 		break;
244 	default:
245 		dev_err(dev, "Port %d: default case!\n", port->port_number);
246 		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
247 	}
248 
249 	port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
250 
251 	dev_dbg(dev,
252 		"Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
253 		port->port_number, resp.type, resp.meas.voltage_max,
254 		resp.meas.voltage_now, resp.meas.current_max,
255 		resp.meas.current_lim, resp.max_power);
256 
257 	/*
258 	 * If power supply type or status changed, explicitly call
259 	 * power_supply_changed. This results in udev event getting generated
260 	 * and allows user mode apps to react quicker instead of waiting for
261 	 * their next poll of power supply status.
262 	 */
263 	if (last_psy_usb_type != port->psy_usb_type ||
264 	    last_psy_status != port->psy_status)
265 		power_supply_changed(port->psy);
266 
267 	return 0;
268 }
269 
270 static int cros_usbpd_charger_get_port_status(struct port_data *port,
271 					      bool ratelimit)
272 {
273 	int ret;
274 
275 	if (ratelimit &&
276 	    time_is_after_jiffies(port->last_update +
277 				  CHARGER_CACHE_UPDATE_DELAY))
278 		return 0;
279 
280 	ret = cros_usbpd_charger_get_power_info(port);
281 	if (ret < 0)
282 		return ret;
283 
284 	ret = cros_usbpd_charger_get_discovery_info(port);
285 	port->last_update = jiffies;
286 
287 	return ret;
288 }
289 
290 static void cros_usbpd_charger_power_changed(struct power_supply *psy)
291 {
292 	struct port_data *port = power_supply_get_drvdata(psy);
293 	struct charger_data *charger = port->charger;
294 	int i;
295 
296 	for (i = 0; i < charger->num_registered_psy; i++)
297 		cros_usbpd_charger_get_port_status(charger->ports[i], false);
298 }
299 
300 static int cros_usbpd_charger_get_prop(struct power_supply *psy,
301 				       enum power_supply_property psp,
302 				       union power_supply_propval *val)
303 {
304 	struct port_data *port = power_supply_get_drvdata(psy);
305 	struct charger_data *charger = port->charger;
306 	struct cros_ec_device *ec_device = charger->ec_device;
307 	struct device *dev = charger->dev;
308 	int ret;
309 
310 	/* Only refresh ec_port_status for dynamic properties */
311 	switch (psp) {
312 	case POWER_SUPPLY_PROP_ONLINE:
313 		/*
314 		 * If mkbp_event_supported, then we can be assured that
315 		 * the driver's state for the online property is consistent
316 		 * with the hardware. However, if we aren't event driven,
317 		 * the optimization before to skip an ec_port_status get
318 		 * and only returned cached values of the online property will
319 		 * cause a delay in detecting a cable attach until one of the
320 		 * other properties are read.
321 		 *
322 		 * Allow an ec_port_status refresh for online property check
323 		 * if we're not already online to check for plug events if
324 		 * not mkbp_event_supported.
325 		 */
326 		if (ec_device->mkbp_event_supported || port->psy_online)
327 			break;
328 		/* fall through */
329 	case POWER_SUPPLY_PROP_CURRENT_MAX:
330 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
331 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
332 		ret = cros_usbpd_charger_get_port_status(port, true);
333 		if (ret < 0) {
334 			dev_err(dev, "Failed to get port status (err:0x%x)\n",
335 				ret);
336 			return -EINVAL;
337 		}
338 		break;
339 	default:
340 		break;
341 	}
342 
343 	switch (psp) {
344 	case POWER_SUPPLY_PROP_ONLINE:
345 		val->intval = port->psy_online;
346 		break;
347 	case POWER_SUPPLY_PROP_STATUS:
348 		val->intval = port->psy_status;
349 		break;
350 	case POWER_SUPPLY_PROP_CURRENT_MAX:
351 		val->intval = port->psy_current_max * 1000;
352 		break;
353 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
354 		val->intval = port->psy_voltage_max_design * 1000;
355 		break;
356 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
357 		val->intval = port->psy_voltage_now * 1000;
358 		break;
359 	case POWER_SUPPLY_PROP_USB_TYPE:
360 		val->intval = port->psy_usb_type;
361 		break;
362 	case POWER_SUPPLY_PROP_MODEL_NAME:
363 		val->strval = port->model_name;
364 		break;
365 	case POWER_SUPPLY_PROP_MANUFACTURER:
366 		val->strval = port->manufacturer;
367 		break;
368 	default:
369 		return -EINVAL;
370 	}
371 
372 	return 0;
373 }
374 
375 static int cros_usbpd_charger_ec_event(struct notifier_block *nb,
376 				       unsigned long queued_during_suspend,
377 				       void *_notify)
378 {
379 	struct cros_ec_device *ec_device;
380 	struct charger_data *charger;
381 	struct device *dev;
382 	u32 host_event;
383 
384 	charger = container_of(nb, struct charger_data, notifier);
385 	ec_device = charger->ec_device;
386 	dev = charger->dev;
387 
388 	host_event = cros_ec_get_host_event(ec_device);
389 	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) {
390 		cros_usbpd_charger_power_changed(charger->ports[0]->psy);
391 		return NOTIFY_OK;
392 	} else {
393 		return NOTIFY_DONE;
394 	}
395 }
396 
397 static void cros_usbpd_charger_unregister_notifier(void *data)
398 {
399 	struct charger_data *charger = data;
400 	struct cros_ec_device *ec_device = charger->ec_device;
401 
402 	blocking_notifier_chain_unregister(&ec_device->event_notifier,
403 					   &charger->notifier);
404 }
405 
406 static int cros_usbpd_charger_probe(struct platform_device *pd)
407 {
408 	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
409 	struct cros_ec_device *ec_device = ec_dev->ec_dev;
410 	struct power_supply_desc *psy_desc;
411 	struct device *dev = &pd->dev;
412 	struct charger_data *charger;
413 	struct power_supply *psy;
414 	struct port_data *port;
415 	int ret = -EINVAL;
416 	int i;
417 
418 	charger = devm_kzalloc(dev, sizeof(struct charger_data),
419 			       GFP_KERNEL);
420 	if (!charger)
421 		return -ENOMEM;
422 
423 	charger->dev = dev;
424 	charger->ec_dev = ec_dev;
425 	charger->ec_device = ec_device;
426 
427 	platform_set_drvdata(pd, charger);
428 
429 	charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger);
430 	if (charger->num_charger_ports <= 0) {
431 		/*
432 		 * This can happen on a system that doesn't support USB PD.
433 		 * Log a message, but no need to warn.
434 		 */
435 		dev_info(dev, "No charging ports found\n");
436 		ret = -ENODEV;
437 		goto fail_nowarn;
438 	}
439 
440 	for (i = 0; i < charger->num_charger_ports; i++) {
441 		struct power_supply_config psy_cfg = {};
442 
443 		port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
444 		if (!port) {
445 			ret = -ENOMEM;
446 			goto fail;
447 		}
448 
449 		port->charger = charger;
450 		port->port_number = i;
451 		sprintf(port->name, CHARGER_DIR_NAME, i);
452 
453 		psy_desc = &port->psy_desc;
454 		psy_desc->name = port->name;
455 		psy_desc->type = POWER_SUPPLY_TYPE_USB;
456 		psy_desc->get_property = cros_usbpd_charger_get_prop;
457 		psy_desc->external_power_changed =
458 					cros_usbpd_charger_power_changed;
459 		psy_desc->properties = cros_usbpd_charger_props;
460 		psy_desc->num_properties =
461 					ARRAY_SIZE(cros_usbpd_charger_props);
462 		psy_desc->usb_types = cros_usbpd_charger_usb_types;
463 		psy_desc->num_usb_types =
464 				ARRAY_SIZE(cros_usbpd_charger_usb_types);
465 		psy_cfg.drv_data = port;
466 
467 		psy = devm_power_supply_register_no_ws(dev, psy_desc,
468 						       &psy_cfg);
469 		if (IS_ERR(psy)) {
470 			dev_err(dev, "Failed to register power supply\n");
471 			continue;
472 		}
473 		port->psy = psy;
474 
475 		charger->ports[charger->num_registered_psy++] = port;
476 	}
477 
478 	if (!charger->num_registered_psy) {
479 		ret = -ENODEV;
480 		dev_err(dev, "No power supplies registered\n");
481 		goto fail;
482 	}
483 
484 	if (ec_device->mkbp_event_supported) {
485 		/* Get PD events from the EC */
486 		charger->notifier.notifier_call = cros_usbpd_charger_ec_event;
487 		ret = blocking_notifier_chain_register(
488 						&ec_device->event_notifier,
489 						&charger->notifier);
490 		if (ret < 0) {
491 			dev_warn(dev, "failed to register notifier\n");
492 		} else {
493 			ret = devm_add_action_or_reset(dev,
494 					cros_usbpd_charger_unregister_notifier,
495 					charger);
496 			if (ret < 0)
497 				goto fail;
498 		}
499 	}
500 
501 	return 0;
502 
503 fail:
504 	WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);
505 
506 fail_nowarn:
507 	dev_info(dev, "Failing probe (err:0x%x)\n", ret);
508 	return ret;
509 }
510 
511 #ifdef CONFIG_PM_SLEEP
512 static int cros_usbpd_charger_resume(struct device *dev)
513 {
514 	struct charger_data *charger = dev_get_drvdata(dev);
515 	int i;
516 
517 	if (!charger)
518 		return 0;
519 
520 	for (i = 0; i < charger->num_registered_psy; i++) {
521 		power_supply_changed(charger->ports[i]->psy);
522 		charger->ports[i]->last_update =
523 				jiffies - CHARGER_CACHE_UPDATE_DELAY;
524 	}
525 
526 	return 0;
527 }
528 #endif
529 
530 static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL,
531 			 cros_usbpd_charger_resume);
532 
533 static struct platform_driver cros_usbpd_charger_driver = {
534 	.driver = {
535 		.name = DRV_NAME,
536 		.pm = &cros_usbpd_charger_pm_ops,
537 	},
538 	.probe = cros_usbpd_charger_probe
539 };
540 
541 module_platform_driver(cros_usbpd_charger_driver);
542 
543 MODULE_LICENSE("GPL");
544 MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
545 MODULE_ALIAS("platform:" DRV_NAME);
546