Lines Matching full:rtc

23 #include <linux/rtc.h>
120 /* Max STM32 RTC register offset is 0x3FC */
123 /* STM32 RTC driver time helpers */
126 /* STM32 RTC pinctrl helpers */
157 void (*clear_events)(struct stm32_rtc *rtc, unsigned int flags);
187 static void stm32_rtc_wpr_unlock(struct stm32_rtc *rtc) in stm32_rtc_wpr_unlock() argument
189 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_wpr_unlock()
191 writel_relaxed(RTC_WPR_1ST_KEY, rtc->base + regs->wpr); in stm32_rtc_wpr_unlock()
192 writel_relaxed(RTC_WPR_2ND_KEY, rtc->base + regs->wpr); in stm32_rtc_wpr_unlock()
195 static void stm32_rtc_wpr_lock(struct stm32_rtc *rtc) in stm32_rtc_wpr_lock() argument
197 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_wpr_lock()
199 writel_relaxed(RTC_WPR_WRONG_KEY, rtc->base + regs->wpr); in stm32_rtc_wpr_lock()
253 struct stm32_rtc *rtc = pinctrl_dev_get_drvdata(pctldev); in stm32_rtc_pinmux_action_alarm() local
254 struct stm32_rtc_registers regs = rtc->data->regs; in stm32_rtc_pinmux_action_alarm()
255 unsigned int cr = readl_relaxed(rtc->base + regs.cr); in stm32_rtc_pinmux_action_alarm()
256 unsigned int cfgr = readl_relaxed(rtc->base + regs.cfgr); in stm32_rtc_pinmux_action_alarm()
258 if (!rtc->data->has_alarm_out) in stm32_rtc_pinmux_action_alarm()
284 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_pinmux_action_alarm()
285 writel_relaxed(cr, rtc->base + regs.cr); in stm32_rtc_pinmux_action_alarm()
286 writel_relaxed(cfgr, rtc->base + regs.cfgr); in stm32_rtc_pinmux_action_alarm()
287 stm32_rtc_wpr_lock(rtc); in stm32_rtc_pinmux_action_alarm()
294 struct stm32_rtc *rtc = pinctrl_dev_get_drvdata(pctldev); in stm32_rtc_pinmux_lsco_available() local
295 struct stm32_rtc_registers regs = rtc->data->regs; in stm32_rtc_pinmux_lsco_available()
296 unsigned int cr = readl_relaxed(rtc->base + regs.cr); in stm32_rtc_pinmux_lsco_available()
297 unsigned int cfgr = readl_relaxed(rtc->base + regs.cfgr); in stm32_rtc_pinmux_lsco_available()
318 if (clk_get_rate(rtc->rtc_ck) != 32768) in stm32_rtc_pinmux_lsco_available()
326 struct stm32_rtc *rtc = pinctrl_dev_get_drvdata(pctldev); in stm32_rtc_pinmux_action_lsco() local
327 struct stm32_rtc_registers regs = rtc->data->regs; in stm32_rtc_pinmux_action_lsco()
328 struct device *dev = rtc->rtc_dev->dev.parent; in stm32_rtc_pinmux_action_lsco()
332 if (!rtc->data->has_lsco) in stm32_rtc_pinmux_action_lsco()
341 rtc->clk_lsco = clk_register_gate(dev, "rtc_lsco", __clk_get_name(rtc->rtc_ck), in stm32_rtc_pinmux_action_lsco()
343 rtc->base + regs.cfgr, lscoen, 0, NULL); in stm32_rtc_pinmux_action_lsco()
344 if (IS_ERR(rtc->clk_lsco)) in stm32_rtc_pinmux_action_lsco()
345 return PTR_ERR(rtc->clk_lsco); in stm32_rtc_pinmux_action_lsco()
347 of_clk_add_provider(dev->of_node, of_clk_src_simple_get, rtc->clk_lsco); in stm32_rtc_pinmux_action_lsco()
405 static int stm32_rtc_enter_init_mode(struct stm32_rtc *rtc) in stm32_rtc_enter_init_mode() argument
407 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_enter_init_mode()
408 unsigned int isr = readl_relaxed(rtc->base + regs->isr); in stm32_rtc_enter_init_mode()
412 writel_relaxed(isr, rtc->base + regs->isr); in stm32_rtc_enter_init_mode()
420 return readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr, isr, in stm32_rtc_enter_init_mode()
428 static void stm32_rtc_exit_init_mode(struct stm32_rtc *rtc) in stm32_rtc_exit_init_mode() argument
430 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_exit_init_mode()
431 unsigned int isr = readl_relaxed(rtc->base + regs->isr); in stm32_rtc_exit_init_mode()
434 writel_relaxed(isr, rtc->base + regs->isr); in stm32_rtc_exit_init_mode()
437 static int stm32_rtc_wait_sync(struct stm32_rtc *rtc) in stm32_rtc_wait_sync() argument
439 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_wait_sync()
440 unsigned int isr = readl_relaxed(rtc->base + regs->isr); in stm32_rtc_wait_sync()
443 writel_relaxed(isr, rtc->base + regs->isr); in stm32_rtc_wait_sync()
449 return readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr, in stm32_rtc_wait_sync()
455 static void stm32_rtc_clear_event_flags(struct stm32_rtc *rtc, in stm32_rtc_clear_event_flags() argument
458 rtc->data->clear_events(rtc, flags); in stm32_rtc_clear_event_flags()
463 struct stm32_rtc *rtc = (struct stm32_rtc *)dev_id; in stm32_rtc_alarm_irq() local
464 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_alarm_irq()
465 const struct stm32_rtc_events *evts = &rtc->data->events; in stm32_rtc_alarm_irq()
468 rtc_lock(rtc->rtc_dev); in stm32_rtc_alarm_irq()
470 status = readl_relaxed(rtc->base + regs->sr); in stm32_rtc_alarm_irq()
471 cr = readl_relaxed(rtc->base + regs->cr); in stm32_rtc_alarm_irq()
476 dev_dbg(&rtc->rtc_dev->dev, "Alarm occurred\n"); in stm32_rtc_alarm_irq()
479 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF); in stm32_rtc_alarm_irq()
482 stm32_rtc_clear_event_flags(rtc, evts->alra); in stm32_rtc_alarm_irq()
485 rtc_unlock(rtc->rtc_dev); in stm32_rtc_alarm_irq()
503 * - on rtc side, 0=invalid,1=Monday...7=Sunday in tm2bcd()
521 * - on rtc side, 0=invalid,1=Monday...7=Sunday in bcd2tm()
528 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_read_time() local
529 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_read_time()
533 tr = readl_relaxed(rtc->base + regs->tr); in stm32_rtc_read_time()
534 dr = readl_relaxed(rtc->base + regs->dr); in stm32_rtc_read_time()
554 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_set_time() local
555 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_set_time()
572 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_set_time()
574 ret = stm32_rtc_enter_init_mode(rtc); in stm32_rtc_set_time()
580 writel_relaxed(tr, rtc->base + regs->tr); in stm32_rtc_set_time()
581 writel_relaxed(dr, rtc->base + regs->dr); in stm32_rtc_set_time()
583 stm32_rtc_exit_init_mode(rtc); in stm32_rtc_set_time()
585 ret = stm32_rtc_wait_sync(rtc); in stm32_rtc_set_time()
587 stm32_rtc_wpr_lock(rtc); in stm32_rtc_set_time()
594 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_read_alarm() local
595 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_read_alarm()
596 const struct stm32_rtc_events *evts = &rtc->data->events; in stm32_rtc_read_alarm()
600 alrmar = readl_relaxed(rtc->base + regs->alrmar); in stm32_rtc_read_alarm()
601 cr = readl_relaxed(rtc->base + regs->cr); in stm32_rtc_read_alarm()
602 status = readl_relaxed(rtc->base + regs->sr); in stm32_rtc_read_alarm()
662 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_alarm_irq_enable() local
663 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_alarm_irq_enable()
664 const struct stm32_rtc_events *evts = &rtc->data->events; in stm32_rtc_alarm_irq_enable()
667 cr = readl_relaxed(rtc->base + regs->cr); in stm32_rtc_alarm_irq_enable()
669 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_alarm_irq_enable()
676 writel_relaxed(cr, rtc->base + regs->cr); in stm32_rtc_alarm_irq_enable()
679 stm32_rtc_clear_event_flags(rtc, evts->alra); in stm32_rtc_alarm_irq_enable()
681 stm32_rtc_wpr_lock(rtc); in stm32_rtc_alarm_irq_enable()
696 * RTC alarm can't be set on a specific month and year. in stm32_rtc_valid_alrm()
726 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_set_alarm() local
727 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_set_alarm()
733 * RTC alarm can't be set on a specific date, unless this date is in stm32_rtc_set_alarm()
744 /* tm_year and tm_mon are not used because not supported by RTC */ in stm32_rtc_set_alarm()
756 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_set_alarm()
759 cr = readl_relaxed(rtc->base + regs->cr); in stm32_rtc_set_alarm()
761 writel_relaxed(cr, rtc->base + regs->cr); in stm32_rtc_set_alarm()
767 ret = readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr, in stm32_rtc_set_alarm()
778 writel_relaxed(alrmar, rtc->base + regs->alrmar); in stm32_rtc_set_alarm()
782 stm32_rtc_wpr_lock(rtc); in stm32_rtc_set_alarm()
795 static void stm32_rtc_clear_events(struct stm32_rtc *rtc, in stm32_rtc_clear_events() argument
798 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_clear_events()
801 writel_relaxed(readl_relaxed(rtc->base + regs->isr) & ~flags, in stm32_rtc_clear_events()
802 rtc->base + regs->isr); in stm32_rtc_clear_events()
857 static void stm32mp1_rtc_clear_events(struct stm32_rtc *rtc, in stm32mp1_rtc_clear_events() argument
860 struct stm32_rtc_registers regs = rtc->data->regs; in stm32mp1_rtc_clear_events()
863 writel_relaxed(flags, rtc->base + regs.scr); in stm32mp1_rtc_clear_events()
919 { .compatible = "st,stm32-rtc", .data = &stm32_rtc_data },
920 { .compatible = "st,stm32h7-rtc", .data = &stm32h7_rtc_data },
921 { .compatible = "st,stm32mp1-rtc", .data = &stm32mp1_data },
922 { .compatible = "st,stm32mp25-rtc", .data = &stm32mp25_data },
927 static void stm32_rtc_clean_outs(struct stm32_rtc *rtc) in stm32_rtc_clean_outs() argument
929 struct stm32_rtc_registers regs = rtc->data->regs; in stm32_rtc_clean_outs()
930 unsigned int cr = readl_relaxed(rtc->base + regs.cr); in stm32_rtc_clean_outs()
938 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_clean_outs()
939 writel_relaxed(cr, rtc->base + regs.cr); in stm32_rtc_clean_outs()
940 stm32_rtc_wpr_lock(rtc); in stm32_rtc_clean_outs()
943 unsigned int cfgr = readl_relaxed(rtc->base + regs.cfgr); in stm32_rtc_clean_outs()
947 writel_relaxed(cfgr, rtc->base + regs.cfgr); in stm32_rtc_clean_outs()
957 /* Check if RTC available for our CID */ in stm32_rtc_check_rif()
962 /* Check if RTC available for non secure world */ in stm32_rtc_check_rif()
971 struct stm32_rtc *rtc) in stm32_rtc_init() argument
973 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_init()
978 rate = clk_get_rate(rtc->rtc_ck); in stm32_rtc_init()
989 if (rtc->data->need_accuracy) { in stm32_rtc_init()
1006 * Can't find a 1Hz, so give priority to RTC power consumption in stm32_rtc_init()
1018 cr = readl_relaxed(rtc->base + regs->cr); in stm32_rtc_init()
1020 prer = readl_relaxed(rtc->base + regs->prer); in stm32_rtc_init()
1032 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_init()
1034 ret = stm32_rtc_enter_init_mode(rtc); in stm32_rtc_init()
1041 writel_relaxed(pred_s, rtc->base + regs->prer); in stm32_rtc_init()
1042 writel_relaxed(pred_a | pred_s, rtc->base + regs->prer); in stm32_rtc_init()
1046 writel_relaxed(cr, rtc->base + regs->cr); in stm32_rtc_init()
1048 stm32_rtc_exit_init_mode(rtc); in stm32_rtc_init()
1050 ret = stm32_rtc_wait_sync(rtc); in stm32_rtc_init()
1052 stm32_rtc_wpr_lock(rtc); in stm32_rtc_init()
1059 struct stm32_rtc *rtc; in stm32_rtc_probe() local
1064 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); in stm32_rtc_probe()
1065 if (!rtc) in stm32_rtc_probe()
1068 rtc->base = devm_platform_ioremap_resource(pdev, 0); in stm32_rtc_probe()
1069 if (IS_ERR(rtc->base)) in stm32_rtc_probe()
1070 return PTR_ERR(rtc->base); in stm32_rtc_probe()
1072 rtc->data = (struct stm32_rtc_data *) in stm32_rtc_probe()
1074 regs = &rtc->data->regs; in stm32_rtc_probe()
1076 if (rtc->data->need_dbp) { in stm32_rtc_probe()
1077 rtc->dbp = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, in stm32_rtc_probe()
1079 if (IS_ERR(rtc->dbp)) { in stm32_rtc_probe()
1081 return PTR_ERR(rtc->dbp); in stm32_rtc_probe()
1085 1, &rtc->dbp_reg); in stm32_rtc_probe()
1092 2, &rtc->dbp_mask); in stm32_rtc_probe()
1099 if (!rtc->data->has_pclk) { in stm32_rtc_probe()
1100 rtc->pclk = NULL; in stm32_rtc_probe()
1101 rtc->rtc_ck = devm_clk_get(&pdev->dev, NULL); in stm32_rtc_probe()
1103 rtc->pclk = devm_clk_get(&pdev->dev, "pclk"); in stm32_rtc_probe()
1104 if (IS_ERR(rtc->pclk)) in stm32_rtc_probe()
1105 return dev_err_probe(&pdev->dev, PTR_ERR(rtc->pclk), "no pclk clock"); in stm32_rtc_probe()
1107 rtc->rtc_ck = devm_clk_get(&pdev->dev, "rtc_ck"); in stm32_rtc_probe()
1109 if (IS_ERR(rtc->rtc_ck)) in stm32_rtc_probe()
1110 return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_ck), "no rtc_ck clock"); in stm32_rtc_probe()
1112 if (rtc->data->has_pclk) { in stm32_rtc_probe()
1113 ret = clk_prepare_enable(rtc->pclk); in stm32_rtc_probe()
1118 ret = clk_prepare_enable(rtc->rtc_ck); in stm32_rtc_probe()
1122 if (rtc->data->need_dbp) in stm32_rtc_probe()
1123 regmap_update_bits(rtc->dbp, rtc->dbp_reg, in stm32_rtc_probe()
1124 rtc->dbp_mask, rtc->dbp_mask); in stm32_rtc_probe()
1126 if (rtc->data->rif_protected) { in stm32_rtc_probe()
1127 ret = stm32_rtc_check_rif(rtc, STM32_RTC_RES_INIT); in stm32_rtc_probe()
1129 ret = stm32_rtc_check_rif(rtc, STM32_RTC_RES_ALRA); in stm32_rtc_probe()
1131 dev_err(&pdev->dev, "Failed to probe RTC due to RIF configuration\n"); in stm32_rtc_probe()
1140 * rtc_ck parent clock has changed (so RTC prescalers need to be in stm32_rtc_probe()
1141 * changed). That's why we cannot rely on this flag to know if RTC in stm32_rtc_probe()
1144 ret = stm32_rtc_init(pdev, rtc); in stm32_rtc_probe()
1148 rtc->irq_alarm = platform_get_irq(pdev, 0); in stm32_rtc_probe()
1149 if (rtc->irq_alarm <= 0) { in stm32_rtc_probe()
1150 ret = rtc->irq_alarm; in stm32_rtc_probe()
1158 ret = dev_pm_set_wake_irq(&pdev->dev, rtc->irq_alarm); in stm32_rtc_probe()
1162 platform_set_drvdata(pdev, rtc); in stm32_rtc_probe()
1164 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name, in stm32_rtc_probe()
1166 if (IS_ERR(rtc->rtc_dev)) { in stm32_rtc_probe()
1167 ret = PTR_ERR(rtc->rtc_dev); in stm32_rtc_probe()
1168 dev_err(&pdev->dev, "rtc device registration failed, err=%d\n", in stm32_rtc_probe()
1173 /* Handle RTC alarm interrupts */ in stm32_rtc_probe()
1174 ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_alarm, NULL, in stm32_rtc_probe()
1176 pdev->name, rtc); in stm32_rtc_probe()
1179 rtc->irq_alarm); in stm32_rtc_probe()
1183 stm32_rtc_clean_outs(rtc); in stm32_rtc_probe()
1185 ret = devm_pinctrl_register_and_init(&pdev->dev, &stm32_rtc_pdesc, rtc, &pctl); in stm32_rtc_probe()
1197 if (!(readl_relaxed(rtc->base + regs->isr) & STM32_RTC_ISR_INITS)) in stm32_rtc_probe()
1201 u32 ver = readl_relaxed(rtc->base + regs->verr); in stm32_rtc_probe()
1211 clk_disable_unprepare(rtc->rtc_ck); in stm32_rtc_probe()
1213 if (rtc->data->has_pclk) in stm32_rtc_probe()
1214 clk_disable_unprepare(rtc->pclk); in stm32_rtc_probe()
1216 if (rtc->data->need_dbp) in stm32_rtc_probe()
1217 regmap_update_bits(rtc->dbp, rtc->dbp_reg, rtc->dbp_mask, 0); in stm32_rtc_probe()
1227 struct stm32_rtc *rtc = platform_get_drvdata(pdev); in stm32_rtc_remove() local
1228 const struct stm32_rtc_registers *regs = &rtc->data->regs; in stm32_rtc_remove()
1231 if (!IS_ERR_OR_NULL(rtc->clk_lsco)) in stm32_rtc_remove()
1232 clk_unregister_gate(rtc->clk_lsco); in stm32_rtc_remove()
1235 stm32_rtc_wpr_unlock(rtc); in stm32_rtc_remove()
1236 cr = readl_relaxed(rtc->base + regs->cr); in stm32_rtc_remove()
1238 writel_relaxed(cr, rtc->base + regs->cr); in stm32_rtc_remove()
1239 stm32_rtc_wpr_lock(rtc); in stm32_rtc_remove()
1241 clk_disable_unprepare(rtc->rtc_ck); in stm32_rtc_remove()
1242 if (rtc->data->has_pclk) in stm32_rtc_remove()
1243 clk_disable_unprepare(rtc->pclk); in stm32_rtc_remove()
1246 if (rtc->data->need_dbp) in stm32_rtc_remove()
1247 regmap_update_bits(rtc->dbp, rtc->dbp_reg, rtc->dbp_mask, 0); in stm32_rtc_remove()
1255 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_suspend() local
1257 if (rtc->data->has_pclk) in stm32_rtc_suspend()
1258 clk_disable_unprepare(rtc->pclk); in stm32_rtc_suspend()
1265 struct stm32_rtc *rtc = dev_get_drvdata(dev); in stm32_rtc_resume() local
1268 if (rtc->data->has_pclk) { in stm32_rtc_resume()
1269 ret = clk_prepare_enable(rtc->pclk); in stm32_rtc_resume()
1274 ret = stm32_rtc_wait_sync(rtc); in stm32_rtc_resume()
1276 if (rtc->data->has_pclk) in stm32_rtc_resume()
1277 clk_disable_unprepare(rtc->pclk); in stm32_rtc_resume()