rtc-max77802.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * RTC driver for Maxim MAX77802
  3. *
  4. * Copyright (C) 2013 Google, Inc
  5. *
  6. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  7. *
  8. * based on rtc-max8997.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/rtc.h>
  18. #include <linux/delay.h>
  19. #include <linux/mutex.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/max77686-private.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/regmap.h>
  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. #define RTC_RBUDR_SHIFT 4
  34. #define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
  35. /* RTC Hour register */
  36. #define HOUR_PM_SHIFT 6
  37. #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
  38. /* RTC Alarm Enable */
  39. #define ALARM_ENABLE_SHIFT 7
  40. #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
  41. /* For the RTCAE1 register, we write this value to enable the alarm */
  42. #define ALARM_ENABLE_VALUE 0x77
  43. #define MAX77802_RTC_UPDATE_DELAY_US 200
  44. enum {
  45. RTC_SEC = 0,
  46. RTC_MIN,
  47. RTC_HOUR,
  48. RTC_WEEKDAY,
  49. RTC_MONTH,
  50. RTC_YEAR,
  51. RTC_DATE,
  52. RTC_NR_TIME
  53. };
  54. struct max77802_rtc_info {
  55. struct device *dev;
  56. struct max77686_dev *max77802;
  57. struct i2c_client *rtc;
  58. struct rtc_device *rtc_dev;
  59. struct mutex lock;
  60. struct regmap *regmap;
  61. int virq;
  62. int rtc_24hr_mode;
  63. };
  64. enum MAX77802_RTC_OP {
  65. MAX77802_RTC_WRITE,
  66. MAX77802_RTC_READ,
  67. };
  68. static void max77802_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
  69. int rtc_24hr_mode)
  70. {
  71. tm->tm_sec = data[RTC_SEC] & 0xff;
  72. tm->tm_min = data[RTC_MIN] & 0xff;
  73. if (rtc_24hr_mode)
  74. tm->tm_hour = data[RTC_HOUR] & 0x1f;
  75. else {
  76. tm->tm_hour = data[RTC_HOUR] & 0x0f;
  77. if (data[RTC_HOUR] & HOUR_PM_MASK)
  78. tm->tm_hour += 12;
  79. }
  80. /* Only a single bit is set in data[], so fls() would be equivalent */
  81. tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0xff) - 1;
  82. tm->tm_mday = data[RTC_DATE] & 0x1f;
  83. tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
  84. tm->tm_year = data[RTC_YEAR] & 0xff;
  85. tm->tm_yday = 0;
  86. tm->tm_isdst = 0;
  87. }
  88. static int max77802_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
  89. {
  90. data[RTC_SEC] = tm->tm_sec;
  91. data[RTC_MIN] = tm->tm_min;
  92. data[RTC_HOUR] = tm->tm_hour;
  93. data[RTC_WEEKDAY] = 1 << tm->tm_wday;
  94. data[RTC_DATE] = tm->tm_mday;
  95. data[RTC_MONTH] = tm->tm_mon + 1;
  96. data[RTC_YEAR] = tm->tm_year;
  97. return 0;
  98. }
  99. static int max77802_rtc_update(struct max77802_rtc_info *info,
  100. enum MAX77802_RTC_OP op)
  101. {
  102. int ret;
  103. unsigned int data;
  104. if (op == MAX77802_RTC_WRITE)
  105. data = 1 << RTC_UDR_SHIFT;
  106. else
  107. data = 1 << RTC_RBUDR_SHIFT;
  108. ret = regmap_update_bits(info->max77802->regmap,
  109. MAX77802_RTC_UPDATE0, data, data);
  110. if (ret < 0)
  111. dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
  112. __func__, ret, data);
  113. else {
  114. /* Minimum delay required before RTC update. */
  115. usleep_range(MAX77802_RTC_UPDATE_DELAY_US,
  116. MAX77802_RTC_UPDATE_DELAY_US * 2);
  117. }
  118. return ret;
  119. }
  120. static int max77802_rtc_read_time(struct device *dev, struct rtc_time *tm)
  121. {
  122. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  123. u8 data[RTC_NR_TIME];
  124. int ret;
  125. mutex_lock(&info->lock);
  126. ret = max77802_rtc_update(info, MAX77802_RTC_READ);
  127. if (ret < 0)
  128. goto out;
  129. ret = regmap_bulk_read(info->max77802->regmap,
  130. MAX77802_RTC_SEC, data, RTC_NR_TIME);
  131. if (ret < 0) {
  132. dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,
  133. ret);
  134. goto out;
  135. }
  136. max77802_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 max77802_rtc_set_time(struct device *dev, struct rtc_time *tm)
  143. {
  144. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  145. u8 data[RTC_NR_TIME];
  146. int ret;
  147. ret = max77802_rtc_tm_to_data(tm, data);
  148. if (ret < 0)
  149. return ret;
  150. mutex_lock(&info->lock);
  151. ret = regmap_bulk_write(info->max77802->regmap,
  152. MAX77802_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 = max77802_rtc_update(info, MAX77802_RTC_WRITE);
  159. out:
  160. mutex_unlock(&info->lock);
  161. return ret;
  162. }
  163. static int max77802_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  164. {
  165. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  166. u8 data[RTC_NR_TIME];
  167. unsigned int val;
  168. int ret;
  169. mutex_lock(&info->lock);
  170. ret = max77802_rtc_update(info, MAX77802_RTC_READ);
  171. if (ret < 0)
  172. goto out;
  173. ret = regmap_bulk_read(info->max77802->regmap,
  174. MAX77802_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. max77802_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
  181. alrm->enabled = 0;
  182. ret = regmap_read(info->max77802->regmap,
  183. MAX77802_RTC_AE1, &val);
  184. if (ret < 0) {
  185. dev_err(info->dev, "%s:%d fail to read alarm enable(%d)\n",
  186. __func__, __LINE__, ret);
  187. goto out;
  188. }
  189. if (val)
  190. alrm->enabled = 1;
  191. alrm->pending = 0;
  192. ret = regmap_read(info->max77802->regmap, MAX77802_REG_STATUS2, &val);
  193. if (ret < 0) {
  194. dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
  195. __func__, __LINE__, ret);
  196. goto out;
  197. }
  198. if (val & (1 << 2)) /* RTCA1 */
  199. alrm->pending = 1;
  200. out:
  201. mutex_unlock(&info->lock);
  202. return 0;
  203. }
  204. static int max77802_rtc_stop_alarm(struct max77802_rtc_info *info)
  205. {
  206. int ret;
  207. if (!mutex_is_locked(&info->lock))
  208. dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
  209. ret = max77802_rtc_update(info, MAX77802_RTC_READ);
  210. if (ret < 0)
  211. goto out;
  212. ret = regmap_write(info->max77802->regmap,
  213. MAX77802_RTC_AE1, 0);
  214. if (ret < 0) {
  215. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  216. __func__, ret);
  217. goto out;
  218. }
  219. ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
  220. out:
  221. return ret;
  222. }
  223. static int max77802_rtc_start_alarm(struct max77802_rtc_info *info)
  224. {
  225. int ret;
  226. if (!mutex_is_locked(&info->lock))
  227. dev_warn(info->dev, "%s: should have mutex locked\n",
  228. __func__);
  229. ret = max77802_rtc_update(info, MAX77802_RTC_READ);
  230. if (ret < 0)
  231. goto out;
  232. ret = regmap_write(info->max77802->regmap,
  233. MAX77802_RTC_AE1,
  234. ALARM_ENABLE_VALUE);
  235. if (ret < 0) {
  236. dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
  237. __func__, ret);
  238. goto out;
  239. }
  240. ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
  241. out:
  242. return ret;
  243. }
  244. static int max77802_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  245. {
  246. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  247. u8 data[RTC_NR_TIME];
  248. int ret;
  249. ret = max77802_rtc_tm_to_data(&alrm->time, data);
  250. if (ret < 0)
  251. return ret;
  252. mutex_lock(&info->lock);
  253. ret = max77802_rtc_stop_alarm(info);
  254. if (ret < 0)
  255. goto out;
  256. ret = regmap_bulk_write(info->max77802->regmap,
  257. MAX77802_ALARM1_SEC, data, RTC_NR_TIME);
  258. if (ret < 0) {
  259. dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
  260. __func__, ret);
  261. goto out;
  262. }
  263. ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
  264. if (ret < 0)
  265. goto out;
  266. if (alrm->enabled)
  267. ret = max77802_rtc_start_alarm(info);
  268. out:
  269. mutex_unlock(&info->lock);
  270. return ret;
  271. }
  272. static int max77802_rtc_alarm_irq_enable(struct device *dev,
  273. unsigned int enabled)
  274. {
  275. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  276. int ret;
  277. mutex_lock(&info->lock);
  278. if (enabled)
  279. ret = max77802_rtc_start_alarm(info);
  280. else
  281. ret = max77802_rtc_stop_alarm(info);
  282. mutex_unlock(&info->lock);
  283. return ret;
  284. }
  285. static irqreturn_t max77802_rtc_alarm_irq(int irq, void *data)
  286. {
  287. struct max77802_rtc_info *info = data;
  288. dev_dbg(info->dev, "%s:irq(%d)\n", __func__, irq);
  289. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  290. return IRQ_HANDLED;
  291. }
  292. static const struct rtc_class_ops max77802_rtc_ops = {
  293. .read_time = max77802_rtc_read_time,
  294. .set_time = max77802_rtc_set_time,
  295. .read_alarm = max77802_rtc_read_alarm,
  296. .set_alarm = max77802_rtc_set_alarm,
  297. .alarm_irq_enable = max77802_rtc_alarm_irq_enable,
  298. };
  299. static int max77802_rtc_init_reg(struct max77802_rtc_info *info)
  300. {
  301. u8 data[2];
  302. int ret;
  303. max77802_rtc_update(info, MAX77802_RTC_READ);
  304. /* Set RTC control register : Binary mode, 24hour mdoe */
  305. data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  306. data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  307. info->rtc_24hr_mode = 1;
  308. ret = regmap_bulk_write(info->max77802->regmap,
  309. MAX77802_RTC_CONTROLM, data, ARRAY_SIZE(data));
  310. if (ret < 0) {
  311. dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
  312. __func__, ret);
  313. return ret;
  314. }
  315. ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
  316. return ret;
  317. }
  318. static int max77802_rtc_probe(struct platform_device *pdev)
  319. {
  320. struct max77686_dev *max77802 = dev_get_drvdata(pdev->dev.parent);
  321. struct max77802_rtc_info *info;
  322. int ret;
  323. dev_dbg(&pdev->dev, "%s\n", __func__);
  324. info = devm_kzalloc(&pdev->dev, sizeof(struct max77802_rtc_info),
  325. GFP_KERNEL);
  326. if (!info)
  327. return -ENOMEM;
  328. mutex_init(&info->lock);
  329. info->dev = &pdev->dev;
  330. info->max77802 = max77802;
  331. info->rtc = max77802->i2c;
  332. platform_set_drvdata(pdev, info);
  333. ret = max77802_rtc_init_reg(info);
  334. if (ret < 0) {
  335. dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
  336. return ret;
  337. }
  338. device_init_wakeup(&pdev->dev, 1);
  339. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77802-rtc",
  340. &max77802_rtc_ops, THIS_MODULE);
  341. if (IS_ERR(info->rtc_dev)) {
  342. ret = PTR_ERR(info->rtc_dev);
  343. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  344. if (ret == 0)
  345. ret = -EINVAL;
  346. return ret;
  347. }
  348. if (!max77802->rtc_irq_data) {
  349. dev_err(&pdev->dev, "No RTC regmap IRQ chip\n");
  350. return -EINVAL;
  351. }
  352. info->virq = regmap_irq_get_virq(max77802->rtc_irq_data,
  353. MAX77686_RTCIRQ_RTCA1);
  354. if (info->virq <= 0) {
  355. dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
  356. MAX77686_RTCIRQ_RTCA1);
  357. return -EINVAL;
  358. }
  359. ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
  360. max77802_rtc_alarm_irq, 0, "rtc-alarm1",
  361. info);
  362. if (ret < 0)
  363. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  364. info->virq, ret);
  365. return ret;
  366. }
  367. #ifdef CONFIG_PM_SLEEP
  368. static int max77802_rtc_suspend(struct device *dev)
  369. {
  370. if (device_may_wakeup(dev)) {
  371. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  372. return enable_irq_wake(info->virq);
  373. }
  374. return 0;
  375. }
  376. static int max77802_rtc_resume(struct device *dev)
  377. {
  378. if (device_may_wakeup(dev)) {
  379. struct max77802_rtc_info *info = dev_get_drvdata(dev);
  380. return disable_irq_wake(info->virq);
  381. }
  382. return 0;
  383. }
  384. #endif
  385. static SIMPLE_DEV_PM_OPS(max77802_rtc_pm_ops,
  386. max77802_rtc_suspend, max77802_rtc_resume);
  387. static const struct platform_device_id rtc_id[] = {
  388. { "max77802-rtc", 0 },
  389. {},
  390. };
  391. MODULE_DEVICE_TABLE(platform, rtc_id);
  392. static struct platform_driver max77802_rtc_driver = {
  393. .driver = {
  394. .name = "max77802-rtc",
  395. .pm = &max77802_rtc_pm_ops,
  396. },
  397. .probe = max77802_rtc_probe,
  398. .id_table = rtc_id,
  399. };
  400. module_platform_driver(max77802_rtc_driver);
  401. MODULE_DESCRIPTION("Maxim MAX77802 RTC driver");
  402. MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
  403. MODULE_LICENSE("GPL");