18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Dumb driver for LiIon batteries using TWL4030 madc. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2013 Golden Delicious Computers 68c2ecf20Sopenharmony_ci * Lukas Märdian <lukas@goldelico.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Based on dumb driver for gta01 battery 98c2ecf20Sopenharmony_ci * Copyright 2009 Openmoko, Inc 108c2ecf20Sopenharmony_ci * Balaji Rao <balajirrao@openmoko.org> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/param.h> 158c2ecf20Sopenharmony_ci#include <linux/delay.h> 168c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 178c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 188c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci#include <linux/sort.h> 218c2ecf20Sopenharmony_ci#include <linux/power/twl4030_madc_battery.h> 228c2ecf20Sopenharmony_ci#include <linux/iio/consumer.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistruct twl4030_madc_battery { 258c2ecf20Sopenharmony_ci struct power_supply *psy; 268c2ecf20Sopenharmony_ci struct twl4030_madc_bat_platform_data *pdata; 278c2ecf20Sopenharmony_ci struct iio_channel *channel_temp; 288c2ecf20Sopenharmony_ci struct iio_channel *channel_ichg; 298c2ecf20Sopenharmony_ci struct iio_channel *channel_vbat; 308c2ecf20Sopenharmony_ci}; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_cistatic enum power_supply_property twl4030_madc_bat_props[] = { 338c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_PRESENT, 348c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 358c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TECHNOLOGY, 368c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_VOLTAGE_NOW, 378c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CURRENT_NOW, 388c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CAPACITY, 398c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_FULL, 408c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CHARGE_NOW, 418c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TEMP, 428c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic int madc_read(struct iio_channel *channel) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci int val, err; 488c2ecf20Sopenharmony_ci err = iio_read_channel_processed(channel, &val); 498c2ecf20Sopenharmony_ci if (err < 0) 508c2ecf20Sopenharmony_ci return err; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci return val; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic int twl4030_madc_bat_get_charging_status(struct twl4030_madc_battery *bt) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci return (madc_read(bt->channel_ichg) > 0) ? 1 : 0; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic int twl4030_madc_bat_get_voltage(struct twl4030_madc_battery *bt) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci return madc_read(bt->channel_vbat); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic int twl4030_madc_bat_get_current(struct twl4030_madc_battery *bt) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci return madc_read(bt->channel_ichg) * 1000; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic int twl4030_madc_bat_get_temp(struct twl4030_madc_battery *bt) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci return madc_read(bt->channel_temp) * 10; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic int twl4030_madc_bat_voltscale(struct twl4030_madc_battery *bat, 768c2ecf20Sopenharmony_ci int volt) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci struct twl4030_madc_bat_calibration *calibration; 798c2ecf20Sopenharmony_ci int i, res = 0; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* choose charging curve */ 828c2ecf20Sopenharmony_ci if (twl4030_madc_bat_get_charging_status(bat)) 838c2ecf20Sopenharmony_ci calibration = bat->pdata->charging; 848c2ecf20Sopenharmony_ci else 858c2ecf20Sopenharmony_ci calibration = bat->pdata->discharging; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (volt > calibration[0].voltage) { 888c2ecf20Sopenharmony_ci res = calibration[0].level; 898c2ecf20Sopenharmony_ci } else { 908c2ecf20Sopenharmony_ci for (i = 0; calibration[i+1].voltage >= 0; i++) { 918c2ecf20Sopenharmony_ci if (volt <= calibration[i].voltage && 928c2ecf20Sopenharmony_ci volt >= calibration[i+1].voltage) { 938c2ecf20Sopenharmony_ci /* interval found - interpolate within range */ 948c2ecf20Sopenharmony_ci res = calibration[i].level - 958c2ecf20Sopenharmony_ci ((calibration[i].voltage - volt) * 968c2ecf20Sopenharmony_ci (calibration[i].level - 978c2ecf20Sopenharmony_ci calibration[i+1].level)) / 988c2ecf20Sopenharmony_ci (calibration[i].voltage - 998c2ecf20Sopenharmony_ci calibration[i+1].voltage); 1008c2ecf20Sopenharmony_ci break; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci return res; 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic int twl4030_madc_bat_get_property(struct power_supply *psy, 1088c2ecf20Sopenharmony_ci enum power_supply_property psp, 1098c2ecf20Sopenharmony_ci union power_supply_propval *val) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct twl4030_madc_battery *bat = power_supply_get_drvdata(psy); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci switch (psp) { 1148c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 1158c2ecf20Sopenharmony_ci if (twl4030_madc_bat_voltscale(bat, 1168c2ecf20Sopenharmony_ci twl4030_madc_bat_get_voltage(bat)) > 95) 1178c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_FULL; 1188c2ecf20Sopenharmony_ci else { 1198c2ecf20Sopenharmony_ci if (twl4030_madc_bat_get_charging_status(bat)) 1208c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_CHARGING; 1218c2ecf20Sopenharmony_ci else 1228c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci break; 1258c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1268c2ecf20Sopenharmony_ci val->intval = twl4030_madc_bat_get_voltage(bat) * 1000; 1278c2ecf20Sopenharmony_ci break; 1288c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TECHNOLOGY: 1298c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 1308c2ecf20Sopenharmony_ci break; 1318c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CURRENT_NOW: 1328c2ecf20Sopenharmony_ci val->intval = twl4030_madc_bat_get_current(bat); 1338c2ecf20Sopenharmony_ci break; 1348c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_PRESENT: 1358c2ecf20Sopenharmony_ci /* assume battery is always present */ 1368c2ecf20Sopenharmony_ci val->intval = 1; 1378c2ecf20Sopenharmony_ci break; 1388c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_NOW: { 1398c2ecf20Sopenharmony_ci int percent = twl4030_madc_bat_voltscale(bat, 1408c2ecf20Sopenharmony_ci twl4030_madc_bat_get_voltage(bat)); 1418c2ecf20Sopenharmony_ci val->intval = (percent * bat->pdata->capacity) / 100; 1428c2ecf20Sopenharmony_ci break; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CAPACITY: 1458c2ecf20Sopenharmony_ci val->intval = twl4030_madc_bat_voltscale(bat, 1468c2ecf20Sopenharmony_ci twl4030_madc_bat_get_voltage(bat)); 1478c2ecf20Sopenharmony_ci break; 1488c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CHARGE_FULL: 1498c2ecf20Sopenharmony_ci val->intval = bat->pdata->capacity; 1508c2ecf20Sopenharmony_ci break; 1518c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TEMP: 1528c2ecf20Sopenharmony_ci val->intval = twl4030_madc_bat_get_temp(bat); 1538c2ecf20Sopenharmony_ci break; 1548c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: { 1558c2ecf20Sopenharmony_ci int percent = twl4030_madc_bat_voltscale(bat, 1568c2ecf20Sopenharmony_ci twl4030_madc_bat_get_voltage(bat)); 1578c2ecf20Sopenharmony_ci /* in mAh */ 1588c2ecf20Sopenharmony_ci int chg = (percent * (bat->pdata->capacity/1000))/100; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* assume discharge with 400 mA (ca. 1.5W) */ 1618c2ecf20Sopenharmony_ci val->intval = (3600l * chg) / 400; 1628c2ecf20Sopenharmony_ci break; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci default: 1658c2ecf20Sopenharmony_ci return -EINVAL; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci return 0; 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_cistatic void twl4030_madc_bat_ext_changed(struct power_supply *psy) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci power_supply_changed(psy); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic const struct power_supply_desc twl4030_madc_bat_desc = { 1778c2ecf20Sopenharmony_ci .name = "twl4030_battery", 1788c2ecf20Sopenharmony_ci .type = POWER_SUPPLY_TYPE_BATTERY, 1798c2ecf20Sopenharmony_ci .properties = twl4030_madc_bat_props, 1808c2ecf20Sopenharmony_ci .num_properties = ARRAY_SIZE(twl4030_madc_bat_props), 1818c2ecf20Sopenharmony_ci .get_property = twl4030_madc_bat_get_property, 1828c2ecf20Sopenharmony_ci .external_power_changed = twl4030_madc_bat_ext_changed, 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci}; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic int twl4030_cmp(const void *a, const void *b) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci return ((struct twl4030_madc_bat_calibration *)b)->voltage - 1898c2ecf20Sopenharmony_ci ((struct twl4030_madc_bat_calibration *)a)->voltage; 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic int twl4030_madc_battery_probe(struct platform_device *pdev) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct twl4030_madc_battery *twl4030_madc_bat; 1958c2ecf20Sopenharmony_ci struct twl4030_madc_bat_platform_data *pdata = pdev->dev.platform_data; 1968c2ecf20Sopenharmony_ci struct power_supply_config psy_cfg = {}; 1978c2ecf20Sopenharmony_ci int ret = 0; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci twl4030_madc_bat = devm_kzalloc(&pdev->dev, sizeof(*twl4030_madc_bat), 2008c2ecf20Sopenharmony_ci GFP_KERNEL); 2018c2ecf20Sopenharmony_ci if (!twl4030_madc_bat) 2028c2ecf20Sopenharmony_ci return -ENOMEM; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci twl4030_madc_bat->channel_temp = iio_channel_get(&pdev->dev, "temp"); 2058c2ecf20Sopenharmony_ci if (IS_ERR(twl4030_madc_bat->channel_temp)) { 2068c2ecf20Sopenharmony_ci ret = PTR_ERR(twl4030_madc_bat->channel_temp); 2078c2ecf20Sopenharmony_ci goto err; 2088c2ecf20Sopenharmony_ci } 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci twl4030_madc_bat->channel_ichg = iio_channel_get(&pdev->dev, "ichg"); 2118c2ecf20Sopenharmony_ci if (IS_ERR(twl4030_madc_bat->channel_ichg)) { 2128c2ecf20Sopenharmony_ci ret = PTR_ERR(twl4030_madc_bat->channel_ichg); 2138c2ecf20Sopenharmony_ci goto err_temp; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci twl4030_madc_bat->channel_vbat = iio_channel_get(&pdev->dev, "vbat"); 2178c2ecf20Sopenharmony_ci if (IS_ERR(twl4030_madc_bat->channel_vbat)) { 2188c2ecf20Sopenharmony_ci ret = PTR_ERR(twl4030_madc_bat->channel_vbat); 2198c2ecf20Sopenharmony_ci goto err_ichg; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* sort charging and discharging calibration data */ 2238c2ecf20Sopenharmony_ci sort(pdata->charging, pdata->charging_size, 2248c2ecf20Sopenharmony_ci sizeof(struct twl4030_madc_bat_calibration), 2258c2ecf20Sopenharmony_ci twl4030_cmp, NULL); 2268c2ecf20Sopenharmony_ci sort(pdata->discharging, pdata->discharging_size, 2278c2ecf20Sopenharmony_ci sizeof(struct twl4030_madc_bat_calibration), 2288c2ecf20Sopenharmony_ci twl4030_cmp, NULL); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci twl4030_madc_bat->pdata = pdata; 2318c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, twl4030_madc_bat); 2328c2ecf20Sopenharmony_ci psy_cfg.drv_data = twl4030_madc_bat; 2338c2ecf20Sopenharmony_ci twl4030_madc_bat->psy = power_supply_register(&pdev->dev, 2348c2ecf20Sopenharmony_ci &twl4030_madc_bat_desc, 2358c2ecf20Sopenharmony_ci &psy_cfg); 2368c2ecf20Sopenharmony_ci if (IS_ERR(twl4030_madc_bat->psy)) { 2378c2ecf20Sopenharmony_ci ret = PTR_ERR(twl4030_madc_bat->psy); 2388c2ecf20Sopenharmony_ci goto err_vbat; 2398c2ecf20Sopenharmony_ci } 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci return 0; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cierr_vbat: 2448c2ecf20Sopenharmony_ci iio_channel_release(twl4030_madc_bat->channel_vbat); 2458c2ecf20Sopenharmony_cierr_ichg: 2468c2ecf20Sopenharmony_ci iio_channel_release(twl4030_madc_bat->channel_ichg); 2478c2ecf20Sopenharmony_cierr_temp: 2488c2ecf20Sopenharmony_ci iio_channel_release(twl4030_madc_bat->channel_temp); 2498c2ecf20Sopenharmony_cierr: 2508c2ecf20Sopenharmony_ci return ret; 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic int twl4030_madc_battery_remove(struct platform_device *pdev) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci struct twl4030_madc_battery *bat = platform_get_drvdata(pdev); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci power_supply_unregister(bat->psy); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci iio_channel_release(bat->channel_vbat); 2608c2ecf20Sopenharmony_ci iio_channel_release(bat->channel_ichg); 2618c2ecf20Sopenharmony_ci iio_channel_release(bat->channel_temp); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci return 0; 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistatic struct platform_driver twl4030_madc_battery_driver = { 2678c2ecf20Sopenharmony_ci .driver = { 2688c2ecf20Sopenharmony_ci .name = "twl4030_madc_battery", 2698c2ecf20Sopenharmony_ci }, 2708c2ecf20Sopenharmony_ci .probe = twl4030_madc_battery_probe, 2718c2ecf20Sopenharmony_ci .remove = twl4030_madc_battery_remove, 2728c2ecf20Sopenharmony_ci}; 2738c2ecf20Sopenharmony_cimodule_platform_driver(twl4030_madc_battery_driver); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2768c2ecf20Sopenharmony_ciMODULE_AUTHOR("Lukas Märdian <lukas@goldelico.com>"); 2778c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("twl4030_madc battery driver"); 2788c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:twl4030_madc_battery"); 279