summaryrefslogtreecommitdiff
path: root/drivers/power/supply/cros_usbpd-charger.c
blob: 688a16bacfbbd509e053358cb6f59b5715f49864 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// SPDX-License-Identifier: GPL-2.0+
/*
 * Power supply driver for ChromeOS EC based USB PD Charger.
 *
 * Copyright (c) 2014 - 2018 Google, Inc
 */

#include <linux/module.h>
#include <linux/mfd/cros_ec.h>
#include <linux/mfd/cros_ec_commands.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/slab.h>

#define CHARGER_DIR_NAME			"CROS_USBPD_CHARGER%d"
#define CHARGER_DIR_NAME_LENGTH			sizeof(CHARGER_DIR_NAME)
#define CHARGER_CACHE_UPDATE_DELAY		msecs_to_jiffies(500)
#define CHARGER_MANUFACTURER_MODEL_LENGTH	32

#define DRV_NAME "cros-usbpd-charger"

struct port_data {
	int port_number;
	char name[CHARGER_DIR_NAME_LENGTH];
	char manufacturer[CHARGER_MANUFACTURER_MODEL_LENGTH];
	char model_name[CHARGER_MANUFACTURER_MODEL_LENGTH];
	struct power_supply *psy;
	struct power_supply_desc psy_desc;
	int psy_usb_type;
	int psy_online;
	int psy_status;
	int psy_current_max;
	int psy_voltage_max_design;
	int psy_voltage_now;
	int psy_power_max;
	struct charger_data *charger;
	unsigned long last_update;
};

struct charger_data {
	struct device *dev;
	struct cros_ec_dev *ec_dev;
	struct cros_ec_device *ec_device;
	int num_charger_ports;
	int num_registered_psy;
	struct port_data *ports[EC_USB_PD_MAX_PORTS];
	struct notifier_block notifier;
};

static enum power_supply_property cros_usbpd_charger_props[] = {
	POWER_SUPPLY_PROP_ONLINE,
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_CURRENT_MAX,
	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_MODEL_NAME,
	POWER_SUPPLY_PROP_MANUFACTURER,
	POWER_SUPPLY_PROP_USB_TYPE
};

static enum power_supply_usb_type cros_usbpd_charger_usb_types[] = {
	POWER_SUPPLY_USB_TYPE_UNKNOWN,
	POWER_SUPPLY_USB_TYPE_SDP,
	POWER_SUPPLY_USB_TYPE_DCP,
	POWER_SUPPLY_USB_TYPE_CDP,
	POWER_SUPPLY_USB_TYPE_C,
	POWER_SUPPLY_USB_TYPE_PD,
	POWER_SUPPLY_USB_TYPE_PD_DRP,
	POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID
};

static int cros_usbpd_charger_ec_command(struct charger_data *charger,
					 unsigned int version,
					 unsigned int command,
					 void *outdata,
					 unsigned int outsize,
					 void *indata,
					 unsigned int insize)
{
	struct cros_ec_dev *ec_dev = charger->ec_dev;
	struct cros_ec_command *msg;
	int ret;

	msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
	if (!msg)
		return -ENOMEM;

	msg->version = version;
	msg->command = ec_dev->cmd_offset + command;
	msg->outsize = outsize;
	msg->insize = insize;

	if (outsize)
		memcpy(msg->data, outdata, outsize);

	ret = cros_ec_cmd_xfer_status(charger->ec_device, msg);
	if (ret >= 0 && insize)
		memcpy(indata, msg->data, insize);

	kfree(msg);
	return ret;
}

static int cros_usbpd_charger_get_num_ports(struct charger_data *charger)
{
	struct ec_response_usb_pd_ports resp;
	int ret;

	ret = cros_usbpd_charger_ec_command(charger, 0, EC_CMD_USB_PD_PORTS,
					    NULL, 0, &resp, sizeof(resp));
	if (ret < 0) {
		dev_err(charger->dev,
			"Unable to get the number or ports (err:0x%x)\n", ret);
		return ret;
	}

	return resp.num_ports;
}

static int cros_usbpd_charger_get_discovery_info(struct port_data *port)
{
	struct charger_data *charger = port->charger;
	struct ec_params_usb_pd_discovery_entry resp;
	struct ec_params_usb_pd_info_request req;
	int ret;

	req.port = port->port_number;

	ret = cros_usbpd_charger_ec_command(charger, 0,
					    EC_CMD_USB_PD_DISCOVERY,
					    &req, sizeof(req),
					    &resp, sizeof(resp));
	if (ret < 0) {
		dev_err(charger->dev,
			"Unable to query discovery info (err:0x%x)\n", ret);
		return ret;
	}

	dev_dbg(charger->dev, "Port %d: VID = 0x%x, PID=0x%x, PTYPE=0x%x\n",
		port->port_number, resp.vid, resp.pid, resp.ptype);

	snprintf(port->manufacturer, sizeof(port->manufacturer), "%x",
		 resp.vid);
	snprintf(port->model_name, sizeof(port->model_name), "%x", resp.pid);

	return 0;
}

static int cros_usbpd_charger_get_power_info(struct port_data *port)
{
	struct charger_data *charger = port->charger;
	struct ec_response_usb_pd_power_info resp;
	struct ec_params_usb_pd_power_info req;
	int last_psy_status, last_psy_usb_type;
	struct device *dev = charger->dev;
	int ret;

	req.port = port->port_number;
	ret = cros_usbpd_charger_ec_command(charger, 0,
					    EC_CMD_USB_PD_POWER_INFO,
					    &req, sizeof(req),
					    &resp, sizeof(resp));
	if (ret < 0) {
		dev_err(dev, "Unable to query PD power info (err:0x%x)\n", ret);
		return ret;
	}

	last_psy_status = port->psy_status;
	last_psy_usb_type = port->psy_usb_type;

	switch (resp.role) {
	case USB_PD_PORT_POWER_DISCONNECTED:
		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
		port->psy_online = 0;
		break;
	case USB_PD_PORT_POWER_SOURCE:
		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
		port->psy_online = 0;
		break;
	case USB_PD_PORT_POWER_SINK:
		port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
		port->psy_online = 1;
		break;
	case USB_PD_PORT_POWER_SINK_NOT_CHARGING:
		port->psy_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
		port->psy_online = 1;
		break;
	default:
		dev_err(dev, "Unknown role %d\n", resp.role);
		break;
	}

	port->psy_voltage_max_design = resp.meas.voltage_max;
	port->psy_voltage_now = resp.meas.voltage_now;
	port->psy_current_max = resp.meas.current_max;
	port->psy_power_max = resp.max_power;

	switch (resp.type) {
	case USB_CHG_TYPE_BC12_SDP:
	case USB_CHG_TYPE_VBUS:
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
		break;
	case USB_CHG_TYPE_NONE:
		/*
		 * For dual-role devices when we are a source, the firmware
		 * reports the type as NONE. Report such chargers as type
		 * USB_PD_DRP.
		 */
		if (resp.role == USB_PD_PORT_POWER_SOURCE && resp.dualrole)
			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
		else
			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
		break;
	case USB_CHG_TYPE_OTHER:
	case USB_CHG_TYPE_PROPRIETARY:
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID;
		break;
	case USB_CHG_TYPE_C:
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_C;
		break;
	case USB_CHG_TYPE_BC12_DCP:
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP;
		break;
	case USB_CHG_TYPE_BC12_CDP:
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP;
		break;
	case USB_CHG_TYPE_PD:
		if (resp.dualrole)
			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD_DRP;
		else
			port->psy_usb_type = POWER_SUPPLY_USB_TYPE_PD;
		break;
	case USB_CHG_TYPE_UNKNOWN:
		/*
		 * While the EC is trying to determine the type of charger that
		 * has been plugged in, it will report the charger type as
		 * unknown. Additionally since the power capabilities are
		 * unknown, report the max current and voltage as zero.
		 */
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
		port->psy_voltage_max_design = 0;
		port->psy_current_max = 0;
		break;
	default:
		dev_err(dev, "Port %d: default case!\n", port->port_number);
		port->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP;
	}

	port->psy_desc.type = POWER_SUPPLY_TYPE_USB;

	dev_dbg(dev,
		"Port %d: type=%d vmax=%d vnow=%d cmax=%d clim=%d pmax=%d\n",
		port->port_number, resp.type, resp.meas.voltage_max,
		resp.meas.voltage_now, resp.meas.current_max,
		resp.meas.current_lim, resp.max_power);

	/*
	 * If power supply type or status changed, explicitly call
	 * power_supply_changed. This results in udev event getting generated
	 * and allows user mode apps to react quicker instead of waiting for
	 * their next poll of power supply status.
	 */
	if (last_psy_usb_type != port->psy_usb_type ||
	    last_psy_status != port->psy_status)
		power_supply_changed(port->psy);

	return 0;
}

static int cros_usbpd_charger_get_port_status(struct port_data *port,
					      bool ratelimit)
{
	int ret;

	if (ratelimit &&
	    time_is_after_jiffies(port->last_update +
				  CHARGER_CACHE_UPDATE_DELAY))
		return 0;

	ret = cros_usbpd_charger_get_power_info(port);
	if (ret < 0)
		return ret;

	ret = cros_usbpd_charger_get_discovery_info(port);
	port->last_update = jiffies;

	return ret;
}

static void cros_usbpd_charger_power_changed(struct power_supply *psy)
{
	struct port_data *port = power_supply_get_drvdata(psy);
	struct charger_data *charger = port->charger;
	int i;

	for (i = 0; i < charger->num_registered_psy; i++)
		cros_usbpd_charger_get_port_status(charger->ports[i], false);
}

static int cros_usbpd_charger_get_prop(struct power_supply *psy,
				       enum power_supply_property psp,
				       union power_supply_propval *val)
{
	struct port_data *port = power_supply_get_drvdata(psy);
	struct charger_data *charger = port->charger;
	struct cros_ec_device *ec_device = charger->ec_device;
	struct device *dev = charger->dev;
	int ret;

	/* Only refresh ec_port_status for dynamic properties */
	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		/*
		 * If mkbp_event_supported, then we can be assured that
		 * the driver's state for the online property is consistent
		 * with the hardware. However, if we aren't event driven,
		 * the optimization before to skip an ec_port_status get
		 * and only returned cached values of the online property will
		 * cause a delay in detecting a cable attach until one of the
		 * other properties are read.
		 *
		 * Allow an ec_port_status refresh for online property check
		 * if we're not already online to check for plug events if
		 * not mkbp_event_supported.
		 */
		if (ec_device->mkbp_event_supported || port->psy_online)
			break;
		/* fall through */
	case POWER_SUPPLY_PROP_CURRENT_MAX:
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		ret = cros_usbpd_charger_get_port_status(port, true);
		if (ret < 0) {
			dev_err(dev, "Failed to get port status (err:0x%x)\n",
				ret);
			return -EINVAL;
		}
		break;
	default:
		break;
	}

	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = port->psy_online;
		break;
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = port->psy_status;
		break;
	case POWER_SUPPLY_PROP_CURRENT_MAX:
		val->intval = port->psy_current_max * 1000;
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
		val->intval = port->psy_voltage_max_design * 1000;
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		val->intval = port->psy_voltage_now * 1000;
		break;
	case POWER_SUPPLY_PROP_USB_TYPE:
		val->intval = port->psy_usb_type;
		break;
	case POWER_SUPPLY_PROP_MODEL_NAME:
		val->strval = port->model_name;
		break;
	case POWER_SUPPLY_PROP_MANUFACTURER:
		val->strval = port->manufacturer;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int cros_usbpd_charger_ec_event(struct notifier_block *nb,
				       unsigned long queued_during_suspend,
				       void *_notify)
{
	struct cros_ec_device *ec_device;
	struct charger_data *charger;
	struct device *dev;
	u32 host_event;

	charger = container_of(nb, struct charger_data, notifier);
	ec_device = charger->ec_device;
	dev = charger->dev;

	host_event = cros_ec_get_host_event(ec_device);
	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) {
		cros_usbpd_charger_power_changed(charger->ports[0]->psy);
		return NOTIFY_OK;
	} else {
		return NOTIFY_DONE;
	}
}

static void cros_usbpd_charger_unregister_notifier(void *data)
{
	struct charger_data *charger = data;
	struct cros_ec_device *ec_device = charger->ec_device;

	blocking_notifier_chain_unregister(&ec_device->event_notifier,
					   &charger->notifier);
}

static int cros_usbpd_charger_probe(struct platform_device *pd)
{
	struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent);
	struct cros_ec_device *ec_device = ec_dev->ec_dev;
	struct power_supply_desc *psy_desc;
	struct device *dev = &pd->dev;
	struct charger_data *charger;
	struct power_supply *psy;
	struct port_data *port;
	int ret = -EINVAL;
	int i;

	charger = devm_kzalloc(dev, sizeof(struct charger_data),
			       GFP_KERNEL);
	if (!charger)
		return -ENOMEM;

	charger->dev = dev;
	charger->ec_dev = ec_dev;
	charger->ec_device = ec_device;

	platform_set_drvdata(pd, charger);

	charger->num_charger_ports = cros_usbpd_charger_get_num_ports(charger);
	if (charger->num_charger_ports <= 0) {
		/*
		 * This can happen on a system that doesn't support USB PD.
		 * Log a message, but no need to warn.
		 */
		dev_info(dev, "No charging ports found\n");
		ret = -ENODEV;
		goto fail_nowarn;
	}

	for (i = 0; i < charger->num_charger_ports; i++) {
		struct power_supply_config psy_cfg = {};

		port = devm_kzalloc(dev, sizeof(struct port_data), GFP_KERNEL);
		if (!port) {
			ret = -ENOMEM;
			goto fail;
		}

		port->charger = charger;
		port->port_number = i;
		sprintf(port->name, CHARGER_DIR_NAME, i);

		psy_desc = &port->psy_desc;
		psy_desc->name = port->name;
		psy_desc->type = POWER_SUPPLY_TYPE_USB;
		psy_desc->get_property = cros_usbpd_charger_get_prop;
		psy_desc->external_power_changed =
					cros_usbpd_charger_power_changed;
		psy_desc->properties = cros_usbpd_charger_props;
		psy_desc->num_properties =
					ARRAY_SIZE(cros_usbpd_charger_props);
		psy_desc->usb_types = cros_usbpd_charger_usb_types;
		psy_desc->num_usb_types =
				ARRAY_SIZE(cros_usbpd_charger_usb_types);
		psy_cfg.drv_data = port;

		psy = devm_power_supply_register_no_ws(dev, psy_desc,
						       &psy_cfg);
		if (IS_ERR(psy)) {
			dev_err(dev, "Failed to register power supply\n");
			continue;
		}
		port->psy = psy;

		charger->ports[charger->num_registered_psy++] = port;
	}

	if (!charger->num_registered_psy) {
		ret = -ENODEV;
		dev_err(dev, "No power supplies registered\n");
		goto fail;
	}

	if (ec_device->mkbp_event_supported) {
		/* Get PD events from the EC */
		charger->notifier.notifier_call = cros_usbpd_charger_ec_event;
		ret = blocking_notifier_chain_register(
						&ec_device->event_notifier,
						&charger->notifier);
		if (ret < 0) {
			dev_warn(dev, "failed to register notifier\n");
		} else {
			ret = devm_add_action_or_reset(dev,
					cros_usbpd_charger_unregister_notifier,
					charger);
			if (ret < 0)
				goto fail;
		}
	}

	return 0;

fail:
	WARN(1, "%s: Failing probe (err:0x%x)\n", dev_name(dev), ret);

fail_nowarn:
	dev_info(dev, "Failing probe (err:0x%x)\n", ret);
	return ret;
}

#ifdef CONFIG_PM_SLEEP
static int cros_usbpd_charger_resume(struct device *dev)
{
	struct charger_data *charger = dev_get_drvdata(dev);
	int i;

	if (!charger)
		return 0;

	for (i = 0; i < charger->num_registered_psy; i++) {
		power_supply_changed(charger->ports[i]->psy);
		charger->ports[i]->last_update =
				jiffies - CHARGER_CACHE_UPDATE_DELAY;
	}

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(cros_usbpd_charger_pm_ops, NULL,
			 cros_usbpd_charger_resume);

static struct platform_driver cros_usbpd_charger_driver = {
	.driver = {
		.name = DRV_NAME,
		.pm = &cros_usbpd_charger_pm_ops,
	},
	.probe = cros_usbpd_charger_probe
};

module_platform_driver(cros_usbpd_charger_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ChromeOS EC USBPD charger");
MODULE_ALIAS("platform:" DRV_NAME);