Billy Tsai | a65bdb9 | 2022-03-08 11:04:05 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 Aspeed Technology Inc. |
| 4 | * |
| 5 | * PWM controller driver for Aspeed ast2600 SoCs. |
| 6 | * This drivers doesn't support earlier version of the IP. |
| 7 | * |
| 8 | * The formula of pwm period duration: |
| 9 | * period duration = ((DIV_L + 1) * (PERIOD + 1) << DIV_H) / input-clk |
| 10 | * |
| 11 | * The formula of pwm duty cycle duration: |
| 12 | * duty cycle duration = period duration * DUTY_CYCLE_FALLING_POINT / (PERIOD + 1) |
| 13 | * = ((DIV_L + 1) * DUTY_CYCLE_FALLING_POINT << DIV_H) / input-clk |
| 14 | * |
| 15 | * The software driver fixes the period to 255, which causes the high-frequency |
| 16 | * precision of the PWM to be coarse, in exchange for the fineness of the duty cycle. |
| 17 | * |
| 18 | * Register usage: |
| 19 | * PIN_ENABLE: When it is unset the pwm controller will always output low to the extern. |
| 20 | * Use to determine whether the PWM channel is enabled or disabled |
| 21 | * CLK_ENABLE: When it is unset the pwm controller will reset the duty counter to 0 and |
| 22 | * output low to the PIN_ENABLE mux after that the driver can still change the pwm period |
| 23 | * and duty and the value will apply when CLK_ENABLE be set again. |
| 24 | * Use to determine whether duty_cycle bigger than 0. |
| 25 | * PWM_ASPEED_CTRL_INVERSE: When it is toggled the output value will inverse immediately. |
| 26 | * PWM_ASPEED_DUTY_CYCLE_FALLING_POINT/PWM_ASPEED_DUTY_CYCLE_RISING_POINT: When these two |
| 27 | * values are equal it means the duty cycle = 100%. |
| 28 | * |
| 29 | * Limitations: |
| 30 | * - When changing both duty cycle and period, we cannot prevent in |
| 31 | * software that the output might produce a period with mixed |
| 32 | * settings. |
| 33 | * - Disabling the PWM doesn't complete the current period. |
| 34 | * |
| 35 | * Improvements: |
| 36 | * - When only changing one of duty cycle or period, our pwm controller will not |
| 37 | * generate the glitch, the configure will change at next cycle of pwm. |
| 38 | * This improvement can disable/enable through PWM_ASPEED_CTRL_DUTY_SYNC_DISABLE. |
| 39 | */ |
| 40 | |
Billy Tsai | a65bdb9 | 2022-03-08 11:04:05 +0800 | [diff] [blame] | 41 | #include <div64.h> |
| 42 | #include <dm.h> |
| 43 | #include <pwm.h> |
| 44 | #include <clk.h> |
| 45 | #include <reset.h> |
| 46 | #include <regmap.h> |
| 47 | #include <syscon.h> |
| 48 | #include <dm/device_compat.h> |
| 49 | #include <linux/math64.h> |
| 50 | #include <linux/bitfield.h> |
Igor Prusov | c3421ea | 2023-11-09 20:10:04 +0300 | [diff] [blame] | 51 | #include <linux/time.h> |
Billy Tsai | a65bdb9 | 2022-03-08 11:04:05 +0800 | [diff] [blame] | 52 | #include <asm/io.h> |
| 53 | |
| 54 | /* The channel number of Aspeed pwm controller */ |
| 55 | #define PWM_ASPEED_NR_PWMS 16 |
| 56 | |
| 57 | /* PWM Control Register */ |
| 58 | #define PWM_ASPEED_CTRL(ch) ((ch) * 0x10 + 0x00) |
| 59 | #define PWM_ASPEED_CTRL_LOAD_SEL_RISING_AS_WDT BIT(19) |
| 60 | #define PWM_ASPEED_CTRL_DUTY_LOAD_AS_WDT_ENABLE BIT(18) |
| 61 | #define PWM_ASPEED_CTRL_DUTY_SYNC_DISABLE BIT(17) |
| 62 | #define PWM_ASPEED_CTRL_CLK_ENABLE BIT(16) |
| 63 | #define PWM_ASPEED_CTRL_LEVEL_OUTPUT BIT(15) |
| 64 | #define PWM_ASPEED_CTRL_INVERSE BIT(14) |
| 65 | #define PWM_ASPEED_CTRL_OPEN_DRAIN_ENABLE BIT(13) |
| 66 | #define PWM_ASPEED_CTRL_PIN_ENABLE BIT(12) |
| 67 | #define PWM_ASPEED_CTRL_CLK_DIV_H GENMASK(11, 8) |
| 68 | #define PWM_ASPEED_CTRL_CLK_DIV_L GENMASK(7, 0) |
| 69 | |
| 70 | /* PWM Duty Cycle Register */ |
| 71 | #define PWM_ASPEED_DUTY_CYCLE(ch) ((ch) * 0x10 + 0x04) |
| 72 | #define PWM_ASPEED_DUTY_CYCLE_PERIOD GENMASK(31, 24) |
| 73 | #define PWM_ASPEED_DUTY_CYCLE_POINT_AS_WDT GENMASK(23, 16) |
| 74 | #define PWM_ASPEED_DUTY_CYCLE_FALLING_POINT GENMASK(15, 8) |
| 75 | #define PWM_ASPEED_DUTY_CYCLE_RISING_POINT GENMASK(7, 0) |
| 76 | |
| 77 | /* PWM fixed value */ |
| 78 | #define PWM_ASPEED_FIXED_PERIOD 0xff |
| 79 | |
Billy Tsai | a65bdb9 | 2022-03-08 11:04:05 +0800 | [diff] [blame] | 80 | struct aspeed_pwm_priv { |
| 81 | struct clk clk; |
| 82 | struct regmap *regmap; |
| 83 | struct reset_ctl reset; |
| 84 | }; |
| 85 | |
| 86 | static int aspeed_pwm_set_invert(struct udevice *dev, uint channel, bool polarity) |
| 87 | { |
| 88 | struct aspeed_pwm_priv *priv = dev_get_priv(dev); |
| 89 | |
| 90 | if (channel >= PWM_ASPEED_NR_PWMS) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | regmap_update_bits(priv->regmap, PWM_ASPEED_CTRL(channel), |
| 94 | PWM_ASPEED_CTRL_INVERSE, |
| 95 | FIELD_PREP(PWM_ASPEED_CTRL_INVERSE, |
| 96 | polarity)); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int aspeed_pwm_set_enable(struct udevice *dev, uint channel, bool enable) |
| 101 | { |
| 102 | struct aspeed_pwm_priv *priv = dev_get_priv(dev); |
| 103 | |
| 104 | if (channel >= PWM_ASPEED_NR_PWMS) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | regmap_update_bits(priv->regmap, PWM_ASPEED_CTRL(channel), |
| 108 | PWM_ASPEED_CTRL_PIN_ENABLE, |
| 109 | enable ? PWM_ASPEED_CTRL_PIN_ENABLE : 0); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int aspeed_pwm_set_config(struct udevice *dev, uint channel, |
| 114 | uint period_ns, uint duty_ns) |
| 115 | { |
| 116 | struct aspeed_pwm_priv *priv = dev_get_priv(dev); |
| 117 | u32 duty_pt; |
| 118 | unsigned long rate; |
| 119 | u64 div_h, div_l, divisor; |
| 120 | bool clk_en; |
| 121 | |
| 122 | if (channel >= PWM_ASPEED_NR_PWMS) |
| 123 | return -EINVAL; |
| 124 | dev_dbg(dev, "expect period: %dns, duty_cycle: %dns\n", period_ns, |
| 125 | duty_ns); |
| 126 | |
| 127 | rate = clk_get_rate(&priv->clk); |
| 128 | /* |
| 129 | * Pick the smallest value for div_h so that div_l can be the biggest |
| 130 | * which results in a finer resolution near the target period value. |
| 131 | */ |
| 132 | divisor = (u64)NSEC_PER_SEC * (PWM_ASPEED_FIXED_PERIOD + 1) * |
| 133 | (PWM_ASPEED_CTRL_CLK_DIV_L + 1); |
| 134 | div_h = order_base_2(div64_u64((u64)rate * period_ns + divisor - 1, divisor)); |
| 135 | if (div_h > 0xf) |
| 136 | div_h = 0xf; |
| 137 | |
| 138 | divisor = ((u64)NSEC_PER_SEC * (PWM_ASPEED_FIXED_PERIOD + 1)) << div_h; |
| 139 | div_l = div64_u64((u64)rate * period_ns, divisor); |
| 140 | |
| 141 | if (div_l == 0) |
| 142 | return -ERANGE; |
| 143 | |
| 144 | div_l -= 1; |
| 145 | |
| 146 | if (div_l > 255) |
| 147 | div_l = 255; |
| 148 | |
| 149 | dev_dbg(dev, "clk source: %ld div_h %lld, div_l : %lld\n", rate, div_h, |
| 150 | div_l); |
| 151 | /* duty_pt = duty_cycle * (PERIOD + 1) / period */ |
| 152 | duty_pt = div64_u64(duty_ns * (u64)rate, |
| 153 | (u64)NSEC_PER_SEC * (div_l + 1) << div_h); |
| 154 | dev_dbg(dev, "duty_cycle = %d, duty_pt = %d\n", duty_ns, |
| 155 | duty_pt); |
| 156 | |
| 157 | if (duty_pt == 0) { |
| 158 | clk_en = 0; |
| 159 | } else { |
| 160 | clk_en = 1; |
| 161 | if (duty_pt >= (PWM_ASPEED_FIXED_PERIOD + 1)) |
| 162 | duty_pt = 0; |
| 163 | /* |
| 164 | * Fixed DUTY_CYCLE_PERIOD to its max value to get a |
| 165 | * fine-grained resolution for duty_cycle at the expense of a |
| 166 | * coarser period resolution. |
| 167 | */ |
| 168 | regmap_update_bits(priv->regmap, PWM_ASPEED_DUTY_CYCLE(channel), |
| 169 | PWM_ASPEED_DUTY_CYCLE_PERIOD | |
| 170 | PWM_ASPEED_DUTY_CYCLE_RISING_POINT | |
| 171 | PWM_ASPEED_DUTY_CYCLE_FALLING_POINT, |
| 172 | FIELD_PREP(PWM_ASPEED_DUTY_CYCLE_PERIOD, |
| 173 | PWM_ASPEED_FIXED_PERIOD) | |
| 174 | FIELD_PREP(PWM_ASPEED_DUTY_CYCLE_FALLING_POINT, |
| 175 | duty_pt)); |
| 176 | } |
| 177 | |
| 178 | regmap_update_bits(priv->regmap, PWM_ASPEED_CTRL(channel), |
| 179 | PWM_ASPEED_CTRL_CLK_DIV_H | |
| 180 | PWM_ASPEED_CTRL_CLK_DIV_L | |
| 181 | PWM_ASPEED_CTRL_CLK_ENABLE, |
| 182 | FIELD_PREP(PWM_ASPEED_CTRL_CLK_DIV_H, div_h) | |
| 183 | FIELD_PREP(PWM_ASPEED_CTRL_CLK_DIV_L, div_l) | |
| 184 | FIELD_PREP(PWM_ASPEED_CTRL_CLK_ENABLE, clk_en)); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int aspeed_pwm_probe(struct udevice *dev) |
| 189 | { |
| 190 | int ret; |
| 191 | struct aspeed_pwm_priv *priv = dev_get_priv(dev); |
| 192 | struct udevice *parent_dev = dev_get_parent(dev); |
| 193 | |
| 194 | priv->regmap = syscon_node_to_regmap(dev_ofnode(dev->parent)); |
| 195 | if (IS_ERR(priv->regmap)) { |
| 196 | dev_err(dev, "Couldn't get regmap\n"); |
| 197 | return PTR_ERR(priv->regmap); |
| 198 | } |
| 199 | |
| 200 | ret = clk_get_by_index(parent_dev, 0, &priv->clk); |
| 201 | if (ret < 0) { |
| 202 | dev_err(dev, "get clock failed\n"); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | ret = reset_get_by_index(parent_dev, 0, &priv->reset); |
| 207 | if (ret) { |
| 208 | dev_err(dev, "get reset failed\n"); |
| 209 | return ret; |
| 210 | } |
| 211 | ret = reset_deassert(&priv->reset); |
| 212 | if (ret) { |
| 213 | dev_err(dev, "cannot deassert reset control: %pe\n", |
| 214 | ERR_PTR(ret)); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int aspeed_pwm_remove(struct udevice *dev) |
| 222 | { |
| 223 | struct aspeed_pwm_priv *priv = dev_get_priv(dev); |
| 224 | |
| 225 | reset_assert(&priv->reset); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static const struct pwm_ops aspeed_pwm_ops = { |
| 231 | .set_invert = aspeed_pwm_set_invert, |
| 232 | .set_config = aspeed_pwm_set_config, |
| 233 | .set_enable = aspeed_pwm_set_enable, |
| 234 | }; |
| 235 | |
| 236 | static const struct udevice_id aspeed_pwm_ids[] = { |
| 237 | { .compatible = "aspeed,ast2600-pwm" }, |
| 238 | { } |
| 239 | }; |
| 240 | |
| 241 | U_BOOT_DRIVER(aspeed_pwm) = { |
| 242 | .name = "aspeed_pwm", |
| 243 | .id = UCLASS_PWM, |
| 244 | .of_match = aspeed_pwm_ids, |
| 245 | .ops = &aspeed_pwm_ops, |
| 246 | .probe = aspeed_pwm_probe, |
| 247 | .remove = aspeed_pwm_remove, |
| 248 | .priv_auto = sizeof(struct aspeed_pwm_priv), |
| 249 | }; |