rtc-ds1305.c 20 KB

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