2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/reboot.h>
38 #include <sys/module.h>
40 #include <dev/iicbus/iicbus.h>
41 #include <dev/iicbus/iiconf.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/extres/regulator/regulator.h>
48 #include "iicbus_if.h"
49 #include "regdev_if.h"
54 #define VSEL_BUCK_EN (1 << 7)
55 #define VSEL_NSEL_MASK 0x3F
56 #define VSEL_VOLTAGE_BASE 712500 /* uV */
57 #define VSEL_VOLTAGE_STEP 12500 /* uV */
60 #define ID1_VENDOR_MASK 0xE0
61 #define ID1_VENDOR_SHIFT 5
62 #define ID1_DIE_MASK 0xF
65 #define ID2_DIE_REV_MASK 0xF
67 static struct ofw_compat_data compat_data[] = {
68 { "silergy,syr827", 1 },
72 struct syr827_reg_sc {
73 struct regnode *regnode;
76 struct regnode_std_param *param;
84 struct intr_config_hook intr_hook;
87 struct syr827_reg_sc *reg;
91 syr827_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
93 return (iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT));
97 syr827_write(device_t dev, uint8_t reg, uint8_t val)
99 return (iicdev_writeto(dev, reg, &val, 1, IIC_INTRWAIT));
103 syr827_regnode_init(struct regnode *regnode)
109 syr827_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
111 struct syr827_reg_sc *sc;
114 sc = regnode_get_softc(regnode);
116 syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
118 val &= ~VSEL_BUCK_EN;
121 syr827_write(sc->base_dev, sc->volt_reg, val);
123 *udelay = sc->param->ramp_delay;
129 syr827_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
130 int max_uvolt, int *udelay)
132 struct syr827_reg_sc *sc;
136 sc = regnode_get_softc(regnode);
138 /* Get current voltage */
139 syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
140 cur_uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
143 /* Set new voltage */
144 val &= ~VSEL_NSEL_MASK;
145 val |= ((min_uvolt - VSEL_VOLTAGE_BASE) / VSEL_VOLTAGE_STEP);
146 syr827_write(sc->base_dev, sc->volt_reg, val);
148 /* Time to delay is based on the number of voltage steps */
149 *udelay = sc->param->ramp_delay *
150 (abs(cur_uvolt - min_uvolt) / VSEL_VOLTAGE_STEP);
156 syr827_regnode_get_voltage(struct regnode *regnode, int *uvolt)
158 struct syr827_reg_sc *sc;
161 sc = regnode_get_softc(regnode);
163 syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
164 *uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
170 static regnode_method_t syr827_regnode_methods[] = {
171 /* Regulator interface */
172 REGNODEMETHOD(regnode_init, syr827_regnode_init),
173 REGNODEMETHOD(regnode_enable, syr827_regnode_enable),
174 REGNODEMETHOD(regnode_set_voltage, syr827_regnode_set_voltage),
175 REGNODEMETHOD(regnode_get_voltage, syr827_regnode_get_voltage),
178 DEFINE_CLASS_1(syr827_regnode, syr827_regnode_class, syr827_regnode_methods,
179 sizeof(struct syr827_reg_sc), regnode_class);
181 static struct syr827_reg_sc *
182 syr827_reg_attach(device_t dev, phandle_t node)
184 struct syr827_reg_sc *reg_sc;
185 struct regnode_init_def initdef;
186 struct regnode *regnode;
189 memset(&initdef, 0, sizeof(initdef));
190 regulator_parse_ofw_stdparam(dev, node, &initdef);
192 initdef.ofw_node = node;
193 regnode = regnode_create(dev, &syr827_regnode_class, &initdef);
194 if (regnode == NULL) {
195 device_printf(dev, "cannot create regulator\n");
199 reg_sc = regnode_get_softc(regnode);
200 reg_sc->regnode = regnode;
201 reg_sc->base_dev = dev;
202 reg_sc->xref = OF_xref_from_node(node);
203 reg_sc->param = regnode_get_stdparam(regnode);
205 if (OF_getencprop(node, "fcs,suspend-voltage-selector", &suspend_reg,
206 sizeof(uint32_t)) <= 0)
209 switch (suspend_reg) {
211 reg_sc->suspend_reg = VSEL0;
212 reg_sc->volt_reg = VSEL1;
215 reg_sc->suspend_reg = VSEL1;
216 reg_sc->volt_reg = VSEL0;
220 regnode_register(regnode);
226 syr827_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
229 struct syr827_softc *sc;
231 sc = device_get_softc(dev);
233 if (sc->reg->xref != xref)
242 syr827_probe(device_t dev)
244 if (!ofw_bus_status_okay(dev))
247 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
250 device_set_desc(dev, "Silergy SYR827 regulator");
252 return (BUS_PROBE_DEFAULT);
256 syr827_start(void *pdev)
258 struct syr827_softc *sc;
263 sc = device_get_softc(dev);
266 syr827_read(dev, ID1, &val, 1);
267 device_printf(dev, "Vendor ID: %x, DIE ID: %x\n",
268 (val & ID1_VENDOR_MASK) >> ID1_VENDOR_SHIFT,
270 syr827_read(dev, ID2, &val, 1);
271 device_printf(dev, "DIE Rev: %x\n", val & ID2_DIE_REV_MASK);
274 config_intrhook_disestablish(&sc->intr_hook);
278 syr827_attach(device_t dev)
280 struct syr827_softc *sc;
283 sc = device_get_softc(dev);
284 node = ofw_bus_get_node(dev);
286 sc->addr = iicbus_get_addr(dev);
288 sc->intr_hook.ich_func = syr827_start;
289 sc->intr_hook.ich_arg = dev;
291 if (config_intrhook_establish(&sc->intr_hook) != 0)
294 sc->reg = syr827_reg_attach(dev, node);
295 if (sc->reg == NULL) {
296 device_printf(dev, "cannot attach regulator\n");
303 static device_method_t syr827_methods[] = {
304 /* Device interface */
305 DEVMETHOD(device_probe, syr827_probe),
306 DEVMETHOD(device_attach, syr827_attach),
308 /* Regdev interface */
309 DEVMETHOD(regdev_map, syr827_regdev_map),
314 static driver_t syr827_driver = {
317 sizeof(struct syr827_softc),
320 static devclass_t syr827_devclass;
322 EARLY_DRIVER_MODULE(syr827, iicbus, syr827_driver, syr827_devclass, 0, 0,
324 MODULE_VERSION(syr827, 1);
325 MODULE_DEPEND(syr827, iicbus, 1, 1, 1);
326 IICBUS_FDT_PNP_INFO(compat_data);