rtc-ds1374.c 14 KB

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