power/ltc2941-battery-gauge.c: Use the devicetree node name as supply name

Make it possible to set the name of the supply from the devicetree.
Like other power supply drivers just use the node name. This makes
the code smaller as well, as it doesn't need to allocate memory to
hold the name and allocate a unique ID.

Signed-off-by: Mike Looijmans <mike.looijmans@topic.nl>
Signed-off-by: Sebastian Reichel <sre@kernel.org>
This commit is contained in:
Mike Looijmans 2015-07-01 11:40:53 +02:00 committed by Sebastian Reichel
parent 325b50aa5d
commit 377b641ac8
1 changed files with 8 additions and 46 deletions

View File

@ -14,7 +14,6 @@
#include <linux/swab.h> #include <linux/swab.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/idr.h>
#include <linux/power_supply.h> #include <linux/power_supply.h>
#include <linux/slab.h> #include <linux/slab.h>
@ -63,15 +62,11 @@ struct ltc294x_info {
struct power_supply_desc supply_desc; /* Supply description */ struct power_supply_desc supply_desc; /* Supply description */
struct delayed_work work; /* Work scheduler */ struct delayed_work work; /* Work scheduler */
int num_regs; /* Number of registers (chip type) */ int num_regs; /* Number of registers (chip type) */
int id; /* Identifier of ltc294x chip */
int charge; /* Last charge register content */ int charge; /* Last charge register content */
int r_sense; /* mOhm */ int r_sense; /* mOhm */
int Qlsb; /* nAh */ int Qlsb; /* nAh */
}; };
static DEFINE_IDR(ltc294x_id);
static DEFINE_MUTEX(ltc294x_lock);
static inline int convert_bin_to_uAh( static inline int convert_bin_to_uAh(
const struct ltc294x_info *info, int Q) const struct ltc294x_info *info, int Q)
{ {
@ -371,10 +366,6 @@ static int ltc294x_i2c_remove(struct i2c_client *client)
cancel_delayed_work(&info->work); cancel_delayed_work(&info->work);
power_supply_unregister(info->supply); power_supply_unregister(info->supply);
kfree(info->supply_desc.name);
mutex_lock(&ltc294x_lock);
idr_remove(&ltc294x_id, info->id);
mutex_unlock(&ltc294x_lock);
return 0; return 0;
} }
@ -384,44 +375,28 @@ static int ltc294x_i2c_probe(struct i2c_client *client,
struct power_supply_config psy_cfg = {}; struct power_supply_config psy_cfg = {};
struct ltc294x_info *info; struct ltc294x_info *info;
int ret; int ret;
int num;
u32 prescaler_exp; u32 prescaler_exp;
s32 r_sense; s32 r_sense;
struct device_node *np; struct device_node *np;
mutex_lock(&ltc294x_lock);
ret = idr_alloc(&ltc294x_id, client, 0, 0, GFP_KERNEL);
mutex_unlock(&ltc294x_lock);
if (ret < 0)
goto fail_id;
num = ret;
info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL); info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
if (info == NULL) { if (info == NULL)
ret = -ENOMEM; return -ENOMEM;
goto fail_info;
}
i2c_set_clientdata(client, info); i2c_set_clientdata(client, info);
info->num_regs = id->driver_data;
info->supply_desc.name = kasprintf(GFP_KERNEL, "%s-%d", client->name,
num);
if (!info->supply_desc.name) {
ret = -ENOMEM;
goto fail_name;
}
np = of_node_get(client->dev.of_node); np = of_node_get(client->dev.of_node);
info->num_regs = id->driver_data;
info->supply_desc.name = np->name;
/* r_sense can be negative, when sense+ is connected to the battery /* r_sense can be negative, when sense+ is connected to the battery
* instead of the sense-. This results in reversed measurements. */ * instead of the sense-. This results in reversed measurements. */
ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense); ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense);
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, dev_err(&client->dev,
"Could not find lltc,resistor-sense in devicetree\n"); "Could not find lltc,resistor-sense in devicetree\n");
goto fail_name; return ret;
} }
info->r_sense = r_sense; info->r_sense = r_sense;
@ -446,7 +421,6 @@ static int ltc294x_i2c_probe(struct i2c_client *client,
} }
info->client = client; info->client = client;
info->id = num;
info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY; info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
info->supply_desc.properties = ltc294x_properties; info->supply_desc.properties = ltc294x_properties;
if (info->num_regs >= LTC294X_REG_TEMPERATURE_LSB) if (info->num_regs >= LTC294X_REG_TEMPERATURE_LSB)
@ -473,31 +447,19 @@ static int ltc294x_i2c_probe(struct i2c_client *client,
ret = ltc294x_reset(info, prescaler_exp); ret = ltc294x_reset(info, prescaler_exp);
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, "Communication with chip failed\n"); dev_err(&client->dev, "Communication with chip failed\n");
goto fail_comm; return ret;
} }
info->supply = power_supply_register(&client->dev, &info->supply_desc, info->supply = power_supply_register(&client->dev, &info->supply_desc,
&psy_cfg); &psy_cfg);
if (IS_ERR(info->supply)) { if (IS_ERR(info->supply)) {
dev_err(&client->dev, "failed to register ltc2941\n"); dev_err(&client->dev, "failed to register ltc2941\n");
ret = PTR_ERR(info->supply); return PTR_ERR(info->supply);
goto fail_register;
} else { } else {
schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ); schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
} }
return 0; return 0;
fail_register:
kfree(info->supply_desc.name);
fail_comm:
fail_name:
fail_info:
mutex_lock(&ltc294x_lock);
idr_remove(&ltc294x_id, num);
mutex_unlock(&ltc294x_lock);
fail_id:
return ret;
} }
#ifdef CONFIG_PM_SLEEP #ifdef CONFIG_PM_SLEEP