1 1.11 thorpej /* $NetBSD: ac100.c,v 1.11 2025/09/17 13:49:13 thorpej Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.3 jmcneill #include "opt_fdt.h" 30 1.3 jmcneill 31 1.1 jmcneill #include <sys/cdefs.h> 32 1.11 thorpej __KERNEL_RCSID(0, "$NetBSD: ac100.c,v 1.11 2025/09/17 13:49:13 thorpej Exp $"); 33 1.1 jmcneill 34 1.1 jmcneill #include <sys/param.h> 35 1.1 jmcneill #include <sys/systm.h> 36 1.1 jmcneill #include <sys/kernel.h> 37 1.1 jmcneill #include <sys/device.h> 38 1.1 jmcneill #include <sys/conf.h> 39 1.1 jmcneill #include <sys/bus.h> 40 1.1 jmcneill #include <sys/kmem.h> 41 1.1 jmcneill 42 1.1 jmcneill #include <dev/clock_subr.h> 43 1.1 jmcneill 44 1.1 jmcneill #include <dev/i2c/i2cvar.h> 45 1.1 jmcneill 46 1.3 jmcneill #ifdef FDT 47 1.3 jmcneill #include <dev/fdt/fdtvar.h> 48 1.3 jmcneill #endif 49 1.3 jmcneill 50 1.1 jmcneill #define AC100_CHIP_AUDIO_RST_REG 0x00 51 1.1 jmcneill #define AC100_PLL_CTRL1_REG 0x02 52 1.1 jmcneill #define AC100_PLL_CTRL2_REG 0x03 53 1.1 jmcneill #define AC100_SYSCLK_CTRL_REG 0x04 54 1.1 jmcneill #define AC100_MOD_RST_CTRL_REG 0x05 55 1.1 jmcneill #define AC100_ADDA_SR_CTRL_REG 0x06 56 1.1 jmcneill #define AC100_I2S1_LCK_CTRL_REG 0x10 57 1.1 jmcneill #define AC100_I2S1_SDIN_CTRL_REG 0x11 58 1.1 jmcneill #define AC100_I2S1_SDOUT_CTRL_REG 0x12 59 1.1 jmcneill #define AC100_I2S1_DIG_MIXER_REG 0x13 60 1.1 jmcneill #define AC100_I2S1_VOL_CTRL1_REG 0x14 61 1.1 jmcneill #define AC100_I2S1_VOL_CTRL2_REG 0x15 62 1.1 jmcneill #define AC100_I2S1_VOL_CTRL3_REG 0x16 63 1.1 jmcneill #define AC100_I2S1_VOL_CTRL4_REG 0x17 64 1.1 jmcneill #define AC100_I2S1_MXR_GAIN_REG 0x18 65 1.1 jmcneill #define AC100_I2S2_CLK_CTRL_REG 0x20 66 1.1 jmcneill #define AC100_I2S2_SDIN_CTRL_REG 0x21 67 1.1 jmcneill #define AC100_I2S2_SDOUT_CTRL_REG 0x22 68 1.1 jmcneill #define AC100_I2S2_DIG_MIXER_REG 0x23 69 1.1 jmcneill #define AC100_I2S2_VOL_CTRL1_REG 0x24 70 1.1 jmcneill #define AC100_I2S2_VOL_CTRL2_REG 0x26 71 1.1 jmcneill #define AC100_I2S2_MXR_GAIN_REG 0x28 72 1.1 jmcneill #define AC100_I2S3_CLK_CTRL_REG 0x30 73 1.1 jmcneill #define AC100_I2S3_SDIN_CTRL_REG 0x31 74 1.1 jmcneill #define AC100_I2S3_SDOUT_CTRL_REG 0x32 75 1.1 jmcneill #define AC100_I2S3_SGP_CTRL_REG 0x33 76 1.1 jmcneill #define AC100_ADC_DIG_CTRL_REG 0x40 77 1.1 jmcneill 78 1.1 jmcneill #define AC100_RTC_RESET_REG 0xc6 79 1.1 jmcneill #define AC100_RTC_CTRL_REG 0xc7 80 1.1 jmcneill #define AC100_RTC_SEC_REG 0xc8 81 1.1 jmcneill #define AC100_RTC_MIN_REG 0xc9 82 1.1 jmcneill #define AC100_RTC_HOU_REG 0xca 83 1.1 jmcneill #define AC100_RTC_WEE_REG 0xcb 84 1.1 jmcneill #define AC100_RTC_DAY_REG 0xcc 85 1.1 jmcneill #define AC100_RTC_MON_REG 0xcd 86 1.1 jmcneill #define AC100_RTC_YEA_REG 0xce 87 1.1 jmcneill #define AC100_RTC_UPD_TRIG_REG 0xcf 88 1.1 jmcneill 89 1.1 jmcneill #define AC100_RTC_GP_REG(n) (0xe0 + (n)) 90 1.1 jmcneill 91 1.1 jmcneill #define AC100_RTC_CTRL_12H_24H_MODE __BIT(0) 92 1.1 jmcneill 93 1.1 jmcneill #define AC100_RTC_UPD_TRIG_WRITE __BIT(15) 94 1.1 jmcneill 95 1.3 jmcneill static const struct device_compatible_entry compat_data[] = { 96 1.5 thorpej { .compat = "x-powers,ac100" }, 97 1.7 thorpej DEVICE_COMPAT_EOL 98 1.3 jmcneill }; 99 1.3 jmcneill 100 1.1 jmcneill struct ac100_softc { 101 1.1 jmcneill device_t sc_dev; 102 1.1 jmcneill i2c_tag_t sc_i2c; 103 1.1 jmcneill i2c_addr_t sc_addr; 104 1.1 jmcneill }; 105 1.1 jmcneill 106 1.1 jmcneill static int ac100_match(device_t, cfdata_t, void *); 107 1.1 jmcneill static void ac100_attach(device_t, device_t, void *); 108 1.1 jmcneill 109 1.1 jmcneill static int ac100_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *); 110 1.1 jmcneill static int ac100_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *); 111 1.1 jmcneill 112 1.1 jmcneill static int ac100_read(struct ac100_softc *, uint8_t, uint16_t *); 113 1.1 jmcneill static int ac100_write(struct ac100_softc *, uint8_t, uint16_t); 114 1.1 jmcneill 115 1.1 jmcneill CFATTACH_DECL_NEW(ac100ic, sizeof(struct ac100_softc), 116 1.1 jmcneill ac100_match, ac100_attach, NULL, NULL); 117 1.1 jmcneill 118 1.8 thorpej struct ac100rtc_softc { 119 1.8 thorpej device_t sc_dev; 120 1.8 thorpej 121 1.8 thorpej struct todr_chip_handle sc_todr; 122 1.8 thorpej }; 123 1.8 thorpej 124 1.8 thorpej static int ac100rtc_match(device_t, cfdata_t, void *); 125 1.8 thorpej static void ac100rtc_attach(device_t, device_t, void *); 126 1.8 thorpej 127 1.8 thorpej CFATTACH_DECL_NEW(ac100rtc, sizeof(struct ac100rtc_softc), 128 1.8 thorpej ac100rtc_match, ac100rtc_attach, NULL, NULL); 129 1.8 thorpej 130 1.1 jmcneill static int 131 1.1 jmcneill ac100_match(device_t parent, cfdata_t match, void *aux) 132 1.1 jmcneill { 133 1.3 jmcneill struct i2c_attach_args *ia = aux; 134 1.3 jmcneill int match_result; 135 1.3 jmcneill 136 1.3 jmcneill if (iic_use_direct_match(ia, match, compat_data, &match_result)) 137 1.3 jmcneill return match_result; 138 1.3 jmcneill 139 1.3 jmcneill /* This device is direct-config only. */ 140 1.3 jmcneill 141 1.3 jmcneill return 0; 142 1.1 jmcneill } 143 1.1 jmcneill 144 1.1 jmcneill static void 145 1.1 jmcneill ac100_attach(device_t parent, device_t self, void *aux) 146 1.1 jmcneill { 147 1.1 jmcneill struct ac100_softc *sc = device_private(self); 148 1.1 jmcneill struct i2c_attach_args *ia = aux; 149 1.1 jmcneill 150 1.1 jmcneill sc->sc_dev = self; 151 1.1 jmcneill sc->sc_i2c = ia->ia_tag; 152 1.1 jmcneill sc->sc_addr = ia->ia_addr; 153 1.1 jmcneill 154 1.1 jmcneill aprint_naive("\n"); 155 1.1 jmcneill aprint_normal(": CODEC/RTC\n"); 156 1.1 jmcneill 157 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, 0); 158 1.1 jmcneill ac100_write(sc, AC100_RTC_CTRL_REG, AC100_RTC_CTRL_12H_24H_MODE); 159 1.1 jmcneill iic_release_bus(sc->sc_i2c, 0); 160 1.1 jmcneill 161 1.8 thorpej #ifdef FDT 162 1.11 thorpej if (devhandle_type(device_handle(self)) == DEVHANDLE_TYPE_OF) { 163 1.11 thorpej int phandle = devhandle_to_of(device_handle(self)); 164 1.11 thorpej fdt_add_bus(self, phandle, NULL); 165 1.11 thorpej } 166 1.8 thorpej #endif 167 1.8 thorpej } 168 1.8 thorpej 169 1.8 thorpej #ifdef FDT 170 1.8 thorpej static const struct device_compatible_entry rtc_compat_data[] = { 171 1.8 thorpej { .compat = "x-powers,ac100-rtc" }, 172 1.8 thorpej DEVICE_COMPAT_EOL 173 1.8 thorpej }; 174 1.8 thorpej 175 1.8 thorpej static int 176 1.8 thorpej ac100rtc_match(device_t parent, cfdata_t match, void *aux) 177 1.8 thorpej { 178 1.8 thorpej struct fdt_attach_args * const faa = aux; 179 1.8 thorpej 180 1.8 thorpej return of_compatible_match(faa->faa_phandle, rtc_compat_data); 181 1.8 thorpej } 182 1.8 thorpej 183 1.8 thorpej static void 184 1.8 thorpej ac100rtc_attach(device_t parent, device_t self, void *aux) 185 1.8 thorpej { 186 1.8 thorpej struct ac100rtc_softc *sc = device_private(self); 187 1.8 thorpej 188 1.8 thorpej sc->sc_dev = self; 189 1.8 thorpej aprint_naive("\n"); 190 1.8 thorpej aprint_normal("\n"); 191 1.8 thorpej 192 1.1 jmcneill sc->sc_todr.todr_gettime_ymdhms = ac100_rtc_gettime; 193 1.1 jmcneill sc->sc_todr.todr_settime_ymdhms = ac100_rtc_settime; 194 1.9 thorpej sc->sc_todr.todr_dev = self; 195 1.3 jmcneill 196 1.10 thorpej todr_attach(&sc->sc_todr); 197 1.1 jmcneill } 198 1.8 thorpej #endif /* FDT */ 199 1.1 jmcneill 200 1.1 jmcneill static int 201 1.1 jmcneill ac100_read(struct ac100_softc *sc, uint8_t reg, uint16_t *val) 202 1.1 jmcneill { 203 1.4 thorpej return iic_smbus_read_word(sc->sc_i2c, sc->sc_addr, reg, val, 0); 204 1.1 jmcneill } 205 1.1 jmcneill 206 1.1 jmcneill static int 207 1.1 jmcneill ac100_write(struct ac100_softc *sc, uint8_t reg, uint16_t val) 208 1.1 jmcneill { 209 1.4 thorpej return iic_smbus_write_word(sc->sc_i2c, sc->sc_addr, reg, val, 0); 210 1.1 jmcneill } 211 1.1 jmcneill 212 1.1 jmcneill static int 213 1.1 jmcneill ac100_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt) 214 1.1 jmcneill { 215 1.9 thorpej struct ac100_softc *sc = device_private(device_parent(tch->todr_dev)); 216 1.1 jmcneill uint16_t sec, min, hou, wee, day, mon, yea; 217 1.1 jmcneill 218 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, 0); 219 1.1 jmcneill ac100_read(sc, AC100_RTC_SEC_REG, &sec); 220 1.1 jmcneill ac100_read(sc, AC100_RTC_MIN_REG, &min); 221 1.1 jmcneill ac100_read(sc, AC100_RTC_HOU_REG, &hou); 222 1.1 jmcneill ac100_read(sc, AC100_RTC_WEE_REG, &wee); 223 1.1 jmcneill ac100_read(sc, AC100_RTC_DAY_REG, &day); 224 1.1 jmcneill ac100_read(sc, AC100_RTC_MON_REG, &mon); 225 1.1 jmcneill ac100_read(sc, AC100_RTC_YEA_REG, &yea); 226 1.1 jmcneill iic_release_bus(sc->sc_i2c, 0); 227 1.1 jmcneill 228 1.1 jmcneill dt->dt_year = POSIX_BASE_YEAR + bcdtobin(yea & 0xff); 229 1.1 jmcneill dt->dt_mon = bcdtobin(mon & 0x1f); 230 1.1 jmcneill dt->dt_day = bcdtobin(day & 0x3f); 231 1.1 jmcneill dt->dt_wday = bcdtobin(wee & 0x7); 232 1.1 jmcneill dt->dt_hour = bcdtobin(hou & 0x3f); 233 1.1 jmcneill dt->dt_min = bcdtobin(min & 0x7f); 234 1.1 jmcneill dt->dt_sec = bcdtobin(sec & 0x7f); 235 1.1 jmcneill 236 1.1 jmcneill return 0; 237 1.1 jmcneill } 238 1.1 jmcneill 239 1.1 jmcneill static int 240 1.1 jmcneill ac100_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt) 241 1.1 jmcneill { 242 1.9 thorpej struct ac100_softc *sc = device_private(device_parent(tch->todr_dev)); 243 1.1 jmcneill 244 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, 0); 245 1.1 jmcneill ac100_write(sc, AC100_RTC_SEC_REG, bintobcd(dt->dt_sec) & 0x7f); 246 1.1 jmcneill ac100_write(sc, AC100_RTC_MIN_REG, bintobcd(dt->dt_min) & 0x7f); 247 1.1 jmcneill ac100_write(sc, AC100_RTC_HOU_REG, bintobcd(dt->dt_hour) & 0x3f); 248 1.1 jmcneill ac100_write(sc, AC100_RTC_WEE_REG, bintobcd(dt->dt_wday) & 0x7); 249 1.1 jmcneill ac100_write(sc, AC100_RTC_DAY_REG, bintobcd(dt->dt_day) & 0x3f); 250 1.1 jmcneill ac100_write(sc, AC100_RTC_MON_REG, bintobcd(dt->dt_mon) & 0x1f); 251 1.1 jmcneill ac100_write(sc, AC100_RTC_YEA_REG, 252 1.1 jmcneill bintobcd(dt->dt_year - POSIX_BASE_YEAR) & 0xff); 253 1.1 jmcneill ac100_write(sc, AC100_RTC_UPD_TRIG_REG, AC100_RTC_UPD_TRIG_WRITE); 254 1.1 jmcneill iic_release_bus(sc->sc_i2c, 0); 255 1.1 jmcneill 256 1.1 jmcneill return 0; 257 1.1 jmcneill } 258