Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +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 | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 17 | |
| 18 | #include "pmc.h" |
| 19 | |
| 20 | #define SYSTEM_MAX_ID 31 |
| 21 | |
| 22 | #define SYSTEM_MAX_NAME_SZ 32 |
| 23 | |
| 24 | #define to_clk_system(hw) container_of(hw, struct clk_system, hw) |
| 25 | struct clk_system { |
| 26 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 27 | struct regmap *regmap; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 28 | u8 id; |
| 29 | }; |
| 30 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 31 | static inline int is_pck(int id) |
| 32 | { |
| 33 | return (id >= 8) && (id <= 15); |
| 34 | } |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 35 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 36 | static inline bool clk_system_ready(struct regmap *regmap, int id) |
| 37 | { |
| 38 | unsigned int status; |
| 39 | |
| 40 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 41 | |
| 42 | return status & (1 << id) ? 1 : 0; |
| 43 | } |
| 44 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 45 | static int clk_system_prepare(struct clk_hw *hw) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 46 | { |
| 47 | struct clk_system *sys = to_clk_system(hw); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 48 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 49 | regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id); |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 50 | |
| 51 | if (!is_pck(sys->id)) |
| 52 | return 0; |
| 53 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 54 | while (!clk_system_ready(sys->regmap, sys->id)) |
| 55 | cpu_relax(); |
| 56 | |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 60 | static void clk_system_unprepare(struct clk_hw *hw) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 61 | { |
| 62 | struct clk_system *sys = to_clk_system(hw); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 63 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 64 | regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 67 | static int clk_system_is_prepared(struct clk_hw *hw) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 68 | { |
| 69 | struct clk_system *sys = to_clk_system(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 70 | unsigned int status; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 71 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 72 | regmap_read(sys->regmap, AT91_PMC_SCSR, &status); |
| 73 | |
| 74 | if (!(status & (1 << sys->id))) |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 75 | return 0; |
| 76 | |
| 77 | if (!is_pck(sys->id)) |
| 78 | return 1; |
| 79 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 80 | regmap_read(sys->regmap, AT91_PMC_SR, &status); |
| 81 | |
| 82 | return status & (1 << sys->id) ? 1 : 0; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static const struct clk_ops system_ops = { |
Jean-Jacques Hiblot | cce6db8 | 2014-03-11 10:00:34 +0100 | [diff] [blame] | 86 | .prepare = clk_system_prepare, |
| 87 | .unprepare = clk_system_unprepare, |
| 88 | .is_prepared = clk_system_is_prepared, |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 91 | static struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 92 | at91_clk_register_system(struct regmap *regmap, const char *name, |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 93 | const char *parent_name, u8 id) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 94 | { |
| 95 | struct clk_system *sys; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 96 | struct clk_hw *hw; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 97 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 98 | int ret; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 99 | |
| 100 | if (!parent_name || id > SYSTEM_MAX_ID) |
| 101 | return ERR_PTR(-EINVAL); |
| 102 | |
| 103 | sys = kzalloc(sizeof(*sys), GFP_KERNEL); |
| 104 | if (!sys) |
| 105 | return ERR_PTR(-ENOMEM); |
| 106 | |
| 107 | init.name = name; |
| 108 | init.ops = &system_ops; |
| 109 | init.parent_names = &parent_name; |
| 110 | init.num_parents = 1; |
Alexandre Belloni | b736bcb | 2014-07-08 18:21:16 +0200 | [diff] [blame] | 111 | init.flags = CLK_SET_RATE_PARENT; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 112 | |
| 113 | sys->id = id; |
| 114 | sys->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 115 | sys->regmap = regmap; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 116 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 117 | hw = &sys->hw; |
| 118 | ret = clk_hw_register(NULL, &sys->hw); |
| 119 | if (ret) { |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 120 | kfree(sys); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 121 | hw = ERR_PTR(ret); |
| 122 | } |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 123 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 124 | return hw; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 127 | static void __init of_at91rm9200_clk_sys_setup(struct device_node *np) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 128 | { |
| 129 | int num; |
| 130 | u32 id; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 131 | struct clk_hw *hw; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 132 | const char *name; |
| 133 | struct device_node *sysclknp; |
| 134 | const char *parent_name; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 135 | struct regmap *regmap; |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 136 | |
| 137 | num = of_get_child_count(np); |
| 138 | if (num > (SYSTEM_MAX_ID + 1)) |
| 139 | return; |
| 140 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 141 | regmap = syscon_node_to_regmap(of_get_parent(np)); |
| 142 | if (IS_ERR(regmap)) |
| 143 | return; |
| 144 | |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 145 | for_each_child_of_node(np, sysclknp) { |
| 146 | if (of_property_read_u32(sysclknp, "reg", &id)) |
| 147 | continue; |
| 148 | |
| 149 | if (of_property_read_string(np, "clock-output-names", &name)) |
| 150 | name = sysclknp->name; |
| 151 | |
| 152 | parent_name = of_clk_get_parent_name(sysclknp, 0); |
| 153 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 154 | hw = at91_clk_register_system(regmap, name, parent_name, id); |
| 155 | if (IS_ERR(hw)) |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 156 | continue; |
| 157 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 158 | of_clk_add_hw_provider(sysclknp, of_clk_hw_simple_get, hw); |
Boris BREZILLON | 5fba62e | 2013-10-11 11:41:41 +0200 | [diff] [blame] | 159 | } |
| 160 | } |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 161 | CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system", |
| 162 | of_at91rm9200_clk_sys_setup); |