1 1.7 thorpej /* $NetBSD: sunxi_pwm.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2018 Jared 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.1 jmcneill #include <sys/cdefs.h> 30 1.1 jmcneill 31 1.7 thorpej __KERNEL_RCSID(1, "$NetBSD: sunxi_pwm.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $"); 32 1.1 jmcneill 33 1.1 jmcneill #include <sys/param.h> 34 1.1 jmcneill #include <sys/bus.h> 35 1.1 jmcneill #include <sys/device.h> 36 1.1 jmcneill #include <sys/intr.h> 37 1.1 jmcneill #include <sys/systm.h> 38 1.1 jmcneill #include <sys/time.h> 39 1.1 jmcneill 40 1.1 jmcneill #include <dev/pwm/pwmvar.h> 41 1.1 jmcneill 42 1.1 jmcneill #include <dev/fdt/fdtvar.h> 43 1.1 jmcneill 44 1.1 jmcneill #define PWM_CH_CTRL 0x00 45 1.1 jmcneill #define PWM0_RDY __BIT(28) 46 1.1 jmcneill #define PWM0_BYPASS __BIT(9) 47 1.1 jmcneill #define PWM_CH0_PUL_START __BIT(8) 48 1.1 jmcneill #define PWM_CHANNEL0_MODE __BIT(7) 49 1.1 jmcneill #define SCLK_CH0_GATING __BIT(6) 50 1.1 jmcneill #define PWM_CH0_ACT_STA __BIT(5) 51 1.1 jmcneill #define PWM_CH0_EN __BIT(4) 52 1.1 jmcneill #define PWM_CH0_PRESCAL __BITS(3,0) 53 1.1 jmcneill #define PWM_CH0_PERIOD 0x04 54 1.1 jmcneill #define PWM_CH0_ENTIRE_CYS __BITS(31,16) 55 1.1 jmcneill #define PWM_CH0_ENTIRE_ACT_CYS __BITS(15,0) 56 1.1 jmcneill 57 1.1 jmcneill enum sunxi_pwm_type { 58 1.1 jmcneill PWM_A64 = 1, 59 1.1 jmcneill }; 60 1.1 jmcneill 61 1.4 thorpej static const struct device_compatible_entry compat_data[] = { 62 1.4 thorpej { .compat = "allwinner,sun50i-a64-pwm", .value = PWM_A64 }, 63 1.6 thorpej DEVICE_COMPAT_EOL 64 1.1 jmcneill }; 65 1.1 jmcneill 66 1.1 jmcneill struct sunxi_pwm_softc { 67 1.1 jmcneill device_t sc_dev; 68 1.1 jmcneill bus_space_tag_t sc_bst; 69 1.1 jmcneill bus_space_handle_t sc_bsh; 70 1.1 jmcneill 71 1.1 jmcneill struct pwm_controller sc_pwm; 72 1.1 jmcneill struct pwm_config sc_conf; 73 1.1 jmcneill 74 1.1 jmcneill u_int sc_clkfreq; 75 1.1 jmcneill }; 76 1.1 jmcneill 77 1.1 jmcneill #define PWM_READ(sc, reg) \ 78 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 79 1.1 jmcneill #define PWM_WRITE(sc, reg, val) \ 80 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 81 1.1 jmcneill 82 1.1 jmcneill static pwm_tag_t 83 1.1 jmcneill sunxi_pwm_get_tag(device_t dev, const void *data, size_t len) 84 1.1 jmcneill { 85 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(dev); 86 1.1 jmcneill const u_int *pwm = data; 87 1.1 jmcneill 88 1.1 jmcneill if (len != 16) 89 1.1 jmcneill return NULL; 90 1.1 jmcneill 91 1.1 jmcneill const u_int index = be32toh(pwm[1]); 92 1.1 jmcneill if (index != 0) 93 1.1 jmcneill return NULL; 94 1.1 jmcneill 95 1.1 jmcneill const u_int period = be32toh(pwm[2]); 96 1.1 jmcneill const u_int polarity = be32toh(pwm[3]); 97 1.1 jmcneill 98 1.1 jmcneill sc->sc_conf.period = period; 99 1.1 jmcneill sc->sc_conf.polarity = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH; 100 1.1 jmcneill 101 1.1 jmcneill return &sc->sc_pwm; 102 1.1 jmcneill } 103 1.1 jmcneill 104 1.1 jmcneill static struct fdtbus_pwm_controller_func sunxi_pwm_funcs = { 105 1.1 jmcneill .get_tag = sunxi_pwm_get_tag 106 1.1 jmcneill }; 107 1.1 jmcneill 108 1.1 jmcneill static int 109 1.1 jmcneill sunxi_pwm_enable(pwm_tag_t pwm, bool enable) 110 1.1 jmcneill { 111 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev); 112 1.1 jmcneill uint32_t ctrl, octrl; 113 1.1 jmcneill 114 1.1 jmcneill octrl = ctrl = PWM_READ(sc, PWM_CH_CTRL); 115 1.1 jmcneill if (enable) 116 1.1 jmcneill ctrl |= (PWM_CH0_EN | SCLK_CH0_GATING); 117 1.1 jmcneill else 118 1.1 jmcneill ctrl &= ~(PWM_CH0_EN | SCLK_CH0_GATING); 119 1.1 jmcneill 120 1.1 jmcneill if (ctrl != octrl) 121 1.1 jmcneill PWM_WRITE(sc, PWM_CH_CTRL, ctrl); 122 1.1 jmcneill 123 1.1 jmcneill return 0; 124 1.1 jmcneill } 125 1.1 jmcneill 126 1.1 jmcneill static int 127 1.1 jmcneill sunxi_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf) 128 1.1 jmcneill { 129 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev); 130 1.1 jmcneill uint32_t ctrl, ch_period; 131 1.1 jmcneill 132 1.1 jmcneill ctrl = PWM_READ(sc, PWM_CH_CTRL); 133 1.1 jmcneill ch_period = PWM_READ(sc, PWM_CH0_PERIOD); 134 1.1 jmcneill 135 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq / 120; 136 1.1 jmcneill const u_int cycles = __SHIFTOUT(ch_period, PWM_CH0_ENTIRE_CYS) + 1; 137 1.1 jmcneill const u_int act_cycles = __SHIFTOUT(ch_period, PWM_CH0_ENTIRE_ACT_CYS); 138 1.1 jmcneill 139 1.1 jmcneill conf->polarity = (ctrl & PWM_CH0_ACT_STA) ? PWM_ACTIVE_HIGH : PWM_ACTIVE_LOW; 140 1.1 jmcneill conf->period = (u_int)(((uint64_t)cycles * 1000000000) / rate); 141 1.1 jmcneill conf->duty_cycle = (u_int)(((uint64_t)act_cycles * 1000000000) / rate); 142 1.1 jmcneill 143 1.1 jmcneill return 0; 144 1.1 jmcneill } 145 1.1 jmcneill 146 1.1 jmcneill static int 147 1.1 jmcneill sunxi_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf) 148 1.1 jmcneill { 149 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev); 150 1.1 jmcneill uint32_t ctrl, ch_period; 151 1.1 jmcneill 152 1.1 jmcneill ctrl = PWM_READ(sc, PWM_CH_CTRL); 153 1.1 jmcneill if (ctrl & PWM0_RDY) 154 1.1 jmcneill return EBUSY; 155 1.1 jmcneill 156 1.1 jmcneill ctrl &= ~PWM0_BYPASS; /* Prescaler /120 = 200 kHz */ 157 1.1 jmcneill ctrl &= ~PWM_CH0_PRESCAL; 158 1.1 jmcneill ctrl |= __SHIFTIN(0, PWM_CH0_PRESCAL); 159 1.1 jmcneill 160 1.1 jmcneill ctrl &= ~PWM_CHANNEL0_MODE; /* Cycle mode */ 161 1.1 jmcneill if (conf->polarity == PWM_ACTIVE_HIGH) 162 1.1 jmcneill ctrl |= PWM_CH0_ACT_STA; 163 1.1 jmcneill else 164 1.1 jmcneill ctrl &= ~PWM_CH0_ACT_STA; 165 1.1 jmcneill 166 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq / 120; 167 1.1 jmcneill const u_int cycles = (u_int)((conf->period * rate) / 1000000000); 168 1.1 jmcneill const u_int act_cycles = (u_int)((conf->duty_cycle * rate) / 1000000000); 169 1.1 jmcneill 170 1.1 jmcneill ch_period = __SHIFTIN(cycles - 1, PWM_CH0_ENTIRE_CYS); 171 1.1 jmcneill ch_period |= __SHIFTIN(act_cycles, PWM_CH0_ENTIRE_ACT_CYS); 172 1.1 jmcneill 173 1.1 jmcneill PWM_WRITE(sc, PWM_CH0_PERIOD, ch_period); 174 1.1 jmcneill PWM_WRITE(sc, PWM_CH_CTRL, ctrl); 175 1.1 jmcneill 176 1.1 jmcneill sc->sc_conf = *conf; 177 1.1 jmcneill 178 1.1 jmcneill return 0; 179 1.1 jmcneill } 180 1.1 jmcneill 181 1.1 jmcneill static int 182 1.1 jmcneill sunxi_pwm_match(device_t parent, cfdata_t cf, void *aux) 183 1.1 jmcneill { 184 1.1 jmcneill struct fdt_attach_args * const faa = aux; 185 1.1 jmcneill 186 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 187 1.1 jmcneill } 188 1.1 jmcneill 189 1.1 jmcneill static void 190 1.1 jmcneill sunxi_pwm_attach(device_t parent, device_t self, void *aux) 191 1.1 jmcneill { 192 1.1 jmcneill struct sunxi_pwm_softc * const sc = device_private(self); 193 1.1 jmcneill struct fdt_attach_args * const faa = aux; 194 1.1 jmcneill const int phandle = faa->faa_phandle; 195 1.1 jmcneill struct clk *clk; 196 1.1 jmcneill bus_addr_t addr; 197 1.1 jmcneill bus_size_t size; 198 1.1 jmcneill int error; 199 1.1 jmcneill 200 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 201 1.1 jmcneill aprint_error(": couldn't get registers\n"); 202 1.1 jmcneill return; 203 1.1 jmcneill } 204 1.1 jmcneill 205 1.1 jmcneill clk = fdtbus_clock_get_index(phandle, 0); 206 1.1 jmcneill if (clk == NULL) { 207 1.1 jmcneill aprint_error(": couldn't get clock\n"); 208 1.1 jmcneill return; 209 1.1 jmcneill } 210 1.1 jmcneill 211 1.1 jmcneill sc->sc_dev = self; 212 1.1 jmcneill sc->sc_clkfreq = clk_get_rate(clk); 213 1.1 jmcneill sc->sc_bst = faa->faa_bst; 214 1.1 jmcneill error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh); 215 1.1 jmcneill if (error) { 216 1.3 skrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", 217 1.3 skrll addr, error); 218 1.1 jmcneill return; 219 1.1 jmcneill } 220 1.1 jmcneill 221 1.1 jmcneill aprint_naive("\n"); 222 1.1 jmcneill aprint_normal(": PWM\n"); 223 1.1 jmcneill 224 1.1 jmcneill sc->sc_pwm.pwm_enable = sunxi_pwm_enable; 225 1.1 jmcneill sc->sc_pwm.pwm_get_config = sunxi_pwm_get_config; 226 1.1 jmcneill sc->sc_pwm.pwm_set_config = sunxi_pwm_set_config; 227 1.1 jmcneill sc->sc_pwm.pwm_dev = self; 228 1.1 jmcneill 229 1.1 jmcneill fdtbus_register_pwm_controller(self, phandle, 230 1.1 jmcneill &sunxi_pwm_funcs); 231 1.1 jmcneill } 232 1.1 jmcneill 233 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_pwm, sizeof(struct sunxi_pwm_softc), 234 1.1 jmcneill sunxi_pwm_match, sunxi_pwm_attach, NULL, NULL); 235