0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/err.h>
0014 #include <linux/power_supply.h>
0015 #include <linux/slab.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/delay.h>
0018
0019 #include "nvec.h"
0020
0021 #define GET_SYSTEM_STATUS 0x00
0022
0023 struct nvec_power {
0024 struct notifier_block notifier;
0025 struct delayed_work poller;
0026 struct nvec_chip *nvec;
0027 int on;
0028 int bat_present;
0029 int bat_status;
0030 int bat_voltage_now;
0031 int bat_current_now;
0032 int bat_current_avg;
0033 int time_remain;
0034 int charge_full_design;
0035 int charge_last_full;
0036 int critical_capacity;
0037 int capacity_remain;
0038 int bat_temperature;
0039 int bat_cap;
0040 int bat_type_enum;
0041 char bat_manu[30];
0042 char bat_model[30];
0043 char bat_type[30];
0044 };
0045
0046 enum {
0047 SLOT_STATUS,
0048 VOLTAGE,
0049 TIME_REMAINING,
0050 CURRENT,
0051 AVERAGE_CURRENT,
0052 AVERAGING_TIME_INTERVAL,
0053 CAPACITY_REMAINING,
0054 LAST_FULL_CHARGE_CAPACITY,
0055 DESIGN_CAPACITY,
0056 CRITICAL_CAPACITY,
0057 TEMPERATURE,
0058 MANUFACTURER,
0059 MODEL,
0060 TYPE,
0061 };
0062
0063 enum {
0064 AC,
0065 BAT,
0066 };
0067
0068 struct bat_response {
0069 u8 event_type;
0070 u8 length;
0071 u8 sub_type;
0072 u8 status;
0073
0074 union {
0075 char plc[30];
0076 u16 plu;
0077 s16 pls;
0078 };
0079 };
0080
0081 static struct power_supply *nvec_bat_psy;
0082 static struct power_supply *nvec_psy;
0083
0084 static int nvec_power_notifier(struct notifier_block *nb,
0085 unsigned long event_type, void *data)
0086 {
0087 struct nvec_power *power =
0088 container_of(nb, struct nvec_power, notifier);
0089 struct bat_response *res = data;
0090
0091 if (event_type != NVEC_SYS)
0092 return NOTIFY_DONE;
0093
0094 if (res->sub_type == 0) {
0095 if (power->on != res->plu) {
0096 power->on = res->plu;
0097 power_supply_changed(nvec_psy);
0098 }
0099 return NOTIFY_STOP;
0100 }
0101 return NOTIFY_OK;
0102 }
0103
0104 static const int bat_init[] = {
0105 LAST_FULL_CHARGE_CAPACITY, DESIGN_CAPACITY, CRITICAL_CAPACITY,
0106 MANUFACTURER, MODEL, TYPE,
0107 };
0108
0109 static void get_bat_mfg_data(struct nvec_power *power)
0110 {
0111 int i;
0112 char buf[] = { NVEC_BAT, SLOT_STATUS };
0113
0114 for (i = 0; i < ARRAY_SIZE(bat_init); i++) {
0115 buf[1] = bat_init[i];
0116 nvec_write_async(power->nvec, buf, 2);
0117 }
0118 }
0119
0120 static int nvec_power_bat_notifier(struct notifier_block *nb,
0121 unsigned long event_type, void *data)
0122 {
0123 struct nvec_power *power =
0124 container_of(nb, struct nvec_power, notifier);
0125 struct bat_response *res = data;
0126 int status_changed = 0;
0127
0128 if (event_type != NVEC_BAT)
0129 return NOTIFY_DONE;
0130
0131 switch (res->sub_type) {
0132 case SLOT_STATUS:
0133 if (res->plc[0] & 1) {
0134 if (power->bat_present == 0) {
0135 status_changed = 1;
0136 get_bat_mfg_data(power);
0137 }
0138
0139 power->bat_present = 1;
0140
0141 switch ((res->plc[0] >> 1) & 3) {
0142 case 0:
0143 power->bat_status =
0144 POWER_SUPPLY_STATUS_NOT_CHARGING;
0145 break;
0146 case 1:
0147 power->bat_status =
0148 POWER_SUPPLY_STATUS_CHARGING;
0149 break;
0150 case 2:
0151 power->bat_status =
0152 POWER_SUPPLY_STATUS_DISCHARGING;
0153 break;
0154 default:
0155 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
0156 }
0157 } else {
0158 if (power->bat_present == 1)
0159 status_changed = 1;
0160
0161 power->bat_present = 0;
0162 power->bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
0163 }
0164 power->bat_cap = res->plc[1];
0165 if (status_changed)
0166 power_supply_changed(nvec_bat_psy);
0167 break;
0168 case VOLTAGE:
0169 power->bat_voltage_now = res->plu * 1000;
0170 break;
0171 case TIME_REMAINING:
0172 power->time_remain = res->plu * 3600;
0173 break;
0174 case CURRENT:
0175 power->bat_current_now = res->pls * 1000;
0176 break;
0177 case AVERAGE_CURRENT:
0178 power->bat_current_avg = res->pls * 1000;
0179 break;
0180 case CAPACITY_REMAINING:
0181 power->capacity_remain = res->plu * 1000;
0182 break;
0183 case LAST_FULL_CHARGE_CAPACITY:
0184 power->charge_last_full = res->plu * 1000;
0185 break;
0186 case DESIGN_CAPACITY:
0187 power->charge_full_design = res->plu * 1000;
0188 break;
0189 case CRITICAL_CAPACITY:
0190 power->critical_capacity = res->plu * 1000;
0191 break;
0192 case TEMPERATURE:
0193 power->bat_temperature = res->plu - 2732;
0194 break;
0195 case MANUFACTURER:
0196 memcpy(power->bat_manu, &res->plc, res->length - 2);
0197 power->bat_model[res->length - 2] = '\0';
0198 break;
0199 case MODEL:
0200 memcpy(power->bat_model, &res->plc, res->length - 2);
0201 power->bat_model[res->length - 2] = '\0';
0202 break;
0203 case TYPE:
0204 memcpy(power->bat_type, &res->plc, res->length - 2);
0205 power->bat_type[res->length - 2] = '\0';
0206
0207
0208
0209
0210 if (!strncmp(power->bat_type, "Li", 30))
0211 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_LION;
0212 else
0213 power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
0214 break;
0215 default:
0216 return NOTIFY_STOP;
0217 }
0218
0219 return NOTIFY_STOP;
0220 }
0221
0222 static int nvec_power_get_property(struct power_supply *psy,
0223 enum power_supply_property psp,
0224 union power_supply_propval *val)
0225 {
0226 struct nvec_power *power = dev_get_drvdata(psy->dev.parent);
0227
0228 switch (psp) {
0229 case POWER_SUPPLY_PROP_ONLINE:
0230 val->intval = power->on;
0231 break;
0232 default:
0233 return -EINVAL;
0234 }
0235 return 0;
0236 }
0237
0238 static int nvec_battery_get_property(struct power_supply *psy,
0239 enum power_supply_property psp,
0240 union power_supply_propval *val)
0241 {
0242 struct nvec_power *power = dev_get_drvdata(psy->dev.parent);
0243
0244 switch (psp) {
0245 case POWER_SUPPLY_PROP_STATUS:
0246 val->intval = power->bat_status;
0247 break;
0248 case POWER_SUPPLY_PROP_CAPACITY:
0249 val->intval = power->bat_cap;
0250 break;
0251 case POWER_SUPPLY_PROP_PRESENT:
0252 val->intval = power->bat_present;
0253 break;
0254 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0255 val->intval = power->bat_voltage_now;
0256 break;
0257 case POWER_SUPPLY_PROP_CURRENT_NOW:
0258 val->intval = power->bat_current_now;
0259 break;
0260 case POWER_SUPPLY_PROP_CURRENT_AVG:
0261 val->intval = power->bat_current_avg;
0262 break;
0263 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
0264 val->intval = power->time_remain;
0265 break;
0266 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0267 val->intval = power->charge_full_design;
0268 break;
0269 case POWER_SUPPLY_PROP_CHARGE_FULL:
0270 val->intval = power->charge_last_full;
0271 break;
0272 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
0273 val->intval = power->critical_capacity;
0274 break;
0275 case POWER_SUPPLY_PROP_CHARGE_NOW:
0276 val->intval = power->capacity_remain;
0277 break;
0278 case POWER_SUPPLY_PROP_TEMP:
0279 val->intval = power->bat_temperature;
0280 break;
0281 case POWER_SUPPLY_PROP_MANUFACTURER:
0282 val->strval = power->bat_manu;
0283 break;
0284 case POWER_SUPPLY_PROP_MODEL_NAME:
0285 val->strval = power->bat_model;
0286 break;
0287 case POWER_SUPPLY_PROP_TECHNOLOGY:
0288 val->intval = power->bat_type_enum;
0289 break;
0290 default:
0291 return -EINVAL;
0292 }
0293 return 0;
0294 }
0295
0296 static enum power_supply_property nvec_power_props[] = {
0297 POWER_SUPPLY_PROP_ONLINE,
0298 };
0299
0300 static enum power_supply_property nvec_battery_props[] = {
0301 POWER_SUPPLY_PROP_STATUS,
0302 POWER_SUPPLY_PROP_PRESENT,
0303 POWER_SUPPLY_PROP_CAPACITY,
0304 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0305 POWER_SUPPLY_PROP_CURRENT_NOW,
0306 #ifdef EC_FULL_DIAG
0307 POWER_SUPPLY_PROP_CURRENT_AVG,
0308 POWER_SUPPLY_PROP_TEMP,
0309 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
0310 #endif
0311 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0312 POWER_SUPPLY_PROP_CHARGE_FULL,
0313 POWER_SUPPLY_PROP_CHARGE_EMPTY,
0314 POWER_SUPPLY_PROP_CHARGE_NOW,
0315 POWER_SUPPLY_PROP_MANUFACTURER,
0316 POWER_SUPPLY_PROP_MODEL_NAME,
0317 POWER_SUPPLY_PROP_TECHNOLOGY,
0318 };
0319
0320 static char *nvec_power_supplied_to[] = {
0321 "battery",
0322 };
0323
0324 static const struct power_supply_desc nvec_bat_psy_desc = {
0325 .name = "battery",
0326 .type = POWER_SUPPLY_TYPE_BATTERY,
0327 .properties = nvec_battery_props,
0328 .num_properties = ARRAY_SIZE(nvec_battery_props),
0329 .get_property = nvec_battery_get_property,
0330 };
0331
0332 static const struct power_supply_desc nvec_psy_desc = {
0333 .name = "ac",
0334 .type = POWER_SUPPLY_TYPE_MAINS,
0335 .properties = nvec_power_props,
0336 .num_properties = ARRAY_SIZE(nvec_power_props),
0337 .get_property = nvec_power_get_property,
0338 };
0339
0340 static int counter;
0341 static const int bat_iter[] = {
0342 SLOT_STATUS, VOLTAGE, CURRENT, CAPACITY_REMAINING,
0343 #ifdef EC_FULL_DIAG
0344 AVERAGE_CURRENT, TEMPERATURE, TIME_REMAINING,
0345 #endif
0346 };
0347
0348 static void nvec_power_poll(struct work_struct *work)
0349 {
0350 char buf[] = { NVEC_SYS, GET_SYSTEM_STATUS };
0351 struct nvec_power *power = container_of(work, struct nvec_power,
0352 poller.work);
0353
0354 if (counter >= ARRAY_SIZE(bat_iter))
0355 counter = 0;
0356
0357
0358 nvec_write_async(power->nvec, buf, 2);
0359 msleep(100);
0360
0361
0362
0363
0364
0365 buf[0] = NVEC_BAT;
0366 buf[1] = bat_iter[counter++];
0367 nvec_write_async(power->nvec, buf, 2);
0368
0369 schedule_delayed_work(to_delayed_work(work), msecs_to_jiffies(5000));
0370 };
0371
0372 static int nvec_power_probe(struct platform_device *pdev)
0373 {
0374 struct power_supply **psy;
0375 const struct power_supply_desc *psy_desc;
0376 struct nvec_power *power;
0377 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
0378 struct power_supply_config psy_cfg = {};
0379
0380 power = devm_kzalloc(&pdev->dev, sizeof(struct nvec_power), GFP_NOWAIT);
0381 if (!power)
0382 return -ENOMEM;
0383
0384 dev_set_drvdata(&pdev->dev, power);
0385 power->nvec = nvec;
0386
0387 switch (pdev->id) {
0388 case AC:
0389 psy = &nvec_psy;
0390 psy_desc = &nvec_psy_desc;
0391 psy_cfg.supplied_to = nvec_power_supplied_to;
0392 psy_cfg.num_supplicants = ARRAY_SIZE(nvec_power_supplied_to);
0393
0394 power->notifier.notifier_call = nvec_power_notifier;
0395
0396 INIT_DELAYED_WORK(&power->poller, nvec_power_poll);
0397 schedule_delayed_work(&power->poller, msecs_to_jiffies(5000));
0398 break;
0399 case BAT:
0400 psy = &nvec_bat_psy;
0401 psy_desc = &nvec_bat_psy_desc;
0402
0403 power->notifier.notifier_call = nvec_power_bat_notifier;
0404 break;
0405 default:
0406 return -ENODEV;
0407 }
0408
0409 nvec_register_notifier(nvec, &power->notifier, NVEC_SYS);
0410
0411 if (pdev->id == BAT)
0412 get_bat_mfg_data(power);
0413
0414 *psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
0415
0416 return PTR_ERR_OR_ZERO(*psy);
0417 }
0418
0419 static int nvec_power_remove(struct platform_device *pdev)
0420 {
0421 struct nvec_power *power = platform_get_drvdata(pdev);
0422
0423 cancel_delayed_work_sync(&power->poller);
0424 nvec_unregister_notifier(power->nvec, &power->notifier);
0425 switch (pdev->id) {
0426 case AC:
0427 power_supply_unregister(nvec_psy);
0428 break;
0429 case BAT:
0430 power_supply_unregister(nvec_bat_psy);
0431 }
0432
0433 return 0;
0434 }
0435
0436 static struct platform_driver nvec_power_driver = {
0437 .probe = nvec_power_probe,
0438 .remove = nvec_power_remove,
0439 .driver = {
0440 .name = "nvec-power",
0441 }
0442 };
0443
0444 module_platform_driver(nvec_power_driver);
0445
0446 MODULE_AUTHOR("Ilya Petrov <ilya.muromec@gmail.com>");
0447 MODULE_LICENSE("GPL");
0448 MODULE_DESCRIPTION("NVEC battery and AC driver");
0449 MODULE_ALIAS("platform:nvec-power");