Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

h8300: clock driver

Signed-off-by: Yoshinori Sato <ysato@users.sourceforge.jp>

+250
+24
Documentation/devicetree/bindings/clock/renesas,h8300-div-clock.txt
··· 1 + * Renesas H8/300 divider clock 2 + 3 + Required Properties: 4 + 5 + - compatible: Must be "renesas,sh73a0-h8300-div-clock" 6 + 7 + - clocks: Reference to the parent clocks ("extal1" and "extal2") 8 + 9 + - #clock-cells: Must be 1 10 + 11 + - reg: Base address and length of the divide rate selector 12 + 13 + - renesas,width: bit width of selector 14 + 15 + Example 16 + ------- 17 + 18 + cclk: cclk { 19 + compatible = "renesas,h8300-div-clock"; 20 + clocks = <&xclk>; 21 + #clock-cells = <0>; 22 + reg = <0xfee01b 2>; 23 + renesas,width = <2>; 24 + };
+23
Documentation/devicetree/bindings/clock/renesas,h8s2678-pll-clock.txt
··· 1 + Renesas H8S2678 PLL clock 2 + 3 + This device is Clock multiplyer 4 + 5 + Required Properties: 6 + 7 + - compatible: Must be "renesas,h8s2678-pll-clock" 8 + 9 + - clocks: Reference to the parent clocks 10 + 11 + - #clock-cells: Must be 0 12 + 13 + - reg: Two rate selector (Multiply / Divide) register address 14 + 15 + Example 16 + ------- 17 + 18 + pllclk: pllclk { 19 + compatible = "renesas,h8s2678-pll-clock"; 20 + clocks = <&xclk>; 21 + #clock-cells = <0>; 22 + reg = <0xfee03b 2>, <0xfee045 2>; 23 + };
+1
drivers/clk/Makefile
··· 73 73 obj-$(CONFIG_COMMON_CLK_VERSATILE) += versatile/ 74 74 obj-$(CONFIG_X86) += x86/ 75 75 obj-$(CONFIG_ARCH_ZYNQ) += zynq/ 76 + obj-$(CONFIG_H8300) += h8300/
+2
drivers/clk/h8300/Makefile
··· 1 + obj-y += clk-div.o 2 + obj-$(CONFIG_H8S2678) += clk-h8s2678.o
+53
drivers/clk/h8300/clk-div.c
··· 1 + /* 2 + * H8/300 divide clock driver 3 + * 4 + * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp> 5 + */ 6 + 7 + #include <linux/clk.h> 8 + #include <linux/clkdev.h> 9 + #include <linux/clk-provider.h> 10 + #include <linux/err.h> 11 + #include <linux/of.h> 12 + #include <linux/of_address.h> 13 + 14 + static DEFINE_SPINLOCK(clklock); 15 + 16 + static void __init h8300_div_clk_setup(struct device_node *node) 17 + { 18 + unsigned int num_parents; 19 + struct clk *clk; 20 + const char *clk_name = node->name; 21 + const char *parent_name; 22 + void __iomem *divcr = NULL; 23 + int width; 24 + 25 + num_parents = of_clk_get_parent_count(node); 26 + if (num_parents < 1) { 27 + pr_err("%s: no parent found", clk_name); 28 + return; 29 + } 30 + 31 + divcr = of_iomap(node, 0); 32 + if (divcr == NULL) { 33 + pr_err("%s: failed to map divide register", clk_name); 34 + goto error; 35 + } 36 + 37 + parent_name = of_clk_get_parent_name(node, 0); 38 + of_property_read_u32(node, "renesas,width", &width); 39 + clk = clk_register_divider(NULL, clk_name, parent_name, 40 + CLK_SET_RATE_GATE, divcr, 0, width, 41 + CLK_DIVIDER_POWER_OF_TWO, &clklock); 42 + if (!IS_ERR(clk)) { 43 + of_clk_add_provider(node, of_clk_src_simple_get, clk); 44 + return; 45 + } 46 + pr_err("%s: failed to register %s div clock (%ld)\n", 47 + __func__, clk_name, PTR_ERR(clk)); 48 + error: 49 + if (divcr) 50 + iounmap(divcr); 51 + } 52 + 53 + CLK_OF_DECLARE(h8300_div_clk, "renesas,h8300-div-clock", h8300_div_clk_setup);
+147
drivers/clk/h8300/clk-h8s2678.c
··· 1 + /* 2 + * H8S2678 clock driver 3 + * 4 + * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp> 5 + */ 6 + 7 + #include <linux/clk.h> 8 + #include <linux/clkdev.h> 9 + #include <linux/clk-provider.h> 10 + #include <linux/err.h> 11 + #include <linux/device.h> 12 + #include <linux/of_address.h> 13 + 14 + static DEFINE_SPINLOCK(clklock); 15 + 16 + #define MAX_FREQ 33333333 17 + #define MIN_FREQ 8000000 18 + 19 + struct pll_clock { 20 + struct clk_hw hw; 21 + void __iomem *sckcr; 22 + void __iomem *pllcr; 23 + }; 24 + 25 + #define to_pll_clock(_hw) container_of(_hw, struct pll_clock, hw) 26 + 27 + static unsigned long pll_recalc_rate(struct clk_hw *hw, 28 + unsigned long parent_rate) 29 + { 30 + struct pll_clock *pll_clock = to_pll_clock(hw); 31 + int mul = 1 << (ctrl_inb((unsigned long)pll_clock->pllcr) & 3); 32 + 33 + return parent_rate * mul; 34 + } 35 + 36 + static long pll_round_rate(struct clk_hw *hw, unsigned long rate, 37 + unsigned long *prate) 38 + { 39 + int i, m = -1; 40 + long offset[3]; 41 + 42 + if (rate > MAX_FREQ) 43 + rate = MAX_FREQ; 44 + if (rate < MIN_FREQ) 45 + rate = MIN_FREQ; 46 + 47 + for (i = 0; i < 3; i++) 48 + offset[i] = abs(rate - (*prate * (1 << i))); 49 + for (i = 0; i < 3; i++) 50 + if (m < 0) 51 + m = i; 52 + else 53 + m = (offset[i] < offset[m])?i:m; 54 + 55 + return *prate * (1 << m); 56 + } 57 + 58 + static int pll_set_rate(struct clk_hw *hw, unsigned long rate, 59 + unsigned long parent_rate) 60 + { 61 + int pll; 62 + unsigned char val; 63 + unsigned long flags; 64 + struct pll_clock *pll_clock = to_pll_clock(hw); 65 + 66 + pll = ((rate / parent_rate) / 2) & 0x03; 67 + spin_lock_irqsave(&clklock, flags); 68 + val = ctrl_inb((unsigned long)pll_clock->sckcr); 69 + val |= 0x08; 70 + ctrl_outb(val, (unsigned long)pll_clock->sckcr); 71 + val = ctrl_inb((unsigned long)pll_clock->pllcr); 72 + val &= ~0x03; 73 + val |= pll; 74 + ctrl_outb(val, (unsigned long)pll_clock->pllcr); 75 + spin_unlock_irqrestore(&clklock, flags); 76 + return 0; 77 + } 78 + 79 + static const struct clk_ops pll_ops = { 80 + .recalc_rate = pll_recalc_rate, 81 + .round_rate = pll_round_rate, 82 + .set_rate = pll_set_rate, 83 + }; 84 + 85 + static void __init h8s2678_pll_clk_setup(struct device_node *node) 86 + { 87 + unsigned int num_parents; 88 + struct clk *clk; 89 + const char *clk_name = node->name; 90 + const char *parent_name; 91 + struct pll_clock *pll_clock; 92 + struct clk_init_data init; 93 + 94 + num_parents = of_clk_get_parent_count(node); 95 + if (num_parents < 1) { 96 + pr_err("%s: no parent found", clk_name); 97 + return; 98 + } 99 + 100 + 101 + pll_clock = kzalloc(sizeof(struct pll_clock), GFP_KERNEL); 102 + if (!pll_clock) { 103 + pr_err("%s: failed to alloc memory", clk_name); 104 + return; 105 + } 106 + 107 + pll_clock->sckcr = of_iomap(node, 0); 108 + if (pll_clock->sckcr == NULL) { 109 + pr_err("%s: failed to map divide register", clk_name); 110 + goto error; 111 + } 112 + 113 + pll_clock->pllcr = of_iomap(node, 1); 114 + if (pll_clock->pllcr == NULL) { 115 + pr_err("%s: failed to map multiply register", clk_name); 116 + goto error; 117 + } 118 + 119 + parent_name = of_clk_get_parent_name(node, 0); 120 + init.name = clk_name; 121 + init.ops = &pll_ops; 122 + init.flags = CLK_IS_BASIC; 123 + init.parent_names = &parent_name; 124 + init.num_parents = 1; 125 + pll_clock->hw.init = &init; 126 + 127 + clk = clk_register(NULL, &pll_clock->hw); 128 + if (IS_ERR(clk)) 129 + kfree(pll_clock); 130 + if (!IS_ERR(clk)) { 131 + of_clk_add_provider(node, of_clk_src_simple_get, clk); 132 + return; 133 + } 134 + pr_err("%s: failed to register %s div clock (%ld)\n", 135 + __func__, clk_name, PTR_ERR(clk)); 136 + error: 137 + if (pll_clock) { 138 + if (pll_clock->sckcr) 139 + iounmap(pll_clock->sckcr); 140 + if (pll_clock->pllcr) 141 + iounmap(pll_clock->pllcr); 142 + kfree(pll_clock); 143 + } 144 + } 145 + 146 + CLK_OF_DECLARE(h8s2678_div_clk, "renesas,h8s2678-pll-clock", 147 + h8s2678_pll_clk_setup);