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