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