b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Base driver for ASR SCS RTC |
| 3 | * |
| 4 | * Copyright 2020 ASR Microelectronics (Shanghai) Co., Ltd. |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General |
| 7 | * Public License. See the file "COPYING" in the main directory of this |
| 8 | * archive for more details. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/mfd/core.h> |
| 25 | #include <linux/mfd/88pm80x.h> |
| 26 | #include <linux/mfd/pm803.h> |
| 27 | #include <linux/mfd/pm802.h> |
| 28 | #include <linux/mfd/pm813.h> |
| 29 | #include <linux/rtc.h> |
| 30 | #include <linux/reboot.h> |
| 31 | #include <linux/sysfs.h> |
| 32 | #include <soc/asr/addr-map.h> |
| 33 | #include <linux/cputype.h> |
| 34 | |
| 35 | #ifdef CONFIG_CPU_ASR1903 |
| 36 | #define SCS_RTC_FREQ (32765) |
| 37 | #else |
| 38 | #define SCS_RTC_FREQ (32787) |
| 39 | #endif |
| 40 | |
| 41 | #define SCS_RTC_COUNTR (0x0) |
| 42 | #define SCS_RTC_ALARM (0x4) |
| 43 | #define SCS_RTC_TRIM (0x8) |
| 44 | #define SCS_RTC_ALARM_EN (0x1 << 0) |
| 45 | #define SCS_RTC_ALARM_MASK (0x1 << 0) |
| 46 | #define SCS_RTC_ALARM_CLR (0x1 << 1) |
| 47 | #define SCS_RTC_CNTL (0xc) |
| 48 | #define SCS_IDAC_CODE (0x10) |
| 49 | #define SCS_RTC_SYNC_CFG (0x14) |
| 50 | #define SCS_DCS_MODE (0x1c) |
| 51 | #define SCS_CLEAR_SYNC_DONE (0x1 << 0 | 0x1 << 6) |
| 52 | #define SCS_SYNC_IDAC_CODE (0x1 << 0 | 0x1 << 5) |
| 53 | #define SCS_SYNC_RTC_CNRL (0x1 << 0 | 0x1 << 4) |
| 54 | #define SCS_SYNC_RTC_TRIM (0x1 << 0 | 0x1 << 3) |
| 55 | #define SCS_SYNC_RTC_ALARM (0x1 << 0 | 0x1 << 2) |
| 56 | #define SCS_SYNC_RTC_ALARM_CNTL (0x1 << 0 | 0x1 << 2 | 0x1 << 4) |
| 57 | #define SCS_SYNC_RTC_COUNTER (0x1 << 0 | 0x1 << 1) |
| 58 | #define SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x03E000) |
| 59 | #define ASR1903_SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x0C0000) |
| 60 | |
| 61 | #define SCR_RTC_SYNC_UDELAY (130) |
| 62 | |
| 63 | struct scs_rtc_info { |
| 64 | spinlock_t lock; |
| 65 | void __iomem *base; |
| 66 | struct pm80x_chip *chip; |
| 67 | struct regmap *map; |
| 68 | struct rtc_device *rtc_dev; |
| 69 | struct device *dev; |
| 70 | int irq; |
| 71 | int vrtc; |
| 72 | int (*sync) (s64 ticks); |
| 73 | |
| 74 | #ifdef CONFIG_RTC_DRV_SA1100 |
| 75 | struct delayed_work sa1100_sync_work; |
| 76 | #endif |
| 77 | }; |
| 78 | |
| 79 | static s64 base_ticks; |
| 80 | #ifdef CONFIG_RTC_DRV_SA1100 |
| 81 | extern int sync_time_to_soc(s64 ticks); |
| 82 | #endif |
| 83 | static int scs_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled); |
| 84 | |
| 85 | #ifdef CONFIG_RTC_DRV_SA1100 |
| 86 | static int scs_rtc_read_time(struct device *dev, struct rtc_time *tm); |
| 87 | static void sa1100_sync_fn(struct work_struct *work) |
| 88 | { |
| 89 | struct scs_rtc_info *info = |
| 90 | container_of(work, struct scs_rtc_info, sa1100_sync_work.work); |
| 91 | s64 ticks; |
| 92 | struct rtc_time tm; |
| 93 | int ret; |
| 94 | |
| 95 | ret = scs_rtc_read_time(info->dev, &tm); |
| 96 | if (ret < 0) { |
| 97 | dev_err(info->dev, "Failed to read time.\n"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | ticks = rtc_tm_to_time64(&tm); |
| 102 | if (info->sync) |
| 103 | info->sync(ticks); |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | static int pm80x_write_base_ticks(struct scs_rtc_info *info, s64 base_ticks) |
| 108 | { |
| 109 | u8 buf[5]; |
| 110 | u32 reg = PM802_RTC_EXPIRE2_1; |
| 111 | |
| 112 | BUG_ON(CHIP_PM801 == info->chip->type); |
| 113 | buf[0] = base_ticks & 0xFF; |
| 114 | buf[1] = (base_ticks >> 8) & 0xFF; |
| 115 | buf[2] = (base_ticks >> 16) & 0xFF; |
| 116 | buf[3] = (base_ticks >> 24) & 0xFF; |
| 117 | buf[4] = (base_ticks >> 32) & 0xFF; |
| 118 | dev_dbg(info->dev, "set base ticks:0x%llx\n", base_ticks); |
| 119 | |
| 120 | if (CHIP_PM803 != info->chip->type) { |
| 121 | regmap_raw_write(info->map, reg, buf, 4); |
| 122 | } |
| 123 | |
| 124 | if (CHIP_PM802 == info->chip->type) { |
| 125 | if (CHIP_PM802_ID_B1 == info->chip->chip_id || |
| 126 | CHIP_PM802_ID_B0 == info->chip->chip_id) { |
| 127 | regmap_raw_read(info->map, PM802_RTC_MISC14, buf, 1); |
| 128 | buf[0] &= 0x0f; |
| 129 | buf[0] |= ((buf[4] & 0xf) << 4); |
| 130 | /* F1[7:4] */ |
| 131 | regmap_raw_write(info->map, PM802_RTC_MISC14, &buf[0], 1); |
| 132 | } else { |
| 133 | /* CD[7:0] */ |
| 134 | regmap_raw_write(info->map, PM802S_RTC_SPARED, &buf[4], 1); |
| 135 | } |
| 136 | } else if (CHIP_PM803 == info->chip->type) { |
| 137 | regmap_raw_write(info->map, PM803_RTC_MISC18, &buf[0], 3); |
| 138 | regmap_raw_write(info->map, PM803_RTC_SPARE5, &buf[3], 2); |
| 139 | } else if (CHIP_PM813 == info->chip->type) { |
| 140 | if (CHIP_PM813_ID == info->chip->chip_id) { |
| 141 | /* F6[7:0] */ |
| 142 | regmap_raw_write(info->map, PM813_RTC_MISC19, &buf[4], 1); |
| 143 | } else { |
| 144 | /* CD[7:0] */ |
| 145 | regmap_raw_write(info->map, PM813S_RTC_SPARED, &buf[4], 1); |
| 146 | } |
| 147 | } else { |
| 148 | BUG(); |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int pm80x_read_base_ticks(struct scs_rtc_info *info, s64 *base_ticks) |
| 155 | { |
| 156 | u8 buf[5]; |
| 157 | u32 reg = PM802_RTC_EXPIRE2_1; |
| 158 | |
| 159 | BUG_ON(CHIP_PM801 == info->chip->type); |
| 160 | |
| 161 | if (CHIP_PM803 != info->chip->type) { |
| 162 | regmap_raw_read(info->map, reg, buf, 4); |
| 163 | } |
| 164 | |
| 165 | if (CHIP_PM802 == info->chip->type) { |
| 166 | if (CHIP_PM802_ID_B1 == info->chip->chip_id || |
| 167 | CHIP_PM802_ID_B0 == info->chip->chip_id) { |
| 168 | /* F1[7:4] */ |
| 169 | regmap_raw_read(info->map, PM802_RTC_MISC14, &buf[4], 1); |
| 170 | buf[4] = (buf[4] >> 4) & 0xf; |
| 171 | } else { |
| 172 | /* CD[7:0] */ |
| 173 | regmap_raw_read(info->map, PM802S_RTC_SPARED, &buf[4], 1); |
| 174 | } |
| 175 | } else if (CHIP_PM803 == info->chip->type) { |
| 176 | regmap_raw_read(info->map, PM803_RTC_MISC18, &buf[0], 3); |
| 177 | regmap_raw_read(info->map, PM803_RTC_SPARE5, &buf[3], 2); |
| 178 | } else if (CHIP_PM813 == info->chip->type) { |
| 179 | if (CHIP_PM813_ID == info->chip->chip_id) { |
| 180 | /* F6[7:0] */ |
| 181 | regmap_raw_read(info->map, PM813_RTC_MISC19, &buf[4], 1); |
| 182 | } else { |
| 183 | /* CD[7:0] */ |
| 184 | regmap_raw_read(info->map, PM813S_RTC_SPARED, &buf[4], 1); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | *base_ticks = (((s64)buf[4]) << 60) >> 28; |
| 189 | *base_ticks |= (((u32)buf[3]) << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 190 | |
| 191 | dev_dbg(info->dev, "%x-%x-%x-%x-%x, base_ticks: %llx\n", |
| 192 | buf[0], buf[1], buf[2], buf[3], buf[4], *base_ticks); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int scs_rtc_alarm_clear(struct device *dev) |
| 198 | { |
| 199 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 200 | int ret = 0; |
| 201 | unsigned long flags; |
| 202 | |
| 203 | spin_lock_irqsave(&info->lock, flags); |
| 204 | writel(readl(info->base + SCS_RTC_CNTL) | SCS_RTC_ALARM_CLR, |
| 205 | info->base + SCS_RTC_CNTL); |
| 206 | writel(SCS_SYNC_RTC_CNRL, info->base + SCS_RTC_SYNC_CFG); |
| 207 | udelay(SCR_RTC_SYNC_UDELAY); |
| 208 | writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG); |
| 209 | spin_unlock_irqrestore(&info->lock, flags); |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | static int scs_rtc_update_trim(struct device *dev) |
| 215 | { |
| 216 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 217 | int ret = 0; |
| 218 | unsigned long flags; |
| 219 | |
| 220 | spin_lock_irqsave(&info->lock, flags); |
| 221 | if (cpu_is_asr1803_a0()) |
| 222 | writel(0xffff, info->base + SCS_RTC_TRIM); |
| 223 | else |
| 224 | writel((SCS_RTC_FREQ - 1), info->base + SCS_RTC_TRIM); |
| 225 | writel(SCS_SYNC_RTC_TRIM, info->base + SCS_RTC_SYNC_CFG); |
| 226 | udelay(SCR_RTC_SYNC_UDELAY); |
| 227 | writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG); |
| 228 | |
| 229 | if (cpu_is_asr1903()) { |
| 230 | writel(0x14, info->base + SCS_IDAC_CODE); |
| 231 | writel(SCS_SYNC_IDAC_CODE, info->base + SCS_RTC_SYNC_CFG); |
| 232 | udelay(SCR_RTC_SYNC_UDELAY); |
| 233 | writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG); |
| 234 | } |
| 235 | spin_unlock_irqrestore(&info->lock, flags); |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static irqreturn_t rtc_update_handler(int irq, void *data) |
| 241 | { |
| 242 | struct scs_rtc_info *info = (struct scs_rtc_info *)data; |
| 243 | |
| 244 | scs_rtc_alarm_clear(info->dev); |
| 245 | rtc_update_irq(info->rtc_dev, 1, RTC_AF); |
| 246 | return IRQ_HANDLED; |
| 247 | } |
| 248 | |
| 249 | static int scs_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 250 | { |
| 251 | unsigned long time_sec; |
| 252 | u64 tmp_sec; |
| 253 | u32 val1, val2; |
| 254 | s64 ticks; |
| 255 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 256 | |
| 257 | do { |
| 258 | val1 = readl(info->base + SCS_RTC_COUNTR); |
| 259 | val2 = readl(info->base + SCS_RTC_COUNTR); |
| 260 | } while (val2 != val1); |
| 261 | |
| 262 | if (cpu_is_asr1803_a0()) { |
| 263 | tmp_sec = ((u64)val1) * 261993; |
| 264 | do_div(tmp_sec, (3 * 128 * 1024)); |
| 265 | time_sec = (u32)tmp_sec; |
| 266 | } else { |
| 267 | time_sec = val1; |
| 268 | } |
| 269 | |
| 270 | pm80x_read_base_ticks(info, &base_ticks); |
| 271 | ticks = time_sec + base_ticks; |
| 272 | rtc_time64_to_tm(ticks, tm); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int scs_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 278 | { |
| 279 | unsigned long time; |
| 280 | u64 tmp_sec; |
| 281 | s64 ticks; |
| 282 | u32 val1, val2; |
| 283 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 284 | int ret = 0; |
| 285 | unsigned long flags; |
| 286 | |
| 287 | if ((tm->tm_year < 70) || (tm->tm_year > 300)) { |
| 288 | dev_err(info->dev, |
| 289 | "SCSRTC: Set time %d out of range. Please set time between 1970 to 2200.\n", |
| 290 | 1900 + tm->tm_year); |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | spin_lock_irqsave(&info->lock, flags); |
| 295 | ticks = rtc_tm_to_time64(tm); |
| 296 | do { |
| 297 | val1 = readl(info->base + SCS_RTC_COUNTR); |
| 298 | val2 = readl(info->base + SCS_RTC_COUNTR); |
| 299 | } while (val2 != val1); |
| 300 | |
| 301 | if (cpu_is_asr1803_a0()) { |
| 302 | tmp_sec = ((u64)val1) * 261993; |
| 303 | do_div(tmp_sec, (3 * 128 * 1024)); |
| 304 | time = (u32)tmp_sec; |
| 305 | } else { |
| 306 | time = val1; |
| 307 | } |
| 308 | |
| 309 | base_ticks = ticks - time; |
| 310 | spin_unlock_irqrestore(&info->lock, flags); |
| 311 | pm80x_write_base_ticks(info, base_ticks); |
| 312 | spin_lock_irqsave(&info->lock, flags); |
| 313 | #if 0 |
| 314 | if (cpu_is_asr1803_a0()) { |
| 315 | tmp_sec = ((u64)time) * (3 * 128 * 1024); |
| 316 | do_div(tmp_sec, 261993); |
| 317 | time = (unsigned long)tmp_sec; |
| 318 | } |
| 319 | writel(time, info->base + SCS_RTC_COUNTR); |
| 320 | |
| 321 | writel(SCS_SYNC_RTC_COUNTER, info->base + SCS_RTC_SYNC_CFG); |
| 322 | udelay(SCR_RTC_SYNC_UDELAY); |
| 323 | writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG); |
| 324 | #endif |
| 325 | if (info->sync) |
| 326 | info->sync(ticks); |
| 327 | spin_unlock_irqrestore(&info->lock, flags); |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | static int scs_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 333 | { |
| 334 | unsigned long ticks; |
| 335 | u64 tmp_sec; |
| 336 | s64 time; |
| 337 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 338 | |
| 339 | ticks = readl(info->base + SCS_RTC_ALARM); |
| 340 | if (cpu_is_asr1803_a0()) { |
| 341 | tmp_sec = ((u64)ticks) * 261993; |
| 342 | do_div(tmp_sec, (3 * 128 * 1024)); |
| 343 | ticks = (u32)tmp_sec; |
| 344 | } |
| 345 | |
| 346 | pm80x_read_base_ticks(info, &base_ticks); |
| 347 | time = ticks + base_ticks; |
| 348 | |
| 349 | rtc_time64_to_tm(time, &alrm->time); |
| 350 | ticks = readl(info->base + SCS_RTC_CNTL); |
| 351 | alrm->enabled = ((ticks & SCS_RTC_ALARM_MASK) == SCS_RTC_ALARM_EN) ? 1 : 0; |
| 352 | alrm->pending = 0; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int scs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 357 | { |
| 358 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 359 | unsigned long flags; |
| 360 | u64 tmp_sec; |
| 361 | s64 ticks; |
| 362 | u32 time; |
| 363 | |
| 364 | ticks = rtc_tm_to_time64(&alrm->time); |
| 365 | pm80x_read_base_ticks(info, &base_ticks); |
| 366 | ticks = ticks - base_ticks; |
| 367 | if (cpu_is_asr1803_a0()) { |
| 368 | tmp_sec = ((u64)ticks) * (3 * 128 * 1024); |
| 369 | //time = tmp_sec / 261993; |
| 370 | do_div(tmp_sec, 261993); |
| 371 | time = (u32)tmp_sec; |
| 372 | } else { |
| 373 | time = (u32)ticks; |
| 374 | } |
| 375 | |
| 376 | spin_lock_irqsave(&info->lock, flags); |
| 377 | writel(time, info->base + SCS_RTC_ALARM); |
| 378 | writel(SCS_SYNC_RTC_ALARM_CNTL, info->base + SCS_RTC_SYNC_CFG); |
| 379 | udelay(SCR_RTC_SYNC_UDELAY); |
| 380 | writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG); |
| 381 | spin_unlock_irqrestore(&info->lock, flags); |
| 382 | scs_rtc_alarm_irq_enable(dev, alrm->enabled); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int scs_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 387 | { |
| 388 | struct scs_rtc_info *info = dev_get_drvdata(dev); |
| 389 | int ret = 0; |
| 390 | unsigned long flags; |
| 391 | |
| 392 | spin_lock_irqsave(&info->lock, flags); |
| 393 | if (enabled) |
| 394 | writel(readl(info->base + SCS_RTC_CNTL) | SCS_RTC_ALARM_MASK, |
| 395 | info->base + SCS_RTC_CNTL); |
| 396 | else |
| 397 | writel(readl(info->base + SCS_RTC_CNTL) & (~SCS_RTC_ALARM_MASK), |
| 398 | info->base + SCS_RTC_CNTL); |
| 399 | writel(SCS_SYNC_RTC_CNRL, info->base + SCS_RTC_SYNC_CFG); |
| 400 | udelay(SCR_RTC_SYNC_UDELAY); |
| 401 | writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG); |
| 402 | spin_unlock_irqrestore(&info->lock, flags); |
| 403 | |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | static const struct rtc_class_ops scs_rtc_ops = { |
| 408 | .read_time = scs_rtc_read_time, |
| 409 | .set_time = scs_rtc_set_time, |
| 410 | .read_alarm = scs_rtc_read_alarm, |
| 411 | .set_alarm = scs_rtc_set_alarm, |
| 412 | .alarm_irq_enable = scs_rtc_alarm_irq_enable, |
| 413 | }; |
| 414 | |
| 415 | #ifdef CONFIG_PM_SLEEP |
| 416 | static int scs_rtc_suspend(struct device *dev) |
| 417 | { |
| 418 | return pm80x_dev_suspend(dev); |
| 419 | } |
| 420 | |
| 421 | static int scs_rtc_resume(struct device *dev) |
| 422 | { |
| 423 | return pm80x_dev_resume(dev); |
| 424 | } |
| 425 | #endif |
| 426 | |
| 427 | static SIMPLE_DEV_PM_OPS(scs_rtc_pm_ops, scs_rtc_suspend, scs_rtc_resume); |
| 428 | |
| 429 | static struct delayed_work sync_work; |
| 430 | static unsigned int print_tstamp_delay = 30; |
| 431 | static void sync_timestamp(struct work_struct *work) |
| 432 | { |
| 433 | struct timespec64 ts; |
| 434 | struct rtc_time tm; |
| 435 | |
| 436 | ktime_get_real_ts64(&ts); |
| 437 | rtc_time64_to_tm(ts.tv_sec - sys_tz.tz_minuteswest * 60, &tm); |
| 438 | pr_info("Timestamp is: %02d-%02d %02d:%02d:%02d.%03lu UTC\n", |
| 439 | tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, |
| 440 | tm.tm_sec, ts.tv_nsec); |
| 441 | /* print timestamp every 30 minutes */ |
| 442 | schedule_delayed_work(&sync_work, print_tstamp_delay*60*HZ); |
| 443 | } |
| 444 | |
| 445 | #ifdef CONFIG_SYSFS |
| 446 | static ssize_t tstamp_show_delay(struct device *dev, |
| 447 | struct device_attribute *attr, char *buf) |
| 448 | { |
| 449 | return snprintf(buf, PAGE_SIZE, "%u\n", print_tstamp_delay); |
| 450 | } |
| 451 | static ssize_t tstamp_store_delay(struct device *dev, |
| 452 | struct device_attribute *attr, |
| 453 | const char *buf, size_t size) |
| 454 | { |
| 455 | char *end; |
| 456 | unsigned long new = simple_strtoul(buf, &end, 0); |
| 457 | if (end == buf) |
| 458 | return -EINVAL; |
| 459 | print_tstamp_delay = new; |
| 460 | cancel_delayed_work(&sync_work); |
| 461 | schedule_delayed_work(&sync_work, print_tstamp_delay*60*HZ); |
| 462 | return size; |
| 463 | } |
| 464 | static DEVICE_ATTR(tstamp_delay, 0664, tstamp_show_delay, tstamp_store_delay); |
| 465 | |
| 466 | static int add_tstamp_delay(struct device *dev) |
| 467 | { |
| 468 | return device_create_file(dev, &dev_attr_tstamp_delay); |
| 469 | } |
| 470 | |
| 471 | static void remove_tstamp_delay(struct device *dev) |
| 472 | { |
| 473 | device_remove_file(dev, &dev_attr_tstamp_delay); |
| 474 | } |
| 475 | |
| 476 | #else |
| 477 | #define add_tstamp_delay(dev) 0 |
| 478 | #define remove_tstamp_delay(dev) do {} while (0) |
| 479 | #endif /* CONFIG_SYSFS */ |
| 480 | |
| 481 | static int scs_rtc_probe(struct platform_device *pdev) |
| 482 | { |
| 483 | struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 484 | struct pm80x_rtc_pdata *pdata = NULL; |
| 485 | struct scs_rtc_info *info; |
| 486 | struct rtc_time tm; |
| 487 | int ret; |
| 488 | |
| 489 | if (cpu_is_asr1803_z1()) |
| 490 | return -ENODEV; |
| 491 | |
| 492 | INIT_DEFERRABLE_WORK(&sync_work, sync_timestamp); |
| 493 | schedule_delayed_work(&sync_work, 1*60*HZ); |
| 494 | add_tstamp_delay(&pdev->dev); |
| 495 | |
| 496 | pdata = pdev->dev.platform_data; |
| 497 | if (IS_ENABLED(CONFIG_OF)) { |
| 498 | if (!pdata) { |
| 499 | pdata = devm_kzalloc(&pdev->dev, |
| 500 | sizeof(*pdata), GFP_KERNEL); |
| 501 | if (!pdata) |
| 502 | return -ENOMEM; |
| 503 | } |
| 504 | } else if (!pdata) { |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | info = |
| 509 | devm_kzalloc(&pdev->dev, sizeof(struct scs_rtc_info), GFP_KERNEL); |
| 510 | if (!info) |
| 511 | return -ENOMEM; |
| 512 | info->irq = platform_get_irq(pdev, 0); |
| 513 | if (info->irq < 0) { |
| 514 | dev_err(&pdev->dev, "No IRQ resource!\n"); |
| 515 | ret = -EINVAL; |
| 516 | goto out; |
| 517 | } |
| 518 | |
| 519 | info->chip = chip; |
| 520 | info->map = chip->regmap; |
| 521 | if (!info->map) { |
| 522 | dev_err(&pdev->dev, "no regmap!\n"); |
| 523 | ret = -EINVAL; |
| 524 | goto out; |
| 525 | } |
| 526 | if (cpu_is_asr1903()) |
| 527 | info->base = ASR1903_SCS_RTC_VIRT_BASE; |
| 528 | else |
| 529 | info->base = SCS_RTC_VIRT_BASE; |
| 530 | spin_lock_init(&info->lock); |
| 531 | |
| 532 | info->dev = &pdev->dev; |
| 533 | dev_set_drvdata(&pdev->dev, info); |
| 534 | |
| 535 | scs_rtc_alarm_clear(&pdev->dev); |
| 536 | scs_rtc_update_trim(&pdev->dev); |
| 537 | scs_rtc_alarm_irq_enable(&pdev->dev, 0); |
| 538 | ret = scs_rtc_read_time(&pdev->dev, &tm); |
| 539 | if (ret < 0) { |
| 540 | dev_err(&pdev->dev, "Failed to read initial time.\n"); |
| 541 | goto out; |
| 542 | } |
| 543 | |
| 544 | if ((tm.tm_year < 70) || (tm.tm_year > 1157)) { |
| 545 | tm.tm_year = 70; |
| 546 | tm.tm_mon = 0; |
| 547 | tm.tm_mday = 1; |
| 548 | tm.tm_hour = 0; |
| 549 | tm.tm_min = 0; |
| 550 | tm.tm_sec = 0; |
| 551 | ret = scs_rtc_set_time(&pdev->dev, &tm); |
| 552 | if (ret < 0) { |
| 553 | dev_err(&pdev->dev, "Failed to set initial time.\n"); |
| 554 | goto out; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | info->sync = NULL; /*initialize*/ |
| 559 | #ifdef CONFIG_RTC_DRV_SA1100 |
| 560 | info->sync = sync_time_to_soc; |
| 561 | INIT_DELAYED_WORK(&info->sa1100_sync_work, sa1100_sync_fn); |
| 562 | schedule_delayed_work(&info->sa1100_sync_work, 2 * HZ); |
| 563 | #endif |
| 564 | |
| 565 | dev_info(&pdev->dev, "%d-%d-%d-->%d: %d: %d\n", |
| 566 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 567 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 568 | |
| 569 | info->rtc_dev = devm_rtc_device_register(&pdev->dev, "scs-rtc", |
| 570 | &scs_rtc_ops, THIS_MODULE); |
| 571 | if (IS_ERR(info->rtc_dev)) { |
| 572 | ret = PTR_ERR(info->rtc_dev); |
| 573 | dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); |
| 574 | goto out; |
| 575 | } |
| 576 | |
| 577 | /* remeber whether this power up is caused by PMIC RTC or not. */ |
| 578 | info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup; |
| 579 | ret = pm80x_request_irq(chip, info->irq, rtc_update_handler, |
| 580 | IRQF_ONESHOT | IRQF_NO_SUSPEND , "rtc", info); |
| 581 | if (ret < 0) { |
| 582 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", |
| 583 | info->irq, ret); |
| 584 | goto out; |
| 585 | } |
| 586 | |
| 587 | BUG_ON((CHIP_PM801 == info->chip->type) || (CHIP_PM800 == info->chip->type)); |
| 588 | |
| 589 | device_init_wakeup(&pdev->dev, 1); |
| 590 | |
| 591 | return 0; |
| 592 | out: |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | static int scs_rtc_remove(struct platform_device *pdev) |
| 597 | { |
| 598 | struct scs_rtc_info *info = platform_get_drvdata(pdev); |
| 599 | |
| 600 | platform_set_drvdata(pdev, NULL); |
| 601 | pm80x_free_irq(info->chip, info->irq, info); |
| 602 | remove_tstamp_delay(&pdev->dev); |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static void scs_rtc_shutdown(struct platform_device *pdev) |
| 607 | { |
| 608 | struct timespec64 now; |
| 609 | struct rtc_time tm; |
| 610 | struct scs_rtc_info *info = platform_get_drvdata(pdev); |
| 611 | |
| 612 | /* sync timekeeping time to pmic rtc */ |
| 613 | ktime_get_real_ts64(&now); |
| 614 | if (now.tv_nsec < (NSEC_PER_SEC >> 1)) |
| 615 | rtc_time64_to_tm(now.tv_sec, &tm); |
| 616 | else |
| 617 | rtc_time64_to_tm(now.tv_sec + 1, &tm); |
| 618 | scs_rtc_set_time(info->dev, &tm); |
| 619 | pm80x_free_irq(info->chip, info->irq, info); |
| 620 | } |
| 621 | |
| 622 | static struct platform_driver scs_rtc_driver = { |
| 623 | .driver = { |
| 624 | .name = "scs-rtc", |
| 625 | .owner = THIS_MODULE, |
| 626 | .pm = &scs_rtc_pm_ops, |
| 627 | }, |
| 628 | .probe = scs_rtc_probe, |
| 629 | .remove = scs_rtc_remove, |
| 630 | .shutdown = scs_rtc_shutdown, |
| 631 | }; |
| 632 | |
| 633 | static int __init scs_rtc_init(void) |
| 634 | { |
| 635 | return platform_driver_register(&scs_rtc_driver); |
| 636 | } |
| 637 | module_init(scs_rtc_init); |
| 638 | |
| 639 | static void __exit scs_rtc_exit(void) |
| 640 | { |
| 641 | platform_driver_unregister(&scs_rtc_driver); |
| 642 | } |
| 643 | module_exit(scs_rtc_exit); |
| 644 | |
| 645 | MODULE_LICENSE("GPL"); |
| 646 | MODULE_DESCRIPTION("ASR PM803 and SCS RTC driver"); |