Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Dumb driver for LiIon batteries using TWL4030 madc. |
| 3 | * |
| 4 | * Copyright 2013 Golden Delicious Computers |
| 5 | * Lukas Märdian <lukas@goldelico.com> |
| 6 | * |
| 7 | * Based on dumb driver for gta01 battery |
| 8 | * Copyright 2009 Openmoko, Inc |
| 9 | * Balaji Rao <balajirrao@openmoko.org> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/param.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/workqueue.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/power_supply.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/sort.h> |
| 20 | #include <linux/i2c/twl4030-madc.h> |
| 21 | #include <linux/power/twl4030_madc_battery.h> |
| 22 | |
| 23 | struct twl4030_madc_battery { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 24 | struct power_supply *psy; |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 25 | struct twl4030_madc_bat_platform_data *pdata; |
| 26 | }; |
| 27 | |
| 28 | static enum power_supply_property twl4030_madc_bat_props[] = { |
| 29 | POWER_SUPPLY_PROP_PRESENT, |
| 30 | POWER_SUPPLY_PROP_STATUS, |
| 31 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 32 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 33 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 34 | POWER_SUPPLY_PROP_CAPACITY, |
| 35 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 36 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 37 | POWER_SUPPLY_PROP_TEMP, |
| 38 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 39 | }; |
| 40 | |
| 41 | static int madc_read(int index) |
| 42 | { |
| 43 | struct twl4030_madc_request req; |
| 44 | int val; |
| 45 | |
| 46 | req.channels = index; |
| 47 | req.method = TWL4030_MADC_SW2; |
| 48 | req.type = TWL4030_MADC_WAIT; |
| 49 | req.do_avg = 0; |
| 50 | req.raw = false; |
| 51 | req.func_cb = NULL; |
| 52 | |
| 53 | val = twl4030_madc_conversion(&req); |
| 54 | if (val < 0) |
| 55 | return val; |
| 56 | |
| 57 | return req.rbuf[ffs(index) - 1]; |
| 58 | } |
| 59 | |
| 60 | static int twl4030_madc_bat_get_charging_status(void) |
| 61 | { |
| 62 | return (madc_read(TWL4030_MADC_ICHG) > 0) ? 1 : 0; |
| 63 | } |
| 64 | |
| 65 | static int twl4030_madc_bat_get_voltage(void) |
| 66 | { |
| 67 | return madc_read(TWL4030_MADC_VBAT); |
| 68 | } |
| 69 | |
| 70 | static int twl4030_madc_bat_get_current(void) |
| 71 | { |
| 72 | return madc_read(TWL4030_MADC_ICHG) * 1000; |
| 73 | } |
| 74 | |
| 75 | static int twl4030_madc_bat_get_temp(void) |
| 76 | { |
| 77 | return madc_read(TWL4030_MADC_BTEMP) * 10; |
| 78 | } |
| 79 | |
| 80 | static int twl4030_madc_bat_voltscale(struct twl4030_madc_battery *bat, |
| 81 | int volt) |
| 82 | { |
| 83 | struct twl4030_madc_bat_calibration *calibration; |
| 84 | int i, res = 0; |
| 85 | |
| 86 | /* choose charging curve */ |
| 87 | if (twl4030_madc_bat_get_charging_status()) |
| 88 | calibration = bat->pdata->charging; |
| 89 | else |
| 90 | calibration = bat->pdata->discharging; |
| 91 | |
| 92 | if (volt > calibration[0].voltage) { |
| 93 | res = calibration[0].level; |
| 94 | } else { |
| 95 | for (i = 0; calibration[i+1].voltage >= 0; i++) { |
| 96 | if (volt <= calibration[i].voltage && |
| 97 | volt >= calibration[i+1].voltage) { |
| 98 | /* interval found - interpolate within range */ |
| 99 | res = calibration[i].level - |
| 100 | ((calibration[i].voltage - volt) * |
| 101 | (calibration[i].level - |
| 102 | calibration[i+1].level)) / |
| 103 | (calibration[i].voltage - |
| 104 | calibration[i+1].voltage); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return res; |
| 110 | } |
| 111 | |
| 112 | static int twl4030_madc_bat_get_property(struct power_supply *psy, |
| 113 | enum power_supply_property psp, |
| 114 | union power_supply_propval *val) |
| 115 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 116 | struct twl4030_madc_battery *bat = power_supply_get_drvdata(psy); |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 117 | |
| 118 | switch (psp) { |
| 119 | case POWER_SUPPLY_PROP_STATUS: |
| 120 | if (twl4030_madc_bat_voltscale(bat, |
| 121 | twl4030_madc_bat_get_voltage()) > 95) |
| 122 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 123 | else { |
| 124 | if (twl4030_madc_bat_get_charging_status()) |
| 125 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 126 | else |
| 127 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 128 | } |
| 129 | break; |
| 130 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 131 | val->intval = twl4030_madc_bat_get_voltage() * 1000; |
| 132 | break; |
| 133 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 134 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 135 | break; |
| 136 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 137 | val->intval = twl4030_madc_bat_get_current(); |
| 138 | break; |
| 139 | case POWER_SUPPLY_PROP_PRESENT: |
| 140 | /* assume battery is always present */ |
| 141 | val->intval = 1; |
| 142 | break; |
| 143 | case POWER_SUPPLY_PROP_CHARGE_NOW: { |
| 144 | int percent = twl4030_madc_bat_voltscale(bat, |
| 145 | twl4030_madc_bat_get_voltage()); |
| 146 | val->intval = (percent * bat->pdata->capacity) / 100; |
| 147 | break; |
| 148 | } |
| 149 | case POWER_SUPPLY_PROP_CAPACITY: |
| 150 | val->intval = twl4030_madc_bat_voltscale(bat, |
| 151 | twl4030_madc_bat_get_voltage()); |
| 152 | break; |
| 153 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 154 | val->intval = bat->pdata->capacity; |
| 155 | break; |
| 156 | case POWER_SUPPLY_PROP_TEMP: |
| 157 | val->intval = twl4030_madc_bat_get_temp(); |
| 158 | break; |
| 159 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: { |
| 160 | int percent = twl4030_madc_bat_voltscale(bat, |
| 161 | twl4030_madc_bat_get_voltage()); |
| 162 | /* in mAh */ |
| 163 | int chg = (percent * (bat->pdata->capacity/1000))/100; |
| 164 | |
| 165 | /* assume discharge with 400 mA (ca. 1.5W) */ |
| 166 | val->intval = (3600l * chg) / 400; |
| 167 | break; |
| 168 | } |
| 169 | default: |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static void twl4030_madc_bat_ext_changed(struct power_supply *psy) |
| 177 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 178 | power_supply_changed(psy); |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 181 | static const struct power_supply_desc twl4030_madc_bat_desc = { |
| 182 | .name = "twl4030_battery", |
| 183 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 184 | .properties = twl4030_madc_bat_props, |
| 185 | .num_properties = ARRAY_SIZE(twl4030_madc_bat_props), |
| 186 | .get_property = twl4030_madc_bat_get_property, |
| 187 | .external_power_changed = twl4030_madc_bat_ext_changed, |
| 188 | |
| 189 | }; |
| 190 | |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 191 | static int twl4030_cmp(const void *a, const void *b) |
| 192 | { |
| 193 | return ((struct twl4030_madc_bat_calibration *)b)->voltage - |
| 194 | ((struct twl4030_madc_bat_calibration *)a)->voltage; |
| 195 | } |
| 196 | |
| 197 | static int twl4030_madc_battery_probe(struct platform_device *pdev) |
| 198 | { |
| 199 | struct twl4030_madc_battery *twl4030_madc_bat; |
| 200 | struct twl4030_madc_bat_platform_data *pdata = pdev->dev.platform_data; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 201 | struct power_supply_config psy_cfg = {}; |
Krzysztof Kozlowski | 68c3ed6 | 2015-02-20 14:32:22 +0100 | [diff] [blame] | 202 | int ret = 0; |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 203 | |
| 204 | twl4030_madc_bat = kzalloc(sizeof(*twl4030_madc_bat), GFP_KERNEL); |
| 205 | if (!twl4030_madc_bat) |
| 206 | return -ENOMEM; |
| 207 | |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 208 | /* sort charging and discharging calibration data */ |
| 209 | sort(pdata->charging, pdata->charging_size, |
| 210 | sizeof(struct twl4030_madc_bat_calibration), |
| 211 | twl4030_cmp, NULL); |
| 212 | sort(pdata->discharging, pdata->discharging_size, |
| 213 | sizeof(struct twl4030_madc_bat_calibration), |
| 214 | twl4030_cmp, NULL); |
| 215 | |
| 216 | twl4030_madc_bat->pdata = pdata; |
| 217 | platform_set_drvdata(pdev, twl4030_madc_bat); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 218 | psy_cfg.drv_data = twl4030_madc_bat; |
| 219 | twl4030_madc_bat->psy = power_supply_register(&pdev->dev, |
| 220 | &twl4030_madc_bat_desc, |
| 221 | &psy_cfg); |
| 222 | if (IS_ERR(twl4030_madc_bat->psy)) { |
| 223 | ret = PTR_ERR(twl4030_madc_bat->psy); |
Krzysztof Kozlowski | 68c3ed6 | 2015-02-20 14:32:22 +0100 | [diff] [blame] | 224 | kfree(twl4030_madc_bat); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 225 | } |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 226 | |
Krzysztof Kozlowski | 68c3ed6 | 2015-02-20 14:32:22 +0100 | [diff] [blame] | 227 | return ret; |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static int twl4030_madc_battery_remove(struct platform_device *pdev) |
| 231 | { |
| 232 | struct twl4030_madc_battery *bat = platform_get_drvdata(pdev); |
| 233 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame^] | 234 | power_supply_unregister(bat->psy); |
Marek Belisko | da0a00e | 2013-08-29 11:41:52 +0200 | [diff] [blame] | 235 | kfree(bat); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static struct platform_driver twl4030_madc_battery_driver = { |
| 241 | .driver = { |
| 242 | .name = "twl4030_madc_battery", |
| 243 | }, |
| 244 | .probe = twl4030_madc_battery_probe, |
| 245 | .remove = twl4030_madc_battery_remove, |
| 246 | }; |
| 247 | module_platform_driver(twl4030_madc_battery_driver); |
| 248 | |
| 249 | MODULE_LICENSE("GPL"); |
| 250 | MODULE_AUTHOR("Lukas Märdian <lukas@goldelico.com>"); |
| 251 | MODULE_DESCRIPTION("twl4030_madc battery driver"); |