089-clk-bcm-Add-driver-for-BCM53573-ILP-clock.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. From bd8dd593f7d2211f2273e05741d157b0c8d020ae Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  3. Date: Tue, 13 Sep 2016 09:06:04 +0200
  4. Subject: [PATCH] clk: bcm: Add driver for BCM53573 ILP clock
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. This clock is present on BCM53573 devices (including BCM47189) that use
  9. Cortex-A7. ILP is a part of PMU (Power Management Unit) multi-function
  10. device so we use syscon (and regmap) for it.
  11. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  12. Acked-by: Rob Herring <robh@kernel.org>
  13. [sboyd@codeaurora.org: Remove 0 from clk_init_data to silence sparse]
  14. Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
  15. ---
  16. .../bindings/clock/brcm,bcm53573-ilp.txt | 36 +++++
  17. drivers/clk/bcm/Makefile | 1 +
  18. drivers/clk/bcm/clk-bcm53573-ilp.c | 148 +++++++++++++++++++++
  19. 3 files changed, 185 insertions(+)
  20. create mode 100644 Documentation/devicetree/bindings/clock/brcm,bcm53573-ilp.txt
  21. create mode 100644 drivers/clk/bcm/clk-bcm53573-ilp.c
  22. --- /dev/null
  23. +++ b/Documentation/devicetree/bindings/clock/brcm,bcm53573-ilp.txt
  24. @@ -0,0 +1,36 @@
  25. +Broadcom BCM53573 ILP clock
  26. +===========================
  27. +
  28. +This binding uses the common clock binding:
  29. + Documentation/devicetree/bindings/clock/clock-bindings.txt
  30. +
  31. +This binding is used for ILP clock (sometimes referred as "slow clock")
  32. +on Broadcom BCM53573 devices using Cortex-A7 CPU.
  33. +
  34. +ILP's rate has to be calculated on runtime and it depends on ALP clock
  35. +which has to be referenced.
  36. +
  37. +This clock is part of PMU (Power Management Unit), a Broadcom's device
  38. +handing power-related aspects. Its node must be sub-node of the PMU
  39. +device.
  40. +
  41. +Required properties:
  42. +- compatible: "brcm,bcm53573-ilp"
  43. +- clocks: has to reference an ALP clock
  44. +- #clock-cells: should be <0>
  45. +- clock-output-names: from common clock bindings, should contain clock
  46. + name
  47. +
  48. +Example:
  49. +
  50. +pmu@18012000 {
  51. + compatible = "simple-mfd", "syscon";
  52. + reg = <0x18012000 0x00001000>;
  53. +
  54. + ilp {
  55. + compatible = "brcm,bcm53573-ilp";
  56. + clocks = <&alp>;
  57. + #clock-cells = <0>;
  58. + clock-output-names = "ilp";
  59. + };
  60. +};
  61. --- a/drivers/clk/bcm/Makefile
  62. +++ b/drivers/clk/bcm/Makefile
  63. @@ -4,6 +4,7 @@ obj-$(CONFIG_CLK_BCM_KONA) += clk-bcm281
  64. obj-$(CONFIG_CLK_BCM_KONA) += clk-bcm21664.o
  65. obj-$(CONFIG_COMMON_CLK_IPROC) += clk-iproc-armpll.o clk-iproc-pll.o clk-iproc-asiu.o
  66. obj-$(CONFIG_ARCH_BCM2835) += clk-bcm2835.o
  67. +obj-$(CONFIG_ARCH_BCM_53573) += clk-bcm53573-ilp.o
  68. obj-$(CONFIG_COMMON_CLK_IPROC) += clk-ns2.o
  69. obj-$(CONFIG_ARCH_BCM_CYGNUS) += clk-cygnus.o
  70. obj-$(CONFIG_ARCH_BCM_NSP) += clk-nsp.o
  71. --- /dev/null
  72. +++ b/drivers/clk/bcm/clk-bcm53573-ilp.c
  73. @@ -0,0 +1,148 @@
  74. +/*
  75. + * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
  76. + *
  77. + * This program is free software; you can redistribute it and/or modify
  78. + * it under the terms of the GNU General Public License version 2 as
  79. + * published by the Free Software Foundation.
  80. + */
  81. +
  82. +#include <linux/clk-provider.h>
  83. +#include <linux/err.h>
  84. +#include <linux/io.h>
  85. +#include <linux/mfd/syscon.h>
  86. +#include <linux/of.h>
  87. +#include <linux/of_address.h>
  88. +#include <linux/regmap.h>
  89. +#include <linux/slab.h>
  90. +
  91. +#define PMU_XTAL_FREQ_RATIO 0x66c
  92. +#define XTAL_ALP_PER_4ILP 0x00001fff
  93. +#define XTAL_CTL_EN 0x80000000
  94. +#define PMU_SLOW_CLK_PERIOD 0x6dc
  95. +
  96. +struct bcm53573_ilp {
  97. + struct clk_hw hw;
  98. + struct regmap *regmap;
  99. +};
  100. +
  101. +static int bcm53573_ilp_enable(struct clk_hw *hw)
  102. +{
  103. + struct bcm53573_ilp *ilp = container_of(hw, struct bcm53573_ilp, hw);
  104. +
  105. + regmap_write(ilp->regmap, PMU_SLOW_CLK_PERIOD, 0x10199);
  106. + regmap_write(ilp->regmap, 0x674, 0x10000);
  107. +
  108. + return 0;
  109. +}
  110. +
  111. +static void bcm53573_ilp_disable(struct clk_hw *hw)
  112. +{
  113. + struct bcm53573_ilp *ilp = container_of(hw, struct bcm53573_ilp, hw);
  114. +
  115. + regmap_write(ilp->regmap, PMU_SLOW_CLK_PERIOD, 0);
  116. + regmap_write(ilp->regmap, 0x674, 0);
  117. +}
  118. +
  119. +static unsigned long bcm53573_ilp_recalc_rate(struct clk_hw *hw,
  120. + unsigned long parent_rate)
  121. +{
  122. + struct bcm53573_ilp *ilp = container_of(hw, struct bcm53573_ilp, hw);
  123. + struct regmap *regmap = ilp->regmap;
  124. + u32 last_val, cur_val;
  125. + int sum = 0, num = 0, loop_num = 0;
  126. + int avg;
  127. +
  128. + /* Enable measurement */
  129. + regmap_write(regmap, PMU_XTAL_FREQ_RATIO, XTAL_CTL_EN);
  130. +
  131. + /* Read initial value */
  132. + regmap_read(regmap, PMU_XTAL_FREQ_RATIO, &last_val);
  133. + last_val &= XTAL_ALP_PER_4ILP;
  134. +
  135. + /*
  136. + * At minimum we should loop for a bit to let hardware do the
  137. + * measurement. This isn't very accurate however, so for a better
  138. + * precision lets try getting 20 different values for and use average.
  139. + */
  140. + while (num < 20) {
  141. + regmap_read(regmap, PMU_XTAL_FREQ_RATIO, &cur_val);
  142. + cur_val &= XTAL_ALP_PER_4ILP;
  143. +
  144. + if (cur_val != last_val) {
  145. + /* Got different value, use it */
  146. + sum += cur_val;
  147. + num++;
  148. + loop_num = 0;
  149. + last_val = cur_val;
  150. + } else if (++loop_num > 5000) {
  151. + /* Same value over and over, give up */
  152. + sum += cur_val;
  153. + num++;
  154. + break;
  155. + }
  156. +
  157. + cpu_relax();
  158. + }
  159. +
  160. + /* Disable measurement to save power */
  161. + regmap_write(regmap, PMU_XTAL_FREQ_RATIO, 0x0);
  162. +
  163. + avg = sum / num;
  164. +
  165. + return parent_rate * 4 / avg;
  166. +}
  167. +
  168. +static const struct clk_ops bcm53573_ilp_clk_ops = {
  169. + .enable = bcm53573_ilp_enable,
  170. + .disable = bcm53573_ilp_disable,
  171. + .recalc_rate = bcm53573_ilp_recalc_rate,
  172. +};
  173. +
  174. +static void bcm53573_ilp_init(struct device_node *np)
  175. +{
  176. + struct bcm53573_ilp *ilp;
  177. + struct clk_init_data init = { };
  178. + const char *parent_name;
  179. + int err;
  180. +
  181. + ilp = kzalloc(sizeof(*ilp), GFP_KERNEL);
  182. + if (!ilp)
  183. + return;
  184. +
  185. + parent_name = of_clk_get_parent_name(np, 0);
  186. + if (!parent_name) {
  187. + err = -ENOENT;
  188. + goto err_free_ilp;
  189. + }
  190. +
  191. + ilp->regmap = syscon_node_to_regmap(of_get_parent(np));
  192. + if (IS_ERR(ilp->regmap)) {
  193. + err = PTR_ERR(ilp->regmap);
  194. + goto err_free_ilp;
  195. + }
  196. +
  197. + init.name = np->name;
  198. + init.ops = &bcm53573_ilp_clk_ops;
  199. + init.parent_names = &parent_name;
  200. + init.num_parents = 1;
  201. +
  202. + ilp->hw.init = &init;
  203. + err = clk_hw_register(NULL, &ilp->hw);
  204. + if (err)
  205. + goto err_free_ilp;
  206. +
  207. + err = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &ilp->hw);
  208. + if (err)
  209. + goto err_clk_hw_unregister;
  210. +
  211. + return;
  212. +
  213. +err_clk_hw_unregister:
  214. + clk_hw_unregister(&ilp->hw);
  215. +err_free_ilp:
  216. + kfree(ilp);
  217. + pr_err("Failed to init ILP clock: %d\n", err);
  218. +}
  219. +
  220. +/* We need it very early for arch code, before device model gets ready */
  221. +CLK_OF_DECLARE(bcm53573_ilp_clk, "brcm,bcm53573-ilp", bcm53573_ilp_init);