Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
0004 //
0005 //  Copyright (C) 2009-2010 Samsung Electronics
0006 //  MyungJoo Ham <myungjoo.ham@samsung.com>
0007 
0008 #include <linux/err.h>
0009 #include <linux/module.h>
0010 #include <linux/mod_devicetable.h>
0011 #include <linux/slab.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/power_supply.h>
0014 #include <linux/mfd/max8998.h>
0015 #include <linux/mfd/max8998-private.h>
0016 
0017 struct max8998_battery_data {
0018     struct device *dev;
0019     struct max8998_dev *iodev;
0020     struct power_supply *battery;
0021 };
0022 
0023 static enum power_supply_property max8998_battery_props[] = {
0024     POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
0025     POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
0026     POWER_SUPPLY_PROP_STATUS, /* charger is charging/discharging/full */
0027 };
0028 
0029 /* Note that the charger control is done by a current regulator "CHARGER" */
0030 static int max8998_battery_get_property(struct power_supply *psy,
0031         enum power_supply_property psp,
0032         union power_supply_propval *val)
0033 {
0034     struct max8998_battery_data *max8998 = power_supply_get_drvdata(psy);
0035     struct i2c_client *i2c = max8998->iodev->i2c;
0036     int ret;
0037     u8 reg;
0038 
0039     switch (psp) {
0040     case POWER_SUPPLY_PROP_PRESENT:
0041         ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
0042         if (ret)
0043             return ret;
0044         if (reg & (1 << 4))
0045             val->intval = 0;
0046         else
0047             val->intval = 1;
0048         break;
0049     case POWER_SUPPLY_PROP_ONLINE:
0050         ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
0051         if (ret)
0052             return ret;
0053 
0054         if (reg & (1 << 5))
0055             val->intval = 1;
0056         else
0057             val->intval = 0;
0058 
0059         break;
0060     case POWER_SUPPLY_PROP_STATUS:
0061         ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
0062         if (ret)
0063             return ret;
0064 
0065         if (!(reg & (1 << 5))) {
0066             val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
0067         } else {
0068             if (reg & (1 << 6))
0069                 val->intval = POWER_SUPPLY_STATUS_FULL;
0070             else if (reg & (1 << 3))
0071                 val->intval = POWER_SUPPLY_STATUS_CHARGING;
0072             else
0073                 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
0074         }
0075         break;
0076     default:
0077         return -EINVAL;
0078     }
0079 
0080     return 0;
0081 }
0082 
0083 static const struct power_supply_desc max8998_battery_desc = {
0084     .name       = "max8998_pmic",
0085     .type       = POWER_SUPPLY_TYPE_BATTERY,
0086     .get_property   = max8998_battery_get_property,
0087     .properties = max8998_battery_props,
0088     .num_properties = ARRAY_SIZE(max8998_battery_props),
0089 };
0090 
0091 static int max8998_battery_probe(struct platform_device *pdev)
0092 {
0093     struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
0094     struct max8998_platform_data *pdata = iodev->pdata;
0095     struct power_supply_config psy_cfg = {};
0096     struct max8998_battery_data *max8998;
0097     struct i2c_client *i2c;
0098     int ret = 0;
0099 
0100     if (!pdata) {
0101         dev_err(pdev->dev.parent, "No platform init data supplied\n");
0102         return -ENODEV;
0103     }
0104 
0105     max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_battery_data),
0106                 GFP_KERNEL);
0107     if (!max8998)
0108         return -ENOMEM;
0109 
0110     max8998->dev = &pdev->dev;
0111     max8998->iodev = iodev;
0112     platform_set_drvdata(pdev, max8998);
0113     i2c = max8998->iodev->i2c;
0114 
0115     /* Setup "End of Charge" */
0116     /* If EOC value equals 0,
0117      * remain value set from bootloader or default value */
0118     if (pdata->eoc >= 10 && pdata->eoc <= 45) {
0119         max8998_update_reg(i2c, MAX8998_REG_CHGR1,
0120                 (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
0121     } else if (pdata->eoc == 0) {
0122         dev_dbg(max8998->dev,
0123             "EOC value not set: leave it unchanged.\n");
0124     } else {
0125         dev_err(max8998->dev, "Invalid EOC value\n");
0126         return -EINVAL;
0127     }
0128 
0129     /* Setup Charge Restart Level */
0130     switch (pdata->restart) {
0131     case 100:
0132         max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3);
0133         break;
0134     case 150:
0135         max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3);
0136         break;
0137     case 200:
0138         max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3);
0139         break;
0140     case -1:
0141         max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3);
0142         break;
0143     case 0:
0144         dev_dbg(max8998->dev,
0145             "Restart Level not set: leave it unchanged.\n");
0146         break;
0147     default:
0148         dev_err(max8998->dev, "Invalid Restart Level\n");
0149         return -EINVAL;
0150     }
0151 
0152     /* Setup Charge Full Timeout */
0153     switch (pdata->timeout) {
0154     case 5:
0155         max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
0156         break;
0157     case 6:
0158         max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
0159         break;
0160     case 7:
0161         max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
0162         break;
0163     case -1:
0164         max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
0165         break;
0166     case 0:
0167         dev_dbg(max8998->dev,
0168             "Full Timeout not set: leave it unchanged.\n");
0169         break;
0170     default:
0171         dev_err(max8998->dev, "Invalid Full Timeout value\n");
0172         return -EINVAL;
0173     }
0174 
0175     psy_cfg.drv_data = max8998;
0176 
0177     max8998->battery = devm_power_supply_register(max8998->dev,
0178                               &max8998_battery_desc,
0179                               &psy_cfg);
0180     if (IS_ERR(max8998->battery)) {
0181         ret = PTR_ERR(max8998->battery);
0182         dev_err(max8998->dev, "failed: power supply register: %d\n",
0183             ret);
0184         return ret;
0185     }
0186 
0187     return 0;
0188 }
0189 
0190 static const struct platform_device_id max8998_battery_id[] = {
0191     { "max8998-battery", TYPE_MAX8998 },
0192     { }
0193 };
0194 
0195 static struct platform_driver max8998_battery_driver = {
0196     .driver = {
0197         .name = "max8998-battery",
0198     },
0199     .probe = max8998_battery_probe,
0200     .id_table = max8998_battery_id,
0201 };
0202 
0203 module_platform_driver(max8998_battery_driver);
0204 
0205 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
0206 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
0207 MODULE_LICENSE("GPL");
0208 MODULE_ALIAS("platform:max8998-battery");