2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Silergy Corp. SY8106A buck regulator
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/reboot.h>
41 #include <sys/module.h>
43 #include <dev/iicbus/iicbus.h>
44 #include <dev/iicbus/iiconf.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/extres/regulator/regulator.h>
51 #include "iicbus_if.h"
52 #include "regdev_if.h"
54 #define VOUT1_SEL 0x01
55 #define SEL_GO (1 << 7)
56 #define SEL_VOLTAGE_MASK 0x7f
57 #define SEL_VOLTAGE_BASE 680000 /* uV */
58 #define SEL_VOLTAGE_STEP 10000 /* uV */
60 #define COM_DISABLE (1 << 0)
61 #define SYS_STATUS 0x06
63 static struct ofw_compat_data compat_data[] = {
64 { "silergy,sy8106a", 1 },
68 struct sy8106a_reg_sc {
69 struct regnode *regnode;
72 struct regnode_std_param *param;
75 struct sy8106a_softc {
79 struct sy8106a_reg_sc *reg;
83 sy8106a_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
85 struct sy8106a_softc *sc;
86 struct iic_msg msg[2];
88 sc = device_get_softc(dev);
90 msg[0].slave = sc->addr;
91 msg[0].flags = IIC_M_WR;
95 msg[1].slave = sc->addr;
96 msg[1].flags = IIC_M_RD;
100 return (iicbus_transfer(dev, msg, 2));
104 sy8106a_write(device_t dev, uint8_t reg, uint8_t val)
106 struct sy8106a_softc *sc;
110 sc = device_get_softc(dev);
115 msg.slave = sc->addr;
116 msg.flags = IIC_M_WR;
120 return (iicbus_transfer(dev, &msg, 1));
124 sy8106a_regnode_init(struct regnode *regnode)
130 sy8106a_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
132 struct sy8106a_reg_sc *sc;
135 sc = regnode_get_softc(regnode);
137 sy8106a_read(sc->base_dev, VOUT_COM, &val, 1);
142 sy8106a_write(sc->base_dev, VOUT_COM, val);
144 *udelay = sc->param->ramp_delay;
150 sy8106a_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
151 int max_uvolt, int *udelay)
153 struct sy8106a_reg_sc *sc;
157 sc = regnode_get_softc(regnode);
159 /* Get current voltage */
160 sy8106a_read(sc->base_dev, VOUT1_SEL, &oval, 1);
161 cur_uvolt = (oval & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
164 /* Set new voltage */
165 val = SEL_GO | ((min_uvolt - SEL_VOLTAGE_BASE) / SEL_VOLTAGE_STEP);
166 sy8106a_write(sc->base_dev, VOUT1_SEL, val);
168 /* Time to delay is based on the number of voltage steps */
169 *udelay = sc->param->ramp_delay *
170 (abs(cur_uvolt - min_uvolt) / SEL_VOLTAGE_STEP);
176 sy8106a_regnode_get_voltage(struct regnode *regnode, int *uvolt)
178 struct sy8106a_reg_sc *sc;
181 sc = regnode_get_softc(regnode);
183 sy8106a_read(sc->base_dev, VOUT1_SEL, &val, 1);
184 *uvolt = (val & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
190 static regnode_method_t sy8106a_regnode_methods[] = {
191 /* Regulator interface */
192 REGNODEMETHOD(regnode_init, sy8106a_regnode_init),
193 REGNODEMETHOD(regnode_enable, sy8106a_regnode_enable),
194 REGNODEMETHOD(regnode_set_voltage, sy8106a_regnode_set_voltage),
195 REGNODEMETHOD(regnode_get_voltage, sy8106a_regnode_get_voltage),
198 DEFINE_CLASS_1(sy8106a_regnode, sy8106a_regnode_class, sy8106a_regnode_methods,
199 sizeof(struct sy8106a_reg_sc), regnode_class);
201 static struct sy8106a_reg_sc *
202 sy8106a_reg_attach(device_t dev, phandle_t node)
204 struct sy8106a_reg_sc *reg_sc;
205 struct regnode_init_def initdef;
206 struct regnode *regnode;
208 memset(&initdef, 0, sizeof(initdef));
209 regulator_parse_ofw_stdparam(dev, node, &initdef);
211 initdef.ofw_node = node;
212 regnode = regnode_create(dev, &sy8106a_regnode_class, &initdef);
213 if (regnode == NULL) {
214 device_printf(dev, "cannot create regulator\n");
218 reg_sc = regnode_get_softc(regnode);
219 reg_sc->regnode = regnode;
220 reg_sc->base_dev = dev;
221 reg_sc->xref = OF_xref_from_node(node);
222 reg_sc->param = regnode_get_stdparam(regnode);
224 regnode_register(regnode);
230 sy8106a_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
233 struct sy8106a_softc *sc;
235 sc = device_get_softc(dev);
237 if (sc->reg->xref != xref)
246 sy8106a_probe(device_t dev)
248 if (!ofw_bus_status_okay(dev))
251 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
254 device_set_desc(dev, "Silergy SY8106A regulator");
256 return (BUS_PROBE_DEFAULT);
260 sy8106a_attach(device_t dev)
262 struct sy8106a_softc *sc;
265 sc = device_get_softc(dev);
266 node = ofw_bus_get_node(dev);
268 sc->addr = iicbus_get_addr(dev);
270 sc->reg = sy8106a_reg_attach(dev, node);
271 if (sc->reg == NULL) {
272 device_printf(dev, "cannot attach regulator\n");
279 static device_method_t sy8106a_methods[] = {
280 /* Device interface */
281 DEVMETHOD(device_probe, sy8106a_probe),
282 DEVMETHOD(device_attach, sy8106a_attach),
284 /* Regdev interface */
285 DEVMETHOD(regdev_map, sy8106a_regdev_map),
290 static driver_t sy8106a_driver = {
293 sizeof(struct sy8106a_softc),
296 static devclass_t sy8106a_devclass;
298 EARLY_DRIVER_MODULE(sy8106a, iicbus, sy8106a_driver, sy8106a_devclass, 0, 0,
300 MODULE_VERSION(sy8106a, 1);
301 MODULE_DEPEND(sy8106a, iicbus, 1, 1, 1);
302 IICBUS_FDT_PNP_INFO(compat_data);