Linux 4.19.133
[linux/fpc-iii.git] / drivers / power / supply / max8998_charger.c
blobaa65e6c36c55eb7d3e03629032a817bfe147e804
1 /*
2 * max8998_charger.c - Power supply consumer driver for the Maxim 8998/LP3974
4 * Copyright (C) 2009-2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
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.
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.
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
22 #include <linux/err.h>
23 #include <linux/module.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/slab.h>
26 #include <linux/platform_device.h>
27 #include <linux/power_supply.h>
28 #include <linux/mfd/max8998.h>
29 #include <linux/mfd/max8998-private.h>
31 struct max8998_battery_data {
32 struct device *dev;
33 struct max8998_dev *iodev;
34 struct power_supply *battery;
37 static enum power_supply_property max8998_battery_props[] = {
38 POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
39 POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
42 /* Note that the charger control is done by a current regulator "CHARGER" */
43 static int max8998_battery_get_property(struct power_supply *psy,
44 enum power_supply_property psp,
45 union power_supply_propval *val)
47 struct max8998_battery_data *max8998 = power_supply_get_drvdata(psy);
48 struct i2c_client *i2c = max8998->iodev->i2c;
49 int ret;
50 u8 reg;
52 switch (psp) {
53 case POWER_SUPPLY_PROP_PRESENT:
54 ret = max8998_read_reg(i2c, MAX8998_REG_STATUS2, &reg);
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, &reg);
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;
75 return 0;
78 static const struct power_supply_desc max8998_battery_desc = {
79 .name = "max8998_pmic",
80 .type = POWER_SUPPLY_TYPE_BATTERY,
81 .get_property = max8998_battery_get_property,
82 .properties = max8998_battery_props,
83 .num_properties = ARRAY_SIZE(max8998_battery_props),
86 static int max8998_battery_probe(struct platform_device *pdev)
88 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
89 struct max8998_platform_data *pdata = iodev->pdata;
90 struct power_supply_config psy_cfg = {};
91 struct max8998_battery_data *max8998;
92 struct i2c_client *i2c;
93 int ret = 0;
95 if (!pdata) {
96 dev_err(pdev->dev.parent, "No platform init data supplied\n");
97 return -ENODEV;
100 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_battery_data),
101 GFP_KERNEL);
102 if (!max8998)
103 return -ENOMEM;
105 max8998->dev = &pdev->dev;
106 max8998->iodev = iodev;
107 platform_set_drvdata(pdev, max8998);
108 i2c = max8998->iodev->i2c;
110 /* Setup "End of Charge" */
111 /* If EOC value equals 0,
112 * remain value set from bootloader or default value */
113 if (pdata->eoc >= 10 && pdata->eoc <= 45) {
114 max8998_update_reg(i2c, MAX8998_REG_CHGR1,
115 (pdata->eoc / 5 - 2) << 5, 0x7 << 5);
116 } else if (pdata->eoc == 0) {
117 dev_dbg(max8998->dev,
118 "EOC value not set: leave it unchanged.\n");
119 } else {
120 dev_err(max8998->dev, "Invalid EOC value\n");
121 return -EINVAL;
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 return -EINVAL;
147 /* Setup Charge Full Timeout */
148 switch (pdata->timeout) {
149 case 5:
150 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x0 << 4, 0x3 << 4);
151 break;
152 case 6:
153 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x1 << 4, 0x3 << 4);
154 break;
155 case 7:
156 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x2 << 4, 0x3 << 4);
157 break;
158 case -1:
159 max8998_update_reg(i2c, MAX8998_REG_CHGR2, 0x3 << 4, 0x3 << 4);
160 break;
161 case 0:
162 dev_dbg(max8998->dev,
163 "Full Timeout not set: leave it unchanged.\n");
164 break;
165 default:
166 dev_err(max8998->dev, "Invalid Full Timeout value\n");
167 return -EINVAL;
170 psy_cfg.drv_data = max8998;
172 max8998->battery = devm_power_supply_register(max8998->dev,
173 &max8998_battery_desc,
174 &psy_cfg);
175 if (IS_ERR(max8998->battery)) {
176 ret = PTR_ERR(max8998->battery);
177 dev_err(max8998->dev, "failed: power supply register: %d\n",
178 ret);
179 return ret;
182 return 0;
185 static const struct platform_device_id max8998_battery_id[] = {
186 { "max8998-battery", TYPE_MAX8998 },
190 static struct platform_driver max8998_battery_driver = {
191 .driver = {
192 .name = "max8998-battery",
194 .probe = max8998_battery_probe,
195 .id_table = max8998_battery_id,
198 module_platform_driver(max8998_battery_driver);
200 MODULE_DESCRIPTION("MAXIM 8998 battery control driver");
201 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
202 MODULE_LICENSE("GPL");
203 MODULE_ALIAS("platform:max8998-battery");