Krzysztof Kozlowski | 7b38ebd | 2018-08-07 18:21:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974 |
| 4 | // |
| 5 | // Copyright (C) 2009-2010 Samsung Electronics |
| 6 | // MyungJoo Ham <myungjoo.ham@samsung.com> |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 7 | |
| 8 | #include <linux/err.h> |
Randy Dunlap | d555ab6 | 2011-07-29 21:11:43 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 10 | #include <linux/mod_devicetable.h> |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/power_supply.h> |
| 14 | #include <linux/mfd/max8998.h> |
| 15 | #include <linux/mfd/max8998-private.h> |
| 16 | |
| 17 | struct max8998_battery_data { |
| 18 | struct device *dev; |
| 19 | struct max8998_dev *iodev; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 20 | struct power_supply *battery; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | static enum power_supply_property max8998_battery_props[] = { |
| 24 | POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */ |
| 25 | POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */ |
| 26 | }; |
| 27 | |
| 28 | /* Note that the charger control is done by a current regulator "CHARGER" */ |
| 29 | static int max8998_battery_get_property(struct power_supply *psy, |
| 30 | enum power_supply_property psp, |
| 31 | union power_supply_propval *val) |
| 32 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 33 | struct max8998_battery_data *max8998 = power_supply_get_drvdata(psy); |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 34 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 35 | int ret; |
| 36 | u8 reg; |
| 37 | |
| 38 | switch (psp) { |
| 39 | case POWER_SUPPLY_PROP_PRESENT: |
| 40 | ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®); |
| 41 | if (ret) |
| 42 | return ret; |
| 43 | if (reg & (1 << 4)) |
| 44 | val->intval = 0; |
| 45 | else |
| 46 | val->intval = 1; |
| 47 | break; |
| 48 | case POWER_SUPPLY_PROP_ONLINE: |
| 49 | ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, ®); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | if (reg & (1 << 3)) |
| 53 | val->intval = 0; |
| 54 | else |
| 55 | val->intval = 1; |
| 56 | break; |
| 57 | default: |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 64 | static const struct power_supply_desc max8998_battery_desc = { |
| 65 | .name = "max8998_pmic", |
| 66 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 67 | .get_property = max8998_battery_get_property, |
| 68 | .properties = max8998_battery_props, |
| 69 | .num_properties = ARRAY_SIZE(max8998_battery_props), |
| 70 | }; |
| 71 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 72 | static int max8998_battery_probe(struct platform_device *pdev) |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 73 | { |
| 74 | struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
Tomasz Figa | cb90a2c | 2018-07-17 18:05:07 +0200 | [diff] [blame] | 75 | struct max8998_platform_data *pdata = iodev->pdata; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 76 | struct power_supply_config psy_cfg = {}; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 77 | struct max8998_battery_data *max8998; |
| 78 | struct i2c_client *i2c; |
| 79 | int ret = 0; |
| 80 | |
| 81 | if (!pdata) { |
| 82 | dev_err(pdev->dev.parent, "No platform init data supplied\n"); |
| 83 | return -ENODEV; |
| 84 | } |
| 85 | |
Jingoo Han | acfbf47 | 2013-03-11 15:35:14 +0900 | [diff] [blame] | 86 | max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_battery_data), |
| 87 | GFP_KERNEL); |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 88 | if (!max8998) |
| 89 | return -ENOMEM; |
| 90 | |
| 91 | max8998->dev = &pdev->dev; |
| 92 | max8998->iodev = iodev; |
| 93 | platform_set_drvdata(pdev, max8998); |
| 94 | i2c = max8998->iodev->i2c; |
| 95 | |
| 96 | /* Setup "End of Charge" */ |
| 97 | /* If EOC value equals 0, |
| 98 | * remain value set from bootloader or default value */ |
| 99 | if (pdata->eoc >= 10 && pdata->eoc <= 45) { |
| 100 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, |
| 101 | (pdata->eoc / 5 - 2) << 5, 0x7 << 5); |
| 102 | } else if (pdata->eoc == 0) { |
| 103 | dev_dbg(max8998->dev, |
| 104 | "EOC value not set: leave it unchanged.\n"); |
| 105 | } else { |
| 106 | dev_err(max8998->dev, "Invalid EOC value\n"); |
Vaishali Thakkar | 5e5822f | 2015-08-13 10:24:41 +0530 | [diff] [blame] | 107 | return -EINVAL; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Setup Charge Restart Level */ |
| 111 | switch (pdata->restart) { |
| 112 | case 100: |
| 113 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3); |
| 114 | break; |
| 115 | case 150: |
| 116 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3); |
| 117 | break; |
| 118 | case 200: |
| 119 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3); |
| 120 | break; |
| 121 | case -1: |
| 122 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3); |
| 123 | break; |
| 124 | case 0: |
| 125 | dev_dbg(max8998->dev, |
| 126 | "Restart Level not set: leave it unchanged.\n"); |
| 127 | break; |
| 128 | default: |
| 129 | dev_err(max8998->dev, "Invalid Restart Level\n"); |
Vaishali Thakkar | 5e5822f | 2015-08-13 10:24:41 +0530 | [diff] [blame] | 130 | return -EINVAL; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* Setup Charge Full Timeout */ |
| 134 | switch (pdata->timeout) { |
| 135 | case 5: |
| 136 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4); |
| 137 | break; |
| 138 | case 6: |
| 139 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4); |
| 140 | break; |
| 141 | case 7: |
| 142 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4); |
| 143 | break; |
| 144 | case -1: |
| 145 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4); |
| 146 | break; |
| 147 | case 0: |
| 148 | dev_dbg(max8998->dev, |
| 149 | "Full Timeout not set: leave it unchanged.\n"); |
Axel Lin | 45d116e | 2011-08-01 07:41:50 +0800 | [diff] [blame] | 150 | break; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 151 | default: |
| 152 | dev_err(max8998->dev, "Invalid Full Timeout value\n"); |
Vaishali Thakkar | 5e5822f | 2015-08-13 10:24:41 +0530 | [diff] [blame] | 153 | return -EINVAL; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 156 | psy_cfg.drv_data = max8998; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 157 | |
Vaishali Thakkar | 5e5822f | 2015-08-13 10:24:41 +0530 | [diff] [blame] | 158 | max8998->battery = devm_power_supply_register(max8998->dev, |
| 159 | &max8998_battery_desc, |
| 160 | &psy_cfg); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 161 | if (IS_ERR(max8998->battery)) { |
| 162 | ret = PTR_ERR(max8998->battery); |
| 163 | dev_err(max8998->dev, "failed: power supply register: %d\n", |
| 164 | ret); |
Vaishali Thakkar | 5e5822f | 2015-08-13 10:24:41 +0530 | [diff] [blame] | 165 | return ret; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return 0; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static const struct platform_device_id max8998_battery_id[] = { |
| 172 | { "max8998-battery", TYPE_MAX8998 }, |
Axel Lin | c03bfab | 2011-08-01 07:23:00 +0800 | [diff] [blame] | 173 | { } |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static struct platform_driver max8998_battery_driver = { |
| 177 | .driver = { |
| 178 | .name = "max8998-battery", |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 179 | }, |
| 180 | .probe = max8998_battery_probe, |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 181 | .id_table = max8998_battery_id, |
| 182 | }; |
| 183 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 184 | module_platform_driver(max8998_battery_driver); |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 185 | |
| 186 | MODULE_DESCRIPTION("MAXIM 8998 battery control driver"); |
| 187 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 188 | MODULE_LICENSE("GPL"); |
| 189 | MODULE_ALIAS("platform:max8998-battery"); |