blob: 8f35d8172909aa5d6765bb0e4f45439fbdce19e7 [file] [log] [blame]
Boris BREZILLON5fba62e2013-10-11 11:41:41 +02001/*
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 Brezillon1bdf0232014-09-07 08:14:29 +020015#include <linux/mfd/syscon.h>
16#include <linux/regmap.h>
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020017
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)
25struct clk_system {
26 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020027 struct regmap *regmap;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020028 u8 id;
29};
30
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010031static inline int is_pck(int id)
32{
33 return (id >= 8) && (id <= 15);
34}
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010035
Boris Brezillon1bdf0232014-09-07 08:14:29 +020036static 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 Hiblotcce6db82014-03-11 10:00:34 +010045static int clk_system_prepare(struct clk_hw *hw)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020046{
47 struct clk_system *sys = to_clk_system(hw);
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020048
Boris Brezillon1bdf0232014-09-07 08:14:29 +020049 regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010050
51 if (!is_pck(sys->id))
52 return 0;
53
Alexandre Belloni99a81702015-09-16 23:47:39 +020054 while (!clk_system_ready(sys->regmap, sys->id))
55 cpu_relax();
56
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020057 return 0;
58}
59
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010060static void clk_system_unprepare(struct clk_hw *hw)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020061{
62 struct clk_system *sys = to_clk_system(hw);
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020063
Boris Brezillon1bdf0232014-09-07 08:14:29 +020064 regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020065}
66
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010067static int clk_system_is_prepared(struct clk_hw *hw)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020068{
69 struct clk_system *sys = to_clk_system(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020070 unsigned int status;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020071
Boris Brezillon1bdf0232014-09-07 08:14:29 +020072 regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
73
74 if (!(status & (1 << sys->id)))
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010075 return 0;
76
77 if (!is_pck(sys->id))
78 return 1;
79
Boris Brezillon1bdf0232014-09-07 08:14:29 +020080 regmap_read(sys->regmap, AT91_PMC_SR, &status);
81
82 return status & (1 << sys->id) ? 1 : 0;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020083}
84
85static const struct clk_ops system_ops = {
Jean-Jacques Hiblotcce6db82014-03-11 10:00:34 +010086 .prepare = clk_system_prepare,
87 .unprepare = clk_system_unprepare,
88 .is_prepared = clk_system_is_prepared,
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020089};
90
91static struct clk * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +020092at91_clk_register_system(struct regmap *regmap, const char *name,
Alexandre Belloni99a81702015-09-16 23:47:39 +020093 const char *parent_name, u8 id)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +020094{
95 struct clk_system *sys;
96 struct clk *clk = NULL;
97 struct clk_init_data init;
98
99 if (!parent_name || id > SYSTEM_MAX_ID)
100 return ERR_PTR(-EINVAL);
101
102 sys = kzalloc(sizeof(*sys), GFP_KERNEL);
103 if (!sys)
104 return ERR_PTR(-ENOMEM);
105
106 init.name = name;
107 init.ops = &system_ops;
108 init.parent_names = &parent_name;
109 init.num_parents = 1;
Alexandre Bellonib736bcb2014-07-08 18:21:16 +0200110 init.flags = CLK_SET_RATE_PARENT;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200111
112 sys->id = id;
113 sys->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200114 sys->regmap = regmap;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200115
116 clk = clk_register(NULL, &sys->hw);
Alexandre Belloni99a81702015-09-16 23:47:39 +0200117 if (IS_ERR(clk))
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200118 kfree(sys);
119
120 return clk;
121}
122
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200123static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200124{
125 int num;
126 u32 id;
127 struct clk *clk;
128 const char *name;
129 struct device_node *sysclknp;
130 const char *parent_name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200131 struct regmap *regmap;
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200132
133 num = of_get_child_count(np);
134 if (num > (SYSTEM_MAX_ID + 1))
135 return;
136
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200137 regmap = syscon_node_to_regmap(of_get_parent(np));
138 if (IS_ERR(regmap))
139 return;
140
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200141 for_each_child_of_node(np, sysclknp) {
142 if (of_property_read_u32(sysclknp, "reg", &id))
143 continue;
144
145 if (of_property_read_string(np, "clock-output-names", &name))
146 name = sysclknp->name;
147
148 parent_name = of_clk_get_parent_name(sysclknp, 0);
149
Alexandre Belloni99a81702015-09-16 23:47:39 +0200150 clk = at91_clk_register_system(regmap, name, parent_name, id);
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200151 if (IS_ERR(clk))
152 continue;
153
154 of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
155 }
156}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200157CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
158 of_at91rm9200_clk_sys_setup);