0001
0002
0003
0004
0005
0006
0007 #include <linux/interrupt.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/power_supply.h>
0010 #include <linux/leds.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/err.h>
0013 #include <linux/timer.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/s3c_adc_battery.h>
0016 #include <linux/errno.h>
0017 #include <linux/init.h>
0018 #include <linux/module.h>
0019
0020 #include <linux/soc/samsung/s3c-adc.h>
0021
0022 #define BAT_POLL_INTERVAL 10000
0023 #define JITTER_DELAY 500
0024
0025 struct s3c_adc_bat {
0026 struct power_supply *psy;
0027 struct s3c_adc_client *client;
0028 struct s3c_adc_bat_pdata *pdata;
0029 struct gpio_desc *charge_finished;
0030 int volt_value;
0031 int cur_value;
0032 unsigned int timestamp;
0033 int level;
0034 int status;
0035 int cable_plugged:1;
0036 };
0037
0038 static struct delayed_work bat_work;
0039
0040 static void s3c_adc_bat_ext_power_changed(struct power_supply *psy)
0041 {
0042 schedule_delayed_work(&bat_work,
0043 msecs_to_jiffies(JITTER_DELAY));
0044 }
0045
0046 static int gather_samples(struct s3c_adc_client *client, int num, int channel)
0047 {
0048 int value, i;
0049
0050
0051 if (num < 1)
0052 num = 1;
0053
0054 value = 0;
0055 for (i = 0; i < num; i++)
0056 value += s3c_adc_read(client, channel);
0057 value /= num;
0058
0059 return value;
0060 }
0061
0062 static enum power_supply_property s3c_adc_backup_bat_props[] = {
0063 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0064 POWER_SUPPLY_PROP_VOLTAGE_MIN,
0065 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
0066 };
0067
0068 static int s3c_adc_backup_bat_get_property(struct power_supply *psy,
0069 enum power_supply_property psp,
0070 union power_supply_propval *val)
0071 {
0072 struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
0073
0074 if (!bat) {
0075 dev_err(&psy->dev, "%s: no battery infos ?!\n", __func__);
0076 return -EINVAL;
0077 }
0078
0079 if (bat->volt_value < 0 ||
0080 jiffies_to_msecs(jiffies - bat->timestamp) >
0081 BAT_POLL_INTERVAL) {
0082 bat->volt_value = gather_samples(bat->client,
0083 bat->pdata->backup_volt_samples,
0084 bat->pdata->backup_volt_channel);
0085 bat->volt_value *= bat->pdata->backup_volt_mult;
0086 bat->timestamp = jiffies;
0087 }
0088
0089 switch (psp) {
0090 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0091 val->intval = bat->volt_value;
0092 return 0;
0093 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
0094 val->intval = bat->pdata->backup_volt_min;
0095 return 0;
0096 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
0097 val->intval = bat->pdata->backup_volt_max;
0098 return 0;
0099 default:
0100 return -EINVAL;
0101 }
0102 }
0103
0104 static const struct power_supply_desc backup_bat_desc = {
0105 .name = "backup-battery",
0106 .type = POWER_SUPPLY_TYPE_BATTERY,
0107 .properties = s3c_adc_backup_bat_props,
0108 .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
0109 .get_property = s3c_adc_backup_bat_get_property,
0110 .use_for_apm = 1,
0111 };
0112
0113 static struct s3c_adc_bat backup_bat;
0114
0115 static enum power_supply_property s3c_adc_main_bat_props[] = {
0116 POWER_SUPPLY_PROP_STATUS,
0117 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0118 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
0119 POWER_SUPPLY_PROP_CHARGE_NOW,
0120 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0121 POWER_SUPPLY_PROP_CURRENT_NOW,
0122 };
0123
0124 static int calc_full_volt(int volt_val, int cur_val, int impedance)
0125 {
0126 return volt_val + cur_val * impedance / 1000;
0127 }
0128
0129 static int charge_finished(struct s3c_adc_bat *bat)
0130 {
0131 return gpiod_get_value(bat->charge_finished);
0132 }
0133
0134 static int s3c_adc_bat_get_property(struct power_supply *psy,
0135 enum power_supply_property psp,
0136 union power_supply_propval *val)
0137 {
0138 struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
0139
0140 int new_level;
0141 int full_volt;
0142 const struct s3c_adc_bat_thresh *lut;
0143 unsigned int lut_size;
0144
0145 if (!bat) {
0146 dev_err(&psy->dev, "no battery infos ?!\n");
0147 return -EINVAL;
0148 }
0149
0150 lut = bat->pdata->lut_noac;
0151 lut_size = bat->pdata->lut_noac_cnt;
0152
0153 if (bat->volt_value < 0 || bat->cur_value < 0 ||
0154 jiffies_to_msecs(jiffies - bat->timestamp) >
0155 BAT_POLL_INTERVAL) {
0156 bat->volt_value = gather_samples(bat->client,
0157 bat->pdata->volt_samples,
0158 bat->pdata->volt_channel) * bat->pdata->volt_mult;
0159 bat->cur_value = gather_samples(bat->client,
0160 bat->pdata->current_samples,
0161 bat->pdata->current_channel) * bat->pdata->current_mult;
0162 bat->timestamp = jiffies;
0163 }
0164
0165 if (bat->cable_plugged &&
0166 (!bat->charge_finished ||
0167 !charge_finished(bat))) {
0168 lut = bat->pdata->lut_acin;
0169 lut_size = bat->pdata->lut_acin_cnt;
0170 }
0171
0172 new_level = 100000;
0173 full_volt = calc_full_volt((bat->volt_value / 1000),
0174 (bat->cur_value / 1000), bat->pdata->internal_impedance);
0175
0176 if (full_volt < calc_full_volt(lut->volt, lut->cur,
0177 bat->pdata->internal_impedance)) {
0178 lut_size--;
0179 while (lut_size--) {
0180 int lut_volt1;
0181 int lut_volt2;
0182
0183 lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
0184 bat->pdata->internal_impedance);
0185 lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
0186 bat->pdata->internal_impedance);
0187 if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
0188 new_level = (lut[1].level +
0189 (lut[0].level - lut[1].level) *
0190 (full_volt - lut_volt2) /
0191 (lut_volt1 - lut_volt2)) * 1000;
0192 break;
0193 }
0194 new_level = lut[1].level * 1000;
0195 lut++;
0196 }
0197 }
0198
0199 bat->level = new_level;
0200
0201 switch (psp) {
0202 case POWER_SUPPLY_PROP_STATUS:
0203 if (!bat->charge_finished)
0204 val->intval = bat->level == 100000 ?
0205 POWER_SUPPLY_STATUS_FULL : bat->status;
0206 else
0207 val->intval = bat->status;
0208 return 0;
0209 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0210 val->intval = 100000;
0211 return 0;
0212 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
0213 val->intval = 0;
0214 return 0;
0215 case POWER_SUPPLY_PROP_CHARGE_NOW:
0216 val->intval = bat->level;
0217 return 0;
0218 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0219 val->intval = bat->volt_value;
0220 return 0;
0221 case POWER_SUPPLY_PROP_CURRENT_NOW:
0222 val->intval = bat->cur_value;
0223 return 0;
0224 default:
0225 return -EINVAL;
0226 }
0227 }
0228
0229 static const struct power_supply_desc main_bat_desc = {
0230 .name = "main-battery",
0231 .type = POWER_SUPPLY_TYPE_BATTERY,
0232 .properties = s3c_adc_main_bat_props,
0233 .num_properties = ARRAY_SIZE(s3c_adc_main_bat_props),
0234 .get_property = s3c_adc_bat_get_property,
0235 .external_power_changed = s3c_adc_bat_ext_power_changed,
0236 .use_for_apm = 1,
0237 };
0238
0239 static struct s3c_adc_bat main_bat;
0240
0241 static void s3c_adc_bat_work(struct work_struct *work)
0242 {
0243 struct s3c_adc_bat *bat = &main_bat;
0244 int is_charged;
0245 int is_plugged;
0246 static int was_plugged;
0247
0248 is_plugged = power_supply_am_i_supplied(bat->psy);
0249 bat->cable_plugged = is_plugged;
0250 if (is_plugged != was_plugged) {
0251 was_plugged = is_plugged;
0252 if (is_plugged) {
0253 if (bat->pdata->enable_charger)
0254 bat->pdata->enable_charger();
0255 bat->status = POWER_SUPPLY_STATUS_CHARGING;
0256 } else {
0257 if (bat->pdata->disable_charger)
0258 bat->pdata->disable_charger();
0259 bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
0260 }
0261 } else {
0262 if (bat->charge_finished && is_plugged) {
0263 is_charged = charge_finished(&main_bat);
0264 if (is_charged) {
0265 if (bat->pdata->disable_charger)
0266 bat->pdata->disable_charger();
0267 bat->status = POWER_SUPPLY_STATUS_FULL;
0268 } else {
0269 if (bat->pdata->enable_charger)
0270 bat->pdata->enable_charger();
0271 bat->status = POWER_SUPPLY_STATUS_CHARGING;
0272 }
0273 }
0274 }
0275
0276 power_supply_changed(bat->psy);
0277 }
0278
0279 static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
0280 {
0281 schedule_delayed_work(&bat_work,
0282 msecs_to_jiffies(JITTER_DELAY));
0283 return IRQ_HANDLED;
0284 }
0285
0286 static int s3c_adc_bat_probe(struct platform_device *pdev)
0287 {
0288 struct s3c_adc_client *client;
0289 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
0290 struct power_supply_config psy_cfg = {};
0291 struct gpio_desc *gpiod;
0292 int ret;
0293
0294 client = s3c_adc_register(pdev, NULL, NULL, 0);
0295 if (IS_ERR(client)) {
0296 dev_err(&pdev->dev, "cannot register adc\n");
0297 return PTR_ERR(client);
0298 }
0299
0300 platform_set_drvdata(pdev, client);
0301
0302 gpiod = devm_gpiod_get_optional(&pdev->dev, "charge-status", GPIOD_IN);
0303 if (IS_ERR(gpiod)) {
0304
0305 ret = PTR_ERR(gpiod);
0306 dev_err(&pdev->dev, "no GPIO %d\n", ret);
0307 return ret;
0308 }
0309
0310 main_bat.client = client;
0311 main_bat.pdata = pdata;
0312 main_bat.charge_finished = gpiod;
0313 main_bat.volt_value = -1;
0314 main_bat.cur_value = -1;
0315 main_bat.cable_plugged = 0;
0316 main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
0317 psy_cfg.drv_data = &main_bat;
0318
0319 main_bat.psy = power_supply_register(&pdev->dev, &main_bat_desc, &psy_cfg);
0320 if (IS_ERR(main_bat.psy)) {
0321 ret = PTR_ERR(main_bat.psy);
0322 goto err_reg_main;
0323 }
0324 if (pdata->backup_volt_mult) {
0325 const struct power_supply_config backup_psy_cfg
0326 = { .drv_data = &backup_bat, };
0327
0328 backup_bat.client = client;
0329 backup_bat.pdata = pdev->dev.platform_data;
0330 backup_bat.charge_finished = gpiod;
0331 backup_bat.volt_value = -1;
0332 backup_bat.psy = power_supply_register(&pdev->dev,
0333 &backup_bat_desc,
0334 &backup_psy_cfg);
0335 if (IS_ERR(backup_bat.psy)) {
0336 ret = PTR_ERR(backup_bat.psy);
0337 goto err_reg_backup;
0338 }
0339 }
0340
0341 INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
0342
0343 if (gpiod) {
0344 ret = request_irq(gpiod_to_irq(gpiod),
0345 s3c_adc_bat_charged,
0346 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
0347 "battery charged", NULL);
0348 if (ret)
0349 goto err_irq;
0350 }
0351
0352 if (pdata->init) {
0353 ret = pdata->init();
0354 if (ret)
0355 goto err_platform;
0356 }
0357
0358 dev_info(&pdev->dev, "successfully loaded\n");
0359 device_init_wakeup(&pdev->dev, 1);
0360
0361
0362 schedule_delayed_work(&bat_work,
0363 msecs_to_jiffies(JITTER_DELAY));
0364
0365 return 0;
0366
0367 err_platform:
0368 if (gpiod)
0369 free_irq(gpiod_to_irq(gpiod), NULL);
0370 err_irq:
0371 if (pdata->backup_volt_mult)
0372 power_supply_unregister(backup_bat.psy);
0373 err_reg_backup:
0374 power_supply_unregister(main_bat.psy);
0375 err_reg_main:
0376 return ret;
0377 }
0378
0379 static int s3c_adc_bat_remove(struct platform_device *pdev)
0380 {
0381 struct s3c_adc_client *client = platform_get_drvdata(pdev);
0382 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
0383
0384 power_supply_unregister(main_bat.psy);
0385 if (pdata->backup_volt_mult)
0386 power_supply_unregister(backup_bat.psy);
0387
0388 s3c_adc_release(client);
0389
0390 if (main_bat.charge_finished)
0391 free_irq(gpiod_to_irq(main_bat.charge_finished), NULL);
0392
0393 cancel_delayed_work_sync(&bat_work);
0394
0395 if (pdata->exit)
0396 pdata->exit();
0397
0398 return 0;
0399 }
0400
0401 #ifdef CONFIG_PM
0402 static int s3c_adc_bat_suspend(struct platform_device *pdev,
0403 pm_message_t state)
0404 {
0405 if (main_bat.charge_finished) {
0406 if (device_may_wakeup(&pdev->dev))
0407 enable_irq_wake(
0408 gpiod_to_irq(main_bat.charge_finished));
0409 else {
0410 disable_irq(gpiod_to_irq(main_bat.charge_finished));
0411 main_bat.pdata->disable_charger();
0412 }
0413 }
0414
0415 return 0;
0416 }
0417
0418 static int s3c_adc_bat_resume(struct platform_device *pdev)
0419 {
0420 if (main_bat.charge_finished) {
0421 if (device_may_wakeup(&pdev->dev))
0422 disable_irq_wake(
0423 gpiod_to_irq(main_bat.charge_finished));
0424 else
0425 enable_irq(gpiod_to_irq(main_bat.charge_finished));
0426 }
0427
0428
0429 schedule_delayed_work(&bat_work,
0430 msecs_to_jiffies(JITTER_DELAY));
0431
0432 return 0;
0433 }
0434 #else
0435 #define s3c_adc_bat_suspend NULL
0436 #define s3c_adc_bat_resume NULL
0437 #endif
0438
0439 static struct platform_driver s3c_adc_bat_driver = {
0440 .driver = {
0441 .name = "s3c-adc-battery",
0442 },
0443 .probe = s3c_adc_bat_probe,
0444 .remove = s3c_adc_bat_remove,
0445 .suspend = s3c_adc_bat_suspend,
0446 .resume = s3c_adc_bat_resume,
0447 };
0448
0449 module_platform_driver(s3c_adc_bat_driver);
0450
0451 MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
0452 MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
0453 MODULE_LICENSE("GPL");