rtc-max8925.c 7.8 KB

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