Skip to contentSkip to conversation

driver/clk/clk-si5338: Add common clock framework driver for si5338

PATCH Superseded York Sun 2 participants 3 messages Oct 7, 2015 – Oct 22, 2015

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

York Sun

author
#0 lore raw

PATCH v6 1/1 driver/clk/clk-si5338: Add common clock framework driver for si5338

SI5338 is a programmable clock generator. It has 4 sets of inputs, PLL, multisynth and dividers to make 4 outputs. This driver splits them into multiple clocks to comply with common clock framework.

See Documentation/devicetree/bindings/clock/silabs,si5338.txt for details.

CC Mike Turquette <[email protected]>
CC Sebastian Hesselbarth <[email protected]>
CC Guenter Roeck <[email protected]>
CC Andrey Filippov <[email protected]>
CC Paul Bolle <[email protected]>

---
Change log:
v6: Fix multiline comments style
Add msleep in loop of polling registers
Deal with function return error consistently Remove unnecessary memset calls
Fix calling strncat
Use IS_ERR_OR_NULL when applicable
Add a local function devm_of_clk_get()
Remove clk_put in remove function
Use clkout name in device tree and platform data if set Add warning if name is too long for clkdev
s/if (a == 0)/if (!a)
s/if (a != 0)/if (a)
Remove "if (a) a = 1" and alike
Add dealing with -EPROBE_DEFER for of_clk_get() Prepare the clocks before calling post init as required by HW Update module license to GPL v2 be consistent with header

v5: Rebase to v4.2
Move clk-si5338.h file into clk-si5338.c
Drop unused macros
Add macro HWDATA to replace container_of
Collapse functions for using bsearch
Use return inside switch {}
Separate hardware setting from recalc
Rename variable ms and p123
Remove casting for returns and write_reg
Change device_attr to S_IRUGO
Drop sysfs, use debugfs for output status
Drop exporting __clk_is_prepared
Add "enabled" in device tree binding if the clocks need to be
enabled when driver is loaded
Add enable/disable ops for clock output

v4: Add binding silabs,pll-vco
Set pll rate initial value
Separate COMMON_CLK change from this patch

v3: Add calling unprepare upon removal
Add registering to clkdev so the clk can be acquired when device
tree is not in use
Add a dev_info message when driver is removed
Add missing "static" to two functions in clk-si5338.c Cosmatic fix in dt-bindings.clock/clk-si5338.h

v2: Fix handling name prefix if the driver is unloaded and loaded again

---
 .../devicetree/bindings/clock/silabs,si5338.txt    |  188 +
 drivers/clk/Kconfig                                |   12 +
 drivers/clk/Makefile                               |    1 +
 drivers/clk/clk-si5338.c                           | 3826 ++++++++++++++++++++
 include/dt-bindings/clock/clk-si5338.h             |   68 +
 include/linux/platform_data/si5338.h               |   52 +
 6 files changed, 4147 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/silabs,si5338.txt
 create mode 100644 drivers/clk/clk-si5338.c
 create mode 100644 include/dt-bindings/clock/clk-si5338.h
 create mode 100644 include/linux/platform_data/si5338.h
Documentation/devicetree/bindings/clock/silabs,si5338.txt new file +188 -0
new file mode 100644
index 0000000..943ac3d
@@ -0,0 +1,188 @@
+Binding for Silicon Labs Si5338 programmable i2c clock generator.
+
+Reference
+[1] Si5338 Data Sheet
+    http://www.silabs.com/Support%20Documents/TechnicalDocs/Si5338.pdf
+
+The Si5338 is a programmable i2c clock generators with up to 4 output
+clocks. It has 4 sets of possible input clocks
+
+IN1/IN2: differential
+IN3: single-ended
+IN4: single-ended
+IN5/IN6: differential
+
+Additionally, IN1/IN2 can be used as XTAL with different setting.
+The clock tree looks like below (without support of zero-delay)
+
+
+      IN1/IN2 IN3         IN4 IN5/IN6
+         |     |           |     |
+   ------|     |           |     |
+   |     |     |           |     |
+   |     \     /           \     /
+   |      \   /             \   /
+   |       \ /               \ /
+ XTAL     REFCLK            FBCLK
+   |       |  \             /   |
+   |       |   \           /    |
+   |       | DIVREFCLK DIVFBCLK |
+   |       |     \       /      |
+   |       |      \     /       |
+   |       |       \   /        |
+   |       |        PLL         |
+   |       |      / | | \       |
+   |       |     /  / \  \      |
+   |       |    /  /   \  \     |
+   |       |   /   |   |   \    |
+   |       |   |   |   |   |    |
+   |       |  MS0 MS1 MS2 MS3   |
+   |       |   |   |   |   |    |
+
+       OUT0  OUT1  OUT2  OUT3
+
+The output clock can choose from any of the above clock as its source, with
+exceptions: MS1 can only be used for OUT1, MS2 can only be used for OUT2, MS3
+can only be used for OUT3.
+
+==I2C device node==
+
+Required properties:
+- compatible: shall be "silabs,si5338".
+- reg: i2c device address, shall be 0x60, 0x61, 0x70, or 0x71
+- #clock-cells: shall be set to 1 for multiple outputs
+- clocks: list of parent clocks in the order of <xtal>, <in1/2>, <in3>, <in4>, <in5/6>
+          Note, xtal and in1/2 are mutually exclusive. Only one can be set.
+- #address-cells: shall be set to 1.
+- #size-cells: shall be set to 0.
+
+Optional properties if not set by platform driver:
+- silab,name-prefix: name prefix for si5338
+        If multiple si5338 chips exist, use name-prefix to form unique names.
+	If omitted, i2c bus name will be used as the prefix.
+- silab,ref-source: source of refclk, valid value is defined as
+	#define SI5338_REF_SRC_CLKIN12          0
+	#define SI5338_REF_SRC_CLKIN3           1
+	#define SI5338_REF_SRC_XTAL             4
+- silab,fb-source:  source of fbclk, valid value is defined as
+	#define SI5338_FB_SRC_CLKIN4            2
+	#define SI5338_FB_SRC_CLKIN56           3
+	#define SI5338_FB_SRC_NOCLK             5
+- silabs,pll-source: source of pll, valid value is defined as
+	#define SI5338_PFD_IN_REF_REFCLK           0
+	#define SI5338_PFD_IN_REF_FBCLK            1
+	#define SI5338_PFD_IN_REF_DIVREFCLK        2
+	#define SI5338_PFD_IN_REF_DIVFBCLK         3
+	#define SI5338_PFD_IN_REF_XOCLK            4
+	#define SI5338_PFD_IN_REF_NOCLK            5
+- silabs,pll-master: Pick one MS (0, 1, 2, or 3) to allow chaning PLL rate
+	This is arbitrary since MS0/1/2/3 share one PLL.  PLL can be calculated
+	backward to satisfy MS.
+- silabs,pll-vco: Specify VCO frequency for optimal ratios for all outputs.
+	If specified, silabs,pll-master is ignored.
+
+==Child nodes==
+
+Each of the clock outputs can be configured individually by
+using a child node to the I2C device node. If a child node for a clock
+output is not set, platform driver has to set up.
+
+Required child node properties:
+- name: name for the child node
+        It has to be unique. The name prefix is ignored.
+	If using platform data and the name is not specificed,
+        clkout0/1/2/3 will be used with name prefix.
+- reg: number of clock output.
+
+Optional child node properties:
+- silabs,drive-config: the configuration of output driver
+  The valid value list is long. Please refer to soruce code.
+- silabs,clock-source: source clock of the output divider
+	#define SI5338_OUT_MUX_FBCLK            0
+	#define SI5338_OUT_MUX_REFCLK           1
+	#deinfe SI5338_OUT_MUX_DIVFBCLK         2
+	#deinfe SI5338_OUT_MUX_DIVREFCLK        3
+	#deinfe SI5338_OUT_MUX_XOCLK            4
+	#deinfe SI5338_OUT_MUX_MS0              5
+	#deinfe SI5338_OUT_MUX_MSN              6 /* MS0/1/2/3 */
+	#deinfe SI5338_OUT_MUX_NOCLK            7
+- silabs,disable-state : clock output disable state, shall be
+	#define SI5338_OUT_DIS_HIZ              0
+	#define SI5338_OUT_DIS_LOW              1
+	#define SI5338_OUT_DIS_HI               2
+	#define SI5338_OUT_DIS_ALWAYS_ON        3
+- enabled: the output is enabled by default
+	The existence of this node enables the output when the driver is loaded
+	otherwise the clock is only enabled when used
+
+==Example==
+
+/* 25MHz reference crystal */
+ref25: ref25M {
+	compatible = "fixed-clock";
+	#clock-cells = <0>;
+	clock-frequency = <25000000>;
+};
+clkin56: ref100M {
+	compatible = "fixed-clock";
+	#clock-cells = <0>;
+	clock-frequency = <100000000>;
+};
+i2c-master-node {
+	si5338: clock-generator@70 {
+		compatible = "silabs,si5338";
+		reg = <0x70>;
+		#address-cells = <1>;
+		#size-cells = <0>;
+		#clock-cells = <1>;
+
+		/* connect xtal to 25MHz, in5/in6 to 100MHz */
+		clocks = <&ref25>, <>, <>, <>, <&clkin56>;
+
+		/* connect xtal as source of refclk */
+		silab,ref-source = <SI5338_REF_SRC_XTAL>;
+
+		/* connect in5/in6 as source of fbclk */
+		silab,fb-source = <SI5338_FB_SRC_CLKIN56>;
+
+		/* connect divrefclk as source of pll */
+		silab,pll-source = <SI5338_PFD_IN_REF_DIVREFCLK>;
+
+		/* Choose one MS for pll master */
+		silabs,pll-master = <0>;
+
+		/* Specify pll-vco frequency. pll-master is ignored. */
+		silabs,pll-vco = <2450000000>;
+
+		/* output */
+		clkout0 {
+			reg = <0>;
+			silabs,drive-config = "1V8_LVDS";
+			silabs,clock-source = <SI5338_OUT_MUX_MS0>;
+			silabs,disable-state = <SI5338_OUT_DIS_HIZ>;
+			clock-frequency = <125000000>;
+		};
+		clkout1 {
+			reg = <1>;
+			silabs,drive-config = "1V8_LVDS";
+			silabs,clock-source = <SI5338_OUT_MUX_MSN>;
+			silabs,disable-state = <SI5338_OUT_DIS_HIZ>;
+			clock-frequency = <125000000>;
+		};
+		clkout2 {
+			reg = <2>;
+			silabs,drive-config = "1V8_LVDS";
+			silabs,clock-source = <SI5338_OUT_MUX_MSN>;
+			silabs,disable-state = <SI5338_OUT_DIS_HIZ>;
+			clock-frequency = <125000000>;
+		};
+		clkout3 {
+			reg = <3>;
+			silabs,drive-config = "1V8_LVDS";
+			silabs,clock-source = <SI5338_OUT_MUX_MSN>;
+			silabs,disable-state = <SI5338_OUT_DIS_HIZ>;
+			clock-frequency = <125000000>;
+		};
+
+	};
+};
drivers/clk/Kconfig +12 -0
index 0269a1c..f10c5d9 100644
@@ -59,6 +59,18 @@ config COMMON_CLK_RK808
 	  clocked at 32KHz each. Clkout1 is always on, Clkout2 can off
 	  by control register.
 
+config COMMON_CLK_SI5338
+	tristate "Clock driver for SiLabs 5338"
+	depends on I2C
+	select REGMAP_I2C
+	select RATIONAL
+	---help---
+	  This driver supports Silicon Labs 5338 programmable clock generators,
+	  using common clock framework. It needs parent clock as input(s).
+	  Internal clocks are registered with unique names in case multiple
+	  devices exist. See devicetree/bindings/clock/silabs,si5338.txt
+	  under Documentation for details.
+
 config COMMON_CLK_SI5351
 	tristate "Clock driver for SiLabs 5351A/B/C"
 	depends on I2C
drivers/clk/Makefile +1 -0
index c4cf075..f7e5b8d 100644
@@ -36,6 +36,7 @@ obj-$(CONFIG_COMMON_CLK_PALMAS)		+= clk-palmas.o
 obj-$(CONFIG_CLK_QORIQ)			+= clk-qoriq.o
 obj-$(CONFIG_COMMON_CLK_RK808)		+= clk-rk808.o
 obj-$(CONFIG_COMMON_CLK_S2MPS11)	+= clk-s2mps11.o
+obj-$(CONFIG_COMMON_CLK_SI5338)		+= clk-si5338.o
 obj-$(CONFIG_COMMON_CLK_SI5351)		+= clk-si5351.o
 obj-$(CONFIG_COMMON_CLK_SI570)		+= clk-si570.o
 obj-$(CONFIG_COMMON_CLK_CDCE925)	+= clk-cdce925.o
drivers/clk/clk-si5338.c new file +3826 -0

2,787 lines not on this page · raw message

include/dt-bindings/clock/clk-si5338.h new file +68 -0

68 lines not on this page · raw message

include/linux/platform_data/si5338.h new file +52 -0

52 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.9.5

Part of the diff is not shown here. View the raw message.

#1 lore raw

Hello York Sun,

Quoting York Sun (2015-10-07 10:59:10)

Review

Quoted 2 lines
(quoted diff) +2 -0
+static const struct clk_ops si5338_xtal_ops =3D {
+       .prepare =3D si5338_xtal_prepare,

There are many instances of .prepare where there is no matching .unprepare. Why is that? And what are these .prepare callbacks doing? Are they changing rates or just enabling the clock signals?

Review

Quoted 5 lines
(quoted diff) +5 -0
+static const struct clk_ops si5338_clkout_ops =3D {
+       .prepare =3D si5338_clkout_prepare,
+       .unprepare =3D si5338_clkout_unprepare,
+       .enable =3D si5338_clkout_enable,
+       .disable =3D si5338_clkout_disable,

This is an i2c device, so I'm confused how an .enable or .disable callback is appropriate.

drivers/clk/clk-si5338.c

Quoted 3 lines
drivers/clk/clk-si5338.c new file +3 -0
+       /*
+        * To form clock names, concatentate name prefix with each name.
+        * The result string is up to MAX_NAME_LENGTH including terminati=

on.

drivers/clk/clk-si5338.c

Quoted 5 lines
drivers/clk/clk-si5338.c new file +5 -0
+        */
+
+       /* Register xtal input clock */
+       if (!IS_ERR_OR_NULL(drvdata->pxtal)) {
+               strlcpy(register_name, drvdata->name_prefix, MAX_NAME_PRE=

FIX);

Quoted 1 line

+ strncat(register_name, si5338_input_names[4], STRNCAT_LEN=

GTH);

Quoted 1 line

+ drvdata->pxtal_name =3D __clk_get_name(drvdata->pxtal);

I don't really understand what's going on here. Why is this necessary? Are you not able to get this info from DT?

Regards,
Mike

author
#2 lore raw
Quoted 9 lines · Michael Turquette

Hello York Sun,

Quoting York Sun (2015-10-07 10:59:10)

drivers/clk/clk-si5338.c line 739

Quoted 2 lines
drivers/clk/clk-si5338.c new file +2 -0
+static const struct clk_ops si5338_xtal_ops = {
+       .prepare = si5338_xtal_prepare,

There are many instances of .prepare where there is no matching .unprepare. Why is that? And what are these .prepare callbacks doing? Are they changing rates or just enabling the clock signals?

These prepare functions set hardware according to their parent clock rate. For example, xtal has different setting if the oscillator frequency is above 26MHz, under 11MHz, and between.

There is no need to unprepare them.

Quoted 9 lines

drivers/clk/clk-si5338.c line 2671

Quoted 6 lines
drivers/clk/clk-si5338.c new file +5 -0
+static const struct clk_ops si5338_clkout_ops = {
+       .prepare = si5338_clkout_prepare,
+       .unprepare = si5338_clkout_unprepare,
+       .enable = si5338_clkout_enable,
+       .disable = si5338_clkout_disable,

This is an i2c device, so I'm confused how an .enable or .disable callback is appropriate.

This clock chip can disable individual clock output, and internal module. User can select various state for disabled clock, i.e. high, low, high-Z, etc. At least, disabling unused clkout saves power. Make sense?

Quoted 12 lines

drivers/clk/clk-si5338.c line 3490

Quoted 11 lines
drivers/clk/clk-si5338.c new file +10 -0
+       /*
+        * To form clock names, concatentate name prefix with each name.
+        * The result string is up to MAX_NAME_LENGTH including termination.
+        */
+
+       /* Register xtal input clock */
+       if (!IS_ERR_OR_NULL(drvdata->pxtal)) {
+               strlcpy(register_name, drvdata->name_prefix, MAX_NAME_PREFIX);
+               strncat(register_name, si5338_input_names[4], STRNCAT_LENGTH);
+               drvdata->pxtal_name = __clk_get_name(drvdata->pxtal);
Quoted 2 lines

I don't really understand what's going on here. Why is this necessary? Are you not able to get this info from DT?

I write this driver with two things in mind. One is device tree, the other is platform data. If device tree is used, everything comes from device tree. Otherwise, the data has to come from platform data. The code you are seeing is to create unique clock names with prefix, passed from either device tree, or platform data. This is needed when multiple chips exist. I am using a system with four identical si5338 chips on one card, with up to 15 cards per system.

I am open to suggestion to do things other ways.

York