max77620.c revision 1.7 1 /* $NetBSD: max77620.c,v 1.7 2019/12/23 18:49:13 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: max77620.c,v 1.7 2019/12/23 18:49:13 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mutex.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/conf.h>
38 #include <sys/bus.h>
39 #include <sys/kmem.h>
40 #include <sys/gpio.h>
41
42 #include <dev/i2c/i2cvar.h>
43
44 #include <dev/fdt/fdtvar.h>
45
46 #define MAX_GPIO_REG(n) (0x36 + (n))
47 #define MAX_GPIO_DEBOUNCE __BITS(7,6)
48 #define MAX_GPIO_INT __BITS(5,4)
49 #define MAX_GPIO_OUTPUT_VAL __BIT(3)
50 #define MAX_GPIO_INPUT_VAL __BIT(2)
51 #define MAX_GPIO_DIR __BIT(1)
52 #define MAX_GPIO_DRV __BIT(0)
53
54 #define MAX_GPIO_COUNT 8
55
56 struct max77620_softc {
57 device_t sc_dev;
58 i2c_tag_t sc_i2c;
59 i2c_addr_t sc_addr;
60 int sc_phandle;
61
62 /*
63 * Locking order:
64 *
65 * max77620 -> i2c
66 */
67 kmutex_t sc_lock;
68 };
69
70 struct max77620_pin {
71 struct max77620_softc *pin_sc;
72 int pin_num;
73 int pin_flags;
74 bool pin_actlo;
75 };
76
77 static const struct device_compatible_entry compat_data[] = {
78 { "maxim,max77620", 0 },
79 { NULL, 0 }
80 };
81
82 static uint8_t
83 max77620_read(struct max77620_softc *sc, uint8_t reg, int flags)
84 {
85 uint8_t val = 0;
86 int error;
87
88 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
89 ®, 1, &val, 1, flags);
90 if (error != 0)
91 aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
92
93 return val;
94 }
95
96 static void
97 max77620_write(struct max77620_softc *sc, uint8_t reg, uint8_t val, int flags)
98 {
99 int error;
100
101 error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
102 ®, 1, &val, 1, flags);
103 if (error != 0)
104 aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
105 }
106
107 #define I2C_READ(sc, reg) max77620_read((sc), (reg), 0)
108 #define I2C_WRITE(sc, reg, val) max77620_write((sc), (reg), (val), 0)
109 #define I2C_LOCK(sc) iic_acquire_bus((sc)->sc_i2c, 0)
110 #define I2C_UNLOCK(sc) iic_release_bus((sc)->sc_i2c, 0)
111
112 static int
113 max77620_gpio_config(struct max77620_softc *sc, int pin, int flags)
114 {
115 uint32_t gpio;
116
117 KASSERT(pin >= 0 && pin < MAX_GPIO_COUNT);
118 KASSERT(mutex_owned(&sc->sc_lock));
119
120 gpio = I2C_READ(sc, MAX_GPIO_REG(pin));
121
122 switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
123 case GPIO_PIN_INPUT:
124 gpio |= MAX_GPIO_DIR;
125 break;
126 case GPIO_PIN_OUTPUT:
127 gpio &= ~MAX_GPIO_DIR;
128 break;
129 default:
130 return EINVAL;
131 }
132
133 switch (flags & (GPIO_PIN_PUSHPULL|GPIO_PIN_OPENDRAIN)) {
134 case GPIO_PIN_PUSHPULL:
135 gpio |= MAX_GPIO_DRV;
136 break;
137 case GPIO_PIN_OPENDRAIN:
138 gpio &= ~MAX_GPIO_DRV;
139 break;
140 default:
141 return EINVAL;
142 }
143
144 I2C_WRITE(sc, MAX_GPIO_REG(pin), gpio);
145
146 return 0;
147 }
148
149 static void *
150 max77620_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
151 {
152 struct max77620_softc * const sc = device_private(dev);
153 struct max77620_pin *gpin;
154 const u_int *gpio = data;
155 int error;
156
157 if (len != 12)
158 return NULL;
159
160 const uint8_t pin = be32toh(gpio[1]) & 0xff;
161 const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
162 const bool pushpull = (be32toh(gpio[2]) & __BIT(1)) == 0;
163 const bool opendrain = (be32toh(gpio[2]) & __BIT(2)) != 0;
164
165 if (pushpull)
166 flags |= GPIO_PIN_PUSHPULL;
167 if (opendrain)
168 flags |= GPIO_PIN_OPENDRAIN;
169
170 if (pin >= MAX_GPIO_COUNT)
171 return NULL;
172
173 mutex_enter(&sc->sc_lock);
174 I2C_LOCK(sc);
175 error = max77620_gpio_config(sc, pin, flags);
176 I2C_UNLOCK(sc);
177 mutex_exit(&sc->sc_lock);
178
179 if (error != 0) {
180 device_printf(dev, "bad pin %d config %#x\n", pin, flags);
181 return NULL;
182 }
183
184 gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
185 gpin->pin_sc = sc;
186 gpin->pin_num = pin;
187 gpin->pin_flags = flags;
188 gpin->pin_actlo = actlo;
189
190 return gpin;
191 }
192
193 static void
194 max77620_gpio_release(device_t dev, void *priv)
195 {
196 struct max77620_softc * const sc = device_private(dev);
197 struct max77620_pin *gpin = priv;
198
199 mutex_enter(&sc->sc_lock);
200 I2C_LOCK(sc);
201 max77620_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT|GPIO_PIN_OPENDRAIN);
202 I2C_UNLOCK(sc);
203 mutex_exit(&sc->sc_lock);
204
205 kmem_free(gpin, sizeof(*gpin));
206 }
207
208 static int
209 max77620_gpio_read(device_t dev, void *priv, bool raw)
210 {
211 struct max77620_softc * const sc = device_private(dev);
212 struct max77620_pin *gpin = priv;
213 uint8_t gpio;
214 int val;
215
216 /*
217 * Performing a register read only; no need to acquire
218 * the max77620 lock.
219 */
220
221 I2C_LOCK(sc);
222 gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
223 I2C_UNLOCK(sc);
224
225 val = __SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL);
226 if (!raw && gpin->pin_actlo)
227 val = !val;
228
229 #ifdef MAX77620_DEBUG
230 device_printf(sc->sc_dev, "GPIO%d rd %02x (%d %d)\n",
231 gpin->pin_num, gpio,
232 (int)__SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL), val);
233 #endif
234
235 return val;
236 }
237
238 static void
239 max77620_gpio_write(device_t dev, void *priv, int val, bool raw)
240 {
241 struct max77620_softc * const sc = device_private(dev);
242 struct max77620_pin *gpin = priv;
243 uint8_t gpio;
244
245 if (!raw && gpin->pin_actlo)
246 val = !val;
247
248 mutex_enter(&sc->sc_lock);
249 I2C_LOCK(sc);
250 gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
251 gpio &= ~MAX_GPIO_OUTPUT_VAL;
252 gpio |= __SHIFTIN(val, MAX_GPIO_OUTPUT_VAL);
253 #ifdef MAX77620_DEBUG
254 device_printf(sc->sc_dev, "GPIO%d wr %02x -> %02x\n",
255 gpin->pin_num,
256 I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num)), gpio);
257 #endif
258 I2C_WRITE(sc, MAX_GPIO_REG(gpin->pin_num), gpio);
259 I2C_UNLOCK(sc);
260 mutex_exit(&sc->sc_lock);
261 }
262
263 static struct fdtbus_gpio_controller_func max77620_gpio_funcs = {
264 .acquire = max77620_gpio_acquire,
265 .release = max77620_gpio_release,
266 .read = max77620_gpio_read,
267 .write = max77620_gpio_write,
268 };
269
270 static void
271 max77620_gpio_attach(struct max77620_softc *sc)
272 {
273 fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle,
274 &max77620_gpio_funcs);
275 }
276
277 static int
278 max77620_match(device_t parent, cfdata_t match, void *aux)
279 {
280 struct i2c_attach_args *ia = aux;
281 int match_result;
282
283 if (iic_use_direct_match(ia, match, compat_data, &match_result))
284 return match_result;
285
286 return 0;
287 }
288
289 static void
290 max77620_attach(device_t parent, device_t self, void *aux)
291 {
292 struct max77620_softc * const sc = device_private(self);
293 struct i2c_attach_args *ia = aux;
294
295 sc->sc_dev = self;
296 sc->sc_i2c = ia->ia_tag;
297 sc->sc_addr = ia->ia_addr;
298 sc->sc_phandle = ia->ia_cookie;
299
300 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
301
302 aprint_naive("\n");
303 aprint_normal(": MAX77620 Power Management IC\n");
304
305 max77620_gpio_attach(sc);
306 }
307
308 CFATTACH_DECL_NEW(max77620pmic, sizeof(struct max77620_softc),
309 max77620_match, max77620_attach, NULL, NULL);
310