Home | History | Annotate | Line # | Download | only in imx
imxpwm.c revision 1.1.4.2
      1  1.1.4.2  yamt /*	$NetBSD: imxpwm.c,v 1.1.4.2 2014/05/22 11:39:32 yamt Exp $	*/
      2  1.1.4.2  yamt 
      3  1.1.4.2  yamt /*
      4  1.1.4.2  yamt  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
      5  1.1.4.2  yamt  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  1.1.4.2  yamt  *
      7  1.1.4.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1.4.2  yamt  * modification, are permitted provided that the following conditions
      9  1.1.4.2  yamt  * are met:
     10  1.1.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1.4.2  yamt  *
     16  1.1.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     17  1.1.4.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  1.1.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1.4.2  yamt  */
     28  1.1.4.2  yamt 
     29  1.1.4.2  yamt #include <sys/cdefs.h>
     30  1.1.4.2  yamt __KERNEL_RCSID(0, "$NetBSD: imxpwm.c,v 1.1.4.2 2014/05/22 11:39:32 yamt Exp $");
     31  1.1.4.2  yamt 
     32  1.1.4.2  yamt #include "opt_imx.h"
     33  1.1.4.2  yamt #include "locators.h"
     34  1.1.4.2  yamt 
     35  1.1.4.2  yamt #include <sys/types.h>
     36  1.1.4.2  yamt #include <sys/param.h>
     37  1.1.4.2  yamt #include <sys/bus.h>
     38  1.1.4.2  yamt #include <sys/device.h>
     39  1.1.4.2  yamt 
     40  1.1.4.2  yamt #include <arm/imx/imxpwmreg.h>
     41  1.1.4.2  yamt #include <arm/imx/imxpwmvar.h>
     42  1.1.4.2  yamt 
     43  1.1.4.2  yamt static inline uint32_t
     44  1.1.4.2  yamt imxpwm_read(struct imxpwm_softc *sc, bus_size_t o)
     45  1.1.4.2  yamt {
     46  1.1.4.2  yamt 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, o);
     47  1.1.4.2  yamt }
     48  1.1.4.2  yamt 
     49  1.1.4.2  yamt static inline void
     50  1.1.4.2  yamt imxpwm_write(struct imxpwm_softc *sc, bus_size_t o, uint32_t v)
     51  1.1.4.2  yamt {
     52  1.1.4.2  yamt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, o, v);
     53  1.1.4.2  yamt }
     54  1.1.4.2  yamt 
     55  1.1.4.2  yamt static int
     56  1.1.4.2  yamt imxpwm_intr(void *arg)
     57  1.1.4.2  yamt {
     58  1.1.4.2  yamt 	struct imxpwm_softc *sc = arg;
     59  1.1.4.2  yamt 	uint32_t sts = imxpwm_read(sc, PWM_SR);
     60  1.1.4.2  yamt 
     61  1.1.4.2  yamt 	imxpwm_write(sc, PWM_SR, sts);
     62  1.1.4.2  yamt 
     63  1.1.4.2  yamt 	if ((sts & SR_ROV) && (sc->sc_handler != NULL))
     64  1.1.4.2  yamt 		sc->sc_handler(sc->sc_cookie);
     65  1.1.4.2  yamt 
     66  1.1.4.2  yamt 	return 1;
     67  1.1.4.2  yamt }
     68  1.1.4.2  yamt 
     69  1.1.4.2  yamt void
     70  1.1.4.2  yamt imxpwm_attach_common(struct imxpwm_softc *sc)
     71  1.1.4.2  yamt {
     72  1.1.4.2  yamt 	uint32_t reg;
     73  1.1.4.2  yamt 	int div;
     74  1.1.4.2  yamt 
     75  1.1.4.2  yamt 	if (sc->sc_handler != NULL) {
     76  1.1.4.2  yamt 		sc->sc_ih = intr_establish(sc->sc_intr, IPL_BIO, IST_LEVEL,
     77  1.1.4.2  yamt 		    imxpwm_intr, sc);
     78  1.1.4.2  yamt 
     79  1.1.4.2  yamt 		reg = IR_RIE;
     80  1.1.4.2  yamt 		imxpwm_write(sc, PWM_IR, reg);
     81  1.1.4.2  yamt 	}
     82  1.1.4.2  yamt 
     83  1.1.4.2  yamt 	if (sc->sc_hz <= 0)
     84  1.1.4.2  yamt 		sc->sc_hz = IMXPWM_DEFAULT_HZ;
     85  1.1.4.2  yamt 	div = 0;
     86  1.1.4.2  yamt 	do {
     87  1.1.4.2  yamt 		div++;
     88  1.1.4.2  yamt 		sc->sc_cycle = sc->sc_freq / div / sc->sc_hz;
     89  1.1.4.2  yamt 	} while (sc->sc_cycle > 0xffff);
     90  1.1.4.2  yamt 
     91  1.1.4.2  yamt 	imxpwm_write(sc, PWM_PR, __SHIFTIN(sc->sc_cycle - 2, PR_PERIOD));
     92  1.1.4.2  yamt 
     93  1.1.4.2  yamt 	reg = 0;
     94  1.1.4.2  yamt 	reg |= __SHIFTIN(CLKSRC_IPG_CLK, CR_CLKSRC);
     95  1.1.4.2  yamt 	reg |= __SHIFTIN(div - 1, CR_PRESCALER);
     96  1.1.4.2  yamt 	reg |= CR_EN;
     97  1.1.4.2  yamt 	imxpwm_write(sc, PWM_CR, reg);
     98  1.1.4.2  yamt }
     99  1.1.4.2  yamt 
    100  1.1.4.2  yamt int
    101  1.1.4.2  yamt imxpwm_set_pwm(struct imxpwm_softc *sc, int duty)
    102  1.1.4.2  yamt {
    103  1.1.4.2  yamt 	if (duty < 0 || IMXPWM_DUTY_MAX < duty)
    104  1.1.4.2  yamt 		return EINVAL;
    105  1.1.4.2  yamt 
    106  1.1.4.2  yamt 	sc->sc_duty = duty;
    107  1.1.4.2  yamt 
    108  1.1.4.2  yamt 	uint16_t reg = sc->sc_cycle * sc->sc_duty / IMXPWM_DUTY_MAX;
    109  1.1.4.2  yamt 	if (reg != 0)
    110  1.1.4.2  yamt 		reg -= 1;
    111  1.1.4.2  yamt 	imxpwm_write(sc, PWM_SAR, __SHIFTIN(reg, SAR_SAMPLE));
    112  1.1.4.2  yamt 
    113  1.1.4.2  yamt 	return 0;
    114  1.1.4.2  yamt }
    115