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