rtc-starfire.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* rtc-starfire.c: Starfire platform RTC driver.
  2. *
  3. * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/time.h>
  9. #include <linux/rtc.h>
  10. #include <linux/platform_device.h>
  11. #include <asm/oplib.h>
  12. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  13. MODULE_DESCRIPTION("Starfire RTC driver");
  14. MODULE_LICENSE("GPL");
  15. struct starfire_rtc {
  16. struct rtc_device *rtc;
  17. spinlock_t lock;
  18. };
  19. static u32 starfire_get_time(void)
  20. {
  21. static char obp_gettod[32];
  22. static u32 unix_tod;
  23. sprintf(obp_gettod, "h# %08x unix-gettod",
  24. (unsigned int) (long) &unix_tod);
  25. prom_feval(obp_gettod);
  26. return unix_tod;
  27. }
  28. static int starfire_read_time(struct device *dev, struct rtc_time *tm)
  29. {
  30. struct starfire_rtc *p = dev_get_drvdata(dev);
  31. unsigned long flags, secs;
  32. spin_lock_irqsave(&p->lock, flags);
  33. secs = starfire_get_time();
  34. spin_unlock_irqrestore(&p->lock, flags);
  35. rtc_time_to_tm(secs, tm);
  36. return 0;
  37. }
  38. static int starfire_set_time(struct device *dev, struct rtc_time *tm)
  39. {
  40. unsigned long secs;
  41. int err;
  42. err = rtc_tm_to_time(tm, &secs);
  43. if (err)
  44. return err;
  45. /* Do nothing, time is set using the service processor
  46. * console on this platform.
  47. */
  48. return 0;
  49. }
  50. static const struct rtc_class_ops starfire_rtc_ops = {
  51. .read_time = starfire_read_time,
  52. .set_time = starfire_set_time,
  53. };
  54. static int __devinit starfire_rtc_probe(struct platform_device *pdev)
  55. {
  56. struct starfire_rtc *p = kzalloc(sizeof(*p), GFP_KERNEL);
  57. if (!p)
  58. return -ENOMEM;
  59. spin_lock_init(&p->lock);
  60. p->rtc = rtc_device_register("starfire", &pdev->dev,
  61. &starfire_rtc_ops, THIS_MODULE);
  62. if (IS_ERR(p->rtc)) {
  63. int err = PTR_ERR(p->rtc);
  64. kfree(p);
  65. return err;
  66. }
  67. platform_set_drvdata(pdev, p);
  68. return 0;
  69. }
  70. static int __devexit starfire_rtc_remove(struct platform_device *pdev)
  71. {
  72. struct starfire_rtc *p = platform_get_drvdata(pdev);
  73. rtc_device_unregister(p->rtc);
  74. kfree(p);
  75. return 0;
  76. }
  77. static struct platform_driver starfire_rtc_driver = {
  78. .driver = {
  79. .name = "rtc-starfire",
  80. .owner = THIS_MODULE,
  81. },
  82. .probe = starfire_rtc_probe,
  83. .remove = __devexit_p(starfire_rtc_remove),
  84. };
  85. static int __init starfire_rtc_init(void)
  86. {
  87. return platform_driver_register(&starfire_rtc_driver);
  88. }
  89. static void __exit starfire_rtc_exit(void)
  90. {
  91. platform_driver_unregister(&starfire_rtc_driver);
  92. }
  93. module_init(starfire_rtc_init);
  94. module_exit(starfire_rtc_exit);