1 /*-
2  * Copyright 2016 Michal Meloun <[email protected]>
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  * $FreeBSD$
27  */
28 
29 #ifndef _DEV_EXTRES_REGULATOR_H_
30 #define _DEV_EXTRES_REGULATOR_H_
31 #include "opt_platform.h"
32 
33 #include <sys/kobj.h>
34 #include <sys/sysctl.h>
35 #ifdef FDT
36 #include <dev/ofw/ofw_bus.h>
37 #endif
38 #include "regnode_if.h"
39 
40 SYSCTL_DECL(_hw_regulator);
41 
42 #define REGULATOR_FLAGS_STATIC		0x00000001  /* Static strings */
43 #define REGULATOR_FLAGS_NOT_DISABLE	0x00000002  /* Cannot be disabled */
44 
45 #define REGULATOR_STATUS_ENABLED	0x00000001
46 #define REGULATOR_STATUS_OVERCURRENT	0x00000002
47 
48 typedef struct regulator *regulator_t;
49 
50 /* Standard regulator parameters. */
51 struct regnode_std_param {
52 	int 			min_uvolt;	/* In uV */
53 	int 			max_uvolt;	/* In uV */
54 	int 			min_uamp;	/* In uA */
55 	int 			max_uamp;	/* In uA */
56 	int 			ramp_delay;	/* In uV/usec */
57 	int 			enable_delay;	/* In usec */
58 	bool 			boot_on;	/* Is enabled on boot */
59 	bool 			always_on;	/* Must be enabled */
60 	int			enable_active_high;
61 };
62 
63 /* Initialization parameters. */
64 struct regnode_init_def {
65 	char			*name;		/* Regulator name */
66 	char			*parent_name;	/* Name of parent regulator */
67 	struct regnode_std_param std_param;	/* Standard parameters */
68 	intptr_t		id;		/* Regulator ID */
69 	int			flags;		/* Flags */
70 #ifdef FDT
71 	 phandle_t 		ofw_node;	/* OFW node of regulator */
72 #endif
73 };
74 
75 struct regulator_range {
76 	int		min_uvolt;
77 	int		step_uvolt;
78 	uint8_t		min_sel;
79 	uint8_t		max_sel;
80 };
81 
82 #define	REG_RANGE_INIT(_min_sel, _max_sel, _min_uvolt, _step_uvolt) {	\
83 	.min_sel	= _min_sel,					\
84 	.max_sel	= _max_sel,					\
85 	.min_uvolt	= _min_uvolt,					\
86 	.step_uvolt	= _step_uvolt,					\
87 }
88 
89 /*
90  * Shorthands for constructing method tables.
91  */
92 #define	REGNODEMETHOD		KOBJMETHOD
93 #define	REGNODEMETHOD_END	KOBJMETHOD_END
94 #define regnode_method_t	kobj_method_t
95 #define regnode_class_t		kobj_class_t
96 DECLARE_CLASS(regnode_class);
97 
98 /* Providers interface. */
99 struct regnode *regnode_create(device_t pdev, regnode_class_t regnode_class,
100     struct regnode_init_def *def);
101 struct regnode *regnode_register(struct regnode *regnode);
102 const char *regnode_get_name(struct regnode *regnode);
103 const char *regnode_get_parent_name(struct regnode *regnode);
104 struct regnode *regnode_get_parent(struct regnode *regnode);
105 int regnode_get_flags(struct regnode *regnode);
106 void *regnode_get_softc(struct regnode *regnode);
107 device_t regnode_get_device(struct regnode *regnode);
108 struct regnode_std_param *regnode_get_stdparam(struct regnode *regnode);
109 void regnode_topo_unlock(void);
110 void regnode_topo_xlock(void);
111 void regnode_topo_slock(void);
112 
113 int regnode_enable(struct regnode *regnode);
114 int regnode_disable(struct regnode *regnode);
115 int regnode_stop(struct regnode *regnode, int depth);
116 int regnode_status(struct regnode *regnode, int *status);
117 int regnode_get_voltage(struct regnode *regnode, int *uvolt);
118 int regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt);
119 int regnode_set_constraint(struct regnode *regnode);
120 
121 /* Standard method that aren't default */
122 int regnode_method_check_voltage(struct regnode *regnode, int uvolt);
123 
124 #ifdef FDT
125 phandle_t regnode_get_ofw_node(struct regnode *regnode);
126 #endif
127 
128 /* Consumers interface. */
129 int regulator_get_by_name(device_t cdev, const char *name,
130      regulator_t *regulator);
131 int regulator_get_by_id(device_t cdev, device_t pdev, intptr_t id,
132     regulator_t *regulator);
133 int regulator_release(regulator_t regulator);
134 const char *regulator_get_name(regulator_t regulator);
135 int regulator_enable(regulator_t reg);
136 int regulator_disable(regulator_t reg);
137 int regulator_stop(regulator_t reg);
138 int regulator_status(regulator_t reg, int *status);
139 int regulator_get_voltage(regulator_t reg, int *uvolt);
140 int regulator_set_voltage(regulator_t reg, int min_uvolt, int max_uvolt);
141 int regulator_check_voltage(regulator_t reg, int uvolt);
142 
143 #ifdef FDT
144 int regulator_get_by_ofw_property(device_t dev, phandle_t node, char *name,
145     regulator_t *reg);
146 int regulator_parse_ofw_stdparam(device_t dev, phandle_t node,
147     struct regnode_init_def *def);
148 #endif
149 
150 /* Utility functions */
151 int regulator_range_volt_to_sel8(struct regulator_range *ranges, int nranges,
152     int min_uvolt, int max_uvolt, uint8_t *out_sel);
153 int regulator_range_sel8_to_volt(struct regulator_range *ranges, int nranges,
154    uint8_t sel, int *volt);
155 
156 #endif /* _DEV_EXTRES_REGULATOR_H_ */
157