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