Fabio Estevam | a99290c | 2018-07-06 19:47:17 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 2 | /* |
| 3 | * simple driver for PWM (Pulse Width Modulator) controller |
| 4 | * |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 5 | * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com> |
Uwe Kleine-König | f696097 | 2019-07-30 14:45:27 +0200 | [diff] [blame] | 6 | * |
| 7 | * Limitations: |
| 8 | * - When disabled the output is driven to 0 independent of the configured |
| 9 | * polarity. |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
Michal Vokáč | 9f617ad | 2018-10-01 16:19:47 +0200 | [diff] [blame] | 12 | #include <linux/bitfield.h> |
| 13 | #include <linux/bitops.h> |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 14 | #include <linux/clk.h> |
Liu Ying | 137fd45 | 2014-05-28 18:50:13 +0800 | [diff] [blame] | 15 | #include <linux/delay.h> |
Michal Vokáč | e3adc7e | 2018-10-01 16:19:46 +0200 | [diff] [blame] | 16 | #include <linux/err.h> |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Michal Vokáč | e3adc7e | 2018-10-01 16:19:46 +0200 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
Sachin Kamat | 2a8876c | 2013-09-27 16:53:23 +0530 | [diff] [blame] | 20 | #include <linux/of.h> |
Michal Vokáč | e3adc7e | 2018-10-01 16:19:46 +0200 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/pwm.h> |
| 23 | #include <linux/slab.h> |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 24 | |
Liu Ying | 40f260c | 2014-05-28 18:50:12 +0800 | [diff] [blame] | 25 | #define MX3_PWMCR 0x00 /* PWM Control Register */ |
Liu Ying | 137fd45 | 2014-05-28 18:50:13 +0800 | [diff] [blame] | 26 | #define MX3_PWMSR 0x04 /* PWM Status Register */ |
Liu Ying | 40f260c | 2014-05-28 18:50:12 +0800 | [diff] [blame] | 27 | #define MX3_PWMSAR 0x0C /* PWM Sample Register */ |
| 28 | #define MX3_PWMPR 0x10 /* PWM Period Register */ |
Michal Vokáč | 9f617ad | 2018-10-01 16:19:47 +0200 | [diff] [blame] | 29 | |
| 30 | #define MX3_PWMCR_FWM GENMASK(27, 26) |
| 31 | #define MX3_PWMCR_STOPEN BIT(25) |
| 32 | #define MX3_PWMCR_DOZEN BIT(24) |
| 33 | #define MX3_PWMCR_WAITEN BIT(23) |
| 34 | #define MX3_PWMCR_DBGEN BIT(22) |
| 35 | #define MX3_PWMCR_BCTR BIT(21) |
| 36 | #define MX3_PWMCR_HCTR BIT(20) |
| 37 | |
| 38 | #define MX3_PWMCR_POUTC GENMASK(19, 18) |
| 39 | #define MX3_PWMCR_POUTC_NORMAL 0 |
| 40 | #define MX3_PWMCR_POUTC_INVERTED 1 |
| 41 | #define MX3_PWMCR_POUTC_OFF 2 |
| 42 | |
| 43 | #define MX3_PWMCR_CLKSRC GENMASK(17, 16) |
| 44 | #define MX3_PWMCR_CLKSRC_OFF 0 |
| 45 | #define MX3_PWMCR_CLKSRC_IPG 1 |
| 46 | #define MX3_PWMCR_CLKSRC_IPG_HIGH 2 |
| 47 | #define MX3_PWMCR_CLKSRC_IPG_32K 3 |
| 48 | |
| 49 | #define MX3_PWMCR_PRESCALER GENMASK(15, 4) |
| 50 | |
| 51 | #define MX3_PWMCR_SWR BIT(3) |
| 52 | |
| 53 | #define MX3_PWMCR_REPEAT GENMASK(2, 1) |
| 54 | #define MX3_PWMCR_REPEAT_1X 0 |
| 55 | #define MX3_PWMCR_REPEAT_2X 1 |
| 56 | #define MX3_PWMCR_REPEAT_4X 2 |
| 57 | #define MX3_PWMCR_REPEAT_8X 3 |
| 58 | |
| 59 | #define MX3_PWMCR_EN BIT(0) |
| 60 | |
| 61 | #define MX3_PWMSR_FWE BIT(6) |
| 62 | #define MX3_PWMSR_CMP BIT(5) |
| 63 | #define MX3_PWMSR_ROV BIT(4) |
| 64 | #define MX3_PWMSR_FE BIT(3) |
| 65 | |
| 66 | #define MX3_PWMSR_FIFOAV GENMASK(2, 0) |
| 67 | #define MX3_PWMSR_FIFOAV_EMPTY 0 |
| 68 | #define MX3_PWMSR_FIFOAV_1WORD 1 |
| 69 | #define MX3_PWMSR_FIFOAV_2WORDS 2 |
| 70 | #define MX3_PWMSR_FIFOAV_3WORDS 3 |
| 71 | #define MX3_PWMSR_FIFOAV_4WORDS 4 |
| 72 | |
| 73 | #define MX3_PWMCR_PRESCALER_SET(x) FIELD_PREP(MX3_PWMCR_PRESCALER, (x) - 1) |
| 74 | #define MX3_PWMCR_PRESCALER_GET(x) (FIELD_GET(MX3_PWMCR_PRESCALER, \ |
| 75 | (x)) + 1) |
Liu Ying | 137fd45 | 2014-05-28 18:50:13 +0800 | [diff] [blame] | 76 | |
| 77 | #define MX3_PWM_SWR_LOOP 5 |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 78 | |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 79 | /* PWMPR register value of 0xffff has the same effect as 0xfffe */ |
| 80 | #define MX3_PWMPR_MAX 0xfffe |
| 81 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 82 | struct pwm_imx27_chip { |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 83 | struct clk *clk_ipg; |
Philipp Zabel | 7b27c16 | 2012-06-25 16:15:20 +0200 | [diff] [blame] | 84 | struct clk *clk_per; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 85 | void __iomem *mmio_base; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 86 | struct pwm_chip chip; |
Thierry Reding | a3597d6 | 2019-10-17 12:56:00 +0200 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * The driver cannot read the current duty cycle from the hardware if |
| 90 | * the hardware is disabled. Cache the last programmed duty cycle |
| 91 | * value to return in that case. |
| 92 | */ |
| 93 | unsigned int duty_cycle; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 96 | #define to_pwm_imx27_chip(chip) container_of(chip, struct pwm_imx27_chip, chip) |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 97 | |
Uwe Kleine-König | aad4e53 | 2020-02-10 22:22:38 +0100 | [diff] [blame] | 98 | static int pwm_imx27_clk_prepare_enable(struct pwm_imx27_chip *imx) |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 99 | { |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 100 | int ret; |
| 101 | |
| 102 | ret = clk_prepare_enable(imx->clk_ipg); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | ret = clk_prepare_enable(imx->clk_per); |
| 107 | if (ret) { |
| 108 | clk_disable_unprepare(imx->clk_ipg); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Uwe Kleine-König | aad4e53 | 2020-02-10 22:22:38 +0100 | [diff] [blame] | 115 | static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 116 | { |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 117 | clk_disable_unprepare(imx->clk_per); |
| 118 | clk_disable_unprepare(imx->clk_ipg); |
| 119 | } |
| 120 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 121 | static void pwm_imx27_get_state(struct pwm_chip *chip, |
| 122 | struct pwm_device *pwm, struct pwm_state *state) |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 123 | { |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 124 | struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); |
Dan Carpenter | 7ca17b2 | 2019-01-09 11:27:47 +0300 | [diff] [blame] | 125 | u32 period, prescaler, pwm_clk, val; |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 126 | u64 tmp; |
Dan Carpenter | 7ca17b2 | 2019-01-09 11:27:47 +0300 | [diff] [blame] | 127 | int ret; |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 128 | |
Uwe Kleine-König | aad4e53 | 2020-02-10 22:22:38 +0100 | [diff] [blame] | 129 | ret = pwm_imx27_clk_prepare_enable(imx); |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 130 | if (ret < 0) |
| 131 | return; |
| 132 | |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 133 | val = readl(imx->mmio_base + MX3_PWMCR); |
| 134 | |
Uwe Kleine-König | 519ef9b | 2019-01-10 20:33:53 +0100 | [diff] [blame] | 135 | if (val & MX3_PWMCR_EN) |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 136 | state->enabled = true; |
Uwe Kleine-König | 519ef9b | 2019-01-10 20:33:53 +0100 | [diff] [blame] | 137 | else |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 138 | state->enabled = false; |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 139 | |
| 140 | switch (FIELD_GET(MX3_PWMCR_POUTC, val)) { |
| 141 | case MX3_PWMCR_POUTC_NORMAL: |
| 142 | state->polarity = PWM_POLARITY_NORMAL; |
| 143 | break; |
| 144 | case MX3_PWMCR_POUTC_INVERTED: |
| 145 | state->polarity = PWM_POLARITY_INVERSED; |
| 146 | break; |
| 147 | default: |
| 148 | dev_warn(chip->dev, "can't set polarity, output disconnected"); |
| 149 | } |
| 150 | |
| 151 | prescaler = MX3_PWMCR_PRESCALER_GET(val); |
| 152 | pwm_clk = clk_get_rate(imx->clk_per); |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 153 | val = readl(imx->mmio_base + MX3_PWMPR); |
| 154 | period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val; |
| 155 | |
| 156 | /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */ |
Uwe Kleine-König | aef1a37 | 2020-04-16 10:02:45 +0200 | [diff] [blame] | 157 | tmp = NSEC_PER_SEC * (u64)(period + 2) * prescaler; |
| 158 | state->period = DIV_ROUND_UP_ULL(tmp, pwm_clk); |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 159 | |
Thierry Reding | a3597d6 | 2019-10-17 12:56:00 +0200 | [diff] [blame] | 160 | /* |
| 161 | * PWMSAR can be read only if PWM is enabled. If the PWM is disabled, |
| 162 | * use the cached value. |
| 163 | */ |
| 164 | if (state->enabled) |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 165 | val = readl(imx->mmio_base + MX3_PWMSAR); |
Thierry Reding | a3597d6 | 2019-10-17 12:56:00 +0200 | [diff] [blame] | 166 | else |
| 167 | val = imx->duty_cycle; |
| 168 | |
Uwe Kleine-König | aef1a37 | 2020-04-16 10:02:45 +0200 | [diff] [blame] | 169 | tmp = NSEC_PER_SEC * (u64)(val) * prescaler; |
| 170 | state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk); |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 171 | |
Uwe Kleine-König | 2cb5cd9 | 2020-02-10 22:22:40 +0100 | [diff] [blame] | 172 | pwm_imx27_clk_disable_unprepare(imx); |
Michal Vokáč | bf9b0b1 | 2018-10-01 16:19:48 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 175 | static void pwm_imx27_sw_reset(struct pwm_chip *chip) |
Sascha Hauer | 19e7333 | 2012-07-03 17:28:14 +0200 | [diff] [blame] | 176 | { |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 177 | struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); |
Lukasz Majewski | 970247a | 2017-01-29 22:54:09 +0100 | [diff] [blame] | 178 | struct device *dev = chip->dev; |
| 179 | int wait_count = 0; |
| 180 | u32 cr; |
| 181 | |
| 182 | writel(MX3_PWMCR_SWR, imx->mmio_base + MX3_PWMCR); |
| 183 | do { |
| 184 | usleep_range(200, 1000); |
| 185 | cr = readl(imx->mmio_base + MX3_PWMCR); |
| 186 | } while ((cr & MX3_PWMCR_SWR) && |
| 187 | (wait_count++ < MX3_PWM_SWR_LOOP)); |
| 188 | |
| 189 | if (cr & MX3_PWMCR_SWR) |
| 190 | dev_warn(dev, "software reset timeout\n"); |
| 191 | } |
| 192 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 193 | static void pwm_imx27_wait_fifo_slot(struct pwm_chip *chip, |
| 194 | struct pwm_device *pwm) |
Lukasz Majewski | 73b1ff1 | 2017-01-29 22:54:10 +0100 | [diff] [blame] | 195 | { |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 196 | struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); |
Lukasz Majewski | 73b1ff1 | 2017-01-29 22:54:10 +0100 | [diff] [blame] | 197 | struct device *dev = chip->dev; |
| 198 | unsigned int period_ms; |
| 199 | int fifoav; |
| 200 | u32 sr; |
| 201 | |
| 202 | sr = readl(imx->mmio_base + MX3_PWMSR); |
Michal Vokáč | 9f617ad | 2018-10-01 16:19:47 +0200 | [diff] [blame] | 203 | fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr); |
Lukasz Majewski | 73b1ff1 | 2017-01-29 22:54:10 +0100 | [diff] [blame] | 204 | if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) { |
Guru Das Srinagesh | 1689dcd | 2020-06-02 15:31:11 -0700 | [diff] [blame] | 205 | period_ms = DIV_ROUND_UP_ULL(pwm_get_period(pwm), |
Lukasz Majewski | 73b1ff1 | 2017-01-29 22:54:10 +0100 | [diff] [blame] | 206 | NSEC_PER_MSEC); |
| 207 | msleep(period_ms); |
| 208 | |
| 209 | sr = readl(imx->mmio_base + MX3_PWMSR); |
Michal Vokáč | 9f617ad | 2018-10-01 16:19:47 +0200 | [diff] [blame] | 210 | if (fifoav == FIELD_GET(MX3_PWMSR_FIFOAV, sr)) |
Lukasz Majewski | 73b1ff1 | 2017-01-29 22:54:10 +0100 | [diff] [blame] | 211 | dev_warn(dev, "there is no free FIFO slot\n"); |
| 212 | } |
| 213 | } |
Lukasz Majewski | 970247a | 2017-01-29 22:54:09 +0100 | [diff] [blame] | 214 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 215 | static int pwm_imx27_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
Uwe Kleine-König | 71523d1 | 2019-08-24 17:37:07 +0200 | [diff] [blame] | 216 | const struct pwm_state *state) |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 217 | { |
| 218 | unsigned long period_cycles, duty_cycles, prescale; |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 219 | struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 220 | struct pwm_state cstate; |
| 221 | unsigned long long c; |
Uwe Kleine-König | aef1a37 | 2020-04-16 10:02:45 +0200 | [diff] [blame] | 222 | unsigned long long clkrate; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 223 | int ret; |
Lukasz Majewski | 326ed31 | 2017-01-29 22:54:15 +0100 | [diff] [blame] | 224 | u32 cr; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 225 | |
| 226 | pwm_get_state(pwm, &cstate); |
| 227 | |
Uwe Kleine-König | aef1a37 | 2020-04-16 10:02:45 +0200 | [diff] [blame] | 228 | clkrate = clk_get_rate(imx->clk_per); |
| 229 | c = clkrate * state->period; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 230 | |
Uwe Kleine-König | aef1a37 | 2020-04-16 10:02:45 +0200 | [diff] [blame] | 231 | do_div(c, NSEC_PER_SEC); |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 232 | period_cycles = c; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 233 | |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 234 | prescale = period_cycles / 0x10000 + 1; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 235 | |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 236 | period_cycles /= prescale; |
Uwe Kleine-König | aef1a37 | 2020-04-16 10:02:45 +0200 | [diff] [blame] | 237 | c = clkrate * state->duty_cycle; |
Uwe Kleine-König | 1ce6539 | 2020-12-07 15:13:24 +0100 | [diff] [blame] | 238 | do_div(c, NSEC_PER_SEC); |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 239 | duty_cycles = c; |
Uwe Kleine-König | 1ce6539 | 2020-12-07 15:13:24 +0100 | [diff] [blame] | 240 | duty_cycles /= prescale; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 241 | |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 242 | /* |
| 243 | * according to imx pwm RM, the real period value should be PERIOD |
| 244 | * value in PWMPR plus 2. |
| 245 | */ |
| 246 | if (period_cycles > 2) |
| 247 | period_cycles -= 2; |
| 248 | else |
| 249 | period_cycles = 0; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 250 | |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 251 | /* |
| 252 | * Wait for a free FIFO slot if the PWM is already enabled, and flush |
| 253 | * the FIFO if the PWM was disabled and is about to be enabled. |
| 254 | */ |
| 255 | if (cstate.enabled) { |
| 256 | pwm_imx27_wait_fifo_slot(chip, pwm); |
| 257 | } else { |
Uwe Kleine-König | aad4e53 | 2020-02-10 22:22:38 +0100 | [diff] [blame] | 258 | ret = pwm_imx27_clk_prepare_enable(imx); |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 259 | if (ret) |
| 260 | return ret; |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 261 | |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 262 | pwm_imx27_sw_reset(chip); |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 265 | writel(duty_cycles, imx->mmio_base + MX3_PWMSAR); |
| 266 | writel(period_cycles, imx->mmio_base + MX3_PWMPR); |
| 267 | |
| 268 | /* |
| 269 | * Store the duty cycle for future reference in cases where the |
| 270 | * MX3_PWMSAR register can't be read (i.e. when the PWM is disabled). |
| 271 | */ |
| 272 | imx->duty_cycle = duty_cycles; |
| 273 | |
| 274 | cr = MX3_PWMCR_PRESCALER_SET(prescale) | |
| 275 | MX3_PWMCR_STOPEN | MX3_PWMCR_DOZEN | MX3_PWMCR_WAITEN | |
| 276 | FIELD_PREP(MX3_PWMCR_CLKSRC, MX3_PWMCR_CLKSRC_IPG_HIGH) | |
| 277 | MX3_PWMCR_DBGEN; |
| 278 | |
| 279 | if (state->polarity == PWM_POLARITY_INVERSED) |
| 280 | cr |= FIELD_PREP(MX3_PWMCR_POUTC, |
| 281 | MX3_PWMCR_POUTC_INVERTED); |
| 282 | |
| 283 | if (state->enabled) |
| 284 | cr |= MX3_PWMCR_EN; |
| 285 | |
| 286 | writel(cr, imx->mmio_base + MX3_PWMCR); |
| 287 | |
Uwe Kleine-König | 15d4dbd | 2020-02-09 22:31:06 +0100 | [diff] [blame] | 288 | if (!state->enabled) |
Uwe Kleine-König | aad4e53 | 2020-02-10 22:22:38 +0100 | [diff] [blame] | 289 | pwm_imx27_clk_disable_unprepare(imx); |
Thierry Reding | bd88d31 | 2019-10-17 17:11:41 +0200 | [diff] [blame] | 290 | |
Lukasz Majewski | 0ca1a11 | 2017-01-29 22:54:11 +0100 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 294 | static const struct pwm_ops pwm_imx27_ops = { |
| 295 | .apply = pwm_imx27_apply, |
| 296 | .get_state = pwm_imx27_get_state, |
Lukasz Majewski | 0038922 | 2017-01-29 22:54:07 +0100 | [diff] [blame] | 297 | .owner = THIS_MODULE, |
| 298 | }; |
| 299 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 300 | static const struct of_device_id pwm_imx27_dt_ids[] = { |
| 301 | { .compatible = "fsl,imx27-pwm", }, |
Philipp Zabel | 479e2e3 | 2012-06-25 16:16:25 +0200 | [diff] [blame] | 302 | { /* sentinel */ } |
| 303 | }; |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 304 | MODULE_DEVICE_TABLE(of, pwm_imx27_dt_ids); |
Philipp Zabel | 479e2e3 | 2012-06-25 16:16:25 +0200 | [diff] [blame] | 305 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 306 | static int pwm_imx27_probe(struct platform_device *pdev) |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 307 | { |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 308 | struct pwm_imx27_chip *imx; |
Uwe Kleine-König | 2cb5cd9 | 2020-02-10 22:22:40 +0100 | [diff] [blame] | 309 | int ret; |
| 310 | u32 pwmcr; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 311 | |
Axel Lin | a9970e3 | 2012-07-01 08:27:23 +0800 | [diff] [blame] | 312 | imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL); |
Jingoo Han | 1cbec74 | 2014-04-23 18:39:49 +0900 | [diff] [blame] | 313 | if (imx == NULL) |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 314 | return -ENOMEM; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 315 | |
Uwe Kleine-König | f20b187 | 2019-01-07 20:53:50 +0100 | [diff] [blame] | 316 | platform_set_drvdata(pdev, imx); |
| 317 | |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 318 | imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
Anson Huang | d109d74 | 2020-08-11 14:24:31 +0800 | [diff] [blame] | 319 | if (IS_ERR(imx->clk_ipg)) |
| 320 | return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_ipg), |
| 321 | "getting ipg clock failed\n"); |
Anson Huang | 9f4c8f9 | 2018-12-19 05:24:58 +0000 | [diff] [blame] | 322 | |
Philipp Zabel | 7b27c16 | 2012-06-25 16:15:20 +0200 | [diff] [blame] | 323 | imx->clk_per = devm_clk_get(&pdev->dev, "per"); |
Anson Huang | d109d74 | 2020-08-11 14:24:31 +0800 | [diff] [blame] | 324 | if (IS_ERR(imx->clk_per)) |
| 325 | return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per), |
| 326 | "failed to get peripheral clock\n"); |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 327 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 328 | imx->chip.ops = &pwm_imx27_ops; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 329 | imx->chip.dev = &pdev->dev; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 330 | imx->chip.npwm = 1; |
| 331 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 332 | imx->chip.of_xlate = of_pwm_xlate_with_flags; |
| 333 | imx->chip.of_pwm_n_cells = 3; |
Lukasz Majewski | 326ed31 | 2017-01-29 22:54:15 +0100 | [diff] [blame] | 334 | |
Anson Huang | 1347c94 | 2019-04-01 05:24:02 +0000 | [diff] [blame] | 335 | imx->mmio_base = devm_platform_ioremap_resource(pdev, 0); |
Thierry Reding | 6d4294d | 2013-01-21 11:09:16 +0100 | [diff] [blame] | 336 | if (IS_ERR(imx->mmio_base)) |
| 337 | return PTR_ERR(imx->mmio_base); |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 338 | |
Uwe Kleine-König | 2cb5cd9 | 2020-02-10 22:22:40 +0100 | [diff] [blame] | 339 | ret = pwm_imx27_clk_prepare_enable(imx); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | /* keep clks on if pwm is running */ |
| 344 | pwmcr = readl(imx->mmio_base + MX3_PWMCR); |
| 345 | if (!(pwmcr & MX3_PWMCR_EN)) |
| 346 | pwm_imx27_clk_disable_unprepare(imx); |
| 347 | |
Uwe Kleine-König | f20b187 | 2019-01-07 20:53:50 +0100 | [diff] [blame] | 348 | return pwmchip_add(&imx->chip); |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 351 | static int pwm_imx27_remove(struct platform_device *pdev) |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 352 | { |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 353 | struct pwm_imx27_chip *imx; |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 354 | |
| 355 | imx = platform_get_drvdata(pdev); |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 356 | |
Axel Lin | a9970e3 | 2012-07-01 08:27:23 +0800 | [diff] [blame] | 357 | return pwmchip_remove(&imx->chip); |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static struct platform_driver imx_pwm_driver = { |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 361 | .driver = { |
| 362 | .name = "pwm-imx27", |
| 363 | .of_match_table = pwm_imx27_dt_ids, |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 364 | }, |
Uwe Kleine-König | d80f820 | 2019-01-07 20:53:52 +0100 | [diff] [blame] | 365 | .probe = pwm_imx27_probe, |
| 366 | .remove = pwm_imx27_remove, |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 367 | }; |
Sascha Hauer | 208d038 | 2012-08-28 08:27:40 +0200 | [diff] [blame] | 368 | module_platform_driver(imx_pwm_driver); |
Sascha Hauer | 2969324 | 2012-03-15 10:04:35 +0100 | [diff] [blame] | 369 | |
| 370 | MODULE_LICENSE("GPL v2"); |
| 371 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |