xref: /linux/drivers/power/supply/max17042_battery.c (revision b2d0f5d5dc53532e6f07bc546a476a55ebdfe0f3)
1 /*
2  * Fuel gauge driver for Maxim 17042 / 8966 / 8997
3  *  Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
4  *
5  * Copyright (C) 2011 Samsung Electronics
6  * MyungJoo Ham <myungjoo.ham@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * This driver is based on max17040_battery.c
23  */
24 
25 #include <linux/acpi.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/pm.h>
33 #include <linux/mod_devicetable.h>
34 #include <linux/power_supply.h>
35 #include <linux/power/max17042_battery.h>
36 #include <linux/of.h>
37 #include <linux/regmap.h>
38 
39 /* Status register bits */
40 #define STATUS_POR_BIT         (1 << 1)
41 #define STATUS_BST_BIT         (1 << 3)
42 #define STATUS_VMN_BIT         (1 << 8)
43 #define STATUS_TMN_BIT         (1 << 9)
44 #define STATUS_SMN_BIT         (1 << 10)
45 #define STATUS_BI_BIT          (1 << 11)
46 #define STATUS_VMX_BIT         (1 << 12)
47 #define STATUS_TMX_BIT         (1 << 13)
48 #define STATUS_SMX_BIT         (1 << 14)
49 #define STATUS_BR_BIT          (1 << 15)
50 
51 /* Interrupt mask bits */
52 #define CONFIG_ALRT_BIT_ENBL	(1 << 2)
53 #define STATUS_INTR_SOCMIN_BIT	(1 << 10)
54 #define STATUS_INTR_SOCMAX_BIT	(1 << 14)
55 
56 #define VFSOC0_LOCK		0x0000
57 #define VFSOC0_UNLOCK		0x0080
58 #define MODEL_UNLOCK1	0X0059
59 #define MODEL_UNLOCK2	0X00C4
60 #define MODEL_LOCK1		0X0000
61 #define MODEL_LOCK2		0X0000
62 
63 #define dQ_ACC_DIV	0x4
64 #define dP_ACC_100	0x1900
65 #define dP_ACC_200	0x3200
66 
67 #define MAX17042_VMAX_TOLERANCE		50 /* 50 mV */
68 
69 struct max17042_chip {
70 	struct i2c_client *client;
71 	struct regmap *regmap;
72 	struct power_supply *battery;
73 	enum max170xx_chip_type chip_type;
74 	struct max17042_platform_data *pdata;
75 	struct work_struct work;
76 	int    init_complete;
77 };
78 
79 static enum power_supply_property max17042_battery_props[] = {
80 	POWER_SUPPLY_PROP_STATUS,
81 	POWER_SUPPLY_PROP_PRESENT,
82 	POWER_SUPPLY_PROP_TECHNOLOGY,
83 	POWER_SUPPLY_PROP_CYCLE_COUNT,
84 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
85 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
86 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
87 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
88 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
89 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
90 	POWER_SUPPLY_PROP_CAPACITY,
91 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
92 	POWER_SUPPLY_PROP_CHARGE_FULL,
93 	POWER_SUPPLY_PROP_CHARGE_NOW,
94 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
95 	POWER_SUPPLY_PROP_TEMP,
96 	POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
97 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
98 	POWER_SUPPLY_PROP_TEMP_MIN,
99 	POWER_SUPPLY_PROP_TEMP_MAX,
100 	POWER_SUPPLY_PROP_HEALTH,
101 	POWER_SUPPLY_PROP_SCOPE,
102 	POWER_SUPPLY_PROP_CURRENT_NOW,
103 	POWER_SUPPLY_PROP_CURRENT_AVG,
104 };
105 
106 static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
107 {
108 	int ret;
109 	u32 data;
110 	struct regmap *map = chip->regmap;
111 
112 	ret = regmap_read(map, MAX17042_TEMP, &data);
113 	if (ret < 0)
114 		return ret;
115 
116 	*temp = sign_extend32(data, 15);
117 	/* The value is converted into deci-centigrade scale */
118 	/* Units of LSB = 1 / 256 degree Celsius */
119 	*temp = *temp * 10 / 256;
120 	return 0;
121 }
122 
123 static int max17042_get_status(struct max17042_chip *chip, int *status)
124 {
125 	int ret, charge_full, charge_now;
126 
127 	ret = power_supply_am_i_supplied(chip->battery);
128 	if (ret < 0) {
129 		*status = POWER_SUPPLY_STATUS_UNKNOWN;
130 		return 0;
131 	}
132 	if (ret == 0) {
133 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
134 		return 0;
135 	}
136 
137 	/*
138 	 * The MAX170xx has builtin end-of-charge detection and will update
139 	 * FullCAP to match RepCap when it detects end of charging.
140 	 *
141 	 * When this cycle the battery gets charged to a higher (calculated)
142 	 * capacity then the previous cycle then FullCAP will get updated
143 	 * contineously once end-of-charge detection kicks in, so allow the
144 	 * 2 to differ a bit.
145 	 */
146 
147 	ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full);
148 	if (ret < 0)
149 		return ret;
150 
151 	ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now);
152 	if (ret < 0)
153 		return ret;
154 
155 	if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD)
156 		*status = POWER_SUPPLY_STATUS_FULL;
157 	else
158 		*status = POWER_SUPPLY_STATUS_CHARGING;
159 
160 	return 0;
161 }
162 
163 static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
164 {
165 	int temp, vavg, vbatt, ret;
166 	u32 val;
167 
168 	ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val);
169 	if (ret < 0)
170 		goto health_error;
171 
172 	/* bits [0-3] unused */
173 	vavg = val * 625 / 8;
174 	/* Convert to millivolts */
175 	vavg /= 1000;
176 
177 	ret = regmap_read(chip->regmap, MAX17042_VCELL, &val);
178 	if (ret < 0)
179 		goto health_error;
180 
181 	/* bits [0-3] unused */
182 	vbatt = val * 625 / 8;
183 	/* Convert to millivolts */
184 	vbatt /= 1000;
185 
186 	if (vavg < chip->pdata->vmin) {
187 		*health = POWER_SUPPLY_HEALTH_DEAD;
188 		goto out;
189 	}
190 
191 	if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) {
192 		*health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
193 		goto out;
194 	}
195 
196 	ret = max17042_get_temperature(chip, &temp);
197 	if (ret < 0)
198 		goto health_error;
199 
200 	if (temp < chip->pdata->temp_min) {
201 		*health = POWER_SUPPLY_HEALTH_COLD;
202 		goto out;
203 	}
204 
205 	if (temp > chip->pdata->temp_max) {
206 		*health = POWER_SUPPLY_HEALTH_OVERHEAT;
207 		goto out;
208 	}
209 
210 	*health = POWER_SUPPLY_HEALTH_GOOD;
211 
212 out:
213 	return 0;
214 
215 health_error:
216 	return ret;
217 }
218 
219 static int max17042_get_property(struct power_supply *psy,
220 			    enum power_supply_property psp,
221 			    union power_supply_propval *val)
222 {
223 	struct max17042_chip *chip = power_supply_get_drvdata(psy);
224 	struct regmap *map = chip->regmap;
225 	int ret;
226 	u32 data;
227 	u64 data64;
228 
229 	if (!chip->init_complete)
230 		return -EAGAIN;
231 
232 	switch (psp) {
233 	case POWER_SUPPLY_PROP_STATUS:
234 		ret = max17042_get_status(chip, &val->intval);
235 		if (ret < 0)
236 			return ret;
237 		break;
238 	case POWER_SUPPLY_PROP_PRESENT:
239 		ret = regmap_read(map, MAX17042_STATUS, &data);
240 		if (ret < 0)
241 			return ret;
242 
243 		if (data & MAX17042_STATUS_BattAbsent)
244 			val->intval = 0;
245 		else
246 			val->intval = 1;
247 		break;
248 	case POWER_SUPPLY_PROP_TECHNOLOGY:
249 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
250 		break;
251 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
252 		ret = regmap_read(map, MAX17042_Cycles, &data);
253 		if (ret < 0)
254 			return ret;
255 
256 		val->intval = data;
257 		break;
258 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
259 		ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
260 		if (ret < 0)
261 			return ret;
262 
263 		val->intval = data >> 8;
264 		val->intval *= 20000; /* Units of LSB = 20mV */
265 		break;
266 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
267 		ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
268 		if (ret < 0)
269 			return ret;
270 
271 		val->intval = (data & 0xff) * 20000; /* Units of 20mV */
272 		break;
273 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
274 		if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
275 			ret = regmap_read(map, MAX17042_V_empty, &data);
276 		else
277 			ret = regmap_read(map, MAX17047_V_empty, &data);
278 		if (ret < 0)
279 			return ret;
280 
281 		val->intval = data >> 7;
282 		val->intval *= 10000; /* Units of LSB = 10mV */
283 		break;
284 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
285 		ret = regmap_read(map, MAX17042_VCELL, &data);
286 		if (ret < 0)
287 			return ret;
288 
289 		val->intval = data * 625 / 8;
290 		break;
291 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
292 		ret = regmap_read(map, MAX17042_AvgVCELL, &data);
293 		if (ret < 0)
294 			return ret;
295 
296 		val->intval = data * 625 / 8;
297 		break;
298 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
299 		ret = regmap_read(map, MAX17042_OCVInternal, &data);
300 		if (ret < 0)
301 			return ret;
302 
303 		val->intval = data * 625 / 8;
304 		break;
305 	case POWER_SUPPLY_PROP_CAPACITY:
306 		ret = regmap_read(map, MAX17042_RepSOC, &data);
307 		if (ret < 0)
308 			return ret;
309 
310 		val->intval = data >> 8;
311 		break;
312 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
313 		ret = regmap_read(map, MAX17042_DesignCap, &data);
314 		if (ret < 0)
315 			return ret;
316 
317 		data64 = data * 5000000ll;
318 		do_div(data64, chip->pdata->r_sns);
319 		val->intval = data64;
320 		break;
321 	case POWER_SUPPLY_PROP_CHARGE_FULL:
322 		ret = regmap_read(map, MAX17042_FullCAP, &data);
323 		if (ret < 0)
324 			return ret;
325 
326 		data64 = data * 5000000ll;
327 		do_div(data64, chip->pdata->r_sns);
328 		val->intval = data64;
329 		break;
330 	case POWER_SUPPLY_PROP_CHARGE_NOW:
331 		ret = regmap_read(map, MAX17042_RepCap, &data);
332 		if (ret < 0)
333 			return ret;
334 
335 		data64 = data * 5000000ll;
336 		do_div(data64, chip->pdata->r_sns);
337 		val->intval = data64;
338 		break;
339 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
340 		ret = regmap_read(map, MAX17042_QH, &data);
341 		if (ret < 0)
342 			return ret;
343 
344 		val->intval = data * 1000 / 2;
345 		break;
346 	case POWER_SUPPLY_PROP_TEMP:
347 		ret = max17042_get_temperature(chip, &val->intval);
348 		if (ret < 0)
349 			return ret;
350 		break;
351 	case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
352 		ret = regmap_read(map, MAX17042_TALRT_Th, &data);
353 		if (ret < 0)
354 			return ret;
355 		/* LSB is Alert Minimum. In deci-centigrade */
356 		val->intval = sign_extend32(data & 0xff, 7) * 10;
357 		break;
358 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
359 		ret = regmap_read(map, MAX17042_TALRT_Th, &data);
360 		if (ret < 0)
361 			return ret;
362 		/* MSB is Alert Maximum. In deci-centigrade */
363 		val->intval = sign_extend32(data >> 8, 7) * 10;
364 		break;
365 	case POWER_SUPPLY_PROP_TEMP_MIN:
366 		val->intval = chip->pdata->temp_min;
367 		break;
368 	case POWER_SUPPLY_PROP_TEMP_MAX:
369 		val->intval = chip->pdata->temp_max;
370 		break;
371 	case POWER_SUPPLY_PROP_HEALTH:
372 		ret = max17042_get_battery_health(chip, &val->intval);
373 		if (ret < 0)
374 			return ret;
375 		break;
376 	case POWER_SUPPLY_PROP_SCOPE:
377 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
378 		break;
379 	case POWER_SUPPLY_PROP_CURRENT_NOW:
380 		if (chip->pdata->enable_current_sense) {
381 			ret = regmap_read(map, MAX17042_Current, &data);
382 			if (ret < 0)
383 				return ret;
384 
385 			val->intval = sign_extend32(data, 15);
386 			val->intval *= 1562500 / chip->pdata->r_sns;
387 		} else {
388 			return -EINVAL;
389 		}
390 		break;
391 	case POWER_SUPPLY_PROP_CURRENT_AVG:
392 		if (chip->pdata->enable_current_sense) {
393 			ret = regmap_read(map, MAX17042_AvgCurrent, &data);
394 			if (ret < 0)
395 				return ret;
396 
397 			val->intval = sign_extend32(data, 15);
398 			val->intval *= 1562500 / chip->pdata->r_sns;
399 		} else {
400 			return -EINVAL;
401 		}
402 		break;
403 	default:
404 		return -EINVAL;
405 	}
406 	return 0;
407 }
408 
409 static int max17042_set_property(struct power_supply *psy,
410 			    enum power_supply_property psp,
411 			    const union power_supply_propval *val)
412 {
413 	struct max17042_chip *chip = power_supply_get_drvdata(psy);
414 	struct regmap *map = chip->regmap;
415 	int ret = 0;
416 	u32 data;
417 	int8_t temp;
418 
419 	switch (psp) {
420 	case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
421 		ret = regmap_read(map, MAX17042_TALRT_Th, &data);
422 		if (ret < 0)
423 			return ret;
424 
425 		/* Input in deci-centigrade, convert to centigrade */
426 		temp = val->intval / 10;
427 		/* force min < max */
428 		if (temp >= (int8_t)(data >> 8))
429 			temp = (int8_t)(data >> 8) - 1;
430 		/* Write both MAX and MIN ALERT */
431 		data = (data & 0xff00) + temp;
432 		ret = regmap_write(map, MAX17042_TALRT_Th, data);
433 		break;
434 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
435 		ret = regmap_read(map, MAX17042_TALRT_Th, &data);
436 		if (ret < 0)
437 			return ret;
438 
439 		/* Input in Deci-Centigrade, convert to centigrade */
440 		temp = val->intval / 10;
441 		/* force max > min */
442 		if (temp <= (int8_t)(data & 0xff))
443 			temp = (int8_t)(data & 0xff) + 1;
444 		/* Write both MAX and MIN ALERT */
445 		data = (data & 0xff) + (temp << 8);
446 		ret = regmap_write(map, MAX17042_TALRT_Th, data);
447 		break;
448 	default:
449 		ret = -EINVAL;
450 	}
451 
452 	return ret;
453 }
454 
455 static int max17042_property_is_writeable(struct power_supply *psy,
456 		enum power_supply_property psp)
457 {
458 	int ret;
459 
460 	switch (psp) {
461 	case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
462 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
463 		ret = 1;
464 		break;
465 	default:
466 		ret = 0;
467 	}
468 
469 	return ret;
470 }
471 
472 static void max17042_external_power_changed(struct power_supply *psy)
473 {
474 	power_supply_changed(psy);
475 }
476 
477 static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
478 {
479 	int retries = 8;
480 	int ret;
481 	u32 read_value;
482 
483 	do {
484 		ret = regmap_write(map, reg, value);
485 		regmap_read(map, reg, &read_value);
486 		if (read_value != value) {
487 			ret = -EIO;
488 			retries--;
489 		}
490 	} while (retries && read_value != value);
491 
492 	if (ret < 0)
493 		pr_err("%s: err %d\n", __func__, ret);
494 
495 	return ret;
496 }
497 
498 static inline void max17042_override_por(struct regmap *map,
499 					 u8 reg, u16 value)
500 {
501 	if (value)
502 		regmap_write(map, reg, value);
503 }
504 
505 static inline void max10742_unlock_model(struct max17042_chip *chip)
506 {
507 	struct regmap *map = chip->regmap;
508 
509 	regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
510 	regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
511 }
512 
513 static inline void max10742_lock_model(struct max17042_chip *chip)
514 {
515 	struct regmap *map = chip->regmap;
516 
517 	regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
518 	regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
519 }
520 
521 static inline void max17042_write_model_data(struct max17042_chip *chip,
522 					u8 addr, int size)
523 {
524 	struct regmap *map = chip->regmap;
525 	int i;
526 
527 	for (i = 0; i < size; i++)
528 		regmap_write(map, addr + i,
529 			chip->pdata->config_data->cell_char_tbl[i]);
530 }
531 
532 static inline void max17042_read_model_data(struct max17042_chip *chip,
533 					u8 addr, u16 *data, int size)
534 {
535 	struct regmap *map = chip->regmap;
536 	int i;
537 	u32 tmp;
538 
539 	for (i = 0; i < size; i++) {
540 		regmap_read(map, addr + i, &tmp);
541 		data[i] = (u16)tmp;
542 	}
543 }
544 
545 static inline int max17042_model_data_compare(struct max17042_chip *chip,
546 					u16 *data1, u16 *data2, int size)
547 {
548 	int i;
549 
550 	if (memcmp(data1, data2, size)) {
551 		dev_err(&chip->client->dev, "%s compare failed\n", __func__);
552 		for (i = 0; i < size; i++)
553 			dev_info(&chip->client->dev, "0x%x, 0x%x",
554 				data1[i], data2[i]);
555 		dev_info(&chip->client->dev, "\n");
556 		return -EINVAL;
557 	}
558 	return 0;
559 }
560 
561 static int max17042_init_model(struct max17042_chip *chip)
562 {
563 	int ret;
564 	int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
565 	u16 *temp_data;
566 
567 	temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
568 	if (!temp_data)
569 		return -ENOMEM;
570 
571 	max10742_unlock_model(chip);
572 	max17042_write_model_data(chip, MAX17042_MODELChrTbl,
573 				table_size);
574 	max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
575 				table_size);
576 
577 	ret = max17042_model_data_compare(
578 		chip,
579 		chip->pdata->config_data->cell_char_tbl,
580 		temp_data,
581 		table_size);
582 
583 	max10742_lock_model(chip);
584 	kfree(temp_data);
585 
586 	return ret;
587 }
588 
589 static int max17042_verify_model_lock(struct max17042_chip *chip)
590 {
591 	int i;
592 	int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
593 	u16 *temp_data;
594 	int ret = 0;
595 
596 	temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
597 	if (!temp_data)
598 		return -ENOMEM;
599 
600 	max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
601 				table_size);
602 	for (i = 0; i < table_size; i++)
603 		if (temp_data[i])
604 			ret = -EINVAL;
605 
606 	kfree(temp_data);
607 	return ret;
608 }
609 
610 static void max17042_write_config_regs(struct max17042_chip *chip)
611 {
612 	struct max17042_config_data *config = chip->pdata->config_data;
613 	struct regmap *map = chip->regmap;
614 
615 	regmap_write(map, MAX17042_CONFIG, config->config);
616 	regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
617 	regmap_write(map, MAX17042_FilterCFG,
618 			config->filter_cfg);
619 	regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
620 	if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 ||
621 			chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)
622 		regmap_write(map, MAX17047_FullSOCThr,
623 						config->full_soc_thresh);
624 }
625 
626 static void  max17042_write_custom_regs(struct max17042_chip *chip)
627 {
628 	struct max17042_config_data *config = chip->pdata->config_data;
629 	struct regmap *map = chip->regmap;
630 
631 	max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
632 	max17042_write_verify_reg(map, MAX17042_TempCo,	config->tcompc0);
633 	max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
634 	if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) {
635 		regmap_write(map, MAX17042_EmptyTempCo,	config->empty_tempco);
636 		max17042_write_verify_reg(map, MAX17042_K_empty0,
637 					config->kempty0);
638 	} else {
639 		max17042_write_verify_reg(map, MAX17047_QRTbl00,
640 						config->qrtbl00);
641 		max17042_write_verify_reg(map, MAX17047_QRTbl10,
642 						config->qrtbl10);
643 		max17042_write_verify_reg(map, MAX17047_QRTbl20,
644 						config->qrtbl20);
645 		max17042_write_verify_reg(map, MAX17047_QRTbl30,
646 						config->qrtbl30);
647 	}
648 }
649 
650 static void max17042_update_capacity_regs(struct max17042_chip *chip)
651 {
652 	struct max17042_config_data *config = chip->pdata->config_data;
653 	struct regmap *map = chip->regmap;
654 
655 	max17042_write_verify_reg(map, MAX17042_FullCAP,
656 				config->fullcap);
657 	regmap_write(map, MAX17042_DesignCap, config->design_cap);
658 	max17042_write_verify_reg(map, MAX17042_FullCAPNom,
659 				config->fullcapnom);
660 }
661 
662 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
663 {
664 	unsigned int vfSoc;
665 	struct regmap *map = chip->regmap;
666 
667 	regmap_read(map, MAX17042_VFSOC, &vfSoc);
668 	regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
669 	max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
670 	regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
671 }
672 
673 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
674 {
675 	u32 full_cap0, rep_cap, dq_acc, vfSoc;
676 	u32 rem_cap;
677 
678 	struct max17042_config_data *config = chip->pdata->config_data;
679 	struct regmap *map = chip->regmap;
680 
681 	regmap_read(map, MAX17042_FullCAP0, &full_cap0);
682 	regmap_read(map, MAX17042_VFSOC, &vfSoc);
683 
684 	/* fg_vfSoc needs to shifted by 8 bits to get the
685 	 * perc in 1% accuracy, to get the right rem_cap multiply
686 	 * full_cap0, fg_vfSoc and devide by 100
687 	 */
688 	rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
689 	max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
690 
691 	rep_cap = rem_cap;
692 	max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
693 
694 	/* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
695 	dq_acc = config->fullcap / dQ_ACC_DIV;
696 	max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
697 	max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
698 
699 	max17042_write_verify_reg(map, MAX17042_FullCAP,
700 			config->fullcap);
701 	regmap_write(map, MAX17042_DesignCap,
702 			config->design_cap);
703 	max17042_write_verify_reg(map, MAX17042_FullCAPNom,
704 			config->fullcapnom);
705 	/* Update SOC register with new SOC */
706 	regmap_write(map, MAX17042_RepSOC, vfSoc);
707 }
708 
709 /*
710  * Block write all the override values coming from platform data.
711  * This function MUST be called before the POR initialization proceedure
712  * specified by maxim.
713  */
714 static inline void max17042_override_por_values(struct max17042_chip *chip)
715 {
716 	struct regmap *map = chip->regmap;
717 	struct max17042_config_data *config = chip->pdata->config_data;
718 
719 	max17042_override_por(map, MAX17042_TGAIN, config->tgain);
720 	max17042_override_por(map, MAx17042_TOFF, config->toff);
721 	max17042_override_por(map, MAX17042_CGAIN, config->cgain);
722 	max17042_override_por(map, MAX17042_COFF, config->coff);
723 
724 	max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
725 	max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
726 	max17042_override_por(map, MAX17042_SALRT_Th,
727 						config->soc_alrt_thresh);
728 	max17042_override_por(map, MAX17042_CONFIG, config->config);
729 	max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
730 
731 	max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
732 	max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
733 
734 	max17042_override_por(map, MAX17042_AtRate, config->at_rate);
735 	max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
736 	max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
737 	max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
738 	max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
739 	max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
740 
741 	max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
742 	max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
743 	if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
744 		max17042_override_por(map, MAX17042_SOC_empty,
745 						config->socempty);
746 	max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
747 	max17042_override_por(map, MAX17042_dQacc, config->dqacc);
748 	max17042_override_por(map, MAX17042_dPacc, config->dpacc);
749 
750 	if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
751 		max17042_override_por(map, MAX17042_V_empty, config->vempty);
752 	else
753 		max17042_override_por(map, MAX17047_V_empty, config->vempty);
754 	max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
755 	max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
756 	max17042_override_por(map, MAX17042_FCTC, config->fctc);
757 	max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
758 	max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
759 	if (chip->chip_type) {
760 		max17042_override_por(map, MAX17042_EmptyTempCo,
761 						config->empty_tempco);
762 		max17042_override_por(map, MAX17042_K_empty0,
763 						config->kempty0);
764 	}
765 }
766 
767 static int max17042_init_chip(struct max17042_chip *chip)
768 {
769 	struct regmap *map = chip->regmap;
770 	int ret;
771 
772 	max17042_override_por_values(chip);
773 	/* After Power up, the MAX17042 requires 500mS in order
774 	 * to perform signal debouncing and initial SOC reporting
775 	 */
776 	msleep(500);
777 
778 	/* Initialize configaration */
779 	max17042_write_config_regs(chip);
780 
781 	/* write cell characterization data */
782 	ret = max17042_init_model(chip);
783 	if (ret) {
784 		dev_err(&chip->client->dev, "%s init failed\n",
785 			__func__);
786 		return -EIO;
787 	}
788 
789 	ret = max17042_verify_model_lock(chip);
790 	if (ret) {
791 		dev_err(&chip->client->dev, "%s lock verify failed\n",
792 			__func__);
793 		return -EIO;
794 	}
795 	/* write custom parameters */
796 	max17042_write_custom_regs(chip);
797 
798 	/* update capacity params */
799 	max17042_update_capacity_regs(chip);
800 
801 	/* delay must be atleast 350mS to allow VFSOC
802 	 * to be calculated from the new configuration
803 	 */
804 	msleep(350);
805 
806 	/* reset vfsoc0 reg */
807 	max17042_reset_vfsoc0_reg(chip);
808 
809 	/* load new capacity params */
810 	max17042_load_new_capacity_params(chip);
811 
812 	/* Init complete, Clear the POR bit */
813 	regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
814 	return 0;
815 }
816 
817 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
818 {
819 	struct regmap *map = chip->regmap;
820 	u32 soc, soc_tr;
821 
822 	/* program interrupt thesholds such that we should
823 	 * get interrupt for every 'off' perc change in the soc
824 	 */
825 	regmap_read(map, MAX17042_RepSOC, &soc);
826 	soc >>= 8;
827 	soc_tr = (soc + off) << 8;
828 	soc_tr |= (soc - off);
829 	regmap_write(map, MAX17042_SALRT_Th, soc_tr);
830 }
831 
832 static irqreturn_t max17042_thread_handler(int id, void *dev)
833 {
834 	struct max17042_chip *chip = dev;
835 	u32 val;
836 
837 	regmap_read(chip->regmap, MAX17042_STATUS, &val);
838 	if ((val & STATUS_INTR_SOCMIN_BIT) ||
839 		(val & STATUS_INTR_SOCMAX_BIT)) {
840 		dev_info(&chip->client->dev, "SOC threshold INTR\n");
841 		max17042_set_soc_threshold(chip, 1);
842 	}
843 
844 	power_supply_changed(chip->battery);
845 	return IRQ_HANDLED;
846 }
847 
848 static void max17042_init_worker(struct work_struct *work)
849 {
850 	struct max17042_chip *chip = container_of(work,
851 				struct max17042_chip, work);
852 	int ret;
853 
854 	/* Initialize registers according to values from the platform data */
855 	if (chip->pdata->enable_por_init && chip->pdata->config_data) {
856 		ret = max17042_init_chip(chip);
857 		if (ret)
858 			return;
859 	}
860 
861 	chip->init_complete = 1;
862 }
863 
864 #ifdef CONFIG_OF
865 static struct max17042_platform_data *
866 max17042_get_pdata(struct max17042_chip *chip)
867 {
868 	struct device *dev = &chip->client->dev;
869 	struct device_node *np = dev->of_node;
870 	u32 prop;
871 	struct max17042_platform_data *pdata;
872 
873 	if (!np)
874 		return dev->platform_data;
875 
876 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
877 	if (!pdata)
878 		return NULL;
879 
880 	/*
881 	 * Require current sense resistor value to be specified for
882 	 * current-sense functionality to be enabled at all.
883 	 */
884 	if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
885 		pdata->r_sns = prop;
886 		pdata->enable_current_sense = true;
887 	}
888 
889 	if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min))
890 		pdata->temp_min = INT_MIN;
891 	if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max))
892 		pdata->temp_max = INT_MAX;
893 	if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin))
894 		pdata->vmin = INT_MIN;
895 	if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax))
896 		pdata->vmax = INT_MAX;
897 
898 	return pdata;
899 }
900 #else
901 static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
902 	/*
903 	 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
904 	 * when the voltage FG reports 95%, as recommended in the datasheet.
905 	 */
906 	{ MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 },
907 };
908 
909 static struct max17042_platform_data *
910 max17042_get_pdata(struct max17042_chip *chip)
911 {
912 	struct device *dev = &chip->client->dev;
913 	struct max17042_platform_data *pdata;
914 	int ret, misc_cfg;
915 
916 	if (dev->platform_data)
917 		return dev->platform_data;
918 
919 	/*
920 	 * The MAX17047 gets used on x86 where we might not have pdata, assume
921 	 * the firmware will already have initialized the fuel-gauge and provide
922 	 * default values for the non init bits to make things work.
923 	 */
924 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
925 	if (!pdata)
926 		return pdata;
927 
928 	if (chip->chip_type != MAXIM_DEVICE_TYPE_MAX17042) {
929 		pdata->init_data = max17047_default_pdata_init_regs;
930 		pdata->num_init_data =
931 			ARRAY_SIZE(max17047_default_pdata_init_regs);
932 	}
933 
934 	ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg);
935 	if (ret < 0)
936 		return NULL;
937 
938 	/* If bits 0-1 are set to 3 then only Voltage readings are used */
939 	if ((misc_cfg & 0x3) == 0x3)
940 		pdata->enable_current_sense = false;
941 	else
942 		pdata->enable_current_sense = true;
943 
944 	pdata->vmin = MAX17042_DEFAULT_VMIN;
945 	pdata->vmax = MAX17042_DEFAULT_VMAX;
946 	pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN;
947 	pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX;
948 
949 	return pdata;
950 }
951 #endif
952 
953 static const struct regmap_config max17042_regmap_config = {
954 	.reg_bits = 8,
955 	.val_bits = 16,
956 	.val_format_endian = REGMAP_ENDIAN_NATIVE,
957 };
958 
959 static const struct power_supply_desc max17042_psy_desc = {
960 	.name		= "max170xx_battery",
961 	.type		= POWER_SUPPLY_TYPE_BATTERY,
962 	.get_property	= max17042_get_property,
963 	.set_property	= max17042_set_property,
964 	.property_is_writeable	= max17042_property_is_writeable,
965 	.external_power_changed	= max17042_external_power_changed,
966 	.properties	= max17042_battery_props,
967 	.num_properties	= ARRAY_SIZE(max17042_battery_props),
968 };
969 
970 static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
971 	.name		= "max170xx_battery",
972 	.type		= POWER_SUPPLY_TYPE_BATTERY,
973 	.get_property	= max17042_get_property,
974 	.set_property	= max17042_set_property,
975 	.property_is_writeable	= max17042_property_is_writeable,
976 	.properties	= max17042_battery_props,
977 	.num_properties	= ARRAY_SIZE(max17042_battery_props) - 2,
978 };
979 
980 static int max17042_probe(struct i2c_client *client,
981 			const struct i2c_device_id *id)
982 {
983 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
984 	const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
985 	struct power_supply_config psy_cfg = {};
986 	const struct acpi_device_id *acpi_id = NULL;
987 	struct device *dev = &client->dev;
988 	struct max17042_chip *chip;
989 	int ret;
990 	int i;
991 	u32 val;
992 
993 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
994 		return -EIO;
995 
996 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
997 	if (!chip)
998 		return -ENOMEM;
999 
1000 	chip->client = client;
1001 	if (id) {
1002 		chip->chip_type = id->driver_data;
1003 	} else {
1004 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
1005 		if (!acpi_id)
1006 			return -ENODEV;
1007 
1008 		chip->chip_type = acpi_id->driver_data;
1009 	}
1010 	chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
1011 	if (IS_ERR(chip->regmap)) {
1012 		dev_err(&client->dev, "Failed to initialize regmap\n");
1013 		return -EINVAL;
1014 	}
1015 
1016 	chip->pdata = max17042_get_pdata(chip);
1017 	if (!chip->pdata) {
1018 		dev_err(&client->dev, "no platform data provided\n");
1019 		return -EINVAL;
1020 	}
1021 
1022 	i2c_set_clientdata(client, chip);
1023 	psy_cfg.drv_data = chip;
1024 
1025 	/* When current is not measured,
1026 	 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
1027 	if (!chip->pdata->enable_current_sense)
1028 		max17042_desc = &max17042_no_current_sense_psy_desc;
1029 
1030 	if (chip->pdata->r_sns == 0)
1031 		chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
1032 
1033 	if (chip->pdata->init_data)
1034 		for (i = 0; i < chip->pdata->num_init_data; i++)
1035 			regmap_write(chip->regmap,
1036 					chip->pdata->init_data[i].addr,
1037 					chip->pdata->init_data[i].data);
1038 
1039 	if (!chip->pdata->enable_current_sense) {
1040 		regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
1041 		regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
1042 		regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
1043 	}
1044 
1045 	chip->battery = devm_power_supply_register(&client->dev, max17042_desc,
1046 						   &psy_cfg);
1047 	if (IS_ERR(chip->battery)) {
1048 		dev_err(&client->dev, "failed: power supply register\n");
1049 		return PTR_ERR(chip->battery);
1050 	}
1051 
1052 	if (client->irq) {
1053 		unsigned int flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
1054 
1055 		/*
1056 		 * On ACPI systems the IRQ may be handled by ACPI-event code,
1057 		 * so we need to share (if the ACPI code is willing to share).
1058 		 */
1059 		if (acpi_id)
1060 			flags |= IRQF_SHARED | IRQF_PROBE_SHARED;
1061 
1062 		ret = devm_request_threaded_irq(&client->dev, client->irq,
1063 						NULL,
1064 						max17042_thread_handler, flags,
1065 						chip->battery->desc->name,
1066 						chip);
1067 		if (!ret) {
1068 			regmap_update_bits(chip->regmap, MAX17042_CONFIG,
1069 					CONFIG_ALRT_BIT_ENBL,
1070 					CONFIG_ALRT_BIT_ENBL);
1071 			max17042_set_soc_threshold(chip, 1);
1072 		} else {
1073 			client->irq = 0;
1074 			if (ret != -EBUSY)
1075 				dev_err(&client->dev, "Failed to get IRQ\n");
1076 		}
1077 	}
1078 	/* Not able to update the charge threshold when exceeded? -> disable */
1079 	if (!client->irq)
1080 		regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00);
1081 
1082 	regmap_read(chip->regmap, MAX17042_STATUS, &val);
1083 	if (val & STATUS_POR_BIT) {
1084 		INIT_WORK(&chip->work, max17042_init_worker);
1085 		schedule_work(&chip->work);
1086 	} else {
1087 		chip->init_complete = 1;
1088 	}
1089 
1090 	return 0;
1091 }
1092 
1093 #ifdef CONFIG_PM_SLEEP
1094 static int max17042_suspend(struct device *dev)
1095 {
1096 	struct max17042_chip *chip = dev_get_drvdata(dev);
1097 
1098 	/*
1099 	 * disable the irq and enable irq_wake
1100 	 * capability to the interrupt line.
1101 	 */
1102 	if (chip->client->irq) {
1103 		disable_irq(chip->client->irq);
1104 		enable_irq_wake(chip->client->irq);
1105 	}
1106 
1107 	return 0;
1108 }
1109 
1110 static int max17042_resume(struct device *dev)
1111 {
1112 	struct max17042_chip *chip = dev_get_drvdata(dev);
1113 
1114 	if (chip->client->irq) {
1115 		disable_irq_wake(chip->client->irq);
1116 		enable_irq(chip->client->irq);
1117 		/* re-program the SOC thresholds to 1% change */
1118 		max17042_set_soc_threshold(chip, 1);
1119 	}
1120 
1121 	return 0;
1122 }
1123 #endif
1124 
1125 static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
1126 			max17042_resume);
1127 
1128 #ifdef CONFIG_ACPI
1129 static const struct acpi_device_id max17042_acpi_match[] = {
1130 	{ "MAX17047", MAXIM_DEVICE_TYPE_MAX17047 },
1131 	{ }
1132 };
1133 MODULE_DEVICE_TABLE(acpi, max17042_acpi_match);
1134 #endif
1135 
1136 #ifdef CONFIG_OF
1137 static const struct of_device_id max17042_dt_match[] = {
1138 	{ .compatible = "maxim,max17042" },
1139 	{ .compatible = "maxim,max17047" },
1140 	{ .compatible = "maxim,max17050" },
1141 	{ },
1142 };
1143 MODULE_DEVICE_TABLE(of, max17042_dt_match);
1144 #endif
1145 
1146 static const struct i2c_device_id max17042_id[] = {
1147 	{ "max17042", MAXIM_DEVICE_TYPE_MAX17042 },
1148 	{ "max17047", MAXIM_DEVICE_TYPE_MAX17047 },
1149 	{ "max17050", MAXIM_DEVICE_TYPE_MAX17050 },
1150 	{ }
1151 };
1152 MODULE_DEVICE_TABLE(i2c, max17042_id);
1153 
1154 static struct i2c_driver max17042_i2c_driver = {
1155 	.driver	= {
1156 		.name	= "max17042",
1157 		.acpi_match_table = ACPI_PTR(max17042_acpi_match),
1158 		.of_match_table = of_match_ptr(max17042_dt_match),
1159 		.pm	= &max17042_pm_ops,
1160 	},
1161 	.probe		= max17042_probe,
1162 	.id_table	= max17042_id,
1163 };
1164 module_i2c_driver(max17042_i2c_driver);
1165 
1166 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1167 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1168 MODULE_LICENSE("GPL");
1169