xref: /freebsd/sys/arm/nvidia/as3722.c (revision b85b9c88eb02298ea7fa3885619f54ac0e930ba4)
1 /*-
2  * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * AS3722 PMIC driver
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/gpio.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/rman.h>
42 #include <sys/sx.h>
43 
44 #include <machine/bus.h>
45 
46 #include <dev/extres/regulator/regulator.h>
47 #include <dev/fdt/fdt_pinctrl.h>
48 #include <dev/gpio/gpiobusvar.h>
49 #include <dev/iicbus/iiconf.h>
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 
54 #include <dt-bindings/mfd/as3722.h>
55 
56 #include "clock_if.h"
57 #include "regdev_if.h"
58 
59 #include "as3722.h"
60 
61 static struct ofw_compat_data compat_data[] = {
62 	{"ams,as3722",		1},
63 	{NULL,			0},
64 };
65 
66 #define	LOCK(_sc)		sx_xlock(&(_sc)->lock)
67 #define	UNLOCK(_sc)		sx_xunlock(&(_sc)->lock)
68 #define	LOCK_INIT(_sc)		sx_init(&(_sc)->lock, "as3722")
69 #define	LOCK_DESTROY(_sc)	sx_destroy(&(_sc)->lock);
70 #define	ASSERT_LOCKED(_sc)	sx_assert(&(_sc)->lock, SA_XLOCKED);
71 #define	ASSERT_UNLOCKED(_sc)	sx_assert(&(_sc)->lock, SA_UNLOCKED);
72 
73 #define	AS3722_DEVICE_ID	0x0C
74 
75 /*
76  * Raw register access function.
77  */
78 int
79 as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val)
80 {
81 	uint8_t addr;
82 	int rv;
83 	struct iic_msg msgs[2] = {
84 		{0, IIC_M_WR, 1, &addr},
85 		{0, IIC_M_RD, 1, val},
86 	};
87 
88 	msgs[0].slave = sc->bus_addr;
89 	msgs[1].slave = sc->bus_addr;
90 	addr = reg;
91 
92 	rv = iicbus_transfer(sc->dev, msgs, 2);
93 	if (rv != 0) {
94 		device_printf(sc->dev,
95 		    "Error when reading reg 0x%02X, rv: %d\n", reg,  rv);
96 		return (EIO);
97 	}
98 
99 	return (0);
100 }
101 
102 int as3722_read_buf(struct as3722_softc *sc, uint8_t reg, uint8_t *buf,
103     size_t size)
104 {
105 	uint8_t addr;
106 	int rv;
107 	struct iic_msg msgs[2] = {
108 		{0, IIC_M_WR, 1, &addr},
109 		{0, IIC_M_RD, size, buf},
110 	};
111 
112 	msgs[0].slave = sc->bus_addr;
113 	msgs[1].slave = sc->bus_addr;
114 	addr = reg;
115 
116 	rv = iicbus_transfer(sc->dev, msgs, 2);
117 	if (rv != 0) {
118 		device_printf(sc->dev,
119 		    "Error when reading reg 0x%02X, rv: %d\n", reg,  rv);
120 		return (EIO);
121 	}
122 
123 	return (0);
124 }
125 
126 int
127 as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val)
128 {
129 	uint8_t data[2];
130 	int rv;
131 
132 	struct iic_msg msgs[1] = {
133 		{0, IIC_M_WR, 2, data},
134 	};
135 
136 	msgs[0].slave = sc->bus_addr;
137 	data[0] = reg;
138 	data[1] = val;
139 
140 	rv = iicbus_transfer(sc->dev, msgs, 1);
141 	if (rv != 0) {
142 		device_printf(sc->dev,
143 		    "Error when writing reg 0x%02X, rv: %d\n", reg, rv);
144 		return (EIO);
145 	}
146 	return (0);
147 }
148 
149 int as3722_write_buf(struct as3722_softc *sc, uint8_t reg, uint8_t *buf,
150     size_t size)
151 {
152 	uint8_t data[1];
153 	int rv;
154 	struct iic_msg msgs[2] = {
155 		{0, IIC_M_WR, 1, data},
156 		{0, IIC_M_WR | IIC_M_NOSTART, size, buf},
157 	};
158 
159 	msgs[0].slave = sc->bus_addr;
160 	msgs[1].slave = sc->bus_addr;
161 	data[0] = reg;
162 
163 	rv = iicbus_transfer(sc->dev, msgs, 2);
164 	if (rv != 0) {
165 		device_printf(sc->dev,
166 		    "Error when writing reg 0x%02X, rv: %d\n", reg, rv);
167 		return (EIO);
168 	}
169 	return (0);
170 }
171 
172 int
173 as3722_modify(struct as3722_softc *sc, uint8_t reg, uint8_t clear, uint8_t set)
174 {
175 	uint8_t val;
176 	int rv;
177 
178 	rv = as3722_read(sc, reg, &val);
179 	if (rv != 0)
180 		return (rv);
181 
182 	val &= ~clear;
183 	val |= set;
184 
185 	rv = as3722_write(sc, reg, val);
186 	if (rv != 0)
187 		return (rv);
188 
189 	return (0);
190 }
191 
192 static int
193 as3722_get_version(struct as3722_softc *sc)
194 {
195 	uint8_t reg;
196 	int rv;
197 
198 	/* Verify AS3722 ID and version. */
199 	rv = RD1(sc, AS3722_ASIC_ID1, &reg);
200 	if (rv != 0)
201 		return (ENXIO);
202 
203 	if (reg != AS3722_DEVICE_ID) {
204 		device_printf(sc->dev, "Invalid chip ID is 0x%x\n", reg);
205 		return (ENXIO);
206 	}
207 
208 	rv = RD1(sc, AS3722_ASIC_ID2, &sc->chip_rev);
209 	if (rv != 0)
210 		return (ENXIO);
211 
212 	if (bootverbose)
213 		device_printf(sc->dev, "AS3722 rev: 0x%x\n", sc->chip_rev);
214 	return (0);
215 }
216 
217 static int
218 as3722_init(struct as3722_softc *sc)
219 {
220 	uint32_t reg;
221 	int rv;
222 
223 	reg = 0;
224 	if (sc->int_pullup)
225 		reg |= AS3722_INT_PULL_UP;
226 	if (sc->i2c_pullup)
227 		reg |= AS3722_I2C_PULL_UP;
228 
229 	rv = RM1(sc, AS3722_IO_VOLTAGE,
230 	    AS3722_INT_PULL_UP | AS3722_I2C_PULL_UP, reg);
231 	if (rv != 0)
232 		return (ENXIO);
233 
234 	/* mask interrupts */
235 	rv = WR1(sc, AS3722_INTERRUPT_MASK1, 0);
236 	if (rv != 0)
237 		return (ENXIO);
238 	rv = WR1(sc, AS3722_INTERRUPT_MASK2, 0);
239 	if (rv != 0)
240 		return (ENXIO);
241 	rv = WR1(sc, AS3722_INTERRUPT_MASK3, 0);
242 	if (rv != 0)
243 		return (ENXIO);
244 	rv = WR1(sc, AS3722_INTERRUPT_MASK4, 0);
245 	if (rv != 0)
246 		return (ENXIO);
247 	return (0);
248 }
249 
250 static int
251 as3722_parse_fdt(struct as3722_softc *sc, phandle_t node)
252 {
253 
254 	sc->int_pullup =
255 	    OF_hasprop(node, "ams,enable-internal-int-pullup") ? 1 : 0;
256 	sc->i2c_pullup =
257 	    OF_hasprop(node, "ams,enable-internal-i2c-pullup") ? 1 : 0;
258 	return 0;
259 }
260 
261 static void
262 as3722_intr(void *arg)
263 {
264 	/* XXX Finish temperature alarms. */
265 }
266 
267 static int
268 as3722_probe(device_t dev)
269 {
270 
271 	if (!ofw_bus_status_okay(dev))
272 		return (ENXIO);
273 
274 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
275 		return (ENXIO);
276 
277 	device_set_desc(dev, "AS3722 PMIC");
278 	return (BUS_PROBE_DEFAULT);
279 }
280 
281 static int
282 as3722_attach(device_t dev)
283 {
284 	struct as3722_softc *sc;
285 	int rv, rid;
286 	phandle_t node;
287 
288 	sc = device_get_softc(dev);
289 	sc->dev = dev;
290 	sc->bus_addr = iicbus_get_addr(dev);
291 	node = ofw_bus_get_node(sc->dev);
292 	rv = 0;
293 	LOCK_INIT(sc);
294 
295 	rid = 0;
296 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
297 	    RF_ACTIVE);
298 	if (sc->irq_res == NULL) {
299 		device_printf(dev, "Cannot allocate interrupt.\n");
300 		rv = ENXIO;
301 		goto fail;
302 	}
303 
304 	rv = as3722_parse_fdt(sc, node);
305 	if (rv != 0)
306 		goto fail;
307 	rv = as3722_get_version(sc);
308 	if (rv != 0)
309 		goto fail;
310 	rv = as3722_init(sc);
311 	if (rv != 0)
312 		goto fail;
313 	rv = as3722_regulator_attach(sc, node);
314 	if (rv != 0)
315 		goto fail;
316 	rv = as3722_gpio_attach(sc, node);
317 	if (rv != 0)
318 		goto fail;
319 	rv = as3722_rtc_attach(sc, node);
320 	if (rv != 0)
321 		goto fail;
322 
323 	fdt_pinctrl_register(dev, NULL);
324 	fdt_pinctrl_configure_by_name(dev, "default");
325 
326 	/* Setup  interrupt. */
327 	rv = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
328 	    NULL, as3722_intr, sc, &sc->irq_h);
329 	if (rv) {
330 		device_printf(dev, "Cannot setup interrupt.\n");
331 		goto fail;
332 	}
333 	return (bus_generic_attach(dev));
334 
335 fail:
336 	if (sc->irq_h != NULL)
337 		bus_teardown_intr(dev, sc->irq_res, sc->irq_h);
338 	if (sc->irq_res != NULL)
339 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
340 	LOCK_DESTROY(sc);
341 	return (rv);
342 }
343 
344 static int
345 as3722_detach(device_t dev)
346 {
347 	struct as3722_softc *sc;
348 
349 	sc = device_get_softc(dev);
350 	if (sc->irq_h != NULL)
351 		bus_teardown_intr(dev, sc->irq_res, sc->irq_h);
352 	if (sc->irq_res != NULL)
353 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
354 	LOCK_DESTROY(sc);
355 
356 	return (bus_generic_detach(dev));
357 }
358 
359 static phandle_t
360 as3722_gpio_get_node(device_t bus, device_t dev)
361 {
362 
363 	/* We only have one child, the GPIO bus, which needs our own node. */
364 	return (ofw_bus_get_node(bus));
365 }
366 
367 static device_method_t as3722_methods[] = {
368 	/* Device interface */
369 	DEVMETHOD(device_probe,		as3722_probe),
370 	DEVMETHOD(device_attach,	as3722_attach),
371 	DEVMETHOD(device_detach,	as3722_detach),
372 
373 	/* Regdev interface */
374 	DEVMETHOD(regdev_map,		as3722_regulator_map),
375 
376 	/* RTC interface */
377 	DEVMETHOD(clock_gettime,	as3722_rtc_gettime),
378 	DEVMETHOD(clock_settime,	as3722_rtc_settime),
379 
380 	/* GPIO protocol interface */
381 	DEVMETHOD(gpio_get_bus,		as3722_gpio_get_bus),
382 	DEVMETHOD(gpio_pin_max,		as3722_gpio_pin_max),
383 	DEVMETHOD(gpio_pin_getname,	as3722_gpio_pin_getname),
384 	DEVMETHOD(gpio_pin_getflags,	as3722_gpio_pin_getflags),
385 	DEVMETHOD(gpio_pin_getcaps,	as3722_gpio_pin_getcaps),
386 	DEVMETHOD(gpio_pin_setflags,	as3722_gpio_pin_setflags),
387 	DEVMETHOD(gpio_pin_get,		as3722_gpio_pin_get),
388 	DEVMETHOD(gpio_pin_set,		as3722_gpio_pin_set),
389 	DEVMETHOD(gpio_pin_toggle,	as3722_gpio_pin_toggle),
390 	DEVMETHOD(gpio_map_gpios,	as3722_gpio_map_gpios),
391 
392 	/* fdt_pinctrl interface */
393 	DEVMETHOD(fdt_pinctrl_configure, as3722_pinmux_configure),
394 
395 	/* ofw_bus interface */
396 	DEVMETHOD(ofw_bus_get_node,	as3722_gpio_get_node),
397 
398 	DEVMETHOD_END
399 };
400 
401 static devclass_t as3722_devclass;
402 static DEFINE_CLASS_0(gpio, as3722_driver, as3722_methods,
403     sizeof(struct as3722_softc));
404 EARLY_DRIVER_MODULE(as3722, iicbus, as3722_driver, as3722_devclass,
405     NULL, NULL, 74);
406