Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * TI clock support
  4 *
  5 * Copyright (C) 2013 Texas Instruments, Inc.
  6 *
  7 * Tero Kristo <t-kristo@ti.com>
  8 */
  9
 10#include <linux/clk.h>
 11#include <linux/clk-provider.h>
 12#include <linux/clkdev.h>
 13#include <linux/clk/ti.h>
 14#include <linux/io.h>
 15#include <linux/of.h>
 16#include <linux/of_address.h>
 17#include <linux/list.h>
 18#include <linux/regmap.h>
 19#include <linux/string_helpers.h>
 20#include <linux/memblock.h>
 21#include <linux/device.h>
 22
 23#include "clock.h"
 24
 25#undef pr_fmt
 26#define pr_fmt(fmt) "%s: " fmt, __func__
 27
 28static LIST_HEAD(clk_hw_omap_clocks);
 29struct ti_clk_ll_ops *ti_clk_ll_ops;
 30static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
 31
 32struct ti_clk_features ti_clk_features;
 33
 34struct clk_iomap {
 35	struct regmap *regmap;
 36	void __iomem *mem;
 37};
 38
 39static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
 40
 41static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
 42{
 43	struct clk_iomap *io = clk_memmaps[reg->index];
 44
 45	if (reg->ptr)
 46		writel_relaxed(val, reg->ptr);
 47	else if (io->regmap)
 48		regmap_write(io->regmap, reg->offset, val);
 49	else
 50		writel_relaxed(val, io->mem + reg->offset);
 51}
 52
 53static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
 54{
 55	u32 v;
 56
 57	v = readl_relaxed(ptr);
 58	v &= ~mask;
 59	v |= val;
 60	writel_relaxed(v, ptr);
 61}
 62
 63static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
 64{
 65	struct clk_iomap *io = clk_memmaps[reg->index];
 66
 67	if (reg->ptr) {
 68		_clk_rmw(val, mask, reg->ptr);
 69	} else if (io->regmap) {
 70		regmap_update_bits(io->regmap, reg->offset, mask, val);
 71	} else {
 72		_clk_rmw(val, mask, io->mem + reg->offset);
 73	}
 74}
 75
 76static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
 77{
 78	u32 val;
 79	struct clk_iomap *io = clk_memmaps[reg->index];
 80
 81	if (reg->ptr)
 82		val = readl_relaxed(reg->ptr);
 83	else if (io->regmap)
 84		regmap_read(io->regmap, reg->offset, &val);
 85	else
 86		val = readl_relaxed(io->mem + reg->offset);
 87
 88	return val;
 89}
 90
 91/**
 92 * ti_clk_setup_ll_ops - setup low level clock operations
 93 * @ops: low level clock ops descriptor
 94 *
 95 * Sets up low level clock operations for TI clock driver. This is used
 96 * to provide various callbacks for the clock driver towards platform
 97 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
 98 * registered already.
 99 */
100int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
101{
102	if (ti_clk_ll_ops) {
103		pr_err("Attempt to register ll_ops multiple times.\n");
104		return -EBUSY;
105	}
106
107	ti_clk_ll_ops = ops;
108	ops->clk_readl = clk_memmap_readl;
109	ops->clk_writel = clk_memmap_writel;
110	ops->clk_rmw = clk_memmap_rmw;
111
112	return 0;
113}
114
115/*
116 * Eventually we could standardize to using '_' for clk-*.c files to follow the
117 * TRM naming and leave out the tmp name here.
118 */
119static struct device_node *ti_find_clock_provider(struct device_node *from,
120						  const char *name)
121{
122	struct device_node *np;
123	bool found = false;
124	const char *n;
125	char *tmp;
126
127	tmp = kstrdup_and_replace(name, '-', '_', GFP_KERNEL);
128	if (!tmp)
129		return NULL;
130
131	/* Node named "clock" with "clock-output-names" */
132	for_each_of_allnodes_from(from, np) {
133		if (of_property_read_string_index(np, "clock-output-names",
134						  0, &n))
135			continue;
136
137		if (!strncmp(n, tmp, strlen(tmp))) {
138			of_node_get(np);
139			found = true;
140			break;
141		}
142	}
143	kfree(tmp);
144
145	if (found) {
146		of_node_put(from);
147		return np;
148	}
149
150	/* Fall back to using old node name base provider name */
151	return of_find_node_by_name(from, name);
152}
153
154/**
155 * ti_dt_clocks_register - register DT alias clocks during boot
156 * @oclks: list of clocks to register
157 *
158 * Register alias or non-standard DT clock entries during boot. By
159 * default, DT clocks are found based on their clock-output-names
160 * property, or the clock node name for legacy cases. If any
161 * additional con-id / dev-id -> clock mapping is required, use this
162 * function to list these.
163 */
164void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
165{
166	struct ti_dt_clk *c;
167	struct device_node *node, *parent, *child;
168	struct clk *clk;
169	struct of_phandle_args clkspec;
170	char buf[64];
171	char *ptr;
172	char *tags[2];
173	int i;
174	int num_args;
175	int ret;
176	static bool clkctrl_nodes_missing;
177	static bool has_clkctrl_data;
178	static bool compat_mode;
179
180	compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
181
182	for (c = oclks; c->node_name != NULL; c++) {
183		strcpy(buf, c->node_name);
184		ptr = buf;
185		for (i = 0; i < 2; i++)
186			tags[i] = NULL;
187		num_args = 0;
188		while (*ptr) {
189			if (*ptr == ':') {
190				if (num_args >= 2) {
191					pr_warn("Bad number of tags on %s\n",
192						c->node_name);
193					return;
194				}
195				tags[num_args++] = ptr + 1;
196				*ptr = 0;
197			}
198			ptr++;
199		}
200
201		if (num_args && clkctrl_nodes_missing)
202			continue;
203
204		node = ti_find_clock_provider(NULL, buf);
205		if (num_args && compat_mode) {
206			parent = node;
207			child = of_get_child_by_name(parent, "clock");
208			if (!child)
209				child = of_get_child_by_name(parent, "clk");
210			if (child) {
211				of_node_put(parent);
212				node = child;
213			}
214		}
215
216		clkspec.np = node;
217		clkspec.args_count = num_args;
218		for (i = 0; i < num_args; i++) {
219			ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
220			if (ret) {
221				pr_warn("Bad tag in %s at %d: %s\n",
222					c->node_name, i, tags[i]);
223				of_node_put(node);
224				return;
225			}
226		}
227		clk = of_clk_get_from_provider(&clkspec);
228		of_node_put(node);
229		if (!IS_ERR(clk)) {
230			c->lk.clk = clk;
231			clkdev_add(&c->lk);
232		} else {
233			if (num_args && !has_clkctrl_data) {
234				struct device_node *np;
235
236				np = of_find_compatible_node(NULL, NULL,
237							     "ti,clkctrl");
238				if (np) {
239					has_clkctrl_data = true;
240					of_node_put(np);
241				} else {
242					clkctrl_nodes_missing = true;
243
244					pr_warn("missing clkctrl nodes, please update your dts.\n");
245					continue;
246				}
247			}
248
249			pr_warn("failed to lookup clock node %s, ret=%ld\n",
250				c->node_name, PTR_ERR(clk));
251		}
252	}
253}
254
255struct clk_init_item {
256	struct device_node *node;
257	void *user;
258	ti_of_clk_init_cb_t func;
259	struct list_head link;
260};
261
262static LIST_HEAD(retry_list);
263
264/**
265 * ti_clk_retry_init - retries a failed clock init at later phase
266 * @node: device node for the clock
267 * @user: user data pointer
268 * @func: init function to be called for the clock
269 *
270 * Adds a failed clock init to the retry list. The retry list is parsed
271 * once all the other clocks have been initialized.
272 */
273int __init ti_clk_retry_init(struct device_node *node, void *user,
274			     ti_of_clk_init_cb_t func)
275{
276	struct clk_init_item *retry;
277
278	pr_debug("%pOFn: adding to retry list...\n", node);
279	retry = kzalloc(sizeof(*retry), GFP_KERNEL);
280	if (!retry)
281		return -ENOMEM;
282
283	retry->node = node;
284	retry->func = func;
285	retry->user = user;
286	list_add(&retry->link, &retry_list);
287
288	return 0;
289}
290
291/**
292 * ti_clk_get_reg_addr - get register address for a clock register
293 * @node: device node for the clock
294 * @index: register index from the clock node
295 * @reg: pointer to target register struct
296 *
297 * Builds clock register address from device tree information, and returns
298 * the data via the provided output pointer @reg. Returns 0 on success,
299 * negative error value on failure.
300 */
301int ti_clk_get_reg_addr(struct device_node *node, int index,
302			struct clk_omap_reg *reg)
303{
304	u32 val;
305	int i;
306
307	for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
308		if (clocks_node_ptr[i] == node->parent)
309			break;
310		if (clocks_node_ptr[i] == node->parent->parent)
311			break;
312	}
313
314	if (i == CLK_MAX_MEMMAPS) {
315		pr_err("clk-provider not found for %pOFn!\n", node);
316		return -ENOENT;
317	}
318
319	reg->index = i;
320
321	if (of_property_read_u32_index(node, "reg", index, &val)) {
322		if (of_property_read_u32_index(node->parent, "reg",
323					       index, &val)) {
324			pr_err("%pOFn or parent must have reg[%d]!\n",
325			       node, index);
326			return -EINVAL;
327		}
328	}
329
330	reg->offset = val;
331	reg->ptr = NULL;
332
333	return 0;
334}
335
336void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
337{
338	u32 latch;
339
340	if (shift < 0)
341		return;
342
343	latch = 1 << shift;
344
345	ti_clk_ll_ops->clk_rmw(latch, latch, reg);
346	ti_clk_ll_ops->clk_rmw(0, latch, reg);
347	ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
348}
349
350/**
351 * omap2_clk_provider_init - init master clock provider
352 * @parent: master node
353 * @index: internal index for clk_reg_ops
354 * @syscon: syscon regmap pointer for accessing clock registers
355 * @mem: iomem pointer for the clock provider memory area, only used if
356 *       syscon is not provided
357 *
358 * Initializes a master clock IP block. This basically sets up the
359 * mapping from clocks node to the memory map index. All the clocks
360 * are then initialized through the common of_clk_init call, and the
361 * clocks will access their memory maps based on the node layout.
362 * Returns 0 in success.
363 */
364int __init omap2_clk_provider_init(struct device_node *parent, int index,
365				   struct regmap *syscon, void __iomem *mem)
366{
367	struct device_node *clocks;
368	struct clk_iomap *io;
369
370	/* get clocks for this parent */
371	clocks = of_get_child_by_name(parent, "clocks");
372	if (!clocks) {
373		pr_err("%pOFn missing 'clocks' child node.\n", parent);
374		return -EINVAL;
375	}
376
377	/* add clocks node info */
378	clocks_node_ptr[index] = clocks;
379
380	io = kzalloc(sizeof(*io), GFP_KERNEL);
381	if (!io)
382		return -ENOMEM;
383
384	io->regmap = syscon;
385	io->mem = mem;
386
387	clk_memmaps[index] = io;
388
389	return 0;
390}
391
392/**
393 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
394 * @index: index for the clock provider
395 * @mem: iomem pointer for the clock provider memory area
396 *
397 * Initializes a legacy clock provider memory mapping.
398 */
399void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
400{
401	struct clk_iomap *io;
402
403	io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
404	if (!io)
405		panic("%s: Failed to allocate %zu bytes\n", __func__,
406		      sizeof(*io));
407
408	io->mem = mem;
409
410	clk_memmaps[index] = io;
411}
412
413/**
414 * ti_dt_clk_init_retry_clks - init clocks from the retry list
415 *
416 * Initializes any clocks that have failed to initialize before,
417 * reasons being missing parent node(s) during earlier init. This
418 * typically happens only for DPLLs which need to have both of their
419 * parent clocks ready during init.
420 */
421void ti_dt_clk_init_retry_clks(void)
422{
423	struct clk_init_item *retry;
424	struct clk_init_item *tmp;
425	int retries = 5;
426
427	while (!list_empty(&retry_list) && retries) {
428		list_for_each_entry_safe(retry, tmp, &retry_list, link) {
429			pr_debug("retry-init: %pOFn\n", retry->node);
430			retry->func(retry->user, retry->node);
431			list_del(&retry->link);
432			kfree(retry);
433		}
434		retries--;
435	}
436}
437
438static const struct of_device_id simple_clk_match_table[] __initconst = {
439	{ .compatible = "fixed-clock" },
440	{ .compatible = "fixed-factor-clock" },
441	{ }
442};
443
444/**
445 * ti_dt_clk_name - init clock name from first output name or node name
446 * @np: device node
447 *
448 * Use the first clock-output-name for the clock name if found. Fall back
449 * to legacy naming based on node name.
450 */
451const char *ti_dt_clk_name(struct device_node *np)
452{
453	const char *name;
454
455	if (!of_property_read_string_index(np, "clock-output-names", 0,
456					   &name))
457		return name;
458
459	return np->name;
460}
461
462/**
463 * ti_clk_add_aliases - setup clock aliases
464 *
465 * Sets up any missing clock aliases. No return value.
466 */
467void __init ti_clk_add_aliases(void)
468{
469	struct device_node *np;
470	struct clk *clk;
471
472	for_each_matching_node(np, simple_clk_match_table) {
473		struct of_phandle_args clkspec;
474
475		clkspec.np = np;
476		clk = of_clk_get_from_provider(&clkspec);
477
478		ti_clk_add_alias(clk, ti_dt_clk_name(np));
479	}
480}
481
482/**
483 * ti_clk_setup_features - setup clock features flags
484 * @features: features definition to use
485 *
486 * Initializes the clock driver features flags based on platform
487 * provided data. No return value.
488 */
489void __init ti_clk_setup_features(struct ti_clk_features *features)
490{
491	memcpy(&ti_clk_features, features, sizeof(*features));
492}
493
494/**
495 * ti_clk_get_features - get clock driver features flags
496 *
497 * Get TI clock driver features description. Returns a pointer
498 * to the current feature setup.
499 */
500const struct ti_clk_features *ti_clk_get_features(void)
501{
502	return &ti_clk_features;
503}
504
505/**
506 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
507 * @clk_names: ptr to an array of strings of clock names to enable
508 * @num_clocks: number of clock names in @clk_names
509 *
510 * Prepare and enable a list of clocks, named by @clk_names.  No
511 * return value. XXX Deprecated; only needed until these clocks are
512 * properly claimed and enabled by the drivers or core code that uses
513 * them.  XXX What code disables & calls clk_put on these clocks?
514 */
515void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
516{
517	struct clk *init_clk;
518	int i;
519
520	for (i = 0; i < num_clocks; i++) {
521		init_clk = clk_get(NULL, clk_names[i]);
522		if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
523			 clk_names[i]))
524			continue;
525		clk_prepare_enable(init_clk);
526	}
527}
528
529/**
530 * ti_clk_add_alias - add a clock alias for a TI clock
531 * @clk: clock handle to create alias for
532 * @con: connection ID for this clock
533 *
534 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
535 * and assigns the data to it. Returns 0 if successful, negative error
536 * value otherwise.
537 */
538int ti_clk_add_alias(struct clk *clk, const char *con)
539{
540	struct clk_lookup *cl;
541
542	if (!clk)
543		return 0;
544
545	if (IS_ERR(clk))
546		return PTR_ERR(clk);
547
548	cl = kzalloc(sizeof(*cl), GFP_KERNEL);
549	if (!cl)
550		return -ENOMEM;
551
552	cl->con_id = con;
553	cl->clk = clk;
554
555	clkdev_add(cl);
556
557	return 0;
558}
559
560/**
561 * of_ti_clk_register - register a TI clock to the common clock framework
562 * @node: device node for this clock
563 * @hw: hardware clock handle
564 * @con: connection ID for this clock
565 *
566 * Registers a TI clock to the common clock framework, and adds a clock
567 * alias for it. Returns a handle to the registered clock if successful,
568 * ERR_PTR value in failure.
569 */
570struct clk *of_ti_clk_register(struct device_node *node, struct clk_hw *hw,
571			       const char *con)
572{
573	struct clk *clk;
574	int ret;
575
576	ret = of_clk_hw_register(node, hw);
577	if (ret)
578		return ERR_PTR(ret);
579
580	clk = hw->clk;
581	ret = ti_clk_add_alias(clk, con);
582	if (ret) {
583		clk_unregister(clk);
584		return ERR_PTR(ret);
585	}
586
587	return clk;
588}
589
590/**
591 * of_ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
592 * @node: device node for this clock
593 * @hw: hardware clock handle
594 * @con: connection ID for this clock
595 *
596 * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
597 * for it, and adds the list to the available clk_hw_omap type clocks.
598 * Returns a handle to the registered clock if successful, ERR_PTR value
599 * in failure.
600 */
601struct clk *of_ti_clk_register_omap_hw(struct device_node *node,
602				       struct clk_hw *hw, const char *con)
603{
604	struct clk *clk;
605	struct clk_hw_omap *oclk;
606
607	clk = of_ti_clk_register(node, hw, con);
608	if (IS_ERR(clk))
609		return clk;
610
611	oclk = to_clk_hw_omap(hw);
612
613	list_add(&oclk->node, &clk_hw_omap_clocks);
614
615	return clk;
616}
617
618/**
619 * omap2_clk_for_each - call function for each registered clk_hw_omap
620 * @fn: pointer to a callback function
621 *
622 * Call @fn for each registered clk_hw_omap, passing @hw to each
623 * function.  @fn must return 0 for success or any other value for
624 * failure.  If @fn returns non-zero, the iteration across clocks
625 * will stop and the non-zero return value will be passed to the
626 * caller of omap2_clk_for_each().
627 */
628int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
629{
630	int ret;
631	struct clk_hw_omap *hw;
632
633	list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
634		ret = (*fn)(hw);
635		if (ret)
636			break;
637	}
638
639	return ret;
640}
641
642/**
643 * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
644 * @hw: clk_hw to check if it is an omap clock or not
645 *
646 * Checks if the provided clk_hw is OMAP clock or not. Returns true if
647 * it is, false otherwise.
648 */
649bool omap2_clk_is_hw_omap(struct clk_hw *hw)
650{
651	struct clk_hw_omap *oclk;
652
653	list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
654		if (&oclk->hw == hw)
655			return true;
656	}
657
658	return false;
659}