1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * PWM controller driver for Amlogic Meson SoCs.
5 * This PWM is only a set of Gates, Dividers and Counters:
6 * PWM output is achieved by calculating a clock that permits calculating
7 * two periods (low and high). The counter then has to be set to switch after
8 * N cycles for the first half period.
9 * The hardware has no "polarity" setting. This driver reverses the period
10 * cycles (the low length is inverted with the high length) for
11 * PWM_POLARITY_INVERSED. This means that .get_state cannot read the polarity
13 * Setting the duty cycle will disable and re-enable the PWM output.
14 * Disabling the PWM stops the output immediately (without waiting for the
15 * current period to complete first).
17 * The public S912 (GXM) datasheet contains some documentation for this PWM
18 * controller starting on page 543:
19 * https://dl.khadas.com/Hardware/VIM2/Datasheet/S912_Datasheet_V0.220170314publicversion-Wesion.pdf
20 * An updated version of this IP block is found in S922X (G12B) SoCs. The
21 * datasheet contains the description for this IP block revision starting at
23 * https://dn.odroid.com/S922X/ODROID-N2/Datasheet/S922X_Public_Datasheet_V0.2.pdf
25 * Copyright (c) 2016 BayLibre, SAS.
26 * Author: Neil Armstrong <narmstrong@baylibre.com>
27 * Copyright (C) 2014 Amlogic, Inc.
30 #include <linux/bitfield.h>
31 #include <linux/bits.h>
32 #include <linux/clk.h>
33 #include <linux/clk-provider.h>
34 #include <linux/err.h>
36 #include <linux/kernel.h>
37 #include <linux/math64.h>
38 #include <linux/module.h>
40 #include <linux/of_device.h>
41 #include <linux/platform_device.h>
42 #include <linux/pwm.h>
43 #include <linux/slab.h>
44 #include <linux/spinlock.h>
48 #define PWM_LOW_MASK GENMASK(15, 0)
49 #define PWM_HIGH_MASK GENMASK(31, 16)
51 #define REG_MISC_AB 0x8
52 #define MISC_B_CLK_EN_SHIFT 23
53 #define MISC_A_CLK_EN_SHIFT 15
54 #define MISC_CLK_DIV_WIDTH 7
55 #define MISC_B_CLK_DIV_SHIFT 16
56 #define MISC_A_CLK_DIV_SHIFT 8
57 #define MISC_B_CLK_SEL_SHIFT 6
58 #define MISC_A_CLK_SEL_SHIFT 4
59 #define MISC_CLK_SEL_MASK 0x3
60 #define MISC_B_EN BIT(1)
61 #define MISC_A_EN BIT(0)
63 #define MESON_NUM_PWMS 2
64 #define MESON_MAX_MUX_PARENTS 4
66 static struct meson_pwm_channel_data {
72 } meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
74 .reg_offset = REG_PWM_A,
75 .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
76 .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
77 .clk_en_shift = MISC_A_CLK_EN_SHIFT,
78 .pwm_en_mask = MISC_A_EN,
81 .reg_offset = REG_PWM_B,
82 .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
83 .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
84 .clk_en_shift = MISC_B_CLK_EN_SHIFT,
85 .pwm_en_mask = MISC_B_EN,
89 struct meson_pwm_channel {
95 struct clk_divider div;
100 struct meson_pwm_data {
101 const char * const *parent_names;
102 unsigned int num_parents;
106 struct pwm_chip chip;
107 const struct meson_pwm_data *data;
108 struct meson_pwm_channel channels[MESON_NUM_PWMS];
111 * Protects register (write) access to the REG_MISC_AB register
112 * that is shared between the two PWMs.
117 static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
119 return container_of(chip, struct meson_pwm, chip);
122 static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
124 struct meson_pwm *meson = to_meson_pwm(chip);
125 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
126 struct device *dev = chip->dev;
129 err = clk_prepare_enable(channel->clk);
131 dev_err(dev, "failed to enable clock %s: %d\n",
132 __clk_get_name(channel->clk), err);
139 static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
141 struct meson_pwm *meson = to_meson_pwm(chip);
142 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
144 clk_disable_unprepare(channel->clk);
147 static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
148 const struct pwm_state *state)
150 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
151 unsigned int cnt, duty_cnt;
152 unsigned long fin_freq;
153 u64 duty, period, freq;
155 duty = state->duty_cycle;
156 period = state->period;
159 * Note this is wrong. The result is an output wave that isn't really
160 * inverted and so is wrongly identified by .get_state as normal.
161 * Fixing this needs some care however as some machines might rely on
164 if (state->polarity == PWM_POLARITY_INVERSED)
165 duty = period - duty;
167 freq = div64_u64(NSEC_PER_SEC * 0xffffULL, period);
168 if (freq > ULONG_MAX)
171 fin_freq = clk_round_rate(channel->clk, freq);
173 dev_err(meson->chip.dev, "invalid source clock frequency\n");
177 dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
179 cnt = div_u64(fin_freq * period, NSEC_PER_SEC);
181 dev_err(meson->chip.dev, "unable to get period cnt\n");
185 dev_dbg(meson->chip.dev, "period=%llu cnt=%u\n", period, cnt);
187 if (duty == period) {
190 } else if (duty == 0) {
194 duty_cnt = div_u64(fin_freq * duty, NSEC_PER_SEC);
196 dev_dbg(meson->chip.dev, "duty=%llu duty_cnt=%u\n", duty, duty_cnt);
198 channel->hi = duty_cnt;
199 channel->lo = cnt - duty_cnt;
202 channel->rate = fin_freq;
207 static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm)
209 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
210 struct meson_pwm_channel_data *channel_data;
215 channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
217 err = clk_set_rate(channel->clk, channel->rate);
219 dev_err(meson->chip.dev, "setting clock rate failed\n");
221 spin_lock_irqsave(&meson->lock, flags);
223 value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
224 FIELD_PREP(PWM_LOW_MASK, channel->lo);
225 writel(value, meson->base + channel_data->reg_offset);
227 value = readl(meson->base + REG_MISC_AB);
228 value |= channel_data->pwm_en_mask;
229 writel(value, meson->base + REG_MISC_AB);
231 spin_unlock_irqrestore(&meson->lock, flags);
234 static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm)
239 spin_lock_irqsave(&meson->lock, flags);
241 value = readl(meson->base + REG_MISC_AB);
242 value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
243 writel(value, meson->base + REG_MISC_AB);
245 spin_unlock_irqrestore(&meson->lock, flags);
248 static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
249 const struct pwm_state *state)
251 struct meson_pwm *meson = to_meson_pwm(chip);
252 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
255 if (!state->enabled) {
256 if (state->polarity == PWM_POLARITY_INVERSED) {
258 * This IP block revision doesn't have an "always high"
259 * setting which we can use for "inverted disabled".
260 * Instead we achieve this by setting mux parent with
261 * highest rate and minimum divider value, resulting
262 * in the shortest possible duration for one "count"
263 * and "period == duty_cycle". This results in a signal
264 * which is LOW for one "count", while being HIGH for
265 * the rest of the (so the signal is HIGH for slightly
266 * less than 100% of the period, but this is the best
269 channel->rate = ULONG_MAX;
273 meson_pwm_enable(meson, pwm);
275 meson_pwm_disable(meson, pwm);
278 err = meson_pwm_calc(meson, pwm, state);
282 meson_pwm_enable(meson, pwm);
288 static u64 meson_pwm_cnt_to_ns(struct pwm_chip *chip, struct pwm_device *pwm,
291 struct meson_pwm *meson = to_meson_pwm(chip);
292 struct meson_pwm_channel *channel;
293 unsigned long fin_freq;
295 /* to_meson_pwm() can only be used after .get_state() is called */
296 channel = &meson->channels[pwm->hwpwm];
298 fin_freq = clk_get_rate(channel->clk);
302 return div64_ul(NSEC_PER_SEC * (u64)cnt, fin_freq);
305 static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
306 struct pwm_state *state)
308 struct meson_pwm *meson = to_meson_pwm(chip);
309 struct meson_pwm_channel_data *channel_data;
310 struct meson_pwm_channel *channel;
316 channel = &meson->channels[pwm->hwpwm];
317 channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
319 value = readl(meson->base + REG_MISC_AB);
320 state->enabled = value & channel_data->pwm_en_mask;
322 value = readl(meson->base + channel_data->reg_offset);
323 channel->lo = FIELD_GET(PWM_LOW_MASK, value);
324 channel->hi = FIELD_GET(PWM_HIGH_MASK, value);
326 state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->lo + channel->hi);
327 state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
329 state->polarity = PWM_POLARITY_NORMAL;
334 static const struct pwm_ops meson_pwm_ops = {
335 .request = meson_pwm_request,
336 .free = meson_pwm_free,
337 .apply = meson_pwm_apply,
338 .get_state = meson_pwm_get_state,
339 .owner = THIS_MODULE,
342 static const char * const pwm_meson8b_parent_names[] = {
343 "xtal", NULL, "fclk_div4", "fclk_div3"
346 static const struct meson_pwm_data pwm_meson8b_data = {
347 .parent_names = pwm_meson8b_parent_names,
348 .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
352 * Only the 2 first inputs of the GXBB AO PWMs are valid
353 * The last 2 are grounded
355 static const char * const pwm_gxbb_ao_parent_names[] = {
359 static const struct meson_pwm_data pwm_gxbb_ao_data = {
360 .parent_names = pwm_gxbb_ao_parent_names,
361 .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names),
364 static const char * const pwm_axg_ee_parent_names[] = {
365 "xtal", "fclk_div5", "fclk_div4", "fclk_div3"
368 static const struct meson_pwm_data pwm_axg_ee_data = {
369 .parent_names = pwm_axg_ee_parent_names,
370 .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_names),
373 static const char * const pwm_axg_ao_parent_names[] = {
374 "xtal", "axg_ao_clk81", "fclk_div4", "fclk_div5"
377 static const struct meson_pwm_data pwm_axg_ao_data = {
378 .parent_names = pwm_axg_ao_parent_names,
379 .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names),
382 static const char * const pwm_g12a_ao_ab_parent_names[] = {
383 "xtal", "g12a_ao_clk81", "fclk_div4", "fclk_div5"
386 static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
387 .parent_names = pwm_g12a_ao_ab_parent_names,
388 .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_names),
391 static const char * const pwm_g12a_ao_cd_parent_names[] = {
392 "xtal", "g12a_ao_clk81",
395 static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
396 .parent_names = pwm_g12a_ao_cd_parent_names,
397 .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_names),
400 static const struct of_device_id meson_pwm_matches[] = {
402 .compatible = "amlogic,meson8b-pwm",
403 .data = &pwm_meson8b_data
406 .compatible = "amlogic,meson-gxbb-pwm",
407 .data = &pwm_meson8b_data
410 .compatible = "amlogic,meson-gxbb-ao-pwm",
411 .data = &pwm_gxbb_ao_data
414 .compatible = "amlogic,meson-axg-ee-pwm",
415 .data = &pwm_axg_ee_data
418 .compatible = "amlogic,meson-axg-ao-pwm",
419 .data = &pwm_axg_ao_data
422 .compatible = "amlogic,meson-g12a-ee-pwm",
423 .data = &pwm_meson8b_data
426 .compatible = "amlogic,meson-g12a-ao-pwm-ab",
427 .data = &pwm_g12a_ao_ab_data
430 .compatible = "amlogic,meson-g12a-ao-pwm-cd",
431 .data = &pwm_g12a_ao_cd_data
435 MODULE_DEVICE_TABLE(of, meson_pwm_matches);
437 static int meson_pwm_init_channels(struct meson_pwm *meson)
439 struct clk_parent_data mux_parent_data[MESON_MAX_MUX_PARENTS] = {};
440 struct device *dev = meson->chip.dev;
445 for (i = 0; i < meson->data->num_parents; i++) {
446 mux_parent_data[i].index = -1;
447 mux_parent_data[i].name = meson->data->parent_names[i];
450 for (i = 0; i < meson->chip.npwm; i++) {
451 struct meson_pwm_channel *channel = &meson->channels[i];
452 struct clk_parent_data div_parent = {}, gate_parent = {};
453 struct clk_init_data init = {};
455 snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
458 init.ops = &clk_mux_ops;
460 init.parent_data = mux_parent_data;
461 init.num_parents = meson->data->num_parents;
463 channel->mux.reg = meson->base + REG_MISC_AB;
465 meson_pwm_per_channel_data[i].clk_sel_shift;
466 channel->mux.mask = MISC_CLK_SEL_MASK;
467 channel->mux.flags = 0;
468 channel->mux.lock = &meson->lock;
469 channel->mux.table = NULL;
470 channel->mux.hw.init = &init;
472 err = devm_clk_hw_register(dev, &channel->mux.hw);
474 dev_err(dev, "failed to register %s: %d\n", name, err);
478 snprintf(name, sizeof(name), "%s#div%u", dev_name(dev), i);
481 init.ops = &clk_divider_ops;
482 init.flags = CLK_SET_RATE_PARENT;
483 div_parent.index = -1;
484 div_parent.hw = &channel->mux.hw;
485 init.parent_data = &div_parent;
486 init.num_parents = 1;
488 channel->div.reg = meson->base + REG_MISC_AB;
489 channel->div.shift = meson_pwm_per_channel_data[i].clk_div_shift;
490 channel->div.width = MISC_CLK_DIV_WIDTH;
491 channel->div.hw.init = &init;
492 channel->div.flags = 0;
493 channel->div.lock = &meson->lock;
495 err = devm_clk_hw_register(dev, &channel->div.hw);
497 dev_err(dev, "failed to register %s: %d\n", name, err);
501 snprintf(name, sizeof(name), "%s#gate%u", dev_name(dev), i);
504 init.ops = &clk_gate_ops;
505 init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
506 gate_parent.index = -1;
507 gate_parent.hw = &channel->div.hw;
508 init.parent_data = &gate_parent;
509 init.num_parents = 1;
511 channel->gate.reg = meson->base + REG_MISC_AB;
512 channel->gate.bit_idx = meson_pwm_per_channel_data[i].clk_en_shift;
513 channel->gate.hw.init = &init;
514 channel->gate.flags = 0;
515 channel->gate.lock = &meson->lock;
517 err = devm_clk_hw_register(dev, &channel->gate.hw);
519 dev_err(dev, "failed to register %s: %d\n", name, err);
523 channel->clk = devm_clk_hw_get_clk(dev, &channel->gate.hw, NULL);
524 if (IS_ERR(channel->clk)) {
525 err = PTR_ERR(channel->clk);
526 dev_err(dev, "failed to register %s: %d\n", name, err);
534 static int meson_pwm_probe(struct platform_device *pdev)
536 struct meson_pwm *meson;
539 meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
543 meson->base = devm_platform_ioremap_resource(pdev, 0);
544 if (IS_ERR(meson->base))
545 return PTR_ERR(meson->base);
547 spin_lock_init(&meson->lock);
548 meson->chip.dev = &pdev->dev;
549 meson->chip.ops = &meson_pwm_ops;
550 meson->chip.npwm = MESON_NUM_PWMS;
552 meson->data = of_device_get_match_data(&pdev->dev);
554 err = meson_pwm_init_channels(meson);
558 err = devm_pwmchip_add(&pdev->dev, &meson->chip);
560 dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
567 static struct platform_driver meson_pwm_driver = {
570 .of_match_table = meson_pwm_matches,
572 .probe = meson_pwm_probe,
574 module_platform_driver(meson_pwm_driver);
576 MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
577 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
578 MODULE_LICENSE("Dual BSD/GPL");