1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // max8997_charger.c - Power supply consumer driver for the Maxim 8997/8966 4 // 5 // Copyright (C) 2011 Samsung Electronics 6 // MyungJoo Ham <myungjoo.ham@samsung.com> 7 8 #include <linux/err.h> 9 #include <linux/module.h> 10 #include <linux/slab.h> 11 #include <linux/platform_device.h> 12 #include <linux/power_supply.h> 13 #include <linux/mfd/max8997.h> 14 #include <linux/mfd/max8997-private.h> 15 16 /* MAX8997_REG_STATUS4 */ 17 #define DCINOK_SHIFT 1 18 #define DCINOK_MASK (1 << DCINOK_SHIFT) 19 #define DETBAT_SHIFT 2 20 #define DETBAT_MASK (1 << DETBAT_SHIFT) 21 22 /* MAX8997_REG_MBCCTRL1 */ 23 #define TFCH_SHIFT 4 24 #define TFCH_MASK (7 << TFCH_SHIFT) 25 26 /* MAX8997_REG_MBCCTRL5 */ 27 #define ITOPOFF_SHIFT 0 28 #define ITOPOFF_MASK (0xF << ITOPOFF_SHIFT) 29 30 struct charger_data { 31 struct device *dev; 32 struct max8997_dev *iodev; 33 struct power_supply *battery; 34 }; 35 36 static enum power_supply_property max8997_battery_props[] = { 37 POWER_SUPPLY_PROP_STATUS, /* "FULL", "CHARGING" or "DISCHARGING". */ 38 POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */ 39 POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */ 40 }; 41 42 /* Note that the charger control is done by a current regulator "CHARGER" */ 43 static int max8997_battery_get_property(struct power_supply *psy, 44 enum power_supply_property psp, 45 union power_supply_propval *val) 46 { 47 struct charger_data *charger = power_supply_get_drvdata(psy); 48 struct i2c_client *i2c = charger->iodev->i2c; 49 int ret; 50 u8 reg; 51 52 switch (psp) { 53 case POWER_SUPPLY_PROP_STATUS: 54 val->intval = 0; 55 ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, ®); 56 if (ret) 57 return ret; 58 if ((reg & (1 << 0)) == 0x1) 59 val->intval = POWER_SUPPLY_STATUS_FULL; 60 else if ((reg & DCINOK_MASK)) 61 val->intval = POWER_SUPPLY_STATUS_CHARGING; 62 else 63 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 64 65 break; 66 case POWER_SUPPLY_PROP_PRESENT: 67 val->intval = 0; 68 ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, ®); 69 if (ret) 70 return ret; 71 if ((reg & DETBAT_MASK) == 0x0) 72 val->intval = 1; 73 74 break; 75 case POWER_SUPPLY_PROP_ONLINE: 76 val->intval = 0; 77 ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, ®); 78 if (ret) 79 return ret; 80 if (reg & DCINOK_MASK) 81 val->intval = 1; 82 83 break; 84 default: 85 return -EINVAL; 86 } 87 88 return 0; 89 } 90 91 static const struct power_supply_desc max8997_battery_desc = { 92 .name = "max8997_pmic", 93 .type = POWER_SUPPLY_TYPE_BATTERY, 94 .get_property = max8997_battery_get_property, 95 .properties = max8997_battery_props, 96 .num_properties = ARRAY_SIZE(max8997_battery_props), 97 }; 98 99 static int max8997_battery_probe(struct platform_device *pdev) 100 { 101 int ret = 0; 102 struct charger_data *charger; 103 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent); 104 struct i2c_client *i2c = iodev->i2c; 105 struct max8997_platform_data *pdata = iodev->pdata; 106 struct power_supply_config psy_cfg = {}; 107 108 if (!pdata) { 109 dev_err(&pdev->dev, "No platform data supplied.\n"); 110 return -EINVAL; 111 } 112 113 if (pdata->eoc_mA) { 114 int val = (pdata->eoc_mA - 50) / 10; 115 if (val < 0) 116 val = 0; 117 if (val > 0xf) 118 val = 0xf; 119 120 ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL5, 121 val << ITOPOFF_SHIFT, ITOPOFF_MASK); 122 if (ret < 0) { 123 dev_err(&pdev->dev, "Cannot use i2c bus.\n"); 124 return ret; 125 } 126 } 127 switch (pdata->timeout) { 128 case 5: 129 ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, 130 0x2 << TFCH_SHIFT, TFCH_MASK); 131 break; 132 case 6: 133 ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, 134 0x3 << TFCH_SHIFT, TFCH_MASK); 135 break; 136 case 7: 137 ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, 138 0x4 << TFCH_SHIFT, TFCH_MASK); 139 break; 140 case 0: 141 ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, 142 0x7 << TFCH_SHIFT, TFCH_MASK); 143 break; 144 default: 145 dev_err(&pdev->dev, "incorrect timeout value (%d)\n", 146 pdata->timeout); 147 return -EINVAL; 148 } 149 if (ret < 0) { 150 dev_err(&pdev->dev, "Cannot use i2c bus.\n"); 151 return ret; 152 } 153 154 charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); 155 if (!charger) 156 return -ENOMEM; 157 158 platform_set_drvdata(pdev, charger); 159 160 charger->dev = &pdev->dev; 161 charger->iodev = iodev; 162 163 psy_cfg.drv_data = charger; 164 165 charger->battery = devm_power_supply_register(&pdev->dev, 166 &max8997_battery_desc, 167 &psy_cfg); 168 if (IS_ERR(charger->battery)) { 169 dev_err(&pdev->dev, "failed: power supply register\n"); 170 return PTR_ERR(charger->battery); 171 } 172 173 return 0; 174 } 175 176 static const struct platform_device_id max8997_battery_id[] = { 177 { "max8997-battery", 0 }, 178 { } 179 }; 180 MODULE_DEVICE_TABLE(platform, max8997_battery_id); 181 182 static struct platform_driver max8997_battery_driver = { 183 .driver = { 184 .name = "max8997-battery", 185 }, 186 .probe = max8997_battery_probe, 187 .id_table = max8997_battery_id, 188 }; 189 module_platform_driver(max8997_battery_driver); 190 191 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver"); 192 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 193 MODULE_LICENSE("GPL"); 194