1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2020 BayLibre, SAS.
5 * Copyright (C) 2014 Amlogic, Inc.
7 * This PWM is only a set of Gates, Dividers and Counters:
8 * PWM output is achieved by calculating a clock that permits calculating
9 * two periods (low and high). The counter then has to be set to switch after
10 * N cycles for the first half period.
11 * The hardware has no "polarity" setting. This driver reverses the period
12 * cycles (the low length is inverted with the high length) for
13 * PWM_POLARITY_INVERSED.
14 * Setting the polarity will disable and re-enable the PWM output.
15 * Disabling the PWM stops the output immediately (without waiting for the
16 * current period to complete first).
26 #include <linux/math64.h>
27 #include <linux/bitfield.h>
28 #include <linux/clk-provider.h>
30 #define NSEC_PER_SEC 1000000000L
34 #define PWM_LOW_MASK GENMASK(15, 0)
35 #define PWM_HIGH_MASK GENMASK(31, 16)
37 #define REG_MISC_AB 0x8
38 #define MISC_B_CLK_EN BIT(23)
39 #define MISC_A_CLK_EN BIT(15)
40 #define MISC_CLK_DIV_MASK 0x7f
41 #define MISC_B_CLK_DIV_SHIFT 16
42 #define MISC_A_CLK_DIV_SHIFT 8
43 #define MISC_B_CLK_SEL_SHIFT 6
44 #define MISC_A_CLK_SEL_SHIFT 4
45 #define MISC_CLK_SEL_MASK 0x3
46 #define MISC_B_EN BIT(1)
47 #define MISC_A_EN BIT(0)
49 #define MESON_NUM_PWMS 2
51 static struct meson_pwm_channel_data {
57 } meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
59 .reg_offset = REG_PWM_A,
60 .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
61 .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
62 .clk_en_mask = MISC_A_CLK_EN,
63 .pwm_en_mask = MISC_A_EN,
66 .reg_offset = REG_PWM_B,
67 .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
68 .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
69 .clk_en_mask = MISC_B_CLK_EN,
70 .pwm_en_mask = MISC_B_EN,
74 struct meson_pwm_channel {
86 struct meson_pwm_data {
87 const long *parent_ids;
88 unsigned int num_parents;
92 const struct meson_pwm_data *data;
93 struct meson_pwm_channel channels[MESON_NUM_PWMS];
97 static int meson_pwm_set_enable(struct udevice *dev, uint channel, bool enable);
99 static int meson_pwm_set_config(struct udevice *dev, uint channeln,
100 uint period_ns, uint duty_ns)
102 struct meson_pwm *priv = dev_get_priv(dev);
103 struct meson_pwm_channel *channel;
104 struct meson_pwm_channel_data *channel_data;
105 unsigned int duty, period, pre_div, cnt, duty_cnt;
106 unsigned long fin_freq;
108 if (channeln >= MESON_NUM_PWMS)
111 channel = &priv->channels[channeln];
112 channel_data = &meson_pwm_per_channel_data[channeln];
115 if (channel->polarity)
116 duty = period_ns - duty_ns;
120 debug("%s%d: polarity %s duty %d period %d\n", __func__, channeln,
121 channel->polarity ? "true" : "false", duty, period);
123 fin_freq = clk_get_rate(&channel->clk);
125 printf("%s%d: invalid source clock frequency\n", __func__, channeln);
129 debug("%s%d: fin_freq: %lu Hz\n", __func__, channeln, fin_freq);
131 pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL);
132 if (pre_div > MISC_CLK_DIV_MASK) {
133 printf("%s%d: unable to get period pre_div\n", __func__, channeln);
137 cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1));
139 printf("%s%d: unable to get period cnt\n", __func__, channeln);
143 debug("%s%d: period=%u pre_div=%u cnt=%u\n", __func__, channeln, period, pre_div, cnt);
145 if (duty == period) {
146 channel->pre_div = pre_div;
149 } else if (duty == 0) {
150 channel->pre_div = pre_div;
154 /* Then check is we can have the duty with the same pre_div */
155 duty_cnt = div64_u64(fin_freq * (u64)duty, NSEC_PER_SEC * (pre_div + 1));
156 if (duty_cnt > 0xffff) {
157 printf("%s%d: unable to get duty cycle\n", __func__, channeln);
161 debug("%s%d: duty=%u pre_div=%u duty_cnt=%u\n",
162 __func__, channeln, duty, pre_div, duty_cnt);
164 channel->pre_div = pre_div;
165 channel->hi = duty_cnt;
166 channel->lo = cnt - duty_cnt;
169 channel->period_ns = period_ns;
170 channel->duty_ns = duty_ns;
171 channel->configured = true;
173 if (channel->enabled) {
174 meson_pwm_set_enable(dev, channeln, false);
175 meson_pwm_set_enable(dev, channeln, true);
181 static int meson_pwm_set_enable(struct udevice *dev, uint channeln, bool enable)
183 struct meson_pwm *priv = dev_get_priv(dev);
184 struct meson_pwm_channel *channel;
185 struct meson_pwm_channel_data *channel_data;
188 if (channeln >= MESON_NUM_PWMS)
191 channel = &priv->channels[channeln];
192 channel_data = &meson_pwm_per_channel_data[channeln];
194 if (!channel->configured)
198 if (channel->enabled)
201 value = readl(priv->base + REG_MISC_AB);
202 value &= ~(MISC_CLK_DIV_MASK << channel_data->clk_div_shift);
203 value |= channel->pre_div << channel_data->clk_div_shift;
204 value |= channel_data->clk_en_mask;
205 writel(value, priv->base + REG_MISC_AB);
207 value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
208 FIELD_PREP(PWM_LOW_MASK, channel->lo);
209 writel(value, priv->base + channel_data->reg_offset);
211 value = readl(priv->base + REG_MISC_AB);
212 value |= channel_data->pwm_en_mask;
213 writel(value, priv->base + REG_MISC_AB);
215 debug("%s%d: enabled\n", __func__, channeln);
216 channel->enabled = true;
218 if (!channel->enabled)
221 value = readl(priv->base + REG_MISC_AB);
222 value &= channel_data->pwm_en_mask;
223 writel(value, priv->base + REG_MISC_AB);
225 debug("%s%d: disabled\n", __func__, channeln);
226 channel->enabled = false;
232 static int meson_pwm_set_invert(struct udevice *dev, uint channeln, bool polarity)
234 struct meson_pwm *priv = dev_get_priv(dev);
235 struct meson_pwm_channel *channel;
237 if (channeln >= MESON_NUM_PWMS)
240 debug("%s%d: set invert %s\n", __func__, channeln, polarity ? "true" : "false");
242 channel = &priv->channels[channeln];
244 channel->polarity = polarity;
246 if (!channel->configured)
249 return meson_pwm_set_config(dev, channeln, channel->period_ns, channel->duty_ns);
252 static int meson_pwm_of_to_plat(struct udevice *dev)
254 struct meson_pwm *priv = dev_get_priv(dev);
256 priv->base = dev_read_addr_ptr(dev);
261 static int meson_pwm_probe(struct udevice *dev)
263 struct meson_pwm *priv = dev_get_priv(dev);
264 struct meson_pwm_data *data;
270 data = (struct meson_pwm_data *)dev_get_driver_data(dev);
274 for (i = 0; i < MESON_NUM_PWMS; i++) {
275 struct meson_pwm_channel *channel = &priv->channels[i];
276 struct meson_pwm_channel_data *channel_data = &meson_pwm_per_channel_data[i];
278 snprintf(name, sizeof(name), "clkin%u", i);
280 err = clk_get_by_name(dev, name, &channel->clk);
281 /* If clock is not specified, use the already set clock */
282 if (err == -ENODATA) {
283 struct udevice *cdev;
286 /* Get parent from mux */
287 p = (readl(priv->base + REG_MISC_AB) >> channel_data->clk_sel_shift) &
290 if (p >= data->num_parents) {
291 printf("%s%d: hw parent is invalid\n", __func__, i);
295 if (data->parent_ids[p] == -1) {
296 /* Search for xtal clk */
299 err = uclass_get(UCLASS_CLK, &uc);
303 uclass_foreach_dev(cdev, uc) {
304 if (strcmp(cdev->driver->name, "fixed_rate_clock"))
307 str = ofnode_read_string(cdev->node, "clock-output-names");
311 if (!strcmp(str, "xtal")) {
312 err = uclass_get_device_by_ofnode(UCLASS_CLK,
316 printf("%s%d: Failed to get xtal clk\n", __func__, i);
325 printf("%s%d: Failed to find xtal clk device\n", __func__, i);
329 channel->clk.dev = cdev;
331 channel->clk.data = 0;
333 /* Look for parent clock */
334 err = uclass_get(UCLASS_CLK, &uc);
338 uclass_foreach_dev(cdev, uc) {
339 if (strstr(cdev->driver->name, "meson_clk"))
344 printf("%s%d: Failed to find clk device\n", __func__, i);
348 err = uclass_get_device_by_ofnode(UCLASS_CLK, cdev->node, &cdev);
350 printf("%s%d: Failed to get clk controller\n", __func__, i);
354 channel->clk.dev = cdev;
355 channel->clk.id = data->parent_ids[p];
356 channel->clk.data = 0;
359 /* We have our source clock, do not alter HW clock mux */
365 for (p = 0 ; p < data->num_parents ; ++p) {
366 if (!strcmp(channel->clk.dev->driver->name, "fixed_rate_clock")) {
367 if (data->parent_ids[p] == -1)
370 if (data->parent_ids[p] == channel->clk.id)
375 /* Invalid clock ID */
376 if (p == data->num_parents) {
377 printf("%s%d: source clock is invalid\n", __func__, i);
381 /* switch parent in mux */
382 reg = readl(priv->base + REG_MISC_AB);
384 debug("%s%d: switching parent %d to %d\n", __func__, i,
385 (reg >> channel_data->clk_sel_shift) & MISC_CLK_SEL_MASK, p);
387 reg &= MISC_CLK_SEL_MASK << channel_data->clk_sel_shift;
388 reg |= (p & MISC_CLK_SEL_MASK) << channel_data->clk_sel_shift;
389 writel(reg, priv->base + REG_MISC_AB);
395 static const struct pwm_ops meson_pwm_ops = {
396 .set_config = meson_pwm_set_config,
397 .set_enable = meson_pwm_set_enable,
398 .set_invert = meson_pwm_set_invert,
403 /* Local clock ids aliases to avoid define conflicts */
404 #define GXBB_CLKID_HDMI_PLL 2
405 #define GXBB_CLKID_FCLK_DIV3 5
406 #define GXBB_CLKID_FCLK_DIV4 6
407 #define GXBB_CLKID_CLK81 12
409 static const long pwm_gxbb_parent_ids[] = {
410 XTAL, GXBB_CLKID_HDMI_PLL, GXBB_CLKID_FCLK_DIV4, GXBB_CLKID_FCLK_DIV3
413 static const struct meson_pwm_data pwm_gxbb_data = {
414 .parent_ids = pwm_gxbb_parent_ids,
415 .num_parents = ARRAY_SIZE(pwm_gxbb_parent_ids),
419 * Only the 2 first inputs of the GXBB AO PWMs are valid
420 * The last 2 are grounded
422 static const long pwm_gxbb_ao_parent_ids[] = {
423 XTAL, GXBB_CLKID_CLK81
426 static const struct meson_pwm_data pwm_gxbb_ao_data = {
427 .parent_ids = pwm_gxbb_ao_parent_ids,
428 .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_ids),
431 /* Local clock ids aliases to avoid define conflicts */
432 #define AXG_CLKID_FCLK_DIV3 3
433 #define AXG_CLKID_FCLK_DIV4 4
434 #define AXG_CLKID_FCLK_DIV5 5
435 #define AXG_CLKID_CLK81 10
437 static const long pwm_axg_ee_parent_ids[] = {
438 XTAL, AXG_CLKID_FCLK_DIV5, AXG_CLKID_FCLK_DIV4, AXG_CLKID_FCLK_DIV3
441 static const struct meson_pwm_data pwm_axg_ee_data = {
442 .parent_ids = pwm_axg_ee_parent_ids,
443 .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_ids),
446 static const long pwm_axg_ao_parent_ids[] = {
447 AXG_CLKID_CLK81, XTAL, AXG_CLKID_FCLK_DIV4, AXG_CLKID_FCLK_DIV5
450 static const struct meson_pwm_data pwm_axg_ao_data = {
451 .parent_ids = pwm_axg_ao_parent_ids,
452 .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_ids),
455 /* Local clock ids aliases to avoid define conflicts */
456 #define G12A_CLKID_FCLK_DIV3 3
457 #define G12A_CLKID_FCLK_DIV4 4
458 #define G12A_CLKID_FCLK_DIV5 5
459 #define G12A_CLKID_CLK81 10
460 #define G12A_CLKID_HDMI_PLL 128
462 static const long pwm_g12a_ao_ab_parent_ids[] = {
463 XTAL, G12A_CLKID_CLK81, G12A_CLKID_FCLK_DIV4, G12A_CLKID_FCLK_DIV5
466 static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
467 .parent_ids = pwm_g12a_ao_ab_parent_ids,
468 .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_ids),
471 static const long pwm_g12a_ao_cd_parent_ids[] = {
472 XTAL, G12A_CLKID_CLK81,
475 static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
476 .parent_ids = pwm_g12a_ao_cd_parent_ids,
477 .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_ids),
480 static const long pwm_g12a_ee_parent_ids[] = {
481 XTAL, G12A_CLKID_HDMI_PLL, G12A_CLKID_FCLK_DIV4, G12A_CLKID_FCLK_DIV3
484 static const struct meson_pwm_data pwm_g12a_ee_data = {
485 .parent_ids = pwm_g12a_ee_parent_ids,
486 .num_parents = ARRAY_SIZE(pwm_g12a_ee_parent_ids),
489 static const struct udevice_id meson_pwm_ids[] = {
491 .compatible = "amlogic,meson-gxbb-pwm",
492 .data = (ulong)&pwm_gxbb_data
495 .compatible = "amlogic,meson-gxbb-ao-pwm",
496 .data = (ulong)&pwm_gxbb_ao_data
499 .compatible = "amlogic,meson-axg-ee-pwm",
500 .data = (ulong)&pwm_axg_ee_data
503 .compatible = "amlogic,meson-axg-ao-pwm",
504 .data = (ulong)&pwm_axg_ao_data
507 .compatible = "amlogic,meson-g12a-ee-pwm",
508 .data = (ulong)&pwm_g12a_ee_data
511 .compatible = "amlogic,meson-g12a-ao-pwm-ab",
512 .data = (ulong)&pwm_g12a_ao_ab_data
515 .compatible = "amlogic,meson-g12a-ao-pwm-cd",
516 .data = (ulong)&pwm_g12a_ao_cd_data
520 U_BOOT_DRIVER(meson_pwm) = {
523 .of_match = meson_pwm_ids,
524 .ops = &meson_pwm_ops,
525 .of_to_plat = meson_pwm_of_to_plat,
526 .probe = meson_pwm_probe,
527 .priv_auto = sizeof(struct meson_pwm),