Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Timer Support - TMU |
| 3 | * |
| 4 | * Copyright (C) 2009 Magnus Damm |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/irq.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/clocksource.h> |
| 31 | #include <linux/clockchips.h> |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 32 | #include <linux/sh_timer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Paul Gortmaker | 7deeab5 | 2011-07-03 13:36:22 -0400 | [diff] [blame] | 34 | #include <linux/module.h> |
Rafael J. Wysocki | 2ee619f | 2012-03-13 22:40:00 +0100 | [diff] [blame] | 35 | #include <linux/pm_domain.h> |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 36 | #include <linux/pm_runtime.h> |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 37 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 38 | struct sh_tmu_device; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 39 | |
| 40 | struct sh_tmu_channel { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 41 | struct sh_tmu_device *tmu; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 42 | |
Laurent Pinchart | 1c56cf6 | 2014-02-17 11:27:49 +0100 | [diff] [blame] | 43 | int irq; |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 44 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 45 | unsigned long rate; |
| 46 | unsigned long periodic; |
| 47 | struct clock_event_device ced; |
| 48 | struct clocksource cs; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 49 | bool cs_enabled; |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 50 | unsigned int enable_count; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 53 | struct sh_tmu_device { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 54 | struct platform_device *pdev; |
| 55 | |
| 56 | void __iomem *mapbase; |
| 57 | struct clk *clk; |
| 58 | |
| 59 | struct sh_tmu_channel channel; |
| 60 | }; |
| 61 | |
Paul Mundt | c2225a5 | 2012-05-25 13:39:09 +0900 | [diff] [blame] | 62 | static DEFINE_RAW_SPINLOCK(sh_tmu_lock); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 63 | |
| 64 | #define TSTR -1 /* shared register */ |
| 65 | #define TCOR 0 /* channel register */ |
| 66 | #define TCNT 1 /* channel register */ |
| 67 | #define TCR 2 /* channel register */ |
| 68 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 69 | static inline unsigned long sh_tmu_read(struct sh_tmu_channel *ch, int reg_nr) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 70 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 71 | struct sh_timer_config *cfg = ch->tmu->pdev->dev.platform_data; |
| 72 | void __iomem *base = ch->tmu->mapbase; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 73 | unsigned long offs; |
| 74 | |
| 75 | if (reg_nr == TSTR) |
| 76 | return ioread8(base - cfg->channel_offset); |
| 77 | |
| 78 | offs = reg_nr << 2; |
| 79 | |
| 80 | if (reg_nr == TCR) |
| 81 | return ioread16(base + offs); |
| 82 | else |
| 83 | return ioread32(base + offs); |
| 84 | } |
| 85 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 86 | static inline void sh_tmu_write(struct sh_tmu_channel *ch, int reg_nr, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 87 | unsigned long value) |
| 88 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 89 | struct sh_timer_config *cfg = ch->tmu->pdev->dev.platform_data; |
| 90 | void __iomem *base = ch->tmu->mapbase; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 91 | unsigned long offs; |
| 92 | |
| 93 | if (reg_nr == TSTR) { |
| 94 | iowrite8(value, base - cfg->channel_offset); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | offs = reg_nr << 2; |
| 99 | |
| 100 | if (reg_nr == TCR) |
| 101 | iowrite16(value, base + offs); |
| 102 | else |
| 103 | iowrite32(value, base + offs); |
| 104 | } |
| 105 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 106 | static void sh_tmu_start_stop_ch(struct sh_tmu_channel *ch, int start) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 107 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 108 | struct sh_timer_config *cfg = ch->tmu->pdev->dev.platform_data; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 109 | unsigned long flags, value; |
| 110 | |
| 111 | /* start stop register shared by multiple timer channels */ |
Paul Mundt | c2225a5 | 2012-05-25 13:39:09 +0900 | [diff] [blame] | 112 | raw_spin_lock_irqsave(&sh_tmu_lock, flags); |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 113 | value = sh_tmu_read(ch, TSTR); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 114 | |
| 115 | if (start) |
| 116 | value |= 1 << cfg->timer_bit; |
| 117 | else |
| 118 | value &= ~(1 << cfg->timer_bit); |
| 119 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 120 | sh_tmu_write(ch, TSTR, value); |
Paul Mundt | c2225a5 | 2012-05-25 13:39:09 +0900 | [diff] [blame] | 121 | raw_spin_unlock_irqrestore(&sh_tmu_lock, flags); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 124 | static int __sh_tmu_enable(struct sh_tmu_channel *ch) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 125 | { |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 126 | int ret; |
| 127 | |
Paul Mundt | d4905ce | 2011-05-31 15:23:20 +0900 | [diff] [blame] | 128 | /* enable clock */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 129 | ret = clk_enable(ch->tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 130 | if (ret) { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 131 | dev_err(&ch->tmu->pdev->dev, "cannot enable clock\n"); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | /* make sure channel is disabled */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 136 | sh_tmu_start_stop_ch(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 137 | |
| 138 | /* maximum timeout */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 139 | sh_tmu_write(ch, TCOR, 0xffffffff); |
| 140 | sh_tmu_write(ch, TCNT, 0xffffffff); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 141 | |
| 142 | /* configure channel to parent clock / 4, irq off */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 143 | ch->rate = clk_get_rate(ch->tmu->clk) / 4; |
| 144 | sh_tmu_write(ch, TCR, 0x0000); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 145 | |
| 146 | /* enable channel */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 147 | sh_tmu_start_stop_ch(ch, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 152 | static int sh_tmu_enable(struct sh_tmu_channel *ch) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 153 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 154 | if (ch->enable_count++ > 0) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 157 | pm_runtime_get_sync(&ch->tmu->pdev->dev); |
| 158 | dev_pm_syscore_device(&ch->tmu->pdev->dev, true); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 159 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 160 | return __sh_tmu_enable(ch); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 163 | static void __sh_tmu_disable(struct sh_tmu_channel *ch) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 164 | { |
| 165 | /* disable channel */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 166 | sh_tmu_start_stop_ch(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 167 | |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 168 | /* disable interrupts in TMU block */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 169 | sh_tmu_write(ch, TCR, 0x0000); |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 170 | |
Paul Mundt | d4905ce | 2011-05-31 15:23:20 +0900 | [diff] [blame] | 171 | /* stop clock */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 172 | clk_disable(ch->tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 175 | static void sh_tmu_disable(struct sh_tmu_channel *ch) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 176 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 177 | if (WARN_ON(ch->enable_count == 0)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 178 | return; |
| 179 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 180 | if (--ch->enable_count > 0) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 181 | return; |
| 182 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 183 | __sh_tmu_disable(ch); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 184 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 185 | dev_pm_syscore_device(&ch->tmu->pdev->dev, false); |
| 186 | pm_runtime_put(&ch->tmu->pdev->dev); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 189 | static void sh_tmu_set_next(struct sh_tmu_channel *ch, unsigned long delta, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 190 | int periodic) |
| 191 | { |
| 192 | /* stop timer */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 193 | sh_tmu_start_stop_ch(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 194 | |
| 195 | /* acknowledge interrupt */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 196 | sh_tmu_read(ch, TCR); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 197 | |
| 198 | /* enable interrupt */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 199 | sh_tmu_write(ch, TCR, 0x0020); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 200 | |
| 201 | /* reload delta value in case of periodic timer */ |
| 202 | if (periodic) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 203 | sh_tmu_write(ch, TCOR, delta); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 204 | else |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 205 | sh_tmu_write(ch, TCOR, 0xffffffff); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 206 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 207 | sh_tmu_write(ch, TCNT, delta); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 208 | |
| 209 | /* start timer */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 210 | sh_tmu_start_stop_ch(ch, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static irqreturn_t sh_tmu_interrupt(int irq, void *dev_id) |
| 214 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 215 | struct sh_tmu_channel *ch = dev_id; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 216 | |
| 217 | /* disable or acknowledge interrupt */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 218 | if (ch->ced.mode == CLOCK_EVT_MODE_ONESHOT) |
| 219 | sh_tmu_write(ch, TCR, 0x0000); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 220 | else |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 221 | sh_tmu_write(ch, TCR, 0x0020); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 222 | |
| 223 | /* notify clockevent layer */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 224 | ch->ced.event_handler(&ch->ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 225 | return IRQ_HANDLED; |
| 226 | } |
| 227 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 228 | static struct sh_tmu_channel *cs_to_sh_tmu(struct clocksource *cs) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 229 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 230 | return container_of(cs, struct sh_tmu_channel, cs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static cycle_t sh_tmu_clocksource_read(struct clocksource *cs) |
| 234 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 235 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 236 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 237 | return sh_tmu_read(ch, TCNT) ^ 0xffffffff; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static int sh_tmu_clocksource_enable(struct clocksource *cs) |
| 241 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 242 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Magnus Damm | 0aeac45 | 2011-04-25 22:38:37 +0900 | [diff] [blame] | 243 | int ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 244 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 245 | if (WARN_ON(ch->cs_enabled)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 248 | ret = sh_tmu_enable(ch); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 249 | if (!ret) { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 250 | __clocksource_updatefreq_hz(cs, ch->rate); |
| 251 | ch->cs_enabled = true; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 252 | } |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 253 | |
Magnus Damm | 0aeac45 | 2011-04-25 22:38:37 +0900 | [diff] [blame] | 254 | return ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void sh_tmu_clocksource_disable(struct clocksource *cs) |
| 258 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 259 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 260 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 261 | if (WARN_ON(!ch->cs_enabled)) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 262 | return; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 263 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 264 | sh_tmu_disable(ch); |
| 265 | ch->cs_enabled = false; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void sh_tmu_clocksource_suspend(struct clocksource *cs) |
| 269 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 270 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 271 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 272 | if (!ch->cs_enabled) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 273 | return; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 274 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 275 | if (--ch->enable_count == 0) { |
| 276 | __sh_tmu_disable(ch); |
| 277 | pm_genpd_syscore_poweroff(&ch->tmu->pdev->dev); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 278 | } |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void sh_tmu_clocksource_resume(struct clocksource *cs) |
| 282 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 283 | struct sh_tmu_channel *ch = cs_to_sh_tmu(cs); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 284 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 285 | if (!ch->cs_enabled) |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 286 | return; |
| 287 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 288 | if (ch->enable_count++ == 0) { |
| 289 | pm_genpd_syscore_poweron(&ch->tmu->pdev->dev); |
| 290 | __sh_tmu_enable(ch); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 291 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 294 | static int sh_tmu_register_clocksource(struct sh_tmu_channel *ch, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 295 | char *name, unsigned long rating) |
| 296 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 297 | struct clocksource *cs = &ch->cs; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 298 | |
| 299 | cs->name = name; |
| 300 | cs->rating = rating; |
| 301 | cs->read = sh_tmu_clocksource_read; |
| 302 | cs->enable = sh_tmu_clocksource_enable; |
| 303 | cs->disable = sh_tmu_clocksource_disable; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 304 | cs->suspend = sh_tmu_clocksource_suspend; |
| 305 | cs->resume = sh_tmu_clocksource_resume; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 306 | cs->mask = CLOCKSOURCE_MASK(32); |
| 307 | cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; |
Aurelien Jarno | 66f4912 | 2010-05-31 21:45:48 +0000 | [diff] [blame] | 308 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 309 | dev_info(&ch->tmu->pdev->dev, "used as clock source\n"); |
Magnus Damm | 0aeac45 | 2011-04-25 22:38:37 +0900 | [diff] [blame] | 310 | |
| 311 | /* Register with dummy 1 Hz value, gets updated in ->enable() */ |
| 312 | clocksource_register_hz(cs, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 316 | static struct sh_tmu_channel *ced_to_sh_tmu(struct clock_event_device *ced) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 317 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 318 | return container_of(ced, struct sh_tmu_channel, ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 321 | static void sh_tmu_clock_event_start(struct sh_tmu_channel *ch, int periodic) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 322 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 323 | struct clock_event_device *ced = &ch->ced; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 324 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 325 | sh_tmu_enable(ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 326 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 327 | clockevents_config(ced, ch->rate); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 328 | |
| 329 | if (periodic) { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 330 | ch->periodic = (ch->rate + HZ/2) / HZ; |
| 331 | sh_tmu_set_next(ch, ch->periodic, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | static void sh_tmu_clock_event_mode(enum clock_event_mode mode, |
| 336 | struct clock_event_device *ced) |
| 337 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 338 | struct sh_tmu_channel *ch = ced_to_sh_tmu(ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 339 | int disabled = 0; |
| 340 | |
| 341 | /* deal with old setting first */ |
| 342 | switch (ced->mode) { |
| 343 | case CLOCK_EVT_MODE_PERIODIC: |
| 344 | case CLOCK_EVT_MODE_ONESHOT: |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 345 | sh_tmu_disable(ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 346 | disabled = 1; |
| 347 | break; |
| 348 | default: |
| 349 | break; |
| 350 | } |
| 351 | |
| 352 | switch (mode) { |
| 353 | case CLOCK_EVT_MODE_PERIODIC: |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 354 | dev_info(&ch->tmu->pdev->dev, |
| 355 | "used for periodic clock events\n"); |
| 356 | sh_tmu_clock_event_start(ch, 1); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 357 | break; |
| 358 | case CLOCK_EVT_MODE_ONESHOT: |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 359 | dev_info(&ch->tmu->pdev->dev, |
| 360 | "used for oneshot clock events\n"); |
| 361 | sh_tmu_clock_event_start(ch, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 362 | break; |
| 363 | case CLOCK_EVT_MODE_UNUSED: |
| 364 | if (!disabled) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 365 | sh_tmu_disable(ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 366 | break; |
| 367 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 368 | default: |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static int sh_tmu_clock_event_next(unsigned long delta, |
| 374 | struct clock_event_device *ced) |
| 375 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 376 | struct sh_tmu_channel *ch = ced_to_sh_tmu(ced); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 377 | |
| 378 | BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT); |
| 379 | |
| 380 | /* program new delta value */ |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 381 | sh_tmu_set_next(ch, delta, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 385 | static void sh_tmu_clock_event_suspend(struct clock_event_device *ced) |
| 386 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 387 | pm_genpd_syscore_poweroff(&ced_to_sh_tmu(ced)->tmu->pdev->dev); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void sh_tmu_clock_event_resume(struct clock_event_device *ced) |
| 391 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 392 | pm_genpd_syscore_poweron(&ced_to_sh_tmu(ced)->tmu->pdev->dev); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 395 | static void sh_tmu_register_clockevent(struct sh_tmu_channel *ch, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 396 | char *name, unsigned long rating) |
| 397 | { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 398 | struct clock_event_device *ced = &ch->ced; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 399 | int ret; |
| 400 | |
| 401 | memset(ced, 0, sizeof(*ced)); |
| 402 | |
| 403 | ced->name = name; |
| 404 | ced->features = CLOCK_EVT_FEAT_PERIODIC; |
| 405 | ced->features |= CLOCK_EVT_FEAT_ONESHOT; |
| 406 | ced->rating = rating; |
| 407 | ced->cpumask = cpumask_of(0); |
| 408 | ced->set_next_event = sh_tmu_clock_event_next; |
| 409 | ced->set_mode = sh_tmu_clock_event_mode; |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 410 | ced->suspend = sh_tmu_clock_event_suspend; |
| 411 | ced->resume = sh_tmu_clock_event_resume; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 412 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 413 | dev_info(&ch->tmu->pdev->dev, "used for clock events\n"); |
Paul Mundt | 3977407 | 2012-06-11 17:10:16 +0900 | [diff] [blame] | 414 | |
| 415 | clockevents_config_and_register(ced, 1, 0x300, 0xffffffff); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 416 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 417 | ret = request_irq(ch->irq, sh_tmu_interrupt, |
Laurent Pinchart | 1c56cf6 | 2014-02-17 11:27:49 +0100 | [diff] [blame] | 418 | IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING, |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 419 | dev_name(&ch->tmu->pdev->dev), ch); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 420 | if (ret) { |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 421 | dev_err(&ch->tmu->pdev->dev, "failed to request irq %d\n", |
| 422 | ch->irq); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 423 | return; |
| 424 | } |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 427 | static int sh_tmu_register(struct sh_tmu_channel *ch, char *name, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 428 | unsigned long clockevent_rating, |
| 429 | unsigned long clocksource_rating) |
| 430 | { |
| 431 | if (clockevent_rating) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 432 | sh_tmu_register_clockevent(ch, name, clockevent_rating); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 433 | else if (clocksource_rating) |
Laurent Pinchart | de2d12c | 2014-01-27 15:29:19 +0100 | [diff] [blame] | 434 | sh_tmu_register_clocksource(ch, name, clocksource_rating); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 439 | static int sh_tmu_setup(struct sh_tmu_device *tmu, struct platform_device *pdev) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 440 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 441 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 442 | struct resource *res; |
Laurent Pinchart | 1c56cf6 | 2014-02-17 11:27:49 +0100 | [diff] [blame] | 443 | int ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 444 | ret = -ENXIO; |
| 445 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 446 | memset(tmu, 0, sizeof(*tmu)); |
| 447 | tmu->pdev = pdev; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 448 | |
| 449 | if (!cfg) { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 450 | dev_err(&tmu->pdev->dev, "missing platform data\n"); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 451 | goto err0; |
| 452 | } |
| 453 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 454 | platform_set_drvdata(pdev, tmu); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 455 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 456 | res = platform_get_resource(tmu->pdev, IORESOURCE_MEM, 0); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 457 | if (!res) { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 458 | dev_err(&tmu->pdev->dev, "failed to get I/O memory\n"); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 459 | goto err0; |
| 460 | } |
| 461 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 462 | tmu->channel.irq = platform_get_irq(tmu->pdev, 0); |
| 463 | if (tmu->channel.irq < 0) { |
| 464 | dev_err(&tmu->pdev->dev, "failed to get irq\n"); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 465 | goto err0; |
| 466 | } |
| 467 | |
| 468 | /* map memory, let mapbase point to our channel */ |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 469 | tmu->mapbase = ioremap_nocache(res->start, resource_size(res)); |
| 470 | if (tmu->mapbase == NULL) { |
| 471 | dev_err(&tmu->pdev->dev, "failed to remap I/O memory\n"); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 472 | goto err0; |
| 473 | } |
| 474 | |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 475 | /* get hold of clock */ |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 476 | tmu->clk = clk_get(&tmu->pdev->dev, "tmu_fck"); |
| 477 | if (IS_ERR(tmu->clk)) { |
| 478 | dev_err(&tmu->pdev->dev, "cannot get clock\n"); |
| 479 | ret = PTR_ERR(tmu->clk); |
Magnus Damm | 03ff858 | 2010-10-13 07:36:38 +0000 | [diff] [blame] | 480 | goto err1; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 481 | } |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 482 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 483 | ret = clk_prepare(tmu->clk); |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 484 | if (ret < 0) |
| 485 | goto err2; |
| 486 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 487 | tmu->channel.cs_enabled = false; |
| 488 | tmu->channel.enable_count = 0; |
| 489 | tmu->channel.tmu = tmu; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 490 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 491 | ret = sh_tmu_register(&tmu->channel, (char *)dev_name(&tmu->pdev->dev), |
Laurent Pinchart | 394a448 | 2013-11-08 11:07:59 +0100 | [diff] [blame] | 492 | cfg->clockevent_rating, |
| 493 | cfg->clocksource_rating); |
| 494 | if (ret < 0) |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 495 | goto err3; |
Laurent Pinchart | 394a448 | 2013-11-08 11:07:59 +0100 | [diff] [blame] | 496 | |
| 497 | return 0; |
| 498 | |
Laurent Pinchart | 1c09eb3 | 2013-11-08 11:08:00 +0100 | [diff] [blame] | 499 | err3: |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 500 | clk_unprepare(tmu->clk); |
Laurent Pinchart | 394a448 | 2013-11-08 11:07:59 +0100 | [diff] [blame] | 501 | err2: |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 502 | clk_put(tmu->clk); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 503 | err1: |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 504 | iounmap(tmu->mapbase); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 505 | err0: |
| 506 | return ret; |
| 507 | } |
| 508 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 509 | static int sh_tmu_probe(struct platform_device *pdev) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 510 | { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 511 | struct sh_tmu_device *tmu = platform_get_drvdata(pdev); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 512 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 513 | int ret; |
| 514 | |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 515 | if (!is_early_platform_device(pdev)) { |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 516 | pm_runtime_set_active(&pdev->dev); |
| 517 | pm_runtime_enable(&pdev->dev); |
Rafael J. Wysocki | eaa49a8 | 2012-08-06 01:41:20 +0200 | [diff] [blame] | 518 | } |
Rafael J. Wysocki | 2ee619f | 2012-03-13 22:40:00 +0100 | [diff] [blame] | 519 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 520 | if (tmu) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 521 | dev_info(&pdev->dev, "kept as earlytimer\n"); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 522 | goto out; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 525 | tmu = kmalloc(sizeof(*tmu), GFP_KERNEL); |
| 526 | if (tmu == NULL) { |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 527 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 528 | return -ENOMEM; |
| 529 | } |
| 530 | |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 531 | ret = sh_tmu_setup(tmu, pdev); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 532 | if (ret) { |
Laurent Pinchart | 0a72aa3 | 2014-01-27 22:04:17 +0100 | [diff] [blame^] | 533 | kfree(tmu); |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 534 | pm_runtime_idle(&pdev->dev); |
| 535 | return ret; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 536 | } |
Rafael J. Wysocki | 61a53bf | 2012-08-06 01:48:17 +0200 | [diff] [blame] | 537 | if (is_early_platform_device(pdev)) |
| 538 | return 0; |
| 539 | |
| 540 | out: |
| 541 | if (cfg->clockevent_rating || cfg->clocksource_rating) |
| 542 | pm_runtime_irq_safe(&pdev->dev); |
| 543 | else |
| 544 | pm_runtime_idle(&pdev->dev); |
| 545 | |
| 546 | return 0; |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 549 | static int sh_tmu_remove(struct platform_device *pdev) |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 550 | { |
| 551 | return -EBUSY; /* cannot unregister clockevent and clocksource */ |
| 552 | } |
| 553 | |
| 554 | static struct platform_driver sh_tmu_device_driver = { |
| 555 | .probe = sh_tmu_probe, |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 556 | .remove = sh_tmu_remove, |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 557 | .driver = { |
| 558 | .name = "sh_tmu", |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | static int __init sh_tmu_init(void) |
| 563 | { |
| 564 | return platform_driver_register(&sh_tmu_device_driver); |
| 565 | } |
| 566 | |
| 567 | static void __exit sh_tmu_exit(void) |
| 568 | { |
| 569 | platform_driver_unregister(&sh_tmu_device_driver); |
| 570 | } |
| 571 | |
| 572 | early_platform_init("earlytimer", &sh_tmu_device_driver); |
Simon Horman | b9773c3 | 2013-03-05 15:40:42 +0900 | [diff] [blame] | 573 | subsys_initcall(sh_tmu_init); |
Magnus Damm | 9570ef2 | 2009-05-01 06:51:00 +0000 | [diff] [blame] | 574 | module_exit(sh_tmu_exit); |
| 575 | |
| 576 | MODULE_AUTHOR("Magnus Damm"); |
| 577 | MODULE_DESCRIPTION("SuperH TMU Timer Driver"); |
| 578 | MODULE_LICENSE("GPL v2"); |