rtc-ds1305.c 22 KB

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