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 struct syr827_softc *sc;
94 struct iic_msg msg[2];
96 sc = device_get_softc(dev);
98 msg[0].slave = sc->addr;
99 msg[0].flags = IIC_M_WR;
103 msg[1].slave = sc->addr;
104 msg[1].flags = IIC_M_RD;
108 return (iicbus_transfer(dev, msg, 2));
112 syr827_write(device_t dev, uint8_t reg, uint8_t val)
114 struct syr827_softc *sc;
118 sc = device_get_softc(dev);
123 msg.slave = sc->addr;
124 msg.flags = IIC_M_WR;
128 return (iicbus_transfer(dev, &msg, 1));
132 syr827_regnode_init(struct regnode *regnode)
138 syr827_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
140 struct syr827_reg_sc *sc;
143 sc = regnode_get_softc(regnode);
145 syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
147 val &= ~VSEL_BUCK_EN;
150 syr827_write(sc->base_dev, sc->volt_reg, val);
152 *udelay = sc->param->ramp_delay;
158 syr827_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
159 int max_uvolt, int *udelay)
161 struct syr827_reg_sc *sc;
165 sc = regnode_get_softc(regnode);
167 /* Get current voltage */
168 syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
169 cur_uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
172 /* Set new voltage */
173 val &= ~VSEL_NSEL_MASK;
174 val |= ((min_uvolt - VSEL_VOLTAGE_BASE) / VSEL_VOLTAGE_STEP);
175 syr827_write(sc->base_dev, sc->volt_reg, val);
177 /* Time to delay is based on the number of voltage steps */
178 *udelay = sc->param->ramp_delay *
179 (abs(cur_uvolt - min_uvolt) / VSEL_VOLTAGE_STEP);
185 syr827_regnode_get_voltage(struct regnode *regnode, int *uvolt)
187 struct syr827_reg_sc *sc;
190 sc = regnode_get_softc(regnode);
192 syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
193 *uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
199 static regnode_method_t syr827_regnode_methods[] = {
200 /* Regulator interface */
201 REGNODEMETHOD(regnode_init, syr827_regnode_init),
202 REGNODEMETHOD(regnode_enable, syr827_regnode_enable),
203 REGNODEMETHOD(regnode_set_voltage, syr827_regnode_set_voltage),
204 REGNODEMETHOD(regnode_get_voltage, syr827_regnode_get_voltage),
207 DEFINE_CLASS_1(syr827_regnode, syr827_regnode_class, syr827_regnode_methods,
208 sizeof(struct syr827_reg_sc), regnode_class);
210 static struct syr827_reg_sc *
211 syr827_reg_attach(device_t dev, phandle_t node)
213 struct syr827_reg_sc *reg_sc;
214 struct regnode_init_def initdef;
215 struct regnode *regnode;
218 memset(&initdef, 0, sizeof(initdef));
219 regulator_parse_ofw_stdparam(dev, node, &initdef);
221 initdef.ofw_node = node;
222 regnode = regnode_create(dev, &syr827_regnode_class, &initdef);
223 if (regnode == NULL) {
224 device_printf(dev, "cannot create regulator\n");
228 reg_sc = regnode_get_softc(regnode);
229 reg_sc->regnode = regnode;
230 reg_sc->base_dev = dev;
231 reg_sc->xref = OF_xref_from_node(node);
232 reg_sc->param = regnode_get_stdparam(regnode);
234 if (OF_getencprop(node, "fcs,suspend-voltage-selector", &suspend_reg,
235 sizeof(uint32_t)) <= 0)
238 switch (suspend_reg) {
240 reg_sc->suspend_reg = VSEL0;
241 reg_sc->volt_reg = VSEL1;
244 reg_sc->suspend_reg = VSEL1;
245 reg_sc->volt_reg = VSEL0;
249 regnode_register(regnode);
255 syr827_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
258 struct syr827_softc *sc;
260 sc = device_get_softc(dev);
262 if (sc->reg->xref != xref)
271 syr827_probe(device_t dev)
273 if (!ofw_bus_status_okay(dev))
276 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
279 device_set_desc(dev, "Silergy SYR827 regulator");
281 return (BUS_PROBE_DEFAULT);
285 syr827_start(void *pdev)
287 struct syr827_softc *sc;
292 sc = device_get_softc(dev);
295 syr827_read(dev, ID1, &val, 1);
296 device_printf(dev, "Vendor ID: %x, DIE ID: %x\n",
297 (val & ID1_VENDOR_MASK) >> ID1_VENDOR_SHIFT,
299 syr827_read(dev, ID2, &val, 1);
300 device_printf(dev, "DIE Rev: %x\n", val & ID2_DIE_REV_MASK);
303 config_intrhook_disestablish(&sc->intr_hook);
307 syr827_attach(device_t dev)
309 struct syr827_softc *sc;
312 sc = device_get_softc(dev);
313 node = ofw_bus_get_node(dev);
315 sc->addr = iicbus_get_addr(dev);
317 sc->intr_hook.ich_func = syr827_start;
318 sc->intr_hook.ich_arg = dev;
320 if (config_intrhook_establish(&sc->intr_hook) != 0)
323 sc->reg = syr827_reg_attach(dev, node);
324 if (sc->reg == NULL) {
325 device_printf(dev, "cannot attach regulator\n");
332 static device_method_t syr827_methods[] = {
333 /* Device interface */
334 DEVMETHOD(device_probe, syr827_probe),
335 DEVMETHOD(device_attach, syr827_attach),
337 /* Regdev interface */
338 DEVMETHOD(regdev_map, syr827_regdev_map),
343 static driver_t syr827_driver = {
346 sizeof(struct syr827_softc),
349 static devclass_t syr827_devclass;
351 EARLY_DRIVER_MODULE(syr827, iicbus, syr827_driver, syr827_devclass, 0, 0,
353 MODULE_VERSION(syr827, 1);
354 MODULE_DEPEND(syr827, iicbus, 1, 1, 1);