Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 2b38776 | 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 | 1f8378a | 2016-09-13 08:35:18 -0400 | [diff] [blame] | 6 | * Basic support for the pwm module on imx6. |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <div64.h> |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Heiko Schocher | 2b38776 | 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> |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 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 | ecae644 | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 21 | if (!pwm) |
| 22 | return -1; |
| 23 | |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 24 | writel(0, &pwm->ir); |
| 25 | return 0; |
| 26 | } |
Tommaso Merciai | 20b9c1d | 2022-03-26 12:19:06 +0100 | [diff] [blame] | 27 | #include <clk.h> |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 28 | |
Heiko Schocher | 00aa066 | 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 | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 31 | { |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 32 | u32 cr; |
| 33 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 34 | writel(0, &pwm->ir); |
Heiko Schocher | 2b38776 | 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 | |
Tommaso Merciai | 8c3b617 | 2022-03-26 12:19:05 +0100 | [diff] [blame^] | 47 | /* pwm_id from 0..7 */ |
| 48 | struct pwm_regs *pwm_id_to_reg(int pwm_id) |
| 49 | { |
| 50 | |
| 51 | switch (pwm_id) { |
| 52 | case 0: |
| 53 | return (struct pwm_regs *)PWM1_BASE_ADDR; |
| 54 | case 1: |
| 55 | return (struct pwm_regs *)PWM2_BASE_ADDR; |
| 56 | #ifdef CONFIG_MX6 |
| 57 | case 2: |
| 58 | return (struct pwm_regs *)PWM3_BASE_ADDR; |
| 59 | case 3: |
| 60 | return (struct pwm_regs *)PWM4_BASE_ADDR; |
| 61 | #endif |
| 62 | #ifdef CONFIG_MX6SX |
| 63 | case 4: |
| 64 | return (struct pwm_regs *)PWM5_BASE_ADDR; |
| 65 | case 5: |
| 66 | return (struct pwm_regs *)PWM6_BASE_ADDR; |
| 67 | case 6: |
| 68 | return (struct pwm_regs *)PWM7_BASE_ADDR; |
| 69 | case 7: |
| 70 | return (struct pwm_regs *)PWM8_BASE_ADDR; |
| 71 | #endif |
| 72 | default: |
| 73 | printf("unknown pwm_id: %d\n", pwm_id); |
| 74 | break; |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c, |
| 80 | unsigned long *duty_c, unsigned long *prescale) |
| 81 | { |
| 82 | unsigned long long c; |
| 83 | |
| 84 | /* |
| 85 | * we have not yet a clock framework for imx6, so add the clock |
| 86 | * value here as a define. Replace it when we have the clock |
| 87 | * framework. |
| 88 | */ |
| 89 | c = CONFIG_IMX6_PWM_PER_CLK; |
| 90 | c = c * period_ns; |
| 91 | do_div(c, 1000000000); |
| 92 | *period_c = c; |
| 93 | |
| 94 | *prescale = *period_c / 0x10000 + 1; |
| 95 | |
| 96 | *period_c /= *prescale; |
| 97 | c = *period_c * (unsigned long long)duty_ns; |
| 98 | do_div(c, period_ns); |
| 99 | *duty_c = c; |
| 100 | |
| 101 | /* |
| 102 | * according to imx pwm RM, the real period value should be |
| 103 | * PERIOD value in PWMPR plus 2. |
| 104 | */ |
| 105 | if (*period_c > 2) |
| 106 | *period_c -= 2; |
| 107 | else |
| 108 | *period_c = 0; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 113 | int pwm_config(int pwm_id, int duty_ns, int period_ns) |
| 114 | { |
| 115 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 116 | unsigned long period_cycles, duty_cycles, prescale; |
| 117 | |
| 118 | if (!pwm) |
| 119 | return -1; |
| 120 | |
| 121 | pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles, |
| 122 | &prescale); |
| 123 | |
| 124 | return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale); |
| 125 | } |
| 126 | |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 127 | int pwm_enable(int pwm_id) |
| 128 | { |
| 129 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 130 | |
Axel Lin | ecae644 | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 131 | if (!pwm) |
| 132 | return -1; |
| 133 | |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 134 | setbits_le32(&pwm->cr, PWMCR_EN); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | void pwm_disable(int pwm_id) |
| 139 | { |
| 140 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 141 | |
Axel Lin | ecae644 | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 142 | if (!pwm) |
| 143 | return; |
| 144 | |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 145 | clrbits_le32(&pwm->cr, PWMCR_EN); |
| 146 | } |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 147 | |
| 148 | #if defined(CONFIG_DM_PWM) |
| 149 | struct imx_pwm_priv { |
| 150 | struct pwm_regs *regs; |
| 151 | bool invert; |
Tommaso Merciai | 20b9c1d | 2022-03-26 12:19:06 +0100 | [diff] [blame] | 152 | struct clk per_clk; |
| 153 | struct clk ipg_clk; |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
Tommaso Merciai | 42eb296 | 2022-03-26 12:19:07 +0100 | [diff] [blame] | 156 | int pwm_dm_imx_get_parms(struct imx_pwm_priv *priv, int period_ns, |
| 157 | int duty_ns, unsigned long *period_c, unsigned long *duty_c, |
| 158 | unsigned long *prescale) |
| 159 | { |
| 160 | unsigned long long c; |
| 161 | |
| 162 | c = clk_get_rate(&priv->per_clk); |
| 163 | c = c * period_ns; |
| 164 | do_div(c, 1000000000); |
| 165 | *period_c = c; |
| 166 | |
| 167 | *prescale = *period_c / 0x10000 + 1; |
| 168 | |
| 169 | *period_c /= *prescale; |
| 170 | c = *period_c * (unsigned long long)duty_ns; |
| 171 | do_div(c, period_ns); |
| 172 | *duty_c = c; |
| 173 | |
| 174 | /* |
| 175 | * according to imx pwm RM, the real period value should be |
| 176 | * PERIOD value in PWMPR plus 2. |
| 177 | */ |
| 178 | if (*period_c > 2) |
| 179 | *period_c -= 2; |
| 180 | else |
| 181 | *period_c = 0; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 186 | static int imx_pwm_set_invert(struct udevice *dev, uint channel, |
| 187 | bool polarity) |
| 188 | { |
| 189 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 190 | |
| 191 | debug("%s: polarity=%u\n", __func__, polarity); |
| 192 | priv->invert = polarity; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int imx_pwm_set_config(struct udevice *dev, uint channel, |
| 198 | uint period_ns, uint duty_ns) |
| 199 | { |
| 200 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 201 | struct pwm_regs *regs = priv->regs; |
| 202 | unsigned long period_cycles, duty_cycles, prescale; |
| 203 | |
| 204 | debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel); |
| 205 | |
Tommaso Merciai | 42eb296 | 2022-03-26 12:19:07 +0100 | [diff] [blame] | 206 | pwm_dm_imx_get_parms(priv, period_ns, duty_ns, &period_cycles, &duty_cycles, |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 207 | &prescale); |
| 208 | |
| 209 | return pwm_config_internal(regs, period_cycles, duty_cycles, prescale); |
| 210 | }; |
| 211 | |
| 212 | static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable) |
| 213 | { |
| 214 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 215 | struct pwm_regs *regs = priv->regs; |
| 216 | |
| 217 | debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable); |
| 218 | |
| 219 | if (enable) |
| 220 | setbits_le32(®s->cr, PWMCR_EN); |
| 221 | else |
| 222 | clrbits_le32(®s->cr, PWMCR_EN); |
| 223 | |
| 224 | return 0; |
| 225 | }; |
| 226 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 227 | static int imx_pwm_of_to_plat(struct udevice *dev) |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 228 | { |
Tommaso Merciai | 20b9c1d | 2022-03-26 12:19:06 +0100 | [diff] [blame] | 229 | int ret; |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 230 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 231 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 232 | priv->regs = dev_read_addr_ptr(dev); |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 233 | |
Tommaso Merciai | 20b9c1d | 2022-03-26 12:19:06 +0100 | [diff] [blame] | 234 | ret = clk_get_by_name(dev, "per", &priv->per_clk); |
| 235 | if (ret) { |
| 236 | printf("Failed to get per_clk\n"); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk); |
| 241 | if (ret) { |
| 242 | printf("Failed to get ipg_clk\n"); |
| 243 | return ret; |
| 244 | } |
| 245 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int imx_pwm_probe(struct udevice *dev) |
| 250 | { |
Tommaso Merciai | 20b9c1d | 2022-03-26 12:19:06 +0100 | [diff] [blame] | 251 | int ret; |
| 252 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 253 | |
| 254 | ret = clk_enable(&priv->per_clk); |
| 255 | if (ret) { |
| 256 | printf("Failed to enable per_clk\n"); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | ret = clk_enable(&priv->ipg_clk); |
| 261 | if (ret) { |
| 262 | printf("Failed to enable ipg_clk\n"); |
| 263 | return ret; |
| 264 | } |
| 265 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static const struct pwm_ops imx_pwm_ops = { |
| 270 | .set_invert = imx_pwm_set_invert, |
| 271 | .set_config = imx_pwm_set_config, |
| 272 | .set_enable = imx_pwm_set_enable, |
| 273 | }; |
| 274 | |
| 275 | static const struct udevice_id imx_pwm_ids[] = { |
| 276 | { .compatible = "fsl,imx27-pwm" }, |
| 277 | { } |
| 278 | }; |
| 279 | |
| 280 | U_BOOT_DRIVER(imx_pwm) = { |
| 281 | .name = "imx_pwm", |
| 282 | .id = UCLASS_PWM, |
| 283 | .of_match = imx_pwm_ids, |
| 284 | .ops = &imx_pwm_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 285 | .of_to_plat = imx_pwm_of_to_plat, |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 286 | .probe = imx_pwm_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 287 | .priv_auto = sizeof(struct imx_pwm_priv), |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 288 | }; |
| 289 | #endif |