blob: 08e43224fd522a876ed8cf736e43f2bc739c930d [file] [log] [blame]
Jyri Sarhac873d142014-09-05 15:21:34 +03001/*
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>
Mark Brown44b4aa92014-09-30 18:16:22 +010016#include <linux/gpio/consumer.h>
Jyri Sarhac873d142014-09-05 15:21:34 +030017#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
33static 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
42static 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
49static 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
56const 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};
61EXPORT_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
68 * @gpiod: gpio descriptor to gate this clock
69 */
70struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
71 const char *parent_name, struct gpio_desc *gpiod,
72 unsigned long flags)
73{
74 struct clk_gpio *clk_gpio = NULL;
75 struct clk *clk = ERR_PTR(-EINVAL);
76 struct clk_init_data init = { NULL };
77 unsigned long gpio_flags;
78 int err;
79
80 if (gpiod_is_active_low(gpiod))
81 gpio_flags = GPIOF_OUT_INIT_HIGH;
82 else
83 gpio_flags = GPIOF_OUT_INIT_LOW;
84
85 if (dev)
86 err = devm_gpio_request_one(dev, desc_to_gpio(gpiod),
87 gpio_flags, name);
88 else
89 err = gpio_request_one(desc_to_gpio(gpiod), gpio_flags, name);
90
91 if (err) {
92 pr_err("%s: %s: Error requesting clock control gpio %u\n",
93 __func__, name, desc_to_gpio(gpiod));
94 return ERR_PTR(err);
95 }
96
97 if (dev)
98 clk_gpio = devm_kzalloc(dev, sizeof(struct clk_gpio),
99 GFP_KERNEL);
100 else
101 clk_gpio = kzalloc(sizeof(struct clk_gpio), GFP_KERNEL);
102
103 if (!clk_gpio) {
104 clk = ERR_PTR(-ENOMEM);
105 goto clk_register_gpio_gate_err;
106 }
107
108 init.name = name;
109 init.ops = &clk_gpio_gate_ops;
110 init.flags = flags | CLK_IS_BASIC;
111 init.parent_names = (parent_name ? &parent_name : NULL);
112 init.num_parents = (parent_name ? 1 : 0);
113
114 clk_gpio->gpiod = gpiod;
115 clk_gpio->hw.init = &init;
116
117 clk = clk_register(dev, &clk_gpio->hw);
118
119 if (!IS_ERR(clk))
120 return clk;
121
122 if (!dev)
123 kfree(clk_gpio);
124
125clk_register_gpio_gate_err:
126 gpiod_put(gpiod);
127
128 return clk;
129}
130EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
131
132#ifdef CONFIG_OF
133/**
134 * The clk_register_gpio_gate has to be delayed, because the EPROBE_DEFER
135 * can not be handled properly at of_clk_init() call time.
136 */
137
138struct clk_gpio_gate_delayed_register_data {
139 struct device_node *node;
140 struct mutex lock;
141 struct clk *clk;
142};
143
144static struct clk *of_clk_gpio_gate_delayed_register_get(
145 struct of_phandle_args *clkspec,
146 void *_data)
147{
148 struct clk_gpio_gate_delayed_register_data *data = _data;
149 struct clk *clk;
150 const char *clk_name = data->node->name;
151 const char *parent_name;
152 struct gpio_desc *gpiod;
153 int gpio;
154
155 mutex_lock(&data->lock);
156
157 if (data->clk) {
158 mutex_unlock(&data->lock);
159 return data->clk;
160 }
161
162 gpio = of_get_named_gpio_flags(data->node, "enable-gpios", 0, NULL);
163 if (gpio < 0) {
164 mutex_unlock(&data->lock);
165 if (gpio != -EPROBE_DEFER)
166 pr_err("%s: %s: Can't get 'enable-gpios' DT property\n",
167 __func__, clk_name);
168 return ERR_PTR(gpio);
169 }
170 gpiod = gpio_to_desc(gpio);
171
172 parent_name = of_clk_get_parent_name(data->node, 0);
173
174 clk = clk_register_gpio_gate(NULL, clk_name, parent_name, gpiod, 0);
175 if (IS_ERR(clk)) {
176 mutex_unlock(&data->lock);
177 return clk;
178 }
179
180 data->clk = clk;
181 mutex_unlock(&data->lock);
182
183 return clk;
184}
185
186/**
187 * of_gpio_gate_clk_setup() - Setup function for gpio controlled clock
188 */
189void __init of_gpio_gate_clk_setup(struct device_node *node)
190{
191 struct clk_gpio_gate_delayed_register_data *data;
192
193 data = kzalloc(sizeof(struct clk_gpio_gate_delayed_register_data),
194 GFP_KERNEL);
195 if (!data)
196 return;
197
198 data->node = node;
199 mutex_init(&data->lock);
200
201 of_clk_add_provider(node, of_clk_gpio_gate_delayed_register_get, data);
202}
203EXPORT_SYMBOL_GPL(of_gpio_gate_clk_setup);
204CLK_OF_DECLARE(gpio_gate_clk, "gpio-gate-clock", of_gpio_gate_clk_setup);
205#endif