xref: /netbsd-src/sys/dev/i2c/max77620.c (revision 18f3098ca55677bef03345e9be442d445d06437b)
1 /* $NetBSD: max77620.c,v 1.11 2021/01/27 02:29:48 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@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.11 2021/01/27 02:29:48 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 struct max77620_pin {
64 	struct max77620_softc	*pin_sc;
65 	int			pin_num;
66 	int			pin_flags;
67 	bool			pin_actlo;
68 };
69 
70 static const struct device_compatible_entry compat_data[] = {
71 	{ .compat = "maxim,max77620" },
72 	DEVICE_COMPAT_EOL
73 };
74 
75 static uint8_t
max77620_read(struct max77620_softc * sc,uint8_t reg,int flags)76 max77620_read(struct max77620_softc *sc, uint8_t reg, int flags)
77 {
78 	uint8_t val = 0;
79 	int error;
80 
81 	error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
82 	    &reg, 1, &val, 1, flags);
83 	if (error != 0)
84 		aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
85 
86 	return val;
87 }
88 
89 static void
max77620_write(struct max77620_softc * sc,uint8_t reg,uint8_t val,int flags)90 max77620_write(struct max77620_softc *sc, uint8_t reg, uint8_t val, int flags)
91 {
92 	int error;
93 
94 	error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
95 	    &reg, 1, &val, 1, flags);
96 	if (error != 0)
97 		aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
98 }
99 
100 #define	I2C_READ(sc, reg)	max77620_read((sc), (reg), 0)
101 #define	I2C_WRITE(sc, reg, val)	max77620_write((sc), (reg), (val), 0)
102 #define	I2C_LOCK(sc)		iic_acquire_bus((sc)->sc_i2c, 0)
103 #define	I2C_UNLOCK(sc)		iic_release_bus((sc)->sc_i2c, 0)
104 
105 static int
max77620_gpio_config(struct max77620_softc * sc,int pin,int flags)106 max77620_gpio_config(struct max77620_softc *sc, int pin, int flags)
107 {
108 	uint32_t gpio;
109 
110 	KASSERT(pin >= 0 && pin < MAX_GPIO_COUNT);
111 
112 	gpio = I2C_READ(sc, MAX_GPIO_REG(pin));
113 
114 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
115 	case GPIO_PIN_INPUT:
116 		gpio |= MAX_GPIO_DIR;
117 		break;
118 	case GPIO_PIN_OUTPUT:
119 		gpio &= ~MAX_GPIO_DIR;
120 		break;
121 	default:
122 		return EINVAL;
123 	}
124 
125 	switch (flags & (GPIO_PIN_PUSHPULL|GPIO_PIN_OPENDRAIN)) {
126 	case GPIO_PIN_PUSHPULL:
127 		gpio |= MAX_GPIO_DRV;
128 		break;
129 	case GPIO_PIN_OPENDRAIN:
130 		gpio &= ~MAX_GPIO_DRV;
131 		break;
132 	default:
133 		return EINVAL;
134 	}
135 
136 	I2C_WRITE(sc, MAX_GPIO_REG(pin), gpio);
137 
138 	return 0;
139 }
140 
141 static void *
max77620_gpio_acquire(device_t dev,const void * data,size_t len,int flags)142 max77620_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
143 {
144 	struct max77620_softc * const sc = device_private(dev);
145 	struct max77620_pin *gpin;
146 	const u_int *gpio = data;
147 	int error;
148 
149 	if (len != 12)
150 		return NULL;
151 
152 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
153 	const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
154 	const bool pushpull = (be32toh(gpio[2]) & __BIT(1)) == 0;
155 	const bool opendrain = (be32toh(gpio[2]) & __BIT(2)) != 0;
156 
157 	if (pushpull)
158 		flags |= GPIO_PIN_PUSHPULL;
159 	if (opendrain)
160 		flags |= GPIO_PIN_OPENDRAIN;
161 
162 	if (pin >= MAX_GPIO_COUNT)
163 		return NULL;
164 
165 	I2C_LOCK(sc);
166 	error = max77620_gpio_config(sc, pin, flags);
167 	I2C_UNLOCK(sc);
168 
169 	if (error != 0) {
170 		device_printf(dev, "bad pin %d config %#x\n", pin, flags);
171 		return NULL;
172 	}
173 
174 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
175 	gpin->pin_sc = sc;
176 	gpin->pin_num = pin;
177 	gpin->pin_flags = flags;
178 	gpin->pin_actlo = actlo;
179 
180 	return gpin;
181 }
182 
183 static void
max77620_gpio_release(device_t dev,void * priv)184 max77620_gpio_release(device_t dev, void *priv)
185 {
186 	struct max77620_softc * const sc = device_private(dev);
187 	struct max77620_pin *gpin = priv;
188 
189 	I2C_LOCK(sc);
190 	max77620_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT|GPIO_PIN_OPENDRAIN);
191 	I2C_UNLOCK(sc);
192 
193 	kmem_free(gpin, sizeof(*gpin));
194 }
195 
196 static int
max77620_gpio_read(device_t dev,void * priv,bool raw)197 max77620_gpio_read(device_t dev, void *priv, bool raw)
198 {
199 	struct max77620_softc * const sc = device_private(dev);
200 	struct max77620_pin *gpin = priv;
201 	uint8_t gpio;
202 	int val;
203 
204 	/*
205 	 * Performing a register read only; no need to acquire
206 	 * the max77620 lock.
207 	 */
208 
209 	I2C_LOCK(sc);
210 	gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
211 	I2C_UNLOCK(sc);
212 
213 	val = __SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL);
214 	if (!raw && gpin->pin_actlo)
215 		val = !val;
216 
217 #ifdef MAX77620_DEBUG
218 	device_printf(sc->sc_dev, "GPIO%d rd %02x (%d %d)\n",
219 	    gpin->pin_num, gpio,
220 	    (int)__SHIFTOUT(gpio, MAX_GPIO_INPUT_VAL), val);
221 #endif
222 
223 	return val;
224 }
225 
226 static void
max77620_gpio_write(device_t dev,void * priv,int val,bool raw)227 max77620_gpio_write(device_t dev, void *priv, int val, bool raw)
228 {
229 	struct max77620_softc * const sc = device_private(dev);
230 	struct max77620_pin *gpin = priv;
231 	uint8_t gpio;
232 
233 	if (!raw && gpin->pin_actlo)
234 		val = !val;
235 
236 	I2C_LOCK(sc);
237 	gpio = I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num));
238 	gpio &= ~MAX_GPIO_OUTPUT_VAL;
239 	gpio |= __SHIFTIN(val, MAX_GPIO_OUTPUT_VAL);
240 #ifdef MAX77620_DEBUG
241 	device_printf(sc->sc_dev, "GPIO%d wr %02x -> %02x\n",
242 	    gpin->pin_num,
243 	    I2C_READ(sc, MAX_GPIO_REG(gpin->pin_num)), gpio);
244 #endif
245 	I2C_WRITE(sc, MAX_GPIO_REG(gpin->pin_num), gpio);
246 	I2C_UNLOCK(sc);
247 }
248 
249 static struct fdtbus_gpio_controller_func max77620_gpio_funcs = {
250 	.acquire = max77620_gpio_acquire,
251 	.release = max77620_gpio_release,
252 	.read = max77620_gpio_read,
253 	.write = max77620_gpio_write,
254 };
255 
256 static void
max77620_gpio_attach(struct max77620_softc * sc)257 max77620_gpio_attach(struct max77620_softc *sc)
258 {
259 	fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle,
260 	    &max77620_gpio_funcs);
261 }
262 
263 static int
max77620_match(device_t parent,cfdata_t match,void * aux)264 max77620_match(device_t parent, cfdata_t match, void *aux)
265 {
266 	struct i2c_attach_args *ia = aux;
267 	int match_result;
268 
269 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
270 		return match_result;
271 
272 	return 0;
273 }
274 
275 static void
max77620_attach(device_t parent,device_t self,void * aux)276 max77620_attach(device_t parent, device_t self, void *aux)
277 {
278 	struct max77620_softc * const sc = device_private(self);
279 	struct i2c_attach_args *ia = aux;
280 
281 	sc->sc_dev = self;
282 	sc->sc_i2c = ia->ia_tag;
283 	sc->sc_addr = ia->ia_addr;
284 	sc->sc_phandle = ia->ia_cookie;
285 
286 	aprint_naive("\n");
287 	aprint_normal(": MAX77620 Power Management IC\n");
288 
289 	max77620_gpio_attach(sc);
290 }
291 
292 CFATTACH_DECL_NEW(max77620pmic, sizeof(struct max77620_softc),
293     max77620_match, max77620_attach, NULL, NULL);
294