sunxi_twi.c revision 1.3
1/* $NetBSD: sunxi_twi.c,v 1.3 2017/08/25 00:07:03 jmcneill 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
31__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.3 2017/08/25 00:07:03 jmcneill Exp $");
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/device.h>
36#include <sys/intr.h>
37#include <sys/systm.h>
38#include <sys/time.h>
39
40#include <dev/i2c/i2cvar.h>
41#include <dev/i2c/gttwsivar.h>
42
43#include <dev/fdt/fdtvar.h>
44
45static int sunxi_twi_match(device_t, cfdata_t, void *);
46static void sunxi_twi_attach(device_t, device_t, void *);
47
48static const char * const compatible[] = {
49	"allwinner,sun4i-a10-i2c",
50	"allwinner,sun6i-a31-i2c",
51	NULL
52};
53
54CFATTACH_DECL_NEW(sunxi_twi, sizeof(struct gttwsi_softc),
55	sunxi_twi_match, sunxi_twi_attach, NULL, NULL);
56
57static i2c_tag_t
58sunxi_twi_get_tag(device_t dev)
59{
60	struct gttwsi_softc * const sc = device_private(dev);
61
62	return &sc->sc_i2c;
63}
64
65const struct fdtbus_i2c_controller_func sunxi_twi_funcs = {
66	.get_tag = sunxi_twi_get_tag,
67};
68
69static int
70sunxi_twi_match(device_t parent, cfdata_t cf, void *aux)
71{
72	struct fdt_attach_args * const faa = aux;
73
74	return of_match_compatible(faa->faa_phandle, compatible);
75}
76
77static void
78sunxi_twi_attach(device_t parent, device_t self, void *aux)
79{
80	struct gttwsi_softc * const sc = device_private(self);
81	struct fdt_attach_args * const faa = aux;
82	struct i2cbus_attach_args iba;
83	const int phandle = faa->faa_phandle;
84	bus_space_tag_t bst = faa->faa_bst;
85	bus_space_handle_t bsh;
86	prop_dictionary_t devs;
87	uint32_t address_cells;
88	struct fdtbus_reset *rst;
89	struct clk *clk;
90	char intrstr[128];
91	bus_addr_t addr;
92	bus_size_t size;
93	void *ih;
94
95	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
96		aprint_error(": couldn't get registers\n");
97		return;
98	}
99
100	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
101		aprint_error(": couldn't map registers\n");
102		return;
103	}
104
105	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
106		aprint_error(": failed to decode interrupt\n");
107		return;
108	}
109
110	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
111		if (clk_enable(clk) != 0) {
112			aprint_error(": couldn't enable clock\n");
113			return;
114		}
115	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
116		if (fdtbus_reset_deassert(rst) != 0) {
117			aprint_error(": couldn't de-assert reset\n");
118			return;
119		}
120
121	gttwsi_attach_subr(self, bst, bsh);
122
123	ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0, gttwsi_intr, sc);
124	if (ih == NULL) {
125		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
126		    intrstr);
127		return;
128	}
129	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
130
131	fdtbus_register_i2c_controller(self, phandle, &sunxi_twi_funcs);
132
133	devs = prop_dictionary_create();
134	if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
135		address_cells = 1;
136
137	of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
138
139	memset(&iba, 0, sizeof(iba));
140	iba.iba_tag = &sc->sc_i2c;
141	iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
142	if (iba.iba_child_devices)
143		prop_object_retain(iba.iba_child_devices);
144	else
145		iba.iba_child_devices = prop_array_create();
146	prop_object_release(devs);
147
148	config_found_ia(self, "i2cbus", &iba, iicbus_print);
149}
150