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>
31 #include <linux/mod_devicetable.h>
32 #include <linux/power_supply.h>
33 #include <linux/power/max17042_battery.h>
36 /* Status register bits */
37 #define STATUS_POR_BIT (1 << 1)
38 #define STATUS_BST_BIT (1 << 3)
39 #define STATUS_VMN_BIT (1 << 8)
40 #define STATUS_TMN_BIT (1 << 9)
41 #define STATUS_SMN_BIT (1 << 10)
42 #define STATUS_BI_BIT (1 << 11)
43 #define STATUS_VMX_BIT (1 << 12)
44 #define STATUS_TMX_BIT (1 << 13)
45 #define STATUS_SMX_BIT (1 << 14)
46 #define STATUS_BR_BIT (1 << 15)
48 /* Interrupt mask bits */
49 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
50 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
51 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
53 #define VFSOC0_LOCK 0x0000
54 #define VFSOC0_UNLOCK 0x0080
55 #define MODEL_UNLOCK1 0X0059
56 #define MODEL_UNLOCK2 0X00C4
57 #define MODEL_LOCK1 0X0000
58 #define MODEL_LOCK2 0X0000
60 #define dQ_ACC_DIV 0x4
61 #define dP_ACC_100 0x1900
62 #define dP_ACC_200 0x3200
64 struct max17042_chip {
65 struct i2c_client *client;
66 struct power_supply battery;
67 struct max17042_platform_data *pdata;
68 struct work_struct work;
72 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
74 int ret = i2c_smbus_write_word_data(client, reg, value);
77 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
82 static int max17042_read_reg(struct i2c_client *client, u8 reg)
84 int ret = i2c_smbus_read_word_data(client, reg);
87 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
92 static void max17042_set_reg(struct i2c_client *client,
93 struct max17042_reg_data *data, int size)
97 for (i = 0; i < size; i++)
98 max17042_write_reg(client, data[i].addr, data[i].data);
101 static enum power_supply_property max17042_battery_props[] = {
102 POWER_SUPPLY_PROP_PRESENT,
103 POWER_SUPPLY_PROP_CYCLE_COUNT,
104 POWER_SUPPLY_PROP_VOLTAGE_MAX,
105 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
106 POWER_SUPPLY_PROP_VOLTAGE_NOW,
107 POWER_SUPPLY_PROP_VOLTAGE_AVG,
108 POWER_SUPPLY_PROP_CAPACITY,
109 POWER_SUPPLY_PROP_CHARGE_FULL,
110 POWER_SUPPLY_PROP_TEMP,
111 POWER_SUPPLY_PROP_CURRENT_NOW,
112 POWER_SUPPLY_PROP_CURRENT_AVG,
115 static int max17042_get_property(struct power_supply *psy,
116 enum power_supply_property psp,
117 union power_supply_propval *val)
119 struct max17042_chip *chip = container_of(psy,
120 struct max17042_chip, battery);
123 if (!chip->init_complete)
127 case POWER_SUPPLY_PROP_PRESENT:
128 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
132 if (ret & MAX17042_STATUS_BattAbsent)
137 case POWER_SUPPLY_PROP_CYCLE_COUNT:
138 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
144 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
145 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
149 val->intval = ret >> 8;
150 val->intval *= 20000; /* Units of LSB = 20mV */
152 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
153 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
157 val->intval = ret >> 7;
158 val->intval *= 10000; /* Units of LSB = 10mV */
160 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
161 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
165 val->intval = ret * 625 / 8;
167 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
168 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
172 val->intval = ret * 625 / 8;
174 case POWER_SUPPLY_PROP_CAPACITY:
175 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
179 val->intval = ret >> 8;
181 case POWER_SUPPLY_PROP_CHARGE_FULL:
182 ret = max17042_read_reg(chip->client, MAX17042_FullCAP);
186 val->intval = ret * 1000 / 2;
188 case POWER_SUPPLY_PROP_TEMP:
189 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
194 /* The value is signed. */
195 if (val->intval & 0x8000) {
196 val->intval = (0x7fff & ~val->intval) + 1;
199 /* The value is converted into deci-centigrade scale */
200 /* Units of LSB = 1 / 256 degree Celsius */
201 val->intval = val->intval * 10 / 256;
203 case POWER_SUPPLY_PROP_CURRENT_NOW:
204 if (chip->pdata->enable_current_sense) {
205 ret = max17042_read_reg(chip->client, MAX17042_Current);
210 if (val->intval & 0x8000) {
212 val->intval = ~val->intval & 0x7fff;
216 val->intval *= 1562500 / chip->pdata->r_sns;
221 case POWER_SUPPLY_PROP_CURRENT_AVG:
222 if (chip->pdata->enable_current_sense) {
223 ret = max17042_read_reg(chip->client,
224 MAX17042_AvgCurrent);
229 if (val->intval & 0x8000) {
231 val->intval = ~val->intval & 0x7fff;
235 val->intval *= 1562500 / chip->pdata->r_sns;
246 static int max17042_write_verify_reg(struct i2c_client *client,
254 ret = i2c_smbus_write_word_data(client, reg, value);
255 read_value = max17042_read_reg(client, reg);
256 if (read_value != value) {
260 } while (retries && read_value != value);
263 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
268 static inline void max17042_override_por(
269 struct i2c_client *client, u8 reg, u16 value)
272 max17042_write_reg(client, reg, value);
275 static inline void max10742_unlock_model(struct max17042_chip *chip)
277 struct i2c_client *client = chip->client;
278 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
279 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
282 static inline void max10742_lock_model(struct max17042_chip *chip)
284 struct i2c_client *client = chip->client;
285 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1);
286 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2);
289 static inline void max17042_write_model_data(struct max17042_chip *chip,
292 struct i2c_client *client = chip->client;
294 for (i = 0; i < size; i++)
295 max17042_write_reg(client, addr + i,
296 chip->pdata->config_data->cell_char_tbl[i]);
299 static inline void max17042_read_model_data(struct max17042_chip *chip,
300 u8 addr, u16 *data, int size)
302 struct i2c_client *client = chip->client;
305 for (i = 0; i < size; i++)
306 data[i] = max17042_read_reg(client, addr + i);
309 static inline int max17042_model_data_compare(struct max17042_chip *chip,
310 u16 *data1, u16 *data2, int size)
314 if (memcmp(data1, data2, size)) {
315 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
316 for (i = 0; i < size; i++)
317 dev_info(&chip->client->dev, "0x%x, 0x%x",
319 dev_info(&chip->client->dev, "\n");
325 static int max17042_init_model(struct max17042_chip *chip)
329 sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16);
332 temp_data = kzalloc(table_size, GFP_KERNEL);
336 max10742_unlock_model(chip);
337 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
339 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
342 ret = max17042_model_data_compare(
344 chip->pdata->config_data->cell_char_tbl,
348 max10742_lock_model(chip);
354 static int max17042_verify_model_lock(struct max17042_chip *chip)
358 sizeof(chip->pdata->config_data->cell_char_tbl);
362 temp_data = kzalloc(table_size, GFP_KERNEL);
366 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
368 for (i = 0; i < table_size; i++)
376 static void max17042_write_config_regs(struct max17042_chip *chip)
378 struct max17042_config_data *config = chip->pdata->config_data;
380 max17042_write_reg(chip->client, MAX17042_CONFIG, config->config);
381 max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg);
382 max17042_write_reg(chip->client, MAX17042_FilterCFG,
384 max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg);
387 static void max17042_write_custom_regs(struct max17042_chip *chip)
389 struct max17042_config_data *config = chip->pdata->config_data;
391 max17042_write_verify_reg(chip->client, MAX17042_RCOMP0,
393 max17042_write_verify_reg(chip->client, MAX17042_TempCo,
395 max17042_write_reg(chip->client, MAX17042_EmptyTempCo,
396 config->empty_tempco);
397 max17042_write_verify_reg(chip->client, MAX17042_K_empty0,
399 max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm,
403 static void max17042_update_capacity_regs(struct max17042_chip *chip)
405 struct max17042_config_data *config = chip->pdata->config_data;
407 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
409 max17042_write_reg(chip->client, MAX17042_DesignCap,
411 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
415 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
419 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
420 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
421 max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc);
422 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
425 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
427 u16 full_cap0, rep_cap, dq_acc, vfSoc;
430 struct max17042_config_data *config = chip->pdata->config_data;
432 full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0);
433 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
435 /* fg_vfSoc needs to shifted by 8 bits to get the
436 * perc in 1% accuracy, to get the right rem_cap multiply
437 * full_cap0, fg_vfSoc and devide by 100
439 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
440 max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap);
442 rep_cap = (u16)rem_cap;
443 max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap);
445 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
446 dq_acc = config->fullcap / dQ_ACC_DIV;
447 max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc);
448 max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200);
450 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
452 max17042_write_reg(chip->client, MAX17042_DesignCap,
454 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
459 * Block write all the override values coming from platform data.
460 * This function MUST be called before the POR initialization proceedure
461 * specified by maxim.
463 static inline void max17042_override_por_values(struct max17042_chip *chip)
465 struct i2c_client *client = chip->client;
466 struct max17042_config_data *config = chip->pdata->config_data;
468 max17042_override_por(client, MAX17042_TGAIN, config->tgain);
469 max17042_override_por(client, MAx17042_TOFF, config->toff);
470 max17042_override_por(client, MAX17042_CGAIN, config->cgain);
471 max17042_override_por(client, MAX17042_COFF, config->coff);
473 max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh);
474 max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh);
475 max17042_override_por(client, MAX17042_SALRT_Th,
476 config->soc_alrt_thresh);
477 max17042_override_por(client, MAX17042_CONFIG, config->config);
478 max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer);
480 max17042_override_por(client, MAX17042_DesignCap, config->design_cap);
481 max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term);
483 max17042_override_por(client, MAX17042_AtRate, config->at_rate);
484 max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg);
485 max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg);
486 max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg);
487 max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg);
488 max17042_override_por(client, MAX17042_MaskSOC, config->masksoc);
490 max17042_override_por(client, MAX17042_FullCAP, config->fullcap);
491 max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom);
492 max17042_override_por(client, MAX17042_SOC_empty, config->socempty);
493 max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty);
494 max17042_override_por(client, MAX17042_dQacc, config->dqacc);
495 max17042_override_por(client, MAX17042_dPacc, config->dpacc);
497 max17042_override_por(client, MAX17042_V_empty, config->vempty);
498 max17042_override_por(client, MAX17042_TempNom, config->temp_nom);
499 max17042_override_por(client, MAX17042_TempLim, config->temp_lim);
500 max17042_override_por(client, MAX17042_FCTC, config->fctc);
501 max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0);
502 max17042_override_por(client, MAX17042_TempCo, config->tcompc0);
503 max17042_override_por(client, MAX17042_EmptyTempCo,
504 config->empty_tempco);
505 max17042_override_por(client, MAX17042_K_empty0, config->kempty0);
508 static int max17042_init_chip(struct max17042_chip *chip)
513 max17042_override_por_values(chip);
514 /* After Power up, the MAX17042 requires 500mS in order
515 * to perform signal debouncing and initial SOC reporting
519 /* Initialize configaration */
520 max17042_write_config_regs(chip);
522 /* write cell characterization data */
523 ret = max17042_init_model(chip);
525 dev_err(&chip->client->dev, "%s init failed\n",
529 max17042_verify_model_lock(chip);
531 dev_err(&chip->client->dev, "%s lock verify failed\n",
535 /* write custom parameters */
536 max17042_write_custom_regs(chip);
538 /* update capacity params */
539 max17042_update_capacity_regs(chip);
541 /* delay must be atleast 350mS to allow VFSOC
542 * to be calculated from the new configuration
546 /* reset vfsoc0 reg */
547 max17042_reset_vfsoc0_reg(chip);
549 /* load new capacity params */
550 max17042_load_new_capacity_params(chip);
552 /* Init complete, Clear the POR bit */
553 val = max17042_read_reg(chip->client, MAX17042_STATUS);
554 max17042_write_reg(chip->client, MAX17042_STATUS,
555 val & (~STATUS_POR_BIT));
559 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
563 /* program interrupt thesholds such that we should
564 * get interrupt for every 'off' perc change in the soc
566 soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8;
567 soc_tr = (soc + off) << 8;
568 soc_tr |= (soc - off);
569 max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr);
572 static irqreturn_t max17042_thread_handler(int id, void *dev)
574 struct max17042_chip *chip = dev;
577 val = max17042_read_reg(chip->client, MAX17042_STATUS);
578 if ((val & STATUS_INTR_SOCMIN_BIT) ||
579 (val & STATUS_INTR_SOCMAX_BIT)) {
580 dev_info(&chip->client->dev, "SOC threshold INTR\n");
581 max17042_set_soc_threshold(chip, 1);
584 power_supply_changed(&chip->battery);
588 static void max17042_init_worker(struct work_struct *work)
590 struct max17042_chip *chip = container_of(work,
591 struct max17042_chip, work);
594 /* Initialize registers according to values from the platform data */
595 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
596 ret = max17042_init_chip(chip);
601 chip->init_complete = 1;
605 static struct max17042_platform_data *
606 max17042_get_pdata(struct device *dev)
608 struct device_node *np = dev->of_node;
610 struct max17042_platform_data *pdata;
613 return dev->platform_data;
615 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
620 * Require current sense resistor value to be specified for
621 * current-sense functionality to be enabled at all.
623 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
625 pdata->enable_current_sense = true;
631 static struct max17042_platform_data *
632 max17042_get_pdata(struct device *dev)
634 return dev->platform_data;
638 static int __devinit max17042_probe(struct i2c_client *client,
639 const struct i2c_device_id *id)
641 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
642 struct max17042_chip *chip;
646 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
649 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
653 chip->client = client;
654 chip->pdata = max17042_get_pdata(&client->dev);
656 dev_err(&client->dev, "no platform data provided\n");
660 i2c_set_clientdata(client, chip);
662 chip->battery.name = "max17042_battery";
663 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
664 chip->battery.get_property = max17042_get_property;
665 chip->battery.properties = max17042_battery_props;
666 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
668 /* When current is not measured,
669 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
670 if (!chip->pdata->enable_current_sense)
671 chip->battery.num_properties -= 2;
673 if (chip->pdata->r_sns == 0)
674 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
676 if (chip->pdata->init_data)
677 max17042_set_reg(client, chip->pdata->init_data,
678 chip->pdata->num_init_data);
680 if (!chip->pdata->enable_current_sense) {
681 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
682 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
683 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
687 ret = request_threaded_irq(client->irq, NULL,
688 max17042_thread_handler,
689 IRQF_TRIGGER_FALLING,
690 chip->battery.name, chip);
692 reg = max17042_read_reg(client, MAX17042_CONFIG);
693 reg |= CONFIG_ALRT_BIT_ENBL;
694 max17042_write_reg(client, MAX17042_CONFIG, reg);
695 max17042_set_soc_threshold(chip, 1);
697 dev_err(&client->dev, "%s(): cannot get IRQ\n",
701 reg = max17042_read_reg(chip->client, MAX17042_STATUS);
703 if (reg & STATUS_POR_BIT) {
704 INIT_WORK(&chip->work, max17042_init_worker);
705 schedule_work(&chip->work);
707 chip->init_complete = 1;
710 ret = power_supply_register(&client->dev, &chip->battery);
712 dev_err(&client->dev, "failed: power supply register\n");
716 static int __devexit max17042_remove(struct i2c_client *client)
718 struct max17042_chip *chip = i2c_get_clientdata(client);
720 power_supply_unregister(&chip->battery);
725 static const struct of_device_id max17042_dt_match[] = {
726 { .compatible = "maxim,max17042" },
729 MODULE_DEVICE_TABLE(of, max17042_dt_match);
732 static const struct i2c_device_id max17042_id[] = {
736 MODULE_DEVICE_TABLE(i2c, max17042_id);
738 static struct i2c_driver max17042_i2c_driver = {
741 .of_match_table = of_match_ptr(max17042_dt_match),
743 .probe = max17042_probe,
744 .remove = __devexit_p(max17042_remove),
745 .id_table = max17042_id,
747 module_i2c_driver(max17042_i2c_driver);
749 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
750 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
751 MODULE_LICENSE("GPL");