blob: 86b245746a6bad5895b267101268124e5050c4b9 [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>
18#include <linux/gpio.h>
Mark Brown44b4aa92014-09-30 18:16:22 +010019#include <linux/gpio/consumer.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030020#include <linux/of_gpio.h>
21#include <linux/err.h>
22#include <linux/device.h>
Stephen Boyd14b04f22016-02-02 17:09:26 -080023#include <linux/platform_device.h>
24#include <linux/of_device.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030025
26/**
27 * DOC: basic gpio gated clock which can be enabled and disabled
28 * with gpio output
29 * Traits of this clock:
30 * prepare - clk_(un)prepare only ensures parent is (un)prepared
31 * enable - clk_enable and clk_disable are functional & control gpio
32 * rate - inherits rate from parent. No clk_set_rate support
33 * parent - fixed parent. No clk_set_parent support
34 */
35
Jyri Sarhac873d142014-09-05 15:21:34 +030036static int clk_gpio_gate_enable(struct clk_hw *hw)
37{
38 struct clk_gpio *clk = to_clk_gpio(hw);
39
40 gpiod_set_value(clk->gpiod, 1);
41
42 return 0;
43}
44
45static void clk_gpio_gate_disable(struct clk_hw *hw)
46{
47 struct clk_gpio *clk = to_clk_gpio(hw);
48
49 gpiod_set_value(clk->gpiod, 0);
50}
51
52static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
53{
54 struct clk_gpio *clk = to_clk_gpio(hw);
55
56 return gpiod_get_value(clk->gpiod);
57}
58
59const struct clk_ops clk_gpio_gate_ops = {
60 .enable = clk_gpio_gate_enable,
61 .disable = clk_gpio_gate_disable,
62 .is_enabled = clk_gpio_gate_is_enabled,
63};
64EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
65
66/**
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020067 * DOC: basic clock multiplexer which can be controlled with a gpio output
68 * Traits of this clock:
69 * prepare - clk_prepare only ensures that parents are prepared
70 * rate - rate is only affected by parent switching. No clk_set_rate support
71 * parent - parent is adjustable through clk_set_parent
72 */
73
74static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
75{
76 struct clk_gpio *clk = to_clk_gpio(hw);
77
78 return gpiod_get_value(clk->gpiod);
79}
80
81static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
82{
83 struct clk_gpio *clk = to_clk_gpio(hw);
84
85 gpiod_set_value(clk->gpiod, index);
86
87 return 0;
88}
89
90const struct clk_ops clk_gpio_mux_ops = {
91 .get_parent = clk_gpio_mux_get_parent,
92 .set_parent = clk_gpio_mux_set_parent,
93 .determine_rate = __clk_mux_determine_rate,
94};
95EXPORT_SYMBOL_GPL(clk_gpio_mux_ops);
96
Stephen Boydb1207432016-02-07 00:27:55 -080097static struct clk_hw *clk_register_gpio(struct device *dev, const char *name,
Stephen Boyd37bff2c2015-07-24 09:31:29 -070098 const char * const *parent_names, u8 num_parents, unsigned gpio,
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +020099 bool active_low, unsigned long flags,
100 const struct clk_ops *clk_gpio_ops)
101{
102 struct clk_gpio *clk_gpio;
Stephen Boydb1207432016-02-07 00:27:55 -0800103 struct clk_hw *hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200104 struct clk_init_data init = {};
105 unsigned long gpio_flags;
106 int err;
107
108 if (dev)
109 clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
110 else
111 clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL);
112
113 if (!clk_gpio)
114 return ERR_PTR(-ENOMEM);
115
116 if (active_low)
117 gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_HIGH;
118 else
119 gpio_flags = GPIOF_OUT_INIT_LOW;
120
121 if (dev)
122 err = devm_gpio_request_one(dev, gpio, gpio_flags, name);
123 else
124 err = gpio_request_one(gpio, gpio_flags, name);
125 if (err) {
126 if (err != -EPROBE_DEFER)
127 pr_err("%s: %s: Error requesting clock control gpio %u\n",
128 __func__, name, gpio);
129 if (!dev)
130 kfree(clk_gpio);
131
132 return ERR_PTR(err);
133 }
134
135 init.name = name;
136 init.ops = clk_gpio_ops;
137 init.flags = flags | CLK_IS_BASIC;
138 init.parent_names = parent_names;
139 init.num_parents = num_parents;
140
141 clk_gpio->gpiod = gpio_to_desc(gpio);
142 clk_gpio->hw.init = &init;
143
Stephen Boydb1207432016-02-07 00:27:55 -0800144 hw = &clk_gpio->hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200145 if (dev)
Stephen Boydb1207432016-02-07 00:27:55 -0800146 err = devm_clk_hw_register(dev, hw);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200147 else
Stephen Boydb1207432016-02-07 00:27:55 -0800148 err = clk_hw_register(NULL, hw);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200149
Stephen Boydb1207432016-02-07 00:27:55 -0800150 if (!err)
151 return hw;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200152
153 if (!dev) {
154 gpiod_put(clk_gpio->gpiod);
155 kfree(clk_gpio);
156 }
157
Stephen Boydb1207432016-02-07 00:27:55 -0800158 return ERR_PTR(err);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200159}
160
161/**
Stephen Boydb1207432016-02-07 00:27:55 -0800162 * clk_hw_register_gpio_gate - register a gpio clock gate with the clock
163 * framework
Jyri Sarhac873d142014-09-05 15:21:34 +0300164 * @dev: device that is registering this clock
165 * @name: name of this clock
166 * @parent_name: name of this clock's parent
Martin Fuzzey820ad972015-03-18 14:53:17 +0100167 * @gpio: gpio number to gate this clock
168 * @active_low: true if gpio should be set to 0 to enable clock
169 * @flags: clock flags
Jyri Sarhac873d142014-09-05 15:21:34 +0300170 */
Stephen Boydb1207432016-02-07 00:27:55 -0800171struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
Martin Fuzzey820ad972015-03-18 14:53:17 +0100172 const char *parent_name, unsigned gpio, bool active_low,
Jyri Sarhac873d142014-09-05 15:21:34 +0300173 unsigned long flags)
174{
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200175 return clk_register_gpio(dev, name,
176 (parent_name ? &parent_name : NULL),
177 (parent_name ? 1 : 0), gpio, active_low, flags,
178 &clk_gpio_gate_ops);
Jyri Sarhac873d142014-09-05 15:21:34 +0300179}
Stephen Boydb1207432016-02-07 00:27:55 -0800180EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate);
181
182struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
183 const char *parent_name, unsigned gpio, bool active_low,
184 unsigned long flags)
185{
186 struct clk_hw *hw;
187
188 hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpio, active_low,
189 flags);
190 if (IS_ERR(hw))
191 return ERR_CAST(hw);
192 return hw->clk;
193}
Jyri Sarhac873d142014-09-05 15:21:34 +0300194EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
195
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200196/**
Stephen Boydb1207432016-02-07 00:27:55 -0800197 * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200198 * @dev: device that is registering this clock
199 * @name: name of this clock
200 * @parent_names: names of this clock's parents
201 * @num_parents: number of parents listed in @parent_names
202 * @gpio: gpio number to gate this clock
203 * @active_low: true if gpio should be set to 0 to enable clock
204 * @flags: clock flags
205 */
Stephen Boydb1207432016-02-07 00:27:55 -0800206struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
Stephen Boyd37bff2c2015-07-24 09:31:29 -0700207 const char * const *parent_names, u8 num_parents, unsigned gpio,
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200208 bool active_low, unsigned long flags)
209{
210 if (num_parents != 2) {
211 pr_err("mux-clock %s must have 2 parents\n", name);
212 return ERR_PTR(-EINVAL);
213 }
214
215 return clk_register_gpio(dev, name, parent_names, num_parents,
216 gpio, active_low, flags, &clk_gpio_mux_ops);
217}
Stephen Boydb1207432016-02-07 00:27:55 -0800218EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux);
219
220struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
221 const char * const *parent_names, u8 num_parents, unsigned gpio,
222 bool active_low, unsigned long flags)
223{
224 struct clk_hw *hw;
225
226 hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents,
227 gpio, active_low, flags);
228 if (IS_ERR(hw))
229 return ERR_CAST(hw);
230 return hw->clk;
231}
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200232EXPORT_SYMBOL_GPL(clk_register_gpio_mux);
233
Stephen Boyd14b04f22016-02-02 17:09:26 -0800234static int gpio_clk_driver_probe(struct platform_device *pdev)
Jyri Sarhac873d142014-09-05 15:21:34 +0300235{
Stephen Boyd14b04f22016-02-02 17:09:26 -0800236 struct device_node *node = pdev->dev.of_node;
237 const char **parent_names, *gpio_name;
Stephen Boyd0985df82016-02-19 17:31:52 -0800238 unsigned int num_parents;
239 int gpio;
Martin Fuzzey820ad972015-03-18 14:53:17 +0100240 enum of_gpio_flags of_flags;
Stephen Boyd14b04f22016-02-02 17:09:26 -0800241 struct clk *clk;
242 bool active_low, is_mux;
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200243
Brian Norris0b2e7882015-12-16 10:35:03 -0800244 num_parents = of_clk_get_parent_count(node);
Russell King7ed88aa2016-01-02 10:01:34 +0000245 if (num_parents) {
Stephen Boyd14b04f22016-02-02 17:09:26 -0800246 parent_names = devm_kcalloc(&pdev->dev, num_parents,
247 sizeof(char *), GFP_KERNEL);
248 if (!parent_names)
249 return -ENOMEM;
Jyri Sarhaf66541b2015-11-17 11:56:44 +0200250
Stephen Boyd14b04f22016-02-02 17:09:26 -0800251 of_clk_parent_fill(node, parent_names, num_parents);
Russell King7ed88aa2016-01-02 10:01:34 +0000252 } else {
253 parent_names = NULL;
254 }
Jyri Sarhaf66541b2015-11-17 11:56:44 +0200255
Stephen Boyd14b04f22016-02-02 17:09:26 -0800256 is_mux = of_device_is_compatible(node, "gpio-mux-clock");
Jyri Sarhac873d142014-09-05 15:21:34 +0300257
Stephen Boyd14b04f22016-02-02 17:09:26 -0800258 gpio_name = is_mux ? "select-gpios" : "enable-gpios";
259 gpio = of_get_named_gpio_flags(node, gpio_name, 0, &of_flags);
260 if (gpio < 0) {
261 if (gpio == -EPROBE_DEFER)
262 pr_debug("%s: %s: GPIOs not yet available, retry later\n",
263 node->name, __func__);
264 else
265 pr_err("%s: %s: Can't get '%s' DT property\n",
266 node->name, __func__,
267 gpio_name);
268 return gpio;
269 }
270
271 active_low = of_flags & OF_GPIO_ACTIVE_LOW;
272
273 if (is_mux)
274 clk = clk_register_gpio_mux(&pdev->dev, node->name,
275 parent_names, num_parents, gpio, active_low, 0);
276 else
277 clk = clk_register_gpio_gate(&pdev->dev, node->name,
278 parent_names ? parent_names[0] : NULL, gpio,
279 active_low, 0);
280 if (IS_ERR(clk))
281 return PTR_ERR(clk);
282
283 return of_clk_add_provider(node, of_clk_src_simple_get, clk);
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200284}
285
Stephen Boyd14b04f22016-02-02 17:09:26 -0800286static const struct of_device_id gpio_clk_match_table[] = {
287 { .compatible = "gpio-mux-clock" },
288 { .compatible = "gpio-gate-clock" },
289 { }
290};
Sergej Sawazki80eeb1f2015-06-28 16:24:55 +0200291
Stephen Boyd14b04f22016-02-02 17:09:26 -0800292static struct platform_driver gpio_clk_driver = {
293 .probe = gpio_clk_driver_probe,
294 .driver = {
295 .name = "gpio-clk",
296 .of_match_table = gpio_clk_match_table,
297 },
298};
299builtin_platform_driver(gpio_clk_driver);