Skip to content

Commit 7ef9651

Browse files
Uwe Kleine-Königbebarino
authored andcommitted
clk: Provide new devm_clk helpers for prepared and enabled clocks
When a driver keeps a clock prepared (or enabled) during the whole lifetime of the driver, these helpers allow to simplify the drivers. Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Alexandru Ardelean <aardelean@deviqon.com> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Link: https://lore.kernel.org/r/20220520075737.758761-4-u.kleine-koenig@pengutronix.de Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent abae8e5 commit 7ef9651

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

drivers/clk/clk-devres.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,39 @@ struct clk *devm_clk_get(struct device *dev, const char *id)
6666
}
6767
EXPORT_SYMBOL(devm_clk_get);
6868

69+
struct clk *devm_clk_get_prepared(struct device *dev, const char *id)
70+
{
71+
return __devm_clk_get(dev, id, clk_get, clk_prepare, clk_unprepare);
72+
}
73+
EXPORT_SYMBOL_GPL(devm_clk_get_prepared);
74+
75+
struct clk *devm_clk_get_enabled(struct device *dev, const char *id)
76+
{
77+
return __devm_clk_get(dev, id, clk_get,
78+
clk_prepare_enable, clk_disable_unprepare);
79+
}
80+
EXPORT_SYMBOL_GPL(devm_clk_get_enabled);
81+
6982
struct clk *devm_clk_get_optional(struct device *dev, const char *id)
7083
{
7184
return __devm_clk_get(dev, id, clk_get_optional, NULL, NULL);
7285
}
7386
EXPORT_SYMBOL(devm_clk_get_optional);
7487

88+
struct clk *devm_clk_get_optional_prepared(struct device *dev, const char *id)
89+
{
90+
return __devm_clk_get(dev, id, clk_get_optional,
91+
clk_prepare, clk_unprepare);
92+
}
93+
EXPORT_SYMBOL_GPL(devm_clk_get_optional_prepared);
94+
95+
struct clk *devm_clk_get_optional_enabled(struct device *dev, const char *id)
96+
{
97+
return __devm_clk_get(dev, id, clk_get_optional,
98+
clk_prepare_enable, clk_disable_unprepare);
99+
}
100+
EXPORT_SYMBOL_GPL(devm_clk_get_optional_enabled);
101+
75102
struct clk_bulk_devres {
76103
struct clk_bulk_data *clks;
77104
int num_clks;

include/linux/clk.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,47 @@ int __must_check devm_clk_bulk_get_all(struct device *dev,
459459
*/
460460
struct clk *devm_clk_get(struct device *dev, const char *id);
461461

462+
/**
463+
* devm_clk_get_prepared - devm_clk_get() + clk_prepare()
464+
* @dev: device for clock "consumer"
465+
* @id: clock consumer ID
466+
*
467+
* Context: May sleep.
468+
*
469+
* Return: a struct clk corresponding to the clock producer, or
470+
* valid IS_ERR() condition containing errno. The implementation
471+
* uses @dev and @id to determine the clock consumer, and thereby
472+
* the clock producer. (IOW, @id may be identical strings, but
473+
* clk_get may return different clock producers depending on @dev.)
474+
*
475+
* The returned clk (if valid) is prepared. Drivers must however assume
476+
* that the clock is not enabled.
477+
*
478+
* The clock will automatically be unprepared and freed when the device
479+
* is unbound from the bus.
480+
*/
481+
struct clk *devm_clk_get_prepared(struct device *dev, const char *id);
482+
483+
/**
484+
* devm_clk_get_enabled - devm_clk_get() + clk_prepare_enable()
485+
* @dev: device for clock "consumer"
486+
* @id: clock consumer ID
487+
*
488+
* Context: May sleep.
489+
*
490+
* Return: a struct clk corresponding to the clock producer, or
491+
* valid IS_ERR() condition containing errno. The implementation
492+
* uses @dev and @id to determine the clock consumer, and thereby
493+
* the clock producer. (IOW, @id may be identical strings, but
494+
* clk_get may return different clock producers depending on @dev.)
495+
*
496+
* The returned clk (if valid) is prepared and enabled.
497+
*
498+
* The clock will automatically be disabled, unprepared and freed
499+
* when the device is unbound from the bus.
500+
*/
501+
struct clk *devm_clk_get_enabled(struct device *dev, const char *id);
502+
462503
/**
463504
* devm_clk_get_optional - lookup and obtain a managed reference to an optional
464505
* clock producer.
@@ -482,6 +523,50 @@ struct clk *devm_clk_get(struct device *dev, const char *id);
482523
*/
483524
struct clk *devm_clk_get_optional(struct device *dev, const char *id);
484525

526+
/**
527+
* devm_clk_get_optional_prepared - devm_clk_get_optional() + clk_prepare()
528+
* @dev: device for clock "consumer"
529+
* @id: clock consumer ID
530+
*
531+
* Context: May sleep.
532+
*
533+
* Return: a struct clk corresponding to the clock producer, or
534+
* valid IS_ERR() condition containing errno. The implementation
535+
* uses @dev and @id to determine the clock consumer, and thereby
536+
* the clock producer. If no such clk is found, it returns NULL
537+
* which serves as a dummy clk. That's the only difference compared
538+
* to devm_clk_get_prepared().
539+
*
540+
* The returned clk (if valid) is prepared. Drivers must however
541+
* assume that the clock is not enabled.
542+
*
543+
* The clock will automatically be unprepared and freed when the
544+
* device is unbound from the bus.
545+
*/
546+
struct clk *devm_clk_get_optional_prepared(struct device *dev, const char *id);
547+
548+
/**
549+
* devm_clk_get_optional_enabled - devm_clk_get_optional() +
550+
* clk_prepare_enable()
551+
* @dev: device for clock "consumer"
552+
* @id: clock consumer ID
553+
*
554+
* Context: May sleep.
555+
*
556+
* Return: a struct clk corresponding to the clock producer, or
557+
* valid IS_ERR() condition containing errno. The implementation
558+
* uses @dev and @id to determine the clock consumer, and thereby
559+
* the clock producer. If no such clk is found, it returns NULL
560+
* which serves as a dummy clk. That's the only difference compared
561+
* to devm_clk_get_enabled().
562+
*
563+
* The returned clk (if valid) is prepared and enabled.
564+
*
565+
* The clock will automatically be disabled, unprepared and freed
566+
* when the device is unbound from the bus.
567+
*/
568+
struct clk *devm_clk_get_optional_enabled(struct device *dev, const char *id);
569+
485570
/**
486571
* devm_get_clk_from_child - lookup and obtain a managed reference to a
487572
* clock producer from child node.
@@ -826,12 +911,36 @@ static inline struct clk *devm_clk_get(struct device *dev, const char *id)
826911
return NULL;
827912
}
828913

914+
static inline struct clk *devm_clk_get_prepared(struct device *dev,
915+
const char *id)
916+
{
917+
return NULL;
918+
}
919+
920+
static inline struct clk *devm_clk_get_enabled(struct device *dev,
921+
const char *id)
922+
{
923+
return NULL;
924+
}
925+
829926
static inline struct clk *devm_clk_get_optional(struct device *dev,
830927
const char *id)
831928
{
832929
return NULL;
833930
}
834931

932+
static inline struct clk *devm_clk_get_optional_prepared(struct device *dev,
933+
const char *id)
934+
{
935+
return NULL;
936+
}
937+
938+
static inline struct clk *devm_clk_get_optional_enabled(struct device *dev,
939+
const char *id)
940+
{
941+
return NULL;
942+
}
943+
835944
static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
836945
struct clk_bulk_data *clks)
837946
{

0 commit comments

Comments
 (0)