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 23struct twl4030_madc_battery { 24 struct power_supply psy; 25 struct twl4030_madc_bat_platform_data *pdata; 26}; 27 28static 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 41static 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 60static int twl4030_madc_bat_get_charging_status(void) 61{ 62 return (madc_read(TWL4030_MADC_ICHG) > 0) ? 1 : 0; 63} 64 65static int twl4030_madc_bat_get_voltage(void) 66{ 67 return madc_read(TWL4030_MADC_VBAT); 68} 69 70static int twl4030_madc_bat_get_current(void) 71{ 72 return madc_read(TWL4030_MADC_ICHG) * 1000; 73} 74 75static int twl4030_madc_bat_get_temp(void) 76{ 77 return madc_read(TWL4030_MADC_BTEMP) * 10; 78} 79 80static 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 112static int twl4030_madc_bat_get_property(struct power_supply *psy, 113 enum power_supply_property psp, 114 union power_supply_propval *val) 115{ 116 struct twl4030_madc_battery *bat = container_of(psy, 117 struct twl4030_madc_battery, psy); 118 119 switch (psp) { 120 case POWER_SUPPLY_PROP_STATUS: 121 if (twl4030_madc_bat_voltscale(bat, 122 twl4030_madc_bat_get_voltage()) > 95) 123 val->intval = POWER_SUPPLY_STATUS_FULL; 124 else { 125 if (twl4030_madc_bat_get_charging_status()) 126 val->intval = POWER_SUPPLY_STATUS_CHARGING; 127 else 128 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 129 } 130 break; 131 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 132 val->intval = twl4030_madc_bat_get_voltage() * 1000; 133 break; 134 case POWER_SUPPLY_PROP_TECHNOLOGY: 135 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 136 break; 137 case POWER_SUPPLY_PROP_CURRENT_NOW: 138 val->intval = twl4030_madc_bat_get_current(); 139 break; 140 case POWER_SUPPLY_PROP_PRESENT: 141 /* assume battery is always present */ 142 val->intval = 1; 143 break; 144 case POWER_SUPPLY_PROP_CHARGE_NOW: { 145 int percent = twl4030_madc_bat_voltscale(bat, 146 twl4030_madc_bat_get_voltage()); 147 val->intval = (percent * bat->pdata->capacity) / 100; 148 break; 149 } 150 case POWER_SUPPLY_PROP_CAPACITY: 151 val->intval = twl4030_madc_bat_voltscale(bat, 152 twl4030_madc_bat_get_voltage()); 153 break; 154 case POWER_SUPPLY_PROP_CHARGE_FULL: 155 val->intval = bat->pdata->capacity; 156 break; 157 case POWER_SUPPLY_PROP_TEMP: 158 val->intval = twl4030_madc_bat_get_temp(); 159 break; 160 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: { 161 int percent = twl4030_madc_bat_voltscale(bat, 162 twl4030_madc_bat_get_voltage()); 163 /* in mAh */ 164 int chg = (percent * (bat->pdata->capacity/1000))/100; 165 166 /* assume discharge with 400 mA (ca. 1.5W) */ 167 val->intval = (3600l * chg) / 400; 168 break; 169 } 170 default: 171 return -EINVAL; 172 } 173 174 return 0; 175} 176 177static void twl4030_madc_bat_ext_changed(struct power_supply *psy) 178{ 179 struct twl4030_madc_battery *bat = container_of(psy, 180 struct twl4030_madc_battery, psy); 181 182 power_supply_changed(&bat->psy); 183} 184 185static int twl4030_cmp(const void *a, const void *b) 186{ 187 return ((struct twl4030_madc_bat_calibration *)b)->voltage - 188 ((struct twl4030_madc_bat_calibration *)a)->voltage; 189} 190 191static int twl4030_madc_battery_probe(struct platform_device *pdev) 192{ 193 struct twl4030_madc_battery *twl4030_madc_bat; 194 struct twl4030_madc_bat_platform_data *pdata = pdev->dev.platform_data; 195 196 twl4030_madc_bat = kzalloc(sizeof(*twl4030_madc_bat), GFP_KERNEL); 197 if (!twl4030_madc_bat) 198 return -ENOMEM; 199 200 twl4030_madc_bat->psy.name = "twl4030_battery"; 201 twl4030_madc_bat->psy.type = POWER_SUPPLY_TYPE_BATTERY; 202 twl4030_madc_bat->psy.properties = twl4030_madc_bat_props; 203 twl4030_madc_bat->psy.num_properties = 204 ARRAY_SIZE(twl4030_madc_bat_props); 205 twl4030_madc_bat->psy.get_property = twl4030_madc_bat_get_property; 206 twl4030_madc_bat->psy.external_power_changed = 207 twl4030_madc_bat_ext_changed; 208 209 /* sort charging and discharging calibration data */ 210 sort(pdata->charging, pdata->charging_size, 211 sizeof(struct twl4030_madc_bat_calibration), 212 twl4030_cmp, NULL); 213 sort(pdata->discharging, pdata->discharging_size, 214 sizeof(struct twl4030_madc_bat_calibration), 215 twl4030_cmp, NULL); 216 217 twl4030_madc_bat->pdata = pdata; 218 platform_set_drvdata(pdev, twl4030_madc_bat); 219 power_supply_register(&pdev->dev, &twl4030_madc_bat->psy); 220 221 return 0; 222} 223 224static int twl4030_madc_battery_remove(struct platform_device *pdev) 225{ 226 struct twl4030_madc_battery *bat = platform_get_drvdata(pdev); 227 228 power_supply_unregister(&bat->psy); 229 kfree(bat); 230 231 return 0; 232} 233 234static struct platform_driver twl4030_madc_battery_driver = { 235 .driver = { 236 .name = "twl4030_madc_battery", 237 }, 238 .probe = twl4030_madc_battery_probe, 239 .remove = twl4030_madc_battery_remove, 240}; 241module_platform_driver(twl4030_madc_battery_driver); 242 243MODULE_LICENSE("GPL"); 244MODULE_AUTHOR("Lukas Märdian <lukas@goldelico.com>"); 245MODULE_DESCRIPTION("twl4030_madc battery driver"); 246