rtc-ds1374.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
  3. *
  4. * Based on code by Randy Vinson <rvinson@mvista.com>,
  5. * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
  6. *
  7. * Copyright (C) 2014 Rose Technology
  8. * Copyright (C) 2006-2007 Freescale Semiconductor
  9. *
  10. * 2005 (c) MontaVista Software, Inc. This file is licensed under
  11. * the terms of the GNU General Public License version 2. This program
  12. * is licensed "as is" without any warranty of any kind, whether express
  13. * or implied.
  14. */
  15. /*
  16. * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
  17. * recommended in .../Documentation/i2c/writing-clients.rst section
  18. * "Sending and receiving", using SMBus level communication is preferred.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/i2c.h>
  25. #include <linux/rtc.h>
  26. #include <linux/bcd.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/slab.h>
  29. #include <linux/pm.h>
  30. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  31. #include <linux/fs.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/reboot.h>
  35. #include <linux/watchdog.h>
  36. #endif
  37. #define DS1374_REG_TOD0 0x00 /* Time of Day */
  38. #define DS1374_REG_TOD1 0x01
  39. #define DS1374_REG_TOD2 0x02
  40. #define DS1374_REG_TOD3 0x03
  41. #define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */
  42. #define DS1374_REG_WDALM1 0x05
  43. #define DS1374_REG_WDALM2 0x06
  44. #define DS1374_REG_CR 0x07 /* Control */
  45. #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
  46. #define DS1374_REG_CR_WDSTR 0x08 /* 1=INT, 0=RST */
  47. #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
  48. #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
  49. #define DS1374_REG_SR 0x08 /* Status */
  50. #define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */
  51. #define DS1374_REG_SR_AF 0x01 /* Alarm Flag */
  52. #define DS1374_REG_TCR 0x09 /* Trickle Charge */
  53. static const struct i2c_device_id ds1374_id[] = {
  54. { "ds1374", 0 },
  55. { }
  56. };
  57. MODULE_DEVICE_TABLE(i2c, ds1374_id);
  58. #ifdef CONFIG_OF
  59. static const struct of_device_id ds1374_of_match[] = {
  60. { .compatible = "dallas,ds1374" },
  61. { }
  62. };
  63. MODULE_DEVICE_TABLE(of, ds1374_of_match);
  64. #endif
  65. struct ds1374 {
  66. struct i2c_client *client;
  67. struct rtc_device *rtc;
  68. struct work_struct work;
  69. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  70. struct watchdog_device wdt;
  71. #endif
  72. /* The mutex protects alarm operations, and prevents a race
  73. * between the enable_irq() in the workqueue and the free_irq()
  74. * in the remove function.
  75. */
  76. struct mutex mutex;
  77. int exiting;
  78. };
  79. static struct i2c_driver ds1374_driver;
  80. static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
  81. int reg, int nbytes)
  82. {
  83. u8 buf[4];
  84. int ret;
  85. int i;
  86. if (WARN_ON(nbytes > 4))
  87. return -EINVAL;
  88. ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
  89. if (ret < 0)
  90. return ret;
  91. if (ret < nbytes)
  92. return -EIO;
  93. for (i = nbytes - 1, *time = 0; i >= 0; i--)
  94. *time = (*time << 8) | buf[i];
  95. return 0;
  96. }
  97. static int ds1374_write_rtc(struct i2c_client *client, u32 time,
  98. int reg, int nbytes)
  99. {
  100. u8 buf[4];
  101. int i;
  102. if (nbytes > 4) {
  103. WARN_ON(1);
  104. return -EINVAL;
  105. }
  106. for (i = 0; i < nbytes; i++) {
  107. buf[i] = time & 0xff;
  108. time >>= 8;
  109. }
  110. return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
  111. }
  112. static int ds1374_check_rtc_status(struct i2c_client *client)
  113. {
  114. int ret = 0;
  115. int control, stat;
  116. stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  117. if (stat < 0)
  118. return stat;
  119. if (stat & DS1374_REG_SR_OSF)
  120. dev_warn(&client->dev,
  121. "oscillator discontinuity flagged, time unreliable\n");
  122. stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
  123. ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
  124. if (ret < 0)
  125. return ret;
  126. /* If the alarm is pending, clear it before requesting
  127. * the interrupt, so an interrupt event isn't reported
  128. * before everything is initialized.
  129. */
  130. control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  131. if (control < 0)
  132. return control;
  133. control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
  134. return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
  135. }
  136. static int ds1374_read_time(struct device *dev, struct rtc_time *time)
  137. {
  138. struct i2c_client *client = to_i2c_client(dev);
  139. u32 itime;
  140. int ret;
  141. ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
  142. if (!ret)
  143. rtc_time64_to_tm(itime, time);
  144. return ret;
  145. }
  146. static int ds1374_set_time(struct device *dev, struct rtc_time *time)
  147. {
  148. struct i2c_client *client = to_i2c_client(dev);
  149. unsigned long itime = rtc_tm_to_time64(time);
  150. return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
  151. }
  152. #ifndef CONFIG_RTC_DRV_DS1374_WDT
  153. /* The ds1374 has a decrementer for an alarm, rather than a comparator.
  154. * If the time of day is changed, then the alarm will need to be
  155. * reset.
  156. */
  157. static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  158. {
  159. struct i2c_client *client = to_i2c_client(dev);
  160. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  161. u32 now, cur_alarm;
  162. int cr, sr;
  163. int ret = 0;
  164. if (client->irq <= 0)
  165. return -EINVAL;
  166. mutex_lock(&ds1374->mutex);
  167. cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  168. if (ret < 0)
  169. goto out;
  170. sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  171. if (ret < 0)
  172. goto out;
  173. ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
  174. if (ret)
  175. goto out;
  176. ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
  177. if (ret)
  178. goto out;
  179. rtc_time64_to_tm(now + cur_alarm, &alarm->time);
  180. alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
  181. alarm->pending = !!(sr & DS1374_REG_SR_AF);
  182. out:
  183. mutex_unlock(&ds1374->mutex);
  184. return ret;
  185. }
  186. static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  187. {
  188. struct i2c_client *client = to_i2c_client(dev);
  189. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  190. struct rtc_time now;
  191. unsigned long new_alarm, itime;
  192. int cr;
  193. int ret = 0;
  194. if (client->irq <= 0)
  195. return -EINVAL;
  196. ret = ds1374_read_time(dev, &now);
  197. if (ret < 0)
  198. return ret;
  199. new_alarm = rtc_tm_to_time64(&alarm->time);
  200. itime = rtc_tm_to_time64(&now);
  201. /* This can happen due to races, in addition to dates that are
  202. * truly in the past. To avoid requiring the caller to check for
  203. * races, dates in the past are assumed to be in the recent past
  204. * (i.e. not something that we'd rather the caller know about via
  205. * an error), and the alarm is set to go off as soon as possible.
  206. */
  207. if (time_before_eq(new_alarm, itime))
  208. new_alarm = 1;
  209. else
  210. new_alarm -= itime;
  211. mutex_lock(&ds1374->mutex);
  212. ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  213. if (ret < 0)
  214. goto out;
  215. /* Disable any existing alarm before setting the new one
  216. * (or lack thereof). */
  217. cr &= ~DS1374_REG_CR_WACE;
  218. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  219. if (ret < 0)
  220. goto out;
  221. ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
  222. if (ret)
  223. goto out;
  224. if (alarm->enabled) {
  225. cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
  226. cr &= ~DS1374_REG_CR_WDALM;
  227. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  228. }
  229. out:
  230. mutex_unlock(&ds1374->mutex);
  231. return ret;
  232. }
  233. #endif
  234. static irqreturn_t ds1374_irq(int irq, void *dev_id)
  235. {
  236. struct i2c_client *client = dev_id;
  237. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  238. disable_irq_nosync(irq);
  239. schedule_work(&ds1374->work);
  240. return IRQ_HANDLED;
  241. }
  242. static void ds1374_work(struct work_struct *work)
  243. {
  244. struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
  245. struct i2c_client *client = ds1374->client;
  246. int stat, control;
  247. mutex_lock(&ds1374->mutex);
  248. stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
  249. if (stat < 0)
  250. goto unlock;
  251. if (stat & DS1374_REG_SR_AF) {
  252. stat &= ~DS1374_REG_SR_AF;
  253. i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
  254. control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  255. if (control < 0)
  256. goto out;
  257. control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
  258. i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
  259. rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
  260. }
  261. out:
  262. if (!ds1374->exiting)
  263. enable_irq(client->irq);
  264. unlock:
  265. mutex_unlock(&ds1374->mutex);
  266. }
  267. #ifndef CONFIG_RTC_DRV_DS1374_WDT
  268. static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
  269. {
  270. struct i2c_client *client = to_i2c_client(dev);
  271. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  272. int ret;
  273. mutex_lock(&ds1374->mutex);
  274. ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  275. if (ret < 0)
  276. goto out;
  277. if (enabled) {
  278. ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
  279. ret &= ~DS1374_REG_CR_WDALM;
  280. } else {
  281. ret &= ~DS1374_REG_CR_WACE;
  282. }
  283. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
  284. out:
  285. mutex_unlock(&ds1374->mutex);
  286. return ret;
  287. }
  288. #endif
  289. static const struct rtc_class_ops ds1374_rtc_ops = {
  290. .read_time = ds1374_read_time,
  291. .set_time = ds1374_set_time,
  292. #ifndef CONFIG_RTC_DRV_DS1374_WDT
  293. .read_alarm = ds1374_read_alarm,
  294. .set_alarm = ds1374_set_alarm,
  295. .alarm_irq_enable = ds1374_alarm_irq_enable,
  296. #endif
  297. };
  298. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  299. /*
  300. *****************************************************************************
  301. *
  302. * Watchdog Driver
  303. *
  304. *****************************************************************************
  305. */
  306. /* Default margin */
  307. #define TIMER_MARGIN_DEFAULT 32
  308. #define TIMER_MARGIN_MIN 1
  309. #define TIMER_MARGIN_MAX 4095 /* 24-bit value */
  310. static int wdt_margin;
  311. module_param(wdt_margin, int, 0);
  312. MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
  313. static bool nowayout = WATCHDOG_NOWAYOUT;
  314. module_param(nowayout, bool, 0);
  315. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default ="
  316. __MODULE_STRING(WATCHDOG_NOWAYOUT)")");
  317. static const struct watchdog_info ds1374_wdt_info = {
  318. .identity = "DS1374 Watchdog",
  319. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  320. WDIOF_MAGICCLOSE,
  321. };
  322. static int ds1374_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout)
  323. {
  324. struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
  325. struct i2c_client *client = ds1374->client;
  326. int ret, cr;
  327. wdt->timeout = timeout;
  328. cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  329. if (cr < 0)
  330. return cr;
  331. /* Disable any existing watchdog/alarm before setting the new one */
  332. cr &= ~DS1374_REG_CR_WACE;
  333. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  334. if (ret < 0)
  335. return ret;
  336. /* Set new watchdog time */
  337. timeout = timeout * 4096;
  338. ret = ds1374_write_rtc(client, timeout, DS1374_REG_WDALM0, 3);
  339. if (ret)
  340. return ret;
  341. /* Enable watchdog timer */
  342. cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
  343. cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */
  344. cr &= ~DS1374_REG_CR_AIE;
  345. ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  346. if (ret < 0)
  347. return ret;
  348. return 0;
  349. }
  350. /*
  351. * Reload the watchdog timer. (ie, pat the watchdog)
  352. */
  353. static int ds1374_wdt_start(struct watchdog_device *wdt)
  354. {
  355. struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
  356. u32 val;
  357. return ds1374_read_rtc(ds1374->client, &val, DS1374_REG_WDALM0, 3);
  358. }
  359. static int ds1374_wdt_stop(struct watchdog_device *wdt)
  360. {
  361. struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
  362. struct i2c_client *client = ds1374->client;
  363. int cr;
  364. cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
  365. if (cr < 0)
  366. return cr;
  367. /* Disable watchdog timer */
  368. cr &= ~DS1374_REG_CR_WACE;
  369. return i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
  370. }
  371. static const struct watchdog_ops ds1374_wdt_ops = {
  372. .owner = THIS_MODULE,
  373. .start = ds1374_wdt_start,
  374. .stop = ds1374_wdt_stop,
  375. .set_timeout = ds1374_wdt_settimeout,
  376. };
  377. #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
  378. /*
  379. *****************************************************************************
  380. *
  381. * Driver Interface
  382. *
  383. *****************************************************************************
  384. */
  385. static int ds1374_probe(struct i2c_client *client,
  386. const struct i2c_device_id *id)
  387. {
  388. struct ds1374 *ds1374;
  389. int ret;
  390. ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL);
  391. if (!ds1374)
  392. return -ENOMEM;
  393. ds1374->rtc = devm_rtc_allocate_device(&client->dev);
  394. if (IS_ERR(ds1374->rtc))
  395. return PTR_ERR(ds1374->rtc);
  396. ds1374->client = client;
  397. i2c_set_clientdata(client, ds1374);
  398. INIT_WORK(&ds1374->work, ds1374_work);
  399. mutex_init(&ds1374->mutex);
  400. ret = ds1374_check_rtc_status(client);
  401. if (ret)
  402. return ret;
  403. if (client->irq > 0) {
  404. ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
  405. "ds1374", client);
  406. if (ret) {
  407. dev_err(&client->dev, "unable to request IRQ\n");
  408. return ret;
  409. }
  410. device_set_wakeup_capable(&client->dev, 1);
  411. }
  412. ds1374->rtc->ops = &ds1374_rtc_ops;
  413. ds1374->rtc->range_max = U32_MAX;
  414. ret = rtc_register_device(ds1374->rtc);
  415. if (ret)
  416. return ret;
  417. #ifdef CONFIG_RTC_DRV_DS1374_WDT
  418. ds1374->wdt.info = &ds1374_wdt_info;
  419. ds1374->wdt.ops = &ds1374_wdt_ops;
  420. ds1374->wdt.timeout = TIMER_MARGIN_DEFAULT;
  421. ds1374->wdt.min_timeout = TIMER_MARGIN_MIN;
  422. ds1374->wdt.max_timeout = TIMER_MARGIN_MAX;
  423. watchdog_init_timeout(&ds1374->wdt, wdt_margin, &client->dev);
  424. watchdog_set_nowayout(&ds1374->wdt, nowayout);
  425. watchdog_stop_on_reboot(&ds1374->wdt);
  426. watchdog_stop_on_unregister(&ds1374->wdt);
  427. watchdog_set_drvdata(&ds1374->wdt, ds1374);
  428. ds1374_wdt_settimeout(&ds1374->wdt, ds1374->wdt.timeout);
  429. ret = devm_watchdog_register_device(&client->dev, &ds1374->wdt);
  430. if (ret)
  431. return ret;
  432. #endif
  433. return 0;
  434. }
  435. static int ds1374_remove(struct i2c_client *client)
  436. {
  437. struct ds1374 *ds1374 = i2c_get_clientdata(client);
  438. if (client->irq > 0) {
  439. mutex_lock(&ds1374->mutex);
  440. ds1374->exiting = 1;
  441. mutex_unlock(&ds1374->mutex);
  442. devm_free_irq(&client->dev, client->irq, client);
  443. cancel_work_sync(&ds1374->work);
  444. }
  445. return 0;
  446. }
  447. #ifdef CONFIG_PM_SLEEP
  448. static int ds1374_suspend(struct device *dev)
  449. {
  450. struct i2c_client *client = to_i2c_client(dev);
  451. if (client->irq > 0 && device_may_wakeup(&client->dev))
  452. enable_irq_wake(client->irq);
  453. return 0;
  454. }
  455. static int ds1374_resume(struct device *dev)
  456. {
  457. struct i2c_client *client = to_i2c_client(dev);
  458. if (client->irq > 0 && device_may_wakeup(&client->dev))
  459. disable_irq_wake(client->irq);
  460. return 0;
  461. }
  462. #endif
  463. static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
  464. static struct i2c_driver ds1374_driver = {
  465. .driver = {
  466. .name = "rtc-ds1374",
  467. .of_match_table = of_match_ptr(ds1374_of_match),
  468. .pm = &ds1374_pm,
  469. },
  470. .probe = ds1374_probe,
  471. .remove = ds1374_remove,
  472. .id_table = ds1374_id,
  473. };
  474. module_i2c_driver(ds1374_driver);
  475. MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
  476. MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
  477. MODULE_LICENSE("GPL");