Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Kever Yang | aee175e | 2016-08-12 17:57:48 +0800 | [diff] [blame] | 8 | #include <clk.h> |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 9 | #include <div64.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 12 | #include <pwm.h> |
| 13 | #include <regmap.h> |
| 14 | #include <syscon.h> |
| 15 | #include <asm/io.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 16 | #include <asm/arch-rockchip/pwm.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 18 | #include <power/regulator.h> |
| 19 | |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | struct rockchip_pwm_data { |
| 23 | struct rockchip_pwm_regs regs; |
| 24 | unsigned int prescaler; |
| 25 | bool supports_polarity; |
| 26 | bool supports_lock; |
| 27 | u32 enable_conf; |
| 28 | u32 enable_conf_mask; |
| 29 | }; |
| 30 | |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 31 | struct rk_pwm_priv { |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 32 | fdt_addr_t base; |
Kever Yang | aee175e | 2016-08-12 17:57:48 +0800 | [diff] [blame] | 33 | ulong freq; |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 34 | u32 conf_polarity; |
| 35 | const struct rockchip_pwm_data *data; |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Kever Yang | 73326e9 | 2017-04-24 10:27:50 +0800 | [diff] [blame] | 38 | static int rk_pwm_set_invert(struct udevice *dev, uint channel, bool polarity) |
| 39 | { |
| 40 | struct rk_pwm_priv *priv = dev_get_priv(dev); |
| 41 | |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 42 | if (!priv->data->supports_polarity) { |
| 43 | debug("%s: Do not support polarity\n", __func__); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Kever Yang | 73326e9 | 2017-04-24 10:27:50 +0800 | [diff] [blame] | 47 | debug("%s: polarity=%u\n", __func__, polarity); |
| 48 | if (polarity) |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 49 | priv->conf_polarity = PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSTIVE; |
Kever Yang | 73326e9 | 2017-04-24 10:27:50 +0800 | [diff] [blame] | 50 | else |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 51 | priv->conf_polarity = PWM_DUTY_POSTIVE | PWM_INACTIVE_NEGATIVE; |
Kever Yang | 73326e9 | 2017-04-24 10:27:50 +0800 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 56 | static int rk_pwm_set_config(struct udevice *dev, uint channel, uint period_ns, |
| 57 | uint duty_ns) |
| 58 | { |
| 59 | struct rk_pwm_priv *priv = dev_get_priv(dev); |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 60 | const struct rockchip_pwm_regs *regs = &priv->data->regs; |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 61 | unsigned long period, duty; |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 62 | u32 ctrl; |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 63 | |
| 64 | debug("%s: period_ns=%u, duty_ns=%u\n", __func__, period_ns, duty_ns); |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 65 | |
| 66 | ctrl = readl(priv->base + regs->ctrl); |
| 67 | /* |
| 68 | * Lock the period and duty of previous configuration, then |
| 69 | * change the duty and period, that would not be effective. |
| 70 | */ |
| 71 | if (priv->data->supports_lock) { |
| 72 | ctrl |= PWM_LOCK; |
| 73 | writel(ctrl, priv->base + regs->ctrl); |
| 74 | } |
| 75 | |
| 76 | period = lldiv((uint64_t)priv->freq * period_ns, |
| 77 | priv->data->prescaler * 1000000000); |
| 78 | duty = lldiv((uint64_t)priv->freq * duty_ns, |
| 79 | priv->data->prescaler * 1000000000); |
| 80 | |
| 81 | writel(period, priv->base + regs->period); |
| 82 | writel(duty, priv->base + regs->duty); |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 83 | |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 84 | if (priv->data->supports_polarity) { |
| 85 | ctrl &= ~(PWM_DUTY_MASK | PWM_INACTIVE_MASK); |
| 86 | ctrl |= priv->conf_polarity; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Unlock and set polarity at the same time, |
| 91 | * the configuration of duty, period and polarity |
| 92 | * would be effective together at next period. |
| 93 | */ |
| 94 | if (priv->data->supports_lock) |
| 95 | ctrl &= ~PWM_LOCK; |
| 96 | writel(ctrl, priv->base + regs->ctrl); |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 97 | |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 98 | debug("%s: period=%lu, duty=%lu\n", __func__, period, duty); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int rk_pwm_set_enable(struct udevice *dev, uint channel, bool enable) |
| 104 | { |
| 105 | struct rk_pwm_priv *priv = dev_get_priv(dev); |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 106 | const struct rockchip_pwm_regs *regs = &priv->data->regs; |
| 107 | u32 ctrl; |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 108 | |
| 109 | debug("%s: Enable '%s'\n", __func__, dev->name); |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 110 | |
| 111 | ctrl = readl(priv->base + regs->ctrl); |
| 112 | ctrl &= ~priv->data->enable_conf_mask; |
| 113 | |
| 114 | if (enable) |
| 115 | ctrl |= priv->data->enable_conf; |
| 116 | else |
| 117 | ctrl &= ~priv->data->enable_conf; |
| 118 | |
| 119 | writel(ctrl, priv->base + regs->ctrl); |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 124 | static int rk_pwm_of_to_plat(struct udevice *dev) |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 125 | { |
| 126 | struct rk_pwm_priv *priv = dev_get_priv(dev); |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 127 | |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 128 | priv->base = dev_read_addr(dev); |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int rk_pwm_probe(struct udevice *dev) |
| 134 | { |
| 135 | struct rk_pwm_priv *priv = dev_get_priv(dev); |
Kever Yang | aee175e | 2016-08-12 17:57:48 +0800 | [diff] [blame] | 136 | struct clk clk; |
| 137 | int ret = 0; |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 138 | |
Kever Yang | aee175e | 2016-08-12 17:57:48 +0800 | [diff] [blame] | 139 | ret = clk_get_by_index(dev, 0, &clk); |
| 140 | if (ret < 0) { |
| 141 | debug("%s get clock fail!\n", __func__); |
| 142 | return -EINVAL; |
| 143 | } |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 144 | |
Kever Yang | aee175e | 2016-08-12 17:57:48 +0800 | [diff] [blame] | 145 | priv->freq = clk_get_rate(&clk); |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 146 | priv->data = (struct rockchip_pwm_data *)dev_get_driver_data(dev); |
| 147 | |
| 148 | if (priv->data->supports_polarity) |
| 149 | priv->conf_polarity = PWM_DUTY_POSTIVE | PWM_INACTIVE_POSTIVE; |
Kever Yang | aee175e | 2016-08-12 17:57:48 +0800 | [diff] [blame] | 150 | |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static const struct pwm_ops rk_pwm_ops = { |
Kever Yang | 73326e9 | 2017-04-24 10:27:50 +0800 | [diff] [blame] | 155 | .set_invert = rk_pwm_set_invert, |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 156 | .set_config = rk_pwm_set_config, |
| 157 | .set_enable = rk_pwm_set_enable, |
| 158 | }; |
| 159 | |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 160 | static const struct rockchip_pwm_data pwm_data_v1 = { |
| 161 | .regs = { |
| 162 | .duty = 0x04, |
| 163 | .period = 0x08, |
| 164 | .cntr = 0x00, |
| 165 | .ctrl = 0x0c, |
| 166 | }, |
| 167 | .prescaler = 2, |
| 168 | .supports_polarity = false, |
| 169 | .supports_lock = false, |
| 170 | .enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN, |
| 171 | .enable_conf_mask = BIT(1) | BIT(3), |
| 172 | }; |
| 173 | |
| 174 | static const struct rockchip_pwm_data pwm_data_v2 = { |
| 175 | .regs = { |
| 176 | .duty = 0x08, |
| 177 | .period = 0x04, |
| 178 | .cntr = 0x00, |
| 179 | .ctrl = 0x0c, |
| 180 | }, |
| 181 | .prescaler = 1, |
| 182 | .supports_polarity = true, |
| 183 | .supports_lock = false, |
| 184 | .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | RK_PWM_ENABLE | |
| 185 | PWM_CONTINUOUS, |
| 186 | .enable_conf_mask = GENMASK(2, 0) | BIT(5) | BIT(8), |
| 187 | }; |
| 188 | |
| 189 | static const struct rockchip_pwm_data pwm_data_v3 = { |
| 190 | .regs = { |
| 191 | .duty = 0x08, |
| 192 | .period = 0x04, |
| 193 | .cntr = 0x00, |
| 194 | .ctrl = 0x0c, |
| 195 | }, |
| 196 | .prescaler = 1, |
| 197 | .supports_polarity = true, |
| 198 | .supports_lock = true, |
| 199 | .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | RK_PWM_ENABLE | |
| 200 | PWM_CONTINUOUS, |
| 201 | .enable_conf_mask = GENMASK(2, 0) | BIT(5) | BIT(8), |
| 202 | }; |
| 203 | |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 204 | static const struct udevice_id rk_pwm_ids[] = { |
David Wu | eb82918 | 2019-12-03 17:49:53 +0800 | [diff] [blame] | 205 | { .compatible = "rockchip,rk2928-pwm", .data = (ulong)&pwm_data_v1}, |
| 206 | { .compatible = "rockchip,rk3288-pwm", .data = (ulong)&pwm_data_v2}, |
| 207 | { .compatible = "rockchip,rk3328-pwm", .data = (ulong)&pwm_data_v3}, |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 208 | { } |
| 209 | }; |
| 210 | |
| 211 | U_BOOT_DRIVER(rk_pwm) = { |
| 212 | .name = "rk_pwm", |
| 213 | .id = UCLASS_PWM, |
| 214 | .of_match = rk_pwm_ids, |
| 215 | .ops = &rk_pwm_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 216 | .of_to_plat = rk_pwm_of_to_plat, |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 217 | .probe = rk_pwm_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 218 | .priv_auto = sizeof(struct rk_pwm_priv), |
Simon Glass | c5915f7 | 2016-01-21 19:44:55 -0700 | [diff] [blame] | 219 | }; |