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 76bb68a4f1SSergej Sawazki return gpiod_get_value(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 83bb68a4f1SSergej Sawazki gpiod_set_value(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 112908a543aSLinus Walleij /* 113908a543aSLinus Walleij * Set to disabled no matter what: NOTE if the GPIO line is active low 114908a543aSLinus Walleij * the GPIO descriptor knows this and will set it high to deassert the 115908a543aSLinus Walleij * line. This assumes the GPIO descriptor has been requested using 116908a543aSLinus Walleij * GPIOD_ASIS by the callers so we need to initialize it as disabled here. 117908a543aSLinus Walleij */ 118908a543aSLinus Walleij gpiod_set_value(gpiod, 0); 119bb68a4f1SSergej Sawazki 120bb68a4f1SSergej Sawazki init.name = name; 121bb68a4f1SSergej Sawazki init.ops = clk_gpio_ops; 122bb68a4f1SSergej Sawazki init.flags = flags | CLK_IS_BASIC; 123bb68a4f1SSergej Sawazki init.parent_names = parent_names; 124bb68a4f1SSergej Sawazki init.num_parents = num_parents; 125bb68a4f1SSergej Sawazki 126908a543aSLinus Walleij clk_gpio->gpiod = gpiod; 127bb68a4f1SSergej Sawazki clk_gpio->hw.init = &init; 128bb68a4f1SSergej Sawazki 129b120743aSStephen Boyd hw = &clk_gpio->hw; 130bb68a4f1SSergej Sawazki if (dev) 131b120743aSStephen Boyd err = devm_clk_hw_register(dev, hw); 132bb68a4f1SSergej Sawazki else 133b120743aSStephen Boyd err = clk_hw_register(NULL, hw); 134bb68a4f1SSergej Sawazki 135b120743aSStephen Boyd if (!err) 136b120743aSStephen Boyd return hw; 137bb68a4f1SSergej Sawazki 138bb68a4f1SSergej Sawazki if (!dev) { 139bb68a4f1SSergej Sawazki kfree(clk_gpio); 140bb68a4f1SSergej Sawazki } 141bb68a4f1SSergej Sawazki 142b120743aSStephen Boyd return ERR_PTR(err); 143bb68a4f1SSergej Sawazki } 144bb68a4f1SSergej Sawazki 145bb68a4f1SSergej Sawazki /** 146b120743aSStephen Boyd * clk_hw_register_gpio_gate - register a gpio clock gate with the clock 147b120743aSStephen Boyd * framework 148bb68a4f1SSergej Sawazki * @dev: device that is registering this clock 149bb68a4f1SSergej Sawazki * @name: name of this clock 150bb68a4f1SSergej Sawazki * @parent_name: name of this clock's parent 151908a543aSLinus Walleij * @gpiod: gpio descriptor to gate this clock 152bb68a4f1SSergej Sawazki * @flags: clock flags 153bb68a4f1SSergej Sawazki */ 154b120743aSStephen Boyd struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name, 155908a543aSLinus Walleij const char *parent_name, struct gpio_desc *gpiod, 156bb68a4f1SSergej Sawazki unsigned long flags) 157bb68a4f1SSergej Sawazki { 158bb68a4f1SSergej Sawazki return clk_register_gpio(dev, name, 159bb68a4f1SSergej Sawazki (parent_name ? &parent_name : NULL), 160908a543aSLinus Walleij (parent_name ? 1 : 0), gpiod, flags, 161bb68a4f1SSergej Sawazki &clk_gpio_gate_ops); 162bb68a4f1SSergej Sawazki } 163b120743aSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate); 164b120743aSStephen Boyd 165b120743aSStephen Boyd struct clk *clk_register_gpio_gate(struct device *dev, const char *name, 166908a543aSLinus Walleij const char *parent_name, struct gpio_desc *gpiod, 167b120743aSStephen Boyd unsigned long flags) 168b120743aSStephen Boyd { 169b120743aSStephen Boyd struct clk_hw *hw; 170b120743aSStephen Boyd 171908a543aSLinus Walleij hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags); 172b120743aSStephen Boyd if (IS_ERR(hw)) 173b120743aSStephen Boyd return ERR_CAST(hw); 174b120743aSStephen Boyd return hw->clk; 175b120743aSStephen Boyd } 176bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_register_gpio_gate); 177bb68a4f1SSergej Sawazki 178bb68a4f1SSergej Sawazki /** 179b120743aSStephen Boyd * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework 180bb68a4f1SSergej Sawazki * @dev: device that is registering this clock 181bb68a4f1SSergej Sawazki * @name: name of this clock 182bb68a4f1SSergej Sawazki * @parent_names: names of this clock's parents 183bb68a4f1SSergej Sawazki * @num_parents: number of parents listed in @parent_names 184908a543aSLinus Walleij * @gpiod: gpio descriptor to gate this clock 185bb68a4f1SSergej Sawazki * @flags: clock flags 186bb68a4f1SSergej Sawazki */ 187b120743aSStephen Boyd struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name, 188908a543aSLinus Walleij const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, 189908a543aSLinus Walleij unsigned long flags) 190bb68a4f1SSergej Sawazki { 191bb68a4f1SSergej Sawazki if (num_parents != 2) { 192bb68a4f1SSergej Sawazki pr_err("mux-clock %s must have 2 parents\n", name); 193bb68a4f1SSergej Sawazki return ERR_PTR(-EINVAL); 194bb68a4f1SSergej Sawazki } 195bb68a4f1SSergej Sawazki 196bb68a4f1SSergej Sawazki return clk_register_gpio(dev, name, parent_names, num_parents, 197908a543aSLinus Walleij gpiod, flags, &clk_gpio_mux_ops); 198bb68a4f1SSergej Sawazki } 199b120743aSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux); 200b120743aSStephen Boyd 201b120743aSStephen Boyd struct clk *clk_register_gpio_mux(struct device *dev, const char *name, 202908a543aSLinus Walleij const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod, 203908a543aSLinus Walleij unsigned long flags) 204b120743aSStephen Boyd { 205b120743aSStephen Boyd struct clk_hw *hw; 206b120743aSStephen Boyd 207b120743aSStephen Boyd hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents, 208908a543aSLinus Walleij gpiod, flags); 209b120743aSStephen Boyd if (IS_ERR(hw)) 210b120743aSStephen Boyd return ERR_CAST(hw); 211b120743aSStephen Boyd return hw->clk; 212b120743aSStephen Boyd } 213bb68a4f1SSergej Sawazki EXPORT_SYMBOL_GPL(clk_register_gpio_mux); 214bb68a4f1SSergej Sawazki 21514b04f28SStephen Boyd static int gpio_clk_driver_probe(struct platform_device *pdev) 216bb68a4f1SSergej Sawazki { 21714b04f28SStephen Boyd struct device_node *node = pdev->dev.of_node; 21814b04f28SStephen Boyd const char **parent_names, *gpio_name; 2190985df89SStephen Boyd unsigned int num_parents; 220908a543aSLinus Walleij struct gpio_desc *gpiod; 22114b04f28SStephen Boyd struct clk *clk; 222908a543aSLinus Walleij bool is_mux; 223908a543aSLinus Walleij int ret; 224bb68a4f1SSergej Sawazki 2250b2e7886SBrian Norris num_parents = of_clk_get_parent_count(node); 2267ed88aa2SRussell King if (num_parents) { 22714b04f28SStephen Boyd parent_names = devm_kcalloc(&pdev->dev, num_parents, 22814b04f28SStephen Boyd sizeof(char *), GFP_KERNEL); 22914b04f28SStephen Boyd if (!parent_names) 23014b04f28SStephen Boyd return -ENOMEM; 231f66541baSJyri Sarha 23214b04f28SStephen Boyd of_clk_parent_fill(node, parent_names, num_parents); 2337ed88aa2SRussell King } else { 2347ed88aa2SRussell King parent_names = NULL; 2357ed88aa2SRussell King } 236f66541baSJyri Sarha 23714b04f28SStephen Boyd is_mux = of_device_is_compatible(node, "gpio-mux-clock"); 238bb68a4f1SSergej Sawazki 239908a543aSLinus Walleij gpio_name = is_mux ? "select" : "enable"; 240908a543aSLinus Walleij gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_ASIS); 241908a543aSLinus Walleij if (IS_ERR(gpiod)) { 242908a543aSLinus Walleij ret = PTR_ERR(gpiod); 243908a543aSLinus Walleij if (ret == -EPROBE_DEFER) 24414b04f28SStephen Boyd pr_debug("%s: %s: GPIOs not yet available, retry later\n", 24514b04f28SStephen Boyd node->name, __func__); 24614b04f28SStephen Boyd else 247908a543aSLinus Walleij pr_err("%s: %s: Can't get '%s' named GPIO property\n", 24814b04f28SStephen Boyd node->name, __func__, 24914b04f28SStephen Boyd gpio_name); 250908a543aSLinus Walleij return ret; 251bb68a4f1SSergej Sawazki } 252bb68a4f1SSergej Sawazki 25314b04f28SStephen Boyd if (is_mux) 25414b04f28SStephen Boyd clk = clk_register_gpio_mux(&pdev->dev, node->name, 255908a543aSLinus Walleij parent_names, num_parents, gpiod, 0); 25614b04f28SStephen Boyd else 25714b04f28SStephen Boyd clk = clk_register_gpio_gate(&pdev->dev, node->name, 258908a543aSLinus Walleij parent_names ? parent_names[0] : NULL, gpiod, 259908a543aSLinus Walleij 0); 26014b04f28SStephen Boyd if (IS_ERR(clk)) 26114b04f28SStephen Boyd return PTR_ERR(clk); 26214b04f28SStephen Boyd 26314b04f28SStephen Boyd return of_clk_add_provider(node, of_clk_src_simple_get, clk); 264bb68a4f1SSergej Sawazki } 26514b04f28SStephen Boyd 26614b04f28SStephen Boyd static const struct of_device_id gpio_clk_match_table[] = { 26714b04f28SStephen Boyd { .compatible = "gpio-mux-clock" }, 26814b04f28SStephen Boyd { .compatible = "gpio-gate-clock" }, 26914b04f28SStephen Boyd { } 27014b04f28SStephen Boyd }; 27114b04f28SStephen Boyd 27214b04f28SStephen Boyd static struct platform_driver gpio_clk_driver = { 27314b04f28SStephen Boyd .probe = gpio_clk_driver_probe, 27414b04f28SStephen Boyd .driver = { 27514b04f28SStephen Boyd .name = "gpio-clk", 27614b04f28SStephen Boyd .of_match_table = gpio_clk_match_table, 27714b04f28SStephen Boyd }, 27814b04f28SStephen Boyd }; 27914b04f28SStephen Boyd builtin_platform_driver(gpio_clk_driver); 280