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>
35 /* Status register bits */
36 #define STATUS_POR_BIT (1 << 1)
37 #define STATUS_BST_BIT (1 << 3)
38 #define STATUS_VMN_BIT (1 << 8)
39 #define STATUS_TMN_BIT (1 << 9)
40 #define STATUS_SMN_BIT (1 << 10)
41 #define STATUS_BI_BIT (1 << 11)
42 #define STATUS_VMX_BIT (1 << 12)
43 #define STATUS_TMX_BIT (1 << 13)
44 #define STATUS_SMX_BIT (1 << 14)
45 #define STATUS_BR_BIT (1 << 15)
47 /* Interrupt mask bits */
48 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
49 #define STATUS_INTR_SOC_BIT (1 << 14)
50 #define STATUS_INTR_LOW_SOC_BIT (1 << 10)
52 #define VFSOC0_LOCK 0x0000
53 #define VFSOC0_UNLOCK 0x0080
54 #define MODEL_UNLOCK1 0X0059
55 #define MODEL_UNLOCK2 0X00C4
56 #define MODEL_LOCK1 0X0000
57 #define MODEL_LOCK2 0X0000
59 #define dQ_ACC_DIV 0x4
60 #define dP_ACC_100 0x1900
61 #define dP_ACC_200 0x3200
63 struct max17042_chip {
64 struct i2c_client *client;
65 struct power_supply battery;
66 struct max17042_platform_data *pdata;
67 struct work_struct work;
71 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
73 int ret = i2c_smbus_write_word_data(client, reg, value);
76 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
81 static int max17042_read_reg(struct i2c_client *client, u8 reg)
83 int ret = i2c_smbus_read_word_data(client, reg);
86 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
91 static void max17042_set_reg(struct i2c_client *client,
92 struct max17042_reg_data *data, int size)
96 for (i = 0; i < size; i++)
97 max17042_write_reg(client, data[i].addr, data[i].data);
100 static enum power_supply_property max17042_battery_props[] = {
101 POWER_SUPPLY_PROP_PRESENT,
102 POWER_SUPPLY_PROP_CYCLE_COUNT,
103 POWER_SUPPLY_PROP_VOLTAGE_MAX,
104 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
105 POWER_SUPPLY_PROP_VOLTAGE_NOW,
106 POWER_SUPPLY_PROP_VOLTAGE_AVG,
107 POWER_SUPPLY_PROP_CAPACITY,
108 POWER_SUPPLY_PROP_CHARGE_FULL,
109 POWER_SUPPLY_PROP_TEMP,
110 POWER_SUPPLY_PROP_CURRENT_NOW,
111 POWER_SUPPLY_PROP_CURRENT_AVG,
114 static int max17042_get_property(struct power_supply *psy,
115 enum power_supply_property psp,
116 union power_supply_propval *val)
118 struct max17042_chip *chip = container_of(psy,
119 struct max17042_chip, battery);
122 if (!chip->init_complete)
126 case POWER_SUPPLY_PROP_PRESENT:
127 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
131 if (ret & MAX17042_STATUS_BattAbsent)
136 case POWER_SUPPLY_PROP_CYCLE_COUNT:
137 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
143 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
144 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
148 val->intval = ret >> 8;
149 val->intval *= 20000; /* Units of LSB = 20mV */
151 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
152 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
156 val->intval = ret >> 7;
157 val->intval *= 10000; /* Units of LSB = 10mV */
159 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
160 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
164 val->intval = ret * 625 / 8;
166 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
167 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
171 val->intval = ret * 625 / 8;
173 case POWER_SUPPLY_PROP_CAPACITY:
174 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
178 val->intval = ret >> 8;
180 case POWER_SUPPLY_PROP_CHARGE_FULL:
181 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
185 if ((ret >> 8) >= MAX17042_BATTERY_FULL)
190 case POWER_SUPPLY_PROP_TEMP:
191 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
196 /* The value is signed. */
197 if (val->intval & 0x8000) {
198 val->intval = (0x7fff & ~val->intval) + 1;
201 /* The value is converted into deci-centigrade scale */
202 /* Units of LSB = 1 / 256 degree Celsius */
203 val->intval = val->intval * 10 / 256;
205 case POWER_SUPPLY_PROP_CURRENT_NOW:
206 if (chip->pdata->enable_current_sense) {
207 ret = max17042_read_reg(chip->client, MAX17042_Current);
212 if (val->intval & 0x8000) {
214 val->intval = ~val->intval & 0x7fff;
218 val->intval *= 1562500 / chip->pdata->r_sns;
223 case POWER_SUPPLY_PROP_CURRENT_AVG:
224 if (chip->pdata->enable_current_sense) {
225 ret = max17042_read_reg(chip->client,
226 MAX17042_AvgCurrent);
231 if (val->intval & 0x8000) {
233 val->intval = ~val->intval & 0x7fff;
237 val->intval *= 1562500 / chip->pdata->r_sns;
248 static int max17042_write_verify_reg(struct i2c_client *client,
256 ret = i2c_smbus_write_word_data(client, reg, value);
257 read_value = max17042_read_reg(client, reg);
258 if (read_value != value) {
262 } while (retries && read_value != value);
265 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
270 static inline void max17042_override_por(
271 struct i2c_client *client, u8 reg, u16 value)
274 max17042_write_reg(client, reg, value);
277 static inline void max10742_unlock_model(struct max17042_chip *chip)
279 struct i2c_client *client = chip->client;
280 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
281 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
284 static inline void max10742_lock_model(struct max17042_chip *chip)
286 struct i2c_client *client = chip->client;
287 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1);
288 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2);
291 static inline void max17042_write_model_data(struct max17042_chip *chip,
294 struct i2c_client *client = chip->client;
296 for (i = 0; i < size; i++)
297 max17042_write_reg(client, addr + i,
298 chip->pdata->config_data->cell_char_tbl[i]);
301 static inline void max17042_read_model_data(struct max17042_chip *chip,
302 u8 addr, u16 *data, int size)
304 struct i2c_client *client = chip->client;
307 for (i = 0; i < size; i++)
308 data[i] = max17042_read_reg(client, addr + i);
311 static inline int max17042_model_data_compare(struct max17042_chip *chip,
312 u16 *data1, u16 *data2, int size)
316 if (memcmp(data1, data2, size)) {
317 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
318 for (i = 0; i < size; i++)
319 dev_info(&chip->client->dev, "0x%x, 0x%x",
321 dev_info(&chip->client->dev, "\n");
327 static int max17042_init_model(struct max17042_chip *chip)
331 sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16);
334 temp_data = kzalloc(table_size, GFP_KERNEL);
338 max10742_unlock_model(chip);
339 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
341 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
344 ret = max17042_model_data_compare(
346 chip->pdata->config_data->cell_char_tbl,
350 max10742_lock_model(chip);
356 static int max17042_verify_model_lock(struct max17042_chip *chip)
360 sizeof(chip->pdata->config_data->cell_char_tbl);
364 temp_data = kzalloc(table_size, GFP_KERNEL);
368 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
370 for (i = 0; i < table_size; i++)
378 static void max17042_write_config_regs(struct max17042_chip *chip)
380 struct max17042_config_data *config = chip->pdata->config_data;
382 max17042_write_reg(chip->client, MAX17042_CONFIG, config->config);
383 max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg);
384 max17042_write_reg(chip->client, MAX17042_FilterCFG,
386 max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg);
389 static void max17042_write_custom_regs(struct max17042_chip *chip)
391 struct max17042_config_data *config = chip->pdata->config_data;
393 max17042_write_verify_reg(chip->client, MAX17042_RCOMP0,
395 max17042_write_verify_reg(chip->client, MAX17042_TempCo,
397 max17042_write_reg(chip->client, MAX17042_EmptyTempCo,
398 config->empty_tempco);
399 max17042_write_verify_reg(chip->client, MAX17042_K_empty0,
401 max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm,
405 static void max17042_update_capacity_regs(struct max17042_chip *chip)
407 struct max17042_config_data *config = chip->pdata->config_data;
409 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
411 max17042_write_reg(chip->client, MAX17042_DesignCap,
413 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
417 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
421 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
422 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
423 max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc);
424 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
427 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
429 u16 full_cap0, rep_cap, dq_acc, vfSoc;
432 struct max17042_config_data *config = chip->pdata->config_data;
434 full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0);
435 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
437 /* fg_vfSoc needs to shifted by 8 bits to get the
438 * perc in 1% accuracy, to get the right rem_cap multiply
439 * full_cap0, fg_vfSoc and devide by 100
441 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
442 max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap);
444 rep_cap = (u16)rem_cap;
445 max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap);
447 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
448 dq_acc = config->fullcap / dQ_ACC_DIV;
449 max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc);
450 max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200);
452 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
454 max17042_write_reg(chip->client, MAX17042_DesignCap,
456 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
461 * Block write all the override values coming from platform data.
462 * This function MUST be called before the POR initialization proceedure
463 * specified by maxim.
465 static inline void max17042_override_por_values(struct max17042_chip *chip)
467 struct i2c_client *client = chip->client;
468 struct max17042_config_data *config = chip->pdata->config_data;
470 max17042_override_por(client, MAX17042_TGAIN, config->tgain);
471 max17042_override_por(client, MAx17042_TOFF, config->toff);
472 max17042_override_por(client, MAX17042_CGAIN, config->cgain);
473 max17042_override_por(client, MAX17042_COFF, config->coff);
475 max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh);
476 max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh);
477 max17042_override_por(client, MAX17042_SALRT_Th,
478 config->soc_alrt_thresh);
479 max17042_override_por(client, MAX17042_CONFIG, config->config);
480 max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer);
482 max17042_override_por(client, MAX17042_DesignCap, config->design_cap);
483 max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term);
485 max17042_override_por(client, MAX17042_AtRate, config->at_rate);
486 max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg);
487 max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg);
488 max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg);
489 max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg);
490 max17042_override_por(client, MAX17042_MaskSOC, config->masksoc);
492 max17042_override_por(client, MAX17042_FullCAP, config->fullcap);
493 max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom);
494 max17042_override_por(client, MAX17042_SOC_empty, config->socempty);
495 max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty);
496 max17042_override_por(client, MAX17042_dQacc, config->dqacc);
497 max17042_override_por(client, MAX17042_dPacc, config->dpacc);
499 max17042_override_por(client, MAX17042_V_empty, config->vempty);
500 max17042_override_por(client, MAX17042_TempNom, config->temp_nom);
501 max17042_override_por(client, MAX17042_TempLim, config->temp_lim);
502 max17042_override_por(client, MAX17042_FCTC, config->fctc);
503 max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0);
504 max17042_override_por(client, MAX17042_TempCo, config->tcompc0);
505 max17042_override_por(client, MAX17042_EmptyTempCo,
506 config->empty_tempco);
507 max17042_override_por(client, MAX17042_K_empty0, config->kempty0);
510 static int max17042_init_chip(struct max17042_chip *chip)
515 max17042_override_por_values(chip);
516 /* After Power up, the MAX17042 requires 500mS in order
517 * to perform signal debouncing and initial SOC reporting
521 /* Initialize configaration */
522 max17042_write_config_regs(chip);
524 /* write cell characterization data */
525 ret = max17042_init_model(chip);
527 dev_err(&chip->client->dev, "%s init failed\n",
531 max17042_verify_model_lock(chip);
533 dev_err(&chip->client->dev, "%s lock verify failed\n",
537 /* write custom parameters */
538 max17042_write_custom_regs(chip);
540 /* update capacity params */
541 max17042_update_capacity_regs(chip);
543 /* delay must be atleast 350mS to allow VFSOC
544 * to be calculated from the new configuration
548 /* reset vfsoc0 reg */
549 max17042_reset_vfsoc0_reg(chip);
551 /* load new capacity params */
552 max17042_load_new_capacity_params(chip);
554 /* Init complete, Clear the POR bit */
555 val = max17042_read_reg(chip->client, MAX17042_STATUS);
556 max17042_write_reg(chip->client, MAX17042_STATUS,
557 val & (~STATUS_POR_BIT));
561 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
565 /* program interrupt thesholds such that we should
566 * get interrupt for every 'off' perc change in the soc
568 soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8;
569 soc_tr = (soc + off) << 8;
570 soc_tr |= (soc - off);
571 max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr);
574 static irqreturn_t max17042_intr_handler(int id, void *dev)
576 return IRQ_WAKE_THREAD;
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_SOC_BIT) ||
586 (val & STATUS_INTR_LOW_SOC_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;
611 static int __devinit max17042_probe(struct i2c_client *client,
612 const struct i2c_device_id *id)
614 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
615 struct max17042_chip *chip;
619 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
622 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
626 chip->client = client;
627 chip->pdata = client->dev.platform_data;
629 i2c_set_clientdata(client, chip);
631 chip->battery.name = "max17042_battery";
632 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
633 chip->battery.get_property = max17042_get_property;
634 chip->battery.properties = max17042_battery_props;
635 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
637 /* When current is not measured,
638 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
639 if (!chip->pdata->enable_current_sense)
640 chip->battery.num_properties -= 2;
642 if (chip->pdata->r_sns == 0)
643 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
645 if (chip->pdata->init_data)
646 max17042_set_reg(client, chip->pdata->init_data,
647 chip->pdata->num_init_data);
649 if (!chip->pdata->enable_current_sense) {
650 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
651 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
652 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
656 ret = request_threaded_irq(client->irq, max17042_intr_handler,
657 max17042_thread_handler,
658 0, chip->battery.name, chip);
660 reg = max17042_read_reg(client, MAX17042_CONFIG);
661 reg |= CONFIG_ALRT_BIT_ENBL;
662 max17042_write_reg(client, MAX17042_CONFIG, reg);
663 max17042_set_soc_threshold(chip, 1);
665 dev_err(&client->dev, "%s(): cannot get IRQ\n",
669 reg = max17042_read_reg(chip->client, MAX17042_STATUS);
671 if (reg & STATUS_POR_BIT) {
672 INIT_WORK(&chip->work, max17042_init_worker);
673 schedule_work(&chip->work);
675 chip->init_complete = 1;
678 ret = power_supply_register(&client->dev, &chip->battery);
680 dev_err(&client->dev, "failed: power supply register\n");
686 static int __devexit max17042_remove(struct i2c_client *client)
688 struct max17042_chip *chip = i2c_get_clientdata(client);
690 power_supply_unregister(&chip->battery);
695 static const struct i2c_device_id max17042_id[] = {
699 MODULE_DEVICE_TABLE(i2c, max17042_id);
701 static struct i2c_driver max17042_i2c_driver = {
705 .probe = max17042_probe,
706 .remove = __devexit_p(max17042_remove),
707 .id_table = max17042_id,
710 static int __init max17042_init(void)
712 return i2c_add_driver(&max17042_i2c_driver);
714 module_init(max17042_init);
716 static void __exit max17042_exit(void)
718 i2c_del_driver(&max17042_i2c_driver);
720 module_exit(max17042_exit);
722 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
723 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
724 MODULE_LICENSE("GPL");