clk-gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 <jsarha@ti.com>
  7. * Sergej Sawazki <ce3a@gmx.de>
  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/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. /**
  21. * DOC: basic gpio gated clock which can be enabled and disabled
  22. * with gpio output
  23. * Traits of this clock:
  24. * prepare - clk_(un)prepare are functional and control a gpio that can sleep
  25. * enable - clk_enable and clk_disable are functional & control
  26. * non-sleeping gpio
  27. * rate - inherits rate from parent. No clk_set_rate support
  28. * parent - fixed parent. No clk_set_parent support
  29. */
  30. /**
  31. * struct clk_gpio - gpio gated clock
  32. *
  33. * @hw: handle between common and hardware-specific interfaces
  34. * @gpiod: gpio descriptor
  35. *
  36. * Clock with a gpio control for enabling and disabling the parent clock
  37. * or switching between two parents by asserting or deasserting the gpio.
  38. *
  39. * Implements .enable, .disable and .is_enabled or
  40. * .get_parent, .set_parent and .determine_rate depending on which clk_ops
  41. * is used.
  42. */
  43. struct clk_gpio {
  44. struct clk_hw hw;
  45. struct gpio_desc *gpiod;
  46. };
  47. #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
  48. static int clk_gpio_gate_enable(struct clk_hw *hw)
  49. {
  50. struct clk_gpio *clk = to_clk_gpio(hw);
  51. gpiod_set_value(clk->gpiod, 1);
  52. return 0;
  53. }
  54. static void clk_gpio_gate_disable(struct clk_hw *hw)
  55. {
  56. struct clk_gpio *clk = to_clk_gpio(hw);
  57. gpiod_set_value(clk->gpiod, 0);
  58. }
  59. static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
  60. {
  61. struct clk_gpio *clk = to_clk_gpio(hw);
  62. return gpiod_get_value(clk->gpiod);
  63. }
  64. static const struct clk_ops clk_gpio_gate_ops = {
  65. .enable = clk_gpio_gate_enable,
  66. .disable = clk_gpio_gate_disable,
  67. .is_enabled = clk_gpio_gate_is_enabled,
  68. };
  69. static int clk_sleeping_gpio_gate_prepare(struct clk_hw *hw)
  70. {
  71. struct clk_gpio *clk = to_clk_gpio(hw);
  72. gpiod_set_value_cansleep(clk->gpiod, 1);
  73. return 0;
  74. }
  75. static void clk_sleeping_gpio_gate_unprepare(struct clk_hw *hw)
  76. {
  77. struct clk_gpio *clk = to_clk_gpio(hw);
  78. gpiod_set_value_cansleep(clk->gpiod, 0);
  79. }
  80. static int clk_sleeping_gpio_gate_is_prepared(struct clk_hw *hw)
  81. {
  82. struct clk_gpio *clk = to_clk_gpio(hw);
  83. return gpiod_get_value_cansleep(clk->gpiod);
  84. }
  85. static const struct clk_ops clk_sleeping_gpio_gate_ops = {
  86. .prepare = clk_sleeping_gpio_gate_prepare,
  87. .unprepare = clk_sleeping_gpio_gate_unprepare,
  88. .is_prepared = clk_sleeping_gpio_gate_is_prepared,
  89. };
  90. /**
  91. * DOC: basic clock multiplexer which can be controlled with a gpio output
  92. * Traits of this clock:
  93. * prepare - clk_prepare only ensures that parents are prepared
  94. * rate - rate is only affected by parent switching. No clk_set_rate support
  95. * parent - parent is adjustable through clk_set_parent
  96. */
  97. static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
  98. {
  99. struct clk_gpio *clk = to_clk_gpio(hw);
  100. return gpiod_get_value_cansleep(clk->gpiod);
  101. }
  102. static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
  103. {
  104. struct clk_gpio *clk = to_clk_gpio(hw);
  105. gpiod_set_value_cansleep(clk->gpiod, index);
  106. return 0;
  107. }
  108. static const struct clk_ops clk_gpio_mux_ops = {
  109. .get_parent = clk_gpio_mux_get_parent,
  110. .set_parent = clk_gpio_mux_set_parent,
  111. .determine_rate = __clk_mux_determine_rate,
  112. };
  113. static struct clk_hw *clk_register_gpio(struct device *dev, u8 num_parents,
  114. struct gpio_desc *gpiod,
  115. const struct clk_ops *clk_gpio_ops)
  116. {
  117. struct clk_gpio *clk_gpio;
  118. struct clk_hw *hw;
  119. struct clk_init_data init = {};
  120. int err;
  121. const struct clk_parent_data gpio_parent_data[] = {
  122. { .index = 0 },
  123. { .index = 1 },
  124. };
  125. clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
  126. if (!clk_gpio)
  127. return ERR_PTR(-ENOMEM);
  128. init.name = dev->of_node->name;
  129. init.ops = clk_gpio_ops;
  130. init.parent_data = gpio_parent_data;
  131. init.num_parents = num_parents;
  132. init.flags = CLK_SET_RATE_PARENT;
  133. clk_gpio->gpiod = gpiod;
  134. clk_gpio->hw.init = &init;
  135. hw = &clk_gpio->hw;
  136. err = devm_clk_hw_register(dev, hw);
  137. if (err)
  138. return ERR_PTR(err);
  139. return hw;
  140. }
  141. static struct clk_hw *clk_hw_register_gpio_gate(struct device *dev,
  142. int num_parents,
  143. struct gpio_desc *gpiod)
  144. {
  145. const struct clk_ops *ops;
  146. if (gpiod_cansleep(gpiod))
  147. ops = &clk_sleeping_gpio_gate_ops;
  148. else
  149. ops = &clk_gpio_gate_ops;
  150. return clk_register_gpio(dev, num_parents, gpiod, ops);
  151. }
  152. static struct clk_hw *clk_hw_register_gpio_mux(struct device *dev,
  153. struct gpio_desc *gpiod)
  154. {
  155. return clk_register_gpio(dev, 2, gpiod, &clk_gpio_mux_ops);
  156. }
  157. static int gpio_clk_driver_probe(struct platform_device *pdev)
  158. {
  159. struct device *dev = &pdev->dev;
  160. struct device_node *node = dev->of_node;
  161. const char *gpio_name;
  162. unsigned int num_parents;
  163. struct gpio_desc *gpiod;
  164. struct clk_hw *hw;
  165. bool is_mux;
  166. is_mux = of_device_is_compatible(node, "gpio-mux-clock");
  167. num_parents = of_clk_get_parent_count(node);
  168. if (is_mux && num_parents != 2) {
  169. dev_err(dev, "mux-clock must have 2 parents\n");
  170. return -EINVAL;
  171. }
  172. gpio_name = is_mux ? "select" : "enable";
  173. gpiod = devm_gpiod_get(dev, gpio_name, GPIOD_OUT_LOW);
  174. if (IS_ERR(gpiod))
  175. return dev_err_probe(dev, PTR_ERR(gpiod),
  176. "Can't get '%s' named GPIO property\n", gpio_name);
  177. if (is_mux)
  178. hw = clk_hw_register_gpio_mux(dev, gpiod);
  179. else
  180. hw = clk_hw_register_gpio_gate(dev, num_parents, gpiod);
  181. if (IS_ERR(hw))
  182. return PTR_ERR(hw);
  183. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, hw);
  184. }
  185. static const struct of_device_id gpio_clk_match_table[] = {
  186. { .compatible = "gpio-mux-clock" },
  187. { .compatible = "gpio-gate-clock" },
  188. { }
  189. };
  190. static struct platform_driver gpio_clk_driver = {
  191. .probe = gpio_clk_driver_probe,
  192. .driver = {
  193. .name = "gpio-clk",
  194. .of_match_table = gpio_clk_match_table,
  195. },
  196. };
  197. builtin_platform_driver(gpio_clk_driver);
  198. /**
  199. * DOC: gated fixed clock, controlled with a gpio output and a regulator
  200. * Traits of this clock:
  201. * prepare - clk_prepare and clk_unprepare are function & control regulator
  202. * optionally a gpio that can sleep
  203. * enable - clk_enable and clk_disable are functional & control gpio
  204. * rate - rate is fixed and set on clock registration
  205. * parent - fixed clock is a root clock and has no parent
  206. */
  207. /**
  208. * struct clk_gated_fixed - Gateable fixed rate clock
  209. * @clk_gpio: instance of clk_gpio for gate-gpio
  210. * @supply: supply regulator
  211. * @rate: fixed rate
  212. */
  213. struct clk_gated_fixed {
  214. struct clk_gpio clk_gpio;
  215. struct regulator *supply;
  216. unsigned long rate;
  217. };
  218. #define to_clk_gated_fixed(_clk_gpio) container_of(_clk_gpio, struct clk_gated_fixed, clk_gpio)
  219. static unsigned long clk_gated_fixed_recalc_rate(struct clk_hw *hw,
  220. unsigned long parent_rate)
  221. {
  222. return to_clk_gated_fixed(to_clk_gpio(hw))->rate;
  223. }
  224. static int clk_gated_fixed_prepare(struct clk_hw *hw)
  225. {
  226. struct clk_gated_fixed *clk = to_clk_gated_fixed(to_clk_gpio(hw));
  227. if (!clk->supply)
  228. return 0;
  229. return regulator_enable(clk->supply);
  230. }
  231. static void clk_gated_fixed_unprepare(struct clk_hw *hw)
  232. {
  233. struct clk_gated_fixed *clk = to_clk_gated_fixed(to_clk_gpio(hw));
  234. if (!clk->supply)
  235. return;
  236. regulator_disable(clk->supply);
  237. }
  238. static int clk_gated_fixed_is_prepared(struct clk_hw *hw)
  239. {
  240. struct clk_gated_fixed *clk = to_clk_gated_fixed(to_clk_gpio(hw));
  241. if (!clk->supply)
  242. return true;
  243. return regulator_is_enabled(clk->supply);
  244. }
  245. /*
  246. * Fixed gated clock with non-sleeping gpio.
  247. *
  248. * Prepare operation turns on the supply regulator
  249. * and the enable operation switches the enable-gpio.
  250. */
  251. static const struct clk_ops clk_gated_fixed_ops = {
  252. .prepare = clk_gated_fixed_prepare,
  253. .unprepare = clk_gated_fixed_unprepare,
  254. .is_prepared = clk_gated_fixed_is_prepared,
  255. .enable = clk_gpio_gate_enable,
  256. .disable = clk_gpio_gate_disable,
  257. .is_enabled = clk_gpio_gate_is_enabled,
  258. .recalc_rate = clk_gated_fixed_recalc_rate,
  259. };
  260. static int clk_sleeping_gated_fixed_prepare(struct clk_hw *hw)
  261. {
  262. int ret;
  263. ret = clk_gated_fixed_prepare(hw);
  264. if (ret)
  265. return ret;
  266. ret = clk_sleeping_gpio_gate_prepare(hw);
  267. if (ret)
  268. clk_gated_fixed_unprepare(hw);
  269. return ret;
  270. }
  271. static void clk_sleeping_gated_fixed_unprepare(struct clk_hw *hw)
  272. {
  273. clk_gated_fixed_unprepare(hw);
  274. clk_sleeping_gpio_gate_unprepare(hw);
  275. }
  276. /*
  277. * Fixed gated clock with non-sleeping gpio.
  278. *
  279. * Enabling the supply regulator and switching the enable-gpio happens
  280. * both in the prepare step.
  281. * is_prepared only needs to check the gpio state, as toggling the
  282. * gpio is the last step when preparing.
  283. */
  284. static const struct clk_ops clk_sleeping_gated_fixed_ops = {
  285. .prepare = clk_sleeping_gated_fixed_prepare,
  286. .unprepare = clk_sleeping_gated_fixed_unprepare,
  287. .is_prepared = clk_sleeping_gpio_gate_is_prepared,
  288. .recalc_rate = clk_gated_fixed_recalc_rate,
  289. };
  290. static int clk_gated_fixed_probe(struct platform_device *pdev)
  291. {
  292. struct device *dev = &pdev->dev;
  293. struct clk_gated_fixed *clk;
  294. const struct clk_ops *ops;
  295. const char *clk_name;
  296. u32 rate;
  297. int ret;
  298. clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL);
  299. if (!clk)
  300. return -ENOMEM;
  301. ret = device_property_read_u32(dev, "clock-frequency", &rate);
  302. if (ret)
  303. return dev_err_probe(dev, ret, "Failed to get clock-frequency\n");
  304. clk->rate = rate;
  305. ret = device_property_read_string(dev, "clock-output-names", &clk_name);
  306. if (ret)
  307. clk_name = fwnode_get_name(dev->fwnode);
  308. clk->supply = devm_regulator_get_optional(dev, "vdd");
  309. if (IS_ERR(clk->supply)) {
  310. if (PTR_ERR(clk->supply) != -ENODEV)
  311. return dev_err_probe(dev, PTR_ERR(clk->supply),
  312. "Failed to get regulator\n");
  313. clk->supply = NULL;
  314. }
  315. clk->clk_gpio.gpiod = devm_gpiod_get_optional(dev, "enable",
  316. GPIOD_OUT_LOW);
  317. if (IS_ERR(clk->clk_gpio.gpiod))
  318. return dev_err_probe(dev, PTR_ERR(clk->clk_gpio.gpiod),
  319. "Failed to get gpio\n");
  320. if (gpiod_cansleep(clk->clk_gpio.gpiod))
  321. ops = &clk_sleeping_gated_fixed_ops;
  322. else
  323. ops = &clk_gated_fixed_ops;
  324. clk->clk_gpio.hw.init = CLK_HW_INIT_NO_PARENT(clk_name, ops, 0);
  325. /* register the clock */
  326. ret = devm_clk_hw_register(dev, &clk->clk_gpio.hw);
  327. if (ret)
  328. return dev_err_probe(dev, ret,
  329. "Failed to register clock\n");
  330. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  331. &clk->clk_gpio.hw);
  332. if (ret)
  333. return dev_err_probe(dev, ret,
  334. "Failed to register clock provider\n");
  335. return 0;
  336. }
  337. static const struct of_device_id gated_fixed_clk_match_table[] = {
  338. { .compatible = "gated-fixed-clock" },
  339. { /* sentinel */ }
  340. };
  341. static struct platform_driver gated_fixed_clk_driver = {
  342. .probe = clk_gated_fixed_probe,
  343. .driver = {
  344. .name = "gated-fixed-clk",
  345. .of_match_table = gated_fixed_clk_match_table,
  346. },
  347. };
  348. builtin_platform_driver(gated_fixed_clk_driver);