Skip to content

Commit 02113ba

Browse files
jonhunterrafaeljw
authored andcommitted
PM / clk: Add support for obtaining clocks from device-tree
The PM clocks framework requires clients to pass either a con-id or a valid clk pointer in order to add a clock to a device. Add a new function of_pm_clk_add_clks() to allows device clocks to be retrieved from device-tree and populated for a given device. Note that it is not necessary to make the compilation of this new function dependent upon CONFIG_OF because there are stubs functions for the device-tree APIs used. In order to handle errors encountered when adding clocks from device-tree, add a function pm_clk_remove_clk() to remove any clocks (using a pointer to the clk structure) that have been added successfully before the error occurred. Signed-off-by: Jon Hunter <jonathanh@nvidia.com> Acked-by: Geert Uytterhoeven <geert+renesas@glider.be> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent b562e44 commit 02113ba

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

drivers/base/power/clock_ops.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,62 @@ int pm_clk_add_clk(struct device *dev, struct clk *clk)
137137
return __pm_clk_add(dev, NULL, clk);
138138
}
139139

140+
141+
/**
142+
* of_pm_clk_add_clks - Start using device clock(s) for power management.
143+
* @dev: Device whose clock(s) is going to be used for power management.
144+
*
145+
* Add a series of clocks described in the 'clocks' device-tree node for
146+
* a device to the list of clocks used for the power management of @dev.
147+
* On success, returns the number of clocks added. Returns a negative
148+
* error code if there are no clocks in the device node for the device
149+
* or if adding a clock fails.
150+
*/
151+
int of_pm_clk_add_clks(struct device *dev)
152+
{
153+
struct clk **clks;
154+
unsigned int i, count;
155+
int ret;
156+
157+
if (!dev || !dev->of_node)
158+
return -EINVAL;
159+
160+
count = of_count_phandle_with_args(dev->of_node, "clocks",
161+
"#clock-cells");
162+
if (count == 0)
163+
return -ENODEV;
164+
165+
clks = kcalloc(count, sizeof(*clks), GFP_KERNEL);
166+
if (!clks)
167+
return -ENOMEM;
168+
169+
for (i = 0; i < count; i++) {
170+
clks[i] = of_clk_get(dev->of_node, i);
171+
if (IS_ERR(clks[i])) {
172+
ret = PTR_ERR(clks[i]);
173+
goto error;
174+
}
175+
176+
ret = pm_clk_add_clk(dev, clks[i]);
177+
if (ret) {
178+
clk_put(clks[i]);
179+
goto error;
180+
}
181+
}
182+
183+
kfree(clks);
184+
185+
return i;
186+
187+
error:
188+
while (i--)
189+
pm_clk_remove_clk(dev, clks[i]);
190+
191+
kfree(clks);
192+
193+
return ret;
194+
}
195+
140196
/**
141197
* __pm_clk_remove - Destroy PM clock entry.
142198
* @ce: PM clock entry to destroy.
@@ -197,6 +253,39 @@ void pm_clk_remove(struct device *dev, const char *con_id)
197253
__pm_clk_remove(ce);
198254
}
199255

256+
/**
257+
* pm_clk_remove_clk - Stop using a device clock for power management.
258+
* @dev: Device whose clock should not be used for PM any more.
259+
* @clk: Clock pointer
260+
*
261+
* Remove the clock pointed to by @clk from the list of clocks used for
262+
* the power management of @dev.
263+
*/
264+
void pm_clk_remove_clk(struct device *dev, struct clk *clk)
265+
{
266+
struct pm_subsys_data *psd = dev_to_psd(dev);
267+
struct pm_clock_entry *ce;
268+
269+
if (!psd || !clk)
270+
return;
271+
272+
spin_lock_irq(&psd->lock);
273+
274+
list_for_each_entry(ce, &psd->clock_list, node) {
275+
if (clk == ce->clk)
276+
goto remove;
277+
}
278+
279+
spin_unlock_irq(&psd->lock);
280+
return;
281+
282+
remove:
283+
list_del(&ce->node);
284+
spin_unlock_irq(&psd->lock);
285+
286+
__pm_clk_remove(ce);
287+
}
288+
200289
/**
201290
* pm_clk_init - Initialize a device's list of power management clocks.
202291
* @dev: Device to initialize the list of PM clocks for.

include/linux/pm_clock.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ extern int pm_clk_create(struct device *dev);
4242
extern void pm_clk_destroy(struct device *dev);
4343
extern int pm_clk_add(struct device *dev, const char *con_id);
4444
extern int pm_clk_add_clk(struct device *dev, struct clk *clk);
45+
extern int of_pm_clk_add_clks(struct device *dev);
4546
extern void pm_clk_remove(struct device *dev, const char *con_id);
47+
extern void pm_clk_remove_clk(struct device *dev, struct clk *clk);
4648
extern int pm_clk_suspend(struct device *dev);
4749
extern int pm_clk_resume(struct device *dev);
4850
#else
@@ -69,11 +71,18 @@ static inline int pm_clk_add_clk(struct device *dev, struct clk *clk)
6971
{
7072
return -EINVAL;
7173
}
74+
static inline int of_pm_clk_add_clks(struct device *dev)
75+
{
76+
return -EINVAL;
77+
}
7278
static inline void pm_clk_remove(struct device *dev, const char *con_id)
7379
{
7480
}
7581
#define pm_clk_suspend NULL
7682
#define pm_clk_resume NULL
83+
static inline void pm_clk_remove_clk(struct device *dev, struct clk *clk)
84+
{
85+
}
7786
#endif
7887

7988
#ifdef CONFIG_HAVE_CLK

0 commit comments

Comments
 (0)