1 /* $NetBSD: nxpiic_acpi.c,v 1.7 2025/09/15 15:31:45 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2021 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jared McNeill <jmcneill (at) invisible.ca>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: nxpiic_acpi.c,v 1.7 2025/09/15 15:31:45 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/cpu.h> 38 #include <sys/device.h> 39 40 #include <dev/acpi/acpireg.h> 41 #include <dev/acpi/acpivar.h> 42 #include <dev/acpi/acpi_intr.h> 43 44 #include <dev/i2c/motoi2cvar.h> 45 #include <dev/i2c/motoi2creg.h> 46 47 #define NXPIIC_SPEED_STD 100000 48 49 static const struct clk_div { 50 int scl_div; 51 uint8_t ibc; 52 } nxpiic_clk_div[] = { 53 { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 }, 54 { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 }, 55 { 36, 0x0a }, { 40, 0x07 }, { 44, 0x0c }, { 48, 0x0d }, 56 { 52, 0x43 }, { 56, 0x0e }, { 60, 0x45 }, { 64, 0x12 }, 57 { 68, 0x0f }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 }, 58 { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1a }, { 128, 0x17 }, 59 { 136, 0x4f }, { 144, 0x1c }, { 160, 0x1d }, { 176, 0x55 }, 60 { 192, 0x1e }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 }, 61 { 240, 0x1f }, { 256, 0x23 }, { 288, 0x5c }, { 320, 0x25 }, 62 { 384, 0x26 }, { 448, 0x2a }, { 480, 0x27 }, { 512, 0x2b }, 63 { 576, 0x2c }, { 640, 0x2d }, { 768, 0x31 }, { 896, 0x32 }, 64 { 960, 0x2f }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 }, 65 { 1536, 0x36 }, { 1792, 0x3a }, { 1920, 0x37 }, { 2048, 0x3b }, 66 { 2304, 0x3c }, { 2560, 0x3d }, { 3072, 0x3e }, { 3584, 0x7a }, 67 { 3840, 0x3f }, { 4096, 0x7B }, { 5120, 0x7d }, { 6144, 0x7e }, 68 }; 69 70 static int nxpiic_acpi_match(device_t, cfdata_t, void *); 71 static void nxpiic_acpi_attach(device_t, device_t, void *); 72 73 static uint8_t nxpiic_acpi_iord(struct motoi2c_softc *, bus_size_t); 74 static void nxpiic_acpi_iowr(struct motoi2c_softc *, bus_size_t, uint8_t); 75 76 CFATTACH_DECL_NEW(nxpiic_acpi, sizeof(struct motoi2c_softc), 77 nxpiic_acpi_match, nxpiic_acpi_attach, NULL, NULL); 78 79 static const struct device_compatible_entry compat_data[] = { 80 { .compat = "NXP0001" }, 81 DEVICE_COMPAT_EOL 82 }; 83 84 static int 85 nxpiic_acpi_match(device_t parent, cfdata_t cf, void *aux) 86 { 87 struct acpi_attach_args *aa = aux; 88 89 return acpi_compatible_match(aa, compat_data); 90 } 91 92 static void 93 nxpiic_acpi_attach(device_t parent, device_t self, void *aux) 94 { 95 struct motoi2c_softc * const sc = device_private(self); 96 struct motoi2c_settings settings; 97 struct acpi_attach_args *aa = aux; 98 struct acpi_resources res; 99 struct acpi_mem *mem; 100 ACPI_INTEGER clock_freq; 101 ACPI_STATUS rv; 102 int error, n; 103 104 sc->sc_dev = self; 105 sc->sc_iot = aa->aa_memt; 106 107 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS", 108 &res, &acpi_resource_parse_ops_default); 109 if (ACPI_FAILURE(rv)) 110 return; 111 112 mem = acpi_res_mem(&res, 0); 113 if (mem == NULL) { 114 aprint_error_dev(self, "couldn't find mem resource\n"); 115 goto done; 116 } 117 118 rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency", 119 &clock_freq); 120 if (ACPI_FAILURE(rv) || clock_freq == 0) { 121 aprint_error_dev(self, "couldn't get clock frequency\n"); 122 goto done; 123 } 124 aprint_debug_dev(self, "bus clock %u Hz\n", (u_int)clock_freq); 125 126 error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, 127 &sc->sc_ioh); 128 if (error) { 129 aprint_error_dev(self, "couldn't map registers\n"); 130 return; 131 } 132 133 settings.i2c_adr = MOTOI2C_ADR_DEFAULT; 134 settings.i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT; 135 for (n = 0; n < __arraycount(nxpiic_clk_div) - 1; n++) { 136 if (clock_freq / nxpiic_clk_div[n].scl_div < NXPIIC_SPEED_STD) 137 break; 138 } 139 settings.i2c_fdr = nxpiic_clk_div[n].ibc; 140 141 sc->sc_flags |= MOTOI2C_F_ENABLE_INV | MOTOI2C_F_STATUS_W1C; 142 sc->sc_iord = nxpiic_acpi_iord; 143 sc->sc_iowr = nxpiic_acpi_iowr; 144 145 motoi2c_attach(sc, &settings); 146 147 done: 148 acpi_resource_cleanup(&res); 149 150 } 151 152 static uint8_t 153 nxpiic_acpi_iord(struct motoi2c_softc *msc, bus_size_t off) 154 { 155 KASSERT((off & 3) == 0); 156 157 if (off >= I2CDFSRR) 158 return 0; 159 160 return bus_space_read_1(msc->sc_iot, msc->sc_ioh, off >> 2); 161 } 162 163 static void 164 nxpiic_acpi_iowr(struct motoi2c_softc *msc, bus_size_t off, uint8_t val) 165 { 166 KASSERT((off & 3) == 0); 167 168 if (off >= I2CDFSRR) 169 return; 170 171 bus_space_write_1(msc->sc_iot, msc->sc_ioh, off >> 2, val); 172 } 173