xref: /freebsd/sys/dev/clk/clk.h (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
1 /*-
2  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _DEV_CLK_H_
28 #define	_DEV_CLK_H_
29 
30 #include "opt_platform.h"
31 
32 #include <sys/kobj.h>
33 #ifdef FDT
34 #include <dev/ofw/ofw_bus.h>
35 #endif
36 #include "clknode_if.h"
37 
38 #define CLKNODE_IDX_NONE	-1		/* Not-selected index */
39 
40 /* clknode flags. */
41 #define	CLK_NODE_STATIC_STRINGS	0x00000001	/* Static name strings */
42 #define	CLK_NODE_GLITCH_FREE	0x00000002	/* Freq can change w/o stop */
43 #define	CLK_NODE_CANNOT_STOP	0x00000004	/* Cannot be disabled */
44 #define	CLK_NODE_LINKED		0x00000008	/* Is linked clock */
45 #define	CLK_NODE_REGISTERED	0x00000020	/* Is already registered */
46 
47 /* Flags passed to clk_set_freq() and clknode_set_freq(). */
48 #define	CLK_SET_ROUND(x)	((x) & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN))
49 #define	CLK_SET_ROUND_EXACT	0
50 #define	CLK_SET_ROUND_UP	0x00000001
51 #define	CLK_SET_ROUND_DOWN	0x00000002
52 #define	CLK_SET_ROUND_MULTIPLE	0x00000004
53 #define	CLK_SET_ROUND_ANY	(CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)
54 
55 #define	CLK_SET_USER_MASK	0x0000FFFF
56 #define	CLK_SET_DRYRUN		0x00010000
57 
58 typedef struct clk *clk_t;
59 
60 /* Initialization parameters for clocknode creation. */
61 struct clknode_init_def {
62 	const char	*name;
63 	intptr_t	id;
64 	const char	**parent_names;
65 	int		parent_cnt;
66 	int		flags;
67 };
68 
69 /*
70  * Shorthands for constructing method tables.
71  */
72 #define	CLKNODEMETHOD		KOBJMETHOD
73 #define	CLKNODEMETHOD_END	KOBJMETHOD_END
74 #define	clknode_method_t	kobj_method_t
75 #define	clknode_class_t		kobj_class_t
76 DECLARE_CLASS(clknode_class);
77 
78 /*
79  *  Clock domain functions.
80  */
81 struct clkdom *clkdom_create(device_t dev);
82 int clkdom_finit(struct clkdom *clkdom);
83 void clkdom_dump(struct clkdom * clkdom);
84 void clkdom_unlock(struct clkdom *clkdom);
85 void clkdom_xlock(struct clkdom *clkdom);
86 
87 /*
88  * Clock providers interface.
89  */
90 struct clkdom *clkdom_get_by_dev(const device_t dev);
91 
92 struct clknode *clknode_create(struct clkdom *clkdom,
93     clknode_class_t clknode_class, const struct clknode_init_def *def);
94 struct clknode *clknode_register(struct clkdom *cldom, struct clknode *clk);
95 #ifdef FDT
96 typedef int clknode_ofw_mapper_func(struct clkdom *clkdom, uint32_t ncells,
97     phandle_t *cells, struct clknode **clk);
98 void clkdom_set_ofw_mapper(struct clkdom *clkdom, clknode_ofw_mapper_func *cmp);
99 #endif
100 
101 void clknode_init_parent_idx(struct clknode *clknode, int idx);
102 int clknode_set_parent_by_idx(struct clknode *clk, int idx);
103 int clknode_set_parent_by_name(struct clknode *clk, const char *name);
104 const char *clknode_get_name(struct clknode *clk);
105 const char **clknode_get_parent_names(struct clknode *clk);
106 int clknode_get_parents_num(struct clknode *clk);
107 int clknode_get_parent_idx(struct clknode *clk);
108 struct clknode *clknode_get_parent(struct clknode *clk);
109 int clknode_get_flags(struct clknode *clk);
110 void *clknode_get_softc(struct clknode *clk);
111 device_t clknode_get_device(struct clknode *clk);
112 struct clknode *clknode_find_by_name(const char *name);
113 struct clknode *clknode_find_by_id(struct clkdom *clkdom, intptr_t id);
114 int clknode_get_freq(struct clknode *clknode, uint64_t *freq);
115 int clknode_set_freq(struct clknode *clknode, uint64_t freq, int flags,
116     int enablecnt);
117 int clknode_test_freq(struct clknode *clknode, uint64_t freq, int flags,
118     int enablecnt, uint64_t *out_freq);
119 int clknode_enable(struct clknode *clknode);
120 int clknode_disable(struct clknode *clknode);
121 int clknode_stop(struct clknode *clknode, int depth);
122 
123 /*
124  * Clock consumers interface.
125  */
126 int clk_get_by_name(device_t dev, const char *name, clk_t *clk);
127 int clk_get_by_id(device_t dev, struct clkdom *clkdom, intptr_t id, clk_t *clk);
128 int clk_release(clk_t clk);
129 int clk_get_freq(clk_t clk, uint64_t *freq);
130 int clk_set_freq(clk_t clk, uint64_t freq, int flags);
131 int clk_test_freq(clk_t clk, uint64_t freq, int flags);
132 int clk_enable(clk_t clk);
133 int clk_disable(clk_t clk);
134 int clk_stop(clk_t clk);
135 int clk_get_parent(clk_t clk, clk_t *parent);
136 int clk_set_parent_by_clk(clk_t clk, clk_t parent);
137 const char *clk_get_name(clk_t clk);
138 
139 static inline uint64_t
clk_freq_diff(uint64_t x,uint64_t y)140 clk_freq_diff(uint64_t x, uint64_t y)
141 {
142 	return (x >= y ? x - y : y - x);
143 }
144 
145 #ifdef FDT
146 int clk_set_assigned(device_t dev, phandle_t node);
147 int clk_get_by_ofw_index(device_t dev, phandle_t node, int idx, clk_t *clk);
148 int clk_get_by_ofw_index_prop(device_t dev, phandle_t cnode, const char *prop, int idx, clk_t *clk);
149 int clk_get_by_ofw_name(device_t dev, phandle_t node, const char *name,
150      clk_t *clk);
151 int clk_parse_ofw_out_names(device_t dev, phandle_t node,
152     const char ***out_names, uint32_t **indices);
153 int clk_parse_ofw_clk_name(device_t dev, phandle_t node, const char **name);
154 #endif
155 
156 #endif	/* _DEV_CLK_H_ */
157