^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Fuel gauge driver for Richtek RT5033
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Beomho Seo <beomho.seo@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mfd/rt5033-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mfd/rt5033.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static int rt5033_battery_get_capacity(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct rt5033_battery *battery = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u32 msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) regmap_read(battery->regmap, RT5033_FUEL_REG_SOC_H, &msb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) return msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int rt5033_battery_get_present(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct rt5033_battery *battery = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) regmap_read(battery->regmap, RT5033_FUEL_REG_CONFIG_L, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return (val & RT5033_FUEL_BAT_PRESENT) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int rt5033_battery_get_watt_prop(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct rt5033_battery *battery = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int regh, regl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u32 msb, lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) regh = RT5033_FUEL_REG_VBAT_H;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) regl = RT5033_FUEL_REG_VBAT_L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) case POWER_SUPPLY_PROP_VOLTAGE_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) regh = RT5033_FUEL_REG_AVG_VOLT_H;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) regl = RT5033_FUEL_REG_AVG_VOLT_L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) case POWER_SUPPLY_PROP_VOLTAGE_OCV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) regh = RT5033_FUEL_REG_OCV_H;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) regl = RT5033_FUEL_REG_OCV_L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) regmap_read(battery->regmap, regh, &msb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) regmap_read(battery->regmap, regl, &lsb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = ((msb << 4) + (lsb >> 4)) * 1250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int rt5033_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct rt5033_battery *battery = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) case POWER_SUPPLY_PROP_VOLTAGE_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case POWER_SUPPLY_PROP_VOLTAGE_OCV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) val->intval = rt5033_battery_get_watt_prop(battery->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) psp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) val->intval = rt5033_battery_get_present(battery->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) val->intval = rt5033_battery_get_capacity(battery->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static enum power_supply_property rt5033_battery_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) POWER_SUPPLY_PROP_VOLTAGE_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) POWER_SUPPLY_PROP_VOLTAGE_OCV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static const struct regmap_config rt5033_battery_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .max_register = RT5033_FUEL_REG_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static const struct power_supply_desc rt5033_battery_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .name = "rt5033-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .get_property = rt5033_battery_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .properties = rt5033_battery_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .num_properties = ARRAY_SIZE(rt5033_battery_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int rt5033_battery_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct rt5033_battery *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) u32 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) battery = devm_kzalloc(&client->dev, sizeof(*battery), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) battery->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) battery->regmap = devm_regmap_init_i2c(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) &rt5033_battery_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (IS_ERR(battery->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_err(&client->dev, "Failed to initialize regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) i2c_set_clientdata(client, battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) psy_cfg.drv_data = battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) battery->psy = power_supply_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) &rt5033_battery_desc, &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (IS_ERR(battery->psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev_err(&client->dev, "Failed to register power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ret = PTR_ERR(battery->psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int rt5033_battery_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct rt5033_battery *battery = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) power_supply_unregister(battery->psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static const struct i2c_device_id rt5033_battery_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) { "rt5033-battery", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) MODULE_DEVICE_TABLE(i2c, rt5033_battery_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static const struct of_device_id rt5033_battery_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) { .compatible = "richtek,rt5033-battery", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_DEVICE_TABLE(of, rt5033_battery_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static struct i2c_driver rt5033_battery_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .name = "rt5033-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .of_match_table = rt5033_battery_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .probe = rt5033_battery_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .remove = rt5033_battery_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .id_table = rt5033_battery_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) module_i2c_driver(rt5033_battery_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) MODULE_DESCRIPTION("Richtek RT5033 fuel gauge driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) MODULE_LICENSE("GPL");