blob: af9cc00d2d920a5565a3ffca2d0176bfc62b3d8b [file] [log] [blame]
Jyri Sarhac873d142014-09-05 15:21:34 +03001/*
2 * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +02003 *
4 * Authors:
5 * Jyri Sarha <jsarha@ti.com>
6 * Sergej Sawazki <ce3a@gmx.de>
Jyri Sarhac873d142014-09-05 15:21:34 +03007 *
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 *
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020012 * Gpio controlled clock implementation
Jyri Sarhac873d142014-09-05 15:21:34 +030013 */
14
15#include <linux/clk-provider.h>
Sergej Sawazkie21b08e2015-06-28 16:24:53 +020016#include <linux/export.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030017#include <linux/slab.h>
Mark Brown44b4aa92014-09-30 18:16:22 +010018#include <linux/gpio/consumer.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030019#include <linux/err.h>
20#include <linux/device.h>
Stephen Boyd14b04f22016-02-02 17:09:26 -080021#include <linux/platform_device.h>
22#include <linux/of_device.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030023
24/**
25 * DOC: basic gpio gated clock which can be enabled and disabled
26 * with gpio output
27 * Traits of this clock:
28 * prepare - clk_(un)prepare only ensures parent is (un)prepared
29 * enable - clk_enable and clk_disable are functional & control gpio
30 * rate - inherits rate from parent. No clk_set_rate support
31 * parent - fixed parent. No clk_set_parent support
32 */
33
Jyri Sarhac873d142014-09-05 15:21:34 +030034static int clk_gpio_gate_enable(struct clk_hw *hw)
35{
36 struct clk_gpio *clk = to_clk_gpio(hw);
37
38 gpiod_set_value(clk->gpiod, 1);
39
40 return 0;
41}
42
43static void clk_gpio_gate_disable(struct clk_hw *hw)
44{
45 struct clk_gpio *clk = to_clk_gpio(hw);
46
47 gpiod_set_value(clk->gpiod, 0);
48}
49
50static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
51{
52 struct clk_gpio *clk = to_clk_gpio(hw);
53
54 return gpiod_get_value(clk->gpiod);
55}
56
57const struct clk_ops clk_gpio_gate_ops = {
58 .enable = clk_gpio_gate_enable,
59 .disable = clk_gpio_gate_disable,
60 .is_enabled = clk_gpio_gate_is_enabled,
61};
62EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
63
64/**
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020065 * DOC: basic clock multiplexer which can be controlled with a gpio output
66 * Traits of this clock:
67 * prepare - clk_prepare only ensures that parents are prepared
68 * rate - rate is only affected by parent switching. No clk_set_rate support
69 * parent - parent is adjustable through clk_set_parent
70 */
71
72static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
73{
74 struct clk_gpio *clk = to_clk_gpio(hw);
75
Mike Looijmans2e838e72018-03-13 09:54:03 +010076 return gpiod_get_value_cansleep(clk->gpiod);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020077}
78
79static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
80{
81 struct clk_gpio *clk = to_clk_gpio(hw);
82
Mike Looijmans2e838e72018-03-13 09:54:03 +010083 gpiod_set_value_cansleep(clk->gpiod, index);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020084
85 return 0;
86}
87
88const struct clk_ops clk_gpio_mux_ops = {
89 .get_parent = clk_gpio_mux_get_parent,
90 .set_parent = clk_gpio_mux_set_parent,
91 .determine_rate = __clk_mux_determine_rate,
92};
93EXPORT_SYMBOL_GPL(clk_gpio_mux_ops);
94
Stephen Boydb1207432016-02-07 00:27:55 -080095static struct clk_hw *clk_register_gpio(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +020096 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
97 unsigned long flags, const struct clk_ops *clk_gpio_ops)
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020098{
99 struct clk_gpio *clk_gpio;
Stephen Boydb1207432016-02-07 00:27:55 -0800100 struct clk_hw *hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200101 struct clk_init_data init = {};
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200102 int err;
103
104 if (dev)
105 clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
106 else
107 clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL);
108
109 if (!clk_gpio)
110 return ERR_PTR(-ENOMEM);
111
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200112 init.name = name;
113 init.ops = clk_gpio_ops;
114 init.flags = flags | CLK_IS_BASIC;
115 init.parent_names = parent_names;
116 init.num_parents = num_parents;
117
Linus Walleij908a5432017-09-24 18:19:18 +0200118 clk_gpio->gpiod = gpiod;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200119 clk_gpio->hw.init = &init;
120
Stephen Boydb1207432016-02-07 00:27:55 -0800121 hw = &clk_gpio->hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200122 if (dev)
Stephen Boydb1207432016-02-07 00:27:55 -0800123 err = devm_clk_hw_register(dev, hw);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200124 else
Stephen Boydb1207432016-02-07 00:27:55 -0800125 err = clk_hw_register(NULL, hw);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200126
Stephen Boydb1207432016-02-07 00:27:55 -0800127 if (!err)
128 return hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200129
130 if (!dev) {
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200131 kfree(clk_gpio);
132 }
133
Stephen Boydb1207432016-02-07 00:27:55 -0800134 return ERR_PTR(err);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200135}
136
137/**
Stephen Boydb1207432016-02-07 00:27:55 -0800138 * clk_hw_register_gpio_gate - register a gpio clock gate with the clock
139 * framework
Jyri Sarhac873d142014-09-05 15:21:34 +0300140 * @dev: device that is registering this clock
141 * @name: name of this clock
142 * @parent_name: name of this clock's parent
Linus Walleij908a5432017-09-24 18:19:18 +0200143 * @gpiod: gpio descriptor to gate this clock
Martin Fuzzey820ad972015-03-18 14:53:17 +0100144 * @flags: clock flags
Jyri Sarhac873d142014-09-05 15:21:34 +0300145 */
Stephen Boydb1207432016-02-07 00:27:55 -0800146struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200147 const char *parent_name, struct gpio_desc *gpiod,
Jyri Sarhac873d142014-09-05 15:21:34 +0300148 unsigned long flags)
149{
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200150 return clk_register_gpio(dev, name,
151 (parent_name ? &parent_name : NULL),
Linus Walleij908a5432017-09-24 18:19:18 +0200152 (parent_name ? 1 : 0), gpiod, flags,
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200153 &clk_gpio_gate_ops);
Jyri Sarhac873d142014-09-05 15:21:34 +0300154}
Stephen Boydb1207432016-02-07 00:27:55 -0800155EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate);
156
157struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200158 const char *parent_name, struct gpio_desc *gpiod,
Stephen Boydb1207432016-02-07 00:27:55 -0800159 unsigned long flags)
160{
161 struct clk_hw *hw;
162
Linus Walleij908a5432017-09-24 18:19:18 +0200163 hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags);
Stephen Boydb1207432016-02-07 00:27:55 -0800164 if (IS_ERR(hw))
165 return ERR_CAST(hw);
166 return hw->clk;
167}
Jyri Sarhac873d142014-09-05 15:21:34 +0300168EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
169
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200170/**
Stephen Boydb1207432016-02-07 00:27:55 -0800171 * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200172 * @dev: device that is registering this clock
173 * @name: name of this clock
174 * @parent_names: names of this clock's parents
175 * @num_parents: number of parents listed in @parent_names
Linus Walleij908a5432017-09-24 18:19:18 +0200176 * @gpiod: gpio descriptor to gate this clock
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200177 * @flags: clock flags
178 */
Stephen Boydb1207432016-02-07 00:27:55 -0800179struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200180 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
181 unsigned long flags)
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200182{
183 if (num_parents != 2) {
184 pr_err("mux-clock %s must have 2 parents\n", name);
185 return ERR_PTR(-EINVAL);
186 }
187
188 return clk_register_gpio(dev, name, parent_names, num_parents,
Linus Walleij908a5432017-09-24 18:19:18 +0200189 gpiod, flags, &clk_gpio_mux_ops);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200190}
Stephen Boydb1207432016-02-07 00:27:55 -0800191EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux);
192
193struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200194 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
195 unsigned long flags)
Stephen Boydb1207432016-02-07 00:27:55 -0800196{
197 struct clk_hw *hw;
198
199 hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents,
Linus Walleij908a5432017-09-24 18:19:18 +0200200 gpiod, flags);
Stephen Boydb1207432016-02-07 00:27:55 -0800201 if (IS_ERR(hw))
202 return ERR_CAST(hw);
203 return hw->clk;
204}
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200205EXPORT_SYMBOL_GPL(clk_register_gpio_mux);
206
Stephen Boyd14b04f22016-02-02 17:09:26 -0800207static int gpio_clk_driver_probe(struct platform_device *pdev)
Jyri Sarhac873d142014-09-05 15:21:34 +0300208{
Stephen Boyd14b04f22016-02-02 17:09:26 -0800209 struct device_node *node = pdev->dev.of_node;
210 const char **parent_names, *gpio_name;
Stephen Boyd0985df82016-02-19 17:31:52 -0800211 unsigned int num_parents;
Linus Walleij908a5432017-09-24 18:19:18 +0200212 struct gpio_desc *gpiod;
Stephen Boyd14b04f22016-02-02 17:09:26 -0800213 struct clk *clk;
Linus Walleij908a5432017-09-24 18:19:18 +0200214 bool is_mux;
215 int ret;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200216
Brian Norris0b2e7882015-12-16 10:35:03 -0800217 num_parents = of_clk_get_parent_count(node);
Russell King7ed88aa2016-01-02 10:01:34 +0000218 if (num_parents) {
Stephen Boyd14b04f22016-02-02 17:09:26 -0800219 parent_names = devm_kcalloc(&pdev->dev, num_parents,
220 sizeof(char *), GFP_KERNEL);
221 if (!parent_names)
222 return -ENOMEM;
Jyri Sarhaf66541b2015-11-17 11:56:44 +0200223
Stephen Boyd14b04f22016-02-02 17:09:26 -0800224 of_clk_parent_fill(node, parent_names, num_parents);
Russell King7ed88aa2016-01-02 10:01:34 +0000225 } else {
226 parent_names = NULL;
227 }
Jyri Sarhaf66541b2015-11-17 11:56:44 +0200228
Stephen Boyd14b04f22016-02-02 17:09:26 -0800229 is_mux = of_device_is_compatible(node, "gpio-mux-clock");
Jyri Sarhac873d142014-09-05 15:21:34 +0300230
Linus Walleij908a5432017-09-24 18:19:18 +0200231 gpio_name = is_mux ? "select" : "enable";
Linus Walleij1b5d1a52017-09-24 18:19:19 +0200232 gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_OUT_LOW);
Linus Walleij908a5432017-09-24 18:19:18 +0200233 if (IS_ERR(gpiod)) {
234 ret = PTR_ERR(gpiod);
235 if (ret == -EPROBE_DEFER)
Stephen Boyd14b04f22016-02-02 17:09:26 -0800236 pr_debug("%s: %s: GPIOs not yet available, retry later\n",
237 node->name, __func__);
238 else
Linus Walleij908a5432017-09-24 18:19:18 +0200239 pr_err("%s: %s: Can't get '%s' named GPIO property\n",
Stephen Boyd14b04f22016-02-02 17:09:26 -0800240 node->name, __func__,
241 gpio_name);
Linus Walleij908a5432017-09-24 18:19:18 +0200242 return ret;
Stephen Boyd14b04f22016-02-02 17:09:26 -0800243 }
244
Stephen Boyd14b04f22016-02-02 17:09:26 -0800245 if (is_mux)
246 clk = clk_register_gpio_mux(&pdev->dev, node->name,
Linus Walleij908a5432017-09-24 18:19:18 +0200247 parent_names, num_parents, gpiod, 0);
Stephen Boyd14b04f22016-02-02 17:09:26 -0800248 else
249 clk = clk_register_gpio_gate(&pdev->dev, node->name,
Linus Walleij908a5432017-09-24 18:19:18 +0200250 parent_names ? parent_names[0] : NULL, gpiod,
Michael Henneriche3c56352019-11-08 09:17:18 +0200251 CLK_SET_RATE_PARENT);
Stephen Boyd14b04f22016-02-02 17:09:26 -0800252 if (IS_ERR(clk))
253 return PTR_ERR(clk);
254
255 return of_clk_add_provider(node, of_clk_src_simple_get, clk);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200256}
257
Stephen Boyd14b04f22016-02-02 17:09:26 -0800258static const struct of_device_id gpio_clk_match_table[] = {
259 { .compatible = "gpio-mux-clock" },
260 { .compatible = "gpio-gate-clock" },
261 { }
262};
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200263
Stephen Boyd14b04f22016-02-02 17:09:26 -0800264static struct platform_driver gpio_clk_driver = {
265 .probe = gpio_clk_driver_probe,
266 .driver = {
267 .name = "gpio-clk",
268 .of_match_table = gpio_clk_match_table,
269 },
270};
271builtin_platform_driver(gpio_clk_driver);