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