Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 */
5
6#include <linux/clk-provider.h>
7#include <linux/clkdev.h>
8#include <linux/clk/at91_pmc.h>
9#include <linux/of.h>
10#include <linux/mfd/syscon.h>
11#include <linux/regmap.h>
12
13#include "pmc.h"
14
15#define SYSTEM_MAX_ID 31
16
17#define SYSTEM_MAX_NAME_SZ 32
18
19#define to_clk_system(hw) container_of(hw, struct clk_system, hw)
20struct clk_system {
21 struct clk_hw hw;
22 struct regmap *regmap;
23 struct at91_clk_pms pms;
24 u8 id;
25};
26
27static inline int is_pck(int id)
28{
29 return (id >= 8) && (id <= 15);
30}
31
32static inline bool clk_system_ready(struct regmap *regmap, int id)
33{
34 unsigned int status;
35
36 regmap_read(regmap, AT91_PMC_SR, &status);
37
38 return !!(status & (1 << id));
39}
40
41static int clk_system_prepare(struct clk_hw *hw)
42{
43 struct clk_system *sys = to_clk_system(hw);
44
45 regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
46
47 if (!is_pck(sys->id))
48 return 0;
49
50 while (!clk_system_ready(sys->regmap, sys->id))
51 cpu_relax();
52
53 return 0;
54}
55
56static void clk_system_unprepare(struct clk_hw *hw)
57{
58 struct clk_system *sys = to_clk_system(hw);
59
60 regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
61}
62
63static int clk_system_is_prepared(struct clk_hw *hw)
64{
65 struct clk_system *sys = to_clk_system(hw);
66 unsigned int status;
67
68 regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
69
70 if (!(status & (1 << sys->id)))
71 return 0;
72
73 if (!is_pck(sys->id))
74 return 1;
75
76 regmap_read(sys->regmap, AT91_PMC_SR, &status);
77
78 return !!(status & (1 << sys->id));
79}
80
81static int clk_system_save_context(struct clk_hw *hw)
82{
83 struct clk_system *sys = to_clk_system(hw);
84
85 sys->pms.status = clk_system_is_prepared(hw);
86
87 return 0;
88}
89
90static void clk_system_restore_context(struct clk_hw *hw)
91{
92 struct clk_system *sys = to_clk_system(hw);
93
94 if (sys->pms.status)
95 clk_system_prepare(&sys->hw);
96}
97
98static const struct clk_ops system_ops = {
99 .prepare = clk_system_prepare,
100 .unprepare = clk_system_unprepare,
101 .is_prepared = clk_system_is_prepared,
102 .save_context = clk_system_save_context,
103 .restore_context = clk_system_restore_context,
104};
105
106struct clk_hw * __init
107at91_clk_register_system(struct regmap *regmap, const char *name,
108 const char *parent_name, struct clk_hw *parent_hw, u8 id,
109 unsigned long flags)
110{
111 struct clk_system *sys;
112 struct clk_hw *hw;
113 struct clk_init_data init = {};
114 int ret;
115
116 if (!(parent_name || parent_hw) || id > SYSTEM_MAX_ID)
117 return ERR_PTR(-EINVAL);
118
119 sys = kzalloc(sizeof(*sys), GFP_KERNEL);
120 if (!sys)
121 return ERR_PTR(-ENOMEM);
122
123 init.name = name;
124 init.ops = &system_ops;
125 if (parent_hw)
126 init.parent_hws = (const struct clk_hw **)&parent_hw;
127 else
128 init.parent_names = &parent_name;
129 init.num_parents = 1;
130 init.flags = CLK_SET_RATE_PARENT | flags;
131
132 sys->id = id;
133 sys->hw.init = &init;
134 sys->regmap = regmap;
135
136 hw = &sys->hw;
137 ret = clk_hw_register(NULL, &sys->hw);
138 if (ret) {
139 kfree(sys);
140 hw = ERR_PTR(ret);
141 }
142
143 return hw;
144}
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>
15#include <linux/mfd/syscon.h>
16#include <linux/regmap.h>
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)
25struct clk_system {
26 struct clk_hw hw;
27 struct regmap *regmap;
28 u8 id;
29};
30
31static inline int is_pck(int id)
32{
33 return (id >= 8) && (id <= 15);
34}
35
36static 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
45static int clk_system_prepare(struct clk_hw *hw)
46{
47 struct clk_system *sys = to_clk_system(hw);
48
49 regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
50
51 if (!is_pck(sys->id))
52 return 0;
53
54 while (!clk_system_ready(sys->regmap, sys->id))
55 cpu_relax();
56
57 return 0;
58}
59
60static void clk_system_unprepare(struct clk_hw *hw)
61{
62 struct clk_system *sys = to_clk_system(hw);
63
64 regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
65}
66
67static int clk_system_is_prepared(struct clk_hw *hw)
68{
69 struct clk_system *sys = to_clk_system(hw);
70 unsigned int status;
71
72 regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
73
74 if (!(status & (1 << sys->id)))
75 return 0;
76
77 if (!is_pck(sys->id))
78 return 1;
79
80 regmap_read(sys->regmap, AT91_PMC_SR, &status);
81
82 return status & (1 << sys->id) ? 1 : 0;
83}
84
85static const struct clk_ops system_ops = {
86 .prepare = clk_system_prepare,
87 .unprepare = clk_system_unprepare,
88 .is_prepared = clk_system_is_prepared,
89};
90
91static struct clk * __init
92at91_clk_register_system(struct regmap *regmap, const char *name,
93 const char *parent_name, u8 id)
94{
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;
110 init.flags = CLK_SET_RATE_PARENT;
111
112 sys->id = id;
113 sys->hw.init = &init;
114 sys->regmap = regmap;
115
116 clk = clk_register(NULL, &sys->hw);
117 if (IS_ERR(clk))
118 kfree(sys);
119
120 return clk;
121}
122
123static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
124{
125 int num;
126 u32 id;
127 struct clk *clk;
128 const char *name;
129 struct device_node *sysclknp;
130 const char *parent_name;
131 struct regmap *regmap;
132
133 num = of_get_child_count(np);
134 if (num > (SYSTEM_MAX_ID + 1))
135 return;
136
137 regmap = syscon_node_to_regmap(of_get_parent(np));
138 if (IS_ERR(regmap))
139 return;
140
141 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
150 clk = at91_clk_register_system(regmap, name, parent_name, id);
151 if (IS_ERR(clk))
152 continue;
153
154 of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
155 }
156}
157CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
158 of_at91rm9200_clk_sys_setup);