xref: /freebsd/sys/arm/nvidia/as3722.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 	struct as3722_softc *sc;
265 
266 	sc = (struct as3722_softc *)arg;
267 	/* XXX Finish temperature alarms. */
268 }
269 
270 static int
271 as3722_probe(device_t dev)
272 {
273 
274 	if (!ofw_bus_status_okay(dev))
275 		return (ENXIO);
276 
277 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
278 		return (ENXIO);
279 
280 	device_set_desc(dev, "AS3722 PMIC");
281 	return (BUS_PROBE_DEFAULT);
282 }
283 
284 static int
285 as3722_attach(device_t dev)
286 {
287 	struct as3722_softc *sc;
288 	const char *dname;
289 	int dunit, rv, rid;
290 	phandle_t node;
291 
292 	sc = device_get_softc(dev);
293 	sc->dev = dev;
294 	sc->bus_addr = iicbus_get_addr(dev);
295 	node = ofw_bus_get_node(sc->dev);
296 	dname = device_get_name(dev);
297 	dunit = device_get_unit(dev);
298 	rv = 0;
299 	LOCK_INIT(sc);
300 
301 	rid = 0;
302 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
303 	    RF_ACTIVE);
304 	if (sc->irq_res == NULL) {
305 		device_printf(dev, "Cannot allocate interrupt.\n");
306 		rv = ENXIO;
307 		goto fail;
308 	}
309 
310 	rv = as3722_parse_fdt(sc, node);
311 	if (rv != 0)
312 		goto fail;
313 	rv = as3722_get_version(sc);
314 	if (rv != 0)
315 		goto fail;
316 	rv = as3722_init(sc);
317 	if (rv != 0)
318 		goto fail;
319 	rv = as3722_regulator_attach(sc, node);
320 	if (rv != 0)
321 		goto fail;
322 	rv = as3722_gpio_attach(sc, node);
323 	if (rv != 0)
324 		goto fail;
325 	rv = as3722_rtc_attach(sc, node);
326 	if (rv != 0)
327 		goto fail;
328 
329 	fdt_pinctrl_register(dev, NULL);
330 	fdt_pinctrl_configure_by_name(dev, "default");
331 
332 	/* Setup  interrupt. */
333 	rv = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
334 	    NULL, as3722_intr, sc, &sc->irq_h);
335 	if (rv) {
336 		device_printf(dev, "Cannot setup interrupt.\n");
337 		goto fail;
338 	}
339 	return (bus_generic_attach(dev));
340 
341 fail:
342 	if (sc->irq_h != NULL)
343 		bus_teardown_intr(dev, sc->irq_res, sc->irq_h);
344 	if (sc->irq_res != NULL)
345 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
346 	LOCK_DESTROY(sc);
347 	return (rv);
348 }
349 
350 static int
351 as3722_detach(device_t dev)
352 {
353 	struct as3722_softc *sc;
354 
355 	sc = device_get_softc(dev);
356 	if (sc->irq_h != NULL)
357 		bus_teardown_intr(dev, sc->irq_res, sc->irq_h);
358 	if (sc->irq_res != NULL)
359 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
360 	LOCK_DESTROY(sc);
361 
362 	return (bus_generic_detach(dev));
363 }
364 
365 static phandle_t
366 as3722_gpio_get_node(device_t bus, device_t dev)
367 {
368 
369 	/* We only have one child, the GPIO bus, which needs our own node. */
370 	return (ofw_bus_get_node(bus));
371 }
372 
373 static device_method_t as3722_methods[] = {
374 	/* Device interface */
375 	DEVMETHOD(device_probe,		as3722_probe),
376 	DEVMETHOD(device_attach,	as3722_attach),
377 	DEVMETHOD(device_detach,	as3722_detach),
378 
379 	/* Regdev interface */
380 	DEVMETHOD(regdev_map,		as3722_regulator_map),
381 
382 	/* RTC interface */
383 	DEVMETHOD(clock_gettime,	as3722_rtc_gettime),
384 	DEVMETHOD(clock_settime,	as3722_rtc_settime),
385 
386 	/* GPIO protocol interface */
387 	DEVMETHOD(gpio_get_bus,		as3722_gpio_get_bus),
388 	DEVMETHOD(gpio_pin_max,		as3722_gpio_pin_max),
389 	DEVMETHOD(gpio_pin_getname,	as3722_gpio_pin_getname),
390 	DEVMETHOD(gpio_pin_getflags,	as3722_gpio_pin_getflags),
391 	DEVMETHOD(gpio_pin_getcaps,	as3722_gpio_pin_getcaps),
392 	DEVMETHOD(gpio_pin_setflags,	as3722_gpio_pin_setflags),
393 	DEVMETHOD(gpio_pin_get,		as3722_gpio_pin_get),
394 	DEVMETHOD(gpio_pin_set,		as3722_gpio_pin_set),
395 	DEVMETHOD(gpio_pin_toggle,	as3722_gpio_pin_toggle),
396 	DEVMETHOD(gpio_map_gpios,	as3722_gpio_map_gpios),
397 
398 	/* fdt_pinctrl interface */
399 	DEVMETHOD(fdt_pinctrl_configure, as3722_pinmux_configure),
400 
401 	/* ofw_bus interface */
402 	DEVMETHOD(ofw_bus_get_node,	as3722_gpio_get_node),
403 
404 	DEVMETHOD_END
405 };
406 
407 static devclass_t as3722_devclass;
408 static DEFINE_CLASS_0(gpio, as3722_driver, as3722_methods,
409     sizeof(struct as3722_softc));
410 EARLY_DRIVER_MODULE(as3722, iicbus, as3722_driver, as3722_devclass,
411     NULL, NULL, 74);
412