rtc-max8997.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * RTC driver for Maxim MAX8997
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co.Ltd
  5. *
  6. * based on rtc-max8998.c
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/slab.h>
  16. #include <linux/rtc.h>
  17. #include <linux/delay.h>
  18. #include <linux/mutex.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mfd/max8997-private.h>
  22. #include <linux/irqdomain.h>
  23. /* Module parameter for WTSR function control */
  24. static int wtsr_en = 1;
  25. module_param(wtsr_en, int, 0444);
  26. MODULE_PARM_DESC(wtsr_en, "Watchdog Timeout & Software Reset (default=on)");
  27. /* Module parameter for SMPL function control */
  28. static int smpl_en = 1;
  29. module_param(smpl_en, int, 0444);
  30. MODULE_PARM_DESC(smpl_en, "Sudden Momentary Power Loss (default=on)");
  31. /* RTC Control Register */
  32. #define BCD_EN_SHIFT 0
  33. #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
  34. #define MODEL24_SHIFT 1
  35. #define MODEL24_MASK (1 << MODEL24_SHIFT)
  36. /* RTC Update Register1 */
  37. #define RTC_UDR_SHIFT 0
  38. #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
  39. /* WTSR and SMPL Register */
  40. #define WTSRT_SHIFT 0
  41. #define SMPLT_SHIFT 2
  42. #define WTSR_EN_SHIFT 6
  43. #define SMPL_EN_SHIFT 7
  44. #define WTSRT_MASK (3 << WTSRT_SHIFT)
  45. #define SMPLT_MASK (3 << SMPLT_SHIFT)
  46. #define WTSR_EN_MASK (1 << WTSR_EN_SHIFT)
  47. #define SMPL_EN_MASK (1 << SMPL_EN_SHIFT)
  48. /* RTC Hour register */
  49. #define HOUR_PM_SHIFT 6
  50. #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
  51. /* RTC Alarm Enable */
  52. #define ALARM_ENABLE_SHIFT 7
  53. #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
  54. enum {
  55. RTC_SEC = 0,
  56. RTC_MIN,
  57. RTC_HOUR,
  58. RTC_WEEKDAY,
  59. RTC_MONTH,
  60. RTC_YEAR,
  61. RTC_DATE,
  62. RTC_NR_TIME
  63. };
  64. struct max8997_rtc_info {
  65. struct device *dev;
  66. struct max8997_dev *max8997;
  67. struct i2c_client *rtc;
  68. struct rtc_device *rtc_dev;
  69. struct mutex lock;
  70. int virq;
  71. int rtc_24hr_mode;
  72. };
  73. static void max8997_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
  74. int rtc_24hr_mode)
  75. {
  76. tm->tm_sec = data[RTC_SEC] & 0x7f;
  77. tm->tm_min = data[RTC_MIN] & 0x7f;
  78. if (rtc_24hr_mode)
  79. tm->tm_hour = data[RTC_HOUR] & 0x1f;
  80. else {
  81. tm->tm_hour = data[RTC_HOUR] & 0x0f;
  82. if (data[RTC_HOUR] & HOUR_PM_MASK)
  83. tm->tm_hour += 12;
  84. }
  85. tm->tm_wday = fls(data[RTC_WEEKDAY] & 0x7f) - 1;
  86. tm->tm_mday = data[RTC_DATE] & 0x1f;
  87. tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
  88. tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
  89. tm->tm_yday = 0;
  90. tm->tm_isdst = 0;
  91. }
  92. static int max8997_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
  93. {
  94. data[RTC_SEC] = tm->tm_sec;
  95. data[RTC_MIN] = tm->tm_min;
  96. data[RTC_HOUR] = tm->tm_hour;
  97. data[RTC_WEEKDAY] = 1 << tm->tm_wday;
  98. data[RTC_DATE] = tm->tm_mday;
  99. data[RTC_MONTH] = tm->tm_mon + 1;
  100. data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
  101. if (tm->tm_year < 100) {
  102. pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
  103. 1900 + tm->tm_year);
  104. return -EINVAL;
  105. }
  106. return 0;
  107. }
  108. static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info *info)
  109. {
  110. int ret;
  111. ret = max8997_write_reg(info->rtc, MAX8997_RTC_UPDATE1,
  112. RTC_UDR_MASK);
  113. if (ret < 0)
  114. dev_err(info->dev, "%s: fail to write update reg(%d)\n",
  115. __func__, ret);
  116. else {
  117. /* Minimum 16ms delay required before RTC update.
  118. * Otherwise, we may read and update based on out-of-date
  119. * value */
  120. msleep(20);
  121. }
  122. return ret;
  123. }
  124. static int max8997_rtc_read_time(struct device *dev, struct rtc_time *tm)
  125. {
  126. struct max8997_rtc_info *info = dev_get_drvdata(dev);
  127. u8 data[RTC_NR_TIME];
  128. int ret;
  129. mutex_lock(&info->lock);
  130. ret = max8997_bulk_read(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
  131. mutex_unlock(&info->lock);
  132. if (ret < 0) {
  133. dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,
  134. ret);
  135. return ret;
  136. }
  137. max8997_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
  138. return rtc_valid_tm(tm);
  139. }
  140. static int max8997_rtc_set_time(struct device *dev, struct rtc_time *tm)
  141. {
  142. struct max8997_rtc_info *info = dev_get_drvdata(dev);
  143. u8 data[RTC_NR_TIME];
  144. int ret;
  145. ret = max8997_rtc_tm_to_data(tm, data);
  146. if (ret < 0)
  147. return ret;
  148. mutex_lock(&info->lock);
  149. ret = max8997_bulk_write(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
  150. if (ret < 0) {
  151. dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
  152. ret);
  153. goto out;
  154. }
  155. ret = max8997_rtc_set_update_reg(info);
  156. out:
  157. mutex_unlock(&info->lock);
  158. return ret;
  159. }
  160. static int max8997_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  161. {
  162. struct max8997_rtc_info *info = dev_get_drvdata(dev);
  163. u8 data[RTC_NR_TIME];
  164. u8 val;
  165. int i, ret;
  166. mutex_lock(&info->lock);
  167. ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
  168. data);
  169. if (ret < 0) {
  170. dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
  171. __func__, __LINE__, ret);
  172. goto out;
  173. }
  174. max8997_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
  175. alrm->enabled = 0;
  176. for (i = 0; i < RTC_NR_TIME; i++) {
  177. if (data[i] & ALARM_ENABLE_MASK) {
  178. alrm->enabled = 1;
  179. break;
  180. }
  181. }
  182. alrm->pending = 0;
  183. ret = max8997_read_reg(info->max8997->i2c, MAX8997_REG_STATUS1, &val);
  184. if (ret < 0) {
  185. dev_err(info->dev, "%s:%d fail to read status1 reg(%d)\n",
  186. __func__, __LINE__, ret);
  187. goto out;
  188. }
  189. if (val & (1 << 4)) /* RTCA1 */
  190. alrm->pending = 1;
  191. out:
  192. mutex_unlock(&info->lock);
  193. return 0;
  194. }
  195. static int max8997_rtc_stop_alarm(struct max8997_rtc_info *info)
  196. {
  197. u8 data[RTC_NR_TIME];
  198. int ret, i;
  199. if (!mutex_is_locked(&info->lock))
  200. dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
  201. ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
  202. data);
  203. if (ret < 0) {
  204. dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
  205. __func__, ret);
  206. goto out;
  207. }
  208. for (i = 0; i < RTC_NR_TIME; i++)
  209. data[i] &= ~ALARM_ENABLE_MASK;
  210. ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
  211. data);
  212. if (ret < 0) {
  213. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  214. __func__, ret);
  215. goto out;
  216. }
  217. ret = max8997_rtc_set_update_reg(info);
  218. out:
  219. return ret;
  220. }
  221. static int max8997_rtc_start_alarm(struct max8997_rtc_info *info)
  222. {
  223. u8 data[RTC_NR_TIME];
  224. int ret;
  225. if (!mutex_is_locked(&info->lock))
  226. dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
  227. ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
  228. data);
  229. if (ret < 0) {
  230. dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
  231. __func__, ret);
  232. goto out;
  233. }
  234. data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
  235. data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
  236. data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
  237. data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
  238. if (data[RTC_MONTH] & 0xf)
  239. data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
  240. if (data[RTC_YEAR] & 0x7f)
  241. data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
  242. if (data[RTC_DATE] & 0x1f)
  243. data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
  244. ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
  245. data);
  246. if (ret < 0) {
  247. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  248. __func__, ret);
  249. goto out;
  250. }
  251. ret = max8997_rtc_set_update_reg(info);
  252. out:
  253. return ret;
  254. }
  255. static int max8997_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  256. {
  257. struct max8997_rtc_info *info = dev_get_drvdata(dev);
  258. u8 data[RTC_NR_TIME];
  259. int ret;
  260. ret = max8997_rtc_tm_to_data(&alrm->time, data);
  261. if (ret < 0)
  262. return ret;
  263. dev_info(info->dev, "%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__,
  264. data[RTC_YEAR] + 2000, data[RTC_MONTH], data[RTC_DATE],
  265. data[RTC_HOUR], data[RTC_MIN], data[RTC_SEC]);
  266. mutex_lock(&info->lock);
  267. ret = max8997_rtc_stop_alarm(info);
  268. if (ret < 0)
  269. goto out;
  270. ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
  271. data);
  272. if (ret < 0) {
  273. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  274. __func__, ret);
  275. goto out;
  276. }
  277. ret = max8997_rtc_set_update_reg(info);
  278. if (ret < 0)
  279. goto out;
  280. if (alrm->enabled)
  281. ret = max8997_rtc_start_alarm(info);
  282. out:
  283. mutex_unlock(&info->lock);
  284. return ret;
  285. }
  286. static int max8997_rtc_alarm_irq_enable(struct device *dev,
  287. unsigned int enabled)
  288. {
  289. struct max8997_rtc_info *info = dev_get_drvdata(dev);
  290. int ret;
  291. mutex_lock(&info->lock);
  292. if (enabled)
  293. ret = max8997_rtc_start_alarm(info);
  294. else
  295. ret = max8997_rtc_stop_alarm(info);
  296. mutex_unlock(&info->lock);
  297. return ret;
  298. }
  299. static irqreturn_t max8997_rtc_alarm_irq(int irq, void *data)
  300. {
  301. struct max8997_rtc_info *info = data;
  302. dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
  303. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  304. return IRQ_HANDLED;
  305. }
  306. static const struct rtc_class_ops max8997_rtc_ops = {
  307. .read_time = max8997_rtc_read_time,
  308. .set_time = max8997_rtc_set_time,
  309. .read_alarm = max8997_rtc_read_alarm,
  310. .set_alarm = max8997_rtc_set_alarm,
  311. .alarm_irq_enable = max8997_rtc_alarm_irq_enable,
  312. };
  313. static void max8997_rtc_enable_wtsr(struct max8997_rtc_info *info, bool enable)
  314. {
  315. int ret;
  316. u8 val, mask;
  317. if (!wtsr_en)
  318. return;
  319. if (enable)
  320. val = (1 << WTSR_EN_SHIFT) | (3 << WTSRT_SHIFT);
  321. else
  322. val = 0;
  323. mask = WTSR_EN_MASK | WTSRT_MASK;
  324. dev_info(info->dev, "%s: %s WTSR\n", __func__,
  325. enable ? "enable" : "disable");
  326. ret = max8997_update_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, val, mask);
  327. if (ret < 0) {
  328. dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
  329. __func__, ret);
  330. return;
  331. }
  332. max8997_rtc_set_update_reg(info);
  333. }
  334. static void max8997_rtc_enable_smpl(struct max8997_rtc_info *info, bool enable)
  335. {
  336. int ret;
  337. u8 val, mask;
  338. if (!smpl_en)
  339. return;
  340. if (enable)
  341. val = (1 << SMPL_EN_SHIFT) | (0 << SMPLT_SHIFT);
  342. else
  343. val = 0;
  344. mask = SMPL_EN_MASK | SMPLT_MASK;
  345. dev_info(info->dev, "%s: %s SMPL\n", __func__,
  346. enable ? "enable" : "disable");
  347. ret = max8997_update_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, val, mask);
  348. if (ret < 0) {
  349. dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
  350. __func__, ret);
  351. return;
  352. }
  353. max8997_rtc_set_update_reg(info);
  354. val = 0;
  355. max8997_read_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, &val);
  356. pr_info("WTSR_SMPL(0x%02x)\n", val);
  357. }
  358. static int max8997_rtc_init_reg(struct max8997_rtc_info *info)
  359. {
  360. u8 data[2];
  361. int ret;
  362. /* Set RTC control register : Binary mode, 24hour mdoe */
  363. data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  364. data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  365. info->rtc_24hr_mode = 1;
  366. ret = max8997_bulk_write(info->rtc, MAX8997_RTC_CTRLMASK, 2, data);
  367. if (ret < 0) {
  368. dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
  369. __func__, ret);
  370. return ret;
  371. }
  372. ret = max8997_rtc_set_update_reg(info);
  373. return ret;
  374. }
  375. static int max8997_rtc_probe(struct platform_device *pdev)
  376. {
  377. struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
  378. struct max8997_rtc_info *info;
  379. int ret, virq;
  380. info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_rtc_info),
  381. GFP_KERNEL);
  382. if (!info)
  383. return -ENOMEM;
  384. mutex_init(&info->lock);
  385. info->dev = &pdev->dev;
  386. info->max8997 = max8997;
  387. info->rtc = max8997->rtc;
  388. platform_set_drvdata(pdev, info);
  389. ret = max8997_rtc_init_reg(info);
  390. if (ret < 0) {
  391. dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
  392. return ret;
  393. }
  394. max8997_rtc_enable_wtsr(info, true);
  395. max8997_rtc_enable_smpl(info, true);
  396. device_init_wakeup(&pdev->dev, 1);
  397. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8997-rtc",
  398. &max8997_rtc_ops, THIS_MODULE);
  399. if (IS_ERR(info->rtc_dev)) {
  400. ret = PTR_ERR(info->rtc_dev);
  401. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  402. return ret;
  403. }
  404. virq = irq_create_mapping(max8997->irq_domain, MAX8997_PMICIRQ_RTCA1);
  405. if (!virq) {
  406. dev_err(&pdev->dev, "Failed to create mapping alarm IRQ\n");
  407. ret = -ENXIO;
  408. goto err_out;
  409. }
  410. info->virq = virq;
  411. ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
  412. max8997_rtc_alarm_irq, 0,
  413. "rtc-alarm0", info);
  414. if (ret < 0)
  415. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  416. info->virq, ret);
  417. err_out:
  418. return ret;
  419. }
  420. static void max8997_rtc_shutdown(struct platform_device *pdev)
  421. {
  422. struct max8997_rtc_info *info = platform_get_drvdata(pdev);
  423. max8997_rtc_enable_wtsr(info, false);
  424. max8997_rtc_enable_smpl(info, false);
  425. }
  426. static const struct platform_device_id rtc_id[] = {
  427. { "max8997-rtc", 0 },
  428. {},
  429. };
  430. MODULE_DEVICE_TABLE(platform, rtc_id);
  431. static struct platform_driver max8997_rtc_driver = {
  432. .driver = {
  433. .name = "max8997-rtc",
  434. },
  435. .probe = max8997_rtc_probe,
  436. .shutdown = max8997_rtc_shutdown,
  437. .id_table = rtc_id,
  438. };
  439. module_platform_driver(max8997_rtc_driver);
  440. MODULE_DESCRIPTION("Maxim MAX8997 RTC driver");
  441. MODULE_AUTHOR("<ms925.kim@samsung.com>");
  442. MODULE_LICENSE("GPL");