]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/sy8106a.c
Add liblutok a lightweight C++ API for lua.
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / sy8106a.c
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
63 static struct ofw_compat_data compat_data[] = {
64         { "silergy,sy8106a",                    1 },
65         { NULL,                                 0 }
66 };
67
68 struct sy8106a_reg_sc {
69         struct regnode          *regnode;
70         device_t                base_dev;
71         phandle_t               xref;
72         struct regnode_std_param *param;
73 };
74
75 struct sy8106a_softc {
76         uint16_t                addr;
77
78         /* Regulator */
79         struct sy8106a_reg_sc   *reg;
80 };
81
82 static int
83 sy8106a_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
103 static int
104 sy8106a_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
123 static int
124 sy8106a_regnode_init(struct regnode *regnode)
125 {
126         return (0);
127 }
128
129 static int
130 sy8106a_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
137         sy8106a_read(sc->base_dev, VOUT_COM, &val, 1);
138         if (enable)
139                 val &= ~COM_DISABLE;
140         else
141                 val |= COM_DISABLE;
142         sy8106a_write(sc->base_dev, VOUT_COM, val);
143
144         *udelay = sc->param->ramp_delay;
145
146         return (0);
147 }
148
149 static int
150 sy8106a_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
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 +
162             SEL_VOLTAGE_BASE;
163
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);
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
175 static int
176 sy8106a_regnode_get_voltage(struct regnode *regnode, int *uvolt)
177 {
178         struct sy8106a_reg_sc *sc;
179         uint8_t val;
180
181         sc = regnode_get_softc(regnode);
182
183         sy8106a_read(sc->base_dev, VOUT1_SEL, &val, 1);
184         *uvolt = (val & SEL_VOLTAGE_MASK) * SEL_VOLTAGE_STEP +
185             SEL_VOLTAGE_BASE;
186
187         return (0);
188 }
189
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),
196         REGNODEMETHOD_END
197 };
198 DEFINE_CLASS_1(sy8106a_regnode, sy8106a_regnode_class, sy8106a_regnode_methods,
199     sizeof(struct sy8106a_reg_sc), regnode_class);
200
201 static struct sy8106a_reg_sc *
202 sy8106a_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
229 static int
230 sy8106a_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
245 static int
246 sy8106a_probe(device_t dev)
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
259 static int
260 sy8106a_attach(device_t dev)
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
279 static 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
290 static driver_t sy8106a_driver = {
291         "sy8106a",
292         sy8106a_methods,
293         sizeof(struct sy8106a_softc),
294 };
295
296 static devclass_t sy8106a_devclass;
297
298 EARLY_DRIVER_MODULE(sy8106a, iicbus, sy8106a_driver, sy8106a_devclass, 0, 0,
299     BUS_PASS_RESOURCE);
300 MODULE_VERSION(sy8106a, 1);
301 MODULE_DEPEND(sy8106a, iicbus, 1, 1, 1);
302 IICBUS_FDT_PNP_INFO(compat_data);