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