aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Looijmans <mike.looijmans@topic.nl>2015-07-01 05:40:53 -0400
committerSebastian Reichel <sre@kernel.org>2015-07-24 10:52:24 -0400
commit377b641ac84c573bcea06aec47f7ad1ba4047e1c (patch)
treed6170cd8ced052df6cad22faeb62d9ffb8d01f51
parent325b50aa5d1c7dae57d1e44defdbacd1e2bcabde (diff)
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>
-rw-r--r--drivers/power/ltc2941-battery-gauge.c54
1 files changed, 8 insertions, 46 deletions
diff --git a/drivers/power/ltc2941-battery-gauge.c b/drivers/power/ltc2941-battery-gauge.c
index daeb0860736c..4adf2ba021ce 100644
--- a/drivers/power/ltc2941-battery-gauge.c
+++ b/drivers/power/ltc2941-battery-gauge.c
@@ -14,7 +14,6 @@
14#include <linux/swab.h> 14#include <linux/swab.h>
15#include <linux/i2c.h> 15#include <linux/i2c.h>
16#include <linux/delay.h> 16#include <linux/delay.h>
17#include <linux/idr.h>
18#include <linux/power_supply.h> 17#include <linux/power_supply.h>
19#include <linux/slab.h> 18#include <linux/slab.h>
20 19
@@ -63,15 +62,11 @@ struct ltc294x_info {
63 struct power_supply_desc supply_desc; /* Supply description */ 62 struct power_supply_desc supply_desc; /* Supply description */
64 struct delayed_work work; /* Work scheduler */ 63 struct delayed_work work; /* Work scheduler */
65 int num_regs; /* Number of registers (chip type) */ 64 int num_regs; /* Number of registers (chip type) */
66 int id; /* Identifier of ltc294x chip */
67 int charge; /* Last charge register content */ 65 int charge; /* Last charge register content */
68 int r_sense; /* mOhm */ 66 int r_sense; /* mOhm */
69 int Qlsb; /* nAh */ 67 int Qlsb; /* nAh */
70}; 68};
71 69
72static DEFINE_IDR(ltc294x_id);
73static DEFINE_MUTEX(ltc294x_lock);
74
75static inline int convert_bin_to_uAh( 70static inline int convert_bin_to_uAh(
76 const struct ltc294x_info *info, int Q) 71 const struct ltc294x_info *info, int Q)
77{ 72{
@@ -371,10 +366,6 @@ static int ltc294x_i2c_remove(struct i2c_client *client)
371 366
372 cancel_delayed_work(&info->work); 367 cancel_delayed_work(&info->work);
373 power_supply_unregister(info->supply); 368 power_supply_unregister(info->supply);
374 kfree(info->supply_desc.name);
375 mutex_lock(&ltc294x_lock);
376 idr_remove(&ltc294x_id, info->id);
377 mutex_unlock(&ltc294x_lock);
378 return 0; 369 return 0;
379} 370}
380 371
@@ -384,44 +375,28 @@ static int ltc294x_i2c_probe(struct i2c_client *client,
384 struct power_supply_config psy_cfg = {}; 375 struct power_supply_config psy_cfg = {};
385 struct ltc294x_info *info; 376 struct ltc294x_info *info;
386 int ret; 377 int ret;
387 int num;
388 u32 prescaler_exp; 378 u32 prescaler_exp;
389 s32 r_sense; 379 s32 r_sense;
390 struct device_node *np; 380 struct device_node *np;
391 381
392 mutex_lock(&ltc294x_lock);
393 ret = idr_alloc(&ltc294x_id, client, 0, 0, GFP_KERNEL);
394 mutex_unlock(&ltc294x_lock);
395 if (ret < 0)
396 goto fail_id;
397
398 num = ret;
399
400 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL); 382 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
401 if (info == NULL) { 383 if (info == NULL)
402 ret = -ENOMEM; 384 return -ENOMEM;
403 goto fail_info;
404 }
405 385
406 i2c_set_clientdata(client, info); 386 i2c_set_clientdata(client, info);
407 387
408 info->num_regs = id->driver_data;
409 info->supply_desc.name = kasprintf(GFP_KERNEL, "%s-%d", client->name,
410 num);
411 if (!info->supply_desc.name) {
412 ret = -ENOMEM;
413 goto fail_name;
414 }
415
416 np = of_node_get(client->dev.of_node); 388 np = of_node_get(client->dev.of_node);
417 389
390 info->num_regs = id->driver_data;
391 info->supply_desc.name = np->name;
392
418 /* r_sense can be negative, when sense+ is connected to the battery 393 /* r_sense can be negative, when sense+ is connected to the battery
419 * instead of the sense-. This results in reversed measurements. */ 394 * instead of the sense-. This results in reversed measurements. */
420 ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense); 395 ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense);
421 if (ret < 0) { 396 if (ret < 0) {
422 dev_err(&client->dev, 397 dev_err(&client->dev,
423 "Could not find lltc,resistor-sense in devicetree\n"); 398 "Could not find lltc,resistor-sense in devicetree\n");
424 goto fail_name; 399 return ret;
425 } 400 }
426 info->r_sense = r_sense; 401 info->r_sense = r_sense;
427 402
@@ -446,7 +421,6 @@ static int ltc294x_i2c_probe(struct i2c_client *client,
446 } 421 }
447 422
448 info->client = client; 423 info->client = client;
449 info->id = num;
450 info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY; 424 info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
451 info->supply_desc.properties = ltc294x_properties; 425 info->supply_desc.properties = ltc294x_properties;
452 if (info->num_regs >= LTC294X_REG_TEMPERATURE_LSB) 426 if (info->num_regs >= LTC294X_REG_TEMPERATURE_LSB)
@@ -473,31 +447,19 @@ static int ltc294x_i2c_probe(struct i2c_client *client,
473 ret = ltc294x_reset(info, prescaler_exp); 447 ret = ltc294x_reset(info, prescaler_exp);
474 if (ret < 0) { 448 if (ret < 0) {
475 dev_err(&client->dev, "Communication with chip failed\n"); 449 dev_err(&client->dev, "Communication with chip failed\n");
476 goto fail_comm; 450 return ret;
477 } 451 }
478 452
479 info->supply = power_supply_register(&client->dev, &info->supply_desc, 453 info->supply = power_supply_register(&client->dev, &info->supply_desc,
480 &psy_cfg); 454 &psy_cfg);
481 if (IS_ERR(info->supply)) { 455 if (IS_ERR(info->supply)) {
482 dev_err(&client->dev, "failed to register ltc2941\n"); 456 dev_err(&client->dev, "failed to register ltc2941\n");
483 ret = PTR_ERR(info->supply); 457 return PTR_ERR(info->supply);
484 goto fail_register;
485 } else { 458 } else {
486 schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ); 459 schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
487 } 460 }
488 461
489 return 0; 462 return 0;
490
491fail_register:
492 kfree(info->supply_desc.name);
493fail_comm:
494fail_name:
495fail_info:
496 mutex_lock(&ltc294x_lock);
497 idr_remove(&ltc294x_id, num);
498 mutex_unlock(&ltc294x_lock);
499fail_id:
500 return ret;
501} 463}
502 464
503#ifdef CONFIG_PM_SLEEP 465#ifdef CONFIG_PM_SLEEP