rtc-ds1511.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. }
  177. if (rtc_tm->tm_year < 1970) {
  178. return -EINVAL;
  179. }
  180. yrs = rtc_tm->tm_year % 100;
  181. cen = rtc_tm->tm_year / 100;
  182. mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
  183. day = rtc_tm->tm_mday;
  184. dow = rtc_tm->tm_wday & 0x7; /* automatic BCD */
  185. hrs = rtc_tm->tm_hour;
  186. min = rtc_tm->tm_min;
  187. sec = rtc_tm->tm_sec;
  188. if ((mon > 12) || (day == 0)) {
  189. return -EINVAL;
  190. }
  191. if (day > rtc_month_days(rtc_tm->tm_mon, rtc_tm->tm_year)) {
  192. return -EINVAL;
  193. }
  194. if ((hrs >= 24) || (min >= 60) || (sec >= 60)) {
  195. return -EINVAL;
  196. }
  197. /*
  198. * each register is a different number of valid bits
  199. */
  200. sec = bin2bcd(sec) & 0x7f;
  201. min = bin2bcd(min) & 0x7f;
  202. hrs = bin2bcd(hrs) & 0x3f;
  203. day = bin2bcd(day) & 0x3f;
  204. mon = bin2bcd(mon) & 0x1f;
  205. yrs = bin2bcd(yrs) & 0xff;
  206. cen = bin2bcd(cen) & 0xff;
  207. spin_lock_irqsave(&ds1511_lock, flags);
  208. rtc_disable_update();
  209. rtc_write(cen, RTC_CENTURY);
  210. rtc_write(yrs, RTC_YEAR);
  211. rtc_write((rtc_read(RTC_MON) & 0xe0) | mon, RTC_MON);
  212. rtc_write(day, RTC_DOM);
  213. rtc_write(hrs, RTC_HOUR);
  214. rtc_write(min, RTC_MIN);
  215. rtc_write(sec, RTC_SEC);
  216. rtc_write(dow, RTC_DOW);
  217. rtc_enable_update();
  218. spin_unlock_irqrestore(&ds1511_lock, flags);
  219. return 0;
  220. }
  221. static int ds1511_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
  222. {
  223. unsigned int century;
  224. unsigned long flags;
  225. spin_lock_irqsave(&ds1511_lock, flags);
  226. rtc_disable_update();
  227. rtc_tm->tm_sec = rtc_read(RTC_SEC) & 0x7f;
  228. rtc_tm->tm_min = rtc_read(RTC_MIN) & 0x7f;
  229. rtc_tm->tm_hour = rtc_read(RTC_HOUR) & 0x3f;
  230. rtc_tm->tm_mday = rtc_read(RTC_DOM) & 0x3f;
  231. rtc_tm->tm_wday = rtc_read(RTC_DOW) & 0x7;
  232. rtc_tm->tm_mon = rtc_read(RTC_MON) & 0x1f;
  233. rtc_tm->tm_year = rtc_read(RTC_YEAR) & 0x7f;
  234. century = rtc_read(RTC_CENTURY);
  235. rtc_enable_update();
  236. spin_unlock_irqrestore(&ds1511_lock, flags);
  237. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  238. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  239. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  240. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  241. rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
  242. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  243. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  244. century = bcd2bin(century) * 100;
  245. /*
  246. * Account for differences between how the RTC uses the values
  247. * and how they are defined in a struct rtc_time;
  248. */
  249. century += rtc_tm->tm_year;
  250. rtc_tm->tm_year = century - 1900;
  251. rtc_tm->tm_mon--;
  252. if (rtc_valid_tm(rtc_tm) < 0) {
  253. dev_err(dev, "retrieved date/time is not valid.\n");
  254. rtc_time_to_tm(0, rtc_tm);
  255. }
  256. return 0;
  257. }
  258. /*
  259. * write the alarm register settings
  260. *
  261. * we only have the use to interrupt every second, otherwise
  262. * known as the update interrupt, or the interrupt if the whole
  263. * date/hours/mins/secs matches. the ds1511 has many more
  264. * permutations, but the kernel doesn't.
  265. */
  266. static void
  267. ds1511_rtc_update_alarm(struct rtc_plat_data *pdata)
  268. {
  269. unsigned long flags;
  270. spin_lock_irqsave(&pdata->lock, flags);
  271. rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
  272. 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f,
  273. RTC_ALARM_DATE);
  274. rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
  275. 0x80 : bin2bcd(pdata->alrm_hour) & 0x3f,
  276. RTC_ALARM_HOUR);
  277. rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
  278. 0x80 : bin2bcd(pdata->alrm_min) & 0x7f,
  279. RTC_ALARM_MIN);
  280. rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
  281. 0x80 : bin2bcd(pdata->alrm_sec) & 0x7f,
  282. RTC_ALARM_SEC);
  283. rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD);
  284. rtc_read(RTC_CMD1); /* clear interrupts */
  285. spin_unlock_irqrestore(&pdata->lock, flags);
  286. }
  287. static int
  288. ds1511_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  289. {
  290. struct platform_device *pdev = to_platform_device(dev);
  291. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  292. if (pdata->irq <= 0)
  293. return -EINVAL;
  294. pdata->alrm_mday = alrm->time.tm_mday;
  295. pdata->alrm_hour = alrm->time.tm_hour;
  296. pdata->alrm_min = alrm->time.tm_min;
  297. pdata->alrm_sec = alrm->time.tm_sec;
  298. if (alrm->enabled) {
  299. pdata->irqen |= RTC_AF;
  300. }
  301. ds1511_rtc_update_alarm(pdata);
  302. return 0;
  303. }
  304. static int
  305. ds1511_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  306. {
  307. struct platform_device *pdev = to_platform_device(dev);
  308. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  309. if (pdata->irq <= 0)
  310. return -EINVAL;
  311. alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
  312. alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
  313. alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
  314. alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
  315. alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
  316. return 0;
  317. }
  318. static irqreturn_t
  319. ds1511_interrupt(int irq, void *dev_id)
  320. {
  321. struct platform_device *pdev = dev_id;
  322. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  323. unsigned long events = 0;
  324. spin_lock(&pdata->lock);
  325. /*
  326. * read and clear interrupt
  327. */
  328. if (rtc_read(RTC_CMD1) & DS1511_IRQF) {
  329. events = RTC_IRQF;
  330. if (rtc_read(RTC_ALARM_SEC) & 0x80)
  331. events |= RTC_UF;
  332. else
  333. events |= RTC_AF;
  334. if (likely(pdata->rtc))
  335. rtc_update_irq(pdata->rtc, 1, events);
  336. }
  337. spin_unlock(&pdata->lock);
  338. return events ? IRQ_HANDLED : IRQ_NONE;
  339. }
  340. static int ds1511_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  341. {
  342. struct platform_device *pdev = to_platform_device(dev);
  343. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  344. if (pdata->irq <= 0)
  345. return -EINVAL;
  346. if (enabled)
  347. pdata->irqen |= RTC_AF;
  348. else
  349. pdata->irqen &= ~RTC_AF;
  350. ds1511_rtc_update_alarm(pdata);
  351. return 0;
  352. }
  353. static const struct rtc_class_ops ds1511_rtc_ops = {
  354. .read_time = ds1511_rtc_read_time,
  355. .set_time = ds1511_rtc_set_time,
  356. .read_alarm = ds1511_rtc_read_alarm,
  357. .set_alarm = ds1511_rtc_set_alarm,
  358. .alarm_irq_enable = ds1511_rtc_alarm_irq_enable,
  359. };
  360. static ssize_t
  361. ds1511_nvram_read(struct file *filp, struct kobject *kobj,
  362. struct bin_attribute *ba,
  363. char *buf, loff_t pos, size_t size)
  364. {
  365. ssize_t count;
  366. /*
  367. * if count is more than one, turn on "burst" mode
  368. * turn it off when you're done
  369. */
  370. if (size > 1) {
  371. rtc_write((rtc_read(RTC_CMD) | DS1511_BME), RTC_CMD);
  372. }
  373. if (pos > DS1511_RAM_MAX) {
  374. pos = DS1511_RAM_MAX;
  375. }
  376. if (size + pos > DS1511_RAM_MAX + 1) {
  377. size = DS1511_RAM_MAX - pos + 1;
  378. }
  379. rtc_write(pos, DS1511_RAMADDR_LSB);
  380. for (count = 0; size > 0; count++, size--) {
  381. *buf++ = rtc_read(DS1511_RAMDATA);
  382. }
  383. if (count > 1) {
  384. rtc_write((rtc_read(RTC_CMD) & ~DS1511_BME), RTC_CMD);
  385. }
  386. return count;
  387. }
  388. static ssize_t
  389. ds1511_nvram_write(struct file *filp, struct kobject *kobj,
  390. struct bin_attribute *bin_attr,
  391. char *buf, loff_t pos, size_t size)
  392. {
  393. ssize_t count;
  394. /*
  395. * if count is more than one, turn on "burst" mode
  396. * turn it off when you're done
  397. */
  398. if (size > 1) {
  399. rtc_write((rtc_read(RTC_CMD) | DS1511_BME), RTC_CMD);
  400. }
  401. if (pos > DS1511_RAM_MAX) {
  402. pos = DS1511_RAM_MAX;
  403. }
  404. if (size + pos > DS1511_RAM_MAX + 1) {
  405. size = DS1511_RAM_MAX - pos + 1;
  406. }
  407. rtc_write(pos, DS1511_RAMADDR_LSB);
  408. for (count = 0; size > 0; count++, size--) {
  409. rtc_write(*buf++, DS1511_RAMDATA);
  410. }
  411. if (count > 1) {
  412. rtc_write((rtc_read(RTC_CMD) & ~DS1511_BME), RTC_CMD);
  413. }
  414. return count;
  415. }
  416. static struct bin_attribute ds1511_nvram_attr = {
  417. .attr = {
  418. .name = "nvram",
  419. .mode = S_IRUGO | S_IWUSR,
  420. },
  421. .size = DS1511_RAM_MAX,
  422. .read = ds1511_nvram_read,
  423. .write = ds1511_nvram_write,
  424. };
  425. static int __devinit
  426. ds1511_rtc_probe(struct platform_device *pdev)
  427. {
  428. struct rtc_device *rtc;
  429. struct resource *res;
  430. struct rtc_plat_data *pdata;
  431. int ret = 0;
  432. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  433. if (!res) {
  434. return -ENODEV;
  435. }
  436. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  437. if (!pdata)
  438. return -ENOMEM;
  439. pdata->size = resource_size(res);
  440. if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size,
  441. pdev->name))
  442. return -EBUSY;
  443. ds1511_base = devm_ioremap(&pdev->dev, res->start, pdata->size);
  444. if (!ds1511_base)
  445. return -ENOMEM;
  446. pdata->ioaddr = ds1511_base;
  447. pdata->irq = platform_get_irq(pdev, 0);
  448. /*
  449. * turn on the clock and the crystal, etc.
  450. */
  451. rtc_write(0, RTC_CMD);
  452. rtc_write(0, RTC_CMD1);
  453. /*
  454. * clear the wdog counter
  455. */
  456. rtc_write(0, DS1511_WD_MSEC);
  457. rtc_write(0, DS1511_WD_SEC);
  458. /*
  459. * start the clock
  460. */
  461. rtc_enable_update();
  462. /*
  463. * check for a dying bat-tree
  464. */
  465. if (rtc_read(RTC_CMD1) & DS1511_BLF1) {
  466. dev_warn(&pdev->dev, "voltage-low detected.\n");
  467. }
  468. spin_lock_init(&pdata->lock);
  469. platform_set_drvdata(pdev, pdata);
  470. /*
  471. * if the platform has an interrupt in mind for this device,
  472. * then by all means, set it
  473. */
  474. if (pdata->irq > 0) {
  475. rtc_read(RTC_CMD1);
  476. if (devm_request_irq(&pdev->dev, pdata->irq, ds1511_interrupt,
  477. IRQF_SHARED, pdev->name, pdev) < 0) {
  478. dev_warn(&pdev->dev, "interrupt not available.\n");
  479. pdata->irq = 0;
  480. }
  481. }
  482. rtc = rtc_device_register(pdev->name, &pdev->dev, &ds1511_rtc_ops,
  483. THIS_MODULE);
  484. if (IS_ERR(rtc))
  485. return PTR_ERR(rtc);
  486. pdata->rtc = rtc;
  487. ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr);
  488. if (ret)
  489. rtc_device_unregister(pdata->rtc);
  490. return ret;
  491. }
  492. static int __devexit
  493. ds1511_rtc_remove(struct platform_device *pdev)
  494. {
  495. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  496. sysfs_remove_bin_file(&pdev->dev.kobj, &ds1511_nvram_attr);
  497. rtc_device_unregister(pdata->rtc);
  498. if (pdata->irq > 0) {
  499. /*
  500. * disable the alarm interrupt
  501. */
  502. rtc_write(rtc_read(RTC_CMD) & ~RTC_TIE, RTC_CMD);
  503. rtc_read(RTC_CMD1);
  504. }
  505. return 0;
  506. }
  507. /* work with hotplug and coldplug */
  508. MODULE_ALIAS("platform:ds1511");
  509. static struct platform_driver ds1511_rtc_driver = {
  510. .probe = ds1511_rtc_probe,
  511. .remove = __devexit_p(ds1511_rtc_remove),
  512. .driver = {
  513. .name = "ds1511",
  514. .owner = THIS_MODULE,
  515. },
  516. };
  517. module_platform_driver(ds1511_rtc_driver);
  518. MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
  519. MODULE_DESCRIPTION("Dallas DS1511 RTC driver");
  520. MODULE_LICENSE("GPL");
  521. MODULE_VERSION(DRV_VERSION);