Skip to content

Commit 93297ef

Browse files
committed
power: supply: generic-adc-battery: convert to managed resources
Convert driver to use managed resources to simplify driver code. Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com> Signed-off-by: Sebastian Reichel <sre@kernel.org>
1 parent 27a2195 commit 93297ef

1 file changed

Lines changed: 23 additions & 58 deletions

File tree

drivers/power/supply/generic-adc-battery.c

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/iio/consumer.h>
2424
#include <linux/iio/types.h>
2525
#include <linux/power/generic-adc-battery.h>
26+
#include <linux/devm-helpers.h>
2627

2728
#define JITTER_DEFAULT 10 /* hope 10ms is enough */
2829

@@ -266,14 +267,13 @@ static int gab_probe(struct platform_device *pdev)
266267
* copying the static properties and allocating extra memory for holding
267268
* the extra configurable properties received from platform data.
268269
*/
269-
properties = kcalloc(ARRAY_SIZE(gab_props) +
270-
ARRAY_SIZE(gab_chan_name),
271-
sizeof(*properties),
272-
GFP_KERNEL);
273-
if (!properties) {
274-
ret = -ENOMEM;
275-
goto first_mem_fail;
276-
}
270+
properties = devm_kcalloc(&pdev->dev,
271+
ARRAY_SIZE(gab_props) +
272+
ARRAY_SIZE(gab_chan_name),
273+
sizeof(*properties),
274+
GFP_KERNEL);
275+
if (!properties)
276+
return -ENOMEM;
277277

278278
memcpy(properties, gab_props, sizeof(gab_props));
279279

@@ -282,12 +282,13 @@ static int gab_probe(struct platform_device *pdev)
282282
* based on the channel supported by consumer device.
283283
*/
284284
for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
285-
adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
286-
gab_chan_name[chan]);
285+
adc_bat->channel[chan] = devm_iio_channel_get(&pdev->dev, gab_chan_name[chan]);
287286
if (IS_ERR(adc_bat->channel[chan])) {
288287
ret = PTR_ERR(adc_bat->channel[chan]);
288+
if (ret != -ENODEV)
289+
return dev_err_probe(&pdev->dev, ret, "Failed to get ADC channel %s\n", gab_chan_name[chan]);
289290
adc_bat->channel[chan] = NULL;
290-
} else {
291+
} else if (adc_bat->channel[chan]) {
291292
/* copying properties for supported channels only */
292293
int index2;
293294

@@ -302,10 +303,8 @@ static int gab_probe(struct platform_device *pdev)
302303
}
303304

304305
/* none of the channels are supported so let's bail out */
305-
if (!any) {
306-
ret = -ENODEV;
307-
goto second_mem_fail;
308-
}
306+
if (!any)
307+
return dev_err_probe(&pdev->dev, -ENODEV, "Failed to get any ADC channel\n");
309308

310309
/*
311310
* Total number of properties is equal to static properties
@@ -316,25 +315,24 @@ static int gab_probe(struct platform_device *pdev)
316315
psy_desc->properties = properties;
317316
psy_desc->num_properties = index;
318317

319-
adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
320-
if (IS_ERR(adc_bat->psy)) {
321-
ret = PTR_ERR(adc_bat->psy);
322-
goto err_reg_fail;
323-
}
318+
adc_bat->psy = devm_power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
319+
if (IS_ERR(adc_bat->psy))
320+
return dev_err_probe(&pdev->dev, PTR_ERR(adc_bat->psy), "Failed to register power-supply device\n");
324321

325-
INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
322+
ret = devm_delayed_work_autocancel(&pdev->dev, &adc_bat->bat_work, gab_work);
323+
if (ret)
324+
return dev_err_probe(&pdev->dev, ret, "Failed to register delayed work\n");
326325

327-
adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev,
328-
"charged", GPIOD_IN);
326+
adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev, "charged", GPIOD_IN);
329327
if (adc_bat->charge_finished) {
330328
int irq;
331329

332330
irq = gpiod_to_irq(adc_bat->charge_finished);
333-
ret = request_any_context_irq(irq, gab_charged,
331+
ret = devm_request_any_context_irq(&pdev->dev, irq, gab_charged,
334332
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
335333
"battery charged", adc_bat);
336334
if (ret < 0)
337-
goto gpio_req_fail;
335+
return dev_err_probe(&pdev->dev, ret, "Failed to register irq\n");
338336
}
339337

340338
platform_set_drvdata(pdev, adc_bat);
@@ -343,38 +341,6 @@ static int gab_probe(struct platform_device *pdev)
343341
schedule_delayed_work(&adc_bat->bat_work,
344342
msecs_to_jiffies(0));
345343
return 0;
346-
347-
gpio_req_fail:
348-
power_supply_unregister(adc_bat->psy);
349-
err_reg_fail:
350-
for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
351-
if (adc_bat->channel[chan])
352-
iio_channel_release(adc_bat->channel[chan]);
353-
}
354-
second_mem_fail:
355-
kfree(properties);
356-
first_mem_fail:
357-
return ret;
358-
}
359-
360-
static int gab_remove(struct platform_device *pdev)
361-
{
362-
int chan;
363-
struct gab *adc_bat = platform_get_drvdata(pdev);
364-
365-
power_supply_unregister(adc_bat->psy);
366-
367-
if (adc_bat->charge_finished)
368-
free_irq(gpiod_to_irq(adc_bat->charge_finished), adc_bat);
369-
370-
for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
371-
if (adc_bat->channel[chan])
372-
iio_channel_release(adc_bat->channel[chan]);
373-
}
374-
375-
kfree(adc_bat->psy_desc.properties);
376-
cancel_delayed_work_sync(&adc_bat->bat_work);
377-
return 0;
378344
}
379345

380346
static int __maybe_unused gab_suspend(struct device *dev)
@@ -408,7 +374,6 @@ static struct platform_driver gab_driver = {
408374
.pm = &gab_pm_ops,
409375
},
410376
.probe = gab_probe,
411-
.remove = gab_remove,
412377
};
413378
module_platform_driver(gab_driver);
414379

0 commit comments

Comments
 (0)