rtc-max8998.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * RTC driver for Maxim MAX8998
  3. *
  4. * Copyright (C) 2010 Samsung Electronics Co.Ltd
  5. * Author: Minkyu Kang <mk7.kang@samsung.com>
  6. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/bcd.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/rtc.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mfd/max8998.h>
  22. #include <linux/mfd/max8998-private.h>
  23. #include <linux/delay.h>
  24. #define MAX8998_RTC_SEC 0x00
  25. #define MAX8998_RTC_MIN 0x01
  26. #define MAX8998_RTC_HOUR 0x02
  27. #define MAX8998_RTC_WEEKDAY 0x03
  28. #define MAX8998_RTC_DATE 0x04
  29. #define MAX8998_RTC_MONTH 0x05
  30. #define MAX8998_RTC_YEAR1 0x06
  31. #define MAX8998_RTC_YEAR2 0x07
  32. #define MAX8998_ALARM0_SEC 0x08
  33. #define MAX8998_ALARM0_MIN 0x09
  34. #define MAX8998_ALARM0_HOUR 0x0a
  35. #define MAX8998_ALARM0_WEEKDAY 0x0b
  36. #define MAX8998_ALARM0_DATE 0x0c
  37. #define MAX8998_ALARM0_MONTH 0x0d
  38. #define MAX8998_ALARM0_YEAR1 0x0e
  39. #define MAX8998_ALARM0_YEAR2 0x0f
  40. #define MAX8998_ALARM1_SEC 0x10
  41. #define MAX8998_ALARM1_MIN 0x11
  42. #define MAX8998_ALARM1_HOUR 0x12
  43. #define MAX8998_ALARM1_WEEKDAY 0x13
  44. #define MAX8998_ALARM1_DATE 0x14
  45. #define MAX8998_ALARM1_MONTH 0x15
  46. #define MAX8998_ALARM1_YEAR1 0x16
  47. #define MAX8998_ALARM1_YEAR2 0x17
  48. #define MAX8998_ALARM0_CONF 0x18
  49. #define MAX8998_ALARM1_CONF 0x19
  50. #define MAX8998_RTC_STATUS 0x1a
  51. #define MAX8998_WTSR_SMPL_CNTL 0x1b
  52. #define MAX8998_TEST 0x1f
  53. #define HOUR_12 (1 << 7)
  54. #define HOUR_PM (1 << 5)
  55. #define ALARM0_STATUS (1 << 1)
  56. #define ALARM1_STATUS (1 << 2)
  57. enum {
  58. RTC_SEC = 0,
  59. RTC_MIN,
  60. RTC_HOUR,
  61. RTC_WEEKDAY,
  62. RTC_DATE,
  63. RTC_MONTH,
  64. RTC_YEAR1,
  65. RTC_YEAR2,
  66. };
  67. struct max8998_rtc_info {
  68. struct device *dev;
  69. struct max8998_dev *max8998;
  70. struct i2c_client *rtc;
  71. struct rtc_device *rtc_dev;
  72. int irq;
  73. bool lp3974_bug_workaround;
  74. };
  75. static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
  76. {
  77. tm->tm_sec = bcd2bin(data[RTC_SEC]);
  78. tm->tm_min = bcd2bin(data[RTC_MIN]);
  79. if (data[RTC_HOUR] & HOUR_12) {
  80. tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
  81. if (data[RTC_HOUR] & HOUR_PM)
  82. tm->tm_hour += 12;
  83. } else
  84. tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
  85. tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
  86. tm->tm_mday = bcd2bin(data[RTC_DATE]);
  87. tm->tm_mon = bcd2bin(data[RTC_MONTH]);
  88. tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
  89. tm->tm_year -= 1900;
  90. }
  91. static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
  92. {
  93. data[RTC_SEC] = bin2bcd(tm->tm_sec);
  94. data[RTC_MIN] = bin2bcd(tm->tm_min);
  95. data[RTC_HOUR] = bin2bcd(tm->tm_hour);
  96. data[RTC_WEEKDAY] = tm->tm_wday;
  97. data[RTC_DATE] = bin2bcd(tm->tm_mday);
  98. data[RTC_MONTH] = bin2bcd(tm->tm_mon);
  99. data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
  100. data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
  101. }
  102. static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
  103. {
  104. struct max8998_rtc_info *info = dev_get_drvdata(dev);
  105. u8 data[8];
  106. int ret;
  107. ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
  108. if (ret < 0)
  109. return ret;
  110. max8998_data_to_tm(data, tm);
  111. return rtc_valid_tm(tm);
  112. }
  113. static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
  114. {
  115. struct max8998_rtc_info *info = dev_get_drvdata(dev);
  116. u8 data[8];
  117. int ret;
  118. max8998_tm_to_data(tm, data);
  119. ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
  120. if (info->lp3974_bug_workaround)
  121. msleep(2000);
  122. return ret;
  123. }
  124. static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  125. {
  126. struct max8998_rtc_info *info = dev_get_drvdata(dev);
  127. u8 data[8];
  128. u8 val;
  129. int ret;
  130. ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
  131. if (ret < 0)
  132. return ret;
  133. max8998_data_to_tm(data, &alrm->time);
  134. ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
  135. if (ret < 0)
  136. return ret;
  137. alrm->enabled = !!val;
  138. ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
  139. if (ret < 0)
  140. return ret;
  141. if (val & ALARM0_STATUS)
  142. alrm->pending = 1;
  143. else
  144. alrm->pending = 0;
  145. return 0;
  146. }
  147. static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
  148. {
  149. int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
  150. if (info->lp3974_bug_workaround)
  151. msleep(2000);
  152. return ret;
  153. }
  154. static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
  155. {
  156. int ret;
  157. u8 alarm0_conf = 0x77;
  158. /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
  159. if (info->lp3974_bug_workaround)
  160. alarm0_conf = 0x57;
  161. ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
  162. if (info->lp3974_bug_workaround)
  163. msleep(2000);
  164. return ret;
  165. }
  166. static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  167. {
  168. struct max8998_rtc_info *info = dev_get_drvdata(dev);
  169. u8 data[8];
  170. int ret;
  171. max8998_tm_to_data(&alrm->time, data);
  172. ret = max8998_rtc_stop_alarm(info);
  173. if (ret < 0)
  174. return ret;
  175. ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
  176. if (ret < 0)
  177. return ret;
  178. if (info->lp3974_bug_workaround)
  179. msleep(2000);
  180. if (alrm->enabled)
  181. ret = max8998_rtc_start_alarm(info);
  182. return ret;
  183. }
  184. static int max8998_rtc_alarm_irq_enable(struct device *dev,
  185. unsigned int enabled)
  186. {
  187. struct max8998_rtc_info *info = dev_get_drvdata(dev);
  188. if (enabled)
  189. return max8998_rtc_start_alarm(info);
  190. else
  191. return max8998_rtc_stop_alarm(info);
  192. }
  193. static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
  194. {
  195. struct max8998_rtc_info *info = data;
  196. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  197. return IRQ_HANDLED;
  198. }
  199. static const struct rtc_class_ops max8998_rtc_ops = {
  200. .read_time = max8998_rtc_read_time,
  201. .set_time = max8998_rtc_set_time,
  202. .read_alarm = max8998_rtc_read_alarm,
  203. .set_alarm = max8998_rtc_set_alarm,
  204. .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
  205. };
  206. static int max8998_rtc_probe(struct platform_device *pdev)
  207. {
  208. struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
  209. struct max8998_platform_data *pdata = max8998->pdata;
  210. struct max8998_rtc_info *info;
  211. int ret;
  212. info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info),
  213. GFP_KERNEL);
  214. if (!info)
  215. return -ENOMEM;
  216. info->dev = &pdev->dev;
  217. info->max8998 = max8998;
  218. info->rtc = max8998->rtc;
  219. platform_set_drvdata(pdev, info);
  220. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8998-rtc",
  221. &max8998_rtc_ops, THIS_MODULE);
  222. if (IS_ERR(info->rtc_dev)) {
  223. ret = PTR_ERR(info->rtc_dev);
  224. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  225. return ret;
  226. }
  227. if (!max8998->irq_domain)
  228. goto no_irq;
  229. info->irq = irq_create_mapping(max8998->irq_domain, MAX8998_IRQ_ALARM0);
  230. if (!info->irq) {
  231. dev_warn(&pdev->dev, "Failed to map alarm IRQ\n");
  232. goto no_irq;
  233. }
  234. ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
  235. max8998_rtc_alarm_irq, 0, "rtc-alarm0", info);
  236. if (ret < 0)
  237. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  238. info->irq, ret);
  239. no_irq:
  240. dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
  241. if (pdata && pdata->rtc_delay) {
  242. info->lp3974_bug_workaround = true;
  243. dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
  244. " RTC updates will be extremely slow.\n");
  245. }
  246. return 0;
  247. }
  248. static const struct platform_device_id max8998_rtc_id[] = {
  249. { "max8998-rtc", TYPE_MAX8998 },
  250. { "lp3974-rtc", TYPE_LP3974 },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(platform, max8998_rtc_id);
  254. static struct platform_driver max8998_rtc_driver = {
  255. .driver = {
  256. .name = "max8998-rtc",
  257. },
  258. .probe = max8998_rtc_probe,
  259. .id_table = max8998_rtc_id,
  260. };
  261. module_platform_driver(max8998_rtc_driver);
  262. MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
  263. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  264. MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
  265. MODULE_LICENSE("GPL");