rtc-ds1305.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * rtc-ds1305.c -- driver for DS1305 and DS1306 SPI RTC chips
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/bcd.h>
  14. #include <linux/slab.h>
  15. #include <linux/rtc.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/spi/ds1305.h>
  19. /*
  20. * Registers ... mask DS1305_WRITE into register address to write,
  21. * otherwise you're reading it. All non-bitmask values are BCD.
  22. */
  23. #define DS1305_WRITE 0x80
  24. /* RTC date/time ... the main special cases are that we:
  25. * - Need fancy "hours" encoding in 12hour mode
  26. * - Don't rely on the "day-of-week" field (or tm_wday)
  27. * - Are a 21st-century clock (2000 <= year < 2100)
  28. */
  29. #define DS1305_RTC_LEN 7 /* bytes for RTC regs */
  30. #define DS1305_SEC 0x00 /* register addresses */
  31. #define DS1305_MIN 0x01
  32. #define DS1305_HOUR 0x02
  33. # define DS1305_HR_12 0x40 /* set == 12 hr mode */
  34. # define DS1305_HR_PM 0x20 /* set == PM (12hr mode) */
  35. #define DS1305_WDAY 0x03
  36. #define DS1305_MDAY 0x04
  37. #define DS1305_MON 0x05
  38. #define DS1305_YEAR 0x06
  39. /* The two alarms have only sec/min/hour/wday fields (ALM_LEN).
  40. * DS1305_ALM_DISABLE disables a match field (some combos are bad).
  41. *
  42. * NOTE that since we don't use WDAY, we limit ourselves to alarms
  43. * only one day into the future (vs potentially up to a week).
  44. *
  45. * NOTE ALSO that while we could generate once-a-second IRQs (UIE), we
  46. * don't currently support them. We'd either need to do it only when
  47. * no alarm is pending (not the standard model), or to use the second
  48. * alarm (implying that this is a DS1305 not DS1306, *and* that either
  49. * it's wired up a second IRQ we know, or that INTCN is set)
  50. */
  51. #define DS1305_ALM_LEN 4 /* bytes for ALM regs */
  52. #define DS1305_ALM_DISABLE 0x80
  53. #define DS1305_ALM0(r) (0x07 + (r)) /* register addresses */
  54. #define DS1305_ALM1(r) (0x0b + (r))
  55. /* three control registers */
  56. #define DS1305_CONTROL_LEN 3 /* bytes of control regs */
  57. #define DS1305_CONTROL 0x0f /* register addresses */
  58. # define DS1305_nEOSC 0x80 /* low enables oscillator */
  59. # define DS1305_WP 0x40 /* write protect */
  60. # define DS1305_INTCN 0x04 /* clear == only int0 used */
  61. # define DS1306_1HZ 0x04 /* enable 1Hz output */
  62. # define DS1305_AEI1 0x02 /* enable ALM1 IRQ */
  63. # define DS1305_AEI0 0x01 /* enable ALM0 IRQ */
  64. #define DS1305_STATUS 0x10
  65. /* status has just AEIx bits, mirrored as IRQFx */
  66. #define DS1305_TRICKLE 0x11
  67. /* trickle bits are defined in <linux/spi/ds1305.h> */
  68. /* a bunch of NVRAM */
  69. #define DS1305_NVRAM_LEN 96 /* bytes of NVRAM */
  70. #define DS1305_NVRAM 0x20 /* register addresses */
  71. struct ds1305 {
  72. struct spi_device *spi;
  73. struct rtc_device *rtc;
  74. struct work_struct work;
  75. unsigned long flags;
  76. #define FLAG_EXITING 0
  77. bool hr12;
  78. u8 ctrl[DS1305_CONTROL_LEN];
  79. };
  80. /*----------------------------------------------------------------------*/
  81. /*
  82. * Utilities ... tolerate 12-hour AM/PM notation in case of non-Linux
  83. * software (like a bootloader) which may require it.
  84. */
  85. static unsigned bcd2hour(u8 bcd)
  86. {
  87. if (bcd & DS1305_HR_12) {
  88. unsigned hour = 0;
  89. bcd &= ~DS1305_HR_12;
  90. if (bcd & DS1305_HR_PM) {
  91. hour = 12;
  92. bcd &= ~DS1305_HR_PM;
  93. }
  94. hour += bcd2bin(bcd);
  95. return hour - 1;
  96. }
  97. return bcd2bin(bcd);
  98. }
  99. static u8 hour2bcd(bool hr12, int hour)
  100. {
  101. if (hr12) {
  102. hour++;
  103. if (hour <= 12)
  104. return DS1305_HR_12 | bin2bcd(hour);
  105. hour -= 12;
  106. return DS1305_HR_12 | DS1305_HR_PM | bin2bcd(hour);
  107. }
  108. return bin2bcd(hour);
  109. }
  110. /*----------------------------------------------------------------------*/
  111. /*
  112. * Interface to RTC framework
  113. */
  114. static int ds1305_alarm_irq_enable(struct device *dev, unsigned int enabled)
  115. {
  116. struct ds1305 *ds1305 = dev_get_drvdata(dev);
  117. u8 buf[2];
  118. long err = -EINVAL;
  119. buf[0] = DS1305_WRITE | DS1305_CONTROL;
  120. buf[1] = ds1305->ctrl[0];
  121. if (enabled) {
  122. if (ds1305->ctrl[0] & DS1305_AEI0)
  123. goto done;
  124. buf[1] |= DS1305_AEI0;
  125. } else {
  126. if (!(buf[1] & DS1305_AEI0))
  127. goto done;
  128. buf[1] &= ~DS1305_AEI0;
  129. }
  130. err = spi_write_then_read(ds1305->spi, buf, sizeof buf, NULL, 0);
  131. if (err >= 0)
  132. ds1305->ctrl[0] = buf[1];
  133. done:
  134. return err;
  135. }
  136. /*
  137. * Get/set of date and time is pretty normal.
  138. */
  139. static int ds1305_get_time(struct device *dev, struct rtc_time *time)
  140. {
  141. struct ds1305 *ds1305 = dev_get_drvdata(dev);
  142. u8 addr = DS1305_SEC;
  143. u8 buf[DS1305_RTC_LEN];
  144. int status;
  145. /* Use write-then-read to get all the date/time registers
  146. * since dma from stack is nonportable
  147. */
  148. status = spi_write_then_read(ds1305->spi, &addr, sizeof addr,
  149. buf, sizeof buf);
  150. if (status < 0)
  151. return status;
  152. dev_vdbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
  153. "read", buf[0], buf[1], buf[2], buf[3],
  154. buf[4], buf[5], buf[6]);
  155. /* Decode the registers */
  156. time->tm_sec = bcd2bin(buf[DS1305_SEC]);
  157. time->tm_min = bcd2bin(buf[DS1305_MIN]);
  158. time->tm_hour = bcd2hour(buf[DS1305_HOUR]);
  159. time->tm_wday = buf[DS1305_WDAY] - 1;
  160. time->tm_mday = bcd2bin(buf[DS1305_MDAY]);
  161. time->tm_mon = bcd2bin(buf[DS1305_MON]) - 1;
  162. time->tm_year = bcd2bin(buf[DS1305_YEAR]) + 100;
  163. dev_vdbg(dev, "%s secs=%d, mins=%d, "
  164. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  165. "read", time->tm_sec, time->tm_min,
  166. time->tm_hour, time->tm_mday,
  167. time->tm_mon, time->tm_year, time->tm_wday);
  168. /* Time may not be set */
  169. return rtc_valid_tm(time);
  170. }
  171. static int ds1305_set_time(struct device *dev, struct rtc_time *time)
  172. {
  173. struct ds1305 *ds1305 = dev_get_drvdata(dev);
  174. u8 buf[1 + DS1305_RTC_LEN];
  175. u8 *bp = buf;
  176. dev_vdbg(dev, "%s secs=%d, mins=%d, "
  177. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  178. "write", time->tm_sec, time->tm_min,
  179. time->tm_hour, time->tm_mday,
  180. time->tm_mon, time->tm_year, time->tm_wday);
  181. /* Write registers starting at the first time/date address. */
  182. *bp++ = DS1305_WRITE | DS1305_SEC;
  183. *bp++ = bin2bcd(time->tm_sec);
  184. *bp++ = bin2bcd(time->tm_min);
  185. *bp++ = hour2bcd(ds1305->hr12, time->tm_hour);
  186. *bp++ = (time->tm_wday < 7) ? (time->tm_wday + 1) : 1;
  187. *bp++ = bin2bcd(time->tm_mday);
  188. *bp++ = bin2bcd(time->tm_mon + 1);
  189. *bp++ = bin2bcd(time->tm_year - 100);
  190. dev_dbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
  191. "write", buf[1], buf[2], buf[3],
  192. buf[4], buf[5], buf[6], buf[7]);
  193. /* use write-then-read since dma from stack is nonportable */
  194. return spi_write_then_read(ds1305->spi, buf, sizeof buf,
  195. NULL, 0);
  196. }
  197. /*
  198. * Get/set of alarm is a bit funky:
  199. *
  200. * - First there's the inherent raciness of getting the (partitioned)
  201. * status of an alarm that could trigger while we're reading parts
  202. * of that status.
  203. *
  204. * - Second there's its limited range (we could increase it a bit by
  205. * relying on WDAY), which means it will easily roll over.
  206. *
  207. * - Third there's the choice of two alarms and alarm signals.
  208. * Here we use ALM0 and expect that nINT0 (open drain) is used;
  209. * that's the only real option for DS1306 runtime alarms, and is
  210. * natural on DS1305.
  211. *
  212. * - Fourth, there's also ALM1, and a second interrupt signal:
  213. * + On DS1305 ALM1 uses nINT1 (when INTCN=1) else nINT0;
  214. * + On DS1306 ALM1 only uses INT1 (an active high pulse)
  215. * and it won't work when VCC1 is active.
  216. *
  217. * So to be most general, we should probably set both alarms to the
  218. * same value, letting ALM1 be the wakeup event source on DS1306
  219. * and handling several wiring options on DS1305.
  220. *
  221. * - Fifth, we support the polled mode (as well as possible; why not?)
  222. * even when no interrupt line is wired to an IRQ.
  223. */
  224. /*
  225. * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
  226. */
  227. static int ds1305_get_alarm(struct device *dev, struct rtc_wkalrm *alm)
  228. {
  229. struct ds1305 *ds1305 = dev_get_drvdata(dev);
  230. struct spi_device *spi = ds1305->spi;
  231. u8 addr;
  232. int status;
  233. u8 buf[DS1305_ALM_LEN];
  234. /* Refresh control register cache BEFORE reading ALM0 registers,
  235. * since reading alarm registers acks any pending IRQ. That
  236. * makes returning "pending" status a bit of a lie, but that bit
  237. * of EFI status is at best fragile anyway (given IRQ handlers).
  238. */
  239. addr = DS1305_CONTROL;
  240. status = spi_write_then_read(spi, &addr, sizeof addr,
  241. ds1305->ctrl, sizeof ds1305->ctrl);
  242. if (status < 0)
  243. return status;
  244. alm->enabled = !!(ds1305->ctrl[0] & DS1305_AEI0);
  245. alm->pending = !!(ds1305->ctrl[1] & DS1305_AEI0);
  246. /* get and check ALM0 registers */
  247. addr = DS1305_ALM0(DS1305_SEC);
  248. status = spi_write_then_read(spi, &addr, sizeof addr,
  249. buf, sizeof buf);
  250. if (status < 0)
  251. return status;
  252. dev_vdbg(dev, "%s: %02x %02x %02x %02x\n",
  253. "alm0 read", buf[DS1305_SEC], buf[DS1305_MIN],
  254. buf[DS1305_HOUR], buf[DS1305_WDAY]);
  255. if ((DS1305_ALM_DISABLE & buf[DS1305_SEC])
  256. || (DS1305_ALM_DISABLE & buf[DS1305_MIN])
  257. || (DS1305_ALM_DISABLE & buf[DS1305_HOUR]))
  258. return -EIO;
  259. /* Stuff these values into alm->time and let RTC framework code
  260. * fill in the rest ... and also handle rollover to tomorrow when
  261. * that's needed.
  262. */
  263. alm->time.tm_sec = bcd2bin(buf[DS1305_SEC]);
  264. alm->time.tm_min = bcd2bin(buf[DS1305_MIN]);
  265. alm->time.tm_hour = bcd2hour(buf[DS1305_HOUR]);
  266. alm->time.tm_mday = -1;
  267. alm->time.tm_mon = -1;
  268. alm->time.tm_year = -1;
  269. /* next three fields are unused by Linux */
  270. alm->time.tm_wday = -1;
  271. alm->time.tm_mday = -1;
  272. alm->time.tm_isdst = -1;
  273. return 0;
  274. }
  275. /*
  276. * Context: caller holds rtc->ops_lock (to protect ds1305->ctrl)
  277. */
  278. static int ds1305_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  279. {
  280. struct ds1305 *ds1305 = dev_get_drvdata(dev);
  281. struct spi_device *spi = ds1305->spi;
  282. unsigned long now, later;
  283. struct rtc_time tm;
  284. int status;
  285. u8 buf[1 + DS1305_ALM_LEN];
  286. /* convert desired alarm to time_t */
  287. status = rtc_tm_to_time(&alm->time, &later);
  288. if (status < 0)
  289. return status;
  290. /* Read current time as time_t */
  291. status = ds1305_get_time(dev, &tm);
  292. if (status < 0)
  293. return status;
  294. status = rtc_tm_to_time(&tm, &now);
  295. if (status < 0)
  296. return status;
  297. /* make sure alarm fires within the next 24 hours */
  298. if (later <= now)
  299. return -EINVAL;
  300. if ((later - now) > 24 * 60 * 60)
  301. return -EDOM;
  302. /* disable alarm if needed */
  303. if (ds1305->ctrl[0] & DS1305_AEI0) {
  304. ds1305->ctrl[0] &= ~DS1305_AEI0;
  305. buf[0] = DS1305_WRITE | DS1305_CONTROL;
  306. buf[1] = ds1305->ctrl[0];
  307. status = spi_write_then_read(ds1305->spi, buf, 2, NULL, 0);
  308. if (status < 0)
  309. return status;
  310. }
  311. /* write alarm */
  312. buf[0] = DS1305_WRITE | DS1305_ALM0(DS1305_SEC);
  313. buf[1 + DS1305_SEC] = bin2bcd(alm->time.tm_sec);
  314. buf[1 + DS1305_MIN] = bin2bcd(alm->time.tm_min);
  315. buf[1 + DS1305_HOUR] = hour2bcd(ds1305->hr12, alm->time.tm_hour);
  316. buf[1 + DS1305_WDAY] = DS1305_ALM_DISABLE;
  317. dev_dbg(dev, "%s: %02x %02x %02x %02x\n",
  318. "alm0 write", buf[1 + DS1305_SEC], buf[1 + DS1305_MIN],
  319. buf[1 + DS1305_HOUR], buf[1 + DS1305_WDAY]);
  320. status = spi_write_then_read(spi, buf, sizeof buf, NULL, 0);
  321. if (status < 0)
  322. return status;
  323. /* enable alarm if requested */
  324. if (alm->enabled) {
  325. ds1305->ctrl[0] |= DS1305_AEI0;
  326. buf[0] = DS1305_WRITE | DS1305_CONTROL;
  327. buf[1] = ds1305->ctrl[0];
  328. status = spi_write_then_read(ds1305->spi, buf, 2, NULL, 0);
  329. }
  330. return status;
  331. }
  332. #ifdef CONFIG_PROC_FS
  333. static int ds1305_proc(struct device *dev, struct seq_file *seq)
  334. {
  335. struct ds1305 *ds1305 = dev_get_drvdata(dev);
  336. char *diodes = "no";
  337. char *resistors = "";
  338. /* ctrl[2] is treated as read-only; no locking needed */
  339. if ((ds1305->ctrl[2] & 0xf0) == DS1305_TRICKLE_MAGIC) {
  340. switch (ds1305->ctrl[2] & 0x0c) {
  341. case DS1305_TRICKLE_DS2:
  342. diodes = "2 diodes, ";
  343. break;
  344. case DS1305_TRICKLE_DS1:
  345. diodes = "1 diode, ";
  346. break;
  347. default:
  348. goto done;
  349. }
  350. switch (ds1305->ctrl[2] & 0x03) {
  351. case DS1305_TRICKLE_2K:
  352. resistors = "2k Ohm";
  353. break;
  354. case DS1305_TRICKLE_4K:
  355. resistors = "4k Ohm";
  356. break;
  357. case DS1305_TRICKLE_8K:
  358. resistors = "8k Ohm";
  359. break;
  360. default:
  361. diodes = "no";
  362. break;
  363. }
  364. }
  365. done:
  366. return seq_printf(seq,
  367. "trickle_charge\t: %s%s\n",
  368. diodes, resistors);
  369. }
  370. #else
  371. #define ds1305_proc NULL
  372. #endif
  373. static const struct rtc_class_ops ds1305_ops = {
  374. .read_time = ds1305_get_time,
  375. .set_time = ds1305_set_time,
  376. .read_alarm = ds1305_get_alarm,
  377. .set_alarm = ds1305_set_alarm,
  378. .proc = ds1305_proc,
  379. .alarm_irq_enable = ds1305_alarm_irq_enable,
  380. };
  381. static void ds1305_work(struct work_struct *work)
  382. {
  383. struct ds1305 *ds1305 = container_of(work, struct ds1305, work);
  384. struct mutex *lock = &ds1305->rtc->ops_lock;
  385. struct spi_device *spi = ds1305->spi;
  386. u8 buf[3];
  387. int status;
  388. /* lock to protect ds1305->ctrl */
  389. mutex_lock(lock);
  390. /* Disable the IRQ, and clear its status ... for now, we "know"
  391. * that if more than one alarm is active, they're in sync.
  392. * Note that reading ALM data registers also clears IRQ status.
  393. */
  394. ds1305->ctrl[0] &= ~(DS1305_AEI1 | DS1305_AEI0);
  395. ds1305->ctrl[1] = 0;
  396. buf[0] = DS1305_WRITE | DS1305_CONTROL;
  397. buf[1] = ds1305->ctrl[0];
  398. buf[2] = 0;
  399. status = spi_write_then_read(spi, buf, sizeof buf,
  400. NULL, 0);
  401. if (status < 0)
  402. dev_dbg(&spi->dev, "clear irq --> %d\n", status);
  403. mutex_unlock(lock);
  404. if (!test_bit(FLAG_EXITING, &ds1305->flags))
  405. enable_irq(spi->irq);
  406. rtc_update_irq(ds1305->rtc, 1, RTC_AF | RTC_IRQF);
  407. }
  408. /*
  409. * This "real" IRQ handler hands off to a workqueue mostly to allow
  410. * mutex locking for ds1305->ctrl ... unlike I2C, we could issue async
  411. * I/O requests in IRQ context (to clear the IRQ status).
  412. */
  413. static irqreturn_t ds1305_irq(int irq, void *p)
  414. {
  415. struct ds1305 *ds1305 = p;
  416. disable_irq(irq);
  417. schedule_work(&ds1305->work);
  418. return IRQ_HANDLED;
  419. }
  420. /*----------------------------------------------------------------------*/
  421. /*
  422. * Interface for NVRAM
  423. */
  424. static void msg_init(struct spi_message *m, struct spi_transfer *x,
  425. u8 *addr, size_t count, char *tx, char *rx)
  426. {
  427. spi_message_init(m);
  428. memset(x, 0, 2 * sizeof(*x));
  429. x->tx_buf = addr;
  430. x->len = 1;
  431. spi_message_add_tail(x, m);
  432. x++;
  433. x->tx_buf = tx;
  434. x->rx_buf = rx;
  435. x->len = count;
  436. spi_message_add_tail(x, m);
  437. }
  438. static ssize_t
  439. ds1305_nvram_read(struct file *filp, struct kobject *kobj,
  440. struct bin_attribute *attr,
  441. char *buf, loff_t off, size_t count)
  442. {
  443. struct spi_device *spi;
  444. u8 addr;
  445. struct spi_message m;
  446. struct spi_transfer x[2];
  447. int status;
  448. spi = container_of(kobj, struct spi_device, dev.kobj);
  449. if (unlikely(off >= DS1305_NVRAM_LEN))
  450. return 0;
  451. if (count >= DS1305_NVRAM_LEN)
  452. count = DS1305_NVRAM_LEN;
  453. if ((off + count) > DS1305_NVRAM_LEN)
  454. count = DS1305_NVRAM_LEN - off;
  455. if (unlikely(!count))
  456. return count;
  457. addr = DS1305_NVRAM + off;
  458. msg_init(&m, x, &addr, count, NULL, buf);
  459. status = spi_sync(spi, &m);
  460. if (status < 0)
  461. dev_err(&spi->dev, "nvram %s error %d\n", "read", status);
  462. return (status < 0) ? status : count;
  463. }
  464. static ssize_t
  465. ds1305_nvram_write(struct file *filp, struct kobject *kobj,
  466. struct bin_attribute *attr,
  467. char *buf, loff_t off, size_t count)
  468. {
  469. struct spi_device *spi;
  470. u8 addr;
  471. struct spi_message m;
  472. struct spi_transfer x[2];
  473. int status;
  474. spi = container_of(kobj, struct spi_device, dev.kobj);
  475. if (unlikely(off >= DS1305_NVRAM_LEN))
  476. return -EFBIG;
  477. if (count >= DS1305_NVRAM_LEN)
  478. count = DS1305_NVRAM_LEN;
  479. if ((off + count) > DS1305_NVRAM_LEN)
  480. count = DS1305_NVRAM_LEN - off;
  481. if (unlikely(!count))
  482. return count;
  483. addr = (DS1305_WRITE | DS1305_NVRAM) + off;
  484. msg_init(&m, x, &addr, count, buf, NULL);
  485. status = spi_sync(spi, &m);
  486. if (status < 0)
  487. dev_err(&spi->dev, "nvram %s error %d\n", "write", status);
  488. return (status < 0) ? status : count;
  489. }
  490. static struct bin_attribute nvram = {
  491. .attr.name = "nvram",
  492. .attr.mode = S_IRUGO | S_IWUSR,
  493. .read = ds1305_nvram_read,
  494. .write = ds1305_nvram_write,
  495. .size = DS1305_NVRAM_LEN,
  496. };
  497. /*----------------------------------------------------------------------*/
  498. /*
  499. * Interface to SPI stack
  500. */
  501. static int __devinit ds1305_probe(struct spi_device *spi)
  502. {
  503. struct ds1305 *ds1305;
  504. int status;
  505. u8 addr, value;
  506. struct ds1305_platform_data *pdata = spi->dev.platform_data;
  507. bool write_ctrl = false;
  508. /* Sanity check board setup data. This may be hooked up
  509. * in 3wire mode, but we don't care. Note that unless
  510. * there's an inverter in place, this needs SPI_CS_HIGH!
  511. */
  512. if ((spi->bits_per_word && spi->bits_per_word != 8)
  513. || (spi->max_speed_hz > 2000000)
  514. || !(spi->mode & SPI_CPHA))
  515. return -EINVAL;
  516. /* set up driver data */
  517. ds1305 = kzalloc(sizeof *ds1305, GFP_KERNEL);
  518. if (!ds1305)
  519. return -ENOMEM;
  520. ds1305->spi = spi;
  521. spi_set_drvdata(spi, ds1305);
  522. /* read and cache control registers */
  523. addr = DS1305_CONTROL;
  524. status = spi_write_then_read(spi, &addr, sizeof addr,
  525. ds1305->ctrl, sizeof ds1305->ctrl);
  526. if (status < 0) {
  527. dev_dbg(&spi->dev, "can't %s, %d\n",
  528. "read", status);
  529. goto fail0;
  530. }
  531. dev_dbg(&spi->dev, "ctrl %s: %02x %02x %02x\n",
  532. "read", ds1305->ctrl[0],
  533. ds1305->ctrl[1], ds1305->ctrl[2]);
  534. /* Sanity check register values ... partially compensating for the
  535. * fact that SPI has no device handshake. A pullup on MISO would
  536. * make these tests fail; but not all systems will have one. If
  537. * some register is neither 0x00 nor 0xff, a chip is likely there.
  538. */
  539. if ((ds1305->ctrl[0] & 0x38) != 0 || (ds1305->ctrl[1] & 0xfc) != 0) {
  540. dev_dbg(&spi->dev, "RTC chip is not present\n");
  541. status = -ENODEV;
  542. goto fail0;
  543. }
  544. if (ds1305->ctrl[2] == 0)
  545. dev_dbg(&spi->dev, "chip may not be present\n");
  546. /* enable writes if needed ... if we were paranoid it would
  547. * make sense to enable them only when absolutely necessary.
  548. */
  549. if (ds1305->ctrl[0] & DS1305_WP) {
  550. u8 buf[2];
  551. ds1305->ctrl[0] &= ~DS1305_WP;
  552. buf[0] = DS1305_WRITE | DS1305_CONTROL;
  553. buf[1] = ds1305->ctrl[0];
  554. status = spi_write_then_read(spi, buf, sizeof buf, NULL, 0);
  555. dev_dbg(&spi->dev, "clear WP --> %d\n", status);
  556. if (status < 0)
  557. goto fail0;
  558. }
  559. /* on DS1305, maybe start oscillator; like most low power
  560. * oscillators, it may take a second to stabilize
  561. */
  562. if (ds1305->ctrl[0] & DS1305_nEOSC) {
  563. ds1305->ctrl[0] &= ~DS1305_nEOSC;
  564. write_ctrl = true;
  565. dev_warn(&spi->dev, "SET TIME!\n");
  566. }
  567. /* ack any pending IRQs */
  568. if (ds1305->ctrl[1]) {
  569. ds1305->ctrl[1] = 0;
  570. write_ctrl = true;
  571. }
  572. /* this may need one-time (re)init */
  573. if (pdata) {
  574. /* maybe enable trickle charge */
  575. if (((ds1305->ctrl[2] & 0xf0) != DS1305_TRICKLE_MAGIC)) {
  576. ds1305->ctrl[2] = DS1305_TRICKLE_MAGIC
  577. | pdata->trickle;
  578. write_ctrl = true;
  579. }
  580. /* on DS1306, configure 1 Hz signal */
  581. if (pdata->is_ds1306) {
  582. if (pdata->en_1hz) {
  583. if (!(ds1305->ctrl[0] & DS1306_1HZ)) {
  584. ds1305->ctrl[0] |= DS1306_1HZ;
  585. write_ctrl = true;
  586. }
  587. } else {
  588. if (ds1305->ctrl[0] & DS1306_1HZ) {
  589. ds1305->ctrl[0] &= ~DS1306_1HZ;
  590. write_ctrl = true;
  591. }
  592. }
  593. }
  594. }
  595. if (write_ctrl) {
  596. u8 buf[4];
  597. buf[0] = DS1305_WRITE | DS1305_CONTROL;
  598. buf[1] = ds1305->ctrl[0];
  599. buf[2] = ds1305->ctrl[1];
  600. buf[3] = ds1305->ctrl[2];
  601. status = spi_write_then_read(spi, buf, sizeof buf, NULL, 0);
  602. if (status < 0) {
  603. dev_dbg(&spi->dev, "can't %s, %d\n",
  604. "write", status);
  605. goto fail0;
  606. }
  607. dev_dbg(&spi->dev, "ctrl %s: %02x %02x %02x\n",
  608. "write", ds1305->ctrl[0],
  609. ds1305->ctrl[1], ds1305->ctrl[2]);
  610. }
  611. /* see if non-Linux software set up AM/PM mode */
  612. addr = DS1305_HOUR;
  613. status = spi_write_then_read(spi, &addr, sizeof addr,
  614. &value, sizeof value);
  615. if (status < 0) {
  616. dev_dbg(&spi->dev, "read HOUR --> %d\n", status);
  617. goto fail0;
  618. }
  619. ds1305->hr12 = (DS1305_HR_12 & value) != 0;
  620. if (ds1305->hr12)
  621. dev_dbg(&spi->dev, "AM/PM\n");
  622. /* register RTC ... from here on, ds1305->ctrl needs locking */
  623. ds1305->rtc = rtc_device_register("ds1305", &spi->dev,
  624. &ds1305_ops, THIS_MODULE);
  625. if (IS_ERR(ds1305->rtc)) {
  626. status = PTR_ERR(ds1305->rtc);
  627. dev_dbg(&spi->dev, "register rtc --> %d\n", status);
  628. goto fail0;
  629. }
  630. /* Maybe set up alarm IRQ; be ready to handle it triggering right
  631. * away. NOTE that we don't share this. The signal is active low,
  632. * and we can't ack it before a SPI message delay. We temporarily
  633. * disable the IRQ until it's acked, which lets us work with more
  634. * IRQ trigger modes (not all IRQ controllers can do falling edge).
  635. */
  636. if (spi->irq) {
  637. INIT_WORK(&ds1305->work, ds1305_work);
  638. status = request_irq(spi->irq, ds1305_irq,
  639. 0, dev_name(&ds1305->rtc->dev), ds1305);
  640. if (status < 0) {
  641. dev_dbg(&spi->dev, "request_irq %d --> %d\n",
  642. spi->irq, status);
  643. goto fail1;
  644. }
  645. device_set_wakeup_capable(&spi->dev, 1);
  646. }
  647. /* export NVRAM */
  648. status = sysfs_create_bin_file(&spi->dev.kobj, &nvram);
  649. if (status < 0) {
  650. dev_dbg(&spi->dev, "register nvram --> %d\n", status);
  651. goto fail2;
  652. }
  653. return 0;
  654. fail2:
  655. free_irq(spi->irq, ds1305);
  656. fail1:
  657. rtc_device_unregister(ds1305->rtc);
  658. fail0:
  659. kfree(ds1305);
  660. return status;
  661. }
  662. static int __devexit ds1305_remove(struct spi_device *spi)
  663. {
  664. struct ds1305 *ds1305 = spi_get_drvdata(spi);
  665. sysfs_remove_bin_file(&spi->dev.kobj, &nvram);
  666. /* carefully shut down irq and workqueue, if present */
  667. if (spi->irq) {
  668. set_bit(FLAG_EXITING, &ds1305->flags);
  669. free_irq(spi->irq, ds1305);
  670. cancel_work_sync(&ds1305->work);
  671. }
  672. rtc_device_unregister(ds1305->rtc);
  673. spi_set_drvdata(spi, NULL);
  674. kfree(ds1305);
  675. return 0;
  676. }
  677. static struct spi_driver ds1305_driver = {
  678. .driver.name = "rtc-ds1305",
  679. .driver.owner = THIS_MODULE,
  680. .probe = ds1305_probe,
  681. .remove = __devexit_p(ds1305_remove),
  682. /* REVISIT add suspend/resume */
  683. };
  684. static int __init ds1305_init(void)
  685. {
  686. return spi_register_driver(&ds1305_driver);
  687. }
  688. module_init(ds1305_init);
  689. static void __exit ds1305_exit(void)
  690. {
  691. spi_unregister_driver(&ds1305_driver);
  692. }
  693. module_exit(ds1305_exit);
  694. MODULE_DESCRIPTION("RTC driver for DS1305 and DS1306 chips");
  695. MODULE_LICENSE("GPL");
  696. MODULE_ALIAS("spi:rtc-ds1305");