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