xref: /netbsd-src/sys/dev/i2c/fan53555.c (revision 18f3098ca55677bef03345e9be442d445d06437b)
1 /* $NetBSD: fan53555.c,v 1.9 2021/01/27 02:29:48 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 Jared 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fan53555.c,v 1.9 2021/01/27 02:29:48 thorpej Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/conf.h>
37 #include <sys/bus.h>
38 #include <sys/kmem.h>
39 
40 #include <dev/i2c/i2cvar.h>
41 
42 #include <dev/fdt/fdtvar.h>
43 
44 #define	VSEL0_REG		0x00
45 #define	VSEL1_REG		0x01
46 #define	 VSEL_BUCK_EN			__BIT(7)
47 #define	 VSEL_MODE			__BIT(6)
48 #define	 VSEL_NSEL			__BITS(5,0)
49 #define	CONTROL_REG		0x02
50 #define	 CONTROL_OUTPUT_DISCHARGE	__BIT(7)
51 #define	 CONTROL_SLEW			__BITS(6,4)
52 #define	 CONTROL_RESET			__BIT(2)
53 #define	ID1_REG			0x03
54 #define	 ID1_VENDOR			__BITS(7,5)
55 #define	 ID1_DIE_ID			__BITS(3,0)
56 #define	  SILERGY_DIE_ID_SYR82X		8
57 #define	  SILERGY_DIE_ID_SYR83X		9
58 #define	ID2_REG			0x04
59 #define	 ID2_DIE_REV			__BITS(3,0)
60 #define	MONITOR_REG		0x05
61 #define	 MONITOR_PGOOD			__BIT(7)
62 
63 struct fan53555_softc {
64 	device_t	sc_dev;
65 	i2c_tag_t	sc_i2c;
66 	i2c_addr_t	sc_addr;
67 	int		sc_phandle;
68 
69 	u_int		sc_suspend_reg;
70 	u_int		sc_runtime_reg;
71 
72 	u_int		sc_base;
73 	u_int		sc_step;
74 
75 	u_int		sc_ramp_delay;
76 	u_int		sc_suspend_voltage_selector;
77 };
78 
79 enum fan53555_vendor {
80 	FAN_VENDOR_FAIRCHILD = 1,
81 	FAN_VENDOR_SILERGY,
82 };
83 
84 /*
85  * Transition slew rates.
86  * Array index is reg value, value is slew rate in uV / us
87  */
88 static const int fan53555_slew_rates[] = {
89 	64000, 32000, 16000, 8000, 4000, 2000, 1000, 500
90 };
91 
92 static const struct device_compatible_entry compat_data[] = {
93 	{ .compat = "silergy,syr827",	.value = FAN_VENDOR_SILERGY },
94 	{ .compat = "silergy,syr828",	.value = FAN_VENDOR_SILERGY },
95 	DEVICE_COMPAT_EOL
96 };
97 
98 static uint8_t
fan53555_read(struct fan53555_softc * sc,uint8_t reg,int flags)99 fan53555_read(struct fan53555_softc *sc, uint8_t reg, int flags)
100 {
101 	uint8_t val = 0;
102 	int error;
103 
104 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
105 	    &reg, 1, &val, 1, flags);
106 	if (error != 0)
107 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
108 
109 	return val;
110 }
111 
112 static void
fan53555_write(struct fan53555_softc * sc,uint8_t reg,uint8_t val,int flags)113 fan53555_write(struct fan53555_softc *sc, uint8_t reg, uint8_t val, int flags)
114 {
115 	uint8_t buf[2];
116 	int error;
117 
118 	buf[0] = reg;
119 	buf[1] = val;
120 
121 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
122 	    NULL, 0, buf, 2, flags);
123 	if (error != 0)
124 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
125 }
126 
127 #define	I2C_READ(sc, reg)	fan53555_read((sc), (reg), 0)
128 #define	I2C_WRITE(sc, reg, val)	fan53555_write((sc), (reg), (val), 0)
129 #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, 0)
130 #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, 0)
131 
132 static int
fan53555_acquire(device_t dev)133 fan53555_acquire(device_t dev)
134 {
135 	return 0;
136 }
137 
138 static void
fan53555_release(device_t dev)139 fan53555_release(device_t dev)
140 {
141 }
142 
143 static int
fan53555_enable(device_t dev,bool enable)144 fan53555_enable(device_t dev, bool enable)
145 {
146 	struct fan53555_softc * const sc = device_private(dev);
147 	uint8_t val;
148 
149 	I2C_LOCK(sc);
150 	val = I2C_READ(sc, sc->sc_runtime_reg);
151 	if (enable)
152 		val |= VSEL_BUCK_EN;
153 	else
154 		val &= ~VSEL_BUCK_EN;
155 	I2C_WRITE(sc, sc->sc_runtime_reg, val);
156 	I2C_UNLOCK(sc);
157 
158 	if (sc->sc_ramp_delay)
159 		delay(sc->sc_ramp_delay);
160 
161 	return 0;
162 }
163 
164 static int
fan53555_set_voltage(device_t dev,u_int min_uvol,u_int max_uvol)165 fan53555_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
166 {
167 	struct fan53555_softc * const sc = device_private(dev);
168 	uint8_t val, oval;
169 	u_int cur_uvol;
170 
171 	I2C_LOCK(sc);
172 
173 	/* Get current voltage */
174 	oval = I2C_READ(sc, sc->sc_runtime_reg);
175 	cur_uvol = __SHIFTOUT(oval, VSEL_NSEL) * sc->sc_step + sc->sc_base;
176 
177 	/* Set new voltage */
178 	val = oval & ~VSEL_NSEL;
179 	val |= __SHIFTIN((min_uvol - sc->sc_base) / sc->sc_step, VSEL_NSEL);
180 	I2C_WRITE(sc, sc->sc_runtime_reg, val);
181 
182 	I2C_UNLOCK(sc);
183 
184 	/* Time to delay is based on the number of voltage steps */
185 	if (sc->sc_ramp_delay)
186 		delay((abs(cur_uvol - min_uvol) / sc->sc_step) * sc->sc_ramp_delay);
187 
188 	return 0;
189 }
190 
191 static int
fan53555_get_voltage(device_t dev,u_int * puvol)192 fan53555_get_voltage(device_t dev, u_int *puvol)
193 {
194 	struct fan53555_softc * const sc = device_private(dev);
195 	uint8_t val;
196 
197 	I2C_LOCK(sc);
198 	val = I2C_READ(sc, sc->sc_runtime_reg);
199 	I2C_UNLOCK(sc);
200 
201 	*puvol = __SHIFTOUT(val, VSEL_NSEL) * sc->sc_step + sc->sc_base;
202 
203 	return 0;
204 }
205 
206 static struct fdtbus_regulator_controller_func fan53555_funcs = {
207 	.acquire = fan53555_acquire,
208 	.release = fan53555_release,
209 	.enable = fan53555_enable,
210 	.set_voltage = fan53555_set_voltage,
211 	.get_voltage = fan53555_get_voltage,
212 };
213 
214 static int
fan53555_init(struct fan53555_softc * sc,enum fan53555_vendor vendor)215 fan53555_init(struct fan53555_softc *sc, enum fan53555_vendor vendor)
216 {
217 	uint8_t id1, control;
218 	int n;
219 
220 	I2C_LOCK(sc);
221 	id1 = I2C_READ(sc, ID1_REG);
222 	I2C_UNLOCK(sc);
223 
224 	const u_int die_id = __SHIFTOUT(id1, ID1_DIE_ID);
225 
226 	aprint_naive("\n");
227 	switch (vendor) {
228 	case FAN_VENDOR_SILERGY:
229 		switch (die_id) {
230 		case SILERGY_DIE_ID_SYR82X:
231 			aprint_normal(": Silergy SYR82X\n");
232 			sc->sc_base = 712500;
233 			sc->sc_step = 12500;
234 			break;
235 		case SILERGY_DIE_ID_SYR83X:
236 			aprint_normal(": Silergy SYR83X\n");
237 			sc->sc_base = 712500;
238 			sc->sc_step = 12500;
239 			break;
240 		default:
241 			aprint_error(": Unsupported Silergy chip (0x%x)\n", die_id);
242 			return ENXIO;
243 		}
244 		break;
245 	default:
246 		aprint_error(": Unsupported vendor (%d)\n", vendor);
247 		return ENXIO;
248 	}
249 
250 	sc->sc_suspend_voltage_selector = -1;
251 	of_getprop_uint32(sc->sc_phandle, "fcs,suspend-voltage-selector",
252 	    &sc->sc_suspend_voltage_selector);
253 	switch (sc->sc_suspend_voltage_selector) {
254 	case 0:
255 		sc->sc_suspend_reg = VSEL0_REG;
256 		sc->sc_runtime_reg = VSEL1_REG;
257 		break;
258 	case 1:
259 		sc->sc_suspend_reg = VSEL1_REG;
260 		sc->sc_runtime_reg = VSEL0_REG;
261 		break;
262 	default:
263 		aprint_error_dev(sc->sc_dev, "unsupported 'fcs,suspend-voltage-selector' value %u\n", sc->sc_suspend_voltage_selector);
264 		return EINVAL;
265 	}
266 
267 	of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay",
268 	    &sc->sc_ramp_delay);
269 	if (sc->sc_ramp_delay) {
270 		for (n = 0; n < __arraycount(fan53555_slew_rates); n++)
271 			if (sc->sc_ramp_delay > fan53555_slew_rates[n])
272 				break;
273 		if (n == __arraycount(fan53555_slew_rates)) {
274 			aprint_error_dev(sc->sc_dev, "unsupported 'regulator-ramp-delay' value %u\n", sc->sc_ramp_delay);
275 			return EINVAL;
276 		}
277 		I2C_LOCK(sc);
278 		control = I2C_READ(sc, CONTROL_REG);
279 		control &= ~CONTROL_SLEW;
280 		control |= __SHIFTIN(fan53555_slew_rates[n], CONTROL_SLEW);
281 		I2C_WRITE(sc, CONTROL_REG, control);
282 		I2C_UNLOCK(sc);
283 	}
284 
285 	return 0;
286 }
287 
288 static int
fan53555_match(device_t parent,cfdata_t match,void * aux)289 fan53555_match(device_t parent, cfdata_t match, void *aux)
290 {
291 	struct i2c_attach_args *ia = aux;
292 	int match_result;
293 
294 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
295 		return match_result;
296 
297 	return 0;
298 }
299 
300 static void
fan53555_attach(device_t parent,device_t self,void * aux)301 fan53555_attach(device_t parent, device_t self, void *aux)
302 {
303 	struct fan53555_softc * const sc = device_private(self);
304 	const struct device_compatible_entry *compat;
305 	struct i2c_attach_args *ia = aux;
306 
307 	sc->sc_dev = self;
308 	sc->sc_i2c = ia->ia_tag;
309 	sc->sc_addr = ia->ia_addr;
310 	sc->sc_phandle = ia->ia_cookie;
311 
312 	compat = iic_compatible_lookup(ia, compat_data);
313 	KASSERT(compat != NULL);
314 
315 	if (fan53555_init(sc, compat->value) != 0)
316 		return;
317 
318 	fdtbus_register_regulator_controller(self, sc->sc_phandle,
319 	    &fan53555_funcs);
320 }
321 
322 CFATTACH_DECL_NEW(fan53555reg, sizeof(struct fan53555_softc),
323     fan53555_match, fan53555_attach, NULL, NULL);
324