rtc-max77686.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * RTC driver for Maxim MAX77686
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  5. *
  6. * based on rtc-max8997.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/max77686-private.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/regmap.h>
  24. /* RTC Control Register */
  25. #define BCD_EN_SHIFT 0
  26. #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
  27. #define MODEL24_SHIFT 1
  28. #define MODEL24_MASK (1 << MODEL24_SHIFT)
  29. /* RTC Update Register1 */
  30. #define RTC_UDR_SHIFT 0
  31. #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
  32. #define RTC_RBUDR_SHIFT 4
  33. #define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
  34. /* RTC Hour register */
  35. #define HOUR_PM_SHIFT 6
  36. #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
  37. /* RTC Alarm Enable */
  38. #define ALARM_ENABLE_SHIFT 7
  39. #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
  40. #define MAX77686_RTC_UPDATE_DELAY 16
  41. enum {
  42. RTC_SEC = 0,
  43. RTC_MIN,
  44. RTC_HOUR,
  45. RTC_WEEKDAY,
  46. RTC_MONTH,
  47. RTC_YEAR,
  48. RTC_DATE,
  49. RTC_NR_TIME
  50. };
  51. struct max77686_rtc_info {
  52. struct device *dev;
  53. struct max77686_dev *max77686;
  54. struct i2c_client *rtc;
  55. struct rtc_device *rtc_dev;
  56. struct mutex lock;
  57. struct regmap *regmap;
  58. int virq;
  59. int rtc_24hr_mode;
  60. };
  61. enum MAX77686_RTC_OP {
  62. MAX77686_RTC_WRITE,
  63. MAX77686_RTC_READ,
  64. };
  65. static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
  66. int rtc_24hr_mode)
  67. {
  68. tm->tm_sec = data[RTC_SEC] & 0x7f;
  69. tm->tm_min = data[RTC_MIN] & 0x7f;
  70. if (rtc_24hr_mode)
  71. tm->tm_hour = data[RTC_HOUR] & 0x1f;
  72. else {
  73. tm->tm_hour = data[RTC_HOUR] & 0x0f;
  74. if (data[RTC_HOUR] & HOUR_PM_MASK)
  75. tm->tm_hour += 12;
  76. }
  77. /* Only a single bit is set in data[], so fls() would be equivalent */
  78. tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f) - 1;
  79. tm->tm_mday = data[RTC_DATE] & 0x1f;
  80. tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
  81. tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
  82. tm->tm_yday = 0;
  83. tm->tm_isdst = 0;
  84. }
  85. static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
  86. {
  87. data[RTC_SEC] = tm->tm_sec;
  88. data[RTC_MIN] = tm->tm_min;
  89. data[RTC_HOUR] = tm->tm_hour;
  90. data[RTC_WEEKDAY] = 1 << tm->tm_wday;
  91. data[RTC_DATE] = tm->tm_mday;
  92. data[RTC_MONTH] = tm->tm_mon + 1;
  93. data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
  94. if (tm->tm_year < 100) {
  95. pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
  96. 1900 + tm->tm_year);
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. static int max77686_rtc_update(struct max77686_rtc_info *info,
  102. enum MAX77686_RTC_OP op)
  103. {
  104. int ret;
  105. unsigned int data;
  106. if (op == MAX77686_RTC_WRITE)
  107. data = 1 << RTC_UDR_SHIFT;
  108. else
  109. data = 1 << RTC_RBUDR_SHIFT;
  110. ret = regmap_update_bits(info->max77686->rtc_regmap,
  111. MAX77686_RTC_UPDATE0, data, data);
  112. if (ret < 0)
  113. dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
  114. __func__, ret, data);
  115. else {
  116. /* Minimum 16ms delay required before RTC update. */
  117. msleep(MAX77686_RTC_UPDATE_DELAY);
  118. }
  119. return ret;
  120. }
  121. static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
  122. {
  123. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  124. u8 data[RTC_NR_TIME];
  125. int ret;
  126. mutex_lock(&info->lock);
  127. ret = max77686_rtc_update(info, MAX77686_RTC_READ);
  128. if (ret < 0)
  129. goto out;
  130. ret = regmap_bulk_read(info->max77686->rtc_regmap,
  131. MAX77686_RTC_SEC, data, RTC_NR_TIME);
  132. if (ret < 0) {
  133. dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__, ret);
  134. goto out;
  135. }
  136. max77686_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
  137. ret = rtc_valid_tm(tm);
  138. out:
  139. mutex_unlock(&info->lock);
  140. return ret;
  141. }
  142. static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
  143. {
  144. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  145. u8 data[RTC_NR_TIME];
  146. int ret;
  147. ret = max77686_rtc_tm_to_data(tm, data);
  148. if (ret < 0)
  149. return ret;
  150. mutex_lock(&info->lock);
  151. ret = regmap_bulk_write(info->max77686->rtc_regmap,
  152. MAX77686_RTC_SEC, data, RTC_NR_TIME);
  153. if (ret < 0) {
  154. dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
  155. ret);
  156. goto out;
  157. }
  158. ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
  159. out:
  160. mutex_unlock(&info->lock);
  161. return ret;
  162. }
  163. static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  164. {
  165. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  166. u8 data[RTC_NR_TIME];
  167. unsigned int val;
  168. int i, ret;
  169. mutex_lock(&info->lock);
  170. ret = max77686_rtc_update(info, MAX77686_RTC_READ);
  171. if (ret < 0)
  172. goto out;
  173. ret = regmap_bulk_read(info->max77686->rtc_regmap,
  174. MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
  175. if (ret < 0) {
  176. dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
  177. __func__, __LINE__, ret);
  178. goto out;
  179. }
  180. max77686_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
  181. alrm->enabled = 0;
  182. for (i = 0; i < RTC_NR_TIME; i++) {
  183. if (data[i] & ALARM_ENABLE_MASK) {
  184. alrm->enabled = 1;
  185. break;
  186. }
  187. }
  188. alrm->pending = 0;
  189. ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
  190. if (ret < 0) {
  191. dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
  192. __func__, __LINE__, ret);
  193. goto out;
  194. }
  195. if (val & (1 << 4)) /* RTCA1 */
  196. alrm->pending = 1;
  197. out:
  198. mutex_unlock(&info->lock);
  199. return 0;
  200. }
  201. static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
  202. {
  203. u8 data[RTC_NR_TIME];
  204. int ret, i;
  205. struct rtc_time tm;
  206. if (!mutex_is_locked(&info->lock))
  207. dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
  208. ret = max77686_rtc_update(info, MAX77686_RTC_READ);
  209. if (ret < 0)
  210. goto out;
  211. ret = regmap_bulk_read(info->max77686->rtc_regmap,
  212. MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
  213. if (ret < 0) {
  214. dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
  215. __func__, ret);
  216. goto out;
  217. }
  218. max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
  219. for (i = 0; i < RTC_NR_TIME; i++)
  220. data[i] &= ~ALARM_ENABLE_MASK;
  221. ret = regmap_bulk_write(info->max77686->rtc_regmap,
  222. MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
  223. if (ret < 0) {
  224. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  225. __func__, ret);
  226. goto out;
  227. }
  228. ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
  229. out:
  230. return ret;
  231. }
  232. static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
  233. {
  234. u8 data[RTC_NR_TIME];
  235. int ret;
  236. struct rtc_time tm;
  237. if (!mutex_is_locked(&info->lock))
  238. dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
  239. ret = max77686_rtc_update(info, MAX77686_RTC_READ);
  240. if (ret < 0)
  241. goto out;
  242. ret = regmap_bulk_read(info->max77686->rtc_regmap,
  243. MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
  244. if (ret < 0) {
  245. dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
  246. __func__, ret);
  247. goto out;
  248. }
  249. max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
  250. data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
  251. data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
  252. data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
  253. data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
  254. if (data[RTC_MONTH] & 0xf)
  255. data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
  256. if (data[RTC_YEAR] & 0x7f)
  257. data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
  258. if (data[RTC_DATE] & 0x1f)
  259. data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
  260. ret = regmap_bulk_write(info->max77686->rtc_regmap,
  261. MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
  262. if (ret < 0) {
  263. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  264. __func__, ret);
  265. goto out;
  266. }
  267. ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
  268. out:
  269. return ret;
  270. }
  271. static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  272. {
  273. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  274. u8 data[RTC_NR_TIME];
  275. int ret;
  276. ret = max77686_rtc_tm_to_data(&alrm->time, data);
  277. if (ret < 0)
  278. return ret;
  279. mutex_lock(&info->lock);
  280. ret = max77686_rtc_stop_alarm(info);
  281. if (ret < 0)
  282. goto out;
  283. ret = regmap_bulk_write(info->max77686->rtc_regmap,
  284. MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
  285. if (ret < 0) {
  286. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  287. __func__, ret);
  288. goto out;
  289. }
  290. ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
  291. if (ret < 0)
  292. goto out;
  293. if (alrm->enabled)
  294. ret = max77686_rtc_start_alarm(info);
  295. out:
  296. mutex_unlock(&info->lock);
  297. return ret;
  298. }
  299. static int max77686_rtc_alarm_irq_enable(struct device *dev,
  300. unsigned int enabled)
  301. {
  302. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  303. int ret;
  304. mutex_lock(&info->lock);
  305. if (enabled)
  306. ret = max77686_rtc_start_alarm(info);
  307. else
  308. ret = max77686_rtc_stop_alarm(info);
  309. mutex_unlock(&info->lock);
  310. return ret;
  311. }
  312. static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
  313. {
  314. struct max77686_rtc_info *info = data;
  315. dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
  316. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  317. return IRQ_HANDLED;
  318. }
  319. static const struct rtc_class_ops max77686_rtc_ops = {
  320. .read_time = max77686_rtc_read_time,
  321. .set_time = max77686_rtc_set_time,
  322. .read_alarm = max77686_rtc_read_alarm,
  323. .set_alarm = max77686_rtc_set_alarm,
  324. .alarm_irq_enable = max77686_rtc_alarm_irq_enable,
  325. };
  326. static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
  327. {
  328. u8 data[2];
  329. int ret;
  330. /* Set RTC control register : Binary mode, 24hour mdoe */
  331. data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  332. data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  333. info->rtc_24hr_mode = 1;
  334. ret = regmap_bulk_write(info->max77686->rtc_regmap, MAX77686_RTC_CONTROLM, data, 2);
  335. if (ret < 0) {
  336. dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
  337. __func__, ret);
  338. return ret;
  339. }
  340. ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
  341. return ret;
  342. }
  343. static int max77686_rtc_probe(struct platform_device *pdev)
  344. {
  345. struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
  346. struct max77686_rtc_info *info;
  347. int ret;
  348. dev_info(&pdev->dev, "%s\n", __func__);
  349. info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
  350. GFP_KERNEL);
  351. if (!info)
  352. return -ENOMEM;
  353. mutex_init(&info->lock);
  354. info->dev = &pdev->dev;
  355. info->max77686 = max77686;
  356. info->rtc = max77686->rtc;
  357. platform_set_drvdata(pdev, info);
  358. ret = max77686_rtc_init_reg(info);
  359. if (ret < 0) {
  360. dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
  361. goto err_rtc;
  362. }
  363. device_init_wakeup(&pdev->dev, 1);
  364. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
  365. &max77686_rtc_ops, THIS_MODULE);
  366. if (IS_ERR(info->rtc_dev)) {
  367. ret = PTR_ERR(info->rtc_dev);
  368. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  369. if (ret == 0)
  370. ret = -EINVAL;
  371. goto err_rtc;
  372. }
  373. if (!max77686->rtc_irq_data) {
  374. ret = -EINVAL;
  375. dev_err(&pdev->dev, "%s: no RTC regmap IRQ chip\n", __func__);
  376. goto err_rtc;
  377. }
  378. info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
  379. MAX77686_RTCIRQ_RTCA1);
  380. if (info->virq <= 0) {
  381. ret = -ENXIO;
  382. goto err_rtc;
  383. }
  384. ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
  385. max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
  386. if (ret < 0)
  387. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  388. info->virq, ret);
  389. err_rtc:
  390. return ret;
  391. }
  392. #ifdef CONFIG_PM_SLEEP
  393. static int max77686_rtc_suspend(struct device *dev)
  394. {
  395. if (device_may_wakeup(dev)) {
  396. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  397. return enable_irq_wake(info->virq);
  398. }
  399. return 0;
  400. }
  401. static int max77686_rtc_resume(struct device *dev)
  402. {
  403. if (device_may_wakeup(dev)) {
  404. struct max77686_rtc_info *info = dev_get_drvdata(dev);
  405. return disable_irq_wake(info->virq);
  406. }
  407. return 0;
  408. }
  409. #endif
  410. static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
  411. max77686_rtc_suspend, max77686_rtc_resume);
  412. static const struct platform_device_id rtc_id[] = {
  413. { "max77686-rtc", 0 },
  414. {},
  415. };
  416. MODULE_DEVICE_TABLE(platform, rtc_id);
  417. static struct platform_driver max77686_rtc_driver = {
  418. .driver = {
  419. .name = "max77686-rtc",
  420. .pm = &max77686_rtc_pm_ops,
  421. },
  422. .probe = max77686_rtc_probe,
  423. .id_table = rtc_id,
  424. };
  425. module_platform_driver(max77686_rtc_driver);
  426. MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
  427. MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
  428. MODULE_LICENSE("GPL");