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