clkgen-mux.c 2.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * clkgen-mux.c: ST GEN-MUX Clock driver
 *
 * Copyright (C) 2014 STMicroelectronics (R&D) Limited
 *
 * Authors: Stephen Gallimore <stephen.gallimore@st.com>
 *	    Pankaj Dev <pankaj.dev@st.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include <linux/slab.h>
#include <linux/of_address.h>
Stephen Boyd's avatar
Stephen Boyd committed
18
#include <linux/clk.h>
19
#include <linux/clk-provider.h>
20
#include "clkgen.h"
21
22
23
24
25

static const char ** __init clkgen_mux_get_parents(struct device_node *np,
						       int *num_parents)
{
	const char **parents;
26
	unsigned int nparents;
27

28
	nparents = of_clk_get_parent_count(np);
29
	if (WARN_ON(!nparents))
30
31
		return ERR_PTR(-EINVAL);

32
	parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
33
34
35
	if (!parents)
		return ERR_PTR(-ENOMEM);

36
	*num_parents = of_clk_parent_fill(np, parents, nparents);
37
38
39
	return parents;
}

40
41
42
43
44
45
46
47
48
struct clkgen_mux_data {
	u32 offset;
	u8 shift;
	u8 width;
	spinlock_t *lock;
	unsigned long clk_flags;
	u8 mux_flags;
};

49
50
static struct clkgen_mux_data stih407_a9_mux_data = {
	.offset = 0x1a4,
51
	.shift = 0,
52
	.width = 2,
53
	.lock = &clkgen_a9_lock,
54
};
55

56
57
static void __init st_of_clkgen_mux_setup(struct device_node *np,
		struct clkgen_mux_data *data)
58
59
60
61
{
	struct clk *clk;
	void __iomem *reg;
	const char **parents;
62
	int num_parents = 0;
63
64
65
66
67
68
69
70
71
72
73

	reg = of_iomap(np, 0);
	if (!reg) {
		pr_err("%s: Failed to get base address\n", __func__);
		return;
	}

	parents = clkgen_mux_get_parents(np, &num_parents);
	if (IS_ERR(parents)) {
		pr_err("%s: Failed to get parents (%ld)\n",
				__func__, PTR_ERR(parents));
74
		goto err_parents;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	}

	clk = clk_register_mux(NULL, np->name, parents, num_parents,
				data->clk_flags | CLK_SET_RATE_PARENT,
				reg + data->offset,
				data->shift, data->width, data->mux_flags,
				data->lock);
	if (IS_ERR(clk))
		goto err;

	pr_debug("%s: parent %s rate %u\n",
			__clk_get_name(clk),
			__clk_get_name(clk_get_parent(clk)),
			(unsigned int)clk_get_rate(clk));

90
	kfree(parents);
91
	of_clk_add_provider(np, of_clk_src_simple_get, clk);
92
	return;
93
94
95

err:
	kfree(parents);
96
97
err_parents:
	iounmap(reg);
98
}
99
100
101
102
103
104
105

static void __init st_of_clkgen_a9_mux_setup(struct device_node *np)
{
	st_of_clkgen_mux_setup(np, &stih407_a9_mux_data);
}
CLK_OF_DECLARE(clkgen_a9mux, "st,stih407-clkgen-a9-mux",
		st_of_clkgen_a9_mux_setup);