blob: efc226bda2146c01aa550ab910597f360e9ce2e0 [file] [log] [blame]
Xiubo Lib5051832014-02-27 17:39:49 +08001/*
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.comdb6c51a2018-06-08 14:22:35 -050019#include <linux/of_device.h>
Xiubo Lib5051832014-02-27 17:39:49 +080020#include <linux/platform_device.h>
Xiubo Li97d0b422014-10-15 13:21:35 +080021#include <linux/pm.h>
Xiubo Lib5051832014-02-27 17:39:49 +080022#include <linux/pwm.h>
Xiubo Li42fa98a2014-08-19 12:38:02 +080023#include <linux/regmap.h>
Xiubo Lib5051832014-02-27 17:39:49 +080024#include <linux/slab.h>
Patrick Havelangee590eb402019-04-02 15:30:48 +090025#include <linux/fsl/ftm.h>
Xiubo Lib5051832014-02-27 17:39:49 +080026
Xiubo Licd6d92d2014-08-19 12:38:01 +080027#define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
Xiubo Lib5051832014-02-27 17:39:49 +080028
29enum 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.comdb6c51a2018-06-08 14:22:35 -050037struct fsl_ftm_soc {
38 bool has_enable_bits;
39};
40
Patrick Havelange3479bbd2019-06-12 16:12:45 +020041struct fsl_pwm_periodcfg {
42 enum fsl_pwm_clk clk_select;
43 unsigned int clk_ps;
44 unsigned int mod_period;
45};
46
Xiubo Lib5051832014-02-27 17:39:49 +080047struct fsl_pwm_chip {
48 struct pwm_chip chip;
Xiubo Lib5051832014-02-27 17:39:49 +080049 struct mutex lock;
Xiubo Li42fa98a2014-08-19 12:38:02 +080050 struct regmap *regmap;
Xiubo Lib5051832014-02-27 17:39:49 +080051
Patrick Havelange3479bbd2019-06-12 16:12:45 +020052 /* This value is valid iff a pwm is running */
53 struct fsl_pwm_periodcfg period;
Xiubo Lib5051832014-02-27 17:39:49 +080054
shenwei.wang@nxp.com82a9c552018-06-08 14:22:34 -050055 struct clk *ipg_clk;
Xiubo Lib5051832014-02-27 17:39:49 +080056 struct clk *clk[FSL_PWM_CLK_MAX];
shenwei.wang@nxp.comdb6c51a2018-06-08 14:22:35 -050057
58 const struct fsl_ftm_soc *soc;
Xiubo Lib5051832014-02-27 17:39:49 +080059};
60
61static 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 Havelangea2a28222019-06-12 16:12:46 +020066static 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
76static 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 Havelange3479bbd2019-06-12 16:12:45 +020081static 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 Lib5051832014-02-27 17:39:49 +080093static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
94{
shenwei.wang@nxp.comdb6c51a2018-06-08 14:22:35 -050095 int ret;
Xiubo Lib5051832014-02-27 17:39:49 +080096 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
97
shenwei.wang@nxp.comdb6c51a2018-06-08 14:22:35 -050098 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 Lib5051832014-02-27 17:39:49 +0800107}
108
109static 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.comdb6c51a2018-06-08 14:22:35 -0500113 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.com82a9c552018-06-08 14:22:34 -0500120 clk_disable_unprepare(fpc->ipg_clk);
Xiubo Lib5051832014-02-27 17:39:49 +0800121}
122
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200123static unsigned int fsl_pwm_ticks_to_ns(struct fsl_pwm_chip *fpc,
124 unsigned int ticks)
Xiubo Lib5051832014-02-27 17:39:49 +0800125{
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200126 unsigned long rate;
127 unsigned long long exval;
Xiubo Lib5051832014-02-27 17:39:49 +0800128
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200129 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 Lib5051832014-02-27 17:39:49 +0800134}
135
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200136static 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 Lib5051832014-02-27 17:39:49 +0800141{
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200142 unsigned long long c;
143 unsigned int ps;
Xiubo Lib5051832014-02-27 17:39:49 +0800144
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200145 c = clk_get_rate(fpc->clk[index]);
Xiubo Lib5051832014-02-27 17:39:49 +0800146 c = c * period_ns;
147 do_div(c, 1000000000UL);
148
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200149 if (c == 0)
150 return false;
Xiubo Lib5051832014-02-27 17:39:49 +0800151
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200152 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 Lib5051832014-02-27 17:39:49 +0800159 }
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200160 return false;
Xiubo Lib5051832014-02-27 17:39:49 +0800161}
162
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200163static bool fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
164 unsigned int period_ns,
165 struct fsl_pwm_periodcfg *periodcfg)
Xiubo Lib5051832014-02-27 17:39:49 +0800166{
167 enum fsl_pwm_clk m0, m1;
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200168 unsigned long fix_rate, ext_rate;
169 bool ret;
Xiubo Lib5051832014-02-27 17:39:49 +0800170
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200171 ret = fsl_pwm_calculate_period_clk(fpc, period_ns, FSL_PWM_CLK_SYS,
172 periodcfg);
173 if (ret)
174 return true;
Xiubo Lib5051832014-02-27 17:39:49 +0800175
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 Havelange3479bbd2019-06-12 16:12:45 +0200187 ret = fsl_pwm_calculate_period_clk(fpc, period_ns, m0, periodcfg);
188 if (ret)
189 return true;
Xiubo Lib5051832014-02-27 17:39:49 +0800190
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200191 return fsl_pwm_calculate_period_clk(fpc, period_ns, m1, periodcfg);
Xiubo Lib5051832014-02-27 17:39:49 +0800192}
193
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200194static unsigned int fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
195 unsigned int duty_ns)
Xiubo Lib5051832014-02-27 17:39:49 +0800196{
Xiubo Li42fa98a2014-08-19 12:38:02 +0800197 unsigned long long duty;
Xiubo Lib5051832014-02-27 17:39:49 +0800198
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200199 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 Lib5051832014-02-27 17:39:49 +0800203 do_div(duty, period_ns);
204
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200205 return (unsigned int)duty;
Xiubo Lib5051832014-02-27 17:39:49 +0800206}
207
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200208static bool fsl_pwm_is_any_pwm_enabled(struct fsl_pwm_chip *fpc,
209 struct pwm_device *pwm)
Xiubo Lib5051832014-02-27 17:39:49 +0800210{
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200211 u32 val;
Xiubo Lib5051832014-02-27 17:39:49 +0800212
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200213 regmap_read(fpc->regmap, FTM_OUTMASK, &val);
214 if (~val & 0xFF)
215 return true;
216 else
217 return false;
218}
Xiubo Lib5051832014-02-27 17:39:49 +0800219
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200220static 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
232static 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 Lib5051832014-02-27 17:39:49 +0800249 /*
250 * The Freescale FTM controller supports only a single period for
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200251 * 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 Lib5051832014-02-27 17:39:49 +0800254 */
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200255 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 Lib5051832014-02-27 17:39:49 +0800261 }
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200262 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 Lib5051832014-02-27 17:39:49 +0800266
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200267 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 Lib5051832014-02-27 17:39:49 +0800273 }
274
Patrick Havelangea2a28222019-06-12 16:12:46 +0200275 ftm_clear_write_protection(fpc);
276
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200277 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 Lib5051832014-02-27 17:39:49 +0800283
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200284 fpc->period = periodcfg;
285 }
286
287 duty = fsl_pwm_calculate_duty(fpc, newstate->duty_cycle);
Xiubo Lib5051832014-02-27 17:39:49 +0800288
Xiubo Li42fa98a2014-08-19 12:38:02 +0800289 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 Lib5051832014-02-27 17:39:49 +0800292
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200293 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 Havelangea2a28222019-06-12 16:12:46 +0200303 ftm_set_write_protection(fpc);
304
Xiubo Lib5051832014-02-27 17:39:49 +0800305 return 0;
306}
307
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200308static int fsl_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
309 struct pwm_state *newstate)
Xiubo Lib5051832014-02-27 17:39:49 +0800310{
311 struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200312 struct pwm_state *oldstate = &pwm->state;
313 int ret = 0;
Xiubo Lib5051832014-02-27 17:39:49 +0800314
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200315 /*
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 Lib5051832014-02-27 17:39:49 +0800323
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200324 mutex_lock(&fpc->lock);
Xiubo Lib5051832014-02-27 17:39:49 +0800325
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200326 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 Lib5051832014-02-27 17:39:49 +0800333
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200334 goto end_mutex;
Xiubo Lib5051832014-02-27 17:39:49 +0800335 }
336
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200337 ret = fsl_pwm_apply_config(fpc, pwm, newstate);
338 if (ret)
339 goto end_mutex;
Xiubo Lib5051832014-02-27 17:39:49 +0800340
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200341 /* check if need to enable */
342 if (!oldstate->enabled) {
343 ret = clk_prepare_enable(fpc->clk[fpc->period.clk_select]);
344 if (ret)
Thierry Reding3d250252019-06-26 11:36:40 +0200345 goto end_mutex;
Xiubo Lib5051832014-02-27 17:39:49 +0800346
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200347 ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
348 if (ret) {
349 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
Thierry Reding3d250252019-06-26 11:36:40 +0200350 goto end_mutex;
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200351 }
Xiubo Lib5051832014-02-27 17:39:49 +0800352
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200353 regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm),
354 0);
355 }
356
357end_mutex:
Xiubo Lib5051832014-02-27 17:39:49 +0800358 mutex_unlock(&fpc->lock);
Xiubo Lib5051832014-02-27 17:39:49 +0800359 return ret;
360}
361
Xiubo Lib5051832014-02-27 17:39:49 +0800362static const struct pwm_ops fsl_pwm_ops = {
363 .request = fsl_pwm_request,
364 .free = fsl_pwm_free,
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200365 .apply = fsl_pwm_apply,
Xiubo Lib5051832014-02-27 17:39:49 +0800366 .owner = THIS_MODULE,
367};
368
369static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
370{
371 int ret;
372
shenwei.wang@nxp.com82a9c552018-06-08 14:22:34 -0500373 ret = clk_prepare_enable(fpc->ipg_clk);
Xiubo Lib5051832014-02-27 17:39:49 +0800374 if (ret)
375 return ret;
376
Xiubo Li42fa98a2014-08-19 12:38:02 +0800377 regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
378 regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
379 regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
Xiubo Lib5051832014-02-27 17:39:49 +0800380
shenwei.wang@nxp.com82a9c552018-06-08 14:22:34 -0500381 clk_disable_unprepare(fpc->ipg_clk);
Xiubo Lib5051832014-02-27 17:39:49 +0800382
383 return 0;
384}
385
Xiubo Li49599cf2014-10-15 13:21:34 +0800386static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
387{
388 switch (reg) {
Patrick Havelangea2a28222019-06-12 16:12:46 +0200389 case FTM_FMS:
390 case FTM_MODE:
Xiubo Li49599cf2014-10-15 13:21:34 +0800391 case FTM_CNT:
392 return true;
393 }
394 return false;
395}
396
Xiubo Li42fa98a2014-08-19 12:38:02 +0800397static 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 Li49599cf2014-10-15 13:21:34 +0800403 .volatile_reg = fsl_pwm_volatile_reg,
Stefan Agnerad06fde2016-01-20 18:56:22 -0800404 .cache_type = REGCACHE_FLAT,
Xiubo Li42fa98a2014-08-19 12:38:02 +0800405};
406
Xiubo Lib5051832014-02-27 17:39:49 +0800407static int fsl_pwm_probe(struct platform_device *pdev)
408{
409 struct fsl_pwm_chip *fpc;
410 struct resource *res;
Xiubo Li42fa98a2014-08-19 12:38:02 +0800411 void __iomem *base;
Xiubo Lib5051832014-02-27 17:39:49 +0800412 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.comdb6c51a2018-06-08 14:22:35 -0500420 fpc->soc = of_device_get_match_data(&pdev->dev);
Xiubo Lib5051832014-02-27 17:39:49 +0800421 fpc->chip.dev = &pdev->dev;
422
423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Xiubo Li42fa98a2014-08-19 12:38:02 +0800424 base = devm_ioremap_resource(&pdev->dev, res);
425 if (IS_ERR(base))
426 return PTR_ERR(base);
427
Xiubo Li97d0b422014-10-15 13:21:35 +0800428 fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
Xiubo Li42fa98a2014-08-19 12:38:02 +0800429 &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 Lib5051832014-02-27 17:39:49 +0800434
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.com82a9c552018-06-08 14:22:34 -0500454 /*
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 Lib5051832014-02-27 17:39:49 +0800463 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 Lib5051832014-02-27 17:39:49 +0800468
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
480static 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 Li97d0b422014-10-15 13:21:35 +0800487#ifdef CONFIG_PM_SLEEP
488static int fsl_pwm_suspend(struct device *dev)
489{
490 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
Stefan Agner816aec22015-11-23 14:45:07 -0800491 int i;
Xiubo Li97d0b422014-10-15 13:21:35 +0800492
493 regcache_cache_only(fpc->regmap, true);
494 regcache_mark_dirty(fpc->regmap);
495
Stefan Agner816aec22015-11-23 14:45:07 -0800496 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.com82a9c552018-06-08 14:22:34 -0500502 clk_disable_unprepare(fpc->ipg_clk);
Stefan Agner816aec22015-11-23 14:45:07 -0800503
504 if (!pwm_is_enabled(pwm))
505 continue;
506
Xiubo Li97d0b422014-10-15 13:21:35 +0800507 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200508 clk_disable_unprepare(fpc->clk[fpc->period.clk_select]);
Xiubo Li97d0b422014-10-15 13:21:35 +0800509 }
510
511 return 0;
512}
513
514static int fsl_pwm_resume(struct device *dev)
515{
516 struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
Stefan Agner816aec22015-11-23 14:45:07 -0800517 int i;
Xiubo Li97d0b422014-10-15 13:21:35 +0800518
Stefan Agner816aec22015-11-23 14:45:07 -0800519 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.com82a9c552018-06-08 14:22:34 -0500525 clk_prepare_enable(fpc->ipg_clk);
Stefan Agner816aec22015-11-23 14:45:07 -0800526
527 if (!pwm_is_enabled(pwm))
528 continue;
529
Patrick Havelange3479bbd2019-06-12 16:12:45 +0200530 clk_prepare_enable(fpc->clk[fpc->period.clk_select]);
Xiubo Li97d0b422014-10-15 13:21:35 +0800531 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
542static 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.comdb6c51a2018-06-08 14:22:35 -0500546static const struct fsl_ftm_soc vf610_ftm_pwm = {
547 .has_enable_bits = false,
548};
549
shenwei.wang@nxp.com2c4f2e32018-06-08 14:22:36 -0500550static const struct fsl_ftm_soc imx8qm_ftm_pwm = {
551 .has_enable_bits = true,
552};
553
Xiubo Lib5051832014-02-27 17:39:49 +0800554static const struct of_device_id fsl_pwm_dt_ids[] = {
shenwei.wang@nxp.comdb6c51a2018-06-08 14:22:35 -0500555 { .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm },
shenwei.wang@nxp.com2c4f2e32018-06-08 14:22:36 -0500556 { .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm },
Xiubo Lib5051832014-02-27 17:39:49 +0800557 { /* sentinel */ }
558};
559MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
560
561static struct platform_driver fsl_pwm_driver = {
562 .driver = {
563 .name = "fsl-ftm-pwm",
564 .of_match_table = fsl_pwm_dt_ids,
Xiubo Li97d0b422014-10-15 13:21:35 +0800565 .pm = &fsl_pwm_pm_ops,
Xiubo Lib5051832014-02-27 17:39:49 +0800566 },
567 .probe = fsl_pwm_probe,
568 .remove = fsl_pwm_remove,
569};
570module_platform_driver(fsl_pwm_driver);
571
572MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
573MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
574MODULE_ALIAS("platform:fsl-ftm-pwm");
575MODULE_LICENSE("GPL");