0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/module.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/power_supply.h>
0016 #include <linux/regmap.h>
0017 #include <linux/sched.h>
0018 #include <linux/slab.h>
0019 #include <linux/workqueue.h>
0020
0021 enum {
0022 REG_CAPACITY,
0023 REG_VOLTAGE,
0024 REG_CURRENT,
0025 REG_DESIGN_CAPACITY,
0026 REG_TEMPERATURE,
0027 };
0028
0029 #define EC_DATA(_reg, _psp) { \
0030 .psp = POWER_SUPPLY_PROP_ ## _psp, \
0031 .reg = _reg, \
0032 }
0033
0034 static const struct battery_register {
0035 enum power_supply_property psp;
0036 unsigned int reg;
0037 } ec_data[] = {
0038 [REG_CAPACITY] = EC_DATA(0x00, CAPACITY),
0039 [REG_VOLTAGE] = EC_DATA(0x01, VOLTAGE_NOW),
0040 [REG_CURRENT] = EC_DATA(0x03, CURRENT_NOW),
0041 [REG_DESIGN_CAPACITY] = EC_DATA(0x08, CHARGE_FULL_DESIGN),
0042 [REG_TEMPERATURE] = EC_DATA(0x0a, TEMP),
0043 };
0044
0045 static const enum power_supply_property a500_battery_properties[] = {
0046 POWER_SUPPLY_PROP_CAPACITY,
0047 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0048 POWER_SUPPLY_PROP_CURRENT_NOW,
0049 POWER_SUPPLY_PROP_PRESENT,
0050 POWER_SUPPLY_PROP_STATUS,
0051 POWER_SUPPLY_PROP_TECHNOLOGY,
0052 POWER_SUPPLY_PROP_TEMP,
0053 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0054 };
0055
0056 struct a500_battery {
0057 struct delayed_work poll_work;
0058 struct power_supply *psy;
0059 struct regmap *regmap;
0060 unsigned int capacity;
0061 };
0062
0063 static bool a500_battery_update_capacity(struct a500_battery *bat)
0064 {
0065 unsigned int capacity;
0066 int err;
0067
0068 err = regmap_read(bat->regmap, ec_data[REG_CAPACITY].reg, &capacity);
0069 if (err)
0070 return false;
0071
0072
0073 capacity = min(capacity, 100u);
0074
0075 if (bat->capacity != capacity) {
0076 bat->capacity = capacity;
0077 return true;
0078 }
0079
0080 return false;
0081 }
0082
0083 static int a500_battery_get_status(struct a500_battery *bat)
0084 {
0085 if (bat->capacity < 100) {
0086 if (power_supply_am_i_supplied(bat->psy))
0087 return POWER_SUPPLY_STATUS_CHARGING;
0088 else
0089 return POWER_SUPPLY_STATUS_DISCHARGING;
0090 }
0091
0092 return POWER_SUPPLY_STATUS_FULL;
0093 }
0094
0095 static void a500_battery_unit_adjustment(struct device *dev,
0096 enum power_supply_property psp,
0097 union power_supply_propval *val)
0098 {
0099 const unsigned int base_unit_conversion = 1000;
0100 const unsigned int temp_kelvin_to_celsius = 2731;
0101
0102 switch (psp) {
0103 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0104 case POWER_SUPPLY_PROP_CURRENT_NOW:
0105 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0106 val->intval *= base_unit_conversion;
0107 break;
0108
0109 case POWER_SUPPLY_PROP_TEMP:
0110 val->intval -= temp_kelvin_to_celsius;
0111 break;
0112
0113 case POWER_SUPPLY_PROP_PRESENT:
0114 val->intval = !!val->intval;
0115 break;
0116
0117 default:
0118 dev_dbg(dev,
0119 "%s: no need for unit conversion %d\n", __func__, psp);
0120 }
0121 }
0122
0123 static int a500_battery_get_ec_data_index(struct device *dev,
0124 enum power_supply_property psp)
0125 {
0126 unsigned int i;
0127
0128
0129
0130
0131
0132
0133 if (psp == POWER_SUPPLY_PROP_PRESENT)
0134 psp = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
0135
0136 for (i = 0; i < ARRAY_SIZE(ec_data); i++)
0137 if (psp == ec_data[i].psp)
0138 return i;
0139
0140 dev_dbg(dev, "%s: invalid property %u\n", __func__, psp);
0141
0142 return -EINVAL;
0143 }
0144
0145 static int a500_battery_get_property(struct power_supply *psy,
0146 enum power_supply_property psp,
0147 union power_supply_propval *val)
0148 {
0149 struct a500_battery *bat = power_supply_get_drvdata(psy);
0150 struct device *dev = psy->dev.parent;
0151 int ret = 0;
0152
0153 switch (psp) {
0154 case POWER_SUPPLY_PROP_STATUS:
0155 val->intval = a500_battery_get_status(bat);
0156 break;
0157
0158 case POWER_SUPPLY_PROP_TECHNOLOGY:
0159 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
0160 break;
0161
0162 case POWER_SUPPLY_PROP_CAPACITY:
0163 a500_battery_update_capacity(bat);
0164 val->intval = bat->capacity;
0165 break;
0166
0167 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0168 case POWER_SUPPLY_PROP_CURRENT_NOW:
0169 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0170 case POWER_SUPPLY_PROP_PRESENT:
0171 case POWER_SUPPLY_PROP_TEMP:
0172 ret = a500_battery_get_ec_data_index(dev, psp);
0173 if (ret < 0)
0174 break;
0175
0176 ret = regmap_read(bat->regmap, ec_data[ret].reg, &val->intval);
0177 break;
0178
0179 default:
0180 dev_err(dev, "%s: invalid property %u\n", __func__, psp);
0181 return -EINVAL;
0182 }
0183
0184 if (!ret) {
0185
0186 a500_battery_unit_adjustment(dev, psp, val);
0187 }
0188
0189 dev_dbg(dev, "%s: property = %d, value = %x\n",
0190 __func__, psp, val->intval);
0191
0192
0193 if (ret)
0194 return -ENODATA;
0195
0196 return 0;
0197 }
0198
0199 static void a500_battery_poll_work(struct work_struct *work)
0200 {
0201 struct a500_battery *bat;
0202 bool capacity_changed;
0203
0204 bat = container_of(work, struct a500_battery, poll_work.work);
0205 capacity_changed = a500_battery_update_capacity(bat);
0206
0207 if (capacity_changed)
0208 power_supply_changed(bat->psy);
0209
0210
0211 schedule_delayed_work(&bat->poll_work, 30 * HZ);
0212 }
0213
0214 static const struct power_supply_desc a500_battery_desc = {
0215 .name = "ec-battery",
0216 .type = POWER_SUPPLY_TYPE_BATTERY,
0217 .properties = a500_battery_properties,
0218 .get_property = a500_battery_get_property,
0219 .num_properties = ARRAY_SIZE(a500_battery_properties),
0220 .external_power_changed = power_supply_changed,
0221 };
0222
0223 static int a500_battery_probe(struct platform_device *pdev)
0224 {
0225 struct power_supply_config psy_cfg = {};
0226 struct a500_battery *bat;
0227
0228 bat = devm_kzalloc(&pdev->dev, sizeof(*bat), GFP_KERNEL);
0229 if (!bat)
0230 return -ENOMEM;
0231
0232 platform_set_drvdata(pdev, bat);
0233
0234 psy_cfg.of_node = pdev->dev.parent->of_node;
0235 psy_cfg.drv_data = bat;
0236
0237 bat->regmap = dev_get_regmap(pdev->dev.parent, "KB930");
0238 if (!bat->regmap)
0239 return -EINVAL;
0240
0241 bat->psy = devm_power_supply_register_no_ws(&pdev->dev,
0242 &a500_battery_desc,
0243 &psy_cfg);
0244 if (IS_ERR(bat->psy))
0245 return dev_err_probe(&pdev->dev, PTR_ERR(bat->psy),
0246 "failed to register battery\n");
0247
0248 INIT_DELAYED_WORK(&bat->poll_work, a500_battery_poll_work);
0249 schedule_delayed_work(&bat->poll_work, HZ);
0250
0251 return 0;
0252 }
0253
0254 static int a500_battery_remove(struct platform_device *pdev)
0255 {
0256 struct a500_battery *bat = dev_get_drvdata(&pdev->dev);
0257
0258 cancel_delayed_work_sync(&bat->poll_work);
0259
0260 return 0;
0261 }
0262
0263 static int __maybe_unused a500_battery_suspend(struct device *dev)
0264 {
0265 struct a500_battery *bat = dev_get_drvdata(dev);
0266
0267 cancel_delayed_work_sync(&bat->poll_work);
0268
0269 return 0;
0270 }
0271
0272 static int __maybe_unused a500_battery_resume(struct device *dev)
0273 {
0274 struct a500_battery *bat = dev_get_drvdata(dev);
0275
0276 schedule_delayed_work(&bat->poll_work, HZ);
0277
0278 return 0;
0279 }
0280
0281 static SIMPLE_DEV_PM_OPS(a500_battery_pm_ops,
0282 a500_battery_suspend, a500_battery_resume);
0283
0284 static struct platform_driver a500_battery_driver = {
0285 .driver = {
0286 .name = "acer-a500-iconia-battery",
0287 .pm = &a500_battery_pm_ops,
0288 },
0289 .probe = a500_battery_probe,
0290 .remove = a500_battery_remove,
0291 };
0292 module_platform_driver(a500_battery_driver);
0293
0294 MODULE_DESCRIPTION("Battery gauge driver for Acer Iconia Tab A500");
0295 MODULE_AUTHOR("Dmitry Osipenko <digetx@gmail.com>");
0296 MODULE_ALIAS("platform:acer-a500-iconia-battery");
0297 MODULE_LICENSE("GPL");