1 /* $NetBSD: pcfiic_fdt.c,v 1.1 2026/07/12 04:23:59 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2025 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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: pcfiic_fdt.c,v 1.1 2026/07/12 04:23:59 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/device.h> 38 #include <sys/kernel.h> 39 40 #include <dev/fdt/fdtvar.h> 41 42 #include <dev/ofw/openfirm.h> 43 44 #include <dev/i2c/i2cvar.h> 45 46 #include <dev/ic/pcf8584var.h> 47 #include <dev/ic/pcf8584reg.h> 48 49 static const struct device_compatible_entry compat_data[] = { 50 { .compat = "nxp,pcf8584" }, 51 DEVICE_COMPAT_EOL 52 }; 53 54 struct pcfiic_fdt_softc { 55 struct pcfiic_softc sc_pcfdev; 56 57 int sc_phandle; 58 void *sc_ih; 59 }; 60 61 static int 62 pcfiic_fdt_match(device_t parent, cfdata_t cf, void *aux) 63 { 64 struct fdt_attach_args * const faa = aux; 65 66 return of_compatible_match(faa->faa_phandle, compat_data); 67 } 68 69 static void 70 pcfiic_fdt_attach(device_t parent, device_t self, void *aux) 71 { 72 struct pcfiic_fdt_softc *sc = device_private(self); 73 struct fdt_attach_args * const faa = aux; 74 const int phandle = faa->faa_phandle; 75 char intrstr[128]; 76 struct clk *clk; 77 bus_addr_t addr; 78 bus_size_t size; 79 uint32_t frequency = 0; 80 uint32_t own_addr; 81 int error; 82 83 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 84 aprint_error(": couldn't get registers\n"); 85 return; 86 } 87 88 sc->sc_phandle = phandle; 89 sc->sc_pcfdev.sc_iot = faa->faa_bst; 90 error = bus_space_map(faa->faa_bst, addr, size, 0, 91 &sc->sc_pcfdev.sc_ioh); 92 if (error) { 93 aprint_error(": couldn't map registers (error=%d)\n", 94 error); 95 return; 96 } 97 98 sc->sc_pcfdev.sc_dev = self; 99 if (of_getprop_uint32(phandle, "clock-frequency", &frequency)) { 100 clk = fdtbus_clock_get_index(phandle, 0); 101 if (clk != NULL) { 102 frequency = clk_get_rate(clk); 103 } 104 } 105 if (frequency == 0) { 106 aprint_error(": couldn't get frequency\n"); 107 return; 108 } 109 110 /* 111 * Compute the clock configuration values based on the external 112 * clock frequency. In general, we want to run at full speed 113 * (90KHz, just shy of the standard I2C full-speed 100KHz). 114 * 115 * However, the speeds that we can configure (90KHz, 45KHz, 116 * 11KHz, and 1.5KHz, respectively) can only be achieved if 117 * we get an exact match on the external clock frequency (and 118 * can thus configure the prescalar properly). Those frequencies 119 * are 3MHz, 4.43MHz, 6MHz, 8MHz, and 12MHz. 120 * 121 * XXX We could be a little more sophisticated here... like what 122 * if the external clock is 6.25MHz? 123 */ 124 uint8_t scl_val = PCF8584_SCL_90; 125 uint8_t clk_val; 126 127 if (frequency <= 3000000) { 128 clk_val = PCF8584_CLK_3; 129 } else if (frequency <= 4430000) { 130 clk_val = PCF8584_CLK_4_43; 131 } else if (frequency <= 6000000) { 132 clk_val = PCF8584_CLK_6; 133 } else if (frequency <= 8000000) { 134 clk_val = PCF8584_CLK_8; 135 } else if (frequency <= 12000000) { 136 clk_val = PCF8584_CLK_12; 137 } else { 138 aprint_error(": frequency %u out of range\n", frequency); 139 return; 140 } 141 142 if (of_getprop_uint32(phandle, "own-address", &own_addr)) { 143 own_addr = 0xaa; 144 } else if (own_addr == 0x00 || own_addr > 0xff) { 145 aprint_error(": invalid own-address property\n"); 146 return; 147 } 148 149 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 150 sc->sc_pcfdev.sc_poll = 1; 151 } 152 aprint_normal("\n"); 153 154 uint32_t reg_shift; 155 if (of_getprop_uint32(phandle, "reg-shift", ®_shift) < 0) { 156 reg_shift = 0; 157 } 158 159 sc->sc_pcfdev.sc_regmap[PCF8584_S0] = PCF8584_S0 << reg_shift; 160 sc->sc_pcfdev.sc_regmap[PCF8584_S1] = PCF8584_S1 << reg_shift; 161 162 if (!sc->sc_pcfdev.sc_poll) { 163 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, 164 IPL_SERIAL, 0, pcfiic_intr, &sc->sc_pcfdev, 165 device_xname(self)); 166 if (sc->sc_ih == NULL) { 167 aprint_error_dev(self, 168 "failed to establish interrupt at %s\n", intrstr); 169 sc->sc_pcfdev.sc_poll = 1; 170 } else { 171 aprint_normal_dev(self, 172 "interrupting at %s\n", intrstr); 173 } 174 } 175 176 pcfiic_attach(&sc->sc_pcfdev, 177 (i2c_addr_t)(own_addr >> 1), scl_val | clk_val); 178 } 179 180 CFATTACH_DECL_NEW(pcfiic_fdt, sizeof(struct pcfiic_fdt_softc), 181 pcfiic_fdt_match, pcfiic_fdt_attach, NULL, NULL); 182