FreeBSD kernel IICBUS device code
sy8106a.c
Go to the documentation of this file.
1/*-
2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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
23 * SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28/*
29 * Silergy Corp. SY8106A buck regulator
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/bus.h>
38#include <sys/rman.h>
39#include <sys/kernel.h>
40#include <sys/reboot.h>
41#include <sys/module.h>
42
43#include <dev/iicbus/iicbus.h>
44#include <dev/iicbus/iiconf.h>
45
46#include <dev/ofw/ofw_bus.h>
47#include <dev/ofw/ofw_bus_subr.h>
48
49#include <dev/extres/regulator/regulator.h>
50
51#include "iicbus_if.h"
52#include "regdev_if.h"
53
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 */
59#define VOUT_COM 0x02
60#define COM_DISABLE (1 << 0)
61#define SYS_STATUS 0x06
62
63static struct ofw_compat_data compat_data[] = {
64 { "silergy,sy8106a", 1 },
65 { NULL, 0 }
66};
67
70 device_t base_dev;
71 phandle_t xref;
72 struct regnode_std_param *param;
73};
74
76 uint16_t addr;
77
78 /* Regulator */
80};
81
82static int
83sy8106a_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
84{
85 struct sy8106a_softc *sc;
86 struct iic_msg msg[2];
87
88 sc = device_get_softc(dev);
89
90 msg[0].slave = sc->addr;
91 msg[0].flags = IIC_M_WR;
92 msg[0].len = 1;
93 msg[0].buf = &reg;
94
95 msg[1].slave = sc->addr;
96 msg[1].flags = IIC_M_RD;
97 msg[1].len = size;
98 msg[1].buf = data;
99
100 return (iicbus_transfer(dev, msg, 2));
101}
102
103static int
104sy8106a_write(device_t dev, uint8_t reg, uint8_t val)
105{
106 struct sy8106a_softc *sc;
107 struct iic_msg msg;
108 uint8_t buffer[2];
109
110 sc = device_get_softc(dev);
111
112 buffer[0] = reg;
113 buffer[1] = val;
114
115 msg.slave = sc->addr;
116 msg.flags = IIC_M_WR;
117 msg.len = 2;
118 msg.buf = buffer;
119
120 return (iicbus_transfer(dev, &msg, 1));
121}
122
123static int
124sy8106a_regnode_init(struct regnode *regnode)
125{
126 return (0);
127}
128
129static int
130sy8106a_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
131{
132 struct sy8106a_reg_sc *sc;
133 uint8_t val;
134
135 sc = regnode_get_softc(regnode);
136
138 if (enable)
139 val &= ~COM_DISABLE;
140 else
141 val |= COM_DISABLE;
143
144 *udelay = sc->param->ramp_delay;
145
146 return (0);
147}
148
149static int
151 int max_uvolt, int *udelay)
152{
153 struct sy8106a_reg_sc *sc;
154 int cur_uvolt;
155 uint8_t val, oval;
156
157 sc = regnode_get_softc(regnode);
158
159 /* Get current voltage */
160 sy8106a_read(sc->base_dev, VOUT1_SEL, &oval, 1);
161 cur_uvolt = (oval & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
163
164 /* Set new voltage */
165 val = SEL_GO | ((min_uvolt - SEL_VOLTAGE_BASE) / SEL_VOLTAGE_STEP);
167
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);
171
172 return (0);
173}
174
175static int
177{
178 struct sy8106a_reg_sc *sc;
179 uint8_t val;
180
181 sc = regnode_get_softc(regnode);
182
184 *uvolt = (val & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
186
187 return (0);
188}
189
190static 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),
196 REGNODEMETHOD_END
197};
198DEFINE_CLASS_1(sy8106a_regnode, sy8106a_regnode_class, sy8106a_regnode_methods,
199 sizeof(struct sy8106a_reg_sc), regnode_class);
200
201static struct sy8106a_reg_sc *
202sy8106a_reg_attach(device_t dev, phandle_t node)
203{
204 struct sy8106a_reg_sc *reg_sc;
205 struct regnode_init_def initdef;
206 struct regnode *regnode;
207
208 memset(&initdef, 0, sizeof(initdef));
209 regulator_parse_ofw_stdparam(dev, node, &initdef);
210 initdef.id = 0;
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");
215 return (NULL);
216 }
217
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);
223
224 regnode_register(regnode);
225
226 return (reg_sc);
227}
228
229static int
230sy8106a_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
231 intptr_t *num)
232{
233 struct sy8106a_softc *sc;
234
235 sc = device_get_softc(dev);
236
237 if (sc->reg->xref != xref)
238 return (ENXIO);
239
240 *num = 0;
241
242 return (0);
243}
244
245static int
247{
248 if (!ofw_bus_status_okay(dev))
249 return (ENXIO);
250
251 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
252 return (ENXIO);
253
254 device_set_desc(dev, "Silergy SY8106A regulator");
255
256 return (BUS_PROBE_DEFAULT);
257}
258
259static int
261{
262 struct sy8106a_softc *sc;
263 phandle_t node;
264
265 sc = device_get_softc(dev);
266 node = ofw_bus_get_node(dev);
267
268 sc->addr = iicbus_get_addr(dev);
269
270 sc->reg = sy8106a_reg_attach(dev, node);
271 if (sc->reg == NULL) {
272 device_printf(dev, "cannot attach regulator\n");
273 return (ENXIO);
274 }
275
276 return (0);
277}
278
279static device_method_t sy8106a_methods[] = {
280 /* Device interface */
281 DEVMETHOD(device_probe, sy8106a_probe),
282 DEVMETHOD(device_attach, sy8106a_attach),
283
284 /* Regdev interface */
285 DEVMETHOD(regdev_map, sy8106a_regdev_map),
286
287 DEVMETHOD_END
288};
289
290static driver_t sy8106a_driver = {
291 "sy8106a",
293 sizeof(struct sy8106a_softc),
294};
295
296static devclass_t sy8106a_devclass;
297
299 BUS_PASS_RESOURCE);
300MODULE_VERSION(sy8106a, 1);
301MODULE_DEPEND(sy8106a, iicbus, 1, 1, 1);
#define IIC_M_WR
Definition: ad7418.c:45
#define IIC_M_RD
Definition: iic.h:42
caddr_t data
Definition: iicbb_if.m:61
int val
Definition: iicbb_if.m:83
INTERFACE iicbus
Definition: iicbus_if.m:32
int iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
Definition: iiconf.c:442
device_t dev
Definition: ofw_iicbus_if.m:38
Definition: iic.h:38
uint16_t slave
Definition: iic.h:39
uint16_t len
Definition: iic.h:45
uint8_t * buf
Definition: iic.h:46
uint16_t flags
Definition: iic.h:40
phandle_t xref
Definition: sy8106a.c:71
struct regnode_std_param * param
Definition: sy8106a.c:72
struct regnode * regnode
Definition: sy8106a.c:69
device_t base_dev
Definition: sy8106a.c:70
uint16_t addr
Definition: sy8106a.c:76
struct sy8106a_reg_sc * reg
Definition: sy8106a.c:79
static int sy8106a_probe(device_t dev)
Definition: sy8106a.c:246
#define VOUT1_SEL
Definition: sy8106a.c:54
DEFINE_CLASS_1(sy8106a_regnode, sy8106a_regnode_class, sy8106a_regnode_methods, sizeof(struct sy8106a_reg_sc), regnode_class)
static int sy8106a_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells, intptr_t *num)
Definition: sy8106a.c:230
#define COM_DISABLE
Definition: sy8106a.c:60
static int sy8106a_write(device_t dev, uint8_t reg, uint8_t val)
Definition: sy8106a.c:104
#define SEL_GO
Definition: sy8106a.c:55
static regnode_method_t sy8106a_regnode_methods[]
Definition: sy8106a.c:190
static int sy8106a_regnode_init(struct regnode *regnode)
Definition: sy8106a.c:124
#define SEL_VOLTAGE_MASK
Definition: sy8106a.c:56
MODULE_VERSION(sy8106a, 1)
static device_method_t sy8106a_methods[]
Definition: sy8106a.c:279
static int sy8106a_attach(device_t dev)
Definition: sy8106a.c:260
static struct sy8106a_reg_sc * sy8106a_reg_attach(device_t dev, phandle_t node)
Definition: sy8106a.c:202
__FBSDID("$FreeBSD$")
#define VOUT_COM
Definition: sy8106a.c:59
EARLY_DRIVER_MODULE(sy8106a, iicbus, sy8106a_driver, sy8106a_devclass, 0, 0, BUS_PASS_RESOURCE)
IICBUS_FDT_PNP_INFO(compat_data)
static struct ofw_compat_data compat_data[]
Definition: sy8106a.c:63
static int sy8106a_regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt, int *udelay)
Definition: sy8106a.c:150
static int sy8106a_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
Definition: sy8106a.c:130
static driver_t sy8106a_driver
Definition: sy8106a.c:290
#define SEL_VOLTAGE_BASE
Definition: sy8106a.c:57
MODULE_DEPEND(sy8106a, iicbus, 1, 1, 1)
static int sy8106a_regnode_get_voltage(struct regnode *regnode, int *uvolt)
Definition: sy8106a.c:176
static devclass_t sy8106a_devclass
Definition: sy8106a.c:296
#define SEL_VOLTAGE_STEP
Definition: sy8106a.c:58
static int sy8106a_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
Definition: sy8106a.c:83