rtc-max8998.c 7.6 KB

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