Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * http://www.samsung.com/ |
| 5 | * |
| 6 | * samsung - Common hr-timer support (s3c and s5p) |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 7 | */ |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 8 | |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/irq.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/clockchips.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_address.h> |
| 18 | #include <linux/of_irq.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/slab.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 21 | #include <linux/sched_clock.h> |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 22 | |
| 23 | #include <clocksource/samsung_pwm.h> |
| 24 | |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 25 | /* |
| 26 | * Clocksource driver |
| 27 | */ |
| 28 | |
| 29 | #define REG_TCFG0 0x00 |
| 30 | #define REG_TCFG1 0x04 |
| 31 | #define REG_TCON 0x08 |
| 32 | #define REG_TINT_CSTAT 0x44 |
| 33 | |
| 34 | #define REG_TCNTB(chan) (0x0c + 12 * (chan)) |
| 35 | #define REG_TCMPB(chan) (0x10 + 12 * (chan)) |
| 36 | |
| 37 | #define TCFG0_PRESCALER_MASK 0xff |
| 38 | #define TCFG0_PRESCALER1_SHIFT 8 |
| 39 | |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 40 | #define TCFG1_SHIFT(x) ((x) * 4) |
| 41 | #define TCFG1_MUX_MASK 0xf |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 42 | |
Tomasz Figa | ceea124 | 2013-06-17 02:10:24 +0200 | [diff] [blame] | 43 | /* |
| 44 | * Each channel occupies 4 bits in TCON register, but there is a gap of 4 |
| 45 | * bits (one channel) after channel 0, so channels have different numbering |
| 46 | * when accessing TCON register. |
| 47 | * |
| 48 | * In addition, the location of autoreload bit for channel 4 (TCON channel 5) |
| 49 | * in its set of bits is 2 as opposed to 3 for other channels. |
| 50 | */ |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 51 | #define TCON_START(chan) (1 << (4 * (chan) + 0)) |
| 52 | #define TCON_MANUALUPDATE(chan) (1 << (4 * (chan) + 1)) |
| 53 | #define TCON_INVERT(chan) (1 << (4 * (chan) + 2)) |
Tomasz Figa | ceea124 | 2013-06-17 02:10:24 +0200 | [diff] [blame] | 54 | #define _TCON_AUTORELOAD(chan) (1 << (4 * (chan) + 3)) |
| 55 | #define _TCON_AUTORELOAD4(chan) (1 << (4 * (chan) + 2)) |
| 56 | #define TCON_AUTORELOAD(chan) \ |
| 57 | ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan)) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 58 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 59 | DEFINE_SPINLOCK(samsung_pwm_lock); |
| 60 | EXPORT_SYMBOL(samsung_pwm_lock); |
| 61 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 62 | struct samsung_pwm_clocksource { |
| 63 | void __iomem *base; |
Krzysztof Kozlowski | b4318ce | 2021-05-06 16:27:28 -0400 | [diff] [blame] | 64 | const void __iomem *source_reg; |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 65 | unsigned int irq[SAMSUNG_PWM_NUM]; |
| 66 | struct samsung_pwm_variant variant; |
| 67 | |
| 68 | struct clk *timerclk; |
| 69 | |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 70 | unsigned int event_id; |
| 71 | unsigned int source_id; |
| 72 | unsigned int tcnt_max; |
| 73 | unsigned int tscaler_div; |
| 74 | unsigned int tdiv; |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 75 | |
| 76 | unsigned long clock_count_per_tick; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 79 | static struct samsung_pwm_clocksource pwm; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 80 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 81 | static void samsung_timer_set_prescale(unsigned int channel, u16 prescale) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 82 | { |
| 83 | unsigned long flags; |
| 84 | u8 shift = 0; |
| 85 | u32 reg; |
| 86 | |
| 87 | if (channel >= 2) |
| 88 | shift = TCFG0_PRESCALER1_SHIFT; |
| 89 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 90 | spin_lock_irqsave(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 91 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 92 | reg = readl(pwm.base + REG_TCFG0); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 93 | reg &= ~(TCFG0_PRESCALER_MASK << shift); |
| 94 | reg |= (prescale - 1) << shift; |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 95 | writel(reg, pwm.base + REG_TCFG0); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 96 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 97 | spin_unlock_irqrestore(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 100 | static void samsung_timer_set_divisor(unsigned int channel, u8 divisor) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 101 | { |
| 102 | u8 shift = TCFG1_SHIFT(channel); |
| 103 | unsigned long flags; |
| 104 | u32 reg; |
| 105 | u8 bits; |
| 106 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 107 | bits = (fls(divisor) - 1) - pwm.variant.div_base; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 108 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 109 | spin_lock_irqsave(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 110 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 111 | reg = readl(pwm.base + REG_TCFG1); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 112 | reg &= ~(TCFG1_MUX_MASK << shift); |
| 113 | reg |= bits << shift; |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 114 | writel(reg, pwm.base + REG_TCFG1); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 115 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 116 | spin_unlock_irqrestore(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void samsung_time_stop(unsigned int channel) |
| 120 | { |
| 121 | unsigned long tcon; |
| 122 | unsigned long flags; |
| 123 | |
| 124 | if (channel > 0) |
| 125 | ++channel; |
| 126 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 127 | spin_lock_irqsave(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 128 | |
Matthew Leach | 7cc0617 | 2016-06-16 15:51:29 +0200 | [diff] [blame] | 129 | tcon = readl_relaxed(pwm.base + REG_TCON); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 130 | tcon &= ~TCON_START(channel); |
Matthew Leach | 7cc0617 | 2016-06-16 15:51:29 +0200 | [diff] [blame] | 131 | writel_relaxed(tcon, pwm.base + REG_TCON); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 132 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 133 | spin_unlock_irqrestore(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void samsung_time_setup(unsigned int channel, unsigned long tcnt) |
| 137 | { |
| 138 | unsigned long tcon; |
| 139 | unsigned long flags; |
| 140 | unsigned int tcon_chan = channel; |
| 141 | |
| 142 | if (tcon_chan > 0) |
| 143 | ++tcon_chan; |
| 144 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 145 | spin_lock_irqsave(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 146 | |
Matthew Leach | 7cc0617 | 2016-06-16 15:51:29 +0200 | [diff] [blame] | 147 | tcon = readl_relaxed(pwm.base + REG_TCON); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 148 | |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 149 | tcon &= ~(TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan)); |
| 150 | tcon |= TCON_MANUALUPDATE(tcon_chan); |
| 151 | |
Matthew Leach | 7cc0617 | 2016-06-16 15:51:29 +0200 | [diff] [blame] | 152 | writel_relaxed(tcnt, pwm.base + REG_TCNTB(channel)); |
| 153 | writel_relaxed(tcnt, pwm.base + REG_TCMPB(channel)); |
| 154 | writel_relaxed(tcon, pwm.base + REG_TCON); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 155 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 156 | spin_unlock_irqrestore(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void samsung_time_start(unsigned int channel, bool periodic) |
| 160 | { |
| 161 | unsigned long tcon; |
| 162 | unsigned long flags; |
| 163 | |
| 164 | if (channel > 0) |
| 165 | ++channel; |
| 166 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 167 | spin_lock_irqsave(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 168 | |
Matthew Leach | 7cc0617 | 2016-06-16 15:51:29 +0200 | [diff] [blame] | 169 | tcon = readl_relaxed(pwm.base + REG_TCON); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 170 | |
| 171 | tcon &= ~TCON_MANUALUPDATE(channel); |
| 172 | tcon |= TCON_START(channel); |
| 173 | |
| 174 | if (periodic) |
| 175 | tcon |= TCON_AUTORELOAD(channel); |
| 176 | else |
| 177 | tcon &= ~TCON_AUTORELOAD(channel); |
| 178 | |
Matthew Leach | 7cc0617 | 2016-06-16 15:51:29 +0200 | [diff] [blame] | 179 | writel_relaxed(tcon, pwm.base + REG_TCON); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 180 | |
Tomasz Figa | 7aac482 | 2013-04-23 17:46:24 +0200 | [diff] [blame] | 181 | spin_unlock_irqrestore(&samsung_pwm_lock, flags); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static int samsung_set_next_event(unsigned long cycles, |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 185 | struct clock_event_device *evt) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 186 | { |
Tomasz Figa | 81d4f7b | 2013-04-23 17:46:30 +0200 | [diff] [blame] | 187 | /* |
| 188 | * This check is needed to account for internal rounding |
| 189 | * errors inside clockevents core, which might result in |
| 190 | * passing cycles = 0, which in turn would not generate any |
| 191 | * timer interrupt and hang the system. |
| 192 | * |
| 193 | * Another solution would be to set up the clockevent device |
| 194 | * with min_delta = 2, but this would unnecessarily increase |
| 195 | * the minimum sleep period. |
| 196 | */ |
| 197 | if (!cycles) |
| 198 | cycles = 1; |
| 199 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 200 | samsung_time_setup(pwm.event_id, cycles); |
| 201 | samsung_time_start(pwm.event_id, false); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Viresh Kumar | b49b570 | 2015-06-18 16:24:33 +0530 | [diff] [blame] | 206 | static int samsung_shutdown(struct clock_event_device *evt) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 207 | { |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 208 | samsung_time_stop(pwm.event_id); |
Viresh Kumar | b49b570 | 2015-06-18 16:24:33 +0530 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 211 | |
Viresh Kumar | b49b570 | 2015-06-18 16:24:33 +0530 | [diff] [blame] | 212 | static int samsung_set_periodic(struct clock_event_device *evt) |
| 213 | { |
| 214 | samsung_time_stop(pwm.event_id); |
| 215 | samsung_time_setup(pwm.event_id, pwm.clock_count_per_tick - 1); |
| 216 | samsung_time_start(pwm.event_id, true); |
| 217 | return 0; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Tomasz Figa | 0b96258 | 2013-06-17 01:11:31 +0200 | [diff] [blame] | 220 | static void samsung_clockevent_resume(struct clock_event_device *cev) |
| 221 | { |
| 222 | samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div); |
| 223 | samsung_timer_set_divisor(pwm.event_id, pwm.tdiv); |
| 224 | |
| 225 | if (pwm.variant.has_tint_cstat) { |
| 226 | u32 mask = (1 << pwm.event_id); |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 227 | |
Tomasz Figa | 0b96258 | 2013-06-17 01:11:31 +0200 | [diff] [blame] | 228 | writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT); |
| 229 | } |
| 230 | } |
| 231 | |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 232 | static struct clock_event_device time_event_device = { |
Viresh Kumar | b49b570 | 2015-06-18 16:24:33 +0530 | [diff] [blame] | 233 | .name = "samsung_event_timer", |
| 234 | .features = CLOCK_EVT_FEAT_PERIODIC | |
| 235 | CLOCK_EVT_FEAT_ONESHOT, |
| 236 | .rating = 200, |
| 237 | .set_next_event = samsung_set_next_event, |
| 238 | .set_state_shutdown = samsung_shutdown, |
| 239 | .set_state_periodic = samsung_set_periodic, |
| 240 | .set_state_oneshot = samsung_shutdown, |
| 241 | .tick_resume = samsung_shutdown, |
| 242 | .resume = samsung_clockevent_resume, |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | static irqreturn_t samsung_clock_event_isr(int irq, void *dev_id) |
| 246 | { |
| 247 | struct clock_event_device *evt = dev_id; |
| 248 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 249 | if (pwm.variant.has_tint_cstat) { |
| 250 | u32 mask = (1 << pwm.event_id); |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 251 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 252 | writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | evt->event_handler(evt); |
| 256 | |
| 257 | return IRQ_HANDLED; |
| 258 | } |
| 259 | |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 260 | static void __init samsung_clockevent_init(void) |
| 261 | { |
| 262 | unsigned long pclk; |
| 263 | unsigned long clock_rate; |
| 264 | unsigned int irq_number; |
| 265 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 266 | pclk = clk_get_rate(pwm.timerclk); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 267 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 268 | samsung_timer_set_prescale(pwm.event_id, pwm.tscaler_div); |
| 269 | samsung_timer_set_divisor(pwm.event_id, pwm.tdiv); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 270 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 271 | clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv); |
| 272 | pwm.clock_count_per_tick = clock_rate / HZ; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 273 | |
| 274 | time_event_device.cpumask = cpumask_of(0); |
Tomasz Figa | e9b852b | 2013-04-23 17:46:28 +0200 | [diff] [blame] | 275 | clockevents_config_and_register(&time_event_device, |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 276 | clock_rate, 1, pwm.tcnt_max); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 277 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 278 | irq_number = pwm.irq[pwm.event_id]; |
afzal mohammed | cc2550b | 2020-02-27 16:29:02 +0530 | [diff] [blame] | 279 | if (request_irq(irq_number, samsung_clock_event_isr, |
| 280 | IRQF_TIMER | IRQF_IRQPOLL, "samsung_time_irq", |
| 281 | &time_event_device)) |
| 282 | pr_err("%s: request_irq() failed\n", "samsung_time_irq"); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 283 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 284 | if (pwm.variant.has_tint_cstat) { |
| 285 | u32 mask = (1 << pwm.event_id); |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 286 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 287 | writel(mask | (mask << 5), pwm.base + REG_TINT_CSTAT); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Tomasz Figa | 0b96258 | 2013-06-17 01:11:31 +0200 | [diff] [blame] | 291 | static void samsung_clocksource_suspend(struct clocksource *cs) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 292 | { |
Tomasz Figa | 0b96258 | 2013-06-17 01:11:31 +0200 | [diff] [blame] | 293 | samsung_time_stop(pwm.source_id); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Tomasz Figa | 0b96258 | 2013-06-17 01:11:31 +0200 | [diff] [blame] | 296 | static void samsung_clocksource_resume(struct clocksource *cs) |
| 297 | { |
| 298 | samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div); |
| 299 | samsung_timer_set_divisor(pwm.source_id, pwm.tdiv); |
| 300 | |
| 301 | samsung_time_setup(pwm.source_id, pwm.tcnt_max); |
| 302 | samsung_time_start(pwm.source_id, true); |
| 303 | } |
| 304 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 305 | static u64 notrace samsung_clocksource_read(struct clocksource *c) |
Tomasz Figa | 6792e63 | 2013-06-17 00:13:06 +0200 | [diff] [blame] | 306 | { |
| 307 | return ~readl_relaxed(pwm.source_reg); |
| 308 | } |
| 309 | |
| 310 | static struct clocksource samsung_clocksource = { |
| 311 | .name = "samsung_clocksource_timer", |
| 312 | .rating = 250, |
| 313 | .read = samsung_clocksource_read, |
Tomasz Figa | 0b96258 | 2013-06-17 01:11:31 +0200 | [diff] [blame] | 314 | .suspend = samsung_clocksource_suspend, |
| 315 | .resume = samsung_clocksource_resume, |
Tomasz Figa | 6792e63 | 2013-06-17 00:13:06 +0200 | [diff] [blame] | 316 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 317 | }; |
| 318 | |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 319 | /* |
| 320 | * Override the global weak sched_clock symbol with this |
| 321 | * local implementation which uses the clocksource to get some |
| 322 | * better resolution when scheduling the kernel. We accept that |
| 323 | * this wraps around for now, since it is just a relative time |
| 324 | * stamp. (Inspired by U300 implementation.) |
| 325 | */ |
Stephen Boyd | 2902b30 | 2013-07-18 16:21:25 -0700 | [diff] [blame] | 326 | static u64 notrace samsung_read_sched_clock(void) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 327 | { |
Tomasz Figa | 6792e63 | 2013-06-17 00:13:06 +0200 | [diff] [blame] | 328 | return samsung_clocksource_read(NULL); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 331 | static int __init samsung_clocksource_init(void) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 332 | { |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 333 | unsigned long pclk; |
| 334 | unsigned long clock_rate; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 335 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 336 | pclk = clk_get_rate(pwm.timerclk); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 337 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 338 | samsung_timer_set_prescale(pwm.source_id, pwm.tscaler_div); |
| 339 | samsung_timer_set_divisor(pwm.source_id, pwm.tdiv); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 340 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 341 | clock_rate = pclk / (pwm.tscaler_div * pwm.tdiv); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 342 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 343 | samsung_time_setup(pwm.source_id, pwm.tcnt_max); |
| 344 | samsung_time_start(pwm.source_id, true); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 345 | |
Tomasz Figa | 61d7e20 | 2013-06-17 00:07:03 +0200 | [diff] [blame] | 346 | if (pwm.source_id == 4) |
| 347 | pwm.source_reg = pwm.base + 0x40; |
| 348 | else |
| 349 | pwm.source_reg = pwm.base + pwm.source_id * 0x0c + 0x14; |
| 350 | |
Stephen Boyd | 2902b30 | 2013-07-18 16:21:25 -0700 | [diff] [blame] | 351 | sched_clock_register(samsung_read_sched_clock, |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 352 | pwm.variant.bits, clock_rate); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 353 | |
Tomasz Figa | 6792e63 | 2013-06-17 00:13:06 +0200 | [diff] [blame] | 354 | samsung_clocksource.mask = CLOCKSOURCE_MASK(pwm.variant.bits); |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 355 | return clocksource_register_hz(&samsung_clocksource, clock_rate); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void __init samsung_timer_resources(void) |
| 359 | { |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 360 | clk_prepare_enable(pwm.timerclk); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 361 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 362 | pwm.tcnt_max = (1UL << pwm.variant.bits) - 1; |
| 363 | if (pwm.variant.bits == 16) { |
| 364 | pwm.tscaler_div = 25; |
| 365 | pwm.tdiv = 2; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 366 | } else { |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 367 | pwm.tscaler_div = 2; |
| 368 | pwm.tdiv = 1; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * PWM master driver |
| 374 | */ |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 375 | static int __init _samsung_pwm_clocksource_init(void) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 376 | { |
| 377 | u8 mask; |
| 378 | int channel; |
| 379 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 380 | mask = ~pwm.variant.output_mask & ((1 << SAMSUNG_PWM_NUM) - 1); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 381 | channel = fls(mask) - 1; |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 382 | if (channel < 0) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 383 | pr_crit("failed to find PWM channel for clocksource\n"); |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 384 | return -EINVAL; |
| 385 | } |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 386 | pwm.source_id = channel; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 387 | |
| 388 | mask &= ~(1 << channel); |
| 389 | channel = fls(mask) - 1; |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 390 | if (channel < 0) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 391 | pr_crit("failed to find PWM channel for clock event\n"); |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 392 | return -EINVAL; |
| 393 | } |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 394 | pwm.event_id = channel; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 395 | |
| 396 | samsung_timer_resources(); |
| 397 | samsung_clockevent_init(); |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 398 | |
| 399 | return samsung_clocksource_init(); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Tomasz Figa | f9bb48a2 | 2013-04-23 17:46:27 +0200 | [diff] [blame] | 402 | void __init samsung_pwm_clocksource_init(void __iomem *base, |
Krzysztof Kozlowski | a0143f5a | 2021-05-06 16:27:25 -0400 | [diff] [blame] | 403 | unsigned int *irqs, |
Krzysztof Kozlowski | bb08e96 | 2021-05-06 16:27:26 -0400 | [diff] [blame] | 404 | const struct samsung_pwm_variant *variant) |
Tomasz Figa | f9bb48a2 | 2013-04-23 17:46:27 +0200 | [diff] [blame] | 405 | { |
| 406 | pwm.base = base; |
| 407 | memcpy(&pwm.variant, variant, sizeof(pwm.variant)); |
| 408 | memcpy(pwm.irq, irqs, SAMSUNG_PWM_NUM * sizeof(*irqs)); |
| 409 | |
Tomasz Figa | a1fa6f5 | 2013-08-26 19:08:58 +0200 | [diff] [blame] | 410 | pwm.timerclk = clk_get(NULL, "timers"); |
| 411 | if (IS_ERR(pwm.timerclk)) |
| 412 | panic("failed to get timers clock for timer"); |
| 413 | |
Tomasz Figa | f9bb48a2 | 2013-04-23 17:46:27 +0200 | [diff] [blame] | 414 | _samsung_pwm_clocksource_init(); |
| 415 | } |
| 416 | |
Daniel Lezcano | bb0eb05 | 2017-05-26 19:34:11 +0200 | [diff] [blame] | 417 | #ifdef CONFIG_TIMER_OF |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 418 | static int __init samsung_pwm_alloc(struct device_node *np, |
| 419 | const struct samsung_pwm_variant *variant) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 420 | { |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 421 | struct property *prop; |
| 422 | const __be32 *cur; |
| 423 | u32 val; |
Krzysztof Kozlowski | 63e83bd | 2021-05-06 16:27:27 -0400 | [diff] [blame] | 424 | int i, ret; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 425 | |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 426 | memcpy(&pwm.variant, variant, sizeof(pwm.variant)); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 427 | for (i = 0; i < SAMSUNG_PWM_NUM; ++i) |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 428 | pwm.irq[i] = irq_of_parse_and_map(np, i); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 429 | |
| 430 | of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) { |
| 431 | if (val >= SAMSUNG_PWM_NUM) { |
Kefeng Wang | 43fc6b2 | 2019-10-18 11:18:28 +0800 | [diff] [blame] | 432 | pr_warn("%s: invalid channel index in samsung,pwm-outputs property\n", __func__); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 433 | continue; |
| 434 | } |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 435 | pwm.variant.output_mask |= 1 << val; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Tomasz Figa | e241548 | 2013-06-13 21:22:44 +0200 | [diff] [blame] | 438 | pwm.base = of_iomap(np, 0); |
Tomasz Figa | 030c2a1 | 2013-04-23 17:46:25 +0200 | [diff] [blame] | 439 | if (!pwm.base) { |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 440 | pr_err("%s: failed to map PWM registers\n", __func__); |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 441 | return -ENXIO; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Tomasz Figa | a1fa6f5 | 2013-08-26 19:08:58 +0200 | [diff] [blame] | 444 | pwm.timerclk = of_clk_get_by_name(np, "timers"); |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 445 | if (IS_ERR(pwm.timerclk)) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 446 | pr_crit("failed to get timers clock for timer\n"); |
Krzysztof Kozlowski | 63e83bd | 2021-05-06 16:27:27 -0400 | [diff] [blame] | 447 | ret = PTR_ERR(pwm.timerclk); |
| 448 | goto err_clk; |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 449 | } |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 450 | |
Krzysztof Kozlowski | 63e83bd | 2021-05-06 16:27:27 -0400 | [diff] [blame] | 451 | ret = _samsung_pwm_clocksource_init(); |
| 452 | if (ret) |
| 453 | goto err_clocksource; |
| 454 | |
| 455 | return 0; |
| 456 | |
| 457 | err_clocksource: |
| 458 | clk_put(pwm.timerclk); |
| 459 | pwm.timerclk = NULL; |
| 460 | err_clk: |
| 461 | iounmap(pwm.base); |
| 462 | pwm.base = NULL; |
| 463 | |
| 464 | return ret; |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static const struct samsung_pwm_variant s3c24xx_variant = { |
| 468 | .bits = 16, |
| 469 | .div_base = 1, |
| 470 | .has_tint_cstat = false, |
| 471 | .tclk_mask = (1 << 4), |
| 472 | }; |
| 473 | |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 474 | static int __init s3c2410_pwm_clocksource_init(struct device_node *np) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 475 | { |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 476 | return samsung_pwm_alloc(np, &s3c24xx_variant); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 477 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 478 | TIMER_OF_DECLARE(s3c2410_pwm, "samsung,s3c2410-pwm", s3c2410_pwm_clocksource_init); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 479 | |
| 480 | static const struct samsung_pwm_variant s3c64xx_variant = { |
| 481 | .bits = 32, |
| 482 | .div_base = 0, |
| 483 | .has_tint_cstat = true, |
| 484 | .tclk_mask = (1 << 7) | (1 << 6) | (1 << 5), |
| 485 | }; |
| 486 | |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 487 | static int __init s3c64xx_pwm_clocksource_init(struct device_node *np) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 488 | { |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 489 | return samsung_pwm_alloc(np, &s3c64xx_variant); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 490 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 491 | TIMER_OF_DECLARE(s3c6400_pwm, "samsung,s3c6400-pwm", s3c64xx_pwm_clocksource_init); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 492 | |
| 493 | static const struct samsung_pwm_variant s5p64x0_variant = { |
| 494 | .bits = 32, |
| 495 | .div_base = 0, |
| 496 | .has_tint_cstat = true, |
| 497 | .tclk_mask = 0, |
| 498 | }; |
| 499 | |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 500 | static int __init s5p64x0_pwm_clocksource_init(struct device_node *np) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 501 | { |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 502 | return samsung_pwm_alloc(np, &s5p64x0_variant); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 503 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 504 | TIMER_OF_DECLARE(s5p6440_pwm, "samsung,s5p6440-pwm", s5p64x0_pwm_clocksource_init); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 505 | |
| 506 | static const struct samsung_pwm_variant s5p_variant = { |
| 507 | .bits = 32, |
| 508 | .div_base = 0, |
| 509 | .has_tint_cstat = true, |
| 510 | .tclk_mask = (1 << 5), |
| 511 | }; |
| 512 | |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 513 | static int __init s5p_pwm_clocksource_init(struct device_node *np) |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 514 | { |
Daniel Lezcano | 0993f57 | 2016-06-06 17:58:56 +0200 | [diff] [blame] | 515 | return samsung_pwm_alloc(np, &s5p_variant); |
Tomasz Figa | f118998 | 2013-04-20 23:22:13 +0200 | [diff] [blame] | 516 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 517 | TIMER_OF_DECLARE(s5pc100_pwm, "samsung,s5pc100-pwm", s5p_pwm_clocksource_init); |
Tomasz Figa | f9bb48a2 | 2013-04-23 17:46:27 +0200 | [diff] [blame] | 518 | #endif |