clk-gpio-gate.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * @gpio: gpio number to gate this clock
  57. * @active_low: true if gpio should be set to 0 to enable clock
  58. * @flags: clock flags
  59. */
  60. struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
  61. const char *parent_name, unsigned gpio, bool active_low,
  62. unsigned long flags)
  63. {
  64. struct clk_gpio *clk_gpio = NULL;
  65. struct clk *clk = ERR_PTR(-EINVAL);
  66. struct clk_init_data init = { NULL };
  67. unsigned long gpio_flags;
  68. int err;
  69. if (active_low)
  70. gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_HIGH;
  71. else
  72. gpio_flags = GPIOF_OUT_INIT_LOW;
  73. if (dev)
  74. err = devm_gpio_request_one(dev, gpio, gpio_flags, name);
  75. else
  76. err = gpio_request_one(gpio, gpio_flags, name);
  77. if (err) {
  78. pr_err("%s: %s: Error requesting clock control gpio %u\n",
  79. __func__, name, gpio);
  80. return ERR_PTR(err);
  81. }
  82. if (dev)
  83. clk_gpio = devm_kzalloc(dev, sizeof(struct clk_gpio),
  84. GFP_KERNEL);
  85. else
  86. clk_gpio = kzalloc(sizeof(struct clk_gpio), GFP_KERNEL);
  87. if (!clk_gpio) {
  88. clk = ERR_PTR(-ENOMEM);
  89. goto clk_register_gpio_gate_err;
  90. }
  91. init.name = name;
  92. init.ops = &clk_gpio_gate_ops;
  93. init.flags = flags | CLK_IS_BASIC;
  94. init.parent_names = (parent_name ? &parent_name : NULL);
  95. init.num_parents = (parent_name ? 1 : 0);
  96. clk_gpio->gpiod = gpio_to_desc(gpio);
  97. clk_gpio->hw.init = &init;
  98. clk = clk_register(dev, &clk_gpio->hw);
  99. if (!IS_ERR(clk))
  100. return clk;
  101. if (!dev)
  102. kfree(clk_gpio);
  103. clk_register_gpio_gate_err:
  104. if (!dev)
  105. gpio_free(gpio);
  106. return clk;
  107. }
  108. EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
  109. #ifdef CONFIG_OF
  110. /**
  111. * The clk_register_gpio_gate has to be delayed, because the EPROBE_DEFER
  112. * can not be handled properly at of_clk_init() call time.
  113. */
  114. struct clk_gpio_gate_delayed_register_data {
  115. struct device_node *node;
  116. struct mutex lock;
  117. struct clk *clk;
  118. };
  119. static struct clk *of_clk_gpio_gate_delayed_register_get(
  120. struct of_phandle_args *clkspec,
  121. void *_data)
  122. {
  123. struct clk_gpio_gate_delayed_register_data *data = _data;
  124. struct clk *clk;
  125. const char *clk_name = data->node->name;
  126. const char *parent_name;
  127. int gpio;
  128. enum of_gpio_flags of_flags;
  129. mutex_lock(&data->lock);
  130. if (data->clk) {
  131. mutex_unlock(&data->lock);
  132. return data->clk;
  133. }
  134. gpio = of_get_named_gpio_flags(data->node, "enable-gpios", 0,
  135. &of_flags);
  136. if (gpio < 0) {
  137. mutex_unlock(&data->lock);
  138. if (gpio != -EPROBE_DEFER)
  139. pr_err("%s: %s: Can't get 'enable-gpios' DT property\n",
  140. __func__, clk_name);
  141. return ERR_PTR(gpio);
  142. }
  143. parent_name = of_clk_get_parent_name(data->node, 0);
  144. clk = clk_register_gpio_gate(NULL, clk_name, parent_name, gpio,
  145. of_flags & OF_GPIO_ACTIVE_LOW, 0);
  146. if (IS_ERR(clk)) {
  147. mutex_unlock(&data->lock);
  148. return clk;
  149. }
  150. data->clk = clk;
  151. mutex_unlock(&data->lock);
  152. return clk;
  153. }
  154. /**
  155. * of_gpio_gate_clk_setup() - Setup function for gpio controlled clock
  156. */
  157. void __init of_gpio_gate_clk_setup(struct device_node *node)
  158. {
  159. struct clk_gpio_gate_delayed_register_data *data;
  160. data = kzalloc(sizeof(struct clk_gpio_gate_delayed_register_data),
  161. GFP_KERNEL);
  162. if (!data)
  163. return;
  164. data->node = node;
  165. mutex_init(&data->lock);
  166. of_clk_add_provider(node, of_clk_gpio_gate_delayed_register_get, data);
  167. }
  168. EXPORT_SYMBOL_GPL(of_gpio_gate_clk_setup);
  169. CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup);
  170. #endif