Blame view

drivers/rtc/rtc-stm32.c 24.3 KB
248056457   Amelie Delaunay   rtc: stm32: fix c...
1
  // SPDX-License-Identifier: GPL-2.0
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
2
  /*
248056457   Amelie Delaunay   rtc: stm32: fix c...
3
4
   * Copyright (C) STMicroelectronics 2017
   * Author:  Amelie Delaunay <amelie.delaunay@st.com>
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
5
6
7
8
9
10
11
12
13
   */
  
  #include <linux/bcd.h>
  #include <linux/clk.h>
  #include <linux/iopoll.h>
  #include <linux/ioport.h>
  #include <linux/mfd/syscon.h>
  #include <linux/module.h>
  #include <linux/of_device.h>
b72252b65   Amelie Delaunay   rtc: stm32: add s...
14
  #include <linux/pm_wakeirq.h>
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
15
16
17
18
  #include <linux/regmap.h>
  #include <linux/rtc.h>
  
  #define DRIVER_NAME "stm32_rtc"
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  /* STM32_RTC_TR bit fields  */
  #define STM32_RTC_TR_SEC_SHIFT		0
  #define STM32_RTC_TR_SEC		GENMASK(6, 0)
  #define STM32_RTC_TR_MIN_SHIFT		8
  #define STM32_RTC_TR_MIN		GENMASK(14, 8)
  #define STM32_RTC_TR_HOUR_SHIFT		16
  #define STM32_RTC_TR_HOUR		GENMASK(21, 16)
  
  /* STM32_RTC_DR bit fields */
  #define STM32_RTC_DR_DATE_SHIFT		0
  #define STM32_RTC_DR_DATE		GENMASK(5, 0)
  #define STM32_RTC_DR_MONTH_SHIFT	8
  #define STM32_RTC_DR_MONTH		GENMASK(12, 8)
  #define STM32_RTC_DR_WDAY_SHIFT		13
  #define STM32_RTC_DR_WDAY		GENMASK(15, 13)
  #define STM32_RTC_DR_YEAR_SHIFT		16
  #define STM32_RTC_DR_YEAR		GENMASK(23, 16)
  
  /* STM32_RTC_CR bit fields */
  #define STM32_RTC_CR_FMT		BIT(6)
  #define STM32_RTC_CR_ALRAE		BIT(8)
  #define STM32_RTC_CR_ALRAIE		BIT(12)
b72252b65   Amelie Delaunay   rtc: stm32: add s...
41
  /* STM32_RTC_ISR/STM32_RTC_ICSR bit fields */
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #define STM32_RTC_ISR_ALRAWF		BIT(0)
  #define STM32_RTC_ISR_INITS		BIT(4)
  #define STM32_RTC_ISR_RSF		BIT(5)
  #define STM32_RTC_ISR_INITF		BIT(6)
  #define STM32_RTC_ISR_INIT		BIT(7)
  #define STM32_RTC_ISR_ALRAF		BIT(8)
  
  /* STM32_RTC_PRER bit fields */
  #define STM32_RTC_PRER_PRED_S_SHIFT	0
  #define STM32_RTC_PRER_PRED_S		GENMASK(14, 0)
  #define STM32_RTC_PRER_PRED_A_SHIFT	16
  #define STM32_RTC_PRER_PRED_A		GENMASK(22, 16)
  
  /* STM32_RTC_ALRMAR and STM32_RTC_ALRMBR bit fields */
  #define STM32_RTC_ALRMXR_SEC_SHIFT	0
  #define STM32_RTC_ALRMXR_SEC		GENMASK(6, 0)
  #define STM32_RTC_ALRMXR_SEC_MASK	BIT(7)
  #define STM32_RTC_ALRMXR_MIN_SHIFT	8
  #define STM32_RTC_ALRMXR_MIN		GENMASK(14, 8)
  #define STM32_RTC_ALRMXR_MIN_MASK	BIT(15)
  #define STM32_RTC_ALRMXR_HOUR_SHIFT	16
  #define STM32_RTC_ALRMXR_HOUR		GENMASK(21, 16)
  #define STM32_RTC_ALRMXR_PM		BIT(22)
  #define STM32_RTC_ALRMXR_HOUR_MASK	BIT(23)
  #define STM32_RTC_ALRMXR_DATE_SHIFT	24
  #define STM32_RTC_ALRMXR_DATE		GENMASK(29, 24)
  #define STM32_RTC_ALRMXR_WDSEL		BIT(30)
  #define STM32_RTC_ALRMXR_WDAY_SHIFT	24
  #define STM32_RTC_ALRMXR_WDAY		GENMASK(27, 24)
  #define STM32_RTC_ALRMXR_DATE_MASK	BIT(31)
b72252b65   Amelie Delaunay   rtc: stm32: add s...
72
73
74
75
76
77
78
79
  /* STM32_RTC_SR/_SCR bit fields */
  #define STM32_RTC_SR_ALRA		BIT(0)
  
  /* STM32_RTC_VERR bit fields */
  #define STM32_RTC_VERR_MINREV_SHIFT	0
  #define STM32_RTC_VERR_MINREV		GENMASK(3, 0)
  #define STM32_RTC_VERR_MAJREV_SHIFT	4
  #define STM32_RTC_VERR_MAJREV		GENMASK(7, 4)
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
80
81
82
83
  /* STM32_RTC_WPR key constants */
  #define RTC_WPR_1ST_KEY			0xCA
  #define RTC_WPR_2ND_KEY			0x53
  #define RTC_WPR_WRONG_KEY		0xFF
b72252b65   Amelie Delaunay   rtc: stm32: add s...
84
85
  /* Max STM32 RTC register offset is 0x3FC */
  #define UNDEF_REG			0xFFFF
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
86
87
88
  struct stm32_rtc;
  
  struct stm32_rtc_registers {
b72252b65   Amelie Delaunay   rtc: stm32: add s...
89
90
91
92
93
94
95
96
97
98
  	u16 tr;
  	u16 dr;
  	u16 cr;
  	u16 isr;
  	u16 prer;
  	u16 alrmar;
  	u16 wpr;
  	u16 sr;
  	u16 scr;
  	u16 verr;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
99
100
101
102
103
  };
  
  struct stm32_rtc_events {
  	u32 alra;
  };
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
104
  struct stm32_rtc_data {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
105
106
107
  	const struct stm32_rtc_registers regs;
  	const struct stm32_rtc_events events;
  	void (*clear_events)(struct stm32_rtc *rtc, unsigned int flags);
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
108
  	bool has_pclk;
22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
109
  	bool need_dbp;
b72252b65   Amelie Delaunay   rtc: stm32: add s...
110
  	bool has_wakeirq;
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
111
  };
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
112
113
114
115
  struct stm32_rtc {
  	struct rtc_device *rtc_dev;
  	void __iomem *base;
  	struct regmap *dbp;
22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
116
117
  	unsigned int dbp_reg;
  	unsigned int dbp_mask;
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
118
119
  	struct clk *pclk;
  	struct clk *rtc_ck;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
120
  	const struct stm32_rtc_data *data;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
121
  	int irq_alarm;
b72252b65   Amelie Delaunay   rtc: stm32: add s...
122
  	int wakeirq_alarm;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
123
124
125
126
  };
  
  static void stm32_rtc_wpr_unlock(struct stm32_rtc *rtc)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
127
128
129
130
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  
  	writel_relaxed(RTC_WPR_1ST_KEY, rtc->base + regs->wpr);
  	writel_relaxed(RTC_WPR_2ND_KEY, rtc->base + regs->wpr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
131
132
133
134
  }
  
  static void stm32_rtc_wpr_lock(struct stm32_rtc *rtc)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
135
136
137
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  
  	writel_relaxed(RTC_WPR_WRONG_KEY, rtc->base + regs->wpr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
138
139
140
141
  }
  
  static int stm32_rtc_enter_init_mode(struct stm32_rtc *rtc)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
142
143
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  	unsigned int isr = readl_relaxed(rtc->base + regs->isr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
144
145
146
  
  	if (!(isr & STM32_RTC_ISR_INITF)) {
  		isr |= STM32_RTC_ISR_INIT;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
147
  		writel_relaxed(isr, rtc->base + regs->isr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
148
149
  
  		/*
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
150
  		 * It takes around 2 rtc_ck clock cycles to enter in
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
151
  		 * initialization phase mode (and have INITF flag set). As
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
152
  		 * slowest rtc_ck frequency may be 32kHz and highest should be
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
153
154
155
  		 * 1MHz, we poll every 10 us with a timeout of 100ms.
  		 */
  		return readl_relaxed_poll_timeout_atomic(
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
156
  					rtc->base + regs->isr,
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
157
158
159
160
161
162
163
164
165
  					isr, (isr & STM32_RTC_ISR_INITF),
  					10, 100000);
  	}
  
  	return 0;
  }
  
  static void stm32_rtc_exit_init_mode(struct stm32_rtc *rtc)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
166
167
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  	unsigned int isr = readl_relaxed(rtc->base + regs->isr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
168
169
  
  	isr &= ~STM32_RTC_ISR_INIT;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
170
  	writel_relaxed(isr, rtc->base + regs->isr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
171
172
173
174
  }
  
  static int stm32_rtc_wait_sync(struct stm32_rtc *rtc)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
175
176
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  	unsigned int isr = readl_relaxed(rtc->base + regs->isr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
177
178
  
  	isr &= ~STM32_RTC_ISR_RSF;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
179
  	writel_relaxed(isr, rtc->base + regs->isr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
180
181
182
  
  	/*
  	 * Wait for RSF to be set to ensure the calendar registers are
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
183
  	 * synchronised, it takes around 2 rtc_ck clock cycles
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
184
  	 */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
185
  	return readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr,
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
186
187
188
189
  						 isr,
  						 (isr & STM32_RTC_ISR_RSF),
  						 10, 100000);
  }
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
190
191
192
193
194
  static void stm32_rtc_clear_event_flags(struct stm32_rtc *rtc,
  					unsigned int flags)
  {
  	rtc->data->clear_events(rtc, flags);
  }
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
195
196
197
  static irqreturn_t stm32_rtc_alarm_irq(int irq, void *dev_id)
  {
  	struct stm32_rtc *rtc = (struct stm32_rtc *)dev_id;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
198
199
200
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  	const struct stm32_rtc_events *evts = &rtc->data->events;
  	unsigned int status, cr;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
201
202
  
  	mutex_lock(&rtc->rtc_dev->ops_lock);
b72252b65   Amelie Delaunay   rtc: stm32: add s...
203
  	status = readl_relaxed(rtc->base + regs->sr);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
204
  	cr = readl_relaxed(rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
205

02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
206
  	if ((status & evts->alra) &&
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
207
208
209
210
211
212
213
  	    (cr & STM32_RTC_CR_ALRAIE)) {
  		/* Alarm A flag - Alarm interrupt */
  		dev_dbg(&rtc->rtc_dev->dev, "Alarm occurred
  ");
  
  		/* Pass event to the kernel */
  		rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
214
215
  		/* Clear event flags, otherwise new events won't be received */
  		stm32_rtc_clear_event_flags(rtc, evts->alra);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  	}
  
  	mutex_unlock(&rtc->rtc_dev->ops_lock);
  
  	return IRQ_HANDLED;
  }
  
  /* Convert rtc_time structure from bin to bcd format */
  static void tm2bcd(struct rtc_time *tm)
  {
  	tm->tm_sec = bin2bcd(tm->tm_sec);
  	tm->tm_min = bin2bcd(tm->tm_min);
  	tm->tm_hour = bin2bcd(tm->tm_hour);
  
  	tm->tm_mday = bin2bcd(tm->tm_mday);
  	tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  	tm->tm_year = bin2bcd(tm->tm_year - 100);
  	/*
  	 * Number of days since Sunday
  	 * - on kernel side, 0=Sunday...6=Saturday
  	 * - on rtc side, 0=invalid,1=Monday...7=Sunday
  	 */
  	tm->tm_wday = (!tm->tm_wday) ? 7 : tm->tm_wday;
  }
  
  /* Convert rtc_time structure from bcd to bin format */
  static void bcd2tm(struct rtc_time *tm)
  {
  	tm->tm_sec = bcd2bin(tm->tm_sec);
  	tm->tm_min = bcd2bin(tm->tm_min);
  	tm->tm_hour = bcd2bin(tm->tm_hour);
  
  	tm->tm_mday = bcd2bin(tm->tm_mday);
  	tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  	tm->tm_year = bcd2bin(tm->tm_year) + 100;
  	/*
  	 * Number of days since Sunday
  	 * - on kernel side, 0=Sunday...6=Saturday
  	 * - on rtc side, 0=invalid,1=Monday...7=Sunday
  	 */
  	tm->tm_wday %= 7;
  }
  
  static int stm32_rtc_read_time(struct device *dev, struct rtc_time *tm)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
262
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
263
264
265
  	unsigned int tr, dr;
  
  	/* Time and Date in BCD format */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
266
267
  	tr = readl_relaxed(rtc->base + regs->tr);
  	dr = readl_relaxed(rtc->base + regs->dr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  
  	tm->tm_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
  	tm->tm_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
  	tm->tm_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
  
  	tm->tm_mday = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
  	tm->tm_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
  	tm->tm_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
  	tm->tm_wday = (dr & STM32_RTC_DR_WDAY) >> STM32_RTC_DR_WDAY_SHIFT;
  
  	/* We don't report tm_yday and tm_isdst */
  
  	bcd2tm(tm);
  
  	return 0;
  }
  
  static int stm32_rtc_set_time(struct device *dev, struct rtc_time *tm)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
288
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  	unsigned int tr, dr;
  	int ret = 0;
  
  	tm2bcd(tm);
  
  	/* Time in BCD format */
  	tr = ((tm->tm_sec << STM32_RTC_TR_SEC_SHIFT) & STM32_RTC_TR_SEC) |
  	     ((tm->tm_min << STM32_RTC_TR_MIN_SHIFT) & STM32_RTC_TR_MIN) |
  	     ((tm->tm_hour << STM32_RTC_TR_HOUR_SHIFT) & STM32_RTC_TR_HOUR);
  
  	/* Date in BCD format */
  	dr = ((tm->tm_mday << STM32_RTC_DR_DATE_SHIFT) & STM32_RTC_DR_DATE) |
  	     ((tm->tm_mon << STM32_RTC_DR_MONTH_SHIFT) & STM32_RTC_DR_MONTH) |
  	     ((tm->tm_year << STM32_RTC_DR_YEAR_SHIFT) & STM32_RTC_DR_YEAR) |
  	     ((tm->tm_wday << STM32_RTC_DR_WDAY_SHIFT) & STM32_RTC_DR_WDAY);
  
  	stm32_rtc_wpr_unlock(rtc);
  
  	ret = stm32_rtc_enter_init_mode(rtc);
  	if (ret) {
  		dev_err(dev, "Can't enter in init mode. Set time aborted.
  ");
  		goto end;
  	}
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
313
314
  	writel_relaxed(tr, rtc->base + regs->tr);
  	writel_relaxed(dr, rtc->base + regs->dr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
315
316
317
318
319
320
321
322
323
324
325
326
327
  
  	stm32_rtc_exit_init_mode(rtc);
  
  	ret = stm32_rtc_wait_sync(rtc);
  end:
  	stm32_rtc_wpr_lock(rtc);
  
  	return ret;
  }
  
  static int stm32_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
328
329
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  	const struct stm32_rtc_events *evts = &rtc->data->events;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
330
  	struct rtc_time *tm = &alrm->time;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
331
  	unsigned int alrmar, cr, status;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
332

02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
333
334
  	alrmar = readl_relaxed(rtc->base + regs->alrmar);
  	cr = readl_relaxed(rtc->base + regs->cr);
b72252b65   Amelie Delaunay   rtc: stm32: add s...
335
  	status = readl_relaxed(rtc->base + regs->sr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  
  	if (alrmar & STM32_RTC_ALRMXR_DATE_MASK) {
  		/*
  		 * Date/day doesn't matter in Alarm comparison so alarm
  		 * triggers every day
  		 */
  		tm->tm_mday = -1;
  		tm->tm_wday = -1;
  	} else {
  		if (alrmar & STM32_RTC_ALRMXR_WDSEL) {
  			/* Alarm is set to a day of week */
  			tm->tm_mday = -1;
  			tm->tm_wday = (alrmar & STM32_RTC_ALRMXR_WDAY) >>
  				      STM32_RTC_ALRMXR_WDAY_SHIFT;
  			tm->tm_wday %= 7;
  		} else {
  			/* Alarm is set to a day of month */
  			tm->tm_wday = -1;
  			tm->tm_mday = (alrmar & STM32_RTC_ALRMXR_DATE) >>
  				       STM32_RTC_ALRMXR_DATE_SHIFT;
  		}
  	}
  
  	if (alrmar & STM32_RTC_ALRMXR_HOUR_MASK) {
  		/* Hours don't matter in Alarm comparison */
  		tm->tm_hour = -1;
  	} else {
  		tm->tm_hour = (alrmar & STM32_RTC_ALRMXR_HOUR) >>
  			       STM32_RTC_ALRMXR_HOUR_SHIFT;
  		if (alrmar & STM32_RTC_ALRMXR_PM)
  			tm->tm_hour += 12;
  	}
  
  	if (alrmar & STM32_RTC_ALRMXR_MIN_MASK) {
  		/* Minutes don't matter in Alarm comparison */
  		tm->tm_min = -1;
  	} else {
  		tm->tm_min = (alrmar & STM32_RTC_ALRMXR_MIN) >>
  			      STM32_RTC_ALRMXR_MIN_SHIFT;
  	}
  
  	if (alrmar & STM32_RTC_ALRMXR_SEC_MASK) {
  		/* Seconds don't matter in Alarm comparison */
  		tm->tm_sec = -1;
  	} else {
  		tm->tm_sec = (alrmar & STM32_RTC_ALRMXR_SEC) >>
  			      STM32_RTC_ALRMXR_SEC_SHIFT;
  	}
  
  	bcd2tm(tm);
  
  	alrm->enabled = (cr & STM32_RTC_CR_ALRAE) ? 1 : 0;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
388
  	alrm->pending = (status & evts->alra) ? 1 : 0;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
389
390
391
392
393
394
395
  
  	return 0;
  }
  
  static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
396
397
398
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  	const struct stm32_rtc_events *evts = &rtc->data->events;
  	unsigned int cr;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
399

02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
400
  	cr = readl_relaxed(rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
401
402
403
404
405
406
407
408
  
  	stm32_rtc_wpr_unlock(rtc);
  
  	/* We expose Alarm A to the kernel */
  	if (enabled)
  		cr |= (STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
  	else
  		cr &= ~(STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
409
  	writel_relaxed(cr, rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
410

02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
411
412
  	/* Clear event flags, otherwise new events won't be received */
  	stm32_rtc_clear_event_flags(rtc, evts->alra);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
413
414
415
416
417
418
419
420
  
  	stm32_rtc_wpr_lock(rtc);
  
  	return 0;
  }
  
  static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
421
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
1d70ba3bf   Amelie Delaunay   rtc: stm32: fix c...
422
  	int cur_day, cur_mon, cur_year, cur_hour, cur_min, cur_sec;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
423
424
  	unsigned int dr = readl_relaxed(rtc->base + regs->dr);
  	unsigned int tr = readl_relaxed(rtc->base + regs->tr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  
  	cur_day = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
  	cur_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
  	cur_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
  	cur_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
  	cur_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
  	cur_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
  
  	/*
  	 * Assuming current date is M-D-Y H:M:S.
  	 * RTC alarm can't be set on a specific month and year.
  	 * So the valid alarm range is:
  	 *	M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S
  	 * with a specific case for December...
  	 */
  	if ((((tm->tm_year > cur_year) &&
  	      (tm->tm_mon == 0x1) && (cur_mon == 0x12)) ||
  	     ((tm->tm_year == cur_year) &&
  	      (tm->tm_mon <= cur_mon + 1))) &&
  	    ((tm->tm_mday > cur_day) ||
  	     ((tm->tm_mday == cur_day) &&
  	     ((tm->tm_hour > cur_hour) ||
  	      ((tm->tm_hour == cur_hour) && (tm->tm_min > cur_min)) ||
  	      ((tm->tm_hour == cur_hour) && (tm->tm_min == cur_min) &&
  	       (tm->tm_sec >= cur_sec))))))
  		return 0;
  
  	return -EINVAL;
  }
  
  static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
458
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  	struct rtc_time *tm = &alrm->time;
  	unsigned int cr, isr, alrmar;
  	int ret = 0;
  
  	tm2bcd(tm);
  
  	/*
  	 * RTC alarm can't be set on a specific date, unless this date is
  	 * up to the same day of month next month.
  	 */
  	if (stm32_rtc_valid_alrm(rtc, tm) < 0) {
  		dev_err(dev, "Alarm can be set only on upcoming month.
  ");
  		return -EINVAL;
  	}
  
  	alrmar = 0;
  	/* tm_year and tm_mon are not used because not supported by RTC */
  	alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &
  		  STM32_RTC_ALRMXR_DATE;
  	/* 24-hour format */
  	alrmar &= ~STM32_RTC_ALRMXR_PM;
  	alrmar |= (tm->tm_hour << STM32_RTC_ALRMXR_HOUR_SHIFT) &
  		  STM32_RTC_ALRMXR_HOUR;
  	alrmar |= (tm->tm_min << STM32_RTC_ALRMXR_MIN_SHIFT) &
  		  STM32_RTC_ALRMXR_MIN;
  	alrmar |= (tm->tm_sec << STM32_RTC_ALRMXR_SEC_SHIFT) &
  		  STM32_RTC_ALRMXR_SEC;
  
  	stm32_rtc_wpr_unlock(rtc);
  
  	/* Disable Alarm */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
491
  	cr = readl_relaxed(rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
492
  	cr &= ~STM32_RTC_CR_ALRAE;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
493
  	writel_relaxed(cr, rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
494
495
496
  
  	/*
  	 * Poll Alarm write flag to be sure that Alarm update is allowed: it
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
497
  	 * takes around 2 rtc_ck clock cycles
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
498
  	 */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
499
  	ret = readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr,
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
500
501
502
503
504
505
506
507
508
509
510
  						isr,
  						(isr & STM32_RTC_ISR_ALRAWF),
  						10, 100000);
  
  	if (ret) {
  		dev_err(dev, "Alarm update not allowed
  ");
  		goto end;
  	}
  
  	/* Write to Alarm register */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
511
  	writel_relaxed(alrmar, rtc->base + regs->alrmar);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
512

fe63604c6   Markus Elfring   rtc: stm32: remov...
513
  	stm32_rtc_alarm_irq_enable(dev, alrm->enabled);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
514
515
516
517
518
519
520
521
522
523
524
525
526
  end:
  	stm32_rtc_wpr_lock(rtc);
  
  	return ret;
  }
  
  static const struct rtc_class_ops stm32_rtc_ops = {
  	.read_time	= stm32_rtc_read_time,
  	.set_time	= stm32_rtc_set_time,
  	.read_alarm	= stm32_rtc_read_alarm,
  	.set_alarm	= stm32_rtc_set_alarm,
  	.alarm_irq_enable = stm32_rtc_alarm_irq_enable,
  };
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
527
528
529
530
531
532
533
534
535
  static void stm32_rtc_clear_events(struct stm32_rtc *rtc,
  				   unsigned int flags)
  {
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
  
  	/* Flags are cleared by writing 0 in RTC_ISR */
  	writel_relaxed(readl_relaxed(rtc->base + regs->isr) & ~flags,
  		       rtc->base + regs->isr);
  }
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
536
537
  static const struct stm32_rtc_data stm32_rtc_data = {
  	.has_pclk = false,
22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
538
  	.need_dbp = true,
b72252b65   Amelie Delaunay   rtc: stm32: add s...
539
  	.has_wakeirq = false,
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
540
541
542
543
544
545
546
547
  	.regs = {
  		.tr = 0x00,
  		.dr = 0x04,
  		.cr = 0x08,
  		.isr = 0x0C,
  		.prer = 0x10,
  		.alrmar = 0x1C,
  		.wpr = 0x24,
b72252b65   Amelie Delaunay   rtc: stm32: add s...
548
549
550
  		.sr = 0x0C, /* set to ISR offset to ease alarm management */
  		.scr = UNDEF_REG,
  		.verr = UNDEF_REG,
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
551
552
553
554
555
  	},
  	.events = {
  		.alra = STM32_RTC_ISR_ALRAF,
  	},
  	.clear_events = stm32_rtc_clear_events,
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
556
557
558
559
  };
  
  static const struct stm32_rtc_data stm32h7_rtc_data = {
  	.has_pclk = true,
22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
560
  	.need_dbp = true,
b72252b65   Amelie Delaunay   rtc: stm32: add s...
561
  	.has_wakeirq = false,
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
562
563
564
565
566
567
568
569
  	.regs = {
  		.tr = 0x00,
  		.dr = 0x04,
  		.cr = 0x08,
  		.isr = 0x0C,
  		.prer = 0x10,
  		.alrmar = 0x1C,
  		.wpr = 0x24,
b72252b65   Amelie Delaunay   rtc: stm32: add s...
570
571
572
  		.sr = 0x0C, /* set to ISR offset to ease alarm management */
  		.scr = UNDEF_REG,
  		.verr = UNDEF_REG,
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
573
574
575
576
577
  	},
  	.events = {
  		.alra = STM32_RTC_ISR_ALRAF,
  	},
  	.clear_events = stm32_rtc_clear_events,
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
578
  };
b72252b65   Amelie Delaunay   rtc: stm32: add s...
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  static void stm32mp1_rtc_clear_events(struct stm32_rtc *rtc,
  				      unsigned int flags)
  {
  	struct stm32_rtc_registers regs = rtc->data->regs;
  
  	/* Flags are cleared by writing 1 in RTC_SCR */
  	writel_relaxed(flags, rtc->base + regs.scr);
  }
  
  static const struct stm32_rtc_data stm32mp1_data = {
  	.has_pclk = true,
  	.need_dbp = false,
  	.has_wakeirq = true,
  	.regs = {
  		.tr = 0x00,
  		.dr = 0x04,
  		.cr = 0x18,
  		.isr = 0x0C, /* named RTC_ICSR on stm32mp1 */
  		.prer = 0x10,
  		.alrmar = 0x40,
  		.wpr = 0x24,
  		.sr = 0x50,
  		.scr = 0x5C,
  		.verr = 0x3F4,
  	},
  	.events = {
  		.alra = STM32_RTC_SR_ALRA,
  	},
  	.clear_events = stm32mp1_rtc_clear_events,
  };
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
609
  static const struct of_device_id stm32_rtc_of_match[] = {
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
610
611
  	{ .compatible = "st,stm32-rtc", .data = &stm32_rtc_data },
  	{ .compatible = "st,stm32h7-rtc", .data = &stm32h7_rtc_data },
b72252b65   Amelie Delaunay   rtc: stm32: add s...
612
  	{ .compatible = "st,stm32mp1-rtc", .data = &stm32mp1_data },
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
613
614
615
  	{}
  };
  MODULE_DEVICE_TABLE(of, stm32_rtc_of_match);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
616
617
618
619
  
  static int stm32_rtc_init(struct platform_device *pdev,
  			  struct stm32_rtc *rtc)
  {
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
620
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
621
622
623
  	unsigned int prer, pred_a, pred_s, pred_a_max, pred_s_max, cr;
  	unsigned int rate;
  	int ret = 0;
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
624
  	rate = clk_get_rate(rtc->rtc_ck);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
625
626
627
628
  
  	/* Find prediv_a and prediv_s to obtain the 1Hz calendar clock */
  	pred_a_max = STM32_RTC_PRER_PRED_A >> STM32_RTC_PRER_PRED_A_SHIFT;
  	pred_s_max = STM32_RTC_PRER_PRED_S >> STM32_RTC_PRER_PRED_S_SHIFT;
1d70ba3bf   Amelie Delaunay   rtc: stm32: fix c...
629
  	for (pred_a = pred_a_max; pred_a + 1 > 0; pred_a--) {
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
630
631
632
633
634
635
636
637
638
639
640
641
642
  		pred_s = (rate / (pred_a + 1)) - 1;
  
  		if (((pred_s + 1) * (pred_a + 1)) == rate)
  			break;
  	}
  
  	/*
  	 * Can't find a 1Hz, so give priority to RTC power consumption
  	 * by choosing the higher possible value for prediv_a
  	 */
  	if ((pred_s > pred_s_max) || (pred_a > pred_a_max)) {
  		pred_a = pred_a_max;
  		pred_s = (rate / (pred_a + 1)) - 1;
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
643
644
  		dev_warn(&pdev->dev, "rtc_ck is %s
  ",
1d70ba3bf   Amelie Delaunay   rtc: stm32: fix c...
645
  			 (rate < ((pred_a + 1) * (pred_s + 1))) ?
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
646
647
648
649
650
651
652
653
654
655
656
657
658
659
  			 "fast" : "slow");
  	}
  
  	stm32_rtc_wpr_unlock(rtc);
  
  	ret = stm32_rtc_enter_init_mode(rtc);
  	if (ret) {
  		dev_err(&pdev->dev,
  			"Can't enter in init mode. Prescaler config failed.
  ");
  		goto end;
  	}
  
  	prer = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & STM32_RTC_PRER_PRED_S;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
660
  	writel_relaxed(prer, rtc->base + regs->prer);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
661
  	prer |= (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & STM32_RTC_PRER_PRED_A;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
662
  	writel_relaxed(prer, rtc->base + regs->prer);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
663
664
  
  	/* Force 24h time format */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
665
  	cr = readl_relaxed(rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
666
  	cr &= ~STM32_RTC_CR_FMT;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
667
  	writel_relaxed(cr, rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
668
669
670
671
672
673
674
675
676
677
678
679
680
  
  	stm32_rtc_exit_init_mode(rtc);
  
  	ret = stm32_rtc_wait_sync(rtc);
  end:
  	stm32_rtc_wpr_lock(rtc);
  
  	return ret;
  }
  
  static int stm32_rtc_probe(struct platform_device *pdev)
  {
  	struct stm32_rtc *rtc;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
681
  	const struct stm32_rtc_registers *regs;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
682
683
684
685
686
  	int ret;
  
  	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  	if (!rtc)
  		return -ENOMEM;
09ef18bcd   YueHaibing   rtc: use devm_pla...
687
  	rtc->base = devm_platform_ioremap_resource(pdev, 0);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
688
689
  	if (IS_ERR(rtc->base))
  		return PTR_ERR(rtc->base);
22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
690
691
  	rtc->data = (struct stm32_rtc_data *)
  		    of_device_get_match_data(&pdev->dev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
692
  	regs = &rtc->data->regs;
22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
693
694
695
696
697
698
699
700
701
  
  	if (rtc->data->need_dbp) {
  		rtc->dbp = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  							   "st,syscfg");
  		if (IS_ERR(rtc->dbp)) {
  			dev_err(&pdev->dev, "no st,syscfg
  ");
  			return PTR_ERR(rtc->dbp);
  		}
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
702

22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  		ret = of_property_read_u32_index(pdev->dev.of_node, "st,syscfg",
  						 1, &rtc->dbp_reg);
  		if (ret) {
  			dev_err(&pdev->dev, "can't read DBP register offset
  ");
  			return ret;
  		}
  
  		ret = of_property_read_u32_index(pdev->dev.of_node, "st,syscfg",
  						 2, &rtc->dbp_mask);
  		if (ret) {
  			dev_err(&pdev->dev, "can't read DBP register mask
  ");
  			return ret;
  		}
  	}
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
  
  	if (!rtc->data->has_pclk) {
  		rtc->pclk = NULL;
  		rtc->rtc_ck = devm_clk_get(&pdev->dev, NULL);
  	} else {
  		rtc->pclk = devm_clk_get(&pdev->dev, "pclk");
  		if (IS_ERR(rtc->pclk)) {
  			dev_err(&pdev->dev, "no pclk clock");
  			return PTR_ERR(rtc->pclk);
  		}
  		rtc->rtc_ck = devm_clk_get(&pdev->dev, "rtc_ck");
  	}
  	if (IS_ERR(rtc->rtc_ck)) {
  		dev_err(&pdev->dev, "no rtc_ck clock");
  		return PTR_ERR(rtc->rtc_ck);
  	}
  
  	if (rtc->data->has_pclk) {
  		ret = clk_prepare_enable(rtc->pclk);
  		if (ret)
  			return ret;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
740
  	}
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
741
  	ret = clk_prepare_enable(rtc->rtc_ck);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
742
  	if (ret)
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
743
  		goto err;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
744

22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
745
746
747
  	if (rtc->data->need_dbp)
  		regmap_update_bits(rtc->dbp, rtc->dbp_reg,
  				   rtc->dbp_mask, rtc->dbp_mask);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
748
749
750
  
  	/*
  	 * After a system reset, RTC_ISR.INITS flag can be read to check if
819cbde52   Amelie Delaunay   rtc: stm32: fix m...
751
  	 * the calendar has been initialized or not. INITS flag is reset by a
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
752
  	 * power-on reset (no vbat, no power-supply). It is not reset if
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
753
  	 * rtc_ck parent clock has changed (so RTC prescalers need to be
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
754
755
756
757
758
759
760
761
762
  	 * changed). That's why we cannot rely on this flag to know if RTC
  	 * init has to be done.
  	 */
  	ret = stm32_rtc_init(pdev, rtc);
  	if (ret)
  		goto err;
  
  	rtc->irq_alarm = platform_get_irq(pdev, 0);
  	if (rtc->irq_alarm <= 0) {
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
763
764
765
  		ret = rtc->irq_alarm;
  		goto err;
  	}
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
766
  	ret = device_init_wakeup(&pdev->dev, true);
b72252b65   Amelie Delaunay   rtc: stm32: add s...
767
768
  	if (rtc->data->has_wakeirq) {
  		rtc->wakeirq_alarm = platform_get_irq(pdev, 1);
cf612c594   Fabien Dessenne   rtc: stm32: manag...
769
  		if (rtc->wakeirq_alarm > 0) {
b72252b65   Amelie Delaunay   rtc: stm32: add s...
770
771
  			ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
  							    rtc->wakeirq_alarm);
cf612c594   Fabien Dessenne   rtc: stm32: manag...
772
773
774
775
776
  		} else {
  			ret = rtc->wakeirq_alarm;
  			if (rtc->wakeirq_alarm == -EPROBE_DEFER)
  				goto err;
  		}
b72252b65   Amelie Delaunay   rtc: stm32: add s...
777
  	}
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
778
  	if (ret)
b72252b65   Amelie Delaunay   rtc: stm32: add s...
779
780
781
  		dev_warn(&pdev->dev, "alarm can't wake up the system: %d", ret);
  
  	platform_set_drvdata(pdev, rtc);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
782
783
  
  	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
819cbde52   Amelie Delaunay   rtc: stm32: fix m...
784
  						&stm32_rtc_ops, THIS_MODULE);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
785
786
787
788
789
790
791
792
793
794
  	if (IS_ERR(rtc->rtc_dev)) {
  		ret = PTR_ERR(rtc->rtc_dev);
  		dev_err(&pdev->dev, "rtc device registration failed, err=%d
  ",
  			ret);
  		goto err;
  	}
  
  	/* Handle RTC alarm interrupts */
  	ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_alarm, NULL,
d213217d2   Amelie Delaunay   rtc: stm32: fix a...
795
  					stm32_rtc_alarm_irq, IRQF_ONESHOT,
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
796
797
798
799
800
801
802
803
804
805
806
807
  					pdev->name, rtc);
  	if (ret) {
  		dev_err(&pdev->dev, "IRQ%d (alarm interrupt) already claimed
  ",
  			rtc->irq_alarm);
  		goto err;
  	}
  
  	/*
  	 * If INITS flag is reset (calendar year field set to 0x00), calendar
  	 * must be initialized
  	 */
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
808
  	if (!(readl_relaxed(rtc->base + regs->isr) & STM32_RTC_ISR_INITS))
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
809
810
  		dev_warn(&pdev->dev, "Date/Time must be initialized
  ");
b72252b65   Amelie Delaunay   rtc: stm32: add s...
811
812
813
814
815
816
817
818
  	if (regs->verr != UNDEF_REG) {
  		u32 ver = readl_relaxed(rtc->base + regs->verr);
  
  		dev_info(&pdev->dev, "registered rev:%d.%d
  ",
  			 (ver >> STM32_RTC_VERR_MAJREV_SHIFT) & 0xF,
  			 (ver >> STM32_RTC_VERR_MINREV_SHIFT) & 0xF);
  	}
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
819
820
  	return 0;
  err:
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
821
822
823
  	if (rtc->data->has_pclk)
  		clk_disable_unprepare(rtc->pclk);
  	clk_disable_unprepare(rtc->rtc_ck);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
824

22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
825
826
  	if (rtc->data->need_dbp)
  		regmap_update_bits(rtc->dbp, rtc->dbp_reg, rtc->dbp_mask, 0);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
827

b72252b65   Amelie Delaunay   rtc: stm32: add s...
828
  	dev_pm_clear_wake_irq(&pdev->dev);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
829
830
831
832
  	device_init_wakeup(&pdev->dev, false);
  
  	return ret;
  }
0404abb22   Arnd Bergmann   rtc: stm32: remov...
833
  static int stm32_rtc_remove(struct platform_device *pdev)
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
834
835
  {
  	struct stm32_rtc *rtc = platform_get_drvdata(pdev);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
836
  	const struct stm32_rtc_registers *regs = &rtc->data->regs;
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
837
838
839
840
  	unsigned int cr;
  
  	/* Disable interrupts */
  	stm32_rtc_wpr_unlock(rtc);
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
841
  	cr = readl_relaxed(rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
842
  	cr &= ~STM32_RTC_CR_ALRAIE;
02b0cc345   Amelie Delaunay   rtc: stm32: rewor...
843
  	writel_relaxed(cr, rtc->base + regs->cr);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
844
  	stm32_rtc_wpr_lock(rtc);
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
845
846
847
  	clk_disable_unprepare(rtc->rtc_ck);
  	if (rtc->data->has_pclk)
  		clk_disable_unprepare(rtc->pclk);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
848

22cb47c1e   Amelie Delaunay   rtc: stm32: get D...
849
850
851
  	/* Enable backup domain write protection if needed */
  	if (rtc->data->need_dbp)
  		regmap_update_bits(rtc->dbp, rtc->dbp_reg, rtc->dbp_mask, 0);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
852

b72252b65   Amelie Delaunay   rtc: stm32: add s...
853
  	dev_pm_clear_wake_irq(&pdev->dev);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
854
855
856
857
858
859
860
861
862
  	device_init_wakeup(&pdev->dev, false);
  
  	return 0;
  }
  
  #ifdef CONFIG_PM_SLEEP
  static int stm32_rtc_suspend(struct device *dev)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
863
864
  	if (rtc->data->has_pclk)
  		clk_disable_unprepare(rtc->pclk);
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
865
866
867
868
869
870
871
872
873
874
  	if (device_may_wakeup(dev))
  		return enable_irq_wake(rtc->irq_alarm);
  
  	return 0;
  }
  
  static int stm32_rtc_resume(struct device *dev)
  {
  	struct stm32_rtc *rtc = dev_get_drvdata(dev);
  	int ret = 0;
9a6757ead   Amelie Delaunay   rtc: stm32: add S...
875
876
877
878
879
  	if (rtc->data->has_pclk) {
  		ret = clk_prepare_enable(rtc->pclk);
  		if (ret)
  			return ret;
  	}
4e64350f4   Amelie Delaunay   rtc: add STM32 RT...
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
  	ret = stm32_rtc_wait_sync(rtc);
  	if (ret < 0)
  		return ret;
  
  	if (device_may_wakeup(dev))
  		return disable_irq_wake(rtc->irq_alarm);
  
  	return ret;
  }
  #endif
  
  static SIMPLE_DEV_PM_OPS(stm32_rtc_pm_ops,
  			 stm32_rtc_suspend, stm32_rtc_resume);
  
  static struct platform_driver stm32_rtc_driver = {
  	.probe		= stm32_rtc_probe,
  	.remove		= stm32_rtc_remove,
  	.driver		= {
  		.name	= DRIVER_NAME,
  		.pm	= &stm32_rtc_pm_ops,
  		.of_match_table = stm32_rtc_of_match,
  	},
  };
  
  module_platform_driver(stm32_rtc_driver);
  
  MODULE_ALIAS("platform:" DRIVER_NAME);
  MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
  MODULE_DESCRIPTION("STMicroelectronics STM32 Real Time Clock driver");
  MODULE_LICENSE("GPL v2");