Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
| 4 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 5 | * |
Robert P. J. Day | 5052e81 | 2016-09-13 08:35:18 -0400 | [diff] [blame] | 6 | * Basic support for the pwm module on imx6. |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <div64.h> |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 13 | #include <pwm.h> |
| 14 | #include <asm/arch/imx-regs.h> |
| 15 | #include <asm/io.h> |
| 16 | #include "pwm-imx-util.h" |
| 17 | |
| 18 | int pwm_init(int pwm_id, int div, int invert) |
| 19 | { |
| 20 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 21 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 22 | if (!pwm) |
| 23 | return -1; |
| 24 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 25 | writel(0, &pwm->ir); |
| 26 | return 0; |
| 27 | } |
| 28 | |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 29 | int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles, |
| 30 | unsigned long duty_cycles, unsigned long prescale) |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 31 | { |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 32 | u32 cr; |
| 33 | |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 34 | writel(0, &pwm->ir); |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 35 | cr = PWMCR_PRESCALER(prescale) | |
| 36 | PWMCR_DOZEEN | PWMCR_WAITEN | |
| 37 | PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH; |
| 38 | |
| 39 | writel(cr, &pwm->cr); |
| 40 | /* set duty cycles */ |
| 41 | writel(duty_cycles, &pwm->sar); |
| 42 | /* set period cycles */ |
| 43 | writel(period_cycles, &pwm->pr); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 47 | int pwm_config(int pwm_id, int duty_ns, int period_ns) |
| 48 | { |
| 49 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 50 | unsigned long period_cycles, duty_cycles, prescale; |
| 51 | |
| 52 | if (!pwm) |
| 53 | return -1; |
| 54 | |
| 55 | pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles, |
| 56 | &prescale); |
| 57 | |
| 58 | return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale); |
| 59 | } |
| 60 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 61 | int pwm_enable(int pwm_id) |
| 62 | { |
| 63 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 64 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 65 | if (!pwm) |
| 66 | return -1; |
| 67 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 68 | setbits_le32(&pwm->cr, PWMCR_EN); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | void pwm_disable(int pwm_id) |
| 73 | { |
| 74 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 75 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 76 | if (!pwm) |
| 77 | return; |
| 78 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 79 | clrbits_le32(&pwm->cr, PWMCR_EN); |
| 80 | } |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 81 | |
| 82 | #if defined(CONFIG_DM_PWM) |
| 83 | struct imx_pwm_priv { |
| 84 | struct pwm_regs *regs; |
| 85 | bool invert; |
| 86 | }; |
| 87 | |
| 88 | static int imx_pwm_set_invert(struct udevice *dev, uint channel, |
| 89 | bool polarity) |
| 90 | { |
| 91 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 92 | |
| 93 | debug("%s: polarity=%u\n", __func__, polarity); |
| 94 | priv->invert = polarity; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int imx_pwm_set_config(struct udevice *dev, uint channel, |
| 100 | uint period_ns, uint duty_ns) |
| 101 | { |
| 102 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 103 | struct pwm_regs *regs = priv->regs; |
| 104 | unsigned long period_cycles, duty_cycles, prescale; |
| 105 | |
| 106 | debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel); |
| 107 | |
| 108 | pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles, |
| 109 | &prescale); |
| 110 | |
| 111 | return pwm_config_internal(regs, period_cycles, duty_cycles, prescale); |
| 112 | }; |
| 113 | |
| 114 | static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable) |
| 115 | { |
| 116 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 117 | struct pwm_regs *regs = priv->regs; |
| 118 | |
| 119 | debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable); |
| 120 | |
| 121 | if (enable) |
| 122 | setbits_le32(®s->cr, PWMCR_EN); |
| 123 | else |
| 124 | clrbits_le32(®s->cr, PWMCR_EN); |
| 125 | |
| 126 | return 0; |
| 127 | }; |
| 128 | |
| 129 | static int imx_pwm_ofdata_to_platdata(struct udevice *dev) |
| 130 | { |
| 131 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 132 | |
| 133 | priv->regs = (struct pwm_regs *)devfdt_get_addr(dev); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int imx_pwm_probe(struct udevice *dev) |
| 139 | { |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static const struct pwm_ops imx_pwm_ops = { |
| 144 | .set_invert = imx_pwm_set_invert, |
| 145 | .set_config = imx_pwm_set_config, |
| 146 | .set_enable = imx_pwm_set_enable, |
| 147 | }; |
| 148 | |
| 149 | static const struct udevice_id imx_pwm_ids[] = { |
| 150 | { .compatible = "fsl,imx27-pwm" }, |
| 151 | { } |
| 152 | }; |
| 153 | |
| 154 | U_BOOT_DRIVER(imx_pwm) = { |
| 155 | .name = "imx_pwm", |
| 156 | .id = UCLASS_PWM, |
| 157 | .of_match = imx_pwm_ids, |
| 158 | .ops = &imx_pwm_ops, |
| 159 | .ofdata_to_platdata = imx_pwm_ofdata_to_platdata, |
| 160 | .probe = imx_pwm_probe, |
| 161 | .priv_auto_alloc_size = sizeof(struct imx_pwm_priv), |
| 162 | }; |
| 163 | #endif |