xref: /openbmc/linux/drivers/clk/at91/clk-system.c (revision 5f32c314)
1 /*
2  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  */
10 
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/io.h>
17 
18 #include "pmc.h"
19 
20 #define SYSTEM_MAX_ID		31
21 
22 #define SYSTEM_MAX_NAME_SZ	32
23 
24 #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
25 struct clk_system {
26 	struct clk_hw hw;
27 	struct at91_pmc *pmc;
28 	u8 id;
29 };
30 
31 static int clk_system_enable(struct clk_hw *hw)
32 {
33 	struct clk_system *sys = to_clk_system(hw);
34 	struct at91_pmc *pmc = sys->pmc;
35 
36 	pmc_write(pmc, AT91_PMC_SCER, 1 << sys->id);
37 	return 0;
38 }
39 
40 static void clk_system_disable(struct clk_hw *hw)
41 {
42 	struct clk_system *sys = to_clk_system(hw);
43 	struct at91_pmc *pmc = sys->pmc;
44 
45 	pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
46 }
47 
48 static int clk_system_is_enabled(struct clk_hw *hw)
49 {
50 	struct clk_system *sys = to_clk_system(hw);
51 	struct at91_pmc *pmc = sys->pmc;
52 
53 	return !!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id));
54 }
55 
56 static const struct clk_ops system_ops = {
57 	.enable = clk_system_enable,
58 	.disable = clk_system_disable,
59 	.is_enabled = clk_system_is_enabled,
60 };
61 
62 static struct clk * __init
63 at91_clk_register_system(struct at91_pmc *pmc, const char *name,
64 			 const char *parent_name, u8 id)
65 {
66 	struct clk_system *sys;
67 	struct clk *clk = NULL;
68 	struct clk_init_data init;
69 
70 	if (!parent_name || id > SYSTEM_MAX_ID)
71 		return ERR_PTR(-EINVAL);
72 
73 	sys = kzalloc(sizeof(*sys), GFP_KERNEL);
74 	if (!sys)
75 		return ERR_PTR(-ENOMEM);
76 
77 	init.name = name;
78 	init.ops = &system_ops;
79 	init.parent_names = &parent_name;
80 	init.num_parents = 1;
81 	/*
82 	 * CLK_IGNORE_UNUSED is used to avoid ddrck switch off.
83 	 * TODO : we should implement a driver supporting at91 ddr controller
84 	 * (see drivers/memory) which would request and enable the ddrck clock.
85 	 * When this is done we will be able to remove CLK_IGNORE_UNUSED flag.
86 	 */
87 	init.flags = CLK_IGNORE_UNUSED;
88 
89 	sys->id = id;
90 	sys->hw.init = &init;
91 	sys->pmc = pmc;
92 
93 	clk = clk_register(NULL, &sys->hw);
94 	if (IS_ERR(clk))
95 		kfree(sys);
96 
97 	return clk;
98 }
99 
100 static void __init
101 of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
102 {
103 	int num;
104 	u32 id;
105 	struct clk *clk;
106 	const char *name;
107 	struct device_node *sysclknp;
108 	const char *parent_name;
109 
110 	num = of_get_child_count(np);
111 	if (num > (SYSTEM_MAX_ID + 1))
112 		return;
113 
114 	for_each_child_of_node(np, sysclknp) {
115 		if (of_property_read_u32(sysclknp, "reg", &id))
116 			continue;
117 
118 		if (of_property_read_string(np, "clock-output-names", &name))
119 			name = sysclknp->name;
120 
121 		parent_name = of_clk_get_parent_name(sysclknp, 0);
122 
123 		clk = at91_clk_register_system(pmc, name, parent_name, id);
124 		if (IS_ERR(clk))
125 			continue;
126 
127 		of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
128 	}
129 }
130 
131 void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
132 					struct at91_pmc *pmc)
133 {
134 	of_at91_clk_sys_setup(np, pmc);
135 }
136