blob: 55d3e03f2cd4fefa02ed8be70aa96c7472687eeb [file] [log] [blame]
Kuninori Morimotoefad0112018-08-22 02:25:53 +00001// SPDX-License-Identifier: GPL-2.0
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00002/*
3 * SuperH Timer Support - CMT
4 *
5 * Copyright (C) 2008 Magnus Damm
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00006 */
7
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00008#include <linux/clk.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00009#include <linux/clockchips.h>
Laurent Pincharte7a9bcc2014-02-12 16:56:44 +010010#include <linux/clocksource.h>
11#include <linux/delay.h>
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/ioport.h>
17#include <linux/irq.h>
Paul Gortmaker7deeab52011-07-03 13:36:22 -040018#include <linux/module.h>
Laurent Pinchart1768aa22014-02-12 17:12:40 +010019#include <linux/of.h>
Geert Uytterhoeven2d1d5172017-09-18 15:46:47 +020020#include <linux/of_device.h>
Laurent Pincharte7a9bcc2014-02-12 16:56:44 +010021#include <linux/platform_device.h>
Rafael J. Wysocki615a4452012-03-13 22:40:06 +010022#include <linux/pm_domain.h>
Rafael J. Wysockibad81382012-08-06 01:48:57 +020023#include <linux/pm_runtime.h>
Laurent Pincharte7a9bcc2014-02-12 16:56:44 +010024#include <linux/sh_timer.h>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000027
Laurent Pinchart2653caf2014-01-27 22:04:17 +010028struct sh_cmt_device;
Laurent Pinchart7269f932014-01-27 15:29:19 +010029
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010030/*
31 * The CMT comes in 5 different identified flavours, depending not only on the
32 * SoC but also on the particular instance. The following table lists the main
33 * characteristics of those flavours.
34 *
Magnus Damm83c79a62017-09-18 15:46:43 +020035 * 16B 32B 32B-F 48B R-Car Gen2
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010036 * -----------------------------------------------------------------------------
37 * Channels 2 1/4 1 6 2/8
38 * Control Width 16 16 16 16 32
39 * Counter Width 16 32 32 32/48 32/48
40 * Shared Start/Stop Y Y Y Y N
41 *
Magnus Damm83c79a62017-09-18 15:46:43 +020042 * The r8a73a4 / R-Car Gen2 version has a per-channel start/stop register
43 * located in the channel registers block. All other versions have a shared
44 * start/stop register located in the global space.
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010045 *
Laurent Pinchart81b3b272014-01-28 12:36:48 +010046 * Channels are indexed from 0 to N-1 in the documentation. The channel index
47 * infers the start/stop bit position in the control register and the channel
48 * registers block address. Some CMT instances have a subset of channels
49 * available, in which case the index in the documentation doesn't match the
50 * "real" index as implemented in hardware. This is for instance the case with
51 * CMT0 on r8a7740, which is a 32-bit variant with a single channel numbered 0
52 * in the documentation but using start/stop bit 5 and having its registers
53 * block at 0x60.
54 *
55 * Similarly CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010056 * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable.
57 */
58
59enum sh_cmt_model {
60 SH_CMT_16BIT,
61 SH_CMT_32BIT,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010062 SH_CMT_48BIT,
Magnus Damm83c79a62017-09-18 15:46:43 +020063 SH_CMT0_RCAR_GEN2,
64 SH_CMT1_RCAR_GEN2,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010065};
66
67struct sh_cmt_info {
68 enum sh_cmt_model model;
69
Magnus Damm464eed82017-09-18 15:46:42 +020070 unsigned int channels_mask;
71
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010072 unsigned long width; /* 16 or 32 bit version of hardware block */
Sergei Shtylyov22627c62018-09-08 23:54:05 +030073 u32 overflow_bit;
74 u32 clear_bits;
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010075
76 /* callbacks for CMSTR and CMCSR access */
Sergei Shtylyov22627c62018-09-08 23:54:05 +030077 u32 (*read_control)(void __iomem *base, unsigned long offs);
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010078 void (*write_control)(void __iomem *base, unsigned long offs,
Sergei Shtylyov22627c62018-09-08 23:54:05 +030079 u32 value);
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010080
81 /* callbacks for CMCNT and CMCOR access */
Sergei Shtylyov22627c62018-09-08 23:54:05 +030082 u32 (*read_count)(void __iomem *base, unsigned long offs);
83 void (*write_count)(void __iomem *base, unsigned long offs, u32 value);
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010084};
85
Laurent Pinchart7269f932014-01-27 15:29:19 +010086struct sh_cmt_channel {
Laurent Pinchart2653caf2014-01-27 22:04:17 +010087 struct sh_cmt_device *cmt;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000088
Laurent Pinchart81b3b272014-01-28 12:36:48 +010089 unsigned int index; /* Index in the documentation */
90 unsigned int hwidx; /* Real hardware index */
Laurent Pinchartc924d2d2014-01-27 22:04:17 +010091
Laurent Pinchart81b3b272014-01-28 12:36:48 +010092 void __iomem *iostart;
93 void __iomem *ioctrl;
94
95 unsigned int timer_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000096 unsigned long flags;
Sergei Shtylyov22627c62018-09-08 23:54:05 +030097 u32 match_value;
98 u32 next_match_value;
99 u32 max_match_value;
Paul Mundt7d0c3992012-05-25 13:36:43 +0900100 raw_spinlock_t lock;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000101 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000102 struct clocksource cs;
Sergei Shtylyov37e77422018-09-10 23:22:16 +0300103 u64 total_cycles;
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200104 bool cs_enabled;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100105};
106
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100107struct sh_cmt_device {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100108 struct platform_device *pdev;
109
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100110 const struct sh_cmt_info *info;
111
Laurent Pinchart7269f932014-01-27 15:29:19 +0100112 void __iomem *mapbase;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100113 struct clk *clk;
Nicolai Stange890f4232017-02-06 22:11:59 +0100114 unsigned long rate;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100115
Laurent Pinchartde599c82014-02-17 16:49:05 +0100116 raw_spinlock_t lock; /* Protect the shared start/stop register */
117
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100118 struct sh_cmt_channel *channels;
119 unsigned int num_channels;
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100120 unsigned int hw_channels;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100121
122 bool has_clockevent;
123 bool has_clocksource;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000124};
125
Laurent Pinchartd14be992014-01-29 00:33:08 +0100126#define SH_CMT16_CMCSR_CMF (1 << 7)
127#define SH_CMT16_CMCSR_CMIE (1 << 6)
128#define SH_CMT16_CMCSR_CKS8 (0 << 0)
129#define SH_CMT16_CMCSR_CKS32 (1 << 0)
130#define SH_CMT16_CMCSR_CKS128 (2 << 0)
131#define SH_CMT16_CMCSR_CKS512 (3 << 0)
132#define SH_CMT16_CMCSR_CKS_MASK (3 << 0)
133
134#define SH_CMT32_CMCSR_CMF (1 << 15)
135#define SH_CMT32_CMCSR_OVF (1 << 14)
136#define SH_CMT32_CMCSR_WRFLG (1 << 13)
137#define SH_CMT32_CMCSR_STTF (1 << 12)
138#define SH_CMT32_CMCSR_STPF (1 << 11)
139#define SH_CMT32_CMCSR_SSIE (1 << 10)
140#define SH_CMT32_CMCSR_CMS (1 << 9)
141#define SH_CMT32_CMCSR_CMM (1 << 8)
142#define SH_CMT32_CMCSR_CMTOUT_IE (1 << 7)
143#define SH_CMT32_CMCSR_CMR_NONE (0 << 4)
144#define SH_CMT32_CMCSR_CMR_DMA (1 << 4)
145#define SH_CMT32_CMCSR_CMR_IRQ (2 << 4)
146#define SH_CMT32_CMCSR_CMR_MASK (3 << 4)
147#define SH_CMT32_CMCSR_DBGIVD (1 << 3)
148#define SH_CMT32_CMCSR_CKS_RCLK8 (4 << 0)
149#define SH_CMT32_CMCSR_CKS_RCLK32 (5 << 0)
150#define SH_CMT32_CMCSR_CKS_RCLK128 (6 << 0)
151#define SH_CMT32_CMCSR_CKS_RCLK1 (7 << 0)
152#define SH_CMT32_CMCSR_CKS_MASK (7 << 0)
153
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300154static u32 sh_cmt_read16(void __iomem *base, unsigned long offs)
Magnus Damm587acb32012-12-14 14:54:10 +0900155{
156 return ioread16(base + (offs << 1));
157}
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000158
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300159static u32 sh_cmt_read32(void __iomem *base, unsigned long offs)
Magnus Damma6a912c2012-12-14 14:54:19 +0900160{
161 return ioread32(base + (offs << 2));
162}
163
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300164static void sh_cmt_write16(void __iomem *base, unsigned long offs, u32 value)
Magnus Damm587acb32012-12-14 14:54:10 +0900165{
166 iowrite16(value, base + (offs << 1));
167}
168
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300169static void sh_cmt_write32(void __iomem *base, unsigned long offs, u32 value)
Magnus Damma6a912c2012-12-14 14:54:19 +0900170{
171 iowrite32(value, base + (offs << 2));
172}
173
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100174static const struct sh_cmt_info sh_cmt_info[] = {
175 [SH_CMT_16BIT] = {
176 .model = SH_CMT_16BIT,
177 .width = 16,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100178 .overflow_bit = SH_CMT16_CMCSR_CMF,
179 .clear_bits = ~SH_CMT16_CMCSR_CMF,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100180 .read_control = sh_cmt_read16,
181 .write_control = sh_cmt_write16,
182 .read_count = sh_cmt_read16,
183 .write_count = sh_cmt_write16,
184 },
185 [SH_CMT_32BIT] = {
186 .model = SH_CMT_32BIT,
187 .width = 32,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100188 .overflow_bit = SH_CMT32_CMCSR_CMF,
189 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100190 .read_control = sh_cmt_read16,
191 .write_control = sh_cmt_write16,
192 .read_count = sh_cmt_read32,
193 .write_count = sh_cmt_write32,
194 },
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100195 [SH_CMT_48BIT] = {
196 .model = SH_CMT_48BIT,
Magnus Damm464eed82017-09-18 15:46:42 +0200197 .channels_mask = 0x3f,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100198 .width = 32,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100199 .overflow_bit = SH_CMT32_CMCSR_CMF,
200 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100201 .read_control = sh_cmt_read32,
202 .write_control = sh_cmt_write32,
203 .read_count = sh_cmt_read32,
204 .write_count = sh_cmt_write32,
205 },
Magnus Damm83c79a62017-09-18 15:46:43 +0200206 [SH_CMT0_RCAR_GEN2] = {
207 .model = SH_CMT0_RCAR_GEN2,
208 .channels_mask = 0x60,
209 .width = 32,
210 .overflow_bit = SH_CMT32_CMCSR_CMF,
211 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
212 .read_control = sh_cmt_read32,
213 .write_control = sh_cmt_write32,
214 .read_count = sh_cmt_read32,
215 .write_count = sh_cmt_write32,
216 },
217 [SH_CMT1_RCAR_GEN2] = {
218 .model = SH_CMT1_RCAR_GEN2,
219 .channels_mask = 0xff,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100220 .width = 32,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100221 .overflow_bit = SH_CMT32_CMCSR_CMF,
222 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100223 .read_control = sh_cmt_read32,
224 .write_control = sh_cmt_write32,
225 .read_count = sh_cmt_read32,
226 .write_count = sh_cmt_write32,
227 },
228};
229
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000230#define CMCSR 0 /* channel register */
231#define CMCNT 1 /* channel register */
232#define CMCOR 2 /* channel register */
233
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300234static inline u32 sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900235{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100236 if (ch->iostart)
237 return ch->cmt->info->read_control(ch->iostart, 0);
238 else
239 return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000240}
241
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300242static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch, u32 value)
Magnus Damm1b56b962012-12-14 14:54:00 +0900243{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100244 if (ch->iostart)
245 ch->cmt->info->write_control(ch->iostart, 0, value);
246 else
247 ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
248}
249
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300250static inline u32 sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100251{
252 return ch->cmt->info->read_control(ch->ioctrl, CMCSR);
Magnus Damm1b56b962012-12-14 14:54:00 +0900253}
254
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300255static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch, u32 value)
Magnus Damm1b56b962012-12-14 14:54:00 +0900256{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100257 ch->cmt->info->write_control(ch->ioctrl, CMCSR, value);
258}
259
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300260static inline u32 sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100261{
262 return ch->cmt->info->read_count(ch->ioctrl, CMCNT);
Magnus Damm1b56b962012-12-14 14:54:00 +0900263}
264
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300265static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch, u32 value)
Magnus Damm1b56b962012-12-14 14:54:00 +0900266{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100267 ch->cmt->info->write_count(ch->ioctrl, CMCNT, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900268}
269
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300270static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch, u32 value)
Magnus Damm1b56b962012-12-14 14:54:00 +0900271{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100272 ch->cmt->info->write_count(ch->ioctrl, CMCOR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900273}
274
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300275static u32 sh_cmt_get_counter(struct sh_cmt_channel *ch, u32 *has_wrapped)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000276{
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300277 u32 v1, v2, v3;
278 u32 o1, o2;
Magnus Damm5b644c72009-04-28 08:17:54 +0000279
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100280 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000281
282 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
283 do {
Magnus Damm5b644c72009-04-28 08:17:54 +0000284 o2 = o1;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100285 v1 = sh_cmt_read_cmcnt(ch);
286 v2 = sh_cmt_read_cmcnt(ch);
287 v3 = sh_cmt_read_cmcnt(ch);
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100288 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
Magnus Damm5b644c72009-04-28 08:17:54 +0000289 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
290 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000291
Magnus Damm5b644c72009-04-28 08:17:54 +0000292 *has_wrapped = o1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000293 return v2;
294}
295
Laurent Pinchart7269f932014-01-27 15:29:19 +0100296static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000297{
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300298 unsigned long flags;
299 u32 value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000300
301 /* start stop register shared by multiple timer channels */
Laurent Pinchartde599c82014-02-17 16:49:05 +0100302 raw_spin_lock_irqsave(&ch->cmt->lock, flags);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100303 value = sh_cmt_read_cmstr(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000304
305 if (start)
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100306 value |= 1 << ch->timer_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000307 else
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100308 value &= ~(1 << ch->timer_bit);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000309
Laurent Pinchart7269f932014-01-27 15:29:19 +0100310 sh_cmt_write_cmstr(ch, value);
Laurent Pinchartde599c82014-02-17 16:49:05 +0100311 raw_spin_unlock_irqrestore(&ch->cmt->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000312}
313
Nicolai Stange890f4232017-02-06 22:11:59 +0100314static int sh_cmt_enable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000315{
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000316 int k, ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000317
Laurent Pinchart7269f932014-01-27 15:29:19 +0100318 pm_runtime_get_sync(&ch->cmt->pdev->dev);
319 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200320
Paul Mundt9436b4a2011-05-31 15:26:42 +0900321 /* enable clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100322 ret = clk_enable(ch->cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000323 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100324 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
325 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000326 goto err0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000327 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000328
329 /* make sure channel is disabled */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100330 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000331
332 /* configure channel, periodic mode and maximum timeout */
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100333 if (ch->cmt->info->width == 16) {
Laurent Pinchartd14be992014-01-29 00:33:08 +0100334 sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
335 SH_CMT16_CMCSR_CKS512);
Magnus Damm3014f472009-04-29 14:50:37 +0000336 } else {
Laurent Pinchartd14be992014-01-29 00:33:08 +0100337 sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
338 SH_CMT32_CMCSR_CMTOUT_IE |
339 SH_CMT32_CMCSR_CMR_IRQ |
340 SH_CMT32_CMCSR_CKS_RCLK8);
Magnus Damm3014f472009-04-29 14:50:37 +0000341 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000342
Laurent Pinchart7269f932014-01-27 15:29:19 +0100343 sh_cmt_write_cmcor(ch, 0xffffffff);
344 sh_cmt_write_cmcnt(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000345
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000346 /*
347 * According to the sh73a0 user's manual, as CMCNT can be operated
348 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
349 * modifying CMCNT register; two RCLK cycles are necessary before
350 * this register is either read or any modification of the value
351 * it holds is reflected in the LSI's actual operation.
352 *
353 * While at it, we're supposed to clear out the CMCNT as of this
354 * moment, so make sure it's processed properly here. This will
355 * take RCLKx2 at maximum.
356 */
357 for (k = 0; k < 100; k++) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100358 if (!sh_cmt_read_cmcnt(ch))
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000359 break;
360 udelay(1);
361 }
362
Laurent Pinchart7269f932014-01-27 15:29:19 +0100363 if (sh_cmt_read_cmcnt(ch)) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100364 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
365 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000366 ret = -ETIMEDOUT;
367 goto err1;
368 }
369
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000370 /* enable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100371 sh_cmt_start_stop_ch(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000372 return 0;
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000373 err1:
374 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100375 clk_disable(ch->cmt->clk);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000376
377 err0:
378 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000379}
380
Laurent Pinchart7269f932014-01-27 15:29:19 +0100381static void sh_cmt_disable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000382{
383 /* disable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100384 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000385
Magnus Dammbe890a12009-06-17 05:04:04 +0000386 /* disable interrupts in CMT block */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100387 sh_cmt_write_cmcsr(ch, 0);
Magnus Dammbe890a12009-06-17 05:04:04 +0000388
Paul Mundt9436b4a2011-05-31 15:26:42 +0900389 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100390 clk_disable(ch->cmt->clk);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200391
Laurent Pinchart7269f932014-01-27 15:29:19 +0100392 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
393 pm_runtime_put(&ch->cmt->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000394}
395
396/* private flags */
397#define FLAG_CLOCKEVENT (1 << 0)
398#define FLAG_CLOCKSOURCE (1 << 1)
399#define FLAG_REPROGRAM (1 << 2)
400#define FLAG_SKIPEVENT (1 << 3)
401#define FLAG_IRQCONTEXT (1 << 4)
402
Laurent Pinchart7269f932014-01-27 15:29:19 +0100403static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000404 int absolute)
405{
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300406 u32 value = ch->next_match_value;
407 u32 new_match;
408 u32 delay = 0;
409 u32 now = 0;
410 u32 has_wrapped;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000411
Laurent Pinchart7269f932014-01-27 15:29:19 +0100412 now = sh_cmt_get_counter(ch, &has_wrapped);
413 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000414
415 if (has_wrapped) {
416 /* we're competing with the interrupt handler.
417 * -> let the interrupt handler reprogram the timer.
418 * -> interrupt number two handles the event.
419 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100420 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000421 return;
422 }
423
424 if (absolute)
425 now = 0;
426
427 do {
428 /* reprogram the timer hardware,
429 * but don't save the new match value yet.
430 */
431 new_match = now + value + delay;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100432 if (new_match > ch->max_match_value)
433 new_match = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000434
Laurent Pinchart7269f932014-01-27 15:29:19 +0100435 sh_cmt_write_cmcor(ch, new_match);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000436
Laurent Pinchart7269f932014-01-27 15:29:19 +0100437 now = sh_cmt_get_counter(ch, &has_wrapped);
438 if (has_wrapped && (new_match > ch->match_value)) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000439 /* we are changing to a greater match value,
440 * so this wrap must be caused by the counter
441 * matching the old value.
442 * -> first interrupt reprograms the timer.
443 * -> interrupt number two handles the event.
444 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100445 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000446 break;
447 }
448
449 if (has_wrapped) {
450 /* we are changing to a smaller match value,
451 * so the wrap must be caused by the counter
452 * matching the new value.
453 * -> save programmed match value.
454 * -> let isr handle the event.
455 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100456 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000457 break;
458 }
459
460 /* be safe: verify hardware settings */
461 if (now < new_match) {
462 /* timer value is below match value, all good.
463 * this makes sure we won't miss any match events.
464 * -> save programmed match value.
465 * -> let isr handle the event.
466 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100467 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000468 break;
469 }
470
471 /* the counter has reached a value greater
472 * than our new match value. and since the
473 * has_wrapped flag isn't set we must have
474 * programmed a too close event.
475 * -> increase delay and retry.
476 */
477 if (delay)
478 delay <<= 1;
479 else
480 delay = 1;
481
482 if (!delay)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100483 dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
484 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000485
486 } while (delay);
487}
488
Laurent Pinchart7269f932014-01-27 15:29:19 +0100489static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Takashi YOSHII65ada542010-12-17 07:25:09 +0000490{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100491 if (delta > ch->max_match_value)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100492 dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
493 ch->index);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000494
Laurent Pinchart7269f932014-01-27 15:29:19 +0100495 ch->next_match_value = delta;
496 sh_cmt_clock_event_program_verify(ch, 0);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000497}
498
Laurent Pinchart7269f932014-01-27 15:29:19 +0100499static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000500{
501 unsigned long flags;
502
Laurent Pinchart7269f932014-01-27 15:29:19 +0100503 raw_spin_lock_irqsave(&ch->lock, flags);
504 __sh_cmt_set_next(ch, delta);
505 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000506}
507
508static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
509{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100510 struct sh_cmt_channel *ch = dev_id;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000511
512 /* clear flags */
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100513 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
514 ch->cmt->info->clear_bits);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000515
516 /* update clock source counter to begin with if enabled
517 * the wrap flag should be cleared by the timer specific
518 * isr before we end up here.
519 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100520 if (ch->flags & FLAG_CLOCKSOURCE)
521 ch->total_cycles += ch->match_value + 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000522
Laurent Pinchart7269f932014-01-27 15:29:19 +0100523 if (!(ch->flags & FLAG_REPROGRAM))
524 ch->next_match_value = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000525
Laurent Pinchart7269f932014-01-27 15:29:19 +0100526 ch->flags |= FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000527
Laurent Pinchart7269f932014-01-27 15:29:19 +0100528 if (ch->flags & FLAG_CLOCKEVENT) {
529 if (!(ch->flags & FLAG_SKIPEVENT)) {
Viresh Kumar051b7822015-06-18 16:24:34 +0530530 if (clockevent_state_oneshot(&ch->ced)) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100531 ch->next_match_value = ch->max_match_value;
532 ch->flags |= FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000533 }
534
Laurent Pinchart7269f932014-01-27 15:29:19 +0100535 ch->ced.event_handler(&ch->ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000536 }
537 }
538
Laurent Pinchart7269f932014-01-27 15:29:19 +0100539 ch->flags &= ~FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000540
Laurent Pinchart7269f932014-01-27 15:29:19 +0100541 if (ch->flags & FLAG_REPROGRAM) {
542 ch->flags &= ~FLAG_REPROGRAM;
543 sh_cmt_clock_event_program_verify(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000544
Laurent Pinchart7269f932014-01-27 15:29:19 +0100545 if (ch->flags & FLAG_CLOCKEVENT)
Viresh Kumar051b7822015-06-18 16:24:34 +0530546 if ((clockevent_state_shutdown(&ch->ced))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100547 || (ch->match_value == ch->next_match_value))
548 ch->flags &= ~FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000549 }
550
Laurent Pinchart7269f932014-01-27 15:29:19 +0100551 ch->flags &= ~FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000552
553 return IRQ_HANDLED;
554}
555
Laurent Pinchart7269f932014-01-27 15:29:19 +0100556static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000557{
558 int ret = 0;
559 unsigned long flags;
560
Laurent Pinchart7269f932014-01-27 15:29:19 +0100561 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000562
Laurent Pinchart7269f932014-01-27 15:29:19 +0100563 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
Nicolai Stange890f4232017-02-06 22:11:59 +0100564 ret = sh_cmt_enable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000565
566 if (ret)
567 goto out;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100568 ch->flags |= flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000569
570 /* setup timeout if no clockevent */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100571 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
572 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000573 out:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100574 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000575
576 return ret;
577}
578
Laurent Pinchart7269f932014-01-27 15:29:19 +0100579static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000580{
581 unsigned long flags;
582 unsigned long f;
583
Laurent Pinchart7269f932014-01-27 15:29:19 +0100584 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000585
Laurent Pinchart7269f932014-01-27 15:29:19 +0100586 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
587 ch->flags &= ~flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000588
Laurent Pinchart7269f932014-01-27 15:29:19 +0100589 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
590 sh_cmt_disable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000591
592 /* adjust the timeout to maximum if only clocksource left */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100593 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
594 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000595
Laurent Pinchart7269f932014-01-27 15:29:19 +0100596 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000597}
598
Laurent Pinchart7269f932014-01-27 15:29:19 +0100599static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000600{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100601 return container_of(cs, struct sh_cmt_channel, cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000602}
603
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100604static u64 sh_cmt_clocksource_read(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000605{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100606 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300607 unsigned long flags;
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300608 u32 has_wrapped;
Sergei Shtylyov37e77422018-09-10 23:22:16 +0300609 u64 value;
Sergei Shtylyov22627c62018-09-08 23:54:05 +0300610 u32 raw;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000611
Laurent Pinchart7269f932014-01-27 15:29:19 +0100612 raw_spin_lock_irqsave(&ch->lock, flags);
613 value = ch->total_cycles;
614 raw = sh_cmt_get_counter(ch, &has_wrapped);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000615
616 if (unlikely(has_wrapped))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100617 raw += ch->match_value + 1;
618 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000619
620 return value + raw;
621}
622
623static int sh_cmt_clocksource_enable(struct clocksource *cs)
624{
Magnus Damm3593f5f2011-04-25 22:32:11 +0900625 int ret;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100626 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000627
Laurent Pinchart7269f932014-01-27 15:29:19 +0100628 WARN_ON(ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200629
Laurent Pinchart7269f932014-01-27 15:29:19 +0100630 ch->total_cycles = 0;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000631
Laurent Pinchart7269f932014-01-27 15:29:19 +0100632 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Nicolai Stange890f4232017-02-06 22:11:59 +0100633 if (!ret)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100634 ch->cs_enabled = true;
Nicolai Stange890f4232017-02-06 22:11:59 +0100635
Magnus Damm3593f5f2011-04-25 22:32:11 +0900636 return ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000637}
638
639static void sh_cmt_clocksource_disable(struct clocksource *cs)
640{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100641 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200642
Laurent Pinchart7269f932014-01-27 15:29:19 +0100643 WARN_ON(!ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200644
Laurent Pinchart7269f932014-01-27 15:29:19 +0100645 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
646 ch->cs_enabled = false;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000647}
648
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200649static void sh_cmt_clocksource_suspend(struct clocksource *cs)
650{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100651 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200652
Geert Uytterhoeven54d46b72015-08-06 17:32:06 +0200653 if (!ch->cs_enabled)
654 return;
655
Laurent Pinchart7269f932014-01-27 15:29:19 +0100656 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
657 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200658}
659
Magnus Dammc8162882010-02-02 14:41:40 -0800660static void sh_cmt_clocksource_resume(struct clocksource *cs)
661{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100662 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200663
Geert Uytterhoeven54d46b72015-08-06 17:32:06 +0200664 if (!ch->cs_enabled)
665 return;
666
Laurent Pinchart7269f932014-01-27 15:29:19 +0100667 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
668 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Magnus Dammc8162882010-02-02 14:41:40 -0800669}
670
Laurent Pinchart7269f932014-01-27 15:29:19 +0100671static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100672 const char *name)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000673{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100674 struct clocksource *cs = &ch->cs;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000675
676 cs->name = name;
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100677 cs->rating = 125;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000678 cs->read = sh_cmt_clocksource_read;
679 cs->enable = sh_cmt_clocksource_enable;
680 cs->disable = sh_cmt_clocksource_disable;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200681 cs->suspend = sh_cmt_clocksource_suspend;
Magnus Dammc8162882010-02-02 14:41:40 -0800682 cs->resume = sh_cmt_clocksource_resume;
Sergei Shtylyov37e77422018-09-10 23:22:16 +0300683 cs->mask = CLOCKSOURCE_MASK(sizeof(u64) * 8);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000684 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Paul Mundtf4d7c352010-06-02 17:10:44 +0900685
Laurent Pinchart740a9512014-01-27 22:04:17 +0100686 dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
687 ch->index);
Paul Mundtf4d7c352010-06-02 17:10:44 +0900688
Nicolai Stange890f4232017-02-06 22:11:59 +0100689 clocksource_register_hz(cs, ch->cmt->rate);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000690 return 0;
691}
692
Laurent Pinchart7269f932014-01-27 15:29:19 +0100693static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000694{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100695 return container_of(ced, struct sh_cmt_channel, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000696}
697
Laurent Pinchart7269f932014-01-27 15:29:19 +0100698static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000699{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100700 sh_cmt_start(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000701
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000702 if (periodic)
Nicolai Stange890f4232017-02-06 22:11:59 +0100703 sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000704 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100705 sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000706}
707
Viresh Kumar051b7822015-06-18 16:24:34 +0530708static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
709{
710 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
711
712 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
713 return 0;
714}
715
716static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
717 int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000718{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100719 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000720
721 /* deal with old setting first */
Viresh Kumar051b7822015-06-18 16:24:34 +0530722 if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100723 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000724
Viresh Kumar051b7822015-06-18 16:24:34 +0530725 dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
726 ch->index, periodic ? "periodic" : "oneshot");
727 sh_cmt_clock_event_start(ch, periodic);
728 return 0;
729}
730
731static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
732{
733 return sh_cmt_clock_event_set_state(ced, 0);
734}
735
736static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
737{
738 return sh_cmt_clock_event_set_state(ced, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000739}
740
741static int sh_cmt_clock_event_next(unsigned long delta,
742 struct clock_event_device *ced)
743{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100744 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000745
Viresh Kumar051b7822015-06-18 16:24:34 +0530746 BUG_ON(!clockevent_state_oneshot(ced));
Laurent Pinchart7269f932014-01-27 15:29:19 +0100747 if (likely(ch->flags & FLAG_IRQCONTEXT))
748 ch->next_match_value = delta - 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000749 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100750 sh_cmt_set_next(ch, delta - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000751
752 return 0;
753}
754
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200755static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
756{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100757 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900758
Laurent Pinchart7269f932014-01-27 15:29:19 +0100759 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
760 clk_unprepare(ch->cmt->clk);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200761}
762
763static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
764{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100765 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900766
Laurent Pinchart7269f932014-01-27 15:29:19 +0100767 clk_prepare(ch->cmt->clk);
768 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200769}
770
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100771static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
772 const char *name)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000773{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100774 struct clock_event_device *ced = &ch->ced;
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100775 int irq;
776 int ret;
777
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100778 irq = platform_get_irq(ch->cmt->pdev, ch->index);
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100779 if (irq < 0) {
780 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
781 ch->index);
782 return irq;
783 }
784
785 ret = request_irq(irq, sh_cmt_interrupt,
786 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
787 dev_name(&ch->cmt->pdev->dev), ch);
788 if (ret) {
789 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
790 ch->index, irq);
791 return ret;
792 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000793
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000794 ced->name = name;
795 ced->features = CLOCK_EVT_FEAT_PERIODIC;
796 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
Laurent Pinchartb7fcbb02014-02-19 17:00:31 +0100797 ced->rating = 125;
Laurent Pinchartf1ebe1e2014-02-19 16:19:44 +0100798 ced->cpumask = cpu_possible_mask;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000799 ced->set_next_event = sh_cmt_clock_event_next;
Viresh Kumar051b7822015-06-18 16:24:34 +0530800 ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
801 ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
802 ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200803 ced->suspend = sh_cmt_clock_event_suspend;
804 ced->resume = sh_cmt_clock_event_resume;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000805
Nicolai Stange890f4232017-02-06 22:11:59 +0100806 /* TODO: calculate good shift from rate and counter bit width */
807 ced->shift = 32;
808 ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
809 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
Nicolai Stangebb2e94a2017-03-30 22:09:12 +0200810 ced->max_delta_ticks = ch->max_match_value;
Nicolai Stange890f4232017-02-06 22:11:59 +0100811 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
Nicolai Stangebb2e94a2017-03-30 22:09:12 +0200812 ced->min_delta_ticks = 0x1f;
Nicolai Stange890f4232017-02-06 22:11:59 +0100813
Laurent Pinchart740a9512014-01-27 22:04:17 +0100814 dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
815 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000816 clockevents_register_device(ced);
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100817
818 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000819}
820
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100821static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100822 bool clockevent, bool clocksource)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000823{
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100824 int ret;
825
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100826 if (clockevent) {
827 ch->cmt->has_clockevent = true;
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100828 ret = sh_cmt_register_clockevent(ch, name);
829 if (ret < 0)
830 return ret;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100831 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000832
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100833 if (clocksource) {
834 ch->cmt->has_clocksource = true;
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100835 sh_cmt_register_clocksource(ch, name);
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100836 }
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000837
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000838 return 0;
839}
840
Laurent Pinchart740a9512014-01-27 22:04:17 +0100841static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100842 unsigned int hwidx, bool clockevent,
843 bool clocksource, struct sh_cmt_device *cmt)
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100844{
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100845 int ret;
846
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100847 /* Skip unused channels. */
848 if (!clockevent && !clocksource)
849 return 0;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100850
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100851 ch->cmt = cmt;
852 ch->index = index;
853 ch->hwidx = hwidx;
Magnus Damm83c79a62017-09-18 15:46:43 +0200854 ch->timer_bit = hwidx;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100855
856 /*
857 * Compute the address of the channel control register block. For the
858 * timers with a per-channel start/stop register, compute its address
859 * as well.
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100860 */
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100861 switch (cmt->info->model) {
862 case SH_CMT_16BIT:
863 ch->ioctrl = cmt->mapbase + 2 + ch->hwidx * 6;
864 break;
865 case SH_CMT_32BIT:
866 case SH_CMT_48BIT:
867 ch->ioctrl = cmt->mapbase + 0x10 + ch->hwidx * 0x10;
868 break;
Magnus Damm83c79a62017-09-18 15:46:43 +0200869 case SH_CMT0_RCAR_GEN2:
870 case SH_CMT1_RCAR_GEN2:
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100871 ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
872 ch->ioctrl = ch->iostart + 0x10;
Magnus Damm83c79a62017-09-18 15:46:43 +0200873 ch->timer_bit = 0;
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100874 break;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100875 }
876
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100877 if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100878 ch->max_match_value = ~0;
879 else
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100880 ch->max_match_value = (1 << cmt->info->width) - 1;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100881
882 ch->match_value = ch->max_match_value;
883 raw_spin_lock_init(&ch->lock);
884
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100885 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100886 clockevent, clocksource);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100887 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100888 dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
889 ch->index);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100890 return ret;
891 }
892 ch->cs_enabled = false;
893
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100894 return 0;
895}
896
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100897static int sh_cmt_map_memory(struct sh_cmt_device *cmt)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000898{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100899 struct resource *mem;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000900
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100901 mem = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
902 if (!mem) {
903 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
904 return -ENXIO;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000905 }
906
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100907 cmt->mapbase = ioremap_nocache(mem->start, resource_size(mem));
908 if (cmt->mapbase == NULL) {
909 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
910 return -ENXIO;
911 }
912
913 return 0;
914}
915
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100916static const struct platform_device_id sh_cmt_id_table[] = {
917 { "sh-cmt-16", (kernel_ulong_t)&sh_cmt_info[SH_CMT_16BIT] },
918 { "sh-cmt-32", (kernel_ulong_t)&sh_cmt_info[SH_CMT_32BIT] },
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100919 { }
920};
921MODULE_DEVICE_TABLE(platform, sh_cmt_id_table);
922
923static const struct of_device_id sh_cmt_of_table[] __maybe_unused = {
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100924 { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
Geert Uytterhoeven8d50e942017-09-18 15:46:45 +0200925 {
926 /* deprecated, preserved for backward compatibility */
927 .compatible = "renesas,cmt-48-gen2",
928 .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
929 },
Sergei Shtylyoveceb4c42018-09-12 23:14:14 +0300930 {
931 .compatible = "renesas,rcar-gen2-cmt0",
932 .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
933 },
934 {
935 .compatible = "renesas,rcar-gen2-cmt1",
936 .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2]
937 },
Sergei Shtylyovac142a72018-09-12 23:17:37 +0300938 {
939 .compatible = "renesas,rcar-gen3-cmt0",
940 .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2]
941 },
942 {
943 .compatible = "renesas,rcar-gen3-cmt1",
944 .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2]
945 },
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100946 { }
947};
948MODULE_DEVICE_TABLE(of, sh_cmt_of_table);
949
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100950static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
951{
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100952 unsigned int mask;
953 unsigned int i;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100954 int ret;
955
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100956 cmt->pdev = pdev;
Laurent Pinchartde599c82014-02-17 16:49:05 +0100957 raw_spin_lock_init(&cmt->lock);
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100958
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100959 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
Geert Uytterhoeven2d1d5172017-09-18 15:46:47 +0200960 cmt->info = of_device_get_match_data(&pdev->dev);
Geert Uytterhoevend1d28592017-09-18 15:46:46 +0200961 cmt->hw_channels = cmt->info->channels_mask;
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100962 } else if (pdev->dev.platform_data) {
963 struct sh_timer_config *cfg = pdev->dev.platform_data;
964 const struct platform_device_id *id = pdev->id_entry;
965
966 cmt->info = (const struct sh_cmt_info *)id->driver_data;
967 cmt->hw_channels = cfg->channels_mask;
968 } else {
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100969 dev_err(&cmt->pdev->dev, "missing platform data\n");
970 return -ENXIO;
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100971 }
972
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100973 /* Get hold of clock. */
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100974 cmt->clk = clk_get(&cmt->pdev->dev, "fck");
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100975 if (IS_ERR(cmt->clk)) {
976 dev_err(&cmt->pdev->dev, "cannot get clock\n");
977 return PTR_ERR(cmt->clk);
978 }
979
980 ret = clk_prepare(cmt->clk);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100981 if (ret < 0)
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100982 goto err_clk_put;
983
Nicolai Stange890f4232017-02-06 22:11:59 +0100984 /* Determine clock rate. */
985 ret = clk_enable(cmt->clk);
986 if (ret < 0)
987 goto err_clk_unprepare;
988
989 if (cmt->info->width == 16)
990 cmt->rate = clk_get_rate(cmt->clk) / 512;
991 else
992 cmt->rate = clk_get_rate(cmt->clk) / 8;
993
994 clk_disable(cmt->clk);
995
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100996 /* Map the memory resource(s). */
997 ret = sh_cmt_map_memory(cmt);
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100998 if (ret < 0)
999 goto err_clk_unprepare;
1000
1001 /* Allocate and setup the channels. */
Laurent Pinchart1768aa22014-02-12 17:12:40 +01001002 cmt->num_channels = hweight8(cmt->hw_channels);
Kees Cook6396bb22018-06-12 14:03:40 -07001003 cmt->channels = kcalloc(cmt->num_channels, sizeof(*cmt->channels),
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001004 GFP_KERNEL);
1005 if (cmt->channels == NULL) {
1006 ret = -ENOMEM;
1007 goto err_unmap;
1008 }
1009
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001010 /*
1011 * Use the first channel as a clock event device and the second channel
1012 * as a clock source. If only one channel is available use it for both.
1013 */
Laurent Pinchart1768aa22014-02-12 17:12:40 +01001014 for (i = 0, mask = cmt->hw_channels; i < cmt->num_channels; ++i) {
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001015 unsigned int hwidx = ffs(mask) - 1;
1016 bool clocksource = i == 1 || cmt->num_channels == 1;
1017 bool clockevent = i == 0;
1018
1019 ret = sh_cmt_setup_channel(&cmt->channels[i], i, hwidx,
1020 clockevent, clocksource, cmt);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001021 if (ret < 0)
1022 goto err_unmap;
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001023
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001024 mask &= ~(1 << hwidx);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001025 }
Paul Mundtda64c2a2010-02-25 16:37:46 +09001026
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001027 platform_set_drvdata(pdev, cmt);
Magnus Dammadccc692012-12-14 14:53:51 +09001028
Paul Mundtda64c2a2010-02-25 16:37:46 +09001029 return 0;
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001030
1031err_unmap:
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +01001032 kfree(cmt->channels);
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001033 iounmap(cmt->mapbase);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001034err_clk_unprepare:
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001035 clk_unprepare(cmt->clk);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001036err_clk_put:
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001037 clk_put(cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001038 return ret;
1039}
1040
Greg Kroah-Hartman18505142012-12-21 15:11:38 -08001041static int sh_cmt_probe(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001042{
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001043 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001044 int ret;
1045
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +02001046 if (!is_early_platform_device(pdev)) {
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001047 pm_runtime_set_active(&pdev->dev);
1048 pm_runtime_enable(&pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +02001049 }
Rafael J. Wysocki615a4452012-03-13 22:40:06 +01001050
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001051 if (cmt) {
Paul Mundt214a6072010-03-10 16:26:25 +09001052 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001053 goto out;
Magnus Damme475eed2009-04-15 10:50:04 +00001054 }
1055
Laurent Pinchartb262bc72014-01-27 22:04:17 +01001056 cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
Jingoo Han0178f412014-05-22 14:05:06 +02001057 if (cmt == NULL)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001058 return -ENOMEM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001059
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001060 ret = sh_cmt_setup(cmt, pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001061 if (ret) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001062 kfree(cmt);
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001063 pm_runtime_idle(&pdev->dev);
1064 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001065 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001066 if (is_early_platform_device(pdev))
1067 return 0;
1068
1069 out:
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001070 if (cmt->has_clockevent || cmt->has_clocksource)
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001071 pm_runtime_irq_safe(&pdev->dev);
1072 else
1073 pm_runtime_idle(&pdev->dev);
1074
1075 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001076}
1077
Greg Kroah-Hartman18505142012-12-21 15:11:38 -08001078static int sh_cmt_remove(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001079{
1080 return -EBUSY; /* cannot unregister clockevent and clocksource */
1081}
1082
1083static struct platform_driver sh_cmt_device_driver = {
1084 .probe = sh_cmt_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -08001085 .remove = sh_cmt_remove,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001086 .driver = {
1087 .name = "sh_cmt",
Laurent Pinchart1768aa22014-02-12 17:12:40 +01001088 .of_match_table = of_match_ptr(sh_cmt_of_table),
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001089 },
1090 .id_table = sh_cmt_id_table,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001091};
1092
1093static int __init sh_cmt_init(void)
1094{
1095 return platform_driver_register(&sh_cmt_device_driver);
1096}
1097
1098static void __exit sh_cmt_exit(void)
1099{
1100 platform_driver_unregister(&sh_cmt_device_driver);
1101}
1102
Magnus Damme475eed2009-04-15 10:50:04 +00001103early_platform_init("earlytimer", &sh_cmt_device_driver);
Simon Hormane903a032013-03-05 15:40:42 +09001104subsys_initcall(sh_cmt_init);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001105module_exit(sh_cmt_exit);
1106
1107MODULE_AUTHOR("Magnus Damm");
1108MODULE_DESCRIPTION("SuperH CMT Timer Driver");
1109MODULE_LICENSE("GPL v2");