2 * max17042_battery.c - 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 not provided / owned by Maxim Integrated Products.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This driver is based on max17040_battery.c
27 #include <linux/irq.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/power_supply.h>
32 #include <linux/power/max17042_battery.h>
34 #define MAX17042_BATTERY_FULL (100)
36 struct max17042_chip {
37 struct i2c_client *client;
38 struct power_supply battery;
39 struct max17042_platform_data *pdata;
40 struct delayed_work dwork;
42 bool irq_masked[MAX17042_IRQ_NR];
47 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
49 int ret = i2c_smbus_write_word_data(client, reg, value);
52 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
57 static int max17042_read_reg(struct i2c_client *client, u8 reg)
59 int ret = i2c_smbus_read_word_data(client, reg);
62 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
67 static void max17042_set_reg(struct i2c_client *client,
68 struct max17042_reg_data *data, int size)
72 for (i = 0; i < size; i++)
73 max17042_write_reg(client, data[i].addr, data[i].data);
76 static void __maybe_unused max17042_dump(struct max17042_chip *chip)
81 for (i = 0; i < 0x50; i++)
82 buf[i] = max17042_read_reg(chip->client, i);
84 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 2, buf,
88 static enum power_supply_property max17042_battery_props[] = {
89 POWER_SUPPLY_PROP_PRESENT,
90 POWER_SUPPLY_PROP_CYCLE_COUNT,
91 POWER_SUPPLY_PROP_VOLTAGE_MAX,
92 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
93 POWER_SUPPLY_PROP_VOLTAGE_NOW,
94 POWER_SUPPLY_PROP_VOLTAGE_AVG,
95 POWER_SUPPLY_PROP_CURRENT_NOW,
96 POWER_SUPPLY_PROP_CURRENT_AVG,
97 POWER_SUPPLY_PROP_CAPACITY,
98 POWER_SUPPLY_PROP_CHARGE_FULL,
99 POWER_SUPPLY_PROP_TEMP,
102 #define MAX17042_HANDLE(reg) \
103 val->intval = max17042_read_reg(chip->client, reg); \
104 if (val->intval < 0) \
106 static int max17042_get_property(struct power_supply *psy,
107 enum power_supply_property psp,
108 union power_supply_propval *val)
110 struct max17042_chip *chip = container_of(psy,
111 struct max17042_chip, battery);
114 case POWER_SUPPLY_PROP_PRESENT:
115 MAX17042_HANDLE(MAX17042_STATUS);
116 if (val->intval & MAX17042_STATUS_BattAbsent)
121 case POWER_SUPPLY_PROP_CYCLE_COUNT:
122 MAX17042_HANDLE(MAX17042_Cycles);
124 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
125 MAX17042_HANDLE(MAX17042_MinMaxVolt);
127 val->intval *= 20000; /* LSB = 20mV */
129 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
130 MAX17042_HANDLE(MAX17042_V_empty);
132 val->intval *= 10000; /* LSB = 10mV */
134 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
135 MAX17042_HANDLE(MAX17042_VCELL);
139 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
140 MAX17042_HANDLE(MAX17042_AvgVCELL);
144 case POWER_SUPPLY_PROP_CURRENT_NOW:
145 /* FIXME: Check COFF,CGAIN are sain. */
146 MAX17042_HANDLE(MAX17042_Current);
147 if (val->intval & 0x8000) {
149 val->intval = ~val->intval & 0x7fff;
153 val->intval *= 1562500 / chip->pdata->r_sns;
155 case POWER_SUPPLY_PROP_CURRENT_AVG:
156 /* FIXME: Check COFF,CGAIN are sain. */
157 MAX17042_HANDLE(MAX17042_AvgCurrent);
158 if (val->intval & 0x8000) {
160 val->intval = ~val->intval & 0x7fff;
164 val->intval *= 1562500 / chip->pdata->r_sns;
166 case POWER_SUPPLY_PROP_CAPACITY:
167 MAX17042_HANDLE(MAX17042_VFSOC);
168 val->intval = (val->intval >> 8);
170 case POWER_SUPPLY_PROP_CHARGE_FULL:
171 /* FIXME: CHARGE_FULL does not mean this! */
172 MAX17042_HANDLE(MAX17042_RepSOC);
173 if ((val->intval / 256) >= MAX17042_BATTERY_FULL)
175 else if (val->intval >= 0)
178 case POWER_SUPPLY_PROP_TEMP:
179 MAX17042_HANDLE(MAX17042_TEMP);
180 /* The value is signed. max17042_read_reg gives u16. */
181 if (val->intval & 0x8000) {
182 val->intval = (0x7fff & ~val->intval) + 1;
185 /* psy wants deci-centigrade */
186 val->intval = val->intval * 10 / 256;
194 static int max17042_set_property(struct power_supply *psy,
195 enum power_supply_property psp,
196 const union power_supply_propval *val)
198 struct max17042_chip *chip = container_of(psy,
199 struct max17042_chip, battery);
203 case POWER_SUPPLY_PROP_CAPACITY:
204 reg = max17042_read_reg(chip->client, MAX17042_MiscCFG);
205 /* 0x0800(Bidb=1) for OCV compensation. Toggle it. */
210 max17042_write_reg(chip->client, MAX17042_MiscCFG, reg);
219 static int max17042_property_is_writeable(struct power_supply *psy,
220 enum power_supply_property psp)
223 case POWER_SUPPLY_PROP_CAPACITY:
232 static void max17042_irq_lock(unsigned int irq)
234 struct max17042_chip *chip = get_irq_chip_data(irq);
236 mutex_lock(&chip->irqlock);
239 static void max17042_irq_sync_unlock(unsigned int irq)
241 struct max17042_chip *chip = get_irq_chip_data(irq);
243 /* The hardware does not have maksing feature. skip caching */
245 mutex_unlock(&chip->irqlock);
248 static void max17042_irq_mask(unsigned int irq)
250 struct max17042_chip *chip = get_irq_chip_data(irq);
252 chip->irq_masked[irq - chip->irq_base] = true;
255 static void max17042_irq_unmask(unsigned int irq)
257 struct max17042_chip *chip = get_irq_chip_data(irq);
259 chip->irq_masked[irq - chip->irq_base] = false;
262 static struct irq_chip max17042_irq_chip = {
263 /* Note that in 2.6.39, this is deprecated */
265 .bus_lock = max17042_irq_lock,
266 .bus_sync_unlock = max17042_irq_sync_unlock,
267 .mask = max17042_irq_mask,
268 .unmask = max17042_irq_unmask,
271 static void max17042_irq_thread_work(struct work_struct *work)
274 bool irq_reg[MAX17042_IRQ_NR] = {};
275 struct delayed_work *dwork = container_of(work, struct delayed_work,
277 struct max17042_chip *chip = container_of(dwork, struct max17042_chip,
280 val = max17042_read_reg(chip->client, MAX17042_STATUS);
281 max17042_write_reg(chip->client, MAX17042_STATUS,
282 val & (MAX17042_STATUS_BattAbsent |
283 MAX17042_STATUS_POR));
286 if (val & MAX17042_STATUS_BattRemove)
287 irq_reg[MAX17042_IRQ_Battery_Removed] = true;
288 if (val & MAX17042_STATUS_BattInsert)
289 irq_reg[MAX17042_IRQ_Battery_Inserted] = true;
290 if (val & MAX17042_STATUS_BattInsert2)
291 irq_reg[MAX17042_IRQ_Battery_Inserted2] = true;
292 if (val & MAX17042_STATUS_SOCMAX)
293 irq_reg[MAX17042_IRQ_SOC_MAX] = true;
294 if (val & MAX17042_STATUS_TempMAX)
295 irq_reg[MAX17042_IRQ_TEMP_MAX] = true;
296 if (val & MAX17042_STATUS_VoltMAX)
297 irq_reg[MAX17042_IRQ_V_MAX] = true;
298 if (val & MAX17042_STATUS_SOCMIN)
299 irq_reg[MAX17042_IRQ_SOC_MIN] = true;
300 if (val & MAX17042_STATUS_TempMIN)
301 irq_reg[MAX17042_IRQ_TEMP_MIN] = true;
302 if (val & MAX17042_STATUS_VoltMIN)
303 irq_reg[MAX17042_IRQ_V_MIN] = true;
305 for (i = 0; i < MAX17042_IRQ_NR; i++) {
306 if (irq_reg[i] && !chip->irq_masked[i])
307 handle_nested_irq(chip->irq_base + i);
311 static irqreturn_t max17042_irq_thread(int irq, void *irq_data)
313 struct max17042_chip *chip = irq_data;
316 * Process 125ms later: datasheet says there is 125ms delay
317 * between ALRT pin responses and STATUS value changes.
318 * Give some margin and use 150ms.
320 schedule_delayed_work(&chip->dwork, msecs_to_jiffies(150));
325 static int max17042_irq_init(struct max17042_chip *chip)
331 INIT_DELAYED_WORK(&chip->dwork, max17042_irq_thread_work);
332 mutex_init(&chip->irqlock);
333 chip->irq_base = chip->pdata->irq_base;
335 /* Register with genirq */
336 for (i = 0; i < MAX17042_IRQ_NR; i++) {
337 chip->irq_masked[i] = true;
338 cur_irq = i + chip->irq_base;
339 set_irq_chip_data(cur_irq, chip);
340 set_irq_chip_and_handler(cur_irq, &max17042_irq_chip,
342 set_irq_nested_thread(cur_irq, 1);
344 set_irq_flags(cur_irq, IRQF_VALID);
346 set_irq_noprobe(cur_irq);
350 if (chip->pdata->alrt_data)
351 max17042_set_reg(chip->client, chip->pdata->alrt_data,
352 chip->pdata->num_alrt_data);
354 ret = request_threaded_irq(chip->client->irq, NULL,
356 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
357 "max17042 fuel alert", chip);
361 value = max17042_read_reg(chip->client, MAX17042_CONFIG);
365 value &= ~MAX17042_CONFIG_AEN;
366 if (chip->pdata->enable_alert)
367 value |= MAX17042_CONFIG_AEN;
369 max17042_write_reg(chip->client, MAX17042_CONFIG, value);
374 static int __devinit max17042_probe(struct i2c_client *client,
375 const struct i2c_device_id *id)
377 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
378 struct max17042_chip *chip;
381 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
384 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
388 chip->client = client;
389 chip->pdata = client->dev.platform_data;
390 chip->reg_dump = NULL;
391 if (chip->pdata->r_sns == 0)
392 chip->pdata->r_sns = 10000;
394 i2c_set_clientdata(client, chip);
396 /* Reset Average Current with Current */
397 ret = max17042_read_reg(client, MAX17042_Current);
400 ret = max17042_write_reg(client, MAX17042_AvgCurrent, ret & 0xffff);
404 chip->battery.name = "max17042_battery";
405 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
406 chip->battery.get_property = max17042_get_property;
407 chip->battery.set_property = max17042_set_property;
408 chip->battery.property_is_writeable = max17042_property_is_writeable;
409 chip->battery.properties = max17042_battery_props;
410 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
412 ret = power_supply_register(&client->dev, &chip->battery);
414 dev_err(&client->dev, "failed: power supply register\n");
418 if (chip->pdata->init_data)
419 max17042_set_reg(client, chip->pdata->init_data,
420 chip->pdata->num_init_data);
422 if (chip->pdata->irq_base && chip->client->irq) {
423 ret = max17042_irq_init(chip);
425 dev_err(&client->dev, "failed: irq init.\n");
432 power_supply_unregister(&chip->battery);
438 static int __devexit max17042_remove(struct i2c_client *client)
440 struct max17042_chip *chip = i2c_get_clientdata(client);
443 kfree(chip->reg_dump);
445 power_supply_unregister(&chip->battery);
446 i2c_set_clientdata(client, NULL);
451 static const struct i2c_device_id max17042_id[] = {
455 MODULE_DEVICE_TABLE(i2c, max17042_id);
457 static int max17042_suspend(struct device *dev)
459 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
460 struct max17042_chip *chip = i2c_get_clientdata(client);
462 if (chip->pdata->wakeup)
463 set_irq_wake(client->irq, 1);
467 static int max17042_resume(struct device *dev)
469 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
470 struct max17042_chip *chip = i2c_get_clientdata(client);
472 if (chip->pdata->wakeup)
473 set_irq_wake(client->irq, 0);
475 if (chip->pdata->irq_base && client->irq)
476 max17042_irq_thread(client->irq, chip);
481 static const u16 save_addr[] = {
510 static int max17042_freeze(struct device *dev)
512 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
513 struct max17042_chip *chip = i2c_get_clientdata(client);
516 if (chip->reg_dump) {
517 dev_err(dev, "Register dump is not clean.\n");
521 chip->reg_dump = kzalloc(sizeof(*chip->reg_dump) * ARRAY_SIZE(save_addr),
523 if (!chip->reg_dump) {
524 dev_err(dev, "Cannot allocate memory for hibernation dump.\n");
528 for (i = 0; i < ARRAY_SIZE(save_addr); i++)
529 chip->reg_dump[i] = max17042_read_reg(client, save_addr[i]) & 0xffff;
534 static int max17042_restore(struct device *dev)
536 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
537 struct max17042_chip *chip = i2c_get_clientdata(client);
540 if (!chip->reg_dump) {
541 dev_err(dev, "Cannot allocate memory for hibernation dump.\n");
545 for (i = 0; i < ARRAY_SIZE(save_addr); i++)
546 max17042_write_reg(client, save_addr[i], chip->reg_dump[i]);
548 kfree(chip->reg_dump);
549 chip->reg_dump = NULL;
554 const struct dev_pm_ops max17042_pm = {
555 .suspend = max17042_suspend,
556 .resume = max17042_resume,
557 .freeze = max17042_freeze,
558 .restore = max17042_restore,
561 static struct i2c_driver max17042_i2c_driver = {
566 .probe = max17042_probe,
567 .remove = __devexit_p(max17042_remove),
568 .id_table = max17042_id,
571 static int __init max17042_init(void)
573 return i2c_add_driver(&max17042_i2c_driver);
575 module_init(max17042_init);
577 static void __exit max17042_exit(void)
579 i2c_del_driver(&max17042_i2c_driver);
581 module_exit(max17042_exit);
583 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
584 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
585 MODULE_LICENSE("GPL");
586 MODULE_ALIAS("max17042");