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