Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2017-2018 Vasily Khoruzhick <anarsoul@gmail.com> |
| 4 | */ |
| 5 | |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 6 | #include <div64.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 9 | #include <pwm.h> |
| 10 | #include <regmap.h> |
| 11 | #include <syscon.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/pwm.h> |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 15 | #include <power/regulator.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | #define OSC_24MHZ 24000000 |
| 20 | |
| 21 | struct sunxi_pwm_priv { |
| 22 | struct sunxi_pwm *regs; |
| 23 | bool invert; |
| 24 | u32 prescaler; |
| 25 | }; |
| 26 | |
| 27 | static const u32 prescaler_table[] = { |
| 28 | 120, /* 0000 */ |
| 29 | 180, /* 0001 */ |
| 30 | 240, /* 0010 */ |
| 31 | 360, /* 0011 */ |
| 32 | 480, /* 0100 */ |
| 33 | 0, /* 0101 */ |
| 34 | 0, /* 0110 */ |
| 35 | 0, /* 0111 */ |
| 36 | 12000, /* 1000 */ |
| 37 | 24000, /* 1001 */ |
| 38 | 36000, /* 1010 */ |
| 39 | 48000, /* 1011 */ |
| 40 | 72000, /* 1100 */ |
| 41 | 0, /* 1101 */ |
| 42 | 0, /* 1110 */ |
| 43 | 1, /* 1111 */ |
| 44 | }; |
| 45 | |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 46 | static int sunxi_pwm_set_invert(struct udevice *dev, uint channel, |
| 47 | bool polarity) |
| 48 | { |
| 49 | struct sunxi_pwm_priv *priv = dev_get_priv(dev); |
| 50 | |
| 51 | debug("%s: polarity=%u\n", __func__, polarity); |
| 52 | priv->invert = polarity; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int sunxi_pwm_set_config(struct udevice *dev, uint channel, |
| 58 | uint period_ns, uint duty_ns) |
| 59 | { |
| 60 | struct sunxi_pwm_priv *priv = dev_get_priv(dev); |
| 61 | struct sunxi_pwm *regs = priv->regs; |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 62 | int best_prescaler = 0; |
| 63 | u32 v, best_period = 0, duty; |
| 64 | u64 best_scaled_freq = 0; |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 65 | const u32 nsecs_per_sec = 1000000000U; |
| 66 | |
| 67 | debug("%s: period_ns=%u, duty_ns=%u\n", __func__, period_ns, duty_ns); |
| 68 | |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 69 | for (int prescaler = 0; prescaler <= SUNXI_PWM_CTRL_PRESCALE0_MASK; |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 70 | prescaler++) { |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 71 | u32 period = 0; |
| 72 | u64 scaled_freq = 0; |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 73 | if (!prescaler_table[prescaler]) |
| 74 | continue; |
| 75 | scaled_freq = lldiv(OSC_24MHZ, prescaler_table[prescaler]); |
| 76 | period = lldiv(scaled_freq * period_ns, nsecs_per_sec); |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 77 | if ((period - 1 <= SUNXI_PWM_CH0_PERIOD_MAX) && |
| 78 | best_period < period) { |
| 79 | best_period = period; |
| 80 | best_scaled_freq = scaled_freq; |
| 81 | best_prescaler = prescaler; |
| 82 | } |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 85 | if (best_period - 1 > SUNXI_PWM_CH0_PERIOD_MAX) { |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 86 | debug("%s: failed to find prescaler value\n", __func__); |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 90 | duty = lldiv(best_scaled_freq * duty_ns, nsecs_per_sec); |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 91 | |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 92 | if (priv->prescaler != best_prescaler) { |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 93 | /* Mask clock to update prescaler */ |
| 94 | v = readl(®s->ctrl); |
| 95 | v &= ~SUNXI_PWM_CTRL_CLK_GATE; |
| 96 | writel(v, ®s->ctrl); |
| 97 | v &= ~SUNXI_PWM_CTRL_PRESCALE0_MASK; |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 98 | v |= (best_prescaler & SUNXI_PWM_CTRL_PRESCALE0_MASK); |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 99 | writel(v, ®s->ctrl); |
| 100 | v |= SUNXI_PWM_CTRL_CLK_GATE; |
| 101 | writel(v, ®s->ctrl); |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 102 | priv->prescaler = best_prescaler; |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 105 | writel(SUNXI_PWM_CH0_PERIOD_PRD(best_period) | |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 106 | SUNXI_PWM_CH0_PERIOD_DUTY(duty), ®s->ch0_period); |
| 107 | |
| 108 | debug("%s: prescaler: %d, period: %d, duty: %d\n", |
| 109 | __func__, priv->prescaler, |
Vasily Khoruzhick | 50f3feb | 2018-10-16 21:56:35 -0700 | [diff] [blame] | 110 | best_period, duty); |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int sunxi_pwm_set_enable(struct udevice *dev, uint channel, bool enable) |
| 116 | { |
| 117 | struct sunxi_pwm_priv *priv = dev_get_priv(dev); |
| 118 | struct sunxi_pwm *regs = priv->regs; |
| 119 | u32 v; |
| 120 | |
| 121 | debug("%s: Enable '%s'\n", __func__, dev->name); |
| 122 | |
| 123 | v = readl(®s->ctrl); |
| 124 | if (!enable) { |
| 125 | v &= ~SUNXI_PWM_CTRL_ENABLE0; |
| 126 | writel(v, ®s->ctrl); |
| 127 | return 0; |
| 128 | } |
| 129 | |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 130 | if (priv->invert) |
| 131 | v &= ~SUNXI_PWM_CTRL_CH0_ACT_STA; |
| 132 | else |
| 133 | v |= SUNXI_PWM_CTRL_CH0_ACT_STA; |
| 134 | v |= SUNXI_PWM_CTRL_ENABLE0; |
| 135 | writel(v, ®s->ctrl); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 140 | static int sunxi_pwm_of_to_plat(struct udevice *dev) |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 141 | { |
| 142 | struct sunxi_pwm_priv *priv = dev_get_priv(dev); |
| 143 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 144 | priv->regs = dev_read_addr_ptr(dev); |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int sunxi_pwm_probe(struct udevice *dev) |
| 150 | { |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static const struct pwm_ops sunxi_pwm_ops = { |
| 155 | .set_invert = sunxi_pwm_set_invert, |
| 156 | .set_config = sunxi_pwm_set_config, |
| 157 | .set_enable = sunxi_pwm_set_enable, |
| 158 | }; |
| 159 | |
| 160 | static const struct udevice_id sunxi_pwm_ids[] = { |
| 161 | { .compatible = "allwinner,sun5i-a13-pwm" }, |
| 162 | { .compatible = "allwinner,sun50i-a64-pwm" }, |
| 163 | { } |
| 164 | }; |
| 165 | |
| 166 | U_BOOT_DRIVER(sunxi_pwm) = { |
| 167 | .name = "sunxi_pwm", |
| 168 | .id = UCLASS_PWM, |
| 169 | .of_match = sunxi_pwm_ids, |
| 170 | .ops = &sunxi_pwm_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 171 | .of_to_plat = sunxi_pwm_of_to_plat, |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 172 | .probe = sunxi_pwm_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 173 | .priv_auto = sizeof(struct sunxi_pwm_priv), |
Vasily Khoruzhick | 86a38e5 | 2018-05-14 08:16:20 -0700 | [diff] [blame] | 174 | }; |