1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2020 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
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(dev_ofnode(cdev),
308 "clock-output-names");
312 if (!strcmp(str, "xtal")) {
313 err = uclass_get_device_by_ofnode(UCLASS_CLK,
317 printf("%s%d: Failed to get xtal clk\n", __func__, i);
326 printf("%s%d: Failed to find xtal clk device\n", __func__, i);
330 channel->clk.dev = cdev;
332 channel->clk.data = 0;
334 /* Look for parent clock */
335 err = uclass_get(UCLASS_CLK, &uc);
339 uclass_foreach_dev(cdev, uc) {
340 if (strstr(cdev->driver->name, "meson_clk"))
345 printf("%s%d: Failed to find clk device\n", __func__, i);
349 err = uclass_get_device_by_ofnode(UCLASS_CLK,
353 printf("%s%d: Failed to get clk controller\n", __func__, i);
357 channel->clk.dev = cdev;
358 channel->clk.id = data->parent_ids[p];
359 channel->clk.data = 0;
362 /* We have our source clock, do not alter HW clock mux */
368 for (p = 0 ; p < data->num_parents ; ++p) {
369 if (!strcmp(channel->clk.dev->driver->name, "fixed_rate_clock")) {
370 if (data->parent_ids[p] == -1)
373 if (data->parent_ids[p] == channel->clk.id)
378 /* Invalid clock ID */
379 if (p == data->num_parents) {
380 printf("%s%d: source clock is invalid\n", __func__, i);
384 /* switch parent in mux */
385 reg = readl(priv->base + REG_MISC_AB);
387 debug("%s%d: switching parent %d to %d\n", __func__, i,
388 (reg >> channel_data->clk_sel_shift) & MISC_CLK_SEL_MASK, p);
390 reg &= MISC_CLK_SEL_MASK << channel_data->clk_sel_shift;
391 reg |= (p & MISC_CLK_SEL_MASK) << channel_data->clk_sel_shift;
392 writel(reg, priv->base + REG_MISC_AB);
398 static const struct pwm_ops meson_pwm_ops = {
399 .set_config = meson_pwm_set_config,
400 .set_enable = meson_pwm_set_enable,
401 .set_invert = meson_pwm_set_invert,
406 /* Local clock ids aliases to avoid define conflicts */
407 #define GXBB_CLKID_HDMI_PLL 2
408 #define GXBB_CLKID_FCLK_DIV3 5
409 #define GXBB_CLKID_FCLK_DIV4 6
410 #define GXBB_CLKID_CLK81 12
412 static const long pwm_gxbb_parent_ids[] = {
413 XTAL, GXBB_CLKID_HDMI_PLL, GXBB_CLKID_FCLK_DIV4, GXBB_CLKID_FCLK_DIV3
416 static const struct meson_pwm_data pwm_gxbb_data = {
417 .parent_ids = pwm_gxbb_parent_ids,
418 .num_parents = ARRAY_SIZE(pwm_gxbb_parent_ids),
422 * Only the 2 first inputs of the GXBB AO PWMs are valid
423 * The last 2 are grounded
425 static const long pwm_gxbb_ao_parent_ids[] = {
426 XTAL, GXBB_CLKID_CLK81
429 static const struct meson_pwm_data pwm_gxbb_ao_data = {
430 .parent_ids = pwm_gxbb_ao_parent_ids,
431 .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_ids),
434 /* Local clock ids aliases to avoid define conflicts */
435 #define AXG_CLKID_FCLK_DIV3 3
436 #define AXG_CLKID_FCLK_DIV4 4
437 #define AXG_CLKID_FCLK_DIV5 5
438 #define AXG_CLKID_CLK81 10
440 static const long pwm_axg_ee_parent_ids[] = {
441 XTAL, AXG_CLKID_FCLK_DIV5, AXG_CLKID_FCLK_DIV4, AXG_CLKID_FCLK_DIV3
444 static const struct meson_pwm_data pwm_axg_ee_data = {
445 .parent_ids = pwm_axg_ee_parent_ids,
446 .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_ids),
449 static const long pwm_axg_ao_parent_ids[] = {
450 AXG_CLKID_CLK81, XTAL, AXG_CLKID_FCLK_DIV4, AXG_CLKID_FCLK_DIV5
453 static const struct meson_pwm_data pwm_axg_ao_data = {
454 .parent_ids = pwm_axg_ao_parent_ids,
455 .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_ids),
458 /* Local clock ids aliases to avoid define conflicts */
459 #define G12A_CLKID_FCLK_DIV3 3
460 #define G12A_CLKID_FCLK_DIV4 4
461 #define G12A_CLKID_FCLK_DIV5 5
462 #define G12A_CLKID_CLK81 10
463 #define G12A_CLKID_HDMI_PLL 128
465 static const long pwm_g12a_ao_ab_parent_ids[] = {
466 XTAL, G12A_CLKID_CLK81, G12A_CLKID_FCLK_DIV4, G12A_CLKID_FCLK_DIV5
469 static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
470 .parent_ids = pwm_g12a_ao_ab_parent_ids,
471 .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_ids),
474 static const long pwm_g12a_ao_cd_parent_ids[] = {
475 XTAL, G12A_CLKID_CLK81,
478 static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
479 .parent_ids = pwm_g12a_ao_cd_parent_ids,
480 .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_ids),
483 static const long pwm_g12a_ee_parent_ids[] = {
484 XTAL, G12A_CLKID_HDMI_PLL, G12A_CLKID_FCLK_DIV4, G12A_CLKID_FCLK_DIV3
487 static const struct meson_pwm_data pwm_g12a_ee_data = {
488 .parent_ids = pwm_g12a_ee_parent_ids,
489 .num_parents = ARRAY_SIZE(pwm_g12a_ee_parent_ids),
492 static const struct udevice_id meson_pwm_ids[] = {
494 .compatible = "amlogic,meson-gxbb-pwm",
495 .data = (ulong)&pwm_gxbb_data
498 .compatible = "amlogic,meson-gxbb-ao-pwm",
499 .data = (ulong)&pwm_gxbb_ao_data
502 .compatible = "amlogic,meson-axg-ee-pwm",
503 .data = (ulong)&pwm_axg_ee_data
506 .compatible = "amlogic,meson-axg-ao-pwm",
507 .data = (ulong)&pwm_axg_ao_data
510 .compatible = "amlogic,meson-g12a-ee-pwm",
511 .data = (ulong)&pwm_g12a_ee_data
514 .compatible = "amlogic,meson-g12a-ao-pwm-ab",
515 .data = (ulong)&pwm_g12a_ao_ab_data
518 .compatible = "amlogic,meson-g12a-ao-pwm-cd",
519 .data = (ulong)&pwm_g12a_ao_cd_data
523 U_BOOT_DRIVER(meson_pwm) = {
526 .of_match = meson_pwm_ids,
527 .ops = &meson_pwm_ops,
528 .of_to_plat = meson_pwm_of_to_plat,
529 .probe = meson_pwm_probe,
530 .priv_auto = sizeof(struct meson_pwm),