2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Silergy Corp. SY8106A buck regulator
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
42 #include <sys/module.h>
44 #include <dev/iicbus/iicbus.h>
45 #include <dev/iicbus/iiconf.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/extres/regulator/regulator.h>
52 #include "iicbus_if.h"
53 #include "regdev_if.h"
55 #define VOUT1_SEL 0x01
56 #define SEL_GO (1 << 7)
57 #define SEL_VOLTAGE_MASK 0x7f
58 #define SEL_VOLTAGE_BASE 680000 /* uV */
59 #define SEL_VOLTAGE_STEP 10000 /* uV */
61 #define COM_DISABLE (1 << 0)
62 #define SYS_STATUS 0x06
64 static struct ofw_compat_data compat_data[] = {
65 { "silergy,sy8106a", 1 },
69 struct sy8106a_reg_sc {
70 struct regnode *regnode;
73 struct regnode_std_param *param;
76 struct sy8106a_softc {
80 struct sy8106a_reg_sc *reg;
84 sy8106a_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
86 struct sy8106a_softc *sc;
87 struct iic_msg msg[2];
89 sc = device_get_softc(dev);
91 msg[0].slave = sc->addr;
92 msg[0].flags = IIC_M_WR;
96 msg[1].slave = sc->addr;
97 msg[1].flags = IIC_M_RD;
101 return (iicbus_transfer(dev, msg, 2));
105 sy8106a_write(device_t dev, uint8_t reg, uint8_t val)
107 struct sy8106a_softc *sc;
111 sc = device_get_softc(dev);
116 msg.slave = sc->addr;
117 msg.flags = IIC_M_WR;
121 return (iicbus_transfer(dev, &msg, 1));
125 sy8106a_regnode_init(struct regnode *regnode)
131 sy8106a_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
133 struct sy8106a_reg_sc *sc;
136 sc = regnode_get_softc(regnode);
138 sy8106a_read(sc->base_dev, VOUT_COM, &val, 1);
143 sy8106a_write(sc->base_dev, VOUT_COM, val);
145 *udelay = sc->param->ramp_delay;
151 sy8106a_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
152 int max_uvolt, int *udelay)
154 struct sy8106a_reg_sc *sc;
158 sc = regnode_get_softc(regnode);
160 /* Get current voltage */
161 sy8106a_read(sc->base_dev, VOUT1_SEL, &oval, 1);
162 cur_uvolt = (oval & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
165 /* Set new voltage */
166 val = SEL_GO | ((min_uvolt - SEL_VOLTAGE_BASE) / SEL_VOLTAGE_STEP);
167 sy8106a_write(sc->base_dev, VOUT1_SEL, val);
169 /* Time to delay is based on the number of voltage steps */
170 *udelay = sc->param->ramp_delay *
171 (abs(cur_uvolt - min_uvolt) / SEL_VOLTAGE_STEP);
177 sy8106a_regnode_get_voltage(struct regnode *regnode, int *uvolt)
179 struct sy8106a_reg_sc *sc;
182 sc = regnode_get_softc(regnode);
184 sy8106a_read(sc->base_dev, VOUT1_SEL, &val, 1);
185 *uvolt = (val & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
191 static regnode_method_t sy8106a_regnode_methods[] = {
192 /* Regulator interface */
193 REGNODEMETHOD(regnode_init, sy8106a_regnode_init),
194 REGNODEMETHOD(regnode_enable, sy8106a_regnode_enable),
195 REGNODEMETHOD(regnode_set_voltage, sy8106a_regnode_set_voltage),
196 REGNODEMETHOD(regnode_get_voltage, sy8106a_regnode_get_voltage),
199 DEFINE_CLASS_1(sy8106a_regnode, sy8106a_regnode_class, sy8106a_regnode_methods,
200 sizeof(struct sy8106a_reg_sc), regnode_class);
202 static struct sy8106a_reg_sc *
203 sy8106a_reg_attach(device_t dev, phandle_t node)
205 struct sy8106a_reg_sc *reg_sc;
206 struct regnode_init_def initdef;
207 struct regnode *regnode;
209 memset(&initdef, 0, sizeof(initdef));
210 regulator_parse_ofw_stdparam(dev, node, &initdef);
212 initdef.ofw_node = node;
213 regnode = regnode_create(dev, &sy8106a_regnode_class, &initdef);
214 if (regnode == NULL) {
215 device_printf(dev, "cannot create regulator\n");
219 reg_sc = regnode_get_softc(regnode);
220 reg_sc->regnode = regnode;
221 reg_sc->base_dev = dev;
222 reg_sc->xref = OF_xref_from_node(node);
223 reg_sc->param = regnode_get_stdparam(regnode);
225 regnode_register(regnode);
231 sy8106a_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
234 struct sy8106a_softc *sc;
236 sc = device_get_softc(dev);
238 if (sc->reg->xref != xref)
247 sy8106a_probe(device_t dev)
249 if (!ofw_bus_status_okay(dev))
252 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
255 device_set_desc(dev, "Silergy SY8106A regulator");
257 return (BUS_PROBE_DEFAULT);
261 sy8106a_attach(device_t dev)
263 struct sy8106a_softc *sc;
266 sc = device_get_softc(dev);
267 node = ofw_bus_get_node(dev);
269 sc->addr = iicbus_get_addr(dev);
271 sc->reg = sy8106a_reg_attach(dev, node);
272 if (sc->reg == NULL) {
273 device_printf(dev, "cannot attach regulator\n");
280 static device_method_t sy8106a_methods[] = {
281 /* Device interface */
282 DEVMETHOD(device_probe, sy8106a_probe),
283 DEVMETHOD(device_attach, sy8106a_attach),
285 /* Regdev interface */
286 DEVMETHOD(regdev_map, sy8106a_regdev_map),
291 static driver_t sy8106a_driver = {
294 sizeof(struct sy8106a_softc),
297 static devclass_t sy8106a_devclass;
299 EARLY_DRIVER_MODULE(sy8106a, iicbus, sy8106a_driver, sy8106a_devclass, 0, 0,
301 MODULE_VERSION(sy8106a, 1);
302 MODULE_DEPEND(sy8106a, iicbus, 1, 1, 1);