1bb68a4f1SSergej Sawazki /* 2bb68a4f1SSergej Sawazki * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com 3bb68a4f1SSergej Sawazki * 4bb68a4f1SSergej Sawazki * Authors: 5bb68a4f1SSergej Sawazki * Jyri Sarha <jsarha@ti.com> 6bb68a4f1SSergej Sawazki * Sergej Sawazki <ce3a@gmx.de> 7bb68a4f1SSergej Sawazki * 8bb68a4f1SSergej Sawazki * This program is free software; you can redistribute it and/or modify 9bb68a4f1SSergej Sawazki * it under the terms of the GNU General Public License version 2 as 10bb68a4f1SSergej Sawazki * published by the Free Software Foundation. 11bb68a4f1SSergej Sawazki * 12bb68a4f1SSergej Sawazki * Gpio controlled clock implementation 13bb68a4f1SSergej Sawazki */ 14bb68a4f1SSergej Sawazki 15bb68a4f1SSergej Sawazki #include <linux/clk-provider.h> 16bb68a4f1SSergej Sawazki #include <linux/export.h> 17bb68a4f1SSergej Sawazki #include <linux/slab.h> 18bb68a4f1SSergej Sawazki #include <linux/gpio/consumer.h> 19bb68a4f1SSergej Sawazki #include <linux/err.h> 20bb68a4f1SSergej Sawazki #include <linux/device.h> 2114b04f28SStephen Boyd #include <linux/platform_device.h> 2214b04f28SStephen Boyd #include <linux/of_device.h> 23bb68a4f1SSergej Sawazki 24bb68a4f1SSergej Sawazki /** 25bb68a4f1SSergej Sawazki * DOC: basic gpio gated clock which can be enabled and disabled 26bb68a4f1SSergej Sawazki * with gpio output 27bb68a4f1SSergej Sawazki * Traits of this clock: 28bb68a4f1SSergej Sawazki * prepare - clk_(un)prepare only ensures parent is (un)prepared 29bb68a4f1SSergej Sawazki * enable - clk_enable and clk_disable are functional & control gpio 30bb68a4f1SSergej Sawazki * rate - inherits rate from parent. No clk_set_rate support 31bb68a4f1SSergej Sawazki * parent - fixed parent. No clk_set_parent support 32bb68a4f1SSergej Sawazki */ 33bb68a4f1SSergej Sawazki 34bb68a4f1SSergej Sawazki static int clk_gpio_gate_enable(struct clk_hw *hw) 35bb68a4f1SSergej Sawazki { 36bb68a4f1SSergej Sawazki struct clk_gpio *clk = to_clk_gpio(hw); 37bb68a4f1SSergej Sawazki 38bb68a4f1SSergej Sawazki gpiod_set_value(clk->gpiod, 1); 39bb68a4f1SSergej Sawazki 40bb68a4f1SSergej Sawazki return 0; 41bb68a4f1SSergej Sawazki } 42bb68a4f1SSergej Sawazki 43bb68a4f1SSergej Sawazki static void clk_gpio_gate_disable(struct clk_hw *hw) 44bb68a4f1SSergej Sawazki { 45bb68a4f1SSergej Sawazki struct clk_gpio *clk = to_clk_gpio(hw); 46bb68a4f1SSergej Sawazki 47bb68a4f1SSergej Sawazki gpiod_set_value(clk->gpiod, 0); 48bb68a4f1SSergej Sawazki } 49bb68a4f1SSergej Sawazki 50bb68a4f1SSergej Sawazki static int clk_gpio_gate_is_enabled(struct clk_hw *hw) 51bb68a4f1SSergej Sawazki { 52bb68a4f1SSergej Sawazki struct clk_gpio *clk = to_clk_gpio(hw); 53bb68a4f1SSergej Sawazki 54bb68a4f1SSergej Sawazki return gpiod_get_value(clk->gpiod); 55bb68a4f1SSergej Sawazki } 56bb68a4f1SSergej Sawazki 57bb68a4f1SSergej Sawazki const struct clk_ops clk_gpio_gate_ops = { 58bb68a4f1SSergej Sawazki .enable = clk_gpio_gate_enable, 59bb68a4f1SSergej Sawazki .disable = clk_gpio_gate_disable, 60bb68a4f1SSergej Sawazki .is_enabled = clk_gpio_gate_is_enabled, 61bb68a4f1SSergej Sawazki }; 62bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_gpio_gate_ops); 63bb68a4f1SSergej Sawazki 64bb68a4f1SSergej Sawazki /** 65bb68a4f1SSergej Sawazki * DOC: basic clock multiplexer which can be controlled with a gpio output 66bb68a4f1SSergej Sawazki * Traits of this clock: 67bb68a4f1SSergej Sawazki * prepare - clk_prepare only ensures that parents are prepared 68bb68a4f1SSergej Sawazki * rate - rate is only affected by parent switching. No clk_set_rate support 69bb68a4f1SSergej Sawazki * parent - parent is adjustable through clk_set_parent 70bb68a4f1SSergej Sawazki */ 71bb68a4f1SSergej Sawazki 72bb68a4f1SSergej Sawazki static u8 clk_gpio_mux_get_parent(struct clk_hw *hw) 73bb68a4f1SSergej Sawazki { 74bb68a4f1SSergej Sawazki struct clk_gpio *clk = to_clk_gpio(hw); 75bb68a4f1SSergej Sawazki 762e838e7fSMike Looijmans return gpiod_get_value_cansleep(clk->gpiod); 77bb68a4f1SSergej Sawazki } 78bb68a4f1SSergej Sawazki 79bb68a4f1SSergej Sawazki static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index) 80bb68a4f1SSergej Sawazki { 81bb68a4f1SSergej Sawazki struct clk_gpio *clk = to_clk_gpio(hw); 82bb68a4f1SSergej Sawazki 832e838e7fSMike Looijmans gpiod_set_value_cansleep(clk->gpiod, index); 84bb68a4f1SSergej Sawazki 85bb68a4f1SSergej Sawazki return 0; 86bb68a4f1SSergej Sawazki } 87bb68a4f1SSergej Sawazki 88bb68a4f1SSergej Sawazki const struct clk_ops clk_gpio_mux_ops = { 89bb68a4f1SSergej Sawazki .get_parent = clk_gpio_mux_get_parent, 90bb68a4f1SSergej Sawazki .set_parent = clk_gpio_mux_set_parent, 91bb68a4f1SSergej Sawazki .determine_rate = __clk_mux_determine_rate, 92bb68a4f1SSergej Sawazki }; 93bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_gpio_mux_ops); 94bb68a4f1SSergej Sawazki 95b120743aSStephen Boyd static struct clk_hw *clk_register_gpio(struct device *dev, const char *name, 96908a543aSLinus Walleij const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, 97908a543aSLinus Walleij unsigned long flags, const struct clk_ops *clk_gpio_ops) 98bb68a4f1SSergej Sawazki { 99bb68a4f1SSergej Sawazki struct clk_gpio *clk_gpio; 100b120743aSStephen Boyd struct clk_hw *hw; 101bb68a4f1SSergej Sawazki struct clk_init_data init = {}; 102bb68a4f1SSergej Sawazki int err; 103bb68a4f1SSergej Sawazki 104bb68a4f1SSergej Sawazki if (dev) 105bb68a4f1SSergej Sawazki clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL); 106bb68a4f1SSergej Sawazki else 107bb68a4f1SSergej Sawazki clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL); 108bb68a4f1SSergej Sawazki 109bb68a4f1SSergej Sawazki if (!clk_gpio) 110bb68a4f1SSergej Sawazki return ERR_PTR(-ENOMEM); 111bb68a4f1SSergej Sawazki 112bb68a4f1SSergej Sawazki init.name = name; 113bb68a4f1SSergej Sawazki init.ops = clk_gpio_ops; 114bb68a4f1SSergej Sawazki init.flags = flags | CLK_IS_BASIC; 115bb68a4f1SSergej Sawazki init.parent_names = parent_names; 116bb68a4f1SSergej Sawazki init.num_parents = num_parents; 117bb68a4f1SSergej Sawazki 118908a543aSLinus Walleij clk_gpio->gpiod = gpiod; 119bb68a4f1SSergej Sawazki clk_gpio->hw.init = &init; 120bb68a4f1SSergej Sawazki 121b120743aSStephen Boyd hw = &clk_gpio->hw; 122bb68a4f1SSergej Sawazki if (dev) 123b120743aSStephen Boyd err = devm_clk_hw_register(dev, hw); 124bb68a4f1SSergej Sawazki else 125b120743aSStephen Boyd err = clk_hw_register(NULL, hw); 126bb68a4f1SSergej Sawazki 127b120743aSStephen Boyd if (!err) 128b120743aSStephen Boyd return hw; 129bb68a4f1SSergej Sawazki 130bb68a4f1SSergej Sawazki if (!dev) { 131bb68a4f1SSergej Sawazki kfree(clk_gpio); 132bb68a4f1SSergej Sawazki } 133bb68a4f1SSergej Sawazki 134b120743aSStephen Boyd return ERR_PTR(err); 135bb68a4f1SSergej Sawazki } 136bb68a4f1SSergej Sawazki 137bb68a4f1SSergej Sawazki /** 138b120743aSStephen Boyd * clk_hw_register_gpio_gate - register a gpio clock gate with the clock 139b120743aSStephen Boyd * framework 140bb68a4f1SSergej Sawazki * @dev: device that is registering this clock 141bb68a4f1SSergej Sawazki * @name: name of this clock 142bb68a4f1SSergej Sawazki * @parent_name: name of this clock's parent 143908a543aSLinus Walleij * @gpiod: gpio descriptor to gate this clock 144bb68a4f1SSergej Sawazki * @flags: clock flags 145bb68a4f1SSergej Sawazki */ 146b120743aSStephen Boyd struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name, 147908a543aSLinus Walleij const char *parent_name, struct gpio_desc *gpiod, 148bb68a4f1SSergej Sawazki unsigned long flags) 149bb68a4f1SSergej Sawazki { 150bb68a4f1SSergej Sawazki return clk_register_gpio(dev, name, 151bb68a4f1SSergej Sawazki (parent_name ? &parent_name : NULL), 152908a543aSLinus Walleij (parent_name ? 1 : 0), gpiod, flags, 153bb68a4f1SSergej Sawazki &clk_gpio_gate_ops); 154bb68a4f1SSergej Sawazki } 155b120743aSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate); 156b120743aSStephen Boyd 157b120743aSStephen Boyd struct clk *clk_register_gpio_gate(struct device *dev, const char *name, 158908a543aSLinus Walleij const char *parent_name, struct gpio_desc *gpiod, 159b120743aSStephen Boyd unsigned long flags) 160b120743aSStephen Boyd { 161b120743aSStephen Boyd struct clk_hw *hw; 162b120743aSStephen Boyd 163908a543aSLinus Walleij hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags); 164b120743aSStephen Boyd if (IS_ERR(hw)) 165b120743aSStephen Boyd return ERR_CAST(hw); 166b120743aSStephen Boyd return hw->clk; 167b120743aSStephen Boyd } 168bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_register_gpio_gate); 169bb68a4f1SSergej Sawazki 170bb68a4f1SSergej Sawazki /** 171b120743aSStephen Boyd * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework 172bb68a4f1SSergej Sawazki * @dev: device that is registering this clock 173bb68a4f1SSergej Sawazki * @name: name of this clock 174bb68a4f1SSergej Sawazki * @parent_names: names of this clock's parents 175bb68a4f1SSergej Sawazki * @num_parents: number of parents listed in @parent_names 176908a543aSLinus Walleij * @gpiod: gpio descriptor to gate this clock 177bb68a4f1SSergej Sawazki * @flags: clock flags 178bb68a4f1SSergej Sawazki */ 179b120743aSStephen Boyd struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name, 180908a543aSLinus Walleij const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, 181908a543aSLinus Walleij unsigned long flags) 182bb68a4f1SSergej Sawazki { 183bb68a4f1SSergej Sawazki if (num_parents != 2) { 184bb68a4f1SSergej Sawazki pr_err("mux-clock %s must have 2 parents\n", name); 185bb68a4f1SSergej Sawazki return ERR_PTR(-EINVAL); 186bb68a4f1SSergej Sawazki } 187bb68a4f1SSergej Sawazki 188bb68a4f1SSergej Sawazki return clk_register_gpio(dev, name, parent_names, num_parents, 189908a543aSLinus Walleij gpiod, flags, &clk_gpio_mux_ops); 190bb68a4f1SSergej Sawazki } 191b120743aSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux); 192b120743aSStephen Boyd 193b120743aSStephen Boyd struct clk *clk_register_gpio_mux(struct device *dev, const char *name, 194908a543aSLinus Walleij const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, 195908a543aSLinus Walleij unsigned long flags) 196b120743aSStephen Boyd { 197b120743aSStephen Boyd struct clk_hw *hw; 198b120743aSStephen Boyd 199b120743aSStephen Boyd hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents, 200908a543aSLinus Walleij gpiod, flags); 201b120743aSStephen Boyd if (IS_ERR(hw)) 202b120743aSStephen Boyd return ERR_CAST(hw); 203b120743aSStephen Boyd return hw->clk; 204b120743aSStephen Boyd } 205bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_register_gpio_mux); 206bb68a4f1SSergej Sawazki 20714b04f28SStephen Boyd static int gpio_clk_driver_probe(struct platform_device *pdev) 208bb68a4f1SSergej Sawazki { 20914b04f28SStephen Boyd struct device_node *node = pdev->dev.of_node; 21014b04f28SStephen Boyd const char **parent_names, *gpio_name; 2110985df89SStephen Boyd unsigned int num_parents; 212908a543aSLinus Walleij struct gpio_desc *gpiod; 21314b04f28SStephen Boyd struct clk *clk; 214908a543aSLinus Walleij bool is_mux; 215908a543aSLinus Walleij int ret; 216bb68a4f1SSergej Sawazki 2170b2e7886SBrian Norris num_parents = of_clk_get_parent_count(node); 2187ed88aa2SRussell King if (num_parents) { 21914b04f28SStephen Boyd parent_names = devm_kcalloc(&pdev->dev, num_parents, 22014b04f28SStephen Boyd sizeof(char *), GFP_KERNEL); 22114b04f28SStephen Boyd if (!parent_names) 22214b04f28SStephen Boyd return -ENOMEM; 223f66541baSJyri Sarha 22414b04f28SStephen Boyd of_clk_parent_fill(node, parent_names, num_parents); 2257ed88aa2SRussell King } else { 2267ed88aa2SRussell King parent_names = NULL; 2277ed88aa2SRussell King } 228f66541baSJyri Sarha 22914b04f28SStephen Boyd is_mux = of_device_is_compatible(node, "gpio-mux-clock"); 230bb68a4f1SSergej Sawazki 231908a543aSLinus Walleij gpio_name = is_mux ? "select" : "enable"; 2321b5d1a58SLinus Walleij gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_OUT_LOW); 233908a543aSLinus Walleij if (IS_ERR(gpiod)) { 234908a543aSLinus Walleij ret = PTR_ERR(gpiod); 235908a543aSLinus Walleij if (ret == -EPROBE_DEFER) 23614b04f28SStephen Boyd pr_debug("%s: %s: GPIOs not yet available, retry later\n", 23714b04f28SStephen Boyd node->name, __func__); 23814b04f28SStephen Boyd else 239908a543aSLinus Walleij pr_err("%s: %s: Can't get '%s' named GPIO property\n", 24014b04f28SStephen Boyd node->name, __func__, 24114b04f28SStephen Boyd gpio_name); 242908a543aSLinus Walleij return ret; 243bb68a4f1SSergej Sawazki } 244bb68a4f1SSergej Sawazki 24514b04f28SStephen Boyd if (is_mux) 24614b04f28SStephen Boyd clk = clk_register_gpio_mux(&pdev->dev, node->name, 247908a543aSLinus Walleij parent_names, num_parents, gpiod, 0); 24814b04f28SStephen Boyd else 24914b04f28SStephen Boyd clk = clk_register_gpio_gate(&pdev->dev, node->name, 250908a543aSLinus Walleij parent_names ? parent_names[0] : NULL, gpiod, 251908a543aSLinus Walleij 0); 25214b04f28SStephen Boyd if (IS_ERR(clk)) 25314b04f28SStephen Boyd return PTR_ERR(clk); 25414b04f28SStephen Boyd 25514b04f28SStephen Boyd return of_clk_add_provider(node, of_clk_src_simple_get, clk); 256bb68a4f1SSergej Sawazki } 25714b04f28SStephen Boyd 25814b04f28SStephen Boyd static const struct of_device_id gpio_clk_match_table[] = { 25914b04f28SStephen Boyd { .compatible = "gpio-mux-clock" }, 26014b04f28SStephen Boyd { .compatible = "gpio-gate-clock" }, 26114b04f28SStephen Boyd { } 26214b04f28SStephen Boyd }; 26314b04f28SStephen Boyd 26414b04f28SStephen Boyd static struct platform_driver gpio_clk_driver = { 26514b04f28SStephen Boyd .probe = gpio_clk_driver_probe, 26614b04f28SStephen Boyd .driver = { 26714b04f28SStephen Boyd .name = "gpio-clk", 26814b04f28SStephen Boyd .of_match_table = gpio_clk_match_table, 26914b04f28SStephen Boyd }, 27014b04f28SStephen Boyd }; 27114b04f28SStephen Boyd builtin_platform_driver(gpio_clk_driver); 272