aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/devicetree/bindings/clock/gpio-gate-clock.txt21
-rw-r--r--drivers/clk/Makefile1
-rw-r--r--drivers/clk/clk-gpio-gate.c204
-rw-r--r--include/linux/clk-provider.h22
4 files changed, 248 insertions, 0 deletions
diff --git a/Documentation/devicetree/bindings/clock/gpio-gate-clock.txt b/Documentation/devicetree/bindings/clock/gpio-gate-clock.txt
new file mode 100644
index 000000000000..d3379ff9b84b
--- /dev/null
+++ b/Documentation/devicetree/bindings/clock/gpio-gate-clock.txt
@@ -0,0 +1,21 @@
1Binding for simple gpio gated clock.
2
3This binding uses the common clock binding[1].
4
5[1] Documentation/devicetree/bindings/clock/clock-bindings.txt
6
7Required properties:
8- compatible : shall be "gpio-gate-clock".
9- #clock-cells : from common clock binding; shall be set to 0.
10- enable-gpios : GPIO reference for enabling and disabling the clock.
11
12Optional properties:
13- clocks: Maximum of one parent clock is supported.
14
15Example:
16 clock {
17 compatible = "gpio-gate-clock";
18 clocks = <&parentclk>;
19 #clock-cells = <0>;
20 enable-gpios = <&gpio 1 GPIO_ACTIVE_HIGH>;
21 };
diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
index 27c542ba9e73..92a7f6c02394 100644
--- a/drivers/clk/Makefile
+++ b/drivers/clk/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_COMMON_CLK) += clk-gate.o
9obj-$(CONFIG_COMMON_CLK) += clk-mux.o 9obj-$(CONFIG_COMMON_CLK) += clk-mux.o
10obj-$(CONFIG_COMMON_CLK) += clk-composite.o 10obj-$(CONFIG_COMMON_CLK) += clk-composite.o
11obj-$(CONFIG_COMMON_CLK) += clk-fractional-divider.o 11obj-$(CONFIG_COMMON_CLK) += clk-fractional-divider.o
12obj-$(CONFIG_COMMON_CLK) += clk-gpio-gate.o
12ifeq ($(CONFIG_OF), y) 13ifeq ($(CONFIG_OF), y)
13obj-$(CONFIG_COMMON_CLK) += clk-conf.o 14obj-$(CONFIG_COMMON_CLK) += clk-conf.o
14endif 15endif
diff --git a/drivers/clk/clk-gpio-gate.c b/drivers/clk/clk-gpio-gate.c
new file mode 100644
index 000000000000..9dde88533684
--- /dev/null
+++ b/drivers/clk/clk-gpio-gate.c
@@ -0,0 +1,204 @@
1/*
2 * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
3 * Author: Jyri Sarha <jsarha@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Gpio gated clock implementation
10 */
11
12#include <linux/clk-provider.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/gpio.h>
16#include <linux/of_gpio.h>
17#include <linux/err.h>
18#include <linux/device.h>
19
20/**
21 * DOC: basic gpio gated clock which can be enabled and disabled
22 * with gpio output
23 * Traits of this clock:
24 * prepare - clk_(un)prepare only ensures parent is (un)prepared
25 * enable - clk_enable and clk_disable are functional & control gpio
26 * rate - inherits rate from parent. No clk_set_rate support
27 * parent - fixed parent. No clk_set_parent support
28 */
29
30#define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw)
31
32static int clk_gpio_gate_enable(struct clk_hw *hw)
33{
34 struct clk_gpio *clk = to_clk_gpio(hw);
35
36 gpiod_set_value(clk->gpiod, 1);
37
38 return 0;
39}
40
41static void clk_gpio_gate_disable(struct clk_hw *hw)
42{
43 struct clk_gpio *clk = to_clk_gpio(hw);
44
45 gpiod_set_value(clk->gpiod, 0);
46}
47
48static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
49{
50 struct clk_gpio *clk = to_clk_gpio(hw);
51
52 return gpiod_get_value(clk->gpiod);
53}
54
55const struct clk_ops clk_gpio_gate_ops = {
56 .enable = clk_gpio_gate_enable,
57 .disable = clk_gpio_gate_disable,
58 .is_enabled = clk_gpio_gate_is_enabled,
59};
60EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
61
62/**
63 * clk_register_gpio - register a gpip clock with the clock framework
64 * @dev: device that is registering this clock
65 * @name: name of this clock
66 * @parent_name: name of this clock's parent
67 * @gpiod: gpio descriptor to gate this clock
68 */
69struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
70 const char *parent_name, struct gpio_desc *gpiod,
71 unsigned long flags)
72{
73 struct clk_gpio *clk_gpio = NULL;
74 struct clk *clk = ERR_PTR(-EINVAL);
75 struct clk_init_data init = { NULL };
76 unsigned long gpio_flags;
77 int err;
78
79 if (gpiod_is_active_low(gpiod))
80 gpio_flags = GPIOF_OUT_INIT_HIGH;
81 else
82 gpio_flags = GPIOF_OUT_INIT_LOW;
83
84 if (dev)
85 err = devm_gpio_request_one(dev, desc_to_gpio(gpiod),
86 gpio_flags, name);
87 else
88 err = gpio_request_one(desc_to_gpio(gpiod), gpio_flags, name);
89
90 if (err) {
91 pr_err("%s: %s: Error requesting clock control gpio %u\n",
92 __func__, name, desc_to_gpio(gpiod));
93 return ERR_PTR(err);
94 }
95
96 if (dev)
97 clk_gpio = devm_kzalloc(dev, sizeof(struct clk_gpio),
98 GFP_KERNEL);
99 else
100 clk_gpio = kzalloc(sizeof(struct clk_gpio), GFP_KERNEL);
101
102 if (!clk_gpio) {
103 clk = ERR_PTR(-ENOMEM);
104 goto clk_register_gpio_gate_err;
105 }
106
107 init.name = name;
108 init.ops = &clk_gpio_gate_ops;
109 init.flags = flags | CLK_IS_BASIC;
110 init.parent_names = (parent_name ? &parent_name : NULL);
111 init.num_parents = (parent_name ? 1 : 0);
112
113 clk_gpio->gpiod = gpiod;
114 clk_gpio->hw.init = &init;
115
116 clk = clk_register(dev, &clk_gpio->hw);
117
118 if (!IS_ERR(clk))
119 return clk;
120
121 if (!dev)
122 kfree(clk_gpio);
123
124clk_register_gpio_gate_err:
125 gpiod_put(gpiod);
126
127 return clk;
128}
129EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
130
131#ifdef CONFIG_OF
132/**
133 * The clk_register_gpio_gate has to be delayed, because the EPROBE_DEFER
134 * can not be handled properly at of_clk_init() call time.
135 */
136
137struct clk_gpio_gate_delayed_register_data {
138 struct device_node *node;
139 struct mutex lock;
140 struct clk *clk;
141};
142
143static struct clk *of_clk_gpio_gate_delayed_register_get(
144 struct of_phandle_args *clkspec,
145 void *_data)
146{
147 struct clk_gpio_gate_delayed_register_data *data = _data;
148 struct clk *clk;
149 const char *clk_name = data->node->name;
150 const char *parent_name;
151 struct gpio_desc *gpiod;
152 int gpio;
153
154 mutex_lock(&data->lock);
155
156 if (data->clk) {
157 mutex_unlock(&data->lock);
158 return data->clk;
159 }
160
161 gpio = of_get_named_gpio_flags(data->node, "enable-gpios", 0, NULL);
162 if (gpio < 0) {
163 mutex_unlock(&data->lock);
164 if (gpio != -EPROBE_DEFER)
165 pr_err("%s: %s: Can't get 'enable-gpios' DT property\n",
166 __func__, clk_name);
167 return ERR_PTR(gpio);
168 }
169 gpiod = gpio_to_desc(gpio);
170
171 parent_name = of_clk_get_parent_name(data->node, 0);
172
173 clk = clk_register_gpio_gate(NULL, clk_name, parent_name, gpiod, 0);
174 if (IS_ERR(clk)) {
175 mutex_unlock(&data->lock);
176 return clk;
177 }
178
179 data->clk = clk;
180 mutex_unlock(&data->lock);
181
182 return clk;
183}
184
185/**
186 * of_gpio_gate_clk_setup() - Setup function for gpio controlled clock
187 */
188void __init of_gpio_gate_clk_setup(struct device_node *node)
189{
190 struct clk_gpio_gate_delayed_register_data *data;
191
192 data = kzalloc(sizeof(struct clk_gpio_gate_delayed_register_data),
193 GFP_KERNEL);
194 if (!data)
195 return;
196
197 data->node = node;
198 mutex_init(&data->lock);
199
200 of_clk_add_provider(node, of_clk_gpio_gate_delayed_register_get, data);
201}
202EXPORT_SYMBOL_GPL(of_gpio_gate_clk_setup);
203CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup);
204#endif
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 411dd7eb2653..ec1581bd94cd 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -488,6 +488,28 @@ struct clk *clk_register_composite(struct device *dev, const char *name,
488 struct clk_hw *gate_hw, const struct clk_ops *gate_ops, 488 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
489 unsigned long flags); 489 unsigned long flags);
490 490
491/***
492 * struct clk_gpio_gate - gpio gated clock
493 *
494 * @hw: handle between common and hardware-specific interfaces
495 * @gpiod: gpio descriptor
496 *
497 * Clock with a gpio control for enabling and disabling the parent clock.
498 * Implements .enable, .disable and .is_enabled
499 */
500
501struct clk_gpio {
502 struct clk_hw hw;
503 struct gpio_desc *gpiod;
504};
505
506extern const struct clk_ops clk_gpio_gate_ops;
507struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
508 const char *parent_name, struct gpio_desc *gpio,
509 unsigned long flags);
510
511void of_gpio_clk_gate_setup(struct device_node *node);
512
491/** 513/**
492 * clk_register - allocate a new clock, register it and return an opaque cookie 514 * clk_register - allocate a new clock, register it and return an opaque cookie
493 * @dev: device that is registering this clock 515 * @dev: device that is registering this clock