Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +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/of.h> |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 15 | #include <linux/mfd/syscon.h> |
| 16 | #include <linux/regmap.h> |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 17 | |
| 18 | #include "pmc.h" |
| 19 | |
| 20 | #define MASTER_SOURCE_MAX 4 |
| 21 | |
| 22 | #define MASTER_PRES_MASK 0x7 |
| 23 | #define MASTER_PRES_MAX MASTER_PRES_MASK |
| 24 | #define MASTER_DIV_SHIFT 8 |
| 25 | #define MASTER_DIV_MASK 0x3 |
| 26 | |
| 27 | struct clk_master_characteristics { |
| 28 | struct clk_range output; |
| 29 | u32 divisors[4]; |
| 30 | u8 have_div3_pres; |
| 31 | }; |
| 32 | |
| 33 | struct clk_master_layout { |
| 34 | u32 mask; |
| 35 | u8 pres_shift; |
| 36 | }; |
| 37 | |
| 38 | #define to_clk_master(hw) container_of(hw, struct clk_master, hw) |
| 39 | |
| 40 | struct clk_master { |
| 41 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 42 | struct regmap *regmap; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 43 | const struct clk_master_layout *layout; |
| 44 | const struct clk_master_characteristics *characteristics; |
| 45 | }; |
| 46 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 47 | static inline bool clk_master_ready(struct regmap *regmap) |
| 48 | { |
| 49 | unsigned int status; |
| 50 | |
| 51 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 52 | |
| 53 | return status & AT91_PMC_MCKRDY ? 1 : 0; |
| 54 | } |
| 55 | |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 56 | static int clk_master_prepare(struct clk_hw *hw) |
| 57 | { |
| 58 | struct clk_master *master = to_clk_master(hw); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 59 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 60 | while (!clk_master_ready(master->regmap)) |
| 61 | cpu_relax(); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int clk_master_is_prepared(struct clk_hw *hw) |
| 67 | { |
| 68 | struct clk_master *master = to_clk_master(hw); |
| 69 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 70 | return clk_master_ready(master->regmap); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static unsigned long clk_master_recalc_rate(struct clk_hw *hw, |
| 74 | unsigned long parent_rate) |
| 75 | { |
| 76 | u8 pres; |
| 77 | u8 div; |
| 78 | unsigned long rate = parent_rate; |
| 79 | struct clk_master *master = to_clk_master(hw); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 80 | const struct clk_master_layout *layout = master->layout; |
| 81 | const struct clk_master_characteristics *characteristics = |
| 82 | master->characteristics; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 83 | unsigned int mckr; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 84 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 85 | regmap_read(master->regmap, AT91_PMC_MCKR, &mckr); |
| 86 | mckr &= layout->mask; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 87 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 88 | pres = (mckr >> layout->pres_shift) & MASTER_PRES_MASK; |
| 89 | div = (mckr >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 90 | |
| 91 | if (characteristics->have_div3_pres && pres == MASTER_PRES_MAX) |
| 92 | rate /= 3; |
| 93 | else |
| 94 | rate >>= pres; |
| 95 | |
| 96 | rate /= characteristics->divisors[div]; |
| 97 | |
| 98 | if (rate < characteristics->output.min) |
| 99 | pr_warn("master clk is underclocked"); |
| 100 | else if (rate > characteristics->output.max) |
| 101 | pr_warn("master clk is overclocked"); |
| 102 | |
| 103 | return rate; |
| 104 | } |
| 105 | |
| 106 | static u8 clk_master_get_parent(struct clk_hw *hw) |
| 107 | { |
| 108 | struct clk_master *master = to_clk_master(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 109 | unsigned int mckr; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 110 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 111 | regmap_read(master->regmap, AT91_PMC_MCKR, &mckr); |
| 112 | |
| 113 | return mckr & AT91_PMC_CSS; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static const struct clk_ops master_ops = { |
| 117 | .prepare = clk_master_prepare, |
| 118 | .is_prepared = clk_master_is_prepared, |
| 119 | .recalc_rate = clk_master_recalc_rate, |
| 120 | .get_parent = clk_master_get_parent, |
| 121 | }; |
| 122 | |
| 123 | static struct clk * __init |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 124 | at91_clk_register_master(struct regmap *regmap, |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 125 | const char *name, int num_parents, |
| 126 | const char **parent_names, |
| 127 | const struct clk_master_layout *layout, |
| 128 | const struct clk_master_characteristics *characteristics) |
| 129 | { |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 130 | struct clk_master *master; |
| 131 | struct clk *clk = NULL; |
| 132 | struct clk_init_data init; |
| 133 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 134 | if (!name || !num_parents || !parent_names) |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 135 | return ERR_PTR(-EINVAL); |
| 136 | |
| 137 | master = kzalloc(sizeof(*master), GFP_KERNEL); |
| 138 | if (!master) |
| 139 | return ERR_PTR(-ENOMEM); |
| 140 | |
| 141 | init.name = name; |
| 142 | init.ops = &master_ops; |
| 143 | init.parent_names = parent_names; |
| 144 | init.num_parents = num_parents; |
| 145 | init.flags = 0; |
| 146 | |
| 147 | master->hw.init = &init; |
| 148 | master->layout = layout; |
| 149 | master->characteristics = characteristics; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 150 | master->regmap = regmap; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 151 | |
| 152 | clk = clk_register(NULL, &master->hw); |
David Dueck | c76a024 | 2015-06-26 15:30:22 +0200 | [diff] [blame] | 153 | if (IS_ERR(clk)) { |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 154 | kfree(master); |
David Dueck | c76a024 | 2015-06-26 15:30:22 +0200 | [diff] [blame] | 155 | } |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 156 | |
| 157 | return clk; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | static const struct clk_master_layout at91rm9200_master_layout = { |
| 162 | .mask = 0x31F, |
| 163 | .pres_shift = 2, |
| 164 | }; |
| 165 | |
| 166 | static const struct clk_master_layout at91sam9x5_master_layout = { |
| 167 | .mask = 0x373, |
| 168 | .pres_shift = 4, |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | static struct clk_master_characteristics * __init |
| 173 | of_at91_clk_master_get_characteristics(struct device_node *np) |
| 174 | { |
| 175 | struct clk_master_characteristics *characteristics; |
| 176 | |
| 177 | characteristics = kzalloc(sizeof(*characteristics), GFP_KERNEL); |
| 178 | if (!characteristics) |
| 179 | return NULL; |
| 180 | |
| 181 | if (of_at91_get_clk_range(np, "atmel,clk-output-range", &characteristics->output)) |
| 182 | goto out_free_characteristics; |
| 183 | |
| 184 | of_property_read_u32_array(np, "atmel,clk-divisors", |
| 185 | characteristics->divisors, 4); |
| 186 | |
| 187 | characteristics->have_div3_pres = |
| 188 | of_property_read_bool(np, "atmel,master-clk-have-div3-pres"); |
| 189 | |
| 190 | return characteristics; |
| 191 | |
| 192 | out_free_characteristics: |
| 193 | kfree(characteristics); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | static void __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 198 | of_at91_clk_master_setup(struct device_node *np, |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 199 | const struct clk_master_layout *layout) |
| 200 | { |
| 201 | struct clk *clk; |
Stephen Boyd | 8c1b1e5 | 2016-02-19 17:29:17 -0800 | [diff] [blame] | 202 | unsigned int num_parents; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 203 | const char *parent_names[MASTER_SOURCE_MAX]; |
| 204 | const char *name = np->name; |
| 205 | struct clk_master_characteristics *characteristics; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 206 | struct regmap *regmap; |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 207 | |
Geert Uytterhoeven | 51a43be | 2015-05-29 11:25:45 +0200 | [diff] [blame] | 208 | num_parents = of_clk_get_parent_count(np); |
Stephen Boyd | 8c1b1e5 | 2016-02-19 17:29:17 -0800 | [diff] [blame] | 209 | if (num_parents == 0 || num_parents > MASTER_SOURCE_MAX) |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 210 | return; |
| 211 | |
Dinh Nguyen | f0557fb | 2015-07-06 22:59:01 -0500 | [diff] [blame] | 212 | of_clk_parent_fill(np, parent_names, num_parents); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 213 | |
| 214 | of_property_read_string(np, "clock-output-names", &name); |
| 215 | |
| 216 | characteristics = of_at91_clk_master_get_characteristics(np); |
| 217 | if (!characteristics) |
| 218 | return; |
| 219 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 220 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 221 | if (IS_ERR(regmap)) |
| 222 | return; |
| 223 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 224 | clk = at91_clk_register_master(regmap, name, num_parents, |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 225 | parent_names, layout, |
| 226 | characteristics); |
| 227 | if (IS_ERR(clk)) |
| 228 | goto out_free_characteristics; |
| 229 | |
| 230 | of_clk_add_provider(np, of_clk_src_simple_get, clk); |
| 231 | return; |
| 232 | |
| 233 | out_free_characteristics: |
| 234 | kfree(characteristics); |
| 235 | } |
| 236 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 237 | static void __init of_at91rm9200_clk_master_setup(struct device_node *np) |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 238 | { |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 239 | of_at91_clk_master_setup(np, &at91rm9200_master_layout); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 240 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 241 | CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master", |
| 242 | of_at91rm9200_clk_master_setup); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 243 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 244 | static void __init of_at91sam9x5_clk_master_setup(struct device_node *np) |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 245 | { |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 246 | of_at91_clk_master_setup(np, &at91sam9x5_master_layout); |
Boris BREZILLON | e442d23 | 2013-10-11 10:51:23 +0200 | [diff] [blame] | 247 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 248 | CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master", |
| 249 | of_at91sam9x5_clk_master_setup); |