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