clk-gpio-gate.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
  3. * Author: Jyri Sarha <jsarha@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Gpio gated clock implementation
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/gpio.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/err.h>
  18. #include <linux/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. #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
  29. static int clk_gpio_gate_enable(struct clk_hw *hw)
  30. {
  31. struct clk_gpio *clk = to_clk_gpio(hw);
  32. gpiod_set_value(clk->gpiod, 1);
  33. return 0;
  34. }
  35. static void clk_gpio_gate_disable(struct clk_hw *hw)
  36. {
  37. struct clk_gpio *clk = to_clk_gpio(hw);
  38. gpiod_set_value(clk->gpiod, 0);
  39. }
  40. static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
  41. {
  42. struct clk_gpio *clk = to_clk_gpio(hw);
  43. return gpiod_get_value(clk->gpiod);
  44. }
  45. const struct clk_ops clk_gpio_gate_ops = {
  46. .enable = clk_gpio_gate_enable,
  47. .disable = clk_gpio_gate_disable,
  48. .is_enabled = clk_gpio_gate_is_enabled,
  49. };
  50. EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
  51. /**
  52. * clk_register_gpio - register a gpip clock with the clock framework
  53. * @dev: device that is registering this clock
  54. * @name: name of this clock
  55. * @parent_name: name of this clock's parent
  56. * @gpiod: gpio descriptor to gate this clock
  57. */
  58. struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
  59. const char *parent_name, struct gpio_desc *gpiod,
  60. unsigned long flags)
  61. {
  62. struct clk_gpio *clk_gpio = NULL;
  63. struct clk *clk = ERR_PTR(-EINVAL);
  64. struct clk_init_data init = { NULL };
  65. unsigned long gpio_flags;
  66. int err;
  67. if (gpiod_is_active_low(gpiod))
  68. gpio_flags = GPIOF_OUT_INIT_HIGH;
  69. else
  70. gpio_flags = GPIOF_OUT_INIT_LOW;
  71. if (dev)
  72. err = devm_gpio_request_one(dev, desc_to_gpio(gpiod),
  73. gpio_flags, name);
  74. else
  75. err = gpio_request_one(desc_to_gpio(gpiod), gpio_flags, name);
  76. if (err) {
  77. pr_err("%s: %s: Error requesting clock control gpio %u\n",
  78. __func__, name, desc_to_gpio(gpiod));
  79. return ERR_PTR(err);
  80. }
  81. if (dev)
  82. clk_gpio = devm_kzalloc(dev, sizeof(struct clk_gpio),
  83. GFP_KERNEL);
  84. else
  85. clk_gpio = kzalloc(sizeof(struct clk_gpio), GFP_KERNEL);
  86. if (!clk_gpio) {
  87. clk = ERR_PTR(-ENOMEM);
  88. goto clk_register_gpio_gate_err;
  89. }
  90. init.name = name;
  91. init.ops = &clk_gpio_gate_ops;
  92. init.flags = flags | CLK_IS_BASIC;
  93. init.parent_names = (parent_name ? &parent_name : NULL);
  94. init.num_parents = (parent_name ? 1 : 0);
  95. clk_gpio->gpiod = gpiod;
  96. clk_gpio->hw.init = &init;
  97. clk = clk_register(dev, &clk_gpio->hw);
  98. if (!IS_ERR(clk))
  99. return clk;
  100. if (!dev)
  101. kfree(clk_gpio);
  102. clk_register_gpio_gate_err:
  103. gpiod_put(gpiod);
  104. return clk;
  105. }
  106. EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
  107. #ifdef CONFIG_OF
  108. /**
  109. * The clk_register_gpio_gate has to be delayed, because the EPROBE_DEFER
  110. * can not be handled properly at of_clk_init() call time.
  111. */
  112. struct clk_gpio_gate_delayed_register_data {
  113. struct device_node *node;
  114. struct mutex lock;
  115. struct clk *clk;
  116. };
  117. static struct clk *of_clk_gpio_gate_delayed_register_get(
  118. struct of_phandle_args *clkspec,
  119. void *_data)
  120. {
  121. struct clk_gpio_gate_delayed_register_data *data = _data;
  122. struct clk *clk;
  123. const char *clk_name = data->node->name;
  124. const char *parent_name;
  125. struct gpio_desc *gpiod;
  126. int gpio;
  127. mutex_lock(&data->lock);
  128. if (data->clk) {
  129. mutex_unlock(&data->lock);
  130. return data->clk;
  131. }
  132. gpio = of_get_named_gpio_flags(data->node, "enable-gpios", 0, NULL);
  133. if (gpio < 0) {
  134. mutex_unlock(&data->lock);
  135. if (gpio != -EPROBE_DEFER)
  136. pr_err("%s: %s: Can't get 'enable-gpios' DT property\n",
  137. __func__, clk_name);
  138. return ERR_PTR(gpio);
  139. }
  140. gpiod = gpio_to_desc(gpio);
  141. parent_name = of_clk_get_parent_name(data->node, 0);
  142. clk = clk_register_gpio_gate(NULL, clk_name, parent_name, gpiod, 0);
  143. if (IS_ERR(clk)) {
  144. mutex_unlock(&data->lock);
  145. return clk;
  146. }
  147. data->clk = clk;
  148. mutex_unlock(&data->lock);
  149. return clk;
  150. }
  151. /**
  152. * of_gpio_gate_clk_setup() - Setup function for gpio controlled clock
  153. */
  154. void __init of_gpio_gate_clk_setup(struct device_node *node)
  155. {
  156. struct clk_gpio_gate_delayed_register_data *data;
  157. data = kzalloc(sizeof(struct clk_gpio_gate_delayed_register_data),
  158. GFP_KERNEL);
  159. if (!data)
  160. return;
  161. data->node = node;
  162. mutex_init(&data->lock);
  163. of_clk_add_provider(node, of_clk_gpio_gate_delayed_register_get, data);
  164. }
  165. EXPORT_SYMBOL_GPL(of_gpio_gate_clk_setup);
  166. CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup);
  167. #endif