blob: 9d930edd651626ac0997dca0d6d78d1fad107aad [file] [log] [blame]
Stephen Boyde1bd55e2018-12-11 09:57:48 -08001// SPDX-License-Identifier: GPL-2.0
Jyri Sarhac873d142014-09-05 15:21:34 +03002/*
3 * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +02004 *
5 * Authors:
6 * Jyri Sarha <jsarha@ti.com>
7 * Sergej Sawazki <ce3a@gmx.de>
Jyri Sarhac873d142014-09-05 15:21:34 +03008 *
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +02009 * Gpio controlled clock implementation
Jyri Sarhac873d142014-09-05 15:21:34 +030010 */
11
12#include <linux/clk-provider.h>
Sergej Sawazkie21b08e2015-06-28 16:24:53 +020013#include <linux/export.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030014#include <linux/slab.h>
Mark Brown44b4aa92014-09-30 18:16:22 +010015#include <linux/gpio/consumer.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030016#include <linux/err.h>
17#include <linux/device.h>
Stephen Boyd14b04f22016-02-02 17:09:26 -080018#include <linux/platform_device.h>
19#include <linux/of_device.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030020
21/**
22 * DOC: basic gpio gated clock which can be enabled and disabled
23 * with gpio output
24 * Traits of this clock:
25 * prepare - clk_(un)prepare only ensures parent is (un)prepared
26 * enable - clk_enable and clk_disable are functional & control gpio
27 * rate - inherits rate from parent. No clk_set_rate support
28 * parent - fixed parent. No clk_set_parent support
29 */
30
Jyri Sarhac873d142014-09-05 15:21:34 +030031static int clk_gpio_gate_enable(struct clk_hw *hw)
32{
33 struct clk_gpio *clk = to_clk_gpio(hw);
34
35 gpiod_set_value(clk->gpiod, 1);
36
37 return 0;
38}
39
40static void clk_gpio_gate_disable(struct clk_hw *hw)
41{
42 struct clk_gpio *clk = to_clk_gpio(hw);
43
44 gpiod_set_value(clk->gpiod, 0);
45}
46
47static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
48{
49 struct clk_gpio *clk = to_clk_gpio(hw);
50
51 return gpiod_get_value(clk->gpiod);
52}
53
54const struct clk_ops clk_gpio_gate_ops = {
55 .enable = clk_gpio_gate_enable,
56 .disable = clk_gpio_gate_disable,
57 .is_enabled = clk_gpio_gate_is_enabled,
58};
59EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
60
Thomas Petazzonic0189fe2019-02-11 14:58:18 +010061static int clk_sleeping_gpio_gate_prepare(struct clk_hw *hw)
62{
63 struct clk_gpio *clk = to_clk_gpio(hw);
64
65 gpiod_set_value_cansleep(clk->gpiod, 1);
66
67 return 0;
68}
69
70static void clk_sleeping_gpio_gate_unprepare(struct clk_hw *hw)
71{
72 struct clk_gpio *clk = to_clk_gpio(hw);
73
74 gpiod_set_value_cansleep(clk->gpiod, 0);
75}
76
77static int clk_sleeping_gpio_gate_is_prepared(struct clk_hw *hw)
78{
79 struct clk_gpio *clk = to_clk_gpio(hw);
80
81 return gpiod_get_value_cansleep(clk->gpiod);
82}
83
84static const struct clk_ops clk_sleeping_gpio_gate_ops = {
85 .prepare = clk_sleeping_gpio_gate_prepare,
86 .unprepare = clk_sleeping_gpio_gate_unprepare,
87 .is_prepared = clk_sleeping_gpio_gate_is_prepared,
88};
89
Jyri Sarhac873d142014-09-05 15:21:34 +030090/**
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020091 * 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
98static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
99{
100 struct clk_gpio *clk = to_clk_gpio(hw);
101
Mike Looijmans2e838e72018-03-13 09:54:03 +0100102 return gpiod_get_value_cansleep(clk->gpiod);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200103}
104
105static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
106{
107 struct clk_gpio *clk = to_clk_gpio(hw);
108
Mike Looijmans2e838e72018-03-13 09:54:03 +0100109 gpiod_set_value_cansleep(clk->gpiod, index);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200110
111 return 0;
112}
113
114const struct clk_ops clk_gpio_mux_ops = {
115 .get_parent = clk_gpio_mux_get_parent,
116 .set_parent = clk_gpio_mux_set_parent,
117 .determine_rate = __clk_mux_determine_rate,
118};
119EXPORT_SYMBOL_GPL(clk_gpio_mux_ops);
120
Stephen Boydb1207432016-02-07 00:27:55 -0800121static struct clk_hw *clk_register_gpio(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200122 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
123 unsigned long flags, const struct clk_ops *clk_gpio_ops)
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200124{
125 struct clk_gpio *clk_gpio;
Stephen Boydb1207432016-02-07 00:27:55 -0800126 struct clk_hw *hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200127 struct clk_init_data init = {};
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200128 int err;
129
130 if (dev)
131 clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
132 else
133 clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL);
134
135 if (!clk_gpio)
136 return ERR_PTR(-ENOMEM);
137
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200138 init.name = name;
139 init.ops = clk_gpio_ops;
Stephen Boyd90b6c5c2019-04-25 10:57:37 -0700140 init.flags = flags;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200141 init.parent_names = parent_names;
142 init.num_parents = num_parents;
143
Linus Walleij908a5432017-09-24 18:19:18 +0200144 clk_gpio->gpiod = gpiod;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200145 clk_gpio->hw.init = &init;
146
Stephen Boydb1207432016-02-07 00:27:55 -0800147 hw = &clk_gpio->hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200148 if (dev)
Stephen Boydb1207432016-02-07 00:27:55 -0800149 err = devm_clk_hw_register(dev, hw);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200150 else
Stephen Boydb1207432016-02-07 00:27:55 -0800151 err = clk_hw_register(NULL, hw);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200152
Stephen Boydb1207432016-02-07 00:27:55 -0800153 if (!err)
154 return hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200155
156 if (!dev) {
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200157 kfree(clk_gpio);
158 }
159
Stephen Boydb1207432016-02-07 00:27:55 -0800160 return ERR_PTR(err);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200161}
162
163/**
Stephen Boydb1207432016-02-07 00:27:55 -0800164 * clk_hw_register_gpio_gate - register a gpio clock gate with the clock
165 * framework
Jyri Sarhac873d142014-09-05 15:21:34 +0300166 * @dev: device that is registering this clock
167 * @name: name of this clock
168 * @parent_name: name of this clock's parent
Linus Walleij908a5432017-09-24 18:19:18 +0200169 * @gpiod: gpio descriptor to gate this clock
Martin Fuzzey820ad972015-03-18 14:53:17 +0100170 * @flags: clock flags
Jyri Sarhac873d142014-09-05 15:21:34 +0300171 */
Stephen Boydb1207432016-02-07 00:27:55 -0800172struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200173 const char *parent_name, struct gpio_desc *gpiod,
Jyri Sarhac873d142014-09-05 15:21:34 +0300174 unsigned long flags)
175{
Thomas Petazzonic0189fe2019-02-11 14:58:18 +0100176 const struct clk_ops *ops;
177
178 if (gpiod_cansleep(gpiod))
179 ops = &clk_sleeping_gpio_gate_ops;
180 else
181 ops = &clk_gpio_gate_ops;
182
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200183 return clk_register_gpio(dev, name,
184 (parent_name ? &parent_name : NULL),
Thomas Petazzonic0189fe2019-02-11 14:58:18 +0100185 (parent_name ? 1 : 0), gpiod, flags, ops);
Jyri Sarhac873d142014-09-05 15:21:34 +0300186}
Stephen Boydb1207432016-02-07 00:27:55 -0800187EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate);
188
189struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200190 const char *parent_name, struct gpio_desc *gpiod,
Stephen Boydb1207432016-02-07 00:27:55 -0800191 unsigned long flags)
192{
193 struct clk_hw *hw;
194
Linus Walleij908a5432017-09-24 18:19:18 +0200195 hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags);
Stephen Boydb1207432016-02-07 00:27:55 -0800196 if (IS_ERR(hw))
197 return ERR_CAST(hw);
198 return hw->clk;
199}
Jyri Sarhac873d142014-09-05 15:21:34 +0300200EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
201
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200202/**
Stephen Boydb1207432016-02-07 00:27:55 -0800203 * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200204 * @dev: device that is registering this clock
205 * @name: name of this clock
206 * @parent_names: names of this clock's parents
207 * @num_parents: number of parents listed in @parent_names
Linus Walleij908a5432017-09-24 18:19:18 +0200208 * @gpiod: gpio descriptor to gate this clock
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200209 * @flags: clock flags
210 */
Stephen Boydb1207432016-02-07 00:27:55 -0800211struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200212 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
213 unsigned long flags)
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200214{
215 if (num_parents != 2) {
216 pr_err("mux-clock %s must have 2 parents\n", name);
217 return ERR_PTR(-EINVAL);
218 }
219
220 return clk_register_gpio(dev, name, parent_names, num_parents,
Linus Walleij908a5432017-09-24 18:19:18 +0200221 gpiod, flags, &clk_gpio_mux_ops);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200222}
Stephen Boydb1207432016-02-07 00:27:55 -0800223EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux);
224
225struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
Linus Walleij908a5432017-09-24 18:19:18 +0200226 const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
227 unsigned long flags)
Stephen Boydb1207432016-02-07 00:27:55 -0800228{
229 struct clk_hw *hw;
230
231 hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents,
Linus Walleij908a5432017-09-24 18:19:18 +0200232 gpiod, flags);
Stephen Boydb1207432016-02-07 00:27:55 -0800233 if (IS_ERR(hw))
234 return ERR_CAST(hw);
235 return hw->clk;
236}
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200237EXPORT_SYMBOL_GPL(clk_register_gpio_mux);
238
Stephen Boyd14b04f22016-02-02 17:09:26 -0800239static int gpio_clk_driver_probe(struct platform_device *pdev)
Jyri Sarhac873d142014-09-05 15:21:34 +0300240{
Stephen Boyd14b04f22016-02-02 17:09:26 -0800241 struct device_node *node = pdev->dev.of_node;
242 const char **parent_names, *gpio_name;
Stephen Boyd0985df82016-02-19 17:31:52 -0800243 unsigned int num_parents;
Linus Walleij908a5432017-09-24 18:19:18 +0200244 struct gpio_desc *gpiod;
Stephen Boyd14b04f22016-02-02 17:09:26 -0800245 struct clk *clk;
Linus Walleij908a5432017-09-24 18:19:18 +0200246 bool is_mux;
247 int ret;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200248
Brian Norris0b2e7882015-12-16 10:35:03 -0800249 num_parents = of_clk_get_parent_count(node);
Russell King7ed88aa2016-01-02 10:01:34 +0000250 if (num_parents) {
Stephen Boyd14b04f22016-02-02 17:09:26 -0800251 parent_names = devm_kcalloc(&pdev->dev, num_parents,
252 sizeof(char *), GFP_KERNEL);
253 if (!parent_names)
254 return -ENOMEM;
Jyri Sarhaf66541b2015-11-17 11:56:44 +0200255
Stephen Boyd14b04f22016-02-02 17:09:26 -0800256 of_clk_parent_fill(node, parent_names, num_parents);
Russell King7ed88aa2016-01-02 10:01:34 +0000257 } else {
258 parent_names = NULL;
259 }
Jyri Sarhaf66541b2015-11-17 11:56:44 +0200260
Stephen Boyd14b04f22016-02-02 17:09:26 -0800261 is_mux = of_device_is_compatible(node, "gpio-mux-clock");
Jyri Sarhac873d142014-09-05 15:21:34 +0300262
Linus Walleij908a5432017-09-24 18:19:18 +0200263 gpio_name = is_mux ? "select" : "enable";
Linus Walleij1b5d1a52017-09-24 18:19:19 +0200264 gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_OUT_LOW);
Linus Walleij908a5432017-09-24 18:19:18 +0200265 if (IS_ERR(gpiod)) {
266 ret = PTR_ERR(gpiod);
267 if (ret == -EPROBE_DEFER)
Rob Herringe665f022018-08-28 10:44:29 -0500268 pr_debug("%pOFn: %s: GPIOs not yet available, retry later\n",
269 node, __func__);
Stephen Boyd14b04f22016-02-02 17:09:26 -0800270 else
Rob Herringe665f022018-08-28 10:44:29 -0500271 pr_err("%pOFn: %s: Can't get '%s' named GPIO property\n",
272 node, __func__,
Stephen Boyd14b04f22016-02-02 17:09:26 -0800273 gpio_name);
Linus Walleij908a5432017-09-24 18:19:18 +0200274 return ret;
Stephen Boyd14b04f22016-02-02 17:09:26 -0800275 }
276
Stephen Boyd14b04f22016-02-02 17:09:26 -0800277 if (is_mux)
278 clk = clk_register_gpio_mux(&pdev->dev, node->name,
Linus Walleij908a5432017-09-24 18:19:18 +0200279 parent_names, num_parents, gpiod, 0);
Stephen Boyd14b04f22016-02-02 17:09:26 -0800280 else
281 clk = clk_register_gpio_gate(&pdev->dev, node->name,
Linus Walleij908a5432017-09-24 18:19:18 +0200282 parent_names ? parent_names[0] : NULL, gpiod,
283 0);
Stephen Boyd14b04f22016-02-02 17:09:26 -0800284 if (IS_ERR(clk))
285 return PTR_ERR(clk);
286
287 return of_clk_add_provider(node, of_clk_src_simple_get, clk);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200288}
289
Stephen Boyd14b04f22016-02-02 17:09:26 -0800290static const struct of_device_id gpio_clk_match_table[] = {
291 { .compatible = "gpio-mux-clock" },
292 { .compatible = "gpio-gate-clock" },
293 { }
294};
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200295
Stephen Boyd14b04f22016-02-02 17:09:26 -0800296static struct platform_driver gpio_clk_driver = {
297 .probe = gpio_clk_driver_probe,
298 .driver = {
299 .name = "gpio-clk",
300 .of_match_table = gpio_clk_match_table,
301 },
302};
303builtin_platform_driver(gpio_clk_driver);