rtc-ds1305.c 20 KB

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