xref: /freebsd/sys/dev/iicbus/pmic/silergy/syr827.c (revision b2f0caf160993c30a0d0e3d52e8a2d03616908cd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/rman.h>
33 #include <sys/kernel.h>
34 #include <sys/reboot.h>
35 #include <sys/module.h>
36 
37 #include <dev/iicbus/iicbus.h>
38 #include <dev/iicbus/iiconf.h>
39 
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <dev/regulator/regulator.h>
44 
45 #include "iicbus_if.h"
46 #include "regdev_if.h"
47 
48 #define	VSEL0	0x00
49 #define	VSEL1	0x01
50 
51 #define	VSEL_BUCK_EN	(1 << 7)
52 #define	VSEL_NSEL_MASK	0x3F
53 #define	VSEL_VOLTAGE_BASE	712500 /* uV */
54 #define	VSEL_VOLTAGE_STEP	12500  /* uV */
55 
56 #define	ID1	0x03
57 #define	 ID1_VENDOR_MASK	0xE0
58 #define	 ID1_VENDOR_SHIFT	5
59 #define	 ID1_DIE_MASK		0xF
60 
61 #define	ID2	0x4
62 #define	 ID2_DIE_REV_MASK	0xF
63 
64 static struct ofw_compat_data compat_data[] = {
65 	{ "silergy,syr827",			1 },
66 	{ NULL,					0 }
67 };
68 
69 struct syr827_reg_sc {
70 	struct regnode		*regnode;
71 	device_t		base_dev;
72 	phandle_t		xref;
73 	struct regnode_std_param *param;
74 
75 	int			volt_reg;
76 	int			suspend_reg;
77 };
78 
79 struct syr827_softc {
80 	uint16_t		addr;
81 	struct intr_config_hook	intr_hook;
82 
83 	/* Regulator */
84 	struct syr827_reg_sc	*reg;
85 };
86 
87 static int
syr827_read(device_t dev,uint8_t reg,uint8_t * data,uint8_t size)88 syr827_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
89 {
90 	return (iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT));
91 }
92 
93 static int
syr827_write(device_t dev,uint8_t reg,uint8_t val)94 syr827_write(device_t dev, uint8_t reg, uint8_t val)
95 {
96 	return (iicdev_writeto(dev, reg, &val, 1, IIC_INTRWAIT));
97 }
98 
99 static int
syr827_regnode_init(struct regnode * regnode)100 syr827_regnode_init(struct regnode *regnode)
101 {
102 	return (0);
103 }
104 
105 static int
syr827_regnode_enable(struct regnode * regnode,bool enable,int * udelay)106 syr827_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
107 {
108 	struct syr827_reg_sc *sc;
109 	uint8_t val;
110 
111 	sc = regnode_get_softc(regnode);
112 
113 	syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
114 	if (enable)
115 		val &= ~VSEL_BUCK_EN;
116 	else
117 		val |= VSEL_BUCK_EN;
118 	syr827_write(sc->base_dev, sc->volt_reg, val);
119 
120 	*udelay = sc->param->ramp_delay;
121 
122 	return (0);
123 }
124 
125 static int
syr827_regnode_set_voltage(struct regnode * regnode,int min_uvolt,int max_uvolt,int * udelay)126 syr827_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
127     int max_uvolt, int *udelay)
128 {
129 	struct syr827_reg_sc *sc;
130 	int cur_uvolt;
131 	uint8_t val;
132 
133 	sc = regnode_get_softc(regnode);
134 
135 	/* Get current voltage */
136 	syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
137 	cur_uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
138 	    VSEL_VOLTAGE_BASE;
139 
140 	/* Set new voltage */
141 	val &= ~VSEL_NSEL_MASK;
142 	val |= ((min_uvolt - VSEL_VOLTAGE_BASE) / VSEL_VOLTAGE_STEP);
143 	syr827_write(sc->base_dev, sc->volt_reg, val);
144 
145 	/* Time to delay is based on the number of voltage steps */
146 	*udelay = sc->param->ramp_delay *
147 	    (abs(cur_uvolt - min_uvolt) / VSEL_VOLTAGE_STEP);
148 
149 	return (0);
150 }
151 
152 static int
syr827_regnode_get_voltage(struct regnode * regnode,int * uvolt)153 syr827_regnode_get_voltage(struct regnode *regnode, int *uvolt)
154 {
155 	struct syr827_reg_sc *sc;
156 	uint8_t val;
157 
158 	sc = regnode_get_softc(regnode);
159 
160 	syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
161 	*uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
162 	    VSEL_VOLTAGE_BASE;
163 
164 	return (0);
165 }
166 
167 static regnode_method_t syr827_regnode_methods[] = {
168 	/* Regulator interface */
169 	REGNODEMETHOD(regnode_init,		syr827_regnode_init),
170 	REGNODEMETHOD(regnode_enable,		syr827_regnode_enable),
171 	REGNODEMETHOD(regnode_set_voltage,	syr827_regnode_set_voltage),
172 	REGNODEMETHOD(regnode_get_voltage,	syr827_regnode_get_voltage),
173 	REGNODEMETHOD_END
174 };
175 DEFINE_CLASS_1(syr827_regnode, syr827_regnode_class, syr827_regnode_methods,
176     sizeof(struct syr827_reg_sc), regnode_class);
177 
178 static struct syr827_reg_sc *
syr827_reg_attach(device_t dev,phandle_t node)179 syr827_reg_attach(device_t dev, phandle_t node)
180 {
181 	struct syr827_reg_sc *reg_sc;
182 	struct regnode_init_def initdef;
183 	struct regnode *regnode;
184 	int suspend_reg;
185 
186 	memset(&initdef, 0, sizeof(initdef));
187 	regulator_parse_ofw_stdparam(dev, node, &initdef);
188 	initdef.id = 0;
189 	initdef.ofw_node = node;
190 	regnode = regnode_create(dev, &syr827_regnode_class, &initdef);
191 	if (regnode == NULL) {
192 		device_printf(dev, "cannot create regulator\n");
193 		return (NULL);
194 	}
195 
196 	reg_sc = regnode_get_softc(regnode);
197 	reg_sc->regnode = regnode;
198 	reg_sc->base_dev = dev;
199 	reg_sc->xref = OF_xref_from_node(node);
200 	reg_sc->param = regnode_get_stdparam(regnode);
201 
202 	if (OF_getencprop(node, "fcs,suspend-voltage-selector", &suspend_reg,
203 	    sizeof(uint32_t)) <= 0)
204 		suspend_reg = 0;
205 
206 	switch (suspend_reg) {
207 	case 0:
208 		reg_sc->suspend_reg = VSEL0;
209 		reg_sc->volt_reg = VSEL1;
210 		break;
211 	case 1:
212 		reg_sc->suspend_reg = VSEL1;
213 		reg_sc->volt_reg = VSEL0;
214 		break;
215 	}
216 
217 	regnode_register(regnode);
218 
219 	return (reg_sc);
220 }
221 
222 static int
syr827_regdev_map(device_t dev,phandle_t xref,int ncells,pcell_t * cells,intptr_t * num)223 syr827_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
224     intptr_t *num)
225 {
226 	struct syr827_softc *sc;
227 
228 	sc = device_get_softc(dev);
229 
230 	if (sc->reg->xref != xref)
231 		return (ENXIO);
232 
233 	*num = 0;
234 
235 	return (0);
236 }
237 
238 static int
syr827_probe(device_t dev)239 syr827_probe(device_t dev)
240 {
241 	if (!ofw_bus_status_okay(dev))
242 		return (ENXIO);
243 
244 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
245 		return (ENXIO);
246 
247 	device_set_desc(dev, "Silergy SYR827 regulator");
248 
249 	return (BUS_PROBE_DEFAULT);
250 }
251 
252 static void
syr827_start(void * pdev)253 syr827_start(void *pdev)
254 {
255 	struct syr827_softc *sc;
256 	device_t dev;
257 	uint8_t val;
258 
259 	dev = pdev;
260 	sc = device_get_softc(dev);
261 
262 	if (bootverbose) {
263 		syr827_read(dev, ID1, &val, 1);
264 		device_printf(dev, "Vendor ID: %x, DIE ID: %x\n",
265 		    (val & ID1_VENDOR_MASK) >> ID1_VENDOR_SHIFT,
266 		    val & ID1_DIE_MASK);
267 		syr827_read(dev, ID2, &val, 1);
268 		device_printf(dev, "DIE Rev: %x\n", val & ID2_DIE_REV_MASK);
269 	}
270 
271 	config_intrhook_disestablish(&sc->intr_hook);
272 }
273 
274 static int
syr827_attach(device_t dev)275 syr827_attach(device_t dev)
276 {
277 	struct syr827_softc *sc;
278 	phandle_t node;
279 
280 	sc = device_get_softc(dev);
281 	node = ofw_bus_get_node(dev);
282 
283 	sc->addr = iicbus_get_addr(dev);
284 
285 	sc->intr_hook.ich_func = syr827_start;
286 	sc->intr_hook.ich_arg = dev;
287 
288 	if (config_intrhook_establish(&sc->intr_hook) != 0)
289 		return (ENOMEM);
290 
291 	sc->reg = syr827_reg_attach(dev, node);
292 	if (sc->reg == NULL) {
293 		device_printf(dev, "cannot attach regulator\n");
294 		return (ENXIO);
295 	}
296 
297 	return (0);
298 }
299 
300 static device_method_t syr827_methods[] = {
301 	/* Device interface */
302 	DEVMETHOD(device_probe,		syr827_probe),
303 	DEVMETHOD(device_attach,	syr827_attach),
304 
305 	/* Regdev interface */
306 	DEVMETHOD(regdev_map,		syr827_regdev_map),
307 
308 	DEVMETHOD_END
309 };
310 
311 static driver_t syr827_driver = {
312 	"syr827",
313 	syr827_methods,
314 	sizeof(struct syr827_softc),
315 };
316 
317 EARLY_DRIVER_MODULE(syr827, iicbus, syr827_driver, 0, 0, BUS_PASS_RESOURCE);
318 MODULE_VERSION(syr827, 1);
319 MODULE_DEPEND(syr827, iicbus, 1, 1, 1);
320 IICBUS_FDT_PNP_INFO(compat_data);
321