Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * Authors: |
| 5 | * Jyri Sarha <jsarha@ti.com> |
| 6 | * Sergej Sawazki <ce3a@gmx.de> |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 7 | * |
| 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 Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 12 | * Gpio controlled clock implementation |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/clk-provider.h> |
Sergej Sawazki | e21b08e | 2015-06-28 16:24:53 +0200 | [diff] [blame] | 16 | #include <linux/export.h> |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 17 | #include <linux/slab.h> |
Mark Brown | 44b4aa9 | 2014-09-30 18:16:22 +0100 | [diff] [blame] | 18 | #include <linux/gpio/consumer.h> |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/device.h> |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/of_device.h> |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * DOC: basic gpio gated clock which can be enabled and disabled |
| 26 | * with gpio output |
| 27 | * Traits of this clock: |
| 28 | * prepare - clk_(un)prepare only ensures parent is (un)prepared |
| 29 | * enable - clk_enable and clk_disable are functional & control gpio |
| 30 | * rate - inherits rate from parent. No clk_set_rate support |
| 31 | * parent - fixed parent. No clk_set_parent support |
| 32 | */ |
| 33 | |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 34 | static int clk_gpio_gate_enable(struct clk_hw *hw) |
| 35 | { |
| 36 | struct clk_gpio *clk = to_clk_gpio(hw); |
| 37 | |
| 38 | gpiod_set_value(clk->gpiod, 1); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static void clk_gpio_gate_disable(struct clk_hw *hw) |
| 44 | { |
| 45 | struct clk_gpio *clk = to_clk_gpio(hw); |
| 46 | |
| 47 | gpiod_set_value(clk->gpiod, 0); |
| 48 | } |
| 49 | |
| 50 | static int clk_gpio_gate_is_enabled(struct clk_hw *hw) |
| 51 | { |
| 52 | struct clk_gpio *clk = to_clk_gpio(hw); |
| 53 | |
| 54 | return gpiod_get_value(clk->gpiod); |
| 55 | } |
| 56 | |
| 57 | const struct clk_ops clk_gpio_gate_ops = { |
| 58 | .enable = clk_gpio_gate_enable, |
| 59 | .disable = clk_gpio_gate_disable, |
| 60 | .is_enabled = clk_gpio_gate_is_enabled, |
| 61 | }; |
| 62 | EXPORT_SYMBOL_GPL(clk_gpio_gate_ops); |
| 63 | |
| 64 | /** |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 65 | * DOC: basic clock multiplexer which can be controlled with a gpio output |
| 66 | * Traits of this clock: |
| 67 | * prepare - clk_prepare only ensures that parents are prepared |
| 68 | * rate - rate is only affected by parent switching. No clk_set_rate support |
| 69 | * parent - parent is adjustable through clk_set_parent |
| 70 | */ |
| 71 | |
| 72 | static u8 clk_gpio_mux_get_parent(struct clk_hw *hw) |
| 73 | { |
| 74 | struct clk_gpio *clk = to_clk_gpio(hw); |
| 75 | |
| 76 | return gpiod_get_value(clk->gpiod); |
| 77 | } |
| 78 | |
| 79 | static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index) |
| 80 | { |
| 81 | struct clk_gpio *clk = to_clk_gpio(hw); |
| 82 | |
| 83 | gpiod_set_value(clk->gpiod, index); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | const struct clk_ops clk_gpio_mux_ops = { |
| 89 | .get_parent = clk_gpio_mux_get_parent, |
| 90 | .set_parent = clk_gpio_mux_set_parent, |
| 91 | .determine_rate = __clk_mux_determine_rate, |
| 92 | }; |
| 93 | EXPORT_SYMBOL_GPL(clk_gpio_mux_ops); |
| 94 | |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 95 | static struct clk_hw *clk_register_gpio(struct device *dev, const char *name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 96 | const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, |
| 97 | unsigned long flags, const struct clk_ops *clk_gpio_ops) |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 98 | { |
| 99 | struct clk_gpio *clk_gpio; |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 100 | struct clk_hw *hw; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 101 | struct clk_init_data init = {}; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 102 | int err; |
| 103 | |
| 104 | if (dev) |
| 105 | clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL); |
| 106 | else |
| 107 | clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL); |
| 108 | |
| 109 | if (!clk_gpio) |
| 110 | return ERR_PTR(-ENOMEM); |
| 111 | |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 112 | /* |
| 113 | * Set to disabled no matter what: NOTE if the GPIO line is active low |
| 114 | * the GPIO descriptor knows this and will set it high to deassert the |
| 115 | * line. This assumes the GPIO descriptor has been requested using |
| 116 | * GPIOD_ASIS by the callers so we need to initialize it as disabled here. |
| 117 | */ |
| 118 | gpiod_set_value(gpiod, 0); |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 119 | |
| 120 | init.name = name; |
| 121 | init.ops = clk_gpio_ops; |
| 122 | init.flags = flags | CLK_IS_BASIC; |
| 123 | init.parent_names = parent_names; |
| 124 | init.num_parents = num_parents; |
| 125 | |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 126 | clk_gpio->gpiod = gpiod; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 127 | clk_gpio->hw.init = &init; |
| 128 | |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 129 | hw = &clk_gpio->hw; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 130 | if (dev) |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 131 | err = devm_clk_hw_register(dev, hw); |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 132 | else |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 133 | err = clk_hw_register(NULL, hw); |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 134 | |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 135 | if (!err) |
| 136 | return hw; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 137 | |
| 138 | if (!dev) { |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 139 | kfree(clk_gpio); |
| 140 | } |
| 141 | |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 142 | return ERR_PTR(err); |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 146 | * clk_hw_register_gpio_gate - register a gpio clock gate with the clock |
| 147 | * framework |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 148 | * @dev: device that is registering this clock |
| 149 | * @name: name of this clock |
| 150 | * @parent_name: name of this clock's parent |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 151 | * @gpiod: gpio descriptor to gate this clock |
Martin Fuzzey | 820ad97 | 2015-03-18 14:53:17 +0100 | [diff] [blame] | 152 | * @flags: clock flags |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 153 | */ |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 154 | struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 155 | const char *parent_name, struct gpio_desc *gpiod, |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 156 | unsigned long flags) |
| 157 | { |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 158 | return clk_register_gpio(dev, name, |
| 159 | (parent_name ? &parent_name : NULL), |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 160 | (parent_name ? 1 : 0), gpiod, flags, |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 161 | &clk_gpio_gate_ops); |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 162 | } |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate); |
| 164 | |
| 165 | struct clk *clk_register_gpio_gate(struct device *dev, const char *name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 166 | const char *parent_name, struct gpio_desc *gpiod, |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 167 | unsigned long flags) |
| 168 | { |
| 169 | struct clk_hw *hw; |
| 170 | |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 171 | hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags); |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 172 | if (IS_ERR(hw)) |
| 173 | return ERR_CAST(hw); |
| 174 | return hw->clk; |
| 175 | } |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 176 | EXPORT_SYMBOL_GPL(clk_register_gpio_gate); |
| 177 | |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 178 | /** |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 179 | * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 180 | * @dev: device that is registering this clock |
| 181 | * @name: name of this clock |
| 182 | * @parent_names: names of this clock's parents |
| 183 | * @num_parents: number of parents listed in @parent_names |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 184 | * @gpiod: gpio descriptor to gate this clock |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 185 | * @flags: clock flags |
| 186 | */ |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 187 | struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 188 | const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, |
| 189 | unsigned long flags) |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 190 | { |
| 191 | if (num_parents != 2) { |
| 192 | pr_err("mux-clock %s must have 2 parents\n", name); |
| 193 | return ERR_PTR(-EINVAL); |
| 194 | } |
| 195 | |
| 196 | return clk_register_gpio(dev, name, parent_names, num_parents, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 197 | gpiod, flags, &clk_gpio_mux_ops); |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 198 | } |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 199 | EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux); |
| 200 | |
| 201 | struct clk *clk_register_gpio_mux(struct device *dev, const char *name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 202 | const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, |
| 203 | unsigned long flags) |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 204 | { |
| 205 | struct clk_hw *hw; |
| 206 | |
| 207 | hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 208 | gpiod, flags); |
Stephen Boyd | b120743 | 2016-02-07 00:27:55 -0800 | [diff] [blame] | 209 | if (IS_ERR(hw)) |
| 210 | return ERR_CAST(hw); |
| 211 | return hw->clk; |
| 212 | } |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 213 | EXPORT_SYMBOL_GPL(clk_register_gpio_mux); |
| 214 | |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 215 | static int gpio_clk_driver_probe(struct platform_device *pdev) |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 216 | { |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 217 | struct device_node *node = pdev->dev.of_node; |
| 218 | const char **parent_names, *gpio_name; |
Stephen Boyd | 0985df8 | 2016-02-19 17:31:52 -0800 | [diff] [blame] | 219 | unsigned int num_parents; |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 220 | struct gpio_desc *gpiod; |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 221 | struct clk *clk; |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 222 | bool is_mux; |
| 223 | int ret; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 224 | |
Brian Norris | 0b2e788 | 2015-12-16 10:35:03 -0800 | [diff] [blame] | 225 | num_parents = of_clk_get_parent_count(node); |
Russell King | 7ed88aa | 2016-01-02 10:01:34 +0000 | [diff] [blame] | 226 | if (num_parents) { |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 227 | parent_names = devm_kcalloc(&pdev->dev, num_parents, |
| 228 | sizeof(char *), GFP_KERNEL); |
| 229 | if (!parent_names) |
| 230 | return -ENOMEM; |
Jyri Sarha | f66541b | 2015-11-17 11:56:44 +0200 | [diff] [blame] | 231 | |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 232 | of_clk_parent_fill(node, parent_names, num_parents); |
Russell King | 7ed88aa | 2016-01-02 10:01:34 +0000 | [diff] [blame] | 233 | } else { |
| 234 | parent_names = NULL; |
| 235 | } |
Jyri Sarha | f66541b | 2015-11-17 11:56:44 +0200 | [diff] [blame] | 236 | |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 237 | is_mux = of_device_is_compatible(node, "gpio-mux-clock"); |
Jyri Sarha | c873d14 | 2014-09-05 15:21:34 +0300 | [diff] [blame] | 238 | |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 239 | gpio_name = is_mux ? "select" : "enable"; |
| 240 | gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_ASIS); |
| 241 | if (IS_ERR(gpiod)) { |
| 242 | ret = PTR_ERR(gpiod); |
| 243 | if (ret == -EPROBE_DEFER) |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 244 | pr_debug("%s: %s: GPIOs not yet available, retry later\n", |
| 245 | node->name, __func__); |
| 246 | else |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 247 | pr_err("%s: %s: Can't get '%s' named GPIO property\n", |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 248 | node->name, __func__, |
| 249 | gpio_name); |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 250 | return ret; |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 253 | if (is_mux) |
| 254 | clk = clk_register_gpio_mux(&pdev->dev, node->name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 255 | parent_names, num_parents, gpiod, 0); |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 256 | else |
| 257 | clk = clk_register_gpio_gate(&pdev->dev, node->name, |
Linus Walleij | 908a543 | 2017-09-24 18:19:18 +0200 | [diff] [blame^] | 258 | parent_names ? parent_names[0] : NULL, gpiod, |
| 259 | 0); |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 260 | if (IS_ERR(clk)) |
| 261 | return PTR_ERR(clk); |
| 262 | |
| 263 | return of_clk_add_provider(node, of_clk_src_simple_get, clk); |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 266 | static const struct of_device_id gpio_clk_match_table[] = { |
| 267 | { .compatible = "gpio-mux-clock" }, |
| 268 | { .compatible = "gpio-gate-clock" }, |
| 269 | { } |
| 270 | }; |
Sergej Sawazki | 80eeb1f | 2015-06-28 16:24:55 +0200 | [diff] [blame] | 271 | |
Stephen Boyd | 14b04f2 | 2016-02-02 17:09:26 -0800 | [diff] [blame] | 272 | static struct platform_driver gpio_clk_driver = { |
| 273 | .probe = gpio_clk_driver_probe, |
| 274 | .driver = { |
| 275 | .name = "gpio-clk", |
| 276 | .of_match_table = gpio_clk_match_table, |
| 277 | }, |
| 278 | }; |
| 279 | builtin_platform_driver(gpio_clk_driver); |