rtc-max8907.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_update_bits(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0x7f, 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) - 1;
  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 + 1;
  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_update_bits(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0x7f, 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_update_bits(rtc->regmap, MAX8907_REG_ALARM0_CNTL,
  138. 0x7f, 0x7f);
  139. return ret;
  140. }
  141. static const struct rtc_class_ops max8907_rtc_ops = {
  142. .read_time = max8907_rtc_read_time,
  143. .set_time = max8907_rtc_set_time,
  144. .read_alarm = max8907_rtc_read_alarm,
  145. .set_alarm = max8907_rtc_set_alarm,
  146. };
  147. static int __devinit max8907_rtc_probe(struct platform_device *pdev)
  148. {
  149. struct max8907 *max8907 = dev_get_drvdata(pdev->dev.parent);
  150. struct max8907_rtc *rtc;
  151. int ret;
  152. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  153. if (!rtc)
  154. return -ENOMEM;
  155. platform_set_drvdata(pdev, rtc);
  156. rtc->max8907 = max8907;
  157. rtc->regmap = max8907->regmap_rtc;
  158. rtc->rtc_dev = rtc_device_register("max8907-rtc", &pdev->dev,
  159. &max8907_rtc_ops, THIS_MODULE);
  160. if (IS_ERR(rtc->rtc_dev)) {
  161. ret = PTR_ERR(rtc->rtc_dev);
  162. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  163. return ret;
  164. }
  165. rtc->irq = regmap_irq_get_virq(max8907->irqc_rtc,
  166. MAX8907_IRQ_RTC_ALARM0);
  167. if (rtc->irq < 0) {
  168. ret = rtc->irq;
  169. goto err_unregister;
  170. }
  171. ret = request_threaded_irq(rtc->irq, NULL, max8907_irq_handler,
  172. IRQF_ONESHOT, "max8907-alarm0", rtc);
  173. if (ret < 0) {
  174. dev_err(&pdev->dev, "Failed to request IRQ%d: %d\n",
  175. rtc->irq, ret);
  176. goto err_unregister;
  177. }
  178. return 0;
  179. err_unregister:
  180. rtc_device_unregister(rtc->rtc_dev);
  181. return ret;
  182. }
  183. static int __devexit max8907_rtc_remove(struct platform_device *pdev)
  184. {
  185. struct max8907_rtc *rtc = platform_get_drvdata(pdev);
  186. free_irq(rtc->irq, rtc);
  187. rtc_device_unregister(rtc->rtc_dev);
  188. return 0;
  189. }
  190. static struct platform_driver max8907_rtc_driver = {
  191. .driver = {
  192. .name = "max8907-rtc",
  193. .owner = THIS_MODULE,
  194. },
  195. .probe = max8907_rtc_probe,
  196. .remove = __devexit_p(max8907_rtc_remove),
  197. };
  198. module_platform_driver(max8907_rtc_driver);
  199. MODULE_DESCRIPTION("Maxim MAX8907 RTC driver");
  200. MODULE_LICENSE("GPL v2");