Skip to content

Commit 5edffb9

Browse files
zonquebebarino
authored andcommitted
clk: cs2000-cp: convert driver to regmap
Regmap gives us caching, debugging infrastructure and other things for free and does away with open-coded bit-fiddling implementations. Signed-off-by: Daniel Mack <daniel@zonque.org> Link: https://lore.kernel.org/r/20220125093336.226787-10-daniel@zonque.org Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 2f3d32f commit 5edffb9

File tree

2 files changed

+69
-56
lines changed

2 files changed

+69
-56
lines changed

drivers/clk/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ config COMMON_CLK_CDCE925
197197
config COMMON_CLK_CS2000_CP
198198
tristate "Clock driver for CS2000 Fractional-N Clock Synthesizer & Clock Multiplier"
199199
depends on I2C
200+
select REGMAP_I2C
200201
help
201202
If you say yes here you get support for the CS2000 clock multiplier.
202203

drivers/clk/clk-cs2000-cp.c

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/i2c.h>
1212
#include <linux/of_device.h>
1313
#include <linux/module.h>
14+
#include <linux/regmap.h>
1415

1516
#define CH_MAX 4
1617
#define RATIO_REG_SIZE 4
@@ -74,11 +75,36 @@
7475
#define REF_CLK 1
7576
#define CLK_MAX 2
7677

78+
static bool cs2000_readable_reg(struct device *dev, unsigned int reg)
79+
{
80+
return reg > 0;
81+
}
82+
83+
static bool cs2000_writeable_reg(struct device *dev, unsigned int reg)
84+
{
85+
return reg != DEVICE_ID;
86+
}
87+
88+
static bool cs2000_volatile_reg(struct device *dev, unsigned int reg)
89+
{
90+
return reg == DEVICE_CTRL;
91+
}
92+
93+
static const struct regmap_config cs2000_regmap_config = {
94+
.reg_bits = 8,
95+
.val_bits = 8,
96+
.max_register = FUNC_CFG2,
97+
.readable_reg = cs2000_readable_reg,
98+
.writeable_reg = cs2000_writeable_reg,
99+
.volatile_reg = cs2000_volatile_reg,
100+
};
101+
77102
struct cs2000_priv {
78103
struct clk_hw hw;
79104
struct i2c_client *client;
80105
struct clk *clk_in;
81106
struct clk *ref_clk;
107+
struct regmap *regmap;
82108

83109
bool dynamic_mode;
84110
bool lf_ratio;
@@ -101,41 +127,22 @@ static const struct i2c_device_id cs2000_id[] = {
101127
};
102128
MODULE_DEVICE_TABLE(i2c, cs2000_id);
103129

104-
#define cs2000_read(priv, addr) \
105-
i2c_smbus_read_byte_data(priv_to_client(priv), addr)
106-
#define cs2000_write(priv, addr, val) \
107-
i2c_smbus_write_byte_data(priv_to_client(priv), addr, val)
108-
109-
static int cs2000_bset(struct cs2000_priv *priv, u8 addr, u8 mask, u8 val)
110-
{
111-
s32 data;
112-
113-
data = cs2000_read(priv, addr);
114-
if (data < 0)
115-
return data;
116-
117-
data &= ~mask;
118-
data |= (val & mask);
119-
120-
return cs2000_write(priv, addr, data);
121-
}
122-
123130
static int cs2000_enable_dev_config(struct cs2000_priv *priv, bool enable)
124131
{
125132
int ret;
126133

127-
ret = cs2000_bset(priv, DEVICE_CFG1, ENDEV1,
128-
enable ? ENDEV1 : 0);
134+
ret = regmap_update_bits(priv->regmap, DEVICE_CFG1, ENDEV1,
135+
enable ? ENDEV1 : 0);
129136
if (ret < 0)
130137
return ret;
131138

132-
ret = cs2000_bset(priv, GLOBAL_CFG, ENDEV2,
133-
enable ? ENDEV2 : 0);
139+
ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, ENDEV2,
140+
enable ? ENDEV2 : 0);
134141
if (ret < 0)
135142
return ret;
136143

137-
ret = cs2000_bset(priv, FUNC_CFG1, CLKSKIPEN,
138-
(enable && priv->clk_skip) ? CLKSKIPEN : 0);
144+
ret = regmap_update_bits(priv->regmap, FUNC_CFG1, CLKSKIPEN,
145+
(enable && priv->clk_skip) ? CLKSKIPEN : 0);
139146
if (ret < 0)
140147
return ret;
141148

@@ -156,21 +163,21 @@ static int cs2000_ref_clk_bound_rate(struct cs2000_priv *priv,
156163
else
157164
return -EINVAL;
158165

159-
return cs2000_bset(priv, FUNC_CFG1,
160-
REFCLKDIV_MASK,
161-
REFCLKDIV(val));
166+
return regmap_update_bits(priv->regmap, FUNC_CFG1,
167+
REFCLKDIV_MASK,
168+
REFCLKDIV(val));
162169
}
163170

164171
static int cs2000_wait_pll_lock(struct cs2000_priv *priv)
165172
{
166173
struct device *dev = priv_to_dev(priv);
167-
s32 val;
168-
unsigned int i;
174+
unsigned int i, val;
175+
int ret;
169176

170177
for (i = 0; i < 256; i++) {
171-
val = cs2000_read(priv, DEVICE_CTRL);
172-
if (val < 0)
173-
return val;
178+
ret = regmap_read(priv->regmap, DEVICE_CTRL, &val);
179+
if (ret < 0)
180+
return ret;
174181
if (!(val & PLL_UNLOCK))
175182
return 0;
176183
udelay(1);
@@ -184,10 +191,10 @@ static int cs2000_wait_pll_lock(struct cs2000_priv *priv)
184191
static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable)
185192
{
186193
/* enable both AUX_OUT, CLK_OUT */
187-
return cs2000_bset(priv, DEVICE_CTRL,
188-
(AUXOUTDIS | CLKOUTDIS),
189-
enable ? 0 :
190-
(AUXOUTDIS | CLKOUTDIS));
194+
return regmap_update_bits(priv->regmap, DEVICE_CTRL,
195+
(AUXOUTDIS | CLKOUTDIS),
196+
enable ? 0 :
197+
(AUXOUTDIS | CLKOUTDIS));
191198
}
192199

193200
static u32 cs2000_rate_to_ratio(u32 rate_in, u32 rate_out, bool lf_ratio)
@@ -235,7 +242,7 @@ static int cs2000_ratio_set(struct cs2000_priv *priv,
235242

236243
val = cs2000_rate_to_ratio(rate_in, rate_out, priv->lf_ratio);
237244
for (i = 0; i < RATIO_REG_SIZE; i++) {
238-
ret = cs2000_write(priv,
245+
ret = regmap_write(priv->regmap,
239246
Ratio_Add(ch, i),
240247
Ratio_Val(val, i));
241248
if (ret < 0)
@@ -247,14 +254,14 @@ static int cs2000_ratio_set(struct cs2000_priv *priv,
247254

248255
static u32 cs2000_ratio_get(struct cs2000_priv *priv, int ch)
249256
{
250-
s32 tmp;
257+
unsigned int tmp, i;
251258
u32 val;
252-
unsigned int i;
259+
int ret;
253260

254261
val = 0;
255262
for (i = 0; i < RATIO_REG_SIZE; i++) {
256-
tmp = cs2000_read(priv, Ratio_Add(ch, i));
257-
if (tmp < 0)
263+
ret = regmap_read(priv->regmap, Ratio_Add(ch, i), &tmp);
264+
if (ret < 0)
258265
return 0;
259266

260267
val |= Val_Ratio(tmp, i);
@@ -271,15 +278,15 @@ static int cs2000_ratio_select(struct cs2000_priv *priv, int ch)
271278
if (CH_SIZE_ERR(ch))
272279
return -EINVAL;
273280

274-
ret = cs2000_bset(priv, DEVICE_CFG1, RSEL_MASK, RSEL(ch));
281+
ret = regmap_update_bits(priv->regmap, DEVICE_CFG1, RSEL_MASK, RSEL(ch));
275282
if (ret < 0)
276283
return ret;
277284

278285
fracnsrc = priv->dynamic_mode ? FRACNSRC_DYNAMIC : FRACNSRC_STATIC;
279286

280-
ret = cs2000_bset(priv, DEVICE_CFG2,
281-
AUTORMOD | LOCKCLK_MASK | FRACNSRC_MASK,
282-
LOCKCLK(ch) | fracnsrc);
287+
ret = regmap_update_bits(priv->regmap, DEVICE_CFG2,
288+
AUTORMOD | LOCKCLK_MASK | FRACNSRC_MASK,
289+
LOCKCLK(ch) | fracnsrc);
283290
if (ret < 0)
284291
return ret;
285292

@@ -326,8 +333,8 @@ static int cs2000_select_ratio_mode(struct cs2000_priv *priv,
326333
*/
327334
priv->lf_ratio = priv->dynamic_mode && ((rate / parent_rate) > 4096);
328335

329-
return cs2000_bset(priv, FUNC_CFG2, LFRATIO_MASK,
330-
priv->lf_ratio ? LFRATIO_20_12 : LFRATIO_12_20);
336+
return regmap_update_bits(priv->regmap, FUNC_CFG2, LFRATIO_MASK,
337+
priv->lf_ratio ? LFRATIO_20_12 : LFRATIO_12_20);
331338
}
332339

333340
static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
@@ -336,7 +343,7 @@ static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
336343
{
337344
int ret;
338345

339-
ret = cs2000_bset(priv, GLOBAL_CFG, FREEZE, FREEZE);
346+
ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, FREEZE, FREEZE);
340347
if (ret < 0)
341348
return ret;
342349

@@ -352,7 +359,7 @@ static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
352359
if (ret < 0)
353360
return ret;
354361

355-
ret = cs2000_bset(priv, GLOBAL_CFG, FREEZE, 0);
362+
ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, FREEZE, 0);
356363
if (ret < 0)
357364
return ret;
358365

@@ -469,8 +476,8 @@ static int cs2000_clk_register(struct cs2000_priv *priv)
469476
priv->dynamic_mode ? "dynamic" : "static");
470477

471478
of_property_read_u32(np, "cirrus,aux-output-source", &aux_out);
472-
ret = cs2000_bset(priv, DEVICE_CFG1,
473-
AUXOUTSRC_MASK, AUXOUTSRC(aux_out));
479+
ret = regmap_update_bits(priv->regmap, DEVICE_CFG1,
480+
AUXOUTSRC_MASK, AUXOUTSRC(aux_out));
474481
if (ret < 0)
475482
return ret;
476483

@@ -522,12 +529,13 @@ static int cs2000_clk_register(struct cs2000_priv *priv)
522529
static int cs2000_version_print(struct cs2000_priv *priv)
523530
{
524531
struct device *dev = priv_to_dev(priv);
525-
s32 val;
526532
const char *revision;
533+
unsigned int val;
534+
int ret;
527535

528-
val = cs2000_read(priv, DEVICE_ID);
529-
if (val < 0)
530-
return val;
536+
ret = regmap_read(priv->regmap, DEVICE_ID, &val);
537+
if (ret < 0)
538+
return ret;
531539

532540
/* CS2000 should be 0x0 */
533541
if (val >> 3)
@@ -576,6 +584,10 @@ static int cs2000_probe(struct i2c_client *client,
576584
priv->client = client;
577585
i2c_set_clientdata(client, priv);
578586

587+
priv->regmap = devm_regmap_init_i2c(client, &cs2000_regmap_config);
588+
if (IS_ERR(priv->regmap))
589+
return PTR_ERR(priv->regmap);
590+
579591
ret = cs2000_clk_get(priv);
580592
if (ret < 0)
581593
return ret;

0 commit comments

Comments
 (0)