2 * nvec_power: power supply driver for a NVIDIA compliant embedded controller
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/err.h>
18 #include <linux/power_supply.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
21 #include <linux/delay.h>
25 #define GET_SYSTEM_STATUS 0x00
28 struct notifier_block notifier;
29 struct delayed_work poller;
30 struct nvec_chip *nvec;
38 int charge_full_design;
40 int critical_capacity;
56 AVERAGING_TIME_INTERVAL,
58 LAST_FULL_CHARGE_CAPACITY,
85 static struct power_supply *nvec_bat_psy;
86 static struct power_supply *nvec_psy;
88 static int nvec_power_notifier(struct notifier_block *nb,
89 unsigned long event_type, void *data)
91 struct nvec_power *power =
92 container_of(nb, struct nvec_power, notifier);
93 struct bat_response *res = data;
95 if (event_type != NVEC_SYS)
98 if (res->sub_type == 0) {
99 if (power->on != res->plu) {
100 power->on = res->plu;
101 power_supply_changed(nvec_psy);
108 static const int bat_init[] = {
109 LAST_FULL_CHARGE_CAPACITY, DESIGN_CAPACITY, CRITICAL_CAPACITY,
110 MANUFACTURER, MODEL, TYPE,
113 static void get_bat_mfg_data(struct nvec_power *power)
116 char buf[] = { NVEC_BAT, SLOT_STATUS };
118 for (i = 0; i < ARRAY_SIZE(bat_init); i++) {
119 buf[1] = bat_init[i];
120 nvec_write_async(power->nvec, buf, 2);
124 static int nvec_power_bat_notifier(struct notifier_block *nb,
125 unsigned long event_type, void *data)
127 struct nvec_power *power =
128 container_of(nb, struct nvec_power, notifier);
129 struct bat_response *res = data;
130 int status_changed = 0;
132 if (event_type != NVEC_BAT)
135 switch (res->sub_type) {
137 if (res->plc[0] & 1) {
138 if (power->bat_present == 0) {
140 get_bat_mfg_data(power);
143 power->bat_present = 1;
145 switch ((res->plc[0] >> 1) & 3) {
148 POWER_SUPPLY_STATUS_NOT_CHARGING;
152 POWER_SUPPLY_STATUS_CHARGING;
156 POWER_SUPPLY_STATUS_DISCHARGING;
159 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
162 if (power->bat_present == 1)
165 power->bat_present = 0;
166 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
168 power->bat_cap = res->plc[1];
170 power_supply_changed(nvec_bat_psy);
173 power->bat_voltage_now = res->plu * 1000;
176 power->time_remain = res->plu * 3600;
179 power->bat_current_now = res->pls * 1000;
181 case AVERAGE_CURRENT:
182 power->bat_current_avg = res->pls * 1000;
184 case CAPACITY_REMAINING:
185 power->capacity_remain = res->plu * 1000;
187 case LAST_FULL_CHARGE_CAPACITY:
188 power->charge_last_full = res->plu * 1000;
190 case DESIGN_CAPACITY:
191 power->charge_full_design = res->plu * 1000;
193 case CRITICAL_CAPACITY:
194 power->critical_capacity = res->plu * 1000;
197 power->bat_temperature = res->plu - 2732;
200 memcpy(power->bat_manu, &res->plc, res->length - 2);
201 power->bat_model[res->length - 2] = '\0';
204 memcpy(power->bat_model, &res->plc, res->length - 2);
205 power->bat_model[res->length - 2] = '\0';
208 memcpy(power->bat_type, &res->plc, res->length - 2);
209 power->bat_type[res->length - 2] = '\0';
211 * This differs a little from the spec fill in more if you find
214 if (!strncmp(power->bat_type, "Li", 30))
215 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_LION;
217 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
226 static int nvec_power_get_property(struct power_supply *psy,
227 enum power_supply_property psp,
228 union power_supply_propval *val)
230 struct nvec_power *power = dev_get_drvdata(psy->dev.parent);
233 case POWER_SUPPLY_PROP_ONLINE:
234 val->intval = power->on;
242 static int nvec_battery_get_property(struct power_supply *psy,
243 enum power_supply_property psp,
244 union power_supply_propval *val)
246 struct nvec_power *power = dev_get_drvdata(psy->dev.parent);
249 case POWER_SUPPLY_PROP_STATUS:
250 val->intval = power->bat_status;
252 case POWER_SUPPLY_PROP_CAPACITY:
253 val->intval = power->bat_cap;
255 case POWER_SUPPLY_PROP_PRESENT:
256 val->intval = power->bat_present;
258 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
259 val->intval = power->bat_voltage_now;
261 case POWER_SUPPLY_PROP_CURRENT_NOW:
262 val->intval = power->bat_current_now;
264 case POWER_SUPPLY_PROP_CURRENT_AVG:
265 val->intval = power->bat_current_avg;
267 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
268 val->intval = power->time_remain;
270 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
271 val->intval = power->charge_full_design;
273 case POWER_SUPPLY_PROP_CHARGE_FULL:
274 val->intval = power->charge_last_full;
276 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
277 val->intval = power->critical_capacity;
279 case POWER_SUPPLY_PROP_CHARGE_NOW:
280 val->intval = power->capacity_remain;
282 case POWER_SUPPLY_PROP_TEMP:
283 val->intval = power->bat_temperature;
285 case POWER_SUPPLY_PROP_MANUFACTURER:
286 val->strval = power->bat_manu;
288 case POWER_SUPPLY_PROP_MODEL_NAME:
289 val->strval = power->bat_model;
291 case POWER_SUPPLY_PROP_TECHNOLOGY:
292 val->intval = power->bat_type_enum;
300 static enum power_supply_property nvec_power_props[] = {
301 POWER_SUPPLY_PROP_ONLINE,
304 static enum power_supply_property nvec_battery_props[] = {
305 POWER_SUPPLY_PROP_STATUS,
306 POWER_SUPPLY_PROP_PRESENT,
307 POWER_SUPPLY_PROP_CAPACITY,
308 POWER_SUPPLY_PROP_VOLTAGE_NOW,
309 POWER_SUPPLY_PROP_CURRENT_NOW,
311 POWER_SUPPLY_PROP_CURRENT_AVG,
312 POWER_SUPPLY_PROP_TEMP,
313 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
315 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
316 POWER_SUPPLY_PROP_CHARGE_FULL,
317 POWER_SUPPLY_PROP_CHARGE_EMPTY,
318 POWER_SUPPLY_PROP_CHARGE_NOW,
319 POWER_SUPPLY_PROP_MANUFACTURER,
320 POWER_SUPPLY_PROP_MODEL_NAME,
321 POWER_SUPPLY_PROP_TECHNOLOGY,
324 static char *nvec_power_supplied_to[] = {
328 static const struct power_supply_desc nvec_bat_psy_desc = {
330 .type = POWER_SUPPLY_TYPE_BATTERY,
331 .properties = nvec_battery_props,
332 .num_properties = ARRAY_SIZE(nvec_battery_props),
333 .get_property = nvec_battery_get_property,
336 static const struct power_supply_desc nvec_psy_desc = {
338 .type = POWER_SUPPLY_TYPE_MAINS,
339 .properties = nvec_power_props,
340 .num_properties = ARRAY_SIZE(nvec_power_props),
341 .get_property = nvec_power_get_property,
345 static int const bat_iter[] = {
346 SLOT_STATUS, VOLTAGE, CURRENT, CAPACITY_REMAINING,
348 AVERAGE_CURRENT, TEMPERATURE, TIME_REMAINING,
352 static void nvec_power_poll(struct work_struct *work)
354 char buf[] = { NVEC_SYS, GET_SYSTEM_STATUS };
355 struct nvec_power *power = container_of(work, struct nvec_power,
358 if (counter >= ARRAY_SIZE(bat_iter))
361 /* AC status via sys req */
362 nvec_write_async(power->nvec, buf, 2);
366 * Select a battery request function via round robin doing it all at
367 * once seems to overload the power supply.
370 buf[1] = bat_iter[counter++];
371 nvec_write_async(power->nvec, buf, 2);
373 schedule_delayed_work(to_delayed_work(work), msecs_to_jiffies(5000));
376 static int nvec_power_probe(struct platform_device *pdev)
378 struct power_supply **psy;
379 const struct power_supply_desc *psy_desc;
380 struct nvec_power *power;
381 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
382 struct power_supply_config psy_cfg = {};
384 power = devm_kzalloc(&pdev->dev, sizeof(struct nvec_power), GFP_NOWAIT);
388 dev_set_drvdata(&pdev->dev, power);
394 psy_desc = &nvec_psy_desc;
395 psy_cfg.supplied_to = nvec_power_supplied_to;
396 psy_cfg.num_supplicants = ARRAY_SIZE(nvec_power_supplied_to);
398 power->notifier.notifier_call = nvec_power_notifier;
400 INIT_DELAYED_WORK(&power->poller, nvec_power_poll);
401 schedule_delayed_work(&power->poller, msecs_to_jiffies(5000));
405 psy_desc = &nvec_bat_psy_desc;
407 power->notifier.notifier_call = nvec_power_bat_notifier;
413 nvec_register_notifier(nvec, &power->notifier, NVEC_SYS);
416 get_bat_mfg_data(power);
418 *psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
420 return PTR_ERR_OR_ZERO(*psy);
423 static int nvec_power_remove(struct platform_device *pdev)
425 struct nvec_power *power = platform_get_drvdata(pdev);
427 cancel_delayed_work_sync(&power->poller);
428 nvec_unregister_notifier(power->nvec, &power->notifier);
431 power_supply_unregister(nvec_psy);
434 power_supply_unregister(nvec_bat_psy);
440 static struct platform_driver nvec_power_driver = {
441 .probe = nvec_power_probe,
442 .remove = nvec_power_remove,
444 .name = "nvec-power",
448 module_platform_driver(nvec_power_driver);
451 MODULE_LICENSE("GPL");
452 MODULE_DESCRIPTION("NVEC battery and AC driver");
453 MODULE_ALIAS("platform:nvec-power");