2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
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.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/delay.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
26 #define SLOW_CLOCK_FREQ 32768
28 #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
30 #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
31 #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
38 wait_queue_head_t wait
;
41 #define to_clk_main(hw) container_of(hw, struct clk_main, hw)
43 static irqreturn_t
clk_main_irq_handler(int irq
, void *dev_id
)
45 struct clk_main
*clkmain
= (struct clk_main
*)dev_id
;
47 wake_up(&clkmain
->wait
);
48 disable_irq_nosync(clkmain
->irq
);
53 static int clk_main_prepare(struct clk_hw
*hw
)
55 struct clk_main
*clkmain
= to_clk_main(hw
);
56 struct at91_pmc
*pmc
= clkmain
->pmc
;
57 unsigned long halt_time
, timeout
;
60 while (!(pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCS
)) {
61 enable_irq(clkmain
->irq
);
62 wait_event(clkmain
->wait
,
63 pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCS
);
69 timeout
= jiffies
+ usecs_to_jiffies(MAINFRDY_TIMEOUT
);
72 tmp
= pmc_read(pmc
, AT91_CKGR_MCFR
);
73 if (tmp
& AT91_PMC_MAINRDY
)
75 usleep_range(MAINF_LOOP_MIN_WAIT
, MAINF_LOOP_MAX_WAIT
);
76 } while (time_before(halt_time
, timeout
));
81 static int clk_main_is_prepared(struct clk_hw
*hw
)
83 struct clk_main
*clkmain
= to_clk_main(hw
);
85 return !!(pmc_read(clkmain
->pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCS
);
88 static unsigned long clk_main_recalc_rate(struct clk_hw
*hw
,
89 unsigned long parent_rate
)
92 struct clk_main
*clkmain
= to_clk_main(hw
);
93 struct at91_pmc
*pmc
= clkmain
->pmc
;
98 tmp
= pmc_read(pmc
, AT91_CKGR_MCFR
) & AT91_PMC_MAINF
;
99 clkmain
->rate
= (tmp
* parent_rate
) / MAINF_DIV
;
101 return clkmain
->rate
;
104 static const struct clk_ops main_ops
= {
105 .prepare
= clk_main_prepare
,
106 .is_prepared
= clk_main_is_prepared
,
107 .recalc_rate
= clk_main_recalc_rate
,
110 static struct clk
* __init
111 at91_clk_register_main(struct at91_pmc
*pmc
,
114 const char *parent_name
,
118 struct clk_main
*clkmain
;
119 struct clk
*clk
= NULL
;
120 struct clk_init_data init
;
122 if (!pmc
|| !irq
|| !name
)
123 return ERR_PTR(-EINVAL
);
125 if (!rate
&& !parent_name
)
126 return ERR_PTR(-EINVAL
);
128 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
130 return ERR_PTR(-ENOMEM
);
133 init
.ops
= &main_ops
;
134 init
.parent_names
= parent_name
? &parent_name
: NULL
;
135 init
.num_parents
= parent_name
? 1 : 0;
136 init
.flags
= parent_name
? 0 : CLK_IS_ROOT
;
138 clkmain
->hw
.init
= &init
;
139 clkmain
->rate
= rate
;
142 init_waitqueue_head(&clkmain
->wait
);
143 irq_set_status_flags(clkmain
->irq
, IRQ_NOAUTOEN
);
144 ret
= request_irq(clkmain
->irq
, clk_main_irq_handler
,
145 IRQF_TRIGGER_HIGH
, "clk-main", clkmain
);
149 clk
= clk_register(NULL
, &clkmain
->hw
);
151 free_irq(clkmain
->irq
, clkmain
);
161 of_at91_clk_main_setup(struct device_node
*np
, struct at91_pmc
*pmc
)
165 const char *parent_name
;
166 const char *name
= np
->name
;
169 parent_name
= of_clk_get_parent_name(np
, 0);
170 of_property_read_string(np
, "clock-output-names", &name
);
171 of_property_read_u32(np
, "clock-frequency", &rate
);
172 irq
= irq_of_parse_and_map(np
, 0);
176 clk
= at91_clk_register_main(pmc
, irq
, name
, parent_name
, rate
);
180 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
183 void __init
of_at91rm9200_clk_main_setup(struct device_node
*np
,
184 struct at91_pmc
*pmc
)
186 of_at91_clk_main_setup(np
, pmc
);