]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/syr827.c
Merge LLVM libunwind trunk r351319, from just before upstream's
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / syr827.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/kernel.h>
37 #include <sys/reboot.h>
38 #include <sys/module.h>
39
40 #include <dev/iicbus/iicbus.h>
41 #include <dev/iicbus/iiconf.h>
42
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45
46 #include <dev/extres/regulator/regulator.h>
47
48 #include "iicbus_if.h"
49 #include "regdev_if.h"
50
51 #define VSEL0   0x00
52 #define VSEL1   0x01
53
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 */
58
59 #define ID1     0x03
60 #define  ID1_VENDOR_MASK        0xE0
61 #define  ID1_VENDOR_SHIFT       5
62 #define  ID1_DIE_MASK           0xF
63
64 #define ID2     0x4
65 #define  ID2_DIE_REV_MASK       0xF
66
67 static struct ofw_compat_data compat_data[] = {
68         { "silergy,syr827",                     1 },
69         { NULL,                                 0 }
70 };
71
72 struct syr827_reg_sc {
73         struct regnode          *regnode;
74         device_t                base_dev;
75         phandle_t               xref;
76         struct regnode_std_param *param;
77
78         int                     volt_reg;
79         int                     suspend_reg;
80 };
81
82 struct syr827_softc {
83         uint16_t                addr;
84         struct intr_config_hook intr_hook;
85
86         /* Regulator */
87         struct syr827_reg_sc    *reg;
88 };
89
90 static int
91 syr827_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
92 {
93         struct syr827_softc *sc;
94         struct iic_msg msg[2];
95
96         sc = device_get_softc(dev);
97
98         msg[0].slave = sc->addr;
99         msg[0].flags = IIC_M_WR;
100         msg[0].len = 1;
101         msg[0].buf = &reg;
102
103         msg[1].slave = sc->addr;
104         msg[1].flags = IIC_M_RD;
105         msg[1].len = size;
106         msg[1].buf = data;
107
108         return (iicbus_transfer(dev, msg, 2));
109 }
110
111 static int
112 syr827_write(device_t dev, uint8_t reg, uint8_t val)
113 {
114         struct syr827_softc *sc;
115         struct iic_msg msg;
116         uint8_t buffer[2];
117
118         sc = device_get_softc(dev);
119
120         buffer[0] = reg;
121         buffer[1] = val;
122
123         msg.slave = sc->addr;
124         msg.flags = IIC_M_WR;
125         msg.len = 2;
126         msg.buf = buffer;
127
128         return (iicbus_transfer(dev, &msg, 1));
129 }
130
131 static int
132 syr827_regnode_init(struct regnode *regnode)
133 {
134         return (0);
135 }
136
137 static int
138 syr827_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
139 {
140         struct syr827_reg_sc *sc;
141         uint8_t val;
142
143         sc = regnode_get_softc(regnode);
144
145         syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
146         if (enable)
147                 val &= ~VSEL_BUCK_EN;
148         else
149                 val |= VSEL_BUCK_EN;
150         syr827_write(sc->base_dev, sc->volt_reg, val);
151
152         *udelay = sc->param->ramp_delay;
153
154         return (0);
155 }
156
157 static int
158 syr827_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
159     int max_uvolt, int *udelay)
160 {
161         struct syr827_reg_sc *sc;
162         int cur_uvolt;
163         uint8_t val;
164
165         sc = regnode_get_softc(regnode);
166
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 +
170             VSEL_VOLTAGE_BASE;
171
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);
176
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);
180
181         return (0);
182 }
183
184 static int
185 syr827_regnode_get_voltage(struct regnode *regnode, int *uvolt)
186 {
187         struct syr827_reg_sc *sc;
188         uint8_t val;
189
190         sc = regnode_get_softc(regnode);
191
192         syr827_read(sc->base_dev, sc->volt_reg, &val, 1);
193         *uvolt = (val & VSEL_NSEL_MASK) * VSEL_VOLTAGE_STEP +
194             VSEL_VOLTAGE_BASE;
195
196         return (0);
197 }
198
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),
205         REGNODEMETHOD_END
206 };
207 DEFINE_CLASS_1(syr827_regnode, syr827_regnode_class, syr827_regnode_methods,
208     sizeof(struct syr827_reg_sc), regnode_class);
209
210 static struct syr827_reg_sc *
211 syr827_reg_attach(device_t dev, phandle_t node)
212 {
213         struct syr827_reg_sc *reg_sc;
214         struct regnode_init_def initdef;
215         struct regnode *regnode;
216         int suspend_reg;
217
218         memset(&initdef, 0, sizeof(initdef));
219         regulator_parse_ofw_stdparam(dev, node, &initdef);
220         initdef.id = 0;
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");
225                 return (NULL);
226         }
227
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);
233
234         if (OF_getencprop(node, "fcs,suspend-voltage-selector", &suspend_reg,
235             sizeof(uint32_t)) <= 0)
236                 suspend_reg = 0;
237
238         switch (suspend_reg) {
239         case 0:
240                 reg_sc->suspend_reg = VSEL0;
241                 reg_sc->volt_reg = VSEL1;
242                 break;
243         case 1:
244                 reg_sc->suspend_reg = VSEL1;
245                 reg_sc->volt_reg = VSEL0;
246                 break;
247         }
248
249         regnode_register(regnode);
250
251         return (reg_sc);
252 }
253
254 static int
255 syr827_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
256     intptr_t *num)
257 {
258         struct syr827_softc *sc;
259
260         sc = device_get_softc(dev);
261
262         if (sc->reg->xref != xref)
263                 return (ENXIO);
264
265         *num = 0;
266
267         return (0);
268 }
269
270 static int
271 syr827_probe(device_t dev)
272 {
273         if (!ofw_bus_status_okay(dev))
274                 return (ENXIO);
275
276         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
277                 return (ENXIO);
278
279         device_set_desc(dev, "Silergy SYR827 regulator");
280
281         return (BUS_PROBE_DEFAULT);
282 }
283
284 static void
285 syr827_start(void *pdev)
286 {
287         struct syr827_softc *sc;
288         device_t dev;
289         uint8_t val;
290
291         dev = pdev;
292         sc = device_get_softc(dev);
293
294         if (bootverbose) {
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,
298                     val & ID1_DIE_MASK);
299                 syr827_read(dev, ID2, &val, 1);
300                 device_printf(dev, "DIE Rev: %x\n", val & ID2_DIE_REV_MASK);
301         }
302
303         config_intrhook_disestablish(&sc->intr_hook);
304 }
305
306 static int
307 syr827_attach(device_t dev)
308 {
309         struct syr827_softc *sc;
310         phandle_t node;
311
312         sc = device_get_softc(dev);
313         node = ofw_bus_get_node(dev);
314
315         sc->addr = iicbus_get_addr(dev);
316
317         sc->intr_hook.ich_func = syr827_start;
318         sc->intr_hook.ich_arg = dev;
319
320         if (config_intrhook_establish(&sc->intr_hook) != 0)
321                 return (ENOMEM);
322
323         sc->reg = syr827_reg_attach(dev, node);
324         if (sc->reg == NULL) {
325                 device_printf(dev, "cannot attach regulator\n");
326                 return (ENXIO);
327         }
328
329         return (0);
330 }
331
332 static device_method_t syr827_methods[] = {
333         /* Device interface */
334         DEVMETHOD(device_probe,         syr827_probe),
335         DEVMETHOD(device_attach,        syr827_attach),
336
337         /* Regdev interface */
338         DEVMETHOD(regdev_map,           syr827_regdev_map),
339
340         DEVMETHOD_END
341 };
342
343 static driver_t syr827_driver = {
344         "syr827",
345         syr827_methods,
346         sizeof(struct syr827_softc),
347 };
348
349 static devclass_t syr827_devclass;
350
351 EARLY_DRIVER_MODULE(syr827, iicbus, syr827_driver, syr827_devclass, 0, 0,
352     BUS_PASS_RESOURCE);
353 MODULE_VERSION(syr827, 1);
354 MODULE_DEPEND(syr827, iicbus, 1, 1, 1);