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>
15 #include <linux/mfd/syscon.h>
16 #include <linux/regmap.h>
20 #define SYSTEM_MAX_ID 31
22 #define SYSTEM_MAX_NAME_SZ 32
24 #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
27 struct regmap
*regmap
;
31 static inline int is_pck(int id
)
33 return (id
>= 8) && (id
<= 15);
36 static inline bool clk_system_ready(struct regmap
*regmap
, int id
)
40 regmap_read(regmap
, AT91_PMC_SR
, &status
);
42 return status
& (1 << id
) ? 1 : 0;
45 static int clk_system_prepare(struct clk_hw
*hw
)
47 struct clk_system
*sys
= to_clk_system(hw
);
49 regmap_write(sys
->regmap
, AT91_PMC_SCER
, 1 << sys
->id
);
54 while (!clk_system_ready(sys
->regmap
, sys
->id
))
60 static void clk_system_unprepare(struct clk_hw
*hw
)
62 struct clk_system
*sys
= to_clk_system(hw
);
64 regmap_write(sys
->regmap
, AT91_PMC_SCDR
, 1 << sys
->id
);
67 static int clk_system_is_prepared(struct clk_hw
*hw
)
69 struct clk_system
*sys
= to_clk_system(hw
);
72 regmap_read(sys
->regmap
, AT91_PMC_SCSR
, &status
);
74 if (!(status
& (1 << sys
->id
)))
80 regmap_read(sys
->regmap
, AT91_PMC_SR
, &status
);
82 return status
& (1 << sys
->id
) ? 1 : 0;
85 static const struct clk_ops system_ops
= {
86 .prepare
= clk_system_prepare
,
87 .unprepare
= clk_system_unprepare
,
88 .is_prepared
= clk_system_is_prepared
,
91 static struct clk_hw
* __init
92 at91_clk_register_system(struct regmap
*regmap
, const char *name
,
93 const char *parent_name
, u8 id
)
95 struct clk_system
*sys
;
97 struct clk_init_data init
;
100 if (!parent_name
|| id
> SYSTEM_MAX_ID
)
101 return ERR_PTR(-EINVAL
);
103 sys
= kzalloc(sizeof(*sys
), GFP_KERNEL
);
105 return ERR_PTR(-ENOMEM
);
108 init
.ops
= &system_ops
;
109 init
.parent_names
= &parent_name
;
110 init
.num_parents
= 1;
111 init
.flags
= CLK_SET_RATE_PARENT
;
114 sys
->hw
.init
= &init
;
115 sys
->regmap
= regmap
;
118 ret
= clk_hw_register(NULL
, &sys
->hw
);
127 static void __init
of_at91rm9200_clk_sys_setup(struct device_node
*np
)
133 struct device_node
*sysclknp
;
134 const char *parent_name
;
135 struct regmap
*regmap
;
137 num
= of_get_child_count(np
);
138 if (num
> (SYSTEM_MAX_ID
+ 1))
141 regmap
= syscon_node_to_regmap(of_get_parent(np
));
145 for_each_child_of_node(np
, sysclknp
) {
146 if (of_property_read_u32(sysclknp
, "reg", &id
))
149 if (of_property_read_string(np
, "clock-output-names", &name
))
150 name
= sysclknp
->name
;
152 parent_name
= of_clk_get_parent_name(sysclknp
, 0);
154 hw
= at91_clk_register_system(regmap
, name
, parent_name
, id
);
158 of_clk_add_hw_provider(sysclknp
, of_clk_hw_simple_get
, hw
);
161 CLK_OF_DECLARE(at91rm9200_clk_sys
, "atmel,at91rm9200-clk-system",
162 of_at91rm9200_clk_sys_setup
);