Home | History | Annotate | Line # | Download | only in nxp
imx6_pwm.c revision 1.1
      1  1.1  skrll /*	$NetBSD: imx6_pwm.c,v 1.1 2020/12/23 14:42:38 skrll Exp $	*/
      2  1.1  skrll /*-
      3  1.1  skrll  * Copyright (c) 2019  Genetec Corporation.  All rights reserved.
      4  1.1  skrll  * Written by Hashimoto Kenichi for Genetec Corporation.
      5  1.1  skrll  *
      6  1.1  skrll  * Redistribution and use in source and binary forms, with or without
      7  1.1  skrll  * modification, are permitted provided that the following conditions
      8  1.1  skrll  * are met:
      9  1.1  skrll  * 1. Redistributions of source code must retain the above copyright
     10  1.1  skrll  *    notice, this list of conditions and the following disclaimer.
     11  1.1  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  skrll  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  skrll  *    documentation and/or other materials provided with the distribution.
     14  1.1  skrll  *
     15  1.1  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1  skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1  skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1  skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1  skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  skrll  * SUCH DAMAGE.
     26  1.1  skrll  */
     27  1.1  skrll 
     28  1.1  skrll #include <sys/cdefs.h>
     29  1.1  skrll __KERNEL_RCSID(0, "$NetBSD: imx6_pwm.c,v 1.1 2020/12/23 14:42:38 skrll Exp $");
     30  1.1  skrll 
     31  1.1  skrll #include <sys/types.h>
     32  1.1  skrll #include <sys/param.h>
     33  1.1  skrll #include <sys/bus.h>
     34  1.1  skrll #include <sys/device.h>
     35  1.1  skrll 
     36  1.1  skrll #include <arm/imx/imxpwmvar.h>
     37  1.1  skrll 
     38  1.1  skrll #include <dev/fdt/fdtvar.h>
     39  1.1  skrll 
     40  1.1  skrll struct imxpwm_fdt_softc {
     41  1.1  skrll 	struct imxpwm_softc sc_imxpwm; /* Must be first */
     42  1.1  skrll };
     43  1.1  skrll 
     44  1.1  skrll static int imx6_pwm_match(device_t, cfdata_t, void *);
     45  1.1  skrll static void imx6_pwm_attach(device_t, device_t, void *);
     46  1.1  skrll 
     47  1.1  skrll CFATTACH_DECL_NEW(imxpwm_fdt, sizeof(struct imxpwm_fdt_softc),
     48  1.1  skrll     imx6_pwm_match, imx6_pwm_attach, NULL, NULL);
     49  1.1  skrll 
     50  1.1  skrll static pwm_tag_t
     51  1.1  skrll imxpwm_get_tag(device_t dev, const void *data, size_t len)
     52  1.1  skrll {
     53  1.1  skrll 	struct imxpwm_fdt_softc * const ifsc = device_private(dev);
     54  1.1  skrll 	struct imxpwm_softc * const sc = &ifsc->sc_imxpwm;
     55  1.1  skrll 	const u_int *pwm = data;
     56  1.1  skrll 
     57  1.1  skrll 	if (len < 12)
     58  1.1  skrll 		return NULL;
     59  1.1  skrll 
     60  1.1  skrll 	const u_int index = be32toh(pwm[1]);
     61  1.1  skrll 	if (index != 0)
     62  1.1  skrll 		return NULL;
     63  1.1  skrll 	const u_int period = be32toh(pwm[2]);
     64  1.1  skrll 
     65  1.1  skrll 	sc->sc_conf.period = period;
     66  1.1  skrll 	if (len >= 16) {
     67  1.1  skrll 		const u_int polarity = be32toh(pwm[3]);
     68  1.1  skrll 		sc->sc_conf.period = polarity ? PWM_ACTIVE_LOW : PWM_ACTIVE_HIGH;
     69  1.1  skrll 	}
     70  1.1  skrll 
     71  1.1  skrll 	return &sc->sc_pwm;
     72  1.1  skrll }
     73  1.1  skrll 
     74  1.1  skrll static struct fdtbus_pwm_controller_func imxpwm_funcs = {
     75  1.1  skrll 	.get_tag = imxpwm_get_tag
     76  1.1  skrll };
     77  1.1  skrll 
     78  1.1  skrll static int
     79  1.1  skrll imx6_pwm_match(device_t parent, cfdata_t cf, void *aux)
     80  1.1  skrll {
     81  1.1  skrll 	const char * const compatible[] = { "fsl,imx6q-pwm", NULL };
     82  1.1  skrll 	struct fdt_attach_args * const faa = aux;
     83  1.1  skrll 
     84  1.1  skrll 	return of_match_compatible(faa->faa_phandle, compatible);
     85  1.1  skrll }
     86  1.1  skrll 
     87  1.1  skrll void
     88  1.1  skrll imx6_pwm_attach(device_t parent, device_t self, void *aux)
     89  1.1  skrll {
     90  1.1  skrll 	struct imxpwm_fdt_softc * const ifsc = device_private(self);
     91  1.1  skrll 	struct imxpwm_softc * const sc = &ifsc->sc_imxpwm;
     92  1.1  skrll 	struct fdt_attach_args * const faa = aux;
     93  1.1  skrll 	const int phandle = faa->faa_phandle;
     94  1.1  skrll 	char intrstr[128];
     95  1.1  skrll 	bus_addr_t addr;
     96  1.1  skrll 	bus_size_t size;
     97  1.1  skrll 	int error;
     98  1.1  skrll 
     99  1.1  skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    100  1.1  skrll 		aprint_error(": couldn't get PWM registers\n");
    101  1.1  skrll 		return;
    102  1.1  skrll 	}
    103  1.1  skrll 
    104  1.1  skrll 	sc->sc_dev = self;
    105  1.1  skrll 	sc->sc_iot = faa->faa_bst;
    106  1.1  skrll 
    107  1.1  skrll 	error = bus_space_map(sc->sc_iot, addr, size, 0,
    108  1.1  skrll 	    &sc->sc_ioh);
    109  1.1  skrll 	if (error) {
    110  1.1  skrll 		aprint_error(": couldn't map gpc registers: %d\n", error);
    111  1.1  skrll 		return;
    112  1.1  skrll 	}
    113  1.1  skrll 
    114  1.1  skrll 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    115  1.1  skrll 		aprint_error_dev(self, "failed to decode interrupt\n");
    116  1.1  skrll 		return;
    117  1.1  skrll 	}
    118  1.1  skrll 
    119  1.1  skrll 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM,
    120  1.1  skrll 	    0, imxpwm_intr, sc);
    121  1.1  skrll 	if (sc->sc_ih == NULL) {
    122  1.1  skrll 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    123  1.1  skrll 		    intrstr);
    124  1.1  skrll 		return;
    125  1.1  skrll 	}
    126  1.1  skrll 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    127  1.1  skrll 
    128  1.1  skrll 	sc->sc_clk = fdtbus_clock_get(phandle, "per");
    129  1.1  skrll 	if (sc->sc_clk == NULL) {
    130  1.1  skrll 		aprint_error(": couldn't get clk\n");
    131  1.1  skrll 		return;
    132  1.1  skrll 	}
    133  1.1  skrll 	sc->sc_freq = clk_get_rate(sc->sc_clk);
    134  1.1  skrll 
    135  1.1  skrll 	imxpwm_attach_common(sc);
    136  1.1  skrll 
    137  1.1  skrll 	fdtbus_register_pwm_controller(self, phandle,
    138  1.1  skrll 	    &imxpwm_funcs);
    139  1.1  skrll 
    140  1.1  skrll 	return;
    141  1.1  skrll }
    142  1.1  skrll 
    143