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