Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: add gpio controlled clock multiplexer

Add a common clock driver for basic gpio controlled clock multiplexers.
This driver can be used for devices like 5V41068A or 831721I from IDT
or for discrete multiplexer circuits. The 'select' pin selects one of
two parent clocks.

Cc: Jyri Sarha <jsarha@ti.com>
Signed-off-by: Sergej Sawazki <ce3a@gmx.de>
[sboyd@codeaurora.org: Fix error paths to free memory and do it
in the correct order]
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>

authored by

Sergej Sawazki and committed by
Stephen Boyd
80eeb1f0 281cbb00

+237 -87
+19
Documentation/devicetree/bindings/clock/gpio-mux-clock.txt
··· 1 + Binding for simple gpio clock multiplexer. 2 + 3 + This binding uses the common clock binding[1]. 4 + 5 + [1] Documentation/devicetree/bindings/clock/clock-bindings.txt 6 + 7 + Required properties: 8 + - compatible : shall be "gpio-mux-clock". 9 + - clocks: list of two references to parent clocks. 10 + - #clock-cells : from common clock binding; shall be set to 0. 11 + - select-gpios : GPIO reference for selecting the parent clock. 12 + 13 + Example: 14 + clock { 15 + compatible = "gpio-mux-clock"; 16 + clocks = <&parentclk1>, <&parentclk2>; 17 + #clock-cells = <0>; 18 + select-gpios = <&gpio 1 GPIO_ACTIVE_HIGH>; 19 + };
+201 -87
drivers/clk/clk-gpio-gate.c
··· 1 1 /* 2 2 * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com 3 - * Author: Jyri Sarha <jsarha@ti.com> 3 + * 4 + * Authors: 5 + * Jyri Sarha <jsarha@ti.com> 6 + * Sergej Sawazki <ce3a@gmx.de> 4 7 * 5 8 * This program is free software; you can redistribute it and/or modify 6 9 * it under the terms of the GNU General Public License version 2 as 7 10 * published by the Free Software Foundation. 8 11 * 9 - * Gpio gated clock implementation 12 + * Gpio controlled clock implementation 10 13 */ 11 14 12 15 #include <linux/clk-provider.h> ··· 64 61 EXPORT_SYMBOL_GPL(clk_gpio_gate_ops); 65 62 66 63 /** 67 - * clk_register_gpio - register a gpip clock with the clock framework 64 + * DOC: basic clock multiplexer which can be controlled with a gpio output 65 + * Traits of this clock: 66 + * prepare - clk_prepare only ensures that parents are prepared 67 + * rate - rate is only affected by parent switching. No clk_set_rate support 68 + * parent - parent is adjustable through clk_set_parent 69 + */ 70 + 71 + static u8 clk_gpio_mux_get_parent(struct clk_hw *hw) 72 + { 73 + struct clk_gpio *clk = to_clk_gpio(hw); 74 + 75 + return gpiod_get_value(clk->gpiod); 76 + } 77 + 78 + static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index) 79 + { 80 + struct clk_gpio *clk = to_clk_gpio(hw); 81 + 82 + gpiod_set_value(clk->gpiod, index); 83 + 84 + return 0; 85 + } 86 + 87 + const struct clk_ops clk_gpio_mux_ops = { 88 + .get_parent = clk_gpio_mux_get_parent, 89 + .set_parent = clk_gpio_mux_set_parent, 90 + .determine_rate = __clk_mux_determine_rate, 91 + }; 92 + EXPORT_SYMBOL_GPL(clk_gpio_mux_ops); 93 + 94 + static struct clk *clk_register_gpio(struct device *dev, const char *name, 95 + const char **parent_names, u8 num_parents, unsigned gpio, 96 + bool active_low, unsigned long flags, 97 + const struct clk_ops *clk_gpio_ops) 98 + { 99 + struct clk_gpio *clk_gpio; 100 + struct clk *clk; 101 + struct clk_init_data init = {}; 102 + unsigned long gpio_flags; 103 + int err; 104 + 105 + if (dev) 106 + clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL); 107 + else 108 + clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL); 109 + 110 + if (!clk_gpio) 111 + return ERR_PTR(-ENOMEM); 112 + 113 + if (active_low) 114 + gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_HIGH; 115 + else 116 + gpio_flags = GPIOF_OUT_INIT_LOW; 117 + 118 + if (dev) 119 + err = devm_gpio_request_one(dev, gpio, gpio_flags, name); 120 + else 121 + err = gpio_request_one(gpio, gpio_flags, name); 122 + if (err) { 123 + if (err != -EPROBE_DEFER) 124 + pr_err("%s: %s: Error requesting clock control gpio %u\n", 125 + __func__, name, gpio); 126 + if (!dev) 127 + kfree(clk_gpio); 128 + 129 + return ERR_PTR(err); 130 + } 131 + 132 + init.name = name; 133 + init.ops = clk_gpio_ops; 134 + init.flags = flags | CLK_IS_BASIC; 135 + init.parent_names = parent_names; 136 + init.num_parents = num_parents; 137 + 138 + clk_gpio->gpiod = gpio_to_desc(gpio); 139 + clk_gpio->hw.init = &init; 140 + 141 + if (dev) 142 + clk = devm_clk_register(dev, &clk_gpio->hw); 143 + else 144 + clk = clk_register(NULL, &clk_gpio->hw); 145 + 146 + if (!IS_ERR(clk)) 147 + return clk; 148 + 149 + if (!dev) { 150 + gpiod_put(clk_gpio->gpiod); 151 + kfree(clk_gpio); 152 + } 153 + 154 + return clk; 155 + } 156 + 157 + /** 158 + * clk_register_gpio_gate - register a gpio clock gate with the clock framework 68 159 * @dev: device that is registering this clock 69 160 * @name: name of this clock 70 161 * @parent_name: name of this clock's parent ··· 170 73 const char *parent_name, unsigned gpio, bool active_low, 171 74 unsigned long flags) 172 75 { 173 - struct clk_gpio *clk_gpio = NULL; 174 - struct clk *clk = ERR_PTR(-EINVAL); 175 - struct clk_init_data init = { NULL }; 176 - unsigned long gpio_flags; 177 - int err; 178 - 179 - if (active_low) 180 - gpio_flags = GPIOF_ACTIVE_LOW | GPIOF_OUT_INIT_HIGH; 181 - else 182 - gpio_flags = GPIOF_OUT_INIT_LOW; 183 - 184 - if (dev) 185 - err = devm_gpio_request_one(dev, gpio, gpio_flags, name); 186 - else 187 - err = gpio_request_one(gpio, gpio_flags, name); 188 - 189 - if (err) { 190 - if (err != -EPROBE_DEFER) 191 - pr_err("%s: %s: Error requesting clock control gpio %u\n", 192 - __func__, name, gpio); 193 - return ERR_PTR(err); 194 - } 195 - 196 - if (dev) 197 - clk_gpio = devm_kzalloc(dev, sizeof(struct clk_gpio), 198 - GFP_KERNEL); 199 - else 200 - clk_gpio = kzalloc(sizeof(struct clk_gpio), GFP_KERNEL); 201 - 202 - if (!clk_gpio) { 203 - clk = ERR_PTR(-ENOMEM); 204 - goto clk_register_gpio_gate_err; 205 - } 206 - 207 - init.name = name; 208 - init.ops = &clk_gpio_gate_ops; 209 - init.flags = flags | CLK_IS_BASIC; 210 - init.parent_names = (parent_name ? &parent_name : NULL); 211 - init.num_parents = (parent_name ? 1 : 0); 212 - 213 - clk_gpio->gpiod = gpio_to_desc(gpio); 214 - clk_gpio->hw.init = &init; 215 - 216 - clk = clk_register(dev, &clk_gpio->hw); 217 - 218 - if (!IS_ERR(clk)) 219 - return clk; 220 - 221 - if (!dev) 222 - kfree(clk_gpio); 223 - 224 - clk_register_gpio_gate_err: 225 - if (!dev) 226 - gpio_free(gpio); 227 - 228 - return clk; 76 + return clk_register_gpio(dev, name, 77 + (parent_name ? &parent_name : NULL), 78 + (parent_name ? 1 : 0), gpio, active_low, flags, 79 + &clk_gpio_gate_ops); 229 80 } 230 81 EXPORT_SYMBOL_GPL(clk_register_gpio_gate); 231 82 83 + /** 84 + * clk_register_gpio_mux - register a gpio clock mux with the clock framework 85 + * @dev: device that is registering this clock 86 + * @name: name of this clock 87 + * @parent_names: names of this clock's parents 88 + * @num_parents: number of parents listed in @parent_names 89 + * @gpio: gpio number to gate this clock 90 + * @active_low: true if gpio should be set to 0 to enable clock 91 + * @flags: clock flags 92 + */ 93 + struct clk *clk_register_gpio_mux(struct device *dev, const char *name, 94 + const char **parent_names, u8 num_parents, unsigned gpio, 95 + bool active_low, unsigned long flags) 96 + { 97 + if (num_parents != 2) { 98 + pr_err("mux-clock %s must have 2 parents\n", name); 99 + return ERR_PTR(-EINVAL); 100 + } 101 + 102 + return clk_register_gpio(dev, name, parent_names, num_parents, 103 + gpio, active_low, flags, &clk_gpio_mux_ops); 104 + } 105 + EXPORT_SYMBOL_GPL(clk_register_gpio_mux); 106 + 232 107 #ifdef CONFIG_OF 233 108 /** 234 - * The clk_register_gpio_gate has to be delayed, because the EPROBE_DEFER 109 + * clk_register_get() has to be delayed, because -EPROBE_DEFER 235 110 * can not be handled properly at of_clk_init() call time. 236 111 */ 237 112 238 - struct clk_gpio_gate_delayed_register_data { 113 + struct clk_gpio_delayed_register_data { 114 + const char *gpio_name; 239 115 struct device_node *node; 240 116 struct mutex lock; 241 117 struct clk *clk; 118 + struct clk *(*clk_register_get)(const char *name, 119 + const char **parent_names, u8 num_parents, 120 + unsigned gpio, bool active_low); 242 121 }; 243 122 244 - static struct clk *of_clk_gpio_gate_delayed_register_get( 245 - struct of_phandle_args *clkspec, 246 - void *_data) 123 + static struct clk *of_clk_gpio_delayed_register_get( 124 + struct of_phandle_args *clkspec, void *_data) 247 125 { 248 - struct clk_gpio_gate_delayed_register_data *data = _data; 126 + struct clk_gpio_delayed_register_data *data = _data; 249 127 struct clk *clk; 250 - const char *clk_name = data->node->name; 251 - const char *parent_name; 128 + const char **parent_names; 129 + int i, num_parents; 252 130 int gpio; 253 131 enum of_gpio_flags of_flags; 254 132 ··· 234 162 return data->clk; 235 163 } 236 164 237 - gpio = of_get_named_gpio_flags(data->node, "enable-gpios", 0, 238 - &of_flags); 165 + gpio = of_get_named_gpio_flags(data->node, data->gpio_name, 0, 166 + &of_flags); 239 167 if (gpio < 0) { 240 168 mutex_unlock(&data->lock); 241 - if (gpio != -EPROBE_DEFER) 242 - pr_err("%s: %s: Can't get 'enable-gpios' DT property\n", 243 - __func__, clk_name); 169 + if (gpio == -EPROBE_DEFER) 170 + pr_debug("%s: %s: GPIOs not yet available, retry later\n", 171 + data->node->name, __func__); 172 + else 173 + pr_err("%s: %s: Can't get '%s' DT property\n", 174 + data->node->name, __func__, 175 + data->gpio_name); 244 176 return ERR_PTR(gpio); 245 177 } 246 178 247 - parent_name = of_clk_get_parent_name(data->node, 0); 179 + num_parents = of_clk_get_parent_count(data->node); 248 180 249 - clk = clk_register_gpio_gate(NULL, clk_name, parent_name, gpio, 250 - of_flags & OF_GPIO_ACTIVE_LOW, 0); 251 - if (IS_ERR(clk)) { 252 - mutex_unlock(&data->lock); 253 - return clk; 254 - } 181 + parent_names = kcalloc(num_parents, sizeof(char *), GFP_KERNEL); 182 + if (!parent_names) 183 + return ERR_PTR(-ENOMEM); 184 + 185 + for (i = 0; i < num_parents; i++) 186 + parent_names[i] = of_clk_get_parent_name(data->node, i); 187 + 188 + clk = data->clk_register_get(data->node->name, parent_names, 189 + num_parents, gpio, of_flags & OF_GPIO_ACTIVE_LOW); 190 + if (IS_ERR(clk)) 191 + goto out; 255 192 256 193 data->clk = clk; 194 + out: 257 195 mutex_unlock(&data->lock); 196 + kfree(parent_names); 258 197 259 198 return clk; 260 199 } 261 200 262 - /** 263 - * of_gpio_gate_clk_setup() - Setup function for gpio controlled clock 264 - */ 265 - static void __init of_gpio_gate_clk_setup(struct device_node *node) 201 + static struct clk *of_clk_gpio_gate_delayed_register_get(const char *name, 202 + const char **parent_names, u8 num_parents, 203 + unsigned gpio, bool active_low) 266 204 { 267 - struct clk_gpio_gate_delayed_register_data *data; 205 + return clk_register_gpio_gate(NULL, name, parent_names[0], 206 + gpio, active_low, 0); 207 + } 268 208 269 - data = kzalloc(sizeof(struct clk_gpio_gate_delayed_register_data), 270 - GFP_KERNEL); 209 + static struct clk *of_clk_gpio_mux_delayed_register_get(const char *name, 210 + const char **parent_names, u8 num_parents, unsigned gpio, 211 + bool active_low) 212 + { 213 + return clk_register_gpio_mux(NULL, name, parent_names, num_parents, 214 + gpio, active_low, 0); 215 + } 216 + 217 + static void __init of_gpio_clk_setup(struct device_node *node, 218 + const char *gpio_name, 219 + struct clk *(*clk_register_get)(const char *name, 220 + const char **parent_names, u8 num_parents, 221 + unsigned gpio, bool active_low)) 222 + { 223 + struct clk_gpio_delayed_register_data *data; 224 + 225 + data = kzalloc(sizeof(*data), GFP_KERNEL); 271 226 if (!data) 272 227 return; 273 228 274 229 data->node = node; 230 + data->gpio_name = gpio_name; 231 + data->clk_register_get = clk_register_get; 275 232 mutex_init(&data->lock); 276 233 277 - of_clk_add_provider(node, of_clk_gpio_gate_delayed_register_get, data); 234 + of_clk_add_provider(node, of_clk_gpio_delayed_register_get, data); 235 + } 236 + 237 + static void __init of_gpio_gate_clk_setup(struct device_node *node) 238 + { 239 + of_gpio_clk_setup(node, "enable-gpios", 240 + of_clk_gpio_gate_delayed_register_get); 278 241 } 279 242 CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup); 243 + 244 + void __init of_gpio_mux_clk_setup(struct device_node *node) 245 + { 246 + of_gpio_clk_setup(node, "select-gpios", 247 + of_clk_gpio_mux_delayed_register_get); 248 + } 249 + CLK_OF_DECLARE(gpio_mux_clk, "gpio-mux-clock", of_gpio_mux_clk_setup); 280 250 #endif
+17
include/linux/clk-provider.h
··· 550 550 void of_gpio_clk_gate_setup(struct device_node *node); 551 551 552 552 /** 553 + * struct clk_gpio_mux - gpio controlled clock multiplexer 554 + * 555 + * @hw: see struct clk_gpio 556 + * @gpiod: gpio descriptor to select the parent of this clock multiplexer 557 + * 558 + * Clock with a gpio control for selecting the parent clock. 559 + * Implements .get_parent, .set_parent and .determine_rate 560 + */ 561 + 562 + extern const struct clk_ops clk_gpio_mux_ops; 563 + struct clk *clk_register_gpio_mux(struct device *dev, const char *name, 564 + const char **parent_names, u8 num_parents, unsigned gpio, 565 + bool active_low, unsigned long flags); 566 + 567 + void of_gpio_mux_clk_setup(struct device_node *node); 568 + 569 + /** 553 570 * clk_register - allocate a new clock, register it and return an opaque cookie 554 571 * @dev: device that is registering this clock 555 572 * @hw: link to hardware-specific clock data