Commit | Line | Data |
---|---|---|
c873d14d JS |
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> | |
44b4aa97 | 16 | #include <linux/gpio/consumer.h> |
c873d14d JS |
17 | #include <linux/of_gpio.h> |
18 | #include <linux/err.h> | |
19 | #include <linux/device.h> | |
20 | ||
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 | ||
31 | #define to_clk_gpio(_hw) container_of(_hw, struct clk_gpio, hw) | |
32 | ||
33 | static int clk_gpio_gate_enable(struct clk_hw *hw) | |
34 | { | |
35 | struct clk_gpio *clk = to_clk_gpio(hw); | |
36 | ||
37 | gpiod_set_value(clk->gpiod, 1); | |
38 | ||
39 | return 0; | |
40 | } | |
41 | ||
42 | static void clk_gpio_gate_disable(struct clk_hw *hw) | |
43 | { | |
44 | struct clk_gpio *clk = to_clk_gpio(hw); | |
45 | ||
46 | gpiod_set_value(clk->gpiod, 0); | |
47 | } | |
48 | ||
49 | static int clk_gpio_gate_is_enabled(struct clk_hw *hw) | |
50 | { | |
51 | struct clk_gpio *clk = to_clk_gpio(hw); | |
52 | ||
53 | return gpiod_get_value(clk->gpiod); | |
54 | } | |
55 | ||
56 | const struct clk_ops clk_gpio_gate_ops = { | |
57 | .enable = clk_gpio_gate_enable, | |
58 | .disable = clk_gpio_gate_disable, | |
59 | .is_enabled = clk_gpio_gate_is_enabled, | |
60 | }; | |
61 | EXPORT_SYMBOL_GPL(clk_gpio_gate_ops); | |
62 | ||
63 | /** | |
64 | * clk_register_gpio - register a gpip clock with the clock framework | |
65 | * @dev: device that is registering this clock | |
66 | * @name: name of this clock | |
67 | * @parent_name: name of this clock's parent | |
820ad975 MF |
68 | * @gpio: gpio number to gate this clock |
69 | * @active_low: true if gpio should be set to 0 to enable clock | |
70 | * @flags: clock flags | |
c873d14d JS |
71 | */ |
72 | struct clk *clk_register_gpio_gate(struct device *dev, const char *name, | |
820ad975 | 73 | const char *parent_name, unsigned gpio, bool active_low, |
c873d14d JS |
74 | unsigned long flags) |
75 | { | |
76 | struct clk_gpio *clk_gpio = NULL; | |
77 | struct clk *clk = ERR_PTR(-EINVAL); | |
78 | struct clk_init_data init = { NULL }; | |
79 | unsigned long gpio_flags; | |
80 | int err; | |
81 | ||
820ad975 MF |
82 | if (active_low) |
83 | gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_HIGH; | |
c873d14d JS |
84 | else |
85 | gpio_flags = GPIOF_OUT_INIT_LOW; | |
86 | ||
87 | if (dev) | |
820ad975 | 88 | err = devm_gpio_request_one(dev, gpio, gpio_flags, name); |
c873d14d | 89 | else |
820ad975 | 90 | err = gpio_request_one(gpio, gpio_flags, name); |
c873d14d JS |
91 | |
92 | if (err) { | |
93 | pr_err("%s: %s: Error requesting clock control gpio %u\n", | |
820ad975 | 94 | __func__, name, gpio); |
c873d14d JS |
95 | return ERR_PTR(err); |
96 | } | |
97 | ||
98 | if (dev) | |
99 | clk_gpio = devm_kzalloc(dev, sizeof(struct clk_gpio), | |
100 | GFP_KERNEL); | |
101 | else | |
102 | clk_gpio = kzalloc(sizeof(struct clk_gpio), GFP_KERNEL); | |
103 | ||
104 | if (!clk_gpio) { | |
105 | clk = ERR_PTR(-ENOMEM); | |
106 | goto clk_register_gpio_gate_err; | |
107 | } | |
108 | ||
109 | init.name = name; | |
110 | init.ops = &clk_gpio_gate_ops; | |
111 | init.flags = flags | CLK_IS_BASIC; | |
112 | init.parent_names = (parent_name ? &parent_name : NULL); | |
113 | init.num_parents = (parent_name ? 1 : 0); | |
114 | ||
820ad975 | 115 | clk_gpio->gpiod = gpio_to_desc(gpio); |
c873d14d JS |
116 | clk_gpio->hw.init = &init; |
117 | ||
118 | clk = clk_register(dev, &clk_gpio->hw); | |
119 | ||
120 | if (!IS_ERR(clk)) | |
121 | return clk; | |
122 | ||
123 | if (!dev) | |
124 | kfree(clk_gpio); | |
125 | ||
126 | clk_register_gpio_gate_err: | |
820ad975 MF |
127 | if (!dev) |
128 | gpio_free(gpio); | |
c873d14d JS |
129 | |
130 | return clk; | |
131 | } | |
132 | EXPORT_SYMBOL_GPL(clk_register_gpio_gate); | |
133 | ||
134 | #ifdef CONFIG_OF | |
135 | /** | |
136 | * The clk_register_gpio_gate has to be delayed, because the EPROBE_DEFER | |
137 | * can not be handled properly at of_clk_init() call time. | |
138 | */ | |
139 | ||
140 | struct clk_gpio_gate_delayed_register_data { | |
141 | struct device_node *node; | |
142 | struct mutex lock; | |
143 | struct clk *clk; | |
144 | }; | |
145 | ||
146 | static struct clk *of_clk_gpio_gate_delayed_register_get( | |
147 | struct of_phandle_args *clkspec, | |
148 | void *_data) | |
149 | { | |
150 | struct clk_gpio_gate_delayed_register_data *data = _data; | |
151 | struct clk *clk; | |
152 | const char *clk_name = data->node->name; | |
153 | const char *parent_name; | |
c873d14d | 154 | int gpio; |
820ad975 | 155 | enum of_gpio_flags of_flags; |
c873d14d JS |
156 | |
157 | mutex_lock(&data->lock); | |
158 | ||
159 | if (data->clk) { | |
160 | mutex_unlock(&data->lock); | |
161 | return data->clk; | |
162 | } | |
163 | ||
820ad975 MF |
164 | gpio = of_get_named_gpio_flags(data->node, "enable-gpios", 0, |
165 | &of_flags); | |
c873d14d JS |
166 | if (gpio < 0) { |
167 | mutex_unlock(&data->lock); | |
168 | if (gpio != -EPROBE_DEFER) | |
169 | pr_err("%s: %s: Can't get 'enable-gpios' DT property\n", | |
170 | __func__, clk_name); | |
171 | return ERR_PTR(gpio); | |
172 | } | |
c873d14d JS |
173 | |
174 | parent_name = of_clk_get_parent_name(data->node, 0); | |
175 | ||
820ad975 MF |
176 | clk = clk_register_gpio_gate(NULL, clk_name, parent_name, gpio, |
177 | of_flags & OF_GPIO_ACTIVE_LOW, 0); | |
c873d14d JS |
178 | if (IS_ERR(clk)) { |
179 | mutex_unlock(&data->lock); | |
180 | return clk; | |
181 | } | |
182 | ||
183 | data->clk = clk; | |
184 | mutex_unlock(&data->lock); | |
185 | ||
186 | return clk; | |
187 | } | |
188 | ||
189 | /** | |
190 | * of_gpio_gate_clk_setup() - Setup function for gpio controlled clock | |
191 | */ | |
2bb5d1b8 | 192 | static void __init of_gpio_gate_clk_setup(struct device_node *node) |
c873d14d JS |
193 | { |
194 | struct clk_gpio_gate_delayed_register_data *data; | |
195 | ||
196 | data = kzalloc(sizeof(struct clk_gpio_gate_delayed_register_data), | |
197 | GFP_KERNEL); | |
198 | if (!data) | |
199 | return; | |
200 | ||
201 | data->node = node; | |
202 | mutex_init(&data->lock); | |
203 | ||
204 | of_clk_add_provider(node, of_clk_gpio_gate_delayed_register_get, data); | |
205 | } | |
c873d14d JS |
206 | CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup); |
207 | #endif |