Skip to content

Commit 1d7d206

Browse files
lumagbebarino
authored andcommitted
clk: fixed-rate: add devm_clk_hw_register_fixed_rate
Add devm_clk_hw_register_fixed_rate(), devres-managed helper to register fixed-rate clock. Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org> Link: https://lore.kernel.org/r/20220916061740.87167-3-dmitry.baryshkov@linaro.org Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent f5290d8 commit 1d7d206

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

drivers/clk/clk-fixed-rate.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,36 @@ const struct clk_ops clk_fixed_rate_ops = {
4949
};
5050
EXPORT_SYMBOL_GPL(clk_fixed_rate_ops);
5151

52+
static void devm_clk_hw_register_fixed_rate_release(struct device *dev, void *res)
53+
{
54+
struct clk_fixed_rate *fix = res;
55+
56+
/*
57+
* We can not use clk_hw_unregister_fixed_rate, since it will kfree()
58+
* the hw, resulting in double free. Just unregister the hw and let
59+
* devres code kfree() it.
60+
*/
61+
clk_hw_unregister(&fix->hw);
62+
}
63+
5264
struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev,
5365
struct device_node *np, const char *name,
5466
const char *parent_name, const struct clk_hw *parent_hw,
5567
const struct clk_parent_data *parent_data, unsigned long flags,
5668
unsigned long fixed_rate, unsigned long fixed_accuracy,
57-
unsigned long clk_fixed_flags)
69+
unsigned long clk_fixed_flags, bool devm)
5870
{
5971
struct clk_fixed_rate *fixed;
6072
struct clk_hw *hw;
6173
struct clk_init_data init = {};
6274
int ret = -EINVAL;
6375

6476
/* allocate fixed-rate clock */
65-
fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
77+
if (devm)
78+
fixed = devres_alloc(devm_clk_hw_register_fixed_rate_release,
79+
sizeof(*fixed), GFP_KERNEL);
80+
else
81+
fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
6682
if (!fixed)
6783
return ERR_PTR(-ENOMEM);
6884

@@ -90,9 +106,13 @@ struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev,
90106
else
91107
ret = of_clk_hw_register(np, hw);
92108
if (ret) {
93-
kfree(fixed);
109+
if (devm)
110+
devres_free(fixed);
111+
else
112+
kfree(fixed);
94113
hw = ERR_PTR(ret);
95-
}
114+
} else if (devm)
115+
devres_add(dev, fixed);
96116

97117
return hw;
98118
}

include/linux/clk-provider.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ struct clk_hw *__clk_hw_register_fixed_rate(struct device *dev,
350350
const char *parent_name, const struct clk_hw *parent_hw,
351351
const struct clk_parent_data *parent_data, unsigned long flags,
352352
unsigned long fixed_rate, unsigned long fixed_accuracy,
353-
unsigned long clk_fixed_flags);
353+
unsigned long clk_fixed_flags, bool devm);
354354
struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
355355
const char *parent_name, unsigned long flags,
356356
unsigned long fixed_rate);
@@ -365,7 +365,20 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
365365
*/
366366
#define clk_hw_register_fixed_rate(dev, name, parent_name, flags, fixed_rate) \
367367
__clk_hw_register_fixed_rate((dev), NULL, (name), (parent_name), NULL, \
368-
NULL, (flags), (fixed_rate), 0, 0)
368+
NULL, (flags), (fixed_rate), 0, 0, false)
369+
370+
/**
371+
* devm_clk_hw_register_fixed_rate - register fixed-rate clock with the clock
372+
* framework
373+
* @dev: device that is registering this clock
374+
* @name: name of this clock
375+
* @parent_name: name of clock's parent
376+
* @flags: framework-specific flags
377+
* @fixed_rate: non-adjustable clock rate
378+
*/
379+
#define devm_clk_hw_register_fixed_rate(dev, name, parent_name, flags, fixed_rate) \
380+
__clk_hw_register_fixed_rate((dev), NULL, (name), (parent_name), NULL, \
381+
NULL, (flags), (fixed_rate), 0, 0, true)
369382
/**
370383
* clk_hw_register_fixed_rate_parent_hw - register fixed-rate clock with
371384
* the clock framework
@@ -378,7 +391,7 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
378391
#define clk_hw_register_fixed_rate_parent_hw(dev, name, parent_hw, flags, \
379392
fixed_rate) \
380393
__clk_hw_register_fixed_rate((dev), NULL, (name), NULL, (parent_hw), \
381-
NULL, (flags), (fixed_rate), 0, 0)
394+
NULL, (flags), (fixed_rate), 0, 0, false)
382395
/**
383396
* clk_hw_register_fixed_rate_parent_data - register fixed-rate clock with
384397
* the clock framework
@@ -392,7 +405,7 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
392405
fixed_rate) \
393406
__clk_hw_register_fixed_rate((dev), NULL, (name), NULL, NULL, \
394407
(parent_data), (flags), (fixed_rate), 0, \
395-
0)
408+
0, false)
396409
/**
397410
* clk_hw_register_fixed_rate_with_accuracy - register fixed-rate clock with
398411
* the clock framework
@@ -408,7 +421,7 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
408421
fixed_accuracy) \
409422
__clk_hw_register_fixed_rate((dev), NULL, (name), (parent_name), \
410423
NULL, NULL, (flags), (fixed_rate), \
411-
(fixed_accuracy), 0)
424+
(fixed_accuracy), 0, false)
412425
/**
413426
* clk_hw_register_fixed_rate_with_accuracy_parent_hw - register fixed-rate
414427
* clock with the clock framework
@@ -423,7 +436,7 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
423436
parent_hw, flags, fixed_rate, fixed_accuracy) \
424437
__clk_hw_register_fixed_rate((dev), NULL, (name), NULL, (parent_hw) \
425438
NULL, NULL, (flags), (fixed_rate), \
426-
(fixed_accuracy), 0)
439+
(fixed_accuracy), 0, false)
427440
/**
428441
* clk_hw_register_fixed_rate_with_accuracy_parent_data - register fixed-rate
429442
* clock with the clock framework
@@ -438,7 +451,7 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
438451
parent_data, flags, fixed_rate, fixed_accuracy) \
439452
__clk_hw_register_fixed_rate((dev), NULL, (name), NULL, NULL, \
440453
(parent_data), NULL, (flags), \
441-
(fixed_rate), (fixed_accuracy), 0)
454+
(fixed_rate), (fixed_accuracy), 0, false)
442455
/**
443456
* clk_hw_register_fixed_rate_parent_accuracy - register fixed-rate clock with
444457
* the clock framework
@@ -452,7 +465,7 @@ struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
452465
flags, fixed_rate) \
453466
__clk_hw_register_fixed_rate((dev), NULL, (name), NULL, NULL, \
454467
(parent_data), (flags), (fixed_rate), 0, \
455-
CLK_FIXED_RATE_PARENT_ACCURACY)
468+
CLK_FIXED_RATE_PARENT_ACCURACY, false)
456469

457470
void clk_unregister_fixed_rate(struct clk *clk);
458471
void clk_hw_unregister_fixed_rate(struct clk_hw *hw);

0 commit comments

Comments
 (0)