linux/drivers/power/max8998_charger.c
<<
>>
Prefs
   1/*
   2 * max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
   3 *
   4 *  Copyright (C) 2009-2010 Samsung Electronics
   5 *  MyungJoo Ham <myungjoo.ham@samsung.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 */
  21
  22#include <linux/err.h>
  23#include <linux/module.h>
  24#include <linux/slab.h>
  25#include <linux/platform_device.h>
  26#include <linux/power_supply.h>
  27#include <linux/mfd/max8998.h>
  28#include <linux/mfd/max8998-private.h>
  29
  30struct max8998_battery_data {
  31        struct device *dev;
  32        struct max8998_dev *iodev;
  33        struct power_supply battery;
  34};
  35
  36static enum power_supply_property max8998_battery_props[] = {
  37        POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
  38        POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
  39};
  40
  41/* Note that the charger control is done by a current regulator "CHARGER" */
  42static int max8998_battery_get_property(struct power_supply *psy,
  43                enum power_supply_property psp,
  44                union power_supply_propval *val)
  45{
  46        struct max8998_battery_data *max8998 = container_of(psy,
  47                        struct max8998_battery_data, battery);
  48        struct i2c_client *i2c = max8998->iodev->i2c;
  49        int ret;
  50        u8 reg;
  51
  52        switch (psp) {
  53        case POWER_SUPPLY_PROP_PRESENT:
  54                ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
  55                if (ret)
  56                        return ret;
  57                if (reg & (1 << 4))
  58                        val->intval = 0;
  59                else
  60                        val->intval = 1;
  61                break;
  62        case POWER_SUPPLY_PROP_ONLINE:
  63                ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
  64                if (ret)
  65                        return ret;
  66                if (reg & (1 << 3))
  67                        val->intval = 0;
  68                else
  69                        val->intval = 1;
  70                break;
  71        default:
  72                return -EINVAL;
  73        }
  74
  75        return 0;
  76}
  77
  78static __devinit int max8998_battery_probe(struct platform_device *pdev)
  79{
  80        struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  81        struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
  82        struct max8998_battery_data *max8998;
  83        struct i2c_client *i2c;
  84        int ret = 0;
  85
  86        if (!pdata) {
  87                dev_err(pdev->dev.parent, "No platform init data supplied\n");
  88                return -ENODEV;
  89        }
  90
  91        max8998 = kzalloc(sizeof(struct max8998_battery_data), GFP_KERNEL);
  92        if (!max8998)
  93                return -ENOMEM;
  94
  95        max8998->dev = &pdev->dev;
  96        max8998->iodev = iodev;
  97        platform_set_drvdata(pdev, max8998);
  98        i2c = max8998->iodev->i2c;
  99
 100        /* Setup "End of Charge" */
 101        /* If EOC value equals 0,
 102         * remain value set from bootloader or default value */
 103        if (pdata->eoc >= 10 && pdata->eoc <= 45) {
 104                max8998_update_reg(i2c, MAX8998_REG_CHGR1,
 105                                (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
 106        } else if (pdata->eoc == 0) {
 107                dev_dbg(max8998->dev,
 108                        "EOC value not set: leave it unchanged.\n");
 109        } else {
 110                dev_err(max8998->dev, "Invalid EOC value\n");
 111                ret = -EINVAL;
 112                goto err;
 113        }
 114
 115        /* Setup Charge Restart Level */
 116        switch (pdata->restart) {
 117        case 100:
 118                max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
 119                break;
 120        case 150:
 121                max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
 122                break;
 123        case 200:
 124                max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
 125                break;
 126        case -1:
 127                max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
 128                break;
 129        case 0:
 130                dev_dbg(max8998->dev,
 131                        "Restart Level not set: leave it unchanged.\n");
 132                break;
 133        default:
 134                dev_err(max8998->dev, "Invalid Restart Level\n");
 135                ret = -EINVAL;
 136                goto err;
 137        }
 138
 139        /* Setup Charge Full Timeout */
 140        switch (pdata->timeout) {
 141        case 5:
 142                max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
 143                break;
 144        case 6:
 145                max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
 146                break;
 147        case 7:
 148                max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
 149                break;
 150        case -1:
 151                max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
 152                break;
 153        case 0:
 154                dev_dbg(max8998->dev,
 155                        "Full Timeout not set: leave it unchanged.\n");
 156                break;
 157        default:
 158                dev_err(max8998->dev, "Invalid Full Timeout value\n");
 159                ret = -EINVAL;
 160                goto err;
 161        }
 162
 163        max8998->battery.name = "max8998_pmic";
 164        max8998->battery.type = POWER_SUPPLY_TYPE_BATTERY;
 165        max8998->battery.get_property = max8998_battery_get_property;
 166        max8998->battery.properties = max8998_battery_props;
 167        max8998->battery.num_properties = ARRAY_SIZE(max8998_battery_props);
 168
 169        ret = power_supply_register(max8998->dev, &max8998->battery);
 170        if (ret) {
 171                dev_err(max8998->dev, "failed: power supply register\n");
 172                goto err;
 173        }
 174
 175        return 0;
 176err:
 177        kfree(max8998);
 178        return ret;
 179}
 180
 181static int __devexit max8998_battery_remove(struct platform_device *pdev)
 182{
 183        struct max8998_battery_data *max8998 = platform_get_drvdata(pdev);
 184
 185        power_supply_unregister(&max8998->battery);
 186        kfree(max8998);
 187
 188        return 0;
 189}
 190
 191static const struct platform_device_id max8998_battery_id[] = {
 192        { "max8998-battery", TYPE_MAX8998 },
 193        { }
 194};
 195
 196static struct platform_driver max8998_battery_driver = {
 197        .driver = {
 198                .name = "max8998-battery",
 199                .owner = THIS_MODULE,
 200        },
 201        .probe = max8998_battery_probe,
 202        .remove = __devexit_p(max8998_battery_remove),
 203        .id_table = max8998_battery_id,
 204};
 205
 206module_platform_driver(max8998_battery_driver);
 207
 208MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
 209MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
 210MODULE_LICENSE("GPL");
 211MODULE_ALIAS("platform:max8998-battery");
 212
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.