rtc-max8925.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * RTC driver for Maxim MAX8925
  3. *
  4. * Copyright (C) 2009-2010 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/slab.h>
  14. #include <linux/rtc.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mfd/max8925.h>
  17. enum {
  18. RTC_SEC = 0,
  19. RTC_MIN,
  20. RTC_HOUR,
  21. RTC_WEEKDAY,
  22. RTC_DATE,
  23. RTC_MONTH,
  24. RTC_YEAR1,
  25. RTC_YEAR2,
  26. };
  27. #define MAX8925_RTC_SEC 0x00
  28. #define MAX8925_RTC_MIN 0x01
  29. #define MAX8925_RTC_HOUR 0x02
  30. #define MAX8925_RTC_WEEKDAY 0x03
  31. #define MAX8925_RTC_DATE 0x04
  32. #define MAX8925_RTC_MONTH 0x05
  33. #define MAX8925_RTC_YEAR1 0x06
  34. #define MAX8925_RTC_YEAR2 0x07
  35. #define MAX8925_ALARM0_SEC 0x08
  36. #define MAX8925_ALARM0_MIN 0x09
  37. #define MAX8925_ALARM0_HOUR 0x0a
  38. #define MAX8925_ALARM0_WEEKDAY 0x0b
  39. #define MAX8925_ALARM0_DATE 0x0c
  40. #define MAX8925_ALARM0_MON 0x0d
  41. #define MAX8925_ALARM0_YEAR1 0x0e
  42. #define MAX8925_ALARM0_YEAR2 0x0f
  43. #define MAX8925_ALARM1_SEC 0x10
  44. #define MAX8925_ALARM1_MIN 0x11
  45. #define MAX8925_ALARM1_HOUR 0x12
  46. #define MAX8925_ALARM1_WEEKDAY 0x13
  47. #define MAX8925_ALARM1_DATE 0x14
  48. #define MAX8925_ALARM1_MON 0x15
  49. #define MAX8925_ALARM1_YEAR1 0x16
  50. #define MAX8925_ALARM1_YEAR2 0x17
  51. #define MAX8925_RTC_CNTL 0x1b
  52. #define MAX8925_RTC_STATUS 0x20
  53. #define TIME_NUM 8
  54. #define ALARM_1SEC (1 << 7)
  55. #define HOUR_12 (1 << 7)
  56. #define HOUR_AM_PM (1 << 5)
  57. #define ALARM0_IRQ (1 << 3)
  58. #define ALARM1_IRQ (1 << 2)
  59. #define ALARM0_STATUS (1 << 2)
  60. #define ALARM1_STATUS (1 << 1)
  61. struct max8925_rtc_info {
  62. struct rtc_device *rtc_dev;
  63. struct max8925_chip *chip;
  64. struct i2c_client *rtc;
  65. struct device *dev;
  66. int irq;
  67. };
  68. static irqreturn_t rtc_update_handler(int irq, void *data)
  69. {
  70. struct max8925_rtc_info *info = (struct max8925_rtc_info *)data;
  71. /* disable ALARM0 except for 1SEC alarm */
  72. max8925_set_bits(info->rtc, MAX8925_ALARM0_CNTL, 0x7f, 0);
  73. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  74. return IRQ_HANDLED;
  75. }
  76. static int tm_calc(struct rtc_time *tm, unsigned char *buf, int len)
  77. {
  78. if (len < TIME_NUM)
  79. return -EINVAL;
  80. tm->tm_year = (buf[RTC_YEAR2] >> 4) * 1000
  81. + (buf[RTC_YEAR2] & 0xf) * 100
  82. + (buf[RTC_YEAR1] >> 4) * 10
  83. + (buf[RTC_YEAR1] & 0xf);
  84. tm->tm_year -= 1900;
  85. tm->tm_mon = ((buf[RTC_MONTH] >> 4) & 0x01) * 10
  86. + (buf[RTC_MONTH] & 0x0f);
  87. tm->tm_mday = ((buf[RTC_DATE] >> 4) & 0x03) * 10
  88. + (buf[RTC_DATE] & 0x0f);
  89. tm->tm_wday = buf[RTC_WEEKDAY] & 0x07;
  90. if (buf[RTC_HOUR] & HOUR_12) {
  91. tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x1) * 10
  92. + (buf[RTC_HOUR] & 0x0f);
  93. if (buf[RTC_HOUR] & HOUR_AM_PM)
  94. tm->tm_hour += 12;
  95. } else
  96. tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x03) * 10
  97. + (buf[RTC_HOUR] & 0x0f);
  98. tm->tm_min = ((buf[RTC_MIN] >> 4) & 0x7) * 10
  99. + (buf[RTC_MIN] & 0x0f);
  100. tm->tm_sec = ((buf[RTC_SEC] >> 4) & 0x7) * 10
  101. + (buf[RTC_SEC] & 0x0f);
  102. return 0;
  103. }
  104. static int data_calc(unsigned char *buf, struct rtc_time *tm, int len)
  105. {
  106. unsigned char high, low;
  107. if (len < TIME_NUM)
  108. return -EINVAL;
  109. high = (tm->tm_year + 1900) / 1000;
  110. low = (tm->tm_year + 1900) / 100;
  111. low = low - high * 10;
  112. buf[RTC_YEAR2] = (high << 4) + low;
  113. high = (tm->tm_year + 1900) / 10;
  114. low = tm->tm_year + 1900;
  115. low = low - high * 10;
  116. high = high - (high / 10) * 10;
  117. buf[RTC_YEAR1] = (high << 4) + low;
  118. high = tm->tm_mon / 10;
  119. low = tm->tm_mon;
  120. low = low - high * 10;
  121. buf[RTC_MONTH] = (high << 4) + low;
  122. high = tm->tm_mday / 10;
  123. low = tm->tm_mday;
  124. low = low - high * 10;
  125. buf[RTC_DATE] = (high << 4) + low;
  126. buf[RTC_WEEKDAY] = tm->tm_wday;
  127. high = tm->tm_hour / 10;
  128. low = tm->tm_hour;
  129. low = low - high * 10;
  130. buf[RTC_HOUR] = (high << 4) + low;
  131. high = tm->tm_min / 10;
  132. low = tm->tm_min;
  133. low = low - high * 10;
  134. buf[RTC_MIN] = (high << 4) + low;
  135. high = tm->tm_sec / 10;
  136. low = tm->tm_sec;
  137. low = low - high * 10;
  138. buf[RTC_SEC] = (high << 4) + low;
  139. return 0;
  140. }
  141. static int max8925_rtc_read_time(struct device *dev, struct rtc_time *tm)
  142. {
  143. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  144. unsigned char buf[TIME_NUM];
  145. int ret;
  146. ret = max8925_bulk_read(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
  147. if (ret < 0)
  148. goto out;
  149. ret = tm_calc(tm, buf, TIME_NUM);
  150. out:
  151. return ret;
  152. }
  153. static int max8925_rtc_set_time(struct device *dev, struct rtc_time *tm)
  154. {
  155. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  156. unsigned char buf[TIME_NUM];
  157. int ret;
  158. ret = data_calc(buf, tm, TIME_NUM);
  159. if (ret < 0)
  160. goto out;
  161. ret = max8925_bulk_write(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
  162. out:
  163. return ret;
  164. }
  165. static int max8925_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  166. {
  167. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  168. unsigned char buf[TIME_NUM];
  169. int ret;
  170. ret = max8925_bulk_read(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
  171. if (ret < 0)
  172. goto out;
  173. ret = tm_calc(&alrm->time, buf, TIME_NUM);
  174. if (ret < 0)
  175. goto out;
  176. ret = max8925_reg_read(info->rtc, MAX8925_RTC_IRQ_MASK);
  177. if (ret < 0)
  178. goto out;
  179. if (ret & ALARM0_IRQ) {
  180. alrm->enabled = 0;
  181. } else {
  182. ret = max8925_reg_read(info->rtc, MAX8925_ALARM0_CNTL);
  183. if (ret < 0)
  184. goto out;
  185. if (!ret)
  186. alrm->enabled = 0;
  187. else
  188. alrm->enabled = 1;
  189. }
  190. ret = max8925_reg_read(info->rtc, MAX8925_RTC_STATUS);
  191. if (ret < 0)
  192. goto out;
  193. if (ret & ALARM0_STATUS)
  194. alrm->pending = 1;
  195. else
  196. alrm->pending = 0;
  197. return 0;
  198. out:
  199. return ret;
  200. }
  201. static int max8925_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  202. {
  203. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  204. unsigned char buf[TIME_NUM];
  205. int ret;
  206. ret = data_calc(buf, &alrm->time, TIME_NUM);
  207. if (ret < 0)
  208. goto out;
  209. ret = max8925_bulk_write(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
  210. if (ret < 0)
  211. goto out;
  212. if (alrm->enabled)
  213. /* only enable alarm on year/month/day/hour/min/sec */
  214. ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x77);
  215. else
  216. ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x0);
  217. if (ret < 0)
  218. goto out;
  219. out:
  220. return ret;
  221. }
  222. static const struct rtc_class_ops max8925_rtc_ops = {
  223. .read_time = max8925_rtc_read_time,
  224. .set_time = max8925_rtc_set_time,
  225. .read_alarm = max8925_rtc_read_alarm,
  226. .set_alarm = max8925_rtc_set_alarm,
  227. };
  228. static int max8925_rtc_probe(struct platform_device *pdev)
  229. {
  230. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  231. struct max8925_rtc_info *info;
  232. int ret;
  233. info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_rtc_info),
  234. GFP_KERNEL);
  235. if (!info)
  236. return -ENOMEM;
  237. info->chip = chip;
  238. info->rtc = chip->rtc;
  239. info->dev = &pdev->dev;
  240. info->irq = platform_get_irq(pdev, 0);
  241. ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
  242. rtc_update_handler, IRQF_ONESHOT,
  243. "rtc-alarm0", info);
  244. if (ret < 0) {
  245. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  246. info->irq, ret);
  247. return ret;
  248. }
  249. dev_set_drvdata(&pdev->dev, info);
  250. /* XXX - isn't this redundant? */
  251. platform_set_drvdata(pdev, info);
  252. device_init_wakeup(&pdev->dev, 1);
  253. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8925-rtc",
  254. &max8925_rtc_ops, THIS_MODULE);
  255. ret = PTR_ERR(info->rtc_dev);
  256. if (IS_ERR(info->rtc_dev)) {
  257. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  258. return ret;
  259. }
  260. return 0;
  261. }
  262. #ifdef CONFIG_PM_SLEEP
  263. static int max8925_rtc_suspend(struct device *dev)
  264. {
  265. struct platform_device *pdev = to_platform_device(dev);
  266. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  267. if (device_may_wakeup(dev))
  268. chip->wakeup_flag |= 1 << MAX8925_IRQ_RTC_ALARM0;
  269. return 0;
  270. }
  271. static int max8925_rtc_resume(struct device *dev)
  272. {
  273. struct platform_device *pdev = to_platform_device(dev);
  274. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  275. if (device_may_wakeup(dev))
  276. chip->wakeup_flag &= ~(1 << MAX8925_IRQ_RTC_ALARM0);
  277. return 0;
  278. }
  279. #endif
  280. static SIMPLE_DEV_PM_OPS(max8925_rtc_pm_ops, max8925_rtc_suspend, max8925_rtc_resume);
  281. static struct platform_driver max8925_rtc_driver = {
  282. .driver = {
  283. .name = "max8925-rtc",
  284. .pm = &max8925_rtc_pm_ops,
  285. },
  286. .probe = max8925_rtc_probe,
  287. };
  288. module_platform_driver(max8925_rtc_driver);
  289. MODULE_DESCRIPTION("Maxim MAX8925 RTC driver");
  290. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  291. MODULE_LICENSE("GPL");