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