xref: /freebsd/sys/arm64/nvidia/tegra210/max77620.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2020 Michal Meloun <mmel@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _MAX77620_H_
29 
30 #include <sys/clock.h>
31 
32 #define	MAX77620_REG_CNFGGLBL1		0x00
33 #define	MAX77620_REG_CNFGGLBL2		0x01
34 #define	MAX77620_REG_CNFGGLBL3		0x02
35 #define	MAX77620_REG_CNFG1_32K		0x03
36 #define	MAX77620_REG_CNFGBBC		0x04
37 #define	MAX77620_REG_IRQTOP		0x05
38 #define	MAX77620_REG_INTLBT		0x06
39 #define	MAX77620_REG_IRQSD		0x07
40 #define	MAX77620_REG_IRQ_LVL2_L0_7	0x08
41 #define	MAX77620_REG_IRQ_LVL2_L8	0x09
42 #define	MAX77620_REG_IRQ_LVL2_GPIO	0x0A
43 #define	MAX77620_REG_ONOFFIRQ		0x0B
44 #define	MAX77620_REG_NVERC		0x0C
45 #define	MAX77620_REG_IRQTOPM		0x0D
46 #define	MAX77620_REG_INTENLBT		0x0E
47 #define	MAX77620_REG_IRQMASKSD		0x0F
48 #define	MAX77620_REG_IRQ_MSK_L0_7	0x10
49 #define	MAX77620_REG_IRQ_MSK_L8		0x11
50 #define	MAX77620_REG_ONOFFIRQM		0x12
51 #define	MAX77620_REG_STATLBT		0x13
52 #define	MAX77620_REG_STATSD		0x14
53 #define	MAX77620_REG_ONOFFSTAT		0x15
54 #define	MAX77620_REG_SD0		0x16
55 #define	 MAX77620_SD0_VSEL_MASK			0x3F
56 
57 #define	MAX77620_REG_SD1		0x17
58 #define	 MAX77620_SD1_VSEL_MASK			0x7F
59 
60 #define	MAX77620_REG_SD2		0x18
61 #define	MAX77620_REG_SD3		0x19
62 #define	MAX77620_REG_SD4		0x1A
63 #define	 MAX77620_SDX_VSEL_MASK			0xFF
64 
65 #define	MAX77620_REG_DVSSD0		0x1B
66 #define	MAX77620_REG_DVSSD1		0x1C
67 #define	MAX77620_REG_CFG_SD0		0x1D
68 #define	MAX77620_REG_CFG_SD1		0x1E
69 #define	MAX77620_REG_CFG_SD2		0x1F
70 #define	MAX77620_REG_CFG_SD3		0x20
71 #define	MAX77620_REG_CFG_SD4		0x21
72 #define	 MAX77620_SD_SR_MASK			0xC0
73 #define	 MAX77620_SD_SR_SHIFT			6
74 #define	 MAX77620_SD_POWER_MODE_MASK		0x30
75 #define	 MAX77620_SD_POWER_MODE_SHIFT		4
76 #define	 MAX77620_SD_FPWM_MASK			0x04
77 #define	 MAX77620_SD_FPWM_SHIFT			2
78 #define	 MAX77620_SD_FSRADE_MASK		0x01
79 #define	 MAX77620_SD_FSRADE_SHIFT		0
80 
81 #define	MAX77620_REG_CFG2_SD		0x22
82 #define	MAX77620_REG_CFG_LDO0		0x23
83 #define	MAX77620_REG_CFG2_LDO0		0x24
84 #define	MAX77620_REG_CFG_LDO1		0x25
85 #define	MAX77620_REG_CFG2_LDO1		0x26
86 #define	MAX77620_REG_CFG_LDO2		0x27
87 #define	MAX77620_REG_CFG2_LDO2		0x28
88 #define	MAX77620_REG_CFG_LDO3		0x29
89 #define	MAX77620_REG_CFG2_LDO3		0x2A
90 #define	MAX77620_REG_CFG_LDO4		0x2B
91 #define	MAX77620_REG_CFG2_LDO4		0x2C
92 #define	MAX77620_REG_CFG_LDO5		0x2D
93 #define	MAX77620_REG_CFG2_LDO5		0x2E
94 #define	MAX77620_REG_CFG_LDO6		0x2F
95 #define	MAX77620_REG_CFG2_LDO6		0x30
96 #define	MAX77620_REG_CFG_LDO7		0x31
97 #define	MAX77620_REG_CFG2_LDO7		0x32
98 #define	MAX77620_REG_CFG_LDO8		0x33
99 #define	 MAX77620_LDO_POWER_MODE_MASK		0xC0
100 #define	 MAX77620_LDO_POWER_MODE_SHIFT		6
101 #define	 MAX77620_LDO_VSEL_MASK			0x3F
102 
103 #define	MAX77620_REG_CFG2_LDO8		0x34
104 #define	 MAX77620_LDO_SLEW_RATE_MASK		0x1
105 #define	 MAX77620_LDO_SLEW_RATE_SHIFT		0x0
106 
107 #define	MAX77620_REG_CFG3_LDO		0x35
108 
109 #define	MAX77620_REG_GPIO0		0x36
110 #define	MAX77620_REG_GPIO1		0x37
111 #define	MAX77620_REG_GPIO2		0x38
112 #define	MAX77620_REG_GPIO3		0x39
113 #define	MAX77620_REG_GPIO4		0x3A
114 #define	MAX77620_REG_GPIO5		0x3B
115 #define	MAX77620_REG_GPIO6		0x3C
116 #define	MAX77620_REG_GPIO7		0x3D
117 #define	 MAX77620_REG_GPIO_INT_GET(x)		(((x) >> 5) & 0x3)
118 #define	 MAX77620_REG_GPIO_INT(x)		(((x) & 0x3) << 5)
119 #define	  MAX77620_REG_GPIO_INT_NONE			0
120 #define	  MAX77620_REG_GPIO_INT_FALLING			1
121 #define	  MAX77620_REG_GPIO_INT_RISING			2
122 #define	  MAX77620_REG_GPIO_INT_BOTH			3
123 #define	 MAX77620_REG_GPIO_OUTPUT_VAL_GET(x)	(((x) >> 3) & 0x1)
124 #define	 MAX77620_REG_GPIO_OUTPUT_VAL(x)	(((x) & 0x1) << 3)
125 #define	 MAX77620_REG_GPIO_INPUT_VAL_GET(x)	(((x) << 2) & 0x1)
126 #define	 MAX77620_REG_GPIO_INPUT_VAL		(1 << 2)
127 #define	 MAX77620_REG_GPIO_DRV_GET(x)		(((x) >> 0) & 0x1)
128 #define	 MAX77620_REG_GPIO_DRV(x)		(((x) & 0x1) << 0)
129 #define	  MAX77620_REG_GPIO_DRV_PUSHPULL		1
130 #define	  MAX77620_REG_GPIO_DRV_OPENDRAIN		0
131 
132 #define	MAX77620_REG_PUE_GPIO		0x3E
133 #define	MAX77620_REG_PDE_GPIO		0x3F
134 #define	MAX77620_REG_AME_GPIO		0x40
135 #define	MAX77620_REG_ONOFFCNFG1		0x41
136 #define	MAX77620_REG_ONOFFCNFG2		0x42
137 
138 #define	MAX77620_REG_FPS_CFG0		0x43
139 #define	MAX77620_REG_FPS_CFG1		0x44
140 #define	MAX77620_REG_FPS_CFG2		0x45
141 #define	 MAX77620_FPS_TIME_PERIOD_MASK		0x38
142 #define	 MAX77620_FPS_TIME_PERIOD_SHIFT		3
143 #define	 MAX77620_FPS_EN_SRC_MASK		0x06
144 #define	 MAX77620_FPS_EN_SRC_SHIFT		1
145 #define	 MAX77620_FPS_ENFPS_SW_MASK		0x01
146 #define	 MAX77620_FPS_ENFPS_SW			0x01
147 
148 #define	MAX77620_REG_FPS_LDO0		0x46
149 #define	MAX77620_REG_FPS_LDO1		0x47
150 #define	MAX77620_REG_FPS_LDO2		0x48
151 #define	MAX77620_REG_FPS_LDO3		0x49
152 #define	MAX77620_REG_FPS_LDO4		0x4A
153 #define	MAX77620_REG_FPS_LDO5		0x4B
154 #define	MAX77620_REG_FPS_LDO6		0x4C
155 #define	MAX77620_REG_FPS_LDO7		0x4D
156 #define	MAX77620_REG_FPS_LDO8		0x4E
157 #define	MAX77620_REG_FPS_SD0		0x4F
158 #define	MAX77620_REG_FPS_SD1		0x50
159 #define	MAX77620_REG_FPS_SD2		0x51
160 #define	MAX77620_REG_FPS_SD3		0x52
161 #define	MAX77620_REG_FPS_SD4		0x53
162 #define	MAX77620_REG_FPS_GPIO1		0x54
163 #define	MAX77620_REG_FPS_GPIO2		0x55
164 #define	MAX77620_REG_FPS_GPIO3		0x56
165 #define	MAX77620_REG_FPS_RSO		0x57
166 #define	 MAX77620_FPS_SRC_MASK			0xC0
167 #define	 MAX77620_FPS_SRC_SHIFT			6
168 #define	 MAX77620_FPS_PU_PERIOD_MASK		0x38
169 #define	 MAX77620_FPS_PU_PERIOD_SHIFT		3
170 #define	 MAX77620_FPS_PD_PERIOD_MASK		0x07
171 #define	 MAX77620_FPS_PD_PERIOD_SHIFT		0
172 
173 #define	MAX77620_REG_CID0		0x58
174 #define	MAX77620_REG_CID1		0x59
175 #define	MAX77620_REG_CID2		0x5A
176 #define	MAX77620_REG_CID3		0x5B
177 #define	MAX77620_REG_CID4		0x5C
178 #define	MAX77620_REG_CID5		0x5D
179 #define	MAX77620_REG_DVSSD4		0x5E
180 #define	MAX20024_REG_MAX_ADD		0x70
181 
182 /* MIsc FPS definitions. */
183 #define	MAX77620_FPS_COUNT			3
184 #define	MAX77620_FPS_PERIOD_MIN_US		40
185 #define	MAX77620_FPS_PERIOD_MAX_US		2560
186 
187 /* Power modes */
188 #define	MAX77620_POWER_MODE_NORMAL		3
189 #define	MAX77620_POWER_MODE_LPM			2
190 #define	MAX77620_POWER_MODE_GLPM		1
191 #define	MAX77620_POWER_MODE_DISABLE		0
192 
193 
194 struct max77620_reg_sc;
195 struct max77620_gpio_pin;
196 
197 struct max77620_softc {
198 	device_t			dev;
199 	struct sx			lock;
200 	int				bus_addr;
201 	struct resource			*irq_res;
202 	void				*irq_h;
203 
204 	int 				shutdown_fps[MAX77620_FPS_COUNT];
205 	int 				suspend_fps[MAX77620_FPS_COUNT];
206 	int 				event_source[MAX77620_FPS_COUNT];
207 
208 	/* Regulators. */
209 	struct max77620_reg_sc		**regs;
210 	int				nregs;
211 
212 	/* GPIO */
213 	device_t			gpio_busdev;
214 	struct max77620_gpio_pin 	**gpio_pins;
215 	int				gpio_npins;
216 	struct sx			gpio_lock;
217 	uint8_t				gpio_reg_pue;	/* pull-up enables */
218 	uint8_t				gpio_reg_pde;	/* pull-down enables */
219 	uint8_t				gpio_reg_ame;	/* alternate fnc */
220 
221 
222 };
223 
224 #define	RD1(sc, reg, val)	max77620_read(sc, reg, val)
225 #define	WR1(sc, reg, val)	max77620_write(sc, reg, val)
226 #define	RM1(sc, reg, clr, set)	max77620_modify(sc, reg, clr, set)
227 
228 int max77620_read(struct max77620_softc *sc, uint8_t reg, uint8_t *val);
229 int max77620_write(struct max77620_softc *sc, uint8_t reg, uint8_t val);
230 int max77620_modify(struct max77620_softc *sc, uint8_t reg, uint8_t clear,
231     uint8_t set);
232 int max77620_read_buf(struct max77620_softc *sc, uint8_t reg, uint8_t *buf,
233     size_t size);
234 int max77620_write_buf(struct max77620_softc *sc, uint8_t reg, uint8_t *buf,
235     size_t size);
236 
237 /* Regulators */
238 int max77620_regulator_attach(struct max77620_softc *sc, phandle_t node);
239 int max77620_regulator_map(device_t dev, phandle_t xref, int ncells,
240     pcell_t *cells, intptr_t *num);
241 
242 /* RTC */
243 int max77620_rtc_create(struct max77620_softc *sc, phandle_t node);
244 
245 /* GPIO */
246 device_t max77620_gpio_get_bus(device_t dev);
247 int max77620_gpio_pin_max(device_t dev, int *maxpin);
248 int max77620_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
249 int max77620_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags);
250 int max77620_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
251 int max77620_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
252 int max77620_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
253 int max77620_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
254 int max77620_gpio_pin_toggle(device_t dev, uint32_t pin);
255 int max77620_gpio_map_gpios(device_t dev, phandle_t pdev, phandle_t gparent,
256     int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags);
257 int max77620_gpio_attach(struct max77620_softc *sc, phandle_t node);
258 int max77620_pinmux_configure(device_t dev, phandle_t cfgxref);
259 
260 #endif /* _MAX77620_H_ */
261