rtc-tps80031.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * rtc-tps80031.c -- TI TPS80031/TPS80032 RTC driver
  3. *
  4. * RTC driver for TI TPS80031/TPS80032 Fully Integrated
  5. * Power Management with Power Path and Battery Charger
  6. *
  7. * Copyright (c) 2012, NVIDIA Corporation.
  8. *
  9. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation version 2.
  14. *
  15. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  16. * whether express or implied; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307, USA
  24. */
  25. #include <linux/bcd.h>
  26. #include <linux/device.h>
  27. #include <linux/err.h>
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/mfd/tps80031.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/pm.h>
  34. #include <linux/rtc.h>
  35. #include <linux/slab.h>
  36. #define ENABLE_ALARM_INT 0x08
  37. #define ALARM_INT_STATUS 0x40
  38. /**
  39. * Setting bit to 1 in STOP_RTC will run the RTC and
  40. * setting this bit to 0 will freeze RTC.
  41. */
  42. #define STOP_RTC 0x1
  43. /* Power on reset Values of RTC registers */
  44. #define TPS80031_RTC_POR_YEAR 0
  45. #define TPS80031_RTC_POR_MONTH 1
  46. #define TPS80031_RTC_POR_DAY 1
  47. /* Numbers of registers for time and alarms */
  48. #define TPS80031_RTC_TIME_NUM_REGS 7
  49. #define TPS80031_RTC_ALARM_NUM_REGS 6
  50. /**
  51. * PMU RTC have only 2 nibbles to store year information, so using an
  52. * offset of 100 to set the base year as 2000 for our driver.
  53. */
  54. #define RTC_YEAR_OFFSET 100
  55. struct tps80031_rtc {
  56. struct rtc_device *rtc;
  57. int irq;
  58. };
  59. static int tps80031_rtc_read_time(struct device *dev, struct rtc_time *tm)
  60. {
  61. u8 buff[TPS80031_RTC_TIME_NUM_REGS];
  62. int ret;
  63. ret = tps80031_reads(dev->parent, TPS80031_SLAVE_ID1,
  64. TPS80031_SECONDS_REG, TPS80031_RTC_TIME_NUM_REGS, buff);
  65. if (ret < 0) {
  66. dev_err(dev, "reading RTC_SECONDS_REG failed, err = %d\n", ret);
  67. return ret;
  68. }
  69. tm->tm_sec = bcd2bin(buff[0]);
  70. tm->tm_min = bcd2bin(buff[1]);
  71. tm->tm_hour = bcd2bin(buff[2]);
  72. tm->tm_mday = bcd2bin(buff[3]);
  73. tm->tm_mon = bcd2bin(buff[4]) - 1;
  74. tm->tm_year = bcd2bin(buff[5]) + RTC_YEAR_OFFSET;
  75. tm->tm_wday = bcd2bin(buff[6]);
  76. return 0;
  77. }
  78. static int tps80031_rtc_set_time(struct device *dev, struct rtc_time *tm)
  79. {
  80. u8 buff[7];
  81. int ret;
  82. buff[0] = bin2bcd(tm->tm_sec);
  83. buff[1] = bin2bcd(tm->tm_min);
  84. buff[2] = bin2bcd(tm->tm_hour);
  85. buff[3] = bin2bcd(tm->tm_mday);
  86. buff[4] = bin2bcd(tm->tm_mon + 1);
  87. buff[5] = bin2bcd(tm->tm_year % RTC_YEAR_OFFSET);
  88. buff[6] = bin2bcd(tm->tm_wday);
  89. /* Stop RTC while updating the RTC time registers */
  90. ret = tps80031_clr_bits(dev->parent, TPS80031_SLAVE_ID1,
  91. TPS80031_RTC_CTRL_REG, STOP_RTC);
  92. if (ret < 0) {
  93. dev_err(dev->parent, "Stop RTC failed, err = %d\n", ret);
  94. return ret;
  95. }
  96. ret = tps80031_writes(dev->parent, TPS80031_SLAVE_ID1,
  97. TPS80031_SECONDS_REG,
  98. TPS80031_RTC_TIME_NUM_REGS, buff);
  99. if (ret < 0) {
  100. dev_err(dev, "writing RTC_SECONDS_REG failed, err %d\n", ret);
  101. return ret;
  102. }
  103. ret = tps80031_set_bits(dev->parent, TPS80031_SLAVE_ID1,
  104. TPS80031_RTC_CTRL_REG, STOP_RTC);
  105. if (ret < 0)
  106. dev_err(dev->parent, "Start RTC failed, err = %d\n", ret);
  107. return ret;
  108. }
  109. static int tps80031_rtc_alarm_irq_enable(struct device *dev,
  110. unsigned int enable)
  111. {
  112. int ret;
  113. if (enable)
  114. ret = tps80031_set_bits(dev->parent, TPS80031_SLAVE_ID1,
  115. TPS80031_RTC_INTERRUPTS_REG, ENABLE_ALARM_INT);
  116. else
  117. ret = tps80031_clr_bits(dev->parent, TPS80031_SLAVE_ID1,
  118. TPS80031_RTC_INTERRUPTS_REG, ENABLE_ALARM_INT);
  119. if (ret < 0) {
  120. dev_err(dev, "Update on RTC_INT failed, err = %d\n", ret);
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static int tps80031_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  126. {
  127. u8 buff[TPS80031_RTC_ALARM_NUM_REGS];
  128. int ret;
  129. buff[0] = bin2bcd(alrm->time.tm_sec);
  130. buff[1] = bin2bcd(alrm->time.tm_min);
  131. buff[2] = bin2bcd(alrm->time.tm_hour);
  132. buff[3] = bin2bcd(alrm->time.tm_mday);
  133. buff[4] = bin2bcd(alrm->time.tm_mon + 1);
  134. buff[5] = bin2bcd(alrm->time.tm_year % RTC_YEAR_OFFSET);
  135. ret = tps80031_writes(dev->parent, TPS80031_SLAVE_ID1,
  136. TPS80031_ALARM_SECONDS_REG,
  137. TPS80031_RTC_ALARM_NUM_REGS, buff);
  138. if (ret < 0) {
  139. dev_err(dev, "Writing RTC_ALARM failed, err %d\n", ret);
  140. return ret;
  141. }
  142. return tps80031_rtc_alarm_irq_enable(dev, alrm->enabled);
  143. }
  144. static int tps80031_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  145. {
  146. u8 buff[6];
  147. int ret;
  148. ret = tps80031_reads(dev->parent, TPS80031_SLAVE_ID1,
  149. TPS80031_ALARM_SECONDS_REG,
  150. TPS80031_RTC_ALARM_NUM_REGS, buff);
  151. if (ret < 0) {
  152. dev_err(dev->parent,
  153. "reading RTC_ALARM failed, err = %d\n", ret);
  154. return ret;
  155. }
  156. alrm->time.tm_sec = bcd2bin(buff[0]);
  157. alrm->time.tm_min = bcd2bin(buff[1]);
  158. alrm->time.tm_hour = bcd2bin(buff[2]);
  159. alrm->time.tm_mday = bcd2bin(buff[3]);
  160. alrm->time.tm_mon = bcd2bin(buff[4]) - 1;
  161. alrm->time.tm_year = bcd2bin(buff[5]) + RTC_YEAR_OFFSET;
  162. return 0;
  163. }
  164. static int clear_alarm_int_status(struct device *dev, struct tps80031_rtc *rtc)
  165. {
  166. int ret;
  167. u8 buf;
  168. /**
  169. * As per datasheet, A dummy read of this RTC_STATUS_REG register
  170. * is necessary before each I2C read in order to update the status
  171. * register value.
  172. */
  173. ret = tps80031_read(dev->parent, TPS80031_SLAVE_ID1,
  174. TPS80031_RTC_STATUS_REG, &buf);
  175. if (ret < 0) {
  176. dev_err(dev, "reading RTC_STATUS failed. err = %d\n", ret);
  177. return ret;
  178. }
  179. /* clear Alarm status bits.*/
  180. ret = tps80031_set_bits(dev->parent, TPS80031_SLAVE_ID1,
  181. TPS80031_RTC_STATUS_REG, ALARM_INT_STATUS);
  182. if (ret < 0) {
  183. dev_err(dev, "clear Alarm INT failed, err = %d\n", ret);
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static irqreturn_t tps80031_rtc_irq(int irq, void *data)
  189. {
  190. struct device *dev = data;
  191. struct tps80031_rtc *rtc = dev_get_drvdata(dev);
  192. int ret;
  193. ret = clear_alarm_int_status(dev, rtc);
  194. if (ret < 0)
  195. return ret;
  196. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
  197. return IRQ_HANDLED;
  198. }
  199. static const struct rtc_class_ops tps80031_rtc_ops = {
  200. .read_time = tps80031_rtc_read_time,
  201. .set_time = tps80031_rtc_set_time,
  202. .set_alarm = tps80031_rtc_set_alarm,
  203. .read_alarm = tps80031_rtc_read_alarm,
  204. .alarm_irq_enable = tps80031_rtc_alarm_irq_enable,
  205. };
  206. static int tps80031_rtc_probe(struct platform_device *pdev)
  207. {
  208. struct tps80031_rtc *rtc;
  209. struct rtc_time tm;
  210. int ret;
  211. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  212. if (!rtc)
  213. return -ENOMEM;
  214. rtc->irq = platform_get_irq(pdev, 0);
  215. platform_set_drvdata(pdev, rtc);
  216. /* Start RTC */
  217. ret = tps80031_set_bits(pdev->dev.parent, TPS80031_SLAVE_ID1,
  218. TPS80031_RTC_CTRL_REG, STOP_RTC);
  219. if (ret < 0) {
  220. dev_err(&pdev->dev, "failed to start RTC. err = %d\n", ret);
  221. return ret;
  222. }
  223. /* If RTC have POR values, set time 01:01:2000 */
  224. tps80031_rtc_read_time(&pdev->dev, &tm);
  225. if ((tm.tm_year == RTC_YEAR_OFFSET + TPS80031_RTC_POR_YEAR) &&
  226. (tm.tm_mon == (TPS80031_RTC_POR_MONTH - 1)) &&
  227. (tm.tm_mday == TPS80031_RTC_POR_DAY)) {
  228. tm.tm_year = 2000;
  229. tm.tm_mday = 1;
  230. tm.tm_mon = 1;
  231. ret = tps80031_rtc_set_time(&pdev->dev, &tm);
  232. if (ret < 0) {
  233. dev_err(&pdev->dev,
  234. "RTC set time failed, err = %d\n", ret);
  235. return ret;
  236. }
  237. }
  238. /* Clear alarm intretupt status if it is there */
  239. ret = clear_alarm_int_status(&pdev->dev, rtc);
  240. if (ret < 0) {
  241. dev_err(&pdev->dev, "Clear alarm int failed, err = %d\n", ret);
  242. return ret;
  243. }
  244. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  245. &tps80031_rtc_ops, THIS_MODULE);
  246. if (IS_ERR(rtc->rtc)) {
  247. ret = PTR_ERR(rtc->rtc);
  248. dev_err(&pdev->dev, "RTC registration failed, err %d\n", ret);
  249. return ret;
  250. }
  251. ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
  252. tps80031_rtc_irq,
  253. IRQF_ONESHOT | IRQF_EARLY_RESUME,
  254. dev_name(&pdev->dev), rtc);
  255. if (ret < 0) {
  256. dev_err(&pdev->dev, "request IRQ:%d failed, err = %d\n",
  257. rtc->irq, ret);
  258. return ret;
  259. }
  260. device_set_wakeup_capable(&pdev->dev, 1);
  261. return 0;
  262. }
  263. #ifdef CONFIG_PM_SLEEP
  264. static int tps80031_rtc_suspend(struct device *dev)
  265. {
  266. struct tps80031_rtc *rtc = dev_get_drvdata(dev);
  267. if (device_may_wakeup(dev))
  268. enable_irq_wake(rtc->irq);
  269. return 0;
  270. }
  271. static int tps80031_rtc_resume(struct device *dev)
  272. {
  273. struct tps80031_rtc *rtc = dev_get_drvdata(dev);
  274. if (device_may_wakeup(dev))
  275. disable_irq_wake(rtc->irq);
  276. return 0;
  277. };
  278. #endif
  279. static SIMPLE_DEV_PM_OPS(tps80031_pm_ops, tps80031_rtc_suspend,
  280. tps80031_rtc_resume);
  281. static struct platform_driver tps80031_rtc_driver = {
  282. .driver = {
  283. .name = "tps80031-rtc",
  284. .pm = &tps80031_pm_ops,
  285. },
  286. .probe = tps80031_rtc_probe,
  287. };
  288. module_platform_driver(tps80031_rtc_driver);
  289. MODULE_ALIAS("platform:tps80031-rtc");
  290. MODULE_DESCRIPTION("TI TPS80031/TPS80032 RTC driver");
  291. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  292. MODULE_LICENSE("GPL v2");