1 // SPDX-License-Identifier: GPL-2.0
3 * nvec_power: power supply driver for a NVIDIA compliant embedded controller
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/err.h>
14 #include <linux/power_supply.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/delay.h>
21 #define GET_SYSTEM_STATUS 0x00
24 struct notifier_block notifier;
25 struct delayed_work poller;
26 struct nvec_chip *nvec;
34 int charge_full_design;
36 int critical_capacity;
52 AVERAGING_TIME_INTERVAL,
54 LAST_FULL_CHARGE_CAPACITY,
81 static struct power_supply *nvec_bat_psy;
82 static struct power_supply *nvec_psy;
84 static int nvec_power_notifier(struct notifier_block *nb,
85 unsigned long event_type, void *data)
87 struct nvec_power *power =
88 container_of(nb, struct nvec_power, notifier);
89 struct bat_response *res = data;
91 if (event_type != NVEC_SYS)
94 if (res->sub_type == 0) {
95 if (power->on != res->plu) {
97 power_supply_changed(nvec_psy);
104 static const int bat_init[] = {
105 LAST_FULL_CHARGE_CAPACITY, DESIGN_CAPACITY, CRITICAL_CAPACITY,
106 MANUFACTURER, MODEL, TYPE,
109 static void get_bat_mfg_data(struct nvec_power *power)
112 char buf[] = { NVEC_BAT, SLOT_STATUS };
114 for (i = 0; i < ARRAY_SIZE(bat_init); i++) {
115 buf[1] = bat_init[i];
116 nvec_write_async(power->nvec, buf, 2);
120 static int nvec_power_bat_notifier(struct notifier_block *nb,
121 unsigned long event_type, void *data)
123 struct nvec_power *power =
124 container_of(nb, struct nvec_power, notifier);
125 struct bat_response *res = data;
126 int status_changed = 0;
128 if (event_type != NVEC_BAT)
131 switch (res->sub_type) {
133 if (res->plc[0] & 1) {
134 if (power->bat_present == 0) {
136 get_bat_mfg_data(power);
139 power->bat_present = 1;
141 switch ((res->plc[0] >> 1) & 3) {
144 POWER_SUPPLY_STATUS_NOT_CHARGING;
148 POWER_SUPPLY_STATUS_CHARGING;
152 POWER_SUPPLY_STATUS_DISCHARGING;
155 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
158 if (power->bat_present == 1)
161 power->bat_present = 0;
162 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
164 power->bat_cap = res->plc[1];
166 power_supply_changed(nvec_bat_psy);
169 power->bat_voltage_now = res->plu * 1000;
172 power->time_remain = res->plu * 3600;
175 power->bat_current_now = res->pls * 1000;
177 case AVERAGE_CURRENT:
178 power->bat_current_avg = res->pls * 1000;
180 case CAPACITY_REMAINING:
181 power->capacity_remain = res->plu * 1000;
183 case LAST_FULL_CHARGE_CAPACITY:
184 power->charge_last_full = res->plu * 1000;
186 case DESIGN_CAPACITY:
187 power->charge_full_design = res->plu * 1000;
189 case CRITICAL_CAPACITY:
190 power->critical_capacity = res->plu * 1000;
193 power->bat_temperature = res->plu - 2732;
196 memcpy(power->bat_manu, &res->plc, res->length - 2);
197 power->bat_model[res->length - 2] = '\0';
200 memcpy(power->bat_model, &res->plc, res->length - 2);
201 power->bat_model[res->length - 2] = '\0';
204 memcpy(power->bat_type, &res->plc, res->length - 2);
205 power->bat_type[res->length - 2] = '\0';
207 * This differs a little from the spec fill in more if you find
210 if (!strncmp(power->bat_type, "Li", 30))
211 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_LION;
213 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
222 static int nvec_power_get_property(struct power_supply *psy,
223 enum power_supply_property psp,
224 union power_supply_propval *val)
226 struct nvec_power *power = dev_get_drvdata(psy->dev.parent);
229 case POWER_SUPPLY_PROP_ONLINE:
230 val->intval = power->on;
238 static int nvec_battery_get_property(struct power_supply *psy,
239 enum power_supply_property psp,
240 union power_supply_propval *val)
242 struct nvec_power *power = dev_get_drvdata(psy->dev.parent);
245 case POWER_SUPPLY_PROP_STATUS:
246 val->intval = power->bat_status;
248 case POWER_SUPPLY_PROP_CAPACITY:
249 val->intval = power->bat_cap;
251 case POWER_SUPPLY_PROP_PRESENT:
252 val->intval = power->bat_present;
254 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
255 val->intval = power->bat_voltage_now;
257 case POWER_SUPPLY_PROP_CURRENT_NOW:
258 val->intval = power->bat_current_now;
260 case POWER_SUPPLY_PROP_CURRENT_AVG:
261 val->intval = power->bat_current_avg;
263 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
264 val->intval = power->time_remain;
266 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
267 val->intval = power->charge_full_design;
269 case POWER_SUPPLY_PROP_CHARGE_FULL:
270 val->intval = power->charge_last_full;
272 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
273 val->intval = power->critical_capacity;
275 case POWER_SUPPLY_PROP_CHARGE_NOW:
276 val->intval = power->capacity_remain;
278 case POWER_SUPPLY_PROP_TEMP:
279 val->intval = power->bat_temperature;
281 case POWER_SUPPLY_PROP_MANUFACTURER:
282 val->strval = power->bat_manu;
284 case POWER_SUPPLY_PROP_MODEL_NAME:
285 val->strval = power->bat_model;
287 case POWER_SUPPLY_PROP_TECHNOLOGY:
288 val->intval = power->bat_type_enum;
296 static enum power_supply_property nvec_power_props[] = {
297 POWER_SUPPLY_PROP_ONLINE,
300 static enum power_supply_property nvec_battery_props[] = {
301 POWER_SUPPLY_PROP_STATUS,
302 POWER_SUPPLY_PROP_PRESENT,
303 POWER_SUPPLY_PROP_CAPACITY,
304 POWER_SUPPLY_PROP_VOLTAGE_NOW,
305 POWER_SUPPLY_PROP_CURRENT_NOW,
307 POWER_SUPPLY_PROP_CURRENT_AVG,
308 POWER_SUPPLY_PROP_TEMP,
309 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
311 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
312 POWER_SUPPLY_PROP_CHARGE_FULL,
313 POWER_SUPPLY_PROP_CHARGE_EMPTY,
314 POWER_SUPPLY_PROP_CHARGE_NOW,
315 POWER_SUPPLY_PROP_MANUFACTURER,
316 POWER_SUPPLY_PROP_MODEL_NAME,
317 POWER_SUPPLY_PROP_TECHNOLOGY,
320 static char *nvec_power_supplied_to[] = {
324 static const struct power_supply_desc nvec_bat_psy_desc = {
326 .type = POWER_SUPPLY_TYPE_BATTERY,
327 .properties = nvec_battery_props,
328 .num_properties = ARRAY_SIZE(nvec_battery_props),
329 .get_property = nvec_battery_get_property,
332 static const struct power_supply_desc nvec_psy_desc = {
334 .type = POWER_SUPPLY_TYPE_MAINS,
335 .properties = nvec_power_props,
336 .num_properties = ARRAY_SIZE(nvec_power_props),
337 .get_property = nvec_power_get_property,
341 static int const bat_iter[] = {
342 SLOT_STATUS, VOLTAGE, CURRENT, CAPACITY_REMAINING,
344 AVERAGE_CURRENT, TEMPERATURE, TIME_REMAINING,
348 static void nvec_power_poll(struct work_struct *work)
350 char buf[] = { NVEC_SYS, GET_SYSTEM_STATUS };
351 struct nvec_power *power = container_of(work, struct nvec_power,
354 if (counter >= ARRAY_SIZE(bat_iter))
357 /* AC status via sys req */
358 nvec_write_async(power->nvec, buf, 2);
362 * Select a battery request function via round robin doing it all at
363 * once seems to overload the power supply.
366 buf[1] = bat_iter[counter++];
367 nvec_write_async(power->nvec, buf, 2);
369 schedule_delayed_work(to_delayed_work(work), msecs_to_jiffies(5000));
372 static int nvec_power_probe(struct platform_device *pdev)
374 struct power_supply **psy;
375 const struct power_supply_desc *psy_desc;
376 struct nvec_power *power;
377 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
378 struct power_supply_config psy_cfg = {};
380 power = devm_kzalloc(&pdev->dev, sizeof(struct nvec_power), GFP_NOWAIT);
384 dev_set_drvdata(&pdev->dev, power);
390 psy_desc = &nvec_psy_desc;
391 psy_cfg.supplied_to = nvec_power_supplied_to;
392 psy_cfg.num_supplicants = ARRAY_SIZE(nvec_power_supplied_to);
394 power->notifier.notifier_call = nvec_power_notifier;
396 INIT_DELAYED_WORK(&power->poller, nvec_power_poll);
397 schedule_delayed_work(&power->poller, msecs_to_jiffies(5000));
401 psy_desc = &nvec_bat_psy_desc;
403 power->notifier.notifier_call = nvec_power_bat_notifier;
409 nvec_register_notifier(nvec, &power->notifier, NVEC_SYS);
412 get_bat_mfg_data(power);
414 *psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
416 return PTR_ERR_OR_ZERO(*psy);
419 static int nvec_power_remove(struct platform_device *pdev)
421 struct nvec_power *power = platform_get_drvdata(pdev);
423 cancel_delayed_work_sync(&power->poller);
424 nvec_unregister_notifier(power->nvec, &power->notifier);
427 power_supply_unregister(nvec_psy);
430 power_supply_unregister(nvec_bat_psy);
436 static struct platform_driver nvec_power_driver = {
437 .probe = nvec_power_probe,
438 .remove = nvec_power_remove,
440 .name = "nvec-power",
444 module_platform_driver(nvec_power_driver);
447 MODULE_LICENSE("GPL");
448 MODULE_DESCRIPTION("NVEC battery and AC driver");
449 MODULE_ALIAS("platform:nvec-power");