rtc-ds1511.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * An rtc driver for the Dallas DS1511
  3. *
  4. * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. * Copyright (C) 2007 Andrew Sharp <andy.sharp@lsi.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Real time clock driver for the Dallas 1511 chip, which also
  12. * contains a watchdog timer. There is a tiny amount of code that
  13. * platform code could use to mess with the watchdog device a little
  14. * bit, but not a full watchdog driver.
  15. */
  16. #include <linux/bcd.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/gfp.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/rtc.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #define DRV_VERSION "0.6"
  27. enum ds1511reg {
  28. DS1511_SEC = 0x0,
  29. DS1511_MIN = 0x1,
  30. DS1511_HOUR = 0x2,
  31. DS1511_DOW = 0x3,
  32. DS1511_DOM = 0x4,
  33. DS1511_MONTH = 0x5,
  34. DS1511_YEAR = 0x6,
  35. DS1511_CENTURY = 0x7,
  36. DS1511_AM1_SEC = 0x8,
  37. DS1511_AM2_MIN = 0x9,
  38. DS1511_AM3_HOUR = 0xa,
  39. DS1511_AM4_DATE = 0xb,
  40. DS1511_WD_MSEC = 0xc,
  41. DS1511_WD_SEC = 0xd,
  42. DS1511_CONTROL_A = 0xe,
  43. DS1511_CONTROL_B = 0xf,
  44. DS1511_RAMADDR_LSB = 0x10,
  45. DS1511_RAMDATA = 0x13
  46. };
  47. #define DS1511_BLF1 0x80
  48. #define DS1511_BLF2 0x40
  49. #define DS1511_PRS 0x20
  50. #define DS1511_PAB 0x10
  51. #define DS1511_TDF 0x08
  52. #define DS1511_KSF 0x04
  53. #define DS1511_WDF 0x02
  54. #define DS1511_IRQF 0x01
  55. #define DS1511_TE 0x80
  56. #define DS1511_CS 0x40
  57. #define DS1511_BME 0x20
  58. #define DS1511_TPE 0x10
  59. #define DS1511_TIE 0x08
  60. #define DS1511_KIE 0x04
  61. #define DS1511_WDE 0x02
  62. #define DS1511_WDS 0x01
  63. #define DS1511_RAM_MAX 0xff
  64. #define RTC_CMD DS1511_CONTROL_B
  65. #define RTC_CMD1 DS1511_CONTROL_A
  66. #define RTC_ALARM_SEC DS1511_AM1_SEC
  67. #define RTC_ALARM_MIN DS1511_AM2_MIN
  68. #define RTC_ALARM_HOUR DS1511_AM3_HOUR
  69. #define RTC_ALARM_DATE DS1511_AM4_DATE
  70. #define RTC_SEC DS1511_SEC
  71. #define RTC_MIN DS1511_MIN
  72. #define RTC_HOUR DS1511_HOUR
  73. #define RTC_DOW DS1511_DOW
  74. #define RTC_DOM DS1511_DOM
  75. #define RTC_MON DS1511_MONTH
  76. #define RTC_YEAR DS1511_YEAR
  77. #define RTC_CENTURY DS1511_CENTURY
  78. #define RTC_TIE DS1511_TIE
  79. #define RTC_TE DS1511_TE
  80. struct rtc_plat_data {
  81. struct rtc_device *rtc;
  82. void __iomem *ioaddr; /* virtual base address */
  83. int size; /* amount of memory mapped */
  84. int irq;
  85. unsigned int irqen;
  86. int alrm_sec;
  87. int alrm_min;
  88. int alrm_hour;
  89. int alrm_mday;
  90. spinlock_t lock;
  91. };
  92. static DEFINE_SPINLOCK(ds1511_lock);
  93. static __iomem char *ds1511_base;
  94. static u32 reg_spacing = 1;
  95. static noinline void
  96. rtc_write(uint8_t val, uint32_t reg)
  97. {
  98. writeb(val, ds1511_base + (reg * reg_spacing));
  99. }
  100. static inline void
  101. rtc_write_alarm(uint8_t val, enum ds1511reg reg)
  102. {
  103. rtc_write((val | 0x80), reg);
  104. }
  105. static noinline uint8_t
  106. rtc_read(enum ds1511reg reg)
  107. {
  108. return readb(ds1511_base + (reg * reg_spacing));
  109. }
  110. static inline void
  111. rtc_disable_update(void)
  112. {
  113. rtc_write((rtc_read(RTC_CMD) & ~RTC_TE), RTC_CMD);
  114. }
  115. static void
  116. rtc_enable_update(void)
  117. {
  118. rtc_write((rtc_read(RTC_CMD) | RTC_TE), RTC_CMD);
  119. }
  120. /*
  121. * #define DS1511_WDOG_RESET_SUPPORT
  122. *
  123. * Uncomment this if you want to use these routines in
  124. * some platform code.
  125. */
  126. #ifdef DS1511_WDOG_RESET_SUPPORT
  127. /*
  128. * just enough code to set the watchdog timer so that it
  129. * will reboot the system
  130. */
  131. void
  132. ds1511_wdog_set(unsigned long deciseconds)
  133. {
  134. /*
  135. * the wdog timer can take 99.99 seconds
  136. */
  137. deciseconds %= 10000;
  138. /*
  139. * set the wdog values in the wdog registers
  140. */
  141. rtc_write(bin2bcd(deciseconds % 100), DS1511_WD_MSEC);
  142. rtc_write(bin2bcd(deciseconds / 100), DS1511_WD_SEC);
  143. /*
  144. * set wdog enable and wdog 'steering' bit to issue a reset
  145. */
  146. rtc_write(DS1511_WDE | DS1511_WDS, RTC_CMD);
  147. }
  148. void
  149. ds1511_wdog_disable(void)
  150. {
  151. /*
  152. * clear wdog enable and wdog 'steering' bits
  153. */
  154. rtc_write(rtc_read(RTC_CMD) & ~(DS1511_WDE | DS1511_WDS), RTC_CMD);
  155. /*
  156. * clear the wdog counter
  157. */
  158. rtc_write(0, DS1511_WD_MSEC);
  159. rtc_write(0, DS1511_WD_SEC);
  160. }
  161. #endif
  162. /*
  163. * set the rtc chip's idea of the time.
  164. * stupidly, some callers call with year unmolested;
  165. * and some call with year = year - 1900. thanks.
  166. */
  167. static int ds1511_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
  168. {
  169. u8 mon, day, dow, hrs, min, sec, yrs, cen;
  170. unsigned long flags;
  171. /*
  172. * won't have to change this for a while
  173. */
  174. if (rtc_tm->tm_year < 1900)
  175. rtc_tm->tm_year += 1900;
  176. if (rtc_tm->tm_year < 1970)
  177. return -EINVAL;
  178. yrs = rtc_tm->tm_year % 100;
  179. cen = rtc_tm->tm_year / 100;
  180. mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
  181. day = rtc_tm->tm_mday;
  182. dow = rtc_tm->tm_wday & 0x7; /* automatic BCD */
  183. hrs = rtc_tm->tm_hour;
  184. min = rtc_tm->tm_min;
  185. sec = rtc_tm->tm_sec;
  186. if ((mon > 12) || (day == 0))
  187. return -EINVAL;
  188. if (day > rtc_month_days(rtc_tm->tm_mon, rtc_tm->tm_year))
  189. return -EINVAL;
  190. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  191. return -EINVAL;
  192. /*
  193. * each register is a different number of valid bits
  194. */
  195. sec = bin2bcd(sec) & 0x7f;
  196. min = bin2bcd(min) & 0x7f;
  197. hrs = bin2bcd(hrs) & 0x3f;
  198. day = bin2bcd(day) & 0x3f;
  199. mon = bin2bcd(mon) & 0x1f;
  200. yrs = bin2bcd(yrs) & 0xff;
  201. cen = bin2bcd(cen) & 0xff;
  202. spin_lock_irqsave(&ds1511_lock, flags);
  203. rtc_disable_update();
  204. rtc_write(cen, RTC_CENTURY);
  205. rtc_write(yrs, RTC_YEAR);
  206. rtc_write((rtc_read(RTC_MON) & 0xe0) | mon, RTC_MON);
  207. rtc_write(day, RTC_DOM);
  208. rtc_write(hrs, RTC_HOUR);
  209. rtc_write(min, RTC_MIN);
  210. rtc_write(sec, RTC_SEC);
  211. rtc_write(dow, RTC_DOW);
  212. rtc_enable_update();
  213. spin_unlock_irqrestore(&ds1511_lock, flags);
  214. return 0;
  215. }
  216. static int ds1511_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
  217. {
  218. unsigned int century;
  219. unsigned long flags;
  220. spin_lock_irqsave(&ds1511_lock, flags);
  221. rtc_disable_update();
  222. rtc_tm->tm_sec = rtc_read(RTC_SEC) & 0x7f;
  223. rtc_tm->tm_min = rtc_read(RTC_MIN) & 0x7f;
  224. rtc_tm->tm_hour = rtc_read(RTC_HOUR) & 0x3f;
  225. rtc_tm->tm_mday = rtc_read(RTC_DOM) & 0x3f;
  226. rtc_tm->tm_wday = rtc_read(RTC_DOW) & 0x7;
  227. rtc_tm->tm_mon = rtc_read(RTC_MON) & 0x1f;
  228. rtc_tm->tm_year = rtc_read(RTC_YEAR) & 0x7f;
  229. century = rtc_read(RTC_CENTURY);
  230. rtc_enable_update();
  231. spin_unlock_irqrestore(&ds1511_lock, flags);
  232. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  233. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  234. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  235. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  236. rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
  237. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  238. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  239. century = bcd2bin(century) * 100;
  240. /*
  241. * Account for differences between how the RTC uses the values
  242. * and how they are defined in a struct rtc_time;
  243. */
  244. century += rtc_tm->tm_year;
  245. rtc_tm->tm_year = century - 1900;
  246. rtc_tm->tm_mon--;
  247. if (rtc_valid_tm(rtc_tm) < 0) {
  248. dev_err(dev, "retrieved date/time is not valid.\n");
  249. rtc_time_to_tm(0, rtc_tm);
  250. }
  251. return 0;
  252. }
  253. /*
  254. * write the alarm register settings
  255. *
  256. * we only have the use to interrupt every second, otherwise
  257. * known as the update interrupt, or the interrupt if the whole
  258. * date/hours/mins/secs matches. the ds1511 has many more
  259. * permutations, but the kernel doesn't.
  260. */
  261. static void
  262. ds1511_rtc_update_alarm(struct rtc_plat_data *pdata)
  263. {
  264. unsigned long flags;
  265. spin_lock_irqsave(&pdata->lock, flags);
  266. rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
  267. 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f,
  268. RTC_ALARM_DATE);
  269. rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
  270. 0x80 : bin2bcd(pdata->alrm_hour) & 0x3f,
  271. RTC_ALARM_HOUR);
  272. rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
  273. 0x80 : bin2bcd(pdata->alrm_min) & 0x7f,
  274. RTC_ALARM_MIN);
  275. rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
  276. 0x80 : bin2bcd(pdata->alrm_sec) & 0x7f,
  277. RTC_ALARM_SEC);
  278. rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD);
  279. rtc_read(RTC_CMD1); /* clear interrupts */
  280. spin_unlock_irqrestore(&pdata->lock, flags);
  281. }
  282. static int
  283. ds1511_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  284. {
  285. struct platform_device *pdev = to_platform_device(dev);
  286. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  287. if (pdata->irq <= 0)
  288. return -EINVAL;
  289. pdata->alrm_mday = alrm->time.tm_mday;
  290. pdata->alrm_hour = alrm->time.tm_hour;
  291. pdata->alrm_min = alrm->time.tm_min;
  292. pdata->alrm_sec = alrm->time.tm_sec;
  293. if (alrm->enabled)
  294. pdata->irqen |= RTC_AF;
  295. ds1511_rtc_update_alarm(pdata);
  296. return 0;
  297. }
  298. static int
  299. ds1511_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  300. {
  301. struct platform_device *pdev = to_platform_device(dev);
  302. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  303. if (pdata->irq <= 0)
  304. return -EINVAL;
  305. alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
  306. alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
  307. alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
  308. alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
  309. alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
  310. return 0;
  311. }
  312. static irqreturn_t
  313. ds1511_interrupt(int irq, void *dev_id)
  314. {
  315. struct platform_device *pdev = dev_id;
  316. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  317. unsigned long events = 0;
  318. spin_lock(&pdata->lock);
  319. /*
  320. * read and clear interrupt
  321. */
  322. if (rtc_read(RTC_CMD1) & DS1511_IRQF) {
  323. events = RTC_IRQF;
  324. if (rtc_read(RTC_ALARM_SEC) & 0x80)
  325. events |= RTC_UF;
  326. else
  327. events |= RTC_AF;
  328. if (likely(pdata->rtc))
  329. rtc_update_irq(pdata->rtc, 1, events);
  330. }
  331. spin_unlock(&pdata->lock);
  332. return events ? IRQ_HANDLED : IRQ_NONE;
  333. }
  334. static int ds1511_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  335. {
  336. struct platform_device *pdev = to_platform_device(dev);
  337. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  338. if (pdata->irq <= 0)
  339. return -EINVAL;
  340. if (enabled)
  341. pdata->irqen |= RTC_AF;
  342. else
  343. pdata->irqen &= ~RTC_AF;
  344. ds1511_rtc_update_alarm(pdata);
  345. return 0;
  346. }
  347. static const struct rtc_class_ops ds1511_rtc_ops = {
  348. .read_time = ds1511_rtc_read_time,
  349. .set_time = ds1511_rtc_set_time,
  350. .read_alarm = ds1511_rtc_read_alarm,
  351. .set_alarm = ds1511_rtc_set_alarm,
  352. .alarm_irq_enable = ds1511_rtc_alarm_irq_enable,
  353. };
  354. static ssize_t
  355. ds1511_nvram_read(struct file *filp, struct kobject *kobj,
  356. struct bin_attribute *ba,
  357. char *buf, loff_t pos, size_t size)
  358. {
  359. ssize_t count;
  360. /*
  361. * if count is more than one, turn on "burst" mode
  362. * turn it off when you're done
  363. */
  364. if (size > 1)
  365. rtc_write((rtc_read(RTC_CMD) | DS1511_BME), RTC_CMD);
  366. if (pos > DS1511_RAM_MAX)
  367. pos = DS1511_RAM_MAX;
  368. if (size + pos > DS1511_RAM_MAX + 1)
  369. size = DS1511_RAM_MAX - pos + 1;
  370. rtc_write(pos, DS1511_RAMADDR_LSB);
  371. for (count = 0; size > 0; count++, size--)
  372. *buf++ = rtc_read(DS1511_RAMDATA);
  373. if (count > 1)
  374. rtc_write((rtc_read(RTC_CMD) & ~DS1511_BME), RTC_CMD);
  375. return count;
  376. }
  377. static ssize_t
  378. ds1511_nvram_write(struct file *filp, struct kobject *kobj,
  379. struct bin_attribute *bin_attr,
  380. char *buf, loff_t pos, size_t size)
  381. {
  382. ssize_t count;
  383. /*
  384. * if count is more than one, turn on "burst" mode
  385. * turn it off when you're done
  386. */
  387. if (size > 1)
  388. rtc_write((rtc_read(RTC_CMD) | DS1511_BME), RTC_CMD);
  389. if (pos > DS1511_RAM_MAX)
  390. pos = DS1511_RAM_MAX;
  391. if (size + pos > DS1511_RAM_MAX + 1)
  392. size = DS1511_RAM_MAX - pos + 1;
  393. rtc_write(pos, DS1511_RAMADDR_LSB);
  394. for (count = 0; size > 0; count++, size--)
  395. rtc_write(*buf++, DS1511_RAMDATA);
  396. if (count > 1)
  397. rtc_write((rtc_read(RTC_CMD) & ~DS1511_BME), RTC_CMD);
  398. return count;
  399. }
  400. static struct bin_attribute ds1511_nvram_attr = {
  401. .attr = {
  402. .name = "nvram",
  403. .mode = S_IRUGO | S_IWUSR,
  404. },
  405. .size = DS1511_RAM_MAX,
  406. .read = ds1511_nvram_read,
  407. .write = ds1511_nvram_write,
  408. };
  409. static int ds1511_rtc_probe(struct platform_device *pdev)
  410. {
  411. struct rtc_device *rtc;
  412. struct resource *res;
  413. struct rtc_plat_data *pdata;
  414. int ret = 0;
  415. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  416. if (!res)
  417. return -ENODEV;
  418. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  419. if (!pdata)
  420. return -ENOMEM;
  421. pdata->size = resource_size(res);
  422. if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size,
  423. pdev->name))
  424. return -EBUSY;
  425. ds1511_base = devm_ioremap(&pdev->dev, res->start, pdata->size);
  426. if (!ds1511_base)
  427. return -ENOMEM;
  428. pdata->ioaddr = ds1511_base;
  429. pdata->irq = platform_get_irq(pdev, 0);
  430. /*
  431. * turn on the clock and the crystal, etc.
  432. */
  433. rtc_write(0, RTC_CMD);
  434. rtc_write(0, RTC_CMD1);
  435. /*
  436. * clear the wdog counter
  437. */
  438. rtc_write(0, DS1511_WD_MSEC);
  439. rtc_write(0, DS1511_WD_SEC);
  440. /*
  441. * start the clock
  442. */
  443. rtc_enable_update();
  444. /*
  445. * check for a dying bat-tree
  446. */
  447. if (rtc_read(RTC_CMD1) & DS1511_BLF1)
  448. dev_warn(&pdev->dev, "voltage-low detected.\n");
  449. spin_lock_init(&pdata->lock);
  450. platform_set_drvdata(pdev, pdata);
  451. /*
  452. * if the platform has an interrupt in mind for this device,
  453. * then by all means, set it
  454. */
  455. if (pdata->irq > 0) {
  456. rtc_read(RTC_CMD1);
  457. if (devm_request_irq(&pdev->dev, pdata->irq, ds1511_interrupt,
  458. IRQF_SHARED, pdev->name, pdev) < 0) {
  459. dev_warn(&pdev->dev, "interrupt not available.\n");
  460. pdata->irq = 0;
  461. }
  462. }
  463. rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &ds1511_rtc_ops,
  464. THIS_MODULE);
  465. if (IS_ERR(rtc))
  466. return PTR_ERR(rtc);
  467. pdata->rtc = rtc;
  468. ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr);
  469. return ret;
  470. }
  471. static int ds1511_rtc_remove(struct platform_device *pdev)
  472. {
  473. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  474. sysfs_remove_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr);
  475. if (pdata->irq > 0) {
  476. /*
  477. * disable the alarm interrupt
  478. */
  479. rtc_write(rtc_read(RTC_CMD) & ~RTC_TIE, RTC_CMD);
  480. rtc_read(RTC_CMD1);
  481. }
  482. return 0;
  483. }
  484. /* work with hotplug and coldplug */
  485. MODULE_ALIAS("platform:ds1511");
  486. static struct platform_driver ds1511_rtc_driver = {
  487. .probe = ds1511_rtc_probe,
  488. .remove = ds1511_rtc_remove,
  489. .driver = {
  490. .name = "ds1511",
  491. .owner = THIS_MODULE,
  492. },
  493. };
  494. module_platform_driver(ds1511_rtc_driver);
  495. MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
  496. MODULE_DESCRIPTION("Dallas DS1511 RTC driver");
  497. MODULE_LICENSE("GPL");
  498. MODULE_VERSION(DRV_VERSION);