xref: /openbmc/linux/drivers/clk/clk-gpio.c (revision 0985df89)
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>
2314b04f28SStephen Boyd #include <linux/platform_device.h>
2414b04f28SStephen Boyd #include <linux/of_device.h>
25bb68a4f1SSergej Sawazki 
26bb68a4f1SSergej Sawazki /**
27bb68a4f1SSergej Sawazki  * DOC: basic gpio gated clock which can be enabled and disabled
28bb68a4f1SSergej Sawazki  *      with gpio output
29bb68a4f1SSergej Sawazki  * Traits of this clock:
30bb68a4f1SSergej Sawazki  * prepare - clk_(un)prepare only ensures parent is (un)prepared
31bb68a4f1SSergej Sawazki  * enable - clk_enable and clk_disable are functional & control gpio
32bb68a4f1SSergej Sawazki  * rate - inherits rate from parent.  No clk_set_rate support
33bb68a4f1SSergej Sawazki  * parent - fixed parent.  No clk_set_parent support
34bb68a4f1SSergej Sawazki  */
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 
20414b04f28SStephen Boyd static int gpio_clk_driver_probe(struct platform_device *pdev)
205bb68a4f1SSergej Sawazki {
20614b04f28SStephen Boyd 	struct device_node *node = pdev->dev.of_node;
20714b04f28SStephen Boyd 	const char **parent_names, *gpio_name;
2080985df89SStephen Boyd 	unsigned int num_parents;
2090985df89SStephen Boyd 	int gpio;
210bb68a4f1SSergej Sawazki 	enum of_gpio_flags of_flags;
21114b04f28SStephen Boyd 	struct clk *clk;
21214b04f28SStephen Boyd 	bool active_low, is_mux;
213bb68a4f1SSergej Sawazki 
2140b2e7886SBrian Norris 	num_parents = of_clk_get_parent_count(node);
2157ed88aa2SRussell King 	if (num_parents) {
21614b04f28SStephen Boyd 		parent_names = devm_kcalloc(&pdev->dev, num_parents,
21714b04f28SStephen Boyd 					    sizeof(char *), GFP_KERNEL);
21814b04f28SStephen Boyd 		if (!parent_names)
21914b04f28SStephen Boyd 			return -ENOMEM;
220f66541baSJyri Sarha 
22114b04f28SStephen Boyd 		of_clk_parent_fill(node, parent_names, num_parents);
2227ed88aa2SRussell King 	} else {
2237ed88aa2SRussell King 		parent_names = NULL;
2247ed88aa2SRussell King 	}
225f66541baSJyri Sarha 
22614b04f28SStephen Boyd 	is_mux = of_device_is_compatible(node, "gpio-mux-clock");
227bb68a4f1SSergej Sawazki 
22814b04f28SStephen Boyd 	gpio_name = is_mux ? "select-gpios" : "enable-gpios";
22914b04f28SStephen Boyd 	gpio = of_get_named_gpio_flags(node, gpio_name, 0, &of_flags);
23014b04f28SStephen Boyd 	if (gpio < 0) {
23114b04f28SStephen Boyd 		if (gpio == -EPROBE_DEFER)
23214b04f28SStephen Boyd 			pr_debug("%s: %s: GPIOs not yet available, retry later\n",
23314b04f28SStephen Boyd 					node->name, __func__);
23414b04f28SStephen Boyd 		else
23514b04f28SStephen Boyd 			pr_err("%s: %s: Can't get '%s' DT property\n",
23614b04f28SStephen Boyd 					node->name, __func__,
23714b04f28SStephen Boyd 					gpio_name);
23814b04f28SStephen Boyd 		return gpio;
239bb68a4f1SSergej Sawazki 	}
240bb68a4f1SSergej Sawazki 
24114b04f28SStephen Boyd 	active_low = of_flags & OF_GPIO_ACTIVE_LOW;
242bb68a4f1SSergej Sawazki 
24314b04f28SStephen Boyd 	if (is_mux)
24414b04f28SStephen Boyd 		clk = clk_register_gpio_mux(&pdev->dev, node->name,
24514b04f28SStephen Boyd 				parent_names, num_parents, gpio, active_low, 0);
24614b04f28SStephen Boyd 	else
24714b04f28SStephen Boyd 		clk = clk_register_gpio_gate(&pdev->dev, node->name,
24814b04f28SStephen Boyd 				parent_names ?  parent_names[0] : NULL, gpio,
24914b04f28SStephen Boyd 				active_low, 0);
25014b04f28SStephen Boyd 	if (IS_ERR(clk))
25114b04f28SStephen Boyd 		return PTR_ERR(clk);
25214b04f28SStephen Boyd 
25314b04f28SStephen Boyd 	return of_clk_add_provider(node, of_clk_src_simple_get, clk);
254bb68a4f1SSergej Sawazki }
25514b04f28SStephen Boyd 
25614b04f28SStephen Boyd static const struct of_device_id gpio_clk_match_table[] = {
25714b04f28SStephen Boyd 	{ .compatible = "gpio-mux-clock" },
25814b04f28SStephen Boyd 	{ .compatible = "gpio-gate-clock" },
25914b04f28SStephen Boyd 	{ }
26014b04f28SStephen Boyd };
26114b04f28SStephen Boyd 
26214b04f28SStephen Boyd static struct platform_driver gpio_clk_driver = {
26314b04f28SStephen Boyd 	.probe		= gpio_clk_driver_probe,
26414b04f28SStephen Boyd 	.driver		= {
26514b04f28SStephen Boyd 		.name	= "gpio-clk",
26614b04f28SStephen Boyd 		.of_match_table = gpio_clk_match_table,
26714b04f28SStephen Boyd 	},
26814b04f28SStephen Boyd };
26914b04f28SStephen Boyd builtin_platform_driver(gpio_clk_driver);
270