rtc-bq4802.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* rtc-bq4802.c: TI BQ4802 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/io.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/rtc.h>
  11. #include <linux/bcd.h>
  12. #include <linux/slab.h>
  13. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  14. MODULE_DESCRIPTION("TI BQ4802 RTC driver");
  15. MODULE_LICENSE("GPL");
  16. struct bq4802 {
  17. void __iomem *regs;
  18. unsigned long ioport;
  19. struct rtc_device *rtc;
  20. spinlock_t lock;
  21. struct resource *r;
  22. u8 (*read)(struct bq4802 *, int);
  23. void (*write)(struct bq4802 *, int, u8);
  24. };
  25. static u8 bq4802_read_io(struct bq4802 *p, int off)
  26. {
  27. return inb(p->ioport + off);
  28. }
  29. static void bq4802_write_io(struct bq4802 *p, int off, u8 val)
  30. {
  31. outb(val, p->ioport + off);
  32. }
  33. static u8 bq4802_read_mem(struct bq4802 *p, int off)
  34. {
  35. return readb(p->regs + off);
  36. }
  37. static void bq4802_write_mem(struct bq4802 *p, int off, u8 val)
  38. {
  39. writeb(val, p->regs + off);
  40. }
  41. static int bq4802_read_time(struct device *dev, struct rtc_time *tm)
  42. {
  43. struct platform_device *pdev = to_platform_device(dev);
  44. struct bq4802 *p = platform_get_drvdata(pdev);
  45. unsigned long flags;
  46. unsigned int century;
  47. u8 val;
  48. spin_lock_irqsave(&p->lock, flags);
  49. val = p->read(p, 0x0e);
  50. p->write(p, 0xe, val | 0x08);
  51. tm->tm_sec = p->read(p, 0x00);
  52. tm->tm_min = p->read(p, 0x02);
  53. tm->tm_hour = p->read(p, 0x04);
  54. tm->tm_mday = p->read(p, 0x06);
  55. tm->tm_mon = p->read(p, 0x09);
  56. tm->tm_year = p->read(p, 0x0a);
  57. tm->tm_wday = p->read(p, 0x08);
  58. century = p->read(p, 0x0f);
  59. p->write(p, 0x0e, val);
  60. spin_unlock_irqrestore(&p->lock, flags);
  61. tm->tm_sec = bcd2bin(tm->tm_sec);
  62. tm->tm_min = bcd2bin(tm->tm_min);
  63. tm->tm_hour = bcd2bin(tm->tm_hour);
  64. tm->tm_mday = bcd2bin(tm->tm_mday);
  65. tm->tm_mon = bcd2bin(tm->tm_mon);
  66. tm->tm_year = bcd2bin(tm->tm_year);
  67. tm->tm_wday = bcd2bin(tm->tm_wday);
  68. century = bcd2bin(century);
  69. tm->tm_year += (century * 100);
  70. tm->tm_year -= 1900;
  71. tm->tm_mon--;
  72. return 0;
  73. }
  74. static int bq4802_set_time(struct device *dev, struct rtc_time *tm)
  75. {
  76. struct platform_device *pdev = to_platform_device(dev);
  77. struct bq4802 *p = platform_get_drvdata(pdev);
  78. u8 sec, min, hrs, day, mon, yrs, century, val;
  79. unsigned long flags;
  80. unsigned int year;
  81. year = tm->tm_year + 1900;
  82. century = year / 100;
  83. yrs = year % 100;
  84. mon = tm->tm_mon + 1; /* tm_mon starts at zero */
  85. day = tm->tm_mday;
  86. hrs = tm->tm_hour;
  87. min = tm->tm_min;
  88. sec = tm->tm_sec;
  89. sec = bin2bcd(sec);
  90. min = bin2bcd(min);
  91. hrs = bin2bcd(hrs);
  92. day = bin2bcd(day);
  93. mon = bin2bcd(mon);
  94. yrs = bin2bcd(yrs);
  95. century = bin2bcd(century);
  96. spin_lock_irqsave(&p->lock, flags);
  97. val = p->read(p, 0x0e);
  98. p->write(p, 0x0e, val | 0x08);
  99. p->write(p, 0x00, sec);
  100. p->write(p, 0x02, min);
  101. p->write(p, 0x04, hrs);
  102. p->write(p, 0x06, day);
  103. p->write(p, 0x09, mon);
  104. p->write(p, 0x0a, yrs);
  105. p->write(p, 0x0f, century);
  106. p->write(p, 0x0e, val);
  107. spin_unlock_irqrestore(&p->lock, flags);
  108. return 0;
  109. }
  110. static const struct rtc_class_ops bq4802_ops = {
  111. .read_time = bq4802_read_time,
  112. .set_time = bq4802_set_time,
  113. };
  114. static int bq4802_probe(struct platform_device *pdev)
  115. {
  116. struct bq4802 *p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  117. int err = -ENOMEM;
  118. if (!p)
  119. goto out;
  120. spin_lock_init(&p->lock);
  121. p->r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  122. if (!p->r) {
  123. p->r = platform_get_resource(pdev, IORESOURCE_IO, 0);
  124. err = -EINVAL;
  125. if (!p->r)
  126. goto out;
  127. }
  128. if (p->r->flags & IORESOURCE_IO) {
  129. p->ioport = p->r->start;
  130. p->read = bq4802_read_io;
  131. p->write = bq4802_write_io;
  132. } else if (p->r->flags & IORESOURCE_MEM) {
  133. p->regs = devm_ioremap(&pdev->dev, p->r->start,
  134. resource_size(p->r));
  135. p->read = bq4802_read_mem;
  136. p->write = bq4802_write_mem;
  137. } else {
  138. err = -EINVAL;
  139. goto out;
  140. }
  141. platform_set_drvdata(pdev, p);
  142. p->rtc = devm_rtc_device_register(&pdev->dev, "bq4802",
  143. &bq4802_ops, THIS_MODULE);
  144. if (IS_ERR(p->rtc)) {
  145. err = PTR_ERR(p->rtc);
  146. goto out;
  147. }
  148. err = 0;
  149. out:
  150. return err;
  151. }
  152. /* work with hotplug and coldplug */
  153. MODULE_ALIAS("platform:rtc-bq4802");
  154. static struct platform_driver bq4802_driver = {
  155. .driver = {
  156. .name = "rtc-bq4802",
  157. },
  158. .probe = bq4802_probe,
  159. };
  160. module_platform_driver(bq4802_driver);