Skip to content

Commit ea11dda

Browse files
jbrun3tsuperna9999
authored andcommitted
clk: meson: add regmap clocks
Meson clock controllers need to move the classical iomem registers to regmap. This is triggered because the HHI controllers found on the GXBB and GXL host more than just clocks. To properly handle this, we would like to migrate HHI to syscon. Also GXBB AO clock controller already use regmap, AXG AO and Audio clock controllers will as well. The purpose of this change is to provide a common structure to these meson controllers (and possibly others) for regmap based clocks. This change provides the basic gate, mux and divider, based on the helpers provided by the related generic clocks Signed-off-by: Jerome Brunet <[email protected]> Signed-off-by: Neil Armstrong <[email protected]>
1 parent 7b174c5 commit ea11dda

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed

drivers/clk/meson/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ config COMMON_CLK_AMLOGIC
33
depends on OF
44
depends on ARCH_MESON || COMPILE_TEST
55

6+
config COMMON_CLK_REGMAP_MESON
7+
bool
8+
select REGMAP
9+
610
config COMMON_CLK_MESON8B
711
bool
812
depends on COMMON_CLK_AMLOGIC

drivers/clk/meson/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ obj-$(CONFIG_COMMON_CLK_AMLOGIC) += clk-pll.o clk-cpu.o clk-mpll.o clk-audio-div
66
obj-$(CONFIG_COMMON_CLK_MESON8B) += meson8b.o
77
obj-$(CONFIG_COMMON_CLK_GXBB) += gxbb.o gxbb-aoclk.o gxbb-aoclk-regmap.o gxbb-aoclk-32k.o
88
obj-$(CONFIG_COMMON_CLK_AXG) += axg.o
9+
obj-$(CONFIG_COMMON_CLK_REGMAP_MESON) += clk-regmap.o

drivers/clk/meson/clk-regmap.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2018 BayLibre, SAS.
3+
// Author: Jerome Brunet <[email protected]>
4+
5+
#include "clk-regmap.h"
6+
7+
static int clk_regmap_gate_endisable(struct clk_hw *hw, int enable)
8+
{
9+
struct clk_regmap *clk = to_clk_regmap(hw);
10+
struct clk_regmap_gate_data *gate = clk_get_regmap_gate_data(clk);
11+
int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
12+
13+
set ^= enable;
14+
15+
return regmap_update_bits(clk->map, gate->offset, BIT(gate->bit_idx),
16+
set ? BIT(gate->bit_idx) : 0);
17+
}
18+
19+
static int clk_regmap_gate_enable(struct clk_hw *hw)
20+
{
21+
return clk_regmap_gate_endisable(hw, 1);
22+
}
23+
24+
static void clk_regmap_gate_disable(struct clk_hw *hw)
25+
{
26+
clk_regmap_gate_endisable(hw, 0);
27+
}
28+
29+
static int clk_regmap_gate_is_enabled(struct clk_hw *hw)
30+
{
31+
struct clk_regmap *clk = to_clk_regmap(hw);
32+
struct clk_regmap_gate_data *gate = clk_get_regmap_gate_data(clk);
33+
unsigned int val;
34+
35+
regmap_read(clk->map, gate->offset, &val);
36+
if (gate->flags & CLK_GATE_SET_TO_DISABLE)
37+
val ^= BIT(gate->bit_idx);
38+
39+
val &= BIT(gate->bit_idx);
40+
41+
return val ? 1 : 0;
42+
}
43+
44+
const struct clk_ops clk_regmap_gate_ops = {
45+
.enable = clk_regmap_gate_enable,
46+
.disable = clk_regmap_gate_disable,
47+
.is_enabled = clk_regmap_gate_is_enabled,
48+
};
49+
EXPORT_SYMBOL_GPL(clk_regmap_gate_ops);
50+
51+
static unsigned long clk_regmap_div_recalc_rate(struct clk_hw *hw,
52+
unsigned long prate)
53+
{
54+
struct clk_regmap *clk = to_clk_regmap(hw);
55+
struct clk_regmap_div_data *div = clk_get_regmap_div_data(clk);
56+
unsigned int val;
57+
int ret;
58+
59+
ret = regmap_read(clk->map, div->offset, &val);
60+
if (ret)
61+
/* Gives a hint that something is wrong */
62+
return 0;
63+
64+
val >>= div->shift;
65+
val &= clk_div_mask(div->width);
66+
return divider_recalc_rate(hw, prate, val, div->table, div->flags,
67+
div->width);
68+
}
69+
70+
static long clk_regmap_div_round_rate(struct clk_hw *hw, unsigned long rate,
71+
unsigned long *prate)
72+
{
73+
struct clk_regmap *clk = to_clk_regmap(hw);
74+
struct clk_regmap_div_data *div = clk_get_regmap_div_data(clk);
75+
unsigned int val;
76+
int ret;
77+
78+
/* if read only, just return current value */
79+
if (div->flags & CLK_DIVIDER_READ_ONLY) {
80+
ret = regmap_read(clk->map, div->offset, &val);
81+
if (ret)
82+
/* Gives a hint that something is wrong */
83+
return 0;
84+
85+
val >>= div->shift;
86+
val &= clk_div_mask(div->width);
87+
88+
return divider_ro_round_rate(hw, rate, prate, div->table,
89+
div->width, div->flags, val);
90+
}
91+
92+
return divider_round_rate(hw, rate, prate, div->table, div->width,
93+
div->flags);
94+
}
95+
96+
static int clk_regmap_div_set_rate(struct clk_hw *hw, unsigned long rate,
97+
unsigned long parent_rate)
98+
{
99+
struct clk_regmap *clk = to_clk_regmap(hw);
100+
struct clk_regmap_div_data *div = clk_get_regmap_div_data(clk);
101+
unsigned int val;
102+
int ret;
103+
104+
ret = divider_get_val(rate, parent_rate, div->table, div->width,
105+
div->flags);
106+
if (ret < 0)
107+
return ret;
108+
109+
val = (unsigned int)ret << div->shift;
110+
return regmap_update_bits(clk->map, div->offset,
111+
clk_div_mask(div->width) << div->shift, val);
112+
};
113+
114+
/* Would prefer clk_regmap_div_ro_ops but clashes with qcom */
115+
116+
const struct clk_ops clk_regmap_divider_ops = {
117+
.recalc_rate = clk_regmap_div_recalc_rate,
118+
.round_rate = clk_regmap_div_round_rate,
119+
.set_rate = clk_regmap_div_set_rate,
120+
};
121+
EXPORT_SYMBOL_GPL(clk_regmap_divider_ops);
122+
123+
const struct clk_ops clk_regmap_divider_ro_ops = {
124+
.recalc_rate = clk_regmap_div_recalc_rate,
125+
.round_rate = clk_regmap_div_round_rate,
126+
};
127+
EXPORT_SYMBOL_GPL(clk_regmap_divider_ro_ops);
128+
129+
static u8 clk_regmap_mux_get_parent(struct clk_hw *hw)
130+
{
131+
struct clk_regmap *clk = to_clk_regmap(hw);
132+
struct clk_regmap_mux_data *mux = clk_get_regmap_mux_data(clk);
133+
unsigned int val;
134+
int ret;
135+
136+
ret = regmap_read(clk->map, mux->offset, &val);
137+
if (ret)
138+
return ret;
139+
140+
val >>= mux->shift;
141+
val &= mux->mask;
142+
return clk_mux_val_to_index(hw, mux->table, mux->flags, val);
143+
}
144+
145+
static int clk_regmap_mux_set_parent(struct clk_hw *hw, u8 index)
146+
{
147+
struct clk_regmap *clk = to_clk_regmap(hw);
148+
struct clk_regmap_mux_data *mux = clk_get_regmap_mux_data(clk);
149+
unsigned int val = clk_mux_index_to_val(mux->table, mux->flags, index);
150+
151+
return regmap_update_bits(clk->map, mux->offset,
152+
mux->mask << mux->shift,
153+
val << mux->shift);
154+
}
155+
156+
const struct clk_ops clk_regmap_mux_ops = {
157+
.get_parent = clk_regmap_mux_get_parent,
158+
.set_parent = clk_regmap_mux_set_parent,
159+
.determine_rate = __clk_mux_determine_rate,
160+
};
161+
EXPORT_SYMBOL_GPL(clk_regmap_mux_ops);
162+
163+
const struct clk_ops clk_regmap_mux_ro_ops = {
164+
.get_parent = clk_regmap_mux_get_parent,
165+
};
166+
EXPORT_SYMBOL_GPL(clk_regmap_mux_ro_ops);

drivers/clk/meson/clk-regmap.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2018 BayLibre, SAS.
3+
// Author: Jerome Brunet <[email protected]>
4+
5+
#ifndef __CLK_REGMAP_H
6+
#define __CLK_REGMAP_H
7+
8+
#include <linux/clk-provider.h>
9+
#include <linux/regmap.h>
10+
11+
/**
12+
* struct clk_regmap - regmap backed clock
13+
*
14+
* @hw: handle between common and hardware-specific interfaces
15+
* @map: pointer to the regmap structure controlling the clock
16+
* @data: data specific to the clock type
17+
*
18+
* Clock which is controlled by regmap backed registers. The actual type of
19+
* of the clock is controlled by the clock_ops and data.
20+
*/
21+
struct clk_regmap {
22+
struct clk_hw hw;
23+
struct regmap *map;
24+
void *data;
25+
};
26+
27+
#define to_clk_regmap(_hw) container_of(_hw, struct clk_regmap, hw)
28+
29+
/**
30+
* struct clk_regmap_gate_data - regmap backed gate specific data
31+
*
32+
* @offset: offset of the register controlling gate
33+
* @bit_idx: single bit controlling gate
34+
* @flags: hardware-specific flags
35+
*
36+
* Flags:
37+
* Same as clk_gate except CLK_GATE_HIWORD_MASK which is ignored
38+
*/
39+
struct clk_regmap_gate_data {
40+
unsigned int offset;
41+
u8 bit_idx;
42+
u8 flags;
43+
};
44+
45+
static inline struct clk_regmap_gate_data *
46+
clk_get_regmap_gate_data(struct clk_regmap *clk)
47+
{
48+
return (struct clk_regmap_gate_data *)clk->data;
49+
}
50+
51+
extern const struct clk_ops clk_regmap_gate_ops;
52+
53+
/**
54+
* struct clk_regmap_div_data - regmap backed adjustable divider specific data
55+
*
56+
* @offset: offset of the register controlling the divider
57+
* @shift: shift to the divider bit field
58+
* @width: width of the divider bit field
59+
* @table: array of value/divider pairs, last entry should have div = 0
60+
*
61+
* Flags:
62+
* Same as clk_divider except CLK_DIVIDER_HIWORD_MASK which is ignored
63+
*/
64+
struct clk_regmap_div_data {
65+
unsigned int offset;
66+
u8 shift;
67+
u8 width;
68+
u8 flags;
69+
const struct clk_div_table *table;
70+
};
71+
72+
static inline struct clk_regmap_div_data *
73+
clk_get_regmap_div_data(struct clk_regmap *clk)
74+
{
75+
return (struct clk_regmap_div_data *)clk->data;
76+
}
77+
78+
extern const struct clk_ops clk_regmap_divider_ops;
79+
extern const struct clk_ops clk_regmap_divider_ro_ops;
80+
81+
/**
82+
* struct clk_regmap_mux_data - regmap backed multiplexer clock specific data
83+
*
84+
* @hw: handle between common and hardware-specific interfaces
85+
* @offset: offset of theregister controlling multiplexer
86+
* @table: array of parent indexed register values
87+
* @shift: shift to multiplexer bit field
88+
* @mask: mask of mutliplexer bit field
89+
* @flags: hardware-specific flags
90+
*
91+
* Flags:
92+
* Same as clk_divider except CLK_MUX_HIWORD_MASK which is ignored
93+
*/
94+
struct clk_regmap_mux_data {
95+
unsigned int offset;
96+
u32 *table;
97+
u32 mask;
98+
u8 shift;
99+
u8 flags;
100+
};
101+
102+
static inline struct clk_regmap_mux_data *
103+
clk_get_regmap_mux_data(struct clk_regmap *clk)
104+
{
105+
return (struct clk_regmap_mux_data *)clk->data;
106+
}
107+
108+
extern const struct clk_ops clk_regmap_mux_ops;
109+
extern const struct clk_ops clk_regmap_mux_ro_ops;
110+
111+
#endif /* __CLK_REGMAP_H */

0 commit comments

Comments
 (0)