linux/drivers/power/s3c_adc_battery.c
<<
>>
Prefs
   1/*
   2 *      iPAQ h1930/h1940/rx1950 battery controller driver
   3 *      Copyright (c) Vasily Khoruzhick
   4 *      Based on h1940_battery.c by Arnaud Patard
   5 *
   6 * This file is subject to the terms and conditions of the GNU General Public
   7 * License.  See the file COPYING in the main directory of this archive for
   8 * more details.
   9 *
  10 */
  11
  12#include <linux/interrupt.h>
  13#include <linux/platform_device.h>
  14#include <linux/power_supply.h>
  15#include <linux/leds.h>
  16#include <linux/gpio.h>
  17#include <linux/err.h>
  18#include <linux/timer.h>
  19#include <linux/jiffies.h>
  20#include <linux/s3c_adc_battery.h>
  21#include <linux/errno.h>
  22#include <linux/init.h>
  23
  24#include <plat/adc.h>
  25
  26#define BAT_POLL_INTERVAL               10000 /* ms */
  27#define JITTER_DELAY                    500 /* ms */
  28
  29struct s3c_adc_bat {
  30        struct power_supply             psy;
  31        struct s3c_adc_client           *client;
  32        struct s3c_adc_bat_pdata        *pdata;
  33        int                             volt_value;
  34        int                             cur_value;
  35        unsigned int                    timestamp;
  36        int                             level;
  37        int                             status;
  38        int                             cable_plugged:1;
  39};
  40
  41static struct delayed_work bat_work;
  42
  43static void s3c_adc_bat_ext_power_changed(struct power_supply *psy)
  44{
  45        schedule_delayed_work(&bat_work,
  46                msecs_to_jiffies(JITTER_DELAY));
  47}
  48
  49static enum power_supply_property s3c_adc_backup_bat_props[] = {
  50        POWER_SUPPLY_PROP_VOLTAGE_NOW,
  51        POWER_SUPPLY_PROP_VOLTAGE_MIN,
  52        POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  53};
  54
  55static int s3c_adc_backup_bat_get_property(struct power_supply *psy,
  56                                enum power_supply_property psp,
  57                                union power_supply_propval *val)
  58{
  59        struct s3c_adc_bat *bat = container_of(psy, struct s3c_adc_bat, psy);
  60
  61        if (!bat) {
  62                dev_err(psy->dev, "%s: no battery infos ?!\n", __func__);
  63                return -EINVAL;
  64        }
  65
  66        if (bat->volt_value < 0 ||
  67                jiffies_to_msecs(jiffies - bat->timestamp) >
  68                        BAT_POLL_INTERVAL) {
  69                bat->volt_value = s3c_adc_read(bat->client,
  70                        bat->pdata->backup_volt_channel);
  71                bat->volt_value *= bat->pdata->backup_volt_mult;
  72                bat->timestamp = jiffies;
  73        }
  74
  75        switch (psp) {
  76        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  77                val->intval = bat->volt_value;
  78                return 0;
  79        case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  80                val->intval = bat->pdata->backup_volt_min;
  81                return 0;
  82        case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  83                val->intval = bat->pdata->backup_volt_max;
  84                return 0;
  85        default:
  86                return -EINVAL;
  87        }
  88}
  89
  90static struct s3c_adc_bat backup_bat = {
  91        .psy = {
  92                .name           = "backup-battery",
  93                .type           = POWER_SUPPLY_TYPE_BATTERY,
  94                .properties     = s3c_adc_backup_bat_props,
  95                .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
  96                .get_property   = s3c_adc_backup_bat_get_property,
  97                .use_for_apm    = 1,
  98        },
  99};
 100
 101static enum power_supply_property s3c_adc_main_bat_props[] = {
 102        POWER_SUPPLY_PROP_STATUS,
 103        POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 104        POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
 105        POWER_SUPPLY_PROP_CHARGE_NOW,
 106        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 107        POWER_SUPPLY_PROP_CURRENT_NOW,
 108};
 109
 110static int calc_full_volt(int volt_val, int cur_val, int impedance)
 111{
 112        return volt_val + cur_val * impedance / 1000;
 113}
 114
 115static int charge_finished(struct s3c_adc_bat *bat)
 116{
 117        return bat->pdata->gpio_inverted ?
 118                !gpio_get_value(bat->pdata->gpio_charge_finished) :
 119                gpio_get_value(bat->pdata->gpio_charge_finished);
 120}
 121
 122static int s3c_adc_bat_get_property(struct power_supply *psy,
 123                                    enum power_supply_property psp,
 124                                    union power_supply_propval *val)
 125{
 126        struct s3c_adc_bat *bat = container_of(psy, struct s3c_adc_bat, psy);
 127
 128        int new_level;
 129        int full_volt;
 130        const struct s3c_adc_bat_thresh *lut = bat->pdata->lut_noac;
 131        unsigned int lut_size = bat->pdata->lut_noac_cnt;
 132
 133        if (!bat) {
 134                dev_err(psy->dev, "no battery infos ?!\n");
 135                return -EINVAL;
 136        }
 137
 138        if (bat->volt_value < 0 || bat->cur_value < 0 ||
 139                jiffies_to_msecs(jiffies - bat->timestamp) >
 140                        BAT_POLL_INTERVAL) {
 141                bat->volt_value = s3c_adc_read(bat->client,
 142                        bat->pdata->volt_channel) * bat->pdata->volt_mult;
 143                bat->cur_value = s3c_adc_read(bat->client,
 144                        bat->pdata->current_channel) * bat->pdata->current_mult;
 145                bat->timestamp = jiffies;
 146        }
 147
 148        if (bat->cable_plugged &&
 149                ((bat->pdata->gpio_charge_finished < 0) ||
 150                !charge_finished(bat))) {
 151                lut = bat->pdata->lut_acin;
 152                lut_size = bat->pdata->lut_acin_cnt;
 153        }
 154
 155        new_level = 100000;
 156        full_volt = calc_full_volt((bat->volt_value / 1000),
 157                (bat->cur_value / 1000), bat->pdata->internal_impedance);
 158
 159        if (full_volt < calc_full_volt(lut->volt, lut->cur,
 160                bat->pdata->internal_impedance)) {
 161                lut_size--;
 162                while (lut_size--) {
 163                        int lut_volt1;
 164                        int lut_volt2;
 165
 166                        lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
 167                                bat->pdata->internal_impedance);
 168                        lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
 169                                bat->pdata->internal_impedance);
 170                        if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
 171                                new_level = (lut[1].level +
 172                                        (lut[0].level - lut[1].level) *
 173                                        (full_volt - lut_volt2) /
 174                                        (lut_volt1 - lut_volt2)) * 1000;
 175                                break;
 176                        }
 177                        new_level = lut[1].level * 1000;
 178                        lut++;
 179                }
 180        }
 181
 182        bat->level = new_level;
 183
 184        switch (psp) {
 185        case POWER_SUPPLY_PROP_STATUS:
 186                if (bat->pdata->gpio_charge_finished < 0)
 187                        val->intval = bat->level == 100000 ?
 188                                POWER_SUPPLY_STATUS_FULL : bat->status;
 189                else
 190                        val->intval = bat->status;
 191                return 0;
 192        case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 193                val->intval = 100000;
 194                return 0;
 195        case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
 196                val->intval = 0;
 197                return 0;
 198        case POWER_SUPPLY_PROP_CHARGE_NOW:
 199                val->intval = bat->level;
 200                return 0;
 201        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 202                val->intval = bat->volt_value;
 203                return 0;
 204        case POWER_SUPPLY_PROP_CURRENT_NOW:
 205                val->intval = bat->cur_value;
 206                return 0;
 207        default:
 208                return -EINVAL;
 209        }
 210}
 211
 212static struct s3c_adc_bat main_bat = {
 213        .psy = {
 214                .name                   = "main-battery",
 215                .type                   = POWER_SUPPLY_TYPE_BATTERY,
 216                .properties             = s3c_adc_main_bat_props,
 217                .num_properties         = ARRAY_SIZE(s3c_adc_main_bat_props),
 218                .get_property           = s3c_adc_bat_get_property,
 219                .external_power_changed = s3c_adc_bat_ext_power_changed,
 220                .use_for_apm            = 1,
 221        },
 222};
 223
 224static void s3c_adc_bat_work(struct work_struct *work)
 225{
 226        struct s3c_adc_bat *bat = &main_bat;
 227        int is_charged;
 228        int is_plugged;
 229        static int was_plugged;
 230
 231        is_plugged = power_supply_am_i_supplied(&bat->psy);
 232        bat->cable_plugged = is_plugged;
 233        if (is_plugged != was_plugged) {
 234                was_plugged = is_plugged;
 235                if (is_plugged) {
 236                        if (bat->pdata->enable_charger)
 237                                bat->pdata->enable_charger();
 238                        bat->status = POWER_SUPPLY_STATUS_CHARGING;
 239                } else {
 240                        if (bat->pdata->disable_charger)
 241                                bat->pdata->disable_charger();
 242                        bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
 243                }
 244        } else {
 245                if ((bat->pdata->gpio_charge_finished >= 0) && is_plugged) {
 246                        is_charged = charge_finished(&main_bat);
 247                        if (is_charged) {
 248                                if (bat->pdata->disable_charger)
 249                                        bat->pdata->disable_charger();
 250                                bat->status = POWER_SUPPLY_STATUS_FULL;
 251                        } else {
 252                                if (bat->pdata->enable_charger)
 253                                        bat->pdata->enable_charger();
 254                                bat->status = POWER_SUPPLY_STATUS_CHARGING;
 255                        }
 256                }
 257        }
 258
 259        power_supply_changed(&bat->psy);
 260}
 261
 262static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
 263{
 264        schedule_delayed_work(&bat_work,
 265                msecs_to_jiffies(JITTER_DELAY));
 266        return IRQ_HANDLED;
 267}
 268
 269static int __init s3c_adc_bat_probe(struct platform_device *pdev)
 270{
 271        struct s3c_adc_client   *client;
 272        struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
 273        int ret;
 274
 275        client = s3c_adc_register(pdev, NULL, NULL, 0);
 276        if (IS_ERR(client)) {
 277                dev_err(&pdev->dev, "cannot register adc\n");
 278                return PTR_ERR(client);
 279        }
 280
 281        platform_set_drvdata(pdev, client);
 282
 283        main_bat.client = client;
 284        main_bat.pdata = pdata;
 285        main_bat.volt_value = -1;
 286        main_bat.cur_value = -1;
 287        main_bat.cable_plugged = 0;
 288        main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
 289
 290        ret = power_supply_register(&pdev->dev, &main_bat.psy);
 291        if (ret)
 292                goto err_reg_main;
 293        if (pdata->backup_volt_mult) {
 294                backup_bat.client = client;
 295                backup_bat.pdata = pdev->dev.platform_data;
 296                backup_bat.volt_value = -1;
 297                ret = power_supply_register(&pdev->dev, &backup_bat.psy);
 298                if (ret)
 299                        goto err_reg_backup;
 300        }
 301
 302        INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
 303
 304        if (pdata->gpio_charge_finished >= 0) {
 305                ret = gpio_request(pdata->gpio_charge_finished, "charged");
 306                if (ret)
 307                        goto err_gpio;
 308
 309                ret = request_irq(gpio_to_irq(pdata->gpio_charge_finished),
 310                                s3c_adc_bat_charged,
 311                                IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 312                                "battery charged", NULL);
 313                if (ret)
 314                        goto err_irq;
 315        }
 316
 317        if (pdata->init) {
 318                ret = pdata->init();
 319                if (ret)
 320                        goto err_platform;
 321        }
 322
 323        dev_info(&pdev->dev, "successfully loaded\n");
 324        device_init_wakeup(&pdev->dev, 1);
 325
 326        /* Schedule timer to check current status */
 327        schedule_delayed_work(&bat_work,
 328                msecs_to_jiffies(JITTER_DELAY));
 329
 330        return 0;
 331
 332err_platform:
 333        if (pdata->gpio_charge_finished >= 0)
 334                free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
 335err_irq:
 336        if (pdata->gpio_charge_finished >= 0)
 337                gpio_free(pdata->gpio_charge_finished);
 338err_gpio:
 339        if (pdata->backup_volt_mult)
 340                power_supply_unregister(&backup_bat.psy);
 341err_reg_backup:
 342        power_supply_unregister(&main_bat.psy);
 343err_reg_main:
 344        return ret;
 345}
 346
 347static int s3c_adc_bat_remove(struct platform_device *pdev)
 348{
 349        struct s3c_adc_client *client = platform_get_drvdata(pdev);
 350        struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
 351
 352        power_supply_unregister(&main_bat.psy);
 353        if (pdata->backup_volt_mult)
 354                power_supply_unregister(&backup_bat.psy);
 355
 356        s3c_adc_release(client);
 357
 358        if (pdata->gpio_charge_finished >= 0) {
 359                free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
 360                gpio_free(pdata->gpio_charge_finished);
 361        }
 362
 363        cancel_delayed_work(&bat_work);
 364
 365        if (pdata->exit)
 366                pdata->exit();
 367
 368        return 0;
 369}
 370
 371#ifdef CONFIG_PM
 372static int s3c_adc_bat_suspend(struct platform_device *pdev,
 373        pm_message_t state)
 374{
 375        struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
 376
 377        if (pdata->gpio_charge_finished >= 0) {
 378                if (device_may_wakeup(&pdev->dev))
 379                        enable_irq_wake(
 380                                gpio_to_irq(pdata->gpio_charge_finished));
 381                else {
 382                        disable_irq(gpio_to_irq(pdata->gpio_charge_finished));
 383                        main_bat.pdata->disable_charger();
 384                }
 385        }
 386
 387        return 0;
 388}
 389
 390static int s3c_adc_bat_resume(struct platform_device *pdev)
 391{
 392        struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
 393
 394        if (pdata->gpio_charge_finished >= 0) {
 395                if (device_may_wakeup(&pdev->dev))
 396                        disable_irq_wake(
 397                                gpio_to_irq(pdata->gpio_charge_finished));
 398                else
 399                        enable_irq(gpio_to_irq(pdata->gpio_charge_finished));
 400        }
 401
 402        /* Schedule timer to check current status */
 403        schedule_delayed_work(&bat_work,
 404                msecs_to_jiffies(JITTER_DELAY));
 405
 406        return 0;
 407}
 408#else
 409#define s3c_adc_bat_suspend NULL
 410#define s3c_adc_bat_resume NULL
 411#endif
 412
 413static struct platform_driver s3c_adc_bat_driver = {
 414        .driver         = {
 415                .name   = "s3c-adc-battery",
 416        },
 417        .probe          = s3c_adc_bat_probe,
 418        .remove         = s3c_adc_bat_remove,
 419        .suspend        = s3c_adc_bat_suspend,
 420        .resume         = s3c_adc_bat_resume,
 421};
 422
 423static int __init s3c_adc_bat_init(void)
 424{
 425        return platform_driver_register(&s3c_adc_bat_driver);
 426}
 427module_init(s3c_adc_bat_init);
 428
 429static void __exit s3c_adc_bat_exit(void)
 430{
 431        platform_driver_unregister(&s3c_adc_bat_driver);
 432}
 433module_exit(s3c_adc_bat_exit);
 434
 435MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
 436MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
 437MODULE_LICENSE("GPL");
 438