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