xref: /linux/drivers/power/supply/max8997_charger.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
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/extcon.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/mfd/max8997.h>
15 #include <linux/mfd/max8997-private.h>
16 #include <linux/regulator/consumer.h>
17 
18 /* MAX8997_REG_STATUS4 */
19 #define DCINOK_SHIFT		1
20 #define DCINOK_MASK		(1 << DCINOK_SHIFT)
21 #define DETBAT_SHIFT		2
22 #define DETBAT_MASK		(1 << DETBAT_SHIFT)
23 
24 /* MAX8997_REG_MBCCTRL1 */
25 #define TFCH_SHIFT		4
26 #define TFCH_MASK		(7 << TFCH_SHIFT)
27 
28 /* MAX8997_REG_MBCCTRL5 */
29 #define ITOPOFF_SHIFT		0
30 #define ITOPOFF_MASK		(0xF << ITOPOFF_SHIFT)
31 
32 struct charger_data {
33 	struct device *dev;
34 	struct max8997_dev *iodev;
35 	struct power_supply *battery;
36 	struct regulator *reg;
37 	struct extcon_dev *edev;
38 	struct notifier_block extcon_nb;
39 	struct work_struct extcon_work;
40 };
41 
42 static enum power_supply_property max8997_battery_props[] = {
43 	POWER_SUPPLY_PROP_STATUS, /* "FULL", "CHARGING" or "DISCHARGING". */
44 	POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */
45 	POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */
46 };
47 
48 /* Note that the charger control is done by a current regulator "CHARGER" */
49 static int max8997_battery_get_property(struct power_supply *psy,
50 		enum power_supply_property psp,
51 		union power_supply_propval *val)
52 {
53 	struct charger_data *charger = power_supply_get_drvdata(psy);
54 	struct i2c_client *i2c = charger->iodev->i2c;
55 	int ret;
56 	u8 reg;
57 
58 	switch (psp) {
59 	case POWER_SUPPLY_PROP_STATUS:
60 		val->intval = 0;
61 		ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
62 		if (ret)
63 			return ret;
64 		if ((reg & (1 << 0)) == 0x1)
65 			val->intval = POWER_SUPPLY_STATUS_FULL;
66 		else if ((reg & DCINOK_MASK))
67 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
68 		else
69 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
70 
71 		break;
72 	case POWER_SUPPLY_PROP_PRESENT:
73 		val->intval = 0;
74 		ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
75 		if (ret)
76 			return ret;
77 		if ((reg & DETBAT_MASK) == 0x0)
78 			val->intval = 1;
79 
80 		break;
81 	case POWER_SUPPLY_PROP_ONLINE:
82 		val->intval = 0;
83 		ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, &reg);
84 		if (ret)
85 			return ret;
86 		if (reg & DCINOK_MASK)
87 			val->intval = 1;
88 
89 		break;
90 	default:
91 		return -EINVAL;
92 	}
93 
94 	return 0;
95 }
96 
97 static void max8997_battery_extcon_evt_stop_work(void *data)
98 {
99 	struct charger_data *charger = data;
100 
101 	cancel_work_sync(&charger->extcon_work);
102 }
103 
104 static void max8997_battery_extcon_evt_worker(struct work_struct *work)
105 {
106 	struct charger_data *charger =
107 	    container_of(work, struct charger_data, extcon_work);
108 	struct extcon_dev *edev = charger->edev;
109 	int current_limit;
110 
111 	if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
112 		dev_dbg(charger->dev, "USB SDP charger is connected\n");
113 		current_limit = 450000;
114 	} else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
115 		dev_dbg(charger->dev, "USB DCP charger is connected\n");
116 		current_limit = 650000;
117 	} else if (extcon_get_state(edev, EXTCON_CHG_USB_FAST) > 0) {
118 		dev_dbg(charger->dev, "USB FAST charger is connected\n");
119 		current_limit = 650000;
120 	} else if (extcon_get_state(edev, EXTCON_CHG_USB_SLOW) > 0) {
121 		dev_dbg(charger->dev, "USB SLOW charger is connected\n");
122 		current_limit = 650000;
123 	} else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
124 		dev_dbg(charger->dev, "USB CDP charger is connected\n");
125 		current_limit = 650000;
126 	} else {
127 		dev_dbg(charger->dev, "USB charger is disconnected\n");
128 		current_limit = -1;
129 	}
130 
131 	if (current_limit > 0) {
132 		int ret = regulator_set_current_limit(charger->reg, current_limit, current_limit);
133 
134 		if (ret) {
135 			dev_err(charger->dev, "failed to set current limit: %d\n", ret);
136 			return;
137 		}
138 		ret = regulator_enable(charger->reg);
139 		if (ret)
140 			dev_err(charger->dev, "failed to enable regulator: %d\n", ret);
141 	} else {
142 		int ret  = regulator_disable(charger->reg);
143 
144 		if (ret)
145 			dev_err(charger->dev, "failed to disable regulator: %d\n", ret);
146 	}
147 }
148 
149 static int max8997_battery_extcon_evt(struct notifier_block *nb,
150 				unsigned long event, void *param)
151 {
152 	struct charger_data *charger =
153 		container_of(nb, struct charger_data, extcon_nb);
154 	schedule_work(&charger->extcon_work);
155 	return NOTIFY_OK;
156 }
157 
158 static const struct power_supply_desc max8997_battery_desc = {
159 	.name		= "max8997_pmic",
160 	.type		= POWER_SUPPLY_TYPE_BATTERY,
161 	.get_property	= max8997_battery_get_property,
162 	.properties	= max8997_battery_props,
163 	.num_properties	= ARRAY_SIZE(max8997_battery_props),
164 };
165 
166 static int max8997_battery_probe(struct platform_device *pdev)
167 {
168 	int ret = 0;
169 	struct charger_data *charger;
170 	struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
171 	struct device_node *np = pdev->dev.of_node;
172 	struct i2c_client *i2c = iodev->i2c;
173 	struct max8997_platform_data *pdata = iodev->pdata;
174 	struct power_supply_config psy_cfg = {};
175 
176 	if (!pdata) {
177 		dev_err(&pdev->dev, "No platform data supplied.\n");
178 		return -EINVAL;
179 	}
180 
181 	if (pdata->eoc_mA) {
182 		int val = (pdata->eoc_mA - 50) / 10;
183 		if (val < 0)
184 			val = 0;
185 		if (val > 0xf)
186 			val = 0xf;
187 
188 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL5,
189 				val << ITOPOFF_SHIFT, ITOPOFF_MASK);
190 		if (ret < 0) {
191 			dev_err(&pdev->dev, "Cannot use i2c bus.\n");
192 			return ret;
193 		}
194 	}
195 	switch (pdata->timeout) {
196 	case 5:
197 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
198 				0x2 << TFCH_SHIFT, TFCH_MASK);
199 		break;
200 	case 6:
201 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
202 				0x3 << TFCH_SHIFT, TFCH_MASK);
203 		break;
204 	case 7:
205 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
206 				0x4 << TFCH_SHIFT, TFCH_MASK);
207 		break;
208 	case 0:
209 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
210 				0x7 << TFCH_SHIFT, TFCH_MASK);
211 		break;
212 	default:
213 		dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
214 				pdata->timeout);
215 		return -EINVAL;
216 	}
217 	if (ret < 0) {
218 		dev_err(&pdev->dev, "Cannot use i2c bus.\n");
219 		return ret;
220 	}
221 
222 	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
223 	if (!charger)
224 		return -ENOMEM;
225 
226 	platform_set_drvdata(pdev, charger);
227 
228 	charger->dev = &pdev->dev;
229 	charger->iodev = iodev;
230 
231 	psy_cfg.drv_data = charger;
232 
233 	charger->battery = devm_power_supply_register(&pdev->dev,
234 						 &max8997_battery_desc,
235 						 &psy_cfg);
236 	if (IS_ERR(charger->battery)) {
237 		dev_err(&pdev->dev, "failed: power supply register\n");
238 		return PTR_ERR(charger->battery);
239 	}
240 
241 	// grab regulator from parent device's node
242 	pdev->dev.of_node = iodev->dev->of_node;
243 	charger->reg = devm_regulator_get_optional(&pdev->dev, "charger");
244 	pdev->dev.of_node = np;
245 	if (IS_ERR(charger->reg)) {
246 		if (PTR_ERR(charger->reg) == -EPROBE_DEFER)
247 			return -EPROBE_DEFER;
248 		dev_info(&pdev->dev, "couldn't get charger regulator\n");
249 	}
250 	charger->edev = extcon_get_extcon_dev("max8997-muic");
251 	if (IS_ERR_OR_NULL(charger->edev)) {
252 		if (!charger->edev)
253 			return -EPROBE_DEFER;
254 		dev_info(charger->dev, "couldn't get extcon device\n");
255 	}
256 
257 	if (!IS_ERR(charger->reg) && !IS_ERR_OR_NULL(charger->edev)) {
258 		INIT_WORK(&charger->extcon_work, max8997_battery_extcon_evt_worker);
259 		ret = devm_add_action(&pdev->dev, max8997_battery_extcon_evt_stop_work, charger);
260 		if (ret) {
261 			dev_err(&pdev->dev, "failed to add extcon evt stop action: %d\n", ret);
262 			return ret;
263 		}
264 		charger->extcon_nb.notifier_call = max8997_battery_extcon_evt;
265 		ret = devm_extcon_register_notifier_all(&pdev->dev, charger->edev,
266 							&charger->extcon_nb);
267 		if (ret) {
268 			dev_err(&pdev->dev, "failed to register extcon notifier\n");
269 			return ret;
270 		}
271 	}
272 
273 	return 0;
274 }
275 
276 static const struct platform_device_id max8997_battery_id[] = {
277 	{ "max8997-battery", 0 },
278 	{ }
279 };
280 MODULE_DEVICE_TABLE(platform, max8997_battery_id);
281 
282 static struct platform_driver max8997_battery_driver = {
283 	.driver = {
284 		.name = "max8997-battery",
285 	},
286 	.probe = max8997_battery_probe,
287 	.id_table = max8997_battery_id,
288 };
289 module_platform_driver(max8997_battery_driver);
290 
291 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
292 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
293 MODULE_LICENSE("GPL");
294