• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * An RTC driver for Sunxi Platform of Allwinner SoC
3  *
4  * Copyright (c) 2020, Martin <wuyan@allwinnertech.com>
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, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  */
17 //#define DEBUG	/* Enable dev_dbg */
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_wakeirq.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/types.h>
31 #include <linux/notifier.h>
32 #include <linux/reboot.h>
33 #include "rtc-sunxi.h"
34 
35 #define LOSC_CTRL_REG			0x00
36 #define RTC_DAY_ACCESS			BIT(7)  /* 1: the DAY setting operation is in progress */
37 #define RTC_HHMMSS_ACCESS		BIT(8)  /* 1: the HH-MM-SS setting operation is in progress */
38 
39 #define LOSC_AUTO_SWT_STA_REG		0x0004
40 #define LOSC_STATUS			BIT(0)
41 
42 #define RTC_DAY_REG			0x0010
43 #define RTC_HHMMSS_REG			0x0014
44 
45 #define ALARM0_DAY_REG			0x0020
46 #define ALARM0_HHMMSS_REG		0x0024
47 
48 #define ALARM0_ENABLE_REG		0x0028
49 #define ALARM0_ENABLE			BIT(0)
50 
51 #define ALARM0_IRQ_EN_REG		0x002c
52 #define ALARM0_IRQ_EN			BIT(0)
53 
54 #define ALARM0_IRQ_STA_REG		0x0030
55 #define ALARM0_IRQ_PEND			BIT(0)
56 
57 #define ALARM0_CONFIG_REG		0x0050
58 #define ALARM0_OUTPUT_EN		BIT(0)
59 
60 #define GET_VAL_FROM_REG(reg, mask, shift)	(((reg) & ((mask) << (shift))) >> (shift))
61 #define PUT_VAL_IN_REG(val, mask, shift)	(((val) & (mask)) << (shift))
62 
63 #define GET_DAY_FROM_REG(x)		GET_VAL_FROM_REG(x, 0x1f, 0)
64 #define GET_MON_FROM_REG(x)		GET_VAL_FROM_REG(x, 0x0f, 8)
65 #define GET_YEAR_FROM_REG(x, d)		GET_VAL_FROM_REG(x, (d)->year_mask, (d)->year_shift)
66 
67 #define GET_SEC_FROM_REG(x)		GET_VAL_FROM_REG(x, 0x3f, 0)
68 #define GET_MIN_FROM_REG(x)		GET_VAL_FROM_REG(x, 0x3f, 8)
69 #define GET_HOUR_FROM_REG(x)		GET_VAL_FROM_REG(x, 0x1f, 16)
70 
71 #define PUT_DAY_IN_REG(x)		PUT_VAL_IN_REG(x, 0x1f, 0)
72 #define PUT_MON_IN_REG(x)		PUT_VAL_IN_REG(x, 0x0f, 8)
73 #define PUT_YEAR_IN_REG(x, d)		PUT_VAL_IN_REG(x, (d)->year_mask, (d)->year_shift)
74 #define PUT_LEAP_IN_REG(is_leap, shift)	PUT_VAL_IN_REG(is_leap, 0x01, shift)
75 
76 #define PUT_SEC_IN_REG(x)		PUT_VAL_IN_REG(x, 0x3f, 0)
77 #define PUT_MIN_IN_REG(x)		PUT_VAL_IN_REG(x, 0x3f, 8)
78 #define PUT_HOUR_IN_REG(x)		PUT_VAL_IN_REG(x, 0x1f, 16)
79 
80 #define SEC_IN_MIN			(60)
81 #define SEC_IN_HOUR			(60 * SEC_IN_MIN)
82 #define SEC_IN_DAY			(24 * SEC_IN_HOUR)
83 #define XO_CTRL_REG			0x160
84 #define XO_CTRL_MASK			0x0F000000
85 #define XO_ICTRL_SHIFT			24
86 
87 /*
88  * NOTE: To know the valid range of each member in 'struct rtc_time', see 'struct tm' in include/linux/time.h
89  *       See also rtc_valid_tm() in drivers/rtc/lib.c.
90  */
91 
92 /*
93  * @tm_year:   the year in kernel. See '(struct rtc_time).tm_year' and '(struct tm).tm_year',
94  *	       offset to 1900. @tm_year=0 means year 1900
95  * @real_year: the year in human world.
96  *	       for example 1900
97  * @hw_year:   the year in hardware register, offset to @hw_year_base.
98  *	       @hw_year=0 means year @hw_year_base
99  * @hw_year_base: the base of @hw_year, for example 1970.
100  *		  let's say @hw_year=30 && @hw_year_base=1970, then @real_year should be 2000.
101  */
102 #define TM_YEAR_BASE					(1900)
103 #define TM_YEAR_TO_HW_YEAR(tm_year, hw_year_base)	((tm_year) + TM_YEAR_BASE - (hw_year_base))
104 #define HW_YEAR_TO_TM_YEAR(hw_year, hw_year_base)	((hw_year) + (hw_year_base) - TM_YEAR_BASE)
105 #define TM_YEAR_TO_REAL_YEAR(tm_year)			((tm_year) + TM_YEAR_BASE)
106 #define REAL_YEAR_TO_TM_YEAR(real_year)			((real_year) - TM_YEAR_BASE)
107 #define HW_YEAR_TO_REAL_YEAR(hw_year, hw_year_base)	((hw_year) + (hw_year_base))
108 #define REAL_YEAR_TO_HW_YEAR(real_year, hw_year_base)	((real_year) - (hw_year_base))
109 
110 /*
111  * @tm_mon:   the month in kernel. See '(struct rtc_time).tm_mon' and '(struct tm).tm_mon', In the range 0 to 11
112  * @real_mon: the month in human world, in the range 1 to 12
113  * @hw_mon:   the month in hardware register, in the range 1 to 12
114  */
115 #define TM_MON_TO_REAL_MON(tm_mon)	((tm_mon) + 1)
116 #define REAL_MON_TO_TM_MON(real_mon)	((real_mon) - 1)
117 #define TM_MON_TO_HW_MON(tm_mon)	((tm_mon) + 1)
118 #define HW_MON_TO_TM_MON(hw_mon)	((hw_mon) - 1)
119 #define REAL_MON_TO_HW_MON(real_mon)	(real_mon)
120 #define HW_MON_TO_REAL_MON(hw_mon)	(hw_mon)
121 
122 /*
123  * @hw_day:   the day in hardware register, in the range HW_DAY_MIN to HW_DAY_MAX.
124  *	      let's say @hw_year_base=1970, then @hw_day=HW_DAY_MIN means 1970-01-01.
125  * @real_day: the day in human world. starts from 1 (REAL_DAY_MIN).
126  *	      It's the number of day since the first day of @hw_year_base.
127  *	      let's say @hw_year_base=1970, then @real_day=1 means 1970-01-01
128  */
129 #define HW_DAY_MIN			(1)
130 /*
131  * AW1855 RTC SPEC said the DAY register starts from 0, but actually it's 1.
132  * The fact is, RTC_DAY_REG starts from 1, but ALARM0_DAY_REG starts from 0.
133  * See errata__fix_alarm_day_reg_default_value() for more information.
134  */
135 #define HW_DAY_MAX			(65535)
136 #define REAL_DAY_MIN			(1)
137 #define HW_DAY_TO_REAL_DAY(hw_day)	((hw_day) + (REAL_DAY_MIN - HW_DAY_MIN))
138 #define REAL_DAY_TO_HW_DAY(real_day)	((real_day) - (REAL_DAY_MIN - HW_DAY_MIN))
139 #define HW_YEAR_MAX(min_year)		((min_year) + (HW_DAY_MAX - HW_DAY_MIN + 1) / 366 - 1)
140 /* '366' and '-1' make the result smaller, so that the time to be set won't exceed the hardware capability */
141 
rtc_reg_write(struct sunxi_rtc_dev * chip,u32 reg,u32 val)142 static inline void rtc_reg_write(struct sunxi_rtc_dev *chip, u32 reg, u32 val)
143 {
144 	writel(val, chip->base + reg);
145 }
rtc_reg_read(struct sunxi_rtc_dev * chip,u32 reg)146 static inline u32 rtc_reg_read(struct sunxi_rtc_dev *chip, u32 reg)
147 {
148 	return readl(chip->base + reg);
149 }
150 
151 static short month_days[2][13] = {
152 	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
153 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},  /* Leap year */
154 };
155 
156 /*
157  * Convert real @day based on @year_base-01-01 to real @yyyy+@mm+@dd.
158  * @day: The number of day from @year_base-01-01. The minimum value is REAL_DAY_MIN.
159  *       For example, if @year_base=1970, then @day=REAL_DAY_MIN means 1970-01-01.
160  *       Note that 0 is invalid.
161  * @year_base: the base year of @day, for example 1970.
162  * @yyyy: real year (output), for example 2000
163  * @mm: real month (output), 1 ~ 12
164  * @dd: real date (output),  1 ~ 31
165  */
real_day_to_real_ymd(u32 day,u32 year_base,u32 * yyyy,u32 * mm,u32 * dd)166 static int real_day_to_real_ymd(u32 day, u32 year_base, u32 *yyyy, u32 *mm, u32 *dd)
167 {
168 	u32 y, m, d, leap;
169 
170 	if (day < REAL_DAY_MIN) {
171 		printk("real_day_to_real_ymd(): Invalid argument\n");
172 		*yyyy = *mm = *dd = 0;
173 		return -EINVAL;
174 	}
175 
176 	/* Calc year */
177 	for (y = year_base; day > (is_leap_year(y) ? 366 : 365); y++)
178 		day -= (is_leap_year(y) ? 366 : 365);
179 
180 	/* Calc month */
181 	leap = is_leap_year(y);
182 	for (m = 1; day > month_days[leap][m]; m++)
183 		day -= month_days[leap][m];
184 
185 	/* Calc date */
186 	d = day;
187 
188 	/* Return result */
189 	*yyyy = y;
190 	*mm = m;
191 	*dd = d;
192 
193 	return 0;
194 }
195 
196 /*
197  * Convert real @yyyy+@mm+@dd to @day based on @year_base-01-01.
198  * Arguments are the same as real_day_to_real_ymd()
199  */
real_ymd_to_real_day(u32 yyyy,u32 mm,u32 dd,u32 year_base,u32 * day)200 static int real_ymd_to_real_day(u32 yyyy, u32 mm, u32 dd, u32 year_base, u32 *day)
201 {
202 	u32 leap = is_leap_year(yyyy);
203 	u32 i;
204 
205 	*day = 0;  /* 0 is invalid */
206 
207 	if (yyyy < year_base || mm < 1 || mm > 12 || dd < 1 || dd > month_days[leap][mm]) {
208 		printk("real_ymd_to_real_day(): Invalid argument\n");
209 		return -EINVAL;
210 	}
211 
212 	for (i = year_base; i < yyyy; i++)
213 		*day += is_leap_year(i) ? 366 : 365;
214 	for (i = 1; i < mm; i++)
215 		*day += month_days[leap][i];
216 	*day += dd;
217 
218 	return 0;
219 }
220 
221 /* Convert register value @hw_day and @hw_hhmmss to @tm */
hw_day_hhmmss_to_tm(const struct sunxi_rtc_data * data,u32 hw_day,u32 hw_hhmmss,struct rtc_time * tm)222 static inline int hw_day_hhmmss_to_tm(const struct sunxi_rtc_data *data,
223 				u32 hw_day, u32 hw_hhmmss, struct rtc_time *tm)
224 {
225 	int err;
226 	u32 real_year, real_mon, real_mday;
227 	u32 real_day = HW_DAY_TO_REAL_DAY(hw_day);
228 
229 	err = real_day_to_real_ymd(real_day, data->min_year, &real_year, &real_mon, &real_mday);
230 	if (err)
231 		return err;
232 
233 	tm->tm_year = REAL_YEAR_TO_TM_YEAR(real_year);
234 	tm->tm_mon  = REAL_MON_TO_TM_MON(real_mon);
235 	tm->tm_mday = real_mday;
236 	tm->tm_hour = GET_HOUR_FROM_REG(hw_hhmmss);
237 	tm->tm_min  = GET_MIN_FROM_REG(hw_hhmmss);
238 	tm->tm_sec  = GET_SEC_FROM_REG(hw_hhmmss);
239 
240 	return 0;
241 }
242 
243 /* Convert @tm to register value @hw_day and @hw_hhmmss */
tm_to_hw_day_hhmmss(const struct sunxi_rtc_data * data,const struct rtc_time * tm,u32 * hw_day,u32 * hw_hhmmss)244 static inline int tm_to_hw_day_hhmmss(const struct sunxi_rtc_data *data,
245 			const struct rtc_time *tm, u32 *hw_day, u32 *hw_hhmmss)
246 {
247 	int err;
248 	u32 real_year, real_mon, real_mday;
249 	u32 real_day;
250 
251 	real_year = TM_YEAR_TO_REAL_YEAR(tm->tm_year);
252 	real_mon = TM_MON_TO_REAL_MON(tm->tm_mon);
253 	real_mday = tm->tm_mday;
254 	err = real_ymd_to_real_day(real_year, real_mon, real_mday, data->min_year, &real_day);
255 	if (err)
256 		return err;
257 	*hw_day = REAL_DAY_TO_HW_DAY(real_day);
258 	*hw_hhmmss = PUT_HOUR_IN_REG(tm->tm_hour) |
259 		     PUT_MIN_IN_REG(tm->tm_min) |
260 		     PUT_SEC_IN_REG(tm->tm_sec);
261 	return 0;
262 }
263 
264 /* Convert register value @hw_yymmdd and @hw_hhmmss to @tm */
hw_yymmdd_hhmmss_to_tm(const struct sunxi_rtc_data * data,u32 hw_yymmdd,u32 hw_hhmmss,struct rtc_time * tm)265 static __maybe_unused inline void hw_yymmdd_hhmmss_to_tm(const struct sunxi_rtc_data *data,
266 				u32 hw_yymmdd, u32 hw_hhmmss, struct rtc_time *tm)
267 {
268 	u32 hw_year = GET_YEAR_FROM_REG(hw_yymmdd, data);
269 	u32 hw_mon  = GET_MON_FROM_REG(hw_yymmdd);
270 
271 	tm->tm_year = HW_YEAR_TO_TM_YEAR(hw_year, data->min_year);
272 	tm->tm_mon  = HW_MON_TO_TM_MON(hw_mon);
273 	tm->tm_mday = GET_DAY_FROM_REG(hw_yymmdd);
274 	tm->tm_hour = GET_HOUR_FROM_REG(hw_hhmmss);
275 	tm->tm_min  = GET_MIN_FROM_REG(hw_hhmmss);
276 	tm->tm_sec  = GET_SEC_FROM_REG(hw_hhmmss);
277 }
278 
279 /* Convert @tm to register value @hw_yymmdd and @hw_hhmmss */
tm_to_hw_yymmdd_hhmmss(const struct sunxi_rtc_data * data,const struct rtc_time * tm,u32 * hw_yymmdd,u32 * hw_hhmmss)280 static inline void tm_to_hw_yymmdd_hhmmss(const struct sunxi_rtc_data *data,
281 			const struct rtc_time *tm, u32 *hw_yymmdd, u32 *hw_hhmmss)
282 {
283 	u32 hw_year = TM_YEAR_TO_HW_YEAR(tm->tm_year, data->min_year);
284 	u32 hw_mon  = TM_MON_TO_HW_MON(tm->tm_mon);
285 	u32 real_year = TM_YEAR_TO_REAL_YEAR(tm->tm_year);
286 
287 	*hw_yymmdd = PUT_LEAP_IN_REG(is_leap_year(real_year), data->leap_shift) |
288 		     PUT_YEAR_IN_REG(hw_year, data) |
289 		     PUT_MON_IN_REG(hw_mon) |
290 		     PUT_DAY_IN_REG(tm->tm_mday);
291 	*hw_hhmmss = PUT_HOUR_IN_REG(tm->tm_hour) |
292 		     PUT_MIN_IN_REG(tm->tm_min) |
293 		     PUT_SEC_IN_REG(tm->tm_sec);
294 }
295 
296 /* Wait until the @bits in @reg are cleared, or else return -ETIMEDOUT */
wait_bits_cleared(struct sunxi_rtc_dev * chip,int reg,unsigned int bits,unsigned int ms_timeout)297 static int wait_bits_cleared(struct sunxi_rtc_dev *chip, int reg,
298 			  unsigned int bits, unsigned int ms_timeout)
299 {
300 	const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
301 	u32 val;
302 
303 	do {
304 		val = rtc_reg_read(chip, reg);
305 		if ((val & bits) == 0) {
306 			/*
307 			 * For sunxi chips ahead of sun8iw16p1's platform, we must wait at least 2ms
308 			 * after the access bits of LOSC_CTRL_REG's HMS/YMD have been cleared.
309 			 */
310 			if ((reg == LOSC_CTRL_REG) &&
311 			    ((bits == RTC_HHMMSS_ACCESS) || (bits == RTC_DAY_ACCESS)))
312 				msleep(2);
313 			return 0;
314 		}
315 	} while (time_before(jiffies, timeout));
316 
317 	return -ETIMEDOUT;
318 }
319 
show_tm(struct device * dev,struct rtc_time * tm)320 static __maybe_unused void show_tm(struct device *dev, struct rtc_time *tm)
321 {
322 	dev_info(dev, "%04d-%02d-%02d %02d:%02d:%02d\n",
323 		TM_YEAR_TO_REAL_YEAR(tm->tm_year),
324 		TM_MON_TO_REAL_MON(tm->tm_mon), tm->tm_mday,
325 		tm->tm_hour, tm->tm_min, tm->tm_sec);
326 }
327 
sunxi_rtc_gettime(struct device * dev,struct rtc_time * tm)328 static int sunxi_rtc_gettime(struct device *dev, struct rtc_time *tm)
329 {
330 	struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
331 	u32 hw_day, hw_hhmmss;
332 	u32 val;
333 	int err;
334 
335 	val = rtc_reg_read(chip, LOSC_AUTO_SWT_STA_REG);
336 	if ((val & LOSC_STATUS) == 0)
337 		dev_warn(dev, "Warning: Using internal RC 16M clock source. Time may be inaccurate!\n");
338 
339 	do {  /* read again in case it changes */
340 		hw_day = rtc_reg_read(chip, RTC_DAY_REG);
341 		hw_hhmmss = rtc_reg_read(chip, RTC_HHMMSS_REG);
342 	} while ((hw_day != rtc_reg_read(chip, RTC_DAY_REG)) ||
343 			(hw_hhmmss != rtc_reg_read(chip, RTC_HHMMSS_REG)));
344 	err = hw_day_hhmmss_to_tm(chip->data, hw_day, hw_hhmmss, tm);
345 	if (err)
346 		return err;
347 
348 	err = rtc_valid_tm(tm);
349 	if (err) {
350 		dev_err(dev, "sunxi_rtc_gettime(): Invalid rtc_time: %ptR\n", tm);
351 		return err;
352 	}
353 
354 	dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
355 	return err;
356 }
357 
sunxi_rtc_settime(struct device * dev,struct rtc_time * tm)358 static int sunxi_rtc_settime(struct device *dev, struct rtc_time *tm)
359 {
360 	struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
361 	u32 hw_day, hw_hhmmss;
362 	int real_year;
363 	int err;
364 
365 	dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
366 
367 	err = rtc_valid_tm(tm);
368 	if (err) {
369 		dev_err(dev, "sunxi_rtc_settime(): Invalid rtc_time: %ptR\n", tm);
370 		return err;
371 	}
372 
373 	real_year = TM_YEAR_TO_REAL_YEAR(tm->tm_year);
374 	if (rtc_valid_tm(tm)
375 	     || real_year < chip->data->min_year
376 	     || real_year > chip->data->max_year) {
377 		dev_err(dev, "Invalid year %d. Should be in range %d ~ %d\n",
378 			real_year, chip->data->min_year, chip->data->max_year);
379 		return -EINVAL;
380 	}
381 
382 	err = tm_to_hw_day_hhmmss(chip->data, tm, &hw_day, &hw_hhmmss);
383 	if (err)
384 		return err;
385 	/* Before writting RTC_HHMMSS_REG, we should check the RTC_HHMMSS_ACCESS bit */
386 	err = wait_bits_cleared(chip, LOSC_CTRL_REG, RTC_HHMMSS_ACCESS, 50);
387 	if (err) {
388 		dev_err(dev, "sunxi_rtc_settime(): wait_bits_cleared() timeout (1)\n");
389 		return err;
390 	}
391 	rtc_reg_write(chip, RTC_HHMMSS_REG, hw_hhmmss);
392 	err = wait_bits_cleared(chip, LOSC_CTRL_REG, RTC_HHMMSS_ACCESS, 50);
393 	if (err) {
394 		dev_err(dev, "sunxi_rtc_settime(): wait_bits_cleared() timeout (2)\n");
395 		return err;
396 	}
397 	/* Before writting RTC_DAY_REG, we should check the RTC_DAY_ACCESS bit */
398 	err = wait_bits_cleared(chip, LOSC_CTRL_REG, RTC_DAY_ACCESS, 50);
399 	if (err) {
400 		dev_err(dev, "sunxi_rtc_settime(): wait_bits_cleared() timeout (3)\n");
401 		return err;
402 	}
403 	rtc_reg_write(chip, RTC_DAY_REG, hw_day);
404 	err = wait_bits_cleared(chip, LOSC_CTRL_REG, RTC_DAY_ACCESS, 50);
405 	if (err) {
406 		dev_err(dev, "sunxi_rtc_settime(): wait_bits_cleared() timeout (4)\n");
407 		return err;
408 	}
409 
410 	return 0;
411 }
412 
413 extern bool alarm0_is_enabled(struct sunxi_rtc_dev *chip);
414 extern void alarm0_ctrl(struct sunxi_rtc_dev *chip, bool en);
415 
416 /*
417  * The RTC has two DAY registers: RTC_DAY_REG and ALARM0_DAY_REG.
418  * RTC_DAY_REG starts from 1, but ALARM0_DAY_REG starts from 0.
419  * If RTC power losts, ALARM0_DAY_REG will resets to it's default value 0.
420  * When RTC is registered into the framework, sunxi_rtc_getalarm() will be called.
421  * And if ALARM0_DAY_REG has a value of 0, hw_day_hhmmss_to_tm() will throw an error,
422  * which causes the probe failure.
423  * So we fix it here: Forcely modify the default value of ALARM0_DAY_REG from 0 to 1.
424  */
errata__fix_alarm_day_reg_default_value(struct device * dev)425 static void errata__fix_alarm_day_reg_default_value(struct device *dev)
426 {
427 	struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
428 
429 	if (rtc_reg_read(chip, ALARM0_DAY_REG) == 0) {
430 		dev_info(dev, "%s(): ALARM0_DAY_REG=0, set it to 1\n", __func__);
431 		rtc_reg_write(chip, ALARM0_DAY_REG, 1);
432 	}
433 }
434 
sunxi_rtc_getalarm(struct device * dev,struct rtc_wkalrm * wkalrm)435 static int sunxi_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
436 {
437 	struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
438 	struct rtc_time *tm = &wkalrm->time;
439 	u32 hw_day, hw_hhmmss;
440 	int err;
441 
442 	hw_day = rtc_reg_read(chip, ALARM0_DAY_REG);
443 	hw_hhmmss = rtc_reg_read(chip, ALARM0_HHMMSS_REG);
444 	err = hw_day_hhmmss_to_tm(chip->data, hw_day, hw_hhmmss, tm);
445 	if (err)
446 		return err;
447 
448 	err = rtc_valid_tm(tm);
449 	if (err)
450 		dev_err(dev, "sunxi_rtc_getalarm(): Invalid rtc_time: %ptR\n", tm);
451 
452 	wkalrm->enabled = alarm0_is_enabled(chip);
453 
454 	dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
455 	return 0;
456 }
457 
sunxi_rtc_setalarm(struct device * dev,struct rtc_wkalrm * wkalrm)458 static int sunxi_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
459 {
460 	struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
461 	struct rtc_time *tm = &wkalrm->time;
462 	u32 hw_day, hw_hhmmss;
463 	int err;
464 
465 	dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
466 
467 	err = rtc_valid_tm(tm);
468 	if (err) {
469 		dev_err(dev, "sunxi_rtc_setalarm(): Invalid rtc_time: %ptR\n", tm);
470 		return err;
471 	}
472 	//@TODO: check that tm should be later than current time?
473 
474 	err = tm_to_hw_day_hhmmss(chip->data, tm, &hw_day, &hw_hhmmss);
475 	if (err)
476 		return err;
477 	rtc_reg_write(chip, ALARM0_DAY_REG, hw_day);
478 	rtc_reg_write(chip, ALARM0_HHMMSS_REG, hw_hhmmss);
479 
480 	alarm0_ctrl(chip, wkalrm->enabled);
481 
482 	return 0;
483 }
484 
485 /* Alarm0 Interrupt Sevice Routine */
sunxi_rtc_alarm0_isr(int irq,void * id)486 static irqreturn_t sunxi_rtc_alarm0_isr(int irq, void *id)
487 {
488 	struct sunxi_rtc_dev *chip = (struct sunxi_rtc_dev *)id;
489 	u32 val;
490 
491 	val = rtc_reg_read(chip, ALARM0_IRQ_STA_REG);
492 	if (val & ALARM0_IRQ_PEND) {
493 		val |= ALARM0_IRQ_PEND;
494 		rtc_reg_write(chip, ALARM0_IRQ_STA_REG, val);  /* clear the interrupt */
495 		rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);  /* Report Alarm interrupt */
496 		dev_dbg(chip->dev, "%s(): IRQ_HANDLED\n", __func__);
497 		return IRQ_HANDLED;
498 	}
499 
500 	return IRQ_NONE;
501 }
502 
alarm0_is_enabled(struct sunxi_rtc_dev * chip)503 bool alarm0_is_enabled(struct sunxi_rtc_dev *chip)
504 {
505 	u32 val1, val2;
506 
507 	val1 = rtc_reg_read(chip, ALARM0_ENABLE_REG);
508 	val2 = rtc_reg_read(chip, ALARM0_IRQ_EN_REG);
509 
510 	return (val1 & ALARM0_ENABLE) && (val2 & ALARM0_IRQ_EN);
511 }
512 
alarm0_ctrl(struct sunxi_rtc_dev * chip,bool en)513 void alarm0_ctrl(struct sunxi_rtc_dev *chip, bool en)
514 {
515 	u32 val;
516 
517 	if (en) {
518 		/* enable alarm0 */
519 		val = rtc_reg_read(chip, ALARM0_ENABLE_REG);
520 		val |= ALARM0_ENABLE;
521 		rtc_reg_write(chip, ALARM0_ENABLE_REG, val);
522 		/* enable the interrupt */
523 		val = rtc_reg_read(chip, ALARM0_IRQ_EN_REG);
524 		val |= ALARM0_IRQ_EN;
525 		rtc_reg_write(chip, ALARM0_IRQ_EN_REG, val);
526 		dev_dbg(chip->dev, "%s(): enabled\n", __func__);
527 	} else {
528 		/* disable alarm0 */
529 		val = rtc_reg_read(chip, ALARM0_ENABLE_REG);
530 		val &= ~ALARM0_ENABLE;
531 		rtc_reg_write(chip, ALARM0_ENABLE_REG, val);
532 		/* disable the interrupt */
533 		val = rtc_reg_read(chip, ALARM0_IRQ_EN_REG);
534 		val &= ~ALARM0_IRQ_EN;
535 		rtc_reg_write(chip, ALARM0_IRQ_EN_REG, val);
536 		/* clear the interrupt */
537 		val = rtc_reg_read(chip, ALARM0_IRQ_STA_REG);
538 		val |= ALARM0_IRQ_PEND;
539 		rtc_reg_write(chip, ALARM0_IRQ_STA_REG, val);
540 		dev_dbg(chip->dev, "%s(): disabled\n", __func__);
541 	}
542 }
543 
alarm0_reset(struct sunxi_rtc_dev * chip)544 static __maybe_unused void alarm0_reset(struct sunxi_rtc_dev *chip)
545 {
546 	struct rtc_wkalrm wkalrm;
547 
548 	wkalrm.time.tm_year = HW_YEAR_TO_TM_YEAR(0, chip->data->min_year);
549 	wkalrm.time.tm_mon = 0;
550 	wkalrm.time.tm_mday = 1;
551 	wkalrm.time.tm_hour = 0;
552 	wkalrm.time.tm_min = 0;
553 	wkalrm.time.tm_sec = 0;
554 	wkalrm.enabled = false;
555 
556 	sunxi_rtc_setalarm(chip->dev, &wkalrm);
557 }
558 
alarm0_output_ctrl(struct sunxi_rtc_dev * chip,bool en)559 static void alarm0_output_ctrl(struct sunxi_rtc_dev *chip, bool en)
560 {
561 	u32 val;
562 
563 	/*
564 	 * Alarm's output control:
565 	 * When the alarm expires in poweroff state (while SoC pin RESET# is asserted),
566 	 * the SoC pin 'AP-NMI#' will ouput LOW to PMIC, so that the system is powered up.
567 	 */
568 
569 	if (en) {
570 		/*
571 		 * Release 'AP-NMI#' (LOW -> HIGH) by clearing alarm IRQ pending status.
572 		 * Some newer SoCs (such as A100) don't need this -- 'AP-NMI#' will be
573 		 * released automatically after SoC pin RESET# is de-asserted.
574 		 */
575 		/* We don't have to do this here... sunxi_rtc_alarm0_isr() will do
576 		val = rtc_reg_read(chip, ALARM0_IRQ_STA_REG);
577 		val |= ALARM0_IRQ_PEND;
578 		rtc_reg_write(chip, ALARM0_IRQ_STA_REG, val);
579 		*/
580 
581 		/* Enable alarm output */
582 		val = rtc_reg_read(chip, ALARM0_CONFIG_REG);
583 		val |= ALARM0_OUTPUT_EN;
584 		rtc_reg_write(chip, ALARM0_CONFIG_REG, val);
585 
586 		dev_dbg(chip->dev, "%s(): enabled\n", __func__);
587 	} else {
588 		/* Disable alarm output */
589 		val = rtc_reg_read(chip, ALARM0_CONFIG_REG);
590 		val &= ~ALARM0_OUTPUT_EN;
591 		rtc_reg_write(chip, ALARM0_CONFIG_REG, val);
592 
593 		dev_dbg(chip->dev, "%s(): disabled\n", __func__);
594 	}
595 }
596 
sunxi_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)597 static int sunxi_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
598 {
599 	struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
600 
601 	alarm0_ctrl(chip, enabled);
602 
603 	return 0;
604 }
605 
606 static const struct rtc_class_ops sunxi_rtc_ops = {
607 	.read_time	  = sunxi_rtc_gettime,
608 	.set_time	  = sunxi_rtc_settime,
609 	.read_alarm	  = sunxi_rtc_getalarm,
610 	.set_alarm	  = sunxi_rtc_setalarm,
611 	.alarm_irq_enable = sunxi_rtc_alarm_irq_enable
612 };
613 
614 static struct sunxi_rtc_data sunxi_rtc_v200_data = {
615 	.min_year   = 1970,
616 	.max_year   = HW_YEAR_MAX(1970),
617 	.gpr_offset = 0x100,
618 	.gpr_len    = 8,
619 	.has_dcxo_ictrl = false,
620 };
621 
622 static struct sunxi_rtc_data sunxi_rtc_v201_data = {
623 	.min_year   = 1970,
624 	.max_year   = HW_YEAR_MAX(1970),
625 	.gpr_offset = 0x100,
626 	.gpr_len    = 8,
627 	.has_dcxo_ictrl = true,
628 	.dcxo_ictrl_val = 0xf,
629 };
630 
631 static const struct of_device_id sunxi_rtc_dt_ids[] = {
632 	{.compatible = "allwinner,rtc-v200",        .data = &sunxi_rtc_v200_data},
633 	{.compatible = "allwinner,rtc-v201",        .data = &sunxi_rtc_v201_data},
634 	{ /* sentinel */ },
635 };
636 MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
637 
638 /* These sysfs files are only for RTC test */
min_year_show(struct device * dev,struct device_attribute * attr,char * buf)639 static ssize_t min_year_show(struct device *dev,
640 		struct device_attribute *attr, char *buf)
641 {
642 	struct platform_device *pdev = container_of(dev, struct platform_device, dev);
643 	struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
644 	return snprintf(buf, PAGE_SIZE, "%u \n", chip->data->min_year);
645 }
max_year_show(struct device * dev,struct device_attribute * attr,char * buf)646 static ssize_t max_year_show(struct device *dev,
647 		struct device_attribute *attr, char *buf)
648 {
649 	struct platform_device *pdev = container_of(dev, struct platform_device, dev);
650 	struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
651 	return snprintf(buf, PAGE_SIZE, "%u \n", chip->data->max_year);
652 }
653 
dcxo_ictrl_show(struct device * dev,struct device_attribute * attr,char * buf)654 static ssize_t dcxo_ictrl_show(struct device *dev,
655 	struct device_attribute *attr, char *buf)
656 {
657 	unsigned int val;
658 	struct platform_device *pdev = container_of(dev, struct platform_device, dev);
659 	struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
660 
661 	val = rtc_reg_read(chip, XO_CTRL_REG);
662 	val = (val & XO_CTRL_MASK) >> XO_ICTRL_SHIFT;
663 
664 	return sprintf(buf, "0x%x\n", val);
665 
666 }
667 
dcxo_ictrl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)668 static ssize_t dcxo_ictrl_store(struct device *dev,
669 	struct device_attribute *attr, const char *buf, size_t size)
670 {
671 	u32 val, temp, ret;
672 	char str[5]; /* the value rage is 0x0~0xf, so the len is set to 5 */
673 	struct platform_device *pdev = container_of(dev, struct platform_device, dev);
674 	struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
675 
676 	if (size >= ARRAY_SIZE(str)) {
677 		dev_err(dev, "parameter is too long\n");
678 		return -EINVAL;
679 	}
680 
681 	ret = kstrtou32(buf, 16, &val);
682 	if (ret) {
683 		dev_err(dev, "invalid para!\n");
684 		return -EINVAL;
685 	}
686 
687 	temp = rtc_reg_read(chip, XO_CTRL_REG);
688 	temp = (temp & (~XO_CTRL_MASK)) | (val << XO_ICTRL_SHIFT);
689 	rtc_reg_write(chip, XO_CTRL_REG, temp);
690 
691 	return size;
692 
693 }
694 
695 static struct device_attribute min_year_attr =
696 	__ATTR(min_year, S_IRUGO, min_year_show, NULL);
697 static struct device_attribute max_year_attr =
698 	__ATTR(max_year, S_IRUGO, max_year_show, NULL);
699 static struct device_attribute dcxo_ictrl_attr =
700 	__ATTR(dcxo_ictrl, 0664, dcxo_ictrl_show, dcxo_ictrl_store);
701 
rtc_registers_access_check(struct sunxi_rtc_dev * chip)702 static int rtc_registers_access_check(struct sunxi_rtc_dev *chip)
703 {
704 	struct device *dev = chip->dev;
705 	void __iomem *addr = chip->base + chip->data->gpr_offset + 0 * 0x04;  /* The first General Purpose Register */
706 	u32 val_backup;
707 	u32 val_w = 0x12345678;
708 	u32 val_r;
709 
710 	val_backup = readl(addr);
711 	writel(val_w, addr);
712 	val_r = readl(addr);
713 	if (val_r != val_w) {
714 		dev_err(dev, "%s(): FAILED: Expect 0x%x but got 0x%x\n", __func__, val_w, val_r);
715 		writel(val_backup, addr);
716 		return -EINVAL;
717 	}
718 	writel(val_backup, addr);
719 	return 0;
720 }
721 
722 /* Inform user about the alarm-irq with sysfs node 'alarm_in_booting' */
723 static int alarm_in_booting;
724 module_param(alarm_in_booting, int, S_IRUGO | S_IWUSR);
sunxi_rtc_poweroff_alarm(struct sunxi_rtc_dev * chip)725 static int sunxi_rtc_poweroff_alarm(struct sunxi_rtc_dev *chip)
726 {
727 #if IS_ENABLED(CONFIG_AW_RTC_POWEROFF_ALARM)
728 	int err;
729 	bool alarm0_en;
730 	bool alarm0_irq_pending;
731 	bool alarm0_expired;
732 	struct rtc_wkalrm wkalrm;
733 	struct rtc_time now;
734 
735 	alarm0_en = alarm0_is_enabled(chip);
736 	alarm0_irq_pending = rtc_reg_read(chip, ALARM0_IRQ_STA_REG) & ALARM0_IRQ_PEND;
737 
738 	/* @WARNING: Driver callbacks are used before RTC is registered! Take care of this... */
739 	err = sunxi_rtc_getalarm(chip->dev, &wkalrm);
740 	if (err) {
741 		dev_err(chip->dev, "%s(): get alarm failed\n", __func__);
742 		return err;
743 	}
744 	err = sunxi_rtc_gettime(chip->dev, &now);
745 	if (err) {
746 		dev_err(chip->dev, "%s(): get time failed\n", __func__);
747 		return err;
748 	}
749 	alarm0_expired = (rtc_tm_sub(&now, &(wkalrm.time)) >= 0);
750 
751 	if (alarm0_en && alarm0_irq_pending && alarm0_expired)
752 		alarm_in_booting = 1;
753 
754 	dev_dbg(chip->dev, "%s(): alarm0_en=%d, alarm0_irq_pending=%d, alarm0_expired=%d, alarm=%ptR, now=%ptR\n",
755 		 __func__, alarm0_en, alarm0_irq_pending, alarm0_expired, &(wkalrm.time), &now);
756 
757 	alarm0_output_ctrl(chip, true);
758 
759 	return 0;
760 #else
761 	/* This will clear the alarm that was set before. We should not do this? */
762 	/* alarm0_reset(chip); */
763 
764 	alarm0_output_ctrl(chip, false);
765 
766 	return 0;
767 #endif
768 }
769 
770 #if IS_ENABLED(CONFIG_AW_RTC_REBOOT_FLAG)
771 
772 static void __iomem *gpr_cur_addr;  /* Currently Used General Purpose Register's Address */
773 
774 struct str_num_pair {
775 	const char *str;
776 	u32 num;
777 };
778 
779 #define REBOOT_PARAM_LEN_MAX    32  /* Max length of user input from sysfs (including '\0') */
780 static const struct str_num_pair str_flag_table[] = {
781 	{ "debug",		0x59 },
782 	{ "efex",		0x5A },
783 	{ "boot-resignature",	0x5B },
784 	{ "recovery",		0x5C },
785 	{ "boot-recovery",	0x5C },
786 	{ "sysrecovery",	0x5D },
787 	{ "usb-recovery",	0x5E },
788 	{ "bootloader",		0x5F },
789 	{ "uboot",		0x60 },
790 };
791 
str2flag(const char * str)792 static unsigned int str2flag(const char *str)
793 {
794 	unsigned int flag = 0;  /* default value is 0 */
795 	int i;
796 
797 	for (i = 0; i < ARRAY_SIZE(str_flag_table); i++) {
798 		if (!strcmp(str, str_flag_table[i].str)) {
799 			flag = str_flag_table[i].num;
800 			break;
801 		}
802 	}
803 
804 	return flag;
805 }
806 
reboot_callback(struct notifier_block * this,unsigned long code,void * data)807 static int reboot_callback(struct notifier_block *this,
808 	unsigned long code, void *data)
809 {
810 	unsigned int flag;
811 	const char *str = (const char *)data;
812 
813 	if (!data) {
814 		pr_info("%s(): empty arg\n", __func__);
815 		return NOTIFY_DONE;
816 	}
817 
818 	flag = str2flag(str);
819 	if (flag == 0) {
820 		pr_info("%s(): unkown arg '%s'\n", __func__, str);
821 		return NOTIFY_DONE;
822 	}
823 
824 	pr_info("%s(): store flag '%s' (0x%x) in RTC General-Purpose-Register\n", __func__, str, flag);
825 	writel(flag, gpr_cur_addr);
826 	return NOTIFY_DONE;
827 }
828 
829 static struct notifier_block reboot_notifier = {
830 	.notifier_call = reboot_callback,
831 };
832 
flag_show(struct device * dev,struct device_attribute * attr,char * buf)833 static ssize_t flag_show(struct device *dev,
834 	struct device_attribute *attr, char *buf)
835 {
836 	unsigned int flag;
837 	flag = readl(gpr_cur_addr);
838 	return sprintf(buf, "0x%x\n", flag);
839 }
840 
flag_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)841 static ssize_t flag_store(struct device *dev,
842 	struct device_attribute *attr, const char *buf, size_t size)
843 {
844 	unsigned int flag;
845 	char str[REBOOT_PARAM_LEN_MAX];
846 
847 	/* 'buf' contains an extra '\n', we need to remove it here */
848 	if (size >= ARRAY_SIZE(str)) {
849 		dev_err(dev, "parameter is too long\n");
850 		return size;
851 	}
852 	strncpy(str, buf, size - 1);
853 	str[size - 1] = '\0';
854 
855 	flag = str2flag(str);
856 	if (flag == 0) {
857 		dev_err(dev, "unkown arg '%s'\n", str);
858 		return size;
859 	}
860 	dev_dbg(dev, "store flag '%s' (0x%x) in RTC General-Purpose-Register\n", str, flag);
861 	writel(flag, gpr_cur_addr);
862 
863 	return size;
864 }
865 
866 static struct device_attribute flag_attr =
867 	__ATTR(flag, 0664, flag_show, flag_store);
868 
sunxi_rtc_reboot_flag_setup(struct sunxi_rtc_dev * chip)869 static int sunxi_rtc_reboot_flag_setup(struct sunxi_rtc_dev *chip)
870 {
871 	struct device *dev = chip->dev;
872 	int err;
873 	u32 value;
874 	const char *name = "gpr_cur_pos";  /* A dts property indicating where to store the reboot flag */
875 
876 	err = of_property_read_u32(dev->of_node, name, &value);
877 	if (err) {
878 		dev_err(dev, "Fail to read dts property '%s'\n", name);
879 		return err;
880 	}
881 	dev_dbg(dev, "Read dts property '%s' = 0x%x\n", name, value);
882 	if (value >= chip->data->gpr_len) {
883 		dev_err(dev, "dts property '%s' is out of range!\n", name);
884 		return -EINVAL;
885 	}
886 	gpr_cur_addr = chip->base + chip->data->gpr_offset + value * 0x4;
887 
888 	/* When rebooting, reboot_notifier.notifier_call will be called.
889 	 * If you pass a parameter to 'reboot' like this: $ reboot param
890 	 * - For Android: reboot_notifier.notifier_call(..., data=param).
891 	 * - For busybox: reboot_notifier.notifier_call(..., data=NULL). i.e. param will be lost.
892 	 */
893 	err = register_reboot_notifier(&reboot_notifier);
894 	if (err) {
895 		dev_err(dev, "register reboot notifier error %d\n", err);
896 		return err;
897 	}
898 
899 	/* Export sunxi-rtc-flag to sysfs:
900 	 * Android's reboot can pass parameters to kernel but busybox's reboot can not.
901 	 * Here we export rtc-flag to sysfs, so the following command can be used as
902 	 * an alternative of "reboot efex":
903 	 * $ echo efex > /sys/devices/platform/soc@2900000/7000000.rtc/flag; reboot
904 	 */
905 	err = device_create_file(dev, &flag_attr);
906 	if (err) {
907 		dev_err(dev, "device_create_file() failed\n");
908 		/* unregister_reboot_notifier(&reboot_notifier); */
909 		/* We don't need to rollback here:
910 		 * 'reboot_notifier' and 'flag_attr' does not rely on each other.
911 		 * Even one of them cannot work, the other one could still be useful.
912 		 */
913 		return err;
914 	}
915 
916 	return 0;
917 }
918 
sunxi_rtc_reboot_flag_destroy(struct sunxi_rtc_dev * chip)919 static void sunxi_rtc_reboot_flag_destroy(struct sunxi_rtc_dev *chip)
920 {
921 	struct device *dev = chip->dev;
922 
923 	device_remove_file(dev, &flag_attr);
924 	unregister_reboot_notifier(&reboot_notifier);
925 	gpr_cur_addr = NULL;
926 }
927 
928 #else  /* CONFIG_AW_RTC_REBOOT_FLAG */
929 #define sunxi_rtc_reboot_flag_setup(chip)       (0)
930 #define sunxi_rtc_reboot_flag_destroy(chip)
931 #endif  /* CONFIG_AW_RTC_REBOOT_FLAG */
932 
sunxi_rtc_probe(struct platform_device * pdev)933 static int sunxi_rtc_probe(struct platform_device *pdev)
934 {
935 	struct sunxi_rtc_dev *chip;
936 	struct device *dev = &pdev->dev;
937 	struct device_node *np = dev->of_node;
938 	const struct of_device_id *of_id;
939 	struct resource *res;
940 	int err;
941 	u32 val;
942 
943 	dev_dbg(dev, "%s(): BEGIN\n", __func__);
944 
945 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
946 	if (!chip)
947 		return -ENOMEM;
948 
949 	of_id = of_match_device(sunxi_rtc_dt_ids, dev);
950 	if (!of_id) {
951 		dev_err(dev, "of_match_device() failed\n");
952 		return -EINVAL;
953 	}
954 	chip->data = (struct sunxi_rtc_data *)(of_id->data);
955 
956 	platform_set_drvdata(pdev, chip);
957 	chip->dev = dev;
958 
959 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
960 	if (!res) {
961 		dev_err(dev, "Fail to get IORESOURCE_MEM\n");
962 		return -EINVAL;
963 	}
964 	chip->base = devm_ioremap_resource(dev, res);
965 	if (IS_ERR(chip->base)) {
966 		dev_err(dev, "Fail to map IO resource\n");
967 		return PTR_ERR(chip->base);
968 	}
969 
970 	/* FIXME: Move the reset and clk logic to a separated function */
971 	/* Release the reset */
972 	chip->reset = devm_reset_control_get(dev, NULL);
973 	if (IS_ERR(chip->reset)) {
974 		dev_err(dev, "reset_control_get() failed\n");
975 		/* Some SoCs have no resets... Let's continue */
976 		/*
977 		err = PTR_ERR(chip->reset);
978 		goto err1;
979 		*/
980 	} else {
981 		err = reset_control_deassert(chip->reset);
982 		if (err) {
983 			dev_err(dev, "reset_control_deassert() failed\n");
984 			goto err1;
985 		}
986 	}
987 
988 	/* Enable BUS clock */
989 	chip->clk_bus = devm_clk_get(dev, "r-ahb-rtc");
990 	if (IS_ERR(chip->clk_bus)) {
991 		dev_err(dev, "Fail to get clock 'r-ahb-rtc'\n");
992 		err = PTR_ERR(chip->clk_bus);
993 		goto err2;
994 	}
995 	err = clk_prepare_enable(chip->clk_bus);
996 	if (err) {
997 		dev_err(dev, "Cannot enable clock 'r-ahb-rtc'\n");
998 		goto err2;
999 	}
1000 
1001 	/* Enable RTC clock */
1002 	chip->clk = devm_clk_get(dev, "rtc-1k");
1003 	if (IS_ERR(chip->clk)) {
1004 		dev_err(dev, "Fail to get clock 'rtc-1k'\n");
1005 		err = PTR_ERR(chip->clk);
1006 		goto err3;
1007 	}
1008 	err = clk_prepare_enable(chip->clk);
1009 	if (err) {
1010 		dev_err(dev, "Cannot enable clock 'rtc-1k'\n");
1011 		goto err3;
1012 	}
1013 
1014 	/* Enable RTC SPI clock */
1015 	chip->clk_spi = devm_clk_get(dev, "rtc-spi");
1016 	if (IS_ERR(chip->clk_spi))
1017 		dev_warn(dev, "Fail to get clock 'rtc-spi'\n");
1018 		/* Some SoCs have no such clock. Let's continue */
1019 	else {
1020 		err = clk_prepare_enable(chip->clk_spi);
1021 		if (err) {
1022 			dev_err(dev, "Cannot enable clock 'rtc-spi'\n");
1023 			goto err4;
1024 		}
1025 	}
1026 
1027 	/* Now that the clks and resets are ready. We should be able to read/write registers.
1028 	 * Let's ensure this before any access to the registers...Because we always have troubles here.
1029 	 */
1030 	err = rtc_registers_access_check(chip);
1031 	if (err) {
1032 		goto err5;
1033 	}
1034 
1035 	/* We must ensure the error value of ALARM0_DAY_REG is fixed before rtc_register_device().
1036 	 * Because ALARM0_DAY_REG will be read during registration.
1037 	 */
1038 	errata__fix_alarm_day_reg_default_value(chip->dev);
1039 
1040 	/*
1041 	 * sunxi_rtc_poweroff_alarm() must be placed before IRQ setting up.
1042 	 * Since we'll read the IRQ pending status in sunxi_rtc_poweroff_alarm(),
1043 	 * we don't want the IRQ to be handled and cleared before this.
1044 	 */
1045 	sunxi_rtc_poweroff_alarm(chip);
1046 
1047 	chip->irq = platform_get_irq(pdev, 0);
1048 	if (chip->irq < 0) {
1049 		dev_err(dev, "No IRQ resource %d\n", chip->irq);
1050 		err = chip->irq;
1051 		goto err5;
1052 	}
1053 	err = devm_request_irq(dev, chip->irq, sunxi_rtc_alarm0_isr, 0,
1054 				dev_name(dev), chip);
1055 	if (err) {
1056 		dev_err(dev, "Could not request IRQ\n");
1057 		goto err5;
1058 	}
1059 
1060 	if (of_property_read_bool(np, "wakeup-source")) {
1061 		device_init_wakeup(dev, true);
1062 		dev_pm_set_wake_irq(dev, chip->irq);
1063 	}
1064 
1065 	/* Register RTC device */
1066 	chip->rtc = devm_rtc_allocate_device(dev);
1067 	if (IS_ERR(chip->rtc))  {
1068 		dev_err(dev, "Unable to allocate RTC device\n");
1069 		err = PTR_ERR(chip->rtc);
1070 		goto err5;
1071 	}
1072 	chip->rtc->ops = &sunxi_rtc_ops;
1073 	err = rtc_register_device(chip->rtc);
1074 	if (err) {
1075 		dev_err(dev, "Unable to register RTC device\n");
1076 		goto err5;
1077 	}
1078 
1079 	err = sunxi_rtc_reboot_flag_setup(chip);
1080 	if (err) {
1081 		dev_err(dev, "sunxi_rtc_reboot_flag_setup() failed\n");
1082 		goto err5;
1083 	}
1084 
1085 	/* Change XO_CTRL_REG bit[24:27] value to dcxo_ictrl_val for
1086 	 * adjust current value to reduce power consumption
1087 	 */
1088 	if (chip->data->has_dcxo_ictrl) {
1089 		val = rtc_reg_read(chip, XO_CTRL_REG);
1090 		val |= (chip->data->dcxo_ictrl_val << XO_ICTRL_SHIFT);
1091 		rtc_reg_write(chip, XO_CTRL_REG, val);
1092 		device_create_file(dev, &dcxo_ictrl_attr);
1093 	}
1094 
1095 	device_create_file(dev, &min_year_attr);
1096 	device_create_file(dev, &max_year_attr);
1097 
1098 	dev_info(dev, "sunxi rtc probed\n");
1099 	return 0;
1100 
1101 err5:
1102 	if (!IS_ERR(chip->clk_spi))
1103 		clk_disable_unprepare(chip->clk_spi);
1104 err4:
1105 	clk_disable_unprepare(chip->clk);
1106 err3:
1107 	clk_disable_unprepare(chip->clk_bus);
1108 err2:
1109 	if (!IS_ERR(chip->reset))
1110 		reset_control_assert(chip->reset);
1111 err1:
1112 	return err;
1113 }
1114 
sunxi_rtc_remove(struct platform_device * pdev)1115 static int sunxi_rtc_remove(struct platform_device *pdev)
1116 {
1117 	struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
1118 	struct device *dev = &pdev->dev;
1119 
1120 	device_remove_file(dev, &max_year_attr);
1121 	device_remove_file(dev, &min_year_attr);
1122 	if (chip->data->has_dcxo_ictrl)
1123 		device_remove_file(dev, &dcxo_ictrl_attr);
1124 	sunxi_rtc_reboot_flag_destroy(chip);
1125 	clk_disable_unprepare(chip->clk_spi);
1126 	clk_disable_unprepare(chip->clk);
1127 	clk_disable_unprepare(chip->clk_bus);
1128 	reset_control_assert(chip->reset);
1129 
1130 	return 0;
1131 }
1132 
1133 static struct platform_driver sunxi_rtc_driver = {
1134 	.probe    = sunxi_rtc_probe,
1135 	.remove   = sunxi_rtc_remove,
1136 	.driver   = {
1137 		.name  = "sunxi-rtc",
1138 		.owner = THIS_MODULE,
1139 		.of_match_table = sunxi_rtc_dt_ids,
1140 	},
1141 };
1142 
sunxi_rtc_init(void)1143 static int __init sunxi_rtc_init(void)
1144 {
1145 	int err;
1146 
1147 	err = platform_driver_register(&sunxi_rtc_driver);
1148 	if (err)
1149 		pr_err("register sunxi rtc failed\n");
1150 
1151 	return err;
1152 }
1153 module_init(sunxi_rtc_init);
1154 
sunxi_rtc_exit(void)1155 static void __exit sunxi_rtc_exit(void)
1156 {
1157 	platform_driver_unregister(&sunxi_rtc_driver);
1158 }
1159 module_exit(sunxi_rtc_exit);
1160 
1161 MODULE_DESCRIPTION("sunxi RTC driver");
1162 MODULE_AUTHOR("Martin <wuyan@allwinnertech.com>");
1163 MODULE_LICENSE("GPL v2");
1164 MODULE_VERSION("1.1.6");
1165