4 * Copyright (C) 2013 Ideas On Board SPRL
5 * Copyright (C) 2015 Glider bvba
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/clkdev.h>
17 #include <linux/clk/renesas.h>
18 #include <linux/device.h>
21 #include <linux/of_address.h>
22 #include <linux/pm_clock.h>
23 #include <linux/pm_domain.h>
24 #include <linux/spinlock.h>
27 * MSTP clocks. We can't use standard gate clocks as we need to poll on the
28 * status register when enabling the clock.
31 #define MSTP_MAX_CLOCKS 32
34 * struct mstp_clock_group - MSTP gating clocks group
36 * @data: clocks in this group
37 * @smstpcr: module stop control register
38 * @mstpsr: module stop status register (optional)
39 * @lock: protects writes to SMSTPCR
41 struct mstp_clock_group
{
42 struct clk_onecell_data data
;
43 void __iomem
*smstpcr
;
49 * struct mstp_clock - MSTP gating clock
50 * @hw: handle between common and hardware-specific interfaces
51 * @bit_index: control bit index
52 * @group: MSTP clocks group
57 struct mstp_clock_group
*group
;
60 #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
62 static int cpg_mstp_clock_endisable(struct clk_hw
*hw
, bool enable
)
64 struct mstp_clock
*clock
= to_mstp_clock(hw
);
65 struct mstp_clock_group
*group
= clock
->group
;
66 u32 bitmask
= BIT(clock
->bit_index
);
71 spin_lock_irqsave(&group
->lock
, flags
);
73 value
= clk_readl(group
->smstpcr
);
78 clk_writel(value
, group
->smstpcr
);
80 spin_unlock_irqrestore(&group
->lock
, flags
);
82 if (!enable
|| !group
->mstpsr
)
85 for (i
= 1000; i
> 0; --i
) {
86 if (!(clk_readl(group
->mstpsr
) & bitmask
))
92 pr_err("%s: failed to enable %p[%d]\n", __func__
,
93 group
->smstpcr
, clock
->bit_index
);
100 static int cpg_mstp_clock_enable(struct clk_hw
*hw
)
102 return cpg_mstp_clock_endisable(hw
, true);
105 static void cpg_mstp_clock_disable(struct clk_hw
*hw
)
107 cpg_mstp_clock_endisable(hw
, false);
110 static int cpg_mstp_clock_is_enabled(struct clk_hw
*hw
)
112 struct mstp_clock
*clock
= to_mstp_clock(hw
);
113 struct mstp_clock_group
*group
= clock
->group
;
117 value
= clk_readl(group
->mstpsr
);
119 value
= clk_readl(group
->smstpcr
);
121 return !(value
& BIT(clock
->bit_index
));
124 static const struct clk_ops cpg_mstp_clock_ops
= {
125 .enable
= cpg_mstp_clock_enable
,
126 .disable
= cpg_mstp_clock_disable
,
127 .is_enabled
= cpg_mstp_clock_is_enabled
,
130 static struct clk
* __init
131 cpg_mstp_clock_register(const char *name
, const char *parent_name
,
132 unsigned int index
, struct mstp_clock_group
*group
)
134 struct clk_init_data init
;
135 struct mstp_clock
*clock
;
138 clock
= kzalloc(sizeof(*clock
), GFP_KERNEL
);
140 pr_err("%s: failed to allocate MSTP clock.\n", __func__
);
141 return ERR_PTR(-ENOMEM
);
145 init
.ops
= &cpg_mstp_clock_ops
;
146 init
.flags
= CLK_IS_BASIC
| CLK_SET_RATE_PARENT
;
147 init
.parent_names
= &parent_name
;
148 init
.num_parents
= 1;
150 clock
->bit_index
= index
;
151 clock
->group
= group
;
152 clock
->hw
.init
= &init
;
154 clk
= clk_register(NULL
, &clock
->hw
);
162 static void __init
cpg_mstp_clocks_init(struct device_node
*np
)
164 struct mstp_clock_group
*group
;
169 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
170 clks
= kmalloc(MSTP_MAX_CLOCKS
* sizeof(*clks
), GFP_KERNEL
);
171 if (group
== NULL
|| clks
== NULL
) {
174 pr_err("%s: failed to allocate group\n", __func__
);
178 spin_lock_init(&group
->lock
);
179 group
->data
.clks
= clks
;
181 group
->smstpcr
= of_iomap(np
, 0);
182 group
->mstpsr
= of_iomap(np
, 1);
184 if (group
->smstpcr
== NULL
) {
185 pr_err("%s: failed to remap SMSTPCR\n", __func__
);
191 for (i
= 0; i
< MSTP_MAX_CLOCKS
; ++i
)
192 clks
[i
] = ERR_PTR(-ENOENT
);
194 if (of_find_property(np
, "clock-indices", &i
))
195 idxname
= "clock-indices";
197 idxname
= "renesas,clock-indices";
199 for (i
= 0; i
< MSTP_MAX_CLOCKS
; ++i
) {
200 const char *parent_name
;
205 /* Skip clocks with no name. */
206 ret
= of_property_read_string_index(np
, "clock-output-names",
208 if (ret
< 0 || strlen(name
) == 0)
211 parent_name
= of_clk_get_parent_name(np
, i
);
212 ret
= of_property_read_u32_index(np
, idxname
, i
, &clkidx
);
213 if (parent_name
== NULL
|| ret
< 0)
216 if (clkidx
>= MSTP_MAX_CLOCKS
) {
217 pr_err("%s: invalid clock %s %s index %u\n",
218 __func__
, np
->name
, name
, clkidx
);
222 clks
[clkidx
] = cpg_mstp_clock_register(name
, parent_name
,
224 if (!IS_ERR(clks
[clkidx
])) {
225 group
->data
.clk_num
= max(group
->data
.clk_num
,
228 * Register a clkdev to let board code retrieve the
229 * clock by name and register aliases for non-DT
232 * FIXME: Remove this when all devices that require a
233 * clock will be instantiated from DT.
235 clk_register_clkdev(clks
[clkidx
], name
, NULL
);
237 pr_err("%s: failed to register %s %s clock (%ld)\n",
238 __func__
, np
->name
, name
, PTR_ERR(clks
[clkidx
]));
242 of_clk_add_provider(np
, of_clk_src_onecell_get
, &group
->data
);
244 CLK_OF_DECLARE(cpg_mstp_clks
, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init
);
247 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
248 int cpg_mstp_attach_dev(struct generic_pm_domain
*domain
, struct device
*dev
)
250 struct device_node
*np
= dev
->of_node
;
251 struct of_phandle_args clkspec
;
256 while (!of_parse_phandle_with_args(np
, "clocks", "#clock-cells", i
,
258 if (of_device_is_compatible(clkspec
.np
,
259 "renesas,cpg-mstp-clocks"))
262 /* BSC on r8a73a4/sh73a0 uses zb_clk instead of an mstp clock */
263 if (!strcmp(clkspec
.np
->name
, "zb_clk"))
266 of_node_put(clkspec
.np
);
273 clk
= of_clk_get_from_provider(&clkspec
);
274 of_node_put(clkspec
.np
);
279 error
= pm_clk_create(dev
);
281 dev_err(dev
, "pm_clk_create failed %d\n", error
);
285 error
= pm_clk_add_clk(dev
, clk
);
287 dev_err(dev
, "pm_clk_add_clk %pC failed %d\n", clk
, error
);
300 void cpg_mstp_detach_dev(struct generic_pm_domain
*domain
, struct device
*dev
)
302 if (!list_empty(&dev
->power
.subsys_data
->clock_list
))
306 void __init
cpg_mstp_add_clk_domain(struct device_node
*np
)
308 struct generic_pm_domain
*pd
;
311 if (of_property_read_u32(np
, "#power-domain-cells", &ncells
)) {
312 pr_warn("%s lacks #power-domain-cells\n", np
->full_name
);
316 pd
= kzalloc(sizeof(*pd
), GFP_KERNEL
);
322 pd
->flags
= GENPD_FLAG_PM_CLK
;
323 pm_genpd_init(pd
, &simple_qos_governor
, false);
324 pd
->attach_dev
= cpg_mstp_attach_dev
;
325 pd
->detach_dev
= cpg_mstp_detach_dev
;
327 of_genpd_add_provider_simple(np
, pd
);
329 #endif /* !CONFIG_PM_GENERIC_DOMAINS_OF */