2 * Fuel gauge driver for Richtek RT5033
4 * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
5 * Author: Beomho Seo <beomho.seo@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published bythe Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/mfd/rt5033-private.h>
16 #include <linux/mfd/rt5033.h>
18 static int rt5033_battery_get_capacity(struct i2c_client
*client
)
20 struct rt5033_battery
*battery
= i2c_get_clientdata(client
);
23 regmap_read(battery
->regmap
, RT5033_FUEL_REG_SOC_H
, &msb
);
28 static int rt5033_battery_get_present(struct i2c_client
*client
)
30 struct rt5033_battery
*battery
= i2c_get_clientdata(client
);
33 regmap_read(battery
->regmap
, RT5033_FUEL_REG_CONFIG_L
, &val
);
35 return (val
& RT5033_FUEL_BAT_PRESENT
) ? true : false;
38 static int rt5033_battery_get_watt_prop(struct i2c_client
*client
,
39 enum power_supply_property psp
)
41 struct rt5033_battery
*battery
= i2c_get_clientdata(client
);
42 unsigned int regh
, regl
;
47 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
48 regh
= RT5033_FUEL_REG_VBAT_H
;
49 regl
= RT5033_FUEL_REG_VBAT_L
;
51 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
52 regh
= RT5033_FUEL_REG_AVG_VOLT_H
;
53 regl
= RT5033_FUEL_REG_AVG_VOLT_L
;
55 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
56 regh
= RT5033_FUEL_REG_OCV_H
;
57 regl
= RT5033_FUEL_REG_OCV_L
;
63 regmap_read(battery
->regmap
, regh
, &msb
);
64 regmap_read(battery
->regmap
, regl
, &lsb
);
66 ret
= ((msb
<< 4) + (lsb
>> 4)) * 1250 / 1000;
71 static int rt5033_battery_get_property(struct power_supply
*psy
,
72 enum power_supply_property psp
,
73 union power_supply_propval
*val
)
75 struct rt5033_battery
*battery
= container_of(psy
,
76 struct rt5033_battery
, psy
);
79 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
80 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
81 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
82 val
->intval
= rt5033_battery_get_watt_prop(battery
->client
,
85 case POWER_SUPPLY_PROP_PRESENT
:
86 val
->intval
= rt5033_battery_get_present(battery
->client
);
88 case POWER_SUPPLY_PROP_CAPACITY
:
89 val
->intval
= rt5033_battery_get_capacity(battery
->client
);
97 static enum power_supply_property rt5033_battery_props
[] = {
98 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
99 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
100 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
101 POWER_SUPPLY_PROP_PRESENT
,
102 POWER_SUPPLY_PROP_CAPACITY
,
105 static struct regmap_config rt5033_battery_regmap_config
= {
108 .max_register
= RT5033_FUEL_REG_END
,
111 static int rt5033_battery_probe(struct i2c_client
*client
,
112 const struct i2c_device_id
*id
)
114 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
115 struct rt5033_battery
*battery
;
118 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
121 battery
= devm_kzalloc(&client
->dev
, sizeof(*battery
), GFP_KERNEL
);
125 battery
->client
= client
;
126 battery
->regmap
= devm_regmap_init_i2c(client
,
127 &rt5033_battery_regmap_config
);
128 if (IS_ERR(battery
->regmap
)) {
129 dev_err(&client
->dev
, "Failed to initialize regmap\n");
133 i2c_set_clientdata(client
, battery
);
135 battery
->psy
.name
= "rt5033-battery";
136 battery
->psy
.type
= POWER_SUPPLY_TYPE_BATTERY
;
137 battery
->psy
.get_property
= rt5033_battery_get_property
;
138 battery
->psy
.properties
= rt5033_battery_props
;
139 battery
->psy
.num_properties
= ARRAY_SIZE(rt5033_battery_props
);
141 ret
= power_supply_register(&client
->dev
, &battery
->psy
);
143 dev_err(&client
->dev
, "Failed to register power supply\n");
150 static int rt5033_battery_remove(struct i2c_client
*client
)
152 struct rt5033_battery
*battery
= i2c_get_clientdata(client
);
154 power_supply_unregister(&battery
->psy
);
159 static const struct i2c_device_id rt5033_battery_id
[] = {
160 { "rt5033-battery", },
163 MODULE_DEVICE_TABLE(platform
, rt5033_battery_id
);
165 static struct i2c_driver rt5033_battery_driver
= {
167 .name
= "rt5033-battery",
169 .probe
= rt5033_battery_probe
,
170 .remove
= rt5033_battery_remove
,
171 .id_table
= rt5033_battery_id
,
173 module_i2c_driver(rt5033_battery_driver
);
175 MODULE_DESCRIPTION("Richtek RT5033 fuel gauge driver");
176 MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
177 MODULE_LICENSE("GPL");