sy8106a.c revision 1.1.2.2 1 1.1.2.2 jdolecek /* $NetBSD: sy8106a.c,v 1.1.2.2 2017/12/03 11:37:02 jdolecek Exp $ */
2 1.1.2.2 jdolecek
3 1.1.2.2 jdolecek /*-
4 1.1.2.2 jdolecek * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 jdolecek * All rights reserved.
6 1.1.2.2 jdolecek *
7 1.1.2.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 jdolecek * modification, are permitted provided that the following conditions
9 1.1.2.2 jdolecek * are met:
10 1.1.2.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1.2.2 jdolecek *
16 1.1.2.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 jdolecek */
28 1.1.2.2 jdolecek
29 1.1.2.2 jdolecek #include <sys/cdefs.h>
30 1.1.2.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: sy8106a.c,v 1.1.2.2 2017/12/03 11:37:02 jdolecek Exp $");
31 1.1.2.2 jdolecek
32 1.1.2.2 jdolecek #include <sys/param.h>
33 1.1.2.2 jdolecek #include <sys/systm.h>
34 1.1.2.2 jdolecek #include <sys/kernel.h>
35 1.1.2.2 jdolecek #include <sys/device.h>
36 1.1.2.2 jdolecek #include <sys/conf.h>
37 1.1.2.2 jdolecek #include <sys/bus.h>
38 1.1.2.2 jdolecek #include <sys/kmem.h>
39 1.1.2.2 jdolecek
40 1.1.2.2 jdolecek #include <dev/i2c/i2cvar.h>
41 1.1.2.2 jdolecek
42 1.1.2.2 jdolecek #include <dev/fdt/fdtvar.h>
43 1.1.2.2 jdolecek
44 1.1.2.2 jdolecek #define VOUT1_SEL 0x01
45 1.1.2.2 jdolecek #define SEL_GO __BIT(7)
46 1.1.2.2 jdolecek #define SEL_VOLTAGE __BITS(6,0)
47 1.1.2.2 jdolecek #define SEL_VOLTAGE_BASE 680000 /* uV */
48 1.1.2.2 jdolecek #define SEL_VOLTAGE_STEP 10000 /* uV */
49 1.1.2.2 jdolecek #define VOUT_COM 0x02
50 1.1.2.2 jdolecek #define COM_DISABLE __BIT(0)
51 1.1.2.2 jdolecek #define SYS_STATUS 0x06
52 1.1.2.2 jdolecek
53 1.1.2.2 jdolecek struct sy8106a_softc {
54 1.1.2.2 jdolecek device_t sc_dev;
55 1.1.2.2 jdolecek i2c_tag_t sc_i2c;
56 1.1.2.2 jdolecek i2c_addr_t sc_addr;
57 1.1.2.2 jdolecek int sc_phandle;
58 1.1.2.2 jdolecek
59 1.1.2.2 jdolecek u_int sc_ramp_delay;
60 1.1.2.2 jdolecek };
61 1.1.2.2 jdolecek
62 1.1.2.2 jdolecek static const char * compatible[] = {
63 1.1.2.2 jdolecek "silergy,sy8106a",
64 1.1.2.2 jdolecek NULL
65 1.1.2.2 jdolecek };
66 1.1.2.2 jdolecek
67 1.1.2.2 jdolecek static uint8_t
68 1.1.2.2 jdolecek sy8106a_read(struct sy8106a_softc *sc, uint8_t reg, int flags)
69 1.1.2.2 jdolecek {
70 1.1.2.2 jdolecek uint8_t val = 0;
71 1.1.2.2 jdolecek int error;
72 1.1.2.2 jdolecek
73 1.1.2.2 jdolecek error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
74 1.1.2.2 jdolecek ®, 1, &val, 1, flags);
75 1.1.2.2 jdolecek if (error != 0)
76 1.1.2.2 jdolecek aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
77 1.1.2.2 jdolecek
78 1.1.2.2 jdolecek return val;
79 1.1.2.2 jdolecek }
80 1.1.2.2 jdolecek
81 1.1.2.2 jdolecek static void
82 1.1.2.2 jdolecek sy8106a_write(struct sy8106a_softc *sc, uint8_t reg, uint8_t val, int flags)
83 1.1.2.2 jdolecek {
84 1.1.2.2 jdolecek uint8_t buf[2];
85 1.1.2.2 jdolecek int error;
86 1.1.2.2 jdolecek
87 1.1.2.2 jdolecek buf[0] = reg;
88 1.1.2.2 jdolecek buf[1] = val;
89 1.1.2.2 jdolecek
90 1.1.2.2 jdolecek error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
91 1.1.2.2 jdolecek NULL, 0, buf, 2, flags);
92 1.1.2.2 jdolecek if (error != 0)
93 1.1.2.2 jdolecek aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
94 1.1.2.2 jdolecek }
95 1.1.2.2 jdolecek
96 1.1.2.2 jdolecek #define I2C_READ(sc, reg) sy8106a_read((sc), (reg), I2C_F_POLL)
97 1.1.2.2 jdolecek #define I2C_WRITE(sc, reg, val) sy8106a_write((sc), (reg), (val), I2C_F_POLL)
98 1.1.2.2 jdolecek #define I2C_LOCK(sc) iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL)
99 1.1.2.2 jdolecek #define I2C_UNLOCK(sc) iic_release_bus((sc)->sc_i2c, I2C_F_POLL)
100 1.1.2.2 jdolecek
101 1.1.2.2 jdolecek static int
102 1.1.2.2 jdolecek sy8106a_acquire(device_t dev)
103 1.1.2.2 jdolecek {
104 1.1.2.2 jdolecek return 0;
105 1.1.2.2 jdolecek }
106 1.1.2.2 jdolecek
107 1.1.2.2 jdolecek static void
108 1.1.2.2 jdolecek sy8106a_release(device_t dev)
109 1.1.2.2 jdolecek {
110 1.1.2.2 jdolecek }
111 1.1.2.2 jdolecek
112 1.1.2.2 jdolecek static int
113 1.1.2.2 jdolecek sy8106a_enable(device_t dev, bool enable)
114 1.1.2.2 jdolecek {
115 1.1.2.2 jdolecek struct sy8106a_softc * const sc = device_private(dev);
116 1.1.2.2 jdolecek uint8_t val;
117 1.1.2.2 jdolecek
118 1.1.2.2 jdolecek I2C_LOCK(sc);
119 1.1.2.2 jdolecek val = I2C_READ(sc, VOUT_COM);
120 1.1.2.2 jdolecek if (enable)
121 1.1.2.2 jdolecek val &= ~COM_DISABLE;
122 1.1.2.2 jdolecek else
123 1.1.2.2 jdolecek val |= COM_DISABLE;
124 1.1.2.2 jdolecek I2C_WRITE(sc, VOUT_COM, val);
125 1.1.2.2 jdolecek I2C_UNLOCK(sc);
126 1.1.2.2 jdolecek
127 1.1.2.2 jdolecek if (sc->sc_ramp_delay)
128 1.1.2.2 jdolecek delay(sc->sc_ramp_delay);
129 1.1.2.2 jdolecek
130 1.1.2.2 jdolecek return 0;
131 1.1.2.2 jdolecek }
132 1.1.2.2 jdolecek
133 1.1.2.2 jdolecek static int
134 1.1.2.2 jdolecek sy8106a_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
135 1.1.2.2 jdolecek {
136 1.1.2.2 jdolecek struct sy8106a_softc * const sc = device_private(dev);
137 1.1.2.2 jdolecek uint8_t val, oval;
138 1.1.2.2 jdolecek u_int cur_uvol;
139 1.1.2.2 jdolecek
140 1.1.2.2 jdolecek I2C_LOCK(sc);
141 1.1.2.2 jdolecek
142 1.1.2.2 jdolecek /* Get current voltage */
143 1.1.2.2 jdolecek oval = I2C_READ(sc, VOUT1_SEL);
144 1.1.2.2 jdolecek cur_uvol = __SHIFTOUT(oval, SEL_VOLTAGE) * SEL_VOLTAGE_STEP +
145 1.1.2.2 jdolecek SEL_VOLTAGE_BASE;
146 1.1.2.2 jdolecek
147 1.1.2.2 jdolecek /* Set new voltage */
148 1.1.2.2 jdolecek val = SEL_GO | ((min_uvol - SEL_VOLTAGE_BASE) / SEL_VOLTAGE_STEP);
149 1.1.2.2 jdolecek I2C_WRITE(sc, VOUT1_SEL, val);
150 1.1.2.2 jdolecek
151 1.1.2.2 jdolecek I2C_UNLOCK(sc);
152 1.1.2.2 jdolecek
153 1.1.2.2 jdolecek /* Time to delay is based on the number of voltage steps */
154 1.1.2.2 jdolecek if (sc->sc_ramp_delay)
155 1.1.2.2 jdolecek delay((abs(cur_uvol - min_uvol) / SEL_VOLTAGE_STEP) * sc->sc_ramp_delay);
156 1.1.2.2 jdolecek
157 1.1.2.2 jdolecek return 0;
158 1.1.2.2 jdolecek }
159 1.1.2.2 jdolecek
160 1.1.2.2 jdolecek static int
161 1.1.2.2 jdolecek sy8106a_get_voltage(device_t dev, u_int *puvol)
162 1.1.2.2 jdolecek {
163 1.1.2.2 jdolecek struct sy8106a_softc * const sc = device_private(dev);
164 1.1.2.2 jdolecek uint8_t val;
165 1.1.2.2 jdolecek
166 1.1.2.2 jdolecek I2C_LOCK(sc);
167 1.1.2.2 jdolecek val = I2C_READ(sc, VOUT1_SEL);
168 1.1.2.2 jdolecek I2C_UNLOCK(sc);
169 1.1.2.2 jdolecek
170 1.1.2.2 jdolecek *puvol = __SHIFTOUT(val, SEL_VOLTAGE) * SEL_VOLTAGE_STEP +
171 1.1.2.2 jdolecek SEL_VOLTAGE_BASE;
172 1.1.2.2 jdolecek
173 1.1.2.2 jdolecek return 0;
174 1.1.2.2 jdolecek }
175 1.1.2.2 jdolecek
176 1.1.2.2 jdolecek static struct fdtbus_regulator_controller_func sy8106a_funcs = {
177 1.1.2.2 jdolecek .acquire = sy8106a_acquire,
178 1.1.2.2 jdolecek .release = sy8106a_release,
179 1.1.2.2 jdolecek .enable = sy8106a_enable,
180 1.1.2.2 jdolecek .set_voltage = sy8106a_set_voltage,
181 1.1.2.2 jdolecek .get_voltage = sy8106a_get_voltage,
182 1.1.2.2 jdolecek };
183 1.1.2.2 jdolecek
184 1.1.2.2 jdolecek static int
185 1.1.2.2 jdolecek sy8106a_match(device_t parent, cfdata_t match, void *aux)
186 1.1.2.2 jdolecek {
187 1.1.2.2 jdolecek struct i2c_attach_args *ia = aux;
188 1.1.2.2 jdolecek
189 1.1.2.2 jdolecek if (ia->ia_name == NULL)
190 1.1.2.2 jdolecek return 0;
191 1.1.2.2 jdolecek
192 1.1.2.2 jdolecek return iic_compat_match(ia, compatible);
193 1.1.2.2 jdolecek }
194 1.1.2.2 jdolecek
195 1.1.2.2 jdolecek static void
196 1.1.2.2 jdolecek sy8106a_attach(device_t parent, device_t self, void *aux)
197 1.1.2.2 jdolecek {
198 1.1.2.2 jdolecek struct sy8106a_softc * const sc = device_private(self);
199 1.1.2.2 jdolecek struct i2c_attach_args *ia = aux;
200 1.1.2.2 jdolecek
201 1.1.2.2 jdolecek sc->sc_dev = self;
202 1.1.2.2 jdolecek sc->sc_i2c = ia->ia_tag;
203 1.1.2.2 jdolecek sc->sc_addr = ia->ia_addr;
204 1.1.2.2 jdolecek sc->sc_phandle = ia->ia_cookie;
205 1.1.2.2 jdolecek
206 1.1.2.2 jdolecek aprint_naive("\n");
207 1.1.2.2 jdolecek aprint_normal(": Silergy SY8106A regulator\n");
208 1.1.2.2 jdolecek
209 1.1.2.2 jdolecek of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay",
210 1.1.2.2 jdolecek &sc->sc_ramp_delay);
211 1.1.2.2 jdolecek
212 1.1.2.2 jdolecek fdtbus_register_regulator_controller(self, sc->sc_phandle,
213 1.1.2.2 jdolecek &sy8106a_funcs);
214 1.1.2.2 jdolecek }
215 1.1.2.2 jdolecek
216 1.1.2.2 jdolecek CFATTACH_DECL_NEW(sy8106a, sizeof(struct sy8106a_softc),
217 1.1.2.2 jdolecek sy8106a_match, sy8106a_attach, NULL, NULL);
218