Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2014 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 4 | * |
Robert P. J. Day | 5052e81 | 2016-09-13 08:35:18 -0400 | [diff] [blame] | 5 | * Basic support for the pwm module on imx6. |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <div64.h> |
| 12 | #include <pwm.h> |
| 13 | #include <asm/arch/imx-regs.h> |
| 14 | #include <asm/io.h> |
| 15 | #include "pwm-imx-util.h" |
| 16 | |
| 17 | int pwm_init(int pwm_id, int div, int invert) |
| 18 | { |
| 19 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 20 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 21 | if (!pwm) |
| 22 | return -1; |
| 23 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 24 | writel(0, &pwm->ir); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | int pwm_config(int pwm_id, int duty_ns, int period_ns) |
| 29 | { |
| 30 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 31 | unsigned long period_cycles, duty_cycles, prescale; |
| 32 | u32 cr; |
| 33 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 34 | if (!pwm) |
| 35 | return -1; |
| 36 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 37 | pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles, |
| 38 | &prescale); |
| 39 | |
| 40 | cr = PWMCR_PRESCALER(prescale) | |
| 41 | PWMCR_DOZEEN | PWMCR_WAITEN | |
| 42 | PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH; |
| 43 | |
| 44 | writel(cr, &pwm->cr); |
| 45 | /* set duty cycles */ |
| 46 | writel(duty_cycles, &pwm->sar); |
| 47 | /* set period cycles */ |
| 48 | writel(period_cycles, &pwm->pr); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int pwm_enable(int pwm_id) |
| 53 | { |
| 54 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 55 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 56 | if (!pwm) |
| 57 | return -1; |
| 58 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 59 | setbits_le32(&pwm->cr, PWMCR_EN); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | void pwm_disable(int pwm_id) |
| 64 | { |
| 65 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 66 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 67 | if (!pwm) |
| 68 | return; |
| 69 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 70 | clrbits_le32(&pwm->cr, PWMCR_EN); |
| 71 | } |