rtc-ds1305.c 20 KB

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