drm/exynos: Stop using drm_framebuffer_unregister_private
[linux/fpc-iii.git] / drivers / gpu / drm / sun4i / sun4i_dotclock.c
blobd401156490f36c890f49f23d1eab1b3f7691d108
1 /*
2 * Copyright (C) 2016 Free Electrons
3 * Copyright (C) 2016 NextThing Co
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
13 #include <linux/clk-provider.h>
14 #include <linux/regmap.h>
16 #include "sun4i_tcon.h"
17 #include "sun4i_dotclock.h"
19 struct sun4i_dclk {
20 struct clk_hw hw;
21 struct regmap *regmap;
24 static inline struct sun4i_dclk *hw_to_dclk(struct clk_hw *hw)
26 return container_of(hw, struct sun4i_dclk, hw);
29 static void sun4i_dclk_disable(struct clk_hw *hw)
31 struct sun4i_dclk *dclk = hw_to_dclk(hw);
33 regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
34 BIT(SUN4I_TCON0_DCLK_GATE_BIT), 0);
37 static int sun4i_dclk_enable(struct clk_hw *hw)
39 struct sun4i_dclk *dclk = hw_to_dclk(hw);
41 return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
42 BIT(SUN4I_TCON0_DCLK_GATE_BIT),
43 BIT(SUN4I_TCON0_DCLK_GATE_BIT));
46 static int sun4i_dclk_is_enabled(struct clk_hw *hw)
48 struct sun4i_dclk *dclk = hw_to_dclk(hw);
49 u32 val;
51 regmap_read(dclk->regmap, SUN4I_TCON0_DCLK_REG, &val);
53 return val & BIT(SUN4I_TCON0_DCLK_GATE_BIT);
56 static unsigned long sun4i_dclk_recalc_rate(struct clk_hw *hw,
57 unsigned long parent_rate)
59 struct sun4i_dclk *dclk = hw_to_dclk(hw);
60 u32 val;
62 regmap_read(dclk->regmap, SUN4I_TCON0_DCLK_REG, &val);
64 val >>= SUN4I_TCON0_DCLK_DIV_SHIFT;
65 val &= (1 << SUN4I_TCON0_DCLK_DIV_WIDTH) - 1;
67 if (!val)
68 val = 1;
70 return parent_rate / val;
73 static long sun4i_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
74 unsigned long *parent_rate)
76 unsigned long best_parent = 0;
77 u8 best_div = 1;
78 int i;
80 for (i = 6; i <= 127; i++) {
81 unsigned long ideal = rate * i;
82 unsigned long rounded;
84 rounded = clk_hw_round_rate(clk_hw_get_parent(hw),
85 ideal);
87 if (rounded == ideal) {
88 best_parent = rounded;
89 best_div = i;
90 goto out;
93 if (abs(rate - rounded / i) <
94 abs(rate - best_parent / best_div)) {
95 best_parent = rounded;
96 best_div = i;
100 out:
101 *parent_rate = best_parent;
103 return best_parent / best_div;
106 static int sun4i_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
107 unsigned long parent_rate)
109 struct sun4i_dclk *dclk = hw_to_dclk(hw);
110 u8 div = parent_rate / rate;
112 return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
113 GENMASK(6, 0), div);
116 static int sun4i_dclk_get_phase(struct clk_hw *hw)
118 struct sun4i_dclk *dclk = hw_to_dclk(hw);
119 u32 val;
121 regmap_read(dclk->regmap, SUN4I_TCON0_IO_POL_REG, &val);
123 val >>= 28;
124 val &= 3;
126 return val * 120;
129 static int sun4i_dclk_set_phase(struct clk_hw *hw, int degrees)
131 struct sun4i_dclk *dclk = hw_to_dclk(hw);
133 regmap_update_bits(dclk->regmap, SUN4I_TCON0_IO_POL_REG,
134 GENMASK(29, 28),
135 degrees / 120);
137 return 0;
140 static const struct clk_ops sun4i_dclk_ops = {
141 .disable = sun4i_dclk_disable,
142 .enable = sun4i_dclk_enable,
143 .is_enabled = sun4i_dclk_is_enabled,
145 .recalc_rate = sun4i_dclk_recalc_rate,
146 .round_rate = sun4i_dclk_round_rate,
147 .set_rate = sun4i_dclk_set_rate,
149 .get_phase = sun4i_dclk_get_phase,
150 .set_phase = sun4i_dclk_set_phase,
153 int sun4i_dclk_create(struct device *dev, struct sun4i_tcon *tcon)
155 const char *clk_name, *parent_name;
156 struct clk_init_data init;
157 struct sun4i_dclk *dclk;
158 int ret;
160 parent_name = __clk_get_name(tcon->sclk0);
161 ret = of_property_read_string_index(dev->of_node,
162 "clock-output-names", 0,
163 &clk_name);
164 if (ret)
165 return ret;
167 dclk = devm_kzalloc(dev, sizeof(*dclk), GFP_KERNEL);
168 if (!dclk)
169 return -ENOMEM;
171 init.name = clk_name;
172 init.ops = &sun4i_dclk_ops;
173 init.parent_names = &parent_name;
174 init.num_parents = 1;
175 init.flags = CLK_SET_RATE_PARENT;
177 dclk->regmap = tcon->regs;
178 dclk->hw.init = &init;
180 tcon->dclk = clk_register(dev, &dclk->hw);
181 if (IS_ERR(tcon->dclk))
182 return PTR_ERR(tcon->dclk);
184 return 0;
186 EXPORT_SYMBOL(sun4i_dclk_create);
188 int sun4i_dclk_free(struct sun4i_tcon *tcon)
190 clk_unregister(tcon->dclk);
191 return 0;
193 EXPORT_SYMBOL(sun4i_dclk_free);