rtc-m48t59.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * ST M48T59 RTC driver
  3. *
  4. * Copyright (c) 2007 Wind River Systems, Inc.
  5. *
  6. * Author: Mark Zhan <rongkai.zhan@windriver.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rtc.h>
  19. #include <linux/rtc/m48t59.h>
  20. #include <linux/bcd.h>
  21. #include <linux/slab.h>
  22. #ifndef NO_IRQ
  23. #define NO_IRQ (-1)
  24. #endif
  25. #define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
  26. #define M48T59_WRITE(val, reg) \
  27. (pdata->write_byte(dev, pdata->offset + reg, val))
  28. #define M48T59_SET_BITS(mask, reg) \
  29. M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
  30. #define M48T59_CLEAR_BITS(mask, reg) \
  31. M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
  32. struct m48t59_private {
  33. void __iomem *ioaddr;
  34. int irq;
  35. struct rtc_device *rtc;
  36. spinlock_t lock; /* serialize the NVRAM and RTC access */
  37. };
  38. /*
  39. * This is the generic access method when the chip is memory-mapped
  40. */
  41. static void
  42. m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
  43. {
  44. struct platform_device *pdev = to_platform_device(dev);
  45. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  46. writeb(val, m48t59->ioaddr+ofs);
  47. }
  48. static u8
  49. m48t59_mem_readb(struct device *dev, u32 ofs)
  50. {
  51. struct platform_device *pdev = to_platform_device(dev);
  52. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  53. return readb(m48t59->ioaddr+ofs);
  54. }
  55. /*
  56. * NOTE: M48T59 only uses BCD mode
  57. */
  58. static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
  59. {
  60. struct platform_device *pdev = to_platform_device(dev);
  61. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  62. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  63. unsigned long flags;
  64. u8 val;
  65. spin_lock_irqsave(&m48t59->lock, flags);
  66. /* Issue the READ command */
  67. M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  68. tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
  69. /* tm_mon is 0-11 */
  70. tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
  71. tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
  72. val = M48T59_READ(M48T59_WDAY);
  73. if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
  74. (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
  75. dev_dbg(dev, "Century bit is enabled\n");
  76. tm->tm_year += 100; /* one century */
  77. }
  78. #ifdef CONFIG_SPARC
  79. /* Sun SPARC machines count years since 1968 */
  80. tm->tm_year += 68;
  81. #endif
  82. tm->tm_wday = bcd2bin(val & 0x07);
  83. tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
  84. tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
  85. tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
  86. /* Clear the READ bit */
  87. M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  88. spin_unlock_irqrestore(&m48t59->lock, flags);
  89. dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
  90. tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
  91. tm->tm_hour, tm->tm_min, tm->tm_sec);
  92. return rtc_valid_tm(tm);
  93. }
  94. static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
  95. {
  96. struct platform_device *pdev = to_platform_device(dev);
  97. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  98. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  99. unsigned long flags;
  100. u8 val = 0;
  101. int year = tm->tm_year;
  102. #ifdef CONFIG_SPARC
  103. /* Sun SPARC machines count years since 1968 */
  104. year -= 68;
  105. #endif
  106. dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
  107. year + 1900, tm->tm_mon, tm->tm_mday,
  108. tm->tm_hour, tm->tm_min, tm->tm_sec);
  109. if (year < 0)
  110. return -EINVAL;
  111. spin_lock_irqsave(&m48t59->lock, flags);
  112. /* Issue the WRITE command */
  113. M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  114. M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
  115. M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
  116. M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
  117. M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
  118. /* tm_mon is 0-11 */
  119. M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
  120. M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
  121. if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
  122. val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
  123. val |= (bin2bcd(tm->tm_wday) & 0x07);
  124. M48T59_WRITE(val, M48T59_WDAY);
  125. /* Clear the WRITE bit */
  126. M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  127. spin_unlock_irqrestore(&m48t59->lock, flags);
  128. return 0;
  129. }
  130. /*
  131. * Read alarm time and date in RTC
  132. */
  133. static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  134. {
  135. struct platform_device *pdev = to_platform_device(dev);
  136. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  137. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  138. struct rtc_time *tm = &alrm->time;
  139. unsigned long flags;
  140. u8 val;
  141. /* If no irq, we don't support ALARM */
  142. if (m48t59->irq == NO_IRQ)
  143. return -EIO;
  144. spin_lock_irqsave(&m48t59->lock, flags);
  145. /* Issue the READ command */
  146. M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  147. tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
  148. #ifdef CONFIG_SPARC
  149. /* Sun SPARC machines count years since 1968 */
  150. tm->tm_year += 68;
  151. #endif
  152. /* tm_mon is 0-11 */
  153. tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
  154. val = M48T59_READ(M48T59_WDAY);
  155. if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
  156. tm->tm_year += 100; /* one century */
  157. tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE));
  158. tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR));
  159. tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN));
  160. tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC));
  161. /* Clear the READ bit */
  162. M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  163. spin_unlock_irqrestore(&m48t59->lock, flags);
  164. dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
  165. tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
  166. tm->tm_hour, tm->tm_min, tm->tm_sec);
  167. return rtc_valid_tm(tm);
  168. }
  169. /*
  170. * Set alarm time and date in RTC
  171. */
  172. static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  173. {
  174. struct platform_device *pdev = to_platform_device(dev);
  175. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  176. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  177. struct rtc_time *tm = &alrm->time;
  178. u8 mday, hour, min, sec;
  179. unsigned long flags;
  180. int year = tm->tm_year;
  181. #ifdef CONFIG_SPARC
  182. /* Sun SPARC machines count years since 1968 */
  183. year -= 68;
  184. #endif
  185. /* If no irq, we don't support ALARM */
  186. if (m48t59->irq == NO_IRQ)
  187. return -EIO;
  188. if (year < 0)
  189. return -EINVAL;
  190. /*
  191. * 0xff means "always match"
  192. */
  193. mday = tm->tm_mday;
  194. mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
  195. if (mday == 0xff)
  196. mday = M48T59_READ(M48T59_MDAY);
  197. hour = tm->tm_hour;
  198. hour = (hour < 24) ? bin2bcd(hour) : 0x00;
  199. min = tm->tm_min;
  200. min = (min < 60) ? bin2bcd(min) : 0x00;
  201. sec = tm->tm_sec;
  202. sec = (sec < 60) ? bin2bcd(sec) : 0x00;
  203. spin_lock_irqsave(&m48t59->lock, flags);
  204. /* Issue the WRITE command */
  205. M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  206. M48T59_WRITE(mday, M48T59_ALARM_DATE);
  207. M48T59_WRITE(hour, M48T59_ALARM_HOUR);
  208. M48T59_WRITE(min, M48T59_ALARM_MIN);
  209. M48T59_WRITE(sec, M48T59_ALARM_SEC);
  210. /* Clear the WRITE bit */
  211. M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  212. spin_unlock_irqrestore(&m48t59->lock, flags);
  213. dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
  214. year + 1900, tm->tm_mon, tm->tm_mday,
  215. tm->tm_hour, tm->tm_min, tm->tm_sec);
  216. return 0;
  217. }
  218. /*
  219. * Handle commands from user-space
  220. */
  221. static int m48t59_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  222. {
  223. struct platform_device *pdev = to_platform_device(dev);
  224. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  225. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  226. unsigned long flags;
  227. spin_lock_irqsave(&m48t59->lock, flags);
  228. if (enabled)
  229. M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR);
  230. else
  231. M48T59_WRITE(0x00, M48T59_INTR);
  232. spin_unlock_irqrestore(&m48t59->lock, flags);
  233. return 0;
  234. }
  235. static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq)
  236. {
  237. struct platform_device *pdev = to_platform_device(dev);
  238. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  239. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  240. unsigned long flags;
  241. u8 val;
  242. spin_lock_irqsave(&m48t59->lock, flags);
  243. val = M48T59_READ(M48T59_FLAGS);
  244. spin_unlock_irqrestore(&m48t59->lock, flags);
  245. seq_printf(seq, "battery\t\t: %s\n",
  246. (val & M48T59_FLAGS_BF) ? "low" : "normal");
  247. return 0;
  248. }
  249. /*
  250. * IRQ handler for the RTC
  251. */
  252. static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id)
  253. {
  254. struct device *dev = (struct device *)dev_id;
  255. struct platform_device *pdev = to_platform_device(dev);
  256. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  257. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  258. u8 event;
  259. spin_lock(&m48t59->lock);
  260. event = M48T59_READ(M48T59_FLAGS);
  261. spin_unlock(&m48t59->lock);
  262. if (event & M48T59_FLAGS_AF) {
  263. rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF));
  264. return IRQ_HANDLED;
  265. }
  266. return IRQ_NONE;
  267. }
  268. static const struct rtc_class_ops m48t59_rtc_ops = {
  269. .read_time = m48t59_rtc_read_time,
  270. .set_time = m48t59_rtc_set_time,
  271. .read_alarm = m48t59_rtc_readalarm,
  272. .set_alarm = m48t59_rtc_setalarm,
  273. .proc = m48t59_rtc_proc,
  274. .alarm_irq_enable = m48t59_rtc_alarm_irq_enable,
  275. };
  276. static const struct rtc_class_ops m48t02_rtc_ops = {
  277. .read_time = m48t59_rtc_read_time,
  278. .set_time = m48t59_rtc_set_time,
  279. };
  280. static ssize_t m48t59_nvram_read(struct file *filp, struct kobject *kobj,
  281. struct bin_attribute *bin_attr,
  282. char *buf, loff_t pos, size_t size)
  283. {
  284. struct device *dev = container_of(kobj, struct device, kobj);
  285. struct platform_device *pdev = to_platform_device(dev);
  286. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  287. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  288. ssize_t cnt = 0;
  289. unsigned long flags;
  290. spin_lock_irqsave(&m48t59->lock, flags);
  291. for (; cnt < size; cnt++)
  292. *buf++ = M48T59_READ(cnt);
  293. spin_unlock_irqrestore(&m48t59->lock, flags);
  294. return cnt;
  295. }
  296. static ssize_t m48t59_nvram_write(struct file *filp, struct kobject *kobj,
  297. struct bin_attribute *bin_attr,
  298. char *buf, loff_t pos, size_t size)
  299. {
  300. struct device *dev = container_of(kobj, struct device, kobj);
  301. struct platform_device *pdev = to_platform_device(dev);
  302. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  303. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  304. ssize_t cnt = 0;
  305. unsigned long flags;
  306. spin_lock_irqsave(&m48t59->lock, flags);
  307. for (; cnt < size; cnt++)
  308. M48T59_WRITE(*buf++, cnt);
  309. spin_unlock_irqrestore(&m48t59->lock, flags);
  310. return cnt;
  311. }
  312. static struct bin_attribute m48t59_nvram_attr = {
  313. .attr = {
  314. .name = "nvram",
  315. .mode = S_IRUGO | S_IWUSR,
  316. },
  317. .read = m48t59_nvram_read,
  318. .write = m48t59_nvram_write,
  319. };
  320. static int m48t59_rtc_probe(struct platform_device *pdev)
  321. {
  322. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  323. struct m48t59_private *m48t59 = NULL;
  324. struct resource *res;
  325. int ret = -ENOMEM;
  326. char *name;
  327. const struct rtc_class_ops *ops;
  328. /* This chip could be memory-mapped or I/O-mapped */
  329. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  330. if (!res) {
  331. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  332. if (!res)
  333. return -EINVAL;
  334. }
  335. if (res->flags & IORESOURCE_IO) {
  336. /* If we are I/O-mapped, the platform should provide
  337. * the operations accessing chip registers.
  338. */
  339. if (!pdata || !pdata->write_byte || !pdata->read_byte)
  340. return -EINVAL;
  341. } else if (res->flags & IORESOURCE_MEM) {
  342. /* we are memory-mapped */
  343. if (!pdata) {
  344. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
  345. GFP_KERNEL);
  346. if (!pdata)
  347. return -ENOMEM;
  348. /* Ensure we only kmalloc platform data once */
  349. pdev->dev.platform_data = pdata;
  350. }
  351. if (!pdata->type)
  352. pdata->type = M48T59RTC_TYPE_M48T59;
  353. /* Try to use the generic memory read/write ops */
  354. if (!pdata->write_byte)
  355. pdata->write_byte = m48t59_mem_writeb;
  356. if (!pdata->read_byte)
  357. pdata->read_byte = m48t59_mem_readb;
  358. }
  359. m48t59 = devm_kzalloc(&pdev->dev, sizeof(*m48t59), GFP_KERNEL);
  360. if (!m48t59)
  361. return -ENOMEM;
  362. m48t59->ioaddr = pdata->ioaddr;
  363. if (!m48t59->ioaddr) {
  364. /* ioaddr not mapped externally */
  365. m48t59->ioaddr = devm_ioremap(&pdev->dev, res->start,
  366. resource_size(res));
  367. if (!m48t59->ioaddr)
  368. return ret;
  369. }
  370. /* Try to get irq number. We also can work in
  371. * the mode without IRQ.
  372. */
  373. m48t59->irq = platform_get_irq(pdev, 0);
  374. if (m48t59->irq <= 0)
  375. m48t59->irq = NO_IRQ;
  376. if (m48t59->irq != NO_IRQ) {
  377. ret = devm_request_irq(&pdev->dev, m48t59->irq,
  378. m48t59_rtc_interrupt, IRQF_SHARED,
  379. "rtc-m48t59", &pdev->dev);
  380. if (ret)
  381. return ret;
  382. }
  383. switch (pdata->type) {
  384. case M48T59RTC_TYPE_M48T59:
  385. name = "m48t59";
  386. ops = &m48t59_rtc_ops;
  387. pdata->offset = 0x1ff0;
  388. break;
  389. case M48T59RTC_TYPE_M48T02:
  390. name = "m48t02";
  391. ops = &m48t02_rtc_ops;
  392. pdata->offset = 0x7f0;
  393. break;
  394. case M48T59RTC_TYPE_M48T08:
  395. name = "m48t08";
  396. ops = &m48t02_rtc_ops;
  397. pdata->offset = 0x1ff0;
  398. break;
  399. default:
  400. dev_err(&pdev->dev, "Unknown RTC type\n");
  401. return -ENODEV;
  402. }
  403. spin_lock_init(&m48t59->lock);
  404. platform_set_drvdata(pdev, m48t59);
  405. m48t59->rtc = devm_rtc_device_register(&pdev->dev, name, ops,
  406. THIS_MODULE);
  407. if (IS_ERR(m48t59->rtc))
  408. return PTR_ERR(m48t59->rtc);
  409. m48t59_nvram_attr.size = pdata->offset;
  410. ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
  411. if (ret)
  412. return ret;
  413. return 0;
  414. }
  415. static int m48t59_rtc_remove(struct platform_device *pdev)
  416. {
  417. sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
  418. return 0;
  419. }
  420. /* work with hotplug and coldplug */
  421. MODULE_ALIAS("platform:rtc-m48t59");
  422. static struct platform_driver m48t59_rtc_driver = {
  423. .driver = {
  424. .name = "rtc-m48t59",
  425. },
  426. .probe = m48t59_rtc_probe,
  427. .remove = m48t59_rtc_remove,
  428. };
  429. module_platform_driver(m48t59_rtc_driver);
  430. MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
  431. MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
  432. MODULE_LICENSE("GPL");