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> |
Heiko Schocher | b2f97cf | 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> |
| 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 | |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 28 | int pwm_config_internal(struct pwm_regs *pwm, unsigned long period_cycles, |
| 29 | unsigned long duty_cycles, unsigned long prescale) |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 30 | { |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 31 | u32 cr; |
| 32 | |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 33 | writel(0, &pwm->ir); |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 34 | cr = PWMCR_PRESCALER(prescale) | |
| 35 | PWMCR_DOZEEN | PWMCR_WAITEN | |
| 36 | PWMCR_DBGEN | PWMCR_CLKSRC_IPG_HIGH; |
| 37 | |
| 38 | writel(cr, &pwm->cr); |
| 39 | /* set duty cycles */ |
| 40 | writel(duty_cycles, &pwm->sar); |
| 41 | /* set period cycles */ |
| 42 | writel(period_cycles, &pwm->pr); |
| 43 | return 0; |
| 44 | } |
| 45 | |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 46 | int pwm_config(int pwm_id, int duty_ns, int period_ns) |
| 47 | { |
| 48 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 49 | unsigned long period_cycles, duty_cycles, prescale; |
| 50 | |
| 51 | if (!pwm) |
| 52 | return -1; |
| 53 | |
| 54 | pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles, |
| 55 | &prescale); |
| 56 | |
| 57 | return pwm_config_internal(pwm, period_cycles, duty_cycles, prescale); |
| 58 | } |
| 59 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 60 | int pwm_enable(int pwm_id) |
| 61 | { |
| 62 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 63 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 64 | if (!pwm) |
| 65 | return -1; |
| 66 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 67 | setbits_le32(&pwm->cr, PWMCR_EN); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | void pwm_disable(int pwm_id) |
| 72 | { |
| 73 | struct pwm_regs *pwm = (struct pwm_regs *)pwm_id_to_reg(pwm_id); |
| 74 | |
Axel Lin | 16b0c0c | 2015-05-23 15:16:48 +0800 | [diff] [blame] | 75 | if (!pwm) |
| 76 | return; |
| 77 | |
Heiko Schocher | b2f97cf | 2014-07-18 06:07:19 +0200 | [diff] [blame] | 78 | clrbits_le32(&pwm->cr, PWMCR_EN); |
| 79 | } |
Heiko Schocher | 46e10e9 | 2019-05-28 06:51:52 +0200 | [diff] [blame] | 80 | |
| 81 | #if defined(CONFIG_DM_PWM) |
| 82 | struct imx_pwm_priv { |
| 83 | struct pwm_regs *regs; |
| 84 | bool invert; |
| 85 | }; |
| 86 | |
| 87 | static int imx_pwm_set_invert(struct udevice *dev, uint channel, |
| 88 | bool polarity) |
| 89 | { |
| 90 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 91 | |
| 92 | debug("%s: polarity=%u\n", __func__, polarity); |
| 93 | priv->invert = polarity; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int imx_pwm_set_config(struct udevice *dev, uint channel, |
| 99 | uint period_ns, uint duty_ns) |
| 100 | { |
| 101 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 102 | struct pwm_regs *regs = priv->regs; |
| 103 | unsigned long period_cycles, duty_cycles, prescale; |
| 104 | |
| 105 | debug("%s: Config '%s' channel: %d\n", __func__, dev->name, channel); |
| 106 | |
| 107 | pwm_imx_get_parms(period_ns, duty_ns, &period_cycles, &duty_cycles, |
| 108 | &prescale); |
| 109 | |
| 110 | return pwm_config_internal(regs, period_cycles, duty_cycles, prescale); |
| 111 | }; |
| 112 | |
| 113 | static int imx_pwm_set_enable(struct udevice *dev, uint channel, bool enable) |
| 114 | { |
| 115 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 116 | struct pwm_regs *regs = priv->regs; |
| 117 | |
| 118 | debug("%s: Enable '%s' state: %d\n", __func__, dev->name, enable); |
| 119 | |
| 120 | if (enable) |
| 121 | setbits_le32(®s->cr, PWMCR_EN); |
| 122 | else |
| 123 | clrbits_le32(®s->cr, PWMCR_EN); |
| 124 | |
| 125 | return 0; |
| 126 | }; |
| 127 | |
| 128 | static int imx_pwm_ofdata_to_platdata(struct udevice *dev) |
| 129 | { |
| 130 | struct imx_pwm_priv *priv = dev_get_priv(dev); |
| 131 | |
| 132 | priv->regs = (struct pwm_regs *)devfdt_get_addr(dev); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int imx_pwm_probe(struct udevice *dev) |
| 138 | { |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static const struct pwm_ops imx_pwm_ops = { |
| 143 | .set_invert = imx_pwm_set_invert, |
| 144 | .set_config = imx_pwm_set_config, |
| 145 | .set_enable = imx_pwm_set_enable, |
| 146 | }; |
| 147 | |
| 148 | static const struct udevice_id imx_pwm_ids[] = { |
| 149 | { .compatible = "fsl,imx27-pwm" }, |
| 150 | { } |
| 151 | }; |
| 152 | |
| 153 | U_BOOT_DRIVER(imx_pwm) = { |
| 154 | .name = "imx_pwm", |
| 155 | .id = UCLASS_PWM, |
| 156 | .of_match = imx_pwm_ids, |
| 157 | .ops = &imx_pwm_ops, |
| 158 | .ofdata_to_platdata = imx_pwm_ofdata_to_platdata, |
| 159 | .probe = imx_pwm_probe, |
| 160 | .priv_auto_alloc_size = sizeof(struct imx_pwm_priv), |
| 161 | }; |
| 162 | #endif |