Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 1 | /* |
| 2 | * max17040_battery.c |
| 3 | * fuel-gauge systems for lithium-ion (Li+) batteries |
| 4 | * |
| 5 | * Copyright (C) 2009 Samsung Electronics |
| 6 | * Minkyu Kang <mk7.kang@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/power_supply.h> |
| 21 | #include <linux/max17040_battery.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 23 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 24 | #define MAX17040_VCELL 0x02 |
| 25 | #define MAX17040_SOC 0x04 |
| 26 | #define MAX17040_MODE 0x06 |
| 27 | #define MAX17040_VER 0x08 |
| 28 | #define MAX17040_RCOMP 0x0C |
| 29 | #define MAX17040_CMD 0xFE |
| 30 | |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 31 | |
| 32 | #define MAX17040_DELAY 1000 |
| 33 | #define MAX17040_BATTERY_FULL 95 |
| 34 | |
| 35 | struct max17040_chip { |
| 36 | struct i2c_client *client; |
| 37 | struct delayed_work work; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 38 | struct power_supply *battery; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 39 | struct max17040_platform_data *pdata; |
| 40 | |
| 41 | /* State Of Connect */ |
| 42 | int online; |
| 43 | /* battery voltage */ |
| 44 | int vcell; |
| 45 | /* battery capacity */ |
| 46 | int soc; |
| 47 | /* State Of Charge */ |
| 48 | int status; |
| 49 | }; |
| 50 | |
| 51 | static int max17040_get_property(struct power_supply *psy, |
| 52 | enum power_supply_property psp, |
| 53 | union power_supply_propval *val) |
| 54 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 55 | struct max17040_chip *chip = power_supply_get_drvdata(psy); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 56 | |
| 57 | switch (psp) { |
| 58 | case POWER_SUPPLY_PROP_STATUS: |
| 59 | val->intval = chip->status; |
| 60 | break; |
| 61 | case POWER_SUPPLY_PROP_ONLINE: |
| 62 | val->intval = chip->online; |
| 63 | break; |
| 64 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 65 | val->intval = chip->vcell; |
| 66 | break; |
| 67 | case POWER_SUPPLY_PROP_CAPACITY: |
| 68 | val->intval = chip->soc; |
| 69 | break; |
| 70 | default: |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 76 | static int max17040_write_reg(struct i2c_client *client, int reg, u16 value) |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 77 | { |
| 78 | int ret; |
| 79 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 80 | ret = i2c_smbus_write_word_swapped(client, reg, value); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 81 | |
| 82 | if (ret < 0) |
| 83 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int max17040_read_reg(struct i2c_client *client, int reg) |
| 89 | { |
| 90 | int ret; |
| 91 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 92 | ret = i2c_smbus_read_word_swapped(client, reg); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 93 | |
| 94 | if (ret < 0) |
| 95 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static void max17040_reset(struct i2c_client *client) |
| 101 | { |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 102 | max17040_write_reg(client, MAX17040_CMD, 0x0054); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void max17040_get_vcell(struct i2c_client *client) |
| 106 | { |
| 107 | struct max17040_chip *chip = i2c_get_clientdata(client); |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 108 | u16 vcell; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 109 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 110 | vcell = max17040_read_reg(client, MAX17040_VCELL); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 111 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 112 | chip->vcell = vcell; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void max17040_get_soc(struct i2c_client *client) |
| 116 | { |
| 117 | struct max17040_chip *chip = i2c_get_clientdata(client); |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 118 | u16 soc; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 119 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 120 | soc = max17040_read_reg(client, MAX17040_SOC); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 121 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 122 | chip->soc = (soc >> 8); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void max17040_get_version(struct i2c_client *client) |
| 126 | { |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 127 | u16 version; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 128 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 129 | version = max17040_read_reg(client, MAX17040_VER); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 130 | |
Liu Xiang | 14d60bd | 2016-09-21 22:17:34 +0800 | [diff] [blame] | 131 | dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void max17040_get_online(struct i2c_client *client) |
| 135 | { |
| 136 | struct max17040_chip *chip = i2c_get_clientdata(client); |
| 137 | |
Krzysztof Kozlowski | ac323d8 | 2014-01-30 14:32:45 +0100 | [diff] [blame] | 138 | if (chip->pdata && chip->pdata->battery_online) |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 139 | chip->online = chip->pdata->battery_online(); |
| 140 | else |
| 141 | chip->online = 1; |
| 142 | } |
| 143 | |
| 144 | static void max17040_get_status(struct i2c_client *client) |
| 145 | { |
| 146 | struct max17040_chip *chip = i2c_get_clientdata(client); |
| 147 | |
Krzysztof Kozlowski | ac323d8 | 2014-01-30 14:32:45 +0100 | [diff] [blame] | 148 | if (!chip->pdata || !chip->pdata->charger_online |
| 149 | || !chip->pdata->charger_enable) { |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 150 | chip->status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if (chip->pdata->charger_online()) { |
| 155 | if (chip->pdata->charger_enable()) |
| 156 | chip->status = POWER_SUPPLY_STATUS_CHARGING; |
| 157 | else |
| 158 | chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 159 | } else { |
| 160 | chip->status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 161 | } |
| 162 | |
| 163 | if (chip->soc > MAX17040_BATTERY_FULL) |
| 164 | chip->status = POWER_SUPPLY_STATUS_FULL; |
| 165 | } |
| 166 | |
| 167 | static void max17040_work(struct work_struct *work) |
| 168 | { |
| 169 | struct max17040_chip *chip; |
| 170 | |
| 171 | chip = container_of(work, struct max17040_chip, work.work); |
| 172 | |
| 173 | max17040_get_vcell(chip->client); |
| 174 | max17040_get_soc(chip->client); |
| 175 | max17040_get_online(chip->client); |
| 176 | max17040_get_status(chip->client); |
| 177 | |
Krzysztof Kozlowski | 52fa74e | 2015-02-24 10:54:46 +0100 | [diff] [blame] | 178 | queue_delayed_work(system_power_efficient_wq, &chip->work, |
| 179 | MAX17040_DELAY); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static enum power_supply_property max17040_battery_props[] = { |
| 183 | POWER_SUPPLY_PROP_STATUS, |
| 184 | POWER_SUPPLY_PROP_ONLINE, |
| 185 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 186 | POWER_SUPPLY_PROP_CAPACITY, |
| 187 | }; |
| 188 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 189 | static const struct power_supply_desc max17040_battery_desc = { |
| 190 | .name = "battery", |
| 191 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 192 | .get_property = max17040_get_property, |
| 193 | .properties = max17040_battery_props, |
| 194 | .num_properties = ARRAY_SIZE(max17040_battery_props), |
| 195 | }; |
| 196 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 197 | static int max17040_probe(struct i2c_client *client, |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 198 | const struct i2c_device_id *id) |
| 199 | { |
| 200 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 201 | struct power_supply_config psy_cfg = {}; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 202 | struct max17040_chip *chip; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 203 | |
| 204 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 205 | return -EIO; |
| 206 | |
Devendra Naga | 00edfc6 | 2013-01-05 23:53:25 -0500 | [diff] [blame] | 207 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 208 | if (!chip) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | chip->client = client; |
| 212 | chip->pdata = client->dev.platform_data; |
| 213 | |
| 214 | i2c_set_clientdata(client, chip); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 215 | psy_cfg.drv_data = chip; |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 216 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 217 | chip->battery = power_supply_register(&client->dev, |
| 218 | &max17040_battery_desc, &psy_cfg); |
| 219 | if (IS_ERR(chip->battery)) { |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 220 | dev_err(&client->dev, "failed: power supply register\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 221 | return PTR_ERR(chip->battery); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | max17040_reset(client); |
| 225 | max17040_get_version(client); |
| 226 | |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 227 | INIT_DEFERRABLE_WORK(&chip->work, max17040_work); |
Krzysztof Kozlowski | 52fa74e | 2015-02-24 10:54:46 +0100 | [diff] [blame] | 228 | queue_delayed_work(system_power_efficient_wq, &chip->work, |
| 229 | MAX17040_DELAY); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 234 | static int max17040_remove(struct i2c_client *client) |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 235 | { |
| 236 | struct max17040_chip *chip = i2c_get_clientdata(client); |
| 237 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 238 | power_supply_unregister(chip->battery); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 239 | cancel_delayed_work(&chip->work); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 243 | #ifdef CONFIG_PM_SLEEP |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 244 | |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 245 | static int max17040_suspend(struct device *dev) |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 246 | { |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 247 | struct i2c_client *client = to_i2c_client(dev); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 248 | struct max17040_chip *chip = i2c_get_clientdata(client); |
| 249 | |
| 250 | cancel_delayed_work(&chip->work); |
| 251 | return 0; |
| 252 | } |
| 253 | |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 254 | static int max17040_resume(struct device *dev) |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 255 | { |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 256 | struct i2c_client *client = to_i2c_client(dev); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 257 | struct max17040_chip *chip = i2c_get_clientdata(client); |
| 258 | |
Krzysztof Kozlowski | 52fa74e | 2015-02-24 10:54:46 +0100 | [diff] [blame] | 259 | queue_delayed_work(system_power_efficient_wq, &chip->work, |
| 260 | MAX17040_DELAY); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 264 | static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume); |
| 265 | #define MAX17040_PM_OPS (&max17040_pm_ops) |
| 266 | |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 267 | #else |
| 268 | |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 269 | #define MAX17040_PM_OPS NULL |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 270 | |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 271 | #endif /* CONFIG_PM_SLEEP */ |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 272 | |
| 273 | static const struct i2c_device_id max17040_id[] = { |
Krzysztof Kozlowski | 2c33e92 | 2014-09-12 08:53:58 +0200 | [diff] [blame] | 274 | { "max17040" }, |
| 275 | { "max77836-battery" }, |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 276 | { } |
| 277 | }; |
| 278 | MODULE_DEVICE_TABLE(i2c, max17040_id); |
| 279 | |
Javier Martinez Canillas | da28122 | 2017-03-15 00:43:49 -0300 | [diff] [blame] | 280 | static const struct of_device_id max17040_of_match[] = { |
| 281 | { .compatible = "maxim,max17040" }, |
| 282 | { .compatible = "maxim,max77836-battery" }, |
| 283 | { }, |
| 284 | }; |
| 285 | MODULE_DEVICE_TABLE(of, max17040_of_match); |
| 286 | |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 287 | static struct i2c_driver max17040_i2c_driver = { |
| 288 | .driver = { |
| 289 | .name = "max17040", |
Javier Martinez Canillas | da28122 | 2017-03-15 00:43:49 -0300 | [diff] [blame] | 290 | .of_match_table = max17040_of_match, |
Lars-Peter Clausen | 43cf454 | 2013-03-10 14:34:06 +0100 | [diff] [blame] | 291 | .pm = MAX17040_PM_OPS, |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 292 | }, |
| 293 | .probe = max17040_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 294 | .remove = max17040_remove, |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 295 | .id_table = max17040_id, |
| 296 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 297 | module_i2c_driver(max17040_i2c_driver); |
Minkyu Kang | c6f4a42 | 2009-06-05 15:33:04 +0900 | [diff] [blame] | 298 | |
| 299 | MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>"); |
| 300 | MODULE_DESCRIPTION("MAX17040 Fuel Gauge"); |
| 301 | MODULE_LICENSE("GPL"); |