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