xref: /openbmc/linux/drivers/clk/clk-gpio.c (revision f66541ba)
1bb68a4f1SSergej Sawazki /*
2bb68a4f1SSergej Sawazki  * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
3bb68a4f1SSergej Sawazki  *
4bb68a4f1SSergej Sawazki  * Authors:
5bb68a4f1SSergej Sawazki  *    Jyri Sarha <jsarha@ti.com>
6bb68a4f1SSergej Sawazki  *    Sergej Sawazki <ce3a@gmx.de>
7bb68a4f1SSergej Sawazki  *
8bb68a4f1SSergej Sawazki  * This program is free software; you can redistribute it and/or modify
9bb68a4f1SSergej Sawazki  * it under the terms of the GNU General Public License version 2 as
10bb68a4f1SSergej Sawazki  * published by the Free Software Foundation.
11bb68a4f1SSergej Sawazki  *
12bb68a4f1SSergej Sawazki  * Gpio controlled clock implementation
13bb68a4f1SSergej Sawazki  */
14bb68a4f1SSergej Sawazki 
15bb68a4f1SSergej Sawazki #include <linux/clk-provider.h>
16bb68a4f1SSergej Sawazki #include <linux/export.h>
17bb68a4f1SSergej Sawazki #include <linux/slab.h>
18bb68a4f1SSergej Sawazki #include <linux/gpio.h>
19bb68a4f1SSergej Sawazki #include <linux/gpio/consumer.h>
20bb68a4f1SSergej Sawazki #include <linux/of_gpio.h>
21bb68a4f1SSergej Sawazki #include <linux/err.h>
22bb68a4f1SSergej Sawazki #include <linux/device.h>
23bb68a4f1SSergej Sawazki 
24bb68a4f1SSergej Sawazki /**
25bb68a4f1SSergej Sawazki  * DOC: basic gpio gated clock which can be enabled and disabled
26bb68a4f1SSergej Sawazki  *      with gpio output
27bb68a4f1SSergej Sawazki  * Traits of this clock:
28bb68a4f1SSergej Sawazki  * prepare - clk_(un)prepare only ensures parent is (un)prepared
29bb68a4f1SSergej Sawazki  * enable - clk_enable and clk_disable are functional & control gpio
30bb68a4f1SSergej Sawazki  * rate - inherits rate from parent.  No clk_set_rate support
31bb68a4f1SSergej Sawazki  * parent - fixed parent.  No clk_set_parent support
32bb68a4f1SSergej Sawazki  */
33bb68a4f1SSergej Sawazki 
34bb68a4f1SSergej Sawazki #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
35bb68a4f1SSergej Sawazki 
36bb68a4f1SSergej Sawazki static int clk_gpio_gate_enable(struct clk_hw *hw)
37bb68a4f1SSergej Sawazki {
38bb68a4f1SSergej Sawazki 	struct clk_gpio *clk = to_clk_gpio(hw);
39bb68a4f1SSergej Sawazki 
40bb68a4f1SSergej Sawazki 	gpiod_set_value(clk->gpiod, 1);
41bb68a4f1SSergej Sawazki 
42bb68a4f1SSergej Sawazki 	return 0;
43bb68a4f1SSergej Sawazki }
44bb68a4f1SSergej Sawazki 
45bb68a4f1SSergej Sawazki static void clk_gpio_gate_disable(struct clk_hw *hw)
46bb68a4f1SSergej Sawazki {
47bb68a4f1SSergej Sawazki 	struct clk_gpio *clk = to_clk_gpio(hw);
48bb68a4f1SSergej Sawazki 
49bb68a4f1SSergej Sawazki 	gpiod_set_value(clk->gpiod, 0);
50bb68a4f1SSergej Sawazki }
51bb68a4f1SSergej Sawazki 
52bb68a4f1SSergej Sawazki static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
53bb68a4f1SSergej Sawazki {
54bb68a4f1SSergej Sawazki 	struct clk_gpio *clk = to_clk_gpio(hw);
55bb68a4f1SSergej Sawazki 
56bb68a4f1SSergej Sawazki 	return gpiod_get_value(clk->gpiod);
57bb68a4f1SSergej Sawazki }
58bb68a4f1SSergej Sawazki 
59bb68a4f1SSergej Sawazki const struct clk_ops clk_gpio_gate_ops = {
60bb68a4f1SSergej Sawazki 	.enable = clk_gpio_gate_enable,
61bb68a4f1SSergej Sawazki 	.disable = clk_gpio_gate_disable,
62bb68a4f1SSergej Sawazki 	.is_enabled = clk_gpio_gate_is_enabled,
63bb68a4f1SSergej Sawazki };
64bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
65bb68a4f1SSergej Sawazki 
66bb68a4f1SSergej Sawazki /**
67bb68a4f1SSergej Sawazki  * DOC: basic clock multiplexer which can be controlled with a gpio output
68bb68a4f1SSergej Sawazki  * Traits of this clock:
69bb68a4f1SSergej Sawazki  * prepare - clk_prepare only ensures that parents are prepared
70bb68a4f1SSergej Sawazki  * rate - rate is only affected by parent switching.  No clk_set_rate support
71bb68a4f1SSergej Sawazki  * parent - parent is adjustable through clk_set_parent
72bb68a4f1SSergej Sawazki  */
73bb68a4f1SSergej Sawazki 
74bb68a4f1SSergej Sawazki static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
75bb68a4f1SSergej Sawazki {
76bb68a4f1SSergej Sawazki 	struct clk_gpio *clk = to_clk_gpio(hw);
77bb68a4f1SSergej Sawazki 
78bb68a4f1SSergej Sawazki 	return gpiod_get_value(clk->gpiod);
79bb68a4f1SSergej Sawazki }
80bb68a4f1SSergej Sawazki 
81bb68a4f1SSergej Sawazki static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
82bb68a4f1SSergej Sawazki {
83bb68a4f1SSergej Sawazki 	struct clk_gpio *clk = to_clk_gpio(hw);
84bb68a4f1SSergej Sawazki 
85bb68a4f1SSergej Sawazki 	gpiod_set_value(clk->gpiod, index);
86bb68a4f1SSergej Sawazki 
87bb68a4f1SSergej Sawazki 	return 0;
88bb68a4f1SSergej Sawazki }
89bb68a4f1SSergej Sawazki 
90bb68a4f1SSergej Sawazki const struct clk_ops clk_gpio_mux_ops = {
91bb68a4f1SSergej Sawazki 	.get_parent = clk_gpio_mux_get_parent,
92bb68a4f1SSergej Sawazki 	.set_parent = clk_gpio_mux_set_parent,
93bb68a4f1SSergej Sawazki 	.determine_rate = __clk_mux_determine_rate,
94bb68a4f1SSergej Sawazki };
95bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_gpio_mux_ops);
96bb68a4f1SSergej Sawazki 
97bb68a4f1SSergej Sawazki static struct clk *clk_register_gpio(struct device *dev, const char *name,
9837bff2c1SStephen Boyd 		const char * const *parent_names, u8 num_parents, unsigned gpio,
99bb68a4f1SSergej Sawazki 		bool active_low, unsigned long flags,
100bb68a4f1SSergej Sawazki 		const struct clk_ops *clk_gpio_ops)
101bb68a4f1SSergej Sawazki {
102bb68a4f1SSergej Sawazki 	struct clk_gpio *clk_gpio;
103bb68a4f1SSergej Sawazki 	struct clk *clk;
104bb68a4f1SSergej Sawazki 	struct clk_init_data init = {};
105bb68a4f1SSergej Sawazki 	unsigned long gpio_flags;
106bb68a4f1SSergej Sawazki 	int err;
107bb68a4f1SSergej Sawazki 
108bb68a4f1SSergej Sawazki 	if (dev)
109bb68a4f1SSergej Sawazki 		clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio),	GFP_KERNEL);
110bb68a4f1SSergej Sawazki 	else
111bb68a4f1SSergej Sawazki 		clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL);
112bb68a4f1SSergej Sawazki 
113bb68a4f1SSergej Sawazki 	if (!clk_gpio)
114bb68a4f1SSergej Sawazki 		return ERR_PTR(-ENOMEM);
115bb68a4f1SSergej Sawazki 
116bb68a4f1SSergej Sawazki 	if (active_low)
117bb68a4f1SSergej Sawazki 		gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_HIGH;
118bb68a4f1SSergej Sawazki 	else
119bb68a4f1SSergej Sawazki 		gpio_flags = GPIOF_OUT_INIT_LOW;
120bb68a4f1SSergej Sawazki 
121bb68a4f1SSergej Sawazki 	if (dev)
122bb68a4f1SSergej Sawazki 		err = devm_gpio_request_one(dev, gpio, gpio_flags, name);
123bb68a4f1SSergej Sawazki 	else
124bb68a4f1SSergej Sawazki 		err = gpio_request_one(gpio, gpio_flags, name);
125bb68a4f1SSergej Sawazki 	if (err) {
126bb68a4f1SSergej Sawazki 		if (err != -EPROBE_DEFER)
127bb68a4f1SSergej Sawazki 			pr_err("%s: %s: Error requesting clock control gpio %u\n",
128bb68a4f1SSergej Sawazki 					__func__, name, gpio);
129bb68a4f1SSergej Sawazki 		if (!dev)
130bb68a4f1SSergej Sawazki 			kfree(clk_gpio);
131bb68a4f1SSergej Sawazki 
132bb68a4f1SSergej Sawazki 		return ERR_PTR(err);
133bb68a4f1SSergej Sawazki 	}
134bb68a4f1SSergej Sawazki 
135bb68a4f1SSergej Sawazki 	init.name = name;
136bb68a4f1SSergej Sawazki 	init.ops = clk_gpio_ops;
137bb68a4f1SSergej Sawazki 	init.flags = flags | CLK_IS_BASIC;
138bb68a4f1SSergej Sawazki 	init.parent_names = parent_names;
139bb68a4f1SSergej Sawazki 	init.num_parents = num_parents;
140bb68a4f1SSergej Sawazki 
141bb68a4f1SSergej Sawazki 	clk_gpio->gpiod = gpio_to_desc(gpio);
142bb68a4f1SSergej Sawazki 	clk_gpio->hw.init = &init;
143bb68a4f1SSergej Sawazki 
144bb68a4f1SSergej Sawazki 	if (dev)
145bb68a4f1SSergej Sawazki 		clk = devm_clk_register(dev, &clk_gpio->hw);
146bb68a4f1SSergej Sawazki 	else
147bb68a4f1SSergej Sawazki 		clk = clk_register(NULL, &clk_gpio->hw);
148bb68a4f1SSergej Sawazki 
149bb68a4f1SSergej Sawazki 	if (!IS_ERR(clk))
150bb68a4f1SSergej Sawazki 		return clk;
151bb68a4f1SSergej Sawazki 
152bb68a4f1SSergej Sawazki 	if (!dev) {
153bb68a4f1SSergej Sawazki 		gpiod_put(clk_gpio->gpiod);
154bb68a4f1SSergej Sawazki 		kfree(clk_gpio);
155bb68a4f1SSergej Sawazki 	}
156bb68a4f1SSergej Sawazki 
157bb68a4f1SSergej Sawazki 	return clk;
158bb68a4f1SSergej Sawazki }
159bb68a4f1SSergej Sawazki 
160bb68a4f1SSergej Sawazki /**
161bb68a4f1SSergej Sawazki  * clk_register_gpio_gate - register a gpio clock gate with the clock framework
162bb68a4f1SSergej Sawazki  * @dev: device that is registering this clock
163bb68a4f1SSergej Sawazki  * @name: name of this clock
164bb68a4f1SSergej Sawazki  * @parent_name: name of this clock's parent
165bb68a4f1SSergej Sawazki  * @gpio: gpio number to gate this clock
166bb68a4f1SSergej Sawazki  * @active_low: true if gpio should be set to 0 to enable clock
167bb68a4f1SSergej Sawazki  * @flags: clock flags
168bb68a4f1SSergej Sawazki  */
169bb68a4f1SSergej Sawazki struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
170bb68a4f1SSergej Sawazki 		const char *parent_name, unsigned gpio, bool active_low,
171bb68a4f1SSergej Sawazki 		unsigned long flags)
172bb68a4f1SSergej Sawazki {
173bb68a4f1SSergej Sawazki 	return clk_register_gpio(dev, name,
174bb68a4f1SSergej Sawazki 			(parent_name ? &parent_name : NULL),
175bb68a4f1SSergej Sawazki 			(parent_name ? 1 : 0), gpio, active_low, flags,
176bb68a4f1SSergej Sawazki 			&clk_gpio_gate_ops);
177bb68a4f1SSergej Sawazki }
178bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
179bb68a4f1SSergej Sawazki 
180bb68a4f1SSergej Sawazki /**
181bb68a4f1SSergej Sawazki  * clk_register_gpio_mux - register a gpio clock mux with the clock framework
182bb68a4f1SSergej Sawazki  * @dev: device that is registering this clock
183bb68a4f1SSergej Sawazki  * @name: name of this clock
184bb68a4f1SSergej Sawazki  * @parent_names: names of this clock's parents
185bb68a4f1SSergej Sawazki  * @num_parents: number of parents listed in @parent_names
186bb68a4f1SSergej Sawazki  * @gpio: gpio number to gate this clock
187bb68a4f1SSergej Sawazki  * @active_low: true if gpio should be set to 0 to enable clock
188bb68a4f1SSergej Sawazki  * @flags: clock flags
189bb68a4f1SSergej Sawazki  */
190bb68a4f1SSergej Sawazki struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
19137bff2c1SStephen Boyd 		const char * const *parent_names, u8 num_parents, unsigned gpio,
192bb68a4f1SSergej Sawazki 		bool active_low, unsigned long flags)
193bb68a4f1SSergej Sawazki {
194bb68a4f1SSergej Sawazki 	if (num_parents != 2) {
195bb68a4f1SSergej Sawazki 		pr_err("mux-clock %s must have 2 parents\n", name);
196bb68a4f1SSergej Sawazki 		return ERR_PTR(-EINVAL);
197bb68a4f1SSergej Sawazki 	}
198bb68a4f1SSergej Sawazki 
199bb68a4f1SSergej Sawazki 	return clk_register_gpio(dev, name, parent_names, num_parents,
200bb68a4f1SSergej Sawazki 			gpio, active_low, flags, &clk_gpio_mux_ops);
201bb68a4f1SSergej Sawazki }
202bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_register_gpio_mux);
203bb68a4f1SSergej Sawazki 
204bb68a4f1SSergej Sawazki #ifdef CONFIG_OF
205bb68a4f1SSergej Sawazki /**
206bb68a4f1SSergej Sawazki  * clk_register_get() has to be delayed, because -EPROBE_DEFER
207bb68a4f1SSergej Sawazki  * can not be handled properly at of_clk_init() call time.
208bb68a4f1SSergej Sawazki  */
209bb68a4f1SSergej Sawazki 
210bb68a4f1SSergej Sawazki struct clk_gpio_delayed_register_data {
211bb68a4f1SSergej Sawazki 	const char *gpio_name;
212f66541baSJyri Sarha 	int num_parents;
213f66541baSJyri Sarha 	const char **parent_names;
214bb68a4f1SSergej Sawazki 	struct device_node *node;
215bb68a4f1SSergej Sawazki 	struct mutex lock;
216bb68a4f1SSergej Sawazki 	struct clk *clk;
217bb68a4f1SSergej Sawazki 	struct clk *(*clk_register_get)(const char *name,
21837bff2c1SStephen Boyd 			const char * const *parent_names, u8 num_parents,
219bb68a4f1SSergej Sawazki 			unsigned gpio, bool active_low);
220bb68a4f1SSergej Sawazki };
221bb68a4f1SSergej Sawazki 
222bb68a4f1SSergej Sawazki static struct clk *of_clk_gpio_delayed_register_get(
223bb68a4f1SSergej Sawazki 		struct of_phandle_args *clkspec, void *_data)
224bb68a4f1SSergej Sawazki {
225bb68a4f1SSergej Sawazki 	struct clk_gpio_delayed_register_data *data = _data;
226bb68a4f1SSergej Sawazki 	struct clk *clk;
227bb68a4f1SSergej Sawazki 	int gpio;
228bb68a4f1SSergej Sawazki 	enum of_gpio_flags of_flags;
229bb68a4f1SSergej Sawazki 
230bb68a4f1SSergej Sawazki 	mutex_lock(&data->lock);
231bb68a4f1SSergej Sawazki 
232bb68a4f1SSergej Sawazki 	if (data->clk) {
233bb68a4f1SSergej Sawazki 		mutex_unlock(&data->lock);
234bb68a4f1SSergej Sawazki 		return data->clk;
235bb68a4f1SSergej Sawazki 	}
236bb68a4f1SSergej Sawazki 
237bb68a4f1SSergej Sawazki 	gpio = of_get_named_gpio_flags(data->node, data->gpio_name, 0,
238bb68a4f1SSergej Sawazki 			&of_flags);
239bb68a4f1SSergej Sawazki 	if (gpio < 0) {
240bb68a4f1SSergej Sawazki 		mutex_unlock(&data->lock);
241bb68a4f1SSergej Sawazki 		if (gpio == -EPROBE_DEFER)
242bb68a4f1SSergej Sawazki 			pr_debug("%s: %s: GPIOs not yet available, retry later\n",
243bb68a4f1SSergej Sawazki 					data->node->name, __func__);
244bb68a4f1SSergej Sawazki 		else
245bb68a4f1SSergej Sawazki 			pr_err("%s: %s: Can't get '%s' DT property\n",
246bb68a4f1SSergej Sawazki 					data->node->name, __func__,
247bb68a4f1SSergej Sawazki 					data->gpio_name);
248bb68a4f1SSergej Sawazki 		return ERR_PTR(gpio);
249bb68a4f1SSergej Sawazki 	}
250bb68a4f1SSergej Sawazki 
251f66541baSJyri Sarha 	clk = data->clk_register_get(data->node->name, data->parent_names,
252f66541baSJyri Sarha 			data->num_parents, gpio, of_flags & OF_GPIO_ACTIVE_LOW);
253bb68a4f1SSergej Sawazki 	if (IS_ERR(clk))
254bb68a4f1SSergej Sawazki 		goto out;
255bb68a4f1SSergej Sawazki 
256bb68a4f1SSergej Sawazki 	data->clk = clk;
257bb68a4f1SSergej Sawazki out:
258bb68a4f1SSergej Sawazki 	mutex_unlock(&data->lock);
259bb68a4f1SSergej Sawazki 
260bb68a4f1SSergej Sawazki 	return clk;
261bb68a4f1SSergej Sawazki }
262bb68a4f1SSergej Sawazki 
263bb68a4f1SSergej Sawazki static struct clk *of_clk_gpio_gate_delayed_register_get(const char *name,
26437bff2c1SStephen Boyd 		const char * const *parent_names, u8 num_parents,
265bb68a4f1SSergej Sawazki 		unsigned gpio, bool active_low)
266bb68a4f1SSergej Sawazki {
267bb68a4f1SSergej Sawazki 	return clk_register_gpio_gate(NULL, name, parent_names[0],
268bb68a4f1SSergej Sawazki 			gpio, active_low, 0);
269bb68a4f1SSergej Sawazki }
270bb68a4f1SSergej Sawazki 
271bb68a4f1SSergej Sawazki static struct clk *of_clk_gpio_mux_delayed_register_get(const char *name,
27237bff2c1SStephen Boyd 		const char * const *parent_names, u8 num_parents, unsigned gpio,
273bb68a4f1SSergej Sawazki 		bool active_low)
274bb68a4f1SSergej Sawazki {
275bb68a4f1SSergej Sawazki 	return clk_register_gpio_mux(NULL, name, parent_names, num_parents,
276bb68a4f1SSergej Sawazki 			gpio, active_low, 0);
277bb68a4f1SSergej Sawazki }
278bb68a4f1SSergej Sawazki 
279bb68a4f1SSergej Sawazki static void __init of_gpio_clk_setup(struct device_node *node,
280bb68a4f1SSergej Sawazki 		const char *gpio_name,
281bb68a4f1SSergej Sawazki 		struct clk *(*clk_register_get)(const char *name,
28237bff2c1SStephen Boyd 				const char * const *parent_names,
28337bff2c1SStephen Boyd 				u8 num_parents,
284bb68a4f1SSergej Sawazki 				unsigned gpio, bool active_low))
285bb68a4f1SSergej Sawazki {
286bb68a4f1SSergej Sawazki 	struct clk_gpio_delayed_register_data *data;
287f66541baSJyri Sarha 	const char **parent_names;
288f66541baSJyri Sarha 	int i, num_parents;
289bb68a4f1SSergej Sawazki 
290bb68a4f1SSergej Sawazki 	data = kzalloc(sizeof(*data), GFP_KERNEL);
291bb68a4f1SSergej Sawazki 	if (!data)
292bb68a4f1SSergej Sawazki 		return;
293bb68a4f1SSergej Sawazki 
294f66541baSJyri Sarha 	num_parents = of_clk_get_parent_count(node);
295f66541baSJyri Sarha 
296f66541baSJyri Sarha 	parent_names = kcalloc(num_parents, sizeof(char *), GFP_KERNEL);
297f66541baSJyri Sarha 	if (!parent_names)
298f66541baSJyri Sarha 		return;
299f66541baSJyri Sarha 
300f66541baSJyri Sarha 	for (i = 0; i < num_parents; i++)
301f66541baSJyri Sarha 		parent_names[i] = of_clk_get_parent_name(node, i);
302f66541baSJyri Sarha 
303f66541baSJyri Sarha 	data->num_parents = num_parents;
304f66541baSJyri Sarha 	data->parent_names = parent_names;
305bb68a4f1SSergej Sawazki 	data->node = node;
306bb68a4f1SSergej Sawazki 	data->gpio_name = gpio_name;
307bb68a4f1SSergej Sawazki 	data->clk_register_get = clk_register_get;
308bb68a4f1SSergej Sawazki 	mutex_init(&data->lock);
309bb68a4f1SSergej Sawazki 
310bb68a4f1SSergej Sawazki 	of_clk_add_provider(node, of_clk_gpio_delayed_register_get, data);
311bb68a4f1SSergej Sawazki }
312bb68a4f1SSergej Sawazki 
313bb68a4f1SSergej Sawazki static void __init of_gpio_gate_clk_setup(struct device_node *node)
314bb68a4f1SSergej Sawazki {
315bb68a4f1SSergej Sawazki 	of_gpio_clk_setup(node, "enable-gpios",
316bb68a4f1SSergej Sawazki 		of_clk_gpio_gate_delayed_register_get);
317bb68a4f1SSergej Sawazki }
318bb68a4f1SSergej Sawazki CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup);
319bb68a4f1SSergej Sawazki 
320bb68a4f1SSergej Sawazki void __init of_gpio_mux_clk_setup(struct device_node *node)
321bb68a4f1SSergej Sawazki {
322bb68a4f1SSergej Sawazki 	of_gpio_clk_setup(node, "select-gpios",
323bb68a4f1SSergej Sawazki 		of_clk_gpio_mux_delayed_register_get);
324bb68a4f1SSergej Sawazki }
325bb68a4f1SSergej Sawazki CLK_OF_DECLARE(gpio_mux_clk, "gpio-mux-clock", of_gpio_mux_clk_setup);
326bb68a4f1SSergej Sawazki #endif
327