clk-gpio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
  3. *
  4. * Authors:
  5. * Jyri Sarha <jsarha@ti.com>
  6. * Sergej Sawazki <ce3a@gmx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Gpio controlled clock implementation
  13. */
  14. #include <linux/clk-provider.h>
  15. #include <linux/export.h>
  16. #include <linux/slab.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/err.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of_device.h>
  22. /**
  23. * DOC: basic gpio gated clock which can be enabled and disabled
  24. * with gpio output
  25. * Traits of this clock:
  26. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  27. * enable - clk_enable and clk_disable are functional & control gpio
  28. * rate - inherits rate from parent. No clk_set_rate support
  29. * parent - fixed parent. No clk_set_parent support
  30. */
  31. static int clk_gpio_gate_enable(struct clk_hw *hw)
  32. {
  33. struct clk_gpio *clk = to_clk_gpio(hw);
  34. gpiod_set_value(clk->gpiod, 1);
  35. return 0;
  36. }
  37. static void clk_gpio_gate_disable(struct clk_hw *hw)
  38. {
  39. struct clk_gpio *clk = to_clk_gpio(hw);
  40. gpiod_set_value(clk->gpiod, 0);
  41. }
  42. static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
  43. {
  44. struct clk_gpio *clk = to_clk_gpio(hw);
  45. return gpiod_get_value(clk->gpiod);
  46. }
  47. const struct clk_ops clk_gpio_gate_ops = {
  48. .enable = clk_gpio_gate_enable,
  49. .disable = clk_gpio_gate_disable,
  50. .is_enabled = clk_gpio_gate_is_enabled,
  51. };
  52. EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
  53. /**
  54. * DOC: basic clock multiplexer which can be controlled with a gpio output
  55. * Traits of this clock:
  56. * prepare - clk_prepare only ensures that parents are prepared
  57. * rate - rate is only affected by parent switching. No clk_set_rate support
  58. * parent - parent is adjustable through clk_set_parent
  59. */
  60. static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
  61. {
  62. struct clk_gpio *clk = to_clk_gpio(hw);
  63. return gpiod_get_value_cansleep(clk->gpiod);
  64. }
  65. static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
  66. {
  67. struct clk_gpio *clk = to_clk_gpio(hw);
  68. gpiod_set_value_cansleep(clk->gpiod, index);
  69. return 0;
  70. }
  71. const struct clk_ops clk_gpio_mux_ops = {
  72. .get_parent = clk_gpio_mux_get_parent,
  73. .set_parent = clk_gpio_mux_set_parent,
  74. .determine_rate = __clk_mux_determine_rate,
  75. };
  76. EXPORT_SYMBOL_GPL(clk_gpio_mux_ops);
  77. static struct clk_hw *clk_register_gpio(struct device *dev, const char *name,
  78. const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
  79. unsigned long flags, const struct clk_ops *clk_gpio_ops)
  80. {
  81. struct clk_gpio *clk_gpio;
  82. struct clk_hw *hw;
  83. struct clk_init_data init = {};
  84. int err;
  85. if (dev)
  86. clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
  87. else
  88. clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL);
  89. if (!clk_gpio)
  90. return ERR_PTR(-ENOMEM);
  91. init.name = name;
  92. init.ops = clk_gpio_ops;
  93. init.flags = flags | CLK_IS_BASIC;
  94. init.parent_names = parent_names;
  95. init.num_parents = num_parents;
  96. clk_gpio->gpiod = gpiod;
  97. clk_gpio->hw.init = &init;
  98. hw = &clk_gpio->hw;
  99. if (dev)
  100. err = devm_clk_hw_register(dev, hw);
  101. else
  102. err = clk_hw_register(NULL, hw);
  103. if (!err)
  104. return hw;
  105. if (!dev) {
  106. kfree(clk_gpio);
  107. }
  108. return ERR_PTR(err);
  109. }
  110. /**
  111. * clk_hw_register_gpio_gate - register a gpio clock gate with the clock
  112. * framework
  113. * @dev: device that is registering this clock
  114. * @name: name of this clock
  115. * @parent_name: name of this clock's parent
  116. * @gpiod: gpio descriptor to gate this clock
  117. * @flags: clock flags
  118. */
  119. struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
  120. const char *parent_name, struct gpio_desc *gpiod,
  121. unsigned long flags)
  122. {
  123. return clk_register_gpio(dev, name,
  124. (parent_name ? &parent_name : NULL),
  125. (parent_name ? 1 : 0), gpiod, flags,
  126. &clk_gpio_gate_ops);
  127. }
  128. EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate);
  129. struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
  130. const char *parent_name, struct gpio_desc *gpiod,
  131. unsigned long flags)
  132. {
  133. struct clk_hw *hw;
  134. hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags);
  135. if (IS_ERR(hw))
  136. return ERR_CAST(hw);
  137. return hw->clk;
  138. }
  139. EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
  140. /**
  141. * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
  142. * @dev: device that is registering this clock
  143. * @name: name of this clock
  144. * @parent_names: names of this clock's parents
  145. * @num_parents: number of parents listed in @parent_names
  146. * @gpiod: gpio descriptor to gate this clock
  147. * @flags: clock flags
  148. */
  149. struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
  150. const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
  151. unsigned long flags)
  152. {
  153. if (num_parents != 2) {
  154. pr_err("mux-clock %s must have 2 parents\n", name);
  155. return ERR_PTR(-EINVAL);
  156. }
  157. return clk_register_gpio(dev, name, parent_names, num_parents,
  158. gpiod, flags, &clk_gpio_mux_ops);
  159. }
  160. EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux);
  161. struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
  162. const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
  163. unsigned long flags)
  164. {
  165. struct clk_hw *hw;
  166. hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents,
  167. gpiod, flags);
  168. if (IS_ERR(hw))
  169. return ERR_CAST(hw);
  170. return hw->clk;
  171. }
  172. EXPORT_SYMBOL_GPL(clk_register_gpio_mux);
  173. static int gpio_clk_driver_probe(struct platform_device *pdev)
  174. {
  175. struct device_node *node = pdev->dev.of_node;
  176. const char **parent_names, *gpio_name;
  177. unsigned int num_parents;
  178. struct gpio_desc *gpiod;
  179. struct clk *clk;
  180. bool is_mux;
  181. int ret;
  182. num_parents = of_clk_get_parent_count(node);
  183. if (num_parents) {
  184. parent_names = devm_kcalloc(&pdev->dev, num_parents,
  185. sizeof(char *), GFP_KERNEL);
  186. if (!parent_names)
  187. return -ENOMEM;
  188. of_clk_parent_fill(node, parent_names, num_parents);
  189. } else {
  190. parent_names = NULL;
  191. }
  192. is_mux = of_device_is_compatible(node, "gpio-mux-clock");
  193. gpio_name = is_mux ? "select" : "enable";
  194. gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_OUT_LOW);
  195. if (IS_ERR(gpiod)) {
  196. ret = PTR_ERR(gpiod);
  197. if (ret == -EPROBE_DEFER)
  198. pr_debug("%pOFn: %s: GPIOs not yet available, retry later\n",
  199. node, __func__);
  200. else
  201. pr_err("%pOFn: %s: Can't get '%s' named GPIO property\n",
  202. node, __func__,
  203. gpio_name);
  204. return ret;
  205. }
  206. if (is_mux)
  207. clk = clk_register_gpio_mux(&pdev->dev, node->name,
  208. parent_names, num_parents, gpiod, 0);
  209. else
  210. clk = clk_register_gpio_gate(&pdev->dev, node->name,
  211. parent_names ? parent_names[0] : NULL, gpiod,
  212. 0);
  213. if (IS_ERR(clk))
  214. return PTR_ERR(clk);
  215. return of_clk_add_provider(node, of_clk_src_simple_get, clk);
  216. }
  217. static const struct of_device_id gpio_clk_match_table[] = {
  218. { .compatible = "gpio-mux-clock" },
  219. { .compatible = "gpio-gate-clock" },
  220. { }
  221. };
  222. static struct platform_driver gpio_clk_driver = {
  223. .probe = gpio_clk_driver_probe,
  224. .driver = {
  225. .name = "gpio-clk",
  226. .of_match_table = gpio_clk_match_table,
  227. },
  228. };
  229. builtin_platform_driver(gpio_clk_driver);