Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Samsung Electronics |
| 4 | * |
| 5 | * Donghwa Lee <dh09.lee@samsung.com> |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/pwm.h> |
| 12 | #include <asm/arch/clk.h> |
| 13 | |
Tom Rini | 66faa43 | 2022-12-04 10:03:26 -0500 | [diff] [blame] | 14 | int s5p_pwm_enable(int pwm_id) |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 15 | { |
| 16 | const struct s5p_timer *pwm = |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 17 | #if defined(CONFIG_ARCH_NEXELL) |
| 18 | (struct s5p_timer *)PHY_BASEADDR_PWM; |
| 19 | #else |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 20 | (struct s5p_timer *)samsung_get_base_timer(); |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 21 | #endif |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 22 | unsigned long tcon; |
| 23 | |
| 24 | tcon = readl(&pwm->tcon); |
| 25 | tcon |= TCON_START(pwm_id); |
| 26 | |
| 27 | writel(tcon, &pwm->tcon); |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
Tom Rini | 66faa43 | 2022-12-04 10:03:26 -0500 | [diff] [blame] | 32 | void s5p_pwm_disable(int pwm_id) |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 33 | { |
| 34 | const struct s5p_timer *pwm = |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 35 | #if defined(CONFIG_ARCH_NEXELL) |
| 36 | (struct s5p_timer *)PHY_BASEADDR_PWM; |
| 37 | #else |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 38 | (struct s5p_timer *)samsung_get_base_timer(); |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 39 | #endif |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 40 | unsigned long tcon; |
| 41 | |
| 42 | tcon = readl(&pwm->tcon); |
| 43 | tcon &= ~TCON_START(pwm_id); |
| 44 | |
| 45 | writel(tcon, &pwm->tcon); |
| 46 | } |
| 47 | |
| 48 | static unsigned long pwm_calc_tin(int pwm_id, unsigned long freq) |
| 49 | { |
| 50 | unsigned long tin_parent_rate; |
| 51 | unsigned int div; |
| 52 | |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 53 | #if defined(CONFIG_ARCH_NEXELL) |
| 54 | unsigned int pre_div; |
| 55 | const struct s5p_timer *pwm = |
| 56 | (struct s5p_timer *)PHY_BASEADDR_PWM; |
| 57 | unsigned int val; |
| 58 | struct clk *clk = clk_get(CORECLK_NAME_PCLK); |
| 59 | |
| 60 | tin_parent_rate = clk_get_rate(clk); |
| 61 | #else |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 62 | tin_parent_rate = get_pwm_clk(); |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | #if defined(CONFIG_ARCH_NEXELL) |
| 66 | writel(0, &pwm->tcfg0); |
| 67 | val = readl(&pwm->tcfg0); |
| 68 | |
| 69 | if (pwm_id < 2) |
| 70 | div = ((val >> 0) & 0xff) + 1; |
| 71 | else |
| 72 | div = ((val >> 8) & 0xff) + 1; |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 73 | |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 74 | writel(0, &pwm->tcfg1); |
| 75 | val = readl(&pwm->tcfg1); |
| 76 | val = (val >> MUX_DIV_SHIFT(pwm_id)) & 0xF; |
| 77 | pre_div = (1UL << val); |
| 78 | |
| 79 | freq = tin_parent_rate / div / pre_div; |
| 80 | |
| 81 | return freq; |
| 82 | #else |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 83 | for (div = 2; div <= 16; div *= 2) { |
| 84 | if ((tin_parent_rate / (div << 16)) < freq) |
| 85 | return tin_parent_rate / div; |
| 86 | } |
| 87 | |
| 88 | return tin_parent_rate / 16; |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 89 | #endif |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Gabe Black | ad73b91 | 2013-03-28 04:32:20 +0000 | [diff] [blame] | 92 | #define NS_IN_SEC 1000000000UL |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 93 | |
Tom Rini | 66faa43 | 2022-12-04 10:03:26 -0500 | [diff] [blame] | 94 | int s5p_pwm_config(int pwm_id, int duty_ns, int period_ns) |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 95 | { |
| 96 | const struct s5p_timer *pwm = |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 97 | #if defined(CONFIG_ARCH_NEXELL) |
| 98 | (struct s5p_timer *)PHY_BASEADDR_PWM; |
| 99 | #else |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 100 | (struct s5p_timer *)samsung_get_base_timer(); |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 101 | #endif |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 102 | unsigned int offset; |
| 103 | unsigned long tin_rate; |
| 104 | unsigned long tin_ns; |
Gabe Black | ad73b91 | 2013-03-28 04:32:20 +0000 | [diff] [blame] | 105 | unsigned long frequency; |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 106 | unsigned long tcon; |
| 107 | unsigned long tcnt; |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 108 | unsigned long tcmp; |
| 109 | |
| 110 | /* |
| 111 | * We currently avoid using 64bit arithmetic by using the |
| 112 | * fact that anything faster than 1GHz is easily representable |
| 113 | * by 32bits. |
| 114 | */ |
Gabe Black | ad73b91 | 2013-03-28 04:32:20 +0000 | [diff] [blame] | 115 | if (period_ns > NS_IN_SEC || duty_ns > NS_IN_SEC || period_ns == 0) |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 116 | return -ERANGE; |
| 117 | |
| 118 | if (duty_ns > period_ns) |
| 119 | return -EINVAL; |
| 120 | |
Gabe Black | ad73b91 | 2013-03-28 04:32:20 +0000 | [diff] [blame] | 121 | frequency = NS_IN_SEC / period_ns; |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 122 | |
| 123 | /* Check to see if we are changing the clock rate of the PWM */ |
Gabe Black | ad73b91 | 2013-03-28 04:32:20 +0000 | [diff] [blame] | 124 | tin_rate = pwm_calc_tin(pwm_id, frequency); |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 125 | |
Gabe Black | ad73b91 | 2013-03-28 04:32:20 +0000 | [diff] [blame] | 126 | tin_ns = NS_IN_SEC / tin_rate; |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 127 | |
| 128 | if (IS_ENABLED(CONFIG_ARCH_NEXELL)) |
| 129 | /* The counter starts at zero. */ |
| 130 | tcnt = (period_ns / tin_ns) - 1; |
| 131 | else |
| 132 | tcnt = period_ns / tin_ns; |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 133 | |
| 134 | /* Note, counters count down */ |
| 135 | tcmp = duty_ns / tin_ns; |
| 136 | tcmp = tcnt - tcmp; |
| 137 | |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 138 | /* Update the PWM register block. */ |
| 139 | offset = pwm_id * 3; |
| 140 | if (pwm_id < 4) { |
| 141 | writel(tcnt, &pwm->tcntb0 + offset); |
| 142 | writel(tcmp, &pwm->tcmpb0 + offset); |
| 143 | } |
| 144 | |
| 145 | tcon = readl(&pwm->tcon); |
| 146 | tcon |= TCON_UPDATE(pwm_id); |
| 147 | if (pwm_id < 4) |
| 148 | tcon |= TCON_AUTO_RELOAD(pwm_id); |
| 149 | else |
| 150 | tcon |= TCON4_AUTO_RELOAD; |
| 151 | writel(tcon, &pwm->tcon); |
| 152 | |
| 153 | tcon &= ~TCON_UPDATE(pwm_id); |
| 154 | writel(tcon, &pwm->tcon); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Tom Rini | 66faa43 | 2022-12-04 10:03:26 -0500 | [diff] [blame] | 159 | int s5p_pwm_init(int pwm_id, int div, int invert) |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 160 | { |
| 161 | u32 val; |
| 162 | const struct s5p_timer *pwm = |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 163 | #if defined(CONFIG_ARCH_NEXELL) |
| 164 | (struct s5p_timer *)PHY_BASEADDR_PWM; |
| 165 | #else |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 166 | (struct s5p_timer *)samsung_get_base_timer(); |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 167 | #endif |
Gabe Black | 4f44a3c | 2013-03-28 04:32:18 +0000 | [diff] [blame] | 168 | unsigned long ticks_per_period; |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 169 | unsigned int offset, prescaler; |
| 170 | |
| 171 | /* |
| 172 | * Timer Freq(HZ) = |
| 173 | * PWM_CLK / { (prescaler_value + 1) * (divider_value) } |
| 174 | */ |
| 175 | |
| 176 | val = readl(&pwm->tcfg0); |
| 177 | if (pwm_id < 2) { |
| 178 | prescaler = PRESCALER_0; |
| 179 | val &= ~0xff; |
| 180 | val |= (prescaler & 0xff); |
| 181 | } else { |
| 182 | prescaler = PRESCALER_1; |
| 183 | val &= ~(0xff << 8); |
| 184 | val |= (prescaler & 0xff) << 8; |
| 185 | } |
| 186 | writel(val, &pwm->tcfg0); |
| 187 | val = readl(&pwm->tcfg1); |
| 188 | val &= ~(0xf << MUX_DIV_SHIFT(pwm_id)); |
| 189 | val |= (div & 0xf) << MUX_DIV_SHIFT(pwm_id); |
| 190 | writel(val, &pwm->tcfg1); |
| 191 | |
Gabe Black | 4f44a3c | 2013-03-28 04:32:18 +0000 | [diff] [blame] | 192 | if (pwm_id == 4) { |
| 193 | /* |
| 194 | * TODO(sjg): Use this as a countdown timer for now. We count |
| 195 | * down from the maximum value to 0, then reset. |
| 196 | */ |
| 197 | ticks_per_period = -1UL; |
| 198 | } else { |
| 199 | const unsigned long pwm_hz = 1000; |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 200 | #if defined(CONFIG_ARCH_NEXELL) |
| 201 | struct clk *clk = clk_get(CORECLK_NAME_PCLK); |
| 202 | unsigned long timer_rate_hz = clk_get_rate(clk) / |
| 203 | #else |
Gabe Black | 4f44a3c | 2013-03-28 04:32:18 +0000 | [diff] [blame] | 204 | unsigned long timer_rate_hz = get_pwm_clk() / |
Stefan Bosch | 34cffbf | 2020-07-10 19:07:31 +0200 | [diff] [blame] | 205 | #endif |
Gabe Black | 4f44a3c | 2013-03-28 04:32:18 +0000 | [diff] [blame] | 206 | ((prescaler + 1) * (1 << div)); |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 207 | |
Gabe Black | 4f44a3c | 2013-03-28 04:32:18 +0000 | [diff] [blame] | 208 | ticks_per_period = timer_rate_hz / pwm_hz; |
| 209 | } |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 210 | |
| 211 | /* set count value */ |
| 212 | offset = pwm_id * 3; |
Simon Glass | a9e9abb | 2013-03-28 04:32:16 +0000 | [diff] [blame] | 213 | |
Gabe Black | 4f44a3c | 2013-03-28 04:32:18 +0000 | [diff] [blame] | 214 | writel(ticks_per_period, &pwm->tcntb0 + offset); |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 215 | |
| 216 | val = readl(&pwm->tcon) & ~(0xf << TCON_OFFSET(pwm_id)); |
| 217 | if (invert && (pwm_id < 4)) |
| 218 | val |= TCON_INVERTER(pwm_id); |
| 219 | writel(val, &pwm->tcon); |
| 220 | |
Tom Rini | 66faa43 | 2022-12-04 10:03:26 -0500 | [diff] [blame] | 221 | s5p_pwm_enable(pwm_id); |
Donghwa Lee | 85f5df2 | 2011-03-07 21:11:42 +0000 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |