1 // SPDX-License-Identifier: GPL-2.0
3 * Power supply driver for ChromeOS EC based Peripheral Device Charger.
5 * Copyright 2020 Google LLC.
8 #include <linux/module.h>
9 #include <linux/notifier.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 #include <linux/stringify.h>
16 #include <linux/types.h>
17 #include <asm/unaligned.h>
19 #define DRV_NAME "cros-ec-pchg"
20 #define PCHG_DIR_PREFIX "peripheral"
21 #define PCHG_DIR_NAME PCHG_DIR_PREFIX "%d"
22 #define PCHG_DIR_NAME_LENGTH \
23 sizeof(PCHG_DIR_PREFIX __stringify(EC_PCHG_MAX_PORTS))
24 #define PCHG_CACHE_UPDATE_DELAY msecs_to_jiffies(500)
28 char name[PCHG_DIR_NAME_LENGTH];
29 struct power_supply *psy;
30 struct power_supply_desc psy_desc;
32 int battery_percentage;
34 struct charger_data *charger;
35 unsigned long last_update;
40 struct cros_ec_dev *ec_dev;
41 struct cros_ec_device *ec_device;
42 int num_registered_psy;
43 struct port_data *ports[EC_PCHG_MAX_PORTS];
44 struct notifier_block notifier;
47 static enum power_supply_property cros_pchg_props[] = {
48 POWER_SUPPLY_PROP_STATUS,
49 POWER_SUPPLY_PROP_CHARGE_TYPE,
50 POWER_SUPPLY_PROP_CAPACITY,
51 POWER_SUPPLY_PROP_SCOPE,
54 static int cros_pchg_ec_command(const struct charger_data *charger,
62 struct cros_ec_dev *ec_dev = charger->ec_dev;
63 struct cros_ec_command *msg;
66 msg = kzalloc(struct_size(msg, data, max(outsize, insize)), GFP_KERNEL);
70 msg->version = version;
71 msg->command = ec_dev->cmd_offset + command;
72 msg->outsize = outsize;
76 memcpy(msg->data, outdata, outsize);
78 ret = cros_ec_cmd_xfer_status(charger->ec_device, msg);
79 if (ret >= 0 && insize)
80 memcpy(indata, msg->data, insize);
86 static const unsigned int pchg_cmd_version = 1;
88 static bool cros_pchg_cmd_ver_check(const struct charger_data *charger)
90 struct ec_params_get_cmd_versions_v1 req;
91 struct ec_response_get_cmd_versions rsp;
94 req.cmd = EC_CMD_PCHG;
95 ret = cros_pchg_ec_command(charger, 1, EC_CMD_GET_CMD_VERSIONS,
96 &req, sizeof(req), &rsp, sizeof(rsp));
98 dev_warn(charger->dev,
99 "Unable to get versions of EC_CMD_PCHG (err:%d)\n",
104 return !!(rsp.version_mask & BIT(pchg_cmd_version));
107 static int cros_pchg_port_count(const struct charger_data *charger)
109 struct ec_response_pchg_count rsp;
112 ret = cros_pchg_ec_command(charger, 0, EC_CMD_PCHG_COUNT,
113 NULL, 0, &rsp, sizeof(rsp));
115 dev_warn(charger->dev,
116 "Unable to get number or ports (err:%d)\n", ret);
120 return rsp.port_count;
123 static int cros_pchg_get_status(struct port_data *port)
125 struct charger_data *charger = port->charger;
126 struct ec_params_pchg req;
127 struct ec_response_pchg rsp;
128 struct device *dev = charger->dev;
129 int old_status = port->psy_status;
130 int old_percentage = port->battery_percentage;
133 req.port = port->port_number;
134 ret = cros_pchg_ec_command(charger, pchg_cmd_version, EC_CMD_PCHG,
135 &req, sizeof(req), &rsp, sizeof(rsp));
137 dev_err(dev, "Unable to get port.%d status (err:%d)\n",
138 port->port_number, ret);
143 case PCHG_STATE_RESET:
144 case PCHG_STATE_INITIALIZED:
145 case PCHG_STATE_ENABLED:
147 port->psy_status = POWER_SUPPLY_STATUS_UNKNOWN;
148 port->charge_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
150 case PCHG_STATE_DETECTED:
151 port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
152 port->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
154 case PCHG_STATE_CHARGING:
155 port->psy_status = POWER_SUPPLY_STATUS_CHARGING;
156 port->charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
158 case PCHG_STATE_FULL:
159 port->psy_status = POWER_SUPPLY_STATUS_FULL;
160 port->charge_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
164 port->battery_percentage = rsp.battery_percentage;
166 if (port->psy_status != old_status ||
167 port->battery_percentage != old_percentage)
168 power_supply_changed(port->psy);
171 "Port %d: state=%d battery=%d%%\n",
172 port->port_number, rsp.state, rsp.battery_percentage);
177 static int cros_pchg_get_port_status(struct port_data *port, bool ratelimit)
182 time_is_after_jiffies(port->last_update + PCHG_CACHE_UPDATE_DELAY))
185 ret = cros_pchg_get_status(port);
189 port->last_update = jiffies;
194 static int cros_pchg_get_prop(struct power_supply *psy,
195 enum power_supply_property psp,
196 union power_supply_propval *val)
198 struct port_data *port = power_supply_get_drvdata(psy);
201 case POWER_SUPPLY_PROP_STATUS:
202 case POWER_SUPPLY_PROP_CAPACITY:
203 case POWER_SUPPLY_PROP_CHARGE_TYPE:
204 cros_pchg_get_port_status(port, true);
211 case POWER_SUPPLY_PROP_STATUS:
212 val->intval = port->psy_status;
214 case POWER_SUPPLY_PROP_CAPACITY:
215 val->intval = port->battery_percentage;
217 case POWER_SUPPLY_PROP_CHARGE_TYPE:
218 val->intval = port->charge_type;
220 case POWER_SUPPLY_PROP_SCOPE:
221 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
230 static int cros_pchg_event(const struct charger_data *charger,
231 unsigned long host_event)
235 for (i = 0; i < charger->num_registered_psy; i++)
236 cros_pchg_get_port_status(charger->ports[i], false);
241 static int cros_ec_notify(struct notifier_block *nb,
242 unsigned long queued_during_suspend,
245 struct cros_ec_device *ec_dev = data;
246 struct charger_data *charger =
247 container_of(nb, struct charger_data, notifier);
250 if (ec_dev->event_data.event_type != EC_MKBP_EVENT_PCHG ||
251 ec_dev->event_size != sizeof(host_event))
254 host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
256 if (!(host_event & EC_MKBP_PCHG_DEVICE_EVENT))
259 return cros_pchg_event(charger, host_event);
262 static int cros_pchg_probe(struct platform_device *pdev)
264 struct device *dev = &pdev->dev;
265 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
266 struct cros_ec_device *ec_device = ec_dev->ec_dev;
267 struct power_supply_desc *psy_desc;
268 struct charger_data *charger;
269 struct power_supply *psy;
270 struct port_data *port;
271 struct notifier_block *nb;
276 charger = devm_kzalloc(dev, sizeof(*charger), GFP_KERNEL);
281 charger->ec_dev = ec_dev;
282 charger->ec_device = ec_device;
284 ret = cros_pchg_port_count(charger);
287 * This feature is enabled by the EC and the kernel driver is
288 * included by default for CrOS devices. Don't need to be loud
289 * since this error can be normal.
291 dev_info(dev, "No peripheral charge ports (err:%d)\n", ret);
295 if (!cros_pchg_cmd_ver_check(charger)) {
296 dev_err(dev, "EC_CMD_PCHG version %d isn't available.\n",
302 if (num_ports > EC_PCHG_MAX_PORTS) {
303 dev_err(dev, "Too many peripheral charge ports (%d)\n",
308 dev_info(dev, "%d peripheral charge ports found\n", num_ports);
310 for (i = 0; i < num_ports; i++) {
311 struct power_supply_config psy_cfg = {};
313 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
317 port->charger = charger;
318 port->port_number = i;
319 snprintf(port->name, sizeof(port->name), PCHG_DIR_NAME, i);
321 psy_desc = &port->psy_desc;
322 psy_desc->name = port->name;
323 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
324 psy_desc->get_property = cros_pchg_get_prop;
325 psy_desc->external_power_changed = NULL;
326 psy_desc->properties = cros_pchg_props;
327 psy_desc->num_properties = ARRAY_SIZE(cros_pchg_props);
328 psy_cfg.drv_data = port;
330 psy = devm_power_supply_register(dev, psy_desc, &psy_cfg);
332 return dev_err_probe(dev, PTR_ERR(psy),
333 "Failed to register power supply\n");
336 charger->ports[charger->num_registered_psy++] = port;
339 if (!charger->num_registered_psy)
342 nb = &charger->notifier;
343 nb->notifier_call = cros_ec_notify;
344 ret = blocking_notifier_chain_register(&ec_dev->ec_dev->event_notifier,
347 dev_err(dev, "Failed to register notifier (err:%d)\n", ret);
352 static struct platform_driver cros_pchg_driver = {
356 .probe = cros_pchg_probe
359 module_platform_driver(cros_pchg_driver);
361 MODULE_LICENSE("GPL");
362 MODULE_DESCRIPTION("ChromeOS EC peripheral device charger");
363 MODULE_ALIAS("platform:" DRV_NAME);