rtc-ds1305.c 19 KB

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