1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
6 * Jyri Sarha <jsarha@ti.com>
7 * Sergej Sawazki <ce3a@gmx.de>
9 * Gpio controlled clock implementation
12 #include <linux/clk-provider.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/err.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_device.h>
22 * DOC: basic gpio gated clock which can be enabled and disabled
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
31 static int clk_gpio_gate_enable(struct clk_hw
*hw
)
33 struct clk_gpio
*clk
= to_clk_gpio(hw
);
35 gpiod_set_value(clk
->gpiod
, 1);
40 static void clk_gpio_gate_disable(struct clk_hw
*hw
)
42 struct clk_gpio
*clk
= to_clk_gpio(hw
);
44 gpiod_set_value(clk
->gpiod
, 0);
47 static int clk_gpio_gate_is_enabled(struct clk_hw
*hw
)
49 struct clk_gpio
*clk
= to_clk_gpio(hw
);
51 return gpiod_get_value(clk
->gpiod
);
54 const struct clk_ops clk_gpio_gate_ops
= {
55 .enable
= clk_gpio_gate_enable
,
56 .disable
= clk_gpio_gate_disable
,
57 .is_enabled
= clk_gpio_gate_is_enabled
,
59 EXPORT_SYMBOL_GPL(clk_gpio_gate_ops
);
62 * DOC: basic clock multiplexer which can be controlled with a gpio output
63 * Traits of this clock:
64 * prepare - clk_prepare only ensures that parents are prepared
65 * rate - rate is only affected by parent switching. No clk_set_rate support
66 * parent - parent is adjustable through clk_set_parent
69 static u8
clk_gpio_mux_get_parent(struct clk_hw
*hw
)
71 struct clk_gpio
*clk
= to_clk_gpio(hw
);
73 return gpiod_get_value_cansleep(clk
->gpiod
);
76 static int clk_gpio_mux_set_parent(struct clk_hw
*hw
, u8 index
)
78 struct clk_gpio
*clk
= to_clk_gpio(hw
);
80 gpiod_set_value_cansleep(clk
->gpiod
, index
);
85 const struct clk_ops clk_gpio_mux_ops
= {
86 .get_parent
= clk_gpio_mux_get_parent
,
87 .set_parent
= clk_gpio_mux_set_parent
,
88 .determine_rate
= __clk_mux_determine_rate
,
90 EXPORT_SYMBOL_GPL(clk_gpio_mux_ops
);
92 static struct clk_hw
*clk_register_gpio(struct device
*dev
, const char *name
,
93 const char * const *parent_names
, u8 num_parents
, struct gpio_desc
*gpiod
,
94 unsigned long flags
, const struct clk_ops
*clk_gpio_ops
)
96 struct clk_gpio
*clk_gpio
;
98 struct clk_init_data init
= {};
102 clk_gpio
= devm_kzalloc(dev
, sizeof(*clk_gpio
), GFP_KERNEL
);
104 clk_gpio
= kzalloc(sizeof(*clk_gpio
), GFP_KERNEL
);
107 return ERR_PTR(-ENOMEM
);
110 init
.ops
= clk_gpio_ops
;
111 init
.flags
= flags
| CLK_IS_BASIC
;
112 init
.parent_names
= parent_names
;
113 init
.num_parents
= num_parents
;
115 clk_gpio
->gpiod
= gpiod
;
116 clk_gpio
->hw
.init
= &init
;
120 err
= devm_clk_hw_register(dev
, hw
);
122 err
= clk_hw_register(NULL
, hw
);
135 * clk_hw_register_gpio_gate - register a gpio clock gate with the clock
137 * @dev: device that is registering this clock
138 * @name: name of this clock
139 * @parent_name: name of this clock's parent
140 * @gpiod: gpio descriptor to gate this clock
141 * @flags: clock flags
143 struct clk_hw
*clk_hw_register_gpio_gate(struct device
*dev
, const char *name
,
144 const char *parent_name
, struct gpio_desc
*gpiod
,
147 return clk_register_gpio(dev
, name
,
148 (parent_name
? &parent_name
: NULL
),
149 (parent_name
? 1 : 0), gpiod
, flags
,
152 EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate
);
154 struct clk
*clk_register_gpio_gate(struct device
*dev
, const char *name
,
155 const char *parent_name
, struct gpio_desc
*gpiod
,
160 hw
= clk_hw_register_gpio_gate(dev
, name
, parent_name
, gpiod
, flags
);
165 EXPORT_SYMBOL_GPL(clk_register_gpio_gate
);
168 * clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
169 * @dev: device that is registering this clock
170 * @name: name of this clock
171 * @parent_names: names of this clock's parents
172 * @num_parents: number of parents listed in @parent_names
173 * @gpiod: gpio descriptor to gate this clock
174 * @flags: clock flags
176 struct clk_hw
*clk_hw_register_gpio_mux(struct device
*dev
, const char *name
,
177 const char * const *parent_names
, u8 num_parents
, struct gpio_desc
*gpiod
,
180 if (num_parents
!= 2) {
181 pr_err("mux-clock %s must have 2 parents\n", name
);
182 return ERR_PTR(-EINVAL
);
185 return clk_register_gpio(dev
, name
, parent_names
, num_parents
,
186 gpiod
, flags
, &clk_gpio_mux_ops
);
188 EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux
);
190 struct clk
*clk_register_gpio_mux(struct device
*dev
, const char *name
,
191 const char * const *parent_names
, u8 num_parents
, struct gpio_desc
*gpiod
,
196 hw
= clk_hw_register_gpio_mux(dev
, name
, parent_names
, num_parents
,
202 EXPORT_SYMBOL_GPL(clk_register_gpio_mux
);
204 static int gpio_clk_driver_probe(struct platform_device
*pdev
)
206 struct device_node
*node
= pdev
->dev
.of_node
;
207 const char **parent_names
, *gpio_name
;
208 unsigned int num_parents
;
209 struct gpio_desc
*gpiod
;
214 num_parents
= of_clk_get_parent_count(node
);
216 parent_names
= devm_kcalloc(&pdev
->dev
, num_parents
,
217 sizeof(char *), GFP_KERNEL
);
221 of_clk_parent_fill(node
, parent_names
, num_parents
);
226 is_mux
= of_device_is_compatible(node
, "gpio-mux-clock");
228 gpio_name
= is_mux
? "select" : "enable";
229 gpiod
= devm_gpiod_get(&pdev
->dev
, gpio_name
, GPIOD_OUT_LOW
);
231 ret
= PTR_ERR(gpiod
);
232 if (ret
== -EPROBE_DEFER
)
233 pr_debug("%pOFn: %s: GPIOs not yet available, retry later\n",
236 pr_err("%pOFn: %s: Can't get '%s' named GPIO property\n",
243 clk
= clk_register_gpio_mux(&pdev
->dev
, node
->name
,
244 parent_names
, num_parents
, gpiod
, 0);
246 clk
= clk_register_gpio_gate(&pdev
->dev
, node
->name
,
247 parent_names
? parent_names
[0] : NULL
, gpiod
,
252 return of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
255 static const struct of_device_id gpio_clk_match_table
[] = {
256 { .compatible
= "gpio-mux-clock" },
257 { .compatible
= "gpio-gate-clock" },
261 static struct platform_driver gpio_clk_driver
= {
262 .probe
= gpio_clk_driver_probe
,
265 .of_match_table
= gpio_clk_match_table
,
268 builtin_platform_driver(gpio_clk_driver
);