2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <asm/unaligned.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/mutex.h>
22 #include <linux/err.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/power_supply.h>
26 #include <linux/slab.h>
27 #include <linux/power/battery-charger-gauge-comm.h>
29 #include <linux/jiffies.h>
30 #include <linux/interrupt.h>
32 #define LC709203F_THERMISTOR_B 0x06
33 #define LC709203F_INITIAL_RSOC 0x07
34 #define LC709203F_TEMPERATURE 0x08
35 #define LC709203F_VOLTAGE 0x09
37 #define LC709203F_ADJUSTMENT_PACK_APPLI 0x0B
38 #define LC709203F_ADJUSTMENT_PACK_THERM 0x0C
39 #define LC709203F_RSOC 0x0D
40 #define LC709203F_INDICATOR_TO_EMPTY 0x0F
42 #define LC709203F_IC_VERSION 0x11
43 #define LC709203F_CHANGE_OF_THE_PARAM 0x12
44 #define LC709203F_ALARM_LOW_CELL_RSOC 0x13
45 #define LC709203F_ALARM_LOW_CELL_VOLT 0x14
46 #define LC709203F_IC_POWER_MODE 0x15
47 #define LC709203F_STATUS_BIT 0x16
48 #define LC709203F_NUM_OF_THE_PARAM 0x1A
50 #define LC709203F_DELAY (30*HZ)
51 #define LC709203F_MAX_REGS 0x1A
53 #define LC709203F_BATTERY_LOW 15
54 #define LC709203F_BATTERY_FULL 100
56 struct lc709203f_platform_data {
65 u32 alert_low_voltage;
66 bool support_battery_current;
69 struct lc709203f_chip {
70 struct i2c_client *client;
71 struct delayed_work work;
72 struct power_supply battery;
73 struct lc709203f_platform_data *pdata;
74 struct battery_gauge_dev *bg_dev;
78 /* battery capacity */
84 /* battery capacity */
91 int shutdown_complete;
97 static int lc709203f_read_word(struct i2c_client *client, u8 reg)
101 ret = i2c_smbus_read_word_data(client, reg);
103 dev_err(&client->dev, "err reading reg: 0x%x, %d\n", reg, ret);
107 static int lc709203f_write_word(struct i2c_client *client, u8 reg, u16 value)
111 ret = i2c_smbus_write_word_data(client, reg, value);
113 dev_err(&client->dev, "err writing 0x%0x, %d\n" , reg, ret);
118 static int lc709203f_get_battery_soc(struct battery_gauge_dev *bg_dev)
120 struct lc709203f_chip *chip = battery_gauge_get_drvdata(bg_dev);
123 val = lc709203f_read_word(chip->client, LC709203F_RSOC);
125 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
127 val = battery_gauge_get_adjusted_soc(chip->bg_dev,
128 chip->pdata->threshold_soc,
129 chip->pdata->maximum_soc, val * 100);
134 static int lc709203f_update_soc_voltage(struct lc709203f_chip *chip)
138 val = lc709203f_read_word(chip->client, LC709203F_VOLTAGE);
140 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
144 val = lc709203f_read_word(chip->client, LC709203F_RSOC);
146 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
148 chip->soc = battery_gauge_get_adjusted_soc(chip->bg_dev,
149 chip->pdata->threshold_soc,
150 chip->pdata->maximum_soc, val * 100);
152 if (chip->soc == LC709203F_BATTERY_FULL && chip->charge_complete) {
153 chip->status = POWER_SUPPLY_STATUS_FULL;
154 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
155 chip->health = POWER_SUPPLY_HEALTH_GOOD;
156 } else if (chip->soc < LC709203F_BATTERY_LOW) {
157 chip->status = chip->lasttime_status;
158 chip->health = POWER_SUPPLY_HEALTH_DEAD;
159 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
161 chip->charge_complete = 0;
162 chip->status = chip->lasttime_status;
163 chip->health = POWER_SUPPLY_HEALTH_GOOD;
164 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
170 static void lc709203f_work(struct work_struct *work)
172 struct lc709203f_chip *chip;
176 chip = container_of(work, struct lc709203f_chip, work.work);
178 mutex_lock(&chip->mutex);
179 if (chip->shutdown_complete) {
180 mutex_unlock(&chip->mutex);
184 lc709203f_update_soc_voltage(chip);
186 if (chip->soc != chip->lasttime_soc ||
187 chip->status != chip->lasttime_status) {
188 chip->lasttime_soc = chip->soc;
189 power_supply_changed(&chip->battery);
192 if (chip->pdata->tz_name) {
193 val = battery_gauge_get_battery_temperature(chip->bg_dev,
196 dev_err(&chip->client->dev, "temp invalid\n");
198 lc709203f_write_word(chip->client, LC709203F_TEMPERATURE
199 , temperature * 10 + 2732);
200 chip->temperature = temperature;
204 mutex_unlock(&chip->mutex);
205 battery_gauge_report_battery_soc(chip->bg_dev, chip->soc);
206 schedule_delayed_work(&chip->work, LC709203F_DELAY);
209 static int lc709203f_get_temperature(struct lc709203f_chip *chip)
215 if (chip->shutdown_complete)
216 return chip->temperature;
218 for (i = 0; i < retries; i++) {
219 val = lc709203f_read_word(chip->client, LC709203F_TEMPERATURE);
226 dev_err(&chip->client->dev, "%s: err %d\n", __func__, val);
227 if (chip->read_failed > 50)
229 return chip->temperature;
231 chip->read_failed = 0;;
232 chip->temperature = val;
236 static enum power_supply_property lc709203f_battery_props[] = {
237 POWER_SUPPLY_PROP_TECHNOLOGY,
238 POWER_SUPPLY_PROP_STATUS,
239 POWER_SUPPLY_PROP_VOLTAGE_NOW,
240 POWER_SUPPLY_PROP_CAPACITY,
241 POWER_SUPPLY_PROP_HEALTH,
242 POWER_SUPPLY_PROP_PRESENT,
243 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
244 POWER_SUPPLY_PROP_TEMP,
245 POWER_SUPPLY_PROP_CURRENT_NOW,
248 static int lc709203f_get_property(struct power_supply *psy,
249 enum power_supply_property psp,
250 union power_supply_propval *val)
252 struct lc709203f_chip *chip = container_of(psy,
253 struct lc709203f_chip, battery);
258 mutex_lock(&chip->mutex);
261 case POWER_SUPPLY_PROP_TECHNOLOGY:
262 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
264 case POWER_SUPPLY_PROP_STATUS:
265 val->intval = chip->status;
267 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
268 val->intval = 1000 * chip->vcell;
270 case POWER_SUPPLY_PROP_CAPACITY:
271 val->intval = chip->soc;
273 dev_warn(&chip->client->dev,
274 "\nSystem Running low on battery - 15 percent\n");
276 dev_warn(&chip->client->dev,
277 "\nSystem Running low on battery - 10 percent\n");
279 dev_warn(&chip->client->dev,
280 "\nSystem Running low on battery - 5 percent\n");
282 case POWER_SUPPLY_PROP_HEALTH:
283 val->intval = chip->health;
285 case POWER_SUPPLY_PROP_PRESENT:
288 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
289 val->intval = chip->capacity_level;
291 case POWER_SUPPLY_PROP_TEMP:
292 temperature = lc709203f_get_temperature(chip);
294 Temp ready by device is deci-kelvin
296 Report temp in dec-celcius.
298 val->intval = temperature - 2732;
300 case POWER_SUPPLY_PROP_CURRENT_NOW:
302 ret = battery_gauge_get_battery_current(chip->bg_dev, &curr_ma);
304 val->intval = 1000 * curr_ma;
311 mutex_unlock(&chip->mutex);
315 static int lc709203f_update_battery_status(struct battery_gauge_dev *bg_dev,
316 enum battery_charger_status status)
318 struct lc709203f_chip *chip = battery_gauge_get_drvdata(bg_dev);
321 mutex_lock(&chip->mutex);
322 if (chip->shutdown_complete) {
323 mutex_unlock(&chip->mutex);
327 val = lc709203f_read_word(chip->client, LC709203F_RSOC);
329 dev_err(&chip->client->dev, "%s: err %d\n",
332 chip->soc = battery_gauge_get_adjusted_soc(chip->bg_dev,
333 chip->pdata->threshold_soc,
334 chip->pdata->maximum_soc, val * 100);
336 if (status == BATTERY_CHARGING) {
337 chip->charge_complete = 0;
338 chip->status = POWER_SUPPLY_STATUS_CHARGING;
339 } else if (status == BATTERY_CHARGING_DONE) {
340 if (chip->soc == LC709203F_BATTERY_FULL) {
341 chip->charge_complete = 1;
342 chip->status = POWER_SUPPLY_STATUS_FULL;
343 chip->capacity_level =
344 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
348 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
349 chip->charge_complete = 0;
351 chip->lasttime_status = chip->status;
354 mutex_unlock(&chip->mutex);
355 power_supply_changed(&chip->battery);
356 dev_info(&chip->client->dev,
357 "%s() Battery status: %d and SoC: %d%% UI status: %d\n",
358 __func__, status, chip->soc, chip->status);
363 static struct battery_gauge_ops lc709203f_bg_ops = {
364 .update_battery_status = lc709203f_update_battery_status,
365 .get_battery_soc = lc709203f_get_battery_soc,
368 static struct battery_gauge_info lc709203f_bgi = {
370 .bg_ops = &lc709203f_bg_ops,
371 .current_channel_name = "battery-current",
374 static irqreturn_t lc709203f_irq(int id, void *dev)
376 struct lc709203f_chip *chip = dev;
377 struct i2c_client *client = chip->client;
379 dev_info(&client->dev, "%s(): STATUS_VL\n", __func__);
380 /* Forced set SOC 0 to power off */
382 chip->lasttime_soc = chip->soc;
383 chip->status = chip->lasttime_status;
384 chip->health = POWER_SUPPLY_HEALTH_DEAD;
385 chip->capacity_level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
386 power_supply_changed(&chip->battery);
391 static void of_lc709203f_parse_platform_data(struct i2c_client *client,
392 struct lc709203f_platform_data *pdata)
395 struct device_node *np = client->dev.of_node;
399 ret = of_property_read_u32(np, "onsemi,initial-rsoc", &pval);
401 pdata->initial_rsoc = pval;
403 ret = of_property_read_u32(np, "onsemi,appli-adjustment", &pval);
405 pdata->appli_adjustment = pval;
407 pdata->tz_name = NULL;
408 ret = of_property_read_string(np, "onsemi,tz-name", &pstr);
410 pdata->tz_name = pstr;
412 ret = of_property_read_u32(np, "onsemi,thermistor-beta", &pval);
414 pdata->thermistor_beta = pval;
417 dev_warn(&client->dev,
418 "Thermistor beta not provided\n");
421 ret = of_property_read_u32(np, "onsemi,thermistor-adjustment", &pval);
423 pdata->therm_adjustment = pval;
425 ret = of_property_read_u32(np, "onsemi,kernel-threshold-soc", &pval);
427 pdata->threshold_soc = pval;
429 ret = of_property_read_u32(np, "onsemi,kernel-maximum-soc", &pval);
431 pdata->maximum_soc = pval;
433 pdata->maximum_soc = 100;
435 ret = of_property_read_u32(np, "onsemi,alert-low-rsoc", &pval);
437 pdata->alert_low_rsoc = pval;
439 ret = of_property_read_u32(np, "onsemi,alert-low-voltage", &pval);
441 pdata->alert_low_voltage = pval;
443 pdata->support_battery_current = of_property_read_bool(np,
447 #ifdef CONFIG_DEBUG_FS
449 #include <linux/debugfs.h>
450 #include <linux/seq_file.h>
452 static struct dentry *debugfs_root;
453 static u8 valid_command[] = {0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xD, 0xF,
454 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x1A};
455 static int dbg_lc709203f_show(struct seq_file *s, void *data)
457 struct i2c_client *client = s->private;
461 seq_puts(s, "Register-->Value(16bit)\n");
462 for (i = 0; i < ARRAY_SIZE(valid_command); ++i) {
463 ret = lc709203f_read_word(client, valid_command[i]);
465 seq_printf(s, "0x%02x: ERROR\n", valid_command[i]);
467 seq_printf(s, "0x%02x: 0x%04x\n",
468 valid_command[i], ret);
473 static int dbg_lc709203f_open(struct inode *inode, struct file *file)
475 return single_open(file, dbg_lc709203f_show, inode->i_private);
478 static const struct file_operations lc709203f_debug_fops = {
479 .open = dbg_lc709203f_open,
482 .release = single_release,
485 static int lc709203f_debugfs_init(struct i2c_client *client)
487 debugfs_root = debugfs_create_dir("lc709203f", NULL);
489 pr_warn("lc709203f: Failed to create debugfs directory\n");
491 (void) debugfs_create_file("registers", S_IRUGO,
492 debugfs_root, (void *)client, &lc709203f_debug_fops);
496 static int lc709203f_debugfs_init(struct i2c_client *client)
502 static int lc709203f_probe(struct i2c_client *client,
503 const struct i2c_device_id *id)
505 struct lc709203f_chip *chip;
508 /* Required PEC functionality */
509 client->flags = client->flags | I2C_CLIENT_PEC;
511 /* Check if device exist or not */
512 ret = i2c_smbus_read_word_data(client, LC709203F_NUM_OF_THE_PARAM);
514 dev_err(&client->dev, "device is not responding, %d\n", ret);
518 dev_info(&client->dev, "Device Params 0x%04x\n", ret);
520 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
524 chip->client = client;
525 if (client->dev.of_node) {
526 chip->pdata = devm_kzalloc(&client->dev,
527 sizeof(*chip->pdata), GFP_KERNEL);
530 of_lc709203f_parse_platform_data(client, chip->pdata);
532 chip->pdata = client->dev.platform_data;
538 mutex_init(&chip->mutex);
539 chip->shutdown_complete = 0;
540 i2c_set_clientdata(client, chip);
542 if (chip->pdata->initial_rsoc) {
543 ret = lc709203f_write_word(chip->client,
544 LC709203F_INITIAL_RSOC, chip->pdata->initial_rsoc);
546 dev_err(&client->dev,
547 "INITIAL_RSOC write failed: %d\n", ret);
550 dev_info(&client->dev, "initial-rsoc: 0x%04x\n",
551 chip->pdata->initial_rsoc);
554 ret = lc709203f_write_word(chip->client,
555 LC709203F_ALARM_LOW_CELL_RSOC, chip->pdata->alert_low_rsoc);
557 dev_err(&client->dev, "LOW_RSOC write failed: %d\n", ret);
561 ret = lc709203f_write_word(chip->client,
562 LC709203F_ALARM_LOW_CELL_VOLT, chip->pdata->alert_low_voltage);
564 dev_err(&client->dev, "LOW_VOLT write failed: %d\n", ret);
568 if (chip->pdata->appli_adjustment) {
569 ret = lc709203f_write_word(chip->client,
570 LC709203F_ADJUSTMENT_PACK_APPLI,
571 chip->pdata->appli_adjustment);
573 dev_err(&client->dev,
574 "ADJUSTMENT_APPLI write failed: %d\n", ret);
579 if (chip->pdata->tz_name || !chip->pdata->thermistor_beta)
580 goto skip_thermistor_config;
582 if (chip->pdata->therm_adjustment) {
583 ret = lc709203f_write_word(chip->client,
584 LC709203F_ADJUSTMENT_PACK_THERM,
585 chip->pdata->therm_adjustment);
587 dev_err(&client->dev,
588 "ADJUSTMENT_THERM write failed: %d\n", ret);
593 ret = lc709203f_write_word(chip->client,
594 LC709203F_THERMISTOR_B, chip->pdata->thermistor_beta);
596 dev_err(&client->dev, "THERMISTOR_B write failed: %d\n", ret);
600 ret = lc709203f_write_word(chip->client, LC709203F_STATUS_BIT, 0x1);
602 dev_err(&client->dev, "STATUS_BIT write failed: %d\n", ret);
606 skip_thermistor_config:
607 lc709203f_update_soc_voltage(chip);
609 chip->battery.name = "battery";
610 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
611 chip->battery.get_property = lc709203f_get_property;
612 chip->battery.properties = lc709203f_battery_props;
613 chip->battery.num_properties = ARRAY_SIZE(lc709203f_battery_props);
614 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
615 chip->lasttime_status = POWER_SUPPLY_STATUS_DISCHARGING;
616 chip->charge_complete = 0;
618 /* Remove current property if it is not supported */
619 if (!chip->pdata->support_battery_current)
620 chip->battery.num_properties--;
622 ret = power_supply_register(&client->dev, &chip->battery);
624 dev_err(&client->dev, "failed: power supply register\n");
628 lc709203f_bgi.tz_name = chip->pdata->tz_name;
629 chip->bg_dev = battery_gauge_register(&client->dev, &lc709203f_bgi,
631 if (IS_ERR(chip->bg_dev)) {
632 ret = PTR_ERR(chip->bg_dev);
633 dev_err(&client->dev, "battery gauge register failed: %d\n",
638 INIT_DEFERRABLE_WORK(&chip->work, lc709203f_work);
639 schedule_delayed_work(&chip->work, 0);
641 lc709203f_debugfs_init(client);
644 ret = devm_request_threaded_irq(&client->dev, client->irq,
646 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
647 dev_name(&client->dev), chip);
649 dev_err(&client->dev,
650 "%s: request IRQ %d fail, err = %d\n",
651 __func__, client->irq, ret);
656 device_set_wakeup_capable(&client->dev, 1);
658 dev_info(&client->dev, "Battery Voltage %dmV and SoC %d%%\n",
659 chip->vcell, chip->soc);
663 cancel_delayed_work_sync(&chip->work);
665 power_supply_unregister(&chip->battery);
667 mutex_destroy(&chip->mutex);
672 static int lc709203f_remove(struct i2c_client *client)
674 struct lc709203f_chip *chip = i2c_get_clientdata(client);
676 battery_gauge_unregister(chip->bg_dev);
677 power_supply_unregister(&chip->battery);
678 cancel_delayed_work_sync(&chip->work);
679 mutex_destroy(&chip->mutex);
684 static void lc709203f_shutdown(struct i2c_client *client)
686 struct lc709203f_chip *chip = i2c_get_clientdata(client);
688 mutex_lock(&chip->mutex);
689 chip->shutdown_complete = 1;
690 mutex_unlock(&chip->mutex);
692 cancel_delayed_work_sync(&chip->work);
693 dev_info(&chip->client->dev, "At shutdown Voltage %dmV and SoC %d%%\n",
694 chip->vcell, chip->soc);
697 #ifdef CONFIG_PM_SLEEP
698 static int lc709203f_suspend(struct device *dev)
700 struct lc709203f_chip *chip = dev_get_drvdata(dev);
701 cancel_delayed_work_sync(&chip->work);
703 if (device_may_wakeup(&chip->client->dev))
704 enable_irq_wake(chip->client->irq);
706 dev_info(&chip->client->dev, "At suspend Voltage %dmV and SoC %d%%\n",
707 chip->vcell, chip->soc);
712 static int lc709203f_resume(struct device *dev)
714 struct lc709203f_chip *chip = dev_get_drvdata(dev);
716 if (device_may_wakeup(&chip->client->dev))
717 disable_irq_wake(chip->client->irq);
719 mutex_lock(&chip->mutex);
720 lc709203f_update_soc_voltage(chip);
721 mutex_unlock(&chip->mutex);
722 battery_gauge_report_battery_soc(chip->bg_dev, chip->soc);
724 dev_info(&chip->client->dev, "At resume Voltage %dmV and SoC %d%%\n",
725 chip->vcell, chip->soc);
727 schedule_delayed_work(&chip->work, LC709203F_DELAY);
730 #endif /* CONFIG_PM_SLEEP */
732 static SIMPLE_DEV_PM_OPS(lc709203f_pm_ops, lc709203f_suspend, lc709203f_resume);
734 static const struct i2c_device_id lc709203f_id[] = {
738 MODULE_DEVICE_TABLE(i2c, lc709203f_id);
740 static struct i2c_driver lc709203f_i2c_driver = {
743 .pm = &lc709203f_pm_ops,
745 .probe = lc709203f_probe,
746 .remove = lc709203f_remove,
747 .id_table = lc709203f_id,
748 .shutdown = lc709203f_shutdown,
751 static int __init lc709203f_init(void)
753 return i2c_add_driver(&lc709203f_i2c_driver);
755 fs_initcall_sync(lc709203f_init);
757 static void __exit lc709203f_exit(void)
759 i2c_del_driver(&lc709203f_i2c_driver);
761 module_exit(lc709203f_exit);
763 MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
764 MODULE_DESCRIPTION("OnSemi LC709203F Fuel Gauge");
765 MODULE_LICENSE("GPL v2");