rtc-max8925.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. };
  67. static irqreturn_t rtc_update_handler(int irq, void *data)
  68. {
  69. struct max8925_rtc_info *info = (struct max8925_rtc_info *)data;
  70. /* disable ALARM0 except for 1SEC alarm */
  71. max8925_set_bits(info->rtc, MAX8925_ALARM0_CNTL, 0x7f, 0);
  72. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  73. return IRQ_HANDLED;
  74. }
  75. static int tm_calc(struct rtc_time *tm, unsigned char *buf, int len)
  76. {
  77. if (len < TIME_NUM)
  78. return -EINVAL;
  79. tm->tm_year = (buf[RTC_YEAR2] >> 4) * 1000
  80. + (buf[RTC_YEAR2] & 0xf) * 100
  81. + (buf[RTC_YEAR1] >> 4) * 10
  82. + (buf[RTC_YEAR1] & 0xf);
  83. tm->tm_year -= 1900;
  84. tm->tm_mon = ((buf[RTC_MONTH] >> 4) & 0x01) * 10
  85. + (buf[RTC_MONTH] & 0x0f);
  86. tm->tm_mday = ((buf[RTC_DATE] >> 4) & 0x03) * 10
  87. + (buf[RTC_DATE] & 0x0f);
  88. tm->tm_wday = buf[RTC_WEEKDAY] & 0x07;
  89. if (buf[RTC_HOUR] & HOUR_12) {
  90. tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x1) * 10
  91. + (buf[RTC_HOUR] & 0x0f);
  92. if (buf[RTC_HOUR] & HOUR_AM_PM)
  93. tm->tm_hour += 12;
  94. } else
  95. tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x03) * 10
  96. + (buf[RTC_HOUR] & 0x0f);
  97. tm->tm_min = ((buf[RTC_MIN] >> 4) & 0x7) * 10
  98. + (buf[RTC_MIN] & 0x0f);
  99. tm->tm_sec = ((buf[RTC_SEC] >> 4) & 0x7) * 10
  100. + (buf[RTC_SEC] & 0x0f);
  101. return 0;
  102. }
  103. static int data_calc(unsigned char *buf, struct rtc_time *tm, int len)
  104. {
  105. unsigned char high, low;
  106. if (len < TIME_NUM)
  107. return -EINVAL;
  108. high = (tm->tm_year + 1900) / 1000;
  109. low = (tm->tm_year + 1900) / 100;
  110. low = low - high * 10;
  111. buf[RTC_YEAR2] = (high << 4) + low;
  112. high = (tm->tm_year + 1900) / 10;
  113. low = tm->tm_year + 1900;
  114. low = low - high * 10;
  115. high = high - (high / 10) * 10;
  116. buf[RTC_YEAR1] = (high << 4) + low;
  117. high = tm->tm_mon / 10;
  118. low = tm->tm_mon;
  119. low = low - high * 10;
  120. buf[RTC_MONTH] = (high << 4) + low;
  121. high = tm->tm_mday / 10;
  122. low = tm->tm_mday;
  123. low = low - high * 10;
  124. buf[RTC_DATE] = (high << 4) + low;
  125. buf[RTC_WEEKDAY] = tm->tm_wday;
  126. high = tm->tm_hour / 10;
  127. low = tm->tm_hour;
  128. low = low - high * 10;
  129. buf[RTC_HOUR] = (high << 4) + low;
  130. high = tm->tm_min / 10;
  131. low = tm->tm_min;
  132. low = low - high * 10;
  133. buf[RTC_MIN] = (high << 4) + low;
  134. high = tm->tm_sec / 10;
  135. low = tm->tm_sec;
  136. low = low - high * 10;
  137. buf[RTC_SEC] = (high << 4) + low;
  138. return 0;
  139. }
  140. static int max8925_rtc_read_time(struct device *dev, struct rtc_time *tm)
  141. {
  142. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  143. unsigned char buf[TIME_NUM];
  144. int ret;
  145. ret = max8925_bulk_read(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
  146. if (ret < 0)
  147. goto out;
  148. ret = tm_calc(tm, buf, TIME_NUM);
  149. out:
  150. return ret;
  151. }
  152. static int max8925_rtc_set_time(struct device *dev, struct rtc_time *tm)
  153. {
  154. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  155. unsigned char buf[TIME_NUM];
  156. int ret;
  157. ret = data_calc(buf, tm, TIME_NUM);
  158. if (ret < 0)
  159. goto out;
  160. ret = max8925_bulk_write(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
  161. out:
  162. return ret;
  163. }
  164. static int max8925_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  165. {
  166. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  167. unsigned char buf[TIME_NUM];
  168. int ret;
  169. ret = max8925_bulk_read(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
  170. if (ret < 0)
  171. goto out;
  172. ret = tm_calc(&alrm->time, buf, TIME_NUM);
  173. if (ret < 0)
  174. goto out;
  175. ret = max8925_reg_read(info->rtc, MAX8925_RTC_IRQ_MASK);
  176. if (ret < 0)
  177. goto out;
  178. if (ret & ALARM0_IRQ) {
  179. alrm->enabled = 0;
  180. } else {
  181. ret = max8925_reg_read(info->rtc, MAX8925_ALARM0_CNTL);
  182. if (ret < 0)
  183. goto out;
  184. if (!ret)
  185. alrm->enabled = 0;
  186. else
  187. alrm->enabled = 1;
  188. }
  189. ret = max8925_reg_read(info->rtc, MAX8925_RTC_STATUS);
  190. if (ret < 0)
  191. goto out;
  192. if (ret & ALARM0_STATUS)
  193. alrm->pending = 1;
  194. else
  195. alrm->pending = 0;
  196. return 0;
  197. out:
  198. return ret;
  199. }
  200. static int max8925_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  201. {
  202. struct max8925_rtc_info *info = dev_get_drvdata(dev);
  203. unsigned char buf[TIME_NUM];
  204. int ret;
  205. ret = data_calc(buf, &alrm->time, TIME_NUM);
  206. if (ret < 0)
  207. goto out;
  208. ret = max8925_bulk_write(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
  209. if (ret < 0)
  210. goto out;
  211. if (alrm->enabled)
  212. /* only enable alarm on year/month/day/hour/min/sec */
  213. ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x77);
  214. else
  215. ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x0);
  216. if (ret < 0)
  217. goto out;
  218. out:
  219. return ret;
  220. }
  221. static const struct rtc_class_ops max8925_rtc_ops = {
  222. .read_time = max8925_rtc_read_time,
  223. .set_time = max8925_rtc_set_time,
  224. .read_alarm = max8925_rtc_read_alarm,
  225. .set_alarm = max8925_rtc_set_alarm,
  226. };
  227. static int __devinit max8925_rtc_probe(struct platform_device *pdev)
  228. {
  229. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  230. struct max8925_rtc_info *info;
  231. int irq, ret;
  232. info = kzalloc(sizeof(struct max8925_rtc_info), GFP_KERNEL);
  233. if (!info)
  234. return -ENOMEM;
  235. info->chip = chip;
  236. info->rtc = chip->rtc;
  237. info->dev = &pdev->dev;
  238. irq = chip->irq_base + MAX8925_IRQ_RTC_ALARM0;
  239. ret = request_threaded_irq(irq, NULL, rtc_update_handler,
  240. IRQF_ONESHOT, "rtc-alarm0", info);
  241. if (ret < 0) {
  242. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  243. irq, ret);
  244. goto out_irq;
  245. }
  246. dev_set_drvdata(&pdev->dev, info);
  247. /* XXX - isn't this redundant? */
  248. platform_set_drvdata(pdev, info);
  249. device_init_wakeup(&pdev->dev, 1);
  250. info->rtc_dev = rtc_device_register("max8925-rtc", &pdev->dev,
  251. &max8925_rtc_ops, THIS_MODULE);
  252. ret = PTR_ERR(info->rtc_dev);
  253. if (IS_ERR(info->rtc_dev)) {
  254. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  255. goto out_rtc;
  256. }
  257. return 0;
  258. out_rtc:
  259. platform_set_drvdata(pdev, NULL);
  260. free_irq(chip->irq_base + MAX8925_IRQ_RTC_ALARM0, info);
  261. out_irq:
  262. kfree(info);
  263. return ret;
  264. }
  265. static int __devexit max8925_rtc_remove(struct platform_device *pdev)
  266. {
  267. struct max8925_rtc_info *info = platform_get_drvdata(pdev);
  268. if (info) {
  269. free_irq(info->chip->irq_base + MAX8925_IRQ_RTC_ALARM0, info);
  270. rtc_device_unregister(info->rtc_dev);
  271. kfree(info);
  272. }
  273. return 0;
  274. }
  275. #ifdef CONFIG_PM_SLEEP
  276. static int max8925_rtc_suspend(struct device *dev)
  277. {
  278. struct platform_device *pdev = to_platform_device(dev);
  279. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  280. if (device_may_wakeup(dev))
  281. chip->wakeup_flag |= 1 << MAX8925_IRQ_RTC_ALARM0;
  282. return 0;
  283. }
  284. static int max8925_rtc_resume(struct device *dev)
  285. {
  286. struct platform_device *pdev = to_platform_device(dev);
  287. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  288. if (device_may_wakeup(dev))
  289. chip->wakeup_flag &= ~(1 << MAX8925_IRQ_RTC_ALARM0);
  290. return 0;
  291. }
  292. #endif
  293. static SIMPLE_DEV_PM_OPS(max8925_rtc_pm_ops, max8925_rtc_suspend, max8925_rtc_resume);
  294. static struct platform_driver max8925_rtc_driver = {
  295. .driver = {
  296. .name = "max8925-rtc",
  297. .owner = THIS_MODULE,
  298. .pm = &max8925_rtc_pm_ops,
  299. },
  300. .probe = max8925_rtc_probe,
  301. .remove = __devexit_p(max8925_rtc_remove),
  302. };
  303. module_platform_driver(max8925_rtc_driver);
  304. MODULE_DESCRIPTION("Maxim MAX8925 RTC driver");
  305. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  306. MODULE_LICENSE("GPL");