rtc-max8997.c 12 KB

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