clk-gpio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - https://www.ti.com
  4. *
  5. * Authors:
  6. * Jyri Sarha <[email protected]>
  7. * Sergej Sawazki <[email protected]>
  8. *
  9. * Gpio controlled clock implementation
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/export.h>
  13. #include <linux/slab.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/err.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of_device.h>
  19. /**
  20. * DOC: basic gpio gated clock which can be enabled and disabled
  21. * with gpio output
  22. * Traits of this clock:
  23. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  24. * enable - clk_enable and clk_disable are functional & control gpio
  25. * rate - inherits rate from parent. No clk_set_rate support
  26. * parent - fixed parent. No clk_set_parent support
  27. */
  28. /**
  29. * struct clk_gpio - gpio gated clock
  30. *
  31. * @hw: handle between common and hardware-specific interfaces
  32. * @gpiod: gpio descriptor
  33. *
  34. * Clock with a gpio control for enabling and disabling the parent clock
  35. * or switching between two parents by asserting or deasserting the gpio.
  36. *
  37. * Implements .enable, .disable and .is_enabled or
  38. * .get_parent, .set_parent and .determine_rate depending on which clk_ops
  39. * is used.
  40. */
  41. struct clk_gpio {
  42. struct clk_hw hw;
  43. struct gpio_desc *gpiod;
  44. };
  45. #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
  46. static int clk_gpio_gate_enable(struct clk_hw *hw)
  47. {
  48. struct clk_gpio *clk = to_clk_gpio(hw);
  49. gpiod_set_value(clk->gpiod, 1);
  50. return 0;
  51. }
  52. static void clk_gpio_gate_disable(struct clk_hw *hw)
  53. {
  54. struct clk_gpio *clk = to_clk_gpio(hw);
  55. gpiod_set_value(clk->gpiod, 0);
  56. }
  57. static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
  58. {
  59. struct clk_gpio *clk = to_clk_gpio(hw);
  60. return gpiod_get_value(clk->gpiod);
  61. }
  62. static const struct clk_ops clk_gpio_gate_ops = {
  63. .enable = clk_gpio_gate_enable,
  64. .disable = clk_gpio_gate_disable,
  65. .is_enabled = clk_gpio_gate_is_enabled,
  66. };
  67. static int clk_sleeping_gpio_gate_prepare(struct clk_hw *hw)
  68. {
  69. struct clk_gpio *clk = to_clk_gpio(hw);
  70. gpiod_set_value_cansleep(clk->gpiod, 1);
  71. return 0;
  72. }
  73. static void clk_sleeping_gpio_gate_unprepare(struct clk_hw *hw)
  74. {
  75. struct clk_gpio *clk = to_clk_gpio(hw);
  76. gpiod_set_value_cansleep(clk->gpiod, 0);
  77. }
  78. static int clk_sleeping_gpio_gate_is_prepared(struct clk_hw *hw)
  79. {
  80. struct clk_gpio *clk = to_clk_gpio(hw);
  81. return gpiod_get_value_cansleep(clk->gpiod);
  82. }
  83. static const struct clk_ops clk_sleeping_gpio_gate_ops = {
  84. .prepare = clk_sleeping_gpio_gate_prepare,
  85. .unprepare = clk_sleeping_gpio_gate_unprepare,
  86. .is_prepared = clk_sleeping_gpio_gate_is_prepared,
  87. };
  88. /**
  89. * DOC: basic clock multiplexer which can be controlled with a gpio output
  90. * Traits of this clock:
  91. * prepare - clk_prepare only ensures that parents are prepared
  92. * rate - rate is only affected by parent switching. No clk_set_rate support
  93. * parent - parent is adjustable through clk_set_parent
  94. */
  95. static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
  96. {
  97. struct clk_gpio *clk = to_clk_gpio(hw);
  98. return gpiod_get_value_cansleep(clk->gpiod);
  99. }
  100. static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
  101. {
  102. struct clk_gpio *clk = to_clk_gpio(hw);
  103. gpiod_set_value_cansleep(clk->gpiod, index);
  104. return 0;
  105. }
  106. static const struct clk_ops clk_gpio_mux_ops = {
  107. .get_parent = clk_gpio_mux_get_parent,
  108. .set_parent = clk_gpio_mux_set_parent,
  109. .determine_rate = __clk_mux_determine_rate,
  110. };
  111. static struct clk_hw *clk_register_gpio(struct device *dev, u8 num_parents,
  112. struct gpio_desc *gpiod,
  113. const struct clk_ops *clk_gpio_ops)
  114. {
  115. struct clk_gpio *clk_gpio;
  116. struct clk_hw *hw;
  117. struct clk_init_data init = {};
  118. int err;
  119. const struct clk_parent_data gpio_parent_data[] = {
  120. { .index = 0 },
  121. { .index = 1 },
  122. };
  123. clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
  124. if (!clk_gpio)
  125. return ERR_PTR(-ENOMEM);
  126. init.name = dev->of_node->name;
  127. init.ops = clk_gpio_ops;
  128. init.parent_data = gpio_parent_data;
  129. init.num_parents = num_parents;
  130. init.flags = CLK_SET_RATE_PARENT;
  131. clk_gpio->gpiod = gpiod;
  132. clk_gpio->hw.init = &init;
  133. hw = &clk_gpio->hw;
  134. err = devm_clk_hw_register(dev, hw);
  135. if (err)
  136. return ERR_PTR(err);
  137. return hw;
  138. }
  139. static struct clk_hw *clk_hw_register_gpio_gate(struct device *dev,
  140. int num_parents,
  141. struct gpio_desc *gpiod)
  142. {
  143. const struct clk_ops *ops;
  144. if (gpiod_cansleep(gpiod))
  145. ops = &clk_sleeping_gpio_gate_ops;
  146. else
  147. ops = &clk_gpio_gate_ops;
  148. return clk_register_gpio(dev, num_parents, gpiod, ops);
  149. }
  150. static struct clk_hw *clk_hw_register_gpio_mux(struct device *dev,
  151. struct gpio_desc *gpiod)
  152. {
  153. return clk_register_gpio(dev, 2, gpiod, &clk_gpio_mux_ops);
  154. }
  155. static int gpio_clk_driver_probe(struct platform_device *pdev)
  156. {
  157. struct device *dev = &pdev->dev;
  158. struct device_node *node = dev->of_node;
  159. const char *gpio_name;
  160. unsigned int num_parents;
  161. struct gpio_desc *gpiod;
  162. struct clk_hw *hw;
  163. bool is_mux;
  164. int ret;
  165. is_mux = of_device_is_compatible(node, "gpio-mux-clock");
  166. num_parents = of_clk_get_parent_count(node);
  167. if (is_mux && num_parents != 2) {
  168. dev_err(dev, "mux-clock must have 2 parents\n");
  169. return -EINVAL;
  170. }
  171. gpio_name = is_mux ? "select" : "enable";
  172. gpiod = devm_gpiod_get(dev, gpio_name, GPIOD_OUT_LOW);
  173. if (IS_ERR(gpiod)) {
  174. ret = PTR_ERR(gpiod);
  175. if (ret == -EPROBE_DEFER)
  176. pr_debug("%pOFn: %s: GPIOs not yet available, retry later\n",
  177. node, __func__);
  178. else
  179. pr_err("%pOFn: %s: Can't get '%s' named GPIO property\n",
  180. node, __func__,
  181. gpio_name);
  182. return ret;
  183. }
  184. if (is_mux)
  185. hw = clk_hw_register_gpio_mux(dev, gpiod);
  186. else
  187. hw = clk_hw_register_gpio_gate(dev, num_parents, gpiod);
  188. if (IS_ERR(hw))
  189. return PTR_ERR(hw);
  190. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
  191. }
  192. static const struct of_device_id gpio_clk_match_table[] = {
  193. { .compatible = "gpio-mux-clock" },
  194. { .compatible = "gpio-gate-clock" },
  195. { }
  196. };
  197. static struct platform_driver gpio_clk_driver = {
  198. .probe = gpio_clk_driver_probe,
  199. .driver = {
  200. .name = "gpio-clk",
  201. .of_match_table = gpio_clk_match_table,
  202. },
  203. };
  204. builtin_platform_driver(gpio_clk_driver);