Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 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/delay.h> |
| 15 | #include <linux/of.h> |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 16 | #include <linux/mfd/syscon.h> |
| 17 | #include <linux/regmap.h> |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 18 | |
| 19 | #include "pmc.h" |
| 20 | |
| 21 | #define SLOW_CLOCK_FREQ 32768 |
| 22 | #define MAINF_DIV 16 |
| 23 | #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \ |
| 24 | SLOW_CLOCK_FREQ) |
| 25 | #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ) |
| 26 | #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT |
| 27 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 28 | #define MOR_KEY_MASK (0xff << 16) |
| 29 | |
| 30 | struct clk_main_osc { |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 31 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 32 | struct regmap *regmap; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 35 | #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 36 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 37 | struct clk_main_rc_osc { |
| 38 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 39 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 40 | unsigned long frequency; |
| 41 | unsigned long accuracy; |
| 42 | }; |
| 43 | |
| 44 | #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw) |
| 45 | |
| 46 | struct clk_rm9200_main { |
| 47 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 48 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw) |
| 52 | |
| 53 | struct clk_sam9x5_main { |
| 54 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 55 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 56 | u8 parent; |
| 57 | }; |
| 58 | |
| 59 | #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw) |
| 60 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 61 | static inline bool clk_main_osc_ready(struct regmap *regmap) |
| 62 | { |
| 63 | unsigned int status; |
| 64 | |
| 65 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 66 | |
| 67 | return status & AT91_PMC_MOSCS; |
| 68 | } |
| 69 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 70 | static int clk_main_osc_prepare(struct clk_hw *hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 71 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 72 | struct clk_main_osc *osc = to_clk_main_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 73 | struct regmap *regmap = osc->regmap; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 74 | u32 tmp; |
| 75 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 76 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
| 77 | tmp &= ~MOR_KEY_MASK; |
| 78 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 79 | if (tmp & AT91_PMC_OSCBYPASS) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 80 | return 0; |
| 81 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 82 | if (!(tmp & AT91_PMC_MOSCEN)) { |
| 83 | tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 84 | regmap_write(regmap, AT91_CKGR_MOR, tmp); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 87 | while (!clk_main_osc_ready(regmap)) |
| 88 | cpu_relax(); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 93 | static void clk_main_osc_unprepare(struct clk_hw *hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 94 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 95 | struct clk_main_osc *osc = to_clk_main_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 96 | struct regmap *regmap = osc->regmap; |
| 97 | u32 tmp; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 98 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 99 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 100 | if (tmp & AT91_PMC_OSCBYPASS) |
| 101 | return; |
| 102 | |
| 103 | if (!(tmp & AT91_PMC_MOSCEN)) |
| 104 | return; |
| 105 | |
| 106 | tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 107 | regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 110 | static int clk_main_osc_is_prepared(struct clk_hw *hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 111 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 112 | struct clk_main_osc *osc = to_clk_main_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 113 | struct regmap *regmap = osc->regmap; |
| 114 | u32 tmp, status; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 115 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 116 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 117 | if (tmp & AT91_PMC_OSCBYPASS) |
| 118 | return 1; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 119 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 120 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 121 | |
| 122 | return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 125 | static const struct clk_ops main_osc_ops = { |
| 126 | .prepare = clk_main_osc_prepare, |
| 127 | .unprepare = clk_main_osc_unprepare, |
| 128 | .is_prepared = clk_main_osc_is_prepared, |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 131 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 132 | at91_clk_register_main_osc(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 133 | const char *name, |
| 134 | const char *parent_name, |
| 135 | bool bypass) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 136 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 137 | struct clk_main_osc *osc; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 138 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 139 | struct clk_hw *hw; |
| 140 | int ret; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 141 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 142 | if (!name || !parent_name) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 143 | return ERR_PTR(-EINVAL); |
| 144 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 145 | osc = kzalloc(sizeof(*osc), GFP_KERNEL); |
| 146 | if (!osc) |
| 147 | return ERR_PTR(-ENOMEM); |
| 148 | |
| 149 | init.name = name; |
| 150 | init.ops = &main_osc_ops; |
| 151 | init.parent_names = &parent_name; |
| 152 | init.num_parents = 1; |
| 153 | init.flags = CLK_IGNORE_UNUSED; |
| 154 | |
| 155 | osc->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 156 | osc->regmap = regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 157 | |
| 158 | if (bypass) |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 159 | regmap_update_bits(regmap, |
| 160 | AT91_CKGR_MOR, MOR_KEY_MASK | |
| 161 | AT91_PMC_MOSCEN, |
| 162 | AT91_PMC_OSCBYPASS | AT91_PMC_KEY); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 163 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 164 | hw = &osc->hw; |
| 165 | ret = clk_hw_register(NULL, &osc->hw); |
| 166 | if (ret) { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 167 | kfree(osc); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 168 | hw = ERR_PTR(ret); |
| 169 | } |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 170 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 171 | return hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 174 | static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 175 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 176 | struct clk_hw *hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 177 | const char *name = np->name; |
| 178 | const char *parent_name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 179 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 180 | bool bypass; |
| 181 | |
| 182 | of_property_read_string(np, "clock-output-names", &name); |
| 183 | bypass = of_property_read_bool(np, "atmel,osc-bypass"); |
| 184 | parent_name = of_clk_get_parent_name(np, 0); |
| 185 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 186 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 187 | if (IS_ERR(regmap)) |
| 188 | return; |
| 189 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 190 | hw = at91_clk_register_main_osc(regmap, name, parent_name, bypass); |
| 191 | if (IS_ERR(hw)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 192 | return; |
| 193 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 194 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 195 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 196 | CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc", |
| 197 | of_at91rm9200_clk_main_osc_setup); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 198 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 199 | static bool clk_main_rc_osc_ready(struct regmap *regmap) |
| 200 | { |
| 201 | unsigned int status; |
| 202 | |
| 203 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 204 | |
| 205 | return status & AT91_PMC_MOSCRCS; |
| 206 | } |
| 207 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 208 | static int clk_main_rc_osc_prepare(struct clk_hw *hw) |
| 209 | { |
| 210 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 211 | struct regmap *regmap = osc->regmap; |
| 212 | unsigned int mor; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 213 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 214 | regmap_read(regmap, AT91_CKGR_MOR, &mor); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 215 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 216 | if (!(mor & AT91_PMC_MOSCRCEN)) |
| 217 | regmap_update_bits(regmap, AT91_CKGR_MOR, |
| 218 | MOR_KEY_MASK | AT91_PMC_MOSCRCEN, |
| 219 | AT91_PMC_MOSCRCEN | AT91_PMC_KEY); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 220 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 221 | while (!clk_main_rc_osc_ready(regmap)) |
| 222 | cpu_relax(); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static void clk_main_rc_osc_unprepare(struct clk_hw *hw) |
| 228 | { |
| 229 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 230 | struct regmap *regmap = osc->regmap; |
| 231 | unsigned int mor; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 232 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 233 | regmap_read(regmap, AT91_CKGR_MOR, &mor); |
| 234 | |
| 235 | if (!(mor & AT91_PMC_MOSCRCEN)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 236 | return; |
| 237 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 238 | regmap_update_bits(regmap, AT91_CKGR_MOR, |
| 239 | MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static int clk_main_rc_osc_is_prepared(struct clk_hw *hw) |
| 243 | { |
| 244 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 245 | struct regmap *regmap = osc->regmap; |
| 246 | unsigned int mor, status; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 247 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 248 | regmap_read(regmap, AT91_CKGR_MOR, &mor); |
| 249 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 250 | |
| 251 | return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw, |
| 255 | unsigned long parent_rate) |
| 256 | { |
| 257 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
| 258 | |
| 259 | return osc->frequency; |
| 260 | } |
| 261 | |
| 262 | static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw, |
| 263 | unsigned long parent_acc) |
| 264 | { |
| 265 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
| 266 | |
| 267 | return osc->accuracy; |
| 268 | } |
| 269 | |
| 270 | static const struct clk_ops main_rc_osc_ops = { |
| 271 | .prepare = clk_main_rc_osc_prepare, |
| 272 | .unprepare = clk_main_rc_osc_unprepare, |
| 273 | .is_prepared = clk_main_rc_osc_is_prepared, |
| 274 | .recalc_rate = clk_main_rc_osc_recalc_rate, |
| 275 | .recalc_accuracy = clk_main_rc_osc_recalc_accuracy, |
| 276 | }; |
| 277 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 278 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 279 | at91_clk_register_main_rc_osc(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 280 | const char *name, |
| 281 | u32 frequency, u32 accuracy) |
| 282 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 283 | struct clk_main_rc_osc *osc; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 284 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 285 | struct clk_hw *hw; |
| 286 | int ret; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 287 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 288 | if (!name || !frequency) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 289 | return ERR_PTR(-EINVAL); |
| 290 | |
| 291 | osc = kzalloc(sizeof(*osc), GFP_KERNEL); |
| 292 | if (!osc) |
| 293 | return ERR_PTR(-ENOMEM); |
| 294 | |
| 295 | init.name = name; |
| 296 | init.ops = &main_rc_osc_ops; |
| 297 | init.parent_names = NULL; |
| 298 | init.num_parents = 0; |
Stephen Boyd | a9bb2ef | 2016-03-01 10:59:46 -0800 | [diff] [blame] | 299 | init.flags = CLK_IGNORE_UNUSED; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 300 | |
| 301 | osc->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 302 | osc->regmap = regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 303 | osc->frequency = frequency; |
| 304 | osc->accuracy = accuracy; |
| 305 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 306 | hw = &osc->hw; |
| 307 | ret = clk_hw_register(NULL, hw); |
| 308 | if (ret) { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 309 | kfree(osc); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 310 | hw = ERR_PTR(ret); |
| 311 | } |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 312 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 313 | return hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 316 | static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 317 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 318 | struct clk_hw *hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 319 | u32 frequency = 0; |
| 320 | u32 accuracy = 0; |
| 321 | const char *name = np->name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 322 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 323 | |
| 324 | of_property_read_string(np, "clock-output-names", &name); |
| 325 | of_property_read_u32(np, "clock-frequency", &frequency); |
| 326 | of_property_read_u32(np, "clock-accuracy", &accuracy); |
| 327 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 328 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 329 | if (IS_ERR(regmap)) |
| 330 | return; |
| 331 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 332 | hw = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy); |
| 333 | if (IS_ERR(hw)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 334 | return; |
| 335 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 336 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 337 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 338 | CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc", |
| 339 | of_at91sam9x5_clk_main_rc_osc_setup); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 340 | |
| 341 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 342 | static int clk_main_probe_frequency(struct regmap *regmap) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 343 | { |
| 344 | unsigned long prep_time, timeout; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 345 | unsigned int mcfr; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 346 | |
| 347 | timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT); |
| 348 | do { |
| 349 | prep_time = jiffies; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 350 | regmap_read(regmap, AT91_CKGR_MCFR, &mcfr); |
| 351 | if (mcfr & AT91_PMC_MAINRDY) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 352 | return 0; |
| 353 | usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT); |
| 354 | } while (time_before(prep_time, timeout)); |
| 355 | |
| 356 | return -ETIMEDOUT; |
| 357 | } |
| 358 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 359 | static unsigned long clk_main_recalc_rate(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 360 | unsigned long parent_rate) |
| 361 | { |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 362 | unsigned int mcfr; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 363 | |
| 364 | if (parent_rate) |
| 365 | return parent_rate; |
| 366 | |
Alexandre Belloni | 4da66b6 | 2014-07-01 16:12:12 +0200 | [diff] [blame] | 367 | pr_warn("Main crystal frequency not set, using approximate value\n"); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 368 | regmap_read(regmap, AT91_CKGR_MCFR, &mcfr); |
| 369 | if (!(mcfr & AT91_PMC_MAINRDY)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 370 | return 0; |
| 371 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 372 | return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int clk_rm9200_main_prepare(struct clk_hw *hw) |
| 376 | { |
| 377 | struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw); |
| 378 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 379 | return clk_main_probe_frequency(clkmain->regmap); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int clk_rm9200_main_is_prepared(struct clk_hw *hw) |
| 383 | { |
| 384 | struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 385 | unsigned int status; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 386 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 387 | regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status); |
| 388 | |
| 389 | return status & AT91_PMC_MAINRDY ? 1 : 0; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw, |
| 393 | unsigned long parent_rate) |
| 394 | { |
| 395 | struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw); |
| 396 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 397 | return clk_main_recalc_rate(clkmain->regmap, parent_rate); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static const struct clk_ops rm9200_main_ops = { |
| 401 | .prepare = clk_rm9200_main_prepare, |
| 402 | .is_prepared = clk_rm9200_main_is_prepared, |
| 403 | .recalc_rate = clk_rm9200_main_recalc_rate, |
| 404 | }; |
| 405 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 406 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 407 | at91_clk_register_rm9200_main(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 408 | const char *name, |
| 409 | const char *parent_name) |
| 410 | { |
| 411 | struct clk_rm9200_main *clkmain; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 412 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 413 | struct clk_hw *hw; |
| 414 | int ret; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 415 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 416 | if (!name) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 417 | return ERR_PTR(-EINVAL); |
| 418 | |
| 419 | if (!parent_name) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 420 | return ERR_PTR(-EINVAL); |
| 421 | |
| 422 | clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL); |
| 423 | if (!clkmain) |
| 424 | return ERR_PTR(-ENOMEM); |
| 425 | |
| 426 | init.name = name; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 427 | init.ops = &rm9200_main_ops; |
| 428 | init.parent_names = &parent_name; |
| 429 | init.num_parents = 1; |
| 430 | init.flags = 0; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 431 | |
| 432 | clkmain->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 433 | clkmain->regmap = regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 434 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 435 | hw = &clkmain->hw; |
| 436 | ret = clk_hw_register(NULL, &clkmain->hw); |
| 437 | if (ret) { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 438 | kfree(clkmain); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 439 | hw = ERR_PTR(ret); |
| 440 | } |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 441 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 442 | return hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 445 | static void __init of_at91rm9200_clk_main_setup(struct device_node *np) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 446 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 447 | struct clk_hw *hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 448 | const char *parent_name; |
| 449 | const char *name = np->name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 450 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 451 | |
| 452 | parent_name = of_clk_get_parent_name(np, 0); |
| 453 | of_property_read_string(np, "clock-output-names", &name); |
| 454 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 455 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 456 | if (IS_ERR(regmap)) |
| 457 | return; |
| 458 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 459 | hw = at91_clk_register_rm9200_main(regmap, name, parent_name); |
| 460 | if (IS_ERR(hw)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 461 | return; |
| 462 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 463 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 464 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 465 | CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main", |
| 466 | of_at91rm9200_clk_main_setup); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 467 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 468 | static inline bool clk_sam9x5_main_ready(struct regmap *regmap) |
| 469 | { |
| 470 | unsigned int status; |
| 471 | |
| 472 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 473 | |
| 474 | return status & AT91_PMC_MOSCSELS ? 1 : 0; |
| 475 | } |
| 476 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 477 | static int clk_sam9x5_main_prepare(struct clk_hw *hw) |
| 478 | { |
| 479 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 480 | struct regmap *regmap = clkmain->regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 481 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 482 | while (!clk_sam9x5_main_ready(regmap)) |
| 483 | cpu_relax(); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 484 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 485 | return clk_main_probe_frequency(regmap); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static int clk_sam9x5_main_is_prepared(struct clk_hw *hw) |
| 489 | { |
| 490 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
| 491 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 492 | return clk_sam9x5_main_ready(clkmain->regmap); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw, |
| 496 | unsigned long parent_rate) |
| 497 | { |
| 498 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
| 499 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 500 | return clk_main_recalc_rate(clkmain->regmap, parent_rate); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index) |
| 504 | { |
| 505 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 506 | struct regmap *regmap = clkmain->regmap; |
| 507 | unsigned int tmp; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 508 | |
| 509 | if (index > 1) |
| 510 | return -EINVAL; |
| 511 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 512 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
| 513 | tmp &= ~MOR_KEY_MASK; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 514 | |
| 515 | if (index && !(tmp & AT91_PMC_MOSCSEL)) |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 516 | regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 517 | else if (!index && (tmp & AT91_PMC_MOSCSEL)) |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 518 | regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 519 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 520 | while (!clk_sam9x5_main_ready(regmap)) |
| 521 | cpu_relax(); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw) |
| 527 | { |
| 528 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 529 | unsigned int status; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 530 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 531 | regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status); |
| 532 | |
| 533 | return status & AT91_PMC_MOSCEN ? 1 : 0; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static const struct clk_ops sam9x5_main_ops = { |
| 537 | .prepare = clk_sam9x5_main_prepare, |
| 538 | .is_prepared = clk_sam9x5_main_is_prepared, |
| 539 | .recalc_rate = clk_sam9x5_main_recalc_rate, |
| 540 | .set_parent = clk_sam9x5_main_set_parent, |
| 541 | .get_parent = clk_sam9x5_main_get_parent, |
| 542 | }; |
| 543 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 544 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 545 | at91_clk_register_sam9x5_main(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 546 | const char *name, |
| 547 | const char **parent_names, |
| 548 | int num_parents) |
| 549 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 550 | struct clk_sam9x5_main *clkmain; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 551 | struct clk_init_data init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 552 | unsigned int status; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 553 | struct clk_hw *hw; |
| 554 | int ret; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 555 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 556 | if (!name) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 557 | return ERR_PTR(-EINVAL); |
| 558 | |
| 559 | if (!parent_names || !num_parents) |
| 560 | return ERR_PTR(-EINVAL); |
| 561 | |
| 562 | clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL); |
| 563 | if (!clkmain) |
| 564 | return ERR_PTR(-ENOMEM); |
| 565 | |
| 566 | init.name = name; |
| 567 | init.ops = &sam9x5_main_ops; |
| 568 | init.parent_names = parent_names; |
| 569 | init.num_parents = num_parents; |
| 570 | init.flags = CLK_SET_PARENT_GATE; |
| 571 | |
| 572 | clkmain->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 573 | clkmain->regmap = regmap; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 574 | regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status); |
| 575 | clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 576 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 577 | hw = &clkmain->hw; |
| 578 | ret = clk_hw_register(NULL, &clkmain->hw); |
| 579 | if (ret) { |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 580 | kfree(clkmain); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 581 | hw = ERR_PTR(ret); |
| 582 | } |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 583 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 584 | return hw; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 587 | static void __init of_at91sam9x5_clk_main_setup(struct device_node *np) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 588 | { |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 589 | struct clk_hw *hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 590 | const char *parent_names[2]; |
Stephen Boyd | 8c1b1e5 | 2016-02-19 17:29:17 -0800 | [diff] [blame] | 591 | unsigned int num_parents; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 592 | const char *name = np->name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 593 | struct regmap *regmap; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 594 | |
Geert Uytterhoeven | 51a43be | 2015-05-29 11:25:45 +0200 | [diff] [blame] | 595 | num_parents = of_clk_get_parent_count(np); |
Stephen Boyd | 8c1b1e5 | 2016-02-19 17:29:17 -0800 | [diff] [blame] | 596 | if (num_parents == 0 || num_parents > 2) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 597 | return; |
| 598 | |
Dinh Nguyen | f0557fb | 2015-07-06 22:59:01 -0500 | [diff] [blame] | 599 | of_clk_parent_fill(np, parent_names, num_parents); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 600 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 601 | if (IS_ERR(regmap)) |
| 602 | return; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 603 | |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 604 | of_property_read_string(np, "clock-output-names", &name); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 605 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 606 | hw = at91_clk_register_sam9x5_main(regmap, name, parent_names, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 607 | num_parents); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 608 | if (IS_ERR(hw)) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 609 | return; |
| 610 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 611 | of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 612 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 613 | CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main", |
| 614 | of_at91sam9x5_clk_main_setup); |