rtc-bq4802.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 bq4802 *p = dev_get_drvdata(dev);
  44. unsigned long flags;
  45. unsigned int century;
  46. u8 val;
  47. spin_lock_irqsave(&p->lock, flags);
  48. val = p->read(p, 0x0e);
  49. p->write(p, 0xe, val | 0x08);
  50. tm->tm_sec = p->read(p, 0x00);
  51. tm->tm_min = p->read(p, 0x02);
  52. tm->tm_hour = p->read(p, 0x04);
  53. tm->tm_mday = p->read(p, 0x06);
  54. tm->tm_mon = p->read(p, 0x09);
  55. tm->tm_year = p->read(p, 0x0a);
  56. tm->tm_wday = p->read(p, 0x08);
  57. century = p->read(p, 0x0f);
  58. p->write(p, 0x0e, val);
  59. spin_unlock_irqrestore(&p->lock, flags);
  60. tm->tm_sec = bcd2bin(tm->tm_sec);
  61. tm->tm_min = bcd2bin(tm->tm_min);
  62. tm->tm_hour = bcd2bin(tm->tm_hour);
  63. tm->tm_mday = bcd2bin(tm->tm_mday);
  64. tm->tm_mon = bcd2bin(tm->tm_mon);
  65. tm->tm_year = bcd2bin(tm->tm_year);
  66. tm->tm_wday = bcd2bin(tm->tm_wday);
  67. century = bcd2bin(century);
  68. tm->tm_year += (century * 100);
  69. tm->tm_year -= 1900;
  70. tm->tm_mon--;
  71. return 0;
  72. }
  73. static int bq4802_set_time(struct device *dev, struct rtc_time *tm)
  74. {
  75. struct bq4802 *p = dev_get_drvdata(dev);
  76. u8 sec, min, hrs, day, mon, yrs, century, val;
  77. unsigned long flags;
  78. unsigned int year;
  79. year = tm->tm_year + 1900;
  80. century = year / 100;
  81. yrs = year % 100;
  82. mon = tm->tm_mon + 1; /* tm_mon starts at zero */
  83. day = tm->tm_mday;
  84. hrs = tm->tm_hour;
  85. min = tm->tm_min;
  86. sec = tm->tm_sec;
  87. sec = bin2bcd(sec);
  88. min = bin2bcd(min);
  89. hrs = bin2bcd(hrs);
  90. day = bin2bcd(day);
  91. mon = bin2bcd(mon);
  92. yrs = bin2bcd(yrs);
  93. century = bin2bcd(century);
  94. spin_lock_irqsave(&p->lock, flags);
  95. val = p->read(p, 0x0e);
  96. p->write(p, 0x0e, val | 0x08);
  97. p->write(p, 0x00, sec);
  98. p->write(p, 0x02, min);
  99. p->write(p, 0x04, hrs);
  100. p->write(p, 0x06, day);
  101. p->write(p, 0x09, mon);
  102. p->write(p, 0x0a, yrs);
  103. p->write(p, 0x0f, century);
  104. p->write(p, 0x0e, val);
  105. spin_unlock_irqrestore(&p->lock, flags);
  106. return 0;
  107. }
  108. static const struct rtc_class_ops bq4802_ops = {
  109. .read_time = bq4802_read_time,
  110. .set_time = bq4802_set_time,
  111. };
  112. static int bq4802_probe(struct platform_device *pdev)
  113. {
  114. struct bq4802 *p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  115. int err = -ENOMEM;
  116. if (!p)
  117. goto out;
  118. spin_lock_init(&p->lock);
  119. p->r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  120. if (!p->r) {
  121. p->r = platform_get_resource(pdev, IORESOURCE_IO, 0);
  122. err = -EINVAL;
  123. if (!p->r)
  124. goto out;
  125. }
  126. if (p->r->flags & IORESOURCE_IO) {
  127. p->ioport = p->r->start;
  128. p->read = bq4802_read_io;
  129. p->write = bq4802_write_io;
  130. } else if (p->r->flags & IORESOURCE_MEM) {
  131. p->regs = devm_ioremap(&pdev->dev, p->r->start,
  132. resource_size(p->r));
  133. if (!p->regs){
  134. err = -ENOMEM;
  135. goto out;
  136. }
  137. p->read = bq4802_read_mem;
  138. p->write = bq4802_write_mem;
  139. } else {
  140. err = -EINVAL;
  141. goto out;
  142. }
  143. platform_set_drvdata(pdev, p);
  144. p->rtc = devm_rtc_device_register(&pdev->dev, "bq4802",
  145. &bq4802_ops, THIS_MODULE);
  146. if (IS_ERR(p->rtc)) {
  147. err = PTR_ERR(p->rtc);
  148. goto out;
  149. }
  150. err = 0;
  151. out:
  152. return err;
  153. }
  154. /* work with hotplug and coldplug */
  155. MODULE_ALIAS("platform:rtc-bq4802");
  156. static struct platform_driver bq4802_driver = {
  157. .driver = {
  158. .name = "rtc-bq4802",
  159. },
  160. .probe = bq4802_probe,
  161. };
  162. module_platform_driver(bq4802_driver);