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