1 // SPDX-License-Identifier: GPL-2.0
2 //
3 //  max17040_battery.c
4 //  fuel-gauge systems for lithium-ion (Li+) batteries
5 //
6 //  Copyright (C) 2009 Samsung Electronics
7 //  Minkyu Kang <mk7.kang@samsung.com>
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/mutex.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/power_supply.h>
18 #include <linux/max17040_battery.h>
19 #include <linux/slab.h>
20 
21 #define MAX17040_VCELL	0x02
22 #define MAX17040_SOC	0x04
23 #define MAX17040_MODE	0x06
24 #define MAX17040_VER	0x08
25 #define MAX17040_RCOMP	0x0C
26 #define MAX17040_CMD	0xFE
27 
28 
29 #define MAX17040_DELAY		1000
30 #define MAX17040_BATTERY_FULL	95
31 
32 #define MAX17040_ATHD_MASK		0xFFC0
33 #define MAX17040_ATHD_DEFAULT_POWER_UP	4
34 
35 struct max17040_chip {
36 	struct i2c_client		*client;
37 	struct delayed_work		work;
38 	struct power_supply		*battery;
39 	struct max17040_platform_data	*pdata;
40 
41 	/* State Of Connect */
42 	int online;
43 	/* battery voltage */
44 	int vcell;
45 	/* battery capacity */
46 	int soc;
47 	/* State Of Charge */
48 	int status;
49 	/* Low alert threshold from 32% to 1% of the State of Charge */
50 	u32 low_soc_alert;
51 };
52 
53 static int max17040_get_property(struct power_supply *psy,
54 			    enum power_supply_property psp,
55 			    union power_supply_propval *val)
56 {
57 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
58 
59 	switch (psp) {
60 	case POWER_SUPPLY_PROP_STATUS:
61 		val->intval = chip->status;
62 		break;
63 	case POWER_SUPPLY_PROP_ONLINE:
64 		val->intval = chip->online;
65 		break;
66 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
67 		val->intval = chip->vcell;
68 		break;
69 	case POWER_SUPPLY_PROP_CAPACITY:
70 		val->intval = chip->soc;
71 		break;
72 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
73 		val->intval = chip->low_soc_alert;
74 		break;
75 	default:
76 		return -EINVAL;
77 	}
78 	return 0;
79 }
80 
81 static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
82 {
83 	int ret;
84 
85 	ret = i2c_smbus_write_word_swapped(client, reg, value);
86 
87 	if (ret < 0)
88 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
89 
90 	return ret;
91 }
92 
93 static int max17040_read_reg(struct i2c_client *client, int reg)
94 {
95 	int ret;
96 
97 	ret = i2c_smbus_read_word_swapped(client, reg);
98 
99 	if (ret < 0)
100 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
101 
102 	return ret;
103 }
104 
105 static void max17040_reset(struct i2c_client *client)
106 {
107 	max17040_write_reg(client, MAX17040_CMD, 0x0054);
108 }
109 
110 static int max17040_set_low_soc_alert(struct i2c_client *client, u32 level)
111 {
112 	int ret;
113 	u16 data;
114 
115 	level = 32 - level;
116 	data = max17040_read_reg(client, MAX17040_RCOMP);
117 	/* clear the alrt bit and set LSb 5 bits */
118 	data &= MAX17040_ATHD_MASK;
119 	data |= level;
120 	ret = max17040_write_reg(client, MAX17040_RCOMP, data);
121 
122 	return ret;
123 }
124 
125 static void max17040_get_vcell(struct i2c_client *client)
126 {
127 	struct max17040_chip *chip = i2c_get_clientdata(client);
128 	u16 vcell;
129 
130 	vcell = max17040_read_reg(client, MAX17040_VCELL);
131 
132 	chip->vcell = (vcell >> 4) * 1250;
133 }
134 
135 static void max17040_get_soc(struct i2c_client *client)
136 {
137 	struct max17040_chip *chip = i2c_get_clientdata(client);
138 	u16 soc;
139 
140 	soc = max17040_read_reg(client, MAX17040_SOC);
141 
142 	chip->soc = (soc >> 8);
143 }
144 
145 static void max17040_get_version(struct i2c_client *client)
146 {
147 	u16 version;
148 
149 	version = max17040_read_reg(client, MAX17040_VER);
150 
151 	dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
152 }
153 
154 static void max17040_get_online(struct i2c_client *client)
155 {
156 	struct max17040_chip *chip = i2c_get_clientdata(client);
157 
158 	if (chip->pdata && chip->pdata->battery_online)
159 		chip->online = chip->pdata->battery_online();
160 	else
161 		chip->online = 1;
162 }
163 
164 static void max17040_get_status(struct i2c_client *client)
165 {
166 	struct max17040_chip *chip = i2c_get_clientdata(client);
167 
168 	if (!chip->pdata || !chip->pdata->charger_online
169 			|| !chip->pdata->charger_enable) {
170 		chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
171 		return;
172 	}
173 
174 	if (chip->pdata->charger_online()) {
175 		if (chip->pdata->charger_enable())
176 			chip->status = POWER_SUPPLY_STATUS_CHARGING;
177 		else
178 			chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
179 	} else {
180 		chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
181 	}
182 
183 	if (chip->soc > MAX17040_BATTERY_FULL)
184 		chip->status = POWER_SUPPLY_STATUS_FULL;
185 }
186 
187 static int max17040_get_of_data(struct max17040_chip *chip)
188 {
189 	struct device *dev = &chip->client->dev;
190 
191 	chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
192 	device_property_read_u32(dev,
193 				 "maxim,alert-low-soc-level",
194 				 &chip->low_soc_alert);
195 
196 	if (chip->low_soc_alert <= 0 || chip->low_soc_alert >= 33)
197 		return -EINVAL;
198 
199 	return 0;
200 }
201 
202 static void max17040_check_changes(struct i2c_client *client)
203 {
204 	max17040_get_vcell(client);
205 	max17040_get_soc(client);
206 	max17040_get_online(client);
207 	max17040_get_status(client);
208 }
209 
210 static void max17040_work(struct work_struct *work)
211 {
212 	struct max17040_chip *chip;
213 	int last_soc, last_status;
214 
215 	chip = container_of(work, struct max17040_chip, work.work);
216 
217 	/* store SOC and status to check changes */
218 	last_soc = chip->soc;
219 	last_status = chip->status;
220 	max17040_check_changes(chip->client);
221 
222 	/* check changes and send uevent */
223 	if (last_soc != chip->soc || last_status != chip->status)
224 		power_supply_changed(chip->battery);
225 
226 	queue_delayed_work(system_power_efficient_wq, &chip->work,
227 			   MAX17040_DELAY);
228 }
229 
230 static irqreturn_t max17040_thread_handler(int id, void *dev)
231 {
232 	struct max17040_chip *chip = dev;
233 	struct i2c_client *client = chip->client;
234 
235 	dev_warn(&client->dev, "IRQ: Alert battery low level");
236 	/* read registers */
237 	max17040_check_changes(chip->client);
238 
239 	/* send uevent */
240 	power_supply_changed(chip->battery);
241 
242 	/* reset alert bit */
243 	max17040_set_low_soc_alert(client, chip->low_soc_alert);
244 
245 	return IRQ_HANDLED;
246 }
247 
248 static int max17040_enable_alert_irq(struct max17040_chip *chip)
249 {
250 	struct i2c_client *client = chip->client;
251 	unsigned int flags;
252 	int ret;
253 
254 	flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
255 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
256 					max17040_thread_handler, flags,
257 					chip->battery->desc->name, chip);
258 
259 	return ret;
260 }
261 
262 static int max17040_prop_writeable(struct power_supply *psy,
263 				   enum power_supply_property psp)
264 {
265 	switch (psp) {
266 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
267 		return 1;
268 	default:
269 		return 0;
270 	}
271 }
272 
273 static int max17040_set_property(struct power_supply *psy,
274 			    enum power_supply_property psp,
275 			    const union power_supply_propval *val)
276 {
277 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
278 	int ret;
279 
280 	switch (psp) {
281 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
282 		/* alert threshold can be programmed from 1% up to 32% */
283 		if ((val->intval < 1) || (val->intval > 32)) {
284 			ret = -EINVAL;
285 			break;
286 		}
287 		ret = max17040_set_low_soc_alert(chip->client, val->intval);
288 		chip->low_soc_alert = val->intval;
289 		break;
290 	default:
291 		ret = -EINVAL;
292 	}
293 
294 	return ret;
295 }
296 
297 static enum power_supply_property max17040_battery_props[] = {
298 	POWER_SUPPLY_PROP_STATUS,
299 	POWER_SUPPLY_PROP_ONLINE,
300 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
301 	POWER_SUPPLY_PROP_CAPACITY,
302 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
303 };
304 
305 static const struct power_supply_desc max17040_battery_desc = {
306 	.name			= "battery",
307 	.type			= POWER_SUPPLY_TYPE_BATTERY,
308 	.get_property		= max17040_get_property,
309 	.set_property		= max17040_set_property,
310 	.property_is_writeable  = max17040_prop_writeable,
311 	.properties		= max17040_battery_props,
312 	.num_properties		= ARRAY_SIZE(max17040_battery_props),
313 };
314 
315 static int max17040_probe(struct i2c_client *client,
316 			const struct i2c_device_id *id)
317 {
318 	struct i2c_adapter *adapter = client->adapter;
319 	struct power_supply_config psy_cfg = {};
320 	struct max17040_chip *chip;
321 	int ret;
322 
323 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
324 		return -EIO;
325 
326 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
327 	if (!chip)
328 		return -ENOMEM;
329 
330 	chip->client = client;
331 	chip->pdata = client->dev.platform_data;
332 	ret = max17040_get_of_data(chip);
333 	if (ret) {
334 		dev_err(&client->dev,
335 			"failed: low SOC alert OF data out of bounds\n");
336 		return ret;
337 	}
338 
339 	i2c_set_clientdata(client, chip);
340 	psy_cfg.drv_data = chip;
341 
342 	chip->battery = power_supply_register(&client->dev,
343 				&max17040_battery_desc, &psy_cfg);
344 	if (IS_ERR(chip->battery)) {
345 		dev_err(&client->dev, "failed: power supply register\n");
346 		return PTR_ERR(chip->battery);
347 	}
348 
349 	max17040_reset(client);
350 	max17040_get_version(client);
351 
352 	/* check interrupt */
353 	if (client->irq && of_device_is_compatible(client->dev.of_node,
354 						   "maxim,max77836-battery")) {
355 		ret = max17040_set_low_soc_alert(client, chip->low_soc_alert);
356 		if (ret) {
357 			dev_err(&client->dev,
358 				"Failed to set low SOC alert: err %d\n", ret);
359 			return ret;
360 		}
361 
362 		ret = max17040_enable_alert_irq(chip);
363 		if (ret) {
364 			client->irq = 0;
365 			dev_warn(&client->dev,
366 				 "Failed to get IRQ err %d\n", ret);
367 		}
368 	}
369 
370 	INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
371 	queue_delayed_work(system_power_efficient_wq, &chip->work,
372 			   MAX17040_DELAY);
373 
374 	return 0;
375 }
376 
377 static int max17040_remove(struct i2c_client *client)
378 {
379 	struct max17040_chip *chip = i2c_get_clientdata(client);
380 
381 	power_supply_unregister(chip->battery);
382 	cancel_delayed_work(&chip->work);
383 	return 0;
384 }
385 
386 #ifdef CONFIG_PM_SLEEP
387 
388 static int max17040_suspend(struct device *dev)
389 {
390 	struct i2c_client *client = to_i2c_client(dev);
391 	struct max17040_chip *chip = i2c_get_clientdata(client);
392 
393 	cancel_delayed_work(&chip->work);
394 
395 	if (client->irq && device_may_wakeup(dev))
396 		enable_irq_wake(client->irq);
397 
398 	return 0;
399 }
400 
401 static int max17040_resume(struct device *dev)
402 {
403 	struct i2c_client *client = to_i2c_client(dev);
404 	struct max17040_chip *chip = i2c_get_clientdata(client);
405 
406 	queue_delayed_work(system_power_efficient_wq, &chip->work,
407 			   MAX17040_DELAY);
408 
409 	if (client->irq && device_may_wakeup(dev))
410 		disable_irq_wake(client->irq);
411 
412 	return 0;
413 }
414 
415 static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
416 #define MAX17040_PM_OPS (&max17040_pm_ops)
417 
418 #else
419 
420 #define MAX17040_PM_OPS NULL
421 
422 #endif /* CONFIG_PM_SLEEP */
423 
424 static const struct i2c_device_id max17040_id[] = {
425 	{ "max17040" },
426 	{ "max77836-battery" },
427 	{ }
428 };
429 MODULE_DEVICE_TABLE(i2c, max17040_id);
430 
431 static const struct of_device_id max17040_of_match[] = {
432 	{ .compatible = "maxim,max17040" },
433 	{ .compatible = "maxim,max77836-battery" },
434 	{ },
435 };
436 MODULE_DEVICE_TABLE(of, max17040_of_match);
437 
438 static struct i2c_driver max17040_i2c_driver = {
439 	.driver	= {
440 		.name	= "max17040",
441 		.of_match_table = max17040_of_match,
442 		.pm	= MAX17040_PM_OPS,
443 	},
444 	.probe		= max17040_probe,
445 	.remove		= max17040_remove,
446 	.id_table	= max17040_id,
447 };
448 module_i2c_driver(max17040_i2c_driver);
449 
450 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
451 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
452 MODULE_LICENSE("GPL");
453