blob: 45af436483f39cb012d3b4d4eacd5ca7d227d7ae [file] [log] [blame]
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001/*
2 * SuperH Timer Support - CMT
3 *
4 * Copyright (C) 2008 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.
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000014 */
15
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000016#include <linux/clk.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000017#include <linux/clockchips.h>
Laurent Pincharte7a9bcc2014-02-12 16:56:44 +010018#include <linux/clocksource.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/ioport.h>
25#include <linux/irq.h>
Paul Gortmaker7deeab52011-07-03 13:36:22 -040026#include <linux/module.h>
Laurent Pinchart1768aa22014-02-12 17:12:40 +010027#include <linux/of.h>
Laurent Pincharte7a9bcc2014-02-12 16:56:44 +010028#include <linux/platform_device.h>
Rafael J. Wysocki615a4452012-03-13 22:40:06 +010029#include <linux/pm_domain.h>
Rafael J. Wysockibad81382012-08-06 01:48:57 +020030#include <linux/pm_runtime.h>
Laurent Pincharte7a9bcc2014-02-12 16:56:44 +010031#include <linux/sh_timer.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000034
Laurent Pinchart2653caf2014-01-27 22:04:17 +010035struct sh_cmt_device;
Laurent Pinchart7269f932014-01-27 15:29:19 +010036
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010037/*
38 * The CMT comes in 5 different identified flavours, depending not only on the
39 * SoC but also on the particular instance. The following table lists the main
40 * characteristics of those flavours.
41 *
Magnus Damm83c79a62017-09-18 15:46:43 +020042 * 16B 32B 32B-F 48B R-Car Gen2
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010043 * -----------------------------------------------------------------------------
44 * Channels 2 1/4 1 6 2/8
45 * Control Width 16 16 16 16 32
46 * Counter Width 16 32 32 32/48 32/48
47 * Shared Start/Stop Y Y Y Y N
48 *
Magnus Damm83c79a62017-09-18 15:46:43 +020049 * The r8a73a4 / R-Car Gen2 version has a per-channel start/stop register
50 * located in the channel registers block. All other versions have a shared
51 * start/stop register located in the global space.
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010052 *
Laurent Pinchart81b3b272014-01-28 12:36:48 +010053 * Channels are indexed from 0 to N-1 in the documentation. The channel index
54 * infers the start/stop bit position in the control register and the channel
55 * registers block address. Some CMT instances have a subset of channels
56 * available, in which case the index in the documentation doesn't match the
57 * "real" index as implemented in hardware. This is for instance the case with
58 * CMT0 on r8a7740, which is a 32-bit variant with a single channel numbered 0
59 * in the documentation but using start/stop bit 5 and having its registers
60 * block at 0x60.
61 *
62 * Similarly CMT0 on r8a73a4, r8a7790 and r8a7791, while implementing 32-bit
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010063 * channels only, is a 48-bit gen2 CMT with the 48-bit channels unavailable.
64 */
65
66enum sh_cmt_model {
67 SH_CMT_16BIT,
68 SH_CMT_32BIT,
69 SH_CMT_32BIT_FAST,
70 SH_CMT_48BIT,
Magnus Damm83c79a62017-09-18 15:46:43 +020071 SH_CMT0_RCAR_GEN2,
72 SH_CMT1_RCAR_GEN2,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010073};
74
75struct sh_cmt_info {
76 enum sh_cmt_model model;
77
Magnus Damm464eed82017-09-18 15:46:42 +020078 unsigned int channels_mask;
79
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +010080 unsigned long width; /* 16 or 32 bit version of hardware block */
81 unsigned long overflow_bit;
82 unsigned long clear_bits;
83
84 /* callbacks for CMSTR and CMCSR access */
85 unsigned long (*read_control)(void __iomem *base, unsigned long offs);
86 void (*write_control)(void __iomem *base, unsigned long offs,
87 unsigned long value);
88
89 /* callbacks for CMCNT and CMCOR access */
90 unsigned long (*read_count)(void __iomem *base, unsigned long offs);
91 void (*write_count)(void __iomem *base, unsigned long offs,
92 unsigned long value);
93};
94
Laurent Pinchart7269f932014-01-27 15:29:19 +010095struct sh_cmt_channel {
Laurent Pinchart2653caf2014-01-27 22:04:17 +010096 struct sh_cmt_device *cmt;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +000097
Laurent Pinchart81b3b272014-01-28 12:36:48 +010098 unsigned int index; /* Index in the documentation */
99 unsigned int hwidx; /* Real hardware index */
Laurent Pinchartc924d2d2014-01-27 22:04:17 +0100100
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100101 void __iomem *iostart;
102 void __iomem *ioctrl;
103
104 unsigned int timer_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000105 unsigned long flags;
106 unsigned long match_value;
107 unsigned long next_match_value;
108 unsigned long max_match_value;
Paul Mundt7d0c3992012-05-25 13:36:43 +0900109 raw_spinlock_t lock;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000110 struct clock_event_device ced;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000111 struct clocksource cs;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000112 unsigned long total_cycles;
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200113 bool cs_enabled;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100114};
115
Laurent Pinchart2653caf2014-01-27 22:04:17 +0100116struct sh_cmt_device {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100117 struct platform_device *pdev;
118
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100119 const struct sh_cmt_info *info;
120
Laurent Pinchart7269f932014-01-27 15:29:19 +0100121 void __iomem *mapbase;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100122 struct clk *clk;
Nicolai Stange890f4232017-02-06 22:11:59 +0100123 unsigned long rate;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100124
Laurent Pinchartde599c82014-02-17 16:49:05 +0100125 raw_spinlock_t lock; /* Protect the shared start/stop register */
126
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +0100127 struct sh_cmt_channel *channels;
128 unsigned int num_channels;
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100129 unsigned int hw_channels;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100130
131 bool has_clockevent;
132 bool has_clocksource;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000133};
134
Laurent Pinchartd14be992014-01-29 00:33:08 +0100135#define SH_CMT16_CMCSR_CMF (1 << 7)
136#define SH_CMT16_CMCSR_CMIE (1 << 6)
137#define SH_CMT16_CMCSR_CKS8 (0 << 0)
138#define SH_CMT16_CMCSR_CKS32 (1 << 0)
139#define SH_CMT16_CMCSR_CKS128 (2 << 0)
140#define SH_CMT16_CMCSR_CKS512 (3 << 0)
141#define SH_CMT16_CMCSR_CKS_MASK (3 << 0)
142
143#define SH_CMT32_CMCSR_CMF (1 << 15)
144#define SH_CMT32_CMCSR_OVF (1 << 14)
145#define SH_CMT32_CMCSR_WRFLG (1 << 13)
146#define SH_CMT32_CMCSR_STTF (1 << 12)
147#define SH_CMT32_CMCSR_STPF (1 << 11)
148#define SH_CMT32_CMCSR_SSIE (1 << 10)
149#define SH_CMT32_CMCSR_CMS (1 << 9)
150#define SH_CMT32_CMCSR_CMM (1 << 8)
151#define SH_CMT32_CMCSR_CMTOUT_IE (1 << 7)
152#define SH_CMT32_CMCSR_CMR_NONE (0 << 4)
153#define SH_CMT32_CMCSR_CMR_DMA (1 << 4)
154#define SH_CMT32_CMCSR_CMR_IRQ (2 << 4)
155#define SH_CMT32_CMCSR_CMR_MASK (3 << 4)
156#define SH_CMT32_CMCSR_DBGIVD (1 << 3)
157#define SH_CMT32_CMCSR_CKS_RCLK8 (4 << 0)
158#define SH_CMT32_CMCSR_CKS_RCLK32 (5 << 0)
159#define SH_CMT32_CMCSR_CKS_RCLK128 (6 << 0)
160#define SH_CMT32_CMCSR_CKS_RCLK1 (7 << 0)
161#define SH_CMT32_CMCSR_CKS_MASK (7 << 0)
162
Magnus Damma6a912c2012-12-14 14:54:19 +0900163static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
Magnus Damm587acb32012-12-14 14:54:10 +0900164{
165 return ioread16(base + (offs << 1));
166}
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000167
Magnus Damma6a912c2012-12-14 14:54:19 +0900168static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
169{
170 return ioread32(base + (offs << 2));
171}
172
173static void sh_cmt_write16(void __iomem *base, unsigned long offs,
174 unsigned long value)
Magnus Damm587acb32012-12-14 14:54:10 +0900175{
176 iowrite16(value, base + (offs << 1));
177}
178
Magnus Damma6a912c2012-12-14 14:54:19 +0900179static void sh_cmt_write32(void __iomem *base, unsigned long offs,
180 unsigned long value)
181{
182 iowrite32(value, base + (offs << 2));
183}
184
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100185static const struct sh_cmt_info sh_cmt_info[] = {
186 [SH_CMT_16BIT] = {
187 .model = SH_CMT_16BIT,
188 .width = 16,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100189 .overflow_bit = SH_CMT16_CMCSR_CMF,
190 .clear_bits = ~SH_CMT16_CMCSR_CMF,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100191 .read_control = sh_cmt_read16,
192 .write_control = sh_cmt_write16,
193 .read_count = sh_cmt_read16,
194 .write_count = sh_cmt_write16,
195 },
196 [SH_CMT_32BIT] = {
197 .model = SH_CMT_32BIT,
198 .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_read16,
202 .write_control = sh_cmt_write16,
203 .read_count = sh_cmt_read32,
204 .write_count = sh_cmt_write32,
205 },
206 [SH_CMT_32BIT_FAST] = {
207 .model = SH_CMT_32BIT_FAST,
208 .width = 32,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100209 .overflow_bit = SH_CMT32_CMCSR_CMF,
210 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100211 .read_control = sh_cmt_read16,
212 .write_control = sh_cmt_write16,
213 .read_count = sh_cmt_read32,
214 .write_count = sh_cmt_write32,
215 },
216 [SH_CMT_48BIT] = {
217 .model = SH_CMT_48BIT,
Magnus Damm464eed82017-09-18 15:46:42 +0200218 .channels_mask = 0x3f,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100219 .width = 32,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100220 .overflow_bit = SH_CMT32_CMCSR_CMF,
221 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100222 .read_control = sh_cmt_read32,
223 .write_control = sh_cmt_write32,
224 .read_count = sh_cmt_read32,
225 .write_count = sh_cmt_write32,
226 },
Magnus Damm83c79a62017-09-18 15:46:43 +0200227 [SH_CMT0_RCAR_GEN2] = {
228 .model = SH_CMT0_RCAR_GEN2,
229 .channels_mask = 0x60,
230 .width = 32,
231 .overflow_bit = SH_CMT32_CMCSR_CMF,
232 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
233 .read_control = sh_cmt_read32,
234 .write_control = sh_cmt_write32,
235 .read_count = sh_cmt_read32,
236 .write_count = sh_cmt_write32,
237 },
238 [SH_CMT1_RCAR_GEN2] = {
239 .model = SH_CMT1_RCAR_GEN2,
240 .channels_mask = 0xff,
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100241 .width = 32,
Laurent Pinchartd14be992014-01-29 00:33:08 +0100242 .overflow_bit = SH_CMT32_CMCSR_CMF,
243 .clear_bits = ~(SH_CMT32_CMCSR_CMF | SH_CMT32_CMCSR_OVF),
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100244 .read_control = sh_cmt_read32,
245 .write_control = sh_cmt_write32,
246 .read_count = sh_cmt_read32,
247 .write_count = sh_cmt_write32,
248 },
249};
250
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000251#define CMCSR 0 /* channel register */
252#define CMCNT 1 /* channel register */
253#define CMCOR 2 /* channel register */
254
Laurent Pinchart7269f932014-01-27 15:29:19 +0100255static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_channel *ch)
Magnus Damm1b56b962012-12-14 14:54:00 +0900256{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100257 if (ch->iostart)
258 return ch->cmt->info->read_control(ch->iostart, 0);
259 else
260 return ch->cmt->info->read_control(ch->cmt->mapbase, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000261}
262
Laurent Pinchart7269f932014-01-27 15:29:19 +0100263static inline void sh_cmt_write_cmstr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900264 unsigned long value)
265{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100266 if (ch->iostart)
267 ch->cmt->info->write_control(ch->iostart, 0, value);
268 else
269 ch->cmt->info->write_control(ch->cmt->mapbase, 0, value);
270}
271
272static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_channel *ch)
273{
274 return ch->cmt->info->read_control(ch->ioctrl, CMCSR);
Magnus Damm1b56b962012-12-14 14:54:00 +0900275}
276
Laurent Pinchart7269f932014-01-27 15:29:19 +0100277static inline void sh_cmt_write_cmcsr(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900278 unsigned long value)
279{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100280 ch->cmt->info->write_control(ch->ioctrl, CMCSR, value);
281}
282
283static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_channel *ch)
284{
285 return ch->cmt->info->read_count(ch->ioctrl, CMCNT);
Magnus Damm1b56b962012-12-14 14:54:00 +0900286}
287
Laurent Pinchart7269f932014-01-27 15:29:19 +0100288static inline void sh_cmt_write_cmcnt(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900289 unsigned long value)
290{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100291 ch->cmt->info->write_count(ch->ioctrl, CMCNT, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900292}
293
Laurent Pinchart7269f932014-01-27 15:29:19 +0100294static inline void sh_cmt_write_cmcor(struct sh_cmt_channel *ch,
Magnus Damm1b56b962012-12-14 14:54:00 +0900295 unsigned long value)
296{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100297 ch->cmt->info->write_count(ch->ioctrl, CMCOR, value);
Magnus Damm1b56b962012-12-14 14:54:00 +0900298}
299
Laurent Pinchart7269f932014-01-27 15:29:19 +0100300static unsigned long sh_cmt_get_counter(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000301 int *has_wrapped)
302{
303 unsigned long v1, v2, v3;
Magnus Damm5b644c72009-04-28 08:17:54 +0000304 int o1, o2;
305
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100306 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000307
308 /* Make sure the timer value is stable. Stolen from acpi_pm.c */
309 do {
Magnus Damm5b644c72009-04-28 08:17:54 +0000310 o2 = o1;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100311 v1 = sh_cmt_read_cmcnt(ch);
312 v2 = sh_cmt_read_cmcnt(ch);
313 v3 = sh_cmt_read_cmcnt(ch);
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100314 o1 = sh_cmt_read_cmcsr(ch) & ch->cmt->info->overflow_bit;
Magnus Damm5b644c72009-04-28 08:17:54 +0000315 } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
316 || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000317
Magnus Damm5b644c72009-04-28 08:17:54 +0000318 *has_wrapped = o1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000319 return v2;
320}
321
Laurent Pinchart7269f932014-01-27 15:29:19 +0100322static void sh_cmt_start_stop_ch(struct sh_cmt_channel *ch, int start)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000323{
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000324 unsigned long flags, value;
325
326 /* start stop register shared by multiple timer channels */
Laurent Pinchartde599c82014-02-17 16:49:05 +0100327 raw_spin_lock_irqsave(&ch->cmt->lock, flags);
Laurent Pinchart7269f932014-01-27 15:29:19 +0100328 value = sh_cmt_read_cmstr(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000329
330 if (start)
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100331 value |= 1 << ch->timer_bit;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000332 else
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100333 value &= ~(1 << ch->timer_bit);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000334
Laurent Pinchart7269f932014-01-27 15:29:19 +0100335 sh_cmt_write_cmstr(ch, value);
Laurent Pinchartde599c82014-02-17 16:49:05 +0100336 raw_spin_unlock_irqrestore(&ch->cmt->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000337}
338
Nicolai Stange890f4232017-02-06 22:11:59 +0100339static int sh_cmt_enable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000340{
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000341 int k, ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000342
Laurent Pinchart7269f932014-01-27 15:29:19 +0100343 pm_runtime_get_sync(&ch->cmt->pdev->dev);
344 dev_pm_syscore_device(&ch->cmt->pdev->dev, true);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200345
Paul Mundt9436b4a2011-05-31 15:26:42 +0900346 /* enable clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100347 ret = clk_enable(ch->cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000348 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100349 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot enable clock\n",
350 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000351 goto err0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000352 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000353
354 /* make sure channel is disabled */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100355 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000356
357 /* configure channel, periodic mode and maximum timeout */
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100358 if (ch->cmt->info->width == 16) {
Laurent Pinchartd14be992014-01-29 00:33:08 +0100359 sh_cmt_write_cmcsr(ch, SH_CMT16_CMCSR_CMIE |
360 SH_CMT16_CMCSR_CKS512);
Magnus Damm3014f472009-04-29 14:50:37 +0000361 } else {
Laurent Pinchartd14be992014-01-29 00:33:08 +0100362 sh_cmt_write_cmcsr(ch, SH_CMT32_CMCSR_CMM |
363 SH_CMT32_CMCSR_CMTOUT_IE |
364 SH_CMT32_CMCSR_CMR_IRQ |
365 SH_CMT32_CMCSR_CKS_RCLK8);
Magnus Damm3014f472009-04-29 14:50:37 +0000366 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000367
Laurent Pinchart7269f932014-01-27 15:29:19 +0100368 sh_cmt_write_cmcor(ch, 0xffffffff);
369 sh_cmt_write_cmcnt(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000370
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000371 /*
372 * According to the sh73a0 user's manual, as CMCNT can be operated
373 * only by the RCLK (Pseudo 32 KHz), there's one restriction on
374 * modifying CMCNT register; two RCLK cycles are necessary before
375 * this register is either read or any modification of the value
376 * it holds is reflected in the LSI's actual operation.
377 *
378 * While at it, we're supposed to clear out the CMCNT as of this
379 * moment, so make sure it's processed properly here. This will
380 * take RCLKx2 at maximum.
381 */
382 for (k = 0; k < 100; k++) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100383 if (!sh_cmt_read_cmcnt(ch))
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000384 break;
385 udelay(1);
386 }
387
Laurent Pinchart7269f932014-01-27 15:29:19 +0100388 if (sh_cmt_read_cmcnt(ch)) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100389 dev_err(&ch->cmt->pdev->dev, "ch%u: cannot clear CMCNT\n",
390 ch->index);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000391 ret = -ETIMEDOUT;
392 goto err1;
393 }
394
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000395 /* enable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100396 sh_cmt_start_stop_ch(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000397 return 0;
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000398 err1:
399 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100400 clk_disable(ch->cmt->clk);
Magnus Damm3f7e5e22011-07-13 07:59:48 +0000401
402 err0:
403 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000404}
405
Laurent Pinchart7269f932014-01-27 15:29:19 +0100406static void sh_cmt_disable(struct sh_cmt_channel *ch)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000407{
408 /* disable channel */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100409 sh_cmt_start_stop_ch(ch, 0);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000410
Magnus Dammbe890a12009-06-17 05:04:04 +0000411 /* disable interrupts in CMT block */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100412 sh_cmt_write_cmcsr(ch, 0);
Magnus Dammbe890a12009-06-17 05:04:04 +0000413
Paul Mundt9436b4a2011-05-31 15:26:42 +0900414 /* stop clock */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100415 clk_disable(ch->cmt->clk);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200416
Laurent Pinchart7269f932014-01-27 15:29:19 +0100417 dev_pm_syscore_device(&ch->cmt->pdev->dev, false);
418 pm_runtime_put(&ch->cmt->pdev->dev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000419}
420
421/* private flags */
422#define FLAG_CLOCKEVENT (1 << 0)
423#define FLAG_CLOCKSOURCE (1 << 1)
424#define FLAG_REPROGRAM (1 << 2)
425#define FLAG_SKIPEVENT (1 << 3)
426#define FLAG_IRQCONTEXT (1 << 4)
427
Laurent Pinchart7269f932014-01-27 15:29:19 +0100428static void sh_cmt_clock_event_program_verify(struct sh_cmt_channel *ch,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000429 int absolute)
430{
431 unsigned long new_match;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100432 unsigned long value = ch->next_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000433 unsigned long delay = 0;
434 unsigned long now = 0;
435 int has_wrapped;
436
Laurent Pinchart7269f932014-01-27 15:29:19 +0100437 now = sh_cmt_get_counter(ch, &has_wrapped);
438 ch->flags |= FLAG_REPROGRAM; /* force reprogram */
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000439
440 if (has_wrapped) {
441 /* we're competing with the interrupt handler.
442 * -> let the interrupt handler reprogram 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 return;
447 }
448
449 if (absolute)
450 now = 0;
451
452 do {
453 /* reprogram the timer hardware,
454 * but don't save the new match value yet.
455 */
456 new_match = now + value + delay;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100457 if (new_match > ch->max_match_value)
458 new_match = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000459
Laurent Pinchart7269f932014-01-27 15:29:19 +0100460 sh_cmt_write_cmcor(ch, new_match);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000461
Laurent Pinchart7269f932014-01-27 15:29:19 +0100462 now = sh_cmt_get_counter(ch, &has_wrapped);
463 if (has_wrapped && (new_match > ch->match_value)) {
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000464 /* we are changing to a greater match value,
465 * so this wrap must be caused by the counter
466 * matching the old value.
467 * -> first interrupt reprograms the timer.
468 * -> interrupt number two handles the event.
469 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100470 ch->flags |= FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000471 break;
472 }
473
474 if (has_wrapped) {
475 /* we are changing to a smaller match value,
476 * so the wrap must be caused by the counter
477 * matching the new value.
478 * -> save programmed match value.
479 * -> let isr handle the event.
480 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100481 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000482 break;
483 }
484
485 /* be safe: verify hardware settings */
486 if (now < new_match) {
487 /* timer value is below match value, all good.
488 * this makes sure we won't miss any match events.
489 * -> save programmed match value.
490 * -> let isr handle the event.
491 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100492 ch->match_value = new_match;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000493 break;
494 }
495
496 /* the counter has reached a value greater
497 * than our new match value. and since the
498 * has_wrapped flag isn't set we must have
499 * programmed a too close event.
500 * -> increase delay and retry.
501 */
502 if (delay)
503 delay <<= 1;
504 else
505 delay = 1;
506
507 if (!delay)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100508 dev_warn(&ch->cmt->pdev->dev, "ch%u: too long delay\n",
509 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000510
511 } while (delay);
512}
513
Laurent Pinchart7269f932014-01-27 15:29:19 +0100514static void __sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Takashi YOSHII65ada542010-12-17 07:25:09 +0000515{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100516 if (delta > ch->max_match_value)
Laurent Pinchart740a9512014-01-27 22:04:17 +0100517 dev_warn(&ch->cmt->pdev->dev, "ch%u: delta out of range\n",
518 ch->index);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000519
Laurent Pinchart7269f932014-01-27 15:29:19 +0100520 ch->next_match_value = delta;
521 sh_cmt_clock_event_program_verify(ch, 0);
Takashi YOSHII65ada542010-12-17 07:25:09 +0000522}
523
Laurent Pinchart7269f932014-01-27 15:29:19 +0100524static void sh_cmt_set_next(struct sh_cmt_channel *ch, unsigned long delta)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000525{
526 unsigned long flags;
527
Laurent Pinchart7269f932014-01-27 15:29:19 +0100528 raw_spin_lock_irqsave(&ch->lock, flags);
529 __sh_cmt_set_next(ch, delta);
530 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000531}
532
533static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
534{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100535 struct sh_cmt_channel *ch = dev_id;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000536
537 /* clear flags */
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100538 sh_cmt_write_cmcsr(ch, sh_cmt_read_cmcsr(ch) &
539 ch->cmt->info->clear_bits);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000540
541 /* update clock source counter to begin with if enabled
542 * the wrap flag should be cleared by the timer specific
543 * isr before we end up here.
544 */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100545 if (ch->flags & FLAG_CLOCKSOURCE)
546 ch->total_cycles += ch->match_value + 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000547
Laurent Pinchart7269f932014-01-27 15:29:19 +0100548 if (!(ch->flags & FLAG_REPROGRAM))
549 ch->next_match_value = ch->max_match_value;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000550
Laurent Pinchart7269f932014-01-27 15:29:19 +0100551 ch->flags |= FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000552
Laurent Pinchart7269f932014-01-27 15:29:19 +0100553 if (ch->flags & FLAG_CLOCKEVENT) {
554 if (!(ch->flags & FLAG_SKIPEVENT)) {
Viresh Kumar051b7822015-06-18 16:24:34 +0530555 if (clockevent_state_oneshot(&ch->ced)) {
Laurent Pinchart7269f932014-01-27 15:29:19 +0100556 ch->next_match_value = ch->max_match_value;
557 ch->flags |= FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000558 }
559
Laurent Pinchart7269f932014-01-27 15:29:19 +0100560 ch->ced.event_handler(&ch->ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000561 }
562 }
563
Laurent Pinchart7269f932014-01-27 15:29:19 +0100564 ch->flags &= ~FLAG_SKIPEVENT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000565
Laurent Pinchart7269f932014-01-27 15:29:19 +0100566 if (ch->flags & FLAG_REPROGRAM) {
567 ch->flags &= ~FLAG_REPROGRAM;
568 sh_cmt_clock_event_program_verify(ch, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000569
Laurent Pinchart7269f932014-01-27 15:29:19 +0100570 if (ch->flags & FLAG_CLOCKEVENT)
Viresh Kumar051b7822015-06-18 16:24:34 +0530571 if ((clockevent_state_shutdown(&ch->ced))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100572 || (ch->match_value == ch->next_match_value))
573 ch->flags &= ~FLAG_REPROGRAM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000574 }
575
Laurent Pinchart7269f932014-01-27 15:29:19 +0100576 ch->flags &= ~FLAG_IRQCONTEXT;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000577
578 return IRQ_HANDLED;
579}
580
Laurent Pinchart7269f932014-01-27 15:29:19 +0100581static int sh_cmt_start(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000582{
583 int ret = 0;
584 unsigned long flags;
585
Laurent Pinchart7269f932014-01-27 15:29:19 +0100586 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000587
Laurent Pinchart7269f932014-01-27 15:29:19 +0100588 if (!(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
Nicolai Stange890f4232017-02-06 22:11:59 +0100589 ret = sh_cmt_enable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000590
591 if (ret)
592 goto out;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100593 ch->flags |= flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000594
595 /* setup timeout if no clockevent */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100596 if ((flag == FLAG_CLOCKSOURCE) && (!(ch->flags & FLAG_CLOCKEVENT)))
597 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000598 out:
Laurent Pinchart7269f932014-01-27 15:29:19 +0100599 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000600
601 return ret;
602}
603
Laurent Pinchart7269f932014-01-27 15:29:19 +0100604static void sh_cmt_stop(struct sh_cmt_channel *ch, unsigned long flag)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000605{
606 unsigned long flags;
607 unsigned long f;
608
Laurent Pinchart7269f932014-01-27 15:29:19 +0100609 raw_spin_lock_irqsave(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000610
Laurent Pinchart7269f932014-01-27 15:29:19 +0100611 f = ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
612 ch->flags &= ~flag;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000613
Laurent Pinchart7269f932014-01-27 15:29:19 +0100614 if (f && !(ch->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
615 sh_cmt_disable(ch);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000616
617 /* adjust the timeout to maximum if only clocksource left */
Laurent Pinchart7269f932014-01-27 15:29:19 +0100618 if ((flag == FLAG_CLOCKEVENT) && (ch->flags & FLAG_CLOCKSOURCE))
619 __sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000620
Laurent Pinchart7269f932014-01-27 15:29:19 +0100621 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000622}
623
Laurent Pinchart7269f932014-01-27 15:29:19 +0100624static struct sh_cmt_channel *cs_to_sh_cmt(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000625{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100626 return container_of(cs, struct sh_cmt_channel, cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000627}
628
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100629static u64 sh_cmt_clocksource_read(struct clocksource *cs)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000630{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100631 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000632 unsigned long flags, raw;
633 unsigned long value;
634 int has_wrapped;
635
Laurent Pinchart7269f932014-01-27 15:29:19 +0100636 raw_spin_lock_irqsave(&ch->lock, flags);
637 value = ch->total_cycles;
638 raw = sh_cmt_get_counter(ch, &has_wrapped);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000639
640 if (unlikely(has_wrapped))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100641 raw += ch->match_value + 1;
642 raw_spin_unlock_irqrestore(&ch->lock, flags);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000643
644 return value + raw;
645}
646
647static int sh_cmt_clocksource_enable(struct clocksource *cs)
648{
Magnus Damm3593f5f2011-04-25 22:32:11 +0900649 int ret;
Laurent Pinchart7269f932014-01-27 15:29:19 +0100650 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000651
Laurent Pinchart7269f932014-01-27 15:29:19 +0100652 WARN_ON(ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200653
Laurent Pinchart7269f932014-01-27 15:29:19 +0100654 ch->total_cycles = 0;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000655
Laurent Pinchart7269f932014-01-27 15:29:19 +0100656 ret = sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Nicolai Stange890f4232017-02-06 22:11:59 +0100657 if (!ret)
Laurent Pinchart7269f932014-01-27 15:29:19 +0100658 ch->cs_enabled = true;
Nicolai Stange890f4232017-02-06 22:11:59 +0100659
Magnus Damm3593f5f2011-04-25 22:32:11 +0900660 return ret;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000661}
662
663static void sh_cmt_clocksource_disable(struct clocksource *cs)
664{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100665 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200666
Laurent Pinchart7269f932014-01-27 15:29:19 +0100667 WARN_ON(!ch->cs_enabled);
Rafael J. Wysockibad81382012-08-06 01:48:57 +0200668
Laurent Pinchart7269f932014-01-27 15:29:19 +0100669 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
670 ch->cs_enabled = false;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000671}
672
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200673static void sh_cmt_clocksource_suspend(struct clocksource *cs)
674{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100675 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200676
Geert Uytterhoeven54d46b72015-08-06 17:32:06 +0200677 if (!ch->cs_enabled)
678 return;
679
Laurent Pinchart7269f932014-01-27 15:29:19 +0100680 sh_cmt_stop(ch, FLAG_CLOCKSOURCE);
681 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200682}
683
Magnus Dammc8162882010-02-02 14:41:40 -0800684static void sh_cmt_clocksource_resume(struct clocksource *cs)
685{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100686 struct sh_cmt_channel *ch = cs_to_sh_cmt(cs);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200687
Geert Uytterhoeven54d46b72015-08-06 17:32:06 +0200688 if (!ch->cs_enabled)
689 return;
690
Laurent Pinchart7269f932014-01-27 15:29:19 +0100691 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
692 sh_cmt_start(ch, FLAG_CLOCKSOURCE);
Magnus Dammc8162882010-02-02 14:41:40 -0800693}
694
Laurent Pinchart7269f932014-01-27 15:29:19 +0100695static int sh_cmt_register_clocksource(struct sh_cmt_channel *ch,
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100696 const char *name)
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000697{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100698 struct clocksource *cs = &ch->cs;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000699
700 cs->name = name;
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100701 cs->rating = 125;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000702 cs->read = sh_cmt_clocksource_read;
703 cs->enable = sh_cmt_clocksource_enable;
704 cs->disable = sh_cmt_clocksource_disable;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200705 cs->suspend = sh_cmt_clocksource_suspend;
Magnus Dammc8162882010-02-02 14:41:40 -0800706 cs->resume = sh_cmt_clocksource_resume;
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000707 cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
708 cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
Paul Mundtf4d7c352010-06-02 17:10:44 +0900709
Laurent Pinchart740a9512014-01-27 22:04:17 +0100710 dev_info(&ch->cmt->pdev->dev, "ch%u: used as clock source\n",
711 ch->index);
Paul Mundtf4d7c352010-06-02 17:10:44 +0900712
Nicolai Stange890f4232017-02-06 22:11:59 +0100713 clocksource_register_hz(cs, ch->cmt->rate);
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000714 return 0;
715}
716
Laurent Pinchart7269f932014-01-27 15:29:19 +0100717static struct sh_cmt_channel *ced_to_sh_cmt(struct clock_event_device *ced)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000718{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100719 return container_of(ced, struct sh_cmt_channel, ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000720}
721
Laurent Pinchart7269f932014-01-27 15:29:19 +0100722static void sh_cmt_clock_event_start(struct sh_cmt_channel *ch, int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000723{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100724 sh_cmt_start(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000725
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000726 if (periodic)
Nicolai Stange890f4232017-02-06 22:11:59 +0100727 sh_cmt_set_next(ch, ((ch->cmt->rate + HZ/2) / HZ) - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000728 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100729 sh_cmt_set_next(ch, ch->max_match_value);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000730}
731
Viresh Kumar051b7822015-06-18 16:24:34 +0530732static int sh_cmt_clock_event_shutdown(struct clock_event_device *ced)
733{
734 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
735
736 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
737 return 0;
738}
739
740static int sh_cmt_clock_event_set_state(struct clock_event_device *ced,
741 int periodic)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000742{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100743 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000744
745 /* deal with old setting first */
Viresh Kumar051b7822015-06-18 16:24:34 +0530746 if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
Laurent Pinchart7269f932014-01-27 15:29:19 +0100747 sh_cmt_stop(ch, FLAG_CLOCKEVENT);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000748
Viresh Kumar051b7822015-06-18 16:24:34 +0530749 dev_info(&ch->cmt->pdev->dev, "ch%u: used for %s clock events\n",
750 ch->index, periodic ? "periodic" : "oneshot");
751 sh_cmt_clock_event_start(ch, periodic);
752 return 0;
753}
754
755static int sh_cmt_clock_event_set_oneshot(struct clock_event_device *ced)
756{
757 return sh_cmt_clock_event_set_state(ced, 0);
758}
759
760static int sh_cmt_clock_event_set_periodic(struct clock_event_device *ced)
761{
762 return sh_cmt_clock_event_set_state(ced, 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000763}
764
765static int sh_cmt_clock_event_next(unsigned long delta,
766 struct clock_event_device *ced)
767{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100768 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000769
Viresh Kumar051b7822015-06-18 16:24:34 +0530770 BUG_ON(!clockevent_state_oneshot(ced));
Laurent Pinchart7269f932014-01-27 15:29:19 +0100771 if (likely(ch->flags & FLAG_IRQCONTEXT))
772 ch->next_match_value = delta - 1;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000773 else
Laurent Pinchart7269f932014-01-27 15:29:19 +0100774 sh_cmt_set_next(ch, delta - 1);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000775
776 return 0;
777}
778
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200779static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
780{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100781 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900782
Laurent Pinchart7269f932014-01-27 15:29:19 +0100783 pm_genpd_syscore_poweroff(&ch->cmt->pdev->dev);
784 clk_unprepare(ch->cmt->clk);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200785}
786
787static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
788{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100789 struct sh_cmt_channel *ch = ced_to_sh_cmt(ced);
Laurent Pinchart57dee992013-12-14 15:07:32 +0900790
Laurent Pinchart7269f932014-01-27 15:29:19 +0100791 clk_prepare(ch->cmt->clk);
792 pm_genpd_syscore_poweron(&ch->cmt->pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200793}
794
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100795static int sh_cmt_register_clockevent(struct sh_cmt_channel *ch,
796 const char *name)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000797{
Laurent Pinchart7269f932014-01-27 15:29:19 +0100798 struct clock_event_device *ced = &ch->ced;
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100799 int irq;
800 int ret;
801
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100802 irq = platform_get_irq(ch->cmt->pdev, ch->index);
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100803 if (irq < 0) {
804 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to get irq\n",
805 ch->index);
806 return irq;
807 }
808
809 ret = request_irq(irq, sh_cmt_interrupt,
810 IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
811 dev_name(&ch->cmt->pdev->dev), ch);
812 if (ret) {
813 dev_err(&ch->cmt->pdev->dev, "ch%u: failed to request irq %d\n",
814 ch->index, irq);
815 return ret;
816 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000817
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000818 ced->name = name;
819 ced->features = CLOCK_EVT_FEAT_PERIODIC;
820 ced->features |= CLOCK_EVT_FEAT_ONESHOT;
Laurent Pinchartb7fcbb02014-02-19 17:00:31 +0100821 ced->rating = 125;
Laurent Pinchartf1ebe1e2014-02-19 16:19:44 +0100822 ced->cpumask = cpu_possible_mask;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000823 ced->set_next_event = sh_cmt_clock_event_next;
Viresh Kumar051b7822015-06-18 16:24:34 +0530824 ced->set_state_shutdown = sh_cmt_clock_event_shutdown;
825 ced->set_state_periodic = sh_cmt_clock_event_set_periodic;
826 ced->set_state_oneshot = sh_cmt_clock_event_set_oneshot;
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +0200827 ced->suspend = sh_cmt_clock_event_suspend;
828 ced->resume = sh_cmt_clock_event_resume;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000829
Nicolai Stange890f4232017-02-06 22:11:59 +0100830 /* TODO: calculate good shift from rate and counter bit width */
831 ced->shift = 32;
832 ced->mult = div_sc(ch->cmt->rate, NSEC_PER_SEC, ced->shift);
833 ced->max_delta_ns = clockevent_delta2ns(ch->max_match_value, ced);
Nicolai Stangebb2e94a2017-03-30 22:09:12 +0200834 ced->max_delta_ticks = ch->max_match_value;
Nicolai Stange890f4232017-02-06 22:11:59 +0100835 ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
Nicolai Stangebb2e94a2017-03-30 22:09:12 +0200836 ced->min_delta_ticks = 0x1f;
Nicolai Stange890f4232017-02-06 22:11:59 +0100837
Laurent Pinchart740a9512014-01-27 22:04:17 +0100838 dev_info(&ch->cmt->pdev->dev, "ch%u: used for clock events\n",
839 ch->index);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000840 clockevents_register_device(ced);
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100841
842 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000843}
844
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100845static int sh_cmt_register(struct sh_cmt_channel *ch, const char *name,
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100846 bool clockevent, bool clocksource)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000847{
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100848 int ret;
849
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100850 if (clockevent) {
851 ch->cmt->has_clockevent = true;
Laurent Pinchartbfa76bb2014-02-21 01:24:47 +0100852 ret = sh_cmt_register_clockevent(ch, name);
853 if (ret < 0)
854 return ret;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100855 }
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000856
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100857 if (clocksource) {
858 ch->cmt->has_clocksource = true;
Laurent Pinchartfb28a652014-02-19 17:00:31 +0100859 sh_cmt_register_clocksource(ch, name);
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100860 }
Magnus Damm19bdc9d2009-04-17 05:26:31 +0000861
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000862 return 0;
863}
864
Laurent Pinchart740a9512014-01-27 22:04:17 +0100865static int sh_cmt_setup_channel(struct sh_cmt_channel *ch, unsigned int index,
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100866 unsigned int hwidx, bool clockevent,
867 bool clocksource, struct sh_cmt_device *cmt)
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100868{
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100869 int ret;
870
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100871 /* Skip unused channels. */
872 if (!clockevent && !clocksource)
873 return 0;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100874
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100875 ch->cmt = cmt;
876 ch->index = index;
877 ch->hwidx = hwidx;
Magnus Damm83c79a62017-09-18 15:46:43 +0200878 ch->timer_bit = hwidx;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100879
880 /*
881 * Compute the address of the channel control register block. For the
882 * timers with a per-channel start/stop register, compute its address
883 * as well.
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100884 */
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100885 switch (cmt->info->model) {
886 case SH_CMT_16BIT:
887 ch->ioctrl = cmt->mapbase + 2 + ch->hwidx * 6;
888 break;
889 case SH_CMT_32BIT:
890 case SH_CMT_48BIT:
891 ch->ioctrl = cmt->mapbase + 0x10 + ch->hwidx * 0x10;
892 break;
893 case SH_CMT_32BIT_FAST:
894 /*
895 * The 32-bit "fast" timer has a single channel at hwidx 5 but
896 * is located at offset 0x40 instead of 0x60 for some reason.
897 */
898 ch->ioctrl = cmt->mapbase + 0x40;
899 break;
Magnus Damm83c79a62017-09-18 15:46:43 +0200900 case SH_CMT0_RCAR_GEN2:
901 case SH_CMT1_RCAR_GEN2:
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100902 ch->iostart = cmt->mapbase + ch->hwidx * 0x100;
903 ch->ioctrl = ch->iostart + 0x10;
Magnus Damm83c79a62017-09-18 15:46:43 +0200904 ch->timer_bit = 0;
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100905 break;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100906 }
907
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100908 if (cmt->info->width == (sizeof(ch->max_match_value) * 8))
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100909 ch->max_match_value = ~0;
910 else
Laurent Pinchart2cda3ac2014-02-11 23:46:48 +0100911 ch->max_match_value = (1 << cmt->info->width) - 1;
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100912
913 ch->match_value = ch->max_match_value;
914 raw_spin_lock_init(&ch->lock);
915
Laurent Pinchart1d053e12014-02-17 16:04:16 +0100916 ret = sh_cmt_register(ch, dev_name(&cmt->pdev->dev),
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100917 clockevent, clocksource);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100918 if (ret) {
Laurent Pinchart740a9512014-01-27 22:04:17 +0100919 dev_err(&cmt->pdev->dev, "ch%u: registration failed\n",
920 ch->index);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100921 return ret;
922 }
923 ch->cs_enabled = false;
924
Laurent Pinchartb882e7b2014-01-27 22:04:17 +0100925 return 0;
926}
927
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100928static int sh_cmt_map_memory(struct sh_cmt_device *cmt)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000929{
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100930 struct resource *mem;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000931
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100932 mem = platform_get_resource(cmt->pdev, IORESOURCE_MEM, 0);
933 if (!mem) {
934 dev_err(&cmt->pdev->dev, "failed to get I/O memory\n");
935 return -ENXIO;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +0000936 }
937
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100938 cmt->mapbase = ioremap_nocache(mem->start, resource_size(mem));
939 if (cmt->mapbase == NULL) {
940 dev_err(&cmt->pdev->dev, "failed to remap I/O memory\n");
941 return -ENXIO;
942 }
943
944 return 0;
945}
946
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100947static const struct platform_device_id sh_cmt_id_table[] = {
948 { "sh-cmt-16", (kernel_ulong_t)&sh_cmt_info[SH_CMT_16BIT] },
949 { "sh-cmt-32", (kernel_ulong_t)&sh_cmt_info[SH_CMT_32BIT] },
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100950 { }
951};
952MODULE_DEVICE_TABLE(platform, sh_cmt_id_table);
953
954static const struct of_device_id sh_cmt_of_table[] __maybe_unused = {
955 { .compatible = "renesas,cmt-32", .data = &sh_cmt_info[SH_CMT_32BIT] },
956 { .compatible = "renesas,cmt-32-fast", .data = &sh_cmt_info[SH_CMT_32BIT_FAST] },
957 { .compatible = "renesas,cmt-48", .data = &sh_cmt_info[SH_CMT_48BIT] },
Magnus Damm83c79a62017-09-18 15:46:43 +0200958 { .compatible = "renesas,cmt-48-gen2", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
959 { .compatible = "renesas,rcar-gen2-cmt0", .data = &sh_cmt_info[SH_CMT0_RCAR_GEN2] },
960 { .compatible = "renesas,rcar-gen2-cmt1", .data = &sh_cmt_info[SH_CMT1_RCAR_GEN2] },
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100961 { }
962};
963MODULE_DEVICE_TABLE(of, sh_cmt_of_table);
964
965static int sh_cmt_parse_dt(struct sh_cmt_device *cmt)
966{
967 struct device_node *np = cmt->pdev->dev.of_node;
968
969 return of_property_read_u32(np, "renesas,channels-mask",
970 &cmt->hw_channels);
971}
972
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100973static int sh_cmt_setup(struct sh_cmt_device *cmt, struct platform_device *pdev)
974{
Laurent Pinchart31e912f2014-01-28 15:52:46 +0100975 unsigned int mask;
976 unsigned int i;
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100977 int ret;
978
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100979 cmt->pdev = pdev;
Laurent Pinchartde599c82014-02-17 16:49:05 +0100980 raw_spin_lock_init(&cmt->lock);
Laurent Pinchart81b3b272014-01-28 12:36:48 +0100981
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100982 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
983 const struct of_device_id *id;
984
985 id = of_match_node(sh_cmt_of_table, pdev->dev.of_node);
986 cmt->info = id->data;
987
Magnus Damm464eed82017-09-18 15:46:42 +0200988 /* prefer in-driver channel configuration over DT */
989 if (cmt->info->channels_mask) {
990 cmt->hw_channels = cmt->info->channels_mask;
991 } else {
992 ret = sh_cmt_parse_dt(cmt);
993 if (ret < 0)
994 return ret;
995 }
Laurent Pinchart1768aa22014-02-12 17:12:40 +0100996 } else if (pdev->dev.platform_data) {
997 struct sh_timer_config *cfg = pdev->dev.platform_data;
998 const struct platform_device_id *id = pdev->id_entry;
999
1000 cmt->info = (const struct sh_cmt_info *)id->driver_data;
1001 cmt->hw_channels = cfg->channels_mask;
1002 } else {
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001003 dev_err(&cmt->pdev->dev, "missing platform data\n");
1004 return -ENXIO;
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +01001005 }
1006
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001007 /* Get hold of clock. */
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001008 cmt->clk = clk_get(&cmt->pdev->dev, "fck");
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001009 if (IS_ERR(cmt->clk)) {
1010 dev_err(&cmt->pdev->dev, "cannot get clock\n");
1011 return PTR_ERR(cmt->clk);
1012 }
1013
1014 ret = clk_prepare(cmt->clk);
Laurent Pinchartb882e7b2014-01-27 22:04:17 +01001015 if (ret < 0)
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001016 goto err_clk_put;
1017
Nicolai Stange890f4232017-02-06 22:11:59 +01001018 /* Determine clock rate. */
1019 ret = clk_enable(cmt->clk);
1020 if (ret < 0)
1021 goto err_clk_unprepare;
1022
1023 if (cmt->info->width == 16)
1024 cmt->rate = clk_get_rate(cmt->clk) / 512;
1025 else
1026 cmt->rate = clk_get_rate(cmt->clk) / 8;
1027
1028 clk_disable(cmt->clk);
1029
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001030 /* Map the memory resource(s). */
1031 ret = sh_cmt_map_memory(cmt);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001032 if (ret < 0)
1033 goto err_clk_unprepare;
1034
1035 /* Allocate and setup the channels. */
Laurent Pinchart1768aa22014-02-12 17:12:40 +01001036 cmt->num_channels = hweight8(cmt->hw_channels);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001037 cmt->channels = kzalloc(cmt->num_channels * sizeof(*cmt->channels),
1038 GFP_KERNEL);
1039 if (cmt->channels == NULL) {
1040 ret = -ENOMEM;
1041 goto err_unmap;
1042 }
1043
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001044 /*
1045 * Use the first channel as a clock event device and the second channel
1046 * as a clock source. If only one channel is available use it for both.
1047 */
Laurent Pinchart1768aa22014-02-12 17:12:40 +01001048 for (i = 0, mask = cmt->hw_channels; i < cmt->num_channels; ++i) {
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001049 unsigned int hwidx = ffs(mask) - 1;
1050 bool clocksource = i == 1 || cmt->num_channels == 1;
1051 bool clockevent = i == 0;
1052
1053 ret = sh_cmt_setup_channel(&cmt->channels[i], i, hwidx,
1054 clockevent, clocksource, cmt);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001055 if (ret < 0)
1056 goto err_unmap;
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001057
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001058 mask &= ~(1 << hwidx);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001059 }
Paul Mundtda64c2a2010-02-25 16:37:46 +09001060
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001061 platform_set_drvdata(pdev, cmt);
Magnus Dammadccc692012-12-14 14:53:51 +09001062
Paul Mundtda64c2a2010-02-25 16:37:46 +09001063 return 0;
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001064
1065err_unmap:
Laurent Pinchartf5ec9b12014-01-27 22:04:17 +01001066 kfree(cmt->channels);
Laurent Pinchart31e912f2014-01-28 15:52:46 +01001067 iounmap(cmt->mapbase);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001068err_clk_unprepare:
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001069 clk_unprepare(cmt->clk);
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001070err_clk_put:
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001071 clk_put(cmt->clk);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001072 return ret;
1073}
1074
Greg Kroah-Hartman18505142012-12-21 15:11:38 -08001075static int sh_cmt_probe(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001076{
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001077 struct sh_cmt_device *cmt = platform_get_drvdata(pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001078 int ret;
1079
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +02001080 if (!is_early_platform_device(pdev)) {
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001081 pm_runtime_set_active(&pdev->dev);
1082 pm_runtime_enable(&pdev->dev);
Rafael J. Wysocki9bb5ec82012-08-06 01:43:03 +02001083 }
Rafael J. Wysocki615a4452012-03-13 22:40:06 +01001084
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001085 if (cmt) {
Paul Mundt214a6072010-03-10 16:26:25 +09001086 dev_info(&pdev->dev, "kept as earlytimer\n");
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001087 goto out;
Magnus Damme475eed2009-04-15 10:50:04 +00001088 }
1089
Laurent Pinchartb262bc72014-01-27 22:04:17 +01001090 cmt = kzalloc(sizeof(*cmt), GFP_KERNEL);
Jingoo Han0178f412014-05-22 14:05:06 +02001091 if (cmt == NULL)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001092 return -ENOMEM;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001093
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001094 ret = sh_cmt_setup(cmt, pdev);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001095 if (ret) {
Laurent Pinchart2653caf2014-01-27 22:04:17 +01001096 kfree(cmt);
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001097 pm_runtime_idle(&pdev->dev);
1098 return ret;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001099 }
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001100 if (is_early_platform_device(pdev))
1101 return 0;
1102
1103 out:
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001104 if (cmt->has_clockevent || cmt->has_clocksource)
Rafael J. Wysockibad81382012-08-06 01:48:57 +02001105 pm_runtime_irq_safe(&pdev->dev);
1106 else
1107 pm_runtime_idle(&pdev->dev);
1108
1109 return 0;
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001110}
1111
Greg Kroah-Hartman18505142012-12-21 15:11:38 -08001112static int sh_cmt_remove(struct platform_device *pdev)
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001113{
1114 return -EBUSY; /* cannot unregister clockevent and clocksource */
1115}
1116
1117static struct platform_driver sh_cmt_device_driver = {
1118 .probe = sh_cmt_probe,
Greg Kroah-Hartman18505142012-12-21 15:11:38 -08001119 .remove = sh_cmt_remove,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001120 .driver = {
1121 .name = "sh_cmt",
Laurent Pinchart1768aa22014-02-12 17:12:40 +01001122 .of_match_table = of_match_ptr(sh_cmt_of_table),
Laurent Pinchart81b3b272014-01-28 12:36:48 +01001123 },
1124 .id_table = sh_cmt_id_table,
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001125};
1126
1127static int __init sh_cmt_init(void)
1128{
1129 return platform_driver_register(&sh_cmt_device_driver);
1130}
1131
1132static void __exit sh_cmt_exit(void)
1133{
1134 platform_driver_unregister(&sh_cmt_device_driver);
1135}
1136
Magnus Damme475eed2009-04-15 10:50:04 +00001137early_platform_init("earlytimer", &sh_cmt_device_driver);
Simon Hormane903a032013-03-05 15:40:42 +09001138subsys_initcall(sh_cmt_init);
Magnus Damm3fb1b6a2009-01-22 09:55:59 +00001139module_exit(sh_cmt_exit);
1140
1141MODULE_AUTHOR("Magnus Damm");
1142MODULE_DESCRIPTION("SuperH CMT Timer Driver");
1143MODULE_LICENSE("GPL v2");