Blame view

drivers/clk/clk.c 83.5 KB
b2476490e   Mike Turquette   clk: introduce th...
1
2
3
4
5
6
7
8
9
10
  /*
   * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
   * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * Standard functionality for the common clock API.  See Documentation/clk.txt
   */
3c3731173   Stephen Boyd   clk: Include clk....
11
  #include <linux/clk.h>
b09d6d991   Michael Turquette   clk: remove clk-p...
12
  #include <linux/clk-provider.h>
86be408bf   Sylwester Nawrocki   clk: Support for ...
13
  #include <linux/clk/clk-conf.h>
b2476490e   Mike Turquette   clk: introduce th...
14
15
16
17
18
19
  #include <linux/module.h>
  #include <linux/mutex.h>
  #include <linux/spinlock.h>
  #include <linux/err.h>
  #include <linux/list.h>
  #include <linux/slab.h>
766e6a4ec   Grant Likely   clk: add DT clock...
20
  #include <linux/of.h>
46c8773a5   Stephen Boyd   clk: Add devm_clk...
21
  #include <linux/device.h>
f2f6c2556   Prashant Gaikwad   clk: add common o...
22
  #include <linux/init.h>
533ddeb1e   Mike Turquette   clk: allow reentr...
23
  #include <linux/sched.h>
562ef0b09   Stephen Boyd   clk: Silence spar...
24
  #include <linux/clkdev.h>
b2476490e   Mike Turquette   clk: introduce th...
25

d6782c263   Sylwester Nawrocki   clk: Provide not ...
26
  #include "clk.h"
b2476490e   Mike Turquette   clk: introduce th...
27
28
  static DEFINE_SPINLOCK(enable_lock);
  static DEFINE_MUTEX(prepare_lock);
533ddeb1e   Mike Turquette   clk: allow reentr...
29
30
31
32
33
  static struct task_struct *prepare_owner;
  static struct task_struct *enable_owner;
  
  static int prepare_refcnt;
  static int enable_refcnt;
b2476490e   Mike Turquette   clk: introduce th...
34
35
36
  static HLIST_HEAD(clk_root_list);
  static HLIST_HEAD(clk_orphan_list);
  static LIST_HEAD(clk_notifier_list);
b09d6d991   Michael Turquette   clk: remove clk-p...
37
38
39
40
41
42
43
44
45
46
47
48
49
  /***    private data structures    ***/
  
  struct clk_core {
  	const char		*name;
  	const struct clk_ops	*ops;
  	struct clk_hw		*hw;
  	struct module		*owner;
  	struct clk_core		*parent;
  	const char		**parent_names;
  	struct clk_core		**parents;
  	u8			num_parents;
  	u8			new_parent_index;
  	unsigned long		rate;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
50
  	unsigned long		req_rate;
b09d6d991   Michael Turquette   clk: remove clk-p...
51
52
53
54
  	unsigned long		new_rate;
  	struct clk_core		*new_parent;
  	struct clk_core		*new_child;
  	unsigned long		flags;
e6500344e   Heiko Stuebner   clk: track the or...
55
  	bool			orphan;
b09d6d991   Michael Turquette   clk: remove clk-p...
56
57
  	unsigned int		enable_count;
  	unsigned int		prepare_count;
9783c0d98   Stephen Boyd   clk: Allow provid...
58
59
  	unsigned long		min_rate;
  	unsigned long		max_rate;
b09d6d991   Michael Turquette   clk: remove clk-p...
60
61
62
63
  	unsigned long		accuracy;
  	int			phase;
  	struct hlist_head	children;
  	struct hlist_node	child_node;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
64
  	struct hlist_head	clks;
b09d6d991   Michael Turquette   clk: remove clk-p...
65
66
67
  	unsigned int		notifier_count;
  #ifdef CONFIG_DEBUG_FS
  	struct dentry		*dentry;
8c9a8a8f7   Maxime Coquelin   clk: Move debug_n...
68
  	struct hlist_node	debug_node;
b09d6d991   Michael Turquette   clk: remove clk-p...
69
70
71
  #endif
  	struct kref		ref;
  };
dfc202ead   Stephen Boyd   clk: Add tracepoi...
72
73
  #define CREATE_TRACE_POINTS
  #include <trace/events/clk.h>
b09d6d991   Michael Turquette   clk: remove clk-p...
74
75
76
77
  struct clk {
  	struct clk_core	*core;
  	const char *dev_id;
  	const char *con_id;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
78
79
  	unsigned long min_rate;
  	unsigned long max_rate;
50595f8b9   Stephen Boyd   clk: Rename child...
80
  	struct hlist_node clks_node;
b09d6d991   Michael Turquette   clk: remove clk-p...
81
  };
eab89f690   Mike Turquette   clk: abstract loc...
82
83
84
  /***           locking             ***/
  static void clk_prepare_lock(void)
  {
533ddeb1e   Mike Turquette   clk: allow reentr...
85
86
87
88
89
90
91
92
93
94
95
  	if (!mutex_trylock(&prepare_lock)) {
  		if (prepare_owner == current) {
  			prepare_refcnt++;
  			return;
  		}
  		mutex_lock(&prepare_lock);
  	}
  	WARN_ON_ONCE(prepare_owner != NULL);
  	WARN_ON_ONCE(prepare_refcnt != 0);
  	prepare_owner = current;
  	prepare_refcnt = 1;
eab89f690   Mike Turquette   clk: abstract loc...
96
97
98
99
  }
  
  static void clk_prepare_unlock(void)
  {
533ddeb1e   Mike Turquette   clk: allow reentr...
100
101
102
103
104
105
  	WARN_ON_ONCE(prepare_owner != current);
  	WARN_ON_ONCE(prepare_refcnt == 0);
  
  	if (--prepare_refcnt)
  		return;
  	prepare_owner = NULL;
eab89f690   Mike Turquette   clk: abstract loc...
106
107
108
109
  	mutex_unlock(&prepare_lock);
  }
  
  static unsigned long clk_enable_lock(void)
a57aa1853   Stephen Boyd   clk: Silence warn...
110
  	__acquires(enable_lock)
eab89f690   Mike Turquette   clk: abstract loc...
111
112
  {
  	unsigned long flags;
533ddeb1e   Mike Turquette   clk: allow reentr...
113
114
115
116
  
  	if (!spin_trylock_irqsave(&enable_lock, flags)) {
  		if (enable_owner == current) {
  			enable_refcnt++;
a57aa1853   Stephen Boyd   clk: Silence warn...
117
  			__acquire(enable_lock);
533ddeb1e   Mike Turquette   clk: allow reentr...
118
119
120
121
122
123
124
125
  			return flags;
  		}
  		spin_lock_irqsave(&enable_lock, flags);
  	}
  	WARN_ON_ONCE(enable_owner != NULL);
  	WARN_ON_ONCE(enable_refcnt != 0);
  	enable_owner = current;
  	enable_refcnt = 1;
eab89f690   Mike Turquette   clk: abstract loc...
126
127
128
129
  	return flags;
  }
  
  static void clk_enable_unlock(unsigned long flags)
a57aa1853   Stephen Boyd   clk: Silence warn...
130
  	__releases(enable_lock)
eab89f690   Mike Turquette   clk: abstract loc...
131
  {
533ddeb1e   Mike Turquette   clk: allow reentr...
132
133
  	WARN_ON_ONCE(enable_owner != current);
  	WARN_ON_ONCE(enable_refcnt == 0);
a57aa1853   Stephen Boyd   clk: Silence warn...
134
135
  	if (--enable_refcnt) {
  		__release(enable_lock);
533ddeb1e   Mike Turquette   clk: allow reentr...
136
  		return;
a57aa1853   Stephen Boyd   clk: Silence warn...
137
  	}
533ddeb1e   Mike Turquette   clk: allow reentr...
138
  	enable_owner = NULL;
eab89f690   Mike Turquette   clk: abstract loc...
139
140
  	spin_unlock_irqrestore(&enable_lock, flags);
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
141
142
143
144
145
146
147
148
  static bool clk_core_is_prepared(struct clk_core *core)
  {
  	/*
  	 * .is_prepared is optional for clocks that can prepare
  	 * fall back to software usage counter if it is missing
  	 */
  	if (!core->ops->is_prepared)
  		return core->prepare_count;
b2476490e   Mike Turquette   clk: introduce th...
149

4dff95dc9   Stephen Boyd   clk: Remove forwa...
150
151
  	return core->ops->is_prepared(core->hw);
  }
b2476490e   Mike Turquette   clk: introduce th...
152

4dff95dc9   Stephen Boyd   clk: Remove forwa...
153
154
155
156
157
158
159
160
  static bool clk_core_is_enabled(struct clk_core *core)
  {
  	/*
  	 * .is_enabled is only mandatory for clocks that gate
  	 * fall back to software usage counter if .is_enabled is missing
  	 */
  	if (!core->ops->is_enabled)
  		return core->enable_count;
6b44c854b   Sachin Kamat   clk: Fix build wa...
161

4dff95dc9   Stephen Boyd   clk: Remove forwa...
162
163
  	return core->ops->is_enabled(core->hw);
  }
6b44c854b   Sachin Kamat   clk: Fix build wa...
164

4dff95dc9   Stephen Boyd   clk: Remove forwa...
165
  /***    helper functions   ***/
1af599df6   Prashant Gaikwad   clk: human-readab...
166

b76281cb9   Geert Uytterhoeven   clk: Make clk inp...
167
  const char *__clk_get_name(const struct clk *clk)
1af599df6   Prashant Gaikwad   clk: human-readab...
168
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
169
  	return !clk ? NULL : clk->core->name;
1af599df6   Prashant Gaikwad   clk: human-readab...
170
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
171
  EXPORT_SYMBOL_GPL(__clk_get_name);
1af599df6   Prashant Gaikwad   clk: human-readab...
172

e7df6f6e2   Stephen Boyd   clk: Constify clk...
173
  const char *clk_hw_get_name(const struct clk_hw *hw)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
174
175
176
177
  {
  	return hw->core->name;
  }
  EXPORT_SYMBOL_GPL(clk_hw_get_name);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
178
179
180
181
182
  struct clk_hw *__clk_get_hw(struct clk *clk)
  {
  	return !clk ? NULL : clk->core->hw;
  }
  EXPORT_SYMBOL_GPL(__clk_get_hw);
1af599df6   Prashant Gaikwad   clk: human-readab...
183

e7df6f6e2   Stephen Boyd   clk: Constify clk...
184
  unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
185
186
187
188
  {
  	return hw->core->num_parents;
  }
  EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
e7df6f6e2   Stephen Boyd   clk: Constify clk...
189
  struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
190
191
192
193
  {
  	return hw->core->parent ? hw->core->parent->hw : NULL;
  }
  EXPORT_SYMBOL_GPL(clk_hw_get_parent);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
194
195
  static struct clk_core *__clk_lookup_subtree(const char *name,
  					     struct clk_core *core)
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
196
  {
035a61c31   Tomeu Vizoso   clk: Make clk API...
197
  	struct clk_core *child;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
198
  	struct clk_core *ret;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
199

4dff95dc9   Stephen Boyd   clk: Remove forwa...
200
201
  	if (!strcmp(core->name, name))
  		return core;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
202

4dff95dc9   Stephen Boyd   clk: Remove forwa...
203
204
205
206
  	hlist_for_each_entry(child, &core->children, child_node) {
  		ret = __clk_lookup_subtree(name, child);
  		if (ret)
  			return ret;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
207
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
208
  	return NULL;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
209
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
210
  static struct clk_core *clk_core_lookup(const char *name)
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
211
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
212
213
  	struct clk_core *root_clk;
  	struct clk_core *ret;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
214

4dff95dc9   Stephen Boyd   clk: Remove forwa...
215
216
  	if (!name)
  		return NULL;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
217

4dff95dc9   Stephen Boyd   clk: Remove forwa...
218
219
220
221
222
  	/* search the 'proper' clk tree first */
  	hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  		ret = __clk_lookup_subtree(name, root_clk);
  		if (ret)
  			return ret;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
223
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
224
225
226
227
228
229
  	/* if not found, then search the orphan tree */
  	hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  		ret = __clk_lookup_subtree(name, root_clk);
  		if (ret)
  			return ret;
  	}
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
230

4dff95dc9   Stephen Boyd   clk: Remove forwa...
231
  	return NULL;
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
232
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
233
234
  static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
  							 u8 index)
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
235
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
236
237
  	if (!core || index >= core->num_parents)
  		return NULL;
88cfbef2a   Masahiro Yamada   clk: simplify clk...
238
239
240
241
242
243
  
  	if (!core->parents[index])
  		core->parents[index] =
  				clk_core_lookup(core->parent_names[index]);
  
  	return core->parents[index];
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
244
  }
e7df6f6e2   Stephen Boyd   clk: Constify clk...
245
246
  struct clk_hw *
  clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
247
248
249
250
251
252
253
254
  {
  	struct clk_core *parent;
  
  	parent = clk_core_get_parent_by_index(hw->core, index);
  
  	return !parent ? NULL : parent->hw;
  }
  EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
255
256
257
258
  unsigned int __clk_get_enable_count(struct clk *clk)
  {
  	return !clk ? 0 : clk->core->enable_count;
  }
b2476490e   Mike Turquette   clk: introduce th...
259

4dff95dc9   Stephen Boyd   clk: Remove forwa...
260
261
262
  static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
  {
  	unsigned long ret;
b2476490e   Mike Turquette   clk: introduce th...
263

4dff95dc9   Stephen Boyd   clk: Remove forwa...
264
265
266
267
  	if (!core) {
  		ret = 0;
  		goto out;
  	}
b2476490e   Mike Turquette   clk: introduce th...
268

4dff95dc9   Stephen Boyd   clk: Remove forwa...
269
  	ret = core->rate;
b2476490e   Mike Turquette   clk: introduce th...
270

47b0eeb3d   Stephen Boyd   clk: Deprecate CL...
271
  	if (!core->num_parents)
4dff95dc9   Stephen Boyd   clk: Remove forwa...
272
  		goto out;
c646cbf10   Alex Elder   clk: support hard...
273

4dff95dc9   Stephen Boyd   clk: Remove forwa...
274
275
  	if (!core->parent)
  		ret = 0;
b2476490e   Mike Turquette   clk: introduce th...
276

b2476490e   Mike Turquette   clk: introduce th...
277
278
279
  out:
  	return ret;
  }
e7df6f6e2   Stephen Boyd   clk: Constify clk...
280
  unsigned long clk_hw_get_rate(const struct clk_hw *hw)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
281
282
283
284
  {
  	return clk_core_get_rate_nolock(hw->core);
  }
  EXPORT_SYMBOL_GPL(clk_hw_get_rate);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
285
286
287
288
  static unsigned long __clk_get_accuracy(struct clk_core *core)
  {
  	if (!core)
  		return 0;
b2476490e   Mike Turquette   clk: introduce th...
289

4dff95dc9   Stephen Boyd   clk: Remove forwa...
290
  	return core->accuracy;
b2476490e   Mike Turquette   clk: introduce th...
291
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
292
  unsigned long __clk_get_flags(struct clk *clk)
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
293
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
294
  	return !clk ? 0 : clk->core->flags;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
295
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
296
  EXPORT_SYMBOL_GPL(__clk_get_flags);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
297

e7df6f6e2   Stephen Boyd   clk: Constify clk...
298
  unsigned long clk_hw_get_flags(const struct clk_hw *hw)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
299
300
301
302
  {
  	return hw->core->flags;
  }
  EXPORT_SYMBOL_GPL(clk_hw_get_flags);
e7df6f6e2   Stephen Boyd   clk: Constify clk...
303
  bool clk_hw_is_prepared(const struct clk_hw *hw)
1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
304
305
306
  {
  	return clk_core_is_prepared(hw->core);
  }
be68bf883   Joachim Eastwood   clk: Add clk_hw_i...
307
308
309
310
  bool clk_hw_is_enabled(const struct clk_hw *hw)
  {
  	return clk_core_is_enabled(hw->core);
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
311
  bool __clk_is_enabled(struct clk *clk)
b2476490e   Mike Turquette   clk: introduce th...
312
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
313
314
  	if (!clk)
  		return false;
b2476490e   Mike Turquette   clk: introduce th...
315

4dff95dc9   Stephen Boyd   clk: Remove forwa...
316
317
318
  	return clk_core_is_enabled(clk->core);
  }
  EXPORT_SYMBOL_GPL(__clk_is_enabled);
b2476490e   Mike Turquette   clk: introduce th...
319

4dff95dc9   Stephen Boyd   clk: Remove forwa...
320
321
322
323
324
  static bool mux_is_better_rate(unsigned long rate, unsigned long now,
  			   unsigned long best, unsigned long flags)
  {
  	if (flags & CLK_MUX_ROUND_CLOSEST)
  		return abs(now - rate) < abs(best - rate);
1af599df6   Prashant Gaikwad   clk: human-readab...
325

4dff95dc9   Stephen Boyd   clk: Remove forwa...
326
327
  	return now <= rate && now > best;
  }
bddca8944   Prashant Gaikwad   clk: JSON debugfs...
328

0817b62cc   Boris Brezillon   clk: change clk_o...
329
330
  static int
  clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
4dff95dc9   Stephen Boyd   clk: Remove forwa...
331
332
333
  			     unsigned long flags)
  {
  	struct clk_core *core = hw->core, *parent, *best_parent = NULL;
0817b62cc   Boris Brezillon   clk: change clk_o...
334
335
336
  	int i, num_parents, ret;
  	unsigned long best = 0;
  	struct clk_rate_request parent_req = *req;
b2476490e   Mike Turquette   clk: introduce th...
337

4dff95dc9   Stephen Boyd   clk: Remove forwa...
338
339
340
  	/* if NO_REPARENT flag set, pass through to current parent */
  	if (core->flags & CLK_SET_RATE_NO_REPARENT) {
  		parent = core->parent;
0817b62cc   Boris Brezillon   clk: change clk_o...
341
342
343
344
345
346
347
348
  		if (core->flags & CLK_SET_RATE_PARENT) {
  			ret = __clk_determine_rate(parent ? parent->hw : NULL,
  						   &parent_req);
  			if (ret)
  				return ret;
  
  			best = parent_req.rate;
  		} else if (parent) {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
349
  			best = clk_core_get_rate_nolock(parent);
0817b62cc   Boris Brezillon   clk: change clk_o...
350
  		} else {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
351
  			best = clk_core_get_rate_nolock(core);
0817b62cc   Boris Brezillon   clk: change clk_o...
352
  		}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
353
354
  		goto out;
  	}
b2476490e   Mike Turquette   clk: introduce th...
355

4dff95dc9   Stephen Boyd   clk: Remove forwa...
356
357
358
359
360
361
  	/* find the parent that can provide the fastest rate <= rate */
  	num_parents = core->num_parents;
  	for (i = 0; i < num_parents; i++) {
  		parent = clk_core_get_parent_by_index(core, i);
  		if (!parent)
  			continue;
0817b62cc   Boris Brezillon   clk: change clk_o...
362
363
364
365
366
367
368
369
370
371
372
373
  
  		if (core->flags & CLK_SET_RATE_PARENT) {
  			parent_req = *req;
  			ret = __clk_determine_rate(parent->hw, &parent_req);
  			if (ret)
  				continue;
  		} else {
  			parent_req.rate = clk_core_get_rate_nolock(parent);
  		}
  
  		if (mux_is_better_rate(req->rate, parent_req.rate,
  				       best, flags)) {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
374
  			best_parent = parent;
0817b62cc   Boris Brezillon   clk: change clk_o...
375
  			best = parent_req.rate;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
376
377
  		}
  	}
b2476490e   Mike Turquette   clk: introduce th...
378

57d866e60   Boris Brezillon   clk: fix some det...
379
380
  	if (!best_parent)
  		return -EINVAL;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
381
382
  out:
  	if (best_parent)
0817b62cc   Boris Brezillon   clk: change clk_o...
383
384
385
  		req->best_parent_hw = best_parent->hw;
  	req->best_parent_rate = best;
  	req->rate = best;
b2476490e   Mike Turquette   clk: introduce th...
386

0817b62cc   Boris Brezillon   clk: change clk_o...
387
  	return 0;
b33d212f4   Ulf Hansson   clk: Restructure ...
388
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
389
390
  
  struct clk *__clk_lookup(const char *name)
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
391
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
392
393
394
  	struct clk_core *core = clk_core_lookup(name);
  
  	return !core ? NULL : core->hw->clk;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
395
  }
b2476490e   Mike Turquette   clk: introduce th...
396

4dff95dc9   Stephen Boyd   clk: Remove forwa...
397
398
399
  static void clk_core_get_boundaries(struct clk_core *core,
  				    unsigned long *min_rate,
  				    unsigned long *max_rate)
1c155b3df   Ulf Hansson   clk: Unprepare th...
400
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
401
  	struct clk *clk_user;
1c155b3df   Ulf Hansson   clk: Unprepare th...
402

9783c0d98   Stephen Boyd   clk: Allow provid...
403
404
  	*min_rate = core->min_rate;
  	*max_rate = core->max_rate;
496eadf82   Krzysztof Kozlowski   clk: Use lockdep ...
405

4dff95dc9   Stephen Boyd   clk: Remove forwa...
406
407
  	hlist_for_each_entry(clk_user, &core->clks, clks_node)
  		*min_rate = max(*min_rate, clk_user->min_rate);
1c155b3df   Ulf Hansson   clk: Unprepare th...
408

4dff95dc9   Stephen Boyd   clk: Remove forwa...
409
410
411
  	hlist_for_each_entry(clk_user, &core->clks, clks_node)
  		*max_rate = min(*max_rate, clk_user->max_rate);
  }
1c155b3df   Ulf Hansson   clk: Unprepare th...
412

9783c0d98   Stephen Boyd   clk: Allow provid...
413
414
415
416
417
418
419
  void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
  			   unsigned long max_rate)
  {
  	hw->core->min_rate = min_rate;
  	hw->core->max_rate = max_rate;
  }
  EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
420
421
422
423
424
  /*
   * Helper for finding best parent to provide a given frequency. This can be used
   * directly as a determine_rate callback (e.g. for a mux), or from a more
   * complex clock that may combine a mux with other operations.
   */
0817b62cc   Boris Brezillon   clk: change clk_o...
425
426
  int __clk_mux_determine_rate(struct clk_hw *hw,
  			     struct clk_rate_request *req)
4dff95dc9   Stephen Boyd   clk: Remove forwa...
427
  {
0817b62cc   Boris Brezillon   clk: change clk_o...
428
  	return clk_mux_determine_rate_flags(hw, req, 0);
1c155b3df   Ulf Hansson   clk: Unprepare th...
429
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
430
  EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
1c155b3df   Ulf Hansson   clk: Unprepare th...
431

0817b62cc   Boris Brezillon   clk: change clk_o...
432
433
  int __clk_mux_determine_rate_closest(struct clk_hw *hw,
  				     struct clk_rate_request *req)
b2476490e   Mike Turquette   clk: introduce th...
434
  {
0817b62cc   Boris Brezillon   clk: change clk_o...
435
  	return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
436
437
  }
  EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
b2476490e   Mike Turquette   clk: introduce th...
438

4dff95dc9   Stephen Boyd   clk: Remove forwa...
439
  /***        clk api        ***/
496eadf82   Krzysztof Kozlowski   clk: Use lockdep ...
440

4dff95dc9   Stephen Boyd   clk: Remove forwa...
441
442
  static void clk_core_unprepare(struct clk_core *core)
  {
a63347251   Stephen Boyd   clk: Add some mor...
443
  	lockdep_assert_held(&prepare_lock);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
444
445
  	if (!core)
  		return;
b2476490e   Mike Turquette   clk: introduce th...
446

4dff95dc9   Stephen Boyd   clk: Remove forwa...
447
448
  	if (WARN_ON(core->prepare_count == 0))
  		return;
b2476490e   Mike Turquette   clk: introduce th...
449

2e20fbf59   Lee Jones   clk: WARN_ON abou...
450
451
  	if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
  		return;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
452
453
  	if (--core->prepare_count > 0)
  		return;
b2476490e   Mike Turquette   clk: introduce th...
454

4dff95dc9   Stephen Boyd   clk: Remove forwa...
455
  	WARN_ON(core->enable_count > 0);
b2476490e   Mike Turquette   clk: introduce th...
456

4dff95dc9   Stephen Boyd   clk: Remove forwa...
457
  	trace_clk_unprepare(core);
b2476490e   Mike Turquette   clk: introduce th...
458

4dff95dc9   Stephen Boyd   clk: Remove forwa...
459
460
461
462
463
  	if (core->ops->unprepare)
  		core->ops->unprepare(core->hw);
  
  	trace_clk_unprepare_complete(core);
  	clk_core_unprepare(core->parent);
b2476490e   Mike Turquette   clk: introduce th...
464
  }
a6adc30ba   Dong Aisheng   clk: introduce cl...
465
466
467
468
469
470
  static void clk_core_unprepare_lock(struct clk_core *core)
  {
  	clk_prepare_lock();
  	clk_core_unprepare(core);
  	clk_prepare_unlock();
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
471
472
473
474
475
476
477
478
479
480
481
482
  /**
   * clk_unprepare - undo preparation of a clock source
   * @clk: the clk being unprepared
   *
   * clk_unprepare may sleep, which differentiates it from clk_disable.  In a
   * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
   * if the operation may sleep.  One example is a clk which is accessed over
   * I2c.  In the complex case a clk gate operation may require a fast and a slow
   * part.  It is this reason that clk_unprepare and clk_disable are not mutually
   * exclusive.  In fact clk_disable must be called before clk_unprepare.
   */
  void clk_unprepare(struct clk *clk)
1e435256d   Olof Johansson   clk: add clk_igno...
483
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
484
485
  	if (IS_ERR_OR_NULL(clk))
  		return;
a6adc30ba   Dong Aisheng   clk: introduce cl...
486
  	clk_core_unprepare_lock(clk->core);
1e435256d   Olof Johansson   clk: add clk_igno...
487
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
488
  EXPORT_SYMBOL_GPL(clk_unprepare);
1e435256d   Olof Johansson   clk: add clk_igno...
489

4dff95dc9   Stephen Boyd   clk: Remove forwa...
490
  static int clk_core_prepare(struct clk_core *core)
b2476490e   Mike Turquette   clk: introduce th...
491
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
492
  	int ret = 0;
b2476490e   Mike Turquette   clk: introduce th...
493

a63347251   Stephen Boyd   clk: Add some mor...
494
  	lockdep_assert_held(&prepare_lock);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
495
  	if (!core)
1e435256d   Olof Johansson   clk: add clk_igno...
496
  		return 0;
1e435256d   Olof Johansson   clk: add clk_igno...
497

4dff95dc9   Stephen Boyd   clk: Remove forwa...
498
499
500
501
  	if (core->prepare_count == 0) {
  		ret = clk_core_prepare(core->parent);
  		if (ret)
  			return ret;
b2476490e   Mike Turquette   clk: introduce th...
502

4dff95dc9   Stephen Boyd   clk: Remove forwa...
503
  		trace_clk_prepare(core);
b2476490e   Mike Turquette   clk: introduce th...
504

4dff95dc9   Stephen Boyd   clk: Remove forwa...
505
506
  		if (core->ops->prepare)
  			ret = core->ops->prepare(core->hw);
b2476490e   Mike Turquette   clk: introduce th...
507

4dff95dc9   Stephen Boyd   clk: Remove forwa...
508
  		trace_clk_prepare_complete(core);
1c155b3df   Ulf Hansson   clk: Unprepare th...
509

4dff95dc9   Stephen Boyd   clk: Remove forwa...
510
511
512
513
514
  		if (ret) {
  			clk_core_unprepare(core->parent);
  			return ret;
  		}
  	}
1c155b3df   Ulf Hansson   clk: Unprepare th...
515

4dff95dc9   Stephen Boyd   clk: Remove forwa...
516
  	core->prepare_count++;
b2476490e   Mike Turquette   clk: introduce th...
517
518
519
  
  	return 0;
  }
b2476490e   Mike Turquette   clk: introduce th...
520

a6adc30ba   Dong Aisheng   clk: introduce cl...
521
522
523
524
525
526
527
528
529
530
  static int clk_core_prepare_lock(struct clk_core *core)
  {
  	int ret;
  
  	clk_prepare_lock();
  	ret = clk_core_prepare(core);
  	clk_prepare_unlock();
  
  	return ret;
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
531
532
533
534
535
536
537
538
539
540
541
542
543
  /**
   * clk_prepare - prepare a clock source
   * @clk: the clk being prepared
   *
   * clk_prepare may sleep, which differentiates it from clk_enable.  In a simple
   * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
   * operation may sleep.  One example is a clk which is accessed over I2c.  In
   * the complex case a clk ungate operation may require a fast and a slow part.
   * It is this reason that clk_prepare and clk_enable are not mutually
   * exclusive.  In fact clk_prepare must be called before clk_enable.
   * Returns 0 on success, -EERROR otherwise.
   */
  int clk_prepare(struct clk *clk)
b2476490e   Mike Turquette   clk: introduce th...
544
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
545
546
  	if (!clk)
  		return 0;
b2476490e   Mike Turquette   clk: introduce th...
547

a6adc30ba   Dong Aisheng   clk: introduce cl...
548
  	return clk_core_prepare_lock(clk->core);
b2476490e   Mike Turquette   clk: introduce th...
549
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
550
  EXPORT_SYMBOL_GPL(clk_prepare);
b2476490e   Mike Turquette   clk: introduce th...
551

4dff95dc9   Stephen Boyd   clk: Remove forwa...
552
  static void clk_core_disable(struct clk_core *core)
b2476490e   Mike Turquette   clk: introduce th...
553
  {
a63347251   Stephen Boyd   clk: Add some mor...
554
  	lockdep_assert_held(&enable_lock);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
555
556
  	if (!core)
  		return;
035a61c31   Tomeu Vizoso   clk: Make clk API...
557

4dff95dc9   Stephen Boyd   clk: Remove forwa...
558
559
  	if (WARN_ON(core->enable_count == 0))
  		return;
b2476490e   Mike Turquette   clk: introduce th...
560

2e20fbf59   Lee Jones   clk: WARN_ON abou...
561
562
  	if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
  		return;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
563
564
  	if (--core->enable_count > 0)
  		return;
035a61c31   Tomeu Vizoso   clk: Make clk API...
565

2f87a6ea1   Paul E. McKenney   clk: Add _rcuidle...
566
  	trace_clk_disable_rcuidle(core);
035a61c31   Tomeu Vizoso   clk: Make clk API...
567

4dff95dc9   Stephen Boyd   clk: Remove forwa...
568
569
  	if (core->ops->disable)
  		core->ops->disable(core->hw);
035a61c31   Tomeu Vizoso   clk: Make clk API...
570

2f87a6ea1   Paul E. McKenney   clk: Add _rcuidle...
571
  	trace_clk_disable_complete_rcuidle(core);
035a61c31   Tomeu Vizoso   clk: Make clk API...
572

4dff95dc9   Stephen Boyd   clk: Remove forwa...
573
  	clk_core_disable(core->parent);
035a61c31   Tomeu Vizoso   clk: Make clk API...
574
  }
7ef3dcc81   James Hogan   clk: abstract par...
575

a6adc30ba   Dong Aisheng   clk: introduce cl...
576
577
578
579
580
581
582
583
  static void clk_core_disable_lock(struct clk_core *core)
  {
  	unsigned long flags;
  
  	flags = clk_enable_lock();
  	clk_core_disable(core);
  	clk_enable_unlock(flags);
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
584
585
586
587
588
589
590
591
592
593
594
595
596
  /**
   * clk_disable - gate a clock
   * @clk: the clk being gated
   *
   * clk_disable must not sleep, which differentiates it from clk_unprepare.  In
   * a simple case, clk_disable can be used instead of clk_unprepare to gate a
   * clk if the operation is fast and will never sleep.  One example is a
   * SoC-internal clk which is controlled via simple register writes.  In the
   * complex case a clk gate operation may require a fast and a slow part.  It is
   * this reason that clk_unprepare and clk_disable are not mutually exclusive.
   * In fact clk_disable must be called before clk_unprepare.
   */
  void clk_disable(struct clk *clk)
b2476490e   Mike Turquette   clk: introduce th...
597
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
598
599
  	if (IS_ERR_OR_NULL(clk))
  		return;
a6adc30ba   Dong Aisheng   clk: introduce cl...
600
  	clk_core_disable_lock(clk->core);
b2476490e   Mike Turquette   clk: introduce th...
601
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
602
  EXPORT_SYMBOL_GPL(clk_disable);
b2476490e   Mike Turquette   clk: introduce th...
603

4dff95dc9   Stephen Boyd   clk: Remove forwa...
604
  static int clk_core_enable(struct clk_core *core)
b2476490e   Mike Turquette   clk: introduce th...
605
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
606
  	int ret = 0;
b2476490e   Mike Turquette   clk: introduce th...
607

a63347251   Stephen Boyd   clk: Add some mor...
608
  	lockdep_assert_held(&enable_lock);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
609
610
  	if (!core)
  		return 0;
b2476490e   Mike Turquette   clk: introduce th...
611

4dff95dc9   Stephen Boyd   clk: Remove forwa...
612
613
  	if (WARN_ON(core->prepare_count == 0))
  		return -ESHUTDOWN;
b2476490e   Mike Turquette   clk: introduce th...
614

4dff95dc9   Stephen Boyd   clk: Remove forwa...
615
616
  	if (core->enable_count == 0) {
  		ret = clk_core_enable(core->parent);
b2476490e   Mike Turquette   clk: introduce th...
617

4dff95dc9   Stephen Boyd   clk: Remove forwa...
618
619
  		if (ret)
  			return ret;
b2476490e   Mike Turquette   clk: introduce th...
620

f17a0dd1c   Paul E. McKenney   clk: Use _rcuidle...
621
  		trace_clk_enable_rcuidle(core);
035a61c31   Tomeu Vizoso   clk: Make clk API...
622

4dff95dc9   Stephen Boyd   clk: Remove forwa...
623
624
  		if (core->ops->enable)
  			ret = core->ops->enable(core->hw);
035a61c31   Tomeu Vizoso   clk: Make clk API...
625

f17a0dd1c   Paul E. McKenney   clk: Use _rcuidle...
626
  		trace_clk_enable_complete_rcuidle(core);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
627
628
629
630
631
632
633
634
635
  
  		if (ret) {
  			clk_core_disable(core->parent);
  			return ret;
  		}
  	}
  
  	core->enable_count++;
  	return 0;
035a61c31   Tomeu Vizoso   clk: Make clk API...
636
  }
b2476490e   Mike Turquette   clk: introduce th...
637

a6adc30ba   Dong Aisheng   clk: introduce cl...
638
639
640
641
642
643
644
645
646
647
648
  static int clk_core_enable_lock(struct clk_core *core)
  {
  	unsigned long flags;
  	int ret;
  
  	flags = clk_enable_lock();
  	ret = clk_core_enable(core);
  	clk_enable_unlock(flags);
  
  	return ret;
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
649
650
651
652
653
654
655
656
657
658
659
660
661
662
  /**
   * clk_enable - ungate a clock
   * @clk: the clk being ungated
   *
   * clk_enable must not sleep, which differentiates it from clk_prepare.  In a
   * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
   * if the operation will never sleep.  One example is a SoC-internal clk which
   * is controlled via simple register writes.  In the complex case a clk ungate
   * operation may require a fast and a slow part.  It is this reason that
   * clk_enable and clk_prepare are not mutually exclusive.  In fact clk_prepare
   * must be called before clk_enable.  Returns 0 on success, -EERROR
   * otherwise.
   */
  int clk_enable(struct clk *clk)
5279fc402   Boris BREZILLON   clk: add clk accu...
663
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
664
  	if (!clk)
5279fc402   Boris BREZILLON   clk: add clk accu...
665
  		return 0;
a6adc30ba   Dong Aisheng   clk: introduce cl...
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
  	return clk_core_enable_lock(clk->core);
  }
  EXPORT_SYMBOL_GPL(clk_enable);
  
  static int clk_core_prepare_enable(struct clk_core *core)
  {
  	int ret;
  
  	ret = clk_core_prepare_lock(core);
  	if (ret)
  		return ret;
  
  	ret = clk_core_enable_lock(core);
  	if (ret)
  		clk_core_unprepare_lock(core);
5279fc402   Boris BREZILLON   clk: add clk accu...
681

4dff95dc9   Stephen Boyd   clk: Remove forwa...
682
  	return ret;
b2476490e   Mike Turquette   clk: introduce th...
683
  }
a6adc30ba   Dong Aisheng   clk: introduce cl...
684
685
686
687
688
689
  
  static void clk_core_disable_unprepare(struct clk_core *core)
  {
  	clk_core_disable_lock(core);
  	clk_core_unprepare_lock(core);
  }
b2476490e   Mike Turquette   clk: introduce th...
690

7ec986efe   Dong Aisheng   clk: move clk_dis...
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  static void clk_unprepare_unused_subtree(struct clk_core *core)
  {
  	struct clk_core *child;
  
  	lockdep_assert_held(&prepare_lock);
  
  	hlist_for_each_entry(child, &core->children, child_node)
  		clk_unprepare_unused_subtree(child);
  
  	if (core->prepare_count)
  		return;
  
  	if (core->flags & CLK_IGNORE_UNUSED)
  		return;
  
  	if (clk_core_is_prepared(core)) {
  		trace_clk_unprepare(core);
  		if (core->ops->unprepare_unused)
  			core->ops->unprepare_unused(core->hw);
  		else if (core->ops->unprepare)
  			core->ops->unprepare(core->hw);
  		trace_clk_unprepare_complete(core);
  	}
  }
  
  static void clk_disable_unused_subtree(struct clk_core *core)
  {
  	struct clk_core *child;
  	unsigned long flags;
  
  	lockdep_assert_held(&prepare_lock);
  
  	hlist_for_each_entry(child, &core->children, child_node)
  		clk_disable_unused_subtree(child);
a4b3518d1   Dong Aisheng   clk: core: suppor...
725
726
  	if (core->flags & CLK_OPS_PARENT_ENABLE)
  		clk_core_prepare_enable(core->parent);
7ec986efe   Dong Aisheng   clk: move clk_dis...
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  	flags = clk_enable_lock();
  
  	if (core->enable_count)
  		goto unlock_out;
  
  	if (core->flags & CLK_IGNORE_UNUSED)
  		goto unlock_out;
  
  	/*
  	 * some gate clocks have special needs during the disable-unused
  	 * sequence.  call .disable_unused if available, otherwise fall
  	 * back to .disable
  	 */
  	if (clk_core_is_enabled(core)) {
  		trace_clk_disable(core);
  		if (core->ops->disable_unused)
  			core->ops->disable_unused(core->hw);
  		else if (core->ops->disable)
  			core->ops->disable(core->hw);
  		trace_clk_disable_complete(core);
  	}
  
  unlock_out:
  	clk_enable_unlock(flags);
a4b3518d1   Dong Aisheng   clk: core: suppor...
751
752
  	if (core->flags & CLK_OPS_PARENT_ENABLE)
  		clk_core_disable_unprepare(core->parent);
7ec986efe   Dong Aisheng   clk: move clk_dis...
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
  }
  
  static bool clk_ignore_unused;
  static int __init clk_ignore_unused_setup(char *__unused)
  {
  	clk_ignore_unused = true;
  	return 1;
  }
  __setup("clk_ignore_unused", clk_ignore_unused_setup);
  
  static int clk_disable_unused(void)
  {
  	struct clk_core *core;
  
  	if (clk_ignore_unused) {
  		pr_warn("clk: Not disabling unused clocks
  ");
  		return 0;
  	}
  
  	clk_prepare_lock();
  
  	hlist_for_each_entry(core, &clk_root_list, child_node)
  		clk_disable_unused_subtree(core);
  
  	hlist_for_each_entry(core, &clk_orphan_list, child_node)
  		clk_disable_unused_subtree(core);
  
  	hlist_for_each_entry(core, &clk_root_list, child_node)
  		clk_unprepare_unused_subtree(core);
  
  	hlist_for_each_entry(core, &clk_orphan_list, child_node)
  		clk_unprepare_unused_subtree(core);
  
  	clk_prepare_unlock();
  
  	return 0;
  }
  late_initcall_sync(clk_disable_unused);
0817b62cc   Boris Brezillon   clk: change clk_o...
792
793
  static int clk_core_round_rate_nolock(struct clk_core *core,
  				      struct clk_rate_request *req)
3d6ee287a   Ulf Hansson   clk: Introduce op...
794
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
795
  	struct clk_core *parent;
0817b62cc   Boris Brezillon   clk: change clk_o...
796
  	long rate;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
797
798
  
  	lockdep_assert_held(&prepare_lock);
3d6ee287a   Ulf Hansson   clk: Introduce op...
799

d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
800
  	if (!core)
4dff95dc9   Stephen Boyd   clk: Remove forwa...
801
  		return 0;
3d6ee287a   Ulf Hansson   clk: Introduce op...
802

4dff95dc9   Stephen Boyd   clk: Remove forwa...
803
  	parent = core->parent;
0817b62cc   Boris Brezillon   clk: change clk_o...
804
805
806
807
808
809
810
  	if (parent) {
  		req->best_parent_hw = parent->hw;
  		req->best_parent_rate = parent->rate;
  	} else {
  		req->best_parent_hw = NULL;
  		req->best_parent_rate = 0;
  	}
3d6ee287a   Ulf Hansson   clk: Introduce op...
811

4dff95dc9   Stephen Boyd   clk: Remove forwa...
812
  	if (core->ops->determine_rate) {
0817b62cc   Boris Brezillon   clk: change clk_o...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
  		return core->ops->determine_rate(core->hw, req);
  	} else if (core->ops->round_rate) {
  		rate = core->ops->round_rate(core->hw, req->rate,
  					     &req->best_parent_rate);
  		if (rate < 0)
  			return rate;
  
  		req->rate = rate;
  	} else if (core->flags & CLK_SET_RATE_PARENT) {
  		return clk_core_round_rate_nolock(parent, req);
  	} else {
  		req->rate = core->rate;
  	}
  
  	return 0;
3d6ee287a   Ulf Hansson   clk: Introduce op...
828
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
829
830
831
  /**
   * __clk_determine_rate - get the closest rate actually supported by a clock
   * @hw: determine the rate of this clock
2d5b520cf   Peng Fan   clk: correct comm...
832
   * @req: target rate request
4dff95dc9   Stephen Boyd   clk: Remove forwa...
833
   *
6e5ab41b1   Stephen Boyd   clk: Update some ...
834
   * Useful for clk_ops such as .set_rate and .determine_rate.
4dff95dc9   Stephen Boyd   clk: Remove forwa...
835
   */
0817b62cc   Boris Brezillon   clk: change clk_o...
836
  int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
035a61c31   Tomeu Vizoso   clk: Make clk API...
837
  {
0817b62cc   Boris Brezillon   clk: change clk_o...
838
839
  	if (!hw) {
  		req->rate = 0;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
840
  		return 0;
0817b62cc   Boris Brezillon   clk: change clk_o...
841
  	}
035a61c31   Tomeu Vizoso   clk: Make clk API...
842

0817b62cc   Boris Brezillon   clk: change clk_o...
843
  	return clk_core_round_rate_nolock(hw->core, req);
035a61c31   Tomeu Vizoso   clk: Make clk API...
844
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
845
  EXPORT_SYMBOL_GPL(__clk_determine_rate);
035a61c31   Tomeu Vizoso   clk: Make clk API...
846

1a9c069cb   Stephen Boyd   clk: Add clk_hw_*...
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
  unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
  {
  	int ret;
  	struct clk_rate_request req;
  
  	clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
  	req.rate = rate;
  
  	ret = clk_core_round_rate_nolock(hw->core, &req);
  	if (ret)
  		return 0;
  
  	return req.rate;
  }
  EXPORT_SYMBOL_GPL(clk_hw_round_rate);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
862
863
864
865
866
867
868
869
870
871
  /**
   * clk_round_rate - round the given rate for a clk
   * @clk: the clk for which we are rounding a rate
   * @rate: the rate which is to be rounded
   *
   * Takes in a rate as input and rounds it to a rate that the clk can actually
   * use which is then returned.  If clk doesn't support round_rate operation
   * then the parent rate is returned.
   */
  long clk_round_rate(struct clk *clk, unsigned long rate)
035a61c31   Tomeu Vizoso   clk: Make clk API...
872
  {
fc4a05d4b   Stephen Boyd   clk: Remove unuse...
873
874
  	struct clk_rate_request req;
  	int ret;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
875

035a61c31   Tomeu Vizoso   clk: Make clk API...
876
  	if (!clk)
4dff95dc9   Stephen Boyd   clk: Remove forwa...
877
  		return 0;
035a61c31   Tomeu Vizoso   clk: Make clk API...
878

4dff95dc9   Stephen Boyd   clk: Remove forwa...
879
  	clk_prepare_lock();
fc4a05d4b   Stephen Boyd   clk: Remove unuse...
880
881
882
883
884
  
  	clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
  	req.rate = rate;
  
  	ret = clk_core_round_rate_nolock(clk->core, &req);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
885
  	clk_prepare_unlock();
fc4a05d4b   Stephen Boyd   clk: Remove unuse...
886
887
888
889
  	if (ret)
  		return ret;
  
  	return req.rate;
035a61c31   Tomeu Vizoso   clk: Make clk API...
890
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
891
  EXPORT_SYMBOL_GPL(clk_round_rate);
b2476490e   Mike Turquette   clk: introduce th...
892

4dff95dc9   Stephen Boyd   clk: Remove forwa...
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
  /**
   * __clk_notify - call clk notifier chain
   * @core: clk that is changing rate
   * @msg: clk notifier type (see include/linux/clk.h)
   * @old_rate: old clk rate
   * @new_rate: new clk rate
   *
   * Triggers a notifier call chain on the clk rate-change notification
   * for 'clk'.  Passes a pointer to the struct clk and the previous
   * and current rates to the notifier callback.  Intended to be called by
   * internal clock code only.  Returns NOTIFY_DONE from the last driver
   * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
   * a driver returns that.
   */
  static int __clk_notify(struct clk_core *core, unsigned long msg,
  		unsigned long old_rate, unsigned long new_rate)
b2476490e   Mike Turquette   clk: introduce th...
909
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
910
911
912
  	struct clk_notifier *cn;
  	struct clk_notifier_data cnd;
  	int ret = NOTIFY_DONE;
b2476490e   Mike Turquette   clk: introduce th...
913

4dff95dc9   Stephen Boyd   clk: Remove forwa...
914
915
  	cnd.old_rate = old_rate;
  	cnd.new_rate = new_rate;
b2476490e   Mike Turquette   clk: introduce th...
916

4dff95dc9   Stephen Boyd   clk: Remove forwa...
917
918
919
920
921
922
  	list_for_each_entry(cn, &clk_notifier_list, node) {
  		if (cn->clk->core == core) {
  			cnd.clk = cn->clk;
  			ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  					&cnd);
  		}
b2476490e   Mike Turquette   clk: introduce th...
923
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
924
  	return ret;
b2476490e   Mike Turquette   clk: introduce th...
925
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
926
927
928
929
930
931
  /**
   * __clk_recalc_accuracies
   * @core: first clk in the subtree
   *
   * Walks the subtree of clks starting with clk and recalculates accuracies as
   * it goes.  Note that if a clk does not implement the .recalc_accuracy
6e5ab41b1   Stephen Boyd   clk: Update some ...
932
   * callback then it is assumed that the clock will take on the accuracy of its
4dff95dc9   Stephen Boyd   clk: Remove forwa...
933
   * parent.
4dff95dc9   Stephen Boyd   clk: Remove forwa...
934
935
   */
  static void __clk_recalc_accuracies(struct clk_core *core)
b2476490e   Mike Turquette   clk: introduce th...
936
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
937
938
  	unsigned long parent_accuracy = 0;
  	struct clk_core *child;
b2476490e   Mike Turquette   clk: introduce th...
939

4dff95dc9   Stephen Boyd   clk: Remove forwa...
940
  	lockdep_assert_held(&prepare_lock);
b2476490e   Mike Turquette   clk: introduce th...
941

4dff95dc9   Stephen Boyd   clk: Remove forwa...
942
943
  	if (core->parent)
  		parent_accuracy = core->parent->accuracy;
b2476490e   Mike Turquette   clk: introduce th...
944

4dff95dc9   Stephen Boyd   clk: Remove forwa...
945
946
947
948
949
  	if (core->ops->recalc_accuracy)
  		core->accuracy = core->ops->recalc_accuracy(core->hw,
  							  parent_accuracy);
  	else
  		core->accuracy = parent_accuracy;
b2476490e   Mike Turquette   clk: introduce th...
950

4dff95dc9   Stephen Boyd   clk: Remove forwa...
951
952
  	hlist_for_each_entry(child, &core->children, child_node)
  		__clk_recalc_accuracies(child);
b2476490e   Mike Turquette   clk: introduce th...
953
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
954
  static long clk_core_get_accuracy(struct clk_core *core)
e366fdd72   James Hogan   clk: clk-mux: imp...
955
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
956
  	unsigned long accuracy;
15a02c1f6   Stephen Boyd   clk: Add __clk_mu...
957

4dff95dc9   Stephen Boyd   clk: Remove forwa...
958
959
960
  	clk_prepare_lock();
  	if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
  		__clk_recalc_accuracies(core);
15a02c1f6   Stephen Boyd   clk: Add __clk_mu...
961

4dff95dc9   Stephen Boyd   clk: Remove forwa...
962
963
  	accuracy = __clk_get_accuracy(core);
  	clk_prepare_unlock();
e366fdd72   James Hogan   clk: clk-mux: imp...
964

4dff95dc9   Stephen Boyd   clk: Remove forwa...
965
  	return accuracy;
e366fdd72   James Hogan   clk: clk-mux: imp...
966
  }
15a02c1f6   Stephen Boyd   clk: Add __clk_mu...
967

4dff95dc9   Stephen Boyd   clk: Remove forwa...
968
969
970
971
972
973
974
975
976
977
  /**
   * clk_get_accuracy - return the accuracy of clk
   * @clk: the clk whose accuracy is being returned
   *
   * Simply returns the cached accuracy of the clk, unless
   * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
   * issued.
   * If clk is NULL then returns 0.
   */
  long clk_get_accuracy(struct clk *clk)
035a61c31   Tomeu Vizoso   clk: Make clk API...
978
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
979
980
  	if (!clk)
  		return 0;
035a61c31   Tomeu Vizoso   clk: Make clk API...
981

4dff95dc9   Stephen Boyd   clk: Remove forwa...
982
  	return clk_core_get_accuracy(clk->core);
035a61c31   Tomeu Vizoso   clk: Make clk API...
983
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
984
  EXPORT_SYMBOL_GPL(clk_get_accuracy);
035a61c31   Tomeu Vizoso   clk: Make clk API...
985

4dff95dc9   Stephen Boyd   clk: Remove forwa...
986
987
  static unsigned long clk_recalc(struct clk_core *core,
  				unsigned long parent_rate)
1c8e60044   Tomeu Vizoso   clk: Add rate con...
988
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
989
990
991
  	if (core->ops->recalc_rate)
  		return core->ops->recalc_rate(core->hw, parent_rate);
  	return parent_rate;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
992
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
993
994
995
996
997
998
999
1000
1001
1002
1003
  /**
   * __clk_recalc_rates
   * @core: first clk in the subtree
   * @msg: notification type (see include/linux/clk.h)
   *
   * Walks the subtree of clks starting with clk and recalculates rates as it
   * goes.  Note that if a clk does not implement the .recalc_rate callback then
   * it is assumed that the clock will take on the rate of its parent.
   *
   * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
   * if necessary.
15a02c1f6   Stephen Boyd   clk: Add __clk_mu...
1004
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1005
  static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
15a02c1f6   Stephen Boyd   clk: Add __clk_mu...
1006
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1007
1008
1009
  	unsigned long old_rate;
  	unsigned long parent_rate = 0;
  	struct clk_core *child;
e366fdd72   James Hogan   clk: clk-mux: imp...
1010

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1011
  	lockdep_assert_held(&prepare_lock);
15a02c1f6   Stephen Boyd   clk: Add __clk_mu...
1012

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1013
  	old_rate = core->rate;
b2476490e   Mike Turquette   clk: introduce th...
1014

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1015
1016
  	if (core->parent)
  		parent_rate = core->parent->rate;
b2476490e   Mike Turquette   clk: introduce th...
1017

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1018
  	core->rate = clk_recalc(core, parent_rate);
b2476490e   Mike Turquette   clk: introduce th...
1019

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1020
1021
1022
1023
1024
1025
  	/*
  	 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  	 * & ABORT_RATE_CHANGE notifiers
  	 */
  	if (core->notifier_count && msg)
  		__clk_notify(core, msg, old_rate, core->rate);
b2476490e   Mike Turquette   clk: introduce th...
1026

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1027
1028
1029
  	hlist_for_each_entry(child, &core->children, child_node)
  		__clk_recalc_rates(child, msg);
  }
b2476490e   Mike Turquette   clk: introduce th...
1030

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1031
1032
1033
  static unsigned long clk_core_get_rate(struct clk_core *core)
  {
  	unsigned long rate;
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1034

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1035
  	clk_prepare_lock();
b2476490e   Mike Turquette   clk: introduce th...
1036

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1037
1038
1039
1040
1041
1042
1043
  	if (core && (core->flags & CLK_GET_RATE_NOCACHE))
  		__clk_recalc_rates(core, 0);
  
  	rate = clk_core_get_rate_nolock(core);
  	clk_prepare_unlock();
  
  	return rate;
b2476490e   Mike Turquette   clk: introduce th...
1044
1045
1046
  }
  
  /**
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1047
1048
   * clk_get_rate - return the rate of clk
   * @clk: the clk whose rate is being returned
b2476490e   Mike Turquette   clk: introduce th...
1049
   *
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1050
1051
1052
   * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
   * is set, which means a recalc_rate will be issued.
   * If clk is NULL then returns 0.
b2476490e   Mike Turquette   clk: introduce th...
1053
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1054
  unsigned long clk_get_rate(struct clk *clk)
b2476490e   Mike Turquette   clk: introduce th...
1055
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1056
1057
  	if (!clk)
  		return 0;
63589e92c   Stephen Boyd   clk: Ignore error...
1058

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1059
  	return clk_core_get_rate(clk->core);
b2476490e   Mike Turquette   clk: introduce th...
1060
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1061
  EXPORT_SYMBOL_GPL(clk_get_rate);
b2476490e   Mike Turquette   clk: introduce th...
1062

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1063
1064
  static int clk_fetch_parent_index(struct clk_core *core,
  				  struct clk_core *parent)
b2476490e   Mike Turquette   clk: introduce th...
1065
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1066
  	int i;
b2476490e   Mike Turquette   clk: introduce th...
1067

508f884a6   Masahiro Yamada   clk: make sure pa...
1068
1069
  	if (!parent)
  		return -EINVAL;
470b5e2f9   Masahiro Yamada   clk: simplify clk...
1070
1071
  	for (i = 0; i < core->num_parents; i++)
  		if (clk_core_get_parent_by_index(core, i) == parent)
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1072
  			return i;
b2476490e   Mike Turquette   clk: introduce th...
1073

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1074
  	return -EINVAL;
b2476490e   Mike Turquette   clk: introduce th...
1075
  }
e6500344e   Heiko Stuebner   clk: track the or...
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
  /*
   * Update the orphan status of @core and all its children.
   */
  static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
  {
  	struct clk_core *child;
  
  	core->orphan = is_orphan;
  
  	hlist_for_each_entry(child, &core->children, child_node)
  		clk_core_update_orphan_status(child, is_orphan);
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1088
  static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
b2476490e   Mike Turquette   clk: introduce th...
1089
  {
e6500344e   Heiko Stuebner   clk: track the or...
1090
  	bool was_orphan = core->orphan;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1091
  	hlist_del(&core->child_node);
035a61c31   Tomeu Vizoso   clk: Make clk API...
1092

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1093
  	if (new_parent) {
e6500344e   Heiko Stuebner   clk: track the or...
1094
  		bool becomes_orphan = new_parent->orphan;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1095
1096
1097
  		/* avoid duplicate POST_RATE_CHANGE notifications */
  		if (new_parent->new_child == core)
  			new_parent->new_child = NULL;
b2476490e   Mike Turquette   clk: introduce th...
1098

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1099
  		hlist_add_head(&core->child_node, &new_parent->children);
e6500344e   Heiko Stuebner   clk: track the or...
1100
1101
1102
  
  		if (was_orphan != becomes_orphan)
  			clk_core_update_orphan_status(core, becomes_orphan);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1103
1104
  	} else {
  		hlist_add_head(&core->child_node, &clk_orphan_list);
e6500344e   Heiko Stuebner   clk: track the or...
1105
1106
  		if (!was_orphan)
  			clk_core_update_orphan_status(core, true);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1107
  	}
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1108

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1109
  	core->parent = new_parent;
035a61c31   Tomeu Vizoso   clk: Make clk API...
1110
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1111
1112
  static struct clk_core *__clk_set_parent_before(struct clk_core *core,
  					   struct clk_core *parent)
b2476490e   Mike Turquette   clk: introduce th...
1113
1114
  {
  	unsigned long flags;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1115
  	struct clk_core *old_parent = core->parent;
b2476490e   Mike Turquette   clk: introduce th...
1116

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1117
  	/*
fc8726a2c   Dong Aisheng   clk: core: suppor...
1118
1119
1120
  	 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
  	 *
  	 * 2. Migrate prepare state between parents and prevent race with
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
  	 * clk_enable().
  	 *
  	 * If the clock is not prepared, then a race with
  	 * clk_enable/disable() is impossible since we already have the
  	 * prepare lock (future calls to clk_enable() need to be preceded by
  	 * a clk_prepare()).
  	 *
  	 * If the clock is prepared, migrate the prepared state to the new
  	 * parent and also protect against a race with clk_enable() by
  	 * forcing the clock and the new parent on.  This ensures that all
  	 * future calls to clk_enable() are practically NOPs with respect to
  	 * hardware and software states.
  	 *
  	 * See also: Comment for clk_set_parent() below.
  	 */
fc8726a2c   Dong Aisheng   clk: core: suppor...
1136
1137
1138
1139
1140
1141
1142
1143
  
  	/* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
  	if (core->flags & CLK_OPS_PARENT_ENABLE) {
  		clk_core_prepare_enable(old_parent);
  		clk_core_prepare_enable(parent);
  	}
  
  	/* migrate prepare count if > 0 */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1144
  	if (core->prepare_count) {
fc8726a2c   Dong Aisheng   clk: core: suppor...
1145
1146
  		clk_core_prepare_enable(parent);
  		clk_core_enable_lock(core);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1147
  	}
63589e92c   Stephen Boyd   clk: Ignore error...
1148

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1149
  	/* update the clk tree topology */
eab89f690   Mike Turquette   clk: abstract loc...
1150
  	flags = clk_enable_lock();
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1151
  	clk_reparent(core, parent);
eab89f690   Mike Turquette   clk: abstract loc...
1152
  	clk_enable_unlock(flags);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1153
1154
  
  	return old_parent;
b2476490e   Mike Turquette   clk: introduce th...
1155
  }
b2476490e   Mike Turquette   clk: introduce th...
1156

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1157
1158
1159
  static void __clk_set_parent_after(struct clk_core *core,
  				   struct clk_core *parent,
  				   struct clk_core *old_parent)
b2476490e   Mike Turquette   clk: introduce th...
1160
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1161
1162
1163
1164
1165
  	/*
  	 * Finish the migration of prepare state and undo the changes done
  	 * for preventing a race with clk_enable().
  	 */
  	if (core->prepare_count) {
fc8726a2c   Dong Aisheng   clk: core: suppor...
1166
1167
1168
1169
1170
1171
1172
1173
  		clk_core_disable_lock(core);
  		clk_core_disable_unprepare(old_parent);
  	}
  
  	/* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
  	if (core->flags & CLK_OPS_PARENT_ENABLE) {
  		clk_core_disable_unprepare(parent);
  		clk_core_disable_unprepare(old_parent);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1174
1175
  	}
  }
b2476490e   Mike Turquette   clk: introduce th...
1176

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1177
1178
1179
1180
1181
1182
  static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
  			    u8 p_index)
  {
  	unsigned long flags;
  	int ret = 0;
  	struct clk_core *old_parent;
b2476490e   Mike Turquette   clk: introduce th...
1183

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1184
  	old_parent = __clk_set_parent_before(core, parent);
b2476490e   Mike Turquette   clk: introduce th...
1185

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1186
  	trace_clk_set_parent(core, parent);
b2476490e   Mike Turquette   clk: introduce th...
1187

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1188
1189
1190
  	/* change clock input source */
  	if (parent && core->ops->set_parent)
  		ret = core->ops->set_parent(core->hw, p_index);
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1191

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1192
  	trace_clk_set_parent_complete(core, parent);
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1193

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1194
1195
1196
1197
  	if (ret) {
  		flags = clk_enable_lock();
  		clk_reparent(core, old_parent);
  		clk_enable_unlock(flags);
c660b2ebb   Dong Aisheng   clk: remove dupli...
1198
  		__clk_set_parent_after(core, old_parent, parent);
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1199

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1200
  		return ret;
b2476490e   Mike Turquette   clk: introduce th...
1201
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1202
  	__clk_set_parent_after(core, parent, old_parent);
b2476490e   Mike Turquette   clk: introduce th...
1203
1204
1205
1206
  	return 0;
  }
  
  /**
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1207
1208
1209
   * __clk_speculate_rates
   * @core: first clk in the subtree
   * @parent_rate: the "future" rate of clk's parent
b2476490e   Mike Turquette   clk: introduce th...
1210
   *
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1211
1212
1213
1214
1215
1216
1217
1218
   * Walks the subtree of clks starting with clk, speculating rates as it
   * goes and firing off PRE_RATE_CHANGE notifications as necessary.
   *
   * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
   * pre-rate change notifications and returns early if no clks in the
   * subtree have subscribed to the notifications.  Note that if a clk does not
   * implement the .recalc_rate callback then it is assumed that the clock will
   * take on the rate of its parent.
b2476490e   Mike Turquette   clk: introduce th...
1219
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1220
1221
  static int __clk_speculate_rates(struct clk_core *core,
  				 unsigned long parent_rate)
b2476490e   Mike Turquette   clk: introduce th...
1222
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1223
1224
1225
  	struct clk_core *child;
  	unsigned long new_rate;
  	int ret = NOTIFY_DONE;
b2476490e   Mike Turquette   clk: introduce th...
1226

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1227
  	lockdep_assert_held(&prepare_lock);
864e160ae   Dong Aisheng   clk: Squash __clk...
1228

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
  	new_rate = clk_recalc(core, parent_rate);
  
  	/* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  	if (core->notifier_count)
  		ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
  
  	if (ret & NOTIFY_STOP_MASK) {
  		pr_debug("%s: clk notifier callback for clock %s aborted with error %d
  ",
  				__func__, core->name, ret);
  		goto out;
  	}
  
  	hlist_for_each_entry(child, &core->children, child_node) {
  		ret = __clk_speculate_rates(child, new_rate);
  		if (ret & NOTIFY_STOP_MASK)
  			break;
  	}
b2476490e   Mike Turquette   clk: introduce th...
1247

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1248
  out:
b2476490e   Mike Turquette   clk: introduce th...
1249
1250
  	return ret;
  }
b2476490e   Mike Turquette   clk: introduce th...
1251

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1252
1253
  static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
  			     struct clk_core *new_parent, u8 p_index)
b2476490e   Mike Turquette   clk: introduce th...
1254
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1255
  	struct clk_core *child;
b2476490e   Mike Turquette   clk: introduce th...
1256

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1257
1258
1259
1260
1261
1262
1263
  	core->new_rate = new_rate;
  	core->new_parent = new_parent;
  	core->new_parent_index = p_index;
  	/* include clk in new parent's PRE_RATE_CHANGE notifications */
  	core->new_child = NULL;
  	if (new_parent && new_parent != core->parent)
  		new_parent->new_child = core;
496eadf82   Krzysztof Kozlowski   clk: Use lockdep ...
1264

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1265
1266
1267
1268
1269
  	hlist_for_each_entry(child, &core->children, child_node) {
  		child->new_rate = clk_recalc(child, new_rate);
  		clk_calc_subtree(child, child->new_rate, NULL, 0);
  	}
  }
b2476490e   Mike Turquette   clk: introduce th...
1270

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1271
1272
1273
1274
1275
1276
1277
1278
1279
  /*
   * calculate the new rates returning the topmost clock that has to be
   * changed.
   */
  static struct clk_core *clk_calc_new_rates(struct clk_core *core,
  					   unsigned long rate)
  {
  	struct clk_core *top = core;
  	struct clk_core *old_parent, *parent;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
  	unsigned long best_parent_rate = 0;
  	unsigned long new_rate;
  	unsigned long min_rate;
  	unsigned long max_rate;
  	int p_index = 0;
  	long ret;
  
  	/* sanity */
  	if (IS_ERR_OR_NULL(core))
  		return NULL;
  
  	/* save parent rate, if it exists */
  	parent = old_parent = core->parent;
71472c0c0   James Hogan   clk: add support ...
1293
  	if (parent)
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1294
  		best_parent_rate = parent->rate;
71472c0c0   James Hogan   clk: add support ...
1295

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1296
1297
1298
  	clk_core_get_boundaries(core, &min_rate, &max_rate);
  
  	/* find the closest rate and parent clk/rate */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
1299
  	if (core->ops->determine_rate) {
0817b62cc   Boris Brezillon   clk: change clk_o...
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
  		struct clk_rate_request req;
  
  		req.rate = rate;
  		req.min_rate = min_rate;
  		req.max_rate = max_rate;
  		if (parent) {
  			req.best_parent_hw = parent->hw;
  			req.best_parent_rate = parent->rate;
  		} else {
  			req.best_parent_hw = NULL;
  			req.best_parent_rate = 0;
  		}
  
  		ret = core->ops->determine_rate(core->hw, &req);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1314
1315
  		if (ret < 0)
  			return NULL;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1316

0817b62cc   Boris Brezillon   clk: change clk_o...
1317
1318
1319
  		best_parent_rate = req.best_parent_rate;
  		new_rate = req.rate;
  		parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1320
1321
  	} else if (core->ops->round_rate) {
  		ret = core->ops->round_rate(core->hw, rate,
0817b62cc   Boris Brezillon   clk: change clk_o...
1322
  					    &best_parent_rate);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1323
1324
  		if (ret < 0)
  			return NULL;
035a61c31   Tomeu Vizoso   clk: Make clk API...
1325

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
  		new_rate = ret;
  		if (new_rate < min_rate || new_rate > max_rate)
  			return NULL;
  	} else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
  		/* pass-through clock without adjustable parent */
  		core->new_rate = core->rate;
  		return NULL;
  	} else {
  		/* pass-through clock with adjustable parent */
  		top = clk_calc_new_rates(parent, rate);
  		new_rate = parent->new_rate;
  		goto out;
  	}
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1339

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1340
1341
1342
1343
1344
1345
1346
1347
  	/* some clocks must be gated to change parent */
  	if (parent != old_parent &&
  	    (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  		pr_debug("%s: %s not gated but wants to reparent
  ",
  			 __func__, core->name);
  		return NULL;
  	}
b2476490e   Mike Turquette   clk: introduce th...
1348

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
  	/* try finding the new parent index */
  	if (parent && core->num_parents > 1) {
  		p_index = clk_fetch_parent_index(core, parent);
  		if (p_index < 0) {
  			pr_debug("%s: clk %s can not be parent of clk %s
  ",
  				 __func__, parent->name, core->name);
  			return NULL;
  		}
  	}
b2476490e   Mike Turquette   clk: introduce th...
1359

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1360
1361
1362
  	if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
  	    best_parent_rate != parent->rate)
  		top = clk_calc_new_rates(parent, best_parent_rate);
035a61c31   Tomeu Vizoso   clk: Make clk API...
1363

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1364
1365
  out:
  	clk_calc_subtree(core, new_rate, parent, p_index);
b2476490e   Mike Turquette   clk: introduce th...
1366

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1367
  	return top;
b2476490e   Mike Turquette   clk: introduce th...
1368
  }
b2476490e   Mike Turquette   clk: introduce th...
1369

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1370
1371
1372
1373
  /*
   * Notify about rate changes in a subtree. Always walk down the whole tree
   * so that in case of an error we can walk down the whole tree again and
   * abort the change.
b2476490e   Mike Turquette   clk: introduce th...
1374
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1375
1376
  static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
  						  unsigned long event)
b2476490e   Mike Turquette   clk: introduce th...
1377
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1378
  	struct clk_core *child, *tmp_clk, *fail_clk = NULL;
b2476490e   Mike Turquette   clk: introduce th...
1379
  	int ret = NOTIFY_DONE;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1380
1381
  	if (core->rate == core->new_rate)
  		return NULL;
b2476490e   Mike Turquette   clk: introduce th...
1382

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1383
1384
1385
1386
  	if (core->notifier_count) {
  		ret = __clk_notify(core, event, core->rate, core->new_rate);
  		if (ret & NOTIFY_STOP_MASK)
  			fail_clk = core;
b2476490e   Mike Turquette   clk: introduce th...
1387
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1388
1389
1390
1391
1392
1393
1394
1395
  	hlist_for_each_entry(child, &core->children, child_node) {
  		/* Skip children who will be reparented to another clock */
  		if (child->new_parent && child->new_parent != core)
  			continue;
  		tmp_clk = clk_propagate_rate_change(child, event);
  		if (tmp_clk)
  			fail_clk = tmp_clk;
  	}
5279fc402   Boris BREZILLON   clk: add clk accu...
1396

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1397
1398
1399
1400
1401
1402
  	/* handle the new child who might not be in core->children yet */
  	if (core->new_child) {
  		tmp_clk = clk_propagate_rate_change(core->new_child, event);
  		if (tmp_clk)
  			fail_clk = tmp_clk;
  	}
5279fc402   Boris BREZILLON   clk: add clk accu...
1403

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1404
  	return fail_clk;
5279fc402   Boris BREZILLON   clk: add clk accu...
1405
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1406
1407
1408
1409
1410
  /*
   * walk down a subtree and set the new rates notifying the rate
   * change on the way
   */
  static void clk_change_rate(struct clk_core *core)
035a61c31   Tomeu Vizoso   clk: Make clk API...
1411
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1412
1413
1414
1415
1416
1417
  	struct clk_core *child;
  	struct hlist_node *tmp;
  	unsigned long old_rate;
  	unsigned long best_parent_rate = 0;
  	bool skip_set_rate = false;
  	struct clk_core *old_parent;
fc8726a2c   Dong Aisheng   clk: core: suppor...
1418
  	struct clk_core *parent = NULL;
035a61c31   Tomeu Vizoso   clk: Make clk API...
1419

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1420
  	old_rate = core->rate;
035a61c31   Tomeu Vizoso   clk: Make clk API...
1421

fc8726a2c   Dong Aisheng   clk: core: suppor...
1422
1423
  	if (core->new_parent) {
  		parent = core->new_parent;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1424
  		best_parent_rate = core->new_parent->rate;
fc8726a2c   Dong Aisheng   clk: core: suppor...
1425
1426
  	} else if (core->parent) {
  		parent = core->parent;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1427
  		best_parent_rate = core->parent->rate;
fc8726a2c   Dong Aisheng   clk: core: suppor...
1428
  	}
035a61c31   Tomeu Vizoso   clk: Make clk API...
1429

2eb8c7104   Heiko Stuebner   clk: add flag for...
1430
1431
1432
1433
1434
1435
1436
1437
  	if (core->flags & CLK_SET_RATE_UNGATE) {
  		unsigned long flags;
  
  		clk_core_prepare(core);
  		flags = clk_enable_lock();
  		clk_core_enable(core);
  		clk_enable_unlock(flags);
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1438
1439
1440
  	if (core->new_parent && core->new_parent != core->parent) {
  		old_parent = __clk_set_parent_before(core, core->new_parent);
  		trace_clk_set_parent(core, core->new_parent);
5279fc402   Boris BREZILLON   clk: add clk accu...
1441

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1442
1443
1444
1445
1446
1447
1448
1449
  		if (core->ops->set_rate_and_parent) {
  			skip_set_rate = true;
  			core->ops->set_rate_and_parent(core->hw, core->new_rate,
  					best_parent_rate,
  					core->new_parent_index);
  		} else if (core->ops->set_parent) {
  			core->ops->set_parent(core->hw, core->new_parent_index);
  		}
5279fc402   Boris BREZILLON   clk: add clk accu...
1450

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1451
1452
1453
  		trace_clk_set_parent_complete(core, core->new_parent);
  		__clk_set_parent_after(core, core->new_parent, old_parent);
  	}
8f2c2db13   Stephen Boyd   clk: Consolidate ...
1454

fc8726a2c   Dong Aisheng   clk: core: suppor...
1455
1456
  	if (core->flags & CLK_OPS_PARENT_ENABLE)
  		clk_core_prepare_enable(parent);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1457
  	trace_clk_set_rate(core, core->new_rate);
b2476490e   Mike Turquette   clk: introduce th...
1458

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1459
1460
  	if (!skip_set_rate && core->ops->set_rate)
  		core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
496eadf82   Krzysztof Kozlowski   clk: Use lockdep ...
1461

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1462
  	trace_clk_set_rate_complete(core, core->new_rate);
b2476490e   Mike Turquette   clk: introduce th...
1463

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1464
  	core->rate = clk_recalc(core, best_parent_rate);
b2476490e   Mike Turquette   clk: introduce th...
1465

2eb8c7104   Heiko Stuebner   clk: add flag for...
1466
1467
1468
1469
1470
1471
1472
1473
  	if (core->flags & CLK_SET_RATE_UNGATE) {
  		unsigned long flags;
  
  		flags = clk_enable_lock();
  		clk_core_disable(core);
  		clk_enable_unlock(flags);
  		clk_core_unprepare(core);
  	}
fc8726a2c   Dong Aisheng   clk: core: suppor...
1474
1475
  	if (core->flags & CLK_OPS_PARENT_ENABLE)
  		clk_core_disable_unprepare(parent);
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1476
1477
  	if (core->notifier_count && old_rate != core->rate)
  		__clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
b2476490e   Mike Turquette   clk: introduce th...
1478

85e88fab1   Michael Turquette   Merge branch 'clk...
1479
1480
  	if (core->flags & CLK_RECALC_NEW_RATES)
  		(void)clk_calc_new_rates(core, core->new_rate);
d8d919879   Bartlomiej Zolnierkiewicz   clk: add CLK_RECA...
1481

b2476490e   Mike Turquette   clk: introduce th...
1482
  	/*
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1483
1484
  	 * Use safe iteration, as change_rate can actually swap parents
  	 * for certain clock types.
b2476490e   Mike Turquette   clk: introduce th...
1485
  	 */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1486
1487
1488
1489
1490
1491
  	hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
  		/* Skip children who will be reparented to another clock */
  		if (child->new_parent && child->new_parent != core)
  			continue;
  		clk_change_rate(child);
  	}
b2476490e   Mike Turquette   clk: introduce th...
1492

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1493
1494
1495
  	/* handle the new child who might not be in core->children yet */
  	if (core->new_child)
  		clk_change_rate(core->new_child);
b2476490e   Mike Turquette   clk: introduce th...
1496
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1497
1498
  static int clk_core_set_rate_nolock(struct clk_core *core,
  				    unsigned long req_rate)
a093bde2b   Ulf Hansson   clk: Provide opti...
1499
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1500
1501
  	struct clk_core *top, *fail_clk;
  	unsigned long rate = req_rate;
a093bde2b   Ulf Hansson   clk: Provide opti...
1502

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1503
1504
  	if (!core)
  		return 0;
a093bde2b   Ulf Hansson   clk: Provide opti...
1505

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1506
1507
1508
  	/* bail early if nothing to do */
  	if (rate == clk_core_get_rate_nolock(core))
  		return 0;
a093bde2b   Ulf Hansson   clk: Provide opti...
1509

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1510
1511
  	if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
  		return -EBUSY;
a093bde2b   Ulf Hansson   clk: Provide opti...
1512

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
  	/* calculate new rates and get the topmost changed clock */
  	top = clk_calc_new_rates(core, rate);
  	if (!top)
  		return -EINVAL;
  
  	/* notify that we are about to change rates */
  	fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  	if (fail_clk) {
  		pr_debug("%s: failed to set %s rate
  ", __func__,
  				fail_clk->name);
  		clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  		return -EBUSY;
  	}
  
  	/* change the rates */
  	clk_change_rate(top);
  
  	core->req_rate = req_rate;
06b37e4a6   Lee Jones   clk: Remove unuse...
1532
  	return 0;
a093bde2b   Ulf Hansson   clk: Provide opti...
1533
  }
035a61c31   Tomeu Vizoso   clk: Make clk API...
1534
1535
  
  /**
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1536
1537
1538
   * clk_set_rate - specify a new rate for clk
   * @clk: the clk whose rate is being changed
   * @rate: the new rate for clk
035a61c31   Tomeu Vizoso   clk: Make clk API...
1539
   *
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
   * In the simplest case clk_set_rate will only adjust the rate of clk.
   *
   * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
   * propagate up to clk's parent; whether or not this happens depends on the
   * outcome of clk's .round_rate implementation.  If *parent_rate is unchanged
   * after calling .round_rate then upstream parent propagation is ignored.  If
   * *parent_rate comes back with a new rate for clk's parent then we propagate
   * up to clk's parent and set its rate.  Upward propagation will continue
   * until either a clk does not support the CLK_SET_RATE_PARENT flag or
   * .round_rate stops requesting changes to clk's parent_rate.
   *
   * Rate changes are accomplished via tree traversal that also recalculates the
   * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
   *
   * Returns 0 on success, -EERROR otherwise.
035a61c31   Tomeu Vizoso   clk: Make clk API...
1555
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1556
  int clk_set_rate(struct clk *clk, unsigned long rate)
035a61c31   Tomeu Vizoso   clk: Make clk API...
1557
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1558
  	int ret;
035a61c31   Tomeu Vizoso   clk: Make clk API...
1559
1560
  	if (!clk)
  		return 0;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1561
1562
  	/* prevent racing with updates to the clock topology */
  	clk_prepare_lock();
da0f0b2c3   Tomasz Figa   clk: Correct look...
1563

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1564
  	ret = clk_core_set_rate_nolock(clk->core, rate);
da0f0b2c3   Tomasz Figa   clk: Correct look...
1565

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1566
  	clk_prepare_unlock();
4935b22c4   James Hogan   clk: move some pa...
1567

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1568
  	return ret;
4935b22c4   James Hogan   clk: move some pa...
1569
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1570
  EXPORT_SYMBOL_GPL(clk_set_rate);
4935b22c4   James Hogan   clk: move some pa...
1571

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1572
1573
1574
1575
1576
1577
1578
1579
1580
  /**
   * clk_set_rate_range - set a rate range for a clock source
   * @clk: clock source
   * @min: desired minimum clock rate in Hz, inclusive
   * @max: desired maximum clock rate in Hz, inclusive
   *
   * Returns success (0) or negative errno.
   */
  int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
4935b22c4   James Hogan   clk: move some pa...
1581
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1582
  	int ret = 0;
4935b22c4   James Hogan   clk: move some pa...
1583

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1584
1585
  	if (!clk)
  		return 0;
903efc553   James Hogan   clk: fix new_pare...
1586

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1587
1588
1589
1590
1591
1592
  	if (min > max) {
  		pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]
  ",
  		       __func__, clk->core->name, clk->dev_id, clk->con_id,
  		       min, max);
  		return -EINVAL;
903efc553   James Hogan   clk: fix new_pare...
1593
  	}
4935b22c4   James Hogan   clk: move some pa...
1594

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1595
  	clk_prepare_lock();
4935b22c4   James Hogan   clk: move some pa...
1596

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1597
1598
1599
1600
  	if (min != clk->min_rate || max != clk->max_rate) {
  		clk->min_rate = min;
  		clk->max_rate = max;
  		ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
4935b22c4   James Hogan   clk: move some pa...
1601
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1602
  	clk_prepare_unlock();
4935b22c4   James Hogan   clk: move some pa...
1603

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1604
  	return ret;
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1605
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1606
  EXPORT_SYMBOL_GPL(clk_set_rate_range);
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1607

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1608
1609
1610
1611
1612
1613
1614
1615
  /**
   * clk_set_min_rate - set a minimum clock rate for a clock source
   * @clk: clock source
   * @rate: desired minimum clock rate in Hz, inclusive
   *
   * Returns success (0) or negative errno.
   */
  int clk_set_min_rate(struct clk *clk, unsigned long rate)
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1616
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1617
1618
1619
1620
  	if (!clk)
  		return 0;
  
  	return clk_set_rate_range(clk, rate, clk->max_rate);
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1621
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1622
  EXPORT_SYMBOL_GPL(clk_set_min_rate);
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1623

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1624
1625
1626
1627
1628
1629
1630
1631
  /**
   * clk_set_max_rate - set a maximum clock rate for a clock source
   * @clk: clock source
   * @rate: desired maximum clock rate in Hz, inclusive
   *
   * Returns success (0) or negative errno.
   */
  int clk_set_max_rate(struct clk *clk, unsigned long rate)
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1632
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1633
1634
  	if (!clk)
  		return 0;
4935b22c4   James Hogan   clk: move some pa...
1635

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1636
  	return clk_set_rate_range(clk, clk->min_rate, rate);
4935b22c4   James Hogan   clk: move some pa...
1637
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1638
  EXPORT_SYMBOL_GPL(clk_set_max_rate);
4935b22c4   James Hogan   clk: move some pa...
1639

a093bde2b   Ulf Hansson   clk: Provide opti...
1640
  /**
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1641
1642
   * clk_get_parent - return the parent of a clk
   * @clk: the clk whose parent gets returned
b2476490e   Mike Turquette   clk: introduce th...
1643
   *
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1644
   * Simply returns clk->parent.  Returns NULL if clk is NULL.
b2476490e   Mike Turquette   clk: introduce th...
1645
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1646
  struct clk *clk_get_parent(struct clk *clk)
b2476490e   Mike Turquette   clk: introduce th...
1647
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1648
  	struct clk *parent;
b2476490e   Mike Turquette   clk: introduce th...
1649

fc4a05d4b   Stephen Boyd   clk: Remove unuse...
1650
1651
  	if (!clk)
  		return NULL;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1652
  	clk_prepare_lock();
fc4a05d4b   Stephen Boyd   clk: Remove unuse...
1653
1654
  	/* TODO: Create a per-user clk and change callers to call clk_put */
  	parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1655
  	clk_prepare_unlock();
496eadf82   Krzysztof Kozlowski   clk: Use lockdep ...
1656

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1657
1658
1659
  	return parent;
  }
  EXPORT_SYMBOL_GPL(clk_get_parent);
b2476490e   Mike Turquette   clk: introduce th...
1660

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1661
1662
  static struct clk_core *__clk_init_parent(struct clk_core *core)
  {
5146e0b05   Masahiro Yamada   clk: simplify __c...
1663
  	u8 index = 0;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1664

2430a94d1   Masahiro Yamada   clk: fix __clk_in...
1665
  	if (core->num_parents > 1 && core->ops->get_parent)
5146e0b05   Masahiro Yamada   clk: simplify __c...
1666
  		index = core->ops->get_parent(core->hw);
b2476490e   Mike Turquette   clk: introduce th...
1667

5146e0b05   Masahiro Yamada   clk: simplify __c...
1668
  	return clk_core_get_parent_by_index(core, index);
b2476490e   Mike Turquette   clk: introduce th...
1669
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1670
1671
  static void clk_core_reparent(struct clk_core *core,
  				  struct clk_core *new_parent)
b2476490e   Mike Turquette   clk: introduce th...
1672
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1673
1674
1675
  	clk_reparent(core, new_parent);
  	__clk_recalc_accuracies(core);
  	__clk_recalc_rates(core, POST_RATE_CHANGE);
b2476490e   Mike Turquette   clk: introduce th...
1676
  }
42c86547f   Tomeu Vizoso   clk: Expose clk_h...
1677
1678
1679
1680
1681
1682
1683
  void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
  {
  	if (!hw)
  		return;
  
  	clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1684
1685
1686
1687
1688
1689
1690
1691
1692
  /**
   * clk_has_parent - check if a clock is a possible parent for another
   * @clk: clock source
   * @parent: parent clock source
   *
   * This function can be used in drivers that need to check that a clock can be
   * the parent of another without actually changing the parent.
   *
   * Returns true if @parent is a possible parent for @clk, false otherwise.
b2476490e   Mike Turquette   clk: introduce th...
1693
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1694
  bool clk_has_parent(struct clk *clk, struct clk *parent)
b2476490e   Mike Turquette   clk: introduce th...
1695
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1696
1697
  	struct clk_core *core, *parent_core;
  	unsigned int i;
b2476490e   Mike Turquette   clk: introduce th...
1698

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1699
1700
1701
  	/* NULL clocks should be nops, so return success if either is NULL. */
  	if (!clk || !parent)
  		return true;
7452b2191   Mike Turquette   clk: core: clk_ca...
1702

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1703
1704
  	core = clk->core;
  	parent_core = parent->core;
71472c0c0   James Hogan   clk: add support ...
1705

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1706
1707
1708
  	/* Optimize for the case where the parent is already the parent. */
  	if (core->parent == parent_core)
  		return true;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1709

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1710
1711
1712
  	for (i = 0; i < core->num_parents; i++)
  		if (strcmp(core->parent_names[i], parent_core->name) == 0)
  			return true;
03bc10ab5   Boris Brezillon   clk: check ->dete...
1713

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1714
1715
1716
  	return false;
  }
  EXPORT_SYMBOL_GPL(clk_has_parent);
03bc10ab5   Boris Brezillon   clk: check ->dete...
1717

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
  static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
  {
  	int ret = 0;
  	int p_index = 0;
  	unsigned long p_rate = 0;
  
  	if (!core)
  		return 0;
  
  	/* prevent racing with updates to the clock topology */
  	clk_prepare_lock();
  
  	if (core->parent == parent)
  		goto out;
  
  	/* verify ops for for multi-parent clks */
  	if ((core->num_parents > 1) && (!core->ops->set_parent)) {
  		ret = -ENOSYS;
63f5c3b2b   Mike Turquette   clk: prevent spur...
1736
  		goto out;
7452b2191   Mike Turquette   clk: core: clk_ca...
1737
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1738
1739
1740
1741
  	/* check that we are allowed to re-parent if the clock is in use */
  	if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  		ret = -EBUSY;
  		goto out;
b2476490e   Mike Turquette   clk: introduce th...
1742
  	}
71472c0c0   James Hogan   clk: add support ...
1743
  	/* try finding the new parent index */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1744
  	if (parent) {
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
1745
  		p_index = clk_fetch_parent_index(core, parent);
f1c8b2edf   Tomasz Figa   clk: Add error ha...
1746
  		if (p_index < 0) {
71472c0c0   James Hogan   clk: add support ...
1747
1748
  			pr_debug("%s: clk %s can not be parent of clk %s
  ",
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1749
1750
1751
  					__func__, parent->name, core->name);
  			ret = p_index;
  			goto out;
71472c0c0   James Hogan   clk: add support ...
1752
  		}
e8f0e68ec   Masahiro Yamada   clk: slightly opt...
1753
  		p_rate = parent->rate;
b2476490e   Mike Turquette   clk: introduce th...
1754
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1755
1756
  	/* propagate PRE_RATE_CHANGE notifications */
  	ret = __clk_speculate_rates(core, p_rate);
b2476490e   Mike Turquette   clk: introduce th...
1757

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1758
1759
1760
  	/* abort if a driver objects */
  	if (ret & NOTIFY_STOP_MASK)
  		goto out;
b2476490e   Mike Turquette   clk: introduce th...
1761

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1762
1763
  	/* do the re-parent */
  	ret = __clk_set_parent(core, parent, p_index);
b2476490e   Mike Turquette   clk: introduce th...
1764

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1765
1766
1767
1768
1769
1770
  	/* propagate rate an accuracy recalculation accordingly */
  	if (ret) {
  		__clk_recalc_rates(core, ABORT_RATE_CHANGE);
  	} else {
  		__clk_recalc_rates(core, POST_RATE_CHANGE);
  		__clk_recalc_accuracies(core);
b2476490e   Mike Turquette   clk: introduce th...
1771
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1772
1773
  out:
  	clk_prepare_unlock();
71472c0c0   James Hogan   clk: add support ...
1774

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1775
1776
  	return ret;
  }
b2476490e   Mike Turquette   clk: introduce th...
1777

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
  /**
   * clk_set_parent - switch the parent of a mux clk
   * @clk: the mux clk whose input we are switching
   * @parent: the new input to clk
   *
   * Re-parent clk to use parent as its new input source.  If clk is in
   * prepared state, the clk will get enabled for the duration of this call. If
   * that's not acceptable for a specific clk (Eg: the consumer can't handle
   * that, the reparenting is glitchy in hardware, etc), use the
   * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
   *
   * After successfully changing clk's parent clk_set_parent will update the
   * clk topology, sysfs topology and propagate rate recalculation via
   * __clk_recalc_rates.
   *
   * Returns 0 on success, -EERROR otherwise.
   */
  int clk_set_parent(struct clk *clk, struct clk *parent)
  {
  	if (!clk)
  		return 0;
  
  	return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
b2476490e   Mike Turquette   clk: introduce th...
1801
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1802
  EXPORT_SYMBOL_GPL(clk_set_parent);
b2476490e   Mike Turquette   clk: introduce th...
1803

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
  /**
   * clk_set_phase - adjust the phase shift of a clock signal
   * @clk: clock signal source
   * @degrees: number of degrees the signal is shifted
   *
   * Shifts the phase of a clock signal by the specified
   * degrees. Returns 0 on success, -EERROR otherwise.
   *
   * This function makes no distinction about the input or reference
   * signal that we adjust the clock signal phase against. For example
   * phase locked-loop clock signal generators we may shift phase with
   * respect to feedback clock signal input, but for other cases the
   * clock phase may be shifted with respect to some other, unspecified
   * signal.
   *
   * Additionally the concept of phase shift does not propagate through
   * the clock tree hierarchy, which sets it apart from clock rates and
   * clock accuracy. A parent clock phase attribute does not have an
   * impact on the phase attribute of a child clock.
b2476490e   Mike Turquette   clk: introduce th...
1823
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1824
  int clk_set_phase(struct clk *clk, int degrees)
b2476490e   Mike Turquette   clk: introduce th...
1825
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1826
  	int ret = -EINVAL;
b2476490e   Mike Turquette   clk: introduce th...
1827

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1828
1829
  	if (!clk)
  		return 0;
b2476490e   Mike Turquette   clk: introduce th...
1830

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1831
1832
1833
1834
  	/* sanity check degrees */
  	degrees %= 360;
  	if (degrees < 0)
  		degrees += 360;
bf47b4fd8   Pawel Moll   clk: Check parent...
1835

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1836
  	clk_prepare_lock();
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1837

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1838
  	trace_clk_set_phase(clk->core, degrees);
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1839

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1840
1841
  	if (clk->core->ops->set_phase)
  		ret = clk->core->ops->set_phase(clk->core->hw, degrees);
3fa2252b7   Stephen Boyd   clk: Add set_rate...
1842

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1843
  	trace_clk_set_phase_complete(clk->core, degrees);
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1844

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1845
1846
  	if (!ret)
  		clk->core->phase = degrees;
b2476490e   Mike Turquette   clk: introduce th...
1847

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1848
  	clk_prepare_unlock();
dfc202ead   Stephen Boyd   clk: Add tracepoi...
1849

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1850
1851
1852
  	return ret;
  }
  EXPORT_SYMBOL_GPL(clk_set_phase);
b2476490e   Mike Turquette   clk: introduce th...
1853

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1854
1855
1856
  static int clk_core_get_phase(struct clk_core *core)
  {
  	int ret;
b2476490e   Mike Turquette   clk: introduce th...
1857

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1858
1859
1860
  	clk_prepare_lock();
  	ret = core->phase;
  	clk_prepare_unlock();
71472c0c0   James Hogan   clk: add support ...
1861

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1862
  	return ret;
b2476490e   Mike Turquette   clk: introduce th...
1863
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1864
1865
1866
1867
1868
1869
1870
1871
  /**
   * clk_get_phase - return the phase shift of a clock signal
   * @clk: clock signal source
   *
   * Returns the phase shift of a clock node in degrees, otherwise returns
   * -EERROR.
   */
  int clk_get_phase(struct clk *clk)
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1872
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1873
  	if (!clk)
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1874
  		return 0;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1875
1876
1877
  	return clk_core_get_phase(clk->core);
  }
  EXPORT_SYMBOL_GPL(clk_get_phase);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1878

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
  /**
   * clk_is_match - check if two clk's point to the same hardware clock
   * @p: clk compared against q
   * @q: clk compared against p
   *
   * Returns true if the two struct clk pointers both point to the same hardware
   * clock node. Put differently, returns true if struct clk *p and struct clk *q
   * share the same struct clk_core object.
   *
   * Returns false otherwise. Note that two NULL clks are treated as matching.
   */
  bool clk_is_match(const struct clk *p, const struct clk *q)
  {
  	/* trivial case: identical struct clk's or both NULL */
  	if (p == q)
  		return true;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1895

3fe003f94   Geert Uytterhoeven   clk: Spelling s/d...
1896
  	/* true if clk->core pointers match. Avoid dereferencing garbage */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1897
1898
1899
  	if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
  		if (p->core == q->core)
  			return true;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1900

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1901
1902
1903
  	return false;
  }
  EXPORT_SYMBOL_GPL(clk_is_match);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1904

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1905
  /***        debugfs support        ***/
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1906

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1907
1908
  #ifdef CONFIG_DEBUG_FS
  #include <linux/debugfs.h>
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1909

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1910
1911
1912
1913
  static struct dentry *rootdir;
  static int inited = 0;
  static DEFINE_MUTEX(clk_debug_lock);
  static HLIST_HEAD(clk_debug_list);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1914

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
  static struct hlist_head *all_lists[] = {
  	&clk_root_list,
  	&clk_orphan_list,
  	NULL,
  };
  
  static struct hlist_head *orphan_list[] = {
  	&clk_orphan_list,
  	NULL,
  };
  
  static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
  				 int level)
b2476490e   Mike Turquette   clk: introduce th...
1928
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1929
1930
  	if (!c)
  		return;
b2476490e   Mike Turquette   clk: introduce th...
1931

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1932
1933
1934
1935
1936
1937
1938
  	seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d
  ",
  		   level * 3 + 1, "",
  		   30 - level * 3, c->name,
  		   c->enable_count, c->prepare_count, clk_core_get_rate(c),
  		   clk_core_get_accuracy(c), clk_core_get_phase(c));
  }
89ac8d7ae   Mike Turquette   clk: handle NULL ...
1939

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1940
1941
1942
1943
  static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
  				     int level)
  {
  	struct clk_core *child;
b2476490e   Mike Turquette   clk: introduce th...
1944

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1945
1946
  	if (!c)
  		return;
b2476490e   Mike Turquette   clk: introduce th...
1947

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1948
  	clk_summary_show_one(s, c, level);
0e1c03017   Viresh Kumar   clk: clk_set_rate...
1949

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1950
1951
  	hlist_for_each_entry(child, &c->children, child_node)
  		clk_summary_show_subtree(s, child, level + 1);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1952
  }
b2476490e   Mike Turquette   clk: introduce th...
1953

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1954
  static int clk_summary_show(struct seq_file *s, void *data)
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1955
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1956
1957
  	struct clk_core *c;
  	struct hlist_head **lists = (struct hlist_head **)s->private;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1958

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1959
1960
1961
1962
  	seq_puts(s, "   clock                         enable_cnt  prepare_cnt        rate   accuracy   phase
  ");
  	seq_puts(s, "----------------------------------------------------------------------------------------
  ");
b2476490e   Mike Turquette   clk: introduce th...
1963

1c8e60044   Tomeu Vizoso   clk: Add rate con...
1964
  	clk_prepare_lock();
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1965
1966
1967
  	for (; *lists; lists++)
  		hlist_for_each_entry(c, *lists, child_node)
  			clk_summary_show_subtree(s, c, 0);
b2476490e   Mike Turquette   clk: introduce th...
1968

eab89f690   Mike Turquette   clk: abstract loc...
1969
  	clk_prepare_unlock();
b2476490e   Mike Turquette   clk: introduce th...
1970

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1971
  	return 0;
b2476490e   Mike Turquette   clk: introduce th...
1972
  }
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1973

1c8e60044   Tomeu Vizoso   clk: Add rate con...
1974

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1975
  static int clk_summary_open(struct inode *inode, struct file *file)
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1976
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1977
  	return single_open(file, clk_summary_show, inode->i_private);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
1978
  }
b2476490e   Mike Turquette   clk: introduce th...
1979

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1980
1981
1982
1983
1984
1985
  static const struct file_operations clk_summary_fops = {
  	.open		= clk_summary_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
b2476490e   Mike Turquette   clk: introduce th...
1986

4dff95dc9   Stephen Boyd   clk: Remove forwa...
1987
1988
1989
1990
  static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
  {
  	if (!c)
  		return;
b2476490e   Mike Turquette   clk: introduce th...
1991

7cb81136d   Stefan Wahren   clk: Fix JSON out...
1992
  	/* This should be JSON format, i.e. elements separated with a comma */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1993
1994
1995
  	seq_printf(s, "\"%s\": { ", c->name);
  	seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  	seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
7cb81136d   Stefan Wahren   clk: Fix JSON out...
1996
1997
  	seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
  	seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
4dff95dc9   Stephen Boyd   clk: Remove forwa...
1998
  	seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
b2476490e   Mike Turquette   clk: introduce th...
1999
  }
b2476490e   Mike Turquette   clk: introduce th...
2000

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2001
  static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
b2476490e   Mike Turquette   clk: introduce th...
2002
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2003
  	struct clk_core *child;
b2476490e   Mike Turquette   clk: introduce th...
2004

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2005
2006
  	if (!c)
  		return;
b2476490e   Mike Turquette   clk: introduce th...
2007

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2008
  	clk_dump_one(s, c, level);
b2476490e   Mike Turquette   clk: introduce th...
2009

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2010
2011
2012
  	hlist_for_each_entry(child, &c->children, child_node) {
  		seq_printf(s, ",");
  		clk_dump_subtree(s, child, level + 1);
b2476490e   Mike Turquette   clk: introduce th...
2013
  	}
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2014
  	seq_printf(s, "}");
b2476490e   Mike Turquette   clk: introduce th...
2015
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2016
  static int clk_dump(struct seq_file *s, void *data)
4e88f3de8   Thierry Reding   clk: Introduce cl...
2017
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2018
2019
2020
  	struct clk_core *c;
  	bool first_node = true;
  	struct hlist_head **lists = (struct hlist_head **)s->private;
4e88f3de8   Thierry Reding   clk: Introduce cl...
2021

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2022
  	seq_printf(s, "{");
4e88f3de8   Thierry Reding   clk: Introduce cl...
2023

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2024
  	clk_prepare_lock();
035a61c31   Tomeu Vizoso   clk: Make clk API...
2025

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2026
2027
2028
2029
2030
2031
2032
2033
  	for (; *lists; lists++) {
  		hlist_for_each_entry(c, *lists, child_node) {
  			if (!first_node)
  				seq_puts(s, ",");
  			first_node = false;
  			clk_dump_subtree(s, c, 0);
  		}
  	}
4e88f3de8   Thierry Reding   clk: Introduce cl...
2034

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2035
  	clk_prepare_unlock();
4e88f3de8   Thierry Reding   clk: Introduce cl...
2036

70e9f4dde   Felipe Balbi   clk: add newline ...
2037
2038
  	seq_puts(s, "}
  ");
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2039
  	return 0;
4e88f3de8   Thierry Reding   clk: Introduce cl...
2040
  }
4e88f3de8   Thierry Reding   clk: Introduce cl...
2041

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2042
2043
  
  static int clk_dump_open(struct inode *inode, struct file *file)
b2476490e   Mike Turquette   clk: introduce th...
2044
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2045
2046
  	return single_open(file, clk_dump, inode->i_private);
  }
b2476490e   Mike Turquette   clk: introduce th...
2047

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2048
2049
2050
2051
2052
2053
  static const struct file_operations clk_dump_fops = {
  	.open		= clk_dump_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
89ac8d7ae   Mike Turquette   clk: handle NULL ...
2054

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2055
2056
2057
2058
  static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
  {
  	struct dentry *d;
  	int ret = -ENOMEM;
b2476490e   Mike Turquette   clk: introduce th...
2059

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2060
2061
  	if (!core || !pdentry) {
  		ret = -EINVAL;
b2476490e   Mike Turquette   clk: introduce th...
2062
  		goto out;
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2063
  	}
b2476490e   Mike Turquette   clk: introduce th...
2064

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2065
2066
  	d = debugfs_create_dir(core->name, pdentry);
  	if (!d)
b61c43c09   Stephen Boyd   clk: clk_set_pare...
2067
  		goto out;
b61c43c09   Stephen Boyd   clk: clk_set_pare...
2068

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
  	core->dentry = d;
  
  	d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
  			(u32 *)&core->rate);
  	if (!d)
  		goto err_out;
  
  	d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
  			(u32 *)&core->accuracy);
  	if (!d)
  		goto err_out;
  
  	d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
  			(u32 *)&core->phase);
  	if (!d)
  		goto err_out;
031dcc9bd   Ulf Hansson   clk: Fixup errorh...
2085

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2086
2087
2088
2089
  	d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
  			(u32 *)&core->flags);
  	if (!d)
  		goto err_out;
031dcc9bd   Ulf Hansson   clk: Fixup errorh...
2090

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2091
2092
2093
2094
  	d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
  			(u32 *)&core->prepare_count);
  	if (!d)
  		goto err_out;
b2476490e   Mike Turquette   clk: introduce th...
2095

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2096
2097
2098
2099
  	d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
  			(u32 *)&core->enable_count);
  	if (!d)
  		goto err_out;
b2476490e   Mike Turquette   clk: introduce th...
2100

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2101
2102
2103
2104
  	d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
  			(u32 *)&core->notifier_count);
  	if (!d)
  		goto err_out;
b2476490e   Mike Turquette   clk: introduce th...
2105

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2106
2107
2108
2109
  	if (core->ops->debug_init) {
  		ret = core->ops->debug_init(core->hw, core->dentry);
  		if (ret)
  			goto err_out;
5279fc402   Boris BREZILLON   clk: add clk accu...
2110
  	}
b2476490e   Mike Turquette   clk: introduce th...
2111

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2112
2113
  	ret = 0;
  	goto out;
b2476490e   Mike Turquette   clk: introduce th...
2114

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2115
2116
2117
2118
  err_out:
  	debugfs_remove_recursive(core->dentry);
  	core->dentry = NULL;
  out:
b2476490e   Mike Turquette   clk: introduce th...
2119
2120
  	return ret;
  }
035a61c31   Tomeu Vizoso   clk: Make clk API...
2121
2122
  
  /**
6e5ab41b1   Stephen Boyd   clk: Update some ...
2123
2124
   * clk_debug_register - add a clk node to the debugfs clk directory
   * @core: the clk being added to the debugfs clk directory
035a61c31   Tomeu Vizoso   clk: Make clk API...
2125
   *
6e5ab41b1   Stephen Boyd   clk: Update some ...
2126
2127
   * Dynamically adds a clk to the debugfs clk directory if debugfs has been
   * initialized.  Otherwise it bails out early since the debugfs clk directory
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2128
   * will be created lazily by clk_debug_init as part of a late_initcall.
035a61c31   Tomeu Vizoso   clk: Make clk API...
2129
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2130
  static int clk_debug_register(struct clk_core *core)
035a61c31   Tomeu Vizoso   clk: Make clk API...
2131
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2132
  	int ret = 0;
035a61c31   Tomeu Vizoso   clk: Make clk API...
2133

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
  	mutex_lock(&clk_debug_lock);
  	hlist_add_head(&core->debug_node, &clk_debug_list);
  
  	if (!inited)
  		goto unlock;
  
  	ret = clk_debug_create_one(core, rootdir);
  unlock:
  	mutex_unlock(&clk_debug_lock);
  
  	return ret;
035a61c31   Tomeu Vizoso   clk: Make clk API...
2145
  }
b2476490e   Mike Turquette   clk: introduce th...
2146

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2147
   /**
6e5ab41b1   Stephen Boyd   clk: Update some ...
2148
2149
   * clk_debug_unregister - remove a clk node from the debugfs clk directory
   * @core: the clk being removed from the debugfs clk directory
e59c5371f   Mike Turquette   clk: introduce cl...
2150
   *
6e5ab41b1   Stephen Boyd   clk: Update some ...
2151
2152
   * Dynamically removes a clk and all its child nodes from the
   * debugfs clk directory if clk->dentry points to debugfs created by
706d5c73e   Stephen Boyd   clk: Update some ...
2153
   * clk_debug_register in __clk_core_init.
e59c5371f   Mike Turquette   clk: introduce cl...
2154
   */
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2155
  static void clk_debug_unregister(struct clk_core *core)
e59c5371f   Mike Turquette   clk: introduce cl...
2156
  {
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2157
2158
2159
2160
2161
2162
  	mutex_lock(&clk_debug_lock);
  	hlist_del_init(&core->debug_node);
  	debugfs_remove_recursive(core->dentry);
  	core->dentry = NULL;
  	mutex_unlock(&clk_debug_lock);
  }
e59c5371f   Mike Turquette   clk: introduce cl...
2163

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2164
2165
2166
2167
  struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
  				void *data, const struct file_operations *fops)
  {
  	struct dentry *d = NULL;
e59c5371f   Mike Turquette   clk: introduce cl...
2168

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2169
2170
2171
  	if (hw->core->dentry)
  		d = debugfs_create_file(name, mode, hw->core->dentry, data,
  					fops);
e59c5371f   Mike Turquette   clk: introduce cl...
2172

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2173
2174
2175
  	return d;
  }
  EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
e59c5371f   Mike Turquette   clk: introduce cl...
2176

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2177
  /**
6e5ab41b1   Stephen Boyd   clk: Update some ...
2178
   * clk_debug_init - lazily populate the debugfs clk directory
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2179
   *
6e5ab41b1   Stephen Boyd   clk: Update some ...
2180
2181
2182
2183
2184
   * clks are often initialized very early during boot before memory can be
   * dynamically allocated and well before debugfs is setup. This function
   * populates the debugfs clk directory once at boot-time when we know that
   * debugfs is setup. It should only be called once at boot-time, all other clks
   * added dynamically will be done so with clk_debug_register.
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2185
2186
2187
2188
2189
   */
  static int __init clk_debug_init(void)
  {
  	struct clk_core *core;
  	struct dentry *d;
dfc202ead   Stephen Boyd   clk: Add tracepoi...
2190

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2191
  	rootdir = debugfs_create_dir("clk", NULL);
e59c5371f   Mike Turquette   clk: introduce cl...
2192

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2193
2194
  	if (!rootdir)
  		return -ENOMEM;
dfc202ead   Stephen Boyd   clk: Add tracepoi...
2195

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2196
2197
2198
2199
  	d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
  				&clk_summary_fops);
  	if (!d)
  		return -ENOMEM;
e59c5371f   Mike Turquette   clk: introduce cl...
2200

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2201
2202
2203
2204
  	d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
  				&clk_dump_fops);
  	if (!d)
  		return -ENOMEM;
e59c5371f   Mike Turquette   clk: introduce cl...
2205

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2206
2207
2208
2209
  	d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
  				&orphan_list, &clk_summary_fops);
  	if (!d)
  		return -ENOMEM;
e59c5371f   Mike Turquette   clk: introduce cl...
2210

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2211
2212
2213
2214
  	d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
  				&orphan_list, &clk_dump_fops);
  	if (!d)
  		return -ENOMEM;
e59c5371f   Mike Turquette   clk: introduce cl...
2215

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2216
2217
2218
  	mutex_lock(&clk_debug_lock);
  	hlist_for_each_entry(core, &clk_debug_list, debug_node)
  		clk_debug_create_one(core, rootdir);
e59c5371f   Mike Turquette   clk: introduce cl...
2219

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2220
2221
  	inited = 1;
  	mutex_unlock(&clk_debug_lock);
e59c5371f   Mike Turquette   clk: introduce cl...
2222

4dff95dc9   Stephen Boyd   clk: Remove forwa...
2223
2224
2225
2226
2227
2228
2229
  	return 0;
  }
  late_initcall(clk_debug_init);
  #else
  static inline int clk_debug_register(struct clk_core *core) { return 0; }
  static inline void clk_debug_reparent(struct clk_core *core,
  				      struct clk_core *new_parent)
035a61c31   Tomeu Vizoso   clk: Make clk API...
2230
  {
035a61c31   Tomeu Vizoso   clk: Make clk API...
2231
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2232
  static inline void clk_debug_unregister(struct clk_core *core)
3d3801eff   Michael Turquette   clk: introduce cl...
2233
  {
3d3801eff   Michael Turquette   clk: introduce cl...
2234
  }
4dff95dc9   Stephen Boyd   clk: Remove forwa...
2235
  #endif
3d3801eff   Michael Turquette   clk: introduce cl...
2236
2237
  
  /**
be45ebf25   Masahiro Yamada   clk: rename __clk...
2238
   * __clk_core_init - initialize the data structures in a struct clk_core
d35c80c24   Masahiro Yamada   clk: change the a...
2239
   * @core:	clk_core being initialized
b2476490e   Mike Turquette   clk: introduce th...
2240
   *
035a61c31   Tomeu Vizoso   clk: Make clk API...
2241
   * Initializes the lists in struct clk_core, queries the hardware for the
b2476490e   Mike Turquette   clk: introduce th...
2242
   * parent and rate and sets them both.
b2476490e   Mike Turquette   clk: introduce th...
2243
   */
be45ebf25   Masahiro Yamada   clk: rename __clk...
2244
  static int __clk_core_init(struct clk_core *core)
b2476490e   Mike Turquette   clk: introduce th...
2245
  {
d1302a36a   Mike Turquette   clk: core: copy p...
2246
  	int i, ret = 0;
035a61c31   Tomeu Vizoso   clk: Make clk API...
2247
  	struct clk_core *orphan;
b67bfe0d4   Sasha Levin   hlist: drop the n...
2248
  	struct hlist_node *tmp2;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2249
  	unsigned long rate;
b2476490e   Mike Turquette   clk: introduce th...
2250

d35c80c24   Masahiro Yamada   clk: change the a...
2251
  	if (!core)
d1302a36a   Mike Turquette   clk: core: copy p...
2252
  		return -EINVAL;
b2476490e   Mike Turquette   clk: introduce th...
2253

eab89f690   Mike Turquette   clk: abstract loc...
2254
  	clk_prepare_lock();
b2476490e   Mike Turquette   clk: introduce th...
2255
2256
  
  	/* check to see if a clock with this name is already registered */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2257
  	if (clk_core_lookup(core->name)) {
d1302a36a   Mike Turquette   clk: core: copy p...
2258
2259
  		pr_debug("%s: clk %s already initialized
  ",
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2260
  				__func__, core->name);
d1302a36a   Mike Turquette   clk: core: copy p...
2261
  		ret = -EEXIST;
b2476490e   Mike Turquette   clk: introduce th...
2262
  		goto out;
d1302a36a   Mike Turquette   clk: core: copy p...
2263
  	}
b2476490e   Mike Turquette   clk: introduce th...
2264

d4d7e3ddc   Mike Turquette   clk: core: enforc...
2265
  	/* check that clk_ops are sane.  See Documentation/clk.txt */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2266
2267
2268
  	if (core->ops->set_rate &&
  	    !((core->ops->round_rate || core->ops->determine_rate) &&
  	      core->ops->recalc_rate)) {
c44fccb5f   Masahiro Yamada   clk: replace pr_w...
2269
2270
2271
  		pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate
  ",
  		       __func__, core->name);
d1302a36a   Mike Turquette   clk: core: copy p...
2272
  		ret = -EINVAL;
d4d7e3ddc   Mike Turquette   clk: core: enforc...
2273
2274
  		goto out;
  	}
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2275
  	if (core->ops->set_parent && !core->ops->get_parent) {
c44fccb5f   Masahiro Yamada   clk: replace pr_w...
2276
2277
2278
  		pr_err("%s: %s must implement .get_parent & .set_parent
  ",
  		       __func__, core->name);
d1302a36a   Mike Turquette   clk: core: copy p...
2279
  		ret = -EINVAL;
d4d7e3ddc   Mike Turquette   clk: core: enforc...
2280
2281
  		goto out;
  	}
3c8e77dd2   Masahiro Yamada   clk: move checkin...
2282
2283
2284
2285
2286
2287
2288
  	if (core->num_parents > 1 && !core->ops->get_parent) {
  		pr_err("%s: %s must implement .get_parent as it has multi parents
  ",
  		       __func__, core->name);
  		ret = -EINVAL;
  		goto out;
  	}
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2289
2290
  	if (core->ops->set_rate_and_parent &&
  			!(core->ops->set_parent && core->ops->set_rate)) {
c44fccb5f   Masahiro Yamada   clk: replace pr_w...
2291
2292
  		pr_err("%s: %s must implement .set_parent & .set_rate
  ",
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2293
  				__func__, core->name);
3fa2252b7   Stephen Boyd   clk: Add set_rate...
2294
2295
2296
  		ret = -EINVAL;
  		goto out;
  	}
b2476490e   Mike Turquette   clk: introduce th...
2297
  	/* throw a WARN if any entries in parent_names are NULL */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2298
2299
  	for (i = 0; i < core->num_parents; i++)
  		WARN(!core->parent_names[i],
b2476490e   Mike Turquette   clk: introduce th...
2300
2301
  				"%s: invalid NULL in %s's .parent_names
  ",
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2302
  				__func__, core->name);
b2476490e   Mike Turquette   clk: introduce th...
2303

d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2304
  	core->parent = __clk_init_parent(core);
b2476490e   Mike Turquette   clk: introduce th...
2305
2306
  
  	/*
706d5c73e   Stephen Boyd   clk: Update some ...
2307
2308
  	 * Populate core->parent if parent has already been clk_core_init'd. If
  	 * parent has not yet been clk_core_init'd then place clk in the orphan
47b0eeb3d   Stephen Boyd   clk: Deprecate CL...
2309
  	 * list.  If clk doesn't have any parents then place it in the root
b2476490e   Mike Turquette   clk: introduce th...
2310
2311
2312
2313
2314
2315
  	 * clk list.
  	 *
  	 * Every time a new clk is clk_init'd then we walk the list of orphan
  	 * clocks and re-parent any that are children of the clock currently
  	 * being clk_init'd.
  	 */
e6500344e   Heiko Stuebner   clk: track the or...
2316
  	if (core->parent) {
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2317
2318
  		hlist_add_head(&core->child_node,
  				&core->parent->children);
e6500344e   Heiko Stuebner   clk: track the or...
2319
  		core->orphan = core->parent->orphan;
47b0eeb3d   Stephen Boyd   clk: Deprecate CL...
2320
  	} else if (!core->num_parents) {
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2321
  		hlist_add_head(&core->child_node, &clk_root_list);
e6500344e   Heiko Stuebner   clk: track the or...
2322
2323
  		core->orphan = false;
  	} else {
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2324
  		hlist_add_head(&core->child_node, &clk_orphan_list);
e6500344e   Heiko Stuebner   clk: track the or...
2325
2326
  		core->orphan = true;
  	}
b2476490e   Mike Turquette   clk: introduce th...
2327
2328
  
  	/*
5279fc402   Boris BREZILLON   clk: add clk accu...
2329
2330
2331
2332
2333
2334
  	 * Set clk's accuracy.  The preferred method is to use
  	 * .recalc_accuracy. For simple clocks and lazy developers the default
  	 * fallback is to use the parent's accuracy.  If a clock doesn't have a
  	 * parent (or is orphaned) then accuracy is set to zero (perfect
  	 * clock).
  	 */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2335
2336
2337
2338
2339
  	if (core->ops->recalc_accuracy)
  		core->accuracy = core->ops->recalc_accuracy(core->hw,
  					__clk_get_accuracy(core->parent));
  	else if (core->parent)
  		core->accuracy = core->parent->accuracy;
5279fc402   Boris BREZILLON   clk: add clk accu...
2340
  	else
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2341
  		core->accuracy = 0;
5279fc402   Boris BREZILLON   clk: add clk accu...
2342
2343
  
  	/*
9824cf73c   Maxime Ripard   clk: Add a functi...
2344
2345
2346
2347
  	 * Set clk's phase.
  	 * Since a phase is by definition relative to its parent, just
  	 * query the current clock phase, or just assume it's in phase.
  	 */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2348
2349
  	if (core->ops->get_phase)
  		core->phase = core->ops->get_phase(core->hw);
9824cf73c   Maxime Ripard   clk: Add a functi...
2350
  	else
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2351
  		core->phase = 0;
9824cf73c   Maxime Ripard   clk: Add a functi...
2352
2353
  
  	/*
b2476490e   Mike Turquette   clk: introduce th...
2354
2355
2356
2357
2358
  	 * Set clk's rate.  The preferred method is to use .recalc_rate.  For
  	 * simple clocks and lazy developers the default fallback is to use the
  	 * parent's rate.  If a clock doesn't have a parent (or is orphaned)
  	 * then rate is set to zero.
  	 */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2359
2360
2361
2362
2363
  	if (core->ops->recalc_rate)
  		rate = core->ops->recalc_rate(core->hw,
  				clk_core_get_rate_nolock(core->parent));
  	else if (core->parent)
  		rate = core->parent->rate;
b2476490e   Mike Turquette   clk: introduce th...
2364
  	else
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2365
  		rate = 0;
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2366
  	core->rate = core->req_rate = rate;
b2476490e   Mike Turquette   clk: introduce th...
2367
2368
  
  	/*
0e8f6e499   Masahiro Yamada   clk: walk the orp...
2369
2370
  	 * walk the list of orphan clocks and reparent any that newly finds a
  	 * parent.
b2476490e   Mike Turquette   clk: introduce th...
2371
  	 */
b67bfe0d4   Sasha Levin   hlist: drop the n...
2372
  	hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
0e8f6e499   Masahiro Yamada   clk: walk the orp...
2373
  		struct clk_core *parent = __clk_init_parent(orphan);
1f61e5f14   Martin Fuzzey   clk: clock multip...
2374

904e6ead0   Michael Turquette   clk: migrate ref ...
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
  		/*
  		 * we could call __clk_set_parent, but that would result in a
  		 * redundant call to the .set_rate op, if it exists
  		 */
  		if (parent) {
  			__clk_set_parent_before(orphan, parent);
  			__clk_set_parent_after(orphan, parent, NULL);
  			__clk_recalc_accuracies(orphan);
  			__clk_recalc_rates(orphan, 0);
  		}
0e8f6e499   Masahiro Yamada   clk: walk the orp...
2385
  	}
b2476490e   Mike Turquette   clk: introduce th...
2386
2387
2388
2389
2390
2391
2392
  
  	/*
  	 * optional platform-specific magic
  	 *
  	 * The .init callback is not used by any of the basic clock types, but
  	 * exists for weird hardware that must perform initialization magic.
  	 * Please consider other ways of solving initialization problems before
24ee1a083   Peter Meerwald   clk: fix typos
2393
  	 * using this callback, as its use is discouraged.
b2476490e   Mike Turquette   clk: introduce th...
2394
  	 */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2395
2396
  	if (core->ops->init)
  		core->ops->init(core->hw);
b2476490e   Mike Turquette   clk: introduce th...
2397

32b9b1096   Lee Jones   clk: Allow clocks...
2398
  	if (core->flags & CLK_IS_CRITICAL) {
ef56b79b6   Maxime Ripard   clk: fix critical...
2399
  		unsigned long flags;
32b9b1096   Lee Jones   clk: Allow clocks...
2400
  		clk_core_prepare(core);
ef56b79b6   Maxime Ripard   clk: fix critical...
2401
2402
  
  		flags = clk_enable_lock();
32b9b1096   Lee Jones   clk: Allow clocks...
2403
  		clk_core_enable(core);
ef56b79b6   Maxime Ripard   clk: fix critical...
2404
  		clk_enable_unlock(flags);
32b9b1096   Lee Jones   clk: Allow clocks...
2405
  	}
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2406
  	kref_init(&core->ref);
b2476490e   Mike Turquette   clk: introduce th...
2407
  out:
eab89f690   Mike Turquette   clk: abstract loc...
2408
  	clk_prepare_unlock();
b2476490e   Mike Turquette   clk: introduce th...
2409

89f7e9de5   Stephen Boyd   clk: Really fix d...
2410
  	if (!ret)
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2411
  		clk_debug_register(core);
89f7e9de5   Stephen Boyd   clk: Really fix d...
2412

d1302a36a   Mike Turquette   clk: core: copy p...
2413
  	return ret;
b2476490e   Mike Turquette   clk: introduce th...
2414
  }
035a61c31   Tomeu Vizoso   clk: Make clk API...
2415
2416
  struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
  			     const char *con_id)
0197b3ea0   Saravana Kannan   clk: Use a separa...
2417
  {
0197b3ea0   Saravana Kannan   clk: Use a separa...
2418
  	struct clk *clk;
035a61c31   Tomeu Vizoso   clk: Make clk API...
2419
  	/* This is to allow this function to be chained to others */
c1de13574   Masahiro Yamada   clk: use IS_ERR_O...
2420
  	if (IS_ERR_OR_NULL(hw))
8a23133c7   Masahiro Yamada   clk: use ERR_CAST...
2421
  		return ERR_CAST(hw);
0197b3ea0   Saravana Kannan   clk: Use a separa...
2422

035a61c31   Tomeu Vizoso   clk: Make clk API...
2423
2424
2425
2426
2427
2428
2429
  	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  	if (!clk)
  		return ERR_PTR(-ENOMEM);
  
  	clk->core = hw->core;
  	clk->dev_id = dev_id;
  	clk->con_id = con_id;
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2430
2431
2432
  	clk->max_rate = ULONG_MAX;
  
  	clk_prepare_lock();
50595f8b9   Stephen Boyd   clk: Rename child...
2433
  	hlist_add_head(&clk->clks_node, &hw->core->clks);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2434
  	clk_prepare_unlock();
0197b3ea0   Saravana Kannan   clk: Use a separa...
2435
2436
2437
  
  	return clk;
  }
035a61c31   Tomeu Vizoso   clk: Make clk API...
2438

73e0e496a   Stephen Boyd   clkdev: Always al...
2439
  void __clk_free_clk(struct clk *clk)
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2440
2441
  {
  	clk_prepare_lock();
50595f8b9   Stephen Boyd   clk: Rename child...
2442
  	hlist_del(&clk->clks_node);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2443
2444
2445
2446
  	clk_prepare_unlock();
  
  	kfree(clk);
  }
0197b3ea0   Saravana Kannan   clk: Use a separa...
2447

293ba3b4a   Stephen Boyd   clk: Fix double f...
2448
2449
2450
2451
2452
2453
2454
  /**
   * clk_register - allocate a new clock, register it and return an opaque cookie
   * @dev: device that is registering this clock
   * @hw: link to hardware-specific clock data
   *
   * clk_register is the primary interface for populating the clock tree with new
   * clock nodes.  It returns a pointer to the newly allocated struct clk which
a59a51639   Shailendra Verma   clk: Fix typo in ...
2455
   * cannot be dereferenced by driver code but may be used in conjunction with the
293ba3b4a   Stephen Boyd   clk: Fix double f...
2456
2457
2458
2459
   * rest of the clock API.  In the event of an error clk_register will return an
   * error code; drivers must test for an error code after calling clk_register.
   */
  struct clk *clk_register(struct device *dev, struct clk_hw *hw)
b2476490e   Mike Turquette   clk: introduce th...
2460
  {
d1302a36a   Mike Turquette   clk: core: copy p...
2461
  	int i, ret;
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2462
  	struct clk_core *core;
293ba3b4a   Stephen Boyd   clk: Fix double f...
2463

d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2464
2465
  	core = kzalloc(sizeof(*core), GFP_KERNEL);
  	if (!core) {
293ba3b4a   Stephen Boyd   clk: Fix double f...
2466
2467
2468
  		ret = -ENOMEM;
  		goto fail_out;
  	}
b2476490e   Mike Turquette   clk: introduce th...
2469

d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2470
2471
  	core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
  	if (!core->name) {
0197b3ea0   Saravana Kannan   clk: Use a separa...
2472
2473
2474
  		ret = -ENOMEM;
  		goto fail_name;
  	}
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2475
  	core->ops = hw->init->ops;
ac2df527f   Sylwester Nawrocki   clk: Add common _...
2476
  	if (dev && dev->driver)
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2477
2478
2479
2480
  		core->owner = dev->driver->owner;
  	core->hw = hw;
  	core->flags = hw->init->flags;
  	core->num_parents = hw->init->num_parents;
9783c0d98   Stephen Boyd   clk: Allow provid...
2481
2482
  	core->min_rate = 0;
  	core->max_rate = ULONG_MAX;
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2483
  	hw->core = core;
b2476490e   Mike Turquette   clk: introduce th...
2484

d1302a36a   Mike Turquette   clk: core: copy p...
2485
  	/* allocate local copy in case parent_names is __initdata */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2486
  	core->parent_names = kcalloc(core->num_parents, sizeof(char *),
96a7ed907   Tomasz Figa   clk: Use kcalloc(...
2487
  					GFP_KERNEL);
d1302a36a   Mike Turquette   clk: core: copy p...
2488

d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2489
  	if (!core->parent_names) {
d1302a36a   Mike Turquette   clk: core: copy p...
2490
2491
2492
2493
2494
2495
  		ret = -ENOMEM;
  		goto fail_parent_names;
  	}
  
  
  	/* copy each string name in case parent_names is __initdata */
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2496
2497
  	for (i = 0; i < core->num_parents; i++) {
  		core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
0197b3ea0   Saravana Kannan   clk: Use a separa...
2498
  						GFP_KERNEL);
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2499
  		if (!core->parent_names[i]) {
d1302a36a   Mike Turquette   clk: core: copy p...
2500
2501
2502
2503
  			ret = -ENOMEM;
  			goto fail_parent_names_copy;
  		}
  	}
176d11690   Masahiro Yamada   clk: move core->p...
2504
2505
2506
2507
2508
2509
2510
  	/* avoid unnecessary string look-ups of clk_core's possible parents. */
  	core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
  				GFP_KERNEL);
  	if (!core->parents) {
  		ret = -ENOMEM;
  		goto fail_parents;
  	};
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2511
  	INIT_HLIST_HEAD(&core->clks);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2512

035a61c31   Tomeu Vizoso   clk: Make clk API...
2513
2514
  	hw->clk = __clk_create_clk(hw, NULL, NULL);
  	if (IS_ERR(hw->clk)) {
035a61c31   Tomeu Vizoso   clk: Make clk API...
2515
  		ret = PTR_ERR(hw->clk);
176d11690   Masahiro Yamada   clk: move core->p...
2516
  		goto fail_parents;
035a61c31   Tomeu Vizoso   clk: Make clk API...
2517
  	}
be45ebf25   Masahiro Yamada   clk: rename __clk...
2518
  	ret = __clk_core_init(core);
d1302a36a   Mike Turquette   clk: core: copy p...
2519
  	if (!ret)
035a61c31   Tomeu Vizoso   clk: Make clk API...
2520
  		return hw->clk;
b2476490e   Mike Turquette   clk: introduce th...
2521

1c8e60044   Tomeu Vizoso   clk: Add rate con...
2522
  	__clk_free_clk(hw->clk);
035a61c31   Tomeu Vizoso   clk: Make clk API...
2523
  	hw->clk = NULL;
b2476490e   Mike Turquette   clk: introduce th...
2524

176d11690   Masahiro Yamada   clk: move core->p...
2525
2526
  fail_parents:
  	kfree(core->parents);
d1302a36a   Mike Turquette   clk: core: copy p...
2527
2528
  fail_parent_names_copy:
  	while (--i >= 0)
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2529
2530
  		kfree_const(core->parent_names[i]);
  	kfree(core->parent_names);
d1302a36a   Mike Turquette   clk: core: copy p...
2531
  fail_parent_names:
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2532
  	kfree_const(core->name);
0197b3ea0   Saravana Kannan   clk: Use a separa...
2533
  fail_name:
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2534
  	kfree(core);
d1302a36a   Mike Turquette   clk: core: copy p...
2535
2536
  fail_out:
  	return ERR_PTR(ret);
b2476490e   Mike Turquette   clk: introduce th...
2537
2538
  }
  EXPORT_SYMBOL_GPL(clk_register);
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
  /**
   * clk_hw_register - register a clk_hw and return an error code
   * @dev: device that is registering this clock
   * @hw: link to hardware-specific clock data
   *
   * clk_hw_register is the primary interface for populating the clock tree with
   * new clock nodes. It returns an integer equal to zero indicating success or
   * less than zero indicating failure. Drivers must test for an error code after
   * calling clk_hw_register().
   */
  int clk_hw_register(struct device *dev, struct clk_hw *hw)
  {
  	return PTR_ERR_OR_ZERO(clk_register(dev, hw));
  }
  EXPORT_SYMBOL_GPL(clk_hw_register);
6e5ab41b1   Stephen Boyd   clk: Update some ...
2554
  /* Free memory allocated for a clock. */
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2555
2556
  static void __clk_release(struct kref *ref)
  {
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2557
2558
  	struct clk_core *core = container_of(ref, struct clk_core, ref);
  	int i = core->num_parents;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2559

496eadf82   Krzysztof Kozlowski   clk: Use lockdep ...
2560
  	lockdep_assert_held(&prepare_lock);
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2561
  	kfree(core->parents);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2562
  	while (--i >= 0)
d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2563
  		kfree_const(core->parent_names[i]);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2564

d6968fca7   Stephen Boyd   clk: s/clk/core/ ...
2565
2566
2567
  	kfree(core->parent_names);
  	kfree_const(core->name);
  	kfree(core);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
  }
  
  /*
   * Empty clk_ops for unregistered clocks. These are used temporarily
   * after clk_unregister() was called on a clock and until last clock
   * consumer calls clk_put() and the struct clk object is freed.
   */
  static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  {
  	return -ENXIO;
  }
  
  static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  {
  	WARN_ON_ONCE(1);
  }
  
  static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  					unsigned long parent_rate)
  {
  	return -ENXIO;
  }
  
  static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  {
  	return -ENXIO;
  }
  
  static const struct clk_ops clk_nodrv_ops = {
  	.enable		= clk_nodrv_prepare_enable,
  	.disable	= clk_nodrv_disable_unprepare,
  	.prepare	= clk_nodrv_prepare_enable,
  	.unprepare	= clk_nodrv_disable_unprepare,
  	.set_rate	= clk_nodrv_set_rate,
  	.set_parent	= clk_nodrv_set_parent,
  };
1df5c939f   Mark Brown   clk: Provide dumm...
2604
2605
2606
  /**
   * clk_unregister - unregister a currently registered clock
   * @clk: clock to unregister
1df5c939f   Mark Brown   clk: Provide dumm...
2607
   */
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2608
2609
2610
  void clk_unregister(struct clk *clk)
  {
  	unsigned long flags;
6314b6796   Stephen Boyd   clk: Don't hold p...
2611
2612
  	if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  		return;
035a61c31   Tomeu Vizoso   clk: Make clk API...
2613
  	clk_debug_unregister(clk->core);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2614
2615
  
  	clk_prepare_lock();
035a61c31   Tomeu Vizoso   clk: Make clk API...
2616
2617
2618
2619
  	if (clk->core->ops == &clk_nodrv_ops) {
  		pr_err("%s: unregistered clock: %s
  ", __func__,
  		       clk->core->name);
4106a3d9e   Insu Yun   clk: unlock for h...
2620
  		goto unlock;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2621
2622
2623
2624
2625
2626
  	}
  	/*
  	 * Assign empty clock ops for consumers that might still hold
  	 * a reference to this clock.
  	 */
  	flags = clk_enable_lock();
035a61c31   Tomeu Vizoso   clk: Make clk API...
2627
  	clk->core->ops = &clk_nodrv_ops;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2628
  	clk_enable_unlock(flags);
035a61c31   Tomeu Vizoso   clk: Make clk API...
2629
2630
  	if (!hlist_empty(&clk->core->children)) {
  		struct clk_core *child;
874f224cc   Stephen Boyd   clk: Fix slab cor...
2631
  		struct hlist_node *t;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2632
2633
  
  		/* Reparent all children to the orphan list. */
035a61c31   Tomeu Vizoso   clk: Make clk API...
2634
2635
2636
  		hlist_for_each_entry_safe(child, t, &clk->core->children,
  					  child_node)
  			clk_core_set_parent(child, NULL);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2637
  	}
035a61c31   Tomeu Vizoso   clk: Make clk API...
2638
  	hlist_del_init(&clk->core->child_node);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2639

035a61c31   Tomeu Vizoso   clk: Make clk API...
2640
  	if (clk->core->prepare_count)
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2641
2642
  		pr_warn("%s: unregistering prepared clock: %s
  ",
035a61c31   Tomeu Vizoso   clk: Make clk API...
2643
2644
  					__func__, clk->core->name);
  	kref_put(&clk->core->ref, __clk_release);
4106a3d9e   Insu Yun   clk: unlock for h...
2645
  unlock:
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2646
2647
  	clk_prepare_unlock();
  }
1df5c939f   Mark Brown   clk: Provide dumm...
2648
  EXPORT_SYMBOL_GPL(clk_unregister);
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2649
2650
2651
2652
2653
2654
2655
2656
2657
  /**
   * clk_hw_unregister - unregister a currently registered clk_hw
   * @hw: hardware-specific clock data to unregister
   */
  void clk_hw_unregister(struct clk_hw *hw)
  {
  	clk_unregister(hw->clk);
  }
  EXPORT_SYMBOL_GPL(clk_hw_unregister);
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2658
2659
  static void devm_clk_release(struct device *dev, void *res)
  {
293ba3b4a   Stephen Boyd   clk: Fix double f...
2660
  	clk_unregister(*(struct clk **)res);
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2661
  }
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2662
2663
2664
2665
  static void devm_clk_hw_release(struct device *dev, void *res)
  {
  	clk_hw_unregister(*(struct clk_hw **)res);
  }
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
  /**
   * devm_clk_register - resource managed clk_register()
   * @dev: device that is registering this clock
   * @hw: link to hardware-specific clock data
   *
   * Managed clk_register(). Clocks returned from this function are
   * automatically clk_unregister()ed on driver detach. See clk_register() for
   * more information.
   */
  struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  {
  	struct clk *clk;
293ba3b4a   Stephen Boyd   clk: Fix double f...
2678
  	struct clk **clkp;
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2679

293ba3b4a   Stephen Boyd   clk: Fix double f...
2680
2681
  	clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  	if (!clkp)
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2682
  		return ERR_PTR(-ENOMEM);
293ba3b4a   Stephen Boyd   clk: Fix double f...
2683
2684
2685
2686
  	clk = clk_register(dev, hw);
  	if (!IS_ERR(clk)) {
  		*clkp = clk;
  		devres_add(dev, clkp);
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2687
  	} else {
293ba3b4a   Stephen Boyd   clk: Fix double f...
2688
  		devres_free(clkp);
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2689
2690
2691
2692
2693
  	}
  
  	return clk;
  }
  EXPORT_SYMBOL_GPL(devm_clk_register);
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2694
2695
2696
2697
2698
  /**
   * devm_clk_hw_register - resource managed clk_hw_register()
   * @dev: device that is registering this clock
   * @hw: link to hardware-specific clock data
   *
c47265ad6   Masahiro Yamada   clk: fix comment ...
2699
   * Managed clk_hw_register(). Clocks registered by this function are
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
   * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
   * for more information.
   */
  int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
  {
  	struct clk_hw **hwp;
  	int ret;
  
  	hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
  	if (!hwp)
  		return -ENOMEM;
  
  	ret = clk_hw_register(dev, hw);
  	if (!ret) {
  		*hwp = hw;
  		devres_add(dev, hwp);
  	} else {
  		devres_free(hwp);
  	}
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(devm_clk_hw_register);
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2723
2724
2725
2726
2727
2728
2729
  static int devm_clk_match(struct device *dev, void *res, void *data)
  {
  	struct clk *c = res;
  	if (WARN_ON(!c))
  		return 0;
  	return c == data;
  }
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2730
2731
2732
2733
2734
2735
2736
2737
  static int devm_clk_hw_match(struct device *dev, void *res, void *data)
  {
  	struct clk_hw *hw = res;
  
  	if (WARN_ON(!hw))
  		return 0;
  	return hw == data;
  }
46c8773a5   Stephen Boyd   clk: Add devm_clk...
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
  /**
   * devm_clk_unregister - resource managed clk_unregister()
   * @clk: clock to unregister
   *
   * Deallocate a clock allocated with devm_clk_register(). Normally
   * this function will not need to be called and the resource management
   * code will ensure that the resource is freed.
   */
  void devm_clk_unregister(struct device *dev, struct clk *clk)
  {
  	WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
  }
  EXPORT_SYMBOL_GPL(devm_clk_unregister);
4143804c4   Stephen Boyd   clk: Add {devm_}c...
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
  /**
   * devm_clk_hw_unregister - resource managed clk_hw_unregister()
   * @dev: device that is unregistering the hardware-specific clock data
   * @hw: link to hardware-specific clock data
   *
   * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
   * this function will not need to be called and the resource management
   * code will ensure that the resource is freed.
   */
  void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
  {
  	WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
  				hw));
  }
  EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
ac2df527f   Sylwester Nawrocki   clk: Add common _...
2766
2767
2768
2769
2770
  /*
   * clkdev helpers
   */
  int __clk_get(struct clk *clk)
  {
035a61c31   Tomeu Vizoso   clk: Make clk API...
2771
2772
2773
2774
  	struct clk_core *core = !clk ? NULL : clk->core;
  
  	if (core) {
  		if (!try_module_get(core->owner))
00efcb1c8   Sylwester Nawrocki   clk: Correct hand...
2775
  			return 0;
ac2df527f   Sylwester Nawrocki   clk: Add common _...
2776

035a61c31   Tomeu Vizoso   clk: Make clk API...
2777
  		kref_get(&core->ref);
00efcb1c8   Sylwester Nawrocki   clk: Correct hand...
2778
  	}
ac2df527f   Sylwester Nawrocki   clk: Add common _...
2779
2780
2781
2782
2783
  	return 1;
  }
  
  void __clk_put(struct clk *clk)
  {
10cdfe54d   Tomeu Vizoso   clk: Don't try to...
2784
  	struct module *owner;
00efcb1c8   Sylwester Nawrocki   clk: Correct hand...
2785
  	if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
ac2df527f   Sylwester Nawrocki   clk: Add common _...
2786
  		return;
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2787
  	clk_prepare_lock();
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2788

50595f8b9   Stephen Boyd   clk: Rename child...
2789
  	hlist_del(&clk->clks_node);
ec02ace8c   Tomeu Vizoso   clk: Only recalcu...
2790
2791
2792
  	if (clk->min_rate > clk->core->req_rate ||
  	    clk->max_rate < clk->core->req_rate)
  		clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1c8e60044   Tomeu Vizoso   clk: Add rate con...
2793
2794
  	owner = clk->core->owner;
  	kref_put(&clk->core->ref, __clk_release);
fcb0ee6a3   Sylwester Nawrocki   clk: Implement cl...
2795
  	clk_prepare_unlock();
10cdfe54d   Tomeu Vizoso   clk: Don't try to...
2796
  	module_put(owner);
035a61c31   Tomeu Vizoso   clk: Make clk API...
2797

035a61c31   Tomeu Vizoso   clk: Make clk API...
2798
  	kfree(clk);
ac2df527f   Sylwester Nawrocki   clk: Add common _...
2799
  }
b2476490e   Mike Turquette   clk: introduce th...
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
  /***        clk rate change notifiers        ***/
  
  /**
   * clk_notifier_register - add a clk rate change notifier
   * @clk: struct clk * to watch
   * @nb: struct notifier_block * with callback info
   *
   * Request notification when clk's rate changes.  This uses an SRCU
   * notifier because we want it to block and notifier unregistrations are
   * uncommon.  The callbacks associated with the notifier must not
   * re-enter into the clk framework by calling any top-level clk APIs;
   * this will cause a nested prepare_lock mutex.
   *
198bb5949   Masahiro Yamada   clk: fix a typo i...
2813
2814
2815
   * In all notification cases (pre, post and abort rate change) the original
   * clock rate is passed to the callback via struct clk_notifier_data.old_rate
   * and the new frequency is passed via struct clk_notifier_data.new_rate.
b2476490e   Mike Turquette   clk: introduce th...
2816
   *
b2476490e   Mike Turquette   clk: introduce th...
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
   * clk_notifier_register() must be called from non-atomic context.
   * Returns -EINVAL if called with null arguments, -ENOMEM upon
   * allocation failure; otherwise, passes along the return value of
   * srcu_notifier_chain_register().
   */
  int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  {
  	struct clk_notifier *cn;
  	int ret = -ENOMEM;
  
  	if (!clk || !nb)
  		return -EINVAL;
eab89f690   Mike Turquette   clk: abstract loc...
2829
  	clk_prepare_lock();
b2476490e   Mike Turquette   clk: introduce th...
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
  
  	/* search the list of notifiers for this clk */
  	list_for_each_entry(cn, &clk_notifier_list, node)
  		if (cn->clk == clk)
  			break;
  
  	/* if clk wasn't in the notifier list, allocate new clk_notifier */
  	if (cn->clk != clk) {
  		cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
  		if (!cn)
  			goto out;
  
  		cn->clk = clk;
  		srcu_init_notifier_head(&cn->notifier_head);
  
  		list_add(&cn->node, &clk_notifier_list);
  	}
  
  	ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
035a61c31   Tomeu Vizoso   clk: Make clk API...
2849
  	clk->core->notifier_count++;
b2476490e   Mike Turquette   clk: introduce th...
2850
2851
  
  out:
eab89f690   Mike Turquette   clk: abstract loc...
2852
  	clk_prepare_unlock();
b2476490e   Mike Turquette   clk: introduce th...
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(clk_notifier_register);
  
  /**
   * clk_notifier_unregister - remove a clk rate change notifier
   * @clk: struct clk *
   * @nb: struct notifier_block * with callback info
   *
   * Request no further notification for changes to 'clk' and frees memory
   * allocated in clk_notifier_register.
   *
   * Returns -EINVAL if called with null arguments; otherwise, passes
   * along the return value of srcu_notifier_chain_unregister().
   */
  int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  {
  	struct clk_notifier *cn = NULL;
  	int ret = -EINVAL;
  
  	if (!clk || !nb)
  		return -EINVAL;
eab89f690   Mike Turquette   clk: abstract loc...
2876
  	clk_prepare_lock();
b2476490e   Mike Turquette   clk: introduce th...
2877
2878
2879
2880
2881
2882
2883
  
  	list_for_each_entry(cn, &clk_notifier_list, node)
  		if (cn->clk == clk)
  			break;
  
  	if (cn->clk == clk) {
  		ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
035a61c31   Tomeu Vizoso   clk: Make clk API...
2884
  		clk->core->notifier_count--;
b2476490e   Mike Turquette   clk: introduce th...
2885
2886
2887
2888
  
  		/* XXX the notifier code should handle this better */
  		if (!cn->notifier_head.head) {
  			srcu_cleanup_notifier_head(&cn->notifier_head);
72b5322f1   Lai Jiangshan   clk: remove notif...
2889
  			list_del(&cn->node);
b2476490e   Mike Turquette   clk: introduce th...
2890
2891
2892
2893
2894
2895
  			kfree(cn);
  		}
  
  	} else {
  		ret = -ENOENT;
  	}
eab89f690   Mike Turquette   clk: abstract loc...
2896
  	clk_prepare_unlock();
b2476490e   Mike Turquette   clk: introduce th...
2897
2898
2899
2900
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(clk_notifier_unregister);
766e6a4ec   Grant Likely   clk: add DT clock...
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
  
  #ifdef CONFIG_OF
  /**
   * struct of_clk_provider - Clock provider registration structure
   * @link: Entry in global list of clock providers
   * @node: Pointer to device tree node of clock provider
   * @get: Get clock callback.  Returns NULL or a struct clk for the
   *       given clock specifier
   * @data: context pointer to be passed into @get callback
   */
  struct of_clk_provider {
  	struct list_head link;
  
  	struct device_node *node;
  	struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
2916
  	struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
766e6a4ec   Grant Likely   clk: add DT clock...
2917
2918
  	void *data;
  };
f2f6c2556   Prashant Gaikwad   clk: add common o...
2919
2920
  static const struct of_device_id __clk_of_table_sentinel
  	__used __section(__clk_of_table_end);
766e6a4ec   Grant Likely   clk: add DT clock...
2921
  static LIST_HEAD(of_clk_providers);
d6782c263   Sylwester Nawrocki   clk: Provide not ...
2922
  static DEFINE_MUTEX(of_clk_mutex);
766e6a4ec   Grant Likely   clk: add DT clock...
2923
2924
2925
2926
2927
2928
  struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  				     void *data)
  {
  	return data;
  }
  EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
2929
2930
2931
2932
2933
  struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  {
  	return data;
  }
  EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
494bfec99   Shawn Guo   clk: add of_clk_s...
2934
2935
2936
2937
2938
2939
  struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  {
  	struct clk_onecell_data *clk_data = data;
  	unsigned int idx = clkspec->args[0];
  
  	if (idx >= clk_data->clk_num) {
7e96353c3   Geert Uytterhoeven   clk: Use %u to fo...
2940
2941
  		pr_err("%s: invalid clock index %u
  ", __func__, idx);
494bfec99   Shawn Guo   clk: add of_clk_s...
2942
2943
2944
2945
2946
2947
  		return ERR_PTR(-EINVAL);
  	}
  
  	return clk_data->clks[idx];
  }
  EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
  struct clk_hw *
  of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  {
  	struct clk_hw_onecell_data *hw_data = data;
  	unsigned int idx = clkspec->args[0];
  
  	if (idx >= hw_data->num) {
  		pr_err("%s: invalid index %u
  ", __func__, idx);
  		return ERR_PTR(-EINVAL);
  	}
  
  	return hw_data->hws[idx];
  }
  EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
766e6a4ec   Grant Likely   clk: add DT clock...
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
  /**
   * of_clk_add_provider() - Register a clock provider for a node
   * @np: Device node pointer associated with clock provider
   * @clk_src_get: callback for decoding clock
   * @data: context pointer for @clk_src_get callback.
   */
  int of_clk_add_provider(struct device_node *np,
  			struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  						   void *data),
  			void *data)
  {
  	struct of_clk_provider *cp;
86be408bf   Sylwester Nawrocki   clk: Support for ...
2975
  	int ret;
766e6a4ec   Grant Likely   clk: add DT clock...
2976
2977
2978
2979
2980
2981
2982
2983
  
  	cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
  	if (!cp)
  		return -ENOMEM;
  
  	cp->node = of_node_get(np);
  	cp->data = data;
  	cp->get = clk_src_get;
d6782c263   Sylwester Nawrocki   clk: Provide not ...
2984
  	mutex_lock(&of_clk_mutex);
766e6a4ec   Grant Likely   clk: add DT clock...
2985
  	list_add(&cp->link, &of_clk_providers);
d6782c263   Sylwester Nawrocki   clk: Provide not ...
2986
  	mutex_unlock(&of_clk_mutex);
766e6a4ec   Grant Likely   clk: add DT clock...
2987
2988
  	pr_debug("Added clock from %s
  ", np->full_name);
86be408bf   Sylwester Nawrocki   clk: Support for ...
2989
2990
2991
2992
2993
  	ret = of_clk_set_defaults(np, true);
  	if (ret < 0)
  		of_clk_del_provider(np);
  
  	return ret;
766e6a4ec   Grant Likely   clk: add DT clock...
2994
2995
2996
2997
  }
  EXPORT_SYMBOL_GPL(of_clk_add_provider);
  
  /**
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
   * of_clk_add_hw_provider() - Register a clock provider for a node
   * @np: Device node pointer associated with clock provider
   * @get: callback for decoding clk_hw
   * @data: context pointer for @get callback.
   */
  int of_clk_add_hw_provider(struct device_node *np,
  			   struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  						 void *data),
  			   void *data)
  {
  	struct of_clk_provider *cp;
  	int ret;
  
  	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  	if (!cp)
  		return -ENOMEM;
  
  	cp->node = of_node_get(np);
  	cp->data = data;
  	cp->get_hw = get;
  
  	mutex_lock(&of_clk_mutex);
  	list_add(&cp->link, &of_clk_providers);
  	mutex_unlock(&of_clk_mutex);
  	pr_debug("Added clk_hw provider from %s
  ", np->full_name);
  
  	ret = of_clk_set_defaults(np, true);
  	if (ret < 0)
  		of_clk_del_provider(np);
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
  
  /**
766e6a4ec   Grant Likely   clk: add DT clock...
3034
3035
3036
3037
3038
3039
   * of_clk_del_provider() - Remove a previously registered clock provider
   * @np: Device node pointer associated with clock provider
   */
  void of_clk_del_provider(struct device_node *np)
  {
  	struct of_clk_provider *cp;
d6782c263   Sylwester Nawrocki   clk: Provide not ...
3040
  	mutex_lock(&of_clk_mutex);
766e6a4ec   Grant Likely   clk: add DT clock...
3041
3042
3043
3044
3045
3046
3047
3048
  	list_for_each_entry(cp, &of_clk_providers, link) {
  		if (cp->node == np) {
  			list_del(&cp->link);
  			of_node_put(cp->node);
  			kfree(cp);
  			break;
  		}
  	}
d6782c263   Sylwester Nawrocki   clk: Provide not ...
3049
  	mutex_unlock(&of_clk_mutex);
766e6a4ec   Grant Likely   clk: add DT clock...
3050
3051
  }
  EXPORT_SYMBOL_GPL(of_clk_del_provider);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
3052
3053
3054
3055
3056
  static struct clk_hw *
  __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
  			      struct of_phandle_args *clkspec)
  {
  	struct clk *clk;
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
3057

74002fcde   Stephen Boyd   clk: Simplify __o...
3058
3059
  	if (provider->get_hw)
  		return provider->get_hw(clkspec, provider->data);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
3060

74002fcde   Stephen Boyd   clk: Simplify __o...
3061
3062
3063
3064
  	clk = provider->get(clkspec, provider->data);
  	if (IS_ERR(clk))
  		return ERR_CAST(clk);
  	return __clk_get_hw(clk);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
3065
  }
73e0e496a   Stephen Boyd   clkdev: Always al...
3066
3067
  struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
  				       const char *dev_id, const char *con_id)
766e6a4ec   Grant Likely   clk: add DT clock...
3068
3069
  {
  	struct of_clk_provider *provider;
a34cd4666   Jean-Francois Moine   clk: return probe...
3070
  	struct clk *clk = ERR_PTR(-EPROBE_DEFER);
f155d15b6   Stephen Boyd   clk: Return error...
3071
  	struct clk_hw *hw;
766e6a4ec   Grant Likely   clk: add DT clock...
3072

306c342f9   Stephen Boyd   clk: Replace of_c...
3073
3074
  	if (!clkspec)
  		return ERR_PTR(-EINVAL);
766e6a4ec   Grant Likely   clk: add DT clock...
3075
  	/* Check if we have such a provider in our array */
306c342f9   Stephen Boyd   clk: Replace of_c...
3076
  	mutex_lock(&of_clk_mutex);
766e6a4ec   Grant Likely   clk: add DT clock...
3077
  	list_for_each_entry(provider, &of_clk_providers, link) {
f155d15b6   Stephen Boyd   clk: Return error...
3078
  		if (provider->node == clkspec->np) {
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
3079
  			hw = __of_clk_get_hw_from_provider(provider, clkspec);
0861e5b8c   Stephen Boyd   clk: Add clk_hw O...
3080
  			clk = __clk_create_clk(hw, dev_id, con_id);
f155d15b6   Stephen Boyd   clk: Return error...
3081
  		}
73e0e496a   Stephen Boyd   clkdev: Always al...
3082

f155d15b6   Stephen Boyd   clk: Return error...
3083
3084
  		if (!IS_ERR(clk)) {
  			if (!__clk_get(clk)) {
73e0e496a   Stephen Boyd   clkdev: Always al...
3085
3086
3087
  				__clk_free_clk(clk);
  				clk = ERR_PTR(-ENOENT);
  			}
766e6a4ec   Grant Likely   clk: add DT clock...
3088
  			break;
73e0e496a   Stephen Boyd   clkdev: Always al...
3089
  		}
766e6a4ec   Grant Likely   clk: add DT clock...
3090
  	}
306c342f9   Stephen Boyd   clk: Replace of_c...
3091
  	mutex_unlock(&of_clk_mutex);
d6782c263   Sylwester Nawrocki   clk: Provide not ...
3092
3093
3094
  
  	return clk;
  }
306c342f9   Stephen Boyd   clk: Replace of_c...
3095
3096
3097
3098
3099
3100
3101
3102
  /**
   * of_clk_get_from_provider() - Lookup a clock from a clock provider
   * @clkspec: pointer to a clock specifier data structure
   *
   * This function looks up a struct clk from the registered list of clock
   * providers, an input is a clock specifier data structure as returned
   * from the of_parse_phandle_with_args() function call.
   */
d6782c263   Sylwester Nawrocki   clk: Provide not ...
3103
3104
  struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  {
306c342f9   Stephen Boyd   clk: Replace of_c...
3105
  	return __of_clk_get_from_provider(clkspec, NULL, __func__);
766e6a4ec   Grant Likely   clk: add DT clock...
3106
  }
fb4dd2220   Andrew F. Davis   clk: Make of_clk_...
3107
  EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
766e6a4ec   Grant Likely   clk: add DT clock...
3108

929e7f3bc   Stephen Boyd   clk: Make of_clk_...
3109
3110
3111
3112
3113
3114
3115
  /**
   * of_clk_get_parent_count() - Count the number of clocks a device node has
   * @np: device node to count
   *
   * Returns: The number of clocks that are possible parents of this node
   */
  unsigned int of_clk_get_parent_count(struct device_node *np)
f61027426   Mike Turquette   clk: of: helper f...
3116
  {
929e7f3bc   Stephen Boyd   clk: Make of_clk_...
3117
3118
3119
3120
3121
3122
3123
  	int count;
  
  	count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  	if (count < 0)
  		return 0;
  
  	return count;
f61027426   Mike Turquette   clk: of: helper f...
3124
3125
  }
  EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
766e6a4ec   Grant Likely   clk: add DT clock...
3126
3127
3128
  const char *of_clk_get_parent_name(struct device_node *np, int index)
  {
  	struct of_phandle_args clkspec;
7a0fc1a3d   Ben Dooks   clk: add clock-in...
3129
  	struct property *prop;
766e6a4ec   Grant Likely   clk: add DT clock...
3130
  	const char *clk_name;
7a0fc1a3d   Ben Dooks   clk: add clock-in...
3131
3132
  	const __be32 *vp;
  	u32 pv;
766e6a4ec   Grant Likely   clk: add DT clock...
3133
  	int rc;
7a0fc1a3d   Ben Dooks   clk: add clock-in...
3134
  	int count;
0a4807c2f   Stephen Boyd   clk: Make of_clk_...
3135
  	struct clk *clk;
766e6a4ec   Grant Likely   clk: add DT clock...
3136

766e6a4ec   Grant Likely   clk: add DT clock...
3137
3138
3139
3140
  	rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  					&clkspec);
  	if (rc)
  		return NULL;
7a0fc1a3d   Ben Dooks   clk: add clock-in...
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
  	index = clkspec.args_count ? clkspec.args[0] : 0;
  	count = 0;
  
  	/* if there is an indices property, use it to transfer the index
  	 * specified into an array offset for the clock-output-names property.
  	 */
  	of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
  		if (index == pv) {
  			index = count;
  			break;
  		}
  		count++;
  	}
8da411cc1   Masahiro Yamada   clk: let of_clk_g...
3154
3155
3156
  	/* We went off the end of 'clock-indices' without finding it */
  	if (prop && !vp)
  		return NULL;
7a0fc1a3d   Ben Dooks   clk: add clock-in...
3157

766e6a4ec   Grant Likely   clk: add DT clock...
3158
  	if (of_property_read_string_index(clkspec.np, "clock-output-names",
7a0fc1a3d   Ben Dooks   clk: add clock-in...
3159
  					  index,
0a4807c2f   Stephen Boyd   clk: Make of_clk_...
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
  					  &clk_name) < 0) {
  		/*
  		 * Best effort to get the name if the clock has been
  		 * registered with the framework. If the clock isn't
  		 * registered, we return the node name as the name of
  		 * the clock as long as #clock-cells = 0.
  		 */
  		clk = of_clk_get_from_provider(&clkspec);
  		if (IS_ERR(clk)) {
  			if (clkspec.args_count == 0)
  				clk_name = clkspec.np->name;
  			else
  				clk_name = NULL;
  		} else {
  			clk_name = __clk_get_name(clk);
  			clk_put(clk);
  		}
  	}
766e6a4ec   Grant Likely   clk: add DT clock...
3178
3179
3180
3181
3182
  
  	of_node_put(clkspec.np);
  	return clk_name;
  }
  EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
2e61dfb36   Dinh Nguyen   clk: of: helper f...
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
  /**
   * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
   * number of parents
   * @np: Device node pointer associated with clock provider
   * @parents: pointer to char array that hold the parents' names
   * @size: size of the @parents array
   *
   * Return: number of parents for the clock node.
   */
  int of_clk_parent_fill(struct device_node *np, const char **parents,
  		       unsigned int size)
  {
  	unsigned int i = 0;
  
  	while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
  		i++;
  
  	return i;
  }
  EXPORT_SYMBOL_GPL(of_clk_parent_fill);
1771b10d6   Gregory CLEMENT   clk: respect the ...
3203
3204
3205
3206
3207
  struct clock_provider {
  	of_clk_init_cb_t clk_init_cb;
  	struct device_node *np;
  	struct list_head node;
  };
1771b10d6   Gregory CLEMENT   clk: respect the ...
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
  /*
   * This function looks for a parent clock. If there is one, then it
   * checks that the provider for this parent clock was initialized, in
   * this case the parent clock will be ready.
   */
  static int parent_ready(struct device_node *np)
  {
  	int i = 0;
  
  	while (true) {
  		struct clk *clk = of_clk_get(np, i);
  
  		/* this parent is ready we can check the next one */
  		if (!IS_ERR(clk)) {
  			clk_put(clk);
  			i++;
  			continue;
  		}
  
  		/* at least one parent is not ready, we exit now */
  		if (PTR_ERR(clk) == -EPROBE_DEFER)
  			return 0;
  
  		/*
  		 * Here we make assumption that the device tree is
  		 * written correctly. So an error means that there is
  		 * no more parent. As we didn't exit yet, then the
  		 * previous parent are ready. If there is no clock
  		 * parent, no need to wait for them, then we can
  		 * consider their absence as being ready
  		 */
  		return 1;
  	}
  }
766e6a4ec   Grant Likely   clk: add DT clock...
3242
  /**
d56f8994b   Lee Jones   clk: Provide OF h...
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
   * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
   * @np: Device node pointer associated with clock provider
   * @index: clock index
   * @flags: pointer to clk_core->flags
   *
   * Detects if the clock-critical property exists and, if so, sets the
   * corresponding CLK_IS_CRITICAL flag.
   *
   * Do not use this function. It exists only for legacy Device Tree
   * bindings, such as the one-clock-per-node style that are outdated.
   * Those bindings typically put all clock data into .dts and the Linux
   * driver has no clock data, thus making it impossible to set this flag
   * correctly from the driver. Only those drivers may call
   * of_clk_detect_critical from their setup functions.
   *
   * Return: error code or zero on success
   */
  int of_clk_detect_critical(struct device_node *np,
  					  int index, unsigned long *flags)
  {
  	struct property *prop;
  	const __be32 *cur;
  	uint32_t idx;
  
  	if (!np || !flags)
  		return -EINVAL;
  
  	of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
  		if (index == idx)
  			*flags |= CLK_IS_CRITICAL;
  
  	return 0;
  }
  
  /**
766e6a4ec   Grant Likely   clk: add DT clock...
3278
3279
3280
   * of_clk_init() - Scan and init clock providers from the DT
   * @matches: array of compatible values and init functions for providers.
   *
1771b10d6   Gregory CLEMENT   clk: respect the ...
3281
   * This function scans the device tree for matching clock providers
e5ca8fb4c   Sylwester Nawrocki   clk: Fix minor er...
3282
   * and calls their initialization functions. It also does it by trying
1771b10d6   Gregory CLEMENT   clk: respect the ...
3283
   * to follow the dependencies.
766e6a4ec   Grant Likely   clk: add DT clock...
3284
3285
3286
   */
  void __init of_clk_init(const struct of_device_id *matches)
  {
7f7ed584d   Alex Elder   clk: get matching...
3287
  	const struct of_device_id *match;
766e6a4ec   Grant Likely   clk: add DT clock...
3288
  	struct device_node *np;
1771b10d6   Gregory CLEMENT   clk: respect the ...
3289
3290
3291
  	struct clock_provider *clk_provider, *next;
  	bool is_init_done;
  	bool force = false;
2573a02aa   Stephen Boyd   clk: Move clk_pro...
3292
  	LIST_HEAD(clk_provider_list);
766e6a4ec   Grant Likely   clk: add DT clock...
3293

f2f6c2556   Prashant Gaikwad   clk: add common o...
3294
  	if (!matches)
819b4861c   Tero Kristo   CLK: ti: add init...
3295
  		matches = &__clk_of_table;
f2f6c2556   Prashant Gaikwad   clk: add common o...
3296

1771b10d6   Gregory CLEMENT   clk: respect the ...
3297
  	/* First prepare the list of the clocks providers */
7f7ed584d   Alex Elder   clk: get matching...
3298
  	for_each_matching_node_and_match(np, matches, &match) {
2e3b19f13   Stephen Boyd   clk: Check for al...
3299
  		struct clock_provider *parent;
3e5dd6f6e   Geert Uytterhoeven   clk: Ignore disab...
3300
3301
  		if (!of_device_is_available(np))
  			continue;
2e3b19f13   Stephen Boyd   clk: Check for al...
3302
3303
3304
3305
3306
  		parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  		if (!parent) {
  			list_for_each_entry_safe(clk_provider, next,
  						 &clk_provider_list, node) {
  				list_del(&clk_provider->node);
6bc9d9d62   Julia Lawall   clk: add missing ...
3307
  				of_node_put(clk_provider->np);
2e3b19f13   Stephen Boyd   clk: Check for al...
3308
3309
  				kfree(clk_provider);
  			}
6bc9d9d62   Julia Lawall   clk: add missing ...
3310
  			of_node_put(np);
2e3b19f13   Stephen Boyd   clk: Check for al...
3311
3312
  			return;
  		}
1771b10d6   Gregory CLEMENT   clk: respect the ...
3313
3314
  
  		parent->clk_init_cb = match->data;
6bc9d9d62   Julia Lawall   clk: add missing ...
3315
  		parent->np = of_node_get(np);
3f6d439f2   Sylwester Nawrocki   clk: reverse defa...
3316
  		list_add_tail(&parent->node, &clk_provider_list);
1771b10d6   Gregory CLEMENT   clk: respect the ...
3317
3318
3319
3320
3321
3322
3323
  	}
  
  	while (!list_empty(&clk_provider_list)) {
  		is_init_done = false;
  		list_for_each_entry_safe(clk_provider, next,
  					&clk_provider_list, node) {
  			if (force || parent_ready(clk_provider->np)) {
86be408bf   Sylwester Nawrocki   clk: Support for ...
3324

989eafd0b   Ricardo Ribalda Delgado   clk: core: Avoid ...
3325
3326
3327
  				/* Don't populate platform devices */
  				of_node_set_flag(clk_provider->np,
  						 OF_POPULATED);
1771b10d6   Gregory CLEMENT   clk: respect the ...
3328
  				clk_provider->clk_init_cb(clk_provider->np);
86be408bf   Sylwester Nawrocki   clk: Support for ...
3329
  				of_clk_set_defaults(clk_provider->np, true);
1771b10d6   Gregory CLEMENT   clk: respect the ...
3330
  				list_del(&clk_provider->node);
6bc9d9d62   Julia Lawall   clk: add missing ...
3331
  				of_node_put(clk_provider->np);
1771b10d6   Gregory CLEMENT   clk: respect the ...
3332
3333
3334
3335
3336
3337
  				kfree(clk_provider);
  				is_init_done = true;
  			}
  		}
  
  		/*
e5ca8fb4c   Sylwester Nawrocki   clk: Fix minor er...
3338
  		 * We didn't manage to initialize any of the
1771b10d6   Gregory CLEMENT   clk: respect the ...
3339
3340
3341
3342
3343
3344
  		 * remaining providers during the last loop, so now we
  		 * initialize all the remaining ones unconditionally
  		 * in case the clock parent was not mandatory
  		 */
  		if (!is_init_done)
  			force = true;
766e6a4ec   Grant Likely   clk: add DT clock...
3345
3346
3347
  	}
  }
  #endif