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