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 i2c_client *i2c = iodev->i2c;
172 	struct max8997_platform_data *pdata = iodev->pdata;
173 	struct power_supply_config psy_cfg = {};
174 
175 	if (!pdata) {
176 		dev_err(&pdev->dev, "No platform data supplied.\n");
177 		return -EINVAL;
178 	}
179 
180 	if (pdata->eoc_mA) {
181 		int val = (pdata->eoc_mA - 50) / 10;
182 		if (val < 0)
183 			val = 0;
184 		if (val > 0xf)
185 			val = 0xf;
186 
187 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL5,
188 				val << ITOPOFF_SHIFT, ITOPOFF_MASK);
189 		if (ret < 0) {
190 			dev_err(&pdev->dev, "Cannot use i2c bus.\n");
191 			return ret;
192 		}
193 	}
194 	switch (pdata->timeout) {
195 	case 5:
196 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
197 				0x2 << TFCH_SHIFT, TFCH_MASK);
198 		break;
199 	case 6:
200 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
201 				0x3 << TFCH_SHIFT, TFCH_MASK);
202 		break;
203 	case 7:
204 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
205 				0x4 << TFCH_SHIFT, TFCH_MASK);
206 		break;
207 	case 0:
208 		ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1,
209 				0x7 << TFCH_SHIFT, TFCH_MASK);
210 		break;
211 	default:
212 		dev_err(&pdev->dev, "incorrect timeout value (%d)\n",
213 				pdata->timeout);
214 		return -EINVAL;
215 	}
216 	if (ret < 0) {
217 		dev_err(&pdev->dev, "Cannot use i2c bus.\n");
218 		return ret;
219 	}
220 
221 	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
222 	if (!charger)
223 		return -ENOMEM;
224 
225 	platform_set_drvdata(pdev, charger);
226 
227 	charger->dev = &pdev->dev;
228 	charger->iodev = iodev;
229 
230 	psy_cfg.drv_data = charger;
231 
232 	charger->battery = devm_power_supply_register(&pdev->dev,
233 						 &max8997_battery_desc,
234 						 &psy_cfg);
235 	if (IS_ERR(charger->battery)) {
236 		dev_err(&pdev->dev, "failed: power supply register\n");
237 		return PTR_ERR(charger->battery);
238 	}
239 
240 	charger->reg = devm_regulator_get_optional(&pdev->dev, "charger");
241 	if (IS_ERR(charger->reg)) {
242 		if (PTR_ERR(charger->reg) == -EPROBE_DEFER)
243 			return -EPROBE_DEFER;
244 		dev_info(&pdev->dev, "couldn't get charger regulator\n");
245 	}
246 	charger->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
247 	if (IS_ERR(charger->edev)) {
248 		if (PTR_ERR(charger->edev) == -EPROBE_DEFER)
249 			return -EPROBE_DEFER;
250 		dev_info(charger->dev, "couldn't get extcon device\n");
251 	}
252 
253 	if (!IS_ERR(charger->reg) && !IS_ERR(charger->edev)) {
254 		INIT_WORK(&charger->extcon_work, max8997_battery_extcon_evt_worker);
255 		ret = devm_add_action(&pdev->dev, max8997_battery_extcon_evt_stop_work, charger);
256 		if (ret) {
257 			dev_err(&pdev->dev, "failed to add extcon evt stop action: %d\n", ret);
258 			return ret;
259 		}
260 		charger->extcon_nb.notifier_call = max8997_battery_extcon_evt;
261 		ret = devm_extcon_register_notifier_all(&pdev->dev, charger->edev,
262 							&charger->extcon_nb);
263 		if (ret) {
264 			dev_err(&pdev->dev, "failed to register extcon notifier\n");
265 			return ret;
266 		};
267 	}
268 
269 	return 0;
270 }
271 
272 static const struct platform_device_id max8997_battery_id[] = {
273 	{ "max8997-battery", 0 },
274 	{ }
275 };
276 MODULE_DEVICE_TABLE(platform, max8997_battery_id);
277 
278 static struct platform_driver max8997_battery_driver = {
279 	.driver = {
280 		.name = "max8997-battery",
281 	},
282 	.probe = max8997_battery_probe,
283 	.id_table = max8997_battery_id,
284 };
285 module_platform_driver(max8997_battery_driver);
286 
287 MODULE_DESCRIPTION("MAXIM 8997/8966 battery control driver");
288 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
289 MODULE_LICENSE("GPL");
290