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>
37 /* Status register bits */
38 #define STATUS_POR_BIT (1 << 1)
39 #define STATUS_BST_BIT (1 << 3)
40 #define STATUS_VMN_BIT (1 << 8)
41 #define STATUS_TMN_BIT (1 << 9)
42 #define STATUS_SMN_BIT (1 << 10)
43 #define STATUS_BI_BIT (1 << 11)
44 #define STATUS_VMX_BIT (1 << 12)
45 #define STATUS_TMX_BIT (1 << 13)
46 #define STATUS_SMX_BIT (1 << 14)
47 #define STATUS_BR_BIT (1 << 15)
49 /* Interrupt mask bits */
50 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
51 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
52 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
54 #define VFSOC0_LOCK 0x0000
55 #define VFSOC0_UNLOCK 0x0080
56 #define MODEL_UNLOCK1 0X0059
57 #define MODEL_UNLOCK2 0X00C4
58 #define MODEL_LOCK1 0X0000
59 #define MODEL_LOCK2 0X0000
61 #define dQ_ACC_DIV 0x4
62 #define dP_ACC_100 0x1900
63 #define dP_ACC_200 0x3200
65 struct max17042_chip {
66 struct i2c_client *client;
67 struct power_supply battery;
68 struct max17042_platform_data *pdata;
69 struct work_struct work;
73 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
75 int ret = i2c_smbus_write_word_data(client, reg, value);
78 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
83 static int max17042_read_reg(struct i2c_client *client, u8 reg)
85 int ret = i2c_smbus_read_word_data(client, reg);
88 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
93 static void max17042_set_reg(struct i2c_client *client,
94 struct max17042_reg_data *data, int size)
98 for (i = 0; i < size; i++)
99 max17042_write_reg(client, data[i].addr, data[i].data);
102 static enum power_supply_property max17042_battery_props[] = {
103 POWER_SUPPLY_PROP_PRESENT,
104 POWER_SUPPLY_PROP_CYCLE_COUNT,
105 POWER_SUPPLY_PROP_VOLTAGE_MAX,
106 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
107 POWER_SUPPLY_PROP_VOLTAGE_NOW,
108 POWER_SUPPLY_PROP_VOLTAGE_AVG,
109 POWER_SUPPLY_PROP_VOLTAGE_OCV,
110 POWER_SUPPLY_PROP_CAPACITY,
111 POWER_SUPPLY_PROP_CHARGE_FULL,
112 POWER_SUPPLY_PROP_TEMP,
113 POWER_SUPPLY_PROP_CURRENT_NOW,
114 POWER_SUPPLY_PROP_CURRENT_AVG,
117 static int max17042_get_property(struct power_supply *psy,
118 enum power_supply_property psp,
119 union power_supply_propval *val)
121 struct max17042_chip *chip = container_of(psy,
122 struct max17042_chip, battery);
125 if (!chip->init_complete)
129 case POWER_SUPPLY_PROP_PRESENT:
130 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
134 if (ret & MAX17042_STATUS_BattAbsent)
139 case POWER_SUPPLY_PROP_CYCLE_COUNT:
140 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
146 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
147 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
151 val->intval = ret >> 8;
152 val->intval *= 20000; /* Units of LSB = 20mV */
154 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
155 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
159 val->intval = ret >> 7;
160 val->intval *= 10000; /* Units of LSB = 10mV */
162 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
163 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
167 val->intval = ret * 625 / 8;
169 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
170 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
174 val->intval = ret * 625 / 8;
176 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
177 ret = max17042_read_reg(chip->client, MAX17042_OCVInternal);
181 val->intval = ret * 625 / 8;
183 case POWER_SUPPLY_PROP_CAPACITY:
184 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
188 val->intval = ret >> 8;
190 case POWER_SUPPLY_PROP_CHARGE_FULL:
191 ret = max17042_read_reg(chip->client, MAX17042_FullCAP);
195 val->intval = ret * 1000 / 2;
197 case POWER_SUPPLY_PROP_TEMP:
198 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
203 /* The value is signed. */
204 if (val->intval & 0x8000) {
205 val->intval = (0x7fff & ~val->intval) + 1;
208 /* The value is converted into deci-centigrade scale */
209 /* Units of LSB = 1 / 256 degree Celsius */
210 val->intval = val->intval * 10 / 256;
212 case POWER_SUPPLY_PROP_CURRENT_NOW:
213 if (chip->pdata->enable_current_sense) {
214 ret = max17042_read_reg(chip->client, MAX17042_Current);
219 if (val->intval & 0x8000) {
221 val->intval = ~val->intval & 0x7fff;
225 val->intval *= 1562500 / chip->pdata->r_sns;
230 case POWER_SUPPLY_PROP_CURRENT_AVG:
231 if (chip->pdata->enable_current_sense) {
232 ret = max17042_read_reg(chip->client,
233 MAX17042_AvgCurrent);
238 if (val->intval & 0x8000) {
240 val->intval = ~val->intval & 0x7fff;
244 val->intval *= 1562500 / chip->pdata->r_sns;
255 static int max17042_write_verify_reg(struct i2c_client *client,
263 ret = i2c_smbus_write_word_data(client, reg, value);
264 read_value = max17042_read_reg(client, reg);
265 if (read_value != value) {
269 } while (retries && read_value != value);
272 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
277 static inline void max17042_override_por(
278 struct i2c_client *client, u8 reg, u16 value)
281 max17042_write_reg(client, reg, value);
284 static inline void max10742_unlock_model(struct max17042_chip *chip)
286 struct i2c_client *client = chip->client;
287 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
288 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
291 static inline void max10742_lock_model(struct max17042_chip *chip)
293 struct i2c_client *client = chip->client;
294 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1);
295 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2);
298 static inline void max17042_write_model_data(struct max17042_chip *chip,
301 struct i2c_client *client = chip->client;
303 for (i = 0; i < size; i++)
304 max17042_write_reg(client, addr + i,
305 chip->pdata->config_data->cell_char_tbl[i]);
308 static inline void max17042_read_model_data(struct max17042_chip *chip,
309 u8 addr, u16 *data, int size)
311 struct i2c_client *client = chip->client;
314 for (i = 0; i < size; i++)
315 data[i] = max17042_read_reg(client, addr + i);
318 static inline int max17042_model_data_compare(struct max17042_chip *chip,
319 u16 *data1, u16 *data2, int size)
323 if (memcmp(data1, data2, size)) {
324 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
325 for (i = 0; i < size; i++)
326 dev_info(&chip->client->dev, "0x%x, 0x%x",
328 dev_info(&chip->client->dev, "\n");
334 static int max17042_init_model(struct max17042_chip *chip)
337 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
340 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
344 max10742_unlock_model(chip);
345 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
347 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
350 ret = max17042_model_data_compare(
352 chip->pdata->config_data->cell_char_tbl,
356 max10742_lock_model(chip);
362 static int max17042_verify_model_lock(struct max17042_chip *chip)
365 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
369 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
373 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
375 for (i = 0; i < table_size; i++)
383 static void max17042_write_config_regs(struct max17042_chip *chip)
385 struct max17042_config_data *config = chip->pdata->config_data;
387 max17042_write_reg(chip->client, MAX17042_CONFIG, config->config);
388 max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg);
389 max17042_write_reg(chip->client, MAX17042_FilterCFG,
391 max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg);
394 static void max17042_write_custom_regs(struct max17042_chip *chip)
396 struct max17042_config_data *config = chip->pdata->config_data;
398 max17042_write_verify_reg(chip->client, MAX17042_RCOMP0,
400 max17042_write_verify_reg(chip->client, MAX17042_TempCo,
402 max17042_write_reg(chip->client, MAX17042_EmptyTempCo,
403 config->empty_tempco);
404 max17042_write_verify_reg(chip->client, MAX17042_K_empty0,
406 max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm,
410 static void max17042_update_capacity_regs(struct max17042_chip *chip)
412 struct max17042_config_data *config = chip->pdata->config_data;
414 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
416 max17042_write_reg(chip->client, MAX17042_DesignCap,
418 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
422 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
426 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
427 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
428 max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc);
429 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
432 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
434 u16 full_cap0, rep_cap, dq_acc, vfSoc;
437 struct max17042_config_data *config = chip->pdata->config_data;
439 full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0);
440 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
442 /* fg_vfSoc needs to shifted by 8 bits to get the
443 * perc in 1% accuracy, to get the right rem_cap multiply
444 * full_cap0, fg_vfSoc and devide by 100
446 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
447 max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap);
449 rep_cap = (u16)rem_cap;
450 max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap);
452 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
453 dq_acc = config->fullcap / dQ_ACC_DIV;
454 max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc);
455 max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200);
457 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
459 max17042_write_reg(chip->client, MAX17042_DesignCap,
461 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
466 * Block write all the override values coming from platform data.
467 * This function MUST be called before the POR initialization proceedure
468 * specified by maxim.
470 static inline void max17042_override_por_values(struct max17042_chip *chip)
472 struct i2c_client *client = chip->client;
473 struct max17042_config_data *config = chip->pdata->config_data;
475 max17042_override_por(client, MAX17042_TGAIN, config->tgain);
476 max17042_override_por(client, MAx17042_TOFF, config->toff);
477 max17042_override_por(client, MAX17042_CGAIN, config->cgain);
478 max17042_override_por(client, MAX17042_COFF, config->coff);
480 max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh);
481 max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh);
482 max17042_override_por(client, MAX17042_SALRT_Th,
483 config->soc_alrt_thresh);
484 max17042_override_por(client, MAX17042_CONFIG, config->config);
485 max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer);
487 max17042_override_por(client, MAX17042_DesignCap, config->design_cap);
488 max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term);
490 max17042_override_por(client, MAX17042_AtRate, config->at_rate);
491 max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg);
492 max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg);
493 max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg);
494 max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg);
495 max17042_override_por(client, MAX17042_MaskSOC, config->masksoc);
497 max17042_override_por(client, MAX17042_FullCAP, config->fullcap);
498 max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom);
499 max17042_override_por(client, MAX17042_SOC_empty, config->socempty);
500 max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty);
501 max17042_override_por(client, MAX17042_dQacc, config->dqacc);
502 max17042_override_por(client, MAX17042_dPacc, config->dpacc);
504 max17042_override_por(client, MAX17042_V_empty, config->vempty);
505 max17042_override_por(client, MAX17042_TempNom, config->temp_nom);
506 max17042_override_por(client, MAX17042_TempLim, config->temp_lim);
507 max17042_override_por(client, MAX17042_FCTC, config->fctc);
508 max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0);
509 max17042_override_por(client, MAX17042_TempCo, config->tcompc0);
510 max17042_override_por(client, MAX17042_EmptyTempCo,
511 config->empty_tempco);
512 max17042_override_por(client, MAX17042_K_empty0, config->kempty0);
515 static int max17042_init_chip(struct max17042_chip *chip)
520 max17042_override_por_values(chip);
521 /* After Power up, the MAX17042 requires 500mS in order
522 * to perform signal debouncing and initial SOC reporting
526 /* Initialize configaration */
527 max17042_write_config_regs(chip);
529 /* write cell characterization data */
530 ret = max17042_init_model(chip);
532 dev_err(&chip->client->dev, "%s init failed\n",
536 max17042_verify_model_lock(chip);
538 dev_err(&chip->client->dev, "%s lock verify failed\n",
542 /* write custom parameters */
543 max17042_write_custom_regs(chip);
545 /* update capacity params */
546 max17042_update_capacity_regs(chip);
548 /* delay must be atleast 350mS to allow VFSOC
549 * to be calculated from the new configuration
553 /* reset vfsoc0 reg */
554 max17042_reset_vfsoc0_reg(chip);
556 /* load new capacity params */
557 max17042_load_new_capacity_params(chip);
559 /* Init complete, Clear the POR bit */
560 val = max17042_read_reg(chip->client, MAX17042_STATUS);
561 max17042_write_reg(chip->client, MAX17042_STATUS,
562 val & (~STATUS_POR_BIT));
566 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
570 /* program interrupt thesholds such that we should
571 * get interrupt for every 'off' perc change in the soc
573 soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8;
574 soc_tr = (soc + off) << 8;
575 soc_tr |= (soc - off);
576 max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr);
579 static irqreturn_t max17042_thread_handler(int id, void *dev)
581 struct max17042_chip *chip = dev;
584 val = max17042_read_reg(chip->client, MAX17042_STATUS);
585 if ((val & STATUS_INTR_SOCMIN_BIT) ||
586 (val & STATUS_INTR_SOCMAX_BIT)) {
587 dev_info(&chip->client->dev, "SOC threshold INTR\n");
588 max17042_set_soc_threshold(chip, 1);
591 power_supply_changed(&chip->battery);
595 static void max17042_init_worker(struct work_struct *work)
597 struct max17042_chip *chip = container_of(work,
598 struct max17042_chip, work);
601 /* Initialize registers according to values from the platform data */
602 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
603 ret = max17042_init_chip(chip);
608 chip->init_complete = 1;
612 static struct max17042_platform_data *
613 max17042_get_pdata(struct device *dev)
615 struct device_node *np = dev->of_node;
617 struct max17042_platform_data *pdata;
620 return dev->platform_data;
622 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
627 * Require current sense resistor value to be specified for
628 * current-sense functionality to be enabled at all.
630 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
632 pdata->enable_current_sense = true;
638 static struct max17042_platform_data *
639 max17042_get_pdata(struct device *dev)
641 return dev->platform_data;
645 static int __devinit max17042_probe(struct i2c_client *client,
646 const struct i2c_device_id *id)
648 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
649 struct max17042_chip *chip;
653 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
656 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
660 chip->client = client;
661 chip->pdata = max17042_get_pdata(&client->dev);
663 dev_err(&client->dev, "no platform data provided\n");
667 i2c_set_clientdata(client, chip);
669 chip->battery.name = "max17042_battery";
670 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
671 chip->battery.get_property = max17042_get_property;
672 chip->battery.properties = max17042_battery_props;
673 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
675 /* When current is not measured,
676 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
677 if (!chip->pdata->enable_current_sense)
678 chip->battery.num_properties -= 2;
680 if (chip->pdata->r_sns == 0)
681 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
683 if (chip->pdata->init_data)
684 max17042_set_reg(client, chip->pdata->init_data,
685 chip->pdata->num_init_data);
687 if (!chip->pdata->enable_current_sense) {
688 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
689 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
690 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
694 ret = request_threaded_irq(client->irq, NULL,
695 max17042_thread_handler,
696 IRQF_TRIGGER_FALLING,
697 chip->battery.name, chip);
699 reg = max17042_read_reg(client, MAX17042_CONFIG);
700 reg |= CONFIG_ALRT_BIT_ENBL;
701 max17042_write_reg(client, MAX17042_CONFIG, reg);
702 max17042_set_soc_threshold(chip, 1);
704 dev_err(&client->dev, "%s(): cannot get IRQ\n",
708 reg = max17042_read_reg(chip->client, MAX17042_STATUS);
710 if (reg & STATUS_POR_BIT) {
711 INIT_WORK(&chip->work, max17042_init_worker);
712 schedule_work(&chip->work);
714 chip->init_complete = 1;
717 ret = power_supply_register(&client->dev, &chip->battery);
719 dev_err(&client->dev, "failed: power supply register\n");
723 static int __devexit max17042_remove(struct i2c_client *client)
725 struct max17042_chip *chip = i2c_get_clientdata(client);
728 free_irq(client->irq, chip);
729 power_supply_unregister(&chip->battery);
734 static int max17042_suspend(struct device *dev)
736 struct max17042_chip *chip = dev_get_drvdata(dev);
739 * disable the irq and enable irq_wake
740 * capability to the interrupt line.
742 if (chip->client->irq) {
743 disable_irq(chip->client->irq);
744 enable_irq_wake(chip->client->irq);
750 static int max17042_resume(struct device *dev)
752 struct max17042_chip *chip = dev_get_drvdata(dev);
754 if (chip->client->irq) {
755 disable_irq_wake(chip->client->irq);
756 enable_irq(chip->client->irq);
757 /* re-program the SOC thresholds to 1% change */
758 max17042_set_soc_threshold(chip, 1);
764 static const struct dev_pm_ops max17042_pm_ops = {
765 .suspend = max17042_suspend,
766 .resume = max17042_resume,
769 #define MAX17042_PM_OPS (&max17042_pm_ops)
771 #define MAX17042_PM_OPS NULL
775 static const struct of_device_id max17042_dt_match[] = {
776 { .compatible = "maxim,max17042" },
779 MODULE_DEVICE_TABLE(of, max17042_dt_match);
782 static const struct i2c_device_id max17042_id[] = {
786 MODULE_DEVICE_TABLE(i2c, max17042_id);
788 static struct i2c_driver max17042_i2c_driver = {
791 .of_match_table = of_match_ptr(max17042_dt_match),
792 .pm = MAX17042_PM_OPS,
794 .probe = max17042_probe,
795 .remove = __devexit_p(max17042_remove),
796 .id_table = max17042_id,
798 module_i2c_driver(max17042_i2c_driver);
800 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
801 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
802 MODULE_LICENSE("GPL");