hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/rtc/rtc-mxc.c
....@@ -8,6 +8,7 @@
88 #include <linux/slab.h>
99 #include <linux/interrupt.h>
1010 #include <linux/platform_device.h>
11
+#include <linux/pm_wakeirq.h>
1112 #include <linux/clk.h>
1213 #include <linux/of.h>
1314 #include <linux/of_device.h>
....@@ -183,8 +184,9 @@
183184 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
184185 void __iomem *ioaddr = pdata->ioaddr;
185186 u32 reg;
187
+ unsigned long flags;
186188
187
- spin_lock_irq(&pdata->rtc->irq_lock);
189
+ spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
188190 reg = readw(ioaddr + RTC_RTCIENR);
189191
190192 if (enabled)
....@@ -193,7 +195,7 @@
193195 reg &= ~bit;
194196
195197 writew(reg, ioaddr + RTC_RTCIENR);
196
- spin_unlock_irq(&pdata->rtc->irq_lock);
198
+ spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags);
197199 }
198200
199201 /* This function is the RTC interrupt service routine. */
....@@ -253,20 +255,9 @@
253255 /*
254256 * This function sets the internal RTC time based on tm in Gregorian date.
255257 */
256
-static int mxc_rtc_set_mmss(struct device *dev, time64_t time)
258
+static int mxc_rtc_set_time(struct device *dev, struct rtc_time *tm)
257259 {
258
- struct rtc_plat_data *pdata = dev_get_drvdata(dev);
259
-
260
- /*
261
- * TTC_DAYR register is 9-bit in MX1 SoC, save time and day of year only
262
- */
263
- if (is_imx1_rtc(pdata)) {
264
- struct rtc_time tm;
265
-
266
- rtc_time64_to_tm(time, &tm);
267
- tm.tm_year = 70;
268
- time = rtc_tm_to_time64(&tm);
269
- }
260
+ time64_t time = rtc_tm_to_time64(tm);
270261
271262 /* Avoid roll-over from reading the different registers */
272263 do {
....@@ -310,15 +301,22 @@
310301 /* RTC layer */
311302 static const struct rtc_class_ops mxc_rtc_ops = {
312303 .read_time = mxc_rtc_read_time,
313
- .set_mmss64 = mxc_rtc_set_mmss,
304
+ .set_time = mxc_rtc_set_time,
314305 .read_alarm = mxc_rtc_read_alarm,
315306 .set_alarm = mxc_rtc_set_alarm,
316307 .alarm_irq_enable = mxc_rtc_alarm_irq_enable,
317308 };
318309
310
+static void mxc_rtc_action(void *p)
311
+{
312
+ struct rtc_plat_data *pdata = p;
313
+
314
+ clk_disable_unprepare(pdata->clk_ref);
315
+ clk_disable_unprepare(pdata->clk_ipg);
316
+}
317
+
319318 static int mxc_rtc_probe(struct platform_device *pdev)
320319 {
321
- struct resource *res;
322320 struct rtc_device *rtc;
323321 struct rtc_plat_data *pdata = NULL;
324322 u32 reg;
....@@ -336,10 +334,33 @@
336334 else
337335 pdata->devtype = pdev->id_entry->driver_data;
338336
339
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
340
- pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
337
+ pdata->ioaddr = devm_platform_ioremap_resource(pdev, 0);
341338 if (IS_ERR(pdata->ioaddr))
342339 return PTR_ERR(pdata->ioaddr);
340
+
341
+ rtc = devm_rtc_allocate_device(&pdev->dev);
342
+ if (IS_ERR(rtc))
343
+ return PTR_ERR(rtc);
344
+
345
+ pdata->rtc = rtc;
346
+ rtc->ops = &mxc_rtc_ops;
347
+ if (is_imx1_rtc(pdata)) {
348
+ struct rtc_time tm;
349
+
350
+ /* 9bit days + hours minutes seconds */
351
+ rtc->range_max = (1 << 9) * 86400 - 1;
352
+
353
+ /*
354
+ * Set the start date as beginning of the current year. This can
355
+ * be overridden using device tree.
356
+ */
357
+ rtc_time64_to_tm(ktime_get_real_seconds(), &tm);
358
+ rtc->start_secs = mktime64(tm.tm_year, 1, 1, 0, 0, 0);
359
+ rtc->set_start_time = true;
360
+ } else {
361
+ /* 16bit days + hours minutes seconds */
362
+ rtc->range_max = (1 << 16) * 86400ULL - 1;
363
+ }
343364
344365 pdata->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
345366 if (IS_ERR(pdata->clk_ipg)) {
....@@ -353,14 +374,20 @@
353374
354375 pdata->clk_ref = devm_clk_get(&pdev->dev, "ref");
355376 if (IS_ERR(pdata->clk_ref)) {
377
+ clk_disable_unprepare(pdata->clk_ipg);
356378 dev_err(&pdev->dev, "unable to get ref clock!\n");
357
- ret = PTR_ERR(pdata->clk_ref);
358
- goto exit_put_clk_ipg;
379
+ return PTR_ERR(pdata->clk_ref);
359380 }
360381
361382 ret = clk_prepare_enable(pdata->clk_ref);
383
+ if (ret) {
384
+ clk_disable_unprepare(pdata->clk_ipg);
385
+ return ret;
386
+ }
387
+
388
+ ret = devm_add_action_or_reset(&pdev->dev, mxc_rtc_action, pdata);
362389 if (ret)
363
- goto exit_put_clk_ipg;
390
+ return ret;
364391
365392 rate = clk_get_rate(pdata->clk_ref);
366393
....@@ -372,16 +399,14 @@
372399 reg = RTC_INPUT_CLK_38400HZ;
373400 else {
374401 dev_err(&pdev->dev, "rtc clock is not valid (%lu)\n", rate);
375
- ret = -EINVAL;
376
- goto exit_put_clk_ref;
402
+ return -EINVAL;
377403 }
378404
379405 reg |= RTC_ENABLE_BIT;
380406 writew(reg, (pdata->ioaddr + RTC_RTCCTL));
381407 if (((readw(pdata->ioaddr + RTC_RTCCTL)) & RTC_ENABLE_BIT) == 0) {
382408 dev_err(&pdev->dev, "hardware module can't be enabled!\n");
383
- ret = -EIO;
384
- goto exit_put_clk_ref;
409
+ return -EIO;
385410 }
386411
387412 platform_set_drvdata(pdev, pdata);
....@@ -396,71 +421,25 @@
396421 pdata->irq = -1;
397422 }
398423
399
- if (pdata->irq >= 0)
424
+ if (pdata->irq >= 0) {
400425 device_init_wakeup(&pdev->dev, 1);
401
-
402
- rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops,
403
- THIS_MODULE);
404
- if (IS_ERR(rtc)) {
405
- ret = PTR_ERR(rtc);
406
- goto exit_put_clk_ref;
426
+ ret = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
427
+ if (ret)
428
+ dev_err(&pdev->dev, "failed to enable irq wake\n");
407429 }
408430
409
- pdata->rtc = rtc;
410
-
411
- return 0;
412
-
413
-exit_put_clk_ref:
414
- clk_disable_unprepare(pdata->clk_ref);
415
-exit_put_clk_ipg:
416
- clk_disable_unprepare(pdata->clk_ipg);
431
+ ret = rtc_register_device(rtc);
417432
418433 return ret;
419434 }
420
-
421
-static int mxc_rtc_remove(struct platform_device *pdev)
422
-{
423
- struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
424
-
425
- clk_disable_unprepare(pdata->clk_ref);
426
- clk_disable_unprepare(pdata->clk_ipg);
427
-
428
- return 0;
429
-}
430
-
431
-#ifdef CONFIG_PM_SLEEP
432
-static int mxc_rtc_suspend(struct device *dev)
433
-{
434
- struct rtc_plat_data *pdata = dev_get_drvdata(dev);
435
-
436
- if (device_may_wakeup(dev))
437
- enable_irq_wake(pdata->irq);
438
-
439
- return 0;
440
-}
441
-
442
-static int mxc_rtc_resume(struct device *dev)
443
-{
444
- struct rtc_plat_data *pdata = dev_get_drvdata(dev);
445
-
446
- if (device_may_wakeup(dev))
447
- disable_irq_wake(pdata->irq);
448
-
449
- return 0;
450
-}
451
-#endif
452
-
453
-static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume);
454435
455436 static struct platform_driver mxc_rtc_driver = {
456437 .driver = {
457438 .name = "mxc_rtc",
458439 .of_match_table = of_match_ptr(imx_rtc_dt_ids),
459
- .pm = &mxc_rtc_pm_ops,
460440 },
461441 .id_table = imx_rtc_devtype,
462442 .probe = mxc_rtc_probe,
463
- .remove = mxc_rtc_remove,
464443 };
465444
466445 module_platform_driver(mxc_rtc_driver)