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 | |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 9 | #include <div64.h> |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 12 | #include <pwm.h> |
| 13 | #include <asm/arch/imx-regs.h> |
| 14 | #include <asm/io.h> |
Tommaso Merciai | 20b9c1d | 2022-03-26 12:19:06 +0100 | [diff] [blame] | 15 | #include <clk.h> |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 16 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 17 | int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles, |
| 18 | unsigned long duty_cycles, unsigned long prescale) |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 19 | { |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 20 | u32 cr; |
| 21 | |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 22 | writel(0, &pwm->ir); |
Heiko Schocher | 2b38776 | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 23 | cr = PWMCR_PRESCALER(prescale) | |
| 24 | PWMCR_DOZEEN | PWMCR_WAITEN | |
| 25 | PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH; |
| 26 | |
| 27 | writel(cr, &pwm->cr); |
| 28 | /* set duty cycles */ |
| 29 | writel(duty_cycles, &pwm->sar); |
| 30 | /* set period cycles */ |
| 31 | writel(period_cycles, &pwm->pr); |
| 32 | return 0; |
| 33 | } |
| 34 | |
Tommaso Merciai | f57578b | 2022-03-26 12:19:08 +0100 | [diff] [blame] | 35 | #ifndef CONFIG_DM_PWM |
Tommaso Merciai | 8c3b617 | 2022-03-26 12:19:05 +0100 | [diff] [blame] | 36 | /* pwm_id from 0..7 */ |
| 37 | struct pwm_regs *pwm_id_to_reg(int pwm_id) |
| 38 | { |
| 39 | |
| 40 | switch (pwm_id) { |
| 41 | case 0: |
| 42 | return (struct pwm_regs *)PWM1_BASE_ADDR; |
| 43 | case 1: |
| 44 | return (struct pwm_regs *)PWM2_BASE_ADDR; |
| 45 | #ifdef CONFIG_MX6 |
| 46 | case 2: |
| 47 | return (struct pwm_regs *)PWM3_BASE_ADDR; |
| 48 | case 3: |
| 49 | return (struct pwm_regs *)PWM4_BASE_ADDR; |
| 50 | #endif |
| 51 | #ifdef CONFIG_MX6SX |
| 52 | case 4: |
| 53 | return (struct pwm_regs *)PWM5_BASE_ADDR; |
| 54 | case 5: |
| 55 | return (struct pwm_regs *)PWM6_BASE_ADDR; |
| 56 | case 6: |
| 57 | return (struct pwm_regs *)PWM7_BASE_ADDR; |
| 58 | case 7: |
| 59 | return (struct pwm_regs *)PWM8_BASE_ADDR; |
| 60 | #endif |
| 61 | default: |
| 62 | printf("unknown pwm_id: %d\n", pwm_id); |
| 63 | break; |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c, |
| 69 | unsigned long *duty_c, unsigned long *prescale) |
| 70 | { |
| 71 | unsigned long long c; |
| 72 | |
| 73 | /* |
| 74 | * we have not yet a clock framework for imx6, so add the clock |
| 75 | * value here as a define. Replace it when we have the clock |
| 76 | * framework. |
| 77 | */ |
Tom Rini | fbdbe53 | 2022-12-04 10:04:12 -0500 | [diff] [blame] | 78 | c = CFG_IMX6_PWM_PER_CLK; |
Tommaso Merciai | 8c3b617 | 2022-03-26 12:19:05 +0100 | [diff] [blame] | 79 | c = c * period_ns; |
| 80 | do_div(c, 1000000000); |
| 81 | *period_c = c; |
| 82 | |
| 83 | *prescale = *period_c / 0x10000 + 1; |
| 84 | |
| 85 | *period_c /= *prescale; |
| 86 | c = *period_c * (unsigned long long)duty_ns; |
| 87 | do_div(c, period_ns); |
| 88 | *duty_c = c; |
| 89 | |
| 90 | /* |
| 91 | * according to imx pwm RM, the real period value should be |
| 92 | * PERIOD value in PWMPR plus 2. |
| 93 | */ |
| 94 | if (*period_c > 2) |
| 95 | *period_c -= 2; |
| 96 | else |
| 97 | *period_c = 0; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Tommaso Merciai | f57578b | 2022-03-26 12:19:08 +0100 | [diff] [blame] | 102 | int pwm_init(int pwm_id, int div, int invert) |
| 103 | { |
| 104 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 105 | |
| 106 | if (!pwm) |
| 107 | return -1; |
| 108 | |
| 109 | writel(0, &pwm->ir); |
| 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 | |
Tommaso Merciai | f57578b | 2022-03-26 12:19:08 +0100 | [diff] [blame] | 148 | #else |
Heiko Schocher | 00aa066 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 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 |