rtc-max8907.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * RTC driver for Maxim MAX8907
  3. *
  4. * Copyright (c) 2011-2012, NVIDIA Corporation.
  5. *
  6. * Based on drivers/rtc/rtc-max8925.c,
  7. * Copyright (C) 2009-2010 Marvell International Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/bcd.h>
  14. #include <linux/i2c.h>
  15. #include <linux/mfd/max8907.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/rtc.h>
  20. #include <linux/slab.h>
  21. enum {
  22. RTC_SEC = 0,
  23. RTC_MIN,
  24. RTC_HOUR,
  25. RTC_WEEKDAY,
  26. RTC_DATE,
  27. RTC_MONTH,
  28. RTC_YEAR1,
  29. RTC_YEAR2,
  30. };
  31. #define TIME_NUM 8
  32. #define ALARM_1SEC (1 << 7)
  33. #define HOUR_12 (1 << 7)
  34. #define HOUR_AM_PM (1 << 5)
  35. #define ALARM0_IRQ (1 << 3)
  36. #define ALARM1_IRQ (1 << 2)
  37. #define ALARM0_STATUS (1 << 2)
  38. #define ALARM1_STATUS (1 << 1)
  39. struct max8907_rtc {
  40. struct max8907 *max8907;
  41. struct regmap *regmap;
  42. struct rtc_device *rtc_dev;
  43. int irq;
  44. };
  45. static irqreturn_t max8907_irq_handler(int irq, void *data)
  46. {
  47. struct max8907_rtc *rtc = data;
  48. regmap_write(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0);
  49. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  50. return IRQ_HANDLED;
  51. }
  52. static void regs_to_tm(u8 *regs, struct rtc_time *tm)
  53. {
  54. tm->tm_year = bcd2bin(regs[RTC_YEAR2]) * 100 +
  55. bcd2bin(regs[RTC_YEAR1]) - 1900;
  56. tm->tm_mon = bcd2bin(regs[RTC_MONTH] & 0x1f) - 1;
  57. tm->tm_mday = bcd2bin(regs[RTC_DATE] & 0x3f);
  58. tm->tm_wday = (regs[RTC_WEEKDAY] & 0x07);
  59. if (regs[RTC_HOUR] & HOUR_12) {
  60. tm->tm_hour = bcd2bin(regs[RTC_HOUR] & 0x01f);
  61. if (tm->tm_hour == 12)
  62. tm->tm_hour = 0;
  63. if (regs[RTC_HOUR] & HOUR_AM_PM)
  64. tm->tm_hour += 12;
  65. } else {
  66. tm->tm_hour = bcd2bin(regs[RTC_HOUR] & 0x03f);
  67. }
  68. tm->tm_min = bcd2bin(regs[RTC_MIN] & 0x7f);
  69. tm->tm_sec = bcd2bin(regs[RTC_SEC] & 0x7f);
  70. }
  71. static void tm_to_regs(struct rtc_time *tm, u8 *regs)
  72. {
  73. u8 high, low;
  74. high = (tm->tm_year + 1900) / 100;
  75. low = tm->tm_year % 100;
  76. regs[RTC_YEAR2] = bin2bcd(high);
  77. regs[RTC_YEAR1] = bin2bcd(low);
  78. regs[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
  79. regs[RTC_DATE] = bin2bcd(tm->tm_mday);
  80. regs[RTC_WEEKDAY] = tm->tm_wday;
  81. regs[RTC_HOUR] = bin2bcd(tm->tm_hour);
  82. regs[RTC_MIN] = bin2bcd(tm->tm_min);
  83. regs[RTC_SEC] = bin2bcd(tm->tm_sec);
  84. }
  85. static int max8907_rtc_read_time(struct device *dev, struct rtc_time *tm)
  86. {
  87. struct max8907_rtc *rtc = dev_get_drvdata(dev);
  88. u8 regs[TIME_NUM];
  89. int ret;
  90. ret = regmap_bulk_read(rtc->regmap, MAX8907_REG_RTC_SEC, regs,
  91. TIME_NUM);
  92. if (ret < 0)
  93. return ret;
  94. regs_to_tm(regs, tm);
  95. return 0;
  96. }
  97. static int max8907_rtc_set_time(struct device *dev, struct rtc_time *tm)
  98. {
  99. struct max8907_rtc *rtc = dev_get_drvdata(dev);
  100. u8 regs[TIME_NUM];
  101. tm_to_regs(tm, regs);
  102. return regmap_bulk_write(rtc->regmap, MAX8907_REG_RTC_SEC, regs,
  103. TIME_NUM);
  104. }
  105. static int max8907_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  106. {
  107. struct max8907_rtc *rtc = dev_get_drvdata(dev);
  108. u8 regs[TIME_NUM];
  109. unsigned int val;
  110. int ret;
  111. ret = regmap_bulk_read(rtc->regmap, MAX8907_REG_ALARM0_SEC, regs,
  112. TIME_NUM);
  113. if (ret < 0)
  114. return ret;
  115. regs_to_tm(regs, &alrm->time);
  116. ret = regmap_read(rtc->regmap, MAX8907_REG_ALARM0_CNTL, &val);
  117. if (ret < 0)
  118. return ret;
  119. alrm->enabled = !!(val & 0x7f);
  120. return 0;
  121. }
  122. static int max8907_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  123. {
  124. struct max8907_rtc *rtc = dev_get_drvdata(dev);
  125. u8 regs[TIME_NUM];
  126. int ret;
  127. tm_to_regs(&alrm->time, regs);
  128. /* Disable alarm while we update the target time */
  129. ret = regmap_write(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0);
  130. if (ret < 0)
  131. return ret;
  132. ret = regmap_bulk_write(rtc->regmap, MAX8907_REG_ALARM0_SEC, regs,
  133. TIME_NUM);
  134. if (ret < 0)
  135. return ret;
  136. if (alrm->enabled)
  137. ret = regmap_write(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0x77);
  138. return ret;
  139. }
  140. static const struct rtc_class_ops max8907_rtc_ops = {
  141. .read_time = max8907_rtc_read_time,
  142. .set_time = max8907_rtc_set_time,
  143. .read_alarm = max8907_rtc_read_alarm,
  144. .set_alarm = max8907_rtc_set_alarm,
  145. };
  146. static int max8907_rtc_probe(struct platform_device *pdev)
  147. {
  148. struct max8907 *max8907 = dev_get_drvdata(pdev->dev.parent);
  149. struct max8907_rtc *rtc;
  150. int ret;
  151. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  152. if (!rtc)
  153. return -ENOMEM;
  154. platform_set_drvdata(pdev, rtc);
  155. rtc->max8907 = max8907;
  156. rtc->regmap = max8907->regmap_rtc;
  157. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8907-rtc",
  158. &max8907_rtc_ops, THIS_MODULE);
  159. if (IS_ERR(rtc->rtc_dev)) {
  160. ret = PTR_ERR(rtc->rtc_dev);
  161. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  162. return ret;
  163. }
  164. rtc->irq = regmap_irq_get_virq(max8907->irqc_rtc,
  165. MAX8907_IRQ_RTC_ALARM0);
  166. if (rtc->irq < 0)
  167. return rtc->irq;
  168. ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
  169. max8907_irq_handler,
  170. IRQF_ONESHOT, "max8907-alarm0", rtc);
  171. if (ret < 0)
  172. dev_err(&pdev->dev, "Failed to request IRQ%d: %d\n",
  173. rtc->irq, ret);
  174. return ret;
  175. }
  176. static struct platform_driver max8907_rtc_driver = {
  177. .driver = {
  178. .name = "max8907-rtc",
  179. },
  180. .probe = max8907_rtc_probe,
  181. };
  182. module_platform_driver(max8907_rtc_driver);
  183. MODULE_DESCRIPTION("Maxim MAX8907 RTC driver");
  184. MODULE_LICENSE("GPL v2");