xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_pwm.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sunxi_pwm.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $ */
226f780d3Sjmcneill 
326f780d3Sjmcneill /*-
426f780d3Sjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
526f780d3Sjmcneill  * All rights reserved.
626f780d3Sjmcneill  *
726f780d3Sjmcneill  * Redistribution and use in source and binary forms, with or without
826f780d3Sjmcneill  * modification, are permitted provided that the following conditions
926f780d3Sjmcneill  * are met:
1026f780d3Sjmcneill  * 1. Redistributions of source code must retain the above copyright
1126f780d3Sjmcneill  *    notice, this list of conditions and the following disclaimer.
1226f780d3Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1326f780d3Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
1426f780d3Sjmcneill  *    documentation and/or other materials provided with the distribution.
1526f780d3Sjmcneill  *
1626f780d3Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1726f780d3Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1826f780d3Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1926f780d3Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2026f780d3Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2126f780d3Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2226f780d3Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2326f780d3Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2426f780d3Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2526f780d3Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626f780d3Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
2726f780d3Sjmcneill  */
2826f780d3Sjmcneill 
2926f780d3Sjmcneill #include <sys/cdefs.h>
3026f780d3Sjmcneill 
31*6e54367aSthorpej __KERNEL_RCSID(1, "$NetBSD: sunxi_pwm.c,v 1.7 2021/01/27 03:10:20 thorpej Exp $");
3226f780d3Sjmcneill 
3326f780d3Sjmcneill #include <sys/param.h>
3426f780d3Sjmcneill #include <sys/bus.h>
3526f780d3Sjmcneill #include <sys/device.h>
3626f780d3Sjmcneill #include <sys/intr.h>
3726f780d3Sjmcneill #include <sys/systm.h>
3826f780d3Sjmcneill #include <sys/time.h>
3926f780d3Sjmcneill 
4026f780d3Sjmcneill #include <dev/pwm/pwmvar.h>
4126f780d3Sjmcneill 
4226f780d3Sjmcneill #include <dev/fdt/fdtvar.h>
4326f780d3Sjmcneill 
4426f780d3Sjmcneill #define	PWM_CH_CTRL		0x00
4526f780d3Sjmcneill #define	 PWM0_RDY		__BIT(28)
4626f780d3Sjmcneill #define	 PWM0_BYPASS		__BIT(9)
4726f780d3Sjmcneill #define	 PWM_CH0_PUL_START	__BIT(8)
4826f780d3Sjmcneill #define	 PWM_CHANNEL0_MODE	__BIT(7)
4926f780d3Sjmcneill #define	 SCLK_CH0_GATING	__BIT(6)
5026f780d3Sjmcneill #define	 PWM_CH0_ACT_STA	__BIT(5)
5126f780d3Sjmcneill #define	 PWM_CH0_EN		__BIT(4)
5226f780d3Sjmcneill #define	 PWM_CH0_PRESCAL	__BITS(3,0)
5326f780d3Sjmcneill #define	PWM_CH0_PERIOD		0x04
5426f780d3Sjmcneill #define	 PWM_CH0_ENTIRE_CYS	__BITS(31,16)
5526f780d3Sjmcneill #define	 PWM_CH0_ENTIRE_ACT_CYS	__BITS(15,0)
5626f780d3Sjmcneill 
5726f780d3Sjmcneill enum sunxi_pwm_type {
5826f780d3Sjmcneill 	PWM_A64 = 1,
5926f780d3Sjmcneill };
6026f780d3Sjmcneill 
61646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
62646c0f59Sthorpej 	{ .compat = "allwinner,sun50i-a64-pwm",	.value = PWM_A64 },
63ec189949Sthorpej 	DEVICE_COMPAT_EOL
6426f780d3Sjmcneill };
6526f780d3Sjmcneill 
6626f780d3Sjmcneill struct sunxi_pwm_softc {
6726f780d3Sjmcneill 	device_t		sc_dev;
6826f780d3Sjmcneill 	bus_space_tag_t		sc_bst;
6926f780d3Sjmcneill 	bus_space_handle_t	sc_bsh;
7026f780d3Sjmcneill 
7126f780d3Sjmcneill 	struct pwm_controller	sc_pwm;
7226f780d3Sjmcneill 	struct pwm_config	sc_conf;
7326f780d3Sjmcneill 
7426f780d3Sjmcneill 	u_int			sc_clkfreq;
7526f780d3Sjmcneill };
7626f780d3Sjmcneill 
7726f780d3Sjmcneill #define	PWM_READ(sc, reg)		\
7826f780d3Sjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
7926f780d3Sjmcneill #define	PWM_WRITE(sc, reg, val)		\
8026f780d3Sjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
8126f780d3Sjmcneill 
8226f780d3Sjmcneill static pwm_tag_t
sunxi_pwm_get_tag(device_t dev,const void * data,size_t len)8326f780d3Sjmcneill sunxi_pwm_get_tag(device_t dev, const void *data, size_t len)
8426f780d3Sjmcneill {
8526f780d3Sjmcneill 	struct sunxi_pwm_softc * const sc = device_private(dev);
8626f780d3Sjmcneill 	const u_int *pwm = data;
8726f780d3Sjmcneill 
8826f780d3Sjmcneill 	if (len != 16)
8926f780d3Sjmcneill 		return NULL;
9026f780d3Sjmcneill 
9126f780d3Sjmcneill 	const u_int index = be32toh(pwm[1]);
9226f780d3Sjmcneill 	if (index != 0)
9326f780d3Sjmcneill 		return NULL;
9426f780d3Sjmcneill 
9526f780d3Sjmcneill 	const u_int period = be32toh(pwm[2]);
9626f780d3Sjmcneill 	const u_int polarity = be32toh(pwm[3]);
9726f780d3Sjmcneill 
9826f780d3Sjmcneill 	sc->sc_conf.period = period;
9926f780d3Sjmcneill 	sc->sc_conf.polarity = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH;
10026f780d3Sjmcneill 
10126f780d3Sjmcneill 	return &sc->sc_pwm;
10226f780d3Sjmcneill }
10326f780d3Sjmcneill 
10426f780d3Sjmcneill static struct fdtbus_pwm_controller_func sunxi_pwm_funcs = {
10526f780d3Sjmcneill 	.get_tag = sunxi_pwm_get_tag
10626f780d3Sjmcneill };
10726f780d3Sjmcneill 
10826f780d3Sjmcneill static int
sunxi_pwm_enable(pwm_tag_t pwm,bool enable)10926f780d3Sjmcneill sunxi_pwm_enable(pwm_tag_t pwm, bool enable)
11026f780d3Sjmcneill {
11126f780d3Sjmcneill 	struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev);
11226f780d3Sjmcneill 	uint32_t ctrl, octrl;
11326f780d3Sjmcneill 
11426f780d3Sjmcneill 	octrl = ctrl = PWM_READ(sc, PWM_CH_CTRL);
11526f780d3Sjmcneill 	if (enable)
11626f780d3Sjmcneill 		ctrl |= (PWM_CH0_EN | SCLK_CH0_GATING);
11726f780d3Sjmcneill 	else
11826f780d3Sjmcneill 		ctrl &= ~(PWM_CH0_EN | SCLK_CH0_GATING);
11926f780d3Sjmcneill 
12026f780d3Sjmcneill 	if (ctrl != octrl)
12126f780d3Sjmcneill 		PWM_WRITE(sc, PWM_CH_CTRL, ctrl);
12226f780d3Sjmcneill 
12326f780d3Sjmcneill 	return 0;
12426f780d3Sjmcneill }
12526f780d3Sjmcneill 
12626f780d3Sjmcneill static int
sunxi_pwm_get_config(pwm_tag_t pwm,struct pwm_config * conf)12726f780d3Sjmcneill sunxi_pwm_get_config(pwm_tag_t pwm, struct pwm_config *conf)
12826f780d3Sjmcneill {
12926f780d3Sjmcneill 	struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev);
13026f780d3Sjmcneill 	uint32_t ctrl, ch_period;
13126f780d3Sjmcneill 
13226f780d3Sjmcneill 	ctrl = PWM_READ(sc, PWM_CH_CTRL);
13326f780d3Sjmcneill 	ch_period = PWM_READ(sc, PWM_CH0_PERIOD);
13426f780d3Sjmcneill 
13526f780d3Sjmcneill 	const uint64_t rate = sc->sc_clkfreq / 120;
13626f780d3Sjmcneill 	const u_int cycles = __SHIFTOUT(ch_period, PWM_CH0_ENTIRE_CYS) + 1;
13726f780d3Sjmcneill 	const u_int act_cycles = __SHIFTOUT(ch_period, PWM_CH0_ENTIRE_ACT_CYS);
13826f780d3Sjmcneill 
13926f780d3Sjmcneill 	conf->polarity = (ctrl & PWM_CH0_ACT_STA) ? PWM_ACTIVE_HIGH : PWM_ACTIVE_LOW;
14026f780d3Sjmcneill 	conf->period = (u_int)(((uint64_t)cycles * 1000000000) / rate);
14126f780d3Sjmcneill 	conf->duty_cycle = (u_int)(((uint64_t)act_cycles * 1000000000) / rate);
14226f780d3Sjmcneill 
14326f780d3Sjmcneill 	return 0;
14426f780d3Sjmcneill }
14526f780d3Sjmcneill 
14626f780d3Sjmcneill static int
sunxi_pwm_set_config(pwm_tag_t pwm,const struct pwm_config * conf)14726f780d3Sjmcneill sunxi_pwm_set_config(pwm_tag_t pwm, const struct pwm_config *conf)
14826f780d3Sjmcneill {
14926f780d3Sjmcneill 	struct sunxi_pwm_softc * const sc = device_private(pwm->pwm_dev);
15026f780d3Sjmcneill 	uint32_t ctrl, ch_period;
15126f780d3Sjmcneill 
15226f780d3Sjmcneill 	ctrl = PWM_READ(sc, PWM_CH_CTRL);
15326f780d3Sjmcneill 	if (ctrl & PWM0_RDY)
15426f780d3Sjmcneill 		return EBUSY;
15526f780d3Sjmcneill 
15626f780d3Sjmcneill 	ctrl &= ~PWM0_BYPASS;		/* Prescaler /120 = 200 kHz */
15726f780d3Sjmcneill 	ctrl &= ~PWM_CH0_PRESCAL;
15826f780d3Sjmcneill 	ctrl |= __SHIFTIN(0, PWM_CH0_PRESCAL);
15926f780d3Sjmcneill 
16026f780d3Sjmcneill 	ctrl &= ~PWM_CHANNEL0_MODE;	/* Cycle mode */
16126f780d3Sjmcneill 	if (conf->polarity == PWM_ACTIVE_HIGH)
16226f780d3Sjmcneill 		ctrl |= PWM_CH0_ACT_STA;
16326f780d3Sjmcneill 	else
16426f780d3Sjmcneill 		ctrl &= ~PWM_CH0_ACT_STA;
16526f780d3Sjmcneill 
16626f780d3Sjmcneill 	const uint64_t rate = sc->sc_clkfreq / 120;
16726f780d3Sjmcneill 	const u_int cycles = (u_int)((conf->period * rate) / 1000000000);
16826f780d3Sjmcneill 	const u_int act_cycles = (u_int)((conf->duty_cycle * rate) / 1000000000);
16926f780d3Sjmcneill 
17026f780d3Sjmcneill 	ch_period = __SHIFTIN(cycles - 1, PWM_CH0_ENTIRE_CYS);
17126f780d3Sjmcneill 	ch_period |= __SHIFTIN(act_cycles, PWM_CH0_ENTIRE_ACT_CYS);
17226f780d3Sjmcneill 
17326f780d3Sjmcneill 	PWM_WRITE(sc, PWM_CH0_PERIOD, ch_period);
17426f780d3Sjmcneill 	PWM_WRITE(sc, PWM_CH_CTRL, ctrl);
17526f780d3Sjmcneill 
17626f780d3Sjmcneill 	sc->sc_conf = *conf;
17726f780d3Sjmcneill 
17826f780d3Sjmcneill 	return 0;
17926f780d3Sjmcneill }
18026f780d3Sjmcneill 
18126f780d3Sjmcneill static int
sunxi_pwm_match(device_t parent,cfdata_t cf,void * aux)18226f780d3Sjmcneill sunxi_pwm_match(device_t parent, cfdata_t cf, void *aux)
18326f780d3Sjmcneill {
18426f780d3Sjmcneill 	struct fdt_attach_args * const faa = aux;
18526f780d3Sjmcneill 
186*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
18726f780d3Sjmcneill }
18826f780d3Sjmcneill 
18926f780d3Sjmcneill static void
sunxi_pwm_attach(device_t parent,device_t self,void * aux)19026f780d3Sjmcneill sunxi_pwm_attach(device_t parent, device_t self, void *aux)
19126f780d3Sjmcneill {
19226f780d3Sjmcneill 	struct sunxi_pwm_softc * const sc = device_private(self);
19326f780d3Sjmcneill 	struct fdt_attach_args * const faa = aux;
19426f780d3Sjmcneill 	const int phandle = faa->faa_phandle;
19526f780d3Sjmcneill 	struct clk *clk;
19626f780d3Sjmcneill 	bus_addr_t addr;
19726f780d3Sjmcneill 	bus_size_t size;
19826f780d3Sjmcneill 	int error;
19926f780d3Sjmcneill 
20026f780d3Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
20126f780d3Sjmcneill 		aprint_error(": couldn't get registers\n");
20226f780d3Sjmcneill 		return;
20326f780d3Sjmcneill 	}
20426f780d3Sjmcneill 
20526f780d3Sjmcneill 	clk = fdtbus_clock_get_index(phandle, 0);
20626f780d3Sjmcneill 	if (clk == NULL) {
20726f780d3Sjmcneill 		aprint_error(": couldn't get clock\n");
20826f780d3Sjmcneill 		return;
20926f780d3Sjmcneill 	}
21026f780d3Sjmcneill 
21126f780d3Sjmcneill 	sc->sc_dev = self;
21226f780d3Sjmcneill 	sc->sc_clkfreq = clk_get_rate(clk);
21326f780d3Sjmcneill 	sc->sc_bst = faa->faa_bst;
21426f780d3Sjmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
21526f780d3Sjmcneill 	if (error) {
216b815897eSskrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
217b815897eSskrll 		    addr, error);
21826f780d3Sjmcneill 		return;
21926f780d3Sjmcneill 	}
22026f780d3Sjmcneill 
22126f780d3Sjmcneill 	aprint_naive("\n");
22226f780d3Sjmcneill 	aprint_normal(": PWM\n");
22326f780d3Sjmcneill 
22426f780d3Sjmcneill 	sc->sc_pwm.pwm_enable = sunxi_pwm_enable;
22526f780d3Sjmcneill 	sc->sc_pwm.pwm_get_config = sunxi_pwm_get_config;
22626f780d3Sjmcneill 	sc->sc_pwm.pwm_set_config = sunxi_pwm_set_config;
22726f780d3Sjmcneill 	sc->sc_pwm.pwm_dev = self;
22826f780d3Sjmcneill 
22926f780d3Sjmcneill 	fdtbus_register_pwm_controller(self, phandle,
23026f780d3Sjmcneill 	    &sunxi_pwm_funcs);
23126f780d3Sjmcneill }
23226f780d3Sjmcneill 
23326f780d3Sjmcneill CFATTACH_DECL_NEW(sunxi_pwm, sizeof(struct sunxi_pwm_softc),
23426f780d3Sjmcneill 	sunxi_pwm_match, sunxi_pwm_attach, NULL, NULL);
235