Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Freescale FlexTimer Module (FTM) PWM Driver |
| 4 | * |
| 5 | * Copyright 2012-2013 Freescale Semiconductor, Inc. |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/io.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/of_address.h> |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 15 | #include <linux/of_device.h> |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 17 | #include <linux/pm.h> |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 18 | #include <linux/pwm.h> |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 20 | #include <linux/slab.h> |
Patrick Havelange | e590eb40 | 2019-04-02 15:30:48 +0900 | [diff] [blame] | 21 | #include <linux/fsl/ftm.h> |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 22 | |
Xiubo Li | cd6d92d | 2014-08-19 12:38:01 +0800 | [diff] [blame] | 23 | #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 24 | |
| 25 | enum fsl_pwm_clk { |
| 26 | FSL_PWM_CLK_SYS, |
| 27 | FSL_PWM_CLK_FIX, |
| 28 | FSL_PWM_CLK_EXT, |
| 29 | FSL_PWM_CLK_CNTEN, |
| 30 | FSL_PWM_CLK_MAX |
| 31 | }; |
| 32 | |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 33 | struct fsl_ftm_soc { |
| 34 | bool has_enable_bits; |
| 35 | }; |
| 36 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 37 | struct fsl_pwm_periodcfg { |
| 38 | enum fsl_pwm_clk clk_select; |
| 39 | unsigned int clk_ps; |
| 40 | unsigned int mod_period; |
| 41 | }; |
| 42 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 43 | struct fsl_pwm_chip { |
| 44 | struct pwm_chip chip; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 45 | struct mutex lock; |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 46 | struct regmap *regmap; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 47 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 48 | /* This value is valid iff a pwm is running */ |
| 49 | struct fsl_pwm_periodcfg period; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 50 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 51 | struct clk *ipg_clk; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 52 | struct clk *clk[FSL_PWM_CLK_MAX]; |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 53 | |
| 54 | const struct fsl_ftm_soc *soc; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip) |
| 58 | { |
| 59 | return container_of(chip, struct fsl_pwm_chip, chip); |
| 60 | } |
| 61 | |
Patrick Havelange | a2a2822 | 2019-06-12 16:12:46 +0200 | [diff] [blame] | 62 | static void ftm_clear_write_protection(struct fsl_pwm_chip *fpc) |
| 63 | { |
| 64 | u32 val; |
| 65 | |
| 66 | regmap_read(fpc->regmap, FTM_FMS, &val); |
| 67 | if (val & FTM_FMS_WPEN) |
| 68 | regmap_update_bits(fpc->regmap, FTM_MODE, FTM_MODE_WPDIS, |
| 69 | FTM_MODE_WPDIS); |
| 70 | } |
| 71 | |
| 72 | static void ftm_set_write_protection(struct fsl_pwm_chip *fpc) |
| 73 | { |
| 74 | regmap_update_bits(fpc->regmap, FTM_FMS, FTM_FMS_WPEN, FTM_FMS_WPEN); |
| 75 | } |
| 76 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 77 | static bool fsl_pwm_periodcfg_are_equal(const struct fsl_pwm_periodcfg *a, |
| 78 | const struct fsl_pwm_periodcfg *b) |
| 79 | { |
| 80 | if (a->clk_select != b->clk_select) |
| 81 | return false; |
| 82 | if (a->clk_ps != b->clk_ps) |
| 83 | return false; |
| 84 | if (a->mod_period != b->mod_period) |
| 85 | return false; |
| 86 | return true; |
| 87 | } |
| 88 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 89 | static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
| 90 | { |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 91 | int ret; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 92 | struct fsl_pwm_chip *fpc = to_fsl_chip(chip); |
| 93 | |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 94 | ret = clk_prepare_enable(fpc->ipg_clk); |
| 95 | if (!ret && fpc->soc->has_enable_bits) { |
| 96 | mutex_lock(&fpc->lock); |
| 97 | regmap_update_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16), |
| 98 | BIT(pwm->hwpwm + 16)); |
| 99 | mutex_unlock(&fpc->lock); |
| 100 | } |
| 101 | |
| 102 | return ret; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 106 | { |
| 107 | struct fsl_pwm_chip *fpc = to_fsl_chip(chip); |
| 108 | |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 109 | if (fpc->soc->has_enable_bits) { |
| 110 | mutex_lock(&fpc->lock); |
| 111 | regmap_update_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16), |
| 112 | 0); |
| 113 | mutex_unlock(&fpc->lock); |
| 114 | } |
| 115 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 116 | clk_disable_unprepare(fpc->ipg_clk); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 117 | } |
| 118 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 119 | static unsigned int fsl_pwm_ticks_to_ns(struct fsl_pwm_chip *fpc, |
| 120 | unsigned int ticks) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 121 | { |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 122 | unsigned long rate; |
| 123 | unsigned long long exval; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 124 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 125 | rate = clk_get_rate(fpc->clk[fpc->period.clk_select]); |
| 126 | exval = ticks; |
| 127 | exval *= 1000000000UL; |
| 128 | do_div(exval, rate >> fpc->period.clk_ps); |
| 129 | return exval; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 130 | } |
| 131 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 132 | static bool fsl_pwm_calculate_period_clk(struct fsl_pwm_chip *fpc, |
| 133 | unsigned int period_ns, |
| 134 | enum fsl_pwm_clk index, |
| 135 | struct fsl_pwm_periodcfg *periodcfg |
| 136 | ) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 137 | { |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 138 | unsigned long long c; |
| 139 | unsigned int ps; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 140 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 141 | c = clk_get_rate(fpc->clk[index]); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 142 | c = c * period_ns; |
| 143 | do_div(c, 1000000000UL); |
| 144 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 145 | if (c == 0) |
| 146 | return false; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 147 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 148 | for (ps = 0; ps < 8 ; ++ps, c >>= 1) { |
| 149 | if (c <= 0x10000) { |
| 150 | periodcfg->clk_select = index; |
| 151 | periodcfg->clk_ps = ps; |
| 152 | periodcfg->mod_period = c - 1; |
| 153 | return true; |
| 154 | } |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 155 | } |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 156 | return false; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 159 | static bool fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc, |
| 160 | unsigned int period_ns, |
| 161 | struct fsl_pwm_periodcfg *periodcfg) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 162 | { |
| 163 | enum fsl_pwm_clk m0, m1; |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 164 | unsigned long fix_rate, ext_rate; |
| 165 | bool ret; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 166 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 167 | ret = fsl_pwm_calculate_period_clk(fpc, period_ns, FSL_PWM_CLK_SYS, |
| 168 | periodcfg); |
| 169 | if (ret) |
| 170 | return true; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 171 | |
| 172 | fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]); |
| 173 | ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]); |
| 174 | |
| 175 | if (fix_rate > ext_rate) { |
| 176 | m0 = FSL_PWM_CLK_FIX; |
| 177 | m1 = FSL_PWM_CLK_EXT; |
| 178 | } else { |
| 179 | m0 = FSL_PWM_CLK_EXT; |
| 180 | m1 = FSL_PWM_CLK_FIX; |
| 181 | } |
| 182 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 183 | ret = fsl_pwm_calculate_period_clk(fpc, period_ns, m0, periodcfg); |
| 184 | if (ret) |
| 185 | return true; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 186 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 187 | return fsl_pwm_calculate_period_clk(fpc, period_ns, m1, periodcfg); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 190 | static unsigned int fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc, |
| 191 | unsigned int duty_ns) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 192 | { |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 193 | unsigned long long duty; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 194 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 195 | unsigned int period = fpc->period.mod_period + 1; |
| 196 | unsigned int period_ns = fsl_pwm_ticks_to_ns(fpc, period); |
| 197 | |
| 198 | duty = (unsigned long long)duty_ns * period; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 199 | do_div(duty, period_ns); |
| 200 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 201 | return (unsigned int)duty; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 204 | static bool fsl_pwm_is_any_pwm_enabled(struct fsl_pwm_chip *fpc, |
| 205 | struct pwm_device *pwm) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 206 | { |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 207 | u32 val; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 208 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 209 | regmap_read(fpc->regmap, FTM_OUTMASK, &val); |
| 210 | if (~val & 0xFF) |
| 211 | return true; |
| 212 | else |
| 213 | return false; |
| 214 | } |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 215 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 216 | static bool fsl_pwm_is_other_pwm_enabled(struct fsl_pwm_chip *fpc, |
| 217 | struct pwm_device *pwm) |
| 218 | { |
| 219 | u32 val; |
| 220 | |
| 221 | regmap_read(fpc->regmap, FTM_OUTMASK, &val); |
| 222 | if (~(val | BIT(pwm->hwpwm)) & 0xFF) |
| 223 | return true; |
| 224 | else |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | static int fsl_pwm_apply_config(struct fsl_pwm_chip *fpc, |
| 229 | struct pwm_device *pwm, |
Uwe Kleine-König | 71523d1 | 2019-08-24 17:37:07 +0200 | [diff] [blame] | 230 | const struct pwm_state *newstate) |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 231 | { |
| 232 | unsigned int duty; |
| 233 | u32 reg_polarity; |
| 234 | |
| 235 | struct fsl_pwm_periodcfg periodcfg; |
| 236 | bool do_write_period = false; |
| 237 | |
| 238 | if (!fsl_pwm_calculate_period(fpc, newstate->period, &periodcfg)) { |
| 239 | dev_err(fpc->chip.dev, "failed to calculate new period\n"); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | if (!fsl_pwm_is_any_pwm_enabled(fpc, pwm)) |
| 244 | do_write_period = true; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 245 | /* |
| 246 | * The Freescale FTM controller supports only a single period for |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 247 | * all PWM channels, therefore verify if the newly computed period |
| 248 | * is different than the current period being used. In such case |
| 249 | * we allow to change the period only if no other pwm is running. |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 250 | */ |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 251 | else if (!fsl_pwm_periodcfg_are_equal(&fpc->period, &periodcfg)) { |
| 252 | if (fsl_pwm_is_other_pwm_enabled(fpc, pwm)) { |
| 253 | dev_err(fpc->chip.dev, |
| 254 | "Cannot change period for PWM %u, disable other PWMs first\n", |
| 255 | pwm->hwpwm); |
| 256 | return -EBUSY; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 257 | } |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 258 | if (fpc->period.clk_select != periodcfg.clk_select) { |
| 259 | int ret; |
| 260 | enum fsl_pwm_clk oldclk = fpc->period.clk_select; |
| 261 | enum fsl_pwm_clk newclk = periodcfg.clk_select; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 262 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 263 | ret = clk_prepare_enable(fpc->clk[newclk]); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | clk_disable_unprepare(fpc->clk[oldclk]); |
| 267 | } |
| 268 | do_write_period = true; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 269 | } |
| 270 | |
Patrick Havelange | a2a2822 | 2019-06-12 16:12:46 +0200 | [diff] [blame] | 271 | ftm_clear_write_protection(fpc); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 272 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 273 | if (do_write_period) { |
| 274 | regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK, |
| 275 | FTM_SC_CLK(periodcfg.clk_select)); |
| 276 | regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK, |
| 277 | periodcfg.clk_ps); |
| 278 | regmap_write(fpc->regmap, FTM_MOD, periodcfg.mod_period); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 279 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 280 | fpc->period = periodcfg; |
| 281 | } |
| 282 | |
| 283 | duty = fsl_pwm_calculate_duty(fpc, newstate->duty_cycle); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 284 | |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 285 | regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm), |
| 286 | FTM_CSC_MSB | FTM_CSC_ELSB); |
| 287 | regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 288 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 289 | reg_polarity = 0; |
| 290 | if (newstate->polarity == PWM_POLARITY_INVERSED) |
| 291 | reg_polarity = BIT(pwm->hwpwm); |
| 292 | |
| 293 | regmap_update_bits(fpc->regmap, FTM_POL, BIT(pwm->hwpwm), reg_polarity); |
| 294 | |
Patrick Havelange | a2a2822 | 2019-06-12 16:12:46 +0200 | [diff] [blame] | 295 | ftm_set_write_protection(fpc); |
| 296 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 300 | static int fsl_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
Uwe Kleine-König | 71523d1 | 2019-08-24 17:37:07 +0200 | [diff] [blame] | 301 | const struct pwm_state *newstate) |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 302 | { |
| 303 | struct fsl_pwm_chip *fpc = to_fsl_chip(chip); |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 304 | struct pwm_state *oldstate = &pwm->state; |
| 305 | int ret = 0; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 306 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 307 | /* |
| 308 | * oldstate to newstate : action |
| 309 | * |
| 310 | * disabled to disabled : ignore |
| 311 | * enabled to disabled : disable |
| 312 | * enabled to enabled : update settings |
| 313 | * disabled to enabled : update settings + enable |
| 314 | */ |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 315 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 316 | mutex_lock(&fpc->lock); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 317 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 318 | if (!newstate->enabled) { |
| 319 | if (oldstate->enabled) { |
| 320 | regmap_update_bits(fpc->regmap, FTM_OUTMASK, |
| 321 | BIT(pwm->hwpwm), BIT(pwm->hwpwm)); |
| 322 | clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]); |
| 323 | clk_disable_unprepare(fpc->clk[fpc->period.clk_select]); |
| 324 | } |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 325 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 326 | goto end_mutex; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 327 | } |
| 328 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 329 | ret = fsl_pwm_apply_config(fpc, pwm, newstate); |
| 330 | if (ret) |
| 331 | goto end_mutex; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 332 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 333 | /* check if need to enable */ |
| 334 | if (!oldstate->enabled) { |
| 335 | ret = clk_prepare_enable(fpc->clk[fpc->period.clk_select]); |
| 336 | if (ret) |
Thierry Reding | 3d25025 | 2019-06-26 11:36:40 +0200 | [diff] [blame] | 337 | goto end_mutex; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 338 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 339 | ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]); |
| 340 | if (ret) { |
| 341 | clk_disable_unprepare(fpc->clk[fpc->period.clk_select]); |
Thierry Reding | 3d25025 | 2019-06-26 11:36:40 +0200 | [diff] [blame] | 342 | goto end_mutex; |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 343 | } |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 344 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 345 | regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), |
| 346 | 0); |
| 347 | } |
| 348 | |
| 349 | end_mutex: |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 350 | mutex_unlock(&fpc->lock); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 351 | return ret; |
| 352 | } |
| 353 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 354 | static const struct pwm_ops fsl_pwm_ops = { |
| 355 | .request = fsl_pwm_request, |
| 356 | .free = fsl_pwm_free, |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 357 | .apply = fsl_pwm_apply, |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 358 | .owner = THIS_MODULE, |
| 359 | }; |
| 360 | |
| 361 | static int fsl_pwm_init(struct fsl_pwm_chip *fpc) |
| 362 | { |
| 363 | int ret; |
| 364 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 365 | ret = clk_prepare_enable(fpc->ipg_clk); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 366 | if (ret) |
| 367 | return ret; |
| 368 | |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 369 | regmap_write(fpc->regmap, FTM_CNTIN, 0x00); |
| 370 | regmap_write(fpc->regmap, FTM_OUTINIT, 0x00); |
| 371 | regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 372 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 373 | clk_disable_unprepare(fpc->ipg_clk); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Xiubo Li | 49599cf | 2014-10-15 13:21:34 +0800 | [diff] [blame] | 378 | static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg) |
| 379 | { |
| 380 | switch (reg) { |
Patrick Havelange | a2a2822 | 2019-06-12 16:12:46 +0200 | [diff] [blame] | 381 | case FTM_FMS: |
| 382 | case FTM_MODE: |
Xiubo Li | 49599cf | 2014-10-15 13:21:34 +0800 | [diff] [blame] | 383 | case FTM_CNT: |
| 384 | return true; |
| 385 | } |
| 386 | return false; |
| 387 | } |
| 388 | |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 389 | static const struct regmap_config fsl_pwm_regmap_config = { |
| 390 | .reg_bits = 32, |
| 391 | .reg_stride = 4, |
| 392 | .val_bits = 32, |
| 393 | |
| 394 | .max_register = FTM_PWMLOAD, |
Xiubo Li | 49599cf | 2014-10-15 13:21:34 +0800 | [diff] [blame] | 395 | .volatile_reg = fsl_pwm_volatile_reg, |
Stefan Agner | ad06fde | 2016-01-20 18:56:22 -0800 | [diff] [blame] | 396 | .cache_type = REGCACHE_FLAT, |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 397 | }; |
| 398 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 399 | static int fsl_pwm_probe(struct platform_device *pdev) |
| 400 | { |
| 401 | struct fsl_pwm_chip *fpc; |
| 402 | struct resource *res; |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 403 | void __iomem *base; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 404 | int ret; |
| 405 | |
| 406 | fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL); |
| 407 | if (!fpc) |
| 408 | return -ENOMEM; |
| 409 | |
| 410 | mutex_init(&fpc->lock); |
| 411 | |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 412 | fpc->soc = of_device_get_match_data(&pdev->dev); |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 413 | fpc->chip.dev = &pdev->dev; |
| 414 | |
| 415 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 416 | base = devm_ioremap_resource(&pdev->dev, res); |
| 417 | if (IS_ERR(base)) |
| 418 | return PTR_ERR(base); |
| 419 | |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 420 | fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base, |
Xiubo Li | 42fa98a | 2014-08-19 12:38:02 +0800 | [diff] [blame] | 421 | &fsl_pwm_regmap_config); |
| 422 | if (IS_ERR(fpc->regmap)) { |
| 423 | dev_err(&pdev->dev, "regmap init failed\n"); |
| 424 | return PTR_ERR(fpc->regmap); |
| 425 | } |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 426 | |
| 427 | fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys"); |
| 428 | if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) { |
| 429 | dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n"); |
| 430 | return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]); |
| 431 | } |
| 432 | |
| 433 | fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix"); |
| 434 | if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX])) |
| 435 | return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]); |
| 436 | |
| 437 | fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext"); |
| 438 | if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT])) |
| 439 | return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]); |
| 440 | |
| 441 | fpc->clk[FSL_PWM_CLK_CNTEN] = |
| 442 | devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en"); |
| 443 | if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN])) |
| 444 | return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]); |
| 445 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 446 | /* |
| 447 | * ipg_clk is the interface clock for the IP. If not provided, use the |
| 448 | * ftm_sys clock as the default. |
| 449 | */ |
| 450 | fpc->ipg_clk = devm_clk_get(&pdev->dev, "ipg"); |
| 451 | if (IS_ERR(fpc->ipg_clk)) |
| 452 | fpc->ipg_clk = fpc->clk[FSL_PWM_CLK_SYS]; |
| 453 | |
| 454 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 455 | fpc->chip.ops = &fsl_pwm_ops; |
| 456 | fpc->chip.of_xlate = of_pwm_xlate_with_flags; |
| 457 | fpc->chip.of_pwm_n_cells = 3; |
| 458 | fpc->chip.base = -1; |
| 459 | fpc->chip.npwm = 8; |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 460 | |
| 461 | ret = pwmchip_add(&fpc->chip); |
| 462 | if (ret < 0) { |
| 463 | dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | platform_set_drvdata(pdev, fpc); |
| 468 | |
| 469 | return fsl_pwm_init(fpc); |
| 470 | } |
| 471 | |
| 472 | static int fsl_pwm_remove(struct platform_device *pdev) |
| 473 | { |
| 474 | struct fsl_pwm_chip *fpc = platform_get_drvdata(pdev); |
| 475 | |
| 476 | return pwmchip_remove(&fpc->chip); |
| 477 | } |
| 478 | |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 479 | #ifdef CONFIG_PM_SLEEP |
| 480 | static int fsl_pwm_suspend(struct device *dev) |
| 481 | { |
| 482 | struct fsl_pwm_chip *fpc = dev_get_drvdata(dev); |
Stefan Agner | 816aec2 | 2015-11-23 14:45:07 -0800 | [diff] [blame] | 483 | int i; |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 484 | |
| 485 | regcache_cache_only(fpc->regmap, true); |
| 486 | regcache_mark_dirty(fpc->regmap); |
| 487 | |
Stefan Agner | 816aec2 | 2015-11-23 14:45:07 -0800 | [diff] [blame] | 488 | for (i = 0; i < fpc->chip.npwm; i++) { |
| 489 | struct pwm_device *pwm = &fpc->chip.pwms[i]; |
| 490 | |
| 491 | if (!test_bit(PWMF_REQUESTED, &pwm->flags)) |
| 492 | continue; |
| 493 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 494 | clk_disable_unprepare(fpc->ipg_clk); |
Stefan Agner | 816aec2 | 2015-11-23 14:45:07 -0800 | [diff] [blame] | 495 | |
| 496 | if (!pwm_is_enabled(pwm)) |
| 497 | continue; |
| 498 | |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 499 | clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]); |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 500 | clk_disable_unprepare(fpc->clk[fpc->period.clk_select]); |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int fsl_pwm_resume(struct device *dev) |
| 507 | { |
| 508 | struct fsl_pwm_chip *fpc = dev_get_drvdata(dev); |
Stefan Agner | 816aec2 | 2015-11-23 14:45:07 -0800 | [diff] [blame] | 509 | int i; |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 510 | |
Stefan Agner | 816aec2 | 2015-11-23 14:45:07 -0800 | [diff] [blame] | 511 | for (i = 0; i < fpc->chip.npwm; i++) { |
| 512 | struct pwm_device *pwm = &fpc->chip.pwms[i]; |
| 513 | |
| 514 | if (!test_bit(PWMF_REQUESTED, &pwm->flags)) |
| 515 | continue; |
| 516 | |
shenwei.wang@nxp.com | 82a9c55 | 2018-06-08 14:22:34 -0500 | [diff] [blame] | 517 | clk_prepare_enable(fpc->ipg_clk); |
Stefan Agner | 816aec2 | 2015-11-23 14:45:07 -0800 | [diff] [blame] | 518 | |
| 519 | if (!pwm_is_enabled(pwm)) |
| 520 | continue; |
| 521 | |
Patrick Havelange | 3479bbd | 2019-06-12 16:12:45 +0200 | [diff] [blame] | 522 | clk_prepare_enable(fpc->clk[fpc->period.clk_select]); |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 523 | clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]); |
| 524 | } |
| 525 | |
| 526 | /* restore all registers from cache */ |
| 527 | regcache_cache_only(fpc->regmap, false); |
| 528 | regcache_sync(fpc->regmap); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | #endif |
| 533 | |
| 534 | static const struct dev_pm_ops fsl_pwm_pm_ops = { |
| 535 | SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume) |
| 536 | }; |
| 537 | |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 538 | static const struct fsl_ftm_soc vf610_ftm_pwm = { |
| 539 | .has_enable_bits = false, |
| 540 | }; |
| 541 | |
shenwei.wang@nxp.com | 2c4f2e3 | 2018-06-08 14:22:36 -0500 | [diff] [blame] | 542 | static const struct fsl_ftm_soc imx8qm_ftm_pwm = { |
| 543 | .has_enable_bits = true, |
| 544 | }; |
| 545 | |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 546 | static const struct of_device_id fsl_pwm_dt_ids[] = { |
shenwei.wang@nxp.com | db6c51a | 2018-06-08 14:22:35 -0500 | [diff] [blame] | 547 | { .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm }, |
shenwei.wang@nxp.com | 2c4f2e3 | 2018-06-08 14:22:36 -0500 | [diff] [blame] | 548 | { .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm }, |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 549 | { /* sentinel */ } |
| 550 | }; |
| 551 | MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids); |
| 552 | |
| 553 | static struct platform_driver fsl_pwm_driver = { |
| 554 | .driver = { |
| 555 | .name = "fsl-ftm-pwm", |
| 556 | .of_match_table = fsl_pwm_dt_ids, |
Xiubo Li | 97d0b42 | 2014-10-15 13:21:35 +0800 | [diff] [blame] | 557 | .pm = &fsl_pwm_pm_ops, |
Xiubo Li | b505183 | 2014-02-27 17:39:49 +0800 | [diff] [blame] | 558 | }, |
| 559 | .probe = fsl_pwm_probe, |
| 560 | .remove = fsl_pwm_remove, |
| 561 | }; |
| 562 | module_platform_driver(fsl_pwm_driver); |
| 563 | |
| 564 | MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver"); |
| 565 | MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>"); |
| 566 | MODULE_ALIAS("platform:fsl-ftm-pwm"); |
| 567 | MODULE_LICENSE("GPL"); |