1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Silicon Labs Si514 Programmable Oscillator
5 * Copyright (C) 2015 Topic Embedded Products
7 * Author: Mike Looijmans <mike.looijmans@topic.nl>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
18 #define SI514_REG_LP 0
19 #define SI514_REG_M_FRAC1 5
20 #define SI514_REG_M_FRAC2 6
21 #define SI514_REG_M_FRAC3 7
22 #define SI514_REG_M_INT_FRAC 8
23 #define SI514_REG_M_INT 9
24 #define SI514_REG_HS_DIV 10
25 #define SI514_REG_LS_HS_DIV 11
26 #define SI514_REG_OE_STATE 14
27 #define SI514_REG_RESET 128
28 #define SI514_REG_CONTROL 132
31 #define SI514_RESET_RST BIT(7)
33 #define SI514_CONTROL_FCAL BIT(0)
34 #define SI514_CONTROL_OE BIT(2)
36 #define SI514_MIN_FREQ 100000U
37 #define SI514_MAX_FREQ 250000000U
41 #define FVCO_MIN 2080000000U
42 #define FVCO_MAX 2500000000U
44 #define HS_DIV_MAX 1022
48 struct regmap
*regmap
;
49 struct i2c_client
*i2c_client
;
51 #define to_clk_si514(_hw) container_of(_hw, struct clk_si514, hw)
53 /* Multiplier/divider settings */
54 struct clk_si514_muldiv
{
55 u32 m_frac
; /* 29-bit Fractional part of multiplier M */
56 u8 m_int
; /* Integer part of multiplier M, 65..78 */
57 u8 ls_div_bits
; /* 2nd divider, as 2^x */
58 u16 hs_div
; /* 1st divider, must be even and 10<=x<=1022 */
61 /* Enables or disables the output driver */
62 static int si514_enable_output(struct clk_si514
*data
, bool enable
)
64 return regmap_update_bits(data
->regmap
, SI514_REG_CONTROL
,
65 SI514_CONTROL_OE
, enable
? SI514_CONTROL_OE
: 0);
68 static int si514_prepare(struct clk_hw
*hw
)
70 struct clk_si514
*data
= to_clk_si514(hw
);
72 return si514_enable_output(data
, true);
75 static void si514_unprepare(struct clk_hw
*hw
)
77 struct clk_si514
*data
= to_clk_si514(hw
);
79 si514_enable_output(data
, false);
82 static int si514_is_prepared(struct clk_hw
*hw
)
84 struct clk_si514
*data
= to_clk_si514(hw
);
88 err
= regmap_read(data
->regmap
, SI514_REG_CONTROL
, &val
);
92 return !!(val
& SI514_CONTROL_OE
);
95 /* Retrieve clock multiplier and dividers from hardware */
96 static int si514_get_muldiv(struct clk_si514
*data
,
97 struct clk_si514_muldiv
*settings
)
102 err
= regmap_bulk_read(data
->regmap
, SI514_REG_M_FRAC1
,
103 reg
, ARRAY_SIZE(reg
));
107 settings
->m_frac
= reg
[0] | reg
[1] << 8 | reg
[2] << 16 |
108 (reg
[3] & 0x1F) << 24;
109 settings
->m_int
= (reg
[4] & 0x3f) << 3 | reg
[3] >> 5;
110 settings
->ls_div_bits
= (reg
[6] >> 4) & 0x07;
111 settings
->hs_div
= (reg
[6] & 0x03) << 8 | reg
[5];
115 static int si514_set_muldiv(struct clk_si514
*data
,
116 struct clk_si514_muldiv
*settings
)
122 /* Calculate LP1/LP2 according to table 13 in the datasheet */
124 if (settings
->m_int
< 65 ||
125 (settings
->m_int
== 65 && settings
->m_frac
<= 139575831))
128 else if (settings
->m_int
< 67 ||
129 (settings
->m_int
== 67 && settings
->m_frac
<= 461581994))
132 else if (settings
->m_int
< 72 ||
133 (settings
->m_int
== 72 && settings
->m_frac
<= 503383578))
136 else if (settings
->m_int
< 75 ||
137 (settings
->m_int
== 75 && settings
->m_frac
<= 452724474))
142 err
= regmap_write(data
->regmap
, SI514_REG_LP
, lp
);
146 reg
[0] = settings
->m_frac
;
147 reg
[1] = settings
->m_frac
>> 8;
148 reg
[2] = settings
->m_frac
>> 16;
149 reg
[3] = settings
->m_frac
>> 24 | settings
->m_int
<< 5;
150 reg
[4] = settings
->m_int
>> 3;
151 reg
[5] = settings
->hs_div
;
152 reg
[6] = (settings
->hs_div
>> 8) | (settings
->ls_div_bits
<< 4);
154 err
= regmap_bulk_write(data
->regmap
, SI514_REG_HS_DIV
, reg
+ 5, 2);
158 * Writing to SI514_REG_M_INT_FRAC triggers the clock change, so that
159 * must be written last
161 return regmap_bulk_write(data
->regmap
, SI514_REG_M_FRAC1
, reg
, 5);
164 /* Calculate divider settings for a given frequency */
165 static int si514_calc_muldiv(struct clk_si514_muldiv
*settings
,
166 unsigned long frequency
)
173 if ((frequency
< SI514_MIN_FREQ
) || (frequency
> SI514_MAX_FREQ
))
176 /* Determine the minimum value of LS_DIV and resulting target freq. */
178 if (frequency
>= (FVCO_MIN
/ HS_DIV_MAX
))
179 settings
->ls_div_bits
= 0;
182 tmp
= 2 * HS_DIV_MAX
;
183 while (tmp
<= (HS_DIV_MAX
* 32)) {
184 if ((frequency
* tmp
) >= FVCO_MIN
)
189 settings
->ls_div_bits
= res
;
190 ls_freq
= frequency
<< res
;
193 /* Determine minimum HS_DIV, round up to even number */
194 settings
->hs_div
= DIV_ROUND_UP(FVCO_MIN
>> 1, ls_freq
) << 1;
196 /* M = LS_DIV x HS_DIV x frequency / F_XO (in fixed-point) */
197 m
= ((u64
)(ls_freq
* settings
->hs_div
) << 29) + (FXO
/ 2);
199 settings
->m_frac
= (u32
)m
& (BIT(29) - 1);
200 settings
->m_int
= (u32
)(m
>> 29);
205 /* Calculate resulting frequency given the register settings */
206 static unsigned long si514_calc_rate(struct clk_si514_muldiv
*settings
)
208 u64 m
= settings
->m_frac
| ((u64
)settings
->m_int
<< 29);
209 u32 d
= settings
->hs_div
* BIT(settings
->ls_div_bits
);
211 return ((u32
)(((m
* FXO
) + (FXO
/ 2)) >> 29)) / d
;
214 static unsigned long si514_recalc_rate(struct clk_hw
*hw
,
215 unsigned long parent_rate
)
217 struct clk_si514
*data
= to_clk_si514(hw
);
218 struct clk_si514_muldiv settings
;
221 err
= si514_get_muldiv(data
, &settings
);
223 dev_err(&data
->i2c_client
->dev
, "unable to retrieve settings\n");
227 return si514_calc_rate(&settings
);
230 static long si514_round_rate(struct clk_hw
*hw
, unsigned long rate
,
231 unsigned long *parent_rate
)
233 struct clk_si514_muldiv settings
;
239 err
= si514_calc_muldiv(&settings
, rate
);
243 return si514_calc_rate(&settings
);
247 * Update output frequency for big frequency changes (> 1000 ppm).
248 * The chip supports <1000ppm changes "on the fly", we haven't implemented
251 static int si514_set_rate(struct clk_hw
*hw
, unsigned long rate
,
252 unsigned long parent_rate
)
254 struct clk_si514
*data
= to_clk_si514(hw
);
255 struct clk_si514_muldiv settings
;
256 unsigned int old_oe_state
;
259 err
= si514_calc_muldiv(&settings
, rate
);
263 err
= regmap_read(data
->regmap
, SI514_REG_CONTROL
, &old_oe_state
);
267 si514_enable_output(data
, false);
269 err
= si514_set_muldiv(data
, &settings
);
271 return err
; /* Undefined state now, best to leave disabled */
273 /* Trigger calibration */
274 err
= regmap_write(data
->regmap
, SI514_REG_CONTROL
, SI514_CONTROL_FCAL
);
278 /* Applying a new frequency can take up to 10ms */
279 usleep_range(10000, 12000);
281 if (old_oe_state
& SI514_CONTROL_OE
)
282 si514_enable_output(data
, true);
287 static const struct clk_ops si514_clk_ops
= {
288 .prepare
= si514_prepare
,
289 .unprepare
= si514_unprepare
,
290 .is_prepared
= si514_is_prepared
,
291 .recalc_rate
= si514_recalc_rate
,
292 .round_rate
= si514_round_rate
,
293 .set_rate
= si514_set_rate
,
296 static bool si514_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
299 case SI514_REG_CONTROL
:
300 case SI514_REG_RESET
:
307 static bool si514_regmap_is_writeable(struct device
*dev
, unsigned int reg
)
311 case SI514_REG_M_FRAC1
... SI514_REG_LS_HS_DIV
:
312 case SI514_REG_OE_STATE
:
313 case SI514_REG_RESET
:
314 case SI514_REG_CONTROL
:
321 static const struct regmap_config si514_regmap_config
= {
324 .cache_type
= REGCACHE_RBTREE
,
325 .max_register
= SI514_REG_CONTROL
,
326 .writeable_reg
= si514_regmap_is_writeable
,
327 .volatile_reg
= si514_regmap_is_volatile
,
330 static int si514_probe(struct i2c_client
*client
,
331 const struct i2c_device_id
*id
)
333 struct clk_si514
*data
;
334 struct clk_init_data init
;
337 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
341 init
.ops
= &si514_clk_ops
;
343 init
.num_parents
= 0;
344 data
->hw
.init
= &init
;
345 data
->i2c_client
= client
;
347 if (of_property_read_string(client
->dev
.of_node
, "clock-output-names",
349 init
.name
= client
->dev
.of_node
->name
;
351 data
->regmap
= devm_regmap_init_i2c(client
, &si514_regmap_config
);
352 if (IS_ERR(data
->regmap
)) {
353 dev_err(&client
->dev
, "failed to allocate register map\n");
354 return PTR_ERR(data
->regmap
);
357 i2c_set_clientdata(client
, data
);
359 err
= devm_clk_hw_register(&client
->dev
, &data
->hw
);
361 dev_err(&client
->dev
, "clock registration failed\n");
364 err
= of_clk_add_hw_provider(client
->dev
.of_node
, of_clk_hw_simple_get
,
367 dev_err(&client
->dev
, "unable to add clk provider\n");
374 static int si514_remove(struct i2c_client
*client
)
376 of_clk_del_provider(client
->dev
.of_node
);
380 static const struct i2c_device_id si514_id
[] = {
384 MODULE_DEVICE_TABLE(i2c
, si514_id
);
386 static const struct of_device_id clk_si514_of_match
[] = {
387 { .compatible
= "silabs,si514" },
390 MODULE_DEVICE_TABLE(of
, clk_si514_of_match
);
392 static struct i2c_driver si514_driver
= {
395 .of_match_table
= clk_si514_of_match
,
397 .probe
= si514_probe
,
398 .remove
= si514_remove
,
399 .id_table
= si514_id
,
401 module_i2c_driver(si514_driver
);
403 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
404 MODULE_DESCRIPTION("Si514 driver");
405 MODULE_LICENSE("GPL");