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; |
| 33 | struct power_supply battery; |
| 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 | { |
| 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, ®); |
| 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, ®); |
| 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 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 78 | static int max8998_battery_probe(struct platform_device *pdev) |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 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 | |
Jingoo Han | acfbf47 | 2013-03-11 15:35:14 +0900 | [diff] [blame] | 91 | max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_battery_data), |
| 92 | GFP_KERNEL); |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 93 | if (!max8998) |
| 94 | return -ENOMEM; |
| 95 | |
| 96 | max8998->dev = &pdev->dev; |
| 97 | max8998->iodev = iodev; |
| 98 | platform_set_drvdata(pdev, max8998); |
| 99 | i2c = max8998->iodev->i2c; |
| 100 | |
| 101 | /* Setup "End of Charge" */ |
| 102 | /* If EOC value equals 0, |
| 103 | * remain value set from bootloader or default value */ |
| 104 | if (pdata->eoc >= 10 && pdata->eoc <= 45) { |
| 105 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, |
| 106 | (pdata->eoc / 5 - 2) << 5, 0x7 << 5); |
| 107 | } else if (pdata->eoc == 0) { |
| 108 | dev_dbg(max8998->dev, |
| 109 | "EOC value not set: leave it unchanged.\n"); |
| 110 | } else { |
| 111 | dev_err(max8998->dev, "Invalid EOC value\n"); |
| 112 | ret = -EINVAL; |
| 113 | goto err; |
| 114 | } |
| 115 | |
| 116 | /* Setup Charge Restart Level */ |
| 117 | switch (pdata->restart) { |
| 118 | case 100: |
| 119 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x1 << 3, 0x3 << 3); |
| 120 | break; |
| 121 | case 150: |
| 122 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x0 << 3, 0x3 << 3); |
| 123 | break; |
| 124 | case 200: |
| 125 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x2 << 3, 0x3 << 3); |
| 126 | break; |
| 127 | case -1: |
| 128 | max8998_update_reg(i2c, MAX8998_REG_CHGR1, 0x3 << 3, 0x3 << 3); |
| 129 | break; |
| 130 | case 0: |
| 131 | dev_dbg(max8998->dev, |
| 132 | "Restart Level not set: leave it unchanged.\n"); |
| 133 | break; |
| 134 | default: |
| 135 | dev_err(max8998->dev, "Invalid Restart Level\n"); |
| 136 | ret = -EINVAL; |
| 137 | goto err; |
| 138 | } |
| 139 | |
| 140 | /* Setup Charge Full Timeout */ |
| 141 | switch (pdata->timeout) { |
| 142 | case 5: |
| 143 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4); |
| 144 | break; |
| 145 | case 6: |
| 146 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4); |
| 147 | break; |
| 148 | case 7: |
| 149 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4); |
| 150 | break; |
| 151 | case -1: |
| 152 | max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4); |
| 153 | break; |
| 154 | case 0: |
| 155 | dev_dbg(max8998->dev, |
| 156 | "Full Timeout not set: leave it unchanged.\n"); |
Axel Lin | 45d116e | 2011-08-01 07:41:50 +0800 | [diff] [blame] | 157 | break; |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 158 | default: |
| 159 | dev_err(max8998->dev, "Invalid Full Timeout value\n"); |
| 160 | ret = -EINVAL; |
| 161 | goto err; |
| 162 | } |
| 163 | |
| 164 | max8998->battery.name = "max8998_pmic"; |
| 165 | max8998->battery.type = POWER_SUPPLY_TYPE_BATTERY; |
| 166 | max8998->battery.get_property = max8998_battery_get_property; |
| 167 | max8998->battery.properties = max8998_battery_props; |
| 168 | max8998->battery.num_properties = ARRAY_SIZE(max8998_battery_props); |
| 169 | |
| 170 | ret = power_supply_register(max8998->dev, &max8998->battery); |
| 171 | if (ret) { |
| 172 | dev_err(max8998->dev, "failed: power supply register\n"); |
| 173 | goto err; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | err: |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 178 | return ret; |
| 179 | } |
| 180 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 181 | static int max8998_battery_remove(struct platform_device *pdev) |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 182 | { |
| 183 | struct max8998_battery_data *max8998 = platform_get_drvdata(pdev); |
| 184 | |
| 185 | power_supply_unregister(&max8998->battery); |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static const struct platform_device_id max8998_battery_id[] = { |
| 191 | { "max8998-battery", TYPE_MAX8998 }, |
Axel Lin | c03bfab | 2011-08-01 07:23:00 +0800 | [diff] [blame] | 192 | { } |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static struct platform_driver max8998_battery_driver = { |
| 196 | .driver = { |
| 197 | .name = "max8998-battery", |
| 198 | .owner = THIS_MODULE, |
| 199 | }, |
| 200 | .probe = max8998_battery_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 201 | .remove = max8998_battery_remove, |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 202 | .id_table = max8998_battery_id, |
| 203 | }; |
| 204 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 205 | module_platform_driver(max8998_battery_driver); |
Donggeun Kim | bb4ce97 | 2011-06-24 19:04:18 +0900 | [diff] [blame] | 206 | |
| 207 | MODULE_DESCRIPTION("MAXIM 8998 battery control driver"); |
| 208 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 209 | MODULE_LICENSE("GPL"); |
| 210 | MODULE_ALIAS("platform:max8998-battery"); |