1 1.7 thorpej /* $NetBSD: rk_pwm.c,v 1.7 2021/01/27 03:10:19 thorpej Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2019 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: rk_pwm.c,v 1.7 2021/01/27 03:10:19 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 PWM0_CNT 0x00 45 1.1 jmcneill #define PWM0_PERIOD_HPR 0x04 46 1.1 jmcneill #define PWM0_DUTY_LPR 0x08 47 1.1 jmcneill #define PWM0_CTRL 0x0c 48 1.1 jmcneill #define CTRL_RPT __BITS(31,24) 49 1.1 jmcneill #define CTRL_SCALE __BITS(23,16) 50 1.1 jmcneill #define CTRL_PRESCALE __BITS(14,12) 51 1.1 jmcneill #define CTRL_CLK_SEL __BIT(9) 52 1.1 jmcneill #define CTRL_LP_EN __BIT(8) 53 1.1 jmcneill #define CTRL_OUTPUT_MODE __BIT(5) 54 1.1 jmcneill #define CTRL_INACTIVE_POL __BIT(4) 55 1.1 jmcneill #define CTRL_DUTY_POL __BIT(3) 56 1.1 jmcneill #define CTRL_PWM_MODE __BITS(2,1) 57 1.1 jmcneill #define CTRL_PWM_MODE_ONESHOT 0 58 1.1 jmcneill #define CTRL_PWM_MODE_CONTINUOUS 1 59 1.1 jmcneill #define CTRL_PWM_MODE_CAPTURE 2 60 1.1 jmcneill #define CTRL_PWM_EN __BIT(0) 61 1.1 jmcneill 62 1.1 jmcneill enum rk_pwm_type { 63 1.1 jmcneill PWM_RK3288 = 1, 64 1.1 jmcneill }; 65 1.1 jmcneill 66 1.4 thorpej static const struct device_compatible_entry compat_data[] = { 67 1.4 thorpej { .compat = "rockchip,rk3288-pwm", .value = PWM_RK3288 }, 68 1.6 thorpej DEVICE_COMPAT_EOL 69 1.1 jmcneill }; 70 1.1 jmcneill 71 1.1 jmcneill struct rk_pwm_softc { 72 1.1 jmcneill device_t sc_dev; 73 1.1 jmcneill bus_space_tag_t sc_bst; 74 1.1 jmcneill bus_space_handle_t sc_bsh; 75 1.1 jmcneill 76 1.1 jmcneill struct pwm_controller sc_pwm; 77 1.1 jmcneill struct pwm_config sc_conf; 78 1.1 jmcneill 79 1.1 jmcneill u_int sc_clkfreq; 80 1.1 jmcneill }; 81 1.1 jmcneill 82 1.1 jmcneill #define PWM_READ(sc, reg) \ 83 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 84 1.1 jmcneill #define PWM_WRITE(sc, reg, val) \ 85 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 86 1.1 jmcneill 87 1.1 jmcneill static pwm_tag_t 88 1.1 jmcneill rk_pwm_get_tag(device_t dev, const void *data, size_t len) 89 1.1 jmcneill { 90 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(dev); 91 1.1 jmcneill const u_int *pwm = data; 92 1.1 jmcneill 93 1.1 jmcneill if (len != 16) 94 1.1 jmcneill return NULL; 95 1.1 jmcneill 96 1.1 jmcneill const u_int index = be32toh(pwm[1]); 97 1.1 jmcneill if (index != 0) 98 1.1 jmcneill return NULL; 99 1.1 jmcneill 100 1.1 jmcneill const u_int period = be32toh(pwm[2]); 101 1.1 jmcneill const u_int polarity = be32toh(pwm[3]); 102 1.1 jmcneill 103 1.1 jmcneill sc->sc_conf.period = period; 104 1.1 jmcneill sc->sc_conf.polarity = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH; 105 1.1 jmcneill 106 1.1 jmcneill return &sc->sc_pwm; 107 1.1 jmcneill } 108 1.1 jmcneill 109 1.1 jmcneill static struct fdtbus_pwm_controller_func rk_pwm_funcs = { 110 1.1 jmcneill .get_tag = rk_pwm_get_tag 111 1.1 jmcneill }; 112 1.1 jmcneill 113 1.1 jmcneill static int 114 1.1 jmcneill rk_pwm_enable(pwm_tag_t pwm, bool enable) 115 1.1 jmcneill { 116 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(pwm->pwm_dev); 117 1.1 jmcneill uint32_t ctrl, octrl; 118 1.1 jmcneill 119 1.1 jmcneill octrl = ctrl = PWM_READ(sc, PWM0_CTRL); 120 1.1 jmcneill if (enable) 121 1.1 jmcneill ctrl |= CTRL_PWM_EN; 122 1.1 jmcneill else 123 1.1 jmcneill ctrl &= ~CTRL_PWM_EN; 124 1.1 jmcneill 125 1.1 jmcneill if (ctrl != octrl) 126 1.1 jmcneill PWM_WRITE(sc, PWM0_CTRL, ctrl); 127 1.1 jmcneill 128 1.1 jmcneill return 0; 129 1.1 jmcneill } 130 1.1 jmcneill 131 1.1 jmcneill static int 132 1.1 jmcneill rk_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf) 133 1.1 jmcneill { 134 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(pwm->pwm_dev); 135 1.3 jakllsch 136 1.3 jakllsch #if 0 137 1.3 jakllsch /* XXX may be useful someday */ 138 1.3 jakllsch 139 1.1 jmcneill uint32_t ctrl, period, duty; 140 1.1 jmcneill u_int div; 141 1.1 jmcneill 142 1.1 jmcneill ctrl = PWM_READ(sc, PWM0_CTRL); 143 1.1 jmcneill period = PWM_READ(sc, PWM0_PERIOD_HPR); 144 1.1 jmcneill duty = PWM_READ(sc, PWM0_DUTY_LPR); 145 1.1 jmcneill 146 1.1 jmcneill if (ctrl & CTRL_CLK_SEL) { 147 1.1 jmcneill div = __SHIFTOUT(ctrl, CTRL_SCALE) * 2; 148 1.1 jmcneill if (div == 0) 149 1.1 jmcneill div = 512; 150 1.1 jmcneill } else { 151 1.1 jmcneill div = 1; 152 1.1 jmcneill } 153 1.1 jmcneill div /= (1 << __SHIFTOUT(ctrl, CTRL_PRESCALE)); 154 1.1 jmcneill 155 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq / div; 156 1.1 jmcneill 157 1.1 jmcneill conf->polarity = (ctrl & CTRL_DUTY_POL) ? PWM_ACTIVE_HIGH : PWM_ACTIVE_LOW; 158 1.1 jmcneill conf->period = (u_int)(((uint64_t)period * 1000000000) / rate); 159 1.1 jmcneill conf->duty_cycle = (u_int)(((uint64_t)duty * 1000000000) / rate); 160 1.3 jakllsch #else 161 1.3 jakllsch *conf = sc->sc_conf; 162 1.3 jakllsch #endif 163 1.1 jmcneill 164 1.1 jmcneill return 0; 165 1.1 jmcneill } 166 1.1 jmcneill 167 1.1 jmcneill static int 168 1.1 jmcneill rk_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf) 169 1.1 jmcneill { 170 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(pwm->pwm_dev); 171 1.1 jmcneill uint32_t ctrl; 172 1.1 jmcneill 173 1.1 jmcneill const uint64_t rate = sc->sc_clkfreq; 174 1.1 jmcneill const uint32_t period = (u_int)((conf->period * rate) / 1000000000); 175 1.1 jmcneill const uint32_t duty = (u_int)((conf->duty_cycle * rate) / 1000000000); 176 1.1 jmcneill 177 1.1 jmcneill /* Preserve PWM_EN flag */ 178 1.1 jmcneill ctrl = PWM_READ(sc, PWM0_CTRL) & CTRL_PWM_EN; 179 1.1 jmcneill 180 1.1 jmcneill ctrl |= __SHIFTIN(CTRL_PWM_MODE_CONTINUOUS, CTRL_PWM_MODE); 181 1.1 jmcneill if (conf->polarity == PWM_ACTIVE_HIGH) 182 1.1 jmcneill ctrl |= CTRL_DUTY_POL; 183 1.1 jmcneill else 184 1.1 jmcneill ctrl |= CTRL_INACTIVE_POL; 185 1.1 jmcneill 186 1.1 jmcneill PWM_WRITE(sc, PWM0_CTRL, 0); 187 1.1 jmcneill PWM_WRITE(sc, PWM0_PERIOD_HPR, period); 188 1.1 jmcneill PWM_WRITE(sc, PWM0_DUTY_LPR, duty); 189 1.1 jmcneill PWM_WRITE(sc, PWM0_CTRL, ctrl); 190 1.1 jmcneill 191 1.1 jmcneill sc->sc_conf = *conf; 192 1.1 jmcneill 193 1.1 jmcneill return 0; 194 1.1 jmcneill } 195 1.1 jmcneill 196 1.1 jmcneill static int 197 1.1 jmcneill rk_pwm_match(device_t parent, cfdata_t cf, void *aux) 198 1.1 jmcneill { 199 1.1 jmcneill struct fdt_attach_args * const faa = aux; 200 1.1 jmcneill 201 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 202 1.1 jmcneill } 203 1.1 jmcneill 204 1.1 jmcneill static void 205 1.1 jmcneill rk_pwm_attach(device_t parent, device_t self, void *aux) 206 1.1 jmcneill { 207 1.1 jmcneill struct rk_pwm_softc * const sc = device_private(self); 208 1.1 jmcneill struct fdt_attach_args * const faa = aux; 209 1.1 jmcneill const int phandle = faa->faa_phandle; 210 1.1 jmcneill struct clk *clk; 211 1.1 jmcneill bus_addr_t addr; 212 1.1 jmcneill bus_size_t size; 213 1.1 jmcneill int error; 214 1.1 jmcneill 215 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 216 1.1 jmcneill aprint_error(": couldn't get registers\n"); 217 1.1 jmcneill return; 218 1.1 jmcneill } 219 1.1 jmcneill 220 1.1 jmcneill clk = fdtbus_clock_get_index(phandle, 0); 221 1.1 jmcneill if (clk == NULL) { 222 1.1 jmcneill aprint_error(": couldn't get clock\n"); 223 1.1 jmcneill return; 224 1.1 jmcneill } 225 1.1 jmcneill 226 1.1 jmcneill sc->sc_dev = self; 227 1.1 jmcneill sc->sc_clkfreq = clk_get_rate(clk); 228 1.1 jmcneill sc->sc_bst = faa->faa_bst; 229 1.1 jmcneill error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh); 230 1.1 jmcneill if (error) { 231 1.2 skrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", 232 1.2 skrll addr, error); 233 1.1 jmcneill return; 234 1.1 jmcneill } 235 1.1 jmcneill 236 1.1 jmcneill aprint_naive("\n"); 237 1.1 jmcneill aprint_normal(": PWM\n"); 238 1.1 jmcneill 239 1.1 jmcneill sc->sc_pwm.pwm_enable = rk_pwm_enable; 240 1.1 jmcneill sc->sc_pwm.pwm_get_config = rk_pwm_get_config; 241 1.1 jmcneill sc->sc_pwm.pwm_set_config = rk_pwm_set_config; 242 1.1 jmcneill sc->sc_pwm.pwm_dev = self; 243 1.1 jmcneill 244 1.1 jmcneill fdtbus_register_pwm_controller(self, phandle, 245 1.1 jmcneill &rk_pwm_funcs); 246 1.1 jmcneill } 247 1.1 jmcneill 248 1.1 jmcneill CFATTACH_DECL_NEW(rk_pwm, sizeof(struct rk_pwm_softc), 249 1.1 jmcneill rk_pwm_match, rk_pwm_attach, NULL, NULL); 250