xref: /netbsd-src/sys/dev/clk/clk.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* $NetBSD: clk.c,v 1.1 2015/12/05 13:31:07 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: clk.c,v 1.1 2015/12/05 13:31:07 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 
34 #include <dev/clk/clk.h>
35 #include <dev/clk/clk_backend.h>
36 
37 static const char *clk_backend = NULL;
38 static const struct clk_funcs *clk_funcs = NULL;
39 static void *clk_priv = NULL;
40 
41 int
42 clk_backend_register(const char *name, const struct clk_funcs *funcs, void *priv)
43 {
44 	KASSERT(clk_funcs == NULL);
45 
46 	clk_backend = name;
47 	clk_funcs = funcs;
48 	clk_priv = priv;
49 
50 	return 0;
51 }
52 
53 struct clk *
54 clk_get(const char *name)
55 {
56 	if (clk_funcs == NULL)
57 		return NULL;
58 	return clk_funcs->get(clk_priv, name);
59 }
60 
61 void
62 clk_put(struct clk *clk)
63 {
64 	return clk_funcs->put(clk_priv, clk);
65 }
66 
67 u_int
68 clk_get_rate(struct clk *clk)
69 {
70 	return clk_funcs->get_rate(clk_priv, clk);
71 }
72 
73 int
74 clk_set_rate(struct clk *clk, u_int rate)
75 {
76 	if (clk->flags & CLK_SET_RATE_PARENT) {
77 		return clk_set_rate(clk_get_parent(clk), rate);
78 	} else {
79 		return clk_funcs->set_rate(clk_priv, clk, rate);
80 	}
81 }
82 
83 int
84 clk_enable(struct clk *clk)
85 {
86 	return clk_funcs->enable(clk_priv, clk);
87 }
88 
89 int
90 clk_disable(struct clk *clk)
91 {
92 	return clk_funcs->disable(clk_priv, clk);
93 }
94 
95 int
96 clk_set_parent(struct clk *clk, struct clk *parent_clk)
97 {
98 	return clk_funcs->set_parent(clk_priv, clk, parent_clk);
99 }
100 
101 struct clk *
102 clk_get_parent(struct clk *clk)
103 {
104 	return clk_funcs->get_parent(clk_priv, clk);
105 }
106