2 * Fuel gauge driver for Maxim 17042 / 8966 / 8997
3 * Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This driver is based on max17040_battery.c
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/power_supply.h>
34 #include <linux/power/max17042_battery.h>
36 #include <linux/regmap.h>
38 /* Status register bits */
39 #define STATUS_POR_BIT (1 << 1)
40 #define STATUS_BST_BIT (1 << 3)
41 #define STATUS_VMN_BIT (1 << 8)
42 #define STATUS_TMN_BIT (1 << 9)
43 #define STATUS_SMN_BIT (1 << 10)
44 #define STATUS_BI_BIT (1 << 11)
45 #define STATUS_VMX_BIT (1 << 12)
46 #define STATUS_TMX_BIT (1 << 13)
47 #define STATUS_SMX_BIT (1 << 14)
48 #define STATUS_BR_BIT (1 << 15)
50 /* Interrupt mask bits */
51 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
52 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
53 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
55 #define VFSOC0_LOCK 0x0000
56 #define VFSOC0_UNLOCK 0x0080
57 #define MODEL_UNLOCK1 0X0059
58 #define MODEL_UNLOCK2 0X00C4
59 #define MODEL_LOCK1 0X0000
60 #define MODEL_LOCK2 0X0000
62 #define dQ_ACC_DIV 0x4
63 #define dP_ACC_100 0x1900
64 #define dP_ACC_200 0x3200
66 #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
68 struct max17042_chip {
69 struct i2c_client *client;
70 struct regmap *regmap;
71 struct power_supply *battery;
72 enum max170xx_chip_type chip_type;
73 struct max17042_platform_data *pdata;
74 struct work_struct work;
78 static enum power_supply_property max17042_battery_props[] = {
79 POWER_SUPPLY_PROP_STATUS,
80 POWER_SUPPLY_PROP_PRESENT,
81 POWER_SUPPLY_PROP_TECHNOLOGY,
82 POWER_SUPPLY_PROP_CYCLE_COUNT,
83 POWER_SUPPLY_PROP_VOLTAGE_MAX,
84 POWER_SUPPLY_PROP_VOLTAGE_MIN,
85 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
86 POWER_SUPPLY_PROP_VOLTAGE_NOW,
87 POWER_SUPPLY_PROP_VOLTAGE_AVG,
88 POWER_SUPPLY_PROP_VOLTAGE_OCV,
89 POWER_SUPPLY_PROP_CAPACITY,
90 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
91 POWER_SUPPLY_PROP_CHARGE_FULL,
92 POWER_SUPPLY_PROP_CHARGE_NOW,
93 POWER_SUPPLY_PROP_CHARGE_COUNTER,
94 POWER_SUPPLY_PROP_TEMP,
95 POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
96 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
97 POWER_SUPPLY_PROP_TEMP_MIN,
98 POWER_SUPPLY_PROP_TEMP_MAX,
99 POWER_SUPPLY_PROP_HEALTH,
100 POWER_SUPPLY_PROP_SCOPE,
101 POWER_SUPPLY_PROP_CURRENT_NOW,
102 POWER_SUPPLY_PROP_CURRENT_AVG,
105 static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
109 struct regmap *map = chip->regmap;
111 ret = regmap_read(map, MAX17042_TEMP, &data);
115 *temp = sign_extend32(data, 15);
116 /* The value is converted into deci-centigrade scale */
117 /* Units of LSB = 1 / 256 degree Celsius */
118 *temp = *temp * 10 / 256;
122 static int max17042_get_status(struct max17042_chip *chip, int *status)
124 int ret, charge_full, charge_now;
126 ret = power_supply_am_i_supplied(chip->battery);
128 *status = POWER_SUPPLY_STATUS_UNKNOWN;
132 *status = POWER_SUPPLY_STATUS_DISCHARGING;
137 * The MAX170xx has builtin end-of-charge detection and will update
138 * FullCAP to match RepCap when it detects end of charging.
140 * When this cycle the battery gets charged to a higher (calculated)
141 * capacity then the previous cycle then FullCAP will get updated
142 * contineously once end-of-charge detection kicks in, so allow the
146 ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full);
150 ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now);
154 if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD)
155 *status = POWER_SUPPLY_STATUS_FULL;
157 *status = POWER_SUPPLY_STATUS_CHARGING;
162 static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
164 int temp, vavg, vbatt, ret;
167 ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val);
171 /* bits [0-3] unused */
172 vavg = val * 625 / 8;
173 /* Convert to millivolts */
176 ret = regmap_read(chip->regmap, MAX17042_VCELL, &val);
180 /* bits [0-3] unused */
181 vbatt = val * 625 / 8;
182 /* Convert to millivolts */
185 if (vavg < chip->pdata->vmin) {
186 *health = POWER_SUPPLY_HEALTH_DEAD;
190 if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) {
191 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
195 ret = max17042_get_temperature(chip, &temp);
199 if (temp < chip->pdata->temp_min) {
200 *health = POWER_SUPPLY_HEALTH_COLD;
204 if (temp > chip->pdata->temp_max) {
205 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
209 *health = POWER_SUPPLY_HEALTH_GOOD;
218 static int max17042_get_property(struct power_supply *psy,
219 enum power_supply_property psp,
220 union power_supply_propval *val)
222 struct max17042_chip *chip = power_supply_get_drvdata(psy);
223 struct regmap *map = chip->regmap;
228 if (!chip->init_complete)
232 case POWER_SUPPLY_PROP_STATUS:
233 ret = max17042_get_status(chip, &val->intval);
237 case POWER_SUPPLY_PROP_PRESENT:
238 ret = regmap_read(map, MAX17042_STATUS, &data);
242 if (data & MAX17042_STATUS_BattAbsent)
247 case POWER_SUPPLY_PROP_TECHNOLOGY:
248 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
250 case POWER_SUPPLY_PROP_CYCLE_COUNT:
251 ret = regmap_read(map, MAX17042_Cycles, &data);
257 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
258 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
262 val->intval = data >> 8;
263 val->intval *= 20000; /* Units of LSB = 20mV */
265 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
266 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
270 val->intval = (data & 0xff) * 20000; /* Units of 20mV */
272 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
273 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
274 ret = regmap_read(map, MAX17042_V_empty, &data);
276 ret = regmap_read(map, MAX17047_V_empty, &data);
280 val->intval = data >> 7;
281 val->intval *= 10000; /* Units of LSB = 10mV */
283 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
284 ret = regmap_read(map, MAX17042_VCELL, &data);
288 val->intval = data * 625 / 8;
290 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
291 ret = regmap_read(map, MAX17042_AvgVCELL, &data);
295 val->intval = data * 625 / 8;
297 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
298 ret = regmap_read(map, MAX17042_OCVInternal, &data);
302 val->intval = data * 625 / 8;
304 case POWER_SUPPLY_PROP_CAPACITY:
305 ret = regmap_read(map, MAX17042_RepSOC, &data);
309 val->intval = data >> 8;
311 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
312 ret = regmap_read(map, MAX17042_DesignCap, &data);
316 data64 = data * 5000000ll;
317 do_div(data64, chip->pdata->r_sns);
318 val->intval = data64;
320 case POWER_SUPPLY_PROP_CHARGE_FULL:
321 ret = regmap_read(map, MAX17042_FullCAP, &data);
325 data64 = data * 5000000ll;
326 do_div(data64, chip->pdata->r_sns);
327 val->intval = data64;
329 case POWER_SUPPLY_PROP_CHARGE_NOW:
330 ret = regmap_read(map, MAX17042_RepCap, &data);
334 data64 = data * 5000000ll;
335 do_div(data64, chip->pdata->r_sns);
336 val->intval = data64;
338 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
339 ret = regmap_read(map, MAX17042_QH, &data);
343 val->intval = data * 1000 / 2;
345 case POWER_SUPPLY_PROP_TEMP:
346 ret = max17042_get_temperature(chip, &val->intval);
350 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
351 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
354 /* LSB is Alert Minimum. In deci-centigrade */
355 val->intval = sign_extend32(data & 0xff, 7) * 10;
357 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
358 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
361 /* MSB is Alert Maximum. In deci-centigrade */
362 val->intval = sign_extend32(data >> 8, 7) * 10;
364 case POWER_SUPPLY_PROP_TEMP_MIN:
365 val->intval = chip->pdata->temp_min;
367 case POWER_SUPPLY_PROP_TEMP_MAX:
368 val->intval = chip->pdata->temp_max;
370 case POWER_SUPPLY_PROP_HEALTH:
371 ret = max17042_get_battery_health(chip, &val->intval);
375 case POWER_SUPPLY_PROP_SCOPE:
376 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
378 case POWER_SUPPLY_PROP_CURRENT_NOW:
379 if (chip->pdata->enable_current_sense) {
380 ret = regmap_read(map, MAX17042_Current, &data);
384 val->intval = sign_extend32(data, 15);
385 val->intval *= 1562500 / chip->pdata->r_sns;
390 case POWER_SUPPLY_PROP_CURRENT_AVG:
391 if (chip->pdata->enable_current_sense) {
392 ret = regmap_read(map, MAX17042_AvgCurrent, &data);
396 val->intval = sign_extend32(data, 15);
397 val->intval *= 1562500 / chip->pdata->r_sns;
408 static int max17042_set_property(struct power_supply *psy,
409 enum power_supply_property psp,
410 const union power_supply_propval *val)
412 struct max17042_chip *chip = power_supply_get_drvdata(psy);
413 struct regmap *map = chip->regmap;
419 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
420 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
424 /* Input in deci-centigrade, convert to centigrade */
425 temp = val->intval / 10;
426 /* force min < max */
427 if (temp >= (int8_t)(data >> 8))
428 temp = (int8_t)(data >> 8) - 1;
429 /* Write both MAX and MIN ALERT */
430 data = (data & 0xff00) + temp;
431 ret = regmap_write(map, MAX17042_TALRT_Th, data);
433 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
434 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
438 /* Input in Deci-Centigrade, convert to centigrade */
439 temp = val->intval / 10;
440 /* force max > min */
441 if (temp <= (int8_t)(data & 0xff))
442 temp = (int8_t)(data & 0xff) + 1;
443 /* Write both MAX and MIN ALERT */
444 data = (data & 0xff) + (temp << 8);
445 ret = regmap_write(map, MAX17042_TALRT_Th, data);
454 static int max17042_property_is_writeable(struct power_supply *psy,
455 enum power_supply_property psp)
460 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
461 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
471 static void max17042_external_power_changed(struct power_supply *psy)
473 power_supply_changed(psy);
476 static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
483 ret = regmap_write(map, reg, value);
484 regmap_read(map, reg, &read_value);
485 if (read_value != value) {
489 } while (retries && read_value != value);
492 pr_err("%s: err %d\n", __func__, ret);
497 static inline void max17042_override_por(struct regmap *map,
501 regmap_write(map, reg, value);
504 static inline void max10742_unlock_model(struct max17042_chip *chip)
506 struct regmap *map = chip->regmap;
508 regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
509 regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
512 static inline void max10742_lock_model(struct max17042_chip *chip)
514 struct regmap *map = chip->regmap;
516 regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
517 regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
520 static inline void max17042_write_model_data(struct max17042_chip *chip,
523 struct regmap *map = chip->regmap;
526 for (i = 0; i < size; i++)
527 regmap_write(map, addr + i,
528 chip->pdata->config_data->cell_char_tbl[i]);
531 static inline void max17042_read_model_data(struct max17042_chip *chip,
532 u8 addr, u16 *data, int size)
534 struct regmap *map = chip->regmap;
538 for (i = 0; i < size; i++) {
539 regmap_read(map, addr + i, &tmp);
544 static inline int max17042_model_data_compare(struct max17042_chip *chip,
545 u16 *data1, u16 *data2, int size)
549 if (memcmp(data1, data2, size)) {
550 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
551 for (i = 0; i < size; i++)
552 dev_info(&chip->client->dev, "0x%x, 0x%x",
554 dev_info(&chip->client->dev, "\n");
560 static int max17042_init_model(struct max17042_chip *chip)
563 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
566 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
570 max10742_unlock_model(chip);
571 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
573 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
576 ret = max17042_model_data_compare(
578 chip->pdata->config_data->cell_char_tbl,
582 max10742_lock_model(chip);
588 static int max17042_verify_model_lock(struct max17042_chip *chip)
591 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
595 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
599 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
601 for (i = 0; i < table_size; i++)
609 static void max17042_write_config_regs(struct max17042_chip *chip)
611 struct max17042_config_data *config = chip->pdata->config_data;
612 struct regmap *map = chip->regmap;
614 regmap_write(map, MAX17042_CONFIG, config->config);
615 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
616 regmap_write(map, MAX17042_FilterCFG,
618 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
619 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 ||
620 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)
621 regmap_write(map, MAX17047_FullSOCThr,
622 config->full_soc_thresh);
625 static void max17042_write_custom_regs(struct max17042_chip *chip)
627 struct max17042_config_data *config = chip->pdata->config_data;
628 struct regmap *map = chip->regmap;
630 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
631 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
632 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
633 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) {
634 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
635 max17042_write_verify_reg(map, MAX17042_K_empty0,
638 max17042_write_verify_reg(map, MAX17047_QRTbl00,
640 max17042_write_verify_reg(map, MAX17047_QRTbl10,
642 max17042_write_verify_reg(map, MAX17047_QRTbl20,
644 max17042_write_verify_reg(map, MAX17047_QRTbl30,
649 static void max17042_update_capacity_regs(struct max17042_chip *chip)
651 struct max17042_config_data *config = chip->pdata->config_data;
652 struct regmap *map = chip->regmap;
654 max17042_write_verify_reg(map, MAX17042_FullCAP,
656 regmap_write(map, MAX17042_DesignCap, config->design_cap);
657 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
661 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
664 struct regmap *map = chip->regmap;
666 regmap_read(map, MAX17042_VFSOC, &vfSoc);
667 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
668 max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
669 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
672 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
674 u32 full_cap0, rep_cap, dq_acc, vfSoc;
677 struct max17042_config_data *config = chip->pdata->config_data;
678 struct regmap *map = chip->regmap;
680 regmap_read(map, MAX17042_FullCAP0, &full_cap0);
681 regmap_read(map, MAX17042_VFSOC, &vfSoc);
683 /* fg_vfSoc needs to shifted by 8 bits to get the
684 * perc in 1% accuracy, to get the right rem_cap multiply
685 * full_cap0, fg_vfSoc and devide by 100
687 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
688 max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
691 max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
693 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
694 dq_acc = config->fullcap / dQ_ACC_DIV;
695 max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
696 max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
698 max17042_write_verify_reg(map, MAX17042_FullCAP,
700 regmap_write(map, MAX17042_DesignCap,
702 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
704 /* Update SOC register with new SOC */
705 regmap_write(map, MAX17042_RepSOC, vfSoc);
709 * Block write all the override values coming from platform data.
710 * This function MUST be called before the POR initialization proceedure
711 * specified by maxim.
713 static inline void max17042_override_por_values(struct max17042_chip *chip)
715 struct regmap *map = chip->regmap;
716 struct max17042_config_data *config = chip->pdata->config_data;
718 max17042_override_por(map, MAX17042_TGAIN, config->tgain);
719 max17042_override_por(map, MAx17042_TOFF, config->toff);
720 max17042_override_por(map, MAX17042_CGAIN, config->cgain);
721 max17042_override_por(map, MAX17042_COFF, config->coff);
723 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
724 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
725 max17042_override_por(map, MAX17042_SALRT_Th,
726 config->soc_alrt_thresh);
727 max17042_override_por(map, MAX17042_CONFIG, config->config);
728 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
730 max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
731 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
733 max17042_override_por(map, MAX17042_AtRate, config->at_rate);
734 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
735 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
736 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
737 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
738 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
740 max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
741 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
742 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
743 max17042_override_por(map, MAX17042_SOC_empty,
745 max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
746 max17042_override_por(map, MAX17042_dQacc, config->dqacc);
747 max17042_override_por(map, MAX17042_dPacc, config->dpacc);
749 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
750 max17042_override_por(map, MAX17042_V_empty, config->vempty);
752 max17042_override_por(map, MAX17047_V_empty, config->vempty);
753 max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
754 max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
755 max17042_override_por(map, MAX17042_FCTC, config->fctc);
756 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
757 max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
758 if (chip->chip_type) {
759 max17042_override_por(map, MAX17042_EmptyTempCo,
760 config->empty_tempco);
761 max17042_override_por(map, MAX17042_K_empty0,
766 static int max17042_init_chip(struct max17042_chip *chip)
768 struct regmap *map = chip->regmap;
771 max17042_override_por_values(chip);
772 /* After Power up, the MAX17042 requires 500mS in order
773 * to perform signal debouncing and initial SOC reporting
777 /* Initialize configaration */
778 max17042_write_config_regs(chip);
780 /* write cell characterization data */
781 ret = max17042_init_model(chip);
783 dev_err(&chip->client->dev, "%s init failed\n",
788 ret = max17042_verify_model_lock(chip);
790 dev_err(&chip->client->dev, "%s lock verify failed\n",
794 /* write custom parameters */
795 max17042_write_custom_regs(chip);
797 /* update capacity params */
798 max17042_update_capacity_regs(chip);
800 /* delay must be atleast 350mS to allow VFSOC
801 * to be calculated from the new configuration
805 /* reset vfsoc0 reg */
806 max17042_reset_vfsoc0_reg(chip);
808 /* load new capacity params */
809 max17042_load_new_capacity_params(chip);
811 /* Init complete, Clear the POR bit */
812 regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
816 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
818 struct regmap *map = chip->regmap;
821 /* program interrupt thesholds such that we should
822 * get interrupt for every 'off' perc change in the soc
824 regmap_read(map, MAX17042_RepSOC, &soc);
826 soc_tr = (soc + off) << 8;
827 soc_tr |= (soc - off);
828 regmap_write(map, MAX17042_SALRT_Th, soc_tr);
831 static irqreturn_t max17042_thread_handler(int id, void *dev)
833 struct max17042_chip *chip = dev;
836 regmap_read(chip->regmap, MAX17042_STATUS, &val);
837 if ((val & STATUS_INTR_SOCMIN_BIT) ||
838 (val & STATUS_INTR_SOCMAX_BIT)) {
839 dev_info(&chip->client->dev, "SOC threshold INTR\n");
840 max17042_set_soc_threshold(chip, 1);
843 power_supply_changed(chip->battery);
847 static void max17042_init_worker(struct work_struct *work)
849 struct max17042_chip *chip = container_of(work,
850 struct max17042_chip, work);
853 /* Initialize registers according to values from the platform data */
854 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
855 ret = max17042_init_chip(chip);
860 chip->init_complete = 1;
864 static struct max17042_platform_data *
865 max17042_get_pdata(struct max17042_chip *chip)
867 struct device *dev = &chip->client->dev;
868 struct device_node *np = dev->of_node;
870 struct max17042_platform_data *pdata;
873 return dev->platform_data;
875 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
880 * Require current sense resistor value to be specified for
881 * current-sense functionality to be enabled at all.
883 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
885 pdata->enable_current_sense = true;
888 if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min))
889 pdata->temp_min = INT_MIN;
890 if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max))
891 pdata->temp_max = INT_MAX;
892 if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin))
893 pdata->vmin = INT_MIN;
894 if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax))
895 pdata->vmax = INT_MAX;
900 static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
902 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
903 * when the voltage FG reports 95%, as recommended in the datasheet.
905 { MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 },
908 static struct max17042_platform_data *
909 max17042_get_pdata(struct max17042_chip *chip)
911 struct device *dev = &chip->client->dev;
912 struct max17042_platform_data *pdata;
915 if (dev->platform_data)
916 return dev->platform_data;
919 * The MAX17047 gets used on x86 where we might not have pdata, assume
920 * the firmware will already have initialized the fuel-gauge and provide
921 * default values for the non init bits to make things work.
923 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
927 if (chip->chip_type != MAXIM_DEVICE_TYPE_MAX17042) {
928 pdata->init_data = max17047_default_pdata_init_regs;
929 pdata->num_init_data =
930 ARRAY_SIZE(max17047_default_pdata_init_regs);
933 ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg);
937 /* If bits 0-1 are set to 3 then only Voltage readings are used */
938 if ((misc_cfg & 0x3) == 0x3)
939 pdata->enable_current_sense = false;
941 pdata->enable_current_sense = true;
943 pdata->vmin = MAX17042_DEFAULT_VMIN;
944 pdata->vmax = MAX17042_DEFAULT_VMAX;
945 pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN;
946 pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX;
952 static const struct regmap_config max17042_regmap_config = {
955 .val_format_endian = REGMAP_ENDIAN_NATIVE,
958 static const struct power_supply_desc max17042_psy_desc = {
959 .name = "max170xx_battery",
960 .type = POWER_SUPPLY_TYPE_BATTERY,
961 .get_property = max17042_get_property,
962 .set_property = max17042_set_property,
963 .property_is_writeable = max17042_property_is_writeable,
964 .external_power_changed = max17042_external_power_changed,
965 .properties = max17042_battery_props,
966 .num_properties = ARRAY_SIZE(max17042_battery_props),
969 static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
970 .name = "max170xx_battery",
971 .type = POWER_SUPPLY_TYPE_BATTERY,
972 .get_property = max17042_get_property,
973 .set_property = max17042_set_property,
974 .property_is_writeable = max17042_property_is_writeable,
975 .properties = max17042_battery_props,
976 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
979 static int max17042_probe(struct i2c_client *client,
980 const struct i2c_device_id *id)
982 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
983 const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
984 struct power_supply_config psy_cfg = {};
985 struct max17042_chip *chip;
990 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
993 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
997 chip->client = client;
998 chip->chip_type = id->driver_data;
999 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
1000 if (IS_ERR(chip->regmap)) {
1001 dev_err(&client->dev, "Failed to initialize regmap\n");
1005 chip->pdata = max17042_get_pdata(chip);
1007 dev_err(&client->dev, "no platform data provided\n");
1011 i2c_set_clientdata(client, chip);
1012 psy_cfg.drv_data = chip;
1014 /* When current is not measured,
1015 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
1016 if (!chip->pdata->enable_current_sense)
1017 max17042_desc = &max17042_no_current_sense_psy_desc;
1019 if (chip->pdata->r_sns == 0)
1020 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
1022 if (chip->pdata->init_data)
1023 for (i = 0; i < chip->pdata->num_init_data; i++)
1024 regmap_write(chip->regmap,
1025 chip->pdata->init_data[i].addr,
1026 chip->pdata->init_data[i].data);
1028 if (!chip->pdata->enable_current_sense) {
1029 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
1030 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
1031 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
1034 chip->battery = devm_power_supply_register(&client->dev, max17042_desc,
1036 if (IS_ERR(chip->battery)) {
1037 dev_err(&client->dev, "failed: power supply register\n");
1038 return PTR_ERR(chip->battery);
1042 ret = devm_request_threaded_irq(&client->dev, client->irq,
1044 max17042_thread_handler,
1045 IRQF_TRIGGER_FALLING |
1047 chip->battery->desc->name,
1050 regmap_update_bits(chip->regmap, MAX17042_CONFIG,
1051 CONFIG_ALRT_BIT_ENBL,
1052 CONFIG_ALRT_BIT_ENBL);
1053 max17042_set_soc_threshold(chip, 1);
1056 dev_err(&client->dev, "%s(): cannot get IRQ\n",
1061 regmap_read(chip->regmap, MAX17042_STATUS, &val);
1062 if (val & STATUS_POR_BIT) {
1063 INIT_WORK(&chip->work, max17042_init_worker);
1064 schedule_work(&chip->work);
1066 chip->init_complete = 1;
1072 #ifdef CONFIG_PM_SLEEP
1073 static int max17042_suspend(struct device *dev)
1075 struct max17042_chip *chip = dev_get_drvdata(dev);
1078 * disable the irq and enable irq_wake
1079 * capability to the interrupt line.
1081 if (chip->client->irq) {
1082 disable_irq(chip->client->irq);
1083 enable_irq_wake(chip->client->irq);
1089 static int max17042_resume(struct device *dev)
1091 struct max17042_chip *chip = dev_get_drvdata(dev);
1093 if (chip->client->irq) {
1094 disable_irq_wake(chip->client->irq);
1095 enable_irq(chip->client->irq);
1096 /* re-program the SOC thresholds to 1% change */
1097 max17042_set_soc_threshold(chip, 1);
1104 static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
1108 static const struct of_device_id max17042_dt_match[] = {
1109 { .compatible = "maxim,max17042" },
1110 { .compatible = "maxim,max17047" },
1111 { .compatible = "maxim,max17050" },
1114 MODULE_DEVICE_TABLE(of, max17042_dt_match);
1117 static const struct i2c_device_id max17042_id[] = {
1118 { "max17042", MAXIM_DEVICE_TYPE_MAX17042 },
1119 { "max17047", MAXIM_DEVICE_TYPE_MAX17047 },
1120 { "max17050", MAXIM_DEVICE_TYPE_MAX17050 },
1123 MODULE_DEVICE_TABLE(i2c, max17042_id);
1125 static struct i2c_driver max17042_i2c_driver = {
1128 .of_match_table = of_match_ptr(max17042_dt_match),
1129 .pm = &max17042_pm_ops,
1131 .probe = max17042_probe,
1132 .id_table = max17042_id,
1134 module_i2c_driver(max17042_i2c_driver);
1136 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1137 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1138 MODULE_LICENSE("GPL");