#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/mod_devicetable.h>
#include <linux/power_supply.h>
#include "adc-battery-helper.h"
#define UG3105_REG_MODE 0x00
#define UG3105_REG_CTRL1 0x01
#define UG3105_REG_COULOMB_CNT 0x02
#define UG3105_REG_BAT_VOLT 0x08
#define UG3105_REG_BAT_CURR 0x0c
#define UG3105_MODE_STANDBY 0x00
#define UG3105_MODE_RUN 0x10
#define UG3105_CTRL1_RESET_COULOMB_CNT 0x03
struct ug3105_chip {
struct adc_battery_helper helper;
struct i2c_client *client;
struct power_supply *psy;
int uv_per_unit;
int ua_per_unit;
};
static int ug3105_read_word(struct i2c_client *client, u8 reg)
{
int val;
val = i2c_smbus_read_word_data(client, reg);
if (val < 0)
dev_err(&client->dev, "Error reading reg 0x%02x\n", reg);
return val;
}
static int ug3105_get_voltage_and_current_now(struct power_supply *psy, int *volt, int *curr)
{
struct ug3105_chip *chip = power_supply_get_drvdata(psy);
int ret;
ret = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT);
if (ret < 0)
return ret;
*volt = ret * chip->uv_per_unit;
ret = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR);
if (ret < 0)
return ret;
*curr = (s16)ret * chip->ua_per_unit;
return 0;
}
static const struct power_supply_desc ug3105_psy_desc = {
.name = "ug3105_battery",
.type = POWER_SUPPLY_TYPE_BATTERY,
.get_property = adc_battery_helper_get_property,
.external_power_changed = adc_battery_helper_external_power_changed,
.properties = adc_battery_helper_properties,
.num_properties = ADC_HELPER_NUM_PROPERTIES,
};
static void ug3105_start(struct i2c_client *client)
{
i2c_smbus_write_byte_data(client, UG3105_REG_MODE, UG3105_MODE_RUN);
i2c_smbus_write_byte_data(client, UG3105_REG_CTRL1, UG3105_CTRL1_RESET_COULOMB_CNT);
}
static void ug3105_stop(struct i2c_client *client)
{
i2c_smbus_write_byte_data(client, UG3105_REG_MODE, UG3105_MODE_STANDBY);
}
static int ug3105_probe(struct i2c_client *client)
{
struct power_supply_config psy_cfg = {};
struct device *dev = &client->dev;
u32 curr_sense_res_uohm = 10000;
struct ug3105_chip *chip;
int ret;
chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
chip->client = client;
ug3105_start(client);
device_property_read_u32(dev, "upisemi,rsns-microohm", &curr_sense_res_uohm);
chip->uv_per_unit = 45000000 / 65536;
chip->ua_per_unit = 8100000 / curr_sense_res_uohm;
psy_cfg.drv_data = chip;
chip->psy = devm_power_supply_register(dev, &ug3105_psy_desc, &psy_cfg);
if (IS_ERR(chip->psy)) {
ret = PTR_ERR(chip->psy);
goto stop;
}
ret = adc_battery_helper_init(&chip->helper, chip->psy,
ug3105_get_voltage_and_current_now, NULL);
if (ret)
goto stop;
i2c_set_clientdata(client, chip);
return 0;
stop:
ug3105_stop(client);
return ret;
}
static int __maybe_unused ug3105_suspend(struct device *dev)
{
struct ug3105_chip *chip = dev_get_drvdata(dev);
adc_battery_helper_suspend(dev);
ug3105_stop(chip->client);
return 0;
}
static int __maybe_unused ug3105_resume(struct device *dev)
{
struct ug3105_chip *chip = dev_get_drvdata(dev);
ug3105_start(chip->client);
adc_battery_helper_resume(dev);
return 0;
}
static SIMPLE_DEV_PM_OPS(ug3105_pm_ops, ug3105_suspend,
ug3105_resume);
static const struct i2c_device_id ug3105_id[] = {
{ "ug3105" },
{ }
};
MODULE_DEVICE_TABLE(i2c, ug3105_id);
static struct i2c_driver ug3105_i2c_driver = {
.driver = {
.name = "ug3105",
.pm = &ug3105_pm_ops,
},
.probe = ug3105_probe,
.remove = ug3105_stop,
.shutdown = ug3105_stop,
.id_table = ug3105_id,
};
module_i2c_driver(ug3105_i2c_driver);
MODULE_AUTHOR("Hans de Goede <hansg@kernel.org");
MODULE_DESCRIPTION("uPI uG3105 battery monitor driver");
MODULE_LICENSE("GPL");